4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
26 * Copyright 2017 Nexenta Systems, Inc.
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysmacros.h>
38 #include <sys/resource.h>
40 #include <sys/vfs_opreg.h>
44 #include <sys/taskq.h>
46 #include <sys/vmsystm.h>
47 #include <sys/atomic.h>
49 #include <sys/pathname.h>
50 #include <sys/cmn_err.h>
51 #include <sys/errno.h>
52 #include <sys/unistd.h>
53 #include <sys/zfs_dir.h>
54 #include <sys/zfs_acl.h>
55 #include <sys/zfs_ioctl.h>
56 #include <sys/fs/zfs.h>
58 #include <sys/dmu_objset.h>
64 #include <sys/dirent.h>
65 #include <sys/policy.h>
66 #include <sys/sunddi.h>
69 #include "fs/fs_subr.h"
70 #include <sys/zfs_ctldir.h>
71 #include <sys/zfs_fuid.h>
72 #include <sys/zfs_sa.h>
73 #include <sys/zfs_vnops.h>
75 #include <sys/zfs_rlock.h>
76 #include <sys/extdirent.h>
77 #include <sys/kidmap.h>
82 #include <sys/sa_impl.h>
87 * Each vnode op performs some logical unit of work. To do this, the ZPL must
88 * properly lock its in-core state, create a DMU transaction, do the work,
89 * record this work in the intent log (ZIL), commit the DMU transaction,
90 * and wait for the intent log to commit if it is a synchronous operation.
91 * Moreover, the vnode ops must work in both normal and log replay context.
92 * The ordering of events is important to avoid deadlocks and references
93 * to freed memory. The example below illustrates the following Big Rules:
95 * (1) A check must be made in each zfs thread for a mounted file system.
96 * This is done avoiding races using ZFS_ENTER(zfsvfs).
97 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
98 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
99 * can return EIO from the calling function.
101 * (2) iput() should always be the last thing except for zil_commit()
102 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
103 * First, if it's the last reference, the vnode/znode
104 * can be freed, so the zp may point to freed memory. Second, the last
105 * reference will call zfs_zinactive(), which may induce a lot of work --
106 * pushing cached pages (which acquires range locks) and syncing out
107 * cached atime changes. Third, zfs_zinactive() may require a new tx,
108 * which could deadlock the system if you were already holding one.
109 * If you must call iput() within a tx then use zfs_iput_async().
111 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
112 * as they can span dmu_tx_assign() calls.
114 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
115 * dmu_tx_assign(). This is critical because we don't want to block
116 * while holding locks.
118 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
119 * reduces lock contention and CPU usage when we must wait (note that if
120 * throughput is constrained by the storage, nearly every transaction
123 * Note, in particular, that if a lock is sometimes acquired before
124 * the tx assigns, and sometimes after (e.g. z_lock), then failing
125 * to use a non-blocking assign can deadlock the system. The scenario:
127 * Thread A has grabbed a lock before calling dmu_tx_assign().
128 * Thread B is in an already-assigned tx, and blocks for this lock.
129 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
130 * forever, because the previous txg can't quiesce until B's tx commits.
132 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
133 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
134 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
135 * to indicate that this operation has already called dmu_tx_wait().
136 * This will ensure that we don't retry forever, waiting a short bit
139 * (5) If the operation succeeded, generate the intent log entry for it
140 * before dropping locks. This ensures that the ordering of events
141 * in the intent log matches the order in which they actually occurred.
142 * During ZIL replay the zfs_log_* functions will update the sequence
143 * number to indicate the zil transaction has replayed.
145 * (6) At the end of each vnode op, the DMU tx must always commit,
146 * regardless of whether there were any errors.
148 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
149 * to ensure that synchronous semantics are provided when necessary.
151 * In general, this is how things should be ordered in each vnode op:
153 * ZFS_ENTER(zfsvfs); // exit if unmounted
155 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
156 * rw_enter(...); // grab any other locks you need
157 * tx = dmu_tx_create(...); // get DMU tx
158 * dmu_tx_hold_*(); // hold each object you might modify
159 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
161 * rw_exit(...); // drop locks
162 * zfs_dirent_unlock(dl); // unlock directory entry
163 * iput(...); // release held vnodes
164 * if (error == ERESTART) {
170 * dmu_tx_abort(tx); // abort DMU tx
171 * ZFS_EXIT(zfsvfs); // finished in zfs
172 * return (error); // really out of space
174 * error = do_real_work(); // do whatever this VOP does
176 * zfs_log_*(...); // on success, make ZIL entry
177 * dmu_tx_commit(tx); // commit DMU tx -- error or not
178 * rw_exit(...); // drop locks
179 * zfs_dirent_unlock(dl); // unlock directory entry
180 * iput(...); // release held vnodes
181 * zil_commit(zilog, foid); // synchronous when necessary
182 * ZFS_EXIT(zfsvfs); // finished in zfs
183 * return (error); // done, report error
187 * Virus scanning is unsupported. It would be possible to add a hook
188 * here to performance the required virus scan. This could be done
189 * entirely in the kernel or potentially as an update to invoke a
193 zfs_vscan(struct inode
*ip
, cred_t
*cr
, int async
)
200 zfs_open(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
202 znode_t
*zp
= ITOZ(ip
);
203 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
208 /* Honor ZFS_APPENDONLY file attribute */
209 if ((mode
& FMODE_WRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
210 ((flag
& O_APPEND
) == 0)) {
212 return (SET_ERROR(EPERM
));
215 /* Virus scan eligible files on open */
216 if (!zfs_has_ctldir(zp
) && zfsvfs
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
217 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0) {
218 if (zfs_vscan(ip
, cr
, 0) != 0) {
220 return (SET_ERROR(EACCES
));
224 /* Keep a count of the synchronous opens in the znode */
226 atomic_inc_32(&zp
->z_sync_cnt
);
234 zfs_close(struct inode
*ip
, int flag
, cred_t
*cr
)
236 znode_t
*zp
= ITOZ(ip
);
237 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
242 /* Decrement the synchronous opens in the znode */
244 atomic_dec_32(&zp
->z_sync_cnt
);
246 if (!zfs_has_ctldir(zp
) && zfsvfs
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
247 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0)
248 VERIFY(zfs_vscan(ip
, cr
, 1) == 0);
254 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
256 * Lseek support for finding holes (cmd == SEEK_HOLE) and
257 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
260 zfs_holey_common(struct inode
*ip
, int cmd
, loff_t
*off
)
262 znode_t
*zp
= ITOZ(ip
);
263 uint64_t noff
= (uint64_t)*off
; /* new offset */
268 file_sz
= zp
->z_size
;
269 if (noff
>= file_sz
) {
270 return (SET_ERROR(ENXIO
));
273 if (cmd
== SEEK_HOLE
)
278 error
= dmu_offset_next(ZTOZSB(zp
)->z_os
, zp
->z_id
, hole
, &noff
);
281 return (SET_ERROR(ENXIO
));
283 /* file was dirty, so fall back to using generic logic */
284 if (error
== EBUSY
) {
292 * We could find a hole that begins after the logical end-of-file,
293 * because dmu_offset_next() only works on whole blocks. If the
294 * EOF falls mid-block, then indicate that the "virtual hole"
295 * at the end of the file begins at the logical EOF, rather than
296 * at the end of the last block.
298 if (noff
> file_sz
) {
310 zfs_holey(struct inode
*ip
, int cmd
, loff_t
*off
)
312 znode_t
*zp
= ITOZ(ip
);
313 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
319 error
= zfs_holey_common(ip
, cmd
, off
);
324 #endif /* SEEK_HOLE && SEEK_DATA */
328 * When a file is memory mapped, we must keep the IO data synchronized
329 * between the DMU cache and the memory mapped pages. What this means:
331 * On Write: If we find a memory mapped page, we write to *both*
332 * the page and the dmu buffer.
335 update_pages(struct inode
*ip
, int64_t start
, int len
,
336 objset_t
*os
, uint64_t oid
)
338 struct address_space
*mp
= ip
->i_mapping
;
344 off
= start
& (PAGE_SIZE
-1);
345 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
346 nbytes
= MIN(PAGE_SIZE
- off
, len
);
348 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
350 if (mapping_writably_mapped(mp
))
351 flush_dcache_page(pp
);
354 (void) dmu_read(os
, oid
, start
+off
, nbytes
, pb
+off
,
358 if (mapping_writably_mapped(mp
))
359 flush_dcache_page(pp
);
361 mark_page_accessed(pp
);
374 * When a file is memory mapped, we must keep the IO data synchronized
375 * between the DMU cache and the memory mapped pages. What this means:
377 * On Read: We "read" preferentially from memory mapped pages,
378 * else we default from the dmu buffer.
380 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
381 * the file is memory mapped.
384 mappedread(struct inode
*ip
, int nbytes
, uio_t
*uio
)
386 struct address_space
*mp
= ip
->i_mapping
;
388 znode_t
*zp
= ITOZ(ip
);
395 start
= uio
->uio_loffset
;
396 off
= start
& (PAGE_SIZE
-1);
397 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
398 bytes
= MIN(PAGE_SIZE
- off
, len
);
400 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
402 ASSERT(PageUptodate(pp
));
405 error
= uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
408 if (mapping_writably_mapped(mp
))
409 flush_dcache_page(pp
);
411 mark_page_accessed(pp
);
415 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
428 unsigned long zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
429 unsigned long zfs_delete_blocks
= DMU_MAX_DELETEBLKCNT
;
432 * Read bytes from specified file into supplied buffer.
434 * IN: ip - inode of file to be read from.
435 * uio - structure supplying read location, range info,
437 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
438 * O_DIRECT flag; used to bypass page cache.
439 * cr - credentials of caller.
441 * OUT: uio - updated offset and range, buffer filled.
443 * RETURN: 0 on success, error code on failure.
446 * inode - atime updated if byte count > 0
450 zfs_read(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
452 znode_t
*zp
= ITOZ(ip
);
453 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
457 #ifdef HAVE_UIO_ZEROCOPY
459 #endif /* HAVE_UIO_ZEROCOPY */
464 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
466 return (SET_ERROR(EACCES
));
470 * Validate file offset
472 if (uio
->uio_loffset
< (offset_t
)0) {
474 return (SET_ERROR(EINVAL
));
478 * Fasttrack empty reads
480 if (uio
->uio_resid
== 0) {
486 * If we're in FRSYNC mode, sync out this znode before reading it.
487 * Only do this for non-snapshots.
490 (ioflag
& FRSYNC
|| zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
))
491 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
494 * Lock the range against changes.
496 rl
= zfs_range_lock(&zp
->z_range_lock
, uio
->uio_loffset
, uio
->uio_resid
,
500 * If we are reading past end-of-file we can skip
501 * to the end; but we might still need to set atime.
503 if (uio
->uio_loffset
>= zp
->z_size
) {
508 ASSERT(uio
->uio_loffset
< zp
->z_size
);
509 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
511 #ifdef HAVE_UIO_ZEROCOPY
512 if ((uio
->uio_extflg
== UIO_XUIO
) &&
513 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
515 int blksz
= zp
->z_blksz
;
516 uint64_t offset
= uio
->uio_loffset
;
518 xuio
= (xuio_t
*)uio
;
520 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
523 ASSERT(offset
+ n
<= blksz
);
526 (void) dmu_xuio_init(xuio
, nblk
);
528 if (vn_has_cached_data(ip
)) {
530 * For simplicity, we always allocate a full buffer
531 * even if we only expect to read a portion of a block.
533 while (--nblk
>= 0) {
534 (void) dmu_xuio_add(xuio
,
535 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
540 #endif /* HAVE_UIO_ZEROCOPY */
543 nbytes
= MIN(n
, zfs_read_chunk_size
-
544 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
546 if (zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
)) {
547 error
= mappedread(ip
, nbytes
, uio
);
549 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
554 /* convert checksum errors into IO errors */
556 error
= SET_ERROR(EIO
);
563 zfs_range_unlock(rl
);
570 * Write the bytes to a file.
572 * IN: ip - inode of file to be written to.
573 * uio - structure supplying write location, range info,
575 * ioflag - FAPPEND flag set if in append mode.
576 * O_DIRECT flag; used to bypass page cache.
577 * cr - credentials of caller.
579 * OUT: uio - updated offset and range.
581 * RETURN: 0 if success
582 * error code if failure
585 * ip - ctime|mtime updated if byte count > 0
590 zfs_write(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
592 znode_t
*zp
= ITOZ(ip
);
593 rlim64_t limit
= uio
->uio_limit
;
594 ssize_t start_resid
= uio
->uio_resid
;
598 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
603 int max_blksz
= zfsvfs
->z_max_blksz
;
606 const iovec_t
*aiov
= NULL
;
610 sa_bulk_attr_t bulk
[4];
611 uint64_t mtime
[2], ctime
[2];
613 #ifdef HAVE_UIO_ZEROCOPY
615 const iovec_t
*iovp
= uio
->uio_iov
;
616 ASSERTV(int iovcnt
= uio
->uio_iovcnt
);
620 * Fasttrack empty write
626 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
632 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
633 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
634 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
636 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
640 * Callers might not be able to detect properly that we are read-only,
641 * so check it explicitly here.
643 if (zfs_is_readonly(zfsvfs
)) {
645 return (SET_ERROR(EROFS
));
649 * If immutable or not appending then return EPERM
651 if ((zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
652 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
653 (uio
->uio_loffset
< zp
->z_size
))) {
655 return (SET_ERROR(EPERM
));
658 zilog
= zfsvfs
->z_log
;
661 * Validate file offset
663 woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
666 return (SET_ERROR(EINVAL
));
670 * Pre-fault the pages to ensure slow (eg NFS) pages
672 * Skip this if uio contains loaned arc_buf.
674 #ifdef HAVE_UIO_ZEROCOPY
675 if ((uio
->uio_extflg
== UIO_XUIO
) &&
676 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
677 xuio
= (xuio_t
*)uio
;
680 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
683 * If in append mode, set the io offset pointer to eof.
685 if (ioflag
& FAPPEND
) {
687 * Obtain an appending range lock to guarantee file append
688 * semantics. We reset the write offset once we have the lock.
690 rl
= zfs_range_lock(&zp
->z_range_lock
, 0, n
, RL_APPEND
);
692 if (rl
->r_len
== UINT64_MAX
) {
694 * We overlocked the file because this write will cause
695 * the file block size to increase.
696 * Note that zp_size cannot change with this lock held.
700 uio
->uio_loffset
= woff
;
703 * Note that if the file block size will change as a result of
704 * this write, then this range lock will lock the entire file
705 * so that we can re-write the block safely.
707 rl
= zfs_range_lock(&zp
->z_range_lock
, woff
, n
, RL_WRITER
);
711 zfs_range_unlock(rl
);
713 return (SET_ERROR(EFBIG
));
716 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
719 /* Will this write extend the file length? */
720 write_eof
= (woff
+ n
> zp
->z_size
);
722 end_size
= MAX(zp
->z_size
, woff
+ n
);
725 * Write the file in reasonable size chunks. Each chunk is written
726 * in a separate transaction; this keeps the intent log records small
727 * and allows us to do more fine-grained space accounting.
731 woff
= uio
->uio_loffset
;
732 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
,
733 KUID_TO_SUID(ip
->i_uid
)) ||
734 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
735 KGID_TO_SGID(ip
->i_gid
)) ||
736 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
737 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
740 dmu_return_arcbuf(abuf
);
741 error
= SET_ERROR(EDQUOT
);
745 if (xuio
&& abuf
== NULL
) {
746 #ifdef HAVE_UIO_ZEROCOPY
747 ASSERT(i_iov
< iovcnt
);
748 ASSERT3U(uio
->uio_segflg
, !=, UIO_BVEC
);
750 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
751 dmu_xuio_clear(xuio
, i_iov
);
752 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
753 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
754 aiov
->iov_len
== arc_buf_size(abuf
)));
757 } else if (abuf
== NULL
&& n
>= max_blksz
&&
758 woff
>= zp
->z_size
&&
759 P2PHASE(woff
, max_blksz
) == 0 &&
760 zp
->z_blksz
== max_blksz
) {
762 * This write covers a full block. "Borrow" a buffer
763 * from the dmu so that we can fill it before we enter
764 * a transaction. This avoids the possibility of
765 * holding up the transaction if the data copy hangs
766 * up on a pagefault (e.g., from an NFS server mapping).
770 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
772 ASSERT(abuf
!= NULL
);
773 ASSERT(arc_buf_size(abuf
) == max_blksz
);
774 if ((error
= uiocopy(abuf
->b_data
, max_blksz
,
775 UIO_WRITE
, uio
, &cbytes
))) {
776 dmu_return_arcbuf(abuf
);
779 ASSERT(cbytes
== max_blksz
);
783 * Start a transaction.
785 tx
= dmu_tx_create(zfsvfs
->z_os
);
786 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
787 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
788 zfs_sa_upgrade_txholds(tx
, zp
);
789 error
= dmu_tx_assign(tx
, TXG_WAIT
);
793 dmu_return_arcbuf(abuf
);
798 * If zfs_range_lock() over-locked we grow the blocksize
799 * and then reduce the lock range. This will only happen
800 * on the first iteration since zfs_range_reduce() will
801 * shrink down r_len to the appropriate size.
803 if (rl
->r_len
== UINT64_MAX
) {
806 if (zp
->z_blksz
> max_blksz
) {
808 * File's blocksize is already larger than the
809 * "recordsize" property. Only let it grow to
810 * the next power of 2.
812 ASSERT(!ISP2(zp
->z_blksz
));
813 new_blksz
= MIN(end_size
,
814 1 << highbit64(zp
->z_blksz
));
816 new_blksz
= MIN(end_size
, max_blksz
);
818 zfs_grow_blocksize(zp
, new_blksz
, tx
);
819 zfs_range_reduce(rl
, woff
, n
);
823 * XXX - should we really limit each write to z_max_blksz?
824 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
826 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
829 tx_bytes
= uio
->uio_resid
;
830 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
832 tx_bytes
-= uio
->uio_resid
;
835 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
837 * If this is not a full block write, but we are
838 * extending the file past EOF and this data starts
839 * block-aligned, use assign_arcbuf(). Otherwise,
840 * write via dmu_write().
842 if (tx_bytes
< max_blksz
&& (!write_eof
||
843 aiov
->iov_base
!= abuf
->b_data
)) {
845 dmu_write(zfsvfs
->z_os
, zp
->z_id
, woff
,
846 /* cppcheck-suppress nullPointer */
847 aiov
->iov_len
, aiov
->iov_base
, tx
);
848 dmu_return_arcbuf(abuf
);
849 xuio_stat_wbuf_copied();
851 ASSERT(xuio
|| tx_bytes
== max_blksz
);
852 dmu_assign_arcbuf_by_dbuf(
853 sa_get_db(zp
->z_sa_hdl
), woff
, abuf
, tx
);
855 ASSERT(tx_bytes
<= uio
->uio_resid
);
856 uioskip(uio
, tx_bytes
);
858 if (tx_bytes
&& zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
)) {
859 update_pages(ip
, woff
,
860 tx_bytes
, zfsvfs
->z_os
, zp
->z_id
);
864 * If we made no progress, we're done. If we made even
865 * partial progress, update the znode and ZIL accordingly.
868 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
869 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
876 * Clear Set-UID/Set-GID bits on successful write if not
877 * privileged and at least one of the execute bits is set.
879 * It would be nice to to this after all writes have
880 * been done, but that would still expose the ISUID/ISGID
881 * to another app after the partial write is committed.
883 * Note: we don't call zfs_fuid_map_id() here because
884 * user 0 is not an ephemeral uid.
886 mutex_enter(&zp
->z_acl_lock
);
887 uid
= KUID_TO_SUID(ip
->i_uid
);
888 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
889 (S_IXUSR
>> 6))) != 0 &&
890 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
891 secpolicy_vnode_setid_retain(cr
,
892 ((zp
->z_mode
& S_ISUID
) != 0 && uid
== 0)) != 0) {
894 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
895 ip
->i_mode
= newmode
= zp
->z_mode
;
896 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zfsvfs
),
897 (void *)&newmode
, sizeof (uint64_t), tx
);
899 mutex_exit(&zp
->z_acl_lock
);
901 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
904 * Update the file size (zp_size) if it has changed;
905 * account for possible concurrent updates.
907 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
908 (void) atomic_cas_64(&zp
->z_size
, end_size
,
913 * If we are replaying and eof is non zero then force
914 * the file size to the specified eof. Note, there's no
915 * concurrency during replay.
917 if (zfsvfs
->z_replay
&& zfsvfs
->z_replay_eof
!= 0)
918 zp
->z_size
= zfsvfs
->z_replay_eof
;
920 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
922 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
,
928 ASSERT(tx_bytes
== nbytes
);
932 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
935 zfs_inode_update(zp
);
936 zfs_range_unlock(rl
);
939 * If we're in replay mode, or we made no progress, return error.
940 * Otherwise, it's at least a partial write, so it's successful.
942 if (zfsvfs
->z_replay
|| uio
->uio_resid
== start_resid
) {
947 if (ioflag
& (FSYNC
| FDSYNC
) ||
948 zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
949 zil_commit(zilog
, zp
->z_id
);
956 * Drop a reference on the passed inode asynchronously. This ensures
957 * that the caller will never drop the last reference on an inode in
958 * the current context. Doing so while holding open a tx could result
959 * in a deadlock if iput_final() re-enters the filesystem code.
962 zfs_iput_async(struct inode
*ip
)
964 objset_t
*os
= ITOZSB(ip
)->z_os
;
966 ASSERT(atomic_read(&ip
->i_count
) > 0);
969 if (atomic_read(&ip
->i_count
) == 1)
970 VERIFY(taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os
)),
971 (task_func_t
*)iput
, ip
, TQ_SLEEP
) != TASKQID_INVALID
);
977 zfs_get_done(zgd_t
*zgd
, int error
)
979 znode_t
*zp
= zgd
->zgd_private
;
982 dmu_buf_rele(zgd
->zgd_db
, zgd
);
984 zfs_range_unlock(zgd
->zgd_rl
);
987 * Release the vnode asynchronously as we currently have the
988 * txg stopped from syncing.
990 zfs_iput_async(ZTOI(zp
));
992 if (error
== 0 && zgd
->zgd_bp
)
993 zil_lwb_add_block(zgd
->zgd_lwb
, zgd
->zgd_bp
);
995 kmem_free(zgd
, sizeof (zgd_t
));
999 static int zil_fault_io
= 0;
1003 * Get data to generate a TX_WRITE intent log record.
1006 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
, zio_t
*zio
)
1008 zfsvfs_t
*zfsvfs
= arg
;
1009 objset_t
*os
= zfsvfs
->z_os
;
1011 uint64_t object
= lr
->lr_foid
;
1012 uint64_t offset
= lr
->lr_offset
;
1013 uint64_t size
= lr
->lr_length
;
1018 ASSERT3P(lwb
, !=, NULL
);
1019 ASSERT3P(zio
, !=, NULL
);
1020 ASSERT3U(size
, !=, 0);
1023 * Nothing to do if the file has been removed
1025 if (zfs_zget(zfsvfs
, object
, &zp
) != 0)
1026 return (SET_ERROR(ENOENT
));
1027 if (zp
->z_unlinked
) {
1029 * Release the vnode asynchronously as we currently have the
1030 * txg stopped from syncing.
1032 zfs_iput_async(ZTOI(zp
));
1033 return (SET_ERROR(ENOENT
));
1036 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
1038 zgd
->zgd_private
= zp
;
1041 * Write records come in two flavors: immediate and indirect.
1042 * For small writes it's cheaper to store the data with the
1043 * log record (immediate); for large writes it's cheaper to
1044 * sync the data and get a pointer to it (indirect) so that
1045 * we don't have to write the data twice.
1047 if (buf
!= NULL
) { /* immediate write */
1048 zgd
->zgd_rl
= zfs_range_lock(&zp
->z_range_lock
, offset
, size
,
1050 /* test for truncation needs to be done while range locked */
1051 if (offset
>= zp
->z_size
) {
1052 error
= SET_ERROR(ENOENT
);
1054 error
= dmu_read(os
, object
, offset
, size
, buf
,
1055 DMU_READ_NO_PREFETCH
);
1057 ASSERT(error
== 0 || error
== ENOENT
);
1058 } else { /* indirect write */
1060 * Have to lock the whole block to ensure when it's
1061 * written out and its checksum is being calculated
1062 * that no one can change the data. We need to re-check
1063 * blocksize after we get the lock in case it's changed!
1068 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
1070 zgd
->zgd_rl
= zfs_range_lock(&zp
->z_range_lock
, offset
,
1072 if (zp
->z_blksz
== size
)
1075 zfs_range_unlock(zgd
->zgd_rl
);
1077 /* test for truncation needs to be done while range locked */
1078 if (lr
->lr_offset
>= zp
->z_size
)
1079 error
= SET_ERROR(ENOENT
);
1082 error
= SET_ERROR(EIO
);
1087 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1088 DMU_READ_NO_PREFETCH
);
1091 blkptr_t
*bp
= &lr
->lr_blkptr
;
1096 ASSERT(db
->db_offset
== offset
);
1097 ASSERT(db
->db_size
== size
);
1099 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1101 ASSERT(error
|| lr
->lr_length
<= size
);
1104 * On success, we need to wait for the write I/O
1105 * initiated by dmu_sync() to complete before we can
1106 * release this dbuf. We will finish everything up
1107 * in the zfs_get_done() callback.
1112 if (error
== EALREADY
) {
1113 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
1119 zfs_get_done(zgd
, error
);
1126 zfs_access(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
1128 znode_t
*zp
= ITOZ(ip
);
1129 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
1135 if (flag
& V_ACE_MASK
)
1136 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
1138 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
1145 * Lookup an entry in a directory, or an extended attribute directory.
1146 * If it exists, return a held inode reference for it.
1148 * IN: dip - inode of directory to search.
1149 * nm - name of entry to lookup.
1150 * flags - LOOKUP_XATTR set if looking for an attribute.
1151 * cr - credentials of caller.
1152 * direntflags - directory lookup flags
1153 * realpnp - returned pathname.
1155 * OUT: ipp - inode of located entry, NULL if not found.
1157 * RETURN: 0 on success, error code on failure.
1164 zfs_lookup(struct inode
*dip
, char *nm
, struct inode
**ipp
, int flags
,
1165 cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
1167 znode_t
*zdp
= ITOZ(dip
);
1168 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1172 * Fast path lookup, however we must skip DNLC lookup
1173 * for case folding or normalizing lookups because the
1174 * DNLC code only stores the passed in name. This means
1175 * creating 'a' and removing 'A' on a case insensitive
1176 * file system would work, but DNLC still thinks 'a'
1177 * exists and won't let you create it again on the next
1178 * pass through fast path.
1180 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1182 if (!S_ISDIR(dip
->i_mode
)) {
1183 return (SET_ERROR(ENOTDIR
));
1184 } else if (zdp
->z_sa_hdl
== NULL
) {
1185 return (SET_ERROR(EIO
));
1188 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1189 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1197 } else if (!zdp
->z_zfsvfs
->z_norm
&&
1198 (zdp
->z_zfsvfs
->z_case
== ZFS_CASE_SENSITIVE
)) {
1200 vnode_t
*tvp
= dnlc_lookup(dvp
, nm
);
1203 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1208 if (tvp
== DNLC_NO_VNODE
) {
1210 return (SET_ERROR(ENOENT
));
1213 return (specvp_check(vpp
, cr
));
1216 #endif /* HAVE_DNLC */
1225 if (flags
& LOOKUP_XATTR
) {
1227 * We don't allow recursive attributes..
1228 * Maybe someday we will.
1230 if (zdp
->z_pflags
& ZFS_XATTR
) {
1232 return (SET_ERROR(EINVAL
));
1235 if ((error
= zfs_get_xattrdir(zdp
, ipp
, cr
, flags
))) {
1241 * Do we have permission to get into attribute directory?
1244 if ((error
= zfs_zaccess(ITOZ(*ipp
), ACE_EXECUTE
, 0,
1254 if (!S_ISDIR(dip
->i_mode
)) {
1256 return (SET_ERROR(ENOTDIR
));
1260 * Check accessibility of directory.
1263 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
))) {
1268 if (zfsvfs
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1269 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1271 return (SET_ERROR(EILSEQ
));
1274 error
= zfs_dirlook(zdp
, nm
, ipp
, flags
, direntflags
, realpnp
);
1275 if ((error
== 0) && (*ipp
))
1276 zfs_inode_update(ITOZ(*ipp
));
1283 * Attempt to create a new entry in a directory. If the entry
1284 * already exists, truncate the file if permissible, else return
1285 * an error. Return the ip of the created or trunc'd file.
1287 * IN: dip - inode of directory to put new file entry in.
1288 * name - name of new file entry.
1289 * vap - attributes of new file.
1290 * excl - flag indicating exclusive or non-exclusive mode.
1291 * mode - mode to open file with.
1292 * cr - credentials of caller.
1293 * flag - large file flag [UNUSED].
1294 * vsecp - ACL to be set
1296 * OUT: ipp - inode of created or trunc'd entry.
1298 * RETURN: 0 on success, error code on failure.
1301 * dip - ctime|mtime updated if new entry created
1302 * ip - ctime|mtime always, atime if new
1307 zfs_create(struct inode
*dip
, char *name
, vattr_t
*vap
, int excl
,
1308 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
1310 znode_t
*zp
, *dzp
= ITOZ(dip
);
1311 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1319 zfs_acl_ids_t acl_ids
;
1320 boolean_t fuid_dirtied
;
1321 boolean_t have_acl
= B_FALSE
;
1322 boolean_t waited
= B_FALSE
;
1325 * If we have an ephemeral id, ACL, or XVATTR then
1326 * make sure file system is at proper version
1332 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1333 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1334 return (SET_ERROR(EINVAL
));
1337 return (SET_ERROR(EINVAL
));
1342 zilog
= zfsvfs
->z_log
;
1344 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
1345 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1347 return (SET_ERROR(EILSEQ
));
1350 if (vap
->va_mask
& ATTR_XVATTR
) {
1351 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1352 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1360 if (*name
== '\0') {
1362 * Null component name refers to the directory itself.
1369 /* possible igrab(zp) */
1372 if (flag
& FIGNORECASE
)
1375 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1379 zfs_acl_ids_free(&acl_ids
);
1380 if (strcmp(name
, "..") == 0)
1381 error
= SET_ERROR(EISDIR
);
1389 uint64_t projid
= ZFS_DEFAULT_PROJID
;
1392 * Create a new file object and update the directory
1395 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
1397 zfs_acl_ids_free(&acl_ids
);
1402 * We only support the creation of regular files in
1403 * extended attribute directories.
1406 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
1408 zfs_acl_ids_free(&acl_ids
);
1409 error
= SET_ERROR(EINVAL
);
1413 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1414 cr
, vsecp
, &acl_ids
)) != 0)
1418 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
1419 projid
= zfs_inherit_projid(dzp
);
1420 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
1421 zfs_acl_ids_free(&acl_ids
);
1422 error
= SET_ERROR(EDQUOT
);
1426 tx
= dmu_tx_create(os
);
1428 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1429 ZFS_SA_BASE_ATTR_SIZE
);
1431 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1433 zfs_fuid_txhold(zfsvfs
, tx
);
1434 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1435 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1436 if (!zfsvfs
->z_use_sa
&&
1437 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1438 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1439 0, acl_ids
.z_aclp
->z_acl_bytes
);
1442 error
= dmu_tx_assign(tx
,
1443 (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1445 zfs_dirent_unlock(dl
);
1446 if (error
== ERESTART
) {
1452 zfs_acl_ids_free(&acl_ids
);
1457 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1459 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
1462 * Since, we failed to add the directory entry for it,
1463 * delete the newly created dnode.
1465 zfs_znode_delete(zp
, tx
);
1466 remove_inode_hash(ZTOI(zp
));
1467 zfs_acl_ids_free(&acl_ids
);
1473 zfs_fuid_sync(zfsvfs
, tx
);
1475 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1476 if (flag
& FIGNORECASE
)
1478 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1479 vsecp
, acl_ids
.z_fuidp
, vap
);
1480 zfs_acl_ids_free(&acl_ids
);
1483 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1486 zfs_acl_ids_free(&acl_ids
);
1490 * A directory entry already exists for this name.
1493 * Can't truncate an existing file if in exclusive mode.
1496 error
= SET_ERROR(EEXIST
);
1500 * Can't open a directory for writing.
1502 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
1503 error
= SET_ERROR(EISDIR
);
1507 * Verify requested access to file.
1509 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1513 mutex_enter(&dzp
->z_lock
);
1515 mutex_exit(&dzp
->z_lock
);
1518 * Truncate regular files if requested.
1520 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
1521 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
1522 /* we can't hold any locks when calling zfs_freesp() */
1524 zfs_dirent_unlock(dl
);
1527 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1533 zfs_dirent_unlock(dl
);
1539 zfs_inode_update(dzp
);
1540 zfs_inode_update(zp
);
1544 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1545 zil_commit(zilog
, 0);
1553 zfs_tmpfile(struct inode
*dip
, vattr_t
*vap
, int excl
,
1554 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
1556 znode_t
*zp
= NULL
, *dzp
= ITOZ(dip
);
1557 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1563 zfs_acl_ids_t acl_ids
;
1564 uint64_t projid
= ZFS_DEFAULT_PROJID
;
1565 boolean_t fuid_dirtied
;
1566 boolean_t have_acl
= B_FALSE
;
1567 boolean_t waited
= B_FALSE
;
1570 * If we have an ephemeral id, ACL, or XVATTR then
1571 * make sure file system is at proper version
1577 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1578 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1579 return (SET_ERROR(EINVAL
));
1585 if (vap
->va_mask
& ATTR_XVATTR
) {
1586 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1587 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1597 * Create a new file object and update the directory
1600 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
1602 zfs_acl_ids_free(&acl_ids
);
1606 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1607 cr
, vsecp
, &acl_ids
)) != 0)
1611 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
1612 projid
= zfs_inherit_projid(dzp
);
1613 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
1614 zfs_acl_ids_free(&acl_ids
);
1615 error
= SET_ERROR(EDQUOT
);
1619 tx
= dmu_tx_create(os
);
1621 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1622 ZFS_SA_BASE_ATTR_SIZE
);
1623 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1625 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1627 zfs_fuid_txhold(zfsvfs
, tx
);
1628 if (!zfsvfs
->z_use_sa
&&
1629 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1630 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1631 0, acl_ids
.z_aclp
->z_acl_bytes
);
1633 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1635 if (error
== ERESTART
) {
1641 zfs_acl_ids_free(&acl_ids
);
1646 zfs_mknode(dzp
, vap
, tx
, cr
, IS_TMPFILE
, &zp
, &acl_ids
);
1649 zfs_fuid_sync(zfsvfs
, tx
);
1651 /* Add to unlinked set */
1653 zfs_unlinked_add(zp
, tx
);
1654 zfs_acl_ids_free(&acl_ids
);
1662 zfs_inode_update(dzp
);
1663 zfs_inode_update(zp
);
1672 * Remove an entry from a directory.
1674 * IN: dip - inode of directory to remove entry from.
1675 * name - name of entry to remove.
1676 * cr - credentials of caller.
1678 * RETURN: 0 if success
1679 * error code if failure
1683 * ip - ctime (if nlink > 0)
1686 uint64_t null_xattr
= 0;
1690 zfs_remove(struct inode
*dip
, char *name
, cred_t
*cr
, int flags
)
1692 znode_t
*zp
, *dzp
= ITOZ(dip
);
1695 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1697 uint64_t acl_obj
, xattr_obj
;
1698 uint64_t xattr_obj_unlinked
= 0;
1703 boolean_t may_delete_now
, delete_now
= FALSE
;
1704 boolean_t unlinked
, toobig
= FALSE
;
1706 pathname_t
*realnmp
= NULL
;
1710 boolean_t waited
= B_FALSE
;
1713 return (SET_ERROR(EINVAL
));
1717 zilog
= zfsvfs
->z_log
;
1719 if (flags
& FIGNORECASE
) {
1729 * Attempt to lock directory; fail if entry doesn't exist.
1731 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1741 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1746 * Need to use rmdir for removing directories.
1748 if (S_ISDIR(ip
->i_mode
)) {
1749 error
= SET_ERROR(EPERM
);
1755 dnlc_remove(dvp
, realnmp
->pn_buf
);
1757 dnlc_remove(dvp
, name
);
1758 #endif /* HAVE_DNLC */
1760 mutex_enter(&zp
->z_lock
);
1761 may_delete_now
= atomic_read(&ip
->i_count
) == 1 && !(zp
->z_is_mapped
);
1762 mutex_exit(&zp
->z_lock
);
1765 * We may delete the znode now, or we may put it in the unlinked set;
1766 * it depends on whether we're the last link, and on whether there are
1767 * other holds on the inode. So we dmu_tx_hold() the right things to
1768 * allow for either case.
1771 tx
= dmu_tx_create(zfsvfs
->z_os
);
1772 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1773 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1774 zfs_sa_upgrade_txholds(tx
, zp
);
1775 zfs_sa_upgrade_txholds(tx
, dzp
);
1776 if (may_delete_now
) {
1777 toobig
= zp
->z_size
> zp
->z_blksz
* zfs_delete_blocks
;
1778 /* if the file is too big, only hold_free a token amount */
1779 dmu_tx_hold_free(tx
, zp
->z_id
, 0,
1780 (toobig
? DMU_MAX_ACCESS
: DMU_OBJECT_END
));
1783 /* are there any extended attributes? */
1784 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1785 &xattr_obj
, sizeof (xattr_obj
));
1786 if (error
== 0 && xattr_obj
) {
1787 error
= zfs_zget(zfsvfs
, xattr_obj
, &xzp
);
1789 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1790 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1793 mutex_enter(&zp
->z_lock
);
1794 if ((acl_obj
= zfs_external_acl(zp
)) != 0 && may_delete_now
)
1795 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
1796 mutex_exit(&zp
->z_lock
);
1798 /* charge as an update -- would be nice not to charge at all */
1799 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1802 * Mark this transaction as typically resulting in a net free of space
1804 dmu_tx_mark_netfree(tx
);
1806 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1808 zfs_dirent_unlock(dl
);
1809 if (error
== ERESTART
) {
1829 * Remove the directory entry.
1831 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1840 * Hold z_lock so that we can make sure that the ACL obj
1841 * hasn't changed. Could have been deleted due to
1844 mutex_enter(&zp
->z_lock
);
1845 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1846 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1847 delete_now
= may_delete_now
&& !toobig
&&
1848 atomic_read(&ip
->i_count
) == 1 && !(zp
->z_is_mapped
) &&
1849 xattr_obj
== xattr_obj_unlinked
&& zfs_external_acl(zp
) ==
1854 if (xattr_obj_unlinked
) {
1855 ASSERT3U(ZTOI(xzp
)->i_nlink
, ==, 2);
1856 mutex_enter(&xzp
->z_lock
);
1857 xzp
->z_unlinked
= 1;
1858 clear_nlink(ZTOI(xzp
));
1860 error
= sa_update(xzp
->z_sa_hdl
, SA_ZPL_LINKS(zfsvfs
),
1861 &links
, sizeof (links
), tx
);
1862 ASSERT3U(error
, ==, 0);
1863 mutex_exit(&xzp
->z_lock
);
1864 zfs_unlinked_add(xzp
, tx
);
1867 error
= sa_remove(zp
->z_sa_hdl
,
1868 SA_ZPL_XATTR(zfsvfs
), tx
);
1870 error
= sa_update(zp
->z_sa_hdl
,
1871 SA_ZPL_XATTR(zfsvfs
), &null_xattr
,
1872 sizeof (uint64_t), tx
);
1876 * Add to the unlinked set because a new reference could be
1877 * taken concurrently resulting in a deferred destruction.
1879 zfs_unlinked_add(zp
, tx
);
1880 mutex_exit(&zp
->z_lock
);
1881 } else if (unlinked
) {
1882 mutex_exit(&zp
->z_lock
);
1883 zfs_unlinked_add(zp
, tx
);
1887 if (flags
& FIGNORECASE
)
1889 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
);
1896 zfs_dirent_unlock(dl
);
1897 zfs_inode_update(dzp
);
1898 zfs_inode_update(zp
);
1906 zfs_inode_update(xzp
);
1907 zfs_iput_async(ZTOI(xzp
));
1910 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1911 zil_commit(zilog
, 0);
1918 * Create a new directory and insert it into dip using the name
1919 * provided. Return a pointer to the inserted directory.
1921 * IN: dip - inode of directory to add subdir to.
1922 * dirname - name of new directory.
1923 * vap - attributes of new directory.
1924 * cr - credentials of caller.
1925 * vsecp - ACL to be set
1927 * OUT: ipp - inode of created directory.
1929 * RETURN: 0 if success
1930 * error code if failure
1933 * dip - ctime|mtime updated
1934 * ipp - ctime|mtime|atime updated
1938 zfs_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
, struct inode
**ipp
,
1939 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1941 znode_t
*zp
, *dzp
= ITOZ(dip
);
1942 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1950 gid_t gid
= crgetgid(cr
);
1951 zfs_acl_ids_t acl_ids
;
1952 boolean_t fuid_dirtied
;
1953 boolean_t waited
= B_FALSE
;
1955 ASSERT(S_ISDIR(vap
->va_mode
));
1958 * If we have an ephemeral id, ACL, or XVATTR then
1959 * make sure file system is at proper version
1963 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1964 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1965 return (SET_ERROR(EINVAL
));
1967 if (dirname
== NULL
)
1968 return (SET_ERROR(EINVAL
));
1972 zilog
= zfsvfs
->z_log
;
1974 if (dzp
->z_pflags
& ZFS_XATTR
) {
1976 return (SET_ERROR(EINVAL
));
1979 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1980 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1982 return (SET_ERROR(EILSEQ
));
1984 if (flags
& FIGNORECASE
)
1987 if (vap
->va_mask
& ATTR_XVATTR
) {
1988 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1989 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1995 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1996 vsecp
, &acl_ids
)) != 0) {
2001 * First make sure the new directory doesn't exist.
2003 * Existence is checked first to make sure we don't return
2004 * EACCES instead of EEXIST which can cause some applications
2010 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
2012 zfs_acl_ids_free(&acl_ids
);
2017 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
2018 zfs_acl_ids_free(&acl_ids
);
2019 zfs_dirent_unlock(dl
);
2024 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, zfs_inherit_projid(dzp
))) {
2025 zfs_acl_ids_free(&acl_ids
);
2026 zfs_dirent_unlock(dl
);
2028 return (SET_ERROR(EDQUOT
));
2032 * Add a new entry to the directory.
2034 tx
= dmu_tx_create(zfsvfs
->z_os
);
2035 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
2036 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
2037 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
2039 zfs_fuid_txhold(zfsvfs
, tx
);
2040 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2041 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
2042 acl_ids
.z_aclp
->z_acl_bytes
);
2045 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
2046 ZFS_SA_BASE_ATTR_SIZE
);
2048 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
2050 zfs_dirent_unlock(dl
);
2051 if (error
== ERESTART
) {
2057 zfs_acl_ids_free(&acl_ids
);
2066 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
2069 * Now put new name in parent dir.
2071 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
2073 zfs_znode_delete(zp
, tx
);
2074 remove_inode_hash(ZTOI(zp
));
2079 zfs_fuid_sync(zfsvfs
, tx
);
2083 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
2084 if (flags
& FIGNORECASE
)
2086 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
2087 acl_ids
.z_fuidp
, vap
);
2090 zfs_acl_ids_free(&acl_ids
);
2094 zfs_dirent_unlock(dl
);
2096 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2097 zil_commit(zilog
, 0);
2102 zfs_inode_update(dzp
);
2103 zfs_inode_update(zp
);
2110 * Remove a directory subdir entry. If the current working
2111 * directory is the same as the subdir to be removed, the
2114 * IN: dip - inode of directory to remove from.
2115 * name - name of directory to be removed.
2116 * cwd - inode of current working directory.
2117 * cr - credentials of caller.
2118 * flags - case flags
2120 * RETURN: 0 on success, error code on failure.
2123 * dip - ctime|mtime updated
2127 zfs_rmdir(struct inode
*dip
, char *name
, struct inode
*cwd
, cred_t
*cr
,
2130 znode_t
*dzp
= ITOZ(dip
);
2133 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
2139 boolean_t waited
= B_FALSE
;
2142 return (SET_ERROR(EINVAL
));
2146 zilog
= zfsvfs
->z_log
;
2148 if (flags
& FIGNORECASE
)
2154 * Attempt to lock directory; fail if entry doesn't exist.
2156 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
2164 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
2168 if (!S_ISDIR(ip
->i_mode
)) {
2169 error
= SET_ERROR(ENOTDIR
);
2174 error
= SET_ERROR(EINVAL
);
2179 * Grab a lock on the directory to make sure that no one is
2180 * trying to add (or lookup) entries while we are removing it.
2182 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
2185 * Grab a lock on the parent pointer to make sure we play well
2186 * with the treewalk and directory rename code.
2188 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
2190 tx
= dmu_tx_create(zfsvfs
->z_os
);
2191 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
2192 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2193 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
2194 zfs_sa_upgrade_txholds(tx
, zp
);
2195 zfs_sa_upgrade_txholds(tx
, dzp
);
2196 dmu_tx_mark_netfree(tx
);
2197 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
2199 rw_exit(&zp
->z_parent_lock
);
2200 rw_exit(&zp
->z_name_lock
);
2201 zfs_dirent_unlock(dl
);
2202 if (error
== ERESTART
) {
2215 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
2218 uint64_t txtype
= TX_RMDIR
;
2219 if (flags
& FIGNORECASE
)
2221 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
);
2226 rw_exit(&zp
->z_parent_lock
);
2227 rw_exit(&zp
->z_name_lock
);
2229 zfs_dirent_unlock(dl
);
2231 zfs_inode_update(dzp
);
2232 zfs_inode_update(zp
);
2235 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2236 zil_commit(zilog
, 0);
2243 * Read as many directory entries as will fit into the provided
2244 * dirent buffer from the given directory cursor position.
2246 * IN: ip - inode of directory to read.
2247 * dirent - buffer for directory entries.
2249 * OUT: dirent - filler buffer of directory entries.
2251 * RETURN: 0 if success
2252 * error code if failure
2255 * ip - atime updated
2257 * Note that the low 4 bits of the cookie returned by zap is always zero.
2258 * This allows us to use the low range for "special" directory entries:
2259 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2260 * we use the offset 2 for the '.zfs' directory.
2264 zfs_readdir(struct inode
*ip
, struct dir_context
*ctx
, cred_t
*cr
)
2266 znode_t
*zp
= ITOZ(ip
);
2267 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2270 zap_attribute_t zap
;
2276 uint64_t offset
; /* must be unsigned; checks for < 1 */
2281 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
2282 &parent
, sizeof (parent
))) != 0)
2286 * Quit if directory has been removed (posix)
2294 prefetch
= zp
->z_zn_prefetch
;
2297 * Initialize the iterator cursor.
2301 * Start iteration from the beginning of the directory.
2303 zap_cursor_init(&zc
, os
, zp
->z_id
);
2306 * The offset is a serialized cursor.
2308 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
2312 * Transform to file-system independent format
2317 * Special case `.', `..', and `.zfs'.
2320 (void) strcpy(zap
.za_name
, ".");
2321 zap
.za_normalization_conflict
= 0;
2324 } else if (offset
== 1) {
2325 (void) strcpy(zap
.za_name
, "..");
2326 zap
.za_normalization_conflict
= 0;
2329 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
2330 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
2331 zap
.za_normalization_conflict
= 0;
2332 objnum
= ZFSCTL_INO_ROOT
;
2338 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
2339 if (error
== ENOENT
)
2346 * Allow multiple entries provided the first entry is
2347 * the object id. Non-zpl consumers may safely make
2348 * use of the additional space.
2350 * XXX: This should be a feature flag for compatibility
2352 if (zap
.za_integer_length
!= 8 ||
2353 zap
.za_num_integers
== 0) {
2354 cmn_err(CE_WARN
, "zap_readdir: bad directory "
2355 "entry, obj = %lld, offset = %lld, "
2356 "length = %d, num = %lld\n",
2357 (u_longlong_t
)zp
->z_id
,
2358 (u_longlong_t
)offset
,
2359 zap
.za_integer_length
,
2360 (u_longlong_t
)zap
.za_num_integers
);
2361 error
= SET_ERROR(ENXIO
);
2365 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
2366 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
2369 done
= !dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
2374 /* Prefetch znode */
2376 dmu_prefetch(os
, objnum
, 0, 0, 0,
2377 ZIO_PRIORITY_SYNC_READ
);
2381 * Move to the next entry, fill in the previous offset.
2383 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2384 zap_cursor_advance(&zc
);
2385 offset
= zap_cursor_serialize(&zc
);
2391 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2394 zap_cursor_fini(&zc
);
2395 if (error
== ENOENT
)
2403 ulong_t zfs_fsync_sync_cnt
= 4;
2406 zfs_fsync(struct inode
*ip
, int syncflag
, cred_t
*cr
)
2408 znode_t
*zp
= ITOZ(ip
);
2409 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2411 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2413 if (zfsvfs
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2416 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
2419 tsd_set(zfs_fsyncer_key
, NULL
);
2426 * Get the requested file attributes and place them in the provided
2429 * IN: ip - inode of file.
2430 * vap - va_mask identifies requested attributes.
2431 * If ATTR_XVATTR set, then optional attrs are requested
2432 * flags - ATTR_NOACLCHECK (CIFS server context)
2433 * cr - credentials of caller.
2435 * OUT: vap - attribute values.
2437 * RETURN: 0 (always succeeds)
2441 zfs_getattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2443 znode_t
*zp
= ITOZ(ip
);
2444 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2447 uint64_t atime
[2], mtime
[2], ctime
[2];
2448 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2449 xoptattr_t
*xoap
= NULL
;
2450 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2451 sa_bulk_attr_t bulk
[3];
2457 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2459 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
2460 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
2461 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
2463 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2469 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2470 * Also, if we are the owner don't bother, since owner should
2471 * always be allowed to read basic attributes of file.
2473 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2474 (vap
->va_uid
!= crgetuid(cr
))) {
2475 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2483 * Return all attributes. It's cheaper to provide the answer
2484 * than to determine whether we were asked the question.
2487 mutex_enter(&zp
->z_lock
);
2488 vap
->va_type
= vn_mode_to_vtype(zp
->z_mode
);
2489 vap
->va_mode
= zp
->z_mode
;
2490 vap
->va_fsid
= ZTOI(zp
)->i_sb
->s_dev
;
2491 vap
->va_nodeid
= zp
->z_id
;
2492 if ((zp
->z_id
== zfsvfs
->z_root
) && zfs_show_ctldir(zp
))
2493 links
= ZTOI(zp
)->i_nlink
+ 1;
2495 links
= ZTOI(zp
)->i_nlink
;
2496 vap
->va_nlink
= MIN(links
, ZFS_LINK_MAX
);
2497 vap
->va_size
= i_size_read(ip
);
2498 vap
->va_rdev
= ip
->i_rdev
;
2499 vap
->va_seq
= ip
->i_generation
;
2502 * Add in any requested optional attributes and the create time.
2503 * Also set the corresponding bits in the returned attribute bitmap.
2505 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zfsvfs
->z_use_fuids
) {
2506 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2508 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2509 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2512 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2513 xoap
->xoa_readonly
=
2514 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2515 XVA_SET_RTN(xvap
, XAT_READONLY
);
2518 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2520 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2521 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2524 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2526 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2527 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2530 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2531 xoap
->xoa_nounlink
=
2532 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2533 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2536 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2537 xoap
->xoa_immutable
=
2538 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2539 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2542 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2543 xoap
->xoa_appendonly
=
2544 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2545 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2548 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2550 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2551 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2554 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2556 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2557 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2560 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2561 xoap
->xoa_av_quarantined
=
2562 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2563 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2566 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2567 xoap
->xoa_av_modified
=
2568 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2569 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2572 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2573 S_ISREG(ip
->i_mode
)) {
2574 zfs_sa_get_scanstamp(zp
, xvap
);
2577 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2580 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zfsvfs
),
2581 times
, sizeof (times
));
2582 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2583 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2586 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2587 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2588 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2590 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2591 xoap
->xoa_generation
= ip
->i_generation
;
2592 XVA_SET_RTN(xvap
, XAT_GEN
);
2595 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2597 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2598 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2601 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2603 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2604 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2607 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2608 xoap
->xoa_projinherit
=
2609 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0);
2610 XVA_SET_RTN(xvap
, XAT_PROJINHERIT
);
2613 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2614 xoap
->xoa_projid
= zp
->z_projid
;
2615 XVA_SET_RTN(xvap
, XAT_PROJID
);
2619 ZFS_TIME_DECODE(&vap
->va_atime
, atime
);
2620 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2621 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2623 mutex_exit(&zp
->z_lock
);
2625 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2627 if (zp
->z_blksz
== 0) {
2629 * Block size hasn't been set; suggest maximal I/O transfers.
2631 vap
->va_blksize
= zfsvfs
->z_max_blksz
;
2639 * Get the basic file attributes and place them in the provided kstat
2640 * structure. The inode is assumed to be the authoritative source
2641 * for most of the attributes. However, the znode currently has the
2642 * authoritative atime, blksize, and block count.
2644 * IN: ip - inode of file.
2646 * OUT: sp - kstat values.
2648 * RETURN: 0 (always succeeds)
2652 zfs_getattr_fast(struct inode
*ip
, struct kstat
*sp
)
2654 znode_t
*zp
= ITOZ(ip
);
2655 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2657 u_longlong_t nblocks
;
2662 mutex_enter(&zp
->z_lock
);
2664 generic_fillattr(ip
, sp
);
2666 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
2667 sp
->blksize
= blksize
;
2668 sp
->blocks
= nblocks
;
2670 if (unlikely(zp
->z_blksz
== 0)) {
2672 * Block size hasn't been set; suggest maximal I/O transfers.
2674 sp
->blksize
= zfsvfs
->z_max_blksz
;
2677 mutex_exit(&zp
->z_lock
);
2680 * Required to prevent NFS client from detecting different inode
2681 * numbers of snapshot root dentry before and after snapshot mount.
2683 if (zfsvfs
->z_issnap
) {
2684 if (ip
->i_sb
->s_root
->d_inode
== ip
)
2685 sp
->ino
= ZFSCTL_INO_SNAPDIRS
-
2686 dmu_objset_id(zfsvfs
->z_os
);
2695 * For the operation of changing file's user/group/project, we need to
2696 * handle not only the main object that is assigned to the file directly,
2697 * but also the ones that are used by the file via hidden xattr directory.
2699 * Because the xattr directory may contains many EA entries, as to it may
2700 * be impossible to change all of them via the transaction of changing the
2701 * main object's user/group/project attributes. Then we have to change them
2702 * via other multiple independent transactions one by one. It may be not good
2703 * solution, but we have no better idea yet.
2706 zfs_setattr_dir(znode_t
*dzp
)
2708 struct inode
*dxip
= ZTOI(dzp
);
2709 struct inode
*xip
= NULL
;
2710 zfsvfs_t
*zfsvfs
= ITOZSB(dxip
);
2711 objset_t
*os
= zfsvfs
->z_os
;
2713 zap_attribute_t zap
;
2716 dmu_tx_t
*tx
= NULL
;
2718 sa_bulk_attr_t bulk
[4];
2722 zap_cursor_init(&zc
, os
, dzp
->z_id
);
2723 while ((err
= zap_cursor_retrieve(&zc
, &zap
)) == 0) {
2724 if (zap
.za_integer_length
!= 8 || zap
.za_num_integers
!= 1) {
2729 err
= zfs_dirent_lock(&dl
, dzp
, (char *)zap
.za_name
, &zp
,
2730 ZEXISTS
, NULL
, NULL
);
2737 if (KUID_TO_SUID(xip
->i_uid
) == KUID_TO_SUID(dxip
->i_uid
) &&
2738 KGID_TO_SGID(xip
->i_gid
) == KGID_TO_SGID(dxip
->i_gid
) &&
2739 zp
->z_projid
== dzp
->z_projid
)
2742 tx
= dmu_tx_create(os
);
2743 if (!(zp
->z_pflags
& ZFS_PROJID
))
2744 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2746 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2748 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2752 mutex_enter(&dzp
->z_lock
);
2754 if (KUID_TO_SUID(xip
->i_uid
) != KUID_TO_SUID(dxip
->i_uid
)) {
2755 xip
->i_uid
= dxip
->i_uid
;
2756 uid
= zfs_uid_read(dxip
);
2757 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
2758 &uid
, sizeof (uid
));
2761 if (KGID_TO_SGID(xip
->i_gid
) != KGID_TO_SGID(dxip
->i_gid
)) {
2762 xip
->i_gid
= dxip
->i_gid
;
2763 gid
= zfs_gid_read(dxip
);
2764 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
2765 &gid
, sizeof (gid
));
2768 if (zp
->z_projid
!= dzp
->z_projid
) {
2769 if (!(zp
->z_pflags
& ZFS_PROJID
)) {
2770 zp
->z_pflags
|= ZFS_PROJID
;
2771 SA_ADD_BULK_ATTR(bulk
, count
,
2772 SA_ZPL_FLAGS(zfsvfs
), NULL
, &zp
->z_pflags
,
2773 sizeof (zp
->z_pflags
));
2776 zp
->z_projid
= dzp
->z_projid
;
2777 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PROJID(zfsvfs
),
2778 NULL
, &zp
->z_projid
, sizeof (zp
->z_projid
));
2781 mutex_exit(&dzp
->z_lock
);
2783 if (likely(count
> 0)) {
2784 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
2790 if (err
!= 0 && err
!= ENOENT
)
2797 zfs_dirent_unlock(dl
);
2799 zap_cursor_advance(&zc
);
2806 zfs_dirent_unlock(dl
);
2808 zap_cursor_fini(&zc
);
2810 return (err
== ENOENT
? 0 : err
);
2814 * Set the file attributes to the values contained in the
2817 * IN: ip - inode of file to be modified.
2818 * vap - new attribute values.
2819 * If ATTR_XVATTR set, then optional attrs are being set
2820 * flags - ATTR_UTIME set if non-default time values provided.
2821 * - ATTR_NOACLCHECK (CIFS context only).
2822 * cr - credentials of caller.
2824 * RETURN: 0 if success
2825 * error code if failure
2828 * ip - ctime updated, mtime updated if size changed.
2832 zfs_setattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2834 znode_t
*zp
= ITOZ(ip
);
2835 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2836 objset_t
*os
= zfsvfs
->z_os
;
2840 xvattr_t
*tmpxvattr
;
2841 uint_t mask
= vap
->va_mask
;
2842 uint_t saved_mask
= 0;
2845 uint64_t new_kuid
= 0, new_kgid
= 0, new_uid
, new_gid
;
2847 uint64_t mtime
[2], ctime
[2], atime
[2];
2848 uint64_t projid
= ZFS_INVALID_PROJID
;
2850 int need_policy
= FALSE
;
2852 zfs_fuid_info_t
*fuidp
= NULL
;
2853 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2856 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2857 boolean_t fuid_dirtied
= B_FALSE
;
2858 boolean_t handle_eadir
= B_FALSE
;
2859 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
2860 int count
= 0, xattr_count
= 0, bulks
= 8;
2869 * If this is a xvattr_t, then get a pointer to the structure of
2870 * optional attributes. If this is NULL, then we have a vattr_t.
2872 xoap
= xva_getxoptattr(xvap
);
2873 if (xoap
!= NULL
&& (mask
& ATTR_XVATTR
)) {
2874 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2875 if (!dmu_objset_projectquota_enabled(os
) ||
2876 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
))) {
2878 return (SET_ERROR(ENOTSUP
));
2881 projid
= xoap
->xoa_projid
;
2882 if (unlikely(projid
== ZFS_INVALID_PROJID
)) {
2884 return (SET_ERROR(EINVAL
));
2887 if (projid
== zp
->z_projid
&& zp
->z_pflags
& ZFS_PROJID
)
2888 projid
= ZFS_INVALID_PROJID
;
2893 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
) &&
2894 (!dmu_objset_projectquota_enabled(os
) ||
2895 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
)))) {
2897 return (SET_ERROR(ENOTSUP
));
2901 zilog
= zfsvfs
->z_log
;
2904 * Make sure that if we have ephemeral uid/gid or xvattr specified
2905 * that file system is at proper version level
2908 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
2909 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2910 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2911 (mask
& ATTR_XVATTR
))) {
2913 return (SET_ERROR(EINVAL
));
2916 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
2918 return (SET_ERROR(EISDIR
));
2921 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
2923 return (SET_ERROR(EINVAL
));
2926 tmpxvattr
= kmem_alloc(sizeof (xvattr_t
), KM_SLEEP
);
2927 xva_init(tmpxvattr
);
2929 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
2930 xattr_bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
2933 * Immutable files can only alter immutable bit and atime
2935 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2936 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
2937 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2938 err
= SET_ERROR(EPERM
);
2942 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2943 err
= SET_ERROR(EPERM
);
2948 * Verify timestamps doesn't overflow 32 bits.
2949 * ZFS can handle large timestamps, but 32bit syscalls can't
2950 * handle times greater than 2039. This check should be removed
2951 * once large timestamps are fully supported.
2953 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
2954 if (((mask
& ATTR_ATIME
) &&
2955 TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2956 ((mask
& ATTR_MTIME
) &&
2957 TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2958 err
= SET_ERROR(EOVERFLOW
);
2967 /* Can this be moved to before the top label? */
2968 if (zfs_is_readonly(zfsvfs
)) {
2969 err
= SET_ERROR(EROFS
);
2974 * First validate permissions
2977 if (mask
& ATTR_SIZE
) {
2978 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2983 * XXX - Note, we are not providing any open
2984 * mode flags here (like FNDELAY), so we may
2985 * block if there are locks present... this
2986 * should be addressed in openat().
2988 /* XXX - would it be OK to generate a log record here? */
2989 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2994 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
2995 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2996 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2997 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2998 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2999 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
3000 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
3001 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
3002 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
3006 if (mask
& (ATTR_UID
|ATTR_GID
)) {
3007 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
3012 * NOTE: even if a new mode is being set,
3013 * we may clear S_ISUID/S_ISGID bits.
3016 if (!(mask
& ATTR_MODE
))
3017 vap
->va_mode
= zp
->z_mode
;
3020 * Take ownership or chgrp to group we are a member of
3023 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
3024 take_group
= (mask
& ATTR_GID
) &&
3025 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
3028 * If both ATTR_UID and ATTR_GID are set then take_owner and
3029 * take_group must both be set in order to allow taking
3032 * Otherwise, send the check through secpolicy_vnode_setattr()
3036 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
3037 take_owner
&& take_group
) ||
3038 ((idmask
== ATTR_UID
) && take_owner
) ||
3039 ((idmask
== ATTR_GID
) && take_group
)) {
3040 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
3041 skipaclchk
, cr
) == 0) {
3043 * Remove setuid/setgid for non-privileged users
3045 (void) secpolicy_setid_clear(vap
, cr
);
3046 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
3055 mutex_enter(&zp
->z_lock
);
3056 oldva
.va_mode
= zp
->z_mode
;
3057 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
3058 if (mask
& ATTR_XVATTR
) {
3060 * Update xvattr mask to include only those attributes
3061 * that are actually changing.
3063 * the bits will be restored prior to actually setting
3064 * the attributes so the caller thinks they were set.
3066 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
3067 if (xoap
->xoa_appendonly
!=
3068 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
3071 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
3072 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
3076 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
3077 if (xoap
->xoa_projinherit
!=
3078 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) {
3081 XVA_CLR_REQ(xvap
, XAT_PROJINHERIT
);
3082 XVA_SET_REQ(tmpxvattr
, XAT_PROJINHERIT
);
3086 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
3087 if (xoap
->xoa_nounlink
!=
3088 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
3091 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
3092 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
3096 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
3097 if (xoap
->xoa_immutable
!=
3098 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
3101 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
3102 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
3106 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
3107 if (xoap
->xoa_nodump
!=
3108 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
3111 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
3112 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
3116 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
3117 if (xoap
->xoa_av_modified
!=
3118 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
3121 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
3122 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
3126 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
3127 if ((!S_ISREG(ip
->i_mode
) &&
3128 xoap
->xoa_av_quarantined
) ||
3129 xoap
->xoa_av_quarantined
!=
3130 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
3133 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
3134 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
3138 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
3139 mutex_exit(&zp
->z_lock
);
3140 err
= SET_ERROR(EPERM
);
3144 if (need_policy
== FALSE
&&
3145 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
3146 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
3151 mutex_exit(&zp
->z_lock
);
3153 if (mask
& ATTR_MODE
) {
3154 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
3155 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
3160 trim_mask
|= ATTR_MODE
;
3168 * If trim_mask is set then take ownership
3169 * has been granted or write_acl is present and user
3170 * has the ability to modify mode. In that case remove
3171 * UID|GID and or MODE from mask so that
3172 * secpolicy_vnode_setattr() doesn't revoke it.
3176 saved_mask
= vap
->va_mask
;
3177 vap
->va_mask
&= ~trim_mask
;
3179 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
3180 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
3185 vap
->va_mask
|= saved_mask
;
3189 * secpolicy_vnode_setattr, or take ownership may have
3192 mask
= vap
->va_mask
;
3194 if ((mask
& (ATTR_UID
| ATTR_GID
)) || projid
!= ZFS_INVALID_PROJID
) {
3195 handle_eadir
= B_TRUE
;
3196 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
3197 &xattr_obj
, sizeof (xattr_obj
));
3199 if (err
== 0 && xattr_obj
) {
3200 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
3204 if (mask
& ATTR_UID
) {
3205 new_kuid
= zfs_fuid_create(zfsvfs
,
3206 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
3207 if (new_kuid
!= KUID_TO_SUID(ZTOI(zp
)->i_uid
) &&
3208 zfs_id_overquota(zfsvfs
, DMU_USERUSED_OBJECT
,
3212 err
= SET_ERROR(EDQUOT
);
3217 if (mask
& ATTR_GID
) {
3218 new_kgid
= zfs_fuid_create(zfsvfs
,
3219 (uint64_t)vap
->va_gid
, cr
, ZFS_GROUP
, &fuidp
);
3220 if (new_kgid
!= KGID_TO_SGID(ZTOI(zp
)->i_gid
) &&
3221 zfs_id_overquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
3225 err
= SET_ERROR(EDQUOT
);
3230 if (projid
!= ZFS_INVALID_PROJID
&&
3231 zfs_id_overquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
, projid
)) {
3238 tx
= dmu_tx_create(os
);
3240 if (mask
& ATTR_MODE
) {
3241 uint64_t pmode
= zp
->z_mode
;
3243 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
3245 zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
);
3247 mutex_enter(&zp
->z_lock
);
3248 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
3250 * Are we upgrading ACL from old V0 format
3253 if (zfsvfs
->z_version
>= ZPL_VERSION_FUID
&&
3254 zfs_znode_acl_version(zp
) ==
3255 ZFS_ACL_VERSION_INITIAL
) {
3256 dmu_tx_hold_free(tx
, acl_obj
, 0,
3258 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
3259 0, aclp
->z_acl_bytes
);
3261 dmu_tx_hold_write(tx
, acl_obj
, 0,
3264 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3265 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
3266 0, aclp
->z_acl_bytes
);
3268 mutex_exit(&zp
->z_lock
);
3269 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
3271 if (((mask
& ATTR_XVATTR
) &&
3272 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) ||
3273 (projid
!= ZFS_INVALID_PROJID
&&
3274 !(zp
->z_pflags
& ZFS_PROJID
)))
3275 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
3277 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3281 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
3284 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3286 zfs_fuid_txhold(zfsvfs
, tx
);
3288 zfs_sa_upgrade_txholds(tx
, zp
);
3290 err
= dmu_tx_assign(tx
, TXG_WAIT
);
3296 * Set each attribute requested.
3297 * We group settings according to the locks they need to acquire.
3299 * Note: you cannot set ctime directly, although it will be
3300 * updated as a side-effect of calling this function.
3303 if (projid
!= ZFS_INVALID_PROJID
&& !(zp
->z_pflags
& ZFS_PROJID
)) {
3305 * For the existed object that is upgraded from old system,
3306 * its on-disk layout has no slot for the project ID attribute.
3307 * But quota accounting logic needs to access related slots by
3308 * offset directly. So we need to adjust old objects' layout
3309 * to make the project ID to some unified and fixed offset.
3312 err
= sa_add_projid(attrzp
->z_sa_hdl
, tx
, projid
);
3314 err
= sa_add_projid(zp
->z_sa_hdl
, tx
, projid
);
3316 if (unlikely(err
== EEXIST
))
3321 projid
= ZFS_INVALID_PROJID
;
3324 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3325 mutex_enter(&zp
->z_acl_lock
);
3326 mutex_enter(&zp
->z_lock
);
3328 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
3329 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
3332 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3333 mutex_enter(&attrzp
->z_acl_lock
);
3334 mutex_enter(&attrzp
->z_lock
);
3335 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3336 SA_ZPL_FLAGS(zfsvfs
), NULL
, &attrzp
->z_pflags
,
3337 sizeof (attrzp
->z_pflags
));
3338 if (projid
!= ZFS_INVALID_PROJID
) {
3339 attrzp
->z_projid
= projid
;
3340 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3341 SA_ZPL_PROJID(zfsvfs
), NULL
, &attrzp
->z_projid
,
3342 sizeof (attrzp
->z_projid
));
3346 if (mask
& (ATTR_UID
|ATTR_GID
)) {
3348 if (mask
& ATTR_UID
) {
3349 ZTOI(zp
)->i_uid
= SUID_TO_KUID(new_kuid
);
3350 new_uid
= zfs_uid_read(ZTOI(zp
));
3351 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
3352 &new_uid
, sizeof (new_uid
));
3354 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3355 SA_ZPL_UID(zfsvfs
), NULL
, &new_uid
,
3357 ZTOI(attrzp
)->i_uid
= SUID_TO_KUID(new_uid
);
3361 if (mask
& ATTR_GID
) {
3362 ZTOI(zp
)->i_gid
= SGID_TO_KGID(new_kgid
);
3363 new_gid
= zfs_gid_read(ZTOI(zp
));
3364 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
),
3365 NULL
, &new_gid
, sizeof (new_gid
));
3367 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3368 SA_ZPL_GID(zfsvfs
), NULL
, &new_gid
,
3370 ZTOI(attrzp
)->i_gid
= SGID_TO_KGID(new_kgid
);
3373 if (!(mask
& ATTR_MODE
)) {
3374 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
),
3375 NULL
, &new_mode
, sizeof (new_mode
));
3376 new_mode
= zp
->z_mode
;
3378 err
= zfs_acl_chown_setattr(zp
);
3381 err
= zfs_acl_chown_setattr(attrzp
);
3386 if (mask
& ATTR_MODE
) {
3387 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
3388 &new_mode
, sizeof (new_mode
));
3389 zp
->z_mode
= ZTOI(zp
)->i_mode
= new_mode
;
3390 ASSERT3P(aclp
, !=, NULL
);
3391 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
3393 if (zp
->z_acl_cached
)
3394 zfs_acl_free(zp
->z_acl_cached
);
3395 zp
->z_acl_cached
= aclp
;
3399 if ((mask
& ATTR_ATIME
) || zp
->z_atime_dirty
) {
3400 zp
->z_atime_dirty
= 0;
3401 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
3402 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
3403 &atime
, sizeof (atime
));
3406 if (mask
& (ATTR_MTIME
| ATTR_SIZE
)) {
3407 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
3408 ZTOI(zp
)->i_mtime
= timespec_trunc(vap
->va_mtime
,
3409 ZTOI(zp
)->i_sb
->s_time_gran
);
3411 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
3412 mtime
, sizeof (mtime
));
3415 if (mask
& (ATTR_CTIME
| ATTR_SIZE
)) {
3416 ZFS_TIME_ENCODE(&vap
->va_ctime
, ctime
);
3417 ZTOI(zp
)->i_ctime
= timespec_trunc(vap
->va_ctime
,
3418 ZTOI(zp
)->i_sb
->s_time_gran
);
3419 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
3420 ctime
, sizeof (ctime
));
3423 if (projid
!= ZFS_INVALID_PROJID
) {
3424 zp
->z_projid
= projid
;
3425 SA_ADD_BULK_ATTR(bulk
, count
,
3426 SA_ZPL_PROJID(zfsvfs
), NULL
, &zp
->z_projid
,
3427 sizeof (zp
->z_projid
));
3430 if (attrzp
&& mask
) {
3431 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3432 SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
,
3437 * Do this after setting timestamps to prevent timestamp
3438 * update from toggling bit
3441 if (xoap
&& (mask
& ATTR_XVATTR
)) {
3444 * restore trimmed off masks
3445 * so that return masks can be set for caller.
3448 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
3449 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
3451 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
3452 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
3454 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
3455 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
3457 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
3458 XVA_SET_REQ(xvap
, XAT_NODUMP
);
3460 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
3461 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
3463 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
3464 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
3466 if (XVA_ISSET_REQ(tmpxvattr
, XAT_PROJINHERIT
)) {
3467 XVA_SET_REQ(xvap
, XAT_PROJINHERIT
);
3470 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
3471 ASSERT(S_ISREG(ip
->i_mode
));
3473 zfs_xvattr_set(zp
, xvap
, tx
);
3477 zfs_fuid_sync(zfsvfs
, tx
);
3480 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
3482 mutex_exit(&zp
->z_lock
);
3483 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3484 mutex_exit(&zp
->z_acl_lock
);
3487 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3488 mutex_exit(&attrzp
->z_acl_lock
);
3489 mutex_exit(&attrzp
->z_lock
);
3492 if (err
== 0 && xattr_count
> 0) {
3493 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3502 zfs_fuid_info_free(fuidp
);
3510 if (err
== ERESTART
)
3514 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3517 if (err2
== 0 && handle_eadir
)
3518 err2
= zfs_setattr_dir(attrzp
);
3521 zfs_inode_update(zp
);
3525 if (os
->os_sync
== ZFS_SYNC_ALWAYS
)
3526 zil_commit(zilog
, 0);
3529 kmem_free(xattr_bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
3530 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
3531 kmem_free(tmpxvattr
, sizeof (xvattr_t
));
3536 typedef struct zfs_zlock
{
3537 krwlock_t
*zl_rwlock
; /* lock we acquired */
3538 znode_t
*zl_znode
; /* znode we held */
3539 struct zfs_zlock
*zl_next
; /* next in list */
3543 * Drop locks and release vnodes that were held by zfs_rename_lock().
3546 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3550 while ((zl
= *zlpp
) != NULL
) {
3551 if (zl
->zl_znode
!= NULL
)
3552 zfs_iput_async(ZTOI(zl
->zl_znode
));
3553 rw_exit(zl
->zl_rwlock
);
3554 *zlpp
= zl
->zl_next
;
3555 kmem_free(zl
, sizeof (*zl
));
3560 * Search back through the directory tree, using the ".." entries.
3561 * Lock each directory in the chain to prevent concurrent renames.
3562 * Fail any attempt to move a directory into one of its own descendants.
3563 * XXX - z_parent_lock can overlap with map or grow locks
3566 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3570 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
3571 uint64_t oidp
= zp
->z_id
;
3572 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3573 krw_t rw
= RW_WRITER
;
3576 * First pass write-locks szp and compares to zp->z_id.
3577 * Later passes read-lock zp and compare to zp->z_parent.
3580 if (!rw_tryenter(rwlp
, rw
)) {
3582 * Another thread is renaming in this path.
3583 * Note that if we are a WRITER, we don't have any
3584 * parent_locks held yet.
3586 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3588 * Drop our locks and restart
3590 zfs_rename_unlock(&zl
);
3594 rwlp
= &szp
->z_parent_lock
;
3599 * Wait for other thread to drop its locks
3605 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3606 zl
->zl_rwlock
= rwlp
;
3607 zl
->zl_znode
= NULL
;
3608 zl
->zl_next
= *zlpp
;
3611 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3612 return (SET_ERROR(EINVAL
));
3614 if (oidp
== rootid
) /* We've hit the top */
3617 if (rw
== RW_READER
) { /* i.e. not the first pass */
3618 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
3623 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
3624 &oidp
, sizeof (oidp
));
3625 rwlp
= &zp
->z_parent_lock
;
3628 } while (zp
->z_id
!= sdzp
->z_id
);
3634 * Move an entry from the provided source directory to the target
3635 * directory. Change the entry name as indicated.
3637 * IN: sdip - Source directory containing the "old entry".
3638 * snm - Old entry name.
3639 * tdip - Target directory to contain the "new entry".
3640 * tnm - New entry name.
3641 * cr - credentials of caller.
3642 * flags - case flags
3644 * RETURN: 0 on success, error code on failure.
3647 * sdip,tdip - ctime|mtime updated
3651 zfs_rename(struct inode
*sdip
, char *snm
, struct inode
*tdip
, char *tnm
,
3652 cred_t
*cr
, int flags
)
3654 znode_t
*tdzp
, *szp
, *tzp
;
3655 znode_t
*sdzp
= ITOZ(sdip
);
3656 zfsvfs_t
*zfsvfs
= ITOZSB(sdip
);
3658 zfs_dirlock_t
*sdl
, *tdl
;
3661 int cmp
, serr
, terr
;
3664 boolean_t waited
= B_FALSE
;
3666 if (snm
== NULL
|| tnm
== NULL
)
3667 return (SET_ERROR(EINVAL
));
3670 ZFS_VERIFY_ZP(sdzp
);
3671 zilog
= zfsvfs
->z_log
;
3674 ZFS_VERIFY_ZP(tdzp
);
3677 * We check i_sb because snapshots and the ctldir must have different
3680 if (tdip
->i_sb
!= sdip
->i_sb
|| zfsctl_is_node(tdip
)) {
3682 return (SET_ERROR(EXDEV
));
3685 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
3686 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3688 return (SET_ERROR(EILSEQ
));
3691 if (flags
& FIGNORECASE
)
3700 * This is to prevent the creation of links into attribute space
3701 * by renaming a linked file into/outof an attribute directory.
3702 * See the comment in zfs_link() for why this is considered bad.
3704 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3706 return (SET_ERROR(EINVAL
));
3710 * Lock source and target directory entries. To prevent deadlock,
3711 * a lock ordering must be defined. We lock the directory with
3712 * the smallest object id first, or if it's a tie, the one with
3713 * the lexically first name.
3715 if (sdzp
->z_id
< tdzp
->z_id
) {
3717 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3721 * First compare the two name arguments without
3722 * considering any case folding.
3724 int nofold
= (zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3726 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3727 ASSERT(error
== 0 || !zfsvfs
->z_utf8
);
3730 * POSIX: "If the old argument and the new argument
3731 * both refer to links to the same existing file,
3732 * the rename() function shall return successfully
3733 * and perform no other action."
3739 * If the file system is case-folding, then we may
3740 * have some more checking to do. A case-folding file
3741 * system is either supporting mixed case sensitivity
3742 * access or is completely case-insensitive. Note
3743 * that the file system is always case preserving.
3745 * In mixed sensitivity mode case sensitive behavior
3746 * is the default. FIGNORECASE must be used to
3747 * explicitly request case insensitive behavior.
3749 * If the source and target names provided differ only
3750 * by case (e.g., a request to rename 'tim' to 'Tim'),
3751 * we will treat this as a special case in the
3752 * case-insensitive mode: as long as the source name
3753 * is an exact match, we will allow this to proceed as
3754 * a name-change request.
3756 if ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
3757 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
3758 flags
& FIGNORECASE
)) &&
3759 u8_strcmp(snm
, tnm
, 0, zfsvfs
->z_norm
, U8_UNICODE_LATEST
,
3762 * case preserving rename request, require exact
3771 * If the source and destination directories are the same, we should
3772 * grab the z_name_lock of that directory only once.
3776 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3780 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3781 ZEXISTS
| zflg
, NULL
, NULL
);
3782 terr
= zfs_dirent_lock(&tdl
,
3783 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3785 terr
= zfs_dirent_lock(&tdl
,
3786 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3787 serr
= zfs_dirent_lock(&sdl
,
3788 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3794 * Source entry invalid or not there.
3797 zfs_dirent_unlock(tdl
);
3803 rw_exit(&sdzp
->z_name_lock
);
3805 if (strcmp(snm
, "..") == 0)
3811 zfs_dirent_unlock(sdl
);
3815 rw_exit(&sdzp
->z_name_lock
);
3817 if (strcmp(tnm
, "..") == 0)
3824 * If we are using project inheritance, means if the directory has
3825 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3826 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3827 * such case, we only allow renames into our tree when the project
3830 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3831 tdzp
->z_projid
!= szp
->z_projid
) {
3832 error
= SET_ERROR(EXDEV
);
3837 * Must have write access at the source to remove the old entry
3838 * and write access at the target to create the new entry.
3839 * Note that if target and source are the same, this can be
3840 * done in a single check.
3843 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
3846 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3848 * Check to make sure rename is valid.
3849 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3851 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
3856 * Does target exist?
3860 * Source and target must be the same type.
3862 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3863 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3864 error
= SET_ERROR(ENOTDIR
);
3868 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3869 error
= SET_ERROR(EISDIR
);
3874 * POSIX dictates that when the source and target
3875 * entries refer to the same file object, rename
3876 * must do nothing and exit without error.
3878 if (szp
->z_id
== tzp
->z_id
) {
3884 tx
= dmu_tx_create(zfsvfs
->z_os
);
3885 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3886 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3887 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3888 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3890 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3891 zfs_sa_upgrade_txholds(tx
, tdzp
);
3894 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3895 zfs_sa_upgrade_txholds(tx
, tzp
);
3898 zfs_sa_upgrade_txholds(tx
, szp
);
3899 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3900 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
3903 zfs_rename_unlock(&zl
);
3904 zfs_dirent_unlock(sdl
);
3905 zfs_dirent_unlock(tdl
);
3908 rw_exit(&sdzp
->z_name_lock
);
3910 if (error
== ERESTART
) {
3927 if (tzp
) /* Attempt to remove the existing target */
3928 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3931 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3933 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3934 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
)
3935 szp
->z_pflags
|= ZFS_PROJINHERIT
;
3937 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
3938 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3941 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3943 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3944 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
3945 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3948 * At this point, we have successfully created
3949 * the target name, but have failed to remove
3950 * the source name. Since the create was done
3951 * with the ZRENAMING flag, there are
3952 * complications; for one, the link count is
3953 * wrong. The easiest way to deal with this
3954 * is to remove the newly created target, and
3955 * return the original error. This must
3956 * succeed; fortunately, it is very unlikely to
3957 * fail, since we just created it.
3959 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3960 ZRENAMING
, NULL
), ==, 0);
3964 * If we had removed the existing target, subsequent
3965 * call to zfs_link_create() to add back the same entry
3966 * but, the new dnode (szp) should not fail.
3968 ASSERT(tzp
== NULL
);
3975 zfs_rename_unlock(&zl
);
3977 zfs_dirent_unlock(sdl
);
3978 zfs_dirent_unlock(tdl
);
3980 zfs_inode_update(sdzp
);
3982 rw_exit(&sdzp
->z_name_lock
);
3985 zfs_inode_update(tdzp
);
3987 zfs_inode_update(szp
);
3990 zfs_inode_update(tzp
);
3994 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3995 zil_commit(zilog
, 0);
4002 * Insert the indicated symbolic reference entry into the directory.
4004 * IN: dip - Directory to contain new symbolic link.
4005 * link - Name for new symlink entry.
4006 * vap - Attributes of new entry.
4007 * target - Target path of new symlink.
4009 * cr - credentials of caller.
4010 * flags - case flags
4012 * RETURN: 0 on success, error code on failure.
4015 * dip - ctime|mtime updated
4019 zfs_symlink(struct inode
*dip
, char *name
, vattr_t
*vap
, char *link
,
4020 struct inode
**ipp
, cred_t
*cr
, int flags
)
4022 znode_t
*zp
, *dzp
= ITOZ(dip
);
4025 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
4027 uint64_t len
= strlen(link
);
4030 zfs_acl_ids_t acl_ids
;
4031 boolean_t fuid_dirtied
;
4032 uint64_t txtype
= TX_SYMLINK
;
4033 boolean_t waited
= B_FALSE
;
4035 ASSERT(S_ISLNK(vap
->va_mode
));
4038 return (SET_ERROR(EINVAL
));
4042 zilog
= zfsvfs
->z_log
;
4044 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
4045 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
4047 return (SET_ERROR(EILSEQ
));
4049 if (flags
& FIGNORECASE
)
4052 if (len
> MAXPATHLEN
) {
4054 return (SET_ERROR(ENAMETOOLONG
));
4057 if ((error
= zfs_acl_ids_create(dzp
, 0,
4058 vap
, cr
, NULL
, &acl_ids
)) != 0) {
4066 * Attempt to lock directory; fail if entry already exists.
4068 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
4070 zfs_acl_ids_free(&acl_ids
);
4075 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
4076 zfs_acl_ids_free(&acl_ids
);
4077 zfs_dirent_unlock(dl
);
4082 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, ZFS_DEFAULT_PROJID
)) {
4083 zfs_acl_ids_free(&acl_ids
);
4084 zfs_dirent_unlock(dl
);
4086 return (SET_ERROR(EDQUOT
));
4088 tx
= dmu_tx_create(zfsvfs
->z_os
);
4089 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
4090 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
4091 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
4092 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
4093 ZFS_SA_BASE_ATTR_SIZE
+ len
);
4094 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
4095 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
4096 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
4097 acl_ids
.z_aclp
->z_acl_bytes
);
4100 zfs_fuid_txhold(zfsvfs
, tx
);
4101 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
4103 zfs_dirent_unlock(dl
);
4104 if (error
== ERESTART
) {
4110 zfs_acl_ids_free(&acl_ids
);
4117 * Create a new object for the symlink.
4118 * for version 4 ZPL datsets the symlink will be an SA attribute
4120 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
4123 zfs_fuid_sync(zfsvfs
, tx
);
4125 mutex_enter(&zp
->z_lock
);
4127 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zfsvfs
),
4130 zfs_sa_symlink(zp
, link
, len
, tx
);
4131 mutex_exit(&zp
->z_lock
);
4134 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
4135 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
4137 * Insert the new object into the directory.
4139 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
4141 zfs_znode_delete(zp
, tx
);
4142 remove_inode_hash(ZTOI(zp
));
4144 if (flags
& FIGNORECASE
)
4146 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
4148 zfs_inode_update(dzp
);
4149 zfs_inode_update(zp
);
4152 zfs_acl_ids_free(&acl_ids
);
4156 zfs_dirent_unlock(dl
);
4161 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4162 zil_commit(zilog
, 0);
4172 * Return, in the buffer contained in the provided uio structure,
4173 * the symbolic path referred to by ip.
4175 * IN: ip - inode of symbolic link
4176 * uio - structure to contain the link path.
4177 * cr - credentials of caller.
4179 * RETURN: 0 if success
4180 * error code if failure
4183 * ip - atime updated
4187 zfs_readlink(struct inode
*ip
, uio_t
*uio
, cred_t
*cr
)
4189 znode_t
*zp
= ITOZ(ip
);
4190 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4196 mutex_enter(&zp
->z_lock
);
4198 error
= sa_lookup_uio(zp
->z_sa_hdl
,
4199 SA_ZPL_SYMLINK(zfsvfs
), uio
);
4201 error
= zfs_sa_readlink(zp
, uio
);
4202 mutex_exit(&zp
->z_lock
);
4209 * Insert a new entry into directory tdip referencing sip.
4211 * IN: tdip - Directory to contain new entry.
4212 * sip - inode of new entry.
4213 * name - name of new entry.
4214 * cr - credentials of caller.
4216 * RETURN: 0 if success
4217 * error code if failure
4220 * tdip - ctime|mtime updated
4221 * sip - ctime updated
4225 zfs_link(struct inode
*tdip
, struct inode
*sip
, char *name
, cred_t
*cr
,
4228 znode_t
*dzp
= ITOZ(tdip
);
4230 zfsvfs_t
*zfsvfs
= ITOZSB(tdip
);
4238 boolean_t waited
= B_FALSE
;
4239 boolean_t is_tmpfile
= 0;
4242 is_tmpfile
= (sip
->i_nlink
== 0 && (sip
->i_state
& I_LINKABLE
));
4244 ASSERT(S_ISDIR(tdip
->i_mode
));
4247 return (SET_ERROR(EINVAL
));
4251 zilog
= zfsvfs
->z_log
;
4254 * POSIX dictates that we return EPERM here.
4255 * Better choices include ENOTSUP or EISDIR.
4257 if (S_ISDIR(sip
->i_mode
)) {
4259 return (SET_ERROR(EPERM
));
4266 * If we are using project inheritance, means if the directory has
4267 * ZFS_PROJINHERIT set, then its descendant directories will inherit
4268 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
4269 * such case, we only allow hard link creation in our tree when the
4270 * project IDs are the same.
4272 if (dzp
->z_pflags
& ZFS_PROJINHERIT
&& dzp
->z_projid
!= szp
->z_projid
) {
4274 return (SET_ERROR(EXDEV
));
4278 * We check i_sb because snapshots and the ctldir must have different
4281 if (sip
->i_sb
!= tdip
->i_sb
|| zfsctl_is_node(sip
)) {
4283 return (SET_ERROR(EXDEV
));
4286 /* Prevent links to .zfs/shares files */
4288 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
4289 &parent
, sizeof (uint64_t))) != 0) {
4293 if (parent
== zfsvfs
->z_shares_dir
) {
4295 return (SET_ERROR(EPERM
));
4298 if (zfsvfs
->z_utf8
&& u8_validate(name
,
4299 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
4301 return (SET_ERROR(EILSEQ
));
4303 if (flags
& FIGNORECASE
)
4307 * We do not support links between attributes and non-attributes
4308 * because of the potential security risk of creating links
4309 * into "normal" file space in order to circumvent restrictions
4310 * imposed in attribute space.
4312 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
4314 return (SET_ERROR(EINVAL
));
4317 owner
= zfs_fuid_map_id(zfsvfs
, KUID_TO_SUID(sip
->i_uid
),
4319 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
4321 return (SET_ERROR(EPERM
));
4324 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
4331 * Attempt to lock directory; fail if entry already exists.
4333 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
4339 tx
= dmu_tx_create(zfsvfs
->z_os
);
4340 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
4341 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
4343 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
4345 zfs_sa_upgrade_txholds(tx
, szp
);
4346 zfs_sa_upgrade_txholds(tx
, dzp
);
4347 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
4349 zfs_dirent_unlock(dl
);
4350 if (error
== ERESTART
) {
4360 /* unmark z_unlinked so zfs_link_create will not reject */
4362 szp
->z_unlinked
= 0;
4363 error
= zfs_link_create(dl
, szp
, tx
, 0);
4366 uint64_t txtype
= TX_LINK
;
4368 * tmpfile is created to be in z_unlinkedobj, so remove it.
4369 * Also, we don't log in ZIL, be cause all previous file
4370 * operation on the tmpfile are ignored by ZIL. Instead we
4371 * always wait for txg to sync to make sure all previous
4372 * operation are sync safe.
4375 VERIFY(zap_remove_int(zfsvfs
->z_os
,
4376 zfsvfs
->z_unlinkedobj
, szp
->z_id
, tx
) == 0);
4378 if (flags
& FIGNORECASE
)
4380 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
4382 } else if (is_tmpfile
) {
4383 /* restore z_unlinked since when linking failed */
4384 szp
->z_unlinked
= 1;
4386 txg
= dmu_tx_get_txg(tx
);
4389 zfs_dirent_unlock(dl
);
4391 if (!is_tmpfile
&& zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4392 zil_commit(zilog
, 0);
4395 txg_wait_synced(dmu_objset_pool(zfsvfs
->z_os
), txg
);
4397 zfs_inode_update(dzp
);
4398 zfs_inode_update(szp
);
4404 zfs_putpage_commit_cb(void *arg
)
4406 struct page
*pp
= arg
;
4409 end_page_writeback(pp
);
4413 * Push a page out to disk, once the page is on stable storage the
4414 * registered commit callback will be run as notification of completion.
4416 * IN: ip - page mapped for inode.
4417 * pp - page to push (page is locked)
4418 * wbc - writeback control data
4420 * RETURN: 0 if success
4421 * error code if failure
4424 * ip - ctime|mtime updated
4428 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
4430 znode_t
*zp
= ITOZ(ip
);
4431 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4439 uint64_t mtime
[2], ctime
[2];
4440 sa_bulk_attr_t bulk
[3];
4442 struct address_space
*mapping
;
4447 ASSERT(PageLocked(pp
));
4449 pgoff
= page_offset(pp
); /* Page byte-offset in file */
4450 offset
= i_size_read(ip
); /* File length in bytes */
4451 pglen
= MIN(PAGE_SIZE
, /* Page length in bytes */
4452 P2ROUNDUP(offset
, PAGE_SIZE
)-pgoff
);
4454 /* Page is beyond end of file */
4455 if (pgoff
>= offset
) {
4461 /* Truncate page length to end of file */
4462 if (pgoff
+ pglen
> offset
)
4463 pglen
= offset
- pgoff
;
4467 * FIXME: Allow mmap writes past its quota. The correct fix
4468 * is to register a page_mkwrite() handler to count the page
4469 * against its quota when it is about to be dirtied.
4471 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
,
4472 KUID_TO_SUID(ip
->i_uid
)) ||
4473 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
4474 KGID_TO_SGID(ip
->i_gid
)) ||
4475 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
4476 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
4483 * The ordering here is critical and must adhere to the following
4484 * rules in order to avoid deadlocking in either zfs_read() or
4485 * zfs_free_range() due to a lock inversion.
4487 * 1) The page must be unlocked prior to acquiring the range lock.
4488 * This is critical because zfs_read() calls find_lock_page()
4489 * which may block on the page lock while holding the range lock.
4491 * 2) Before setting or clearing write back on a page the range lock
4492 * must be held in order to prevent a lock inversion with the
4493 * zfs_free_range() function.
4495 * This presents a problem because upon entering this function the
4496 * page lock is already held. To safely acquire the range lock the
4497 * page lock must be dropped. This creates a window where another
4498 * process could truncate, invalidate, dirty, or write out the page.
4500 * Therefore, after successfully reacquiring the range and page locks
4501 * the current page state is checked. In the common case everything
4502 * will be as is expected and it can be written out. However, if
4503 * the page state has changed it must be handled accordingly.
4505 mapping
= pp
->mapping
;
4506 redirty_page_for_writepage(wbc
, pp
);
4509 rl
= zfs_range_lock(&zp
->z_range_lock
, pgoff
, pglen
, RL_WRITER
);
4512 /* Page mapping changed or it was no longer dirty, we're done */
4513 if (unlikely((mapping
!= pp
->mapping
) || !PageDirty(pp
))) {
4515 zfs_range_unlock(rl
);
4520 /* Another process started write block if required */
4521 if (PageWriteback(pp
)) {
4523 zfs_range_unlock(rl
);
4525 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
4526 wait_on_page_writeback(pp
);
4532 /* Clear the dirty flag the required locks are held */
4533 if (!clear_page_dirty_for_io(pp
)) {
4535 zfs_range_unlock(rl
);
4541 * Counterpart for redirty_page_for_writepage() above. This page
4542 * was in fact not skipped and should not be counted as if it were.
4544 wbc
->pages_skipped
--;
4545 set_page_writeback(pp
);
4548 tx
= dmu_tx_create(zfsvfs
->z_os
);
4549 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
4550 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4551 zfs_sa_upgrade_txholds(tx
, zp
);
4553 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
4555 if (err
== ERESTART
)
4559 __set_page_dirty_nobuffers(pp
);
4561 end_page_writeback(pp
);
4562 zfs_range_unlock(rl
);
4568 ASSERT3U(pglen
, <=, PAGE_SIZE
);
4569 dmu_write(zfsvfs
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
4572 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
4573 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
4574 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
4577 /* Preserve the mtime and ctime provided by the inode */
4578 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4579 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4580 zp
->z_atime_dirty
= 0;
4583 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4585 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
4586 zfs_putpage_commit_cb
, pp
);
4589 zfs_range_unlock(rl
);
4591 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
4593 * Note that this is rarely called under writepages(), because
4594 * writepages() normally handles the entire commit for
4595 * performance reasons.
4597 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
4605 * Update the system attributes when the inode has been dirtied. For the
4606 * moment we only update the mode, atime, mtime, and ctime.
4609 zfs_dirty_inode(struct inode
*ip
, int flags
)
4611 znode_t
*zp
= ITOZ(ip
);
4612 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4614 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
4615 sa_bulk_attr_t bulk
[4];
4619 if (zfs_is_readonly(zfsvfs
) || dmu_objset_is_snapshot(zfsvfs
->z_os
))
4627 * This is the lazytime semantic indroduced in Linux 4.0
4628 * This flag will only be called from update_time when lazytime is set.
4629 * (Note, I_DIRTY_SYNC will also set if not lazytime)
4630 * Fortunately mtime and ctime are managed within ZFS itself, so we
4631 * only need to dirty atime.
4633 if (flags
== I_DIRTY_TIME
) {
4634 zp
->z_atime_dirty
= 1;
4639 tx
= dmu_tx_create(zfsvfs
->z_os
);
4641 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4642 zfs_sa_upgrade_txholds(tx
, zp
);
4644 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4650 mutex_enter(&zp
->z_lock
);
4651 zp
->z_atime_dirty
= 0;
4653 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zfsvfs
), NULL
, &mode
, 8);
4654 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
4655 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
4656 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
4658 /* Preserve the mode, mtime and ctime provided by the inode */
4659 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4660 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4661 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4666 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4667 mutex_exit(&zp
->z_lock
);
4677 zfs_inactive(struct inode
*ip
)
4679 znode_t
*zp
= ITOZ(ip
);
4680 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4683 int need_unlock
= 0;
4685 /* Only read lock if we haven't already write locked, e.g. rollback */
4686 if (!RW_WRITE_HELD(&zfsvfs
->z_teardown_inactive_lock
)) {
4688 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_READER
);
4690 if (zp
->z_sa_hdl
== NULL
) {
4692 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
4696 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4697 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
4699 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4700 zfs_sa_upgrade_txholds(tx
, zp
);
4701 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4705 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4706 mutex_enter(&zp
->z_lock
);
4707 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
),
4708 (void *)&atime
, sizeof (atime
), tx
);
4709 zp
->z_atime_dirty
= 0;
4710 mutex_exit(&zp
->z_lock
);
4717 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
4721 * Bounds-check the seek operation.
4723 * IN: ip - inode seeking within
4724 * ooff - old file offset
4725 * noffp - pointer to new file offset
4726 * ct - caller context
4728 * RETURN: 0 if success
4729 * EINVAL if new offset invalid
4733 zfs_seek(struct inode
*ip
, offset_t ooff
, offset_t
*noffp
)
4735 if (S_ISDIR(ip
->i_mode
))
4737 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4741 * Fill pages with data from the disk.
4744 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4746 znode_t
*zp
= ITOZ(ip
);
4747 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4749 struct page
*cur_pp
;
4750 u_offset_t io_off
, total
;
4757 io_len
= nr_pages
<< PAGE_SHIFT
;
4758 i_size
= i_size_read(ip
);
4759 io_off
= page_offset(pl
[0]);
4761 if (io_off
+ io_len
> i_size
)
4762 io_len
= i_size
- io_off
;
4765 * Iterate over list of pages and read each page individually.
4768 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4771 cur_pp
= pl
[page_idx
++];
4773 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4777 /* convert checksum errors into IO errors */
4779 err
= SET_ERROR(EIO
);
4788 * Uses zfs_fillpage to read data from the file and fill the pages.
4790 * IN: ip - inode of file to get data from.
4791 * pl - list of pages to read
4792 * nr_pages - number of pages to read
4794 * RETURN: 0 on success, error code on failure.
4797 * vp - atime updated
4801 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4803 znode_t
*zp
= ITOZ(ip
);
4804 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4813 err
= zfs_fillpage(ip
, pl
, nr_pages
);
4820 * Check ZFS specific permissions to memory map a section of a file.
4822 * IN: ip - inode of the file to mmap
4824 * addrp - start address in memory region
4825 * len - length of memory region
4826 * vm_flags- address flags
4828 * RETURN: 0 if success
4829 * error code if failure
4833 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
4834 unsigned long vm_flags
)
4836 znode_t
*zp
= ITOZ(ip
);
4837 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4842 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
4843 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4845 return (SET_ERROR(EPERM
));
4848 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
4849 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4851 return (SET_ERROR(EACCES
));
4854 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4856 return (SET_ERROR(ENXIO
));
4864 * convoff - converts the given data (start, whence) to the
4868 convoff(struct inode
*ip
, flock64_t
*lckdat
, int whence
, offset_t offset
)
4873 if ((lckdat
->l_whence
== 2) || (whence
== 2)) {
4874 if ((error
= zfs_getattr(ip
, &vap
, 0, CRED())))
4878 switch (lckdat
->l_whence
) {
4880 lckdat
->l_start
+= offset
;
4883 lckdat
->l_start
+= vap
.va_size
;
4888 return (SET_ERROR(EINVAL
));
4891 if (lckdat
->l_start
< 0)
4892 return (SET_ERROR(EINVAL
));
4896 lckdat
->l_start
-= offset
;
4899 lckdat
->l_start
-= vap
.va_size
;
4904 return (SET_ERROR(EINVAL
));
4907 lckdat
->l_whence
= (short)whence
;
4912 * Free or allocate space in a file. Currently, this function only
4913 * supports the `F_FREESP' command. However, this command is somewhat
4914 * misnamed, as its functionality includes the ability to allocate as
4915 * well as free space.
4917 * IN: ip - inode of file to free data in.
4918 * cmd - action to take (only F_FREESP supported).
4919 * bfp - section of file to free/alloc.
4920 * flag - current file open mode flags.
4921 * offset - current file offset.
4922 * cr - credentials of caller [UNUSED].
4924 * RETURN: 0 on success, error code on failure.
4927 * ip - ctime|mtime updated
4931 zfs_space(struct inode
*ip
, int cmd
, flock64_t
*bfp
, int flag
,
4932 offset_t offset
, cred_t
*cr
)
4934 znode_t
*zp
= ITOZ(ip
);
4935 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4942 if (cmd
!= F_FREESP
) {
4944 return (SET_ERROR(EINVAL
));
4948 * Callers might not be able to detect properly that we are read-only,
4949 * so check it explicitly here.
4951 if (zfs_is_readonly(zfsvfs
)) {
4953 return (SET_ERROR(EROFS
));
4956 if ((error
= convoff(ip
, bfp
, 0, offset
))) {
4961 if (bfp
->l_len
< 0) {
4963 return (SET_ERROR(EINVAL
));
4967 * Permissions aren't checked on Solaris because on this OS
4968 * zfs_space() can only be called with an opened file handle.
4969 * On Linux we can get here through truncate_range() which
4970 * operates directly on inodes, so we need to check access rights.
4972 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
4978 len
= bfp
->l_len
; /* 0 means from off to end of file */
4980 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4988 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4990 znode_t
*zp
= ITOZ(ip
);
4991 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4994 uint64_t object
= zp
->z_id
;
5001 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
),
5002 &gen64
, sizeof (uint64_t))) != 0) {
5007 gen
= (uint32_t)gen64
;
5009 size
= SHORT_FID_LEN
;
5011 zfid
= (zfid_short_t
*)fidp
;
5013 zfid
->zf_len
= size
;
5015 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
5016 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
5018 /* Must have a non-zero generation number to distinguish from .zfs */
5021 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
5022 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
5030 zfs_getsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
5032 znode_t
*zp
= ITOZ(ip
);
5033 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
5035 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
5039 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
5047 zfs_setsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
5049 znode_t
*zp
= ITOZ(ip
);
5050 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
5052 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
5053 zilog_t
*zilog
= zfsvfs
->z_log
;
5058 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
5060 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
5061 zil_commit(zilog
, 0);
5067 #ifdef HAVE_UIO_ZEROCOPY
5069 * Tunable, both must be a power of 2.
5071 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
5072 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
5073 * an arcbuf for a partial block read
5075 int zcr_blksz_min
= (1 << 10); /* 1K */
5076 int zcr_blksz_max
= (1 << 17); /* 128K */
5080 zfs_reqzcbuf(struct inode
*ip
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
)
5082 znode_t
*zp
= ITOZ(ip
);
5083 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
5084 int max_blksz
= zfsvfs
->z_max_blksz
;
5085 uio_t
*uio
= &xuio
->xu_uio
;
5086 ssize_t size
= uio
->uio_resid
;
5087 offset_t offset
= uio
->uio_loffset
;
5092 int preamble
, postamble
;
5094 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
5095 return (SET_ERROR(EINVAL
));
5102 * Loan out an arc_buf for write if write size is bigger than
5103 * max_blksz, and the file's block size is also max_blksz.
5106 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
5108 return (SET_ERROR(EINVAL
));
5111 * Caller requests buffers for write before knowing where the
5112 * write offset might be (e.g. NFS TCP write).
5117 preamble
= P2PHASE(offset
, blksz
);
5119 preamble
= blksz
- preamble
;
5124 postamble
= P2PHASE(size
, blksz
);
5127 fullblk
= size
/ blksz
;
5128 (void) dmu_xuio_init(xuio
,
5129 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
5132 * Have to fix iov base/len for partial buffers. They
5133 * currently represent full arc_buf's.
5136 /* data begins in the middle of the arc_buf */
5137 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5140 (void) dmu_xuio_add(xuio
, abuf
,
5141 blksz
- preamble
, preamble
);
5144 for (i
= 0; i
< fullblk
; i
++) {
5145 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5148 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
5152 /* data ends in the middle of the arc_buf */
5153 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5156 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
5161 * Loan out an arc_buf for read if the read size is larger than
5162 * the current file block size. Block alignment is not
5163 * considered. Partial arc_buf will be loaned out for read.
5165 blksz
= zp
->z_blksz
;
5166 if (blksz
< zcr_blksz_min
)
5167 blksz
= zcr_blksz_min
;
5168 if (blksz
> zcr_blksz_max
)
5169 blksz
= zcr_blksz_max
;
5170 /* avoid potential complexity of dealing with it */
5171 if (blksz
> max_blksz
) {
5173 return (SET_ERROR(EINVAL
));
5176 maxsize
= zp
->z_size
- uio
->uio_loffset
;
5182 return (SET_ERROR(EINVAL
));
5187 return (SET_ERROR(EINVAL
));
5190 uio
->uio_extflg
= UIO_XUIO
;
5191 XUIO_XUZC_RW(xuio
) = ioflag
;
5198 zfs_retzcbuf(struct inode
*ip
, xuio_t
*xuio
, cred_t
*cr
)
5202 int ioflag
= XUIO_XUZC_RW(xuio
);
5204 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
5206 i
= dmu_xuio_cnt(xuio
);
5208 abuf
= dmu_xuio_arcbuf(xuio
, i
);
5210 * if abuf == NULL, it must be a write buffer
5211 * that has been returned in zfs_write().
5214 dmu_return_arcbuf(abuf
);
5215 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
5218 dmu_xuio_fini(xuio
);
5221 #endif /* HAVE_UIO_ZEROCOPY */
5223 #if defined(_KERNEL) && defined(HAVE_SPL)
5224 EXPORT_SYMBOL(zfs_open
);
5225 EXPORT_SYMBOL(zfs_close
);
5226 EXPORT_SYMBOL(zfs_read
);
5227 EXPORT_SYMBOL(zfs_write
);
5228 EXPORT_SYMBOL(zfs_access
);
5229 EXPORT_SYMBOL(zfs_lookup
);
5230 EXPORT_SYMBOL(zfs_create
);
5231 EXPORT_SYMBOL(zfs_tmpfile
);
5232 EXPORT_SYMBOL(zfs_remove
);
5233 EXPORT_SYMBOL(zfs_mkdir
);
5234 EXPORT_SYMBOL(zfs_rmdir
);
5235 EXPORT_SYMBOL(zfs_readdir
);
5236 EXPORT_SYMBOL(zfs_fsync
);
5237 EXPORT_SYMBOL(zfs_getattr
);
5238 EXPORT_SYMBOL(zfs_getattr_fast
);
5239 EXPORT_SYMBOL(zfs_setattr
);
5240 EXPORT_SYMBOL(zfs_rename
);
5241 EXPORT_SYMBOL(zfs_symlink
);
5242 EXPORT_SYMBOL(zfs_readlink
);
5243 EXPORT_SYMBOL(zfs_link
);
5244 EXPORT_SYMBOL(zfs_inactive
);
5245 EXPORT_SYMBOL(zfs_space
);
5246 EXPORT_SYMBOL(zfs_fid
);
5247 EXPORT_SYMBOL(zfs_getsecattr
);
5248 EXPORT_SYMBOL(zfs_setsecattr
);
5249 EXPORT_SYMBOL(zfs_getpage
);
5250 EXPORT_SYMBOL(zfs_putpage
);
5251 EXPORT_SYMBOL(zfs_dirty_inode
);
5252 EXPORT_SYMBOL(zfs_map
);
5255 module_param(zfs_delete_blocks
, ulong
, 0644);
5256 MODULE_PARM_DESC(zfs_delete_blocks
, "Delete files larger than N blocks async");
5257 module_param(zfs_read_chunk_size
, long, 0644);
5258 MODULE_PARM_DESC(zfs_read_chunk_size
, "Bytes to read per chunk");