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 zfs_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
);
238 /* Decrement the synchronous opens in the znode */
240 atomic_dec_32(&zp
->z_sync_cnt
);
242 if (!zfs_has_ctldir(zp
) && zsb
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
243 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0)
244 VERIFY(zfs_vscan(ip
, cr
, 1) == 0);
249 EXPORT_SYMBOL(zfs_close
);
251 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
253 * Lseek support for finding holes (cmd == SEEK_HOLE) and
254 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
257 zfs_holey_common(struct inode
*ip
, int cmd
, loff_t
*off
)
259 znode_t
*zp
= ITOZ(ip
);
260 uint64_t noff
= (uint64_t)*off
; /* new offset */
265 file_sz
= zp
->z_size
;
266 if (noff
>= file_sz
) {
267 return (SET_ERROR(ENXIO
));
270 if (cmd
== SEEK_HOLE
)
275 error
= dmu_offset_next(ZTOZSB(zp
)->z_os
, zp
->z_id
, hole
, &noff
);
278 return (SET_ERROR(ENXIO
));
281 * We could find a hole that begins after the logical end-of-file,
282 * because dmu_offset_next() only works on whole blocks. If the
283 * EOF falls mid-block, then indicate that the "virtual hole"
284 * at the end of the file begins at the logical EOF, rather than
285 * at the end of the last block.
287 if (noff
> file_sz
) {
299 zfs_holey(struct inode
*ip
, int cmd
, loff_t
*off
)
301 znode_t
*zp
= ITOZ(ip
);
302 zfs_sb_t
*zsb
= ITOZSB(ip
);
308 error
= zfs_holey_common(ip
, cmd
, off
);
313 EXPORT_SYMBOL(zfs_holey
);
314 #endif /* SEEK_HOLE && SEEK_DATA */
318 * When a file is memory mapped, we must keep the IO data synchronized
319 * between the DMU cache and the memory mapped pages. What this means:
321 * On Write: If we find a memory mapped page, we write to *both*
322 * the page and the dmu buffer.
325 update_pages(struct inode
*ip
, int64_t start
, int len
,
326 objset_t
*os
, uint64_t oid
)
328 struct address_space
*mp
= ip
->i_mapping
;
334 off
= start
& (PAGE_CACHE_SIZE
-1);
335 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
336 nbytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
338 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_SHIFT
);
340 if (mapping_writably_mapped(mp
))
341 flush_dcache_page(pp
);
344 (void) dmu_read(os
, oid
, start
+off
, nbytes
, pb
+off
,
348 if (mapping_writably_mapped(mp
))
349 flush_dcache_page(pp
);
351 mark_page_accessed(pp
);
355 page_cache_release(pp
);
364 * When a file is memory mapped, we must keep the IO data synchronized
365 * between the DMU cache and the memory mapped pages. What this means:
367 * On Read: We "read" preferentially from memory mapped pages,
368 * else we default from the dmu buffer.
370 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
371 * the file is memory mapped.
374 mappedread(struct inode
*ip
, int nbytes
, uio_t
*uio
)
376 struct address_space
*mp
= ip
->i_mapping
;
378 znode_t
*zp
= ITOZ(ip
);
379 objset_t
*os
= ITOZSB(ip
)->z_os
;
386 start
= uio
->uio_loffset
;
387 off
= start
& (PAGE_CACHE_SIZE
-1);
388 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
389 bytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
391 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_SHIFT
);
393 ASSERT(PageUptodate(pp
));
396 error
= uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
399 if (mapping_writably_mapped(mp
))
400 flush_dcache_page(pp
);
402 mark_page_accessed(pp
);
404 page_cache_release(pp
);
406 error
= dmu_read_uio(os
, zp
->z_id
, uio
, bytes
);
418 unsigned long zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
421 * Read bytes from specified file into supplied buffer.
423 * IN: ip - inode of file to be read from.
424 * uio - structure supplying read location, range info,
426 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
427 * O_DIRECT flag; used to bypass page cache.
428 * cr - credentials of caller.
430 * OUT: uio - updated offset and range, buffer filled.
432 * RETURN: 0 on success, error code on failure.
435 * inode - atime updated if byte count > 0
439 zfs_read(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
441 znode_t
*zp
= ITOZ(ip
);
442 zfs_sb_t
*zsb
= ITOZSB(ip
);
447 #ifdef HAVE_UIO_ZEROCOPY
449 #endif /* HAVE_UIO_ZEROCOPY */
455 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
457 return (SET_ERROR(EACCES
));
461 * Validate file offset
463 if (uio
->uio_loffset
< (offset_t
)0) {
465 return (SET_ERROR(EINVAL
));
469 * Fasttrack empty reads
471 if (uio
->uio_resid
== 0) {
477 * If we're in FRSYNC mode, sync out this znode before reading it.
479 if (ioflag
& FRSYNC
|| zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
480 zil_commit(zsb
->z_log
, zp
->z_id
);
483 * Lock the range against changes.
485 rl
= zfs_range_lock(zp
, uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
488 * If we are reading past end-of-file we can skip
489 * to the end; but we might still need to set atime.
491 if (uio
->uio_loffset
>= zp
->z_size
) {
496 ASSERT(uio
->uio_loffset
< zp
->z_size
);
497 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
499 #ifdef HAVE_UIO_ZEROCOPY
500 if ((uio
->uio_extflg
== UIO_XUIO
) &&
501 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
503 int blksz
= zp
->z_blksz
;
504 uint64_t offset
= uio
->uio_loffset
;
506 xuio
= (xuio_t
*)uio
;
508 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
511 ASSERT(offset
+ n
<= blksz
);
514 (void) dmu_xuio_init(xuio
, nblk
);
516 if (vn_has_cached_data(ip
)) {
518 * For simplicity, we always allocate a full buffer
519 * even if we only expect to read a portion of a block.
521 while (--nblk
>= 0) {
522 (void) dmu_xuio_add(xuio
,
523 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
528 #endif /* HAVE_UIO_ZEROCOPY */
531 nbytes
= MIN(n
, zfs_read_chunk_size
-
532 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
534 if (zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
535 error
= mappedread(ip
, nbytes
, uio
);
537 error
= dmu_read_uio(os
, zp
->z_id
, uio
, nbytes
);
540 /* convert checksum errors into IO errors */
542 error
= SET_ERROR(EIO
);
549 zfs_range_unlock(rl
);
551 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
555 EXPORT_SYMBOL(zfs_read
);
558 * Write the bytes to a file.
560 * IN: ip - inode of file to be written to.
561 * uio - structure supplying write location, range info,
563 * ioflag - FAPPEND flag set if in append mode.
564 * O_DIRECT flag; used to bypass page cache.
565 * cr - credentials of caller.
567 * OUT: uio - updated offset and range.
569 * RETURN: 0 if success
570 * error code if failure
573 * ip - ctime|mtime updated if byte count > 0
578 zfs_write(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
580 znode_t
*zp
= ITOZ(ip
);
581 rlim64_t limit
= uio
->uio_limit
;
582 ssize_t start_resid
= uio
->uio_resid
;
586 zfs_sb_t
*zsb
= ZTOZSB(zp
);
591 int max_blksz
= zsb
->z_max_blksz
;
594 iovec_t
*aiov
= NULL
;
597 iovec_t
*iovp
= uio
->uio_iov
;
600 sa_bulk_attr_t bulk
[4];
601 uint64_t mtime
[2], ctime
[2];
602 ASSERTV(int iovcnt
= uio
->uio_iovcnt
);
605 * Fasttrack empty write
611 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
617 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
618 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
619 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
, &zp
->z_size
, 8);
620 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
624 * If immutable or not appending then return EPERM
626 if ((zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
627 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
628 (uio
->uio_loffset
< zp
->z_size
))) {
630 return (SET_ERROR(EPERM
));
636 * Validate file offset
638 woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
641 return (SET_ERROR(EINVAL
));
645 * Pre-fault the pages to ensure slow (eg NFS) pages
647 * Skip this if uio contains loaned arc_buf.
649 #ifdef HAVE_UIO_ZEROCOPY
650 if ((uio
->uio_extflg
== UIO_XUIO
) &&
651 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
652 xuio
= (xuio_t
*)uio
;
655 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
658 * If in append mode, set the io offset pointer to eof.
660 if (ioflag
& FAPPEND
) {
662 * Obtain an appending range lock to guarantee file append
663 * semantics. We reset the write offset once we have the lock.
665 rl
= zfs_range_lock(zp
, 0, n
, RL_APPEND
);
667 if (rl
->r_len
== UINT64_MAX
) {
669 * We overlocked the file because this write will cause
670 * the file block size to increase.
671 * Note that zp_size cannot change with this lock held.
675 uio
->uio_loffset
= woff
;
678 * Note that if the file block size will change as a result of
679 * this write, then this range lock will lock the entire file
680 * so that we can re-write the block safely.
682 rl
= zfs_range_lock(zp
, woff
, n
, RL_WRITER
);
686 zfs_range_unlock(rl
);
688 return (SET_ERROR(EFBIG
));
691 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
694 /* Will this write extend the file length? */
695 write_eof
= (woff
+ n
> zp
->z_size
);
697 end_size
= MAX(zp
->z_size
, woff
+ n
);
700 * Write the file in reasonable size chunks. Each chunk is written
701 * in a separate transaction; this keeps the intent log records small
702 * and allows us to do more fine-grained space accounting.
706 woff
= uio
->uio_loffset
;
707 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
708 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
710 dmu_return_arcbuf(abuf
);
711 error
= SET_ERROR(EDQUOT
);
715 if (xuio
&& abuf
== NULL
) {
716 ASSERT(i_iov
< iovcnt
);
718 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
719 dmu_xuio_clear(xuio
, i_iov
);
720 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
721 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
722 aiov
->iov_len
== arc_buf_size(abuf
)));
724 } else if (abuf
== NULL
&& n
>= max_blksz
&&
725 woff
>= zp
->z_size
&&
726 P2PHASE(woff
, max_blksz
) == 0 &&
727 zp
->z_blksz
== max_blksz
) {
729 * This write covers a full block. "Borrow" a buffer
730 * from the dmu so that we can fill it before we enter
731 * a transaction. This avoids the possibility of
732 * holding up the transaction if the data copy hangs
733 * up on a pagefault (e.g., from an NFS server mapping).
737 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
739 ASSERT(abuf
!= NULL
);
740 ASSERT(arc_buf_size(abuf
) == max_blksz
);
741 if ((error
= uiocopy(abuf
->b_data
, max_blksz
,
742 UIO_WRITE
, uio
, &cbytes
))) {
743 dmu_return_arcbuf(abuf
);
746 ASSERT(cbytes
== max_blksz
);
750 * Start a transaction.
752 tx
= dmu_tx_create(zsb
->z_os
);
753 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
754 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
755 zfs_sa_upgrade_txholds(tx
, zp
);
756 error
= dmu_tx_assign(tx
, TXG_WAIT
);
760 dmu_return_arcbuf(abuf
);
765 * If zfs_range_lock() over-locked we grow the blocksize
766 * and then reduce the lock range. This will only happen
767 * on the first iteration since zfs_range_reduce() will
768 * shrink down r_len to the appropriate size.
770 if (rl
->r_len
== UINT64_MAX
) {
773 if (zp
->z_blksz
> max_blksz
) {
774 ASSERT(!ISP2(zp
->z_blksz
));
775 new_blksz
= MIN(end_size
, SPA_MAXBLOCKSIZE
);
777 new_blksz
= MIN(end_size
, max_blksz
);
779 zfs_grow_blocksize(zp
, new_blksz
, tx
);
780 zfs_range_reduce(rl
, woff
, n
);
784 * XXX - should we really limit each write to z_max_blksz?
785 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
787 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
790 tx_bytes
= uio
->uio_resid
;
791 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
793 tx_bytes
-= uio
->uio_resid
;
796 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
798 * If this is not a full block write, but we are
799 * extending the file past EOF and this data starts
800 * block-aligned, use assign_arcbuf(). Otherwise,
801 * write via dmu_write().
803 if (tx_bytes
< max_blksz
&& (!write_eof
||
804 aiov
->iov_base
!= abuf
->b_data
)) {
806 dmu_write(zsb
->z_os
, zp
->z_id
, woff
,
807 aiov
->iov_len
, aiov
->iov_base
, tx
);
808 dmu_return_arcbuf(abuf
);
809 xuio_stat_wbuf_copied();
811 ASSERT(xuio
|| tx_bytes
== max_blksz
);
812 dmu_assign_arcbuf(sa_get_db(zp
->z_sa_hdl
),
815 ASSERT(tx_bytes
<= uio
->uio_resid
);
816 uioskip(uio
, tx_bytes
);
819 if (tx_bytes
&& zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
820 update_pages(ip
, woff
, tx_bytes
, zsb
->z_os
, zp
->z_id
);
823 * If we made no progress, we're done. If we made even
824 * partial progress, update the znode and ZIL accordingly.
827 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
828 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
835 * Clear Set-UID/Set-GID bits on successful write if not
836 * privileged and at least one of the excute bits is set.
838 * It would be nice to to this after all writes have
839 * been done, but that would still expose the ISUID/ISGID
840 * to another app after the partial write is committed.
842 * Note: we don't call zfs_fuid_map_id() here because
843 * user 0 is not an ephemeral uid.
845 mutex_enter(&zp
->z_acl_lock
);
846 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
847 (S_IXUSR
>> 6))) != 0 &&
848 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
849 secpolicy_vnode_setid_retain(cr
,
850 (zp
->z_mode
& S_ISUID
) != 0 && zp
->z_uid
== 0) != 0) {
852 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
853 newmode
= zp
->z_mode
;
854 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
),
855 (void *)&newmode
, sizeof (uint64_t), tx
);
857 mutex_exit(&zp
->z_acl_lock
);
859 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
863 * Update the file size (zp_size) if it has changed;
864 * account for possible concurrent updates.
866 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
867 (void) atomic_cas_64(&zp
->z_size
, end_size
,
872 * If we are replaying and eof is non zero then force
873 * the file size to the specified eof. Note, there's no
874 * concurrency during replay.
876 if (zsb
->z_replay
&& zsb
->z_replay_eof
!= 0)
877 zp
->z_size
= zsb
->z_replay_eof
;
879 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
881 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
,
887 ASSERT(tx_bytes
== nbytes
);
891 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
894 zfs_range_unlock(rl
);
897 * If we're in replay mode, or we made no progress, return error.
898 * Otherwise, it's at least a partial write, so it's successful.
900 if (zsb
->z_replay
|| uio
->uio_resid
== start_resid
) {
905 if (ioflag
& (FSYNC
| FDSYNC
) ||
906 zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
907 zil_commit(zilog
, zp
->z_id
);
909 zfs_inode_update(zp
);
913 EXPORT_SYMBOL(zfs_write
);
916 zfs_iput_async(struct inode
*ip
)
918 objset_t
*os
= ITOZSB(ip
)->z_os
;
920 ASSERT(atomic_read(&ip
->i_count
) > 0);
923 if (atomic_read(&ip
->i_count
) == 1)
924 taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os
)),
925 (task_func_t
*)iput
, ip
, TQ_PUSHPAGE
);
931 zfs_get_done(zgd_t
*zgd
, int error
)
933 znode_t
*zp
= zgd
->zgd_private
;
936 dmu_buf_rele(zgd
->zgd_db
, zgd
);
938 zfs_range_unlock(zgd
->zgd_rl
);
941 * Release the vnode asynchronously as we currently have the
942 * txg stopped from syncing.
944 zfs_iput_async(ZTOI(zp
));
946 if (error
== 0 && zgd
->zgd_bp
)
947 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
949 kmem_free(zgd
, sizeof (zgd_t
));
953 static int zil_fault_io
= 0;
957 * Get data to generate a TX_WRITE intent log record.
960 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
963 objset_t
*os
= zsb
->z_os
;
965 uint64_t object
= lr
->lr_foid
;
966 uint64_t offset
= lr
->lr_offset
;
967 uint64_t size
= lr
->lr_length
;
968 blkptr_t
*bp
= &lr
->lr_blkptr
;
977 * Nothing to do if the file has been removed
979 if (zfs_zget(zsb
, object
, &zp
) != 0)
980 return (SET_ERROR(ENOENT
));
981 if (zp
->z_unlinked
) {
983 * Release the vnode asynchronously as we currently have the
984 * txg stopped from syncing.
986 zfs_iput_async(ZTOI(zp
));
987 return (SET_ERROR(ENOENT
));
990 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_PUSHPAGE
);
991 zgd
->zgd_zilog
= zsb
->z_log
;
992 zgd
->zgd_private
= zp
;
995 * Write records come in two flavors: immediate and indirect.
996 * For small writes it's cheaper to store the data with the
997 * log record (immediate); for large writes it's cheaper to
998 * sync the data and get a pointer to it (indirect) so that
999 * we don't have to write the data twice.
1001 if (buf
!= NULL
) { /* immediate write */
1002 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
, RL_READER
);
1003 /* test for truncation needs to be done while range locked */
1004 if (offset
>= zp
->z_size
) {
1005 error
= SET_ERROR(ENOENT
);
1007 error
= dmu_read(os
, object
, offset
, size
, buf
,
1008 DMU_READ_NO_PREFETCH
);
1010 ASSERT(error
== 0 || error
== ENOENT
);
1011 } else { /* indirect write */
1013 * Have to lock the whole block to ensure when it's
1014 * written out and it's checksum is being calculated
1015 * that no one can change the data. We need to re-check
1016 * blocksize after we get the lock in case it's changed!
1021 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
1023 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
,
1025 if (zp
->z_blksz
== size
)
1028 zfs_range_unlock(zgd
->zgd_rl
);
1030 /* test for truncation needs to be done while range locked */
1031 if (lr
->lr_offset
>= zp
->z_size
)
1032 error
= SET_ERROR(ENOENT
);
1035 error
= SET_ERROR(EIO
);
1040 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1041 DMU_READ_NO_PREFETCH
);
1044 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
1046 ASSERT(BP_IS_HOLE(bp
));
1053 ASSERT(db
->db_offset
== offset
);
1054 ASSERT(db
->db_size
== size
);
1056 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1058 ASSERT(error
|| lr
->lr_length
<= zp
->z_blksz
);
1061 * On success, we need to wait for the write I/O
1062 * initiated by dmu_sync() to complete before we can
1063 * release this dbuf. We will finish everything up
1064 * in the zfs_get_done() callback.
1069 if (error
== EALREADY
) {
1070 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
1076 zfs_get_done(zgd
, error
);
1083 zfs_access(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
1085 znode_t
*zp
= ITOZ(ip
);
1086 zfs_sb_t
*zsb
= ITOZSB(ip
);
1092 if (flag
& V_ACE_MASK
)
1093 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
1095 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
1100 EXPORT_SYMBOL(zfs_access
);
1103 * Lookup an entry in a directory, or an extended attribute directory.
1104 * If it exists, return a held inode reference for it.
1106 * IN: dip - inode of directory to search.
1107 * nm - name of entry to lookup.
1108 * flags - LOOKUP_XATTR set if looking for an attribute.
1109 * cr - credentials of caller.
1110 * direntflags - directory lookup flags
1111 * realpnp - returned pathname.
1113 * OUT: ipp - inode of located entry, NULL if not found.
1115 * RETURN: 0 on success, error code on failure.
1122 zfs_lookup(struct inode
*dip
, char *nm
, struct inode
**ipp
, int flags
,
1123 cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
1125 znode_t
*zdp
= ITOZ(dip
);
1126 zfs_sb_t
*zsb
= ITOZSB(dip
);
1130 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1132 if (!S_ISDIR(dip
->i_mode
)) {
1133 return (SET_ERROR(ENOTDIR
));
1134 } else if (zdp
->z_sa_hdl
== NULL
) {
1135 return (SET_ERROR(EIO
));
1138 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1139 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1148 vnode_t
*tvp
= dnlc_lookup(dvp
, nm
);
1151 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1156 if (tvp
== DNLC_NO_VNODE
) {
1158 return (SET_ERROR(ENOENT
));
1161 return (specvp_check(vpp
, cr
));
1164 #endif /* HAVE_DNLC */
1173 if (flags
& LOOKUP_XATTR
) {
1175 * We don't allow recursive attributes..
1176 * Maybe someday we will.
1178 if (zdp
->z_pflags
& ZFS_XATTR
) {
1180 return (SET_ERROR(EINVAL
));
1183 if ((error
= zfs_get_xattrdir(zdp
, ipp
, cr
, flags
))) {
1189 * Do we have permission to get into attribute directory?
1192 if ((error
= zfs_zaccess(ITOZ(*ipp
), ACE_EXECUTE
, 0,
1202 if (!S_ISDIR(dip
->i_mode
)) {
1204 return (SET_ERROR(ENOTDIR
));
1208 * Check accessibility of directory.
1211 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
))) {
1216 if (zsb
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1217 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1219 return (SET_ERROR(EILSEQ
));
1222 error
= zfs_dirlook(zdp
, nm
, ipp
, flags
, direntflags
, realpnp
);
1223 if ((error
== 0) && (*ipp
))
1224 zfs_inode_update(ITOZ(*ipp
));
1229 EXPORT_SYMBOL(zfs_lookup
);
1232 * Attempt to create a new entry in a directory. If the entry
1233 * already exists, truncate the file if permissible, else return
1234 * an error. Return the ip of the created or trunc'd file.
1236 * IN: dip - inode of directory to put new file entry in.
1237 * name - name of new file entry.
1238 * vap - attributes of new file.
1239 * excl - flag indicating exclusive or non-exclusive mode.
1240 * mode - mode to open file with.
1241 * cr - credentials of caller.
1242 * flag - large file flag [UNUSED].
1243 * vsecp - ACL to be set
1245 * OUT: ipp - inode of created or trunc'd entry.
1247 * RETURN: 0 on success, error code on failure.
1250 * dip - ctime|mtime updated if new entry created
1251 * ip - ctime|mtime always, atime if new
1256 zfs_create(struct inode
*dip
, char *name
, vattr_t
*vap
, int excl
,
1257 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
1259 znode_t
*zp
, *dzp
= ITOZ(dip
);
1260 zfs_sb_t
*zsb
= ITOZSB(dip
);
1268 zfs_acl_ids_t acl_ids
;
1269 boolean_t fuid_dirtied
;
1270 boolean_t have_acl
= B_FALSE
;
1271 boolean_t waited
= B_FALSE
;
1274 * If we have an ephemeral id, ACL, or XVATTR then
1275 * make sure file system is at proper version
1281 if (zsb
->z_use_fuids
== B_FALSE
&&
1282 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1283 return (SET_ERROR(EINVAL
));
1290 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
1291 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1293 return (SET_ERROR(EILSEQ
));
1296 if (vap
->va_mask
& ATTR_XVATTR
) {
1297 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1298 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1306 if (*name
== '\0') {
1308 * Null component name refers to the directory itself.
1315 /* possible igrab(zp) */
1318 if (flag
& FIGNORECASE
)
1321 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1325 zfs_acl_ids_free(&acl_ids
);
1326 if (strcmp(name
, "..") == 0)
1327 error
= SET_ERROR(EISDIR
);
1337 * Create a new file object and update the directory
1340 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
1342 zfs_acl_ids_free(&acl_ids
);
1347 * We only support the creation of regular files in
1348 * extended attribute directories.
1351 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
1353 zfs_acl_ids_free(&acl_ids
);
1354 error
= SET_ERROR(EINVAL
);
1358 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1359 cr
, vsecp
, &acl_ids
)) != 0)
1363 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1364 zfs_acl_ids_free(&acl_ids
);
1365 error
= SET_ERROR(EDQUOT
);
1369 tx
= dmu_tx_create(os
);
1371 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1372 ZFS_SA_BASE_ATTR_SIZE
);
1374 fuid_dirtied
= zsb
->z_fuid_dirty
;
1376 zfs_fuid_txhold(zsb
, tx
);
1377 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1378 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1379 if (!zsb
->z_use_sa
&&
1380 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1381 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1382 0, acl_ids
.z_aclp
->z_acl_bytes
);
1384 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1386 zfs_dirent_unlock(dl
);
1387 if (error
== ERESTART
) {
1393 zfs_acl_ids_free(&acl_ids
);
1398 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1401 zfs_fuid_sync(zsb
, tx
);
1403 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1404 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1405 if (flag
& FIGNORECASE
)
1407 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1408 vsecp
, acl_ids
.z_fuidp
, vap
);
1409 zfs_acl_ids_free(&acl_ids
);
1412 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1415 zfs_acl_ids_free(&acl_ids
);
1419 * A directory entry already exists for this name.
1422 * Can't truncate an existing file if in exclusive mode.
1425 error
= SET_ERROR(EEXIST
);
1429 * Can't open a directory for writing.
1431 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
1432 error
= SET_ERROR(EISDIR
);
1436 * Verify requested access to file.
1438 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1442 mutex_enter(&dzp
->z_lock
);
1444 mutex_exit(&dzp
->z_lock
);
1447 * Truncate regular files if requested.
1449 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
1450 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
1451 /* we can't hold any locks when calling zfs_freesp() */
1452 zfs_dirent_unlock(dl
);
1454 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1460 zfs_dirent_unlock(dl
);
1466 zfs_inode_update(dzp
);
1467 zfs_inode_update(zp
);
1471 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1472 zil_commit(zilog
, 0);
1477 EXPORT_SYMBOL(zfs_create
);
1480 * Remove an entry from a directory.
1482 * IN: dip - inode of directory to remove entry from.
1483 * name - name of entry to remove.
1484 * cr - credentials of caller.
1486 * RETURN: 0 if success
1487 * error code if failure
1491 * ip - ctime (if nlink > 0)
1494 uint64_t null_xattr
= 0;
1498 zfs_remove(struct inode
*dip
, char *name
, cred_t
*cr
)
1500 znode_t
*zp
, *dzp
= ITOZ(dip
);
1503 zfs_sb_t
*zsb
= ITOZSB(dip
);
1506 uint64_t xattr_obj_unlinked
= 0;
1512 pathname_t
*realnmp
= NULL
;
1513 #ifdef HAVE_PN_UTILS
1515 #endif /* HAVE_PN_UTILS */
1518 boolean_t waited
= B_FALSE
;
1524 #ifdef HAVE_PN_UTILS
1525 if (flags
& FIGNORECASE
) {
1530 #endif /* HAVE_PN_UTILS */
1536 * Attempt to lock directory; fail if entry doesn't exist.
1538 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1540 #ifdef HAVE_PN_UTILS
1543 #endif /* HAVE_PN_UTILS */
1550 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1555 * Need to use rmdir for removing directories.
1557 if (S_ISDIR(ip
->i_mode
)) {
1558 error
= SET_ERROR(EPERM
);
1564 dnlc_remove(dvp
, realnmp
->pn_buf
);
1566 dnlc_remove(dvp
, name
);
1567 #endif /* HAVE_DNLC */
1570 * We never delete the znode and always place it in the unlinked
1571 * set. The dentry cache will always hold the last reference and
1572 * is responsible for safely freeing the znode.
1575 tx
= dmu_tx_create(zsb
->z_os
);
1576 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1577 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1578 zfs_sa_upgrade_txholds(tx
, zp
);
1579 zfs_sa_upgrade_txholds(tx
, dzp
);
1581 /* are there any extended attributes? */
1582 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1583 &xattr_obj
, sizeof (xattr_obj
));
1584 if (error
== 0 && xattr_obj
) {
1585 error
= zfs_zget(zsb
, xattr_obj
, &xzp
);
1587 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1588 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1591 /* charge as an update -- would be nice not to charge at all */
1592 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1594 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1596 zfs_dirent_unlock(dl
);
1600 if (error
== ERESTART
) {
1606 #ifdef HAVE_PN_UTILS
1609 #endif /* HAVE_PN_UTILS */
1616 * Remove the directory entry.
1618 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1627 * Hold z_lock so that we can make sure that the ACL obj
1628 * hasn't changed. Could have been deleted due to
1631 mutex_enter(&zp
->z_lock
);
1632 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1633 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1634 mutex_exit(&zp
->z_lock
);
1635 zfs_unlinked_add(zp
, tx
);
1639 #ifdef HAVE_PN_UTILS
1640 if (flags
& FIGNORECASE
)
1642 #endif /* HAVE_PN_UTILS */
1643 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
);
1647 #ifdef HAVE_PN_UTILS
1650 #endif /* HAVE_PN_UTILS */
1652 zfs_dirent_unlock(dl
);
1653 zfs_inode_update(dzp
);
1654 zfs_inode_update(zp
);
1656 zfs_inode_update(xzp
);
1662 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1663 zil_commit(zilog
, 0);
1668 EXPORT_SYMBOL(zfs_remove
);
1671 * Create a new directory and insert it into dip using the name
1672 * provided. Return a pointer to the inserted directory.
1674 * IN: dip - inode of directory to add subdir to.
1675 * dirname - name of new directory.
1676 * vap - attributes of new directory.
1677 * cr - credentials of caller.
1678 * vsecp - ACL to be set
1680 * OUT: ipp - inode of created directory.
1682 * RETURN: 0 if success
1683 * error code if failure
1686 * dip - ctime|mtime updated
1687 * ipp - ctime|mtime|atime updated
1691 zfs_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
, struct inode
**ipp
,
1692 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1694 znode_t
*zp
, *dzp
= ITOZ(dip
);
1695 zfs_sb_t
*zsb
= ITOZSB(dip
);
1703 gid_t gid
= crgetgid(cr
);
1704 zfs_acl_ids_t acl_ids
;
1705 boolean_t fuid_dirtied
;
1706 boolean_t waited
= B_FALSE
;
1708 ASSERT(S_ISDIR(vap
->va_mode
));
1711 * If we have an ephemeral id, ACL, or XVATTR then
1712 * make sure file system is at proper version
1716 if (zsb
->z_use_fuids
== B_FALSE
&&
1717 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1718 return (SET_ERROR(EINVAL
));
1724 if (dzp
->z_pflags
& ZFS_XATTR
) {
1726 return (SET_ERROR(EINVAL
));
1729 if (zsb
->z_utf8
&& u8_validate(dirname
,
1730 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1732 return (SET_ERROR(EILSEQ
));
1734 if (flags
& FIGNORECASE
)
1737 if (vap
->va_mask
& ATTR_XVATTR
) {
1738 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1739 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1745 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1746 vsecp
, &acl_ids
)) != 0) {
1751 * First make sure the new directory doesn't exist.
1753 * Existence is checked first to make sure we don't return
1754 * EACCES instead of EEXIST which can cause some applications
1760 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1762 zfs_acl_ids_free(&acl_ids
);
1767 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
1768 zfs_acl_ids_free(&acl_ids
);
1769 zfs_dirent_unlock(dl
);
1774 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1775 zfs_acl_ids_free(&acl_ids
);
1776 zfs_dirent_unlock(dl
);
1778 return (SET_ERROR(EDQUOT
));
1782 * Add a new entry to the directory.
1784 tx
= dmu_tx_create(zsb
->z_os
);
1785 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1786 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1787 fuid_dirtied
= zsb
->z_fuid_dirty
;
1789 zfs_fuid_txhold(zsb
, tx
);
1790 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1791 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1792 acl_ids
.z_aclp
->z_acl_bytes
);
1795 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1796 ZFS_SA_BASE_ATTR_SIZE
);
1798 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1800 zfs_dirent_unlock(dl
);
1801 if (error
== ERESTART
) {
1807 zfs_acl_ids_free(&acl_ids
);
1816 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1819 zfs_fuid_sync(zsb
, tx
);
1822 * Now put new name in parent dir.
1824 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1828 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1829 if (flags
& FIGNORECASE
)
1831 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
1832 acl_ids
.z_fuidp
, vap
);
1834 zfs_acl_ids_free(&acl_ids
);
1838 zfs_dirent_unlock(dl
);
1840 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1841 zil_commit(zilog
, 0);
1843 zfs_inode_update(dzp
);
1844 zfs_inode_update(zp
);
1848 EXPORT_SYMBOL(zfs_mkdir
);
1851 * Remove a directory subdir entry. If the current working
1852 * directory is the same as the subdir to be removed, the
1855 * IN: dip - inode of directory to remove from.
1856 * name - name of directory to be removed.
1857 * cwd - inode of current working directory.
1858 * cr - credentials of caller.
1859 * flags - case flags
1861 * RETURN: 0 on success, error code on failure.
1864 * dip - ctime|mtime updated
1868 zfs_rmdir(struct inode
*dip
, char *name
, struct inode
*cwd
, cred_t
*cr
,
1871 znode_t
*dzp
= ITOZ(dip
);
1874 zfs_sb_t
*zsb
= ITOZSB(dip
);
1880 boolean_t waited
= B_FALSE
;
1886 if (flags
& FIGNORECASE
)
1892 * Attempt to lock directory; fail if entry doesn't exist.
1894 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1902 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1906 if (!S_ISDIR(ip
->i_mode
)) {
1907 error
= SET_ERROR(ENOTDIR
);
1912 error
= SET_ERROR(EINVAL
);
1917 * Grab a lock on the directory to make sure that noone is
1918 * trying to add (or lookup) entries while we are removing it.
1920 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1923 * Grab a lock on the parent pointer to make sure we play well
1924 * with the treewalk and directory rename code.
1926 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1928 tx
= dmu_tx_create(zsb
->z_os
);
1929 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1930 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1931 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1932 zfs_sa_upgrade_txholds(tx
, zp
);
1933 zfs_sa_upgrade_txholds(tx
, dzp
);
1934 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1936 rw_exit(&zp
->z_parent_lock
);
1937 rw_exit(&zp
->z_name_lock
);
1938 zfs_dirent_unlock(dl
);
1940 if (error
== ERESTART
) {
1951 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
1954 uint64_t txtype
= TX_RMDIR
;
1955 if (flags
& FIGNORECASE
)
1957 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
);
1962 rw_exit(&zp
->z_parent_lock
);
1963 rw_exit(&zp
->z_name_lock
);
1965 zfs_dirent_unlock(dl
);
1967 zfs_inode_update(dzp
);
1968 zfs_inode_update(zp
);
1971 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1972 zil_commit(zilog
, 0);
1977 EXPORT_SYMBOL(zfs_rmdir
);
1980 * Read as many directory entries as will fit into the provided
1981 * dirent buffer from the given directory cursor position.
1983 * IN: ip - inode of directory to read.
1984 * dirent - buffer for directory entries.
1986 * OUT: dirent - filler buffer of directory entries.
1988 * RETURN: 0 if success
1989 * error code if failure
1992 * ip - atime updated
1994 * Note that the low 4 bits of the cookie returned by zap is always zero.
1995 * This allows us to use the low range for "special" directory entries:
1996 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1997 * we use the offset 2 for the '.zfs' directory.
2001 zfs_readdir(struct inode
*ip
, struct dir_context
*ctx
, cred_t
*cr
)
2003 znode_t
*zp
= ITOZ(ip
);
2004 zfs_sb_t
*zsb
= ITOZSB(ip
);
2007 zap_attribute_t zap
;
2013 uint64_t offset
; /* must be unsigned; checks for < 1 */
2018 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
2019 &parent
, sizeof (parent
))) != 0)
2023 * Quit if directory has been removed (posix)
2031 prefetch
= zp
->z_zn_prefetch
;
2034 * Initialize the iterator cursor.
2038 * Start iteration from the beginning of the directory.
2040 zap_cursor_init(&zc
, os
, zp
->z_id
);
2043 * The offset is a serialized cursor.
2045 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
2049 * Transform to file-system independent format
2054 * Special case `.', `..', and `.zfs'.
2057 (void) strcpy(zap
.za_name
, ".");
2058 zap
.za_normalization_conflict
= 0;
2061 } else if (offset
== 1) {
2062 (void) strcpy(zap
.za_name
, "..");
2063 zap
.za_normalization_conflict
= 0;
2066 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
2067 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
2068 zap
.za_normalization_conflict
= 0;
2069 objnum
= ZFSCTL_INO_ROOT
;
2075 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
2076 if (error
== ENOENT
)
2083 * Allow multiple entries provided the first entry is
2084 * the object id. Non-zpl consumers may safely make
2085 * use of the additional space.
2087 * XXX: This should be a feature flag for compatibility
2089 if (zap
.za_integer_length
!= 8 ||
2090 zap
.za_num_integers
== 0) {
2091 cmn_err(CE_WARN
, "zap_readdir: bad directory "
2092 "entry, obj = %lld, offset = %lld, "
2093 "length = %d, num = %lld\n",
2094 (u_longlong_t
)zp
->z_id
,
2095 (u_longlong_t
)offset
,
2096 zap
.za_integer_length
,
2097 (u_longlong_t
)zap
.za_num_integers
);
2098 error
= SET_ERROR(ENXIO
);
2102 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
2103 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
2106 done
= !dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
2111 /* Prefetch znode */
2113 dmu_prefetch(os
, objnum
, 0, 0);
2117 * Move to the next entry, fill in the previous offset.
2119 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2120 zap_cursor_advance(&zc
);
2121 offset
= zap_cursor_serialize(&zc
);
2127 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2130 zap_cursor_fini(&zc
);
2131 if (error
== ENOENT
)
2134 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
2141 EXPORT_SYMBOL(zfs_readdir
);
2143 ulong_t zfs_fsync_sync_cnt
= 4;
2146 zfs_fsync(struct inode
*ip
, int syncflag
, cred_t
*cr
)
2148 znode_t
*zp
= ITOZ(ip
);
2149 zfs_sb_t
*zsb
= ITOZSB(ip
);
2151 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2153 if (zsb
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2156 zil_commit(zsb
->z_log
, zp
->z_id
);
2161 EXPORT_SYMBOL(zfs_fsync
);
2165 * Get the requested file attributes and place them in the provided
2168 * IN: ip - inode of file.
2169 * vap - va_mask identifies requested attributes.
2170 * If ATTR_XVATTR set, then optional attrs are requested
2171 * flags - ATTR_NOACLCHECK (CIFS server context)
2172 * cr - credentials of caller.
2174 * OUT: vap - attribute values.
2176 * RETURN: 0 (always succeeds)
2180 zfs_getattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2182 znode_t
*zp
= ITOZ(ip
);
2183 zfs_sb_t
*zsb
= ITOZSB(ip
);
2186 uint64_t mtime
[2], ctime
[2];
2187 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2188 xoptattr_t
*xoap
= NULL
;
2189 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2190 sa_bulk_attr_t bulk
[2];
2196 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2198 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
2199 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
2201 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2207 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2208 * Also, if we are the owner don't bother, since owner should
2209 * always be allowed to read basic attributes of file.
2211 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2212 (vap
->va_uid
!= crgetuid(cr
))) {
2213 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2221 * Return all attributes. It's cheaper to provide the answer
2222 * than to determine whether we were asked the question.
2225 mutex_enter(&zp
->z_lock
);
2226 vap
->va_type
= vn_mode_to_vtype(zp
->z_mode
);
2227 vap
->va_mode
= zp
->z_mode
;
2228 vap
->va_fsid
= ZTOI(zp
)->i_sb
->s_dev
;
2229 vap
->va_nodeid
= zp
->z_id
;
2230 if ((zp
->z_id
== zsb
->z_root
) && zfs_show_ctldir(zp
))
2231 links
= zp
->z_links
+ 1;
2233 links
= zp
->z_links
;
2234 vap
->va_nlink
= MIN(links
, ZFS_LINK_MAX
);
2235 vap
->va_size
= i_size_read(ip
);
2236 vap
->va_rdev
= ip
->i_rdev
;
2237 vap
->va_seq
= ip
->i_generation
;
2240 * Add in any requested optional attributes and the create time.
2241 * Also set the corresponding bits in the returned attribute bitmap.
2243 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zsb
->z_use_fuids
) {
2244 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2246 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2247 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2250 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2251 xoap
->xoa_readonly
=
2252 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2253 XVA_SET_RTN(xvap
, XAT_READONLY
);
2256 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2258 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2259 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2262 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2264 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2265 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2268 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2269 xoap
->xoa_nounlink
=
2270 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2271 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2274 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2275 xoap
->xoa_immutable
=
2276 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2277 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2280 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2281 xoap
->xoa_appendonly
=
2282 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2283 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2286 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2288 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2289 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2292 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2294 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2295 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2298 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2299 xoap
->xoa_av_quarantined
=
2300 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2301 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2304 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2305 xoap
->xoa_av_modified
=
2306 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2307 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2310 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2311 S_ISREG(ip
->i_mode
)) {
2312 zfs_sa_get_scanstamp(zp
, xvap
);
2315 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2318 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zsb
),
2319 times
, sizeof (times
));
2320 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2321 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2324 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2325 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2326 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2328 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2329 xoap
->xoa_generation
= zp
->z_gen
;
2330 XVA_SET_RTN(xvap
, XAT_GEN
);
2333 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2335 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2336 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2339 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2341 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2342 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2346 ZFS_TIME_DECODE(&vap
->va_atime
, zp
->z_atime
);
2347 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2348 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2350 mutex_exit(&zp
->z_lock
);
2352 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2354 if (zp
->z_blksz
== 0) {
2356 * Block size hasn't been set; suggest maximal I/O transfers.
2358 vap
->va_blksize
= zsb
->z_max_blksz
;
2364 EXPORT_SYMBOL(zfs_getattr
);
2367 * Get the basic file attributes and place them in the provided kstat
2368 * structure. The inode is assumed to be the authoritative source
2369 * for most of the attributes. However, the znode currently has the
2370 * authoritative atime, blksize, and block count.
2372 * IN: ip - inode of file.
2374 * OUT: sp - kstat values.
2376 * RETURN: 0 (always succeeds)
2380 zfs_getattr_fast(struct inode
*ip
, struct kstat
*sp
)
2382 znode_t
*zp
= ITOZ(ip
);
2383 zfs_sb_t
*zsb
= ITOZSB(ip
);
2385 u_longlong_t nblocks
;
2390 mutex_enter(&zp
->z_lock
);
2392 generic_fillattr(ip
, sp
);
2393 ZFS_TIME_DECODE(&sp
->atime
, zp
->z_atime
);
2395 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
2396 sp
->blksize
= blksize
;
2397 sp
->blocks
= nblocks
;
2399 if (unlikely(zp
->z_blksz
== 0)) {
2401 * Block size hasn't been set; suggest maximal I/O transfers.
2403 sp
->blksize
= zsb
->z_max_blksz
;
2406 mutex_exit(&zp
->z_lock
);
2412 EXPORT_SYMBOL(zfs_getattr_fast
);
2415 * Set the file attributes to the values contained in the
2418 * IN: ip - inode of file to be modified.
2419 * vap - new attribute values.
2420 * If ATTR_XVATTR set, then optional attrs are being set
2421 * flags - ATTR_UTIME set if non-default time values provided.
2422 * - ATTR_NOACLCHECK (CIFS context only).
2423 * cr - credentials of caller.
2425 * RETURN: 0 if success
2426 * error code if failure
2429 * ip - ctime updated, mtime updated if size changed.
2433 zfs_setattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2435 znode_t
*zp
= ITOZ(ip
);
2436 zfs_sb_t
*zsb
= ITOZSB(ip
);
2440 xvattr_t
*tmpxvattr
;
2441 uint_t mask
= vap
->va_mask
;
2442 uint_t saved_mask
= 0;
2445 uint64_t new_uid
, new_gid
;
2447 uint64_t mtime
[2], ctime
[2];
2449 int need_policy
= FALSE
;
2451 zfs_fuid_info_t
*fuidp
= NULL
;
2452 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2455 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2456 boolean_t fuid_dirtied
= B_FALSE
;
2457 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
2458 int count
= 0, xattr_count
= 0;
2469 * Make sure that if we have ephemeral uid/gid or xvattr specified
2470 * that file system is at proper version level
2473 if (zsb
->z_use_fuids
== B_FALSE
&&
2474 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2475 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2476 (mask
& ATTR_XVATTR
))) {
2478 return (SET_ERROR(EINVAL
));
2481 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
2483 return (SET_ERROR(EISDIR
));
2486 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
2488 return (SET_ERROR(EINVAL
));
2492 * If this is an xvattr_t, then get a pointer to the structure of
2493 * optional attributes. If this is NULL, then we have a vattr_t.
2495 xoap
= xva_getxoptattr(xvap
);
2497 tmpxvattr
= kmem_alloc(sizeof (xvattr_t
), KM_SLEEP
);
2498 xva_init(tmpxvattr
);
2500 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * 7, KM_SLEEP
);
2501 xattr_bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * 7, KM_SLEEP
);
2504 * Immutable files can only alter immutable bit and atime
2506 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2507 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
2508 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2513 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2519 * Verify timestamps doesn't overflow 32 bits.
2520 * ZFS can handle large timestamps, but 32bit syscalls can't
2521 * handle times greater than 2039. This check should be removed
2522 * once large timestamps are fully supported.
2524 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
2525 if (((mask
& ATTR_ATIME
) &&
2526 TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2527 ((mask
& ATTR_MTIME
) &&
2528 TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2538 /* Can this be moved to before the top label? */
2539 if (zfs_is_readonly(zsb
)) {
2545 * First validate permissions
2548 if (mask
& ATTR_SIZE
) {
2549 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2554 * XXX - Note, we are not providing any open
2555 * mode flags here (like FNDELAY), so we may
2556 * block if there are locks present... this
2557 * should be addressed in openat().
2559 /* XXX - would it be OK to generate a log record here? */
2560 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2565 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
2566 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2567 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2568 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2569 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2570 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2571 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2572 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2573 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2577 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2578 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
2583 * NOTE: even if a new mode is being set,
2584 * we may clear S_ISUID/S_ISGID bits.
2587 if (!(mask
& ATTR_MODE
))
2588 vap
->va_mode
= zp
->z_mode
;
2591 * Take ownership or chgrp to group we are a member of
2594 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
2595 take_group
= (mask
& ATTR_GID
) &&
2596 zfs_groupmember(zsb
, vap
->va_gid
, cr
);
2599 * If both ATTR_UID and ATTR_GID are set then take_owner and
2600 * take_group must both be set in order to allow taking
2603 * Otherwise, send the check through secpolicy_vnode_setattr()
2607 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
2608 take_owner
&& take_group
) ||
2609 ((idmask
== ATTR_UID
) && take_owner
) ||
2610 ((idmask
== ATTR_GID
) && take_group
)) {
2611 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2612 skipaclchk
, cr
) == 0) {
2614 * Remove setuid/setgid for non-privileged users
2616 (void) secpolicy_setid_clear(vap
, cr
);
2617 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
2626 mutex_enter(&zp
->z_lock
);
2627 oldva
.va_mode
= zp
->z_mode
;
2628 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2629 if (mask
& ATTR_XVATTR
) {
2631 * Update xvattr mask to include only those attributes
2632 * that are actually changing.
2634 * the bits will be restored prior to actually setting
2635 * the attributes so the caller thinks they were set.
2637 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2638 if (xoap
->xoa_appendonly
!=
2639 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2642 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2643 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
2647 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2648 if (xoap
->xoa_nounlink
!=
2649 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2652 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2653 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
2657 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2658 if (xoap
->xoa_immutable
!=
2659 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2662 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2663 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
2667 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2668 if (xoap
->xoa_nodump
!=
2669 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2672 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2673 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
2677 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2678 if (xoap
->xoa_av_modified
!=
2679 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2682 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2683 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
2687 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2688 if ((!S_ISREG(ip
->i_mode
) &&
2689 xoap
->xoa_av_quarantined
) ||
2690 xoap
->xoa_av_quarantined
!=
2691 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2694 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2695 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
2699 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2700 mutex_exit(&zp
->z_lock
);
2705 if (need_policy
== FALSE
&&
2706 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2707 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2712 mutex_exit(&zp
->z_lock
);
2714 if (mask
& ATTR_MODE
) {
2715 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2716 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
2721 trim_mask
|= ATTR_MODE
;
2729 * If trim_mask is set then take ownership
2730 * has been granted or write_acl is present and user
2731 * has the ability to modify mode. In that case remove
2732 * UID|GID and or MODE from mask so that
2733 * secpolicy_vnode_setattr() doesn't revoke it.
2737 saved_mask
= vap
->va_mask
;
2738 vap
->va_mask
&= ~trim_mask
;
2740 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
2741 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2746 vap
->va_mask
|= saved_mask
;
2750 * secpolicy_vnode_setattr, or take ownership may have
2753 mask
= vap
->va_mask
;
2755 if ((mask
& (ATTR_UID
| ATTR_GID
))) {
2756 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
2757 &xattr_obj
, sizeof (xattr_obj
));
2759 if (err
== 0 && xattr_obj
) {
2760 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
2764 if (mask
& ATTR_UID
) {
2765 new_uid
= zfs_fuid_create(zsb
,
2766 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2767 if (new_uid
!= zp
->z_uid
&&
2768 zfs_fuid_overquota(zsb
, B_FALSE
, new_uid
)) {
2776 if (mask
& ATTR_GID
) {
2777 new_gid
= zfs_fuid_create(zsb
, (uint64_t)vap
->va_gid
,
2778 cr
, ZFS_GROUP
, &fuidp
);
2779 if (new_gid
!= zp
->z_gid
&&
2780 zfs_fuid_overquota(zsb
, B_TRUE
, new_gid
)) {
2788 tx
= dmu_tx_create(zsb
->z_os
);
2790 if (mask
& ATTR_MODE
) {
2791 uint64_t pmode
= zp
->z_mode
;
2793 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2795 zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
);
2797 mutex_enter(&zp
->z_lock
);
2798 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2800 * Are we upgrading ACL from old V0 format
2803 if (zsb
->z_version
>= ZPL_VERSION_FUID
&&
2804 zfs_znode_acl_version(zp
) ==
2805 ZFS_ACL_VERSION_INITIAL
) {
2806 dmu_tx_hold_free(tx
, acl_obj
, 0,
2808 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2809 0, aclp
->z_acl_bytes
);
2811 dmu_tx_hold_write(tx
, acl_obj
, 0,
2814 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2815 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2816 0, aclp
->z_acl_bytes
);
2818 mutex_exit(&zp
->z_lock
);
2819 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2821 if ((mask
& ATTR_XVATTR
) &&
2822 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2823 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2825 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2829 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2832 fuid_dirtied
= zsb
->z_fuid_dirty
;
2834 zfs_fuid_txhold(zsb
, tx
);
2836 zfs_sa_upgrade_txholds(tx
, zp
);
2838 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2844 * Set each attribute requested.
2845 * We group settings according to the locks they need to acquire.
2847 * Note: you cannot set ctime directly, although it will be
2848 * updated as a side-effect of calling this function.
2852 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2853 mutex_enter(&zp
->z_acl_lock
);
2854 mutex_enter(&zp
->z_lock
);
2856 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
2857 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2860 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2861 mutex_enter(&attrzp
->z_acl_lock
);
2862 mutex_enter(&attrzp
->z_lock
);
2863 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2864 SA_ZPL_FLAGS(zsb
), NULL
, &attrzp
->z_pflags
,
2865 sizeof (attrzp
->z_pflags
));
2868 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2870 if (mask
& ATTR_UID
) {
2871 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
2872 &new_uid
, sizeof (new_uid
));
2873 zp
->z_uid
= new_uid
;
2875 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2876 SA_ZPL_UID(zsb
), NULL
, &new_uid
,
2878 attrzp
->z_uid
= new_uid
;
2882 if (mask
& ATTR_GID
) {
2883 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
),
2884 NULL
, &new_gid
, sizeof (new_gid
));
2885 zp
->z_gid
= new_gid
;
2887 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2888 SA_ZPL_GID(zsb
), NULL
, &new_gid
,
2890 attrzp
->z_gid
= new_gid
;
2893 if (!(mask
& ATTR_MODE
)) {
2894 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
),
2895 NULL
, &new_mode
, sizeof (new_mode
));
2896 new_mode
= zp
->z_mode
;
2898 err
= zfs_acl_chown_setattr(zp
);
2901 err
= zfs_acl_chown_setattr(attrzp
);
2906 if (mask
& ATTR_MODE
) {
2907 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
2908 &new_mode
, sizeof (new_mode
));
2909 zp
->z_mode
= new_mode
;
2910 ASSERT3P(aclp
, !=, NULL
);
2911 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2913 if (zp
->z_acl_cached
)
2914 zfs_acl_free(zp
->z_acl_cached
);
2915 zp
->z_acl_cached
= aclp
;
2920 if (mask
& ATTR_ATIME
) {
2921 ZFS_TIME_ENCODE(&vap
->va_atime
, zp
->z_atime
);
2922 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
2923 &zp
->z_atime
, sizeof (zp
->z_atime
));
2926 if (mask
& ATTR_MTIME
) {
2927 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2928 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
,
2929 mtime
, sizeof (mtime
));
2932 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2933 if (mask
& ATTR_SIZE
&& !(mask
& ATTR_MTIME
)) {
2934 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
),
2935 NULL
, mtime
, sizeof (mtime
));
2936 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2937 &ctime
, sizeof (ctime
));
2938 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
2940 } else if (mask
!= 0) {
2941 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2942 &ctime
, sizeof (ctime
));
2943 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
,
2946 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2947 SA_ZPL_CTIME(zsb
), NULL
,
2948 &ctime
, sizeof (ctime
));
2949 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
2950 mtime
, ctime
, B_TRUE
);
2954 * Do this after setting timestamps to prevent timestamp
2955 * update from toggling bit
2958 if (xoap
&& (mask
& ATTR_XVATTR
)) {
2961 * restore trimmed off masks
2962 * so that return masks can be set for caller.
2965 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
2966 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2968 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
2969 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2971 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
2972 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2974 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
2975 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2977 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
2978 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2980 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
2981 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2984 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2985 ASSERT(S_ISREG(ip
->i_mode
));
2987 zfs_xvattr_set(zp
, xvap
, tx
);
2991 zfs_fuid_sync(zsb
, tx
);
2994 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
2996 mutex_exit(&zp
->z_lock
);
2997 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2998 mutex_exit(&zp
->z_acl_lock
);
3001 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3002 mutex_exit(&attrzp
->z_acl_lock
);
3003 mutex_exit(&attrzp
->z_lock
);
3006 if (err
== 0 && attrzp
) {
3007 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3018 zfs_fuid_info_free(fuidp
);
3024 if (err
== ERESTART
)
3027 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3029 zfs_inode_update(zp
);
3033 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3034 zil_commit(zilog
, 0);
3037 kmem_free(xattr_bulk
, sizeof (sa_bulk_attr_t
) * 7);
3038 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * 7);
3039 kmem_free(tmpxvattr
, sizeof (xvattr_t
));
3043 EXPORT_SYMBOL(zfs_setattr
);
3045 typedef struct zfs_zlock
{
3046 krwlock_t
*zl_rwlock
; /* lock we acquired */
3047 znode_t
*zl_znode
; /* znode we held */
3048 struct zfs_zlock
*zl_next
; /* next in list */
3052 * Drop locks and release vnodes that were held by zfs_rename_lock().
3055 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3059 while ((zl
= *zlpp
) != NULL
) {
3060 if (zl
->zl_znode
!= NULL
)
3061 iput(ZTOI(zl
->zl_znode
));
3062 rw_exit(zl
->zl_rwlock
);
3063 *zlpp
= zl
->zl_next
;
3064 kmem_free(zl
, sizeof (*zl
));
3069 * Search back through the directory tree, using the ".." entries.
3070 * Lock each directory in the chain to prevent concurrent renames.
3071 * Fail any attempt to move a directory into one of its own descendants.
3072 * XXX - z_parent_lock can overlap with map or grow locks
3075 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3079 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
3080 uint64_t oidp
= zp
->z_id
;
3081 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3082 krw_t rw
= RW_WRITER
;
3085 * First pass write-locks szp and compares to zp->z_id.
3086 * Later passes read-lock zp and compare to zp->z_parent.
3089 if (!rw_tryenter(rwlp
, rw
)) {
3091 * Another thread is renaming in this path.
3092 * Note that if we are a WRITER, we don't have any
3093 * parent_locks held yet.
3095 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3097 * Drop our locks and restart
3099 zfs_rename_unlock(&zl
);
3103 rwlp
= &szp
->z_parent_lock
;
3108 * Wait for other thread to drop its locks
3114 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3115 zl
->zl_rwlock
= rwlp
;
3116 zl
->zl_znode
= NULL
;
3117 zl
->zl_next
= *zlpp
;
3120 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3121 return (SET_ERROR(EINVAL
));
3123 if (oidp
== rootid
) /* We've hit the top */
3126 if (rw
== RW_READER
) { /* i.e. not the first pass */
3127 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
3132 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
3133 &oidp
, sizeof (oidp
));
3134 rwlp
= &zp
->z_parent_lock
;
3137 } while (zp
->z_id
!= sdzp
->z_id
);
3143 * Move an entry from the provided source directory to the target
3144 * directory. Change the entry name as indicated.
3146 * IN: sdip - Source directory containing the "old entry".
3147 * snm - Old entry name.
3148 * tdip - Target directory to contain the "new entry".
3149 * tnm - New entry name.
3150 * cr - credentials of caller.
3151 * flags - case flags
3153 * RETURN: 0 on success, error code on failure.
3156 * sdip,tdip - ctime|mtime updated
3160 zfs_rename(struct inode
*sdip
, char *snm
, struct inode
*tdip
, char *tnm
,
3161 cred_t
*cr
, int flags
)
3163 znode_t
*tdzp
, *szp
, *tzp
;
3164 znode_t
*sdzp
= ITOZ(sdip
);
3165 zfs_sb_t
*zsb
= ITOZSB(sdip
);
3167 zfs_dirlock_t
*sdl
, *tdl
;
3170 int cmp
, serr
, terr
;
3173 boolean_t waited
= B_FALSE
;
3176 ZFS_VERIFY_ZP(sdzp
);
3179 if (tdip
->i_sb
!= sdip
->i_sb
|| zfsctl_is_node(tdip
)) {
3181 return (SET_ERROR(EXDEV
));
3185 ZFS_VERIFY_ZP(tdzp
);
3186 if (zsb
->z_utf8
&& u8_validate(tnm
,
3187 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3189 return (SET_ERROR(EILSEQ
));
3192 if (flags
& FIGNORECASE
)
3201 * This is to prevent the creation of links into attribute space
3202 * by renaming a linked file into/outof an attribute directory.
3203 * See the comment in zfs_link() for why this is considered bad.
3205 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3207 return (SET_ERROR(EINVAL
));
3211 * Lock source and target directory entries. To prevent deadlock,
3212 * a lock ordering must be defined. We lock the directory with
3213 * the smallest object id first, or if it's a tie, the one with
3214 * the lexically first name.
3216 if (sdzp
->z_id
< tdzp
->z_id
) {
3218 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3222 * First compare the two name arguments without
3223 * considering any case folding.
3225 int nofold
= (zsb
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3227 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3228 ASSERT(error
== 0 || !zsb
->z_utf8
);
3231 * POSIX: "If the old argument and the new argument
3232 * both refer to links to the same existing file,
3233 * the rename() function shall return successfully
3234 * and perform no other action."
3240 * If the file system is case-folding, then we may
3241 * have some more checking to do. A case-folding file
3242 * system is either supporting mixed case sensitivity
3243 * access or is completely case-insensitive. Note
3244 * that the file system is always case preserving.
3246 * In mixed sensitivity mode case sensitive behavior
3247 * is the default. FIGNORECASE must be used to
3248 * explicitly request case insensitive behavior.
3250 * If the source and target names provided differ only
3251 * by case (e.g., a request to rename 'tim' to 'Tim'),
3252 * we will treat this as a special case in the
3253 * case-insensitive mode: as long as the source name
3254 * is an exact match, we will allow this to proceed as
3255 * a name-change request.
3257 if ((zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
3258 (zsb
->z_case
== ZFS_CASE_MIXED
&&
3259 flags
& FIGNORECASE
)) &&
3260 u8_strcmp(snm
, tnm
, 0, zsb
->z_norm
, U8_UNICODE_LATEST
,
3263 * case preserving rename request, require exact
3272 * If the source and destination directories are the same, we should
3273 * grab the z_name_lock of that directory only once.
3277 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3281 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3282 ZEXISTS
| zflg
, NULL
, NULL
);
3283 terr
= zfs_dirent_lock(&tdl
,
3284 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3286 terr
= zfs_dirent_lock(&tdl
,
3287 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3288 serr
= zfs_dirent_lock(&sdl
,
3289 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3295 * Source entry invalid or not there.
3298 zfs_dirent_unlock(tdl
);
3304 rw_exit(&sdzp
->z_name_lock
);
3306 if (strcmp(snm
, "..") == 0)
3312 zfs_dirent_unlock(sdl
);
3316 rw_exit(&sdzp
->z_name_lock
);
3318 if (strcmp(tnm
, "..") == 0)
3325 * Must have write access at the source to remove the old entry
3326 * and write access at the target to create the new entry.
3327 * Note that if target and source are the same, this can be
3328 * done in a single check.
3331 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
3334 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3336 * Check to make sure rename is valid.
3337 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3339 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
3344 * Does target exist?
3348 * Source and target must be the same type.
3350 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3351 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3352 error
= SET_ERROR(ENOTDIR
);
3356 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3357 error
= SET_ERROR(EISDIR
);
3362 * POSIX dictates that when the source and target
3363 * entries refer to the same file object, rename
3364 * must do nothing and exit without error.
3366 if (szp
->z_id
== tzp
->z_id
) {
3372 tx
= dmu_tx_create(zsb
->z_os
);
3373 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3374 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3375 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3376 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3378 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3379 zfs_sa_upgrade_txholds(tx
, tdzp
);
3382 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3383 zfs_sa_upgrade_txholds(tx
, tzp
);
3386 zfs_sa_upgrade_txholds(tx
, szp
);
3387 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
3388 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3391 zfs_rename_unlock(&zl
);
3392 zfs_dirent_unlock(sdl
);
3393 zfs_dirent_unlock(tdl
);
3396 rw_exit(&sdzp
->z_name_lock
);
3401 if (error
== ERESTART
) {
3412 if (tzp
) /* Attempt to remove the existing target */
3413 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3416 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3418 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3420 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zsb
),
3421 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3424 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3426 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3427 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
3428 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3431 * At this point, we have successfully created
3432 * the target name, but have failed to remove
3433 * the source name. Since the create was done
3434 * with the ZRENAMING flag, there are
3435 * complications; for one, the link count is
3436 * wrong. The easiest way to deal with this
3437 * is to remove the newly created target, and
3438 * return the original error. This must
3439 * succeed; fortunately, it is very unlikely to
3440 * fail, since we just created it.
3442 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3443 ZRENAMING
, NULL
), ==, 0);
3451 zfs_rename_unlock(&zl
);
3453 zfs_dirent_unlock(sdl
);
3454 zfs_dirent_unlock(tdl
);
3456 zfs_inode_update(sdzp
);
3458 rw_exit(&sdzp
->z_name_lock
);
3461 zfs_inode_update(tdzp
);
3463 zfs_inode_update(szp
);
3466 zfs_inode_update(tzp
);
3470 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3471 zil_commit(zilog
, 0);
3476 EXPORT_SYMBOL(zfs_rename
);
3479 * Insert the indicated symbolic reference entry into the directory.
3481 * IN: dip - Directory to contain new symbolic link.
3482 * link - Name for new symlink entry.
3483 * vap - Attributes of new entry.
3484 * target - Target path of new symlink.
3486 * cr - credentials of caller.
3487 * flags - case flags
3489 * RETURN: 0 on success, error code on failure.
3492 * dip - ctime|mtime updated
3496 zfs_symlink(struct inode
*dip
, char *name
, vattr_t
*vap
, char *link
,
3497 struct inode
**ipp
, cred_t
*cr
, int flags
)
3499 znode_t
*zp
, *dzp
= ITOZ(dip
);
3502 zfs_sb_t
*zsb
= ITOZSB(dip
);
3504 uint64_t len
= strlen(link
);
3507 zfs_acl_ids_t acl_ids
;
3508 boolean_t fuid_dirtied
;
3509 uint64_t txtype
= TX_SYMLINK
;
3510 boolean_t waited
= B_FALSE
;
3512 ASSERT(S_ISLNK(vap
->va_mode
));
3518 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
3519 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3521 return (SET_ERROR(EILSEQ
));
3523 if (flags
& FIGNORECASE
)
3526 if (len
> MAXPATHLEN
) {
3528 return (SET_ERROR(ENAMETOOLONG
));
3531 if ((error
= zfs_acl_ids_create(dzp
, 0,
3532 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3540 * Attempt to lock directory; fail if entry already exists.
3542 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3544 zfs_acl_ids_free(&acl_ids
);
3549 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3550 zfs_acl_ids_free(&acl_ids
);
3551 zfs_dirent_unlock(dl
);
3556 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
3557 zfs_acl_ids_free(&acl_ids
);
3558 zfs_dirent_unlock(dl
);
3560 return (SET_ERROR(EDQUOT
));
3562 tx
= dmu_tx_create(zsb
->z_os
);
3563 fuid_dirtied
= zsb
->z_fuid_dirty
;
3564 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3565 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3566 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3567 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3568 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3569 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3570 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3571 acl_ids
.z_aclp
->z_acl_bytes
);
3574 zfs_fuid_txhold(zsb
, tx
);
3575 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3577 zfs_dirent_unlock(dl
);
3578 if (error
== ERESTART
) {
3584 zfs_acl_ids_free(&acl_ids
);
3591 * Create a new object for the symlink.
3592 * for version 4 ZPL datsets the symlink will be an SA attribute
3594 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3597 zfs_fuid_sync(zsb
, tx
);
3599 mutex_enter(&zp
->z_lock
);
3601 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zsb
),
3604 zfs_sa_symlink(zp
, link
, len
, tx
);
3605 mutex_exit(&zp
->z_lock
);
3608 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
3609 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3611 * Insert the new object into the directory.
3613 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3615 if (flags
& FIGNORECASE
)
3617 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3619 zfs_inode_update(dzp
);
3620 zfs_inode_update(zp
);
3622 zfs_acl_ids_free(&acl_ids
);
3626 zfs_dirent_unlock(dl
);
3630 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3631 zil_commit(zilog
, 0);
3636 EXPORT_SYMBOL(zfs_symlink
);
3639 * Return, in the buffer contained in the provided uio structure,
3640 * the symbolic path referred to by ip.
3642 * IN: ip - inode of symbolic link
3643 * uio - structure to contain the link path.
3644 * cr - credentials of caller.
3646 * RETURN: 0 if success
3647 * error code if failure
3650 * ip - atime updated
3654 zfs_readlink(struct inode
*ip
, uio_t
*uio
, cred_t
*cr
)
3656 znode_t
*zp
= ITOZ(ip
);
3657 zfs_sb_t
*zsb
= ITOZSB(ip
);
3663 mutex_enter(&zp
->z_lock
);
3665 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3666 SA_ZPL_SYMLINK(zsb
), uio
);
3668 error
= zfs_sa_readlink(zp
, uio
);
3669 mutex_exit(&zp
->z_lock
);
3671 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
3675 EXPORT_SYMBOL(zfs_readlink
);
3678 * Insert a new entry into directory tdip referencing sip.
3680 * IN: tdip - Directory to contain new entry.
3681 * sip - inode of new entry.
3682 * name - name of new entry.
3683 * cr - credentials of caller.
3685 * RETURN: 0 if success
3686 * error code if failure
3689 * tdip - ctime|mtime updated
3690 * sip - ctime updated
3694 zfs_link(struct inode
*tdip
, struct inode
*sip
, char *name
, cred_t
*cr
)
3696 znode_t
*dzp
= ITOZ(tdip
);
3698 zfs_sb_t
*zsb
= ITOZSB(tdip
);
3706 boolean_t waited
= B_FALSE
;
3708 ASSERT(S_ISDIR(tdip
->i_mode
));
3715 * POSIX dictates that we return EPERM here.
3716 * Better choices include ENOTSUP or EISDIR.
3718 if (S_ISDIR(sip
->i_mode
)) {
3720 return (SET_ERROR(EPERM
));
3723 if (sip
->i_sb
!= tdip
->i_sb
|| zfsctl_is_node(sip
)) {
3725 return (SET_ERROR(EXDEV
));
3731 /* Prevent links to .zfs/shares files */
3733 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
3734 &parent
, sizeof (uint64_t))) != 0) {
3738 if (parent
== zsb
->z_shares_dir
) {
3740 return (SET_ERROR(EPERM
));
3743 if (zsb
->z_utf8
&& u8_validate(name
,
3744 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3746 return (SET_ERROR(EILSEQ
));
3748 #ifdef HAVE_PN_UTILS
3749 if (flags
& FIGNORECASE
)
3751 #endif /* HAVE_PN_UTILS */
3754 * We do not support links between attributes and non-attributes
3755 * because of the potential security risk of creating links
3756 * into "normal" file space in order to circumvent restrictions
3757 * imposed in attribute space.
3759 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
3761 return (SET_ERROR(EINVAL
));
3764 owner
= zfs_fuid_map_id(zsb
, szp
->z_uid
, cr
, ZFS_OWNER
);
3765 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
3767 return (SET_ERROR(EPERM
));
3770 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3777 * Attempt to lock directory; fail if entry already exists.
3779 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
3785 tx
= dmu_tx_create(zsb
->z_os
);
3786 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3787 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3788 zfs_sa_upgrade_txholds(tx
, szp
);
3789 zfs_sa_upgrade_txholds(tx
, dzp
);
3790 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3792 zfs_dirent_unlock(dl
);
3793 if (error
== ERESTART
) {
3804 error
= zfs_link_create(dl
, szp
, tx
, 0);
3807 uint64_t txtype
= TX_LINK
;
3808 #ifdef HAVE_PN_UTILS
3809 if (flags
& FIGNORECASE
)
3811 #endif /* HAVE_PN_UTILS */
3812 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
3817 zfs_dirent_unlock(dl
);
3819 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3820 zil_commit(zilog
, 0);
3822 zfs_inode_update(dzp
);
3823 zfs_inode_update(szp
);
3827 EXPORT_SYMBOL(zfs_link
);
3830 zfs_putpage_commit_cb(void *arg
)
3832 struct page
*pp
= arg
;
3835 end_page_writeback(pp
);
3839 * Push a page out to disk, once the page is on stable storage the
3840 * registered commit callback will be run as notification of completion.
3842 * IN: ip - page mapped for inode.
3843 * pp - page to push (page is locked)
3844 * wbc - writeback control data
3846 * RETURN: 0 if success
3847 * error code if failure
3850 * ip - ctime|mtime updated
3854 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
3856 znode_t
*zp
= ITOZ(ip
);
3857 zfs_sb_t
*zsb
= ITOZSB(ip
);
3865 uint64_t mtime
[2], ctime
[2];
3866 sa_bulk_attr_t bulk
[3];
3872 ASSERT(PageLocked(pp
));
3874 pgoff
= page_offset(pp
); /* Page byte-offset in file */
3875 offset
= i_size_read(ip
); /* File length in bytes */
3876 pglen
= MIN(PAGE_CACHE_SIZE
, /* Page length in bytes */
3877 P2ROUNDUP(offset
, PAGE_CACHE_SIZE
)-pgoff
);
3879 /* Page is beyond end of file */
3880 if (pgoff
>= offset
) {
3886 /* Truncate page length to end of file */
3887 if (pgoff
+ pglen
> offset
)
3888 pglen
= offset
- pgoff
;
3892 * FIXME: Allow mmap writes past its quota. The correct fix
3893 * is to register a page_mkwrite() handler to count the page
3894 * against its quota when it is about to be dirtied.
3896 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
3897 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
3902 rl
= zfs_range_lock(zp
, pgoff
, pglen
, RL_WRITER
);
3904 set_page_writeback(pp
);
3907 tx
= dmu_tx_create(zsb
->z_os
);
3908 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
3909 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3910 zfs_sa_upgrade_txholds(tx
, zp
);
3911 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3913 if (err
== ERESTART
)
3917 __set_page_dirty_nobuffers(pp
);
3919 end_page_writeback(pp
);
3920 zfs_range_unlock(rl
);
3926 ASSERT3U(pglen
, <=, PAGE_CACHE_SIZE
);
3927 dmu_write(zsb
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
3930 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
3931 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
3932 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zsb
), NULL
, &zp
->z_pflags
, 8);
3934 /* Preserve the mtime and ctime provided by the inode */
3935 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
3936 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
3937 zp
->z_atime_dirty
= 0;
3940 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
3942 zfs_log_write(zsb
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
3943 zfs_putpage_commit_cb
, pp
);
3946 zfs_range_unlock(rl
);
3948 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3950 * Note that this is rarely called under writepages(), because
3951 * writepages() normally handles the entire commit for
3952 * performance reasons.
3954 if (zsb
->z_log
!= NULL
)
3955 zil_commit(zsb
->z_log
, zp
->z_id
);
3963 * Update the system attributes when the inode has been dirtied. For the
3964 * moment we only update the mode, atime, mtime, and ctime.
3967 zfs_dirty_inode(struct inode
*ip
, int flags
)
3969 znode_t
*zp
= ITOZ(ip
);
3970 zfs_sb_t
*zsb
= ITOZSB(ip
);
3972 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
3973 sa_bulk_attr_t bulk
[4];
3977 if (zfs_is_readonly(zsb
) || dmu_objset_is_snapshot(zsb
->z_os
))
3983 tx
= dmu_tx_create(zsb
->z_os
);
3985 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3986 zfs_sa_upgrade_txholds(tx
, zp
);
3988 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3994 mutex_enter(&zp
->z_lock
);
3995 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zsb
), NULL
, &mode
, 8);
3996 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zsb
), NULL
, &atime
, 16);
3997 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
3998 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
4000 /* Preserve the mode, mtime and ctime provided by the inode */
4001 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4002 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4003 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4007 zp
->z_atime_dirty
= 0;
4009 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4010 mutex_exit(&zp
->z_lock
);
4017 EXPORT_SYMBOL(zfs_dirty_inode
);
4021 zfs_inactive(struct inode
*ip
)
4023 znode_t
*zp
= ITOZ(ip
);
4024 zfs_sb_t
*zsb
= ITOZSB(ip
);
4027 if (zfsctl_is_node(ip
)) {
4028 zfsctl_inode_inactive(ip
);
4032 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_READER
);
4033 if (zp
->z_sa_hdl
== NULL
) {
4034 rw_exit(&zsb
->z_teardown_inactive_lock
);
4038 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4039 dmu_tx_t
*tx
= dmu_tx_create(zsb
->z_os
);
4041 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4042 zfs_sa_upgrade_txholds(tx
, zp
);
4043 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4047 mutex_enter(&zp
->z_lock
);
4048 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
),
4049 (void *)&zp
->z_atime
, sizeof (zp
->z_atime
), tx
);
4050 zp
->z_atime_dirty
= 0;
4051 mutex_exit(&zp
->z_lock
);
4057 rw_exit(&zsb
->z_teardown_inactive_lock
);
4059 EXPORT_SYMBOL(zfs_inactive
);
4062 * Bounds-check the seek operation.
4064 * IN: ip - inode seeking within
4065 * ooff - old file offset
4066 * noffp - pointer to new file offset
4067 * ct - caller context
4069 * RETURN: 0 if success
4070 * EINVAL if new offset invalid
4074 zfs_seek(struct inode
*ip
, offset_t ooff
, offset_t
*noffp
)
4076 if (S_ISDIR(ip
->i_mode
))
4078 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4080 EXPORT_SYMBOL(zfs_seek
);
4083 * Fill pages with data from the disk.
4086 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4088 znode_t
*zp
= ITOZ(ip
);
4089 zfs_sb_t
*zsb
= ITOZSB(ip
);
4091 struct page
*cur_pp
;
4092 u_offset_t io_off
, total
;
4099 io_len
= nr_pages
<< PAGE_CACHE_SHIFT
;
4100 i_size
= i_size_read(ip
);
4101 io_off
= page_offset(pl
[0]);
4103 if (io_off
+ io_len
> i_size
)
4104 io_len
= i_size
- io_off
;
4107 * Iterate over list of pages and read each page individually.
4111 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4115 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4119 /* convert checksum errors into IO errors */
4121 err
= SET_ERROR(EIO
);
4124 cur_pp
= pl
[++page_idx
];
4131 * Uses zfs_fillpage to read data from the file and fill the pages.
4133 * IN: ip - inode of file to get data from.
4134 * pl - list of pages to read
4135 * nr_pages - number of pages to read
4137 * RETURN: 0 on success, error code on failure.
4140 * vp - atime updated
4144 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4146 znode_t
*zp
= ITOZ(ip
);
4147 zfs_sb_t
*zsb
= ITOZSB(ip
);
4156 err
= zfs_fillpage(ip
, pl
, nr_pages
);
4159 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
4164 EXPORT_SYMBOL(zfs_getpage
);
4167 * Check ZFS specific permissions to memory map a section of a file.
4169 * IN: ip - inode of the file to mmap
4171 * addrp - start address in memory region
4172 * len - length of memory region
4173 * vm_flags- address flags
4175 * RETURN: 0 if success
4176 * error code if failure
4180 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
4181 unsigned long vm_flags
)
4183 znode_t
*zp
= ITOZ(ip
);
4184 zfs_sb_t
*zsb
= ITOZSB(ip
);
4189 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
4190 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4192 return (SET_ERROR(EPERM
));
4195 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
4196 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4198 return (SET_ERROR(EACCES
));
4201 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4203 return (SET_ERROR(ENXIO
));
4209 EXPORT_SYMBOL(zfs_map
);
4212 * convoff - converts the given data (start, whence) to the
4216 convoff(struct inode
*ip
, flock64_t
*lckdat
, int whence
, offset_t offset
)
4221 if ((lckdat
->l_whence
== 2) || (whence
== 2)) {
4222 if ((error
= zfs_getattr(ip
, &vap
, 0, CRED()) != 0))
4226 switch (lckdat
->l_whence
) {
4228 lckdat
->l_start
+= offset
;
4231 lckdat
->l_start
+= vap
.va_size
;
4236 return (SET_ERROR(EINVAL
));
4239 if (lckdat
->l_start
< 0)
4240 return (SET_ERROR(EINVAL
));
4244 lckdat
->l_start
-= offset
;
4247 lckdat
->l_start
-= vap
.va_size
;
4252 return (SET_ERROR(EINVAL
));
4255 lckdat
->l_whence
= (short)whence
;
4260 * Free or allocate space in a file. Currently, this function only
4261 * supports the `F_FREESP' command. However, this command is somewhat
4262 * misnamed, as its functionality includes the ability to allocate as
4263 * well as free space.
4265 * IN: ip - inode of file to free data in.
4266 * cmd - action to take (only F_FREESP supported).
4267 * bfp - section of file to free/alloc.
4268 * flag - current file open mode flags.
4269 * offset - current file offset.
4270 * cr - credentials of caller [UNUSED].
4272 * RETURN: 0 on success, error code on failure.
4275 * ip - ctime|mtime updated
4279 zfs_space(struct inode
*ip
, int cmd
, flock64_t
*bfp
, int flag
,
4280 offset_t offset
, cred_t
*cr
)
4282 znode_t
*zp
= ITOZ(ip
);
4283 zfs_sb_t
*zsb
= ITOZSB(ip
);
4290 if (cmd
!= F_FREESP
) {
4292 return (SET_ERROR(EINVAL
));
4295 if ((error
= convoff(ip
, bfp
, 0, offset
))) {
4300 if (bfp
->l_len
< 0) {
4302 return (SET_ERROR(EINVAL
));
4306 * Permissions aren't checked on Solaris because on this OS
4307 * zfs_space() can only be called with an opened file handle.
4308 * On Linux we can get here through truncate_range() which
4309 * operates directly on inodes, so we need to check access rights.
4311 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
4317 len
= bfp
->l_len
; /* 0 means from off to end of file */
4319 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4324 EXPORT_SYMBOL(zfs_space
);
4328 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4330 znode_t
*zp
= ITOZ(ip
);
4331 zfs_sb_t
*zsb
= ITOZSB(ip
);
4334 uint64_t object
= zp
->z_id
;
4341 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
),
4342 &gen64
, sizeof (uint64_t))) != 0) {
4347 gen
= (uint32_t)gen64
;
4349 size
= (zsb
->z_parent
!= zsb
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4350 if (fidp
->fid_len
< size
) {
4351 fidp
->fid_len
= size
;
4353 return (SET_ERROR(ENOSPC
));
4356 zfid
= (zfid_short_t
*)fidp
;
4358 zfid
->zf_len
= size
;
4360 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4361 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4363 /* Must have a non-zero generation number to distinguish from .zfs */
4366 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4367 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4369 if (size
== LONG_FID_LEN
) {
4370 uint64_t objsetid
= dmu_objset_id(zsb
->z_os
);
4373 zlfid
= (zfid_long_t
*)fidp
;
4375 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4376 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4378 /* XXX - this should be the generation number for the objset */
4379 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4380 zlfid
->zf_setgen
[i
] = 0;
4386 EXPORT_SYMBOL(zfs_fid
);
4390 zfs_getsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4392 znode_t
*zp
= ITOZ(ip
);
4393 zfs_sb_t
*zsb
= ITOZSB(ip
);
4395 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4399 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
4404 EXPORT_SYMBOL(zfs_getsecattr
);
4408 zfs_setsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4410 znode_t
*zp
= ITOZ(ip
);
4411 zfs_sb_t
*zsb
= ITOZSB(ip
);
4413 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4414 zilog_t
*zilog
= zsb
->z_log
;
4419 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
4421 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4422 zil_commit(zilog
, 0);
4427 EXPORT_SYMBOL(zfs_setsecattr
);
4429 #ifdef HAVE_UIO_ZEROCOPY
4431 * Tunable, both must be a power of 2.
4433 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4434 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4435 * an arcbuf for a partial block read
4437 int zcr_blksz_min
= (1 << 10); /* 1K */
4438 int zcr_blksz_max
= (1 << 17); /* 128K */
4442 zfs_reqzcbuf(struct inode
*ip
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
)
4444 znode_t
*zp
= ITOZ(ip
);
4445 zfs_sb_t
*zsb
= ITOZSB(ip
);
4446 int max_blksz
= zsb
->z_max_blksz
;
4447 uio_t
*uio
= &xuio
->xu_uio
;
4448 ssize_t size
= uio
->uio_resid
;
4449 offset_t offset
= uio
->uio_loffset
;
4454 int preamble
, postamble
;
4456 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
4457 return (SET_ERROR(EINVAL
));
4464 * Loan out an arc_buf for write if write size is bigger than
4465 * max_blksz, and the file's block size is also max_blksz.
4468 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
4470 return (SET_ERROR(EINVAL
));
4473 * Caller requests buffers for write before knowing where the
4474 * write offset might be (e.g. NFS TCP write).
4479 preamble
= P2PHASE(offset
, blksz
);
4481 preamble
= blksz
- preamble
;
4486 postamble
= P2PHASE(size
, blksz
);
4489 fullblk
= size
/ blksz
;
4490 (void) dmu_xuio_init(xuio
,
4491 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
4494 * Have to fix iov base/len for partial buffers. They
4495 * currently represent full arc_buf's.
4498 /* data begins in the middle of the arc_buf */
4499 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4502 (void) dmu_xuio_add(xuio
, abuf
,
4503 blksz
- preamble
, preamble
);
4506 for (i
= 0; i
< fullblk
; i
++) {
4507 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4510 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
4514 /* data ends in the middle of the arc_buf */
4515 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4518 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
4523 * Loan out an arc_buf for read if the read size is larger than
4524 * the current file block size. Block alignment is not
4525 * considered. Partial arc_buf will be loaned out for read.
4527 blksz
= zp
->z_blksz
;
4528 if (blksz
< zcr_blksz_min
)
4529 blksz
= zcr_blksz_min
;
4530 if (blksz
> zcr_blksz_max
)
4531 blksz
= zcr_blksz_max
;
4532 /* avoid potential complexity of dealing with it */
4533 if (blksz
> max_blksz
) {
4535 return (SET_ERROR(EINVAL
));
4538 maxsize
= zp
->z_size
- uio
->uio_loffset
;
4544 return (SET_ERROR(EINVAL
));
4549 return (SET_ERROR(EINVAL
));
4552 uio
->uio_extflg
= UIO_XUIO
;
4553 XUIO_XUZC_RW(xuio
) = ioflag
;
4560 zfs_retzcbuf(struct inode
*ip
, xuio_t
*xuio
, cred_t
*cr
)
4564 int ioflag
= XUIO_XUZC_RW(xuio
);
4566 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
4568 i
= dmu_xuio_cnt(xuio
);
4570 abuf
= dmu_xuio_arcbuf(xuio
, i
);
4572 * if abuf == NULL, it must be a write buffer
4573 * that has been returned in zfs_write().
4576 dmu_return_arcbuf(abuf
);
4577 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
4580 dmu_xuio_fini(xuio
);
4583 #endif /* HAVE_UIO_ZEROCOPY */
4585 #if defined(_KERNEL) && defined(HAVE_SPL)
4586 module_param(zfs_read_chunk_size
, long, 0644);
4587 MODULE_PARM_DESC(zfs_read_chunk_size
, "Bytes to read per chunk");