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) 2013 by Delphix. All rights reserved.
24 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
27 /* Portions Copyright 2007 Jeremy Teo */
28 /* Portions Copyright 2010 Robert Milkowski */
31 #include <sys/types.h>
32 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/resource.h>
38 #include <sys/vfs_opreg.h>
42 #include <sys/taskq.h>
44 #include <sys/vmsystm.h>
45 #include <sys/atomic.h>
47 #include <sys/pathname.h>
48 #include <sys/cmn_err.h>
49 #include <sys/errno.h>
50 #include <sys/unistd.h>
51 #include <sys/zfs_dir.h>
52 #include <sys/zfs_acl.h>
53 #include <sys/zfs_ioctl.h>
54 #include <sys/fs/zfs.h>
56 #include <sys/dmu_objset.h>
62 #include <sys/dirent.h>
63 #include <sys/policy.h>
64 #include <sys/sunddi.h>
67 #include "fs/fs_subr.h"
68 #include <sys/zfs_ctldir.h>
69 #include <sys/zfs_fuid.h>
70 #include <sys/zfs_sa.h>
71 #include <sys/zfs_vnops.h>
73 #include <sys/zfs_rlock.h>
74 #include <sys/extdirent.h>
75 #include <sys/kidmap.h>
83 * Each vnode op performs some logical unit of work. To do this, the ZPL must
84 * properly lock its in-core state, create a DMU transaction, do the work,
85 * record this work in the intent log (ZIL), commit the DMU transaction,
86 * and wait for the intent log to commit if it is a synchronous operation.
87 * Moreover, the vnode ops must work in both normal and log replay context.
88 * The ordering of events is important to avoid deadlocks and references
89 * to freed memory. The example below illustrates the following Big Rules:
91 * (1) A check must be made in each zfs thread for a mounted file system.
92 * This is done avoiding races using ZFS_ENTER(zsb).
93 * A ZFS_EXIT(zsb) is needed before all returns. Any znodes
94 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
95 * can return EIO from the calling function.
97 * (2) iput() should always be the last thing except for zil_commit()
98 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
99 * First, if it's the last reference, the vnode/znode
100 * can be freed, so the zp may point to freed memory. Second, the last
101 * reference will call zfs_zinactive(), which may induce a lot of work --
102 * pushing cached pages (which acquires range locks) and syncing out
103 * cached atime changes. Third, zfs_zinactive() may require a new tx,
104 * which could deadlock the system if you were already holding one.
105 * If you must call iput() within a tx then use zfs_iput_async().
107 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
108 * as they can span dmu_tx_assign() calls.
110 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
111 * dmu_tx_assign(). This is critical because we don't want to block
112 * while holding locks.
114 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
115 * reduces lock contention and CPU usage when we must wait (note that if
116 * throughput is constrained by the storage, nearly every transaction
119 * Note, in particular, that if a lock is sometimes acquired before
120 * the tx assigns, and sometimes after (e.g. z_lock), then failing
121 * to use a non-blocking assign can deadlock the system. The scenario:
123 * Thread A has grabbed a lock before calling dmu_tx_assign().
124 * Thread B is in an already-assigned tx, and blocks for this lock.
125 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
126 * forever, because the previous txg can't quiesce until B's tx commits.
128 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
129 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
130 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
131 * to indicate that this operation has already called dmu_tx_wait().
132 * This will ensure that we don't retry forever, waiting a short bit
135 * (5) If the operation succeeded, generate the intent log entry for it
136 * before dropping locks. This ensures that the ordering of events
137 * in the intent log matches the order in which they actually occurred.
138 * During ZIL replay the zfs_log_* functions will update the sequence
139 * number to indicate the zil transaction has replayed.
141 * (6) At the end of each vnode op, the DMU tx must always commit,
142 * regardless of whether there were any errors.
144 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
145 * to ensure that synchronous semantics are provided when necessary.
147 * In general, this is how things should be ordered in each vnode op:
149 * ZFS_ENTER(zsb); // exit if unmounted
151 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
152 * rw_enter(...); // grab any other locks you need
153 * tx = dmu_tx_create(...); // get DMU tx
154 * dmu_tx_hold_*(); // hold each object you might modify
155 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
157 * rw_exit(...); // drop locks
158 * zfs_dirent_unlock(dl); // unlock directory entry
159 * iput(...); // release held vnodes
160 * if (error == ERESTART) {
166 * dmu_tx_abort(tx); // abort DMU tx
167 * ZFS_EXIT(zsb); // finished in zfs
168 * return (error); // really out of space
170 * error = do_real_work(); // do whatever this VOP does
172 * zfs_log_*(...); // on success, make ZIL entry
173 * dmu_tx_commit(tx); // commit DMU tx -- error or not
174 * rw_exit(...); // drop locks
175 * zfs_dirent_unlock(dl); // unlock directory entry
176 * iput(...); // release held vnodes
177 * zil_commit(zilog, foid); // synchronous when necessary
178 * ZFS_EXIT(zsb); // finished in zfs
179 * return (error); // done, report error
183 * Virus scanning is unsupported. It would be possible to add a hook
184 * here to performance the required virus scan. This could be done
185 * entirely in the kernel or potentially as an update to invoke a
189 zfs_vscan(struct inode
*ip
, cred_t
*cr
, int async
)
196 zfs_open(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
198 znode_t
*zp
= ITOZ(ip
);
199 zfs_sb_t
*zsb
= ITOZSB(ip
);
204 /* Honor ZFS_APPENDONLY file attribute */
205 if ((mode
& FMODE_WRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
206 ((flag
& O_APPEND
) == 0)) {
208 return (SET_ERROR(EPERM
));
211 /* Virus scan eligible files on open */
212 if (!zfs_has_ctldir(zp
) && zsb
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
213 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0) {
214 if (zfs_vscan(ip
, cr
, 0) != 0) {
216 return (SET_ERROR(EACCES
));
220 /* Keep a count of the synchronous opens in the znode */
222 atomic_inc_32(&zp
->z_sync_cnt
);
227 EXPORT_SYMBOL(zfs_open
);
231 zfs_close(struct inode
*ip
, int flag
, cred_t
*cr
)
233 znode_t
*zp
= ITOZ(ip
);
234 zfs_sb_t
*zsb
= ITOZSB(ip
);
239 /* Decrement the synchronous opens in the znode */
241 atomic_dec_32(&zp
->z_sync_cnt
);
243 if (!zfs_has_ctldir(zp
) && zsb
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
244 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0)
245 VERIFY(zfs_vscan(ip
, cr
, 1) == 0);
250 EXPORT_SYMBOL(zfs_close
);
252 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
254 * Lseek support for finding holes (cmd == SEEK_HOLE) and
255 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
258 zfs_holey_common(struct inode
*ip
, int cmd
, loff_t
*off
)
260 znode_t
*zp
= ITOZ(ip
);
261 uint64_t noff
= (uint64_t)*off
; /* new offset */
266 file_sz
= zp
->z_size
;
267 if (noff
>= file_sz
) {
268 return (SET_ERROR(ENXIO
));
271 if (cmd
== SEEK_HOLE
)
276 error
= dmu_offset_next(ZTOZSB(zp
)->z_os
, zp
->z_id
, hole
, &noff
);
279 return (SET_ERROR(ENXIO
));
282 * We could find a hole that begins after the logical end-of-file,
283 * because dmu_offset_next() only works on whole blocks. If the
284 * EOF falls mid-block, then indicate that the "virtual hole"
285 * at the end of the file begins at the logical EOF, rather than
286 * at the end of the last block.
288 if (noff
> file_sz
) {
300 zfs_holey(struct inode
*ip
, int cmd
, loff_t
*off
)
302 znode_t
*zp
= ITOZ(ip
);
303 zfs_sb_t
*zsb
= ITOZSB(ip
);
309 error
= zfs_holey_common(ip
, cmd
, off
);
314 EXPORT_SYMBOL(zfs_holey
);
315 #endif /* SEEK_HOLE && SEEK_DATA */
319 * When a file is memory mapped, we must keep the IO data synchronized
320 * between the DMU cache and the memory mapped pages. What this means:
322 * On Write: If we find a memory mapped page, we write to *both*
323 * the page and the dmu buffer.
326 update_pages(struct inode
*ip
, int64_t start
, int len
,
327 objset_t
*os
, uint64_t oid
)
329 struct address_space
*mp
= ip
->i_mapping
;
335 off
= start
& (PAGE_SIZE
-1);
336 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
337 nbytes
= MIN(PAGE_SIZE
- off
, len
);
339 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
341 if (mapping_writably_mapped(mp
))
342 flush_dcache_page(pp
);
345 (void) dmu_read(os
, oid
, start
+off
, nbytes
, pb
+off
,
349 if (mapping_writably_mapped(mp
))
350 flush_dcache_page(pp
);
352 mark_page_accessed(pp
);
365 * When a file is memory mapped, we must keep the IO data synchronized
366 * between the DMU cache and the memory mapped pages. What this means:
368 * On Read: We "read" preferentially from memory mapped pages,
369 * else we default from the dmu buffer.
371 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
372 * the file is memory mapped.
375 mappedread(struct inode
*ip
, int nbytes
, uio_t
*uio
)
377 struct address_space
*mp
= ip
->i_mapping
;
379 znode_t
*zp
= ITOZ(ip
);
386 start
= uio
->uio_loffset
;
387 off
= start
& (PAGE_SIZE
-1);
388 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
389 bytes
= MIN(PAGE_SIZE
- off
, len
);
391 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
393 ASSERT(PageUptodate(pp
));
396 error
= uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
399 if (mapping_writably_mapped(mp
))
400 flush_dcache_page(pp
);
402 mark_page_accessed(pp
);
406 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
419 unsigned long zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
422 * Read bytes from specified file into supplied buffer.
424 * IN: ip - inode of file to be read from.
425 * uio - structure supplying read location, range info,
427 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
428 * O_DIRECT flag; used to bypass page cache.
429 * cr - credentials of caller.
431 * OUT: uio - updated offset and range, buffer filled.
433 * RETURN: 0 on success, error code on failure.
436 * inode - atime updated if byte count > 0
440 zfs_read(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
442 znode_t
*zp
= ITOZ(ip
);
443 zfs_sb_t
*zsb
= ITOZSB(ip
);
447 #ifdef HAVE_UIO_ZEROCOPY
449 #endif /* HAVE_UIO_ZEROCOPY */
454 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
456 return (SET_ERROR(EACCES
));
460 * Validate file offset
462 if (uio
->uio_loffset
< (offset_t
)0) {
464 return (SET_ERROR(EINVAL
));
468 * Fasttrack empty reads
470 if (uio
->uio_resid
== 0) {
476 * If we're in FRSYNC mode, sync out this znode before reading it.
478 if (ioflag
& FRSYNC
|| zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
479 zil_commit(zsb
->z_log
, zp
->z_id
);
482 * Lock the range against changes.
484 rl
= zfs_range_lock(&zp
->z_range_lock
, uio
->uio_loffset
, uio
->uio_resid
,
488 * If we are reading past end-of-file we can skip
489 * to the end; but we might still need to set atime.
491 if (uio
->uio_loffset
>= zp
->z_size
) {
496 ASSERT(uio
->uio_loffset
< zp
->z_size
);
497 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
499 #ifdef HAVE_UIO_ZEROCOPY
500 if ((uio
->uio_extflg
== UIO_XUIO
) &&
501 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
503 int blksz
= zp
->z_blksz
;
504 uint64_t offset
= uio
->uio_loffset
;
506 xuio
= (xuio_t
*)uio
;
508 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
511 ASSERT(offset
+ n
<= blksz
);
514 (void) dmu_xuio_init(xuio
, nblk
);
516 if (vn_has_cached_data(ip
)) {
518 * For simplicity, we always allocate a full buffer
519 * even if we only expect to read a portion of a block.
521 while (--nblk
>= 0) {
522 (void) dmu_xuio_add(xuio
,
523 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
528 #endif /* HAVE_UIO_ZEROCOPY */
531 nbytes
= MIN(n
, zfs_read_chunk_size
-
532 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
534 if (zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
)) {
535 error
= mappedread(ip
, nbytes
, uio
);
537 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
542 /* convert checksum errors into IO errors */
544 error
= SET_ERROR(EIO
);
551 zfs_range_unlock(rl
);
556 EXPORT_SYMBOL(zfs_read
);
559 * Write the bytes to a file.
561 * IN: ip - inode of file to be written to.
562 * uio - structure supplying write location, range info,
564 * ioflag - FAPPEND flag set if in append mode.
565 * O_DIRECT flag; used to bypass page cache.
566 * cr - credentials of caller.
568 * OUT: uio - updated offset and range.
570 * RETURN: 0 if success
571 * error code if failure
574 * ip - ctime|mtime updated if byte count > 0
579 zfs_write(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
581 znode_t
*zp
= ITOZ(ip
);
582 rlim64_t limit
= uio
->uio_limit
;
583 ssize_t start_resid
= uio
->uio_resid
;
587 zfs_sb_t
*zsb
= ZTOZSB(zp
);
592 int max_blksz
= zsb
->z_max_blksz
;
595 const iovec_t
*aiov
= NULL
;
598 const iovec_t
*iovp
= uio
->uio_iov
;
601 sa_bulk_attr_t bulk
[4];
602 uint64_t mtime
[2], ctime
[2];
603 ASSERTV(int iovcnt
= uio
->uio_iovcnt
);
606 * Fasttrack empty write
612 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
618 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
619 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
620 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
, &zp
->z_size
, 8);
621 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
625 * If immutable or not appending then return EPERM
627 if ((zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
628 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
629 (uio
->uio_loffset
< zp
->z_size
))) {
631 return (SET_ERROR(EPERM
));
637 * Validate file offset
639 woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
642 return (SET_ERROR(EINVAL
));
646 * Pre-fault the pages to ensure slow (eg NFS) pages
648 * Skip this if uio contains loaned arc_buf.
650 #ifdef HAVE_UIO_ZEROCOPY
651 if ((uio
->uio_extflg
== UIO_XUIO
) &&
652 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
653 xuio
= (xuio_t
*)uio
;
656 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
659 * If in append mode, set the io offset pointer to eof.
661 if (ioflag
& FAPPEND
) {
663 * Obtain an appending range lock to guarantee file append
664 * semantics. We reset the write offset once we have the lock.
666 rl
= zfs_range_lock(&zp
->z_range_lock
, 0, n
, RL_APPEND
);
668 if (rl
->r_len
== UINT64_MAX
) {
670 * We overlocked the file because this write will cause
671 * the file block size to increase.
672 * Note that zp_size cannot change with this lock held.
676 uio
->uio_loffset
= woff
;
679 * Note that if the file block size will change as a result of
680 * this write, then this range lock will lock the entire file
681 * so that we can re-write the block safely.
683 rl
= zfs_range_lock(&zp
->z_range_lock
, woff
, n
, RL_WRITER
);
687 zfs_range_unlock(rl
);
689 return (SET_ERROR(EFBIG
));
692 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
695 /* Will this write extend the file length? */
696 write_eof
= (woff
+ n
> zp
->z_size
);
698 end_size
= MAX(zp
->z_size
, woff
+ n
);
701 * Write the file in reasonable size chunks. Each chunk is written
702 * in a separate transaction; this keeps the intent log records small
703 * and allows us to do more fine-grained space accounting.
707 woff
= uio
->uio_loffset
;
708 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
709 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
711 dmu_return_arcbuf(abuf
);
712 error
= SET_ERROR(EDQUOT
);
716 if (xuio
&& abuf
== NULL
) {
717 ASSERT(i_iov
< iovcnt
);
718 ASSERT3U(uio
->uio_segflg
, !=, UIO_BVEC
);
720 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
721 dmu_xuio_clear(xuio
, i_iov
);
722 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
723 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
724 aiov
->iov_len
== arc_buf_size(abuf
)));
726 } else if (abuf
== NULL
&& n
>= max_blksz
&&
727 woff
>= zp
->z_size
&&
728 P2PHASE(woff
, max_blksz
) == 0 &&
729 zp
->z_blksz
== max_blksz
) {
731 * This write covers a full block. "Borrow" a buffer
732 * from the dmu so that we can fill it before we enter
733 * a transaction. This avoids the possibility of
734 * holding up the transaction if the data copy hangs
735 * up on a pagefault (e.g., from an NFS server mapping).
739 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
741 ASSERT(abuf
!= NULL
);
742 ASSERT(arc_buf_size(abuf
) == max_blksz
);
743 if ((error
= uiocopy(abuf
->b_data
, max_blksz
,
744 UIO_WRITE
, uio
, &cbytes
))) {
745 dmu_return_arcbuf(abuf
);
748 ASSERT(cbytes
== max_blksz
);
752 * Start a transaction.
754 tx
= dmu_tx_create(zsb
->z_os
);
755 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
756 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
757 zfs_sa_upgrade_txholds(tx
, zp
);
758 error
= dmu_tx_assign(tx
, TXG_WAIT
);
762 dmu_return_arcbuf(abuf
);
767 * If zfs_range_lock() over-locked we grow the blocksize
768 * and then reduce the lock range. This will only happen
769 * on the first iteration since zfs_range_reduce() will
770 * shrink down r_len to the appropriate size.
772 if (rl
->r_len
== UINT64_MAX
) {
775 if (zp
->z_blksz
> max_blksz
) {
777 * File's blocksize is already larger than the
778 * "recordsize" property. Only let it grow to
779 * the next power of 2.
781 ASSERT(!ISP2(zp
->z_blksz
));
782 new_blksz
= MIN(end_size
,
783 1 << highbit64(zp
->z_blksz
));
785 new_blksz
= MIN(end_size
, max_blksz
);
787 zfs_grow_blocksize(zp
, new_blksz
, tx
);
788 zfs_range_reduce(rl
, woff
, n
);
792 * XXX - should we really limit each write to z_max_blksz?
793 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
795 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
798 tx_bytes
= uio
->uio_resid
;
799 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
801 tx_bytes
-= uio
->uio_resid
;
804 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
806 * If this is not a full block write, but we are
807 * extending the file past EOF and this data starts
808 * block-aligned, use assign_arcbuf(). Otherwise,
809 * write via dmu_write().
811 if (tx_bytes
< max_blksz
&& (!write_eof
||
812 aiov
->iov_base
!= abuf
->b_data
)) {
814 dmu_write(zsb
->z_os
, zp
->z_id
, woff
,
815 aiov
->iov_len
, aiov
->iov_base
, tx
);
816 dmu_return_arcbuf(abuf
);
817 xuio_stat_wbuf_copied();
819 ASSERT(xuio
|| tx_bytes
== max_blksz
);
820 dmu_assign_arcbuf(sa_get_db(zp
->z_sa_hdl
),
823 ASSERT(tx_bytes
<= uio
->uio_resid
);
824 uioskip(uio
, tx_bytes
);
827 if (tx_bytes
&& zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
828 update_pages(ip
, woff
, tx_bytes
, zsb
->z_os
, zp
->z_id
);
831 * If we made no progress, we're done. If we made even
832 * partial progress, update the znode and ZIL accordingly.
835 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
836 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
843 * Clear Set-UID/Set-GID bits on successful write if not
844 * privileged and at least one of the excute bits is set.
846 * It would be nice to to this after all writes have
847 * been done, but that would still expose the ISUID/ISGID
848 * to another app after the partial write is committed.
850 * Note: we don't call zfs_fuid_map_id() here because
851 * user 0 is not an ephemeral uid.
853 mutex_enter(&zp
->z_acl_lock
);
854 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
855 (S_IXUSR
>> 6))) != 0 &&
856 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
857 secpolicy_vnode_setid_retain(cr
,
858 (zp
->z_mode
& S_ISUID
) != 0 && zp
->z_uid
== 0) != 0) {
860 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
861 newmode
= zp
->z_mode
;
862 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
),
863 (void *)&newmode
, sizeof (uint64_t), tx
);
865 mutex_exit(&zp
->z_acl_lock
);
867 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
870 * Update the file size (zp_size) if it has changed;
871 * account for possible concurrent updates.
873 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
874 (void) atomic_cas_64(&zp
->z_size
, end_size
,
879 * If we are replaying and eof is non zero then force
880 * the file size to the specified eof. Note, there's no
881 * concurrency during replay.
883 if (zsb
->z_replay
&& zsb
->z_replay_eof
!= 0)
884 zp
->z_size
= zsb
->z_replay_eof
;
886 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
888 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
,
894 ASSERT(tx_bytes
== nbytes
);
898 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
901 zfs_inode_update(zp
);
902 zfs_range_unlock(rl
);
905 * If we're in replay mode, or we made no progress, return error.
906 * Otherwise, it's at least a partial write, so it's successful.
908 if (zsb
->z_replay
|| uio
->uio_resid
== start_resid
) {
913 if (ioflag
& (FSYNC
| FDSYNC
) ||
914 zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
915 zil_commit(zilog
, zp
->z_id
);
920 EXPORT_SYMBOL(zfs_write
);
923 zfs_iput_async(struct inode
*ip
)
925 objset_t
*os
= ITOZSB(ip
)->z_os
;
927 ASSERT(atomic_read(&ip
->i_count
) > 0);
930 if (atomic_read(&ip
->i_count
) == 1)
931 taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os
)),
932 (task_func_t
*)iput
, ip
, TQ_SLEEP
);
938 zfs_get_done(zgd_t
*zgd
, int error
)
940 znode_t
*zp
= zgd
->zgd_private
;
943 dmu_buf_rele(zgd
->zgd_db
, zgd
);
945 zfs_range_unlock(zgd
->zgd_rl
);
948 * Release the vnode asynchronously as we currently have the
949 * txg stopped from syncing.
951 zfs_iput_async(ZTOI(zp
));
953 if (error
== 0 && zgd
->zgd_bp
)
954 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
956 kmem_free(zgd
, sizeof (zgd_t
));
960 static int zil_fault_io
= 0;
964 * Get data to generate a TX_WRITE intent log record.
967 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
970 objset_t
*os
= zsb
->z_os
;
972 uint64_t object
= lr
->lr_foid
;
973 uint64_t offset
= lr
->lr_offset
;
974 uint64_t size
= lr
->lr_length
;
975 blkptr_t
*bp
= &lr
->lr_blkptr
;
984 * Nothing to do if the file has been removed
986 if (zfs_zget(zsb
, object
, &zp
) != 0)
987 return (SET_ERROR(ENOENT
));
988 if (zp
->z_unlinked
) {
990 * Release the vnode asynchronously as we currently have the
991 * txg stopped from syncing.
993 zfs_iput_async(ZTOI(zp
));
994 return (SET_ERROR(ENOENT
));
997 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
998 zgd
->zgd_zilog
= zsb
->z_log
;
999 zgd
->zgd_private
= zp
;
1002 * Write records come in two flavors: immediate and indirect.
1003 * For small writes it's cheaper to store the data with the
1004 * log record (immediate); for large writes it's cheaper to
1005 * sync the data and get a pointer to it (indirect) so that
1006 * we don't have to write the data twice.
1008 if (buf
!= NULL
) { /* immediate write */
1009 zgd
->zgd_rl
= zfs_range_lock(&zp
->z_range_lock
, offset
, size
,
1011 /* test for truncation needs to be done while range locked */
1012 if (offset
>= zp
->z_size
) {
1013 error
= SET_ERROR(ENOENT
);
1015 error
= dmu_read(os
, object
, offset
, size
, buf
,
1016 DMU_READ_NO_PREFETCH
);
1018 ASSERT(error
== 0 || error
== ENOENT
);
1019 } else { /* indirect write */
1021 * Have to lock the whole block to ensure when it's
1022 * written out and it's checksum is being calculated
1023 * that no one can change the data. We need to re-check
1024 * blocksize after we get the lock in case it's changed!
1029 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
1031 zgd
->zgd_rl
= zfs_range_lock(&zp
->z_range_lock
, offset
,
1033 if (zp
->z_blksz
== size
)
1036 zfs_range_unlock(zgd
->zgd_rl
);
1038 /* test for truncation needs to be done while range locked */
1039 if (lr
->lr_offset
>= zp
->z_size
)
1040 error
= SET_ERROR(ENOENT
);
1043 error
= SET_ERROR(EIO
);
1048 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1049 DMU_READ_NO_PREFETCH
);
1052 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
1054 ASSERT(BP_IS_HOLE(bp
));
1061 ASSERT(db
->db_offset
== offset
);
1062 ASSERT(db
->db_size
== size
);
1064 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1066 ASSERT(error
|| lr
->lr_length
<= zp
->z_blksz
);
1069 * On success, we need to wait for the write I/O
1070 * initiated by dmu_sync() to complete before we can
1071 * release this dbuf. We will finish everything up
1072 * in the zfs_get_done() callback.
1077 if (error
== EALREADY
) {
1078 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
1084 zfs_get_done(zgd
, error
);
1091 zfs_access(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
1093 znode_t
*zp
= ITOZ(ip
);
1094 zfs_sb_t
*zsb
= ITOZSB(ip
);
1100 if (flag
& V_ACE_MASK
)
1101 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
1103 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
1108 EXPORT_SYMBOL(zfs_access
);
1111 * Lookup an entry in a directory, or an extended attribute directory.
1112 * If it exists, return a held inode reference for it.
1114 * IN: dip - inode of directory to search.
1115 * nm - name of entry to lookup.
1116 * flags - LOOKUP_XATTR set if looking for an attribute.
1117 * cr - credentials of caller.
1118 * direntflags - directory lookup flags
1119 * realpnp - returned pathname.
1121 * OUT: ipp - inode of located entry, NULL if not found.
1123 * RETURN: 0 on success, error code on failure.
1130 zfs_lookup(struct inode
*dip
, char *nm
, struct inode
**ipp
, int flags
,
1131 cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
1133 znode_t
*zdp
= ITOZ(dip
);
1134 zfs_sb_t
*zsb
= ITOZSB(dip
);
1138 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1140 if (!S_ISDIR(dip
->i_mode
)) {
1141 return (SET_ERROR(ENOTDIR
));
1142 } else if (zdp
->z_sa_hdl
== NULL
) {
1143 return (SET_ERROR(EIO
));
1146 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1147 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1156 vnode_t
*tvp
= dnlc_lookup(dvp
, nm
);
1159 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1164 if (tvp
== DNLC_NO_VNODE
) {
1166 return (SET_ERROR(ENOENT
));
1169 return (specvp_check(vpp
, cr
));
1172 #endif /* HAVE_DNLC */
1181 if (flags
& LOOKUP_XATTR
) {
1183 * We don't allow recursive attributes..
1184 * Maybe someday we will.
1186 if (zdp
->z_pflags
& ZFS_XATTR
) {
1188 return (SET_ERROR(EINVAL
));
1191 if ((error
= zfs_get_xattrdir(zdp
, ipp
, cr
, flags
))) {
1197 * Do we have permission to get into attribute directory?
1200 if ((error
= zfs_zaccess(ITOZ(*ipp
), ACE_EXECUTE
, 0,
1210 if (!S_ISDIR(dip
->i_mode
)) {
1212 return (SET_ERROR(ENOTDIR
));
1216 * Check accessibility of directory.
1219 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
))) {
1224 if (zsb
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1225 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1227 return (SET_ERROR(EILSEQ
));
1230 error
= zfs_dirlook(zdp
, nm
, ipp
, flags
, direntflags
, realpnp
);
1231 if ((error
== 0) && (*ipp
))
1232 zfs_inode_update(ITOZ(*ipp
));
1237 EXPORT_SYMBOL(zfs_lookup
);
1240 * Attempt to create a new entry in a directory. If the entry
1241 * already exists, truncate the file if permissible, else return
1242 * an error. Return the ip of the created or trunc'd file.
1244 * IN: dip - inode of directory to put new file entry in.
1245 * name - name of new file entry.
1246 * vap - attributes of new file.
1247 * excl - flag indicating exclusive or non-exclusive mode.
1248 * mode - mode to open file with.
1249 * cr - credentials of caller.
1250 * flag - large file flag [UNUSED].
1251 * vsecp - ACL to be set
1253 * OUT: ipp - inode of created or trunc'd entry.
1255 * RETURN: 0 on success, error code on failure.
1258 * dip - ctime|mtime updated if new entry created
1259 * ip - ctime|mtime always, atime if new
1264 zfs_create(struct inode
*dip
, char *name
, vattr_t
*vap
, int excl
,
1265 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
1267 znode_t
*zp
, *dzp
= ITOZ(dip
);
1268 zfs_sb_t
*zsb
= ITOZSB(dip
);
1276 zfs_acl_ids_t acl_ids
;
1277 boolean_t fuid_dirtied
;
1278 boolean_t have_acl
= B_FALSE
;
1279 boolean_t waited
= B_FALSE
;
1282 * If we have an ephemeral id, ACL, or XVATTR then
1283 * make sure file system is at proper version
1289 if (zsb
->z_use_fuids
== B_FALSE
&&
1290 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1291 return (SET_ERROR(EINVAL
));
1298 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
1299 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1301 return (SET_ERROR(EILSEQ
));
1304 if (vap
->va_mask
& ATTR_XVATTR
) {
1305 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1306 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1314 if (*name
== '\0') {
1316 * Null component name refers to the directory itself.
1323 /* possible igrab(zp) */
1326 if (flag
& FIGNORECASE
)
1329 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1333 zfs_acl_ids_free(&acl_ids
);
1334 if (strcmp(name
, "..") == 0)
1335 error
= SET_ERROR(EISDIR
);
1345 * Create a new file object and update the directory
1348 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
1350 zfs_acl_ids_free(&acl_ids
);
1355 * We only support the creation of regular files in
1356 * extended attribute directories.
1359 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
1361 zfs_acl_ids_free(&acl_ids
);
1362 error
= SET_ERROR(EINVAL
);
1366 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1367 cr
, vsecp
, &acl_ids
)) != 0)
1371 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1372 zfs_acl_ids_free(&acl_ids
);
1373 error
= SET_ERROR(EDQUOT
);
1377 tx
= dmu_tx_create(os
);
1379 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1380 ZFS_SA_BASE_ATTR_SIZE
);
1382 fuid_dirtied
= zsb
->z_fuid_dirty
;
1384 zfs_fuid_txhold(zsb
, tx
);
1385 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1386 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1387 if (!zsb
->z_use_sa
&&
1388 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1389 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1390 0, acl_ids
.z_aclp
->z_acl_bytes
);
1392 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1394 zfs_dirent_unlock(dl
);
1395 if (error
== ERESTART
) {
1401 zfs_acl_ids_free(&acl_ids
);
1406 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1409 zfs_fuid_sync(zsb
, tx
);
1411 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1412 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1413 if (flag
& FIGNORECASE
)
1415 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1416 vsecp
, acl_ids
.z_fuidp
, vap
);
1417 zfs_acl_ids_free(&acl_ids
);
1420 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1423 zfs_acl_ids_free(&acl_ids
);
1427 * A directory entry already exists for this name.
1430 * Can't truncate an existing file if in exclusive mode.
1433 error
= SET_ERROR(EEXIST
);
1437 * Can't open a directory for writing.
1439 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
1440 error
= SET_ERROR(EISDIR
);
1444 * Verify requested access to file.
1446 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1450 mutex_enter(&dzp
->z_lock
);
1452 mutex_exit(&dzp
->z_lock
);
1455 * Truncate regular files if requested.
1457 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
1458 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
1459 /* we can't hold any locks when calling zfs_freesp() */
1460 zfs_dirent_unlock(dl
);
1462 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1468 zfs_dirent_unlock(dl
);
1474 zfs_inode_update(dzp
);
1475 zfs_inode_update(zp
);
1479 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1480 zil_commit(zilog
, 0);
1485 EXPORT_SYMBOL(zfs_create
);
1488 * Remove an entry from a directory.
1490 * IN: dip - inode of directory to remove entry from.
1491 * name - name of entry to remove.
1492 * cr - credentials of caller.
1494 * RETURN: 0 if success
1495 * error code if failure
1499 * ip - ctime (if nlink > 0)
1502 uint64_t null_xattr
= 0;
1506 zfs_remove(struct inode
*dip
, char *name
, cred_t
*cr
)
1508 znode_t
*zp
, *dzp
= ITOZ(dip
);
1511 zfs_sb_t
*zsb
= ITOZSB(dip
);
1514 uint64_t xattr_obj_unlinked
= 0;
1520 pathname_t
*realnmp
= NULL
;
1521 #ifdef HAVE_PN_UTILS
1523 #endif /* HAVE_PN_UTILS */
1526 boolean_t waited
= B_FALSE
;
1532 #ifdef HAVE_PN_UTILS
1533 if (flags
& FIGNORECASE
) {
1538 #endif /* HAVE_PN_UTILS */
1544 * Attempt to lock directory; fail if entry doesn't exist.
1546 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1548 #ifdef HAVE_PN_UTILS
1551 #endif /* HAVE_PN_UTILS */
1558 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1563 * Need to use rmdir for removing directories.
1565 if (S_ISDIR(ip
->i_mode
)) {
1566 error
= SET_ERROR(EPERM
);
1572 dnlc_remove(dvp
, realnmp
->pn_buf
);
1574 dnlc_remove(dvp
, name
);
1575 #endif /* HAVE_DNLC */
1578 * We never delete the znode and always place it in the unlinked
1579 * set. The dentry cache will always hold the last reference and
1580 * is responsible for safely freeing the znode.
1583 tx
= dmu_tx_create(zsb
->z_os
);
1584 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1585 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1586 zfs_sa_upgrade_txholds(tx
, zp
);
1587 zfs_sa_upgrade_txholds(tx
, dzp
);
1589 /* are there any extended attributes? */
1590 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1591 &xattr_obj
, sizeof (xattr_obj
));
1592 if (error
== 0 && xattr_obj
) {
1593 error
= zfs_zget(zsb
, xattr_obj
, &xzp
);
1595 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1596 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1599 /* charge as an update -- would be nice not to charge at all */
1600 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1602 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1604 zfs_dirent_unlock(dl
);
1608 if (error
== ERESTART
) {
1614 #ifdef HAVE_PN_UTILS
1617 #endif /* HAVE_PN_UTILS */
1624 * Remove the directory entry.
1626 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1635 * Hold z_lock so that we can make sure that the ACL obj
1636 * hasn't changed. Could have been deleted due to
1639 mutex_enter(&zp
->z_lock
);
1640 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1641 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1642 mutex_exit(&zp
->z_lock
);
1643 zfs_unlinked_add(zp
, tx
);
1647 #ifdef HAVE_PN_UTILS
1648 if (flags
& FIGNORECASE
)
1650 #endif /* HAVE_PN_UTILS */
1651 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
);
1655 #ifdef HAVE_PN_UTILS
1658 #endif /* HAVE_PN_UTILS */
1660 zfs_dirent_unlock(dl
);
1661 zfs_inode_update(dzp
);
1662 zfs_inode_update(zp
);
1664 zfs_inode_update(xzp
);
1670 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1671 zil_commit(zilog
, 0);
1676 EXPORT_SYMBOL(zfs_remove
);
1679 * Create a new directory and insert it into dip using the name
1680 * provided. Return a pointer to the inserted directory.
1682 * IN: dip - inode of directory to add subdir to.
1683 * dirname - name of new directory.
1684 * vap - attributes of new directory.
1685 * cr - credentials of caller.
1686 * vsecp - ACL to be set
1688 * OUT: ipp - inode of created directory.
1690 * RETURN: 0 if success
1691 * error code if failure
1694 * dip - ctime|mtime updated
1695 * ipp - ctime|mtime|atime updated
1699 zfs_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
, struct inode
**ipp
,
1700 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1702 znode_t
*zp
, *dzp
= ITOZ(dip
);
1703 zfs_sb_t
*zsb
= ITOZSB(dip
);
1711 gid_t gid
= crgetgid(cr
);
1712 zfs_acl_ids_t acl_ids
;
1713 boolean_t fuid_dirtied
;
1714 boolean_t waited
= B_FALSE
;
1716 ASSERT(S_ISDIR(vap
->va_mode
));
1719 * If we have an ephemeral id, ACL, or XVATTR then
1720 * make sure file system is at proper version
1724 if (zsb
->z_use_fuids
== B_FALSE
&&
1725 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1726 return (SET_ERROR(EINVAL
));
1732 if (dzp
->z_pflags
& ZFS_XATTR
) {
1734 return (SET_ERROR(EINVAL
));
1737 if (zsb
->z_utf8
&& u8_validate(dirname
,
1738 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1740 return (SET_ERROR(EILSEQ
));
1742 if (flags
& FIGNORECASE
)
1745 if (vap
->va_mask
& ATTR_XVATTR
) {
1746 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1747 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1753 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1754 vsecp
, &acl_ids
)) != 0) {
1759 * First make sure the new directory doesn't exist.
1761 * Existence is checked first to make sure we don't return
1762 * EACCES instead of EEXIST which can cause some applications
1768 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1770 zfs_acl_ids_free(&acl_ids
);
1775 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
1776 zfs_acl_ids_free(&acl_ids
);
1777 zfs_dirent_unlock(dl
);
1782 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1783 zfs_acl_ids_free(&acl_ids
);
1784 zfs_dirent_unlock(dl
);
1786 return (SET_ERROR(EDQUOT
));
1790 * Add a new entry to the directory.
1792 tx
= dmu_tx_create(zsb
->z_os
);
1793 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1794 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1795 fuid_dirtied
= zsb
->z_fuid_dirty
;
1797 zfs_fuid_txhold(zsb
, tx
);
1798 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1799 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1800 acl_ids
.z_aclp
->z_acl_bytes
);
1803 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1804 ZFS_SA_BASE_ATTR_SIZE
);
1806 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1808 zfs_dirent_unlock(dl
);
1809 if (error
== ERESTART
) {
1815 zfs_acl_ids_free(&acl_ids
);
1824 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1827 zfs_fuid_sync(zsb
, tx
);
1830 * Now put new name in parent dir.
1832 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1836 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1837 if (flags
& FIGNORECASE
)
1839 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
1840 acl_ids
.z_fuidp
, vap
);
1842 zfs_acl_ids_free(&acl_ids
);
1846 zfs_dirent_unlock(dl
);
1848 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1849 zil_commit(zilog
, 0);
1851 zfs_inode_update(dzp
);
1852 zfs_inode_update(zp
);
1856 EXPORT_SYMBOL(zfs_mkdir
);
1859 * Remove a directory subdir entry. If the current working
1860 * directory is the same as the subdir to be removed, the
1863 * IN: dip - inode of directory to remove from.
1864 * name - name of directory to be removed.
1865 * cwd - inode of current working directory.
1866 * cr - credentials of caller.
1867 * flags - case flags
1869 * RETURN: 0 on success, error code on failure.
1872 * dip - ctime|mtime updated
1876 zfs_rmdir(struct inode
*dip
, char *name
, struct inode
*cwd
, cred_t
*cr
,
1879 znode_t
*dzp
= ITOZ(dip
);
1882 zfs_sb_t
*zsb
= ITOZSB(dip
);
1888 boolean_t waited
= B_FALSE
;
1894 if (flags
& FIGNORECASE
)
1900 * Attempt to lock directory; fail if entry doesn't exist.
1902 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1910 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1914 if (!S_ISDIR(ip
->i_mode
)) {
1915 error
= SET_ERROR(ENOTDIR
);
1920 error
= SET_ERROR(EINVAL
);
1925 * Grab a lock on the directory to make sure that noone is
1926 * trying to add (or lookup) entries while we are removing it.
1928 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1931 * Grab a lock on the parent pointer to make sure we play well
1932 * with the treewalk and directory rename code.
1934 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1936 tx
= dmu_tx_create(zsb
->z_os
);
1937 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1938 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1939 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1940 zfs_sa_upgrade_txholds(tx
, zp
);
1941 zfs_sa_upgrade_txholds(tx
, dzp
);
1942 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1944 rw_exit(&zp
->z_parent_lock
);
1945 rw_exit(&zp
->z_name_lock
);
1946 zfs_dirent_unlock(dl
);
1948 if (error
== ERESTART
) {
1959 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
1962 uint64_t txtype
= TX_RMDIR
;
1963 if (flags
& FIGNORECASE
)
1965 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
);
1970 rw_exit(&zp
->z_parent_lock
);
1971 rw_exit(&zp
->z_name_lock
);
1973 zfs_dirent_unlock(dl
);
1975 zfs_inode_update(dzp
);
1976 zfs_inode_update(zp
);
1979 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1980 zil_commit(zilog
, 0);
1985 EXPORT_SYMBOL(zfs_rmdir
);
1988 * Read as many directory entries as will fit into the provided
1989 * dirent buffer from the given directory cursor position.
1991 * IN: ip - inode of directory to read.
1992 * dirent - buffer for directory entries.
1994 * OUT: dirent - filler buffer of directory entries.
1996 * RETURN: 0 if success
1997 * error code if failure
2000 * ip - atime updated
2002 * Note that the low 4 bits of the cookie returned by zap is always zero.
2003 * This allows us to use the low range for "special" directory entries:
2004 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2005 * we use the offset 2 for the '.zfs' directory.
2009 zfs_readdir(struct inode
*ip
, struct dir_context
*ctx
, cred_t
*cr
)
2011 znode_t
*zp
= ITOZ(ip
);
2012 zfs_sb_t
*zsb
= ITOZSB(ip
);
2015 zap_attribute_t zap
;
2021 uint64_t offset
; /* must be unsigned; checks for < 1 */
2026 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
2027 &parent
, sizeof (parent
))) != 0)
2031 * Quit if directory has been removed (posix)
2039 prefetch
= zp
->z_zn_prefetch
;
2042 * Initialize the iterator cursor.
2046 * Start iteration from the beginning of the directory.
2048 zap_cursor_init(&zc
, os
, zp
->z_id
);
2051 * The offset is a serialized cursor.
2053 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
2057 * Transform to file-system independent format
2062 * Special case `.', `..', and `.zfs'.
2065 (void) strcpy(zap
.za_name
, ".");
2066 zap
.za_normalization_conflict
= 0;
2069 } else if (offset
== 1) {
2070 (void) strcpy(zap
.za_name
, "..");
2071 zap
.za_normalization_conflict
= 0;
2074 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
2075 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
2076 zap
.za_normalization_conflict
= 0;
2077 objnum
= ZFSCTL_INO_ROOT
;
2083 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
2084 if (error
== ENOENT
)
2091 * Allow multiple entries provided the first entry is
2092 * the object id. Non-zpl consumers may safely make
2093 * use of the additional space.
2095 * XXX: This should be a feature flag for compatibility
2097 if (zap
.za_integer_length
!= 8 ||
2098 zap
.za_num_integers
== 0) {
2099 cmn_err(CE_WARN
, "zap_readdir: bad directory "
2100 "entry, obj = %lld, offset = %lld, "
2101 "length = %d, num = %lld\n",
2102 (u_longlong_t
)zp
->z_id
,
2103 (u_longlong_t
)offset
,
2104 zap
.za_integer_length
,
2105 (u_longlong_t
)zap
.za_num_integers
);
2106 error
= SET_ERROR(ENXIO
);
2110 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
2111 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
2114 done
= !dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
2119 /* Prefetch znode */
2121 dmu_prefetch(os
, objnum
, 0, 0);
2125 * Move to the next entry, fill in the previous offset.
2127 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2128 zap_cursor_advance(&zc
);
2129 offset
= zap_cursor_serialize(&zc
);
2135 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2138 zap_cursor_fini(&zc
);
2139 if (error
== ENOENT
)
2146 EXPORT_SYMBOL(zfs_readdir
);
2148 ulong_t zfs_fsync_sync_cnt
= 4;
2151 zfs_fsync(struct inode
*ip
, int syncflag
, cred_t
*cr
)
2153 znode_t
*zp
= ITOZ(ip
);
2154 zfs_sb_t
*zsb
= ITOZSB(ip
);
2156 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2158 if (zsb
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2161 zil_commit(zsb
->z_log
, zp
->z_id
);
2164 tsd_set(zfs_fsyncer_key
, NULL
);
2168 EXPORT_SYMBOL(zfs_fsync
);
2172 * Get the requested file attributes and place them in the provided
2175 * IN: ip - inode of file.
2176 * vap - va_mask identifies requested attributes.
2177 * If ATTR_XVATTR set, then optional attrs are requested
2178 * flags - ATTR_NOACLCHECK (CIFS server context)
2179 * cr - credentials of caller.
2181 * OUT: vap - attribute values.
2183 * RETURN: 0 (always succeeds)
2187 zfs_getattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2189 znode_t
*zp
= ITOZ(ip
);
2190 zfs_sb_t
*zsb
= ITOZSB(ip
);
2193 uint64_t atime
[2], mtime
[2], ctime
[2];
2194 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2195 xoptattr_t
*xoap
= NULL
;
2196 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2197 sa_bulk_attr_t bulk
[3];
2203 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2205 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
, &atime
, 16);
2206 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
2207 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
2209 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2215 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2216 * Also, if we are the owner don't bother, since owner should
2217 * always be allowed to read basic attributes of file.
2219 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2220 (vap
->va_uid
!= crgetuid(cr
))) {
2221 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2229 * Return all attributes. It's cheaper to provide the answer
2230 * than to determine whether we were asked the question.
2233 mutex_enter(&zp
->z_lock
);
2234 vap
->va_type
= vn_mode_to_vtype(zp
->z_mode
);
2235 vap
->va_mode
= zp
->z_mode
;
2236 vap
->va_fsid
= ZTOI(zp
)->i_sb
->s_dev
;
2237 vap
->va_nodeid
= zp
->z_id
;
2238 if ((zp
->z_id
== zsb
->z_root
) && zfs_show_ctldir(zp
))
2239 links
= zp
->z_links
+ 1;
2241 links
= zp
->z_links
;
2242 vap
->va_nlink
= MIN(links
, ZFS_LINK_MAX
);
2243 vap
->va_size
= i_size_read(ip
);
2244 vap
->va_rdev
= ip
->i_rdev
;
2245 vap
->va_seq
= ip
->i_generation
;
2248 * Add in any requested optional attributes and the create time.
2249 * Also set the corresponding bits in the returned attribute bitmap.
2251 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zsb
->z_use_fuids
) {
2252 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2254 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2255 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2258 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2259 xoap
->xoa_readonly
=
2260 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2261 XVA_SET_RTN(xvap
, XAT_READONLY
);
2264 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2266 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2267 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2270 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2272 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2273 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2276 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2277 xoap
->xoa_nounlink
=
2278 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2279 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2282 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2283 xoap
->xoa_immutable
=
2284 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2285 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2288 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2289 xoap
->xoa_appendonly
=
2290 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2291 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2294 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2296 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2297 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2300 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2302 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2303 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2306 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2307 xoap
->xoa_av_quarantined
=
2308 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2309 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2312 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2313 xoap
->xoa_av_modified
=
2314 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2315 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2318 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2319 S_ISREG(ip
->i_mode
)) {
2320 zfs_sa_get_scanstamp(zp
, xvap
);
2323 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2326 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zsb
),
2327 times
, sizeof (times
));
2328 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2329 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2332 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2333 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2334 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2336 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2337 xoap
->xoa_generation
= zp
->z_gen
;
2338 XVA_SET_RTN(xvap
, XAT_GEN
);
2341 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2343 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2344 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2347 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2349 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2350 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2354 ZFS_TIME_DECODE(&vap
->va_atime
, atime
);
2355 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2356 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2358 mutex_exit(&zp
->z_lock
);
2360 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2362 if (zp
->z_blksz
== 0) {
2364 * Block size hasn't been set; suggest maximal I/O transfers.
2366 vap
->va_blksize
= zsb
->z_max_blksz
;
2372 EXPORT_SYMBOL(zfs_getattr
);
2375 * Get the basic file attributes and place them in the provided kstat
2376 * structure. The inode is assumed to be the authoritative source
2377 * for most of the attributes. However, the znode currently has the
2378 * authoritative atime, blksize, and block count.
2380 * IN: ip - inode of file.
2382 * OUT: sp - kstat values.
2384 * RETURN: 0 (always succeeds)
2388 zfs_getattr_fast(struct inode
*ip
, struct kstat
*sp
)
2390 znode_t
*zp
= ITOZ(ip
);
2391 zfs_sb_t
*zsb
= ITOZSB(ip
);
2393 u_longlong_t nblocks
;
2398 mutex_enter(&zp
->z_lock
);
2400 generic_fillattr(ip
, sp
);
2402 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
2403 sp
->blksize
= blksize
;
2404 sp
->blocks
= nblocks
;
2406 if (unlikely(zp
->z_blksz
== 0)) {
2408 * Block size hasn't been set; suggest maximal I/O transfers.
2410 sp
->blksize
= zsb
->z_max_blksz
;
2413 mutex_exit(&zp
->z_lock
);
2416 * Required to prevent NFS client from detecting different inode
2417 * numbers of snapshot root dentry before and after snapshot mount.
2419 if (zsb
->z_issnap
) {
2420 if (ip
->i_sb
->s_root
->d_inode
== ip
)
2421 sp
->ino
= ZFSCTL_INO_SNAPDIRS
-
2422 dmu_objset_id(zsb
->z_os
);
2429 EXPORT_SYMBOL(zfs_getattr_fast
);
2432 * Set the file attributes to the values contained in the
2435 * IN: ip - inode of file to be modified.
2436 * vap - new attribute values.
2437 * If ATTR_XVATTR set, then optional attrs are being set
2438 * flags - ATTR_UTIME set if non-default time values provided.
2439 * - ATTR_NOACLCHECK (CIFS context only).
2440 * cr - credentials of caller.
2442 * RETURN: 0 if success
2443 * error code if failure
2446 * ip - ctime updated, mtime updated if size changed.
2450 zfs_setattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2452 znode_t
*zp
= ITOZ(ip
);
2453 zfs_sb_t
*zsb
= ITOZSB(ip
);
2457 xvattr_t
*tmpxvattr
;
2458 uint_t mask
= vap
->va_mask
;
2459 uint_t saved_mask
= 0;
2462 uint64_t new_uid
, new_gid
;
2464 uint64_t mtime
[2], ctime
[2], atime
[2];
2466 int need_policy
= FALSE
;
2468 zfs_fuid_info_t
*fuidp
= NULL
;
2469 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2472 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2473 boolean_t fuid_dirtied
= B_FALSE
;
2474 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
2475 int count
= 0, xattr_count
= 0;
2486 * Make sure that if we have ephemeral uid/gid or xvattr specified
2487 * that file system is at proper version level
2490 if (zsb
->z_use_fuids
== B_FALSE
&&
2491 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2492 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2493 (mask
& ATTR_XVATTR
))) {
2495 return (SET_ERROR(EINVAL
));
2498 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
2500 return (SET_ERROR(EISDIR
));
2503 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
2505 return (SET_ERROR(EINVAL
));
2509 * If this is an xvattr_t, then get a pointer to the structure of
2510 * optional attributes. If this is NULL, then we have a vattr_t.
2512 xoap
= xva_getxoptattr(xvap
);
2514 tmpxvattr
= kmem_alloc(sizeof (xvattr_t
), KM_SLEEP
);
2515 xva_init(tmpxvattr
);
2517 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * 7, KM_SLEEP
);
2518 xattr_bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * 7, KM_SLEEP
);
2521 * Immutable files can only alter immutable bit and atime
2523 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2524 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
2525 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2530 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2536 * Verify timestamps doesn't overflow 32 bits.
2537 * ZFS can handle large timestamps, but 32bit syscalls can't
2538 * handle times greater than 2039. This check should be removed
2539 * once large timestamps are fully supported.
2541 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
2542 if (((mask
& ATTR_ATIME
) &&
2543 TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2544 ((mask
& ATTR_MTIME
) &&
2545 TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2555 /* Can this be moved to before the top label? */
2556 if (zfs_is_readonly(zsb
)) {
2562 * First validate permissions
2565 if (mask
& ATTR_SIZE
) {
2566 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2571 * XXX - Note, we are not providing any open
2572 * mode flags here (like FNDELAY), so we may
2573 * block if there are locks present... this
2574 * should be addressed in openat().
2576 /* XXX - would it be OK to generate a log record here? */
2577 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2582 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
2583 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2584 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2585 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2586 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2587 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2588 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2589 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2590 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2594 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2595 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
2600 * NOTE: even if a new mode is being set,
2601 * we may clear S_ISUID/S_ISGID bits.
2604 if (!(mask
& ATTR_MODE
))
2605 vap
->va_mode
= zp
->z_mode
;
2608 * Take ownership or chgrp to group we are a member of
2611 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
2612 take_group
= (mask
& ATTR_GID
) &&
2613 zfs_groupmember(zsb
, vap
->va_gid
, cr
);
2616 * If both ATTR_UID and ATTR_GID are set then take_owner and
2617 * take_group must both be set in order to allow taking
2620 * Otherwise, send the check through secpolicy_vnode_setattr()
2624 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
2625 take_owner
&& take_group
) ||
2626 ((idmask
== ATTR_UID
) && take_owner
) ||
2627 ((idmask
== ATTR_GID
) && take_group
)) {
2628 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2629 skipaclchk
, cr
) == 0) {
2631 * Remove setuid/setgid for non-privileged users
2633 (void) secpolicy_setid_clear(vap
, cr
);
2634 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
2643 mutex_enter(&zp
->z_lock
);
2644 oldva
.va_mode
= zp
->z_mode
;
2645 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2646 if (mask
& ATTR_XVATTR
) {
2648 * Update xvattr mask to include only those attributes
2649 * that are actually changing.
2651 * the bits will be restored prior to actually setting
2652 * the attributes so the caller thinks they were set.
2654 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2655 if (xoap
->xoa_appendonly
!=
2656 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2659 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2660 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
2664 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2665 if (xoap
->xoa_nounlink
!=
2666 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2669 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2670 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
2674 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2675 if (xoap
->xoa_immutable
!=
2676 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2679 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2680 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
2684 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2685 if (xoap
->xoa_nodump
!=
2686 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2689 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2690 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
2694 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2695 if (xoap
->xoa_av_modified
!=
2696 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2699 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2700 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
2704 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2705 if ((!S_ISREG(ip
->i_mode
) &&
2706 xoap
->xoa_av_quarantined
) ||
2707 xoap
->xoa_av_quarantined
!=
2708 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2711 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2712 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
2716 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2717 mutex_exit(&zp
->z_lock
);
2722 if (need_policy
== FALSE
&&
2723 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2724 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2729 mutex_exit(&zp
->z_lock
);
2731 if (mask
& ATTR_MODE
) {
2732 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2733 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
2738 trim_mask
|= ATTR_MODE
;
2746 * If trim_mask is set then take ownership
2747 * has been granted or write_acl is present and user
2748 * has the ability to modify mode. In that case remove
2749 * UID|GID and or MODE from mask so that
2750 * secpolicy_vnode_setattr() doesn't revoke it.
2754 saved_mask
= vap
->va_mask
;
2755 vap
->va_mask
&= ~trim_mask
;
2757 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
2758 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2763 vap
->va_mask
|= saved_mask
;
2767 * secpolicy_vnode_setattr, or take ownership may have
2770 mask
= vap
->va_mask
;
2772 if ((mask
& (ATTR_UID
| ATTR_GID
))) {
2773 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
2774 &xattr_obj
, sizeof (xattr_obj
));
2776 if (err
== 0 && xattr_obj
) {
2777 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
2781 if (mask
& ATTR_UID
) {
2782 new_uid
= zfs_fuid_create(zsb
,
2783 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2784 if (new_uid
!= zp
->z_uid
&&
2785 zfs_fuid_overquota(zsb
, B_FALSE
, new_uid
)) {
2793 if (mask
& ATTR_GID
) {
2794 new_gid
= zfs_fuid_create(zsb
, (uint64_t)vap
->va_gid
,
2795 cr
, ZFS_GROUP
, &fuidp
);
2796 if (new_gid
!= zp
->z_gid
&&
2797 zfs_fuid_overquota(zsb
, B_TRUE
, new_gid
)) {
2805 tx
= dmu_tx_create(zsb
->z_os
);
2807 if (mask
& ATTR_MODE
) {
2808 uint64_t pmode
= zp
->z_mode
;
2810 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2812 zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
);
2814 mutex_enter(&zp
->z_lock
);
2815 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2817 * Are we upgrading ACL from old V0 format
2820 if (zsb
->z_version
>= ZPL_VERSION_FUID
&&
2821 zfs_znode_acl_version(zp
) ==
2822 ZFS_ACL_VERSION_INITIAL
) {
2823 dmu_tx_hold_free(tx
, acl_obj
, 0,
2825 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2826 0, aclp
->z_acl_bytes
);
2828 dmu_tx_hold_write(tx
, acl_obj
, 0,
2831 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2832 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2833 0, aclp
->z_acl_bytes
);
2835 mutex_exit(&zp
->z_lock
);
2836 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2838 if ((mask
& ATTR_XVATTR
) &&
2839 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2840 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2842 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2846 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2849 fuid_dirtied
= zsb
->z_fuid_dirty
;
2851 zfs_fuid_txhold(zsb
, tx
);
2853 zfs_sa_upgrade_txholds(tx
, zp
);
2855 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2861 * Set each attribute requested.
2862 * We group settings according to the locks they need to acquire.
2864 * Note: you cannot set ctime directly, although it will be
2865 * updated as a side-effect of calling this function.
2869 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2870 mutex_enter(&zp
->z_acl_lock
);
2871 mutex_enter(&zp
->z_lock
);
2873 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
2874 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2877 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2878 mutex_enter(&attrzp
->z_acl_lock
);
2879 mutex_enter(&attrzp
->z_lock
);
2880 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2881 SA_ZPL_FLAGS(zsb
), NULL
, &attrzp
->z_pflags
,
2882 sizeof (attrzp
->z_pflags
));
2885 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2887 if (mask
& ATTR_UID
) {
2888 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
2889 &new_uid
, sizeof (new_uid
));
2890 zp
->z_uid
= new_uid
;
2892 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2893 SA_ZPL_UID(zsb
), NULL
, &new_uid
,
2895 attrzp
->z_uid
= new_uid
;
2899 if (mask
& ATTR_GID
) {
2900 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
),
2901 NULL
, &new_gid
, sizeof (new_gid
));
2902 zp
->z_gid
= new_gid
;
2904 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2905 SA_ZPL_GID(zsb
), NULL
, &new_gid
,
2907 attrzp
->z_gid
= new_gid
;
2910 if (!(mask
& ATTR_MODE
)) {
2911 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
),
2912 NULL
, &new_mode
, sizeof (new_mode
));
2913 new_mode
= zp
->z_mode
;
2915 err
= zfs_acl_chown_setattr(zp
);
2918 err
= zfs_acl_chown_setattr(attrzp
);
2923 if (mask
& ATTR_MODE
) {
2924 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
2925 &new_mode
, sizeof (new_mode
));
2926 zp
->z_mode
= new_mode
;
2927 ASSERT3P(aclp
, !=, NULL
);
2928 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2930 if (zp
->z_acl_cached
)
2931 zfs_acl_free(zp
->z_acl_cached
);
2932 zp
->z_acl_cached
= aclp
;
2937 if ((mask
& ATTR_ATIME
) || zp
->z_atime_dirty
) {
2938 zp
->z_atime_dirty
= 0;
2939 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
2940 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
2941 &atime
, sizeof (atime
));
2944 if (mask
& ATTR_MTIME
) {
2945 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2946 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
,
2947 mtime
, sizeof (mtime
));
2950 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2951 if (mask
& ATTR_SIZE
&& !(mask
& ATTR_MTIME
)) {
2952 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
),
2953 NULL
, mtime
, sizeof (mtime
));
2954 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2955 &ctime
, sizeof (ctime
));
2956 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
2957 } else if (mask
!= 0) {
2958 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2959 &ctime
, sizeof (ctime
));
2960 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
);
2962 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2963 SA_ZPL_CTIME(zsb
), NULL
,
2964 &ctime
, sizeof (ctime
));
2965 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
2970 * Do this after setting timestamps to prevent timestamp
2971 * update from toggling bit
2974 if (xoap
&& (mask
& ATTR_XVATTR
)) {
2977 * restore trimmed off masks
2978 * so that return masks can be set for caller.
2981 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
2982 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2984 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
2985 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2987 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
2988 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2990 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
2991 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2993 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
2994 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2996 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
2997 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
3000 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
3001 ASSERT(S_ISREG(ip
->i_mode
));
3003 zfs_xvattr_set(zp
, xvap
, tx
);
3007 zfs_fuid_sync(zsb
, tx
);
3010 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
3012 mutex_exit(&zp
->z_lock
);
3013 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3014 mutex_exit(&zp
->z_acl_lock
);
3017 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3018 mutex_exit(&attrzp
->z_acl_lock
);
3019 mutex_exit(&attrzp
->z_lock
);
3022 if (err
== 0 && attrzp
) {
3023 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3034 zfs_fuid_info_free(fuidp
);
3040 if (err
== ERESTART
)
3043 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3045 zfs_inode_update(zp
);
3049 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3050 zil_commit(zilog
, 0);
3053 kmem_free(xattr_bulk
, sizeof (sa_bulk_attr_t
) * 7);
3054 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * 7);
3055 kmem_free(tmpxvattr
, sizeof (xvattr_t
));
3059 EXPORT_SYMBOL(zfs_setattr
);
3061 typedef struct zfs_zlock
{
3062 krwlock_t
*zl_rwlock
; /* lock we acquired */
3063 znode_t
*zl_znode
; /* znode we held */
3064 struct zfs_zlock
*zl_next
; /* next in list */
3068 * Drop locks and release vnodes that were held by zfs_rename_lock().
3071 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3075 while ((zl
= *zlpp
) != NULL
) {
3076 if (zl
->zl_znode
!= NULL
)
3077 iput(ZTOI(zl
->zl_znode
));
3078 rw_exit(zl
->zl_rwlock
);
3079 *zlpp
= zl
->zl_next
;
3080 kmem_free(zl
, sizeof (*zl
));
3085 * Search back through the directory tree, using the ".." entries.
3086 * Lock each directory in the chain to prevent concurrent renames.
3087 * Fail any attempt to move a directory into one of its own descendants.
3088 * XXX - z_parent_lock can overlap with map or grow locks
3091 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3095 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
3096 uint64_t oidp
= zp
->z_id
;
3097 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3098 krw_t rw
= RW_WRITER
;
3101 * First pass write-locks szp and compares to zp->z_id.
3102 * Later passes read-lock zp and compare to zp->z_parent.
3105 if (!rw_tryenter(rwlp
, rw
)) {
3107 * Another thread is renaming in this path.
3108 * Note that if we are a WRITER, we don't have any
3109 * parent_locks held yet.
3111 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3113 * Drop our locks and restart
3115 zfs_rename_unlock(&zl
);
3119 rwlp
= &szp
->z_parent_lock
;
3124 * Wait for other thread to drop its locks
3130 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3131 zl
->zl_rwlock
= rwlp
;
3132 zl
->zl_znode
= NULL
;
3133 zl
->zl_next
= *zlpp
;
3136 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3137 return (SET_ERROR(EINVAL
));
3139 if (oidp
== rootid
) /* We've hit the top */
3142 if (rw
== RW_READER
) { /* i.e. not the first pass */
3143 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
3148 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
3149 &oidp
, sizeof (oidp
));
3150 rwlp
= &zp
->z_parent_lock
;
3153 } while (zp
->z_id
!= sdzp
->z_id
);
3159 * Move an entry from the provided source directory to the target
3160 * directory. Change the entry name as indicated.
3162 * IN: sdip - Source directory containing the "old entry".
3163 * snm - Old entry name.
3164 * tdip - Target directory to contain the "new entry".
3165 * tnm - New entry name.
3166 * cr - credentials of caller.
3167 * flags - case flags
3169 * RETURN: 0 on success, error code on failure.
3172 * sdip,tdip - ctime|mtime updated
3176 zfs_rename(struct inode
*sdip
, char *snm
, struct inode
*tdip
, char *tnm
,
3177 cred_t
*cr
, int flags
)
3179 znode_t
*tdzp
, *szp
, *tzp
;
3180 znode_t
*sdzp
= ITOZ(sdip
);
3181 zfs_sb_t
*zsb
= ITOZSB(sdip
);
3183 zfs_dirlock_t
*sdl
, *tdl
;
3186 int cmp
, serr
, terr
;
3189 boolean_t waited
= B_FALSE
;
3192 ZFS_VERIFY_ZP(sdzp
);
3195 if (tdip
->i_sb
!= sdip
->i_sb
|| zfsctl_is_node(tdip
)) {
3197 return (SET_ERROR(EXDEV
));
3201 ZFS_VERIFY_ZP(tdzp
);
3202 if (zsb
->z_utf8
&& u8_validate(tnm
,
3203 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3205 return (SET_ERROR(EILSEQ
));
3208 if (flags
& FIGNORECASE
)
3217 * This is to prevent the creation of links into attribute space
3218 * by renaming a linked file into/outof an attribute directory.
3219 * See the comment in zfs_link() for why this is considered bad.
3221 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3223 return (SET_ERROR(EINVAL
));
3227 * Lock source and target directory entries. To prevent deadlock,
3228 * a lock ordering must be defined. We lock the directory with
3229 * the smallest object id first, or if it's a tie, the one with
3230 * the lexically first name.
3232 if (sdzp
->z_id
< tdzp
->z_id
) {
3234 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3238 * First compare the two name arguments without
3239 * considering any case folding.
3241 int nofold
= (zsb
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3243 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3244 ASSERT(error
== 0 || !zsb
->z_utf8
);
3247 * POSIX: "If the old argument and the new argument
3248 * both refer to links to the same existing file,
3249 * the rename() function shall return successfully
3250 * and perform no other action."
3256 * If the file system is case-folding, then we may
3257 * have some more checking to do. A case-folding file
3258 * system is either supporting mixed case sensitivity
3259 * access or is completely case-insensitive. Note
3260 * that the file system is always case preserving.
3262 * In mixed sensitivity mode case sensitive behavior
3263 * is the default. FIGNORECASE must be used to
3264 * explicitly request case insensitive behavior.
3266 * If the source and target names provided differ only
3267 * by case (e.g., a request to rename 'tim' to 'Tim'),
3268 * we will treat this as a special case in the
3269 * case-insensitive mode: as long as the source name
3270 * is an exact match, we will allow this to proceed as
3271 * a name-change request.
3273 if ((zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
3274 (zsb
->z_case
== ZFS_CASE_MIXED
&&
3275 flags
& FIGNORECASE
)) &&
3276 u8_strcmp(snm
, tnm
, 0, zsb
->z_norm
, U8_UNICODE_LATEST
,
3279 * case preserving rename request, require exact
3288 * If the source and destination directories are the same, we should
3289 * grab the z_name_lock of that directory only once.
3293 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3297 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3298 ZEXISTS
| zflg
, NULL
, NULL
);
3299 terr
= zfs_dirent_lock(&tdl
,
3300 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3302 terr
= zfs_dirent_lock(&tdl
,
3303 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3304 serr
= zfs_dirent_lock(&sdl
,
3305 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3311 * Source entry invalid or not there.
3314 zfs_dirent_unlock(tdl
);
3320 rw_exit(&sdzp
->z_name_lock
);
3322 if (strcmp(snm
, "..") == 0)
3328 zfs_dirent_unlock(sdl
);
3332 rw_exit(&sdzp
->z_name_lock
);
3334 if (strcmp(tnm
, "..") == 0)
3341 * Must have write access at the source to remove the old entry
3342 * and write access at the target to create the new entry.
3343 * Note that if target and source are the same, this can be
3344 * done in a single check.
3347 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
3350 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3352 * Check to make sure rename is valid.
3353 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3355 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
3360 * Does target exist?
3364 * Source and target must be the same type.
3366 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3367 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3368 error
= SET_ERROR(ENOTDIR
);
3372 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3373 error
= SET_ERROR(EISDIR
);
3378 * POSIX dictates that when the source and target
3379 * entries refer to the same file object, rename
3380 * must do nothing and exit without error.
3382 if (szp
->z_id
== tzp
->z_id
) {
3388 tx
= dmu_tx_create(zsb
->z_os
);
3389 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3390 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3391 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3392 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3394 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3395 zfs_sa_upgrade_txholds(tx
, tdzp
);
3398 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3399 zfs_sa_upgrade_txholds(tx
, tzp
);
3402 zfs_sa_upgrade_txholds(tx
, szp
);
3403 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
3404 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3407 zfs_rename_unlock(&zl
);
3408 zfs_dirent_unlock(sdl
);
3409 zfs_dirent_unlock(tdl
);
3412 rw_exit(&sdzp
->z_name_lock
);
3417 if (error
== ERESTART
) {
3428 if (tzp
) /* Attempt to remove the existing target */
3429 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3432 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3434 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3436 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zsb
),
3437 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3440 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3442 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3443 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
3444 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3447 * At this point, we have successfully created
3448 * the target name, but have failed to remove
3449 * the source name. Since the create was done
3450 * with the ZRENAMING flag, there are
3451 * complications; for one, the link count is
3452 * wrong. The easiest way to deal with this
3453 * is to remove the newly created target, and
3454 * return the original error. This must
3455 * succeed; fortunately, it is very unlikely to
3456 * fail, since we just created it.
3458 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3459 ZRENAMING
, NULL
), ==, 0);
3467 zfs_rename_unlock(&zl
);
3469 zfs_dirent_unlock(sdl
);
3470 zfs_dirent_unlock(tdl
);
3472 zfs_inode_update(sdzp
);
3474 rw_exit(&sdzp
->z_name_lock
);
3477 zfs_inode_update(tdzp
);
3479 zfs_inode_update(szp
);
3482 zfs_inode_update(tzp
);
3486 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3487 zil_commit(zilog
, 0);
3492 EXPORT_SYMBOL(zfs_rename
);
3495 * Insert the indicated symbolic reference entry into the directory.
3497 * IN: dip - Directory to contain new symbolic link.
3498 * link - Name for new symlink entry.
3499 * vap - Attributes of new entry.
3500 * target - Target path of new symlink.
3502 * cr - credentials of caller.
3503 * flags - case flags
3505 * RETURN: 0 on success, error code on failure.
3508 * dip - ctime|mtime updated
3512 zfs_symlink(struct inode
*dip
, char *name
, vattr_t
*vap
, char *link
,
3513 struct inode
**ipp
, cred_t
*cr
, int flags
)
3515 znode_t
*zp
, *dzp
= ITOZ(dip
);
3518 zfs_sb_t
*zsb
= ITOZSB(dip
);
3520 uint64_t len
= strlen(link
);
3523 zfs_acl_ids_t acl_ids
;
3524 boolean_t fuid_dirtied
;
3525 uint64_t txtype
= TX_SYMLINK
;
3526 boolean_t waited
= B_FALSE
;
3528 ASSERT(S_ISLNK(vap
->va_mode
));
3534 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
3535 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3537 return (SET_ERROR(EILSEQ
));
3539 if (flags
& FIGNORECASE
)
3542 if (len
> MAXPATHLEN
) {
3544 return (SET_ERROR(ENAMETOOLONG
));
3547 if ((error
= zfs_acl_ids_create(dzp
, 0,
3548 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3556 * Attempt to lock directory; fail if entry already exists.
3558 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3560 zfs_acl_ids_free(&acl_ids
);
3565 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3566 zfs_acl_ids_free(&acl_ids
);
3567 zfs_dirent_unlock(dl
);
3572 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
3573 zfs_acl_ids_free(&acl_ids
);
3574 zfs_dirent_unlock(dl
);
3576 return (SET_ERROR(EDQUOT
));
3578 tx
= dmu_tx_create(zsb
->z_os
);
3579 fuid_dirtied
= zsb
->z_fuid_dirty
;
3580 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3581 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3582 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3583 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3584 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3585 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3586 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3587 acl_ids
.z_aclp
->z_acl_bytes
);
3590 zfs_fuid_txhold(zsb
, tx
);
3591 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3593 zfs_dirent_unlock(dl
);
3594 if (error
== ERESTART
) {
3600 zfs_acl_ids_free(&acl_ids
);
3607 * Create a new object for the symlink.
3608 * for version 4 ZPL datsets the symlink will be an SA attribute
3610 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3613 zfs_fuid_sync(zsb
, tx
);
3615 mutex_enter(&zp
->z_lock
);
3617 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zsb
),
3620 zfs_sa_symlink(zp
, link
, len
, tx
);
3621 mutex_exit(&zp
->z_lock
);
3624 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
3625 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3627 * Insert the new object into the directory.
3629 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3631 if (flags
& FIGNORECASE
)
3633 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3635 zfs_inode_update(dzp
);
3636 zfs_inode_update(zp
);
3638 zfs_acl_ids_free(&acl_ids
);
3642 zfs_dirent_unlock(dl
);
3646 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3647 zil_commit(zilog
, 0);
3652 EXPORT_SYMBOL(zfs_symlink
);
3655 * Return, in the buffer contained in the provided uio structure,
3656 * the symbolic path referred to by ip.
3658 * IN: ip - inode of symbolic link
3659 * uio - structure to contain the link path.
3660 * cr - credentials of caller.
3662 * RETURN: 0 if success
3663 * error code if failure
3666 * ip - atime updated
3670 zfs_readlink(struct inode
*ip
, uio_t
*uio
, cred_t
*cr
)
3672 znode_t
*zp
= ITOZ(ip
);
3673 zfs_sb_t
*zsb
= ITOZSB(ip
);
3679 mutex_enter(&zp
->z_lock
);
3681 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3682 SA_ZPL_SYMLINK(zsb
), uio
);
3684 error
= zfs_sa_readlink(zp
, uio
);
3685 mutex_exit(&zp
->z_lock
);
3690 EXPORT_SYMBOL(zfs_readlink
);
3693 * Insert a new entry into directory tdip referencing sip.
3695 * IN: tdip - Directory to contain new entry.
3696 * sip - inode of new entry.
3697 * name - name of new entry.
3698 * cr - credentials of caller.
3700 * RETURN: 0 if success
3701 * error code if failure
3704 * tdip - ctime|mtime updated
3705 * sip - ctime updated
3709 zfs_link(struct inode
*tdip
, struct inode
*sip
, char *name
, cred_t
*cr
)
3711 znode_t
*dzp
= ITOZ(tdip
);
3713 zfs_sb_t
*zsb
= ITOZSB(tdip
);
3721 boolean_t waited
= B_FALSE
;
3723 ASSERT(S_ISDIR(tdip
->i_mode
));
3730 * POSIX dictates that we return EPERM here.
3731 * Better choices include ENOTSUP or EISDIR.
3733 if (S_ISDIR(sip
->i_mode
)) {
3735 return (SET_ERROR(EPERM
));
3738 if (sip
->i_sb
!= tdip
->i_sb
|| zfsctl_is_node(sip
)) {
3740 return (SET_ERROR(EXDEV
));
3746 /* Prevent links to .zfs/shares files */
3748 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
3749 &parent
, sizeof (uint64_t))) != 0) {
3753 if (parent
== zsb
->z_shares_dir
) {
3755 return (SET_ERROR(EPERM
));
3758 if (zsb
->z_utf8
&& u8_validate(name
,
3759 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3761 return (SET_ERROR(EILSEQ
));
3763 #ifdef HAVE_PN_UTILS
3764 if (flags
& FIGNORECASE
)
3766 #endif /* HAVE_PN_UTILS */
3769 * We do not support links between attributes and non-attributes
3770 * because of the potential security risk of creating links
3771 * into "normal" file space in order to circumvent restrictions
3772 * imposed in attribute space.
3774 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
3776 return (SET_ERROR(EINVAL
));
3779 owner
= zfs_fuid_map_id(zsb
, szp
->z_uid
, cr
, ZFS_OWNER
);
3780 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
3782 return (SET_ERROR(EPERM
));
3785 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3792 * Attempt to lock directory; fail if entry already exists.
3794 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
3800 tx
= dmu_tx_create(zsb
->z_os
);
3801 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3802 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3803 zfs_sa_upgrade_txholds(tx
, szp
);
3804 zfs_sa_upgrade_txholds(tx
, dzp
);
3805 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3807 zfs_dirent_unlock(dl
);
3808 if (error
== ERESTART
) {
3819 error
= zfs_link_create(dl
, szp
, tx
, 0);
3822 uint64_t txtype
= TX_LINK
;
3823 #ifdef HAVE_PN_UTILS
3824 if (flags
& FIGNORECASE
)
3826 #endif /* HAVE_PN_UTILS */
3827 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
3832 zfs_dirent_unlock(dl
);
3834 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3835 zil_commit(zilog
, 0);
3837 zfs_inode_update(dzp
);
3838 zfs_inode_update(szp
);
3842 EXPORT_SYMBOL(zfs_link
);
3845 zfs_putpage_commit_cb(void *arg
)
3847 struct page
*pp
= arg
;
3850 end_page_writeback(pp
);
3854 * Push a page out to disk, once the page is on stable storage the
3855 * registered commit callback will be run as notification of completion.
3857 * IN: ip - page mapped for inode.
3858 * pp - page to push (page is locked)
3859 * wbc - writeback control data
3861 * RETURN: 0 if success
3862 * error code if failure
3865 * ip - ctime|mtime updated
3869 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
3871 znode_t
*zp
= ITOZ(ip
);
3872 zfs_sb_t
*zsb
= ITOZSB(ip
);
3880 uint64_t mtime
[2], ctime
[2];
3881 sa_bulk_attr_t bulk
[3];
3883 struct address_space
*mapping
;
3888 ASSERT(PageLocked(pp
));
3890 pgoff
= page_offset(pp
); /* Page byte-offset in file */
3891 offset
= i_size_read(ip
); /* File length in bytes */
3892 pglen
= MIN(PAGE_SIZE
, /* Page length in bytes */
3893 P2ROUNDUP(offset
, PAGE_SIZE
)-pgoff
);
3895 /* Page is beyond end of file */
3896 if (pgoff
>= offset
) {
3902 /* Truncate page length to end of file */
3903 if (pgoff
+ pglen
> offset
)
3904 pglen
= offset
- pgoff
;
3908 * FIXME: Allow mmap writes past its quota. The correct fix
3909 * is to register a page_mkwrite() handler to count the page
3910 * against its quota when it is about to be dirtied.
3912 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
3913 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
3919 * The ordering here is critical and must adhere to the following
3920 * rules in order to avoid deadlocking in either zfs_read() or
3921 * zfs_free_range() due to a lock inversion.
3923 * 1) The page must be unlocked prior to acquiring the range lock.
3924 * This is critical because zfs_read() calls find_lock_page()
3925 * which may block on the page lock while holding the range lock.
3927 * 2) Before setting or clearing write back on a page the range lock
3928 * must be held in order to prevent a lock inversion with the
3929 * zfs_free_range() function.
3931 * This presents a problem because upon entering this function the
3932 * page lock is already held. To safely acquire the range lock the
3933 * page lock must be dropped. This creates a window where another
3934 * process could truncate, invalidate, dirty, or write out the page.
3936 * Therefore, after successfully reacquiring the range and page locks
3937 * the current page state is checked. In the common case everything
3938 * will be as is expected and it can be written out. However, if
3939 * the page state has changed it must be handled accordingly.
3941 mapping
= pp
->mapping
;
3942 redirty_page_for_writepage(wbc
, pp
);
3945 rl
= zfs_range_lock(&zp
->z_range_lock
, pgoff
, pglen
, RL_WRITER
);
3948 /* Page mapping changed or it was no longer dirty, we're done */
3949 if (unlikely((mapping
!= pp
->mapping
) || !PageDirty(pp
))) {
3951 zfs_range_unlock(rl
);
3956 /* Another process started write block if required */
3957 if (PageWriteback(pp
)) {
3959 zfs_range_unlock(rl
);
3961 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
3962 wait_on_page_writeback(pp
);
3968 /* Clear the dirty flag the required locks are held */
3969 if (!clear_page_dirty_for_io(pp
)) {
3971 zfs_range_unlock(rl
);
3977 * Counterpart for redirty_page_for_writepage() above. This page
3978 * was in fact not skipped and should not be counted as if it were.
3980 wbc
->pages_skipped
--;
3981 set_page_writeback(pp
);
3984 tx
= dmu_tx_create(zsb
->z_os
);
3985 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
3986 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3987 zfs_sa_upgrade_txholds(tx
, zp
);
3989 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3991 if (err
== ERESTART
)
3995 __set_page_dirty_nobuffers(pp
);
3997 end_page_writeback(pp
);
3998 zfs_range_unlock(rl
);
4004 ASSERT3U(pglen
, <=, PAGE_SIZE
);
4005 dmu_write(zsb
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
4008 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
4009 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
4010 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zsb
), NULL
, &zp
->z_pflags
, 8);
4012 /* Preserve the mtime and ctime provided by the inode */
4013 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4014 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4015 zp
->z_atime_dirty
= 0;
4018 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4020 zfs_log_write(zsb
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
4021 zfs_putpage_commit_cb
, pp
);
4024 zfs_range_unlock(rl
);
4026 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
4028 * Note that this is rarely called under writepages(), because
4029 * writepages() normally handles the entire commit for
4030 * performance reasons.
4032 if (zsb
->z_log
!= NULL
)
4033 zil_commit(zsb
->z_log
, zp
->z_id
);
4041 * Update the system attributes when the inode has been dirtied. For the
4042 * moment we only update the mode, atime, mtime, and ctime.
4045 zfs_dirty_inode(struct inode
*ip
, int flags
)
4047 znode_t
*zp
= ITOZ(ip
);
4048 zfs_sb_t
*zsb
= ITOZSB(ip
);
4050 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
4051 sa_bulk_attr_t bulk
[4];
4055 if (zfs_is_readonly(zsb
) || dmu_objset_is_snapshot(zsb
->z_os
))
4063 * This is the lazytime semantic indroduced in Linux 4.0
4064 * This flag will only be called from update_time when lazytime is set.
4065 * (Note, I_DIRTY_SYNC will also set if not lazytime)
4066 * Fortunately mtime and ctime are managed within ZFS itself, so we
4067 * only need to dirty atime.
4069 if (flags
== I_DIRTY_TIME
) {
4070 zp
->z_atime_dirty
= 1;
4075 tx
= dmu_tx_create(zsb
->z_os
);
4077 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4078 zfs_sa_upgrade_txholds(tx
, zp
);
4080 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4086 mutex_enter(&zp
->z_lock
);
4087 zp
->z_atime_dirty
= 0;
4089 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zsb
), NULL
, &mode
, 8);
4090 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zsb
), NULL
, &atime
, 16);
4091 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
4092 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
4094 /* Preserve the mode, mtime and ctime provided by the inode */
4095 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4096 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4097 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4102 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4103 mutex_exit(&zp
->z_lock
);
4110 EXPORT_SYMBOL(zfs_dirty_inode
);
4114 zfs_inactive(struct inode
*ip
)
4116 znode_t
*zp
= ITOZ(ip
);
4117 zfs_sb_t
*zsb
= ITOZSB(ip
);
4120 int need_unlock
= 0;
4122 /* Only read lock if we haven't already write locked, e.g. rollback */
4123 if (!RW_WRITE_HELD(&zsb
->z_teardown_inactive_lock
)) {
4125 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_READER
);
4127 if (zp
->z_sa_hdl
== NULL
) {
4129 rw_exit(&zsb
->z_teardown_inactive_lock
);
4133 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4134 dmu_tx_t
*tx
= dmu_tx_create(zsb
->z_os
);
4136 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4137 zfs_sa_upgrade_txholds(tx
, zp
);
4138 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4142 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4143 mutex_enter(&zp
->z_lock
);
4144 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
),
4145 (void *)&atime
, sizeof (atime
), tx
);
4146 zp
->z_atime_dirty
= 0;
4147 mutex_exit(&zp
->z_lock
);
4154 rw_exit(&zsb
->z_teardown_inactive_lock
);
4156 EXPORT_SYMBOL(zfs_inactive
);
4159 * Bounds-check the seek operation.
4161 * IN: ip - inode seeking within
4162 * ooff - old file offset
4163 * noffp - pointer to new file offset
4164 * ct - caller context
4166 * RETURN: 0 if success
4167 * EINVAL if new offset invalid
4171 zfs_seek(struct inode
*ip
, offset_t ooff
, offset_t
*noffp
)
4173 if (S_ISDIR(ip
->i_mode
))
4175 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4177 EXPORT_SYMBOL(zfs_seek
);
4180 * Fill pages with data from the disk.
4183 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4185 znode_t
*zp
= ITOZ(ip
);
4186 zfs_sb_t
*zsb
= ITOZSB(ip
);
4188 struct page
*cur_pp
;
4189 u_offset_t io_off
, total
;
4196 io_len
= nr_pages
<< PAGE_SHIFT
;
4197 i_size
= i_size_read(ip
);
4198 io_off
= page_offset(pl
[0]);
4200 if (io_off
+ io_len
> i_size
)
4201 io_len
= i_size
- io_off
;
4204 * Iterate over list of pages and read each page individually.
4207 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4210 cur_pp
= pl
[page_idx
++];
4212 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4216 /* convert checksum errors into IO errors */
4218 err
= SET_ERROR(EIO
);
4227 * Uses zfs_fillpage to read data from the file and fill the pages.
4229 * IN: ip - inode of file to get data from.
4230 * pl - list of pages to read
4231 * nr_pages - number of pages to read
4233 * RETURN: 0 on success, error code on failure.
4236 * vp - atime updated
4240 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4242 znode_t
*zp
= ITOZ(ip
);
4243 zfs_sb_t
*zsb
= ITOZSB(ip
);
4252 err
= zfs_fillpage(ip
, pl
, nr_pages
);
4257 EXPORT_SYMBOL(zfs_getpage
);
4260 * Check ZFS specific permissions to memory map a section of a file.
4262 * IN: ip - inode of the file to mmap
4264 * addrp - start address in memory region
4265 * len - length of memory region
4266 * vm_flags- address flags
4268 * RETURN: 0 if success
4269 * error code if failure
4273 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
4274 unsigned long vm_flags
)
4276 znode_t
*zp
= ITOZ(ip
);
4277 zfs_sb_t
*zsb
= ITOZSB(ip
);
4282 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
4283 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4285 return (SET_ERROR(EPERM
));
4288 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
4289 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4291 return (SET_ERROR(EACCES
));
4294 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4296 return (SET_ERROR(ENXIO
));
4302 EXPORT_SYMBOL(zfs_map
);
4305 * convoff - converts the given data (start, whence) to the
4309 convoff(struct inode
*ip
, flock64_t
*lckdat
, int whence
, offset_t offset
)
4314 if ((lckdat
->l_whence
== 2) || (whence
== 2)) {
4315 if ((error
= zfs_getattr(ip
, &vap
, 0, CRED()) != 0))
4319 switch (lckdat
->l_whence
) {
4321 lckdat
->l_start
+= offset
;
4324 lckdat
->l_start
+= vap
.va_size
;
4329 return (SET_ERROR(EINVAL
));
4332 if (lckdat
->l_start
< 0)
4333 return (SET_ERROR(EINVAL
));
4337 lckdat
->l_start
-= offset
;
4340 lckdat
->l_start
-= vap
.va_size
;
4345 return (SET_ERROR(EINVAL
));
4348 lckdat
->l_whence
= (short)whence
;
4353 * Free or allocate space in a file. Currently, this function only
4354 * supports the `F_FREESP' command. However, this command is somewhat
4355 * misnamed, as its functionality includes the ability to allocate as
4356 * well as free space.
4358 * IN: ip - inode of file to free data in.
4359 * cmd - action to take (only F_FREESP supported).
4360 * bfp - section of file to free/alloc.
4361 * flag - current file open mode flags.
4362 * offset - current file offset.
4363 * cr - credentials of caller [UNUSED].
4365 * RETURN: 0 on success, error code on failure.
4368 * ip - ctime|mtime updated
4372 zfs_space(struct inode
*ip
, int cmd
, flock64_t
*bfp
, int flag
,
4373 offset_t offset
, cred_t
*cr
)
4375 znode_t
*zp
= ITOZ(ip
);
4376 zfs_sb_t
*zsb
= ITOZSB(ip
);
4383 if (cmd
!= F_FREESP
) {
4385 return (SET_ERROR(EINVAL
));
4388 if ((error
= convoff(ip
, bfp
, 0, offset
))) {
4393 if (bfp
->l_len
< 0) {
4395 return (SET_ERROR(EINVAL
));
4399 * Permissions aren't checked on Solaris because on this OS
4400 * zfs_space() can only be called with an opened file handle.
4401 * On Linux we can get here through truncate_range() which
4402 * operates directly on inodes, so we need to check access rights.
4404 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
4410 len
= bfp
->l_len
; /* 0 means from off to end of file */
4412 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4417 EXPORT_SYMBOL(zfs_space
);
4421 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4423 znode_t
*zp
= ITOZ(ip
);
4424 zfs_sb_t
*zsb
= ITOZSB(ip
);
4427 uint64_t object
= zp
->z_id
;
4434 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
),
4435 &gen64
, sizeof (uint64_t))) != 0) {
4440 gen
= (uint32_t)gen64
;
4442 size
= (zsb
->z_parent
!= zsb
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4443 if (fidp
->fid_len
< size
) {
4444 fidp
->fid_len
= size
;
4446 return (SET_ERROR(ENOSPC
));
4449 zfid
= (zfid_short_t
*)fidp
;
4451 zfid
->zf_len
= size
;
4453 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4454 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4456 /* Must have a non-zero generation number to distinguish from .zfs */
4459 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4460 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4462 if (size
== LONG_FID_LEN
) {
4463 uint64_t objsetid
= dmu_objset_id(zsb
->z_os
);
4466 zlfid
= (zfid_long_t
*)fidp
;
4468 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4469 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4471 /* XXX - this should be the generation number for the objset */
4472 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4473 zlfid
->zf_setgen
[i
] = 0;
4479 EXPORT_SYMBOL(zfs_fid
);
4483 zfs_getsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4485 znode_t
*zp
= ITOZ(ip
);
4486 zfs_sb_t
*zsb
= ITOZSB(ip
);
4488 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4492 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
4497 EXPORT_SYMBOL(zfs_getsecattr
);
4501 zfs_setsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4503 znode_t
*zp
= ITOZ(ip
);
4504 zfs_sb_t
*zsb
= ITOZSB(ip
);
4506 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4507 zilog_t
*zilog
= zsb
->z_log
;
4512 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
4514 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4515 zil_commit(zilog
, 0);
4520 EXPORT_SYMBOL(zfs_setsecattr
);
4522 #ifdef HAVE_UIO_ZEROCOPY
4524 * Tunable, both must be a power of 2.
4526 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4527 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4528 * an arcbuf for a partial block read
4530 int zcr_blksz_min
= (1 << 10); /* 1K */
4531 int zcr_blksz_max
= (1 << 17); /* 128K */
4535 zfs_reqzcbuf(struct inode
*ip
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
)
4537 znode_t
*zp
= ITOZ(ip
);
4538 zfs_sb_t
*zsb
= ITOZSB(ip
);
4539 int max_blksz
= zsb
->z_max_blksz
;
4540 uio_t
*uio
= &xuio
->xu_uio
;
4541 ssize_t size
= uio
->uio_resid
;
4542 offset_t offset
= uio
->uio_loffset
;
4547 int preamble
, postamble
;
4549 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
4550 return (SET_ERROR(EINVAL
));
4557 * Loan out an arc_buf for write if write size is bigger than
4558 * max_blksz, and the file's block size is also max_blksz.
4561 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
4563 return (SET_ERROR(EINVAL
));
4566 * Caller requests buffers for write before knowing where the
4567 * write offset might be (e.g. NFS TCP write).
4572 preamble
= P2PHASE(offset
, blksz
);
4574 preamble
= blksz
- preamble
;
4579 postamble
= P2PHASE(size
, blksz
);
4582 fullblk
= size
/ blksz
;
4583 (void) dmu_xuio_init(xuio
,
4584 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
4587 * Have to fix iov base/len for partial buffers. They
4588 * currently represent full arc_buf's.
4591 /* data begins in the middle of the arc_buf */
4592 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4595 (void) dmu_xuio_add(xuio
, abuf
,
4596 blksz
- preamble
, preamble
);
4599 for (i
= 0; i
< fullblk
; i
++) {
4600 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4603 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
4607 /* data ends in the middle of the arc_buf */
4608 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4611 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
4616 * Loan out an arc_buf for read if the read size is larger than
4617 * the current file block size. Block alignment is not
4618 * considered. Partial arc_buf will be loaned out for read.
4620 blksz
= zp
->z_blksz
;
4621 if (blksz
< zcr_blksz_min
)
4622 blksz
= zcr_blksz_min
;
4623 if (blksz
> zcr_blksz_max
)
4624 blksz
= zcr_blksz_max
;
4625 /* avoid potential complexity of dealing with it */
4626 if (blksz
> max_blksz
) {
4628 return (SET_ERROR(EINVAL
));
4631 maxsize
= zp
->z_size
- uio
->uio_loffset
;
4637 return (SET_ERROR(EINVAL
));
4642 return (SET_ERROR(EINVAL
));
4645 uio
->uio_extflg
= UIO_XUIO
;
4646 XUIO_XUZC_RW(xuio
) = ioflag
;
4653 zfs_retzcbuf(struct inode
*ip
, xuio_t
*xuio
, cred_t
*cr
)
4657 int ioflag
= XUIO_XUZC_RW(xuio
);
4659 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
4661 i
= dmu_xuio_cnt(xuio
);
4663 abuf
= dmu_xuio_arcbuf(xuio
, i
);
4665 * if abuf == NULL, it must be a write buffer
4666 * that has been returned in zfs_write().
4669 dmu_return_arcbuf(abuf
);
4670 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
4673 dmu_xuio_fini(xuio
);
4676 #endif /* HAVE_UIO_ZEROCOPY */
4678 #if defined(_KERNEL) && defined(HAVE_SPL)
4679 module_param(zfs_read_chunk_size
, long, 0644);
4680 MODULE_PARM_DESC(zfs_read_chunk_size
, "Bytes to read per chunk");