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 2009 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>
60 #include <sys/zfs_ctldir.h>
61 #include <sys/zfs_fuid.h>
63 #include <sys/zfs_rlock.h>
64 #include <sys/extdirent.h>
65 #include <sys/kidmap.h>
68 #include <sys/sf_buf.h>
69 #include <sys/sched.h>
75 * Each vnode op performs some logical unit of work. To do this, the ZPL must
76 * properly lock its in-core state, create a DMU transaction, do the work,
77 * record this work in the intent log (ZIL), commit the DMU transaction,
78 * and wait for the intent log to commit if it is a synchronous operation.
79 * Moreover, the vnode ops must work in both normal and log replay context.
80 * The ordering of events is important to avoid deadlocks and references
81 * to freed memory. The example below illustrates the following Big Rules:
83 * (1) A check must be made in each zfs thread for a mounted file system.
84 * This is done avoiding races using ZFS_ENTER(zfsvfs).
85 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
86 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
87 * can return EIO from the calling function.
89 * (2) VN_RELE() should always be the last thing except for zil_commit()
90 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
91 * First, if it's the last reference, the vnode/znode
92 * can be freed, so the zp may point to freed memory. Second, the last
93 * reference will call zfs_zinactive(), which may induce a lot of work --
94 * pushing cached pages (which acquires range locks) and syncing out
95 * cached atime changes. Third, zfs_zinactive() may require a new tx,
96 * which could deadlock the system if you were already holding one.
97 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
99 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
100 * as they can span dmu_tx_assign() calls.
102 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
103 * This is critical because we don't want to block while holding locks.
104 * Note, in particular, that if a lock is sometimes acquired before
105 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
106 * use a non-blocking assign can deadlock the system. The scenario:
108 * Thread A has grabbed a lock before calling dmu_tx_assign().
109 * Thread B is in an already-assigned tx, and blocks for this lock.
110 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
111 * forever, because the previous txg can't quiesce until B's tx commits.
113 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
114 * then drop all locks, call dmu_tx_wait(), and try again.
116 * (5) If the operation succeeded, generate the intent log entry for it
117 * before dropping locks. This ensures that the ordering of events
118 * in the intent log matches the order in which they actually occurred.
119 * During ZIL replay the zfs_log_* functions will update the sequence
120 * number to indicate the zil transaction has replayed.
122 * (6) At the end of each vnode op, the DMU tx must always commit,
123 * regardless of whether there were any errors.
125 * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid)
126 * to ensure that synchronous semantics are provided when necessary.
128 * In general, this is how things should be ordered in each vnode op:
130 * ZFS_ENTER(zfsvfs); // exit if unmounted
132 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
133 * rw_enter(...); // grab any other locks you need
134 * tx = dmu_tx_create(...); // get DMU tx
135 * dmu_tx_hold_*(); // hold each object you might modify
136 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
138 * rw_exit(...); // drop locks
139 * zfs_dirent_unlock(dl); // unlock directory entry
140 * VN_RELE(...); // release held vnodes
141 * if (error == ERESTART) {
146 * dmu_tx_abort(tx); // abort DMU tx
147 * ZFS_EXIT(zfsvfs); // finished in zfs
148 * return (error); // really out of space
150 * error = do_real_work(); // do whatever this VOP does
152 * zfs_log_*(...); // on success, make ZIL entry
153 * dmu_tx_commit(tx); // commit DMU tx -- error or not
154 * rw_exit(...); // drop locks
155 * zfs_dirent_unlock(dl); // unlock directory entry
156 * VN_RELE(...); // release held vnodes
157 * zil_commit(zilog, seq, foid); // synchronous when necessary
158 * ZFS_EXIT(zfsvfs); // finished in zfs
159 * return (error); // done, report error
164 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
166 znode_t *zp = VTOZ(*vpp);
167 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
172 if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) &&
173 ((flag & FAPPEND) == 0)) {
178 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
179 ZTOV(zp)->v_type == VREG &&
180 !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
181 zp->z_phys->zp_size > 0) {
182 if (fs_vscan(*vpp, cr, 0) != 0) {
188 /* Keep a count of the synchronous opens in the znode */
189 if (flag & (FSYNC | FDSYNC))
190 atomic_inc_32(&zp->z_sync_cnt);
198 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
199 caller_context_t *ct)
201 znode_t *zp = VTOZ(vp);
202 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
207 /* Decrement the synchronous opens in the znode */
208 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
209 atomic_dec_32(&zp->z_sync_cnt);
212 * Clean up any locks held by this process on the vp.
214 cleanlocks(vp, ddi_get_pid(), 0);
215 cleanshares(vp, ddi_get_pid());
217 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
218 ZTOV(zp)->v_type == VREG &&
219 !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
220 zp->z_phys->zp_size > 0)
221 VERIFY(fs_vscan(vp, cr, 1) == 0);
228 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
229 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
232 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
234 znode_t *zp = VTOZ(vp);
235 uint64_t noff = (uint64_t)*off; /* new offset */
240 file_sz = zp->z_phys->zp_size;
241 if (noff >= file_sz) {
245 if (cmd == _FIO_SEEK_HOLE)
250 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
253 if ((error == ESRCH) || (noff > file_sz)) {
255 * Handle the virtual hole at the end of file.
272 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
273 int *rvalp, caller_context_t *ct)
285 * The following two ioctls are used by bfu. Faking out,
286 * necessary to avoid bfu errors.
294 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
298 zfsvfs = zp->z_zfsvfs;
302 /* offset parameter is in/out */
303 error = zfs_holey(vp, com, &off);
307 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
315 page_lookup(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
321 VM_OBJECT_LOCK_ASSERT(obj, MA_OWNED);
324 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
325 vm_page_is_valid(pp, (vm_offset_t)off, nbytes)) {
326 if (vm_page_sleep_if_busy(pp, FALSE, "zfsmwb"))
329 vm_page_lock_queues();
331 vm_page_unlock_queues();
333 if (__predict_false(obj->cache != NULL)) {
334 vm_page_cache_free(obj, OFF_TO_IDX(start),
335 OFF_TO_IDX(start) + 1);
345 page_unlock(vm_page_t pp)
352 zfs_map_page(vm_page_t pp, struct sf_buf **sfp)
356 *sfp = sf_buf_alloc(pp, SFB_CPUPRIVATE);
357 return ((caddr_t)sf_buf_kva(*sfp));
361 zfs_unmap_page(struct sf_buf *sf)
370 * When a file is memory mapped, we must keep the IO data synchronized
371 * between the DMU cache and the memory mapped pages. What this means:
373 * On Write: If we find a memory mapped page, we write to *both*
374 * the page and the dmu buffer.
378 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
379 int segflg, dmu_tx_t *tx)
385 ASSERT(vp->v_mount != NULL);
389 off = start & PAGEOFFSET;
391 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
393 uint64_t nbytes = MIN(PAGESIZE - off, len);
395 if ((pp = page_lookup(vp, start, off, nbytes)) != NULL) {
398 VM_OBJECT_UNLOCK(obj);
399 va = zfs_map_page(pp, &sf);
400 if (segflg == UIO_NOCOPY) {
401 (void) dmu_write(os, oid, start+off, nbytes,
404 (void) dmu_read(os, oid, start+off, nbytes,
405 va+off, DMU_READ_PREFETCH);;
415 VM_OBJECT_UNLOCK(obj);
419 * When a file is memory mapped, we must keep the IO data synchronized
420 * between the DMU cache and the memory mapped pages. What this means:
422 * On Read: We "read" preferentially from memory mapped pages,
423 * else we default from the dmu buffer.
425 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
426 * the file is memory mapped.
429 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
431 znode_t *zp = VTOZ(vp);
432 objset_t *os = zp->z_zfsvfs->z_os;
442 ASSERT(vp->v_mount != NULL);
446 start = uio->uio_loffset;
447 off = start & PAGEOFFSET;
450 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
451 uint64_t bytes = MIN(PAGESIZE - off, len);
454 if ((m = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
455 vm_page_is_valid(m, (vm_offset_t)off, bytes)) {
456 if (vm_page_sleep_if_busy(m, FALSE, "zfsmrb"))
459 VM_OBJECT_UNLOCK(obj);
461 error = dmu_read_uio(os, zp->z_id, uio,
467 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
468 va = (caddr_t)sf_buf_kva(sf);
469 error = uiomove(va + off, bytes, UIO_READ, uio);
475 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
477 * The code below is here to make sendfile(2) work
478 * correctly with ZFS. As pointed out by ups@
479 * sendfile(2) should be changed to use VOP_GETPAGES(),
480 * but it pessimize performance of sendfile/UFS, that's
481 * why I handle this special case in ZFS code.
483 if (vm_page_sleep_if_busy(m, FALSE, "zfsmrb"))
486 VM_OBJECT_UNLOCK(obj);
488 error = dmu_read_uio(os, zp->z_id, uio,
494 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
495 va = (caddr_t)sf_buf_kva(sf);
496 error = dmu_read(os, zp->z_id, start + off,
497 bytes, (void *)(va + off),
505 uio->uio_resid -= bytes;
514 VM_OBJECT_UNLOCK(obj);
515 if (error == 0 && dirbytes > 0)
516 error = dmu_read_uio(os, zp->z_id, uio, dirbytes);
520 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
523 * Read bytes from specified file into supplied buffer.
525 * IN: vp - vnode of file to be read from.
526 * uio - structure supplying read location, range info,
528 * ioflag - SYNC flags; used to provide FRSYNC semantics.
529 * cr - credentials of caller.
530 * ct - caller context
532 * OUT: uio - updated offset and range, buffer filled.
534 * RETURN: 0 if success
535 * error code if failure
538 * vp - atime updated if byte count > 0
542 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
544 znode_t *zp = VTOZ(vp);
545 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
555 if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) {
561 * Validate file offset
563 if (uio->uio_loffset < (offset_t)0) {
569 * Fasttrack empty reads
571 if (uio->uio_resid == 0) {
577 * Check for mandatory locks
579 if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
580 if (error = chklock(vp, FREAD,
581 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
588 * If we're in FRSYNC mode, sync out this znode before reading it.
591 zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
594 * Lock the range against changes.
596 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
599 * If we are reading past end-of-file we can skip
600 * to the end; but we might still need to set atime.
602 if (uio->uio_loffset >= zp->z_phys->zp_size) {
607 ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
608 n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
611 nbytes = MIN(n, zfs_read_chunk_size -
612 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
614 if (vn_has_cached_data(vp))
615 error = mappedread(vp, nbytes, uio);
617 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
619 /* convert checksum errors into IO errors */
629 zfs_range_unlock(rl);
631 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
637 * Fault in the pages of the first n bytes specified by the uio structure.
638 * 1 byte in each page is touched and the uio struct is unmodified.
639 * Any error will exit this routine as this is only a best
640 * attempt to get the pages resident. This is a copy of ufs_trans_touch().
643 zfs_prefault_write(ssize_t n, struct uio *uio)
649 if (uio->uio_segflg != UIO_USERSPACE)
655 cnt = MIN(iov->iov_len, n);
657 /* empty iov entry */
663 * touch each page in this segment.
669 incr = MIN(cnt, PAGESIZE);
674 * touch the last byte in case it straddles a page.
684 * Write the bytes to a file.
686 * IN: vp - vnode of file to be written to.
687 * uio - structure supplying write location, range info,
689 * ioflag - IO_APPEND flag set if in append mode.
690 * cr - credentials of caller.
691 * ct - caller context (NFS/CIFS fem monitor only)
693 * OUT: uio - updated offset and range.
695 * RETURN: 0 if success
696 * error code if failure
699 * vp - ctime|mtime updated if byte count > 0
703 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
705 znode_t *zp = VTOZ(vp);
706 rlim64_t limit = MAXOFFSET_T;
707 ssize_t start_resid = uio->uio_resid;
711 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
716 int max_blksz = zfsvfs->z_max_blksz;
722 * Fasttrack empty write
728 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
735 * If immutable or not appending then return EPERM
737 pflags = zp->z_phys->zp_flags;
738 if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
739 ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
740 (uio->uio_loffset < zp->z_phys->zp_size))) {
745 zilog = zfsvfs->z_log;
748 * Pre-fault the pages to ensure slow (eg NFS) pages
751 zfs_prefault_write(n, uio);
754 * If in append mode, set the io offset pointer to eof.
756 if (ioflag & IO_APPEND) {
758 * Range lock for a file append:
759 * The value for the start of range will be determined by
760 * zfs_range_lock() (to guarantee append semantics).
761 * If this write will cause the block size to increase,
762 * zfs_range_lock() will lock the entire file, so we must
763 * later reduce the range after we grow the block size.
765 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
766 if (rl->r_len == UINT64_MAX) {
767 /* overlocked, zp_size can't change */
768 woff = uio->uio_loffset = zp->z_phys->zp_size;
770 woff = uio->uio_loffset = rl->r_off;
773 woff = uio->uio_loffset;
775 * Validate file offset
783 * If we need to grow the block size then zfs_range_lock()
784 * will lock a wider range than we request here.
785 * Later after growing the block size we reduce the range.
787 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
791 zfs_range_unlock(rl);
796 if ((woff + n) > limit || woff > (limit - n))
800 * Check for mandatory locks
802 if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
803 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
804 zfs_range_unlock(rl);
808 end_size = MAX(zp->z_phys->zp_size, woff + n);
811 * Write the file in reasonable size chunks. Each chunk is written
812 * in a separate transaction; this keeps the intent log records small
813 * and allows us to do more fine-grained space accounting.
817 woff = uio->uio_loffset;
820 if (zfs_usergroup_overquota(zfsvfs,
821 B_FALSE, zp->z_phys->zp_uid) ||
822 zfs_usergroup_overquota(zfsvfs,
823 B_TRUE, zp->z_phys->zp_gid)) {
825 dmu_return_arcbuf(abuf);
831 * If dmu_assign_arcbuf() is expected to execute with minimum
832 * overhead loan an arc buffer and copy user data to it before
833 * we enter a txg. This avoids holding a txg forever while we
834 * pagefault on a hanging NFS server mapping.
836 if (abuf == NULL && n >= max_blksz &&
837 woff >= zp->z_phys->zp_size &&
838 P2PHASE(woff, max_blksz) == 0 &&
839 zp->z_blksz == max_blksz) {
842 abuf = dmu_request_arcbuf(zp->z_dbuf, max_blksz);
843 ASSERT(abuf != NULL);
844 ASSERT(arc_buf_size(abuf) == max_blksz);
845 if (error = uiocopy(abuf->b_data, max_blksz,
846 UIO_WRITE, uio, &cbytes)) {
847 dmu_return_arcbuf(abuf);
850 ASSERT(cbytes == max_blksz);
854 * Start a transaction.
856 tx = dmu_tx_create(zfsvfs->z_os);
857 dmu_tx_hold_bonus(tx, zp->z_id);
858 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
859 error = dmu_tx_assign(tx, TXG_NOWAIT);
861 if (error == ERESTART) {
868 dmu_return_arcbuf(abuf);
873 * If zfs_range_lock() over-locked we grow the blocksize
874 * and then reduce the lock range. This will only happen
875 * on the first iteration since zfs_range_reduce() will
876 * shrink down r_len to the appropriate size.
878 if (rl->r_len == UINT64_MAX) {
881 if (zp->z_blksz > max_blksz) {
882 ASSERT(!ISP2(zp->z_blksz));
883 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
885 new_blksz = MIN(end_size, max_blksz);
887 zfs_grow_blocksize(zp, new_blksz, tx);
888 zfs_range_reduce(rl, woff, n);
892 * XXX - should we really limit each write to z_max_blksz?
893 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
895 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
897 if (woff + nbytes > zp->z_phys->zp_size)
898 vnode_pager_setsize(vp, woff + nbytes);
901 tx_bytes = uio->uio_resid;
902 error = dmu_write_uio(zfsvfs->z_os, zp->z_id, uio,
904 tx_bytes -= uio->uio_resid;
907 ASSERT(tx_bytes == max_blksz);
908 dmu_assign_arcbuf(zp->z_dbuf, woff, abuf, tx);
909 ASSERT(tx_bytes <= uio->uio_resid);
910 uioskip(uio, tx_bytes);
914 * XXXPJD: There are some cases (triggered by fsx) where
915 * vn_has_cached_data(vp) returns false when it should
916 * return true. This should be investigated.
919 if (tx_bytes && vn_has_cached_data(vp))
921 if (tx_bytes && vp->v_object != NULL)
924 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
925 zp->z_id, uio->uio_segflg, tx);
929 * If we made no progress, we're done. If we made even
930 * partial progress, update the znode and ZIL accordingly.
939 * Clear Set-UID/Set-GID bits on successful write if not
940 * privileged and at least one of the excute bits is set.
942 * It would be nice to to this after all writes have
943 * been done, but that would still expose the ISUID/ISGID
944 * to another app after the partial write is committed.
946 * Note: we don't call zfs_fuid_map_id() here because
947 * user 0 is not an ephemeral uid.
949 mutex_enter(&zp->z_acl_lock);
950 if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
951 (S_IXUSR >> 6))) != 0 &&
952 (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
953 secpolicy_vnode_setid_retain(vp, cr,
954 (zp->z_phys->zp_mode & S_ISUID) != 0 &&
955 zp->z_phys->zp_uid == 0) != 0) {
956 zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
958 mutex_exit(&zp->z_acl_lock);
961 * Update time stamp. NOTE: This marks the bonus buffer as
962 * dirty, so we don't have to do it again for zp_size.
964 zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
967 * Update the file size (zp_size) if it has changed;
968 * account for possible concurrent updates.
970 while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
971 (void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
973 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
978 ASSERT(tx_bytes == nbytes);
982 zfs_range_unlock(rl);
985 * If we're in replay mode, or we made no progress, return error.
986 * Otherwise, it's at least a partial write, so it's successful.
988 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
993 if (ioflag & (FSYNC | FDSYNC))
994 zil_commit(zilog, zp->z_last_itx, zp->z_id);
1001 zfs_get_done(dmu_buf_t *db, void *vzgd)
1003 zgd_t *zgd = (zgd_t *)vzgd;
1004 rl_t *rl = zgd->zgd_rl;
1005 vnode_t *vp = ZTOV(rl->r_zp);
1006 objset_t *os = rl->r_zp->z_zfsvfs->z_os;
1009 vfslocked = VFS_LOCK_GIANT(vp->v_vfsp);
1010 dmu_buf_rele(db, vzgd);
1011 zfs_range_unlock(rl);
1013 * Release the vnode asynchronously as we currently have the
1014 * txg stopped from syncing.
1016 VN_RELE_ASYNC(vp, dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1017 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1018 kmem_free(zgd, sizeof (zgd_t));
1019 VFS_UNLOCK_GIANT(vfslocked);
1023 * Get data to generate a TX_WRITE intent log record.
1026 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1028 zfsvfs_t *zfsvfs = arg;
1029 objset_t *os = zfsvfs->z_os;
1031 uint64_t off = lr->lr_offset;
1035 int dlen = lr->lr_length; /* length of user data */
1042 * Nothing to do if the file has been removed
1044 if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
1046 if (zp->z_unlinked) {
1048 * Release the vnode asynchronously as we currently have the
1049 * txg stopped from syncing.
1051 VN_RELE_ASYNC(ZTOV(zp),
1052 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1057 * Write records come in two flavors: immediate and indirect.
1058 * For small writes it's cheaper to store the data with the
1059 * log record (immediate); for large writes it's cheaper to
1060 * sync the data and get a pointer to it (indirect) so that
1061 * we don't have to write the data twice.
1063 if (buf != NULL) { /* immediate write */
1064 rl = zfs_range_lock(zp, off, dlen, RL_READER);
1065 /* test for truncation needs to be done while range locked */
1066 if (off >= zp->z_phys->zp_size) {
1070 VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf,
1071 DMU_READ_NO_PREFETCH));
1072 } else { /* indirect write */
1073 uint64_t boff; /* block starting offset */
1076 * Have to lock the whole block to ensure when it's
1077 * written out and it's checksum is being calculated
1078 * that no one can change the data. We need to re-check
1079 * blocksize after we get the lock in case it's changed!
1082 if (ISP2(zp->z_blksz)) {
1083 boff = P2ALIGN_TYPED(off, zp->z_blksz,
1089 rl = zfs_range_lock(zp, boff, dlen, RL_READER);
1090 if (zp->z_blksz == dlen)
1092 zfs_range_unlock(rl);
1094 /* test for truncation needs to be done while range locked */
1095 if (off >= zp->z_phys->zp_size) {
1099 zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
1101 zgd->zgd_zilog = zfsvfs->z_log;
1102 zgd->zgd_bp = &lr->lr_blkptr;
1103 VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
1104 ASSERT(boff == db->db_offset);
1105 lr->lr_blkoff = off - boff;
1106 error = dmu_sync(zio, db, &lr->lr_blkptr,
1107 lr->lr_common.lrc_txg, zfs_get_done, zgd);
1108 ASSERT((error && error != EINPROGRESS) ||
1109 lr->lr_length <= zp->z_blksz);
1112 * dmu_sync() can compress a block of zeros to a null
1113 * blkptr but the block size still needs to be passed
1114 * through to replay.
1116 BP_SET_LSIZE(&lr->lr_blkptr, db->db_size);
1117 zil_add_block(zfsvfs->z_log, &lr->lr_blkptr);
1121 * If we get EINPROGRESS, then we need to wait for a
1122 * write IO initiated by dmu_sync() to complete before
1123 * we can release this dbuf. We will finish everything
1124 * up in the zfs_get_done() callback.
1126 if (error == EINPROGRESS) {
1128 } else if (error == EALREADY) {
1129 lr->lr_common.lrc_txtype = TX_WRITE2;
1132 dmu_buf_rele(db, zgd);
1133 kmem_free(zgd, sizeof (zgd_t));
1136 zfs_range_unlock(rl);
1138 * Release the vnode asynchronously as we currently have the
1139 * txg stopped from syncing.
1141 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1147 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1148 caller_context_t *ct)
1150 znode_t *zp = VTOZ(vp);
1151 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1157 if (flag & V_ACE_MASK)
1158 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1160 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1167 * Lookup an entry in a directory, or an extended attribute directory.
1168 * If it exists, return a held vnode reference for it.
1170 * IN: dvp - vnode of directory to search.
1171 * nm - name of entry to lookup.
1172 * pnp - full pathname to lookup [UNUSED].
1173 * flags - LOOKUP_XATTR set if looking for an attribute.
1174 * rdir - root directory vnode [UNUSED].
1175 * cr - credentials of caller.
1176 * ct - caller context
1177 * direntflags - directory lookup flags
1178 * realpnp - returned pathname.
1180 * OUT: vpp - vnode of located entry, NULL if not found.
1182 * RETURN: 0 if success
1183 * error code if failure
1190 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1191 int nameiop, cred_t *cr, kthread_t *td, int flags)
1193 znode_t *zdp = VTOZ(dvp);
1194 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1196 int *direntflags = NULL;
1197 void *realpnp = NULL;
1204 if (flags & LOOKUP_XATTR) {
1207 * If the xattr property is off, refuse the lookup request.
1209 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1216 * We don't allow recursive attributes..
1217 * Maybe someday we will.
1219 if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1224 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1230 * Do we have permission to get into attribute directory?
1233 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1243 if (dvp->v_type != VDIR) {
1249 * Check accessibility of directory.
1252 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1257 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1258 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1263 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1266 * Convert device special files
1268 if (IS_DEVVP(*vpp)) {
1271 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1280 /* Translate errors and add SAVENAME when needed. */
1281 if (cnp->cn_flags & ISLASTCN) {
1285 if (error == ENOENT) {
1286 error = EJUSTRETURN;
1287 cnp->cn_flags |= SAVENAME;
1293 cnp->cn_flags |= SAVENAME;
1297 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) {
1300 if (cnp->cn_flags & ISDOTDOT) {
1301 ltype = VOP_ISLOCKED(dvp);
1305 error = vn_lock(*vpp, cnp->cn_lkflags);
1306 if (cnp->cn_flags & ISDOTDOT)
1307 vn_lock(dvp, ltype | LK_RETRY);
1317 #ifdef FREEBSD_NAMECACHE
1319 * Insert name into cache (as non-existent) if appropriate.
1321 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
1322 cache_enter(dvp, *vpp, cnp);
1324 * Insert name into cache if appropriate.
1326 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1327 if (!(cnp->cn_flags & ISLASTCN) ||
1328 (nameiop != DELETE && nameiop != RENAME)) {
1329 cache_enter(dvp, *vpp, cnp);
1338 * Attempt to create a new entry in a directory. If the entry
1339 * already exists, truncate the file if permissible, else return
1340 * an error. Return the vp of the created or trunc'd file.
1342 * IN: dvp - vnode of directory to put new file entry in.
1343 * name - name of new file entry.
1344 * vap - attributes of new file.
1345 * excl - flag indicating exclusive or non-exclusive mode.
1346 * mode - mode to open file with.
1347 * cr - credentials of caller.
1348 * flag - large file flag [UNUSED].
1349 * ct - caller context
1350 * vsecp - ACL to be set
1352 * OUT: vpp - vnode of created or trunc'd entry.
1354 * RETURN: 0 if success
1355 * error code if failure
1358 * dvp - ctime|mtime updated if new entry created
1359 * vp - ctime|mtime always, atime if new
1364 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1365 vnode_t **vpp, cred_t *cr, kthread_t *td)
1367 znode_t *zp, *dzp = VTOZ(dvp);
1368 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1376 gid_t gid = crgetgid(cr);
1377 zfs_acl_ids_t acl_ids;
1378 boolean_t fuid_dirtied;
1383 * If we have an ephemeral id, ACL, or XVATTR then
1384 * make sure file system is at proper version
1387 ksid = crgetsid(cr, KSID_OWNER);
1389 uid = ksid_getid(ksid);
1392 if (zfsvfs->z_use_fuids == B_FALSE &&
1393 (vsecp || (vap->va_mask & AT_XVATTR) ||
1394 IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))))
1400 zilog = zfsvfs->z_log;
1402 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1403 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1408 if (vap->va_mask & AT_XVATTR) {
1409 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1410 crgetuid(cr), cr, vap->va_type)) != 0) {
1418 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1419 vap->va_mode &= ~S_ISVTX;
1421 if (*name == '\0') {
1423 * Null component name refers to the directory itself.
1430 /* possible VN_HOLD(zp) */
1433 if (flag & FIGNORECASE)
1436 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1439 if (strcmp(name, "..") == 0)
1449 * Create a new file object and update the directory
1452 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1457 * We only support the creation of regular files in
1458 * extended attribute directories.
1460 if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1461 (vap->va_type != VREG)) {
1467 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
1470 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1475 tx = dmu_tx_create(os);
1476 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1477 fuid_dirtied = zfsvfs->z_fuid_dirty;
1479 zfs_fuid_txhold(zfsvfs, tx);
1480 dmu_tx_hold_bonus(tx, dzp->z_id);
1481 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1482 if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1483 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1484 0, SPA_MAXBLOCKSIZE);
1486 error = dmu_tx_assign(tx, TXG_NOWAIT);
1488 zfs_acl_ids_free(&acl_ids);
1489 zfs_dirent_unlock(dl);
1490 if (error == ERESTART) {
1499 zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
1502 zfs_fuid_sync(zfsvfs, tx);
1504 (void) zfs_link_create(dl, zp, tx, ZNEW);
1506 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1507 if (flag & FIGNORECASE)
1509 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1510 vsecp, acl_ids.z_fuidp, vap);
1511 zfs_acl_ids_free(&acl_ids);
1514 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1517 * A directory entry already exists for this name.
1520 * Can't truncate an existing file if in exclusive mode.
1527 * Can't open a directory for writing.
1529 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1534 * Verify requested access to file.
1536 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1540 mutex_enter(&dzp->z_lock);
1542 mutex_exit(&dzp->z_lock);
1545 * Truncate regular files if requested.
1547 if ((ZTOV(zp)->v_type == VREG) &&
1548 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1549 /* we can't hold any locks when calling zfs_freesp() */
1550 zfs_dirent_unlock(dl);
1552 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1554 vnevent_create(ZTOV(zp), ct);
1560 zfs_dirent_unlock(dl);
1568 * If vnode is for a device return a specfs vnode instead.
1570 if (IS_DEVVP(*vpp)) {
1573 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1587 * Remove an entry from a directory.
1589 * IN: dvp - vnode of directory to remove entry from.
1590 * name - name of entry to remove.
1591 * cr - credentials of caller.
1592 * ct - caller context
1593 * flags - case flags
1595 * RETURN: 0 if success
1596 * error code if failure
1600 * vp - ctime (if nlink > 0)
1604 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1607 znode_t *zp, *dzp = VTOZ(dvp);
1608 znode_t *xzp = NULL;
1610 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1612 uint64_t acl_obj, xattr_obj;
1615 boolean_t may_delete_now, delete_now = FALSE;
1616 boolean_t unlinked, toobig = FALSE;
1618 pathname_t *realnmp = NULL;
1625 zilog = zfsvfs->z_log;
1627 if (flags & FIGNORECASE) {
1635 * Attempt to lock directory; fail if entry doesn't exist.
1637 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1647 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1652 * Need to use rmdir for removing directories.
1654 if (vp->v_type == VDIR) {
1659 vnevent_remove(vp, dvp, name, ct);
1662 dnlc_remove(dvp, realnmp->pn_buf);
1664 dnlc_remove(dvp, name);
1666 may_delete_now = FALSE;
1669 * We may delete the znode now, or we may put it in the unlinked set;
1670 * it depends on whether we're the last link, and on whether there are
1671 * other holds on the vnode. So we dmu_tx_hold() the right things to
1672 * allow for either case.
1674 tx = dmu_tx_create(zfsvfs->z_os);
1675 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1676 dmu_tx_hold_bonus(tx, zp->z_id);
1677 if (may_delete_now) {
1679 zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1680 /* if the file is too big, only hold_free a token amount */
1681 dmu_tx_hold_free(tx, zp->z_id, 0,
1682 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1685 /* are there any extended attributes? */
1686 if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1687 /* XXX - do we need this if we are deleting? */
1688 dmu_tx_hold_bonus(tx, xattr_obj);
1691 /* are there any additional acls */
1692 if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1694 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1696 /* charge as an update -- would be nice not to charge at all */
1697 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1699 error = dmu_tx_assign(tx, TXG_NOWAIT);
1701 zfs_dirent_unlock(dl);
1703 if (error == ERESTART) {
1716 * Remove the directory entry.
1718 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1725 if (0 && unlinked) {
1727 delete_now = may_delete_now && !toobig &&
1728 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1729 zp->z_phys->zp_xattr == xattr_obj &&
1730 zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1735 if (zp->z_phys->zp_xattr) {
1736 error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1737 ASSERT3U(error, ==, 0);
1738 ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1739 dmu_buf_will_dirty(xzp->z_dbuf, tx);
1740 mutex_enter(&xzp->z_lock);
1741 xzp->z_unlinked = 1;
1742 xzp->z_phys->zp_links = 0;
1743 mutex_exit(&xzp->z_lock);
1744 zfs_unlinked_add(xzp, tx);
1745 zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1747 mutex_enter(&zp->z_lock);
1750 ASSERT3U(vp->v_count, ==, 0);
1752 mutex_exit(&zp->z_lock);
1753 zfs_znode_delete(zp, tx);
1754 } else if (unlinked) {
1755 zfs_unlinked_add(zp, tx);
1759 if (flags & FIGNORECASE)
1761 zfs_log_remove(zilog, tx, txtype, dzp, name);
1768 zfs_dirent_unlock(dl);
1773 /* this rele is delayed to prevent nesting transactions */
1782 * Create a new directory and insert it into dvp using the name
1783 * provided. Return a pointer to the inserted directory.
1785 * IN: dvp - vnode of directory to add subdir to.
1786 * dirname - name of new directory.
1787 * vap - attributes of new directory.
1788 * cr - credentials of caller.
1789 * ct - caller context
1790 * vsecp - ACL to be set
1792 * OUT: vpp - vnode of created directory.
1794 * RETURN: 0 if success
1795 * error code if failure
1798 * dvp - ctime|mtime updated
1799 * vp - ctime|mtime|atime updated
1803 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1804 caller_context_t *ct, int flags, vsecattr_t *vsecp)
1806 znode_t *zp, *dzp = VTOZ(dvp);
1807 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1816 gid_t gid = crgetgid(cr);
1817 zfs_acl_ids_t acl_ids;
1818 boolean_t fuid_dirtied;
1820 ASSERT(vap->va_type == VDIR);
1823 * If we have an ephemeral id, ACL, or XVATTR then
1824 * make sure file system is at proper version
1827 ksid = crgetsid(cr, KSID_OWNER);
1829 uid = ksid_getid(ksid);
1832 if (zfsvfs->z_use_fuids == B_FALSE &&
1833 (vsecp || (vap->va_mask & AT_XVATTR) || IS_EPHEMERAL(crgetuid(cr))||
1834 IS_EPHEMERAL(crgetgid(cr))))
1839 zilog = zfsvfs->z_log;
1841 if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1846 if (zfsvfs->z_utf8 && u8_validate(dirname,
1847 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1851 if (flags & FIGNORECASE)
1854 if (vap->va_mask & AT_XVATTR)
1855 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1856 crgetuid(cr), cr, vap->va_type)) != 0) {
1862 * First make sure the new directory doesn't exist.
1867 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1873 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1874 zfs_dirent_unlock(dl);
1879 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
1881 zfs_dirent_unlock(dl);
1885 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1886 zfs_dirent_unlock(dl);
1892 * Add a new entry to the directory.
1894 tx = dmu_tx_create(zfsvfs->z_os);
1895 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1896 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1897 fuid_dirtied = zfsvfs->z_fuid_dirty;
1899 zfs_fuid_txhold(zfsvfs, tx);
1900 if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE)
1901 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1902 0, SPA_MAXBLOCKSIZE);
1903 error = dmu_tx_assign(tx, TXG_NOWAIT);
1905 zfs_acl_ids_free(&acl_ids);
1906 zfs_dirent_unlock(dl);
1907 if (error == ERESTART) {
1920 zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
1923 zfs_fuid_sync(zfsvfs, tx);
1925 * Now put new name in parent dir.
1927 (void) zfs_link_create(dl, zp, tx, ZNEW);
1931 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1932 if (flags & FIGNORECASE)
1934 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1935 acl_ids.z_fuidp, vap);
1937 zfs_acl_ids_free(&acl_ids);
1940 zfs_dirent_unlock(dl);
1947 * Remove a directory subdir entry. If the current working
1948 * directory is the same as the subdir to be removed, the
1951 * IN: dvp - vnode of directory to remove from.
1952 * name - name of directory to be removed.
1953 * cwd - vnode of current working directory.
1954 * cr - credentials of caller.
1955 * ct - caller context
1956 * flags - case flags
1958 * RETURN: 0 if success
1959 * error code if failure
1962 * dvp - ctime|mtime updated
1966 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
1967 caller_context_t *ct, int flags)
1969 znode_t *dzp = VTOZ(dvp);
1972 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1981 zilog = zfsvfs->z_log;
1983 if (flags & FIGNORECASE)
1989 * Attempt to lock directory; fail if entry doesn't exist.
1991 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1999 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2003 if (vp->v_type != VDIR) {
2013 vnevent_rmdir(vp, dvp, name, ct);
2016 * Grab a lock on the directory to make sure that noone is
2017 * trying to add (or lookup) entries while we are removing it.
2019 rw_enter(&zp->z_name_lock, RW_WRITER);
2022 * Grab a lock on the parent pointer to make sure we play well
2023 * with the treewalk and directory rename code.
2025 rw_enter(&zp->z_parent_lock, RW_WRITER);
2027 tx = dmu_tx_create(zfsvfs->z_os);
2028 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2029 dmu_tx_hold_bonus(tx, zp->z_id);
2030 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2031 error = dmu_tx_assign(tx, TXG_NOWAIT);
2033 rw_exit(&zp->z_parent_lock);
2034 rw_exit(&zp->z_name_lock);
2035 zfs_dirent_unlock(dl);
2037 if (error == ERESTART) {
2047 #ifdef FREEBSD_NAMECACHE
2051 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2054 uint64_t txtype = TX_RMDIR;
2055 if (flags & FIGNORECASE)
2057 zfs_log_remove(zilog, tx, txtype, dzp, name);
2062 rw_exit(&zp->z_parent_lock);
2063 rw_exit(&zp->z_name_lock);
2064 #ifdef FREEBSD_NAMECACHE
2068 zfs_dirent_unlock(dl);
2077 * Read as many directory entries as will fit into the provided
2078 * buffer from the given directory cursor position (specified in
2079 * the uio structure.
2081 * IN: vp - vnode of directory to read.
2082 * uio - structure supplying read location, range info,
2083 * and return buffer.
2084 * cr - credentials of caller.
2085 * ct - caller context
2086 * flags - case flags
2088 * OUT: uio - updated offset and range, buffer filled.
2089 * eofp - set to true if end-of-file detected.
2091 * RETURN: 0 if success
2092 * error code if failure
2095 * vp - atime updated
2097 * Note that the low 4 bits of the cookie returned by zap is always zero.
2098 * This allows us to use the low range for "special" directory entries:
2099 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2100 * we use the offset 2 for the '.zfs' directory.
2104 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2106 znode_t *zp = VTOZ(vp);
2110 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2115 zap_attribute_t zap;
2116 uint_t bytes_wanted;
2117 uint64_t offset; /* must be unsigned; checks for < 1 */
2122 boolean_t check_sysattrs;
2125 u_long *cooks = NULL;
2132 * If we are not given an eof variable,
2139 * Check for valid iov_len.
2141 if (uio->uio_iov->iov_len <= 0) {
2147 * Quit if directory has been removed (posix)
2149 if ((*eofp = zp->z_unlinked) != 0) {
2156 offset = uio->uio_loffset;
2157 prefetch = zp->z_zn_prefetch;
2160 * Initialize the iterator cursor.
2164 * Start iteration from the beginning of the directory.
2166 zap_cursor_init(&zc, os, zp->z_id);
2169 * The offset is a serialized cursor.
2171 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2175 * Get space to change directory entries into fs independent format.
2177 iovp = uio->uio_iov;
2178 bytes_wanted = iovp->iov_len;
2179 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2180 bufsize = bytes_wanted;
2181 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2182 odp = (struct dirent64 *)outbuf;
2184 bufsize = bytes_wanted;
2185 odp = (struct dirent64 *)iovp->iov_base;
2187 eodp = (struct edirent *)odp;
2189 if (ncookies != NULL) {
2191 * Minimum entry size is dirent size and 1 byte for a file name.
2193 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2194 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2199 * If this VFS supports the system attribute view interface; and
2200 * we're looking at an extended attribute directory; and we care
2201 * about normalization conflicts on this vfs; then we must check
2202 * for normalization conflicts with the sysattr name space.
2205 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2206 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2207 (flags & V_RDDIR_ENTFLAGS);
2213 * Transform to file-system independent format
2216 while (outcount < bytes_wanted) {
2222 * Special case `.', `..', and `.zfs'.
2225 (void) strcpy(zap.za_name, ".");
2226 zap.za_normalization_conflict = 0;
2229 } else if (offset == 1) {
2230 (void) strcpy(zap.za_name, "..");
2231 zap.za_normalization_conflict = 0;
2232 objnum = zp->z_phys->zp_parent;
2234 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2235 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2236 zap.za_normalization_conflict = 0;
2237 objnum = ZFSCTL_INO_ROOT;
2243 if (error = zap_cursor_retrieve(&zc, &zap)) {
2244 if ((*eofp = (error == ENOENT)) != 0)
2250 if (zap.za_integer_length != 8 ||
2251 zap.za_num_integers != 1) {
2252 cmn_err(CE_WARN, "zap_readdir: bad directory "
2253 "entry, obj = %lld, offset = %lld\n",
2254 (u_longlong_t)zp->z_id,
2255 (u_longlong_t)offset);
2260 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2262 * MacOS X can extract the object type here such as:
2263 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2265 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2267 if (check_sysattrs && !zap.za_normalization_conflict) {
2269 zap.za_normalization_conflict =
2270 xattr_sysattr_casechk(zap.za_name);
2272 panic("%s:%u: TODO", __func__, __LINE__);
2277 if (flags & V_RDDIR_ENTFLAGS)
2278 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2280 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2283 * Will this entry fit in the buffer?
2285 if (outcount + reclen > bufsize) {
2287 * Did we manage to fit anything in the buffer?
2295 if (flags & V_RDDIR_ENTFLAGS) {
2297 * Add extended flag entry:
2299 eodp->ed_ino = objnum;
2300 eodp->ed_reclen = reclen;
2301 /* NOTE: ed_off is the offset for the *next* entry */
2302 next = &(eodp->ed_off);
2303 eodp->ed_eflags = zap.za_normalization_conflict ?
2304 ED_CASE_CONFLICT : 0;
2305 (void) strncpy(eodp->ed_name, zap.za_name,
2306 EDIRENT_NAMELEN(reclen));
2307 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2312 odp->d_ino = objnum;
2313 odp->d_reclen = reclen;
2314 odp->d_namlen = strlen(zap.za_name);
2315 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2317 odp = (dirent64_t *)((intptr_t)odp + reclen);
2321 ASSERT(outcount <= bufsize);
2323 /* Prefetch znode */
2325 dmu_prefetch(os, objnum, 0, 0);
2328 * Move to the next entry, fill in the previous offset.
2330 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2331 zap_cursor_advance(&zc);
2332 offset = zap_cursor_serialize(&zc);
2337 if (cooks != NULL) {
2340 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2343 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2345 /* Subtract unused cookies */
2346 if (ncookies != NULL)
2347 *ncookies -= ncooks;
2349 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2350 iovp->iov_base += outcount;
2351 iovp->iov_len -= outcount;
2352 uio->uio_resid -= outcount;
2353 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2355 * Reset the pointer.
2357 offset = uio->uio_loffset;
2361 zap_cursor_fini(&zc);
2362 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2363 kmem_free(outbuf, bufsize);
2365 if (error == ENOENT)
2368 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2370 uio->uio_loffset = offset;
2372 if (error != 0 && cookies != NULL) {
2373 free(*cookies, M_TEMP);
2380 ulong_t zfs_fsync_sync_cnt = 4;
2383 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2385 znode_t *zp = VTOZ(vp);
2386 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2388 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2392 zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2399 * Get the requested file attributes and place them in the provided
2402 * IN: vp - vnode of file.
2403 * vap - va_mask identifies requested attributes.
2404 * If AT_XVATTR set, then optional attrs are requested
2405 * flags - ATTR_NOACLCHECK (CIFS server context)
2406 * cr - credentials of caller.
2407 * ct - caller context
2409 * OUT: vap - attribute values.
2411 * RETURN: 0 (always succeeds)
2415 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2416 caller_context_t *ct)
2418 znode_t *zp = VTOZ(vp);
2419 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2423 u_longlong_t nblocks;
2425 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2426 xoptattr_t *xoap = NULL;
2427 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2434 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2435 * Also, if we are the owner don't bother, since owner should
2436 * always be allowed to read basic attributes of file.
2438 if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
2439 (pzp->zp_uid != crgetuid(cr))) {
2440 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2448 * Return all attributes. It's cheaper to provide the answer
2449 * than to determine whether we were asked the question.
2452 mutex_enter(&zp->z_lock);
2453 vap->va_type = IFTOVT(pzp->zp_mode);
2454 vap->va_mode = pzp->zp_mode & ~S_IFMT;
2455 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2456 // vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2457 vap->va_nodeid = zp->z_id;
2458 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2459 links = pzp->zp_links + 1;
2461 links = pzp->zp_links;
2462 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2463 vap->va_size = pzp->zp_size;
2464 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2465 vap->va_rdev = zfs_cmpldev(pzp->zp_rdev);
2466 vap->va_seq = zp->z_seq;
2467 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2470 * Add in any requested optional attributes and the create time.
2471 * Also set the corresponding bits in the returned attribute bitmap.
2473 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2474 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2476 ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
2477 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2480 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2481 xoap->xoa_readonly =
2482 ((pzp->zp_flags & ZFS_READONLY) != 0);
2483 XVA_SET_RTN(xvap, XAT_READONLY);
2486 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2488 ((pzp->zp_flags & ZFS_SYSTEM) != 0);
2489 XVA_SET_RTN(xvap, XAT_SYSTEM);
2492 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2494 ((pzp->zp_flags & ZFS_HIDDEN) != 0);
2495 XVA_SET_RTN(xvap, XAT_HIDDEN);
2498 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2499 xoap->xoa_nounlink =
2500 ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
2501 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2504 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2505 xoap->xoa_immutable =
2506 ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
2507 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2510 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2511 xoap->xoa_appendonly =
2512 ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
2513 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2516 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2518 ((pzp->zp_flags & ZFS_NODUMP) != 0);
2519 XVA_SET_RTN(xvap, XAT_NODUMP);
2522 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2524 ((pzp->zp_flags & ZFS_OPAQUE) != 0);
2525 XVA_SET_RTN(xvap, XAT_OPAQUE);
2528 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2529 xoap->xoa_av_quarantined =
2530 ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
2531 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2534 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2535 xoap->xoa_av_modified =
2536 ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
2537 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2540 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2541 vp->v_type == VREG &&
2542 (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
2544 dmu_object_info_t doi;
2547 * Only VREG files have anti-virus scanstamps, so we
2548 * won't conflict with symlinks in the bonus buffer.
2550 dmu_object_info_from_db(zp->z_dbuf, &doi);
2551 len = sizeof (xoap->xoa_av_scanstamp) +
2552 sizeof (znode_phys_t);
2553 if (len <= doi.doi_bonus_size) {
2555 * pzp points to the start of the
2556 * znode_phys_t. pzp + 1 points to the
2557 * first byte after the znode_phys_t.
2559 (void) memcpy(xoap->xoa_av_scanstamp,
2561 sizeof (xoap->xoa_av_scanstamp));
2562 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
2566 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2567 ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
2568 XVA_SET_RTN(xvap, XAT_CREATETIME);
2572 ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
2573 ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
2574 ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
2575 ZFS_TIME_DECODE(&vap->va_birthtime, pzp->zp_crtime);
2577 mutex_exit(&zp->z_lock);
2579 dmu_object_size_from_db(zp->z_dbuf, &blksize, &nblocks);
2580 vap->va_blksize = blksize;
2581 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2583 if (zp->z_blksz == 0) {
2585 * Block size hasn't been set; suggest maximal I/O transfers.
2587 vap->va_blksize = zfsvfs->z_max_blksz;
2595 * Set the file attributes to the values contained in the
2598 * IN: vp - vnode of file to be modified.
2599 * vap - new attribute values.
2600 * If AT_XVATTR set, then optional attrs are being set
2601 * flags - ATTR_UTIME set if non-default time values provided.
2602 * - ATTR_NOACLCHECK (CIFS context only).
2603 * cr - credentials of caller.
2604 * ct - caller context
2606 * RETURN: 0 if success
2607 * error code if failure
2610 * vp - ctime updated, mtime updated if size changed.
2614 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2615 caller_context_t *ct)
2617 znode_t *zp = VTOZ(vp);
2619 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2624 uint_t mask = vap->va_mask;
2626 uint64_t saved_mode;
2629 uint64_t new_uid, new_gid;
2631 int need_policy = FALSE;
2633 zfs_fuid_info_t *fuidp = NULL;
2634 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2636 zfs_acl_t *aclp = NULL;
2637 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2638 boolean_t fuid_dirtied = B_FALSE;
2643 if (mask & AT_NOSET)
2650 zilog = zfsvfs->z_log;
2653 * Make sure that if we have ephemeral uid/gid or xvattr specified
2654 * that file system is at proper version level
2657 if (zfsvfs->z_use_fuids == B_FALSE &&
2658 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2659 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2660 (mask & AT_XVATTR))) {
2665 if (mask & AT_SIZE && vp->v_type == VDIR) {
2670 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2676 * If this is an xvattr_t, then get a pointer to the structure of
2677 * optional attributes. If this is NULL, then we have a vattr_t.
2679 xoap = xva_getxoptattr(xvap);
2681 xva_init(&tmpxvattr);
2684 * Immutable files can only alter immutable bit and atime
2686 if ((pzp->zp_flags & ZFS_IMMUTABLE) &&
2687 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2688 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2693 if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) {
2699 * Verify timestamps doesn't overflow 32 bits.
2700 * ZFS can handle large timestamps, but 32bit syscalls can't
2701 * handle times greater than 2039. This check should be removed
2702 * once large timestamps are fully supported.
2704 if (mask & (AT_ATIME | AT_MTIME)) {
2705 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2706 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2715 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2721 * First validate permissions
2724 if (mask & AT_SIZE) {
2725 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2731 * XXX - Note, we are not providing any open
2732 * mode flags here (like FNDELAY), so we may
2733 * block if there are locks present... this
2734 * should be addressed in openat().
2736 /* XXX - would it be OK to generate a log record here? */
2737 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2744 if (mask & (AT_ATIME|AT_MTIME) ||
2745 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2746 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2747 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2748 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2749 XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
2750 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2753 if (mask & (AT_UID|AT_GID)) {
2754 int idmask = (mask & (AT_UID|AT_GID));
2759 * NOTE: even if a new mode is being set,
2760 * we may clear S_ISUID/S_ISGID bits.
2763 if (!(mask & AT_MODE))
2764 vap->va_mode = pzp->zp_mode;
2767 * Take ownership or chgrp to group we are a member of
2770 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2771 take_group = (mask & AT_GID) &&
2772 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2775 * If both AT_UID and AT_GID are set then take_owner and
2776 * take_group must both be set in order to allow taking
2779 * Otherwise, send the check through secpolicy_vnode_setattr()
2783 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2784 ((idmask == AT_UID) && take_owner) ||
2785 ((idmask == AT_GID) && take_group)) {
2786 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2787 skipaclchk, cr) == 0) {
2789 * Remove setuid/setgid for non-privileged users
2791 secpolicy_setid_clear(vap, vp, cr);
2792 trim_mask = (mask & (AT_UID|AT_GID));
2801 mutex_enter(&zp->z_lock);
2802 oldva.va_mode = pzp->zp_mode;
2803 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2804 if (mask & AT_XVATTR) {
2806 * Update xvattr mask to include only those attributes
2807 * that are actually changing.
2809 * the bits will be restored prior to actually setting
2810 * the attributes so the caller thinks they were set.
2812 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2813 if (xoap->xoa_appendonly !=
2814 ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) {
2817 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2818 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2822 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2823 if (xoap->xoa_nounlink !=
2824 ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) {
2827 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2828 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2832 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2833 if (xoap->xoa_immutable !=
2834 ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) {
2837 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2838 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2842 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2843 if (xoap->xoa_nodump !=
2844 ((pzp->zp_flags & ZFS_NODUMP) != 0)) {
2847 XVA_CLR_REQ(xvap, XAT_NODUMP);
2848 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2852 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2853 if (xoap->xoa_av_modified !=
2854 ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) {
2857 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2858 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2862 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2863 if ((vp->v_type != VREG &&
2864 xoap->xoa_av_quarantined) ||
2865 xoap->xoa_av_quarantined !=
2866 ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) {
2869 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2870 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2874 if (need_policy == FALSE &&
2875 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2876 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2881 mutex_exit(&zp->z_lock);
2883 if (mask & AT_MODE) {
2884 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2885 err = secpolicy_setid_setsticky_clear(vp, vap,
2891 trim_mask |= AT_MODE;
2899 * If trim_mask is set then take ownership
2900 * has been granted or write_acl is present and user
2901 * has the ability to modify mode. In that case remove
2902 * UID|GID and or MODE from mask so that
2903 * secpolicy_vnode_setattr() doesn't revoke it.
2907 saved_mask = vap->va_mask;
2908 vap->va_mask &= ~trim_mask;
2909 if (trim_mask & AT_MODE) {
2911 * Save the mode, as secpolicy_vnode_setattr()
2912 * will overwrite it with ova.va_mode.
2914 saved_mode = vap->va_mode;
2917 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2918 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2925 vap->va_mask |= saved_mask;
2926 if (trim_mask & AT_MODE) {
2928 * Recover the mode after
2929 * secpolicy_vnode_setattr().
2931 vap->va_mode = saved_mode;
2937 * secpolicy_vnode_setattr, or take ownership may have
2940 mask = vap->va_mask;
2942 tx = dmu_tx_create(zfsvfs->z_os);
2943 dmu_tx_hold_bonus(tx, zp->z_id);
2945 if (mask & AT_MODE) {
2946 uint64_t pmode = pzp->zp_mode;
2948 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2950 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
2952 if (pzp->zp_acl.z_acl_extern_obj) {
2953 /* Are we upgrading ACL from old V0 format to new V1 */
2954 if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
2955 pzp->zp_acl.z_acl_version ==
2956 ZFS_ACL_VERSION_INITIAL) {
2957 dmu_tx_hold_free(tx,
2958 pzp->zp_acl.z_acl_extern_obj, 0,
2960 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2961 0, aclp->z_acl_bytes);
2963 dmu_tx_hold_write(tx,
2964 pzp->zp_acl.z_acl_extern_obj, 0,
2967 } else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2968 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2969 0, aclp->z_acl_bytes);
2973 if (mask & (AT_UID | AT_GID)) {
2974 if (pzp->zp_xattr) {
2975 err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
2978 dmu_tx_hold_bonus(tx, attrzp->z_id);
2980 if (mask & AT_UID) {
2981 new_uid = zfs_fuid_create(zfsvfs,
2982 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2983 if (new_uid != pzp->zp_uid &&
2984 zfs_usergroup_overquota(zfsvfs, B_FALSE, new_uid)) {
2990 if (mask & AT_GID) {
2991 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
2992 cr, ZFS_GROUP, &fuidp);
2993 if (new_gid != pzp->zp_gid &&
2994 zfs_usergroup_overquota(zfsvfs, B_TRUE, new_gid)) {
2999 fuid_dirtied = zfsvfs->z_fuid_dirty;
3001 if (zfsvfs->z_fuid_obj == 0) {
3002 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
3003 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3004 FUID_SIZE_ESTIMATE(zfsvfs));
3005 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ,
3008 dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
3009 dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
3010 FUID_SIZE_ESTIMATE(zfsvfs));
3015 err = dmu_tx_assign(tx, TXG_NOWAIT);
3017 if (err == ERESTART)
3022 dmu_buf_will_dirty(zp->z_dbuf, tx);
3025 * Set each attribute requested.
3026 * We group settings according to the locks they need to acquire.
3028 * Note: you cannot set ctime directly, although it will be
3029 * updated as a side-effect of calling this function.
3032 mutex_enter(&zp->z_lock);
3034 if (mask & AT_MODE) {
3035 mutex_enter(&zp->z_acl_lock);
3036 zp->z_phys->zp_mode = new_mode;
3037 err = zfs_aclset_common(zp, aclp, cr, tx);
3038 ASSERT3U(err, ==, 0);
3039 mutex_exit(&zp->z_acl_lock);
3043 mutex_enter(&attrzp->z_lock);
3045 if (mask & AT_UID) {
3046 pzp->zp_uid = new_uid;
3048 attrzp->z_phys->zp_uid = new_uid;
3051 if (mask & AT_GID) {
3052 pzp->zp_gid = new_gid;
3054 attrzp->z_phys->zp_gid = new_gid;
3058 mutex_exit(&attrzp->z_lock);
3060 if (mask & AT_ATIME)
3061 ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
3063 if (mask & AT_MTIME)
3064 ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
3066 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3068 zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
3070 zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
3072 * Do this after setting timestamps to prevent timestamp
3073 * update from toggling bit
3076 if (xoap && (mask & AT_XVATTR)) {
3079 * restore trimmed off masks
3080 * so that return masks can be set for caller.
3083 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3084 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3086 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3087 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3089 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3090 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3092 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3093 XVA_SET_REQ(xvap, XAT_NODUMP);
3095 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3096 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3098 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3099 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3102 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
3104 dmu_object_info_t doi;
3106 ASSERT(vp->v_type == VREG);
3108 /* Grow the bonus buffer if necessary. */
3109 dmu_object_info_from_db(zp->z_dbuf, &doi);
3110 len = sizeof (xoap->xoa_av_scanstamp) +
3111 sizeof (znode_phys_t);
3112 if (len > doi.doi_bonus_size)
3113 VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
3115 zfs_xvattr_set(zp, xvap);
3119 zfs_fuid_sync(zfsvfs, tx);
3122 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3124 mutex_exit(&zp->z_lock);
3128 VN_RELE(ZTOV(attrzp));
3136 zfs_fuid_info_free(fuidp);
3145 if (err == ERESTART)
3152 typedef struct zfs_zlock {
3153 krwlock_t *zl_rwlock; /* lock we acquired */
3154 znode_t *zl_znode; /* znode we held */
3155 struct zfs_zlock *zl_next; /* next in list */
3159 * Drop locks and release vnodes that were held by zfs_rename_lock().
3162 zfs_rename_unlock(zfs_zlock_t **zlpp)
3166 while ((zl = *zlpp) != NULL) {
3167 if (zl->zl_znode != NULL)
3168 VN_RELE(ZTOV(zl->zl_znode));
3169 rw_exit(zl->zl_rwlock);
3170 *zlpp = zl->zl_next;
3171 kmem_free(zl, sizeof (*zl));
3176 * Search back through the directory tree, using the ".." entries.
3177 * Lock each directory in the chain to prevent concurrent renames.
3178 * Fail any attempt to move a directory into one of its own descendants.
3179 * XXX - z_parent_lock can overlap with map or grow locks
3182 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3186 uint64_t rootid = zp->z_zfsvfs->z_root;
3187 uint64_t *oidp = &zp->z_id;
3188 krwlock_t *rwlp = &szp->z_parent_lock;
3189 krw_t rw = RW_WRITER;
3192 * First pass write-locks szp and compares to zp->z_id.
3193 * Later passes read-lock zp and compare to zp->z_parent.
3196 if (!rw_tryenter(rwlp, rw)) {
3198 * Another thread is renaming in this path.
3199 * Note that if we are a WRITER, we don't have any
3200 * parent_locks held yet.
3202 if (rw == RW_READER && zp->z_id > szp->z_id) {
3204 * Drop our locks and restart
3206 zfs_rename_unlock(&zl);
3210 rwlp = &szp->z_parent_lock;
3215 * Wait for other thread to drop its locks
3221 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3222 zl->zl_rwlock = rwlp;
3223 zl->zl_znode = NULL;
3224 zl->zl_next = *zlpp;
3227 if (*oidp == szp->z_id) /* We're a descendant of szp */
3230 if (*oidp == rootid) /* We've hit the top */
3233 if (rw == RW_READER) { /* i.e. not the first pass */
3234 int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
3239 oidp = &zp->z_phys->zp_parent;
3240 rwlp = &zp->z_parent_lock;
3243 } while (zp->z_id != sdzp->z_id);
3249 * Move an entry from the provided source directory to the target
3250 * directory. Change the entry name as indicated.
3252 * IN: sdvp - Source directory containing the "old entry".
3253 * snm - Old entry name.
3254 * tdvp - Target directory to contain the "new entry".
3255 * tnm - New entry name.
3256 * cr - credentials of caller.
3257 * ct - caller context
3258 * flags - case flags
3260 * RETURN: 0 if success
3261 * error code if failure
3264 * sdvp,tdvp - ctime|mtime updated
3268 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3269 caller_context_t *ct, int flags)
3271 znode_t *tdzp, *szp, *tzp;
3272 znode_t *sdzp = VTOZ(sdvp);
3273 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3276 zfs_dirlock_t *sdl, *tdl;
3279 int cmp, serr, terr;
3284 ZFS_VERIFY_ZP(sdzp);
3285 zilog = zfsvfs->z_log;
3288 * Make sure we have the real vp for the target directory.
3290 if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3293 if (tdvp->v_vfsp != sdvp->v_vfsp) {
3299 ZFS_VERIFY_ZP(tdzp);
3300 if (zfsvfs->z_utf8 && u8_validate(tnm,
3301 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3306 if (flags & FIGNORECASE)
3315 * This is to prevent the creation of links into attribute space
3316 * by renaming a linked file into/outof an attribute directory.
3317 * See the comment in zfs_link() for why this is considered bad.
3319 if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
3320 (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
3326 * Lock source and target directory entries. To prevent deadlock,
3327 * a lock ordering must be defined. We lock the directory with
3328 * the smallest object id first, or if it's a tie, the one with
3329 * the lexically first name.
3331 if (sdzp->z_id < tdzp->z_id) {
3333 } else if (sdzp->z_id > tdzp->z_id) {
3337 * First compare the two name arguments without
3338 * considering any case folding.
3340 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3342 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3343 ASSERT(error == 0 || !zfsvfs->z_utf8);
3346 * POSIX: "If the old argument and the new argument
3347 * both refer to links to the same existing file,
3348 * the rename() function shall return successfully
3349 * and perform no other action."
3355 * If the file system is case-folding, then we may
3356 * have some more checking to do. A case-folding file
3357 * system is either supporting mixed case sensitivity
3358 * access or is completely case-insensitive. Note
3359 * that the file system is always case preserving.
3361 * In mixed sensitivity mode case sensitive behavior
3362 * is the default. FIGNORECASE must be used to
3363 * explicitly request case insensitive behavior.
3365 * If the source and target names provided differ only
3366 * by case (e.g., a request to rename 'tim' to 'Tim'),
3367 * we will treat this as a special case in the
3368 * case-insensitive mode: as long as the source name
3369 * is an exact match, we will allow this to proceed as
3370 * a name-change request.
3372 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3373 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3374 flags & FIGNORECASE)) &&
3375 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3378 * case preserving rename request, require exact
3387 * If the source and destination directories are the same, we should
3388 * grab the z_name_lock of that directory only once.
3392 rw_enter(&sdzp->z_name_lock, RW_READER);
3396 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3397 ZEXISTS | zflg, NULL, NULL);
3398 terr = zfs_dirent_lock(&tdl,
3399 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3401 terr = zfs_dirent_lock(&tdl,
3402 tdzp, tnm, &tzp, zflg, NULL, NULL);
3403 serr = zfs_dirent_lock(&sdl,
3404 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3410 * Source entry invalid or not there.
3413 zfs_dirent_unlock(tdl);
3419 rw_exit(&sdzp->z_name_lock);
3421 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0)
3427 zfs_dirent_unlock(sdl);
3431 rw_exit(&sdzp->z_name_lock);
3433 if (strcmp(tnm, "..") == 0)
3440 * Must have write access at the source to remove the old entry
3441 * and write access at the target to create the new entry.
3442 * Note that if target and source are the same, this can be
3443 * done in a single check.
3446 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3449 if (ZTOV(szp)->v_type == VDIR) {
3451 * Check to make sure rename is valid.
3452 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3454 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3459 * Does target exist?
3463 * Source and target must be the same type.
3465 if (ZTOV(szp)->v_type == VDIR) {
3466 if (ZTOV(tzp)->v_type != VDIR) {
3471 if (ZTOV(tzp)->v_type == VDIR) {
3477 * POSIX dictates that when the source and target
3478 * entries refer to the same file object, rename
3479 * must do nothing and exit without error.
3481 if (szp->z_id == tzp->z_id) {
3487 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3489 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3492 * notify the target directory if it is not the same
3493 * as source directory.
3496 vnevent_rename_dest_dir(tdvp, ct);
3499 tx = dmu_tx_create(zfsvfs->z_os);
3500 dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */
3501 dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */
3502 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3503 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3505 dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */
3507 dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */
3508 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3509 error = dmu_tx_assign(tx, TXG_NOWAIT);
3512 zfs_rename_unlock(&zl);
3513 zfs_dirent_unlock(sdl);
3514 zfs_dirent_unlock(tdl);
3517 rw_exit(&sdzp->z_name_lock);
3522 if (error == ERESTART) {
3532 if (tzp) /* Attempt to remove the existing target */
3533 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3536 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3538 szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
3540 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3543 zfs_log_rename(zilog, tx,
3544 TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
3545 sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
3547 /* Update path information for the target vnode */
3548 vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3550 #ifdef FREEBSD_NAMECACHE
3561 zfs_rename_unlock(&zl);
3563 zfs_dirent_unlock(sdl);
3564 zfs_dirent_unlock(tdl);
3567 rw_exit(&sdzp->z_name_lock);
3579 * Insert the indicated symbolic reference entry into the directory.
3581 * IN: dvp - Directory to contain new symbolic link.
3582 * link - Name for new symlink entry.
3583 * vap - Attributes of new entry.
3584 * target - Target path of new symlink.
3585 * cr - credentials of caller.
3586 * ct - caller context
3587 * flags - case flags
3589 * RETURN: 0 if success
3590 * error code if failure
3593 * dvp - ctime|mtime updated
3597 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
3598 cred_t *cr, kthread_t *td)
3600 znode_t *zp, *dzp = VTOZ(dvp);
3603 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3605 int len = strlen(link);
3608 zfs_acl_ids_t acl_ids;
3609 boolean_t fuid_dirtied;
3612 ASSERT(vap->va_type == VLNK);
3616 zilog = zfsvfs->z_log;
3618 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3619 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3623 if (flags & FIGNORECASE)
3626 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3631 if (len > MAXPATHLEN) {
3633 return (ENAMETOOLONG);
3637 * Attempt to lock directory; fail if entry already exists.
3639 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3645 VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids));
3646 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3647 zfs_acl_ids_free(&acl_ids);
3648 zfs_dirent_unlock(dl);
3652 tx = dmu_tx_create(zfsvfs->z_os);
3653 fuid_dirtied = zfsvfs->z_fuid_dirty;
3654 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3655 dmu_tx_hold_bonus(tx, dzp->z_id);
3656 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3657 if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE)
3658 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
3660 zfs_fuid_txhold(zfsvfs, tx);
3661 error = dmu_tx_assign(tx, TXG_NOWAIT);
3663 zfs_acl_ids_free(&acl_ids);
3664 zfs_dirent_unlock(dl);
3665 if (error == ERESTART) {
3675 dmu_buf_will_dirty(dzp->z_dbuf, tx);
3678 * Create a new object for the symlink.
3679 * Put the link content into bonus buffer if it will fit;
3680 * otherwise, store it just like any other file data.
3682 if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
3683 zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, &acl_ids);
3685 bcopy(link, zp->z_phys + 1, len);
3689 zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
3692 zfs_fuid_sync(zfsvfs, tx);
3694 * Nothing can access the znode yet so no locking needed
3695 * for growing the znode's blocksize.
3697 zfs_grow_blocksize(zp, len, tx);
3699 VERIFY(0 == dmu_buf_hold(zfsvfs->z_os,
3700 zp->z_id, 0, FTAG, &dbp));
3701 dmu_buf_will_dirty(dbp, tx);
3703 ASSERT3U(len, <=, dbp->db_size);
3704 bcopy(link, dbp->db_data, len);
3705 dmu_buf_rele(dbp, FTAG);
3707 zp->z_phys->zp_size = len;
3710 * Insert the new object into the directory.
3712 (void) zfs_link_create(dl, zp, tx, ZNEW);
3714 uint64_t txtype = TX_SYMLINK;
3715 if (flags & FIGNORECASE)
3717 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3721 zfs_acl_ids_free(&acl_ids);
3725 zfs_dirent_unlock(dl);
3732 * Return, in the buffer contained in the provided uio structure,
3733 * the symbolic path referred to by vp.
3735 * IN: vp - vnode of symbolic link.
3736 * uoip - structure to contain the link path.
3737 * cr - credentials of caller.
3738 * ct - caller context
3740 * OUT: uio - structure to contain the link path.
3742 * RETURN: 0 if success
3743 * error code if failure
3746 * vp - atime updated
3750 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3752 znode_t *zp = VTOZ(vp);
3753 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3760 bufsz = (size_t)zp->z_phys->zp_size;
3761 if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
3762 error = uiomove(zp->z_phys + 1,
3763 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3766 error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
3771 error = uiomove(dbp->db_data,
3772 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3773 dmu_buf_rele(dbp, FTAG);
3776 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3782 * Insert a new entry into directory tdvp referencing svp.
3784 * IN: tdvp - Directory to contain new entry.
3785 * svp - vnode of new entry.
3786 * name - name of new entry.
3787 * cr - credentials of caller.
3788 * ct - caller context
3790 * RETURN: 0 if success
3791 * error code if failure
3794 * tdvp - ctime|mtime updated
3795 * svp - ctime updated
3799 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
3800 caller_context_t *ct, int flags)
3802 znode_t *dzp = VTOZ(tdvp);
3804 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3813 ASSERT(tdvp->v_type == VDIR);
3817 zilog = zfsvfs->z_log;
3819 if (VOP_REALVP(svp, &realvp, ct) == 0)
3822 if (svp->v_vfsp != tdvp->v_vfsp) {
3829 if (zfsvfs->z_utf8 && u8_validate(name,
3830 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3834 if (flags & FIGNORECASE)
3839 * We do not support links between attributes and non-attributes
3840 * because of the potential security risk of creating links
3841 * into "normal" file space in order to circumvent restrictions
3842 * imposed in attribute space.
3844 if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
3845 (dzp->z_phys->zp_flags & ZFS_XATTR)) {
3851 * POSIX dictates that we return EPERM here.
3852 * Better choices include ENOTSUP or EISDIR.
3854 if (svp->v_type == VDIR) {
3859 owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER);
3860 if (owner != crgetuid(cr) &&
3861 secpolicy_basic_link(svp, cr) != 0) {
3866 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3872 * Attempt to lock directory; fail if entry already exists.
3874 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3880 tx = dmu_tx_create(zfsvfs->z_os);
3881 dmu_tx_hold_bonus(tx, szp->z_id);
3882 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3883 error = dmu_tx_assign(tx, TXG_NOWAIT);
3885 zfs_dirent_unlock(dl);
3886 if (error == ERESTART) {
3896 error = zfs_link_create(dl, szp, tx, 0);
3899 uint64_t txtype = TX_LINK;
3900 if (flags & FIGNORECASE)
3902 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3907 zfs_dirent_unlock(dl);
3910 vnevent_link(svp, ct);
3919 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3921 znode_t *zp = VTOZ(vp);
3922 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3925 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
3926 if (zp->z_dbuf == NULL) {
3928 * The fs has been unmounted, or we did a
3929 * suspend/resume and this file no longer exists.
3932 vp->v_count = 0; /* count arrives as 1 */
3934 vrecycle(vp, curthread);
3935 rw_exit(&zfsvfs->z_teardown_inactive_lock);
3939 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3940 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3942 dmu_tx_hold_bonus(tx, zp->z_id);
3943 error = dmu_tx_assign(tx, TXG_WAIT);
3947 dmu_buf_will_dirty(zp->z_dbuf, tx);
3948 mutex_enter(&zp->z_lock);
3949 zp->z_atime_dirty = 0;
3950 mutex_exit(&zp->z_lock);
3956 rw_exit(&zfsvfs->z_teardown_inactive_lock);
3959 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
3960 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
3964 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3966 znode_t *zp = VTOZ(vp);
3967 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3969 uint64_t object = zp->z_id;
3975 gen = (uint32_t)zp->z_gen;
3977 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3978 fidp->fid_len = size;
3980 zfid = (zfid_short_t *)fidp;
3982 zfid->zf_len = size;
3984 for (i = 0; i < sizeof (zfid->zf_object); i++)
3985 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3987 /* Must have a non-zero generation number to distinguish from .zfs */
3990 for (i = 0; i < sizeof (zfid->zf_gen); i++)
3991 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3993 if (size == LONG_FID_LEN) {
3994 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
3997 zlfid = (zfid_long_t *)fidp;
3999 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4000 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4002 /* XXX - this should be the generation number for the objset */
4003 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4004 zlfid->zf_setgen[i] = 0;
4012 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4013 caller_context_t *ct)
4025 case _PC_FILESIZEBITS:
4030 case _PC_XATTR_EXISTS:
4032 zfsvfs = zp->z_zfsvfs;
4036 error = zfs_dirent_lock(&dl, zp, "", &xzp,
4037 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4039 zfs_dirent_unlock(dl);
4040 if (!zfs_dirempty(xzp))
4043 } else if (error == ENOENT) {
4045 * If there aren't extended attributes, it's the
4046 * same as having zero of them.
4054 case _PC_ACL_EXTENDED:
4062 case _PC_ACL_PATH_MAX:
4063 *valp = ACL_MAX_ENTRIES;
4066 case _PC_MIN_HOLE_SIZE:
4067 *valp = (int)SPA_MINBLOCKSIZE;
4071 return (EOPNOTSUPP);
4077 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4078 caller_context_t *ct)
4080 znode_t *zp = VTOZ(vp);
4081 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4083 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4087 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4095 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4096 caller_context_t *ct)
4098 znode_t *zp = VTOZ(vp);
4099 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4101 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4105 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4111 zfs_freebsd_open(ap)
4112 struct vop_open_args /* {
4115 struct ucred *a_cred;
4116 struct thread *a_td;
4119 vnode_t *vp = ap->a_vp;
4120 znode_t *zp = VTOZ(vp);
4123 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
4125 vnode_create_vobject(vp, zp->z_phys->zp_size, ap->a_td);
4130 zfs_freebsd_close(ap)
4131 struct vop_close_args /* {
4134 struct ucred *a_cred;
4135 struct thread *a_td;
4139 return (zfs_close(ap->a_vp, ap->a_fflag, 0, 0, ap->a_cred, NULL));
4143 zfs_freebsd_ioctl(ap)
4144 struct vop_ioctl_args /* {
4154 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4155 ap->a_fflag, ap->a_cred, NULL, NULL));
4159 zfs_freebsd_read(ap)
4160 struct vop_read_args /* {
4164 struct ucred *a_cred;
4168 return (zfs_read(ap->a_vp, ap->a_uio, ap->a_ioflag, ap->a_cred, NULL));
4172 zfs_freebsd_write(ap)
4173 struct vop_write_args /* {
4177 struct ucred *a_cred;
4181 return (zfs_write(ap->a_vp, ap->a_uio, ap->a_ioflag, ap->a_cred, NULL));
4185 zfs_freebsd_access(ap)
4186 struct vop_access_args /* {
4188 accmode_t a_accmode;
4189 struct ucred *a_cred;
4190 struct thread *a_td;
4197 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4199 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4201 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
4204 * VADMIN has to be handled by vaccess().
4207 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4209 vnode_t *vp = ap->a_vp;
4210 znode_t *zp = VTOZ(vp);
4211 znode_phys_t *zphys = zp->z_phys;
4213 error = vaccess(vp->v_type, zphys->zp_mode,
4214 zphys->zp_uid, zphys->zp_gid, accmode, ap->a_cred,
4223 zfs_freebsd_lookup(ap)
4224 struct vop_lookup_args /* {
4225 struct vnode *a_dvp;
4226 struct vnode **a_vpp;
4227 struct componentname *a_cnp;
4230 struct componentname *cnp = ap->a_cnp;
4231 char nm[NAME_MAX + 1];
4233 ASSERT(cnp->cn_namelen < sizeof(nm));
4234 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
4236 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4237 cnp->cn_cred, cnp->cn_thread, 0));
4241 zfs_freebsd_create(ap)
4242 struct vop_create_args /* {
4243 struct vnode *a_dvp;
4244 struct vnode **a_vpp;
4245 struct componentname *a_cnp;
4246 struct vattr *a_vap;
4249 struct componentname *cnp = ap->a_cnp;
4250 vattr_t *vap = ap->a_vap;
4253 ASSERT(cnp->cn_flags & SAVENAME);
4255 vattr_init_mask(vap);
4256 mode = vap->va_mode & ALLPERMS;
4258 return (zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
4259 ap->a_vpp, cnp->cn_cred, cnp->cn_thread));
4263 zfs_freebsd_remove(ap)
4264 struct vop_remove_args /* {
4265 struct vnode *a_dvp;
4267 struct componentname *a_cnp;
4271 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4273 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr,
4274 ap->a_cnp->cn_cred, NULL, 0));
4278 zfs_freebsd_mkdir(ap)
4279 struct vop_mkdir_args /* {
4280 struct vnode *a_dvp;
4281 struct vnode **a_vpp;
4282 struct componentname *a_cnp;
4283 struct vattr *a_vap;
4286 vattr_t *vap = ap->a_vap;
4288 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4290 vattr_init_mask(vap);
4292 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
4293 ap->a_cnp->cn_cred, NULL, 0, NULL));
4297 zfs_freebsd_rmdir(ap)
4298 struct vop_rmdir_args /* {
4299 struct vnode *a_dvp;
4301 struct componentname *a_cnp;
4304 struct componentname *cnp = ap->a_cnp;
4306 ASSERT(cnp->cn_flags & SAVENAME);
4308 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0));
4312 zfs_freebsd_readdir(ap)
4313 struct vop_readdir_args /* {
4316 struct ucred *a_cred;
4323 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
4324 ap->a_ncookies, ap->a_cookies));
4328 zfs_freebsd_fsync(ap)
4329 struct vop_fsync_args /* {
4332 struct thread *a_td;
4337 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
4341 zfs_freebsd_getattr(ap)
4342 struct vop_getattr_args /* {
4344 struct vattr *a_vap;
4345 struct ucred *a_cred;
4346 struct thread *a_td;
4349 vattr_t *vap = ap->a_vap;
4355 xvap.xva_vattr = *vap;
4356 xvap.xva_vattr.va_mask |= AT_XVATTR;
4358 /* Convert chflags into ZFS-type flags. */
4359 /* XXX: what about SF_SETTABLE?. */
4360 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
4361 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
4362 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
4363 XVA_SET_REQ(&xvap, XAT_NODUMP);
4364 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
4368 /* Convert ZFS xattr into chflags. */
4369 #define FLAG_CHECK(fflag, xflag, xfield) do { \
4370 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
4371 fflags |= (fflag); \
4373 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
4374 xvap.xva_xoptattrs.xoa_immutable);
4375 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
4376 xvap.xva_xoptattrs.xoa_appendonly);
4377 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
4378 xvap.xva_xoptattrs.xoa_nounlink);
4379 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
4380 xvap.xva_xoptattrs.xoa_nodump);
4382 *vap = xvap.xva_vattr;
4383 vap->va_flags = fflags;
4388 zfs_freebsd_setattr(ap)
4389 struct vop_setattr_args /* {
4391 struct vattr *a_vap;
4392 struct ucred *a_cred;
4393 struct thread *a_td;
4396 vnode_t *vp = ap->a_vp;
4397 vattr_t *vap = ap->a_vap;
4398 cred_t *cred = ap->a_cred;
4403 vattr_init_mask(vap);
4404 vap->va_mask &= ~AT_NOSET;
4407 xvap.xva_vattr = *vap;
4409 zflags = VTOZ(vp)->z_phys->zp_flags;
4411 if (vap->va_flags != VNOVAL) {
4412 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
4415 if (zfsvfs->z_use_fuids == B_FALSE)
4416 return (EOPNOTSUPP);
4418 fflags = vap->va_flags;
4419 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_NODUMP)) != 0)
4420 return (EOPNOTSUPP);
4422 * Unprivileged processes are not permitted to unset system
4423 * flags, or modify flags if any system flags are set.
4424 * Privileged non-jail processes may not modify system flags
4425 * if securelevel > 0 and any existing system flags are set.
4426 * Privileged jail processes behave like privileged non-jail
4427 * processes if the security.jail.chflags_allowed sysctl is
4428 * is non-zero; otherwise, they behave like unprivileged
4431 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
4432 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
4434 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4435 error = securelevel_gt(cred, 0);
4441 * Callers may only modify the file flags on objects they
4442 * have VADMIN rights for.
4444 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
4447 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4451 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
4456 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
4457 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
4458 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
4459 XVA_SET_REQ(&xvap, (xflag)); \
4460 (xfield) = ((fflags & (fflag)) != 0); \
4463 /* Convert chflags into ZFS-type flags. */
4464 /* XXX: what about SF_SETTABLE?. */
4465 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
4466 xvap.xva_xoptattrs.xoa_immutable);
4467 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
4468 xvap.xva_xoptattrs.xoa_appendonly);
4469 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
4470 xvap.xva_xoptattrs.xoa_nounlink);
4471 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
4472 xvap.xva_xoptattrs.xoa_nodump);
4475 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
4479 zfs_freebsd_rename(ap)
4480 struct vop_rename_args /* {
4481 struct vnode *a_fdvp;
4482 struct vnode *a_fvp;
4483 struct componentname *a_fcnp;
4484 struct vnode *a_tdvp;
4485 struct vnode *a_tvp;
4486 struct componentname *a_tcnp;
4489 vnode_t *fdvp = ap->a_fdvp;
4490 vnode_t *fvp = ap->a_fvp;
4491 vnode_t *tdvp = ap->a_tdvp;
4492 vnode_t *tvp = ap->a_tvp;
4495 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
4496 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
4498 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp,
4499 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0);
4514 zfs_freebsd_symlink(ap)
4515 struct vop_symlink_args /* {
4516 struct vnode *a_dvp;
4517 struct vnode **a_vpp;
4518 struct componentname *a_cnp;
4519 struct vattr *a_vap;
4523 struct componentname *cnp = ap->a_cnp;
4524 vattr_t *vap = ap->a_vap;
4526 ASSERT(cnp->cn_flags & SAVENAME);
4528 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
4529 vattr_init_mask(vap);
4531 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
4532 ap->a_target, cnp->cn_cred, cnp->cn_thread));
4536 zfs_freebsd_readlink(ap)
4537 struct vop_readlink_args /* {
4540 struct ucred *a_cred;
4544 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
4548 zfs_freebsd_link(ap)
4549 struct vop_link_args /* {
4550 struct vnode *a_tdvp;
4552 struct componentname *a_cnp;
4555 struct componentname *cnp = ap->a_cnp;
4557 ASSERT(cnp->cn_flags & SAVENAME);
4559 return (zfs_link(ap->a_tdvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
4563 zfs_freebsd_inactive(ap)
4564 struct vop_inactive_args /* {
4566 struct thread *a_td;
4569 vnode_t *vp = ap->a_vp;
4571 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
4576 zfs_reclaim_complete(void *arg, int pending)
4579 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4581 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4582 if (zp->z_dbuf != NULL) {
4583 ZFS_OBJ_HOLD_ENTER(zfsvfs, zp->z_id);
4584 zfs_znode_dmu_fini(zp);
4585 ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
4588 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4590 * If the file system is being unmounted, there is a process waiting
4591 * for us, wake it up.
4593 if (zfsvfs->z_unmounted)
4598 zfs_freebsd_reclaim(ap)
4599 struct vop_reclaim_args /* {
4601 struct thread *a_td;
4604 vnode_t *vp = ap->a_vp;
4605 znode_t *zp = VTOZ(vp);
4606 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4608 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4613 * Destroy the vm object and flush associated pages.
4615 vnode_destroy_vobject(vp);
4617 mutex_enter(&zp->z_lock);
4618 ASSERT(zp->z_phys != NULL);
4620 mutex_exit(&zp->z_lock);
4624 else if (zp->z_dbuf == NULL)
4626 else /* if (!zp->z_unlinked && zp->z_dbuf != NULL) */ {
4629 locked = MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)) ? 2 :
4630 ZFS_OBJ_HOLD_TRYENTER(zfsvfs, zp->z_id);
4633 * Lock can't be obtained due to deadlock possibility,
4634 * so defer znode destruction.
4636 TASK_INIT(&zp->z_task, 0, zfs_reclaim_complete, zp);
4637 taskqueue_enqueue(taskqueue_thread, &zp->z_task);
4639 zfs_znode_dmu_fini(zp);
4641 ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
4647 ASSERT(vp->v_holdcnt >= 1);
4649 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4655 struct vop_fid_args /* {
4661 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
4665 zfs_freebsd_pathconf(ap)
4666 struct vop_pathconf_args /* {
4669 register_t *a_retval;
4675 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
4677 *ap->a_retval = val;
4678 else if (error == EOPNOTSUPP)
4679 error = vop_stdpathconf(ap);
4684 zfs_freebsd_fifo_pathconf(ap)
4685 struct vop_pathconf_args /* {
4688 register_t *a_retval;
4692 switch (ap->a_name) {
4693 case _PC_ACL_EXTENDED:
4695 case _PC_ACL_PATH_MAX:
4696 case _PC_MAC_PRESENT:
4697 return (zfs_freebsd_pathconf(ap));
4699 return (fifo_specops.vop_pathconf(ap));
4704 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
4705 * extended attribute name:
4708 * system freebsd:system:
4709 * user (none, can be used to access ZFS fsattr(5) attributes
4710 * created on Solaris)
4713 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
4716 const char *namespace, *prefix, *suffix;
4718 /* We don't allow '/' character in attribute name. */
4719 if (strchr(name, '/') != NULL)
4721 /* We don't allow attribute names that start with "freebsd:" string. */
4722 if (strncmp(name, "freebsd:", 8) == 0)
4725 bzero(attrname, size);
4727 switch (attrnamespace) {
4728 case EXTATTR_NAMESPACE_USER:
4730 prefix = "freebsd:";
4731 namespace = EXTATTR_NAMESPACE_USER_STRING;
4735 * This is the default namespace by which we can access all
4736 * attributes created on Solaris.
4738 prefix = namespace = suffix = "";
4741 case EXTATTR_NAMESPACE_SYSTEM:
4742 prefix = "freebsd:";
4743 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
4746 case EXTATTR_NAMESPACE_EMPTY:
4750 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
4752 return (ENAMETOOLONG);
4758 * Vnode operating to retrieve a named extended attribute.
4761 zfs_getextattr(struct vop_getextattr_args *ap)
4764 IN struct vnode *a_vp;
4765 IN int a_attrnamespace;
4766 IN const char *a_name;
4767 INOUT struct uio *a_uio;
4769 IN struct ucred *a_cred;
4770 IN struct thread *a_td;
4774 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
4775 struct thread *td = ap->a_td;
4776 struct nameidata nd;
4779 vnode_t *xvp = NULL, *vp;
4782 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
4783 ap->a_cred, ap->a_td, VREAD);
4787 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
4794 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
4802 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
4804 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
4806 NDFREE(&nd, NDF_ONLY_PNBUF);
4809 if (error == ENOENT)
4814 if (ap->a_size != NULL) {
4815 error = VOP_GETATTR(vp, &va, ap->a_cred);
4817 *ap->a_size = (size_t)va.va_size;
4818 } else if (ap->a_uio != NULL)
4819 error = VOP_READ(vp, ap->a_uio, IO_UNIT | IO_SYNC, ap->a_cred);
4822 vn_close(vp, flags, ap->a_cred, td);
4829 * Vnode operation to remove a named attribute.
4832 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
4835 IN struct vnode *a_vp;
4836 IN int a_attrnamespace;
4837 IN const char *a_name;
4838 IN struct ucred *a_cred;
4839 IN struct thread *a_td;
4843 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
4844 struct thread *td = ap->a_td;
4845 struct nameidata nd;
4848 vnode_t *xvp = NULL, *vp;
4851 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
4852 ap->a_cred, ap->a_td, VWRITE);
4856 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
4863 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
4870 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF | MPSAFE,
4871 UIO_SYSSPACE, attrname, xvp, td);
4874 NDFREE(&nd, NDF_ONLY_PNBUF);
4877 if (error == ENOENT)
4881 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
4884 if (vp == nd.ni_dvp)
4894 * Vnode operation to set a named attribute.
4897 zfs_setextattr(struct vop_setextattr_args *ap)
4900 IN struct vnode *a_vp;
4901 IN int a_attrnamespace;
4902 IN const char *a_name;
4903 INOUT struct uio *a_uio;
4904 IN struct ucred *a_cred;
4905 IN struct thread *a_td;
4909 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
4910 struct thread *td = ap->a_td;
4911 struct nameidata nd;
4914 vnode_t *xvp = NULL, *vp;
4917 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
4918 ap->a_cred, ap->a_td, VWRITE);
4922 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
4929 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
4930 LOOKUP_XATTR | CREATE_XATTR_DIR);
4936 flags = FFLAGS(O_WRONLY | O_CREAT);
4937 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
4939 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
4941 NDFREE(&nd, NDF_ONLY_PNBUF);
4949 error = VOP_SETATTR(vp, &va, ap->a_cred);
4951 VOP_WRITE(vp, ap->a_uio, IO_UNIT | IO_SYNC, ap->a_cred);
4954 vn_close(vp, flags, ap->a_cred, td);
4961 * Vnode operation to retrieve extended attributes on a vnode.
4964 zfs_listextattr(struct vop_listextattr_args *ap)
4967 IN struct vnode *a_vp;
4968 IN int a_attrnamespace;
4969 INOUT struct uio *a_uio;
4971 IN struct ucred *a_cred;
4972 IN struct thread *a_td;
4976 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
4977 struct thread *td = ap->a_td;
4978 struct nameidata nd;
4979 char attrprefix[16];
4980 u_char dirbuf[sizeof(struct dirent)];
4983 struct uio auio, *uio = ap->a_uio;
4984 size_t *sizep = ap->a_size;
4986 vnode_t *xvp = NULL, *vp;
4987 int done, error, eof, pos;
4989 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
4990 ap->a_cred, ap->a_td, VREAD);
4994 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
4995 sizeof(attrprefix));
4998 plen = strlen(attrprefix);
5005 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5010 * ENOATTR means that the EA directory does not yet exist,
5011 * i.e. there are no extended attributes there.
5013 if (error == ENOATTR)
5018 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE,
5019 UIO_SYSSPACE, ".", xvp, td);
5022 NDFREE(&nd, NDF_ONLY_PNBUF);
5028 auio.uio_iov = &aiov;
5029 auio.uio_iovcnt = 1;
5030 auio.uio_segflg = UIO_SYSSPACE;
5032 auio.uio_rw = UIO_READ;
5033 auio.uio_offset = 0;
5038 aiov.iov_base = (void *)dirbuf;
5039 aiov.iov_len = sizeof(dirbuf);
5040 auio.uio_resid = sizeof(dirbuf);
5041 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5042 done = sizeof(dirbuf) - auio.uio_resid;
5045 for (pos = 0; pos < done;) {
5046 dp = (struct dirent *)(dirbuf + pos);
5047 pos += dp->d_reclen;
5049 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5050 * is what we get when attribute was created on Solaris.
5052 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5054 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
5056 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5058 nlen = dp->d_namlen - plen;
5061 else if (uio != NULL) {
5063 * Format of extattr name entry is one byte for
5064 * length and the rest for name.
5066 error = uiomove(&nlen, 1, uio->uio_rw, uio);
5068 error = uiomove(dp->d_name + plen, nlen,
5075 } while (!eof && error == 0);
5084 zfs_freebsd_getacl(ap)
5085 struct vop_getacl_args /* {
5094 vsecattr_t vsecattr;
5096 if (ap->a_type != ACL_TYPE_NFS4)
5099 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
5100 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
5103 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
5104 if (vsecattr.vsa_aclentp != NULL)
5105 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
5111 zfs_freebsd_setacl(ap)
5112 struct vop_setacl_args /* {
5121 vsecattr_t vsecattr;
5122 int aclbsize; /* size of acl list in bytes */
5125 if (ap->a_type != ACL_TYPE_NFS4)
5128 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
5132 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
5133 * splitting every entry into two and appending "canonical six"
5134 * entries at the end. Don't allow for setting an ACL that would
5135 * cause chmod(2) to run out of ACL entries.
5137 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
5140 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
5144 vsecattr.vsa_mask = VSA_ACE;
5145 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
5146 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
5147 aaclp = vsecattr.vsa_aclentp;
5148 vsecattr.vsa_aclentsz = aclbsize;
5150 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
5151 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
5152 kmem_free(aaclp, aclbsize);
5158 zfs_freebsd_aclcheck(ap)
5159 struct vop_aclcheck_args /* {
5168 return (EOPNOTSUPP);
5171 struct vop_vector zfs_vnodeops;
5172 struct vop_vector zfs_fifoops;
5173 struct vop_vector zfs_shareops;
5175 struct vop_vector zfs_vnodeops = {
5176 .vop_default = &default_vnodeops,
5177 .vop_inactive = zfs_freebsd_inactive,
5178 .vop_reclaim = zfs_freebsd_reclaim,
5179 .vop_access = zfs_freebsd_access,
5180 #ifdef FREEBSD_NAMECACHE
5181 .vop_lookup = vfs_cache_lookup,
5182 .vop_cachedlookup = zfs_freebsd_lookup,
5184 .vop_lookup = zfs_freebsd_lookup,
5186 .vop_getattr = zfs_freebsd_getattr,
5187 .vop_setattr = zfs_freebsd_setattr,
5188 .vop_create = zfs_freebsd_create,
5189 .vop_mknod = zfs_freebsd_create,
5190 .vop_mkdir = zfs_freebsd_mkdir,
5191 .vop_readdir = zfs_freebsd_readdir,
5192 .vop_fsync = zfs_freebsd_fsync,
5193 .vop_open = zfs_freebsd_open,
5194 .vop_close = zfs_freebsd_close,
5195 .vop_rmdir = zfs_freebsd_rmdir,
5196 .vop_ioctl = zfs_freebsd_ioctl,
5197 .vop_link = zfs_freebsd_link,
5198 .vop_symlink = zfs_freebsd_symlink,
5199 .vop_readlink = zfs_freebsd_readlink,
5200 .vop_read = zfs_freebsd_read,
5201 .vop_write = zfs_freebsd_write,
5202 .vop_remove = zfs_freebsd_remove,
5203 .vop_rename = zfs_freebsd_rename,
5204 .vop_pathconf = zfs_freebsd_pathconf,
5205 .vop_bmap = VOP_EOPNOTSUPP,
5206 .vop_fid = zfs_freebsd_fid,
5207 .vop_getextattr = zfs_getextattr,
5208 .vop_deleteextattr = zfs_deleteextattr,
5209 .vop_setextattr = zfs_setextattr,
5210 .vop_listextattr = zfs_listextattr,
5211 .vop_getacl = zfs_freebsd_getacl,
5212 .vop_setacl = zfs_freebsd_setacl,
5213 .vop_aclcheck = zfs_freebsd_aclcheck,
5216 struct vop_vector zfs_fifoops = {
5217 .vop_default = &fifo_specops,
5218 .vop_fsync = zfs_freebsd_fsync,
5219 .vop_access = zfs_freebsd_access,
5220 .vop_getattr = zfs_freebsd_getattr,
5221 .vop_inactive = zfs_freebsd_inactive,
5222 .vop_read = VOP_PANIC,
5223 .vop_reclaim = zfs_freebsd_reclaim,
5224 .vop_setattr = zfs_freebsd_setattr,
5225 .vop_write = VOP_PANIC,
5226 .vop_pathconf = zfs_freebsd_fifo_pathconf,
5227 .vop_fid = zfs_freebsd_fid,
5228 .vop_getacl = zfs_freebsd_getacl,
5229 .vop_setacl = zfs_freebsd_setacl,
5230 .vop_aclcheck = zfs_freebsd_aclcheck,
5234 * special share hidden files vnode operations template
5236 struct vop_vector zfs_shareops = {
5237 .vop_default = &default_vnodeops,
5238 .vop_access = zfs_freebsd_access,
5239 .vop_inactive = zfs_freebsd_inactive,
5240 .vop_reclaim = zfs_freebsd_reclaim,
5241 .vop_fid = zfs_freebsd_fid,
5242 .vop_pathconf = zfs_freebsd_pathconf,