4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 /* Portions Copyright 2007 Jeremy Teo */
27 #include <sys/types.h>
28 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/resource.h>
34 #include <sys/vnode.h>
38 #include <sys/taskq.h>
40 #include <sys/atomic.h>
41 #include <sys/namei.h>
43 #include <sys/cmn_err.h>
44 #include <sys/errno.h>
45 #include <sys/unistd.h>
46 #include <sys/zfs_dir.h>
47 #include <sys/zfs_ioctl.h>
48 #include <sys/fs/zfs.h>
54 #include <sys/dirent.h>
55 #include <sys/policy.h>
56 #include <sys/sunddi.h>
57 #include <sys/filio.h>
59 #include <sys/zfs_ctldir.h>
60 #include <sys/zfs_fuid.h>
62 #include <sys/zfs_rlock.h>
63 #include <sys/extdirent.h>
64 #include <sys/kidmap.h>
67 #include <sys/sf_buf.h>
68 #include <sys/sched.h>
74 * Each vnode op performs some logical unit of work. To do this, the ZPL must
75 * properly lock its in-core state, create a DMU transaction, do the work,
76 * record this work in the intent log (ZIL), commit the DMU transaction,
77 * and wait for the intent log to commit if it is a synchronous operation.
78 * Moreover, the vnode ops must work in both normal and log replay context.
79 * The ordering of events is important to avoid deadlocks and references
80 * to freed memory. The example below illustrates the following Big Rules:
82 * (1) A check must be made in each zfs thread for a mounted file system.
83 * This is done avoiding races using ZFS_ENTER(zfsvfs).
84 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
85 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
86 * can return EIO from the calling function.
88 * (2) VN_RELE() should always be the last thing except for zil_commit()
89 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
90 * First, if it's the last reference, the vnode/znode
91 * can be freed, so the zp may point to freed memory. Second, the last
92 * reference will call zfs_zinactive(), which may induce a lot of work --
93 * pushing cached pages (which acquires range locks) and syncing out
94 * cached atime changes. Third, zfs_zinactive() may require a new tx,
95 * which could deadlock the system if you were already holding one.
96 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
98 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
99 * as they can span dmu_tx_assign() calls.
101 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
102 * This is critical because we don't want to block while holding locks.
103 * Note, in particular, that if a lock is sometimes acquired before
104 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
105 * use a non-blocking assign can deadlock the system. The scenario:
107 * Thread A has grabbed a lock before calling dmu_tx_assign().
108 * Thread B is in an already-assigned tx, and blocks for this lock.
109 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
110 * forever, because the previous txg can't quiesce until B's tx commits.
112 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
113 * then drop all locks, call dmu_tx_wait(), and try again.
115 * (5) If the operation succeeded, generate the intent log entry for it
116 * before dropping locks. This ensures that the ordering of events
117 * in the intent log matches the order in which they actually occurred.
118 * During ZIL replay the zfs_log_* functions will update the sequence
119 * number to indicate the zil transaction has replayed.
121 * (6) At the end of each vnode op, the DMU tx must always commit,
122 * regardless of whether there were any errors.
124 * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid)
125 * to ensure that synchronous semantics are provided when necessary.
127 * In general, this is how things should be ordered in each vnode op:
129 * ZFS_ENTER(zfsvfs); // exit if unmounted
131 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
132 * rw_enter(...); // grab any other locks you need
133 * tx = dmu_tx_create(...); // get DMU tx
134 * dmu_tx_hold_*(); // hold each object you might modify
135 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
137 * rw_exit(...); // drop locks
138 * zfs_dirent_unlock(dl); // unlock directory entry
139 * VN_RELE(...); // release held vnodes
140 * if (error == ERESTART) {
145 * dmu_tx_abort(tx); // abort DMU tx
146 * ZFS_EXIT(zfsvfs); // finished in zfs
147 * return (error); // really out of space
149 * error = do_real_work(); // do whatever this VOP does
151 * zfs_log_*(...); // on success, make ZIL entry
152 * dmu_tx_commit(tx); // commit DMU tx -- error or not
153 * rw_exit(...); // drop locks
154 * zfs_dirent_unlock(dl); // unlock directory entry
155 * VN_RELE(...); // release held vnodes
156 * zil_commit(zilog, seq, foid); // synchronous when necessary
157 * ZFS_EXIT(zfsvfs); // finished in zfs
158 * return (error); // done, report error
163 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
165 znode_t *zp = VTOZ(*vpp);
166 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
171 if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) &&
172 ((flag & FAPPEND) == 0)) {
177 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
178 ZTOV(zp)->v_type == VREG &&
179 !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
180 zp->z_phys->zp_size > 0) {
181 if (fs_vscan(*vpp, cr, 0) != 0) {
187 /* Keep a count of the synchronous opens in the znode */
188 if (flag & (FSYNC | FDSYNC))
189 atomic_inc_32(&zp->z_sync_cnt);
197 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
198 caller_context_t *ct)
200 znode_t *zp = VTOZ(vp);
201 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
204 * Clean up any locks held by this process on the vp.
206 cleanlocks(vp, ddi_get_pid(), 0);
207 cleanshares(vp, ddi_get_pid());
212 /* Decrement the synchronous opens in the znode */
213 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
214 atomic_dec_32(&zp->z_sync_cnt);
216 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
217 ZTOV(zp)->v_type == VREG &&
218 !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
219 zp->z_phys->zp_size > 0)
220 VERIFY(fs_vscan(vp, cr, 1) == 0);
227 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
228 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
231 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
233 znode_t *zp = VTOZ(vp);
234 uint64_t noff = (uint64_t)*off; /* new offset */
239 file_sz = zp->z_phys->zp_size;
240 if (noff >= file_sz) {
244 if (cmd == _FIO_SEEK_HOLE)
249 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
252 if ((error == ESRCH) || (noff > file_sz)) {
254 * Handle the virtual hole at the end of file.
271 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
272 int *rvalp, caller_context_t *ct)
284 * The following two ioctls are used by bfu. Faking out,
285 * necessary to avoid bfu errors.
293 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
297 zfsvfs = zp->z_zfsvfs;
301 /* offset parameter is in/out */
302 error = zfs_holey(vp, com, &off);
306 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
314 page_lookup(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
320 VM_OBJECT_LOCK_ASSERT(obj, MA_OWNED);
323 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
324 vm_page_is_valid(pp, (vm_offset_t)off, nbytes)) {
325 if ((pp->oflags & VPO_BUSY) != 0) {
327 * Reference the page before unlocking and
328 * sleeping so that the page daemon is less
329 * likely to reclaim it.
331 vm_page_lock_queues();
332 vm_page_flag_set(pp, PG_REFERENCED);
333 vm_page_sleep(pp, "zfsmwb");
337 vm_page_lock_queues();
339 vm_page_unlock_queues();
341 if (__predict_false(obj->cache != NULL)) {
342 vm_page_cache_free(obj, OFF_TO_IDX(start),
343 OFF_TO_IDX(start) + 1);
353 page_unlock(vm_page_t pp)
360 zfs_map_page(vm_page_t pp, struct sf_buf **sfp)
363 *sfp = sf_buf_alloc(pp, 0);
364 return ((caddr_t)sf_buf_kva(*sfp));
368 zfs_unmap_page(struct sf_buf *sf)
376 * When a file is memory mapped, we must keep the IO data synchronized
377 * between the DMU cache and the memory mapped pages. What this means:
379 * On Write: If we find a memory mapped page, we write to *both*
380 * the page and the dmu buffer.
384 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
385 int segflg, dmu_tx_t *tx)
391 ASSERT(vp->v_mount != NULL);
395 off = start & PAGEOFFSET;
397 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
399 int nbytes = MIN(PAGESIZE - off, len);
401 if ((pp = page_lookup(vp, start, off, nbytes)) != NULL) {
404 VM_OBJECT_UNLOCK(obj);
405 va = zfs_map_page(pp, &sf);
406 if (segflg == UIO_NOCOPY) {
407 (void) dmu_write(os, oid, start+off, nbytes,
410 (void) dmu_read(os, oid, start+off, nbytes,
411 va+off, DMU_READ_PREFETCH);;
421 VM_OBJECT_UNLOCK(obj);
425 * When a file is memory mapped, we must keep the IO data synchronized
426 * between the DMU cache and the memory mapped pages. What this means:
428 * On Read: We "read" preferentially from memory mapped pages,
429 * else we default from the dmu buffer.
431 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
432 * the file is memory mapped.
435 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
437 znode_t *zp = VTOZ(vp);
438 objset_t *os = zp->z_zfsvfs->z_os;
449 ASSERT(vp->v_mount != NULL);
453 start = uio->uio_loffset;
454 off = start & PAGEOFFSET;
457 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
458 int bytes = MIN(PAGESIZE - off, len);
461 if ((m = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
462 vm_page_is_valid(m, off, bytes)) {
463 if ((m->oflags & VPO_BUSY) != 0) {
465 * Reference the page before unlocking and
466 * sleeping so that the page daemon is less
467 * likely to reclaim it.
469 vm_page_lock_queues();
470 vm_page_flag_set(m, PG_REFERENCED);
471 vm_page_sleep(m, "zfsmrb");
476 VM_OBJECT_UNLOCK(obj);
478 error = dmu_read_uio(os, zp->z_id, uio,
483 uiomove_fromphys(&m, off, bytes, uio);
486 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
488 * The code below is here to make sendfile(2) work
489 * correctly with ZFS. As pointed out by ups@
490 * sendfile(2) should be changed to use VOP_GETPAGES(),
491 * but it pessimize performance of sendfile/UFS, that's
492 * why I handle this special case in ZFS code.
494 if ((m->oflags & VPO_BUSY) != 0) {
496 * Reference the page before unlocking and
497 * sleeping so that the page daemon is less
498 * likely to reclaim it.
500 vm_page_lock_queues();
501 vm_page_flag_set(m, PG_REFERENCED);
502 vm_page_sleep(m, "zfsmrb");
506 VM_OBJECT_UNLOCK(obj);
508 error = dmu_read_uio(os, zp->z_id, uio,
513 va = zfs_map_page(m, &sf);
514 error = dmu_read(os, zp->z_id, start + off,
515 bytes, (void *)(va + off),
521 vm_page_set_valid(m, off, bytes);
524 uio->uio_resid -= bytes;
525 uio->uio_offset += bytes;
535 VM_OBJECT_UNLOCK(obj);
536 if (error == 0 && dirbytes > 0)
537 error = dmu_read_uio(os, zp->z_id, uio, dirbytes);
541 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
544 * Read bytes from specified file into supplied buffer.
546 * IN: vp - vnode of file to be read from.
547 * uio - structure supplying read location, range info,
549 * ioflag - SYNC flags; used to provide FRSYNC semantics.
550 * cr - credentials of caller.
551 * ct - caller context
553 * OUT: uio - updated offset and range, buffer filled.
555 * RETURN: 0 if success
556 * error code if failure
559 * vp - atime updated if byte count > 0
563 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
565 znode_t *zp = VTOZ(vp);
566 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
576 if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) {
582 * Validate file offset
584 if (uio->uio_loffset < (offset_t)0) {
590 * Fasttrack empty reads
592 if (uio->uio_resid == 0) {
598 * Check for mandatory locks
600 if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
601 if (error = chklock(vp, FREAD,
602 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
609 * If we're in FRSYNC mode, sync out this znode before reading it.
612 zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
615 * Lock the range against changes.
617 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
620 * If we are reading past end-of-file we can skip
621 * to the end; but we might still need to set atime.
623 if (uio->uio_loffset >= zp->z_phys->zp_size) {
628 ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
629 n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
632 nbytes = MIN(n, zfs_read_chunk_size -
633 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
635 if (vn_has_cached_data(vp))
636 error = mappedread(vp, nbytes, uio);
638 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
640 /* convert checksum errors into IO errors */
650 zfs_range_unlock(rl);
652 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
658 * Fault in the pages of the first n bytes specified by the uio structure.
659 * 1 byte in each page is touched and the uio struct is unmodified.
660 * Any error will exit this routine as this is only a best
661 * attempt to get the pages resident. This is a copy of ufs_trans_touch().
664 zfs_prefault_write(ssize_t n, struct uio *uio)
670 if (uio->uio_segflg != UIO_USERSPACE)
676 cnt = MIN(iov->iov_len, n);
678 /* empty iov entry */
684 * touch each page in this segment.
690 incr = MIN(cnt, PAGESIZE);
695 * touch the last byte in case it straddles a page.
705 * Write the bytes to a file.
707 * IN: vp - vnode of file to be written to.
708 * uio - structure supplying write location, range info,
710 * ioflag - IO_APPEND flag set if in append mode.
711 * cr - credentials of caller.
712 * ct - caller context (NFS/CIFS fem monitor only)
714 * OUT: uio - updated offset and range.
716 * RETURN: 0 if success
717 * error code if failure
720 * vp - ctime|mtime updated if byte count > 0
724 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
726 znode_t *zp = VTOZ(vp);
727 rlim64_t limit = MAXOFFSET_T;
728 ssize_t start_resid = uio->uio_resid;
732 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
737 int max_blksz = zfsvfs->z_max_blksz;
743 * Fasttrack empty write
749 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
756 * If immutable or not appending then return EPERM
758 pflags = zp->z_phys->zp_flags;
759 if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
760 ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
761 (uio->uio_loffset < zp->z_phys->zp_size))) {
766 zilog = zfsvfs->z_log;
769 * Pre-fault the pages to ensure slow (eg NFS) pages
772 zfs_prefault_write(n, uio);
775 * If in append mode, set the io offset pointer to eof.
777 if (ioflag & IO_APPEND) {
779 * Range lock for a file append:
780 * The value for the start of range will be determined by
781 * zfs_range_lock() (to guarantee append semantics).
782 * If this write will cause the block size to increase,
783 * zfs_range_lock() will lock the entire file, so we must
784 * later reduce the range after we grow the block size.
786 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
787 if (rl->r_len == UINT64_MAX) {
788 /* overlocked, zp_size can't change */
789 woff = uio->uio_loffset = zp->z_phys->zp_size;
791 woff = uio->uio_loffset = rl->r_off;
794 woff = uio->uio_loffset;
796 * Validate file offset
804 * If we need to grow the block size then zfs_range_lock()
805 * will lock a wider range than we request here.
806 * Later after growing the block size we reduce the range.
808 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
812 zfs_range_unlock(rl);
817 if ((woff + n) > limit || woff > (limit - n))
821 * Check for mandatory locks
823 if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
824 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
825 zfs_range_unlock(rl);
829 end_size = MAX(zp->z_phys->zp_size, woff + n);
832 * Write the file in reasonable size chunks. Each chunk is written
833 * in a separate transaction; this keeps the intent log records small
834 * and allows us to do more fine-grained space accounting.
838 woff = uio->uio_loffset;
841 if (zfs_usergroup_overquota(zfsvfs,
842 B_FALSE, zp->z_phys->zp_uid) ||
843 zfs_usergroup_overquota(zfsvfs,
844 B_TRUE, zp->z_phys->zp_gid)) {
846 dmu_return_arcbuf(abuf);
852 * If dmu_assign_arcbuf() is expected to execute with minimum
853 * overhead loan an arc buffer and copy user data to it before
854 * we enter a txg. This avoids holding a txg forever while we
855 * pagefault on a hanging NFS server mapping.
857 if (abuf == NULL && n >= max_blksz &&
858 woff >= zp->z_phys->zp_size &&
859 P2PHASE(woff, max_blksz) == 0 &&
860 zp->z_blksz == max_blksz) {
863 abuf = dmu_request_arcbuf(zp->z_dbuf, max_blksz);
864 ASSERT(abuf != NULL);
865 ASSERT(arc_buf_size(abuf) == max_blksz);
866 if (error = uiocopy(abuf->b_data, max_blksz,
867 UIO_WRITE, uio, &cbytes)) {
868 dmu_return_arcbuf(abuf);
871 ASSERT(cbytes == max_blksz);
875 * Start a transaction.
877 tx = dmu_tx_create(zfsvfs->z_os);
878 dmu_tx_hold_bonus(tx, zp->z_id);
879 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
880 error = dmu_tx_assign(tx, TXG_NOWAIT);
882 if (error == ERESTART) {
889 dmu_return_arcbuf(abuf);
894 * If zfs_range_lock() over-locked we grow the blocksize
895 * and then reduce the lock range. This will only happen
896 * on the first iteration since zfs_range_reduce() will
897 * shrink down r_len to the appropriate size.
899 if (rl->r_len == UINT64_MAX) {
902 if (zp->z_blksz > max_blksz) {
903 ASSERT(!ISP2(zp->z_blksz));
904 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
906 new_blksz = MIN(end_size, max_blksz);
908 zfs_grow_blocksize(zp, new_blksz, tx);
909 zfs_range_reduce(rl, woff, n);
913 * XXX - should we really limit each write to z_max_blksz?
914 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
916 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
918 if (woff + nbytes > zp->z_phys->zp_size)
919 vnode_pager_setsize(vp, woff + nbytes);
922 tx_bytes = uio->uio_resid;
923 error = dmu_write_uio(zfsvfs->z_os, zp->z_id, uio,
925 tx_bytes -= uio->uio_resid;
928 ASSERT(tx_bytes == max_blksz);
929 dmu_assign_arcbuf(zp->z_dbuf, woff, abuf, tx);
930 ASSERT(tx_bytes <= uio->uio_resid);
931 uioskip(uio, tx_bytes);
934 if (tx_bytes && vn_has_cached_data(vp)) {
935 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
936 zp->z_id, uio->uio_segflg, tx);
940 * If we made no progress, we're done. If we made even
941 * partial progress, update the znode and ZIL accordingly.
950 * Clear Set-UID/Set-GID bits on successful write if not
951 * privileged and at least one of the excute bits is set.
953 * It would be nice to to this after all writes have
954 * been done, but that would still expose the ISUID/ISGID
955 * to another app after the partial write is committed.
957 * Note: we don't call zfs_fuid_map_id() here because
958 * user 0 is not an ephemeral uid.
960 mutex_enter(&zp->z_acl_lock);
961 if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
962 (S_IXUSR >> 6))) != 0 &&
963 (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
964 secpolicy_vnode_setid_retain(vp, cr,
965 (zp->z_phys->zp_mode & S_ISUID) != 0 &&
966 zp->z_phys->zp_uid == 0) != 0) {
967 zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
969 mutex_exit(&zp->z_acl_lock);
972 * Update time stamp. NOTE: This marks the bonus buffer as
973 * dirty, so we don't have to do it again for zp_size.
975 zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
978 * Update the file size (zp_size) if it has changed;
979 * account for possible concurrent updates.
981 while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
982 (void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
984 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
989 ASSERT(tx_bytes == nbytes);
993 zfs_range_unlock(rl);
996 * If we're in replay mode, or we made no progress, return error.
997 * Otherwise, it's at least a partial write, so it's successful.
999 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1004 if (ioflag & (FSYNC | FDSYNC))
1005 zil_commit(zilog, zp->z_last_itx, zp->z_id);
1012 zfs_get_done(dmu_buf_t *db, void *vzgd)
1014 zgd_t *zgd = (zgd_t *)vzgd;
1015 rl_t *rl = zgd->zgd_rl;
1016 vnode_t *vp = ZTOV(rl->r_zp);
1017 objset_t *os = rl->r_zp->z_zfsvfs->z_os;
1020 vfslocked = VFS_LOCK_GIANT(vp->v_vfsp);
1021 dmu_buf_rele(db, vzgd);
1022 zfs_range_unlock(rl);
1024 * Release the vnode asynchronously as we currently have the
1025 * txg stopped from syncing.
1027 VN_RELE_ASYNC(vp, dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1028 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1029 kmem_free(zgd, sizeof (zgd_t));
1030 VFS_UNLOCK_GIANT(vfslocked);
1034 * Get data to generate a TX_WRITE intent log record.
1037 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1039 zfsvfs_t *zfsvfs = arg;
1040 objset_t *os = zfsvfs->z_os;
1042 uint64_t off = lr->lr_offset;
1046 int dlen = lr->lr_length; /* length of user data */
1053 * Nothing to do if the file has been removed
1055 if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
1057 if (zp->z_unlinked) {
1059 * Release the vnode asynchronously as we currently have the
1060 * txg stopped from syncing.
1062 VN_RELE_ASYNC(ZTOV(zp),
1063 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1068 * Write records come in two flavors: immediate and indirect.
1069 * For small writes it's cheaper to store the data with the
1070 * log record (immediate); for large writes it's cheaper to
1071 * sync the data and get a pointer to it (indirect) so that
1072 * we don't have to write the data twice.
1074 if (buf != NULL) { /* immediate write */
1075 rl = zfs_range_lock(zp, off, dlen, RL_READER);
1076 /* test for truncation needs to be done while range locked */
1077 if (off >= zp->z_phys->zp_size) {
1081 VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf,
1082 DMU_READ_NO_PREFETCH));
1083 } else { /* indirect write */
1084 uint64_t boff; /* block starting offset */
1087 * Have to lock the whole block to ensure when it's
1088 * written out and it's checksum is being calculated
1089 * that no one can change the data. We need to re-check
1090 * blocksize after we get the lock in case it's changed!
1093 if (ISP2(zp->z_blksz)) {
1094 boff = P2ALIGN_TYPED(off, zp->z_blksz,
1100 rl = zfs_range_lock(zp, boff, dlen, RL_READER);
1101 if (zp->z_blksz == dlen)
1103 zfs_range_unlock(rl);
1105 /* test for truncation needs to be done while range locked */
1106 if (off >= zp->z_phys->zp_size) {
1110 zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
1112 zgd->zgd_zilog = zfsvfs->z_log;
1113 zgd->zgd_bp = &lr->lr_blkptr;
1114 VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
1115 ASSERT(boff == db->db_offset);
1116 lr->lr_blkoff = off - boff;
1117 error = dmu_sync(zio, db, &lr->lr_blkptr,
1118 lr->lr_common.lrc_txg, zfs_get_done, zgd);
1119 ASSERT((error && error != EINPROGRESS) ||
1120 lr->lr_length <= zp->z_blksz);
1123 * dmu_sync() can compress a block of zeros to a null
1124 * blkptr but the block size still needs to be passed
1125 * through to replay.
1127 BP_SET_LSIZE(&lr->lr_blkptr, db->db_size);
1128 zil_add_block(zfsvfs->z_log, &lr->lr_blkptr);
1132 * If we get EINPROGRESS, then we need to wait for a
1133 * write IO initiated by dmu_sync() to complete before
1134 * we can release this dbuf. We will finish everything
1135 * up in the zfs_get_done() callback.
1137 if (error == EINPROGRESS) {
1139 } else if (error == EALREADY) {
1140 lr->lr_common.lrc_txtype = TX_WRITE2;
1143 dmu_buf_rele(db, zgd);
1144 kmem_free(zgd, sizeof (zgd_t));
1147 zfs_range_unlock(rl);
1149 * Release the vnode asynchronously as we currently have the
1150 * txg stopped from syncing.
1152 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1158 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1159 caller_context_t *ct)
1161 znode_t *zp = VTOZ(vp);
1162 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1168 if (flag & V_ACE_MASK)
1169 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1171 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1178 * If vnode is for a device return a specfs vnode instead.
1181 specvp_check(vnode_t **vpp, cred_t *cr)
1185 if (IS_DEVVP(*vpp)) {
1188 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1199 * Lookup an entry in a directory, or an extended attribute directory.
1200 * If it exists, return a held vnode reference for it.
1202 * IN: dvp - vnode of directory to search.
1203 * nm - name of entry to lookup.
1204 * pnp - full pathname to lookup [UNUSED].
1205 * flags - LOOKUP_XATTR set if looking for an attribute.
1206 * rdir - root directory vnode [UNUSED].
1207 * cr - credentials of caller.
1208 * ct - caller context
1209 * direntflags - directory lookup flags
1210 * realpnp - returned pathname.
1212 * OUT: vpp - vnode of located entry, NULL if not found.
1214 * RETURN: 0 if success
1215 * error code if failure
1222 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1223 int nameiop, cred_t *cr, kthread_t *td, int flags)
1225 znode_t *zdp = VTOZ(dvp);
1226 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1228 int *direntflags = NULL;
1229 void *realpnp = NULL;
1232 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1234 if (dvp->v_type != VDIR) {
1236 } else if (zdp->z_dbuf == NULL) {
1240 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1241 error = zfs_fastaccesschk_execute(zdp, cr);
1249 vnode_t *tvp = dnlc_lookup(dvp, nm);
1252 error = zfs_fastaccesschk_execute(zdp, cr);
1257 if (tvp == DNLC_NO_VNODE) {
1262 return (specvp_check(vpp, cr));
1268 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1275 if (flags & LOOKUP_XATTR) {
1278 * If the xattr property is off, refuse the lookup request.
1280 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1287 * We don't allow recursive attributes..
1288 * Maybe someday we will.
1290 if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1295 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1301 * Do we have permission to get into attribute directory?
1304 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1314 if (dvp->v_type != VDIR) {
1320 * Check accessibility of directory.
1323 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1328 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1329 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1334 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1336 error = specvp_check(vpp, cr);
1338 /* Translate errors and add SAVENAME when needed. */
1339 if (cnp->cn_flags & ISLASTCN) {
1343 if (error == ENOENT) {
1344 error = EJUSTRETURN;
1345 cnp->cn_flags |= SAVENAME;
1351 cnp->cn_flags |= SAVENAME;
1355 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) {
1358 if (cnp->cn_flags & ISDOTDOT) {
1359 ltype = VOP_ISLOCKED(dvp);
1363 error = vn_lock(*vpp, cnp->cn_lkflags);
1364 if (cnp->cn_flags & ISDOTDOT)
1365 vn_lock(dvp, ltype | LK_RETRY);
1375 #ifdef FREEBSD_NAMECACHE
1377 * Insert name into cache (as non-existent) if appropriate.
1379 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
1380 cache_enter(dvp, *vpp, cnp);
1382 * Insert name into cache if appropriate.
1384 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1385 if (!(cnp->cn_flags & ISLASTCN) ||
1386 (nameiop != DELETE && nameiop != RENAME)) {
1387 cache_enter(dvp, *vpp, cnp);
1396 * Attempt to create a new entry in a directory. If the entry
1397 * already exists, truncate the file if permissible, else return
1398 * an error. Return the vp of the created or trunc'd file.
1400 * IN: dvp - vnode of directory to put new file entry in.
1401 * name - name of new file entry.
1402 * vap - attributes of new file.
1403 * excl - flag indicating exclusive or non-exclusive mode.
1404 * mode - mode to open file with.
1405 * cr - credentials of caller.
1406 * flag - large file flag [UNUSED].
1407 * ct - caller context
1408 * vsecp - ACL to be set
1410 * OUT: vpp - vnode of created or trunc'd entry.
1412 * RETURN: 0 if success
1413 * error code if failure
1416 * dvp - ctime|mtime updated if new entry created
1417 * vp - ctime|mtime always, atime if new
1422 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1423 vnode_t **vpp, cred_t *cr, kthread_t *td)
1425 znode_t *zp, *dzp = VTOZ(dvp);
1426 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1434 gid_t gid = crgetgid(cr);
1435 zfs_acl_ids_t acl_ids;
1436 boolean_t fuid_dirtied;
1441 * If we have an ephemeral id, ACL, or XVATTR then
1442 * make sure file system is at proper version
1445 ksid = crgetsid(cr, KSID_OWNER);
1447 uid = ksid_getid(ksid);
1450 if (zfsvfs->z_use_fuids == B_FALSE &&
1451 (vsecp || (vap->va_mask & AT_XVATTR) ||
1452 IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))))
1458 zilog = zfsvfs->z_log;
1460 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1461 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1466 if (vap->va_mask & AT_XVATTR) {
1467 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1468 crgetuid(cr), cr, vap->va_type)) != 0) {
1476 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1477 vap->va_mode &= ~S_ISVTX;
1479 if (*name == '\0') {
1481 * Null component name refers to the directory itself.
1488 /* possible VN_HOLD(zp) */
1491 if (flag & FIGNORECASE)
1494 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1497 if (strcmp(name, "..") == 0)
1507 * Create a new file object and update the directory
1510 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1515 * We only support the creation of regular files in
1516 * extended attribute directories.
1518 if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1519 (vap->va_type != VREG)) {
1525 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
1528 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1529 zfs_acl_ids_free(&acl_ids);
1534 tx = dmu_tx_create(os);
1535 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1536 fuid_dirtied = zfsvfs->z_fuid_dirty;
1538 zfs_fuid_txhold(zfsvfs, tx);
1539 dmu_tx_hold_bonus(tx, dzp->z_id);
1540 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1541 if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1542 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1543 0, SPA_MAXBLOCKSIZE);
1545 error = dmu_tx_assign(tx, TXG_NOWAIT);
1547 zfs_acl_ids_free(&acl_ids);
1548 zfs_dirent_unlock(dl);
1549 if (error == ERESTART) {
1558 zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
1561 zfs_fuid_sync(zfsvfs, tx);
1563 (void) zfs_link_create(dl, zp, tx, ZNEW);
1565 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1566 if (flag & FIGNORECASE)
1568 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1569 vsecp, acl_ids.z_fuidp, vap);
1570 zfs_acl_ids_free(&acl_ids);
1573 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1576 * A directory entry already exists for this name.
1579 * Can't truncate an existing file if in exclusive mode.
1586 * Can't open a directory for writing.
1588 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1593 * Verify requested access to file.
1595 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1599 mutex_enter(&dzp->z_lock);
1601 mutex_exit(&dzp->z_lock);
1604 * Truncate regular files if requested.
1606 if ((ZTOV(zp)->v_type == VREG) &&
1607 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1608 /* we can't hold any locks when calling zfs_freesp() */
1609 zfs_dirent_unlock(dl);
1611 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1613 vnevent_create(ZTOV(zp), ct);
1619 zfs_dirent_unlock(dl);
1626 error = specvp_check(vpp, cr);
1634 * Remove an entry from a directory.
1636 * IN: dvp - vnode of directory to remove entry from.
1637 * name - name of entry to remove.
1638 * cr - credentials of caller.
1639 * ct - caller context
1640 * flags - case flags
1642 * RETURN: 0 if success
1643 * error code if failure
1647 * vp - ctime (if nlink > 0)
1651 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1654 znode_t *zp, *dzp = VTOZ(dvp);
1655 znode_t *xzp = NULL;
1657 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1659 uint64_t acl_obj, xattr_obj;
1662 boolean_t may_delete_now, delete_now = FALSE;
1663 boolean_t unlinked, toobig = FALSE;
1665 pathname_t *realnmp = NULL;
1672 zilog = zfsvfs->z_log;
1674 if (flags & FIGNORECASE) {
1682 * Attempt to lock directory; fail if entry doesn't exist.
1684 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1694 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1699 * Need to use rmdir for removing directories.
1701 if (vp->v_type == VDIR) {
1706 vnevent_remove(vp, dvp, name, ct);
1709 dnlc_remove(dvp, realnmp->pn_buf);
1711 dnlc_remove(dvp, name);
1713 may_delete_now = FALSE;
1716 * We may delete the znode now, or we may put it in the unlinked set;
1717 * it depends on whether we're the last link, and on whether there are
1718 * other holds on the vnode. So we dmu_tx_hold() the right things to
1719 * allow for either case.
1721 tx = dmu_tx_create(zfsvfs->z_os);
1722 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1723 dmu_tx_hold_bonus(tx, zp->z_id);
1724 if (may_delete_now) {
1726 zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1727 /* if the file is too big, only hold_free a token amount */
1728 dmu_tx_hold_free(tx, zp->z_id, 0,
1729 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1732 /* are there any extended attributes? */
1733 if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1734 /* XXX - do we need this if we are deleting? */
1735 dmu_tx_hold_bonus(tx, xattr_obj);
1738 /* are there any additional acls */
1739 if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1741 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1743 /* charge as an update -- would be nice not to charge at all */
1744 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1746 error = dmu_tx_assign(tx, TXG_NOWAIT);
1748 zfs_dirent_unlock(dl);
1750 if (error == ERESTART) {
1763 * Remove the directory entry.
1765 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1772 if (0 && unlinked) {
1774 delete_now = may_delete_now && !toobig &&
1775 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1776 zp->z_phys->zp_xattr == xattr_obj &&
1777 zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1782 if (zp->z_phys->zp_xattr) {
1783 error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1784 ASSERT3U(error, ==, 0);
1785 ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1786 dmu_buf_will_dirty(xzp->z_dbuf, tx);
1787 mutex_enter(&xzp->z_lock);
1788 xzp->z_unlinked = 1;
1789 xzp->z_phys->zp_links = 0;
1790 mutex_exit(&xzp->z_lock);
1791 zfs_unlinked_add(xzp, tx);
1792 zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1794 mutex_enter(&zp->z_lock);
1797 ASSERT3U(vp->v_count, ==, 0);
1799 mutex_exit(&zp->z_lock);
1800 zfs_znode_delete(zp, tx);
1801 } else if (unlinked) {
1802 zfs_unlinked_add(zp, tx);
1806 if (flags & FIGNORECASE)
1808 zfs_log_remove(zilog, tx, txtype, dzp, name);
1815 zfs_dirent_unlock(dl);
1820 /* this rele is delayed to prevent nesting transactions */
1829 * Create a new directory and insert it into dvp using the name
1830 * provided. Return a pointer to the inserted directory.
1832 * IN: dvp - vnode of directory to add subdir to.
1833 * dirname - name of new directory.
1834 * vap - attributes of new directory.
1835 * cr - credentials of caller.
1836 * ct - caller context
1837 * vsecp - ACL to be set
1839 * OUT: vpp - vnode of created directory.
1841 * RETURN: 0 if success
1842 * error code if failure
1845 * dvp - ctime|mtime updated
1846 * vp - ctime|mtime|atime updated
1850 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1851 caller_context_t *ct, int flags, vsecattr_t *vsecp)
1853 znode_t *zp, *dzp = VTOZ(dvp);
1854 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1863 gid_t gid = crgetgid(cr);
1864 zfs_acl_ids_t acl_ids;
1865 boolean_t fuid_dirtied;
1867 ASSERT(vap->va_type == VDIR);
1870 * If we have an ephemeral id, ACL, or XVATTR then
1871 * make sure file system is at proper version
1874 ksid = crgetsid(cr, KSID_OWNER);
1876 uid = ksid_getid(ksid);
1879 if (zfsvfs->z_use_fuids == B_FALSE &&
1880 (vsecp || (vap->va_mask & AT_XVATTR) || IS_EPHEMERAL(crgetuid(cr))||
1881 IS_EPHEMERAL(crgetgid(cr))))
1886 zilog = zfsvfs->z_log;
1888 if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1893 if (zfsvfs->z_utf8 && u8_validate(dirname,
1894 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1898 if (flags & FIGNORECASE)
1901 if (vap->va_mask & AT_XVATTR)
1902 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1903 crgetuid(cr), cr, vap->va_type)) != 0) {
1909 * First make sure the new directory doesn't exist.
1914 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1920 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1921 zfs_dirent_unlock(dl);
1926 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
1928 zfs_dirent_unlock(dl);
1932 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1933 zfs_acl_ids_free(&acl_ids);
1934 zfs_dirent_unlock(dl);
1940 * Add a new entry to the directory.
1942 tx = dmu_tx_create(zfsvfs->z_os);
1943 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1944 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1945 fuid_dirtied = zfsvfs->z_fuid_dirty;
1947 zfs_fuid_txhold(zfsvfs, tx);
1948 if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE)
1949 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1950 0, SPA_MAXBLOCKSIZE);
1951 error = dmu_tx_assign(tx, TXG_NOWAIT);
1953 zfs_acl_ids_free(&acl_ids);
1954 zfs_dirent_unlock(dl);
1955 if (error == ERESTART) {
1968 zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
1971 zfs_fuid_sync(zfsvfs, tx);
1973 * Now put new name in parent dir.
1975 (void) zfs_link_create(dl, zp, tx, ZNEW);
1979 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1980 if (flags & FIGNORECASE)
1982 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1983 acl_ids.z_fuidp, vap);
1985 zfs_acl_ids_free(&acl_ids);
1988 zfs_dirent_unlock(dl);
1995 * Remove a directory subdir entry. If the current working
1996 * directory is the same as the subdir to be removed, the
1999 * IN: dvp - vnode of directory to remove from.
2000 * name - name of directory to be removed.
2001 * cwd - vnode of current working directory.
2002 * cr - credentials of caller.
2003 * ct - caller context
2004 * flags - case flags
2006 * RETURN: 0 if success
2007 * error code if failure
2010 * dvp - ctime|mtime updated
2014 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2015 caller_context_t *ct, int flags)
2017 znode_t *dzp = VTOZ(dvp);
2020 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2029 zilog = zfsvfs->z_log;
2031 if (flags & FIGNORECASE)
2037 * Attempt to lock directory; fail if entry doesn't exist.
2039 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2047 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2051 if (vp->v_type != VDIR) {
2061 vnevent_rmdir(vp, dvp, name, ct);
2064 * Grab a lock on the directory to make sure that noone is
2065 * trying to add (or lookup) entries while we are removing it.
2067 rw_enter(&zp->z_name_lock, RW_WRITER);
2070 * Grab a lock on the parent pointer to make sure we play well
2071 * with the treewalk and directory rename code.
2073 rw_enter(&zp->z_parent_lock, RW_WRITER);
2075 tx = dmu_tx_create(zfsvfs->z_os);
2076 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2077 dmu_tx_hold_bonus(tx, zp->z_id);
2078 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2079 error = dmu_tx_assign(tx, TXG_NOWAIT);
2081 rw_exit(&zp->z_parent_lock);
2082 rw_exit(&zp->z_name_lock);
2083 zfs_dirent_unlock(dl);
2085 if (error == ERESTART) {
2095 #ifdef FREEBSD_NAMECACHE
2099 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2102 uint64_t txtype = TX_RMDIR;
2103 if (flags & FIGNORECASE)
2105 zfs_log_remove(zilog, tx, txtype, dzp, name);
2110 rw_exit(&zp->z_parent_lock);
2111 rw_exit(&zp->z_name_lock);
2112 #ifdef FREEBSD_NAMECACHE
2116 zfs_dirent_unlock(dl);
2125 * Read as many directory entries as will fit into the provided
2126 * buffer from the given directory cursor position (specified in
2127 * the uio structure.
2129 * IN: vp - vnode of directory to read.
2130 * uio - structure supplying read location, range info,
2131 * and return buffer.
2132 * cr - credentials of caller.
2133 * ct - caller context
2134 * flags - case flags
2136 * OUT: uio - updated offset and range, buffer filled.
2137 * eofp - set to true if end-of-file detected.
2139 * RETURN: 0 if success
2140 * error code if failure
2143 * vp - atime updated
2145 * Note that the low 4 bits of the cookie returned by zap is always zero.
2146 * This allows us to use the low range for "special" directory entries:
2147 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2148 * we use the offset 2 for the '.zfs' directory.
2152 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2154 znode_t *zp = VTOZ(vp);
2158 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2163 zap_attribute_t zap;
2164 uint_t bytes_wanted;
2165 uint64_t offset; /* must be unsigned; checks for < 1 */
2170 boolean_t check_sysattrs;
2173 u_long *cooks = NULL;
2180 * If we are not given an eof variable,
2187 * Check for valid iov_len.
2189 if (uio->uio_iov->iov_len <= 0) {
2195 * Quit if directory has been removed (posix)
2197 if ((*eofp = zp->z_unlinked) != 0) {
2204 offset = uio->uio_loffset;
2205 prefetch = zp->z_zn_prefetch;
2208 * Initialize the iterator cursor.
2212 * Start iteration from the beginning of the directory.
2214 zap_cursor_init(&zc, os, zp->z_id);
2217 * The offset is a serialized cursor.
2219 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2223 * Get space to change directory entries into fs independent format.
2225 iovp = uio->uio_iov;
2226 bytes_wanted = iovp->iov_len;
2227 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2228 bufsize = bytes_wanted;
2229 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2230 odp = (struct dirent64 *)outbuf;
2232 bufsize = bytes_wanted;
2233 odp = (struct dirent64 *)iovp->iov_base;
2235 eodp = (struct edirent *)odp;
2237 if (ncookies != NULL) {
2239 * Minimum entry size is dirent size and 1 byte for a file name.
2241 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2242 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2247 * If this VFS supports the system attribute view interface; and
2248 * we're looking at an extended attribute directory; and we care
2249 * about normalization conflicts on this vfs; then we must check
2250 * for normalization conflicts with the sysattr name space.
2253 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2254 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2255 (flags & V_RDDIR_ENTFLAGS);
2261 * Transform to file-system independent format
2264 while (outcount < bytes_wanted) {
2270 * Special case `.', `..', and `.zfs'.
2273 (void) strcpy(zap.za_name, ".");
2274 zap.za_normalization_conflict = 0;
2277 } else if (offset == 1) {
2278 (void) strcpy(zap.za_name, "..");
2279 zap.za_normalization_conflict = 0;
2280 objnum = zp->z_phys->zp_parent;
2282 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2283 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2284 zap.za_normalization_conflict = 0;
2285 objnum = ZFSCTL_INO_ROOT;
2291 if (error = zap_cursor_retrieve(&zc, &zap)) {
2292 if ((*eofp = (error == ENOENT)) != 0)
2298 if (zap.za_integer_length != 8 ||
2299 zap.za_num_integers != 1) {
2300 cmn_err(CE_WARN, "zap_readdir: bad directory "
2301 "entry, obj = %lld, offset = %lld\n",
2302 (u_longlong_t)zp->z_id,
2303 (u_longlong_t)offset);
2308 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2310 * MacOS X can extract the object type here such as:
2311 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2313 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2315 if (check_sysattrs && !zap.za_normalization_conflict) {
2317 zap.za_normalization_conflict =
2318 xattr_sysattr_casechk(zap.za_name);
2320 panic("%s:%u: TODO", __func__, __LINE__);
2325 if (flags & V_RDDIR_ACCFILTER) {
2327 * If we have no access at all, don't include
2328 * this entry in the returned information
2331 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2333 if (!zfs_has_access(ezp, cr)) {
2340 if (flags & V_RDDIR_ENTFLAGS)
2341 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2343 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2346 * Will this entry fit in the buffer?
2348 if (outcount + reclen > bufsize) {
2350 * Did we manage to fit anything in the buffer?
2358 if (flags & V_RDDIR_ENTFLAGS) {
2360 * Add extended flag entry:
2362 eodp->ed_ino = objnum;
2363 eodp->ed_reclen = reclen;
2364 /* NOTE: ed_off is the offset for the *next* entry */
2365 next = &(eodp->ed_off);
2366 eodp->ed_eflags = zap.za_normalization_conflict ?
2367 ED_CASE_CONFLICT : 0;
2368 (void) strncpy(eodp->ed_name, zap.za_name,
2369 EDIRENT_NAMELEN(reclen));
2370 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2375 odp->d_ino = objnum;
2376 odp->d_reclen = reclen;
2377 odp->d_namlen = strlen(zap.za_name);
2378 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2380 odp = (dirent64_t *)((intptr_t)odp + reclen);
2384 ASSERT(outcount <= bufsize);
2386 /* Prefetch znode */
2388 dmu_prefetch(os, objnum, 0, 0);
2392 * Move to the next entry, fill in the previous offset.
2394 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2395 zap_cursor_advance(&zc);
2396 offset = zap_cursor_serialize(&zc);
2401 if (cooks != NULL) {
2404 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2407 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2409 /* Subtract unused cookies */
2410 if (ncookies != NULL)
2411 *ncookies -= ncooks;
2413 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2414 iovp->iov_base += outcount;
2415 iovp->iov_len -= outcount;
2416 uio->uio_resid -= outcount;
2417 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2419 * Reset the pointer.
2421 offset = uio->uio_loffset;
2425 zap_cursor_fini(&zc);
2426 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2427 kmem_free(outbuf, bufsize);
2429 if (error == ENOENT)
2432 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2434 uio->uio_loffset = offset;
2436 if (error != 0 && cookies != NULL) {
2437 free(*cookies, M_TEMP);
2444 ulong_t zfs_fsync_sync_cnt = 4;
2447 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2449 znode_t *zp = VTOZ(vp);
2450 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2452 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2456 zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2463 * Get the requested file attributes and place them in the provided
2466 * IN: vp - vnode of file.
2467 * vap - va_mask identifies requested attributes.
2468 * If AT_XVATTR set, then optional attrs are requested
2469 * flags - ATTR_NOACLCHECK (CIFS server context)
2470 * cr - credentials of caller.
2471 * ct - caller context
2473 * OUT: vap - attribute values.
2475 * RETURN: 0 (always succeeds)
2479 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2480 caller_context_t *ct)
2482 znode_t *zp = VTOZ(vp);
2483 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2487 u_longlong_t nblocks;
2489 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2490 xoptattr_t *xoap = NULL;
2491 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2498 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2499 * Also, if we are the owner don't bother, since owner should
2500 * always be allowed to read basic attributes of file.
2502 if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
2503 (pzp->zp_uid != crgetuid(cr))) {
2504 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2512 * Return all attributes. It's cheaper to provide the answer
2513 * than to determine whether we were asked the question.
2516 mutex_enter(&zp->z_lock);
2517 vap->va_type = IFTOVT(pzp->zp_mode);
2518 vap->va_mode = pzp->zp_mode & ~S_IFMT;
2519 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2520 // vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2521 vap->va_nodeid = zp->z_id;
2522 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2523 links = pzp->zp_links + 1;
2525 links = pzp->zp_links;
2526 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2527 vap->va_size = pzp->zp_size;
2528 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2529 vap->va_rdev = zfs_cmpldev(pzp->zp_rdev);
2530 vap->va_seq = zp->z_seq;
2531 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2534 * Add in any requested optional attributes and the create time.
2535 * Also set the corresponding bits in the returned attribute bitmap.
2537 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2538 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2540 ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
2541 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2544 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2545 xoap->xoa_readonly =
2546 ((pzp->zp_flags & ZFS_READONLY) != 0);
2547 XVA_SET_RTN(xvap, XAT_READONLY);
2550 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2552 ((pzp->zp_flags & ZFS_SYSTEM) != 0);
2553 XVA_SET_RTN(xvap, XAT_SYSTEM);
2556 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2558 ((pzp->zp_flags & ZFS_HIDDEN) != 0);
2559 XVA_SET_RTN(xvap, XAT_HIDDEN);
2562 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2563 xoap->xoa_nounlink =
2564 ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
2565 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2568 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2569 xoap->xoa_immutable =
2570 ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
2571 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2574 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2575 xoap->xoa_appendonly =
2576 ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
2577 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2580 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2582 ((pzp->zp_flags & ZFS_NODUMP) != 0);
2583 XVA_SET_RTN(xvap, XAT_NODUMP);
2586 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2588 ((pzp->zp_flags & ZFS_OPAQUE) != 0);
2589 XVA_SET_RTN(xvap, XAT_OPAQUE);
2592 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2593 xoap->xoa_av_quarantined =
2594 ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
2595 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2598 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2599 xoap->xoa_av_modified =
2600 ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
2601 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2604 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2605 vp->v_type == VREG &&
2606 (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
2608 dmu_object_info_t doi;
2611 * Only VREG files have anti-virus scanstamps, so we
2612 * won't conflict with symlinks in the bonus buffer.
2614 dmu_object_info_from_db(zp->z_dbuf, &doi);
2615 len = sizeof (xoap->xoa_av_scanstamp) +
2616 sizeof (znode_phys_t);
2617 if (len <= doi.doi_bonus_size) {
2619 * pzp points to the start of the
2620 * znode_phys_t. pzp + 1 points to the
2621 * first byte after the znode_phys_t.
2623 (void) memcpy(xoap->xoa_av_scanstamp,
2625 sizeof (xoap->xoa_av_scanstamp));
2626 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
2630 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2631 ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
2632 XVA_SET_RTN(xvap, XAT_CREATETIME);
2636 ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
2637 ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
2638 ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
2639 ZFS_TIME_DECODE(&vap->va_birthtime, pzp->zp_crtime);
2641 mutex_exit(&zp->z_lock);
2643 dmu_object_size_from_db(zp->z_dbuf, &blksize, &nblocks);
2644 vap->va_blksize = blksize;
2645 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2647 if (zp->z_blksz == 0) {
2649 * Block size hasn't been set; suggest maximal I/O transfers.
2651 vap->va_blksize = zfsvfs->z_max_blksz;
2659 * Set the file attributes to the values contained in the
2662 * IN: vp - vnode of file to be modified.
2663 * vap - new attribute values.
2664 * If AT_XVATTR set, then optional attrs are being set
2665 * flags - ATTR_UTIME set if non-default time values provided.
2666 * - ATTR_NOACLCHECK (CIFS context only).
2667 * cr - credentials of caller.
2668 * ct - caller context
2670 * RETURN: 0 if success
2671 * error code if failure
2674 * vp - ctime updated, mtime updated if size changed.
2678 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2679 caller_context_t *ct)
2681 znode_t *zp = VTOZ(vp);
2683 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2688 uint_t mask = vap->va_mask;
2690 uint64_t saved_mode;
2693 uint64_t new_uid, new_gid;
2695 int need_policy = FALSE;
2697 zfs_fuid_info_t *fuidp = NULL;
2698 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2700 zfs_acl_t *aclp = NULL;
2701 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2702 boolean_t fuid_dirtied = B_FALSE;
2707 if (mask & AT_NOSET)
2714 zilog = zfsvfs->z_log;
2717 * Make sure that if we have ephemeral uid/gid or xvattr specified
2718 * that file system is at proper version level
2721 if (zfsvfs->z_use_fuids == B_FALSE &&
2722 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2723 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2724 (mask & AT_XVATTR))) {
2729 if (mask & AT_SIZE && vp->v_type == VDIR) {
2734 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2740 * If this is an xvattr_t, then get a pointer to the structure of
2741 * optional attributes. If this is NULL, then we have a vattr_t.
2743 xoap = xva_getxoptattr(xvap);
2745 xva_init(&tmpxvattr);
2748 * Immutable files can only alter immutable bit and atime
2750 if ((pzp->zp_flags & ZFS_IMMUTABLE) &&
2751 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2752 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2757 if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) {
2763 * Verify timestamps doesn't overflow 32 bits.
2764 * ZFS can handle large timestamps, but 32bit syscalls can't
2765 * handle times greater than 2039. This check should be removed
2766 * once large timestamps are fully supported.
2768 if (mask & (AT_ATIME | AT_MTIME)) {
2769 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2770 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2779 /* Can this be moved to before the top label? */
2780 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2786 * First validate permissions
2789 if (mask & AT_SIZE) {
2790 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2796 * XXX - Note, we are not providing any open
2797 * mode flags here (like FNDELAY), so we may
2798 * block if there are locks present... this
2799 * should be addressed in openat().
2801 /* XXX - would it be OK to generate a log record here? */
2802 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2809 if (mask & (AT_ATIME|AT_MTIME) ||
2810 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2811 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2812 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2813 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2814 XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
2815 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2818 if (mask & (AT_UID|AT_GID)) {
2819 int idmask = (mask & (AT_UID|AT_GID));
2824 * NOTE: even if a new mode is being set,
2825 * we may clear S_ISUID/S_ISGID bits.
2828 if (!(mask & AT_MODE))
2829 vap->va_mode = pzp->zp_mode;
2832 * Take ownership or chgrp to group we are a member of
2835 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2836 take_group = (mask & AT_GID) &&
2837 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2840 * If both AT_UID and AT_GID are set then take_owner and
2841 * take_group must both be set in order to allow taking
2844 * Otherwise, send the check through secpolicy_vnode_setattr()
2848 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2849 ((idmask == AT_UID) && take_owner) ||
2850 ((idmask == AT_GID) && take_group)) {
2851 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2852 skipaclchk, cr) == 0) {
2854 * Remove setuid/setgid for non-privileged users
2856 secpolicy_setid_clear(vap, vp, cr);
2857 trim_mask = (mask & (AT_UID|AT_GID));
2866 mutex_enter(&zp->z_lock);
2867 oldva.va_mode = pzp->zp_mode;
2868 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2869 if (mask & AT_XVATTR) {
2871 * Update xvattr mask to include only those attributes
2872 * that are actually changing.
2874 * the bits will be restored prior to actually setting
2875 * the attributes so the caller thinks they were set.
2877 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2878 if (xoap->xoa_appendonly !=
2879 ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) {
2882 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2883 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2887 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2888 if (xoap->xoa_nounlink !=
2889 ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) {
2892 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2893 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2897 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2898 if (xoap->xoa_immutable !=
2899 ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) {
2902 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2903 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2907 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2908 if (xoap->xoa_nodump !=
2909 ((pzp->zp_flags & ZFS_NODUMP) != 0)) {
2912 XVA_CLR_REQ(xvap, XAT_NODUMP);
2913 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2917 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2918 if (xoap->xoa_av_modified !=
2919 ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) {
2922 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2923 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2927 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2928 if ((vp->v_type != VREG &&
2929 xoap->xoa_av_quarantined) ||
2930 xoap->xoa_av_quarantined !=
2931 ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) {
2934 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2935 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2939 if (need_policy == FALSE &&
2940 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2941 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2946 mutex_exit(&zp->z_lock);
2948 if (mask & AT_MODE) {
2949 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2950 err = secpolicy_setid_setsticky_clear(vp, vap,
2956 trim_mask |= AT_MODE;
2964 * If trim_mask is set then take ownership
2965 * has been granted or write_acl is present and user
2966 * has the ability to modify mode. In that case remove
2967 * UID|GID and or MODE from mask so that
2968 * secpolicy_vnode_setattr() doesn't revoke it.
2972 saved_mask = vap->va_mask;
2973 vap->va_mask &= ~trim_mask;
2974 if (trim_mask & AT_MODE) {
2976 * Save the mode, as secpolicy_vnode_setattr()
2977 * will overwrite it with ova.va_mode.
2979 saved_mode = vap->va_mode;
2982 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2983 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2990 vap->va_mask |= saved_mask;
2991 if (trim_mask & AT_MODE) {
2993 * Recover the mode after
2994 * secpolicy_vnode_setattr().
2996 vap->va_mode = saved_mode;
3002 * secpolicy_vnode_setattr, or take ownership may have
3005 mask = vap->va_mask;
3007 tx = dmu_tx_create(zfsvfs->z_os);
3008 dmu_tx_hold_bonus(tx, zp->z_id);
3010 if (mask & AT_MODE) {
3011 uint64_t pmode = pzp->zp_mode;
3013 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3015 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3017 if (pzp->zp_acl.z_acl_extern_obj) {
3018 /* Are we upgrading ACL from old V0 format to new V1 */
3019 if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
3020 pzp->zp_acl.z_acl_version ==
3021 ZFS_ACL_VERSION_INITIAL) {
3022 dmu_tx_hold_free(tx,
3023 pzp->zp_acl.z_acl_extern_obj, 0,
3025 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3026 0, aclp->z_acl_bytes);
3028 dmu_tx_hold_write(tx,
3029 pzp->zp_acl.z_acl_extern_obj, 0,
3032 } else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3033 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3034 0, aclp->z_acl_bytes);
3038 if (mask & (AT_UID | AT_GID)) {
3039 if (pzp->zp_xattr) {
3040 err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
3043 dmu_tx_hold_bonus(tx, attrzp->z_id);
3045 if (mask & AT_UID) {
3046 new_uid = zfs_fuid_create(zfsvfs,
3047 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3048 if (new_uid != pzp->zp_uid &&
3049 zfs_usergroup_overquota(zfsvfs, B_FALSE, new_uid)) {
3055 if (mask & AT_GID) {
3056 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3057 cr, ZFS_GROUP, &fuidp);
3058 if (new_gid != pzp->zp_gid &&
3059 zfs_usergroup_overquota(zfsvfs, B_TRUE, new_gid)) {
3064 fuid_dirtied = zfsvfs->z_fuid_dirty;
3066 if (zfsvfs->z_fuid_obj == 0) {
3067 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
3068 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3069 FUID_SIZE_ESTIMATE(zfsvfs));
3070 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ,
3073 dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
3074 dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
3075 FUID_SIZE_ESTIMATE(zfsvfs));
3080 err = dmu_tx_assign(tx, TXG_NOWAIT);
3082 if (err == ERESTART)
3087 dmu_buf_will_dirty(zp->z_dbuf, tx);
3090 * Set each attribute requested.
3091 * We group settings according to the locks they need to acquire.
3093 * Note: you cannot set ctime directly, although it will be
3094 * updated as a side-effect of calling this function.
3097 mutex_enter(&zp->z_lock);
3099 if (mask & AT_MODE) {
3100 mutex_enter(&zp->z_acl_lock);
3101 zp->z_phys->zp_mode = new_mode;
3102 err = zfs_aclset_common(zp, aclp, cr, tx);
3103 ASSERT3U(err, ==, 0);
3104 zp->z_acl_cached = aclp;
3106 mutex_exit(&zp->z_acl_lock);
3110 mutex_enter(&attrzp->z_lock);
3112 if (mask & AT_UID) {
3113 pzp->zp_uid = new_uid;
3115 attrzp->z_phys->zp_uid = new_uid;
3118 if (mask & AT_GID) {
3119 pzp->zp_gid = new_gid;
3121 attrzp->z_phys->zp_gid = new_gid;
3125 mutex_exit(&attrzp->z_lock);
3127 if (mask & AT_ATIME)
3128 ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
3130 if (mask & AT_MTIME)
3131 ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
3133 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3135 zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
3137 zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
3139 * Do this after setting timestamps to prevent timestamp
3140 * update from toggling bit
3143 if (xoap && (mask & AT_XVATTR)) {
3146 * restore trimmed off masks
3147 * so that return masks can be set for caller.
3150 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3151 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3153 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3154 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3156 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3157 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3159 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3160 XVA_SET_REQ(xvap, XAT_NODUMP);
3162 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3163 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3165 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3166 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3169 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
3171 dmu_object_info_t doi;
3173 ASSERT(vp->v_type == VREG);
3175 /* Grow the bonus buffer if necessary. */
3176 dmu_object_info_from_db(zp->z_dbuf, &doi);
3177 len = sizeof (xoap->xoa_av_scanstamp) +
3178 sizeof (znode_phys_t);
3179 if (len > doi.doi_bonus_size)
3180 VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
3182 zfs_xvattr_set(zp, xvap);
3186 zfs_fuid_sync(zfsvfs, tx);
3189 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3191 mutex_exit(&zp->z_lock);
3195 VN_RELE(ZTOV(attrzp));
3201 zfs_fuid_info_free(fuidp);
3210 if (err == ERESTART)
3217 typedef struct zfs_zlock {
3218 krwlock_t *zl_rwlock; /* lock we acquired */
3219 znode_t *zl_znode; /* znode we held */
3220 struct zfs_zlock *zl_next; /* next in list */
3224 * Drop locks and release vnodes that were held by zfs_rename_lock().
3227 zfs_rename_unlock(zfs_zlock_t **zlpp)
3231 while ((zl = *zlpp) != NULL) {
3232 if (zl->zl_znode != NULL)
3233 VN_RELE(ZTOV(zl->zl_znode));
3234 rw_exit(zl->zl_rwlock);
3235 *zlpp = zl->zl_next;
3236 kmem_free(zl, sizeof (*zl));
3241 * Search back through the directory tree, using the ".." entries.
3242 * Lock each directory in the chain to prevent concurrent renames.
3243 * Fail any attempt to move a directory into one of its own descendants.
3244 * XXX - z_parent_lock can overlap with map or grow locks
3247 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3251 uint64_t rootid = zp->z_zfsvfs->z_root;
3252 uint64_t *oidp = &zp->z_id;
3253 krwlock_t *rwlp = &szp->z_parent_lock;
3254 krw_t rw = RW_WRITER;
3257 * First pass write-locks szp and compares to zp->z_id.
3258 * Later passes read-lock zp and compare to zp->z_parent.
3261 if (!rw_tryenter(rwlp, rw)) {
3263 * Another thread is renaming in this path.
3264 * Note that if we are a WRITER, we don't have any
3265 * parent_locks held yet.
3267 if (rw == RW_READER && zp->z_id > szp->z_id) {
3269 * Drop our locks and restart
3271 zfs_rename_unlock(&zl);
3275 rwlp = &szp->z_parent_lock;
3280 * Wait for other thread to drop its locks
3286 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3287 zl->zl_rwlock = rwlp;
3288 zl->zl_znode = NULL;
3289 zl->zl_next = *zlpp;
3292 if (*oidp == szp->z_id) /* We're a descendant of szp */
3295 if (*oidp == rootid) /* We've hit the top */
3298 if (rw == RW_READER) { /* i.e. not the first pass */
3299 int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
3304 oidp = &zp->z_phys->zp_parent;
3305 rwlp = &zp->z_parent_lock;
3308 } while (zp->z_id != sdzp->z_id);
3314 * Move an entry from the provided source directory to the target
3315 * directory. Change the entry name as indicated.
3317 * IN: sdvp - Source directory containing the "old entry".
3318 * snm - Old entry name.
3319 * tdvp - Target directory to contain the "new entry".
3320 * tnm - New entry name.
3321 * cr - credentials of caller.
3322 * ct - caller context
3323 * flags - case flags
3325 * RETURN: 0 if success
3326 * error code if failure
3329 * sdvp,tdvp - ctime|mtime updated
3333 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3334 caller_context_t *ct, int flags)
3336 znode_t *tdzp, *szp, *tzp;
3337 znode_t *sdzp = VTOZ(sdvp);
3338 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3341 zfs_dirlock_t *sdl, *tdl;
3344 int cmp, serr, terr;
3349 ZFS_VERIFY_ZP(sdzp);
3350 zilog = zfsvfs->z_log;
3353 * Make sure we have the real vp for the target directory.
3355 if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3358 if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3364 ZFS_VERIFY_ZP(tdzp);
3365 if (zfsvfs->z_utf8 && u8_validate(tnm,
3366 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3371 if (flags & FIGNORECASE)
3380 * This is to prevent the creation of links into attribute space
3381 * by renaming a linked file into/outof an attribute directory.
3382 * See the comment in zfs_link() for why this is considered bad.
3384 if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
3385 (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
3391 * Lock source and target directory entries. To prevent deadlock,
3392 * a lock ordering must be defined. We lock the directory with
3393 * the smallest object id first, or if it's a tie, the one with
3394 * the lexically first name.
3396 if (sdzp->z_id < tdzp->z_id) {
3398 } else if (sdzp->z_id > tdzp->z_id) {
3402 * First compare the two name arguments without
3403 * considering any case folding.
3405 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3407 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3408 ASSERT(error == 0 || !zfsvfs->z_utf8);
3411 * POSIX: "If the old argument and the new argument
3412 * both refer to links to the same existing file,
3413 * the rename() function shall return successfully
3414 * and perform no other action."
3420 * If the file system is case-folding, then we may
3421 * have some more checking to do. A case-folding file
3422 * system is either supporting mixed case sensitivity
3423 * access or is completely case-insensitive. Note
3424 * that the file system is always case preserving.
3426 * In mixed sensitivity mode case sensitive behavior
3427 * is the default. FIGNORECASE must be used to
3428 * explicitly request case insensitive behavior.
3430 * If the source and target names provided differ only
3431 * by case (e.g., a request to rename 'tim' to 'Tim'),
3432 * we will treat this as a special case in the
3433 * case-insensitive mode: as long as the source name
3434 * is an exact match, we will allow this to proceed as
3435 * a name-change request.
3437 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3438 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3439 flags & FIGNORECASE)) &&
3440 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3443 * case preserving rename request, require exact
3452 * If the source and destination directories are the same, we should
3453 * grab the z_name_lock of that directory only once.
3457 rw_enter(&sdzp->z_name_lock, RW_READER);
3461 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3462 ZEXISTS | zflg, NULL, NULL);
3463 terr = zfs_dirent_lock(&tdl,
3464 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3466 terr = zfs_dirent_lock(&tdl,
3467 tdzp, tnm, &tzp, zflg, NULL, NULL);
3468 serr = zfs_dirent_lock(&sdl,
3469 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3475 * Source entry invalid or not there.
3478 zfs_dirent_unlock(tdl);
3484 rw_exit(&sdzp->z_name_lock);
3486 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0)
3492 zfs_dirent_unlock(sdl);
3496 rw_exit(&sdzp->z_name_lock);
3498 if (strcmp(tnm, "..") == 0)
3505 * Must have write access at the source to remove the old entry
3506 * and write access at the target to create the new entry.
3507 * Note that if target and source are the same, this can be
3508 * done in a single check.
3511 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3514 if (ZTOV(szp)->v_type == VDIR) {
3516 * Check to make sure rename is valid.
3517 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3519 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3524 * Does target exist?
3528 * Source and target must be the same type.
3530 if (ZTOV(szp)->v_type == VDIR) {
3531 if (ZTOV(tzp)->v_type != VDIR) {
3536 if (ZTOV(tzp)->v_type == VDIR) {
3542 * POSIX dictates that when the source and target
3543 * entries refer to the same file object, rename
3544 * must do nothing and exit without error.
3546 if (szp->z_id == tzp->z_id) {
3552 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3554 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3557 * notify the target directory if it is not the same
3558 * as source directory.
3561 vnevent_rename_dest_dir(tdvp, ct);
3564 tx = dmu_tx_create(zfsvfs->z_os);
3565 dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */
3566 dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */
3567 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3568 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3570 dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */
3572 dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */
3573 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3574 error = dmu_tx_assign(tx, TXG_NOWAIT);
3577 zfs_rename_unlock(&zl);
3578 zfs_dirent_unlock(sdl);
3579 zfs_dirent_unlock(tdl);
3582 rw_exit(&sdzp->z_name_lock);
3587 if (error == ERESTART) {
3597 if (tzp) /* Attempt to remove the existing target */
3598 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3601 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3603 szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
3605 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3608 zfs_log_rename(zilog, tx,
3609 TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
3610 sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
3612 /* Update path information for the target vnode */
3613 vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3615 #ifdef FREEBSD_NAMECACHE
3626 zfs_rename_unlock(&zl);
3628 zfs_dirent_unlock(sdl);
3629 zfs_dirent_unlock(tdl);
3632 rw_exit(&sdzp->z_name_lock);
3644 * Insert the indicated symbolic reference entry into the directory.
3646 * IN: dvp - Directory to contain new symbolic link.
3647 * link - Name for new symlink entry.
3648 * vap - Attributes of new entry.
3649 * target - Target path of new symlink.
3650 * cr - credentials of caller.
3651 * ct - caller context
3652 * flags - case flags
3654 * RETURN: 0 if success
3655 * error code if failure
3658 * dvp - ctime|mtime updated
3662 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
3663 cred_t *cr, kthread_t *td)
3665 znode_t *zp, *dzp = VTOZ(dvp);
3668 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3670 int len = strlen(link);
3673 zfs_acl_ids_t acl_ids;
3674 boolean_t fuid_dirtied;
3677 ASSERT(vap->va_type == VLNK);
3681 zilog = zfsvfs->z_log;
3683 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3684 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3688 if (flags & FIGNORECASE)
3691 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3696 if (len > MAXPATHLEN) {
3698 return (ENAMETOOLONG);
3702 * Attempt to lock directory; fail if entry already exists.
3704 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3710 VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids));
3711 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3712 zfs_acl_ids_free(&acl_ids);
3713 zfs_dirent_unlock(dl);
3717 tx = dmu_tx_create(zfsvfs->z_os);
3718 fuid_dirtied = zfsvfs->z_fuid_dirty;
3719 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3720 dmu_tx_hold_bonus(tx, dzp->z_id);
3721 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3722 if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE)
3723 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
3725 zfs_fuid_txhold(zfsvfs, tx);
3726 error = dmu_tx_assign(tx, TXG_NOWAIT);
3728 zfs_acl_ids_free(&acl_ids);
3729 zfs_dirent_unlock(dl);
3730 if (error == ERESTART) {
3740 dmu_buf_will_dirty(dzp->z_dbuf, tx);
3743 * Create a new object for the symlink.
3744 * Put the link content into bonus buffer if it will fit;
3745 * otherwise, store it just like any other file data.
3747 if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
3748 zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, &acl_ids);
3750 bcopy(link, zp->z_phys + 1, len);
3754 zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
3757 zfs_fuid_sync(zfsvfs, tx);
3759 * Nothing can access the znode yet so no locking needed
3760 * for growing the znode's blocksize.
3762 zfs_grow_blocksize(zp, len, tx);
3764 VERIFY(0 == dmu_buf_hold(zfsvfs->z_os,
3765 zp->z_id, 0, FTAG, &dbp));
3766 dmu_buf_will_dirty(dbp, tx);
3768 ASSERT3U(len, <=, dbp->db_size);
3769 bcopy(link, dbp->db_data, len);
3770 dmu_buf_rele(dbp, FTAG);
3772 zp->z_phys->zp_size = len;
3775 * Insert the new object into the directory.
3777 (void) zfs_link_create(dl, zp, tx, ZNEW);
3779 uint64_t txtype = TX_SYMLINK;
3780 if (flags & FIGNORECASE)
3782 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3786 zfs_acl_ids_free(&acl_ids);
3790 zfs_dirent_unlock(dl);
3797 * Return, in the buffer contained in the provided uio structure,
3798 * the symbolic path referred to by vp.
3800 * IN: vp - vnode of symbolic link.
3801 * uoip - structure to contain the link path.
3802 * cr - credentials of caller.
3803 * ct - caller context
3805 * OUT: uio - structure to contain the link path.
3807 * RETURN: 0 if success
3808 * error code if failure
3811 * vp - atime updated
3815 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3817 znode_t *zp = VTOZ(vp);
3818 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3825 bufsz = (size_t)zp->z_phys->zp_size;
3826 if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
3827 error = uiomove(zp->z_phys + 1,
3828 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3831 error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
3836 error = uiomove(dbp->db_data,
3837 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3838 dmu_buf_rele(dbp, FTAG);
3841 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3847 * Insert a new entry into directory tdvp referencing svp.
3849 * IN: tdvp - Directory to contain new entry.
3850 * svp - vnode of new entry.
3851 * name - name of new entry.
3852 * cr - credentials of caller.
3853 * ct - caller context
3855 * RETURN: 0 if success
3856 * error code if failure
3859 * tdvp - ctime|mtime updated
3860 * svp - ctime updated
3864 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
3865 caller_context_t *ct, int flags)
3867 znode_t *dzp = VTOZ(tdvp);
3869 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3879 ASSERT(tdvp->v_type == VDIR);
3883 zilog = zfsvfs->z_log;
3885 if (VOP_REALVP(svp, &realvp, ct) == 0)
3889 * POSIX dictates that we return EPERM here.
3890 * Better choices include ENOTSUP or EISDIR.
3892 if (svp->v_type == VDIR) {
3897 if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) {
3905 /* Prevent links to .zfs/shares files */
3907 if (szp->z_phys->zp_parent == zfsvfs->z_shares_dir) {
3912 if (zfsvfs->z_utf8 && u8_validate(name,
3913 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3917 if (flags & FIGNORECASE)
3921 * We do not support links between attributes and non-attributes
3922 * because of the potential security risk of creating links
3923 * into "normal" file space in order to circumvent restrictions
3924 * imposed in attribute space.
3926 if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
3927 (dzp->z_phys->zp_flags & ZFS_XATTR)) {
3933 owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER);
3934 if (owner != crgetuid(cr) &&
3935 secpolicy_basic_link(svp, cr) != 0) {
3940 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3947 * Attempt to lock directory; fail if entry already exists.
3949 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3955 tx = dmu_tx_create(zfsvfs->z_os);
3956 dmu_tx_hold_bonus(tx, szp->z_id);
3957 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3958 error = dmu_tx_assign(tx, TXG_NOWAIT);
3960 zfs_dirent_unlock(dl);
3961 if (error == ERESTART) {
3971 error = zfs_link_create(dl, szp, tx, 0);
3974 uint64_t txtype = TX_LINK;
3975 if (flags & FIGNORECASE)
3977 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3982 zfs_dirent_unlock(dl);
3985 vnevent_link(svp, ct);
3994 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3996 znode_t *zp = VTOZ(vp);
3997 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4000 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4001 if (zp->z_dbuf == NULL) {
4003 * The fs has been unmounted, or we did a
4004 * suspend/resume and this file no longer exists.
4007 vp->v_count = 0; /* count arrives as 1 */
4009 vrecycle(vp, curthread);
4010 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4014 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4015 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4017 dmu_tx_hold_bonus(tx, zp->z_id);
4018 error = dmu_tx_assign(tx, TXG_WAIT);
4022 dmu_buf_will_dirty(zp->z_dbuf, tx);
4023 mutex_enter(&zp->z_lock);
4024 zp->z_atime_dirty = 0;
4025 mutex_exit(&zp->z_lock);
4031 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4034 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
4035 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
4039 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4041 znode_t *zp = VTOZ(vp);
4042 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4044 uint64_t object = zp->z_id;
4050 gen = (uint32_t)zp->z_gen;
4052 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4053 fidp->fid_len = size;
4055 zfid = (zfid_short_t *)fidp;
4057 zfid->zf_len = size;
4059 for (i = 0; i < sizeof (zfid->zf_object); i++)
4060 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4062 /* Must have a non-zero generation number to distinguish from .zfs */
4065 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4066 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4068 if (size == LONG_FID_LEN) {
4069 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4072 zlfid = (zfid_long_t *)fidp;
4074 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4075 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4077 /* XXX - this should be the generation number for the objset */
4078 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4079 zlfid->zf_setgen[i] = 0;
4087 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4088 caller_context_t *ct)
4100 case _PC_FILESIZEBITS:
4105 case _PC_XATTR_EXISTS:
4107 zfsvfs = zp->z_zfsvfs;
4111 error = zfs_dirent_lock(&dl, zp, "", &xzp,
4112 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4114 zfs_dirent_unlock(dl);
4115 if (!zfs_dirempty(xzp))
4118 } else if (error == ENOENT) {
4120 * If there aren't extended attributes, it's the
4121 * same as having zero of them.
4129 case _PC_ACL_EXTENDED:
4137 case _PC_ACL_PATH_MAX:
4138 *valp = ACL_MAX_ENTRIES;
4141 case _PC_MIN_HOLE_SIZE:
4142 *valp = (int)SPA_MINBLOCKSIZE;
4146 return (EOPNOTSUPP);
4152 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4153 caller_context_t *ct)
4155 znode_t *zp = VTOZ(vp);
4156 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4158 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4162 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4170 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4171 caller_context_t *ct)
4173 znode_t *zp = VTOZ(vp);
4174 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4176 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4180 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4186 zfs_freebsd_open(ap)
4187 struct vop_open_args /* {
4190 struct ucred *a_cred;
4191 struct thread *a_td;
4194 vnode_t *vp = ap->a_vp;
4195 znode_t *zp = VTOZ(vp);
4198 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
4200 vnode_create_vobject(vp, zp->z_phys->zp_size, ap->a_td);
4205 zfs_freebsd_close(ap)
4206 struct vop_close_args /* {
4209 struct ucred *a_cred;
4210 struct thread *a_td;
4214 return (zfs_close(ap->a_vp, ap->a_fflag, 0, 0, ap->a_cred, NULL));
4218 zfs_freebsd_ioctl(ap)
4219 struct vop_ioctl_args /* {
4229 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4230 ap->a_fflag, ap->a_cred, NULL, NULL));
4234 zfs_freebsd_read(ap)
4235 struct vop_read_args /* {
4239 struct ucred *a_cred;
4243 return (zfs_read(ap->a_vp, ap->a_uio, ap->a_ioflag, ap->a_cred, NULL));
4247 zfs_freebsd_write(ap)
4248 struct vop_write_args /* {
4252 struct ucred *a_cred;
4256 return (zfs_write(ap->a_vp, ap->a_uio, ap->a_ioflag, ap->a_cred, NULL));
4260 zfs_freebsd_access(ap)
4261 struct vop_access_args /* {
4263 accmode_t a_accmode;
4264 struct ucred *a_cred;
4265 struct thread *a_td;
4272 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4274 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4276 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
4279 * VADMIN has to be handled by vaccess().
4282 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4284 vnode_t *vp = ap->a_vp;
4285 znode_t *zp = VTOZ(vp);
4286 znode_phys_t *zphys = zp->z_phys;
4288 error = vaccess(vp->v_type, zphys->zp_mode,
4289 zphys->zp_uid, zphys->zp_gid, accmode, ap->a_cred,
4298 zfs_freebsd_lookup(ap)
4299 struct vop_lookup_args /* {
4300 struct vnode *a_dvp;
4301 struct vnode **a_vpp;
4302 struct componentname *a_cnp;
4305 struct componentname *cnp = ap->a_cnp;
4306 char nm[NAME_MAX + 1];
4308 ASSERT(cnp->cn_namelen < sizeof(nm));
4309 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
4311 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4312 cnp->cn_cred, cnp->cn_thread, 0));
4316 zfs_freebsd_create(ap)
4317 struct vop_create_args /* {
4318 struct vnode *a_dvp;
4319 struct vnode **a_vpp;
4320 struct componentname *a_cnp;
4321 struct vattr *a_vap;
4324 struct componentname *cnp = ap->a_cnp;
4325 vattr_t *vap = ap->a_vap;
4328 ASSERT(cnp->cn_flags & SAVENAME);
4330 vattr_init_mask(vap);
4331 mode = vap->va_mode & ALLPERMS;
4333 return (zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
4334 ap->a_vpp, cnp->cn_cred, cnp->cn_thread));
4338 zfs_freebsd_remove(ap)
4339 struct vop_remove_args /* {
4340 struct vnode *a_dvp;
4342 struct componentname *a_cnp;
4346 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4348 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr,
4349 ap->a_cnp->cn_cred, NULL, 0));
4353 zfs_freebsd_mkdir(ap)
4354 struct vop_mkdir_args /* {
4355 struct vnode *a_dvp;
4356 struct vnode **a_vpp;
4357 struct componentname *a_cnp;
4358 struct vattr *a_vap;
4361 vattr_t *vap = ap->a_vap;
4363 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4365 vattr_init_mask(vap);
4367 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
4368 ap->a_cnp->cn_cred, NULL, 0, NULL));
4372 zfs_freebsd_rmdir(ap)
4373 struct vop_rmdir_args /* {
4374 struct vnode *a_dvp;
4376 struct componentname *a_cnp;
4379 struct componentname *cnp = ap->a_cnp;
4381 ASSERT(cnp->cn_flags & SAVENAME);
4383 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0));
4387 zfs_freebsd_readdir(ap)
4388 struct vop_readdir_args /* {
4391 struct ucred *a_cred;
4398 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
4399 ap->a_ncookies, ap->a_cookies));
4403 zfs_freebsd_fsync(ap)
4404 struct vop_fsync_args /* {
4407 struct thread *a_td;
4412 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
4416 zfs_freebsd_getattr(ap)
4417 struct vop_getattr_args /* {
4419 struct vattr *a_vap;
4420 struct ucred *a_cred;
4421 struct thread *a_td;
4424 vattr_t *vap = ap->a_vap;
4430 xvap.xva_vattr = *vap;
4431 xvap.xva_vattr.va_mask |= AT_XVATTR;
4433 /* Convert chflags into ZFS-type flags. */
4434 /* XXX: what about SF_SETTABLE?. */
4435 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
4436 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
4437 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
4438 XVA_SET_REQ(&xvap, XAT_NODUMP);
4439 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
4443 /* Convert ZFS xattr into chflags. */
4444 #define FLAG_CHECK(fflag, xflag, xfield) do { \
4445 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
4446 fflags |= (fflag); \
4448 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
4449 xvap.xva_xoptattrs.xoa_immutable);
4450 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
4451 xvap.xva_xoptattrs.xoa_appendonly);
4452 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
4453 xvap.xva_xoptattrs.xoa_nounlink);
4454 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
4455 xvap.xva_xoptattrs.xoa_nodump);
4457 *vap = xvap.xva_vattr;
4458 vap->va_flags = fflags;
4463 zfs_freebsd_setattr(ap)
4464 struct vop_setattr_args /* {
4466 struct vattr *a_vap;
4467 struct ucred *a_cred;
4468 struct thread *a_td;
4471 vnode_t *vp = ap->a_vp;
4472 vattr_t *vap = ap->a_vap;
4473 cred_t *cred = ap->a_cred;
4478 vattr_init_mask(vap);
4479 vap->va_mask &= ~AT_NOSET;
4482 xvap.xva_vattr = *vap;
4484 zflags = VTOZ(vp)->z_phys->zp_flags;
4486 if (vap->va_flags != VNOVAL) {
4487 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
4490 if (zfsvfs->z_use_fuids == B_FALSE)
4491 return (EOPNOTSUPP);
4493 fflags = vap->va_flags;
4494 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_NODUMP)) != 0)
4495 return (EOPNOTSUPP);
4497 * Unprivileged processes are not permitted to unset system
4498 * flags, or modify flags if any system flags are set.
4499 * Privileged non-jail processes may not modify system flags
4500 * if securelevel > 0 and any existing system flags are set.
4501 * Privileged jail processes behave like privileged non-jail
4502 * processes if the security.jail.chflags_allowed sysctl is
4503 * is non-zero; otherwise, they behave like unprivileged
4506 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
4507 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
4509 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4510 error = securelevel_gt(cred, 0);
4516 * Callers may only modify the file flags on objects they
4517 * have VADMIN rights for.
4519 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
4522 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4526 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
4531 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
4532 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
4533 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
4534 XVA_SET_REQ(&xvap, (xflag)); \
4535 (xfield) = ((fflags & (fflag)) != 0); \
4538 /* Convert chflags into ZFS-type flags. */
4539 /* XXX: what about SF_SETTABLE?. */
4540 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
4541 xvap.xva_xoptattrs.xoa_immutable);
4542 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
4543 xvap.xva_xoptattrs.xoa_appendonly);
4544 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
4545 xvap.xva_xoptattrs.xoa_nounlink);
4546 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
4547 xvap.xva_xoptattrs.xoa_nodump);
4550 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
4554 zfs_freebsd_rename(ap)
4555 struct vop_rename_args /* {
4556 struct vnode *a_fdvp;
4557 struct vnode *a_fvp;
4558 struct componentname *a_fcnp;
4559 struct vnode *a_tdvp;
4560 struct vnode *a_tvp;
4561 struct componentname *a_tcnp;
4564 vnode_t *fdvp = ap->a_fdvp;
4565 vnode_t *fvp = ap->a_fvp;
4566 vnode_t *tdvp = ap->a_tdvp;
4567 vnode_t *tvp = ap->a_tvp;
4570 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
4571 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
4573 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp,
4574 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0);
4589 zfs_freebsd_symlink(ap)
4590 struct vop_symlink_args /* {
4591 struct vnode *a_dvp;
4592 struct vnode **a_vpp;
4593 struct componentname *a_cnp;
4594 struct vattr *a_vap;
4598 struct componentname *cnp = ap->a_cnp;
4599 vattr_t *vap = ap->a_vap;
4601 ASSERT(cnp->cn_flags & SAVENAME);
4603 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
4604 vattr_init_mask(vap);
4606 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
4607 ap->a_target, cnp->cn_cred, cnp->cn_thread));
4611 zfs_freebsd_readlink(ap)
4612 struct vop_readlink_args /* {
4615 struct ucred *a_cred;
4619 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
4623 zfs_freebsd_link(ap)
4624 struct vop_link_args /* {
4625 struct vnode *a_tdvp;
4627 struct componentname *a_cnp;
4630 struct componentname *cnp = ap->a_cnp;
4632 ASSERT(cnp->cn_flags & SAVENAME);
4634 return (zfs_link(ap->a_tdvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
4638 zfs_freebsd_inactive(ap)
4639 struct vop_inactive_args /* {
4641 struct thread *a_td;
4644 vnode_t *vp = ap->a_vp;
4646 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
4651 zfs_reclaim_complete(void *arg, int pending)
4654 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4656 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4657 if (zp->z_dbuf != NULL) {
4658 ZFS_OBJ_HOLD_ENTER(zfsvfs, zp->z_id);
4659 zfs_znode_dmu_fini(zp);
4660 ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
4663 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4665 * If the file system is being unmounted, there is a process waiting
4666 * for us, wake it up.
4668 if (zfsvfs->z_unmounted)
4673 zfs_freebsd_reclaim(ap)
4674 struct vop_reclaim_args /* {
4676 struct thread *a_td;
4679 vnode_t *vp = ap->a_vp;
4680 znode_t *zp = VTOZ(vp);
4681 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4683 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4688 * Destroy the vm object and flush associated pages.
4690 vnode_destroy_vobject(vp);
4692 mutex_enter(&zp->z_lock);
4693 ASSERT(zp->z_phys != NULL);
4695 mutex_exit(&zp->z_lock);
4699 else if (zp->z_dbuf == NULL)
4701 else /* if (!zp->z_unlinked && zp->z_dbuf != NULL) */ {
4704 locked = MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)) ? 2 :
4705 ZFS_OBJ_HOLD_TRYENTER(zfsvfs, zp->z_id);
4708 * Lock can't be obtained due to deadlock possibility,
4709 * so defer znode destruction.
4711 TASK_INIT(&zp->z_task, 0, zfs_reclaim_complete, zp);
4712 taskqueue_enqueue(taskqueue_thread, &zp->z_task);
4714 zfs_znode_dmu_fini(zp);
4716 ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
4722 ASSERT(vp->v_holdcnt >= 1);
4724 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4730 struct vop_fid_args /* {
4736 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
4740 zfs_freebsd_pathconf(ap)
4741 struct vop_pathconf_args /* {
4744 register_t *a_retval;
4750 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
4752 *ap->a_retval = val;
4753 else if (error == EOPNOTSUPP)
4754 error = vop_stdpathconf(ap);
4759 zfs_freebsd_fifo_pathconf(ap)
4760 struct vop_pathconf_args /* {
4763 register_t *a_retval;
4767 switch (ap->a_name) {
4768 case _PC_ACL_EXTENDED:
4770 case _PC_ACL_PATH_MAX:
4771 case _PC_MAC_PRESENT:
4772 return (zfs_freebsd_pathconf(ap));
4774 return (fifo_specops.vop_pathconf(ap));
4779 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
4780 * extended attribute name:
4783 * system freebsd:system:
4784 * user (none, can be used to access ZFS fsattr(5) attributes
4785 * created on Solaris)
4788 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
4791 const char *namespace, *prefix, *suffix;
4793 /* We don't allow '/' character in attribute name. */
4794 if (strchr(name, '/') != NULL)
4796 /* We don't allow attribute names that start with "freebsd:" string. */
4797 if (strncmp(name, "freebsd:", 8) == 0)
4800 bzero(attrname, size);
4802 switch (attrnamespace) {
4803 case EXTATTR_NAMESPACE_USER:
4805 prefix = "freebsd:";
4806 namespace = EXTATTR_NAMESPACE_USER_STRING;
4810 * This is the default namespace by which we can access all
4811 * attributes created on Solaris.
4813 prefix = namespace = suffix = "";
4816 case EXTATTR_NAMESPACE_SYSTEM:
4817 prefix = "freebsd:";
4818 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
4821 case EXTATTR_NAMESPACE_EMPTY:
4825 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
4827 return (ENAMETOOLONG);
4833 * Vnode operating to retrieve a named extended attribute.
4836 zfs_getextattr(struct vop_getextattr_args *ap)
4839 IN struct vnode *a_vp;
4840 IN int a_attrnamespace;
4841 IN const char *a_name;
4842 INOUT struct uio *a_uio;
4844 IN struct ucred *a_cred;
4845 IN struct thread *a_td;
4849 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
4850 struct thread *td = ap->a_td;
4851 struct nameidata nd;
4854 vnode_t *xvp = NULL, *vp;
4857 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
4858 ap->a_cred, ap->a_td, VREAD);
4862 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
4869 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
4877 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
4879 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
4881 NDFREE(&nd, NDF_ONLY_PNBUF);
4884 if (error == ENOENT)
4889 if (ap->a_size != NULL) {
4890 error = VOP_GETATTR(vp, &va, ap->a_cred);
4892 *ap->a_size = (size_t)va.va_size;
4893 } else if (ap->a_uio != NULL)
4894 error = VOP_READ(vp, ap->a_uio, IO_UNIT | IO_SYNC, ap->a_cred);
4897 vn_close(vp, flags, ap->a_cred, td);
4904 * Vnode operation to remove a named attribute.
4907 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
4910 IN struct vnode *a_vp;
4911 IN int a_attrnamespace;
4912 IN const char *a_name;
4913 IN struct ucred *a_cred;
4914 IN struct thread *a_td;
4918 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
4919 struct thread *td = ap->a_td;
4920 struct nameidata nd;
4923 vnode_t *xvp = NULL, *vp;
4926 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
4927 ap->a_cred, ap->a_td, VWRITE);
4931 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
4938 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
4945 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF | MPSAFE,
4946 UIO_SYSSPACE, attrname, xvp, td);
4949 NDFREE(&nd, NDF_ONLY_PNBUF);
4952 if (error == ENOENT)
4956 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
4959 if (vp == nd.ni_dvp)
4969 * Vnode operation to set a named attribute.
4972 zfs_setextattr(struct vop_setextattr_args *ap)
4975 IN struct vnode *a_vp;
4976 IN int a_attrnamespace;
4977 IN const char *a_name;
4978 INOUT struct uio *a_uio;
4979 IN struct ucred *a_cred;
4980 IN struct thread *a_td;
4984 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
4985 struct thread *td = ap->a_td;
4986 struct nameidata nd;
4989 vnode_t *xvp = NULL, *vp;
4992 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
4993 ap->a_cred, ap->a_td, VWRITE);
4997 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5004 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5005 LOOKUP_XATTR | CREATE_XATTR_DIR);
5011 flags = FFLAGS(O_WRONLY | O_CREAT);
5012 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
5014 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
5016 NDFREE(&nd, NDF_ONLY_PNBUF);
5024 error = VOP_SETATTR(vp, &va, ap->a_cred);
5026 VOP_WRITE(vp, ap->a_uio, IO_UNIT | IO_SYNC, ap->a_cred);
5029 vn_close(vp, flags, ap->a_cred, td);
5036 * Vnode operation to retrieve extended attributes on a vnode.
5039 zfs_listextattr(struct vop_listextattr_args *ap)
5042 IN struct vnode *a_vp;
5043 IN int a_attrnamespace;
5044 INOUT struct uio *a_uio;
5046 IN struct ucred *a_cred;
5047 IN struct thread *a_td;
5051 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5052 struct thread *td = ap->a_td;
5053 struct nameidata nd;
5054 char attrprefix[16];
5055 u_char dirbuf[sizeof(struct dirent)];
5058 struct uio auio, *uio = ap->a_uio;
5059 size_t *sizep = ap->a_size;
5061 vnode_t *xvp = NULL, *vp;
5062 int done, error, eof, pos;
5064 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5065 ap->a_cred, ap->a_td, VREAD);
5069 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5070 sizeof(attrprefix));
5073 plen = strlen(attrprefix);
5080 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5085 * ENOATTR means that the EA directory does not yet exist,
5086 * i.e. there are no extended attributes there.
5088 if (error == ENOATTR)
5093 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE,
5094 UIO_SYSSPACE, ".", xvp, td);
5097 NDFREE(&nd, NDF_ONLY_PNBUF);
5103 auio.uio_iov = &aiov;
5104 auio.uio_iovcnt = 1;
5105 auio.uio_segflg = UIO_SYSSPACE;
5107 auio.uio_rw = UIO_READ;
5108 auio.uio_offset = 0;
5113 aiov.iov_base = (void *)dirbuf;
5114 aiov.iov_len = sizeof(dirbuf);
5115 auio.uio_resid = sizeof(dirbuf);
5116 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5117 done = sizeof(dirbuf) - auio.uio_resid;
5120 for (pos = 0; pos < done;) {
5121 dp = (struct dirent *)(dirbuf + pos);
5122 pos += dp->d_reclen;
5124 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5125 * is what we get when attribute was created on Solaris.
5127 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5129 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
5131 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5133 nlen = dp->d_namlen - plen;
5136 else if (uio != NULL) {
5138 * Format of extattr name entry is one byte for
5139 * length and the rest for name.
5141 error = uiomove(&nlen, 1, uio->uio_rw, uio);
5143 error = uiomove(dp->d_name + plen, nlen,
5150 } while (!eof && error == 0);
5159 zfs_freebsd_getacl(ap)
5160 struct vop_getacl_args /* {
5169 vsecattr_t vsecattr;
5171 if (ap->a_type != ACL_TYPE_NFS4)
5174 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
5175 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
5178 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
5179 if (vsecattr.vsa_aclentp != NULL)
5180 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
5186 zfs_freebsd_setacl(ap)
5187 struct vop_setacl_args /* {
5196 vsecattr_t vsecattr;
5197 int aclbsize; /* size of acl list in bytes */
5200 if (ap->a_type != ACL_TYPE_NFS4)
5203 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
5207 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
5208 * splitting every entry into two and appending "canonical six"
5209 * entries at the end. Don't allow for setting an ACL that would
5210 * cause chmod(2) to run out of ACL entries.
5212 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
5215 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
5219 vsecattr.vsa_mask = VSA_ACE;
5220 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
5221 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
5222 aaclp = vsecattr.vsa_aclentp;
5223 vsecattr.vsa_aclentsz = aclbsize;
5225 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
5226 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
5227 kmem_free(aaclp, aclbsize);
5233 zfs_freebsd_aclcheck(ap)
5234 struct vop_aclcheck_args /* {
5243 return (EOPNOTSUPP);
5246 struct vop_vector zfs_vnodeops;
5247 struct vop_vector zfs_fifoops;
5248 struct vop_vector zfs_shareops;
5250 struct vop_vector zfs_vnodeops = {
5251 .vop_default = &default_vnodeops,
5252 .vop_inactive = zfs_freebsd_inactive,
5253 .vop_reclaim = zfs_freebsd_reclaim,
5254 .vop_access = zfs_freebsd_access,
5255 #ifdef FREEBSD_NAMECACHE
5256 .vop_lookup = vfs_cache_lookup,
5257 .vop_cachedlookup = zfs_freebsd_lookup,
5259 .vop_lookup = zfs_freebsd_lookup,
5261 .vop_getattr = zfs_freebsd_getattr,
5262 .vop_setattr = zfs_freebsd_setattr,
5263 .vop_create = zfs_freebsd_create,
5264 .vop_mknod = zfs_freebsd_create,
5265 .vop_mkdir = zfs_freebsd_mkdir,
5266 .vop_readdir = zfs_freebsd_readdir,
5267 .vop_fsync = zfs_freebsd_fsync,
5268 .vop_open = zfs_freebsd_open,
5269 .vop_close = zfs_freebsd_close,
5270 .vop_rmdir = zfs_freebsd_rmdir,
5271 .vop_ioctl = zfs_freebsd_ioctl,
5272 .vop_link = zfs_freebsd_link,
5273 .vop_symlink = zfs_freebsd_symlink,
5274 .vop_readlink = zfs_freebsd_readlink,
5275 .vop_read = zfs_freebsd_read,
5276 .vop_write = zfs_freebsd_write,
5277 .vop_remove = zfs_freebsd_remove,
5278 .vop_rename = zfs_freebsd_rename,
5279 .vop_pathconf = zfs_freebsd_pathconf,
5280 .vop_bmap = VOP_EOPNOTSUPP,
5281 .vop_fid = zfs_freebsd_fid,
5282 .vop_getextattr = zfs_getextattr,
5283 .vop_deleteextattr = zfs_deleteextattr,
5284 .vop_setextattr = zfs_setextattr,
5285 .vop_listextattr = zfs_listextattr,
5286 .vop_getacl = zfs_freebsd_getacl,
5287 .vop_setacl = zfs_freebsd_setacl,
5288 .vop_aclcheck = zfs_freebsd_aclcheck,
5291 struct vop_vector zfs_fifoops = {
5292 .vop_default = &fifo_specops,
5293 .vop_fsync = zfs_freebsd_fsync,
5294 .vop_access = zfs_freebsd_access,
5295 .vop_getattr = zfs_freebsd_getattr,
5296 .vop_inactive = zfs_freebsd_inactive,
5297 .vop_read = VOP_PANIC,
5298 .vop_reclaim = zfs_freebsd_reclaim,
5299 .vop_setattr = zfs_freebsd_setattr,
5300 .vop_write = VOP_PANIC,
5301 .vop_pathconf = zfs_freebsd_fifo_pathconf,
5302 .vop_fid = zfs_freebsd_fid,
5303 .vop_getacl = zfs_freebsd_getacl,
5304 .vop_setacl = zfs_freebsd_setacl,
5305 .vop_aclcheck = zfs_freebsd_aclcheck,
5309 * special share hidden files vnode operations template
5311 struct vop_vector zfs_shareops = {
5312 .vop_default = &default_vnodeops,
5313 .vop_access = zfs_freebsd_access,
5314 .vop_inactive = zfs_freebsd_inactive,
5315 .vop_reclaim = zfs_freebsd_reclaim,
5316 .vop_fid = zfs_freebsd_fid,
5317 .vop_pathconf = zfs_freebsd_pathconf,