4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
26 /* Portions Copyright 2007 Jeremy Teo */
27 /* Portions Copyright 2010 Robert Milkowski */
30 #include <sys/types.h>
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
37 #include <sys/vfs_opreg.h>
41 #include <sys/taskq.h>
43 #include <sys/vmsystm.h>
44 #include <sys/atomic.h>
46 #include <sys/pathname.h>
47 #include <sys/cmn_err.h>
48 #include <sys/errno.h>
49 #include <sys/unistd.h>
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_acl.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/fs/zfs.h>
55 #include <sys/dmu_objset.h>
61 #include <sys/dirent.h>
62 #include <sys/policy.h>
63 #include <sys/sunddi.h>
66 #include "fs/fs_subr.h"
67 #include <sys/zfs_ctldir.h>
68 #include <sys/zfs_fuid.h>
69 #include <sys/zfs_sa.h>
70 #include <sys/zfs_vnops.h>
72 #include <sys/zfs_rlock.h>
73 #include <sys/extdirent.h>
74 #include <sys/kidmap.h>
82 * Each vnode op performs some logical unit of work. To do this, the ZPL must
83 * properly lock its in-core state, create a DMU transaction, do the work,
84 * record this work in the intent log (ZIL), commit the DMU transaction,
85 * and wait for the intent log to commit if it is a synchronous operation.
86 * Moreover, the vnode ops must work in both normal and log replay context.
87 * The ordering of events is important to avoid deadlocks and references
88 * to freed memory. The example below illustrates the following Big Rules:
90 * (1) A check must be made in each zfs thread for a mounted file system.
91 * This is done avoiding races using ZFS_ENTER(zsb).
92 * A ZFS_EXIT(zsb) is needed before all returns. Any znodes
93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
94 * can return EIO from the calling function.
96 * (2) iput() should always be the last thing except for zil_commit()
97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
98 * First, if it's the last reference, the vnode/znode
99 * can be freed, so the zp may point to freed memory. Second, the last
100 * reference will call zfs_zinactive(), which may induce a lot of work --
101 * pushing cached pages (which acquires range locks) and syncing out
102 * cached atime changes. Third, zfs_zinactive() may require a new tx,
103 * which could deadlock the system if you were already holding one.
104 * If you must call iput() within a tx then use iput_ASYNC().
106 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
107 * as they can span dmu_tx_assign() calls.
109 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
110 * dmu_tx_assign(). This is critical because we don't want to block
111 * while holding locks.
113 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
114 * reduces lock contention and CPU usage when we must wait (note that if
115 * throughput is constrained by the storage, nearly every transaction
118 * Note, in particular, that if a lock is sometimes acquired before
119 * the tx assigns, and sometimes after (e.g. z_lock), then failing
120 * to use a non-blocking assign can deadlock the system. The scenario:
122 * Thread A has grabbed a lock before calling dmu_tx_assign().
123 * Thread B is in an already-assigned tx, and blocks for this lock.
124 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
125 * forever, because the previous txg can't quiesce until B's tx commits.
127 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
128 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
129 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
130 * to indicate that this operation has already called dmu_tx_wait().
131 * This will ensure that we don't retry forever, waiting a short bit
134 * (5) If the operation succeeded, generate the intent log entry for it
135 * before dropping locks. This ensures that the ordering of events
136 * in the intent log matches the order in which they actually occurred.
137 * During ZIL replay the zfs_log_* functions will update the sequence
138 * number to indicate the zil transaction has replayed.
140 * (6) At the end of each vnode op, the DMU tx must always commit,
141 * regardless of whether there were any errors.
143 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
144 * to ensure that synchronous semantics are provided when necessary.
146 * In general, this is how things should be ordered in each vnode op:
148 * ZFS_ENTER(zsb); // exit if unmounted
150 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
151 * rw_enter(...); // grab any other locks you need
152 * tx = dmu_tx_create(...); // get DMU tx
153 * dmu_tx_hold_*(); // hold each object you might modify
154 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
156 * rw_exit(...); // drop locks
157 * zfs_dirent_unlock(dl); // unlock directory entry
158 * iput(...); // release held vnodes
159 * if (error == ERESTART) {
165 * dmu_tx_abort(tx); // abort DMU tx
166 * ZFS_EXIT(zsb); // finished in zfs
167 * return (error); // really out of space
169 * error = do_real_work(); // do whatever this VOP does
171 * zfs_log_*(...); // on success, make ZIL entry
172 * dmu_tx_commit(tx); // commit DMU tx -- error or not
173 * rw_exit(...); // drop locks
174 * zfs_dirent_unlock(dl); // unlock directory entry
175 * iput(...); // release held vnodes
176 * zil_commit(zilog, foid); // synchronous when necessary
177 * ZFS_EXIT(zsb); // finished in zfs
178 * return (error); // done, report error
182 * Virus scanning is unsupported. It would be possible to add a hook
183 * here to performance the required virus scan. This could be done
184 * entirely in the kernel or potentially as an update to invoke a
188 zfs_vscan(struct inode
*ip
, cred_t
*cr
, int async
)
195 zfs_open(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
197 znode_t
*zp
= ITOZ(ip
);
198 zfs_sb_t
*zsb
= ITOZSB(ip
);
203 /* Honor ZFS_APPENDONLY file attribute */
204 if ((mode
& FMODE_WRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
205 ((flag
& O_APPEND
) == 0)) {
207 return (SET_ERROR(EPERM
));
210 /* Virus scan eligible files on open */
211 if (!zfs_has_ctldir(zp
) && zsb
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
212 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0) {
213 if (zfs_vscan(ip
, cr
, 0) != 0) {
215 return (SET_ERROR(EACCES
));
219 /* Keep a count of the synchronous opens in the znode */
221 atomic_inc_32(&zp
->z_sync_cnt
);
226 EXPORT_SYMBOL(zfs_open
);
230 zfs_close(struct inode
*ip
, int flag
, cred_t
*cr
)
232 znode_t
*zp
= ITOZ(ip
);
233 zfs_sb_t
*zsb
= ITOZSB(ip
);
239 * Zero the synchronous opens in the znode. Under Linux the
240 * zfs_close() hook is not symmetric with zfs_open(), it is
241 * only called once when the last reference is dropped.
246 if (!zfs_has_ctldir(zp
) && zsb
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
247 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0)
248 VERIFY(zfs_vscan(ip
, cr
, 1) == 0);
253 EXPORT_SYMBOL(zfs_close
);
255 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
257 * Lseek support for finding holes (cmd == SEEK_HOLE) and
258 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
261 zfs_holey_common(struct inode
*ip
, int cmd
, loff_t
*off
)
263 znode_t
*zp
= ITOZ(ip
);
264 uint64_t noff
= (uint64_t)*off
; /* new offset */
269 file_sz
= zp
->z_size
;
270 if (noff
>= file_sz
) {
271 return (SET_ERROR(ENXIO
));
274 if (cmd
== SEEK_HOLE
)
279 error
= dmu_offset_next(ZTOZSB(zp
)->z_os
, zp
->z_id
, hole
, &noff
);
282 if ((error
== ESRCH
) || (noff
> file_sz
)) {
284 * Handle the virtual hole at the end of file.
290 return (SET_ERROR(ENXIO
));
300 zfs_holey(struct inode
*ip
, int cmd
, loff_t
*off
)
302 znode_t
*zp
= ITOZ(ip
);
303 zfs_sb_t
*zsb
= ITOZSB(ip
);
309 error
= zfs_holey_common(ip
, cmd
, off
);
314 EXPORT_SYMBOL(zfs_holey
);
315 #endif /* SEEK_HOLE && SEEK_DATA */
319 * When a file is memory mapped, we must keep the IO data synchronized
320 * between the DMU cache and the memory mapped pages. What this means:
322 * On Write: If we find a memory mapped page, we write to *both*
323 * the page and the dmu buffer.
326 update_pages(struct inode
*ip
, int64_t start
, int len
,
327 objset_t
*os
, uint64_t oid
)
329 struct address_space
*mp
= ip
->i_mapping
;
335 off
= start
& (PAGE_CACHE_SIZE
-1);
336 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
337 nbytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
339 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_SHIFT
);
341 if (mapping_writably_mapped(mp
))
342 flush_dcache_page(pp
);
345 (void) dmu_read(os
, oid
, start
+off
, nbytes
, pb
+off
,
349 if (mapping_writably_mapped(mp
))
350 flush_dcache_page(pp
);
352 mark_page_accessed(pp
);
356 page_cache_release(pp
);
365 * When a file is memory mapped, we must keep the IO data synchronized
366 * between the DMU cache and the memory mapped pages. What this means:
368 * On Read: We "read" preferentially from memory mapped pages,
369 * else we default from the dmu buffer.
371 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
372 * the file is memory mapped.
375 mappedread(struct inode
*ip
, int nbytes
, uio_t
*uio
)
377 struct address_space
*mp
= ip
->i_mapping
;
379 znode_t
*zp
= ITOZ(ip
);
380 objset_t
*os
= ITOZSB(ip
)->z_os
;
387 start
= uio
->uio_loffset
;
388 off
= start
& (PAGE_CACHE_SIZE
-1);
389 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
390 bytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
392 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_SHIFT
);
394 ASSERT(PageUptodate(pp
));
397 error
= uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
400 if (mapping_writably_mapped(mp
))
401 flush_dcache_page(pp
);
403 mark_page_accessed(pp
);
405 page_cache_release(pp
);
407 error
= dmu_read_uio(os
, zp
->z_id
, uio
, bytes
);
419 unsigned long zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
422 * Read bytes from specified file into supplied buffer.
424 * IN: ip - inode of file to be read from.
425 * uio - structure supplying read location, range info,
427 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
428 * O_DIRECT flag; used to bypass page cache.
429 * cr - credentials of caller.
431 * OUT: uio - updated offset and range, buffer filled.
433 * RETURN: 0 on success, error code on failure.
436 * inode - atime updated if byte count > 0
440 zfs_read(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
442 znode_t
*zp
= ITOZ(ip
);
443 zfs_sb_t
*zsb
= ITOZSB(ip
);
448 #ifdef HAVE_UIO_ZEROCOPY
450 #endif /* HAVE_UIO_ZEROCOPY */
456 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
458 return (SET_ERROR(EACCES
));
462 * Validate file offset
464 if (uio
->uio_loffset
< (offset_t
)0) {
466 return (SET_ERROR(EINVAL
));
470 * Fasttrack empty reads
472 if (uio
->uio_resid
== 0) {
478 * Check for mandatory locks
480 if (mandatory_lock(ip
) &&
481 !lock_may_read(ip
, uio
->uio_loffset
, uio
->uio_resid
)) {
483 return (SET_ERROR(EAGAIN
));
487 * If we're in FRSYNC mode, sync out this znode before reading it.
489 if (ioflag
& FRSYNC
|| zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
490 zil_commit(zsb
->z_log
, zp
->z_id
);
493 * Lock the range against changes.
495 rl
= zfs_range_lock(zp
, uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
498 * If we are reading past end-of-file we can skip
499 * to the end; but we might still need to set atime.
501 if (uio
->uio_loffset
>= zp
->z_size
) {
506 ASSERT(uio
->uio_loffset
< zp
->z_size
);
507 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
509 #ifdef HAVE_UIO_ZEROCOPY
510 if ((uio
->uio_extflg
== UIO_XUIO
) &&
511 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
513 int blksz
= zp
->z_blksz
;
514 uint64_t offset
= uio
->uio_loffset
;
516 xuio
= (xuio_t
*)uio
;
518 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
521 ASSERT(offset
+ n
<= blksz
);
524 (void) dmu_xuio_init(xuio
, nblk
);
526 if (vn_has_cached_data(ip
)) {
528 * For simplicity, we always allocate a full buffer
529 * even if we only expect to read a portion of a block.
531 while (--nblk
>= 0) {
532 (void) dmu_xuio_add(xuio
,
533 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
538 #endif /* HAVE_UIO_ZEROCOPY */
541 nbytes
= MIN(n
, zfs_read_chunk_size
-
542 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
544 if (zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
545 error
= mappedread(ip
, nbytes
, uio
);
547 error
= dmu_read_uio(os
, zp
->z_id
, uio
, nbytes
);
550 /* convert checksum errors into IO errors */
552 error
= SET_ERROR(EIO
);
559 zfs_range_unlock(rl
);
561 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
562 zfs_inode_update(zp
);
566 EXPORT_SYMBOL(zfs_read
);
569 * Write the bytes to a file.
571 * IN: ip - inode of file to be written to.
572 * uio - structure supplying write location, range info,
574 * ioflag - FAPPEND flag set if in append mode.
575 * O_DIRECT flag; used to bypass page cache.
576 * cr - credentials of caller.
578 * OUT: uio - updated offset and range.
580 * RETURN: 0 if success
581 * error code if failure
584 * ip - ctime|mtime updated if byte count > 0
589 zfs_write(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
591 znode_t
*zp
= ITOZ(ip
);
592 rlim64_t limit
= uio
->uio_limit
;
593 ssize_t start_resid
= uio
->uio_resid
;
597 zfs_sb_t
*zsb
= ZTOZSB(zp
);
602 int max_blksz
= zsb
->z_max_blksz
;
605 iovec_t
*aiov
= NULL
;
608 iovec_t
*iovp
= uio
->uio_iov
;
611 sa_bulk_attr_t bulk
[4];
612 uint64_t mtime
[2], ctime
[2];
613 ASSERTV(int iovcnt
= uio
->uio_iovcnt
);
616 * Fasttrack empty write
622 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
628 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
629 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
630 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
, &zp
->z_size
, 8);
631 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
635 * If immutable or not appending then return EPERM
637 if ((zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
638 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
639 (uio
->uio_loffset
< zp
->z_size
))) {
641 return (SET_ERROR(EPERM
));
647 * Validate file offset
649 woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
652 return (SET_ERROR(EINVAL
));
656 * Check for mandatory locks before calling zfs_range_lock()
657 * in order to prevent a deadlock with locks set via fcntl().
659 if (mandatory_lock(ip
) && !lock_may_write(ip
, woff
, n
)) {
661 return (SET_ERROR(EAGAIN
));
665 * Pre-fault the pages to ensure slow (eg NFS) pages
667 * Skip this if uio contains loaned arc_buf.
669 #ifdef HAVE_UIO_ZEROCOPY
670 if ((uio
->uio_extflg
== UIO_XUIO
) &&
671 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
672 xuio
= (xuio_t
*)uio
;
675 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
678 * If in append mode, set the io offset pointer to eof.
680 if (ioflag
& FAPPEND
) {
682 * Obtain an appending range lock to guarantee file append
683 * semantics. We reset the write offset once we have the lock.
685 rl
= zfs_range_lock(zp
, 0, n
, RL_APPEND
);
687 if (rl
->r_len
== UINT64_MAX
) {
689 * We overlocked the file because this write will cause
690 * the file block size to increase.
691 * Note that zp_size cannot change with this lock held.
695 uio
->uio_loffset
= woff
;
698 * Note that if the file block size will change as a result of
699 * this write, then this range lock will lock the entire file
700 * so that we can re-write the block safely.
702 rl
= zfs_range_lock(zp
, woff
, n
, RL_WRITER
);
706 zfs_range_unlock(rl
);
708 return (SET_ERROR(EFBIG
));
711 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
714 /* Will this write extend the file length? */
715 write_eof
= (woff
+ n
> zp
->z_size
);
717 end_size
= MAX(zp
->z_size
, woff
+ n
);
720 * Write the file in reasonable size chunks. Each chunk is written
721 * in a separate transaction; this keeps the intent log records small
722 * and allows us to do more fine-grained space accounting.
726 woff
= uio
->uio_loffset
;
727 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
728 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
730 dmu_return_arcbuf(abuf
);
731 error
= SET_ERROR(EDQUOT
);
735 if (xuio
&& abuf
== NULL
) {
736 ASSERT(i_iov
< iovcnt
);
738 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
739 dmu_xuio_clear(xuio
, i_iov
);
740 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
741 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
742 aiov
->iov_len
== arc_buf_size(abuf
)));
744 } else if (abuf
== NULL
&& n
>= max_blksz
&&
745 woff
>= zp
->z_size
&&
746 P2PHASE(woff
, max_blksz
) == 0 &&
747 zp
->z_blksz
== max_blksz
) {
749 * This write covers a full block. "Borrow" a buffer
750 * from the dmu so that we can fill it before we enter
751 * a transaction. This avoids the possibility of
752 * holding up the transaction if the data copy hangs
753 * up on a pagefault (e.g., from an NFS server mapping).
757 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
759 ASSERT(abuf
!= NULL
);
760 ASSERT(arc_buf_size(abuf
) == max_blksz
);
761 if ((error
= uiocopy(abuf
->b_data
, max_blksz
,
762 UIO_WRITE
, uio
, &cbytes
))) {
763 dmu_return_arcbuf(abuf
);
766 ASSERT(cbytes
== max_blksz
);
770 * Start a transaction.
772 tx
= dmu_tx_create(zsb
->z_os
);
773 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
774 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
775 zfs_sa_upgrade_txholds(tx
, zp
);
776 error
= dmu_tx_assign(tx
, TXG_WAIT
);
780 dmu_return_arcbuf(abuf
);
785 * If zfs_range_lock() over-locked we grow the blocksize
786 * and then reduce the lock range. This will only happen
787 * on the first iteration since zfs_range_reduce() will
788 * shrink down r_len to the appropriate size.
790 if (rl
->r_len
== UINT64_MAX
) {
793 if (zp
->z_blksz
> max_blksz
) {
794 ASSERT(!ISP2(zp
->z_blksz
));
795 new_blksz
= MIN(end_size
, SPA_MAXBLOCKSIZE
);
797 new_blksz
= MIN(end_size
, max_blksz
);
799 zfs_grow_blocksize(zp
, new_blksz
, tx
);
800 zfs_range_reduce(rl
, woff
, n
);
804 * XXX - should we really limit each write to z_max_blksz?
805 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
807 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
810 tx_bytes
= uio
->uio_resid
;
811 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
813 tx_bytes
-= uio
->uio_resid
;
816 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
818 * If this is not a full block write, but we are
819 * extending the file past EOF and this data starts
820 * block-aligned, use assign_arcbuf(). Otherwise,
821 * write via dmu_write().
823 if (tx_bytes
< max_blksz
&& (!write_eof
||
824 aiov
->iov_base
!= abuf
->b_data
)) {
826 dmu_write(zsb
->z_os
, zp
->z_id
, woff
,
827 aiov
->iov_len
, aiov
->iov_base
, tx
);
828 dmu_return_arcbuf(abuf
);
829 xuio_stat_wbuf_copied();
831 ASSERT(xuio
|| tx_bytes
== max_blksz
);
832 dmu_assign_arcbuf(sa_get_db(zp
->z_sa_hdl
),
835 ASSERT(tx_bytes
<= uio
->uio_resid
);
836 uioskip(uio
, tx_bytes
);
839 if (tx_bytes
&& zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
840 update_pages(ip
, woff
, tx_bytes
, zsb
->z_os
, zp
->z_id
);
843 * If we made no progress, we're done. If we made even
844 * partial progress, update the znode and ZIL accordingly.
847 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
848 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
855 * Clear Set-UID/Set-GID bits on successful write if not
856 * privileged and at least one of the excute bits is set.
858 * It would be nice to to this after all writes have
859 * been done, but that would still expose the ISUID/ISGID
860 * to another app after the partial write is committed.
862 * Note: we don't call zfs_fuid_map_id() here because
863 * user 0 is not an ephemeral uid.
865 mutex_enter(&zp
->z_acl_lock
);
866 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
867 (S_IXUSR
>> 6))) != 0 &&
868 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
869 secpolicy_vnode_setid_retain(cr
,
870 (zp
->z_mode
& S_ISUID
) != 0 && zp
->z_uid
== 0) != 0) {
872 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
873 newmode
= zp
->z_mode
;
874 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
),
875 (void *)&newmode
, sizeof (uint64_t), tx
);
877 mutex_exit(&zp
->z_acl_lock
);
879 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
883 * Update the file size (zp_size) if it has changed;
884 * account for possible concurrent updates.
886 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
887 (void) atomic_cas_64(&zp
->z_size
, end_size
,
892 * If we are replaying and eof is non zero then force
893 * the file size to the specified eof. Note, there's no
894 * concurrency during replay.
896 if (zsb
->z_replay
&& zsb
->z_replay_eof
!= 0)
897 zp
->z_size
= zsb
->z_replay_eof
;
899 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
901 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
,
907 ASSERT(tx_bytes
== nbytes
);
911 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
914 zfs_range_unlock(rl
);
917 * If we're in replay mode, or we made no progress, return error.
918 * Otherwise, it's at least a partial write, so it's successful.
920 if (zsb
->z_replay
|| uio
->uio_resid
== start_resid
) {
925 if (ioflag
& (FSYNC
| FDSYNC
) ||
926 zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
927 zil_commit(zilog
, zp
->z_id
);
929 zfs_inode_update(zp
);
933 EXPORT_SYMBOL(zfs_write
);
936 iput_async(struct inode
*ip
, taskq_t
*taskq
)
938 ASSERT(atomic_read(&ip
->i_count
) > 0);
939 if (atomic_read(&ip
->i_count
) == 1)
940 taskq_dispatch(taskq
, (task_func_t
*)iput
, ip
, TQ_PUSHPAGE
);
946 zfs_get_done(zgd_t
*zgd
, int error
)
948 znode_t
*zp
= zgd
->zgd_private
;
949 objset_t
*os
= ZTOZSB(zp
)->z_os
;
952 dmu_buf_rele(zgd
->zgd_db
, zgd
);
954 zfs_range_unlock(zgd
->zgd_rl
);
957 * Release the vnode asynchronously as we currently have the
958 * txg stopped from syncing.
960 iput_async(ZTOI(zp
), dsl_pool_iput_taskq(dmu_objset_pool(os
)));
962 if (error
== 0 && zgd
->zgd_bp
)
963 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
965 kmem_free(zgd
, sizeof (zgd_t
));
969 static int zil_fault_io
= 0;
973 * Get data to generate a TX_WRITE intent log record.
976 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
979 objset_t
*os
= zsb
->z_os
;
981 uint64_t object
= lr
->lr_foid
;
982 uint64_t offset
= lr
->lr_offset
;
983 uint64_t size
= lr
->lr_length
;
984 blkptr_t
*bp
= &lr
->lr_blkptr
;
993 * Nothing to do if the file has been removed
995 if (zfs_zget(zsb
, object
, &zp
) != 0)
996 return (SET_ERROR(ENOENT
));
997 if (zp
->z_unlinked
) {
999 * Release the vnode asynchronously as we currently have the
1000 * txg stopped from syncing.
1002 iput_async(ZTOI(zp
), dsl_pool_iput_taskq(dmu_objset_pool(os
)));
1003 return (SET_ERROR(ENOENT
));
1006 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_PUSHPAGE
);
1007 zgd
->zgd_zilog
= zsb
->z_log
;
1008 zgd
->zgd_private
= zp
;
1011 * Write records come in two flavors: immediate and indirect.
1012 * For small writes it's cheaper to store the data with the
1013 * log record (immediate); for large writes it's cheaper to
1014 * sync the data and get a pointer to it (indirect) so that
1015 * we don't have to write the data twice.
1017 if (buf
!= NULL
) { /* immediate write */
1018 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
, RL_READER
);
1019 /* test for truncation needs to be done while range locked */
1020 if (offset
>= zp
->z_size
) {
1021 error
= SET_ERROR(ENOENT
);
1023 error
= dmu_read(os
, object
, offset
, size
, buf
,
1024 DMU_READ_NO_PREFETCH
);
1026 ASSERT(error
== 0 || error
== ENOENT
);
1027 } else { /* indirect write */
1029 * Have to lock the whole block to ensure when it's
1030 * written out and it's checksum is being calculated
1031 * that no one can change the data. We need to re-check
1032 * blocksize after we get the lock in case it's changed!
1037 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
1039 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
,
1041 if (zp
->z_blksz
== size
)
1044 zfs_range_unlock(zgd
->zgd_rl
);
1046 /* test for truncation needs to be done while range locked */
1047 if (lr
->lr_offset
>= zp
->z_size
)
1048 error
= SET_ERROR(ENOENT
);
1051 error
= SET_ERROR(EIO
);
1056 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1057 DMU_READ_NO_PREFETCH
);
1060 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
1062 ASSERT(BP_IS_HOLE(bp
));
1069 ASSERT(db
->db_offset
== offset
);
1070 ASSERT(db
->db_size
== size
);
1072 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1074 ASSERT(error
|| lr
->lr_length
<= zp
->z_blksz
);
1077 * On success, we need to wait for the write I/O
1078 * initiated by dmu_sync() to complete before we can
1079 * release this dbuf. We will finish everything up
1080 * in the zfs_get_done() callback.
1085 if (error
== EALREADY
) {
1086 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
1092 zfs_get_done(zgd
, error
);
1099 zfs_access(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
1101 znode_t
*zp
= ITOZ(ip
);
1102 zfs_sb_t
*zsb
= ITOZSB(ip
);
1108 if (flag
& V_ACE_MASK
)
1109 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
1111 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
1116 EXPORT_SYMBOL(zfs_access
);
1119 * Lookup an entry in a directory, or an extended attribute directory.
1120 * If it exists, return a held inode reference for it.
1122 * IN: dip - inode of directory to search.
1123 * nm - name of entry to lookup.
1124 * flags - LOOKUP_XATTR set if looking for an attribute.
1125 * cr - credentials of caller.
1126 * direntflags - directory lookup flags
1127 * realpnp - returned pathname.
1129 * OUT: ipp - inode of located entry, NULL if not found.
1131 * RETURN: 0 on success, error code on failure.
1138 zfs_lookup(struct inode
*dip
, char *nm
, struct inode
**ipp
, int flags
,
1139 cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
1141 znode_t
*zdp
= ITOZ(dip
);
1142 zfs_sb_t
*zsb
= ITOZSB(dip
);
1146 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1148 if (!S_ISDIR(dip
->i_mode
)) {
1149 return (SET_ERROR(ENOTDIR
));
1150 } else if (zdp
->z_sa_hdl
== NULL
) {
1151 return (SET_ERROR(EIO
));
1154 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1155 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1164 vnode_t
*tvp
= dnlc_lookup(dvp
, nm
);
1167 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1172 if (tvp
== DNLC_NO_VNODE
) {
1174 return (SET_ERROR(ENOENT
));
1177 return (specvp_check(vpp
, cr
));
1180 #endif /* HAVE_DNLC */
1189 if (flags
& LOOKUP_XATTR
) {
1191 * We don't allow recursive attributes..
1192 * Maybe someday we will.
1194 if (zdp
->z_pflags
& ZFS_XATTR
) {
1196 return (SET_ERROR(EINVAL
));
1199 if ((error
= zfs_get_xattrdir(zdp
, ipp
, cr
, flags
))) {
1205 * Do we have permission to get into attribute directory?
1208 if ((error
= zfs_zaccess(ITOZ(*ipp
), ACE_EXECUTE
, 0,
1218 if (!S_ISDIR(dip
->i_mode
)) {
1220 return (SET_ERROR(ENOTDIR
));
1224 * Check accessibility of directory.
1227 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
))) {
1232 if (zsb
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1233 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1235 return (SET_ERROR(EILSEQ
));
1238 error
= zfs_dirlook(zdp
, nm
, ipp
, flags
, direntflags
, realpnp
);
1239 if ((error
== 0) && (*ipp
))
1240 zfs_inode_update(ITOZ(*ipp
));
1245 EXPORT_SYMBOL(zfs_lookup
);
1248 * Attempt to create a new entry in a directory. If the entry
1249 * already exists, truncate the file if permissible, else return
1250 * an error. Return the ip of the created or trunc'd file.
1252 * IN: dip - inode of directory to put new file entry in.
1253 * name - name of new file entry.
1254 * vap - attributes of new file.
1255 * excl - flag indicating exclusive or non-exclusive mode.
1256 * mode - mode to open file with.
1257 * cr - credentials of caller.
1258 * flag - large file flag [UNUSED].
1259 * vsecp - ACL to be set
1261 * OUT: ipp - inode of created or trunc'd entry.
1263 * RETURN: 0 on success, error code on failure.
1266 * dip - ctime|mtime updated if new entry created
1267 * ip - ctime|mtime always, atime if new
1272 zfs_create(struct inode
*dip
, char *name
, vattr_t
*vap
, int excl
,
1273 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
1275 znode_t
*zp
, *dzp
= ITOZ(dip
);
1276 zfs_sb_t
*zsb
= ITOZSB(dip
);
1284 zfs_acl_ids_t acl_ids
;
1285 boolean_t fuid_dirtied
;
1286 boolean_t have_acl
= B_FALSE
;
1287 boolean_t waited
= B_FALSE
;
1290 * If we have an ephemeral id, ACL, or XVATTR then
1291 * make sure file system is at proper version
1297 if (zsb
->z_use_fuids
== B_FALSE
&&
1298 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1299 return (SET_ERROR(EINVAL
));
1306 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
1307 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1309 return (SET_ERROR(EILSEQ
));
1312 if (vap
->va_mask
& ATTR_XVATTR
) {
1313 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1314 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1322 if (*name
== '\0') {
1324 * Null component name refers to the directory itself.
1331 /* possible igrab(zp) */
1334 if (flag
& FIGNORECASE
)
1337 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1341 zfs_acl_ids_free(&acl_ids
);
1342 if (strcmp(name
, "..") == 0)
1343 error
= SET_ERROR(EISDIR
);
1353 * Create a new file object and update the directory
1356 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
1358 zfs_acl_ids_free(&acl_ids
);
1363 * We only support the creation of regular files in
1364 * extended attribute directories.
1367 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
1369 zfs_acl_ids_free(&acl_ids
);
1370 error
= SET_ERROR(EINVAL
);
1374 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1375 cr
, vsecp
, &acl_ids
)) != 0)
1379 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1380 zfs_acl_ids_free(&acl_ids
);
1381 error
= SET_ERROR(EDQUOT
);
1385 tx
= dmu_tx_create(os
);
1387 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1388 ZFS_SA_BASE_ATTR_SIZE
);
1390 fuid_dirtied
= zsb
->z_fuid_dirty
;
1392 zfs_fuid_txhold(zsb
, tx
);
1393 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1394 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1395 if (!zsb
->z_use_sa
&&
1396 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1397 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1398 0, acl_ids
.z_aclp
->z_acl_bytes
);
1400 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1402 zfs_dirent_unlock(dl
);
1403 if (error
== ERESTART
) {
1409 zfs_acl_ids_free(&acl_ids
);
1414 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1417 zfs_fuid_sync(zsb
, tx
);
1419 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1420 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1421 if (flag
& FIGNORECASE
)
1423 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1424 vsecp
, acl_ids
.z_fuidp
, vap
);
1425 zfs_acl_ids_free(&acl_ids
);
1428 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1431 zfs_acl_ids_free(&acl_ids
);
1435 * A directory entry already exists for this name.
1438 * Can't truncate an existing file if in exclusive mode.
1441 error
= SET_ERROR(EEXIST
);
1445 * Can't open a directory for writing.
1447 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
1448 error
= SET_ERROR(EISDIR
);
1452 * Verify requested access to file.
1454 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1458 mutex_enter(&dzp
->z_lock
);
1460 mutex_exit(&dzp
->z_lock
);
1463 * Truncate regular files if requested.
1465 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
1466 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
1467 /* we can't hold any locks when calling zfs_freesp() */
1468 zfs_dirent_unlock(dl
);
1470 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1476 zfs_dirent_unlock(dl
);
1482 zfs_inode_update(dzp
);
1483 zfs_inode_update(zp
);
1487 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1488 zil_commit(zilog
, 0);
1493 EXPORT_SYMBOL(zfs_create
);
1496 * Remove an entry from a directory.
1498 * IN: dip - inode of directory to remove entry from.
1499 * name - name of entry to remove.
1500 * cr - credentials of caller.
1502 * RETURN: 0 if success
1503 * error code if failure
1507 * ip - ctime (if nlink > 0)
1510 uint64_t null_xattr
= 0;
1514 zfs_remove(struct inode
*dip
, char *name
, cred_t
*cr
)
1516 znode_t
*zp
, *dzp
= ITOZ(dip
);
1519 zfs_sb_t
*zsb
= ITOZSB(dip
);
1522 uint64_t xattr_obj_unlinked
= 0;
1528 pathname_t
*realnmp
= NULL
;
1529 #ifdef HAVE_PN_UTILS
1531 #endif /* HAVE_PN_UTILS */
1534 boolean_t waited
= B_FALSE
;
1540 #ifdef HAVE_PN_UTILS
1541 if (flags
& FIGNORECASE
) {
1546 #endif /* HAVE_PN_UTILS */
1552 * Attempt to lock directory; fail if entry doesn't exist.
1554 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1556 #ifdef HAVE_PN_UTILS
1559 #endif /* HAVE_PN_UTILS */
1566 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1571 * Need to use rmdir for removing directories.
1573 if (S_ISDIR(ip
->i_mode
)) {
1574 error
= SET_ERROR(EPERM
);
1580 dnlc_remove(dvp
, realnmp
->pn_buf
);
1582 dnlc_remove(dvp
, name
);
1583 #endif /* HAVE_DNLC */
1586 * We never delete the znode and always place it in the unlinked
1587 * set. The dentry cache will always hold the last reference and
1588 * is responsible for safely freeing the znode.
1591 tx
= dmu_tx_create(zsb
->z_os
);
1592 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1593 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1594 zfs_sa_upgrade_txholds(tx
, zp
);
1595 zfs_sa_upgrade_txholds(tx
, dzp
);
1597 /* are there any extended attributes? */
1598 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1599 &xattr_obj
, sizeof (xattr_obj
));
1600 if (error
== 0 && xattr_obj
) {
1601 error
= zfs_zget(zsb
, xattr_obj
, &xzp
);
1603 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1604 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1607 /* charge as an update -- would be nice not to charge at all */
1608 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1610 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1612 zfs_dirent_unlock(dl
);
1616 if (error
== ERESTART
) {
1622 #ifdef HAVE_PN_UTILS
1625 #endif /* HAVE_PN_UTILS */
1632 * Remove the directory entry.
1634 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1643 * Hold z_lock so that we can make sure that the ACL obj
1644 * hasn't changed. Could have been deleted due to
1647 mutex_enter(&zp
->z_lock
);
1648 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1649 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1650 mutex_exit(&zp
->z_lock
);
1651 zfs_unlinked_add(zp
, tx
);
1655 #ifdef HAVE_PN_UTILS
1656 if (flags
& FIGNORECASE
)
1658 #endif /* HAVE_PN_UTILS */
1659 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
);
1663 #ifdef HAVE_PN_UTILS
1666 #endif /* HAVE_PN_UTILS */
1668 zfs_dirent_unlock(dl
);
1669 zfs_inode_update(dzp
);
1670 zfs_inode_update(zp
);
1672 zfs_inode_update(xzp
);
1678 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1679 zil_commit(zilog
, 0);
1684 EXPORT_SYMBOL(zfs_remove
);
1687 * Create a new directory and insert it into dip using the name
1688 * provided. Return a pointer to the inserted directory.
1690 * IN: dip - inode of directory to add subdir to.
1691 * dirname - name of new directory.
1692 * vap - attributes of new directory.
1693 * cr - credentials of caller.
1694 * vsecp - ACL to be set
1696 * OUT: ipp - inode of created directory.
1698 * RETURN: 0 if success
1699 * error code if failure
1702 * dip - ctime|mtime updated
1703 * ipp - ctime|mtime|atime updated
1707 zfs_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
, struct inode
**ipp
,
1708 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1710 znode_t
*zp
, *dzp
= ITOZ(dip
);
1711 zfs_sb_t
*zsb
= ITOZSB(dip
);
1719 gid_t gid
= crgetgid(cr
);
1720 zfs_acl_ids_t acl_ids
;
1721 boolean_t fuid_dirtied
;
1722 boolean_t waited
= B_FALSE
;
1724 ASSERT(S_ISDIR(vap
->va_mode
));
1727 * If we have an ephemeral id, ACL, or XVATTR then
1728 * make sure file system is at proper version
1732 if (zsb
->z_use_fuids
== B_FALSE
&&
1733 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1734 return (SET_ERROR(EINVAL
));
1740 if (dzp
->z_pflags
& ZFS_XATTR
) {
1742 return (SET_ERROR(EINVAL
));
1745 if (zsb
->z_utf8
&& u8_validate(dirname
,
1746 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1748 return (SET_ERROR(EILSEQ
));
1750 if (flags
& FIGNORECASE
)
1753 if (vap
->va_mask
& ATTR_XVATTR
) {
1754 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1755 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1761 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1762 vsecp
, &acl_ids
)) != 0) {
1767 * First make sure the new directory doesn't exist.
1769 * Existence is checked first to make sure we don't return
1770 * EACCES instead of EEXIST which can cause some applications
1776 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1778 zfs_acl_ids_free(&acl_ids
);
1783 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
1784 zfs_acl_ids_free(&acl_ids
);
1785 zfs_dirent_unlock(dl
);
1790 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1791 zfs_acl_ids_free(&acl_ids
);
1792 zfs_dirent_unlock(dl
);
1794 return (SET_ERROR(EDQUOT
));
1798 * Add a new entry to the directory.
1800 tx
= dmu_tx_create(zsb
->z_os
);
1801 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1802 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1803 fuid_dirtied
= zsb
->z_fuid_dirty
;
1805 zfs_fuid_txhold(zsb
, tx
);
1806 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1807 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1808 acl_ids
.z_aclp
->z_acl_bytes
);
1811 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1812 ZFS_SA_BASE_ATTR_SIZE
);
1814 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1816 zfs_dirent_unlock(dl
);
1817 if (error
== ERESTART
) {
1823 zfs_acl_ids_free(&acl_ids
);
1832 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1835 zfs_fuid_sync(zsb
, tx
);
1838 * Now put new name in parent dir.
1840 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1844 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1845 if (flags
& FIGNORECASE
)
1847 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
1848 acl_ids
.z_fuidp
, vap
);
1850 zfs_acl_ids_free(&acl_ids
);
1854 zfs_dirent_unlock(dl
);
1856 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1857 zil_commit(zilog
, 0);
1859 zfs_inode_update(dzp
);
1860 zfs_inode_update(zp
);
1864 EXPORT_SYMBOL(zfs_mkdir
);
1867 * Remove a directory subdir entry. If the current working
1868 * directory is the same as the subdir to be removed, the
1871 * IN: dip - inode of directory to remove from.
1872 * name - name of directory to be removed.
1873 * cwd - inode of current working directory.
1874 * cr - credentials of caller.
1875 * flags - case flags
1877 * RETURN: 0 on success, error code on failure.
1880 * dip - ctime|mtime updated
1884 zfs_rmdir(struct inode
*dip
, char *name
, struct inode
*cwd
, cred_t
*cr
,
1887 znode_t
*dzp
= ITOZ(dip
);
1890 zfs_sb_t
*zsb
= ITOZSB(dip
);
1896 boolean_t waited
= B_FALSE
;
1902 if (flags
& FIGNORECASE
)
1908 * Attempt to lock directory; fail if entry doesn't exist.
1910 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1918 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1922 if (!S_ISDIR(ip
->i_mode
)) {
1923 error
= SET_ERROR(ENOTDIR
);
1928 error
= SET_ERROR(EINVAL
);
1933 * Grab a lock on the directory to make sure that noone is
1934 * trying to add (or lookup) entries while we are removing it.
1936 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1939 * Grab a lock on the parent pointer to make sure we play well
1940 * with the treewalk and directory rename code.
1942 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1944 tx
= dmu_tx_create(zsb
->z_os
);
1945 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1946 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1947 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1948 zfs_sa_upgrade_txholds(tx
, zp
);
1949 zfs_sa_upgrade_txholds(tx
, dzp
);
1950 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1952 rw_exit(&zp
->z_parent_lock
);
1953 rw_exit(&zp
->z_name_lock
);
1954 zfs_dirent_unlock(dl
);
1956 if (error
== ERESTART
) {
1967 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
1970 uint64_t txtype
= TX_RMDIR
;
1971 if (flags
& FIGNORECASE
)
1973 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
);
1978 rw_exit(&zp
->z_parent_lock
);
1979 rw_exit(&zp
->z_name_lock
);
1981 zfs_dirent_unlock(dl
);
1983 zfs_inode_update(dzp
);
1984 zfs_inode_update(zp
);
1987 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1988 zil_commit(zilog
, 0);
1993 EXPORT_SYMBOL(zfs_rmdir
);
1996 * Read as many directory entries as will fit into the provided
1997 * dirent buffer from the given directory cursor position.
1999 * IN: ip - inode of directory to read.
2000 * dirent - buffer for directory entries.
2002 * OUT: dirent - filler buffer of directory entries.
2004 * RETURN: 0 if success
2005 * error code if failure
2008 * ip - atime updated
2010 * Note that the low 4 bits of the cookie returned by zap is always zero.
2011 * This allows us to use the low range for "special" directory entries:
2012 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2013 * we use the offset 2 for the '.zfs' directory.
2017 zfs_readdir(struct inode
*ip
, struct dir_context
*ctx
, cred_t
*cr
)
2019 znode_t
*zp
= ITOZ(ip
);
2020 zfs_sb_t
*zsb
= ITOZSB(ip
);
2023 zap_attribute_t zap
;
2029 uint64_t offset
; /* must be unsigned; checks for < 1 */
2034 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
2035 &parent
, sizeof (parent
))) != 0)
2039 * Quit if directory has been removed (posix)
2047 prefetch
= zp
->z_zn_prefetch
;
2050 * Initialize the iterator cursor.
2054 * Start iteration from the beginning of the directory.
2056 zap_cursor_init(&zc
, os
, zp
->z_id
);
2059 * The offset is a serialized cursor.
2061 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
2065 * Transform to file-system independent format
2070 * Special case `.', `..', and `.zfs'.
2073 (void) strcpy(zap
.za_name
, ".");
2074 zap
.za_normalization_conflict
= 0;
2077 } else if (offset
== 1) {
2078 (void) strcpy(zap
.za_name
, "..");
2079 zap
.za_normalization_conflict
= 0;
2082 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
2083 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
2084 zap
.za_normalization_conflict
= 0;
2085 objnum
= ZFSCTL_INO_ROOT
;
2091 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
2092 if (error
== ENOENT
)
2099 * Allow multiple entries provided the first entry is
2100 * the object id. Non-zpl consumers may safely make
2101 * use of the additional space.
2103 * XXX: This should be a feature flag for compatibility
2105 if (zap
.za_integer_length
!= 8 ||
2106 zap
.za_num_integers
== 0) {
2107 cmn_err(CE_WARN
, "zap_readdir: bad directory "
2108 "entry, obj = %lld, offset = %lld, "
2109 "length = %d, num = %lld\n",
2110 (u_longlong_t
)zp
->z_id
,
2111 (u_longlong_t
)offset
,
2112 zap
.za_integer_length
,
2113 (u_longlong_t
)zap
.za_num_integers
);
2114 error
= SET_ERROR(ENXIO
);
2118 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
2119 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
2122 done
= !dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
2127 /* Prefetch znode */
2129 dmu_prefetch(os
, objnum
, 0, 0);
2133 * Move to the next entry, fill in the previous offset.
2135 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2136 zap_cursor_advance(&zc
);
2137 offset
= zap_cursor_serialize(&zc
);
2143 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2146 zap_cursor_fini(&zc
);
2147 if (error
== ENOENT
)
2150 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
2151 zfs_inode_update(zp
);
2158 EXPORT_SYMBOL(zfs_readdir
);
2160 ulong_t zfs_fsync_sync_cnt
= 4;
2163 zfs_fsync(struct inode
*ip
, int syncflag
, cred_t
*cr
)
2165 znode_t
*zp
= ITOZ(ip
);
2166 zfs_sb_t
*zsb
= ITOZSB(ip
);
2168 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2170 if (zsb
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2173 zil_commit(zsb
->z_log
, zp
->z_id
);
2178 EXPORT_SYMBOL(zfs_fsync
);
2182 * Get the requested file attributes and place them in the provided
2185 * IN: ip - inode of file.
2186 * vap - va_mask identifies requested attributes.
2187 * If ATTR_XVATTR set, then optional attrs are requested
2188 * flags - ATTR_NOACLCHECK (CIFS server context)
2189 * cr - credentials of caller.
2191 * OUT: vap - attribute values.
2193 * RETURN: 0 (always succeeds)
2197 zfs_getattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2199 znode_t
*zp
= ITOZ(ip
);
2200 zfs_sb_t
*zsb
= ITOZSB(ip
);
2203 uint64_t mtime
[2], ctime
[2];
2204 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2205 xoptattr_t
*xoap
= NULL
;
2206 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2207 sa_bulk_attr_t bulk
[2];
2213 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2215 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
2216 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
2218 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2224 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2225 * Also, if we are the owner don't bother, since owner should
2226 * always be allowed to read basic attributes of file.
2228 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2229 (vap
->va_uid
!= crgetuid(cr
))) {
2230 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2238 * Return all attributes. It's cheaper to provide the answer
2239 * than to determine whether we were asked the question.
2242 mutex_enter(&zp
->z_lock
);
2243 vap
->va_type
= vn_mode_to_vtype(zp
->z_mode
);
2244 vap
->va_mode
= zp
->z_mode
;
2245 vap
->va_fsid
= ZTOI(zp
)->i_sb
->s_dev
;
2246 vap
->va_nodeid
= zp
->z_id
;
2247 if ((zp
->z_id
== zsb
->z_root
) && zfs_show_ctldir(zp
))
2248 links
= zp
->z_links
+ 1;
2250 links
= zp
->z_links
;
2251 vap
->va_nlink
= MIN(links
, ZFS_LINK_MAX
);
2252 vap
->va_size
= i_size_read(ip
);
2253 vap
->va_rdev
= ip
->i_rdev
;
2254 vap
->va_seq
= ip
->i_generation
;
2257 * Add in any requested optional attributes and the create time.
2258 * Also set the corresponding bits in the returned attribute bitmap.
2260 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zsb
->z_use_fuids
) {
2261 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2263 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2264 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2267 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2268 xoap
->xoa_readonly
=
2269 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2270 XVA_SET_RTN(xvap
, XAT_READONLY
);
2273 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2275 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2276 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2279 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2281 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2282 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2285 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2286 xoap
->xoa_nounlink
=
2287 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2288 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2291 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2292 xoap
->xoa_immutable
=
2293 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2294 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2297 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2298 xoap
->xoa_appendonly
=
2299 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2300 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2303 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2305 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2306 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2309 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2311 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2312 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2315 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2316 xoap
->xoa_av_quarantined
=
2317 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2318 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2321 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2322 xoap
->xoa_av_modified
=
2323 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2324 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2327 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2328 S_ISREG(ip
->i_mode
)) {
2329 zfs_sa_get_scanstamp(zp
, xvap
);
2332 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2335 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zsb
),
2336 times
, sizeof (times
));
2337 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2338 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2341 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2342 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2343 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2345 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2346 xoap
->xoa_generation
= zp
->z_gen
;
2347 XVA_SET_RTN(xvap
, XAT_GEN
);
2350 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2352 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2353 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2356 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2358 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2359 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2363 ZFS_TIME_DECODE(&vap
->va_atime
, zp
->z_atime
);
2364 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2365 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2367 mutex_exit(&zp
->z_lock
);
2369 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2371 if (zp
->z_blksz
== 0) {
2373 * Block size hasn't been set; suggest maximal I/O transfers.
2375 vap
->va_blksize
= zsb
->z_max_blksz
;
2381 EXPORT_SYMBOL(zfs_getattr
);
2384 * Get the basic file attributes and place them in the provided kstat
2385 * structure. The inode is assumed to be the authoritative source
2386 * for most of the attributes. However, the znode currently has the
2387 * authoritative atime, blksize, and block count.
2389 * IN: ip - inode of file.
2391 * OUT: sp - kstat values.
2393 * RETURN: 0 (always succeeds)
2397 zfs_getattr_fast(struct inode
*ip
, struct kstat
*sp
)
2399 znode_t
*zp
= ITOZ(ip
);
2400 zfs_sb_t
*zsb
= ITOZSB(ip
);
2405 mutex_enter(&zp
->z_lock
);
2407 generic_fillattr(ip
, sp
);
2408 ZFS_TIME_DECODE(&sp
->atime
, zp
->z_atime
);
2410 sa_object_size(zp
->z_sa_hdl
, (uint32_t *)&sp
->blksize
, &sp
->blocks
);
2411 if (unlikely(zp
->z_blksz
== 0)) {
2413 * Block size hasn't been set; suggest maximal I/O transfers.
2415 sp
->blksize
= zsb
->z_max_blksz
;
2418 mutex_exit(&zp
->z_lock
);
2424 EXPORT_SYMBOL(zfs_getattr_fast
);
2427 * Set the file attributes to the values contained in the
2430 * IN: ip - inode of file to be modified.
2431 * vap - new attribute values.
2432 * If ATTR_XVATTR set, then optional attrs are being set
2433 * flags - ATTR_UTIME set if non-default time values provided.
2434 * - ATTR_NOACLCHECK (CIFS context only).
2435 * cr - credentials of caller.
2437 * RETURN: 0 if success
2438 * error code if failure
2441 * ip - ctime updated, mtime updated if size changed.
2445 zfs_setattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2447 znode_t
*zp
= ITOZ(ip
);
2448 zfs_sb_t
*zsb
= ITOZSB(ip
);
2452 xvattr_t
*tmpxvattr
;
2453 uint_t mask
= vap
->va_mask
;
2454 uint_t saved_mask
= 0;
2457 uint64_t new_uid
, new_gid
;
2459 uint64_t mtime
[2], ctime
[2];
2461 int need_policy
= FALSE
;
2463 zfs_fuid_info_t
*fuidp
= NULL
;
2464 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2467 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2468 boolean_t fuid_dirtied
= B_FALSE
;
2469 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
2470 int count
= 0, xattr_count
= 0;
2481 * Make sure that if we have ephemeral uid/gid or xvattr specified
2482 * that file system is at proper version level
2485 if (zsb
->z_use_fuids
== B_FALSE
&&
2486 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2487 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2488 (mask
& ATTR_XVATTR
))) {
2490 return (SET_ERROR(EINVAL
));
2493 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
2495 return (SET_ERROR(EISDIR
));
2498 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
2500 return (SET_ERROR(EINVAL
));
2504 * If this is an xvattr_t, then get a pointer to the structure of
2505 * optional attributes. If this is NULL, then we have a vattr_t.
2507 xoap
= xva_getxoptattr(xvap
);
2509 tmpxvattr
= kmem_alloc(sizeof(xvattr_t
), KM_SLEEP
);
2510 xva_init(tmpxvattr
);
2512 bulk
= kmem_alloc(sizeof(sa_bulk_attr_t
) * 7, KM_SLEEP
);
2513 xattr_bulk
= kmem_alloc(sizeof(sa_bulk_attr_t
) * 7, KM_SLEEP
);
2516 * Immutable files can only alter immutable bit and atime
2518 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2519 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
2520 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2525 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2531 * Verify timestamps doesn't overflow 32 bits.
2532 * ZFS can handle large timestamps, but 32bit syscalls can't
2533 * handle times greater than 2039. This check should be removed
2534 * once large timestamps are fully supported.
2536 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
2537 if (((mask
& ATTR_ATIME
) && TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2538 ((mask
& ATTR_MTIME
) && TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2548 /* Can this be moved to before the top label? */
2549 if (zfs_is_readonly(zsb
)) {
2555 * First validate permissions
2558 if (mask
& ATTR_SIZE
) {
2559 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2563 truncate_setsize(ip
, vap
->va_size
);
2566 * XXX - Note, we are not providing any open
2567 * mode flags here (like FNDELAY), so we may
2568 * block if there are locks present... this
2569 * should be addressed in openat().
2571 /* XXX - would it be OK to generate a log record here? */
2572 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2577 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
2578 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2579 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2580 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2581 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2582 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2583 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2584 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2585 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2589 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2590 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
2595 * NOTE: even if a new mode is being set,
2596 * we may clear S_ISUID/S_ISGID bits.
2599 if (!(mask
& ATTR_MODE
))
2600 vap
->va_mode
= zp
->z_mode
;
2603 * Take ownership or chgrp to group we are a member of
2606 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
2607 take_group
= (mask
& ATTR_GID
) &&
2608 zfs_groupmember(zsb
, vap
->va_gid
, cr
);
2611 * If both ATTR_UID and ATTR_GID are set then take_owner and
2612 * take_group must both be set in order to allow taking
2615 * Otherwise, send the check through secpolicy_vnode_setattr()
2619 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
2620 take_owner
&& take_group
) ||
2621 ((idmask
== ATTR_UID
) && take_owner
) ||
2622 ((idmask
== ATTR_GID
) && take_group
)) {
2623 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2624 skipaclchk
, cr
) == 0) {
2626 * Remove setuid/setgid for non-privileged users
2628 (void) secpolicy_setid_clear(vap
, cr
);
2629 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
2638 mutex_enter(&zp
->z_lock
);
2639 oldva
.va_mode
= zp
->z_mode
;
2640 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2641 if (mask
& ATTR_XVATTR
) {
2643 * Update xvattr mask to include only those attributes
2644 * that are actually changing.
2646 * the bits will be restored prior to actually setting
2647 * the attributes so the caller thinks they were set.
2649 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2650 if (xoap
->xoa_appendonly
!=
2651 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2654 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2655 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
2659 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2660 if (xoap
->xoa_nounlink
!=
2661 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2664 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2665 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
2669 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2670 if (xoap
->xoa_immutable
!=
2671 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2674 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2675 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
2679 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2680 if (xoap
->xoa_nodump
!=
2681 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2684 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2685 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
2689 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2690 if (xoap
->xoa_av_modified
!=
2691 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2694 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2695 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
2699 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2700 if ((!S_ISREG(ip
->i_mode
) &&
2701 xoap
->xoa_av_quarantined
) ||
2702 xoap
->xoa_av_quarantined
!=
2703 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2706 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2707 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
2711 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2712 mutex_exit(&zp
->z_lock
);
2717 if (need_policy
== FALSE
&&
2718 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2719 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2724 mutex_exit(&zp
->z_lock
);
2726 if (mask
& ATTR_MODE
) {
2727 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2728 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
2733 trim_mask
|= ATTR_MODE
;
2741 * If trim_mask is set then take ownership
2742 * has been granted or write_acl is present and user
2743 * has the ability to modify mode. In that case remove
2744 * UID|GID and or MODE from mask so that
2745 * secpolicy_vnode_setattr() doesn't revoke it.
2749 saved_mask
= vap
->va_mask
;
2750 vap
->va_mask
&= ~trim_mask
;
2752 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
2753 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2758 vap
->va_mask
|= saved_mask
;
2762 * secpolicy_vnode_setattr, or take ownership may have
2765 mask
= vap
->va_mask
;
2767 if ((mask
& (ATTR_UID
| ATTR_GID
))) {
2768 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
2769 &xattr_obj
, sizeof (xattr_obj
));
2771 if (err
== 0 && xattr_obj
) {
2772 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
2776 if (mask
& ATTR_UID
) {
2777 new_uid
= zfs_fuid_create(zsb
,
2778 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2779 if (new_uid
!= zp
->z_uid
&&
2780 zfs_fuid_overquota(zsb
, B_FALSE
, new_uid
)) {
2788 if (mask
& ATTR_GID
) {
2789 new_gid
= zfs_fuid_create(zsb
, (uint64_t)vap
->va_gid
,
2790 cr
, ZFS_GROUP
, &fuidp
);
2791 if (new_gid
!= zp
->z_gid
&&
2792 zfs_fuid_overquota(zsb
, B_TRUE
, new_gid
)) {
2800 tx
= dmu_tx_create(zsb
->z_os
);
2802 if (mask
& ATTR_MODE
) {
2803 uint64_t pmode
= zp
->z_mode
;
2805 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2807 zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
);
2809 mutex_enter(&zp
->z_lock
);
2810 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2812 * Are we upgrading ACL from old V0 format
2815 if (zsb
->z_version
>= ZPL_VERSION_FUID
&&
2816 zfs_znode_acl_version(zp
) ==
2817 ZFS_ACL_VERSION_INITIAL
) {
2818 dmu_tx_hold_free(tx
, acl_obj
, 0,
2820 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2821 0, aclp
->z_acl_bytes
);
2823 dmu_tx_hold_write(tx
, acl_obj
, 0,
2826 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2827 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2828 0, aclp
->z_acl_bytes
);
2830 mutex_exit(&zp
->z_lock
);
2831 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2833 if ((mask
& ATTR_XVATTR
) &&
2834 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2835 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2837 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2841 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2844 fuid_dirtied
= zsb
->z_fuid_dirty
;
2846 zfs_fuid_txhold(zsb
, tx
);
2848 zfs_sa_upgrade_txholds(tx
, zp
);
2850 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2856 * Set each attribute requested.
2857 * We group settings according to the locks they need to acquire.
2859 * Note: you cannot set ctime directly, although it will be
2860 * updated as a side-effect of calling this function.
2864 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2865 mutex_enter(&zp
->z_acl_lock
);
2866 mutex_enter(&zp
->z_lock
);
2868 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
2869 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2872 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2873 mutex_enter(&attrzp
->z_acl_lock
);
2874 mutex_enter(&attrzp
->z_lock
);
2875 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2876 SA_ZPL_FLAGS(zsb
), NULL
, &attrzp
->z_pflags
,
2877 sizeof (attrzp
->z_pflags
));
2880 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2882 if (mask
& ATTR_UID
) {
2883 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
2884 &new_uid
, sizeof (new_uid
));
2885 zp
->z_uid
= new_uid
;
2887 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2888 SA_ZPL_UID(zsb
), NULL
, &new_uid
,
2890 attrzp
->z_uid
= new_uid
;
2894 if (mask
& ATTR_GID
) {
2895 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
),
2896 NULL
, &new_gid
, sizeof (new_gid
));
2897 zp
->z_gid
= new_gid
;
2899 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2900 SA_ZPL_GID(zsb
), NULL
, &new_gid
,
2902 attrzp
->z_gid
= new_gid
;
2905 if (!(mask
& ATTR_MODE
)) {
2906 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
),
2907 NULL
, &new_mode
, sizeof (new_mode
));
2908 new_mode
= zp
->z_mode
;
2910 err
= zfs_acl_chown_setattr(zp
);
2913 err
= zfs_acl_chown_setattr(attrzp
);
2918 if (mask
& ATTR_MODE
) {
2919 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
2920 &new_mode
, sizeof (new_mode
));
2921 zp
->z_mode
= new_mode
;
2922 ASSERT3P(aclp
, !=, NULL
);
2923 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2925 if (zp
->z_acl_cached
)
2926 zfs_acl_free(zp
->z_acl_cached
);
2927 zp
->z_acl_cached
= aclp
;
2932 if (mask
& ATTR_ATIME
) {
2933 ZFS_TIME_ENCODE(&vap
->va_atime
, zp
->z_atime
);
2934 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
2935 &zp
->z_atime
, sizeof (zp
->z_atime
));
2938 if (mask
& ATTR_MTIME
) {
2939 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2940 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
,
2941 mtime
, sizeof (mtime
));
2944 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2945 if (mask
& ATTR_SIZE
&& !(mask
& ATTR_MTIME
)) {
2946 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
),
2947 NULL
, mtime
, sizeof (mtime
));
2948 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2949 &ctime
, sizeof (ctime
));
2950 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
2952 } else if (mask
!= 0) {
2953 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2954 &ctime
, sizeof (ctime
));
2955 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
,
2958 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2959 SA_ZPL_CTIME(zsb
), NULL
,
2960 &ctime
, sizeof (ctime
));
2961 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
2962 mtime
, ctime
, B_TRUE
);
2966 * Do this after setting timestamps to prevent timestamp
2967 * update from toggling bit
2970 if (xoap
&& (mask
& ATTR_XVATTR
)) {
2973 * restore trimmed off masks
2974 * so that return masks can be set for caller.
2977 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
2978 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2980 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
2981 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2983 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
2984 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2986 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
2987 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2989 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
2990 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2992 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
2993 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2996 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2997 ASSERT(S_ISREG(ip
->i_mode
));
2999 zfs_xvattr_set(zp
, xvap
, tx
);
3003 zfs_fuid_sync(zsb
, tx
);
3006 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
3008 mutex_exit(&zp
->z_lock
);
3009 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3010 mutex_exit(&zp
->z_acl_lock
);
3013 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3014 mutex_exit(&attrzp
->z_acl_lock
);
3015 mutex_exit(&attrzp
->z_lock
);
3018 if (err
== 0 && attrzp
) {
3019 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3030 zfs_fuid_info_free(fuidp
);
3036 if (err
== ERESTART
)
3039 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3041 zfs_inode_update(zp
);
3045 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3046 zil_commit(zilog
, 0);
3049 kmem_free(xattr_bulk
, sizeof(sa_bulk_attr_t
) * 7);
3050 kmem_free(bulk
, sizeof(sa_bulk_attr_t
) * 7);
3051 kmem_free(tmpxvattr
, sizeof(xvattr_t
));
3055 EXPORT_SYMBOL(zfs_setattr
);
3057 typedef struct zfs_zlock
{
3058 krwlock_t
*zl_rwlock
; /* lock we acquired */
3059 znode_t
*zl_znode
; /* znode we held */
3060 struct zfs_zlock
*zl_next
; /* next in list */
3064 * Drop locks and release vnodes that were held by zfs_rename_lock().
3067 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3071 while ((zl
= *zlpp
) != NULL
) {
3072 if (zl
->zl_znode
!= NULL
)
3073 iput(ZTOI(zl
->zl_znode
));
3074 rw_exit(zl
->zl_rwlock
);
3075 *zlpp
= zl
->zl_next
;
3076 kmem_free(zl
, sizeof (*zl
));
3081 * Search back through the directory tree, using the ".." entries.
3082 * Lock each directory in the chain to prevent concurrent renames.
3083 * Fail any attempt to move a directory into one of its own descendants.
3084 * XXX - z_parent_lock can overlap with map or grow locks
3087 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3091 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
3092 uint64_t oidp
= zp
->z_id
;
3093 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3094 krw_t rw
= RW_WRITER
;
3097 * First pass write-locks szp and compares to zp->z_id.
3098 * Later passes read-lock zp and compare to zp->z_parent.
3101 if (!rw_tryenter(rwlp
, rw
)) {
3103 * Another thread is renaming in this path.
3104 * Note that if we are a WRITER, we don't have any
3105 * parent_locks held yet.
3107 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3109 * Drop our locks and restart
3111 zfs_rename_unlock(&zl
);
3115 rwlp
= &szp
->z_parent_lock
;
3120 * Wait for other thread to drop its locks
3126 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3127 zl
->zl_rwlock
= rwlp
;
3128 zl
->zl_znode
= NULL
;
3129 zl
->zl_next
= *zlpp
;
3132 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3133 return (SET_ERROR(EINVAL
));
3135 if (oidp
== rootid
) /* We've hit the top */
3138 if (rw
== RW_READER
) { /* i.e. not the first pass */
3139 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
3144 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
3145 &oidp
, sizeof (oidp
));
3146 rwlp
= &zp
->z_parent_lock
;
3149 } while (zp
->z_id
!= sdzp
->z_id
);
3155 * Move an entry from the provided source directory to the target
3156 * directory. Change the entry name as indicated.
3158 * IN: sdip - Source directory containing the "old entry".
3159 * snm - Old entry name.
3160 * tdip - Target directory to contain the "new entry".
3161 * tnm - New entry name.
3162 * cr - credentials of caller.
3163 * flags - case flags
3165 * RETURN: 0 on success, error code on failure.
3168 * sdip,tdip - ctime|mtime updated
3172 zfs_rename(struct inode
*sdip
, char *snm
, struct inode
*tdip
, char *tnm
,
3173 cred_t
*cr
, int flags
)
3175 znode_t
*tdzp
, *szp
, *tzp
;
3176 znode_t
*sdzp
= ITOZ(sdip
);
3177 zfs_sb_t
*zsb
= ITOZSB(sdip
);
3179 zfs_dirlock_t
*sdl
, *tdl
;
3182 int cmp
, serr
, terr
;
3185 boolean_t waited
= B_FALSE
;
3188 ZFS_VERIFY_ZP(sdzp
);
3191 if (tdip
->i_sb
!= sdip
->i_sb
|| zfsctl_is_node(tdip
)) {
3193 return (SET_ERROR(EXDEV
));
3197 ZFS_VERIFY_ZP(tdzp
);
3198 if (zsb
->z_utf8
&& u8_validate(tnm
,
3199 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3201 return (SET_ERROR(EILSEQ
));
3204 if (flags
& FIGNORECASE
)
3213 * This is to prevent the creation of links into attribute space
3214 * by renaming a linked file into/outof an attribute directory.
3215 * See the comment in zfs_link() for why this is considered bad.
3217 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3219 return (SET_ERROR(EINVAL
));
3223 * Lock source and target directory entries. To prevent deadlock,
3224 * a lock ordering must be defined. We lock the directory with
3225 * the smallest object id first, or if it's a tie, the one with
3226 * the lexically first name.
3228 if (sdzp
->z_id
< tdzp
->z_id
) {
3230 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3234 * First compare the two name arguments without
3235 * considering any case folding.
3237 int nofold
= (zsb
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3239 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3240 ASSERT(error
== 0 || !zsb
->z_utf8
);
3243 * POSIX: "If the old argument and the new argument
3244 * both refer to links to the same existing file,
3245 * the rename() function shall return successfully
3246 * and perform no other action."
3252 * If the file system is case-folding, then we may
3253 * have some more checking to do. A case-folding file
3254 * system is either supporting mixed case sensitivity
3255 * access or is completely case-insensitive. Note
3256 * that the file system is always case preserving.
3258 * In mixed sensitivity mode case sensitive behavior
3259 * is the default. FIGNORECASE must be used to
3260 * explicitly request case insensitive behavior.
3262 * If the source and target names provided differ only
3263 * by case (e.g., a request to rename 'tim' to 'Tim'),
3264 * we will treat this as a special case in the
3265 * case-insensitive mode: as long as the source name
3266 * is an exact match, we will allow this to proceed as
3267 * a name-change request.
3269 if ((zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
3270 (zsb
->z_case
== ZFS_CASE_MIXED
&&
3271 flags
& FIGNORECASE
)) &&
3272 u8_strcmp(snm
, tnm
, 0, zsb
->z_norm
, U8_UNICODE_LATEST
,
3275 * case preserving rename request, require exact
3284 * If the source and destination directories are the same, we should
3285 * grab the z_name_lock of that directory only once.
3289 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3293 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3294 ZEXISTS
| zflg
, NULL
, NULL
);
3295 terr
= zfs_dirent_lock(&tdl
,
3296 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3298 terr
= zfs_dirent_lock(&tdl
,
3299 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3300 serr
= zfs_dirent_lock(&sdl
,
3301 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3307 * Source entry invalid or not there.
3310 zfs_dirent_unlock(tdl
);
3316 rw_exit(&sdzp
->z_name_lock
);
3318 if (strcmp(snm
, "..") == 0)
3324 zfs_dirent_unlock(sdl
);
3328 rw_exit(&sdzp
->z_name_lock
);
3330 if (strcmp(tnm
, "..") == 0)
3337 * Must have write access at the source to remove the old entry
3338 * and write access at the target to create the new entry.
3339 * Note that if target and source are the same, this can be
3340 * done in a single check.
3343 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
3346 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3348 * Check to make sure rename is valid.
3349 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3351 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
3356 * Does target exist?
3360 * Source and target must be the same type.
3362 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3363 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3364 error
= SET_ERROR(ENOTDIR
);
3368 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3369 error
= SET_ERROR(EISDIR
);
3374 * POSIX dictates that when the source and target
3375 * entries refer to the same file object, rename
3376 * must do nothing and exit without error.
3378 if (szp
->z_id
== tzp
->z_id
) {
3384 tx
= dmu_tx_create(zsb
->z_os
);
3385 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3386 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3387 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3388 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3390 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3391 zfs_sa_upgrade_txholds(tx
, tdzp
);
3394 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3395 zfs_sa_upgrade_txholds(tx
, tzp
);
3398 zfs_sa_upgrade_txholds(tx
, szp
);
3399 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
3400 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3403 zfs_rename_unlock(&zl
);
3404 zfs_dirent_unlock(sdl
);
3405 zfs_dirent_unlock(tdl
);
3408 rw_exit(&sdzp
->z_name_lock
);
3413 if (error
== ERESTART
) {
3424 if (tzp
) /* Attempt to remove the existing target */
3425 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3428 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3430 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3432 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zsb
),
3433 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3436 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3438 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3439 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
3440 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3443 * At this point, we have successfully created
3444 * the target name, but have failed to remove
3445 * the source name. Since the create was done
3446 * with the ZRENAMING flag, there are
3447 * complications; for one, the link count is
3448 * wrong. The easiest way to deal with this
3449 * is to remove the newly created target, and
3450 * return the original error. This must
3451 * succeed; fortunately, it is very unlikely to
3452 * fail, since we just created it.
3454 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3455 ZRENAMING
, NULL
), ==, 0);
3463 zfs_rename_unlock(&zl
);
3465 zfs_dirent_unlock(sdl
);
3466 zfs_dirent_unlock(tdl
);
3468 zfs_inode_update(sdzp
);
3470 rw_exit(&sdzp
->z_name_lock
);
3473 zfs_inode_update(tdzp
);
3475 zfs_inode_update(szp
);
3478 zfs_inode_update(tzp
);
3482 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3483 zil_commit(zilog
, 0);
3488 EXPORT_SYMBOL(zfs_rename
);
3491 * Insert the indicated symbolic reference entry into the directory.
3493 * IN: dip - Directory to contain new symbolic link.
3494 * link - Name for new symlink entry.
3495 * vap - Attributes of new entry.
3496 * target - Target path of new symlink.
3498 * cr - credentials of caller.
3499 * flags - case flags
3501 * RETURN: 0 on success, error code on failure.
3504 * dip - ctime|mtime updated
3508 zfs_symlink(struct inode
*dip
, char *name
, vattr_t
*vap
, char *link
,
3509 struct inode
**ipp
, cred_t
*cr
, int flags
)
3511 znode_t
*zp
, *dzp
= ITOZ(dip
);
3514 zfs_sb_t
*zsb
= ITOZSB(dip
);
3516 uint64_t len
= strlen(link
);
3519 zfs_acl_ids_t acl_ids
;
3520 boolean_t fuid_dirtied
;
3521 uint64_t txtype
= TX_SYMLINK
;
3522 boolean_t waited
= B_FALSE
;
3524 ASSERT(S_ISLNK(vap
->va_mode
));
3530 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
3531 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3533 return (SET_ERROR(EILSEQ
));
3535 if (flags
& FIGNORECASE
)
3538 if (len
> MAXPATHLEN
) {
3540 return (SET_ERROR(ENAMETOOLONG
));
3543 if ((error
= zfs_acl_ids_create(dzp
, 0,
3544 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3552 * Attempt to lock directory; fail if entry already exists.
3554 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3556 zfs_acl_ids_free(&acl_ids
);
3561 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3562 zfs_acl_ids_free(&acl_ids
);
3563 zfs_dirent_unlock(dl
);
3568 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
3569 zfs_acl_ids_free(&acl_ids
);
3570 zfs_dirent_unlock(dl
);
3572 return (SET_ERROR(EDQUOT
));
3574 tx
= dmu_tx_create(zsb
->z_os
);
3575 fuid_dirtied
= zsb
->z_fuid_dirty
;
3576 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3577 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3578 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3579 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3580 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3581 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3582 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3583 acl_ids
.z_aclp
->z_acl_bytes
);
3586 zfs_fuid_txhold(zsb
, tx
);
3587 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3589 zfs_dirent_unlock(dl
);
3590 if (error
== ERESTART
) {
3596 zfs_acl_ids_free(&acl_ids
);
3603 * Create a new object for the symlink.
3604 * for version 4 ZPL datsets the symlink will be an SA attribute
3606 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3609 zfs_fuid_sync(zsb
, tx
);
3611 mutex_enter(&zp
->z_lock
);
3613 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zsb
),
3616 zfs_sa_symlink(zp
, link
, len
, tx
);
3617 mutex_exit(&zp
->z_lock
);
3620 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
3621 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3623 * Insert the new object into the directory.
3625 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3627 if (flags
& FIGNORECASE
)
3629 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3631 zfs_inode_update(dzp
);
3632 zfs_inode_update(zp
);
3634 zfs_acl_ids_free(&acl_ids
);
3638 zfs_dirent_unlock(dl
);
3642 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3643 zil_commit(zilog
, 0);
3648 EXPORT_SYMBOL(zfs_symlink
);
3651 * Return, in the buffer contained in the provided uio structure,
3652 * the symbolic path referred to by ip.
3654 * IN: ip - inode of symbolic link
3655 * uio - structure to contain the link path.
3656 * cr - credentials of caller.
3658 * RETURN: 0 if success
3659 * error code if failure
3662 * ip - atime updated
3666 zfs_readlink(struct inode
*ip
, uio_t
*uio
, cred_t
*cr
)
3668 znode_t
*zp
= ITOZ(ip
);
3669 zfs_sb_t
*zsb
= ITOZSB(ip
);
3675 mutex_enter(&zp
->z_lock
);
3677 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3678 SA_ZPL_SYMLINK(zsb
), uio
);
3680 error
= zfs_sa_readlink(zp
, uio
);
3681 mutex_exit(&zp
->z_lock
);
3683 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
3684 zfs_inode_update(zp
);
3688 EXPORT_SYMBOL(zfs_readlink
);
3691 * Insert a new entry into directory tdip referencing sip.
3693 * IN: tdip - Directory to contain new entry.
3694 * sip - inode of new entry.
3695 * name - name of new entry.
3696 * cr - credentials of caller.
3698 * RETURN: 0 if success
3699 * error code if failure
3702 * tdip - ctime|mtime updated
3703 * sip - ctime updated
3707 zfs_link(struct inode
*tdip
, struct inode
*sip
, char *name
, cred_t
*cr
)
3709 znode_t
*dzp
= ITOZ(tdip
);
3711 zfs_sb_t
*zsb
= ITOZSB(tdip
);
3719 boolean_t waited
= B_FALSE
;
3721 ASSERT(S_ISDIR(tdip
->i_mode
));
3728 * POSIX dictates that we return EPERM here.
3729 * Better choices include ENOTSUP or EISDIR.
3731 if (S_ISDIR(sip
->i_mode
)) {
3733 return (SET_ERROR(EPERM
));
3736 if (sip
->i_sb
!= tdip
->i_sb
|| zfsctl_is_node(sip
)) {
3738 return (SET_ERROR(EXDEV
));
3744 /* Prevent links to .zfs/shares files */
3746 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
3747 &parent
, sizeof (uint64_t))) != 0) {
3751 if (parent
== zsb
->z_shares_dir
) {
3753 return (SET_ERROR(EPERM
));
3756 if (zsb
->z_utf8
&& u8_validate(name
,
3757 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3759 return (SET_ERROR(EILSEQ
));
3761 #ifdef HAVE_PN_UTILS
3762 if (flags
& FIGNORECASE
)
3764 #endif /* HAVE_PN_UTILS */
3767 * We do not support links between attributes and non-attributes
3768 * because of the potential security risk of creating links
3769 * into "normal" file space in order to circumvent restrictions
3770 * imposed in attribute space.
3772 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
3774 return (SET_ERROR(EINVAL
));
3777 owner
= zfs_fuid_map_id(zsb
, szp
->z_uid
, cr
, ZFS_OWNER
);
3778 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
3780 return (SET_ERROR(EPERM
));
3783 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3790 * Attempt to lock directory; fail if entry already exists.
3792 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
3798 tx
= dmu_tx_create(zsb
->z_os
);
3799 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3800 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3801 zfs_sa_upgrade_txholds(tx
, szp
);
3802 zfs_sa_upgrade_txholds(tx
, dzp
);
3803 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3805 zfs_dirent_unlock(dl
);
3806 if (error
== ERESTART
) {
3817 error
= zfs_link_create(dl
, szp
, tx
, 0);
3820 uint64_t txtype
= TX_LINK
;
3821 #ifdef HAVE_PN_UTILS
3822 if (flags
& FIGNORECASE
)
3824 #endif /* HAVE_PN_UTILS */
3825 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
3830 zfs_dirent_unlock(dl
);
3832 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3833 zil_commit(zilog
, 0);
3835 zfs_inode_update(dzp
);
3836 zfs_inode_update(szp
);
3840 EXPORT_SYMBOL(zfs_link
);
3843 zfs_putpage_commit_cb(void *arg
)
3845 struct page
*pp
= arg
;
3848 end_page_writeback(pp
);
3852 * Push a page out to disk, once the page is on stable storage the
3853 * registered commit callback will be run as notification of completion.
3855 * IN: ip - page mapped for inode.
3856 * pp - page to push (page is locked)
3857 * wbc - writeback control data
3859 * RETURN: 0 if success
3860 * error code if failure
3863 * ip - ctime|mtime updated
3867 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
3869 znode_t
*zp
= ITOZ(ip
);
3870 zfs_sb_t
*zsb
= ITOZSB(ip
);
3878 uint64_t mtime
[2], ctime
[2];
3879 sa_bulk_attr_t bulk
[3];
3885 ASSERT(PageLocked(pp
));
3887 pgoff
= page_offset(pp
); /* Page byte-offset in file */
3888 offset
= i_size_read(ip
); /* File length in bytes */
3889 pglen
= MIN(PAGE_CACHE_SIZE
, /* Page length in bytes */
3890 P2ROUNDUP(offset
, PAGE_CACHE_SIZE
)-pgoff
);
3892 /* Page is beyond end of file */
3893 if (pgoff
>= offset
) {
3899 /* Truncate page length to end of file */
3900 if (pgoff
+ pglen
> offset
)
3901 pglen
= offset
- pgoff
;
3905 * FIXME: Allow mmap writes past its quota. The correct fix
3906 * is to register a page_mkwrite() handler to count the page
3907 * against its quota when it is about to be dirtied.
3909 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
3910 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
3915 set_page_writeback(pp
);
3918 rl
= zfs_range_lock(zp
, pgoff
, pglen
, RL_WRITER
);
3919 tx
= dmu_tx_create(zsb
->z_os
);
3921 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
3923 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3924 zfs_sa_upgrade_txholds(tx
, zp
);
3925 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3927 if (err
== ERESTART
)
3931 __set_page_dirty_nobuffers(pp
);
3933 end_page_writeback(pp
);
3934 zfs_range_unlock(rl
);
3940 ASSERT3U(pglen
, <=, PAGE_CACHE_SIZE
);
3941 dmu_write(zsb
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
3944 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
3945 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
3946 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zsb
), NULL
, &zp
->z_pflags
, 8);
3948 /* Preserve the mtime and ctime provided by the inode */
3949 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
3950 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
3951 zp
->z_atime_dirty
= 0;
3954 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
3956 zfs_log_write(zsb
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
3957 zfs_putpage_commit_cb
, pp
);
3960 zfs_range_unlock(rl
);
3962 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3964 * Note that this is rarely called under writepages(), because
3965 * writepages() normally handles the entire commit for
3966 * performance reasons.
3968 zil_commit(zsb
->z_log
, zp
->z_id
);
3976 * Update the system attributes when the inode has been dirtied. For the
3977 * moment we only update the mode, atime, mtime, and ctime.
3980 zfs_dirty_inode(struct inode
*ip
, int flags
)
3982 znode_t
*zp
= ITOZ(ip
);
3983 zfs_sb_t
*zsb
= ITOZSB(ip
);
3985 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
3986 sa_bulk_attr_t bulk
[4];
3993 tx
= dmu_tx_create(zsb
->z_os
);
3995 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3996 zfs_sa_upgrade_txholds(tx
, zp
);
3998 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4004 mutex_enter(&zp
->z_lock
);
4005 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zsb
), NULL
, &mode
, 8);
4006 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zsb
), NULL
, &atime
, 16);
4007 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
4008 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
4010 /* Preserve the mode, mtime and ctime provided by the inode */
4011 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4012 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4013 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4017 zp
->z_atime_dirty
= 0;
4019 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4020 mutex_exit(&zp
->z_lock
);
4027 EXPORT_SYMBOL(zfs_dirty_inode
);
4031 zfs_inactive(struct inode
*ip
)
4033 znode_t
*zp
= ITOZ(ip
);
4034 zfs_sb_t
*zsb
= ITOZSB(ip
);
4037 if (zfsctl_is_node(ip
)) {
4038 zfsctl_inode_inactive(ip
);
4042 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_READER
);
4043 if (zp
->z_sa_hdl
== NULL
) {
4044 rw_exit(&zsb
->z_teardown_inactive_lock
);
4048 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4049 dmu_tx_t
*tx
= dmu_tx_create(zsb
->z_os
);
4051 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4052 zfs_sa_upgrade_txholds(tx
, zp
);
4053 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4057 mutex_enter(&zp
->z_lock
);
4058 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
),
4059 (void *)&zp
->z_atime
, sizeof (zp
->z_atime
), tx
);
4060 zp
->z_atime_dirty
= 0;
4061 mutex_exit(&zp
->z_lock
);
4067 rw_exit(&zsb
->z_teardown_inactive_lock
);
4069 EXPORT_SYMBOL(zfs_inactive
);
4072 * Bounds-check the seek operation.
4074 * IN: ip - inode seeking within
4075 * ooff - old file offset
4076 * noffp - pointer to new file offset
4077 * ct - caller context
4079 * RETURN: 0 if success
4080 * EINVAL if new offset invalid
4084 zfs_seek(struct inode
*ip
, offset_t ooff
, offset_t
*noffp
)
4086 if (S_ISDIR(ip
->i_mode
))
4088 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4090 EXPORT_SYMBOL(zfs_seek
);
4093 * Fill pages with data from the disk.
4096 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4098 znode_t
*zp
= ITOZ(ip
);
4099 zfs_sb_t
*zsb
= ITOZSB(ip
);
4101 struct page
*cur_pp
;
4102 u_offset_t io_off
, total
;
4109 io_len
= nr_pages
<< PAGE_CACHE_SHIFT
;
4110 i_size
= i_size_read(ip
);
4111 io_off
= page_offset(pl
[0]);
4113 if (io_off
+ io_len
> i_size
)
4114 io_len
= i_size
- io_off
;
4117 * Iterate over list of pages and read each page individually.
4121 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4125 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4129 /* convert checksum errors into IO errors */
4131 err
= SET_ERROR(EIO
);
4134 cur_pp
= pl
[++page_idx
];
4141 * Uses zfs_fillpage to read data from the file and fill the pages.
4143 * IN: ip - inode of file to get data from.
4144 * pl - list of pages to read
4145 * nr_pages - number of pages to read
4147 * RETURN: 0 on success, error code on failure.
4150 * vp - atime updated
4154 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4156 znode_t
*zp
= ITOZ(ip
);
4157 zfs_sb_t
*zsb
= ITOZSB(ip
);
4166 err
= zfs_fillpage(ip
, pl
, nr_pages
);
4169 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
4174 EXPORT_SYMBOL(zfs_getpage
);
4177 * Check ZFS specific permissions to memory map a section of a file.
4179 * IN: ip - inode of the file to mmap
4181 * addrp - start address in memory region
4182 * len - length of memory region
4183 * vm_flags- address flags
4185 * RETURN: 0 if success
4186 * error code if failure
4190 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
4191 unsigned long vm_flags
)
4193 znode_t
*zp
= ITOZ(ip
);
4194 zfs_sb_t
*zsb
= ITOZSB(ip
);
4199 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
4200 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4202 return (SET_ERROR(EPERM
));
4205 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
4206 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4208 return (SET_ERROR(EACCES
));
4211 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4213 return (SET_ERROR(ENXIO
));
4219 EXPORT_SYMBOL(zfs_map
);
4222 * convoff - converts the given data (start, whence) to the
4226 convoff(struct inode
*ip
, flock64_t
*lckdat
, int whence
, offset_t offset
)
4231 if ((lckdat
->l_whence
== 2) || (whence
== 2)) {
4232 if ((error
= zfs_getattr(ip
, &vap
, 0, CRED()) != 0))
4236 switch (lckdat
->l_whence
) {
4238 lckdat
->l_start
+= offset
;
4241 lckdat
->l_start
+= vap
.va_size
;
4246 return (SET_ERROR(EINVAL
));
4249 if (lckdat
->l_start
< 0)
4250 return (SET_ERROR(EINVAL
));
4254 lckdat
->l_start
-= offset
;
4257 lckdat
->l_start
-= vap
.va_size
;
4262 return (SET_ERROR(EINVAL
));
4265 lckdat
->l_whence
= (short)whence
;
4270 * Free or allocate space in a file. Currently, this function only
4271 * supports the `F_FREESP' command. However, this command is somewhat
4272 * misnamed, as its functionality includes the ability to allocate as
4273 * well as free space.
4275 * IN: ip - inode of file to free data in.
4276 * cmd - action to take (only F_FREESP supported).
4277 * bfp - section of file to free/alloc.
4278 * flag - current file open mode flags.
4279 * offset - current file offset.
4280 * cr - credentials of caller [UNUSED].
4282 * RETURN: 0 on success, error code on failure.
4285 * ip - ctime|mtime updated
4289 zfs_space(struct inode
*ip
, int cmd
, flock64_t
*bfp
, int flag
,
4290 offset_t offset
, cred_t
*cr
)
4292 znode_t
*zp
= ITOZ(ip
);
4293 zfs_sb_t
*zsb
= ITOZSB(ip
);
4300 if (cmd
!= F_FREESP
) {
4302 return (SET_ERROR(EINVAL
));
4305 if ((error
= convoff(ip
, bfp
, 0, offset
))) {
4310 if (bfp
->l_len
< 0) {
4312 return (SET_ERROR(EINVAL
));
4316 * Permissions aren't checked on Solaris because on this OS
4317 * zfs_space() can only be called with an opened file handle.
4318 * On Linux we can get here through truncate_range() which
4319 * operates directly on inodes, so we need to check access rights.
4321 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
4327 len
= bfp
->l_len
; /* 0 means from off to end of file */
4329 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4334 EXPORT_SYMBOL(zfs_space
);
4338 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4340 znode_t
*zp
= ITOZ(ip
);
4341 zfs_sb_t
*zsb
= ITOZSB(ip
);
4344 uint64_t object
= zp
->z_id
;
4351 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
),
4352 &gen64
, sizeof (uint64_t))) != 0) {
4357 gen
= (uint32_t)gen64
;
4359 size
= (zsb
->z_parent
!= zsb
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4360 if (fidp
->fid_len
< size
) {
4361 fidp
->fid_len
= size
;
4363 return (SET_ERROR(ENOSPC
));
4366 zfid
= (zfid_short_t
*)fidp
;
4368 zfid
->zf_len
= size
;
4370 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4371 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4373 /* Must have a non-zero generation number to distinguish from .zfs */
4376 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4377 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4379 if (size
== LONG_FID_LEN
) {
4380 uint64_t objsetid
= dmu_objset_id(zsb
->z_os
);
4383 zlfid
= (zfid_long_t
*)fidp
;
4385 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4386 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4388 /* XXX - this should be the generation number for the objset */
4389 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4390 zlfid
->zf_setgen
[i
] = 0;
4396 EXPORT_SYMBOL(zfs_fid
);
4400 zfs_getsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4402 znode_t
*zp
= ITOZ(ip
);
4403 zfs_sb_t
*zsb
= ITOZSB(ip
);
4405 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4409 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
4414 EXPORT_SYMBOL(zfs_getsecattr
);
4418 zfs_setsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4420 znode_t
*zp
= ITOZ(ip
);
4421 zfs_sb_t
*zsb
= ITOZSB(ip
);
4423 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4424 zilog_t
*zilog
= zsb
->z_log
;
4429 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
4431 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4432 zil_commit(zilog
, 0);
4437 EXPORT_SYMBOL(zfs_setsecattr
);
4439 #ifdef HAVE_UIO_ZEROCOPY
4441 * Tunable, both must be a power of 2.
4443 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4444 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4445 * an arcbuf for a partial block read
4447 int zcr_blksz_min
= (1 << 10); /* 1K */
4448 int zcr_blksz_max
= (1 << 17); /* 128K */
4452 zfs_reqzcbuf(struct inode
*ip
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
)
4454 znode_t
*zp
= ITOZ(ip
);
4455 zfs_sb_t
*zsb
= ITOZSB(ip
);
4456 int max_blksz
= zsb
->z_max_blksz
;
4457 uio_t
*uio
= &xuio
->xu_uio
;
4458 ssize_t size
= uio
->uio_resid
;
4459 offset_t offset
= uio
->uio_loffset
;
4464 int preamble
, postamble
;
4466 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
4467 return (SET_ERROR(EINVAL
));
4474 * Loan out an arc_buf for write if write size is bigger than
4475 * max_blksz, and the file's block size is also max_blksz.
4478 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
4480 return (SET_ERROR(EINVAL
));
4483 * Caller requests buffers for write before knowing where the
4484 * write offset might be (e.g. NFS TCP write).
4489 preamble
= P2PHASE(offset
, blksz
);
4491 preamble
= blksz
- preamble
;
4496 postamble
= P2PHASE(size
, blksz
);
4499 fullblk
= size
/ blksz
;
4500 (void) dmu_xuio_init(xuio
,
4501 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
4504 * Have to fix iov base/len for partial buffers. They
4505 * currently represent full arc_buf's.
4508 /* data begins in the middle of the arc_buf */
4509 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4512 (void) dmu_xuio_add(xuio
, abuf
,
4513 blksz
- preamble
, preamble
);
4516 for (i
= 0; i
< fullblk
; i
++) {
4517 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4520 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
4524 /* data ends in the middle of the arc_buf */
4525 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4528 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
4533 * Loan out an arc_buf for read if the read size is larger than
4534 * the current file block size. Block alignment is not
4535 * considered. Partial arc_buf will be loaned out for read.
4537 blksz
= zp
->z_blksz
;
4538 if (blksz
< zcr_blksz_min
)
4539 blksz
= zcr_blksz_min
;
4540 if (blksz
> zcr_blksz_max
)
4541 blksz
= zcr_blksz_max
;
4542 /* avoid potential complexity of dealing with it */
4543 if (blksz
> max_blksz
) {
4545 return (SET_ERROR(EINVAL
));
4548 maxsize
= zp
->z_size
- uio
->uio_loffset
;
4554 return (SET_ERROR(EINVAL
));
4559 return (SET_ERROR(EINVAL
));
4562 uio
->uio_extflg
= UIO_XUIO
;
4563 XUIO_XUZC_RW(xuio
) = ioflag
;
4570 zfs_retzcbuf(struct inode
*ip
, xuio_t
*xuio
, cred_t
*cr
)
4574 int ioflag
= XUIO_XUZC_RW(xuio
);
4576 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
4578 i
= dmu_xuio_cnt(xuio
);
4580 abuf
= dmu_xuio_arcbuf(xuio
, i
);
4582 * if abuf == NULL, it must be a write buffer
4583 * that has been returned in zfs_write().
4586 dmu_return_arcbuf(abuf
);
4587 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
4590 dmu_xuio_fini(xuio
);
4593 #endif /* HAVE_UIO_ZEROCOPY */
4595 #if defined(_KERNEL) && defined(HAVE_SPL)
4596 module_param(zfs_read_chunk_size
, long, 0644);
4597 MODULE_PARM_DESC(zfs_read_chunk_size
, "Bytes to read per chunk");