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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Portions Copyright 2007 Jeremy Teo */
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
35 #include <sys/vnode.h>
39 #include <sys/taskq.h>
41 #include <sys/atomic.h>
42 #include <sys/namei.h>
44 #include <sys/cmn_err.h>
45 #include <sys/errno.h>
46 #include <sys/unistd.h>
47 #include <sys/zfs_dir.h>
48 #include <sys/zfs_ioctl.h>
49 #include <sys/fs/zfs.h>
55 #include <sys/dirent.h>
56 #include <sys/policy.h>
57 #include <sys/sunddi.h>
58 #include <sys/filio.h>
59 #include <sys/zfs_ctldir.h>
60 #include <sys/zfs_fuid.h>
62 #include <sys/zfs_rlock.h>
63 #include <sys/extdirent.h>
64 #include <sys/kidmap.h>
67 #include <sys/sf_buf.h>
68 #include <sys/sched.h>
74 * Each vnode op performs some logical unit of work. To do this, the ZPL must
75 * properly lock its in-core state, create a DMU transaction, do the work,
76 * record this work in the intent log (ZIL), commit the DMU transaction,
77 * and wait for the intent log to commit if it is a synchronous operation.
78 * Moreover, the vnode ops must work in both normal and log replay context.
79 * The ordering of events is important to avoid deadlocks and references
80 * to freed memory. The example below illustrates the following Big Rules:
82 * (1) A check must be made in each zfs thread for a mounted file system.
83 * This is done avoiding races using ZFS_ENTER(zfsvfs).
84 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
85 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
86 * can return EIO from the calling function.
88 * (2) VN_RELE() should always be the last thing except for zil_commit()
89 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
90 * First, if it's the last reference, the vnode/znode
91 * can be freed, so the zp may point to freed memory. Second, the last
92 * reference will call zfs_zinactive(), which may induce a lot of work --
93 * pushing cached pages (which acquires range locks) and syncing out
94 * cached atime changes. Third, zfs_zinactive() may require a new tx,
95 * which could deadlock the system if you were already holding one.
96 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
98 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
99 * as they can span dmu_tx_assign() calls.
101 * (4) Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
102 * In normal operation, this will be TXG_NOWAIT. During ZIL replay,
103 * it will be a specific txg. Either way, dmu_tx_assign() never blocks.
104 * This is critical because we don't want to block while holding locks.
105 * Note, in particular, that if a lock is sometimes acquired before
106 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
107 * use a non-blocking assign can deadlock the system. The scenario:
109 * Thread A has grabbed a lock before calling dmu_tx_assign().
110 * Thread B is in an already-assigned tx, and blocks for this lock.
111 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
112 * forever, because the previous txg can't quiesce until B's tx commits.
114 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
115 * then drop all locks, call dmu_tx_wait(), and try again.
117 * (5) If the operation succeeded, generate the intent log entry for it
118 * before dropping locks. This ensures that the ordering of events
119 * in the intent log matches the order in which they actually occurred.
121 * (6) At the end of each vnode op, the DMU tx must always commit,
122 * regardless of whether there were any errors.
124 * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid)
125 * to ensure that synchronous semantics are provided when necessary.
127 * In general, this is how things should be ordered in each vnode op:
129 * ZFS_ENTER(zfsvfs); // exit if unmounted
131 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
132 * rw_enter(...); // grab any other locks you need
133 * tx = dmu_tx_create(...); // get DMU tx
134 * dmu_tx_hold_*(); // hold each object you might modify
135 * error = dmu_tx_assign(tx, zfsvfs->z_assign); // try to assign
137 * rw_exit(...); // drop locks
138 * zfs_dirent_unlock(dl); // unlock directory entry
139 * VN_RELE(...); // release held vnodes
140 * if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
145 * dmu_tx_abort(tx); // abort DMU tx
146 * ZFS_EXIT(zfsvfs); // finished in zfs
147 * return (error); // really out of space
149 * error = do_real_work(); // do whatever this VOP does
151 * zfs_log_*(...); // on success, make ZIL entry
152 * dmu_tx_commit(tx); // commit DMU tx -- error or not
153 * rw_exit(...); // drop locks
154 * zfs_dirent_unlock(dl); // unlock directory entry
155 * VN_RELE(...); // release held vnodes
156 * zil_commit(zilog, seq, foid); // synchronous when necessary
157 * ZFS_EXIT(zfsvfs); // finished in zfs
158 * return (error); // done, report error
163 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
165 znode_t *zp = VTOZ(*vpp);
167 if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) &&
168 ((flag & FAPPEND) == 0)) {
172 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
173 ZTOV(zp)->v_type == VREG &&
174 !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
175 zp->z_phys->zp_size > 0)
176 if (fs_vscan(*vpp, cr, 0) != 0)
179 /* Keep a count of the synchronous opens in the znode */
180 if (flag & (FSYNC | FDSYNC))
181 atomic_inc_32(&zp->z_sync_cnt);
188 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
189 caller_context_t *ct)
191 znode_t *zp = VTOZ(vp);
193 /* Decrement the synchronous opens in the znode */
194 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
195 atomic_dec_32(&zp->z_sync_cnt);
198 * Clean up any locks held by this process on the vp.
200 cleanlocks(vp, ddi_get_pid(), 0);
201 cleanshares(vp, ddi_get_pid());
203 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
204 ZTOV(zp)->v_type == VREG &&
205 !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
206 zp->z_phys->zp_size > 0)
207 VERIFY(fs_vscan(vp, cr, 1) == 0);
213 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
214 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
217 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
219 znode_t *zp = VTOZ(vp);
220 uint64_t noff = (uint64_t)*off; /* new offset */
225 file_sz = zp->z_phys->zp_size;
226 if (noff >= file_sz) {
230 if (cmd == _FIO_SEEK_HOLE)
235 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
238 if ((error == ESRCH) || (noff > file_sz)) {
240 * Handle the virtual hole at the end of file.
257 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
258 int *rvalp, caller_context_t *ct)
270 * The following two ioctls are used by bfu. Faking out,
271 * necessary to avoid bfu errors.
279 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
283 zfsvfs = zp->z_zfsvfs;
287 /* offset parameter is in/out */
288 error = zfs_holey(vp, com, &off);
292 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
300 * When a file is memory mapped, we must keep the IO data synchronized
301 * between the DMU cache and the memory mapped pages. What this means:
303 * On Write: If we find a memory mapped page, we write to *both*
304 * the page and the dmu buffer.
306 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
307 * the file is memory mapped.
310 mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx)
312 znode_t *zp = VTOZ(vp);
313 objset_t *os = zp->z_zfsvfs->z_os;
322 ASSERT(vp->v_mount != NULL);
326 start = uio->uio_loffset;
327 off = start & PAGEOFFSET;
330 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
331 uint64_t bytes = MIN(PAGESIZE - off, len);
335 if ((m = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
336 vm_page_is_valid(m, (vm_offset_t)off, bytes)) {
340 if (vm_page_sleep_if_busy(m, FALSE, "zfsmwb"))
342 fsize = obj->un_pager.vnp.vnp_size;
344 vm_page_lock_queues();
346 vm_page_unlock_queues();
347 VM_OBJECT_UNLOCK(obj);
349 error = dmu_write_uio(os, zp->z_id, uio,
355 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
356 va = (caddr_t)sf_buf_kva(sf);
357 woff = uio->uio_loffset - off;
358 error = uiomove(va + off, bytes, UIO_WRITE, uio);
360 * The uiomove() above could have been partially
361 * successful, that's why we call dmu_write()
362 * below unconditionally. The page was marked
363 * non-dirty above and we would lose the changes
364 * without doing so. If the uiomove() failed
365 * entirely, well, we just write what we got
366 * before one more time.
368 dmu_write(os, zp->z_id, woff,
369 MIN(PAGESIZE, fsize - woff), va, tx);
376 if (__predict_false(obj->cache != NULL)) {
377 vm_page_cache_free(obj, OFF_TO_IDX(start),
378 OFF_TO_IDX(start) + 1);
387 VM_OBJECT_UNLOCK(obj);
388 if (error == 0 && dirbytes > 0)
389 error = dmu_write_uio(os, zp->z_id, uio, dirbytes, tx);
394 * When a file is memory mapped, we must keep the IO data synchronized
395 * between the DMU cache and the memory mapped pages. What this means:
397 * On Read: We "read" preferentially from memory mapped pages,
398 * else we default from the dmu buffer.
400 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
401 * the file is memory mapped.
404 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
406 znode_t *zp = VTOZ(vp);
407 objset_t *os = zp->z_zfsvfs->z_os;
417 ASSERT(vp->v_mount != NULL);
421 start = uio->uio_loffset;
422 off = start & PAGEOFFSET;
425 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
426 uint64_t bytes = MIN(PAGESIZE - off, len);
429 if ((m = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
430 vm_page_is_valid(m, (vm_offset_t)off, bytes)) {
431 if (vm_page_sleep_if_busy(m, FALSE, "zfsmrb"))
434 VM_OBJECT_UNLOCK(obj);
436 error = dmu_read_uio(os, zp->z_id, uio,
442 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
443 va = (caddr_t)sf_buf_kva(sf);
444 error = uiomove(va + off, bytes, UIO_READ, uio);
450 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
452 * The code below is here to make sendfile(2) work
453 * correctly with ZFS. As pointed out by ups@
454 * sendfile(2) should be changed to use VOP_GETPAGES(),
455 * but it pessimize performance of sendfile/UFS, that's
456 * why I handle this special case in ZFS code.
458 if (vm_page_sleep_if_busy(m, FALSE, "zfsmrb"))
461 VM_OBJECT_UNLOCK(obj);
463 error = dmu_read_uio(os, zp->z_id, uio,
469 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
470 va = (caddr_t)sf_buf_kva(sf);
471 error = dmu_read(os, zp->z_id, start + off,
472 bytes, (void *)(va + off));
479 uio->uio_resid -= bytes;
488 VM_OBJECT_UNLOCK(obj);
489 if (error == 0 && dirbytes > 0)
490 error = dmu_read_uio(os, zp->z_id, uio, dirbytes);
494 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
497 * Read bytes from specified file into supplied buffer.
499 * IN: vp - vnode of file to be read from.
500 * uio - structure supplying read location, range info,
502 * ioflag - SYNC flags; used to provide FRSYNC semantics.
503 * cr - credentials of caller.
504 * ct - caller context
506 * OUT: uio - updated offset and range, buffer filled.
508 * RETURN: 0 if success
509 * error code if failure
512 * vp - atime updated if byte count > 0
516 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
518 znode_t *zp = VTOZ(vp);
519 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
529 if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) {
535 * Validate file offset
537 if (uio->uio_loffset < (offset_t)0) {
543 * Fasttrack empty reads
545 if (uio->uio_resid == 0) {
551 * Check for mandatory locks
553 if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
554 if (error = chklock(vp, FREAD,
555 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
562 * If we're in FRSYNC mode, sync out this znode before reading it.
565 zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
568 * Lock the range against changes.
570 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
573 * If we are reading past end-of-file we can skip
574 * to the end; but we might still need to set atime.
576 if (uio->uio_loffset >= zp->z_phys->zp_size) {
581 ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
582 n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
585 nbytes = MIN(n, zfs_read_chunk_size -
586 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
588 if (vn_has_cached_data(vp))
589 error = mappedread(vp, nbytes, uio);
591 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
593 /* convert checksum errors into IO errors */
603 zfs_range_unlock(rl);
605 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
611 * Fault in the pages of the first n bytes specified by the uio structure.
612 * 1 byte in each page is touched and the uio struct is unmodified.
613 * Any error will exit this routine as this is only a best
614 * attempt to get the pages resident. This is a copy of ufs_trans_touch().
617 zfs_prefault_write(ssize_t n, struct uio *uio)
623 if (uio->uio_segflg != UIO_USERSPACE)
629 cnt = MIN(iov->iov_len, n);
631 /* empty iov entry */
637 * touch each page in this segment.
643 incr = MIN(cnt, PAGESIZE);
648 * touch the last byte in case it straddles a page.
658 * Write the bytes to a file.
660 * IN: vp - vnode of file to be written to.
661 * uio - structure supplying write location, range info,
663 * ioflag - IO_APPEND flag set if in append mode.
664 * cr - credentials of caller.
665 * ct - caller context (NFS/CIFS fem monitor only)
667 * OUT: uio - updated offset and range.
669 * RETURN: 0 if success
670 * error code if failure
673 * vp - ctime|mtime updated if byte count > 0
677 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
679 znode_t *zp = VTOZ(vp);
680 rlim64_t limit = MAXOFFSET_T;
681 ssize_t start_resid = uio->uio_resid;
685 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
690 int max_blksz = zfsvfs->z_max_blksz;
695 * Fasttrack empty write
701 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
708 * If immutable or not appending then return EPERM
710 pflags = zp->z_phys->zp_flags;
711 if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
712 ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
713 (uio->uio_loffset < zp->z_phys->zp_size))) {
718 zilog = zfsvfs->z_log;
721 * Pre-fault the pages to ensure slow (eg NFS) pages
724 zfs_prefault_write(n, uio);
727 * If in append mode, set the io offset pointer to eof.
729 if (ioflag & IO_APPEND) {
731 * Range lock for a file append:
732 * The value for the start of range will be determined by
733 * zfs_range_lock() (to guarantee append semantics).
734 * If this write will cause the block size to increase,
735 * zfs_range_lock() will lock the entire file, so we must
736 * later reduce the range after we grow the block size.
738 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
739 if (rl->r_len == UINT64_MAX) {
740 /* overlocked, zp_size can't change */
741 woff = uio->uio_loffset = zp->z_phys->zp_size;
743 woff = uio->uio_loffset = rl->r_off;
746 woff = uio->uio_loffset;
748 * Validate file offset
756 * If we need to grow the block size then zfs_range_lock()
757 * will lock a wider range than we request here.
758 * Later after growing the block size we reduce the range.
760 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
764 zfs_range_unlock(rl);
769 if ((woff + n) > limit || woff > (limit - n))
773 * Check for mandatory locks
775 if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
776 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
777 zfs_range_unlock(rl);
781 end_size = MAX(zp->z_phys->zp_size, woff + n);
784 * Write the file in reasonable size chunks. Each chunk is written
785 * in a separate transaction; this keeps the intent log records small
786 * and allows us to do more fine-grained space accounting.
790 * Start a transaction.
792 woff = uio->uio_loffset;
793 tx = dmu_tx_create(zfsvfs->z_os);
794 dmu_tx_hold_bonus(tx, zp->z_id);
795 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
796 error = dmu_tx_assign(tx, zfsvfs->z_assign);
798 if (error == ERESTART &&
799 zfsvfs->z_assign == TXG_NOWAIT) {
809 * If zfs_range_lock() over-locked we grow the blocksize
810 * and then reduce the lock range. This will only happen
811 * on the first iteration since zfs_range_reduce() will
812 * shrink down r_len to the appropriate size.
814 if (rl->r_len == UINT64_MAX) {
817 if (zp->z_blksz > max_blksz) {
818 ASSERT(!ISP2(zp->z_blksz));
819 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
821 new_blksz = MIN(end_size, max_blksz);
823 zfs_grow_blocksize(zp, new_blksz, tx);
824 zfs_range_reduce(rl, woff, n);
828 * XXX - should we really limit each write to z_max_blksz?
829 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
831 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
833 if (woff + nbytes > zp->z_phys->zp_size)
834 vnode_pager_setsize(vp, woff + nbytes);
836 rw_enter(&zp->z_map_lock, RW_READER);
838 tx_bytes = uio->uio_resid;
839 if (vn_has_cached_data(vp)) {
840 rw_exit(&zp->z_map_lock);
841 error = mappedwrite(vp, nbytes, uio, tx);
843 error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
845 rw_exit(&zp->z_map_lock);
847 tx_bytes -= uio->uio_resid;
850 * If we made no progress, we're done. If we made even
851 * partial progress, update the znode and ZIL accordingly.
860 * Clear Set-UID/Set-GID bits on successful write if not
861 * privileged and at least one of the excute bits is set.
863 * It would be nice to to this after all writes have
864 * been done, but that would still expose the ISUID/ISGID
865 * to another app after the partial write is committed.
867 * Note: we don't call zfs_fuid_map_id() here because
868 * user 0 is not an ephemeral uid.
870 mutex_enter(&zp->z_acl_lock);
871 if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
872 (S_IXUSR >> 6))) != 0 &&
873 (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
874 secpolicy_vnode_setid_retain(vp, cr,
875 (zp->z_phys->zp_mode & S_ISUID) != 0 &&
876 zp->z_phys->zp_uid == 0) != 0) {
877 zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
879 mutex_exit(&zp->z_acl_lock);
882 * Update time stamp. NOTE: This marks the bonus buffer as
883 * dirty, so we don't have to do it again for zp_size.
885 zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
888 * Update the file size (zp_size) if it has changed;
889 * account for possible concurrent updates.
891 while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
892 (void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
894 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
899 ASSERT(tx_bytes == nbytes);
903 zfs_range_unlock(rl);
906 * If we're in replay mode, or we made no progress, return error.
907 * Otherwise, it's at least a partial write, so it's successful.
909 if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
914 if (ioflag & (FSYNC | FDSYNC))
915 zil_commit(zilog, zp->z_last_itx, zp->z_id);
922 zfs_get_done(dmu_buf_t *db, void *vzgd)
924 zgd_t *zgd = (zgd_t *)vzgd;
925 rl_t *rl = zgd->zgd_rl;
926 vnode_t *vp = ZTOV(rl->r_zp);
927 objset_t *os = rl->r_zp->z_zfsvfs->z_os;
930 vfslocked = VFS_LOCK_GIANT(vp->v_vfsp);
931 dmu_buf_rele(db, vzgd);
932 zfs_range_unlock(rl);
934 * Release the vnode asynchronously as we currently have the
935 * txg stopped from syncing.
937 VN_RELE_ASYNC(vp, dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
938 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
939 kmem_free(zgd, sizeof (zgd_t));
940 VFS_UNLOCK_GIANT(vfslocked);
944 * Get data to generate a TX_WRITE intent log record.
947 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
949 zfsvfs_t *zfsvfs = arg;
950 objset_t *os = zfsvfs->z_os;
952 uint64_t off = lr->lr_offset;
956 int dlen = lr->lr_length; /* length of user data */
963 * Nothing to do if the file has been removed
965 if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
967 if (zp->z_unlinked) {
969 * Release the vnode asynchronously as we currently have the
970 * txg stopped from syncing.
972 VN_RELE_ASYNC(ZTOV(zp),
973 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
978 * Write records come in two flavors: immediate and indirect.
979 * For small writes it's cheaper to store the data with the
980 * log record (immediate); for large writes it's cheaper to
981 * sync the data and get a pointer to it (indirect) so that
982 * we don't have to write the data twice.
984 if (buf != NULL) { /* immediate write */
985 rl = zfs_range_lock(zp, off, dlen, RL_READER);
986 /* test for truncation needs to be done while range locked */
987 if (off >= zp->z_phys->zp_size) {
991 VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
992 } else { /* indirect write */
993 uint64_t boff; /* block starting offset */
996 * Have to lock the whole block to ensure when it's
997 * written out and it's checksum is being calculated
998 * that no one can change the data. We need to re-check
999 * blocksize after we get the lock in case it's changed!
1002 if (ISP2(zp->z_blksz)) {
1003 boff = P2ALIGN_TYPED(off, zp->z_blksz,
1009 rl = zfs_range_lock(zp, boff, dlen, RL_READER);
1010 if (zp->z_blksz == dlen)
1012 zfs_range_unlock(rl);
1014 /* test for truncation needs to be done while range locked */
1015 if (off >= zp->z_phys->zp_size) {
1019 zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
1021 zgd->zgd_zilog = zfsvfs->z_log;
1022 zgd->zgd_bp = &lr->lr_blkptr;
1023 VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
1024 ASSERT(boff == db->db_offset);
1025 lr->lr_blkoff = off - boff;
1026 error = dmu_sync(zio, db, &lr->lr_blkptr,
1027 lr->lr_common.lrc_txg, zfs_get_done, zgd);
1028 ASSERT((error && error != EINPROGRESS) ||
1029 lr->lr_length <= zp->z_blksz);
1031 zil_add_block(zfsvfs->z_log, &lr->lr_blkptr);
1033 * If we get EINPROGRESS, then we need to wait for a
1034 * write IO initiated by dmu_sync() to complete before
1035 * we can release this dbuf. We will finish everything
1036 * up in the zfs_get_done() callback.
1038 if (error == EINPROGRESS)
1040 dmu_buf_rele(db, zgd);
1041 kmem_free(zgd, sizeof (zgd_t));
1044 zfs_range_unlock(rl);
1046 * Release the vnode asynchronously as we currently have the
1047 * txg stopped from syncing.
1049 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1055 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1056 caller_context_t *ct)
1058 znode_t *zp = VTOZ(vp);
1059 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1065 if (flag & V_ACE_MASK)
1066 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1068 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1075 * Lookup an entry in a directory, or an extended attribute directory.
1076 * If it exists, return a held vnode reference for it.
1078 * IN: dvp - vnode of directory to search.
1079 * nm - name of entry to lookup.
1080 * pnp - full pathname to lookup [UNUSED].
1081 * flags - LOOKUP_XATTR set if looking for an attribute.
1082 * rdir - root directory vnode [UNUSED].
1083 * cr - credentials of caller.
1084 * ct - caller context
1085 * direntflags - directory lookup flags
1086 * realpnp - returned pathname.
1088 * OUT: vpp - vnode of located entry, NULL if not found.
1090 * RETURN: 0 if success
1091 * error code if failure
1098 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1099 int nameiop, cred_t *cr, kthread_t *td, int flags)
1101 znode_t *zdp = VTOZ(dvp);
1102 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1104 int *direntflags = NULL;
1105 void *realpnp = NULL;
1112 if (flags & LOOKUP_XATTR) {
1115 * If the xattr property is off, refuse the lookup request.
1117 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1124 * We don't allow recursive attributes..
1125 * Maybe someday we will.
1127 if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1132 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1138 * Do we have permission to get into attribute directory?
1141 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1151 if (dvp->v_type != VDIR) {
1157 * Check accessibility of directory.
1160 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1165 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1166 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1171 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1174 * Convert device special files
1176 if (IS_DEVVP(*vpp)) {
1179 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1188 /* Translate errors and add SAVENAME when needed. */
1189 if (cnp->cn_flags & ISLASTCN) {
1193 if (error == ENOENT) {
1194 error = EJUSTRETURN;
1195 cnp->cn_flags |= SAVENAME;
1201 cnp->cn_flags |= SAVENAME;
1205 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) {
1208 if (cnp->cn_flags & ISDOTDOT) {
1209 ltype = VOP_ISLOCKED(dvp);
1213 error = vn_lock(*vpp, cnp->cn_lkflags);
1214 if (cnp->cn_flags & ISDOTDOT)
1215 vn_lock(dvp, ltype | LK_RETRY);
1225 #ifdef FREEBSD_NAMECACHE
1227 * Insert name into cache (as non-existent) if appropriate.
1229 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
1230 cache_enter(dvp, *vpp, cnp);
1232 * Insert name into cache if appropriate.
1234 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1235 if (!(cnp->cn_flags & ISLASTCN) ||
1236 (nameiop != DELETE && nameiop != RENAME)) {
1237 cache_enter(dvp, *vpp, cnp);
1246 * Attempt to create a new entry in a directory. If the entry
1247 * already exists, truncate the file if permissible, else return
1248 * an error. Return the vp of the created or trunc'd file.
1250 * IN: dvp - vnode of directory to put new file entry in.
1251 * name - name of new file entry.
1252 * vap - attributes of new file.
1253 * excl - flag indicating exclusive or non-exclusive mode.
1254 * mode - mode to open file with.
1255 * cr - credentials of caller.
1256 * flag - large file flag [UNUSED].
1257 * ct - caller context
1258 * vsecp - ACL to be set
1260 * OUT: vpp - vnode of created or trunc'd entry.
1262 * RETURN: 0 if success
1263 * error code if failure
1266 * dvp - ctime|mtime updated if new entry created
1267 * vp - ctime|mtime always, atime if new
1272 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1273 vnode_t **vpp, cred_t *cr, kthread_t *td)
1275 znode_t *zp, *dzp = VTOZ(dvp);
1276 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1282 zfs_acl_t *aclp = NULL;
1283 zfs_fuid_info_t *fuidp = NULL;
1288 * If we have an ephemeral id, ACL, or XVATTR then
1289 * make sure file system is at proper version
1292 if (zfsvfs->z_use_fuids == B_FALSE &&
1293 (vsecp || (vap->va_mask & AT_XVATTR) ||
1294 IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))))
1300 zilog = zfsvfs->z_log;
1302 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1303 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1308 if (vap->va_mask & AT_XVATTR) {
1309 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1310 crgetuid(cr), cr, vap->va_type)) != 0) {
1318 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1319 vap->va_mode &= ~S_ISVTX;
1321 if (*name == '\0') {
1323 * Null component name refers to the directory itself.
1330 /* possible VN_HOLD(zp) */
1333 if (flag & FIGNORECASE)
1336 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1339 if (strcmp(name, "..") == 0)
1347 if (vsecp && aclp == NULL) {
1348 error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
1352 zfs_dirent_unlock(dl);
1361 * Create a new file object and update the directory
1364 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1369 * We only support the creation of regular files in
1370 * extended attribute directories.
1372 if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1373 (vap->va_type != VREG)) {
1378 tx = dmu_tx_create(os);
1379 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1380 if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(crgetuid(cr)) ||
1381 IS_EPHEMERAL(crgetgid(cr))) {
1382 if (zfsvfs->z_fuid_obj == 0) {
1383 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1384 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1385 FUID_SIZE_ESTIMATE(zfsvfs));
1386 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ,
1389 dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
1390 dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
1391 FUID_SIZE_ESTIMATE(zfsvfs));
1394 dmu_tx_hold_bonus(tx, dzp->z_id);
1395 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1396 if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) {
1397 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1398 0, SPA_MAXBLOCKSIZE);
1400 error = dmu_tx_assign(tx, zfsvfs->z_assign);
1402 zfs_dirent_unlock(dl);
1403 if (error == ERESTART &&
1404 zfsvfs->z_assign == TXG_NOWAIT) {
1415 zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
1416 (void) zfs_link_create(dl, zp, tx, ZNEW);
1417 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1418 if (flag & FIGNORECASE)
1420 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1423 zfs_fuid_info_free(fuidp);
1426 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1429 * A directory entry already exists for this name.
1432 * Can't truncate an existing file if in exclusive mode.
1439 * Can't open a directory for writing.
1441 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1446 * Verify requested access to file.
1448 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1452 mutex_enter(&dzp->z_lock);
1454 mutex_exit(&dzp->z_lock);
1457 * Truncate regular files if requested.
1459 if ((ZTOV(zp)->v_type == VREG) &&
1460 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1461 /* we can't hold any locks when calling zfs_freesp() */
1462 zfs_dirent_unlock(dl);
1464 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1466 vnevent_create(ZTOV(zp), ct);
1472 zfs_dirent_unlock(dl);
1480 * If vnode is for a device return a specfs vnode instead.
1482 if (IS_DEVVP(*vpp)) {
1485 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1501 * Remove an entry from a directory.
1503 * IN: dvp - vnode of directory to remove entry from.
1504 * name - name of entry to remove.
1505 * cr - credentials of caller.
1506 * ct - caller context
1507 * flags - case flags
1509 * RETURN: 0 if success
1510 * error code if failure
1514 * vp - ctime (if nlink > 0)
1518 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1521 znode_t *zp, *dzp = VTOZ(dvp);
1522 znode_t *xzp = NULL;
1524 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1526 uint64_t acl_obj, xattr_obj;
1529 boolean_t may_delete_now, delete_now = FALSE;
1530 boolean_t unlinked, toobig = FALSE;
1532 pathname_t *realnmp = NULL;
1539 zilog = zfsvfs->z_log;
1541 if (flags & FIGNORECASE) {
1549 * Attempt to lock directory; fail if entry doesn't exist.
1551 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1561 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1566 * Need to use rmdir for removing directories.
1568 if (vp->v_type == VDIR) {
1573 vnevent_remove(vp, dvp, name, ct);
1576 dnlc_remove(dvp, realnmp->pn_buf);
1578 dnlc_remove(dvp, name);
1580 may_delete_now = FALSE;
1583 * We may delete the znode now, or we may put it in the unlinked set;
1584 * it depends on whether we're the last link, and on whether there are
1585 * other holds on the vnode. So we dmu_tx_hold() the right things to
1586 * allow for either case.
1588 tx = dmu_tx_create(zfsvfs->z_os);
1589 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1590 dmu_tx_hold_bonus(tx, zp->z_id);
1591 if (may_delete_now) {
1593 zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1594 /* if the file is too big, only hold_free a token amount */
1595 dmu_tx_hold_free(tx, zp->z_id, 0,
1596 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1599 /* are there any extended attributes? */
1600 if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1601 /* XXX - do we need this if we are deleting? */
1602 dmu_tx_hold_bonus(tx, xattr_obj);
1605 /* are there any additional acls */
1606 if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1608 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1610 /* charge as an update -- would be nice not to charge at all */
1611 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1613 error = dmu_tx_assign(tx, zfsvfs->z_assign);
1615 zfs_dirent_unlock(dl);
1617 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1630 * Remove the directory entry.
1632 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1639 if (0 && unlinked) {
1641 delete_now = may_delete_now && !toobig &&
1642 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1643 zp->z_phys->zp_xattr == xattr_obj &&
1644 zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1649 if (zp->z_phys->zp_xattr) {
1650 error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1651 ASSERT3U(error, ==, 0);
1652 ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1653 dmu_buf_will_dirty(xzp->z_dbuf, tx);
1654 mutex_enter(&xzp->z_lock);
1655 xzp->z_unlinked = 1;
1656 xzp->z_phys->zp_links = 0;
1657 mutex_exit(&xzp->z_lock);
1658 zfs_unlinked_add(xzp, tx);
1659 zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1661 mutex_enter(&zp->z_lock);
1664 ASSERT3U(vp->v_count, ==, 0);
1666 mutex_exit(&zp->z_lock);
1667 zfs_znode_delete(zp, tx);
1668 } else if (unlinked) {
1669 zfs_unlinked_add(zp, tx);
1673 if (flags & FIGNORECASE)
1675 zfs_log_remove(zilog, tx, txtype, dzp, name);
1682 zfs_dirent_unlock(dl);
1687 /* this rele is delayed to prevent nesting transactions */
1696 * Create a new directory and insert it into dvp using the name
1697 * provided. Return a pointer to the inserted directory.
1699 * IN: dvp - vnode of directory to add subdir to.
1700 * dirname - name of new directory.
1701 * vap - attributes of new directory.
1702 * cr - credentials of caller.
1703 * ct - caller context
1704 * vsecp - ACL to be set
1706 * OUT: vpp - vnode of created directory.
1708 * RETURN: 0 if success
1709 * error code if failure
1712 * dvp - ctime|mtime updated
1713 * vp - ctime|mtime|atime updated
1717 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1718 caller_context_t *ct, int flags, vsecattr_t *vsecp)
1720 znode_t *zp, *dzp = VTOZ(dvp);
1721 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1727 zfs_acl_t *aclp = NULL;
1728 zfs_fuid_info_t *fuidp = NULL;
1731 ASSERT(vap->va_type == VDIR);
1734 * If we have an ephemeral id, ACL, or XVATTR then
1735 * make sure file system is at proper version
1738 if (zfsvfs->z_use_fuids == B_FALSE &&
1739 (vsecp || (vap->va_mask & AT_XVATTR) || IS_EPHEMERAL(crgetuid(cr))||
1740 IS_EPHEMERAL(crgetgid(cr))))
1745 zilog = zfsvfs->z_log;
1747 if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1752 if (zfsvfs->z_utf8 && u8_validate(dirname,
1753 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1757 if (flags & FIGNORECASE)
1760 if (vap->va_mask & AT_XVATTR)
1761 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1762 crgetuid(cr), cr, vap->va_type)) != 0) {
1768 * First make sure the new directory doesn't exist.
1773 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1779 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1780 zfs_dirent_unlock(dl);
1785 if (vsecp && aclp == NULL) {
1786 error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
1788 zfs_dirent_unlock(dl);
1794 * Add a new entry to the directory.
1796 tx = dmu_tx_create(zfsvfs->z_os);
1797 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1798 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1799 if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(crgetuid(cr)) ||
1800 IS_EPHEMERAL(crgetgid(cr))) {
1801 if (zfsvfs->z_fuid_obj == 0) {
1802 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1803 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1804 FUID_SIZE_ESTIMATE(zfsvfs));
1805 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
1807 dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
1808 dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
1809 FUID_SIZE_ESTIMATE(zfsvfs));
1812 if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp)
1813 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1814 0, SPA_MAXBLOCKSIZE);
1815 error = dmu_tx_assign(tx, zfsvfs->z_assign);
1817 zfs_dirent_unlock(dl);
1818 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1833 zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
1839 * Now put new name in parent dir.
1841 (void) zfs_link_create(dl, zp, tx, ZNEW);
1845 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1846 if (flags & FIGNORECASE)
1848 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, fuidp, vap);
1851 zfs_fuid_info_free(fuidp);
1854 zfs_dirent_unlock(dl);
1861 * Remove a directory subdir entry. If the current working
1862 * directory is the same as the subdir to be removed, the
1865 * IN: dvp - vnode of directory to remove from.
1866 * name - name of directory to be removed.
1867 * cwd - vnode of current working directory.
1868 * cr - credentials of caller.
1869 * ct - caller context
1870 * flags - case flags
1872 * RETURN: 0 if success
1873 * error code if failure
1876 * dvp - ctime|mtime updated
1880 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
1881 caller_context_t *ct, int flags)
1883 znode_t *dzp = VTOZ(dvp);
1886 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1895 zilog = zfsvfs->z_log;
1897 if (flags & FIGNORECASE)
1903 * Attempt to lock directory; fail if entry doesn't exist.
1905 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1913 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1917 if (vp->v_type != VDIR) {
1927 vnevent_rmdir(vp, dvp, name, ct);
1930 * Grab a lock on the directory to make sure that noone is
1931 * trying to add (or lookup) entries while we are removing it.
1933 rw_enter(&zp->z_name_lock, RW_WRITER);
1936 * Grab a lock on the parent pointer to make sure we play well
1937 * with the treewalk and directory rename code.
1939 rw_enter(&zp->z_parent_lock, RW_WRITER);
1941 tx = dmu_tx_create(zfsvfs->z_os);
1942 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1943 dmu_tx_hold_bonus(tx, zp->z_id);
1944 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1945 error = dmu_tx_assign(tx, zfsvfs->z_assign);
1947 rw_exit(&zp->z_parent_lock);
1948 rw_exit(&zp->z_name_lock);
1949 zfs_dirent_unlock(dl);
1951 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1961 #ifdef FREEBSD_NAMECACHE
1965 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1968 uint64_t txtype = TX_RMDIR;
1969 if (flags & FIGNORECASE)
1971 zfs_log_remove(zilog, tx, txtype, dzp, name);
1976 rw_exit(&zp->z_parent_lock);
1977 rw_exit(&zp->z_name_lock);
1978 #ifdef FREEBSD_NAMECACHE
1982 zfs_dirent_unlock(dl);
1991 * Read as many directory entries as will fit into the provided
1992 * buffer from the given directory cursor position (specified in
1993 * the uio structure.
1995 * IN: vp - vnode of directory to read.
1996 * uio - structure supplying read location, range info,
1997 * and return buffer.
1998 * cr - credentials of caller.
1999 * ct - caller context
2000 * flags - case flags
2002 * OUT: uio - updated offset and range, buffer filled.
2003 * eofp - set to true if end-of-file detected.
2005 * RETURN: 0 if success
2006 * error code if failure
2009 * vp - atime updated
2011 * Note that the low 4 bits of the cookie returned by zap is always zero.
2012 * This allows us to use the low range for "special" directory entries:
2013 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2014 * we use the offset 2 for the '.zfs' directory.
2018 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2020 znode_t *zp = VTOZ(vp);
2024 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2029 zap_attribute_t zap;
2030 uint_t bytes_wanted;
2031 uint64_t offset; /* must be unsigned; checks for < 1 */
2036 boolean_t check_sysattrs;
2039 u_long *cooks = NULL;
2046 * If we are not given an eof variable,
2053 * Check for valid iov_len.
2055 if (uio->uio_iov->iov_len <= 0) {
2061 * Quit if directory has been removed (posix)
2063 if ((*eofp = zp->z_unlinked) != 0) {
2070 offset = uio->uio_loffset;
2071 prefetch = zp->z_zn_prefetch;
2074 * Initialize the iterator cursor.
2078 * Start iteration from the beginning of the directory.
2080 zap_cursor_init(&zc, os, zp->z_id);
2083 * The offset is a serialized cursor.
2085 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2089 * Get space to change directory entries into fs independent format.
2091 iovp = uio->uio_iov;
2092 bytes_wanted = iovp->iov_len;
2093 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2094 bufsize = bytes_wanted;
2095 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2096 odp = (struct dirent64 *)outbuf;
2098 bufsize = bytes_wanted;
2099 odp = (struct dirent64 *)iovp->iov_base;
2101 eodp = (struct edirent *)odp;
2103 if (ncookies != NULL) {
2105 * Minimum entry size is dirent size and 1 byte for a file name.
2107 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2108 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2113 * If this VFS supports the system attribute view interface; and
2114 * we're looking at an extended attribute directory; and we care
2115 * about normalization conflicts on this vfs; then we must check
2116 * for normalization conflicts with the sysattr name space.
2119 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2120 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2121 (flags & V_RDDIR_ENTFLAGS);
2127 * Transform to file-system independent format
2130 while (outcount < bytes_wanted) {
2136 * Special case `.', `..', and `.zfs'.
2139 (void) strcpy(zap.za_name, ".");
2140 zap.za_normalization_conflict = 0;
2143 } else if (offset == 1) {
2144 (void) strcpy(zap.za_name, "..");
2145 zap.za_normalization_conflict = 0;
2146 objnum = zp->z_phys->zp_parent;
2148 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2149 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2150 zap.za_normalization_conflict = 0;
2151 objnum = ZFSCTL_INO_ROOT;
2157 if (error = zap_cursor_retrieve(&zc, &zap)) {
2158 if ((*eofp = (error == ENOENT)) != 0)
2164 if (zap.za_integer_length != 8 ||
2165 zap.za_num_integers != 1) {
2166 cmn_err(CE_WARN, "zap_readdir: bad directory "
2167 "entry, obj = %lld, offset = %lld\n",
2168 (u_longlong_t)zp->z_id,
2169 (u_longlong_t)offset);
2174 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2176 * MacOS X can extract the object type here such as:
2177 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2179 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2181 if (check_sysattrs && !zap.za_normalization_conflict) {
2183 zap.za_normalization_conflict =
2184 xattr_sysattr_casechk(zap.za_name);
2186 panic("%s:%u: TODO", __func__, __LINE__);
2191 if (flags & V_RDDIR_ENTFLAGS)
2192 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2194 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2197 * Will this entry fit in the buffer?
2199 if (outcount + reclen > bufsize) {
2201 * Did we manage to fit anything in the buffer?
2209 if (flags & V_RDDIR_ENTFLAGS) {
2211 * Add extended flag entry:
2213 eodp->ed_ino = objnum;
2214 eodp->ed_reclen = reclen;
2215 /* NOTE: ed_off is the offset for the *next* entry */
2216 next = &(eodp->ed_off);
2217 eodp->ed_eflags = zap.za_normalization_conflict ?
2218 ED_CASE_CONFLICT : 0;
2219 (void) strncpy(eodp->ed_name, zap.za_name,
2220 EDIRENT_NAMELEN(reclen));
2221 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2226 odp->d_ino = objnum;
2227 odp->d_reclen = reclen;
2228 odp->d_namlen = strlen(zap.za_name);
2229 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2231 odp = (dirent64_t *)((intptr_t)odp + reclen);
2235 ASSERT(outcount <= bufsize);
2237 /* Prefetch znode */
2239 dmu_prefetch(os, objnum, 0, 0);
2242 * Move to the next entry, fill in the previous offset.
2244 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2245 zap_cursor_advance(&zc);
2246 offset = zap_cursor_serialize(&zc);
2251 if (cooks != NULL) {
2254 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2257 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2259 /* Subtract unused cookies */
2260 if (ncookies != NULL)
2261 *ncookies -= ncooks;
2263 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2264 iovp->iov_base += outcount;
2265 iovp->iov_len -= outcount;
2266 uio->uio_resid -= outcount;
2267 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2269 * Reset the pointer.
2271 offset = uio->uio_loffset;
2275 zap_cursor_fini(&zc);
2276 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2277 kmem_free(outbuf, bufsize);
2279 if (error == ENOENT)
2282 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2284 uio->uio_loffset = offset;
2286 if (error != 0 && cookies != NULL) {
2287 free(*cookies, M_TEMP);
2294 ulong_t zfs_fsync_sync_cnt = 4;
2297 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2299 znode_t *zp = VTOZ(vp);
2300 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2302 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2306 zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2313 * Get the requested file attributes and place them in the provided
2316 * IN: vp - vnode of file.
2317 * vap - va_mask identifies requested attributes.
2318 * If AT_XVATTR set, then optional attrs are requested
2319 * flags - ATTR_NOACLCHECK (CIFS server context)
2320 * cr - credentials of caller.
2321 * ct - caller context
2323 * OUT: vap - attribute values.
2325 * RETURN: 0 (always succeeds)
2329 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2330 caller_context_t *ct)
2332 znode_t *zp = VTOZ(vp);
2333 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2337 u_longlong_t nblocks;
2339 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2340 xoptattr_t *xoap = NULL;
2341 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2347 mutex_enter(&zp->z_lock);
2350 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2351 * Also, if we are the owner don't bother, since owner should
2352 * always be allowed to read basic attributes of file.
2354 if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
2355 (pzp->zp_uid != crgetuid(cr))) {
2356 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2358 mutex_exit(&zp->z_lock);
2365 * Return all attributes. It's cheaper to provide the answer
2366 * than to determine whether we were asked the question.
2369 vap->va_type = IFTOVT(pzp->zp_mode);
2370 vap->va_mode = pzp->zp_mode & ~S_IFMT;
2371 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2372 // vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2373 vap->va_nodeid = zp->z_id;
2374 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2375 links = pzp->zp_links + 1;
2377 links = pzp->zp_links;
2378 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2379 vap->va_size = pzp->zp_size;
2380 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2381 vap->va_rdev = zfs_cmpldev(pzp->zp_rdev);
2382 vap->va_seq = zp->z_seq;
2383 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2386 * Add in any requested optional attributes and the create time.
2387 * Also set the corresponding bits in the returned attribute bitmap.
2389 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2390 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2392 ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
2393 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2396 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2397 xoap->xoa_readonly =
2398 ((pzp->zp_flags & ZFS_READONLY) != 0);
2399 XVA_SET_RTN(xvap, XAT_READONLY);
2402 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2404 ((pzp->zp_flags & ZFS_SYSTEM) != 0);
2405 XVA_SET_RTN(xvap, XAT_SYSTEM);
2408 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2410 ((pzp->zp_flags & ZFS_HIDDEN) != 0);
2411 XVA_SET_RTN(xvap, XAT_HIDDEN);
2414 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2415 xoap->xoa_nounlink =
2416 ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
2417 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2420 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2421 xoap->xoa_immutable =
2422 ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
2423 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2426 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2427 xoap->xoa_appendonly =
2428 ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
2429 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2432 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2434 ((pzp->zp_flags & ZFS_NODUMP) != 0);
2435 XVA_SET_RTN(xvap, XAT_NODUMP);
2438 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2440 ((pzp->zp_flags & ZFS_OPAQUE) != 0);
2441 XVA_SET_RTN(xvap, XAT_OPAQUE);
2444 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2445 xoap->xoa_av_quarantined =
2446 ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
2447 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2450 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2451 xoap->xoa_av_modified =
2452 ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
2453 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2456 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2457 vp->v_type == VREG &&
2458 (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
2460 dmu_object_info_t doi;
2463 * Only VREG files have anti-virus scanstamps, so we
2464 * won't conflict with symlinks in the bonus buffer.
2466 dmu_object_info_from_db(zp->z_dbuf, &doi);
2467 len = sizeof (xoap->xoa_av_scanstamp) +
2468 sizeof (znode_phys_t);
2469 if (len <= doi.doi_bonus_size) {
2471 * pzp points to the start of the
2472 * znode_phys_t. pzp + 1 points to the
2473 * first byte after the znode_phys_t.
2475 (void) memcpy(xoap->xoa_av_scanstamp,
2477 sizeof (xoap->xoa_av_scanstamp));
2478 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
2482 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2483 ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
2484 XVA_SET_RTN(xvap, XAT_CREATETIME);
2488 ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
2489 ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
2490 ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
2491 ZFS_TIME_DECODE(&vap->va_birthtime, pzp->zp_crtime);
2493 mutex_exit(&zp->z_lock);
2495 dmu_object_size_from_db(zp->z_dbuf, &blksize, &nblocks);
2496 vap->va_blksize = blksize;
2497 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2499 if (zp->z_blksz == 0) {
2501 * Block size hasn't been set; suggest maximal I/O transfers.
2503 vap->va_blksize = zfsvfs->z_max_blksz;
2511 * Set the file attributes to the values contained in the
2514 * IN: vp - vnode of file to be modified.
2515 * vap - new attribute values.
2516 * If AT_XVATTR set, then optional attrs are being set
2517 * flags - ATTR_UTIME set if non-default time values provided.
2518 * - ATTR_NOACLCHECK (CIFS context only).
2519 * cr - credentials of caller.
2520 * ct - caller context
2522 * RETURN: 0 if success
2523 * error code if failure
2526 * vp - ctime updated, mtime updated if size changed.
2530 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2531 caller_context_t *ct)
2533 znode_t *zp = VTOZ(vp);
2535 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2539 uint_t mask = vap->va_mask;
2541 uint64_t saved_mode;
2545 int need_policy = FALSE;
2547 zfs_fuid_info_t *fuidp = NULL;
2548 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2550 zfs_acl_t *aclp = NULL;
2551 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2556 if (mask & AT_NOSET)
2563 zilog = zfsvfs->z_log;
2566 * Make sure that if we have ephemeral uid/gid or xvattr specified
2567 * that file system is at proper version level
2570 if (zfsvfs->z_use_fuids == B_FALSE &&
2571 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2572 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2573 (mask & AT_XVATTR))) {
2578 if (mask & AT_SIZE && vp->v_type == VDIR) {
2583 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2589 * If this is an xvattr_t, then get a pointer to the structure of
2590 * optional attributes. If this is NULL, then we have a vattr_t.
2592 xoap = xva_getxoptattr(xvap);
2595 * Immutable files can only alter immutable bit and atime
2597 if ((pzp->zp_flags & ZFS_IMMUTABLE) &&
2598 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2599 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2604 if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) {
2610 * Verify timestamps doesn't overflow 32 bits.
2611 * ZFS can handle large timestamps, but 32bit syscalls can't
2612 * handle times greater than 2039. This check should be removed
2613 * once large timestamps are fully supported.
2615 if (mask & (AT_ATIME | AT_MTIME)) {
2616 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2617 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2626 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2632 * First validate permissions
2635 if (mask & AT_SIZE) {
2636 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2642 * XXX - Note, we are not providing any open
2643 * mode flags here (like FNDELAY), so we may
2644 * block if there are locks present... this
2645 * should be addressed in openat().
2647 /* XXX - would it be OK to generate a log record here? */
2648 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2655 if (mask & (AT_ATIME|AT_MTIME) ||
2656 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2657 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2658 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2659 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2660 XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
2661 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2664 if (mask & (AT_UID|AT_GID)) {
2665 int idmask = (mask & (AT_UID|AT_GID));
2670 * NOTE: even if a new mode is being set,
2671 * we may clear S_ISUID/S_ISGID bits.
2674 if (!(mask & AT_MODE))
2675 vap->va_mode = pzp->zp_mode;
2678 * Take ownership or chgrp to group we are a member of
2681 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2682 take_group = (mask & AT_GID) &&
2683 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2686 * If both AT_UID and AT_GID are set then take_owner and
2687 * take_group must both be set in order to allow taking
2690 * Otherwise, send the check through secpolicy_vnode_setattr()
2694 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2695 ((idmask == AT_UID) && take_owner) ||
2696 ((idmask == AT_GID) && take_group)) {
2697 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2698 skipaclchk, cr) == 0) {
2700 * Remove setuid/setgid for non-privileged users
2702 secpolicy_setid_clear(vap, vp, cr);
2703 trim_mask = (mask & (AT_UID|AT_GID));
2712 mutex_enter(&zp->z_lock);
2713 oldva.va_mode = pzp->zp_mode;
2714 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2715 if (mask & AT_XVATTR) {
2716 if ((need_policy == FALSE) &&
2717 (XVA_ISSET_REQ(xvap, XAT_APPENDONLY) &&
2718 xoap->xoa_appendonly !=
2719 ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) ||
2720 (XVA_ISSET_REQ(xvap, XAT_NOUNLINK) &&
2721 xoap->xoa_nounlink !=
2722 ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) ||
2723 (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE) &&
2724 xoap->xoa_immutable !=
2725 ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) ||
2726 (XVA_ISSET_REQ(xvap, XAT_NODUMP) &&
2728 ((pzp->zp_flags & ZFS_NODUMP) != 0)) ||
2729 (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED) &&
2730 xoap->xoa_av_modified !=
2731 ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) ||
2732 ((XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED) &&
2733 ((vp->v_type != VREG && xoap->xoa_av_quarantined) ||
2734 xoap->xoa_av_quarantined !=
2735 ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)))) ||
2736 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
2737 (XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2742 mutex_exit(&zp->z_lock);
2744 if (mask & AT_MODE) {
2745 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2746 err = secpolicy_setid_setsticky_clear(vp, vap,
2752 trim_mask |= AT_MODE;
2760 * If trim_mask is set then take ownership
2761 * has been granted or write_acl is present and user
2762 * has the ability to modify mode. In that case remove
2763 * UID|GID and or MODE from mask so that
2764 * secpolicy_vnode_setattr() doesn't revoke it.
2768 saved_mask = vap->va_mask;
2769 vap->va_mask &= ~trim_mask;
2770 if (trim_mask & AT_MODE) {
2772 * Save the mode, as secpolicy_vnode_setattr()
2773 * will overwrite it with ova.va_mode.
2775 saved_mode = vap->va_mode;
2778 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2779 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2786 vap->va_mask |= saved_mask;
2787 if (trim_mask & AT_MODE) {
2789 * Recover the mode after
2790 * secpolicy_vnode_setattr().
2792 vap->va_mode = saved_mode;
2798 * secpolicy_vnode_setattr, or take ownership may have
2801 mask = vap->va_mask;
2803 tx = dmu_tx_create(zfsvfs->z_os);
2804 dmu_tx_hold_bonus(tx, zp->z_id);
2805 if (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2806 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid))) {
2807 if (zfsvfs->z_fuid_obj == 0) {
2808 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
2809 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2810 FUID_SIZE_ESTIMATE(zfsvfs));
2811 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
2813 dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
2814 dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
2815 FUID_SIZE_ESTIMATE(zfsvfs));
2819 if (mask & AT_MODE) {
2820 uint64_t pmode = pzp->zp_mode;
2822 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2824 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) {
2829 if (pzp->zp_acl.z_acl_extern_obj) {
2830 /* Are we upgrading ACL from old V0 format to new V1 */
2831 if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
2832 pzp->zp_acl.z_acl_version ==
2833 ZFS_ACL_VERSION_INITIAL) {
2834 dmu_tx_hold_free(tx,
2835 pzp->zp_acl.z_acl_extern_obj, 0,
2837 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2838 0, aclp->z_acl_bytes);
2840 dmu_tx_hold_write(tx,
2841 pzp->zp_acl.z_acl_extern_obj, 0,
2844 } else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2845 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2846 0, aclp->z_acl_bytes);
2850 if ((mask & (AT_UID | AT_GID)) && pzp->zp_xattr != 0) {
2851 err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
2859 dmu_tx_hold_bonus(tx, attrzp->z_id);
2862 err = dmu_tx_assign(tx, zfsvfs->z_assign);
2865 VN_RELE(ZTOV(attrzp));
2872 if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2882 dmu_buf_will_dirty(zp->z_dbuf, tx);
2885 * Set each attribute requested.
2886 * We group settings according to the locks they need to acquire.
2888 * Note: you cannot set ctime directly, although it will be
2889 * updated as a side-effect of calling this function.
2892 mutex_enter(&zp->z_lock);
2894 if (mask & AT_MODE) {
2895 mutex_enter(&zp->z_acl_lock);
2896 zp->z_phys->zp_mode = new_mode;
2897 err = zfs_aclset_common(zp, aclp, cr, &fuidp, tx);
2898 ASSERT3U(err, ==, 0);
2899 mutex_exit(&zp->z_acl_lock);
2903 mutex_enter(&attrzp->z_lock);
2905 if (mask & AT_UID) {
2906 pzp->zp_uid = zfs_fuid_create(zfsvfs,
2907 vap->va_uid, cr, ZFS_OWNER, tx, &fuidp);
2909 attrzp->z_phys->zp_uid = zfs_fuid_create(zfsvfs,
2910 vap->va_uid, cr, ZFS_OWNER, tx, &fuidp);
2914 if (mask & AT_GID) {
2915 pzp->zp_gid = zfs_fuid_create(zfsvfs, vap->va_gid,
2916 cr, ZFS_GROUP, tx, &fuidp);
2918 attrzp->z_phys->zp_gid = zfs_fuid_create(zfsvfs,
2919 vap->va_gid, cr, ZFS_GROUP, tx, &fuidp);
2926 mutex_exit(&attrzp->z_lock);
2928 if (mask & AT_ATIME)
2929 ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2931 if (mask & AT_MTIME)
2932 ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2934 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2936 zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
2938 zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2940 * Do this after setting timestamps to prevent timestamp
2941 * update from toggling bit
2944 if (xoap && (mask & AT_XVATTR)) {
2945 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
2947 dmu_object_info_t doi;
2949 ASSERT(vp->v_type == VREG);
2951 /* Grow the bonus buffer if necessary. */
2952 dmu_object_info_from_db(zp->z_dbuf, &doi);
2953 len = sizeof (xoap->xoa_av_scanstamp) +
2954 sizeof (znode_phys_t);
2955 if (len > doi.doi_bonus_size)
2956 VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
2958 zfs_xvattr_set(zp, xvap);
2962 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2965 zfs_fuid_info_free(fuidp);
2966 mutex_exit(&zp->z_lock);
2969 VN_RELE(ZTOV(attrzp));
2977 typedef struct zfs_zlock {
2978 krwlock_t *zl_rwlock; /* lock we acquired */
2979 znode_t *zl_znode; /* znode we held */
2980 struct zfs_zlock *zl_next; /* next in list */
2984 * Drop locks and release vnodes that were held by zfs_rename_lock().
2987 zfs_rename_unlock(zfs_zlock_t **zlpp)
2991 while ((zl = *zlpp) != NULL) {
2992 if (zl->zl_znode != NULL)
2993 VN_RELE(ZTOV(zl->zl_znode));
2994 rw_exit(zl->zl_rwlock);
2995 *zlpp = zl->zl_next;
2996 kmem_free(zl, sizeof (*zl));
3001 * Search back through the directory tree, using the ".." entries.
3002 * Lock each directory in the chain to prevent concurrent renames.
3003 * Fail any attempt to move a directory into one of its own descendants.
3004 * XXX - z_parent_lock can overlap with map or grow locks
3007 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3011 uint64_t rootid = zp->z_zfsvfs->z_root;
3012 uint64_t *oidp = &zp->z_id;
3013 krwlock_t *rwlp = &szp->z_parent_lock;
3014 krw_t rw = RW_WRITER;
3017 * First pass write-locks szp and compares to zp->z_id.
3018 * Later passes read-lock zp and compare to zp->z_parent.
3021 if (!rw_tryenter(rwlp, rw)) {
3023 * Another thread is renaming in this path.
3024 * Note that if we are a WRITER, we don't have any
3025 * parent_locks held yet.
3027 if (rw == RW_READER && zp->z_id > szp->z_id) {
3029 * Drop our locks and restart
3031 zfs_rename_unlock(&zl);
3035 rwlp = &szp->z_parent_lock;
3040 * Wait for other thread to drop its locks
3046 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3047 zl->zl_rwlock = rwlp;
3048 zl->zl_znode = NULL;
3049 zl->zl_next = *zlpp;
3052 if (*oidp == szp->z_id) /* We're a descendant of szp */
3055 if (*oidp == rootid) /* We've hit the top */
3058 if (rw == RW_READER) { /* i.e. not the first pass */
3059 int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
3064 oidp = &zp->z_phys->zp_parent;
3065 rwlp = &zp->z_parent_lock;
3068 } while (zp->z_id != sdzp->z_id);
3074 * Move an entry from the provided source directory to the target
3075 * directory. Change the entry name as indicated.
3077 * IN: sdvp - Source directory containing the "old entry".
3078 * snm - Old entry name.
3079 * tdvp - Target directory to contain the "new entry".
3080 * tnm - New entry name.
3081 * cr - credentials of caller.
3082 * ct - caller context
3083 * flags - case flags
3085 * RETURN: 0 if success
3086 * error code if failure
3089 * sdvp,tdvp - ctime|mtime updated
3093 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3094 caller_context_t *ct, int flags)
3096 znode_t *tdzp, *szp, *tzp;
3097 znode_t *sdzp = VTOZ(sdvp);
3098 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3101 zfs_dirlock_t *sdl, *tdl;
3104 int cmp, serr, terr;
3109 ZFS_VERIFY_ZP(sdzp);
3110 zilog = zfsvfs->z_log;
3113 * Make sure we have the real vp for the target directory.
3115 if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3118 if (tdvp->v_vfsp != sdvp->v_vfsp) {
3124 ZFS_VERIFY_ZP(tdzp);
3125 if (zfsvfs->z_utf8 && u8_validate(tnm,
3126 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3131 if (flags & FIGNORECASE)
3140 * This is to prevent the creation of links into attribute space
3141 * by renaming a linked file into/outof an attribute directory.
3142 * See the comment in zfs_link() for why this is considered bad.
3144 if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
3145 (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
3151 * Lock source and target directory entries. To prevent deadlock,
3152 * a lock ordering must be defined. We lock the directory with
3153 * the smallest object id first, or if it's a tie, the one with
3154 * the lexically first name.
3156 if (sdzp->z_id < tdzp->z_id) {
3158 } else if (sdzp->z_id > tdzp->z_id) {
3162 * First compare the two name arguments without
3163 * considering any case folding.
3165 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3167 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3168 ASSERT(error == 0 || !zfsvfs->z_utf8);
3171 * POSIX: "If the old argument and the new argument
3172 * both refer to links to the same existing file,
3173 * the rename() function shall return successfully
3174 * and perform no other action."
3180 * If the file system is case-folding, then we may
3181 * have some more checking to do. A case-folding file
3182 * system is either supporting mixed case sensitivity
3183 * access or is completely case-insensitive. Note
3184 * that the file system is always case preserving.
3186 * In mixed sensitivity mode case sensitive behavior
3187 * is the default. FIGNORECASE must be used to
3188 * explicitly request case insensitive behavior.
3190 * If the source and target names provided differ only
3191 * by case (e.g., a request to rename 'tim' to 'Tim'),
3192 * we will treat this as a special case in the
3193 * case-insensitive mode: as long as the source name
3194 * is an exact match, we will allow this to proceed as
3195 * a name-change request.
3197 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3198 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3199 flags & FIGNORECASE)) &&
3200 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3203 * case preserving rename request, require exact
3212 * If the source and destination directories are the same, we should
3213 * grab the z_name_lock of that directory only once.
3217 rw_enter(&sdzp->z_name_lock, RW_READER);
3221 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3222 ZEXISTS | zflg, NULL, NULL);
3223 terr = zfs_dirent_lock(&tdl,
3224 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3226 terr = zfs_dirent_lock(&tdl,
3227 tdzp, tnm, &tzp, zflg, NULL, NULL);
3228 serr = zfs_dirent_lock(&sdl,
3229 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3235 * Source entry invalid or not there.
3238 zfs_dirent_unlock(tdl);
3244 rw_exit(&sdzp->z_name_lock);
3246 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0)
3252 zfs_dirent_unlock(sdl);
3256 rw_exit(&sdzp->z_name_lock);
3258 if (strcmp(tnm, "..") == 0)
3265 * Must have write access at the source to remove the old entry
3266 * and write access at the target to create the new entry.
3267 * Note that if target and source are the same, this can be
3268 * done in a single check.
3271 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3274 if (ZTOV(szp)->v_type == VDIR) {
3276 * Check to make sure rename is valid.
3277 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3279 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3284 * Does target exist?
3288 * Source and target must be the same type.
3290 if (ZTOV(szp)->v_type == VDIR) {
3291 if (ZTOV(tzp)->v_type != VDIR) {
3296 if (ZTOV(tzp)->v_type == VDIR) {
3302 * POSIX dictates that when the source and target
3303 * entries refer to the same file object, rename
3304 * must do nothing and exit without error.
3306 if (szp->z_id == tzp->z_id) {
3312 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3314 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3317 * notify the target directory if it is not the same
3318 * as source directory.
3321 vnevent_rename_dest_dir(tdvp, ct);
3324 tx = dmu_tx_create(zfsvfs->z_os);
3325 dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */
3326 dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */
3327 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3328 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3330 dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */
3332 dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */
3333 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3334 error = dmu_tx_assign(tx, zfsvfs->z_assign);
3337 zfs_rename_unlock(&zl);
3338 zfs_dirent_unlock(sdl);
3339 zfs_dirent_unlock(tdl);
3342 rw_exit(&sdzp->z_name_lock);
3347 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
3357 if (tzp) /* Attempt to remove the existing target */
3358 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3361 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3363 szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
3365 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3368 zfs_log_rename(zilog, tx,
3369 TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
3370 sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
3372 /* Update path information for the target vnode */
3373 vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3375 #ifdef FREEBSD_NAMECACHE
3386 zfs_rename_unlock(&zl);
3388 zfs_dirent_unlock(sdl);
3389 zfs_dirent_unlock(tdl);
3392 rw_exit(&sdzp->z_name_lock);
3404 * Insert the indicated symbolic reference entry into the directory.
3406 * IN: dvp - Directory to contain new symbolic link.
3407 * link - Name for new symlink entry.
3408 * vap - Attributes of new entry.
3409 * target - Target path of new symlink.
3410 * cr - credentials of caller.
3411 * ct - caller context
3412 * flags - case flags
3414 * RETURN: 0 if success
3415 * error code if failure
3418 * dvp - ctime|mtime updated
3422 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
3423 cred_t *cr, kthread_t *td)
3425 znode_t *zp, *dzp = VTOZ(dvp);
3428 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3430 int len = strlen(link);
3433 zfs_fuid_info_t *fuidp = NULL;
3436 ASSERT(vap->va_type == VLNK);
3440 zilog = zfsvfs->z_log;
3442 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3443 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3447 if (flags & FIGNORECASE)
3450 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3455 if (len > MAXPATHLEN) {
3457 return (ENAMETOOLONG);
3461 * Attempt to lock directory; fail if entry already exists.
3463 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3469 tx = dmu_tx_create(zfsvfs->z_os);
3470 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3471 dmu_tx_hold_bonus(tx, dzp->z_id);
3472 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3473 if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
3474 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
3475 if (IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))) {
3476 if (zfsvfs->z_fuid_obj == 0) {
3477 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
3478 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3479 FUID_SIZE_ESTIMATE(zfsvfs));
3480 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
3482 dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
3483 dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
3484 FUID_SIZE_ESTIMATE(zfsvfs));
3487 error = dmu_tx_assign(tx, zfsvfs->z_assign);
3489 zfs_dirent_unlock(dl);
3490 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
3500 dmu_buf_will_dirty(dzp->z_dbuf, tx);
3503 * Create a new object for the symlink.
3504 * Put the link content into bonus buffer if it will fit;
3505 * otherwise, store it just like any other file data.
3507 if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
3508 zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, NULL, &fuidp);
3510 bcopy(link, zp->z_phys + 1, len);
3514 zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, NULL, &fuidp);
3516 * Nothing can access the znode yet so no locking needed
3517 * for growing the znode's blocksize.
3519 zfs_grow_blocksize(zp, len, tx);
3521 VERIFY(0 == dmu_buf_hold(zfsvfs->z_os,
3522 zp->z_id, 0, FTAG, &dbp));
3523 dmu_buf_will_dirty(dbp, tx);
3525 ASSERT3U(len, <=, dbp->db_size);
3526 bcopy(link, dbp->db_data, len);
3527 dmu_buf_rele(dbp, FTAG);
3529 zp->z_phys->zp_size = len;
3532 * Insert the new object into the directory.
3534 (void) zfs_link_create(dl, zp, tx, ZNEW);
3537 uint64_t txtype = TX_SYMLINK;
3538 if (flags & FIGNORECASE)
3540 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3544 zfs_fuid_info_free(fuidp);
3548 zfs_dirent_unlock(dl);
3555 * Return, in the buffer contained in the provided uio structure,
3556 * the symbolic path referred to by vp.
3558 * IN: vp - vnode of symbolic link.
3559 * uoip - structure to contain the link path.
3560 * cr - credentials of caller.
3561 * ct - caller context
3563 * OUT: uio - structure to contain the link path.
3565 * RETURN: 0 if success
3566 * error code if failure
3569 * vp - atime updated
3573 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3575 znode_t *zp = VTOZ(vp);
3576 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3583 bufsz = (size_t)zp->z_phys->zp_size;
3584 if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
3585 error = uiomove(zp->z_phys + 1,
3586 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3589 error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
3594 error = uiomove(dbp->db_data,
3595 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3596 dmu_buf_rele(dbp, FTAG);
3599 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3605 * Insert a new entry into directory tdvp referencing svp.
3607 * IN: tdvp - Directory to contain new entry.
3608 * svp - vnode of new entry.
3609 * name - name of new entry.
3610 * cr - credentials of caller.
3611 * ct - caller context
3613 * RETURN: 0 if success
3614 * error code if failure
3617 * tdvp - ctime|mtime updated
3618 * svp - ctime updated
3622 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
3623 caller_context_t *ct, int flags)
3625 znode_t *dzp = VTOZ(tdvp);
3627 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3636 ASSERT(tdvp->v_type == VDIR);
3640 zilog = zfsvfs->z_log;
3642 if (VOP_REALVP(svp, &realvp, ct) == 0)
3645 if (svp->v_vfsp != tdvp->v_vfsp) {
3652 if (zfsvfs->z_utf8 && u8_validate(name,
3653 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3657 if (flags & FIGNORECASE)
3662 * We do not support links between attributes and non-attributes
3663 * because of the potential security risk of creating links
3664 * into "normal" file space in order to circumvent restrictions
3665 * imposed in attribute space.
3667 if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
3668 (dzp->z_phys->zp_flags & ZFS_XATTR)) {
3674 * POSIX dictates that we return EPERM here.
3675 * Better choices include ENOTSUP or EISDIR.
3677 if (svp->v_type == VDIR) {
3682 owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER);
3683 if (owner != crgetuid(cr) &&
3684 secpolicy_basic_link(svp, cr) != 0) {
3689 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3695 * Attempt to lock directory; fail if entry already exists.
3697 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3703 tx = dmu_tx_create(zfsvfs->z_os);
3704 dmu_tx_hold_bonus(tx, szp->z_id);
3705 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3706 error = dmu_tx_assign(tx, zfsvfs->z_assign);
3708 zfs_dirent_unlock(dl);
3709 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
3719 error = zfs_link_create(dl, szp, tx, 0);
3722 uint64_t txtype = TX_LINK;
3723 if (flags & FIGNORECASE)
3725 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3730 zfs_dirent_unlock(dl);
3733 vnevent_link(svp, ct);
3742 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3744 znode_t *zp = VTOZ(vp);
3745 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3748 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
3749 if (zp->z_dbuf == NULL) {
3751 * The fs has been unmounted, or we did a
3752 * suspend/resume and this file no longer exists.
3755 vp->v_count = 0; /* count arrives as 1 */
3757 vrecycle(vp, curthread);
3758 rw_exit(&zfsvfs->z_teardown_inactive_lock);
3762 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3763 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3765 dmu_tx_hold_bonus(tx, zp->z_id);
3766 error = dmu_tx_assign(tx, TXG_WAIT);
3770 dmu_buf_will_dirty(zp->z_dbuf, tx);
3771 mutex_enter(&zp->z_lock);
3772 zp->z_atime_dirty = 0;
3773 mutex_exit(&zp->z_lock);
3779 rw_exit(&zfsvfs->z_teardown_inactive_lock);
3782 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
3783 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
3787 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3789 znode_t *zp = VTOZ(vp);
3790 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3792 uint64_t object = zp->z_id;
3798 gen = (uint32_t)zp->z_gen;
3800 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3801 fidp->fid_len = size;
3803 zfid = (zfid_short_t *)fidp;
3805 zfid->zf_len = size;
3807 for (i = 0; i < sizeof (zfid->zf_object); i++)
3808 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3810 /* Must have a non-zero generation number to distinguish from .zfs */
3813 for (i = 0; i < sizeof (zfid->zf_gen); i++)
3814 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3816 if (size == LONG_FID_LEN) {
3817 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
3820 zlfid = (zfid_long_t *)fidp;
3822 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3823 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3825 /* XXX - this should be the generation number for the objset */
3826 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3827 zlfid->zf_setgen[i] = 0;
3835 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
3836 caller_context_t *ct)
3848 case _PC_FILESIZEBITS:
3853 case _PC_XATTR_EXISTS:
3855 zfsvfs = zp->z_zfsvfs;
3859 error = zfs_dirent_lock(&dl, zp, "", &xzp,
3860 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
3862 zfs_dirent_unlock(dl);
3863 if (!zfs_dirempty(xzp))
3866 } else if (error == ENOENT) {
3868 * If there aren't extended attributes, it's the
3869 * same as having zero of them.
3877 case _PC_ACL_EXTENDED:
3885 case _PC_ACL_PATH_MAX:
3886 *valp = ACL_MAX_ENTRIES;
3889 case _PC_MIN_HOLE_SIZE:
3890 *valp = (int)SPA_MINBLOCKSIZE;
3894 return (EOPNOTSUPP);
3900 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
3901 caller_context_t *ct)
3903 znode_t *zp = VTOZ(vp);
3904 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3906 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
3910 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
3918 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
3919 caller_context_t *ct)
3921 znode_t *zp = VTOZ(vp);
3922 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3924 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
3928 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
3934 zfs_freebsd_open(ap)
3935 struct vop_open_args /* {
3938 struct ucred *a_cred;
3939 struct thread *a_td;
3942 vnode_t *vp = ap->a_vp;
3943 znode_t *zp = VTOZ(vp);
3946 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
3948 vnode_create_vobject(vp, zp->z_phys->zp_size, ap->a_td);
3953 zfs_freebsd_close(ap)
3954 struct vop_close_args /* {
3957 struct ucred *a_cred;
3958 struct thread *a_td;
3962 return (zfs_close(ap->a_vp, ap->a_fflag, 0, 0, ap->a_cred, NULL));
3966 zfs_freebsd_ioctl(ap)
3967 struct vop_ioctl_args /* {
3977 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
3978 ap->a_fflag, ap->a_cred, NULL, NULL));
3982 zfs_freebsd_read(ap)
3983 struct vop_read_args /* {
3987 struct ucred *a_cred;
3991 return (zfs_read(ap->a_vp, ap->a_uio, ap->a_ioflag, ap->a_cred, NULL));
3995 zfs_freebsd_write(ap)
3996 struct vop_write_args /* {
4000 struct ucred *a_cred;
4004 return (zfs_write(ap->a_vp, ap->a_uio, ap->a_ioflag, ap->a_cred, NULL));
4008 zfs_freebsd_access(ap)
4009 struct vop_access_args /* {
4011 accmode_t a_accmode;
4012 struct ucred *a_cred;
4013 struct thread *a_td;
4020 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4022 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4024 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
4027 * VADMIN has to be handled by vaccess().
4030 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4032 vnode_t *vp = ap->a_vp;
4033 znode_t *zp = VTOZ(vp);
4034 znode_phys_t *zphys = zp->z_phys;
4036 error = vaccess(vp->v_type, zphys->zp_mode,
4037 zphys->zp_uid, zphys->zp_gid, accmode, ap->a_cred,
4046 zfs_freebsd_lookup(ap)
4047 struct vop_lookup_args /* {
4048 struct vnode *a_dvp;
4049 struct vnode **a_vpp;
4050 struct componentname *a_cnp;
4053 struct componentname *cnp = ap->a_cnp;
4054 char nm[NAME_MAX + 1];
4056 ASSERT(cnp->cn_namelen < sizeof(nm));
4057 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
4059 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4060 cnp->cn_cred, cnp->cn_thread, 0));
4064 zfs_freebsd_create(ap)
4065 struct vop_create_args /* {
4066 struct vnode *a_dvp;
4067 struct vnode **a_vpp;
4068 struct componentname *a_cnp;
4069 struct vattr *a_vap;
4072 struct componentname *cnp = ap->a_cnp;
4073 vattr_t *vap = ap->a_vap;
4076 ASSERT(cnp->cn_flags & SAVENAME);
4078 vattr_init_mask(vap);
4079 mode = vap->va_mode & ALLPERMS;
4081 return (zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
4082 ap->a_vpp, cnp->cn_cred, cnp->cn_thread));
4086 zfs_freebsd_remove(ap)
4087 struct vop_remove_args /* {
4088 struct vnode *a_dvp;
4090 struct componentname *a_cnp;
4094 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4096 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr,
4097 ap->a_cnp->cn_cred, NULL, 0));
4101 zfs_freebsd_mkdir(ap)
4102 struct vop_mkdir_args /* {
4103 struct vnode *a_dvp;
4104 struct vnode **a_vpp;
4105 struct componentname *a_cnp;
4106 struct vattr *a_vap;
4109 vattr_t *vap = ap->a_vap;
4111 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4113 vattr_init_mask(vap);
4115 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
4116 ap->a_cnp->cn_cred, NULL, 0, NULL));
4120 zfs_freebsd_rmdir(ap)
4121 struct vop_rmdir_args /* {
4122 struct vnode *a_dvp;
4124 struct componentname *a_cnp;
4127 struct componentname *cnp = ap->a_cnp;
4129 ASSERT(cnp->cn_flags & SAVENAME);
4131 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0));
4135 zfs_freebsd_readdir(ap)
4136 struct vop_readdir_args /* {
4139 struct ucred *a_cred;
4146 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
4147 ap->a_ncookies, ap->a_cookies));
4151 zfs_freebsd_fsync(ap)
4152 struct vop_fsync_args /* {
4155 struct thread *a_td;
4160 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
4164 zfs_freebsd_getattr(ap)
4165 struct vop_getattr_args /* {
4167 struct vattr *a_vap;
4168 struct ucred *a_cred;
4169 struct thread *a_td;
4172 vattr_t *vap = ap->a_vap;
4178 xvap.xva_vattr = *vap;
4179 xvap.xva_vattr.va_mask |= AT_XVATTR;
4181 /* Convert chflags into ZFS-type flags. */
4182 /* XXX: what about SF_SETTABLE?. */
4183 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
4184 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
4185 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
4186 XVA_SET_REQ(&xvap, XAT_NODUMP);
4187 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
4191 /* Convert ZFS xattr into chflags. */
4192 #define FLAG_CHECK(fflag, xflag, xfield) do { \
4193 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
4194 fflags |= (fflag); \
4196 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
4197 xvap.xva_xoptattrs.xoa_immutable);
4198 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
4199 xvap.xva_xoptattrs.xoa_appendonly);
4200 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
4201 xvap.xva_xoptattrs.xoa_nounlink);
4202 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
4203 xvap.xva_xoptattrs.xoa_nodump);
4205 *vap = xvap.xva_vattr;
4206 vap->va_flags = fflags;
4211 zfs_freebsd_setattr(ap)
4212 struct vop_setattr_args /* {
4214 struct vattr *a_vap;
4215 struct ucred *a_cred;
4216 struct thread *a_td;
4219 vnode_t *vp = ap->a_vp;
4220 vattr_t *vap = ap->a_vap;
4221 cred_t *cred = ap->a_cred;
4226 vattr_init_mask(vap);
4227 vap->va_mask &= ~AT_NOSET;
4230 xvap.xva_vattr = *vap;
4232 zflags = VTOZ(vp)->z_phys->zp_flags;
4234 if (vap->va_flags != VNOVAL) {
4235 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
4238 if (zfsvfs->z_use_fuids == B_FALSE)
4239 return (EOPNOTSUPP);
4241 fflags = vap->va_flags;
4242 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_NODUMP)) != 0)
4243 return (EOPNOTSUPP);
4245 * Unprivileged processes are not permitted to unset system
4246 * flags, or modify flags if any system flags are set.
4247 * Privileged non-jail processes may not modify system flags
4248 * if securelevel > 0 and any existing system flags are set.
4249 * Privileged jail processes behave like privileged non-jail
4250 * processes if the security.jail.chflags_allowed sysctl is
4251 * is non-zero; otherwise, they behave like unprivileged
4254 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
4255 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
4257 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4258 error = securelevel_gt(cred, 0);
4264 * Callers may only modify the file flags on objects they
4265 * have VADMIN rights for.
4267 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
4270 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4274 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
4279 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
4280 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
4281 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
4282 XVA_SET_REQ(&xvap, (xflag)); \
4283 (xfield) = ((fflags & (fflag)) != 0); \
4286 /* Convert chflags into ZFS-type flags. */
4287 /* XXX: what about SF_SETTABLE?. */
4288 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
4289 xvap.xva_xoptattrs.xoa_immutable);
4290 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
4291 xvap.xva_xoptattrs.xoa_appendonly);
4292 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
4293 xvap.xva_xoptattrs.xoa_nounlink);
4294 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
4295 xvap.xva_xoptattrs.xoa_nodump);
4298 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
4302 zfs_freebsd_rename(ap)
4303 struct vop_rename_args /* {
4304 struct vnode *a_fdvp;
4305 struct vnode *a_fvp;
4306 struct componentname *a_fcnp;
4307 struct vnode *a_tdvp;
4308 struct vnode *a_tvp;
4309 struct componentname *a_tcnp;
4312 vnode_t *fdvp = ap->a_fdvp;
4313 vnode_t *fvp = ap->a_fvp;
4314 vnode_t *tdvp = ap->a_tdvp;
4315 vnode_t *tvp = ap->a_tvp;
4318 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
4319 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
4321 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp,
4322 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0);
4337 zfs_freebsd_symlink(ap)
4338 struct vop_symlink_args /* {
4339 struct vnode *a_dvp;
4340 struct vnode **a_vpp;
4341 struct componentname *a_cnp;
4342 struct vattr *a_vap;
4346 struct componentname *cnp = ap->a_cnp;
4347 vattr_t *vap = ap->a_vap;
4349 ASSERT(cnp->cn_flags & SAVENAME);
4351 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
4352 vattr_init_mask(vap);
4354 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
4355 ap->a_target, cnp->cn_cred, cnp->cn_thread));
4359 zfs_freebsd_readlink(ap)
4360 struct vop_readlink_args /* {
4363 struct ucred *a_cred;
4367 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
4371 zfs_freebsd_link(ap)
4372 struct vop_link_args /* {
4373 struct vnode *a_tdvp;
4375 struct componentname *a_cnp;
4378 struct componentname *cnp = ap->a_cnp;
4380 ASSERT(cnp->cn_flags & SAVENAME);
4382 return (zfs_link(ap->a_tdvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
4386 zfs_freebsd_inactive(ap)
4387 struct vop_inactive_args /* {
4389 struct thread *a_td;
4392 vnode_t *vp = ap->a_vp;
4394 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
4399 zfs_reclaim_complete(void *arg, int pending)
4402 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4404 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4405 if (zp->z_dbuf != NULL) {
4406 ZFS_OBJ_HOLD_ENTER(zfsvfs, zp->z_id);
4407 zfs_znode_dmu_fini(zp);
4408 ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
4411 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4413 * If the file system is being unmounted, there is a process waiting
4414 * for us, wake it up.
4416 if (zfsvfs->z_unmounted)
4421 zfs_freebsd_reclaim(ap)
4422 struct vop_reclaim_args /* {
4424 struct thread *a_td;
4427 vnode_t *vp = ap->a_vp;
4428 znode_t *zp = VTOZ(vp);
4429 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4431 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4436 * Destroy the vm object and flush associated pages.
4438 vnode_destroy_vobject(vp);
4440 mutex_enter(&zp->z_lock);
4441 ASSERT(zp->z_phys != NULL);
4443 mutex_exit(&zp->z_lock);
4447 else if (zp->z_dbuf == NULL)
4449 else /* if (!zp->z_unlinked && zp->z_dbuf != NULL) */ {
4452 locked = MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)) ? 2 :
4453 ZFS_OBJ_HOLD_TRYENTER(zfsvfs, zp->z_id);
4456 * Lock can't be obtained due to deadlock possibility,
4457 * so defer znode destruction.
4459 TASK_INIT(&zp->z_task, 0, zfs_reclaim_complete, zp);
4460 taskqueue_enqueue(taskqueue_thread, &zp->z_task);
4462 zfs_znode_dmu_fini(zp);
4464 ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
4470 ASSERT(vp->v_holdcnt >= 1);
4472 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4478 struct vop_fid_args /* {
4484 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
4488 zfs_freebsd_pathconf(ap)
4489 struct vop_pathconf_args /* {
4492 register_t *a_retval;
4498 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
4500 *ap->a_retval = val;
4501 else if (error == EOPNOTSUPP)
4502 error = vop_stdpathconf(ap);
4507 zfs_freebsd_fifo_pathconf(ap)
4508 struct vop_pathconf_args /* {
4511 register_t *a_retval;
4515 switch (ap->a_name) {
4516 case _PC_ACL_EXTENDED:
4518 case _PC_ACL_PATH_MAX:
4519 case _PC_MAC_PRESENT:
4520 return (zfs_freebsd_pathconf(ap));
4522 return (fifo_specops.vop_pathconf(ap));
4527 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
4528 * extended attribute name:
4531 * system freebsd:system:
4532 * user (none, can be used to access ZFS fsattr(5) attributes
4533 * created on Solaris)
4536 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
4539 const char *namespace, *prefix, *suffix;
4541 /* We don't allow '/' character in attribute name. */
4542 if (strchr(name, '/') != NULL)
4544 /* We don't allow attribute names that start with "freebsd:" string. */
4545 if (strncmp(name, "freebsd:", 8) == 0)
4548 bzero(attrname, size);
4550 switch (attrnamespace) {
4551 case EXTATTR_NAMESPACE_USER:
4553 prefix = "freebsd:";
4554 namespace = EXTATTR_NAMESPACE_USER_STRING;
4558 * This is the default namespace by which we can access all
4559 * attributes created on Solaris.
4561 prefix = namespace = suffix = "";
4564 case EXTATTR_NAMESPACE_SYSTEM:
4565 prefix = "freebsd:";
4566 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
4569 case EXTATTR_NAMESPACE_EMPTY:
4573 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
4575 return (ENAMETOOLONG);
4581 * Vnode operating to retrieve a named extended attribute.
4584 zfs_getextattr(struct vop_getextattr_args *ap)
4587 IN struct vnode *a_vp;
4588 IN int a_attrnamespace;
4589 IN const char *a_name;
4590 INOUT struct uio *a_uio;
4592 IN struct ucred *a_cred;
4593 IN struct thread *a_td;
4597 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
4598 struct thread *td = ap->a_td;
4599 struct nameidata nd;
4602 vnode_t *xvp = NULL, *vp;
4605 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
4606 ap->a_cred, ap->a_td, VREAD);
4610 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
4617 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
4625 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
4627 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
4629 NDFREE(&nd, NDF_ONLY_PNBUF);
4632 if (error == ENOENT)
4637 if (ap->a_size != NULL) {
4638 error = VOP_GETATTR(vp, &va, ap->a_cred);
4640 *ap->a_size = (size_t)va.va_size;
4641 } else if (ap->a_uio != NULL)
4642 error = VOP_READ(vp, ap->a_uio, IO_UNIT | IO_SYNC, ap->a_cred);
4645 vn_close(vp, flags, ap->a_cred, td);
4652 * Vnode operation to remove a named attribute.
4655 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
4658 IN struct vnode *a_vp;
4659 IN int a_attrnamespace;
4660 IN const char *a_name;
4661 IN struct ucred *a_cred;
4662 IN struct thread *a_td;
4666 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
4667 struct thread *td = ap->a_td;
4668 struct nameidata nd;
4671 vnode_t *xvp = NULL, *vp;
4674 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
4675 ap->a_cred, ap->a_td, VWRITE);
4679 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
4686 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
4693 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF | MPSAFE,
4694 UIO_SYSSPACE, attrname, xvp, td);
4697 NDFREE(&nd, NDF_ONLY_PNBUF);
4700 if (error == ENOENT)
4704 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
4707 if (vp == nd.ni_dvp)
4717 * Vnode operation to set a named attribute.
4720 zfs_setextattr(struct vop_setextattr_args *ap)
4723 IN struct vnode *a_vp;
4724 IN int a_attrnamespace;
4725 IN const char *a_name;
4726 INOUT struct uio *a_uio;
4727 IN struct ucred *a_cred;
4728 IN struct thread *a_td;
4732 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
4733 struct thread *td = ap->a_td;
4734 struct nameidata nd;
4737 vnode_t *xvp = NULL, *vp;
4740 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
4741 ap->a_cred, ap->a_td, VWRITE);
4745 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
4752 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
4753 LOOKUP_XATTR | CREATE_XATTR_DIR);
4759 flags = FFLAGS(O_WRONLY | O_CREAT);
4760 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
4762 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
4764 NDFREE(&nd, NDF_ONLY_PNBUF);
4772 error = VOP_SETATTR(vp, &va, ap->a_cred);
4774 VOP_WRITE(vp, ap->a_uio, IO_UNIT | IO_SYNC, ap->a_cred);
4777 vn_close(vp, flags, ap->a_cred, td);
4784 * Vnode operation to retrieve extended attributes on a vnode.
4787 zfs_listextattr(struct vop_listextattr_args *ap)
4790 IN struct vnode *a_vp;
4791 IN int a_attrnamespace;
4792 INOUT struct uio *a_uio;
4794 IN struct ucred *a_cred;
4795 IN struct thread *a_td;
4799 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
4800 struct thread *td = ap->a_td;
4801 struct nameidata nd;
4802 char attrprefix[16];
4803 u_char dirbuf[sizeof(struct dirent)];
4806 struct uio auio, *uio = ap->a_uio;
4807 size_t *sizep = ap->a_size;
4809 vnode_t *xvp = NULL, *vp;
4810 int done, error, eof, pos;
4812 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
4813 ap->a_cred, ap->a_td, VREAD);
4817 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
4818 sizeof(attrprefix));
4821 plen = strlen(attrprefix);
4828 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
4833 * ENOATTR means that the EA directory does not yet exist,
4834 * i.e. there are no extended attributes there.
4836 if (error == ENOATTR)
4841 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE,
4842 UIO_SYSSPACE, ".", xvp, td);
4845 NDFREE(&nd, NDF_ONLY_PNBUF);
4851 auio.uio_iov = &aiov;
4852 auio.uio_iovcnt = 1;
4853 auio.uio_segflg = UIO_SYSSPACE;
4855 auio.uio_rw = UIO_READ;
4856 auio.uio_offset = 0;
4861 aiov.iov_base = (void *)dirbuf;
4862 aiov.iov_len = sizeof(dirbuf);
4863 auio.uio_resid = sizeof(dirbuf);
4864 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
4865 done = sizeof(dirbuf) - auio.uio_resid;
4868 for (pos = 0; pos < done;) {
4869 dp = (struct dirent *)(dirbuf + pos);
4870 pos += dp->d_reclen;
4872 * XXX: Temporarily we also accept DT_UNKNOWN, as this
4873 * is what we get when attribute was created on Solaris.
4875 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
4877 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
4879 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
4881 nlen = dp->d_namlen - plen;
4884 else if (uio != NULL) {
4886 * Format of extattr name entry is one byte for
4887 * length and the rest for name.
4889 error = uiomove(&nlen, 1, uio->uio_rw, uio);
4891 error = uiomove(dp->d_name + plen, nlen,
4898 } while (!eof && error == 0);
4907 zfs_freebsd_getacl(ap)
4908 struct vop_getacl_args /* {
4917 vsecattr_t vsecattr;
4919 if (ap->a_type != ACL_TYPE_NFS4)
4922 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
4923 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
4926 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
4927 if (vsecattr.vsa_aclentp != NULL)
4928 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
4934 zfs_freebsd_setacl(ap)
4935 struct vop_setacl_args /* {
4944 vsecattr_t vsecattr;
4945 int aclbsize; /* size of acl list in bytes */
4948 if (ap->a_type != ACL_TYPE_NFS4)
4951 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
4955 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
4956 * splitting every entry into two and appending "canonical six"
4957 * entries at the end. Don't allow for setting an ACL that would
4958 * cause chmod(2) to run out of ACL entries.
4960 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
4963 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
4967 vsecattr.vsa_mask = VSA_ACE;
4968 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
4969 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
4970 aaclp = vsecattr.vsa_aclentp;
4971 vsecattr.vsa_aclentsz = aclbsize;
4973 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
4974 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
4975 kmem_free(aaclp, aclbsize);
4981 zfs_freebsd_aclcheck(ap)
4982 struct vop_aclcheck_args /* {
4991 return (EOPNOTSUPP);
4994 struct vop_vector zfs_vnodeops;
4995 struct vop_vector zfs_fifoops;
4997 struct vop_vector zfs_vnodeops = {
4998 .vop_default = &default_vnodeops,
4999 .vop_inactive = zfs_freebsd_inactive,
5000 .vop_reclaim = zfs_freebsd_reclaim,
5001 .vop_access = zfs_freebsd_access,
5002 #ifdef FREEBSD_NAMECACHE
5003 .vop_lookup = vfs_cache_lookup,
5004 .vop_cachedlookup = zfs_freebsd_lookup,
5006 .vop_lookup = zfs_freebsd_lookup,
5008 .vop_getattr = zfs_freebsd_getattr,
5009 .vop_setattr = zfs_freebsd_setattr,
5010 .vop_create = zfs_freebsd_create,
5011 .vop_mknod = zfs_freebsd_create,
5012 .vop_mkdir = zfs_freebsd_mkdir,
5013 .vop_readdir = zfs_freebsd_readdir,
5014 .vop_fsync = zfs_freebsd_fsync,
5015 .vop_open = zfs_freebsd_open,
5016 .vop_close = zfs_freebsd_close,
5017 .vop_rmdir = zfs_freebsd_rmdir,
5018 .vop_ioctl = zfs_freebsd_ioctl,
5019 .vop_link = zfs_freebsd_link,
5020 .vop_symlink = zfs_freebsd_symlink,
5021 .vop_readlink = zfs_freebsd_readlink,
5022 .vop_read = zfs_freebsd_read,
5023 .vop_write = zfs_freebsd_write,
5024 .vop_remove = zfs_freebsd_remove,
5025 .vop_rename = zfs_freebsd_rename,
5026 .vop_pathconf = zfs_freebsd_pathconf,
5027 .vop_bmap = VOP_EOPNOTSUPP,
5028 .vop_fid = zfs_freebsd_fid,
5029 .vop_getextattr = zfs_getextattr,
5030 .vop_deleteextattr = zfs_deleteextattr,
5031 .vop_setextattr = zfs_setextattr,
5032 .vop_listextattr = zfs_listextattr,
5033 .vop_getacl = zfs_freebsd_getacl,
5034 .vop_setacl = zfs_freebsd_setacl,
5035 .vop_aclcheck = zfs_freebsd_aclcheck,
5038 struct vop_vector zfs_fifoops = {
5039 .vop_default = &fifo_specops,
5040 .vop_fsync = zfs_freebsd_fsync,
5041 .vop_access = zfs_freebsd_access,
5042 .vop_getattr = zfs_freebsd_getattr,
5043 .vop_inactive = zfs_freebsd_inactive,
5044 .vop_read = VOP_PANIC,
5045 .vop_reclaim = zfs_freebsd_reclaim,
5046 .vop_setattr = zfs_freebsd_setattr,
5047 .vop_write = VOP_PANIC,
5048 .vop_pathconf = zfs_freebsd_fifo_pathconf,
5049 .vop_fid = zfs_freebsd_fid,
5050 .vop_getacl = zfs_freebsd_getacl,
5051 .vop_setacl = zfs_freebsd_setacl,
5052 .vop_aclcheck = zfs_freebsd_aclcheck,