4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Portions Copyright 2007 Jeremy Teo */
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
35 #include <sys/vfs_opreg.h>
36 #include <sys/vnode.h>
40 #include <sys/taskq.h>
42 #include <sys/vmsystm.h>
43 #include <sys/atomic.h>
45 #include <vm/seg_vn.h>
49 #include <vm/seg_kpm.h>
51 #include <sys/pathname.h>
52 #include <sys/cmn_err.h>
53 #include <sys/errno.h>
54 #include <sys/unistd.h>
55 #include <sys/zfs_dir.h>
56 #include <sys/zfs_acl.h>
57 #include <sys/zfs_ioctl.h>
58 #include <sys/fs/zfs.h>
64 #include <sys/dirent.h>
65 #include <sys/policy.h>
66 #include <sys/sunddi.h>
67 #include <sys/filio.h>
69 #include "fs/fs_subr.h"
70 #include <sys/zfs_ctldir.h>
71 #include <sys/zfs_fuid.h>
73 #include <sys/zfs_rlock.h>
74 #include <sys/extdirent.h>
75 #include <sys/kidmap.h>
76 #include <sys/cred_impl.h>
82 * Each vnode op performs some logical unit of work. To do this, the ZPL must
83 * properly lock its in-core state, create a DMU transaction, do the work,
84 * record this work in the intent log (ZIL), commit the DMU transaction,
85 * and wait for the intent log to commit if it is a synchronous operation.
86 * Moreover, the vnode ops must work in both normal and log replay context.
87 * The ordering of events is important to avoid deadlocks and references
88 * to freed memory. The example below illustrates the following Big Rules:
90 * (1) A check must be made in each zfs thread for a mounted file system.
91 * This is done avoiding races using ZFS_ENTER(zfsvfs).
92 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
94 * can return EIO from the calling function.
96 * (2) VN_RELE() should always be the last thing except for zil_commit()
97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
98 * First, if it's the last reference, the vnode/znode
99 * can be freed, so the zp may point to freed memory. Second, the last
100 * reference will call zfs_zinactive(), which may induce a lot of work --
101 * pushing cached pages (which acquires range locks) and syncing out
102 * cached atime changes. Third, zfs_zinactive() may require a new tx,
103 * which could deadlock the system if you were already holding one.
105 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
106 * as they can span dmu_tx_assign() calls.
108 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
109 * This is critical because we don't want to block while holding locks.
110 * Note, in particular, that if a lock is sometimes acquired before
111 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
112 * use a non-blocking assign can deadlock the system. The scenario:
114 * Thread A has grabbed a lock before calling dmu_tx_assign().
115 * Thread B is in an already-assigned tx, and blocks for this lock.
116 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
117 * forever, because the previous txg can't quiesce until B's tx commits.
119 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
120 * then drop all locks, call dmu_tx_wait(), and try again.
122 * (5) If the operation succeeded, generate the intent log entry for it
123 * before dropping locks. This ensures that the ordering of events
124 * in the intent log matches the order in which they actually occurred.
125 * During ZIL replay the zfs_log_* functions will update the sequence
126 * number to indicate the zil transaction has replayed.
128 * (6) At the end of each vnode op, the DMU tx must always commit,
129 * regardless of whether there were any errors.
131 * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid)
132 * to ensure that synchronous semantics are provided when necessary.
134 * In general, this is how things should be ordered in each vnode op:
136 * ZFS_ENTER(zfsvfs); // exit if unmounted
138 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
139 * rw_enter(...); // grab any other locks you need
140 * tx = dmu_tx_create(...); // get DMU tx
141 * dmu_tx_hold_*(); // hold each object you might modify
142 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
144 * rw_exit(...); // drop locks
145 * zfs_dirent_unlock(dl); // unlock directory entry
146 * VN_RELE(...); // release held vnodes
147 * if (error == ERESTART) {
152 * dmu_tx_abort(tx); // abort DMU tx
153 * ZFS_EXIT(zfsvfs); // finished in zfs
154 * return (error); // really out of space
156 * error = do_real_work(); // do whatever this VOP does
158 * zfs_log_*(...); // on success, make ZIL entry
159 * dmu_tx_commit(tx); // commit DMU tx -- error or not
160 * rw_exit(...); // drop locks
161 * zfs_dirent_unlock(dl); // unlock directory entry
162 * VN_RELE(...); // release held vnodes
163 * zil_commit(zilog, seq, foid); // synchronous when necessary
164 * ZFS_EXIT(zfsvfs); // finished in zfs
165 * return (error); // done, report error
170 zfs_open(vnode_t
**vpp
, int flag
, cred_t
*cr
, caller_context_t
*ct
)
172 znode_t
*zp
= VTOZ(*vpp
);
173 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
178 if ((flag
& FWRITE
) && (zp
->z_phys
->zp_flags
& ZFS_APPENDONLY
) &&
179 ((flag
& FAPPEND
) == 0)) {
184 if (!zfs_has_ctldir(zp
) && zp
->z_zfsvfs
->z_vscan
&&
185 ZTOV(zp
)->v_type
== VREG
&&
186 !(zp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
) &&
187 zp
->z_phys
->zp_size
> 0) {
188 if (fs_vscan(*vpp
, cr
, 0) != 0) {
194 /* Keep a count of the synchronous opens in the znode */
195 if (flag
& (FSYNC
| FDSYNC
))
196 atomic_inc_32(&zp
->z_sync_cnt
);
204 zfs_close(vnode_t
*vp
, int flag
, int count
, offset_t offset
, cred_t
*cr
,
205 caller_context_t
*ct
)
207 znode_t
*zp
= VTOZ(vp
);
208 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
213 /* Decrement the synchronous opens in the znode */
214 if ((flag
& (FSYNC
| FDSYNC
)) && (count
== 1))
215 atomic_dec_32(&zp
->z_sync_cnt
);
218 * Clean up any locks held by this process on the vp.
220 cleanlocks(vp
, ddi_get_pid(), 0);
221 cleanshares(vp
, ddi_get_pid());
223 if (!zfs_has_ctldir(zp
) && zp
->z_zfsvfs
->z_vscan
&&
224 ZTOV(zp
)->v_type
== VREG
&&
225 !(zp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
) &&
226 zp
->z_phys
->zp_size
> 0)
227 VERIFY(fs_vscan(vp
, cr
, 1) == 0);
234 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
235 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
238 zfs_holey(vnode_t
*vp
, int cmd
, offset_t
*off
)
240 znode_t
*zp
= VTOZ(vp
);
241 uint64_t noff
= (uint64_t)*off
; /* new offset */
246 file_sz
= zp
->z_phys
->zp_size
;
247 if (noff
>= file_sz
) {
251 if (cmd
== _FIO_SEEK_HOLE
)
256 error
= dmu_offset_next(zp
->z_zfsvfs
->z_os
, zp
->z_id
, hole
, &noff
);
259 if ((error
== ESRCH
) || (noff
> file_sz
)) {
261 * Handle the virtual hole at the end of file.
278 zfs_ioctl(vnode_t
*vp
, int com
, intptr_t data
, int flag
, cred_t
*cred
,
279 int *rvalp
, caller_context_t
*ct
)
288 return (zfs_sync(vp
->v_vfsp
, 0, cred
));
291 * The following two ioctls are used by bfu. Faking out,
292 * necessary to avoid bfu errors.
300 if (ddi_copyin((void *)data
, &off
, sizeof (off
), flag
))
304 zfsvfs
= zp
->z_zfsvfs
;
308 /* offset parameter is in/out */
309 error
= zfs_holey(vp
, com
, &off
);
313 if (ddi_copyout(&off
, (void *)data
, sizeof (off
), flag
))
321 * Utility functions to map and unmap a single physical page. These
322 * are used to manage the mappable copies of ZFS file data, and therefore
323 * do not update ref/mod bits.
326 zfs_map_page(page_t
*pp
, enum seg_rw rw
)
329 return (hat_kpm_mapin(pp
, 0));
330 ASSERT(rw
== S_READ
|| rw
== S_WRITE
);
331 return (ppmapin(pp
, PROT_READ
| ((rw
== S_WRITE
) ? PROT_WRITE
: 0),
336 zfs_unmap_page(page_t
*pp
, caddr_t addr
)
339 hat_kpm_mapout(pp
, 0, addr
);
346 * When a file is memory mapped, we must keep the IO data synchronized
347 * between the DMU cache and the memory mapped pages. What this means:
349 * On Write: If we find a memory mapped page, we write to *both*
350 * the page and the dmu buffer.
353 update_pages(vnode_t
*vp
, int64_t start
, int len
, objset_t
*os
, uint64_t oid
)
357 off
= start
& PAGEOFFSET
;
358 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
360 uint64_t nbytes
= MIN(PAGESIZE
- off
, len
);
362 if (pp
= page_lookup(vp
, start
, SE_SHARED
)) {
365 va
= zfs_map_page(pp
, S_WRITE
);
366 (void) dmu_read(os
, oid
, start
+off
, nbytes
, va
+off
);
367 zfs_unmap_page(pp
, va
);
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 Read: We "read" preferentially from memory mapped pages,
380 * else we default from the dmu buffer.
382 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
383 * the file is memory mapped.
386 mappedread(vnode_t
*vp
, int nbytes
, uio_t
*uio
)
388 znode_t
*zp
= VTOZ(vp
);
389 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
394 start
= uio
->uio_loffset
;
395 off
= start
& PAGEOFFSET
;
396 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
398 uint64_t bytes
= MIN(PAGESIZE
- off
, len
);
400 if (pp
= page_lookup(vp
, start
, SE_SHARED
)) {
403 va
= zfs_map_page(pp
, S_READ
);
404 error
= uiomove(va
+ off
, bytes
, UIO_READ
, uio
);
405 zfs_unmap_page(pp
, va
);
408 error
= dmu_read_uio(os
, zp
->z_id
, uio
, bytes
);
418 offset_t zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
421 * Read bytes from specified file into supplied buffer.
423 * IN: vp - vnode of file to be read from.
424 * uio - structure supplying read location, range info,
426 * ioflag - SYNC flags; used to provide FRSYNC semantics.
427 * cr - credentials of caller.
428 * ct - caller context
430 * OUT: uio - updated offset and range, buffer filled.
432 * RETURN: 0 if success
433 * error code if failure
436 * vp - atime updated if byte count > 0
440 zfs_read(vnode_t
*vp
, uio_t
*uio
, int ioflag
, cred_t
*cr
, caller_context_t
*ct
)
442 znode_t
*zp
= VTOZ(vp
);
443 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
453 if (zp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
) {
459 * Validate file offset
461 if (uio
->uio_loffset
< (offset_t
)0) {
467 * Fasttrack empty reads
469 if (uio
->uio_resid
== 0) {
475 * Check for mandatory locks
477 if (MANDMODE((mode_t
)zp
->z_phys
->zp_mode
)) {
478 if (error
= chklock(vp
, FREAD
,
479 uio
->uio_loffset
, uio
->uio_resid
, uio
->uio_fmode
, ct
)) {
486 * If we're in FRSYNC mode, sync out this znode before reading it.
489 zil_commit(zfsvfs
->z_log
, zp
->z_last_itx
, zp
->z_id
);
492 * Lock the range against changes.
494 rl
= zfs_range_lock(zp
, uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
497 * If we are reading past end-of-file we can skip
498 * to the end; but we might still need to set atime.
500 if (uio
->uio_loffset
>= zp
->z_phys
->zp_size
) {
505 ASSERT(uio
->uio_loffset
< zp
->z_phys
->zp_size
);
506 n
= MIN(uio
->uio_resid
, zp
->z_phys
->zp_size
- uio
->uio_loffset
);
509 nbytes
= MIN(n
, zfs_read_chunk_size
-
510 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
512 if (vn_has_cached_data(vp
))
513 error
= mappedread(vp
, nbytes
, uio
);
515 error
= dmu_read_uio(os
, zp
->z_id
, uio
, nbytes
);
517 /* convert checksum errors into IO errors */
527 zfs_range_unlock(rl
);
529 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
535 * Write the bytes to a file.
537 * IN: vp - vnode of file to be written to.
538 * uio - structure supplying write location, range info,
540 * ioflag - FAPPEND flag set if in append mode.
541 * cr - credentials of caller.
542 * ct - caller context (NFS/CIFS fem monitor only)
544 * OUT: uio - updated offset and range.
546 * RETURN: 0 if success
547 * error code if failure
550 * vp - ctime|mtime updated if byte count > 0
554 zfs_write(vnode_t
*vp
, uio_t
*uio
, int ioflag
, cred_t
*cr
, caller_context_t
*ct
)
556 znode_t
*zp
= VTOZ(vp
);
557 rlim64_t limit
= uio
->uio_llimit
;
558 ssize_t start_resid
= uio
->uio_resid
;
562 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
567 int max_blksz
= zfsvfs
->z_max_blksz
;
572 * Fasttrack empty write
578 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
585 * If immutable or not appending then return EPERM
587 pflags
= zp
->z_phys
->zp_flags
;
588 if ((pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
589 ((pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
590 (uio
->uio_loffset
< zp
->z_phys
->zp_size
))) {
595 zilog
= zfsvfs
->z_log
;
598 * Pre-fault the pages to ensure slow (eg NFS) pages
601 uio_prefaultpages(n
, uio
);
604 * If in append mode, set the io offset pointer to eof.
606 if (ioflag
& FAPPEND
) {
608 * Range lock for a file append:
609 * The value for the start of range will be determined by
610 * zfs_range_lock() (to guarantee append semantics).
611 * If this write will cause the block size to increase,
612 * zfs_range_lock() will lock the entire file, so we must
613 * later reduce the range after we grow the block size.
615 rl
= zfs_range_lock(zp
, 0, n
, RL_APPEND
);
616 if (rl
->r_len
== UINT64_MAX
) {
617 /* overlocked, zp_size can't change */
618 woff
= uio
->uio_loffset
= zp
->z_phys
->zp_size
;
620 woff
= uio
->uio_loffset
= rl
->r_off
;
623 woff
= uio
->uio_loffset
;
625 * Validate file offset
633 * If we need to grow the block size then zfs_range_lock()
634 * will lock a wider range than we request here.
635 * Later after growing the block size we reduce the range.
637 rl
= zfs_range_lock(zp
, woff
, n
, RL_WRITER
);
641 zfs_range_unlock(rl
);
646 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
650 * Check for mandatory locks
652 if (MANDMODE((mode_t
)zp
->z_phys
->zp_mode
) &&
653 (error
= chklock(vp
, FWRITE
, woff
, n
, uio
->uio_fmode
, ct
)) != 0) {
654 zfs_range_unlock(rl
);
658 end_size
= MAX(zp
->z_phys
->zp_size
, woff
+ n
);
661 * Write the file in reasonable size chunks. Each chunk is written
662 * in a separate transaction; this keeps the intent log records small
663 * and allows us to do more fine-grained space accounting.
667 * Start a transaction.
669 woff
= uio
->uio_loffset
;
670 tx
= dmu_tx_create(zfsvfs
->z_os
);
671 dmu_tx_hold_bonus(tx
, zp
->z_id
);
672 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
673 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
675 if (error
== ERESTART
) {
685 * If zfs_range_lock() over-locked we grow the blocksize
686 * and then reduce the lock range. This will only happen
687 * on the first iteration since zfs_range_reduce() will
688 * shrink down r_len to the appropriate size.
690 if (rl
->r_len
== UINT64_MAX
) {
693 if (zp
->z_blksz
> max_blksz
) {
694 ASSERT(!ISP2(zp
->z_blksz
));
695 new_blksz
= MIN(end_size
, SPA_MAXBLOCKSIZE
);
697 new_blksz
= MIN(end_size
, max_blksz
);
699 zfs_grow_blocksize(zp
, new_blksz
, tx
);
700 zfs_range_reduce(rl
, woff
, n
);
704 * XXX - should we really limit each write to z_max_blksz?
705 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
707 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
709 tx_bytes
= uio
->uio_resid
;
710 error
= dmu_write_uio(zfsvfs
->z_os
, zp
->z_id
, uio
, nbytes
, tx
);
711 tx_bytes
-= uio
->uio_resid
;
712 if (tx_bytes
&& vn_has_cached_data(vp
))
713 update_pages(vp
, woff
,
714 tx_bytes
, zfsvfs
->z_os
, zp
->z_id
);
717 * If we made no progress, we're done. If we made even
718 * partial progress, update the znode and ZIL accordingly.
727 * Clear Set-UID/Set-GID bits on successful write if not
728 * privileged and at least one of the excute bits is set.
730 * It would be nice to to this after all writes have
731 * been done, but that would still expose the ISUID/ISGID
732 * to another app after the partial write is committed.
734 * Note: we don't call zfs_fuid_map_id() here because
735 * user 0 is not an ephemeral uid.
737 mutex_enter(&zp
->z_acl_lock
);
738 if ((zp
->z_phys
->zp_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
739 (S_IXUSR
>> 6))) != 0 &&
740 (zp
->z_phys
->zp_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
741 secpolicy_vnode_setid_retain(cr
,
742 (zp
->z_phys
->zp_mode
& S_ISUID
) != 0 &&
743 zp
->z_phys
->zp_uid
== 0) != 0) {
744 zp
->z_phys
->zp_mode
&= ~(S_ISUID
| S_ISGID
);
746 mutex_exit(&zp
->z_acl_lock
);
749 * Update time stamp. NOTE: This marks the bonus buffer as
750 * dirty, so we don't have to do it again for zp_size.
752 zfs_time_stamper(zp
, CONTENT_MODIFIED
, tx
);
755 * Update the file size (zp_size) if it has changed;
756 * account for possible concurrent updates.
758 while ((end_size
= zp
->z_phys
->zp_size
) < uio
->uio_loffset
)
759 (void) atomic_cas_64(&zp
->z_phys
->zp_size
, end_size
,
761 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
);
766 ASSERT(tx_bytes
== nbytes
);
770 zfs_range_unlock(rl
);
773 * If we're in replay mode, or we made no progress, return error.
774 * Otherwise, it's at least a partial write, so it's successful.
776 if (zfsvfs
->z_replay
|| uio
->uio_resid
== start_resid
) {
781 if (ioflag
& (FSYNC
| FDSYNC
))
782 zil_commit(zilog
, zp
->z_last_itx
, zp
->z_id
);
789 zfs_get_done(dmu_buf_t
*db
, void *vzgd
)
791 zgd_t
*zgd
= (zgd_t
*)vzgd
;
792 rl_t
*rl
= zgd
->zgd_rl
;
793 vnode_t
*vp
= ZTOV(rl
->r_zp
);
795 dmu_buf_rele(db
, vzgd
);
796 zfs_range_unlock(rl
);
798 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
799 kmem_free(zgd
, sizeof (zgd_t
));
803 * Get data to generate a TX_WRITE intent log record.
806 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
808 zfsvfs_t
*zfsvfs
= arg
;
809 objset_t
*os
= zfsvfs
->z_os
;
811 uint64_t off
= lr
->lr_offset
;
815 int dlen
= lr
->lr_length
; /* length of user data */
822 * Nothing to do if the file has been removed
824 if (zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
) != 0)
826 if (zp
->z_unlinked
) {
832 * Write records come in two flavors: immediate and indirect.
833 * For small writes it's cheaper to store the data with the
834 * log record (immediate); for large writes it's cheaper to
835 * sync the data and get a pointer to it (indirect) so that
836 * we don't have to write the data twice.
838 if (buf
!= NULL
) { /* immediate write */
839 rl
= zfs_range_lock(zp
, off
, dlen
, RL_READER
);
840 /* test for truncation needs to be done while range locked */
841 if (off
>= zp
->z_phys
->zp_size
) {
845 VERIFY(0 == dmu_read(os
, lr
->lr_foid
, off
, dlen
, buf
));
846 } else { /* indirect write */
847 uint64_t boff
; /* block starting offset */
850 * Have to lock the whole block to ensure when it's
851 * written out and it's checksum is being calculated
852 * that no one can change the data. We need to re-check
853 * blocksize after we get the lock in case it's changed!
856 if (ISP2(zp
->z_blksz
)) {
857 boff
= P2ALIGN_TYPED(off
, zp
->z_blksz
,
863 rl
= zfs_range_lock(zp
, boff
, dlen
, RL_READER
);
864 if (zp
->z_blksz
== dlen
)
866 zfs_range_unlock(rl
);
868 /* test for truncation needs to be done while range locked */
869 if (off
>= zp
->z_phys
->zp_size
) {
873 zgd
= (zgd_t
*)kmem_alloc(sizeof (zgd_t
), KM_SLEEP
);
875 zgd
->zgd_zilog
= zfsvfs
->z_log
;
876 zgd
->zgd_bp
= &lr
->lr_blkptr
;
877 VERIFY(0 == dmu_buf_hold(os
, lr
->lr_foid
, boff
, zgd
, &db
));
878 ASSERT(boff
== db
->db_offset
);
879 lr
->lr_blkoff
= off
- boff
;
880 error
= dmu_sync(zio
, db
, &lr
->lr_blkptr
,
881 lr
->lr_common
.lrc_txg
, zfs_get_done
, zgd
);
882 ASSERT((error
&& error
!= EINPROGRESS
) ||
883 lr
->lr_length
<= zp
->z_blksz
);
885 zil_add_block(zfsvfs
->z_log
, &lr
->lr_blkptr
);
887 * If we get EINPROGRESS, then we need to wait for a
888 * write IO initiated by dmu_sync() to complete before
889 * we can release this dbuf. We will finish everything
890 * up in the zfs_get_done() callback.
892 if (error
== EINPROGRESS
)
894 dmu_buf_rele(db
, zgd
);
895 kmem_free(zgd
, sizeof (zgd_t
));
898 zfs_range_unlock(rl
);
905 zfs_access(vnode_t
*vp
, int mode
, int flag
, cred_t
*cr
,
906 caller_context_t
*ct
)
908 znode_t
*zp
= VTOZ(vp
);
909 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
915 if (flag
& V_ACE_MASK
)
916 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
918 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
925 * Lookup an entry in a directory, or an extended attribute directory.
926 * If it exists, return a held vnode reference for it.
928 * IN: dvp - vnode of directory to search.
929 * nm - name of entry to lookup.
930 * pnp - full pathname to lookup [UNUSED].
931 * flags - LOOKUP_XATTR set if looking for an attribute.
932 * rdir - root directory vnode [UNUSED].
933 * cr - credentials of caller.
934 * ct - caller context
935 * direntflags - directory lookup flags
936 * realpnp - returned pathname.
938 * OUT: vpp - vnode of located entry, NULL if not found.
940 * RETURN: 0 if success
941 * error code if failure
948 zfs_lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, struct pathname
*pnp
,
949 int flags
, vnode_t
*rdir
, cred_t
*cr
, caller_context_t
*ct
,
950 int *direntflags
, pathname_t
*realpnp
)
952 znode_t
*zdp
= VTOZ(dvp
);
953 zfsvfs_t
*zfsvfs
= zdp
->z_zfsvfs
;
961 if (flags
& LOOKUP_XATTR
) {
963 * If the xattr property is off, refuse the lookup request.
965 if (!(zfsvfs
->z_vfs
->vfs_flag
& VFS_XATTR
)) {
971 * We don't allow recursive attributes..
972 * Maybe someday we will.
974 if (zdp
->z_phys
->zp_flags
& ZFS_XATTR
) {
979 if (error
= zfs_get_xattrdir(VTOZ(dvp
), vpp
, cr
, flags
)) {
985 * Do we have permission to get into attribute directory?
988 if (error
= zfs_zaccess(VTOZ(*vpp
), ACE_EXECUTE
, 0,
998 if (dvp
->v_type
!= VDIR
) {
1004 * Check accessibility of directory.
1007 if (error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
)) {
1012 if (zfsvfs
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1013 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1018 error
= zfs_dirlook(zdp
, nm
, vpp
, flags
, direntflags
, realpnp
);
1021 * Convert device special files
1023 if (IS_DEVVP(*vpp
)) {
1026 svp
= specvp(*vpp
, (*vpp
)->v_rdev
, (*vpp
)->v_type
, cr
);
1040 * Attempt to create a new entry in a directory. If the entry
1041 * already exists, truncate the file if permissible, else return
1042 * an error. Return the vp of the created or trunc'd file.
1044 * IN: dvp - vnode of directory to put new file entry in.
1045 * name - name of new file entry.
1046 * vap - attributes of new file.
1047 * excl - flag indicating exclusive or non-exclusive mode.
1048 * mode - mode to open file with.
1049 * cr - credentials of caller.
1050 * flag - large file flag [UNUSED].
1051 * ct - caller context
1052 * vsecp - ACL to be set
1054 * OUT: vpp - vnode of created or trunc'd entry.
1056 * RETURN: 0 if success
1057 * error code if failure
1060 * dvp - ctime|mtime updated if new entry created
1061 * vp - ctime|mtime always, atime if new
1066 zfs_create(vnode_t
*dvp
, char *name
, vattr_t
*vap
, vcexcl_t excl
,
1067 int mode
, vnode_t
**vpp
, cred_t
*cr
, int flag
, caller_context_t
*ct
,
1070 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1071 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1077 zfs_acl_t
*aclp
= NULL
;
1078 zfs_fuid_info_t
*fuidp
= NULL
;
1081 gid_t gid
= crgetgid(cr
);
1084 * If we have an ephemeral id, ACL, or XVATTR then
1085 * make sure file system is at proper version
1088 ksid
= crgetsid(cr
, KSID_OWNER
);
1090 uid
= ksid_getid(ksid
);
1094 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1095 (vsecp
|| (vap
->va_mask
& AT_XVATTR
) ||
1096 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1102 zilog
= zfsvfs
->z_log
;
1104 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
1105 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1110 if (vap
->va_mask
& AT_XVATTR
) {
1111 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1112 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
1120 if ((vap
->va_mode
& VSVTX
) && secpolicy_vnode_stky_modify(cr
))
1121 vap
->va_mode
&= ~VSVTX
;
1123 if (*name
== '\0') {
1125 * Null component name refers to the directory itself.
1132 /* possible VN_HOLD(zp) */
1135 if (flag
& FIGNORECASE
)
1138 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1141 if (strcmp(name
, "..") == 0)
1149 if (vsecp
&& aclp
== NULL
) {
1150 error
= zfs_vsec_2_aclp(zfsvfs
, vap
->va_type
, vsecp
, &aclp
);
1154 zfs_dirent_unlock(dl
);
1163 * Create a new file object and update the directory
1166 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
1171 * We only support the creation of regular files in
1172 * extended attribute directories.
1174 if ((dzp
->z_phys
->zp_flags
& ZFS_XATTR
) &&
1175 (vap
->va_type
!= VREG
)) {
1180 tx
= dmu_tx_create(os
);
1181 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1182 if ((aclp
&& aclp
->z_has_fuids
) || IS_EPHEMERAL(uid
) ||
1183 IS_EPHEMERAL(gid
)) {
1184 if (zfsvfs
->z_fuid_obj
== 0) {
1185 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1186 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1187 FUID_SIZE_ESTIMATE(zfsvfs
));
1188 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
,
1191 dmu_tx_hold_bonus(tx
, zfsvfs
->z_fuid_obj
);
1192 dmu_tx_hold_write(tx
, zfsvfs
->z_fuid_obj
, 0,
1193 FUID_SIZE_ESTIMATE(zfsvfs
));
1196 dmu_tx_hold_bonus(tx
, dzp
->z_id
);
1197 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1198 if ((dzp
->z_phys
->zp_flags
& ZFS_INHERIT_ACE
) || aclp
) {
1199 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1200 0, SPA_MAXBLOCKSIZE
);
1202 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1204 zfs_dirent_unlock(dl
);
1205 if (error
== ERESTART
) {
1216 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, 0, aclp
, &fuidp
);
1217 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1218 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1219 if (flag
& FIGNORECASE
)
1221 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1224 zfs_fuid_info_free(fuidp
);
1227 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1230 * A directory entry already exists for this name.
1233 * Can't truncate an existing file if in exclusive mode.
1240 * Can't open a directory for writing.
1242 if ((ZTOV(zp
)->v_type
== VDIR
) && (mode
& S_IWRITE
)) {
1247 * Verify requested access to file.
1249 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1253 mutex_enter(&dzp
->z_lock
);
1255 mutex_exit(&dzp
->z_lock
);
1258 * Truncate regular files if requested.
1260 if ((ZTOV(zp
)->v_type
== VREG
) &&
1261 (vap
->va_mask
& AT_SIZE
) && (vap
->va_size
== 0)) {
1262 /* we can't hold any locks when calling zfs_freesp() */
1263 zfs_dirent_unlock(dl
);
1265 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1267 vnevent_create(ZTOV(zp
), ct
);
1274 zfs_dirent_unlock(dl
);
1282 * If vnode is for a device return a specfs vnode instead.
1284 if (IS_DEVVP(*vpp
)) {
1287 svp
= specvp(*vpp
, (*vpp
)->v_rdev
, (*vpp
)->v_type
, cr
);
1303 * Remove an entry from a directory.
1305 * IN: dvp - vnode of directory to remove entry from.
1306 * name - name of entry to remove.
1307 * cr - credentials of caller.
1308 * ct - caller context
1309 * flags - case flags
1311 * RETURN: 0 if success
1312 * error code if failure
1316 * vp - ctime (if nlink > 0)
1320 zfs_remove(vnode_t
*dvp
, char *name
, cred_t
*cr
, caller_context_t
*ct
,
1323 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1324 znode_t
*xzp
= NULL
;
1326 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1328 uint64_t acl_obj
, xattr_obj
;
1331 boolean_t may_delete_now
, delete_now
= FALSE
;
1332 boolean_t unlinked
, toobig
= FALSE
;
1334 pathname_t
*realnmp
= NULL
;
1341 zilog
= zfsvfs
->z_log
;
1343 if (flags
& FIGNORECASE
) {
1351 * Attempt to lock directory; fail if entry doesn't exist.
1353 if (error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1363 if (error
= zfs_zaccess_delete(dzp
, zp
, cr
)) {
1368 * Need to use rmdir for removing directories.
1370 if (vp
->v_type
== VDIR
) {
1375 vnevent_remove(vp
, dvp
, name
, ct
);
1378 dnlc_remove(dvp
, realnmp
->pn_buf
);
1380 dnlc_remove(dvp
, name
);
1382 mutex_enter(&vp
->v_lock
);
1383 may_delete_now
= vp
->v_count
== 1 && !vn_has_cached_data(vp
);
1384 mutex_exit(&vp
->v_lock
);
1387 * We may delete the znode now, or we may put it in the unlinked set;
1388 * it depends on whether we're the last link, and on whether there are
1389 * other holds on the vnode. So we dmu_tx_hold() the right things to
1390 * allow for either case.
1392 tx
= dmu_tx_create(zfsvfs
->z_os
);
1393 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1394 dmu_tx_hold_bonus(tx
, zp
->z_id
);
1395 if (may_delete_now
) {
1397 zp
->z_phys
->zp_size
> zp
->z_blksz
* DMU_MAX_DELETEBLKCNT
;
1398 /* if the file is too big, only hold_free a token amount */
1399 dmu_tx_hold_free(tx
, zp
->z_id
, 0,
1400 (toobig
? DMU_MAX_ACCESS
: DMU_OBJECT_END
));
1403 /* are there any extended attributes? */
1404 if ((xattr_obj
= zp
->z_phys
->zp_xattr
) != 0) {
1405 /* XXX - do we need this if we are deleting? */
1406 dmu_tx_hold_bonus(tx
, xattr_obj
);
1409 /* are there any additional acls */
1410 if ((acl_obj
= zp
->z_phys
->zp_acl
.z_acl_extern_obj
) != 0 &&
1412 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
1414 /* charge as an update -- would be nice not to charge at all */
1415 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1417 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1419 zfs_dirent_unlock(dl
);
1421 if (error
== ERESTART
) {
1434 * Remove the directory entry.
1436 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1444 mutex_enter(&vp
->v_lock
);
1445 delete_now
= may_delete_now
&& !toobig
&&
1446 vp
->v_count
== 1 && !vn_has_cached_data(vp
) &&
1447 zp
->z_phys
->zp_xattr
== xattr_obj
&&
1448 zp
->z_phys
->zp_acl
.z_acl_extern_obj
== acl_obj
;
1449 mutex_exit(&vp
->v_lock
);
1453 if (zp
->z_phys
->zp_xattr
) {
1454 error
= zfs_zget(zfsvfs
, zp
->z_phys
->zp_xattr
, &xzp
);
1455 ASSERT3U(error
, ==, 0);
1456 ASSERT3U(xzp
->z_phys
->zp_links
, ==, 2);
1457 dmu_buf_will_dirty(xzp
->z_dbuf
, tx
);
1458 mutex_enter(&xzp
->z_lock
);
1459 xzp
->z_unlinked
= 1;
1460 xzp
->z_phys
->zp_links
= 0;
1461 mutex_exit(&xzp
->z_lock
);
1462 zfs_unlinked_add(xzp
, tx
);
1463 zp
->z_phys
->zp_xattr
= 0; /* probably unnecessary */
1465 mutex_enter(&zp
->z_lock
);
1466 mutex_enter(&vp
->v_lock
);
1468 ASSERT3U(vp
->v_count
, ==, 0);
1469 mutex_exit(&vp
->v_lock
);
1470 mutex_exit(&zp
->z_lock
);
1471 zfs_znode_delete(zp
, tx
);
1472 } else if (unlinked
) {
1473 zfs_unlinked_add(zp
, tx
);
1477 if (flags
& FIGNORECASE
)
1479 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
);
1486 zfs_dirent_unlock(dl
);
1491 /* this rele is delayed to prevent nesting transactions */
1500 * Create a new directory and insert it into dvp using the name
1501 * provided. Return a pointer to the inserted directory.
1503 * IN: dvp - vnode of directory to add subdir to.
1504 * dirname - name of new directory.
1505 * vap - attributes of new directory.
1506 * cr - credentials of caller.
1507 * ct - caller context
1508 * vsecp - ACL to be set
1510 * OUT: vpp - vnode of created directory.
1512 * RETURN: 0 if success
1513 * error code if failure
1516 * dvp - ctime|mtime updated
1517 * vp - ctime|mtime|atime updated
1521 zfs_mkdir(vnode_t
*dvp
, char *dirname
, vattr_t
*vap
, vnode_t
**vpp
, cred_t
*cr
,
1522 caller_context_t
*ct
, int flags
, vsecattr_t
*vsecp
)
1524 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1525 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1531 zfs_acl_t
*aclp
= NULL
;
1532 zfs_fuid_info_t
*fuidp
= NULL
;
1536 gid_t gid
= crgetgid(cr
);
1538 ASSERT(vap
->va_type
== VDIR
);
1541 * If we have an ephemeral id, ACL, or XVATTR then
1542 * make sure file system is at proper version
1545 ksid
= crgetsid(cr
, KSID_OWNER
);
1547 uid
= ksid_getid(ksid
);
1550 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1551 (vsecp
|| (vap
->va_mask
& AT_XVATTR
) ||
1552 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1557 zilog
= zfsvfs
->z_log
;
1559 if (dzp
->z_phys
->zp_flags
& ZFS_XATTR
) {
1564 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1565 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1569 if (flags
& FIGNORECASE
)
1572 if (vap
->va_mask
& AT_XVATTR
)
1573 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1574 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
1580 * First make sure the new directory doesn't exist.
1585 if (error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1591 if (error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
)) {
1592 zfs_dirent_unlock(dl
);
1597 if (vsecp
&& aclp
== NULL
) {
1598 error
= zfs_vsec_2_aclp(zfsvfs
, vap
->va_type
, vsecp
, &aclp
);
1600 zfs_dirent_unlock(dl
);
1606 * Add a new entry to the directory.
1608 tx
= dmu_tx_create(zfsvfs
->z_os
);
1609 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1610 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1611 if ((aclp
&& aclp
->z_has_fuids
) || IS_EPHEMERAL(uid
) ||
1612 IS_EPHEMERAL(gid
)) {
1613 if (zfsvfs
->z_fuid_obj
== 0) {
1614 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1615 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1616 FUID_SIZE_ESTIMATE(zfsvfs
));
1617 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, FALSE
, NULL
);
1619 dmu_tx_hold_bonus(tx
, zfsvfs
->z_fuid_obj
);
1620 dmu_tx_hold_write(tx
, zfsvfs
->z_fuid_obj
, 0,
1621 FUID_SIZE_ESTIMATE(zfsvfs
));
1624 if ((dzp
->z_phys
->zp_flags
& ZFS_INHERIT_ACE
) || aclp
)
1625 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1626 0, SPA_MAXBLOCKSIZE
);
1627 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1629 zfs_dirent_unlock(dl
);
1630 if (error
== ERESTART
) {
1645 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, 0, aclp
, &fuidp
);
1651 * Now put new name in parent dir.
1653 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1657 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1658 if (flags
& FIGNORECASE
)
1660 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
, fuidp
, vap
);
1663 zfs_fuid_info_free(fuidp
);
1666 zfs_dirent_unlock(dl
);
1673 * Remove a directory subdir entry. If the current working
1674 * directory is the same as the subdir to be removed, the
1677 * IN: dvp - vnode of directory to remove from.
1678 * name - name of directory to be removed.
1679 * cwd - vnode of current working directory.
1680 * cr - credentials of caller.
1681 * ct - caller context
1682 * flags - case flags
1684 * RETURN: 0 if success
1685 * error code if failure
1688 * dvp - ctime|mtime updated
1692 zfs_rmdir(vnode_t
*dvp
, char *name
, vnode_t
*cwd
, cred_t
*cr
,
1693 caller_context_t
*ct
, int flags
)
1695 znode_t
*dzp
= VTOZ(dvp
);
1698 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1707 zilog
= zfsvfs
->z_log
;
1709 if (flags
& FIGNORECASE
)
1715 * Attempt to lock directory; fail if entry doesn't exist.
1717 if (error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1725 if (error
= zfs_zaccess_delete(dzp
, zp
, cr
)) {
1729 if (vp
->v_type
!= VDIR
) {
1739 vnevent_rmdir(vp
, dvp
, name
, ct
);
1742 * Grab a lock on the directory to make sure that noone is
1743 * trying to add (or lookup) entries while we are removing it.
1745 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1748 * Grab a lock on the parent pointer to make sure we play well
1749 * with the treewalk and directory rename code.
1751 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1753 tx
= dmu_tx_create(zfsvfs
->z_os
);
1754 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1755 dmu_tx_hold_bonus(tx
, zp
->z_id
);
1756 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1757 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1759 rw_exit(&zp
->z_parent_lock
);
1760 rw_exit(&zp
->z_name_lock
);
1761 zfs_dirent_unlock(dl
);
1763 if (error
== ERESTART
) {
1773 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
1776 uint64_t txtype
= TX_RMDIR
;
1777 if (flags
& FIGNORECASE
)
1779 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
);
1784 rw_exit(&zp
->z_parent_lock
);
1785 rw_exit(&zp
->z_name_lock
);
1787 zfs_dirent_unlock(dl
);
1796 * Read as many directory entries as will fit into the provided
1797 * buffer from the given directory cursor position (specified in
1798 * the uio structure.
1800 * IN: vp - vnode of directory to read.
1801 * uio - structure supplying read location, range info,
1802 * and return buffer.
1803 * cr - credentials of caller.
1804 * ct - caller context
1805 * flags - case flags
1807 * OUT: uio - updated offset and range, buffer filled.
1808 * eofp - set to true if end-of-file detected.
1810 * RETURN: 0 if success
1811 * error code if failure
1814 * vp - atime updated
1816 * Note that the low 4 bits of the cookie returned by zap is always zero.
1817 * This allows us to use the low range for "special" directory entries:
1818 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1819 * we use the offset 2 for the '.zfs' directory.
1823 zfs_readdir(vnode_t
*vp
, uio_t
*uio
, cred_t
*cr
, int *eofp
,
1824 caller_context_t
*ct
, int flags
)
1826 znode_t
*zp
= VTOZ(vp
);
1830 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1835 zap_attribute_t zap
;
1836 uint_t bytes_wanted
;
1837 uint64_t offset
; /* must be unsigned; checks for < 1 */
1842 boolean_t check_sysattrs
;
1848 * If we are not given an eof variable,
1855 * Check for valid iov_len.
1857 if (uio
->uio_iov
->iov_len
<= 0) {
1863 * Quit if directory has been removed (posix)
1865 if ((*eofp
= zp
->z_unlinked
) != 0) {
1872 offset
= uio
->uio_loffset
;
1873 prefetch
= zp
->z_zn_prefetch
;
1876 * Initialize the iterator cursor.
1880 * Start iteration from the beginning of the directory.
1882 zap_cursor_init(&zc
, os
, zp
->z_id
);
1885 * The offset is a serialized cursor.
1887 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
1891 * Get space to change directory entries into fs independent format.
1893 iovp
= uio
->uio_iov
;
1894 bytes_wanted
= iovp
->iov_len
;
1895 if (uio
->uio_segflg
!= UIO_SYSSPACE
|| uio
->uio_iovcnt
!= 1) {
1896 bufsize
= bytes_wanted
;
1897 outbuf
= kmem_alloc(bufsize
, KM_SLEEP
);
1898 odp
= (struct dirent64
*)outbuf
;
1900 bufsize
= bytes_wanted
;
1901 odp
= (struct dirent64
*)iovp
->iov_base
;
1903 eodp
= (struct edirent
*)odp
;
1906 * If this VFS supports the system attribute view interface; and
1907 * we're looking at an extended attribute directory; and we care
1908 * about normalization conflicts on this vfs; then we must check
1909 * for normalization conflicts with the sysattr name space.
1911 check_sysattrs
= vfs_has_feature(vp
->v_vfsp
, VFSFT_SYSATTR_VIEWS
) &&
1912 (vp
->v_flag
& V_XATTRDIR
) && zfsvfs
->z_norm
&&
1913 (flags
& V_RDDIR_ENTFLAGS
);
1916 * Transform to file-system independent format
1919 while (outcount
< bytes_wanted
) {
1925 * Special case `.', `..', and `.zfs'.
1928 (void) strcpy(zap
.za_name
, ".");
1929 zap
.za_normalization_conflict
= 0;
1931 } else if (offset
== 1) {
1932 (void) strcpy(zap
.za_name
, "..");
1933 zap
.za_normalization_conflict
= 0;
1934 objnum
= zp
->z_phys
->zp_parent
;
1935 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
1936 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
1937 zap
.za_normalization_conflict
= 0;
1938 objnum
= ZFSCTL_INO_ROOT
;
1943 if (error
= zap_cursor_retrieve(&zc
, &zap
)) {
1944 if ((*eofp
= (error
== ENOENT
)) != 0)
1950 if (zap
.za_integer_length
!= 8 ||
1951 zap
.za_num_integers
!= 1) {
1952 cmn_err(CE_WARN
, "zap_readdir: bad directory "
1953 "entry, obj = %lld, offset = %lld\n",
1954 (u_longlong_t
)zp
->z_id
,
1955 (u_longlong_t
)offset
);
1960 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
1962 * MacOS X can extract the object type here such as:
1963 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1966 if (check_sysattrs
&& !zap
.za_normalization_conflict
) {
1967 zap
.za_normalization_conflict
=
1968 xattr_sysattr_casechk(zap
.za_name
);
1972 if (flags
& V_RDDIR_ENTFLAGS
)
1973 reclen
= EDIRENT_RECLEN(strlen(zap
.za_name
));
1975 reclen
= DIRENT64_RECLEN(strlen(zap
.za_name
));
1978 * Will this entry fit in the buffer?
1980 if (outcount
+ reclen
> bufsize
) {
1982 * Did we manage to fit anything in the buffer?
1990 if (flags
& V_RDDIR_ENTFLAGS
) {
1992 * Add extended flag entry:
1994 eodp
->ed_ino
= objnum
;
1995 eodp
->ed_reclen
= reclen
;
1996 /* NOTE: ed_off is the offset for the *next* entry */
1997 next
= &(eodp
->ed_off
);
1998 eodp
->ed_eflags
= zap
.za_normalization_conflict
?
1999 ED_CASE_CONFLICT
: 0;
2000 (void) strncpy(eodp
->ed_name
, zap
.za_name
,
2001 EDIRENT_NAMELEN(reclen
));
2002 eodp
= (edirent_t
*)((intptr_t)eodp
+ reclen
);
2007 odp
->d_ino
= objnum
;
2008 odp
->d_reclen
= reclen
;
2009 /* NOTE: d_off is the offset for the *next* entry */
2010 next
= &(odp
->d_off
);
2011 (void) strncpy(odp
->d_name
, zap
.za_name
,
2012 DIRENT64_NAMELEN(reclen
));
2013 odp
= (dirent64_t
*)((intptr_t)odp
+ reclen
);
2017 ASSERT(outcount
<= bufsize
);
2019 /* Prefetch znode */
2021 dmu_prefetch(os
, objnum
, 0, 0);
2024 * Move to the next entry, fill in the previous offset.
2026 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2027 zap_cursor_advance(&zc
);
2028 offset
= zap_cursor_serialize(&zc
);
2034 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2036 if (uio
->uio_segflg
== UIO_SYSSPACE
&& uio
->uio_iovcnt
== 1) {
2037 iovp
->iov_base
+= outcount
;
2038 iovp
->iov_len
-= outcount
;
2039 uio
->uio_resid
-= outcount
;
2040 } else if (error
= uiomove(outbuf
, (long)outcount
, UIO_READ
, uio
)) {
2042 * Reset the pointer.
2044 offset
= uio
->uio_loffset
;
2048 zap_cursor_fini(&zc
);
2049 if (uio
->uio_segflg
!= UIO_SYSSPACE
|| uio
->uio_iovcnt
!= 1)
2050 kmem_free(outbuf
, bufsize
);
2052 if (error
== ENOENT
)
2055 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
2057 uio
->uio_loffset
= offset
;
2062 ulong_t zfs_fsync_sync_cnt
= 4;
2065 zfs_fsync(vnode_t
*vp
, int syncflag
, cred_t
*cr
, caller_context_t
*ct
)
2067 znode_t
*zp
= VTOZ(vp
);
2068 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2071 * Regardless of whether this is required for standards conformance,
2072 * this is the logical behavior when fsync() is called on a file with
2073 * dirty pages. We use B_ASYNC since the ZIL transactions are already
2074 * going to be pushed out as part of the zil_commit().
2076 if (vn_has_cached_data(vp
) && !(syncflag
& FNODSYNC
) &&
2077 (vp
->v_type
== VREG
) && !(IS_SWAPVP(vp
)))
2078 (void) VOP_PUTPAGE(vp
, (offset_t
)0, (size_t)0, B_ASYNC
, cr
, ct
);
2080 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2084 zil_commit(zfsvfs
->z_log
, zp
->z_last_itx
, zp
->z_id
);
2091 * Get the requested file attributes and place them in the provided
2094 * IN: vp - vnode of file.
2095 * vap - va_mask identifies requested attributes.
2096 * If AT_XVATTR set, then optional attrs are requested
2097 * flags - ATTR_NOACLCHECK (CIFS server context)
2098 * cr - credentials of caller.
2099 * ct - caller context
2101 * OUT: vap - attribute values.
2103 * RETURN: 0 (always succeeds)
2107 zfs_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
2108 caller_context_t
*ct
)
2110 znode_t
*zp
= VTOZ(vp
);
2111 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2115 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2116 xoptattr_t
*xoap
= NULL
;
2117 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2123 mutex_enter(&zp
->z_lock
);
2126 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2127 * Also, if we are the owner don't bother, since owner should
2128 * always be allowed to read basic attributes of file.
2130 if (!(pzp
->zp_flags
& ZFS_ACL_TRIVIAL
) &&
2131 (pzp
->zp_uid
!= crgetuid(cr
))) {
2132 if (error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2134 mutex_exit(&zp
->z_lock
);
2141 * Return all attributes. It's cheaper to provide the answer
2142 * than to determine whether we were asked the question.
2145 vap
->va_type
= vp
->v_type
;
2146 vap
->va_mode
= pzp
->zp_mode
& MODEMASK
;
2147 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2148 vap
->va_fsid
= zp
->z_zfsvfs
->z_vfs
->vfs_dev
;
2149 vap
->va_nodeid
= zp
->z_id
;
2150 if ((vp
->v_flag
& VROOT
) && zfs_show_ctldir(zp
))
2151 links
= pzp
->zp_links
+ 1;
2153 links
= pzp
->zp_links
;
2154 vap
->va_nlink
= MIN(links
, UINT32_MAX
); /* nlink_t limit! */
2155 vap
->va_size
= pzp
->zp_size
;
2156 vap
->va_rdev
= vp
->v_rdev
;
2157 vap
->va_seq
= zp
->z_seq
;
2160 * Add in any requested optional attributes and the create time.
2161 * Also set the corresponding bits in the returned attribute bitmap.
2163 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zfsvfs
->z_use_fuids
) {
2164 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2166 ((pzp
->zp_flags
& ZFS_ARCHIVE
) != 0);
2167 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2170 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2171 xoap
->xoa_readonly
=
2172 ((pzp
->zp_flags
& ZFS_READONLY
) != 0);
2173 XVA_SET_RTN(xvap
, XAT_READONLY
);
2176 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2178 ((pzp
->zp_flags
& ZFS_SYSTEM
) != 0);
2179 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2182 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2184 ((pzp
->zp_flags
& ZFS_HIDDEN
) != 0);
2185 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2188 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2189 xoap
->xoa_nounlink
=
2190 ((pzp
->zp_flags
& ZFS_NOUNLINK
) != 0);
2191 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2194 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2195 xoap
->xoa_immutable
=
2196 ((pzp
->zp_flags
& ZFS_IMMUTABLE
) != 0);
2197 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2200 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2201 xoap
->xoa_appendonly
=
2202 ((pzp
->zp_flags
& ZFS_APPENDONLY
) != 0);
2203 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2206 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2208 ((pzp
->zp_flags
& ZFS_NODUMP
) != 0);
2209 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2212 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2214 ((pzp
->zp_flags
& ZFS_OPAQUE
) != 0);
2215 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2218 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2219 xoap
->xoa_av_quarantined
=
2220 ((pzp
->zp_flags
& ZFS_AV_QUARANTINED
) != 0);
2221 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2224 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2225 xoap
->xoa_av_modified
=
2226 ((pzp
->zp_flags
& ZFS_AV_MODIFIED
) != 0);
2227 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2230 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2231 vp
->v_type
== VREG
&&
2232 (pzp
->zp_flags
& ZFS_BONUS_SCANSTAMP
)) {
2234 dmu_object_info_t doi
;
2237 * Only VREG files have anti-virus scanstamps, so we
2238 * won't conflict with symlinks in the bonus buffer.
2240 dmu_object_info_from_db(zp
->z_dbuf
, &doi
);
2241 len
= sizeof (xoap
->xoa_av_scanstamp
) +
2242 sizeof (znode_phys_t
);
2243 if (len
<= doi
.doi_bonus_size
) {
2245 * pzp points to the start of the
2246 * znode_phys_t. pzp + 1 points to the
2247 * first byte after the znode_phys_t.
2249 (void) memcpy(xoap
->xoa_av_scanstamp
,
2251 sizeof (xoap
->xoa_av_scanstamp
));
2252 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
2256 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2257 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, pzp
->zp_crtime
);
2258 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2262 ZFS_TIME_DECODE(&vap
->va_atime
, pzp
->zp_atime
);
2263 ZFS_TIME_DECODE(&vap
->va_mtime
, pzp
->zp_mtime
);
2264 ZFS_TIME_DECODE(&vap
->va_ctime
, pzp
->zp_ctime
);
2266 mutex_exit(&zp
->z_lock
);
2268 dmu_object_size_from_db(zp
->z_dbuf
, &vap
->va_blksize
, &vap
->va_nblocks
);
2270 if (zp
->z_blksz
== 0) {
2272 * Block size hasn't been set; suggest maximal I/O transfers.
2274 vap
->va_blksize
= zfsvfs
->z_max_blksz
;
2282 * Set the file attributes to the values contained in the
2285 * IN: vp - vnode of file to be modified.
2286 * vap - new attribute values.
2287 * If AT_XVATTR set, then optional attrs are being set
2288 * flags - ATTR_UTIME set if non-default time values provided.
2289 * - ATTR_NOACLCHECK (CIFS context only).
2290 * cr - credentials of caller.
2291 * ct - caller context
2293 * RETURN: 0 if success
2294 * error code if failure
2297 * vp - ctime updated, mtime updated if size changed.
2301 zfs_setattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
2302 caller_context_t
*ct
)
2304 znode_t
*zp
= VTOZ(vp
);
2306 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2311 uint_t mask
= vap
->va_mask
;
2316 int need_policy
= FALSE
;
2318 zfs_fuid_info_t
*fuidp
= NULL
;
2319 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2321 zfs_acl_t
*aclp
= NULL
;
2322 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2327 if (mask
& AT_NOSET
)
2334 zilog
= zfsvfs
->z_log
;
2337 * Make sure that if we have ephemeral uid/gid or xvattr specified
2338 * that file system is at proper version level
2341 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
2342 (((mask
& AT_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2343 ((mask
& AT_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2344 (mask
& AT_XVATTR
))) {
2349 if (mask
& AT_SIZE
&& vp
->v_type
== VDIR
) {
2354 if (mask
& AT_SIZE
&& vp
->v_type
!= VREG
&& vp
->v_type
!= VFIFO
) {
2360 * If this is an xvattr_t, then get a pointer to the structure of
2361 * optional attributes. If this is NULL, then we have a vattr_t.
2363 xoap
= xva_getxoptattr(xvap
);
2365 xva_init(&tmpxvattr
);
2368 * Immutable files can only alter immutable bit and atime
2370 if ((pzp
->zp_flags
& ZFS_IMMUTABLE
) &&
2371 ((mask
& (AT_SIZE
|AT_UID
|AT_GID
|AT_MTIME
|AT_MODE
)) ||
2372 ((mask
& AT_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2377 if ((mask
& AT_SIZE
) && (pzp
->zp_flags
& ZFS_READONLY
)) {
2383 * Verify timestamps doesn't overflow 32 bits.
2384 * ZFS can handle large timestamps, but 32bit syscalls can't
2385 * handle times greater than 2039. This check should be removed
2386 * once large timestamps are fully supported.
2388 if (mask
& (AT_ATIME
| AT_MTIME
)) {
2389 if (((mask
& AT_ATIME
) && TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2390 ((mask
& AT_MTIME
) && TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2399 if (zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) {
2405 * First validate permissions
2408 if (mask
& AT_SIZE
) {
2409 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2415 * XXX - Note, we are not providing any open
2416 * mode flags here (like FNDELAY), so we may
2417 * block if there are locks present... this
2418 * should be addressed in openat().
2420 /* XXX - would it be OK to generate a log record here? */
2421 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2428 if (mask
& (AT_ATIME
|AT_MTIME
) ||
2429 ((mask
& AT_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2430 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2431 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2432 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2433 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
))))
2434 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2437 if (mask
& (AT_UID
|AT_GID
)) {
2438 int idmask
= (mask
& (AT_UID
|AT_GID
));
2443 * NOTE: even if a new mode is being set,
2444 * we may clear S_ISUID/S_ISGID bits.
2447 if (!(mask
& AT_MODE
))
2448 vap
->va_mode
= pzp
->zp_mode
;
2451 * Take ownership or chgrp to group we are a member of
2454 take_owner
= (mask
& AT_UID
) && (vap
->va_uid
== crgetuid(cr
));
2455 take_group
= (mask
& AT_GID
) &&
2456 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
2459 * If both AT_UID and AT_GID are set then take_owner and
2460 * take_group must both be set in order to allow taking
2463 * Otherwise, send the check through secpolicy_vnode_setattr()
2467 if (((idmask
== (AT_UID
|AT_GID
)) && take_owner
&& take_group
) ||
2468 ((idmask
== AT_UID
) && take_owner
) ||
2469 ((idmask
== AT_GID
) && take_group
)) {
2470 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2471 skipaclchk
, cr
) == 0) {
2473 * Remove setuid/setgid for non-privileged users
2475 secpolicy_setid_clear(vap
, cr
);
2476 trim_mask
= (mask
& (AT_UID
|AT_GID
));
2485 mutex_enter(&zp
->z_lock
);
2486 oldva
.va_mode
= pzp
->zp_mode
;
2487 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2488 if (mask
& AT_XVATTR
) {
2490 * Update xvattr mask to include only those attributes
2491 * that are actually changing.
2493 * the bits will be restored prior to actually setting
2494 * the attributes so the caller thinks they were set.
2496 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2497 if (xoap
->xoa_appendonly
!=
2498 ((pzp
->zp_flags
& ZFS_APPENDONLY
) != 0)) {
2501 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2502 XVA_SET_REQ(&tmpxvattr
, XAT_APPENDONLY
);
2506 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2507 if (xoap
->xoa_nounlink
!=
2508 ((pzp
->zp_flags
& ZFS_NOUNLINK
) != 0)) {
2511 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2512 XVA_SET_REQ(&tmpxvattr
, XAT_NOUNLINK
);
2516 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2517 if (xoap
->xoa_immutable
!=
2518 ((pzp
->zp_flags
& ZFS_IMMUTABLE
) != 0)) {
2521 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2522 XVA_SET_REQ(&tmpxvattr
, XAT_IMMUTABLE
);
2526 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2527 if (xoap
->xoa_nodump
!=
2528 ((pzp
->zp_flags
& ZFS_NODUMP
) != 0)) {
2531 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2532 XVA_SET_REQ(&tmpxvattr
, XAT_NODUMP
);
2536 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2537 if (xoap
->xoa_av_modified
!=
2538 ((pzp
->zp_flags
& ZFS_AV_MODIFIED
) != 0)) {
2541 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2542 XVA_SET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
);
2546 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2547 if ((vp
->v_type
!= VREG
&&
2548 xoap
->xoa_av_quarantined
) ||
2549 xoap
->xoa_av_quarantined
!=
2550 ((pzp
->zp_flags
& ZFS_AV_QUARANTINED
) != 0)) {
2553 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2554 XVA_SET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
);
2558 if (need_policy
== FALSE
&&
2559 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2560 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2565 mutex_exit(&zp
->z_lock
);
2567 if (mask
& AT_MODE
) {
2568 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2569 err
= secpolicy_setid_setsticky_clear(vp
, vap
,
2575 trim_mask
|= AT_MODE
;
2583 * If trim_mask is set then take ownership
2584 * has been granted or write_acl is present and user
2585 * has the ability to modify mode. In that case remove
2586 * UID|GID and or MODE from mask so that
2587 * secpolicy_vnode_setattr() doesn't revoke it.
2591 saved_mask
= vap
->va_mask
;
2592 vap
->va_mask
&= ~trim_mask
;
2594 err
= secpolicy_vnode_setattr(cr
, vp
, vap
, &oldva
, flags
,
2595 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2602 vap
->va_mask
|= saved_mask
;
2606 * secpolicy_vnode_setattr, or take ownership may have
2609 mask
= vap
->va_mask
;
2611 tx
= dmu_tx_create(zfsvfs
->z_os
);
2612 dmu_tx_hold_bonus(tx
, zp
->z_id
);
2613 if (((mask
& AT_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2614 ((mask
& AT_GID
) && IS_EPHEMERAL(vap
->va_gid
))) {
2615 if (zfsvfs
->z_fuid_obj
== 0) {
2616 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
2617 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
2618 FUID_SIZE_ESTIMATE(zfsvfs
));
2619 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, FALSE
, NULL
);
2621 dmu_tx_hold_bonus(tx
, zfsvfs
->z_fuid_obj
);
2622 dmu_tx_hold_write(tx
, zfsvfs
->z_fuid_obj
, 0,
2623 FUID_SIZE_ESTIMATE(zfsvfs
));
2627 if (mask
& AT_MODE
) {
2628 uint64_t pmode
= pzp
->zp_mode
;
2630 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2632 if (err
= zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
)) {
2637 if (pzp
->zp_acl
.z_acl_extern_obj
) {
2638 /* Are we upgrading ACL from old V0 format to new V1 */
2639 if (zfsvfs
->z_version
<= ZPL_VERSION_FUID
&&
2640 pzp
->zp_acl
.z_acl_version
==
2641 ZFS_ACL_VERSION_INITIAL
) {
2642 dmu_tx_hold_free(tx
,
2643 pzp
->zp_acl
.z_acl_extern_obj
, 0,
2645 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2646 0, aclp
->z_acl_bytes
);
2648 dmu_tx_hold_write(tx
,
2649 pzp
->zp_acl
.z_acl_extern_obj
, 0,
2652 } else if (aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2653 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2654 0, aclp
->z_acl_bytes
);
2658 if ((mask
& (AT_UID
| AT_GID
)) && pzp
->zp_xattr
!= 0) {
2659 err
= zfs_zget(zp
->z_zfsvfs
, pzp
->zp_xattr
, &attrzp
);
2667 dmu_tx_hold_bonus(tx
, attrzp
->z_id
);
2670 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
2673 VN_RELE(ZTOV(attrzp
));
2680 if (err
== ERESTART
) {
2690 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
2693 * Set each attribute requested.
2694 * We group settings according to the locks they need to acquire.
2696 * Note: you cannot set ctime directly, although it will be
2697 * updated as a side-effect of calling this function.
2700 mutex_enter(&zp
->z_lock
);
2702 if (mask
& AT_MODE
) {
2703 mutex_enter(&zp
->z_acl_lock
);
2704 zp
->z_phys
->zp_mode
= new_mode
;
2705 err
= zfs_aclset_common(zp
, aclp
, cr
, &fuidp
, tx
);
2706 ASSERT3U(err
, ==, 0);
2707 mutex_exit(&zp
->z_acl_lock
);
2711 mutex_enter(&attrzp
->z_lock
);
2713 if (mask
& AT_UID
) {
2714 pzp
->zp_uid
= zfs_fuid_create(zfsvfs
,
2715 vap
->va_uid
, cr
, ZFS_OWNER
, tx
, &fuidp
);
2717 attrzp
->z_phys
->zp_uid
= zfs_fuid_create(zfsvfs
,
2718 vap
->va_uid
, cr
, ZFS_OWNER
, tx
, &fuidp
);
2722 if (mask
& AT_GID
) {
2723 pzp
->zp_gid
= zfs_fuid_create(zfsvfs
, vap
->va_gid
,
2724 cr
, ZFS_GROUP
, tx
, &fuidp
);
2726 attrzp
->z_phys
->zp_gid
= zfs_fuid_create(zfsvfs
,
2727 vap
->va_gid
, cr
, ZFS_GROUP
, tx
, &fuidp
);
2734 mutex_exit(&attrzp
->z_lock
);
2736 if (mask
& AT_ATIME
)
2737 ZFS_TIME_ENCODE(&vap
->va_atime
, pzp
->zp_atime
);
2739 if (mask
& AT_MTIME
)
2740 ZFS_TIME_ENCODE(&vap
->va_mtime
, pzp
->zp_mtime
);
2742 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2744 zfs_time_stamper_locked(zp
, CONTENT_MODIFIED
, tx
);
2746 zfs_time_stamper_locked(zp
, STATE_CHANGED
, tx
);
2748 * Do this after setting timestamps to prevent timestamp
2749 * update from toggling bit
2752 if (xoap
&& (mask
& AT_XVATTR
)) {
2755 * restore trimmed off masks
2756 * so that return masks can be set for caller.
2759 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_APPENDONLY
)) {
2760 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2762 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NOUNLINK
)) {
2763 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2765 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_IMMUTABLE
)) {
2766 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2768 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NODUMP
)) {
2769 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2771 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
)) {
2772 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2774 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
)) {
2775 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2778 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
2780 dmu_object_info_t doi
;
2782 ASSERT(vp
->v_type
== VREG
);
2784 /* Grow the bonus buffer if necessary. */
2785 dmu_object_info_from_db(zp
->z_dbuf
, &doi
);
2786 len
= sizeof (xoap
->xoa_av_scanstamp
) +
2787 sizeof (znode_phys_t
);
2788 if (len
> doi
.doi_bonus_size
)
2789 VERIFY(dmu_set_bonus(zp
->z_dbuf
, len
, tx
) == 0);
2791 zfs_xvattr_set(zp
, xvap
);
2795 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
2798 zfs_fuid_info_free(fuidp
);
2799 mutex_exit(&zp
->z_lock
);
2802 VN_RELE(ZTOV(attrzp
));
2810 typedef struct zfs_zlock
{
2811 krwlock_t
*zl_rwlock
; /* lock we acquired */
2812 znode_t
*zl_znode
; /* znode we held */
2813 struct zfs_zlock
*zl_next
; /* next in list */
2817 * Drop locks and release vnodes that were held by zfs_rename_lock().
2820 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
2824 while ((zl
= *zlpp
) != NULL
) {
2825 if (zl
->zl_znode
!= NULL
)
2826 VN_RELE(ZTOV(zl
->zl_znode
));
2827 rw_exit(zl
->zl_rwlock
);
2828 *zlpp
= zl
->zl_next
;
2829 kmem_free(zl
, sizeof (*zl
));
2834 * Search back through the directory tree, using the ".." entries.
2835 * Lock each directory in the chain to prevent concurrent renames.
2836 * Fail any attempt to move a directory into one of its own descendants.
2837 * XXX - z_parent_lock can overlap with map or grow locks
2840 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
2844 uint64_t rootid
= zp
->z_zfsvfs
->z_root
;
2845 uint64_t *oidp
= &zp
->z_id
;
2846 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
2847 krw_t rw
= RW_WRITER
;
2850 * First pass write-locks szp and compares to zp->z_id.
2851 * Later passes read-lock zp and compare to zp->z_parent.
2854 if (!rw_tryenter(rwlp
, rw
)) {
2856 * Another thread is renaming in this path.
2857 * Note that if we are a WRITER, we don't have any
2858 * parent_locks held yet.
2860 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
2862 * Drop our locks and restart
2864 zfs_rename_unlock(&zl
);
2868 rwlp
= &szp
->z_parent_lock
;
2873 * Wait for other thread to drop its locks
2879 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
2880 zl
->zl_rwlock
= rwlp
;
2881 zl
->zl_znode
= NULL
;
2882 zl
->zl_next
= *zlpp
;
2885 if (*oidp
== szp
->z_id
) /* We're a descendant of szp */
2888 if (*oidp
== rootid
) /* We've hit the top */
2891 if (rw
== RW_READER
) { /* i.e. not the first pass */
2892 int error
= zfs_zget(zp
->z_zfsvfs
, *oidp
, &zp
);
2897 oidp
= &zp
->z_phys
->zp_parent
;
2898 rwlp
= &zp
->z_parent_lock
;
2901 } while (zp
->z_id
!= sdzp
->z_id
);
2907 * Move an entry from the provided source directory to the target
2908 * directory. Change the entry name as indicated.
2910 * IN: sdvp - Source directory containing the "old entry".
2911 * snm - Old entry name.
2912 * tdvp - Target directory to contain the "new entry".
2913 * tnm - New entry name.
2914 * cr - credentials of caller.
2915 * ct - caller context
2916 * flags - case flags
2918 * RETURN: 0 if success
2919 * error code if failure
2922 * sdvp,tdvp - ctime|mtime updated
2926 zfs_rename(vnode_t
*sdvp
, char *snm
, vnode_t
*tdvp
, char *tnm
, cred_t
*cr
,
2927 caller_context_t
*ct
, int flags
)
2929 znode_t
*tdzp
, *szp
, *tzp
;
2930 znode_t
*sdzp
= VTOZ(sdvp
);
2931 zfsvfs_t
*zfsvfs
= sdzp
->z_zfsvfs
;
2934 zfs_dirlock_t
*sdl
, *tdl
;
2937 int cmp
, serr
, terr
;
2942 ZFS_VERIFY_ZP(sdzp
);
2943 zilog
= zfsvfs
->z_log
;
2946 * Make sure we have the real vp for the target directory.
2948 if (VOP_REALVP(tdvp
, &realvp
, ct
) == 0)
2951 if (tdvp
->v_vfsp
!= sdvp
->v_vfsp
) {
2957 ZFS_VERIFY_ZP(tdzp
);
2958 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
2959 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
2964 if (flags
& FIGNORECASE
)
2973 * This is to prevent the creation of links into attribute space
2974 * by renaming a linked file into/outof an attribute directory.
2975 * See the comment in zfs_link() for why this is considered bad.
2977 if ((tdzp
->z_phys
->zp_flags
& ZFS_XATTR
) !=
2978 (sdzp
->z_phys
->zp_flags
& ZFS_XATTR
)) {
2984 * Lock source and target directory entries. To prevent deadlock,
2985 * a lock ordering must be defined. We lock the directory with
2986 * the smallest object id first, or if it's a tie, the one with
2987 * the lexically first name.
2989 if (sdzp
->z_id
< tdzp
->z_id
) {
2991 } else if (sdzp
->z_id
> tdzp
->z_id
) {
2995 * First compare the two name arguments without
2996 * considering any case folding.
2998 int nofold
= (zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3000 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3001 ASSERT(error
== 0 || !zfsvfs
->z_utf8
);
3004 * POSIX: "If the old argument and the new argument
3005 * both refer to links to the same existing file,
3006 * the rename() function shall return successfully
3007 * and perform no other action."
3013 * If the file system is case-folding, then we may
3014 * have some more checking to do. A case-folding file
3015 * system is either supporting mixed case sensitivity
3016 * access or is completely case-insensitive. Note
3017 * that the file system is always case preserving.
3019 * In mixed sensitivity mode case sensitive behavior
3020 * is the default. FIGNORECASE must be used to
3021 * explicitly request case insensitive behavior.
3023 * If the source and target names provided differ only
3024 * by case (e.g., a request to rename 'tim' to 'Tim'),
3025 * we will treat this as a special case in the
3026 * case-insensitive mode: as long as the source name
3027 * is an exact match, we will allow this to proceed as
3028 * a name-change request.
3030 if ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
3031 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
3032 flags
& FIGNORECASE
)) &&
3033 u8_strcmp(snm
, tnm
, 0, zfsvfs
->z_norm
, U8_UNICODE_LATEST
,
3036 * case preserving rename request, require exact
3045 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3046 ZEXISTS
| zflg
, NULL
, NULL
);
3047 terr
= zfs_dirent_lock(&tdl
,
3048 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3050 terr
= zfs_dirent_lock(&tdl
,
3051 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3052 serr
= zfs_dirent_lock(&sdl
,
3053 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3059 * Source entry invalid or not there.
3062 zfs_dirent_unlock(tdl
);
3066 if (strcmp(snm
, "..") == 0)
3072 zfs_dirent_unlock(sdl
);
3074 if (strcmp(tnm
, "..") == 0)
3081 * Must have write access at the source to remove the old entry
3082 * and write access at the target to create the new entry.
3083 * Note that if target and source are the same, this can be
3084 * done in a single check.
3087 if (error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
))
3090 if (ZTOV(szp
)->v_type
== VDIR
) {
3092 * Check to make sure rename is valid.
3093 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3095 if (error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
))
3100 * Does target exist?
3104 * Source and target must be the same type.
3106 if (ZTOV(szp
)->v_type
== VDIR
) {
3107 if (ZTOV(tzp
)->v_type
!= VDIR
) {
3112 if (ZTOV(tzp
)->v_type
== VDIR
) {
3118 * POSIX dictates that when the source and target
3119 * entries refer to the same file object, rename
3120 * must do nothing and exit without error.
3122 if (szp
->z_id
== tzp
->z_id
) {
3128 vnevent_rename_src(ZTOV(szp
), sdvp
, snm
, ct
);
3130 vnevent_rename_dest(ZTOV(tzp
), tdvp
, tnm
, ct
);
3133 * notify the target directory if it is not the same
3134 * as source directory.
3137 vnevent_rename_dest_dir(tdvp
, ct
);
3140 tx
= dmu_tx_create(zfsvfs
->z_os
);
3141 dmu_tx_hold_bonus(tx
, szp
->z_id
); /* nlink changes */
3142 dmu_tx_hold_bonus(tx
, sdzp
->z_id
); /* nlink changes */
3143 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3144 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3146 dmu_tx_hold_bonus(tx
, tdzp
->z_id
); /* nlink changes */
3148 dmu_tx_hold_bonus(tx
, tzp
->z_id
); /* parent changes */
3149 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3150 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3153 zfs_rename_unlock(&zl
);
3154 zfs_dirent_unlock(sdl
);
3155 zfs_dirent_unlock(tdl
);
3159 if (error
== ERESTART
) {
3169 if (tzp
) /* Attempt to remove the existing target */
3170 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3173 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3175 szp
->z_phys
->zp_flags
|= ZFS_AV_MODIFIED
;
3177 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3180 zfs_log_rename(zilog
, tx
,
3181 TX_RENAME
| (flags
& FIGNORECASE
? TX_CI
: 0),
3182 sdzp
, sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3184 /* Update path information for the target vnode */
3185 vn_renamepath(tdvp
, ZTOV(szp
), tnm
, strlen(tnm
));
3192 zfs_rename_unlock(&zl
);
3194 zfs_dirent_unlock(sdl
);
3195 zfs_dirent_unlock(tdl
);
3206 * Insert the indicated symbolic reference entry into the directory.
3208 * IN: dvp - Directory to contain new symbolic link.
3209 * link - Name for new symlink entry.
3210 * vap - Attributes of new entry.
3211 * target - Target path of new symlink.
3212 * cr - credentials of caller.
3213 * ct - caller context
3214 * flags - case flags
3216 * RETURN: 0 if success
3217 * error code if failure
3220 * dvp - ctime|mtime updated
3224 zfs_symlink(vnode_t
*dvp
, char *name
, vattr_t
*vap
, char *link
, cred_t
*cr
,
3225 caller_context_t
*ct
, int flags
)
3227 znode_t
*zp
, *dzp
= VTOZ(dvp
);
3230 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
3232 int len
= strlen(link
);
3235 zfs_fuid_info_t
*fuidp
= NULL
;
3237 ASSERT(vap
->va_type
== VLNK
);
3241 zilog
= zfsvfs
->z_log
;
3243 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
3244 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3248 if (flags
& FIGNORECASE
)
3251 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
3256 if (len
> MAXPATHLEN
) {
3258 return (ENAMETOOLONG
);
3262 * Attempt to lock directory; fail if entry already exists.
3264 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3270 tx
= dmu_tx_create(zfsvfs
->z_os
);
3271 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3272 dmu_tx_hold_bonus(tx
, dzp
->z_id
);
3273 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3274 if (dzp
->z_phys
->zp_flags
& ZFS_INHERIT_ACE
)
3275 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, SPA_MAXBLOCKSIZE
);
3276 if (IS_EPHEMERAL(crgetuid(cr
)) || IS_EPHEMERAL(crgetgid(cr
))) {
3277 if (zfsvfs
->z_fuid_obj
== 0) {
3278 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
3279 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3280 FUID_SIZE_ESTIMATE(zfsvfs
));
3281 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, FALSE
, NULL
);
3283 dmu_tx_hold_bonus(tx
, zfsvfs
->z_fuid_obj
);
3284 dmu_tx_hold_write(tx
, zfsvfs
->z_fuid_obj
, 0,
3285 FUID_SIZE_ESTIMATE(zfsvfs
));
3288 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3290 zfs_dirent_unlock(dl
);
3291 if (error
== ERESTART
) {
3301 dmu_buf_will_dirty(dzp
->z_dbuf
, tx
);
3304 * Create a new object for the symlink.
3305 * Put the link content into bonus buffer if it will fit;
3306 * otherwise, store it just like any other file data.
3308 if (sizeof (znode_phys_t
) + len
<= dmu_bonus_max()) {
3309 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, len
, NULL
, &fuidp
);
3311 bcopy(link
, zp
->z_phys
+ 1, len
);
3315 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, 0, NULL
, &fuidp
);
3317 * Nothing can access the znode yet so no locking needed
3318 * for growing the znode's blocksize.
3320 zfs_grow_blocksize(zp
, len
, tx
);
3322 VERIFY(0 == dmu_buf_hold(zfsvfs
->z_os
,
3323 zp
->z_id
, 0, FTAG
, &dbp
));
3324 dmu_buf_will_dirty(dbp
, tx
);
3326 ASSERT3U(len
, <=, dbp
->db_size
);
3327 bcopy(link
, dbp
->db_data
, len
);
3328 dmu_buf_rele(dbp
, FTAG
);
3330 zp
->z_phys
->zp_size
= len
;
3333 * Insert the new object into the directory.
3335 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3338 uint64_t txtype
= TX_SYMLINK
;
3339 if (flags
& FIGNORECASE
)
3341 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3344 zfs_fuid_info_free(fuidp
);
3348 zfs_dirent_unlock(dl
);
3357 * Return, in the buffer contained in the provided uio structure,
3358 * the symbolic path referred to by vp.
3360 * IN: vp - vnode of symbolic link.
3361 * uoip - structure to contain the link path.
3362 * cr - credentials of caller.
3363 * ct - caller context
3365 * OUT: uio - structure to contain the link path.
3367 * RETURN: 0 if success
3368 * error code if failure
3371 * vp - atime updated
3375 zfs_readlink(vnode_t
*vp
, uio_t
*uio
, cred_t
*cr
, caller_context_t
*ct
)
3377 znode_t
*zp
= VTOZ(vp
);
3378 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3385 bufsz
= (size_t)zp
->z_phys
->zp_size
;
3386 if (bufsz
+ sizeof (znode_phys_t
) <= zp
->z_dbuf
->db_size
) {
3387 error
= uiomove(zp
->z_phys
+ 1,
3388 MIN((size_t)bufsz
, uio
->uio_resid
), UIO_READ
, uio
);
3391 error
= dmu_buf_hold(zfsvfs
->z_os
, zp
->z_id
, 0, FTAG
, &dbp
);
3396 error
= uiomove(dbp
->db_data
,
3397 MIN((size_t)bufsz
, uio
->uio_resid
), UIO_READ
, uio
);
3398 dmu_buf_rele(dbp
, FTAG
);
3401 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
3407 * Insert a new entry into directory tdvp referencing svp.
3409 * IN: tdvp - Directory to contain new entry.
3410 * svp - vnode of new entry.
3411 * name - name of new entry.
3412 * cr - credentials of caller.
3413 * ct - caller context
3415 * RETURN: 0 if success
3416 * error code if failure
3419 * tdvp - ctime|mtime updated
3420 * svp - ctime updated
3424 zfs_link(vnode_t
*tdvp
, vnode_t
*svp
, char *name
, cred_t
*cr
,
3425 caller_context_t
*ct
, int flags
)
3427 znode_t
*dzp
= VTOZ(tdvp
);
3429 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
3438 ASSERT(tdvp
->v_type
== VDIR
);
3442 zilog
= zfsvfs
->z_log
;
3444 if (VOP_REALVP(svp
, &realvp
, ct
) == 0)
3447 if (svp
->v_vfsp
!= tdvp
->v_vfsp
) {
3454 if (zfsvfs
->z_utf8
&& u8_validate(name
,
3455 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3459 if (flags
& FIGNORECASE
)
3464 * We do not support links between attributes and non-attributes
3465 * because of the potential security risk of creating links
3466 * into "normal" file space in order to circumvent restrictions
3467 * imposed in attribute space.
3469 if ((szp
->z_phys
->zp_flags
& ZFS_XATTR
) !=
3470 (dzp
->z_phys
->zp_flags
& ZFS_XATTR
)) {
3476 * POSIX dictates that we return EPERM here.
3477 * Better choices include ENOTSUP or EISDIR.
3479 if (svp
->v_type
== VDIR
) {
3484 owner
= zfs_fuid_map_id(zfsvfs
, szp
->z_phys
->zp_uid
, cr
, ZFS_OWNER
);
3485 if (owner
!= crgetuid(cr
) &&
3486 secpolicy_basic_link(cr
) != 0) {
3491 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
3497 * Attempt to lock directory; fail if entry already exists.
3499 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
3505 tx
= dmu_tx_create(zfsvfs
->z_os
);
3506 dmu_tx_hold_bonus(tx
, szp
->z_id
);
3507 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3508 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3510 zfs_dirent_unlock(dl
);
3511 if (error
== ERESTART
) {
3521 error
= zfs_link_create(dl
, szp
, tx
, 0);
3524 uint64_t txtype
= TX_LINK
;
3525 if (flags
& FIGNORECASE
)
3527 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
3532 zfs_dirent_unlock(dl
);
3535 vnevent_link(svp
, ct
);
3543 * zfs_null_putapage() is used when the file system has been force
3544 * unmounted. It just drops the pages.
3548 zfs_null_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
,
3549 size_t *lenp
, int flags
, cred_t
*cr
)
3551 pvn_write_done(pp
, B_INVAL
|B_FORCE
|B_ERROR
);
3556 * Push a page out to disk, klustering if possible.
3558 * IN: vp - file to push page to.
3559 * pp - page to push.
3560 * flags - additional flags.
3561 * cr - credentials of caller.
3563 * OUT: offp - start of range pushed.
3564 * lenp - len of range pushed.
3566 * RETURN: 0 if success
3567 * error code if failure
3569 * NOTE: callers must have locked the page to be pushed. On
3570 * exit, the page (and all other pages in the kluster) must be
3575 zfs_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
,
3576 size_t *lenp
, int flags
, cred_t
*cr
)
3578 znode_t
*zp
= VTOZ(vp
);
3579 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3581 u_offset_t off
, koff
;
3586 filesz
= zp
->z_phys
->zp_size
;
3590 * If our blocksize is bigger than the page size, try to kluster
3591 * multiple pages so that we write a full block (thus avoiding
3592 * a read-modify-write).
3594 if (off
< filesz
&& zp
->z_blksz
> PAGESIZE
) {
3595 klen
= P2ROUNDUP((ulong_t
)zp
->z_blksz
, PAGESIZE
);
3596 koff
= ISP2(klen
) ? P2ALIGN(off
, (u_offset_t
)klen
) : 0;
3597 ASSERT(koff
<= filesz
);
3598 if (koff
+ klen
> filesz
)
3599 klen
= P2ROUNDUP(filesz
- koff
, (uint64_t)PAGESIZE
);
3600 pp
= pvn_write_kluster(vp
, pp
, &off
, &len
, koff
, klen
, flags
);
3602 ASSERT3U(btop(len
), ==, btopr(len
));
3605 * Can't push pages past end-of-file.
3607 if (off
>= filesz
) {
3608 /* ignore all pages */
3611 } else if (off
+ len
> filesz
) {
3612 int npages
= btopr(filesz
- off
);
3615 page_list_break(&pp
, &trunc
, npages
);
3616 /* ignore pages past end of file */
3618 pvn_write_done(trunc
, flags
);
3622 tx
= dmu_tx_create(zfsvfs
->z_os
);
3623 dmu_tx_hold_write(tx
, zp
->z_id
, off
, len
);
3624 dmu_tx_hold_bonus(tx
, zp
->z_id
);
3625 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3627 if (err
== ERESTART
) {
3636 if (zp
->z_blksz
<= PAGESIZE
) {
3637 caddr_t va
= zfs_map_page(pp
, S_READ
);
3638 ASSERT3U(len
, <=, PAGESIZE
);
3639 dmu_write(zfsvfs
->z_os
, zp
->z_id
, off
, len
, va
, tx
);
3640 zfs_unmap_page(pp
, va
);
3642 err
= dmu_write_pages(zfsvfs
->z_os
, zp
->z_id
, off
, len
, pp
, tx
);
3646 zfs_time_stamper(zp
, CONTENT_MODIFIED
, tx
);
3647 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, off
, len
, 0);
3652 pvn_write_done(pp
, (err
? B_ERROR
: 0) | flags
);
3662 * Copy the portion of the file indicated from pages into the file.
3663 * The pages are stored in a page list attached to the files vnode.
3665 * IN: vp - vnode of file to push page data to.
3666 * off - position in file to put data.
3667 * len - amount of data to write.
3668 * flags - flags to control the operation.
3669 * cr - credentials of caller.
3670 * ct - caller context.
3672 * RETURN: 0 if success
3673 * error code if failure
3676 * vp - ctime|mtime updated
3680 zfs_putpage(vnode_t
*vp
, offset_t off
, size_t len
, int flags
, cred_t
*cr
,
3681 caller_context_t
*ct
)
3683 znode_t
*zp
= VTOZ(vp
);
3684 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3696 * Align this request to the file block size in case we kluster.
3697 * XXX - this can result in pretty aggresive locking, which can
3698 * impact simultanious read/write access. One option might be
3699 * to break up long requests (len == 0) into block-by-block
3700 * operations to get narrower locking.
3702 blksz
= zp
->z_blksz
;
3704 io_off
= P2ALIGN_TYPED(off
, blksz
, u_offset_t
);
3707 if (len
> 0 && ISP2(blksz
))
3708 io_len
= P2ROUNDUP_TYPED(len
+ (io_off
- off
), blksz
, size_t);
3714 * Search the entire vp list for pages >= io_off.
3716 rl
= zfs_range_lock(zp
, io_off
, UINT64_MAX
, RL_WRITER
);
3717 error
= pvn_vplist_dirty(vp
, io_off
, zfs_putapage
, flags
, cr
);
3720 rl
= zfs_range_lock(zp
, io_off
, io_len
, RL_WRITER
);
3722 if (off
> zp
->z_phys
->zp_size
) {
3723 /* past end of file */
3724 zfs_range_unlock(rl
);
3729 len
= MIN(io_len
, P2ROUNDUP(zp
->z_phys
->zp_size
, PAGESIZE
) - io_off
);
3731 for (off
= io_off
; io_off
< off
+ len
; io_off
+= io_len
) {
3732 if ((flags
& B_INVAL
) || ((flags
& B_ASYNC
) == 0)) {
3733 pp
= page_lookup(vp
, io_off
,
3734 (flags
& (B_INVAL
| B_FREE
)) ? SE_EXCL
: SE_SHARED
);
3736 pp
= page_lookup_nowait(vp
, io_off
,
3737 (flags
& B_FREE
) ? SE_EXCL
: SE_SHARED
);
3740 if (pp
!= NULL
&& pvn_getdirty(pp
, flags
)) {
3744 * Found a dirty page to push
3746 err
= zfs_putapage(vp
, pp
, &io_off
, &io_len
, flags
, cr
);
3754 zfs_range_unlock(rl
);
3755 if ((flags
& B_ASYNC
) == 0)
3756 zil_commit(zfsvfs
->z_log
, UINT64_MAX
, zp
->z_id
);
3763 zfs_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
3765 znode_t
*zp
= VTOZ(vp
);
3766 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3769 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_READER
);
3770 if (zp
->z_dbuf
== NULL
) {
3772 * The fs has been unmounted, or we did a
3773 * suspend/resume and this file no longer exists.
3775 if (vn_has_cached_data(vp
)) {
3776 (void) pvn_vplist_dirty(vp
, 0, zfs_null_putapage
,
3780 mutex_enter(&zp
->z_lock
);
3781 vp
->v_count
= 0; /* count arrives as 1 */
3782 mutex_exit(&zp
->z_lock
);
3783 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
3789 * Attempt to push any data in the page cache. If this fails
3790 * we will get kicked out later in zfs_zinactive().
3792 if (vn_has_cached_data(vp
)) {
3793 (void) pvn_vplist_dirty(vp
, 0, zfs_putapage
, B_INVAL
|B_ASYNC
,
3797 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
3798 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
3800 dmu_tx_hold_bonus(tx
, zp
->z_id
);
3801 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3805 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
3806 mutex_enter(&zp
->z_lock
);
3807 zp
->z_atime_dirty
= 0;
3808 mutex_exit(&zp
->z_lock
);
3814 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
3818 * Bounds-check the seek operation.
3820 * IN: vp - vnode seeking within
3821 * ooff - old file offset
3822 * noffp - pointer to new file offset
3823 * ct - caller context
3825 * RETURN: 0 if success
3826 * EINVAL if new offset invalid
3830 zfs_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
,
3831 caller_context_t
*ct
)
3833 if (vp
->v_type
== VDIR
)
3835 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
3839 * Pre-filter the generic locking function to trap attempts to place
3840 * a mandatory lock on a memory mapped file.
3843 zfs_frlock(vnode_t
*vp
, int cmd
, flock64_t
*bfp
, int flag
, offset_t offset
,
3844 flk_callback_t
*flk_cbp
, cred_t
*cr
, caller_context_t
*ct
)
3846 znode_t
*zp
= VTOZ(vp
);
3847 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3854 * We are following the UFS semantics with respect to mapcnt
3855 * here: If we see that the file is mapped already, then we will
3856 * return an error, but we don't worry about races between this
3857 * function and zfs_map().
3859 if (zp
->z_mapcnt
> 0 && MANDMODE((mode_t
)zp
->z_phys
->zp_mode
)) {
3863 error
= fs_frlock(vp
, cmd
, bfp
, flag
, offset
, flk_cbp
, cr
, ct
);
3869 * If we can't find a page in the cache, we will create a new page
3870 * and fill it with file data. For efficiency, we may try to fill
3871 * multiple pages at once (klustering) to fill up the supplied page
3875 zfs_fillpage(vnode_t
*vp
, u_offset_t off
, struct seg
*seg
,
3876 caddr_t addr
, page_t
*pl
[], size_t plsz
, enum seg_rw rw
)
3878 znode_t
*zp
= VTOZ(vp
);
3879 page_t
*pp
, *cur_pp
;
3880 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
3881 u_offset_t io_off
, total
;
3885 if (plsz
== PAGESIZE
|| zp
->z_blksz
<= PAGESIZE
) {
3887 * We only have a single page, don't bother klustering
3891 pp
= page_create_va(vp
, io_off
, io_len
, PG_WAIT
, seg
, addr
);
3894 * Try to find enough pages to fill the page list
3896 pp
= pvn_read_kluster(vp
, off
, seg
, addr
, &io_off
,
3897 &io_len
, off
, plsz
, 0);
3901 * The page already exists, nothing to do here.
3908 * Fill the pages in the kluster.
3911 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
3914 ASSERT3U(io_off
, ==, cur_pp
->p_offset
);
3915 va
= zfs_map_page(cur_pp
, S_WRITE
);
3916 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
);
3917 zfs_unmap_page(cur_pp
, va
);
3919 /* On error, toss the entire kluster */
3920 pvn_read_done(pp
, B_ERROR
);
3921 /* convert checksum errors into IO errors */
3926 cur_pp
= cur_pp
->p_next
;
3930 * Fill in the page list array from the kluster starting
3931 * from the desired offset `off'.
3932 * NOTE: the page list will always be null terminated.
3934 pvn_plist_init(pp
, pl
, plsz
, off
, io_len
, rw
);
3935 ASSERT(pl
== NULL
|| (*pl
)->p_offset
== off
);
3941 * Return pointers to the pages for the file region [off, off + len]
3942 * in the pl array. If plsz is greater than len, this function may
3943 * also return page pointers from after the specified region
3944 * (i.e. the region [off, off + plsz]). These additional pages are
3945 * only returned if they are already in the cache, or were created as
3946 * part of a klustered read.
3948 * IN: vp - vnode of file to get data from.
3949 * off - position in file to get data from.
3950 * len - amount of data to retrieve.
3951 * plsz - length of provided page list.
3952 * seg - segment to obtain pages for.
3953 * addr - virtual address of fault.
3954 * rw - mode of created pages.
3955 * cr - credentials of caller.
3956 * ct - caller context.
3958 * OUT: protp - protection mode of created pages.
3959 * pl - list of pages created.
3961 * RETURN: 0 if success
3962 * error code if failure
3965 * vp - atime updated
3969 zfs_getpage(vnode_t
*vp
, offset_t off
, size_t len
, uint_t
*protp
,
3970 page_t
*pl
[], size_t plsz
, struct seg
*seg
, caddr_t addr
,
3971 enum seg_rw rw
, cred_t
*cr
, caller_context_t
*ct
)
3973 znode_t
*zp
= VTOZ(vp
);
3974 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3978 /* we do our own caching, faultahead is unnecessary */
3981 else if (len
> plsz
)
3984 len
= P2ROUNDUP(len
, PAGESIZE
);
3985 ASSERT(plsz
>= len
);
3994 * Loop through the requested range [off, off + len] looking
3995 * for pages. If we don't find a page, we will need to create
3996 * a new page and fill it with data from the file.
3999 if (*pl
= page_lookup(vp
, off
, SE_SHARED
))
4001 else if (err
= zfs_fillpage(vp
, off
, seg
, addr
, pl
, plsz
, rw
))
4004 ASSERT3U((*pl
)->p_offset
, ==, off
);
4008 ASSERT3U(len
, >=, PAGESIZE
);
4011 ASSERT3U(plsz
, >=, PAGESIZE
);
4018 * Fill out the page array with any pages already in the cache.
4021 (*pl
++ = page_lookup_nowait(vp
, off
, SE_SHARED
))) {
4028 * Release any pages we have previously locked.
4033 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
4043 * Request a memory map for a section of a file. This code interacts
4044 * with common code and the VM system as follows:
4046 * common code calls mmap(), which ends up in smmap_common()
4048 * this calls VOP_MAP(), which takes you into (say) zfs
4050 * zfs_map() calls as_map(), passing segvn_create() as the callback
4052 * segvn_create() creates the new segment and calls VOP_ADDMAP()
4054 * zfs_addmap() updates z_mapcnt
4058 zfs_map(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t
*addrp
,
4059 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
4060 caller_context_t
*ct
)
4062 znode_t
*zp
= VTOZ(vp
);
4063 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4064 segvn_crargs_t vn_a
;
4070 if ((prot
& PROT_WRITE
) &&
4071 (zp
->z_phys
->zp_flags
& (ZFS_IMMUTABLE
| ZFS_READONLY
|
4077 if ((prot
& (PROT_READ
| PROT_EXEC
)) &&
4078 (zp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
)) {
4083 if (vp
->v_flag
& VNOMAP
) {
4088 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4093 if (vp
->v_type
!= VREG
) {
4099 * If file is locked, disallow mapping.
4101 if (MANDMODE((mode_t
)zp
->z_phys
->zp_mode
) && vn_has_flocks(vp
)) {
4107 error
= choose_addr(as
, addrp
, len
, off
, ADDR_VACALIGN
, flags
);
4115 vn_a
.offset
= (u_offset_t
)off
;
4116 vn_a
.type
= flags
& MAP_TYPE
;
4118 vn_a
.maxprot
= maxprot
;
4121 vn_a
.flags
= flags
& ~MAP_TYPE
;
4123 vn_a
.lgrp_mem_policy_flags
= 0;
4125 error
= as_map(as
, *addrp
, len
, segvn_create
, &vn_a
);
4134 zfs_addmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
4135 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
4136 caller_context_t
*ct
)
4138 uint64_t pages
= btopr(len
);
4140 atomic_add_64(&VTOZ(vp
)->z_mapcnt
, pages
);
4145 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4146 * more accurate mtime for the associated file. Since we don't have a way of
4147 * detecting when the data was actually modified, we have to resort to
4148 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4149 * last page is pushed. The problem occurs when the msync() call is omitted,
4150 * which by far the most common case:
4158 * putpage() via fsflush
4160 * If we wait until fsflush to come along, we can have a modification time that
4161 * is some arbitrary point in the future. In order to prevent this in the
4162 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4167 zfs_delmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
4168 size_t len
, uint_t prot
, uint_t maxprot
, uint_t flags
, cred_t
*cr
,
4169 caller_context_t
*ct
)
4171 uint64_t pages
= btopr(len
);
4173 ASSERT3U(VTOZ(vp
)->z_mapcnt
, >=, pages
);
4174 atomic_add_64(&VTOZ(vp
)->z_mapcnt
, -pages
);
4176 if ((flags
& MAP_SHARED
) && (prot
& PROT_WRITE
) &&
4177 vn_has_cached_data(vp
))
4178 (void) VOP_PUTPAGE(vp
, off
, len
, B_ASYNC
, cr
, ct
);
4184 * Free or allocate space in a file. Currently, this function only
4185 * supports the `F_FREESP' command. However, this command is somewhat
4186 * misnamed, as its functionality includes the ability to allocate as
4187 * well as free space.
4189 * IN: vp - vnode of file to free data in.
4190 * cmd - action to take (only F_FREESP supported).
4191 * bfp - section of file to free/alloc.
4192 * flag - current file open mode flags.
4193 * offset - current file offset.
4194 * cr - credentials of caller [UNUSED].
4195 * ct - caller context.
4197 * RETURN: 0 if success
4198 * error code if failure
4201 * vp - ctime|mtime updated
4205 zfs_space(vnode_t
*vp
, int cmd
, flock64_t
*bfp
, int flag
,
4206 offset_t offset
, cred_t
*cr
, caller_context_t
*ct
)
4208 znode_t
*zp
= VTOZ(vp
);
4209 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4216 if (cmd
!= F_FREESP
) {
4221 if (error
= convoff(vp
, bfp
, 0, offset
)) {
4226 if (bfp
->l_len
< 0) {
4232 len
= bfp
->l_len
; /* 0 means from off to end of file */
4234 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4242 zfs_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
4244 znode_t
*zp
= VTOZ(vp
);
4245 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4247 uint64_t object
= zp
->z_id
;
4253 gen
= (uint32_t)zp
->z_gen
;
4255 size
= (zfsvfs
->z_parent
!= zfsvfs
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4256 if (fidp
->fid_len
< size
) {
4257 fidp
->fid_len
= size
;
4262 zfid
= (zfid_short_t
*)fidp
;
4264 zfid
->zf_len
= size
;
4266 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4267 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4269 /* Must have a non-zero generation number to distinguish from .zfs */
4272 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4273 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4275 if (size
== LONG_FID_LEN
) {
4276 uint64_t objsetid
= dmu_objset_id(zfsvfs
->z_os
);
4279 zlfid
= (zfid_long_t
*)fidp
;
4281 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4282 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4284 /* XXX - this should be the generation number for the objset */
4285 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4286 zlfid
->zf_setgen
[i
] = 0;
4294 zfs_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
4295 caller_context_t
*ct
)
4307 case _PC_FILESIZEBITS
:
4311 case _PC_XATTR_EXISTS
:
4313 zfsvfs
= zp
->z_zfsvfs
;
4317 error
= zfs_dirent_lock(&dl
, zp
, "", &xzp
,
4318 ZXATTR
| ZEXISTS
| ZSHARED
, NULL
, NULL
);
4320 zfs_dirent_unlock(dl
);
4321 if (!zfs_dirempty(xzp
))
4324 } else if (error
== ENOENT
) {
4326 * If there aren't extended attributes, it's the
4327 * same as having zero of them.
4334 case _PC_SATTR_ENABLED
:
4335 case _PC_SATTR_EXISTS
:
4336 *valp
= vfs_has_feature(vp
->v_vfsp
, VFSFT_SYSATTR_VIEWS
) &&
4337 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
);
4340 case _PC_ACL_ENABLED
:
4341 *valp
= _ACL_ACE_ENABLED
;
4344 case _PC_MIN_HOLE_SIZE
:
4345 *valp
= (ulong_t
)SPA_MINBLOCKSIZE
;
4349 return (fs_pathconf(vp
, cmd
, valp
, cr
, ct
));
4355 zfs_getsecattr(vnode_t
*vp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
,
4356 caller_context_t
*ct
)
4358 znode_t
*zp
= VTOZ(vp
);
4359 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4361 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4365 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
4373 zfs_setsecattr(vnode_t
*vp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
,
4374 caller_context_t
*ct
)
4376 znode_t
*zp
= VTOZ(vp
);
4377 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4379 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4383 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
4389 * Predeclare these here so that the compiler assumes that
4390 * this is an "old style" function declaration that does
4391 * not include arguments => we won't get type mismatch errors
4392 * in the initializations that follow.
4394 static int zfs_inval();
4395 static int zfs_isdir();
4409 * Directory vnode operations template
4411 vnodeops_t
*zfs_dvnodeops
;
4412 const fs_operation_def_t zfs_dvnodeops_template
[] = {
4413 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
4414 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
4415 VOPNAME_READ
, { .error
= zfs_isdir
},
4416 VOPNAME_WRITE
, { .error
= zfs_isdir
},
4417 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
4418 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
4419 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
4420 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
4421 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
4422 VOPNAME_CREATE
, { .vop_create
= zfs_create
},
4423 VOPNAME_REMOVE
, { .vop_remove
= zfs_remove
},
4424 VOPNAME_LINK
, { .vop_link
= zfs_link
},
4425 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
4426 VOPNAME_MKDIR
, { .vop_mkdir
= zfs_mkdir
},
4427 VOPNAME_RMDIR
, { .vop_rmdir
= zfs_rmdir
},
4428 VOPNAME_READDIR
, { .vop_readdir
= zfs_readdir
},
4429 VOPNAME_SYMLINK
, { .vop_symlink
= zfs_symlink
},
4430 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
4431 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
4432 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
4433 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
4434 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
4435 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
4436 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
4437 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
4442 * Regular file vnode operations template
4444 vnodeops_t
*zfs_fvnodeops
;
4445 const fs_operation_def_t zfs_fvnodeops_template
[] = {
4446 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
4447 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
4448 VOPNAME_READ
, { .vop_read
= zfs_read
},
4449 VOPNAME_WRITE
, { .vop_write
= zfs_write
},
4450 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
4451 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
4452 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
4453 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
4454 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
4455 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
4456 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
4457 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
4458 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
4459 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
4460 VOPNAME_FRLOCK
, { .vop_frlock
= zfs_frlock
},
4461 VOPNAME_SPACE
, { .vop_space
= zfs_space
},
4462 VOPNAME_GETPAGE
, { .vop_getpage
= zfs_getpage
},
4463 VOPNAME_PUTPAGE
, { .vop_putpage
= zfs_putpage
},
4464 VOPNAME_MAP
, { .vop_map
= zfs_map
},
4465 VOPNAME_ADDMAP
, { .vop_addmap
= zfs_addmap
},
4466 VOPNAME_DELMAP
, { .vop_delmap
= zfs_delmap
},
4467 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
4468 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
4469 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
4470 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
4475 * Symbolic link vnode operations template
4477 vnodeops_t
*zfs_symvnodeops
;
4478 const fs_operation_def_t zfs_symvnodeops_template
[] = {
4479 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
4480 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
4481 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
4482 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
4483 VOPNAME_READLINK
, { .vop_readlink
= zfs_readlink
},
4484 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
4485 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
4486 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
4487 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
4492 * Extended attribute directory vnode operations template
4493 * This template is identical to the directory vnodes
4494 * operation template except for restricted operations:
4497 * Note that there are other restrictions embedded in:
4498 * zfs_create() - restrict type to VREG
4499 * zfs_link() - no links into/out of attribute space
4500 * zfs_rename() - no moves into/out of attribute space
4502 vnodeops_t
*zfs_xdvnodeops
;
4503 const fs_operation_def_t zfs_xdvnodeops_template
[] = {
4504 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
4505 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
4506 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
4507 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
4508 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
4509 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
4510 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
4511 VOPNAME_CREATE
, { .vop_create
= zfs_create
},
4512 VOPNAME_REMOVE
, { .vop_remove
= zfs_remove
},
4513 VOPNAME_LINK
, { .vop_link
= zfs_link
},
4514 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
4515 VOPNAME_MKDIR
, { .error
= zfs_inval
},
4516 VOPNAME_RMDIR
, { .vop_rmdir
= zfs_rmdir
},
4517 VOPNAME_READDIR
, { .vop_readdir
= zfs_readdir
},
4518 VOPNAME_SYMLINK
, { .error
= zfs_inval
},
4519 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
4520 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
4521 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
4522 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
4523 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
4524 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
4525 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
4526 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
4531 * Error vnode operations template
4533 vnodeops_t
*zfs_evnodeops
;
4534 const fs_operation_def_t zfs_evnodeops_template
[] = {
4535 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
4536 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},