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) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
27 /* Portions Copyright 2007 Jeremy Teo */
28 /* Portions Copyright 2010 Robert Milkowski */
30 #include <sys/types.h>
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
38 #include <sys/vnode.h>
42 #include <sys/taskq.h>
44 #include <sys/atomic.h>
45 #include <sys/namei.h>
47 #include <sys/cmn_err.h>
48 #include <sys/errno.h>
49 #include <sys/unistd.h>
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/fs/zfs.h>
54 #include <sys/dmu_objset.h>
60 #include <sys/dirent.h>
61 #include <sys/policy.h>
62 #include <sys/sunddi.h>
63 #include <sys/filio.h>
65 #include <sys/zfs_ctldir.h>
66 #include <sys/zfs_fuid.h>
67 #include <sys/zfs_sa.h>
69 #include <sys/zfs_rlock.h>
70 #include <sys/extdirent.h>
71 #include <sys/kidmap.h>
74 #include <sys/sched.h>
76 #include <vm/vm_param.h>
77 #include <vm/vm_pageout.h>
82 * Each vnode op performs some logical unit of work. To do this, the ZPL must
83 * properly lock its in-core state, create a DMU transaction, do the work,
84 * record this work in the intent log (ZIL), commit the DMU transaction,
85 * and wait for the intent log to commit if it is a synchronous operation.
86 * Moreover, the vnode ops must work in both normal and log replay context.
87 * The ordering of events is important to avoid deadlocks and references
88 * to freed memory. The example below illustrates the following Big Rules:
90 * (1) A check must be made in each zfs thread for a mounted file system.
91 * This is done avoiding races using ZFS_ENTER(zfsvfs).
92 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
94 * can return EIO from the calling function.
96 * (2) VN_RELE() should always be the last thing except for zil_commit()
97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
98 * First, if it's the last reference, the vnode/znode
99 * can be freed, so the zp may point to freed memory. Second, the last
100 * reference will call zfs_zinactive(), which may induce a lot of work --
101 * pushing cached pages (which acquires range locks) and syncing out
102 * cached atime changes. Third, zfs_zinactive() may require a new tx,
103 * which could deadlock the system if you were already holding one.
104 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
106 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
107 * as they can span dmu_tx_assign() calls.
109 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
110 * dmu_tx_assign(). This is critical because we don't want to block
111 * while holding locks.
113 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
114 * reduces lock contention and CPU usage when we must wait (note that if
115 * throughput is constrained by the storage, nearly every transaction
118 * Note, in particular, that if a lock is sometimes acquired before
119 * the tx assigns, and sometimes after (e.g. z_lock), then failing
120 * to use a non-blocking assign can deadlock the system. The scenario:
122 * Thread A has grabbed a lock before calling dmu_tx_assign().
123 * Thread B is in an already-assigned tx, and blocks for this lock.
124 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
125 * forever, because the previous txg can't quiesce until B's tx commits.
127 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
128 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
129 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
130 * to indicate that this operation has already called dmu_tx_wait().
131 * This will ensure that we don't retry forever, waiting a short bit
134 * (5) If the operation succeeded, generate the intent log entry for it
135 * before dropping locks. This ensures that the ordering of events
136 * in the intent log matches the order in which they actually occurred.
137 * During ZIL replay the zfs_log_* functions will update the sequence
138 * number to indicate the zil transaction has replayed.
140 * (6) At the end of each vnode op, the DMU tx must always commit,
141 * regardless of whether there were any errors.
143 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
144 * to ensure that synchronous semantics are provided when necessary.
146 * In general, this is how things should be ordered in each vnode op:
148 * ZFS_ENTER(zfsvfs); // exit if unmounted
150 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
151 * rw_enter(...); // grab any other locks you need
152 * tx = dmu_tx_create(...); // get DMU tx
153 * dmu_tx_hold_*(); // hold each object you might modify
154 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
156 * rw_exit(...); // drop locks
157 * zfs_dirent_unlock(dl); // unlock directory entry
158 * VN_RELE(...); // release held vnodes
159 * if (error == ERESTART) {
165 * dmu_tx_abort(tx); // abort DMU tx
166 * ZFS_EXIT(zfsvfs); // finished in zfs
167 * return (error); // really out of space
169 * error = do_real_work(); // do whatever this VOP does
171 * zfs_log_*(...); // on success, make ZIL entry
172 * dmu_tx_commit(tx); // commit DMU tx -- error or not
173 * rw_exit(...); // drop locks
174 * zfs_dirent_unlock(dl); // unlock directory entry
175 * VN_RELE(...); // release held vnodes
176 * zil_commit(zilog, foid); // synchronous when necessary
177 * ZFS_EXIT(zfsvfs); // finished in zfs
178 * return (error); // done, report error
183 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
185 znode_t *zp = VTOZ(*vpp);
186 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
191 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
192 ((flag & FAPPEND) == 0)) {
194 return (SET_ERROR(EPERM));
197 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
198 ZTOV(zp)->v_type == VREG &&
199 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
200 if (fs_vscan(*vpp, cr, 0) != 0) {
202 return (SET_ERROR(EACCES));
206 /* Keep a count of the synchronous opens in the znode */
207 if (flag & (FSYNC | FDSYNC))
208 atomic_inc_32(&zp->z_sync_cnt);
216 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
217 caller_context_t *ct)
219 znode_t *zp = VTOZ(vp);
220 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
223 * Clean up any locks held by this process on the vp.
225 cleanlocks(vp, ddi_get_pid(), 0);
226 cleanshares(vp, ddi_get_pid());
231 /* Decrement the synchronous opens in the znode */
232 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
233 atomic_dec_32(&zp->z_sync_cnt);
235 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
236 ZTOV(zp)->v_type == VREG &&
237 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
238 VERIFY(fs_vscan(vp, cr, 1) == 0);
245 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
246 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
249 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
251 znode_t *zp = VTOZ(vp);
252 uint64_t noff = (uint64_t)*off; /* new offset */
257 file_sz = zp->z_size;
258 if (noff >= file_sz) {
259 return (SET_ERROR(ENXIO));
262 if (cmd == _FIO_SEEK_HOLE)
267 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
270 return (SET_ERROR(ENXIO));
273 * We could find a hole that begins after the logical end-of-file,
274 * because dmu_offset_next() only works on whole blocks. If the
275 * EOF falls mid-block, then indicate that the "virtual hole"
276 * at the end of the file begins at the logical EOF, rather than
277 * at the end of the last block.
279 if (noff > file_sz) {
292 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
293 int *rvalp, caller_context_t *ct)
297 dmu_object_info_t doi;
308 * The following two ioctls are used by bfu. Faking out,
309 * necessary to avoid bfu errors.
322 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
323 return (SET_ERROR(EFAULT));
325 off = *(offset_t *)data;
328 zfsvfs = zp->z_zfsvfs;
332 /* offset parameter is in/out */
333 error = zfs_holey(vp, com, &off);
338 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
339 return (SET_ERROR(EFAULT));
341 *(offset_t *)data = off;
346 case _FIO_COUNT_FILLED:
349 * _FIO_COUNT_FILLED adds a new ioctl command which
350 * exposes the number of filled blocks in a
354 zfsvfs = zp->z_zfsvfs;
359 * Wait for all dirty blocks for this object
360 * to get synced out to disk, and the DMU info
363 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
370 * Retrieve fill count from DMU object.
372 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
378 ndata = doi.doi_fill_count;
381 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
382 return (SET_ERROR(EFAULT));
387 return (SET_ERROR(ENOTTY));
391 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
398 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
399 * aligned boundaries, if the range is not aligned. As a result a
400 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
401 * It may happen that all DEV_BSIZE subranges are marked clean and thus
402 * the whole page would be considred clean despite have some dirty data.
403 * For this reason we should shrink the range to DEV_BSIZE aligned
404 * boundaries before calling vm_page_clear_dirty.
406 end = rounddown2(off + nbytes, DEV_BSIZE);
407 off = roundup2(off, DEV_BSIZE);
411 zfs_vmobject_assert_wlocked(obj);
414 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
416 if (vm_page_xbusied(pp)) {
418 * Reference the page before unlocking and
419 * sleeping so that the page daemon is less
420 * likely to reclaim it.
422 vm_page_reference(pp);
424 zfs_vmobject_wunlock(obj);
425 vm_page_busy_sleep(pp, "zfsmwb");
426 zfs_vmobject_wlock(obj);
430 } else if (pp == NULL) {
431 pp = vm_page_alloc(obj, OFF_TO_IDX(start),
432 VM_ALLOC_SYSTEM | VM_ALLOC_IFCACHED |
435 ASSERT(pp != NULL && !pp->valid);
440 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
441 vm_object_pip_add(obj, 1);
442 pmap_remove_write(pp);
444 vm_page_clear_dirty(pp, off, nbytes);
452 page_unbusy(vm_page_t pp)
456 vm_object_pip_subtract(pp->object, 1);
460 page_hold(vnode_t *vp, int64_t start)
466 zfs_vmobject_assert_wlocked(obj);
469 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
471 if (vm_page_xbusied(pp)) {
473 * Reference the page before unlocking and
474 * sleeping so that the page daemon is less
475 * likely to reclaim it.
477 vm_page_reference(pp);
479 zfs_vmobject_wunlock(obj);
480 vm_page_busy_sleep(pp, "zfsmwb");
481 zfs_vmobject_wlock(obj);
485 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
498 page_unhold(vm_page_t pp)
507 * When a file is memory mapped, we must keep the IO data synchronized
508 * between the DMU cache and the memory mapped pages. What this means:
510 * On Write: If we find a memory mapped page, we write to *both*
511 * the page and the dmu buffer.
514 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
515 int segflg, dmu_tx_t *tx)
522 ASSERT(segflg != UIO_NOCOPY);
523 ASSERT(vp->v_mount != NULL);
527 off = start & PAGEOFFSET;
528 zfs_vmobject_wlock(obj);
529 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
531 int nbytes = imin(PAGESIZE - off, len);
533 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
534 zfs_vmobject_wunlock(obj);
536 va = zfs_map_page(pp, &sf);
537 (void) dmu_read(os, oid, start+off, nbytes,
538 va+off, DMU_READ_PREFETCH);;
541 zfs_vmobject_wlock(obj);
547 vm_object_pip_wakeupn(obj, 0);
548 zfs_vmobject_wunlock(obj);
552 * Read with UIO_NOCOPY flag means that sendfile(2) requests
553 * ZFS to populate a range of page cache pages with data.
555 * NOTE: this function could be optimized to pre-allocate
556 * all pages in advance, drain exclusive busy on all of them,
557 * map them into contiguous KVA region and populate them
558 * in one single dmu_read() call.
561 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
563 znode_t *zp = VTOZ(vp);
564 objset_t *os = zp->z_zfsvfs->z_os;
574 ASSERT(uio->uio_segflg == UIO_NOCOPY);
575 ASSERT(vp->v_mount != NULL);
578 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
580 zfs_vmobject_wlock(obj);
581 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
582 int bytes = MIN(PAGESIZE, len);
584 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_SBUSY |
585 VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
586 if (pp->valid == 0) {
587 zfs_vmobject_wunlock(obj);
588 va = zfs_map_page(pp, &sf);
589 error = dmu_read(os, zp->z_id, start, bytes, va,
591 if (bytes != PAGESIZE && error == 0)
592 bzero(va + bytes, PAGESIZE - bytes);
594 zfs_vmobject_wlock(obj);
598 if (pp->wire_count == 0 && pp->valid == 0 &&
602 pp->valid = VM_PAGE_BITS_ALL;
603 vm_page_activate(pp);
607 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
612 uio->uio_resid -= bytes;
613 uio->uio_offset += bytes;
616 zfs_vmobject_wunlock(obj);
621 * When a file is memory mapped, we must keep the IO data synchronized
622 * between the DMU cache and the memory mapped pages. What this means:
624 * On Read: We "read" preferentially from memory mapped pages,
625 * else we default from the dmu buffer.
627 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
628 * the file is memory mapped.
631 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
633 znode_t *zp = VTOZ(vp);
641 ASSERT(vp->v_mount != NULL);
645 start = uio->uio_loffset;
646 off = start & PAGEOFFSET;
647 zfs_vmobject_wlock(obj);
648 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
650 uint64_t bytes = MIN(PAGESIZE - off, len);
652 if (pp = page_hold(vp, start)) {
656 zfs_vmobject_wunlock(obj);
657 va = zfs_map_page(pp, &sf);
658 error = uiomove(va + off, bytes, UIO_READ, uio);
660 zfs_vmobject_wlock(obj);
663 zfs_vmobject_wunlock(obj);
664 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
666 zfs_vmobject_wlock(obj);
673 zfs_vmobject_wunlock(obj);
677 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
680 * Read bytes from specified file into supplied buffer.
682 * IN: vp - vnode of file to be read from.
683 * uio - structure supplying read location, range info,
685 * ioflag - SYNC flags; used to provide FRSYNC semantics.
686 * cr - credentials of caller.
687 * ct - caller context
689 * OUT: uio - updated offset and range, buffer filled.
691 * RETURN: 0 on success, error code on failure.
694 * vp - atime updated if byte count > 0
698 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
700 znode_t *zp = VTOZ(vp);
701 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
710 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
712 return (SET_ERROR(EACCES));
716 * Validate file offset
718 if (uio->uio_loffset < (offset_t)0) {
720 return (SET_ERROR(EINVAL));
724 * Fasttrack empty reads
726 if (uio->uio_resid == 0) {
732 * Check for mandatory locks
734 if (MANDMODE(zp->z_mode)) {
735 if (error = chklock(vp, FREAD,
736 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
743 * If we're in FRSYNC mode, sync out this znode before reading it.
746 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
747 zil_commit(zfsvfs->z_log, zp->z_id);
750 * Lock the range against changes.
752 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
755 * If we are reading past end-of-file we can skip
756 * to the end; but we might still need to set atime.
758 if (uio->uio_loffset >= zp->z_size) {
763 ASSERT(uio->uio_loffset < zp->z_size);
764 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
767 if ((uio->uio_extflg == UIO_XUIO) &&
768 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
770 int blksz = zp->z_blksz;
771 uint64_t offset = uio->uio_loffset;
773 xuio = (xuio_t *)uio;
775 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
778 ASSERT(offset + n <= blksz);
781 (void) dmu_xuio_init(xuio, nblk);
783 if (vn_has_cached_data(vp)) {
785 * For simplicity, we always allocate a full buffer
786 * even if we only expect to read a portion of a block.
788 while (--nblk >= 0) {
789 (void) dmu_xuio_add(xuio,
790 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
798 nbytes = MIN(n, zfs_read_chunk_size -
799 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
802 if (uio->uio_segflg == UIO_NOCOPY)
803 error = mappedread_sf(vp, nbytes, uio);
805 #endif /* __FreeBSD__ */
806 if (vn_has_cached_data(vp)) {
807 error = mappedread(vp, nbytes, uio);
809 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
813 /* convert checksum errors into IO errors */
815 error = SET_ERROR(EIO);
822 zfs_range_unlock(rl);
824 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
830 * Write the bytes to a file.
832 * IN: vp - vnode of file to be written to.
833 * uio - structure supplying write location, range info,
835 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
836 * set if in append mode.
837 * cr - credentials of caller.
838 * ct - caller context (NFS/CIFS fem monitor only)
840 * OUT: uio - updated offset and range.
842 * RETURN: 0 on success, error code on failure.
845 * vp - ctime|mtime updated if byte count > 0
850 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
852 znode_t *zp = VTOZ(vp);
853 rlim64_t limit = MAXOFFSET_T;
854 ssize_t start_resid = uio->uio_resid;
858 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
863 int max_blksz = zfsvfs->z_max_blksz;
866 iovec_t *aiov = NULL;
869 int iovcnt = uio->uio_iovcnt;
870 iovec_t *iovp = uio->uio_iov;
873 sa_bulk_attr_t bulk[4];
874 uint64_t mtime[2], ctime[2];
877 * Fasttrack empty write
883 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
889 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
890 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
891 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
893 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
897 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
898 * callers might not be able to detect properly that we are read-only,
899 * so check it explicitly here.
901 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
903 return (SET_ERROR(EROFS));
907 * If immutable or not appending then return EPERM
909 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
910 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
911 (uio->uio_loffset < zp->z_size))) {
913 return (SET_ERROR(EPERM));
916 zilog = zfsvfs->z_log;
919 * Validate file offset
921 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
924 return (SET_ERROR(EINVAL));
928 * Check for mandatory locks before calling zfs_range_lock()
929 * in order to prevent a deadlock with locks set via fcntl().
931 if (MANDMODE((mode_t)zp->z_mode) &&
932 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
939 * Pre-fault the pages to ensure slow (eg NFS) pages
941 * Skip this if uio contains loaned arc_buf.
943 if ((uio->uio_extflg == UIO_XUIO) &&
944 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
945 xuio = (xuio_t *)uio;
947 uio_prefaultpages(MIN(n, max_blksz), uio);
951 * If in append mode, set the io offset pointer to eof.
953 if (ioflag & FAPPEND) {
955 * Obtain an appending range lock to guarantee file append
956 * semantics. We reset the write offset once we have the lock.
958 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
960 if (rl->r_len == UINT64_MAX) {
962 * We overlocked the file because this write will cause
963 * the file block size to increase.
964 * Note that zp_size cannot change with this lock held.
968 uio->uio_loffset = woff;
971 * Note that if the file block size will change as a result of
972 * this write, then this range lock will lock the entire file
973 * so that we can re-write the block safely.
975 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
978 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
979 zfs_range_unlock(rl);
985 zfs_range_unlock(rl);
987 return (SET_ERROR(EFBIG));
990 if ((woff + n) > limit || woff > (limit - n))
993 /* Will this write extend the file length? */
994 write_eof = (woff + n > zp->z_size);
996 end_size = MAX(zp->z_size, woff + n);
999 * Write the file in reasonable size chunks. Each chunk is written
1000 * in a separate transaction; this keeps the intent log records small
1001 * and allows us to do more fine-grained space accounting.
1005 woff = uio->uio_loffset;
1006 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
1007 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
1009 dmu_return_arcbuf(abuf);
1010 error = SET_ERROR(EDQUOT);
1014 if (xuio && abuf == NULL) {
1015 ASSERT(i_iov < iovcnt);
1016 aiov = &iovp[i_iov];
1017 abuf = dmu_xuio_arcbuf(xuio, i_iov);
1018 dmu_xuio_clear(xuio, i_iov);
1019 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
1020 iovec_t *, aiov, arc_buf_t *, abuf);
1021 ASSERT((aiov->iov_base == abuf->b_data) ||
1022 ((char *)aiov->iov_base - (char *)abuf->b_data +
1023 aiov->iov_len == arc_buf_size(abuf)));
1025 } else if (abuf == NULL && n >= max_blksz &&
1026 woff >= zp->z_size &&
1027 P2PHASE(woff, max_blksz) == 0 &&
1028 zp->z_blksz == max_blksz) {
1030 * This write covers a full block. "Borrow" a buffer
1031 * from the dmu so that we can fill it before we enter
1032 * a transaction. This avoids the possibility of
1033 * holding up the transaction if the data copy hangs
1034 * up on a pagefault (e.g., from an NFS server mapping).
1038 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
1040 ASSERT(abuf != NULL);
1041 ASSERT(arc_buf_size(abuf) == max_blksz);
1042 if (error = uiocopy(abuf->b_data, max_blksz,
1043 UIO_WRITE, uio, &cbytes)) {
1044 dmu_return_arcbuf(abuf);
1047 ASSERT(cbytes == max_blksz);
1051 * Start a transaction.
1053 tx = dmu_tx_create(zfsvfs->z_os);
1054 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1055 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
1056 zfs_sa_upgrade_txholds(tx, zp);
1057 error = dmu_tx_assign(tx, TXG_WAIT);
1061 dmu_return_arcbuf(abuf);
1066 * If zfs_range_lock() over-locked we grow the blocksize
1067 * and then reduce the lock range. This will only happen
1068 * on the first iteration since zfs_range_reduce() will
1069 * shrink down r_len to the appropriate size.
1071 if (rl->r_len == UINT64_MAX) {
1074 if (zp->z_blksz > max_blksz) {
1076 * File's blocksize is already larger than the
1077 * "recordsize" property. Only let it grow to
1078 * the next power of 2.
1080 ASSERT(!ISP2(zp->z_blksz));
1081 new_blksz = MIN(end_size,
1082 1 << highbit64(zp->z_blksz));
1084 new_blksz = MIN(end_size, max_blksz);
1086 zfs_grow_blocksize(zp, new_blksz, tx);
1087 zfs_range_reduce(rl, woff, n);
1091 * XXX - should we really limit each write to z_max_blksz?
1092 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1094 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1096 if (woff + nbytes > zp->z_size)
1097 vnode_pager_setsize(vp, woff + nbytes);
1100 tx_bytes = uio->uio_resid;
1101 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1103 tx_bytes -= uio->uio_resid;
1106 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1108 * If this is not a full block write, but we are
1109 * extending the file past EOF and this data starts
1110 * block-aligned, use assign_arcbuf(). Otherwise,
1111 * write via dmu_write().
1113 if (tx_bytes < max_blksz && (!write_eof ||
1114 aiov->iov_base != abuf->b_data)) {
1116 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1117 aiov->iov_len, aiov->iov_base, tx);
1118 dmu_return_arcbuf(abuf);
1119 xuio_stat_wbuf_copied();
1121 ASSERT(xuio || tx_bytes == max_blksz);
1122 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1125 ASSERT(tx_bytes <= uio->uio_resid);
1126 uioskip(uio, tx_bytes);
1128 if (tx_bytes && vn_has_cached_data(vp)) {
1129 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1130 zp->z_id, uio->uio_segflg, tx);
1134 * If we made no progress, we're done. If we made even
1135 * partial progress, update the znode and ZIL accordingly.
1137 if (tx_bytes == 0) {
1138 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1139 (void *)&zp->z_size, sizeof (uint64_t), tx);
1146 * Clear Set-UID/Set-GID bits on successful write if not
1147 * privileged and at least one of the excute bits is set.
1149 * It would be nice to to this after all writes have
1150 * been done, but that would still expose the ISUID/ISGID
1151 * to another app after the partial write is committed.
1153 * Note: we don't call zfs_fuid_map_id() here because
1154 * user 0 is not an ephemeral uid.
1156 mutex_enter(&zp->z_acl_lock);
1157 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1158 (S_IXUSR >> 6))) != 0 &&
1159 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1160 secpolicy_vnode_setid_retain(vp, cr,
1161 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1163 zp->z_mode &= ~(S_ISUID | S_ISGID);
1164 newmode = zp->z_mode;
1165 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1166 (void *)&newmode, sizeof (uint64_t), tx);
1168 mutex_exit(&zp->z_acl_lock);
1170 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1174 * Update the file size (zp_size) if it has changed;
1175 * account for possible concurrent updates.
1177 while ((end_size = zp->z_size) < uio->uio_loffset) {
1178 (void) atomic_cas_64(&zp->z_size, end_size,
1183 * If we are replaying and eof is non zero then force
1184 * the file size to the specified eof. Note, there's no
1185 * concurrency during replay.
1187 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1188 zp->z_size = zfsvfs->z_replay_eof;
1190 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1192 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1197 ASSERT(tx_bytes == nbytes);
1202 uio_prefaultpages(MIN(n, max_blksz), uio);
1206 zfs_range_unlock(rl);
1209 * If we're in replay mode, or we made no progress, return error.
1210 * Otherwise, it's at least a partial write, so it's successful.
1212 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1217 if (ioflag & (FSYNC | FDSYNC) ||
1218 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1219 zil_commit(zilog, zp->z_id);
1226 zfs_get_done(zgd_t *zgd, int error)
1228 znode_t *zp = zgd->zgd_private;
1229 objset_t *os = zp->z_zfsvfs->z_os;
1232 dmu_buf_rele(zgd->zgd_db, zgd);
1234 zfs_range_unlock(zgd->zgd_rl);
1237 * Release the vnode asynchronously as we currently have the
1238 * txg stopped from syncing.
1240 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1242 if (error == 0 && zgd->zgd_bp)
1243 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1245 kmem_free(zgd, sizeof (zgd_t));
1249 static int zil_fault_io = 0;
1253 * Get data to generate a TX_WRITE intent log record.
1256 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1258 zfsvfs_t *zfsvfs = arg;
1259 objset_t *os = zfsvfs->z_os;
1261 uint64_t object = lr->lr_foid;
1262 uint64_t offset = lr->lr_offset;
1263 uint64_t size = lr->lr_length;
1264 blkptr_t *bp = &lr->lr_blkptr;
1269 ASSERT(zio != NULL);
1273 * Nothing to do if the file has been removed
1275 if (zfs_zget(zfsvfs, object, &zp) != 0)
1276 return (SET_ERROR(ENOENT));
1277 if (zp->z_unlinked) {
1279 * Release the vnode asynchronously as we currently have the
1280 * txg stopped from syncing.
1282 VN_RELE_ASYNC(ZTOV(zp),
1283 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1284 return (SET_ERROR(ENOENT));
1287 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1288 zgd->zgd_zilog = zfsvfs->z_log;
1289 zgd->zgd_private = zp;
1292 * Write records come in two flavors: immediate and indirect.
1293 * For small writes it's cheaper to store the data with the
1294 * log record (immediate); for large writes it's cheaper to
1295 * sync the data and get a pointer to it (indirect) so that
1296 * we don't have to write the data twice.
1298 if (buf != NULL) { /* immediate write */
1299 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1300 /* test for truncation needs to be done while range locked */
1301 if (offset >= zp->z_size) {
1302 error = SET_ERROR(ENOENT);
1304 error = dmu_read(os, object, offset, size, buf,
1305 DMU_READ_NO_PREFETCH);
1307 ASSERT(error == 0 || error == ENOENT);
1308 } else { /* indirect write */
1310 * Have to lock the whole block to ensure when it's
1311 * written out and it's checksum is being calculated
1312 * that no one can change the data. We need to re-check
1313 * blocksize after we get the lock in case it's changed!
1318 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1320 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1322 if (zp->z_blksz == size)
1325 zfs_range_unlock(zgd->zgd_rl);
1327 /* test for truncation needs to be done while range locked */
1328 if (lr->lr_offset >= zp->z_size)
1329 error = SET_ERROR(ENOENT);
1332 error = SET_ERROR(EIO);
1337 error = dmu_buf_hold(os, object, offset, zgd, &db,
1338 DMU_READ_NO_PREFETCH);
1341 blkptr_t *obp = dmu_buf_get_blkptr(db);
1343 ASSERT(BP_IS_HOLE(bp));
1350 ASSERT(db->db_offset == offset);
1351 ASSERT(db->db_size == size);
1353 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1355 ASSERT(error || lr->lr_length <= zp->z_blksz);
1358 * On success, we need to wait for the write I/O
1359 * initiated by dmu_sync() to complete before we can
1360 * release this dbuf. We will finish everything up
1361 * in the zfs_get_done() callback.
1366 if (error == EALREADY) {
1367 lr->lr_common.lrc_txtype = TX_WRITE2;
1373 zfs_get_done(zgd, error);
1380 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1381 caller_context_t *ct)
1383 znode_t *zp = VTOZ(vp);
1384 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1390 if (flag & V_ACE_MASK)
1391 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1393 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1400 * If vnode is for a device return a specfs vnode instead.
1403 specvp_check(vnode_t **vpp, cred_t *cr)
1407 if (IS_DEVVP(*vpp)) {
1410 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1413 error = SET_ERROR(ENOSYS);
1421 * Lookup an entry in a directory, or an extended attribute directory.
1422 * If it exists, return a held vnode reference for it.
1424 * IN: dvp - vnode of directory to search.
1425 * nm - name of entry to lookup.
1426 * pnp - full pathname to lookup [UNUSED].
1427 * flags - LOOKUP_XATTR set if looking for an attribute.
1428 * rdir - root directory vnode [UNUSED].
1429 * cr - credentials of caller.
1430 * ct - caller context
1431 * direntflags - directory lookup flags
1432 * realpnp - returned pathname.
1434 * OUT: vpp - vnode of located entry, NULL if not found.
1436 * RETURN: 0 on success, error code on failure.
1443 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1444 int nameiop, cred_t *cr, kthread_t *td, int flags)
1446 znode_t *zdp = VTOZ(dvp);
1447 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1449 int *direntflags = NULL;
1450 void *realpnp = NULL;
1453 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1455 if (dvp->v_type != VDIR) {
1456 return (SET_ERROR(ENOTDIR));
1457 } else if (zdp->z_sa_hdl == NULL) {
1458 return (SET_ERROR(EIO));
1461 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1462 error = zfs_fastaccesschk_execute(zdp, cr);
1470 vnode_t *tvp = dnlc_lookup(dvp, nm);
1473 error = zfs_fastaccesschk_execute(zdp, cr);
1478 if (tvp == DNLC_NO_VNODE) {
1480 return (SET_ERROR(ENOENT));
1483 return (specvp_check(vpp, cr));
1489 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1496 if (flags & LOOKUP_XATTR) {
1499 * If the xattr property is off, refuse the lookup request.
1501 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1503 return (SET_ERROR(EINVAL));
1508 * We don't allow recursive attributes..
1509 * Maybe someday we will.
1511 if (zdp->z_pflags & ZFS_XATTR) {
1513 return (SET_ERROR(EINVAL));
1516 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1522 * Do we have permission to get into attribute directory?
1525 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1535 if (dvp->v_type != VDIR) {
1537 return (SET_ERROR(ENOTDIR));
1541 * Check accessibility of directory.
1544 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1549 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1550 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1552 return (SET_ERROR(EILSEQ));
1555 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1557 error = specvp_check(vpp, cr);
1559 /* Translate errors and add SAVENAME when needed. */
1560 if (cnp->cn_flags & ISLASTCN) {
1564 if (error == ENOENT) {
1565 error = EJUSTRETURN;
1566 cnp->cn_flags |= SAVENAME;
1572 cnp->cn_flags |= SAVENAME;
1576 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) {
1579 if (cnp->cn_flags & ISDOTDOT) {
1580 ltype = VOP_ISLOCKED(dvp);
1584 error = vn_lock(*vpp, cnp->cn_lkflags);
1585 if (cnp->cn_flags & ISDOTDOT)
1586 vn_lock(dvp, ltype | LK_RETRY);
1596 #ifdef FREEBSD_NAMECACHE
1598 * Insert name into cache (as non-existent) if appropriate.
1600 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1601 cache_enter(dvp, *vpp, cnp);
1603 * Insert name into cache if appropriate.
1605 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1606 if (!(cnp->cn_flags & ISLASTCN) ||
1607 (nameiop != DELETE && nameiop != RENAME)) {
1608 cache_enter(dvp, *vpp, cnp);
1617 * Attempt to create a new entry in a directory. If the entry
1618 * already exists, truncate the file if permissible, else return
1619 * an error. Return the vp of the created or trunc'd file.
1621 * IN: dvp - vnode of directory to put new file entry in.
1622 * name - name of new file entry.
1623 * vap - attributes of new file.
1624 * excl - flag indicating exclusive or non-exclusive mode.
1625 * mode - mode to open file with.
1626 * cr - credentials of caller.
1627 * flag - large file flag [UNUSED].
1628 * ct - caller context
1629 * vsecp - ACL to be set
1631 * OUT: vpp - vnode of created or trunc'd entry.
1633 * RETURN: 0 on success, error code on failure.
1636 * dvp - ctime|mtime updated if new entry created
1637 * vp - ctime|mtime always, atime if new
1642 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1643 vnode_t **vpp, cred_t *cr, kthread_t *td)
1645 znode_t *zp, *dzp = VTOZ(dvp);
1646 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1654 gid_t gid = crgetgid(cr);
1655 zfs_acl_ids_t acl_ids;
1656 boolean_t fuid_dirtied;
1657 boolean_t have_acl = B_FALSE;
1658 boolean_t waited = B_FALSE;
1663 * If we have an ephemeral id, ACL, or XVATTR then
1664 * make sure file system is at proper version
1667 ksid = crgetsid(cr, KSID_OWNER);
1669 uid = ksid_getid(ksid);
1673 if (zfsvfs->z_use_fuids == B_FALSE &&
1674 (vsecp || (vap->va_mask & AT_XVATTR) ||
1675 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1676 return (SET_ERROR(EINVAL));
1681 zilog = zfsvfs->z_log;
1683 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1684 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1686 return (SET_ERROR(EILSEQ));
1689 if (vap->va_mask & AT_XVATTR) {
1690 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1691 crgetuid(cr), cr, vap->va_type)) != 0) {
1697 getnewvnode_reserve(1);
1702 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1703 vap->va_mode &= ~S_ISVTX;
1705 if (*name == '\0') {
1707 * Null component name refers to the directory itself.
1714 /* possible VN_HOLD(zp) */
1717 if (flag & FIGNORECASE)
1720 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1724 zfs_acl_ids_free(&acl_ids);
1725 if (strcmp(name, "..") == 0)
1726 error = SET_ERROR(EISDIR);
1727 getnewvnode_drop_reserve();
1737 * Create a new file object and update the directory
1740 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1742 zfs_acl_ids_free(&acl_ids);
1747 * We only support the creation of regular files in
1748 * extended attribute directories.
1751 if ((dzp->z_pflags & ZFS_XATTR) &&
1752 (vap->va_type != VREG)) {
1754 zfs_acl_ids_free(&acl_ids);
1755 error = SET_ERROR(EINVAL);
1759 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1760 cr, vsecp, &acl_ids)) != 0)
1764 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1765 zfs_acl_ids_free(&acl_ids);
1766 error = SET_ERROR(EDQUOT);
1770 tx = dmu_tx_create(os);
1772 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1773 ZFS_SA_BASE_ATTR_SIZE);
1775 fuid_dirtied = zfsvfs->z_fuid_dirty;
1777 zfs_fuid_txhold(zfsvfs, tx);
1778 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1779 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1780 if (!zfsvfs->z_use_sa &&
1781 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1782 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1783 0, acl_ids.z_aclp->z_acl_bytes);
1785 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1787 zfs_dirent_unlock(dl);
1788 if (error == ERESTART) {
1794 zfs_acl_ids_free(&acl_ids);
1796 getnewvnode_drop_reserve();
1800 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1803 zfs_fuid_sync(zfsvfs, tx);
1805 (void) zfs_link_create(dl, zp, tx, ZNEW);
1806 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1807 if (flag & FIGNORECASE)
1809 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1810 vsecp, acl_ids.z_fuidp, vap);
1811 zfs_acl_ids_free(&acl_ids);
1814 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1817 zfs_acl_ids_free(&acl_ids);
1821 * A directory entry already exists for this name.
1824 * Can't truncate an existing file if in exclusive mode.
1827 error = SET_ERROR(EEXIST);
1831 * Can't open a directory for writing.
1833 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1834 error = SET_ERROR(EISDIR);
1838 * Verify requested access to file.
1840 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1844 mutex_enter(&dzp->z_lock);
1846 mutex_exit(&dzp->z_lock);
1849 * Truncate regular files if requested.
1851 if ((ZTOV(zp)->v_type == VREG) &&
1852 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1853 /* we can't hold any locks when calling zfs_freesp() */
1854 zfs_dirent_unlock(dl);
1856 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1858 vnevent_create(ZTOV(zp), ct);
1863 getnewvnode_drop_reserve();
1865 zfs_dirent_unlock(dl);
1872 error = specvp_check(vpp, cr);
1875 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1876 zil_commit(zilog, 0);
1883 * Remove an entry from a directory.
1885 * IN: dvp - vnode of directory to remove entry from.
1886 * name - name of entry to remove.
1887 * cr - credentials of caller.
1888 * ct - caller context
1889 * flags - case flags
1891 * RETURN: 0 on success, error code on failure.
1895 * vp - ctime (if nlink > 0)
1898 uint64_t null_xattr = 0;
1902 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1905 znode_t *zp, *dzp = VTOZ(dvp);
1908 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1910 uint64_t acl_obj, xattr_obj;
1911 uint64_t xattr_obj_unlinked = 0;
1915 boolean_t may_delete_now, delete_now = FALSE;
1916 boolean_t unlinked, toobig = FALSE;
1918 pathname_t *realnmp = NULL;
1922 boolean_t waited = B_FALSE;
1926 zilog = zfsvfs->z_log;
1928 if (flags & FIGNORECASE) {
1938 * Attempt to lock directory; fail if entry doesn't exist.
1940 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1950 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1955 * Need to use rmdir for removing directories.
1957 if (vp->v_type == VDIR) {
1958 error = SET_ERROR(EPERM);
1962 vnevent_remove(vp, dvp, name, ct);
1965 dnlc_remove(dvp, realnmp->pn_buf);
1967 dnlc_remove(dvp, name);
1970 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1974 * We may delete the znode now, or we may put it in the unlinked set;
1975 * it depends on whether we're the last link, and on whether there are
1976 * other holds on the vnode. So we dmu_tx_hold() the right things to
1977 * allow for either case.
1980 tx = dmu_tx_create(zfsvfs->z_os);
1981 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1982 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1983 zfs_sa_upgrade_txholds(tx, zp);
1984 zfs_sa_upgrade_txholds(tx, dzp);
1985 if (may_delete_now) {
1987 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1988 /* if the file is too big, only hold_free a token amount */
1989 dmu_tx_hold_free(tx, zp->z_id, 0,
1990 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1993 /* are there any extended attributes? */
1994 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1995 &xattr_obj, sizeof (xattr_obj));
1996 if (error == 0 && xattr_obj) {
1997 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1999 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2000 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
2003 mutex_enter(&zp->z_lock);
2004 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
2005 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
2006 mutex_exit(&zp->z_lock);
2008 /* charge as an update -- would be nice not to charge at all */
2009 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2012 * Mark this transaction as typically resulting in a net free of space
2014 dmu_tx_mark_netfree(tx);
2016 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2018 zfs_dirent_unlock(dl);
2022 if (error == ERESTART) {
2036 * Remove the directory entry.
2038 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
2047 * Hold z_lock so that we can make sure that the ACL obj
2048 * hasn't changed. Could have been deleted due to
2051 mutex_enter(&zp->z_lock);
2053 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2054 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
2055 delete_now = may_delete_now && !toobig &&
2056 vp->v_count == 1 && !vn_has_cached_data(vp) &&
2057 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
2064 panic("zfs_remove: delete_now branch taken");
2066 if (xattr_obj_unlinked) {
2067 ASSERT3U(xzp->z_links, ==, 2);
2068 mutex_enter(&xzp->z_lock);
2069 xzp->z_unlinked = 1;
2071 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
2072 &xzp->z_links, sizeof (xzp->z_links), tx);
2073 ASSERT3U(error, ==, 0);
2074 mutex_exit(&xzp->z_lock);
2075 zfs_unlinked_add(xzp, tx);
2078 error = sa_remove(zp->z_sa_hdl,
2079 SA_ZPL_XATTR(zfsvfs), tx);
2081 error = sa_update(zp->z_sa_hdl,
2082 SA_ZPL_XATTR(zfsvfs), &null_xattr,
2083 sizeof (uint64_t), tx);
2088 ASSERT0(vp->v_count);
2090 mutex_exit(&zp->z_lock);
2091 zfs_znode_delete(zp, tx);
2092 } else if (unlinked) {
2093 mutex_exit(&zp->z_lock);
2094 zfs_unlinked_add(zp, tx);
2096 vp->v_vflag |= VV_NOSYNC;
2101 if (flags & FIGNORECASE)
2103 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2110 zfs_dirent_unlock(dl);
2117 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2118 zil_commit(zilog, 0);
2125 * Create a new directory and insert it into dvp using the name
2126 * provided. Return a pointer to the inserted directory.
2128 * IN: dvp - vnode of directory to add subdir to.
2129 * dirname - name of new directory.
2130 * vap - attributes of new directory.
2131 * cr - credentials of caller.
2132 * ct - caller context
2133 * flags - case flags
2134 * vsecp - ACL to be set
2136 * OUT: vpp - vnode of created directory.
2138 * RETURN: 0 on success, error code on failure.
2141 * dvp - ctime|mtime updated
2142 * vp - ctime|mtime|atime updated
2146 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
2147 caller_context_t *ct, int flags, vsecattr_t *vsecp)
2149 znode_t *zp, *dzp = VTOZ(dvp);
2150 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2159 gid_t gid = crgetgid(cr);
2160 zfs_acl_ids_t acl_ids;
2161 boolean_t fuid_dirtied;
2162 boolean_t waited = B_FALSE;
2164 ASSERT(vap->va_type == VDIR);
2167 * If we have an ephemeral id, ACL, or XVATTR then
2168 * make sure file system is at proper version
2171 ksid = crgetsid(cr, KSID_OWNER);
2173 uid = ksid_getid(ksid);
2176 if (zfsvfs->z_use_fuids == B_FALSE &&
2177 (vsecp || (vap->va_mask & AT_XVATTR) ||
2178 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2179 return (SET_ERROR(EINVAL));
2183 zilog = zfsvfs->z_log;
2185 if (dzp->z_pflags & ZFS_XATTR) {
2187 return (SET_ERROR(EINVAL));
2190 if (zfsvfs->z_utf8 && u8_validate(dirname,
2191 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2193 return (SET_ERROR(EILSEQ));
2195 if (flags & FIGNORECASE)
2198 if (vap->va_mask & AT_XVATTR) {
2199 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2200 crgetuid(cr), cr, vap->va_type)) != 0) {
2206 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2207 vsecp, &acl_ids)) != 0) {
2212 getnewvnode_reserve(1);
2215 * First make sure the new directory doesn't exist.
2217 * Existence is checked first to make sure we don't return
2218 * EACCES instead of EEXIST which can cause some applications
2224 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
2226 zfs_acl_ids_free(&acl_ids);
2227 getnewvnode_drop_reserve();
2232 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2233 zfs_acl_ids_free(&acl_ids);
2234 zfs_dirent_unlock(dl);
2235 getnewvnode_drop_reserve();
2240 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2241 zfs_acl_ids_free(&acl_ids);
2242 zfs_dirent_unlock(dl);
2243 getnewvnode_drop_reserve();
2245 return (SET_ERROR(EDQUOT));
2249 * Add a new entry to the directory.
2251 tx = dmu_tx_create(zfsvfs->z_os);
2252 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2253 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2254 fuid_dirtied = zfsvfs->z_fuid_dirty;
2256 zfs_fuid_txhold(zfsvfs, tx);
2257 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2258 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2259 acl_ids.z_aclp->z_acl_bytes);
2262 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2263 ZFS_SA_BASE_ATTR_SIZE);
2265 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2267 zfs_dirent_unlock(dl);
2268 if (error == ERESTART) {
2274 zfs_acl_ids_free(&acl_ids);
2276 getnewvnode_drop_reserve();
2284 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2287 zfs_fuid_sync(zfsvfs, tx);
2290 * Now put new name in parent dir.
2292 (void) zfs_link_create(dl, zp, tx, ZNEW);
2296 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2297 if (flags & FIGNORECASE)
2299 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2300 acl_ids.z_fuidp, vap);
2302 zfs_acl_ids_free(&acl_ids);
2306 getnewvnode_drop_reserve();
2308 zfs_dirent_unlock(dl);
2310 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2311 zil_commit(zilog, 0);
2318 * Remove a directory subdir entry. If the current working
2319 * directory is the same as the subdir to be removed, the
2322 * IN: dvp - vnode of directory to remove from.
2323 * name - name of directory to be removed.
2324 * cwd - vnode of current working directory.
2325 * cr - credentials of caller.
2326 * ct - caller context
2327 * flags - case flags
2329 * RETURN: 0 on success, error code on failure.
2332 * dvp - ctime|mtime updated
2336 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2337 caller_context_t *ct, int flags)
2339 znode_t *dzp = VTOZ(dvp);
2342 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2348 boolean_t waited = B_FALSE;
2352 zilog = zfsvfs->z_log;
2354 if (flags & FIGNORECASE)
2360 * Attempt to lock directory; fail if entry doesn't exist.
2362 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2370 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2374 if (vp->v_type != VDIR) {
2375 error = SET_ERROR(ENOTDIR);
2380 error = SET_ERROR(EINVAL);
2384 vnevent_rmdir(vp, dvp, name, ct);
2387 * Grab a lock on the directory to make sure that noone is
2388 * trying to add (or lookup) entries while we are removing it.
2390 rw_enter(&zp->z_name_lock, RW_WRITER);
2393 * Grab a lock on the parent pointer to make sure we play well
2394 * with the treewalk and directory rename code.
2396 rw_enter(&zp->z_parent_lock, RW_WRITER);
2398 tx = dmu_tx_create(zfsvfs->z_os);
2399 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2400 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2401 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2402 zfs_sa_upgrade_txholds(tx, zp);
2403 zfs_sa_upgrade_txholds(tx, dzp);
2404 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2406 rw_exit(&zp->z_parent_lock);
2407 rw_exit(&zp->z_name_lock);
2408 zfs_dirent_unlock(dl);
2410 if (error == ERESTART) {
2421 #ifdef FREEBSD_NAMECACHE
2425 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2428 uint64_t txtype = TX_RMDIR;
2429 if (flags & FIGNORECASE)
2431 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2436 rw_exit(&zp->z_parent_lock);
2437 rw_exit(&zp->z_name_lock);
2438 #ifdef FREEBSD_NAMECACHE
2442 zfs_dirent_unlock(dl);
2446 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2447 zil_commit(zilog, 0);
2454 * Read as many directory entries as will fit into the provided
2455 * buffer from the given directory cursor position (specified in
2456 * the uio structure).
2458 * IN: vp - vnode of directory to read.
2459 * uio - structure supplying read location, range info,
2460 * and return buffer.
2461 * cr - credentials of caller.
2462 * ct - caller context
2463 * flags - case flags
2465 * OUT: uio - updated offset and range, buffer filled.
2466 * eofp - set to true if end-of-file detected.
2468 * RETURN: 0 on success, error code on failure.
2471 * vp - atime updated
2473 * Note that the low 4 bits of the cookie returned by zap is always zero.
2474 * This allows us to use the low range for "special" directory entries:
2475 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2476 * we use the offset 2 for the '.zfs' directory.
2480 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2482 znode_t *zp = VTOZ(vp);
2486 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2491 zap_attribute_t zap;
2492 uint_t bytes_wanted;
2493 uint64_t offset; /* must be unsigned; checks for < 1 */
2499 boolean_t check_sysattrs;
2502 u_long *cooks = NULL;
2508 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2509 &parent, sizeof (parent))) != 0) {
2515 * If we are not given an eof variable,
2522 * Check for valid iov_len.
2524 if (uio->uio_iov->iov_len <= 0) {
2526 return (SET_ERROR(EINVAL));
2530 * Quit if directory has been removed (posix)
2532 if ((*eofp = zp->z_unlinked) != 0) {
2539 offset = uio->uio_loffset;
2540 prefetch = zp->z_zn_prefetch;
2543 * Initialize the iterator cursor.
2547 * Start iteration from the beginning of the directory.
2549 zap_cursor_init(&zc, os, zp->z_id);
2552 * The offset is a serialized cursor.
2554 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2558 * Get space to change directory entries into fs independent format.
2560 iovp = uio->uio_iov;
2561 bytes_wanted = iovp->iov_len;
2562 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2563 bufsize = bytes_wanted;
2564 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2565 odp = (struct dirent64 *)outbuf;
2567 bufsize = bytes_wanted;
2569 odp = (struct dirent64 *)iovp->iov_base;
2571 eodp = (struct edirent *)odp;
2573 if (ncookies != NULL) {
2575 * Minimum entry size is dirent size and 1 byte for a file name.
2577 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2578 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2583 * If this VFS supports the system attribute view interface; and
2584 * we're looking at an extended attribute directory; and we care
2585 * about normalization conflicts on this vfs; then we must check
2586 * for normalization conflicts with the sysattr name space.
2589 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2590 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2591 (flags & V_RDDIR_ENTFLAGS);
2597 * Transform to file-system independent format
2600 while (outcount < bytes_wanted) {
2603 off64_t *next = NULL;
2606 * Special case `.', `..', and `.zfs'.
2609 (void) strcpy(zap.za_name, ".");
2610 zap.za_normalization_conflict = 0;
2613 } else if (offset == 1) {
2614 (void) strcpy(zap.za_name, "..");
2615 zap.za_normalization_conflict = 0;
2618 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2619 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2620 zap.za_normalization_conflict = 0;
2621 objnum = ZFSCTL_INO_ROOT;
2627 if (error = zap_cursor_retrieve(&zc, &zap)) {
2628 if ((*eofp = (error == ENOENT)) != 0)
2634 if (zap.za_integer_length != 8 ||
2635 zap.za_num_integers != 1) {
2636 cmn_err(CE_WARN, "zap_readdir: bad directory "
2637 "entry, obj = %lld, offset = %lld\n",
2638 (u_longlong_t)zp->z_id,
2639 (u_longlong_t)offset);
2640 error = SET_ERROR(ENXIO);
2644 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2646 * MacOS X can extract the object type here such as:
2647 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2649 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2651 if (check_sysattrs && !zap.za_normalization_conflict) {
2653 zap.za_normalization_conflict =
2654 xattr_sysattr_casechk(zap.za_name);
2656 panic("%s:%u: TODO", __func__, __LINE__);
2661 if (flags & V_RDDIR_ACCFILTER) {
2663 * If we have no access at all, don't include
2664 * this entry in the returned information
2667 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2669 if (!zfs_has_access(ezp, cr)) {
2676 if (flags & V_RDDIR_ENTFLAGS)
2677 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2679 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2682 * Will this entry fit in the buffer?
2684 if (outcount + reclen > bufsize) {
2686 * Did we manage to fit anything in the buffer?
2689 error = SET_ERROR(EINVAL);
2694 if (flags & V_RDDIR_ENTFLAGS) {
2696 * Add extended flag entry:
2698 eodp->ed_ino = objnum;
2699 eodp->ed_reclen = reclen;
2700 /* NOTE: ed_off is the offset for the *next* entry */
2701 next = &(eodp->ed_off);
2702 eodp->ed_eflags = zap.za_normalization_conflict ?
2703 ED_CASE_CONFLICT : 0;
2704 (void) strncpy(eodp->ed_name, zap.za_name,
2705 EDIRENT_NAMELEN(reclen));
2706 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2711 odp->d_ino = objnum;
2712 odp->d_reclen = reclen;
2713 odp->d_namlen = strlen(zap.za_name);
2714 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2716 odp = (dirent64_t *)((intptr_t)odp + reclen);
2720 ASSERT(outcount <= bufsize);
2722 /* Prefetch znode */
2724 dmu_prefetch(os, objnum, 0, 0, 0,
2725 ZIO_PRIORITY_SYNC_READ);
2729 * Move to the next entry, fill in the previous offset.
2731 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2732 zap_cursor_advance(&zc);
2733 offset = zap_cursor_serialize(&zc);
2738 if (cooks != NULL) {
2741 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2744 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2746 /* Subtract unused cookies */
2747 if (ncookies != NULL)
2748 *ncookies -= ncooks;
2750 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2751 iovp->iov_base += outcount;
2752 iovp->iov_len -= outcount;
2753 uio->uio_resid -= outcount;
2754 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2756 * Reset the pointer.
2758 offset = uio->uio_loffset;
2762 zap_cursor_fini(&zc);
2763 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2764 kmem_free(outbuf, bufsize);
2766 if (error == ENOENT)
2769 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2771 uio->uio_loffset = offset;
2773 if (error != 0 && cookies != NULL) {
2774 free(*cookies, M_TEMP);
2781 ulong_t zfs_fsync_sync_cnt = 4;
2784 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2786 znode_t *zp = VTOZ(vp);
2787 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2789 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2791 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2794 zil_commit(zfsvfs->z_log, zp->z_id);
2802 * Get the requested file attributes and place them in the provided
2805 * IN: vp - vnode of file.
2806 * vap - va_mask identifies requested attributes.
2807 * If AT_XVATTR set, then optional attrs are requested
2808 * flags - ATTR_NOACLCHECK (CIFS server context)
2809 * cr - credentials of caller.
2810 * ct - caller context
2812 * OUT: vap - attribute values.
2814 * RETURN: 0 (always succeeds).
2818 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2819 caller_context_t *ct)
2821 znode_t *zp = VTOZ(vp);
2822 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2825 u_longlong_t nblocks;
2827 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2828 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2829 xoptattr_t *xoap = NULL;
2830 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2831 sa_bulk_attr_t bulk[4];
2837 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2839 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2840 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2841 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2842 if (vp->v_type == VBLK || vp->v_type == VCHR)
2843 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2846 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2852 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2853 * Also, if we are the owner don't bother, since owner should
2854 * always be allowed to read basic attributes of file.
2856 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2857 (vap->va_uid != crgetuid(cr))) {
2858 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2866 * Return all attributes. It's cheaper to provide the answer
2867 * than to determine whether we were asked the question.
2870 mutex_enter(&zp->z_lock);
2871 vap->va_type = IFTOVT(zp->z_mode);
2872 vap->va_mode = zp->z_mode & ~S_IFMT;
2874 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2876 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2878 vap->va_nodeid = zp->z_id;
2879 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2880 links = zp->z_links + 1;
2882 links = zp->z_links;
2883 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */
2884 vap->va_size = zp->z_size;
2886 vap->va_rdev = vp->v_rdev;
2888 if (vp->v_type == VBLK || vp->v_type == VCHR)
2889 vap->va_rdev = zfs_cmpldev(rdev);
2891 vap->va_seq = zp->z_seq;
2892 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2893 vap->va_filerev = zp->z_seq;
2896 * Add in any requested optional attributes and the create time.
2897 * Also set the corresponding bits in the returned attribute bitmap.
2899 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2900 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2902 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2903 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2906 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2907 xoap->xoa_readonly =
2908 ((zp->z_pflags & ZFS_READONLY) != 0);
2909 XVA_SET_RTN(xvap, XAT_READONLY);
2912 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2914 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2915 XVA_SET_RTN(xvap, XAT_SYSTEM);
2918 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2920 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2921 XVA_SET_RTN(xvap, XAT_HIDDEN);
2924 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2925 xoap->xoa_nounlink =
2926 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2927 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2930 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2931 xoap->xoa_immutable =
2932 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2933 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2936 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2937 xoap->xoa_appendonly =
2938 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2939 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2942 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2944 ((zp->z_pflags & ZFS_NODUMP) != 0);
2945 XVA_SET_RTN(xvap, XAT_NODUMP);
2948 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2950 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2951 XVA_SET_RTN(xvap, XAT_OPAQUE);
2954 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2955 xoap->xoa_av_quarantined =
2956 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2957 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2960 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2961 xoap->xoa_av_modified =
2962 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2963 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2966 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2967 vp->v_type == VREG) {
2968 zfs_sa_get_scanstamp(zp, xvap);
2971 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2974 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2975 times, sizeof (times));
2976 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2977 XVA_SET_RTN(xvap, XAT_CREATETIME);
2980 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2981 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2982 XVA_SET_RTN(xvap, XAT_REPARSE);
2984 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2985 xoap->xoa_generation = zp->z_gen;
2986 XVA_SET_RTN(xvap, XAT_GEN);
2989 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2991 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2992 XVA_SET_RTN(xvap, XAT_OFFLINE);
2995 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2997 ((zp->z_pflags & ZFS_SPARSE) != 0);
2998 XVA_SET_RTN(xvap, XAT_SPARSE);
3002 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
3003 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
3004 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
3005 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
3007 mutex_exit(&zp->z_lock);
3009 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
3010 vap->va_blksize = blksize;
3011 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
3013 if (zp->z_blksz == 0) {
3015 * Block size hasn't been set; suggest maximal I/O transfers.
3017 vap->va_blksize = zfsvfs->z_max_blksz;
3025 * Set the file attributes to the values contained in the
3028 * IN: vp - vnode of file to be modified.
3029 * vap - new attribute values.
3030 * If AT_XVATTR set, then optional attrs are being set
3031 * flags - ATTR_UTIME set if non-default time values provided.
3032 * - ATTR_NOACLCHECK (CIFS context only).
3033 * cr - credentials of caller.
3034 * ct - caller context
3036 * RETURN: 0 on success, error code on failure.
3039 * vp - ctime updated, mtime updated if size changed.
3043 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
3044 caller_context_t *ct)
3046 znode_t *zp = VTOZ(vp);
3047 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3052 uint_t mask = vap->va_mask;
3053 uint_t saved_mask = 0;
3054 uint64_t saved_mode;
3057 uint64_t new_uid, new_gid;
3059 uint64_t mtime[2], ctime[2];
3061 int need_policy = FALSE;
3063 zfs_fuid_info_t *fuidp = NULL;
3064 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
3067 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
3068 boolean_t fuid_dirtied = B_FALSE;
3069 sa_bulk_attr_t bulk[7], xattr_bulk[7];
3070 int count = 0, xattr_count = 0;
3075 if (mask & AT_NOSET)
3076 return (SET_ERROR(EINVAL));
3081 zilog = zfsvfs->z_log;
3084 * Make sure that if we have ephemeral uid/gid or xvattr specified
3085 * that file system is at proper version level
3088 if (zfsvfs->z_use_fuids == B_FALSE &&
3089 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
3090 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
3091 (mask & AT_XVATTR))) {
3093 return (SET_ERROR(EINVAL));
3096 if (mask & AT_SIZE && vp->v_type == VDIR) {
3098 return (SET_ERROR(EISDIR));
3101 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
3103 return (SET_ERROR(EINVAL));
3107 * If this is an xvattr_t, then get a pointer to the structure of
3108 * optional attributes. If this is NULL, then we have a vattr_t.
3110 xoap = xva_getxoptattr(xvap);
3112 xva_init(&tmpxvattr);
3115 * Immutable files can only alter immutable bit and atime
3117 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
3118 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
3119 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
3121 return (SET_ERROR(EPERM));
3124 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
3126 return (SET_ERROR(EPERM));
3130 * Verify timestamps doesn't overflow 32 bits.
3131 * ZFS can handle large timestamps, but 32bit syscalls can't
3132 * handle times greater than 2039. This check should be removed
3133 * once large timestamps are fully supported.
3135 if (mask & (AT_ATIME | AT_MTIME)) {
3136 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
3137 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
3139 return (SET_ERROR(EOVERFLOW));
3147 /* Can this be moved to before the top label? */
3148 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
3150 return (SET_ERROR(EROFS));
3154 * First validate permissions
3157 if (mask & AT_SIZE) {
3159 * XXX - Note, we are not providing any open
3160 * mode flags here (like FNDELAY), so we may
3161 * block if there are locks present... this
3162 * should be addressed in openat().
3164 /* XXX - would it be OK to generate a log record here? */
3165 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3172 if (mask & (AT_ATIME|AT_MTIME) ||
3173 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3174 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3175 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3176 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3177 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3178 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3179 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3180 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3184 if (mask & (AT_UID|AT_GID)) {
3185 int idmask = (mask & (AT_UID|AT_GID));
3190 * NOTE: even if a new mode is being set,
3191 * we may clear S_ISUID/S_ISGID bits.
3194 if (!(mask & AT_MODE))
3195 vap->va_mode = zp->z_mode;
3198 * Take ownership or chgrp to group we are a member of
3201 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3202 take_group = (mask & AT_GID) &&
3203 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3206 * If both AT_UID and AT_GID are set then take_owner and
3207 * take_group must both be set in order to allow taking
3210 * Otherwise, send the check through secpolicy_vnode_setattr()
3214 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3215 ((idmask == AT_UID) && take_owner) ||
3216 ((idmask == AT_GID) && take_group)) {
3217 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3218 skipaclchk, cr) == 0) {
3220 * Remove setuid/setgid for non-privileged users
3222 secpolicy_setid_clear(vap, vp, cr);
3223 trim_mask = (mask & (AT_UID|AT_GID));
3232 mutex_enter(&zp->z_lock);
3233 oldva.va_mode = zp->z_mode;
3234 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3235 if (mask & AT_XVATTR) {
3237 * Update xvattr mask to include only those attributes
3238 * that are actually changing.
3240 * the bits will be restored prior to actually setting
3241 * the attributes so the caller thinks they were set.
3243 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3244 if (xoap->xoa_appendonly !=
3245 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3248 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3249 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3253 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3254 if (xoap->xoa_nounlink !=
3255 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3258 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3259 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3263 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3264 if (xoap->xoa_immutable !=
3265 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3268 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3269 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3273 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3274 if (xoap->xoa_nodump !=
3275 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3278 XVA_CLR_REQ(xvap, XAT_NODUMP);
3279 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3283 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3284 if (xoap->xoa_av_modified !=
3285 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3288 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3289 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3293 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3294 if ((vp->v_type != VREG &&
3295 xoap->xoa_av_quarantined) ||
3296 xoap->xoa_av_quarantined !=
3297 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3300 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3301 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3305 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3306 mutex_exit(&zp->z_lock);
3308 return (SET_ERROR(EPERM));
3311 if (need_policy == FALSE &&
3312 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3313 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3318 mutex_exit(&zp->z_lock);
3320 if (mask & AT_MODE) {
3321 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3322 err = secpolicy_setid_setsticky_clear(vp, vap,
3328 trim_mask |= AT_MODE;
3336 * If trim_mask is set then take ownership
3337 * has been granted or write_acl is present and user
3338 * has the ability to modify mode. In that case remove
3339 * UID|GID and or MODE from mask so that
3340 * secpolicy_vnode_setattr() doesn't revoke it.
3344 saved_mask = vap->va_mask;
3345 vap->va_mask &= ~trim_mask;
3346 if (trim_mask & AT_MODE) {
3348 * Save the mode, as secpolicy_vnode_setattr()
3349 * will overwrite it with ova.va_mode.
3351 saved_mode = vap->va_mode;
3354 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3355 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3362 vap->va_mask |= saved_mask;
3363 if (trim_mask & AT_MODE) {
3365 * Recover the mode after
3366 * secpolicy_vnode_setattr().
3368 vap->va_mode = saved_mode;
3374 * secpolicy_vnode_setattr, or take ownership may have
3377 mask = vap->va_mask;
3379 if ((mask & (AT_UID | AT_GID))) {
3380 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3381 &xattr_obj, sizeof (xattr_obj));
3383 if (err == 0 && xattr_obj) {
3384 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3388 if (mask & AT_UID) {
3389 new_uid = zfs_fuid_create(zfsvfs,
3390 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3391 if (new_uid != zp->z_uid &&
3392 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3394 VN_RELE(ZTOV(attrzp));
3395 err = SET_ERROR(EDQUOT);
3400 if (mask & AT_GID) {
3401 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3402 cr, ZFS_GROUP, &fuidp);
3403 if (new_gid != zp->z_gid &&
3404 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3406 VN_RELE(ZTOV(attrzp));
3407 err = SET_ERROR(EDQUOT);
3412 tx = dmu_tx_create(zfsvfs->z_os);
3414 if (mask & AT_MODE) {
3415 uint64_t pmode = zp->z_mode;
3417 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3419 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3420 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3421 err = SET_ERROR(EPERM);
3425 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3428 mutex_enter(&zp->z_lock);
3429 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3431 * Are we upgrading ACL from old V0 format
3434 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3435 zfs_znode_acl_version(zp) ==
3436 ZFS_ACL_VERSION_INITIAL) {
3437 dmu_tx_hold_free(tx, acl_obj, 0,
3439 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3440 0, aclp->z_acl_bytes);
3442 dmu_tx_hold_write(tx, acl_obj, 0,
3445 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3446 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3447 0, aclp->z_acl_bytes);
3449 mutex_exit(&zp->z_lock);
3450 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3452 if ((mask & AT_XVATTR) &&
3453 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3454 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3456 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3460 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3463 fuid_dirtied = zfsvfs->z_fuid_dirty;
3465 zfs_fuid_txhold(zfsvfs, tx);
3467 zfs_sa_upgrade_txholds(tx, zp);
3469 err = dmu_tx_assign(tx, TXG_WAIT);
3475 * Set each attribute requested.
3476 * We group settings according to the locks they need to acquire.
3478 * Note: you cannot set ctime directly, although it will be
3479 * updated as a side-effect of calling this function.
3483 if (mask & (AT_UID|AT_GID|AT_MODE))
3484 mutex_enter(&zp->z_acl_lock);
3485 mutex_enter(&zp->z_lock);
3487 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3488 &zp->z_pflags, sizeof (zp->z_pflags));
3491 if (mask & (AT_UID|AT_GID|AT_MODE))
3492 mutex_enter(&attrzp->z_acl_lock);
3493 mutex_enter(&attrzp->z_lock);
3494 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3495 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3496 sizeof (attrzp->z_pflags));
3499 if (mask & (AT_UID|AT_GID)) {
3501 if (mask & AT_UID) {
3502 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3503 &new_uid, sizeof (new_uid));
3504 zp->z_uid = new_uid;
3506 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3507 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3509 attrzp->z_uid = new_uid;
3513 if (mask & AT_GID) {
3514 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3515 NULL, &new_gid, sizeof (new_gid));
3516 zp->z_gid = new_gid;
3518 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3519 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3521 attrzp->z_gid = new_gid;
3524 if (!(mask & AT_MODE)) {
3525 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3526 NULL, &new_mode, sizeof (new_mode));
3527 new_mode = zp->z_mode;
3529 err = zfs_acl_chown_setattr(zp);
3532 err = zfs_acl_chown_setattr(attrzp);
3537 if (mask & AT_MODE) {
3538 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3539 &new_mode, sizeof (new_mode));
3540 zp->z_mode = new_mode;
3541 ASSERT3U((uintptr_t)aclp, !=, 0);
3542 err = zfs_aclset_common(zp, aclp, cr, tx);
3544 if (zp->z_acl_cached)
3545 zfs_acl_free(zp->z_acl_cached);
3546 zp->z_acl_cached = aclp;
3551 if (mask & AT_ATIME) {
3552 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3553 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3554 &zp->z_atime, sizeof (zp->z_atime));
3557 if (mask & AT_MTIME) {
3558 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3559 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3560 mtime, sizeof (mtime));
3563 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3564 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3565 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3566 NULL, mtime, sizeof (mtime));
3567 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3568 &ctime, sizeof (ctime));
3569 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3571 } else if (mask != 0) {
3572 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3573 &ctime, sizeof (ctime));
3574 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3577 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3578 SA_ZPL_CTIME(zfsvfs), NULL,
3579 &ctime, sizeof (ctime));
3580 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3581 mtime, ctime, B_TRUE);
3585 * Do this after setting timestamps to prevent timestamp
3586 * update from toggling bit
3589 if (xoap && (mask & AT_XVATTR)) {
3592 * restore trimmed off masks
3593 * so that return masks can be set for caller.
3596 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3597 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3599 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3600 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3602 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3603 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3605 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3606 XVA_SET_REQ(xvap, XAT_NODUMP);
3608 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3609 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3611 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3612 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3615 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3616 ASSERT(vp->v_type == VREG);
3618 zfs_xvattr_set(zp, xvap, tx);
3622 zfs_fuid_sync(zfsvfs, tx);
3625 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3627 mutex_exit(&zp->z_lock);
3628 if (mask & (AT_UID|AT_GID|AT_MODE))
3629 mutex_exit(&zp->z_acl_lock);
3632 if (mask & (AT_UID|AT_GID|AT_MODE))
3633 mutex_exit(&attrzp->z_acl_lock);
3634 mutex_exit(&attrzp->z_lock);
3637 if (err == 0 && attrzp) {
3638 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3644 VN_RELE(ZTOV(attrzp));
3650 zfs_fuid_info_free(fuidp);
3656 if (err == ERESTART)
3659 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3664 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3665 zil_commit(zilog, 0);
3671 typedef struct zfs_zlock {
3672 krwlock_t *zl_rwlock; /* lock we acquired */
3673 znode_t *zl_znode; /* znode we held */
3674 struct zfs_zlock *zl_next; /* next in list */
3678 * Drop locks and release vnodes that were held by zfs_rename_lock().
3681 zfs_rename_unlock(zfs_zlock_t **zlpp)
3685 while ((zl = *zlpp) != NULL) {
3686 if (zl->zl_znode != NULL)
3687 VN_RELE(ZTOV(zl->zl_znode));
3688 rw_exit(zl->zl_rwlock);
3689 *zlpp = zl->zl_next;
3690 kmem_free(zl, sizeof (*zl));
3695 * Search back through the directory tree, using the ".." entries.
3696 * Lock each directory in the chain to prevent concurrent renames.
3697 * Fail any attempt to move a directory into one of its own descendants.
3698 * XXX - z_parent_lock can overlap with map or grow locks
3701 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3705 uint64_t rootid = zp->z_zfsvfs->z_root;
3706 uint64_t oidp = zp->z_id;
3707 krwlock_t *rwlp = &szp->z_parent_lock;
3708 krw_t rw = RW_WRITER;
3711 * First pass write-locks szp and compares to zp->z_id.
3712 * Later passes read-lock zp and compare to zp->z_parent.
3715 if (!rw_tryenter(rwlp, rw)) {
3717 * Another thread is renaming in this path.
3718 * Note that if we are a WRITER, we don't have any
3719 * parent_locks held yet.
3721 if (rw == RW_READER && zp->z_id > szp->z_id) {
3723 * Drop our locks and restart
3725 zfs_rename_unlock(&zl);
3729 rwlp = &szp->z_parent_lock;
3734 * Wait for other thread to drop its locks
3740 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3741 zl->zl_rwlock = rwlp;
3742 zl->zl_znode = NULL;
3743 zl->zl_next = *zlpp;
3746 if (oidp == szp->z_id) /* We're a descendant of szp */
3747 return (SET_ERROR(EINVAL));
3749 if (oidp == rootid) /* We've hit the top */
3752 if (rw == RW_READER) { /* i.e. not the first pass */
3753 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3758 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3759 &oidp, sizeof (oidp));
3760 rwlp = &zp->z_parent_lock;
3763 } while (zp->z_id != sdzp->z_id);
3769 * Move an entry from the provided source directory to the target
3770 * directory. Change the entry name as indicated.
3772 * IN: sdvp - Source directory containing the "old entry".
3773 * snm - Old entry name.
3774 * tdvp - Target directory to contain the "new entry".
3775 * tnm - New entry name.
3776 * cr - credentials of caller.
3777 * ct - caller context
3778 * flags - case flags
3780 * RETURN: 0 on success, error code on failure.
3783 * sdvp,tdvp - ctime|mtime updated
3787 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3788 caller_context_t *ct, int flags)
3790 znode_t *tdzp, *sdzp, *szp, *tzp;
3794 zfs_dirlock_t *sdl, *tdl;
3797 int cmp, serr, terr;
3800 boolean_t waited = B_FALSE;
3803 ZFS_VERIFY_ZP(tdzp);
3804 zfsvfs = tdzp->z_zfsvfs;
3806 zilog = zfsvfs->z_log;
3810 * In case sdzp is not valid, let's be sure to exit from the right
3813 if (sdzp->z_sa_hdl == NULL) {
3815 return (SET_ERROR(EIO));
3819 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3820 * ctldir appear to have the same v_vfsp.
3822 if (sdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) {
3824 return (SET_ERROR(EXDEV));
3827 if (zfsvfs->z_utf8 && u8_validate(tnm,
3828 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3830 return (SET_ERROR(EILSEQ));
3833 if (flags & FIGNORECASE)
3842 * This is to prevent the creation of links into attribute space
3843 * by renaming a linked file into/outof an attribute directory.
3844 * See the comment in zfs_link() for why this is considered bad.
3846 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3848 return (SET_ERROR(EINVAL));
3852 * Lock source and target directory entries. To prevent deadlock,
3853 * a lock ordering must be defined. We lock the directory with
3854 * the smallest object id first, or if it's a tie, the one with
3855 * the lexically first name.
3857 if (sdzp->z_id < tdzp->z_id) {
3859 } else if (sdzp->z_id > tdzp->z_id) {
3863 * First compare the two name arguments without
3864 * considering any case folding.
3866 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3868 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3869 ASSERT(error == 0 || !zfsvfs->z_utf8);
3872 * POSIX: "If the old argument and the new argument
3873 * both refer to links to the same existing file,
3874 * the rename() function shall return successfully
3875 * and perform no other action."
3881 * If the file system is case-folding, then we may
3882 * have some more checking to do. A case-folding file
3883 * system is either supporting mixed case sensitivity
3884 * access or is completely case-insensitive. Note
3885 * that the file system is always case preserving.
3887 * In mixed sensitivity mode case sensitive behavior
3888 * is the default. FIGNORECASE must be used to
3889 * explicitly request case insensitive behavior.
3891 * If the source and target names provided differ only
3892 * by case (e.g., a request to rename 'tim' to 'Tim'),
3893 * we will treat this as a special case in the
3894 * case-insensitive mode: as long as the source name
3895 * is an exact match, we will allow this to proceed as
3896 * a name-change request.
3898 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3899 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3900 flags & FIGNORECASE)) &&
3901 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3904 * case preserving rename request, require exact
3913 * If the source and destination directories are the same, we should
3914 * grab the z_name_lock of that directory only once.
3918 rw_enter(&sdzp->z_name_lock, RW_READER);
3922 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3923 ZEXISTS | zflg, NULL, NULL);
3924 terr = zfs_dirent_lock(&tdl,
3925 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3927 terr = zfs_dirent_lock(&tdl,
3928 tdzp, tnm, &tzp, zflg, NULL, NULL);
3929 serr = zfs_dirent_lock(&sdl,
3930 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3936 * Source entry invalid or not there.
3939 zfs_dirent_unlock(tdl);
3945 rw_exit(&sdzp->z_name_lock);
3948 * FreeBSD: In OpenSolaris they only check if rename source is
3949 * ".." here, because "." is handled in their lookup. This is
3950 * not the case for FreeBSD, so we check for "." explicitly.
3952 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0)
3953 serr = SET_ERROR(EINVAL);
3958 zfs_dirent_unlock(sdl);
3962 rw_exit(&sdzp->z_name_lock);
3964 if (strcmp(tnm, "..") == 0)
3965 terr = SET_ERROR(EINVAL);
3971 * Must have write access at the source to remove the old entry
3972 * and write access at the target to create the new entry.
3973 * Note that if target and source are the same, this can be
3974 * done in a single check.
3977 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3980 if (ZTOV(szp)->v_type == VDIR) {
3982 * Check to make sure rename is valid.
3983 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3985 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3990 * Does target exist?
3994 * Source and target must be the same type.
3996 if (ZTOV(szp)->v_type == VDIR) {
3997 if (ZTOV(tzp)->v_type != VDIR) {
3998 error = SET_ERROR(ENOTDIR);
4002 if (ZTOV(tzp)->v_type == VDIR) {
4003 error = SET_ERROR(EISDIR);
4008 * POSIX dictates that when the source and target
4009 * entries refer to the same file object, rename
4010 * must do nothing and exit without error.
4012 if (szp->z_id == tzp->z_id) {
4018 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
4020 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
4023 * notify the target directory if it is not the same
4024 * as source directory.
4027 vnevent_rename_dest_dir(tdvp, ct);
4030 tx = dmu_tx_create(zfsvfs->z_os);
4031 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4032 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
4033 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
4034 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
4036 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
4037 zfs_sa_upgrade_txholds(tx, tdzp);
4040 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
4041 zfs_sa_upgrade_txholds(tx, tzp);
4044 zfs_sa_upgrade_txholds(tx, szp);
4045 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
4046 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4049 zfs_rename_unlock(&zl);
4050 zfs_dirent_unlock(sdl);
4051 zfs_dirent_unlock(tdl);
4054 rw_exit(&sdzp->z_name_lock);
4059 if (error == ERESTART) {
4070 if (tzp) /* Attempt to remove the existing target */
4071 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
4074 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
4076 szp->z_pflags |= ZFS_AV_MODIFIED;
4078 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
4079 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
4082 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
4084 zfs_log_rename(zilog, tx, TX_RENAME |
4085 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
4086 sdl->dl_name, tdzp, tdl->dl_name, szp);
4089 * Update path information for the target vnode
4091 vn_renamepath(tdvp, ZTOV(szp), tnm,
4095 * At this point, we have successfully created
4096 * the target name, but have failed to remove
4097 * the source name. Since the create was done
4098 * with the ZRENAMING flag, there are
4099 * complications; for one, the link count is
4100 * wrong. The easiest way to deal with this
4101 * is to remove the newly created target, and
4102 * return the original error. This must
4103 * succeed; fortunately, it is very unlikely to
4104 * fail, since we just created it.
4106 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
4107 ZRENAMING, NULL), ==, 0);
4110 #ifdef FREEBSD_NAMECACHE
4114 cache_purge(ZTOV(szp));
4116 cache_purge(ZTOV(tzp));
4124 zfs_rename_unlock(&zl);
4126 zfs_dirent_unlock(sdl);
4127 zfs_dirent_unlock(tdl);
4130 rw_exit(&sdzp->z_name_lock);
4137 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4138 zil_commit(zilog, 0);
4146 * Insert the indicated symbolic reference entry into the directory.
4148 * IN: dvp - Directory to contain new symbolic link.
4149 * link - Name for new symlink entry.
4150 * vap - Attributes of new entry.
4151 * cr - credentials of caller.
4152 * ct - caller context
4153 * flags - case flags
4155 * RETURN: 0 on success, error code on failure.
4158 * dvp - ctime|mtime updated
4162 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
4163 cred_t *cr, kthread_t *td)
4165 znode_t *zp, *dzp = VTOZ(dvp);
4168 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4170 uint64_t len = strlen(link);
4173 zfs_acl_ids_t acl_ids;
4174 boolean_t fuid_dirtied;
4175 uint64_t txtype = TX_SYMLINK;
4176 boolean_t waited = B_FALSE;
4179 ASSERT(vap->va_type == VLNK);
4183 zilog = zfsvfs->z_log;
4185 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4186 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4188 return (SET_ERROR(EILSEQ));
4190 if (flags & FIGNORECASE)
4193 if (len > MAXPATHLEN) {
4195 return (SET_ERROR(ENAMETOOLONG));
4198 if ((error = zfs_acl_ids_create(dzp, 0,
4199 vap, cr, NULL, &acl_ids)) != 0) {
4204 getnewvnode_reserve(1);
4208 * Attempt to lock directory; fail if entry already exists.
4210 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
4212 zfs_acl_ids_free(&acl_ids);
4213 getnewvnode_drop_reserve();
4218 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4219 zfs_acl_ids_free(&acl_ids);
4220 zfs_dirent_unlock(dl);
4221 getnewvnode_drop_reserve();
4226 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4227 zfs_acl_ids_free(&acl_ids);
4228 zfs_dirent_unlock(dl);
4229 getnewvnode_drop_reserve();
4231 return (SET_ERROR(EDQUOT));
4233 tx = dmu_tx_create(zfsvfs->z_os);
4234 fuid_dirtied = zfsvfs->z_fuid_dirty;
4235 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4236 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4237 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4238 ZFS_SA_BASE_ATTR_SIZE + len);
4239 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4240 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4241 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4242 acl_ids.z_aclp->z_acl_bytes);
4245 zfs_fuid_txhold(zfsvfs, tx);
4246 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4248 zfs_dirent_unlock(dl);
4249 if (error == ERESTART) {
4255 zfs_acl_ids_free(&acl_ids);
4257 getnewvnode_drop_reserve();
4263 * Create a new object for the symlink.
4264 * for version 4 ZPL datsets the symlink will be an SA attribute
4266 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4269 zfs_fuid_sync(zfsvfs, tx);
4271 mutex_enter(&zp->z_lock);
4273 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4276 zfs_sa_symlink(zp, link, len, tx);
4277 mutex_exit(&zp->z_lock);
4280 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4281 &zp->z_size, sizeof (zp->z_size), tx);
4283 * Insert the new object into the directory.
4285 (void) zfs_link_create(dl, zp, tx, ZNEW);
4287 if (flags & FIGNORECASE)
4289 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4292 zfs_acl_ids_free(&acl_ids);
4296 getnewvnode_drop_reserve();
4298 zfs_dirent_unlock(dl);
4300 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4301 zil_commit(zilog, 0);
4308 * Return, in the buffer contained in the provided uio structure,
4309 * the symbolic path referred to by vp.
4311 * IN: vp - vnode of symbolic link.
4312 * uio - structure to contain the link path.
4313 * cr - credentials of caller.
4314 * ct - caller context
4316 * OUT: uio - structure containing the link path.
4318 * RETURN: 0 on success, error code on failure.
4321 * vp - atime updated
4325 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4327 znode_t *zp = VTOZ(vp);
4328 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4334 mutex_enter(&zp->z_lock);
4336 error = sa_lookup_uio(zp->z_sa_hdl,
4337 SA_ZPL_SYMLINK(zfsvfs), uio);
4339 error = zfs_sa_readlink(zp, uio);
4340 mutex_exit(&zp->z_lock);
4342 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4349 * Insert a new entry into directory tdvp referencing svp.
4351 * IN: tdvp - Directory to contain new entry.
4352 * svp - vnode of new entry.
4353 * name - name of new entry.
4354 * cr - credentials of caller.
4355 * ct - caller context
4357 * RETURN: 0 on success, error code on failure.
4360 * tdvp - ctime|mtime updated
4361 * svp - ctime updated
4365 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4366 caller_context_t *ct, int flags)
4368 znode_t *dzp = VTOZ(tdvp);
4370 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4379 boolean_t waited = B_FALSE;
4381 ASSERT(tdvp->v_type == VDIR);
4385 zilog = zfsvfs->z_log;
4387 if (VOP_REALVP(svp, &realvp, ct) == 0)
4391 * POSIX dictates that we return EPERM here.
4392 * Better choices include ENOTSUP or EISDIR.
4394 if (svp->v_type == VDIR) {
4396 return (SET_ERROR(EPERM));
4402 if (szp->z_pflags & (ZFS_APPENDONLY | ZFS_IMMUTABLE | ZFS_READONLY)) {
4404 return (SET_ERROR(EPERM));
4408 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
4409 * ctldir appear to have the same v_vfsp.
4411 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) {
4413 return (SET_ERROR(EXDEV));
4416 /* Prevent links to .zfs/shares files */
4418 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4419 &parent, sizeof (uint64_t))) != 0) {
4423 if (parent == zfsvfs->z_shares_dir) {
4425 return (SET_ERROR(EPERM));
4428 if (zfsvfs->z_utf8 && u8_validate(name,
4429 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4431 return (SET_ERROR(EILSEQ));
4433 if (flags & FIGNORECASE)
4437 * We do not support links between attributes and non-attributes
4438 * because of the potential security risk of creating links
4439 * into "normal" file space in order to circumvent restrictions
4440 * imposed in attribute space.
4442 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4444 return (SET_ERROR(EINVAL));
4448 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4449 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4451 return (SET_ERROR(EPERM));
4454 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4461 * Attempt to lock directory; fail if entry already exists.
4463 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4469 tx = dmu_tx_create(zfsvfs->z_os);
4470 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4471 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4472 zfs_sa_upgrade_txholds(tx, szp);
4473 zfs_sa_upgrade_txholds(tx, dzp);
4474 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4476 zfs_dirent_unlock(dl);
4477 if (error == ERESTART) {
4488 error = zfs_link_create(dl, szp, tx, 0);
4491 uint64_t txtype = TX_LINK;
4492 if (flags & FIGNORECASE)
4494 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4499 zfs_dirent_unlock(dl);
4502 vnevent_link(svp, ct);
4505 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4506 zil_commit(zilog, 0);
4514 * zfs_null_putapage() is used when the file system has been force
4515 * unmounted. It just drops the pages.
4519 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4520 size_t *lenp, int flags, cred_t *cr)
4522 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4527 * Push a page out to disk, klustering if possible.
4529 * IN: vp - file to push page to.
4530 * pp - page to push.
4531 * flags - additional flags.
4532 * cr - credentials of caller.
4534 * OUT: offp - start of range pushed.
4535 * lenp - len of range pushed.
4537 * RETURN: 0 on success, error code on failure.
4539 * NOTE: callers must have locked the page to be pushed. On
4540 * exit, the page (and all other pages in the kluster) must be
4545 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4546 size_t *lenp, int flags, cred_t *cr)
4548 znode_t *zp = VTOZ(vp);
4549 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4551 u_offset_t off, koff;
4558 * If our blocksize is bigger than the page size, try to kluster
4559 * multiple pages so that we write a full block (thus avoiding
4560 * a read-modify-write).
4562 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4563 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4564 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4565 ASSERT(koff <= zp->z_size);
4566 if (koff + klen > zp->z_size)
4567 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4568 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4570 ASSERT3U(btop(len), ==, btopr(len));
4573 * Can't push pages past end-of-file.
4575 if (off >= zp->z_size) {
4576 /* ignore all pages */
4579 } else if (off + len > zp->z_size) {
4580 int npages = btopr(zp->z_size - off);
4583 page_list_break(&pp, &trunc, npages);
4584 /* ignore pages past end of file */
4586 pvn_write_done(trunc, flags);
4587 len = zp->z_size - off;
4590 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4591 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4592 err = SET_ERROR(EDQUOT);
4595 tx = dmu_tx_create(zfsvfs->z_os);
4596 dmu_tx_hold_write(tx, zp->z_id, off, len);
4598 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4599 zfs_sa_upgrade_txholds(tx, zp);
4600 err = dmu_tx_assign(tx, TXG_WAIT);
4606 if (zp->z_blksz <= PAGESIZE) {
4607 caddr_t va = zfs_map_page(pp, S_READ);
4608 ASSERT3U(len, <=, PAGESIZE);
4609 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4610 zfs_unmap_page(pp, va);
4612 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4616 uint64_t mtime[2], ctime[2];
4617 sa_bulk_attr_t bulk[3];
4620 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4622 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4624 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4626 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4628 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4633 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4643 * Copy the portion of the file indicated from pages into the file.
4644 * The pages are stored in a page list attached to the files vnode.
4646 * IN: vp - vnode of file to push page data to.
4647 * off - position in file to put data.
4648 * len - amount of data to write.
4649 * flags - flags to control the operation.
4650 * cr - credentials of caller.
4651 * ct - caller context.
4653 * RETURN: 0 on success, error code on failure.
4656 * vp - ctime|mtime updated
4660 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4661 caller_context_t *ct)
4663 znode_t *zp = VTOZ(vp);
4664 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4676 * Align this request to the file block size in case we kluster.
4677 * XXX - this can result in pretty aggresive locking, which can
4678 * impact simultanious read/write access. One option might be
4679 * to break up long requests (len == 0) into block-by-block
4680 * operations to get narrower locking.
4682 blksz = zp->z_blksz;
4684 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4687 if (len > 0 && ISP2(blksz))
4688 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4694 * Search the entire vp list for pages >= io_off.
4696 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4697 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4700 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4702 if (off > zp->z_size) {
4703 /* past end of file */
4704 zfs_range_unlock(rl);
4709 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4711 for (off = io_off; io_off < off + len; io_off += io_len) {
4712 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4713 pp = page_lookup(vp, io_off,
4714 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4716 pp = page_lookup_nowait(vp, io_off,
4717 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4720 if (pp != NULL && pvn_getdirty(pp, flags)) {
4724 * Found a dirty page to push
4726 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4734 zfs_range_unlock(rl);
4735 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4736 zil_commit(zfsvfs->z_log, zp->z_id);
4740 #endif /* illumos */
4744 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4746 znode_t *zp = VTOZ(vp);
4747 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4750 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4751 if (zp->z_sa_hdl == NULL) {
4753 * The fs has been unmounted, or we did a
4754 * suspend/resume and this file no longer exists.
4756 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4761 mutex_enter(&zp->z_lock);
4762 if (zp->z_unlinked) {
4764 * Fast path to recycle a vnode of a removed file.
4766 mutex_exit(&zp->z_lock);
4767 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4771 mutex_exit(&zp->z_lock);
4773 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4774 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4776 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4777 zfs_sa_upgrade_txholds(tx, zp);
4778 error = dmu_tx_assign(tx, TXG_WAIT);
4782 mutex_enter(&zp->z_lock);
4783 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4784 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4785 zp->z_atime_dirty = 0;
4786 mutex_exit(&zp->z_lock);
4790 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4795 * Bounds-check the seek operation.
4797 * IN: vp - vnode seeking within
4798 * ooff - old file offset
4799 * noffp - pointer to new file offset
4800 * ct - caller context
4802 * RETURN: 0 on success, EINVAL if new offset invalid.
4806 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4807 caller_context_t *ct)
4809 if (vp->v_type == VDIR)
4811 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4815 * Pre-filter the generic locking function to trap attempts to place
4816 * a mandatory lock on a memory mapped file.
4819 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4820 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4822 znode_t *zp = VTOZ(vp);
4823 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4829 * We are following the UFS semantics with respect to mapcnt
4830 * here: If we see that the file is mapped already, then we will
4831 * return an error, but we don't worry about races between this
4832 * function and zfs_map().
4834 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4836 return (SET_ERROR(EAGAIN));
4839 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4843 * If we can't find a page in the cache, we will create a new page
4844 * and fill it with file data. For efficiency, we may try to fill
4845 * multiple pages at once (klustering) to fill up the supplied page
4846 * list. Note that the pages to be filled are held with an exclusive
4847 * lock to prevent access by other threads while they are being filled.
4850 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4851 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4853 znode_t *zp = VTOZ(vp);
4854 page_t *pp, *cur_pp;
4855 objset_t *os = zp->z_zfsvfs->z_os;
4856 u_offset_t io_off, total;
4860 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4862 * We only have a single page, don't bother klustering
4866 pp = page_create_va(vp, io_off, io_len,
4867 PG_EXCL | PG_WAIT, seg, addr);
4870 * Try to find enough pages to fill the page list
4872 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4873 &io_len, off, plsz, 0);
4877 * The page already exists, nothing to do here.
4884 * Fill the pages in the kluster.
4887 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4890 ASSERT3U(io_off, ==, cur_pp->p_offset);
4891 va = zfs_map_page(cur_pp, S_WRITE);
4892 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4894 zfs_unmap_page(cur_pp, va);
4896 /* On error, toss the entire kluster */
4897 pvn_read_done(pp, B_ERROR);
4898 /* convert checksum errors into IO errors */
4900 err = SET_ERROR(EIO);
4903 cur_pp = cur_pp->p_next;
4907 * Fill in the page list array from the kluster starting
4908 * from the desired offset `off'.
4909 * NOTE: the page list will always be null terminated.
4911 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4912 ASSERT(pl == NULL || (*pl)->p_offset == off);
4918 * Return pointers to the pages for the file region [off, off + len]
4919 * in the pl array. If plsz is greater than len, this function may
4920 * also return page pointers from after the specified region
4921 * (i.e. the region [off, off + plsz]). These additional pages are
4922 * only returned if they are already in the cache, or were created as
4923 * part of a klustered read.
4925 * IN: vp - vnode of file to get data from.
4926 * off - position in file to get data from.
4927 * len - amount of data to retrieve.
4928 * plsz - length of provided page list.
4929 * seg - segment to obtain pages for.
4930 * addr - virtual address of fault.
4931 * rw - mode of created pages.
4932 * cr - credentials of caller.
4933 * ct - caller context.
4935 * OUT: protp - protection mode of created pages.
4936 * pl - list of pages created.
4938 * RETURN: 0 on success, error code on failure.
4941 * vp - atime updated
4945 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4946 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4947 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4949 znode_t *zp = VTOZ(vp);
4950 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4954 /* we do our own caching, faultahead is unnecessary */
4957 else if (len > plsz)
4960 len = P2ROUNDUP(len, PAGESIZE);
4961 ASSERT(plsz >= len);
4970 * Loop through the requested range [off, off + len) looking
4971 * for pages. If we don't find a page, we will need to create
4972 * a new page and fill it with data from the file.
4975 if (*pl = page_lookup(vp, off, SE_SHARED))
4977 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4980 ASSERT3U((*pl)->p_offset, ==, off);
4984 ASSERT3U(len, >=, PAGESIZE);
4987 ASSERT3U(plsz, >=, PAGESIZE);
4994 * Fill out the page array with any pages already in the cache.
4997 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
5004 * Release any pages we have previously locked.
5009 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
5019 * Request a memory map for a section of a file. This code interacts
5020 * with common code and the VM system as follows:
5022 * - common code calls mmap(), which ends up in smmap_common()
5023 * - this calls VOP_MAP(), which takes you into (say) zfs
5024 * - zfs_map() calls as_map(), passing segvn_create() as the callback
5025 * - segvn_create() creates the new segment and calls VOP_ADDMAP()
5026 * - zfs_addmap() updates z_mapcnt
5030 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
5031 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
5032 caller_context_t *ct)
5034 znode_t *zp = VTOZ(vp);
5035 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5036 segvn_crargs_t vn_a;
5042 if ((prot & PROT_WRITE) && (zp->z_pflags &
5043 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
5045 return (SET_ERROR(EPERM));
5048 if ((prot & (PROT_READ | PROT_EXEC)) &&
5049 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
5051 return (SET_ERROR(EACCES));
5054 if (vp->v_flag & VNOMAP) {
5056 return (SET_ERROR(ENOSYS));
5059 if (off < 0 || len > MAXOFFSET_T - off) {
5061 return (SET_ERROR(ENXIO));
5064 if (vp->v_type != VREG) {
5066 return (SET_ERROR(ENODEV));
5070 * If file is locked, disallow mapping.
5072 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
5074 return (SET_ERROR(EAGAIN));
5078 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
5086 vn_a.offset = (u_offset_t)off;
5087 vn_a.type = flags & MAP_TYPE;
5089 vn_a.maxprot = maxprot;
5092 vn_a.flags = flags & ~MAP_TYPE;
5094 vn_a.lgrp_mem_policy_flags = 0;
5096 error = as_map(as, *addrp, len, segvn_create, &vn_a);
5105 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5106 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
5107 caller_context_t *ct)
5109 uint64_t pages = btopr(len);
5111 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
5116 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
5117 * more accurate mtime for the associated file. Since we don't have a way of
5118 * detecting when the data was actually modified, we have to resort to
5119 * heuristics. If an explicit msync() is done, then we mark the mtime when the
5120 * last page is pushed. The problem occurs when the msync() call is omitted,
5121 * which by far the most common case:
5129 * putpage() via fsflush
5131 * If we wait until fsflush to come along, we can have a modification time that
5132 * is some arbitrary point in the future. In order to prevent this in the
5133 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
5138 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5139 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
5140 caller_context_t *ct)
5142 uint64_t pages = btopr(len);
5144 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
5145 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
5147 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
5148 vn_has_cached_data(vp))
5149 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
5155 * Free or allocate space in a file. Currently, this function only
5156 * supports the `F_FREESP' command. However, this command is somewhat
5157 * misnamed, as its functionality includes the ability to allocate as
5158 * well as free space.
5160 * IN: vp - vnode of file to free data in.
5161 * cmd - action to take (only F_FREESP supported).
5162 * bfp - section of file to free/alloc.
5163 * flag - current file open mode flags.
5164 * offset - current file offset.
5165 * cr - credentials of caller [UNUSED].
5166 * ct - caller context.
5168 * RETURN: 0 on success, error code on failure.
5171 * vp - ctime|mtime updated
5175 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
5176 offset_t offset, cred_t *cr, caller_context_t *ct)
5178 znode_t *zp = VTOZ(vp);
5179 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5186 if (cmd != F_FREESP) {
5188 return (SET_ERROR(EINVAL));
5192 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
5193 * callers might not be able to detect properly that we are read-only,
5194 * so check it explicitly here.
5196 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
5198 return (SET_ERROR(EROFS));
5201 if (error = convoff(vp, bfp, 0, offset)) {
5206 if (bfp->l_len < 0) {
5208 return (SET_ERROR(EINVAL));
5212 len = bfp->l_len; /* 0 means from off to end of file */
5214 error = zfs_freesp(zp, off, len, flag, TRUE);
5219 #endif /* illumos */
5221 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
5222 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
5226 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
5228 znode_t *zp = VTOZ(vp);
5229 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5232 uint64_t object = zp->z_id;
5239 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
5240 &gen64, sizeof (uint64_t))) != 0) {
5245 gen = (uint32_t)gen64;
5247 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
5250 if (fidp->fid_len < size) {
5251 fidp->fid_len = size;
5253 return (SET_ERROR(ENOSPC));
5256 fidp->fid_len = size;
5259 zfid = (zfid_short_t *)fidp;
5261 zfid->zf_len = size;
5263 for (i = 0; i < sizeof (zfid->zf_object); i++)
5264 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
5266 /* Must have a non-zero generation number to distinguish from .zfs */
5269 for (i = 0; i < sizeof (zfid->zf_gen); i++)
5270 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
5272 if (size == LONG_FID_LEN) {
5273 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
5276 zlfid = (zfid_long_t *)fidp;
5278 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
5279 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
5281 /* XXX - this should be the generation number for the objset */
5282 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
5283 zlfid->zf_setgen[i] = 0;
5291 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5292 caller_context_t *ct)
5304 case _PC_FILESIZEBITS:
5308 case _PC_XATTR_EXISTS:
5310 zfsvfs = zp->z_zfsvfs;
5314 error = zfs_dirent_lock(&dl, zp, "", &xzp,
5315 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
5317 zfs_dirent_unlock(dl);
5318 if (!zfs_dirempty(xzp))
5321 } else if (error == ENOENT) {
5323 * If there aren't extended attributes, it's the
5324 * same as having zero of them.
5331 case _PC_SATTR_ENABLED:
5332 case _PC_SATTR_EXISTS:
5333 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5334 (vp->v_type == VREG || vp->v_type == VDIR);
5337 case _PC_ACCESS_FILTERING:
5338 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
5342 case _PC_ACL_ENABLED:
5343 *valp = _ACL_ACE_ENABLED;
5345 #endif /* illumos */
5346 case _PC_MIN_HOLE_SIZE:
5347 *valp = (int)SPA_MINBLOCKSIZE;
5350 case _PC_TIMESTAMP_RESOLUTION:
5351 /* nanosecond timestamp resolution */
5355 case _PC_ACL_EXTENDED:
5363 case _PC_ACL_PATH_MAX:
5364 *valp = ACL_MAX_ENTRIES;
5368 return (EOPNOTSUPP);
5374 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5375 caller_context_t *ct)
5377 znode_t *zp = VTOZ(vp);
5378 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5380 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5384 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5392 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5393 caller_context_t *ct)
5395 znode_t *zp = VTOZ(vp);
5396 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5398 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5399 zilog_t *zilog = zfsvfs->z_log;
5404 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5406 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5407 zil_commit(zilog, 0);
5415 * The smallest read we may consider to loan out an arcbuf.
5416 * This must be a power of 2.
5418 int zcr_blksz_min = (1 << 10); /* 1K */
5420 * If set to less than the file block size, allow loaning out of an
5421 * arcbuf for a partial block read. This must be a power of 2.
5423 int zcr_blksz_max = (1 << 17); /* 128K */
5427 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5428 caller_context_t *ct)
5430 znode_t *zp = VTOZ(vp);
5431 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5432 int max_blksz = zfsvfs->z_max_blksz;
5433 uio_t *uio = &xuio->xu_uio;
5434 ssize_t size = uio->uio_resid;
5435 offset_t offset = uio->uio_loffset;
5440 int preamble, postamble;
5442 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5443 return (SET_ERROR(EINVAL));
5450 * Loan out an arc_buf for write if write size is bigger than
5451 * max_blksz, and the file's block size is also max_blksz.
5454 if (size < blksz || zp->z_blksz != blksz) {
5456 return (SET_ERROR(EINVAL));
5459 * Caller requests buffers for write before knowing where the
5460 * write offset might be (e.g. NFS TCP write).
5465 preamble = P2PHASE(offset, blksz);
5467 preamble = blksz - preamble;
5472 postamble = P2PHASE(size, blksz);
5475 fullblk = size / blksz;
5476 (void) dmu_xuio_init(xuio,
5477 (preamble != 0) + fullblk + (postamble != 0));
5478 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5479 int, postamble, int,
5480 (preamble != 0) + fullblk + (postamble != 0));
5483 * Have to fix iov base/len for partial buffers. They
5484 * currently represent full arc_buf's.
5487 /* data begins in the middle of the arc_buf */
5488 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5491 (void) dmu_xuio_add(xuio, abuf,
5492 blksz - preamble, preamble);
5495 for (i = 0; i < fullblk; i++) {
5496 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5499 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5503 /* data ends in the middle of the arc_buf */
5504 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5507 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5512 * Loan out an arc_buf for read if the read size is larger than
5513 * the current file block size. Block alignment is not
5514 * considered. Partial arc_buf will be loaned out for read.
5516 blksz = zp->z_blksz;
5517 if (blksz < zcr_blksz_min)
5518 blksz = zcr_blksz_min;
5519 if (blksz > zcr_blksz_max)
5520 blksz = zcr_blksz_max;
5521 /* avoid potential complexity of dealing with it */
5522 if (blksz > max_blksz) {
5524 return (SET_ERROR(EINVAL));
5527 maxsize = zp->z_size - uio->uio_loffset;
5531 if (size < blksz || vn_has_cached_data(vp)) {
5533 return (SET_ERROR(EINVAL));
5538 return (SET_ERROR(EINVAL));
5541 uio->uio_extflg = UIO_XUIO;
5542 XUIO_XUZC_RW(xuio) = ioflag;
5549 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5553 int ioflag = XUIO_XUZC_RW(xuio);
5555 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5557 i = dmu_xuio_cnt(xuio);
5559 abuf = dmu_xuio_arcbuf(xuio, i);
5561 * if abuf == NULL, it must be a write buffer
5562 * that has been returned in zfs_write().
5565 dmu_return_arcbuf(abuf);
5566 ASSERT(abuf || ioflag == UIO_WRITE);
5569 dmu_xuio_fini(xuio);
5574 * Predeclare these here so that the compiler assumes that
5575 * this is an "old style" function declaration that does
5576 * not include arguments => we won't get type mismatch errors
5577 * in the initializations that follow.
5579 static int zfs_inval();
5580 static int zfs_isdir();
5585 return (SET_ERROR(EINVAL));
5591 return (SET_ERROR(EISDIR));
5594 * Directory vnode operations template
5596 vnodeops_t *zfs_dvnodeops;
5597 const fs_operation_def_t zfs_dvnodeops_template[] = {
5598 VOPNAME_OPEN, { .vop_open = zfs_open },
5599 VOPNAME_CLOSE, { .vop_close = zfs_close },
5600 VOPNAME_READ, { .error = zfs_isdir },
5601 VOPNAME_WRITE, { .error = zfs_isdir },
5602 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5603 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5604 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5605 VOPNAME_ACCESS, { .vop_access = zfs_access },
5606 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5607 VOPNAME_CREATE, { .vop_create = zfs_create },
5608 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5609 VOPNAME_LINK, { .vop_link = zfs_link },
5610 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5611 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5612 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5613 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5614 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5615 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5616 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5617 VOPNAME_FID, { .vop_fid = zfs_fid },
5618 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5619 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5620 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5621 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5622 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5627 * Regular file vnode operations template
5629 vnodeops_t *zfs_fvnodeops;
5630 const fs_operation_def_t zfs_fvnodeops_template[] = {
5631 VOPNAME_OPEN, { .vop_open = zfs_open },
5632 VOPNAME_CLOSE, { .vop_close = zfs_close },
5633 VOPNAME_READ, { .vop_read = zfs_read },
5634 VOPNAME_WRITE, { .vop_write = zfs_write },
5635 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5636 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5637 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5638 VOPNAME_ACCESS, { .vop_access = zfs_access },
5639 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5640 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5641 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5642 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5643 VOPNAME_FID, { .vop_fid = zfs_fid },
5644 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5645 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5646 VOPNAME_SPACE, { .vop_space = zfs_space },
5647 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5648 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5649 VOPNAME_MAP, { .vop_map = zfs_map },
5650 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5651 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5652 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5653 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5654 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5655 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5656 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5657 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5662 * Symbolic link vnode operations template
5664 vnodeops_t *zfs_symvnodeops;
5665 const fs_operation_def_t zfs_symvnodeops_template[] = {
5666 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5667 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5668 VOPNAME_ACCESS, { .vop_access = zfs_access },
5669 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5670 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5671 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5672 VOPNAME_FID, { .vop_fid = zfs_fid },
5673 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5674 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5679 * special share hidden files vnode operations template
5681 vnodeops_t *zfs_sharevnodeops;
5682 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5683 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5684 VOPNAME_ACCESS, { .vop_access = zfs_access },
5685 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5686 VOPNAME_FID, { .vop_fid = zfs_fid },
5687 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5688 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5689 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5690 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5695 * Extended attribute directory vnode operations template
5697 * This template is identical to the directory vnodes
5698 * operation template except for restricted operations:
5702 * Note that there are other restrictions embedded in:
5703 * zfs_create() - restrict type to VREG
5704 * zfs_link() - no links into/out of attribute space
5705 * zfs_rename() - no moves into/out of attribute space
5707 vnodeops_t *zfs_xdvnodeops;
5708 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5709 VOPNAME_OPEN, { .vop_open = zfs_open },
5710 VOPNAME_CLOSE, { .vop_close = zfs_close },
5711 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5712 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5713 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5714 VOPNAME_ACCESS, { .vop_access = zfs_access },
5715 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5716 VOPNAME_CREATE, { .vop_create = zfs_create },
5717 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5718 VOPNAME_LINK, { .vop_link = zfs_link },
5719 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5720 VOPNAME_MKDIR, { .error = zfs_inval },
5721 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5722 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5723 VOPNAME_SYMLINK, { .error = zfs_inval },
5724 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5725 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5726 VOPNAME_FID, { .vop_fid = zfs_fid },
5727 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5728 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5729 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5730 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5731 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5736 * Error vnode operations template
5738 vnodeops_t *zfs_evnodeops;
5739 const fs_operation_def_t zfs_evnodeops_template[] = {
5740 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5741 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5744 #endif /* illumos */
5747 ioflags(int ioflags)
5751 if (ioflags & IO_APPEND)
5753 if (ioflags & IO_NDELAY)
5755 if (ioflags & IO_SYNC)
5756 flags |= (FSYNC | FDSYNC | FRSYNC);
5762 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int reqpage)
5764 znode_t *zp = VTOZ(vp);
5765 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5766 objset_t *os = zp->z_zfsvfs->z_os;
5767 vm_page_t mfirst, mlast, mreq;
5771 off_t startoff, endoff;
5773 vm_pindex_t reqstart, reqend;
5774 int pcount, lsize, reqsize, size;
5779 pcount = OFF_TO_IDX(round_page(count));
5781 object = mreq->object;
5784 KASSERT(vp->v_object == object, ("mismatching object"));
5786 if (pcount > 1 && zp->z_blksz > PAGESIZE) {
5787 startoff = rounddown(IDX_TO_OFF(mreq->pindex), zp->z_blksz);
5788 reqstart = OFF_TO_IDX(round_page(startoff));
5789 if (reqstart < m[0]->pindex)
5792 reqstart = reqstart - m[0]->pindex;
5793 endoff = roundup(IDX_TO_OFF(mreq->pindex) + PAGE_SIZE,
5795 reqend = OFF_TO_IDX(trunc_page(endoff)) - 1;
5796 if (reqend > m[pcount - 1]->pindex)
5797 reqend = m[pcount - 1]->pindex;
5798 reqsize = reqend - m[reqstart]->pindex + 1;
5799 KASSERT(reqstart <= reqpage && reqpage < reqstart + reqsize,
5800 ("reqpage beyond [reqstart, reqstart + reqsize[ bounds"));
5805 mfirst = m[reqstart];
5806 mlast = m[reqstart + reqsize - 1];
5808 zfs_vmobject_wlock(object);
5810 for (i = 0; i < reqstart; i++) {
5813 vm_page_unlock(m[i]);
5815 for (i = reqstart + reqsize; i < pcount; i++) {
5818 vm_page_unlock(m[i]);
5821 if (mreq->valid && reqsize == 1) {
5822 if (mreq->valid != VM_PAGE_BITS_ALL)
5823 vm_page_zero_invalid(mreq, TRUE);
5824 zfs_vmobject_wunlock(object);
5826 return (zfs_vm_pagerret_ok);
5829 PCPU_INC(cnt.v_vnodein);
5830 PCPU_ADD(cnt.v_vnodepgsin, reqsize);
5832 if (IDX_TO_OFF(mreq->pindex) >= object->un_pager.vnp.vnp_size) {
5833 for (i = reqstart; i < reqstart + reqsize; i++) {
5837 vm_page_unlock(m[i]);
5840 zfs_vmobject_wunlock(object);
5842 return (zfs_vm_pagerret_bad);
5846 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size)
5847 lsize = object->un_pager.vnp.vnp_size - IDX_TO_OFF(mlast->pindex);
5849 zfs_vmobject_wunlock(object);
5851 for (i = reqstart; i < reqstart + reqsize; i++) {
5853 if (i == (reqstart + reqsize - 1))
5855 va = zfs_map_page(m[i], &sf);
5856 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex),
5857 size, va, DMU_READ_PREFETCH);
5858 if (size != PAGE_SIZE)
5859 bzero(va + size, PAGE_SIZE - size);
5865 zfs_vmobject_wlock(object);
5867 for (i = reqstart; i < reqstart + reqsize; i++) {
5869 m[i]->valid = VM_PAGE_BITS_ALL;
5870 KASSERT(m[i]->dirty == 0, ("zfs_getpages: page %p is dirty", m[i]));
5872 vm_page_readahead_finish(m[i]);
5875 zfs_vmobject_wunlock(object);
5877 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
5879 return (error ? zfs_vm_pagerret_error : zfs_vm_pagerret_ok);
5883 zfs_freebsd_getpages(ap)
5884 struct vop_getpages_args /* {
5889 vm_ooffset_t a_offset;
5893 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_reqpage));
5897 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
5900 znode_t *zp = VTOZ(vp);
5901 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5910 vm_ooffset_t lo_off;
5921 object = vp->v_object;
5925 KASSERT(ma[0]->object == object, ("mismatching object"));
5926 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
5928 for (i = 0; i < pcount; i++)
5929 rtvals[i] = zfs_vm_pagerret_error;
5931 off = IDX_TO_OFF(ma[0]->pindex);
5932 blksz = zp->z_blksz;
5933 lo_off = rounddown(off, blksz);
5934 lo_len = roundup(len + (off - lo_off), blksz);
5935 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER);
5937 zfs_vmobject_wlock(object);
5938 if (len + off > object->un_pager.vnp.vnp_size) {
5939 if (object->un_pager.vnp.vnp_size > off) {
5942 len = object->un_pager.vnp.vnp_size - off;
5944 if ((pgoff = (int)len & PAGE_MASK) != 0) {
5946 * If the object is locked and the following
5947 * conditions hold, then the page's dirty
5948 * field cannot be concurrently changed by a
5952 vm_page_assert_sbusied(m);
5953 KASSERT(!pmap_page_is_write_mapped(m),
5954 ("zfs_putpages: page %p is not read-only", m));
5955 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
5962 if (ncount < pcount) {
5963 for (i = ncount; i < pcount; i++) {
5964 rtvals[i] = zfs_vm_pagerret_bad;
5968 zfs_vmobject_wunlock(object);
5973 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
5974 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
5979 tx = dmu_tx_create(zfsvfs->z_os);
5980 dmu_tx_hold_write(tx, zp->z_id, off, len);
5982 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
5983 zfs_sa_upgrade_txholds(tx, zp);
5984 err = dmu_tx_assign(tx, TXG_NOWAIT);
5986 if (err == ERESTART) {
5995 if (zp->z_blksz < PAGE_SIZE) {
5997 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
5998 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
5999 va = zfs_map_page(ma[i], &sf);
6000 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
6004 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
6008 uint64_t mtime[2], ctime[2];
6009 sa_bulk_attr_t bulk[3];
6012 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
6014 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
6016 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
6018 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
6020 (void)sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
6021 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
6023 zfs_vmobject_wlock(object);
6024 for (i = 0; i < ncount; i++) {
6025 rtvals[i] = zfs_vm_pagerret_ok;
6026 vm_page_undirty(ma[i]);
6028 zfs_vmobject_wunlock(object);
6029 PCPU_INC(cnt.v_vnodeout);
6030 PCPU_ADD(cnt.v_vnodepgsout, ncount);
6035 zfs_range_unlock(rl);
6036 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
6037 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
6038 zil_commit(zfsvfs->z_log, zp->z_id);
6044 zfs_freebsd_putpages(ap)
6045 struct vop_putpages_args /* {
6051 vm_ooffset_t a_offset;
6055 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
6060 zfs_freebsd_bmap(ap)
6061 struct vop_bmap_args /* {
6064 struct bufobj **a_bop;
6071 if (ap->a_bop != NULL)
6072 *ap->a_bop = &ap->a_vp->v_bufobj;
6073 if (ap->a_bnp != NULL)
6074 *ap->a_bnp = ap->a_bn;
6075 if (ap->a_runp != NULL)
6077 if (ap->a_runb != NULL)
6084 zfs_freebsd_open(ap)
6085 struct vop_open_args /* {
6088 struct ucred *a_cred;
6089 struct thread *a_td;
6092 vnode_t *vp = ap->a_vp;
6093 znode_t *zp = VTOZ(vp);
6096 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
6098 vnode_create_vobject(vp, zp->z_size, ap->a_td);
6103 zfs_freebsd_close(ap)
6104 struct vop_close_args /* {
6107 struct ucred *a_cred;
6108 struct thread *a_td;
6112 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
6116 zfs_freebsd_ioctl(ap)
6117 struct vop_ioctl_args /* {
6127 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
6128 ap->a_fflag, ap->a_cred, NULL, NULL));
6132 zfs_freebsd_read(ap)
6133 struct vop_read_args /* {
6137 struct ucred *a_cred;
6141 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6146 zfs_freebsd_write(ap)
6147 struct vop_write_args /* {
6151 struct ucred *a_cred;
6155 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6160 zfs_freebsd_access(ap)
6161 struct vop_access_args /* {
6163 accmode_t a_accmode;
6164 struct ucred *a_cred;
6165 struct thread *a_td;
6168 vnode_t *vp = ap->a_vp;
6169 znode_t *zp = VTOZ(vp);
6174 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
6176 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
6178 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
6181 * VADMIN has to be handled by vaccess().
6184 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
6186 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
6187 zp->z_gid, accmode, ap->a_cred, NULL);
6192 * For VEXEC, ensure that at least one execute bit is set for
6195 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
6196 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
6204 zfs_freebsd_lookup(ap)
6205 struct vop_lookup_args /* {
6206 struct vnode *a_dvp;
6207 struct vnode **a_vpp;
6208 struct componentname *a_cnp;
6211 struct componentname *cnp = ap->a_cnp;
6212 char nm[NAME_MAX + 1];
6214 ASSERT(cnp->cn_namelen < sizeof(nm));
6215 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
6217 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
6218 cnp->cn_cred, cnp->cn_thread, 0));
6222 zfs_freebsd_create(ap)
6223 struct vop_create_args /* {
6224 struct vnode *a_dvp;
6225 struct vnode **a_vpp;
6226 struct componentname *a_cnp;
6227 struct vattr *a_vap;
6230 struct componentname *cnp = ap->a_cnp;
6231 vattr_t *vap = ap->a_vap;
6234 ASSERT(cnp->cn_flags & SAVENAME);
6236 vattr_init_mask(vap);
6237 mode = vap->va_mode & ALLPERMS;
6239 error = zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
6240 ap->a_vpp, cnp->cn_cred, cnp->cn_thread);
6241 #ifdef FREEBSD_NAMECACHE
6242 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
6243 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
6249 zfs_freebsd_remove(ap)
6250 struct vop_remove_args /* {
6251 struct vnode *a_dvp;
6253 struct componentname *a_cnp;
6257 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6259 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr,
6260 ap->a_cnp->cn_cred, NULL, 0));
6264 zfs_freebsd_mkdir(ap)
6265 struct vop_mkdir_args /* {
6266 struct vnode *a_dvp;
6267 struct vnode **a_vpp;
6268 struct componentname *a_cnp;
6269 struct vattr *a_vap;
6272 vattr_t *vap = ap->a_vap;
6274 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6276 vattr_init_mask(vap);
6278 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
6279 ap->a_cnp->cn_cred, NULL, 0, NULL));
6283 zfs_freebsd_rmdir(ap)
6284 struct vop_rmdir_args /* {
6285 struct vnode *a_dvp;
6287 struct componentname *a_cnp;
6290 struct componentname *cnp = ap->a_cnp;
6292 ASSERT(cnp->cn_flags & SAVENAME);
6294 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0));
6298 zfs_freebsd_readdir(ap)
6299 struct vop_readdir_args /* {
6302 struct ucred *a_cred;
6309 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
6310 ap->a_ncookies, ap->a_cookies));
6314 zfs_freebsd_fsync(ap)
6315 struct vop_fsync_args /* {
6318 struct thread *a_td;
6323 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
6327 zfs_freebsd_getattr(ap)
6328 struct vop_getattr_args /* {
6330 struct vattr *a_vap;
6331 struct ucred *a_cred;
6334 vattr_t *vap = ap->a_vap;
6340 xvap.xva_vattr = *vap;
6341 xvap.xva_vattr.va_mask |= AT_XVATTR;
6343 /* Convert chflags into ZFS-type flags. */
6344 /* XXX: what about SF_SETTABLE?. */
6345 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
6346 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
6347 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
6348 XVA_SET_REQ(&xvap, XAT_NODUMP);
6349 XVA_SET_REQ(&xvap, XAT_READONLY);
6350 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
6351 XVA_SET_REQ(&xvap, XAT_SYSTEM);
6352 XVA_SET_REQ(&xvap, XAT_HIDDEN);
6353 XVA_SET_REQ(&xvap, XAT_REPARSE);
6354 XVA_SET_REQ(&xvap, XAT_OFFLINE);
6355 XVA_SET_REQ(&xvap, XAT_SPARSE);
6357 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
6361 /* Convert ZFS xattr into chflags. */
6362 #define FLAG_CHECK(fflag, xflag, xfield) do { \
6363 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
6364 fflags |= (fflag); \
6366 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
6367 xvap.xva_xoptattrs.xoa_immutable);
6368 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
6369 xvap.xva_xoptattrs.xoa_appendonly);
6370 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
6371 xvap.xva_xoptattrs.xoa_nounlink);
6372 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
6373 xvap.xva_xoptattrs.xoa_archive);
6374 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
6375 xvap.xva_xoptattrs.xoa_nodump);
6376 FLAG_CHECK(UF_READONLY, XAT_READONLY,
6377 xvap.xva_xoptattrs.xoa_readonly);
6378 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
6379 xvap.xva_xoptattrs.xoa_system);
6380 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
6381 xvap.xva_xoptattrs.xoa_hidden);
6382 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
6383 xvap.xva_xoptattrs.xoa_reparse);
6384 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
6385 xvap.xva_xoptattrs.xoa_offline);
6386 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
6387 xvap.xva_xoptattrs.xoa_sparse);
6390 *vap = xvap.xva_vattr;
6391 vap->va_flags = fflags;
6396 zfs_freebsd_setattr(ap)
6397 struct vop_setattr_args /* {
6399 struct vattr *a_vap;
6400 struct ucred *a_cred;
6403 vnode_t *vp = ap->a_vp;
6404 vattr_t *vap = ap->a_vap;
6405 cred_t *cred = ap->a_cred;
6410 vattr_init_mask(vap);
6411 vap->va_mask &= ~AT_NOSET;
6414 xvap.xva_vattr = *vap;
6416 zflags = VTOZ(vp)->z_pflags;
6418 if (vap->va_flags != VNOVAL) {
6419 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
6422 if (zfsvfs->z_use_fuids == B_FALSE)
6423 return (EOPNOTSUPP);
6425 fflags = vap->va_flags;
6428 * We need to figure out whether it makes sense to allow
6429 * UF_REPARSE through, since we don't really have other
6430 * facilities to handle reparse points and zfs_setattr()
6431 * doesn't currently allow setting that attribute anyway.
6433 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
6434 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
6435 UF_OFFLINE|UF_SPARSE)) != 0)
6436 return (EOPNOTSUPP);
6438 * Unprivileged processes are not permitted to unset system
6439 * flags, or modify flags if any system flags are set.
6440 * Privileged non-jail processes may not modify system flags
6441 * if securelevel > 0 and any existing system flags are set.
6442 * Privileged jail processes behave like privileged non-jail
6443 * processes if the security.jail.chflags_allowed sysctl is
6444 * is non-zero; otherwise, they behave like unprivileged
6447 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
6448 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
6450 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6451 error = securelevel_gt(cred, 0);
6457 * Callers may only modify the file flags on objects they
6458 * have VADMIN rights for.
6460 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
6463 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6467 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
6472 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
6473 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
6474 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
6475 XVA_SET_REQ(&xvap, (xflag)); \
6476 (xfield) = ((fflags & (fflag)) != 0); \
6479 /* Convert chflags into ZFS-type flags. */
6480 /* XXX: what about SF_SETTABLE?. */
6481 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
6482 xvap.xva_xoptattrs.xoa_immutable);
6483 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
6484 xvap.xva_xoptattrs.xoa_appendonly);
6485 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
6486 xvap.xva_xoptattrs.xoa_nounlink);
6487 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
6488 xvap.xva_xoptattrs.xoa_archive);
6489 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
6490 xvap.xva_xoptattrs.xoa_nodump);
6491 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
6492 xvap.xva_xoptattrs.xoa_readonly);
6493 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
6494 xvap.xva_xoptattrs.xoa_system);
6495 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
6496 xvap.xva_xoptattrs.xoa_hidden);
6497 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
6498 xvap.xva_xoptattrs.xoa_hidden);
6499 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
6500 xvap.xva_xoptattrs.xoa_offline);
6501 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
6502 xvap.xva_xoptattrs.xoa_sparse);
6505 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
6509 zfs_freebsd_rename(ap)
6510 struct vop_rename_args /* {
6511 struct vnode *a_fdvp;
6512 struct vnode *a_fvp;
6513 struct componentname *a_fcnp;
6514 struct vnode *a_tdvp;
6515 struct vnode *a_tvp;
6516 struct componentname *a_tcnp;
6519 vnode_t *fdvp = ap->a_fdvp;
6520 vnode_t *fvp = ap->a_fvp;
6521 vnode_t *tdvp = ap->a_tdvp;
6522 vnode_t *tvp = ap->a_tvp;
6525 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
6526 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
6529 * Check for cross-device rename.
6531 if ((fdvp->v_mount != tdvp->v_mount) ||
6532 (tvp && (fdvp->v_mount != tvp->v_mount)))
6535 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp,
6536 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0);
6550 zfs_freebsd_symlink(ap)
6551 struct vop_symlink_args /* {
6552 struct vnode *a_dvp;
6553 struct vnode **a_vpp;
6554 struct componentname *a_cnp;
6555 struct vattr *a_vap;
6559 struct componentname *cnp = ap->a_cnp;
6560 vattr_t *vap = ap->a_vap;
6562 ASSERT(cnp->cn_flags & SAVENAME);
6564 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
6565 vattr_init_mask(vap);
6567 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
6568 ap->a_target, cnp->cn_cred, cnp->cn_thread));
6572 zfs_freebsd_readlink(ap)
6573 struct vop_readlink_args /* {
6576 struct ucred *a_cred;
6580 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
6584 zfs_freebsd_link(ap)
6585 struct vop_link_args /* {
6586 struct vnode *a_tdvp;
6588 struct componentname *a_cnp;
6591 struct componentname *cnp = ap->a_cnp;
6592 vnode_t *vp = ap->a_vp;
6593 vnode_t *tdvp = ap->a_tdvp;
6595 if (tdvp->v_mount != vp->v_mount)
6598 ASSERT(cnp->cn_flags & SAVENAME);
6600 return (zfs_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
6604 zfs_freebsd_inactive(ap)
6605 struct vop_inactive_args /* {
6607 struct thread *a_td;
6610 vnode_t *vp = ap->a_vp;
6612 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
6617 zfs_freebsd_reclaim(ap)
6618 struct vop_reclaim_args /* {
6620 struct thread *a_td;
6623 vnode_t *vp = ap->a_vp;
6624 znode_t *zp = VTOZ(vp);
6625 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6629 /* Destroy the vm object and flush associated pages. */
6630 vnode_destroy_vobject(vp);
6633 * z_teardown_inactive_lock protects from a race with
6634 * zfs_znode_dmu_fini in zfsvfs_teardown during
6637 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
6638 if (zp->z_sa_hdl == NULL)
6642 rw_exit(&zfsvfs->z_teardown_inactive_lock);
6650 struct vop_fid_args /* {
6656 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
6660 zfs_freebsd_pathconf(ap)
6661 struct vop_pathconf_args /* {
6664 register_t *a_retval;
6670 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
6672 *ap->a_retval = val;
6673 else if (error == EOPNOTSUPP)
6674 error = vop_stdpathconf(ap);
6679 zfs_freebsd_fifo_pathconf(ap)
6680 struct vop_pathconf_args /* {
6683 register_t *a_retval;
6687 switch (ap->a_name) {
6688 case _PC_ACL_EXTENDED:
6690 case _PC_ACL_PATH_MAX:
6691 case _PC_MAC_PRESENT:
6692 return (zfs_freebsd_pathconf(ap));
6694 return (fifo_specops.vop_pathconf(ap));
6699 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
6700 * extended attribute name:
6703 * system freebsd:system:
6704 * user (none, can be used to access ZFS fsattr(5) attributes
6705 * created on Solaris)
6708 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
6711 const char *namespace, *prefix, *suffix;
6713 /* We don't allow '/' character in attribute name. */
6714 if (strchr(name, '/') != NULL)
6716 /* We don't allow attribute names that start with "freebsd:" string. */
6717 if (strncmp(name, "freebsd:", 8) == 0)
6720 bzero(attrname, size);
6722 switch (attrnamespace) {
6723 case EXTATTR_NAMESPACE_USER:
6725 prefix = "freebsd:";
6726 namespace = EXTATTR_NAMESPACE_USER_STRING;
6730 * This is the default namespace by which we can access all
6731 * attributes created on Solaris.
6733 prefix = namespace = suffix = "";
6736 case EXTATTR_NAMESPACE_SYSTEM:
6737 prefix = "freebsd:";
6738 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
6741 case EXTATTR_NAMESPACE_EMPTY:
6745 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
6747 return (ENAMETOOLONG);
6753 * Vnode operating to retrieve a named extended attribute.
6756 zfs_getextattr(struct vop_getextattr_args *ap)
6759 IN struct vnode *a_vp;
6760 IN int a_attrnamespace;
6761 IN const char *a_name;
6762 INOUT struct uio *a_uio;
6764 IN struct ucred *a_cred;
6765 IN struct thread *a_td;
6769 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6770 struct thread *td = ap->a_td;
6771 struct nameidata nd;
6774 vnode_t *xvp = NULL, *vp;
6777 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6778 ap->a_cred, ap->a_td, VREAD);
6782 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6789 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6797 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
6799 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
6801 NDFREE(&nd, NDF_ONLY_PNBUF);
6804 if (error == ENOENT)
6809 if (ap->a_size != NULL) {
6810 error = VOP_GETATTR(vp, &va, ap->a_cred);
6812 *ap->a_size = (size_t)va.va_size;
6813 } else if (ap->a_uio != NULL)
6814 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6817 vn_close(vp, flags, ap->a_cred, td);
6824 * Vnode operation to remove a named attribute.
6827 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
6830 IN struct vnode *a_vp;
6831 IN int a_attrnamespace;
6832 IN const char *a_name;
6833 IN struct ucred *a_cred;
6834 IN struct thread *a_td;
6838 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6839 struct thread *td = ap->a_td;
6840 struct nameidata nd;
6843 vnode_t *xvp = NULL, *vp;
6846 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6847 ap->a_cred, ap->a_td, VWRITE);
6851 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6858 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6865 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
6866 UIO_SYSSPACE, attrname, xvp, td);
6871 NDFREE(&nd, NDF_ONLY_PNBUF);
6872 if (error == ENOENT)
6877 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
6878 NDFREE(&nd, NDF_ONLY_PNBUF);
6881 if (vp == nd.ni_dvp)
6891 * Vnode operation to set a named attribute.
6894 zfs_setextattr(struct vop_setextattr_args *ap)
6897 IN struct vnode *a_vp;
6898 IN int a_attrnamespace;
6899 IN const char *a_name;
6900 INOUT struct uio *a_uio;
6901 IN struct ucred *a_cred;
6902 IN struct thread *a_td;
6906 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6907 struct thread *td = ap->a_td;
6908 struct nameidata nd;
6911 vnode_t *xvp = NULL, *vp;
6914 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6915 ap->a_cred, ap->a_td, VWRITE);
6919 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6926 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6927 LOOKUP_XATTR | CREATE_XATTR_DIR);
6933 flags = FFLAGS(O_WRONLY | O_CREAT);
6934 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
6936 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
6938 NDFREE(&nd, NDF_ONLY_PNBUF);
6946 error = VOP_SETATTR(vp, &va, ap->a_cred);
6948 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6951 vn_close(vp, flags, ap->a_cred, td);
6958 * Vnode operation to retrieve extended attributes on a vnode.
6961 zfs_listextattr(struct vop_listextattr_args *ap)
6964 IN struct vnode *a_vp;
6965 IN int a_attrnamespace;
6966 INOUT struct uio *a_uio;
6968 IN struct ucred *a_cred;
6969 IN struct thread *a_td;
6973 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6974 struct thread *td = ap->a_td;
6975 struct nameidata nd;
6976 char attrprefix[16];
6977 u_char dirbuf[sizeof(struct dirent)];
6980 struct uio auio, *uio = ap->a_uio;
6981 size_t *sizep = ap->a_size;
6983 vnode_t *xvp = NULL, *vp;
6984 int done, error, eof, pos;
6986 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6987 ap->a_cred, ap->a_td, VREAD);
6991 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
6992 sizeof(attrprefix));
6995 plen = strlen(attrprefix);
7002 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
7007 * ENOATTR means that the EA directory does not yet exist,
7008 * i.e. there are no extended attributes there.
7010 if (error == ENOATTR)
7015 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
7016 UIO_SYSSPACE, ".", xvp, td);
7019 NDFREE(&nd, NDF_ONLY_PNBUF);
7025 auio.uio_iov = &aiov;
7026 auio.uio_iovcnt = 1;
7027 auio.uio_segflg = UIO_SYSSPACE;
7029 auio.uio_rw = UIO_READ;
7030 auio.uio_offset = 0;
7035 aiov.iov_base = (void *)dirbuf;
7036 aiov.iov_len = sizeof(dirbuf);
7037 auio.uio_resid = sizeof(dirbuf);
7038 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
7039 done = sizeof(dirbuf) - auio.uio_resid;
7042 for (pos = 0; pos < done;) {
7043 dp = (struct dirent *)(dirbuf + pos);
7044 pos += dp->d_reclen;
7046 * XXX: Temporarily we also accept DT_UNKNOWN, as this
7047 * is what we get when attribute was created on Solaris.
7049 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
7051 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
7053 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
7055 nlen = dp->d_namlen - plen;
7058 else if (uio != NULL) {
7060 * Format of extattr name entry is one byte for
7061 * length and the rest for name.
7063 error = uiomove(&nlen, 1, uio->uio_rw, uio);
7065 error = uiomove(dp->d_name + plen, nlen,
7072 } while (!eof && error == 0);
7081 zfs_freebsd_getacl(ap)
7082 struct vop_getacl_args /* {
7091 vsecattr_t vsecattr;
7093 if (ap->a_type != ACL_TYPE_NFS4)
7096 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
7097 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
7100 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
7101 if (vsecattr.vsa_aclentp != NULL)
7102 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
7108 zfs_freebsd_setacl(ap)
7109 struct vop_setacl_args /* {
7118 vsecattr_t vsecattr;
7119 int aclbsize; /* size of acl list in bytes */
7122 if (ap->a_type != ACL_TYPE_NFS4)
7125 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
7129 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
7130 * splitting every entry into two and appending "canonical six"
7131 * entries at the end. Don't allow for setting an ACL that would
7132 * cause chmod(2) to run out of ACL entries.
7134 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
7137 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
7141 vsecattr.vsa_mask = VSA_ACE;
7142 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
7143 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
7144 aaclp = vsecattr.vsa_aclentp;
7145 vsecattr.vsa_aclentsz = aclbsize;
7147 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
7148 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
7149 kmem_free(aaclp, aclbsize);
7155 zfs_freebsd_aclcheck(ap)
7156 struct vop_aclcheck_args /* {
7165 return (EOPNOTSUPP);
7168 struct vop_vector zfs_vnodeops;
7169 struct vop_vector zfs_fifoops;
7170 struct vop_vector zfs_shareops;
7172 struct vop_vector zfs_vnodeops = {
7173 .vop_default = &default_vnodeops,
7174 .vop_inactive = zfs_freebsd_inactive,
7175 .vop_reclaim = zfs_freebsd_reclaim,
7176 .vop_access = zfs_freebsd_access,
7177 #ifdef FREEBSD_NAMECACHE
7178 .vop_lookup = vfs_cache_lookup,
7179 .vop_cachedlookup = zfs_freebsd_lookup,
7181 .vop_lookup = zfs_freebsd_lookup,
7183 .vop_getattr = zfs_freebsd_getattr,
7184 .vop_setattr = zfs_freebsd_setattr,
7185 .vop_create = zfs_freebsd_create,
7186 .vop_mknod = zfs_freebsd_create,
7187 .vop_mkdir = zfs_freebsd_mkdir,
7188 .vop_readdir = zfs_freebsd_readdir,
7189 .vop_fsync = zfs_freebsd_fsync,
7190 .vop_open = zfs_freebsd_open,
7191 .vop_close = zfs_freebsd_close,
7192 .vop_rmdir = zfs_freebsd_rmdir,
7193 .vop_ioctl = zfs_freebsd_ioctl,
7194 .vop_link = zfs_freebsd_link,
7195 .vop_symlink = zfs_freebsd_symlink,
7196 .vop_readlink = zfs_freebsd_readlink,
7197 .vop_read = zfs_freebsd_read,
7198 .vop_write = zfs_freebsd_write,
7199 .vop_remove = zfs_freebsd_remove,
7200 .vop_rename = zfs_freebsd_rename,
7201 .vop_pathconf = zfs_freebsd_pathconf,
7202 .vop_bmap = zfs_freebsd_bmap,
7203 .vop_fid = zfs_freebsd_fid,
7204 .vop_getextattr = zfs_getextattr,
7205 .vop_deleteextattr = zfs_deleteextattr,
7206 .vop_setextattr = zfs_setextattr,
7207 .vop_listextattr = zfs_listextattr,
7208 .vop_getacl = zfs_freebsd_getacl,
7209 .vop_setacl = zfs_freebsd_setacl,
7210 .vop_aclcheck = zfs_freebsd_aclcheck,
7211 .vop_getpages = zfs_freebsd_getpages,
7212 .vop_putpages = zfs_freebsd_putpages,
7215 struct vop_vector zfs_fifoops = {
7216 .vop_default = &fifo_specops,
7217 .vop_fsync = zfs_freebsd_fsync,
7218 .vop_access = zfs_freebsd_access,
7219 .vop_getattr = zfs_freebsd_getattr,
7220 .vop_inactive = zfs_freebsd_inactive,
7221 .vop_read = VOP_PANIC,
7222 .vop_reclaim = zfs_freebsd_reclaim,
7223 .vop_setattr = zfs_freebsd_setattr,
7224 .vop_write = VOP_PANIC,
7225 .vop_pathconf = zfs_freebsd_fifo_pathconf,
7226 .vop_fid = zfs_freebsd_fid,
7227 .vop_getacl = zfs_freebsd_getacl,
7228 .vop_setacl = zfs_freebsd_setacl,
7229 .vop_aclcheck = zfs_freebsd_aclcheck,
7233 * special share hidden files vnode operations template
7235 struct vop_vector zfs_shareops = {
7236 .vop_default = &default_vnodeops,
7237 .vop_access = zfs_freebsd_access,
7238 .vop_inactive = zfs_freebsd_inactive,
7239 .vop_reclaim = zfs_freebsd_reclaim,
7240 .vop_fid = zfs_freebsd_fid,
7241 .vop_pathconf = zfs_freebsd_pathconf,