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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
26 * Copyright 2015 Joyent, Inc.
27 * Copyright 2017 Nexenta Systems, Inc.
30 /* Portions Copyright 2007 Jeremy Teo */
31 /* Portions Copyright 2010 Robert Milkowski */
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysmacros.h>
38 #include <sys/resource.h>
40 #include <sys/vfs_opreg.h>
41 #include <sys/vnode.h>
45 #include <sys/taskq.h>
47 #include <sys/vmsystm.h>
48 #include <sys/atomic.h>
50 #include <vm/seg_vn.h>
54 #include <vm/seg_kpm.h>
56 #include <sys/pathname.h>
57 #include <sys/cmn_err.h>
58 #include <sys/errno.h>
59 #include <sys/unistd.h>
60 #include <sys/zfs_dir.h>
61 #include <sys/zfs_acl.h>
62 #include <sys/zfs_ioctl.h>
63 #include <sys/fs/zfs.h>
65 #include <sys/dmu_objset.h>
71 #include <sys/dirent.h>
72 #include <sys/policy.h>
73 #include <sys/sunddi.h>
74 #include <sys/filio.h>
76 #include "fs/fs_subr.h"
77 #include <sys/zfs_ctldir.h>
78 #include <sys/zfs_fuid.h>
79 #include <sys/zfs_sa.h>
81 #include <sys/zfs_rlock.h>
82 #include <sys/extdirent.h>
83 #include <sys/kidmap.h>
91 * Each vnode op performs some logical unit of work. To do this, the ZPL must
92 * properly lock its in-core state, create a DMU transaction, do the work,
93 * record this work in the intent log (ZIL), commit the DMU transaction,
94 * and wait for the intent log to commit if it is a synchronous operation.
95 * Moreover, the vnode ops must work in both normal and log replay context.
96 * The ordering of events is important to avoid deadlocks and references
97 * to freed memory. The example below illustrates the following Big Rules:
99 * (1) A check must be made in each zfs thread for a mounted file system.
100 * This is done avoiding races using ZFS_ENTER(zfsvfs).
101 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
102 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
103 * can return EIO from the calling function.
105 * (2) VN_RELE() should always be the last thing except for zil_commit()
106 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
107 * First, if it's the last reference, the vnode/znode
108 * can be freed, so the zp may point to freed memory. Second, the last
109 * reference will call zfs_zinactive(), which may induce a lot of work --
110 * pushing cached pages (which acquires range locks) and syncing out
111 * cached atime changes. Third, zfs_zinactive() may require a new tx,
112 * which could deadlock the system if you were already holding one.
113 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
115 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
116 * as they can span dmu_tx_assign() calls.
118 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
119 * dmu_tx_assign(). This is critical because we don't want to block
120 * while holding locks.
122 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
123 * reduces lock contention and CPU usage when we must wait (note that if
124 * throughput is constrained by the storage, nearly every transaction
127 * Note, in particular, that if a lock is sometimes acquired before
128 * the tx assigns, and sometimes after (e.g. z_lock), then failing
129 * to use a non-blocking assign can deadlock the system. The scenario:
131 * Thread A has grabbed a lock before calling dmu_tx_assign().
132 * Thread B is in an already-assigned tx, and blocks for this lock.
133 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
134 * forever, because the previous txg can't quiesce until B's tx commits.
136 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
137 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
138 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
139 * to indicate that this operation has already called dmu_tx_wait().
140 * This will ensure that we don't retry forever, waiting a short bit
143 * (5) If the operation succeeded, generate the intent log entry for it
144 * before dropping locks. This ensures that the ordering of events
145 * in the intent log matches the order in which they actually occurred.
146 * During ZIL replay the zfs_log_* functions will update the sequence
147 * number to indicate the zil transaction has replayed.
149 * (6) At the end of each vnode op, the DMU tx must always commit,
150 * regardless of whether there were any errors.
152 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
153 * to ensure that synchronous semantics are provided when necessary.
155 * In general, this is how things should be ordered in each vnode op:
157 * ZFS_ENTER(zfsvfs); // exit if unmounted
159 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
160 * rw_enter(...); // grab any other locks you need
161 * tx = dmu_tx_create(...); // get DMU tx
162 * dmu_tx_hold_*(); // hold each object you might modify
163 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
165 * rw_exit(...); // drop locks
166 * zfs_dirent_unlock(dl); // unlock directory entry
167 * VN_RELE(...); // release held vnodes
168 * if (error == ERESTART) {
174 * dmu_tx_abort(tx); // abort DMU tx
175 * ZFS_EXIT(zfsvfs); // finished in zfs
176 * return (error); // really out of space
178 * error = do_real_work(); // do whatever this VOP does
180 * zfs_log_*(...); // on success, make ZIL entry
181 * dmu_tx_commit(tx); // commit DMU tx -- error or not
182 * rw_exit(...); // drop locks
183 * zfs_dirent_unlock(dl); // unlock directory entry
184 * VN_RELE(...); // release held vnodes
185 * zil_commit(zilog, foid); // synchronous when necessary
186 * ZFS_EXIT(zfsvfs); // finished in zfs
187 * return (error); // done, report error
192 zfs_open(vnode_t
**vpp
, int flag
, cred_t
*cr
, caller_context_t
*ct
)
194 znode_t
*zp
= VTOZ(*vpp
);
195 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
200 if ((flag
& FWRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
201 ((flag
& FAPPEND
) == 0)) {
203 return (SET_ERROR(EPERM
));
206 if (!zfs_has_ctldir(zp
) && zp
->z_zfsvfs
->z_vscan
&&
207 ZTOV(zp
)->v_type
== VREG
&&
208 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0) {
209 if (fs_vscan(*vpp
, cr
, 0) != 0) {
211 return (SET_ERROR(EACCES
));
215 /* Keep a count of the synchronous opens in the znode */
216 if (flag
& (FSYNC
| FDSYNC
))
217 atomic_inc_32(&zp
->z_sync_cnt
);
225 zfs_close(vnode_t
*vp
, int flag
, int count
, offset_t offset
, cred_t
*cr
,
226 caller_context_t
*ct
)
228 znode_t
*zp
= VTOZ(vp
);
229 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
232 * Clean up any locks held by this process on the vp.
234 cleanlocks(vp
, ddi_get_pid(), 0);
235 cleanshares(vp
, ddi_get_pid());
240 /* Decrement the synchronous opens in the znode */
241 if ((flag
& (FSYNC
| FDSYNC
)) && (count
== 1))
242 atomic_dec_32(&zp
->z_sync_cnt
);
244 if (!zfs_has_ctldir(zp
) && zp
->z_zfsvfs
->z_vscan
&&
245 ZTOV(zp
)->v_type
== VREG
&&
246 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0)
247 VERIFY(fs_vscan(vp
, cr
, 1) == 0);
254 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
255 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
258 zfs_holey(vnode_t
*vp
, int cmd
, offset_t
*off
)
260 znode_t
*zp
= VTOZ(vp
);
261 uint64_t noff
= (uint64_t)*off
; /* new offset */
266 file_sz
= zp
->z_size
;
267 if (noff
>= file_sz
) {
268 return (SET_ERROR(ENXIO
));
271 if (cmd
== _FIO_SEEK_HOLE
)
276 error
= dmu_offset_next(zp
->z_zfsvfs
->z_os
, zp
->z_id
, hole
, &noff
);
279 return (SET_ERROR(ENXIO
));
282 * We could find a hole that begins after the logical end-of-file,
283 * because dmu_offset_next() only works on whole blocks. If the
284 * EOF falls mid-block, then indicate that the "virtual hole"
285 * at the end of the file begins at the logical EOF, rather than
286 * at the end of the last block.
288 if (noff
> file_sz
) {
301 zfs_ioctl(vnode_t
*vp
, int com
, intptr_t data
, int flag
, cred_t
*cred
,
302 int *rvalp
, caller_context_t
*ct
)
306 dmu_object_info_t doi
;
314 return (zfs_sync(vp
->v_vfsp
, 0, cred
));
317 * The following two ioctls are used by bfu. Faking out,
318 * necessary to avoid bfu errors.
330 if (ddi_copyin((void *)data
, &off
, sizeof (off
), flag
))
331 return (SET_ERROR(EFAULT
));
334 zfsvfs
= zp
->z_zfsvfs
;
338 /* offset parameter is in/out */
339 error
= zfs_holey(vp
, com
, &off
);
343 if (ddi_copyout(&off
, (void *)data
, sizeof (off
), flag
))
344 return (SET_ERROR(EFAULT
));
347 case _FIO_COUNT_FILLED
:
350 * _FIO_COUNT_FILLED adds a new ioctl command which
351 * exposes the number of filled blocks in a
355 zfsvfs
= zp
->z_zfsvfs
;
360 * Wait for all dirty blocks for this object
361 * to get synced out to disk, and the DMU info
364 error
= dmu_object_wait_synced(zfsvfs
->z_os
, zp
->z_id
);
371 * Retrieve fill count from DMU object.
373 error
= dmu_object_info(zfsvfs
->z_os
, zp
->z_id
, &doi
);
379 ndata
= doi
.doi_fill_count
;
382 if (ddi_copyout(&ndata
, (void *)data
, sizeof (ndata
), flag
))
383 return (SET_ERROR(EFAULT
));
387 return (SET_ERROR(ENOTTY
));
391 * Utility functions to map and unmap a single physical page. These
392 * are used to manage the mappable copies of ZFS file data, and therefore
393 * do not update ref/mod bits.
396 zfs_map_page(page_t
*pp
, enum seg_rw rw
)
399 return (hat_kpm_mapin(pp
, 0));
400 ASSERT(rw
== S_READ
|| rw
== S_WRITE
);
401 return (ppmapin(pp
, PROT_READ
| ((rw
== S_WRITE
) ? PROT_WRITE
: 0),
406 zfs_unmap_page(page_t
*pp
, caddr_t addr
)
409 hat_kpm_mapout(pp
, 0, addr
);
416 * When a file is memory mapped, we must keep the IO data synchronized
417 * between the DMU cache and the memory mapped pages. What this means:
419 * On Write: If we find a memory mapped page, we write to *both*
420 * the page and the dmu buffer.
423 update_pages(vnode_t
*vp
, int64_t start
, int len
, objset_t
*os
, uint64_t oid
)
427 off
= start
& PAGEOFFSET
;
428 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
430 uint64_t nbytes
= MIN(PAGESIZE
- off
, len
);
432 if (pp
= page_lookup(vp
, start
, SE_SHARED
)) {
435 va
= zfs_map_page(pp
, S_WRITE
);
436 (void) dmu_read(os
, oid
, start
+off
, nbytes
, va
+off
,
438 zfs_unmap_page(pp
, va
);
447 * When a file is memory mapped, we must keep the IO data synchronized
448 * between the DMU cache and the memory mapped pages. What this means:
450 * On Read: We "read" preferentially from memory mapped pages,
451 * else we default from the dmu buffer.
453 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
454 * the file is memory mapped.
457 mappedread(vnode_t
*vp
, int nbytes
, uio_t
*uio
)
459 znode_t
*zp
= VTOZ(vp
);
464 start
= uio
->uio_loffset
;
465 off
= start
& PAGEOFFSET
;
466 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
468 uint64_t bytes
= MIN(PAGESIZE
- off
, len
);
470 if (pp
= page_lookup(vp
, start
, SE_SHARED
)) {
473 va
= zfs_map_page(pp
, S_READ
);
474 error
= uiomove(va
+ off
, bytes
, UIO_READ
, uio
);
475 zfs_unmap_page(pp
, va
);
478 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
489 offset_t zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
492 * Read bytes from specified file into supplied buffer.
494 * IN: vp - vnode of file to be read from.
495 * uio - structure supplying read location, range info,
497 * ioflag - SYNC flags; used to provide FRSYNC semantics.
498 * cr - credentials of caller.
499 * ct - caller context
501 * OUT: uio - updated offset and range, buffer filled.
503 * RETURN: 0 on success, error code on failure.
506 * vp - atime updated if byte count > 0
510 zfs_read(vnode_t
*vp
, uio_t
*uio
, int ioflag
, cred_t
*cr
, caller_context_t
*ct
)
512 znode_t
*zp
= VTOZ(vp
);
513 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
522 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
524 return (SET_ERROR(EACCES
));
528 * Validate file offset
530 if (uio
->uio_loffset
< (offset_t
)0) {
532 return (SET_ERROR(EINVAL
));
536 * Fasttrack empty reads
538 if (uio
->uio_resid
== 0) {
544 * Check for mandatory locks
546 if (MANDMODE(zp
->z_mode
)) {
547 if (error
= chklock(vp
, FREAD
,
548 uio
->uio_loffset
, uio
->uio_resid
, uio
->uio_fmode
, ct
)) {
555 * If we're in FRSYNC mode, sync out this znode before reading it.
557 if (ioflag
& FRSYNC
|| zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
558 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
561 * Lock the range against changes.
563 rl
= zfs_range_lock(zp
, uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
566 * If we are reading past end-of-file we can skip
567 * to the end; but we might still need to set atime.
569 if (uio
->uio_loffset
>= zp
->z_size
) {
574 ASSERT(uio
->uio_loffset
< zp
->z_size
);
575 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
577 if ((uio
->uio_extflg
== UIO_XUIO
) &&
578 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
580 int blksz
= zp
->z_blksz
;
581 uint64_t offset
= uio
->uio_loffset
;
583 xuio
= (xuio_t
*)uio
;
585 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
588 ASSERT(offset
+ n
<= blksz
);
591 (void) dmu_xuio_init(xuio
, nblk
);
593 if (vn_has_cached_data(vp
)) {
595 * For simplicity, we always allocate a full buffer
596 * even if we only expect to read a portion of a block.
598 while (--nblk
>= 0) {
599 (void) dmu_xuio_add(xuio
,
600 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
607 nbytes
= MIN(n
, zfs_read_chunk_size
-
608 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
610 if (vn_has_cached_data(vp
)) {
611 error
= mappedread(vp
, nbytes
, uio
);
613 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
617 /* convert checksum errors into IO errors */
619 error
= SET_ERROR(EIO
);
626 zfs_range_unlock(rl
);
628 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
634 * Write the bytes to a file.
636 * IN: vp - vnode of file to be written to.
637 * uio - structure supplying write location, range info,
639 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
640 * set if in append mode.
641 * cr - credentials of caller.
642 * ct - caller context (NFS/CIFS fem monitor only)
644 * OUT: uio - updated offset and range.
646 * RETURN: 0 on success, error code on failure.
649 * vp - ctime|mtime updated if byte count > 0
654 zfs_write(vnode_t
*vp
, uio_t
*uio
, int ioflag
, cred_t
*cr
, caller_context_t
*ct
)
656 znode_t
*zp
= VTOZ(vp
);
657 rlim64_t limit
= uio
->uio_llimit
;
658 ssize_t start_resid
= uio
->uio_resid
;
662 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
667 int max_blksz
= zfsvfs
->z_max_blksz
;
670 iovec_t
*aiov
= NULL
;
673 int iovcnt
= uio
->uio_iovcnt
;
674 iovec_t
*iovp
= uio
->uio_iov
;
677 sa_bulk_attr_t bulk
[4];
678 uint64_t mtime
[2], ctime
[2];
681 * Fasttrack empty write
687 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
693 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
694 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
695 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
697 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
701 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
702 * callers might not be able to detect properly that we are read-only,
703 * so check it explicitly here.
705 if (zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) {
707 return (SET_ERROR(EROFS
));
711 * If immutable or not appending then return EPERM.
712 * Intentionally allow ZFS_READONLY through here.
713 * See zfs_zaccess_common()
715 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) ||
716 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
717 (uio
->uio_loffset
< zp
->z_size
))) {
719 return (SET_ERROR(EPERM
));
722 zilog
= zfsvfs
->z_log
;
725 * Validate file offset
727 woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
730 return (SET_ERROR(EINVAL
));
734 * Check for mandatory locks before calling zfs_range_lock()
735 * in order to prevent a deadlock with locks set via fcntl().
737 if (MANDMODE((mode_t
)zp
->z_mode
) &&
738 (error
= chklock(vp
, FWRITE
, woff
, n
, uio
->uio_fmode
, ct
)) != 0) {
744 * Pre-fault the pages to ensure slow (eg NFS) pages
746 * Skip this if uio contains loaned arc_buf.
748 if ((uio
->uio_extflg
== UIO_XUIO
) &&
749 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
750 xuio
= (xuio_t
*)uio
;
752 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
755 * If in append mode, set the io offset pointer to eof.
757 if (ioflag
& FAPPEND
) {
759 * Obtain an appending range lock to guarantee file append
760 * semantics. We reset the write offset once we have the lock.
762 rl
= zfs_range_lock(zp
, 0, n
, RL_APPEND
);
764 if (rl
->r_len
== UINT64_MAX
) {
766 * We overlocked the file because this write will cause
767 * the file block size to increase.
768 * Note that zp_size cannot change with this lock held.
772 uio
->uio_loffset
= woff
;
775 * Note that if the file block size will change as a result of
776 * this write, then this range lock will lock the entire file
777 * so that we can re-write the block safely.
779 rl
= zfs_range_lock(zp
, woff
, n
, RL_WRITER
);
783 zfs_range_unlock(rl
);
785 return (SET_ERROR(EFBIG
));
788 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
791 /* Will this write extend the file length? */
792 write_eof
= (woff
+ n
> zp
->z_size
);
794 end_size
= MAX(zp
->z_size
, woff
+ n
);
797 * Write the file in reasonable size chunks. Each chunk is written
798 * in a separate transaction; this keeps the intent log records small
799 * and allows us to do more fine-grained space accounting.
803 woff
= uio
->uio_loffset
;
804 if (zfs_owner_overquota(zfsvfs
, zp
, B_FALSE
) ||
805 zfs_owner_overquota(zfsvfs
, zp
, B_TRUE
)) {
807 dmu_return_arcbuf(abuf
);
808 error
= SET_ERROR(EDQUOT
);
812 if (xuio
&& abuf
== NULL
) {
813 ASSERT(i_iov
< iovcnt
);
815 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
816 dmu_xuio_clear(xuio
, i_iov
);
817 DTRACE_PROBE3(zfs_cp_write
, int, i_iov
,
818 iovec_t
*, aiov
, arc_buf_t
*, abuf
);
819 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
820 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
821 aiov
->iov_len
== arc_buf_size(abuf
)));
823 } else if (abuf
== NULL
&& n
>= max_blksz
&&
824 woff
>= zp
->z_size
&&
825 P2PHASE(woff
, max_blksz
) == 0 &&
826 zp
->z_blksz
== max_blksz
) {
828 * This write covers a full block. "Borrow" a buffer
829 * from the dmu so that we can fill it before we enter
830 * a transaction. This avoids the possibility of
831 * holding up the transaction if the data copy hangs
832 * up on a pagefault (e.g., from an NFS server mapping).
836 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
838 ASSERT(abuf
!= NULL
);
839 ASSERT(arc_buf_size(abuf
) == max_blksz
);
840 if (error
= uiocopy(abuf
->b_data
, max_blksz
,
841 UIO_WRITE
, uio
, &cbytes
)) {
842 dmu_return_arcbuf(abuf
);
845 ASSERT(cbytes
== max_blksz
);
849 * Start a transaction.
851 tx
= dmu_tx_create(zfsvfs
->z_os
);
852 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
853 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
854 zfs_sa_upgrade_txholds(tx
, zp
);
855 error
= dmu_tx_assign(tx
, TXG_WAIT
);
859 dmu_return_arcbuf(abuf
);
864 * If zfs_range_lock() over-locked we grow the blocksize
865 * and then reduce the lock range. This will only happen
866 * on the first iteration since zfs_range_reduce() will
867 * shrink down r_len to the appropriate size.
869 if (rl
->r_len
== UINT64_MAX
) {
872 if (zp
->z_blksz
> max_blksz
) {
874 * File's blocksize is already larger than the
875 * "recordsize" property. Only let it grow to
876 * the next power of 2.
878 ASSERT(!ISP2(zp
->z_blksz
));
879 new_blksz
= MIN(end_size
,
880 1 << highbit64(zp
->z_blksz
));
882 new_blksz
= MIN(end_size
, max_blksz
);
884 zfs_grow_blocksize(zp
, new_blksz
, tx
);
885 zfs_range_reduce(rl
, woff
, n
);
889 * XXX - should we really limit each write to z_max_blksz?
890 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
892 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
895 tx_bytes
= uio
->uio_resid
;
896 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
898 tx_bytes
-= uio
->uio_resid
;
901 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
903 * If this is not a full block write, but we are
904 * extending the file past EOF and this data starts
905 * block-aligned, use assign_arcbuf(). Otherwise,
906 * write via dmu_write().
908 if (tx_bytes
< max_blksz
&& (!write_eof
||
909 aiov
->iov_base
!= abuf
->b_data
)) {
911 dmu_write(zfsvfs
->z_os
, zp
->z_id
, woff
,
912 aiov
->iov_len
, aiov
->iov_base
, tx
);
913 dmu_return_arcbuf(abuf
);
914 xuio_stat_wbuf_copied();
916 ASSERT(xuio
|| tx_bytes
== max_blksz
);
917 dmu_assign_arcbuf(sa_get_db(zp
->z_sa_hdl
),
920 ASSERT(tx_bytes
<= uio
->uio_resid
);
921 uioskip(uio
, tx_bytes
);
923 if (tx_bytes
&& vn_has_cached_data(vp
)) {
924 update_pages(vp
, woff
,
925 tx_bytes
, zfsvfs
->z_os
, zp
->z_id
);
929 * If we made no progress, we're done. If we made even
930 * partial progress, update the znode and ZIL accordingly.
933 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
934 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
941 * Clear Set-UID/Set-GID bits on successful write if not
942 * privileged and at least one of the excute bits is set.
944 * It would be nice to to this after all writes have
945 * been done, but that would still expose the ISUID/ISGID
946 * to another app after the partial write is committed.
948 * Note: we don't call zfs_fuid_map_id() here because
949 * user 0 is not an ephemeral uid.
951 mutex_enter(&zp
->z_acl_lock
);
952 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
953 (S_IXUSR
>> 6))) != 0 &&
954 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
955 secpolicy_vnode_setid_retain(cr
,
956 (zp
->z_mode
& S_ISUID
) != 0 && zp
->z_uid
== 0) != 0) {
958 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
959 newmode
= zp
->z_mode
;
960 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zfsvfs
),
961 (void *)&newmode
, sizeof (uint64_t), tx
);
963 mutex_exit(&zp
->z_acl_lock
);
965 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
969 * Update the file size (zp_size) if it has changed;
970 * account for possible concurrent updates.
972 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
973 (void) atomic_cas_64(&zp
->z_size
, end_size
,
978 * If we are replaying and eof is non zero then force
979 * the file size to the specified eof. Note, there's no
980 * concurrency during replay.
982 if (zfsvfs
->z_replay
&& zfsvfs
->z_replay_eof
!= 0)
983 zp
->z_size
= zfsvfs
->z_replay_eof
;
985 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
987 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
);
992 ASSERT(tx_bytes
== nbytes
);
996 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
999 zfs_range_unlock(rl
);
1002 * If we're in replay mode, or we made no progress, return error.
1003 * Otherwise, it's at least a partial write, so it's successful.
1005 if (zfsvfs
->z_replay
|| uio
->uio_resid
== start_resid
) {
1010 if (ioflag
& (FSYNC
| FDSYNC
) ||
1011 zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1012 zil_commit(zilog
, zp
->z_id
);
1019 zfs_get_done(zgd_t
*zgd
, int error
)
1021 znode_t
*zp
= zgd
->zgd_private
;
1022 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
1025 dmu_buf_rele(zgd
->zgd_db
, zgd
);
1027 zfs_range_unlock(zgd
->zgd_rl
);
1030 * Release the vnode asynchronously as we currently have the
1031 * txg stopped from syncing.
1033 VN_RELE_ASYNC(ZTOV(zp
), dsl_pool_vnrele_taskq(dmu_objset_pool(os
)));
1035 if (error
== 0 && zgd
->zgd_bp
)
1036 zil_lwb_add_block(zgd
->zgd_lwb
, zgd
->zgd_bp
);
1038 kmem_free(zgd
, sizeof (zgd_t
));
1042 static int zil_fault_io
= 0;
1046 * Get data to generate a TX_WRITE intent log record.
1049 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
, zio_t
*zio
)
1051 zfsvfs_t
*zfsvfs
= arg
;
1052 objset_t
*os
= zfsvfs
->z_os
;
1054 uint64_t object
= lr
->lr_foid
;
1055 uint64_t offset
= lr
->lr_offset
;
1056 uint64_t size
= lr
->lr_length
;
1061 ASSERT3P(lwb
, !=, NULL
);
1062 ASSERT3P(zio
, !=, NULL
);
1063 ASSERT3U(size
, !=, 0);
1066 * Nothing to do if the file has been removed
1068 if (zfs_zget(zfsvfs
, object
, &zp
) != 0)
1069 return (SET_ERROR(ENOENT
));
1070 if (zp
->z_unlinked
) {
1072 * Release the vnode asynchronously as we currently have the
1073 * txg stopped from syncing.
1075 VN_RELE_ASYNC(ZTOV(zp
),
1076 dsl_pool_vnrele_taskq(dmu_objset_pool(os
)));
1077 return (SET_ERROR(ENOENT
));
1080 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
1082 zgd
->zgd_private
= zp
;
1085 * Write records come in two flavors: immediate and indirect.
1086 * For small writes it's cheaper to store the data with the
1087 * log record (immediate); for large writes it's cheaper to
1088 * sync the data and get a pointer to it (indirect) so that
1089 * we don't have to write the data twice.
1091 if (buf
!= NULL
) { /* immediate write */
1092 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
, RL_READER
);
1093 /* test for truncation needs to be done while range locked */
1094 if (offset
>= zp
->z_size
) {
1095 error
= SET_ERROR(ENOENT
);
1097 error
= dmu_read(os
, object
, offset
, size
, buf
,
1098 DMU_READ_NO_PREFETCH
);
1100 ASSERT(error
== 0 || error
== ENOENT
);
1101 } else { /* indirect write */
1103 * Have to lock the whole block to ensure when it's
1104 * written out and its checksum is being calculated
1105 * that no one can change the data. We need to re-check
1106 * blocksize after we get the lock in case it's changed!
1111 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
1113 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
,
1115 if (zp
->z_blksz
== size
)
1118 zfs_range_unlock(zgd
->zgd_rl
);
1120 /* test for truncation needs to be done while range locked */
1121 if (lr
->lr_offset
>= zp
->z_size
)
1122 error
= SET_ERROR(ENOENT
);
1125 error
= SET_ERROR(EIO
);
1130 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1131 DMU_READ_NO_PREFETCH
);
1134 blkptr_t
*bp
= &lr
->lr_blkptr
;
1139 ASSERT(db
->db_offset
== offset
);
1140 ASSERT(db
->db_size
== size
);
1142 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1144 ASSERT(error
|| lr
->lr_length
<= size
);
1147 * On success, we need to wait for the write I/O
1148 * initiated by dmu_sync() to complete before we can
1149 * release this dbuf. We will finish everything up
1150 * in the zfs_get_done() callback.
1155 if (error
== EALREADY
) {
1156 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
1162 zfs_get_done(zgd
, error
);
1169 zfs_access(vnode_t
*vp
, int mode
, int flag
, cred_t
*cr
,
1170 caller_context_t
*ct
)
1172 znode_t
*zp
= VTOZ(vp
);
1173 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1179 if (flag
& V_ACE_MASK
)
1180 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
1182 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
1189 * If vnode is for a device return a specfs vnode instead.
1192 specvp_check(vnode_t
**vpp
, cred_t
*cr
)
1196 if (IS_DEVVP(*vpp
)) {
1199 svp
= specvp(*vpp
, (*vpp
)->v_rdev
, (*vpp
)->v_type
, cr
);
1202 error
= SET_ERROR(ENOSYS
);
1210 * Lookup an entry in a directory, or an extended attribute directory.
1211 * If it exists, return a held vnode reference for it.
1213 * IN: dvp - vnode of directory to search.
1214 * nm - name of entry to lookup.
1215 * pnp - full pathname to lookup [UNUSED].
1216 * flags - LOOKUP_XATTR set if looking for an attribute.
1217 * rdir - root directory vnode [UNUSED].
1218 * cr - credentials of caller.
1219 * ct - caller context
1220 * direntflags - directory lookup flags
1221 * realpnp - returned pathname.
1223 * OUT: vpp - vnode of located entry, NULL if not found.
1225 * RETURN: 0 on success, error code on failure.
1232 zfs_lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, struct pathname
*pnp
,
1233 int flags
, vnode_t
*rdir
, cred_t
*cr
, caller_context_t
*ct
,
1234 int *direntflags
, pathname_t
*realpnp
)
1236 znode_t
*zdp
= VTOZ(dvp
);
1237 zfsvfs_t
*zfsvfs
= zdp
->z_zfsvfs
;
1241 * Fast path lookup, however we must skip DNLC lookup
1242 * for case folding or normalizing lookups because the
1243 * DNLC code only stores the passed in name. This means
1244 * creating 'a' and removing 'A' on a case insensitive
1245 * file system would work, but DNLC still thinks 'a'
1246 * exists and won't let you create it again on the next
1247 * pass through fast path.
1249 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1251 if (dvp
->v_type
!= VDIR
) {
1252 return (SET_ERROR(ENOTDIR
));
1253 } else if (zdp
->z_sa_hdl
== NULL
) {
1254 return (SET_ERROR(EIO
));
1257 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1258 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1265 } else if (!zdp
->z_zfsvfs
->z_norm
&&
1266 (zdp
->z_zfsvfs
->z_case
== ZFS_CASE_SENSITIVE
)) {
1268 vnode_t
*tvp
= dnlc_lookup(dvp
, nm
);
1271 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1276 if (tvp
== DNLC_NO_VNODE
) {
1278 return (SET_ERROR(ENOENT
));
1281 return (specvp_check(vpp
, cr
));
1287 DTRACE_PROBE2(zfs__fastpath__lookup__miss
, vnode_t
*, dvp
, char *, nm
);
1294 if (flags
& LOOKUP_XATTR
) {
1296 * If the xattr property is off, refuse the lookup request.
1298 if (!(zfsvfs
->z_vfs
->vfs_flag
& VFS_XATTR
)) {
1300 return (SET_ERROR(EINVAL
));
1304 * We don't allow recursive attributes..
1305 * Maybe someday we will.
1307 if (zdp
->z_pflags
& ZFS_XATTR
) {
1309 return (SET_ERROR(EINVAL
));
1312 if (error
= zfs_get_xattrdir(VTOZ(dvp
), vpp
, cr
, flags
)) {
1318 * Do we have permission to get into attribute directory?
1321 if (error
= zfs_zaccess(VTOZ(*vpp
), ACE_EXECUTE
, 0,
1331 if (dvp
->v_type
!= VDIR
) {
1333 return (SET_ERROR(ENOTDIR
));
1337 * Check accessibility of directory.
1340 if (error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
)) {
1345 if (zfsvfs
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1346 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1348 return (SET_ERROR(EILSEQ
));
1351 error
= zfs_dirlook(zdp
, nm
, vpp
, flags
, direntflags
, realpnp
);
1353 error
= specvp_check(vpp
, cr
);
1360 * Attempt to create a new entry in a directory. If the entry
1361 * already exists, truncate the file if permissible, else return
1362 * an error. Return the vp of the created or trunc'd file.
1364 * IN: dvp - vnode of directory to put new file entry in.
1365 * name - name of new file entry.
1366 * vap - attributes of new file.
1367 * excl - flag indicating exclusive or non-exclusive mode.
1368 * mode - mode to open file with.
1369 * cr - credentials of caller.
1370 * flag - large file flag [UNUSED].
1371 * ct - caller context
1372 * vsecp - ACL to be set
1374 * OUT: vpp - vnode of created or trunc'd entry.
1376 * RETURN: 0 on success, error code on failure.
1379 * dvp - ctime|mtime updated if new entry created
1380 * vp - ctime|mtime always, atime if new
1385 zfs_create(vnode_t
*dvp
, char *name
, vattr_t
*vap
, vcexcl_t excl
,
1386 int mode
, vnode_t
**vpp
, cred_t
*cr
, int flag
, caller_context_t
*ct
,
1389 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1390 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1398 gid_t gid
= crgetgid(cr
);
1399 zfs_acl_ids_t acl_ids
;
1400 boolean_t fuid_dirtied
;
1401 boolean_t have_acl
= B_FALSE
;
1402 boolean_t waited
= B_FALSE
;
1405 * If we have an ephemeral id, ACL, or XVATTR then
1406 * make sure file system is at proper version
1409 ksid
= crgetsid(cr
, KSID_OWNER
);
1411 uid
= ksid_getid(ksid
);
1415 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1416 (vsecp
|| (vap
->va_mask
& AT_XVATTR
) ||
1417 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1418 return (SET_ERROR(EINVAL
));
1423 zilog
= zfsvfs
->z_log
;
1425 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
1426 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1428 return (SET_ERROR(EILSEQ
));
1431 if (vap
->va_mask
& AT_XVATTR
) {
1432 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1433 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
1441 if ((vap
->va_mode
& VSVTX
) && secpolicy_vnode_stky_modify(cr
))
1442 vap
->va_mode
&= ~VSVTX
;
1444 if (*name
== '\0') {
1446 * Null component name refers to the directory itself.
1453 /* possible VN_HOLD(zp) */
1456 if (flag
& FIGNORECASE
)
1459 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1463 zfs_acl_ids_free(&acl_ids
);
1464 if (strcmp(name
, "..") == 0)
1465 error
= SET_ERROR(EISDIR
);
1475 * Create a new file object and update the directory
1478 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
1480 zfs_acl_ids_free(&acl_ids
);
1485 * We only support the creation of regular files in
1486 * extended attribute directories.
1489 if ((dzp
->z_pflags
& ZFS_XATTR
) &&
1490 (vap
->va_type
!= VREG
)) {
1492 zfs_acl_ids_free(&acl_ids
);
1493 error
= SET_ERROR(EINVAL
);
1497 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1498 cr
, vsecp
, &acl_ids
)) != 0)
1502 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
)) {
1503 zfs_acl_ids_free(&acl_ids
);
1504 error
= SET_ERROR(EDQUOT
);
1508 tx
= dmu_tx_create(os
);
1510 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1511 ZFS_SA_BASE_ATTR_SIZE
);
1513 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1515 zfs_fuid_txhold(zfsvfs
, tx
);
1516 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1517 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1518 if (!zfsvfs
->z_use_sa
&&
1519 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1520 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1521 0, acl_ids
.z_aclp
->z_acl_bytes
);
1523 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1525 zfs_dirent_unlock(dl
);
1526 if (error
== ERESTART
) {
1532 zfs_acl_ids_free(&acl_ids
);
1537 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1540 zfs_fuid_sync(zfsvfs
, tx
);
1542 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1543 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1544 if (flag
& FIGNORECASE
)
1546 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1547 vsecp
, acl_ids
.z_fuidp
, vap
);
1548 zfs_acl_ids_free(&acl_ids
);
1551 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1554 zfs_acl_ids_free(&acl_ids
);
1558 * A directory entry already exists for this name.
1561 * Can't truncate an existing file if in exclusive mode.
1564 error
= SET_ERROR(EEXIST
);
1568 * Can't open a directory for writing.
1570 if ((ZTOV(zp
)->v_type
== VDIR
) && (mode
& S_IWRITE
)) {
1571 error
= SET_ERROR(EISDIR
);
1575 * Verify requested access to file.
1577 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1581 mutex_enter(&dzp
->z_lock
);
1583 mutex_exit(&dzp
->z_lock
);
1586 * Truncate regular files if requested.
1588 if ((ZTOV(zp
)->v_type
== VREG
) &&
1589 (vap
->va_mask
& AT_SIZE
) && (vap
->va_size
== 0)) {
1590 /* we can't hold any locks when calling zfs_freesp() */
1591 zfs_dirent_unlock(dl
);
1593 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1595 vnevent_create(ZTOV(zp
), ct
);
1602 zfs_dirent_unlock(dl
);
1609 error
= specvp_check(vpp
, cr
);
1612 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1613 zil_commit(zilog
, 0);
1620 * Remove an entry from a directory.
1622 * IN: dvp - vnode of directory to remove entry from.
1623 * name - name of entry to remove.
1624 * cr - credentials of caller.
1625 * ct - caller context
1626 * flags - case flags
1628 * RETURN: 0 on success, error code on failure.
1632 * vp - ctime (if nlink > 0)
1635 uint64_t null_xattr
= 0;
1639 zfs_remove(vnode_t
*dvp
, char *name
, cred_t
*cr
, caller_context_t
*ct
,
1642 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1645 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1647 uint64_t acl_obj
, xattr_obj
;
1648 uint64_t xattr_obj_unlinked
= 0;
1652 boolean_t may_delete_now
, delete_now
= FALSE
;
1653 boolean_t unlinked
, toobig
= FALSE
;
1655 pathname_t
*realnmp
= NULL
;
1659 boolean_t waited
= B_FALSE
;
1663 zilog
= zfsvfs
->z_log
;
1665 if (flags
& FIGNORECASE
) {
1675 * Attempt to lock directory; fail if entry doesn't exist.
1677 if (error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1687 if (error
= zfs_zaccess_delete(dzp
, zp
, cr
)) {
1692 * Need to use rmdir for removing directories.
1694 if (vp
->v_type
== VDIR
) {
1695 error
= SET_ERROR(EPERM
);
1699 vnevent_remove(vp
, dvp
, name
, ct
);
1702 dnlc_remove(dvp
, realnmp
->pn_buf
);
1704 dnlc_remove(dvp
, name
);
1706 mutex_enter(&vp
->v_lock
);
1707 may_delete_now
= vp
->v_count
== 1 && !vn_has_cached_data(vp
);
1708 mutex_exit(&vp
->v_lock
);
1711 * We may delete the znode now, or we may put it in the unlinked set;
1712 * it depends on whether we're the last link, and on whether there are
1713 * other holds on the vnode. So we dmu_tx_hold() the right things to
1714 * allow for either case.
1717 tx
= dmu_tx_create(zfsvfs
->z_os
);
1718 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1719 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1720 zfs_sa_upgrade_txholds(tx
, zp
);
1721 zfs_sa_upgrade_txholds(tx
, dzp
);
1722 if (may_delete_now
) {
1724 zp
->z_size
> zp
->z_blksz
* DMU_MAX_DELETEBLKCNT
;
1725 /* if the file is too big, only hold_free a token amount */
1726 dmu_tx_hold_free(tx
, zp
->z_id
, 0,
1727 (toobig
? DMU_MAX_ACCESS
: DMU_OBJECT_END
));
1730 /* are there any extended attributes? */
1731 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1732 &xattr_obj
, sizeof (xattr_obj
));
1733 if (error
== 0 && xattr_obj
) {
1734 error
= zfs_zget(zfsvfs
, xattr_obj
, &xzp
);
1736 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1737 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1740 mutex_enter(&zp
->z_lock
);
1741 if ((acl_obj
= zfs_external_acl(zp
)) != 0 && may_delete_now
)
1742 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
1743 mutex_exit(&zp
->z_lock
);
1745 /* charge as an update -- would be nice not to charge at all */
1746 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1749 * Mark this transaction as typically resulting in a net free of space
1751 dmu_tx_mark_netfree(tx
);
1753 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1755 zfs_dirent_unlock(dl
);
1759 if (error
== ERESTART
) {
1773 * Remove the directory entry.
1775 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1784 * Hold z_lock so that we can make sure that the ACL obj
1785 * hasn't changed. Could have been deleted due to
1788 mutex_enter(&zp
->z_lock
);
1789 mutex_enter(&vp
->v_lock
);
1790 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1791 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1792 delete_now
= may_delete_now
&& !toobig
&&
1793 vp
->v_count
== 1 && !vn_has_cached_data(vp
) &&
1794 xattr_obj
== xattr_obj_unlinked
&& zfs_external_acl(zp
) ==
1796 mutex_exit(&vp
->v_lock
);
1800 if (xattr_obj_unlinked
) {
1801 ASSERT3U(xzp
->z_links
, ==, 2);
1802 mutex_enter(&xzp
->z_lock
);
1803 xzp
->z_unlinked
= 1;
1805 error
= sa_update(xzp
->z_sa_hdl
, SA_ZPL_LINKS(zfsvfs
),
1806 &xzp
->z_links
, sizeof (xzp
->z_links
), tx
);
1807 ASSERT3U(error
, ==, 0);
1808 mutex_exit(&xzp
->z_lock
);
1809 zfs_unlinked_add(xzp
, tx
);
1812 error
= sa_remove(zp
->z_sa_hdl
,
1813 SA_ZPL_XATTR(zfsvfs
), tx
);
1815 error
= sa_update(zp
->z_sa_hdl
,
1816 SA_ZPL_XATTR(zfsvfs
), &null_xattr
,
1817 sizeof (uint64_t), tx
);
1820 mutex_enter(&vp
->v_lock
);
1822 ASSERT0(vp
->v_count
);
1823 mutex_exit(&vp
->v_lock
);
1824 mutex_exit(&zp
->z_lock
);
1825 zfs_znode_delete(zp
, tx
);
1826 } else if (unlinked
) {
1827 mutex_exit(&zp
->z_lock
);
1828 zfs_unlinked_add(zp
, tx
);
1832 if (flags
& FIGNORECASE
)
1834 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
);
1841 zfs_dirent_unlock(dl
);
1848 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1849 zil_commit(zilog
, 0);
1856 * Create a new directory and insert it into dvp using the name
1857 * provided. Return a pointer to the inserted directory.
1859 * IN: dvp - vnode of directory to add subdir to.
1860 * dirname - name of new directory.
1861 * vap - attributes of new directory.
1862 * cr - credentials of caller.
1863 * ct - caller context
1864 * flags - case flags
1865 * vsecp - ACL to be set
1867 * OUT: vpp - vnode of created directory.
1869 * RETURN: 0 on success, error code on failure.
1872 * dvp - ctime|mtime updated
1873 * vp - ctime|mtime|atime updated
1877 zfs_mkdir(vnode_t
*dvp
, char *dirname
, vattr_t
*vap
, vnode_t
**vpp
, cred_t
*cr
,
1878 caller_context_t
*ct
, int flags
, vsecattr_t
*vsecp
)
1880 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1881 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1890 gid_t gid
= crgetgid(cr
);
1891 zfs_acl_ids_t acl_ids
;
1892 boolean_t fuid_dirtied
;
1893 boolean_t waited
= B_FALSE
;
1895 ASSERT(vap
->va_type
== VDIR
);
1898 * If we have an ephemeral id, ACL, or XVATTR then
1899 * make sure file system is at proper version
1902 ksid
= crgetsid(cr
, KSID_OWNER
);
1904 uid
= ksid_getid(ksid
);
1907 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1908 (vsecp
|| (vap
->va_mask
& AT_XVATTR
) ||
1909 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1910 return (SET_ERROR(EINVAL
));
1914 zilog
= zfsvfs
->z_log
;
1916 if (dzp
->z_pflags
& ZFS_XATTR
) {
1918 return (SET_ERROR(EINVAL
));
1921 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1922 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1924 return (SET_ERROR(EILSEQ
));
1926 if (flags
& FIGNORECASE
)
1929 if (vap
->va_mask
& AT_XVATTR
) {
1930 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1931 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
1937 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1938 vsecp
, &acl_ids
)) != 0) {
1943 * First make sure the new directory doesn't exist.
1945 * Existence is checked first to make sure we don't return
1946 * EACCES instead of EEXIST which can cause some applications
1952 if (error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1954 zfs_acl_ids_free(&acl_ids
);
1959 if (error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
)) {
1960 zfs_acl_ids_free(&acl_ids
);
1961 zfs_dirent_unlock(dl
);
1966 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
)) {
1967 zfs_acl_ids_free(&acl_ids
);
1968 zfs_dirent_unlock(dl
);
1970 return (SET_ERROR(EDQUOT
));
1974 * Add a new entry to the directory.
1976 tx
= dmu_tx_create(zfsvfs
->z_os
);
1977 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1978 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1979 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1981 zfs_fuid_txhold(zfsvfs
, tx
);
1982 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1983 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1984 acl_ids
.z_aclp
->z_acl_bytes
);
1987 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1988 ZFS_SA_BASE_ATTR_SIZE
);
1990 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1992 zfs_dirent_unlock(dl
);
1993 if (error
== ERESTART
) {
1999 zfs_acl_ids_free(&acl_ids
);
2008 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
2011 zfs_fuid_sync(zfsvfs
, tx
);
2014 * Now put new name in parent dir.
2016 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
2020 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
2021 if (flags
& FIGNORECASE
)
2023 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
2024 acl_ids
.z_fuidp
, vap
);
2026 zfs_acl_ids_free(&acl_ids
);
2030 zfs_dirent_unlock(dl
);
2032 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2033 zil_commit(zilog
, 0);
2040 * Remove a directory subdir entry. If the current working
2041 * directory is the same as the subdir to be removed, the
2044 * IN: dvp - vnode of directory to remove from.
2045 * name - name of directory to be removed.
2046 * cwd - vnode of current working directory.
2047 * cr - credentials of caller.
2048 * ct - caller context
2049 * flags - case flags
2051 * RETURN: 0 on success, error code on failure.
2054 * dvp - ctime|mtime updated
2058 zfs_rmdir(vnode_t
*dvp
, char *name
, vnode_t
*cwd
, cred_t
*cr
,
2059 caller_context_t
*ct
, int flags
)
2061 znode_t
*dzp
= VTOZ(dvp
);
2064 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
2070 boolean_t waited
= B_FALSE
;
2074 zilog
= zfsvfs
->z_log
;
2076 if (flags
& FIGNORECASE
)
2082 * Attempt to lock directory; fail if entry doesn't exist.
2084 if (error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
2092 if (error
= zfs_zaccess_delete(dzp
, zp
, cr
)) {
2096 if (vp
->v_type
!= VDIR
) {
2097 error
= SET_ERROR(ENOTDIR
);
2102 error
= SET_ERROR(EINVAL
);
2106 vnevent_rmdir(vp
, dvp
, name
, ct
);
2109 * Grab a lock on the directory to make sure that noone is
2110 * trying to add (or lookup) entries while we are removing it.
2112 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
2115 * Grab a lock on the parent pointer to make sure we play well
2116 * with the treewalk and directory rename code.
2118 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
2120 tx
= dmu_tx_create(zfsvfs
->z_os
);
2121 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
2122 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2123 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
2124 zfs_sa_upgrade_txholds(tx
, zp
);
2125 zfs_sa_upgrade_txholds(tx
, dzp
);
2126 dmu_tx_mark_netfree(tx
);
2127 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
2129 rw_exit(&zp
->z_parent_lock
);
2130 rw_exit(&zp
->z_name_lock
);
2131 zfs_dirent_unlock(dl
);
2133 if (error
== ERESTART
) {
2144 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
2147 uint64_t txtype
= TX_RMDIR
;
2148 if (flags
& FIGNORECASE
)
2150 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
);
2155 rw_exit(&zp
->z_parent_lock
);
2156 rw_exit(&zp
->z_name_lock
);
2158 zfs_dirent_unlock(dl
);
2162 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2163 zil_commit(zilog
, 0);
2170 * Read as many directory entries as will fit into the provided
2171 * buffer from the given directory cursor position (specified in
2172 * the uio structure).
2174 * IN: vp - vnode of directory to read.
2175 * uio - structure supplying read location, range info,
2176 * and return buffer.
2177 * cr - credentials of caller.
2178 * ct - caller context
2179 * flags - case flags
2181 * OUT: uio - updated offset and range, buffer filled.
2182 * eofp - set to true if end-of-file detected.
2184 * RETURN: 0 on success, error code on failure.
2187 * vp - atime updated
2189 * Note that the low 4 bits of the cookie returned by zap is always zero.
2190 * This allows us to use the low range for "special" directory entries:
2191 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2192 * we use the offset 2 for the '.zfs' directory.
2196 zfs_readdir(vnode_t
*vp
, uio_t
*uio
, cred_t
*cr
, int *eofp
,
2197 caller_context_t
*ct
, int flags
)
2199 znode_t
*zp
= VTOZ(vp
);
2203 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2208 zap_attribute_t zap
;
2209 uint_t bytes_wanted
;
2210 uint64_t offset
; /* must be unsigned; checks for < 1 */
2216 boolean_t check_sysattrs
;
2221 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
2222 &parent
, sizeof (parent
))) != 0) {
2228 * If we are not given an eof variable,
2235 * Check for valid iov_len.
2237 if (uio
->uio_iov
->iov_len
<= 0) {
2239 return (SET_ERROR(EINVAL
));
2243 * Quit if directory has been removed (posix)
2245 if ((*eofp
= zp
->z_unlinked
) != 0) {
2252 offset
= uio
->uio_loffset
;
2253 prefetch
= zp
->z_zn_prefetch
;
2256 * Initialize the iterator cursor.
2260 * Start iteration from the beginning of the directory.
2262 zap_cursor_init(&zc
, os
, zp
->z_id
);
2265 * The offset is a serialized cursor.
2267 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
2271 * Get space to change directory entries into fs independent format.
2273 iovp
= uio
->uio_iov
;
2274 bytes_wanted
= iovp
->iov_len
;
2275 if (uio
->uio_segflg
!= UIO_SYSSPACE
|| uio
->uio_iovcnt
!= 1) {
2276 bufsize
= bytes_wanted
;
2277 outbuf
= kmem_alloc(bufsize
, KM_SLEEP
);
2278 odp
= (struct dirent64
*)outbuf
;
2280 bufsize
= bytes_wanted
;
2282 odp
= (struct dirent64
*)iovp
->iov_base
;
2284 eodp
= (struct edirent
*)odp
;
2287 * If this VFS supports the system attribute view interface; and
2288 * we're looking at an extended attribute directory; and we care
2289 * about normalization conflicts on this vfs; then we must check
2290 * for normalization conflicts with the sysattr name space.
2292 check_sysattrs
= vfs_has_feature(vp
->v_vfsp
, VFSFT_SYSATTR_VIEWS
) &&
2293 (vp
->v_flag
& V_XATTRDIR
) && zfsvfs
->z_norm
&&
2294 (flags
& V_RDDIR_ENTFLAGS
);
2297 * Transform to file-system independent format
2300 while (outcount
< bytes_wanted
) {
2303 off64_t
*next
= NULL
;
2306 * Special case `.', `..', and `.zfs'.
2309 (void) strcpy(zap
.za_name
, ".");
2310 zap
.za_normalization_conflict
= 0;
2312 } else if (offset
== 1) {
2313 (void) strcpy(zap
.za_name
, "..");
2314 zap
.za_normalization_conflict
= 0;
2316 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
2317 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
2318 zap
.za_normalization_conflict
= 0;
2319 objnum
= ZFSCTL_INO_ROOT
;
2324 if (error
= zap_cursor_retrieve(&zc
, &zap
)) {
2325 if ((*eofp
= (error
== ENOENT
)) != 0)
2331 if (zap
.za_integer_length
!= 8 ||
2332 zap
.za_num_integers
!= 1) {
2333 cmn_err(CE_WARN
, "zap_readdir: bad directory "
2334 "entry, obj = %lld, offset = %lld\n",
2335 (u_longlong_t
)zp
->z_id
,
2336 (u_longlong_t
)offset
);
2337 error
= SET_ERROR(ENXIO
);
2341 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
2343 * MacOS X can extract the object type here such as:
2344 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2347 if (check_sysattrs
&& !zap
.za_normalization_conflict
) {
2348 zap
.za_normalization_conflict
=
2349 xattr_sysattr_casechk(zap
.za_name
);
2353 if (flags
& V_RDDIR_ACCFILTER
) {
2355 * If we have no access at all, don't include
2356 * this entry in the returned information
2359 if (zfs_zget(zp
->z_zfsvfs
, objnum
, &ezp
) != 0)
2361 if (!zfs_has_access(ezp
, cr
)) {
2368 if (flags
& V_RDDIR_ENTFLAGS
)
2369 reclen
= EDIRENT_RECLEN(strlen(zap
.za_name
));
2371 reclen
= DIRENT64_RECLEN(strlen(zap
.za_name
));
2374 * Will this entry fit in the buffer?
2376 if (outcount
+ reclen
> bufsize
) {
2378 * Did we manage to fit anything in the buffer?
2381 error
= SET_ERROR(EINVAL
);
2386 if (flags
& V_RDDIR_ENTFLAGS
) {
2388 * Add extended flag entry:
2390 eodp
->ed_ino
= objnum
;
2391 eodp
->ed_reclen
= reclen
;
2392 /* NOTE: ed_off is the offset for the *next* entry */
2393 next
= &(eodp
->ed_off
);
2394 eodp
->ed_eflags
= zap
.za_normalization_conflict
?
2395 ED_CASE_CONFLICT
: 0;
2396 (void) strncpy(eodp
->ed_name
, zap
.za_name
,
2397 EDIRENT_NAMELEN(reclen
));
2398 eodp
= (edirent_t
*)((intptr_t)eodp
+ reclen
);
2403 odp
->d_ino
= objnum
;
2404 odp
->d_reclen
= reclen
;
2405 /* NOTE: d_off is the offset for the *next* entry */
2406 next
= &(odp
->d_off
);
2407 (void) strncpy(odp
->d_name
, zap
.za_name
,
2408 DIRENT64_NAMELEN(reclen
));
2409 odp
= (dirent64_t
*)((intptr_t)odp
+ reclen
);
2413 ASSERT(outcount
<= bufsize
);
2415 /* Prefetch znode */
2417 dmu_prefetch(os
, objnum
, 0, 0, 0,
2418 ZIO_PRIORITY_SYNC_READ
);
2422 * Move to the next entry, fill in the previous offset.
2424 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2425 zap_cursor_advance(&zc
);
2426 offset
= zap_cursor_serialize(&zc
);
2433 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2435 if (uio
->uio_segflg
== UIO_SYSSPACE
&& uio
->uio_iovcnt
== 1) {
2436 iovp
->iov_base
+= outcount
;
2437 iovp
->iov_len
-= outcount
;
2438 uio
->uio_resid
-= outcount
;
2439 } else if (error
= uiomove(outbuf
, (long)outcount
, UIO_READ
, uio
)) {
2441 * Reset the pointer.
2443 offset
= uio
->uio_loffset
;
2447 zap_cursor_fini(&zc
);
2448 if (uio
->uio_segflg
!= UIO_SYSSPACE
|| uio
->uio_iovcnt
!= 1)
2449 kmem_free(outbuf
, bufsize
);
2451 if (error
== ENOENT
)
2454 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
2456 uio
->uio_loffset
= offset
;
2461 ulong_t zfs_fsync_sync_cnt
= 4;
2464 zfs_fsync(vnode_t
*vp
, int syncflag
, cred_t
*cr
, caller_context_t
*ct
)
2466 znode_t
*zp
= VTOZ(vp
);
2467 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2470 * Regardless of whether this is required for standards conformance,
2471 * this is the logical behavior when fsync() is called on a file with
2472 * dirty pages. We use B_ASYNC since the ZIL transactions are already
2473 * going to be pushed out as part of the zil_commit().
2475 if (vn_has_cached_data(vp
) && !(syncflag
& FNODSYNC
) &&
2476 (vp
->v_type
== VREG
) && !(IS_SWAPVP(vp
)))
2477 (void) VOP_PUTPAGE(vp
, (offset_t
)0, (size_t)0, B_ASYNC
, cr
, ct
);
2479 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2481 if (zfsvfs
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2484 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
2492 * Get the requested file attributes and place them in the provided
2495 * IN: vp - vnode of file.
2496 * vap - va_mask identifies requested attributes.
2497 * If AT_XVATTR set, then optional attrs are requested
2498 * flags - ATTR_NOACLCHECK (CIFS server context)
2499 * cr - credentials of caller.
2500 * ct - caller context
2502 * OUT: vap - attribute values.
2504 * RETURN: 0 (always succeeds).
2508 zfs_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
2509 caller_context_t
*ct
)
2511 znode_t
*zp
= VTOZ(vp
);
2512 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2515 uint64_t mtime
[2], ctime
[2];
2516 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2517 xoptattr_t
*xoap
= NULL
;
2518 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2519 sa_bulk_attr_t bulk
[2];
2525 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2527 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
2528 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
2530 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2536 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2537 * Also, if we are the owner don't bother, since owner should
2538 * always be allowed to read basic attributes of file.
2540 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2541 (vap
->va_uid
!= crgetuid(cr
))) {
2542 if (error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2550 * Return all attributes. It's cheaper to provide the answer
2551 * than to determine whether we were asked the question.
2554 mutex_enter(&zp
->z_lock
);
2555 vap
->va_type
= vp
->v_type
;
2556 vap
->va_mode
= zp
->z_mode
& MODEMASK
;
2557 vap
->va_fsid
= zp
->z_zfsvfs
->z_vfs
->vfs_dev
;
2558 vap
->va_nodeid
= zp
->z_id
;
2559 if ((vp
->v_flag
& VROOT
) && zfs_show_ctldir(zp
))
2560 links
= zp
->z_links
+ 1;
2562 links
= zp
->z_links
;
2563 vap
->va_nlink
= MIN(links
, UINT32_MAX
); /* nlink_t limit! */
2564 vap
->va_size
= zp
->z_size
;
2565 vap
->va_rdev
= vp
->v_rdev
;
2566 vap
->va_seq
= zp
->z_seq
;
2569 * Add in any requested optional attributes and the create time.
2570 * Also set the corresponding bits in the returned attribute bitmap.
2572 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zfsvfs
->z_use_fuids
) {
2573 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2575 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2576 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2579 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2580 xoap
->xoa_readonly
=
2581 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2582 XVA_SET_RTN(xvap
, XAT_READONLY
);
2585 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2587 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2588 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2591 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2593 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2594 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2597 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2598 xoap
->xoa_nounlink
=
2599 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2600 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2603 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2604 xoap
->xoa_immutable
=
2605 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2606 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2609 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2610 xoap
->xoa_appendonly
=
2611 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2612 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2615 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2617 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2618 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2621 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2623 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2624 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2627 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2628 xoap
->xoa_av_quarantined
=
2629 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2630 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2633 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2634 xoap
->xoa_av_modified
=
2635 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2636 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2639 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2640 vp
->v_type
== VREG
) {
2641 zfs_sa_get_scanstamp(zp
, xvap
);
2644 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2647 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zfsvfs
),
2648 times
, sizeof (times
));
2649 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2650 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2653 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2654 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2655 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2657 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2658 xoap
->xoa_generation
= zp
->z_gen
;
2659 XVA_SET_RTN(xvap
, XAT_GEN
);
2662 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2664 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2665 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2668 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2670 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2671 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2675 ZFS_TIME_DECODE(&vap
->va_atime
, zp
->z_atime
);
2676 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2677 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2679 mutex_exit(&zp
->z_lock
);
2681 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2683 if (zp
->z_blksz
== 0) {
2685 * Block size hasn't been set; suggest maximal I/O transfers.
2687 vap
->va_blksize
= zfsvfs
->z_max_blksz
;
2695 * Set the file attributes to the values contained in the
2698 * IN: vp - vnode of file to be modified.
2699 * vap - new attribute values.
2700 * If AT_XVATTR set, then optional attrs are being set
2701 * flags - ATTR_UTIME set if non-default time values provided.
2702 * - ATTR_NOACLCHECK (CIFS context only).
2703 * cr - credentials of caller.
2704 * ct - caller context
2706 * RETURN: 0 on success, error code on failure.
2709 * vp - ctime updated, mtime updated if size changed.
2713 zfs_setattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
2714 caller_context_t
*ct
)
2716 znode_t
*zp
= VTOZ(vp
);
2717 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2722 uint_t mask
= vap
->va_mask
;
2723 uint_t saved_mask
= 0;
2726 uint64_t new_uid
, new_gid
;
2728 uint64_t mtime
[2], ctime
[2];
2730 int need_policy
= FALSE
;
2732 zfs_fuid_info_t
*fuidp
= NULL
;
2733 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2736 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2737 boolean_t fuid_dirtied
= B_FALSE
;
2738 sa_bulk_attr_t bulk
[7], xattr_bulk
[7];
2739 int count
= 0, xattr_count
= 0;
2744 if (mask
& AT_NOSET
)
2745 return (SET_ERROR(EINVAL
));
2750 zilog
= zfsvfs
->z_log
;
2753 * Make sure that if we have ephemeral uid/gid or xvattr specified
2754 * that file system is at proper version level
2757 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
2758 (((mask
& AT_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2759 ((mask
& AT_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2760 (mask
& AT_XVATTR
))) {
2762 return (SET_ERROR(EINVAL
));
2765 if (mask
& AT_SIZE
&& vp
->v_type
== VDIR
) {
2767 return (SET_ERROR(EISDIR
));
2770 if (mask
& AT_SIZE
&& vp
->v_type
!= VREG
&& vp
->v_type
!= VFIFO
) {
2772 return (SET_ERROR(EINVAL
));
2776 * If this is an xvattr_t, then get a pointer to the structure of
2777 * optional attributes. If this is NULL, then we have a vattr_t.
2779 xoap
= xva_getxoptattr(xvap
);
2781 xva_init(&tmpxvattr
);
2784 * Immutable files can only alter immutable bit and atime
2786 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2787 ((mask
& (AT_SIZE
|AT_UID
|AT_GID
|AT_MTIME
|AT_MODE
)) ||
2788 ((mask
& AT_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2790 return (SET_ERROR(EPERM
));
2794 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2798 * Verify timestamps doesn't overflow 32 bits.
2799 * ZFS can handle large timestamps, but 32bit syscalls can't
2800 * handle times greater than 2039. This check should be removed
2801 * once large timestamps are fully supported.
2803 if (mask
& (AT_ATIME
| AT_MTIME
)) {
2804 if (((mask
& AT_ATIME
) && TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2805 ((mask
& AT_MTIME
) && TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2807 return (SET_ERROR(EOVERFLOW
));
2815 /* Can this be moved to before the top label? */
2816 if (zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) {
2818 return (SET_ERROR(EROFS
));
2822 * First validate permissions
2825 if (mask
& AT_SIZE
) {
2826 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2832 * XXX - Note, we are not providing any open
2833 * mode flags here (like FNDELAY), so we may
2834 * block if there are locks present... this
2835 * should be addressed in openat().
2837 /* XXX - would it be OK to generate a log record here? */
2838 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2844 if (vap
->va_size
== 0)
2845 vnevent_truncate(ZTOV(zp
), ct
);
2848 if (mask
& (AT_ATIME
|AT_MTIME
) ||
2849 ((mask
& AT_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2850 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2851 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2852 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2853 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2854 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2855 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2856 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2860 if (mask
& (AT_UID
|AT_GID
)) {
2861 int idmask
= (mask
& (AT_UID
|AT_GID
));
2866 * NOTE: even if a new mode is being set,
2867 * we may clear S_ISUID/S_ISGID bits.
2870 if (!(mask
& AT_MODE
))
2871 vap
->va_mode
= zp
->z_mode
;
2874 * Take ownership or chgrp to group we are a member of
2877 take_owner
= (mask
& AT_UID
) && (vap
->va_uid
== crgetuid(cr
));
2878 take_group
= (mask
& AT_GID
) &&
2879 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
2882 * If both AT_UID and AT_GID are set then take_owner and
2883 * take_group must both be set in order to allow taking
2886 * Otherwise, send the check through secpolicy_vnode_setattr()
2890 if (((idmask
== (AT_UID
|AT_GID
)) && take_owner
&& take_group
) ||
2891 ((idmask
== AT_UID
) && take_owner
) ||
2892 ((idmask
== AT_GID
) && take_group
)) {
2893 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2894 skipaclchk
, cr
) == 0) {
2896 * Remove setuid/setgid for non-privileged users
2898 secpolicy_setid_clear(vap
, cr
);
2899 trim_mask
= (mask
& (AT_UID
|AT_GID
));
2908 mutex_enter(&zp
->z_lock
);
2909 oldva
.va_mode
= zp
->z_mode
;
2910 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2911 if (mask
& AT_XVATTR
) {
2913 * Update xvattr mask to include only those attributes
2914 * that are actually changing.
2916 * the bits will be restored prior to actually setting
2917 * the attributes so the caller thinks they were set.
2919 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2920 if (xoap
->xoa_appendonly
!=
2921 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2924 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2925 XVA_SET_REQ(&tmpxvattr
, XAT_APPENDONLY
);
2929 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2930 if (xoap
->xoa_nounlink
!=
2931 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2934 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2935 XVA_SET_REQ(&tmpxvattr
, XAT_NOUNLINK
);
2939 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2940 if (xoap
->xoa_immutable
!=
2941 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2944 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2945 XVA_SET_REQ(&tmpxvattr
, XAT_IMMUTABLE
);
2949 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2950 if (xoap
->xoa_nodump
!=
2951 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2954 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2955 XVA_SET_REQ(&tmpxvattr
, XAT_NODUMP
);
2959 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2960 if (xoap
->xoa_av_modified
!=
2961 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2964 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2965 XVA_SET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
);
2969 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2970 if ((vp
->v_type
!= VREG
&&
2971 xoap
->xoa_av_quarantined
) ||
2972 xoap
->xoa_av_quarantined
!=
2973 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2976 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2977 XVA_SET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
);
2981 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2982 mutex_exit(&zp
->z_lock
);
2984 return (SET_ERROR(EPERM
));
2987 if (need_policy
== FALSE
&&
2988 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2989 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2994 mutex_exit(&zp
->z_lock
);
2996 if (mask
& AT_MODE
) {
2997 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2998 err
= secpolicy_setid_setsticky_clear(vp
, vap
,
3004 trim_mask
|= AT_MODE
;
3012 * If trim_mask is set then take ownership
3013 * has been granted or write_acl is present and user
3014 * has the ability to modify mode. In that case remove
3015 * UID|GID and or MODE from mask so that
3016 * secpolicy_vnode_setattr() doesn't revoke it.
3020 saved_mask
= vap
->va_mask
;
3021 vap
->va_mask
&= ~trim_mask
;
3023 err
= secpolicy_vnode_setattr(cr
, vp
, vap
, &oldva
, flags
,
3024 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
3031 vap
->va_mask
|= saved_mask
;
3035 * secpolicy_vnode_setattr, or take ownership may have
3038 mask
= vap
->va_mask
;
3040 if ((mask
& (AT_UID
| AT_GID
))) {
3041 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
3042 &xattr_obj
, sizeof (xattr_obj
));
3044 if (err
== 0 && xattr_obj
) {
3045 err
= zfs_zget(zp
->z_zfsvfs
, xattr_obj
, &attrzp
);
3049 if (mask
& AT_UID
) {
3050 new_uid
= zfs_fuid_create(zfsvfs
,
3051 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
3052 if (new_uid
!= zp
->z_uid
&&
3053 zfs_fuid_overquota(zfsvfs
, B_FALSE
, new_uid
)) {
3055 VN_RELE(ZTOV(attrzp
));
3056 err
= SET_ERROR(EDQUOT
);
3061 if (mask
& AT_GID
) {
3062 new_gid
= zfs_fuid_create(zfsvfs
, (uint64_t)vap
->va_gid
,
3063 cr
, ZFS_GROUP
, &fuidp
);
3064 if (new_gid
!= zp
->z_gid
&&
3065 zfs_fuid_overquota(zfsvfs
, B_TRUE
, new_gid
)) {
3067 VN_RELE(ZTOV(attrzp
));
3068 err
= SET_ERROR(EDQUOT
);
3073 tx
= dmu_tx_create(zfsvfs
->z_os
);
3075 if (mask
& AT_MODE
) {
3076 uint64_t pmode
= zp
->z_mode
;
3078 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
3080 if (zp
->z_zfsvfs
->z_acl_mode
== ZFS_ACL_RESTRICTED
&&
3081 !(zp
->z_pflags
& ZFS_ACL_TRIVIAL
)) {
3082 err
= SET_ERROR(EPERM
);
3086 if (err
= zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
))
3089 mutex_enter(&zp
->z_lock
);
3090 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
3092 * Are we upgrading ACL from old V0 format
3095 if (zfsvfs
->z_version
>= ZPL_VERSION_FUID
&&
3096 zfs_znode_acl_version(zp
) ==
3097 ZFS_ACL_VERSION_INITIAL
) {
3098 dmu_tx_hold_free(tx
, acl_obj
, 0,
3100 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
3101 0, aclp
->z_acl_bytes
);
3103 dmu_tx_hold_write(tx
, acl_obj
, 0,
3106 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3107 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
3108 0, aclp
->z_acl_bytes
);
3110 mutex_exit(&zp
->z_lock
);
3111 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
3113 if ((mask
& AT_XVATTR
) &&
3114 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
3115 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
3117 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3121 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
3124 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3126 zfs_fuid_txhold(zfsvfs
, tx
);
3128 zfs_sa_upgrade_txholds(tx
, zp
);
3130 err
= dmu_tx_assign(tx
, TXG_WAIT
);
3136 * Set each attribute requested.
3137 * We group settings according to the locks they need to acquire.
3139 * Note: you cannot set ctime directly, although it will be
3140 * updated as a side-effect of calling this function.
3144 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
3145 mutex_enter(&zp
->z_acl_lock
);
3146 mutex_enter(&zp
->z_lock
);
3148 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
3149 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
3152 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
3153 mutex_enter(&attrzp
->z_acl_lock
);
3154 mutex_enter(&attrzp
->z_lock
);
3155 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3156 SA_ZPL_FLAGS(zfsvfs
), NULL
, &attrzp
->z_pflags
,
3157 sizeof (attrzp
->z_pflags
));
3160 if (mask
& (AT_UID
|AT_GID
)) {
3162 if (mask
& AT_UID
) {
3163 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
3164 &new_uid
, sizeof (new_uid
));
3165 zp
->z_uid
= new_uid
;
3167 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3168 SA_ZPL_UID(zfsvfs
), NULL
, &new_uid
,
3170 attrzp
->z_uid
= new_uid
;
3174 if (mask
& AT_GID
) {
3175 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
),
3176 NULL
, &new_gid
, sizeof (new_gid
));
3177 zp
->z_gid
= new_gid
;
3179 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3180 SA_ZPL_GID(zfsvfs
), NULL
, &new_gid
,
3182 attrzp
->z_gid
= new_gid
;
3185 if (!(mask
& AT_MODE
)) {
3186 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
),
3187 NULL
, &new_mode
, sizeof (new_mode
));
3188 new_mode
= zp
->z_mode
;
3190 err
= zfs_acl_chown_setattr(zp
);
3193 err
= zfs_acl_chown_setattr(attrzp
);
3198 if (mask
& AT_MODE
) {
3199 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
3200 &new_mode
, sizeof (new_mode
));
3201 zp
->z_mode
= new_mode
;
3202 ASSERT3U((uintptr_t)aclp
, !=, NULL
);
3203 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
3205 if (zp
->z_acl_cached
)
3206 zfs_acl_free(zp
->z_acl_cached
);
3207 zp
->z_acl_cached
= aclp
;
3212 if (mask
& AT_ATIME
) {
3213 ZFS_TIME_ENCODE(&vap
->va_atime
, zp
->z_atime
);
3214 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
3215 &zp
->z_atime
, sizeof (zp
->z_atime
));
3218 if (mask
& AT_MTIME
) {
3219 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
3220 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
3221 mtime
, sizeof (mtime
));
3224 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3225 if (mask
& AT_SIZE
&& !(mask
& AT_MTIME
)) {
3226 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
),
3227 NULL
, mtime
, sizeof (mtime
));
3228 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
3229 &ctime
, sizeof (ctime
));
3230 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
3232 } else if (mask
!= 0) {
3233 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
3234 &ctime
, sizeof (ctime
));
3235 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
,
3238 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3239 SA_ZPL_CTIME(zfsvfs
), NULL
,
3240 &ctime
, sizeof (ctime
));
3241 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
3242 mtime
, ctime
, B_TRUE
);
3246 * Do this after setting timestamps to prevent timestamp
3247 * update from toggling bit
3250 if (xoap
&& (mask
& AT_XVATTR
)) {
3253 * restore trimmed off masks
3254 * so that return masks can be set for caller.
3257 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_APPENDONLY
)) {
3258 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
3260 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NOUNLINK
)) {
3261 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
3263 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_IMMUTABLE
)) {
3264 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
3266 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NODUMP
)) {
3267 XVA_SET_REQ(xvap
, XAT_NODUMP
);
3269 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
)) {
3270 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
3272 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
)) {
3273 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
3276 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
3277 ASSERT(vp
->v_type
== VREG
);
3279 zfs_xvattr_set(zp
, xvap
, tx
);
3283 zfs_fuid_sync(zfsvfs
, tx
);
3286 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
3288 mutex_exit(&zp
->z_lock
);
3289 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
3290 mutex_exit(&zp
->z_acl_lock
);
3293 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
3294 mutex_exit(&attrzp
->z_acl_lock
);
3295 mutex_exit(&attrzp
->z_lock
);
3298 if (err
== 0 && attrzp
) {
3299 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3305 VN_RELE(ZTOV(attrzp
));
3311 zfs_fuid_info_free(fuidp
);
3317 if (err
== ERESTART
)
3320 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3325 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3326 zil_commit(zilog
, 0);
3332 typedef struct zfs_zlock
{
3333 krwlock_t
*zl_rwlock
; /* lock we acquired */
3334 znode_t
*zl_znode
; /* znode we held */
3335 struct zfs_zlock
*zl_next
; /* next in list */
3339 * Drop locks and release vnodes that were held by zfs_rename_lock().
3342 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3346 while ((zl
= *zlpp
) != NULL
) {
3347 if (zl
->zl_znode
!= NULL
)
3348 VN_RELE(ZTOV(zl
->zl_znode
));
3349 rw_exit(zl
->zl_rwlock
);
3350 *zlpp
= zl
->zl_next
;
3351 kmem_free(zl
, sizeof (*zl
));
3356 * Search back through the directory tree, using the ".." entries.
3357 * Lock each directory in the chain to prevent concurrent renames.
3358 * Fail any attempt to move a directory into one of its own descendants.
3359 * XXX - z_parent_lock can overlap with map or grow locks
3362 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3366 uint64_t rootid
= zp
->z_zfsvfs
->z_root
;
3367 uint64_t oidp
= zp
->z_id
;
3368 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3369 krw_t rw
= RW_WRITER
;
3372 * First pass write-locks szp and compares to zp->z_id.
3373 * Later passes read-lock zp and compare to zp->z_parent.
3376 if (!rw_tryenter(rwlp
, rw
)) {
3378 * Another thread is renaming in this path.
3379 * Note that if we are a WRITER, we don't have any
3380 * parent_locks held yet.
3382 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3384 * Drop our locks and restart
3386 zfs_rename_unlock(&zl
);
3390 rwlp
= &szp
->z_parent_lock
;
3395 * Wait for other thread to drop its locks
3401 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3402 zl
->zl_rwlock
= rwlp
;
3403 zl
->zl_znode
= NULL
;
3404 zl
->zl_next
= *zlpp
;
3407 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3408 return (SET_ERROR(EINVAL
));
3410 if (oidp
== rootid
) /* We've hit the top */
3413 if (rw
== RW_READER
) { /* i.e. not the first pass */
3414 int error
= zfs_zget(zp
->z_zfsvfs
, oidp
, &zp
);
3419 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zp
->z_zfsvfs
),
3420 &oidp
, sizeof (oidp
));
3421 rwlp
= &zp
->z_parent_lock
;
3424 } while (zp
->z_id
!= sdzp
->z_id
);
3430 * Move an entry from the provided source directory to the target
3431 * directory. Change the entry name as indicated.
3433 * IN: sdvp - Source directory containing the "old entry".
3434 * snm - Old entry name.
3435 * tdvp - Target directory to contain the "new entry".
3436 * tnm - New entry name.
3437 * cr - credentials of caller.
3438 * ct - caller context
3439 * flags - case flags
3441 * RETURN: 0 on success, error code on failure.
3444 * sdvp,tdvp - ctime|mtime updated
3448 zfs_rename(vnode_t
*sdvp
, char *snm
, vnode_t
*tdvp
, char *tnm
, cred_t
*cr
,
3449 caller_context_t
*ct
, int flags
)
3451 znode_t
*tdzp
, *szp
, *tzp
;
3452 znode_t
*sdzp
= VTOZ(sdvp
);
3453 zfsvfs_t
*zfsvfs
= sdzp
->z_zfsvfs
;
3456 zfs_dirlock_t
*sdl
, *tdl
;
3459 int cmp
, serr
, terr
;
3460 int error
= 0, rm_err
= 0;
3462 boolean_t waited
= B_FALSE
;
3465 ZFS_VERIFY_ZP(sdzp
);
3466 zilog
= zfsvfs
->z_log
;
3469 * Make sure we have the real vp for the target directory.
3471 if (VOP_REALVP(tdvp
, &realvp
, ct
) == 0)
3475 ZFS_VERIFY_ZP(tdzp
);
3478 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3479 * ctldir appear to have the same v_vfsp.
3481 if (tdzp
->z_zfsvfs
!= zfsvfs
|| zfsctl_is_node(tdvp
)) {
3483 return (SET_ERROR(EXDEV
));
3486 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
3487 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3489 return (SET_ERROR(EILSEQ
));
3492 if (flags
& FIGNORECASE
)
3501 * This is to prevent the creation of links into attribute space
3502 * by renaming a linked file into/outof an attribute directory.
3503 * See the comment in zfs_link() for why this is considered bad.
3505 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3507 return (SET_ERROR(EINVAL
));
3511 * Lock source and target directory entries. To prevent deadlock,
3512 * a lock ordering must be defined. We lock the directory with
3513 * the smallest object id first, or if it's a tie, the one with
3514 * the lexically first name.
3516 if (sdzp
->z_id
< tdzp
->z_id
) {
3518 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3522 * First compare the two name arguments without
3523 * considering any case folding.
3525 int nofold
= (zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3527 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3528 ASSERT(error
== 0 || !zfsvfs
->z_utf8
);
3531 * POSIX: "If the old argument and the new argument
3532 * both refer to links to the same existing file,
3533 * the rename() function shall return successfully
3534 * and perform no other action."
3540 * If the file system is case-folding, then we may
3541 * have some more checking to do. A case-folding file
3542 * system is either supporting mixed case sensitivity
3543 * access or is completely case-insensitive. Note
3544 * that the file system is always case preserving.
3546 * In mixed sensitivity mode case sensitive behavior
3547 * is the default. FIGNORECASE must be used to
3548 * explicitly request case insensitive behavior.
3550 * If the source and target names provided differ only
3551 * by case (e.g., a request to rename 'tim' to 'Tim'),
3552 * we will treat this as a special case in the
3553 * case-insensitive mode: as long as the source name
3554 * is an exact match, we will allow this to proceed as
3555 * a name-change request.
3557 if ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
3558 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
3559 flags
& FIGNORECASE
)) &&
3560 u8_strcmp(snm
, tnm
, 0, zfsvfs
->z_norm
, U8_UNICODE_LATEST
,
3563 * case preserving rename request, require exact
3572 * If the source and destination directories are the same, we should
3573 * grab the z_name_lock of that directory only once.
3577 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3581 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3582 ZEXISTS
| zflg
, NULL
, NULL
);
3583 terr
= zfs_dirent_lock(&tdl
,
3584 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3586 terr
= zfs_dirent_lock(&tdl
,
3587 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3588 serr
= zfs_dirent_lock(&sdl
,
3589 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3595 * Source entry invalid or not there.
3598 zfs_dirent_unlock(tdl
);
3604 rw_exit(&sdzp
->z_name_lock
);
3606 if (strcmp(snm
, "..") == 0)
3607 serr
= SET_ERROR(EINVAL
);
3612 zfs_dirent_unlock(sdl
);
3616 rw_exit(&sdzp
->z_name_lock
);
3618 if (strcmp(tnm
, "..") == 0)
3619 terr
= SET_ERROR(EINVAL
);
3625 * Must have write access at the source to remove the old entry
3626 * and write access at the target to create the new entry.
3627 * Note that if target and source are the same, this can be
3628 * done in a single check.
3631 if (error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
))
3634 if (ZTOV(szp
)->v_type
== VDIR
) {
3636 * Check to make sure rename is valid.
3637 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3639 if (error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
))
3644 * Does target exist?
3648 * Source and target must be the same type.
3650 if (ZTOV(szp
)->v_type
== VDIR
) {
3651 if (ZTOV(tzp
)->v_type
!= VDIR
) {
3652 error
= SET_ERROR(ENOTDIR
);
3656 if (ZTOV(tzp
)->v_type
== VDIR
) {
3657 error
= SET_ERROR(EISDIR
);
3662 * POSIX dictates that when the source and target
3663 * entries refer to the same file object, rename
3664 * must do nothing and exit without error.
3666 if (szp
->z_id
== tzp
->z_id
) {
3672 vnevent_pre_rename_src(ZTOV(szp
), sdvp
, snm
, ct
);
3674 vnevent_pre_rename_dest(ZTOV(tzp
), tdvp
, tnm
, ct
);
3677 * notify the target directory if it is not the same
3678 * as source directory.
3681 vnevent_pre_rename_dest_dir(tdvp
, ZTOV(szp
), tnm
, ct
);
3684 tx
= dmu_tx_create(zfsvfs
->z_os
);
3685 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3686 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3687 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3688 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3690 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3691 zfs_sa_upgrade_txholds(tx
, tdzp
);
3694 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3695 zfs_sa_upgrade_txholds(tx
, tzp
);
3698 zfs_sa_upgrade_txholds(tx
, szp
);
3699 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3700 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3703 zfs_rename_unlock(&zl
);
3704 zfs_dirent_unlock(sdl
);
3705 zfs_dirent_unlock(tdl
);
3708 rw_exit(&sdzp
->z_name_lock
);
3713 if (error
== ERESTART
) {
3724 if (tzp
) /* Attempt to remove the existing target */
3725 error
= rm_err
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3728 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3730 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3732 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
3733 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3736 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3738 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3739 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
3740 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3743 * Update path information for the target vnode
3745 vn_renamepath(tdvp
, ZTOV(szp
), tnm
,
3749 * At this point, we have successfully created
3750 * the target name, but have failed to remove
3751 * the source name. Since the create was done
3752 * with the ZRENAMING flag, there are
3753 * complications; for one, the link count is
3754 * wrong. The easiest way to deal with this
3755 * is to remove the newly created target, and
3756 * return the original error. This must
3757 * succeed; fortunately, it is very unlikely to
3758 * fail, since we just created it.
3760 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3761 ZRENAMING
, NULL
), ==, 0);
3768 if (tzp
&& rm_err
== 0)
3769 vnevent_rename_dest(ZTOV(tzp
), tdvp
, tnm
, ct
);
3772 vnevent_rename_src(ZTOV(szp
), sdvp
, snm
, ct
);
3773 /* notify the target dir if it is not the same as source dir */
3775 vnevent_rename_dest_dir(tdvp
, ct
);
3779 zfs_rename_unlock(&zl
);
3781 zfs_dirent_unlock(sdl
);
3782 zfs_dirent_unlock(tdl
);
3785 rw_exit(&sdzp
->z_name_lock
);
3792 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3793 zil_commit(zilog
, 0);
3800 * Insert the indicated symbolic reference entry into the directory.
3802 * IN: dvp - Directory to contain new symbolic link.
3803 * link - Name for new symlink entry.
3804 * vap - Attributes of new entry.
3805 * cr - credentials of caller.
3806 * ct - caller context
3807 * flags - case flags
3809 * RETURN: 0 on success, error code on failure.
3812 * dvp - ctime|mtime updated
3816 zfs_symlink(vnode_t
*dvp
, char *name
, vattr_t
*vap
, char *link
, cred_t
*cr
,
3817 caller_context_t
*ct
, int flags
)
3819 znode_t
*zp
, *dzp
= VTOZ(dvp
);
3822 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
3824 uint64_t len
= strlen(link
);
3827 zfs_acl_ids_t acl_ids
;
3828 boolean_t fuid_dirtied
;
3829 uint64_t txtype
= TX_SYMLINK
;
3830 boolean_t waited
= B_FALSE
;
3832 ASSERT(vap
->va_type
== VLNK
);
3836 zilog
= zfsvfs
->z_log
;
3838 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
3839 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3841 return (SET_ERROR(EILSEQ
));
3843 if (flags
& FIGNORECASE
)
3846 if (len
> MAXPATHLEN
) {
3848 return (SET_ERROR(ENAMETOOLONG
));
3851 if ((error
= zfs_acl_ids_create(dzp
, 0,
3852 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3858 * Attempt to lock directory; fail if entry already exists.
3860 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3862 zfs_acl_ids_free(&acl_ids
);
3867 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
3868 zfs_acl_ids_free(&acl_ids
);
3869 zfs_dirent_unlock(dl
);
3874 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
)) {
3875 zfs_acl_ids_free(&acl_ids
);
3876 zfs_dirent_unlock(dl
);
3878 return (SET_ERROR(EDQUOT
));
3880 tx
= dmu_tx_create(zfsvfs
->z_os
);
3881 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3882 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3883 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3884 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3885 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3886 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3887 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3888 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3889 acl_ids
.z_aclp
->z_acl_bytes
);
3892 zfs_fuid_txhold(zfsvfs
, tx
);
3893 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3895 zfs_dirent_unlock(dl
);
3896 if (error
== ERESTART
) {
3902 zfs_acl_ids_free(&acl_ids
);
3909 * Create a new object for the symlink.
3910 * for version 4 ZPL datsets the symlink will be an SA attribute
3912 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3915 zfs_fuid_sync(zfsvfs
, tx
);
3917 mutex_enter(&zp
->z_lock
);
3919 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zfsvfs
),
3922 zfs_sa_symlink(zp
, link
, len
, tx
);
3923 mutex_exit(&zp
->z_lock
);
3926 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
3927 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3929 * Insert the new object into the directory.
3931 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3933 if (flags
& FIGNORECASE
)
3935 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3937 zfs_acl_ids_free(&acl_ids
);
3941 zfs_dirent_unlock(dl
);
3945 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3946 zil_commit(zilog
, 0);
3953 * Return, in the buffer contained in the provided uio structure,
3954 * the symbolic path referred to by vp.
3956 * IN: vp - vnode of symbolic link.
3957 * uio - structure to contain the link path.
3958 * cr - credentials of caller.
3959 * ct - caller context
3961 * OUT: uio - structure containing the link path.
3963 * RETURN: 0 on success, error code on failure.
3966 * vp - atime updated
3970 zfs_readlink(vnode_t
*vp
, uio_t
*uio
, cred_t
*cr
, caller_context_t
*ct
)
3972 znode_t
*zp
= VTOZ(vp
);
3973 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3979 mutex_enter(&zp
->z_lock
);
3981 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3982 SA_ZPL_SYMLINK(zfsvfs
), uio
);
3984 error
= zfs_sa_readlink(zp
, uio
);
3985 mutex_exit(&zp
->z_lock
);
3987 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
3994 * Insert a new entry into directory tdvp referencing svp.
3996 * IN: tdvp - Directory to contain new entry.
3997 * svp - vnode of new entry.
3998 * name - name of new entry.
3999 * cr - credentials of caller.
4000 * ct - caller context
4002 * RETURN: 0 on success, error code on failure.
4005 * tdvp - ctime|mtime updated
4006 * svp - ctime updated
4010 zfs_link(vnode_t
*tdvp
, vnode_t
*svp
, char *name
, cred_t
*cr
,
4011 caller_context_t
*ct
, int flags
)
4013 znode_t
*dzp
= VTOZ(tdvp
);
4015 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
4024 boolean_t waited
= B_FALSE
;
4026 ASSERT(tdvp
->v_type
== VDIR
);
4030 zilog
= zfsvfs
->z_log
;
4032 if (VOP_REALVP(svp
, &realvp
, ct
) == 0)
4036 * POSIX dictates that we return EPERM here.
4037 * Better choices include ENOTSUP or EISDIR.
4039 if (svp
->v_type
== VDIR
) {
4041 return (SET_ERROR(EPERM
));
4048 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
4049 * ctldir appear to have the same v_vfsp.
4051 if (szp
->z_zfsvfs
!= zfsvfs
|| zfsctl_is_node(svp
)) {
4053 return (SET_ERROR(EXDEV
));
4056 /* Prevent links to .zfs/shares files */
4058 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
4059 &parent
, sizeof (uint64_t))) != 0) {
4063 if (parent
== zfsvfs
->z_shares_dir
) {
4065 return (SET_ERROR(EPERM
));
4068 if (zfsvfs
->z_utf8
&& u8_validate(name
,
4069 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
4071 return (SET_ERROR(EILSEQ
));
4073 if (flags
& FIGNORECASE
)
4077 * We do not support links between attributes and non-attributes
4078 * because of the potential security risk of creating links
4079 * into "normal" file space in order to circumvent restrictions
4080 * imposed in attribute space.
4082 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
4084 return (SET_ERROR(EINVAL
));
4088 owner
= zfs_fuid_map_id(zfsvfs
, szp
->z_uid
, cr
, ZFS_OWNER
);
4089 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
4091 return (SET_ERROR(EPERM
));
4094 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
4101 * Attempt to lock directory; fail if entry already exists.
4103 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
4109 tx
= dmu_tx_create(zfsvfs
->z_os
);
4110 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
4111 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
4112 zfs_sa_upgrade_txholds(tx
, szp
);
4113 zfs_sa_upgrade_txholds(tx
, dzp
);
4114 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
4116 zfs_dirent_unlock(dl
);
4117 if (error
== ERESTART
) {
4128 error
= zfs_link_create(dl
, szp
, tx
, 0);
4131 uint64_t txtype
= TX_LINK
;
4132 if (flags
& FIGNORECASE
)
4134 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
4139 zfs_dirent_unlock(dl
);
4142 vnevent_link(svp
, ct
);
4145 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4146 zil_commit(zilog
, 0);
4153 * zfs_null_putapage() is used when the file system has been force
4154 * unmounted. It just drops the pages.
4158 zfs_null_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
,
4159 size_t *lenp
, int flags
, cred_t
*cr
)
4161 pvn_write_done(pp
, B_INVAL
|B_FORCE
|B_ERROR
);
4166 * Push a page out to disk, klustering if possible.
4168 * IN: vp - file to push page to.
4169 * pp - page to push.
4170 * flags - additional flags.
4171 * cr - credentials of caller.
4173 * OUT: offp - start of range pushed.
4174 * lenp - len of range pushed.
4176 * RETURN: 0 on success, error code on failure.
4178 * NOTE: callers must have locked the page to be pushed. On
4179 * exit, the page (and all other pages in the kluster) must be
4184 zfs_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
,
4185 size_t *lenp
, int flags
, cred_t
*cr
)
4187 znode_t
*zp
= VTOZ(vp
);
4188 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4190 u_offset_t off
, koff
;
4197 * If our blocksize is bigger than the page size, try to kluster
4198 * multiple pages so that we write a full block (thus avoiding
4199 * a read-modify-write).
4201 if (off
< zp
->z_size
&& zp
->z_blksz
> PAGESIZE
) {
4202 klen
= P2ROUNDUP((ulong_t
)zp
->z_blksz
, PAGESIZE
);
4203 koff
= ISP2(klen
) ? P2ALIGN(off
, (u_offset_t
)klen
) : 0;
4204 ASSERT(koff
<= zp
->z_size
);
4205 if (koff
+ klen
> zp
->z_size
)
4206 klen
= P2ROUNDUP(zp
->z_size
- koff
, (uint64_t)PAGESIZE
);
4207 pp
= pvn_write_kluster(vp
, pp
, &off
, &len
, koff
, klen
, flags
);
4209 ASSERT3U(btop(len
), ==, btopr(len
));
4212 * Can't push pages past end-of-file.
4214 if (off
>= zp
->z_size
) {
4215 /* ignore all pages */
4218 } else if (off
+ len
> zp
->z_size
) {
4219 int npages
= btopr(zp
->z_size
- off
);
4222 page_list_break(&pp
, &trunc
, npages
);
4223 /* ignore pages past end of file */
4225 pvn_write_done(trunc
, flags
);
4226 len
= zp
->z_size
- off
;
4229 if (zfs_owner_overquota(zfsvfs
, zp
, B_FALSE
) ||
4230 zfs_owner_overquota(zfsvfs
, zp
, B_TRUE
)) {
4231 err
= SET_ERROR(EDQUOT
);
4234 tx
= dmu_tx_create(zfsvfs
->z_os
);
4235 dmu_tx_hold_write(tx
, zp
->z_id
, off
, len
);
4237 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4238 zfs_sa_upgrade_txholds(tx
, zp
);
4239 err
= dmu_tx_assign(tx
, TXG_WAIT
);
4245 if (zp
->z_blksz
<= PAGESIZE
) {
4246 caddr_t va
= zfs_map_page(pp
, S_READ
);
4247 ASSERT3U(len
, <=, PAGESIZE
);
4248 dmu_write(zfsvfs
->z_os
, zp
->z_id
, off
, len
, va
, tx
);
4249 zfs_unmap_page(pp
, va
);
4251 err
= dmu_write_pages(zfsvfs
->z_os
, zp
->z_id
, off
, len
, pp
, tx
);
4255 uint64_t mtime
[2], ctime
[2];
4256 sa_bulk_attr_t bulk
[3];
4259 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
4261 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
4263 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
4265 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
4267 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
4269 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, off
, len
, 0);
4274 pvn_write_done(pp
, (err
? B_ERROR
: 0) | flags
);
4284 * Copy the portion of the file indicated from pages into the file.
4285 * The pages are stored in a page list attached to the files vnode.
4287 * IN: vp - vnode of file to push page data to.
4288 * off - position in file to put data.
4289 * len - amount of data to write.
4290 * flags - flags to control the operation.
4291 * cr - credentials of caller.
4292 * ct - caller context.
4294 * RETURN: 0 on success, error code on failure.
4297 * vp - ctime|mtime updated
4301 zfs_putpage(vnode_t
*vp
, offset_t off
, size_t len
, int flags
, cred_t
*cr
,
4302 caller_context_t
*ct
)
4304 znode_t
*zp
= VTOZ(vp
);
4305 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4317 * There's nothing to do if no data is cached.
4319 if (!vn_has_cached_data(vp
)) {
4325 * Align this request to the file block size in case we kluster.
4326 * XXX - this can result in pretty aggresive locking, which can
4327 * impact simultanious read/write access. One option might be
4328 * to break up long requests (len == 0) into block-by-block
4329 * operations to get narrower locking.
4331 blksz
= zp
->z_blksz
;
4333 io_off
= P2ALIGN_TYPED(off
, blksz
, u_offset_t
);
4336 if (len
> 0 && ISP2(blksz
))
4337 io_len
= P2ROUNDUP_TYPED(len
+ (off
- io_off
), blksz
, size_t);
4343 * Search the entire vp list for pages >= io_off.
4345 rl
= zfs_range_lock(zp
, io_off
, UINT64_MAX
, RL_WRITER
);
4346 error
= pvn_vplist_dirty(vp
, io_off
, zfs_putapage
, flags
, cr
);
4349 rl
= zfs_range_lock(zp
, io_off
, io_len
, RL_WRITER
);
4351 if (off
> zp
->z_size
) {
4352 /* past end of file */
4353 zfs_range_unlock(rl
);
4358 len
= MIN(io_len
, P2ROUNDUP(zp
->z_size
, PAGESIZE
) - io_off
);
4360 for (off
= io_off
; io_off
< off
+ len
; io_off
+= io_len
) {
4361 if ((flags
& B_INVAL
) || ((flags
& B_ASYNC
) == 0)) {
4362 pp
= page_lookup(vp
, io_off
,
4363 (flags
& (B_INVAL
| B_FREE
)) ? SE_EXCL
: SE_SHARED
);
4365 pp
= page_lookup_nowait(vp
, io_off
,
4366 (flags
& B_FREE
) ? SE_EXCL
: SE_SHARED
);
4369 if (pp
!= NULL
&& pvn_getdirty(pp
, flags
)) {
4373 * Found a dirty page to push
4375 err
= zfs_putapage(vp
, pp
, &io_off
, &io_len
, flags
, cr
);
4383 zfs_range_unlock(rl
);
4384 if ((flags
& B_ASYNC
) == 0 || zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4385 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
4392 zfs_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
4394 znode_t
*zp
= VTOZ(vp
);
4395 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4398 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_READER
);
4399 if (zp
->z_sa_hdl
== NULL
) {
4401 * The fs has been unmounted, or we did a
4402 * suspend/resume and this file no longer exists.
4404 if (vn_has_cached_data(vp
)) {
4405 (void) pvn_vplist_dirty(vp
, 0, zfs_null_putapage
,
4409 mutex_enter(&zp
->z_lock
);
4410 mutex_enter(&vp
->v_lock
);
4411 ASSERT(vp
->v_count
== 1);
4413 mutex_exit(&vp
->v_lock
);
4414 mutex_exit(&zp
->z_lock
);
4415 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
4421 * Attempt to push any data in the page cache. If this fails
4422 * we will get kicked out later in zfs_zinactive().
4424 if (vn_has_cached_data(vp
)) {
4425 (void) pvn_vplist_dirty(vp
, 0, zfs_putapage
, B_INVAL
|B_ASYNC
,
4429 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4430 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
4432 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4433 zfs_sa_upgrade_txholds(tx
, zp
);
4434 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4438 mutex_enter(&zp
->z_lock
);
4439 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
),
4440 (void *)&zp
->z_atime
, sizeof (zp
->z_atime
), tx
);
4441 zp
->z_atime_dirty
= 0;
4442 mutex_exit(&zp
->z_lock
);
4448 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
4452 * Bounds-check the seek operation.
4454 * IN: vp - vnode seeking within
4455 * ooff - old file offset
4456 * noffp - pointer to new file offset
4457 * ct - caller context
4459 * RETURN: 0 on success, EINVAL if new offset invalid.
4463 zfs_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
,
4464 caller_context_t
*ct
)
4466 if (vp
->v_type
== VDIR
)
4468 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4472 * Pre-filter the generic locking function to trap attempts to place
4473 * a mandatory lock on a memory mapped file.
4476 zfs_frlock(vnode_t
*vp
, int cmd
, flock64_t
*bfp
, int flag
, offset_t offset
,
4477 flk_callback_t
*flk_cbp
, cred_t
*cr
, caller_context_t
*ct
)
4479 znode_t
*zp
= VTOZ(vp
);
4480 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4486 * We are following the UFS semantics with respect to mapcnt
4487 * here: If we see that the file is mapped already, then we will
4488 * return an error, but we don't worry about races between this
4489 * function and zfs_map().
4491 if (zp
->z_mapcnt
> 0 && MANDMODE(zp
->z_mode
)) {
4493 return (SET_ERROR(EAGAIN
));
4496 return (fs_frlock(vp
, cmd
, bfp
, flag
, offset
, flk_cbp
, cr
, ct
));
4500 * If we can't find a page in the cache, we will create a new page
4501 * and fill it with file data. For efficiency, we may try to fill
4502 * multiple pages at once (klustering) to fill up the supplied page
4503 * list. Note that the pages to be filled are held with an exclusive
4504 * lock to prevent access by other threads while they are being filled.
4507 zfs_fillpage(vnode_t
*vp
, u_offset_t off
, struct seg
*seg
,
4508 caddr_t addr
, page_t
*pl
[], size_t plsz
, enum seg_rw rw
)
4510 znode_t
*zp
= VTOZ(vp
);
4511 page_t
*pp
, *cur_pp
;
4512 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
4513 u_offset_t io_off
, total
;
4517 if (plsz
== PAGESIZE
|| zp
->z_blksz
<= PAGESIZE
) {
4519 * We only have a single page, don't bother klustering
4523 pp
= page_create_va(vp
, io_off
, io_len
,
4524 PG_EXCL
| PG_WAIT
, seg
, addr
);
4527 * Try to find enough pages to fill the page list
4529 pp
= pvn_read_kluster(vp
, off
, seg
, addr
, &io_off
,
4530 &io_len
, off
, plsz
, 0);
4534 * The page already exists, nothing to do here.
4541 * Fill the pages in the kluster.
4544 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4547 ASSERT3U(io_off
, ==, cur_pp
->p_offset
);
4548 va
= zfs_map_page(cur_pp
, S_WRITE
);
4549 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4551 zfs_unmap_page(cur_pp
, va
);
4553 /* On error, toss the entire kluster */
4554 pvn_read_done(pp
, B_ERROR
);
4555 /* convert checksum errors into IO errors */
4557 err
= SET_ERROR(EIO
);
4560 cur_pp
= cur_pp
->p_next
;
4564 * Fill in the page list array from the kluster starting
4565 * from the desired offset `off'.
4566 * NOTE: the page list will always be null terminated.
4568 pvn_plist_init(pp
, pl
, plsz
, off
, io_len
, rw
);
4569 ASSERT(pl
== NULL
|| (*pl
)->p_offset
== off
);
4575 * Return pointers to the pages for the file region [off, off + len]
4576 * in the pl array. If plsz is greater than len, this function may
4577 * also return page pointers from after the specified region
4578 * (i.e. the region [off, off + plsz]). These additional pages are
4579 * only returned if they are already in the cache, or were created as
4580 * part of a klustered read.
4582 * IN: vp - vnode of file to get data from.
4583 * off - position in file to get data from.
4584 * len - amount of data to retrieve.
4585 * plsz - length of provided page list.
4586 * seg - segment to obtain pages for.
4587 * addr - virtual address of fault.
4588 * rw - mode of created pages.
4589 * cr - credentials of caller.
4590 * ct - caller context.
4592 * OUT: protp - protection mode of created pages.
4593 * pl - list of pages created.
4595 * RETURN: 0 on success, error code on failure.
4598 * vp - atime updated
4602 zfs_getpage(vnode_t
*vp
, offset_t off
, size_t len
, uint_t
*protp
,
4603 page_t
*pl
[], size_t plsz
, struct seg
*seg
, caddr_t addr
,
4604 enum seg_rw rw
, cred_t
*cr
, caller_context_t
*ct
)
4606 znode_t
*zp
= VTOZ(vp
);
4607 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4611 /* we do our own caching, faultahead is unnecessary */
4614 else if (len
> plsz
)
4617 len
= P2ROUNDUP(len
, PAGESIZE
);
4618 ASSERT(plsz
>= len
);
4627 * Loop through the requested range [off, off + len) looking
4628 * for pages. If we don't find a page, we will need to create
4629 * a new page and fill it with data from the file.
4632 if (*pl
= page_lookup(vp
, off
, SE_SHARED
))
4634 else if (err
= zfs_fillpage(vp
, off
, seg
, addr
, pl
, plsz
, rw
))
4637 ASSERT3U((*pl
)->p_offset
, ==, off
);
4641 ASSERT3U(len
, >=, PAGESIZE
);
4644 ASSERT3U(plsz
, >=, PAGESIZE
);
4651 * Fill out the page array with any pages already in the cache.
4654 (*pl
++ = page_lookup_nowait(vp
, off
, SE_SHARED
))) {
4661 * Release any pages we have previously locked.
4666 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
4676 * Request a memory map for a section of a file. This code interacts
4677 * with common code and the VM system as follows:
4679 * - common code calls mmap(), which ends up in smmap_common()
4680 * - this calls VOP_MAP(), which takes you into (say) zfs
4681 * - zfs_map() calls as_map(), passing segvn_create() as the callback
4682 * - segvn_create() creates the new segment and calls VOP_ADDMAP()
4683 * - zfs_addmap() updates z_mapcnt
4687 zfs_map(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t
*addrp
,
4688 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
4689 caller_context_t
*ct
)
4691 znode_t
*zp
= VTOZ(vp
);
4692 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4693 segvn_crargs_t vn_a
;
4700 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
4703 if ((prot
& PROT_WRITE
) && (zp
->z_pflags
&
4704 (ZFS_IMMUTABLE
| ZFS_APPENDONLY
))) {
4706 return (SET_ERROR(EPERM
));
4709 if ((prot
& (PROT_READ
| PROT_EXEC
)) &&
4710 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4712 return (SET_ERROR(EACCES
));
4715 if (vp
->v_flag
& VNOMAP
) {
4717 return (SET_ERROR(ENOSYS
));
4720 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4722 return (SET_ERROR(ENXIO
));
4725 if (vp
->v_type
!= VREG
) {
4727 return (SET_ERROR(ENODEV
));
4731 * If file is locked, disallow mapping.
4733 if (MANDMODE(zp
->z_mode
) && vn_has_flocks(vp
)) {
4735 return (SET_ERROR(EAGAIN
));
4739 error
= choose_addr(as
, addrp
, len
, off
, ADDR_VACALIGN
, flags
);
4747 vn_a
.offset
= (u_offset_t
)off
;
4748 vn_a
.type
= flags
& MAP_TYPE
;
4750 vn_a
.maxprot
= maxprot
;
4753 vn_a
.flags
= flags
& ~MAP_TYPE
;
4755 vn_a
.lgrp_mem_policy_flags
= 0;
4757 error
= as_map(as
, *addrp
, len
, segvn_create
, &vn_a
);
4766 zfs_addmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
4767 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
4768 caller_context_t
*ct
)
4770 uint64_t pages
= btopr(len
);
4772 atomic_add_64(&VTOZ(vp
)->z_mapcnt
, pages
);
4777 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4778 * more accurate mtime for the associated file. Since we don't have a way of
4779 * detecting when the data was actually modified, we have to resort to
4780 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4781 * last page is pushed. The problem occurs when the msync() call is omitted,
4782 * which by far the most common case:
4790 * putpage() via fsflush
4792 * If we wait until fsflush to come along, we can have a modification time that
4793 * is some arbitrary point in the future. In order to prevent this in the
4794 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4799 zfs_delmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
4800 size_t len
, uint_t prot
, uint_t maxprot
, uint_t flags
, cred_t
*cr
,
4801 caller_context_t
*ct
)
4803 uint64_t pages
= btopr(len
);
4805 ASSERT3U(VTOZ(vp
)->z_mapcnt
, >=, pages
);
4806 atomic_add_64(&VTOZ(vp
)->z_mapcnt
, -pages
);
4808 if ((flags
& MAP_SHARED
) && (prot
& PROT_WRITE
) &&
4809 vn_has_cached_data(vp
))
4810 (void) VOP_PUTPAGE(vp
, off
, len
, B_ASYNC
, cr
, ct
);
4816 * Free or allocate space in a file. Currently, this function only
4817 * supports the `F_FREESP' command. However, this command is somewhat
4818 * misnamed, as its functionality includes the ability to allocate as
4819 * well as free space.
4821 * IN: vp - vnode of file to free data in.
4822 * cmd - action to take (only F_FREESP supported).
4823 * bfp - section of file to free/alloc.
4824 * flag - current file open mode flags.
4825 * offset - current file offset.
4826 * cr - credentials of caller [UNUSED].
4827 * ct - caller context.
4829 * RETURN: 0 on success, error code on failure.
4832 * vp - ctime|mtime updated
4836 zfs_space(vnode_t
*vp
, int cmd
, flock64_t
*bfp
, int flag
,
4837 offset_t offset
, cred_t
*cr
, caller_context_t
*ct
)
4839 znode_t
*zp
= VTOZ(vp
);
4840 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4847 if (cmd
!= F_FREESP
) {
4849 return (SET_ERROR(EINVAL
));
4853 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
4854 * callers might not be able to detect properly that we are read-only,
4855 * so check it explicitly here.
4857 if (zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) {
4859 return (SET_ERROR(EROFS
));
4862 if (error
= convoff(vp
, bfp
, 0, offset
)) {
4867 if (bfp
->l_len
< 0) {
4869 return (SET_ERROR(EINVAL
));
4873 len
= bfp
->l_len
; /* 0 means from off to end of file */
4875 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4877 if (error
== 0 && off
== 0 && len
== 0)
4878 vnevent_truncate(ZTOV(zp
), ct
);
4886 zfs_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
4888 znode_t
*zp
= VTOZ(vp
);
4889 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4892 uint64_t object
= zp
->z_id
;
4899 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
),
4900 &gen64
, sizeof (uint64_t))) != 0) {
4905 gen
= (uint32_t)gen64
;
4907 size
= (zfsvfs
->z_parent
!= zfsvfs
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4908 if (fidp
->fid_len
< size
) {
4909 fidp
->fid_len
= size
;
4911 return (SET_ERROR(ENOSPC
));
4914 zfid
= (zfid_short_t
*)fidp
;
4916 zfid
->zf_len
= size
;
4918 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4919 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4921 /* Must have a non-zero generation number to distinguish from .zfs */
4924 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4925 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4927 if (size
== LONG_FID_LEN
) {
4928 uint64_t objsetid
= dmu_objset_id(zfsvfs
->z_os
);
4931 zlfid
= (zfid_long_t
*)fidp
;
4933 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4934 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4936 /* XXX - this should be the generation number for the objset */
4937 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4938 zlfid
->zf_setgen
[i
] = 0;
4946 zfs_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
4947 caller_context_t
*ct
)
4959 case _PC_FILESIZEBITS
:
4963 case _PC_XATTR_EXISTS
:
4965 zfsvfs
= zp
->z_zfsvfs
;
4969 error
= zfs_dirent_lock(&dl
, zp
, "", &xzp
,
4970 ZXATTR
| ZEXISTS
| ZSHARED
, NULL
, NULL
);
4972 zfs_dirent_unlock(dl
);
4973 if (!zfs_dirempty(xzp
))
4976 } else if (error
== ENOENT
) {
4978 * If there aren't extended attributes, it's the
4979 * same as having zero of them.
4986 case _PC_SATTR_ENABLED
:
4987 case _PC_SATTR_EXISTS
:
4988 *valp
= vfs_has_feature(vp
->v_vfsp
, VFSFT_SYSATTR_VIEWS
) &&
4989 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
);
4992 case _PC_ACCESS_FILTERING
:
4993 *valp
= vfs_has_feature(vp
->v_vfsp
, VFSFT_ACCESS_FILTER
) &&
4997 case _PC_ACL_ENABLED
:
4998 *valp
= _ACL_ACE_ENABLED
;
5001 case _PC_MIN_HOLE_SIZE
:
5002 *valp
= (ulong_t
)SPA_MINBLOCKSIZE
;
5005 case _PC_TIMESTAMP_RESOLUTION
:
5006 /* nanosecond timestamp resolution */
5011 return (fs_pathconf(vp
, cmd
, valp
, cr
, ct
));
5017 zfs_getsecattr(vnode_t
*vp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
,
5018 caller_context_t
*ct
)
5020 znode_t
*zp
= VTOZ(vp
);
5021 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
5023 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
5027 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
5035 zfs_setsecattr(vnode_t
*vp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
,
5036 caller_context_t
*ct
)
5038 znode_t
*zp
= VTOZ(vp
);
5039 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
5041 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
5042 zilog_t
*zilog
= zfsvfs
->z_log
;
5047 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
5049 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
5050 zil_commit(zilog
, 0);
5057 * The smallest read we may consider to loan out an arcbuf.
5058 * This must be a power of 2.
5060 int zcr_blksz_min
= (1 << 10); /* 1K */
5062 * If set to less than the file block size, allow loaning out of an
5063 * arcbuf for a partial block read. This must be a power of 2.
5065 int zcr_blksz_max
= (1 << 17); /* 128K */
5069 zfs_reqzcbuf(vnode_t
*vp
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
,
5070 caller_context_t
*ct
)
5072 znode_t
*zp
= VTOZ(vp
);
5073 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
5074 int max_blksz
= zfsvfs
->z_max_blksz
;
5075 uio_t
*uio
= &xuio
->xu_uio
;
5076 ssize_t size
= uio
->uio_resid
;
5077 offset_t offset
= uio
->uio_loffset
;
5082 int preamble
, postamble
;
5084 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
5085 return (SET_ERROR(EINVAL
));
5092 * Loan out an arc_buf for write if write size is bigger than
5093 * max_blksz, and the file's block size is also max_blksz.
5096 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
5098 return (SET_ERROR(EINVAL
));
5101 * Caller requests buffers for write before knowing where the
5102 * write offset might be (e.g. NFS TCP write).
5107 preamble
= P2PHASE(offset
, blksz
);
5109 preamble
= blksz
- preamble
;
5114 postamble
= P2PHASE(size
, blksz
);
5117 fullblk
= size
/ blksz
;
5118 (void) dmu_xuio_init(xuio
,
5119 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
5120 DTRACE_PROBE3(zfs_reqzcbuf_align
, int, preamble
,
5121 int, postamble
, int,
5122 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
5125 * Have to fix iov base/len for partial buffers. They
5126 * currently represent full arc_buf's.
5129 /* data begins in the middle of the arc_buf */
5130 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5133 (void) dmu_xuio_add(xuio
, abuf
,
5134 blksz
- preamble
, preamble
);
5137 for (i
= 0; i
< fullblk
; i
++) {
5138 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5141 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
5145 /* data ends in the middle of the arc_buf */
5146 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5149 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
5154 * Loan out an arc_buf for read if the read size is larger than
5155 * the current file block size. Block alignment is not
5156 * considered. Partial arc_buf will be loaned out for read.
5158 blksz
= zp
->z_blksz
;
5159 if (blksz
< zcr_blksz_min
)
5160 blksz
= zcr_blksz_min
;
5161 if (blksz
> zcr_blksz_max
)
5162 blksz
= zcr_blksz_max
;
5163 /* avoid potential complexity of dealing with it */
5164 if (blksz
> max_blksz
) {
5166 return (SET_ERROR(EINVAL
));
5169 maxsize
= zp
->z_size
- uio
->uio_loffset
;
5173 if (size
< blksz
|| vn_has_cached_data(vp
)) {
5175 return (SET_ERROR(EINVAL
));
5180 return (SET_ERROR(EINVAL
));
5183 uio
->uio_extflg
= UIO_XUIO
;
5184 XUIO_XUZC_RW(xuio
) = ioflag
;
5191 zfs_retzcbuf(vnode_t
*vp
, xuio_t
*xuio
, cred_t
*cr
, caller_context_t
*ct
)
5195 int ioflag
= XUIO_XUZC_RW(xuio
);
5197 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
5199 i
= dmu_xuio_cnt(xuio
);
5201 abuf
= dmu_xuio_arcbuf(xuio
, i
);
5203 * if abuf == NULL, it must be a write buffer
5204 * that has been returned in zfs_write().
5207 dmu_return_arcbuf(abuf
);
5208 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
5211 dmu_xuio_fini(xuio
);
5216 * Predeclare these here so that the compiler assumes that
5217 * this is an "old style" function declaration that does
5218 * not include arguments => we won't get type mismatch errors
5219 * in the initializations that follow.
5221 static int zfs_inval();
5222 static int zfs_isdir();
5227 return (SET_ERROR(EINVAL
));
5233 return (SET_ERROR(EISDIR
));
5236 * Directory vnode operations template
5238 vnodeops_t
*zfs_dvnodeops
;
5239 const fs_operation_def_t zfs_dvnodeops_template
[] = {
5240 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
5241 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
5242 VOPNAME_READ
, { .error
= zfs_isdir
},
5243 VOPNAME_WRITE
, { .error
= zfs_isdir
},
5244 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
5245 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
5246 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
5247 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
5248 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
5249 VOPNAME_CREATE
, { .vop_create
= zfs_create
},
5250 VOPNAME_REMOVE
, { .vop_remove
= zfs_remove
},
5251 VOPNAME_LINK
, { .vop_link
= zfs_link
},
5252 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
5253 VOPNAME_MKDIR
, { .vop_mkdir
= zfs_mkdir
},
5254 VOPNAME_RMDIR
, { .vop_rmdir
= zfs_rmdir
},
5255 VOPNAME_READDIR
, { .vop_readdir
= zfs_readdir
},
5256 VOPNAME_SYMLINK
, { .vop_symlink
= zfs_symlink
},
5257 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
5258 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5259 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
5260 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
5261 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
5262 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
5263 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
5264 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
5269 * Regular file vnode operations template
5271 vnodeops_t
*zfs_fvnodeops
;
5272 const fs_operation_def_t zfs_fvnodeops_template
[] = {
5273 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
5274 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
5275 VOPNAME_READ
, { .vop_read
= zfs_read
},
5276 VOPNAME_WRITE
, { .vop_write
= zfs_write
},
5277 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
5278 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
5279 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
5280 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
5281 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
5282 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
5283 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
5284 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5285 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
5286 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
5287 VOPNAME_FRLOCK
, { .vop_frlock
= zfs_frlock
},
5288 VOPNAME_SPACE
, { .vop_space
= zfs_space
},
5289 VOPNAME_GETPAGE
, { .vop_getpage
= zfs_getpage
},
5290 VOPNAME_PUTPAGE
, { .vop_putpage
= zfs_putpage
},
5291 VOPNAME_MAP
, { .vop_map
= zfs_map
},
5292 VOPNAME_ADDMAP
, { .vop_addmap
= zfs_addmap
},
5293 VOPNAME_DELMAP
, { .vop_delmap
= zfs_delmap
},
5294 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
5295 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
5296 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
5297 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
5298 VOPNAME_REQZCBUF
, { .vop_reqzcbuf
= zfs_reqzcbuf
},
5299 VOPNAME_RETZCBUF
, { .vop_retzcbuf
= zfs_retzcbuf
},
5304 * Symbolic link vnode operations template
5306 vnodeops_t
*zfs_symvnodeops
;
5307 const fs_operation_def_t zfs_symvnodeops_template
[] = {
5308 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
5309 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
5310 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
5311 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
5312 VOPNAME_READLINK
, { .vop_readlink
= zfs_readlink
},
5313 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5314 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
5315 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
5316 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
5321 * special share hidden files vnode operations template
5323 vnodeops_t
*zfs_sharevnodeops
;
5324 const fs_operation_def_t zfs_sharevnodeops_template
[] = {
5325 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
5326 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
5327 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5328 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
5329 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
5330 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
5331 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
5332 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
5337 * Extended attribute directory vnode operations template
5339 * This template is identical to the directory vnodes
5340 * operation template except for restricted operations:
5344 * Note that there are other restrictions embedded in:
5345 * zfs_create() - restrict type to VREG
5346 * zfs_link() - no links into/out of attribute space
5347 * zfs_rename() - no moves into/out of attribute space
5349 vnodeops_t
*zfs_xdvnodeops
;
5350 const fs_operation_def_t zfs_xdvnodeops_template
[] = {
5351 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
5352 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
5353 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
5354 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
5355 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
5356 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
5357 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
5358 VOPNAME_CREATE
, { .vop_create
= zfs_create
},
5359 VOPNAME_REMOVE
, { .vop_remove
= zfs_remove
},
5360 VOPNAME_LINK
, { .vop_link
= zfs_link
},
5361 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
5362 VOPNAME_MKDIR
, { .error
= zfs_inval
},
5363 VOPNAME_RMDIR
, { .vop_rmdir
= zfs_rmdir
},
5364 VOPNAME_READDIR
, { .vop_readdir
= zfs_readdir
},
5365 VOPNAME_SYMLINK
, { .error
= zfs_inval
},
5366 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
5367 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5368 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
5369 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
5370 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
5371 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
5372 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
5373 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
5378 * Error vnode operations template
5380 vnodeops_t
*zfs_evnodeops
;
5381 const fs_operation_def_t zfs_evnodeops_template
[] = {
5382 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
5383 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},