4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Portions Copyright 2007 Jeremy Teo */
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
35 #include <sys/vfs_opreg.h>
36 #include <sys/vnode.h>
40 #include <sys/taskq.h>
42 #include <sys/vmsystm.h>
43 #include <sys/atomic.h>
45 #include <vm/seg_vn.h>
49 #include <vm/seg_kpm.h>
51 #include <sys/pathname.h>
52 #include <sys/cmn_err.h>
53 #include <sys/errno.h>
54 #include <sys/unistd.h>
55 #include <sys/zfs_dir.h>
56 #include <sys/zfs_acl.h>
57 #include <sys/zfs_ioctl.h>
58 #include <sys/fs/zfs.h>
64 #include <sys/dirent.h>
65 #include <sys/policy.h>
66 #include <sys/sunddi.h>
67 #include <sys/filio.h>
69 #include "fs/fs_subr.h"
70 #include <sys/zfs_ctldir.h>
71 #include <sys/zfs_fuid.h>
73 #include <sys/zfs_rlock.h>
74 #include <sys/extdirent.h>
75 #include <sys/kidmap.h>
76 #include <sys/cred_impl.h>
82 * Each vnode op performs some logical unit of work. To do this, the ZPL must
83 * properly lock its in-core state, create a DMU transaction, do the work,
84 * record this work in the intent log (ZIL), commit the DMU transaction,
85 * and wait for the intent log to commit if it is a synchronous operation.
86 * Moreover, the vnode ops must work in both normal and log replay context.
87 * The ordering of events is important to avoid deadlocks and references
88 * to freed memory. The example below illustrates the following Big Rules:
90 * (1) A check must be made in each zfs thread for a mounted file system.
91 * This is done avoiding races using ZFS_ENTER(zfsvfs).
92 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
94 * can return EIO from the calling function.
96 * (2) VN_RELE() should always be the last thing except for zil_commit()
97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
98 * First, if it's the last reference, the vnode/znode
99 * can be freed, so the zp may point to freed memory. Second, the last
100 * reference will call zfs_zinactive(), which may induce a lot of work --
101 * pushing cached pages (which acquires range locks) and syncing out
102 * cached atime changes. Third, zfs_zinactive() may require a new tx,
103 * which could deadlock the system if you were already holding one.
105 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
106 * as they can span dmu_tx_assign() calls.
108 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
109 * This is critical because we don't want to block while holding locks.
110 * Note, in particular, that if a lock is sometimes acquired before
111 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
112 * use a non-blocking assign can deadlock the system. The scenario:
114 * Thread A has grabbed a lock before calling dmu_tx_assign().
115 * Thread B is in an already-assigned tx, and blocks for this lock.
116 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
117 * forever, because the previous txg can't quiesce until B's tx commits.
119 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
120 * then drop all locks, call dmu_tx_wait(), and try again.
122 * (5) If the operation succeeded, generate the intent log entry for it
123 * before dropping locks. This ensures that the ordering of events
124 * in the intent log matches the order in which they actually occurred.
125 * During ZIL replay the zfs_log_* functions will update the sequence
126 * number to indicate the zil transaction has replayed.
128 * (6) At the end of each vnode op, the DMU tx must always commit,
129 * regardless of whether there were any errors.
131 * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid)
132 * to ensure that synchronous semantics are provided when necessary.
134 * In general, this is how things should be ordered in each vnode op:
136 * ZFS_ENTER(zfsvfs); // exit if unmounted
138 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
139 * rw_enter(...); // grab any other locks you need
140 * tx = dmu_tx_create(...); // get DMU tx
141 * dmu_tx_hold_*(); // hold each object you might modify
142 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
144 * rw_exit(...); // drop locks
145 * zfs_dirent_unlock(dl); // unlock directory entry
146 * VN_RELE(...); // release held vnodes
147 * if (error == ERESTART) {
152 * dmu_tx_abort(tx); // abort DMU tx
153 * ZFS_EXIT(zfsvfs); // finished in zfs
154 * return (error); // really out of space
156 * error = do_real_work(); // do whatever this VOP does
158 * zfs_log_*(...); // on success, make ZIL entry
159 * dmu_tx_commit(tx); // commit DMU tx -- error or not
160 * rw_exit(...); // drop locks
161 * zfs_dirent_unlock(dl); // unlock directory entry
162 * VN_RELE(...); // release held vnodes
163 * zil_commit(zilog, seq, foid); // synchronous when necessary
164 * ZFS_EXIT(zfsvfs); // finished in zfs
165 * return (error); // done, report error
170 zfs_open(vnode_t
**vpp
, int flag
, cred_t
*cr
, caller_context_t
*ct
)
172 znode_t
*zp
= VTOZ(*vpp
);
173 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
178 if ((flag
& FWRITE
) && (zp
->z_phys
->zp_flags
& ZFS_APPENDONLY
) &&
179 ((flag
& FAPPEND
) == 0)) {
184 if (!zfs_has_ctldir(zp
) && zp
->z_zfsvfs
->z_vscan
&&
185 ZTOV(zp
)->v_type
== VREG
&&
186 !(zp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
) &&
187 zp
->z_phys
->zp_size
> 0) {
188 if (fs_vscan(*vpp
, cr
, 0) != 0) {
194 /* Keep a count of the synchronous opens in the znode */
195 if (flag
& (FSYNC
| FDSYNC
))
196 atomic_inc_32(&zp
->z_sync_cnt
);
204 zfs_close(vnode_t
*vp
, int flag
, int count
, offset_t offset
, cred_t
*cr
,
205 caller_context_t
*ct
)
207 znode_t
*zp
= VTOZ(vp
);
208 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
213 /* Decrement the synchronous opens in the znode */
214 if ((flag
& (FSYNC
| FDSYNC
)) && (count
== 1))
215 atomic_dec_32(&zp
->z_sync_cnt
);
218 * Clean up any locks held by this process on the vp.
220 cleanlocks(vp
, ddi_get_pid(), 0);
221 cleanshares(vp
, ddi_get_pid());
223 if (!zfs_has_ctldir(zp
) && zp
->z_zfsvfs
->z_vscan
&&
224 ZTOV(zp
)->v_type
== VREG
&&
225 !(zp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
) &&
226 zp
->z_phys
->zp_size
> 0)
227 VERIFY(fs_vscan(vp
, cr
, 1) == 0);
234 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
235 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
238 zfs_holey(vnode_t
*vp
, int cmd
, offset_t
*off
)
240 znode_t
*zp
= VTOZ(vp
);
241 uint64_t noff
= (uint64_t)*off
; /* new offset */
246 file_sz
= zp
->z_phys
->zp_size
;
247 if (noff
>= file_sz
) {
251 if (cmd
== _FIO_SEEK_HOLE
)
256 error
= dmu_offset_next(zp
->z_zfsvfs
->z_os
, zp
->z_id
, hole
, &noff
);
259 if ((error
== ESRCH
) || (noff
> file_sz
)) {
261 * Handle the virtual hole at the end of file.
278 zfs_ioctl(vnode_t
*vp
, int com
, intptr_t data
, int flag
, cred_t
*cred
,
279 int *rvalp
, caller_context_t
*ct
)
288 return (zfs_sync(vp
->v_vfsp
, 0, cred
));
291 * The following two ioctls are used by bfu. Faking out,
292 * necessary to avoid bfu errors.
300 if (ddi_copyin((void *)data
, &off
, sizeof (off
), flag
))
304 zfsvfs
= zp
->z_zfsvfs
;
308 /* offset parameter is in/out */
309 error
= zfs_holey(vp
, com
, &off
);
313 if (ddi_copyout(&off
, (void *)data
, sizeof (off
), flag
))
321 * Utility functions to map and unmap a single physical page. These
322 * are used to manage the mappable copies of ZFS file data, and therefore
323 * do not update ref/mod bits.
326 zfs_map_page(page_t
*pp
, enum seg_rw rw
)
329 return (hat_kpm_mapin(pp
, 0));
330 ASSERT(rw
== S_READ
|| rw
== S_WRITE
);
331 return (ppmapin(pp
, PROT_READ
| ((rw
== S_WRITE
) ? PROT_WRITE
: 0),
336 zfs_unmap_page(page_t
*pp
, caddr_t addr
)
339 hat_kpm_mapout(pp
, 0, addr
);
346 * When a file is memory mapped, we must keep the IO data synchronized
347 * between the DMU cache and the memory mapped pages. What this means:
349 * On Write: If we find a memory mapped page, we write to *both*
350 * the page and the dmu buffer.
352 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
353 * the file is memory mapped.
356 mappedwrite(vnode_t
*vp
, int nbytes
, uio_t
*uio
, dmu_tx_t
*tx
)
358 znode_t
*zp
= VTOZ(vp
);
359 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
364 start
= uio
->uio_loffset
;
365 off
= start
& PAGEOFFSET
;
366 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
368 uint64_t bytes
= MIN(PAGESIZE
- off
, len
);
369 uint64_t woff
= uio
->uio_loffset
;
372 * We don't want a new page to "appear" in the middle of
373 * the file update (because it may not get the write
374 * update data), so we grab a lock to block
377 rw_enter(&zp
->z_map_lock
, RW_WRITER
);
378 if (pp
= page_lookup(vp
, start
, SE_SHARED
)) {
381 rw_exit(&zp
->z_map_lock
);
382 va
= zfs_map_page(pp
, S_WRITE
);
383 error
= uiomove(va
+off
, bytes
, UIO_WRITE
, uio
);
385 dmu_write(zfsvfs
->z_os
, zp
->z_id
,
386 woff
, bytes
, va
+off
, tx
);
388 zfs_unmap_page(pp
, va
);
391 error
= dmu_write_uio(zfsvfs
->z_os
, zp
->z_id
,
393 rw_exit(&zp
->z_map_lock
);
404 * When a file is memory mapped, we must keep the IO data synchronized
405 * between the DMU cache and the memory mapped pages. What this means:
407 * On Read: We "read" preferentially from memory mapped pages,
408 * else we default from the dmu buffer.
410 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
411 * the file is memory mapped.
414 mappedread(vnode_t
*vp
, int nbytes
, uio_t
*uio
)
416 znode_t
*zp
= VTOZ(vp
);
417 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
422 start
= uio
->uio_loffset
;
423 off
= start
& PAGEOFFSET
;
424 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
426 uint64_t bytes
= MIN(PAGESIZE
- off
, len
);
428 if (pp
= page_lookup(vp
, start
, SE_SHARED
)) {
431 va
= zfs_map_page(pp
, S_READ
);
432 error
= uiomove(va
+ off
, bytes
, UIO_READ
, uio
);
433 zfs_unmap_page(pp
, va
);
436 error
= dmu_read_uio(os
, zp
->z_id
, uio
, bytes
);
446 offset_t zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
449 * Read bytes from specified file into supplied buffer.
451 * IN: vp - vnode of file to be read from.
452 * uio - structure supplying read location, range info,
454 * ioflag - SYNC flags; used to provide FRSYNC semantics.
455 * cr - credentials of caller.
456 * ct - caller context
458 * OUT: uio - updated offset and range, buffer filled.
460 * RETURN: 0 if success
461 * error code if failure
464 * vp - atime updated if byte count > 0
468 zfs_read(vnode_t
*vp
, uio_t
*uio
, int ioflag
, cred_t
*cr
, caller_context_t
*ct
)
470 znode_t
*zp
= VTOZ(vp
);
471 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
481 if (zp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
) {
487 * Validate file offset
489 if (uio
->uio_loffset
< (offset_t
)0) {
495 * Fasttrack empty reads
497 if (uio
->uio_resid
== 0) {
503 * Check for mandatory locks
505 if (MANDMODE((mode_t
)zp
->z_phys
->zp_mode
)) {
506 if (error
= chklock(vp
, FREAD
,
507 uio
->uio_loffset
, uio
->uio_resid
, uio
->uio_fmode
, ct
)) {
514 * If we're in FRSYNC mode, sync out this znode before reading it.
517 zil_commit(zfsvfs
->z_log
, zp
->z_last_itx
, zp
->z_id
);
520 * Lock the range against changes.
522 rl
= zfs_range_lock(zp
, uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
525 * If we are reading past end-of-file we can skip
526 * to the end; but we might still need to set atime.
528 if (uio
->uio_loffset
>= zp
->z_phys
->zp_size
) {
533 ASSERT(uio
->uio_loffset
< zp
->z_phys
->zp_size
);
534 n
= MIN(uio
->uio_resid
, zp
->z_phys
->zp_size
- uio
->uio_loffset
);
537 nbytes
= MIN(n
, zfs_read_chunk_size
-
538 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
540 if (vn_has_cached_data(vp
))
541 error
= mappedread(vp
, nbytes
, uio
);
543 error
= dmu_read_uio(os
, zp
->z_id
, uio
, nbytes
);
545 /* convert checksum errors into IO errors */
555 zfs_range_unlock(rl
);
557 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
563 * Write the bytes to a file.
565 * IN: vp - vnode of file to be written to.
566 * uio - structure supplying write location, range info,
568 * ioflag - FAPPEND flag set if in append mode.
569 * cr - credentials of caller.
570 * ct - caller context (NFS/CIFS fem monitor only)
572 * OUT: uio - updated offset and range.
574 * RETURN: 0 if success
575 * error code if failure
578 * vp - ctime|mtime updated if byte count > 0
582 zfs_write(vnode_t
*vp
, uio_t
*uio
, int ioflag
, cred_t
*cr
, caller_context_t
*ct
)
584 znode_t
*zp
= VTOZ(vp
);
585 rlim64_t limit
= uio
->uio_llimit
;
586 ssize_t start_resid
= uio
->uio_resid
;
590 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
595 int max_blksz
= zfsvfs
->z_max_blksz
;
600 * Fasttrack empty write
606 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
613 * If immutable or not appending then return EPERM
615 pflags
= zp
->z_phys
->zp_flags
;
616 if ((pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
617 ((pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
618 (uio
->uio_loffset
< zp
->z_phys
->zp_size
))) {
623 zilog
= zfsvfs
->z_log
;
626 * Pre-fault the pages to ensure slow (eg NFS) pages
629 uio_prefaultpages(n
, uio
);
632 * If in append mode, set the io offset pointer to eof.
634 if (ioflag
& FAPPEND
) {
636 * Range lock for a file append:
637 * The value for the start of range will be determined by
638 * zfs_range_lock() (to guarantee append semantics).
639 * If this write will cause the block size to increase,
640 * zfs_range_lock() will lock the entire file, so we must
641 * later reduce the range after we grow the block size.
643 rl
= zfs_range_lock(zp
, 0, n
, RL_APPEND
);
644 if (rl
->r_len
== UINT64_MAX
) {
645 /* overlocked, zp_size can't change */
646 woff
= uio
->uio_loffset
= zp
->z_phys
->zp_size
;
648 woff
= uio
->uio_loffset
= rl
->r_off
;
651 woff
= uio
->uio_loffset
;
653 * Validate file offset
661 * If we need to grow the block size then zfs_range_lock()
662 * will lock a wider range than we request here.
663 * Later after growing the block size we reduce the range.
665 rl
= zfs_range_lock(zp
, woff
, n
, RL_WRITER
);
669 zfs_range_unlock(rl
);
674 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
678 * Check for mandatory locks
680 if (MANDMODE((mode_t
)zp
->z_phys
->zp_mode
) &&
681 (error
= chklock(vp
, FWRITE
, woff
, n
, uio
->uio_fmode
, ct
)) != 0) {
682 zfs_range_unlock(rl
);
686 end_size
= MAX(zp
->z_phys
->zp_size
, woff
+ n
);
689 * Write the file in reasonable size chunks. Each chunk is written
690 * in a separate transaction; this keeps the intent log records small
691 * and allows us to do more fine-grained space accounting.
695 * Start a transaction.
697 woff
= uio
->uio_loffset
;
698 tx
= dmu_tx_create(zfsvfs
->z_os
);
699 dmu_tx_hold_bonus(tx
, zp
->z_id
);
700 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
701 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
703 if (error
== ERESTART
) {
713 * If zfs_range_lock() over-locked we grow the blocksize
714 * and then reduce the lock range. This will only happen
715 * on the first iteration since zfs_range_reduce() will
716 * shrink down r_len to the appropriate size.
718 if (rl
->r_len
== UINT64_MAX
) {
721 if (zp
->z_blksz
> max_blksz
) {
722 ASSERT(!ISP2(zp
->z_blksz
));
723 new_blksz
= MIN(end_size
, SPA_MAXBLOCKSIZE
);
725 new_blksz
= MIN(end_size
, max_blksz
);
727 zfs_grow_blocksize(zp
, new_blksz
, tx
);
728 zfs_range_reduce(rl
, woff
, n
);
732 * XXX - should we really limit each write to z_max_blksz?
733 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
735 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
736 rw_enter(&zp
->z_map_lock
, RW_READER
);
738 tx_bytes
= uio
->uio_resid
;
739 if (vn_has_cached_data(vp
)) {
740 rw_exit(&zp
->z_map_lock
);
741 error
= mappedwrite(vp
, nbytes
, uio
, tx
);
743 error
= dmu_write_uio(zfsvfs
->z_os
, zp
->z_id
,
745 rw_exit(&zp
->z_map_lock
);
747 tx_bytes
-= uio
->uio_resid
;
750 * If we made no progress, we're done. If we made even
751 * partial progress, update the znode and ZIL accordingly.
760 * Clear Set-UID/Set-GID bits on successful write if not
761 * privileged and at least one of the excute bits is set.
763 * It would be nice to to this after all writes have
764 * been done, but that would still expose the ISUID/ISGID
765 * to another app after the partial write is committed.
767 * Note: we don't call zfs_fuid_map_id() here because
768 * user 0 is not an ephemeral uid.
770 mutex_enter(&zp
->z_acl_lock
);
771 if ((zp
->z_phys
->zp_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
772 (S_IXUSR
>> 6))) != 0 &&
773 (zp
->z_phys
->zp_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
774 secpolicy_vnode_setid_retain(cr
,
775 (zp
->z_phys
->zp_mode
& S_ISUID
) != 0 &&
776 zp
->z_phys
->zp_uid
== 0) != 0) {
777 zp
->z_phys
->zp_mode
&= ~(S_ISUID
| S_ISGID
);
779 mutex_exit(&zp
->z_acl_lock
);
782 * Update time stamp. NOTE: This marks the bonus buffer as
783 * dirty, so we don't have to do it again for zp_size.
785 zfs_time_stamper(zp
, CONTENT_MODIFIED
, tx
);
788 * Update the file size (zp_size) if it has changed;
789 * account for possible concurrent updates.
791 while ((end_size
= zp
->z_phys
->zp_size
) < uio
->uio_loffset
)
792 (void) atomic_cas_64(&zp
->z_phys
->zp_size
, end_size
,
794 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
);
799 ASSERT(tx_bytes
== nbytes
);
803 zfs_range_unlock(rl
);
806 * If we're in replay mode, or we made no progress, return error.
807 * Otherwise, it's at least a partial write, so it's successful.
809 if (zfsvfs
->z_replay
|| uio
->uio_resid
== start_resid
) {
814 if (ioflag
& (FSYNC
| FDSYNC
))
815 zil_commit(zilog
, zp
->z_last_itx
, zp
->z_id
);
822 zfs_get_done(dmu_buf_t
*db
, void *vzgd
)
824 zgd_t
*zgd
= (zgd_t
*)vzgd
;
825 rl_t
*rl
= zgd
->zgd_rl
;
826 vnode_t
*vp
= ZTOV(rl
->r_zp
);
828 dmu_buf_rele(db
, vzgd
);
829 zfs_range_unlock(rl
);
831 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
832 kmem_free(zgd
, sizeof (zgd_t
));
836 * Get data to generate a TX_WRITE intent log record.
839 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
841 zfsvfs_t
*zfsvfs
= arg
;
842 objset_t
*os
= zfsvfs
->z_os
;
844 uint64_t off
= lr
->lr_offset
;
848 int dlen
= lr
->lr_length
; /* length of user data */
855 * Nothing to do if the file has been removed
857 if (zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
) != 0)
859 if (zp
->z_unlinked
) {
865 * Write records come in two flavors: immediate and indirect.
866 * For small writes it's cheaper to store the data with the
867 * log record (immediate); for large writes it's cheaper to
868 * sync the data and get a pointer to it (indirect) so that
869 * we don't have to write the data twice.
871 if (buf
!= NULL
) { /* immediate write */
872 rl
= zfs_range_lock(zp
, off
, dlen
, RL_READER
);
873 /* test for truncation needs to be done while range locked */
874 if (off
>= zp
->z_phys
->zp_size
) {
878 VERIFY(0 == dmu_read(os
, lr
->lr_foid
, off
, dlen
, buf
));
879 } else { /* indirect write */
880 uint64_t boff
; /* block starting offset */
883 * Have to lock the whole block to ensure when it's
884 * written out and it's checksum is being calculated
885 * that no one can change the data. We need to re-check
886 * blocksize after we get the lock in case it's changed!
889 if (ISP2(zp
->z_blksz
)) {
890 boff
= P2ALIGN_TYPED(off
, zp
->z_blksz
,
896 rl
= zfs_range_lock(zp
, boff
, dlen
, RL_READER
);
897 if (zp
->z_blksz
== dlen
)
899 zfs_range_unlock(rl
);
901 /* test for truncation needs to be done while range locked */
902 if (off
>= zp
->z_phys
->zp_size
) {
906 zgd
= (zgd_t
*)kmem_alloc(sizeof (zgd_t
), KM_SLEEP
);
908 zgd
->zgd_zilog
= zfsvfs
->z_log
;
909 zgd
->zgd_bp
= &lr
->lr_blkptr
;
910 VERIFY(0 == dmu_buf_hold(os
, lr
->lr_foid
, boff
, zgd
, &db
));
911 ASSERT(boff
== db
->db_offset
);
912 lr
->lr_blkoff
= off
- boff
;
913 error
= dmu_sync(zio
, db
, &lr
->lr_blkptr
,
914 lr
->lr_common
.lrc_txg
, zfs_get_done
, zgd
);
915 ASSERT((error
&& error
!= EINPROGRESS
) ||
916 lr
->lr_length
<= zp
->z_blksz
);
918 zil_add_block(zfsvfs
->z_log
, &lr
->lr_blkptr
);
920 * If we get EINPROGRESS, then we need to wait for a
921 * write IO initiated by dmu_sync() to complete before
922 * we can release this dbuf. We will finish everything
923 * up in the zfs_get_done() callback.
925 if (error
== EINPROGRESS
)
927 dmu_buf_rele(db
, zgd
);
928 kmem_free(zgd
, sizeof (zgd_t
));
931 zfs_range_unlock(rl
);
938 zfs_access(vnode_t
*vp
, int mode
, int flag
, cred_t
*cr
,
939 caller_context_t
*ct
)
941 znode_t
*zp
= VTOZ(vp
);
942 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
948 if (flag
& V_ACE_MASK
)
949 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
951 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
958 * Lookup an entry in a directory, or an extended attribute directory.
959 * If it exists, return a held vnode reference for it.
961 * IN: dvp - vnode of directory to search.
962 * nm - name of entry to lookup.
963 * pnp - full pathname to lookup [UNUSED].
964 * flags - LOOKUP_XATTR set if looking for an attribute.
965 * rdir - root directory vnode [UNUSED].
966 * cr - credentials of caller.
967 * ct - caller context
968 * direntflags - directory lookup flags
969 * realpnp - returned pathname.
971 * OUT: vpp - vnode of located entry, NULL if not found.
973 * RETURN: 0 if success
974 * error code if failure
981 zfs_lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, struct pathname
*pnp
,
982 int flags
, vnode_t
*rdir
, cred_t
*cr
, caller_context_t
*ct
,
983 int *direntflags
, pathname_t
*realpnp
)
985 znode_t
*zdp
= VTOZ(dvp
);
986 zfsvfs_t
*zfsvfs
= zdp
->z_zfsvfs
;
994 if (flags
& LOOKUP_XATTR
) {
996 * If the xattr property is off, refuse the lookup request.
998 if (!(zfsvfs
->z_vfs
->vfs_flag
& VFS_XATTR
)) {
1004 * We don't allow recursive attributes..
1005 * Maybe someday we will.
1007 if (zdp
->z_phys
->zp_flags
& ZFS_XATTR
) {
1012 if (error
= zfs_get_xattrdir(VTOZ(dvp
), vpp
, cr
, flags
)) {
1018 * Do we have permission to get into attribute directory?
1021 if (error
= zfs_zaccess(VTOZ(*vpp
), ACE_EXECUTE
, 0,
1031 if (dvp
->v_type
!= VDIR
) {
1037 * Check accessibility of directory.
1040 if (error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
)) {
1045 if (zfsvfs
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1046 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1051 error
= zfs_dirlook(zdp
, nm
, vpp
, flags
, direntflags
, realpnp
);
1054 * Convert device special files
1056 if (IS_DEVVP(*vpp
)) {
1059 svp
= specvp(*vpp
, (*vpp
)->v_rdev
, (*vpp
)->v_type
, cr
);
1073 * Attempt to create a new entry in a directory. If the entry
1074 * already exists, truncate the file if permissible, else return
1075 * an error. Return the vp of the created or trunc'd file.
1077 * IN: dvp - vnode of directory to put new file entry in.
1078 * name - name of new file entry.
1079 * vap - attributes of new file.
1080 * excl - flag indicating exclusive or non-exclusive mode.
1081 * mode - mode to open file with.
1082 * cr - credentials of caller.
1083 * flag - large file flag [UNUSED].
1084 * ct - caller context
1085 * vsecp - ACL to be set
1087 * OUT: vpp - vnode of created or trunc'd entry.
1089 * RETURN: 0 if success
1090 * error code if failure
1093 * dvp - ctime|mtime updated if new entry created
1094 * vp - ctime|mtime always, atime if new
1099 zfs_create(vnode_t
*dvp
, char *name
, vattr_t
*vap
, vcexcl_t excl
,
1100 int mode
, vnode_t
**vpp
, cred_t
*cr
, int flag
, caller_context_t
*ct
,
1103 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1104 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1110 zfs_acl_t
*aclp
= NULL
;
1111 zfs_fuid_info_t
*fuidp
= NULL
;
1114 gid_t gid
= crgetgid(cr
);
1117 * If we have an ephemeral id, ACL, or XVATTR then
1118 * make sure file system is at proper version
1121 ksid
= crgetsid(cr
, KSID_OWNER
);
1123 uid
= ksid_getid(ksid
);
1127 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1128 (vsecp
|| (vap
->va_mask
& AT_XVATTR
) ||
1129 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1135 zilog
= zfsvfs
->z_log
;
1137 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
1138 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1143 if (vap
->va_mask
& AT_XVATTR
) {
1144 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1145 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
1153 if ((vap
->va_mode
& VSVTX
) && secpolicy_vnode_stky_modify(cr
))
1154 vap
->va_mode
&= ~VSVTX
;
1156 if (*name
== '\0') {
1158 * Null component name refers to the directory itself.
1165 /* possible VN_HOLD(zp) */
1168 if (flag
& FIGNORECASE
)
1171 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1174 if (strcmp(name
, "..") == 0)
1182 if (vsecp
&& aclp
== NULL
) {
1183 error
= zfs_vsec_2_aclp(zfsvfs
, vap
->va_type
, vsecp
, &aclp
);
1187 zfs_dirent_unlock(dl
);
1196 * Create a new file object and update the directory
1199 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
1204 * We only support the creation of regular files in
1205 * extended attribute directories.
1207 if ((dzp
->z_phys
->zp_flags
& ZFS_XATTR
) &&
1208 (vap
->va_type
!= VREG
)) {
1213 tx
= dmu_tx_create(os
);
1214 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1215 if ((aclp
&& aclp
->z_has_fuids
) || IS_EPHEMERAL(uid
) ||
1216 IS_EPHEMERAL(gid
)) {
1217 if (zfsvfs
->z_fuid_obj
== 0) {
1218 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1219 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1220 FUID_SIZE_ESTIMATE(zfsvfs
));
1221 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
,
1224 dmu_tx_hold_bonus(tx
, zfsvfs
->z_fuid_obj
);
1225 dmu_tx_hold_write(tx
, zfsvfs
->z_fuid_obj
, 0,
1226 FUID_SIZE_ESTIMATE(zfsvfs
));
1229 dmu_tx_hold_bonus(tx
, dzp
->z_id
);
1230 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1231 if ((dzp
->z_phys
->zp_flags
& ZFS_INHERIT_ACE
) || aclp
) {
1232 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1233 0, SPA_MAXBLOCKSIZE
);
1235 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1237 zfs_dirent_unlock(dl
);
1238 if (error
== ERESTART
) {
1249 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, 0, aclp
, &fuidp
);
1250 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1251 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1252 if (flag
& FIGNORECASE
)
1254 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1257 zfs_fuid_info_free(fuidp
);
1260 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1263 * A directory entry already exists for this name.
1266 * Can't truncate an existing file if in exclusive mode.
1273 * Can't open a directory for writing.
1275 if ((ZTOV(zp
)->v_type
== VDIR
) && (mode
& S_IWRITE
)) {
1280 * Verify requested access to file.
1282 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1286 mutex_enter(&dzp
->z_lock
);
1288 mutex_exit(&dzp
->z_lock
);
1291 * Truncate regular files if requested.
1293 if ((ZTOV(zp
)->v_type
== VREG
) &&
1294 (vap
->va_mask
& AT_SIZE
) && (vap
->va_size
== 0)) {
1295 /* we can't hold any locks when calling zfs_freesp() */
1296 zfs_dirent_unlock(dl
);
1298 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1300 vnevent_create(ZTOV(zp
), ct
);
1307 zfs_dirent_unlock(dl
);
1315 * If vnode is for a device return a specfs vnode instead.
1317 if (IS_DEVVP(*vpp
)) {
1320 svp
= specvp(*vpp
, (*vpp
)->v_rdev
, (*vpp
)->v_type
, cr
);
1336 * Remove an entry from a directory.
1338 * IN: dvp - vnode of directory to remove entry from.
1339 * name - name of entry to remove.
1340 * cr - credentials of caller.
1341 * ct - caller context
1342 * flags - case flags
1344 * RETURN: 0 if success
1345 * error code if failure
1349 * vp - ctime (if nlink > 0)
1353 zfs_remove(vnode_t
*dvp
, char *name
, cred_t
*cr
, caller_context_t
*ct
,
1356 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1357 znode_t
*xzp
= NULL
;
1359 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1361 uint64_t acl_obj
, xattr_obj
;
1364 boolean_t may_delete_now
, delete_now
= FALSE
;
1365 boolean_t unlinked
, toobig
= FALSE
;
1367 pathname_t
*realnmp
= NULL
;
1374 zilog
= zfsvfs
->z_log
;
1376 if (flags
& FIGNORECASE
) {
1384 * Attempt to lock directory; fail if entry doesn't exist.
1386 if (error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1396 if (error
= zfs_zaccess_delete(dzp
, zp
, cr
)) {
1401 * Need to use rmdir for removing directories.
1403 if (vp
->v_type
== VDIR
) {
1408 vnevent_remove(vp
, dvp
, name
, ct
);
1411 dnlc_remove(dvp
, realnmp
->pn_buf
);
1413 dnlc_remove(dvp
, name
);
1415 mutex_enter(&vp
->v_lock
);
1416 may_delete_now
= vp
->v_count
== 1 && !vn_has_cached_data(vp
);
1417 mutex_exit(&vp
->v_lock
);
1420 * We may delete the znode now, or we may put it in the unlinked set;
1421 * it depends on whether we're the last link, and on whether there are
1422 * other holds on the vnode. So we dmu_tx_hold() the right things to
1423 * allow for either case.
1425 tx
= dmu_tx_create(zfsvfs
->z_os
);
1426 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1427 dmu_tx_hold_bonus(tx
, zp
->z_id
);
1428 if (may_delete_now
) {
1430 zp
->z_phys
->zp_size
> zp
->z_blksz
* DMU_MAX_DELETEBLKCNT
;
1431 /* if the file is too big, only hold_free a token amount */
1432 dmu_tx_hold_free(tx
, zp
->z_id
, 0,
1433 (toobig
? DMU_MAX_ACCESS
: DMU_OBJECT_END
));
1436 /* are there any extended attributes? */
1437 if ((xattr_obj
= zp
->z_phys
->zp_xattr
) != 0) {
1438 /* XXX - do we need this if we are deleting? */
1439 dmu_tx_hold_bonus(tx
, xattr_obj
);
1442 /* are there any additional acls */
1443 if ((acl_obj
= zp
->z_phys
->zp_acl
.z_acl_extern_obj
) != 0 &&
1445 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
1447 /* charge as an update -- would be nice not to charge at all */
1448 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1450 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1452 zfs_dirent_unlock(dl
);
1454 if (error
== ERESTART
) {
1467 * Remove the directory entry.
1469 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1477 mutex_enter(&vp
->v_lock
);
1478 delete_now
= may_delete_now
&& !toobig
&&
1479 vp
->v_count
== 1 && !vn_has_cached_data(vp
) &&
1480 zp
->z_phys
->zp_xattr
== xattr_obj
&&
1481 zp
->z_phys
->zp_acl
.z_acl_extern_obj
== acl_obj
;
1482 mutex_exit(&vp
->v_lock
);
1486 if (zp
->z_phys
->zp_xattr
) {
1487 error
= zfs_zget(zfsvfs
, zp
->z_phys
->zp_xattr
, &xzp
);
1488 ASSERT3U(error
, ==, 0);
1489 ASSERT3U(xzp
->z_phys
->zp_links
, ==, 2);
1490 dmu_buf_will_dirty(xzp
->z_dbuf
, tx
);
1491 mutex_enter(&xzp
->z_lock
);
1492 xzp
->z_unlinked
= 1;
1493 xzp
->z_phys
->zp_links
= 0;
1494 mutex_exit(&xzp
->z_lock
);
1495 zfs_unlinked_add(xzp
, tx
);
1496 zp
->z_phys
->zp_xattr
= 0; /* probably unnecessary */
1498 mutex_enter(&zp
->z_lock
);
1499 mutex_enter(&vp
->v_lock
);
1501 ASSERT3U(vp
->v_count
, ==, 0);
1502 mutex_exit(&vp
->v_lock
);
1503 mutex_exit(&zp
->z_lock
);
1504 zfs_znode_delete(zp
, tx
);
1505 } else if (unlinked
) {
1506 zfs_unlinked_add(zp
, tx
);
1510 if (flags
& FIGNORECASE
)
1512 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
);
1519 zfs_dirent_unlock(dl
);
1524 /* this rele is delayed to prevent nesting transactions */
1533 * Create a new directory and insert it into dvp using the name
1534 * provided. Return a pointer to the inserted directory.
1536 * IN: dvp - vnode of directory to add subdir to.
1537 * dirname - name of new directory.
1538 * vap - attributes of new directory.
1539 * cr - credentials of caller.
1540 * ct - caller context
1541 * vsecp - ACL to be set
1543 * OUT: vpp - vnode of created directory.
1545 * RETURN: 0 if success
1546 * error code if failure
1549 * dvp - ctime|mtime updated
1550 * vp - ctime|mtime|atime updated
1554 zfs_mkdir(vnode_t
*dvp
, char *dirname
, vattr_t
*vap
, vnode_t
**vpp
, cred_t
*cr
,
1555 caller_context_t
*ct
, int flags
, vsecattr_t
*vsecp
)
1557 znode_t
*zp
, *dzp
= VTOZ(dvp
);
1558 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1564 zfs_acl_t
*aclp
= NULL
;
1565 zfs_fuid_info_t
*fuidp
= NULL
;
1569 gid_t gid
= crgetgid(cr
);
1571 ASSERT(vap
->va_type
== VDIR
);
1574 * If we have an ephemeral id, ACL, or XVATTR then
1575 * make sure file system is at proper version
1578 ksid
= crgetsid(cr
, KSID_OWNER
);
1580 uid
= ksid_getid(ksid
);
1583 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1584 (vsecp
|| (vap
->va_mask
& AT_XVATTR
) ||
1585 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1590 zilog
= zfsvfs
->z_log
;
1592 if (dzp
->z_phys
->zp_flags
& ZFS_XATTR
) {
1597 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1598 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1602 if (flags
& FIGNORECASE
)
1605 if (vap
->va_mask
& AT_XVATTR
)
1606 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1607 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
1613 * First make sure the new directory doesn't exist.
1618 if (error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1624 if (error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
)) {
1625 zfs_dirent_unlock(dl
);
1630 if (vsecp
&& aclp
== NULL
) {
1631 error
= zfs_vsec_2_aclp(zfsvfs
, vap
->va_type
, vsecp
, &aclp
);
1633 zfs_dirent_unlock(dl
);
1639 * Add a new entry to the directory.
1641 tx
= dmu_tx_create(zfsvfs
->z_os
);
1642 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1643 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1644 if ((aclp
&& aclp
->z_has_fuids
) || IS_EPHEMERAL(uid
) ||
1645 IS_EPHEMERAL(gid
)) {
1646 if (zfsvfs
->z_fuid_obj
== 0) {
1647 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1648 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1649 FUID_SIZE_ESTIMATE(zfsvfs
));
1650 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, FALSE
, NULL
);
1652 dmu_tx_hold_bonus(tx
, zfsvfs
->z_fuid_obj
);
1653 dmu_tx_hold_write(tx
, zfsvfs
->z_fuid_obj
, 0,
1654 FUID_SIZE_ESTIMATE(zfsvfs
));
1657 if ((dzp
->z_phys
->zp_flags
& ZFS_INHERIT_ACE
) || aclp
)
1658 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1659 0, SPA_MAXBLOCKSIZE
);
1660 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1662 zfs_dirent_unlock(dl
);
1663 if (error
== ERESTART
) {
1678 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, 0, aclp
, &fuidp
);
1684 * Now put new name in parent dir.
1686 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1690 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1691 if (flags
& FIGNORECASE
)
1693 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
, fuidp
, vap
);
1696 zfs_fuid_info_free(fuidp
);
1699 zfs_dirent_unlock(dl
);
1706 * Remove a directory subdir entry. If the current working
1707 * directory is the same as the subdir to be removed, the
1710 * IN: dvp - vnode of directory to remove from.
1711 * name - name of directory to be removed.
1712 * cwd - vnode of current working directory.
1713 * cr - credentials of caller.
1714 * ct - caller context
1715 * flags - case flags
1717 * RETURN: 0 if success
1718 * error code if failure
1721 * dvp - ctime|mtime updated
1725 zfs_rmdir(vnode_t
*dvp
, char *name
, vnode_t
*cwd
, cred_t
*cr
,
1726 caller_context_t
*ct
, int flags
)
1728 znode_t
*dzp
= VTOZ(dvp
);
1731 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1740 zilog
= zfsvfs
->z_log
;
1742 if (flags
& FIGNORECASE
)
1748 * Attempt to lock directory; fail if entry doesn't exist.
1750 if (error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1758 if (error
= zfs_zaccess_delete(dzp
, zp
, cr
)) {
1762 if (vp
->v_type
!= VDIR
) {
1772 vnevent_rmdir(vp
, dvp
, name
, ct
);
1775 * Grab a lock on the directory to make sure that noone is
1776 * trying to add (or lookup) entries while we are removing it.
1778 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1781 * Grab a lock on the parent pointer to make sure we play well
1782 * with the treewalk and directory rename code.
1784 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1786 tx
= dmu_tx_create(zfsvfs
->z_os
);
1787 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1788 dmu_tx_hold_bonus(tx
, zp
->z_id
);
1789 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1790 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1792 rw_exit(&zp
->z_parent_lock
);
1793 rw_exit(&zp
->z_name_lock
);
1794 zfs_dirent_unlock(dl
);
1796 if (error
== ERESTART
) {
1806 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
1809 uint64_t txtype
= TX_RMDIR
;
1810 if (flags
& FIGNORECASE
)
1812 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
);
1817 rw_exit(&zp
->z_parent_lock
);
1818 rw_exit(&zp
->z_name_lock
);
1820 zfs_dirent_unlock(dl
);
1829 * Read as many directory entries as will fit into the provided
1830 * buffer from the given directory cursor position (specified in
1831 * the uio structure.
1833 * IN: vp - vnode of directory to read.
1834 * uio - structure supplying read location, range info,
1835 * and return buffer.
1836 * cr - credentials of caller.
1837 * ct - caller context
1838 * flags - case flags
1840 * OUT: uio - updated offset and range, buffer filled.
1841 * eofp - set to true if end-of-file detected.
1843 * RETURN: 0 if success
1844 * error code if failure
1847 * vp - atime updated
1849 * Note that the low 4 bits of the cookie returned by zap is always zero.
1850 * This allows us to use the low range for "special" directory entries:
1851 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1852 * we use the offset 2 for the '.zfs' directory.
1856 zfs_readdir(vnode_t
*vp
, uio_t
*uio
, cred_t
*cr
, int *eofp
,
1857 caller_context_t
*ct
, int flags
)
1859 znode_t
*zp
= VTOZ(vp
);
1863 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1868 zap_attribute_t zap
;
1869 uint_t bytes_wanted
;
1870 uint64_t offset
; /* must be unsigned; checks for < 1 */
1875 boolean_t check_sysattrs
;
1881 * If we are not given an eof variable,
1888 * Check for valid iov_len.
1890 if (uio
->uio_iov
->iov_len
<= 0) {
1896 * Quit if directory has been removed (posix)
1898 if ((*eofp
= zp
->z_unlinked
) != 0) {
1905 offset
= uio
->uio_loffset
;
1906 prefetch
= zp
->z_zn_prefetch
;
1909 * Initialize the iterator cursor.
1913 * Start iteration from the beginning of the directory.
1915 zap_cursor_init(&zc
, os
, zp
->z_id
);
1918 * The offset is a serialized cursor.
1920 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
1924 * Get space to change directory entries into fs independent format.
1926 iovp
= uio
->uio_iov
;
1927 bytes_wanted
= iovp
->iov_len
;
1928 if (uio
->uio_segflg
!= UIO_SYSSPACE
|| uio
->uio_iovcnt
!= 1) {
1929 bufsize
= bytes_wanted
;
1930 outbuf
= kmem_alloc(bufsize
, KM_SLEEP
);
1931 odp
= (struct dirent64
*)outbuf
;
1933 bufsize
= bytes_wanted
;
1934 odp
= (struct dirent64
*)iovp
->iov_base
;
1936 eodp
= (struct edirent
*)odp
;
1939 * If this VFS supports the system attribute view interface; and
1940 * we're looking at an extended attribute directory; and we care
1941 * about normalization conflicts on this vfs; then we must check
1942 * for normalization conflicts with the sysattr name space.
1944 check_sysattrs
= vfs_has_feature(vp
->v_vfsp
, VFSFT_SYSATTR_VIEWS
) &&
1945 (vp
->v_flag
& V_XATTRDIR
) && zfsvfs
->z_norm
&&
1946 (flags
& V_RDDIR_ENTFLAGS
);
1949 * Transform to file-system independent format
1952 while (outcount
< bytes_wanted
) {
1958 * Special case `.', `..', and `.zfs'.
1961 (void) strcpy(zap
.za_name
, ".");
1962 zap
.za_normalization_conflict
= 0;
1964 } else if (offset
== 1) {
1965 (void) strcpy(zap
.za_name
, "..");
1966 zap
.za_normalization_conflict
= 0;
1967 objnum
= zp
->z_phys
->zp_parent
;
1968 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
1969 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
1970 zap
.za_normalization_conflict
= 0;
1971 objnum
= ZFSCTL_INO_ROOT
;
1976 if (error
= zap_cursor_retrieve(&zc
, &zap
)) {
1977 if ((*eofp
= (error
== ENOENT
)) != 0)
1983 if (zap
.za_integer_length
!= 8 ||
1984 zap
.za_num_integers
!= 1) {
1985 cmn_err(CE_WARN
, "zap_readdir: bad directory "
1986 "entry, obj = %lld, offset = %lld\n",
1987 (u_longlong_t
)zp
->z_id
,
1988 (u_longlong_t
)offset
);
1993 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
1995 * MacOS X can extract the object type here such as:
1996 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1999 if (check_sysattrs
&& !zap
.za_normalization_conflict
) {
2000 zap
.za_normalization_conflict
=
2001 xattr_sysattr_casechk(zap
.za_name
);
2005 if (flags
& V_RDDIR_ENTFLAGS
)
2006 reclen
= EDIRENT_RECLEN(strlen(zap
.za_name
));
2008 reclen
= DIRENT64_RECLEN(strlen(zap
.za_name
));
2011 * Will this entry fit in the buffer?
2013 if (outcount
+ reclen
> bufsize
) {
2015 * Did we manage to fit anything in the buffer?
2023 if (flags
& V_RDDIR_ENTFLAGS
) {
2025 * Add extended flag entry:
2027 eodp
->ed_ino
= objnum
;
2028 eodp
->ed_reclen
= reclen
;
2029 /* NOTE: ed_off is the offset for the *next* entry */
2030 next
= &(eodp
->ed_off
);
2031 eodp
->ed_eflags
= zap
.za_normalization_conflict
?
2032 ED_CASE_CONFLICT
: 0;
2033 (void) strncpy(eodp
->ed_name
, zap
.za_name
,
2034 EDIRENT_NAMELEN(reclen
));
2035 eodp
= (edirent_t
*)((intptr_t)eodp
+ reclen
);
2040 odp
->d_ino
= objnum
;
2041 odp
->d_reclen
= reclen
;
2042 /* NOTE: d_off is the offset for the *next* entry */
2043 next
= &(odp
->d_off
);
2044 (void) strncpy(odp
->d_name
, zap
.za_name
,
2045 DIRENT64_NAMELEN(reclen
));
2046 odp
= (dirent64_t
*)((intptr_t)odp
+ reclen
);
2050 ASSERT(outcount
<= bufsize
);
2052 /* Prefetch znode */
2054 dmu_prefetch(os
, objnum
, 0, 0);
2057 * Move to the next entry, fill in the previous offset.
2059 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2060 zap_cursor_advance(&zc
);
2061 offset
= zap_cursor_serialize(&zc
);
2067 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2069 if (uio
->uio_segflg
== UIO_SYSSPACE
&& uio
->uio_iovcnt
== 1) {
2070 iovp
->iov_base
+= outcount
;
2071 iovp
->iov_len
-= outcount
;
2072 uio
->uio_resid
-= outcount
;
2073 } else if (error
= uiomove(outbuf
, (long)outcount
, UIO_READ
, uio
)) {
2075 * Reset the pointer.
2077 offset
= uio
->uio_loffset
;
2081 zap_cursor_fini(&zc
);
2082 if (uio
->uio_segflg
!= UIO_SYSSPACE
|| uio
->uio_iovcnt
!= 1)
2083 kmem_free(outbuf
, bufsize
);
2085 if (error
== ENOENT
)
2088 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
2090 uio
->uio_loffset
= offset
;
2095 ulong_t zfs_fsync_sync_cnt
= 4;
2098 zfs_fsync(vnode_t
*vp
, int syncflag
, cred_t
*cr
, caller_context_t
*ct
)
2100 znode_t
*zp
= VTOZ(vp
);
2101 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2104 * Regardless of whether this is required for standards conformance,
2105 * this is the logical behavior when fsync() is called on a file with
2106 * dirty pages. We use B_ASYNC since the ZIL transactions are already
2107 * going to be pushed out as part of the zil_commit().
2109 if (vn_has_cached_data(vp
) && !(syncflag
& FNODSYNC
) &&
2110 (vp
->v_type
== VREG
) && !(IS_SWAPVP(vp
)))
2111 (void) VOP_PUTPAGE(vp
, (offset_t
)0, (size_t)0, B_ASYNC
, cr
, ct
);
2113 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2117 zil_commit(zfsvfs
->z_log
, zp
->z_last_itx
, zp
->z_id
);
2124 * Get the requested file attributes and place them in the provided
2127 * IN: vp - vnode of file.
2128 * vap - va_mask identifies requested attributes.
2129 * If AT_XVATTR set, then optional attrs are requested
2130 * flags - ATTR_NOACLCHECK (CIFS server context)
2131 * cr - credentials of caller.
2132 * ct - caller context
2134 * OUT: vap - attribute values.
2136 * RETURN: 0 (always succeeds)
2140 zfs_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
2141 caller_context_t
*ct
)
2143 znode_t
*zp
= VTOZ(vp
);
2144 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2148 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2149 xoptattr_t
*xoap
= NULL
;
2150 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2156 mutex_enter(&zp
->z_lock
);
2159 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2160 * Also, if we are the owner don't bother, since owner should
2161 * always be allowed to read basic attributes of file.
2163 if (!(pzp
->zp_flags
& ZFS_ACL_TRIVIAL
) &&
2164 (pzp
->zp_uid
!= crgetuid(cr
))) {
2165 if (error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2167 mutex_exit(&zp
->z_lock
);
2174 * Return all attributes. It's cheaper to provide the answer
2175 * than to determine whether we were asked the question.
2178 vap
->va_type
= vp
->v_type
;
2179 vap
->va_mode
= pzp
->zp_mode
& MODEMASK
;
2180 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2181 vap
->va_fsid
= zp
->z_zfsvfs
->z_vfs
->vfs_dev
;
2182 vap
->va_nodeid
= zp
->z_id
;
2183 if ((vp
->v_flag
& VROOT
) && zfs_show_ctldir(zp
))
2184 links
= pzp
->zp_links
+ 1;
2186 links
= pzp
->zp_links
;
2187 vap
->va_nlink
= MIN(links
, UINT32_MAX
); /* nlink_t limit! */
2188 vap
->va_size
= pzp
->zp_size
;
2189 vap
->va_rdev
= vp
->v_rdev
;
2190 vap
->va_seq
= zp
->z_seq
;
2193 * Add in any requested optional attributes and the create time.
2194 * Also set the corresponding bits in the returned attribute bitmap.
2196 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zfsvfs
->z_use_fuids
) {
2197 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2199 ((pzp
->zp_flags
& ZFS_ARCHIVE
) != 0);
2200 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2203 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2204 xoap
->xoa_readonly
=
2205 ((pzp
->zp_flags
& ZFS_READONLY
) != 0);
2206 XVA_SET_RTN(xvap
, XAT_READONLY
);
2209 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2211 ((pzp
->zp_flags
& ZFS_SYSTEM
) != 0);
2212 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2215 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2217 ((pzp
->zp_flags
& ZFS_HIDDEN
) != 0);
2218 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2221 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2222 xoap
->xoa_nounlink
=
2223 ((pzp
->zp_flags
& ZFS_NOUNLINK
) != 0);
2224 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2227 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2228 xoap
->xoa_immutable
=
2229 ((pzp
->zp_flags
& ZFS_IMMUTABLE
) != 0);
2230 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2233 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2234 xoap
->xoa_appendonly
=
2235 ((pzp
->zp_flags
& ZFS_APPENDONLY
) != 0);
2236 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2239 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2241 ((pzp
->zp_flags
& ZFS_NODUMP
) != 0);
2242 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2245 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2247 ((pzp
->zp_flags
& ZFS_OPAQUE
) != 0);
2248 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2251 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2252 xoap
->xoa_av_quarantined
=
2253 ((pzp
->zp_flags
& ZFS_AV_QUARANTINED
) != 0);
2254 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2257 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2258 xoap
->xoa_av_modified
=
2259 ((pzp
->zp_flags
& ZFS_AV_MODIFIED
) != 0);
2260 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2263 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2264 vp
->v_type
== VREG
&&
2265 (pzp
->zp_flags
& ZFS_BONUS_SCANSTAMP
)) {
2267 dmu_object_info_t doi
;
2270 * Only VREG files have anti-virus scanstamps, so we
2271 * won't conflict with symlinks in the bonus buffer.
2273 dmu_object_info_from_db(zp
->z_dbuf
, &doi
);
2274 len
= sizeof (xoap
->xoa_av_scanstamp
) +
2275 sizeof (znode_phys_t
);
2276 if (len
<= doi
.doi_bonus_size
) {
2278 * pzp points to the start of the
2279 * znode_phys_t. pzp + 1 points to the
2280 * first byte after the znode_phys_t.
2282 (void) memcpy(xoap
->xoa_av_scanstamp
,
2284 sizeof (xoap
->xoa_av_scanstamp
));
2285 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
2289 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2290 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, pzp
->zp_crtime
);
2291 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2295 ZFS_TIME_DECODE(&vap
->va_atime
, pzp
->zp_atime
);
2296 ZFS_TIME_DECODE(&vap
->va_mtime
, pzp
->zp_mtime
);
2297 ZFS_TIME_DECODE(&vap
->va_ctime
, pzp
->zp_ctime
);
2299 mutex_exit(&zp
->z_lock
);
2301 dmu_object_size_from_db(zp
->z_dbuf
, &vap
->va_blksize
, &vap
->va_nblocks
);
2303 if (zp
->z_blksz
== 0) {
2305 * Block size hasn't been set; suggest maximal I/O transfers.
2307 vap
->va_blksize
= zfsvfs
->z_max_blksz
;
2315 * Set the file attributes to the values contained in the
2318 * IN: vp - vnode of file to be modified.
2319 * vap - new attribute values.
2320 * If AT_XVATTR set, then optional attrs are being set
2321 * flags - ATTR_UTIME set if non-default time values provided.
2322 * - ATTR_NOACLCHECK (CIFS context only).
2323 * cr - credentials of caller.
2324 * ct - caller context
2326 * RETURN: 0 if success
2327 * error code if failure
2330 * vp - ctime updated, mtime updated if size changed.
2334 zfs_setattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
2335 caller_context_t
*ct
)
2337 znode_t
*zp
= VTOZ(vp
);
2339 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2344 uint_t mask
= vap
->va_mask
;
2349 int need_policy
= FALSE
;
2351 zfs_fuid_info_t
*fuidp
= NULL
;
2352 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2354 zfs_acl_t
*aclp
= NULL
;
2355 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2360 if (mask
& AT_NOSET
)
2367 zilog
= zfsvfs
->z_log
;
2370 * Make sure that if we have ephemeral uid/gid or xvattr specified
2371 * that file system is at proper version level
2374 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
2375 (((mask
& AT_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2376 ((mask
& AT_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2377 (mask
& AT_XVATTR
))) {
2382 if (mask
& AT_SIZE
&& vp
->v_type
== VDIR
) {
2387 if (mask
& AT_SIZE
&& vp
->v_type
!= VREG
&& vp
->v_type
!= VFIFO
) {
2393 * If this is an xvattr_t, then get a pointer to the structure of
2394 * optional attributes. If this is NULL, then we have a vattr_t.
2396 xoap
= xva_getxoptattr(xvap
);
2398 xva_init(&tmpxvattr
);
2401 * Immutable files can only alter immutable bit and atime
2403 if ((pzp
->zp_flags
& ZFS_IMMUTABLE
) &&
2404 ((mask
& (AT_SIZE
|AT_UID
|AT_GID
|AT_MTIME
|AT_MODE
)) ||
2405 ((mask
& AT_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2410 if ((mask
& AT_SIZE
) && (pzp
->zp_flags
& ZFS_READONLY
)) {
2416 * Verify timestamps doesn't overflow 32 bits.
2417 * ZFS can handle large timestamps, but 32bit syscalls can't
2418 * handle times greater than 2039. This check should be removed
2419 * once large timestamps are fully supported.
2421 if (mask
& (AT_ATIME
| AT_MTIME
)) {
2422 if (((mask
& AT_ATIME
) && TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2423 ((mask
& AT_MTIME
) && TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2432 if (zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) {
2438 * First validate permissions
2441 if (mask
& AT_SIZE
) {
2442 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2448 * XXX - Note, we are not providing any open
2449 * mode flags here (like FNDELAY), so we may
2450 * block if there are locks present... this
2451 * should be addressed in openat().
2453 /* XXX - would it be OK to generate a log record here? */
2454 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2461 if (mask
& (AT_ATIME
|AT_MTIME
) ||
2462 ((mask
& AT_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2463 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2464 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2465 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2466 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
))))
2467 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2470 if (mask
& (AT_UID
|AT_GID
)) {
2471 int idmask
= (mask
& (AT_UID
|AT_GID
));
2476 * NOTE: even if a new mode is being set,
2477 * we may clear S_ISUID/S_ISGID bits.
2480 if (!(mask
& AT_MODE
))
2481 vap
->va_mode
= pzp
->zp_mode
;
2484 * Take ownership or chgrp to group we are a member of
2487 take_owner
= (mask
& AT_UID
) && (vap
->va_uid
== crgetuid(cr
));
2488 take_group
= (mask
& AT_GID
) &&
2489 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
2492 * If both AT_UID and AT_GID are set then take_owner and
2493 * take_group must both be set in order to allow taking
2496 * Otherwise, send the check through secpolicy_vnode_setattr()
2500 if (((idmask
== (AT_UID
|AT_GID
)) && take_owner
&& take_group
) ||
2501 ((idmask
== AT_UID
) && take_owner
) ||
2502 ((idmask
== AT_GID
) && take_group
)) {
2503 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2504 skipaclchk
, cr
) == 0) {
2506 * Remove setuid/setgid for non-privileged users
2508 secpolicy_setid_clear(vap
, cr
);
2509 trim_mask
= (mask
& (AT_UID
|AT_GID
));
2518 mutex_enter(&zp
->z_lock
);
2519 oldva
.va_mode
= pzp
->zp_mode
;
2520 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2521 if (mask
& AT_XVATTR
) {
2523 * Update xvattr mask to include only those attributes
2524 * that are actually changing.
2526 * the bits will be restored prior to actually setting
2527 * the attributes so the caller thinks they were set.
2529 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2530 if (xoap
->xoa_appendonly
!=
2531 ((pzp
->zp_flags
& ZFS_APPENDONLY
) != 0)) {
2534 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2535 XVA_SET_REQ(&tmpxvattr
, XAT_APPENDONLY
);
2539 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2540 if (xoap
->xoa_nounlink
!=
2541 ((pzp
->zp_flags
& ZFS_NOUNLINK
) != 0)) {
2544 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2545 XVA_SET_REQ(&tmpxvattr
, XAT_NOUNLINK
);
2549 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2550 if (xoap
->xoa_immutable
!=
2551 ((pzp
->zp_flags
& ZFS_IMMUTABLE
) != 0)) {
2554 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2555 XVA_SET_REQ(&tmpxvattr
, XAT_IMMUTABLE
);
2559 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2560 if (xoap
->xoa_nodump
!=
2561 ((pzp
->zp_flags
& ZFS_NODUMP
) != 0)) {
2564 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2565 XVA_SET_REQ(&tmpxvattr
, XAT_NODUMP
);
2569 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2570 if (xoap
->xoa_av_modified
!=
2571 ((pzp
->zp_flags
& ZFS_AV_MODIFIED
) != 0)) {
2574 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2575 XVA_SET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
);
2579 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2580 if ((vp
->v_type
!= VREG
&&
2581 xoap
->xoa_av_quarantined
) ||
2582 xoap
->xoa_av_quarantined
!=
2583 ((pzp
->zp_flags
& ZFS_AV_QUARANTINED
) != 0)) {
2586 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2587 XVA_SET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
);
2591 if (need_policy
== FALSE
&&
2592 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2593 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2598 mutex_exit(&zp
->z_lock
);
2600 if (mask
& AT_MODE
) {
2601 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2602 err
= secpolicy_setid_setsticky_clear(vp
, vap
,
2608 trim_mask
|= AT_MODE
;
2616 * If trim_mask is set then take ownership
2617 * has been granted or write_acl is present and user
2618 * has the ability to modify mode. In that case remove
2619 * UID|GID and or MODE from mask so that
2620 * secpolicy_vnode_setattr() doesn't revoke it.
2624 saved_mask
= vap
->va_mask
;
2625 vap
->va_mask
&= ~trim_mask
;
2627 err
= secpolicy_vnode_setattr(cr
, vp
, vap
, &oldva
, flags
,
2628 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2635 vap
->va_mask
|= saved_mask
;
2639 * secpolicy_vnode_setattr, or take ownership may have
2642 mask
= vap
->va_mask
;
2644 tx
= dmu_tx_create(zfsvfs
->z_os
);
2645 dmu_tx_hold_bonus(tx
, zp
->z_id
);
2646 if (((mask
& AT_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2647 ((mask
& AT_GID
) && IS_EPHEMERAL(vap
->va_gid
))) {
2648 if (zfsvfs
->z_fuid_obj
== 0) {
2649 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
2650 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
2651 FUID_SIZE_ESTIMATE(zfsvfs
));
2652 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, FALSE
, NULL
);
2654 dmu_tx_hold_bonus(tx
, zfsvfs
->z_fuid_obj
);
2655 dmu_tx_hold_write(tx
, zfsvfs
->z_fuid_obj
, 0,
2656 FUID_SIZE_ESTIMATE(zfsvfs
));
2660 if (mask
& AT_MODE
) {
2661 uint64_t pmode
= pzp
->zp_mode
;
2663 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2665 if (err
= zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
)) {
2670 if (pzp
->zp_acl
.z_acl_extern_obj
) {
2671 /* Are we upgrading ACL from old V0 format to new V1 */
2672 if (zfsvfs
->z_version
<= ZPL_VERSION_FUID
&&
2673 pzp
->zp_acl
.z_acl_version
==
2674 ZFS_ACL_VERSION_INITIAL
) {
2675 dmu_tx_hold_free(tx
,
2676 pzp
->zp_acl
.z_acl_extern_obj
, 0,
2678 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2679 0, aclp
->z_acl_bytes
);
2681 dmu_tx_hold_write(tx
,
2682 pzp
->zp_acl
.z_acl_extern_obj
, 0,
2685 } else if (aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2686 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2687 0, aclp
->z_acl_bytes
);
2691 if ((mask
& (AT_UID
| AT_GID
)) && pzp
->zp_xattr
!= 0) {
2692 err
= zfs_zget(zp
->z_zfsvfs
, pzp
->zp_xattr
, &attrzp
);
2700 dmu_tx_hold_bonus(tx
, attrzp
->z_id
);
2703 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
2706 VN_RELE(ZTOV(attrzp
));
2713 if (err
== ERESTART
) {
2723 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
2726 * Set each attribute requested.
2727 * We group settings according to the locks they need to acquire.
2729 * Note: you cannot set ctime directly, although it will be
2730 * updated as a side-effect of calling this function.
2733 mutex_enter(&zp
->z_lock
);
2735 if (mask
& AT_MODE
) {
2736 mutex_enter(&zp
->z_acl_lock
);
2737 zp
->z_phys
->zp_mode
= new_mode
;
2738 err
= zfs_aclset_common(zp
, aclp
, cr
, &fuidp
, tx
);
2739 ASSERT3U(err
, ==, 0);
2740 mutex_exit(&zp
->z_acl_lock
);
2744 mutex_enter(&attrzp
->z_lock
);
2746 if (mask
& AT_UID
) {
2747 pzp
->zp_uid
= zfs_fuid_create(zfsvfs
,
2748 vap
->va_uid
, cr
, ZFS_OWNER
, tx
, &fuidp
);
2750 attrzp
->z_phys
->zp_uid
= zfs_fuid_create(zfsvfs
,
2751 vap
->va_uid
, cr
, ZFS_OWNER
, tx
, &fuidp
);
2755 if (mask
& AT_GID
) {
2756 pzp
->zp_gid
= zfs_fuid_create(zfsvfs
, vap
->va_gid
,
2757 cr
, ZFS_GROUP
, tx
, &fuidp
);
2759 attrzp
->z_phys
->zp_gid
= zfs_fuid_create(zfsvfs
,
2760 vap
->va_gid
, cr
, ZFS_GROUP
, tx
, &fuidp
);
2767 mutex_exit(&attrzp
->z_lock
);
2769 if (mask
& AT_ATIME
)
2770 ZFS_TIME_ENCODE(&vap
->va_atime
, pzp
->zp_atime
);
2772 if (mask
& AT_MTIME
)
2773 ZFS_TIME_ENCODE(&vap
->va_mtime
, pzp
->zp_mtime
);
2775 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2777 zfs_time_stamper_locked(zp
, CONTENT_MODIFIED
, tx
);
2779 zfs_time_stamper_locked(zp
, STATE_CHANGED
, tx
);
2781 * Do this after setting timestamps to prevent timestamp
2782 * update from toggling bit
2785 if (xoap
&& (mask
& AT_XVATTR
)) {
2788 * restore trimmed off masks
2789 * so that return masks can be set for caller.
2792 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_APPENDONLY
)) {
2793 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2795 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NOUNLINK
)) {
2796 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2798 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_IMMUTABLE
)) {
2799 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2801 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NODUMP
)) {
2802 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2804 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
)) {
2805 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2807 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
)) {
2808 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2811 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
2813 dmu_object_info_t doi
;
2815 ASSERT(vp
->v_type
== VREG
);
2817 /* Grow the bonus buffer if necessary. */
2818 dmu_object_info_from_db(zp
->z_dbuf
, &doi
);
2819 len
= sizeof (xoap
->xoa_av_scanstamp
) +
2820 sizeof (znode_phys_t
);
2821 if (len
> doi
.doi_bonus_size
)
2822 VERIFY(dmu_set_bonus(zp
->z_dbuf
, len
, tx
) == 0);
2824 zfs_xvattr_set(zp
, xvap
);
2828 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
2831 zfs_fuid_info_free(fuidp
);
2832 mutex_exit(&zp
->z_lock
);
2835 VN_RELE(ZTOV(attrzp
));
2843 typedef struct zfs_zlock
{
2844 krwlock_t
*zl_rwlock
; /* lock we acquired */
2845 znode_t
*zl_znode
; /* znode we held */
2846 struct zfs_zlock
*zl_next
; /* next in list */
2850 * Drop locks and release vnodes that were held by zfs_rename_lock().
2853 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
2857 while ((zl
= *zlpp
) != NULL
) {
2858 if (zl
->zl_znode
!= NULL
)
2859 VN_RELE(ZTOV(zl
->zl_znode
));
2860 rw_exit(zl
->zl_rwlock
);
2861 *zlpp
= zl
->zl_next
;
2862 kmem_free(zl
, sizeof (*zl
));
2867 * Search back through the directory tree, using the ".." entries.
2868 * Lock each directory in the chain to prevent concurrent renames.
2869 * Fail any attempt to move a directory into one of its own descendants.
2870 * XXX - z_parent_lock can overlap with map or grow locks
2873 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
2877 uint64_t rootid
= zp
->z_zfsvfs
->z_root
;
2878 uint64_t *oidp
= &zp
->z_id
;
2879 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
2880 krw_t rw
= RW_WRITER
;
2883 * First pass write-locks szp and compares to zp->z_id.
2884 * Later passes read-lock zp and compare to zp->z_parent.
2887 if (!rw_tryenter(rwlp
, rw
)) {
2889 * Another thread is renaming in this path.
2890 * Note that if we are a WRITER, we don't have any
2891 * parent_locks held yet.
2893 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
2895 * Drop our locks and restart
2897 zfs_rename_unlock(&zl
);
2901 rwlp
= &szp
->z_parent_lock
;
2906 * Wait for other thread to drop its locks
2912 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
2913 zl
->zl_rwlock
= rwlp
;
2914 zl
->zl_znode
= NULL
;
2915 zl
->zl_next
= *zlpp
;
2918 if (*oidp
== szp
->z_id
) /* We're a descendant of szp */
2921 if (*oidp
== rootid
) /* We've hit the top */
2924 if (rw
== RW_READER
) { /* i.e. not the first pass */
2925 int error
= zfs_zget(zp
->z_zfsvfs
, *oidp
, &zp
);
2930 oidp
= &zp
->z_phys
->zp_parent
;
2931 rwlp
= &zp
->z_parent_lock
;
2934 } while (zp
->z_id
!= sdzp
->z_id
);
2940 * Move an entry from the provided source directory to the target
2941 * directory. Change the entry name as indicated.
2943 * IN: sdvp - Source directory containing the "old entry".
2944 * snm - Old entry name.
2945 * tdvp - Target directory to contain the "new entry".
2946 * tnm - New entry name.
2947 * cr - credentials of caller.
2948 * ct - caller context
2949 * flags - case flags
2951 * RETURN: 0 if success
2952 * error code if failure
2955 * sdvp,tdvp - ctime|mtime updated
2959 zfs_rename(vnode_t
*sdvp
, char *snm
, vnode_t
*tdvp
, char *tnm
, cred_t
*cr
,
2960 caller_context_t
*ct
, int flags
)
2962 znode_t
*tdzp
, *szp
, *tzp
;
2963 znode_t
*sdzp
= VTOZ(sdvp
);
2964 zfsvfs_t
*zfsvfs
= sdzp
->z_zfsvfs
;
2967 zfs_dirlock_t
*sdl
, *tdl
;
2970 int cmp
, serr
, terr
;
2975 ZFS_VERIFY_ZP(sdzp
);
2976 zilog
= zfsvfs
->z_log
;
2979 * Make sure we have the real vp for the target directory.
2981 if (VOP_REALVP(tdvp
, &realvp
, ct
) == 0)
2984 if (tdvp
->v_vfsp
!= sdvp
->v_vfsp
) {
2990 ZFS_VERIFY_ZP(tdzp
);
2991 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
2992 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
2997 if (flags
& FIGNORECASE
)
3006 * This is to prevent the creation of links into attribute space
3007 * by renaming a linked file into/outof an attribute directory.
3008 * See the comment in zfs_link() for why this is considered bad.
3010 if ((tdzp
->z_phys
->zp_flags
& ZFS_XATTR
) !=
3011 (sdzp
->z_phys
->zp_flags
& ZFS_XATTR
)) {
3017 * Lock source and target directory entries. To prevent deadlock,
3018 * a lock ordering must be defined. We lock the directory with
3019 * the smallest object id first, or if it's a tie, the one with
3020 * the lexically first name.
3022 if (sdzp
->z_id
< tdzp
->z_id
) {
3024 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3028 * First compare the two name arguments without
3029 * considering any case folding.
3031 int nofold
= (zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3033 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3034 ASSERT(error
== 0 || !zfsvfs
->z_utf8
);
3037 * POSIX: "If the old argument and the new argument
3038 * both refer to links to the same existing file,
3039 * the rename() function shall return successfully
3040 * and perform no other action."
3046 * If the file system is case-folding, then we may
3047 * have some more checking to do. A case-folding file
3048 * system is either supporting mixed case sensitivity
3049 * access or is completely case-insensitive. Note
3050 * that the file system is always case preserving.
3052 * In mixed sensitivity mode case sensitive behavior
3053 * is the default. FIGNORECASE must be used to
3054 * explicitly request case insensitive behavior.
3056 * If the source and target names provided differ only
3057 * by case (e.g., a request to rename 'tim' to 'Tim'),
3058 * we will treat this as a special case in the
3059 * case-insensitive mode: as long as the source name
3060 * is an exact match, we will allow this to proceed as
3061 * a name-change request.
3063 if ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
3064 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
3065 flags
& FIGNORECASE
)) &&
3066 u8_strcmp(snm
, tnm
, 0, zfsvfs
->z_norm
, U8_UNICODE_LATEST
,
3069 * case preserving rename request, require exact
3078 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3079 ZEXISTS
| zflg
, NULL
, NULL
);
3080 terr
= zfs_dirent_lock(&tdl
,
3081 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3083 terr
= zfs_dirent_lock(&tdl
,
3084 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3085 serr
= zfs_dirent_lock(&sdl
,
3086 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3092 * Source entry invalid or not there.
3095 zfs_dirent_unlock(tdl
);
3099 if (strcmp(snm
, "..") == 0)
3105 zfs_dirent_unlock(sdl
);
3107 if (strcmp(tnm
, "..") == 0)
3114 * Must have write access at the source to remove the old entry
3115 * and write access at the target to create the new entry.
3116 * Note that if target and source are the same, this can be
3117 * done in a single check.
3120 if (error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
))
3123 if (ZTOV(szp
)->v_type
== VDIR
) {
3125 * Check to make sure rename is valid.
3126 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3128 if (error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
))
3133 * Does target exist?
3137 * Source and target must be the same type.
3139 if (ZTOV(szp
)->v_type
== VDIR
) {
3140 if (ZTOV(tzp
)->v_type
!= VDIR
) {
3145 if (ZTOV(tzp
)->v_type
== VDIR
) {
3151 * POSIX dictates that when the source and target
3152 * entries refer to the same file object, rename
3153 * must do nothing and exit without error.
3155 if (szp
->z_id
== tzp
->z_id
) {
3161 vnevent_rename_src(ZTOV(szp
), sdvp
, snm
, ct
);
3163 vnevent_rename_dest(ZTOV(tzp
), tdvp
, tnm
, ct
);
3166 * notify the target directory if it is not the same
3167 * as source directory.
3170 vnevent_rename_dest_dir(tdvp
, ct
);
3173 tx
= dmu_tx_create(zfsvfs
->z_os
);
3174 dmu_tx_hold_bonus(tx
, szp
->z_id
); /* nlink changes */
3175 dmu_tx_hold_bonus(tx
, sdzp
->z_id
); /* nlink changes */
3176 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3177 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3179 dmu_tx_hold_bonus(tx
, tdzp
->z_id
); /* nlink changes */
3181 dmu_tx_hold_bonus(tx
, tzp
->z_id
); /* parent changes */
3182 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3183 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3186 zfs_rename_unlock(&zl
);
3187 zfs_dirent_unlock(sdl
);
3188 zfs_dirent_unlock(tdl
);
3192 if (error
== ERESTART
) {
3202 if (tzp
) /* Attempt to remove the existing target */
3203 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3206 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3208 szp
->z_phys
->zp_flags
|= ZFS_AV_MODIFIED
;
3210 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3213 zfs_log_rename(zilog
, tx
,
3214 TX_RENAME
| (flags
& FIGNORECASE
? TX_CI
: 0),
3215 sdzp
, sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3217 /* Update path information for the target vnode */
3218 vn_renamepath(tdvp
, ZTOV(szp
), tnm
, strlen(tnm
));
3225 zfs_rename_unlock(&zl
);
3227 zfs_dirent_unlock(sdl
);
3228 zfs_dirent_unlock(tdl
);
3239 * Insert the indicated symbolic reference entry into the directory.
3241 * IN: dvp - Directory to contain new symbolic link.
3242 * link - Name for new symlink entry.
3243 * vap - Attributes of new entry.
3244 * target - Target path of new symlink.
3245 * cr - credentials of caller.
3246 * ct - caller context
3247 * flags - case flags
3249 * RETURN: 0 if success
3250 * error code if failure
3253 * dvp - ctime|mtime updated
3257 zfs_symlink(vnode_t
*dvp
, char *name
, vattr_t
*vap
, char *link
, cred_t
*cr
,
3258 caller_context_t
*ct
, int flags
)
3260 znode_t
*zp
, *dzp
= VTOZ(dvp
);
3263 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
3265 int len
= strlen(link
);
3268 zfs_fuid_info_t
*fuidp
= NULL
;
3270 ASSERT(vap
->va_type
== VLNK
);
3274 zilog
= zfsvfs
->z_log
;
3276 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
3277 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3281 if (flags
& FIGNORECASE
)
3284 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
3289 if (len
> MAXPATHLEN
) {
3291 return (ENAMETOOLONG
);
3295 * Attempt to lock directory; fail if entry already exists.
3297 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3303 tx
= dmu_tx_create(zfsvfs
->z_os
);
3304 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3305 dmu_tx_hold_bonus(tx
, dzp
->z_id
);
3306 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3307 if (dzp
->z_phys
->zp_flags
& ZFS_INHERIT_ACE
)
3308 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, SPA_MAXBLOCKSIZE
);
3309 if (IS_EPHEMERAL(crgetuid(cr
)) || IS_EPHEMERAL(crgetgid(cr
))) {
3310 if (zfsvfs
->z_fuid_obj
== 0) {
3311 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
3312 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3313 FUID_SIZE_ESTIMATE(zfsvfs
));
3314 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, FALSE
, NULL
);
3316 dmu_tx_hold_bonus(tx
, zfsvfs
->z_fuid_obj
);
3317 dmu_tx_hold_write(tx
, zfsvfs
->z_fuid_obj
, 0,
3318 FUID_SIZE_ESTIMATE(zfsvfs
));
3321 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3323 zfs_dirent_unlock(dl
);
3324 if (error
== ERESTART
) {
3334 dmu_buf_will_dirty(dzp
->z_dbuf
, tx
);
3337 * Create a new object for the symlink.
3338 * Put the link content into bonus buffer if it will fit;
3339 * otherwise, store it just like any other file data.
3341 if (sizeof (znode_phys_t
) + len
<= dmu_bonus_max()) {
3342 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, len
, NULL
, &fuidp
);
3344 bcopy(link
, zp
->z_phys
+ 1, len
);
3348 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, 0, NULL
, &fuidp
);
3350 * Nothing can access the znode yet so no locking needed
3351 * for growing the znode's blocksize.
3353 zfs_grow_blocksize(zp
, len
, tx
);
3355 VERIFY(0 == dmu_buf_hold(zfsvfs
->z_os
,
3356 zp
->z_id
, 0, FTAG
, &dbp
));
3357 dmu_buf_will_dirty(dbp
, tx
);
3359 ASSERT3U(len
, <=, dbp
->db_size
);
3360 bcopy(link
, dbp
->db_data
, len
);
3361 dmu_buf_rele(dbp
, FTAG
);
3363 zp
->z_phys
->zp_size
= len
;
3366 * Insert the new object into the directory.
3368 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3371 uint64_t txtype
= TX_SYMLINK
;
3372 if (flags
& FIGNORECASE
)
3374 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3377 zfs_fuid_info_free(fuidp
);
3381 zfs_dirent_unlock(dl
);
3390 * Return, in the buffer contained in the provided uio structure,
3391 * the symbolic path referred to by vp.
3393 * IN: vp - vnode of symbolic link.
3394 * uoip - structure to contain the link path.
3395 * cr - credentials of caller.
3396 * ct - caller context
3398 * OUT: uio - structure to contain the link path.
3400 * RETURN: 0 if success
3401 * error code if failure
3404 * vp - atime updated
3408 zfs_readlink(vnode_t
*vp
, uio_t
*uio
, cred_t
*cr
, caller_context_t
*ct
)
3410 znode_t
*zp
= VTOZ(vp
);
3411 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3418 bufsz
= (size_t)zp
->z_phys
->zp_size
;
3419 if (bufsz
+ sizeof (znode_phys_t
) <= zp
->z_dbuf
->db_size
) {
3420 error
= uiomove(zp
->z_phys
+ 1,
3421 MIN((size_t)bufsz
, uio
->uio_resid
), UIO_READ
, uio
);
3424 error
= dmu_buf_hold(zfsvfs
->z_os
, zp
->z_id
, 0, FTAG
, &dbp
);
3429 error
= uiomove(dbp
->db_data
,
3430 MIN((size_t)bufsz
, uio
->uio_resid
), UIO_READ
, uio
);
3431 dmu_buf_rele(dbp
, FTAG
);
3434 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
3440 * Insert a new entry into directory tdvp referencing svp.
3442 * IN: tdvp - Directory to contain new entry.
3443 * svp - vnode of new entry.
3444 * name - name of new entry.
3445 * cr - credentials of caller.
3446 * ct - caller context
3448 * RETURN: 0 if success
3449 * error code if failure
3452 * tdvp - ctime|mtime updated
3453 * svp - ctime updated
3457 zfs_link(vnode_t
*tdvp
, vnode_t
*svp
, char *name
, cred_t
*cr
,
3458 caller_context_t
*ct
, int flags
)
3460 znode_t
*dzp
= VTOZ(tdvp
);
3462 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
3471 ASSERT(tdvp
->v_type
== VDIR
);
3475 zilog
= zfsvfs
->z_log
;
3477 if (VOP_REALVP(svp
, &realvp
, ct
) == 0)
3480 if (svp
->v_vfsp
!= tdvp
->v_vfsp
) {
3487 if (zfsvfs
->z_utf8
&& u8_validate(name
,
3488 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3492 if (flags
& FIGNORECASE
)
3497 * We do not support links between attributes and non-attributes
3498 * because of the potential security risk of creating links
3499 * into "normal" file space in order to circumvent restrictions
3500 * imposed in attribute space.
3502 if ((szp
->z_phys
->zp_flags
& ZFS_XATTR
) !=
3503 (dzp
->z_phys
->zp_flags
& ZFS_XATTR
)) {
3509 * POSIX dictates that we return EPERM here.
3510 * Better choices include ENOTSUP or EISDIR.
3512 if (svp
->v_type
== VDIR
) {
3517 owner
= zfs_fuid_map_id(zfsvfs
, szp
->z_phys
->zp_uid
, cr
, ZFS_OWNER
);
3518 if (owner
!= crgetuid(cr
) &&
3519 secpolicy_basic_link(cr
) != 0) {
3524 if (error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
)) {
3530 * Attempt to lock directory; fail if entry already exists.
3532 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
3538 tx
= dmu_tx_create(zfsvfs
->z_os
);
3539 dmu_tx_hold_bonus(tx
, szp
->z_id
);
3540 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3541 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3543 zfs_dirent_unlock(dl
);
3544 if (error
== ERESTART
) {
3554 error
= zfs_link_create(dl
, szp
, tx
, 0);
3557 uint64_t txtype
= TX_LINK
;
3558 if (flags
& FIGNORECASE
)
3560 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
3565 zfs_dirent_unlock(dl
);
3568 vnevent_link(svp
, ct
);
3576 * zfs_null_putapage() is used when the file system has been force
3577 * unmounted. It just drops the pages.
3581 zfs_null_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
,
3582 size_t *lenp
, int flags
, cred_t
*cr
)
3584 pvn_write_done(pp
, B_INVAL
|B_FORCE
|B_ERROR
);
3589 * Push a page out to disk, klustering if possible.
3591 * IN: vp - file to push page to.
3592 * pp - page to push.
3593 * flags - additional flags.
3594 * cr - credentials of caller.
3596 * OUT: offp - start of range pushed.
3597 * lenp - len of range pushed.
3599 * RETURN: 0 if success
3600 * error code if failure
3602 * NOTE: callers must have locked the page to be pushed. On
3603 * exit, the page (and all other pages in the kluster) must be
3608 zfs_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
,
3609 size_t *lenp
, int flags
, cred_t
*cr
)
3611 znode_t
*zp
= VTOZ(vp
);
3612 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3613 zilog_t
*zilog
= zfsvfs
->z_log
;
3616 u_offset_t off
, koff
;
3621 filesz
= zp
->z_phys
->zp_size
;
3625 * If our blocksize is bigger than the page size, try to kluster
3626 * multiple pages so that we write a full block (thus avoiding
3627 * a read-modify-write).
3629 if (off
< filesz
&& zp
->z_blksz
> PAGESIZE
) {
3630 if (!ISP2(zp
->z_blksz
)) {
3631 /* Only one block in the file. */
3632 klen
= P2ROUNDUP((ulong_t
)zp
->z_blksz
, PAGESIZE
);
3636 koff
= P2ALIGN(off
, (u_offset_t
)klen
);
3638 ASSERT(koff
<= filesz
);
3639 if (koff
+ klen
> filesz
)
3640 klen
= P2ROUNDUP(filesz
- koff
, (uint64_t)PAGESIZE
);
3641 pp
= pvn_write_kluster(vp
, pp
, &off
, &len
, koff
, klen
, flags
);
3643 ASSERT3U(btop(len
), ==, btopr(len
));
3645 rl
= zfs_range_lock(zp
, off
, len
, RL_WRITER
);
3647 * Can't push pages past end-of-file.
3649 filesz
= zp
->z_phys
->zp_size
;
3650 if (off
>= filesz
) {
3651 /* ignore all pages */
3654 } else if (off
+ len
> filesz
) {
3655 int npages
= btopr(filesz
- off
);
3658 page_list_break(&pp
, &trunc
, npages
);
3659 /* ignore pages past end of file */
3661 pvn_write_done(trunc
, flags
);
3665 tx
= dmu_tx_create(zfsvfs
->z_os
);
3666 dmu_tx_hold_write(tx
, zp
->z_id
, off
, len
);
3667 dmu_tx_hold_bonus(tx
, zp
->z_id
);
3668 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3670 if (err
== ERESTART
) {
3671 zfs_range_unlock(rl
);
3681 if (zp
->z_blksz
<= PAGESIZE
) {
3682 caddr_t va
= zfs_map_page(pp
, S_READ
);
3683 ASSERT3U(len
, <=, PAGESIZE
);
3684 dmu_write(zfsvfs
->z_os
, zp
->z_id
, off
, len
, va
, tx
);
3685 zfs_unmap_page(pp
, va
);
3687 err
= dmu_write_pages(zfsvfs
->z_os
, zp
->z_id
, off
, len
, pp
, tx
);
3691 zfs_time_stamper(zp
, CONTENT_MODIFIED
, tx
);
3692 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, off
, len
, 0);
3697 zfs_range_unlock(rl
);
3698 pvn_write_done(pp
, (err
? B_ERROR
: 0) | flags
);
3708 * Copy the portion of the file indicated from pages into the file.
3709 * The pages are stored in a page list attached to the files vnode.
3711 * IN: vp - vnode of file to push page data to.
3712 * off - position in file to put data.
3713 * len - amount of data to write.
3714 * flags - flags to control the operation.
3715 * cr - credentials of caller.
3716 * ct - caller context.
3718 * RETURN: 0 if success
3719 * error code if failure
3722 * vp - ctime|mtime updated
3726 zfs_putpage(vnode_t
*vp
, offset_t off
, size_t len
, int flags
, cred_t
*cr
,
3727 caller_context_t
*ct
)
3729 znode_t
*zp
= VTOZ(vp
);
3730 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3742 * Search the entire vp list for pages >= off.
3744 error
= pvn_vplist_dirty(vp
, (u_offset_t
)off
, zfs_putapage
,
3749 filesz
= zp
->z_phys
->zp_size
; /* get consistent copy of zp_size */
3751 /* past end of file */
3756 len
= MIN(len
, filesz
- off
);
3758 for (io_off
= off
; io_off
< off
+ len
; io_off
+= io_len
) {
3759 if ((flags
& B_INVAL
) || ((flags
& B_ASYNC
) == 0)) {
3760 pp
= page_lookup(vp
, io_off
,
3761 (flags
& (B_INVAL
| B_FREE
)) ? SE_EXCL
: SE_SHARED
);
3763 pp
= page_lookup_nowait(vp
, io_off
,
3764 (flags
& B_FREE
) ? SE_EXCL
: SE_SHARED
);
3767 if (pp
!= NULL
&& pvn_getdirty(pp
, flags
)) {
3771 * Found a dirty page to push
3773 err
= zfs_putapage(vp
, pp
, &io_off
, &io_len
, flags
, cr
);
3781 if ((flags
& B_ASYNC
) == 0)
3782 zil_commit(zfsvfs
->z_log
, UINT64_MAX
, zp
->z_id
);
3789 zfs_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
3791 znode_t
*zp
= VTOZ(vp
);
3792 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3795 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_READER
);
3796 if (zp
->z_dbuf
== NULL
) {
3798 * The fs has been unmounted, or we did a
3799 * suspend/resume and this file no longer exists.
3801 if (vn_has_cached_data(vp
)) {
3802 (void) pvn_vplist_dirty(vp
, 0, zfs_null_putapage
,
3806 mutex_enter(&zp
->z_lock
);
3807 vp
->v_count
= 0; /* count arrives as 1 */
3808 mutex_exit(&zp
->z_lock
);
3809 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
3815 * Attempt to push any data in the page cache. If this fails
3816 * we will get kicked out later in zfs_zinactive().
3818 if (vn_has_cached_data(vp
)) {
3819 (void) pvn_vplist_dirty(vp
, 0, zfs_putapage
, B_INVAL
|B_ASYNC
,
3823 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
3824 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
3826 dmu_tx_hold_bonus(tx
, zp
->z_id
);
3827 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3831 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
3832 mutex_enter(&zp
->z_lock
);
3833 zp
->z_atime_dirty
= 0;
3834 mutex_exit(&zp
->z_lock
);
3840 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
3844 * Bounds-check the seek operation.
3846 * IN: vp - vnode seeking within
3847 * ooff - old file offset
3848 * noffp - pointer to new file offset
3849 * ct - caller context
3851 * RETURN: 0 if success
3852 * EINVAL if new offset invalid
3856 zfs_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
,
3857 caller_context_t
*ct
)
3859 if (vp
->v_type
== VDIR
)
3861 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
3865 * Pre-filter the generic locking function to trap attempts to place
3866 * a mandatory lock on a memory mapped file.
3869 zfs_frlock(vnode_t
*vp
, int cmd
, flock64_t
*bfp
, int flag
, offset_t offset
,
3870 flk_callback_t
*flk_cbp
, cred_t
*cr
, caller_context_t
*ct
)
3872 znode_t
*zp
= VTOZ(vp
);
3873 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3880 * We are following the UFS semantics with respect to mapcnt
3881 * here: If we see that the file is mapped already, then we will
3882 * return an error, but we don't worry about races between this
3883 * function and zfs_map().
3885 if (zp
->z_mapcnt
> 0 && MANDMODE((mode_t
)zp
->z_phys
->zp_mode
)) {
3889 error
= fs_frlock(vp
, cmd
, bfp
, flag
, offset
, flk_cbp
, cr
, ct
);
3895 * If we can't find a page in the cache, we will create a new page
3896 * and fill it with file data. For efficiency, we may try to fill
3897 * multiple pages at once (klustering).
3900 zfs_fillpage(vnode_t
*vp
, u_offset_t off
, struct seg
*seg
,
3901 caddr_t addr
, page_t
*pl
[], size_t plsz
, enum seg_rw rw
)
3903 znode_t
*zp
= VTOZ(vp
);
3904 page_t
*pp
, *cur_pp
;
3905 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
3907 u_offset_t io_off
, total
;
3908 uint64_t oid
= zp
->z_id
;
3914 * If we are only asking for a single page don't bother klustering.
3916 filesz
= zp
->z_phys
->zp_size
; /* get consistent copy of zp_size */
3919 if (plsz
== PAGESIZE
|| zp
->z_blksz
<= PAGESIZE
) {
3922 pp
= page_create_va(vp
, io_off
, io_len
, PG_WAIT
, seg
, addr
);
3925 * Try to fill a kluster of pages (a blocks worth).
3930 if (!ISP2(zp
->z_blksz
)) {
3931 /* Only one block in the file. */
3932 klen
= P2ROUNDUP((ulong_t
)zp
->z_blksz
, PAGESIZE
);
3936 * It would be ideal to align our offset to the
3937 * blocksize but doing so has resulted in some
3938 * strange application crashes. For now, we
3939 * leave the offset as is and only adjust the
3940 * length if we are off the end of the file.
3945 ASSERT(koff
<= filesz
);
3946 if (koff
+ klen
> filesz
)
3947 klen
= P2ROUNDUP(filesz
, (uint64_t)PAGESIZE
) - koff
;
3948 ASSERT3U(off
, >=, koff
);
3949 ASSERT3U(off
, <, koff
+ klen
);
3950 pp
= pvn_read_kluster(vp
, off
, seg
, addr
, &io_off
,
3951 &io_len
, koff
, klen
, 0);
3955 * Some other thread entered the page before us.
3956 * Return to zfs_getpage to retry the lookup.
3963 * Fill the pages in the kluster.
3966 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
3967 ASSERT3U(io_off
, ==, cur_pp
->p_offset
);
3968 va
= zfs_map_page(cur_pp
, S_WRITE
);
3969 err
= dmu_read(os
, oid
, io_off
, PAGESIZE
, va
);
3970 zfs_unmap_page(cur_pp
, va
);
3972 /* On error, toss the entire kluster */
3973 pvn_read_done(pp
, B_ERROR
);
3974 /* convert checksum errors into IO errors */
3979 cur_pp
= cur_pp
->p_next
;
3983 * Fill in the page list array from the kluster. If
3984 * there are too many pages in the kluster, return
3985 * as many pages as possible starting from the desired
3987 * NOTE: the page list will always be null terminated.
3989 pvn_plist_init(pp
, pl
, plsz
, off
, io_len
, rw
);
3995 * Return pointers to the pages for the file region [off, off + len]
3996 * in the pl array. If plsz is greater than len, this function may
3997 * also return page pointers from before or after the specified
3998 * region (i.e. some region [off', off' + plsz]). These additional
3999 * pages are only returned if they are already in the cache, or were
4000 * created as part of a klustered read.
4002 * IN: vp - vnode of file to get data from.
4003 * off - position in file to get data from.
4004 * len - amount of data to retrieve.
4005 * plsz - length of provided page list.
4006 * seg - segment to obtain pages for.
4007 * addr - virtual address of fault.
4008 * rw - mode of created pages.
4009 * cr - credentials of caller.
4010 * ct - caller context.
4012 * OUT: protp - protection mode of created pages.
4013 * pl - list of pages created.
4015 * RETURN: 0 if success
4016 * error code if failure
4019 * vp - atime updated
4023 zfs_getpage(vnode_t
*vp
, offset_t off
, size_t len
, uint_t
*protp
,
4024 page_t
*pl
[], size_t plsz
, struct seg
*seg
, caddr_t addr
,
4025 enum seg_rw rw
, cred_t
*cr
, caller_context_t
*ct
)
4027 znode_t
*zp
= VTOZ(vp
);
4028 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4029 page_t
*pp
, **pl0
= pl
;
4030 int need_unlock
= 0, err
= 0;
4039 /* no faultahead (for now) */
4045 /* can't fault past EOF */
4046 if (off
>= zp
->z_phys
->zp_size
) {
4053 * If we already own the lock, then we must be page faulting
4054 * in the middle of a write to this file (i.e., we are writing
4055 * to this file using data from a mapped region of the file).
4057 if (rw_owner(&zp
->z_map_lock
) != curthread
) {
4058 rw_enter(&zp
->z_map_lock
, RW_WRITER
);
4063 * Loop through the requested range [off, off + len] looking
4064 * for pages. If we don't find a page, we will need to create
4065 * a new page and fill it with data from the file.
4068 if (plsz
< PAGESIZE
)
4070 if (pp
= page_lookup(vp
, off
, SE_SHARED
)) {
4077 err
= zfs_fillpage(vp
, off
, seg
, addr
, pl
, plsz
, rw
);
4081 * klustering may have changed our region
4082 * to be block aligned.
4084 if (((pp
= *pl
) != 0) && (off
!= pp
->p_offset
)) {
4085 int delta
= off
- pp
->p_offset
;
4104 * Fill out the page array with any pages already in the cache.
4107 pp
= page_lookup_nowait(vp
, off
, SE_SHARED
);
4115 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
4118 * We can't grab the range lock for the page as reader which would
4119 * stop truncation as this leads to deadlock. So we need to recheck
4122 if (orig_off
>= zp
->z_phys
->zp_size
)
4126 * Release any pages we have previously locked.
4135 rw_exit(&zp
->z_map_lock
);
4142 * Request a memory map for a section of a file. This code interacts
4143 * with common code and the VM system as follows:
4145 * common code calls mmap(), which ends up in smmap_common()
4147 * this calls VOP_MAP(), which takes you into (say) zfs
4149 * zfs_map() calls as_map(), passing segvn_create() as the callback
4151 * segvn_create() creates the new segment and calls VOP_ADDMAP()
4153 * zfs_addmap() updates z_mapcnt
4157 zfs_map(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t
*addrp
,
4158 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
4159 caller_context_t
*ct
)
4161 znode_t
*zp
= VTOZ(vp
);
4162 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4163 segvn_crargs_t vn_a
;
4169 if ((prot
& PROT_WRITE
) &&
4170 (zp
->z_phys
->zp_flags
& (ZFS_IMMUTABLE
| ZFS_READONLY
|
4176 if ((prot
& (PROT_READ
| PROT_EXEC
)) &&
4177 (zp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
)) {
4182 if (vp
->v_flag
& VNOMAP
) {
4187 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4192 if (vp
->v_type
!= VREG
) {
4198 * If file is locked, disallow mapping.
4200 if (MANDMODE((mode_t
)zp
->z_phys
->zp_mode
) && vn_has_flocks(vp
)) {
4206 error
= choose_addr(as
, addrp
, len
, off
, ADDR_VACALIGN
, flags
);
4214 vn_a
.offset
= (u_offset_t
)off
;
4215 vn_a
.type
= flags
& MAP_TYPE
;
4217 vn_a
.maxprot
= maxprot
;
4220 vn_a
.flags
= flags
& ~MAP_TYPE
;
4222 vn_a
.lgrp_mem_policy_flags
= 0;
4224 error
= as_map(as
, *addrp
, len
, segvn_create
, &vn_a
);
4233 zfs_addmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
4234 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
4235 caller_context_t
*ct
)
4237 uint64_t pages
= btopr(len
);
4239 atomic_add_64(&VTOZ(vp
)->z_mapcnt
, pages
);
4244 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4245 * more accurate mtime for the associated file. Since we don't have a way of
4246 * detecting when the data was actually modified, we have to resort to
4247 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4248 * last page is pushed. The problem occurs when the msync() call is omitted,
4249 * which by far the most common case:
4257 * putpage() via fsflush
4259 * If we wait until fsflush to come along, we can have a modification time that
4260 * is some arbitrary point in the future. In order to prevent this in the
4261 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4266 zfs_delmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
4267 size_t len
, uint_t prot
, uint_t maxprot
, uint_t flags
, cred_t
*cr
,
4268 caller_context_t
*ct
)
4270 uint64_t pages
= btopr(len
);
4272 ASSERT3U(VTOZ(vp
)->z_mapcnt
, >=, pages
);
4273 atomic_add_64(&VTOZ(vp
)->z_mapcnt
, -pages
);
4275 if ((flags
& MAP_SHARED
) && (prot
& PROT_WRITE
) &&
4276 vn_has_cached_data(vp
))
4277 (void) VOP_PUTPAGE(vp
, off
, len
, B_ASYNC
, cr
, ct
);
4283 * Free or allocate space in a file. Currently, this function only
4284 * supports the `F_FREESP' command. However, this command is somewhat
4285 * misnamed, as its functionality includes the ability to allocate as
4286 * well as free space.
4288 * IN: vp - vnode of file to free data in.
4289 * cmd - action to take (only F_FREESP supported).
4290 * bfp - section of file to free/alloc.
4291 * flag - current file open mode flags.
4292 * offset - current file offset.
4293 * cr - credentials of caller [UNUSED].
4294 * ct - caller context.
4296 * RETURN: 0 if success
4297 * error code if failure
4300 * vp - ctime|mtime updated
4304 zfs_space(vnode_t
*vp
, int cmd
, flock64_t
*bfp
, int flag
,
4305 offset_t offset
, cred_t
*cr
, caller_context_t
*ct
)
4307 znode_t
*zp
= VTOZ(vp
);
4308 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4315 if (cmd
!= F_FREESP
) {
4320 if (error
= convoff(vp
, bfp
, 0, offset
)) {
4325 if (bfp
->l_len
< 0) {
4331 len
= bfp
->l_len
; /* 0 means from off to end of file */
4333 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4341 zfs_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
4343 znode_t
*zp
= VTOZ(vp
);
4344 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4346 uint64_t object
= zp
->z_id
;
4352 gen
= (uint32_t)zp
->z_gen
;
4354 size
= (zfsvfs
->z_parent
!= zfsvfs
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4355 if (fidp
->fid_len
< size
) {
4356 fidp
->fid_len
= size
;
4361 zfid
= (zfid_short_t
*)fidp
;
4363 zfid
->zf_len
= size
;
4365 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4366 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4368 /* Must have a non-zero generation number to distinguish from .zfs */
4371 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4372 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4374 if (size
== LONG_FID_LEN
) {
4375 uint64_t objsetid
= dmu_objset_id(zfsvfs
->z_os
);
4378 zlfid
= (zfid_long_t
*)fidp
;
4380 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4381 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4383 /* XXX - this should be the generation number for the objset */
4384 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4385 zlfid
->zf_setgen
[i
] = 0;
4393 zfs_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
4394 caller_context_t
*ct
)
4406 case _PC_FILESIZEBITS
:
4410 case _PC_XATTR_EXISTS
:
4412 zfsvfs
= zp
->z_zfsvfs
;
4416 error
= zfs_dirent_lock(&dl
, zp
, "", &xzp
,
4417 ZXATTR
| ZEXISTS
| ZSHARED
, NULL
, NULL
);
4419 zfs_dirent_unlock(dl
);
4420 if (!zfs_dirempty(xzp
))
4423 } else if (error
== ENOENT
) {
4425 * If there aren't extended attributes, it's the
4426 * same as having zero of them.
4433 case _PC_SATTR_ENABLED
:
4434 case _PC_SATTR_EXISTS
:
4435 *valp
= vfs_has_feature(vp
->v_vfsp
, VFSFT_SYSATTR_VIEWS
) &&
4436 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
);
4439 case _PC_ACL_ENABLED
:
4440 *valp
= _ACL_ACE_ENABLED
;
4443 case _PC_MIN_HOLE_SIZE
:
4444 *valp
= (ulong_t
)SPA_MINBLOCKSIZE
;
4448 return (fs_pathconf(vp
, cmd
, valp
, cr
, ct
));
4454 zfs_getsecattr(vnode_t
*vp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
,
4455 caller_context_t
*ct
)
4457 znode_t
*zp
= VTOZ(vp
);
4458 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4460 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4464 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
4472 zfs_setsecattr(vnode_t
*vp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
,
4473 caller_context_t
*ct
)
4475 znode_t
*zp
= VTOZ(vp
);
4476 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4478 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4482 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
4488 * Predeclare these here so that the compiler assumes that
4489 * this is an "old style" function declaration that does
4490 * not include arguments => we won't get type mismatch errors
4491 * in the initializations that follow.
4493 static int zfs_inval();
4494 static int zfs_isdir();
4508 * Directory vnode operations template
4510 vnodeops_t
*zfs_dvnodeops
;
4511 const fs_operation_def_t zfs_dvnodeops_template
[] = {
4512 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
4513 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
4514 VOPNAME_READ
, { .error
= zfs_isdir
},
4515 VOPNAME_WRITE
, { .error
= zfs_isdir
},
4516 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
4517 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
4518 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
4519 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
4520 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
4521 VOPNAME_CREATE
, { .vop_create
= zfs_create
},
4522 VOPNAME_REMOVE
, { .vop_remove
= zfs_remove
},
4523 VOPNAME_LINK
, { .vop_link
= zfs_link
},
4524 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
4525 VOPNAME_MKDIR
, { .vop_mkdir
= zfs_mkdir
},
4526 VOPNAME_RMDIR
, { .vop_rmdir
= zfs_rmdir
},
4527 VOPNAME_READDIR
, { .vop_readdir
= zfs_readdir
},
4528 VOPNAME_SYMLINK
, { .vop_symlink
= zfs_symlink
},
4529 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
4530 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
4531 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
4532 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
4533 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
4534 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
4535 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
4536 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
4541 * Regular file vnode operations template
4543 vnodeops_t
*zfs_fvnodeops
;
4544 const fs_operation_def_t zfs_fvnodeops_template
[] = {
4545 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
4546 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
4547 VOPNAME_READ
, { .vop_read
= zfs_read
},
4548 VOPNAME_WRITE
, { .vop_write
= zfs_write
},
4549 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
4550 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
4551 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
4552 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
4553 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
4554 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
4555 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
4556 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
4557 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
4558 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
4559 VOPNAME_FRLOCK
, { .vop_frlock
= zfs_frlock
},
4560 VOPNAME_SPACE
, { .vop_space
= zfs_space
},
4561 VOPNAME_GETPAGE
, { .vop_getpage
= zfs_getpage
},
4562 VOPNAME_PUTPAGE
, { .vop_putpage
= zfs_putpage
},
4563 VOPNAME_MAP
, { .vop_map
= zfs_map
},
4564 VOPNAME_ADDMAP
, { .vop_addmap
= zfs_addmap
},
4565 VOPNAME_DELMAP
, { .vop_delmap
= zfs_delmap
},
4566 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
4567 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
4568 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
4569 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
4574 * Symbolic link vnode operations template
4576 vnodeops_t
*zfs_symvnodeops
;
4577 const fs_operation_def_t zfs_symvnodeops_template
[] = {
4578 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
4579 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
4580 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
4581 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
4582 VOPNAME_READLINK
, { .vop_readlink
= zfs_readlink
},
4583 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
4584 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
4585 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
4586 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
4591 * Extended attribute directory vnode operations template
4592 * This template is identical to the directory vnodes
4593 * operation template except for restricted operations:
4596 * Note that there are other restrictions embedded in:
4597 * zfs_create() - restrict type to VREG
4598 * zfs_link() - no links into/out of attribute space
4599 * zfs_rename() - no moves into/out of attribute space
4601 vnodeops_t
*zfs_xdvnodeops
;
4602 const fs_operation_def_t zfs_xdvnodeops_template
[] = {
4603 VOPNAME_OPEN
, { .vop_open
= zfs_open
},
4604 VOPNAME_CLOSE
, { .vop_close
= zfs_close
},
4605 VOPNAME_IOCTL
, { .vop_ioctl
= zfs_ioctl
},
4606 VOPNAME_GETATTR
, { .vop_getattr
= zfs_getattr
},
4607 VOPNAME_SETATTR
, { .vop_setattr
= zfs_setattr
},
4608 VOPNAME_ACCESS
, { .vop_access
= zfs_access
},
4609 VOPNAME_LOOKUP
, { .vop_lookup
= zfs_lookup
},
4610 VOPNAME_CREATE
, { .vop_create
= zfs_create
},
4611 VOPNAME_REMOVE
, { .vop_remove
= zfs_remove
},
4612 VOPNAME_LINK
, { .vop_link
= zfs_link
},
4613 VOPNAME_RENAME
, { .vop_rename
= zfs_rename
},
4614 VOPNAME_MKDIR
, { .error
= zfs_inval
},
4615 VOPNAME_RMDIR
, { .vop_rmdir
= zfs_rmdir
},
4616 VOPNAME_READDIR
, { .vop_readdir
= zfs_readdir
},
4617 VOPNAME_SYMLINK
, { .error
= zfs_inval
},
4618 VOPNAME_FSYNC
, { .vop_fsync
= zfs_fsync
},
4619 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
4620 VOPNAME_FID
, { .vop_fid
= zfs_fid
},
4621 VOPNAME_SEEK
, { .vop_seek
= zfs_seek
},
4622 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},
4623 VOPNAME_GETSECATTR
, { .vop_getsecattr
= zfs_getsecattr
},
4624 VOPNAME_SETSECATTR
, { .vop_setsecattr
= zfs_setsecattr
},
4625 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
4630 * Error vnode operations template
4632 vnodeops_t
*zfs_evnodeops
;
4633 const fs_operation_def_t zfs_evnodeops_template
[] = {
4634 VOPNAME_INACTIVE
, { .vop_inactive
= zfs_inactive
},
4635 VOPNAME_PATHCONF
, { .vop_pathconf
= zfs_pathconf
},