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_CACHE_SIZE
-1);
336 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
337 nbytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
339 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_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
);
356 page_cache_release(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_CACHE_SIZE
-1);
388 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
389 bytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
391 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_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
);
404 page_cache_release(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
, uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
487 * If we are reading past end-of-file we can skip
488 * to the end; but we might still need to set atime.
490 if (uio
->uio_loffset
>= zp
->z_size
) {
495 ASSERT(uio
->uio_loffset
< zp
->z_size
);
496 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
498 #ifdef HAVE_UIO_ZEROCOPY
499 if ((uio
->uio_extflg
== UIO_XUIO
) &&
500 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
502 int blksz
= zp
->z_blksz
;
503 uint64_t offset
= uio
->uio_loffset
;
505 xuio
= (xuio_t
*)uio
;
507 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
510 ASSERT(offset
+ n
<= blksz
);
513 (void) dmu_xuio_init(xuio
, nblk
);
515 if (vn_has_cached_data(ip
)) {
517 * For simplicity, we always allocate a full buffer
518 * even if we only expect to read a portion of a block.
520 while (--nblk
>= 0) {
521 (void) dmu_xuio_add(xuio
,
522 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
527 #endif /* HAVE_UIO_ZEROCOPY */
530 nbytes
= MIN(n
, zfs_read_chunk_size
-
531 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
533 if (zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
)) {
534 error
= mappedread(ip
, nbytes
, uio
);
536 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
541 /* convert checksum errors into IO errors */
543 error
= SET_ERROR(EIO
);
550 zfs_range_unlock(rl
);
552 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
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
, 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
, 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
,
871 * Update the file size (zp_size) if it has changed;
872 * account for possible concurrent updates.
874 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
875 (void) atomic_cas_64(&zp
->z_size
, end_size
,
880 * If we are replaying and eof is non zero then force
881 * the file size to the specified eof. Note, there's no
882 * concurrency during replay.
884 if (zsb
->z_replay
&& zsb
->z_replay_eof
!= 0)
885 zp
->z_size
= zsb
->z_replay_eof
;
887 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
889 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
,
895 ASSERT(tx_bytes
== nbytes
);
899 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
902 zfs_inode_update(zp
);
903 zfs_range_unlock(rl
);
906 * If we're in replay mode, or we made no progress, return error.
907 * Otherwise, it's at least a partial write, so it's successful.
909 if (zsb
->z_replay
|| uio
->uio_resid
== start_resid
) {
914 if (ioflag
& (FSYNC
| FDSYNC
) ||
915 zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
916 zil_commit(zilog
, zp
->z_id
);
921 EXPORT_SYMBOL(zfs_write
);
924 zfs_iput_async(struct inode
*ip
)
926 objset_t
*os
= ITOZSB(ip
)->z_os
;
928 ASSERT(atomic_read(&ip
->i_count
) > 0);
931 if (atomic_read(&ip
->i_count
) == 1)
932 taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os
)),
933 (task_func_t
*)iput
, ip
, TQ_SLEEP
);
939 zfs_get_done(zgd_t
*zgd
, int error
)
941 znode_t
*zp
= zgd
->zgd_private
;
944 dmu_buf_rele(zgd
->zgd_db
, zgd
);
946 zfs_range_unlock(zgd
->zgd_rl
);
949 * Release the vnode asynchronously as we currently have the
950 * txg stopped from syncing.
952 zfs_iput_async(ZTOI(zp
));
954 if (error
== 0 && zgd
->zgd_bp
)
955 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
957 kmem_free(zgd
, sizeof (zgd_t
));
961 static int zil_fault_io
= 0;
965 * Get data to generate a TX_WRITE intent log record.
968 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
971 objset_t
*os
= zsb
->z_os
;
973 uint64_t object
= lr
->lr_foid
;
974 uint64_t offset
= lr
->lr_offset
;
975 uint64_t size
= lr
->lr_length
;
976 blkptr_t
*bp
= &lr
->lr_blkptr
;
985 * Nothing to do if the file has been removed
987 if (zfs_zget(zsb
, object
, &zp
) != 0)
988 return (SET_ERROR(ENOENT
));
989 if (zp
->z_unlinked
) {
991 * Release the vnode asynchronously as we currently have the
992 * txg stopped from syncing.
994 zfs_iput_async(ZTOI(zp
));
995 return (SET_ERROR(ENOENT
));
998 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
999 zgd
->zgd_zilog
= zsb
->z_log
;
1000 zgd
->zgd_private
= zp
;
1003 * Write records come in two flavors: immediate and indirect.
1004 * For small writes it's cheaper to store the data with the
1005 * log record (immediate); for large writes it's cheaper to
1006 * sync the data and get a pointer to it (indirect) so that
1007 * we don't have to write the data twice.
1009 if (buf
!= NULL
) { /* immediate write */
1010 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
, RL_READER
);
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
, offset
, size
,
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, 0,
2122 ZIO_PRIORITY_SYNC_READ
);
2126 * Move to the next entry, fill in the previous offset.
2128 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2129 zap_cursor_advance(&zc
);
2130 offset
= zap_cursor_serialize(&zc
);
2136 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2139 zap_cursor_fini(&zc
);
2140 if (error
== ENOENT
)
2143 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
2150 EXPORT_SYMBOL(zfs_readdir
);
2152 ulong_t zfs_fsync_sync_cnt
= 4;
2155 zfs_fsync(struct inode
*ip
, int syncflag
, cred_t
*cr
)
2157 znode_t
*zp
= ITOZ(ip
);
2158 zfs_sb_t
*zsb
= ITOZSB(ip
);
2160 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2162 if (zsb
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2165 zil_commit(zsb
->z_log
, zp
->z_id
);
2168 tsd_set(zfs_fsyncer_key
, NULL
);
2172 EXPORT_SYMBOL(zfs_fsync
);
2176 * Get the requested file attributes and place them in the provided
2179 * IN: ip - inode of file.
2180 * vap - va_mask identifies requested attributes.
2181 * If ATTR_XVATTR set, then optional attrs are requested
2182 * flags - ATTR_NOACLCHECK (CIFS server context)
2183 * cr - credentials of caller.
2185 * OUT: vap - attribute values.
2187 * RETURN: 0 (always succeeds)
2191 zfs_getattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2193 znode_t
*zp
= ITOZ(ip
);
2194 zfs_sb_t
*zsb
= ITOZSB(ip
);
2197 uint64_t mtime
[2], ctime
[2];
2198 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2199 xoptattr_t
*xoap
= NULL
;
2200 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2201 sa_bulk_attr_t bulk
[2];
2207 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2209 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
2210 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
2212 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2218 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2219 * Also, if we are the owner don't bother, since owner should
2220 * always be allowed to read basic attributes of file.
2222 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2223 (vap
->va_uid
!= crgetuid(cr
))) {
2224 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2232 * Return all attributes. It's cheaper to provide the answer
2233 * than to determine whether we were asked the question.
2236 mutex_enter(&zp
->z_lock
);
2237 vap
->va_type
= vn_mode_to_vtype(zp
->z_mode
);
2238 vap
->va_mode
= zp
->z_mode
;
2239 vap
->va_fsid
= ZTOI(zp
)->i_sb
->s_dev
;
2240 vap
->va_nodeid
= zp
->z_id
;
2241 if ((zp
->z_id
== zsb
->z_root
) && zfs_show_ctldir(zp
))
2242 links
= zp
->z_links
+ 1;
2244 links
= zp
->z_links
;
2245 vap
->va_nlink
= MIN(links
, ZFS_LINK_MAX
);
2246 vap
->va_size
= i_size_read(ip
);
2247 vap
->va_rdev
= ip
->i_rdev
;
2248 vap
->va_seq
= ip
->i_generation
;
2251 * Add in any requested optional attributes and the create time.
2252 * Also set the corresponding bits in the returned attribute bitmap.
2254 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zsb
->z_use_fuids
) {
2255 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2257 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2258 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2261 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2262 xoap
->xoa_readonly
=
2263 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2264 XVA_SET_RTN(xvap
, XAT_READONLY
);
2267 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2269 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2270 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2273 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2275 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2276 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2279 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2280 xoap
->xoa_nounlink
=
2281 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2282 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2285 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2286 xoap
->xoa_immutable
=
2287 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2288 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2291 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2292 xoap
->xoa_appendonly
=
2293 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2294 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2297 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2299 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2300 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2303 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2305 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2306 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2309 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2310 xoap
->xoa_av_quarantined
=
2311 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2312 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2315 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2316 xoap
->xoa_av_modified
=
2317 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2318 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2321 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2322 S_ISREG(ip
->i_mode
)) {
2323 zfs_sa_get_scanstamp(zp
, xvap
);
2326 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2329 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zsb
),
2330 times
, sizeof (times
));
2331 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2332 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2335 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2336 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2337 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2339 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2340 xoap
->xoa_generation
= zp
->z_gen
;
2341 XVA_SET_RTN(xvap
, XAT_GEN
);
2344 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2346 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2347 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2350 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2352 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2353 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2357 ZFS_TIME_DECODE(&vap
->va_atime
, zp
->z_atime
);
2358 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2359 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2361 mutex_exit(&zp
->z_lock
);
2363 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2365 if (zp
->z_blksz
== 0) {
2367 * Block size hasn't been set; suggest maximal I/O transfers.
2369 vap
->va_blksize
= zsb
->z_max_blksz
;
2375 EXPORT_SYMBOL(zfs_getattr
);
2378 * Get the basic file attributes and place them in the provided kstat
2379 * structure. The inode is assumed to be the authoritative source
2380 * for most of the attributes. However, the znode currently has the
2381 * authoritative atime, blksize, and block count.
2383 * IN: ip - inode of file.
2385 * OUT: sp - kstat values.
2387 * RETURN: 0 (always succeeds)
2391 zfs_getattr_fast(struct inode
*ip
, struct kstat
*sp
)
2393 znode_t
*zp
= ITOZ(ip
);
2394 zfs_sb_t
*zsb
= ITOZSB(ip
);
2396 u_longlong_t nblocks
;
2401 mutex_enter(&zp
->z_lock
);
2403 generic_fillattr(ip
, sp
);
2404 ZFS_TIME_DECODE(&sp
->atime
, zp
->z_atime
);
2406 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
2407 sp
->blksize
= blksize
;
2408 sp
->blocks
= nblocks
;
2410 if (unlikely(zp
->z_blksz
== 0)) {
2412 * Block size hasn't been set; suggest maximal I/O transfers.
2414 sp
->blksize
= zsb
->z_max_blksz
;
2417 mutex_exit(&zp
->z_lock
);
2420 * Required to prevent NFS client from detecting different inode
2421 * numbers of snapshot root dentry before and after snapshot mount.
2423 if (zsb
->z_issnap
) {
2424 if (ip
->i_sb
->s_root
->d_inode
== ip
)
2425 sp
->ino
= ZFSCTL_INO_SNAPDIRS
-
2426 dmu_objset_id(zsb
->z_os
);
2433 EXPORT_SYMBOL(zfs_getattr_fast
);
2436 * Set the file attributes to the values contained in the
2439 * IN: ip - inode of file to be modified.
2440 * vap - new attribute values.
2441 * If ATTR_XVATTR set, then optional attrs are being set
2442 * flags - ATTR_UTIME set if non-default time values provided.
2443 * - ATTR_NOACLCHECK (CIFS context only).
2444 * cr - credentials of caller.
2446 * RETURN: 0 if success
2447 * error code if failure
2450 * ip - ctime updated, mtime updated if size changed.
2454 zfs_setattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2456 znode_t
*zp
= ITOZ(ip
);
2457 zfs_sb_t
*zsb
= ITOZSB(ip
);
2461 xvattr_t
*tmpxvattr
;
2462 uint_t mask
= vap
->va_mask
;
2463 uint_t saved_mask
= 0;
2466 uint64_t new_uid
, new_gid
;
2468 uint64_t mtime
[2], ctime
[2];
2470 int need_policy
= FALSE
;
2472 zfs_fuid_info_t
*fuidp
= NULL
;
2473 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2476 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2477 boolean_t fuid_dirtied
= B_FALSE
;
2478 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
2479 int count
= 0, xattr_count
= 0;
2490 * Make sure that if we have ephemeral uid/gid or xvattr specified
2491 * that file system is at proper version level
2494 if (zsb
->z_use_fuids
== B_FALSE
&&
2495 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2496 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2497 (mask
& ATTR_XVATTR
))) {
2499 return (SET_ERROR(EINVAL
));
2502 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
2504 return (SET_ERROR(EISDIR
));
2507 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
2509 return (SET_ERROR(EINVAL
));
2513 * If this is an xvattr_t, then get a pointer to the structure of
2514 * optional attributes. If this is NULL, then we have a vattr_t.
2516 xoap
= xva_getxoptattr(xvap
);
2518 tmpxvattr
= kmem_alloc(sizeof (xvattr_t
), KM_SLEEP
);
2519 xva_init(tmpxvattr
);
2521 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * 7, KM_SLEEP
);
2522 xattr_bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * 7, KM_SLEEP
);
2525 * Immutable files can only alter immutable bit and atime
2527 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2528 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
2529 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2534 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2540 * Verify timestamps doesn't overflow 32 bits.
2541 * ZFS can handle large timestamps, but 32bit syscalls can't
2542 * handle times greater than 2039. This check should be removed
2543 * once large timestamps are fully supported.
2545 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
2546 if (((mask
& ATTR_ATIME
) &&
2547 TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2548 ((mask
& ATTR_MTIME
) &&
2549 TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2559 /* Can this be moved to before the top label? */
2560 if (zfs_is_readonly(zsb
)) {
2566 * First validate permissions
2569 if (mask
& ATTR_SIZE
) {
2570 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2575 * XXX - Note, we are not providing any open
2576 * mode flags here (like FNDELAY), so we may
2577 * block if there are locks present... this
2578 * should be addressed in openat().
2580 /* XXX - would it be OK to generate a log record here? */
2581 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2586 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
2587 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2588 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2589 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2590 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2591 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2592 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2593 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2594 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2598 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2599 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
2604 * NOTE: even if a new mode is being set,
2605 * we may clear S_ISUID/S_ISGID bits.
2608 if (!(mask
& ATTR_MODE
))
2609 vap
->va_mode
= zp
->z_mode
;
2612 * Take ownership or chgrp to group we are a member of
2615 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
2616 take_group
= (mask
& ATTR_GID
) &&
2617 zfs_groupmember(zsb
, vap
->va_gid
, cr
);
2620 * If both ATTR_UID and ATTR_GID are set then take_owner and
2621 * take_group must both be set in order to allow taking
2624 * Otherwise, send the check through secpolicy_vnode_setattr()
2628 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
2629 take_owner
&& take_group
) ||
2630 ((idmask
== ATTR_UID
) && take_owner
) ||
2631 ((idmask
== ATTR_GID
) && take_group
)) {
2632 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2633 skipaclchk
, cr
) == 0) {
2635 * Remove setuid/setgid for non-privileged users
2637 (void) secpolicy_setid_clear(vap
, cr
);
2638 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
2647 mutex_enter(&zp
->z_lock
);
2648 oldva
.va_mode
= zp
->z_mode
;
2649 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2650 if (mask
& ATTR_XVATTR
) {
2652 * Update xvattr mask to include only those attributes
2653 * that are actually changing.
2655 * the bits will be restored prior to actually setting
2656 * the attributes so the caller thinks they were set.
2658 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2659 if (xoap
->xoa_appendonly
!=
2660 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2663 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2664 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
2668 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2669 if (xoap
->xoa_nounlink
!=
2670 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2673 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2674 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
2678 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2679 if (xoap
->xoa_immutable
!=
2680 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2683 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2684 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
2688 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2689 if (xoap
->xoa_nodump
!=
2690 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2693 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2694 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
2698 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2699 if (xoap
->xoa_av_modified
!=
2700 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2703 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2704 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
2708 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2709 if ((!S_ISREG(ip
->i_mode
) &&
2710 xoap
->xoa_av_quarantined
) ||
2711 xoap
->xoa_av_quarantined
!=
2712 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2715 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2716 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
2720 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2721 mutex_exit(&zp
->z_lock
);
2726 if (need_policy
== FALSE
&&
2727 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2728 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2733 mutex_exit(&zp
->z_lock
);
2735 if (mask
& ATTR_MODE
) {
2736 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2737 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
2742 trim_mask
|= ATTR_MODE
;
2750 * If trim_mask is set then take ownership
2751 * has been granted or write_acl is present and user
2752 * has the ability to modify mode. In that case remove
2753 * UID|GID and or MODE from mask so that
2754 * secpolicy_vnode_setattr() doesn't revoke it.
2758 saved_mask
= vap
->va_mask
;
2759 vap
->va_mask
&= ~trim_mask
;
2761 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
2762 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2767 vap
->va_mask
|= saved_mask
;
2771 * secpolicy_vnode_setattr, or take ownership may have
2774 mask
= vap
->va_mask
;
2776 if ((mask
& (ATTR_UID
| ATTR_GID
))) {
2777 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
2778 &xattr_obj
, sizeof (xattr_obj
));
2780 if (err
== 0 && xattr_obj
) {
2781 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
2785 if (mask
& ATTR_UID
) {
2786 new_uid
= zfs_fuid_create(zsb
,
2787 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2788 if (new_uid
!= zp
->z_uid
&&
2789 zfs_fuid_overquota(zsb
, B_FALSE
, new_uid
)) {
2797 if (mask
& ATTR_GID
) {
2798 new_gid
= zfs_fuid_create(zsb
, (uint64_t)vap
->va_gid
,
2799 cr
, ZFS_GROUP
, &fuidp
);
2800 if (new_gid
!= zp
->z_gid
&&
2801 zfs_fuid_overquota(zsb
, B_TRUE
, new_gid
)) {
2809 tx
= dmu_tx_create(zsb
->z_os
);
2811 if (mask
& ATTR_MODE
) {
2812 uint64_t pmode
= zp
->z_mode
;
2814 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2816 zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
);
2818 mutex_enter(&zp
->z_lock
);
2819 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2821 * Are we upgrading ACL from old V0 format
2824 if (zsb
->z_version
>= ZPL_VERSION_FUID
&&
2825 zfs_znode_acl_version(zp
) ==
2826 ZFS_ACL_VERSION_INITIAL
) {
2827 dmu_tx_hold_free(tx
, acl_obj
, 0,
2829 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2830 0, aclp
->z_acl_bytes
);
2832 dmu_tx_hold_write(tx
, acl_obj
, 0,
2835 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2836 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2837 0, aclp
->z_acl_bytes
);
2839 mutex_exit(&zp
->z_lock
);
2840 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2842 if ((mask
& ATTR_XVATTR
) &&
2843 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2844 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2846 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2850 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2853 fuid_dirtied
= zsb
->z_fuid_dirty
;
2855 zfs_fuid_txhold(zsb
, tx
);
2857 zfs_sa_upgrade_txholds(tx
, zp
);
2859 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2865 * Set each attribute requested.
2866 * We group settings according to the locks they need to acquire.
2868 * Note: you cannot set ctime directly, although it will be
2869 * updated as a side-effect of calling this function.
2873 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2874 mutex_enter(&zp
->z_acl_lock
);
2875 mutex_enter(&zp
->z_lock
);
2877 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
2878 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2881 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2882 mutex_enter(&attrzp
->z_acl_lock
);
2883 mutex_enter(&attrzp
->z_lock
);
2884 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2885 SA_ZPL_FLAGS(zsb
), NULL
, &attrzp
->z_pflags
,
2886 sizeof (attrzp
->z_pflags
));
2889 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2891 if (mask
& ATTR_UID
) {
2892 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
2893 &new_uid
, sizeof (new_uid
));
2894 zp
->z_uid
= new_uid
;
2896 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2897 SA_ZPL_UID(zsb
), NULL
, &new_uid
,
2899 attrzp
->z_uid
= new_uid
;
2903 if (mask
& ATTR_GID
) {
2904 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
),
2905 NULL
, &new_gid
, sizeof (new_gid
));
2906 zp
->z_gid
= new_gid
;
2908 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2909 SA_ZPL_GID(zsb
), NULL
, &new_gid
,
2911 attrzp
->z_gid
= new_gid
;
2914 if (!(mask
& ATTR_MODE
)) {
2915 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
),
2916 NULL
, &new_mode
, sizeof (new_mode
));
2917 new_mode
= zp
->z_mode
;
2919 err
= zfs_acl_chown_setattr(zp
);
2922 err
= zfs_acl_chown_setattr(attrzp
);
2927 if (mask
& ATTR_MODE
) {
2928 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
2929 &new_mode
, sizeof (new_mode
));
2930 zp
->z_mode
= new_mode
;
2931 ASSERT3P(aclp
, !=, NULL
);
2932 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2934 if (zp
->z_acl_cached
)
2935 zfs_acl_free(zp
->z_acl_cached
);
2936 zp
->z_acl_cached
= aclp
;
2941 if (mask
& ATTR_ATIME
) {
2942 ZFS_TIME_ENCODE(&vap
->va_atime
, zp
->z_atime
);
2943 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
2944 &zp
->z_atime
, sizeof (zp
->z_atime
));
2947 if (mask
& ATTR_MTIME
) {
2948 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2949 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
,
2950 mtime
, sizeof (mtime
));
2953 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2954 if (mask
& ATTR_SIZE
&& !(mask
& ATTR_MTIME
)) {
2955 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
),
2956 NULL
, mtime
, sizeof (mtime
));
2957 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2958 &ctime
, sizeof (ctime
));
2959 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
2961 } else if (mask
!= 0) {
2962 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2963 &ctime
, sizeof (ctime
));
2964 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
,
2967 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2968 SA_ZPL_CTIME(zsb
), NULL
,
2969 &ctime
, sizeof (ctime
));
2970 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
2971 mtime
, ctime
, B_TRUE
);
2975 * Do this after setting timestamps to prevent timestamp
2976 * update from toggling bit
2979 if (xoap
&& (mask
& ATTR_XVATTR
)) {
2982 * restore trimmed off masks
2983 * so that return masks can be set for caller.
2986 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
2987 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2989 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
2990 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2992 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
2993 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2995 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
2996 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2998 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
2999 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
3001 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
3002 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
3005 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
3006 ASSERT(S_ISREG(ip
->i_mode
));
3008 zfs_xvattr_set(zp
, xvap
, tx
);
3012 zfs_fuid_sync(zsb
, tx
);
3015 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
3017 mutex_exit(&zp
->z_lock
);
3018 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3019 mutex_exit(&zp
->z_acl_lock
);
3022 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3023 mutex_exit(&attrzp
->z_acl_lock
);
3024 mutex_exit(&attrzp
->z_lock
);
3027 if (err
== 0 && attrzp
) {
3028 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3039 zfs_fuid_info_free(fuidp
);
3045 if (err
== ERESTART
)
3048 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3050 zfs_inode_update(zp
);
3054 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3055 zil_commit(zilog
, 0);
3058 kmem_free(xattr_bulk
, sizeof (sa_bulk_attr_t
) * 7);
3059 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * 7);
3060 kmem_free(tmpxvattr
, sizeof (xvattr_t
));
3064 EXPORT_SYMBOL(zfs_setattr
);
3066 typedef struct zfs_zlock
{
3067 krwlock_t
*zl_rwlock
; /* lock we acquired */
3068 znode_t
*zl_znode
; /* znode we held */
3069 struct zfs_zlock
*zl_next
; /* next in list */
3073 * Drop locks and release vnodes that were held by zfs_rename_lock().
3076 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3080 while ((zl
= *zlpp
) != NULL
) {
3081 if (zl
->zl_znode
!= NULL
)
3082 iput(ZTOI(zl
->zl_znode
));
3083 rw_exit(zl
->zl_rwlock
);
3084 *zlpp
= zl
->zl_next
;
3085 kmem_free(zl
, sizeof (*zl
));
3090 * Search back through the directory tree, using the ".." entries.
3091 * Lock each directory in the chain to prevent concurrent renames.
3092 * Fail any attempt to move a directory into one of its own descendants.
3093 * XXX - z_parent_lock can overlap with map or grow locks
3096 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3100 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
3101 uint64_t oidp
= zp
->z_id
;
3102 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3103 krw_t rw
= RW_WRITER
;
3106 * First pass write-locks szp and compares to zp->z_id.
3107 * Later passes read-lock zp and compare to zp->z_parent.
3110 if (!rw_tryenter(rwlp
, rw
)) {
3112 * Another thread is renaming in this path.
3113 * Note that if we are a WRITER, we don't have any
3114 * parent_locks held yet.
3116 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3118 * Drop our locks and restart
3120 zfs_rename_unlock(&zl
);
3124 rwlp
= &szp
->z_parent_lock
;
3129 * Wait for other thread to drop its locks
3135 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3136 zl
->zl_rwlock
= rwlp
;
3137 zl
->zl_znode
= NULL
;
3138 zl
->zl_next
= *zlpp
;
3141 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3142 return (SET_ERROR(EINVAL
));
3144 if (oidp
== rootid
) /* We've hit the top */
3147 if (rw
== RW_READER
) { /* i.e. not the first pass */
3148 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
3153 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
3154 &oidp
, sizeof (oidp
));
3155 rwlp
= &zp
->z_parent_lock
;
3158 } while (zp
->z_id
!= sdzp
->z_id
);
3164 * Move an entry from the provided source directory to the target
3165 * directory. Change the entry name as indicated.
3167 * IN: sdip - Source directory containing the "old entry".
3168 * snm - Old entry name.
3169 * tdip - Target directory to contain the "new entry".
3170 * tnm - New entry name.
3171 * cr - credentials of caller.
3172 * flags - case flags
3174 * RETURN: 0 on success, error code on failure.
3177 * sdip,tdip - ctime|mtime updated
3181 zfs_rename(struct inode
*sdip
, char *snm
, struct inode
*tdip
, char *tnm
,
3182 cred_t
*cr
, int flags
)
3184 znode_t
*tdzp
, *szp
, *tzp
;
3185 znode_t
*sdzp
= ITOZ(sdip
);
3186 zfs_sb_t
*zsb
= ITOZSB(sdip
);
3188 zfs_dirlock_t
*sdl
, *tdl
;
3191 int cmp
, serr
, terr
;
3194 boolean_t waited
= B_FALSE
;
3197 ZFS_VERIFY_ZP(sdzp
);
3200 if (tdip
->i_sb
!= sdip
->i_sb
|| zfsctl_is_node(tdip
)) {
3202 return (SET_ERROR(EXDEV
));
3206 ZFS_VERIFY_ZP(tdzp
);
3207 if (zsb
->z_utf8
&& u8_validate(tnm
,
3208 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3210 return (SET_ERROR(EILSEQ
));
3213 if (flags
& FIGNORECASE
)
3222 * This is to prevent the creation of links into attribute space
3223 * by renaming a linked file into/outof an attribute directory.
3224 * See the comment in zfs_link() for why this is considered bad.
3226 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3228 return (SET_ERROR(EINVAL
));
3232 * Lock source and target directory entries. To prevent deadlock,
3233 * a lock ordering must be defined. We lock the directory with
3234 * the smallest object id first, or if it's a tie, the one with
3235 * the lexically first name.
3237 if (sdzp
->z_id
< tdzp
->z_id
) {
3239 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3243 * First compare the two name arguments without
3244 * considering any case folding.
3246 int nofold
= (zsb
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3248 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3249 ASSERT(error
== 0 || !zsb
->z_utf8
);
3252 * POSIX: "If the old argument and the new argument
3253 * both refer to links to the same existing file,
3254 * the rename() function shall return successfully
3255 * and perform no other action."
3261 * If the file system is case-folding, then we may
3262 * have some more checking to do. A case-folding file
3263 * system is either supporting mixed case sensitivity
3264 * access or is completely case-insensitive. Note
3265 * that the file system is always case preserving.
3267 * In mixed sensitivity mode case sensitive behavior
3268 * is the default. FIGNORECASE must be used to
3269 * explicitly request case insensitive behavior.
3271 * If the source and target names provided differ only
3272 * by case (e.g., a request to rename 'tim' to 'Tim'),
3273 * we will treat this as a special case in the
3274 * case-insensitive mode: as long as the source name
3275 * is an exact match, we will allow this to proceed as
3276 * a name-change request.
3278 if ((zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
3279 (zsb
->z_case
== ZFS_CASE_MIXED
&&
3280 flags
& FIGNORECASE
)) &&
3281 u8_strcmp(snm
, tnm
, 0, zsb
->z_norm
, U8_UNICODE_LATEST
,
3284 * case preserving rename request, require exact
3293 * If the source and destination directories are the same, we should
3294 * grab the z_name_lock of that directory only once.
3298 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3302 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3303 ZEXISTS
| zflg
, NULL
, NULL
);
3304 terr
= zfs_dirent_lock(&tdl
,
3305 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3307 terr
= zfs_dirent_lock(&tdl
,
3308 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3309 serr
= zfs_dirent_lock(&sdl
,
3310 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3316 * Source entry invalid or not there.
3319 zfs_dirent_unlock(tdl
);
3325 rw_exit(&sdzp
->z_name_lock
);
3327 if (strcmp(snm
, "..") == 0)
3333 zfs_dirent_unlock(sdl
);
3337 rw_exit(&sdzp
->z_name_lock
);
3339 if (strcmp(tnm
, "..") == 0)
3346 * Must have write access at the source to remove the old entry
3347 * and write access at the target to create the new entry.
3348 * Note that if target and source are the same, this can be
3349 * done in a single check.
3352 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
3355 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3357 * Check to make sure rename is valid.
3358 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3360 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
3365 * Does target exist?
3369 * Source and target must be the same type.
3371 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3372 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3373 error
= SET_ERROR(ENOTDIR
);
3377 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3378 error
= SET_ERROR(EISDIR
);
3383 * POSIX dictates that when the source and target
3384 * entries refer to the same file object, rename
3385 * must do nothing and exit without error.
3387 if (szp
->z_id
== tzp
->z_id
) {
3393 tx
= dmu_tx_create(zsb
->z_os
);
3394 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3395 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3396 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3397 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3399 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3400 zfs_sa_upgrade_txholds(tx
, tdzp
);
3403 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3404 zfs_sa_upgrade_txholds(tx
, tzp
);
3407 zfs_sa_upgrade_txholds(tx
, szp
);
3408 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
3409 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3412 zfs_rename_unlock(&zl
);
3413 zfs_dirent_unlock(sdl
);
3414 zfs_dirent_unlock(tdl
);
3417 rw_exit(&sdzp
->z_name_lock
);
3422 if (error
== ERESTART
) {
3433 if (tzp
) /* Attempt to remove the existing target */
3434 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3437 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3439 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3441 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zsb
),
3442 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3445 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3447 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3448 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
3449 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3452 * At this point, we have successfully created
3453 * the target name, but have failed to remove
3454 * the source name. Since the create was done
3455 * with the ZRENAMING flag, there are
3456 * complications; for one, the link count is
3457 * wrong. The easiest way to deal with this
3458 * is to remove the newly created target, and
3459 * return the original error. This must
3460 * succeed; fortunately, it is very unlikely to
3461 * fail, since we just created it.
3463 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3464 ZRENAMING
, NULL
), ==, 0);
3472 zfs_rename_unlock(&zl
);
3474 zfs_dirent_unlock(sdl
);
3475 zfs_dirent_unlock(tdl
);
3477 zfs_inode_update(sdzp
);
3479 rw_exit(&sdzp
->z_name_lock
);
3482 zfs_inode_update(tdzp
);
3484 zfs_inode_update(szp
);
3487 zfs_inode_update(tzp
);
3491 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3492 zil_commit(zilog
, 0);
3497 EXPORT_SYMBOL(zfs_rename
);
3500 * Insert the indicated symbolic reference entry into the directory.
3502 * IN: dip - Directory to contain new symbolic link.
3503 * link - Name for new symlink entry.
3504 * vap - Attributes of new entry.
3505 * target - Target path of new symlink.
3507 * cr - credentials of caller.
3508 * flags - case flags
3510 * RETURN: 0 on success, error code on failure.
3513 * dip - ctime|mtime updated
3517 zfs_symlink(struct inode
*dip
, char *name
, vattr_t
*vap
, char *link
,
3518 struct inode
**ipp
, cred_t
*cr
, int flags
)
3520 znode_t
*zp
, *dzp
= ITOZ(dip
);
3523 zfs_sb_t
*zsb
= ITOZSB(dip
);
3525 uint64_t len
= strlen(link
);
3528 zfs_acl_ids_t acl_ids
;
3529 boolean_t fuid_dirtied
;
3530 uint64_t txtype
= TX_SYMLINK
;
3531 boolean_t waited
= B_FALSE
;
3533 ASSERT(S_ISLNK(vap
->va_mode
));
3539 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
3540 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3542 return (SET_ERROR(EILSEQ
));
3544 if (flags
& FIGNORECASE
)
3547 if (len
> MAXPATHLEN
) {
3549 return (SET_ERROR(ENAMETOOLONG
));
3552 if ((error
= zfs_acl_ids_create(dzp
, 0,
3553 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3561 * Attempt to lock directory; fail if entry already exists.
3563 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3565 zfs_acl_ids_free(&acl_ids
);
3570 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3571 zfs_acl_ids_free(&acl_ids
);
3572 zfs_dirent_unlock(dl
);
3577 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
3578 zfs_acl_ids_free(&acl_ids
);
3579 zfs_dirent_unlock(dl
);
3581 return (SET_ERROR(EDQUOT
));
3583 tx
= dmu_tx_create(zsb
->z_os
);
3584 fuid_dirtied
= zsb
->z_fuid_dirty
;
3585 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3586 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3587 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3588 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3589 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3590 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3591 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3592 acl_ids
.z_aclp
->z_acl_bytes
);
3595 zfs_fuid_txhold(zsb
, tx
);
3596 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3598 zfs_dirent_unlock(dl
);
3599 if (error
== ERESTART
) {
3605 zfs_acl_ids_free(&acl_ids
);
3612 * Create a new object for the symlink.
3613 * for version 4 ZPL datsets the symlink will be an SA attribute
3615 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3618 zfs_fuid_sync(zsb
, tx
);
3620 mutex_enter(&zp
->z_lock
);
3622 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zsb
),
3625 zfs_sa_symlink(zp
, link
, len
, tx
);
3626 mutex_exit(&zp
->z_lock
);
3629 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
3630 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3632 * Insert the new object into the directory.
3634 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3636 if (flags
& FIGNORECASE
)
3638 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3640 zfs_inode_update(dzp
);
3641 zfs_inode_update(zp
);
3643 zfs_acl_ids_free(&acl_ids
);
3647 zfs_dirent_unlock(dl
);
3651 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3652 zil_commit(zilog
, 0);
3657 EXPORT_SYMBOL(zfs_symlink
);
3660 * Return, in the buffer contained in the provided uio structure,
3661 * the symbolic path referred to by ip.
3663 * IN: ip - inode of symbolic link
3664 * uio - structure to contain the link path.
3665 * cr - credentials of caller.
3667 * RETURN: 0 if success
3668 * error code if failure
3671 * ip - atime updated
3675 zfs_readlink(struct inode
*ip
, uio_t
*uio
, cred_t
*cr
)
3677 znode_t
*zp
= ITOZ(ip
);
3678 zfs_sb_t
*zsb
= ITOZSB(ip
);
3684 mutex_enter(&zp
->z_lock
);
3686 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3687 SA_ZPL_SYMLINK(zsb
), uio
);
3689 error
= zfs_sa_readlink(zp
, uio
);
3690 mutex_exit(&zp
->z_lock
);
3692 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
3696 EXPORT_SYMBOL(zfs_readlink
);
3699 * Insert a new entry into directory tdip referencing sip.
3701 * IN: tdip - Directory to contain new entry.
3702 * sip - inode of new entry.
3703 * name - name of new entry.
3704 * cr - credentials of caller.
3706 * RETURN: 0 if success
3707 * error code if failure
3710 * tdip - ctime|mtime updated
3711 * sip - ctime updated
3715 zfs_link(struct inode
*tdip
, struct inode
*sip
, char *name
, cred_t
*cr
)
3717 znode_t
*dzp
= ITOZ(tdip
);
3719 zfs_sb_t
*zsb
= ITOZSB(tdip
);
3727 boolean_t waited
= B_FALSE
;
3729 ASSERT(S_ISDIR(tdip
->i_mode
));
3736 * POSIX dictates that we return EPERM here.
3737 * Better choices include ENOTSUP or EISDIR.
3739 if (S_ISDIR(sip
->i_mode
)) {
3741 return (SET_ERROR(EPERM
));
3744 if (sip
->i_sb
!= tdip
->i_sb
|| zfsctl_is_node(sip
)) {
3746 return (SET_ERROR(EXDEV
));
3752 /* Prevent links to .zfs/shares files */
3754 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
3755 &parent
, sizeof (uint64_t))) != 0) {
3759 if (parent
== zsb
->z_shares_dir
) {
3761 return (SET_ERROR(EPERM
));
3764 if (zsb
->z_utf8
&& u8_validate(name
,
3765 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3767 return (SET_ERROR(EILSEQ
));
3769 #ifdef HAVE_PN_UTILS
3770 if (flags
& FIGNORECASE
)
3772 #endif /* HAVE_PN_UTILS */
3775 * We do not support links between attributes and non-attributes
3776 * because of the potential security risk of creating links
3777 * into "normal" file space in order to circumvent restrictions
3778 * imposed in attribute space.
3780 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
3782 return (SET_ERROR(EINVAL
));
3785 owner
= zfs_fuid_map_id(zsb
, szp
->z_uid
, cr
, ZFS_OWNER
);
3786 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
3788 return (SET_ERROR(EPERM
));
3791 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3798 * Attempt to lock directory; fail if entry already exists.
3800 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
3806 tx
= dmu_tx_create(zsb
->z_os
);
3807 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3808 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3809 zfs_sa_upgrade_txholds(tx
, szp
);
3810 zfs_sa_upgrade_txholds(tx
, dzp
);
3811 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3813 zfs_dirent_unlock(dl
);
3814 if (error
== ERESTART
) {
3825 error
= zfs_link_create(dl
, szp
, tx
, 0);
3828 uint64_t txtype
= TX_LINK
;
3829 #ifdef HAVE_PN_UTILS
3830 if (flags
& FIGNORECASE
)
3832 #endif /* HAVE_PN_UTILS */
3833 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
3838 zfs_dirent_unlock(dl
);
3840 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3841 zil_commit(zilog
, 0);
3843 zfs_inode_update(dzp
);
3844 zfs_inode_update(szp
);
3848 EXPORT_SYMBOL(zfs_link
);
3851 zfs_putpage_commit_cb(void *arg
)
3853 struct page
*pp
= arg
;
3856 end_page_writeback(pp
);
3860 * Push a page out to disk, once the page is on stable storage the
3861 * registered commit callback will be run as notification of completion.
3863 * IN: ip - page mapped for inode.
3864 * pp - page to push (page is locked)
3865 * wbc - writeback control data
3867 * RETURN: 0 if success
3868 * error code if failure
3871 * ip - ctime|mtime updated
3875 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
3877 znode_t
*zp
= ITOZ(ip
);
3878 zfs_sb_t
*zsb
= ITOZSB(ip
);
3886 uint64_t mtime
[2], ctime
[2];
3887 sa_bulk_attr_t bulk
[3];
3889 struct address_space
*mapping
;
3894 ASSERT(PageLocked(pp
));
3896 pgoff
= page_offset(pp
); /* Page byte-offset in file */
3897 offset
= i_size_read(ip
); /* File length in bytes */
3898 pglen
= MIN(PAGE_CACHE_SIZE
, /* Page length in bytes */
3899 P2ROUNDUP(offset
, PAGE_CACHE_SIZE
)-pgoff
);
3901 /* Page is beyond end of file */
3902 if (pgoff
>= offset
) {
3908 /* Truncate page length to end of file */
3909 if (pgoff
+ pglen
> offset
)
3910 pglen
= offset
- pgoff
;
3914 * FIXME: Allow mmap writes past its quota. The correct fix
3915 * is to register a page_mkwrite() handler to count the page
3916 * against its quota when it is about to be dirtied.
3918 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
3919 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
3925 * The ordering here is critical and must adhere to the following
3926 * rules in order to avoid deadlocking in either zfs_read() or
3927 * zfs_free_range() due to a lock inversion.
3929 * 1) The page must be unlocked prior to acquiring the range lock.
3930 * This is critical because zfs_read() calls find_lock_page()
3931 * which may block on the page lock while holding the range lock.
3933 * 2) Before setting or clearing write back on a page the range lock
3934 * must be held in order to prevent a lock inversion with the
3935 * zfs_free_range() function.
3937 * This presents a problem because upon entering this function the
3938 * page lock is already held. To safely acquire the range lock the
3939 * page lock must be dropped. This creates a window where another
3940 * process could truncate, invalidate, dirty, or write out the page.
3942 * Therefore, after successfully reacquiring the range and page locks
3943 * the current page state is checked. In the common case everything
3944 * will be as is expected and it can be written out. However, if
3945 * the page state has changed it must be handled accordingly.
3947 mapping
= pp
->mapping
;
3948 redirty_page_for_writepage(wbc
, pp
);
3951 rl
= zfs_range_lock(zp
, pgoff
, pglen
, RL_WRITER
);
3954 /* Page mapping changed or it was no longer dirty, we're done */
3955 if (unlikely((mapping
!= pp
->mapping
) || !PageDirty(pp
))) {
3957 zfs_range_unlock(rl
);
3962 /* Another process started write block if required */
3963 if (PageWriteback(pp
)) {
3965 zfs_range_unlock(rl
);
3967 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
3968 wait_on_page_writeback(pp
);
3974 /* Clear the dirty flag the required locks are held */
3975 if (!clear_page_dirty_for_io(pp
)) {
3977 zfs_range_unlock(rl
);
3983 * Counterpart for redirty_page_for_writepage() above. This page
3984 * was in fact not skipped and should not be counted as if it were.
3986 wbc
->pages_skipped
--;
3987 set_page_writeback(pp
);
3990 tx
= dmu_tx_create(zsb
->z_os
);
3991 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
3992 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3993 zfs_sa_upgrade_txholds(tx
, zp
);
3995 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3997 if (err
== ERESTART
)
4001 __set_page_dirty_nobuffers(pp
);
4003 end_page_writeback(pp
);
4004 zfs_range_unlock(rl
);
4010 ASSERT3U(pglen
, <=, PAGE_CACHE_SIZE
);
4011 dmu_write(zsb
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
4014 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
4015 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
4016 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zsb
), NULL
, &zp
->z_pflags
, 8);
4018 /* Preserve the mtime and ctime provided by the inode */
4019 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4020 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4021 zp
->z_atime_dirty
= 0;
4024 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4026 zfs_log_write(zsb
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
4027 zfs_putpage_commit_cb
, pp
);
4030 zfs_range_unlock(rl
);
4032 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
4034 * Note that this is rarely called under writepages(), because
4035 * writepages() normally handles the entire commit for
4036 * performance reasons.
4038 if (zsb
->z_log
!= NULL
)
4039 zil_commit(zsb
->z_log
, zp
->z_id
);
4047 * Update the system attributes when the inode has been dirtied. For the
4048 * moment we only update the mode, atime, mtime, and ctime.
4051 zfs_dirty_inode(struct inode
*ip
, int flags
)
4053 znode_t
*zp
= ITOZ(ip
);
4054 zfs_sb_t
*zsb
= ITOZSB(ip
);
4056 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
4057 sa_bulk_attr_t bulk
[4];
4061 if (zfs_is_readonly(zsb
) || dmu_objset_is_snapshot(zsb
->z_os
))
4067 tx
= dmu_tx_create(zsb
->z_os
);
4069 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4070 zfs_sa_upgrade_txholds(tx
, zp
);
4072 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4078 mutex_enter(&zp
->z_lock
);
4079 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zsb
), NULL
, &mode
, 8);
4080 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zsb
), NULL
, &atime
, 16);
4081 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
4082 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
4084 /* Preserve the mode, mtime and ctime provided by the inode */
4085 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4086 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4087 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4091 zp
->z_atime_dirty
= 0;
4093 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4094 mutex_exit(&zp
->z_lock
);
4101 EXPORT_SYMBOL(zfs_dirty_inode
);
4105 zfs_inactive(struct inode
*ip
)
4107 znode_t
*zp
= ITOZ(ip
);
4108 zfs_sb_t
*zsb
= ITOZSB(ip
);
4110 int need_unlock
= 0;
4112 /* Only read lock if we haven't already write locked, e.g. rollback */
4113 if (!RW_WRITE_HELD(&zsb
->z_teardown_inactive_lock
)) {
4115 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_READER
);
4117 if (zp
->z_sa_hdl
== NULL
) {
4119 rw_exit(&zsb
->z_teardown_inactive_lock
);
4123 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4124 dmu_tx_t
*tx
= dmu_tx_create(zsb
->z_os
);
4126 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4127 zfs_sa_upgrade_txholds(tx
, zp
);
4128 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4132 mutex_enter(&zp
->z_lock
);
4133 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
),
4134 (void *)&zp
->z_atime
, sizeof (zp
->z_atime
), tx
);
4135 zp
->z_atime_dirty
= 0;
4136 mutex_exit(&zp
->z_lock
);
4143 rw_exit(&zsb
->z_teardown_inactive_lock
);
4145 EXPORT_SYMBOL(zfs_inactive
);
4148 * Bounds-check the seek operation.
4150 * IN: ip - inode seeking within
4151 * ooff - old file offset
4152 * noffp - pointer to new file offset
4153 * ct - caller context
4155 * RETURN: 0 if success
4156 * EINVAL if new offset invalid
4160 zfs_seek(struct inode
*ip
, offset_t ooff
, offset_t
*noffp
)
4162 if (S_ISDIR(ip
->i_mode
))
4164 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4166 EXPORT_SYMBOL(zfs_seek
);
4169 * Fill pages with data from the disk.
4172 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4174 znode_t
*zp
= ITOZ(ip
);
4175 zfs_sb_t
*zsb
= ITOZSB(ip
);
4177 struct page
*cur_pp
;
4178 u_offset_t io_off
, total
;
4185 io_len
= nr_pages
<< PAGE_CACHE_SHIFT
;
4186 i_size
= i_size_read(ip
);
4187 io_off
= page_offset(pl
[0]);
4189 if (io_off
+ io_len
> i_size
)
4190 io_len
= i_size
- io_off
;
4193 * Iterate over list of pages and read each page individually.
4197 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4201 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4205 /* convert checksum errors into IO errors */
4207 err
= SET_ERROR(EIO
);
4210 cur_pp
= pl
[++page_idx
];
4217 * Uses zfs_fillpage to read data from the file and fill the pages.
4219 * IN: ip - inode of file to get data from.
4220 * pl - list of pages to read
4221 * nr_pages - number of pages to read
4223 * RETURN: 0 on success, error code on failure.
4226 * vp - atime updated
4230 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4232 znode_t
*zp
= ITOZ(ip
);
4233 zfs_sb_t
*zsb
= ITOZSB(ip
);
4242 err
= zfs_fillpage(ip
, pl
, nr_pages
);
4245 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
4250 EXPORT_SYMBOL(zfs_getpage
);
4253 * Check ZFS specific permissions to memory map a section of a file.
4255 * IN: ip - inode of the file to mmap
4257 * addrp - start address in memory region
4258 * len - length of memory region
4259 * vm_flags- address flags
4261 * RETURN: 0 if success
4262 * error code if failure
4266 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
4267 unsigned long vm_flags
)
4269 znode_t
*zp
= ITOZ(ip
);
4270 zfs_sb_t
*zsb
= ITOZSB(ip
);
4275 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
4276 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4278 return (SET_ERROR(EPERM
));
4281 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
4282 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4284 return (SET_ERROR(EACCES
));
4287 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4289 return (SET_ERROR(ENXIO
));
4295 EXPORT_SYMBOL(zfs_map
);
4298 * convoff - converts the given data (start, whence) to the
4302 convoff(struct inode
*ip
, flock64_t
*lckdat
, int whence
, offset_t offset
)
4307 if ((lckdat
->l_whence
== 2) || (whence
== 2)) {
4308 if ((error
= zfs_getattr(ip
, &vap
, 0, CRED()) != 0))
4312 switch (lckdat
->l_whence
) {
4314 lckdat
->l_start
+= offset
;
4317 lckdat
->l_start
+= vap
.va_size
;
4322 return (SET_ERROR(EINVAL
));
4325 if (lckdat
->l_start
< 0)
4326 return (SET_ERROR(EINVAL
));
4330 lckdat
->l_start
-= offset
;
4333 lckdat
->l_start
-= vap
.va_size
;
4338 return (SET_ERROR(EINVAL
));
4341 lckdat
->l_whence
= (short)whence
;
4346 * Free or allocate space in a file. Currently, this function only
4347 * supports the `F_FREESP' command. However, this command is somewhat
4348 * misnamed, as its functionality includes the ability to allocate as
4349 * well as free space.
4351 * IN: ip - inode of file to free data in.
4352 * cmd - action to take (only F_FREESP supported).
4353 * bfp - section of file to free/alloc.
4354 * flag - current file open mode flags.
4355 * offset - current file offset.
4356 * cr - credentials of caller [UNUSED].
4358 * RETURN: 0 on success, error code on failure.
4361 * ip - ctime|mtime updated
4365 zfs_space(struct inode
*ip
, int cmd
, flock64_t
*bfp
, int flag
,
4366 offset_t offset
, cred_t
*cr
)
4368 znode_t
*zp
= ITOZ(ip
);
4369 zfs_sb_t
*zsb
= ITOZSB(ip
);
4376 if (cmd
!= F_FREESP
) {
4378 return (SET_ERROR(EINVAL
));
4381 if ((error
= convoff(ip
, bfp
, 0, offset
))) {
4386 if (bfp
->l_len
< 0) {
4388 return (SET_ERROR(EINVAL
));
4392 * Permissions aren't checked on Solaris because on this OS
4393 * zfs_space() can only be called with an opened file handle.
4394 * On Linux we can get here through truncate_range() which
4395 * operates directly on inodes, so we need to check access rights.
4397 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
4403 len
= bfp
->l_len
; /* 0 means from off to end of file */
4405 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4410 EXPORT_SYMBOL(zfs_space
);
4414 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4416 znode_t
*zp
= ITOZ(ip
);
4417 zfs_sb_t
*zsb
= ITOZSB(ip
);
4420 uint64_t object
= zp
->z_id
;
4427 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
),
4428 &gen64
, sizeof (uint64_t))) != 0) {
4433 gen
= (uint32_t)gen64
;
4435 size
= (zsb
->z_parent
!= zsb
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4436 if (fidp
->fid_len
< size
) {
4437 fidp
->fid_len
= size
;
4439 return (SET_ERROR(ENOSPC
));
4442 zfid
= (zfid_short_t
*)fidp
;
4444 zfid
->zf_len
= size
;
4446 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4447 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4449 /* Must have a non-zero generation number to distinguish from .zfs */
4452 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4453 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4455 if (size
== LONG_FID_LEN
) {
4456 uint64_t objsetid
= dmu_objset_id(zsb
->z_os
);
4459 zlfid
= (zfid_long_t
*)fidp
;
4461 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4462 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4464 /* XXX - this should be the generation number for the objset */
4465 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4466 zlfid
->zf_setgen
[i
] = 0;
4472 EXPORT_SYMBOL(zfs_fid
);
4476 zfs_getsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4478 znode_t
*zp
= ITOZ(ip
);
4479 zfs_sb_t
*zsb
= ITOZSB(ip
);
4481 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4485 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
4490 EXPORT_SYMBOL(zfs_getsecattr
);
4494 zfs_setsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4496 znode_t
*zp
= ITOZ(ip
);
4497 zfs_sb_t
*zsb
= ITOZSB(ip
);
4499 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4500 zilog_t
*zilog
= zsb
->z_log
;
4505 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
4507 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4508 zil_commit(zilog
, 0);
4513 EXPORT_SYMBOL(zfs_setsecattr
);
4515 #ifdef HAVE_UIO_ZEROCOPY
4517 * Tunable, both must be a power of 2.
4519 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4520 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4521 * an arcbuf for a partial block read
4523 int zcr_blksz_min
= (1 << 10); /* 1K */
4524 int zcr_blksz_max
= (1 << 17); /* 128K */
4528 zfs_reqzcbuf(struct inode
*ip
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
)
4530 znode_t
*zp
= ITOZ(ip
);
4531 zfs_sb_t
*zsb
= ITOZSB(ip
);
4532 int max_blksz
= zsb
->z_max_blksz
;
4533 uio_t
*uio
= &xuio
->xu_uio
;
4534 ssize_t size
= uio
->uio_resid
;
4535 offset_t offset
= uio
->uio_loffset
;
4540 int preamble
, postamble
;
4542 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
4543 return (SET_ERROR(EINVAL
));
4550 * Loan out an arc_buf for write if write size is bigger than
4551 * max_blksz, and the file's block size is also max_blksz.
4554 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
4556 return (SET_ERROR(EINVAL
));
4559 * Caller requests buffers for write before knowing where the
4560 * write offset might be (e.g. NFS TCP write).
4565 preamble
= P2PHASE(offset
, blksz
);
4567 preamble
= blksz
- preamble
;
4572 postamble
= P2PHASE(size
, blksz
);
4575 fullblk
= size
/ blksz
;
4576 (void) dmu_xuio_init(xuio
,
4577 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
4580 * Have to fix iov base/len for partial buffers. They
4581 * currently represent full arc_buf's.
4584 /* data begins in the middle of the arc_buf */
4585 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4588 (void) dmu_xuio_add(xuio
, abuf
,
4589 blksz
- preamble
, preamble
);
4592 for (i
= 0; i
< fullblk
; i
++) {
4593 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4596 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
4600 /* data ends in the middle of the arc_buf */
4601 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4604 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
4609 * Loan out an arc_buf for read if the read size is larger than
4610 * the current file block size. Block alignment is not
4611 * considered. Partial arc_buf will be loaned out for read.
4613 blksz
= zp
->z_blksz
;
4614 if (blksz
< zcr_blksz_min
)
4615 blksz
= zcr_blksz_min
;
4616 if (blksz
> zcr_blksz_max
)
4617 blksz
= zcr_blksz_max
;
4618 /* avoid potential complexity of dealing with it */
4619 if (blksz
> max_blksz
) {
4621 return (SET_ERROR(EINVAL
));
4624 maxsize
= zp
->z_size
- uio
->uio_loffset
;
4630 return (SET_ERROR(EINVAL
));
4635 return (SET_ERROR(EINVAL
));
4638 uio
->uio_extflg
= UIO_XUIO
;
4639 XUIO_XUZC_RW(xuio
) = ioflag
;
4646 zfs_retzcbuf(struct inode
*ip
, xuio_t
*xuio
, cred_t
*cr
)
4650 int ioflag
= XUIO_XUZC_RW(xuio
);
4652 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
4654 i
= dmu_xuio_cnt(xuio
);
4656 abuf
= dmu_xuio_arcbuf(xuio
, i
);
4658 * if abuf == NULL, it must be a write buffer
4659 * that has been returned in zfs_write().
4662 dmu_return_arcbuf(abuf
);
4663 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
4666 dmu_xuio_fini(xuio
);
4669 #endif /* HAVE_UIO_ZEROCOPY */
4671 #if defined(_KERNEL) && defined(HAVE_SPL)
4672 module_param(zfs_read_chunk_size
, long, 0644);
4673 MODULE_PARM_DESC(zfs_read_chunk_size
, "Bytes to read per chunk");