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, 2018 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
)
442 boolean_t frsync
= B_FALSE
;
444 znode_t
*zp
= ITOZ(ip
);
445 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
449 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
451 return (SET_ERROR(EACCES
));
455 * Validate file offset
457 if (uio
->uio_loffset
< (offset_t
)0) {
459 return (SET_ERROR(EINVAL
));
463 * Fasttrack empty reads
465 if (uio
->uio_resid
== 0) {
472 * If we're in FRSYNC mode, sync out this znode before reading it.
473 * Only do this for non-snapshots.
475 * Some platforms do not support FRSYNC and instead map it
476 * to FSYNC, which results in unnecessary calls to zil_commit. We
477 * only honor FRSYNC requests on platforms which support it.
479 frsync
= !!(ioflag
& FRSYNC
);
482 (frsync
|| zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
))
483 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
486 * Lock the range against changes.
488 locked_range_t
*lr
= rangelock_enter(&zp
->z_rangelock
,
489 uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
492 * If we are reading past end-of-file we can skip
493 * to the end; but we might still need to set atime.
495 if (uio
->uio_loffset
>= zp
->z_size
) {
500 ASSERT(uio
->uio_loffset
< zp
->z_size
);
501 ssize_t n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
502 ssize_t start_resid
= n
;
504 #ifdef HAVE_UIO_ZEROCOPY
506 if ((uio
->uio_extflg
== UIO_XUIO
) &&
507 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
509 int blksz
= zp
->z_blksz
;
510 uint64_t offset
= uio
->uio_loffset
;
512 xuio
= (xuio_t
*)uio
;
514 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
517 ASSERT(offset
+ n
<= blksz
);
520 (void) dmu_xuio_init(xuio
, nblk
);
522 if (vn_has_cached_data(ip
)) {
524 * For simplicity, we always allocate a full buffer
525 * even if we only expect to read a portion of a block.
527 while (--nblk
>= 0) {
528 (void) dmu_xuio_add(xuio
,
529 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
534 #endif /* HAVE_UIO_ZEROCOPY */
537 ssize_t nbytes
= MIN(n
, zfs_read_chunk_size
-
538 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
540 if (zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
)) {
541 error
= mappedread(ip
, nbytes
, uio
);
543 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
548 /* convert checksum errors into IO errors */
550 error
= SET_ERROR(EIO
);
557 int64_t nread
= start_resid
- n
;
558 dataset_kstats_update_read_kstats(&zfsvfs
->z_kstat
, nread
);
559 task_io_account_read(nread
);
568 * Write the bytes to a file.
570 * IN: ip - inode of file to be written to.
571 * uio - structure supplying write location, range info,
573 * ioflag - FAPPEND flag set if in append mode.
574 * O_DIRECT flag; used to bypass page cache.
575 * cr - credentials of caller.
577 * OUT: uio - updated offset and range.
579 * RETURN: 0 if success
580 * error code if failure
583 * ip - ctime|mtime updated if byte count > 0
588 zfs_write(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
591 ssize_t start_resid
= uio
->uio_resid
;
594 * Fasttrack empty write
596 ssize_t n
= start_resid
;
600 rlim64_t limit
= uio
->uio_limit
;
601 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
604 znode_t
*zp
= ITOZ(ip
);
605 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
609 sa_bulk_attr_t bulk
[4];
611 uint64_t mtime
[2], ctime
[2];
612 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
613 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
614 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
616 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
620 * Callers might not be able to detect properly that we are read-only,
621 * so check it explicitly here.
623 if (zfs_is_readonly(zfsvfs
)) {
625 return (SET_ERROR(EROFS
));
629 * If immutable or not appending then return EPERM
631 if ((zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
632 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
633 (uio
->uio_loffset
< zp
->z_size
))) {
635 return (SET_ERROR(EPERM
));
639 * Validate file offset
641 offset_t woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
644 return (SET_ERROR(EINVAL
));
647 int max_blksz
= zfsvfs
->z_max_blksz
;
651 * Pre-fault the pages to ensure slow (eg NFS) pages
653 * Skip this if uio contains loaned arc_buf.
655 #ifdef HAVE_UIO_ZEROCOPY
656 if ((uio
->uio_extflg
== UIO_XUIO
) &&
657 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
658 xuio
= (xuio_t
*)uio
;
661 if (uio_prefaultpages(MIN(n
, max_blksz
), uio
)) {
663 return (SET_ERROR(EFAULT
));
667 * If in append mode, set the io offset pointer to eof.
670 if (ioflag
& FAPPEND
) {
672 * Obtain an appending range lock to guarantee file append
673 * semantics. We reset the write offset once we have the lock.
675 lr
= rangelock_enter(&zp
->z_rangelock
, 0, n
, RL_APPEND
);
676 woff
= lr
->lr_offset
;
677 if (lr
->lr_length
== UINT64_MAX
) {
679 * We overlocked the file because this write will cause
680 * the file block size to increase.
681 * Note that zp_size cannot change with this lock held.
685 uio
->uio_loffset
= woff
;
688 * Note that if the file block size will change as a result of
689 * this write, then this range lock will lock the entire file
690 * so that we can re-write the block safely.
692 lr
= rangelock_enter(&zp
->z_rangelock
, woff
, n
, RL_WRITER
);
698 return (SET_ERROR(EFBIG
));
701 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
704 /* Will this write extend the file length? */
705 int write_eof
= (woff
+ n
> zp
->z_size
);
707 uint64_t end_size
= MAX(zp
->z_size
, woff
+ n
);
708 zilog_t
*zilog
= zfsvfs
->z_log
;
709 #ifdef HAVE_UIO_ZEROCOPY
711 const iovec_t
*iovp
= uio
->uio_iov
;
712 ASSERTV(int iovcnt
= uio
->uio_iovcnt
);
717 * Write the file in reasonable size chunks. Each chunk is written
718 * in a separate transaction; this keeps the intent log records small
719 * and allows us to do more fine-grained space accounting.
722 woff
= uio
->uio_loffset
;
724 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
,
725 KUID_TO_SUID(ip
->i_uid
)) ||
726 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
727 KGID_TO_SGID(ip
->i_gid
)) ||
728 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
729 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
731 error
= SET_ERROR(EDQUOT
);
735 arc_buf_t
*abuf
= NULL
;
736 const iovec_t
*aiov
= NULL
;
738 #ifdef HAVE_UIO_ZEROCOPY
739 ASSERT(i_iov
< iovcnt
);
740 ASSERT3U(uio
->uio_segflg
, !=, UIO_BVEC
);
742 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
743 dmu_xuio_clear(xuio
, i_iov
);
744 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
745 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
746 aiov
->iov_len
== arc_buf_size(abuf
)));
749 } else if (n
>= max_blksz
&& woff
>= zp
->z_size
&&
750 P2PHASE(woff
, max_blksz
) == 0 &&
751 zp
->z_blksz
== max_blksz
) {
753 * This write covers a full block. "Borrow" a buffer
754 * from the dmu so that we can fill it before we enter
755 * a transaction. This avoids the possibility of
756 * holding up the transaction if the data copy hangs
757 * up on a pagefault (e.g., from an NFS server mapping).
761 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
763 ASSERT(abuf
!= NULL
);
764 ASSERT(arc_buf_size(abuf
) == max_blksz
);
765 if ((error
= uiocopy(abuf
->b_data
, max_blksz
,
766 UIO_WRITE
, uio
, &cbytes
))) {
767 dmu_return_arcbuf(abuf
);
770 ASSERT(cbytes
== max_blksz
);
774 * Start a transaction.
776 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
777 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
778 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
779 zfs_sa_upgrade_txholds(tx
, zp
);
780 error
= dmu_tx_assign(tx
, TXG_WAIT
);
784 dmu_return_arcbuf(abuf
);
789 * If rangelock_enter() over-locked we grow the blocksize
790 * and then reduce the lock range. This will only happen
791 * on the first iteration since rangelock_reduce() will
792 * shrink down lr_length to the appropriate size.
794 if (lr
->lr_length
== UINT64_MAX
) {
797 if (zp
->z_blksz
> max_blksz
) {
799 * File's blocksize is already larger than the
800 * "recordsize" property. Only let it grow to
801 * the next power of 2.
803 ASSERT(!ISP2(zp
->z_blksz
));
804 new_blksz
= MIN(end_size
,
805 1 << highbit64(zp
->z_blksz
));
807 new_blksz
= MIN(end_size
, max_blksz
);
809 zfs_grow_blocksize(zp
, new_blksz
, tx
);
810 rangelock_reduce(lr
, woff
, n
);
814 * XXX - should we really limit each write to z_max_blksz?
815 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
817 ssize_t nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
821 tx_bytes
= uio
->uio_resid
;
822 uio
->uio_fault_disable
= B_TRUE
;
823 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
825 if (error
== EFAULT
) {
827 if (uio_prefaultpages(MIN(n
, max_blksz
), uio
)) {
831 } else if (error
!= 0) {
835 tx_bytes
-= uio
->uio_resid
;
838 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
840 * If this is not a full block write, but we are
841 * extending the file past EOF and this data starts
842 * block-aligned, use assign_arcbuf(). Otherwise,
843 * write via dmu_write().
845 if (tx_bytes
< max_blksz
&& (!write_eof
||
846 aiov
->iov_base
!= abuf
->b_data
)) {
848 dmu_write(zfsvfs
->z_os
, zp
->z_id
, woff
,
849 /* cppcheck-suppress nullPointer */
850 aiov
->iov_len
, aiov
->iov_base
, tx
);
851 dmu_return_arcbuf(abuf
);
852 xuio_stat_wbuf_copied();
854 ASSERT(xuio
|| tx_bytes
== max_blksz
);
855 error
= dmu_assign_arcbuf_by_dbuf(
856 sa_get_db(zp
->z_sa_hdl
), woff
, abuf
, tx
);
858 dmu_return_arcbuf(abuf
);
863 ASSERT(tx_bytes
<= uio
->uio_resid
);
864 uioskip(uio
, tx_bytes
);
866 if (tx_bytes
&& zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
)) {
867 update_pages(ip
, woff
,
868 tx_bytes
, zfsvfs
->z_os
, zp
->z_id
);
872 * If we made no progress, we're done. If we made even
873 * partial progress, update the znode and ZIL accordingly.
876 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
877 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
884 * Clear Set-UID/Set-GID bits on successful write if not
885 * privileged and at least one of the execute bits is set.
887 * It would be nice to to this after all writes have
888 * been done, but that would still expose the ISUID/ISGID
889 * to another app after the partial write is committed.
891 * Note: we don't call zfs_fuid_map_id() here because
892 * user 0 is not an ephemeral uid.
894 mutex_enter(&zp
->z_acl_lock
);
895 uint32_t uid
= KUID_TO_SUID(ip
->i_uid
);
896 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
897 (S_IXUSR
>> 6))) != 0 &&
898 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
899 secpolicy_vnode_setid_retain(cr
,
900 ((zp
->z_mode
& S_ISUID
) != 0 && uid
== 0)) != 0) {
902 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
903 ip
->i_mode
= newmode
= zp
->z_mode
;
904 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zfsvfs
),
905 (void *)&newmode
, sizeof (uint64_t), tx
);
907 mutex_exit(&zp
->z_acl_lock
);
909 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
912 * Update the file size (zp_size) if it has changed;
913 * account for possible concurrent updates.
915 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
916 (void) atomic_cas_64(&zp
->z_size
, end_size
,
921 * If we are replaying and eof is non zero then force
922 * the file size to the specified eof. Note, there's no
923 * concurrency during replay.
925 if (zfsvfs
->z_replay
&& zfsvfs
->z_replay_eof
!= 0)
926 zp
->z_size
= zfsvfs
->z_replay_eof
;
928 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
930 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
,
936 ASSERT(tx_bytes
== nbytes
);
939 if (!xuio
&& n
> 0) {
940 if (uio_prefaultpages(MIN(n
, max_blksz
), uio
)) {
947 zfs_inode_update(zp
);
951 * If we're in replay mode, or we made no progress, return error.
952 * Otherwise, it's at least a partial write, so it's successful.
954 if (zfsvfs
->z_replay
|| uio
->uio_resid
== start_resid
) {
959 if (ioflag
& (FSYNC
| FDSYNC
) ||
960 zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
961 zil_commit(zilog
, zp
->z_id
);
963 int64_t nwritten
= start_resid
- uio
->uio_resid
;
964 dataset_kstats_update_write_kstats(&zfsvfs
->z_kstat
, nwritten
);
965 task_io_account_write(nwritten
);
972 * Drop a reference on the passed inode asynchronously. This ensures
973 * that the caller will never drop the last reference on an inode in
974 * the current context. Doing so while holding open a tx could result
975 * in a deadlock if iput_final() re-enters the filesystem code.
978 zfs_iput_async(struct inode
*ip
)
980 objset_t
*os
= ITOZSB(ip
)->z_os
;
982 ASSERT(atomic_read(&ip
->i_count
) > 0);
985 if (atomic_read(&ip
->i_count
) == 1)
986 VERIFY(taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os
)),
987 (task_func_t
*)iput
, ip
, TQ_SLEEP
) != TASKQID_INVALID
);
994 zfs_get_done(zgd_t
*zgd
, int error
)
996 znode_t
*zp
= zgd
->zgd_private
;
999 dmu_buf_rele(zgd
->zgd_db
, zgd
);
1001 rangelock_exit(zgd
->zgd_lr
);
1004 * Release the vnode asynchronously as we currently have the
1005 * txg stopped from syncing.
1007 zfs_iput_async(ZTOI(zp
));
1009 kmem_free(zgd
, sizeof (zgd_t
));
1013 static int zil_fault_io
= 0;
1017 * Get data to generate a TX_WRITE intent log record.
1020 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
, zio_t
*zio
)
1022 zfsvfs_t
*zfsvfs
= arg
;
1023 objset_t
*os
= zfsvfs
->z_os
;
1025 uint64_t object
= lr
->lr_foid
;
1026 uint64_t offset
= lr
->lr_offset
;
1027 uint64_t size
= lr
->lr_length
;
1032 ASSERT3P(lwb
, !=, NULL
);
1033 ASSERT3P(zio
, !=, NULL
);
1034 ASSERT3U(size
, !=, 0);
1037 * Nothing to do if the file has been removed
1039 if (zfs_zget(zfsvfs
, object
, &zp
) != 0)
1040 return (SET_ERROR(ENOENT
));
1041 if (zp
->z_unlinked
) {
1043 * Release the vnode asynchronously as we currently have the
1044 * txg stopped from syncing.
1046 zfs_iput_async(ZTOI(zp
));
1047 return (SET_ERROR(ENOENT
));
1050 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
1052 zgd
->zgd_private
= zp
;
1055 * Write records come in two flavors: immediate and indirect.
1056 * For small writes it's cheaper to store the data with the
1057 * log record (immediate); for large writes it's cheaper to
1058 * sync the data and get a pointer to it (indirect) so that
1059 * we don't have to write the data twice.
1061 if (buf
!= NULL
) { /* immediate write */
1062 zgd
->zgd_lr
= rangelock_enter(&zp
->z_rangelock
,
1063 offset
, size
, RL_READER
);
1064 /* test for truncation needs to be done while range locked */
1065 if (offset
>= zp
->z_size
) {
1066 error
= SET_ERROR(ENOENT
);
1068 error
= dmu_read(os
, object
, offset
, size
, buf
,
1069 DMU_READ_NO_PREFETCH
);
1071 ASSERT(error
== 0 || error
== ENOENT
);
1072 } else { /* indirect write */
1074 * Have to lock the whole block to ensure when it's
1075 * written out and its checksum is being calculated
1076 * that no one can change the data. We need to re-check
1077 * blocksize after we get the lock in case it's changed!
1082 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
1084 zgd
->zgd_lr
= rangelock_enter(&zp
->z_rangelock
,
1085 offset
, size
, RL_READER
);
1086 if (zp
->z_blksz
== size
)
1089 rangelock_exit(zgd
->zgd_lr
);
1091 /* test for truncation needs to be done while range locked */
1092 if (lr
->lr_offset
>= zp
->z_size
)
1093 error
= SET_ERROR(ENOENT
);
1096 error
= SET_ERROR(EIO
);
1101 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1102 DMU_READ_NO_PREFETCH
);
1105 blkptr_t
*bp
= &lr
->lr_blkptr
;
1110 ASSERT(db
->db_offset
== offset
);
1111 ASSERT(db
->db_size
== size
);
1113 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1115 ASSERT(error
|| lr
->lr_length
<= size
);
1118 * On success, we need to wait for the write I/O
1119 * initiated by dmu_sync() to complete before we can
1120 * release this dbuf. We will finish everything up
1121 * in the zfs_get_done() callback.
1126 if (error
== EALREADY
) {
1127 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
1129 * TX_WRITE2 relies on the data previously
1130 * written by the TX_WRITE that caused
1131 * EALREADY. We zero out the BP because
1132 * it is the old, currently-on-disk BP.
1141 zfs_get_done(zgd
, error
);
1148 zfs_access(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
1150 znode_t
*zp
= ITOZ(ip
);
1151 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
1157 if (flag
& V_ACE_MASK
)
1158 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
1160 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
1167 * Lookup an entry in a directory, or an extended attribute directory.
1168 * If it exists, return a held inode reference for it.
1170 * IN: dip - inode of directory to search.
1171 * nm - name of entry to lookup.
1172 * flags - LOOKUP_XATTR set if looking for an attribute.
1173 * cr - credentials of caller.
1174 * direntflags - directory lookup flags
1175 * realpnp - returned pathname.
1177 * OUT: ipp - inode of located entry, NULL if not found.
1179 * RETURN: 0 on success, error code on failure.
1186 zfs_lookup(struct inode
*dip
, char *nm
, struct inode
**ipp
, int flags
,
1187 cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
1189 znode_t
*zdp
= ITOZ(dip
);
1190 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1194 * Fast path lookup, however we must skip DNLC lookup
1195 * for case folding or normalizing lookups because the
1196 * DNLC code only stores the passed in name. This means
1197 * creating 'a' and removing 'A' on a case insensitive
1198 * file system would work, but DNLC still thinks 'a'
1199 * exists and won't let you create it again on the next
1200 * pass through fast path.
1202 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1204 if (!S_ISDIR(dip
->i_mode
)) {
1205 return (SET_ERROR(ENOTDIR
));
1206 } else if (zdp
->z_sa_hdl
== NULL
) {
1207 return (SET_ERROR(EIO
));
1210 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1211 error
= zfs_fastaccesschk_execute(zdp
, cr
);
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.
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
);
1754 mutex_enter(&zp
->z_lock
);
1755 may_delete_now
= atomic_read(&ip
->i_count
) == 1 && !(zp
->z_is_mapped
);
1756 mutex_exit(&zp
->z_lock
);
1759 * We may delete the znode now, or we may put it in the unlinked set;
1760 * it depends on whether we're the last link, and on whether there are
1761 * other holds on the inode. So we dmu_tx_hold() the right things to
1762 * allow for either case.
1765 tx
= dmu_tx_create(zfsvfs
->z_os
);
1766 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1767 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1768 zfs_sa_upgrade_txholds(tx
, zp
);
1769 zfs_sa_upgrade_txholds(tx
, dzp
);
1770 if (may_delete_now
) {
1771 toobig
= zp
->z_size
> zp
->z_blksz
* zfs_delete_blocks
;
1772 /* if the file is too big, only hold_free a token amount */
1773 dmu_tx_hold_free(tx
, zp
->z_id
, 0,
1774 (toobig
? DMU_MAX_ACCESS
: DMU_OBJECT_END
));
1777 /* are there any extended attributes? */
1778 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1779 &xattr_obj
, sizeof (xattr_obj
));
1780 if (error
== 0 && xattr_obj
) {
1781 error
= zfs_zget(zfsvfs
, xattr_obj
, &xzp
);
1783 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1784 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1787 mutex_enter(&zp
->z_lock
);
1788 if ((acl_obj
= zfs_external_acl(zp
)) != 0 && may_delete_now
)
1789 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
1790 mutex_exit(&zp
->z_lock
);
1792 /* charge as an update -- would be nice not to charge at all */
1793 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1796 * Mark this transaction as typically resulting in a net free of space
1798 dmu_tx_mark_netfree(tx
);
1800 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1802 zfs_dirent_unlock(dl
);
1803 if (error
== ERESTART
) {
1823 * Remove the directory entry.
1825 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1834 * Hold z_lock so that we can make sure that the ACL obj
1835 * hasn't changed. Could have been deleted due to
1838 mutex_enter(&zp
->z_lock
);
1839 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1840 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1841 delete_now
= may_delete_now
&& !toobig
&&
1842 atomic_read(&ip
->i_count
) == 1 && !(zp
->z_is_mapped
) &&
1843 xattr_obj
== xattr_obj_unlinked
&& zfs_external_acl(zp
) ==
1848 if (xattr_obj_unlinked
) {
1849 ASSERT3U(ZTOI(xzp
)->i_nlink
, ==, 2);
1850 mutex_enter(&xzp
->z_lock
);
1851 xzp
->z_unlinked
= 1;
1852 clear_nlink(ZTOI(xzp
));
1854 error
= sa_update(xzp
->z_sa_hdl
, SA_ZPL_LINKS(zfsvfs
),
1855 &links
, sizeof (links
), tx
);
1856 ASSERT3U(error
, ==, 0);
1857 mutex_exit(&xzp
->z_lock
);
1858 zfs_unlinked_add(xzp
, tx
);
1861 error
= sa_remove(zp
->z_sa_hdl
,
1862 SA_ZPL_XATTR(zfsvfs
), tx
);
1864 error
= sa_update(zp
->z_sa_hdl
,
1865 SA_ZPL_XATTR(zfsvfs
), &null_xattr
,
1866 sizeof (uint64_t), tx
);
1870 * Add to the unlinked set because a new reference could be
1871 * taken concurrently resulting in a deferred destruction.
1873 zfs_unlinked_add(zp
, tx
);
1874 mutex_exit(&zp
->z_lock
);
1875 } else if (unlinked
) {
1876 mutex_exit(&zp
->z_lock
);
1877 zfs_unlinked_add(zp
, tx
);
1881 if (flags
& FIGNORECASE
)
1883 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
);
1890 zfs_dirent_unlock(dl
);
1891 zfs_inode_update(dzp
);
1892 zfs_inode_update(zp
);
1900 zfs_inode_update(xzp
);
1901 zfs_iput_async(ZTOI(xzp
));
1904 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1905 zil_commit(zilog
, 0);
1912 * Create a new directory and insert it into dip using the name
1913 * provided. Return a pointer to the inserted directory.
1915 * IN: dip - inode of directory to add subdir to.
1916 * dirname - name of new directory.
1917 * vap - attributes of new directory.
1918 * cr - credentials of caller.
1919 * vsecp - ACL to be set
1921 * OUT: ipp - inode of created directory.
1923 * RETURN: 0 if success
1924 * error code if failure
1927 * dip - ctime|mtime updated
1928 * ipp - ctime|mtime|atime updated
1932 zfs_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
, struct inode
**ipp
,
1933 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1935 znode_t
*zp
, *dzp
= ITOZ(dip
);
1936 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
1944 gid_t gid
= crgetgid(cr
);
1945 zfs_acl_ids_t acl_ids
;
1946 boolean_t fuid_dirtied
;
1947 boolean_t waited
= B_FALSE
;
1949 ASSERT(S_ISDIR(vap
->va_mode
));
1952 * If we have an ephemeral id, ACL, or XVATTR then
1953 * make sure file system is at proper version
1957 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1958 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1959 return (SET_ERROR(EINVAL
));
1961 if (dirname
== NULL
)
1962 return (SET_ERROR(EINVAL
));
1966 zilog
= zfsvfs
->z_log
;
1968 if (dzp
->z_pflags
& ZFS_XATTR
) {
1970 return (SET_ERROR(EINVAL
));
1973 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1974 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1976 return (SET_ERROR(EILSEQ
));
1978 if (flags
& FIGNORECASE
)
1981 if (vap
->va_mask
& ATTR_XVATTR
) {
1982 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1983 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1989 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1990 vsecp
, &acl_ids
)) != 0) {
1995 * First make sure the new directory doesn't exist.
1997 * Existence is checked first to make sure we don't return
1998 * EACCES instead of EEXIST which can cause some applications
2004 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
2006 zfs_acl_ids_free(&acl_ids
);
2011 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
2012 zfs_acl_ids_free(&acl_ids
);
2013 zfs_dirent_unlock(dl
);
2018 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, zfs_inherit_projid(dzp
))) {
2019 zfs_acl_ids_free(&acl_ids
);
2020 zfs_dirent_unlock(dl
);
2022 return (SET_ERROR(EDQUOT
));
2026 * Add a new entry to the directory.
2028 tx
= dmu_tx_create(zfsvfs
->z_os
);
2029 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
2030 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
2031 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
2033 zfs_fuid_txhold(zfsvfs
, tx
);
2034 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2035 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
2036 acl_ids
.z_aclp
->z_acl_bytes
);
2039 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
2040 ZFS_SA_BASE_ATTR_SIZE
);
2042 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
2044 zfs_dirent_unlock(dl
);
2045 if (error
== ERESTART
) {
2051 zfs_acl_ids_free(&acl_ids
);
2060 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
2063 * Now put new name in parent dir.
2065 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
2067 zfs_znode_delete(zp
, tx
);
2068 remove_inode_hash(ZTOI(zp
));
2073 zfs_fuid_sync(zfsvfs
, tx
);
2077 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
2078 if (flags
& FIGNORECASE
)
2080 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
2081 acl_ids
.z_fuidp
, vap
);
2084 zfs_acl_ids_free(&acl_ids
);
2088 zfs_dirent_unlock(dl
);
2090 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2091 zil_commit(zilog
, 0);
2096 zfs_inode_update(dzp
);
2097 zfs_inode_update(zp
);
2104 * Remove a directory subdir entry. If the current working
2105 * directory is the same as the subdir to be removed, the
2108 * IN: dip - inode of directory to remove from.
2109 * name - name of directory to be removed.
2110 * cwd - inode of current working directory.
2111 * cr - credentials of caller.
2112 * flags - case flags
2114 * RETURN: 0 on success, error code on failure.
2117 * dip - ctime|mtime updated
2121 zfs_rmdir(struct inode
*dip
, char *name
, struct inode
*cwd
, cred_t
*cr
,
2124 znode_t
*dzp
= ITOZ(dip
);
2127 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
2133 boolean_t waited
= B_FALSE
;
2136 return (SET_ERROR(EINVAL
));
2140 zilog
= zfsvfs
->z_log
;
2142 if (flags
& FIGNORECASE
)
2148 * Attempt to lock directory; fail if entry doesn't exist.
2150 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
2158 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
2162 if (!S_ISDIR(ip
->i_mode
)) {
2163 error
= SET_ERROR(ENOTDIR
);
2168 error
= SET_ERROR(EINVAL
);
2173 * Grab a lock on the directory to make sure that no one is
2174 * trying to add (or lookup) entries while we are removing it.
2176 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
2179 * Grab a lock on the parent pointer to make sure we play well
2180 * with the treewalk and directory rename code.
2182 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
2184 tx
= dmu_tx_create(zfsvfs
->z_os
);
2185 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
2186 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2187 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
2188 zfs_sa_upgrade_txholds(tx
, zp
);
2189 zfs_sa_upgrade_txholds(tx
, dzp
);
2190 dmu_tx_mark_netfree(tx
);
2191 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
2193 rw_exit(&zp
->z_parent_lock
);
2194 rw_exit(&zp
->z_name_lock
);
2195 zfs_dirent_unlock(dl
);
2196 if (error
== ERESTART
) {
2209 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
2212 uint64_t txtype
= TX_RMDIR
;
2213 if (flags
& FIGNORECASE
)
2215 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
);
2220 rw_exit(&zp
->z_parent_lock
);
2221 rw_exit(&zp
->z_name_lock
);
2223 zfs_dirent_unlock(dl
);
2225 zfs_inode_update(dzp
);
2226 zfs_inode_update(zp
);
2229 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2230 zil_commit(zilog
, 0);
2237 * Read as many directory entries as will fit into the provided
2238 * dirent buffer from the given directory cursor position.
2240 * IN: ip - inode of directory to read.
2241 * dirent - buffer for directory entries.
2243 * OUT: dirent - filler buffer of directory entries.
2245 * RETURN: 0 if success
2246 * error code if failure
2249 * ip - atime updated
2251 * Note that the low 4 bits of the cookie returned by zap is always zero.
2252 * This allows us to use the low range for "special" directory entries:
2253 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2254 * we use the offset 2 for the '.zfs' directory.
2258 zfs_readdir(struct inode
*ip
, zpl_dir_context_t
*ctx
, cred_t
*cr
)
2260 znode_t
*zp
= ITOZ(ip
);
2261 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2264 zap_attribute_t zap
;
2270 uint64_t offset
; /* must be unsigned; checks for < 1 */
2275 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
2276 &parent
, sizeof (parent
))) != 0)
2280 * Quit if directory has been removed (posix)
2288 prefetch
= zp
->z_zn_prefetch
;
2291 * Initialize the iterator cursor.
2295 * Start iteration from the beginning of the directory.
2297 zap_cursor_init(&zc
, os
, zp
->z_id
);
2300 * The offset is a serialized cursor.
2302 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
2306 * Transform to file-system independent format
2311 * Special case `.', `..', and `.zfs'.
2314 (void) strcpy(zap
.za_name
, ".");
2315 zap
.za_normalization_conflict
= 0;
2318 } else if (offset
== 1) {
2319 (void) strcpy(zap
.za_name
, "..");
2320 zap
.za_normalization_conflict
= 0;
2323 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
2324 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
2325 zap
.za_normalization_conflict
= 0;
2326 objnum
= ZFSCTL_INO_ROOT
;
2332 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
2333 if (error
== ENOENT
)
2340 * Allow multiple entries provided the first entry is
2341 * the object id. Non-zpl consumers may safely make
2342 * use of the additional space.
2344 * XXX: This should be a feature flag for compatibility
2346 if (zap
.za_integer_length
!= 8 ||
2347 zap
.za_num_integers
== 0) {
2348 cmn_err(CE_WARN
, "zap_readdir: bad directory "
2349 "entry, obj = %lld, offset = %lld, "
2350 "length = %d, num = %lld\n",
2351 (u_longlong_t
)zp
->z_id
,
2352 (u_longlong_t
)offset
,
2353 zap
.za_integer_length
,
2354 (u_longlong_t
)zap
.za_num_integers
);
2355 error
= SET_ERROR(ENXIO
);
2359 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
2360 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
2363 done
= !zpl_dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
2368 /* Prefetch znode */
2370 dmu_prefetch(os
, objnum
, 0, 0, 0,
2371 ZIO_PRIORITY_SYNC_READ
);
2375 * Move to the next entry, fill in the previous offset.
2377 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2378 zap_cursor_advance(&zc
);
2379 offset
= zap_cursor_serialize(&zc
);
2385 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2388 zap_cursor_fini(&zc
);
2389 if (error
== ENOENT
)
2397 ulong_t zfs_fsync_sync_cnt
= 4;
2400 zfs_fsync(struct inode
*ip
, int syncflag
, cred_t
*cr
)
2402 znode_t
*zp
= ITOZ(ip
);
2403 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2405 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2407 if (zfsvfs
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2410 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
2413 tsd_set(zfs_fsyncer_key
, NULL
);
2420 * Get the requested file attributes and place them in the provided
2423 * IN: ip - inode of file.
2424 * vap - va_mask identifies requested attributes.
2425 * If ATTR_XVATTR set, then optional attrs are requested
2426 * flags - ATTR_NOACLCHECK (CIFS server context)
2427 * cr - credentials of caller.
2429 * OUT: vap - attribute values.
2431 * RETURN: 0 (always succeeds)
2435 zfs_getattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2437 znode_t
*zp
= ITOZ(ip
);
2438 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2441 uint64_t atime
[2], mtime
[2], ctime
[2];
2442 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2443 xoptattr_t
*xoap
= NULL
;
2444 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2445 sa_bulk_attr_t bulk
[3];
2451 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2453 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
2454 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
2455 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
2457 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2463 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2464 * Also, if we are the owner don't bother, since owner should
2465 * always be allowed to read basic attributes of file.
2467 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2468 (vap
->va_uid
!= crgetuid(cr
))) {
2469 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2477 * Return all attributes. It's cheaper to provide the answer
2478 * than to determine whether we were asked the question.
2481 mutex_enter(&zp
->z_lock
);
2482 vap
->va_type
= vn_mode_to_vtype(zp
->z_mode
);
2483 vap
->va_mode
= zp
->z_mode
;
2484 vap
->va_fsid
= ZTOI(zp
)->i_sb
->s_dev
;
2485 vap
->va_nodeid
= zp
->z_id
;
2486 if ((zp
->z_id
== zfsvfs
->z_root
) && zfs_show_ctldir(zp
))
2487 links
= ZTOI(zp
)->i_nlink
+ 1;
2489 links
= ZTOI(zp
)->i_nlink
;
2490 vap
->va_nlink
= MIN(links
, ZFS_LINK_MAX
);
2491 vap
->va_size
= i_size_read(ip
);
2492 vap
->va_rdev
= ip
->i_rdev
;
2493 vap
->va_seq
= ip
->i_generation
;
2496 * Add in any requested optional attributes and the create time.
2497 * Also set the corresponding bits in the returned attribute bitmap.
2499 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zfsvfs
->z_use_fuids
) {
2500 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2502 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2503 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2506 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2507 xoap
->xoa_readonly
=
2508 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2509 XVA_SET_RTN(xvap
, XAT_READONLY
);
2512 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2514 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2515 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2518 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2520 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2521 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2524 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2525 xoap
->xoa_nounlink
=
2526 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2527 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2530 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2531 xoap
->xoa_immutable
=
2532 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2533 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2536 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2537 xoap
->xoa_appendonly
=
2538 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2539 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2542 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2544 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2545 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2548 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2550 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2551 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2554 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2555 xoap
->xoa_av_quarantined
=
2556 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2557 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2560 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2561 xoap
->xoa_av_modified
=
2562 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2563 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2566 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2567 S_ISREG(ip
->i_mode
)) {
2568 zfs_sa_get_scanstamp(zp
, xvap
);
2571 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2574 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zfsvfs
),
2575 times
, sizeof (times
));
2576 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2577 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2580 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2581 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2582 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2584 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2585 xoap
->xoa_generation
= ip
->i_generation
;
2586 XVA_SET_RTN(xvap
, XAT_GEN
);
2589 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2591 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2592 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2595 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2597 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2598 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2601 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2602 xoap
->xoa_projinherit
=
2603 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0);
2604 XVA_SET_RTN(xvap
, XAT_PROJINHERIT
);
2607 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2608 xoap
->xoa_projid
= zp
->z_projid
;
2609 XVA_SET_RTN(xvap
, XAT_PROJID
);
2613 ZFS_TIME_DECODE(&vap
->va_atime
, atime
);
2614 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2615 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2617 mutex_exit(&zp
->z_lock
);
2619 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2621 if (zp
->z_blksz
== 0) {
2623 * Block size hasn't been set; suggest maximal I/O transfers.
2625 vap
->va_blksize
= zfsvfs
->z_max_blksz
;
2633 * Get the basic file attributes and place them in the provided kstat
2634 * structure. The inode is assumed to be the authoritative source
2635 * for most of the attributes. However, the znode currently has the
2636 * authoritative atime, blksize, and block count.
2638 * IN: ip - inode of file.
2640 * OUT: sp - kstat values.
2642 * RETURN: 0 (always succeeds)
2646 zfs_getattr_fast(struct inode
*ip
, struct kstat
*sp
)
2648 znode_t
*zp
= ITOZ(ip
);
2649 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2651 u_longlong_t nblocks
;
2656 mutex_enter(&zp
->z_lock
);
2658 generic_fillattr(ip
, sp
);
2660 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
2661 sp
->blksize
= blksize
;
2662 sp
->blocks
= nblocks
;
2664 if (unlikely(zp
->z_blksz
== 0)) {
2666 * Block size hasn't been set; suggest maximal I/O transfers.
2668 sp
->blksize
= zfsvfs
->z_max_blksz
;
2671 mutex_exit(&zp
->z_lock
);
2674 * Required to prevent NFS client from detecting different inode
2675 * numbers of snapshot root dentry before and after snapshot mount.
2677 if (zfsvfs
->z_issnap
) {
2678 if (ip
->i_sb
->s_root
->d_inode
== ip
)
2679 sp
->ino
= ZFSCTL_INO_SNAPDIRS
-
2680 dmu_objset_id(zfsvfs
->z_os
);
2689 * For the operation of changing file's user/group/project, we need to
2690 * handle not only the main object that is assigned to the file directly,
2691 * but also the ones that are used by the file via hidden xattr directory.
2693 * Because the xattr directory may contains many EA entries, as to it may
2694 * be impossible to change all of them via the transaction of changing the
2695 * main object's user/group/project attributes. Then we have to change them
2696 * via other multiple independent transactions one by one. It may be not good
2697 * solution, but we have no better idea yet.
2700 zfs_setattr_dir(znode_t
*dzp
)
2702 struct inode
*dxip
= ZTOI(dzp
);
2703 struct inode
*xip
= NULL
;
2704 zfsvfs_t
*zfsvfs
= ITOZSB(dxip
);
2705 objset_t
*os
= zfsvfs
->z_os
;
2707 zap_attribute_t zap
;
2710 dmu_tx_t
*tx
= NULL
;
2712 sa_bulk_attr_t bulk
[4];
2716 zap_cursor_init(&zc
, os
, dzp
->z_id
);
2717 while ((err
= zap_cursor_retrieve(&zc
, &zap
)) == 0) {
2719 if (zap
.za_integer_length
!= 8 || zap
.za_num_integers
!= 1) {
2724 err
= zfs_dirent_lock(&dl
, dzp
, (char *)zap
.za_name
, &zp
,
2725 ZEXISTS
, NULL
, NULL
);
2732 if (KUID_TO_SUID(xip
->i_uid
) == KUID_TO_SUID(dxip
->i_uid
) &&
2733 KGID_TO_SGID(xip
->i_gid
) == KGID_TO_SGID(dxip
->i_gid
) &&
2734 zp
->z_projid
== dzp
->z_projid
)
2737 tx
= dmu_tx_create(os
);
2738 if (!(zp
->z_pflags
& ZFS_PROJID
))
2739 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2741 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2743 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2747 mutex_enter(&dzp
->z_lock
);
2749 if (KUID_TO_SUID(xip
->i_uid
) != KUID_TO_SUID(dxip
->i_uid
)) {
2750 xip
->i_uid
= dxip
->i_uid
;
2751 uid
= zfs_uid_read(dxip
);
2752 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
2753 &uid
, sizeof (uid
));
2756 if (KGID_TO_SGID(xip
->i_gid
) != KGID_TO_SGID(dxip
->i_gid
)) {
2757 xip
->i_gid
= dxip
->i_gid
;
2758 gid
= zfs_gid_read(dxip
);
2759 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
2760 &gid
, sizeof (gid
));
2763 if (zp
->z_projid
!= dzp
->z_projid
) {
2764 if (!(zp
->z_pflags
& ZFS_PROJID
)) {
2765 zp
->z_pflags
|= ZFS_PROJID
;
2766 SA_ADD_BULK_ATTR(bulk
, count
,
2767 SA_ZPL_FLAGS(zfsvfs
), NULL
, &zp
->z_pflags
,
2768 sizeof (zp
->z_pflags
));
2771 zp
->z_projid
= dzp
->z_projid
;
2772 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PROJID(zfsvfs
),
2773 NULL
, &zp
->z_projid
, sizeof (zp
->z_projid
));
2776 mutex_exit(&dzp
->z_lock
);
2778 if (likely(count
> 0)) {
2779 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
2785 if (err
!= 0 && err
!= ENOENT
)
2792 zfs_dirent_unlock(dl
);
2794 zap_cursor_advance(&zc
);
2801 zfs_dirent_unlock(dl
);
2803 zap_cursor_fini(&zc
);
2805 return (err
== ENOENT
? 0 : err
);
2809 * Set the file attributes to the values contained in the
2812 * IN: ip - inode of file to be modified.
2813 * vap - new attribute values.
2814 * If ATTR_XVATTR set, then optional attrs are being set
2815 * flags - ATTR_UTIME set if non-default time values provided.
2816 * - ATTR_NOACLCHECK (CIFS context only).
2817 * cr - credentials of caller.
2819 * RETURN: 0 if success
2820 * error code if failure
2823 * ip - ctime updated, mtime updated if size changed.
2827 zfs_setattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2829 znode_t
*zp
= ITOZ(ip
);
2830 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
2831 objset_t
*os
= zfsvfs
->z_os
;
2835 xvattr_t
*tmpxvattr
;
2836 uint_t mask
= vap
->va_mask
;
2837 uint_t saved_mask
= 0;
2840 uint64_t new_kuid
= 0, new_kgid
= 0, new_uid
, new_gid
;
2842 uint64_t mtime
[2], ctime
[2], atime
[2];
2843 uint64_t projid
= ZFS_INVALID_PROJID
;
2845 int need_policy
= FALSE
;
2847 zfs_fuid_info_t
*fuidp
= NULL
;
2848 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2851 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2852 boolean_t fuid_dirtied
= B_FALSE
;
2853 boolean_t handle_eadir
= B_FALSE
;
2854 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
2855 int count
= 0, xattr_count
= 0, bulks
= 8;
2864 * If this is a xvattr_t, then get a pointer to the structure of
2865 * optional attributes. If this is NULL, then we have a vattr_t.
2867 xoap
= xva_getxoptattr(xvap
);
2868 if (xoap
!= NULL
&& (mask
& ATTR_XVATTR
)) {
2869 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2870 if (!dmu_objset_projectquota_enabled(os
) ||
2871 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
))) {
2873 return (SET_ERROR(ENOTSUP
));
2876 projid
= xoap
->xoa_projid
;
2877 if (unlikely(projid
== ZFS_INVALID_PROJID
)) {
2879 return (SET_ERROR(EINVAL
));
2882 if (projid
== zp
->z_projid
&& zp
->z_pflags
& ZFS_PROJID
)
2883 projid
= ZFS_INVALID_PROJID
;
2888 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
) &&
2889 (xoap
->xoa_projinherit
!=
2890 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) &&
2891 (!dmu_objset_projectquota_enabled(os
) ||
2892 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
)))) {
2894 return (SET_ERROR(ENOTSUP
));
2898 zilog
= zfsvfs
->z_log
;
2901 * Make sure that if we have ephemeral uid/gid or xvattr specified
2902 * that file system is at proper version level
2905 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
2906 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2907 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2908 (mask
& ATTR_XVATTR
))) {
2910 return (SET_ERROR(EINVAL
));
2913 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
2915 return (SET_ERROR(EISDIR
));
2918 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
2920 return (SET_ERROR(EINVAL
));
2923 tmpxvattr
= kmem_alloc(sizeof (xvattr_t
), KM_SLEEP
);
2924 xva_init(tmpxvattr
);
2926 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
2927 xattr_bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
2930 * Immutable files can only alter immutable bit and atime
2932 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2933 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
2934 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2935 err
= SET_ERROR(EPERM
);
2939 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2940 err
= SET_ERROR(EPERM
);
2945 * Verify timestamps doesn't overflow 32 bits.
2946 * ZFS can handle large timestamps, but 32bit syscalls can't
2947 * handle times greater than 2039. This check should be removed
2948 * once large timestamps are fully supported.
2950 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
2951 if (((mask
& ATTR_ATIME
) &&
2952 TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2953 ((mask
& ATTR_MTIME
) &&
2954 TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2955 err
= SET_ERROR(EOVERFLOW
);
2964 /* Can this be moved to before the top label? */
2965 if (zfs_is_readonly(zfsvfs
)) {
2966 err
= SET_ERROR(EROFS
);
2971 * First validate permissions
2974 if (mask
& ATTR_SIZE
) {
2975 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2980 * XXX - Note, we are not providing any open
2981 * mode flags here (like FNDELAY), so we may
2982 * block if there are locks present... this
2983 * should be addressed in openat().
2985 /* XXX - would it be OK to generate a log record here? */
2986 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2991 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
2992 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2993 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2994 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2995 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2996 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2997 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2998 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2999 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
3003 if (mask
& (ATTR_UID
|ATTR_GID
)) {
3004 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
3009 * NOTE: even if a new mode is being set,
3010 * we may clear S_ISUID/S_ISGID bits.
3013 if (!(mask
& ATTR_MODE
))
3014 vap
->va_mode
= zp
->z_mode
;
3017 * Take ownership or chgrp to group we are a member of
3020 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
3021 take_group
= (mask
& ATTR_GID
) &&
3022 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
3025 * If both ATTR_UID and ATTR_GID are set then take_owner and
3026 * take_group must both be set in order to allow taking
3029 * Otherwise, send the check through secpolicy_vnode_setattr()
3033 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
3034 take_owner
&& take_group
) ||
3035 ((idmask
== ATTR_UID
) && take_owner
) ||
3036 ((idmask
== ATTR_GID
) && take_group
)) {
3037 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
3038 skipaclchk
, cr
) == 0) {
3040 * Remove setuid/setgid for non-privileged users
3042 (void) secpolicy_setid_clear(vap
, cr
);
3043 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
3052 mutex_enter(&zp
->z_lock
);
3053 oldva
.va_mode
= zp
->z_mode
;
3054 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
3055 if (mask
& ATTR_XVATTR
) {
3057 * Update xvattr mask to include only those attributes
3058 * that are actually changing.
3060 * the bits will be restored prior to actually setting
3061 * the attributes so the caller thinks they were set.
3063 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
3064 if (xoap
->xoa_appendonly
!=
3065 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
3068 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
3069 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
3073 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
3074 if (xoap
->xoa_projinherit
!=
3075 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) {
3078 XVA_CLR_REQ(xvap
, XAT_PROJINHERIT
);
3079 XVA_SET_REQ(tmpxvattr
, XAT_PROJINHERIT
);
3083 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
3084 if (xoap
->xoa_nounlink
!=
3085 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
3088 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
3089 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
3093 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
3094 if (xoap
->xoa_immutable
!=
3095 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
3098 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
3099 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
3103 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
3104 if (xoap
->xoa_nodump
!=
3105 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
3108 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
3109 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
3113 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
3114 if (xoap
->xoa_av_modified
!=
3115 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
3118 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
3119 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
3123 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
3124 if ((!S_ISREG(ip
->i_mode
) &&
3125 xoap
->xoa_av_quarantined
) ||
3126 xoap
->xoa_av_quarantined
!=
3127 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
3130 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
3131 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
3135 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
3136 mutex_exit(&zp
->z_lock
);
3137 err
= SET_ERROR(EPERM
);
3141 if (need_policy
== FALSE
&&
3142 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
3143 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
3148 mutex_exit(&zp
->z_lock
);
3150 if (mask
& ATTR_MODE
) {
3151 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
3152 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
3157 trim_mask
|= ATTR_MODE
;
3165 * If trim_mask is set then take ownership
3166 * has been granted or write_acl is present and user
3167 * has the ability to modify mode. In that case remove
3168 * UID|GID and or MODE from mask so that
3169 * secpolicy_vnode_setattr() doesn't revoke it.
3173 saved_mask
= vap
->va_mask
;
3174 vap
->va_mask
&= ~trim_mask
;
3176 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
3177 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
3182 vap
->va_mask
|= saved_mask
;
3186 * secpolicy_vnode_setattr, or take ownership may have
3189 mask
= vap
->va_mask
;
3191 if ((mask
& (ATTR_UID
| ATTR_GID
)) || projid
!= ZFS_INVALID_PROJID
) {
3192 handle_eadir
= B_TRUE
;
3193 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
3194 &xattr_obj
, sizeof (xattr_obj
));
3196 if (err
== 0 && xattr_obj
) {
3197 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
3201 if (mask
& ATTR_UID
) {
3202 new_kuid
= zfs_fuid_create(zfsvfs
,
3203 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
3204 if (new_kuid
!= KUID_TO_SUID(ZTOI(zp
)->i_uid
) &&
3205 zfs_id_overquota(zfsvfs
, DMU_USERUSED_OBJECT
,
3209 err
= SET_ERROR(EDQUOT
);
3214 if (mask
& ATTR_GID
) {
3215 new_kgid
= zfs_fuid_create(zfsvfs
,
3216 (uint64_t)vap
->va_gid
, cr
, ZFS_GROUP
, &fuidp
);
3217 if (new_kgid
!= KGID_TO_SGID(ZTOI(zp
)->i_gid
) &&
3218 zfs_id_overquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
3222 err
= SET_ERROR(EDQUOT
);
3227 if (projid
!= ZFS_INVALID_PROJID
&&
3228 zfs_id_overquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
, projid
)) {
3235 tx
= dmu_tx_create(os
);
3237 if (mask
& ATTR_MODE
) {
3238 uint64_t pmode
= zp
->z_mode
;
3240 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
3242 zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
);
3244 mutex_enter(&zp
->z_lock
);
3245 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
3247 * Are we upgrading ACL from old V0 format
3250 if (zfsvfs
->z_version
>= ZPL_VERSION_FUID
&&
3251 zfs_znode_acl_version(zp
) ==
3252 ZFS_ACL_VERSION_INITIAL
) {
3253 dmu_tx_hold_free(tx
, acl_obj
, 0,
3255 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
3256 0, aclp
->z_acl_bytes
);
3258 dmu_tx_hold_write(tx
, acl_obj
, 0,
3261 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3262 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
3263 0, aclp
->z_acl_bytes
);
3265 mutex_exit(&zp
->z_lock
);
3266 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
3268 if (((mask
& ATTR_XVATTR
) &&
3269 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) ||
3270 (projid
!= ZFS_INVALID_PROJID
&&
3271 !(zp
->z_pflags
& ZFS_PROJID
)))
3272 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
3274 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3278 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
3281 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3283 zfs_fuid_txhold(zfsvfs
, tx
);
3285 zfs_sa_upgrade_txholds(tx
, zp
);
3287 err
= dmu_tx_assign(tx
, TXG_WAIT
);
3293 * Set each attribute requested.
3294 * We group settings according to the locks they need to acquire.
3296 * Note: you cannot set ctime directly, although it will be
3297 * updated as a side-effect of calling this function.
3300 if (projid
!= ZFS_INVALID_PROJID
&& !(zp
->z_pflags
& ZFS_PROJID
)) {
3302 * For the existed object that is upgraded from old system,
3303 * its on-disk layout has no slot for the project ID attribute.
3304 * But quota accounting logic needs to access related slots by
3305 * offset directly. So we need to adjust old objects' layout
3306 * to make the project ID to some unified and fixed offset.
3309 err
= sa_add_projid(attrzp
->z_sa_hdl
, tx
, projid
);
3311 err
= sa_add_projid(zp
->z_sa_hdl
, tx
, projid
);
3313 if (unlikely(err
== EEXIST
))
3318 projid
= ZFS_INVALID_PROJID
;
3321 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3322 mutex_enter(&zp
->z_acl_lock
);
3323 mutex_enter(&zp
->z_lock
);
3325 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
3326 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
3329 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3330 mutex_enter(&attrzp
->z_acl_lock
);
3331 mutex_enter(&attrzp
->z_lock
);
3332 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3333 SA_ZPL_FLAGS(zfsvfs
), NULL
, &attrzp
->z_pflags
,
3334 sizeof (attrzp
->z_pflags
));
3335 if (projid
!= ZFS_INVALID_PROJID
) {
3336 attrzp
->z_projid
= projid
;
3337 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3338 SA_ZPL_PROJID(zfsvfs
), NULL
, &attrzp
->z_projid
,
3339 sizeof (attrzp
->z_projid
));
3343 if (mask
& (ATTR_UID
|ATTR_GID
)) {
3345 if (mask
& ATTR_UID
) {
3346 ZTOI(zp
)->i_uid
= SUID_TO_KUID(new_kuid
);
3347 new_uid
= zfs_uid_read(ZTOI(zp
));
3348 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
3349 &new_uid
, sizeof (new_uid
));
3351 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3352 SA_ZPL_UID(zfsvfs
), NULL
, &new_uid
,
3354 ZTOI(attrzp
)->i_uid
= SUID_TO_KUID(new_uid
);
3358 if (mask
& ATTR_GID
) {
3359 ZTOI(zp
)->i_gid
= SGID_TO_KGID(new_kgid
);
3360 new_gid
= zfs_gid_read(ZTOI(zp
));
3361 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
),
3362 NULL
, &new_gid
, sizeof (new_gid
));
3364 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3365 SA_ZPL_GID(zfsvfs
), NULL
, &new_gid
,
3367 ZTOI(attrzp
)->i_gid
= SGID_TO_KGID(new_kgid
);
3370 if (!(mask
& ATTR_MODE
)) {
3371 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
),
3372 NULL
, &new_mode
, sizeof (new_mode
));
3373 new_mode
= zp
->z_mode
;
3375 err
= zfs_acl_chown_setattr(zp
);
3378 err
= zfs_acl_chown_setattr(attrzp
);
3383 if (mask
& ATTR_MODE
) {
3384 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
3385 &new_mode
, sizeof (new_mode
));
3386 zp
->z_mode
= ZTOI(zp
)->i_mode
= new_mode
;
3387 ASSERT3P(aclp
, !=, NULL
);
3388 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
3390 if (zp
->z_acl_cached
)
3391 zfs_acl_free(zp
->z_acl_cached
);
3392 zp
->z_acl_cached
= aclp
;
3396 if ((mask
& ATTR_ATIME
) || zp
->z_atime_dirty
) {
3397 zp
->z_atime_dirty
= 0;
3398 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
3399 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
3400 &atime
, sizeof (atime
));
3403 if (mask
& (ATTR_MTIME
| ATTR_SIZE
)) {
3404 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
3405 ZTOI(zp
)->i_mtime
= zpl_inode_timespec_trunc(vap
->va_mtime
,
3406 ZTOI(zp
)->i_sb
->s_time_gran
);
3408 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
3409 mtime
, sizeof (mtime
));
3412 if (mask
& (ATTR_CTIME
| ATTR_SIZE
)) {
3413 ZFS_TIME_ENCODE(&vap
->va_ctime
, ctime
);
3414 ZTOI(zp
)->i_ctime
= zpl_inode_timespec_trunc(vap
->va_ctime
,
3415 ZTOI(zp
)->i_sb
->s_time_gran
);
3416 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
3417 ctime
, sizeof (ctime
));
3420 if (projid
!= ZFS_INVALID_PROJID
) {
3421 zp
->z_projid
= projid
;
3422 SA_ADD_BULK_ATTR(bulk
, count
,
3423 SA_ZPL_PROJID(zfsvfs
), NULL
, &zp
->z_projid
,
3424 sizeof (zp
->z_projid
));
3427 if (attrzp
&& mask
) {
3428 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
3429 SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
,
3434 * Do this after setting timestamps to prevent timestamp
3435 * update from toggling bit
3438 if (xoap
&& (mask
& ATTR_XVATTR
)) {
3441 * restore trimmed off masks
3442 * so that return masks can be set for caller.
3445 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
3446 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
3448 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
3449 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
3451 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
3452 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
3454 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
3455 XVA_SET_REQ(xvap
, XAT_NODUMP
);
3457 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
3458 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
3460 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
3461 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
3463 if (XVA_ISSET_REQ(tmpxvattr
, XAT_PROJINHERIT
)) {
3464 XVA_SET_REQ(xvap
, XAT_PROJINHERIT
);
3467 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
3468 ASSERT(S_ISREG(ip
->i_mode
));
3470 zfs_xvattr_set(zp
, xvap
, tx
);
3474 zfs_fuid_sync(zfsvfs
, tx
);
3477 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
3479 mutex_exit(&zp
->z_lock
);
3480 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3481 mutex_exit(&zp
->z_acl_lock
);
3484 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3485 mutex_exit(&attrzp
->z_acl_lock
);
3486 mutex_exit(&attrzp
->z_lock
);
3489 if (err
== 0 && xattr_count
> 0) {
3490 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3499 zfs_fuid_info_free(fuidp
);
3507 if (err
== ERESTART
)
3511 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3514 if (err2
== 0 && handle_eadir
)
3515 err2
= zfs_setattr_dir(attrzp
);
3518 zfs_inode_update(zp
);
3522 if (os
->os_sync
== ZFS_SYNC_ALWAYS
)
3523 zil_commit(zilog
, 0);
3526 kmem_free(xattr_bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
3527 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
3528 kmem_free(tmpxvattr
, sizeof (xvattr_t
));
3533 typedef struct zfs_zlock
{
3534 krwlock_t
*zl_rwlock
; /* lock we acquired */
3535 znode_t
*zl_znode
; /* znode we held */
3536 struct zfs_zlock
*zl_next
; /* next in list */
3540 * Drop locks and release vnodes that were held by zfs_rename_lock().
3543 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3547 while ((zl
= *zlpp
) != NULL
) {
3548 if (zl
->zl_znode
!= NULL
)
3549 zfs_iput_async(ZTOI(zl
->zl_znode
));
3550 rw_exit(zl
->zl_rwlock
);
3551 *zlpp
= zl
->zl_next
;
3552 kmem_free(zl
, sizeof (*zl
));
3557 * Search back through the directory tree, using the ".." entries.
3558 * Lock each directory in the chain to prevent concurrent renames.
3559 * Fail any attempt to move a directory into one of its own descendants.
3560 * XXX - z_parent_lock can overlap with map or grow locks
3563 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3567 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
3568 uint64_t oidp
= zp
->z_id
;
3569 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3570 krw_t rw
= RW_WRITER
;
3573 * First pass write-locks szp and compares to zp->z_id.
3574 * Later passes read-lock zp and compare to zp->z_parent.
3577 if (!rw_tryenter(rwlp
, rw
)) {
3579 * Another thread is renaming in this path.
3580 * Note that if we are a WRITER, we don't have any
3581 * parent_locks held yet.
3583 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3585 * Drop our locks and restart
3587 zfs_rename_unlock(&zl
);
3591 rwlp
= &szp
->z_parent_lock
;
3596 * Wait for other thread to drop its locks
3602 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3603 zl
->zl_rwlock
= rwlp
;
3604 zl
->zl_znode
= NULL
;
3605 zl
->zl_next
= *zlpp
;
3608 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3609 return (SET_ERROR(EINVAL
));
3611 if (oidp
== rootid
) /* We've hit the top */
3614 if (rw
== RW_READER
) { /* i.e. not the first pass */
3615 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
3620 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
3621 &oidp
, sizeof (oidp
));
3622 rwlp
= &zp
->z_parent_lock
;
3625 } while (zp
->z_id
!= sdzp
->z_id
);
3631 * Move an entry from the provided source directory to the target
3632 * directory. Change the entry name as indicated.
3634 * IN: sdip - Source directory containing the "old entry".
3635 * snm - Old entry name.
3636 * tdip - Target directory to contain the "new entry".
3637 * tnm - New entry name.
3638 * cr - credentials of caller.
3639 * flags - case flags
3641 * RETURN: 0 on success, error code on failure.
3644 * sdip,tdip - ctime|mtime updated
3648 zfs_rename(struct inode
*sdip
, char *snm
, struct inode
*tdip
, char *tnm
,
3649 cred_t
*cr
, int flags
)
3651 znode_t
*tdzp
, *szp
, *tzp
;
3652 znode_t
*sdzp
= ITOZ(sdip
);
3653 zfsvfs_t
*zfsvfs
= ITOZSB(sdip
);
3655 zfs_dirlock_t
*sdl
, *tdl
;
3658 int cmp
, serr
, terr
;
3661 boolean_t waited
= B_FALSE
;
3663 if (snm
== NULL
|| tnm
== NULL
)
3664 return (SET_ERROR(EINVAL
));
3667 ZFS_VERIFY_ZP(sdzp
);
3668 zilog
= zfsvfs
->z_log
;
3671 ZFS_VERIFY_ZP(tdzp
);
3674 * We check i_sb because snapshots and the ctldir must have different
3677 if (tdip
->i_sb
!= sdip
->i_sb
|| zfsctl_is_node(tdip
)) {
3679 return (SET_ERROR(EXDEV
));
3682 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
3683 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3685 return (SET_ERROR(EILSEQ
));
3688 if (flags
& FIGNORECASE
)
3697 * This is to prevent the creation of links into attribute space
3698 * by renaming a linked file into/outof an attribute directory.
3699 * See the comment in zfs_link() for why this is considered bad.
3701 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3703 return (SET_ERROR(EINVAL
));
3707 * Lock source and target directory entries. To prevent deadlock,
3708 * a lock ordering must be defined. We lock the directory with
3709 * the smallest object id first, or if it's a tie, the one with
3710 * the lexically first name.
3712 if (sdzp
->z_id
< tdzp
->z_id
) {
3714 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3718 * First compare the two name arguments without
3719 * considering any case folding.
3721 int nofold
= (zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3723 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3724 ASSERT(error
== 0 || !zfsvfs
->z_utf8
);
3727 * POSIX: "If the old argument and the new argument
3728 * both refer to links to the same existing file,
3729 * the rename() function shall return successfully
3730 * and perform no other action."
3736 * If the file system is case-folding, then we may
3737 * have some more checking to do. A case-folding file
3738 * system is either supporting mixed case sensitivity
3739 * access or is completely case-insensitive. Note
3740 * that the file system is always case preserving.
3742 * In mixed sensitivity mode case sensitive behavior
3743 * is the default. FIGNORECASE must be used to
3744 * explicitly request case insensitive behavior.
3746 * If the source and target names provided differ only
3747 * by case (e.g., a request to rename 'tim' to 'Tim'),
3748 * we will treat this as a special case in the
3749 * case-insensitive mode: as long as the source name
3750 * is an exact match, we will allow this to proceed as
3751 * a name-change request.
3753 if ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
3754 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
3755 flags
& FIGNORECASE
)) &&
3756 u8_strcmp(snm
, tnm
, 0, zfsvfs
->z_norm
, U8_UNICODE_LATEST
,
3759 * case preserving rename request, require exact
3768 * If the source and destination directories are the same, we should
3769 * grab the z_name_lock of that directory only once.
3773 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3777 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3778 ZEXISTS
| zflg
, NULL
, NULL
);
3779 terr
= zfs_dirent_lock(&tdl
,
3780 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3782 terr
= zfs_dirent_lock(&tdl
,
3783 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3784 serr
= zfs_dirent_lock(&sdl
,
3785 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3791 * Source entry invalid or not there.
3794 zfs_dirent_unlock(tdl
);
3800 rw_exit(&sdzp
->z_name_lock
);
3802 if (strcmp(snm
, "..") == 0)
3808 zfs_dirent_unlock(sdl
);
3812 rw_exit(&sdzp
->z_name_lock
);
3814 if (strcmp(tnm
, "..") == 0)
3821 * If we are using project inheritance, means if the directory has
3822 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3823 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3824 * such case, we only allow renames into our tree when the project
3827 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3828 tdzp
->z_projid
!= szp
->z_projid
) {
3829 error
= SET_ERROR(EXDEV
);
3834 * Must have write access at the source to remove the old entry
3835 * and write access at the target to create the new entry.
3836 * Note that if target and source are the same, this can be
3837 * done in a single check.
3840 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
3843 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3845 * Check to make sure rename is valid.
3846 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3848 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
3853 * Does target exist?
3857 * Source and target must be the same type.
3859 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3860 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3861 error
= SET_ERROR(ENOTDIR
);
3865 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3866 error
= SET_ERROR(EISDIR
);
3871 * POSIX dictates that when the source and target
3872 * entries refer to the same file object, rename
3873 * must do nothing and exit without error.
3875 if (szp
->z_id
== tzp
->z_id
) {
3881 tx
= dmu_tx_create(zfsvfs
->z_os
);
3882 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3883 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3884 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3885 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3887 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3888 zfs_sa_upgrade_txholds(tx
, tdzp
);
3891 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3892 zfs_sa_upgrade_txholds(tx
, tzp
);
3895 zfs_sa_upgrade_txholds(tx
, szp
);
3896 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3897 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
3900 zfs_rename_unlock(&zl
);
3901 zfs_dirent_unlock(sdl
);
3902 zfs_dirent_unlock(tdl
);
3905 rw_exit(&sdzp
->z_name_lock
);
3907 if (error
== ERESTART
) {
3924 if (tzp
) /* Attempt to remove the existing target */
3925 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3928 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3930 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3931 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
)
3932 szp
->z_pflags
|= ZFS_PROJINHERIT
;
3934 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
3935 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3938 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3940 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3941 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
3942 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3945 * At this point, we have successfully created
3946 * the target name, but have failed to remove
3947 * the source name. Since the create was done
3948 * with the ZRENAMING flag, there are
3949 * complications; for one, the link count is
3950 * wrong. The easiest way to deal with this
3951 * is to remove the newly created target, and
3952 * return the original error. This must
3953 * succeed; fortunately, it is very unlikely to
3954 * fail, since we just created it.
3956 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3957 ZRENAMING
, NULL
), ==, 0);
3961 * If we had removed the existing target, subsequent
3962 * call to zfs_link_create() to add back the same entry
3963 * but, the new dnode (szp) should not fail.
3965 ASSERT(tzp
== NULL
);
3972 zfs_rename_unlock(&zl
);
3974 zfs_dirent_unlock(sdl
);
3975 zfs_dirent_unlock(tdl
);
3977 zfs_inode_update(sdzp
);
3979 rw_exit(&sdzp
->z_name_lock
);
3982 zfs_inode_update(tdzp
);
3984 zfs_inode_update(szp
);
3987 zfs_inode_update(tzp
);
3991 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3992 zil_commit(zilog
, 0);
3999 * Insert the indicated symbolic reference entry into the directory.
4001 * IN: dip - Directory to contain new symbolic link.
4002 * link - Name for new symlink entry.
4003 * vap - Attributes of new entry.
4004 * target - Target path of new symlink.
4006 * cr - credentials of caller.
4007 * flags - case flags
4009 * RETURN: 0 on success, error code on failure.
4012 * dip - ctime|mtime updated
4016 zfs_symlink(struct inode
*dip
, char *name
, vattr_t
*vap
, char *link
,
4017 struct inode
**ipp
, cred_t
*cr
, int flags
)
4019 znode_t
*zp
, *dzp
= ITOZ(dip
);
4022 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
4024 uint64_t len
= strlen(link
);
4027 zfs_acl_ids_t acl_ids
;
4028 boolean_t fuid_dirtied
;
4029 uint64_t txtype
= TX_SYMLINK
;
4030 boolean_t waited
= B_FALSE
;
4032 ASSERT(S_ISLNK(vap
->va_mode
));
4035 return (SET_ERROR(EINVAL
));
4039 zilog
= zfsvfs
->z_log
;
4041 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
4042 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
4044 return (SET_ERROR(EILSEQ
));
4046 if (flags
& FIGNORECASE
)
4049 if (len
> MAXPATHLEN
) {
4051 return (SET_ERROR(ENAMETOOLONG
));
4054 if ((error
= zfs_acl_ids_create(dzp
, 0,
4055 vap
, cr
, NULL
, &acl_ids
)) != 0) {
4063 * Attempt to lock directory; fail if entry already exists.
4065 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
4067 zfs_acl_ids_free(&acl_ids
);
4072 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
4073 zfs_acl_ids_free(&acl_ids
);
4074 zfs_dirent_unlock(dl
);
4079 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, ZFS_DEFAULT_PROJID
)) {
4080 zfs_acl_ids_free(&acl_ids
);
4081 zfs_dirent_unlock(dl
);
4083 return (SET_ERROR(EDQUOT
));
4085 tx
= dmu_tx_create(zfsvfs
->z_os
);
4086 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
4087 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
4088 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
4089 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
4090 ZFS_SA_BASE_ATTR_SIZE
+ len
);
4091 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
4092 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
4093 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
4094 acl_ids
.z_aclp
->z_acl_bytes
);
4097 zfs_fuid_txhold(zfsvfs
, tx
);
4098 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
4100 zfs_dirent_unlock(dl
);
4101 if (error
== ERESTART
) {
4107 zfs_acl_ids_free(&acl_ids
);
4114 * Create a new object for the symlink.
4115 * for version 4 ZPL datsets the symlink will be an SA attribute
4117 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
4120 zfs_fuid_sync(zfsvfs
, tx
);
4122 mutex_enter(&zp
->z_lock
);
4124 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zfsvfs
),
4127 zfs_sa_symlink(zp
, link
, len
, tx
);
4128 mutex_exit(&zp
->z_lock
);
4131 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
4132 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
4134 * Insert the new object into the directory.
4136 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
4138 zfs_znode_delete(zp
, tx
);
4139 remove_inode_hash(ZTOI(zp
));
4141 if (flags
& FIGNORECASE
)
4143 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
4145 zfs_inode_update(dzp
);
4146 zfs_inode_update(zp
);
4149 zfs_acl_ids_free(&acl_ids
);
4153 zfs_dirent_unlock(dl
);
4158 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4159 zil_commit(zilog
, 0);
4169 * Return, in the buffer contained in the provided uio structure,
4170 * the symbolic path referred to by ip.
4172 * IN: ip - inode of symbolic link
4173 * uio - structure to contain the link path.
4174 * cr - credentials of caller.
4176 * RETURN: 0 if success
4177 * error code if failure
4180 * ip - atime updated
4184 zfs_readlink(struct inode
*ip
, uio_t
*uio
, cred_t
*cr
)
4186 znode_t
*zp
= ITOZ(ip
);
4187 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4193 mutex_enter(&zp
->z_lock
);
4195 error
= sa_lookup_uio(zp
->z_sa_hdl
,
4196 SA_ZPL_SYMLINK(zfsvfs
), uio
);
4198 error
= zfs_sa_readlink(zp
, uio
);
4199 mutex_exit(&zp
->z_lock
);
4206 * Insert a new entry into directory tdip referencing sip.
4208 * IN: tdip - Directory to contain new entry.
4209 * sip - inode of new entry.
4210 * name - name of new entry.
4211 * cr - credentials of caller.
4213 * RETURN: 0 if success
4214 * error code if failure
4217 * tdip - ctime|mtime updated
4218 * sip - ctime updated
4222 zfs_link(struct inode
*tdip
, struct inode
*sip
, char *name
, cred_t
*cr
,
4225 znode_t
*dzp
= ITOZ(tdip
);
4227 zfsvfs_t
*zfsvfs
= ITOZSB(tdip
);
4235 boolean_t waited
= B_FALSE
;
4236 boolean_t is_tmpfile
= 0;
4239 is_tmpfile
= (sip
->i_nlink
== 0 && (sip
->i_state
& I_LINKABLE
));
4241 ASSERT(S_ISDIR(tdip
->i_mode
));
4244 return (SET_ERROR(EINVAL
));
4248 zilog
= zfsvfs
->z_log
;
4251 * POSIX dictates that we return EPERM here.
4252 * Better choices include ENOTSUP or EISDIR.
4254 if (S_ISDIR(sip
->i_mode
)) {
4256 return (SET_ERROR(EPERM
));
4263 * If we are using project inheritance, means if the directory has
4264 * ZFS_PROJINHERIT set, then its descendant directories will inherit
4265 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
4266 * such case, we only allow hard link creation in our tree when the
4267 * project IDs are the same.
4269 if (dzp
->z_pflags
& ZFS_PROJINHERIT
&& dzp
->z_projid
!= szp
->z_projid
) {
4271 return (SET_ERROR(EXDEV
));
4275 * We check i_sb because snapshots and the ctldir must have different
4278 if (sip
->i_sb
!= tdip
->i_sb
|| zfsctl_is_node(sip
)) {
4280 return (SET_ERROR(EXDEV
));
4283 /* Prevent links to .zfs/shares files */
4285 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
4286 &parent
, sizeof (uint64_t))) != 0) {
4290 if (parent
== zfsvfs
->z_shares_dir
) {
4292 return (SET_ERROR(EPERM
));
4295 if (zfsvfs
->z_utf8
&& u8_validate(name
,
4296 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
4298 return (SET_ERROR(EILSEQ
));
4300 if (flags
& FIGNORECASE
)
4304 * We do not support links between attributes and non-attributes
4305 * because of the potential security risk of creating links
4306 * into "normal" file space in order to circumvent restrictions
4307 * imposed in attribute space.
4309 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
4311 return (SET_ERROR(EINVAL
));
4314 owner
= zfs_fuid_map_id(zfsvfs
, KUID_TO_SUID(sip
->i_uid
),
4316 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
4318 return (SET_ERROR(EPERM
));
4321 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
4328 * Attempt to lock directory; fail if entry already exists.
4330 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
4336 tx
= dmu_tx_create(zfsvfs
->z_os
);
4337 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
4338 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
4340 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
4342 zfs_sa_upgrade_txholds(tx
, szp
);
4343 zfs_sa_upgrade_txholds(tx
, dzp
);
4344 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
4346 zfs_dirent_unlock(dl
);
4347 if (error
== ERESTART
) {
4357 /* unmark z_unlinked so zfs_link_create will not reject */
4359 szp
->z_unlinked
= 0;
4360 error
= zfs_link_create(dl
, szp
, tx
, 0);
4363 uint64_t txtype
= TX_LINK
;
4365 * tmpfile is created to be in z_unlinkedobj, so remove it.
4366 * Also, we don't log in ZIL, be cause all previous file
4367 * operation on the tmpfile are ignored by ZIL. Instead we
4368 * always wait for txg to sync to make sure all previous
4369 * operation are sync safe.
4372 VERIFY(zap_remove_int(zfsvfs
->z_os
,
4373 zfsvfs
->z_unlinkedobj
, szp
->z_id
, tx
) == 0);
4375 if (flags
& FIGNORECASE
)
4377 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
4379 } else if (is_tmpfile
) {
4380 /* restore z_unlinked since when linking failed */
4381 szp
->z_unlinked
= 1;
4383 txg
= dmu_tx_get_txg(tx
);
4386 zfs_dirent_unlock(dl
);
4388 if (!is_tmpfile
&& zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4389 zil_commit(zilog
, 0);
4392 txg_wait_synced(dmu_objset_pool(zfsvfs
->z_os
), txg
);
4394 zfs_inode_update(dzp
);
4395 zfs_inode_update(szp
);
4401 zfs_putpage_commit_cb(void *arg
)
4403 struct page
*pp
= arg
;
4406 end_page_writeback(pp
);
4410 * Push a page out to disk, once the page is on stable storage the
4411 * registered commit callback will be run as notification of completion.
4413 * IN: ip - page mapped for inode.
4414 * pp - page to push (page is locked)
4415 * wbc - writeback control data
4417 * RETURN: 0 if success
4418 * error code if failure
4421 * ip - ctime|mtime updated
4425 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
4427 znode_t
*zp
= ITOZ(ip
);
4428 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4435 uint64_t mtime
[2], ctime
[2];
4436 sa_bulk_attr_t bulk
[3];
4438 struct address_space
*mapping
;
4443 ASSERT(PageLocked(pp
));
4445 pgoff
= page_offset(pp
); /* Page byte-offset in file */
4446 offset
= i_size_read(ip
); /* File length in bytes */
4447 pglen
= MIN(PAGE_SIZE
, /* Page length in bytes */
4448 P2ROUNDUP(offset
, PAGE_SIZE
)-pgoff
);
4450 /* Page is beyond end of file */
4451 if (pgoff
>= offset
) {
4457 /* Truncate page length to end of file */
4458 if (pgoff
+ pglen
> offset
)
4459 pglen
= offset
- pgoff
;
4463 * FIXME: Allow mmap writes past its quota. The correct fix
4464 * is to register a page_mkwrite() handler to count the page
4465 * against its quota when it is about to be dirtied.
4467 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
,
4468 KUID_TO_SUID(ip
->i_uid
)) ||
4469 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
4470 KGID_TO_SGID(ip
->i_gid
)) ||
4471 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
4472 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
4479 * The ordering here is critical and must adhere to the following
4480 * rules in order to avoid deadlocking in either zfs_read() or
4481 * zfs_free_range() due to a lock inversion.
4483 * 1) The page must be unlocked prior to acquiring the range lock.
4484 * This is critical because zfs_read() calls find_lock_page()
4485 * which may block on the page lock while holding the range lock.
4487 * 2) Before setting or clearing write back on a page the range lock
4488 * must be held in order to prevent a lock inversion with the
4489 * zfs_free_range() function.
4491 * This presents a problem because upon entering this function the
4492 * page lock is already held. To safely acquire the range lock the
4493 * page lock must be dropped. This creates a window where another
4494 * process could truncate, invalidate, dirty, or write out the page.
4496 * Therefore, after successfully reacquiring the range and page locks
4497 * the current page state is checked. In the common case everything
4498 * will be as is expected and it can be written out. However, if
4499 * the page state has changed it must be handled accordingly.
4501 mapping
= pp
->mapping
;
4502 redirty_page_for_writepage(wbc
, pp
);
4505 locked_range_t
*lr
= rangelock_enter(&zp
->z_rangelock
,
4506 pgoff
, pglen
, RL_WRITER
);
4509 /* Page mapping changed or it was no longer dirty, we're done */
4510 if (unlikely((mapping
!= pp
->mapping
) || !PageDirty(pp
))) {
4517 /* Another process started write block if required */
4518 if (PageWriteback(pp
)) {
4522 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
4523 wait_on_page_writeback(pp
);
4529 /* Clear the dirty flag the required locks are held */
4530 if (!clear_page_dirty_for_io(pp
)) {
4538 * Counterpart for redirty_page_for_writepage() above. This page
4539 * was in fact not skipped and should not be counted as if it were.
4541 wbc
->pages_skipped
--;
4542 set_page_writeback(pp
);
4545 tx
= dmu_tx_create(zfsvfs
->z_os
);
4546 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
4547 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4548 zfs_sa_upgrade_txholds(tx
, zp
);
4550 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
4552 if (err
== ERESTART
)
4556 __set_page_dirty_nobuffers(pp
);
4558 end_page_writeback(pp
);
4565 ASSERT3U(pglen
, <=, PAGE_SIZE
);
4566 dmu_write(zfsvfs
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
4569 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
4570 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
4571 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
4574 /* Preserve the mtime and ctime provided by the inode */
4575 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4576 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4577 zp
->z_atime_dirty
= 0;
4580 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4582 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
4583 zfs_putpage_commit_cb
, pp
);
4588 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
4590 * Note that this is rarely called under writepages(), because
4591 * writepages() normally handles the entire commit for
4592 * performance reasons.
4594 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
4602 * Update the system attributes when the inode has been dirtied. For the
4603 * moment we only update the mode, atime, mtime, and ctime.
4606 zfs_dirty_inode(struct inode
*ip
, int flags
)
4608 znode_t
*zp
= ITOZ(ip
);
4609 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4611 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
4612 sa_bulk_attr_t bulk
[4];
4616 if (zfs_is_readonly(zfsvfs
) || dmu_objset_is_snapshot(zfsvfs
->z_os
))
4624 * This is the lazytime semantic indroduced in Linux 4.0
4625 * This flag will only be called from update_time when lazytime is set.
4626 * (Note, I_DIRTY_SYNC will also set if not lazytime)
4627 * Fortunately mtime and ctime are managed within ZFS itself, so we
4628 * only need to dirty atime.
4630 if (flags
== I_DIRTY_TIME
) {
4631 zp
->z_atime_dirty
= 1;
4636 tx
= dmu_tx_create(zfsvfs
->z_os
);
4638 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4639 zfs_sa_upgrade_txholds(tx
, zp
);
4641 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4647 mutex_enter(&zp
->z_lock
);
4648 zp
->z_atime_dirty
= 0;
4650 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zfsvfs
), NULL
, &mode
, 8);
4651 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
4652 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
4653 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
4655 /* Preserve the mode, mtime and ctime provided by the inode */
4656 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4657 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4658 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4663 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4664 mutex_exit(&zp
->z_lock
);
4674 zfs_inactive(struct inode
*ip
)
4676 znode_t
*zp
= ITOZ(ip
);
4677 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4680 int need_unlock
= 0;
4682 /* Only read lock if we haven't already write locked, e.g. rollback */
4683 if (!RW_WRITE_HELD(&zfsvfs
->z_teardown_inactive_lock
)) {
4685 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_READER
);
4687 if (zp
->z_sa_hdl
== NULL
) {
4689 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
4693 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4694 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
4696 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4697 zfs_sa_upgrade_txholds(tx
, zp
);
4698 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4702 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4703 mutex_enter(&zp
->z_lock
);
4704 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
),
4705 (void *)&atime
, sizeof (atime
), tx
);
4706 zp
->z_atime_dirty
= 0;
4707 mutex_exit(&zp
->z_lock
);
4714 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
4718 * Bounds-check the seek operation.
4720 * IN: ip - inode seeking within
4721 * ooff - old file offset
4722 * noffp - pointer to new file offset
4723 * ct - caller context
4725 * RETURN: 0 if success
4726 * EINVAL if new offset invalid
4730 zfs_seek(struct inode
*ip
, offset_t ooff
, offset_t
*noffp
)
4732 if (S_ISDIR(ip
->i_mode
))
4734 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4738 * Fill pages with data from the disk.
4741 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4743 znode_t
*zp
= ITOZ(ip
);
4744 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4746 struct page
*cur_pp
;
4747 u_offset_t io_off
, total
;
4754 io_len
= nr_pages
<< PAGE_SHIFT
;
4755 i_size
= i_size_read(ip
);
4756 io_off
= page_offset(pl
[0]);
4758 if (io_off
+ io_len
> i_size
)
4759 io_len
= i_size
- io_off
;
4762 * Iterate over list of pages and read each page individually.
4765 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4768 cur_pp
= pl
[page_idx
++];
4770 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4774 /* convert checksum errors into IO errors */
4776 err
= SET_ERROR(EIO
);
4785 * Uses zfs_fillpage to read data from the file and fill the pages.
4787 * IN: ip - inode of file to get data from.
4788 * pl - list of pages to read
4789 * nr_pages - number of pages to read
4791 * RETURN: 0 on success, error code on failure.
4794 * vp - atime updated
4798 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4800 znode_t
*zp
= ITOZ(ip
);
4801 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4810 err
= zfs_fillpage(ip
, pl
, nr_pages
);
4817 * Check ZFS specific permissions to memory map a section of a file.
4819 * IN: ip - inode of the file to mmap
4821 * addrp - start address in memory region
4822 * len - length of memory region
4823 * vm_flags- address flags
4825 * RETURN: 0 if success
4826 * error code if failure
4830 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
4831 unsigned long vm_flags
)
4833 znode_t
*zp
= ITOZ(ip
);
4834 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4839 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
4840 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4842 return (SET_ERROR(EPERM
));
4845 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
4846 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4848 return (SET_ERROR(EACCES
));
4851 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4853 return (SET_ERROR(ENXIO
));
4861 * convoff - converts the given data (start, whence) to the
4865 convoff(struct inode
*ip
, flock64_t
*lckdat
, int whence
, offset_t offset
)
4870 if ((lckdat
->l_whence
== SEEK_END
) || (whence
== SEEK_END
)) {
4871 if ((error
= zfs_getattr(ip
, &vap
, 0, CRED())))
4875 switch (lckdat
->l_whence
) {
4877 lckdat
->l_start
+= offset
;
4880 lckdat
->l_start
+= vap
.va_size
;
4885 return (SET_ERROR(EINVAL
));
4888 if (lckdat
->l_start
< 0)
4889 return (SET_ERROR(EINVAL
));
4893 lckdat
->l_start
-= offset
;
4896 lckdat
->l_start
-= vap
.va_size
;
4901 return (SET_ERROR(EINVAL
));
4904 lckdat
->l_whence
= (short)whence
;
4909 * Free or allocate space in a file. Currently, this function only
4910 * supports the `F_FREESP' command. However, this command is somewhat
4911 * misnamed, as its functionality includes the ability to allocate as
4912 * well as free space.
4914 * IN: ip - inode of file to free data in.
4915 * cmd - action to take (only F_FREESP supported).
4916 * bfp - section of file to free/alloc.
4917 * flag - current file open mode flags.
4918 * offset - current file offset.
4919 * cr - credentials of caller.
4921 * RETURN: 0 on success, error code on failure.
4924 * ip - ctime|mtime updated
4928 zfs_space(struct inode
*ip
, int cmd
, flock64_t
*bfp
, int flag
,
4929 offset_t offset
, cred_t
*cr
)
4931 znode_t
*zp
= ITOZ(ip
);
4932 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4939 if (cmd
!= F_FREESP
) {
4941 return (SET_ERROR(EINVAL
));
4945 * Callers might not be able to detect properly that we are read-only,
4946 * so check it explicitly here.
4948 if (zfs_is_readonly(zfsvfs
)) {
4950 return (SET_ERROR(EROFS
));
4953 if ((error
= convoff(ip
, bfp
, SEEK_SET
, offset
))) {
4958 if (bfp
->l_len
< 0) {
4960 return (SET_ERROR(EINVAL
));
4964 * Permissions aren't checked on Solaris because on this OS
4965 * zfs_space() can only be called with an opened file handle.
4966 * On Linux we can get here through truncate_range() which
4967 * operates directly on inodes, so we need to check access rights.
4969 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
4975 len
= bfp
->l_len
; /* 0 means from off to end of file */
4977 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4985 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4987 znode_t
*zp
= ITOZ(ip
);
4988 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4991 uint64_t object
= zp
->z_id
;
4998 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
),
4999 &gen64
, sizeof (uint64_t))) != 0) {
5004 gen
= (uint32_t)gen64
;
5006 size
= SHORT_FID_LEN
;
5008 zfid
= (zfid_short_t
*)fidp
;
5010 zfid
->zf_len
= size
;
5012 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
5013 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
5015 /* Must have a non-zero generation number to distinguish from .zfs */
5018 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
5019 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
5027 zfs_getsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
5029 znode_t
*zp
= ITOZ(ip
);
5030 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
5032 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
5036 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
5044 zfs_setsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
5046 znode_t
*zp
= ITOZ(ip
);
5047 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
5049 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
5050 zilog_t
*zilog
= zfsvfs
->z_log
;
5055 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
5057 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
5058 zil_commit(zilog
, 0);
5064 #ifdef HAVE_UIO_ZEROCOPY
5066 * Tunable, both must be a power of 2.
5068 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
5069 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
5070 * an arcbuf for a partial block read
5072 int zcr_blksz_min
= (1 << 10); /* 1K */
5073 int zcr_blksz_max
= (1 << 17); /* 128K */
5077 zfs_reqzcbuf(struct inode
*ip
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
)
5079 znode_t
*zp
= ITOZ(ip
);
5080 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
5081 int max_blksz
= zfsvfs
->z_max_blksz
;
5082 uio_t
*uio
= &xuio
->xu_uio
;
5083 ssize_t size
= uio
->uio_resid
;
5084 offset_t offset
= uio
->uio_loffset
;
5089 int preamble
, postamble
;
5091 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
5092 return (SET_ERROR(EINVAL
));
5099 * Loan out an arc_buf for write if write size is bigger than
5100 * max_blksz, and the file's block size is also max_blksz.
5103 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
5105 return (SET_ERROR(EINVAL
));
5108 * Caller requests buffers for write before knowing where the
5109 * write offset might be (e.g. NFS TCP write).
5114 preamble
= P2PHASE(offset
, blksz
);
5116 preamble
= blksz
- preamble
;
5121 postamble
= P2PHASE(size
, blksz
);
5124 fullblk
= size
/ blksz
;
5125 (void) dmu_xuio_init(xuio
,
5126 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
5129 * Have to fix iov base/len for partial buffers. They
5130 * currently represent full arc_buf's.
5133 /* data begins in the middle of the arc_buf */
5134 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5137 (void) dmu_xuio_add(xuio
, abuf
,
5138 blksz
- preamble
, preamble
);
5141 for (i
= 0; i
< fullblk
; i
++) {
5142 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5145 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
5149 /* data ends in the middle of the arc_buf */
5150 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
5153 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
5158 * Loan out an arc_buf for read if the read size is larger than
5159 * the current file block size. Block alignment is not
5160 * considered. Partial arc_buf will be loaned out for read.
5162 blksz
= zp
->z_blksz
;
5163 if (blksz
< zcr_blksz_min
)
5164 blksz
= zcr_blksz_min
;
5165 if (blksz
> zcr_blksz_max
)
5166 blksz
= zcr_blksz_max
;
5167 /* avoid potential complexity of dealing with it */
5168 if (blksz
> max_blksz
) {
5170 return (SET_ERROR(EINVAL
));
5173 maxsize
= zp
->z_size
- uio
->uio_loffset
;
5179 return (SET_ERROR(EINVAL
));
5184 return (SET_ERROR(EINVAL
));
5187 uio
->uio_extflg
= UIO_XUIO
;
5188 XUIO_XUZC_RW(xuio
) = ioflag
;
5195 zfs_retzcbuf(struct inode
*ip
, xuio_t
*xuio
, cred_t
*cr
)
5199 int ioflag
= XUIO_XUZC_RW(xuio
);
5201 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
5203 i
= dmu_xuio_cnt(xuio
);
5205 abuf
= dmu_xuio_arcbuf(xuio
, i
);
5207 * if abuf == NULL, it must be a write buffer
5208 * that has been returned in zfs_write().
5211 dmu_return_arcbuf(abuf
);
5212 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
5215 dmu_xuio_fini(xuio
);
5218 #endif /* HAVE_UIO_ZEROCOPY */
5220 #if defined(_KERNEL)
5221 EXPORT_SYMBOL(zfs_open
);
5222 EXPORT_SYMBOL(zfs_close
);
5223 EXPORT_SYMBOL(zfs_read
);
5224 EXPORT_SYMBOL(zfs_write
);
5225 EXPORT_SYMBOL(zfs_access
);
5226 EXPORT_SYMBOL(zfs_lookup
);
5227 EXPORT_SYMBOL(zfs_create
);
5228 EXPORT_SYMBOL(zfs_tmpfile
);
5229 EXPORT_SYMBOL(zfs_remove
);
5230 EXPORT_SYMBOL(zfs_mkdir
);
5231 EXPORT_SYMBOL(zfs_rmdir
);
5232 EXPORT_SYMBOL(zfs_readdir
);
5233 EXPORT_SYMBOL(zfs_fsync
);
5234 EXPORT_SYMBOL(zfs_getattr
);
5235 EXPORT_SYMBOL(zfs_getattr_fast
);
5236 EXPORT_SYMBOL(zfs_setattr
);
5237 EXPORT_SYMBOL(zfs_rename
);
5238 EXPORT_SYMBOL(zfs_symlink
);
5239 EXPORT_SYMBOL(zfs_readlink
);
5240 EXPORT_SYMBOL(zfs_link
);
5241 EXPORT_SYMBOL(zfs_inactive
);
5242 EXPORT_SYMBOL(zfs_space
);
5243 EXPORT_SYMBOL(zfs_fid
);
5244 EXPORT_SYMBOL(zfs_getsecattr
);
5245 EXPORT_SYMBOL(zfs_setsecattr
);
5246 EXPORT_SYMBOL(zfs_getpage
);
5247 EXPORT_SYMBOL(zfs_putpage
);
5248 EXPORT_SYMBOL(zfs_dirty_inode
);
5249 EXPORT_SYMBOL(zfs_map
);
5252 module_param(zfs_delete_blocks
, ulong
, 0644);
5253 MODULE_PARM_DESC(zfs_delete_blocks
, "Delete files larger than N blocks async");
5254 module_param(zfs_read_chunk_size
, long, 0644);
5255 MODULE_PARM_DESC(zfs_read_chunk_size
, "Bytes to read per chunk");