4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
28 /* Portions Copyright 2007 Jeremy Teo */
29 /* Portions Copyright 2010 Robert Milkowski */
32 #include <sys/types.h>
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sysmacros.h>
37 #include <sys/resource.h>
39 #include <sys/vfs_opreg.h>
43 #include <sys/taskq.h>
45 #include <sys/vmsystm.h>
46 #include <sys/atomic.h>
48 #include <sys/pathname.h>
49 #include <sys/cmn_err.h>
50 #include <sys/errno.h>
51 #include <sys/unistd.h>
52 #include <sys/zfs_dir.h>
53 #include <sys/zfs_acl.h>
54 #include <sys/zfs_ioctl.h>
55 #include <sys/fs/zfs.h>
57 #include <sys/dmu_objset.h>
63 #include <sys/dirent.h>
64 #include <sys/policy.h>
65 #include <sys/sunddi.h>
68 #include "fs/fs_subr.h"
69 #include <sys/zfs_ctldir.h>
70 #include <sys/zfs_fuid.h>
71 #include <sys/zfs_sa.h>
72 #include <sys/zfs_vnops.h>
74 #include <sys/zfs_rlock.h>
75 #include <sys/extdirent.h>
76 #include <sys/kidmap.h>
84 * Each vnode op performs some logical unit of work. To do this, the ZPL must
85 * properly lock its in-core state, create a DMU transaction, do the work,
86 * record this work in the intent log (ZIL), commit the DMU transaction,
87 * and wait for the intent log to commit if it is a synchronous operation.
88 * Moreover, the vnode ops must work in both normal and log replay context.
89 * The ordering of events is important to avoid deadlocks and references
90 * to freed memory. The example below illustrates the following Big Rules:
92 * (1) A check must be made in each zfs thread for a mounted file system.
93 * This is done avoiding races using ZFS_ENTER(zsb).
94 * A ZFS_EXIT(zsb) is needed before all returns. Any znodes
95 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
96 * can return EIO from the calling function.
98 * (2) iput() should always be the last thing except for zil_commit()
99 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
100 * First, if it's the last reference, the vnode/znode
101 * can be freed, so the zp may point to freed memory. Second, the last
102 * reference will call zfs_zinactive(), which may induce a lot of work --
103 * pushing cached pages (which acquires range locks) and syncing out
104 * cached atime changes. Third, zfs_zinactive() may require a new tx,
105 * which could deadlock the system if you were already holding one.
106 * If you must call iput() within a tx then use zfs_iput_async().
108 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
109 * as they can span dmu_tx_assign() calls.
111 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
112 * dmu_tx_assign(). This is critical because we don't want to block
113 * while holding locks.
115 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
116 * reduces lock contention and CPU usage when we must wait (note that if
117 * throughput is constrained by the storage, nearly every transaction
120 * Note, in particular, that if a lock is sometimes acquired before
121 * the tx assigns, and sometimes after (e.g. z_lock), then failing
122 * to use a non-blocking assign can deadlock the system. The scenario:
124 * Thread A has grabbed a lock before calling dmu_tx_assign().
125 * Thread B is in an already-assigned tx, and blocks for this lock.
126 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
127 * forever, because the previous txg can't quiesce until B's tx commits.
129 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
130 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
131 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
132 * to indicate that this operation has already called dmu_tx_wait().
133 * This will ensure that we don't retry forever, waiting a short bit
136 * (5) If the operation succeeded, generate the intent log entry for it
137 * before dropping locks. This ensures that the ordering of events
138 * in the intent log matches the order in which they actually occurred.
139 * During ZIL replay the zfs_log_* functions will update the sequence
140 * number to indicate the zil transaction has replayed.
142 * (6) At the end of each vnode op, the DMU tx must always commit,
143 * regardless of whether there were any errors.
145 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
146 * to ensure that synchronous semantics are provided when necessary.
148 * In general, this is how things should be ordered in each vnode op:
150 * ZFS_ENTER(zsb); // exit if unmounted
152 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
153 * rw_enter(...); // grab any other locks you need
154 * tx = dmu_tx_create(...); // get DMU tx
155 * dmu_tx_hold_*(); // hold each object you might modify
156 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
158 * rw_exit(...); // drop locks
159 * zfs_dirent_unlock(dl); // unlock directory entry
160 * iput(...); // release held vnodes
161 * if (error == ERESTART) {
167 * dmu_tx_abort(tx); // abort DMU tx
168 * ZFS_EXIT(zsb); // finished in zfs
169 * return (error); // really out of space
171 * error = do_real_work(); // do whatever this VOP does
173 * zfs_log_*(...); // on success, make ZIL entry
174 * dmu_tx_commit(tx); // commit DMU tx -- error or not
175 * rw_exit(...); // drop locks
176 * zfs_dirent_unlock(dl); // unlock directory entry
177 * iput(...); // release held vnodes
178 * zil_commit(zilog, foid); // synchronous when necessary
179 * ZFS_EXIT(zsb); // finished in zfs
180 * return (error); // done, report error
184 * Virus scanning is unsupported. It would be possible to add a hook
185 * here to performance the required virus scan. This could be done
186 * entirely in the kernel or potentially as an update to invoke a
190 zfs_vscan(struct inode
*ip
, cred_t
*cr
, int async
)
197 zfs_open(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
199 znode_t
*zp
= ITOZ(ip
);
200 zfs_sb_t
*zsb
= ITOZSB(ip
);
205 /* Honor ZFS_APPENDONLY file attribute */
206 if ((mode
& FMODE_WRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
207 ((flag
& O_APPEND
) == 0)) {
209 return (SET_ERROR(EPERM
));
212 /* Virus scan eligible files on open */
213 if (!zfs_has_ctldir(zp
) && zsb
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
214 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0) {
215 if (zfs_vscan(ip
, cr
, 0) != 0) {
217 return (SET_ERROR(EACCES
));
221 /* Keep a count of the synchronous opens in the znode */
223 atomic_inc_32(&zp
->z_sync_cnt
);
228 EXPORT_SYMBOL(zfs_open
);
232 zfs_close(struct inode
*ip
, int flag
, cred_t
*cr
)
234 znode_t
*zp
= ITOZ(ip
);
235 zfs_sb_t
*zsb
= ITOZSB(ip
);
240 /* Decrement the synchronous opens in the znode */
242 atomic_dec_32(&zp
->z_sync_cnt
);
244 if (!zfs_has_ctldir(zp
) && zsb
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
245 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0)
246 VERIFY(zfs_vscan(ip
, cr
, 1) == 0);
251 EXPORT_SYMBOL(zfs_close
);
253 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
255 * Lseek support for finding holes (cmd == SEEK_HOLE) and
256 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
259 zfs_holey_common(struct inode
*ip
, int cmd
, loff_t
*off
)
261 znode_t
*zp
= ITOZ(ip
);
262 uint64_t noff
= (uint64_t)*off
; /* new offset */
267 file_sz
= zp
->z_size
;
268 if (noff
>= file_sz
) {
269 return (SET_ERROR(ENXIO
));
272 if (cmd
== SEEK_HOLE
)
277 error
= dmu_offset_next(ZTOZSB(zp
)->z_os
, zp
->z_id
, hole
, &noff
);
280 return (SET_ERROR(ENXIO
));
283 * We could find a hole that begins after the logical end-of-file,
284 * because dmu_offset_next() only works on whole blocks. If the
285 * EOF falls mid-block, then indicate that the "virtual hole"
286 * at the end of the file begins at the logical EOF, rather than
287 * at the end of the last block.
289 if (noff
> file_sz
) {
301 zfs_holey(struct inode
*ip
, int cmd
, loff_t
*off
)
303 znode_t
*zp
= ITOZ(ip
);
304 zfs_sb_t
*zsb
= ITOZSB(ip
);
310 error
= zfs_holey_common(ip
, cmd
, off
);
315 EXPORT_SYMBOL(zfs_holey
);
316 #endif /* SEEK_HOLE && SEEK_DATA */
320 * When a file is memory mapped, we must keep the IO data synchronized
321 * between the DMU cache and the memory mapped pages. What this means:
323 * On Write: If we find a memory mapped page, we write to *both*
324 * the page and the dmu buffer.
327 update_pages(struct inode
*ip
, int64_t start
, int len
,
328 objset_t
*os
, uint64_t oid
)
330 struct address_space
*mp
= ip
->i_mapping
;
336 off
= start
& (PAGE_SIZE
-1);
337 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
338 nbytes
= MIN(PAGE_SIZE
- off
, len
);
340 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
342 if (mapping_writably_mapped(mp
))
343 flush_dcache_page(pp
);
346 (void) dmu_read(os
, oid
, start
+off
, nbytes
, pb
+off
,
350 if (mapping_writably_mapped(mp
))
351 flush_dcache_page(pp
);
353 mark_page_accessed(pp
);
366 * When a file is memory mapped, we must keep the IO data synchronized
367 * between the DMU cache and the memory mapped pages. What this means:
369 * On Read: We "read" preferentially from memory mapped pages,
370 * else we default from the dmu buffer.
372 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
373 * the file is memory mapped.
376 mappedread(struct inode
*ip
, int nbytes
, uio_t
*uio
)
378 struct address_space
*mp
= ip
->i_mapping
;
380 znode_t
*zp
= ITOZ(ip
);
387 start
= uio
->uio_loffset
;
388 off
= start
& (PAGE_SIZE
-1);
389 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
390 bytes
= MIN(PAGE_SIZE
- off
, len
);
392 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
394 ASSERT(PageUptodate(pp
));
397 error
= uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
400 if (mapping_writably_mapped(mp
))
401 flush_dcache_page(pp
);
403 mark_page_accessed(pp
);
407 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
420 unsigned long zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
421 unsigned long zfs_delete_blocks
= DMU_MAX_DELETEBLKCNT
;
424 * Read bytes from specified file into supplied buffer.
426 * IN: ip - inode of file to be read from.
427 * uio - structure supplying read location, range info,
429 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
430 * O_DIRECT flag; used to bypass page cache.
431 * cr - credentials of caller.
433 * OUT: uio - updated offset and range, buffer filled.
435 * RETURN: 0 on success, error code on failure.
438 * inode - atime updated if byte count > 0
442 zfs_read(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
444 znode_t
*zp
= ITOZ(ip
);
445 zfs_sb_t
*zsb
= ITOZSB(ip
);
449 #ifdef HAVE_UIO_ZEROCOPY
451 #endif /* HAVE_UIO_ZEROCOPY */
456 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
458 return (SET_ERROR(EACCES
));
462 * Validate file offset
464 if (uio
->uio_loffset
< (offset_t
)0) {
466 return (SET_ERROR(EINVAL
));
470 * Fasttrack empty reads
472 if (uio
->uio_resid
== 0) {
478 * If we're in FRSYNC mode, sync out this znode before reading it.
480 if (ioflag
& FRSYNC
|| zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
481 zil_commit(zsb
->z_log
, zp
->z_id
);
484 * Lock the range against changes.
486 rl
= zfs_range_lock(zp
, uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
489 * If we are reading past end-of-file we can skip
490 * to the end; but we might still need to set atime.
492 if (uio
->uio_loffset
>= zp
->z_size
) {
497 ASSERT(uio
->uio_loffset
< zp
->z_size
);
498 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
500 #ifdef HAVE_UIO_ZEROCOPY
501 if ((uio
->uio_extflg
== UIO_XUIO
) &&
502 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
504 int blksz
= zp
->z_blksz
;
505 uint64_t offset
= uio
->uio_loffset
;
507 xuio
= (xuio_t
*)uio
;
509 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
512 ASSERT(offset
+ n
<= blksz
);
515 (void) dmu_xuio_init(xuio
, nblk
);
517 if (vn_has_cached_data(ip
)) {
519 * For simplicity, we always allocate a full buffer
520 * even if we only expect to read a portion of a block.
522 while (--nblk
>= 0) {
523 (void) dmu_xuio_add(xuio
,
524 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
529 #endif /* HAVE_UIO_ZEROCOPY */
532 nbytes
= MIN(n
, zfs_read_chunk_size
-
533 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
535 if (zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
)) {
536 error
= mappedread(ip
, nbytes
, uio
);
538 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
543 /* convert checksum errors into IO errors */
545 error
= SET_ERROR(EIO
);
552 zfs_range_unlock(rl
);
557 EXPORT_SYMBOL(zfs_read
);
560 * Write the bytes to a file.
562 * IN: ip - inode of file to be written to.
563 * uio - structure supplying write location, range info,
565 * ioflag - FAPPEND flag set if in append mode.
566 * O_DIRECT flag; used to bypass page cache.
567 * cr - credentials of caller.
569 * OUT: uio - updated offset and range.
571 * RETURN: 0 if success
572 * error code if failure
575 * ip - ctime|mtime updated if byte count > 0
580 zfs_write(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
582 znode_t
*zp
= ITOZ(ip
);
583 rlim64_t limit
= uio
->uio_limit
;
584 ssize_t start_resid
= uio
->uio_resid
;
588 zfs_sb_t
*zsb
= ZTOZSB(zp
);
593 int max_blksz
= zsb
->z_max_blksz
;
596 const iovec_t
*aiov
= NULL
;
599 const iovec_t
*iovp
= uio
->uio_iov
;
602 sa_bulk_attr_t bulk
[4];
603 uint64_t mtime
[2], ctime
[2];
604 ASSERTV(int iovcnt
= uio
->uio_iovcnt
);
607 * Fasttrack empty write
613 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
619 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
620 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
621 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
, &zp
->z_size
, 8);
622 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
626 * Callers might not be able to detect properly that we are read-only,
627 * so check it explicitly here.
629 if (zfs_is_readonly(zsb
)) {
631 return (SET_ERROR(EROFS
));
635 * If immutable or not appending then return EPERM
637 if ((zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
638 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
639 (uio
->uio_loffset
< zp
->z_size
))) {
641 return (SET_ERROR(EPERM
));
647 * Validate file offset
649 woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
652 return (SET_ERROR(EINVAL
));
656 * Pre-fault the pages to ensure slow (eg NFS) pages
658 * Skip this if uio contains loaned arc_buf.
660 #ifdef HAVE_UIO_ZEROCOPY
661 if ((uio
->uio_extflg
== UIO_XUIO
) &&
662 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
663 xuio
= (xuio_t
*)uio
;
666 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
669 * If in append mode, set the io offset pointer to eof.
671 if (ioflag
& FAPPEND
) {
673 * Obtain an appending range lock to guarantee file append
674 * semantics. We reset the write offset once we have the lock.
676 rl
= zfs_range_lock(zp
, 0, n
, RL_APPEND
);
678 if (rl
->r_len
== UINT64_MAX
) {
680 * We overlocked the file because this write will cause
681 * the file block size to increase.
682 * Note that zp_size cannot change with this lock held.
686 uio
->uio_loffset
= woff
;
689 * Note that if the file block size will change as a result of
690 * this write, then this range lock will lock the entire file
691 * so that we can re-write the block safely.
693 rl
= zfs_range_lock(zp
, woff
, n
, RL_WRITER
);
697 zfs_range_unlock(rl
);
699 return (SET_ERROR(EFBIG
));
702 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
705 /* Will this write extend the file length? */
706 write_eof
= (woff
+ n
> zp
->z_size
);
708 end_size
= MAX(zp
->z_size
, woff
+ n
);
711 * Write the file in reasonable size chunks. Each chunk is written
712 * in a separate transaction; this keeps the intent log records small
713 * and allows us to do more fine-grained space accounting.
717 woff
= uio
->uio_loffset
;
718 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
719 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
721 dmu_return_arcbuf(abuf
);
722 error
= SET_ERROR(EDQUOT
);
726 if (xuio
&& abuf
== NULL
) {
727 ASSERT(i_iov
< iovcnt
);
728 ASSERT3U(uio
->uio_segflg
, !=, UIO_BVEC
);
730 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
731 dmu_xuio_clear(xuio
, i_iov
);
732 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
733 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
734 aiov
->iov_len
== arc_buf_size(abuf
)));
736 } else if (abuf
== NULL
&& n
>= max_blksz
&&
737 woff
>= zp
->z_size
&&
738 P2PHASE(woff
, max_blksz
) == 0 &&
739 zp
->z_blksz
== max_blksz
) {
741 * This write covers a full block. "Borrow" a buffer
742 * from the dmu so that we can fill it before we enter
743 * a transaction. This avoids the possibility of
744 * holding up the transaction if the data copy hangs
745 * up on a pagefault (e.g., from an NFS server mapping).
749 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
751 ASSERT(abuf
!= NULL
);
752 ASSERT(arc_buf_size(abuf
) == max_blksz
);
753 if ((error
= uiocopy(abuf
->b_data
, max_blksz
,
754 UIO_WRITE
, uio
, &cbytes
))) {
755 dmu_return_arcbuf(abuf
);
758 ASSERT(cbytes
== max_blksz
);
762 * Start a transaction.
764 tx
= dmu_tx_create(zsb
->z_os
);
765 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
766 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
767 zfs_sa_upgrade_txholds(tx
, zp
);
768 error
= dmu_tx_assign(tx
, TXG_WAIT
);
772 dmu_return_arcbuf(abuf
);
777 * If zfs_range_lock() over-locked we grow the blocksize
778 * and then reduce the lock range. This will only happen
779 * on the first iteration since zfs_range_reduce() will
780 * shrink down r_len to the appropriate size.
782 if (rl
->r_len
== UINT64_MAX
) {
785 if (zp
->z_blksz
> max_blksz
) {
787 * File's blocksize is already larger than the
788 * "recordsize" property. Only let it grow to
789 * the next power of 2.
791 ASSERT(!ISP2(zp
->z_blksz
));
792 new_blksz
= MIN(end_size
,
793 1 << highbit64(zp
->z_blksz
));
795 new_blksz
= MIN(end_size
, max_blksz
);
797 zfs_grow_blocksize(zp
, new_blksz
, tx
);
798 zfs_range_reduce(rl
, woff
, n
);
802 * XXX - should we really limit each write to z_max_blksz?
803 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
805 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
808 tx_bytes
= uio
->uio_resid
;
809 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
811 tx_bytes
-= uio
->uio_resid
;
814 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
816 * If this is not a full block write, but we are
817 * extending the file past EOF and this data starts
818 * block-aligned, use assign_arcbuf(). Otherwise,
819 * write via dmu_write().
821 if (tx_bytes
< max_blksz
&& (!write_eof
||
822 aiov
->iov_base
!= abuf
->b_data
)) {
824 dmu_write(zsb
->z_os
, zp
->z_id
, woff
,
825 aiov
->iov_len
, aiov
->iov_base
, tx
);
826 dmu_return_arcbuf(abuf
);
827 xuio_stat_wbuf_copied();
829 ASSERT(xuio
|| tx_bytes
== max_blksz
);
830 dmu_assign_arcbuf(sa_get_db(zp
->z_sa_hdl
),
833 ASSERT(tx_bytes
<= uio
->uio_resid
);
834 uioskip(uio
, tx_bytes
);
837 if (tx_bytes
&& zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
838 update_pages(ip
, woff
, tx_bytes
, zsb
->z_os
, zp
->z_id
);
841 * If we made no progress, we're done. If we made even
842 * partial progress, update the znode and ZIL accordingly.
845 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
846 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
853 * Clear Set-UID/Set-GID bits on successful write if not
854 * privileged and at least one of the excute bits is set.
856 * It would be nice to to this after all writes have
857 * been done, but that would still expose the ISUID/ISGID
858 * to another app after the partial write is committed.
860 * Note: we don't call zfs_fuid_map_id() here because
861 * user 0 is not an ephemeral uid.
863 mutex_enter(&zp
->z_acl_lock
);
864 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
865 (S_IXUSR
>> 6))) != 0 &&
866 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
867 secpolicy_vnode_setid_retain(cr
,
868 (zp
->z_mode
& S_ISUID
) != 0 && zp
->z_uid
== 0) != 0) {
870 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
871 newmode
= zp
->z_mode
;
872 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
),
873 (void *)&newmode
, sizeof (uint64_t), tx
);
875 mutex_exit(&zp
->z_acl_lock
);
877 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
880 * Update the file size (zp_size) if it has changed;
881 * account for possible concurrent updates.
883 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
884 (void) atomic_cas_64(&zp
->z_size
, end_size
,
889 * If we are replaying and eof is non zero then force
890 * the file size to the specified eof. Note, there's no
891 * concurrency during replay.
893 if (zsb
->z_replay
&& zsb
->z_replay_eof
!= 0)
894 zp
->z_size
= zsb
->z_replay_eof
;
896 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
898 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
,
904 ASSERT(tx_bytes
== nbytes
);
908 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
911 zfs_inode_update(zp
);
912 zfs_range_unlock(rl
);
915 * If we're in replay mode, or we made no progress, return error.
916 * Otherwise, it's at least a partial write, so it's successful.
918 if (zsb
->z_replay
|| uio
->uio_resid
== start_resid
) {
923 if (ioflag
& (FSYNC
| FDSYNC
) ||
924 zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
925 zil_commit(zilog
, zp
->z_id
);
930 EXPORT_SYMBOL(zfs_write
);
933 zfs_iput_async(struct inode
*ip
)
935 objset_t
*os
= ITOZSB(ip
)->z_os
;
937 ASSERT(atomic_read(&ip
->i_count
) > 0);
940 if (atomic_read(&ip
->i_count
) == 1)
941 taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os
)),
942 (task_func_t
*)iput
, ip
, TQ_SLEEP
);
948 zfs_get_done(zgd_t
*zgd
, int error
)
950 znode_t
*zp
= zgd
->zgd_private
;
953 dmu_buf_rele(zgd
->zgd_db
, zgd
);
955 zfs_range_unlock(zgd
->zgd_rl
);
958 * Release the vnode asynchronously as we currently have the
959 * txg stopped from syncing.
961 zfs_iput_async(ZTOI(zp
));
963 if (error
== 0 && zgd
->zgd_bp
)
964 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
966 kmem_free(zgd
, sizeof (zgd_t
));
970 static int zil_fault_io
= 0;
974 * Get data to generate a TX_WRITE intent log record.
977 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
980 objset_t
*os
= zsb
->z_os
;
982 uint64_t object
= lr
->lr_foid
;
983 uint64_t offset
= lr
->lr_offset
;
984 uint64_t size
= lr
->lr_length
;
985 blkptr_t
*bp
= &lr
->lr_blkptr
;
994 * Nothing to do if the file has been removed
996 if (zfs_zget(zsb
, object
, &zp
) != 0)
997 return (SET_ERROR(ENOENT
));
998 if (zp
->z_unlinked
) {
1000 * Release the vnode asynchronously as we currently have the
1001 * txg stopped from syncing.
1003 zfs_iput_async(ZTOI(zp
));
1004 return (SET_ERROR(ENOENT
));
1007 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
1008 zgd
->zgd_zilog
= zsb
->z_log
;
1009 zgd
->zgd_private
= zp
;
1012 * Write records come in two flavors: immediate and indirect.
1013 * For small writes it's cheaper to store the data with the
1014 * log record (immediate); for large writes it's cheaper to
1015 * sync the data and get a pointer to it (indirect) so that
1016 * we don't have to write the data twice.
1018 if (buf
!= NULL
) { /* immediate write */
1019 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
, RL_READER
);
1020 /* test for truncation needs to be done while range locked */
1021 if (offset
>= zp
->z_size
) {
1022 error
= SET_ERROR(ENOENT
);
1024 error
= dmu_read(os
, object
, offset
, size
, buf
,
1025 DMU_READ_NO_PREFETCH
);
1027 ASSERT(error
== 0 || error
== ENOENT
);
1028 } else { /* indirect write */
1030 * Have to lock the whole block to ensure when it's
1031 * written out and it's checksum is being calculated
1032 * that no one can change the data. We need to re-check
1033 * blocksize after we get the lock in case it's changed!
1038 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
1040 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
,
1042 if (zp
->z_blksz
== size
)
1045 zfs_range_unlock(zgd
->zgd_rl
);
1047 /* test for truncation needs to be done while range locked */
1048 if (lr
->lr_offset
>= zp
->z_size
)
1049 error
= SET_ERROR(ENOENT
);
1052 error
= SET_ERROR(EIO
);
1057 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1058 DMU_READ_NO_PREFETCH
);
1061 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
1063 ASSERT(BP_IS_HOLE(bp
));
1070 ASSERT(db
->db_offset
== offset
);
1071 ASSERT(db
->db_size
== size
);
1073 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1075 ASSERT(error
|| lr
->lr_length
<= zp
->z_blksz
);
1078 * On success, we need to wait for the write I/O
1079 * initiated by dmu_sync() to complete before we can
1080 * release this dbuf. We will finish everything up
1081 * in the zfs_get_done() callback.
1086 if (error
== EALREADY
) {
1087 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
1093 zfs_get_done(zgd
, error
);
1100 zfs_access(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
1102 znode_t
*zp
= ITOZ(ip
);
1103 zfs_sb_t
*zsb
= ITOZSB(ip
);
1109 if (flag
& V_ACE_MASK
)
1110 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
1112 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
1117 EXPORT_SYMBOL(zfs_access
);
1120 * Lookup an entry in a directory, or an extended attribute directory.
1121 * If it exists, return a held inode reference for it.
1123 * IN: dip - inode of directory to search.
1124 * nm - name of entry to lookup.
1125 * flags - LOOKUP_XATTR set if looking for an attribute.
1126 * cr - credentials of caller.
1127 * direntflags - directory lookup flags
1128 * realpnp - returned pathname.
1130 * OUT: ipp - inode of located entry, NULL if not found.
1132 * RETURN: 0 on success, error code on failure.
1139 zfs_lookup(struct inode
*dip
, char *nm
, struct inode
**ipp
, int flags
,
1140 cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
1142 znode_t
*zdp
= ITOZ(dip
);
1143 zfs_sb_t
*zsb
= ITOZSB(dip
);
1147 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1149 if (!S_ISDIR(dip
->i_mode
)) {
1150 return (SET_ERROR(ENOTDIR
));
1151 } else if (zdp
->z_sa_hdl
== NULL
) {
1152 return (SET_ERROR(EIO
));
1155 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1156 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1165 vnode_t
*tvp
= dnlc_lookup(dvp
, nm
);
1168 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1173 if (tvp
== DNLC_NO_VNODE
) {
1175 return (SET_ERROR(ENOENT
));
1178 return (specvp_check(vpp
, cr
));
1181 #endif /* HAVE_DNLC */
1190 if (flags
& LOOKUP_XATTR
) {
1192 * We don't allow recursive attributes..
1193 * Maybe someday we will.
1195 if (zdp
->z_pflags
& ZFS_XATTR
) {
1197 return (SET_ERROR(EINVAL
));
1200 if ((error
= zfs_get_xattrdir(zdp
, ipp
, cr
, flags
))) {
1206 * Do we have permission to get into attribute directory?
1209 if ((error
= zfs_zaccess(ITOZ(*ipp
), ACE_EXECUTE
, 0,
1219 if (!S_ISDIR(dip
->i_mode
)) {
1221 return (SET_ERROR(ENOTDIR
));
1225 * Check accessibility of directory.
1228 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
))) {
1233 if (zsb
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1234 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1236 return (SET_ERROR(EILSEQ
));
1239 error
= zfs_dirlook(zdp
, nm
, ipp
, flags
, direntflags
, realpnp
);
1240 if ((error
== 0) && (*ipp
))
1241 zfs_inode_update(ITOZ(*ipp
));
1246 EXPORT_SYMBOL(zfs_lookup
);
1249 * Attempt to create a new entry in a directory. If the entry
1250 * already exists, truncate the file if permissible, else return
1251 * an error. Return the ip of the created or trunc'd file.
1253 * IN: dip - inode of directory to put new file entry in.
1254 * name - name of new file entry.
1255 * vap - attributes of new file.
1256 * excl - flag indicating exclusive or non-exclusive mode.
1257 * mode - mode to open file with.
1258 * cr - credentials of caller.
1259 * flag - large file flag [UNUSED].
1260 * vsecp - ACL to be set
1262 * OUT: ipp - inode of created or trunc'd entry.
1264 * RETURN: 0 on success, error code on failure.
1267 * dip - ctime|mtime updated if new entry created
1268 * ip - ctime|mtime always, atime if new
1273 zfs_create(struct inode
*dip
, char *name
, vattr_t
*vap
, int excl
,
1274 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
1276 znode_t
*zp
, *dzp
= ITOZ(dip
);
1277 zfs_sb_t
*zsb
= ITOZSB(dip
);
1285 zfs_acl_ids_t acl_ids
;
1286 boolean_t fuid_dirtied
;
1287 boolean_t have_acl
= B_FALSE
;
1288 boolean_t waited
= B_FALSE
;
1291 * If we have an ephemeral id, ACL, or XVATTR then
1292 * make sure file system is at proper version
1298 if (zsb
->z_use_fuids
== B_FALSE
&&
1299 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1300 return (SET_ERROR(EINVAL
));
1307 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
1308 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1310 return (SET_ERROR(EILSEQ
));
1313 if (vap
->va_mask
& ATTR_XVATTR
) {
1314 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1315 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1323 if (*name
== '\0') {
1325 * Null component name refers to the directory itself.
1332 /* possible igrab(zp) */
1335 if (flag
& FIGNORECASE
)
1338 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1342 zfs_acl_ids_free(&acl_ids
);
1343 if (strcmp(name
, "..") == 0)
1344 error
= SET_ERROR(EISDIR
);
1354 * Create a new file object and update the directory
1357 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
1359 zfs_acl_ids_free(&acl_ids
);
1364 * We only support the creation of regular files in
1365 * extended attribute directories.
1368 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
1370 zfs_acl_ids_free(&acl_ids
);
1371 error
= SET_ERROR(EINVAL
);
1375 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1376 cr
, vsecp
, &acl_ids
)) != 0)
1380 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1381 zfs_acl_ids_free(&acl_ids
);
1382 error
= SET_ERROR(EDQUOT
);
1386 tx
= dmu_tx_create(os
);
1388 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1389 ZFS_SA_BASE_ATTR_SIZE
);
1391 fuid_dirtied
= zsb
->z_fuid_dirty
;
1393 zfs_fuid_txhold(zsb
, tx
);
1394 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1395 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1396 if (!zsb
->z_use_sa
&&
1397 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1398 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1399 0, acl_ids
.z_aclp
->z_acl_bytes
);
1401 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1403 zfs_dirent_unlock(dl
);
1404 if (error
== ERESTART
) {
1410 zfs_acl_ids_free(&acl_ids
);
1415 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1418 zfs_fuid_sync(zsb
, tx
);
1420 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1421 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1422 if (flag
& FIGNORECASE
)
1424 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1425 vsecp
, acl_ids
.z_fuidp
, vap
);
1426 zfs_acl_ids_free(&acl_ids
);
1429 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1432 zfs_acl_ids_free(&acl_ids
);
1436 * A directory entry already exists for this name.
1439 * Can't truncate an existing file if in exclusive mode.
1442 error
= SET_ERROR(EEXIST
);
1446 * Can't open a directory for writing.
1448 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
1449 error
= SET_ERROR(EISDIR
);
1453 * Verify requested access to file.
1455 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1459 mutex_enter(&dzp
->z_lock
);
1461 mutex_exit(&dzp
->z_lock
);
1464 * Truncate regular files if requested.
1466 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
1467 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
1468 /* we can't hold any locks when calling zfs_freesp() */
1469 zfs_dirent_unlock(dl
);
1471 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1477 zfs_dirent_unlock(dl
);
1483 zfs_inode_update(dzp
);
1484 zfs_inode_update(zp
);
1488 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1489 zil_commit(zilog
, 0);
1494 EXPORT_SYMBOL(zfs_create
);
1497 * Remove an entry from a directory.
1499 * IN: dip - inode of directory to remove entry from.
1500 * name - name of entry to remove.
1501 * cr - credentials of caller.
1503 * RETURN: 0 if success
1504 * error code if failure
1508 * ip - ctime (if nlink > 0)
1511 uint64_t null_xattr
= 0;
1515 zfs_remove(struct inode
*dip
, char *name
, cred_t
*cr
, int flags
)
1517 znode_t
*zp
, *dzp
= ITOZ(dip
);
1520 zfs_sb_t
*zsb
= ITOZSB(dip
);
1522 uint64_t acl_obj
, xattr_obj
;
1523 uint64_t xattr_obj_unlinked
= 0;
1527 boolean_t may_delete_now
, delete_now
= FALSE
;
1528 boolean_t unlinked
, toobig
= FALSE
;
1530 pathname_t
*realnmp
= NULL
;
1534 boolean_t waited
= B_FALSE
;
1540 if (flags
& FIGNORECASE
) {
1550 * Attempt to lock directory; fail if entry doesn't exist.
1552 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1562 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1567 * Need to use rmdir for removing directories.
1569 if (S_ISDIR(ip
->i_mode
)) {
1570 error
= SET_ERROR(EPERM
);
1576 dnlc_remove(dvp
, realnmp
->pn_buf
);
1578 dnlc_remove(dvp
, name
);
1579 #endif /* HAVE_DNLC */
1581 mutex_enter(&zp
->z_lock
);
1582 may_delete_now
= atomic_read(&ip
->i_count
) == 1 && !(zp
->z_is_mapped
);
1583 mutex_exit(&zp
->z_lock
);
1586 * We may delete the znode now, or we may put it in the unlinked set;
1587 * it depends on whether we're the last link, and on whether there are
1588 * other holds on the inode. So we dmu_tx_hold() the right things to
1589 * allow for either case.
1592 tx
= dmu_tx_create(zsb
->z_os
);
1593 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1594 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1595 zfs_sa_upgrade_txholds(tx
, zp
);
1596 zfs_sa_upgrade_txholds(tx
, dzp
);
1597 if (may_delete_now
) {
1598 toobig
= zp
->z_size
> zp
->z_blksz
* zfs_delete_blocks
;
1599 /* if the file is too big, only hold_free a token amount */
1600 dmu_tx_hold_free(tx
, zp
->z_id
, 0,
1601 (toobig
? DMU_MAX_ACCESS
: DMU_OBJECT_END
));
1604 /* are there any extended attributes? */
1605 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1606 &xattr_obj
, sizeof (xattr_obj
));
1607 if (error
== 0 && xattr_obj
) {
1608 error
= zfs_zget(zsb
, xattr_obj
, &xzp
);
1610 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1611 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1614 mutex_enter(&zp
->z_lock
);
1615 if ((acl_obj
= zfs_external_acl(zp
)) != 0 && may_delete_now
)
1616 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
1617 mutex_exit(&zp
->z_lock
);
1619 /* charge as an update -- would be nice not to charge at all */
1620 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1623 * Mark this transaction as typically resulting in a net free of space
1625 dmu_tx_mark_netfree(tx
);
1627 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1629 zfs_dirent_unlock(dl
);
1633 if (error
== ERESTART
) {
1647 * Remove the directory entry.
1649 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1658 * Hold z_lock so that we can make sure that the ACL obj
1659 * hasn't changed. Could have been deleted due to
1662 mutex_enter(&zp
->z_lock
);
1663 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1664 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1665 delete_now
= may_delete_now
&& !toobig
&&
1666 atomic_read(&ip
->i_count
) == 1 && !(zp
->z_is_mapped
) &&
1667 xattr_obj
== xattr_obj_unlinked
&& zfs_external_acl(zp
) ==
1672 if (xattr_obj_unlinked
) {
1673 ASSERT3U(xzp
->z_links
, ==, 2);
1674 mutex_enter(&xzp
->z_lock
);
1675 xzp
->z_unlinked
= 1;
1677 error
= sa_update(xzp
->z_sa_hdl
, SA_ZPL_LINKS(zsb
),
1678 &xzp
->z_links
, sizeof (xzp
->z_links
), tx
);
1679 ASSERT3U(error
, ==, 0);
1680 mutex_exit(&xzp
->z_lock
);
1681 zfs_unlinked_add(xzp
, tx
);
1684 error
= sa_remove(zp
->z_sa_hdl
,
1685 SA_ZPL_XATTR(zsb
), tx
);
1687 error
= sa_update(zp
->z_sa_hdl
,
1688 SA_ZPL_XATTR(zsb
), &null_xattr
,
1689 sizeof (uint64_t), tx
);
1693 * Add to the unlinked set because a new reference could be
1694 * taken concurrently resulting in a deferred destruction.
1696 zfs_unlinked_add(zp
, tx
);
1697 mutex_exit(&zp
->z_lock
);
1698 zfs_inode_update(zp
);
1700 } else if (unlinked
) {
1701 mutex_exit(&zp
->z_lock
);
1702 zfs_unlinked_add(zp
, tx
);
1706 if (flags
& FIGNORECASE
)
1708 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
);
1715 zfs_dirent_unlock(dl
);
1716 zfs_inode_update(dzp
);
1719 zfs_inode_update(zp
);
1724 zfs_inode_update(xzp
);
1725 zfs_iput_async(ZTOI(xzp
));
1728 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1729 zil_commit(zilog
, 0);
1734 EXPORT_SYMBOL(zfs_remove
);
1737 * Create a new directory and insert it into dip using the name
1738 * provided. Return a pointer to the inserted directory.
1740 * IN: dip - inode of directory to add subdir to.
1741 * dirname - name of new directory.
1742 * vap - attributes of new directory.
1743 * cr - credentials of caller.
1744 * vsecp - ACL to be set
1746 * OUT: ipp - inode of created directory.
1748 * RETURN: 0 if success
1749 * error code if failure
1752 * dip - ctime|mtime updated
1753 * ipp - ctime|mtime|atime updated
1757 zfs_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
, struct inode
**ipp
,
1758 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1760 znode_t
*zp
, *dzp
= ITOZ(dip
);
1761 zfs_sb_t
*zsb
= ITOZSB(dip
);
1769 gid_t gid
= crgetgid(cr
);
1770 zfs_acl_ids_t acl_ids
;
1771 boolean_t fuid_dirtied
;
1772 boolean_t waited
= B_FALSE
;
1774 ASSERT(S_ISDIR(vap
->va_mode
));
1777 * If we have an ephemeral id, ACL, or XVATTR then
1778 * make sure file system is at proper version
1782 if (zsb
->z_use_fuids
== B_FALSE
&&
1783 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1784 return (SET_ERROR(EINVAL
));
1790 if (dzp
->z_pflags
& ZFS_XATTR
) {
1792 return (SET_ERROR(EINVAL
));
1795 if (zsb
->z_utf8
&& u8_validate(dirname
,
1796 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1798 return (SET_ERROR(EILSEQ
));
1800 if (flags
& FIGNORECASE
)
1803 if (vap
->va_mask
& ATTR_XVATTR
) {
1804 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1805 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1811 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1812 vsecp
, &acl_ids
)) != 0) {
1817 * First make sure the new directory doesn't exist.
1819 * Existence is checked first to make sure we don't return
1820 * EACCES instead of EEXIST which can cause some applications
1826 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1828 zfs_acl_ids_free(&acl_ids
);
1833 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
1834 zfs_acl_ids_free(&acl_ids
);
1835 zfs_dirent_unlock(dl
);
1840 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1841 zfs_acl_ids_free(&acl_ids
);
1842 zfs_dirent_unlock(dl
);
1844 return (SET_ERROR(EDQUOT
));
1848 * Add a new entry to the directory.
1850 tx
= dmu_tx_create(zsb
->z_os
);
1851 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1852 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1853 fuid_dirtied
= zsb
->z_fuid_dirty
;
1855 zfs_fuid_txhold(zsb
, tx
);
1856 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1857 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1858 acl_ids
.z_aclp
->z_acl_bytes
);
1861 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1862 ZFS_SA_BASE_ATTR_SIZE
);
1864 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1866 zfs_dirent_unlock(dl
);
1867 if (error
== ERESTART
) {
1873 zfs_acl_ids_free(&acl_ids
);
1882 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1885 zfs_fuid_sync(zsb
, tx
);
1888 * Now put new name in parent dir.
1890 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1894 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1895 if (flags
& FIGNORECASE
)
1897 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
1898 acl_ids
.z_fuidp
, vap
);
1900 zfs_acl_ids_free(&acl_ids
);
1904 zfs_dirent_unlock(dl
);
1906 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1907 zil_commit(zilog
, 0);
1909 zfs_inode_update(dzp
);
1910 zfs_inode_update(zp
);
1914 EXPORT_SYMBOL(zfs_mkdir
);
1917 * Remove a directory subdir entry. If the current working
1918 * directory is the same as the subdir to be removed, the
1921 * IN: dip - inode of directory to remove from.
1922 * name - name of directory to be removed.
1923 * cwd - inode of current working directory.
1924 * cr - credentials of caller.
1925 * flags - case flags
1927 * RETURN: 0 on success, error code on failure.
1930 * dip - ctime|mtime updated
1934 zfs_rmdir(struct inode
*dip
, char *name
, struct inode
*cwd
, cred_t
*cr
,
1937 znode_t
*dzp
= ITOZ(dip
);
1940 zfs_sb_t
*zsb
= ITOZSB(dip
);
1946 boolean_t waited
= B_FALSE
;
1952 if (flags
& FIGNORECASE
)
1958 * Attempt to lock directory; fail if entry doesn't exist.
1960 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1968 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1972 if (!S_ISDIR(ip
->i_mode
)) {
1973 error
= SET_ERROR(ENOTDIR
);
1978 error
= SET_ERROR(EINVAL
);
1983 * Grab a lock on the directory to make sure that noone is
1984 * trying to add (or lookup) entries while we are removing it.
1986 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1989 * Grab a lock on the parent pointer to make sure we play well
1990 * with the treewalk and directory rename code.
1992 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1994 tx
= dmu_tx_create(zsb
->z_os
);
1995 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1996 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1997 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1998 zfs_sa_upgrade_txholds(tx
, zp
);
1999 zfs_sa_upgrade_txholds(tx
, dzp
);
2000 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
2002 rw_exit(&zp
->z_parent_lock
);
2003 rw_exit(&zp
->z_name_lock
);
2004 zfs_dirent_unlock(dl
);
2006 if (error
== ERESTART
) {
2017 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
2020 uint64_t txtype
= TX_RMDIR
;
2021 if (flags
& FIGNORECASE
)
2023 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
);
2028 rw_exit(&zp
->z_parent_lock
);
2029 rw_exit(&zp
->z_name_lock
);
2031 zfs_dirent_unlock(dl
);
2033 zfs_inode_update(dzp
);
2034 zfs_inode_update(zp
);
2037 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2038 zil_commit(zilog
, 0);
2043 EXPORT_SYMBOL(zfs_rmdir
);
2046 * Read as many directory entries as will fit into the provided
2047 * dirent buffer from the given directory cursor position.
2049 * IN: ip - inode of directory to read.
2050 * dirent - buffer for directory entries.
2052 * OUT: dirent - filler buffer of directory entries.
2054 * RETURN: 0 if success
2055 * error code if failure
2058 * ip - atime updated
2060 * Note that the low 4 bits of the cookie returned by zap is always zero.
2061 * This allows us to use the low range for "special" directory entries:
2062 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2063 * we use the offset 2 for the '.zfs' directory.
2067 zfs_readdir(struct inode
*ip
, struct dir_context
*ctx
, cred_t
*cr
)
2069 znode_t
*zp
= ITOZ(ip
);
2070 zfs_sb_t
*zsb
= ITOZSB(ip
);
2073 zap_attribute_t zap
;
2079 uint64_t offset
; /* must be unsigned; checks for < 1 */
2084 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
2085 &parent
, sizeof (parent
))) != 0)
2089 * Quit if directory has been removed (posix)
2097 prefetch
= zp
->z_zn_prefetch
;
2100 * Initialize the iterator cursor.
2104 * Start iteration from the beginning of the directory.
2106 zap_cursor_init(&zc
, os
, zp
->z_id
);
2109 * The offset is a serialized cursor.
2111 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
2115 * Transform to file-system independent format
2120 * Special case `.', `..', and `.zfs'.
2123 (void) strcpy(zap
.za_name
, ".");
2124 zap
.za_normalization_conflict
= 0;
2127 } else if (offset
== 1) {
2128 (void) strcpy(zap
.za_name
, "..");
2129 zap
.za_normalization_conflict
= 0;
2132 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
2133 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
2134 zap
.za_normalization_conflict
= 0;
2135 objnum
= ZFSCTL_INO_ROOT
;
2141 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
2142 if (error
== ENOENT
)
2149 * Allow multiple entries provided the first entry is
2150 * the object id. Non-zpl consumers may safely make
2151 * use of the additional space.
2153 * XXX: This should be a feature flag for compatibility
2155 if (zap
.za_integer_length
!= 8 ||
2156 zap
.za_num_integers
== 0) {
2157 cmn_err(CE_WARN
, "zap_readdir: bad directory "
2158 "entry, obj = %lld, offset = %lld, "
2159 "length = %d, num = %lld\n",
2160 (u_longlong_t
)zp
->z_id
,
2161 (u_longlong_t
)offset
,
2162 zap
.za_integer_length
,
2163 (u_longlong_t
)zap
.za_num_integers
);
2164 error
= SET_ERROR(ENXIO
);
2168 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
2169 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
2172 done
= !dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
2177 /* Prefetch znode */
2179 dmu_prefetch(os
, objnum
, 0, 0, 0,
2180 ZIO_PRIORITY_SYNC_READ
);
2184 * Move to the next entry, fill in the previous offset.
2186 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2187 zap_cursor_advance(&zc
);
2188 offset
= zap_cursor_serialize(&zc
);
2194 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2197 zap_cursor_fini(&zc
);
2198 if (error
== ENOENT
)
2205 EXPORT_SYMBOL(zfs_readdir
);
2207 ulong_t zfs_fsync_sync_cnt
= 4;
2210 zfs_fsync(struct inode
*ip
, int syncflag
, cred_t
*cr
)
2212 znode_t
*zp
= ITOZ(ip
);
2213 zfs_sb_t
*zsb
= ITOZSB(ip
);
2215 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2217 if (zsb
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2220 zil_commit(zsb
->z_log
, zp
->z_id
);
2223 tsd_set(zfs_fsyncer_key
, NULL
);
2227 EXPORT_SYMBOL(zfs_fsync
);
2231 * Get the requested file attributes and place them in the provided
2234 * IN: ip - inode of file.
2235 * vap - va_mask identifies requested attributes.
2236 * If ATTR_XVATTR set, then optional attrs are requested
2237 * flags - ATTR_NOACLCHECK (CIFS server context)
2238 * cr - credentials of caller.
2240 * OUT: vap - attribute values.
2242 * RETURN: 0 (always succeeds)
2246 zfs_getattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2248 znode_t
*zp
= ITOZ(ip
);
2249 zfs_sb_t
*zsb
= ITOZSB(ip
);
2252 uint64_t atime
[2], mtime
[2], ctime
[2];
2253 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2254 xoptattr_t
*xoap
= NULL
;
2255 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2256 sa_bulk_attr_t bulk
[3];
2262 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2264 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
, &atime
, 16);
2265 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
2266 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
2268 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2274 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2275 * Also, if we are the owner don't bother, since owner should
2276 * always be allowed to read basic attributes of file.
2278 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2279 (vap
->va_uid
!= crgetuid(cr
))) {
2280 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2288 * Return all attributes. It's cheaper to provide the answer
2289 * than to determine whether we were asked the question.
2292 mutex_enter(&zp
->z_lock
);
2293 vap
->va_type
= vn_mode_to_vtype(zp
->z_mode
);
2294 vap
->va_mode
= zp
->z_mode
;
2295 vap
->va_fsid
= ZTOI(zp
)->i_sb
->s_dev
;
2296 vap
->va_nodeid
= zp
->z_id
;
2297 if ((zp
->z_id
== zsb
->z_root
) && zfs_show_ctldir(zp
))
2298 links
= zp
->z_links
+ 1;
2300 links
= zp
->z_links
;
2301 vap
->va_nlink
= MIN(links
, ZFS_LINK_MAX
);
2302 vap
->va_size
= i_size_read(ip
);
2303 vap
->va_rdev
= ip
->i_rdev
;
2304 vap
->va_seq
= ip
->i_generation
;
2307 * Add in any requested optional attributes and the create time.
2308 * Also set the corresponding bits in the returned attribute bitmap.
2310 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zsb
->z_use_fuids
) {
2311 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2313 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2314 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2317 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2318 xoap
->xoa_readonly
=
2319 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2320 XVA_SET_RTN(xvap
, XAT_READONLY
);
2323 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2325 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2326 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2329 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2331 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2332 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2335 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2336 xoap
->xoa_nounlink
=
2337 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2338 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2341 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2342 xoap
->xoa_immutable
=
2343 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2344 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2347 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2348 xoap
->xoa_appendonly
=
2349 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2350 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2353 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2355 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2356 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2359 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2361 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2362 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2365 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2366 xoap
->xoa_av_quarantined
=
2367 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2368 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2371 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2372 xoap
->xoa_av_modified
=
2373 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2374 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2377 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2378 S_ISREG(ip
->i_mode
)) {
2379 zfs_sa_get_scanstamp(zp
, xvap
);
2382 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2385 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zsb
),
2386 times
, sizeof (times
));
2387 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2388 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2391 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2392 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2393 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2395 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2396 xoap
->xoa_generation
= ip
->i_generation
;
2397 XVA_SET_RTN(xvap
, XAT_GEN
);
2400 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2402 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2403 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2406 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2408 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2409 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2413 ZFS_TIME_DECODE(&vap
->va_atime
, atime
);
2414 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2415 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2417 mutex_exit(&zp
->z_lock
);
2419 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2421 if (zp
->z_blksz
== 0) {
2423 * Block size hasn't been set; suggest maximal I/O transfers.
2425 vap
->va_blksize
= zsb
->z_max_blksz
;
2431 EXPORT_SYMBOL(zfs_getattr
);
2434 * Get the basic file attributes and place them in the provided kstat
2435 * structure. The inode is assumed to be the authoritative source
2436 * for most of the attributes. However, the znode currently has the
2437 * authoritative atime, blksize, and block count.
2439 * IN: ip - inode of file.
2441 * OUT: sp - kstat values.
2443 * RETURN: 0 (always succeeds)
2447 zfs_getattr_fast(struct inode
*ip
, struct kstat
*sp
)
2449 znode_t
*zp
= ITOZ(ip
);
2450 zfs_sb_t
*zsb
= ITOZSB(ip
);
2452 u_longlong_t nblocks
;
2457 mutex_enter(&zp
->z_lock
);
2459 generic_fillattr(ip
, sp
);
2461 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
2462 sp
->blksize
= blksize
;
2463 sp
->blocks
= nblocks
;
2465 if (unlikely(zp
->z_blksz
== 0)) {
2467 * Block size hasn't been set; suggest maximal I/O transfers.
2469 sp
->blksize
= zsb
->z_max_blksz
;
2472 mutex_exit(&zp
->z_lock
);
2475 * Required to prevent NFS client from detecting different inode
2476 * numbers of snapshot root dentry before and after snapshot mount.
2478 if (zsb
->z_issnap
) {
2479 if (ip
->i_sb
->s_root
->d_inode
== ip
)
2480 sp
->ino
= ZFSCTL_INO_SNAPDIRS
-
2481 dmu_objset_id(zsb
->z_os
);
2488 EXPORT_SYMBOL(zfs_getattr_fast
);
2491 * Set the file attributes to the values contained in the
2494 * IN: ip - inode of file to be modified.
2495 * vap - new attribute values.
2496 * If ATTR_XVATTR set, then optional attrs are being set
2497 * flags - ATTR_UTIME set if non-default time values provided.
2498 * - ATTR_NOACLCHECK (CIFS context only).
2499 * cr - credentials of caller.
2501 * RETURN: 0 if success
2502 * error code if failure
2505 * ip - ctime updated, mtime updated if size changed.
2509 zfs_setattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2511 znode_t
*zp
= ITOZ(ip
);
2512 zfs_sb_t
*zsb
= ITOZSB(ip
);
2516 xvattr_t
*tmpxvattr
;
2517 uint_t mask
= vap
->va_mask
;
2518 uint_t saved_mask
= 0;
2521 uint64_t new_uid
, new_gid
;
2523 uint64_t mtime
[2], ctime
[2], atime
[2];
2525 int need_policy
= FALSE
;
2527 zfs_fuid_info_t
*fuidp
= NULL
;
2528 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2531 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2532 boolean_t fuid_dirtied
= B_FALSE
;
2533 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
2534 int count
= 0, xattr_count
= 0;
2545 * Make sure that if we have ephemeral uid/gid or xvattr specified
2546 * that file system is at proper version level
2549 if (zsb
->z_use_fuids
== B_FALSE
&&
2550 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2551 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2552 (mask
& ATTR_XVATTR
))) {
2554 return (SET_ERROR(EINVAL
));
2557 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
2559 return (SET_ERROR(EISDIR
));
2562 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
2564 return (SET_ERROR(EINVAL
));
2568 * If this is an xvattr_t, then get a pointer to the structure of
2569 * optional attributes. If this is NULL, then we have a vattr_t.
2571 xoap
= xva_getxoptattr(xvap
);
2573 tmpxvattr
= kmem_alloc(sizeof (xvattr_t
), KM_SLEEP
);
2574 xva_init(tmpxvattr
);
2576 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * 7, KM_SLEEP
);
2577 xattr_bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * 7, KM_SLEEP
);
2580 * Immutable files can only alter immutable bit and atime
2582 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2583 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
2584 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2589 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2595 * Verify timestamps doesn't overflow 32 bits.
2596 * ZFS can handle large timestamps, but 32bit syscalls can't
2597 * handle times greater than 2039. This check should be removed
2598 * once large timestamps are fully supported.
2600 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
2601 if (((mask
& ATTR_ATIME
) &&
2602 TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2603 ((mask
& ATTR_MTIME
) &&
2604 TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2614 /* Can this be moved to before the top label? */
2615 if (zfs_is_readonly(zsb
)) {
2621 * First validate permissions
2624 if (mask
& ATTR_SIZE
) {
2625 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2630 * XXX - Note, we are not providing any open
2631 * mode flags here (like FNDELAY), so we may
2632 * block if there are locks present... this
2633 * should be addressed in openat().
2635 /* XXX - would it be OK to generate a log record here? */
2636 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2641 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
2642 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2643 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2644 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2645 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2646 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2647 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2648 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2649 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2653 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2654 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
2659 * NOTE: even if a new mode is being set,
2660 * we may clear S_ISUID/S_ISGID bits.
2663 if (!(mask
& ATTR_MODE
))
2664 vap
->va_mode
= zp
->z_mode
;
2667 * Take ownership or chgrp to group we are a member of
2670 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
2671 take_group
= (mask
& ATTR_GID
) &&
2672 zfs_groupmember(zsb
, vap
->va_gid
, cr
);
2675 * If both ATTR_UID and ATTR_GID are set then take_owner and
2676 * take_group must both be set in order to allow taking
2679 * Otherwise, send the check through secpolicy_vnode_setattr()
2683 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
2684 take_owner
&& take_group
) ||
2685 ((idmask
== ATTR_UID
) && take_owner
) ||
2686 ((idmask
== ATTR_GID
) && take_group
)) {
2687 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2688 skipaclchk
, cr
) == 0) {
2690 * Remove setuid/setgid for non-privileged users
2692 (void) secpolicy_setid_clear(vap
, cr
);
2693 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
2702 mutex_enter(&zp
->z_lock
);
2703 oldva
.va_mode
= zp
->z_mode
;
2704 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2705 if (mask
& ATTR_XVATTR
) {
2707 * Update xvattr mask to include only those attributes
2708 * that are actually changing.
2710 * the bits will be restored prior to actually setting
2711 * the attributes so the caller thinks they were set.
2713 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2714 if (xoap
->xoa_appendonly
!=
2715 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2718 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2719 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
2723 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2724 if (xoap
->xoa_nounlink
!=
2725 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2728 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2729 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
2733 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2734 if (xoap
->xoa_immutable
!=
2735 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2738 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2739 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
2743 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2744 if (xoap
->xoa_nodump
!=
2745 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2748 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2749 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
2753 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2754 if (xoap
->xoa_av_modified
!=
2755 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2758 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2759 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
2763 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2764 if ((!S_ISREG(ip
->i_mode
) &&
2765 xoap
->xoa_av_quarantined
) ||
2766 xoap
->xoa_av_quarantined
!=
2767 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2770 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2771 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
2775 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2776 mutex_exit(&zp
->z_lock
);
2781 if (need_policy
== FALSE
&&
2782 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2783 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2788 mutex_exit(&zp
->z_lock
);
2790 if (mask
& ATTR_MODE
) {
2791 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2792 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
2797 trim_mask
|= ATTR_MODE
;
2805 * If trim_mask is set then take ownership
2806 * has been granted or write_acl is present and user
2807 * has the ability to modify mode. In that case remove
2808 * UID|GID and or MODE from mask so that
2809 * secpolicy_vnode_setattr() doesn't revoke it.
2813 saved_mask
= vap
->va_mask
;
2814 vap
->va_mask
&= ~trim_mask
;
2816 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
2817 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2822 vap
->va_mask
|= saved_mask
;
2826 * secpolicy_vnode_setattr, or take ownership may have
2829 mask
= vap
->va_mask
;
2831 if ((mask
& (ATTR_UID
| ATTR_GID
))) {
2832 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
2833 &xattr_obj
, sizeof (xattr_obj
));
2835 if (err
== 0 && xattr_obj
) {
2836 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
2840 if (mask
& ATTR_UID
) {
2841 new_uid
= zfs_fuid_create(zsb
,
2842 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2843 if (new_uid
!= zp
->z_uid
&&
2844 zfs_fuid_overquota(zsb
, B_FALSE
, new_uid
)) {
2852 if (mask
& ATTR_GID
) {
2853 new_gid
= zfs_fuid_create(zsb
, (uint64_t)vap
->va_gid
,
2854 cr
, ZFS_GROUP
, &fuidp
);
2855 if (new_gid
!= zp
->z_gid
&&
2856 zfs_fuid_overquota(zsb
, B_TRUE
, new_gid
)) {
2864 tx
= dmu_tx_create(zsb
->z_os
);
2866 if (mask
& ATTR_MODE
) {
2867 uint64_t pmode
= zp
->z_mode
;
2869 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2871 zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
);
2873 mutex_enter(&zp
->z_lock
);
2874 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2876 * Are we upgrading ACL from old V0 format
2879 if (zsb
->z_version
>= ZPL_VERSION_FUID
&&
2880 zfs_znode_acl_version(zp
) ==
2881 ZFS_ACL_VERSION_INITIAL
) {
2882 dmu_tx_hold_free(tx
, acl_obj
, 0,
2884 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2885 0, aclp
->z_acl_bytes
);
2887 dmu_tx_hold_write(tx
, acl_obj
, 0,
2890 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2891 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2892 0, aclp
->z_acl_bytes
);
2894 mutex_exit(&zp
->z_lock
);
2895 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2897 if ((mask
& ATTR_XVATTR
) &&
2898 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2899 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2901 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2905 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2908 fuid_dirtied
= zsb
->z_fuid_dirty
;
2910 zfs_fuid_txhold(zsb
, tx
);
2912 zfs_sa_upgrade_txholds(tx
, zp
);
2914 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2920 * Set each attribute requested.
2921 * We group settings according to the locks they need to acquire.
2923 * Note: you cannot set ctime directly, although it will be
2924 * updated as a side-effect of calling this function.
2928 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2929 mutex_enter(&zp
->z_acl_lock
);
2930 mutex_enter(&zp
->z_lock
);
2932 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
2933 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2936 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2937 mutex_enter(&attrzp
->z_acl_lock
);
2938 mutex_enter(&attrzp
->z_lock
);
2939 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2940 SA_ZPL_FLAGS(zsb
), NULL
, &attrzp
->z_pflags
,
2941 sizeof (attrzp
->z_pflags
));
2944 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2946 if (mask
& ATTR_UID
) {
2947 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
2948 &new_uid
, sizeof (new_uid
));
2949 zp
->z_uid
= new_uid
;
2951 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2952 SA_ZPL_UID(zsb
), NULL
, &new_uid
,
2954 attrzp
->z_uid
= new_uid
;
2958 if (mask
& ATTR_GID
) {
2959 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
),
2960 NULL
, &new_gid
, sizeof (new_gid
));
2961 zp
->z_gid
= new_gid
;
2963 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2964 SA_ZPL_GID(zsb
), NULL
, &new_gid
,
2966 attrzp
->z_gid
= new_gid
;
2969 if (!(mask
& ATTR_MODE
)) {
2970 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
),
2971 NULL
, &new_mode
, sizeof (new_mode
));
2972 new_mode
= zp
->z_mode
;
2974 err
= zfs_acl_chown_setattr(zp
);
2977 err
= zfs_acl_chown_setattr(attrzp
);
2982 if (mask
& ATTR_MODE
) {
2983 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
2984 &new_mode
, sizeof (new_mode
));
2985 zp
->z_mode
= new_mode
;
2986 ASSERT3P(aclp
, !=, NULL
);
2987 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2989 if (zp
->z_acl_cached
)
2990 zfs_acl_free(zp
->z_acl_cached
);
2991 zp
->z_acl_cached
= aclp
;
2996 if ((mask
& ATTR_ATIME
) || zp
->z_atime_dirty
) {
2997 zp
->z_atime_dirty
= 0;
2998 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
2999 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
3000 &atime
, sizeof (atime
));
3003 if (mask
& ATTR_MTIME
) {
3004 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
3005 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
,
3006 mtime
, sizeof (mtime
));
3009 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3010 if (mask
& ATTR_SIZE
&& !(mask
& ATTR_MTIME
)) {
3011 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
),
3012 NULL
, mtime
, sizeof (mtime
));
3013 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
3014 &ctime
, sizeof (ctime
));
3015 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
3016 } else if (mask
!= 0) {
3017 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
3018 &ctime
, sizeof (ctime
));
3019 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
);
3021 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3022 SA_ZPL_CTIME(zsb
), NULL
,
3023 &ctime
, sizeof (ctime
));
3024 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
3029 * Do this after setting timestamps to prevent timestamp
3030 * update from toggling bit
3033 if (xoap
&& (mask
& ATTR_XVATTR
)) {
3036 * restore trimmed off masks
3037 * so that return masks can be set for caller.
3040 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
3041 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
3043 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
3044 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
3046 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
3047 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
3049 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
3050 XVA_SET_REQ(xvap
, XAT_NODUMP
);
3052 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
3053 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
3055 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
3056 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
3059 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
3060 ASSERT(S_ISREG(ip
->i_mode
));
3062 zfs_xvattr_set(zp
, xvap
, tx
);
3066 zfs_fuid_sync(zsb
, tx
);
3069 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
3071 mutex_exit(&zp
->z_lock
);
3072 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3073 mutex_exit(&zp
->z_acl_lock
);
3076 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3077 mutex_exit(&attrzp
->z_acl_lock
);
3078 mutex_exit(&attrzp
->z_lock
);
3081 if (err
== 0 && attrzp
) {
3082 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3093 zfs_fuid_info_free(fuidp
);
3099 if (err
== ERESTART
)
3102 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3104 zfs_inode_update(zp
);
3108 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3109 zil_commit(zilog
, 0);
3112 kmem_free(xattr_bulk
, sizeof (sa_bulk_attr_t
) * 7);
3113 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * 7);
3114 kmem_free(tmpxvattr
, sizeof (xvattr_t
));
3118 EXPORT_SYMBOL(zfs_setattr
);
3120 typedef struct zfs_zlock
{
3121 krwlock_t
*zl_rwlock
; /* lock we acquired */
3122 znode_t
*zl_znode
; /* znode we held */
3123 struct zfs_zlock
*zl_next
; /* next in list */
3127 * Drop locks and release vnodes that were held by zfs_rename_lock().
3130 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3134 while ((zl
= *zlpp
) != NULL
) {
3135 if (zl
->zl_znode
!= NULL
)
3136 iput(ZTOI(zl
->zl_znode
));
3137 rw_exit(zl
->zl_rwlock
);
3138 *zlpp
= zl
->zl_next
;
3139 kmem_free(zl
, sizeof (*zl
));
3144 * Search back through the directory tree, using the ".." entries.
3145 * Lock each directory in the chain to prevent concurrent renames.
3146 * Fail any attempt to move a directory into one of its own descendants.
3147 * XXX - z_parent_lock can overlap with map or grow locks
3150 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3154 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
3155 uint64_t oidp
= zp
->z_id
;
3156 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3157 krw_t rw
= RW_WRITER
;
3160 * First pass write-locks szp and compares to zp->z_id.
3161 * Later passes read-lock zp and compare to zp->z_parent.
3164 if (!rw_tryenter(rwlp
, rw
)) {
3166 * Another thread is renaming in this path.
3167 * Note that if we are a WRITER, we don't have any
3168 * parent_locks held yet.
3170 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3172 * Drop our locks and restart
3174 zfs_rename_unlock(&zl
);
3178 rwlp
= &szp
->z_parent_lock
;
3183 * Wait for other thread to drop its locks
3189 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3190 zl
->zl_rwlock
= rwlp
;
3191 zl
->zl_znode
= NULL
;
3192 zl
->zl_next
= *zlpp
;
3195 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3196 return (SET_ERROR(EINVAL
));
3198 if (oidp
== rootid
) /* We've hit the top */
3201 if (rw
== RW_READER
) { /* i.e. not the first pass */
3202 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
3207 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
3208 &oidp
, sizeof (oidp
));
3209 rwlp
= &zp
->z_parent_lock
;
3212 } while (zp
->z_id
!= sdzp
->z_id
);
3218 * Move an entry from the provided source directory to the target
3219 * directory. Change the entry name as indicated.
3221 * IN: sdip - Source directory containing the "old entry".
3222 * snm - Old entry name.
3223 * tdip - Target directory to contain the "new entry".
3224 * tnm - New entry name.
3225 * cr - credentials of caller.
3226 * flags - case flags
3228 * RETURN: 0 on success, error code on failure.
3231 * sdip,tdip - ctime|mtime updated
3235 zfs_rename(struct inode
*sdip
, char *snm
, struct inode
*tdip
, char *tnm
,
3236 cred_t
*cr
, int flags
)
3238 znode_t
*tdzp
, *szp
, *tzp
;
3239 znode_t
*sdzp
= ITOZ(sdip
);
3240 zfs_sb_t
*zsb
= ITOZSB(sdip
);
3242 zfs_dirlock_t
*sdl
, *tdl
;
3245 int cmp
, serr
, terr
;
3248 boolean_t waited
= B_FALSE
;
3251 ZFS_VERIFY_ZP(sdzp
);
3255 ZFS_VERIFY_ZP(tdzp
);
3258 * We check i_sb because snapshots and the ctldir must have different
3261 if (tdip
->i_sb
!= sdip
->i_sb
|| zfsctl_is_node(tdip
)) {
3263 return (SET_ERROR(EXDEV
));
3266 if (zsb
->z_utf8
&& u8_validate(tnm
,
3267 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3269 return (SET_ERROR(EILSEQ
));
3272 if (flags
& FIGNORECASE
)
3281 * This is to prevent the creation of links into attribute space
3282 * by renaming a linked file into/outof an attribute directory.
3283 * See the comment in zfs_link() for why this is considered bad.
3285 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3287 return (SET_ERROR(EINVAL
));
3291 * Lock source and target directory entries. To prevent deadlock,
3292 * a lock ordering must be defined. We lock the directory with
3293 * the smallest object id first, or if it's a tie, the one with
3294 * the lexically first name.
3296 if (sdzp
->z_id
< tdzp
->z_id
) {
3298 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3302 * First compare the two name arguments without
3303 * considering any case folding.
3305 int nofold
= (zsb
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3307 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3308 ASSERT(error
== 0 || !zsb
->z_utf8
);
3311 * POSIX: "If the old argument and the new argument
3312 * both refer to links to the same existing file,
3313 * the rename() function shall return successfully
3314 * and perform no other action."
3320 * If the file system is case-folding, then we may
3321 * have some more checking to do. A case-folding file
3322 * system is either supporting mixed case sensitivity
3323 * access or is completely case-insensitive. Note
3324 * that the file system is always case preserving.
3326 * In mixed sensitivity mode case sensitive behavior
3327 * is the default. FIGNORECASE must be used to
3328 * explicitly request case insensitive behavior.
3330 * If the source and target names provided differ only
3331 * by case (e.g., a request to rename 'tim' to 'Tim'),
3332 * we will treat this as a special case in the
3333 * case-insensitive mode: as long as the source name
3334 * is an exact match, we will allow this to proceed as
3335 * a name-change request.
3337 if ((zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
3338 (zsb
->z_case
== ZFS_CASE_MIXED
&&
3339 flags
& FIGNORECASE
)) &&
3340 u8_strcmp(snm
, tnm
, 0, zsb
->z_norm
, U8_UNICODE_LATEST
,
3343 * case preserving rename request, require exact
3352 * If the source and destination directories are the same, we should
3353 * grab the z_name_lock of that directory only once.
3357 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3361 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3362 ZEXISTS
| zflg
, NULL
, NULL
);
3363 terr
= zfs_dirent_lock(&tdl
,
3364 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3366 terr
= zfs_dirent_lock(&tdl
,
3367 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3368 serr
= zfs_dirent_lock(&sdl
,
3369 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3375 * Source entry invalid or not there.
3378 zfs_dirent_unlock(tdl
);
3384 rw_exit(&sdzp
->z_name_lock
);
3386 if (strcmp(snm
, "..") == 0)
3392 zfs_dirent_unlock(sdl
);
3396 rw_exit(&sdzp
->z_name_lock
);
3398 if (strcmp(tnm
, "..") == 0)
3405 * Must have write access at the source to remove the old entry
3406 * and write access at the target to create the new entry.
3407 * Note that if target and source are the same, this can be
3408 * done in a single check.
3411 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
3414 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3416 * Check to make sure rename is valid.
3417 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3419 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
3424 * Does target exist?
3428 * Source and target must be the same type.
3430 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3431 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3432 error
= SET_ERROR(ENOTDIR
);
3436 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3437 error
= SET_ERROR(EISDIR
);
3442 * POSIX dictates that when the source and target
3443 * entries refer to the same file object, rename
3444 * must do nothing and exit without error.
3446 if (szp
->z_id
== tzp
->z_id
) {
3452 tx
= dmu_tx_create(zsb
->z_os
);
3453 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3454 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3455 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3456 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3458 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3459 zfs_sa_upgrade_txholds(tx
, tdzp
);
3462 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3463 zfs_sa_upgrade_txholds(tx
, tzp
);
3466 zfs_sa_upgrade_txholds(tx
, szp
);
3467 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
3468 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3471 zfs_rename_unlock(&zl
);
3472 zfs_dirent_unlock(sdl
);
3473 zfs_dirent_unlock(tdl
);
3476 rw_exit(&sdzp
->z_name_lock
);
3481 if (error
== ERESTART
) {
3492 if (tzp
) /* Attempt to remove the existing target */
3493 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3496 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3498 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3500 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zsb
),
3501 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3504 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3506 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3507 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
3508 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3511 * At this point, we have successfully created
3512 * the target name, but have failed to remove
3513 * the source name. Since the create was done
3514 * with the ZRENAMING flag, there are
3515 * complications; for one, the link count is
3516 * wrong. The easiest way to deal with this
3517 * is to remove the newly created target, and
3518 * return the original error. This must
3519 * succeed; fortunately, it is very unlikely to
3520 * fail, since we just created it.
3522 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3523 ZRENAMING
, NULL
), ==, 0);
3531 zfs_rename_unlock(&zl
);
3533 zfs_dirent_unlock(sdl
);
3534 zfs_dirent_unlock(tdl
);
3536 zfs_inode_update(sdzp
);
3538 rw_exit(&sdzp
->z_name_lock
);
3541 zfs_inode_update(tdzp
);
3543 zfs_inode_update(szp
);
3546 zfs_inode_update(tzp
);
3550 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3551 zil_commit(zilog
, 0);
3556 EXPORT_SYMBOL(zfs_rename
);
3559 * Insert the indicated symbolic reference entry into the directory.
3561 * IN: dip - Directory to contain new symbolic link.
3562 * link - Name for new symlink entry.
3563 * vap - Attributes of new entry.
3564 * target - Target path of new symlink.
3566 * cr - credentials of caller.
3567 * flags - case flags
3569 * RETURN: 0 on success, error code on failure.
3572 * dip - ctime|mtime updated
3576 zfs_symlink(struct inode
*dip
, char *name
, vattr_t
*vap
, char *link
,
3577 struct inode
**ipp
, cred_t
*cr
, int flags
)
3579 znode_t
*zp
, *dzp
= ITOZ(dip
);
3582 zfs_sb_t
*zsb
= ITOZSB(dip
);
3584 uint64_t len
= strlen(link
);
3587 zfs_acl_ids_t acl_ids
;
3588 boolean_t fuid_dirtied
;
3589 uint64_t txtype
= TX_SYMLINK
;
3590 boolean_t waited
= B_FALSE
;
3592 ASSERT(S_ISLNK(vap
->va_mode
));
3598 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
3599 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3601 return (SET_ERROR(EILSEQ
));
3603 if (flags
& FIGNORECASE
)
3606 if (len
> MAXPATHLEN
) {
3608 return (SET_ERROR(ENAMETOOLONG
));
3611 if ((error
= zfs_acl_ids_create(dzp
, 0,
3612 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3620 * Attempt to lock directory; fail if entry already exists.
3622 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3624 zfs_acl_ids_free(&acl_ids
);
3629 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3630 zfs_acl_ids_free(&acl_ids
);
3631 zfs_dirent_unlock(dl
);
3636 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
3637 zfs_acl_ids_free(&acl_ids
);
3638 zfs_dirent_unlock(dl
);
3640 return (SET_ERROR(EDQUOT
));
3642 tx
= dmu_tx_create(zsb
->z_os
);
3643 fuid_dirtied
= zsb
->z_fuid_dirty
;
3644 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3645 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3646 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3647 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3648 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3649 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3650 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3651 acl_ids
.z_aclp
->z_acl_bytes
);
3654 zfs_fuid_txhold(zsb
, tx
);
3655 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3657 zfs_dirent_unlock(dl
);
3658 if (error
== ERESTART
) {
3664 zfs_acl_ids_free(&acl_ids
);
3671 * Create a new object for the symlink.
3672 * for version 4 ZPL datsets the symlink will be an SA attribute
3674 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3677 zfs_fuid_sync(zsb
, tx
);
3679 mutex_enter(&zp
->z_lock
);
3681 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zsb
),
3684 zfs_sa_symlink(zp
, link
, len
, tx
);
3685 mutex_exit(&zp
->z_lock
);
3688 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
3689 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3691 * Insert the new object into the directory.
3693 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3695 if (flags
& FIGNORECASE
)
3697 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3699 zfs_inode_update(dzp
);
3700 zfs_inode_update(zp
);
3702 zfs_acl_ids_free(&acl_ids
);
3706 zfs_dirent_unlock(dl
);
3710 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3711 zil_commit(zilog
, 0);
3716 EXPORT_SYMBOL(zfs_symlink
);
3719 * Return, in the buffer contained in the provided uio structure,
3720 * the symbolic path referred to by ip.
3722 * IN: ip - inode of symbolic link
3723 * uio - structure to contain the link path.
3724 * cr - credentials of caller.
3726 * RETURN: 0 if success
3727 * error code if failure
3730 * ip - atime updated
3734 zfs_readlink(struct inode
*ip
, uio_t
*uio
, cred_t
*cr
)
3736 znode_t
*zp
= ITOZ(ip
);
3737 zfs_sb_t
*zsb
= ITOZSB(ip
);
3743 mutex_enter(&zp
->z_lock
);
3745 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3746 SA_ZPL_SYMLINK(zsb
), uio
);
3748 error
= zfs_sa_readlink(zp
, uio
);
3749 mutex_exit(&zp
->z_lock
);
3754 EXPORT_SYMBOL(zfs_readlink
);
3757 * Insert a new entry into directory tdip referencing sip.
3759 * IN: tdip - Directory to contain new entry.
3760 * sip - inode of new entry.
3761 * name - name of new entry.
3762 * cr - credentials of caller.
3764 * RETURN: 0 if success
3765 * error code if failure
3768 * tdip - ctime|mtime updated
3769 * sip - ctime updated
3773 zfs_link(struct inode
*tdip
, struct inode
*sip
, char *name
, cred_t
*cr
,
3776 znode_t
*dzp
= ITOZ(tdip
);
3778 zfs_sb_t
*zsb
= ITOZSB(tdip
);
3786 boolean_t waited
= B_FALSE
;
3788 ASSERT(S_ISDIR(tdip
->i_mode
));
3795 * POSIX dictates that we return EPERM here.
3796 * Better choices include ENOTSUP or EISDIR.
3798 if (S_ISDIR(sip
->i_mode
)) {
3800 return (SET_ERROR(EPERM
));
3807 * We check i_sb because snapshots and the ctldir must have different
3810 if (sip
->i_sb
!= tdip
->i_sb
|| zfsctl_is_node(sip
)) {
3812 return (SET_ERROR(EXDEV
));
3815 /* Prevent links to .zfs/shares files */
3817 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
3818 &parent
, sizeof (uint64_t))) != 0) {
3822 if (parent
== zsb
->z_shares_dir
) {
3824 return (SET_ERROR(EPERM
));
3827 if (zsb
->z_utf8
&& u8_validate(name
,
3828 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3830 return (SET_ERROR(EILSEQ
));
3832 if (flags
& FIGNORECASE
)
3836 * We do not support links between attributes and non-attributes
3837 * because of the potential security risk of creating links
3838 * into "normal" file space in order to circumvent restrictions
3839 * imposed in attribute space.
3841 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
3843 return (SET_ERROR(EINVAL
));
3846 owner
= zfs_fuid_map_id(zsb
, szp
->z_uid
, cr
, ZFS_OWNER
);
3847 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
3849 return (SET_ERROR(EPERM
));
3852 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3859 * Attempt to lock directory; fail if entry already exists.
3861 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
3867 tx
= dmu_tx_create(zsb
->z_os
);
3868 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3869 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3870 zfs_sa_upgrade_txholds(tx
, szp
);
3871 zfs_sa_upgrade_txholds(tx
, dzp
);
3872 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3874 zfs_dirent_unlock(dl
);
3875 if (error
== ERESTART
) {
3886 error
= zfs_link_create(dl
, szp
, tx
, 0);
3889 uint64_t txtype
= TX_LINK
;
3890 if (flags
& FIGNORECASE
)
3892 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
3897 zfs_dirent_unlock(dl
);
3899 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3900 zil_commit(zilog
, 0);
3902 zfs_inode_update(dzp
);
3903 zfs_inode_update(szp
);
3907 EXPORT_SYMBOL(zfs_link
);
3910 zfs_putpage_commit_cb(void *arg
)
3912 struct page
*pp
= arg
;
3915 end_page_writeback(pp
);
3919 * Push a page out to disk, once the page is on stable storage the
3920 * registered commit callback will be run as notification of completion.
3922 * IN: ip - page mapped for inode.
3923 * pp - page to push (page is locked)
3924 * wbc - writeback control data
3926 * RETURN: 0 if success
3927 * error code if failure
3930 * ip - ctime|mtime updated
3934 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
3936 znode_t
*zp
= ITOZ(ip
);
3937 zfs_sb_t
*zsb
= ITOZSB(ip
);
3945 uint64_t mtime
[2], ctime
[2];
3946 sa_bulk_attr_t bulk
[3];
3948 struct address_space
*mapping
;
3953 ASSERT(PageLocked(pp
));
3955 pgoff
= page_offset(pp
); /* Page byte-offset in file */
3956 offset
= i_size_read(ip
); /* File length in bytes */
3957 pglen
= MIN(PAGE_SIZE
, /* Page length in bytes */
3958 P2ROUNDUP(offset
, PAGE_SIZE
)-pgoff
);
3960 /* Page is beyond end of file */
3961 if (pgoff
>= offset
) {
3967 /* Truncate page length to end of file */
3968 if (pgoff
+ pglen
> offset
)
3969 pglen
= offset
- pgoff
;
3973 * FIXME: Allow mmap writes past its quota. The correct fix
3974 * is to register a page_mkwrite() handler to count the page
3975 * against its quota when it is about to be dirtied.
3977 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
3978 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
3984 * The ordering here is critical and must adhere to the following
3985 * rules in order to avoid deadlocking in either zfs_read() or
3986 * zfs_free_range() due to a lock inversion.
3988 * 1) The page must be unlocked prior to acquiring the range lock.
3989 * This is critical because zfs_read() calls find_lock_page()
3990 * which may block on the page lock while holding the range lock.
3992 * 2) Before setting or clearing write back on a page the range lock
3993 * must be held in order to prevent a lock inversion with the
3994 * zfs_free_range() function.
3996 * This presents a problem because upon entering this function the
3997 * page lock is already held. To safely acquire the range lock the
3998 * page lock must be dropped. This creates a window where another
3999 * process could truncate, invalidate, dirty, or write out the page.
4001 * Therefore, after successfully reacquiring the range and page locks
4002 * the current page state is checked. In the common case everything
4003 * will be as is expected and it can be written out. However, if
4004 * the page state has changed it must be handled accordingly.
4006 mapping
= pp
->mapping
;
4007 redirty_page_for_writepage(wbc
, pp
);
4010 rl
= zfs_range_lock(zp
, pgoff
, pglen
, RL_WRITER
);
4013 /* Page mapping changed or it was no longer dirty, we're done */
4014 if (unlikely((mapping
!= pp
->mapping
) || !PageDirty(pp
))) {
4016 zfs_range_unlock(rl
);
4021 /* Another process started write block if required */
4022 if (PageWriteback(pp
)) {
4024 zfs_range_unlock(rl
);
4026 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
4027 wait_on_page_writeback(pp
);
4033 /* Clear the dirty flag the required locks are held */
4034 if (!clear_page_dirty_for_io(pp
)) {
4036 zfs_range_unlock(rl
);
4042 * Counterpart for redirty_page_for_writepage() above. This page
4043 * was in fact not skipped and should not be counted as if it were.
4045 wbc
->pages_skipped
--;
4046 set_page_writeback(pp
);
4049 tx
= dmu_tx_create(zsb
->z_os
);
4050 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
4051 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4052 zfs_sa_upgrade_txholds(tx
, zp
);
4054 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
4056 if (err
== ERESTART
)
4060 __set_page_dirty_nobuffers(pp
);
4062 end_page_writeback(pp
);
4063 zfs_range_unlock(rl
);
4069 ASSERT3U(pglen
, <=, PAGE_SIZE
);
4070 dmu_write(zsb
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
4073 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
4074 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
4075 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zsb
), NULL
, &zp
->z_pflags
, 8);
4077 /* Preserve the mtime and ctime provided by the inode */
4078 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4079 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4080 zp
->z_atime_dirty
= 0;
4083 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4085 zfs_log_write(zsb
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
4086 zfs_putpage_commit_cb
, pp
);
4089 zfs_range_unlock(rl
);
4091 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
4093 * Note that this is rarely called under writepages(), because
4094 * writepages() normally handles the entire commit for
4095 * performance reasons.
4097 if (zsb
->z_log
!= NULL
)
4098 zil_commit(zsb
->z_log
, zp
->z_id
);
4106 * Update the system attributes when the inode has been dirtied. For the
4107 * moment we only update the mode, atime, mtime, and ctime.
4110 zfs_dirty_inode(struct inode
*ip
, int flags
)
4112 znode_t
*zp
= ITOZ(ip
);
4113 zfs_sb_t
*zsb
= ITOZSB(ip
);
4115 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
4116 sa_bulk_attr_t bulk
[4];
4120 if (zfs_is_readonly(zsb
) || dmu_objset_is_snapshot(zsb
->z_os
))
4128 * This is the lazytime semantic indroduced in Linux 4.0
4129 * This flag will only be called from update_time when lazytime is set.
4130 * (Note, I_DIRTY_SYNC will also set if not lazytime)
4131 * Fortunately mtime and ctime are managed within ZFS itself, so we
4132 * only need to dirty atime.
4134 if (flags
== I_DIRTY_TIME
) {
4135 zp
->z_atime_dirty
= 1;
4140 tx
= dmu_tx_create(zsb
->z_os
);
4142 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4143 zfs_sa_upgrade_txholds(tx
, zp
);
4145 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4151 mutex_enter(&zp
->z_lock
);
4152 zp
->z_atime_dirty
= 0;
4154 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zsb
), NULL
, &mode
, 8);
4155 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zsb
), NULL
, &atime
, 16);
4156 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
4157 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
4159 /* Preserve the mode, mtime and ctime provided by the inode */
4160 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4161 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4162 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4167 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4168 mutex_exit(&zp
->z_lock
);
4175 EXPORT_SYMBOL(zfs_dirty_inode
);
4179 zfs_inactive(struct inode
*ip
)
4181 znode_t
*zp
= ITOZ(ip
);
4182 zfs_sb_t
*zsb
= ITOZSB(ip
);
4185 int need_unlock
= 0;
4187 /* Only read lock if we haven't already write locked, e.g. rollback */
4188 if (!RW_WRITE_HELD(&zsb
->z_teardown_inactive_lock
)) {
4190 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_READER
);
4192 if (zp
->z_sa_hdl
== NULL
) {
4194 rw_exit(&zsb
->z_teardown_inactive_lock
);
4198 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4199 dmu_tx_t
*tx
= dmu_tx_create(zsb
->z_os
);
4201 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4202 zfs_sa_upgrade_txholds(tx
, zp
);
4203 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4207 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4208 mutex_enter(&zp
->z_lock
);
4209 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
),
4210 (void *)&atime
, sizeof (atime
), tx
);
4211 zp
->z_atime_dirty
= 0;
4212 mutex_exit(&zp
->z_lock
);
4219 rw_exit(&zsb
->z_teardown_inactive_lock
);
4221 EXPORT_SYMBOL(zfs_inactive
);
4224 * Bounds-check the seek operation.
4226 * IN: ip - inode seeking within
4227 * ooff - old file offset
4228 * noffp - pointer to new file offset
4229 * ct - caller context
4231 * RETURN: 0 if success
4232 * EINVAL if new offset invalid
4236 zfs_seek(struct inode
*ip
, offset_t ooff
, offset_t
*noffp
)
4238 if (S_ISDIR(ip
->i_mode
))
4240 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4242 EXPORT_SYMBOL(zfs_seek
);
4245 * Fill pages with data from the disk.
4248 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4250 znode_t
*zp
= ITOZ(ip
);
4251 zfs_sb_t
*zsb
= ITOZSB(ip
);
4253 struct page
*cur_pp
;
4254 u_offset_t io_off
, total
;
4261 io_len
= nr_pages
<< PAGE_SHIFT
;
4262 i_size
= i_size_read(ip
);
4263 io_off
= page_offset(pl
[0]);
4265 if (io_off
+ io_len
> i_size
)
4266 io_len
= i_size
- io_off
;
4269 * Iterate over list of pages and read each page individually.
4273 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4277 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4281 /* convert checksum errors into IO errors */
4283 err
= SET_ERROR(EIO
);
4286 cur_pp
= pl
[++page_idx
];
4293 * Uses zfs_fillpage to read data from the file and fill the pages.
4295 * IN: ip - inode of file to get data from.
4296 * pl - list of pages to read
4297 * nr_pages - number of pages to read
4299 * RETURN: 0 on success, error code on failure.
4302 * vp - atime updated
4306 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4308 znode_t
*zp
= ITOZ(ip
);
4309 zfs_sb_t
*zsb
= ITOZSB(ip
);
4318 err
= zfs_fillpage(ip
, pl
, nr_pages
);
4323 EXPORT_SYMBOL(zfs_getpage
);
4326 * Check ZFS specific permissions to memory map a section of a file.
4328 * IN: ip - inode of the file to mmap
4330 * addrp - start address in memory region
4331 * len - length of memory region
4332 * vm_flags- address flags
4334 * RETURN: 0 if success
4335 * error code if failure
4339 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
4340 unsigned long vm_flags
)
4342 znode_t
*zp
= ITOZ(ip
);
4343 zfs_sb_t
*zsb
= ITOZSB(ip
);
4348 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
4349 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4351 return (SET_ERROR(EPERM
));
4354 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
4355 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4357 return (SET_ERROR(EACCES
));
4360 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4362 return (SET_ERROR(ENXIO
));
4368 EXPORT_SYMBOL(zfs_map
);
4371 * convoff - converts the given data (start, whence) to the
4375 convoff(struct inode
*ip
, flock64_t
*lckdat
, int whence
, offset_t offset
)
4380 if ((lckdat
->l_whence
== 2) || (whence
== 2)) {
4381 if ((error
= zfs_getattr(ip
, &vap
, 0, CRED()) != 0))
4385 switch (lckdat
->l_whence
) {
4387 lckdat
->l_start
+= offset
;
4390 lckdat
->l_start
+= vap
.va_size
;
4395 return (SET_ERROR(EINVAL
));
4398 if (lckdat
->l_start
< 0)
4399 return (SET_ERROR(EINVAL
));
4403 lckdat
->l_start
-= offset
;
4406 lckdat
->l_start
-= vap
.va_size
;
4411 return (SET_ERROR(EINVAL
));
4414 lckdat
->l_whence
= (short)whence
;
4419 * Free or allocate space in a file. Currently, this function only
4420 * supports the `F_FREESP' command. However, this command is somewhat
4421 * misnamed, as its functionality includes the ability to allocate as
4422 * well as free space.
4424 * IN: ip - inode of file to free data in.
4425 * cmd - action to take (only F_FREESP supported).
4426 * bfp - section of file to free/alloc.
4427 * flag - current file open mode flags.
4428 * offset - current file offset.
4429 * cr - credentials of caller [UNUSED].
4431 * RETURN: 0 on success, error code on failure.
4434 * ip - ctime|mtime updated
4438 zfs_space(struct inode
*ip
, int cmd
, flock64_t
*bfp
, int flag
,
4439 offset_t offset
, cred_t
*cr
)
4441 znode_t
*zp
= ITOZ(ip
);
4442 zfs_sb_t
*zsb
= ITOZSB(ip
);
4449 if (cmd
!= F_FREESP
) {
4451 return (SET_ERROR(EINVAL
));
4455 * Callers might not be able to detect properly that we are read-only,
4456 * so check it explicitly here.
4458 if (zfs_is_readonly(zsb
)) {
4460 return (SET_ERROR(EROFS
));
4463 if ((error
= convoff(ip
, bfp
, 0, offset
))) {
4468 if (bfp
->l_len
< 0) {
4470 return (SET_ERROR(EINVAL
));
4474 * Permissions aren't checked on Solaris because on this OS
4475 * zfs_space() can only be called with an opened file handle.
4476 * On Linux we can get here through truncate_range() which
4477 * operates directly on inodes, so we need to check access rights.
4479 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
4485 len
= bfp
->l_len
; /* 0 means from off to end of file */
4487 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4492 EXPORT_SYMBOL(zfs_space
);
4496 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4498 znode_t
*zp
= ITOZ(ip
);
4499 zfs_sb_t
*zsb
= ITOZSB(ip
);
4502 uint64_t object
= zp
->z_id
;
4509 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
),
4510 &gen64
, sizeof (uint64_t))) != 0) {
4515 gen
= (uint32_t)gen64
;
4517 size
= (zsb
->z_parent
!= zsb
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4518 if (fidp
->fid_len
< size
) {
4519 fidp
->fid_len
= size
;
4521 return (SET_ERROR(ENOSPC
));
4524 zfid
= (zfid_short_t
*)fidp
;
4526 zfid
->zf_len
= size
;
4528 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4529 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4531 /* Must have a non-zero generation number to distinguish from .zfs */
4534 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4535 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4537 if (size
== LONG_FID_LEN
) {
4538 uint64_t objsetid
= dmu_objset_id(zsb
->z_os
);
4541 zlfid
= (zfid_long_t
*)fidp
;
4543 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4544 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4546 /* XXX - this should be the generation number for the objset */
4547 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4548 zlfid
->zf_setgen
[i
] = 0;
4554 EXPORT_SYMBOL(zfs_fid
);
4558 zfs_getsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4560 znode_t
*zp
= ITOZ(ip
);
4561 zfs_sb_t
*zsb
= ITOZSB(ip
);
4563 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4567 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
4572 EXPORT_SYMBOL(zfs_getsecattr
);
4576 zfs_setsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4578 znode_t
*zp
= ITOZ(ip
);
4579 zfs_sb_t
*zsb
= ITOZSB(ip
);
4581 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4582 zilog_t
*zilog
= zsb
->z_log
;
4587 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
4589 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4590 zil_commit(zilog
, 0);
4595 EXPORT_SYMBOL(zfs_setsecattr
);
4597 #ifdef HAVE_UIO_ZEROCOPY
4599 * Tunable, both must be a power of 2.
4601 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4602 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4603 * an arcbuf for a partial block read
4605 int zcr_blksz_min
= (1 << 10); /* 1K */
4606 int zcr_blksz_max
= (1 << 17); /* 128K */
4610 zfs_reqzcbuf(struct inode
*ip
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
)
4612 znode_t
*zp
= ITOZ(ip
);
4613 zfs_sb_t
*zsb
= ITOZSB(ip
);
4614 int max_blksz
= zsb
->z_max_blksz
;
4615 uio_t
*uio
= &xuio
->xu_uio
;
4616 ssize_t size
= uio
->uio_resid
;
4617 offset_t offset
= uio
->uio_loffset
;
4622 int preamble
, postamble
;
4624 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
4625 return (SET_ERROR(EINVAL
));
4632 * Loan out an arc_buf for write if write size is bigger than
4633 * max_blksz, and the file's block size is also max_blksz.
4636 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
4638 return (SET_ERROR(EINVAL
));
4641 * Caller requests buffers for write before knowing where the
4642 * write offset might be (e.g. NFS TCP write).
4647 preamble
= P2PHASE(offset
, blksz
);
4649 preamble
= blksz
- preamble
;
4654 postamble
= P2PHASE(size
, blksz
);
4657 fullblk
= size
/ blksz
;
4658 (void) dmu_xuio_init(xuio
,
4659 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
4662 * Have to fix iov base/len for partial buffers. They
4663 * currently represent full arc_buf's.
4666 /* data begins in the middle of the arc_buf */
4667 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4670 (void) dmu_xuio_add(xuio
, abuf
,
4671 blksz
- preamble
, preamble
);
4674 for (i
= 0; i
< fullblk
; i
++) {
4675 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4678 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
4682 /* data ends in the middle of the arc_buf */
4683 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4686 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
4691 * Loan out an arc_buf for read if the read size is larger than
4692 * the current file block size. Block alignment is not
4693 * considered. Partial arc_buf will be loaned out for read.
4695 blksz
= zp
->z_blksz
;
4696 if (blksz
< zcr_blksz_min
)
4697 blksz
= zcr_blksz_min
;
4698 if (blksz
> zcr_blksz_max
)
4699 blksz
= zcr_blksz_max
;
4700 /* avoid potential complexity of dealing with it */
4701 if (blksz
> max_blksz
) {
4703 return (SET_ERROR(EINVAL
));
4706 maxsize
= zp
->z_size
- uio
->uio_loffset
;
4712 return (SET_ERROR(EINVAL
));
4717 return (SET_ERROR(EINVAL
));
4720 uio
->uio_extflg
= UIO_XUIO
;
4721 XUIO_XUZC_RW(xuio
) = ioflag
;
4728 zfs_retzcbuf(struct inode
*ip
, xuio_t
*xuio
, cred_t
*cr
)
4732 int ioflag
= XUIO_XUZC_RW(xuio
);
4734 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
4736 i
= dmu_xuio_cnt(xuio
);
4738 abuf
= dmu_xuio_arcbuf(xuio
, i
);
4740 * if abuf == NULL, it must be a write buffer
4741 * that has been returned in zfs_write().
4744 dmu_return_arcbuf(abuf
);
4745 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
4748 dmu_xuio_fini(xuio
);
4751 #endif /* HAVE_UIO_ZEROCOPY */
4753 #if defined(_KERNEL) && defined(HAVE_SPL)
4754 module_param(zfs_delete_blocks
, ulong
, 0644);
4755 MODULE_PARM_DESC(zfs_delete_blocks
, "Delete files larger than N blocks async");
4756 module_param(zfs_read_chunk_size
, long, 0644);
4757 MODULE_PARM_DESC(zfs_read_chunk_size
, "Bytes to read per chunk");