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 */
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
35 #include <sys/resourcevar.h>
37 #include <sys/vnode.h>
41 #include <sys/taskq.h>
43 #include <sys/atomic.h>
44 #include <sys/namei.h>
46 #include <sys/cmn_err.h>
47 #include <sys/errno.h>
48 #include <sys/unistd.h>
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_ioctl.h>
51 #include <sys/fs/zfs.h>
53 #include <sys/dmu_objset.h>
59 #include <sys/dirent.h>
60 #include <sys/policy.h>
61 #include <sys/sunddi.h>
62 #include <sys/filio.h>
64 #include <sys/zfs_ctldir.h>
65 #include <sys/zfs_fuid.h>
66 #include <sys/zfs_sa.h>
68 #include <sys/zfs_rlock.h>
69 #include <sys/extdirent.h>
70 #include <sys/kidmap.h>
73 #include <sys/sf_buf.h>
74 #include <sys/sched.h>
76 #include <vm/vm_pageout.h>
81 * Each vnode op performs some logical unit of work. To do this, the ZPL must
82 * properly lock its in-core state, create a DMU transaction, do the work,
83 * record this work in the intent log (ZIL), commit the DMU transaction,
84 * and wait for the intent log to commit if it is a synchronous operation.
85 * Moreover, the vnode ops must work in both normal and log replay context.
86 * The ordering of events is important to avoid deadlocks and references
87 * to freed memory. The example below illustrates the following Big Rules:
89 * (1) A check must be made in each zfs thread for a mounted file system.
90 * This is done avoiding races using ZFS_ENTER(zfsvfs).
91 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
92 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
93 * can return EIO from the calling function.
95 * (2) VN_RELE() should always be the last thing except for zil_commit()
96 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
97 * First, if it's the last reference, the vnode/znode
98 * can be freed, so the zp may point to freed memory. Second, the last
99 * reference will call zfs_zinactive(), which may induce a lot of work --
100 * pushing cached pages (which acquires range locks) and syncing out
101 * cached atime changes. Third, zfs_zinactive() may require a new tx,
102 * which could deadlock the system if you were already holding one.
103 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
105 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
106 * as they can span dmu_tx_assign() calls.
108 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
109 * This is critical because we don't want to block while holding locks.
110 * Note, in particular, that if a lock is sometimes acquired before
111 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
112 * use a non-blocking assign can deadlock the system. The scenario:
114 * Thread A has grabbed a lock before calling dmu_tx_assign().
115 * Thread B is in an already-assigned tx, and blocks for this lock.
116 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
117 * forever, because the previous txg can't quiesce until B's tx commits.
119 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
120 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
121 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
122 * to indicate that this operation has already called dmu_tx_wait().
123 * This will ensure that we don't retry forever, waiting a short bit
126 * (5) If the operation succeeded, generate the intent log entry for it
127 * before dropping locks. This ensures that the ordering of events
128 * in the intent log matches the order in which they actually occurred.
129 * During ZIL replay the zfs_log_* functions will update the sequence
130 * number to indicate the zil transaction has replayed.
132 * (6) At the end of each vnode op, the DMU tx must always commit,
133 * regardless of whether there were any errors.
135 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
136 * to ensure that synchronous semantics are provided when necessary.
138 * In general, this is how things should be ordered in each vnode op:
140 * ZFS_ENTER(zfsvfs); // exit if unmounted
142 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
143 * rw_enter(...); // grab any other locks you need
144 * tx = dmu_tx_create(...); // get DMU tx
145 * dmu_tx_hold_*(); // hold each object you might modify
146 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
148 * rw_exit(...); // drop locks
149 * zfs_dirent_unlock(dl); // unlock directory entry
150 * VN_RELE(...); // release held vnodes
151 * if (error == ERESTART) {
157 * dmu_tx_abort(tx); // abort DMU tx
158 * ZFS_EXIT(zfsvfs); // finished in zfs
159 * return (error); // really out of space
161 * error = do_real_work(); // do whatever this VOP does
163 * zfs_log_*(...); // on success, make ZIL entry
164 * dmu_tx_commit(tx); // commit DMU tx -- error or not
165 * rw_exit(...); // drop locks
166 * zfs_dirent_unlock(dl); // unlock directory entry
167 * VN_RELE(...); // release held vnodes
168 * zil_commit(zilog, foid); // synchronous when necessary
169 * ZFS_EXIT(zfsvfs); // finished in zfs
170 * return (error); // done, report error
175 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
177 znode_t *zp = VTOZ(*vpp);
178 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
183 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
184 ((flag & FAPPEND) == 0)) {
186 return (SET_ERROR(EPERM));
189 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
190 ZTOV(zp)->v_type == VREG &&
191 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
192 if (fs_vscan(*vpp, cr, 0) != 0) {
194 return (SET_ERROR(EACCES));
198 /* Keep a count of the synchronous opens in the znode */
199 if (flag & (FSYNC | FDSYNC))
200 atomic_inc_32(&zp->z_sync_cnt);
208 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
209 caller_context_t *ct)
211 znode_t *zp = VTOZ(vp);
212 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
215 * Clean up any locks held by this process on the vp.
217 cleanlocks(vp, ddi_get_pid(), 0);
218 cleanshares(vp, ddi_get_pid());
223 /* Decrement the synchronous opens in the znode */
224 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
225 atomic_dec_32(&zp->z_sync_cnt);
227 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
228 ZTOV(zp)->v_type == VREG &&
229 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
230 VERIFY(fs_vscan(vp, cr, 1) == 0);
237 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
238 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
241 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
243 znode_t *zp = VTOZ(vp);
244 uint64_t noff = (uint64_t)*off; /* new offset */
249 file_sz = zp->z_size;
250 if (noff >= file_sz) {
251 return (SET_ERROR(ENXIO));
254 if (cmd == _FIO_SEEK_HOLE)
259 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
262 if ((error == ESRCH) || (noff > file_sz)) {
264 * Handle the virtual hole at the end of file.
270 return (SET_ERROR(ENXIO));
281 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
282 int *rvalp, caller_context_t *ct)
294 * The following two ioctls are used by bfu. Faking out,
295 * necessary to avoid bfu errors.
304 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
305 return (SET_ERROR(EFAULT));
307 off = *(offset_t *)data;
310 zfsvfs = zp->z_zfsvfs;
314 /* offset parameter is in/out */
315 error = zfs_holey(vp, com, &off);
320 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
321 return (SET_ERROR(EFAULT));
323 *(offset_t *)data = off;
327 return (SET_ERROR(ENOTTY));
331 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
338 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
339 * aligned boundaries, if the range is not aligned. As a result a
340 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
341 * It may happen that all DEV_BSIZE subranges are marked clean and thus
342 * the whole page would be considred clean despite have some dirty data.
343 * For this reason we should shrink the range to DEV_BSIZE aligned
344 * boundaries before calling vm_page_clear_dirty.
346 end = rounddown2(off + nbytes, DEV_BSIZE);
347 off = roundup2(off, DEV_BSIZE);
351 VM_OBJECT_LOCK_ASSERT(obj, MA_OWNED);
354 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
356 if (vm_page_sleep_if_busy(pp, FALSE, "zfsmwb"))
359 pp = vm_page_alloc(obj, OFF_TO_IDX(start),
360 VM_ALLOC_SYSTEM | VM_ALLOC_IFCACHED |
365 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
366 vm_object_pip_add(obj, 1);
367 vm_page_io_start(pp);
368 vm_page_lock_queues();
369 pmap_remove_write(pp);
371 vm_page_clear_dirty(pp, off, nbytes);
372 vm_page_unlock_queues();
380 page_unbusy(vm_page_t pp)
383 vm_page_io_finish(pp);
384 vm_object_pip_subtract(pp->object, 1);
388 page_hold(vnode_t *vp, int64_t start)
394 VM_OBJECT_LOCK_ASSERT(obj, MA_OWNED);
397 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
399 if (vm_page_sleep_if_busy(pp, FALSE, "zfsmwb"))
401 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
402 vm_page_lock_queues();
404 vm_page_unlock_queues();
414 page_unhold(vm_page_t pp)
417 vm_page_lock_queues();
419 vm_page_unlock_queues();
423 zfs_map_page(vm_page_t pp, struct sf_buf **sfp)
426 *sfp = sf_buf_alloc(pp, 0);
427 return ((caddr_t)sf_buf_kva(*sfp));
431 zfs_unmap_page(struct sf_buf *sf)
438 * When a file is memory mapped, we must keep the IO data synchronized
439 * between the DMU cache and the memory mapped pages. What this means:
441 * On Write: If we find a memory mapped page, we write to *both*
442 * the page and the dmu buffer.
445 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
446 int segflg, dmu_tx_t *tx)
453 ASSERT(vp->v_mount != NULL);
457 off = start & PAGEOFFSET;
459 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
461 int nbytes = imin(PAGESIZE - off, len);
463 if (segflg == UIO_NOCOPY) {
464 pp = vm_page_lookup(obj, OFF_TO_IDX(start));
466 ("zfs update_pages: NULL page in putpages case"));
468 ("zfs update_pages: unaligned data in putpages case"));
469 KASSERT(pp->valid == VM_PAGE_BITS_ALL,
470 ("zfs update_pages: invalid page in putpages case"));
471 KASSERT(pp->busy > 0,
472 ("zfs update_pages: unbusy page in putpages case"));
473 KASSERT((pp->flags & PG_WRITEABLE) == 0,
474 ("zfs update_pages: writable page in putpages case"));
475 VM_OBJECT_UNLOCK(obj);
477 va = zfs_map_page(pp, &sf);
478 (void) dmu_write(os, oid, start, nbytes, va, tx);
483 } else if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
484 VM_OBJECT_UNLOCK(obj);
486 va = zfs_map_page(pp, &sf);
487 (void) dmu_read(os, oid, start+off, nbytes,
488 va+off, DMU_READ_PREFETCH);;
497 if (segflg != UIO_NOCOPY)
498 vm_object_pip_wakeupn(obj, 0);
499 VM_OBJECT_UNLOCK(obj);
503 * Read with UIO_NOCOPY flag means that sendfile(2) requests
504 * ZFS to populate a range of page cache pages with data.
506 * NOTE: this function could be optimized to pre-allocate
507 * all pages in advance, drain VPO_BUSY on all of them,
508 * map them into contiguous KVA region and populate them
509 * in one single dmu_read() call.
512 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
514 znode_t *zp = VTOZ(vp);
515 objset_t *os = zp->z_zfsvfs->z_os;
525 ASSERT(uio->uio_segflg == UIO_NOCOPY);
526 ASSERT(vp->v_mount != NULL);
529 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
532 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
533 int bytes = MIN(PAGESIZE, len);
536 pp = vm_page_lookup(obj, OFF_TO_IDX(start));
537 if (pp != NULL && vm_page_sleep_if_busy(pp, FALSE,
541 pp = vm_page_alloc(obj, OFF_TO_IDX(start),
542 VM_ALLOC_NOBUSY | VM_ALLOC_NORMAL);
544 VM_OBJECT_UNLOCK(obj);
550 if (pp->valid == 0) {
551 vm_page_io_start(pp);
552 VM_OBJECT_UNLOCK(obj);
553 va = zfs_map_page(pp, &sf);
554 error = dmu_read(os, zp->z_id, start, bytes, va,
556 if (bytes != PAGESIZE && error == 0)
557 bzero(va + bytes, PAGESIZE - bytes);
560 vm_page_io_finish(pp);
561 vm_page_lock_queues();
563 if (pp->wire_count == 0 && pp->valid == 0 &&
564 pp->busy == 0 && !(pp->oflags & VPO_BUSY))
567 pp->valid = VM_PAGE_BITS_ALL;
568 vm_page_activate(pp);
570 vm_page_unlock_queues();
574 uio->uio_resid -= bytes;
575 uio->uio_offset += bytes;
578 VM_OBJECT_UNLOCK(obj);
583 * When a file is memory mapped, we must keep the IO data synchronized
584 * between the DMU cache and the memory mapped pages. What this means:
586 * On Read: We "read" preferentially from memory mapped pages,
587 * else we default from the dmu buffer.
589 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
590 * the file is memory mapped.
593 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
595 znode_t *zp = VTOZ(vp);
596 objset_t *os = zp->z_zfsvfs->z_os;
604 ASSERT(vp->v_mount != NULL);
608 start = uio->uio_loffset;
609 off = start & PAGEOFFSET;
611 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
613 uint64_t bytes = MIN(PAGESIZE - off, len);
615 if (pp = page_hold(vp, start)) {
618 VM_OBJECT_UNLOCK(obj);
619 va = zfs_map_page(pp, &sf);
620 error = uiomove(va + off, bytes, UIO_READ, uio);
625 VM_OBJECT_UNLOCK(obj);
626 error = dmu_read_uio(os, zp->z_id, uio, bytes);
634 VM_OBJECT_UNLOCK(obj);
638 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
641 * Read bytes from specified file into supplied buffer.
643 * IN: vp - vnode of file to be read from.
644 * uio - structure supplying read location, range info,
646 * ioflag - SYNC flags; used to provide FRSYNC semantics.
647 * cr - credentials of caller.
648 * ct - caller context
650 * OUT: uio - updated offset and range, buffer filled.
652 * RETURN: 0 on success, error code on failure.
655 * vp - atime updated if byte count > 0
659 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
661 znode_t *zp = VTOZ(vp);
662 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
673 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
675 return (SET_ERROR(EACCES));
679 * Validate file offset
681 if (uio->uio_loffset < (offset_t)0) {
683 return (SET_ERROR(EINVAL));
687 * Fasttrack empty reads
689 if (uio->uio_resid == 0) {
695 * Check for mandatory locks
697 if (MANDMODE(zp->z_mode)) {
698 if (error = chklock(vp, FREAD,
699 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
706 * If we're in FRSYNC mode, sync out this znode before reading it.
709 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
710 zil_commit(zfsvfs->z_log, zp->z_id);
713 * Lock the range against changes.
715 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
718 * If we are reading past end-of-file we can skip
719 * to the end; but we might still need to set atime.
721 if (uio->uio_loffset >= zp->z_size) {
726 ASSERT(uio->uio_loffset < zp->z_size);
727 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
730 if ((uio->uio_extflg == UIO_XUIO) &&
731 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
733 int blksz = zp->z_blksz;
734 uint64_t offset = uio->uio_loffset;
736 xuio = (xuio_t *)uio;
738 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
741 ASSERT(offset + n <= blksz);
744 (void) dmu_xuio_init(xuio, nblk);
746 if (vn_has_cached_data(vp)) {
748 * For simplicity, we always allocate a full buffer
749 * even if we only expect to read a portion of a block.
751 while (--nblk >= 0) {
752 (void) dmu_xuio_add(xuio,
753 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
761 nbytes = MIN(n, zfs_read_chunk_size -
762 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
765 if (uio->uio_segflg == UIO_NOCOPY)
766 error = mappedread_sf(vp, nbytes, uio);
768 #endif /* __FreeBSD__ */
769 if (vn_has_cached_data(vp))
770 error = mappedread(vp, nbytes, uio);
772 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
774 /* convert checksum errors into IO errors */
776 error = SET_ERROR(EIO);
783 zfs_range_unlock(rl);
785 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
791 * Write the bytes to a file.
793 * IN: vp - vnode of file to be written to.
794 * uio - structure supplying write location, range info,
796 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
797 * set if in append mode.
798 * cr - credentials of caller.
799 * ct - caller context (NFS/CIFS fem monitor only)
801 * OUT: uio - updated offset and range.
803 * RETURN: 0 on success, error code on failure.
806 * vp - ctime|mtime updated if byte count > 0
811 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
813 znode_t *zp = VTOZ(vp);
814 rlim64_t limit = MAXOFFSET_T;
815 ssize_t start_resid = uio->uio_resid;
819 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
824 int max_blksz = zfsvfs->z_max_blksz;
827 iovec_t *aiov = NULL;
830 int iovcnt = uio->uio_iovcnt;
831 iovec_t *iovp = uio->uio_iov;
834 sa_bulk_attr_t bulk[4];
835 uint64_t mtime[2], ctime[2];
838 * Fasttrack empty write
844 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
850 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
851 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
852 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
854 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
858 * If immutable or not appending then return EPERM
860 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
861 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
862 (uio->uio_loffset < zp->z_size))) {
864 return (SET_ERROR(EPERM));
867 zilog = zfsvfs->z_log;
870 * Validate file offset
872 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
875 return (SET_ERROR(EINVAL));
879 * Check for mandatory locks before calling zfs_range_lock()
880 * in order to prevent a deadlock with locks set via fcntl().
882 if (MANDMODE((mode_t)zp->z_mode) &&
883 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
890 * Pre-fault the pages to ensure slow (eg NFS) pages
892 * Skip this if uio contains loaned arc_buf.
894 if ((uio->uio_extflg == UIO_XUIO) &&
895 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
896 xuio = (xuio_t *)uio;
898 uio_prefaultpages(MIN(n, max_blksz), uio);
902 * If in append mode, set the io offset pointer to eof.
904 if (ioflag & FAPPEND) {
906 * Obtain an appending range lock to guarantee file append
907 * semantics. We reset the write offset once we have the lock.
909 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
911 if (rl->r_len == UINT64_MAX) {
913 * We overlocked the file because this write will cause
914 * the file block size to increase.
915 * Note that zp_size cannot change with this lock held.
919 uio->uio_loffset = woff;
922 * Note that if the file block size will change as a result of
923 * this write, then this range lock will lock the entire file
924 * so that we can re-write the block safely.
926 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
930 zfs_range_unlock(rl);
932 return (SET_ERROR(EFBIG));
935 if ((woff + n) > limit || woff > (limit - n))
938 /* Will this write extend the file length? */
939 write_eof = (woff + n > zp->z_size);
941 end_size = MAX(zp->z_size, woff + n);
944 * Write the file in reasonable size chunks. Each chunk is written
945 * in a separate transaction; this keeps the intent log records small
946 * and allows us to do more fine-grained space accounting.
950 woff = uio->uio_loffset;
952 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
953 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
955 dmu_return_arcbuf(abuf);
956 error = SET_ERROR(EDQUOT);
960 if (xuio && abuf == NULL) {
961 ASSERT(i_iov < iovcnt);
963 abuf = dmu_xuio_arcbuf(xuio, i_iov);
964 dmu_xuio_clear(xuio, i_iov);
965 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
966 iovec_t *, aiov, arc_buf_t *, abuf);
967 ASSERT((aiov->iov_base == abuf->b_data) ||
968 ((char *)aiov->iov_base - (char *)abuf->b_data +
969 aiov->iov_len == arc_buf_size(abuf)));
971 } else if (abuf == NULL && n >= max_blksz &&
972 woff >= zp->z_size &&
973 P2PHASE(woff, max_blksz) == 0 &&
974 zp->z_blksz == max_blksz) {
976 * This write covers a full block. "Borrow" a buffer
977 * from the dmu so that we can fill it before we enter
978 * a transaction. This avoids the possibility of
979 * holding up the transaction if the data copy hangs
980 * up on a pagefault (e.g., from an NFS server mapping).
984 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
986 ASSERT(abuf != NULL);
987 ASSERT(arc_buf_size(abuf) == max_blksz);
988 if (error = uiocopy(abuf->b_data, max_blksz,
989 UIO_WRITE, uio, &cbytes)) {
990 dmu_return_arcbuf(abuf);
993 ASSERT(cbytes == max_blksz);
997 * Start a transaction.
999 tx = dmu_tx_create(zfsvfs->z_os);
1000 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1001 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
1002 zfs_sa_upgrade_txholds(tx, zp);
1003 error = dmu_tx_assign(tx, TXG_NOWAIT);
1005 if (error == ERESTART) {
1012 dmu_return_arcbuf(abuf);
1017 * If zfs_range_lock() over-locked we grow the blocksize
1018 * and then reduce the lock range. This will only happen
1019 * on the first iteration since zfs_range_reduce() will
1020 * shrink down r_len to the appropriate size.
1022 if (rl->r_len == UINT64_MAX) {
1025 if (zp->z_blksz > max_blksz) {
1026 ASSERT(!ISP2(zp->z_blksz));
1027 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
1029 new_blksz = MIN(end_size, max_blksz);
1031 zfs_grow_blocksize(zp, new_blksz, tx);
1032 zfs_range_reduce(rl, woff, n);
1036 * XXX - should we really limit each write to z_max_blksz?
1037 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1039 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1041 if (woff + nbytes > zp->z_size)
1042 vnode_pager_setsize(vp, woff + nbytes);
1045 tx_bytes = uio->uio_resid;
1046 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1048 tx_bytes -= uio->uio_resid;
1051 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1053 * If this is not a full block write, but we are
1054 * extending the file past EOF and this data starts
1055 * block-aligned, use assign_arcbuf(). Otherwise,
1056 * write via dmu_write().
1058 if (tx_bytes < max_blksz && (!write_eof ||
1059 aiov->iov_base != abuf->b_data)) {
1061 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1062 aiov->iov_len, aiov->iov_base, tx);
1063 dmu_return_arcbuf(abuf);
1064 xuio_stat_wbuf_copied();
1066 ASSERT(xuio || tx_bytes == max_blksz);
1067 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1070 ASSERT(tx_bytes <= uio->uio_resid);
1071 uioskip(uio, tx_bytes);
1073 if (tx_bytes && vn_has_cached_data(vp)) {
1074 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1075 zp->z_id, uio->uio_segflg, tx);
1079 * If we made no progress, we're done. If we made even
1080 * partial progress, update the znode and ZIL accordingly.
1082 if (tx_bytes == 0) {
1083 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1084 (void *)&zp->z_size, sizeof (uint64_t), tx);
1091 * Clear Set-UID/Set-GID bits on successful write if not
1092 * privileged and at least one of the excute bits is set.
1094 * It would be nice to to this after all writes have
1095 * been done, but that would still expose the ISUID/ISGID
1096 * to another app after the partial write is committed.
1098 * Note: we don't call zfs_fuid_map_id() here because
1099 * user 0 is not an ephemeral uid.
1101 mutex_enter(&zp->z_acl_lock);
1102 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1103 (S_IXUSR >> 6))) != 0 &&
1104 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1105 secpolicy_vnode_setid_retain(vp, cr,
1106 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1108 zp->z_mode &= ~(S_ISUID | S_ISGID);
1109 newmode = zp->z_mode;
1110 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1111 (void *)&newmode, sizeof (uint64_t), tx);
1113 mutex_exit(&zp->z_acl_lock);
1115 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1119 * Update the file size (zp_size) if it has changed;
1120 * account for possible concurrent updates.
1122 while ((end_size = zp->z_size) < uio->uio_loffset) {
1123 (void) atomic_cas_64(&zp->z_size, end_size,
1128 * If we are replaying and eof is non zero then force
1129 * the file size to the specified eof. Note, there's no
1130 * concurrency during replay.
1132 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1133 zp->z_size = zfsvfs->z_replay_eof;
1135 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1137 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1142 ASSERT(tx_bytes == nbytes);
1147 uio_prefaultpages(MIN(n, max_blksz), uio);
1151 zfs_range_unlock(rl);
1154 * If we're in replay mode, or we made no progress, return error.
1155 * Otherwise, it's at least a partial write, so it's successful.
1157 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1162 if (ioflag & (FSYNC | FDSYNC) ||
1163 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1164 zil_commit(zilog, zp->z_id);
1171 zfs_get_done(zgd_t *zgd, int error)
1173 znode_t *zp = zgd->zgd_private;
1174 objset_t *os = zp->z_zfsvfs->z_os;
1178 dmu_buf_rele(zgd->zgd_db, zgd);
1180 zfs_range_unlock(zgd->zgd_rl);
1182 vfslocked = VFS_LOCK_GIANT(zp->z_zfsvfs->z_vfs);
1184 * Release the vnode asynchronously as we currently have the
1185 * txg stopped from syncing.
1187 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1189 if (error == 0 && zgd->zgd_bp)
1190 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1192 kmem_free(zgd, sizeof (zgd_t));
1193 VFS_UNLOCK_GIANT(vfslocked);
1197 static int zil_fault_io = 0;
1201 * Get data to generate a TX_WRITE intent log record.
1204 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1206 zfsvfs_t *zfsvfs = arg;
1207 objset_t *os = zfsvfs->z_os;
1209 uint64_t object = lr->lr_foid;
1210 uint64_t offset = lr->lr_offset;
1211 uint64_t size = lr->lr_length;
1212 blkptr_t *bp = &lr->lr_blkptr;
1217 ASSERT(zio != NULL);
1221 * Nothing to do if the file has been removed
1223 if (zfs_zget(zfsvfs, object, &zp) != 0)
1224 return (SET_ERROR(ENOENT));
1225 if (zp->z_unlinked) {
1227 * Release the vnode asynchronously as we currently have the
1228 * txg stopped from syncing.
1230 VN_RELE_ASYNC(ZTOV(zp),
1231 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1232 return (SET_ERROR(ENOENT));
1235 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1236 zgd->zgd_zilog = zfsvfs->z_log;
1237 zgd->zgd_private = zp;
1240 * Write records come in two flavors: immediate and indirect.
1241 * For small writes it's cheaper to store the data with the
1242 * log record (immediate); for large writes it's cheaper to
1243 * sync the data and get a pointer to it (indirect) so that
1244 * we don't have to write the data twice.
1246 if (buf != NULL) { /* immediate write */
1247 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1248 /* test for truncation needs to be done while range locked */
1249 if (offset >= zp->z_size) {
1250 error = SET_ERROR(ENOENT);
1252 error = dmu_read(os, object, offset, size, buf,
1253 DMU_READ_NO_PREFETCH);
1255 ASSERT(error == 0 || error == ENOENT);
1256 } else { /* indirect write */
1258 * Have to lock the whole block to ensure when it's
1259 * written out and it's checksum is being calculated
1260 * that no one can change the data. We need to re-check
1261 * blocksize after we get the lock in case it's changed!
1266 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1268 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1270 if (zp->z_blksz == size)
1273 zfs_range_unlock(zgd->zgd_rl);
1275 /* test for truncation needs to be done while range locked */
1276 if (lr->lr_offset >= zp->z_size)
1277 error = SET_ERROR(ENOENT);
1280 error = SET_ERROR(EIO);
1285 error = dmu_buf_hold(os, object, offset, zgd, &db,
1286 DMU_READ_NO_PREFETCH);
1289 blkptr_t *obp = dmu_buf_get_blkptr(db);
1291 ASSERT(BP_IS_HOLE(bp));
1298 ASSERT(db->db_offset == offset);
1299 ASSERT(db->db_size == size);
1301 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1303 ASSERT(error || lr->lr_length <= zp->z_blksz);
1306 * On success, we need to wait for the write I/O
1307 * initiated by dmu_sync() to complete before we can
1308 * release this dbuf. We will finish everything up
1309 * in the zfs_get_done() callback.
1314 if (error == EALREADY) {
1315 lr->lr_common.lrc_txtype = TX_WRITE2;
1321 zfs_get_done(zgd, error);
1328 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1329 caller_context_t *ct)
1331 znode_t *zp = VTOZ(vp);
1332 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1338 if (flag & V_ACE_MASK)
1339 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1341 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1348 * If vnode is for a device return a specfs vnode instead.
1351 specvp_check(vnode_t **vpp, cred_t *cr)
1355 if (IS_DEVVP(*vpp)) {
1358 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1361 error = SET_ERROR(ENOSYS);
1369 * Lookup an entry in a directory, or an extended attribute directory.
1370 * If it exists, return a held vnode reference for it.
1372 * IN: dvp - vnode of directory to search.
1373 * nm - name of entry to lookup.
1374 * pnp - full pathname to lookup [UNUSED].
1375 * flags - LOOKUP_XATTR set if looking for an attribute.
1376 * rdir - root directory vnode [UNUSED].
1377 * cr - credentials of caller.
1378 * ct - caller context
1379 * direntflags - directory lookup flags
1380 * realpnp - returned pathname.
1382 * OUT: vpp - vnode of located entry, NULL if not found.
1384 * RETURN: 0 on success, error code on failure.
1391 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1392 int nameiop, cred_t *cr, kthread_t *td, int flags)
1394 znode_t *zdp = VTOZ(dvp);
1395 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1397 int *direntflags = NULL;
1398 void *realpnp = NULL;
1401 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1403 if (dvp->v_type != VDIR) {
1404 return (SET_ERROR(ENOTDIR));
1405 } else if (zdp->z_sa_hdl == NULL) {
1406 return (SET_ERROR(EIO));
1409 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1410 error = zfs_fastaccesschk_execute(zdp, cr);
1418 vnode_t *tvp = dnlc_lookup(dvp, nm);
1421 error = zfs_fastaccesschk_execute(zdp, cr);
1426 if (tvp == DNLC_NO_VNODE) {
1428 return (SET_ERROR(ENOENT));
1431 return (specvp_check(vpp, cr));
1437 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1444 if (flags & LOOKUP_XATTR) {
1447 * If the xattr property is off, refuse the lookup request.
1449 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1451 return (SET_ERROR(EINVAL));
1456 * We don't allow recursive attributes..
1457 * Maybe someday we will.
1459 if (zdp->z_pflags & ZFS_XATTR) {
1461 return (SET_ERROR(EINVAL));
1464 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1470 * Do we have permission to get into attribute directory?
1473 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1483 if (dvp->v_type != VDIR) {
1485 return (SET_ERROR(ENOTDIR));
1489 * Check accessibility of directory.
1492 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1497 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1498 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1500 return (SET_ERROR(EILSEQ));
1503 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1505 error = specvp_check(vpp, cr);
1507 /* Translate errors and add SAVENAME when needed. */
1508 if (cnp->cn_flags & ISLASTCN) {
1512 if (error == ENOENT) {
1513 error = EJUSTRETURN;
1514 cnp->cn_flags |= SAVENAME;
1520 cnp->cn_flags |= SAVENAME;
1524 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) {
1527 if (cnp->cn_flags & ISDOTDOT) {
1528 ltype = VOP_ISLOCKED(dvp);
1532 error = vn_lock(*vpp, cnp->cn_lkflags);
1533 if (cnp->cn_flags & ISDOTDOT)
1534 vn_lock(dvp, ltype | LK_RETRY);
1544 #ifdef FREEBSD_NAMECACHE
1546 * Insert name into cache (as non-existent) if appropriate.
1548 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
1549 cache_enter(dvp, *vpp, cnp);
1551 * Insert name into cache if appropriate.
1553 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1554 if (!(cnp->cn_flags & ISLASTCN) ||
1555 (nameiop != DELETE && nameiop != RENAME)) {
1556 cache_enter(dvp, *vpp, cnp);
1565 * Attempt to create a new entry in a directory. If the entry
1566 * already exists, truncate the file if permissible, else return
1567 * an error. Return the vp of the created or trunc'd file.
1569 * IN: dvp - vnode of directory to put new file entry in.
1570 * name - name of new file entry.
1571 * vap - attributes of new file.
1572 * excl - flag indicating exclusive or non-exclusive mode.
1573 * mode - mode to open file with.
1574 * cr - credentials of caller.
1575 * flag - large file flag [UNUSED].
1576 * ct - caller context
1577 * vsecp - ACL to be set
1579 * OUT: vpp - vnode of created or trunc'd entry.
1581 * RETURN: 0 on success, error code on failure.
1584 * dvp - ctime|mtime updated if new entry created
1585 * vp - ctime|mtime always, atime if new
1590 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1591 vnode_t **vpp, cred_t *cr, kthread_t *td)
1593 znode_t *zp, *dzp = VTOZ(dvp);
1594 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1602 gid_t gid = crgetgid(cr);
1603 zfs_acl_ids_t acl_ids;
1604 boolean_t fuid_dirtied;
1605 boolean_t have_acl = B_FALSE;
1606 boolean_t waited = B_FALSE;
1611 * If we have an ephemeral id, ACL, or XVATTR then
1612 * make sure file system is at proper version
1615 ksid = crgetsid(cr, KSID_OWNER);
1617 uid = ksid_getid(ksid);
1621 if (zfsvfs->z_use_fuids == B_FALSE &&
1622 (vsecp || (vap->va_mask & AT_XVATTR) ||
1623 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1624 return (SET_ERROR(EINVAL));
1629 zilog = zfsvfs->z_log;
1631 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1632 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1634 return (SET_ERROR(EILSEQ));
1637 if (vap->va_mask & AT_XVATTR) {
1638 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1639 crgetuid(cr), cr, vap->va_type)) != 0) {
1647 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1648 vap->va_mode &= ~S_ISVTX;
1650 if (*name == '\0') {
1652 * Null component name refers to the directory itself.
1659 /* possible VN_HOLD(zp) */
1662 if (flag & FIGNORECASE)
1665 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1669 zfs_acl_ids_free(&acl_ids);
1670 if (strcmp(name, "..") == 0)
1671 error = SET_ERROR(EISDIR);
1681 * Create a new file object and update the directory
1684 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1686 zfs_acl_ids_free(&acl_ids);
1691 * We only support the creation of regular files in
1692 * extended attribute directories.
1695 if ((dzp->z_pflags & ZFS_XATTR) &&
1696 (vap->va_type != VREG)) {
1698 zfs_acl_ids_free(&acl_ids);
1699 error = SET_ERROR(EINVAL);
1703 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1704 cr, vsecp, &acl_ids)) != 0)
1708 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1709 zfs_acl_ids_free(&acl_ids);
1710 error = SET_ERROR(EDQUOT);
1714 tx = dmu_tx_create(os);
1716 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1717 ZFS_SA_BASE_ATTR_SIZE);
1719 fuid_dirtied = zfsvfs->z_fuid_dirty;
1721 zfs_fuid_txhold(zfsvfs, tx);
1722 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1723 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1724 if (!zfsvfs->z_use_sa &&
1725 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1726 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1727 0, acl_ids.z_aclp->z_acl_bytes);
1729 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1731 zfs_dirent_unlock(dl);
1732 if (error == ERESTART) {
1738 zfs_acl_ids_free(&acl_ids);
1743 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1746 zfs_fuid_sync(zfsvfs, tx);
1748 (void) zfs_link_create(dl, zp, tx, ZNEW);
1749 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1750 if (flag & FIGNORECASE)
1752 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1753 vsecp, acl_ids.z_fuidp, vap);
1754 zfs_acl_ids_free(&acl_ids);
1757 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1760 zfs_acl_ids_free(&acl_ids);
1764 * A directory entry already exists for this name.
1767 * Can't truncate an existing file if in exclusive mode.
1770 error = SET_ERROR(EEXIST);
1774 * Can't open a directory for writing.
1776 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1777 error = SET_ERROR(EISDIR);
1781 * Verify requested access to file.
1783 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1787 mutex_enter(&dzp->z_lock);
1789 mutex_exit(&dzp->z_lock);
1792 * Truncate regular files if requested.
1794 if ((ZTOV(zp)->v_type == VREG) &&
1795 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1796 /* we can't hold any locks when calling zfs_freesp() */
1797 zfs_dirent_unlock(dl);
1799 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1801 vnevent_create(ZTOV(zp), ct);
1807 zfs_dirent_unlock(dl);
1814 error = specvp_check(vpp, cr);
1817 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1818 zil_commit(zilog, 0);
1825 * Remove an entry from a directory.
1827 * IN: dvp - vnode of directory to remove entry from.
1828 * name - name of entry to remove.
1829 * cr - credentials of caller.
1830 * ct - caller context
1831 * flags - case flags
1833 * RETURN: 0 on success, error code on failure.
1837 * vp - ctime (if nlink > 0)
1840 uint64_t null_xattr = 0;
1844 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1847 znode_t *zp, *dzp = VTOZ(dvp);
1850 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1852 uint64_t acl_obj, xattr_obj;
1853 uint64_t xattr_obj_unlinked = 0;
1857 boolean_t may_delete_now, delete_now = FALSE;
1858 boolean_t unlinked, toobig = FALSE;
1860 pathname_t *realnmp = NULL;
1864 boolean_t waited = B_FALSE;
1868 zilog = zfsvfs->z_log;
1870 if (flags & FIGNORECASE) {
1880 * Attempt to lock directory; fail if entry doesn't exist.
1882 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1892 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1897 * Need to use rmdir for removing directories.
1899 if (vp->v_type == VDIR) {
1900 error = SET_ERROR(EPERM);
1904 vnevent_remove(vp, dvp, name, ct);
1907 dnlc_remove(dvp, realnmp->pn_buf);
1909 dnlc_remove(dvp, name);
1912 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1916 * We may delete the znode now, or we may put it in the unlinked set;
1917 * it depends on whether we're the last link, and on whether there are
1918 * other holds on the vnode. So we dmu_tx_hold() the right things to
1919 * allow for either case.
1922 tx = dmu_tx_create(zfsvfs->z_os);
1923 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1924 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1925 zfs_sa_upgrade_txholds(tx, zp);
1926 zfs_sa_upgrade_txholds(tx, dzp);
1927 if (may_delete_now) {
1929 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1930 /* if the file is too big, only hold_free a token amount */
1931 dmu_tx_hold_free(tx, zp->z_id, 0,
1932 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1935 /* are there any extended attributes? */
1936 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1937 &xattr_obj, sizeof (xattr_obj));
1938 if (error == 0 && xattr_obj) {
1939 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1941 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1942 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1945 mutex_enter(&zp->z_lock);
1946 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1947 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1948 mutex_exit(&zp->z_lock);
1950 /* charge as an update -- would be nice not to charge at all */
1951 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1953 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1955 zfs_dirent_unlock(dl);
1959 if (error == ERESTART) {
1973 * Remove the directory entry.
1975 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1985 * Hold z_lock so that we can make sure that the ACL obj
1986 * hasn't changed. Could have been deleted due to
1989 mutex_enter(&zp->z_lock);
1991 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1992 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1993 delete_now = may_delete_now && !toobig &&
1994 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1995 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
2002 panic("zfs_remove: delete_now branch taken");
2004 if (xattr_obj_unlinked) {
2005 ASSERT3U(xzp->z_links, ==, 2);
2006 mutex_enter(&xzp->z_lock);
2007 xzp->z_unlinked = 1;
2009 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
2010 &xzp->z_links, sizeof (xzp->z_links), tx);
2011 ASSERT3U(error, ==, 0);
2012 mutex_exit(&xzp->z_lock);
2013 zfs_unlinked_add(xzp, tx);
2016 error = sa_remove(zp->z_sa_hdl,
2017 SA_ZPL_XATTR(zfsvfs), tx);
2019 error = sa_update(zp->z_sa_hdl,
2020 SA_ZPL_XATTR(zfsvfs), &null_xattr,
2021 sizeof (uint64_t), tx);
2026 ASSERT0(vp->v_count);
2028 mutex_exit(&zp->z_lock);
2029 zfs_znode_delete(zp, tx);
2030 } else if (unlinked) {
2031 mutex_exit(&zp->z_lock);
2032 zfs_unlinked_add(zp, tx);
2034 vp->v_vflag |= VV_NOSYNC;
2039 if (flags & FIGNORECASE)
2041 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2048 zfs_dirent_unlock(dl);
2055 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2056 zil_commit(zilog, 0);
2063 * Create a new directory and insert it into dvp using the name
2064 * provided. Return a pointer to the inserted directory.
2066 * IN: dvp - vnode of directory to add subdir to.
2067 * dirname - name of new directory.
2068 * vap - attributes of new directory.
2069 * cr - credentials of caller.
2070 * ct - caller context
2071 * flags - case flags
2072 * vsecp - ACL to be set
2074 * OUT: vpp - vnode of created directory.
2076 * RETURN: 0 on success, error code on failure.
2079 * dvp - ctime|mtime updated
2080 * vp - ctime|mtime|atime updated
2084 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
2085 caller_context_t *ct, int flags, vsecattr_t *vsecp)
2087 znode_t *zp, *dzp = VTOZ(dvp);
2088 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2097 gid_t gid = crgetgid(cr);
2098 zfs_acl_ids_t acl_ids;
2099 boolean_t fuid_dirtied;
2100 boolean_t waited = B_FALSE;
2102 ASSERT(vap->va_type == VDIR);
2105 * If we have an ephemeral id, ACL, or XVATTR then
2106 * make sure file system is at proper version
2109 ksid = crgetsid(cr, KSID_OWNER);
2111 uid = ksid_getid(ksid);
2114 if (zfsvfs->z_use_fuids == B_FALSE &&
2115 (vsecp || (vap->va_mask & AT_XVATTR) ||
2116 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2117 return (SET_ERROR(EINVAL));
2121 zilog = zfsvfs->z_log;
2123 if (dzp->z_pflags & ZFS_XATTR) {
2125 return (SET_ERROR(EINVAL));
2128 if (zfsvfs->z_utf8 && u8_validate(dirname,
2129 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2131 return (SET_ERROR(EILSEQ));
2133 if (flags & FIGNORECASE)
2136 if (vap->va_mask & AT_XVATTR) {
2137 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2138 crgetuid(cr), cr, vap->va_type)) != 0) {
2144 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2145 vsecp, &acl_ids)) != 0) {
2150 * First make sure the new directory doesn't exist.
2152 * Existence is checked first to make sure we don't return
2153 * EACCES instead of EEXIST which can cause some applications
2159 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
2161 zfs_acl_ids_free(&acl_ids);
2166 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2167 zfs_acl_ids_free(&acl_ids);
2168 zfs_dirent_unlock(dl);
2173 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2174 zfs_acl_ids_free(&acl_ids);
2175 zfs_dirent_unlock(dl);
2177 return (SET_ERROR(EDQUOT));
2181 * Add a new entry to the directory.
2183 tx = dmu_tx_create(zfsvfs->z_os);
2184 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2185 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2186 fuid_dirtied = zfsvfs->z_fuid_dirty;
2188 zfs_fuid_txhold(zfsvfs, tx);
2189 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2190 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2191 acl_ids.z_aclp->z_acl_bytes);
2194 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2195 ZFS_SA_BASE_ATTR_SIZE);
2197 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2199 zfs_dirent_unlock(dl);
2200 if (error == ERESTART) {
2206 zfs_acl_ids_free(&acl_ids);
2215 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2218 zfs_fuid_sync(zfsvfs, tx);
2221 * Now put new name in parent dir.
2223 (void) zfs_link_create(dl, zp, tx, ZNEW);
2227 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2228 if (flags & FIGNORECASE)
2230 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2231 acl_ids.z_fuidp, vap);
2233 zfs_acl_ids_free(&acl_ids);
2237 zfs_dirent_unlock(dl);
2239 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2240 zil_commit(zilog, 0);
2247 * Remove a directory subdir entry. If the current working
2248 * directory is the same as the subdir to be removed, the
2251 * IN: dvp - vnode of directory to remove from.
2252 * name - name of directory to be removed.
2253 * cwd - vnode of current working directory.
2254 * cr - credentials of caller.
2255 * ct - caller context
2256 * flags - case flags
2258 * RETURN: 0 on success, error code on failure.
2261 * dvp - ctime|mtime updated
2265 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2266 caller_context_t *ct, int flags)
2268 znode_t *dzp = VTOZ(dvp);
2271 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2277 boolean_t waited = B_FALSE;
2281 zilog = zfsvfs->z_log;
2283 if (flags & FIGNORECASE)
2289 * Attempt to lock directory; fail if entry doesn't exist.
2291 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2299 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2303 if (vp->v_type != VDIR) {
2304 error = SET_ERROR(ENOTDIR);
2309 error = SET_ERROR(EINVAL);
2313 vnevent_rmdir(vp, dvp, name, ct);
2316 * Grab a lock on the directory to make sure that noone is
2317 * trying to add (or lookup) entries while we are removing it.
2319 rw_enter(&zp->z_name_lock, RW_WRITER);
2322 * Grab a lock on the parent pointer to make sure we play well
2323 * with the treewalk and directory rename code.
2325 rw_enter(&zp->z_parent_lock, RW_WRITER);
2327 tx = dmu_tx_create(zfsvfs->z_os);
2328 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2329 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2330 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2331 zfs_sa_upgrade_txholds(tx, zp);
2332 zfs_sa_upgrade_txholds(tx, dzp);
2333 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2335 rw_exit(&zp->z_parent_lock);
2336 rw_exit(&zp->z_name_lock);
2337 zfs_dirent_unlock(dl);
2339 if (error == ERESTART) {
2350 #ifdef FREEBSD_NAMECACHE
2354 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2357 uint64_t txtype = TX_RMDIR;
2358 if (flags & FIGNORECASE)
2360 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2365 rw_exit(&zp->z_parent_lock);
2366 rw_exit(&zp->z_name_lock);
2367 #ifdef FREEBSD_NAMECACHE
2371 zfs_dirent_unlock(dl);
2375 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2376 zil_commit(zilog, 0);
2383 * Read as many directory entries as will fit into the provided
2384 * buffer from the given directory cursor position (specified in
2385 * the uio structure).
2387 * IN: vp - vnode of directory to read.
2388 * uio - structure supplying read location, range info,
2389 * and return buffer.
2390 * cr - credentials of caller.
2391 * ct - caller context
2392 * flags - case flags
2394 * OUT: uio - updated offset and range, buffer filled.
2395 * eofp - set to true if end-of-file detected.
2397 * RETURN: 0 on success, error code on failure.
2400 * vp - atime updated
2402 * Note that the low 4 bits of the cookie returned by zap is always zero.
2403 * This allows us to use the low range for "special" directory entries:
2404 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2405 * we use the offset 2 for the '.zfs' directory.
2409 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2411 znode_t *zp = VTOZ(vp);
2415 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2420 zap_attribute_t zap;
2421 uint_t bytes_wanted;
2422 uint64_t offset; /* must be unsigned; checks for < 1 */
2428 boolean_t check_sysattrs;
2431 u_long *cooks = NULL;
2437 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2438 &parent, sizeof (parent))) != 0) {
2444 * If we are not given an eof variable,
2451 * Check for valid iov_len.
2453 if (uio->uio_iov->iov_len <= 0) {
2455 return (SET_ERROR(EINVAL));
2459 * Quit if directory has been removed (posix)
2461 if ((*eofp = zp->z_unlinked) != 0) {
2468 offset = uio->uio_loffset;
2469 prefetch = zp->z_zn_prefetch;
2472 * Initialize the iterator cursor.
2476 * Start iteration from the beginning of the directory.
2478 zap_cursor_init(&zc, os, zp->z_id);
2481 * The offset is a serialized cursor.
2483 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2487 * Get space to change directory entries into fs independent format.
2489 iovp = uio->uio_iov;
2490 bytes_wanted = iovp->iov_len;
2491 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2492 bufsize = bytes_wanted;
2493 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2494 odp = (struct dirent64 *)outbuf;
2496 bufsize = bytes_wanted;
2498 odp = (struct dirent64 *)iovp->iov_base;
2500 eodp = (struct edirent *)odp;
2502 if (ncookies != NULL) {
2504 * Minimum entry size is dirent size and 1 byte for a file name.
2506 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2507 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2512 * If this VFS supports the system attribute view interface; and
2513 * we're looking at an extended attribute directory; and we care
2514 * about normalization conflicts on this vfs; then we must check
2515 * for normalization conflicts with the sysattr name space.
2518 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2519 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2520 (flags & V_RDDIR_ENTFLAGS);
2526 * Transform to file-system independent format
2529 while (outcount < bytes_wanted) {
2532 off64_t *next = NULL;
2535 * Special case `.', `..', and `.zfs'.
2538 (void) strcpy(zap.za_name, ".");
2539 zap.za_normalization_conflict = 0;
2542 } else if (offset == 1) {
2543 (void) strcpy(zap.za_name, "..");
2544 zap.za_normalization_conflict = 0;
2547 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2548 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2549 zap.za_normalization_conflict = 0;
2550 objnum = ZFSCTL_INO_ROOT;
2556 if (error = zap_cursor_retrieve(&zc, &zap)) {
2557 if ((*eofp = (error == ENOENT)) != 0)
2563 if (zap.za_integer_length != 8 ||
2564 zap.za_num_integers != 1) {
2565 cmn_err(CE_WARN, "zap_readdir: bad directory "
2566 "entry, obj = %lld, offset = %lld\n",
2567 (u_longlong_t)zp->z_id,
2568 (u_longlong_t)offset);
2569 error = SET_ERROR(ENXIO);
2573 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2575 * MacOS X can extract the object type here such as:
2576 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2578 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2580 if (check_sysattrs && !zap.za_normalization_conflict) {
2582 zap.za_normalization_conflict =
2583 xattr_sysattr_casechk(zap.za_name);
2585 panic("%s:%u: TODO", __func__, __LINE__);
2590 if (flags & V_RDDIR_ACCFILTER) {
2592 * If we have no access at all, don't include
2593 * this entry in the returned information
2596 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2598 if (!zfs_has_access(ezp, cr)) {
2605 if (flags & V_RDDIR_ENTFLAGS)
2606 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2608 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2611 * Will this entry fit in the buffer?
2613 if (outcount + reclen > bufsize) {
2615 * Did we manage to fit anything in the buffer?
2618 error = SET_ERROR(EINVAL);
2623 if (flags & V_RDDIR_ENTFLAGS) {
2625 * Add extended flag entry:
2627 eodp->ed_ino = objnum;
2628 eodp->ed_reclen = reclen;
2629 /* NOTE: ed_off is the offset for the *next* entry */
2630 next = &(eodp->ed_off);
2631 eodp->ed_eflags = zap.za_normalization_conflict ?
2632 ED_CASE_CONFLICT : 0;
2633 (void) strncpy(eodp->ed_name, zap.za_name,
2634 EDIRENT_NAMELEN(reclen));
2635 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2640 odp->d_ino = objnum;
2641 odp->d_reclen = reclen;
2642 odp->d_namlen = strlen(zap.za_name);
2643 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2645 odp = (dirent64_t *)((intptr_t)odp + reclen);
2649 ASSERT(outcount <= bufsize);
2651 /* Prefetch znode */
2653 dmu_prefetch(os, objnum, 0, 0);
2657 * Move to the next entry, fill in the previous offset.
2659 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2660 zap_cursor_advance(&zc);
2661 offset = zap_cursor_serialize(&zc);
2666 if (cooks != NULL) {
2669 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2672 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2674 /* Subtract unused cookies */
2675 if (ncookies != NULL)
2676 *ncookies -= ncooks;
2678 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2679 iovp->iov_base += outcount;
2680 iovp->iov_len -= outcount;
2681 uio->uio_resid -= outcount;
2682 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2684 * Reset the pointer.
2686 offset = uio->uio_loffset;
2690 zap_cursor_fini(&zc);
2691 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2692 kmem_free(outbuf, bufsize);
2694 if (error == ENOENT)
2697 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2699 uio->uio_loffset = offset;
2701 if (error != 0 && cookies != NULL) {
2702 free(*cookies, M_TEMP);
2709 ulong_t zfs_fsync_sync_cnt = 4;
2712 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2714 znode_t *zp = VTOZ(vp);
2715 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2717 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2719 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2722 zil_commit(zfsvfs->z_log, zp->z_id);
2730 * Get the requested file attributes and place them in the provided
2733 * IN: vp - vnode of file.
2734 * vap - va_mask identifies requested attributes.
2735 * If AT_XVATTR set, then optional attrs are requested
2736 * flags - ATTR_NOACLCHECK (CIFS server context)
2737 * cr - credentials of caller.
2738 * ct - caller context
2740 * OUT: vap - attribute values.
2742 * RETURN: 0 (always succeeds).
2746 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2747 caller_context_t *ct)
2749 znode_t *zp = VTOZ(vp);
2750 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2753 u_longlong_t nblocks;
2755 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2756 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2757 xoptattr_t *xoap = NULL;
2758 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2759 sa_bulk_attr_t bulk[4];
2765 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2767 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2768 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2769 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2770 if (vp->v_type == VBLK || vp->v_type == VCHR)
2771 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2774 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2780 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2781 * Also, if we are the owner don't bother, since owner should
2782 * always be allowed to read basic attributes of file.
2784 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2785 (vap->va_uid != crgetuid(cr))) {
2786 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2794 * Return all attributes. It's cheaper to provide the answer
2795 * than to determine whether we were asked the question.
2798 mutex_enter(&zp->z_lock);
2799 vap->va_type = IFTOVT(zp->z_mode);
2800 vap->va_mode = zp->z_mode & ~S_IFMT;
2802 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2804 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2806 vap->va_nodeid = zp->z_id;
2807 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2808 links = zp->z_links + 1;
2810 links = zp->z_links;
2811 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2812 vap->va_size = zp->z_size;
2814 vap->va_rdev = vp->v_rdev;
2816 if (vp->v_type == VBLK || vp->v_type == VCHR)
2817 vap->va_rdev = zfs_cmpldev(rdev);
2819 vap->va_seq = zp->z_seq;
2820 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2823 * Add in any requested optional attributes and the create time.
2824 * Also set the corresponding bits in the returned attribute bitmap.
2826 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2827 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2829 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2830 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2833 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2834 xoap->xoa_readonly =
2835 ((zp->z_pflags & ZFS_READONLY) != 0);
2836 XVA_SET_RTN(xvap, XAT_READONLY);
2839 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2841 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2842 XVA_SET_RTN(xvap, XAT_SYSTEM);
2845 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2847 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2848 XVA_SET_RTN(xvap, XAT_HIDDEN);
2851 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2852 xoap->xoa_nounlink =
2853 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2854 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2857 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2858 xoap->xoa_immutable =
2859 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2860 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2863 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2864 xoap->xoa_appendonly =
2865 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2866 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2869 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2871 ((zp->z_pflags & ZFS_NODUMP) != 0);
2872 XVA_SET_RTN(xvap, XAT_NODUMP);
2875 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2877 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2878 XVA_SET_RTN(xvap, XAT_OPAQUE);
2881 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2882 xoap->xoa_av_quarantined =
2883 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2884 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2887 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2888 xoap->xoa_av_modified =
2889 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2890 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2893 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2894 vp->v_type == VREG) {
2895 zfs_sa_get_scanstamp(zp, xvap);
2898 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2901 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2902 times, sizeof (times));
2903 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2904 XVA_SET_RTN(xvap, XAT_CREATETIME);
2907 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2908 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2909 XVA_SET_RTN(xvap, XAT_REPARSE);
2911 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2912 xoap->xoa_generation = zp->z_gen;
2913 XVA_SET_RTN(xvap, XAT_GEN);
2916 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2918 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2919 XVA_SET_RTN(xvap, XAT_OFFLINE);
2922 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2924 ((zp->z_pflags & ZFS_SPARSE) != 0);
2925 XVA_SET_RTN(xvap, XAT_SPARSE);
2929 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2930 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2931 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2932 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2934 mutex_exit(&zp->z_lock);
2936 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2937 vap->va_blksize = blksize;
2938 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2940 if (zp->z_blksz == 0) {
2942 * Block size hasn't been set; suggest maximal I/O transfers.
2944 vap->va_blksize = zfsvfs->z_max_blksz;
2952 * Set the file attributes to the values contained in the
2955 * IN: vp - vnode of file to be modified.
2956 * vap - new attribute values.
2957 * If AT_XVATTR set, then optional attrs are being set
2958 * flags - ATTR_UTIME set if non-default time values provided.
2959 * - ATTR_NOACLCHECK (CIFS context only).
2960 * cr - credentials of caller.
2961 * ct - caller context
2963 * RETURN: 0 on success, error code on failure.
2966 * vp - ctime updated, mtime updated if size changed.
2970 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2971 caller_context_t *ct)
2973 znode_t *zp = VTOZ(vp);
2974 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2979 uint_t mask = vap->va_mask;
2980 uint_t saved_mask = 0;
2981 uint64_t saved_mode;
2984 uint64_t new_uid, new_gid;
2986 uint64_t mtime[2], ctime[2];
2988 int need_policy = FALSE;
2990 zfs_fuid_info_t *fuidp = NULL;
2991 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2994 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2995 boolean_t fuid_dirtied = B_FALSE;
2996 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2997 int count = 0, xattr_count = 0;
3002 if (mask & AT_NOSET)
3003 return (SET_ERROR(EINVAL));
3008 zilog = zfsvfs->z_log;
3011 * Make sure that if we have ephemeral uid/gid or xvattr specified
3012 * that file system is at proper version level
3015 if (zfsvfs->z_use_fuids == B_FALSE &&
3016 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
3017 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
3018 (mask & AT_XVATTR))) {
3020 return (SET_ERROR(EINVAL));
3023 if (mask & AT_SIZE && vp->v_type == VDIR) {
3025 return (SET_ERROR(EISDIR));
3028 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
3030 return (SET_ERROR(EINVAL));
3034 * If this is an xvattr_t, then get a pointer to the structure of
3035 * optional attributes. If this is NULL, then we have a vattr_t.
3037 xoap = xva_getxoptattr(xvap);
3039 xva_init(&tmpxvattr);
3042 * Immutable files can only alter immutable bit and atime
3044 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
3045 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
3046 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
3048 return (SET_ERROR(EPERM));
3051 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
3053 return (SET_ERROR(EPERM));
3057 * Verify timestamps doesn't overflow 32 bits.
3058 * ZFS can handle large timestamps, but 32bit syscalls can't
3059 * handle times greater than 2039. This check should be removed
3060 * once large timestamps are fully supported.
3062 if (mask & (AT_ATIME | AT_MTIME)) {
3063 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
3064 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
3066 return (SET_ERROR(EOVERFLOW));
3074 /* Can this be moved to before the top label? */
3075 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
3077 return (SET_ERROR(EROFS));
3081 * First validate permissions
3084 if (mask & AT_SIZE) {
3086 * XXX - Note, we are not providing any open
3087 * mode flags here (like FNDELAY), so we may
3088 * block if there are locks present... this
3089 * should be addressed in openat().
3091 /* XXX - would it be OK to generate a log record here? */
3092 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3099 if (mask & (AT_ATIME|AT_MTIME) ||
3100 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3101 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3102 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3103 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3104 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3105 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3106 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3107 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3111 if (mask & (AT_UID|AT_GID)) {
3112 int idmask = (mask & (AT_UID|AT_GID));
3117 * NOTE: even if a new mode is being set,
3118 * we may clear S_ISUID/S_ISGID bits.
3121 if (!(mask & AT_MODE))
3122 vap->va_mode = zp->z_mode;
3125 * Take ownership or chgrp to group we are a member of
3128 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3129 take_group = (mask & AT_GID) &&
3130 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3133 * If both AT_UID and AT_GID are set then take_owner and
3134 * take_group must both be set in order to allow taking
3137 * Otherwise, send the check through secpolicy_vnode_setattr()
3141 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3142 ((idmask == AT_UID) && take_owner) ||
3143 ((idmask == AT_GID) && take_group)) {
3144 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3145 skipaclchk, cr) == 0) {
3147 * Remove setuid/setgid for non-privileged users
3149 secpolicy_setid_clear(vap, vp, cr);
3150 trim_mask = (mask & (AT_UID|AT_GID));
3159 mutex_enter(&zp->z_lock);
3160 oldva.va_mode = zp->z_mode;
3161 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3162 if (mask & AT_XVATTR) {
3164 * Update xvattr mask to include only those attributes
3165 * that are actually changing.
3167 * the bits will be restored prior to actually setting
3168 * the attributes so the caller thinks they were set.
3170 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3171 if (xoap->xoa_appendonly !=
3172 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3175 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3176 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3180 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3181 if (xoap->xoa_nounlink !=
3182 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3185 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3186 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3190 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3191 if (xoap->xoa_immutable !=
3192 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3195 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3196 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3200 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3201 if (xoap->xoa_nodump !=
3202 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3205 XVA_CLR_REQ(xvap, XAT_NODUMP);
3206 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3210 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3211 if (xoap->xoa_av_modified !=
3212 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3215 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3216 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3220 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3221 if ((vp->v_type != VREG &&
3222 xoap->xoa_av_quarantined) ||
3223 xoap->xoa_av_quarantined !=
3224 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3227 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3228 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3232 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3233 mutex_exit(&zp->z_lock);
3235 return (SET_ERROR(EPERM));
3238 if (need_policy == FALSE &&
3239 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3240 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3245 mutex_exit(&zp->z_lock);
3247 if (mask & AT_MODE) {
3248 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3249 err = secpolicy_setid_setsticky_clear(vp, vap,
3255 trim_mask |= AT_MODE;
3263 * If trim_mask is set then take ownership
3264 * has been granted or write_acl is present and user
3265 * has the ability to modify mode. In that case remove
3266 * UID|GID and or MODE from mask so that
3267 * secpolicy_vnode_setattr() doesn't revoke it.
3271 saved_mask = vap->va_mask;
3272 vap->va_mask &= ~trim_mask;
3273 if (trim_mask & AT_MODE) {
3275 * Save the mode, as secpolicy_vnode_setattr()
3276 * will overwrite it with ova.va_mode.
3278 saved_mode = vap->va_mode;
3281 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3282 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3289 vap->va_mask |= saved_mask;
3290 if (trim_mask & AT_MODE) {
3292 * Recover the mode after
3293 * secpolicy_vnode_setattr().
3295 vap->va_mode = saved_mode;
3301 * secpolicy_vnode_setattr, or take ownership may have
3304 mask = vap->va_mask;
3306 if ((mask & (AT_UID | AT_GID))) {
3307 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3308 &xattr_obj, sizeof (xattr_obj));
3310 if (err == 0 && xattr_obj) {
3311 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3315 if (mask & AT_UID) {
3316 new_uid = zfs_fuid_create(zfsvfs,
3317 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3318 if (new_uid != zp->z_uid &&
3319 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3321 VN_RELE(ZTOV(attrzp));
3322 err = SET_ERROR(EDQUOT);
3327 if (mask & AT_GID) {
3328 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3329 cr, ZFS_GROUP, &fuidp);
3330 if (new_gid != zp->z_gid &&
3331 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3333 VN_RELE(ZTOV(attrzp));
3334 err = SET_ERROR(EDQUOT);
3339 tx = dmu_tx_create(zfsvfs->z_os);
3341 if (mask & AT_MODE) {
3342 uint64_t pmode = zp->z_mode;
3344 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3346 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3347 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3348 err = SET_ERROR(EPERM);
3352 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3355 mutex_enter(&zp->z_lock);
3356 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3358 * Are we upgrading ACL from old V0 format
3361 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3362 zfs_znode_acl_version(zp) ==
3363 ZFS_ACL_VERSION_INITIAL) {
3364 dmu_tx_hold_free(tx, acl_obj, 0,
3366 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3367 0, aclp->z_acl_bytes);
3369 dmu_tx_hold_write(tx, acl_obj, 0,
3372 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3373 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3374 0, aclp->z_acl_bytes);
3376 mutex_exit(&zp->z_lock);
3377 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3379 if ((mask & AT_XVATTR) &&
3380 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3381 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3383 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3387 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3390 fuid_dirtied = zfsvfs->z_fuid_dirty;
3392 zfs_fuid_txhold(zfsvfs, tx);
3394 zfs_sa_upgrade_txholds(tx, zp);
3396 err = dmu_tx_assign(tx, TXG_NOWAIT);
3398 if (err == ERESTART)
3405 * Set each attribute requested.
3406 * We group settings according to the locks they need to acquire.
3408 * Note: you cannot set ctime directly, although it will be
3409 * updated as a side-effect of calling this function.
3413 if (mask & (AT_UID|AT_GID|AT_MODE))
3414 mutex_enter(&zp->z_acl_lock);
3415 mutex_enter(&zp->z_lock);
3417 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3418 &zp->z_pflags, sizeof (zp->z_pflags));
3421 if (mask & (AT_UID|AT_GID|AT_MODE))
3422 mutex_enter(&attrzp->z_acl_lock);
3423 mutex_enter(&attrzp->z_lock);
3424 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3425 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3426 sizeof (attrzp->z_pflags));
3429 if (mask & (AT_UID|AT_GID)) {
3431 if (mask & AT_UID) {
3432 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3433 &new_uid, sizeof (new_uid));
3434 zp->z_uid = new_uid;
3436 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3437 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3439 attrzp->z_uid = new_uid;
3443 if (mask & AT_GID) {
3444 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3445 NULL, &new_gid, sizeof (new_gid));
3446 zp->z_gid = new_gid;
3448 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3449 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3451 attrzp->z_gid = new_gid;
3454 if (!(mask & AT_MODE)) {
3455 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3456 NULL, &new_mode, sizeof (new_mode));
3457 new_mode = zp->z_mode;
3459 err = zfs_acl_chown_setattr(zp);
3462 err = zfs_acl_chown_setattr(attrzp);
3467 if (mask & AT_MODE) {
3468 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3469 &new_mode, sizeof (new_mode));
3470 zp->z_mode = new_mode;
3471 ASSERT3U((uintptr_t)aclp, !=, 0);
3472 err = zfs_aclset_common(zp, aclp, cr, tx);
3474 if (zp->z_acl_cached)
3475 zfs_acl_free(zp->z_acl_cached);
3476 zp->z_acl_cached = aclp;
3481 if (mask & AT_ATIME) {
3482 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3483 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3484 &zp->z_atime, sizeof (zp->z_atime));
3487 if (mask & AT_MTIME) {
3488 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3489 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3490 mtime, sizeof (mtime));
3493 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3494 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3495 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3496 NULL, mtime, sizeof (mtime));
3497 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3498 &ctime, sizeof (ctime));
3499 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3501 } else if (mask != 0) {
3502 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3503 &ctime, sizeof (ctime));
3504 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3507 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3508 SA_ZPL_CTIME(zfsvfs), NULL,
3509 &ctime, sizeof (ctime));
3510 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3511 mtime, ctime, B_TRUE);
3515 * Do this after setting timestamps to prevent timestamp
3516 * update from toggling bit
3519 if (xoap && (mask & AT_XVATTR)) {
3522 * restore trimmed off masks
3523 * so that return masks can be set for caller.
3526 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3527 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3529 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3530 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3532 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3533 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3535 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3536 XVA_SET_REQ(xvap, XAT_NODUMP);
3538 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3539 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3541 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3542 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3545 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3546 ASSERT(vp->v_type == VREG);
3548 zfs_xvattr_set(zp, xvap, tx);
3552 zfs_fuid_sync(zfsvfs, tx);
3555 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3557 mutex_exit(&zp->z_lock);
3558 if (mask & (AT_UID|AT_GID|AT_MODE))
3559 mutex_exit(&zp->z_acl_lock);
3562 if (mask & (AT_UID|AT_GID|AT_MODE))
3563 mutex_exit(&attrzp->z_acl_lock);
3564 mutex_exit(&attrzp->z_lock);
3567 if (err == 0 && attrzp) {
3568 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3574 VN_RELE(ZTOV(attrzp));
3580 zfs_fuid_info_free(fuidp);
3586 if (err == ERESTART)
3589 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3594 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3595 zil_commit(zilog, 0);
3601 typedef struct zfs_zlock {
3602 krwlock_t *zl_rwlock; /* lock we acquired */
3603 znode_t *zl_znode; /* znode we held */
3604 struct zfs_zlock *zl_next; /* next in list */
3608 * Drop locks and release vnodes that were held by zfs_rename_lock().
3611 zfs_rename_unlock(zfs_zlock_t **zlpp)
3615 while ((zl = *zlpp) != NULL) {
3616 if (zl->zl_znode != NULL)
3617 VN_RELE(ZTOV(zl->zl_znode));
3618 rw_exit(zl->zl_rwlock);
3619 *zlpp = zl->zl_next;
3620 kmem_free(zl, sizeof (*zl));
3625 * Search back through the directory tree, using the ".." entries.
3626 * Lock each directory in the chain to prevent concurrent renames.
3627 * Fail any attempt to move a directory into one of its own descendants.
3628 * XXX - z_parent_lock can overlap with map or grow locks
3631 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3635 uint64_t rootid = zp->z_zfsvfs->z_root;
3636 uint64_t oidp = zp->z_id;
3637 krwlock_t *rwlp = &szp->z_parent_lock;
3638 krw_t rw = RW_WRITER;
3641 * First pass write-locks szp and compares to zp->z_id.
3642 * Later passes read-lock zp and compare to zp->z_parent.
3645 if (!rw_tryenter(rwlp, rw)) {
3647 * Another thread is renaming in this path.
3648 * Note that if we are a WRITER, we don't have any
3649 * parent_locks held yet.
3651 if (rw == RW_READER && zp->z_id > szp->z_id) {
3653 * Drop our locks and restart
3655 zfs_rename_unlock(&zl);
3659 rwlp = &szp->z_parent_lock;
3664 * Wait for other thread to drop its locks
3670 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3671 zl->zl_rwlock = rwlp;
3672 zl->zl_znode = NULL;
3673 zl->zl_next = *zlpp;
3676 if (oidp == szp->z_id) /* We're a descendant of szp */
3677 return (SET_ERROR(EINVAL));
3679 if (oidp == rootid) /* We've hit the top */
3682 if (rw == RW_READER) { /* i.e. not the first pass */
3683 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3688 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3689 &oidp, sizeof (oidp));
3690 rwlp = &zp->z_parent_lock;
3693 } while (zp->z_id != sdzp->z_id);
3699 * Move an entry from the provided source directory to the target
3700 * directory. Change the entry name as indicated.
3702 * IN: sdvp - Source directory containing the "old entry".
3703 * snm - Old entry name.
3704 * tdvp - Target directory to contain the "new entry".
3705 * tnm - New entry name.
3706 * cr - credentials of caller.
3707 * ct - caller context
3708 * flags - case flags
3710 * RETURN: 0 on success, error code on failure.
3713 * sdvp,tdvp - ctime|mtime updated
3717 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3718 caller_context_t *ct, int flags)
3720 znode_t *tdzp, *szp, *tzp;
3721 znode_t *sdzp = VTOZ(sdvp);
3722 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3725 zfs_dirlock_t *sdl, *tdl;
3728 int cmp, serr, terr;
3731 boolean_t waited = B_FALSE;
3734 ZFS_VERIFY_ZP(sdzp);
3735 zilog = zfsvfs->z_log;
3738 * Make sure we have the real vp for the target directory.
3740 if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3743 if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3745 return (SET_ERROR(EXDEV));
3749 ZFS_VERIFY_ZP(tdzp);
3750 if (zfsvfs->z_utf8 && u8_validate(tnm,
3751 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3753 return (SET_ERROR(EILSEQ));
3756 if (flags & FIGNORECASE)
3765 * This is to prevent the creation of links into attribute space
3766 * by renaming a linked file into/outof an attribute directory.
3767 * See the comment in zfs_link() for why this is considered bad.
3769 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3771 return (SET_ERROR(EINVAL));
3775 * Lock source and target directory entries. To prevent deadlock,
3776 * a lock ordering must be defined. We lock the directory with
3777 * the smallest object id first, or if it's a tie, the one with
3778 * the lexically first name.
3780 if (sdzp->z_id < tdzp->z_id) {
3782 } else if (sdzp->z_id > tdzp->z_id) {
3786 * First compare the two name arguments without
3787 * considering any case folding.
3789 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3791 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3792 ASSERT(error == 0 || !zfsvfs->z_utf8);
3795 * POSIX: "If the old argument and the new argument
3796 * both refer to links to the same existing file,
3797 * the rename() function shall return successfully
3798 * and perform no other action."
3804 * If the file system is case-folding, then we may
3805 * have some more checking to do. A case-folding file
3806 * system is either supporting mixed case sensitivity
3807 * access or is completely case-insensitive. Note
3808 * that the file system is always case preserving.
3810 * In mixed sensitivity mode case sensitive behavior
3811 * is the default. FIGNORECASE must be used to
3812 * explicitly request case insensitive behavior.
3814 * If the source and target names provided differ only
3815 * by case (e.g., a request to rename 'tim' to 'Tim'),
3816 * we will treat this as a special case in the
3817 * case-insensitive mode: as long as the source name
3818 * is an exact match, we will allow this to proceed as
3819 * a name-change request.
3821 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3822 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3823 flags & FIGNORECASE)) &&
3824 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3827 * case preserving rename request, require exact
3836 * If the source and destination directories are the same, we should
3837 * grab the z_name_lock of that directory only once.
3841 rw_enter(&sdzp->z_name_lock, RW_READER);
3845 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3846 ZEXISTS | zflg, NULL, NULL);
3847 terr = zfs_dirent_lock(&tdl,
3848 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3850 terr = zfs_dirent_lock(&tdl,
3851 tdzp, tnm, &tzp, zflg, NULL, NULL);
3852 serr = zfs_dirent_lock(&sdl,
3853 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3859 * Source entry invalid or not there.
3862 zfs_dirent_unlock(tdl);
3868 rw_exit(&sdzp->z_name_lock);
3871 * FreeBSD: In OpenSolaris they only check if rename source is
3872 * ".." here, because "." is handled in their lookup. This is
3873 * not the case for FreeBSD, so we check for "." explicitly.
3875 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0)
3876 serr = SET_ERROR(EINVAL);
3881 zfs_dirent_unlock(sdl);
3885 rw_exit(&sdzp->z_name_lock);
3887 if (strcmp(tnm, "..") == 0)
3888 terr = SET_ERROR(EINVAL);
3894 * Must have write access at the source to remove the old entry
3895 * and write access at the target to create the new entry.
3896 * Note that if target and source are the same, this can be
3897 * done in a single check.
3900 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3903 if (ZTOV(szp)->v_type == VDIR) {
3905 * Check to make sure rename is valid.
3906 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3908 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3913 * Does target exist?
3917 * Source and target must be the same type.
3919 if (ZTOV(szp)->v_type == VDIR) {
3920 if (ZTOV(tzp)->v_type != VDIR) {
3921 error = SET_ERROR(ENOTDIR);
3925 if (ZTOV(tzp)->v_type == VDIR) {
3926 error = SET_ERROR(EISDIR);
3931 * POSIX dictates that when the source and target
3932 * entries refer to the same file object, rename
3933 * must do nothing and exit without error.
3935 if (szp->z_id == tzp->z_id) {
3941 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3943 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3946 * notify the target directory if it is not the same
3947 * as source directory.
3950 vnevent_rename_dest_dir(tdvp, ct);
3953 tx = dmu_tx_create(zfsvfs->z_os);
3954 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3955 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3956 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3957 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3959 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3960 zfs_sa_upgrade_txholds(tx, tdzp);
3963 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3964 zfs_sa_upgrade_txholds(tx, tzp);
3967 zfs_sa_upgrade_txholds(tx, szp);
3968 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3969 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3972 zfs_rename_unlock(&zl);
3973 zfs_dirent_unlock(sdl);
3974 zfs_dirent_unlock(tdl);
3977 rw_exit(&sdzp->z_name_lock);
3982 if (error == ERESTART) {
3993 if (tzp) /* Attempt to remove the existing target */
3994 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3997 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3999 szp->z_pflags |= ZFS_AV_MODIFIED;
4001 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
4002 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
4005 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
4007 zfs_log_rename(zilog, tx, TX_RENAME |
4008 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
4009 sdl->dl_name, tdzp, tdl->dl_name, szp);
4012 * Update path information for the target vnode
4014 vn_renamepath(tdvp, ZTOV(szp), tnm,
4018 * At this point, we have successfully created
4019 * the target name, but have failed to remove
4020 * the source name. Since the create was done
4021 * with the ZRENAMING flag, there are
4022 * complications; for one, the link count is
4023 * wrong. The easiest way to deal with this
4024 * is to remove the newly created target, and
4025 * return the original error. This must
4026 * succeed; fortunately, it is very unlikely to
4027 * fail, since we just created it.
4029 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
4030 ZRENAMING, NULL), ==, 0);
4033 #ifdef FREEBSD_NAMECACHE
4044 zfs_rename_unlock(&zl);
4046 zfs_dirent_unlock(sdl);
4047 zfs_dirent_unlock(tdl);
4050 rw_exit(&sdzp->z_name_lock);
4057 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4058 zil_commit(zilog, 0);
4066 * Insert the indicated symbolic reference entry into the directory.
4068 * IN: dvp - Directory to contain new symbolic link.
4069 * link - Name for new symlink entry.
4070 * vap - Attributes of new entry.
4071 * cr - credentials of caller.
4072 * ct - caller context
4073 * flags - case flags
4075 * RETURN: 0 on success, error code on failure.
4078 * dvp - ctime|mtime updated
4082 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
4083 cred_t *cr, kthread_t *td)
4085 znode_t *zp, *dzp = VTOZ(dvp);
4088 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4090 uint64_t len = strlen(link);
4093 zfs_acl_ids_t acl_ids;
4094 boolean_t fuid_dirtied;
4095 uint64_t txtype = TX_SYMLINK;
4096 boolean_t waited = B_FALSE;
4099 ASSERT(vap->va_type == VLNK);
4103 zilog = zfsvfs->z_log;
4105 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4106 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4108 return (SET_ERROR(EILSEQ));
4110 if (flags & FIGNORECASE)
4113 if (len > MAXPATHLEN) {
4115 return (SET_ERROR(ENAMETOOLONG));
4118 if ((error = zfs_acl_ids_create(dzp, 0,
4119 vap, cr, NULL, &acl_ids)) != 0) {
4125 * Attempt to lock directory; fail if entry already exists.
4127 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
4129 zfs_acl_ids_free(&acl_ids);
4134 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4135 zfs_acl_ids_free(&acl_ids);
4136 zfs_dirent_unlock(dl);
4141 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4142 zfs_acl_ids_free(&acl_ids);
4143 zfs_dirent_unlock(dl);
4145 return (SET_ERROR(EDQUOT));
4147 tx = dmu_tx_create(zfsvfs->z_os);
4148 fuid_dirtied = zfsvfs->z_fuid_dirty;
4149 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4150 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4151 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4152 ZFS_SA_BASE_ATTR_SIZE + len);
4153 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4154 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4155 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4156 acl_ids.z_aclp->z_acl_bytes);
4159 zfs_fuid_txhold(zfsvfs, tx);
4160 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4162 zfs_dirent_unlock(dl);
4163 if (error == ERESTART) {
4169 zfs_acl_ids_free(&acl_ids);
4176 * Create a new object for the symlink.
4177 * for version 4 ZPL datsets the symlink will be an SA attribute
4179 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4182 zfs_fuid_sync(zfsvfs, tx);
4184 mutex_enter(&zp->z_lock);
4186 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4189 zfs_sa_symlink(zp, link, len, tx);
4190 mutex_exit(&zp->z_lock);
4193 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4194 &zp->z_size, sizeof (zp->z_size), tx);
4196 * Insert the new object into the directory.
4198 (void) zfs_link_create(dl, zp, tx, ZNEW);
4200 if (flags & FIGNORECASE)
4202 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4205 zfs_acl_ids_free(&acl_ids);
4209 zfs_dirent_unlock(dl);
4211 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4212 zil_commit(zilog, 0);
4219 * Return, in the buffer contained in the provided uio structure,
4220 * the symbolic path referred to by vp.
4222 * IN: vp - vnode of symbolic link.
4223 * uio - structure to contain the link path.
4224 * cr - credentials of caller.
4225 * ct - caller context
4227 * OUT: uio - structure containing the link path.
4229 * RETURN: 0 on success, error code on failure.
4232 * vp - atime updated
4236 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4238 znode_t *zp = VTOZ(vp);
4239 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4245 mutex_enter(&zp->z_lock);
4247 error = sa_lookup_uio(zp->z_sa_hdl,
4248 SA_ZPL_SYMLINK(zfsvfs), uio);
4250 error = zfs_sa_readlink(zp, uio);
4251 mutex_exit(&zp->z_lock);
4253 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4260 * Insert a new entry into directory tdvp referencing svp.
4262 * IN: tdvp - Directory to contain new entry.
4263 * svp - vnode of new entry.
4264 * name - name of new entry.
4265 * cr - credentials of caller.
4266 * ct - caller context
4268 * RETURN: 0 on success, error code on failure.
4271 * tdvp - ctime|mtime updated
4272 * svp - ctime updated
4276 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4277 caller_context_t *ct, int flags)
4279 znode_t *dzp = VTOZ(tdvp);
4281 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4290 boolean_t waited = B_FALSE;
4292 ASSERT(tdvp->v_type == VDIR);
4296 zilog = zfsvfs->z_log;
4298 if (VOP_REALVP(svp, &realvp, ct) == 0)
4302 * POSIX dictates that we return EPERM here.
4303 * Better choices include ENOTSUP or EISDIR.
4305 if (svp->v_type == VDIR) {
4307 return (SET_ERROR(EPERM));
4310 if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) {
4312 return (SET_ERROR(EXDEV));
4318 /* Prevent links to .zfs/shares files */
4320 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4321 &parent, sizeof (uint64_t))) != 0) {
4325 if (parent == zfsvfs->z_shares_dir) {
4327 return (SET_ERROR(EPERM));
4330 if (zfsvfs->z_utf8 && u8_validate(name,
4331 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4333 return (SET_ERROR(EILSEQ));
4335 if (flags & FIGNORECASE)
4339 * We do not support links between attributes and non-attributes
4340 * because of the potential security risk of creating links
4341 * into "normal" file space in order to circumvent restrictions
4342 * imposed in attribute space.
4344 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4346 return (SET_ERROR(EINVAL));
4350 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4351 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4353 return (SET_ERROR(EPERM));
4356 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4363 * Attempt to lock directory; fail if entry already exists.
4365 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4371 tx = dmu_tx_create(zfsvfs->z_os);
4372 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4373 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4374 zfs_sa_upgrade_txholds(tx, szp);
4375 zfs_sa_upgrade_txholds(tx, dzp);
4376 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4378 zfs_dirent_unlock(dl);
4379 if (error == ERESTART) {
4390 error = zfs_link_create(dl, szp, tx, 0);
4393 uint64_t txtype = TX_LINK;
4394 if (flags & FIGNORECASE)
4396 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4401 zfs_dirent_unlock(dl);
4404 vnevent_link(svp, ct);
4407 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4408 zil_commit(zilog, 0);
4416 * zfs_null_putapage() is used when the file system has been force
4417 * unmounted. It just drops the pages.
4421 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4422 size_t *lenp, int flags, cred_t *cr)
4424 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4429 * Push a page out to disk, klustering if possible.
4431 * IN: vp - file to push page to.
4432 * pp - page to push.
4433 * flags - additional flags.
4434 * cr - credentials of caller.
4436 * OUT: offp - start of range pushed.
4437 * lenp - len of range pushed.
4439 * RETURN: 0 on success, error code on failure.
4441 * NOTE: callers must have locked the page to be pushed. On
4442 * exit, the page (and all other pages in the kluster) must be
4447 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4448 size_t *lenp, int flags, cred_t *cr)
4450 znode_t *zp = VTOZ(vp);
4451 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4453 u_offset_t off, koff;
4460 * If our blocksize is bigger than the page size, try to kluster
4461 * multiple pages so that we write a full block (thus avoiding
4462 * a read-modify-write).
4464 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4465 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4466 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4467 ASSERT(koff <= zp->z_size);
4468 if (koff + klen > zp->z_size)
4469 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4470 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4472 ASSERT3U(btop(len), ==, btopr(len));
4475 * Can't push pages past end-of-file.
4477 if (off >= zp->z_size) {
4478 /* ignore all pages */
4481 } else if (off + len > zp->z_size) {
4482 int npages = btopr(zp->z_size - off);
4485 page_list_break(&pp, &trunc, npages);
4486 /* ignore pages past end of file */
4488 pvn_write_done(trunc, flags);
4489 len = zp->z_size - off;
4492 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4493 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4494 err = SET_ERROR(EDQUOT);
4498 tx = dmu_tx_create(zfsvfs->z_os);
4499 dmu_tx_hold_write(tx, zp->z_id, off, len);
4501 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4502 zfs_sa_upgrade_txholds(tx, zp);
4503 err = dmu_tx_assign(tx, TXG_NOWAIT);
4505 if (err == ERESTART) {
4514 if (zp->z_blksz <= PAGESIZE) {
4515 caddr_t va = zfs_map_page(pp, S_READ);
4516 ASSERT3U(len, <=, PAGESIZE);
4517 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4518 zfs_unmap_page(pp, va);
4520 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4524 uint64_t mtime[2], ctime[2];
4525 sa_bulk_attr_t bulk[3];
4528 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4530 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4532 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4534 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4536 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4541 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4551 * Copy the portion of the file indicated from pages into the file.
4552 * The pages are stored in a page list attached to the files vnode.
4554 * IN: vp - vnode of file to push page data to.
4555 * off - position in file to put data.
4556 * len - amount of data to write.
4557 * flags - flags to control the operation.
4558 * cr - credentials of caller.
4559 * ct - caller context.
4561 * RETURN: 0 on success, error code on failure.
4564 * vp - ctime|mtime updated
4568 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4569 caller_context_t *ct)
4571 znode_t *zp = VTOZ(vp);
4572 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4584 * Align this request to the file block size in case we kluster.
4585 * XXX - this can result in pretty aggresive locking, which can
4586 * impact simultanious read/write access. One option might be
4587 * to break up long requests (len == 0) into block-by-block
4588 * operations to get narrower locking.
4590 blksz = zp->z_blksz;
4592 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4595 if (len > 0 && ISP2(blksz))
4596 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4602 * Search the entire vp list for pages >= io_off.
4604 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4605 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4608 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4610 if (off > zp->z_size) {
4611 /* past end of file */
4612 zfs_range_unlock(rl);
4617 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4619 for (off = io_off; io_off < off + len; io_off += io_len) {
4620 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4621 pp = page_lookup(vp, io_off,
4622 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4624 pp = page_lookup_nowait(vp, io_off,
4625 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4628 if (pp != NULL && pvn_getdirty(pp, flags)) {
4632 * Found a dirty page to push
4634 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4642 zfs_range_unlock(rl);
4643 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4644 zil_commit(zfsvfs->z_log, zp->z_id);
4652 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4654 znode_t *zp = VTOZ(vp);
4655 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4658 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4659 if (zp->z_sa_hdl == NULL) {
4661 * The fs has been unmounted, or we did a
4662 * suspend/resume and this file no longer exists.
4664 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4665 vrecycle(vp, curthread);
4669 mutex_enter(&zp->z_lock);
4670 if (zp->z_unlinked) {
4672 * Fast path to recycle a vnode of a removed file.
4674 mutex_exit(&zp->z_lock);
4675 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4676 vrecycle(vp, curthread);
4679 mutex_exit(&zp->z_lock);
4681 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4682 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4684 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4685 zfs_sa_upgrade_txholds(tx, zp);
4686 error = dmu_tx_assign(tx, TXG_WAIT);
4690 mutex_enter(&zp->z_lock);
4691 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4692 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4693 zp->z_atime_dirty = 0;
4694 mutex_exit(&zp->z_lock);
4698 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4703 * Bounds-check the seek operation.
4705 * IN: vp - vnode seeking within
4706 * ooff - old file offset
4707 * noffp - pointer to new file offset
4708 * ct - caller context
4710 * RETURN: 0 on success, EINVAL if new offset invalid.
4714 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4715 caller_context_t *ct)
4717 if (vp->v_type == VDIR)
4719 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4723 * Pre-filter the generic locking function to trap attempts to place
4724 * a mandatory lock on a memory mapped file.
4727 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4728 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4730 znode_t *zp = VTOZ(vp);
4731 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4737 * We are following the UFS semantics with respect to mapcnt
4738 * here: If we see that the file is mapped already, then we will
4739 * return an error, but we don't worry about races between this
4740 * function and zfs_map().
4742 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4744 return (SET_ERROR(EAGAIN));
4747 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4751 * If we can't find a page in the cache, we will create a new page
4752 * and fill it with file data. For efficiency, we may try to fill
4753 * multiple pages at once (klustering) to fill up the supplied page
4754 * list. Note that the pages to be filled are held with an exclusive
4755 * lock to prevent access by other threads while they are being filled.
4758 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4759 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4761 znode_t *zp = VTOZ(vp);
4762 page_t *pp, *cur_pp;
4763 objset_t *os = zp->z_zfsvfs->z_os;
4764 u_offset_t io_off, total;
4768 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4770 * We only have a single page, don't bother klustering
4774 pp = page_create_va(vp, io_off, io_len,
4775 PG_EXCL | PG_WAIT, seg, addr);
4778 * Try to find enough pages to fill the page list
4780 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4781 &io_len, off, plsz, 0);
4785 * The page already exists, nothing to do here.
4792 * Fill the pages in the kluster.
4795 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4798 ASSERT3U(io_off, ==, cur_pp->p_offset);
4799 va = zfs_map_page(cur_pp, S_WRITE);
4800 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4802 zfs_unmap_page(cur_pp, va);
4804 /* On error, toss the entire kluster */
4805 pvn_read_done(pp, B_ERROR);
4806 /* convert checksum errors into IO errors */
4808 err = SET_ERROR(EIO);
4811 cur_pp = cur_pp->p_next;
4815 * Fill in the page list array from the kluster starting
4816 * from the desired offset `off'.
4817 * NOTE: the page list will always be null terminated.
4819 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4820 ASSERT(pl == NULL || (*pl)->p_offset == off);
4826 * Return pointers to the pages for the file region [off, off + len]
4827 * in the pl array. If plsz is greater than len, this function may
4828 * also return page pointers from after the specified region
4829 * (i.e. the region [off, off + plsz]). These additional pages are
4830 * only returned if they are already in the cache, or were created as
4831 * part of a klustered read.
4833 * IN: vp - vnode of file to get data from.
4834 * off - position in file to get data from.
4835 * len - amount of data to retrieve.
4836 * plsz - length of provided page list.
4837 * seg - segment to obtain pages for.
4838 * addr - virtual address of fault.
4839 * rw - mode of created pages.
4840 * cr - credentials of caller.
4841 * ct - caller context.
4843 * OUT: protp - protection mode of created pages.
4844 * pl - list of pages created.
4846 * RETURN: 0 on success, error code on failure.
4849 * vp - atime updated
4853 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4854 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4855 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4857 znode_t *zp = VTOZ(vp);
4858 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4862 /* we do our own caching, faultahead is unnecessary */
4865 else if (len > plsz)
4868 len = P2ROUNDUP(len, PAGESIZE);
4869 ASSERT(plsz >= len);
4878 * Loop through the requested range [off, off + len) looking
4879 * for pages. If we don't find a page, we will need to create
4880 * a new page and fill it with data from the file.
4883 if (*pl = page_lookup(vp, off, SE_SHARED))
4885 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4888 ASSERT3U((*pl)->p_offset, ==, off);
4892 ASSERT3U(len, >=, PAGESIZE);
4895 ASSERT3U(plsz, >=, PAGESIZE);
4902 * Fill out the page array with any pages already in the cache.
4905 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4912 * Release any pages we have previously locked.
4917 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4927 * Request a memory map for a section of a file. This code interacts
4928 * with common code and the VM system as follows:
4930 * - common code calls mmap(), which ends up in smmap_common()
4931 * - this calls VOP_MAP(), which takes you into (say) zfs
4932 * - zfs_map() calls as_map(), passing segvn_create() as the callback
4933 * - segvn_create() creates the new segment and calls VOP_ADDMAP()
4934 * - zfs_addmap() updates z_mapcnt
4938 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4939 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4940 caller_context_t *ct)
4942 znode_t *zp = VTOZ(vp);
4943 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4944 segvn_crargs_t vn_a;
4950 if ((prot & PROT_WRITE) && (zp->z_pflags &
4951 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4953 return (SET_ERROR(EPERM));
4956 if ((prot & (PROT_READ | PROT_EXEC)) &&
4957 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4959 return (SET_ERROR(EACCES));
4962 if (vp->v_flag & VNOMAP) {
4964 return (SET_ERROR(ENOSYS));
4967 if (off < 0 || len > MAXOFFSET_T - off) {
4969 return (SET_ERROR(ENXIO));
4972 if (vp->v_type != VREG) {
4974 return (SET_ERROR(ENODEV));
4978 * If file is locked, disallow mapping.
4980 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4982 return (SET_ERROR(EAGAIN));
4986 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4994 vn_a.offset = (u_offset_t)off;
4995 vn_a.type = flags & MAP_TYPE;
4997 vn_a.maxprot = maxprot;
5000 vn_a.flags = flags & ~MAP_TYPE;
5002 vn_a.lgrp_mem_policy_flags = 0;
5004 error = as_map(as, *addrp, len, segvn_create, &vn_a);
5013 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5014 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
5015 caller_context_t *ct)
5017 uint64_t pages = btopr(len);
5019 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
5024 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
5025 * more accurate mtime for the associated file. Since we don't have a way of
5026 * detecting when the data was actually modified, we have to resort to
5027 * heuristics. If an explicit msync() is done, then we mark the mtime when the
5028 * last page is pushed. The problem occurs when the msync() call is omitted,
5029 * which by far the most common case:
5037 * putpage() via fsflush
5039 * If we wait until fsflush to come along, we can have a modification time that
5040 * is some arbitrary point in the future. In order to prevent this in the
5041 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
5046 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5047 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
5048 caller_context_t *ct)
5050 uint64_t pages = btopr(len);
5052 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
5053 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
5055 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
5056 vn_has_cached_data(vp))
5057 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
5063 * Free or allocate space in a file. Currently, this function only
5064 * supports the `F_FREESP' command. However, this command is somewhat
5065 * misnamed, as its functionality includes the ability to allocate as
5066 * well as free space.
5068 * IN: vp - vnode of file to free data in.
5069 * cmd - action to take (only F_FREESP supported).
5070 * bfp - section of file to free/alloc.
5071 * flag - current file open mode flags.
5072 * offset - current file offset.
5073 * cr - credentials of caller [UNUSED].
5074 * ct - caller context.
5076 * RETURN: 0 on success, error code on failure.
5079 * vp - ctime|mtime updated
5083 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
5084 offset_t offset, cred_t *cr, caller_context_t *ct)
5086 znode_t *zp = VTOZ(vp);
5087 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5094 if (cmd != F_FREESP) {
5096 return (SET_ERROR(EINVAL));
5099 if (error = convoff(vp, bfp, 0, offset)) {
5104 if (bfp->l_len < 0) {
5106 return (SET_ERROR(EINVAL));
5110 len = bfp->l_len; /* 0 means from off to end of file */
5112 error = zfs_freesp(zp, off, len, flag, TRUE);
5119 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
5120 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
5124 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
5126 znode_t *zp = VTOZ(vp);
5127 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5130 uint64_t object = zp->z_id;
5137 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
5138 &gen64, sizeof (uint64_t))) != 0) {
5143 gen = (uint32_t)gen64;
5145 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
5148 if (fidp->fid_len < size) {
5149 fidp->fid_len = size;
5151 return (SET_ERROR(ENOSPC));
5154 fidp->fid_len = size;
5157 zfid = (zfid_short_t *)fidp;
5159 zfid->zf_len = size;
5161 for (i = 0; i < sizeof (zfid->zf_object); i++)
5162 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
5164 /* Must have a non-zero generation number to distinguish from .zfs */
5167 for (i = 0; i < sizeof (zfid->zf_gen); i++)
5168 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
5170 if (size == LONG_FID_LEN) {
5171 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
5174 zlfid = (zfid_long_t *)fidp;
5176 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
5177 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
5179 /* XXX - this should be the generation number for the objset */
5180 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
5181 zlfid->zf_setgen[i] = 0;
5189 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5190 caller_context_t *ct)
5202 case _PC_FILESIZEBITS:
5206 case _PC_XATTR_EXISTS:
5208 zfsvfs = zp->z_zfsvfs;
5212 error = zfs_dirent_lock(&dl, zp, "", &xzp,
5213 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
5215 zfs_dirent_unlock(dl);
5216 if (!zfs_dirempty(xzp))
5219 } else if (error == ENOENT) {
5221 * If there aren't extended attributes, it's the
5222 * same as having zero of them.
5229 case _PC_SATTR_ENABLED:
5230 case _PC_SATTR_EXISTS:
5231 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5232 (vp->v_type == VREG || vp->v_type == VDIR);
5235 case _PC_ACCESS_FILTERING:
5236 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
5240 case _PC_ACL_ENABLED:
5241 *valp = _ACL_ACE_ENABLED;
5244 case _PC_MIN_HOLE_SIZE:
5245 *valp = (int)SPA_MINBLOCKSIZE;
5248 case _PC_TIMESTAMP_RESOLUTION:
5249 /* nanosecond timestamp resolution */
5253 case _PC_ACL_EXTENDED:
5261 case _PC_ACL_PATH_MAX:
5262 *valp = ACL_MAX_ENTRIES;
5266 return (EOPNOTSUPP);
5272 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5273 caller_context_t *ct)
5275 znode_t *zp = VTOZ(vp);
5276 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5278 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5282 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5290 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5291 caller_context_t *ct)
5293 znode_t *zp = VTOZ(vp);
5294 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5296 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5297 zilog_t *zilog = zfsvfs->z_log;
5302 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5304 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5305 zil_commit(zilog, 0);
5313 * The smallest read we may consider to loan out an arcbuf.
5314 * This must be a power of 2.
5316 int zcr_blksz_min = (1 << 10); /* 1K */
5318 * If set to less than the file block size, allow loaning out of an
5319 * arcbuf for a partial block read. This must be a power of 2.
5321 int zcr_blksz_max = (1 << 17); /* 128K */
5325 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5326 caller_context_t *ct)
5328 znode_t *zp = VTOZ(vp);
5329 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5330 int max_blksz = zfsvfs->z_max_blksz;
5331 uio_t *uio = &xuio->xu_uio;
5332 ssize_t size = uio->uio_resid;
5333 offset_t offset = uio->uio_loffset;
5338 int preamble, postamble;
5340 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5341 return (SET_ERROR(EINVAL));
5348 * Loan out an arc_buf for write if write size is bigger than
5349 * max_blksz, and the file's block size is also max_blksz.
5352 if (size < blksz || zp->z_blksz != blksz) {
5354 return (SET_ERROR(EINVAL));
5357 * Caller requests buffers for write before knowing where the
5358 * write offset might be (e.g. NFS TCP write).
5363 preamble = P2PHASE(offset, blksz);
5365 preamble = blksz - preamble;
5370 postamble = P2PHASE(size, blksz);
5373 fullblk = size / blksz;
5374 (void) dmu_xuio_init(xuio,
5375 (preamble != 0) + fullblk + (postamble != 0));
5376 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5377 int, postamble, int,
5378 (preamble != 0) + fullblk + (postamble != 0));
5381 * Have to fix iov base/len for partial buffers. They
5382 * currently represent full arc_buf's.
5385 /* data begins in the middle of the arc_buf */
5386 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5389 (void) dmu_xuio_add(xuio, abuf,
5390 blksz - preamble, preamble);
5393 for (i = 0; i < fullblk; i++) {
5394 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5397 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5401 /* data ends in the middle of the arc_buf */
5402 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5405 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5410 * Loan out an arc_buf for read if the read size is larger than
5411 * the current file block size. Block alignment is not
5412 * considered. Partial arc_buf will be loaned out for read.
5414 blksz = zp->z_blksz;
5415 if (blksz < zcr_blksz_min)
5416 blksz = zcr_blksz_min;
5417 if (blksz > zcr_blksz_max)
5418 blksz = zcr_blksz_max;
5419 /* avoid potential complexity of dealing with it */
5420 if (blksz > max_blksz) {
5422 return (SET_ERROR(EINVAL));
5425 maxsize = zp->z_size - uio->uio_loffset;
5429 if (size < blksz || vn_has_cached_data(vp)) {
5431 return (SET_ERROR(EINVAL));
5436 return (SET_ERROR(EINVAL));
5439 uio->uio_extflg = UIO_XUIO;
5440 XUIO_XUZC_RW(xuio) = ioflag;
5447 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5451 int ioflag = XUIO_XUZC_RW(xuio);
5453 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5455 i = dmu_xuio_cnt(xuio);
5457 abuf = dmu_xuio_arcbuf(xuio, i);
5459 * if abuf == NULL, it must be a write buffer
5460 * that has been returned in zfs_write().
5463 dmu_return_arcbuf(abuf);
5464 ASSERT(abuf || ioflag == UIO_WRITE);
5467 dmu_xuio_fini(xuio);
5472 * Predeclare these here so that the compiler assumes that
5473 * this is an "old style" function declaration that does
5474 * not include arguments => we won't get type mismatch errors
5475 * in the initializations that follow.
5477 static int zfs_inval();
5478 static int zfs_isdir();
5483 return (SET_ERROR(EINVAL));
5489 return (SET_ERROR(EISDIR));
5492 * Directory vnode operations template
5494 vnodeops_t *zfs_dvnodeops;
5495 const fs_operation_def_t zfs_dvnodeops_template[] = {
5496 VOPNAME_OPEN, { .vop_open = zfs_open },
5497 VOPNAME_CLOSE, { .vop_close = zfs_close },
5498 VOPNAME_READ, { .error = zfs_isdir },
5499 VOPNAME_WRITE, { .error = zfs_isdir },
5500 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5501 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5502 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5503 VOPNAME_ACCESS, { .vop_access = zfs_access },
5504 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5505 VOPNAME_CREATE, { .vop_create = zfs_create },
5506 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5507 VOPNAME_LINK, { .vop_link = zfs_link },
5508 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5509 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5510 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5511 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5512 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5513 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5514 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5515 VOPNAME_FID, { .vop_fid = zfs_fid },
5516 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5517 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5518 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5519 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5520 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5525 * Regular file vnode operations template
5527 vnodeops_t *zfs_fvnodeops;
5528 const fs_operation_def_t zfs_fvnodeops_template[] = {
5529 VOPNAME_OPEN, { .vop_open = zfs_open },
5530 VOPNAME_CLOSE, { .vop_close = zfs_close },
5531 VOPNAME_READ, { .vop_read = zfs_read },
5532 VOPNAME_WRITE, { .vop_write = zfs_write },
5533 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5534 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5535 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5536 VOPNAME_ACCESS, { .vop_access = zfs_access },
5537 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5538 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5539 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5540 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5541 VOPNAME_FID, { .vop_fid = zfs_fid },
5542 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5543 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5544 VOPNAME_SPACE, { .vop_space = zfs_space },
5545 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5546 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5547 VOPNAME_MAP, { .vop_map = zfs_map },
5548 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5549 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5550 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5551 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5552 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5553 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5554 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5555 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5560 * Symbolic link vnode operations template
5562 vnodeops_t *zfs_symvnodeops;
5563 const fs_operation_def_t zfs_symvnodeops_template[] = {
5564 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5565 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5566 VOPNAME_ACCESS, { .vop_access = zfs_access },
5567 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5568 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5569 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5570 VOPNAME_FID, { .vop_fid = zfs_fid },
5571 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5572 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5577 * special share hidden files vnode operations template
5579 vnodeops_t *zfs_sharevnodeops;
5580 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5581 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5582 VOPNAME_ACCESS, { .vop_access = zfs_access },
5583 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5584 VOPNAME_FID, { .vop_fid = zfs_fid },
5585 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5586 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5587 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5588 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5593 * Extended attribute directory vnode operations template
5595 * This template is identical to the directory vnodes
5596 * operation template except for restricted operations:
5600 * Note that there are other restrictions embedded in:
5601 * zfs_create() - restrict type to VREG
5602 * zfs_link() - no links into/out of attribute space
5603 * zfs_rename() - no moves into/out of attribute space
5605 vnodeops_t *zfs_xdvnodeops;
5606 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5607 VOPNAME_OPEN, { .vop_open = zfs_open },
5608 VOPNAME_CLOSE, { .vop_close = zfs_close },
5609 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5610 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5611 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5612 VOPNAME_ACCESS, { .vop_access = zfs_access },
5613 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5614 VOPNAME_CREATE, { .vop_create = zfs_create },
5615 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5616 VOPNAME_LINK, { .vop_link = zfs_link },
5617 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5618 VOPNAME_MKDIR, { .error = zfs_inval },
5619 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5620 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5621 VOPNAME_SYMLINK, { .error = zfs_inval },
5622 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5623 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5624 VOPNAME_FID, { .vop_fid = zfs_fid },
5625 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5626 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5627 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5628 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5629 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5634 * Error vnode operations template
5636 vnodeops_t *zfs_evnodeops;
5637 const fs_operation_def_t zfs_evnodeops_template[] = {
5638 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5639 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5645 ioflags(int ioflags)
5649 if (ioflags & IO_APPEND)
5651 if (ioflags & IO_NDELAY)
5653 if (ioflags & IO_SYNC)
5654 flags |= (FSYNC | FDSYNC | FRSYNC);
5660 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int reqpage)
5662 znode_t *zp = VTOZ(vp);
5663 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5664 objset_t *os = zp->z_zfsvfs->z_os;
5675 pcount = round_page(count) / PAGE_SIZE;
5677 object = mreq->object;
5680 KASSERT(vp->v_object == object, ("mismatching object"));
5682 VM_OBJECT_LOCK(object);
5683 vm_page_lock_queues();
5684 for (i = 0; i < pcount; i++) {
5689 vm_page_unlock_queues();
5692 if (mreq->valid != VM_PAGE_BITS_ALL)
5693 vm_page_zero_invalid(mreq, TRUE);
5694 VM_OBJECT_UNLOCK(object);
5696 return (VM_PAGER_OK);
5699 PCPU_INC(cnt.v_vnodein);
5700 PCPU_INC(cnt.v_vnodepgsin);
5702 if (IDX_TO_OFF(mreq->pindex) >= object->un_pager.vnp.vnp_size) {
5703 VM_OBJECT_UNLOCK(object);
5705 return (VM_PAGER_BAD);
5709 if (IDX_TO_OFF(mreq->pindex) + size > object->un_pager.vnp.vnp_size)
5710 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(mreq->pindex);
5712 VM_OBJECT_UNLOCK(object);
5713 va = zfs_map_page(mreq, &sf);
5714 error = dmu_read(os, zp->z_id, IDX_TO_OFF(mreq->pindex),
5715 size, va, DMU_READ_PREFETCH);
5716 if (size != PAGE_SIZE)
5717 bzero(va + size, PAGE_SIZE - size);
5719 VM_OBJECT_LOCK(object);
5722 mreq->valid = VM_PAGE_BITS_ALL;
5723 KASSERT(mreq->dirty == 0, ("zfs_getpages: page %p is dirty", mreq));
5725 VM_OBJECT_UNLOCK(object);
5727 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
5729 return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
5733 zfs_freebsd_getpages(ap)
5734 struct vop_getpages_args /* {
5739 vm_ooffset_t a_offset;
5743 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_reqpage));
5747 zfs_freebsd_open(ap)
5748 struct vop_open_args /* {
5751 struct ucred *a_cred;
5752 struct thread *a_td;
5755 vnode_t *vp = ap->a_vp;
5756 znode_t *zp = VTOZ(vp);
5759 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
5761 vnode_create_vobject(vp, zp->z_size, ap->a_td);
5766 zfs_freebsd_close(ap)
5767 struct vop_close_args /* {
5770 struct ucred *a_cred;
5771 struct thread *a_td;
5775 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
5779 zfs_freebsd_ioctl(ap)
5780 struct vop_ioctl_args /* {
5790 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
5791 ap->a_fflag, ap->a_cred, NULL, NULL));
5795 zfs_freebsd_read(ap)
5796 struct vop_read_args /* {
5800 struct ucred *a_cred;
5804 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
5809 zfs_freebsd_write(ap)
5810 struct vop_write_args /* {
5814 struct ucred *a_cred;
5818 if (vn_rlimit_fsize(ap->a_vp, ap->a_uio, ap->a_uio->uio_td))
5821 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
5826 zfs_freebsd_access(ap)
5827 struct vop_access_args /* {
5829 accmode_t a_accmode;
5830 struct ucred *a_cred;
5831 struct thread *a_td;
5838 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
5840 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
5842 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
5845 * VADMIN has to be handled by vaccess().
5848 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
5850 vnode_t *vp = ap->a_vp;
5851 znode_t *zp = VTOZ(vp);
5853 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
5854 zp->z_gid, accmode, ap->a_cred, NULL);
5862 zfs_freebsd_lookup(ap)
5863 struct vop_lookup_args /* {
5864 struct vnode *a_dvp;
5865 struct vnode **a_vpp;
5866 struct componentname *a_cnp;
5869 struct componentname *cnp = ap->a_cnp;
5870 char nm[NAME_MAX + 1];
5872 ASSERT(cnp->cn_namelen < sizeof(nm));
5873 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
5875 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
5876 cnp->cn_cred, cnp->cn_thread, 0));
5880 zfs_freebsd_create(ap)
5881 struct vop_create_args /* {
5882 struct vnode *a_dvp;
5883 struct vnode **a_vpp;
5884 struct componentname *a_cnp;
5885 struct vattr *a_vap;
5888 struct componentname *cnp = ap->a_cnp;
5889 vattr_t *vap = ap->a_vap;
5892 ASSERT(cnp->cn_flags & SAVENAME);
5894 vattr_init_mask(vap);
5895 mode = vap->va_mode & ALLPERMS;
5897 return (zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
5898 ap->a_vpp, cnp->cn_cred, cnp->cn_thread));
5902 zfs_freebsd_remove(ap)
5903 struct vop_remove_args /* {
5904 struct vnode *a_dvp;
5906 struct componentname *a_cnp;
5910 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5912 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr,
5913 ap->a_cnp->cn_cred, NULL, 0));
5917 zfs_freebsd_mkdir(ap)
5918 struct vop_mkdir_args /* {
5919 struct vnode *a_dvp;
5920 struct vnode **a_vpp;
5921 struct componentname *a_cnp;
5922 struct vattr *a_vap;
5925 vattr_t *vap = ap->a_vap;
5927 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5929 vattr_init_mask(vap);
5931 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
5932 ap->a_cnp->cn_cred, NULL, 0, NULL));
5936 zfs_freebsd_rmdir(ap)
5937 struct vop_rmdir_args /* {
5938 struct vnode *a_dvp;
5940 struct componentname *a_cnp;
5943 struct componentname *cnp = ap->a_cnp;
5945 ASSERT(cnp->cn_flags & SAVENAME);
5947 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0));
5951 zfs_freebsd_readdir(ap)
5952 struct vop_readdir_args /* {
5955 struct ucred *a_cred;
5962 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
5963 ap->a_ncookies, ap->a_cookies));
5967 zfs_freebsd_fsync(ap)
5968 struct vop_fsync_args /* {
5971 struct thread *a_td;
5976 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
5980 zfs_freebsd_getattr(ap)
5981 struct vop_getattr_args /* {
5983 struct vattr *a_vap;
5984 struct ucred *a_cred;
5987 vattr_t *vap = ap->a_vap;
5993 xvap.xva_vattr = *vap;
5994 xvap.xva_vattr.va_mask |= AT_XVATTR;
5996 /* Convert chflags into ZFS-type flags. */
5997 /* XXX: what about SF_SETTABLE?. */
5998 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
5999 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
6000 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
6001 XVA_SET_REQ(&xvap, XAT_NODUMP);
6002 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
6006 /* Convert ZFS xattr into chflags. */
6007 #define FLAG_CHECK(fflag, xflag, xfield) do { \
6008 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
6009 fflags |= (fflag); \
6011 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
6012 xvap.xva_xoptattrs.xoa_immutable);
6013 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
6014 xvap.xva_xoptattrs.xoa_appendonly);
6015 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
6016 xvap.xva_xoptattrs.xoa_nounlink);
6017 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
6018 xvap.xva_xoptattrs.xoa_nodump);
6020 *vap = xvap.xva_vattr;
6021 vap->va_flags = fflags;
6026 zfs_freebsd_setattr(ap)
6027 struct vop_setattr_args /* {
6029 struct vattr *a_vap;
6030 struct ucred *a_cred;
6033 vnode_t *vp = ap->a_vp;
6034 vattr_t *vap = ap->a_vap;
6035 cred_t *cred = ap->a_cred;
6040 vattr_init_mask(vap);
6041 vap->va_mask &= ~AT_NOSET;
6044 xvap.xva_vattr = *vap;
6046 zflags = VTOZ(vp)->z_pflags;
6048 if (vap->va_flags != VNOVAL) {
6049 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
6052 if (zfsvfs->z_use_fuids == B_FALSE)
6053 return (EOPNOTSUPP);
6055 fflags = vap->va_flags;
6056 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_NODUMP)) != 0)
6057 return (EOPNOTSUPP);
6059 * Unprivileged processes are not permitted to unset system
6060 * flags, or modify flags if any system flags are set.
6061 * Privileged non-jail processes may not modify system flags
6062 * if securelevel > 0 and any existing system flags are set.
6063 * Privileged jail processes behave like privileged non-jail
6064 * processes if the security.jail.chflags_allowed sysctl is
6065 * is non-zero; otherwise, they behave like unprivileged
6068 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
6069 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
6071 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6072 error = securelevel_gt(cred, 0);
6078 * Callers may only modify the file flags on objects they
6079 * have VADMIN rights for.
6081 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
6084 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6088 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
6093 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
6094 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
6095 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
6096 XVA_SET_REQ(&xvap, (xflag)); \
6097 (xfield) = ((fflags & (fflag)) != 0); \
6100 /* Convert chflags into ZFS-type flags. */
6101 /* XXX: what about SF_SETTABLE?. */
6102 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
6103 xvap.xva_xoptattrs.xoa_immutable);
6104 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
6105 xvap.xva_xoptattrs.xoa_appendonly);
6106 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
6107 xvap.xva_xoptattrs.xoa_nounlink);
6108 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
6109 xvap.xva_xoptattrs.xoa_nodump);
6112 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
6116 zfs_freebsd_rename(ap)
6117 struct vop_rename_args /* {
6118 struct vnode *a_fdvp;
6119 struct vnode *a_fvp;
6120 struct componentname *a_fcnp;
6121 struct vnode *a_tdvp;
6122 struct vnode *a_tvp;
6123 struct componentname *a_tcnp;
6126 vnode_t *fdvp = ap->a_fdvp;
6127 vnode_t *fvp = ap->a_fvp;
6128 vnode_t *tdvp = ap->a_tdvp;
6129 vnode_t *tvp = ap->a_tvp;
6132 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
6133 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
6135 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp,
6136 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0);
6151 zfs_freebsd_symlink(ap)
6152 struct vop_symlink_args /* {
6153 struct vnode *a_dvp;
6154 struct vnode **a_vpp;
6155 struct componentname *a_cnp;
6156 struct vattr *a_vap;
6160 struct componentname *cnp = ap->a_cnp;
6161 vattr_t *vap = ap->a_vap;
6163 ASSERT(cnp->cn_flags & SAVENAME);
6165 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
6166 vattr_init_mask(vap);
6168 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
6169 ap->a_target, cnp->cn_cred, cnp->cn_thread));
6173 zfs_freebsd_readlink(ap)
6174 struct vop_readlink_args /* {
6177 struct ucred *a_cred;
6181 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
6185 zfs_freebsd_link(ap)
6186 struct vop_link_args /* {
6187 struct vnode *a_tdvp;
6189 struct componentname *a_cnp;
6192 struct componentname *cnp = ap->a_cnp;
6194 ASSERT(cnp->cn_flags & SAVENAME);
6196 return (zfs_link(ap->a_tdvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
6200 zfs_freebsd_inactive(ap)
6201 struct vop_inactive_args /* {
6203 struct thread *a_td;
6206 vnode_t *vp = ap->a_vp;
6208 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
6213 zfs_freebsd_reclaim(ap)
6214 struct vop_reclaim_args /* {
6216 struct thread *a_td;
6219 vnode_t *vp = ap->a_vp;
6220 znode_t *zp = VTOZ(vp);
6221 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6225 /* Destroy the vm object and flush associated pages. */
6226 vnode_destroy_vobject(vp);
6229 * z_teardown_inactive_lock protects from a race with
6230 * zfs_znode_dmu_fini in zfsvfs_teardown during
6233 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
6234 if (zp->z_sa_hdl == NULL)
6238 rw_exit(&zfsvfs->z_teardown_inactive_lock);
6246 struct vop_fid_args /* {
6252 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
6256 zfs_freebsd_pathconf(ap)
6257 struct vop_pathconf_args /* {
6260 register_t *a_retval;
6266 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
6268 *ap->a_retval = val;
6269 else if (error == EOPNOTSUPP)
6270 error = vop_stdpathconf(ap);
6275 zfs_freebsd_fifo_pathconf(ap)
6276 struct vop_pathconf_args /* {
6279 register_t *a_retval;
6283 switch (ap->a_name) {
6284 case _PC_ACL_EXTENDED:
6286 case _PC_ACL_PATH_MAX:
6287 case _PC_MAC_PRESENT:
6288 return (zfs_freebsd_pathconf(ap));
6290 return (fifo_specops.vop_pathconf(ap));
6295 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
6296 * extended attribute name:
6299 * system freebsd:system:
6300 * user (none, can be used to access ZFS fsattr(5) attributes
6301 * created on Solaris)
6304 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
6307 const char *namespace, *prefix, *suffix;
6309 /* We don't allow '/' character in attribute name. */
6310 if (strchr(name, '/') != NULL)
6312 /* We don't allow attribute names that start with "freebsd:" string. */
6313 if (strncmp(name, "freebsd:", 8) == 0)
6316 bzero(attrname, size);
6318 switch (attrnamespace) {
6319 case EXTATTR_NAMESPACE_USER:
6321 prefix = "freebsd:";
6322 namespace = EXTATTR_NAMESPACE_USER_STRING;
6326 * This is the default namespace by which we can access all
6327 * attributes created on Solaris.
6329 prefix = namespace = suffix = "";
6332 case EXTATTR_NAMESPACE_SYSTEM:
6333 prefix = "freebsd:";
6334 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
6337 case EXTATTR_NAMESPACE_EMPTY:
6341 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
6343 return (ENAMETOOLONG);
6349 * Vnode operating to retrieve a named extended attribute.
6352 zfs_getextattr(struct vop_getextattr_args *ap)
6355 IN struct vnode *a_vp;
6356 IN int a_attrnamespace;
6357 IN const char *a_name;
6358 INOUT struct uio *a_uio;
6360 IN struct ucred *a_cred;
6361 IN struct thread *a_td;
6365 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6366 struct thread *td = ap->a_td;
6367 struct nameidata nd;
6370 vnode_t *xvp = NULL, *vp;
6373 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6374 ap->a_cred, ap->a_td, VREAD);
6378 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6385 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6393 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
6395 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
6397 NDFREE(&nd, NDF_ONLY_PNBUF);
6400 if (error == ENOENT)
6405 if (ap->a_size != NULL) {
6406 error = VOP_GETATTR(vp, &va, ap->a_cred);
6408 *ap->a_size = (size_t)va.va_size;
6409 } else if (ap->a_uio != NULL)
6410 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6413 vn_close(vp, flags, ap->a_cred, td);
6420 * Vnode operation to remove a named attribute.
6423 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
6426 IN struct vnode *a_vp;
6427 IN int a_attrnamespace;
6428 IN const char *a_name;
6429 IN struct ucred *a_cred;
6430 IN struct thread *a_td;
6434 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6435 struct thread *td = ap->a_td;
6436 struct nameidata nd;
6439 vnode_t *xvp = NULL, *vp;
6442 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6443 ap->a_cred, ap->a_td, VWRITE);
6447 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6454 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6461 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF | MPSAFE,
6462 UIO_SYSSPACE, attrname, xvp, td);
6465 NDFREE(&nd, NDF_ONLY_PNBUF);
6468 if (error == ENOENT)
6472 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
6475 if (vp == nd.ni_dvp)
6485 * Vnode operation to set a named attribute.
6488 zfs_setextattr(struct vop_setextattr_args *ap)
6491 IN struct vnode *a_vp;
6492 IN int a_attrnamespace;
6493 IN const char *a_name;
6494 INOUT struct uio *a_uio;
6495 IN struct ucred *a_cred;
6496 IN struct thread *a_td;
6500 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6501 struct thread *td = ap->a_td;
6502 struct nameidata nd;
6505 vnode_t *xvp = NULL, *vp;
6508 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6509 ap->a_cred, ap->a_td, VWRITE);
6513 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6520 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6521 LOOKUP_XATTR | CREATE_XATTR_DIR);
6527 flags = FFLAGS(O_WRONLY | O_CREAT);
6528 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
6530 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
6532 NDFREE(&nd, NDF_ONLY_PNBUF);
6540 error = VOP_SETATTR(vp, &va, ap->a_cred);
6542 VOP_WRITE(vp, ap->a_uio, IO_UNIT | IO_SYNC, ap->a_cred);
6545 vn_close(vp, flags, ap->a_cred, td);
6552 * Vnode operation to retrieve extended attributes on a vnode.
6555 zfs_listextattr(struct vop_listextattr_args *ap)
6558 IN struct vnode *a_vp;
6559 IN int a_attrnamespace;
6560 INOUT struct uio *a_uio;
6562 IN struct ucred *a_cred;
6563 IN struct thread *a_td;
6567 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6568 struct thread *td = ap->a_td;
6569 struct nameidata nd;
6570 char attrprefix[16];
6571 u_char dirbuf[sizeof(struct dirent)];
6574 struct uio auio, *uio = ap->a_uio;
6575 size_t *sizep = ap->a_size;
6577 vnode_t *xvp = NULL, *vp;
6578 int done, error, eof, pos;
6580 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6581 ap->a_cred, ap->a_td, VREAD);
6585 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
6586 sizeof(attrprefix));
6589 plen = strlen(attrprefix);
6596 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6601 * ENOATTR means that the EA directory does not yet exist,
6602 * i.e. there are no extended attributes there.
6604 if (error == ENOATTR)
6609 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE,
6610 UIO_SYSSPACE, ".", xvp, td);
6613 NDFREE(&nd, NDF_ONLY_PNBUF);
6619 auio.uio_iov = &aiov;
6620 auio.uio_iovcnt = 1;
6621 auio.uio_segflg = UIO_SYSSPACE;
6623 auio.uio_rw = UIO_READ;
6624 auio.uio_offset = 0;
6629 aiov.iov_base = (void *)dirbuf;
6630 aiov.iov_len = sizeof(dirbuf);
6631 auio.uio_resid = sizeof(dirbuf);
6632 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
6633 done = sizeof(dirbuf) - auio.uio_resid;
6636 for (pos = 0; pos < done;) {
6637 dp = (struct dirent *)(dirbuf + pos);
6638 pos += dp->d_reclen;
6640 * XXX: Temporarily we also accept DT_UNKNOWN, as this
6641 * is what we get when attribute was created on Solaris.
6643 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
6645 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
6647 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
6649 nlen = dp->d_namlen - plen;
6652 else if (uio != NULL) {
6654 * Format of extattr name entry is one byte for
6655 * length and the rest for name.
6657 error = uiomove(&nlen, 1, uio->uio_rw, uio);
6659 error = uiomove(dp->d_name + plen, nlen,
6666 } while (!eof && error == 0);
6675 zfs_freebsd_getacl(ap)
6676 struct vop_getacl_args /* {
6685 vsecattr_t vsecattr;
6687 if (ap->a_type != ACL_TYPE_NFS4)
6690 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
6691 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
6694 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
6695 if (vsecattr.vsa_aclentp != NULL)
6696 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
6702 zfs_freebsd_setacl(ap)
6703 struct vop_setacl_args /* {
6712 vsecattr_t vsecattr;
6713 int aclbsize; /* size of acl list in bytes */
6716 if (ap->a_type != ACL_TYPE_NFS4)
6719 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
6723 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
6724 * splitting every entry into two and appending "canonical six"
6725 * entries at the end. Don't allow for setting an ACL that would
6726 * cause chmod(2) to run out of ACL entries.
6728 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
6731 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
6735 vsecattr.vsa_mask = VSA_ACE;
6736 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
6737 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
6738 aaclp = vsecattr.vsa_aclentp;
6739 vsecattr.vsa_aclentsz = aclbsize;
6741 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
6742 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
6743 kmem_free(aaclp, aclbsize);
6749 zfs_freebsd_aclcheck(ap)
6750 struct vop_aclcheck_args /* {
6759 return (EOPNOTSUPP);
6762 struct vop_vector zfs_vnodeops;
6763 struct vop_vector zfs_fifoops;
6764 struct vop_vector zfs_shareops;
6766 struct vop_vector zfs_vnodeops = {
6767 .vop_default = &default_vnodeops,
6768 .vop_inactive = zfs_freebsd_inactive,
6769 .vop_reclaim = zfs_freebsd_reclaim,
6770 .vop_access = zfs_freebsd_access,
6771 #ifdef FREEBSD_NAMECACHE
6772 .vop_lookup = vfs_cache_lookup,
6773 .vop_cachedlookup = zfs_freebsd_lookup,
6775 .vop_lookup = zfs_freebsd_lookup,
6777 .vop_getattr = zfs_freebsd_getattr,
6778 .vop_setattr = zfs_freebsd_setattr,
6779 .vop_create = zfs_freebsd_create,
6780 .vop_mknod = zfs_freebsd_create,
6781 .vop_mkdir = zfs_freebsd_mkdir,
6782 .vop_readdir = zfs_freebsd_readdir,
6783 .vop_fsync = zfs_freebsd_fsync,
6784 .vop_open = zfs_freebsd_open,
6785 .vop_close = zfs_freebsd_close,
6786 .vop_rmdir = zfs_freebsd_rmdir,
6787 .vop_ioctl = zfs_freebsd_ioctl,
6788 .vop_link = zfs_freebsd_link,
6789 .vop_symlink = zfs_freebsd_symlink,
6790 .vop_readlink = zfs_freebsd_readlink,
6791 .vop_read = zfs_freebsd_read,
6792 .vop_write = zfs_freebsd_write,
6793 .vop_remove = zfs_freebsd_remove,
6794 .vop_rename = zfs_freebsd_rename,
6795 .vop_pathconf = zfs_freebsd_pathconf,
6796 .vop_bmap = VOP_EOPNOTSUPP,
6797 .vop_fid = zfs_freebsd_fid,
6798 .vop_getextattr = zfs_getextattr,
6799 .vop_deleteextattr = zfs_deleteextattr,
6800 .vop_setextattr = zfs_setextattr,
6801 .vop_listextattr = zfs_listextattr,
6802 .vop_getacl = zfs_freebsd_getacl,
6803 .vop_setacl = zfs_freebsd_setacl,
6804 .vop_aclcheck = zfs_freebsd_aclcheck,
6805 .vop_getpages = zfs_freebsd_getpages,
6808 struct vop_vector zfs_fifoops = {
6809 .vop_default = &fifo_specops,
6810 .vop_fsync = zfs_freebsd_fsync,
6811 .vop_access = zfs_freebsd_access,
6812 .vop_getattr = zfs_freebsd_getattr,
6813 .vop_inactive = zfs_freebsd_inactive,
6814 .vop_read = VOP_PANIC,
6815 .vop_reclaim = zfs_freebsd_reclaim,
6816 .vop_setattr = zfs_freebsd_setattr,
6817 .vop_write = VOP_PANIC,
6818 .vop_pathconf = zfs_freebsd_fifo_pathconf,
6819 .vop_fid = zfs_freebsd_fid,
6820 .vop_getacl = zfs_freebsd_getacl,
6821 .vop_setacl = zfs_freebsd_setacl,
6822 .vop_aclcheck = zfs_freebsd_aclcheck,
6826 * special share hidden files vnode operations template
6828 struct vop_vector zfs_shareops = {
6829 .vop_default = &default_vnodeops,
6830 .vop_access = zfs_freebsd_access,
6831 .vop_inactive = zfs_freebsd_inactive,
6832 .vop_reclaim = zfs_freebsd_reclaim,
6833 .vop_fid = zfs_freebsd_fid,
6834 .vop_pathconf = zfs_freebsd_pathconf,