4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 /* Portions Copyright 2007 Jeremy Teo */
26 /* Portions Copyright 2010 Robert Milkowski */
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>
60 #include <sys/dmu_objset.h>
66 #include <sys/dirent.h>
67 #include <sys/policy.h>
68 #include <sys/sunddi.h>
69 #include <sys/filio.h>
71 #include "fs/fs_subr.h"
72 #include <sys/zfs_ctldir.h>
73 #include <sys/zfs_fuid.h>
74 #include <sys/zfs_sa.h>
76 #include <sys/zfs_rlock.h>
77 #include <sys/extdirent.h>
78 #include <sys/kidmap.h>
85 * Each vnode op performs some logical unit of work. To do this, the ZPL must
86 * properly lock its in-core state, create a DMU transaction, do the work,
87 * record this work in the intent log (ZIL), commit the DMU transaction,
88 * and wait for the intent log to commit if it is a synchronous operation.
89 * Moreover, the vnode ops must work in both normal and log replay context.
90 * The ordering of events is important to avoid deadlocks and references
91 * to freed memory. The example below illustrates the following Big Rules:
93 * (1) A check must be made in each zfs thread for a mounted file system.
94 * This is done avoiding races using ZFS_ENTER(zfsvfs).
95 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
96 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
97 * can return EIO from the calling function.
99 * (2) VN_RELE() should always be the last thing except for zil_commit()
100 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
101 * First, if it's the last reference, the vnode/znode
102 * can be freed, so the zp may point to freed memory. Second, the last
103 * reference will call zfs_zinactive(), which may induce a lot of work --
104 * pushing cached pages (which acquires range locks) and syncing out
105 * cached atime changes. Third, zfs_zinactive() may require a new tx,
106 * which could deadlock the system if you were already holding one.
107 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
109 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
110 * as they can span dmu_tx_assign() calls.
112 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
113 * This is critical because we don't want to block while holding locks.
114 * Note, in particular, that if a lock is sometimes acquired before
115 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
116 * use a non-blocking assign can deadlock the system. The scenario:
118 * Thread A has grabbed a lock before calling dmu_tx_assign().
119 * Thread B is in an already-assigned tx, and blocks for this lock.
120 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
121 * forever, because the previous txg can't quiesce until B's tx commits.
123 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
124 * then drop all locks, call dmu_tx_wait(), and try again.
126 * (5) If the operation succeeded, generate the intent log entry for it
127 * before dropping locks. This ensures that the ordering of events
128 * in the intent log matches the order in which they actually occurred.
129 * During ZIL replay the zfs_log_* functions will update the sequence
130 * number to indicate the zil transaction has replayed.
132 * (6) At the end of each vnode op, the DMU tx must always commit,
133 * regardless of whether there were any errors.
135 * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid)
136 * to ensure that synchronous semantics are provided when necessary.
138 * In general, this is how things should be ordered in each vnode op:
140 * ZFS_ENTER(zfsvfs); // exit if unmounted
142 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
143 * rw_enter(...); // grab any other locks you need
144 * tx = dmu_tx_create(...); // get DMU tx
145 * dmu_tx_hold_*(); // hold each object you might modify
146 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
148 * rw_exit(...); // drop locks
149 * zfs_dirent_unlock(dl); // unlock directory entry
150 * VN_RELE(...); // release held vnodes
151 * if (error == ERESTART) {
156 * dmu_tx_abort(tx); // abort DMU tx
157 * ZFS_EXIT(zfsvfs); // finished in zfs
158 * return (error); // really out of space
160 * error = do_real_work(); // do whatever this VOP does
162 * zfs_log_*(...); // on success, make ZIL entry
163 * dmu_tx_commit(tx); // commit DMU tx -- error or not
164 * rw_exit(...); // drop locks
165 * zfs_dirent_unlock(dl); // unlock directory entry
166 * VN_RELE(...); // release held vnodes
167 * zil_commit(zilog, seq, foid); // synchronous when necessary
168 * ZFS_EXIT(zfsvfs); // finished in zfs
169 * return (error); // done, report error
174 zfs_open(vnode_t
**vpp
, int flag
, cred_t
*cr
, caller_context_t
*ct
)
176 znode_t
*zp
= VTOZ(*vpp
);
177 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
182 if ((flag
& FWRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
183 ((flag
& FAPPEND
) == 0)) {
188 if (!zfs_has_ctldir(zp
) && zp
->z_zfsvfs
->z_vscan
&&
189 ZTOV(zp
)->v_type
== VREG
&&
190 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0) {
191 if (fs_vscan(*vpp
, cr
, 0) != 0) {
197 /* Keep a count of the synchronous opens in the znode */
198 if (flag
& (FSYNC
| FDSYNC
))
199 atomic_inc_32(&zp
->z_sync_cnt
);
207 zfs_close(vnode_t
*vp
, int flag
, int count
, offset_t offset
, cred_t
*cr
,
208 caller_context_t
*ct
)
210 znode_t
*zp
= VTOZ(vp
);
211 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
214 * Clean up any locks held by this process on the vp.
216 cleanlocks(vp
, ddi_get_pid(), 0);
217 cleanshares(vp
, ddi_get_pid());
222 /* Decrement the synchronous opens in the znode */
223 if ((flag
& (FSYNC
| FDSYNC
)) && (count
== 1))
224 atomic_dec_32(&zp
->z_sync_cnt
);
226 if (!zfs_has_ctldir(zp
) && zp
->z_zfsvfs
->z_vscan
&&
227 ZTOV(zp
)->v_type
== VREG
&&
228 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0)
229 VERIFY(fs_vscan(vp
, cr
, 1) == 0);
236 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
237 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
240 zfs_holey(vnode_t
*vp
, int cmd
, offset_t
*off
)
242 znode_t
*zp
= VTOZ(vp
);
243 uint64_t noff
= (uint64_t)*off
; /* new offset */
248 file_sz
= zp
->z_size
;
249 if (noff
>= file_sz
) {
253 if (cmd
== _FIO_SEEK_HOLE
)
258 error
= dmu_offset_next(zp
->z_zfsvfs
->z_os
, zp
->z_id
, hole
, &noff
);
261 if ((error
== ESRCH
) || (noff
> file_sz
)) {
263 * Handle the virtual hole at the end of file.
280 zfs_ioctl(vnode_t
*vp
, int com
, intptr_t data
, int flag
, cred_t
*cred
,
281 int *rvalp
, caller_context_t
*ct
)
290 return (zfs_sync(vp
->v_vfsp
, 0, cred
));
293 * The following two ioctls are used by bfu. Faking out,
294 * necessary to avoid bfu errors.
302 if (ddi_copyin((void *)data
, &off
, sizeof (off
), flag
))
306 zfsvfs
= zp
->z_zfsvfs
;
310 /* offset parameter is in/out */
311 error
= zfs_holey(vp
, com
, &off
);
315 if (ddi_copyout(&off
, (void *)data
, sizeof (off
), flag
))
323 * Utility functions to map and unmap a single physical page. These
324 * are used to manage the mappable copies of ZFS file data, and therefore
325 * do not update ref/mod bits.
328 zfs_map_page(page_t
*pp
, enum seg_rw rw
)
331 return (hat_kpm_mapin(pp
, 0));
332 ASSERT(rw
== S_READ
|| rw
== S_WRITE
);
333 return (ppmapin(pp
, PROT_READ
| ((rw
== S_WRITE
) ? PROT_WRITE
: 0),
338 zfs_unmap_page(page_t
*pp
, caddr_t addr
)
341 hat_kpm_mapout(pp
, 0, addr
);
348 * When a file is memory mapped, we must keep the IO data synchronized
349 * between the DMU cache and the memory mapped pages. What this means:
351 * On Write: If we find a memory mapped page, we write to *both*
352 * the page and the dmu buffer.
355 update_pages(vnode_t
*vp
, int64_t start
, int len
, objset_t
*os
, uint64_t oid
)
359 off
= start
& PAGEOFFSET
;
360 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
362 uint64_t nbytes
= MIN(PAGESIZE
- off
, len
);
364 if (pp
= page_lookup(vp
, start
, SE_SHARED
)) {
367 va
= zfs_map_page(pp
, S_WRITE
);
368 (void) dmu_read(os
, oid
, start
+off
, nbytes
, va
+off
,
370 zfs_unmap_page(pp
, va
);
379 * When a file is memory mapped, we must keep the IO data synchronized
380 * between the DMU cache and the memory mapped pages. What this means:
382 * On Read: We "read" preferentially from memory mapped pages,
383 * else we default from the dmu buffer.
385 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
386 * the file is memory mapped.
389 mappedread(vnode_t
*vp
, int nbytes
, uio_t
*uio
)
391 znode_t
*zp
= VTOZ(vp
);
392 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
397 start
= uio
->uio_loffset
;
398 off
= start
& PAGEOFFSET
;
399 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
401 uint64_t bytes
= MIN(PAGESIZE
- off
, len
);
403 if (pp
= page_lookup(vp
, start
, SE_SHARED
)) {
406 va
= zfs_map_page(pp
, S_READ
);
407 error
= uiomove(va
+ off
, bytes
, UIO_READ
, uio
);
408 zfs_unmap_page(pp
, va
);
411 error
= dmu_read_uio(os
, zp
->z_id
, uio
, bytes
);
421 offset_t zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
424 * Read bytes from specified file into supplied buffer.
426 * IN: vp - vnode of file to be read from.
427 * uio - structure supplying read location, range info,
429 * ioflag - SYNC flags; used to provide FRSYNC semantics.
430 * cr - credentials of caller.
431 * ct - caller context
433 * OUT: uio - updated offset and range, buffer filled.
435 * RETURN: 0 if success
436 * error code if failure
439 * vp - atime updated if byte count > 0
443 zfs_read(vnode_t
*vp
, uio_t
*uio
, int ioflag
, cred_t
*cr
, caller_context_t
*ct
)
445 znode_t
*zp
= VTOZ(vp
);
446 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
457 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
463 * Validate file offset
465 if (uio
->uio_loffset
< (offset_t
)0) {
471 * Fasttrack empty reads
473 if (uio
->uio_resid
== 0) {
479 * Check for mandatory locks
481 if (MANDMODE(zp
->z_mode
)) {
482 if (error
= chklock(vp
, FREAD
,
483 uio
->uio_loffset
, uio
->uio_resid
, uio
->uio_fmode
, ct
)) {
490 * If we're in FRSYNC mode, sync out this znode before reading it.
492 if (ioflag
& FRSYNC
|| zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
493 zil_commit(zfsvfs
->z_log
, zp
->z_last_itx
, zp
->z_id
);
496 * Lock the range against changes.
498 rl
= zfs_range_lock(zp
, uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
501 * If we are reading past end-of-file we can skip
502 * to the end; but we might still need to set atime.
504 if (uio
->uio_loffset
>= zp
->z_size
) {
509 ASSERT(uio
->uio_loffset
< zp
->z_size
);
510 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
512 if ((uio
->uio_extflg
== UIO_XUIO
) &&
513 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
515 int blksz
= zp
->z_blksz
;
516 uint64_t offset
= uio
->uio_loffset
;
518 xuio
= (xuio_t
*)uio
;
520 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
523 ASSERT(offset
+ n
<= blksz
);
526 (void) dmu_xuio_init(xuio
, nblk
);
528 if (vn_has_cached_data(vp
)) {
530 * For simplicity, we always allocate a full buffer
531 * even if we only expect to read a portion of a block.
533 while (--nblk
>= 0) {
534 (void) dmu_xuio_add(xuio
,
535 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
542 nbytes
= MIN(n
, zfs_read_chunk_size
-
543 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
545 if (vn_has_cached_data(vp
))
546 error
= mappedread(vp
, nbytes
, uio
);
548 error
= dmu_read_uio(os
, zp
->z_id
, uio
, nbytes
);
550 /* convert checksum errors into IO errors */
559 zfs_range_unlock(rl
);
561 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
567 * Write the bytes to a file.
569 * IN: vp - vnode of file to be written to.
570 * uio - structure supplying write location, range info,
572 * ioflag - FAPPEND flag set if in append mode.
573 * cr - credentials of caller.
574 * ct - caller context (NFS/CIFS fem monitor only)
576 * OUT: uio - updated offset and range.
578 * RETURN: 0 if success
579 * error code if failure
582 * vp - ctime|mtime updated if byte count > 0
587 zfs_write(vnode_t
*vp
, uio_t
*uio
, int ioflag
, cred_t
*cr
, caller_context_t
*ct
)
589 znode_t
*zp
= VTOZ(vp
);
590 rlim64_t limit
= uio
->uio_llimit
;
591 ssize_t start_resid
= uio
->uio_resid
;
595 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
600 int max_blksz
= zfsvfs
->z_max_blksz
;
606 int iovcnt
= uio
->uio_iovcnt
;
607 iovec_t
*iovp
= uio
->uio_iov
;
610 sa_bulk_attr_t bulk
[4];
611 uint64_t mtime
[2], ctime
[2];
614 * Fasttrack empty write
620 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
626 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
627 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
628 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
630 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
634 * If immutable or not appending then return EPERM
636 if ((zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
637 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
638 (uio
->uio_loffset
< zp
->z_size
))) {
643 zilog
= zfsvfs
->z_log
;
646 * Validate file offset
648 woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
655 * Check for mandatory locks before calling zfs_range_lock()
656 * in order to prevent a deadlock with locks set via fcntl().
658 if (MANDMODE((mode_t
)zp
->z_mode
) &&
659 (error
= chklock(vp
, FWRITE
, woff
, n
, uio
->uio_fmode
, ct
)) != 0) {
665 * Pre-fault the pages to ensure slow (eg NFS) pages
667 * Skip this if uio contains loaned arc_buf.
669 if ((uio
->uio_extflg
== UIO_XUIO
) &&
670 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
671 xuio
= (xuio_t
*)uio
;
673 uio_prefaultpages(n
, uio
);
676 * If in append mode, set the io offset pointer to eof.
678 if (ioflag
& FAPPEND
) {
680 * Obtain an appending range lock to guarantee file append
681 * semantics. We reset the write offset once we have the lock.
683 rl
= zfs_range_lock(zp
, 0, n
, RL_APPEND
);
685 if (rl
->r_len
== UINT64_MAX
) {
687 * We overlocked the file because this write will cause
688 * the file block size to increase.
689 * Note that zp_size cannot change with this lock held.
693 uio
->uio_loffset
= woff
;
696 * Note that if the file block size will change as a result of
697 * this write, then this range lock will lock the entire file
698 * so that we can re-write the block safely.
700 rl
= zfs_range_lock(zp
, woff
, n
, RL_WRITER
);
704 zfs_range_unlock(rl
);
709 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
712 /* Will this write extend the file length? */
713 write_eof
= (woff
+ n
> zp
->z_size
);
715 end_size
= MAX(zp
->z_size
, woff
+ n
);
718 * Write the file in reasonable size chunks. Each chunk is written
719 * in a separate transaction; this keeps the intent log records small
720 * and allows us to do more fine-grained space accounting.
724 woff
= uio
->uio_loffset
;
726 if (zfs_owner_overquota(zfsvfs
, zp
, B_FALSE
) ||
727 zfs_owner_overquota(zfsvfs
, zp
, B_TRUE
)) {
729 dmu_return_arcbuf(abuf
);
734 if (xuio
&& abuf
== NULL
) {
735 ASSERT(i_iov
< iovcnt
);
737 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
738 dmu_xuio_clear(xuio
, i_iov
);
739 DTRACE_PROBE3(zfs_cp_write
, int, i_iov
,
740 iovec_t
*, aiov
, arc_buf_t
*, abuf
);
741 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
742 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
743 aiov
->iov_len
== arc_buf_size(abuf
)));
745 } else if (abuf
== NULL
&& n
>= max_blksz
&&
746 woff
>= zp
->z_size
&&
747 P2PHASE(woff
, max_blksz
) == 0 &&
748 zp
->z_blksz
== max_blksz
) {
750 * This write covers a full block. "Borrow" a buffer
751 * from the dmu so that we can fill it before we enter
752 * a transaction. This avoids the possibility of
753 * holding up the transaction if the data copy hangs
754 * up on a pagefault (e.g., from an NFS server mapping).
758 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
760 ASSERT(abuf
!= NULL
);
761 ASSERT(arc_buf_size(abuf
) == max_blksz
);
762 if (error
= uiocopy(abuf
->b_data
, max_blksz
,
763 UIO_WRITE
, uio
, &cbytes
)) {
764 dmu_return_arcbuf(abuf
);
767 ASSERT(cbytes
== max_blksz
);
771 * Start a transaction.
773 tx
= dmu_tx_create(zfsvfs
->z_os
);
774 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
775 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
776 zfs_sa_upgrade_txholds(tx
, zp
);
777 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
779 if (error
== ERESTART
) {
786 dmu_return_arcbuf(abuf
);
791 * If zfs_range_lock() over-locked we grow the blocksize
792 * and then reduce the lock range. This will only happen
793 * on the first iteration since zfs_range_reduce() will
794 * shrink down r_len to the appropriate size.
796 if (rl
->r_len
== UINT64_MAX
) {
799 if (zp
->z_blksz
> max_blksz
) {
800 ASSERT(!ISP2(zp
->z_blksz
));
801 new_blksz
= MIN(end_size
, SPA_MAXBLOCKSIZE
);
803 new_blksz
= MIN(end_size
, max_blksz
);
805 zfs_grow_blocksize(zp
, new_blksz
, tx
);
806 zfs_range_reduce(rl
, woff
, n
);
810 * XXX - should we really limit each write to z_max_blksz?
811 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
813 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
816 tx_bytes
= uio
->uio_resid
;
817 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
819 tx_bytes
-= uio
->uio_resid
;
822 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
824 * If this is not a full block write, but we are
825 * extending the file past EOF and this data starts
826 * block-aligned, use assign_arcbuf(). Otherwise,
827 * write via dmu_write().
829 if (tx_bytes
< max_blksz
&& (!write_eof
||
830 aiov
->iov_base
!= abuf
->b_data
)) {
832 dmu_write(zfsvfs
->z_os
, zp
->z_id
, woff
,
833 aiov
->iov_len
, aiov
->iov_base
, tx
);
834 dmu_return_arcbuf(abuf
);
835 xuio_stat_wbuf_copied();
837 ASSERT(xuio
|| tx_bytes
== max_blksz
);
838 dmu_assign_arcbuf(sa_get_db(zp
->z_sa_hdl
),
841 ASSERT(tx_bytes
<= uio
->uio_resid
);
842 uioskip(uio
, tx_bytes
);
844 if (tx_bytes
&& vn_has_cached_data(vp
)) {
845 update_pages(vp
, woff
,
846 tx_bytes
, zfsvfs
->z_os
, zp
->z_id
);
850 * If we made no progress, we're done. If we made even
851 * partial progress, update the znode and ZIL accordingly.
854 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
855 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
862 * Clear Set-UID/Set-GID bits on successful write if not
863 * privileged and at least one of the excute bits is set.
865 * It would be nice to to this after all writes have
866 * been done, but that would still expose the ISUID/ISGID
867 * to another app after the partial write is committed.
870 mutex_enter(&zp
->z_acl_lock
);
871 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
872 (S_IXUSR
>> 6))) != 0 &&
873 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
874 secpolicy_vnode_setid_retain(cr
,
875 (zp
->z_mode
& S_ISUID
) != 0 && zp
->z_uid
== 0) != 0) {
877 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
878 newmode
= zp
->z_mode
;
879 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zfsvfs
),
880 (void *)&newmode
, sizeof (uint64_t), tx
);
882 mutex_exit(&zp
->z_acl_lock
);
884 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
888 * Update the file size (zp_size) if it has changed;
889 * account for possible concurrent updates.
891 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
892 (void) atomic_cas_64(&zp
->z_size
, end_size
,
896 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
898 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
);
903 ASSERT(tx_bytes
== nbytes
);
907 zfs_range_unlock(rl
);
910 * If we're in replay mode, or we made no progress, return error.
911 * Otherwise, it's at least a partial write, so it's successful.
913 if (zfsvfs
->z_replay
|| uio
->uio_resid
== start_resid
) {
918 if (ioflag
& (FSYNC
| FDSYNC
) ||
919 zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
920 zil_commit(zilog
, zp
->z_last_itx
, zp
->z_id
);
927 zfs_get_done(zgd_t
*zgd
, int error
)
929 znode_t
*zp
= zgd
->zgd_private
;
930 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
933 dmu_buf_rele(zgd
->zgd_db
, zgd
);
935 zfs_range_unlock(zgd
->zgd_rl
);
938 * Release the vnode asynchronously as we currently have the
939 * txg stopped from syncing.
941 VN_RELE_ASYNC(ZTOV(zp
), dsl_pool_vnrele_taskq(dmu_objset_pool(os
)));
943 if (error
== 0 && zgd
->zgd_bp
)
944 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
946 kmem_free(zgd
, sizeof (zgd_t
));
950 static int zil_fault_io
= 0;
954 * Get data to generate a TX_WRITE intent log record.
957 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
959 zfsvfs_t
*zfsvfs
= arg
;
960 objset_t
*os
= zfsvfs
->z_os
;
962 uint64_t object
= lr
->lr_foid
;
963 uint64_t offset
= lr
->lr_offset
;
964 uint64_t size
= lr
->lr_length
;
965 blkptr_t
*bp
= &lr
->lr_blkptr
;
974 * Nothing to do if the file has been removed
976 if (zfs_zget(zfsvfs
, object
, &zp
) != 0)
978 if (zp
->z_unlinked
) {
980 * Release the vnode asynchronously as we currently have the
981 * txg stopped from syncing.
983 VN_RELE_ASYNC(ZTOV(zp
),
984 dsl_pool_vnrele_taskq(dmu_objset_pool(os
)));
988 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
989 zgd
->zgd_zilog
= zfsvfs
->z_log
;
990 zgd
->zgd_private
= zp
;
993 * Write records come in two flavors: immediate and indirect.
994 * For small writes it's cheaper to store the data with the
995 * log record (immediate); for large writes it's cheaper to
996 * sync the data and get a pointer to it (indirect) so that
997 * we don't have to write the data twice.
999 if (buf
!= NULL
) { /* immediate write */
1000 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
, RL_READER
);
1001 /* test for truncation needs to be done while range locked */
1002 if (offset
>= zp
->z_size
) {
1005 error
= dmu_read(os
, object
, offset
, size
, buf
,
1006 DMU_READ_NO_PREFETCH
);
1008 ASSERT(error
== 0 || error
== ENOENT
);
1009 } else { /* indirect write */
1011 * Have to lock the whole block to ensure when it's
1012 * written out and it's checksum is being calculated
1013 * that no one can change the data. We need to re-check
1014 * blocksize after we get the lock in case it's changed!
1019 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
1021 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
,
1023 if (zp
->z_blksz
== size
)
1026 zfs_range_unlock(zgd
->zgd_rl
);
1028 /* test for truncation needs to be done while range locked */
1029 if (lr
->lr_offset
>= zp
->z_size
)
1038 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1039 DMU_READ_NO_PREFETCH
);
1045 ASSERT(db
->db_offset
== offset
);
1046 ASSERT(db
->db_size
== size
);
1048 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1050 ASSERT(error
|| lr
->lr_length
<= zp
->z_blksz
);
1053 * On success, we need to wait for the write I/O
1054 * initiated by dmu_sync() to complete before we can
1055 * release this dbuf. We will finish everything up
1056 * in the zfs_get_done() callback.
1061 if (error
== EALREADY
) {
1062 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
1068 zfs_get_done(zgd
, error
);
1075 zfs_access(vnode_t
*vp
, int mode
, int flag
, cred_t
*cr
,
1076 caller_context_t
*ct
)
1078 znode_t
*zp
= VTOZ(vp
);
1079 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1085 if (flag
& V_ACE_MASK
)
1086 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
1088 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
1095 * If vnode is for a device return a specfs vnode instead.
1098 specvp_check(vnode_t
**vpp
, cred_t
*cr
)
1102 if (IS_DEVVP(*vpp
)) {
1105 svp
= specvp(*vpp
, (*vpp
)->v_rdev
, (*vpp
)->v_type
, cr
);
1116 * Lookup an entry in a directory, or an extended attribute directory.
1117 * If it exists, return a held vnode reference for it.
1119 * IN: dvp - vnode of directory to search.
1120 * nm - name of entry to lookup.
1121 * pnp - full pathname to lookup [UNUSED].
1122 * flags - LOOKUP_XATTR set if looking for an attribute.
1123 * rdir - root directory vnode [UNUSED].
1124 * cr - credentials of caller.
1125 * ct - caller context
1126 * direntflags - directory lookup flags
1127 * realpnp - returned pathname.
1129 * OUT: vpp - vnode of located entry, NULL if not found.
1131 * RETURN: 0 if success
1132 * error code if failure
1139 zfs_lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, struct pathname
*pnp
,
1140 int flags
, vnode_t
*rdir
, cred_t
*cr
, caller_context_t
*ct
,
1141 int *direntflags
, pathname_t
*realpnp
)
1143 znode_t
*zdp
= VTOZ(dvp
);
1144 zfsvfs_t
*zfsvfs
= zdp
->z_zfsvfs
;
1148 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1150 if (dvp
->v_type
!= VDIR
) {
1152 } else if (zdp
->z_sa_hdl
== NULL
) {
1156 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1157 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1165 vnode_t
*tvp
= dnlc_lookup(dvp
, nm
);
1168 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1173 if (tvp
== DNLC_NO_VNODE
) {
1178 return (specvp_check(vpp
, cr
));
1184 DTRACE_PROBE2(zfs__fastpath__lookup__miss
, vnode_t
*, dvp
, char *, nm
);
1191 if (flags
& LOOKUP_XATTR
) {
1193 * If the xattr property is off, refuse the lookup request.
1195 if (!(zfsvfs
->z_vfs
->vfs_flag
& VFS_XATTR
)) {
1201 * We don't allow recursive attributes..
1202 * Maybe someday we will.
1204 if (zdp
->z_pflags
& ZFS_XATTR
) {
1209 if (error
= zfs_get_xattrdir(VTOZ(dvp
), vpp
, cr
, flags
)) {
1215 * Do we have permission to get into attribute directory?
1218 if (error
= zfs_zaccess(VTOZ(*vpp
), ACE_EXECUTE
, 0,
1228 if (dvp
->v_type
!= VDIR
) {
1234 * Check accessibility of directory.
1237 if (error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
)) {
1242 if (zfsvfs
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1243 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1248 error
= zfs_dirlook(zdp
, nm
, vpp
, flags
, direntflags
, realpnp
);
1250 error
= specvp_check(vpp
, cr
);
1257 * Attempt to create a new entry in a directory. If the entry
1258 * already exists, truncate the file if permissible, else return
1259 * an error. Return the vp of the created or trunc'd file.
1261 * IN: dvp - vnode of directory to put new file entry in.
1262 * name - name of new file entry.
1263 * vap - attributes of new file.
1264 * excl - flag indicating exclusive or non-exclusive mode.
1265 * mode - mode to open file with.
1266 * cr - credentials of caller.
1267 * flag - large file flag [UNUSED].
1268 * ct - caller context
1269 * vsecp - ACL to be set
1271 * OUT: vpp - vnode of created or trunc'd entry.
1273 * RETURN: 0 if success
1274 * error code if failure
1277 * dvp - ctime|mtime updated if new entry created
1278 * vp - ctime|mtime always, atime if new
1283 zfs_create(vnode_t
*dvp
, char *name
, vattr_t
*vap
, vcexcl_t excl
,
1284 int mode
, vnode_t
**vpp
, cred_t
*cr
, int flag
, caller_context_t
*ct
,
1287 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1288 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1296 gid_t gid
= crgetgid(cr
);
1297 zfs_acl_ids_t acl_ids
;
1298 boolean_t fuid_dirtied
;
1299 boolean_t have_acl
= B_FALSE
;
1302 * If we have an ephemeral id, ACL, or XVATTR then
1303 * make sure file system is at proper version
1306 ksid
= crgetsid(cr
, KSID_OWNER
);
1308 uid
= ksid_getid(ksid
);
1312 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1313 (vsecp
|| (vap
->va_mask
& AT_XVATTR
) ||
1314 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1320 zilog
= zfsvfs
->z_log
;
1322 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
1323 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1328 if (vap
->va_mask
& AT_XVATTR
) {
1329 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1330 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
1338 if ((vap
->va_mode
& VSVTX
) && secpolicy_vnode_stky_modify(cr
))
1339 vap
->va_mode
&= ~VSVTX
;
1341 if (*name
== '\0') {
1343 * Null component name refers to the directory itself.
1350 /* possible VN_HOLD(zp) */
1353 if (flag
& FIGNORECASE
)
1356 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1359 if (strcmp(name
, "..") == 0)
1370 * Create a new file object and update the directory
1373 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
1378 * We only support the creation of regular files in
1379 * extended attribute directories.
1382 if ((dzp
->z_pflags
& ZFS_XATTR
) &&
1383 (vap
->va_type
!= VREG
)) {
1388 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1389 cr
, vsecp
, &acl_ids
)) != 0)
1393 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
)) {
1394 zfs_acl_ids_free(&acl_ids
);
1399 tx
= dmu_tx_create(os
);
1401 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1402 ZFS_SA_BASE_ATTR_SIZE
);
1404 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1406 zfs_fuid_txhold(zfsvfs
, tx
);
1407 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1408 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1409 if (!zfsvfs
->z_use_sa
&&
1410 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1411 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1412 0, acl_ids
.z_aclp
->z_acl_bytes
);
1414 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1416 zfs_dirent_unlock(dl
);
1417 if (error
== ERESTART
) {
1422 zfs_acl_ids_free(&acl_ids
);
1427 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1430 zfs_fuid_sync(zfsvfs
, tx
);
1432 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1433 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1434 if (flag
& FIGNORECASE
)
1436 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1437 vsecp
, acl_ids
.z_fuidp
, vap
);
1438 zfs_acl_ids_free(&acl_ids
);
1441 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1444 * A directory entry already exists for this name.
1447 * Can't truncate an existing file if in exclusive mode.
1454 * Can't open a directory for writing.
1456 if ((ZTOV(zp
)->v_type
== VDIR
) && (mode
& S_IWRITE
)) {
1461 * Verify requested access to file.
1463 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1467 mutex_enter(&dzp
->z_lock
);
1469 mutex_exit(&dzp
->z_lock
);
1472 * Truncate regular files if requested.
1474 if ((ZTOV(zp
)->v_type
== VREG
) &&
1475 (vap
->va_mask
& AT_SIZE
) && (vap
->va_size
== 0)) {
1476 /* we can't hold any locks when calling zfs_freesp() */
1477 zfs_dirent_unlock(dl
);
1479 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1481 vnevent_create(ZTOV(zp
), ct
);
1488 zfs_dirent_unlock(dl
);
1495 error
= specvp_check(vpp
, cr
);
1498 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1499 zil_commit(zilog
, UINT64_MAX
, 0);
1506 * Remove an entry from a directory.
1508 * IN: dvp - vnode of directory to remove entry from.
1509 * name - name of entry to remove.
1510 * cr - credentials of caller.
1511 * ct - caller context
1512 * flags - case flags
1514 * RETURN: 0 if success
1515 * error code if failure
1519 * vp - ctime (if nlink > 0)
1522 uint64_t null_xattr
= 0;
1526 zfs_remove(vnode_t
*dvp
, char *name
, cred_t
*cr
, caller_context_t
*ct
,
1529 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1530 znode_t
*xzp
= NULL
;
1532 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1534 uint64_t acl_obj
, xattr_obj
= 0;
1535 uint64_t xattr_obj_unlinked
= 0;
1538 boolean_t may_delete_now
, delete_now
= FALSE
;
1539 boolean_t unlinked
, toobig
= FALSE
;
1541 pathname_t
*realnmp
= NULL
;
1548 zilog
= zfsvfs
->z_log
;
1550 if (flags
& FIGNORECASE
) {
1558 * Attempt to lock directory; fail if entry doesn't exist.
1560 if (error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1570 if (error
= zfs_zaccess_delete(dzp
, zp
, cr
)) {
1575 * Need to use rmdir for removing directories.
1577 if (vp
->v_type
== VDIR
) {
1582 vnevent_remove(vp
, dvp
, name
, ct
);
1585 dnlc_remove(dvp
, realnmp
->pn_buf
);
1587 dnlc_remove(dvp
, name
);
1589 mutex_enter(&vp
->v_lock
);
1590 may_delete_now
= vp
->v_count
== 1 && !vn_has_cached_data(vp
);
1591 mutex_exit(&vp
->v_lock
);
1594 * We may delete the znode now, or we may put it in the unlinked set;
1595 * it depends on whether we're the last link, and on whether there are
1596 * other holds on the vnode. So we dmu_tx_hold() the right things to
1597 * allow for either case.
1599 tx
= dmu_tx_create(zfsvfs
->z_os
);
1600 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1601 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1602 zfs_sa_upgrade_txholds(tx
, zp
);
1603 zfs_sa_upgrade_txholds(tx
, dzp
);
1604 if (may_delete_now
) {
1606 zp
->z_size
> zp
->z_blksz
* DMU_MAX_DELETEBLKCNT
;
1607 /* if the file is too big, only hold_free a token amount */
1608 dmu_tx_hold_free(tx
, zp
->z_id
, 0,
1609 (toobig
? DMU_MAX_ACCESS
: DMU_OBJECT_END
));
1612 /* are there any extended attributes? */
1613 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1614 &xattr_obj
, sizeof (xattr_obj
));
1616 error
= zfs_zget(zfsvfs
, xattr_obj
, &xzp
);
1617 ASSERT3U(error
, ==, 0);
1618 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1619 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1622 /* are there any additional acls */
1623 if ((acl_obj
= ZFS_EXTERNAL_ACL(zp
)) != 0 && may_delete_now
)
1624 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
1626 /* charge as an update -- would be nice not to charge at all */
1627 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1629 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1631 zfs_dirent_unlock(dl
);
1633 if (error
== ERESTART
) {
1646 * Remove the directory entry.
1648 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1657 mutex_enter(&vp
->v_lock
);
1659 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1660 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1661 delete_now
= may_delete_now
&& !toobig
&&
1662 vp
->v_count
== 1 && !vn_has_cached_data(vp
) &&
1663 xattr_obj
== xattr_obj_unlinked
&& ZFS_EXTERNAL_ACL(zp
) ==
1665 mutex_exit(&vp
->v_lock
);
1669 if (xattr_obj_unlinked
) {
1670 ASSERT3U(xzp
->z_links
, ==, 2);
1671 mutex_enter(&xzp
->z_lock
);
1672 xzp
->z_unlinked
= 1;
1674 error
= sa_update(xzp
->z_sa_hdl
, SA_ZPL_LINKS(zfsvfs
),
1675 &xzp
->z_links
, sizeof (xzp
->z_links
), tx
);
1676 ASSERT3U(error
, ==, 0);
1677 mutex_exit(&xzp
->z_lock
);
1678 zfs_unlinked_add(xzp
, tx
);
1680 error
= sa_remove(zp
->z_sa_hdl
,
1681 SA_ZPL_XATTR(zfsvfs
), tx
);
1683 error
= sa_update(zp
->z_sa_hdl
,
1684 SA_ZPL_XATTR(zfsvfs
), &null_xattr
,
1685 sizeof (uint64_t), tx
);
1686 ASSERT3U(error
, ==, 0);
1688 mutex_enter(&zp
->z_lock
);
1689 mutex_enter(&vp
->v_lock
);
1691 ASSERT3U(vp
->v_count
, ==, 0);
1692 mutex_exit(&vp
->v_lock
);
1693 mutex_exit(&zp
->z_lock
);
1694 zfs_znode_delete(zp
, tx
);
1695 } else if (unlinked
) {
1696 zfs_unlinked_add(zp
, tx
);
1700 if (flags
& FIGNORECASE
)
1702 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
);
1709 zfs_dirent_unlock(dl
);
1716 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1717 zil_commit(zilog
, UINT64_MAX
, 0);
1724 * Create a new directory and insert it into dvp using the name
1725 * provided. Return a pointer to the inserted directory.
1727 * IN: dvp - vnode of directory to add subdir to.
1728 * dirname - name of new directory.
1729 * vap - attributes of new directory.
1730 * cr - credentials of caller.
1731 * ct - caller context
1732 * vsecp - ACL to be set
1734 * OUT: vpp - vnode of created directory.
1736 * RETURN: 0 if success
1737 * error code if failure
1740 * dvp - ctime|mtime updated
1741 * vp - ctime|mtime|atime updated
1745 zfs_mkdir(vnode_t
*dvp
, char *dirname
, vattr_t
*vap
, vnode_t
**vpp
, cred_t
*cr
,
1746 caller_context_t
*ct
, int flags
, vsecattr_t
*vsecp
)
1748 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1749 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1758 gid_t gid
= crgetgid(cr
);
1759 zfs_acl_ids_t acl_ids
;
1760 boolean_t fuid_dirtied
;
1762 ASSERT(vap
->va_type
== VDIR
);
1765 * If we have an ephemeral id, ACL, or XVATTR then
1766 * make sure file system is at proper version
1769 ksid
= crgetsid(cr
, KSID_OWNER
);
1771 uid
= ksid_getid(ksid
);
1774 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1775 (vsecp
|| (vap
->va_mask
& AT_XVATTR
) ||
1776 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1781 zilog
= zfsvfs
->z_log
;
1783 if (dzp
->z_pflags
& ZFS_XATTR
) {
1788 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1789 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1793 if (flags
& FIGNORECASE
)
1796 if (vap
->va_mask
& AT_XVATTR
) {
1797 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1798 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
1804 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1805 vsecp
, &acl_ids
)) != 0) {
1810 * First make sure the new directory doesn't exist.
1812 * Existence is checked first to make sure we don't return
1813 * EACCES instead of EEXIST which can cause some applications
1819 if (error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1821 zfs_acl_ids_free(&acl_ids
);
1826 if (error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
)) {
1827 zfs_acl_ids_free(&acl_ids
);
1828 zfs_dirent_unlock(dl
);
1833 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
)) {
1834 zfs_acl_ids_free(&acl_ids
);
1835 zfs_dirent_unlock(dl
);
1841 * Add a new entry to the directory.
1843 tx
= dmu_tx_create(zfsvfs
->z_os
);
1844 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1845 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1846 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1848 zfs_fuid_txhold(zfsvfs
, tx
);
1849 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1850 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1851 acl_ids
.z_aclp
->z_acl_bytes
);
1854 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1855 ZFS_SA_BASE_ATTR_SIZE
);
1857 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1859 zfs_dirent_unlock(dl
);
1860 if (error
== ERESTART
) {
1865 zfs_acl_ids_free(&acl_ids
);
1874 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1877 zfs_fuid_sync(zfsvfs
, tx
);
1880 * Now put new name in parent dir.
1882 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1886 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1887 if (flags
& FIGNORECASE
)
1889 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
1890 acl_ids
.z_fuidp
, vap
);
1892 zfs_acl_ids_free(&acl_ids
);
1896 zfs_dirent_unlock(dl
);
1898 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1899 zil_commit(zilog
, UINT64_MAX
, 0);
1906 * Remove a directory subdir entry. If the current working
1907 * directory is the same as the subdir to be removed, the
1910 * IN: dvp - vnode of directory to remove from.
1911 * name - name of directory to be removed.
1912 * cwd - vnode of current working directory.
1913 * cr - credentials of caller.
1914 * ct - caller context
1915 * flags - case flags
1917 * RETURN: 0 if success
1918 * error code if failure
1921 * dvp - ctime|mtime updated
1925 zfs_rmdir(vnode_t
*dvp
, char *name
, vnode_t
*cwd
, cred_t
*cr
,
1926 caller_context_t
*ct
, int flags
)
1928 znode_t
*dzp
= VTOZ(dvp
);
1931 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1940 zilog
= zfsvfs
->z_log
;
1942 if (flags
& FIGNORECASE
)
1948 * Attempt to lock directory; fail if entry doesn't exist.
1950 if (error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1958 if (error
= zfs_zaccess_delete(dzp
, zp
, cr
)) {
1962 if (vp
->v_type
!= VDIR
) {
1972 vnevent_rmdir(vp
, dvp
, name
, ct
);
1975 * Grab a lock on the directory to make sure that noone is
1976 * trying to add (or lookup) entries while we are removing it.
1978 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1981 * Grab a lock on the parent pointer to make sure we play well
1982 * with the treewalk and directory rename code.
1984 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1986 tx
= dmu_tx_create(zfsvfs
->z_os
);
1987 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1988 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1989 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1990 zfs_sa_upgrade_txholds(tx
, zp
);
1991 zfs_sa_upgrade_txholds(tx
, dzp
);
1992 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1994 rw_exit(&zp
->z_parent_lock
);
1995 rw_exit(&zp
->z_name_lock
);
1996 zfs_dirent_unlock(dl
);
1998 if (error
== ERESTART
) {
2008 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
2011 uint64_t txtype
= TX_RMDIR
;
2012 if (flags
& FIGNORECASE
)
2014 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
);
2019 rw_exit(&zp
->z_parent_lock
);
2020 rw_exit(&zp
->z_name_lock
);
2022 zfs_dirent_unlock(dl
);
2026 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2027 zil_commit(zilog
, UINT64_MAX
, 0);
2034 * Read as many directory entries as will fit into the provided
2035 * buffer from the given directory cursor position (specified in
2036 * the uio structure.
2038 * IN: vp - vnode of directory to read.
2039 * uio - structure supplying read location, range info,
2040 * and return buffer.
2041 * cr - credentials of caller.
2042 * ct - caller context
2043 * flags - case flags
2045 * OUT: uio - updated offset and range, buffer filled.
2046 * eofp - set to true if end-of-file detected.
2048 * RETURN: 0 if success
2049 * error code if failure
2052 * vp - atime updated
2054 * Note that the low 4 bits of the cookie returned by zap is always zero.
2055 * This allows us to use the low range for "special" directory entries:
2056 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2057 * we use the offset 2 for the '.zfs' directory.
2061 zfs_readdir(vnode_t
*vp
, uio_t
*uio
, cred_t
*cr
, int *eofp
,
2062 caller_context_t
*ct
, int flags
)
2064 znode_t
*zp
= VTOZ(vp
);
2068 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2073 zap_attribute_t zap
;
2074 uint_t bytes_wanted
;
2075 uint64_t offset
; /* must be unsigned; checks for < 1 */
2081 boolean_t check_sysattrs
;
2086 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
2087 &parent
, sizeof (parent
))) != 0) {
2093 * If we are not given an eof variable,
2100 * Check for valid iov_len.
2102 if (uio
->uio_iov
->iov_len
<= 0) {
2108 * Quit if directory has been removed (posix)
2110 if ((*eofp
= zp
->z_unlinked
) != 0) {
2117 offset
= uio
->uio_loffset
;
2118 prefetch
= zp
->z_zn_prefetch
;
2121 * Initialize the iterator cursor.
2125 * Start iteration from the beginning of the directory.
2127 zap_cursor_init(&zc
, os
, zp
->z_id
);
2130 * The offset is a serialized cursor.
2132 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
2136 * Get space to change directory entries into fs independent format.
2138 iovp
= uio
->uio_iov
;
2139 bytes_wanted
= iovp
->iov_len
;
2140 if (uio
->uio_segflg
!= UIO_SYSSPACE
|| uio
->uio_iovcnt
!= 1) {
2141 bufsize
= bytes_wanted
;
2142 outbuf
= kmem_alloc(bufsize
, KM_SLEEP
);
2143 odp
= (struct dirent64
*)outbuf
;
2145 bufsize
= bytes_wanted
;
2146 odp
= (struct dirent64
*)iovp
->iov_base
;
2148 eodp
= (struct edirent
*)odp
;
2151 * If this VFS supports the system attribute view interface; and
2152 * we're looking at an extended attribute directory; and we care
2153 * about normalization conflicts on this vfs; then we must check
2154 * for normalization conflicts with the sysattr name space.
2156 check_sysattrs
= vfs_has_feature(vp
->v_vfsp
, VFSFT_SYSATTR_VIEWS
) &&
2157 (vp
->v_flag
& V_XATTRDIR
) && zfsvfs
->z_norm
&&
2158 (flags
& V_RDDIR_ENTFLAGS
);
2161 * Transform to file-system independent format
2164 while (outcount
< bytes_wanted
) {
2170 * Special case `.', `..', and `.zfs'.
2173 (void) strcpy(zap
.za_name
, ".");
2174 zap
.za_normalization_conflict
= 0;
2176 } else if (offset
== 1) {
2177 (void) strcpy(zap
.za_name
, "..");
2178 zap
.za_normalization_conflict
= 0;
2180 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
2181 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
2182 zap
.za_normalization_conflict
= 0;
2183 objnum
= ZFSCTL_INO_ROOT
;
2188 if (error
= zap_cursor_retrieve(&zc
, &zap
)) {
2189 if ((*eofp
= (error
== ENOENT
)) != 0)
2195 if (zap
.za_integer_length
!= 8 ||
2196 zap
.za_num_integers
!= 1) {
2197 cmn_err(CE_WARN
, "zap_readdir: bad directory "
2198 "entry, obj = %lld, offset = %lld\n",
2199 (u_longlong_t
)zp
->z_id
,
2200 (u_longlong_t
)offset
);
2205 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
2207 * MacOS X can extract the object type here such as:
2208 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2211 if (check_sysattrs
&& !zap
.za_normalization_conflict
) {
2212 zap
.za_normalization_conflict
=
2213 xattr_sysattr_casechk(zap
.za_name
);
2217 if (flags
& V_RDDIR_ACCFILTER
) {
2219 * If we have no access at all, don't include
2220 * this entry in the returned information
2223 if (zfs_zget(zp
->z_zfsvfs
, objnum
, &ezp
) != 0)
2225 if (!zfs_has_access(ezp
, cr
)) {
2232 if (flags
& V_RDDIR_ENTFLAGS
)
2233 reclen
= EDIRENT_RECLEN(strlen(zap
.za_name
));
2235 reclen
= DIRENT64_RECLEN(strlen(zap
.za_name
));
2238 * Will this entry fit in the buffer?
2240 if (outcount
+ reclen
> bufsize
) {
2242 * Did we manage to fit anything in the buffer?
2250 if (flags
& V_RDDIR_ENTFLAGS
) {
2252 * Add extended flag entry:
2254 eodp
->ed_ino
= objnum
;
2255 eodp
->ed_reclen
= reclen
;
2256 /* NOTE: ed_off is the offset for the *next* entry */
2257 next
= &(eodp
->ed_off
);
2258 eodp
->ed_eflags
= zap
.za_normalization_conflict
?
2259 ED_CASE_CONFLICT
: 0;
2260 (void) strncpy(eodp
->ed_name
, zap
.za_name
,
2261 EDIRENT_NAMELEN(reclen
));
2262 eodp
= (edirent_t
*)((intptr_t)eodp
+ reclen
);
2267 odp
->d_ino
= objnum
;
2268 odp
->d_reclen
= reclen
;
2269 /* NOTE: d_off is the offset for the *next* entry */
2270 next
= &(odp
->d_off
);
2271 (void) strncpy(odp
->d_name
, zap
.za_name
,
2272 DIRENT64_NAMELEN(reclen
));
2273 odp
= (dirent64_t
*)((intptr_t)odp
+ reclen
);
2277 ASSERT(outcount
<= bufsize
);
2279 /* Prefetch znode */
2281 dmu_prefetch(os
, objnum
, 0, 0);
2285 * Move to the next entry, fill in the previous offset.
2287 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2288 zap_cursor_advance(&zc
);
2289 offset
= zap_cursor_serialize(&zc
);
2295 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2297 if (uio
->uio_segflg
== UIO_SYSSPACE
&& uio
->uio_iovcnt
== 1) {
2298 iovp
->iov_base
+= outcount
;
2299 iovp
->iov_len
-= outcount
;
2300 uio
->uio_resid
-= outcount
;
2301 } else if (error
= uiomove(outbuf
, (long)outcount
, UIO_READ
, uio
)) {
2303 * Reset the pointer.
2305 offset
= uio
->uio_loffset
;
2309 zap_cursor_fini(&zc
);
2310 if (uio
->uio_segflg
!= UIO_SYSSPACE
|| uio
->uio_iovcnt
!= 1)
2311 kmem_free(outbuf
, bufsize
);
2313 if (error
== ENOENT
)
2316 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
2318 uio
->uio_loffset
= offset
;
2323 ulong_t zfs_fsync_sync_cnt
= 4;
2326 zfs_fsync(vnode_t
*vp
, int syncflag
, cred_t
*cr
, caller_context_t
*ct
)
2328 znode_t
*zp
= VTOZ(vp
);
2329 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2332 * Regardless of whether this is required for standards conformance,
2333 * this is the logical behavior when fsync() is called on a file with
2334 * dirty pages. We use B_ASYNC since the ZIL transactions are already
2335 * going to be pushed out as part of the zil_commit().
2337 if (vn_has_cached_data(vp
) && !(syncflag
& FNODSYNC
) &&
2338 (vp
->v_type
== VREG
) && !(IS_SWAPVP(vp
)))
2339 (void) VOP_PUTPAGE(vp
, (offset_t
)0, (size_t)0, B_ASYNC
, cr
, ct
);
2341 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2343 if (zfsvfs
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2346 zil_commit(zfsvfs
->z_log
, zp
->z_last_itx
, zp
->z_id
);
2354 * Get the requested file attributes and place them in the provided
2357 * IN: vp - vnode of file.
2358 * vap - va_mask identifies requested attributes.
2359 * If AT_XVATTR set, then optional attrs are requested
2360 * flags - ATTR_NOACLCHECK (CIFS server context)
2361 * cr - credentials of caller.
2362 * ct - caller context
2364 * OUT: vap - attribute values.
2366 * RETURN: 0 (always succeeds)
2370 zfs_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
2371 caller_context_t
*ct
)
2373 znode_t
*zp
= VTOZ(vp
);
2374 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2377 uint64_t mtime
[2], ctime
[2];
2378 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2379 xoptattr_t
*xoap
= NULL
;
2380 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2381 sa_bulk_attr_t bulk
[2];
2387 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
2388 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
2390 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2396 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2397 * Also, if we are the owner don't bother, since owner should
2398 * always be allowed to read basic attributes of file.
2400 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) && (zp
->z_uid
!= crgetuid(cr
))) {
2401 if (error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2409 * Return all attributes. It's cheaper to provide the answer
2410 * than to determine whether we were asked the question.
2413 mutex_enter(&zp
->z_lock
);
2414 vap
->va_type
= vp
->v_type
;
2415 vap
->va_mode
= zp
->z_mode
& MODEMASK
;
2416 vap
->va_uid
= zp
->z_uid
;
2417 vap
->va_gid
= zp
->z_gid
;
2418 vap
->va_fsid
= zp
->z_zfsvfs
->z_vfs
->vfs_dev
;
2419 vap
->va_nodeid
= zp
->z_id
;
2420 if ((vp
->v_flag
& VROOT
) && zfs_show_ctldir(zp
))
2421 links
= zp
->z_links
+ 1;
2423 links
= zp
->z_links
;
2424 vap
->va_nlink
= MIN(links
, UINT32_MAX
); /* nlink_t limit! */
2425 vap
->va_size
= zp
->z_size
;
2426 vap
->va_rdev
= vp
->v_rdev
;
2427 vap
->va_seq
= zp
->z_seq
;
2430 * Add in any requested optional attributes and the create time.
2431 * Also set the corresponding bits in the returned attribute bitmap.
2433 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zfsvfs
->z_use_fuids
) {
2434 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2436 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2437 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2440 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2441 xoap
->xoa_readonly
=
2442 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2443 XVA_SET_RTN(xvap
, XAT_READONLY
);
2446 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2448 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2449 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2452 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2454 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2455 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2458 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2459 xoap
->xoa_nounlink
=
2460 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2461 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2464 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2465 xoap
->xoa_immutable
=
2466 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2467 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2470 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2471 xoap
->xoa_appendonly
=
2472 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2473 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2476 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2478 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2479 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2482 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2484 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2485 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2488 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2489 xoap
->xoa_av_quarantined
=
2490 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2491 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2494 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2495 xoap
->xoa_av_modified
=
2496 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2497 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2500 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2501 vp
->v_type
== VREG
) {
2502 zfs_sa_get_scanstamp(zp
, xvap
);
2505 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2508 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zfsvfs
),
2509 times
, sizeof (times
));
2510 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2511 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2514 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2515 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2516 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2520 ZFS_TIME_DECODE(&vap
->va_atime
, zp
->z_atime
);
2521 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2522 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2524 mutex_exit(&zp
->z_lock
);
2526 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2528 if (zp
->z_blksz
== 0) {
2530 * Block size hasn't been set; suggest maximal I/O transfers.
2532 vap
->va_blksize
= zfsvfs
->z_max_blksz
;
2540 * Set the file attributes to the values contained in the
2543 * IN: vp - vnode of file to be modified.
2544 * vap - new attribute values.
2545 * If AT_XVATTR set, then optional attrs are being set
2546 * flags - ATTR_UTIME set if non-default time values provided.
2547 * - ATTR_NOACLCHECK (CIFS context only).
2548 * cr - credentials of caller.
2549 * ct - caller context
2551 * RETURN: 0 if success
2552 * error code if failure
2555 * vp - ctime updated, mtime updated if size changed.
2559 zfs_setattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
2560 caller_context_t
*ct
)
2562 znode_t
*zp
= VTOZ(vp
);
2563 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2568 uint_t mask
= vap
->va_mask
;
2572 uint64_t new_uid
, new_gid
;
2573 uint64_t xattr_obj
= 0;
2574 uint64_t mtime
[2], ctime
[2];
2576 int need_policy
= FALSE
;
2578 zfs_fuid_info_t
*fuidp
= NULL
;
2579 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2581 zfs_acl_t
*aclp
= NULL
;
2582 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2583 boolean_t fuid_dirtied
= B_FALSE
;
2584 sa_bulk_attr_t bulk
[7], xattr_bulk
[7];
2585 int count
= 0, xattr_count
= 0;
2590 if (mask
& AT_NOSET
)
2596 zilog
= zfsvfs
->z_log
;
2599 * Make sure that if we have ephemeral uid/gid or xvattr specified
2600 * that file system is at proper version level
2603 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
2604 (((mask
& AT_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2605 ((mask
& AT_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2606 (mask
& AT_XVATTR
))) {
2611 if (mask
& AT_SIZE
&& vp
->v_type
== VDIR
) {
2616 if (mask
& AT_SIZE
&& vp
->v_type
!= VREG
&& vp
->v_type
!= VFIFO
) {
2622 * If this is an xvattr_t, then get a pointer to the structure of
2623 * optional attributes. If this is NULL, then we have a vattr_t.
2625 xoap
= xva_getxoptattr(xvap
);
2627 xva_init(&tmpxvattr
);
2630 * Immutable files can only alter immutable bit and atime
2632 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2633 ((mask
& (AT_SIZE
|AT_UID
|AT_GID
|AT_MTIME
|AT_MODE
)) ||
2634 ((mask
& AT_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2639 if ((mask
& AT_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2645 * Verify timestamps doesn't overflow 32 bits.
2646 * ZFS can handle large timestamps, but 32bit syscalls can't
2647 * handle times greater than 2039. This check should be removed
2648 * once large timestamps are fully supported.
2650 if (mask
& (AT_ATIME
| AT_MTIME
)) {
2651 if (((mask
& AT_ATIME
) && TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2652 ((mask
& AT_MTIME
) && TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2661 /* Can this be moved to before the top label? */
2662 if (zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) {
2668 * First validate permissions
2671 if (mask
& AT_SIZE
) {
2672 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2678 * XXX - Note, we are not providing any open
2679 * mode flags here (like FNDELAY), so we may
2680 * block if there are locks present... this
2681 * should be addressed in openat().
2683 /* XXX - would it be OK to generate a log record here? */
2684 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2691 if (mask
& (AT_ATIME
|AT_MTIME
) ||
2692 ((mask
& AT_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2693 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2694 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2695 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2696 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2697 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2701 if (mask
& (AT_UID
|AT_GID
)) {
2702 int idmask
= (mask
& (AT_UID
|AT_GID
));
2707 * NOTE: even if a new mode is being set,
2708 * we may clear S_ISUID/S_ISGID bits.
2711 if (!(mask
& AT_MODE
))
2712 vap
->va_mode
= zp
->z_mode
;
2715 * Take ownership or chgrp to group we are a member of
2718 take_owner
= (mask
& AT_UID
) && (vap
->va_uid
== crgetuid(cr
));
2719 take_group
= (mask
& AT_GID
) &&
2720 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
2723 * If both AT_UID and AT_GID are set then take_owner and
2724 * take_group must both be set in order to allow taking
2727 * Otherwise, send the check through secpolicy_vnode_setattr()
2731 if (((idmask
== (AT_UID
|AT_GID
)) && take_owner
&& take_group
) ||
2732 ((idmask
== AT_UID
) && take_owner
) ||
2733 ((idmask
== AT_GID
) && take_group
)) {
2734 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2735 skipaclchk
, cr
) == 0) {
2737 * Remove setuid/setgid for non-privileged users
2739 secpolicy_setid_clear(vap
, cr
);
2740 trim_mask
= (mask
& (AT_UID
|AT_GID
));
2749 mutex_enter(&zp
->z_lock
);
2750 oldva
.va_mode
= zp
->z_mode
;
2751 oldva
.va_uid
= zp
->z_uid
;
2752 oldva
.va_gid
= zp
->z_gid
;
2753 if (mask
& AT_XVATTR
) {
2755 * Update xvattr mask to include only those attributes
2756 * that are actually changing.
2758 * the bits will be restored prior to actually setting
2759 * the attributes so the caller thinks they were set.
2761 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2762 if (xoap
->xoa_appendonly
!=
2763 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2766 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2767 XVA_SET_REQ(&tmpxvattr
, XAT_APPENDONLY
);
2771 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2772 if (xoap
->xoa_nounlink
!=
2773 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2776 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2777 XVA_SET_REQ(&tmpxvattr
, XAT_NOUNLINK
);
2781 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2782 if (xoap
->xoa_immutable
!=
2783 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2786 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2787 XVA_SET_REQ(&tmpxvattr
, XAT_IMMUTABLE
);
2791 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2792 if (xoap
->xoa_nodump
!=
2793 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2796 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2797 XVA_SET_REQ(&tmpxvattr
, XAT_NODUMP
);
2801 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2802 if (xoap
->xoa_av_modified
!=
2803 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2806 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2807 XVA_SET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
);
2811 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2812 if ((vp
->v_type
!= VREG
&&
2813 xoap
->xoa_av_quarantined
) ||
2814 xoap
->xoa_av_quarantined
!=
2815 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2818 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2819 XVA_SET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
);
2823 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2824 mutex_exit(&zp
->z_lock
);
2829 if (need_policy
== FALSE
&&
2830 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2831 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2836 mutex_exit(&zp
->z_lock
);
2838 if (mask
& AT_MODE
) {
2839 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2840 err
= secpolicy_setid_setsticky_clear(vp
, vap
,
2846 trim_mask
|= AT_MODE
;
2854 * If trim_mask is set then take ownership
2855 * has been granted or write_acl is present and user
2856 * has the ability to modify mode. In that case remove
2857 * UID|GID and or MODE from mask so that
2858 * secpolicy_vnode_setattr() doesn't revoke it.
2862 saved_mask
= vap
->va_mask
;
2863 vap
->va_mask
&= ~trim_mask
;
2865 err
= secpolicy_vnode_setattr(cr
, vp
, vap
, &oldva
, flags
,
2866 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2873 vap
->va_mask
|= saved_mask
;
2877 * secpolicy_vnode_setattr, or take ownership may have
2880 mask
= vap
->va_mask
;
2882 if ((mask
& (AT_UID
| AT_GID
))) {
2883 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
), &xattr_obj
,
2884 sizeof (xattr_obj
));
2887 err
= zfs_zget(zp
->z_zfsvfs
, xattr_obj
, &attrzp
);
2891 if (mask
& AT_UID
) {
2892 new_uid
= zfs_fuid_create(zfsvfs
,
2893 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2894 if (vap
->va_uid
!= zp
->z_uid
&&
2895 zfs_fuid_overquota(zfsvfs
, B_FALSE
, new_uid
)) {
2901 if (mask
& AT_GID
) {
2902 new_gid
= zfs_fuid_create(zfsvfs
, (uint64_t)vap
->va_gid
,
2903 cr
, ZFS_GROUP
, &fuidp
);
2904 if (new_gid
!= zp
->z_gid
&&
2905 zfs_fuid_overquota(zfsvfs
, B_TRUE
, new_gid
)) {
2911 tx
= dmu_tx_create(zfsvfs
->z_os
);
2913 if (mask
& AT_MODE
) {
2914 uint64_t pmode
= zp
->z_mode
;
2915 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2917 if (err
= zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
))
2920 if (!zp
->z_is_sa
&& ZFS_EXTERNAL_ACL(zp
)) {
2922 * Are we upgrading ACL from old V0 format
2925 if (zfsvfs
->z_version
<= ZPL_VERSION_FUID
&&
2926 ZNODE_ACL_VERSION(zp
) ==
2927 ZFS_ACL_VERSION_INITIAL
) {
2928 dmu_tx_hold_free(tx
,
2929 ZFS_EXTERNAL_ACL(zp
), 0,
2931 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2932 0, aclp
->z_acl_bytes
);
2934 dmu_tx_hold_write(tx
, ZFS_EXTERNAL_ACL(zp
), 0,
2937 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2938 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2939 0, aclp
->z_acl_bytes
);
2941 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2943 if ((mask
& AT_XVATTR
) &&
2944 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2945 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2947 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2951 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2954 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
2956 zfs_fuid_txhold(zfsvfs
, tx
);
2958 zfs_sa_upgrade_txholds(tx
, zp
);
2960 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
2962 if (err
== ERESTART
)
2969 * Set each attribute requested.
2970 * We group settings according to the locks they need to acquire.
2972 * Note: you cannot set ctime directly, although it will be
2973 * updated as a side-effect of calling this function.
2976 mutex_enter(&zp
->z_lock
);
2978 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
2979 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2982 mutex_enter(&attrzp
->z_lock
);
2983 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2984 SA_ZPL_FLAGS(zfsvfs
), NULL
, &attrzp
->z_pflags
,
2985 sizeof (attrzp
->z_pflags
));
2988 if (mask
& (AT_UID
|AT_GID
)) {
2990 if (mask
& AT_UID
) {
2991 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
2992 &new_uid
, sizeof (new_uid
));
2993 zp
->z_uid
= zfs_fuid_map_id(zfsvfs
, new_uid
,
2996 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2997 SA_ZPL_UID(zfsvfs
), NULL
, &new_uid
,
2999 attrzp
->z_uid
= zp
->z_uid
;
3003 if (mask
& AT_GID
) {
3004 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
),
3005 NULL
, &new_gid
, sizeof (new_gid
));
3006 zp
->z_gid
= zfs_fuid_map_id(zfsvfs
, new_gid
, cr
,
3009 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3010 SA_ZPL_GID(zfsvfs
), NULL
, &new_gid
,
3012 attrzp
->z_gid
= zp
->z_gid
;
3015 if (!(mask
& AT_MODE
)) {
3016 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
),
3017 NULL
, &new_mode
, sizeof (new_mode
));
3018 new_mode
= zp
->z_mode
;
3020 err
= zfs_acl_chown_setattr(zp
);
3023 err
= zfs_acl_chown_setattr(attrzp
);
3028 if (mask
& AT_MODE
) {
3029 mutex_enter(&zp
->z_acl_lock
);
3030 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
3031 &new_mode
, sizeof (new_mode
));
3032 zp
->z_mode
= new_mode
;
3033 ASSERT3U((uintptr_t)aclp
, !=, NULL
);
3034 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
3035 ASSERT3U(err
, ==, 0);
3036 zp
->z_acl_cached
= aclp
;
3038 mutex_exit(&zp
->z_acl_lock
);
3042 mutex_exit(&attrzp
->z_lock
);
3044 if (mask
& AT_ATIME
) {
3045 ZFS_TIME_ENCODE(&vap
->va_atime
, zp
->z_atime
);
3046 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
3047 &zp
->z_atime
, sizeof (zp
->z_atime
));
3050 if (mask
& AT_MTIME
) {
3051 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
3052 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
3053 mtime
, sizeof (mtime
));
3056 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3057 if (mask
& AT_SIZE
&& !(mask
& AT_MTIME
)) {
3058 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
),
3059 NULL
, mtime
, sizeof (mtime
));
3060 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
3061 &ctime
, sizeof (ctime
));
3062 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
3064 } else if (mask
!= 0) {
3065 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
3066 &ctime
, sizeof (ctime
));
3067 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
,
3070 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3071 SA_ZPL_CTIME(zfsvfs
), NULL
,
3072 &ctime
, sizeof (ctime
));
3073 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
3074 mtime
, ctime
, B_TRUE
);
3078 * Do this after setting timestamps to prevent timestamp
3079 * update from toggling bit
3082 if (xoap
&& (mask
& AT_XVATTR
)) {
3085 * restore trimmed off masks
3086 * so that return masks can be set for caller.
3089 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_APPENDONLY
)) {
3090 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
3092 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NOUNLINK
)) {
3093 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
3095 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_IMMUTABLE
)) {
3096 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
3098 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NODUMP
)) {
3099 XVA_SET_REQ(xvap
, XAT_NODUMP
);
3101 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
)) {
3102 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
3104 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
)) {
3105 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
3108 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
3109 ASSERT(vp
->v_type
== VREG
);
3111 zfs_xvattr_set(zp
, xvap
, tx
);
3115 zfs_fuid_sync(zfsvfs
, tx
);
3118 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
3120 mutex_exit(&zp
->z_lock
);
3123 if (err
== 0 && attrzp
) {
3124 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3130 VN_RELE(ZTOV(attrzp
));
3135 zfs_fuid_info_free(fuidp
);
3141 if (err
== ERESTART
)
3144 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3150 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3151 zil_commit(zilog
, UINT64_MAX
, 0);
3157 typedef struct zfs_zlock
{
3158 krwlock_t
*zl_rwlock
; /* lock we acquired */
3159 znode_t
*zl_znode
; /* znode we held */
3160 struct zfs_zlock
*zl_next
; /* next in list */
3164 * Drop locks and release vnodes that were held by zfs_rename_lock().
3167 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3171 while ((zl
= *zlpp
) != NULL
) {
3172 if (zl
->zl_znode
!= NULL
)
3173 VN_RELE(ZTOV(zl
->zl_znode
));
3174 rw_exit(zl
->zl_rwlock
);
3175 *zlpp
= zl
->zl_next
;
3176 kmem_free(zl
, sizeof (*zl
));
3181 * Search back through the directory tree, using the ".." entries.
3182 * Lock each directory in the chain to prevent concurrent renames.
3183 * Fail any attempt to move a directory into one of its own descendants.
3184 * XXX - z_parent_lock can overlap with map or grow locks
3187 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3191 uint64_t rootid
= zp
->z_zfsvfs
->z_root
;
3192 uint64_t oidp
= zp
->z_id
;
3193 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3194 krw_t rw
= RW_WRITER
;
3197 * First pass write-locks szp and compares to zp->z_id.
3198 * Later passes read-lock zp and compare to zp->z_parent.
3201 if (!rw_tryenter(rwlp
, rw
)) {
3203 * Another thread is renaming in this path.
3204 * Note that if we are a WRITER, we don't have any
3205 * parent_locks held yet.
3207 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3209 * Drop our locks and restart
3211 zfs_rename_unlock(&zl
);
3215 rwlp
= &szp
->z_parent_lock
;
3220 * Wait for other thread to drop its locks
3226 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3227 zl
->zl_rwlock
= rwlp
;
3228 zl
->zl_znode
= NULL
;
3229 zl
->zl_next
= *zlpp
;
3232 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3235 if (oidp
== rootid
) /* We've hit the top */
3238 if (rw
== RW_READER
) { /* i.e. not the first pass */
3239 int error
= zfs_zget(zp
->z_zfsvfs
, oidp
, &zp
);
3244 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zp
->z_zfsvfs
),
3245 &oidp
, sizeof (oidp
));
3246 rwlp
= &zp
->z_parent_lock
;
3249 } while (zp
->z_id
!= sdzp
->z_id
);
3255 * Move an entry from the provided source directory to the target
3256 * directory. Change the entry name as indicated.
3258 * IN: sdvp - Source directory containing the "old entry".
3259 * snm - Old entry name.
3260 * tdvp - Target directory to contain the "new entry".
3261 * tnm - New entry name.
3262 * cr - credentials of caller.
3263 * ct - caller context
3264 * flags - case flags
3266 * RETURN: 0 if success
3267 * error code if failure
3270 * sdvp,tdvp - ctime|mtime updated
3274 zfs_rename(vnode_t
*sdvp
, char *snm
, vnode_t
*tdvp
, char *tnm
, cred_t
*cr
,
3275 caller_context_t
*ct
, int flags
)
3277 znode_t
*tdzp
, *szp
, *tzp
;
3278 znode_t
*sdzp
= VTOZ(sdvp
);
3279 zfsvfs_t
*zfsvfs
= sdzp
->z_zfsvfs
;
3282 zfs_dirlock_t
*sdl
, *tdl
;
3285 int cmp
, serr
, terr
;
3290 ZFS_VERIFY_ZP(sdzp
);
3291 zilog
= zfsvfs
->z_log
;
3294 * Make sure we have the real vp for the target directory.
3296 if (VOP_REALVP(tdvp
, &realvp
, ct
) == 0)
3299 if (tdvp
->v_vfsp
!= sdvp
->v_vfsp
|| zfsctl_is_node(tdvp
)) {
3305 ZFS_VERIFY_ZP(tdzp
);
3306 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
3307 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3312 if (flags
& FIGNORECASE
)
3321 * This is to prevent the creation of links into attribute space
3322 * by renaming a linked file into/outof an attribute directory.
3323 * See the comment in zfs_link() for why this is considered bad.
3325 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3331 * Lock source and target directory entries. To prevent deadlock,
3332 * a lock ordering must be defined. We lock the directory with
3333 * the smallest object id first, or if it's a tie, the one with
3334 * the lexically first name.
3336 if (sdzp
->z_id
< tdzp
->z_id
) {
3338 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3342 * First compare the two name arguments without
3343 * considering any case folding.
3345 int nofold
= (zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3347 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3348 ASSERT(error
== 0 || !zfsvfs
->z_utf8
);
3351 * POSIX: "If the old argument and the new argument
3352 * both refer to links to the same existing file,
3353 * the rename() function shall return successfully
3354 * and perform no other action."
3360 * If the file system is case-folding, then we may
3361 * have some more checking to do. A case-folding file
3362 * system is either supporting mixed case sensitivity
3363 * access or is completely case-insensitive. Note
3364 * that the file system is always case preserving.
3366 * In mixed sensitivity mode case sensitive behavior
3367 * is the default. FIGNORECASE must be used to
3368 * explicitly request case insensitive behavior.
3370 * If the source and target names provided differ only
3371 * by case (e.g., a request to rename 'tim' to 'Tim'),
3372 * we will treat this as a special case in the
3373 * case-insensitive mode: as long as the source name
3374 * is an exact match, we will allow this to proceed as
3375 * a name-change request.
3377 if ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
3378 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
3379 flags
& FIGNORECASE
)) &&
3380 u8_strcmp(snm
, tnm
, 0, zfsvfs
->z_norm
, U8_UNICODE_LATEST
,
3383 * case preserving rename request, require exact
3392 * If the source and destination directories are the same, we should
3393 * grab the z_name_lock of that directory only once.
3397 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3401 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3402 ZEXISTS
| zflg
, NULL
, NULL
);
3403 terr
= zfs_dirent_lock(&tdl
,
3404 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3406 terr
= zfs_dirent_lock(&tdl
,
3407 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3408 serr
= zfs_dirent_lock(&sdl
,
3409 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3415 * Source entry invalid or not there.
3418 zfs_dirent_unlock(tdl
);
3424 rw_exit(&sdzp
->z_name_lock
);
3426 if (strcmp(snm
, "..") == 0)
3432 zfs_dirent_unlock(sdl
);
3436 rw_exit(&sdzp
->z_name_lock
);
3438 if (strcmp(tnm
, "..") == 0)
3445 * Must have write access at the source to remove the old entry
3446 * and write access at the target to create the new entry.
3447 * Note that if target and source are the same, this can be
3448 * done in a single check.
3451 if (error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
))
3454 if (ZTOV(szp
)->v_type
== VDIR
) {
3456 * Check to make sure rename is valid.
3457 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3459 if (error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
))
3464 * Does target exist?
3468 * Source and target must be the same type.
3470 if (ZTOV(szp
)->v_type
== VDIR
) {
3471 if (ZTOV(tzp
)->v_type
!= VDIR
) {
3476 if (ZTOV(tzp
)->v_type
== VDIR
) {
3482 * POSIX dictates that when the source and target
3483 * entries refer to the same file object, rename
3484 * must do nothing and exit without error.
3486 if (szp
->z_id
== tzp
->z_id
) {
3492 vnevent_rename_src(ZTOV(szp
), sdvp
, snm
, ct
);
3494 vnevent_rename_dest(ZTOV(tzp
), tdvp
, tnm
, ct
);
3497 * notify the target directory if it is not the same
3498 * as source directory.
3501 vnevent_rename_dest_dir(tdvp
, ct
);
3504 tx
= dmu_tx_create(zfsvfs
->z_os
);
3505 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3506 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3507 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3508 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3510 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3511 zfs_sa_upgrade_txholds(tx
, tdzp
);
3514 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3515 zfs_sa_upgrade_txholds(tx
, tzp
);
3518 zfs_sa_upgrade_txholds(tx
, szp
);
3519 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3520 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3523 zfs_rename_unlock(&zl
);
3524 zfs_dirent_unlock(sdl
);
3525 zfs_dirent_unlock(tdl
);
3528 rw_exit(&sdzp
->z_name_lock
);
3533 if (error
== ERESTART
) {
3543 if (tzp
) /* Attempt to remove the existing target */
3544 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3547 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3549 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3551 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
3552 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3553 ASSERT3U(error
, ==, 0);
3555 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3557 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3558 (flags
& FIGNORECASE
? TX_CI
: 0),
3559 sdzp
, sdl
->dl_name
, tdzp
, tdl
->dl_name
,
3563 * Update path information for the target vnode
3565 vn_renamepath(tdvp
, ZTOV(szp
), tnm
,
3569 * At this point, we have successfully created
3570 * the target name, but have failed to remove
3571 * the source name. Since the create was done
3572 * with the ZRENAMING flag, there are
3573 * complications; for one, the link count is
3574 * wrong. The easiest way to deal with this
3575 * is to remove the newly created target, and
3576 * return the original error. This must
3577 * succeed; fortunately, it is very unlikely to
3578 * fail, since we just created it.
3580 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3581 ZRENAMING
, NULL
), ==, 0);
3589 zfs_rename_unlock(&zl
);
3591 zfs_dirent_unlock(sdl
);
3592 zfs_dirent_unlock(tdl
);
3595 rw_exit(&sdzp
->z_name_lock
);
3602 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3603 zil_commit(zilog
, UINT64_MAX
, 0);
3610 * Insert the indicated symbolic reference entry into the directory.
3612 * IN: dvp - Directory to contain new symbolic link.
3613 * link - Name for new symlink entry.
3614 * vap - Attributes of new entry.
3615 * target - Target path of new symlink.
3616 * cr - credentials of caller.
3617 * ct - caller context
3618 * flags - case flags
3620 * RETURN: 0 if success
3621 * error code if failure
3624 * dvp - ctime|mtime updated
3628 zfs_symlink(vnode_t
*dvp
, char *name
, vattr_t
*vap
, char *link
, cred_t
*cr
,
3629 caller_context_t
*ct
, int flags
)
3631 znode_t
*zp
, *dzp
= VTOZ(dvp
);
3634 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
3636 uint64_t len
= strlen(link
);
3639 zfs_acl_ids_t acl_ids
;
3640 boolean_t fuid_dirtied
;
3641 uint64_t txtype
= TX_SYMLINK
;
3643 ASSERT(vap
->va_type
== VLNK
);
3647 zilog
= zfsvfs
->z_log
;
3649 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
3650 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3654 if (flags
& FIGNORECASE
)
3657 if (len
> MAXPATHLEN
) {
3659 return (ENAMETOOLONG
);
3662 if ((error
= zfs_acl_ids_create(dzp
, 0,
3663 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3669 * Attempt to lock directory; fail if entry already exists.
3671 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3673 zfs_acl_ids_free(&acl_ids
);
3678 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
3679 zfs_acl_ids_free(&acl_ids
);
3680 zfs_dirent_unlock(dl
);
3685 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
)) {
3686 zfs_acl_ids_free(&acl_ids
);
3687 zfs_dirent_unlock(dl
);
3691 tx
= dmu_tx_create(zfsvfs
->z_os
);
3692 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3693 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3694 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3695 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3696 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3697 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3698 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3699 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3700 acl_ids
.z_aclp
->z_acl_bytes
);
3703 zfs_fuid_txhold(zfsvfs
, tx
);
3704 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3706 zfs_dirent_unlock(dl
);
3707 if (error
== ERESTART
) {
3712 zfs_acl_ids_free(&acl_ids
);
3719 * Create a new object for the symlink.
3720 * for version 4 ZPL datsets the symlink will be an SA attribute
3722 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3725 zfs_fuid_sync(zfsvfs
, tx
);
3728 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zfsvfs
),
3731 zfs_sa_symlink(zp
, link
, len
, tx
);
3734 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
3735 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3737 * Insert the new object into the directory.
3739 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3741 if (flags
& FIGNORECASE
)
3743 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3745 zfs_acl_ids_free(&acl_ids
);
3749 zfs_dirent_unlock(dl
);
3753 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3754 zil_commit(zilog
, UINT64_MAX
, 0);
3761 * Return, in the buffer contained in the provided uio structure,
3762 * the symbolic path referred to by vp.
3764 * IN: vp - vnode of symbolic link.
3765 * uoip - structure to contain the link path.
3766 * cr - credentials of caller.
3767 * ct - caller context
3769 * OUT: uio - structure to contain the link path.
3771 * RETURN: 0 if success
3772 * error code if failure
3775 * vp - atime updated
3779 zfs_readlink(vnode_t
*vp
, uio_t
*uio
, cred_t
*cr
, caller_context_t
*ct
)
3781 znode_t
*zp
= VTOZ(vp
);
3782 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3789 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3790 SA_ZPL_SYMLINK(zfsvfs
), uio
);
3792 error
= zfs_sa_readlink(zp
, uio
);
3794 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
3801 * Insert a new entry into directory tdvp referencing svp.
3803 * IN: tdvp - Directory to contain new entry.
3804 * svp - vnode of new entry.
3805 * name - name of new entry.
3806 * cr - credentials of caller.
3807 * ct - caller context
3809 * RETURN: 0 if success
3810 * error code if failure
3813 * tdvp - ctime|mtime updated
3814 * svp - ctime updated
3818 zfs_link(vnode_t
*tdvp
, vnode_t
*svp
, char *name
, cred_t
*cr
,
3819 caller_context_t
*ct
, int flags
)
3821 znode_t
*dzp
= VTOZ(tdvp
);
3823 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
3832 ASSERT(tdvp
->v_type
== VDIR
);
3836 zilog
= zfsvfs
->z_log
;
3838 if (VOP_REALVP(svp
, &realvp
, ct
) == 0)
3842 * POSIX dictates that we return EPERM here.
3843 * Better choices include ENOTSUP or EISDIR.
3845 if (svp
->v_type
== VDIR
) {
3850 if (svp
->v_vfsp
!= tdvp
->v_vfsp
|| zfsctl_is_node(svp
)) {
3858 /* Prevent links to .zfs/shares files */
3860 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
3861 &parent
, sizeof (uint64_t))) != 0) {
3865 if (parent
== zfsvfs
->z_shares_dir
) {
3870 if (zfsvfs
->z_utf8
&& u8_validate(name
,
3871 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3875 if (flags
& FIGNORECASE
)
3879 * We do not support links between attributes and non-attributes
3880 * because of the potential security risk of creating links
3881 * into "normal" file space in order to circumvent restrictions
3882 * imposed in attribute space.
3884 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
3890 if (szp
->z_uid
!= crgetuid(cr
) &&
3891 secpolicy_basic_link(cr
) != 0) {
3896 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
3903 * Attempt to lock directory; fail if entry already exists.
3905 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
3911 tx
= dmu_tx_create(zfsvfs
->z_os
);
3912 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3913 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3914 zfs_sa_upgrade_txholds(tx
, szp
);
3915 zfs_sa_upgrade_txholds(tx
, dzp
);
3916 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3918 zfs_dirent_unlock(dl
);
3919 if (error
== ERESTART
) {
3929 error
= zfs_link_create(dl
, szp
, tx
, 0);
3932 uint64_t txtype
= TX_LINK
;
3933 if (flags
& FIGNORECASE
)
3935 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
3940 zfs_dirent_unlock(dl
);
3943 vnevent_link(svp
, ct
);
3946 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3947 zil_commit(zilog
, UINT64_MAX
, 0);
3954 * zfs_null_putapage() is used when the file system has been force
3955 * unmounted. It just drops the pages.
3959 zfs_null_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
,
3960 size_t *lenp
, int flags
, cred_t
*cr
)
3962 pvn_write_done(pp
, B_INVAL
|B_FORCE
|B_ERROR
);
3967 * Push a page out to disk, klustering if possible.
3969 * IN: vp - file to push page to.
3970 * pp - page to push.
3971 * flags - additional flags.
3972 * cr - credentials of caller.
3974 * OUT: offp - start of range pushed.
3975 * lenp - len of range pushed.
3977 * RETURN: 0 if success
3978 * error code if failure
3980 * NOTE: callers must have locked the page to be pushed. On
3981 * exit, the page (and all other pages in the kluster) must be
3986 zfs_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
,
3987 size_t *lenp
, int flags
, cred_t
*cr
)
3989 znode_t
*zp
= VTOZ(vp
);
3990 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3992 u_offset_t off
, koff
;
3999 * If our blocksize is bigger than the page size, try to kluster
4000 * multiple pages so that we write a full block (thus avoiding
4001 * a read-modify-write).
4003 if (off
< zp
->z_size
&& zp
->z_blksz
> PAGESIZE
) {
4004 klen
= P2ROUNDUP((ulong_t
)zp
->z_blksz
, PAGESIZE
);
4005 koff
= ISP2(klen
) ? P2ALIGN(off
, (u_offset_t
)klen
) : 0;
4006 ASSERT(koff
<= zp
->z_size
);
4007 if (koff
+ klen
> zp
->z_size
)
4008 klen
= P2ROUNDUP(zp
->z_size
- koff
, (uint64_t)PAGESIZE
);
4009 pp
= pvn_write_kluster(vp
, pp
, &off
, &len
, koff
, klen
, flags
);
4011 ASSERT3U(btop(len
), ==, btopr(len
));
4014 * Can't push pages past end-of-file.
4016 if (off
>= zp
->z_size
) {
4017 /* ignore all pages */
4020 } else if (off
+ len
> zp
->z_size
) {
4021 int npages
= btopr(zp
->z_size
- off
);
4024 page_list_break(&pp
, &trunc
, npages
);
4025 /* ignore pages past end of file */
4027 pvn_write_done(trunc
, flags
);
4028 len
= zp
->z_size
- off
;
4031 if (zfs_owner_overquota(zfsvfs
, zp
, B_FALSE
) ||
4032 zfs_owner_overquota(zfsvfs
, zp
, B_TRUE
)) {
4037 tx
= dmu_tx_create(zfsvfs
->z_os
);
4038 dmu_tx_hold_write(tx
, zp
->z_id
, off
, len
);
4040 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4041 zfs_sa_upgrade_txholds(tx
, zp
);
4042 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
4044 if (err
== ERESTART
) {
4053 if (zp
->z_blksz
<= PAGESIZE
) {
4054 caddr_t va
= zfs_map_page(pp
, S_READ
);
4055 ASSERT3U(len
, <=, PAGESIZE
);
4056 dmu_write(zfsvfs
->z_os
, zp
->z_id
, off
, len
, va
, tx
);
4057 zfs_unmap_page(pp
, va
);
4059 err
= dmu_write_pages(zfsvfs
->z_os
, zp
->z_id
, off
, len
, pp
, tx
);
4063 uint64_t mtime
[2], ctime
[2];
4064 sa_bulk_attr_t bulk
[3];
4067 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
4069 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
4071 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
4073 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
4075 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, off
, len
, 0);
4080 pvn_write_done(pp
, (err
? B_ERROR
: 0) | flags
);
4090 * Copy the portion of the file indicated from pages into the file.
4091 * The pages are stored in a page list attached to the files vnode.
4093 * IN: vp - vnode of file to push page data to.
4094 * off - position in file to put data.
4095 * len - amount of data to write.
4096 * flags - flags to control the operation.
4097 * cr - credentials of caller.
4098 * ct - caller context.
4100 * RETURN: 0 if success
4101 * error code if failure
4104 * vp - ctime|mtime updated
4108 zfs_putpage(vnode_t
*vp
, offset_t off
, size_t len
, int flags
, cred_t
*cr
,
4109 caller_context_t
*ct
)
4111 znode_t
*zp
= VTOZ(vp
);
4112 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4124 * Align this request to the file block size in case we kluster.
4125 * XXX - this can result in pretty aggresive locking, which can
4126 * impact simultanious read/write access. One option might be
4127 * to break up long requests (len == 0) into block-by-block
4128 * operations to get narrower locking.
4130 blksz
= zp
->z_blksz
;
4132 io_off
= P2ALIGN_TYPED(off
, blksz
, u_offset_t
);
4135 if (len
> 0 && ISP2(blksz
))
4136 io_len
= P2ROUNDUP_TYPED(len
+ (off
- io_off
), blksz
, size_t);
4142 * Search the entire vp list for pages >= io_off.
4144 rl
= zfs_range_lock(zp
, io_off
, UINT64_MAX
, RL_WRITER
);
4145 error
= pvn_vplist_dirty(vp
, io_off
, zfs_putapage
, flags
, cr
);
4148 rl
= zfs_range_lock(zp
, io_off
, io_len
, RL_WRITER
);
4150 if (off
> zp
->z_size
) {
4151 /* past end of file */
4152 zfs_range_unlock(rl
);
4157 len
= MIN(io_len
, P2ROUNDUP(zp
->z_size
, PAGESIZE
) - io_off
);
4159 for (off
= io_off
; io_off
< off
+ len
; io_off
+= io_len
) {
4160 if ((flags
& B_INVAL
) || ((flags
& B_ASYNC
) == 0)) {
4161 pp
= page_lookup(vp
, io_off
,
4162 (flags
& (B_INVAL
| B_FREE
)) ? SE_EXCL
: SE_SHARED
);
4164 pp
= page_lookup_nowait(vp
, io_off
,
4165 (flags
& B_FREE
) ? SE_EXCL
: SE_SHARED
);
4168 if (pp
!= NULL
&& pvn_getdirty(pp
, flags
)) {
4172 * Found a dirty page to push
4174 err
= zfs_putapage(vp
, pp
, &io_off
, &io_len
, flags
, cr
);
4182 zfs_range_unlock(rl
);
4183 if ((flags
& B_ASYNC
) == 0 || zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4184 zil_commit(zfsvfs
->z_log
, UINT64_MAX
, zp
->z_id
);
4191 zfs_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
4193 znode_t
*zp
= VTOZ(vp
);
4194 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4197 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_READER
);
4198 if (zp
->z_sa_hdl
== NULL
) {
4200 * The fs has been unmounted, or we did a
4201 * suspend/resume and this file no longer exists.
4203 if (vn_has_cached_data(vp
)) {
4204 (void) pvn_vplist_dirty(vp
, 0, zfs_null_putapage
,
4208 mutex_enter(&zp
->z_lock
);
4209 mutex_enter(&vp
->v_lock
);
4210 ASSERT(vp
->v_count
== 1);
4212 mutex_exit(&vp
->v_lock
);
4213 mutex_exit(&zp
->z_lock
);
4214 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
4220 * Attempt to push any data in the page cache. If this fails
4221 * we will get kicked out later in zfs_zinactive().
4223 if (vn_has_cached_data(vp
)) {
4224 (void) pvn_vplist_dirty(vp
, 0, zfs_putapage
, B_INVAL
|B_ASYNC
,
4228 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4229 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
4231 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4232 zfs_sa_upgrade_txholds(tx
, zp
);
4233 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4237 mutex_enter(&zp
->z_lock
);
4238 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
),
4239 (void *)&zp
->z_atime
, sizeof (zp
->z_atime
), tx
);
4240 zp
->z_atime_dirty
= 0;
4241 mutex_exit(&zp
->z_lock
);
4247 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
4251 * Bounds-check the seek operation.
4253 * IN: vp - vnode seeking within
4254 * ooff - old file offset
4255 * noffp - pointer to new file offset
4256 * ct - caller context
4258 * RETURN: 0 if success
4259 * EINVAL if new offset invalid
4263 zfs_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
,
4264 caller_context_t
*ct
)
4266 if (vp
->v_type
== VDIR
)
4268 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4272 * Pre-filter the generic locking function to trap attempts to place
4273 * a mandatory lock on a memory mapped file.
4276 zfs_frlock(vnode_t
*vp
, int cmd
, flock64_t
*bfp
, int flag
, offset_t offset
,
4277 flk_callback_t
*flk_cbp
, cred_t
*cr
, caller_context_t
*ct
)
4279 znode_t
*zp
= VTOZ(vp
);
4280 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4286 * We are following the UFS semantics with respect to mapcnt
4287 * here: If we see that the file is mapped already, then we will
4288 * return an error, but we don't worry about races between this
4289 * function and zfs_map().
4291 if (zp
->z_mapcnt
> 0 && MANDMODE(zp
->z_mode
)) {
4296 return (fs_frlock(vp
, cmd
, bfp
, flag
, offset
, flk_cbp
, cr
, ct
));
4300 * If we can't find a page in the cache, we will create a new page
4301 * and fill it with file data. For efficiency, we may try to fill
4302 * multiple pages at once (klustering) to fill up the supplied page
4303 * list. Note that the pages to be filled are held with an exclusive
4304 * lock to prevent access by other threads while they are being filled.
4307 zfs_fillpage(vnode_t
*vp
, u_offset_t off
, struct seg
*seg
,
4308 caddr_t addr
, page_t
*pl
[], size_t plsz
, enum seg_rw rw
)
4310 znode_t
*zp
= VTOZ(vp
);
4311 page_t
*pp
, *cur_pp
;
4312 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
4313 u_offset_t io_off
, total
;
4317 if (plsz
== PAGESIZE
|| zp
->z_blksz
<= PAGESIZE
) {
4319 * We only have a single page, don't bother klustering
4323 pp
= page_create_va(vp
, io_off
, io_len
,
4324 PG_EXCL
| PG_WAIT
, seg
, addr
);
4327 * Try to find enough pages to fill the page list
4329 pp
= pvn_read_kluster(vp
, off
, seg
, addr
, &io_off
,
4330 &io_len
, off
, plsz
, 0);
4334 * The page already exists, nothing to do here.
4341 * Fill the pages in the kluster.
4344 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4347 ASSERT3U(io_off
, ==, cur_pp
->p_offset
);
4348 va
= zfs_map_page(cur_pp
, S_WRITE
);
4349 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4351 zfs_unmap_page(cur_pp
, va
);
4353 /* On error, toss the entire kluster */
4354 pvn_read_done(pp
, B_ERROR
);
4355 /* convert checksum errors into IO errors */
4360 cur_pp
= cur_pp
->p_next
;
4364 * Fill in the page list array from the kluster starting
4365 * from the desired offset `off'.
4366 * NOTE: the page list will always be null terminated.
4368 pvn_plist_init(pp
, pl
, plsz
, off
, io_len
, rw
);
4369 ASSERT(pl
== NULL
|| (*pl
)->p_offset
== off
);
4375 * Return pointers to the pages for the file region [off, off + len]
4376 * in the pl array. If plsz is greater than len, this function may
4377 * also return page pointers from after the specified region
4378 * (i.e. the region [off, off + plsz]). These additional pages are
4379 * only returned if they are already in the cache, or were created as
4380 * part of a klustered read.
4382 * IN: vp - vnode of file to get data from.
4383 * off - position in file to get data from.
4384 * len - amount of data to retrieve.
4385 * plsz - length of provided page list.
4386 * seg - segment to obtain pages for.
4387 * addr - virtual address of fault.
4388 * rw - mode of created pages.
4389 * cr - credentials of caller.
4390 * ct - caller context.
4392 * OUT: protp - protection mode of created pages.
4393 * pl - list of pages created.
4395 * RETURN: 0 if success
4396 * error code if failure
4399 * vp - atime updated
4403 zfs_getpage(vnode_t
*vp
, offset_t off
, size_t len
, uint_t
*protp
,
4404 page_t
*pl
[], size_t plsz
, struct seg
*seg
, caddr_t addr
,
4405 enum seg_rw rw
, cred_t
*cr
, caller_context_t
*ct
)
4407 znode_t
*zp
= VTOZ(vp
);
4408 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4412 /* we do our own caching, faultahead is unnecessary */
4415 else if (len
> plsz
)
4418 len
= P2ROUNDUP(len
, PAGESIZE
);
4419 ASSERT(plsz
>= len
);
4428 * Loop through the requested range [off, off + len) looking
4429 * for pages. If we don't find a page, we will need to create
4430 * a new page and fill it with data from the file.
4433 if (*pl
= page_lookup(vp
, off
, SE_SHARED
))
4435 else if (err
= zfs_fillpage(vp
, off
, seg
, addr
, pl
, plsz
, rw
))
4438 ASSERT3U((*pl
)->p_offset
, ==, off
);
4442 ASSERT3U(len
, >=, PAGESIZE
);
4445 ASSERT3U(plsz
, >=, PAGESIZE
);
4452 * Fill out the page array with any pages already in the cache.
4455 (*pl
++ = page_lookup_nowait(vp
, off
, SE_SHARED
))) {
4462 * Release any pages we have previously locked.
4467 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
4477 * Request a memory map for a section of a file. This code interacts
4478 * with common code and the VM system as follows:
4480 * common code calls mmap(), which ends up in smmap_common()
4482 * this calls VOP_MAP(), which takes you into (say) zfs
4484 * zfs_map() calls as_map(), passing segvn_create() as the callback
4486 * segvn_create() creates the new segment and calls VOP_ADDMAP()
4488 * zfs_addmap() updates z_mapcnt
4492 zfs_map(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t
*addrp
,
4493 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
4494 caller_context_t
*ct
)
4496 znode_t
*zp
= VTOZ(vp
);
4497 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4498 segvn_crargs_t vn_a
;
4504 if ((prot
& PROT_WRITE
) && (zp
->z_pflags
&
4505 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4510 if ((prot
& (PROT_READ
| PROT_EXEC
)) &&
4511 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4516 if (vp
->v_flag
& VNOMAP
) {
4521 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4526 if (vp
->v_type
!= VREG
) {
4532 * If file is locked, disallow mapping.
4534 if (MANDMODE(zp
->z_mode
) && vn_has_flocks(vp
)) {
4540 error
= choose_addr(as
, addrp
, len
, off
, ADDR_VACALIGN
, flags
);
4548 vn_a
.offset
= (u_offset_t
)off
;
4549 vn_a
.type
= flags
& MAP_TYPE
;
4551 vn_a
.maxprot
= maxprot
;
4554 vn_a
.flags
= flags
& ~MAP_TYPE
;
4556 vn_a
.lgrp_mem_policy_flags
= 0;
4558 error
= as_map(as
, *addrp
, len
, segvn_create
, &vn_a
);
4567 zfs_addmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
4568 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
4569 caller_context_t
*ct
)
4571 uint64_t pages
= btopr(len
);
4573 atomic_add_64(&VTOZ(vp
)->z_mapcnt
, pages
);
4578 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4579 * more accurate mtime for the associated file. Since we don't have a way of
4580 * detecting when the data was actually modified, we have to resort to
4581 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4582 * last page is pushed. The problem occurs when the msync() call is omitted,
4583 * which by far the most common case:
4591 * putpage() via fsflush
4593 * If we wait until fsflush to come along, we can have a modification time that
4594 * is some arbitrary point in the future. In order to prevent this in the
4595 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4600 zfs_delmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
4601 size_t len
, uint_t prot
, uint_t maxprot
, uint_t flags
, cred_t
*cr
,
4602 caller_context_t
*ct
)
4604 uint64_t pages
= btopr(len
);
4606 ASSERT3U(VTOZ(vp
)->z_mapcnt
, >=, pages
);
4607 atomic_add_64(&VTOZ(vp
)->z_mapcnt
, -pages
);
4609 if ((flags
& MAP_SHARED
) && (prot
& PROT_WRITE
) &&
4610 vn_has_cached_data(vp
))
4611 (void) VOP_PUTPAGE(vp
, off
, len
, B_ASYNC
, cr
, ct
);
4617 * Free or allocate space in a file. Currently, this function only
4618 * supports the `F_FREESP' command. However, this command is somewhat
4619 * misnamed, as its functionality includes the ability to allocate as
4620 * well as free space.
4622 * IN: vp - vnode of file to free data in.
4623 * cmd - action to take (only F_FREESP supported).
4624 * bfp - section of file to free/alloc.
4625 * flag - current file open mode flags.
4626 * offset - current file offset.
4627 * cr - credentials of caller [UNUSED].
4628 * ct - caller context.
4630 * RETURN: 0 if success
4631 * error code if failure
4634 * vp - ctime|mtime updated
4638 zfs_space(vnode_t
*vp
, int cmd
, flock64_t
*bfp
, int flag
,
4639 offset_t offset
, cred_t
*cr
, caller_context_t
*ct
)
4641 znode_t
*zp
= VTOZ(vp
);
4642 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4649 if (cmd
!= F_FREESP
) {
4654 if (error
= convoff(vp
, bfp
, 0, offset
)) {
4659 if (bfp
->l_len
< 0) {
4665 len
= bfp
->l_len
; /* 0 means from off to end of file */
4667 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4675 zfs_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
4677 znode_t
*zp
= VTOZ(vp
);
4678 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4681 uint64_t object
= zp
->z_id
;
4688 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
),
4689 &gen64
, sizeof (uint64_t))) != 0) {
4694 gen
= (uint32_t)gen64
;
4696 size
= (zfsvfs
->z_parent
!= zfsvfs
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4697 if (fidp
->fid_len
< size
) {
4698 fidp
->fid_len
= size
;
4703 zfid
= (zfid_short_t
*)fidp
;
4705 zfid
->zf_len
= size
;
4707 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4708 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4710 /* Must have a non-zero generation number to distinguish from .zfs */
4713 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4714 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4716 if (size
== LONG_FID_LEN
) {
4717 uint64_t objsetid
= dmu_objset_id(zfsvfs
->z_os
);
4720 zlfid
= (zfid_long_t
*)fidp
;
4722 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4723 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4725 /* XXX - this should be the generation number for the objset */
4726 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4727 zlfid
->zf_setgen
[i
] = 0;
4735 zfs_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
4736 caller_context_t
*ct
)
4748 case _PC_FILESIZEBITS
:
4752 case _PC_XATTR_EXISTS
:
4754 zfsvfs
= zp
->z_zfsvfs
;
4758 error
= zfs_dirent_lock(&dl
, zp
, "", &xzp
,
4759 ZXATTR
| ZEXISTS
| ZSHARED
, NULL
, NULL
);
4761 zfs_dirent_unlock(dl
);
4762 if (!zfs_dirempty(xzp
))
4765 } else if (error
== ENOENT
) {
4767 * If there aren't extended attributes, it's the
4768 * same as having zero of them.
4775 case _PC_SATTR_ENABLED
:
4776 case _PC_SATTR_EXISTS
:
4777 *valp
= vfs_has_feature(vp
->v_vfsp
, VFSFT_SYSATTR_VIEWS
) &&
4778 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
);
4781 case _PC_ACCESS_FILTERING
:
4782 *valp
= vfs_has_feature(vp
->v_vfsp
, VFSFT_ACCESS_FILTER
) &&
4786 case _PC_ACL_ENABLED
:
4787 *valp
= _ACL_ACE_ENABLED
;
4790 case _PC_MIN_HOLE_SIZE
:
4791 *valp
= (ulong_t
)SPA_MINBLOCKSIZE
;
4794 case _PC_TIMESTAMP_RESOLUTION
:
4795 /* nanosecond timestamp resolution */
4800 return (fs_pathconf(vp
, cmd
, valp
, cr
, ct
));
4806 zfs_getsecattr(vnode_t
*vp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
,
4807 caller_context_t
*ct
)
4809 znode_t
*zp
= VTOZ(vp
);
4810 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4812 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4816 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
4824 zfs_setsecattr(vnode_t
*vp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
,
4825 caller_context_t
*ct
)
4827 znode_t
*zp
= VTOZ(vp
);
4828 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4830 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4831 zilog_t
*zilog
= zfsvfs
->z_log
;
4836 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
4838 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4839 zil_commit(zilog
, UINT64_MAX
, 0);
4846 * Tunable, both must be a power of 2.
4848 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4849 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4850 * an arcbuf for a partial block read
4852 int zcr_blksz_min
= (1 << 10); /* 1K */
4853 int zcr_blksz_max
= (1 << 17); /* 128K */
4857 zfs_reqzcbuf(vnode_t
*vp
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
,
4858 caller_context_t
*ct
)
4860 znode_t
*zp
= VTOZ(vp
);
4861 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4862 int max_blksz
= zfsvfs
->z_max_blksz
;
4863 uio_t
*uio
= &xuio
->xu_uio
;
4864 ssize_t size
= uio
->uio_resid
;
4865 offset_t offset
= uio
->uio_loffset
;
4870 int preamble
, postamble
;
4872 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
4880 * Loan out an arc_buf for write if write size is bigger than
4881 * max_blksz, and the file's block size is also max_blksz.
4884 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
4889 * Caller requests buffers for write before knowing where the
4890 * write offset might be (e.g. NFS TCP write).
4895 preamble
= P2PHASE(offset
, blksz
);
4897 preamble
= blksz
- preamble
;
4902 postamble
= P2PHASE(size
, blksz
);
4905 fullblk
= size
/ blksz
;
4906 (void) dmu_xuio_init(xuio
,
4907 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
4908 DTRACE_PROBE3(zfs_reqzcbuf_align
, int, preamble
,
4909 int, postamble
, int,
4910 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
4913 * Have to fix iov base/len for partial buffers. They
4914 * currently represent full arc_buf's.
4917 /* data begins in the middle of the arc_buf */
4918 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4921 (void) dmu_xuio_add(xuio
, abuf
,
4922 blksz
- preamble
, preamble
);
4925 for (i
= 0; i
< fullblk
; i
++) {
4926 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4929 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
4933 /* data ends in the middle of the arc_buf */
4934 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4937 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
4942 * Loan out an arc_buf for read if the read size is larger than
4943 * the current file block size. Block alignment is not
4944 * considered. Partial arc_buf will be loaned out for read.
4946 blksz
= zp
->z_blksz
;
4947 if (blksz
< zcr_blksz_min
)
4948 blksz
= zcr_blksz_min
;
4949 if (blksz
> zcr_blksz_max
)
4950 blksz
= zcr_blksz_max
;
4951 /* avoid potential complexity of dealing with it */
4952 if (blksz
> max_blksz
) {
4957 maxsize
= zp
->z_size
- uio
->uio_loffset
;
4961 if (size
< blksz
|| vn_has_cached_data(vp
)) {
4971 uio
->uio_extflg
= UIO_XUIO
;
4972 XUIO_XUZC_RW(xuio
) = ioflag
;
4979 zfs_retzcbuf(vnode_t
*vp
, xuio_t
*xuio
, cred_t
*cr
, caller_context_t
*ct
)
4983 int ioflag
= XUIO_XUZC_RW(xuio
);
4985 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
4987 i
= dmu_xuio_cnt(xuio
);
4989 abuf
= dmu_xuio_arcbuf(xuio
, i
);
4991 * if abuf == NULL, it must be a write buffer
4992 * that has been returned in zfs_write().
4995 dmu_return_arcbuf(abuf
);
4996 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
4999 dmu_xuio_fini(xuio
);
5004 * Predeclare these here so that the compiler assumes that
5005 * this is an "old style" function declaration that does
5006 * not include arguments => we won't get type mismatch errors
5007 * in the initializations that follow.
5009 static int zfs_inval();
5010 static int zfs_isdir();
5024 * Directory vnode operations template
5026 vnodeops_t
*zfs_dvnodeops
;
5027 const fs_operation_def_t zfs_dvnodeops_template
[] = {
5028 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
5029 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
5030 VOPNAME_READ
, { .error
= zfs_isdir
},
5031 VOPNAME_WRITE
, { .error
= zfs_isdir
},
5032 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
5033 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
5034 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
5035 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
5036 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
5037 VOPNAME_CREATE
, { .vop_create
= zfs_create
},
5038 VOPNAME_REMOVE
, { .vop_remove
= zfs_remove
},
5039 VOPNAME_LINK
, { .vop_link
= zfs_link
},
5040 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
5041 VOPNAME_MKDIR
, { .vop_mkdir
= zfs_mkdir
},
5042 VOPNAME_RMDIR
, { .vop_rmdir
= zfs_rmdir
},
5043 VOPNAME_READDIR
, { .vop_readdir
= zfs_readdir
},
5044 VOPNAME_SYMLINK
, { .vop_symlink
= zfs_symlink
},
5045 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
5046 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5047 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
5048 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
5049 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
5050 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
5051 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
5052 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
5057 * Regular file vnode operations template
5059 vnodeops_t
*zfs_fvnodeops
;
5060 const fs_operation_def_t zfs_fvnodeops_template
[] = {
5061 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
5062 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
5063 VOPNAME_READ
, { .vop_read
= zfs_read
},
5064 VOPNAME_WRITE
, { .vop_write
= zfs_write
},
5065 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
5066 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
5067 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
5068 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
5069 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
5070 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
5071 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
5072 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5073 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
5074 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
5075 VOPNAME_FRLOCK
, { .vop_frlock
= zfs_frlock
},
5076 VOPNAME_SPACE
, { .vop_space
= zfs_space
},
5077 VOPNAME_GETPAGE
, { .vop_getpage
= zfs_getpage
},
5078 VOPNAME_PUTPAGE
, { .vop_putpage
= zfs_putpage
},
5079 VOPNAME_MAP
, { .vop_map
= zfs_map
},
5080 VOPNAME_ADDMAP
, { .vop_addmap
= zfs_addmap
},
5081 VOPNAME_DELMAP
, { .vop_delmap
= zfs_delmap
},
5082 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
5083 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
5084 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
5085 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
5086 VOPNAME_REQZCBUF
, { .vop_reqzcbuf
= zfs_reqzcbuf
},
5087 VOPNAME_RETZCBUF
, { .vop_retzcbuf
= zfs_retzcbuf
},
5092 * Symbolic link vnode operations template
5094 vnodeops_t
*zfs_symvnodeops
;
5095 const fs_operation_def_t zfs_symvnodeops_template
[] = {
5096 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
5097 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
5098 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
5099 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
5100 VOPNAME_READLINK
, { .vop_readlink
= zfs_readlink
},
5101 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5102 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
5103 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
5104 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
5109 * special share hidden files vnode operations template
5111 vnodeops_t
*zfs_sharevnodeops
;
5112 const fs_operation_def_t zfs_sharevnodeops_template
[] = {
5113 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
5114 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
5115 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5116 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
5117 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
5118 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
5119 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
5120 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
5125 * Extended attribute directory vnode operations template
5126 * This template is identical to the directory vnodes
5127 * operation template except for restricted operations:
5130 * Note that there are other restrictions embedded in:
5131 * zfs_create() - restrict type to VREG
5132 * zfs_link() - no links into/out of attribute space
5133 * zfs_rename() - no moves into/out of attribute space
5135 vnodeops_t
*zfs_xdvnodeops
;
5136 const fs_operation_def_t zfs_xdvnodeops_template
[] = {
5137 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
5138 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
5139 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
5140 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
5141 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
5142 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
5143 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
5144 VOPNAME_CREATE
, { .vop_create
= zfs_create
},
5145 VOPNAME_REMOVE
, { .vop_remove
= zfs_remove
},
5146 VOPNAME_LINK
, { .vop_link
= zfs_link
},
5147 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
5148 VOPNAME_MKDIR
, { .error
= zfs_inval
},
5149 VOPNAME_RMDIR
, { .vop_rmdir
= zfs_rmdir
},
5150 VOPNAME_READDIR
, { .vop_readdir
= zfs_readdir
},
5151 VOPNAME_SYMLINK
, { .error
= zfs_inval
},
5152 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
5153 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5154 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
5155 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
5156 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
5157 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
5158 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
5159 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
5164 * Error vnode operations template
5166 vnodeops_t
*zfs_evnodeops
;
5167 const fs_operation_def_t zfs_evnodeops_template
[] = {
5168 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5169 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},