4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
27 /* Portions Copyright 2007 Jeremy Teo */
28 /* Portions Copyright 2010 Robert Milkowski */
30 #include <sys/types.h>
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
38 #include <sys/vnode.h>
42 #include <sys/taskq.h>
44 #include <sys/atomic.h>
45 #include <sys/namei.h>
47 #include <sys/cmn_err.h>
48 #include <sys/errno.h>
49 #include <sys/unistd.h>
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/fs/zfs.h>
54 #include <sys/dmu_objset.h>
60 #include <sys/dirent.h>
61 #include <sys/policy.h>
62 #include <sys/sunddi.h>
63 #include <sys/filio.h>
65 #include <sys/zfs_ctldir.h>
66 #include <sys/zfs_fuid.h>
67 #include <sys/zfs_sa.h>
69 #include <sys/zfs_rlock.h>
70 #include <sys/extdirent.h>
71 #include <sys/kidmap.h>
74 #include <sys/sched.h>
76 #include <vm/vm_param.h>
77 #include <vm/vm_pageout.h>
82 * Each vnode op performs some logical unit of work. To do this, the ZPL must
83 * properly lock its in-core state, create a DMU transaction, do the work,
84 * record this work in the intent log (ZIL), commit the DMU transaction,
85 * and wait for the intent log to commit if it is a synchronous operation.
86 * Moreover, the vnode ops must work in both normal and log replay context.
87 * The ordering of events is important to avoid deadlocks and references
88 * to freed memory. The example below illustrates the following Big Rules:
90 * (1) A check must be made in each zfs thread for a mounted file system.
91 * This is done avoiding races using ZFS_ENTER(zfsvfs).
92 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
94 * can return EIO from the calling function.
96 * (2) VN_RELE() should always be the last thing except for zil_commit()
97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
98 * First, if it's the last reference, the vnode/znode
99 * can be freed, so the zp may point to freed memory. Second, the last
100 * reference will call zfs_zinactive(), which may induce a lot of work --
101 * pushing cached pages (which acquires range locks) and syncing out
102 * cached atime changes. Third, zfs_zinactive() may require a new tx,
103 * which could deadlock the system if you were already holding one.
104 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
106 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
107 * as they can span dmu_tx_assign() calls.
109 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
110 * dmu_tx_assign(). This is critical because we don't want to block
111 * while holding locks.
113 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
114 * reduces lock contention and CPU usage when we must wait (note that if
115 * throughput is constrained by the storage, nearly every transaction
118 * Note, in particular, that if a lock is sometimes acquired before
119 * the tx assigns, and sometimes after (e.g. z_lock), then failing
120 * to use a non-blocking assign can deadlock the system. The scenario:
122 * Thread A has grabbed a lock before calling dmu_tx_assign().
123 * Thread B is in an already-assigned tx, and blocks for this lock.
124 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
125 * forever, because the previous txg can't quiesce until B's tx commits.
127 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
128 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
129 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
130 * to indicate that this operation has already called dmu_tx_wait().
131 * This will ensure that we don't retry forever, waiting a short bit
134 * (5) If the operation succeeded, generate the intent log entry for it
135 * before dropping locks. This ensures that the ordering of events
136 * in the intent log matches the order in which they actually occurred.
137 * During ZIL replay the zfs_log_* functions will update the sequence
138 * number to indicate the zil transaction has replayed.
140 * (6) At the end of each vnode op, the DMU tx must always commit,
141 * regardless of whether there were any errors.
143 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
144 * to ensure that synchronous semantics are provided when necessary.
146 * In general, this is how things should be ordered in each vnode op:
148 * ZFS_ENTER(zfsvfs); // exit if unmounted
150 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
151 * rw_enter(...); // grab any other locks you need
152 * tx = dmu_tx_create(...); // get DMU tx
153 * dmu_tx_hold_*(); // hold each object you might modify
154 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
156 * rw_exit(...); // drop locks
157 * zfs_dirent_unlock(dl); // unlock directory entry
158 * VN_RELE(...); // release held vnodes
159 * if (error == ERESTART) {
165 * dmu_tx_abort(tx); // abort DMU tx
166 * ZFS_EXIT(zfsvfs); // finished in zfs
167 * return (error); // really out of space
169 * error = do_real_work(); // do whatever this VOP does
171 * zfs_log_*(...); // on success, make ZIL entry
172 * dmu_tx_commit(tx); // commit DMU tx -- error or not
173 * rw_exit(...); // drop locks
174 * zfs_dirent_unlock(dl); // unlock directory entry
175 * VN_RELE(...); // release held vnodes
176 * zil_commit(zilog, foid); // synchronous when necessary
177 * ZFS_EXIT(zfsvfs); // finished in zfs
178 * return (error); // done, report error
183 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
185 znode_t *zp = VTOZ(*vpp);
186 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
191 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
192 ((flag & FAPPEND) == 0)) {
194 return (SET_ERROR(EPERM));
197 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
198 ZTOV(zp)->v_type == VREG &&
199 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
200 if (fs_vscan(*vpp, cr, 0) != 0) {
202 return (SET_ERROR(EACCES));
206 /* Keep a count of the synchronous opens in the znode */
207 if (flag & (FSYNC | FDSYNC))
208 atomic_inc_32(&zp->z_sync_cnt);
216 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
217 caller_context_t *ct)
219 znode_t *zp = VTOZ(vp);
220 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
223 * Clean up any locks held by this process on the vp.
225 cleanlocks(vp, ddi_get_pid(), 0);
226 cleanshares(vp, ddi_get_pid());
231 /* Decrement the synchronous opens in the znode */
232 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
233 atomic_dec_32(&zp->z_sync_cnt);
235 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
236 ZTOV(zp)->v_type == VREG &&
237 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
238 VERIFY(fs_vscan(vp, cr, 1) == 0);
245 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
246 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
249 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
251 znode_t *zp = VTOZ(vp);
252 uint64_t noff = (uint64_t)*off; /* new offset */
257 file_sz = zp->z_size;
258 if (noff >= file_sz) {
259 return (SET_ERROR(ENXIO));
262 if (cmd == _FIO_SEEK_HOLE)
267 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
270 return (SET_ERROR(ENXIO));
273 * We could find a hole that begins after the logical end-of-file,
274 * because dmu_offset_next() only works on whole blocks. If the
275 * EOF falls mid-block, then indicate that the "virtual hole"
276 * at the end of the file begins at the logical EOF, rather than
277 * at the end of the last block.
279 if (noff > file_sz) {
292 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
293 int *rvalp, caller_context_t *ct)
297 dmu_object_info_t doi;
308 * The following two ioctls are used by bfu. Faking out,
309 * necessary to avoid bfu errors.
322 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
323 return (SET_ERROR(EFAULT));
325 off = *(offset_t *)data;
328 zfsvfs = zp->z_zfsvfs;
332 /* offset parameter is in/out */
333 error = zfs_holey(vp, com, &off);
338 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
339 return (SET_ERROR(EFAULT));
341 *(offset_t *)data = off;
346 case _FIO_COUNT_FILLED:
349 * _FIO_COUNT_FILLED adds a new ioctl command which
350 * exposes the number of filled blocks in a
354 zfsvfs = zp->z_zfsvfs;
359 * Wait for all dirty blocks for this object
360 * to get synced out to disk, and the DMU info
363 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
370 * Retrieve fill count from DMU object.
372 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
378 ndata = doi.doi_fill_count;
381 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
382 return (SET_ERROR(EFAULT));
387 return (SET_ERROR(ENOTTY));
391 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
398 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
399 * aligned boundaries, if the range is not aligned. As a result a
400 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
401 * It may happen that all DEV_BSIZE subranges are marked clean and thus
402 * the whole page would be considred clean despite have some dirty data.
403 * For this reason we should shrink the range to DEV_BSIZE aligned
404 * boundaries before calling vm_page_clear_dirty.
406 end = rounddown2(off + nbytes, DEV_BSIZE);
407 off = roundup2(off, DEV_BSIZE);
411 zfs_vmobject_assert_wlocked(obj);
414 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
416 if (vm_page_xbusied(pp)) {
418 * Reference the page before unlocking and
419 * sleeping so that the page daemon is less
420 * likely to reclaim it.
422 vm_page_reference(pp);
424 zfs_vmobject_wunlock(obj);
425 vm_page_busy_sleep(pp, "zfsmwb");
426 zfs_vmobject_wlock(obj);
430 } else if (pp == NULL) {
431 pp = vm_page_alloc(obj, OFF_TO_IDX(start),
432 VM_ALLOC_SYSTEM | VM_ALLOC_IFCACHED |
435 ASSERT(pp != NULL && !pp->valid);
440 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
441 vm_object_pip_add(obj, 1);
442 pmap_remove_write(pp);
444 vm_page_clear_dirty(pp, off, nbytes);
452 page_unbusy(vm_page_t pp)
456 vm_object_pip_subtract(pp->object, 1);
460 page_hold(vnode_t *vp, int64_t start)
466 zfs_vmobject_assert_wlocked(obj);
469 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
471 if (vm_page_xbusied(pp)) {
473 * Reference the page before unlocking and
474 * sleeping so that the page daemon is less
475 * likely to reclaim it.
477 vm_page_reference(pp);
479 zfs_vmobject_wunlock(obj);
480 vm_page_busy_sleep(pp, "zfsmwb");
481 zfs_vmobject_wlock(obj);
485 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
498 page_unhold(vm_page_t pp)
507 * When a file is memory mapped, we must keep the IO data synchronized
508 * between the DMU cache and the memory mapped pages. What this means:
510 * On Write: If we find a memory mapped page, we write to *both*
511 * the page and the dmu buffer.
514 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
515 int segflg, dmu_tx_t *tx)
522 ASSERT(segflg != UIO_NOCOPY);
523 ASSERT(vp->v_mount != NULL);
527 off = start & PAGEOFFSET;
528 zfs_vmobject_wlock(obj);
529 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
531 int nbytes = imin(PAGESIZE - off, len);
533 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
534 zfs_vmobject_wunlock(obj);
536 va = zfs_map_page(pp, &sf);
537 (void) dmu_read(os, oid, start+off, nbytes,
538 va+off, DMU_READ_PREFETCH);;
541 zfs_vmobject_wlock(obj);
547 vm_object_pip_wakeupn(obj, 0);
548 zfs_vmobject_wunlock(obj);
552 * Read with UIO_NOCOPY flag means that sendfile(2) requests
553 * ZFS to populate a range of page cache pages with data.
555 * NOTE: this function could be optimized to pre-allocate
556 * all pages in advance, drain exclusive busy on all of them,
557 * map them into contiguous KVA region and populate them
558 * in one single dmu_read() call.
561 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
563 znode_t *zp = VTOZ(vp);
564 objset_t *os = zp->z_zfsvfs->z_os;
574 ASSERT(uio->uio_segflg == UIO_NOCOPY);
575 ASSERT(vp->v_mount != NULL);
578 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
580 zfs_vmobject_wlock(obj);
581 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
582 int bytes = MIN(PAGESIZE, len);
584 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_SBUSY |
585 VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
586 if (pp->valid == 0) {
587 zfs_vmobject_wunlock(obj);
588 va = zfs_map_page(pp, &sf);
589 error = dmu_read(os, zp->z_id, start, bytes, va,
591 if (bytes != PAGESIZE && error == 0)
592 bzero(va + bytes, PAGESIZE - bytes);
594 zfs_vmobject_wlock(obj);
598 if (pp->wire_count == 0 && pp->valid == 0 &&
602 pp->valid = VM_PAGE_BITS_ALL;
603 vm_page_activate(pp);
607 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
612 uio->uio_resid -= bytes;
613 uio->uio_offset += bytes;
616 zfs_vmobject_wunlock(obj);
621 * When a file is memory mapped, we must keep the IO data synchronized
622 * between the DMU cache and the memory mapped pages. What this means:
624 * On Read: We "read" preferentially from memory mapped pages,
625 * else we default from the dmu buffer.
627 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
628 * the file is memory mapped.
631 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
633 znode_t *zp = VTOZ(vp);
641 ASSERT(vp->v_mount != NULL);
645 start = uio->uio_loffset;
646 off = start & PAGEOFFSET;
647 zfs_vmobject_wlock(obj);
648 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
650 uint64_t bytes = MIN(PAGESIZE - off, len);
652 if (pp = page_hold(vp, start)) {
656 zfs_vmobject_wunlock(obj);
657 va = zfs_map_page(pp, &sf);
658 error = uiomove(va + off, bytes, UIO_READ, uio);
660 zfs_vmobject_wlock(obj);
663 zfs_vmobject_wunlock(obj);
664 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
666 zfs_vmobject_wlock(obj);
673 zfs_vmobject_wunlock(obj);
677 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
680 * Read bytes from specified file into supplied buffer.
682 * IN: vp - vnode of file to be read from.
683 * uio - structure supplying read location, range info,
685 * ioflag - SYNC flags; used to provide FRSYNC semantics.
686 * cr - credentials of caller.
687 * ct - caller context
689 * OUT: uio - updated offset and range, buffer filled.
691 * RETURN: 0 on success, error code on failure.
694 * vp - atime updated if byte count > 0
698 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
700 znode_t *zp = VTOZ(vp);
701 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
710 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
712 return (SET_ERROR(EACCES));
716 * Validate file offset
718 if (uio->uio_loffset < (offset_t)0) {
720 return (SET_ERROR(EINVAL));
724 * Fasttrack empty reads
726 if (uio->uio_resid == 0) {
732 * Check for mandatory locks
734 if (MANDMODE(zp->z_mode)) {
735 if (error = chklock(vp, FREAD,
736 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
743 * If we're in FRSYNC mode, sync out this znode before reading it.
746 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
747 zil_commit(zfsvfs->z_log, zp->z_id);
750 * Lock the range against changes.
752 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
755 * If we are reading past end-of-file we can skip
756 * to the end; but we might still need to set atime.
758 if (uio->uio_loffset >= zp->z_size) {
763 ASSERT(uio->uio_loffset < zp->z_size);
764 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
767 if ((uio->uio_extflg == UIO_XUIO) &&
768 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
770 int blksz = zp->z_blksz;
771 uint64_t offset = uio->uio_loffset;
773 xuio = (xuio_t *)uio;
775 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
778 ASSERT(offset + n <= blksz);
781 (void) dmu_xuio_init(xuio, nblk);
783 if (vn_has_cached_data(vp)) {
785 * For simplicity, we always allocate a full buffer
786 * even if we only expect to read a portion of a block.
788 while (--nblk >= 0) {
789 (void) dmu_xuio_add(xuio,
790 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
798 nbytes = MIN(n, zfs_read_chunk_size -
799 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
802 if (uio->uio_segflg == UIO_NOCOPY)
803 error = mappedread_sf(vp, nbytes, uio);
805 #endif /* __FreeBSD__ */
806 if (vn_has_cached_data(vp)) {
807 error = mappedread(vp, nbytes, uio);
809 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
813 /* convert checksum errors into IO errors */
815 error = SET_ERROR(EIO);
822 zfs_range_unlock(rl);
824 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
830 * Write the bytes to a file.
832 * IN: vp - vnode of file to be written to.
833 * uio - structure supplying write location, range info,
835 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
836 * set if in append mode.
837 * cr - credentials of caller.
838 * ct - caller context (NFS/CIFS fem monitor only)
840 * OUT: uio - updated offset and range.
842 * RETURN: 0 on success, error code on failure.
845 * vp - ctime|mtime updated if byte count > 0
850 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
852 znode_t *zp = VTOZ(vp);
853 rlim64_t limit = MAXOFFSET_T;
854 ssize_t start_resid = uio->uio_resid;
858 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
863 int max_blksz = zfsvfs->z_max_blksz;
866 iovec_t *aiov = NULL;
869 int iovcnt = uio->uio_iovcnt;
870 iovec_t *iovp = uio->uio_iov;
873 sa_bulk_attr_t bulk[4];
874 uint64_t mtime[2], ctime[2];
877 * Fasttrack empty write
883 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
889 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
890 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
891 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
893 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
897 * If immutable or not appending then return EPERM
899 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
900 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
901 (uio->uio_loffset < zp->z_size))) {
903 return (SET_ERROR(EPERM));
906 zilog = zfsvfs->z_log;
909 * Validate file offset
911 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
914 return (SET_ERROR(EINVAL));
918 * Check for mandatory locks before calling zfs_range_lock()
919 * in order to prevent a deadlock with locks set via fcntl().
921 if (MANDMODE((mode_t)zp->z_mode) &&
922 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
929 * Pre-fault the pages to ensure slow (eg NFS) pages
931 * Skip this if uio contains loaned arc_buf.
933 if ((uio->uio_extflg == UIO_XUIO) &&
934 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
935 xuio = (xuio_t *)uio;
937 uio_prefaultpages(MIN(n, max_blksz), uio);
941 * If in append mode, set the io offset pointer to eof.
943 if (ioflag & FAPPEND) {
945 * Obtain an appending range lock to guarantee file append
946 * semantics. We reset the write offset once we have the lock.
948 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
950 if (rl->r_len == UINT64_MAX) {
952 * We overlocked the file because this write will cause
953 * the file block size to increase.
954 * Note that zp_size cannot change with this lock held.
958 uio->uio_loffset = woff;
961 * Note that if the file block size will change as a result of
962 * this write, then this range lock will lock the entire file
963 * so that we can re-write the block safely.
965 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
968 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
969 zfs_range_unlock(rl);
975 zfs_range_unlock(rl);
977 return (SET_ERROR(EFBIG));
980 if ((woff + n) > limit || woff > (limit - n))
983 /* Will this write extend the file length? */
984 write_eof = (woff + n > zp->z_size);
986 end_size = MAX(zp->z_size, woff + n);
989 * Write the file in reasonable size chunks. Each chunk is written
990 * in a separate transaction; this keeps the intent log records small
991 * and allows us to do more fine-grained space accounting.
995 woff = uio->uio_loffset;
996 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
997 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
999 dmu_return_arcbuf(abuf);
1000 error = SET_ERROR(EDQUOT);
1004 if (xuio && abuf == NULL) {
1005 ASSERT(i_iov < iovcnt);
1006 aiov = &iovp[i_iov];
1007 abuf = dmu_xuio_arcbuf(xuio, i_iov);
1008 dmu_xuio_clear(xuio, i_iov);
1009 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
1010 iovec_t *, aiov, arc_buf_t *, abuf);
1011 ASSERT((aiov->iov_base == abuf->b_data) ||
1012 ((char *)aiov->iov_base - (char *)abuf->b_data +
1013 aiov->iov_len == arc_buf_size(abuf)));
1015 } else if (abuf == NULL && n >= max_blksz &&
1016 woff >= zp->z_size &&
1017 P2PHASE(woff, max_blksz) == 0 &&
1018 zp->z_blksz == max_blksz) {
1020 * This write covers a full block. "Borrow" a buffer
1021 * from the dmu so that we can fill it before we enter
1022 * a transaction. This avoids the possibility of
1023 * holding up the transaction if the data copy hangs
1024 * up on a pagefault (e.g., from an NFS server mapping).
1028 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
1030 ASSERT(abuf != NULL);
1031 ASSERT(arc_buf_size(abuf) == max_blksz);
1032 if (error = uiocopy(abuf->b_data, max_blksz,
1033 UIO_WRITE, uio, &cbytes)) {
1034 dmu_return_arcbuf(abuf);
1037 ASSERT(cbytes == max_blksz);
1041 * Start a transaction.
1043 tx = dmu_tx_create(zfsvfs->z_os);
1044 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1045 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
1046 zfs_sa_upgrade_txholds(tx, zp);
1047 error = dmu_tx_assign(tx, TXG_WAIT);
1051 dmu_return_arcbuf(abuf);
1056 * If zfs_range_lock() over-locked we grow the blocksize
1057 * and then reduce the lock range. This will only happen
1058 * on the first iteration since zfs_range_reduce() will
1059 * shrink down r_len to the appropriate size.
1061 if (rl->r_len == UINT64_MAX) {
1064 if (zp->z_blksz > max_blksz) {
1066 * File's blocksize is already larger than the
1067 * "recordsize" property. Only let it grow to
1068 * the next power of 2.
1070 ASSERT(!ISP2(zp->z_blksz));
1071 new_blksz = MIN(end_size,
1072 1 << highbit64(zp->z_blksz));
1074 new_blksz = MIN(end_size, max_blksz);
1076 zfs_grow_blocksize(zp, new_blksz, tx);
1077 zfs_range_reduce(rl, woff, n);
1081 * XXX - should we really limit each write to z_max_blksz?
1082 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1084 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1086 if (woff + nbytes > zp->z_size)
1087 vnode_pager_setsize(vp, woff + nbytes);
1090 tx_bytes = uio->uio_resid;
1091 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1093 tx_bytes -= uio->uio_resid;
1096 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1098 * If this is not a full block write, but we are
1099 * extending the file past EOF and this data starts
1100 * block-aligned, use assign_arcbuf(). Otherwise,
1101 * write via dmu_write().
1103 if (tx_bytes < max_blksz && (!write_eof ||
1104 aiov->iov_base != abuf->b_data)) {
1106 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1107 aiov->iov_len, aiov->iov_base, tx);
1108 dmu_return_arcbuf(abuf);
1109 xuio_stat_wbuf_copied();
1111 ASSERT(xuio || tx_bytes == max_blksz);
1112 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1115 ASSERT(tx_bytes <= uio->uio_resid);
1116 uioskip(uio, tx_bytes);
1118 if (tx_bytes && vn_has_cached_data(vp)) {
1119 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1120 zp->z_id, uio->uio_segflg, tx);
1124 * If we made no progress, we're done. If we made even
1125 * partial progress, update the znode and ZIL accordingly.
1127 if (tx_bytes == 0) {
1128 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1129 (void *)&zp->z_size, sizeof (uint64_t), tx);
1136 * Clear Set-UID/Set-GID bits on successful write if not
1137 * privileged and at least one of the excute bits is set.
1139 * It would be nice to to this after all writes have
1140 * been done, but that would still expose the ISUID/ISGID
1141 * to another app after the partial write is committed.
1143 * Note: we don't call zfs_fuid_map_id() here because
1144 * user 0 is not an ephemeral uid.
1146 mutex_enter(&zp->z_acl_lock);
1147 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1148 (S_IXUSR >> 6))) != 0 &&
1149 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1150 secpolicy_vnode_setid_retain(vp, cr,
1151 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1153 zp->z_mode &= ~(S_ISUID | S_ISGID);
1154 newmode = zp->z_mode;
1155 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1156 (void *)&newmode, sizeof (uint64_t), tx);
1158 mutex_exit(&zp->z_acl_lock);
1160 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1164 * Update the file size (zp_size) if it has changed;
1165 * account for possible concurrent updates.
1167 while ((end_size = zp->z_size) < uio->uio_loffset) {
1168 (void) atomic_cas_64(&zp->z_size, end_size,
1173 * If we are replaying and eof is non zero then force
1174 * the file size to the specified eof. Note, there's no
1175 * concurrency during replay.
1177 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1178 zp->z_size = zfsvfs->z_replay_eof;
1180 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1182 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1187 ASSERT(tx_bytes == nbytes);
1192 uio_prefaultpages(MIN(n, max_blksz), uio);
1196 zfs_range_unlock(rl);
1199 * If we're in replay mode, or we made no progress, return error.
1200 * Otherwise, it's at least a partial write, so it's successful.
1202 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1207 if (ioflag & (FSYNC | FDSYNC) ||
1208 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1209 zil_commit(zilog, zp->z_id);
1216 zfs_get_done(zgd_t *zgd, int error)
1218 znode_t *zp = zgd->zgd_private;
1219 objset_t *os = zp->z_zfsvfs->z_os;
1222 dmu_buf_rele(zgd->zgd_db, zgd);
1224 zfs_range_unlock(zgd->zgd_rl);
1227 * Release the vnode asynchronously as we currently have the
1228 * txg stopped from syncing.
1230 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1232 if (error == 0 && zgd->zgd_bp)
1233 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1235 kmem_free(zgd, sizeof (zgd_t));
1239 static int zil_fault_io = 0;
1243 * Get data to generate a TX_WRITE intent log record.
1246 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1248 zfsvfs_t *zfsvfs = arg;
1249 objset_t *os = zfsvfs->z_os;
1251 uint64_t object = lr->lr_foid;
1252 uint64_t offset = lr->lr_offset;
1253 uint64_t size = lr->lr_length;
1254 blkptr_t *bp = &lr->lr_blkptr;
1259 ASSERT(zio != NULL);
1263 * Nothing to do if the file has been removed
1265 if (zfs_zget(zfsvfs, object, &zp) != 0)
1266 return (SET_ERROR(ENOENT));
1267 if (zp->z_unlinked) {
1269 * Release the vnode asynchronously as we currently have the
1270 * txg stopped from syncing.
1272 VN_RELE_ASYNC(ZTOV(zp),
1273 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1274 return (SET_ERROR(ENOENT));
1277 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1278 zgd->zgd_zilog = zfsvfs->z_log;
1279 zgd->zgd_private = zp;
1282 * Write records come in two flavors: immediate and indirect.
1283 * For small writes it's cheaper to store the data with the
1284 * log record (immediate); for large writes it's cheaper to
1285 * sync the data and get a pointer to it (indirect) so that
1286 * we don't have to write the data twice.
1288 if (buf != NULL) { /* immediate write */
1289 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1290 /* test for truncation needs to be done while range locked */
1291 if (offset >= zp->z_size) {
1292 error = SET_ERROR(ENOENT);
1294 error = dmu_read(os, object, offset, size, buf,
1295 DMU_READ_NO_PREFETCH);
1297 ASSERT(error == 0 || error == ENOENT);
1298 } else { /* indirect write */
1300 * Have to lock the whole block to ensure when it's
1301 * written out and it's checksum is being calculated
1302 * that no one can change the data. We need to re-check
1303 * blocksize after we get the lock in case it's changed!
1308 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1310 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1312 if (zp->z_blksz == size)
1315 zfs_range_unlock(zgd->zgd_rl);
1317 /* test for truncation needs to be done while range locked */
1318 if (lr->lr_offset >= zp->z_size)
1319 error = SET_ERROR(ENOENT);
1322 error = SET_ERROR(EIO);
1327 error = dmu_buf_hold(os, object, offset, zgd, &db,
1328 DMU_READ_NO_PREFETCH);
1331 blkptr_t *obp = dmu_buf_get_blkptr(db);
1333 ASSERT(BP_IS_HOLE(bp));
1340 ASSERT(db->db_offset == offset);
1341 ASSERT(db->db_size == size);
1343 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1345 ASSERT(error || lr->lr_length <= zp->z_blksz);
1348 * On success, we need to wait for the write I/O
1349 * initiated by dmu_sync() to complete before we can
1350 * release this dbuf. We will finish everything up
1351 * in the zfs_get_done() callback.
1356 if (error == EALREADY) {
1357 lr->lr_common.lrc_txtype = TX_WRITE2;
1363 zfs_get_done(zgd, error);
1370 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1371 caller_context_t *ct)
1373 znode_t *zp = VTOZ(vp);
1374 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1380 if (flag & V_ACE_MASK)
1381 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1383 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1390 * If vnode is for a device return a specfs vnode instead.
1393 specvp_check(vnode_t **vpp, cred_t *cr)
1397 if (IS_DEVVP(*vpp)) {
1400 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1403 error = SET_ERROR(ENOSYS);
1411 * Lookup an entry in a directory, or an extended attribute directory.
1412 * If it exists, return a held vnode reference for it.
1414 * IN: dvp - vnode of directory to search.
1415 * nm - name of entry to lookup.
1416 * pnp - full pathname to lookup [UNUSED].
1417 * flags - LOOKUP_XATTR set if looking for an attribute.
1418 * rdir - root directory vnode [UNUSED].
1419 * cr - credentials of caller.
1420 * ct - caller context
1421 * direntflags - directory lookup flags
1422 * realpnp - returned pathname.
1424 * OUT: vpp - vnode of located entry, NULL if not found.
1426 * RETURN: 0 on success, error code on failure.
1433 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1434 int nameiop, cred_t *cr, kthread_t *td, int flags)
1436 znode_t *zdp = VTOZ(dvp);
1437 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1439 int *direntflags = NULL;
1440 void *realpnp = NULL;
1443 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1445 if (dvp->v_type != VDIR) {
1446 return (SET_ERROR(ENOTDIR));
1447 } else if (zdp->z_sa_hdl == NULL) {
1448 return (SET_ERROR(EIO));
1451 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1452 error = zfs_fastaccesschk_execute(zdp, cr);
1460 vnode_t *tvp = dnlc_lookup(dvp, nm);
1463 error = zfs_fastaccesschk_execute(zdp, cr);
1468 if (tvp == DNLC_NO_VNODE) {
1470 return (SET_ERROR(ENOENT));
1473 return (specvp_check(vpp, cr));
1479 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1486 if (flags & LOOKUP_XATTR) {
1489 * If the xattr property is off, refuse the lookup request.
1491 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1493 return (SET_ERROR(EINVAL));
1498 * We don't allow recursive attributes..
1499 * Maybe someday we will.
1501 if (zdp->z_pflags & ZFS_XATTR) {
1503 return (SET_ERROR(EINVAL));
1506 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1512 * Do we have permission to get into attribute directory?
1515 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1525 if (dvp->v_type != VDIR) {
1527 return (SET_ERROR(ENOTDIR));
1531 * Check accessibility of directory.
1534 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1539 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1540 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1542 return (SET_ERROR(EILSEQ));
1545 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1547 error = specvp_check(vpp, cr);
1549 /* Translate errors and add SAVENAME when needed. */
1550 if (cnp->cn_flags & ISLASTCN) {
1554 if (error == ENOENT) {
1555 error = EJUSTRETURN;
1556 cnp->cn_flags |= SAVENAME;
1562 cnp->cn_flags |= SAVENAME;
1566 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) {
1569 if (cnp->cn_flags & ISDOTDOT) {
1570 ltype = VOP_ISLOCKED(dvp);
1574 error = vn_lock(*vpp, cnp->cn_lkflags);
1575 if (cnp->cn_flags & ISDOTDOT)
1576 vn_lock(dvp, ltype | LK_RETRY);
1586 #ifdef FREEBSD_NAMECACHE
1588 * Insert name into cache (as non-existent) if appropriate.
1590 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1591 cache_enter(dvp, *vpp, cnp);
1593 * Insert name into cache if appropriate.
1595 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1596 if (!(cnp->cn_flags & ISLASTCN) ||
1597 (nameiop != DELETE && nameiop != RENAME)) {
1598 cache_enter(dvp, *vpp, cnp);
1607 * Attempt to create a new entry in a directory. If the entry
1608 * already exists, truncate the file if permissible, else return
1609 * an error. Return the vp of the created or trunc'd file.
1611 * IN: dvp - vnode of directory to put new file entry in.
1612 * name - name of new file entry.
1613 * vap - attributes of new file.
1614 * excl - flag indicating exclusive or non-exclusive mode.
1615 * mode - mode to open file with.
1616 * cr - credentials of caller.
1617 * flag - large file flag [UNUSED].
1618 * ct - caller context
1619 * vsecp - ACL to be set
1621 * OUT: vpp - vnode of created or trunc'd entry.
1623 * RETURN: 0 on success, error code on failure.
1626 * dvp - ctime|mtime updated if new entry created
1627 * vp - ctime|mtime always, atime if new
1632 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1633 vnode_t **vpp, cred_t *cr, kthread_t *td)
1635 znode_t *zp, *dzp = VTOZ(dvp);
1636 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1644 gid_t gid = crgetgid(cr);
1645 zfs_acl_ids_t acl_ids;
1646 boolean_t fuid_dirtied;
1647 boolean_t have_acl = B_FALSE;
1648 boolean_t waited = B_FALSE;
1653 * If we have an ephemeral id, ACL, or XVATTR then
1654 * make sure file system is at proper version
1657 ksid = crgetsid(cr, KSID_OWNER);
1659 uid = ksid_getid(ksid);
1663 if (zfsvfs->z_use_fuids == B_FALSE &&
1664 (vsecp || (vap->va_mask & AT_XVATTR) ||
1665 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1666 return (SET_ERROR(EINVAL));
1671 zilog = zfsvfs->z_log;
1673 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1674 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1676 return (SET_ERROR(EILSEQ));
1679 if (vap->va_mask & AT_XVATTR) {
1680 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1681 crgetuid(cr), cr, vap->va_type)) != 0) {
1687 getnewvnode_reserve(1);
1692 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1693 vap->va_mode &= ~S_ISVTX;
1695 if (*name == '\0') {
1697 * Null component name refers to the directory itself.
1704 /* possible VN_HOLD(zp) */
1707 if (flag & FIGNORECASE)
1710 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1714 zfs_acl_ids_free(&acl_ids);
1715 if (strcmp(name, "..") == 0)
1716 error = SET_ERROR(EISDIR);
1717 getnewvnode_drop_reserve();
1727 * Create a new file object and update the directory
1730 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1732 zfs_acl_ids_free(&acl_ids);
1737 * We only support the creation of regular files in
1738 * extended attribute directories.
1741 if ((dzp->z_pflags & ZFS_XATTR) &&
1742 (vap->va_type != VREG)) {
1744 zfs_acl_ids_free(&acl_ids);
1745 error = SET_ERROR(EINVAL);
1749 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1750 cr, vsecp, &acl_ids)) != 0)
1754 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1755 zfs_acl_ids_free(&acl_ids);
1756 error = SET_ERROR(EDQUOT);
1760 tx = dmu_tx_create(os);
1762 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1763 ZFS_SA_BASE_ATTR_SIZE);
1765 fuid_dirtied = zfsvfs->z_fuid_dirty;
1767 zfs_fuid_txhold(zfsvfs, tx);
1768 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1769 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1770 if (!zfsvfs->z_use_sa &&
1771 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1772 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1773 0, acl_ids.z_aclp->z_acl_bytes);
1775 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1777 zfs_dirent_unlock(dl);
1778 if (error == ERESTART) {
1784 zfs_acl_ids_free(&acl_ids);
1786 getnewvnode_drop_reserve();
1790 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1793 zfs_fuid_sync(zfsvfs, tx);
1795 (void) zfs_link_create(dl, zp, tx, ZNEW);
1796 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1797 if (flag & FIGNORECASE)
1799 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1800 vsecp, acl_ids.z_fuidp, vap);
1801 zfs_acl_ids_free(&acl_ids);
1804 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1807 zfs_acl_ids_free(&acl_ids);
1811 * A directory entry already exists for this name.
1814 * Can't truncate an existing file if in exclusive mode.
1817 error = SET_ERROR(EEXIST);
1821 * Can't open a directory for writing.
1823 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1824 error = SET_ERROR(EISDIR);
1828 * Verify requested access to file.
1830 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1834 mutex_enter(&dzp->z_lock);
1836 mutex_exit(&dzp->z_lock);
1839 * Truncate regular files if requested.
1841 if ((ZTOV(zp)->v_type == VREG) &&
1842 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1843 /* we can't hold any locks when calling zfs_freesp() */
1844 zfs_dirent_unlock(dl);
1846 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1848 vnevent_create(ZTOV(zp), ct);
1853 getnewvnode_drop_reserve();
1855 zfs_dirent_unlock(dl);
1862 error = specvp_check(vpp, cr);
1865 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1866 zil_commit(zilog, 0);
1873 * Remove an entry from a directory.
1875 * IN: dvp - vnode of directory to remove entry from.
1876 * name - name of entry to remove.
1877 * cr - credentials of caller.
1878 * ct - caller context
1879 * flags - case flags
1881 * RETURN: 0 on success, error code on failure.
1885 * vp - ctime (if nlink > 0)
1888 uint64_t null_xattr = 0;
1892 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1895 znode_t *zp, *dzp = VTOZ(dvp);
1898 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1900 uint64_t acl_obj, xattr_obj;
1901 uint64_t xattr_obj_unlinked = 0;
1905 boolean_t may_delete_now, delete_now = FALSE;
1906 boolean_t unlinked, toobig = FALSE;
1908 pathname_t *realnmp = NULL;
1912 boolean_t waited = B_FALSE;
1916 zilog = zfsvfs->z_log;
1918 if (flags & FIGNORECASE) {
1928 * Attempt to lock directory; fail if entry doesn't exist.
1930 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1940 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1945 * Need to use rmdir for removing directories.
1947 if (vp->v_type == VDIR) {
1948 error = SET_ERROR(EPERM);
1952 vnevent_remove(vp, dvp, name, ct);
1955 dnlc_remove(dvp, realnmp->pn_buf);
1957 dnlc_remove(dvp, name);
1960 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1964 * We may delete the znode now, or we may put it in the unlinked set;
1965 * it depends on whether we're the last link, and on whether there are
1966 * other holds on the vnode. So we dmu_tx_hold() the right things to
1967 * allow for either case.
1970 tx = dmu_tx_create(zfsvfs->z_os);
1971 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1972 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1973 zfs_sa_upgrade_txholds(tx, zp);
1974 zfs_sa_upgrade_txholds(tx, dzp);
1975 if (may_delete_now) {
1977 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1978 /* if the file is too big, only hold_free a token amount */
1979 dmu_tx_hold_free(tx, zp->z_id, 0,
1980 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1983 /* are there any extended attributes? */
1984 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1985 &xattr_obj, sizeof (xattr_obj));
1986 if (error == 0 && xattr_obj) {
1987 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1989 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1990 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1993 mutex_enter(&zp->z_lock);
1994 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1995 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1996 mutex_exit(&zp->z_lock);
1998 /* charge as an update -- would be nice not to charge at all */
1999 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2002 * Mark this transaction as typically resulting in a net free of
2003 * space, unless object removal will be delayed indefinitely
2004 * (due to active holds on the vnode due to the file being open).
2007 dmu_tx_mark_netfree(tx);
2009 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2011 zfs_dirent_unlock(dl);
2015 if (error == ERESTART) {
2029 * Remove the directory entry.
2031 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
2040 * Hold z_lock so that we can make sure that the ACL obj
2041 * hasn't changed. Could have been deleted due to
2044 mutex_enter(&zp->z_lock);
2046 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2047 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
2048 delete_now = may_delete_now && !toobig &&
2049 vp->v_count == 1 && !vn_has_cached_data(vp) &&
2050 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
2057 panic("zfs_remove: delete_now branch taken");
2059 if (xattr_obj_unlinked) {
2060 ASSERT3U(xzp->z_links, ==, 2);
2061 mutex_enter(&xzp->z_lock);
2062 xzp->z_unlinked = 1;
2064 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
2065 &xzp->z_links, sizeof (xzp->z_links), tx);
2066 ASSERT3U(error, ==, 0);
2067 mutex_exit(&xzp->z_lock);
2068 zfs_unlinked_add(xzp, tx);
2071 error = sa_remove(zp->z_sa_hdl,
2072 SA_ZPL_XATTR(zfsvfs), tx);
2074 error = sa_update(zp->z_sa_hdl,
2075 SA_ZPL_XATTR(zfsvfs), &null_xattr,
2076 sizeof (uint64_t), tx);
2081 ASSERT0(vp->v_count);
2083 mutex_exit(&zp->z_lock);
2084 zfs_znode_delete(zp, tx);
2085 } else if (unlinked) {
2086 mutex_exit(&zp->z_lock);
2087 zfs_unlinked_add(zp, tx);
2089 vp->v_vflag |= VV_NOSYNC;
2094 if (flags & FIGNORECASE)
2096 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2103 zfs_dirent_unlock(dl);
2110 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2111 zil_commit(zilog, 0);
2118 * Create a new directory and insert it into dvp using the name
2119 * provided. Return a pointer to the inserted directory.
2121 * IN: dvp - vnode of directory to add subdir to.
2122 * dirname - name of new directory.
2123 * vap - attributes of new directory.
2124 * cr - credentials of caller.
2125 * ct - caller context
2126 * flags - case flags
2127 * vsecp - ACL to be set
2129 * OUT: vpp - vnode of created directory.
2131 * RETURN: 0 on success, error code on failure.
2134 * dvp - ctime|mtime updated
2135 * vp - ctime|mtime|atime updated
2139 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
2140 caller_context_t *ct, int flags, vsecattr_t *vsecp)
2142 znode_t *zp, *dzp = VTOZ(dvp);
2143 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2152 gid_t gid = crgetgid(cr);
2153 zfs_acl_ids_t acl_ids;
2154 boolean_t fuid_dirtied;
2155 boolean_t waited = B_FALSE;
2157 ASSERT(vap->va_type == VDIR);
2160 * If we have an ephemeral id, ACL, or XVATTR then
2161 * make sure file system is at proper version
2164 ksid = crgetsid(cr, KSID_OWNER);
2166 uid = ksid_getid(ksid);
2169 if (zfsvfs->z_use_fuids == B_FALSE &&
2170 (vsecp || (vap->va_mask & AT_XVATTR) ||
2171 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2172 return (SET_ERROR(EINVAL));
2176 zilog = zfsvfs->z_log;
2178 if (dzp->z_pflags & ZFS_XATTR) {
2180 return (SET_ERROR(EINVAL));
2183 if (zfsvfs->z_utf8 && u8_validate(dirname,
2184 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2186 return (SET_ERROR(EILSEQ));
2188 if (flags & FIGNORECASE)
2191 if (vap->va_mask & AT_XVATTR) {
2192 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2193 crgetuid(cr), cr, vap->va_type)) != 0) {
2199 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2200 vsecp, &acl_ids)) != 0) {
2205 getnewvnode_reserve(1);
2208 * First make sure the new directory doesn't exist.
2210 * Existence is checked first to make sure we don't return
2211 * EACCES instead of EEXIST which can cause some applications
2217 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
2219 zfs_acl_ids_free(&acl_ids);
2220 getnewvnode_drop_reserve();
2225 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2226 zfs_acl_ids_free(&acl_ids);
2227 zfs_dirent_unlock(dl);
2228 getnewvnode_drop_reserve();
2233 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2234 zfs_acl_ids_free(&acl_ids);
2235 zfs_dirent_unlock(dl);
2236 getnewvnode_drop_reserve();
2238 return (SET_ERROR(EDQUOT));
2242 * Add a new entry to the directory.
2244 tx = dmu_tx_create(zfsvfs->z_os);
2245 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2246 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2247 fuid_dirtied = zfsvfs->z_fuid_dirty;
2249 zfs_fuid_txhold(zfsvfs, tx);
2250 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2251 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2252 acl_ids.z_aclp->z_acl_bytes);
2255 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2256 ZFS_SA_BASE_ATTR_SIZE);
2258 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2260 zfs_dirent_unlock(dl);
2261 if (error == ERESTART) {
2267 zfs_acl_ids_free(&acl_ids);
2269 getnewvnode_drop_reserve();
2277 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2280 zfs_fuid_sync(zfsvfs, tx);
2283 * Now put new name in parent dir.
2285 (void) zfs_link_create(dl, zp, tx, ZNEW);
2289 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2290 if (flags & FIGNORECASE)
2292 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2293 acl_ids.z_fuidp, vap);
2295 zfs_acl_ids_free(&acl_ids);
2299 getnewvnode_drop_reserve();
2301 zfs_dirent_unlock(dl);
2303 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2304 zil_commit(zilog, 0);
2311 * Remove a directory subdir entry. If the current working
2312 * directory is the same as the subdir to be removed, the
2315 * IN: dvp - vnode of directory to remove from.
2316 * name - name of directory to be removed.
2317 * cwd - vnode of current working directory.
2318 * cr - credentials of caller.
2319 * ct - caller context
2320 * flags - case flags
2322 * RETURN: 0 on success, error code on failure.
2325 * dvp - ctime|mtime updated
2329 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2330 caller_context_t *ct, int flags)
2332 znode_t *dzp = VTOZ(dvp);
2335 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2341 boolean_t waited = B_FALSE;
2345 zilog = zfsvfs->z_log;
2347 if (flags & FIGNORECASE)
2353 * Attempt to lock directory; fail if entry doesn't exist.
2355 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2363 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2367 if (vp->v_type != VDIR) {
2368 error = SET_ERROR(ENOTDIR);
2373 error = SET_ERROR(EINVAL);
2377 vnevent_rmdir(vp, dvp, name, ct);
2380 * Grab a lock on the directory to make sure that noone is
2381 * trying to add (or lookup) entries while we are removing it.
2383 rw_enter(&zp->z_name_lock, RW_WRITER);
2386 * Grab a lock on the parent pointer to make sure we play well
2387 * with the treewalk and directory rename code.
2389 rw_enter(&zp->z_parent_lock, RW_WRITER);
2391 tx = dmu_tx_create(zfsvfs->z_os);
2392 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2393 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2394 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2395 zfs_sa_upgrade_txholds(tx, zp);
2396 zfs_sa_upgrade_txholds(tx, dzp);
2397 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2399 rw_exit(&zp->z_parent_lock);
2400 rw_exit(&zp->z_name_lock);
2401 zfs_dirent_unlock(dl);
2403 if (error == ERESTART) {
2414 #ifdef FREEBSD_NAMECACHE
2418 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2421 uint64_t txtype = TX_RMDIR;
2422 if (flags & FIGNORECASE)
2424 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2429 rw_exit(&zp->z_parent_lock);
2430 rw_exit(&zp->z_name_lock);
2431 #ifdef FREEBSD_NAMECACHE
2435 zfs_dirent_unlock(dl);
2439 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2440 zil_commit(zilog, 0);
2447 * Read as many directory entries as will fit into the provided
2448 * buffer from the given directory cursor position (specified in
2449 * the uio structure).
2451 * IN: vp - vnode of directory to read.
2452 * uio - structure supplying read location, range info,
2453 * and return buffer.
2454 * cr - credentials of caller.
2455 * ct - caller context
2456 * flags - case flags
2458 * OUT: uio - updated offset and range, buffer filled.
2459 * eofp - set to true if end-of-file detected.
2461 * RETURN: 0 on success, error code on failure.
2464 * vp - atime updated
2466 * Note that the low 4 bits of the cookie returned by zap is always zero.
2467 * This allows us to use the low range for "special" directory entries:
2468 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2469 * we use the offset 2 for the '.zfs' directory.
2473 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2475 znode_t *zp = VTOZ(vp);
2479 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2484 zap_attribute_t zap;
2485 uint_t bytes_wanted;
2486 uint64_t offset; /* must be unsigned; checks for < 1 */
2492 boolean_t check_sysattrs;
2495 u_long *cooks = NULL;
2501 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2502 &parent, sizeof (parent))) != 0) {
2508 * If we are not given an eof variable,
2515 * Check for valid iov_len.
2517 if (uio->uio_iov->iov_len <= 0) {
2519 return (SET_ERROR(EINVAL));
2523 * Quit if directory has been removed (posix)
2525 if ((*eofp = zp->z_unlinked) != 0) {
2532 offset = uio->uio_loffset;
2533 prefetch = zp->z_zn_prefetch;
2536 * Initialize the iterator cursor.
2540 * Start iteration from the beginning of the directory.
2542 zap_cursor_init(&zc, os, zp->z_id);
2545 * The offset is a serialized cursor.
2547 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2551 * Get space to change directory entries into fs independent format.
2553 iovp = uio->uio_iov;
2554 bytes_wanted = iovp->iov_len;
2555 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2556 bufsize = bytes_wanted;
2557 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2558 odp = (struct dirent64 *)outbuf;
2560 bufsize = bytes_wanted;
2562 odp = (struct dirent64 *)iovp->iov_base;
2564 eodp = (struct edirent *)odp;
2566 if (ncookies != NULL) {
2568 * Minimum entry size is dirent size and 1 byte for a file name.
2570 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2571 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2576 * If this VFS supports the system attribute view interface; and
2577 * we're looking at an extended attribute directory; and we care
2578 * about normalization conflicts on this vfs; then we must check
2579 * for normalization conflicts with the sysattr name space.
2582 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2583 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2584 (flags & V_RDDIR_ENTFLAGS);
2590 * Transform to file-system independent format
2593 while (outcount < bytes_wanted) {
2596 off64_t *next = NULL;
2599 * Special case `.', `..', and `.zfs'.
2602 (void) strcpy(zap.za_name, ".");
2603 zap.za_normalization_conflict = 0;
2606 } else if (offset == 1) {
2607 (void) strcpy(zap.za_name, "..");
2608 zap.za_normalization_conflict = 0;
2611 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2612 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2613 zap.za_normalization_conflict = 0;
2614 objnum = ZFSCTL_INO_ROOT;
2620 if (error = zap_cursor_retrieve(&zc, &zap)) {
2621 if ((*eofp = (error == ENOENT)) != 0)
2627 if (zap.za_integer_length != 8 ||
2628 zap.za_num_integers != 1) {
2629 cmn_err(CE_WARN, "zap_readdir: bad directory "
2630 "entry, obj = %lld, offset = %lld\n",
2631 (u_longlong_t)zp->z_id,
2632 (u_longlong_t)offset);
2633 error = SET_ERROR(ENXIO);
2637 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2639 * MacOS X can extract the object type here such as:
2640 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2642 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2644 if (check_sysattrs && !zap.za_normalization_conflict) {
2646 zap.za_normalization_conflict =
2647 xattr_sysattr_casechk(zap.za_name);
2649 panic("%s:%u: TODO", __func__, __LINE__);
2654 if (flags & V_RDDIR_ACCFILTER) {
2656 * If we have no access at all, don't include
2657 * this entry in the returned information
2660 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2662 if (!zfs_has_access(ezp, cr)) {
2669 if (flags & V_RDDIR_ENTFLAGS)
2670 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2672 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2675 * Will this entry fit in the buffer?
2677 if (outcount + reclen > bufsize) {
2679 * Did we manage to fit anything in the buffer?
2682 error = SET_ERROR(EINVAL);
2687 if (flags & V_RDDIR_ENTFLAGS) {
2689 * Add extended flag entry:
2691 eodp->ed_ino = objnum;
2692 eodp->ed_reclen = reclen;
2693 /* NOTE: ed_off is the offset for the *next* entry */
2694 next = &(eodp->ed_off);
2695 eodp->ed_eflags = zap.za_normalization_conflict ?
2696 ED_CASE_CONFLICT : 0;
2697 (void) strncpy(eodp->ed_name, zap.za_name,
2698 EDIRENT_NAMELEN(reclen));
2699 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2704 odp->d_ino = objnum;
2705 odp->d_reclen = reclen;
2706 odp->d_namlen = strlen(zap.za_name);
2707 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2709 odp = (dirent64_t *)((intptr_t)odp + reclen);
2713 ASSERT(outcount <= bufsize);
2715 /* Prefetch znode */
2717 dmu_prefetch(os, objnum, 0, 0, 0,
2718 ZIO_PRIORITY_SYNC_READ);
2722 * Move to the next entry, fill in the previous offset.
2724 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2725 zap_cursor_advance(&zc);
2726 offset = zap_cursor_serialize(&zc);
2731 if (cooks != NULL) {
2734 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2737 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2739 /* Subtract unused cookies */
2740 if (ncookies != NULL)
2741 *ncookies -= ncooks;
2743 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2744 iovp->iov_base += outcount;
2745 iovp->iov_len -= outcount;
2746 uio->uio_resid -= outcount;
2747 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2749 * Reset the pointer.
2751 offset = uio->uio_loffset;
2755 zap_cursor_fini(&zc);
2756 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2757 kmem_free(outbuf, bufsize);
2759 if (error == ENOENT)
2762 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2764 uio->uio_loffset = offset;
2766 if (error != 0 && cookies != NULL) {
2767 free(*cookies, M_TEMP);
2774 ulong_t zfs_fsync_sync_cnt = 4;
2777 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2779 znode_t *zp = VTOZ(vp);
2780 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2782 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2784 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2787 zil_commit(zfsvfs->z_log, zp->z_id);
2795 * Get the requested file attributes and place them in the provided
2798 * IN: vp - vnode of file.
2799 * vap - va_mask identifies requested attributes.
2800 * If AT_XVATTR set, then optional attrs are requested
2801 * flags - ATTR_NOACLCHECK (CIFS server context)
2802 * cr - credentials of caller.
2803 * ct - caller context
2805 * OUT: vap - attribute values.
2807 * RETURN: 0 (always succeeds).
2811 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2812 caller_context_t *ct)
2814 znode_t *zp = VTOZ(vp);
2815 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2818 u_longlong_t nblocks;
2820 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2821 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2822 xoptattr_t *xoap = NULL;
2823 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2824 sa_bulk_attr_t bulk[4];
2830 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2832 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2833 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2834 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2835 if (vp->v_type == VBLK || vp->v_type == VCHR)
2836 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2839 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2845 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2846 * Also, if we are the owner don't bother, since owner should
2847 * always be allowed to read basic attributes of file.
2849 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2850 (vap->va_uid != crgetuid(cr))) {
2851 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2859 * Return all attributes. It's cheaper to provide the answer
2860 * than to determine whether we were asked the question.
2863 mutex_enter(&zp->z_lock);
2864 vap->va_type = IFTOVT(zp->z_mode);
2865 vap->va_mode = zp->z_mode & ~S_IFMT;
2867 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2869 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2871 vap->va_nodeid = zp->z_id;
2872 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2873 links = zp->z_links + 1;
2875 links = zp->z_links;
2876 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */
2877 vap->va_size = zp->z_size;
2879 vap->va_rdev = vp->v_rdev;
2881 if (vp->v_type == VBLK || vp->v_type == VCHR)
2882 vap->va_rdev = zfs_cmpldev(rdev);
2884 vap->va_seq = zp->z_seq;
2885 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2886 vap->va_filerev = zp->z_seq;
2889 * Add in any requested optional attributes and the create time.
2890 * Also set the corresponding bits in the returned attribute bitmap.
2892 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2893 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2895 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2896 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2899 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2900 xoap->xoa_readonly =
2901 ((zp->z_pflags & ZFS_READONLY) != 0);
2902 XVA_SET_RTN(xvap, XAT_READONLY);
2905 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2907 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2908 XVA_SET_RTN(xvap, XAT_SYSTEM);
2911 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2913 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2914 XVA_SET_RTN(xvap, XAT_HIDDEN);
2917 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2918 xoap->xoa_nounlink =
2919 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2920 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2923 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2924 xoap->xoa_immutable =
2925 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2926 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2929 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2930 xoap->xoa_appendonly =
2931 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2932 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2935 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2937 ((zp->z_pflags & ZFS_NODUMP) != 0);
2938 XVA_SET_RTN(xvap, XAT_NODUMP);
2941 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2943 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2944 XVA_SET_RTN(xvap, XAT_OPAQUE);
2947 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2948 xoap->xoa_av_quarantined =
2949 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2950 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2953 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2954 xoap->xoa_av_modified =
2955 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2956 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2959 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2960 vp->v_type == VREG) {
2961 zfs_sa_get_scanstamp(zp, xvap);
2964 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2967 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2968 times, sizeof (times));
2969 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2970 XVA_SET_RTN(xvap, XAT_CREATETIME);
2973 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2974 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2975 XVA_SET_RTN(xvap, XAT_REPARSE);
2977 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2978 xoap->xoa_generation = zp->z_gen;
2979 XVA_SET_RTN(xvap, XAT_GEN);
2982 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2984 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2985 XVA_SET_RTN(xvap, XAT_OFFLINE);
2988 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2990 ((zp->z_pflags & ZFS_SPARSE) != 0);
2991 XVA_SET_RTN(xvap, XAT_SPARSE);
2995 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2996 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2997 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2998 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
3000 mutex_exit(&zp->z_lock);
3002 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
3003 vap->va_blksize = blksize;
3004 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
3006 if (zp->z_blksz == 0) {
3008 * Block size hasn't been set; suggest maximal I/O transfers.
3010 vap->va_blksize = zfsvfs->z_max_blksz;
3018 * Set the file attributes to the values contained in the
3021 * IN: vp - vnode of file to be modified.
3022 * vap - new attribute values.
3023 * If AT_XVATTR set, then optional attrs are being set
3024 * flags - ATTR_UTIME set if non-default time values provided.
3025 * - ATTR_NOACLCHECK (CIFS context only).
3026 * cr - credentials of caller.
3027 * ct - caller context
3029 * RETURN: 0 on success, error code on failure.
3032 * vp - ctime updated, mtime updated if size changed.
3036 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
3037 caller_context_t *ct)
3039 znode_t *zp = VTOZ(vp);
3040 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3045 uint_t mask = vap->va_mask;
3046 uint_t saved_mask = 0;
3047 uint64_t saved_mode;
3050 uint64_t new_uid, new_gid;
3052 uint64_t mtime[2], ctime[2];
3054 int need_policy = FALSE;
3056 zfs_fuid_info_t *fuidp = NULL;
3057 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
3060 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
3061 boolean_t fuid_dirtied = B_FALSE;
3062 sa_bulk_attr_t bulk[7], xattr_bulk[7];
3063 int count = 0, xattr_count = 0;
3068 if (mask & AT_NOSET)
3069 return (SET_ERROR(EINVAL));
3074 zilog = zfsvfs->z_log;
3077 * Make sure that if we have ephemeral uid/gid or xvattr specified
3078 * that file system is at proper version level
3081 if (zfsvfs->z_use_fuids == B_FALSE &&
3082 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
3083 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
3084 (mask & AT_XVATTR))) {
3086 return (SET_ERROR(EINVAL));
3089 if (mask & AT_SIZE && vp->v_type == VDIR) {
3091 return (SET_ERROR(EISDIR));
3094 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
3096 return (SET_ERROR(EINVAL));
3100 * If this is an xvattr_t, then get a pointer to the structure of
3101 * optional attributes. If this is NULL, then we have a vattr_t.
3103 xoap = xva_getxoptattr(xvap);
3105 xva_init(&tmpxvattr);
3108 * Immutable files can only alter immutable bit and atime
3110 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
3111 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
3112 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
3114 return (SET_ERROR(EPERM));
3117 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
3119 return (SET_ERROR(EPERM));
3123 * Verify timestamps doesn't overflow 32 bits.
3124 * ZFS can handle large timestamps, but 32bit syscalls can't
3125 * handle times greater than 2039. This check should be removed
3126 * once large timestamps are fully supported.
3128 if (mask & (AT_ATIME | AT_MTIME)) {
3129 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
3130 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
3132 return (SET_ERROR(EOVERFLOW));
3140 /* Can this be moved to before the top label? */
3141 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
3143 return (SET_ERROR(EROFS));
3147 * First validate permissions
3150 if (mask & AT_SIZE) {
3152 * XXX - Note, we are not providing any open
3153 * mode flags here (like FNDELAY), so we may
3154 * block if there are locks present... this
3155 * should be addressed in openat().
3157 /* XXX - would it be OK to generate a log record here? */
3158 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3165 if (mask & (AT_ATIME|AT_MTIME) ||
3166 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3167 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3168 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3169 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3170 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3171 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3172 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3173 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3177 if (mask & (AT_UID|AT_GID)) {
3178 int idmask = (mask & (AT_UID|AT_GID));
3183 * NOTE: even if a new mode is being set,
3184 * we may clear S_ISUID/S_ISGID bits.
3187 if (!(mask & AT_MODE))
3188 vap->va_mode = zp->z_mode;
3191 * Take ownership or chgrp to group we are a member of
3194 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3195 take_group = (mask & AT_GID) &&
3196 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3199 * If both AT_UID and AT_GID are set then take_owner and
3200 * take_group must both be set in order to allow taking
3203 * Otherwise, send the check through secpolicy_vnode_setattr()
3207 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3208 ((idmask == AT_UID) && take_owner) ||
3209 ((idmask == AT_GID) && take_group)) {
3210 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3211 skipaclchk, cr) == 0) {
3213 * Remove setuid/setgid for non-privileged users
3215 secpolicy_setid_clear(vap, vp, cr);
3216 trim_mask = (mask & (AT_UID|AT_GID));
3225 mutex_enter(&zp->z_lock);
3226 oldva.va_mode = zp->z_mode;
3227 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3228 if (mask & AT_XVATTR) {
3230 * Update xvattr mask to include only those attributes
3231 * that are actually changing.
3233 * the bits will be restored prior to actually setting
3234 * the attributes so the caller thinks they were set.
3236 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3237 if (xoap->xoa_appendonly !=
3238 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3241 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3242 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3246 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3247 if (xoap->xoa_nounlink !=
3248 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3251 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3252 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3256 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3257 if (xoap->xoa_immutable !=
3258 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3261 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3262 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3266 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3267 if (xoap->xoa_nodump !=
3268 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3271 XVA_CLR_REQ(xvap, XAT_NODUMP);
3272 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3276 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3277 if (xoap->xoa_av_modified !=
3278 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3281 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3282 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3286 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3287 if ((vp->v_type != VREG &&
3288 xoap->xoa_av_quarantined) ||
3289 xoap->xoa_av_quarantined !=
3290 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3293 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3294 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3298 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3299 mutex_exit(&zp->z_lock);
3301 return (SET_ERROR(EPERM));
3304 if (need_policy == FALSE &&
3305 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3306 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3311 mutex_exit(&zp->z_lock);
3313 if (mask & AT_MODE) {
3314 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3315 err = secpolicy_setid_setsticky_clear(vp, vap,
3321 trim_mask |= AT_MODE;
3329 * If trim_mask is set then take ownership
3330 * has been granted or write_acl is present and user
3331 * has the ability to modify mode. In that case remove
3332 * UID|GID and or MODE from mask so that
3333 * secpolicy_vnode_setattr() doesn't revoke it.
3337 saved_mask = vap->va_mask;
3338 vap->va_mask &= ~trim_mask;
3339 if (trim_mask & AT_MODE) {
3341 * Save the mode, as secpolicy_vnode_setattr()
3342 * will overwrite it with ova.va_mode.
3344 saved_mode = vap->va_mode;
3347 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3348 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3355 vap->va_mask |= saved_mask;
3356 if (trim_mask & AT_MODE) {
3358 * Recover the mode after
3359 * secpolicy_vnode_setattr().
3361 vap->va_mode = saved_mode;
3367 * secpolicy_vnode_setattr, or take ownership may have
3370 mask = vap->va_mask;
3372 if ((mask & (AT_UID | AT_GID))) {
3373 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3374 &xattr_obj, sizeof (xattr_obj));
3376 if (err == 0 && xattr_obj) {
3377 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3381 if (mask & AT_UID) {
3382 new_uid = zfs_fuid_create(zfsvfs,
3383 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3384 if (new_uid != zp->z_uid &&
3385 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3387 VN_RELE(ZTOV(attrzp));
3388 err = SET_ERROR(EDQUOT);
3393 if (mask & AT_GID) {
3394 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3395 cr, ZFS_GROUP, &fuidp);
3396 if (new_gid != zp->z_gid &&
3397 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3399 VN_RELE(ZTOV(attrzp));
3400 err = SET_ERROR(EDQUOT);
3405 tx = dmu_tx_create(zfsvfs->z_os);
3407 if (mask & AT_MODE) {
3408 uint64_t pmode = zp->z_mode;
3410 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3412 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3413 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3414 err = SET_ERROR(EPERM);
3418 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3421 mutex_enter(&zp->z_lock);
3422 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3424 * Are we upgrading ACL from old V0 format
3427 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3428 zfs_znode_acl_version(zp) ==
3429 ZFS_ACL_VERSION_INITIAL) {
3430 dmu_tx_hold_free(tx, acl_obj, 0,
3432 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3433 0, aclp->z_acl_bytes);
3435 dmu_tx_hold_write(tx, acl_obj, 0,
3438 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3439 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3440 0, aclp->z_acl_bytes);
3442 mutex_exit(&zp->z_lock);
3443 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3445 if ((mask & AT_XVATTR) &&
3446 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3447 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3449 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3453 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3456 fuid_dirtied = zfsvfs->z_fuid_dirty;
3458 zfs_fuid_txhold(zfsvfs, tx);
3460 zfs_sa_upgrade_txholds(tx, zp);
3462 err = dmu_tx_assign(tx, TXG_WAIT);
3468 * Set each attribute requested.
3469 * We group settings according to the locks they need to acquire.
3471 * Note: you cannot set ctime directly, although it will be
3472 * updated as a side-effect of calling this function.
3476 if (mask & (AT_UID|AT_GID|AT_MODE))
3477 mutex_enter(&zp->z_acl_lock);
3478 mutex_enter(&zp->z_lock);
3480 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3481 &zp->z_pflags, sizeof (zp->z_pflags));
3484 if (mask & (AT_UID|AT_GID|AT_MODE))
3485 mutex_enter(&attrzp->z_acl_lock);
3486 mutex_enter(&attrzp->z_lock);
3487 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3488 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3489 sizeof (attrzp->z_pflags));
3492 if (mask & (AT_UID|AT_GID)) {
3494 if (mask & AT_UID) {
3495 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3496 &new_uid, sizeof (new_uid));
3497 zp->z_uid = new_uid;
3499 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3500 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3502 attrzp->z_uid = new_uid;
3506 if (mask & AT_GID) {
3507 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3508 NULL, &new_gid, sizeof (new_gid));
3509 zp->z_gid = new_gid;
3511 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3512 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3514 attrzp->z_gid = new_gid;
3517 if (!(mask & AT_MODE)) {
3518 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3519 NULL, &new_mode, sizeof (new_mode));
3520 new_mode = zp->z_mode;
3522 err = zfs_acl_chown_setattr(zp);
3525 err = zfs_acl_chown_setattr(attrzp);
3530 if (mask & AT_MODE) {
3531 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3532 &new_mode, sizeof (new_mode));
3533 zp->z_mode = new_mode;
3534 ASSERT3U((uintptr_t)aclp, !=, 0);
3535 err = zfs_aclset_common(zp, aclp, cr, tx);
3537 if (zp->z_acl_cached)
3538 zfs_acl_free(zp->z_acl_cached);
3539 zp->z_acl_cached = aclp;
3544 if (mask & AT_ATIME) {
3545 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3546 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3547 &zp->z_atime, sizeof (zp->z_atime));
3550 if (mask & AT_MTIME) {
3551 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3552 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3553 mtime, sizeof (mtime));
3556 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3557 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3558 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3559 NULL, mtime, sizeof (mtime));
3560 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3561 &ctime, sizeof (ctime));
3562 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3564 } else if (mask != 0) {
3565 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3566 &ctime, sizeof (ctime));
3567 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3570 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3571 SA_ZPL_CTIME(zfsvfs), NULL,
3572 &ctime, sizeof (ctime));
3573 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3574 mtime, ctime, B_TRUE);
3578 * Do this after setting timestamps to prevent timestamp
3579 * update from toggling bit
3582 if (xoap && (mask & AT_XVATTR)) {
3585 * restore trimmed off masks
3586 * so that return masks can be set for caller.
3589 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3590 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3592 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3593 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3595 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3596 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3598 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3599 XVA_SET_REQ(xvap, XAT_NODUMP);
3601 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3602 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3604 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3605 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3608 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3609 ASSERT(vp->v_type == VREG);
3611 zfs_xvattr_set(zp, xvap, tx);
3615 zfs_fuid_sync(zfsvfs, tx);
3618 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3620 mutex_exit(&zp->z_lock);
3621 if (mask & (AT_UID|AT_GID|AT_MODE))
3622 mutex_exit(&zp->z_acl_lock);
3625 if (mask & (AT_UID|AT_GID|AT_MODE))
3626 mutex_exit(&attrzp->z_acl_lock);
3627 mutex_exit(&attrzp->z_lock);
3630 if (err == 0 && attrzp) {
3631 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3637 VN_RELE(ZTOV(attrzp));
3643 zfs_fuid_info_free(fuidp);
3649 if (err == ERESTART)
3652 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3657 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3658 zil_commit(zilog, 0);
3664 typedef struct zfs_zlock {
3665 krwlock_t *zl_rwlock; /* lock we acquired */
3666 znode_t *zl_znode; /* znode we held */
3667 struct zfs_zlock *zl_next; /* next in list */
3671 * Drop locks and release vnodes that were held by zfs_rename_lock().
3674 zfs_rename_unlock(zfs_zlock_t **zlpp)
3678 while ((zl = *zlpp) != NULL) {
3679 if (zl->zl_znode != NULL)
3680 VN_RELE(ZTOV(zl->zl_znode));
3681 rw_exit(zl->zl_rwlock);
3682 *zlpp = zl->zl_next;
3683 kmem_free(zl, sizeof (*zl));
3688 * Search back through the directory tree, using the ".." entries.
3689 * Lock each directory in the chain to prevent concurrent renames.
3690 * Fail any attempt to move a directory into one of its own descendants.
3691 * XXX - z_parent_lock can overlap with map or grow locks
3694 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3698 uint64_t rootid = zp->z_zfsvfs->z_root;
3699 uint64_t oidp = zp->z_id;
3700 krwlock_t *rwlp = &szp->z_parent_lock;
3701 krw_t rw = RW_WRITER;
3704 * First pass write-locks szp and compares to zp->z_id.
3705 * Later passes read-lock zp and compare to zp->z_parent.
3708 if (!rw_tryenter(rwlp, rw)) {
3710 * Another thread is renaming in this path.
3711 * Note that if we are a WRITER, we don't have any
3712 * parent_locks held yet.
3714 if (rw == RW_READER && zp->z_id > szp->z_id) {
3716 * Drop our locks and restart
3718 zfs_rename_unlock(&zl);
3722 rwlp = &szp->z_parent_lock;
3727 * Wait for other thread to drop its locks
3733 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3734 zl->zl_rwlock = rwlp;
3735 zl->zl_znode = NULL;
3736 zl->zl_next = *zlpp;
3739 if (oidp == szp->z_id) /* We're a descendant of szp */
3740 return (SET_ERROR(EINVAL));
3742 if (oidp == rootid) /* We've hit the top */
3745 if (rw == RW_READER) { /* i.e. not the first pass */
3746 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3751 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3752 &oidp, sizeof (oidp));
3753 rwlp = &zp->z_parent_lock;
3756 } while (zp->z_id != sdzp->z_id);
3762 * Move an entry from the provided source directory to the target
3763 * directory. Change the entry name as indicated.
3765 * IN: sdvp - Source directory containing the "old entry".
3766 * snm - Old entry name.
3767 * tdvp - Target directory to contain the "new entry".
3768 * tnm - New entry name.
3769 * cr - credentials of caller.
3770 * ct - caller context
3771 * flags - case flags
3773 * RETURN: 0 on success, error code on failure.
3776 * sdvp,tdvp - ctime|mtime updated
3780 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3781 caller_context_t *ct, int flags)
3783 znode_t *tdzp, *sdzp, *szp, *tzp;
3787 zfs_dirlock_t *sdl, *tdl;
3790 int cmp, serr, terr;
3793 boolean_t waited = B_FALSE;
3796 ZFS_VERIFY_ZP(tdzp);
3797 zfsvfs = tdzp->z_zfsvfs;
3799 zilog = zfsvfs->z_log;
3803 * In case sdzp is not valid, let's be sure to exit from the right
3806 if (sdzp->z_sa_hdl == NULL) {
3808 return (SET_ERROR(EIO));
3812 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3813 * ctldir appear to have the same v_vfsp.
3815 if (sdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) {
3817 return (SET_ERROR(EXDEV));
3820 if (zfsvfs->z_utf8 && u8_validate(tnm,
3821 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3823 return (SET_ERROR(EILSEQ));
3826 if (flags & FIGNORECASE)
3835 * This is to prevent the creation of links into attribute space
3836 * by renaming a linked file into/outof an attribute directory.
3837 * See the comment in zfs_link() for why this is considered bad.
3839 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3841 return (SET_ERROR(EINVAL));
3845 * Lock source and target directory entries. To prevent deadlock,
3846 * a lock ordering must be defined. We lock the directory with
3847 * the smallest object id first, or if it's a tie, the one with
3848 * the lexically first name.
3850 if (sdzp->z_id < tdzp->z_id) {
3852 } else if (sdzp->z_id > tdzp->z_id) {
3856 * First compare the two name arguments without
3857 * considering any case folding.
3859 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3861 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3862 ASSERT(error == 0 || !zfsvfs->z_utf8);
3865 * POSIX: "If the old argument and the new argument
3866 * both refer to links to the same existing file,
3867 * the rename() function shall return successfully
3868 * and perform no other action."
3874 * If the file system is case-folding, then we may
3875 * have some more checking to do. A case-folding file
3876 * system is either supporting mixed case sensitivity
3877 * access or is completely case-insensitive. Note
3878 * that the file system is always case preserving.
3880 * In mixed sensitivity mode case sensitive behavior
3881 * is the default. FIGNORECASE must be used to
3882 * explicitly request case insensitive behavior.
3884 * If the source and target names provided differ only
3885 * by case (e.g., a request to rename 'tim' to 'Tim'),
3886 * we will treat this as a special case in the
3887 * case-insensitive mode: as long as the source name
3888 * is an exact match, we will allow this to proceed as
3889 * a name-change request.
3891 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3892 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3893 flags & FIGNORECASE)) &&
3894 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3897 * case preserving rename request, require exact
3906 * If the source and destination directories are the same, we should
3907 * grab the z_name_lock of that directory only once.
3911 rw_enter(&sdzp->z_name_lock, RW_READER);
3915 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3916 ZEXISTS | zflg, NULL, NULL);
3917 terr = zfs_dirent_lock(&tdl,
3918 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3920 terr = zfs_dirent_lock(&tdl,
3921 tdzp, tnm, &tzp, zflg, NULL, NULL);
3922 serr = zfs_dirent_lock(&sdl,
3923 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3929 * Source entry invalid or not there.
3932 zfs_dirent_unlock(tdl);
3938 rw_exit(&sdzp->z_name_lock);
3941 * FreeBSD: In OpenSolaris they only check if rename source is
3942 * ".." here, because "." is handled in their lookup. This is
3943 * not the case for FreeBSD, so we check for "." explicitly.
3945 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0)
3946 serr = SET_ERROR(EINVAL);
3951 zfs_dirent_unlock(sdl);
3955 rw_exit(&sdzp->z_name_lock);
3957 if (strcmp(tnm, "..") == 0)
3958 terr = SET_ERROR(EINVAL);
3964 * Must have write access at the source to remove the old entry
3965 * and write access at the target to create the new entry.
3966 * Note that if target and source are the same, this can be
3967 * done in a single check.
3970 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3973 if (ZTOV(szp)->v_type == VDIR) {
3975 * Check to make sure rename is valid.
3976 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3978 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3983 * Does target exist?
3987 * Source and target must be the same type.
3989 if (ZTOV(szp)->v_type == VDIR) {
3990 if (ZTOV(tzp)->v_type != VDIR) {
3991 error = SET_ERROR(ENOTDIR);
3995 if (ZTOV(tzp)->v_type == VDIR) {
3996 error = SET_ERROR(EISDIR);
4001 * POSIX dictates that when the source and target
4002 * entries refer to the same file object, rename
4003 * must do nothing and exit without error.
4005 if (szp->z_id == tzp->z_id) {
4011 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
4013 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
4016 * notify the target directory if it is not the same
4017 * as source directory.
4020 vnevent_rename_dest_dir(tdvp, ct);
4023 tx = dmu_tx_create(zfsvfs->z_os);
4024 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4025 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
4026 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
4027 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
4029 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
4030 zfs_sa_upgrade_txholds(tx, tdzp);
4033 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
4034 zfs_sa_upgrade_txholds(tx, tzp);
4037 zfs_sa_upgrade_txholds(tx, szp);
4038 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
4039 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4042 zfs_rename_unlock(&zl);
4043 zfs_dirent_unlock(sdl);
4044 zfs_dirent_unlock(tdl);
4047 rw_exit(&sdzp->z_name_lock);
4052 if (error == ERESTART) {
4063 if (tzp) /* Attempt to remove the existing target */
4064 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
4067 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
4069 szp->z_pflags |= ZFS_AV_MODIFIED;
4071 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
4072 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
4075 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
4077 zfs_log_rename(zilog, tx, TX_RENAME |
4078 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
4079 sdl->dl_name, tdzp, tdl->dl_name, szp);
4082 * Update path information for the target vnode
4084 vn_renamepath(tdvp, ZTOV(szp), tnm,
4088 * At this point, we have successfully created
4089 * the target name, but have failed to remove
4090 * the source name. Since the create was done
4091 * with the ZRENAMING flag, there are
4092 * complications; for one, the link count is
4093 * wrong. The easiest way to deal with this
4094 * is to remove the newly created target, and
4095 * return the original error. This must
4096 * succeed; fortunately, it is very unlikely to
4097 * fail, since we just created it.
4099 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
4100 ZRENAMING, NULL), ==, 0);
4103 #ifdef FREEBSD_NAMECACHE
4107 cache_purge(ZTOV(szp));
4109 cache_purge(ZTOV(tzp));
4117 zfs_rename_unlock(&zl);
4119 zfs_dirent_unlock(sdl);
4120 zfs_dirent_unlock(tdl);
4123 rw_exit(&sdzp->z_name_lock);
4130 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4131 zil_commit(zilog, 0);
4139 * Insert the indicated symbolic reference entry into the directory.
4141 * IN: dvp - Directory to contain new symbolic link.
4142 * link - Name for new symlink entry.
4143 * vap - Attributes of new entry.
4144 * cr - credentials of caller.
4145 * ct - caller context
4146 * flags - case flags
4148 * RETURN: 0 on success, error code on failure.
4151 * dvp - ctime|mtime updated
4155 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
4156 cred_t *cr, kthread_t *td)
4158 znode_t *zp, *dzp = VTOZ(dvp);
4161 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4163 uint64_t len = strlen(link);
4166 zfs_acl_ids_t acl_ids;
4167 boolean_t fuid_dirtied;
4168 uint64_t txtype = TX_SYMLINK;
4169 boolean_t waited = B_FALSE;
4172 ASSERT(vap->va_type == VLNK);
4176 zilog = zfsvfs->z_log;
4178 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4179 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4181 return (SET_ERROR(EILSEQ));
4183 if (flags & FIGNORECASE)
4186 if (len > MAXPATHLEN) {
4188 return (SET_ERROR(ENAMETOOLONG));
4191 if ((error = zfs_acl_ids_create(dzp, 0,
4192 vap, cr, NULL, &acl_ids)) != 0) {
4197 getnewvnode_reserve(1);
4201 * Attempt to lock directory; fail if entry already exists.
4203 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
4205 zfs_acl_ids_free(&acl_ids);
4206 getnewvnode_drop_reserve();
4211 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4212 zfs_acl_ids_free(&acl_ids);
4213 zfs_dirent_unlock(dl);
4214 getnewvnode_drop_reserve();
4219 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4220 zfs_acl_ids_free(&acl_ids);
4221 zfs_dirent_unlock(dl);
4222 getnewvnode_drop_reserve();
4224 return (SET_ERROR(EDQUOT));
4226 tx = dmu_tx_create(zfsvfs->z_os);
4227 fuid_dirtied = zfsvfs->z_fuid_dirty;
4228 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4229 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4230 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4231 ZFS_SA_BASE_ATTR_SIZE + len);
4232 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4233 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4234 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4235 acl_ids.z_aclp->z_acl_bytes);
4238 zfs_fuid_txhold(zfsvfs, tx);
4239 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4241 zfs_dirent_unlock(dl);
4242 if (error == ERESTART) {
4248 zfs_acl_ids_free(&acl_ids);
4250 getnewvnode_drop_reserve();
4256 * Create a new object for the symlink.
4257 * for version 4 ZPL datsets the symlink will be an SA attribute
4259 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4262 zfs_fuid_sync(zfsvfs, tx);
4264 mutex_enter(&zp->z_lock);
4266 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4269 zfs_sa_symlink(zp, link, len, tx);
4270 mutex_exit(&zp->z_lock);
4273 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4274 &zp->z_size, sizeof (zp->z_size), tx);
4276 * Insert the new object into the directory.
4278 (void) zfs_link_create(dl, zp, tx, ZNEW);
4280 if (flags & FIGNORECASE)
4282 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4285 zfs_acl_ids_free(&acl_ids);
4289 getnewvnode_drop_reserve();
4291 zfs_dirent_unlock(dl);
4293 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4294 zil_commit(zilog, 0);
4301 * Return, in the buffer contained in the provided uio structure,
4302 * the symbolic path referred to by vp.
4304 * IN: vp - vnode of symbolic link.
4305 * uio - structure to contain the link path.
4306 * cr - credentials of caller.
4307 * ct - caller context
4309 * OUT: uio - structure containing the link path.
4311 * RETURN: 0 on success, error code on failure.
4314 * vp - atime updated
4318 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4320 znode_t *zp = VTOZ(vp);
4321 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4327 mutex_enter(&zp->z_lock);
4329 error = sa_lookup_uio(zp->z_sa_hdl,
4330 SA_ZPL_SYMLINK(zfsvfs), uio);
4332 error = zfs_sa_readlink(zp, uio);
4333 mutex_exit(&zp->z_lock);
4335 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4342 * Insert a new entry into directory tdvp referencing svp.
4344 * IN: tdvp - Directory to contain new entry.
4345 * svp - vnode of new entry.
4346 * name - name of new entry.
4347 * cr - credentials of caller.
4348 * ct - caller context
4350 * RETURN: 0 on success, error code on failure.
4353 * tdvp - ctime|mtime updated
4354 * svp - ctime updated
4358 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4359 caller_context_t *ct, int flags)
4361 znode_t *dzp = VTOZ(tdvp);
4363 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4372 boolean_t waited = B_FALSE;
4374 ASSERT(tdvp->v_type == VDIR);
4378 zilog = zfsvfs->z_log;
4380 if (VOP_REALVP(svp, &realvp, ct) == 0)
4384 * POSIX dictates that we return EPERM here.
4385 * Better choices include ENOTSUP or EISDIR.
4387 if (svp->v_type == VDIR) {
4389 return (SET_ERROR(EPERM));
4396 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
4397 * ctldir appear to have the same v_vfsp.
4399 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) {
4401 return (SET_ERROR(EXDEV));
4404 /* Prevent links to .zfs/shares files */
4406 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4407 &parent, sizeof (uint64_t))) != 0) {
4411 if (parent == zfsvfs->z_shares_dir) {
4413 return (SET_ERROR(EPERM));
4416 if (zfsvfs->z_utf8 && u8_validate(name,
4417 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4419 return (SET_ERROR(EILSEQ));
4421 if (flags & FIGNORECASE)
4425 * We do not support links between attributes and non-attributes
4426 * because of the potential security risk of creating links
4427 * into "normal" file space in order to circumvent restrictions
4428 * imposed in attribute space.
4430 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4432 return (SET_ERROR(EINVAL));
4436 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4437 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4439 return (SET_ERROR(EPERM));
4442 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4449 * Attempt to lock directory; fail if entry already exists.
4451 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4457 tx = dmu_tx_create(zfsvfs->z_os);
4458 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4459 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4460 zfs_sa_upgrade_txholds(tx, szp);
4461 zfs_sa_upgrade_txholds(tx, dzp);
4462 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4464 zfs_dirent_unlock(dl);
4465 if (error == ERESTART) {
4476 error = zfs_link_create(dl, szp, tx, 0);
4479 uint64_t txtype = TX_LINK;
4480 if (flags & FIGNORECASE)
4482 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4487 zfs_dirent_unlock(dl);
4490 vnevent_link(svp, ct);
4493 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4494 zil_commit(zilog, 0);
4502 * zfs_null_putapage() is used when the file system has been force
4503 * unmounted. It just drops the pages.
4507 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4508 size_t *lenp, int flags, cred_t *cr)
4510 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4515 * Push a page out to disk, klustering if possible.
4517 * IN: vp - file to push page to.
4518 * pp - page to push.
4519 * flags - additional flags.
4520 * cr - credentials of caller.
4522 * OUT: offp - start of range pushed.
4523 * lenp - len of range pushed.
4525 * RETURN: 0 on success, error code on failure.
4527 * NOTE: callers must have locked the page to be pushed. On
4528 * exit, the page (and all other pages in the kluster) must be
4533 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4534 size_t *lenp, int flags, cred_t *cr)
4536 znode_t *zp = VTOZ(vp);
4537 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4539 u_offset_t off, koff;
4546 * If our blocksize is bigger than the page size, try to kluster
4547 * multiple pages so that we write a full block (thus avoiding
4548 * a read-modify-write).
4550 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4551 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4552 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4553 ASSERT(koff <= zp->z_size);
4554 if (koff + klen > zp->z_size)
4555 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4556 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4558 ASSERT3U(btop(len), ==, btopr(len));
4561 * Can't push pages past end-of-file.
4563 if (off >= zp->z_size) {
4564 /* ignore all pages */
4567 } else if (off + len > zp->z_size) {
4568 int npages = btopr(zp->z_size - off);
4571 page_list_break(&pp, &trunc, npages);
4572 /* ignore pages past end of file */
4574 pvn_write_done(trunc, flags);
4575 len = zp->z_size - off;
4578 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4579 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4580 err = SET_ERROR(EDQUOT);
4583 tx = dmu_tx_create(zfsvfs->z_os);
4584 dmu_tx_hold_write(tx, zp->z_id, off, len);
4586 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4587 zfs_sa_upgrade_txholds(tx, zp);
4588 err = dmu_tx_assign(tx, TXG_WAIT);
4594 if (zp->z_blksz <= PAGESIZE) {
4595 caddr_t va = zfs_map_page(pp, S_READ);
4596 ASSERT3U(len, <=, PAGESIZE);
4597 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4598 zfs_unmap_page(pp, va);
4600 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4604 uint64_t mtime[2], ctime[2];
4605 sa_bulk_attr_t bulk[3];
4608 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4610 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4612 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4614 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4616 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4621 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4631 * Copy the portion of the file indicated from pages into the file.
4632 * The pages are stored in a page list attached to the files vnode.
4634 * IN: vp - vnode of file to push page data to.
4635 * off - position in file to put data.
4636 * len - amount of data to write.
4637 * flags - flags to control the operation.
4638 * cr - credentials of caller.
4639 * ct - caller context.
4641 * RETURN: 0 on success, error code on failure.
4644 * vp - ctime|mtime updated
4648 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4649 caller_context_t *ct)
4651 znode_t *zp = VTOZ(vp);
4652 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4664 * Align this request to the file block size in case we kluster.
4665 * XXX - this can result in pretty aggresive locking, which can
4666 * impact simultanious read/write access. One option might be
4667 * to break up long requests (len == 0) into block-by-block
4668 * operations to get narrower locking.
4670 blksz = zp->z_blksz;
4672 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4675 if (len > 0 && ISP2(blksz))
4676 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4682 * Search the entire vp list for pages >= io_off.
4684 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4685 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4688 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4690 if (off > zp->z_size) {
4691 /* past end of file */
4692 zfs_range_unlock(rl);
4697 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4699 for (off = io_off; io_off < off + len; io_off += io_len) {
4700 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4701 pp = page_lookup(vp, io_off,
4702 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4704 pp = page_lookup_nowait(vp, io_off,
4705 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4708 if (pp != NULL && pvn_getdirty(pp, flags)) {
4712 * Found a dirty page to push
4714 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4722 zfs_range_unlock(rl);
4723 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4724 zil_commit(zfsvfs->z_log, zp->z_id);
4728 #endif /* illumos */
4732 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4734 znode_t *zp = VTOZ(vp);
4735 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4738 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4739 if (zp->z_sa_hdl == NULL) {
4741 * The fs has been unmounted, or we did a
4742 * suspend/resume and this file no longer exists.
4744 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4749 mutex_enter(&zp->z_lock);
4750 if (zp->z_unlinked) {
4752 * Fast path to recycle a vnode of a removed file.
4754 mutex_exit(&zp->z_lock);
4755 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4759 mutex_exit(&zp->z_lock);
4761 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4762 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4764 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4765 zfs_sa_upgrade_txholds(tx, zp);
4766 error = dmu_tx_assign(tx, TXG_WAIT);
4770 mutex_enter(&zp->z_lock);
4771 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4772 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4773 zp->z_atime_dirty = 0;
4774 mutex_exit(&zp->z_lock);
4778 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4783 * Bounds-check the seek operation.
4785 * IN: vp - vnode seeking within
4786 * ooff - old file offset
4787 * noffp - pointer to new file offset
4788 * ct - caller context
4790 * RETURN: 0 on success, EINVAL if new offset invalid.
4794 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4795 caller_context_t *ct)
4797 if (vp->v_type == VDIR)
4799 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4803 * Pre-filter the generic locking function to trap attempts to place
4804 * a mandatory lock on a memory mapped file.
4807 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4808 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4810 znode_t *zp = VTOZ(vp);
4811 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4817 * We are following the UFS semantics with respect to mapcnt
4818 * here: If we see that the file is mapped already, then we will
4819 * return an error, but we don't worry about races between this
4820 * function and zfs_map().
4822 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4824 return (SET_ERROR(EAGAIN));
4827 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4831 * If we can't find a page in the cache, we will create a new page
4832 * and fill it with file data. For efficiency, we may try to fill
4833 * multiple pages at once (klustering) to fill up the supplied page
4834 * list. Note that the pages to be filled are held with an exclusive
4835 * lock to prevent access by other threads while they are being filled.
4838 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4839 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4841 znode_t *zp = VTOZ(vp);
4842 page_t *pp, *cur_pp;
4843 objset_t *os = zp->z_zfsvfs->z_os;
4844 u_offset_t io_off, total;
4848 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4850 * We only have a single page, don't bother klustering
4854 pp = page_create_va(vp, io_off, io_len,
4855 PG_EXCL | PG_WAIT, seg, addr);
4858 * Try to find enough pages to fill the page list
4860 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4861 &io_len, off, plsz, 0);
4865 * The page already exists, nothing to do here.
4872 * Fill the pages in the kluster.
4875 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4878 ASSERT3U(io_off, ==, cur_pp->p_offset);
4879 va = zfs_map_page(cur_pp, S_WRITE);
4880 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4882 zfs_unmap_page(cur_pp, va);
4884 /* On error, toss the entire kluster */
4885 pvn_read_done(pp, B_ERROR);
4886 /* convert checksum errors into IO errors */
4888 err = SET_ERROR(EIO);
4891 cur_pp = cur_pp->p_next;
4895 * Fill in the page list array from the kluster starting
4896 * from the desired offset `off'.
4897 * NOTE: the page list will always be null terminated.
4899 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4900 ASSERT(pl == NULL || (*pl)->p_offset == off);
4906 * Return pointers to the pages for the file region [off, off + len]
4907 * in the pl array. If plsz is greater than len, this function may
4908 * also return page pointers from after the specified region
4909 * (i.e. the region [off, off + plsz]). These additional pages are
4910 * only returned if they are already in the cache, or were created as
4911 * part of a klustered read.
4913 * IN: vp - vnode of file to get data from.
4914 * off - position in file to get data from.
4915 * len - amount of data to retrieve.
4916 * plsz - length of provided page list.
4917 * seg - segment to obtain pages for.
4918 * addr - virtual address of fault.
4919 * rw - mode of created pages.
4920 * cr - credentials of caller.
4921 * ct - caller context.
4923 * OUT: protp - protection mode of created pages.
4924 * pl - list of pages created.
4926 * RETURN: 0 on success, error code on failure.
4929 * vp - atime updated
4933 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4934 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4935 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4937 znode_t *zp = VTOZ(vp);
4938 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4942 /* we do our own caching, faultahead is unnecessary */
4945 else if (len > plsz)
4948 len = P2ROUNDUP(len, PAGESIZE);
4949 ASSERT(plsz >= len);
4958 * Loop through the requested range [off, off + len) looking
4959 * for pages. If we don't find a page, we will need to create
4960 * a new page and fill it with data from the file.
4963 if (*pl = page_lookup(vp, off, SE_SHARED))
4965 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4968 ASSERT3U((*pl)->p_offset, ==, off);
4972 ASSERT3U(len, >=, PAGESIZE);
4975 ASSERT3U(plsz, >=, PAGESIZE);
4982 * Fill out the page array with any pages already in the cache.
4985 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4992 * Release any pages we have previously locked.
4997 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
5007 * Request a memory map for a section of a file. This code interacts
5008 * with common code and the VM system as follows:
5010 * - common code calls mmap(), which ends up in smmap_common()
5011 * - this calls VOP_MAP(), which takes you into (say) zfs
5012 * - zfs_map() calls as_map(), passing segvn_create() as the callback
5013 * - segvn_create() creates the new segment and calls VOP_ADDMAP()
5014 * - zfs_addmap() updates z_mapcnt
5018 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
5019 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
5020 caller_context_t *ct)
5022 znode_t *zp = VTOZ(vp);
5023 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5024 segvn_crargs_t vn_a;
5030 if ((prot & PROT_WRITE) && (zp->z_pflags &
5031 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
5033 return (SET_ERROR(EPERM));
5036 if ((prot & (PROT_READ | PROT_EXEC)) &&
5037 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
5039 return (SET_ERROR(EACCES));
5042 if (vp->v_flag & VNOMAP) {
5044 return (SET_ERROR(ENOSYS));
5047 if (off < 0 || len > MAXOFFSET_T - off) {
5049 return (SET_ERROR(ENXIO));
5052 if (vp->v_type != VREG) {
5054 return (SET_ERROR(ENODEV));
5058 * If file is locked, disallow mapping.
5060 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
5062 return (SET_ERROR(EAGAIN));
5066 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
5074 vn_a.offset = (u_offset_t)off;
5075 vn_a.type = flags & MAP_TYPE;
5077 vn_a.maxprot = maxprot;
5080 vn_a.flags = flags & ~MAP_TYPE;
5082 vn_a.lgrp_mem_policy_flags = 0;
5084 error = as_map(as, *addrp, len, segvn_create, &vn_a);
5093 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5094 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
5095 caller_context_t *ct)
5097 uint64_t pages = btopr(len);
5099 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
5104 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
5105 * more accurate mtime for the associated file. Since we don't have a way of
5106 * detecting when the data was actually modified, we have to resort to
5107 * heuristics. If an explicit msync() is done, then we mark the mtime when the
5108 * last page is pushed. The problem occurs when the msync() call is omitted,
5109 * which by far the most common case:
5117 * putpage() via fsflush
5119 * If we wait until fsflush to come along, we can have a modification time that
5120 * is some arbitrary point in the future. In order to prevent this in the
5121 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
5126 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5127 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
5128 caller_context_t *ct)
5130 uint64_t pages = btopr(len);
5132 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
5133 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
5135 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
5136 vn_has_cached_data(vp))
5137 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
5143 * Free or allocate space in a file. Currently, this function only
5144 * supports the `F_FREESP' command. However, this command is somewhat
5145 * misnamed, as its functionality includes the ability to allocate as
5146 * well as free space.
5148 * IN: vp - vnode of file to free data in.
5149 * cmd - action to take (only F_FREESP supported).
5150 * bfp - section of file to free/alloc.
5151 * flag - current file open mode flags.
5152 * offset - current file offset.
5153 * cr - credentials of caller [UNUSED].
5154 * ct - caller context.
5156 * RETURN: 0 on success, error code on failure.
5159 * vp - ctime|mtime updated
5163 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
5164 offset_t offset, cred_t *cr, caller_context_t *ct)
5166 znode_t *zp = VTOZ(vp);
5167 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5174 if (cmd != F_FREESP) {
5176 return (SET_ERROR(EINVAL));
5179 if (error = convoff(vp, bfp, 0, offset)) {
5184 if (bfp->l_len < 0) {
5186 return (SET_ERROR(EINVAL));
5190 len = bfp->l_len; /* 0 means from off to end of file */
5192 error = zfs_freesp(zp, off, len, flag, TRUE);
5197 #endif /* illumos */
5199 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
5200 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
5204 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
5206 znode_t *zp = VTOZ(vp);
5207 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5210 uint64_t object = zp->z_id;
5217 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
5218 &gen64, sizeof (uint64_t))) != 0) {
5223 gen = (uint32_t)gen64;
5225 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
5228 if (fidp->fid_len < size) {
5229 fidp->fid_len = size;
5231 return (SET_ERROR(ENOSPC));
5234 fidp->fid_len = size;
5237 zfid = (zfid_short_t *)fidp;
5239 zfid->zf_len = size;
5241 for (i = 0; i < sizeof (zfid->zf_object); i++)
5242 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
5244 /* Must have a non-zero generation number to distinguish from .zfs */
5247 for (i = 0; i < sizeof (zfid->zf_gen); i++)
5248 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
5250 if (size == LONG_FID_LEN) {
5251 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
5254 zlfid = (zfid_long_t *)fidp;
5256 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
5257 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
5259 /* XXX - this should be the generation number for the objset */
5260 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
5261 zlfid->zf_setgen[i] = 0;
5269 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5270 caller_context_t *ct)
5282 case _PC_FILESIZEBITS:
5286 case _PC_XATTR_EXISTS:
5288 zfsvfs = zp->z_zfsvfs;
5292 error = zfs_dirent_lock(&dl, zp, "", &xzp,
5293 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
5295 zfs_dirent_unlock(dl);
5296 if (!zfs_dirempty(xzp))
5299 } else if (error == ENOENT) {
5301 * If there aren't extended attributes, it's the
5302 * same as having zero of them.
5309 case _PC_SATTR_ENABLED:
5310 case _PC_SATTR_EXISTS:
5311 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5312 (vp->v_type == VREG || vp->v_type == VDIR);
5315 case _PC_ACCESS_FILTERING:
5316 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
5320 case _PC_ACL_ENABLED:
5321 *valp = _ACL_ACE_ENABLED;
5323 #endif /* illumos */
5324 case _PC_MIN_HOLE_SIZE:
5325 *valp = (int)SPA_MINBLOCKSIZE;
5328 case _PC_TIMESTAMP_RESOLUTION:
5329 /* nanosecond timestamp resolution */
5333 case _PC_ACL_EXTENDED:
5341 case _PC_ACL_PATH_MAX:
5342 *valp = ACL_MAX_ENTRIES;
5346 return (EOPNOTSUPP);
5352 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5353 caller_context_t *ct)
5355 znode_t *zp = VTOZ(vp);
5356 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5358 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5362 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5370 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5371 caller_context_t *ct)
5373 znode_t *zp = VTOZ(vp);
5374 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5376 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5377 zilog_t *zilog = zfsvfs->z_log;
5382 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5384 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5385 zil_commit(zilog, 0);
5393 * The smallest read we may consider to loan out an arcbuf.
5394 * This must be a power of 2.
5396 int zcr_blksz_min = (1 << 10); /* 1K */
5398 * If set to less than the file block size, allow loaning out of an
5399 * arcbuf for a partial block read. This must be a power of 2.
5401 int zcr_blksz_max = (1 << 17); /* 128K */
5405 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5406 caller_context_t *ct)
5408 znode_t *zp = VTOZ(vp);
5409 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5410 int max_blksz = zfsvfs->z_max_blksz;
5411 uio_t *uio = &xuio->xu_uio;
5412 ssize_t size = uio->uio_resid;
5413 offset_t offset = uio->uio_loffset;
5418 int preamble, postamble;
5420 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5421 return (SET_ERROR(EINVAL));
5428 * Loan out an arc_buf for write if write size is bigger than
5429 * max_blksz, and the file's block size is also max_blksz.
5432 if (size < blksz || zp->z_blksz != blksz) {
5434 return (SET_ERROR(EINVAL));
5437 * Caller requests buffers for write before knowing where the
5438 * write offset might be (e.g. NFS TCP write).
5443 preamble = P2PHASE(offset, blksz);
5445 preamble = blksz - preamble;
5450 postamble = P2PHASE(size, blksz);
5453 fullblk = size / blksz;
5454 (void) dmu_xuio_init(xuio,
5455 (preamble != 0) + fullblk + (postamble != 0));
5456 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5457 int, postamble, int,
5458 (preamble != 0) + fullblk + (postamble != 0));
5461 * Have to fix iov base/len for partial buffers. They
5462 * currently represent full arc_buf's.
5465 /* data begins in the middle of the arc_buf */
5466 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5469 (void) dmu_xuio_add(xuio, abuf,
5470 blksz - preamble, preamble);
5473 for (i = 0; i < fullblk; i++) {
5474 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5477 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5481 /* data ends in the middle of the arc_buf */
5482 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5485 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5490 * Loan out an arc_buf for read if the read size is larger than
5491 * the current file block size. Block alignment is not
5492 * considered. Partial arc_buf will be loaned out for read.
5494 blksz = zp->z_blksz;
5495 if (blksz < zcr_blksz_min)
5496 blksz = zcr_blksz_min;
5497 if (blksz > zcr_blksz_max)
5498 blksz = zcr_blksz_max;
5499 /* avoid potential complexity of dealing with it */
5500 if (blksz > max_blksz) {
5502 return (SET_ERROR(EINVAL));
5505 maxsize = zp->z_size - uio->uio_loffset;
5509 if (size < blksz || vn_has_cached_data(vp)) {
5511 return (SET_ERROR(EINVAL));
5516 return (SET_ERROR(EINVAL));
5519 uio->uio_extflg = UIO_XUIO;
5520 XUIO_XUZC_RW(xuio) = ioflag;
5527 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5531 int ioflag = XUIO_XUZC_RW(xuio);
5533 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5535 i = dmu_xuio_cnt(xuio);
5537 abuf = dmu_xuio_arcbuf(xuio, i);
5539 * if abuf == NULL, it must be a write buffer
5540 * that has been returned in zfs_write().
5543 dmu_return_arcbuf(abuf);
5544 ASSERT(abuf || ioflag == UIO_WRITE);
5547 dmu_xuio_fini(xuio);
5552 * Predeclare these here so that the compiler assumes that
5553 * this is an "old style" function declaration that does
5554 * not include arguments => we won't get type mismatch errors
5555 * in the initializations that follow.
5557 static int zfs_inval();
5558 static int zfs_isdir();
5563 return (SET_ERROR(EINVAL));
5569 return (SET_ERROR(EISDIR));
5572 * Directory vnode operations template
5574 vnodeops_t *zfs_dvnodeops;
5575 const fs_operation_def_t zfs_dvnodeops_template[] = {
5576 VOPNAME_OPEN, { .vop_open = zfs_open },
5577 VOPNAME_CLOSE, { .vop_close = zfs_close },
5578 VOPNAME_READ, { .error = zfs_isdir },
5579 VOPNAME_WRITE, { .error = zfs_isdir },
5580 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5581 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5582 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5583 VOPNAME_ACCESS, { .vop_access = zfs_access },
5584 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5585 VOPNAME_CREATE, { .vop_create = zfs_create },
5586 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5587 VOPNAME_LINK, { .vop_link = zfs_link },
5588 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5589 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5590 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5591 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5592 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5593 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5594 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5595 VOPNAME_FID, { .vop_fid = zfs_fid },
5596 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5597 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5598 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5599 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5600 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5605 * Regular file vnode operations template
5607 vnodeops_t *zfs_fvnodeops;
5608 const fs_operation_def_t zfs_fvnodeops_template[] = {
5609 VOPNAME_OPEN, { .vop_open = zfs_open },
5610 VOPNAME_CLOSE, { .vop_close = zfs_close },
5611 VOPNAME_READ, { .vop_read = zfs_read },
5612 VOPNAME_WRITE, { .vop_write = zfs_write },
5613 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5614 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5615 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5616 VOPNAME_ACCESS, { .vop_access = zfs_access },
5617 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5618 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5619 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5620 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5621 VOPNAME_FID, { .vop_fid = zfs_fid },
5622 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5623 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5624 VOPNAME_SPACE, { .vop_space = zfs_space },
5625 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5626 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5627 VOPNAME_MAP, { .vop_map = zfs_map },
5628 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5629 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5630 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5631 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5632 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5633 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5634 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5635 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5640 * Symbolic link vnode operations template
5642 vnodeops_t *zfs_symvnodeops;
5643 const fs_operation_def_t zfs_symvnodeops_template[] = {
5644 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5645 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5646 VOPNAME_ACCESS, { .vop_access = zfs_access },
5647 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5648 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5649 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5650 VOPNAME_FID, { .vop_fid = zfs_fid },
5651 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5652 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5657 * special share hidden files vnode operations template
5659 vnodeops_t *zfs_sharevnodeops;
5660 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5661 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5662 VOPNAME_ACCESS, { .vop_access = zfs_access },
5663 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5664 VOPNAME_FID, { .vop_fid = zfs_fid },
5665 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5666 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5667 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5668 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5673 * Extended attribute directory vnode operations template
5675 * This template is identical to the directory vnodes
5676 * operation template except for restricted operations:
5680 * Note that there are other restrictions embedded in:
5681 * zfs_create() - restrict type to VREG
5682 * zfs_link() - no links into/out of attribute space
5683 * zfs_rename() - no moves into/out of attribute space
5685 vnodeops_t *zfs_xdvnodeops;
5686 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5687 VOPNAME_OPEN, { .vop_open = zfs_open },
5688 VOPNAME_CLOSE, { .vop_close = zfs_close },
5689 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5690 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5691 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5692 VOPNAME_ACCESS, { .vop_access = zfs_access },
5693 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5694 VOPNAME_CREATE, { .vop_create = zfs_create },
5695 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5696 VOPNAME_LINK, { .vop_link = zfs_link },
5697 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5698 VOPNAME_MKDIR, { .error = zfs_inval },
5699 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5700 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5701 VOPNAME_SYMLINK, { .error = zfs_inval },
5702 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5703 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5704 VOPNAME_FID, { .vop_fid = zfs_fid },
5705 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5706 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5707 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5708 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5709 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5714 * Error vnode operations template
5716 vnodeops_t *zfs_evnodeops;
5717 const fs_operation_def_t zfs_evnodeops_template[] = {
5718 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5719 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5722 #endif /* illumos */
5725 ioflags(int ioflags)
5729 if (ioflags & IO_APPEND)
5731 if (ioflags & IO_NDELAY)
5733 if (ioflags & IO_SYNC)
5734 flags |= (FSYNC | FDSYNC | FRSYNC);
5740 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int reqpage)
5742 znode_t *zp = VTOZ(vp);
5743 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5744 objset_t *os = zp->z_zfsvfs->z_os;
5745 vm_page_t mfirst, mlast, mreq;
5749 off_t startoff, endoff;
5751 vm_pindex_t reqstart, reqend;
5752 int pcount, lsize, reqsize, size;
5757 pcount = OFF_TO_IDX(round_page(count));
5759 object = mreq->object;
5762 KASSERT(vp->v_object == object, ("mismatching object"));
5764 if (pcount > 1 && zp->z_blksz > PAGESIZE) {
5765 startoff = rounddown(IDX_TO_OFF(mreq->pindex), zp->z_blksz);
5766 reqstart = OFF_TO_IDX(round_page(startoff));
5767 if (reqstart < m[0]->pindex)
5770 reqstart = reqstart - m[0]->pindex;
5771 endoff = roundup(IDX_TO_OFF(mreq->pindex) + PAGE_SIZE,
5773 reqend = OFF_TO_IDX(trunc_page(endoff)) - 1;
5774 if (reqend > m[pcount - 1]->pindex)
5775 reqend = m[pcount - 1]->pindex;
5776 reqsize = reqend - m[reqstart]->pindex + 1;
5777 KASSERT(reqstart <= reqpage && reqpage < reqstart + reqsize,
5778 ("reqpage beyond [reqstart, reqstart + reqsize[ bounds"));
5783 mfirst = m[reqstart];
5784 mlast = m[reqstart + reqsize - 1];
5786 zfs_vmobject_wlock(object);
5788 for (i = 0; i < reqstart; i++) {
5791 vm_page_unlock(m[i]);
5793 for (i = reqstart + reqsize; i < pcount; i++) {
5796 vm_page_unlock(m[i]);
5799 if (mreq->valid && reqsize == 1) {
5800 if (mreq->valid != VM_PAGE_BITS_ALL)
5801 vm_page_zero_invalid(mreq, TRUE);
5802 zfs_vmobject_wunlock(object);
5804 return (zfs_vm_pagerret_ok);
5807 PCPU_INC(cnt.v_vnodein);
5808 PCPU_ADD(cnt.v_vnodepgsin, reqsize);
5810 if (IDX_TO_OFF(mreq->pindex) >= object->un_pager.vnp.vnp_size) {
5811 for (i = reqstart; i < reqstart + reqsize; i++) {
5815 vm_page_unlock(m[i]);
5818 zfs_vmobject_wunlock(object);
5820 return (zfs_vm_pagerret_bad);
5824 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size)
5825 lsize = object->un_pager.vnp.vnp_size - IDX_TO_OFF(mlast->pindex);
5827 zfs_vmobject_wunlock(object);
5829 for (i = reqstart; i < reqstart + reqsize; i++) {
5831 if (i == (reqstart + reqsize - 1))
5833 va = zfs_map_page(m[i], &sf);
5834 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex),
5835 size, va, DMU_READ_PREFETCH);
5836 if (size != PAGE_SIZE)
5837 bzero(va + size, PAGE_SIZE - size);
5843 zfs_vmobject_wlock(object);
5845 for (i = reqstart; i < reqstart + reqsize; i++) {
5847 m[i]->valid = VM_PAGE_BITS_ALL;
5848 KASSERT(m[i]->dirty == 0, ("zfs_getpages: page %p is dirty", m[i]));
5850 vm_page_readahead_finish(m[i]);
5853 zfs_vmobject_wunlock(object);
5855 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
5857 return (error ? zfs_vm_pagerret_error : zfs_vm_pagerret_ok);
5861 zfs_freebsd_getpages(ap)
5862 struct vop_getpages_args /* {
5867 vm_ooffset_t a_offset;
5871 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_reqpage));
5875 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
5878 znode_t *zp = VTOZ(vp);
5879 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5888 vm_ooffset_t lo_off;
5899 object = vp->v_object;
5903 KASSERT(ma[0]->object == object, ("mismatching object"));
5904 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
5906 for (i = 0; i < pcount; i++)
5907 rtvals[i] = zfs_vm_pagerret_error;
5909 off = IDX_TO_OFF(ma[0]->pindex);
5910 blksz = zp->z_blksz;
5911 lo_off = rounddown(off, blksz);
5912 lo_len = roundup(len + (off - lo_off), blksz);
5913 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER);
5915 zfs_vmobject_wlock(object);
5916 if (len + off > object->un_pager.vnp.vnp_size) {
5917 if (object->un_pager.vnp.vnp_size > off) {
5920 len = object->un_pager.vnp.vnp_size - off;
5922 if ((pgoff = (int)len & PAGE_MASK) != 0) {
5924 * If the object is locked and the following
5925 * conditions hold, then the page's dirty
5926 * field cannot be concurrently changed by a
5930 vm_page_assert_sbusied(m);
5931 KASSERT(!pmap_page_is_write_mapped(m),
5932 ("zfs_putpages: page %p is not read-only", m));
5933 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
5940 if (ncount < pcount) {
5941 for (i = ncount; i < pcount; i++) {
5942 rtvals[i] = zfs_vm_pagerret_bad;
5946 zfs_vmobject_wunlock(object);
5951 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
5952 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
5957 tx = dmu_tx_create(zfsvfs->z_os);
5958 dmu_tx_hold_write(tx, zp->z_id, off, len);
5960 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
5961 zfs_sa_upgrade_txholds(tx, zp);
5962 err = dmu_tx_assign(tx, TXG_NOWAIT);
5964 if (err == ERESTART) {
5973 if (zp->z_blksz < PAGE_SIZE) {
5975 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
5976 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
5977 va = zfs_map_page(ma[i], &sf);
5978 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
5982 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
5986 uint64_t mtime[2], ctime[2];
5987 sa_bulk_attr_t bulk[3];
5990 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
5992 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
5994 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
5996 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
5998 (void)sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
5999 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
6001 zfs_vmobject_wlock(object);
6002 for (i = 0; i < ncount; i++) {
6003 rtvals[i] = zfs_vm_pagerret_ok;
6004 vm_page_undirty(ma[i]);
6006 zfs_vmobject_wunlock(object);
6007 PCPU_INC(cnt.v_vnodeout);
6008 PCPU_ADD(cnt.v_vnodepgsout, ncount);
6013 zfs_range_unlock(rl);
6014 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
6015 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
6016 zil_commit(zfsvfs->z_log, zp->z_id);
6022 zfs_freebsd_putpages(ap)
6023 struct vop_putpages_args /* {
6029 vm_ooffset_t a_offset;
6033 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
6038 zfs_freebsd_bmap(ap)
6039 struct vop_bmap_args /* {
6042 struct bufobj **a_bop;
6049 if (ap->a_bop != NULL)
6050 *ap->a_bop = &ap->a_vp->v_bufobj;
6051 if (ap->a_bnp != NULL)
6052 *ap->a_bnp = ap->a_bn;
6053 if (ap->a_runp != NULL)
6055 if (ap->a_runb != NULL)
6062 zfs_freebsd_open(ap)
6063 struct vop_open_args /* {
6066 struct ucred *a_cred;
6067 struct thread *a_td;
6070 vnode_t *vp = ap->a_vp;
6071 znode_t *zp = VTOZ(vp);
6074 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
6076 vnode_create_vobject(vp, zp->z_size, ap->a_td);
6081 zfs_freebsd_close(ap)
6082 struct vop_close_args /* {
6085 struct ucred *a_cred;
6086 struct thread *a_td;
6090 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
6094 zfs_freebsd_ioctl(ap)
6095 struct vop_ioctl_args /* {
6105 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
6106 ap->a_fflag, ap->a_cred, NULL, NULL));
6110 zfs_freebsd_read(ap)
6111 struct vop_read_args /* {
6115 struct ucred *a_cred;
6119 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6124 zfs_freebsd_write(ap)
6125 struct vop_write_args /* {
6129 struct ucred *a_cred;
6133 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6138 zfs_freebsd_access(ap)
6139 struct vop_access_args /* {
6141 accmode_t a_accmode;
6142 struct ucred *a_cred;
6143 struct thread *a_td;
6146 vnode_t *vp = ap->a_vp;
6147 znode_t *zp = VTOZ(vp);
6152 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
6154 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
6156 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
6159 * VADMIN has to be handled by vaccess().
6162 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
6164 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
6165 zp->z_gid, accmode, ap->a_cred, NULL);
6170 * For VEXEC, ensure that at least one execute bit is set for
6173 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
6174 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
6182 zfs_freebsd_lookup(ap)
6183 struct vop_lookup_args /* {
6184 struct vnode *a_dvp;
6185 struct vnode **a_vpp;
6186 struct componentname *a_cnp;
6189 struct componentname *cnp = ap->a_cnp;
6190 char nm[NAME_MAX + 1];
6192 ASSERT(cnp->cn_namelen < sizeof(nm));
6193 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
6195 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
6196 cnp->cn_cred, cnp->cn_thread, 0));
6200 zfs_freebsd_create(ap)
6201 struct vop_create_args /* {
6202 struct vnode *a_dvp;
6203 struct vnode **a_vpp;
6204 struct componentname *a_cnp;
6205 struct vattr *a_vap;
6208 struct componentname *cnp = ap->a_cnp;
6209 vattr_t *vap = ap->a_vap;
6212 ASSERT(cnp->cn_flags & SAVENAME);
6214 vattr_init_mask(vap);
6215 mode = vap->va_mode & ALLPERMS;
6217 error = zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
6218 ap->a_vpp, cnp->cn_cred, cnp->cn_thread);
6219 #ifdef FREEBSD_NAMECACHE
6220 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
6221 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
6227 zfs_freebsd_remove(ap)
6228 struct vop_remove_args /* {
6229 struct vnode *a_dvp;
6231 struct componentname *a_cnp;
6235 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6237 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr,
6238 ap->a_cnp->cn_cred, NULL, 0));
6242 zfs_freebsd_mkdir(ap)
6243 struct vop_mkdir_args /* {
6244 struct vnode *a_dvp;
6245 struct vnode **a_vpp;
6246 struct componentname *a_cnp;
6247 struct vattr *a_vap;
6250 vattr_t *vap = ap->a_vap;
6252 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6254 vattr_init_mask(vap);
6256 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
6257 ap->a_cnp->cn_cred, NULL, 0, NULL));
6261 zfs_freebsd_rmdir(ap)
6262 struct vop_rmdir_args /* {
6263 struct vnode *a_dvp;
6265 struct componentname *a_cnp;
6268 struct componentname *cnp = ap->a_cnp;
6270 ASSERT(cnp->cn_flags & SAVENAME);
6272 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0));
6276 zfs_freebsd_readdir(ap)
6277 struct vop_readdir_args /* {
6280 struct ucred *a_cred;
6287 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
6288 ap->a_ncookies, ap->a_cookies));
6292 zfs_freebsd_fsync(ap)
6293 struct vop_fsync_args /* {
6296 struct thread *a_td;
6301 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
6305 zfs_freebsd_getattr(ap)
6306 struct vop_getattr_args /* {
6308 struct vattr *a_vap;
6309 struct ucred *a_cred;
6312 vattr_t *vap = ap->a_vap;
6318 xvap.xva_vattr = *vap;
6319 xvap.xva_vattr.va_mask |= AT_XVATTR;
6321 /* Convert chflags into ZFS-type flags. */
6322 /* XXX: what about SF_SETTABLE?. */
6323 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
6324 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
6325 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
6326 XVA_SET_REQ(&xvap, XAT_NODUMP);
6327 XVA_SET_REQ(&xvap, XAT_READONLY);
6328 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
6329 XVA_SET_REQ(&xvap, XAT_SYSTEM);
6330 XVA_SET_REQ(&xvap, XAT_HIDDEN);
6331 XVA_SET_REQ(&xvap, XAT_REPARSE);
6332 XVA_SET_REQ(&xvap, XAT_OFFLINE);
6333 XVA_SET_REQ(&xvap, XAT_SPARSE);
6335 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
6339 /* Convert ZFS xattr into chflags. */
6340 #define FLAG_CHECK(fflag, xflag, xfield) do { \
6341 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
6342 fflags |= (fflag); \
6344 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
6345 xvap.xva_xoptattrs.xoa_immutable);
6346 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
6347 xvap.xva_xoptattrs.xoa_appendonly);
6348 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
6349 xvap.xva_xoptattrs.xoa_nounlink);
6350 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
6351 xvap.xva_xoptattrs.xoa_archive);
6352 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
6353 xvap.xva_xoptattrs.xoa_nodump);
6354 FLAG_CHECK(UF_READONLY, XAT_READONLY,
6355 xvap.xva_xoptattrs.xoa_readonly);
6356 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
6357 xvap.xva_xoptattrs.xoa_system);
6358 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
6359 xvap.xva_xoptattrs.xoa_hidden);
6360 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
6361 xvap.xva_xoptattrs.xoa_reparse);
6362 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
6363 xvap.xva_xoptattrs.xoa_offline);
6364 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
6365 xvap.xva_xoptattrs.xoa_sparse);
6368 *vap = xvap.xva_vattr;
6369 vap->va_flags = fflags;
6374 zfs_freebsd_setattr(ap)
6375 struct vop_setattr_args /* {
6377 struct vattr *a_vap;
6378 struct ucred *a_cred;
6381 vnode_t *vp = ap->a_vp;
6382 vattr_t *vap = ap->a_vap;
6383 cred_t *cred = ap->a_cred;
6388 vattr_init_mask(vap);
6389 vap->va_mask &= ~AT_NOSET;
6392 xvap.xva_vattr = *vap;
6394 zflags = VTOZ(vp)->z_pflags;
6396 if (vap->va_flags != VNOVAL) {
6397 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
6400 if (zfsvfs->z_use_fuids == B_FALSE)
6401 return (EOPNOTSUPP);
6403 fflags = vap->va_flags;
6406 * We need to figure out whether it makes sense to allow
6407 * UF_REPARSE through, since we don't really have other
6408 * facilities to handle reparse points and zfs_setattr()
6409 * doesn't currently allow setting that attribute anyway.
6411 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
6412 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
6413 UF_OFFLINE|UF_SPARSE)) != 0)
6414 return (EOPNOTSUPP);
6416 * Unprivileged processes are not permitted to unset system
6417 * flags, or modify flags if any system flags are set.
6418 * Privileged non-jail processes may not modify system flags
6419 * if securelevel > 0 and any existing system flags are set.
6420 * Privileged jail processes behave like privileged non-jail
6421 * processes if the security.jail.chflags_allowed sysctl is
6422 * is non-zero; otherwise, they behave like unprivileged
6425 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
6426 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
6428 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6429 error = securelevel_gt(cred, 0);
6435 * Callers may only modify the file flags on objects they
6436 * have VADMIN rights for.
6438 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
6441 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6445 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
6450 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
6451 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
6452 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
6453 XVA_SET_REQ(&xvap, (xflag)); \
6454 (xfield) = ((fflags & (fflag)) != 0); \
6457 /* Convert chflags into ZFS-type flags. */
6458 /* XXX: what about SF_SETTABLE?. */
6459 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
6460 xvap.xva_xoptattrs.xoa_immutable);
6461 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
6462 xvap.xva_xoptattrs.xoa_appendonly);
6463 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
6464 xvap.xva_xoptattrs.xoa_nounlink);
6465 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
6466 xvap.xva_xoptattrs.xoa_archive);
6467 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
6468 xvap.xva_xoptattrs.xoa_nodump);
6469 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
6470 xvap.xva_xoptattrs.xoa_readonly);
6471 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
6472 xvap.xva_xoptattrs.xoa_system);
6473 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
6474 xvap.xva_xoptattrs.xoa_hidden);
6475 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
6476 xvap.xva_xoptattrs.xoa_hidden);
6477 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
6478 xvap.xva_xoptattrs.xoa_offline);
6479 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
6480 xvap.xva_xoptattrs.xoa_sparse);
6483 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
6487 zfs_freebsd_rename(ap)
6488 struct vop_rename_args /* {
6489 struct vnode *a_fdvp;
6490 struct vnode *a_fvp;
6491 struct componentname *a_fcnp;
6492 struct vnode *a_tdvp;
6493 struct vnode *a_tvp;
6494 struct componentname *a_tcnp;
6497 vnode_t *fdvp = ap->a_fdvp;
6498 vnode_t *fvp = ap->a_fvp;
6499 vnode_t *tdvp = ap->a_tdvp;
6500 vnode_t *tvp = ap->a_tvp;
6503 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
6504 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
6507 * Check for cross-device rename.
6509 if ((fdvp->v_mount != tdvp->v_mount) ||
6510 (tvp && (fdvp->v_mount != tvp->v_mount)))
6513 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp,
6514 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0);
6528 zfs_freebsd_symlink(ap)
6529 struct vop_symlink_args /* {
6530 struct vnode *a_dvp;
6531 struct vnode **a_vpp;
6532 struct componentname *a_cnp;
6533 struct vattr *a_vap;
6537 struct componentname *cnp = ap->a_cnp;
6538 vattr_t *vap = ap->a_vap;
6540 ASSERT(cnp->cn_flags & SAVENAME);
6542 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
6543 vattr_init_mask(vap);
6545 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
6546 ap->a_target, cnp->cn_cred, cnp->cn_thread));
6550 zfs_freebsd_readlink(ap)
6551 struct vop_readlink_args /* {
6554 struct ucred *a_cred;
6558 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
6562 zfs_freebsd_link(ap)
6563 struct vop_link_args /* {
6564 struct vnode *a_tdvp;
6566 struct componentname *a_cnp;
6569 struct componentname *cnp = ap->a_cnp;
6570 vnode_t *vp = ap->a_vp;
6571 vnode_t *tdvp = ap->a_tdvp;
6573 if (tdvp->v_mount != vp->v_mount)
6576 ASSERT(cnp->cn_flags & SAVENAME);
6578 return (zfs_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
6582 zfs_freebsd_inactive(ap)
6583 struct vop_inactive_args /* {
6585 struct thread *a_td;
6588 vnode_t *vp = ap->a_vp;
6590 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
6595 zfs_freebsd_reclaim(ap)
6596 struct vop_reclaim_args /* {
6598 struct thread *a_td;
6601 vnode_t *vp = ap->a_vp;
6602 znode_t *zp = VTOZ(vp);
6603 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6607 /* Destroy the vm object and flush associated pages. */
6608 vnode_destroy_vobject(vp);
6611 * z_teardown_inactive_lock protects from a race with
6612 * zfs_znode_dmu_fini in zfsvfs_teardown during
6615 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
6616 if (zp->z_sa_hdl == NULL)
6620 rw_exit(&zfsvfs->z_teardown_inactive_lock);
6628 struct vop_fid_args /* {
6634 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
6638 zfs_freebsd_pathconf(ap)
6639 struct vop_pathconf_args /* {
6642 register_t *a_retval;
6648 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
6650 *ap->a_retval = val;
6651 else if (error == EOPNOTSUPP)
6652 error = vop_stdpathconf(ap);
6657 zfs_freebsd_fifo_pathconf(ap)
6658 struct vop_pathconf_args /* {
6661 register_t *a_retval;
6665 switch (ap->a_name) {
6666 case _PC_ACL_EXTENDED:
6668 case _PC_ACL_PATH_MAX:
6669 case _PC_MAC_PRESENT:
6670 return (zfs_freebsd_pathconf(ap));
6672 return (fifo_specops.vop_pathconf(ap));
6677 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
6678 * extended attribute name:
6681 * system freebsd:system:
6682 * user (none, can be used to access ZFS fsattr(5) attributes
6683 * created on Solaris)
6686 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
6689 const char *namespace, *prefix, *suffix;
6691 /* We don't allow '/' character in attribute name. */
6692 if (strchr(name, '/') != NULL)
6694 /* We don't allow attribute names that start with "freebsd:" string. */
6695 if (strncmp(name, "freebsd:", 8) == 0)
6698 bzero(attrname, size);
6700 switch (attrnamespace) {
6701 case EXTATTR_NAMESPACE_USER:
6703 prefix = "freebsd:";
6704 namespace = EXTATTR_NAMESPACE_USER_STRING;
6708 * This is the default namespace by which we can access all
6709 * attributes created on Solaris.
6711 prefix = namespace = suffix = "";
6714 case EXTATTR_NAMESPACE_SYSTEM:
6715 prefix = "freebsd:";
6716 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
6719 case EXTATTR_NAMESPACE_EMPTY:
6723 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
6725 return (ENAMETOOLONG);
6731 * Vnode operating to retrieve a named extended attribute.
6734 zfs_getextattr(struct vop_getextattr_args *ap)
6737 IN struct vnode *a_vp;
6738 IN int a_attrnamespace;
6739 IN const char *a_name;
6740 INOUT struct uio *a_uio;
6742 IN struct ucred *a_cred;
6743 IN struct thread *a_td;
6747 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6748 struct thread *td = ap->a_td;
6749 struct nameidata nd;
6752 vnode_t *xvp = NULL, *vp;
6755 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6756 ap->a_cred, ap->a_td, VREAD);
6760 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6767 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6775 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
6777 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
6779 NDFREE(&nd, NDF_ONLY_PNBUF);
6782 if (error == ENOENT)
6787 if (ap->a_size != NULL) {
6788 error = VOP_GETATTR(vp, &va, ap->a_cred);
6790 *ap->a_size = (size_t)va.va_size;
6791 } else if (ap->a_uio != NULL)
6792 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6795 vn_close(vp, flags, ap->a_cred, td);
6802 * Vnode operation to remove a named attribute.
6805 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
6808 IN struct vnode *a_vp;
6809 IN int a_attrnamespace;
6810 IN const char *a_name;
6811 IN struct ucred *a_cred;
6812 IN struct thread *a_td;
6816 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6817 struct thread *td = ap->a_td;
6818 struct nameidata nd;
6821 vnode_t *xvp = NULL, *vp;
6824 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6825 ap->a_cred, ap->a_td, VWRITE);
6829 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6836 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6843 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
6844 UIO_SYSSPACE, attrname, xvp, td);
6849 NDFREE(&nd, NDF_ONLY_PNBUF);
6850 if (error == ENOENT)
6855 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
6856 NDFREE(&nd, NDF_ONLY_PNBUF);
6859 if (vp == nd.ni_dvp)
6869 * Vnode operation to set a named attribute.
6872 zfs_setextattr(struct vop_setextattr_args *ap)
6875 IN struct vnode *a_vp;
6876 IN int a_attrnamespace;
6877 IN const char *a_name;
6878 INOUT struct uio *a_uio;
6879 IN struct ucred *a_cred;
6880 IN struct thread *a_td;
6884 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6885 struct thread *td = ap->a_td;
6886 struct nameidata nd;
6889 vnode_t *xvp = NULL, *vp;
6892 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6893 ap->a_cred, ap->a_td, VWRITE);
6897 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6904 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6905 LOOKUP_XATTR | CREATE_XATTR_DIR);
6911 flags = FFLAGS(O_WRONLY | O_CREAT);
6912 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
6914 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
6916 NDFREE(&nd, NDF_ONLY_PNBUF);
6924 error = VOP_SETATTR(vp, &va, ap->a_cred);
6926 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6929 vn_close(vp, flags, ap->a_cred, td);
6936 * Vnode operation to retrieve extended attributes on a vnode.
6939 zfs_listextattr(struct vop_listextattr_args *ap)
6942 IN struct vnode *a_vp;
6943 IN int a_attrnamespace;
6944 INOUT struct uio *a_uio;
6946 IN struct ucred *a_cred;
6947 IN struct thread *a_td;
6951 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6952 struct thread *td = ap->a_td;
6953 struct nameidata nd;
6954 char attrprefix[16];
6955 u_char dirbuf[sizeof(struct dirent)];
6958 struct uio auio, *uio = ap->a_uio;
6959 size_t *sizep = ap->a_size;
6961 vnode_t *xvp = NULL, *vp;
6962 int done, error, eof, pos;
6964 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6965 ap->a_cred, ap->a_td, VREAD);
6969 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
6970 sizeof(attrprefix));
6973 plen = strlen(attrprefix);
6980 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6985 * ENOATTR means that the EA directory does not yet exist,
6986 * i.e. there are no extended attributes there.
6988 if (error == ENOATTR)
6993 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
6994 UIO_SYSSPACE, ".", xvp, td);
6997 NDFREE(&nd, NDF_ONLY_PNBUF);
7003 auio.uio_iov = &aiov;
7004 auio.uio_iovcnt = 1;
7005 auio.uio_segflg = UIO_SYSSPACE;
7007 auio.uio_rw = UIO_READ;
7008 auio.uio_offset = 0;
7013 aiov.iov_base = (void *)dirbuf;
7014 aiov.iov_len = sizeof(dirbuf);
7015 auio.uio_resid = sizeof(dirbuf);
7016 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
7017 done = sizeof(dirbuf) - auio.uio_resid;
7020 for (pos = 0; pos < done;) {
7021 dp = (struct dirent *)(dirbuf + pos);
7022 pos += dp->d_reclen;
7024 * XXX: Temporarily we also accept DT_UNKNOWN, as this
7025 * is what we get when attribute was created on Solaris.
7027 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
7029 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
7031 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
7033 nlen = dp->d_namlen - plen;
7036 else if (uio != NULL) {
7038 * Format of extattr name entry is one byte for
7039 * length and the rest for name.
7041 error = uiomove(&nlen, 1, uio->uio_rw, uio);
7043 error = uiomove(dp->d_name + plen, nlen,
7050 } while (!eof && error == 0);
7059 zfs_freebsd_getacl(ap)
7060 struct vop_getacl_args /* {
7069 vsecattr_t vsecattr;
7071 if (ap->a_type != ACL_TYPE_NFS4)
7074 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
7075 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
7078 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
7079 if (vsecattr.vsa_aclentp != NULL)
7080 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
7086 zfs_freebsd_setacl(ap)
7087 struct vop_setacl_args /* {
7096 vsecattr_t vsecattr;
7097 int aclbsize; /* size of acl list in bytes */
7100 if (ap->a_type != ACL_TYPE_NFS4)
7103 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
7107 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
7108 * splitting every entry into two and appending "canonical six"
7109 * entries at the end. Don't allow for setting an ACL that would
7110 * cause chmod(2) to run out of ACL entries.
7112 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
7115 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
7119 vsecattr.vsa_mask = VSA_ACE;
7120 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
7121 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
7122 aaclp = vsecattr.vsa_aclentp;
7123 vsecattr.vsa_aclentsz = aclbsize;
7125 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
7126 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
7127 kmem_free(aaclp, aclbsize);
7133 zfs_freebsd_aclcheck(ap)
7134 struct vop_aclcheck_args /* {
7143 return (EOPNOTSUPP);
7146 struct vop_vector zfs_vnodeops;
7147 struct vop_vector zfs_fifoops;
7148 struct vop_vector zfs_shareops;
7150 struct vop_vector zfs_vnodeops = {
7151 .vop_default = &default_vnodeops,
7152 .vop_inactive = zfs_freebsd_inactive,
7153 .vop_reclaim = zfs_freebsd_reclaim,
7154 .vop_access = zfs_freebsd_access,
7155 #ifdef FREEBSD_NAMECACHE
7156 .vop_lookup = vfs_cache_lookup,
7157 .vop_cachedlookup = zfs_freebsd_lookup,
7159 .vop_lookup = zfs_freebsd_lookup,
7161 .vop_getattr = zfs_freebsd_getattr,
7162 .vop_setattr = zfs_freebsd_setattr,
7163 .vop_create = zfs_freebsd_create,
7164 .vop_mknod = zfs_freebsd_create,
7165 .vop_mkdir = zfs_freebsd_mkdir,
7166 .vop_readdir = zfs_freebsd_readdir,
7167 .vop_fsync = zfs_freebsd_fsync,
7168 .vop_open = zfs_freebsd_open,
7169 .vop_close = zfs_freebsd_close,
7170 .vop_rmdir = zfs_freebsd_rmdir,
7171 .vop_ioctl = zfs_freebsd_ioctl,
7172 .vop_link = zfs_freebsd_link,
7173 .vop_symlink = zfs_freebsd_symlink,
7174 .vop_readlink = zfs_freebsd_readlink,
7175 .vop_read = zfs_freebsd_read,
7176 .vop_write = zfs_freebsd_write,
7177 .vop_remove = zfs_freebsd_remove,
7178 .vop_rename = zfs_freebsd_rename,
7179 .vop_pathconf = zfs_freebsd_pathconf,
7180 .vop_bmap = zfs_freebsd_bmap,
7181 .vop_fid = zfs_freebsd_fid,
7182 .vop_getextattr = zfs_getextattr,
7183 .vop_deleteextattr = zfs_deleteextattr,
7184 .vop_setextattr = zfs_setextattr,
7185 .vop_listextattr = zfs_listextattr,
7186 .vop_getacl = zfs_freebsd_getacl,
7187 .vop_setacl = zfs_freebsd_setacl,
7188 .vop_aclcheck = zfs_freebsd_aclcheck,
7189 .vop_getpages = zfs_freebsd_getpages,
7190 .vop_putpages = zfs_freebsd_putpages,
7193 struct vop_vector zfs_fifoops = {
7194 .vop_default = &fifo_specops,
7195 .vop_fsync = zfs_freebsd_fsync,
7196 .vop_access = zfs_freebsd_access,
7197 .vop_getattr = zfs_freebsd_getattr,
7198 .vop_inactive = zfs_freebsd_inactive,
7199 .vop_read = VOP_PANIC,
7200 .vop_reclaim = zfs_freebsd_reclaim,
7201 .vop_setattr = zfs_freebsd_setattr,
7202 .vop_write = VOP_PANIC,
7203 .vop_pathconf = zfs_freebsd_fifo_pathconf,
7204 .vop_fid = zfs_freebsd_fid,
7205 .vop_getacl = zfs_freebsd_getacl,
7206 .vop_setacl = zfs_freebsd_setacl,
7207 .vop_aclcheck = zfs_freebsd_aclcheck,
7211 * special share hidden files vnode operations template
7213 struct vop_vector zfs_shareops = {
7214 .vop_default = &default_vnodeops,
7215 .vop_access = zfs_freebsd_access,
7216 .vop_inactive = zfs_freebsd_inactive,
7217 .vop_reclaim = zfs_freebsd_reclaim,
7218 .vop_fid = zfs_freebsd_fid,
7219 .vop_pathconf = zfs_freebsd_pathconf,