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) 2013 by Delphix. All rights reserved.
26 /* Portions Copyright 2007 Jeremy Teo */
27 /* Portions Copyright 2010 Robert Milkowski */
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
37 #include <sys/vnode.h>
41 #include <sys/taskq.h>
43 #include <sys/atomic.h>
44 #include <sys/namei.h>
46 #include <sys/cmn_err.h>
47 #include <sys/errno.h>
48 #include <sys/unistd.h>
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_ioctl.h>
51 #include <sys/fs/zfs.h>
53 #include <sys/dmu_objset.h>
59 #include <sys/dirent.h>
60 #include <sys/policy.h>
61 #include <sys/sunddi.h>
62 #include <sys/filio.h>
64 #include <sys/zfs_ctldir.h>
65 #include <sys/zfs_fuid.h>
66 #include <sys/zfs_sa.h>
68 #include <sys/zfs_rlock.h>
69 #include <sys/extdirent.h>
70 #include <sys/kidmap.h>
73 #include <sys/sched.h>
79 * Each vnode op performs some logical unit of work. To do this, the ZPL must
80 * properly lock its in-core state, create a DMU transaction, do the work,
81 * record this work in the intent log (ZIL), commit the DMU transaction,
82 * and wait for the intent log to commit if it is a synchronous operation.
83 * Moreover, the vnode ops must work in both normal and log replay context.
84 * The ordering of events is important to avoid deadlocks and references
85 * to freed memory. The example below illustrates the following Big Rules:
87 * (1) A check must be made in each zfs thread for a mounted file system.
88 * This is done avoiding races using ZFS_ENTER(zfsvfs).
89 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
90 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
91 * can return EIO from the calling function.
93 * (2) VN_RELE() should always be the last thing except for zil_commit()
94 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
95 * First, if it's the last reference, the vnode/znode
96 * can be freed, so the zp may point to freed memory. Second, the last
97 * reference will call zfs_zinactive(), which may induce a lot of work --
98 * pushing cached pages (which acquires range locks) and syncing out
99 * cached atime changes. Third, zfs_zinactive() may require a new tx,
100 * which could deadlock the system if you were already holding one.
101 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
103 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
104 * as they can span dmu_tx_assign() calls.
106 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
107 * dmu_tx_assign(). This is critical because we don't want to block
108 * while holding locks.
110 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
111 * reduces lock contention and CPU usage when we must wait (note that if
112 * throughput is constrained by the storage, nearly every transaction
115 * Note, in particular, that if a lock is sometimes acquired before
116 * the tx assigns, and sometimes after (e.g. z_lock), then failing
117 * to use a non-blocking assign can deadlock the system. The scenario:
119 * Thread A has grabbed a lock before calling dmu_tx_assign().
120 * Thread B is in an already-assigned tx, and blocks for this lock.
121 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
122 * forever, because the previous txg can't quiesce until B's tx commits.
124 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
125 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
126 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
127 * to indicate that this operation has already called dmu_tx_wait().
128 * This will ensure that we don't retry forever, waiting a short bit
131 * (5) If the operation succeeded, generate the intent log entry for it
132 * before dropping locks. This ensures that the ordering of events
133 * in the intent log matches the order in which they actually occurred.
134 * During ZIL replay the zfs_log_* functions will update the sequence
135 * number to indicate the zil transaction has replayed.
137 * (6) At the end of each vnode op, the DMU tx must always commit,
138 * regardless of whether there were any errors.
140 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
141 * to ensure that synchronous semantics are provided when necessary.
143 * In general, this is how things should be ordered in each vnode op:
145 * ZFS_ENTER(zfsvfs); // exit if unmounted
147 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
148 * rw_enter(...); // grab any other locks you need
149 * tx = dmu_tx_create(...); // get DMU tx
150 * dmu_tx_hold_*(); // hold each object you might modify
151 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
153 * rw_exit(...); // drop locks
154 * zfs_dirent_unlock(dl); // unlock directory entry
155 * VN_RELE(...); // release held vnodes
156 * if (error == ERESTART) {
162 * dmu_tx_abort(tx); // abort DMU tx
163 * ZFS_EXIT(zfsvfs); // finished in zfs
164 * return (error); // really out of space
166 * error = do_real_work(); // do whatever this VOP does
168 * zfs_log_*(...); // on success, make ZIL entry
169 * dmu_tx_commit(tx); // commit DMU tx -- error or not
170 * rw_exit(...); // drop locks
171 * zfs_dirent_unlock(dl); // unlock directory entry
172 * VN_RELE(...); // release held vnodes
173 * zil_commit(zilog, foid); // synchronous when necessary
174 * ZFS_EXIT(zfsvfs); // finished in zfs
175 * return (error); // done, report error
180 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
182 znode_t *zp = VTOZ(*vpp);
183 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
188 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
189 ((flag & FAPPEND) == 0)) {
191 return (SET_ERROR(EPERM));
194 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
195 ZTOV(zp)->v_type == VREG &&
196 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
197 if (fs_vscan(*vpp, cr, 0) != 0) {
199 return (SET_ERROR(EACCES));
203 /* Keep a count of the synchronous opens in the znode */
204 if (flag & (FSYNC | FDSYNC))
205 atomic_inc_32(&zp->z_sync_cnt);
213 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
214 caller_context_t *ct)
216 znode_t *zp = VTOZ(vp);
217 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
220 * Clean up any locks held by this process on the vp.
222 cleanlocks(vp, ddi_get_pid(), 0);
223 cleanshares(vp, ddi_get_pid());
228 /* Decrement the synchronous opens in the znode */
229 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
230 atomic_dec_32(&zp->z_sync_cnt);
232 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
233 ZTOV(zp)->v_type == VREG &&
234 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
235 VERIFY(fs_vscan(vp, cr, 1) == 0);
242 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
243 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
246 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
248 znode_t *zp = VTOZ(vp);
249 uint64_t noff = (uint64_t)*off; /* new offset */
254 file_sz = zp->z_size;
255 if (noff >= file_sz) {
256 return (SET_ERROR(ENXIO));
259 if (cmd == _FIO_SEEK_HOLE)
264 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
267 if ((error == ESRCH) || (noff > file_sz)) {
269 * Handle the virtual hole at the end of file.
275 return (SET_ERROR(ENXIO));
286 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
287 int *rvalp, caller_context_t *ct)
299 * The following two ioctls are used by bfu. Faking out,
300 * necessary to avoid bfu errors.
309 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
310 return (SET_ERROR(EFAULT));
312 off = *(offset_t *)data;
315 zfsvfs = zp->z_zfsvfs;
319 /* offset parameter is in/out */
320 error = zfs_holey(vp, com, &off);
325 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
326 return (SET_ERROR(EFAULT));
328 *(offset_t *)data = off;
332 return (SET_ERROR(ENOTTY));
336 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
343 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
344 * aligned boundaries, if the range is not aligned. As a result a
345 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
346 * It may happen that all DEV_BSIZE subranges are marked clean and thus
347 * the whole page would be considred clean despite have some dirty data.
348 * For this reason we should shrink the range to DEV_BSIZE aligned
349 * boundaries before calling vm_page_clear_dirty.
351 end = rounddown2(off + nbytes, DEV_BSIZE);
352 off = roundup2(off, DEV_BSIZE);
356 VM_OBJECT_LOCK_ASSERT(obj, MA_OWNED);
359 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
361 if ((pp->oflags & VPO_BUSY) != 0) {
363 * Reference the page before unlocking and
364 * sleeping so that the page daemon is less
365 * likely to reclaim it.
367 vm_page_reference(pp);
368 vm_page_sleep(pp, "zfsmwb");
371 } else if (pp == NULL) {
372 pp = vm_page_alloc(obj, OFF_TO_IDX(start),
373 VM_ALLOC_SYSTEM | VM_ALLOC_IFCACHED |
376 ASSERT(pp != NULL && !pp->valid);
381 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
382 vm_object_pip_add(obj, 1);
383 vm_page_io_start(pp);
384 pmap_remove_write(pp);
386 vm_page_clear_dirty(pp, off, nbytes);
394 page_unbusy(vm_page_t pp)
397 vm_page_io_finish(pp);
398 vm_object_pip_subtract(pp->object, 1);
402 page_hold(vnode_t *vp, int64_t start)
408 VM_OBJECT_LOCK_ASSERT(obj, MA_OWNED);
411 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
413 if ((pp->oflags & VPO_BUSY) != 0) {
415 * Reference the page before unlocking and
416 * sleeping so that the page daemon is less
417 * likely to reclaim it.
419 vm_page_reference(pp);
420 vm_page_sleep(pp, "zfsmwb");
424 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
437 page_unhold(vm_page_t pp)
446 * When a file is memory mapped, we must keep the IO data synchronized
447 * between the DMU cache and the memory mapped pages. What this means:
449 * On Write: If we find a memory mapped page, we write to *both*
450 * the page and the dmu buffer.
453 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
454 int segflg, dmu_tx_t *tx)
461 ASSERT(segflg != UIO_NOCOPY);
462 ASSERT(vp->v_mount != NULL);
466 off = start & PAGEOFFSET;
468 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
470 int nbytes = imin(PAGESIZE - off, len);
472 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
473 VM_OBJECT_UNLOCK(obj);
475 va = zfs_map_page(pp, &sf);
476 (void) dmu_read(os, oid, start+off, nbytes,
477 va+off, DMU_READ_PREFETCH);;
486 vm_object_pip_wakeupn(obj, 0);
487 VM_OBJECT_UNLOCK(obj);
491 * Read with UIO_NOCOPY flag means that sendfile(2) requests
492 * ZFS to populate a range of page cache pages with data.
494 * NOTE: this function could be optimized to pre-allocate
495 * all pages in advance, drain VPO_BUSY on all of them,
496 * map them into contiguous KVA region and populate them
497 * in one single dmu_read() call.
500 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
502 znode_t *zp = VTOZ(vp);
503 objset_t *os = zp->z_zfsvfs->z_os;
513 ASSERT(uio->uio_segflg == UIO_NOCOPY);
514 ASSERT(vp->v_mount != NULL);
517 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
520 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
521 int bytes = MIN(PAGESIZE, len);
523 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_NOBUSY |
524 VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_IGN_SBUSY);
525 if (pp->valid == 0) {
526 vm_page_io_start(pp);
527 VM_OBJECT_UNLOCK(obj);
528 va = zfs_map_page(pp, &sf);
529 error = dmu_read(os, zp->z_id, start, bytes, va,
531 if (bytes != PAGESIZE && error == 0)
532 bzero(va + bytes, PAGESIZE - bytes);
535 vm_page_io_finish(pp);
538 if (pp->wire_count == 0 && pp->valid == 0 &&
539 pp->busy == 0 && !(pp->oflags & VPO_BUSY))
542 pp->valid = VM_PAGE_BITS_ALL;
543 vm_page_activate(pp);
547 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
551 uio->uio_resid -= bytes;
552 uio->uio_offset += bytes;
555 VM_OBJECT_UNLOCK(obj);
560 * When a file is memory mapped, we must keep the IO data synchronized
561 * between the DMU cache and the memory mapped pages. What this means:
563 * On Read: We "read" preferentially from memory mapped pages,
564 * else we default from the dmu buffer.
566 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
567 * the file is memory mapped.
570 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
572 znode_t *zp = VTOZ(vp);
573 objset_t *os = zp->z_zfsvfs->z_os;
581 ASSERT(vp->v_mount != NULL);
585 start = uio->uio_loffset;
586 off = start & PAGEOFFSET;
588 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
590 uint64_t bytes = MIN(PAGESIZE - off, len);
592 if (pp = page_hold(vp, start)) {
596 VM_OBJECT_UNLOCK(obj);
597 va = zfs_map_page(pp, &sf);
599 error = uiomove(va + off, bytes, UIO_READ, uio);
601 error = vn_io_fault_uiomove(va + off, bytes, uio);
607 VM_OBJECT_UNLOCK(obj);
608 error = dmu_read_uio(os, zp->z_id, uio, bytes);
616 VM_OBJECT_UNLOCK(obj);
620 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
623 * Read bytes from specified file into supplied buffer.
625 * IN: vp - vnode of file to be read from.
626 * uio - structure supplying read location, range info,
628 * ioflag - SYNC flags; used to provide FRSYNC semantics.
629 * cr - credentials of caller.
630 * ct - caller context
632 * OUT: uio - updated offset and range, buffer filled.
634 * RETURN: 0 on success, error code on failure.
637 * vp - atime updated if byte count > 0
641 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
643 znode_t *zp = VTOZ(vp);
644 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
655 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
657 return (SET_ERROR(EACCES));
661 * Validate file offset
663 if (uio->uio_loffset < (offset_t)0) {
665 return (SET_ERROR(EINVAL));
669 * Fasttrack empty reads
671 if (uio->uio_resid == 0) {
677 * Check for mandatory locks
679 if (MANDMODE(zp->z_mode)) {
680 if (error = chklock(vp, FREAD,
681 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
688 * If we're in FRSYNC mode, sync out this znode before reading it.
691 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
692 zil_commit(zfsvfs->z_log, zp->z_id);
695 * Lock the range against changes.
697 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
700 * If we are reading past end-of-file we can skip
701 * to the end; but we might still need to set atime.
703 if (uio->uio_loffset >= zp->z_size) {
708 ASSERT(uio->uio_loffset < zp->z_size);
709 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
712 if ((uio->uio_extflg == UIO_XUIO) &&
713 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
715 int blksz = zp->z_blksz;
716 uint64_t offset = uio->uio_loffset;
718 xuio = (xuio_t *)uio;
720 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
723 ASSERT(offset + n <= blksz);
726 (void) dmu_xuio_init(xuio, nblk);
728 if (vn_has_cached_data(vp)) {
730 * For simplicity, we always allocate a full buffer
731 * even if we only expect to read a portion of a block.
733 while (--nblk >= 0) {
734 (void) dmu_xuio_add(xuio,
735 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
743 nbytes = MIN(n, zfs_read_chunk_size -
744 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
747 if (uio->uio_segflg == UIO_NOCOPY)
748 error = mappedread_sf(vp, nbytes, uio);
750 #endif /* __FreeBSD__ */
751 if (vn_has_cached_data(vp))
752 error = mappedread(vp, nbytes, uio);
754 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
756 /* convert checksum errors into IO errors */
758 error = SET_ERROR(EIO);
765 zfs_range_unlock(rl);
767 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
773 * Write the bytes to a file.
775 * IN: vp - vnode of file to be written to.
776 * uio - structure supplying write location, range info,
778 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
779 * set if in append mode.
780 * cr - credentials of caller.
781 * ct - caller context (NFS/CIFS fem monitor only)
783 * OUT: uio - updated offset and range.
785 * RETURN: 0 on success, error code on failure.
788 * vp - ctime|mtime updated if byte count > 0
793 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
795 znode_t *zp = VTOZ(vp);
796 rlim64_t limit = MAXOFFSET_T;
797 ssize_t start_resid = uio->uio_resid;
801 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
806 int max_blksz = zfsvfs->z_max_blksz;
809 iovec_t *aiov = NULL;
812 int iovcnt = uio->uio_iovcnt;
813 iovec_t *iovp = uio->uio_iov;
816 sa_bulk_attr_t bulk[4];
817 uint64_t mtime[2], ctime[2];
820 * Fasttrack empty write
826 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
832 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
833 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
834 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
836 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
840 * If immutable or not appending then return EPERM
842 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
843 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
844 (uio->uio_loffset < zp->z_size))) {
846 return (SET_ERROR(EPERM));
849 zilog = zfsvfs->z_log;
852 * Validate file offset
854 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
857 return (SET_ERROR(EINVAL));
861 * Check for mandatory locks before calling zfs_range_lock()
862 * in order to prevent a deadlock with locks set via fcntl().
864 if (MANDMODE((mode_t)zp->z_mode) &&
865 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
872 * Pre-fault the pages to ensure slow (eg NFS) pages
874 * Skip this if uio contains loaned arc_buf.
876 if ((uio->uio_extflg == UIO_XUIO) &&
877 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
878 xuio = (xuio_t *)uio;
880 uio_prefaultpages(MIN(n, max_blksz), uio);
884 * If in append mode, set the io offset pointer to eof.
886 if (ioflag & FAPPEND) {
888 * Obtain an appending range lock to guarantee file append
889 * semantics. We reset the write offset once we have the lock.
891 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
893 if (rl->r_len == UINT64_MAX) {
895 * We overlocked the file because this write will cause
896 * the file block size to increase.
897 * Note that zp_size cannot change with this lock held.
901 uio->uio_loffset = woff;
904 * Note that if the file block size will change as a result of
905 * this write, then this range lock will lock the entire file
906 * so that we can re-write the block safely.
908 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
911 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
912 zfs_range_unlock(rl);
918 zfs_range_unlock(rl);
920 return (SET_ERROR(EFBIG));
923 if ((woff + n) > limit || woff > (limit - n))
926 /* Will this write extend the file length? */
927 write_eof = (woff + n > zp->z_size);
929 end_size = MAX(zp->z_size, woff + n);
932 * Write the file in reasonable size chunks. Each chunk is written
933 * in a separate transaction; this keeps the intent log records small
934 * and allows us to do more fine-grained space accounting.
938 woff = uio->uio_loffset;
939 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
940 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
942 dmu_return_arcbuf(abuf);
943 error = SET_ERROR(EDQUOT);
947 if (xuio && abuf == NULL) {
948 ASSERT(i_iov < iovcnt);
950 abuf = dmu_xuio_arcbuf(xuio, i_iov);
951 dmu_xuio_clear(xuio, i_iov);
952 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
953 iovec_t *, aiov, arc_buf_t *, abuf);
954 ASSERT((aiov->iov_base == abuf->b_data) ||
955 ((char *)aiov->iov_base - (char *)abuf->b_data +
956 aiov->iov_len == arc_buf_size(abuf)));
958 } else if (abuf == NULL && n >= max_blksz &&
959 woff >= zp->z_size &&
960 P2PHASE(woff, max_blksz) == 0 &&
961 zp->z_blksz == max_blksz) {
963 * This write covers a full block. "Borrow" a buffer
964 * from the dmu so that we can fill it before we enter
965 * a transaction. This avoids the possibility of
966 * holding up the transaction if the data copy hangs
967 * up on a pagefault (e.g., from an NFS server mapping).
973 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
975 ASSERT(abuf != NULL);
976 ASSERT(arc_buf_size(abuf) == max_blksz);
978 if (error = uiocopy(abuf->b_data, max_blksz,
979 UIO_WRITE, uio, &cbytes)) {
980 dmu_return_arcbuf(abuf);
983 ASSERT(cbytes == max_blksz);
985 ssize_t resid = uio->uio_resid;
986 error = vn_io_fault_uiomove(abuf->b_data, max_blksz, uio);
988 uio->uio_offset -= resid - uio->uio_resid;
989 uio->uio_resid = resid;
990 dmu_return_arcbuf(abuf);
997 * Start a transaction.
999 tx = dmu_tx_create(zfsvfs->z_os);
1000 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1001 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
1002 zfs_sa_upgrade_txholds(tx, zp);
1003 error = dmu_tx_assign(tx, TXG_WAIT);
1007 dmu_return_arcbuf(abuf);
1012 * If zfs_range_lock() over-locked we grow the blocksize
1013 * and then reduce the lock range. This will only happen
1014 * on the first iteration since zfs_range_reduce() will
1015 * shrink down r_len to the appropriate size.
1017 if (rl->r_len == UINT64_MAX) {
1020 if (zp->z_blksz > max_blksz) {
1021 ASSERT(!ISP2(zp->z_blksz));
1022 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
1024 new_blksz = MIN(end_size, max_blksz);
1026 zfs_grow_blocksize(zp, new_blksz, tx);
1027 zfs_range_reduce(rl, woff, n);
1031 * XXX - should we really limit each write to z_max_blksz?
1032 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1034 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1036 if (woff + nbytes > zp->z_size)
1037 vnode_pager_setsize(vp, woff + nbytes);
1040 tx_bytes = uio->uio_resid;
1041 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1043 tx_bytes -= uio->uio_resid;
1046 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1048 * If this is not a full block write, but we are
1049 * extending the file past EOF and this data starts
1050 * block-aligned, use assign_arcbuf(). Otherwise,
1051 * write via dmu_write().
1053 if (tx_bytes < max_blksz && (!write_eof ||
1054 aiov->iov_base != abuf->b_data)) {
1056 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1057 aiov->iov_len, aiov->iov_base, tx);
1058 dmu_return_arcbuf(abuf);
1059 xuio_stat_wbuf_copied();
1061 ASSERT(xuio || tx_bytes == max_blksz);
1062 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1066 ASSERT(tx_bytes <= uio->uio_resid);
1067 uioskip(uio, tx_bytes);
1070 if (tx_bytes && vn_has_cached_data(vp)) {
1071 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1072 zp->z_id, uio->uio_segflg, tx);
1076 * If we made no progress, we're done. If we made even
1077 * partial progress, update the znode and ZIL accordingly.
1079 if (tx_bytes == 0) {
1080 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1081 (void *)&zp->z_size, sizeof (uint64_t), tx);
1088 * Clear Set-UID/Set-GID bits on successful write if not
1089 * privileged and at least one of the excute bits is set.
1091 * It would be nice to to this after all writes have
1092 * been done, but that would still expose the ISUID/ISGID
1093 * to another app after the partial write is committed.
1095 * Note: we don't call zfs_fuid_map_id() here because
1096 * user 0 is not an ephemeral uid.
1098 mutex_enter(&zp->z_acl_lock);
1099 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1100 (S_IXUSR >> 6))) != 0 &&
1101 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1102 secpolicy_vnode_setid_retain(vp, cr,
1103 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1105 zp->z_mode &= ~(S_ISUID | S_ISGID);
1106 newmode = zp->z_mode;
1107 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1108 (void *)&newmode, sizeof (uint64_t), tx);
1110 mutex_exit(&zp->z_acl_lock);
1112 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1116 * Update the file size (zp_size) if it has changed;
1117 * account for possible concurrent updates.
1119 while ((end_size = zp->z_size) < uio->uio_loffset) {
1120 (void) atomic_cas_64(&zp->z_size, end_size,
1125 ASSERT(error == 0 || error == EFAULT);
1129 * If we are replaying and eof is non zero then force
1130 * the file size to the specified eof. Note, there's no
1131 * concurrency during replay.
1133 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1134 zp->z_size = zfsvfs->z_replay_eof;
1137 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1139 (void) sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1141 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1146 ASSERT(tx_bytes == nbytes);
1151 uio_prefaultpages(MIN(n, max_blksz), uio);
1155 zfs_range_unlock(rl);
1158 * If we're in replay mode, or we made no progress, return error.
1159 * Otherwise, it's at least a partial write, so it's successful.
1161 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1168 * EFAULT means that at least one page of the source buffer was not
1169 * available. VFS will re-try remaining I/O upon this error.
1171 if (error == EFAULT) {
1177 if (ioflag & (FSYNC | FDSYNC) ||
1178 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1179 zil_commit(zilog, zp->z_id);
1186 zfs_get_done(zgd_t *zgd, int error)
1188 znode_t *zp = zgd->zgd_private;
1189 objset_t *os = zp->z_zfsvfs->z_os;
1193 dmu_buf_rele(zgd->zgd_db, zgd);
1195 zfs_range_unlock(zgd->zgd_rl);
1197 vfslocked = VFS_LOCK_GIANT(zp->z_zfsvfs->z_vfs);
1199 * Release the vnode asynchronously as we currently have the
1200 * txg stopped from syncing.
1202 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1204 if (error == 0 && zgd->zgd_bp)
1205 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1207 kmem_free(zgd, sizeof (zgd_t));
1208 VFS_UNLOCK_GIANT(vfslocked);
1212 static int zil_fault_io = 0;
1216 * Get data to generate a TX_WRITE intent log record.
1219 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1221 zfsvfs_t *zfsvfs = arg;
1222 objset_t *os = zfsvfs->z_os;
1224 uint64_t object = lr->lr_foid;
1225 uint64_t offset = lr->lr_offset;
1226 uint64_t size = lr->lr_length;
1227 blkptr_t *bp = &lr->lr_blkptr;
1232 ASSERT(zio != NULL);
1236 * Nothing to do if the file has been removed
1238 if (zfs_zget(zfsvfs, object, &zp) != 0)
1239 return (SET_ERROR(ENOENT));
1240 if (zp->z_unlinked) {
1242 * Release the vnode asynchronously as we currently have the
1243 * txg stopped from syncing.
1245 VN_RELE_ASYNC(ZTOV(zp),
1246 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1247 return (SET_ERROR(ENOENT));
1250 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1251 zgd->zgd_zilog = zfsvfs->z_log;
1252 zgd->zgd_private = zp;
1255 * Write records come in two flavors: immediate and indirect.
1256 * For small writes it's cheaper to store the data with the
1257 * log record (immediate); for large writes it's cheaper to
1258 * sync the data and get a pointer to it (indirect) so that
1259 * we don't have to write the data twice.
1261 if (buf != NULL) { /* immediate write */
1262 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1263 /* test for truncation needs to be done while range locked */
1264 if (offset >= zp->z_size) {
1265 error = SET_ERROR(ENOENT);
1267 error = dmu_read(os, object, offset, size, buf,
1268 DMU_READ_NO_PREFETCH);
1270 ASSERT(error == 0 || error == ENOENT);
1271 } else { /* indirect write */
1273 * Have to lock the whole block to ensure when it's
1274 * written out and it's checksum is being calculated
1275 * that no one can change the data. We need to re-check
1276 * blocksize after we get the lock in case it's changed!
1281 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1283 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1285 if (zp->z_blksz == size)
1288 zfs_range_unlock(zgd->zgd_rl);
1290 /* test for truncation needs to be done while range locked */
1291 if (lr->lr_offset >= zp->z_size)
1292 error = SET_ERROR(ENOENT);
1295 error = SET_ERROR(EIO);
1300 error = dmu_buf_hold(os, object, offset, zgd, &db,
1301 DMU_READ_NO_PREFETCH);
1304 blkptr_t *obp = dmu_buf_get_blkptr(db);
1306 ASSERT(BP_IS_HOLE(bp));
1313 ASSERT(db->db_offset == offset);
1314 ASSERT(db->db_size == size);
1316 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1318 ASSERT(error || lr->lr_length <= zp->z_blksz);
1321 * On success, we need to wait for the write I/O
1322 * initiated by dmu_sync() to complete before we can
1323 * release this dbuf. We will finish everything up
1324 * in the zfs_get_done() callback.
1329 if (error == EALREADY) {
1330 lr->lr_common.lrc_txtype = TX_WRITE2;
1336 zfs_get_done(zgd, error);
1343 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1344 caller_context_t *ct)
1346 znode_t *zp = VTOZ(vp);
1347 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1353 if (flag & V_ACE_MASK)
1354 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1356 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1363 * If vnode is for a device return a specfs vnode instead.
1366 specvp_check(vnode_t **vpp, cred_t *cr)
1370 if (IS_DEVVP(*vpp)) {
1373 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1376 error = SET_ERROR(ENOSYS);
1384 * Lookup an entry in a directory, or an extended attribute directory.
1385 * If it exists, return a held vnode reference for it.
1387 * IN: dvp - vnode of directory to search.
1388 * nm - name of entry to lookup.
1389 * pnp - full pathname to lookup [UNUSED].
1390 * flags - LOOKUP_XATTR set if looking for an attribute.
1391 * rdir - root directory vnode [UNUSED].
1392 * cr - credentials of caller.
1393 * ct - caller context
1394 * direntflags - directory lookup flags
1395 * realpnp - returned pathname.
1397 * OUT: vpp - vnode of located entry, NULL if not found.
1399 * RETURN: 0 on success, error code on failure.
1406 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1407 int nameiop, cred_t *cr, kthread_t *td, int flags)
1409 znode_t *zdp = VTOZ(dvp);
1410 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1412 int *direntflags = NULL;
1413 void *realpnp = NULL;
1416 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1418 if (dvp->v_type != VDIR) {
1419 return (SET_ERROR(ENOTDIR));
1420 } else if (zdp->z_sa_hdl == NULL) {
1421 return (SET_ERROR(EIO));
1424 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1425 error = zfs_fastaccesschk_execute(zdp, cr);
1433 vnode_t *tvp = dnlc_lookup(dvp, nm);
1436 error = zfs_fastaccesschk_execute(zdp, cr);
1441 if (tvp == DNLC_NO_VNODE) {
1443 return (SET_ERROR(ENOENT));
1446 return (specvp_check(vpp, cr));
1452 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1459 if (flags & LOOKUP_XATTR) {
1462 * If the xattr property is off, refuse the lookup request.
1464 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1466 return (SET_ERROR(EINVAL));
1471 * We don't allow recursive attributes..
1472 * Maybe someday we will.
1474 if (zdp->z_pflags & ZFS_XATTR) {
1476 return (SET_ERROR(EINVAL));
1479 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1485 * Do we have permission to get into attribute directory?
1488 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1498 if (dvp->v_type != VDIR) {
1500 return (SET_ERROR(ENOTDIR));
1504 * Check accessibility of directory.
1507 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1512 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1513 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1515 return (SET_ERROR(EILSEQ));
1518 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1520 error = specvp_check(vpp, cr);
1522 /* Translate errors and add SAVENAME when needed. */
1523 if (cnp->cn_flags & ISLASTCN) {
1527 if (error == ENOENT) {
1528 error = EJUSTRETURN;
1529 cnp->cn_flags |= SAVENAME;
1535 cnp->cn_flags |= SAVENAME;
1539 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) {
1542 if (cnp->cn_flags & ISDOTDOT) {
1543 ltype = VOP_ISLOCKED(dvp);
1547 error = vn_lock(*vpp, cnp->cn_lkflags);
1548 if (cnp->cn_flags & ISDOTDOT)
1549 vn_lock(dvp, ltype | LK_RETRY);
1559 #ifdef FREEBSD_NAMECACHE
1561 * Insert name into cache (as non-existent) if appropriate.
1563 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
1564 cache_enter(dvp, *vpp, cnp);
1566 * Insert name into cache if appropriate.
1568 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1569 if (!(cnp->cn_flags & ISLASTCN) ||
1570 (nameiop != DELETE && nameiop != RENAME)) {
1571 cache_enter(dvp, *vpp, cnp);
1580 * Attempt to create a new entry in a directory. If the entry
1581 * already exists, truncate the file if permissible, else return
1582 * an error. Return the vp of the created or trunc'd file.
1584 * IN: dvp - vnode of directory to put new file entry in.
1585 * name - name of new file entry.
1586 * vap - attributes of new file.
1587 * excl - flag indicating exclusive or non-exclusive mode.
1588 * mode - mode to open file with.
1589 * cr - credentials of caller.
1590 * flag - large file flag [UNUSED].
1591 * ct - caller context
1592 * vsecp - ACL to be set
1594 * OUT: vpp - vnode of created or trunc'd entry.
1596 * RETURN: 0 on success, error code on failure.
1599 * dvp - ctime|mtime updated if new entry created
1600 * vp - ctime|mtime always, atime if new
1605 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1606 vnode_t **vpp, cred_t *cr, kthread_t *td)
1608 znode_t *zp, *dzp = VTOZ(dvp);
1609 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1617 gid_t gid = crgetgid(cr);
1618 zfs_acl_ids_t acl_ids;
1619 boolean_t fuid_dirtied;
1620 boolean_t have_acl = B_FALSE;
1621 boolean_t waited = B_FALSE;
1626 * If we have an ephemeral id, ACL, or XVATTR then
1627 * make sure file system is at proper version
1630 ksid = crgetsid(cr, KSID_OWNER);
1632 uid = ksid_getid(ksid);
1636 if (zfsvfs->z_use_fuids == B_FALSE &&
1637 (vsecp || (vap->va_mask & AT_XVATTR) ||
1638 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1639 return (SET_ERROR(EINVAL));
1644 zilog = zfsvfs->z_log;
1646 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1647 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1649 return (SET_ERROR(EILSEQ));
1652 if (vap->va_mask & AT_XVATTR) {
1653 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1654 crgetuid(cr), cr, vap->va_type)) != 0) {
1660 getnewvnode_reserve(1);
1665 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1666 vap->va_mode &= ~S_ISVTX;
1668 if (*name == '\0') {
1670 * Null component name refers to the directory itself.
1677 /* possible VN_HOLD(zp) */
1680 if (flag & FIGNORECASE)
1683 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1687 zfs_acl_ids_free(&acl_ids);
1688 if (strcmp(name, "..") == 0)
1689 error = SET_ERROR(EISDIR);
1690 getnewvnode_drop_reserve();
1700 * Create a new file object and update the directory
1703 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1705 zfs_acl_ids_free(&acl_ids);
1710 * We only support the creation of regular files in
1711 * extended attribute directories.
1714 if ((dzp->z_pflags & ZFS_XATTR) &&
1715 (vap->va_type != VREG)) {
1717 zfs_acl_ids_free(&acl_ids);
1718 error = SET_ERROR(EINVAL);
1722 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1723 cr, vsecp, &acl_ids)) != 0)
1727 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1728 zfs_acl_ids_free(&acl_ids);
1729 error = SET_ERROR(EDQUOT);
1733 tx = dmu_tx_create(os);
1735 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1736 ZFS_SA_BASE_ATTR_SIZE);
1738 fuid_dirtied = zfsvfs->z_fuid_dirty;
1740 zfs_fuid_txhold(zfsvfs, tx);
1741 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1742 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1743 if (!zfsvfs->z_use_sa &&
1744 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1745 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1746 0, acl_ids.z_aclp->z_acl_bytes);
1748 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1750 zfs_dirent_unlock(dl);
1751 if (error == ERESTART) {
1757 zfs_acl_ids_free(&acl_ids);
1759 getnewvnode_drop_reserve();
1763 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1766 zfs_fuid_sync(zfsvfs, tx);
1768 (void) zfs_link_create(dl, zp, tx, ZNEW);
1769 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1770 if (flag & FIGNORECASE)
1772 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1773 vsecp, acl_ids.z_fuidp, vap);
1774 zfs_acl_ids_free(&acl_ids);
1777 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1780 zfs_acl_ids_free(&acl_ids);
1784 * A directory entry already exists for this name.
1787 * Can't truncate an existing file if in exclusive mode.
1790 error = SET_ERROR(EEXIST);
1794 * Can't open a directory for writing.
1796 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1797 error = SET_ERROR(EISDIR);
1801 * Verify requested access to file.
1803 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1807 mutex_enter(&dzp->z_lock);
1809 mutex_exit(&dzp->z_lock);
1812 * Truncate regular files if requested.
1814 if ((ZTOV(zp)->v_type == VREG) &&
1815 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1816 /* we can't hold any locks when calling zfs_freesp() */
1817 zfs_dirent_unlock(dl);
1819 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1821 vnevent_create(ZTOV(zp), ct);
1826 getnewvnode_drop_reserve();
1828 zfs_dirent_unlock(dl);
1835 error = specvp_check(vpp, cr);
1838 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1839 zil_commit(zilog, 0);
1846 * Remove an entry from a directory.
1848 * IN: dvp - vnode of directory to remove entry from.
1849 * name - name of entry to remove.
1850 * cr - credentials of caller.
1851 * ct - caller context
1852 * flags - case flags
1854 * RETURN: 0 on success, error code on failure.
1858 * vp - ctime (if nlink > 0)
1861 uint64_t null_xattr = 0;
1865 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1868 znode_t *zp, *dzp = VTOZ(dvp);
1871 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1873 uint64_t acl_obj, xattr_obj;
1874 uint64_t xattr_obj_unlinked = 0;
1878 boolean_t may_delete_now, delete_now = FALSE;
1879 boolean_t unlinked, toobig = FALSE;
1881 pathname_t *realnmp = NULL;
1885 boolean_t waited = B_FALSE;
1889 zilog = zfsvfs->z_log;
1891 if (flags & FIGNORECASE) {
1901 * Attempt to lock directory; fail if entry doesn't exist.
1903 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1913 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1918 * Need to use rmdir for removing directories.
1920 if (vp->v_type == VDIR) {
1921 error = SET_ERROR(EPERM);
1925 vnevent_remove(vp, dvp, name, ct);
1928 dnlc_remove(dvp, realnmp->pn_buf);
1930 dnlc_remove(dvp, name);
1933 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1937 * We may delete the znode now, or we may put it in the unlinked set;
1938 * it depends on whether we're the last link, and on whether there are
1939 * other holds on the vnode. So we dmu_tx_hold() the right things to
1940 * allow for either case.
1943 tx = dmu_tx_create(zfsvfs->z_os);
1944 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1945 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1946 zfs_sa_upgrade_txholds(tx, zp);
1947 zfs_sa_upgrade_txholds(tx, dzp);
1948 if (may_delete_now) {
1950 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1951 /* if the file is too big, only hold_free a token amount */
1952 dmu_tx_hold_free(tx, zp->z_id, 0,
1953 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1956 /* are there any extended attributes? */
1957 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1958 &xattr_obj, sizeof (xattr_obj));
1959 if (error == 0 && xattr_obj) {
1960 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1962 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1963 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1966 mutex_enter(&zp->z_lock);
1967 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1968 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1969 mutex_exit(&zp->z_lock);
1971 /* charge as an update -- would be nice not to charge at all */
1972 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1974 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1976 zfs_dirent_unlock(dl);
1980 if (error == ERESTART) {
1994 * Remove the directory entry.
1996 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
2006 * Hold z_lock so that we can make sure that the ACL obj
2007 * hasn't changed. Could have been deleted due to
2010 mutex_enter(&zp->z_lock);
2012 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2013 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
2014 delete_now = may_delete_now && !toobig &&
2015 vp->v_count == 1 && !vn_has_cached_data(vp) &&
2016 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
2023 panic("zfs_remove: delete_now branch taken");
2025 if (xattr_obj_unlinked) {
2026 ASSERT3U(xzp->z_links, ==, 2);
2027 mutex_enter(&xzp->z_lock);
2028 xzp->z_unlinked = 1;
2030 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
2031 &xzp->z_links, sizeof (xzp->z_links), tx);
2032 ASSERT3U(error, ==, 0);
2033 mutex_exit(&xzp->z_lock);
2034 zfs_unlinked_add(xzp, tx);
2037 error = sa_remove(zp->z_sa_hdl,
2038 SA_ZPL_XATTR(zfsvfs), tx);
2040 error = sa_update(zp->z_sa_hdl,
2041 SA_ZPL_XATTR(zfsvfs), &null_xattr,
2042 sizeof (uint64_t), tx);
2047 ASSERT0(vp->v_count);
2049 mutex_exit(&zp->z_lock);
2050 zfs_znode_delete(zp, tx);
2051 } else if (unlinked) {
2052 mutex_exit(&zp->z_lock);
2053 zfs_unlinked_add(zp, tx);
2055 vp->v_vflag |= VV_NOSYNC;
2060 if (flags & FIGNORECASE)
2062 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2069 zfs_dirent_unlock(dl);
2076 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2077 zil_commit(zilog, 0);
2084 * Create a new directory and insert it into dvp using the name
2085 * provided. Return a pointer to the inserted directory.
2087 * IN: dvp - vnode of directory to add subdir to.
2088 * dirname - name of new directory.
2089 * vap - attributes of new directory.
2090 * cr - credentials of caller.
2091 * ct - caller context
2092 * flags - case flags
2093 * vsecp - ACL to be set
2095 * OUT: vpp - vnode of created directory.
2097 * RETURN: 0 on success, error code on failure.
2100 * dvp - ctime|mtime updated
2101 * vp - ctime|mtime|atime updated
2105 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
2106 caller_context_t *ct, int flags, vsecattr_t *vsecp)
2108 znode_t *zp, *dzp = VTOZ(dvp);
2109 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2118 gid_t gid = crgetgid(cr);
2119 zfs_acl_ids_t acl_ids;
2120 boolean_t fuid_dirtied;
2121 boolean_t waited = B_FALSE;
2123 ASSERT(vap->va_type == VDIR);
2126 * If we have an ephemeral id, ACL, or XVATTR then
2127 * make sure file system is at proper version
2130 ksid = crgetsid(cr, KSID_OWNER);
2132 uid = ksid_getid(ksid);
2135 if (zfsvfs->z_use_fuids == B_FALSE &&
2136 (vsecp || (vap->va_mask & AT_XVATTR) ||
2137 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2138 return (SET_ERROR(EINVAL));
2142 zilog = zfsvfs->z_log;
2144 if (dzp->z_pflags & ZFS_XATTR) {
2146 return (SET_ERROR(EINVAL));
2149 if (zfsvfs->z_utf8 && u8_validate(dirname,
2150 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2152 return (SET_ERROR(EILSEQ));
2154 if (flags & FIGNORECASE)
2157 if (vap->va_mask & AT_XVATTR) {
2158 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2159 crgetuid(cr), cr, vap->va_type)) != 0) {
2165 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2166 vsecp, &acl_ids)) != 0) {
2171 getnewvnode_reserve(1);
2174 * First make sure the new directory doesn't exist.
2176 * Existence is checked first to make sure we don't return
2177 * EACCES instead of EEXIST which can cause some applications
2183 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
2185 zfs_acl_ids_free(&acl_ids);
2186 getnewvnode_drop_reserve();
2191 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2192 zfs_acl_ids_free(&acl_ids);
2193 zfs_dirent_unlock(dl);
2194 getnewvnode_drop_reserve();
2199 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2200 zfs_acl_ids_free(&acl_ids);
2201 zfs_dirent_unlock(dl);
2202 getnewvnode_drop_reserve();
2204 return (SET_ERROR(EDQUOT));
2208 * Add a new entry to the directory.
2210 tx = dmu_tx_create(zfsvfs->z_os);
2211 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2212 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2213 fuid_dirtied = zfsvfs->z_fuid_dirty;
2215 zfs_fuid_txhold(zfsvfs, tx);
2216 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2217 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2218 acl_ids.z_aclp->z_acl_bytes);
2221 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2222 ZFS_SA_BASE_ATTR_SIZE);
2224 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2226 zfs_dirent_unlock(dl);
2227 if (error == ERESTART) {
2233 zfs_acl_ids_free(&acl_ids);
2235 getnewvnode_drop_reserve();
2243 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2246 zfs_fuid_sync(zfsvfs, tx);
2249 * Now put new name in parent dir.
2251 (void) zfs_link_create(dl, zp, tx, ZNEW);
2255 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2256 if (flags & FIGNORECASE)
2258 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2259 acl_ids.z_fuidp, vap);
2261 zfs_acl_ids_free(&acl_ids);
2265 getnewvnode_drop_reserve();
2267 zfs_dirent_unlock(dl);
2269 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2270 zil_commit(zilog, 0);
2277 * Remove a directory subdir entry. If the current working
2278 * directory is the same as the subdir to be removed, the
2281 * IN: dvp - vnode of directory to remove from.
2282 * name - name of directory to be removed.
2283 * cwd - vnode of current working directory.
2284 * cr - credentials of caller.
2285 * ct - caller context
2286 * flags - case flags
2288 * RETURN: 0 on success, error code on failure.
2291 * dvp - ctime|mtime updated
2295 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2296 caller_context_t *ct, int flags)
2298 znode_t *dzp = VTOZ(dvp);
2301 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2307 boolean_t waited = B_FALSE;
2311 zilog = zfsvfs->z_log;
2313 if (flags & FIGNORECASE)
2319 * Attempt to lock directory; fail if entry doesn't exist.
2321 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2329 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2333 if (vp->v_type != VDIR) {
2334 error = SET_ERROR(ENOTDIR);
2339 error = SET_ERROR(EINVAL);
2343 vnevent_rmdir(vp, dvp, name, ct);
2346 * Grab a lock on the directory to make sure that noone is
2347 * trying to add (or lookup) entries while we are removing it.
2349 rw_enter(&zp->z_name_lock, RW_WRITER);
2352 * Grab a lock on the parent pointer to make sure we play well
2353 * with the treewalk and directory rename code.
2355 rw_enter(&zp->z_parent_lock, RW_WRITER);
2357 tx = dmu_tx_create(zfsvfs->z_os);
2358 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2359 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2360 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2361 zfs_sa_upgrade_txholds(tx, zp);
2362 zfs_sa_upgrade_txholds(tx, dzp);
2363 dmu_tx_mark_netfree(tx);
2364 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2366 rw_exit(&zp->z_parent_lock);
2367 rw_exit(&zp->z_name_lock);
2368 zfs_dirent_unlock(dl);
2370 if (error == ERESTART) {
2381 #ifdef FREEBSD_NAMECACHE
2385 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2388 uint64_t txtype = TX_RMDIR;
2389 if (flags & FIGNORECASE)
2391 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2396 rw_exit(&zp->z_parent_lock);
2397 rw_exit(&zp->z_name_lock);
2398 #ifdef FREEBSD_NAMECACHE
2402 zfs_dirent_unlock(dl);
2406 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2407 zil_commit(zilog, 0);
2414 * Read as many directory entries as will fit into the provided
2415 * buffer from the given directory cursor position (specified in
2416 * the uio structure).
2418 * IN: vp - vnode of directory to read.
2419 * uio - structure supplying read location, range info,
2420 * and return buffer.
2421 * cr - credentials of caller.
2422 * ct - caller context
2423 * flags - case flags
2425 * OUT: uio - updated offset and range, buffer filled.
2426 * eofp - set to true if end-of-file detected.
2428 * RETURN: 0 on success, error code on failure.
2431 * vp - atime updated
2433 * Note that the low 4 bits of the cookie returned by zap is always zero.
2434 * This allows us to use the low range for "special" directory entries:
2435 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2436 * we use the offset 2 for the '.zfs' directory.
2440 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2442 znode_t *zp = VTOZ(vp);
2446 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2451 zap_attribute_t zap;
2452 uint_t bytes_wanted;
2453 uint64_t offset; /* must be unsigned; checks for < 1 */
2459 boolean_t check_sysattrs;
2462 u_long *cooks = NULL;
2468 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2469 &parent, sizeof (parent))) != 0) {
2475 * If we are not given an eof variable,
2482 * Check for valid iov_len.
2484 if (uio->uio_iov->iov_len <= 0) {
2486 return (SET_ERROR(EINVAL));
2490 * Quit if directory has been removed (posix)
2492 if ((*eofp = zp->z_unlinked) != 0) {
2499 offset = uio->uio_loffset;
2500 prefetch = zp->z_zn_prefetch;
2503 * Initialize the iterator cursor.
2507 * Start iteration from the beginning of the directory.
2509 zap_cursor_init(&zc, os, zp->z_id);
2512 * The offset is a serialized cursor.
2514 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2518 * Get space to change directory entries into fs independent format.
2520 iovp = uio->uio_iov;
2521 bytes_wanted = iovp->iov_len;
2522 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2523 bufsize = bytes_wanted;
2524 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2525 odp = (struct dirent64 *)outbuf;
2527 bufsize = bytes_wanted;
2529 odp = (struct dirent64 *)iovp->iov_base;
2531 eodp = (struct edirent *)odp;
2533 if (ncookies != NULL) {
2535 * Minimum entry size is dirent size and 1 byte for a file name.
2537 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2538 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2543 * If this VFS supports the system attribute view interface; and
2544 * we're looking at an extended attribute directory; and we care
2545 * about normalization conflicts on this vfs; then we must check
2546 * for normalization conflicts with the sysattr name space.
2549 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2550 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2551 (flags & V_RDDIR_ENTFLAGS);
2557 * Transform to file-system independent format
2560 while (outcount < bytes_wanted) {
2563 off64_t *next = NULL;
2566 * Special case `.', `..', and `.zfs'.
2569 (void) strcpy(zap.za_name, ".");
2570 zap.za_normalization_conflict = 0;
2573 } else if (offset == 1) {
2574 (void) strcpy(zap.za_name, "..");
2575 zap.za_normalization_conflict = 0;
2578 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2579 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2580 zap.za_normalization_conflict = 0;
2581 objnum = ZFSCTL_INO_ROOT;
2587 if (error = zap_cursor_retrieve(&zc, &zap)) {
2588 if ((*eofp = (error == ENOENT)) != 0)
2594 if (zap.za_integer_length != 8 ||
2595 zap.za_num_integers != 1) {
2596 cmn_err(CE_WARN, "zap_readdir: bad directory "
2597 "entry, obj = %lld, offset = %lld\n",
2598 (u_longlong_t)zp->z_id,
2599 (u_longlong_t)offset);
2600 error = SET_ERROR(ENXIO);
2604 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2606 * MacOS X can extract the object type here such as:
2607 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2609 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2611 if (check_sysattrs && !zap.za_normalization_conflict) {
2613 zap.za_normalization_conflict =
2614 xattr_sysattr_casechk(zap.za_name);
2616 panic("%s:%u: TODO", __func__, __LINE__);
2621 if (flags & V_RDDIR_ACCFILTER) {
2623 * If we have no access at all, don't include
2624 * this entry in the returned information
2627 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2629 if (!zfs_has_access(ezp, cr)) {
2636 if (flags & V_RDDIR_ENTFLAGS)
2637 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2639 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2642 * Will this entry fit in the buffer?
2644 if (outcount + reclen > bufsize) {
2646 * Did we manage to fit anything in the buffer?
2649 error = SET_ERROR(EINVAL);
2654 if (flags & V_RDDIR_ENTFLAGS) {
2656 * Add extended flag entry:
2658 eodp->ed_ino = objnum;
2659 eodp->ed_reclen = reclen;
2660 /* NOTE: ed_off is the offset for the *next* entry */
2661 next = &(eodp->ed_off);
2662 eodp->ed_eflags = zap.za_normalization_conflict ?
2663 ED_CASE_CONFLICT : 0;
2664 (void) strncpy(eodp->ed_name, zap.za_name,
2665 EDIRENT_NAMELEN(reclen));
2666 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2671 odp->d_ino = objnum;
2672 odp->d_reclen = reclen;
2673 odp->d_namlen = strlen(zap.za_name);
2674 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2676 odp = (dirent64_t *)((intptr_t)odp + reclen);
2680 ASSERT(outcount <= bufsize);
2682 /* Prefetch znode */
2684 dmu_prefetch(os, objnum, 0, 0);
2688 * Move to the next entry, fill in the previous offset.
2690 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2691 zap_cursor_advance(&zc);
2692 offset = zap_cursor_serialize(&zc);
2697 if (cooks != NULL) {
2700 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2703 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2705 /* Subtract unused cookies */
2706 if (ncookies != NULL)
2707 *ncookies -= ncooks;
2709 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2710 iovp->iov_base += outcount;
2711 iovp->iov_len -= outcount;
2712 uio->uio_resid -= outcount;
2713 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2715 * Reset the pointer.
2717 offset = uio->uio_loffset;
2721 zap_cursor_fini(&zc);
2722 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2723 kmem_free(outbuf, bufsize);
2725 if (error == ENOENT)
2728 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2730 uio->uio_loffset = offset;
2732 if (error != 0 && cookies != NULL) {
2733 free(*cookies, M_TEMP);
2740 ulong_t zfs_fsync_sync_cnt = 4;
2743 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2745 znode_t *zp = VTOZ(vp);
2746 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2748 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2750 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2753 zil_commit(zfsvfs->z_log, zp->z_id);
2761 * Get the requested file attributes and place them in the provided
2764 * IN: vp - vnode of file.
2765 * vap - va_mask identifies requested attributes.
2766 * If AT_XVATTR set, then optional attrs are requested
2767 * flags - ATTR_NOACLCHECK (CIFS server context)
2768 * cr - credentials of caller.
2769 * ct - caller context
2771 * OUT: vap - attribute values.
2773 * RETURN: 0 (always succeeds).
2777 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2778 caller_context_t *ct)
2780 znode_t *zp = VTOZ(vp);
2781 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2784 u_longlong_t nblocks;
2786 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2787 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2788 xoptattr_t *xoap = NULL;
2789 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2790 sa_bulk_attr_t bulk[4];
2796 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2798 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2799 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2800 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2801 if (vp->v_type == VBLK || vp->v_type == VCHR)
2802 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2805 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2811 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2812 * Also, if we are the owner don't bother, since owner should
2813 * always be allowed to read basic attributes of file.
2815 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2816 (vap->va_uid != crgetuid(cr))) {
2817 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2825 * Return all attributes. It's cheaper to provide the answer
2826 * than to determine whether we were asked the question.
2829 mutex_enter(&zp->z_lock);
2830 vap->va_type = IFTOVT(zp->z_mode);
2831 vap->va_mode = zp->z_mode & ~S_IFMT;
2833 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2835 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2837 vap->va_nodeid = zp->z_id;
2838 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2839 links = zp->z_links + 1;
2841 links = zp->z_links;
2842 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */
2843 vap->va_size = zp->z_size;
2845 vap->va_rdev = vp->v_rdev;
2847 if (vp->v_type == VBLK || vp->v_type == VCHR)
2848 vap->va_rdev = zfs_cmpldev(rdev);
2850 vap->va_seq = zp->z_seq;
2851 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2852 vap->va_filerev = zp->z_seq;
2855 * Add in any requested optional attributes and the create time.
2856 * Also set the corresponding bits in the returned attribute bitmap.
2858 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2859 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2861 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2862 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2865 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2866 xoap->xoa_readonly =
2867 ((zp->z_pflags & ZFS_READONLY) != 0);
2868 XVA_SET_RTN(xvap, XAT_READONLY);
2871 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2873 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2874 XVA_SET_RTN(xvap, XAT_SYSTEM);
2877 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2879 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2880 XVA_SET_RTN(xvap, XAT_HIDDEN);
2883 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2884 xoap->xoa_nounlink =
2885 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2886 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2889 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2890 xoap->xoa_immutable =
2891 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2892 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2895 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2896 xoap->xoa_appendonly =
2897 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2898 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2901 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2903 ((zp->z_pflags & ZFS_NODUMP) != 0);
2904 XVA_SET_RTN(xvap, XAT_NODUMP);
2907 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2909 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2910 XVA_SET_RTN(xvap, XAT_OPAQUE);
2913 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2914 xoap->xoa_av_quarantined =
2915 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2916 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2919 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2920 xoap->xoa_av_modified =
2921 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2922 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2925 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2926 vp->v_type == VREG) {
2927 zfs_sa_get_scanstamp(zp, xvap);
2930 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2933 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2934 times, sizeof (times));
2935 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2936 XVA_SET_RTN(xvap, XAT_CREATETIME);
2939 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2940 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2941 XVA_SET_RTN(xvap, XAT_REPARSE);
2943 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2944 xoap->xoa_generation = zp->z_gen;
2945 XVA_SET_RTN(xvap, XAT_GEN);
2948 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2950 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2951 XVA_SET_RTN(xvap, XAT_OFFLINE);
2954 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2956 ((zp->z_pflags & ZFS_SPARSE) != 0);
2957 XVA_SET_RTN(xvap, XAT_SPARSE);
2961 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2962 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2963 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2964 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2966 mutex_exit(&zp->z_lock);
2968 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2969 vap->va_blksize = blksize;
2970 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2972 if (zp->z_blksz == 0) {
2974 * Block size hasn't been set; suggest maximal I/O transfers.
2976 vap->va_blksize = zfsvfs->z_max_blksz;
2984 * Set the file attributes to the values contained in the
2987 * IN: vp - vnode of file to be modified.
2988 * vap - new attribute values.
2989 * If AT_XVATTR set, then optional attrs are being set
2990 * flags - ATTR_UTIME set if non-default time values provided.
2991 * - ATTR_NOACLCHECK (CIFS context only).
2992 * cr - credentials of caller.
2993 * ct - caller context
2995 * RETURN: 0 on success, error code on failure.
2998 * vp - ctime updated, mtime updated if size changed.
3002 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
3003 caller_context_t *ct)
3005 znode_t *zp = VTOZ(vp);
3006 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3011 uint_t mask = vap->va_mask;
3012 uint_t saved_mask = 0;
3013 uint64_t saved_mode;
3016 uint64_t new_uid, new_gid;
3018 uint64_t mtime[2], ctime[2];
3020 int need_policy = FALSE;
3022 zfs_fuid_info_t *fuidp = NULL;
3023 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
3026 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
3027 boolean_t fuid_dirtied = B_FALSE;
3028 sa_bulk_attr_t bulk[7], xattr_bulk[7];
3029 int count = 0, xattr_count = 0;
3034 if (mask & AT_NOSET)
3035 return (SET_ERROR(EINVAL));
3040 zilog = zfsvfs->z_log;
3043 * Make sure that if we have ephemeral uid/gid or xvattr specified
3044 * that file system is at proper version level
3047 if (zfsvfs->z_use_fuids == B_FALSE &&
3048 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
3049 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
3050 (mask & AT_XVATTR))) {
3052 return (SET_ERROR(EINVAL));
3055 if (mask & AT_SIZE && vp->v_type == VDIR) {
3057 return (SET_ERROR(EISDIR));
3060 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
3062 return (SET_ERROR(EINVAL));
3066 * If this is an xvattr_t, then get a pointer to the structure of
3067 * optional attributes. If this is NULL, then we have a vattr_t.
3069 xoap = xva_getxoptattr(xvap);
3071 xva_init(&tmpxvattr);
3074 * Immutable files can only alter immutable bit and atime
3076 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
3077 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
3078 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
3080 return (SET_ERROR(EPERM));
3083 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
3085 return (SET_ERROR(EPERM));
3089 * Verify timestamps doesn't overflow 32 bits.
3090 * ZFS can handle large timestamps, but 32bit syscalls can't
3091 * handle times greater than 2039. This check should be removed
3092 * once large timestamps are fully supported.
3094 if (mask & (AT_ATIME | AT_MTIME)) {
3095 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
3096 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
3098 return (SET_ERROR(EOVERFLOW));
3106 /* Can this be moved to before the top label? */
3107 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
3109 return (SET_ERROR(EROFS));
3113 * First validate permissions
3116 if (mask & AT_SIZE) {
3118 * XXX - Note, we are not providing any open
3119 * mode flags here (like FNDELAY), so we may
3120 * block if there are locks present... this
3121 * should be addressed in openat().
3123 /* XXX - would it be OK to generate a log record here? */
3124 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3131 if (mask & (AT_ATIME|AT_MTIME) ||
3132 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3133 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3134 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3135 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3136 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3137 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3138 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3139 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3143 if (mask & (AT_UID|AT_GID)) {
3144 int idmask = (mask & (AT_UID|AT_GID));
3149 * NOTE: even if a new mode is being set,
3150 * we may clear S_ISUID/S_ISGID bits.
3153 if (!(mask & AT_MODE))
3154 vap->va_mode = zp->z_mode;
3157 * Take ownership or chgrp to group we are a member of
3160 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3161 take_group = (mask & AT_GID) &&
3162 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3165 * If both AT_UID and AT_GID are set then take_owner and
3166 * take_group must both be set in order to allow taking
3169 * Otherwise, send the check through secpolicy_vnode_setattr()
3173 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3174 ((idmask == AT_UID) && take_owner) ||
3175 ((idmask == AT_GID) && take_group)) {
3176 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3177 skipaclchk, cr) == 0) {
3179 * Remove setuid/setgid for non-privileged users
3181 secpolicy_setid_clear(vap, vp, cr);
3182 trim_mask = (mask & (AT_UID|AT_GID));
3191 mutex_enter(&zp->z_lock);
3192 oldva.va_mode = zp->z_mode;
3193 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3194 if (mask & AT_XVATTR) {
3196 * Update xvattr mask to include only those attributes
3197 * that are actually changing.
3199 * the bits will be restored prior to actually setting
3200 * the attributes so the caller thinks they were set.
3202 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3203 if (xoap->xoa_appendonly !=
3204 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3207 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3208 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3212 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3213 if (xoap->xoa_nounlink !=
3214 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3217 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3218 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3222 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3223 if (xoap->xoa_immutable !=
3224 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3227 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3228 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3232 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3233 if (xoap->xoa_nodump !=
3234 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3237 XVA_CLR_REQ(xvap, XAT_NODUMP);
3238 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3242 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3243 if (xoap->xoa_av_modified !=
3244 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3247 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3248 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3252 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3253 if ((vp->v_type != VREG &&
3254 xoap->xoa_av_quarantined) ||
3255 xoap->xoa_av_quarantined !=
3256 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3259 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3260 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3264 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3265 mutex_exit(&zp->z_lock);
3267 return (SET_ERROR(EPERM));
3270 if (need_policy == FALSE &&
3271 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3272 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3277 mutex_exit(&zp->z_lock);
3279 if (mask & AT_MODE) {
3280 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3281 err = secpolicy_setid_setsticky_clear(vp, vap,
3287 trim_mask |= AT_MODE;
3295 * If trim_mask is set then take ownership
3296 * has been granted or write_acl is present and user
3297 * has the ability to modify mode. In that case remove
3298 * UID|GID and or MODE from mask so that
3299 * secpolicy_vnode_setattr() doesn't revoke it.
3303 saved_mask = vap->va_mask;
3304 vap->va_mask &= ~trim_mask;
3305 if (trim_mask & AT_MODE) {
3307 * Save the mode, as secpolicy_vnode_setattr()
3308 * will overwrite it with ova.va_mode.
3310 saved_mode = vap->va_mode;
3313 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3314 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3321 vap->va_mask |= saved_mask;
3322 if (trim_mask & AT_MODE) {
3324 * Recover the mode after
3325 * secpolicy_vnode_setattr().
3327 vap->va_mode = saved_mode;
3333 * secpolicy_vnode_setattr, or take ownership may have
3336 mask = vap->va_mask;
3338 if ((mask & (AT_UID | AT_GID))) {
3339 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3340 &xattr_obj, sizeof (xattr_obj));
3342 if (err == 0 && xattr_obj) {
3343 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3347 if (mask & AT_UID) {
3348 new_uid = zfs_fuid_create(zfsvfs,
3349 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3350 if (new_uid != zp->z_uid &&
3351 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3353 VN_RELE(ZTOV(attrzp));
3354 err = SET_ERROR(EDQUOT);
3359 if (mask & AT_GID) {
3360 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3361 cr, ZFS_GROUP, &fuidp);
3362 if (new_gid != zp->z_gid &&
3363 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3365 VN_RELE(ZTOV(attrzp));
3366 err = SET_ERROR(EDQUOT);
3371 tx = dmu_tx_create(zfsvfs->z_os);
3373 if (mask & AT_MODE) {
3374 uint64_t pmode = zp->z_mode;
3376 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3378 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3379 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3380 err = SET_ERROR(EPERM);
3384 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3387 mutex_enter(&zp->z_lock);
3388 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3390 * Are we upgrading ACL from old V0 format
3393 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3394 zfs_znode_acl_version(zp) ==
3395 ZFS_ACL_VERSION_INITIAL) {
3396 dmu_tx_hold_free(tx, acl_obj, 0,
3398 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3399 0, aclp->z_acl_bytes);
3401 dmu_tx_hold_write(tx, acl_obj, 0,
3404 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3405 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3406 0, aclp->z_acl_bytes);
3408 mutex_exit(&zp->z_lock);
3409 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3411 if ((mask & AT_XVATTR) &&
3412 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3413 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3415 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3419 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3422 fuid_dirtied = zfsvfs->z_fuid_dirty;
3424 zfs_fuid_txhold(zfsvfs, tx);
3426 zfs_sa_upgrade_txholds(tx, zp);
3428 err = dmu_tx_assign(tx, TXG_WAIT);
3434 * Set each attribute requested.
3435 * We group settings according to the locks they need to acquire.
3437 * Note: you cannot set ctime directly, although it will be
3438 * updated as a side-effect of calling this function.
3442 if (mask & (AT_UID|AT_GID|AT_MODE))
3443 mutex_enter(&zp->z_acl_lock);
3444 mutex_enter(&zp->z_lock);
3446 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3447 &zp->z_pflags, sizeof (zp->z_pflags));
3450 if (mask & (AT_UID|AT_GID|AT_MODE))
3451 mutex_enter(&attrzp->z_acl_lock);
3452 mutex_enter(&attrzp->z_lock);
3453 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3454 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3455 sizeof (attrzp->z_pflags));
3458 if (mask & (AT_UID|AT_GID)) {
3460 if (mask & AT_UID) {
3461 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3462 &new_uid, sizeof (new_uid));
3463 zp->z_uid = new_uid;
3465 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3466 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3468 attrzp->z_uid = new_uid;
3472 if (mask & AT_GID) {
3473 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3474 NULL, &new_gid, sizeof (new_gid));
3475 zp->z_gid = new_gid;
3477 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3478 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3480 attrzp->z_gid = new_gid;
3483 if (!(mask & AT_MODE)) {
3484 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3485 NULL, &new_mode, sizeof (new_mode));
3486 new_mode = zp->z_mode;
3488 err = zfs_acl_chown_setattr(zp);
3491 err = zfs_acl_chown_setattr(attrzp);
3496 if (mask & AT_MODE) {
3497 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3498 &new_mode, sizeof (new_mode));
3499 zp->z_mode = new_mode;
3500 ASSERT3U((uintptr_t)aclp, !=, 0);
3501 err = zfs_aclset_common(zp, aclp, cr, tx);
3503 if (zp->z_acl_cached)
3504 zfs_acl_free(zp->z_acl_cached);
3505 zp->z_acl_cached = aclp;
3510 if (mask & AT_ATIME) {
3511 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3512 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3513 &zp->z_atime, sizeof (zp->z_atime));
3516 if (mask & AT_MTIME) {
3517 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3518 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3519 mtime, sizeof (mtime));
3522 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3523 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3524 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3525 NULL, mtime, sizeof (mtime));
3526 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3527 &ctime, sizeof (ctime));
3528 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3530 } else if (mask != 0) {
3531 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3532 &ctime, sizeof (ctime));
3533 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3536 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3537 SA_ZPL_CTIME(zfsvfs), NULL,
3538 &ctime, sizeof (ctime));
3539 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3540 mtime, ctime, B_TRUE);
3544 * Do this after setting timestamps to prevent timestamp
3545 * update from toggling bit
3548 if (xoap && (mask & AT_XVATTR)) {
3551 * restore trimmed off masks
3552 * so that return masks can be set for caller.
3555 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3556 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3558 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3559 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3561 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3562 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3564 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3565 XVA_SET_REQ(xvap, XAT_NODUMP);
3567 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3568 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3570 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3571 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3574 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3575 ASSERT(vp->v_type == VREG);
3577 zfs_xvattr_set(zp, xvap, tx);
3581 zfs_fuid_sync(zfsvfs, tx);
3584 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3586 mutex_exit(&zp->z_lock);
3587 if (mask & (AT_UID|AT_GID|AT_MODE))
3588 mutex_exit(&zp->z_acl_lock);
3591 if (mask & (AT_UID|AT_GID|AT_MODE))
3592 mutex_exit(&attrzp->z_acl_lock);
3593 mutex_exit(&attrzp->z_lock);
3596 if (err == 0 && attrzp) {
3597 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3603 VN_RELE(ZTOV(attrzp));
3609 zfs_fuid_info_free(fuidp);
3615 if (err == ERESTART)
3618 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3623 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3624 zil_commit(zilog, 0);
3630 typedef struct zfs_zlock {
3631 krwlock_t *zl_rwlock; /* lock we acquired */
3632 znode_t *zl_znode; /* znode we held */
3633 struct zfs_zlock *zl_next; /* next in list */
3637 * Drop locks and release vnodes that were held by zfs_rename_lock().
3640 zfs_rename_unlock(zfs_zlock_t **zlpp)
3644 while ((zl = *zlpp) != NULL) {
3645 if (zl->zl_znode != NULL)
3646 VN_RELE(ZTOV(zl->zl_znode));
3647 rw_exit(zl->zl_rwlock);
3648 *zlpp = zl->zl_next;
3649 kmem_free(zl, sizeof (*zl));
3654 * Search back through the directory tree, using the ".." entries.
3655 * Lock each directory in the chain to prevent concurrent renames.
3656 * Fail any attempt to move a directory into one of its own descendants.
3657 * XXX - z_parent_lock can overlap with map or grow locks
3660 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3664 uint64_t rootid = zp->z_zfsvfs->z_root;
3665 uint64_t oidp = zp->z_id;
3666 krwlock_t *rwlp = &szp->z_parent_lock;
3667 krw_t rw = RW_WRITER;
3670 * First pass write-locks szp and compares to zp->z_id.
3671 * Later passes read-lock zp and compare to zp->z_parent.
3674 if (!rw_tryenter(rwlp, rw)) {
3676 * Another thread is renaming in this path.
3677 * Note that if we are a WRITER, we don't have any
3678 * parent_locks held yet.
3680 if (rw == RW_READER && zp->z_id > szp->z_id) {
3682 * Drop our locks and restart
3684 zfs_rename_unlock(&zl);
3688 rwlp = &szp->z_parent_lock;
3693 * Wait for other thread to drop its locks
3699 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3700 zl->zl_rwlock = rwlp;
3701 zl->zl_znode = NULL;
3702 zl->zl_next = *zlpp;
3705 if (oidp == szp->z_id) /* We're a descendant of szp */
3706 return (SET_ERROR(EINVAL));
3708 if (oidp == rootid) /* We've hit the top */
3711 if (rw == RW_READER) { /* i.e. not the first pass */
3712 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3717 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3718 &oidp, sizeof (oidp));
3719 rwlp = &zp->z_parent_lock;
3722 } while (zp->z_id != sdzp->z_id);
3728 * Move an entry from the provided source directory to the target
3729 * directory. Change the entry name as indicated.
3731 * IN: sdvp - Source directory containing the "old entry".
3732 * snm - Old entry name.
3733 * tdvp - Target directory to contain the "new entry".
3734 * tnm - New entry name.
3735 * cr - credentials of caller.
3736 * ct - caller context
3737 * flags - case flags
3739 * RETURN: 0 on success, error code on failure.
3742 * sdvp,tdvp - ctime|mtime updated
3746 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3747 caller_context_t *ct, int flags)
3749 znode_t *tdzp, *szp, *tzp;
3750 znode_t *sdzp = VTOZ(sdvp);
3751 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3754 zfs_dirlock_t *sdl, *tdl;
3757 int cmp, serr, terr;
3760 boolean_t waited = B_FALSE;
3763 ZFS_VERIFY_ZP(sdzp);
3764 zilog = zfsvfs->z_log;
3767 * Make sure we have the real vp for the target directory.
3769 if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3772 if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3774 return (SET_ERROR(EXDEV));
3778 ZFS_VERIFY_ZP(tdzp);
3779 if (zfsvfs->z_utf8 && u8_validate(tnm,
3780 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3782 return (SET_ERROR(EILSEQ));
3785 if (flags & FIGNORECASE)
3794 * This is to prevent the creation of links into attribute space
3795 * by renaming a linked file into/outof an attribute directory.
3796 * See the comment in zfs_link() for why this is considered bad.
3798 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3800 return (SET_ERROR(EINVAL));
3804 * Lock source and target directory entries. To prevent deadlock,
3805 * a lock ordering must be defined. We lock the directory with
3806 * the smallest object id first, or if it's a tie, the one with
3807 * the lexically first name.
3809 if (sdzp->z_id < tdzp->z_id) {
3811 } else if (sdzp->z_id > tdzp->z_id) {
3815 * First compare the two name arguments without
3816 * considering any case folding.
3818 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3820 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3821 ASSERT(error == 0 || !zfsvfs->z_utf8);
3824 * POSIX: "If the old argument and the new argument
3825 * both refer to links to the same existing file,
3826 * the rename() function shall return successfully
3827 * and perform no other action."
3833 * If the file system is case-folding, then we may
3834 * have some more checking to do. A case-folding file
3835 * system is either supporting mixed case sensitivity
3836 * access or is completely case-insensitive. Note
3837 * that the file system is always case preserving.
3839 * In mixed sensitivity mode case sensitive behavior
3840 * is the default. FIGNORECASE must be used to
3841 * explicitly request case insensitive behavior.
3843 * If the source and target names provided differ only
3844 * by case (e.g., a request to rename 'tim' to 'Tim'),
3845 * we will treat this as a special case in the
3846 * case-insensitive mode: as long as the source name
3847 * is an exact match, we will allow this to proceed as
3848 * a name-change request.
3850 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3851 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3852 flags & FIGNORECASE)) &&
3853 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3856 * case preserving rename request, require exact
3865 * If the source and destination directories are the same, we should
3866 * grab the z_name_lock of that directory only once.
3870 rw_enter(&sdzp->z_name_lock, RW_READER);
3874 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3875 ZEXISTS | zflg, NULL, NULL);
3876 terr = zfs_dirent_lock(&tdl,
3877 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3879 terr = zfs_dirent_lock(&tdl,
3880 tdzp, tnm, &tzp, zflg, NULL, NULL);
3881 serr = zfs_dirent_lock(&sdl,
3882 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3888 * Source entry invalid or not there.
3891 zfs_dirent_unlock(tdl);
3897 rw_exit(&sdzp->z_name_lock);
3900 * FreeBSD: In OpenSolaris they only check if rename source is
3901 * ".." here, because "." is handled in their lookup. This is
3902 * not the case for FreeBSD, so we check for "." explicitly.
3904 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0)
3905 serr = SET_ERROR(EINVAL);
3910 zfs_dirent_unlock(sdl);
3914 rw_exit(&sdzp->z_name_lock);
3916 if (strcmp(tnm, "..") == 0)
3917 terr = SET_ERROR(EINVAL);
3923 * Must have write access at the source to remove the old entry
3924 * and write access at the target to create the new entry.
3925 * Note that if target and source are the same, this can be
3926 * done in a single check.
3929 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3932 if (ZTOV(szp)->v_type == VDIR) {
3934 * Check to make sure rename is valid.
3935 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3937 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3942 * Does target exist?
3946 * Source and target must be the same type.
3948 if (ZTOV(szp)->v_type == VDIR) {
3949 if (ZTOV(tzp)->v_type != VDIR) {
3950 error = SET_ERROR(ENOTDIR);
3954 if (ZTOV(tzp)->v_type == VDIR) {
3955 error = SET_ERROR(EISDIR);
3960 * POSIX dictates that when the source and target
3961 * entries refer to the same file object, rename
3962 * must do nothing and exit without error.
3964 if (szp->z_id == tzp->z_id) {
3970 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3972 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3975 * notify the target directory if it is not the same
3976 * as source directory.
3979 vnevent_rename_dest_dir(tdvp, ct);
3982 tx = dmu_tx_create(zfsvfs->z_os);
3983 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3984 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3985 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3986 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3988 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3989 zfs_sa_upgrade_txholds(tx, tdzp);
3992 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3993 zfs_sa_upgrade_txholds(tx, tzp);
3996 zfs_sa_upgrade_txholds(tx, szp);
3997 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3998 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4001 zfs_rename_unlock(&zl);
4002 zfs_dirent_unlock(sdl);
4003 zfs_dirent_unlock(tdl);
4006 rw_exit(&sdzp->z_name_lock);
4011 if (error == ERESTART) {
4022 if (tzp) /* Attempt to remove the existing target */
4023 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
4026 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
4028 szp->z_pflags |= ZFS_AV_MODIFIED;
4030 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
4031 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
4034 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
4036 zfs_log_rename(zilog, tx, TX_RENAME |
4037 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
4038 sdl->dl_name, tdzp, tdl->dl_name, szp);
4041 * Update path information for the target vnode
4043 vn_renamepath(tdvp, ZTOV(szp), tnm,
4047 * At this point, we have successfully created
4048 * the target name, but have failed to remove
4049 * the source name. Since the create was done
4050 * with the ZRENAMING flag, there are
4051 * complications; for one, the link count is
4052 * wrong. The easiest way to deal with this
4053 * is to remove the newly created target, and
4054 * return the original error. This must
4055 * succeed; fortunately, it is very unlikely to
4056 * fail, since we just created it.
4058 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
4059 ZRENAMING, NULL), ==, 0);
4062 #ifdef FREEBSD_NAMECACHE
4066 cache_purge(ZTOV(szp));
4068 cache_purge(ZTOV(tzp));
4076 zfs_rename_unlock(&zl);
4078 zfs_dirent_unlock(sdl);
4079 zfs_dirent_unlock(tdl);
4082 rw_exit(&sdzp->z_name_lock);
4089 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4090 zil_commit(zilog, 0);
4098 * Insert the indicated symbolic reference entry into the directory.
4100 * IN: dvp - Directory to contain new symbolic link.
4101 * link - Name for new symlink entry.
4102 * vap - Attributes of new entry.
4103 * cr - credentials of caller.
4104 * ct - caller context
4105 * flags - case flags
4107 * RETURN: 0 on success, error code on failure.
4110 * dvp - ctime|mtime updated
4114 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
4115 cred_t *cr, kthread_t *td)
4117 znode_t *zp, *dzp = VTOZ(dvp);
4120 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4122 uint64_t len = strlen(link);
4125 zfs_acl_ids_t acl_ids;
4126 boolean_t fuid_dirtied;
4127 uint64_t txtype = TX_SYMLINK;
4128 boolean_t waited = B_FALSE;
4131 ASSERT(vap->va_type == VLNK);
4135 zilog = zfsvfs->z_log;
4137 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4138 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4140 return (SET_ERROR(EILSEQ));
4142 if (flags & FIGNORECASE)
4145 if (len > MAXPATHLEN) {
4147 return (SET_ERROR(ENAMETOOLONG));
4150 if ((error = zfs_acl_ids_create(dzp, 0,
4151 vap, cr, NULL, &acl_ids)) != 0) {
4156 getnewvnode_reserve(1);
4160 * Attempt to lock directory; fail if entry already exists.
4162 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
4164 zfs_acl_ids_free(&acl_ids);
4165 getnewvnode_drop_reserve();
4170 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4171 zfs_acl_ids_free(&acl_ids);
4172 zfs_dirent_unlock(dl);
4173 getnewvnode_drop_reserve();
4178 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4179 zfs_acl_ids_free(&acl_ids);
4180 zfs_dirent_unlock(dl);
4181 getnewvnode_drop_reserve();
4183 return (SET_ERROR(EDQUOT));
4185 tx = dmu_tx_create(zfsvfs->z_os);
4186 fuid_dirtied = zfsvfs->z_fuid_dirty;
4187 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4188 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4189 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4190 ZFS_SA_BASE_ATTR_SIZE + len);
4191 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4192 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4193 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4194 acl_ids.z_aclp->z_acl_bytes);
4197 zfs_fuid_txhold(zfsvfs, tx);
4198 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4200 zfs_dirent_unlock(dl);
4201 if (error == ERESTART) {
4207 zfs_acl_ids_free(&acl_ids);
4209 getnewvnode_drop_reserve();
4215 * Create a new object for the symlink.
4216 * for version 4 ZPL datsets the symlink will be an SA attribute
4218 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4221 zfs_fuid_sync(zfsvfs, tx);
4223 mutex_enter(&zp->z_lock);
4225 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4228 zfs_sa_symlink(zp, link, len, tx);
4229 mutex_exit(&zp->z_lock);
4232 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4233 &zp->z_size, sizeof (zp->z_size), tx);
4235 * Insert the new object into the directory.
4237 (void) zfs_link_create(dl, zp, tx, ZNEW);
4239 if (flags & FIGNORECASE)
4241 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4244 zfs_acl_ids_free(&acl_ids);
4248 getnewvnode_drop_reserve();
4250 zfs_dirent_unlock(dl);
4252 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4253 zil_commit(zilog, 0);
4260 * Return, in the buffer contained in the provided uio structure,
4261 * the symbolic path referred to by vp.
4263 * IN: vp - vnode of symbolic link.
4264 * uio - structure to contain the link path.
4265 * cr - credentials of caller.
4266 * ct - caller context
4268 * OUT: uio - structure containing the link path.
4270 * RETURN: 0 on success, error code on failure.
4273 * vp - atime updated
4277 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4279 znode_t *zp = VTOZ(vp);
4280 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4286 mutex_enter(&zp->z_lock);
4288 error = sa_lookup_uio(zp->z_sa_hdl,
4289 SA_ZPL_SYMLINK(zfsvfs), uio);
4291 error = zfs_sa_readlink(zp, uio);
4292 mutex_exit(&zp->z_lock);
4294 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4301 * Insert a new entry into directory tdvp referencing svp.
4303 * IN: tdvp - Directory to contain new entry.
4304 * svp - vnode of new entry.
4305 * name - name of new entry.
4306 * cr - credentials of caller.
4307 * ct - caller context
4309 * RETURN: 0 on success, error code on failure.
4312 * tdvp - ctime|mtime updated
4313 * svp - ctime updated
4317 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4318 caller_context_t *ct, int flags)
4320 znode_t *dzp = VTOZ(tdvp);
4322 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4331 boolean_t waited = B_FALSE;
4333 ASSERT(tdvp->v_type == VDIR);
4337 zilog = zfsvfs->z_log;
4339 if (VOP_REALVP(svp, &realvp, ct) == 0)
4343 * POSIX dictates that we return EPERM here.
4344 * Better choices include ENOTSUP or EISDIR.
4346 if (svp->v_type == VDIR) {
4348 return (SET_ERROR(EPERM));
4351 if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) {
4353 return (SET_ERROR(EXDEV));
4359 /* Prevent links to .zfs/shares files */
4361 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4362 &parent, sizeof (uint64_t))) != 0) {
4366 if (parent == zfsvfs->z_shares_dir) {
4368 return (SET_ERROR(EPERM));
4371 if (zfsvfs->z_utf8 && u8_validate(name,
4372 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4374 return (SET_ERROR(EILSEQ));
4376 if (flags & FIGNORECASE)
4380 * We do not support links between attributes and non-attributes
4381 * because of the potential security risk of creating links
4382 * into "normal" file space in order to circumvent restrictions
4383 * imposed in attribute space.
4385 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4387 return (SET_ERROR(EINVAL));
4391 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4392 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4394 return (SET_ERROR(EPERM));
4397 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4404 * Attempt to lock directory; fail if entry already exists.
4406 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4412 tx = dmu_tx_create(zfsvfs->z_os);
4413 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4414 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4415 zfs_sa_upgrade_txholds(tx, szp);
4416 zfs_sa_upgrade_txholds(tx, dzp);
4417 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4419 zfs_dirent_unlock(dl);
4420 if (error == ERESTART) {
4431 error = zfs_link_create(dl, szp, tx, 0);
4434 uint64_t txtype = TX_LINK;
4435 if (flags & FIGNORECASE)
4437 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4442 zfs_dirent_unlock(dl);
4445 vnevent_link(svp, ct);
4448 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4449 zil_commit(zilog, 0);
4457 * zfs_null_putapage() is used when the file system has been force
4458 * unmounted. It just drops the pages.
4462 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4463 size_t *lenp, int flags, cred_t *cr)
4465 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4470 * Push a page out to disk, klustering if possible.
4472 * IN: vp - file to push page to.
4473 * pp - page to push.
4474 * flags - additional flags.
4475 * cr - credentials of caller.
4477 * OUT: offp - start of range pushed.
4478 * lenp - len of range pushed.
4480 * RETURN: 0 on success, error code on failure.
4482 * NOTE: callers must have locked the page to be pushed. On
4483 * exit, the page (and all other pages in the kluster) must be
4488 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4489 size_t *lenp, int flags, cred_t *cr)
4491 znode_t *zp = VTOZ(vp);
4492 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4494 u_offset_t off, koff;
4501 * If our blocksize is bigger than the page size, try to kluster
4502 * multiple pages so that we write a full block (thus avoiding
4503 * a read-modify-write).
4505 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4506 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4507 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4508 ASSERT(koff <= zp->z_size);
4509 if (koff + klen > zp->z_size)
4510 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4511 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4513 ASSERT3U(btop(len), ==, btopr(len));
4516 * Can't push pages past end-of-file.
4518 if (off >= zp->z_size) {
4519 /* ignore all pages */
4522 } else if (off + len > zp->z_size) {
4523 int npages = btopr(zp->z_size - off);
4526 page_list_break(&pp, &trunc, npages);
4527 /* ignore pages past end of file */
4529 pvn_write_done(trunc, flags);
4530 len = zp->z_size - off;
4533 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4534 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4535 err = SET_ERROR(EDQUOT);
4538 tx = dmu_tx_create(zfsvfs->z_os);
4539 dmu_tx_hold_write(tx, zp->z_id, off, len);
4541 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4542 zfs_sa_upgrade_txholds(tx, zp);
4543 err = dmu_tx_assign(tx, TXG_WAIT);
4549 if (zp->z_blksz <= PAGESIZE) {
4550 caddr_t va = zfs_map_page(pp, S_READ);
4551 ASSERT3U(len, <=, PAGESIZE);
4552 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4553 zfs_unmap_page(pp, va);
4555 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4559 uint64_t mtime[2], ctime[2];
4560 sa_bulk_attr_t bulk[3];
4563 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4565 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4567 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4569 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4571 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4576 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4586 * Copy the portion of the file indicated from pages into the file.
4587 * The pages are stored in a page list attached to the files vnode.
4589 * IN: vp - vnode of file to push page data to.
4590 * off - position in file to put data.
4591 * len - amount of data to write.
4592 * flags - flags to control the operation.
4593 * cr - credentials of caller.
4594 * ct - caller context.
4596 * RETURN: 0 on success, error code on failure.
4599 * vp - ctime|mtime updated
4603 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4604 caller_context_t *ct)
4606 znode_t *zp = VTOZ(vp);
4607 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4619 * Align this request to the file block size in case we kluster.
4620 * XXX - this can result in pretty aggresive locking, which can
4621 * impact simultanious read/write access. One option might be
4622 * to break up long requests (len == 0) into block-by-block
4623 * operations to get narrower locking.
4625 blksz = zp->z_blksz;
4627 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4630 if (len > 0 && ISP2(blksz))
4631 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4637 * Search the entire vp list for pages >= io_off.
4639 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4640 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4643 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4645 if (off > zp->z_size) {
4646 /* past end of file */
4647 zfs_range_unlock(rl);
4652 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4654 for (off = io_off; io_off < off + len; io_off += io_len) {
4655 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4656 pp = page_lookup(vp, io_off,
4657 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4659 pp = page_lookup_nowait(vp, io_off,
4660 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4663 if (pp != NULL && pvn_getdirty(pp, flags)) {
4667 * Found a dirty page to push
4669 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4677 zfs_range_unlock(rl);
4678 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4679 zil_commit(zfsvfs->z_log, zp->z_id);
4687 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4689 znode_t *zp = VTOZ(vp);
4690 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4693 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4694 if (zp->z_sa_hdl == NULL) {
4696 * The fs has been unmounted, or we did a
4697 * suspend/resume and this file no longer exists.
4699 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4700 vrecycle(vp, curthread);
4704 mutex_enter(&zp->z_lock);
4705 if (zp->z_unlinked) {
4707 * Fast path to recycle a vnode of a removed file.
4709 mutex_exit(&zp->z_lock);
4710 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4711 vrecycle(vp, curthread);
4714 mutex_exit(&zp->z_lock);
4716 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4717 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4719 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4720 zfs_sa_upgrade_txholds(tx, zp);
4721 error = dmu_tx_assign(tx, TXG_WAIT);
4725 mutex_enter(&zp->z_lock);
4726 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4727 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4728 zp->z_atime_dirty = 0;
4729 mutex_exit(&zp->z_lock);
4733 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4738 * Bounds-check the seek operation.
4740 * IN: vp - vnode seeking within
4741 * ooff - old file offset
4742 * noffp - pointer to new file offset
4743 * ct - caller context
4745 * RETURN: 0 on success, EINVAL if new offset invalid.
4749 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4750 caller_context_t *ct)
4752 if (vp->v_type == VDIR)
4754 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4758 * Pre-filter the generic locking function to trap attempts to place
4759 * a mandatory lock on a memory mapped file.
4762 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4763 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4765 znode_t *zp = VTOZ(vp);
4766 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4772 * We are following the UFS semantics with respect to mapcnt
4773 * here: If we see that the file is mapped already, then we will
4774 * return an error, but we don't worry about races between this
4775 * function and zfs_map().
4777 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4779 return (SET_ERROR(EAGAIN));
4782 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4786 * If we can't find a page in the cache, we will create a new page
4787 * and fill it with file data. For efficiency, we may try to fill
4788 * multiple pages at once (klustering) to fill up the supplied page
4789 * list. Note that the pages to be filled are held with an exclusive
4790 * lock to prevent access by other threads while they are being filled.
4793 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4794 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4796 znode_t *zp = VTOZ(vp);
4797 page_t *pp, *cur_pp;
4798 objset_t *os = zp->z_zfsvfs->z_os;
4799 u_offset_t io_off, total;
4803 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4805 * We only have a single page, don't bother klustering
4809 pp = page_create_va(vp, io_off, io_len,
4810 PG_EXCL | PG_WAIT, seg, addr);
4813 * Try to find enough pages to fill the page list
4815 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4816 &io_len, off, plsz, 0);
4820 * The page already exists, nothing to do here.
4827 * Fill the pages in the kluster.
4830 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4833 ASSERT3U(io_off, ==, cur_pp->p_offset);
4834 va = zfs_map_page(cur_pp, S_WRITE);
4835 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4837 zfs_unmap_page(cur_pp, va);
4839 /* On error, toss the entire kluster */
4840 pvn_read_done(pp, B_ERROR);
4841 /* convert checksum errors into IO errors */
4843 err = SET_ERROR(EIO);
4846 cur_pp = cur_pp->p_next;
4850 * Fill in the page list array from the kluster starting
4851 * from the desired offset `off'.
4852 * NOTE: the page list will always be null terminated.
4854 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4855 ASSERT(pl == NULL || (*pl)->p_offset == off);
4861 * Return pointers to the pages for the file region [off, off + len]
4862 * in the pl array. If plsz is greater than len, this function may
4863 * also return page pointers from after the specified region
4864 * (i.e. the region [off, off + plsz]). These additional pages are
4865 * only returned if they are already in the cache, or were created as
4866 * part of a klustered read.
4868 * IN: vp - vnode of file to get data from.
4869 * off - position in file to get data from.
4870 * len - amount of data to retrieve.
4871 * plsz - length of provided page list.
4872 * seg - segment to obtain pages for.
4873 * addr - virtual address of fault.
4874 * rw - mode of created pages.
4875 * cr - credentials of caller.
4876 * ct - caller context.
4878 * OUT: protp - protection mode of created pages.
4879 * pl - list of pages created.
4881 * RETURN: 0 on success, error code on failure.
4884 * vp - atime updated
4888 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4889 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4890 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4892 znode_t *zp = VTOZ(vp);
4893 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4897 /* we do our own caching, faultahead is unnecessary */
4900 else if (len > plsz)
4903 len = P2ROUNDUP(len, PAGESIZE);
4904 ASSERT(plsz >= len);
4913 * Loop through the requested range [off, off + len) looking
4914 * for pages. If we don't find a page, we will need to create
4915 * a new page and fill it with data from the file.
4918 if (*pl = page_lookup(vp, off, SE_SHARED))
4920 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4923 ASSERT3U((*pl)->p_offset, ==, off);
4927 ASSERT3U(len, >=, PAGESIZE);
4930 ASSERT3U(plsz, >=, PAGESIZE);
4937 * Fill out the page array with any pages already in the cache.
4940 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4947 * Release any pages we have previously locked.
4952 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4962 * Request a memory map for a section of a file. This code interacts
4963 * with common code and the VM system as follows:
4965 * - common code calls mmap(), which ends up in smmap_common()
4966 * - this calls VOP_MAP(), which takes you into (say) zfs
4967 * - zfs_map() calls as_map(), passing segvn_create() as the callback
4968 * - segvn_create() creates the new segment and calls VOP_ADDMAP()
4969 * - zfs_addmap() updates z_mapcnt
4973 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4974 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4975 caller_context_t *ct)
4977 znode_t *zp = VTOZ(vp);
4978 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4979 segvn_crargs_t vn_a;
4985 if ((prot & PROT_WRITE) && (zp->z_pflags &
4986 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4988 return (SET_ERROR(EPERM));
4991 if ((prot & (PROT_READ | PROT_EXEC)) &&
4992 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4994 return (SET_ERROR(EACCES));
4997 if (vp->v_flag & VNOMAP) {
4999 return (SET_ERROR(ENOSYS));
5002 if (off < 0 || len > MAXOFFSET_T - off) {
5004 return (SET_ERROR(ENXIO));
5007 if (vp->v_type != VREG) {
5009 return (SET_ERROR(ENODEV));
5013 * If file is locked, disallow mapping.
5015 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
5017 return (SET_ERROR(EAGAIN));
5021 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
5029 vn_a.offset = (u_offset_t)off;
5030 vn_a.type = flags & MAP_TYPE;
5032 vn_a.maxprot = maxprot;
5035 vn_a.flags = flags & ~MAP_TYPE;
5037 vn_a.lgrp_mem_policy_flags = 0;
5039 error = as_map(as, *addrp, len, segvn_create, &vn_a);
5048 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5049 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
5050 caller_context_t *ct)
5052 uint64_t pages = btopr(len);
5054 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
5059 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
5060 * more accurate mtime for the associated file. Since we don't have a way of
5061 * detecting when the data was actually modified, we have to resort to
5062 * heuristics. If an explicit msync() is done, then we mark the mtime when the
5063 * last page is pushed. The problem occurs when the msync() call is omitted,
5064 * which by far the most common case:
5072 * putpage() via fsflush
5074 * If we wait until fsflush to come along, we can have a modification time that
5075 * is some arbitrary point in the future. In order to prevent this in the
5076 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
5081 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5082 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
5083 caller_context_t *ct)
5085 uint64_t pages = btopr(len);
5087 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
5088 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
5090 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
5091 vn_has_cached_data(vp))
5092 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
5098 * Free or allocate space in a file. Currently, this function only
5099 * supports the `F_FREESP' command. However, this command is somewhat
5100 * misnamed, as its functionality includes the ability to allocate as
5101 * well as free space.
5103 * IN: vp - vnode of file to free data in.
5104 * cmd - action to take (only F_FREESP supported).
5105 * bfp - section of file to free/alloc.
5106 * flag - current file open mode flags.
5107 * offset - current file offset.
5108 * cr - credentials of caller [UNUSED].
5109 * ct - caller context.
5111 * RETURN: 0 on success, error code on failure.
5114 * vp - ctime|mtime updated
5118 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
5119 offset_t offset, cred_t *cr, caller_context_t *ct)
5121 znode_t *zp = VTOZ(vp);
5122 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5129 if (cmd != F_FREESP) {
5131 return (SET_ERROR(EINVAL));
5134 if (error = convoff(vp, bfp, 0, offset)) {
5139 if (bfp->l_len < 0) {
5141 return (SET_ERROR(EINVAL));
5145 len = bfp->l_len; /* 0 means from off to end of file */
5147 error = zfs_freesp(zp, off, len, flag, TRUE);
5154 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
5155 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
5159 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
5161 znode_t *zp = VTOZ(vp);
5162 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5165 uint64_t object = zp->z_id;
5172 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
5173 &gen64, sizeof (uint64_t))) != 0) {
5178 gen = (uint32_t)gen64;
5180 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
5183 if (fidp->fid_len < size) {
5184 fidp->fid_len = size;
5186 return (SET_ERROR(ENOSPC));
5189 fidp->fid_len = size;
5192 zfid = (zfid_short_t *)fidp;
5194 zfid->zf_len = size;
5196 for (i = 0; i < sizeof (zfid->zf_object); i++)
5197 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
5199 /* Must have a non-zero generation number to distinguish from .zfs */
5202 for (i = 0; i < sizeof (zfid->zf_gen); i++)
5203 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
5205 if (size == LONG_FID_LEN) {
5206 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
5209 zlfid = (zfid_long_t *)fidp;
5211 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
5212 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
5214 /* XXX - this should be the generation number for the objset */
5215 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
5216 zlfid->zf_setgen[i] = 0;
5224 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5225 caller_context_t *ct)
5237 case _PC_FILESIZEBITS:
5241 case _PC_XATTR_EXISTS:
5243 zfsvfs = zp->z_zfsvfs;
5247 error = zfs_dirent_lock(&dl, zp, "", &xzp,
5248 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
5250 zfs_dirent_unlock(dl);
5251 if (!zfs_dirempty(xzp))
5254 } else if (error == ENOENT) {
5256 * If there aren't extended attributes, it's the
5257 * same as having zero of them.
5264 case _PC_SATTR_ENABLED:
5265 case _PC_SATTR_EXISTS:
5266 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5267 (vp->v_type == VREG || vp->v_type == VDIR);
5270 case _PC_ACCESS_FILTERING:
5271 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
5275 case _PC_ACL_ENABLED:
5276 *valp = _ACL_ACE_ENABLED;
5279 case _PC_MIN_HOLE_SIZE:
5280 *valp = (int)SPA_MINBLOCKSIZE;
5283 case _PC_TIMESTAMP_RESOLUTION:
5284 /* nanosecond timestamp resolution */
5288 case _PC_ACL_EXTENDED:
5296 case _PC_ACL_PATH_MAX:
5297 *valp = ACL_MAX_ENTRIES;
5301 return (EOPNOTSUPP);
5307 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5308 caller_context_t *ct)
5310 znode_t *zp = VTOZ(vp);
5311 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5313 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5317 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5325 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5326 caller_context_t *ct)
5328 znode_t *zp = VTOZ(vp);
5329 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5331 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5332 zilog_t *zilog = zfsvfs->z_log;
5337 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5339 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5340 zil_commit(zilog, 0);
5348 * The smallest read we may consider to loan out an arcbuf.
5349 * This must be a power of 2.
5351 int zcr_blksz_min = (1 << 10); /* 1K */
5353 * If set to less than the file block size, allow loaning out of an
5354 * arcbuf for a partial block read. This must be a power of 2.
5356 int zcr_blksz_max = (1 << 17); /* 128K */
5360 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5361 caller_context_t *ct)
5363 znode_t *zp = VTOZ(vp);
5364 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5365 int max_blksz = zfsvfs->z_max_blksz;
5366 uio_t *uio = &xuio->xu_uio;
5367 ssize_t size = uio->uio_resid;
5368 offset_t offset = uio->uio_loffset;
5373 int preamble, postamble;
5375 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5376 return (SET_ERROR(EINVAL));
5383 * Loan out an arc_buf for write if write size is bigger than
5384 * max_blksz, and the file's block size is also max_blksz.
5387 if (size < blksz || zp->z_blksz != blksz) {
5389 return (SET_ERROR(EINVAL));
5392 * Caller requests buffers for write before knowing where the
5393 * write offset might be (e.g. NFS TCP write).
5398 preamble = P2PHASE(offset, blksz);
5400 preamble = blksz - preamble;
5405 postamble = P2PHASE(size, blksz);
5408 fullblk = size / blksz;
5409 (void) dmu_xuio_init(xuio,
5410 (preamble != 0) + fullblk + (postamble != 0));
5411 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5412 int, postamble, int,
5413 (preamble != 0) + fullblk + (postamble != 0));
5416 * Have to fix iov base/len for partial buffers. They
5417 * currently represent full arc_buf's.
5420 /* data begins in the middle of the arc_buf */
5421 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5424 (void) dmu_xuio_add(xuio, abuf,
5425 blksz - preamble, preamble);
5428 for (i = 0; i < fullblk; i++) {
5429 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5432 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5436 /* data ends in the middle of the arc_buf */
5437 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5440 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5445 * Loan out an arc_buf for read if the read size is larger than
5446 * the current file block size. Block alignment is not
5447 * considered. Partial arc_buf will be loaned out for read.
5449 blksz = zp->z_blksz;
5450 if (blksz < zcr_blksz_min)
5451 blksz = zcr_blksz_min;
5452 if (blksz > zcr_blksz_max)
5453 blksz = zcr_blksz_max;
5454 /* avoid potential complexity of dealing with it */
5455 if (blksz > max_blksz) {
5457 return (SET_ERROR(EINVAL));
5460 maxsize = zp->z_size - uio->uio_loffset;
5464 if (size < blksz || vn_has_cached_data(vp)) {
5466 return (SET_ERROR(EINVAL));
5471 return (SET_ERROR(EINVAL));
5474 uio->uio_extflg = UIO_XUIO;
5475 XUIO_XUZC_RW(xuio) = ioflag;
5482 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5486 int ioflag = XUIO_XUZC_RW(xuio);
5488 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5490 i = dmu_xuio_cnt(xuio);
5492 abuf = dmu_xuio_arcbuf(xuio, i);
5494 * if abuf == NULL, it must be a write buffer
5495 * that has been returned in zfs_write().
5498 dmu_return_arcbuf(abuf);
5499 ASSERT(abuf || ioflag == UIO_WRITE);
5502 dmu_xuio_fini(xuio);
5507 * Predeclare these here so that the compiler assumes that
5508 * this is an "old style" function declaration that does
5509 * not include arguments => we won't get type mismatch errors
5510 * in the initializations that follow.
5512 static int zfs_inval();
5513 static int zfs_isdir();
5518 return (SET_ERROR(EINVAL));
5524 return (SET_ERROR(EISDIR));
5527 * Directory vnode operations template
5529 vnodeops_t *zfs_dvnodeops;
5530 const fs_operation_def_t zfs_dvnodeops_template[] = {
5531 VOPNAME_OPEN, { .vop_open = zfs_open },
5532 VOPNAME_CLOSE, { .vop_close = zfs_close },
5533 VOPNAME_READ, { .error = zfs_isdir },
5534 VOPNAME_WRITE, { .error = zfs_isdir },
5535 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5536 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5537 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5538 VOPNAME_ACCESS, { .vop_access = zfs_access },
5539 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5540 VOPNAME_CREATE, { .vop_create = zfs_create },
5541 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5542 VOPNAME_LINK, { .vop_link = zfs_link },
5543 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5544 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5545 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5546 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5547 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5548 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5549 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5550 VOPNAME_FID, { .vop_fid = zfs_fid },
5551 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5552 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5553 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5554 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5555 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5560 * Regular file vnode operations template
5562 vnodeops_t *zfs_fvnodeops;
5563 const fs_operation_def_t zfs_fvnodeops_template[] = {
5564 VOPNAME_OPEN, { .vop_open = zfs_open },
5565 VOPNAME_CLOSE, { .vop_close = zfs_close },
5566 VOPNAME_READ, { .vop_read = zfs_read },
5567 VOPNAME_WRITE, { .vop_write = zfs_write },
5568 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5569 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5570 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5571 VOPNAME_ACCESS, { .vop_access = zfs_access },
5572 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5573 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5574 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5575 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5576 VOPNAME_FID, { .vop_fid = zfs_fid },
5577 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5578 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5579 VOPNAME_SPACE, { .vop_space = zfs_space },
5580 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5581 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5582 VOPNAME_MAP, { .vop_map = zfs_map },
5583 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5584 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5585 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5586 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5587 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5588 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5589 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5590 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5595 * Symbolic link vnode operations template
5597 vnodeops_t *zfs_symvnodeops;
5598 const fs_operation_def_t zfs_symvnodeops_template[] = {
5599 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5600 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5601 VOPNAME_ACCESS, { .vop_access = zfs_access },
5602 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5603 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5604 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5605 VOPNAME_FID, { .vop_fid = zfs_fid },
5606 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5607 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5612 * special share hidden files vnode operations template
5614 vnodeops_t *zfs_sharevnodeops;
5615 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5616 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5617 VOPNAME_ACCESS, { .vop_access = zfs_access },
5618 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5619 VOPNAME_FID, { .vop_fid = zfs_fid },
5620 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5621 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5622 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5623 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5628 * Extended attribute directory vnode operations template
5630 * This template is identical to the directory vnodes
5631 * operation template except for restricted operations:
5635 * Note that there are other restrictions embedded in:
5636 * zfs_create() - restrict type to VREG
5637 * zfs_link() - no links into/out of attribute space
5638 * zfs_rename() - no moves into/out of attribute space
5640 vnodeops_t *zfs_xdvnodeops;
5641 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5642 VOPNAME_OPEN, { .vop_open = zfs_open },
5643 VOPNAME_CLOSE, { .vop_close = zfs_close },
5644 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5645 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5646 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5647 VOPNAME_ACCESS, { .vop_access = zfs_access },
5648 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5649 VOPNAME_CREATE, { .vop_create = zfs_create },
5650 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5651 VOPNAME_LINK, { .vop_link = zfs_link },
5652 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5653 VOPNAME_MKDIR, { .error = zfs_inval },
5654 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5655 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5656 VOPNAME_SYMLINK, { .error = zfs_inval },
5657 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5658 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5659 VOPNAME_FID, { .vop_fid = zfs_fid },
5660 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5661 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5662 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5663 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5664 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5669 * Error vnode operations template
5671 vnodeops_t *zfs_evnodeops;
5672 const fs_operation_def_t zfs_evnodeops_template[] = {
5673 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5674 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5680 ioflags(int ioflags)
5684 if (ioflags & IO_APPEND)
5686 if (ioflags & IO_NDELAY)
5688 if (ioflags & IO_SYNC)
5689 flags |= (FSYNC | FDSYNC | FRSYNC);
5695 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int reqpage)
5697 znode_t *zp = VTOZ(vp);
5698 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5699 objset_t *os = zp->z_zfsvfs->z_os;
5700 vm_page_t mfirst, mlast, mreq;
5704 off_t startoff, endoff;
5706 vm_pindex_t reqstart, reqend;
5707 int pcount, lsize, reqsize, size;
5712 pcount = OFF_TO_IDX(round_page(count));
5714 object = mreq->object;
5717 KASSERT(vp->v_object == object, ("mismatching object"));
5719 if (pcount > 1 && zp->z_blksz > PAGESIZE) {
5720 startoff = rounddown(IDX_TO_OFF(mreq->pindex), zp->z_blksz);
5721 reqstart = OFF_TO_IDX(round_page(startoff));
5722 if (reqstart < m[0]->pindex)
5725 reqstart = reqstart - m[0]->pindex;
5726 endoff = roundup(IDX_TO_OFF(mreq->pindex) + PAGE_SIZE,
5728 reqend = OFF_TO_IDX(trunc_page(endoff)) - 1;
5729 if (reqend > m[pcount - 1]->pindex)
5730 reqend = m[pcount - 1]->pindex;
5731 reqsize = reqend - m[reqstart]->pindex + 1;
5732 KASSERT(reqstart <= reqpage && reqpage < reqstart + reqsize,
5733 ("reqpage beyond [reqstart, reqstart + reqsize[ bounds"));
5738 mfirst = m[reqstart];
5739 mlast = m[reqstart + reqsize - 1];
5741 VM_OBJECT_LOCK(object);
5743 for (i = 0; i < reqstart; i++) {
5746 vm_page_unlock(m[i]);
5748 for (i = reqstart + reqsize; i < pcount; i++) {
5751 vm_page_unlock(m[i]);
5754 if (mreq->valid && reqsize == 1) {
5755 if (mreq->valid != VM_PAGE_BITS_ALL)
5756 vm_page_zero_invalid(mreq, TRUE);
5757 VM_OBJECT_UNLOCK(object);
5759 return (VM_PAGER_OK);
5762 PCPU_INC(cnt.v_vnodein);
5763 PCPU_ADD(cnt.v_vnodepgsin, reqsize);
5765 if (IDX_TO_OFF(mreq->pindex) >= object->un_pager.vnp.vnp_size) {
5766 for (i = reqstart; i < reqstart + reqsize; i++) {
5770 vm_page_unlock(m[i]);
5773 VM_OBJECT_UNLOCK(object);
5775 return (VM_PAGER_BAD);
5779 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size)
5780 lsize = object->un_pager.vnp.vnp_size - IDX_TO_OFF(mlast->pindex);
5782 VM_OBJECT_UNLOCK(object);
5784 for (i = reqstart; i < reqstart + reqsize; i++) {
5786 if (i == (reqstart + reqsize - 1))
5788 va = zfs_map_page(m[i], &sf);
5789 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex),
5790 size, va, DMU_READ_PREFETCH);
5791 if (size != PAGE_SIZE)
5792 bzero(va + size, PAGE_SIZE - size);
5798 VM_OBJECT_LOCK(object);
5800 for (i = reqstart; i < reqstart + reqsize; i++) {
5802 m[i]->valid = VM_PAGE_BITS_ALL;
5803 KASSERT(m[i]->dirty == 0, ("zfs_getpages: page %p is dirty", m[i]));
5805 vm_page_readahead_finish(m[i]);
5808 VM_OBJECT_UNLOCK(object);
5810 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
5812 return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
5816 zfs_freebsd_getpages(ap)
5817 struct vop_getpages_args /* {
5822 vm_ooffset_t a_offset;
5826 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_reqpage));
5830 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
5833 znode_t *zp = VTOZ(vp);
5834 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5843 vm_ooffset_t lo_off;
5854 object = vp->v_object;
5858 KASSERT(ma[0]->object == object, ("mismatching object"));
5859 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
5861 for (i = 0; i < pcount; i++)
5862 rtvals[i] = VM_PAGER_ERROR;
5864 off = IDX_TO_OFF(ma[0]->pindex);
5865 blksz = zp->z_blksz;
5866 lo_off = rounddown(off, blksz);
5867 lo_len = roundup(len + (off - lo_off), blksz);
5868 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER);
5870 VM_OBJECT_LOCK(object);
5871 if (len + off > object->un_pager.vnp.vnp_size) {
5872 if (object->un_pager.vnp.vnp_size > off) {
5875 len = object->un_pager.vnp.vnp_size - off;
5877 if ((pgoff = (int)len & PAGE_MASK) != 0) {
5879 * If the object is locked and the following
5880 * conditions hold, then the page's dirty
5881 * field cannot be concurrently changed by a
5885 KASSERT(m->busy > 0,
5886 ("zfs_putpages: page %p is not busy", m));
5887 KASSERT(!pmap_page_is_write_mapped(m),
5888 ("zfs_putpages: page %p is not read-only", m));
5889 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
5896 if (ncount < pcount) {
5897 for (i = ncount; i < pcount; i++) {
5898 rtvals[i] = VM_PAGER_BAD;
5902 VM_OBJECT_UNLOCK(object);
5907 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
5908 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
5913 tx = dmu_tx_create(zfsvfs->z_os);
5914 dmu_tx_hold_write(tx, zp->z_id, off, len);
5916 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
5917 zfs_sa_upgrade_txholds(tx, zp);
5918 err = dmu_tx_assign(tx, TXG_NOWAIT);
5920 if (err == ERESTART) {
5929 if (zp->z_blksz < PAGE_SIZE) {
5931 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
5932 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
5933 va = zfs_map_page(ma[i], &sf);
5934 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
5938 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
5942 uint64_t mtime[2], ctime[2];
5943 sa_bulk_attr_t bulk[3];
5946 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
5948 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
5950 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
5952 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
5954 (void)sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
5955 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
5957 VM_OBJECT_LOCK(object);
5958 for (i = 0; i < ncount; i++) {
5959 rtvals[i] = VM_PAGER_OK;
5960 vm_page_undirty(ma[i]);
5962 VM_OBJECT_UNLOCK(object);
5963 PCPU_INC(cnt.v_vnodeout);
5964 PCPU_ADD(cnt.v_vnodepgsout, ncount);
5969 zfs_range_unlock(rl);
5970 if ((flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0 ||
5971 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5972 zil_commit(zfsvfs->z_log, zp->z_id);
5978 zfs_freebsd_putpages(ap)
5979 struct vop_putpages_args /* {
5985 vm_ooffset_t a_offset;
5989 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
5994 zfs_freebsd_bmap(ap)
5995 struct vop_bmap_args /* {
5998 struct bufobj **a_bop;
6005 if (ap->a_bop != NULL)
6006 *ap->a_bop = &ap->a_vp->v_bufobj;
6007 if (ap->a_bnp != NULL)
6008 *ap->a_bnp = ap->a_bn;
6009 if (ap->a_runp != NULL)
6011 if (ap->a_runb != NULL)
6018 zfs_freebsd_open(ap)
6019 struct vop_open_args /* {
6022 struct ucred *a_cred;
6023 struct thread *a_td;
6026 vnode_t *vp = ap->a_vp;
6027 znode_t *zp = VTOZ(vp);
6030 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
6032 vnode_create_vobject(vp, zp->z_size, ap->a_td);
6037 zfs_freebsd_close(ap)
6038 struct vop_close_args /* {
6041 struct ucred *a_cred;
6042 struct thread *a_td;
6046 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
6050 zfs_freebsd_ioctl(ap)
6051 struct vop_ioctl_args /* {
6061 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
6062 ap->a_fflag, ap->a_cred, NULL, NULL));
6066 zfs_freebsd_read(ap)
6067 struct vop_read_args /* {
6071 struct ucred *a_cred;
6075 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6080 zfs_freebsd_write(ap)
6081 struct vop_write_args /* {
6085 struct ucred *a_cred;
6089 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6094 zfs_freebsd_access(ap)
6095 struct vop_access_args /* {
6097 accmode_t a_accmode;
6098 struct ucred *a_cred;
6099 struct thread *a_td;
6102 vnode_t *vp = ap->a_vp;
6103 znode_t *zp = VTOZ(vp);
6108 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
6110 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
6112 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
6115 * VADMIN has to be handled by vaccess().
6118 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
6120 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
6121 zp->z_gid, accmode, ap->a_cred, NULL);
6126 * For VEXEC, ensure that at least one execute bit is set for
6129 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
6130 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
6138 zfs_freebsd_lookup(ap)
6139 struct vop_lookup_args /* {
6140 struct vnode *a_dvp;
6141 struct vnode **a_vpp;
6142 struct componentname *a_cnp;
6145 struct componentname *cnp = ap->a_cnp;
6146 char nm[NAME_MAX + 1];
6148 ASSERT(cnp->cn_namelen < sizeof(nm));
6149 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
6151 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
6152 cnp->cn_cred, cnp->cn_thread, 0));
6156 zfs_freebsd_create(ap)
6157 struct vop_create_args /* {
6158 struct vnode *a_dvp;
6159 struct vnode **a_vpp;
6160 struct componentname *a_cnp;
6161 struct vattr *a_vap;
6164 struct componentname *cnp = ap->a_cnp;
6165 vattr_t *vap = ap->a_vap;
6168 ASSERT(cnp->cn_flags & SAVENAME);
6170 vattr_init_mask(vap);
6171 mode = vap->va_mode & ALLPERMS;
6173 return (zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
6174 ap->a_vpp, cnp->cn_cred, cnp->cn_thread));
6178 zfs_freebsd_remove(ap)
6179 struct vop_remove_args /* {
6180 struct vnode *a_dvp;
6182 struct componentname *a_cnp;
6186 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6188 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr,
6189 ap->a_cnp->cn_cred, NULL, 0));
6193 zfs_freebsd_mkdir(ap)
6194 struct vop_mkdir_args /* {
6195 struct vnode *a_dvp;
6196 struct vnode **a_vpp;
6197 struct componentname *a_cnp;
6198 struct vattr *a_vap;
6201 vattr_t *vap = ap->a_vap;
6203 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6205 vattr_init_mask(vap);
6207 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
6208 ap->a_cnp->cn_cred, NULL, 0, NULL));
6212 zfs_freebsd_rmdir(ap)
6213 struct vop_rmdir_args /* {
6214 struct vnode *a_dvp;
6216 struct componentname *a_cnp;
6219 struct componentname *cnp = ap->a_cnp;
6221 ASSERT(cnp->cn_flags & SAVENAME);
6223 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0));
6227 zfs_freebsd_readdir(ap)
6228 struct vop_readdir_args /* {
6231 struct ucred *a_cred;
6238 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
6239 ap->a_ncookies, ap->a_cookies));
6243 zfs_freebsd_fsync(ap)
6244 struct vop_fsync_args /* {
6247 struct thread *a_td;
6252 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
6256 zfs_freebsd_getattr(ap)
6257 struct vop_getattr_args /* {
6259 struct vattr *a_vap;
6260 struct ucred *a_cred;
6263 vattr_t *vap = ap->a_vap;
6269 xvap.xva_vattr = *vap;
6270 xvap.xva_vattr.va_mask |= AT_XVATTR;
6272 /* Convert chflags into ZFS-type flags. */
6273 /* XXX: what about SF_SETTABLE?. */
6274 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
6275 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
6276 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
6277 XVA_SET_REQ(&xvap, XAT_NODUMP);
6278 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
6282 /* Convert ZFS xattr into chflags. */
6283 #define FLAG_CHECK(fflag, xflag, xfield) do { \
6284 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
6285 fflags |= (fflag); \
6287 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
6288 xvap.xva_xoptattrs.xoa_immutable);
6289 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
6290 xvap.xva_xoptattrs.xoa_appendonly);
6291 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
6292 xvap.xva_xoptattrs.xoa_nounlink);
6293 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
6294 xvap.xva_xoptattrs.xoa_nodump);
6296 *vap = xvap.xva_vattr;
6297 vap->va_flags = fflags;
6302 zfs_freebsd_setattr(ap)
6303 struct vop_setattr_args /* {
6305 struct vattr *a_vap;
6306 struct ucred *a_cred;
6309 vnode_t *vp = ap->a_vp;
6310 vattr_t *vap = ap->a_vap;
6311 cred_t *cred = ap->a_cred;
6316 vattr_init_mask(vap);
6317 vap->va_mask &= ~AT_NOSET;
6320 xvap.xva_vattr = *vap;
6322 zflags = VTOZ(vp)->z_pflags;
6324 if (vap->va_flags != VNOVAL) {
6325 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
6328 if (zfsvfs->z_use_fuids == B_FALSE)
6329 return (EOPNOTSUPP);
6331 fflags = vap->va_flags;
6332 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_NODUMP)) != 0)
6333 return (EOPNOTSUPP);
6335 * Unprivileged processes are not permitted to unset system
6336 * flags, or modify flags if any system flags are set.
6337 * Privileged non-jail processes may not modify system flags
6338 * if securelevel > 0 and any existing system flags are set.
6339 * Privileged jail processes behave like privileged non-jail
6340 * processes if the security.jail.chflags_allowed sysctl is
6341 * is non-zero; otherwise, they behave like unprivileged
6344 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
6345 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
6347 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6348 error = securelevel_gt(cred, 0);
6354 * Callers may only modify the file flags on objects they
6355 * have VADMIN rights for.
6357 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
6360 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6364 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
6369 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
6370 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
6371 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
6372 XVA_SET_REQ(&xvap, (xflag)); \
6373 (xfield) = ((fflags & (fflag)) != 0); \
6376 /* Convert chflags into ZFS-type flags. */
6377 /* XXX: what about SF_SETTABLE?. */
6378 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
6379 xvap.xva_xoptattrs.xoa_immutable);
6380 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
6381 xvap.xva_xoptattrs.xoa_appendonly);
6382 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
6383 xvap.xva_xoptattrs.xoa_nounlink);
6384 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
6385 xvap.xva_xoptattrs.xoa_nodump);
6388 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
6392 zfs_freebsd_rename(ap)
6393 struct vop_rename_args /* {
6394 struct vnode *a_fdvp;
6395 struct vnode *a_fvp;
6396 struct componentname *a_fcnp;
6397 struct vnode *a_tdvp;
6398 struct vnode *a_tvp;
6399 struct componentname *a_tcnp;
6402 vnode_t *fdvp = ap->a_fdvp;
6403 vnode_t *fvp = ap->a_fvp;
6404 vnode_t *tdvp = ap->a_tdvp;
6405 vnode_t *tvp = ap->a_tvp;
6408 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
6409 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
6411 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp,
6412 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0);
6427 zfs_freebsd_symlink(ap)
6428 struct vop_symlink_args /* {
6429 struct vnode *a_dvp;
6430 struct vnode **a_vpp;
6431 struct componentname *a_cnp;
6432 struct vattr *a_vap;
6436 struct componentname *cnp = ap->a_cnp;
6437 vattr_t *vap = ap->a_vap;
6439 ASSERT(cnp->cn_flags & SAVENAME);
6441 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
6442 vattr_init_mask(vap);
6444 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
6445 ap->a_target, cnp->cn_cred, cnp->cn_thread));
6449 zfs_freebsd_readlink(ap)
6450 struct vop_readlink_args /* {
6453 struct ucred *a_cred;
6457 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
6461 zfs_freebsd_link(ap)
6462 struct vop_link_args /* {
6463 struct vnode *a_tdvp;
6465 struct componentname *a_cnp;
6468 struct componentname *cnp = ap->a_cnp;
6470 ASSERT(cnp->cn_flags & SAVENAME);
6472 return (zfs_link(ap->a_tdvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
6476 zfs_freebsd_inactive(ap)
6477 struct vop_inactive_args /* {
6479 struct thread *a_td;
6482 vnode_t *vp = ap->a_vp;
6484 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
6489 zfs_freebsd_reclaim(ap)
6490 struct vop_reclaim_args /* {
6492 struct thread *a_td;
6495 vnode_t *vp = ap->a_vp;
6496 znode_t *zp = VTOZ(vp);
6497 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6501 /* Destroy the vm object and flush associated pages. */
6502 vnode_destroy_vobject(vp);
6505 * z_teardown_inactive_lock protects from a race with
6506 * zfs_znode_dmu_fini in zfsvfs_teardown during
6509 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
6510 if (zp->z_sa_hdl == NULL)
6514 rw_exit(&zfsvfs->z_teardown_inactive_lock);
6522 struct vop_fid_args /* {
6528 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
6532 zfs_freebsd_pathconf(ap)
6533 struct vop_pathconf_args /* {
6536 register_t *a_retval;
6542 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
6544 *ap->a_retval = val;
6545 else if (error == EOPNOTSUPP)
6546 error = vop_stdpathconf(ap);
6551 zfs_freebsd_fifo_pathconf(ap)
6552 struct vop_pathconf_args /* {
6555 register_t *a_retval;
6559 switch (ap->a_name) {
6560 case _PC_ACL_EXTENDED:
6562 case _PC_ACL_PATH_MAX:
6563 case _PC_MAC_PRESENT:
6564 return (zfs_freebsd_pathconf(ap));
6566 return (fifo_specops.vop_pathconf(ap));
6571 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
6572 * extended attribute name:
6575 * system freebsd:system:
6576 * user (none, can be used to access ZFS fsattr(5) attributes
6577 * created on Solaris)
6580 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
6583 const char *namespace, *prefix, *suffix;
6585 /* We don't allow '/' character in attribute name. */
6586 if (strchr(name, '/') != NULL)
6588 /* We don't allow attribute names that start with "freebsd:" string. */
6589 if (strncmp(name, "freebsd:", 8) == 0)
6592 bzero(attrname, size);
6594 switch (attrnamespace) {
6595 case EXTATTR_NAMESPACE_USER:
6597 prefix = "freebsd:";
6598 namespace = EXTATTR_NAMESPACE_USER_STRING;
6602 * This is the default namespace by which we can access all
6603 * attributes created on Solaris.
6605 prefix = namespace = suffix = "";
6608 case EXTATTR_NAMESPACE_SYSTEM:
6609 prefix = "freebsd:";
6610 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
6613 case EXTATTR_NAMESPACE_EMPTY:
6617 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
6619 return (ENAMETOOLONG);
6625 * Vnode operating to retrieve a named extended attribute.
6628 zfs_getextattr(struct vop_getextattr_args *ap)
6631 IN struct vnode *a_vp;
6632 IN int a_attrnamespace;
6633 IN const char *a_name;
6634 INOUT struct uio *a_uio;
6636 IN struct ucred *a_cred;
6637 IN struct thread *a_td;
6641 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6642 struct thread *td = ap->a_td;
6643 struct nameidata nd;
6646 vnode_t *xvp = NULL, *vp;
6649 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6650 ap->a_cred, ap->a_td, VREAD);
6654 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6661 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6669 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
6671 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
6673 NDFREE(&nd, NDF_ONLY_PNBUF);
6676 if (error == ENOENT)
6681 if (ap->a_size != NULL) {
6682 error = VOP_GETATTR(vp, &va, ap->a_cred);
6684 *ap->a_size = (size_t)va.va_size;
6685 } else if (ap->a_uio != NULL)
6686 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6689 vn_close(vp, flags, ap->a_cred, td);
6696 * Vnode operation to remove a named attribute.
6699 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
6702 IN struct vnode *a_vp;
6703 IN int a_attrnamespace;
6704 IN const char *a_name;
6705 IN struct ucred *a_cred;
6706 IN struct thread *a_td;
6710 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6711 struct thread *td = ap->a_td;
6712 struct nameidata nd;
6715 vnode_t *xvp = NULL, *vp;
6718 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6719 ap->a_cred, ap->a_td, VWRITE);
6723 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6730 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6737 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF | MPSAFE,
6738 UIO_SYSSPACE, attrname, xvp, td);
6743 NDFREE(&nd, NDF_ONLY_PNBUF);
6744 if (error == ENOENT)
6749 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
6750 NDFREE(&nd, NDF_ONLY_PNBUF);
6753 if (vp == nd.ni_dvp)
6763 * Vnode operation to set a named attribute.
6766 zfs_setextattr(struct vop_setextattr_args *ap)
6769 IN struct vnode *a_vp;
6770 IN int a_attrnamespace;
6771 IN const char *a_name;
6772 INOUT struct uio *a_uio;
6773 IN struct ucred *a_cred;
6774 IN struct thread *a_td;
6778 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6779 struct thread *td = ap->a_td;
6780 struct nameidata nd;
6783 vnode_t *xvp = NULL, *vp;
6786 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6787 ap->a_cred, ap->a_td, VWRITE);
6791 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6798 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6799 LOOKUP_XATTR | CREATE_XATTR_DIR);
6805 flags = FFLAGS(O_WRONLY | O_CREAT);
6806 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
6808 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
6810 NDFREE(&nd, NDF_ONLY_PNBUF);
6818 error = VOP_SETATTR(vp, &va, ap->a_cred);
6820 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6823 vn_close(vp, flags, ap->a_cred, td);
6830 * Vnode operation to retrieve extended attributes on a vnode.
6833 zfs_listextattr(struct vop_listextattr_args *ap)
6836 IN struct vnode *a_vp;
6837 IN int a_attrnamespace;
6838 INOUT struct uio *a_uio;
6840 IN struct ucred *a_cred;
6841 IN struct thread *a_td;
6845 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6846 struct thread *td = ap->a_td;
6847 struct nameidata nd;
6848 char attrprefix[16];
6849 u_char dirbuf[sizeof(struct dirent)];
6852 struct uio auio, *uio = ap->a_uio;
6853 size_t *sizep = ap->a_size;
6855 vnode_t *xvp = NULL, *vp;
6856 int done, error, eof, pos;
6858 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6859 ap->a_cred, ap->a_td, VREAD);
6863 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
6864 sizeof(attrprefix));
6867 plen = strlen(attrprefix);
6874 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6879 * ENOATTR means that the EA directory does not yet exist,
6880 * i.e. there are no extended attributes there.
6882 if (error == ENOATTR)
6887 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE,
6888 UIO_SYSSPACE, ".", xvp, td);
6891 NDFREE(&nd, NDF_ONLY_PNBUF);
6897 auio.uio_iov = &aiov;
6898 auio.uio_iovcnt = 1;
6899 auio.uio_segflg = UIO_SYSSPACE;
6901 auio.uio_rw = UIO_READ;
6902 auio.uio_offset = 0;
6907 aiov.iov_base = (void *)dirbuf;
6908 aiov.iov_len = sizeof(dirbuf);
6909 auio.uio_resid = sizeof(dirbuf);
6910 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
6911 done = sizeof(dirbuf) - auio.uio_resid;
6914 for (pos = 0; pos < done;) {
6915 dp = (struct dirent *)(dirbuf + pos);
6916 pos += dp->d_reclen;
6918 * XXX: Temporarily we also accept DT_UNKNOWN, as this
6919 * is what we get when attribute was created on Solaris.
6921 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
6923 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
6925 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
6927 nlen = dp->d_namlen - plen;
6930 else if (uio != NULL) {
6932 * Format of extattr name entry is one byte for
6933 * length and the rest for name.
6935 error = uiomove(&nlen, 1, uio->uio_rw, uio);
6937 error = uiomove(dp->d_name + plen, nlen,
6944 } while (!eof && error == 0);
6953 zfs_freebsd_getacl(ap)
6954 struct vop_getacl_args /* {
6963 vsecattr_t vsecattr;
6965 if (ap->a_type != ACL_TYPE_NFS4)
6968 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
6969 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
6972 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
6973 if (vsecattr.vsa_aclentp != NULL)
6974 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
6980 zfs_freebsd_setacl(ap)
6981 struct vop_setacl_args /* {
6990 vsecattr_t vsecattr;
6991 int aclbsize; /* size of acl list in bytes */
6994 if (ap->a_type != ACL_TYPE_NFS4)
6997 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
7001 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
7002 * splitting every entry into two and appending "canonical six"
7003 * entries at the end. Don't allow for setting an ACL that would
7004 * cause chmod(2) to run out of ACL entries.
7006 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
7009 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
7013 vsecattr.vsa_mask = VSA_ACE;
7014 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
7015 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
7016 aaclp = vsecattr.vsa_aclentp;
7017 vsecattr.vsa_aclentsz = aclbsize;
7019 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
7020 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
7021 kmem_free(aaclp, aclbsize);
7027 zfs_freebsd_aclcheck(ap)
7028 struct vop_aclcheck_args /* {
7037 return (EOPNOTSUPP);
7041 zfs_vptocnp(struct vop_vptocnp_args *ap)
7043 vnode_t *covered_vp;
7044 vnode_t *vp = ap->a_vp;;
7045 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
7046 znode_t *zp = VTOZ(vp);
7055 * If we are a snapshot mounted under .zfs, run the operation
7056 * on the covered vnode.
7058 if ((error = sa_lookup(zp->z_sa_hdl,
7059 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0) {
7064 if (zp->z_id != parent || zfsvfs->z_parent == zfsvfs) {
7066 return (vop_stdvptocnp(ap));
7070 covered_vp = vp->v_mount->mnt_vnodecovered;
7072 ltype = VOP_ISLOCKED(vp);
7074 error = vget(covered_vp, LK_EXCLUSIVE, curthread);
7077 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
7078 ap->a_buf, ap->a_buflen);
7081 vn_lock(vp, ltype | LK_RETRY);
7082 if ((vp->v_iflag & VI_DOOMED) != 0)
7083 error = SET_ERROR(ENOENT);
7087 struct vop_vector zfs_vnodeops;
7088 struct vop_vector zfs_fifoops;
7089 struct vop_vector zfs_shareops;
7091 struct vop_vector zfs_vnodeops = {
7092 .vop_default = &default_vnodeops,
7093 .vop_inactive = zfs_freebsd_inactive,
7094 .vop_reclaim = zfs_freebsd_reclaim,
7095 .vop_access = zfs_freebsd_access,
7096 #ifdef FREEBSD_NAMECACHE
7097 .vop_lookup = vfs_cache_lookup,
7098 .vop_cachedlookup = zfs_freebsd_lookup,
7100 .vop_lookup = zfs_freebsd_lookup,
7102 .vop_getattr = zfs_freebsd_getattr,
7103 .vop_setattr = zfs_freebsd_setattr,
7104 .vop_create = zfs_freebsd_create,
7105 .vop_mknod = zfs_freebsd_create,
7106 .vop_mkdir = zfs_freebsd_mkdir,
7107 .vop_readdir = zfs_freebsd_readdir,
7108 .vop_fsync = zfs_freebsd_fsync,
7109 .vop_open = zfs_freebsd_open,
7110 .vop_close = zfs_freebsd_close,
7111 .vop_rmdir = zfs_freebsd_rmdir,
7112 .vop_ioctl = zfs_freebsd_ioctl,
7113 .vop_link = zfs_freebsd_link,
7114 .vop_symlink = zfs_freebsd_symlink,
7115 .vop_readlink = zfs_freebsd_readlink,
7116 .vop_read = zfs_freebsd_read,
7117 .vop_write = zfs_freebsd_write,
7118 .vop_remove = zfs_freebsd_remove,
7119 .vop_rename = zfs_freebsd_rename,
7120 .vop_pathconf = zfs_freebsd_pathconf,
7121 .vop_bmap = zfs_freebsd_bmap,
7122 .vop_fid = zfs_freebsd_fid,
7123 .vop_getextattr = zfs_getextattr,
7124 .vop_deleteextattr = zfs_deleteextattr,
7125 .vop_setextattr = zfs_setextattr,
7126 .vop_listextattr = zfs_listextattr,
7127 .vop_getacl = zfs_freebsd_getacl,
7128 .vop_setacl = zfs_freebsd_setacl,
7129 .vop_aclcheck = zfs_freebsd_aclcheck,
7130 .vop_getpages = zfs_freebsd_getpages,
7131 .vop_putpages = zfs_freebsd_putpages,
7132 .vop_vptocnp = zfs_vptocnp,
7135 struct vop_vector zfs_fifoops = {
7136 .vop_default = &fifo_specops,
7137 .vop_fsync = zfs_freebsd_fsync,
7138 .vop_access = zfs_freebsd_access,
7139 .vop_getattr = zfs_freebsd_getattr,
7140 .vop_inactive = zfs_freebsd_inactive,
7141 .vop_read = VOP_PANIC,
7142 .vop_reclaim = zfs_freebsd_reclaim,
7143 .vop_setattr = zfs_freebsd_setattr,
7144 .vop_write = VOP_PANIC,
7145 .vop_pathconf = zfs_freebsd_fifo_pathconf,
7146 .vop_fid = zfs_freebsd_fid,
7147 .vop_getacl = zfs_freebsd_getacl,
7148 .vop_setacl = zfs_freebsd_setacl,
7149 .vop_aclcheck = zfs_freebsd_aclcheck,
7153 * special share hidden files vnode operations template
7155 struct vop_vector zfs_shareops = {
7156 .vop_default = &default_vnodeops,
7157 .vop_access = zfs_freebsd_access,
7158 .vop_inactive = zfs_freebsd_inactive,
7159 .vop_reclaim = zfs_freebsd_reclaim,
7160 .vop_fid = zfs_freebsd_fid,
7161 .vop_pathconf = zfs_freebsd_pathconf,