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/sysmacros.h>
41 #include <sys/taskq.h>
43 #include <sys/vmsystm.h>
44 #include <sys/atomic.h>
45 #include <sys/pathname.h>
46 #include <sys/cmn_err.h>
47 #include <sys/errno.h>
48 #include <sys/zfs_dir.h>
49 #include <sys/zfs_acl.h>
50 #include <sys/zfs_ioctl.h>
51 #include <sys/fs/zfs.h>
53 #include <sys/dmu_objset.h>
59 #include <sys/policy.h>
60 #include <sys/sunddi.h>
63 #include <sys/zfs_ctldir.h>
64 #include <sys/zfs_fuid.h>
65 #include <sys/zfs_sa.h>
66 #include <sys/zfs_vnops.h>
67 #include <sys/zfs_rlock.h>
71 #include <sys/sa_impl.h>
76 * Each vnode op performs some logical unit of work. To do this, the ZPL must
77 * properly lock its in-core state, create a DMU transaction, do the work,
78 * record this work in the intent log (ZIL), commit the DMU transaction,
79 * and wait for the intent log to commit if it is a synchronous operation.
80 * Moreover, the vnode ops must work in both normal and log replay context.
81 * The ordering of events is important to avoid deadlocks and references
82 * to freed memory. The example below illustrates the following Big Rules:
84 * (1) A check must be made in each zfs thread for a mounted file system.
85 * This is done avoiding races using ZFS_ENTER(zfsvfs).
86 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
87 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
88 * can return EIO from the calling function.
90 * (2) iput() should always be the last thing except for zil_commit()
91 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
92 * First, if it's the last reference, the vnode/znode
93 * can be freed, so the zp may point to freed memory. Second, the last
94 * reference will call zfs_zinactive(), which may induce a lot of work --
95 * pushing cached pages (which acquires range locks) and syncing out
96 * cached atime changes. Third, zfs_zinactive() may require a new tx,
97 * which could deadlock the system if you were already holding one.
98 * If you must call iput() within a tx then use zfs_iput_async().
100 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
101 * as they can span dmu_tx_assign() calls.
103 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
104 * dmu_tx_assign(). This is critical because we don't want to block
105 * while holding locks.
107 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
108 * reduces lock contention and CPU usage when we must wait (note that if
109 * throughput is constrained by the storage, nearly every transaction
112 * Note, in particular, that if a lock is sometimes acquired before
113 * the tx assigns, and sometimes after (e.g. z_lock), then failing
114 * to use a non-blocking assign can deadlock the system. The scenario:
116 * Thread A has grabbed a lock before calling dmu_tx_assign().
117 * Thread B is in an already-assigned tx, and blocks for this lock.
118 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
119 * forever, because the previous txg can't quiesce until B's tx commits.
121 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
122 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
123 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
124 * to indicate that this operation has already called dmu_tx_wait().
125 * This will ensure that we don't retry forever, waiting a short bit
128 * (5) If the operation succeeded, generate the intent log entry for it
129 * before dropping locks. This ensures that the ordering of events
130 * in the intent log matches the order in which they actually occurred.
131 * During ZIL replay the zfs_log_* functions will update the sequence
132 * number to indicate the zil transaction has replayed.
134 * (6) At the end of each vnode op, the DMU tx must always commit,
135 * regardless of whether there were any errors.
137 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
138 * to ensure that synchronous semantics are provided when necessary.
140 * In general, this is how things should be ordered in each vnode op:
142 * ZFS_ENTER(zfsvfs); // exit if unmounted
144 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
145 * rw_enter(...); // grab any other locks you need
146 * tx = dmu_tx_create(...); // get DMU tx
147 * dmu_tx_hold_*(); // hold each object you might modify
148 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
150 * rw_exit(...); // drop locks
151 * zfs_dirent_unlock(dl); // unlock directory entry
152 * iput(...); // release held vnodes
153 * if (error == ERESTART) {
159 * dmu_tx_abort(tx); // abort DMU tx
160 * ZFS_EXIT(zfsvfs); // finished in zfs
161 * return (error); // really out of space
163 * error = do_real_work(); // do whatever this VOP does
165 * zfs_log_*(...); // on success, make ZIL entry
166 * dmu_tx_commit(tx); // commit DMU tx -- error or not
167 * rw_exit(...); // drop locks
168 * zfs_dirent_unlock(dl); // unlock directory entry
169 * iput(...); // release held vnodes
170 * zil_commit(zilog, foid); // synchronous when necessary
171 * ZFS_EXIT(zfsvfs); // finished in zfs
172 * return (error); // done, report error
176 * Virus scanning is unsupported. It would be possible to add a hook
177 * here to performance the required virus scan. This could be done
178 * entirely in the kernel or potentially as an update to invoke a
182 zfs_vscan(struct inode
*ip
, cred_t
*cr
, int async
)
189 zfs_open(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
191 znode_t
*zp
= ITOZ(ip
);
192 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
197 /* Honor ZFS_APPENDONLY file attribute */
198 if ((mode
& FMODE_WRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
199 ((flag
& O_APPEND
) == 0)) {
201 return (SET_ERROR(EPERM
));
204 /* Virus scan eligible files on open */
205 if (!zfs_has_ctldir(zp
) && zfsvfs
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
206 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0) {
207 if (zfs_vscan(ip
, cr
, 0) != 0) {
209 return (SET_ERROR(EACCES
));
213 /* Keep a count of the synchronous opens in the znode */
215 atomic_inc_32(&zp
->z_sync_cnt
);
223 zfs_close(struct inode
*ip
, int flag
, cred_t
*cr
)
225 znode_t
*zp
= ITOZ(ip
);
226 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
231 /* Decrement the synchronous opens in the znode */
233 atomic_dec_32(&zp
->z_sync_cnt
);
235 if (!zfs_has_ctldir(zp
) && zfsvfs
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
236 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0)
237 VERIFY(zfs_vscan(ip
, cr
, 1) == 0);
243 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
245 * Lseek support for finding holes (cmd == SEEK_HOLE) and
246 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
249 zfs_holey_common(struct inode
*ip
, int cmd
, loff_t
*off
)
251 znode_t
*zp
= ITOZ(ip
);
252 uint64_t noff
= (uint64_t)*off
; /* new offset */
257 file_sz
= zp
->z_size
;
258 if (noff
>= file_sz
) {
259 return (SET_ERROR(ENXIO
));
262 if (cmd
== SEEK_HOLE
)
267 error
= dmu_offset_next(ZTOZSB(zp
)->z_os
, zp
->z_id
, hole
, &noff
);
270 return (SET_ERROR(ENXIO
));
272 /* file was dirty, so fall back to using generic logic */
273 if (error
== EBUSY
) {
281 * We could find a hole that begins after the logical end-of-file,
282 * because dmu_offset_next() only works on whole blocks. If the
283 * EOF falls mid-block, then indicate that the "virtual hole"
284 * at the end of the file begins at the logical EOF, rather than
285 * at the end of the last block.
287 if (noff
> file_sz
) {
299 zfs_holey(struct inode
*ip
, int cmd
, loff_t
*off
)
301 znode_t
*zp
= ITOZ(ip
);
302 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
308 error
= zfs_holey_common(ip
, cmd
, off
);
313 #endif /* SEEK_HOLE && SEEK_DATA */
317 * When a file is memory mapped, we must keep the IO data synchronized
318 * between the DMU cache and the memory mapped pages. What this means:
320 * On Write: If we find a memory mapped page, we write to *both*
321 * the page and the dmu buffer.
324 update_pages(struct inode
*ip
, int64_t start
, int len
,
325 objset_t
*os
, uint64_t oid
)
327 struct address_space
*mp
= ip
->i_mapping
;
333 off
= start
& (PAGE_SIZE
-1);
334 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
335 nbytes
= MIN(PAGE_SIZE
- off
, len
);
337 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
339 if (mapping_writably_mapped(mp
))
340 flush_dcache_page(pp
);
343 (void) dmu_read(os
, oid
, start
+off
, nbytes
, pb
+off
,
347 if (mapping_writably_mapped(mp
))
348 flush_dcache_page(pp
);
350 mark_page_accessed(pp
);
363 * When a file is memory mapped, we must keep the IO data synchronized
364 * between the DMU cache and the memory mapped pages. What this means:
366 * On Read: We "read" preferentially from memory mapped pages,
367 * else we default from the dmu buffer.
369 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
370 * the file is memory mapped.
373 mappedread(struct inode
*ip
, int nbytes
, uio_t
*uio
)
375 struct address_space
*mp
= ip
->i_mapping
;
377 znode_t
*zp
= ITOZ(ip
);
384 start
= uio
->uio_loffset
;
385 off
= start
& (PAGE_SIZE
-1);
386 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
387 bytes
= MIN(PAGE_SIZE
- off
, len
);
389 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
391 ASSERT(PageUptodate(pp
));
395 error
= uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
398 if (mapping_writably_mapped(mp
))
399 flush_dcache_page(pp
);
401 mark_page_accessed(pp
);
404 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
417 unsigned long zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
418 unsigned long zfs_delete_blocks
= DMU_MAX_DELETEBLKCNT
;
421 * Read bytes from specified file into supplied buffer.
423 * IN: ip - inode of file to be read from.
424 * uio - structure supplying read location, range info,
426 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
427 * O_DIRECT flag; used to bypass page cache.
428 * cr - credentials of caller.
430 * OUT: uio - updated offset and range, buffer filled.
432 * RETURN: 0 on success, error code on failure.
435 * inode - atime updated if byte count > 0
439 zfs_read(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
441 znode_t
*zp
= ITOZ(ip
);
442 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
446 #ifdef HAVE_UIO_ZEROCOPY
448 #endif /* HAVE_UIO_ZEROCOPY */
453 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
455 return (SET_ERROR(EACCES
));
459 * Validate file offset
461 if (uio
->uio_loffset
< (offset_t
)0) {
463 return (SET_ERROR(EINVAL
));
467 * Fasttrack empty reads
469 if (uio
->uio_resid
== 0) {
475 * If we're in FRSYNC mode, sync out this znode before reading it.
476 * Only do this for non-snapshots.
479 (ioflag
& FRSYNC
|| zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
))
480 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
483 * Lock the range against changes.
485 rl
= zfs_range_lock(&zp
->z_range_lock
, uio
->uio_loffset
, uio
->uio_resid
,
489 * If we are reading past end-of-file we can skip
490 * to the end; but we might still need to set atime.
492 if (uio
->uio_loffset
>= zp
->z_size
) {
497 ASSERT(uio
->uio_loffset
< zp
->z_size
);
498 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
500 #ifdef HAVE_UIO_ZEROCOPY
501 if ((uio
->uio_extflg
== UIO_XUIO
) &&
502 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
504 int blksz
= zp
->z_blksz
;
505 uint64_t offset
= uio
->uio_loffset
;
507 xuio
= (xuio_t
*)uio
;
509 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
512 ASSERT(offset
+ n
<= blksz
);
515 (void) dmu_xuio_init(xuio
, nblk
);
517 if (vn_has_cached_data(ip
)) {
519 * For simplicity, we always allocate a full buffer
520 * even if we only expect to read a portion of a block.
522 while (--nblk
>= 0) {
523 (void) dmu_xuio_add(xuio
,
524 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
529 #endif /* HAVE_UIO_ZEROCOPY */
532 nbytes
= MIN(n
, zfs_read_chunk_size
-
533 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
535 if (zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
)) {
536 error
= mappedread(ip
, nbytes
, uio
);
538 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
543 /* convert checksum errors into IO errors */
545 error
= SET_ERROR(EIO
);
552 zfs_range_unlock(rl
);
559 * Write the bytes to a file.
561 * IN: ip - inode of file to be written to.
562 * uio - structure supplying write location, range info,
564 * ioflag - FAPPEND flag set if in append mode.
565 * O_DIRECT flag; used to bypass page cache.
566 * cr - credentials of caller.
568 * OUT: uio - updated offset and range.
570 * RETURN: 0 if success
571 * error code if failure
574 * ip - ctime|mtime updated if byte count > 0
579 zfs_write(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
581 znode_t
*zp
= ITOZ(ip
);
582 rlim64_t limit
= uio
->uio_limit
;
583 ssize_t start_resid
= uio
->uio_resid
;
587 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
592 int max_blksz
= zfsvfs
->z_max_blksz
;
595 const iovec_t
*aiov
= NULL
;
599 sa_bulk_attr_t bulk
[4];
600 uint64_t mtime
[2], ctime
[2];
602 #ifdef HAVE_UIO_ZEROCOPY
604 const iovec_t
*iovp
= uio
->uio_iov
;
605 ASSERTV(int iovcnt
= uio
->uio_iovcnt
);
609 * Fasttrack empty write
615 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
621 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
622 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
623 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
625 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
629 * Callers might not be able to detect properly that we are read-only,
630 * so check it explicitly here.
632 if (zfs_is_readonly(zfsvfs
)) {
634 return (SET_ERROR(EROFS
));
638 * If immutable or not appending then return EPERM
640 if ((zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
641 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
642 (uio
->uio_loffset
< zp
->z_size
))) {
644 return (SET_ERROR(EPERM
));
647 zilog
= zfsvfs
->z_log
;
650 * Validate file offset
652 woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
655 return (SET_ERROR(EINVAL
));
659 * Pre-fault the pages to ensure slow (eg NFS) pages
661 * Skip this if uio contains loaned arc_buf.
663 #ifdef HAVE_UIO_ZEROCOPY
664 if ((uio
->uio_extflg
== UIO_XUIO
) &&
665 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
666 xuio
= (xuio_t
*)uio
;
669 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
672 * If in append mode, set the io offset pointer to eof.
674 if (ioflag
& FAPPEND
) {
676 * Obtain an appending range lock to guarantee file append
677 * semantics. We reset the write offset once we have the lock.
679 rl
= zfs_range_lock(&zp
->z_range_lock
, 0, n
, RL_APPEND
);
681 if (rl
->r_len
== UINT64_MAX
) {
683 * We overlocked the file because this write will cause
684 * the file block size to increase.
685 * Note that zp_size cannot change with this lock held.
689 uio
->uio_loffset
= woff
;
692 * Note that if the file block size will change as a result of
693 * this write, then this range lock will lock the entire file
694 * so that we can re-write the block safely.
696 rl
= zfs_range_lock(&zp
->z_range_lock
, woff
, n
, RL_WRITER
);
700 zfs_range_unlock(rl
);
702 return (SET_ERROR(EFBIG
));
705 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
708 /* Will this write extend the file length? */
709 write_eof
= (woff
+ n
> zp
->z_size
);
711 end_size
= MAX(zp
->z_size
, woff
+ n
);
714 * Write the file in reasonable size chunks. Each chunk is written
715 * in a separate transaction; this keeps the intent log records small
716 * and allows us to do more fine-grained space accounting.
720 woff
= uio
->uio_loffset
;
721 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
,
722 KUID_TO_SUID(ip
->i_uid
)) ||
723 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
724 KGID_TO_SGID(ip
->i_gid
)) ||
725 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
726 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
729 dmu_return_arcbuf(abuf
);
730 error
= SET_ERROR(EDQUOT
);
734 if (xuio
&& abuf
== NULL
) {
735 #ifdef HAVE_UIO_ZEROCOPY
736 ASSERT(i_iov
< iovcnt
);
737 ASSERT3U(uio
->uio_segflg
, !=, UIO_BVEC
);
739 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
740 dmu_xuio_clear(xuio
, i_iov
);
741 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
742 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
743 aiov
->iov_len
== arc_buf_size(abuf
)));
746 } else if (abuf
== NULL
&& n
>= max_blksz
&&
747 woff
>= zp
->z_size
&&
748 P2PHASE(woff
, max_blksz
) == 0 &&
749 zp
->z_blksz
== max_blksz
) {
751 * This write covers a full block. "Borrow" a buffer
752 * from the dmu so that we can fill it before we enter
753 * a transaction. This avoids the possibility of
754 * holding up the transaction if the data copy hangs
755 * up on a pagefault (e.g., from an NFS server mapping).
759 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
761 ASSERT(abuf
!= NULL
);
762 ASSERT(arc_buf_size(abuf
) == max_blksz
);
763 if ((error
= uiocopy(abuf
->b_data
, max_blksz
,
764 UIO_WRITE
, uio
, &cbytes
))) {
765 dmu_return_arcbuf(abuf
);
768 ASSERT(cbytes
== max_blksz
);
772 * Start a transaction.
774 tx
= dmu_tx_create(zfsvfs
->z_os
);
775 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
776 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
777 zfs_sa_upgrade_txholds(tx
, zp
);
778 error
= dmu_tx_assign(tx
, TXG_WAIT
);
782 dmu_return_arcbuf(abuf
);
787 * If zfs_range_lock() over-locked we grow the blocksize
788 * and then reduce the lock range. This will only happen
789 * on the first iteration since zfs_range_reduce() will
790 * shrink down r_len to the appropriate size.
792 if (rl
->r_len
== UINT64_MAX
) {
795 if (zp
->z_blksz
> max_blksz
) {
797 * File's blocksize is already larger than the
798 * "recordsize" property. Only let it grow to
799 * the next power of 2.
801 ASSERT(!ISP2(zp
->z_blksz
));
802 new_blksz
= MIN(end_size
,
803 1 << highbit64(zp
->z_blksz
));
805 new_blksz
= MIN(end_size
, max_blksz
);
807 zfs_grow_blocksize(zp
, new_blksz
, tx
);
808 zfs_range_reduce(rl
, woff
, n
);
812 * XXX - should we really limit each write to z_max_blksz?
813 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
815 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
818 tx_bytes
= uio
->uio_resid
;
819 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
821 tx_bytes
-= uio
->uio_resid
;
824 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
826 * If this is not a full block write, but we are
827 * extending the file past EOF and this data starts
828 * block-aligned, use assign_arcbuf(). Otherwise,
829 * write via dmu_write().
831 if (tx_bytes
< max_blksz
&& (!write_eof
||
832 aiov
->iov_base
!= abuf
->b_data
)) {
834 dmu_write(zfsvfs
->z_os
, zp
->z_id
, woff
,
835 /* cppcheck-suppress nullPointer */
836 aiov
->iov_len
, aiov
->iov_base
, tx
);
837 dmu_return_arcbuf(abuf
);
838 xuio_stat_wbuf_copied();
840 ASSERT(xuio
|| tx_bytes
== max_blksz
);
841 dmu_assign_arcbuf_by_dbuf(
842 sa_get_db(zp
->z_sa_hdl
), woff
, abuf
, tx
);
844 ASSERT(tx_bytes
<= uio
->uio_resid
);
845 uioskip(uio
, tx_bytes
);
847 if (tx_bytes
&& zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
)) {
848 update_pages(ip
, woff
,
849 tx_bytes
, zfsvfs
->z_os
, zp
->z_id
);
853 * If we made no progress, we're done. If we made even
854 * partial progress, update the znode and ZIL accordingly.
857 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
858 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
865 * Clear Set-UID/Set-GID bits on successful write if not
866 * privileged and at least one of the execute bits is set.
868 * It would be nice to to this after all writes have
869 * been done, but that would still expose the ISUID/ISGID
870 * to another app after the partial write is committed.
872 * Note: we don't call zfs_fuid_map_id() here because
873 * user 0 is not an ephemeral uid.
875 mutex_enter(&zp
->z_acl_lock
);
876 uid
= KUID_TO_SUID(ip
->i_uid
);
877 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
878 (S_IXUSR
>> 6))) != 0 &&
879 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
880 secpolicy_vnode_setid_retain(cr
,
881 ((zp
->z_mode
& S_ISUID
) != 0 && uid
== 0)) != 0) {
883 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
884 ip
->i_mode
= newmode
= zp
->z_mode
;
885 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zfsvfs
),
886 (void *)&newmode
, sizeof (uint64_t), tx
);
888 mutex_exit(&zp
->z_acl_lock
);
890 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
893 * Update the file size (zp_size) if it has changed;
894 * account for possible concurrent updates.
896 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
897 (void) atomic_cas_64(&zp
->z_size
, end_size
,
902 * If we are replaying and eof is non zero then force
903 * the file size to the specified eof. Note, there's no
904 * concurrency during replay.
906 if (zfsvfs
->z_replay
&& zfsvfs
->z_replay_eof
!= 0)
907 zp
->z_size
= zfsvfs
->z_replay_eof
;
909 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
911 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
,
917 ASSERT(tx_bytes
== nbytes
);
921 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
924 zfs_inode_update(zp
);
925 zfs_range_unlock(rl
);
928 * If we're in replay mode, or we made no progress, return error.
929 * Otherwise, it's at least a partial write, so it's successful.
931 if (zfsvfs
->z_replay
|| uio
->uio_resid
== start_resid
) {
936 if (ioflag
& (FSYNC
| FDSYNC
) ||
937 zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
938 zil_commit(zilog
, zp
->z_id
);
945 * Drop a reference on the passed inode asynchronously. This ensures
946 * that the caller will never drop the last reference on an inode in
947 * the current context. Doing so while holding open a tx could result
948 * in a deadlock if iput_final() re-enters the filesystem code.
951 zfs_iput_async(struct inode
*ip
)
953 objset_t
*os
= ITOZSB(ip
)->z_os
;
955 ASSERT(atomic_read(&ip
->i_count
) > 0);
958 if (atomic_read(&ip
->i_count
) == 1)
959 VERIFY(taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os
)),
960 (task_func_t
*)iput
, ip
, TQ_SLEEP
) != TASKQID_INVALID
);
966 zfs_get_done(zgd_t
*zgd
, int error
)
968 znode_t
*zp
= zgd
->zgd_private
;
971 dmu_buf_rele(zgd
->zgd_db
, zgd
);
973 zfs_range_unlock(zgd
->zgd_rl
);
976 * Release the vnode asynchronously as we currently have the
977 * txg stopped from syncing.
979 zfs_iput_async(ZTOI(zp
));
981 if (error
== 0 && zgd
->zgd_bp
)
982 zil_lwb_add_block(zgd
->zgd_lwb
, zgd
->zgd_bp
);
984 kmem_free(zgd
, sizeof (zgd_t
));
988 static int zil_fault_io
= 0;
992 * Get data to generate a TX_WRITE intent log record.
995 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
, zio_t
*zio
)
997 zfsvfs_t
*zfsvfs
= arg
;
998 objset_t
*os
= zfsvfs
->z_os
;
1000 uint64_t object
= lr
->lr_foid
;
1001 uint64_t offset
= lr
->lr_offset
;
1002 uint64_t size
= lr
->lr_length
;
1007 ASSERT3P(lwb
, !=, NULL
);
1008 ASSERT3P(zio
, !=, NULL
);
1009 ASSERT3U(size
, !=, 0);
1012 * Nothing to do if the file has been removed
1014 if (zfs_zget(zfsvfs
, object
, &zp
) != 0)
1015 return (SET_ERROR(ENOENT
));
1016 if (zp
->z_unlinked
) {
1018 * Release the vnode asynchronously as we currently have the
1019 * txg stopped from syncing.
1021 zfs_iput_async(ZTOI(zp
));
1022 return (SET_ERROR(ENOENT
));
1025 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
1027 zgd
->zgd_private
= zp
;
1030 * Write records come in two flavors: immediate and indirect.
1031 * For small writes it's cheaper to store the data with the
1032 * log record (immediate); for large writes it's cheaper to
1033 * sync the data and get a pointer to it (indirect) so that
1034 * we don't have to write the data twice.
1036 if (buf
!= NULL
) { /* immediate write */
1037 zgd
->zgd_rl
= zfs_range_lock(&zp
->z_range_lock
, offset
, size
,
1039 /* test for truncation needs to be done while range locked */
1040 if (offset
>= zp
->z_size
) {
1041 error
= SET_ERROR(ENOENT
);
1043 error
= dmu_read(os
, object
, offset
, size
, buf
,
1044 DMU_READ_NO_PREFETCH
);
1046 ASSERT(error
== 0 || error
== ENOENT
);
1047 } else { /* indirect write */
1049 * Have to lock the whole block to ensure when it's
1050 * written out and its checksum is being calculated
1051 * that no one can change the data. We need to re-check
1052 * blocksize after we get the lock in case it's changed!
1057 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
1059 zgd
->zgd_rl
= zfs_range_lock(&zp
->z_range_lock
, offset
,
1061 if (zp
->z_blksz
== size
)
1064 zfs_range_unlock(zgd
->zgd_rl
);
1066 /* test for truncation needs to be done while range locked */
1067 if (lr
->lr_offset
>= zp
->z_size
)
1068 error
= SET_ERROR(ENOENT
);
1071 error
= SET_ERROR(EIO
);
1076 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1077 DMU_READ_NO_PREFETCH
);
1080 blkptr_t
*bp
= &lr
->lr_blkptr
;
1085 ASSERT(db
->db_offset
== offset
);
1086 ASSERT(db
->db_size
== size
);
1088 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1090 ASSERT(error
|| lr
->lr_length
<= size
);
1093 * On success, we need to wait for the write I/O
1094 * initiated by dmu_sync() to complete before we can
1095 * release this dbuf. We will finish everything up
1096 * in the zfs_get_done() callback.
1101 if (error
== EALREADY
) {
1102 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
1104 * TX_WRITE2 relies on the data previously
1105 * written by the TX_WRITE that caused
1106 * EALREADY. We zero out the BP because
1107 * it is the old, currently-on-disk BP,
1108 * so there's no need to zio_flush() its
1109 * vdevs (flushing would needlesly hurt
1110 * performance, and doesn't work on
1120 zfs_get_done(zgd
, error
);
1127 zfs_access(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
1129 znode_t
*zp
= ITOZ(ip
);
1130 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
1136 if (flag
& V_ACE_MASK
)
1137 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
1139 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
1146 * Lookup an entry in a directory, or an extended attribute directory.
1147 * If it exists, return a held inode reference for it.
1149 * IN: dip - inode of directory to search.
1150 * nm - name of entry to lookup.
1151 * flags - LOOKUP_XATTR set if looking for an attribute.
1152 * cr - credentials of caller.
1153 * direntflags - directory lookup flags
1154 * realpnp - returned pathname.
1156 * OUT: ipp - inode of located entry, NULL if not found.
1158 * RETURN: 0 on success, error code on failure.
1165 zfs_lookup(struct inode
*dip
, char *nm
, struct inode
**ipp
, int flags
,
1166 cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
1168 znode_t
*zdp
= ITOZ(dip
);
1169 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1173 * Fast path lookup, however we must skip DNLC lookup
1174 * for case folding or normalizing lookups because the
1175 * DNLC code only stores the passed in name. This means
1176 * creating 'a' and removing 'A' on a case insensitive
1177 * file system would work, but DNLC still thinks 'a'
1178 * exists and won't let you create it again on the next
1179 * pass through fast path.
1181 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1183 if (!S_ISDIR(dip
->i_mode
)) {
1184 return (SET_ERROR(ENOTDIR
));
1185 } else if (zdp
->z_sa_hdl
== NULL
) {
1186 return (SET_ERROR(EIO
));
1189 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1190 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1198 } else if (!zdp
->z_zfsvfs
->z_norm
&&
1199 (zdp
->z_zfsvfs
->z_case
== ZFS_CASE_SENSITIVE
)) {
1201 vnode_t
*tvp
= dnlc_lookup(dvp
, nm
);
1204 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1209 if (tvp
== DNLC_NO_VNODE
) {
1211 return (SET_ERROR(ENOENT
));
1214 return (specvp_check(vpp
, cr
));
1217 #endif /* HAVE_DNLC */
1226 if (flags
& LOOKUP_XATTR
) {
1228 * We don't allow recursive attributes..
1229 * Maybe someday we will.
1231 if (zdp
->z_pflags
& ZFS_XATTR
) {
1233 return (SET_ERROR(EINVAL
));
1236 if ((error
= zfs_get_xattrdir(zdp
, ipp
, cr
, flags
))) {
1242 * Do we have permission to get into attribute directory?
1245 if ((error
= zfs_zaccess(ITOZ(*ipp
), ACE_EXECUTE
, 0,
1255 if (!S_ISDIR(dip
->i_mode
)) {
1257 return (SET_ERROR(ENOTDIR
));
1261 * Check accessibility of directory.
1264 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
))) {
1269 if (zfsvfs
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1270 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1272 return (SET_ERROR(EILSEQ
));
1275 error
= zfs_dirlook(zdp
, nm
, ipp
, flags
, direntflags
, realpnp
);
1276 if ((error
== 0) && (*ipp
))
1277 zfs_inode_update(ITOZ(*ipp
));
1284 * Attempt to create a new entry in a directory. If the entry
1285 * already exists, truncate the file if permissible, else return
1286 * an error. Return the ip of the created or trunc'd file.
1288 * IN: dip - inode of directory to put new file entry in.
1289 * name - name of new file entry.
1290 * vap - attributes of new file.
1291 * excl - flag indicating exclusive or non-exclusive mode.
1292 * mode - mode to open file with.
1293 * cr - credentials of caller.
1294 * flag - large file flag [UNUSED].
1295 * vsecp - ACL to be set
1297 * OUT: ipp - inode of created or trunc'd entry.
1299 * RETURN: 0 on success, error code on failure.
1302 * dip - ctime|mtime updated if new entry created
1303 * ip - ctime|mtime always, atime if new
1308 zfs_create(struct inode
*dip
, char *name
, vattr_t
*vap
, int excl
,
1309 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
1311 znode_t
*zp
, *dzp
= ITOZ(dip
);
1312 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1320 zfs_acl_ids_t acl_ids
;
1321 boolean_t fuid_dirtied
;
1322 boolean_t have_acl
= B_FALSE
;
1323 boolean_t waited
= B_FALSE
;
1326 * If we have an ephemeral id, ACL, or XVATTR then
1327 * make sure file system is at proper version
1333 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1334 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1335 return (SET_ERROR(EINVAL
));
1338 return (SET_ERROR(EINVAL
));
1343 zilog
= zfsvfs
->z_log
;
1345 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
1346 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1348 return (SET_ERROR(EILSEQ
));
1351 if (vap
->va_mask
& ATTR_XVATTR
) {
1352 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1353 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1361 if (*name
== '\0') {
1363 * Null component name refers to the directory itself.
1370 /* possible igrab(zp) */
1373 if (flag
& FIGNORECASE
)
1376 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1380 zfs_acl_ids_free(&acl_ids
);
1381 if (strcmp(name
, "..") == 0)
1382 error
= SET_ERROR(EISDIR
);
1390 uint64_t projid
= ZFS_DEFAULT_PROJID
;
1393 * Create a new file object and update the directory
1396 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
1398 zfs_acl_ids_free(&acl_ids
);
1403 * We only support the creation of regular files in
1404 * extended attribute directories.
1407 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
1409 zfs_acl_ids_free(&acl_ids
);
1410 error
= SET_ERROR(EINVAL
);
1414 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1415 cr
, vsecp
, &acl_ids
)) != 0)
1419 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
1420 projid
= zfs_inherit_projid(dzp
);
1421 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
1422 zfs_acl_ids_free(&acl_ids
);
1423 error
= SET_ERROR(EDQUOT
);
1427 tx
= dmu_tx_create(os
);
1429 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1430 ZFS_SA_BASE_ATTR_SIZE
);
1432 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1434 zfs_fuid_txhold(zfsvfs
, tx
);
1435 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1436 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1437 if (!zfsvfs
->z_use_sa
&&
1438 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1439 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1440 0, acl_ids
.z_aclp
->z_acl_bytes
);
1443 error
= dmu_tx_assign(tx
,
1444 (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1446 zfs_dirent_unlock(dl
);
1447 if (error
== ERESTART
) {
1453 zfs_acl_ids_free(&acl_ids
);
1458 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1460 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
1463 * Since, we failed to add the directory entry for it,
1464 * delete the newly created dnode.
1466 zfs_znode_delete(zp
, tx
);
1467 remove_inode_hash(ZTOI(zp
));
1468 zfs_acl_ids_free(&acl_ids
);
1474 zfs_fuid_sync(zfsvfs
, tx
);
1476 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1477 if (flag
& FIGNORECASE
)
1479 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1480 vsecp
, acl_ids
.z_fuidp
, vap
);
1481 zfs_acl_ids_free(&acl_ids
);
1484 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1487 zfs_acl_ids_free(&acl_ids
);
1491 * A directory entry already exists for this name.
1494 * Can't truncate an existing file if in exclusive mode.
1497 error
= SET_ERROR(EEXIST
);
1501 * Can't open a directory for writing.
1503 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
1504 error
= SET_ERROR(EISDIR
);
1508 * Verify requested access to file.
1510 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1514 mutex_enter(&dzp
->z_lock
);
1516 mutex_exit(&dzp
->z_lock
);
1519 * Truncate regular files if requested.
1521 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
1522 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
1523 /* we can't hold any locks when calling zfs_freesp() */
1525 zfs_dirent_unlock(dl
);
1528 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1534 zfs_dirent_unlock(dl
);
1540 zfs_inode_update(dzp
);
1541 zfs_inode_update(zp
);
1545 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1546 zil_commit(zilog
, 0);
1554 zfs_tmpfile(struct inode
*dip
, vattr_t
*vap
, int excl
,
1555 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
1557 znode_t
*zp
= NULL
, *dzp
= ITOZ(dip
);
1558 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1564 zfs_acl_ids_t acl_ids
;
1565 uint64_t projid
= ZFS_DEFAULT_PROJID
;
1566 boolean_t fuid_dirtied
;
1567 boolean_t have_acl
= B_FALSE
;
1568 boolean_t waited
= B_FALSE
;
1571 * If we have an ephemeral id, ACL, or XVATTR then
1572 * make sure file system is at proper version
1578 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1579 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1580 return (SET_ERROR(EINVAL
));
1586 if (vap
->va_mask
& ATTR_XVATTR
) {
1587 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1588 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1598 * Create a new file object and update the directory
1601 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
1603 zfs_acl_ids_free(&acl_ids
);
1607 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1608 cr
, vsecp
, &acl_ids
)) != 0)
1612 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
1613 projid
= zfs_inherit_projid(dzp
);
1614 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
1615 zfs_acl_ids_free(&acl_ids
);
1616 error
= SET_ERROR(EDQUOT
);
1620 tx
= dmu_tx_create(os
);
1622 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1623 ZFS_SA_BASE_ATTR_SIZE
);
1624 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1626 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1628 zfs_fuid_txhold(zfsvfs
, tx
);
1629 if (!zfsvfs
->z_use_sa
&&
1630 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1631 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1632 0, acl_ids
.z_aclp
->z_acl_bytes
);
1634 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1636 if (error
== ERESTART
) {
1642 zfs_acl_ids_free(&acl_ids
);
1647 zfs_mknode(dzp
, vap
, tx
, cr
, IS_TMPFILE
, &zp
, &acl_ids
);
1650 zfs_fuid_sync(zfsvfs
, tx
);
1652 /* Add to unlinked set */
1654 zfs_unlinked_add(zp
, tx
);
1655 zfs_acl_ids_free(&acl_ids
);
1663 zfs_inode_update(dzp
);
1664 zfs_inode_update(zp
);
1673 * Remove an entry from a directory.
1675 * IN: dip - inode of directory to remove entry from.
1676 * name - name of entry to remove.
1677 * cr - credentials of caller.
1679 * RETURN: 0 if success
1680 * error code if failure
1684 * ip - ctime (if nlink > 0)
1687 uint64_t null_xattr
= 0;
1691 zfs_remove(struct inode
*dip
, char *name
, cred_t
*cr
, int flags
)
1693 znode_t
*zp
, *dzp
= ITOZ(dip
);
1696 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1698 uint64_t acl_obj
, xattr_obj
;
1699 uint64_t xattr_obj_unlinked
= 0;
1704 boolean_t may_delete_now
, delete_now
= FALSE
;
1705 boolean_t unlinked
, toobig
= FALSE
;
1707 pathname_t
*realnmp
= NULL
;
1711 boolean_t waited
= B_FALSE
;
1714 return (SET_ERROR(EINVAL
));
1718 zilog
= zfsvfs
->z_log
;
1720 if (flags
& FIGNORECASE
) {
1730 * Attempt to lock directory; fail if entry doesn't exist.
1732 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1742 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1747 * Need to use rmdir for removing directories.
1749 if (S_ISDIR(ip
->i_mode
)) {
1750 error
= SET_ERROR(EPERM
);
1756 dnlc_remove(dvp
, realnmp
->pn_buf
);
1758 dnlc_remove(dvp
, name
);
1759 #endif /* HAVE_DNLC */
1761 mutex_enter(&zp
->z_lock
);
1762 may_delete_now
= atomic_read(&ip
->i_count
) == 1 && !(zp
->z_is_mapped
);
1763 mutex_exit(&zp
->z_lock
);
1766 * We may delete the znode now, or we may put it in the unlinked set;
1767 * it depends on whether we're the last link, and on whether there are
1768 * other holds on the inode. So we dmu_tx_hold() the right things to
1769 * allow for either case.
1772 tx
= dmu_tx_create(zfsvfs
->z_os
);
1773 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1774 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1775 zfs_sa_upgrade_txholds(tx
, zp
);
1776 zfs_sa_upgrade_txholds(tx
, dzp
);
1777 if (may_delete_now
) {
1778 toobig
= zp
->z_size
> zp
->z_blksz
* zfs_delete_blocks
;
1779 /* if the file is too big, only hold_free a token amount */
1780 dmu_tx_hold_free(tx
, zp
->z_id
, 0,
1781 (toobig
? DMU_MAX_ACCESS
: DMU_OBJECT_END
));
1784 /* are there any extended attributes? */
1785 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1786 &xattr_obj
, sizeof (xattr_obj
));
1787 if (error
== 0 && xattr_obj
) {
1788 error
= zfs_zget(zfsvfs
, xattr_obj
, &xzp
);
1790 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1791 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1794 mutex_enter(&zp
->z_lock
);
1795 if ((acl_obj
= zfs_external_acl(zp
)) != 0 && may_delete_now
)
1796 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
1797 mutex_exit(&zp
->z_lock
);
1799 /* charge as an update -- would be nice not to charge at all */
1800 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1803 * Mark this transaction as typically resulting in a net free of space
1805 dmu_tx_mark_netfree(tx
);
1807 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1809 zfs_dirent_unlock(dl
);
1810 if (error
== ERESTART
) {
1830 * Remove the directory entry.
1832 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1841 * Hold z_lock so that we can make sure that the ACL obj
1842 * hasn't changed. Could have been deleted due to
1845 mutex_enter(&zp
->z_lock
);
1846 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1847 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1848 delete_now
= may_delete_now
&& !toobig
&&
1849 atomic_read(&ip
->i_count
) == 1 && !(zp
->z_is_mapped
) &&
1850 xattr_obj
== xattr_obj_unlinked
&& zfs_external_acl(zp
) ==
1855 if (xattr_obj_unlinked
) {
1856 ASSERT3U(ZTOI(xzp
)->i_nlink
, ==, 2);
1857 mutex_enter(&xzp
->z_lock
);
1858 xzp
->z_unlinked
= 1;
1859 clear_nlink(ZTOI(xzp
));
1861 error
= sa_update(xzp
->z_sa_hdl
, SA_ZPL_LINKS(zfsvfs
),
1862 &links
, sizeof (links
), tx
);
1863 ASSERT3U(error
, ==, 0);
1864 mutex_exit(&xzp
->z_lock
);
1865 zfs_unlinked_add(xzp
, tx
);
1868 error
= sa_remove(zp
->z_sa_hdl
,
1869 SA_ZPL_XATTR(zfsvfs
), tx
);
1871 error
= sa_update(zp
->z_sa_hdl
,
1872 SA_ZPL_XATTR(zfsvfs
), &null_xattr
,
1873 sizeof (uint64_t), tx
);
1877 * Add to the unlinked set because a new reference could be
1878 * taken concurrently resulting in a deferred destruction.
1880 zfs_unlinked_add(zp
, tx
);
1881 mutex_exit(&zp
->z_lock
);
1882 } else if (unlinked
) {
1883 mutex_exit(&zp
->z_lock
);
1884 zfs_unlinked_add(zp
, tx
);
1888 if (flags
& FIGNORECASE
)
1890 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
);
1897 zfs_dirent_unlock(dl
);
1898 zfs_inode_update(dzp
);
1899 zfs_inode_update(zp
);
1907 zfs_inode_update(xzp
);
1908 zfs_iput_async(ZTOI(xzp
));
1911 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1912 zil_commit(zilog
, 0);
1919 * Create a new directory and insert it into dip using the name
1920 * provided. Return a pointer to the inserted directory.
1922 * IN: dip - inode of directory to add subdir to.
1923 * dirname - name of new directory.
1924 * vap - attributes of new directory.
1925 * cr - credentials of caller.
1926 * vsecp - ACL to be set
1928 * OUT: ipp - inode of created directory.
1930 * RETURN: 0 if success
1931 * error code if failure
1934 * dip - ctime|mtime updated
1935 * ipp - ctime|mtime|atime updated
1939 zfs_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
, struct inode
**ipp
,
1940 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1942 znode_t
*zp
, *dzp
= ITOZ(dip
);
1943 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1951 gid_t gid
= crgetgid(cr
);
1952 zfs_acl_ids_t acl_ids
;
1953 boolean_t fuid_dirtied
;
1954 boolean_t waited
= B_FALSE
;
1956 ASSERT(S_ISDIR(vap
->va_mode
));
1959 * If we have an ephemeral id, ACL, or XVATTR then
1960 * make sure file system is at proper version
1964 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1965 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1966 return (SET_ERROR(EINVAL
));
1968 if (dirname
== NULL
)
1969 return (SET_ERROR(EINVAL
));
1973 zilog
= zfsvfs
->z_log
;
1975 if (dzp
->z_pflags
& ZFS_XATTR
) {
1977 return (SET_ERROR(EINVAL
));
1980 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1981 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1983 return (SET_ERROR(EILSEQ
));
1985 if (flags
& FIGNORECASE
)
1988 if (vap
->va_mask
& ATTR_XVATTR
) {
1989 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1990 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1996 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1997 vsecp
, &acl_ids
)) != 0) {
2002 * First make sure the new directory doesn't exist.
2004 * Existence is checked first to make sure we don't return
2005 * EACCES instead of EEXIST which can cause some applications
2011 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
2013 zfs_acl_ids_free(&acl_ids
);
2018 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
2019 zfs_acl_ids_free(&acl_ids
);
2020 zfs_dirent_unlock(dl
);
2025 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, zfs_inherit_projid(dzp
))) {
2026 zfs_acl_ids_free(&acl_ids
);
2027 zfs_dirent_unlock(dl
);
2029 return (SET_ERROR(EDQUOT
));
2033 * Add a new entry to the directory.
2035 tx
= dmu_tx_create(zfsvfs
->z_os
);
2036 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
2037 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
2038 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
2040 zfs_fuid_txhold(zfsvfs
, tx
);
2041 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2042 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
2043 acl_ids
.z_aclp
->z_acl_bytes
);
2046 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
2047 ZFS_SA_BASE_ATTR_SIZE
);
2049 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
2051 zfs_dirent_unlock(dl
);
2052 if (error
== ERESTART
) {
2058 zfs_acl_ids_free(&acl_ids
);
2067 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
2070 * Now put new name in parent dir.
2072 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
2074 zfs_znode_delete(zp
, tx
);
2075 remove_inode_hash(ZTOI(zp
));
2080 zfs_fuid_sync(zfsvfs
, tx
);
2084 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
2085 if (flags
& FIGNORECASE
)
2087 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
2088 acl_ids
.z_fuidp
, vap
);
2091 zfs_acl_ids_free(&acl_ids
);
2095 zfs_dirent_unlock(dl
);
2097 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2098 zil_commit(zilog
, 0);
2103 zfs_inode_update(dzp
);
2104 zfs_inode_update(zp
);
2111 * Remove a directory subdir entry. If the current working
2112 * directory is the same as the subdir to be removed, the
2115 * IN: dip - inode of directory to remove from.
2116 * name - name of directory to be removed.
2117 * cwd - inode of current working directory.
2118 * cr - credentials of caller.
2119 * flags - case flags
2121 * RETURN: 0 on success, error code on failure.
2124 * dip - ctime|mtime updated
2128 zfs_rmdir(struct inode
*dip
, char *name
, struct inode
*cwd
, cred_t
*cr
,
2131 znode_t
*dzp
= ITOZ(dip
);
2134 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
2140 boolean_t waited
= B_FALSE
;
2143 return (SET_ERROR(EINVAL
));
2147 zilog
= zfsvfs
->z_log
;
2149 if (flags
& FIGNORECASE
)
2155 * Attempt to lock directory; fail if entry doesn't exist.
2157 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
2165 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
2169 if (!S_ISDIR(ip
->i_mode
)) {
2170 error
= SET_ERROR(ENOTDIR
);
2175 error
= SET_ERROR(EINVAL
);
2180 * Grab a lock on the directory to make sure that no one is
2181 * trying to add (or lookup) entries while we are removing it.
2183 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
2186 * Grab a lock on the parent pointer to make sure we play well
2187 * with the treewalk and directory rename code.
2189 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
2191 tx
= dmu_tx_create(zfsvfs
->z_os
);
2192 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
2193 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2194 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
2195 zfs_sa_upgrade_txholds(tx
, zp
);
2196 zfs_sa_upgrade_txholds(tx
, dzp
);
2197 dmu_tx_mark_netfree(tx
);
2198 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
2200 rw_exit(&zp
->z_parent_lock
);
2201 rw_exit(&zp
->z_name_lock
);
2202 zfs_dirent_unlock(dl
);
2203 if (error
== ERESTART
) {
2216 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
2219 uint64_t txtype
= TX_RMDIR
;
2220 if (flags
& FIGNORECASE
)
2222 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
);
2227 rw_exit(&zp
->z_parent_lock
);
2228 rw_exit(&zp
->z_name_lock
);
2230 zfs_dirent_unlock(dl
);
2232 zfs_inode_update(dzp
);
2233 zfs_inode_update(zp
);
2236 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2237 zil_commit(zilog
, 0);
2244 * Read as many directory entries as will fit into the provided
2245 * dirent buffer from the given directory cursor position.
2247 * IN: ip - inode of directory to read.
2248 * dirent - buffer for directory entries.
2250 * OUT: dirent - filler buffer of directory entries.
2252 * RETURN: 0 if success
2253 * error code if failure
2256 * ip - atime updated
2258 * Note that the low 4 bits of the cookie returned by zap is always zero.
2259 * This allows us to use the low range for "special" directory entries:
2260 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2261 * we use the offset 2 for the '.zfs' directory.
2265 zfs_readdir(struct inode
*ip
, zpl_dir_context_t
*ctx
, cred_t
*cr
)
2267 znode_t
*zp
= ITOZ(ip
);
2268 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2271 zap_attribute_t zap
;
2277 uint64_t offset
; /* must be unsigned; checks for < 1 */
2282 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
2283 &parent
, sizeof (parent
))) != 0)
2287 * Quit if directory has been removed (posix)
2295 prefetch
= zp
->z_zn_prefetch
;
2298 * Initialize the iterator cursor.
2302 * Start iteration from the beginning of the directory.
2304 zap_cursor_init(&zc
, os
, zp
->z_id
);
2307 * The offset is a serialized cursor.
2309 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
2313 * Transform to file-system independent format
2318 * Special case `.', `..', and `.zfs'.
2321 (void) strcpy(zap
.za_name
, ".");
2322 zap
.za_normalization_conflict
= 0;
2325 } else if (offset
== 1) {
2326 (void) strcpy(zap
.za_name
, "..");
2327 zap
.za_normalization_conflict
= 0;
2330 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
2331 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
2332 zap
.za_normalization_conflict
= 0;
2333 objnum
= ZFSCTL_INO_ROOT
;
2339 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
2340 if (error
== ENOENT
)
2347 * Allow multiple entries provided the first entry is
2348 * the object id. Non-zpl consumers may safely make
2349 * use of the additional space.
2351 * XXX: This should be a feature flag for compatibility
2353 if (zap
.za_integer_length
!= 8 ||
2354 zap
.za_num_integers
== 0) {
2355 cmn_err(CE_WARN
, "zap_readdir: bad directory "
2356 "entry, obj = %lld, offset = %lld, "
2357 "length = %d, num = %lld\n",
2358 (u_longlong_t
)zp
->z_id
,
2359 (u_longlong_t
)offset
,
2360 zap
.za_integer_length
,
2361 (u_longlong_t
)zap
.za_num_integers
);
2362 error
= SET_ERROR(ENXIO
);
2366 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
2367 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
2370 done
= !zpl_dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
2375 /* Prefetch znode */
2377 dmu_prefetch(os
, objnum
, 0, 0, 0,
2378 ZIO_PRIORITY_SYNC_READ
);
2382 * Move to the next entry, fill in the previous offset.
2384 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2385 zap_cursor_advance(&zc
);
2386 offset
= zap_cursor_serialize(&zc
);
2392 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2395 zap_cursor_fini(&zc
);
2396 if (error
== ENOENT
)
2404 ulong_t zfs_fsync_sync_cnt
= 4;
2407 zfs_fsync(struct inode
*ip
, int syncflag
, cred_t
*cr
)
2409 znode_t
*zp
= ITOZ(ip
);
2410 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2412 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2414 if (zfsvfs
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2417 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
2420 tsd_set(zfs_fsyncer_key
, NULL
);
2427 * Get the requested file attributes and place them in the provided
2430 * IN: ip - inode of file.
2431 * vap - va_mask identifies requested attributes.
2432 * If ATTR_XVATTR set, then optional attrs are requested
2433 * flags - ATTR_NOACLCHECK (CIFS server context)
2434 * cr - credentials of caller.
2436 * OUT: vap - attribute values.
2438 * RETURN: 0 (always succeeds)
2442 zfs_getattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2444 znode_t
*zp
= ITOZ(ip
);
2445 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2448 uint64_t atime
[2], mtime
[2], ctime
[2];
2449 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2450 xoptattr_t
*xoap
= NULL
;
2451 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2452 sa_bulk_attr_t bulk
[3];
2458 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2460 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
2461 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
2462 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
2464 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2470 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2471 * Also, if we are the owner don't bother, since owner should
2472 * always be allowed to read basic attributes of file.
2474 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2475 (vap
->va_uid
!= crgetuid(cr
))) {
2476 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2484 * Return all attributes. It's cheaper to provide the answer
2485 * than to determine whether we were asked the question.
2488 mutex_enter(&zp
->z_lock
);
2489 vap
->va_type
= vn_mode_to_vtype(zp
->z_mode
);
2490 vap
->va_mode
= zp
->z_mode
;
2491 vap
->va_fsid
= ZTOI(zp
)->i_sb
->s_dev
;
2492 vap
->va_nodeid
= zp
->z_id
;
2493 if ((zp
->z_id
== zfsvfs
->z_root
) && zfs_show_ctldir(zp
))
2494 links
= ZTOI(zp
)->i_nlink
+ 1;
2496 links
= ZTOI(zp
)->i_nlink
;
2497 vap
->va_nlink
= MIN(links
, ZFS_LINK_MAX
);
2498 vap
->va_size
= i_size_read(ip
);
2499 vap
->va_rdev
= ip
->i_rdev
;
2500 vap
->va_seq
= ip
->i_generation
;
2503 * Add in any requested optional attributes and the create time.
2504 * Also set the corresponding bits in the returned attribute bitmap.
2506 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zfsvfs
->z_use_fuids
) {
2507 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2509 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2510 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2513 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2514 xoap
->xoa_readonly
=
2515 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2516 XVA_SET_RTN(xvap
, XAT_READONLY
);
2519 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2521 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2522 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2525 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2527 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2528 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2531 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2532 xoap
->xoa_nounlink
=
2533 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2534 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2537 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2538 xoap
->xoa_immutable
=
2539 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2540 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2543 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2544 xoap
->xoa_appendonly
=
2545 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2546 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2549 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2551 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2552 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2555 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2557 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2558 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2561 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2562 xoap
->xoa_av_quarantined
=
2563 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2564 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2567 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2568 xoap
->xoa_av_modified
=
2569 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2570 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2573 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2574 S_ISREG(ip
->i_mode
)) {
2575 zfs_sa_get_scanstamp(zp
, xvap
);
2578 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2581 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zfsvfs
),
2582 times
, sizeof (times
));
2583 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2584 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2587 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2588 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2589 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2591 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2592 xoap
->xoa_generation
= ip
->i_generation
;
2593 XVA_SET_RTN(xvap
, XAT_GEN
);
2596 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2598 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2599 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2602 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2604 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2605 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2608 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2609 xoap
->xoa_projinherit
=
2610 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0);
2611 XVA_SET_RTN(xvap
, XAT_PROJINHERIT
);
2614 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2615 xoap
->xoa_projid
= zp
->z_projid
;
2616 XVA_SET_RTN(xvap
, XAT_PROJID
);
2620 ZFS_TIME_DECODE(&vap
->va_atime
, atime
);
2621 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2622 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2624 mutex_exit(&zp
->z_lock
);
2626 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2628 if (zp
->z_blksz
== 0) {
2630 * Block size hasn't been set; suggest maximal I/O transfers.
2632 vap
->va_blksize
= zfsvfs
->z_max_blksz
;
2640 * Get the basic file attributes and place them in the provided kstat
2641 * structure. The inode is assumed to be the authoritative source
2642 * for most of the attributes. However, the znode currently has the
2643 * authoritative atime, blksize, and block count.
2645 * IN: ip - inode of file.
2647 * OUT: sp - kstat values.
2649 * RETURN: 0 (always succeeds)
2653 zfs_getattr_fast(struct inode
*ip
, struct kstat
*sp
)
2655 znode_t
*zp
= ITOZ(ip
);
2656 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2658 u_longlong_t nblocks
;
2663 mutex_enter(&zp
->z_lock
);
2665 generic_fillattr(ip
, sp
);
2667 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
2668 sp
->blksize
= blksize
;
2669 sp
->blocks
= nblocks
;
2671 if (unlikely(zp
->z_blksz
== 0)) {
2673 * Block size hasn't been set; suggest maximal I/O transfers.
2675 sp
->blksize
= zfsvfs
->z_max_blksz
;
2678 mutex_exit(&zp
->z_lock
);
2681 * Required to prevent NFS client from detecting different inode
2682 * numbers of snapshot root dentry before and after snapshot mount.
2684 if (zfsvfs
->z_issnap
) {
2685 if (ip
->i_sb
->s_root
->d_inode
== ip
)
2686 sp
->ino
= ZFSCTL_INO_SNAPDIRS
-
2687 dmu_objset_id(zfsvfs
->z_os
);
2696 * For the operation of changing file's user/group/project, we need to
2697 * handle not only the main object that is assigned to the file directly,
2698 * but also the ones that are used by the file via hidden xattr directory.
2700 * Because the xattr directory may contains many EA entries, as to it may
2701 * be impossible to change all of them via the transaction of changing the
2702 * main object's user/group/project attributes. Then we have to change them
2703 * via other multiple independent transactions one by one. It may be not good
2704 * solution, but we have no better idea yet.
2707 zfs_setattr_dir(znode_t
*dzp
)
2709 struct inode
*dxip
= ZTOI(dzp
);
2710 struct inode
*xip
= NULL
;
2711 zfsvfs_t
*zfsvfs
= ITOZSB(dxip
);
2712 objset_t
*os
= zfsvfs
->z_os
;
2714 zap_attribute_t zap
;
2717 dmu_tx_t
*tx
= NULL
;
2719 sa_bulk_attr_t bulk
[4];
2723 zap_cursor_init(&zc
, os
, dzp
->z_id
);
2724 while ((err
= zap_cursor_retrieve(&zc
, &zap
)) == 0) {
2725 if (zap
.za_integer_length
!= 8 || zap
.za_num_integers
!= 1) {
2730 err
= zfs_dirent_lock(&dl
, dzp
, (char *)zap
.za_name
, &zp
,
2731 ZEXISTS
, NULL
, NULL
);
2738 if (KUID_TO_SUID(xip
->i_uid
) == KUID_TO_SUID(dxip
->i_uid
) &&
2739 KGID_TO_SGID(xip
->i_gid
) == KGID_TO_SGID(dxip
->i_gid
) &&
2740 zp
->z_projid
== dzp
->z_projid
)
2743 tx
= dmu_tx_create(os
);
2744 if (!(zp
->z_pflags
& ZFS_PROJID
))
2745 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2747 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2749 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2753 mutex_enter(&dzp
->z_lock
);
2755 if (KUID_TO_SUID(xip
->i_uid
) != KUID_TO_SUID(dxip
->i_uid
)) {
2756 xip
->i_uid
= dxip
->i_uid
;
2757 uid
= zfs_uid_read(dxip
);
2758 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
2759 &uid
, sizeof (uid
));
2762 if (KGID_TO_SGID(xip
->i_gid
) != KGID_TO_SGID(dxip
->i_gid
)) {
2763 xip
->i_gid
= dxip
->i_gid
;
2764 gid
= zfs_gid_read(dxip
);
2765 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
2766 &gid
, sizeof (gid
));
2769 if (zp
->z_projid
!= dzp
->z_projid
) {
2770 if (!(zp
->z_pflags
& ZFS_PROJID
)) {
2771 zp
->z_pflags
|= ZFS_PROJID
;
2772 SA_ADD_BULK_ATTR(bulk
, count
,
2773 SA_ZPL_FLAGS(zfsvfs
), NULL
, &zp
->z_pflags
,
2774 sizeof (zp
->z_pflags
));
2777 zp
->z_projid
= dzp
->z_projid
;
2778 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PROJID(zfsvfs
),
2779 NULL
, &zp
->z_projid
, sizeof (zp
->z_projid
));
2782 mutex_exit(&dzp
->z_lock
);
2784 if (likely(count
> 0)) {
2785 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
2791 if (err
!= 0 && err
!= ENOENT
)
2798 zfs_dirent_unlock(dl
);
2800 zap_cursor_advance(&zc
);
2807 zfs_dirent_unlock(dl
);
2809 zap_cursor_fini(&zc
);
2811 return (err
== ENOENT
? 0 : err
);
2815 * Set the file attributes to the values contained in the
2818 * IN: ip - inode of file to be modified.
2819 * vap - new attribute values.
2820 * If ATTR_XVATTR set, then optional attrs are being set
2821 * flags - ATTR_UTIME set if non-default time values provided.
2822 * - ATTR_NOACLCHECK (CIFS context only).
2823 * cr - credentials of caller.
2825 * RETURN: 0 if success
2826 * error code if failure
2829 * ip - ctime updated, mtime updated if size changed.
2833 zfs_setattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2835 znode_t
*zp
= ITOZ(ip
);
2836 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2837 objset_t
*os
= zfsvfs
->z_os
;
2841 xvattr_t
*tmpxvattr
;
2842 uint_t mask
= vap
->va_mask
;
2843 uint_t saved_mask
= 0;
2846 uint64_t new_kuid
= 0, new_kgid
= 0, new_uid
, new_gid
;
2848 uint64_t mtime
[2], ctime
[2], atime
[2];
2849 uint64_t projid
= ZFS_INVALID_PROJID
;
2851 int need_policy
= FALSE
;
2853 zfs_fuid_info_t
*fuidp
= NULL
;
2854 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2857 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2858 boolean_t fuid_dirtied
= B_FALSE
;
2859 boolean_t handle_eadir
= B_FALSE
;
2860 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
2861 int count
= 0, xattr_count
= 0, bulks
= 8;
2870 * If this is a xvattr_t, then get a pointer to the structure of
2871 * optional attributes. If this is NULL, then we have a vattr_t.
2873 xoap
= xva_getxoptattr(xvap
);
2874 if (xoap
!= NULL
&& (mask
& ATTR_XVATTR
)) {
2875 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2876 if (!dmu_objset_projectquota_enabled(os
) ||
2877 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
))) {
2879 return (SET_ERROR(ENOTSUP
));
2882 projid
= xoap
->xoa_projid
;
2883 if (unlikely(projid
== ZFS_INVALID_PROJID
)) {
2885 return (SET_ERROR(EINVAL
));
2888 if (projid
== zp
->z_projid
&& zp
->z_pflags
& ZFS_PROJID
)
2889 projid
= ZFS_INVALID_PROJID
;
2894 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
) &&
2895 (xoap
->xoa_projinherit
!=
2896 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) &&
2897 (!dmu_objset_projectquota_enabled(os
) ||
2898 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
)))) {
2900 return (SET_ERROR(ENOTSUP
));
2904 zilog
= zfsvfs
->z_log
;
2907 * Make sure that if we have ephemeral uid/gid or xvattr specified
2908 * that file system is at proper version level
2911 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
2912 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2913 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2914 (mask
& ATTR_XVATTR
))) {
2916 return (SET_ERROR(EINVAL
));
2919 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
2921 return (SET_ERROR(EISDIR
));
2924 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
2926 return (SET_ERROR(EINVAL
));
2929 tmpxvattr
= kmem_alloc(sizeof (xvattr_t
), KM_SLEEP
);
2930 xva_init(tmpxvattr
);
2932 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
2933 xattr_bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
2936 * Immutable files can only alter immutable bit and atime
2938 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2939 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
2940 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2941 err
= SET_ERROR(EPERM
);
2945 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2946 err
= SET_ERROR(EPERM
);
2951 * Verify timestamps doesn't overflow 32 bits.
2952 * ZFS can handle large timestamps, but 32bit syscalls can't
2953 * handle times greater than 2039. This check should be removed
2954 * once large timestamps are fully supported.
2956 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
2957 if (((mask
& ATTR_ATIME
) &&
2958 TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2959 ((mask
& ATTR_MTIME
) &&
2960 TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2961 err
= SET_ERROR(EOVERFLOW
);
2970 /* Can this be moved to before the top label? */
2971 if (zfs_is_readonly(zfsvfs
)) {
2972 err
= SET_ERROR(EROFS
);
2977 * First validate permissions
2980 if (mask
& ATTR_SIZE
) {
2981 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2986 * XXX - Note, we are not providing any open
2987 * mode flags here (like FNDELAY), so we may
2988 * block if there are locks present... this
2989 * should be addressed in openat().
2991 /* XXX - would it be OK to generate a log record here? */
2992 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2997 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
2998 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2999 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
3000 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
3001 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
3002 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
3003 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
3004 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
3005 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
3009 if (mask
& (ATTR_UID
|ATTR_GID
)) {
3010 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
3015 * NOTE: even if a new mode is being set,
3016 * we may clear S_ISUID/S_ISGID bits.
3019 if (!(mask
& ATTR_MODE
))
3020 vap
->va_mode
= zp
->z_mode
;
3023 * Take ownership or chgrp to group we are a member of
3026 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
3027 take_group
= (mask
& ATTR_GID
) &&
3028 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
3031 * If both ATTR_UID and ATTR_GID are set then take_owner and
3032 * take_group must both be set in order to allow taking
3035 * Otherwise, send the check through secpolicy_vnode_setattr()
3039 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
3040 take_owner
&& take_group
) ||
3041 ((idmask
== ATTR_UID
) && take_owner
) ||
3042 ((idmask
== ATTR_GID
) && take_group
)) {
3043 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
3044 skipaclchk
, cr
) == 0) {
3046 * Remove setuid/setgid for non-privileged users
3048 (void) secpolicy_setid_clear(vap
, cr
);
3049 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
3058 mutex_enter(&zp
->z_lock
);
3059 oldva
.va_mode
= zp
->z_mode
;
3060 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
3061 if (mask
& ATTR_XVATTR
) {
3063 * Update xvattr mask to include only those attributes
3064 * that are actually changing.
3066 * the bits will be restored prior to actually setting
3067 * the attributes so the caller thinks they were set.
3069 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
3070 if (xoap
->xoa_appendonly
!=
3071 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
3074 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
3075 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
3079 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
3080 if (xoap
->xoa_projinherit
!=
3081 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) {
3084 XVA_CLR_REQ(xvap
, XAT_PROJINHERIT
);
3085 XVA_SET_REQ(tmpxvattr
, XAT_PROJINHERIT
);
3089 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
3090 if (xoap
->xoa_nounlink
!=
3091 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
3094 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
3095 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
3099 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
3100 if (xoap
->xoa_immutable
!=
3101 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
3104 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
3105 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
3109 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
3110 if (xoap
->xoa_nodump
!=
3111 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
3114 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
3115 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
3119 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
3120 if (xoap
->xoa_av_modified
!=
3121 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
3124 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
3125 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
3129 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
3130 if ((!S_ISREG(ip
->i_mode
) &&
3131 xoap
->xoa_av_quarantined
) ||
3132 xoap
->xoa_av_quarantined
!=
3133 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
3136 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
3137 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
3141 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
3142 mutex_exit(&zp
->z_lock
);
3143 err
= SET_ERROR(EPERM
);
3147 if (need_policy
== FALSE
&&
3148 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
3149 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
3154 mutex_exit(&zp
->z_lock
);
3156 if (mask
& ATTR_MODE
) {
3157 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
3158 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
3163 trim_mask
|= ATTR_MODE
;
3171 * If trim_mask is set then take ownership
3172 * has been granted or write_acl is present and user
3173 * has the ability to modify mode. In that case remove
3174 * UID|GID and or MODE from mask so that
3175 * secpolicy_vnode_setattr() doesn't revoke it.
3179 saved_mask
= vap
->va_mask
;
3180 vap
->va_mask
&= ~trim_mask
;
3182 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
3183 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
3188 vap
->va_mask
|= saved_mask
;
3192 * secpolicy_vnode_setattr, or take ownership may have
3195 mask
= vap
->va_mask
;
3197 if ((mask
& (ATTR_UID
| ATTR_GID
)) || projid
!= ZFS_INVALID_PROJID
) {
3198 handle_eadir
= B_TRUE
;
3199 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
3200 &xattr_obj
, sizeof (xattr_obj
));
3202 if (err
== 0 && xattr_obj
) {
3203 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
3207 if (mask
& ATTR_UID
) {
3208 new_kuid
= zfs_fuid_create(zfsvfs
,
3209 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
3210 if (new_kuid
!= KUID_TO_SUID(ZTOI(zp
)->i_uid
) &&
3211 zfs_id_overquota(zfsvfs
, DMU_USERUSED_OBJECT
,
3215 err
= SET_ERROR(EDQUOT
);
3220 if (mask
& ATTR_GID
) {
3221 new_kgid
= zfs_fuid_create(zfsvfs
,
3222 (uint64_t)vap
->va_gid
, cr
, ZFS_GROUP
, &fuidp
);
3223 if (new_kgid
!= KGID_TO_SGID(ZTOI(zp
)->i_gid
) &&
3224 zfs_id_overquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
3228 err
= SET_ERROR(EDQUOT
);
3233 if (projid
!= ZFS_INVALID_PROJID
&&
3234 zfs_id_overquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
, projid
)) {
3241 tx
= dmu_tx_create(os
);
3243 if (mask
& ATTR_MODE
) {
3244 uint64_t pmode
= zp
->z_mode
;
3246 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
3248 zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
);
3250 mutex_enter(&zp
->z_lock
);
3251 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
3253 * Are we upgrading ACL from old V0 format
3256 if (zfsvfs
->z_version
>= ZPL_VERSION_FUID
&&
3257 zfs_znode_acl_version(zp
) ==
3258 ZFS_ACL_VERSION_INITIAL
) {
3259 dmu_tx_hold_free(tx
, acl_obj
, 0,
3261 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
3262 0, aclp
->z_acl_bytes
);
3264 dmu_tx_hold_write(tx
, acl_obj
, 0,
3267 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3268 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
3269 0, aclp
->z_acl_bytes
);
3271 mutex_exit(&zp
->z_lock
);
3272 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
3274 if (((mask
& ATTR_XVATTR
) &&
3275 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) ||
3276 (projid
!= ZFS_INVALID_PROJID
&&
3277 !(zp
->z_pflags
& ZFS_PROJID
)))
3278 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
3280 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3284 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
3287 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3289 zfs_fuid_txhold(zfsvfs
, tx
);
3291 zfs_sa_upgrade_txholds(tx
, zp
);
3293 err
= dmu_tx_assign(tx
, TXG_WAIT
);
3299 * Set each attribute requested.
3300 * We group settings according to the locks they need to acquire.
3302 * Note: you cannot set ctime directly, although it will be
3303 * updated as a side-effect of calling this function.
3306 if (projid
!= ZFS_INVALID_PROJID
&& !(zp
->z_pflags
& ZFS_PROJID
)) {
3308 * For the existed object that is upgraded from old system,
3309 * its on-disk layout has no slot for the project ID attribute.
3310 * But quota accounting logic needs to access related slots by
3311 * offset directly. So we need to adjust old objects' layout
3312 * to make the project ID to some unified and fixed offset.
3315 err
= sa_add_projid(attrzp
->z_sa_hdl
, tx
, projid
);
3317 err
= sa_add_projid(zp
->z_sa_hdl
, tx
, projid
);
3319 if (unlikely(err
== EEXIST
))
3324 projid
= ZFS_INVALID_PROJID
;
3327 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3328 mutex_enter(&zp
->z_acl_lock
);
3329 mutex_enter(&zp
->z_lock
);
3331 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
3332 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
3335 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3336 mutex_enter(&attrzp
->z_acl_lock
);
3337 mutex_enter(&attrzp
->z_lock
);
3338 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3339 SA_ZPL_FLAGS(zfsvfs
), NULL
, &attrzp
->z_pflags
,
3340 sizeof (attrzp
->z_pflags
));
3341 if (projid
!= ZFS_INVALID_PROJID
) {
3342 attrzp
->z_projid
= projid
;
3343 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3344 SA_ZPL_PROJID(zfsvfs
), NULL
, &attrzp
->z_projid
,
3345 sizeof (attrzp
->z_projid
));
3349 if (mask
& (ATTR_UID
|ATTR_GID
)) {
3351 if (mask
& ATTR_UID
) {
3352 ZTOI(zp
)->i_uid
= SUID_TO_KUID(new_kuid
);
3353 new_uid
= zfs_uid_read(ZTOI(zp
));
3354 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
3355 &new_uid
, sizeof (new_uid
));
3357 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3358 SA_ZPL_UID(zfsvfs
), NULL
, &new_uid
,
3360 ZTOI(attrzp
)->i_uid
= SUID_TO_KUID(new_uid
);
3364 if (mask
& ATTR_GID
) {
3365 ZTOI(zp
)->i_gid
= SGID_TO_KGID(new_kgid
);
3366 new_gid
= zfs_gid_read(ZTOI(zp
));
3367 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
),
3368 NULL
, &new_gid
, sizeof (new_gid
));
3370 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3371 SA_ZPL_GID(zfsvfs
), NULL
, &new_gid
,
3373 ZTOI(attrzp
)->i_gid
= SGID_TO_KGID(new_kgid
);
3376 if (!(mask
& ATTR_MODE
)) {
3377 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
),
3378 NULL
, &new_mode
, sizeof (new_mode
));
3379 new_mode
= zp
->z_mode
;
3381 err
= zfs_acl_chown_setattr(zp
);
3384 err
= zfs_acl_chown_setattr(attrzp
);
3389 if (mask
& ATTR_MODE
) {
3390 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
3391 &new_mode
, sizeof (new_mode
));
3392 zp
->z_mode
= ZTOI(zp
)->i_mode
= new_mode
;
3393 ASSERT3P(aclp
, !=, NULL
);
3394 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
3396 if (zp
->z_acl_cached
)
3397 zfs_acl_free(zp
->z_acl_cached
);
3398 zp
->z_acl_cached
= aclp
;
3402 if ((mask
& ATTR_ATIME
) || zp
->z_atime_dirty
) {
3403 zp
->z_atime_dirty
= 0;
3404 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
3405 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
3406 &atime
, sizeof (atime
));
3409 if (mask
& (ATTR_MTIME
| ATTR_SIZE
)) {
3410 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
3411 ZTOI(zp
)->i_mtime
= timespec_trunc(vap
->va_mtime
,
3412 ZTOI(zp
)->i_sb
->s_time_gran
);
3414 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
3415 mtime
, sizeof (mtime
));
3418 if (mask
& (ATTR_CTIME
| ATTR_SIZE
)) {
3419 ZFS_TIME_ENCODE(&vap
->va_ctime
, ctime
);
3420 ZTOI(zp
)->i_ctime
= timespec_trunc(vap
->va_ctime
,
3421 ZTOI(zp
)->i_sb
->s_time_gran
);
3422 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
3423 ctime
, sizeof (ctime
));
3426 if (projid
!= ZFS_INVALID_PROJID
) {
3427 zp
->z_projid
= projid
;
3428 SA_ADD_BULK_ATTR(bulk
, count
,
3429 SA_ZPL_PROJID(zfsvfs
), NULL
, &zp
->z_projid
,
3430 sizeof (zp
->z_projid
));
3433 if (attrzp
&& mask
) {
3434 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3435 SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
,
3440 * Do this after setting timestamps to prevent timestamp
3441 * update from toggling bit
3444 if (xoap
&& (mask
& ATTR_XVATTR
)) {
3447 * restore trimmed off masks
3448 * so that return masks can be set for caller.
3451 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
3452 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
3454 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
3455 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
3457 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
3458 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
3460 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
3461 XVA_SET_REQ(xvap
, XAT_NODUMP
);
3463 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
3464 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
3466 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
3467 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
3469 if (XVA_ISSET_REQ(tmpxvattr
, XAT_PROJINHERIT
)) {
3470 XVA_SET_REQ(xvap
, XAT_PROJINHERIT
);
3473 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
3474 ASSERT(S_ISREG(ip
->i_mode
));
3476 zfs_xvattr_set(zp
, xvap
, tx
);
3480 zfs_fuid_sync(zfsvfs
, tx
);
3483 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
3485 mutex_exit(&zp
->z_lock
);
3486 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3487 mutex_exit(&zp
->z_acl_lock
);
3490 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3491 mutex_exit(&attrzp
->z_acl_lock
);
3492 mutex_exit(&attrzp
->z_lock
);
3495 if (err
== 0 && xattr_count
> 0) {
3496 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3505 zfs_fuid_info_free(fuidp
);
3513 if (err
== ERESTART
)
3517 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3520 if (err2
== 0 && handle_eadir
)
3521 err2
= zfs_setattr_dir(attrzp
);
3524 zfs_inode_update(zp
);
3528 if (os
->os_sync
== ZFS_SYNC_ALWAYS
)
3529 zil_commit(zilog
, 0);
3532 kmem_free(xattr_bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
3533 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
3534 kmem_free(tmpxvattr
, sizeof (xvattr_t
));
3539 typedef struct zfs_zlock
{
3540 krwlock_t
*zl_rwlock
; /* lock we acquired */
3541 znode_t
*zl_znode
; /* znode we held */
3542 struct zfs_zlock
*zl_next
; /* next in list */
3546 * Drop locks and release vnodes that were held by zfs_rename_lock().
3549 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3553 while ((zl
= *zlpp
) != NULL
) {
3554 if (zl
->zl_znode
!= NULL
)
3555 zfs_iput_async(ZTOI(zl
->zl_znode
));
3556 rw_exit(zl
->zl_rwlock
);
3557 *zlpp
= zl
->zl_next
;
3558 kmem_free(zl
, sizeof (*zl
));
3563 * Search back through the directory tree, using the ".." entries.
3564 * Lock each directory in the chain to prevent concurrent renames.
3565 * Fail any attempt to move a directory into one of its own descendants.
3566 * XXX - z_parent_lock can overlap with map or grow locks
3569 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3573 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
3574 uint64_t oidp
= zp
->z_id
;
3575 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3576 krw_t rw
= RW_WRITER
;
3579 * First pass write-locks szp and compares to zp->z_id.
3580 * Later passes read-lock zp and compare to zp->z_parent.
3583 if (!rw_tryenter(rwlp
, rw
)) {
3585 * Another thread is renaming in this path.
3586 * Note that if we are a WRITER, we don't have any
3587 * parent_locks held yet.
3589 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3591 * Drop our locks and restart
3593 zfs_rename_unlock(&zl
);
3597 rwlp
= &szp
->z_parent_lock
;
3602 * Wait for other thread to drop its locks
3608 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3609 zl
->zl_rwlock
= rwlp
;
3610 zl
->zl_znode
= NULL
;
3611 zl
->zl_next
= *zlpp
;
3614 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3615 return (SET_ERROR(EINVAL
));
3617 if (oidp
== rootid
) /* We've hit the top */
3620 if (rw
== RW_READER
) { /* i.e. not the first pass */
3621 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
3626 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
3627 &oidp
, sizeof (oidp
));
3628 rwlp
= &zp
->z_parent_lock
;
3631 } while (zp
->z_id
!= sdzp
->z_id
);
3637 * Move an entry from the provided source directory to the target
3638 * directory. Change the entry name as indicated.
3640 * IN: sdip - Source directory containing the "old entry".
3641 * snm - Old entry name.
3642 * tdip - Target directory to contain the "new entry".
3643 * tnm - New entry name.
3644 * cr - credentials of caller.
3645 * flags - case flags
3647 * RETURN: 0 on success, error code on failure.
3650 * sdip,tdip - ctime|mtime updated
3654 zfs_rename(struct inode
*sdip
, char *snm
, struct inode
*tdip
, char *tnm
,
3655 cred_t
*cr
, int flags
)
3657 znode_t
*tdzp
, *szp
, *tzp
;
3658 znode_t
*sdzp
= ITOZ(sdip
);
3659 zfsvfs_t
*zfsvfs
= ITOZSB(sdip
);
3661 zfs_dirlock_t
*sdl
, *tdl
;
3664 int cmp
, serr
, terr
;
3667 boolean_t waited
= B_FALSE
;
3669 if (snm
== NULL
|| tnm
== NULL
)
3670 return (SET_ERROR(EINVAL
));
3673 ZFS_VERIFY_ZP(sdzp
);
3674 zilog
= zfsvfs
->z_log
;
3677 ZFS_VERIFY_ZP(tdzp
);
3680 * We check i_sb because snapshots and the ctldir must have different
3683 if (tdip
->i_sb
!= sdip
->i_sb
|| zfsctl_is_node(tdip
)) {
3685 return (SET_ERROR(EXDEV
));
3688 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
3689 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3691 return (SET_ERROR(EILSEQ
));
3694 if (flags
& FIGNORECASE
)
3703 * This is to prevent the creation of links into attribute space
3704 * by renaming a linked file into/outof an attribute directory.
3705 * See the comment in zfs_link() for why this is considered bad.
3707 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3709 return (SET_ERROR(EINVAL
));
3713 * Lock source and target directory entries. To prevent deadlock,
3714 * a lock ordering must be defined. We lock the directory with
3715 * the smallest object id first, or if it's a tie, the one with
3716 * the lexically first name.
3718 if (sdzp
->z_id
< tdzp
->z_id
) {
3720 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3724 * First compare the two name arguments without
3725 * considering any case folding.
3727 int nofold
= (zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3729 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3730 ASSERT(error
== 0 || !zfsvfs
->z_utf8
);
3733 * POSIX: "If the old argument and the new argument
3734 * both refer to links to the same existing file,
3735 * the rename() function shall return successfully
3736 * and perform no other action."
3742 * If the file system is case-folding, then we may
3743 * have some more checking to do. A case-folding file
3744 * system is either supporting mixed case sensitivity
3745 * access or is completely case-insensitive. Note
3746 * that the file system is always case preserving.
3748 * In mixed sensitivity mode case sensitive behavior
3749 * is the default. FIGNORECASE must be used to
3750 * explicitly request case insensitive behavior.
3752 * If the source and target names provided differ only
3753 * by case (e.g., a request to rename 'tim' to 'Tim'),
3754 * we will treat this as a special case in the
3755 * case-insensitive mode: as long as the source name
3756 * is an exact match, we will allow this to proceed as
3757 * a name-change request.
3759 if ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
3760 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
3761 flags
& FIGNORECASE
)) &&
3762 u8_strcmp(snm
, tnm
, 0, zfsvfs
->z_norm
, U8_UNICODE_LATEST
,
3765 * case preserving rename request, require exact
3774 * If the source and destination directories are the same, we should
3775 * grab the z_name_lock of that directory only once.
3779 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3783 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3784 ZEXISTS
| zflg
, NULL
, NULL
);
3785 terr
= zfs_dirent_lock(&tdl
,
3786 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3788 terr
= zfs_dirent_lock(&tdl
,
3789 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3790 serr
= zfs_dirent_lock(&sdl
,
3791 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3797 * Source entry invalid or not there.
3800 zfs_dirent_unlock(tdl
);
3806 rw_exit(&sdzp
->z_name_lock
);
3808 if (strcmp(snm
, "..") == 0)
3814 zfs_dirent_unlock(sdl
);
3818 rw_exit(&sdzp
->z_name_lock
);
3820 if (strcmp(tnm
, "..") == 0)
3827 * If we are using project inheritance, means if the directory has
3828 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3829 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3830 * such case, we only allow renames into our tree when the project
3833 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3834 tdzp
->z_projid
!= szp
->z_projid
) {
3835 error
= SET_ERROR(EXDEV
);
3840 * Must have write access at the source to remove the old entry
3841 * and write access at the target to create the new entry.
3842 * Note that if target and source are the same, this can be
3843 * done in a single check.
3846 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
3849 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3851 * Check to make sure rename is valid.
3852 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3854 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
3859 * Does target exist?
3863 * Source and target must be the same type.
3865 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3866 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3867 error
= SET_ERROR(ENOTDIR
);
3871 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3872 error
= SET_ERROR(EISDIR
);
3877 * POSIX dictates that when the source and target
3878 * entries refer to the same file object, rename
3879 * must do nothing and exit without error.
3881 if (szp
->z_id
== tzp
->z_id
) {
3887 tx
= dmu_tx_create(zfsvfs
->z_os
);
3888 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3889 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3890 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3891 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3893 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3894 zfs_sa_upgrade_txholds(tx
, tdzp
);
3897 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3898 zfs_sa_upgrade_txholds(tx
, tzp
);
3901 zfs_sa_upgrade_txholds(tx
, szp
);
3902 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3903 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
3906 zfs_rename_unlock(&zl
);
3907 zfs_dirent_unlock(sdl
);
3908 zfs_dirent_unlock(tdl
);
3911 rw_exit(&sdzp
->z_name_lock
);
3913 if (error
== ERESTART
) {
3930 if (tzp
) /* Attempt to remove the existing target */
3931 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3934 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3936 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3937 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
)
3938 szp
->z_pflags
|= ZFS_PROJINHERIT
;
3940 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
3941 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3944 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3946 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3947 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
3948 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3951 * At this point, we have successfully created
3952 * the target name, but have failed to remove
3953 * the source name. Since the create was done
3954 * with the ZRENAMING flag, there are
3955 * complications; for one, the link count is
3956 * wrong. The easiest way to deal with this
3957 * is to remove the newly created target, and
3958 * return the original error. This must
3959 * succeed; fortunately, it is very unlikely to
3960 * fail, since we just created it.
3962 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3963 ZRENAMING
, NULL
), ==, 0);
3967 * If we had removed the existing target, subsequent
3968 * call to zfs_link_create() to add back the same entry
3969 * but, the new dnode (szp) should not fail.
3971 ASSERT(tzp
== NULL
);
3978 zfs_rename_unlock(&zl
);
3980 zfs_dirent_unlock(sdl
);
3981 zfs_dirent_unlock(tdl
);
3983 zfs_inode_update(sdzp
);
3985 rw_exit(&sdzp
->z_name_lock
);
3988 zfs_inode_update(tdzp
);
3990 zfs_inode_update(szp
);
3993 zfs_inode_update(tzp
);
3997 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3998 zil_commit(zilog
, 0);
4005 * Insert the indicated symbolic reference entry into the directory.
4007 * IN: dip - Directory to contain new symbolic link.
4008 * link - Name for new symlink entry.
4009 * vap - Attributes of new entry.
4010 * target - Target path of new symlink.
4012 * cr - credentials of caller.
4013 * flags - case flags
4015 * RETURN: 0 on success, error code on failure.
4018 * dip - ctime|mtime updated
4022 zfs_symlink(struct inode
*dip
, char *name
, vattr_t
*vap
, char *link
,
4023 struct inode
**ipp
, cred_t
*cr
, int flags
)
4025 znode_t
*zp
, *dzp
= ITOZ(dip
);
4028 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
4030 uint64_t len
= strlen(link
);
4033 zfs_acl_ids_t acl_ids
;
4034 boolean_t fuid_dirtied
;
4035 uint64_t txtype
= TX_SYMLINK
;
4036 boolean_t waited
= B_FALSE
;
4038 ASSERT(S_ISLNK(vap
->va_mode
));
4041 return (SET_ERROR(EINVAL
));
4045 zilog
= zfsvfs
->z_log
;
4047 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
4048 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
4050 return (SET_ERROR(EILSEQ
));
4052 if (flags
& FIGNORECASE
)
4055 if (len
> MAXPATHLEN
) {
4057 return (SET_ERROR(ENAMETOOLONG
));
4060 if ((error
= zfs_acl_ids_create(dzp
, 0,
4061 vap
, cr
, NULL
, &acl_ids
)) != 0) {
4069 * Attempt to lock directory; fail if entry already exists.
4071 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
4073 zfs_acl_ids_free(&acl_ids
);
4078 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
4079 zfs_acl_ids_free(&acl_ids
);
4080 zfs_dirent_unlock(dl
);
4085 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, ZFS_DEFAULT_PROJID
)) {
4086 zfs_acl_ids_free(&acl_ids
);
4087 zfs_dirent_unlock(dl
);
4089 return (SET_ERROR(EDQUOT
));
4091 tx
= dmu_tx_create(zfsvfs
->z_os
);
4092 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
4093 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
4094 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
4095 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
4096 ZFS_SA_BASE_ATTR_SIZE
+ len
);
4097 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
4098 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
4099 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
4100 acl_ids
.z_aclp
->z_acl_bytes
);
4103 zfs_fuid_txhold(zfsvfs
, tx
);
4104 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
4106 zfs_dirent_unlock(dl
);
4107 if (error
== ERESTART
) {
4113 zfs_acl_ids_free(&acl_ids
);
4120 * Create a new object for the symlink.
4121 * for version 4 ZPL datsets the symlink will be an SA attribute
4123 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
4126 zfs_fuid_sync(zfsvfs
, tx
);
4128 mutex_enter(&zp
->z_lock
);
4130 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zfsvfs
),
4133 zfs_sa_symlink(zp
, link
, len
, tx
);
4134 mutex_exit(&zp
->z_lock
);
4137 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
4138 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
4140 * Insert the new object into the directory.
4142 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
4144 zfs_znode_delete(zp
, tx
);
4145 remove_inode_hash(ZTOI(zp
));
4147 if (flags
& FIGNORECASE
)
4149 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
4151 zfs_inode_update(dzp
);
4152 zfs_inode_update(zp
);
4155 zfs_acl_ids_free(&acl_ids
);
4159 zfs_dirent_unlock(dl
);
4164 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4165 zil_commit(zilog
, 0);
4175 * Return, in the buffer contained in the provided uio structure,
4176 * the symbolic path referred to by ip.
4178 * IN: ip - inode of symbolic link
4179 * uio - structure to contain the link path.
4180 * cr - credentials of caller.
4182 * RETURN: 0 if success
4183 * error code if failure
4186 * ip - atime updated
4190 zfs_readlink(struct inode
*ip
, uio_t
*uio
, cred_t
*cr
)
4192 znode_t
*zp
= ITOZ(ip
);
4193 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4199 mutex_enter(&zp
->z_lock
);
4201 error
= sa_lookup_uio(zp
->z_sa_hdl
,
4202 SA_ZPL_SYMLINK(zfsvfs
), uio
);
4204 error
= zfs_sa_readlink(zp
, uio
);
4205 mutex_exit(&zp
->z_lock
);
4212 * Insert a new entry into directory tdip referencing sip.
4214 * IN: tdip - Directory to contain new entry.
4215 * sip - inode of new entry.
4216 * name - name of new entry.
4217 * cr - credentials of caller.
4219 * RETURN: 0 if success
4220 * error code if failure
4223 * tdip - ctime|mtime updated
4224 * sip - ctime updated
4228 zfs_link(struct inode
*tdip
, struct inode
*sip
, char *name
, cred_t
*cr
,
4231 znode_t
*dzp
= ITOZ(tdip
);
4233 zfsvfs_t
*zfsvfs
= ITOZSB(tdip
);
4241 boolean_t waited
= B_FALSE
;
4242 boolean_t is_tmpfile
= 0;
4245 is_tmpfile
= (sip
->i_nlink
== 0 && (sip
->i_state
& I_LINKABLE
));
4247 ASSERT(S_ISDIR(tdip
->i_mode
));
4250 return (SET_ERROR(EINVAL
));
4254 zilog
= zfsvfs
->z_log
;
4257 * POSIX dictates that we return EPERM here.
4258 * Better choices include ENOTSUP or EISDIR.
4260 if (S_ISDIR(sip
->i_mode
)) {
4262 return (SET_ERROR(EPERM
));
4269 * If we are using project inheritance, means if the directory has
4270 * ZFS_PROJINHERIT set, then its descendant directories will inherit
4271 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
4272 * such case, we only allow hard link creation in our tree when the
4273 * project IDs are the same.
4275 if (dzp
->z_pflags
& ZFS_PROJINHERIT
&& dzp
->z_projid
!= szp
->z_projid
) {
4277 return (SET_ERROR(EXDEV
));
4281 * We check i_sb because snapshots and the ctldir must have different
4284 if (sip
->i_sb
!= tdip
->i_sb
|| zfsctl_is_node(sip
)) {
4286 return (SET_ERROR(EXDEV
));
4289 /* Prevent links to .zfs/shares files */
4291 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
4292 &parent
, sizeof (uint64_t))) != 0) {
4296 if (parent
== zfsvfs
->z_shares_dir
) {
4298 return (SET_ERROR(EPERM
));
4301 if (zfsvfs
->z_utf8
&& u8_validate(name
,
4302 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
4304 return (SET_ERROR(EILSEQ
));
4306 if (flags
& FIGNORECASE
)
4310 * We do not support links between attributes and non-attributes
4311 * because of the potential security risk of creating links
4312 * into "normal" file space in order to circumvent restrictions
4313 * imposed in attribute space.
4315 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
4317 return (SET_ERROR(EINVAL
));
4320 owner
= zfs_fuid_map_id(zfsvfs
, KUID_TO_SUID(sip
->i_uid
),
4322 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
4324 return (SET_ERROR(EPERM
));
4327 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
4334 * Attempt to lock directory; fail if entry already exists.
4336 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
4342 tx
= dmu_tx_create(zfsvfs
->z_os
);
4343 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
4344 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
4346 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
4348 zfs_sa_upgrade_txholds(tx
, szp
);
4349 zfs_sa_upgrade_txholds(tx
, dzp
);
4350 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
4352 zfs_dirent_unlock(dl
);
4353 if (error
== ERESTART
) {
4363 /* unmark z_unlinked so zfs_link_create will not reject */
4365 szp
->z_unlinked
= 0;
4366 error
= zfs_link_create(dl
, szp
, tx
, 0);
4369 uint64_t txtype
= TX_LINK
;
4371 * tmpfile is created to be in z_unlinkedobj, so remove it.
4372 * Also, we don't log in ZIL, be cause all previous file
4373 * operation on the tmpfile are ignored by ZIL. Instead we
4374 * always wait for txg to sync to make sure all previous
4375 * operation are sync safe.
4378 VERIFY(zap_remove_int(zfsvfs
->z_os
,
4379 zfsvfs
->z_unlinkedobj
, szp
->z_id
, tx
) == 0);
4381 if (flags
& FIGNORECASE
)
4383 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
4385 } else if (is_tmpfile
) {
4386 /* restore z_unlinked since when linking failed */
4387 szp
->z_unlinked
= 1;
4389 txg
= dmu_tx_get_txg(tx
);
4392 zfs_dirent_unlock(dl
);
4394 if (!is_tmpfile
&& zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4395 zil_commit(zilog
, 0);
4398 txg_wait_synced(dmu_objset_pool(zfsvfs
->z_os
), txg
);
4400 zfs_inode_update(dzp
);
4401 zfs_inode_update(szp
);
4407 zfs_putpage_commit_cb(void *arg
)
4409 struct page
*pp
= arg
;
4412 end_page_writeback(pp
);
4416 * Push a page out to disk, once the page is on stable storage the
4417 * registered commit callback will be run as notification of completion.
4419 * IN: ip - page mapped for inode.
4420 * pp - page to push (page is locked)
4421 * wbc - writeback control data
4423 * RETURN: 0 if success
4424 * error code if failure
4427 * ip - ctime|mtime updated
4431 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
4433 znode_t
*zp
= ITOZ(ip
);
4434 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4442 uint64_t mtime
[2], ctime
[2];
4443 sa_bulk_attr_t bulk
[3];
4445 struct address_space
*mapping
;
4450 ASSERT(PageLocked(pp
));
4452 pgoff
= page_offset(pp
); /* Page byte-offset in file */
4453 offset
= i_size_read(ip
); /* File length in bytes */
4454 pglen
= MIN(PAGE_SIZE
, /* Page length in bytes */
4455 P2ROUNDUP(offset
, PAGE_SIZE
)-pgoff
);
4457 /* Page is beyond end of file */
4458 if (pgoff
>= offset
) {
4464 /* Truncate page length to end of file */
4465 if (pgoff
+ pglen
> offset
)
4466 pglen
= offset
- pgoff
;
4470 * FIXME: Allow mmap writes past its quota. The correct fix
4471 * is to register a page_mkwrite() handler to count the page
4472 * against its quota when it is about to be dirtied.
4474 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
,
4475 KUID_TO_SUID(ip
->i_uid
)) ||
4476 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
4477 KGID_TO_SGID(ip
->i_gid
)) ||
4478 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
4479 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
4486 * The ordering here is critical and must adhere to the following
4487 * rules in order to avoid deadlocking in either zfs_read() or
4488 * zfs_free_range() due to a lock inversion.
4490 * 1) The page must be unlocked prior to acquiring the range lock.
4491 * This is critical because zfs_read() calls find_lock_page()
4492 * which may block on the page lock while holding the range lock.
4494 * 2) Before setting or clearing write back on a page the range lock
4495 * must be held in order to prevent a lock inversion with the
4496 * zfs_free_range() function.
4498 * This presents a problem because upon entering this function the
4499 * page lock is already held. To safely acquire the range lock the
4500 * page lock must be dropped. This creates a window where another
4501 * process could truncate, invalidate, dirty, or write out the page.
4503 * Therefore, after successfully reacquiring the range and page locks
4504 * the current page state is checked. In the common case everything
4505 * will be as is expected and it can be written out. However, if
4506 * the page state has changed it must be handled accordingly.
4508 mapping
= pp
->mapping
;
4509 redirty_page_for_writepage(wbc
, pp
);
4512 rl
= zfs_range_lock(&zp
->z_range_lock
, pgoff
, pglen
, RL_WRITER
);
4515 /* Page mapping changed or it was no longer dirty, we're done */
4516 if (unlikely((mapping
!= pp
->mapping
) || !PageDirty(pp
))) {
4518 zfs_range_unlock(rl
);
4523 /* Another process started write block if required */
4524 if (PageWriteback(pp
)) {
4526 zfs_range_unlock(rl
);
4528 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
4529 wait_on_page_writeback(pp
);
4535 /* Clear the dirty flag the required locks are held */
4536 if (!clear_page_dirty_for_io(pp
)) {
4538 zfs_range_unlock(rl
);
4544 * Counterpart for redirty_page_for_writepage() above. This page
4545 * was in fact not skipped and should not be counted as if it were.
4547 wbc
->pages_skipped
--;
4548 set_page_writeback(pp
);
4551 tx
= dmu_tx_create(zfsvfs
->z_os
);
4552 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
4553 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4554 zfs_sa_upgrade_txholds(tx
, zp
);
4556 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
4558 if (err
== ERESTART
)
4562 __set_page_dirty_nobuffers(pp
);
4564 end_page_writeback(pp
);
4565 zfs_range_unlock(rl
);
4571 ASSERT3U(pglen
, <=, PAGE_SIZE
);
4572 dmu_write(zfsvfs
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
4575 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
4576 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
4577 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
4580 /* Preserve the mtime and ctime provided by the inode */
4581 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4582 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4583 zp
->z_atime_dirty
= 0;
4586 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4588 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
4589 zfs_putpage_commit_cb
, pp
);
4592 zfs_range_unlock(rl
);
4594 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
4596 * Note that this is rarely called under writepages(), because
4597 * writepages() normally handles the entire commit for
4598 * performance reasons.
4600 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
4608 * Update the system attributes when the inode has been dirtied. For the
4609 * moment we only update the mode, atime, mtime, and ctime.
4612 zfs_dirty_inode(struct inode
*ip
, int flags
)
4614 znode_t
*zp
= ITOZ(ip
);
4615 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4617 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
4618 sa_bulk_attr_t bulk
[4];
4622 if (zfs_is_readonly(zfsvfs
) || dmu_objset_is_snapshot(zfsvfs
->z_os
))
4630 * This is the lazytime semantic indroduced in Linux 4.0
4631 * This flag will only be called from update_time when lazytime is set.
4632 * (Note, I_DIRTY_SYNC will also set if not lazytime)
4633 * Fortunately mtime and ctime are managed within ZFS itself, so we
4634 * only need to dirty atime.
4636 if (flags
== I_DIRTY_TIME
) {
4637 zp
->z_atime_dirty
= 1;
4642 tx
= dmu_tx_create(zfsvfs
->z_os
);
4644 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4645 zfs_sa_upgrade_txholds(tx
, zp
);
4647 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4653 mutex_enter(&zp
->z_lock
);
4654 zp
->z_atime_dirty
= 0;
4656 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zfsvfs
), NULL
, &mode
, 8);
4657 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
4658 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
4659 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
4661 /* Preserve the mode, mtime and ctime provided by the inode */
4662 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4663 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4664 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4669 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4670 mutex_exit(&zp
->z_lock
);
4680 zfs_inactive(struct inode
*ip
)
4682 znode_t
*zp
= ITOZ(ip
);
4683 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4686 int need_unlock
= 0;
4688 /* Only read lock if we haven't already write locked, e.g. rollback */
4689 if (!RW_WRITE_HELD(&zfsvfs
->z_teardown_inactive_lock
)) {
4691 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_READER
);
4693 if (zp
->z_sa_hdl
== NULL
) {
4695 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
4699 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4700 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
4702 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4703 zfs_sa_upgrade_txholds(tx
, zp
);
4704 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4708 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4709 mutex_enter(&zp
->z_lock
);
4710 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
),
4711 (void *)&atime
, sizeof (atime
), tx
);
4712 zp
->z_atime_dirty
= 0;
4713 mutex_exit(&zp
->z_lock
);
4720 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
4724 * Bounds-check the seek operation.
4726 * IN: ip - inode seeking within
4727 * ooff - old file offset
4728 * noffp - pointer to new file offset
4729 * ct - caller context
4731 * RETURN: 0 if success
4732 * EINVAL if new offset invalid
4736 zfs_seek(struct inode
*ip
, offset_t ooff
, offset_t
*noffp
)
4738 if (S_ISDIR(ip
->i_mode
))
4740 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4744 * Fill pages with data from the disk.
4747 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4749 znode_t
*zp
= ITOZ(ip
);
4750 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4752 struct page
*cur_pp
;
4753 u_offset_t io_off
, total
;
4760 io_len
= nr_pages
<< PAGE_SHIFT
;
4761 i_size
= i_size_read(ip
);
4762 io_off
= page_offset(pl
[0]);
4764 if (io_off
+ io_len
> i_size
)
4765 io_len
= i_size
- io_off
;
4768 * Iterate over list of pages and read each page individually.
4771 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4774 cur_pp
= pl
[page_idx
++];
4776 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4780 /* convert checksum errors into IO errors */
4782 err
= SET_ERROR(EIO
);
4791 * Uses zfs_fillpage to read data from the file and fill the pages.
4793 * IN: ip - inode of file to get data from.
4794 * pl - list of pages to read
4795 * nr_pages - number of pages to read
4797 * RETURN: 0 on success, error code on failure.
4800 * vp - atime updated
4804 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4806 znode_t
*zp
= ITOZ(ip
);
4807 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4816 err
= zfs_fillpage(ip
, pl
, nr_pages
);
4823 * Check ZFS specific permissions to memory map a section of a file.
4825 * IN: ip - inode of the file to mmap
4827 * addrp - start address in memory region
4828 * len - length of memory region
4829 * vm_flags- address flags
4831 * RETURN: 0 if success
4832 * error code if failure
4836 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
4837 unsigned long vm_flags
)
4839 znode_t
*zp
= ITOZ(ip
);
4840 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4845 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
4846 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4848 return (SET_ERROR(EPERM
));
4851 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
4852 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4854 return (SET_ERROR(EACCES
));
4857 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4859 return (SET_ERROR(ENXIO
));
4867 * convoff - converts the given data (start, whence) to the
4871 convoff(struct inode
*ip
, flock64_t
*lckdat
, int whence
, offset_t offset
)
4876 if ((lckdat
->l_whence
== 2) || (whence
== 2)) {
4877 if ((error
= zfs_getattr(ip
, &vap
, 0, CRED())))
4881 switch (lckdat
->l_whence
) {
4883 lckdat
->l_start
+= offset
;
4886 lckdat
->l_start
+= vap
.va_size
;
4891 return (SET_ERROR(EINVAL
));
4894 if (lckdat
->l_start
< 0)
4895 return (SET_ERROR(EINVAL
));
4899 lckdat
->l_start
-= offset
;
4902 lckdat
->l_start
-= vap
.va_size
;
4907 return (SET_ERROR(EINVAL
));
4910 lckdat
->l_whence
= (short)whence
;
4915 * Free or allocate space in a file. Currently, this function only
4916 * supports the `F_FREESP' command. However, this command is somewhat
4917 * misnamed, as its functionality includes the ability to allocate as
4918 * well as free space.
4920 * IN: ip - inode of file to free data in.
4921 * cmd - action to take (only F_FREESP supported).
4922 * bfp - section of file to free/alloc.
4923 * flag - current file open mode flags.
4924 * offset - current file offset.
4925 * cr - credentials of caller [UNUSED].
4927 * RETURN: 0 on success, error code on failure.
4930 * ip - ctime|mtime updated
4934 zfs_space(struct inode
*ip
, int cmd
, flock64_t
*bfp
, int flag
,
4935 offset_t offset
, cred_t
*cr
)
4937 znode_t
*zp
= ITOZ(ip
);
4938 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4945 if (cmd
!= F_FREESP
) {
4947 return (SET_ERROR(EINVAL
));
4951 * Callers might not be able to detect properly that we are read-only,
4952 * so check it explicitly here.
4954 if (zfs_is_readonly(zfsvfs
)) {
4956 return (SET_ERROR(EROFS
));
4959 if ((error
= convoff(ip
, bfp
, 0, offset
))) {
4964 if (bfp
->l_len
< 0) {
4966 return (SET_ERROR(EINVAL
));
4970 * Permissions aren't checked on Solaris because on this OS
4971 * zfs_space() can only be called with an opened file handle.
4972 * On Linux we can get here through truncate_range() which
4973 * operates directly on inodes, so we need to check access rights.
4975 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
4981 len
= bfp
->l_len
; /* 0 means from off to end of file */
4983 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4991 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4993 znode_t
*zp
= ITOZ(ip
);
4994 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4997 uint64_t object
= zp
->z_id
;
5004 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
),
5005 &gen64
, sizeof (uint64_t))) != 0) {
5010 gen
= (uint32_t)gen64
;
5012 size
= SHORT_FID_LEN
;
5014 zfid
= (zfid_short_t
*)fidp
;
5016 zfid
->zf_len
= size
;
5018 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
5019 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
5021 /* Must have a non-zero generation number to distinguish from .zfs */
5024 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
5025 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
5033 zfs_getsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
5035 znode_t
*zp
= ITOZ(ip
);
5036 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
5038 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
5042 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
5050 zfs_setsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
5052 znode_t
*zp
= ITOZ(ip
);
5053 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
5055 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
5056 zilog_t
*zilog
= zfsvfs
->z_log
;
5061 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
5063 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
5064 zil_commit(zilog
, 0);
5070 #ifdef HAVE_UIO_ZEROCOPY
5072 * Tunable, both must be a power of 2.
5074 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
5075 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
5076 * an arcbuf for a partial block read
5078 int zcr_blksz_min
= (1 << 10); /* 1K */
5079 int zcr_blksz_max
= (1 << 17); /* 128K */
5083 zfs_reqzcbuf(struct inode
*ip
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
)
5085 znode_t
*zp
= ITOZ(ip
);
5086 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
5087 int max_blksz
= zfsvfs
->z_max_blksz
;
5088 uio_t
*uio
= &xuio
->xu_uio
;
5089 ssize_t size
= uio
->uio_resid
;
5090 offset_t offset
= uio
->uio_loffset
;
5095 int preamble
, postamble
;
5097 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
5098 return (SET_ERROR(EINVAL
));
5105 * Loan out an arc_buf for write if write size is bigger than
5106 * max_blksz, and the file's block size is also max_blksz.
5109 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
5111 return (SET_ERROR(EINVAL
));
5114 * Caller requests buffers for write before knowing where the
5115 * write offset might be (e.g. NFS TCP write).
5120 preamble
= P2PHASE(offset
, blksz
);
5122 preamble
= blksz
- preamble
;
5127 postamble
= P2PHASE(size
, blksz
);
5130 fullblk
= size
/ blksz
;
5131 (void) dmu_xuio_init(xuio
,
5132 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
5135 * Have to fix iov base/len for partial buffers. They
5136 * currently represent full arc_buf's.
5139 /* data begins in the middle of the arc_buf */
5140 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5143 (void) dmu_xuio_add(xuio
, abuf
,
5144 blksz
- preamble
, preamble
);
5147 for (i
= 0; i
< fullblk
; i
++) {
5148 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5151 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
5155 /* data ends in the middle of the arc_buf */
5156 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5159 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
5164 * Loan out an arc_buf for read if the read size is larger than
5165 * the current file block size. Block alignment is not
5166 * considered. Partial arc_buf will be loaned out for read.
5168 blksz
= zp
->z_blksz
;
5169 if (blksz
< zcr_blksz_min
)
5170 blksz
= zcr_blksz_min
;
5171 if (blksz
> zcr_blksz_max
)
5172 blksz
= zcr_blksz_max
;
5173 /* avoid potential complexity of dealing with it */
5174 if (blksz
> max_blksz
) {
5176 return (SET_ERROR(EINVAL
));
5179 maxsize
= zp
->z_size
- uio
->uio_loffset
;
5185 return (SET_ERROR(EINVAL
));
5190 return (SET_ERROR(EINVAL
));
5193 uio
->uio_extflg
= UIO_XUIO
;
5194 XUIO_XUZC_RW(xuio
) = ioflag
;
5201 zfs_retzcbuf(struct inode
*ip
, xuio_t
*xuio
, cred_t
*cr
)
5205 int ioflag
= XUIO_XUZC_RW(xuio
);
5207 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
5209 i
= dmu_xuio_cnt(xuio
);
5211 abuf
= dmu_xuio_arcbuf(xuio
, i
);
5213 * if abuf == NULL, it must be a write buffer
5214 * that has been returned in zfs_write().
5217 dmu_return_arcbuf(abuf
);
5218 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
5221 dmu_xuio_fini(xuio
);
5224 #endif /* HAVE_UIO_ZEROCOPY */
5226 #if defined(_KERNEL)
5227 EXPORT_SYMBOL(zfs_open
);
5228 EXPORT_SYMBOL(zfs_close
);
5229 EXPORT_SYMBOL(zfs_read
);
5230 EXPORT_SYMBOL(zfs_write
);
5231 EXPORT_SYMBOL(zfs_access
);
5232 EXPORT_SYMBOL(zfs_lookup
);
5233 EXPORT_SYMBOL(zfs_create
);
5234 EXPORT_SYMBOL(zfs_tmpfile
);
5235 EXPORT_SYMBOL(zfs_remove
);
5236 EXPORT_SYMBOL(zfs_mkdir
);
5237 EXPORT_SYMBOL(zfs_rmdir
);
5238 EXPORT_SYMBOL(zfs_readdir
);
5239 EXPORT_SYMBOL(zfs_fsync
);
5240 EXPORT_SYMBOL(zfs_getattr
);
5241 EXPORT_SYMBOL(zfs_getattr_fast
);
5242 EXPORT_SYMBOL(zfs_setattr
);
5243 EXPORT_SYMBOL(zfs_rename
);
5244 EXPORT_SYMBOL(zfs_symlink
);
5245 EXPORT_SYMBOL(zfs_readlink
);
5246 EXPORT_SYMBOL(zfs_link
);
5247 EXPORT_SYMBOL(zfs_inactive
);
5248 EXPORT_SYMBOL(zfs_space
);
5249 EXPORT_SYMBOL(zfs_fid
);
5250 EXPORT_SYMBOL(zfs_getsecattr
);
5251 EXPORT_SYMBOL(zfs_setsecattr
);
5252 EXPORT_SYMBOL(zfs_getpage
);
5253 EXPORT_SYMBOL(zfs_putpage
);
5254 EXPORT_SYMBOL(zfs_dirty_inode
);
5255 EXPORT_SYMBOL(zfs_map
);
5258 module_param(zfs_delete_blocks
, ulong
, 0644);
5259 MODULE_PARM_DESC(zfs_delete_blocks
, "Delete files larger than N blocks async");
5260 module_param(zfs_read_chunk_size
, long, 0644);
5261 MODULE_PARM_DESC(zfs_read_chunk_size
, "Bytes to read per chunk");