FreeBSD: Parameterize ZFS_ENTER/ZFS_VERIFY_VP with an error code
[zfs.git] / module / os / freebsd / zfs / zfs_vnops_os.c
blob212c05cb863e63349ee32c776d220c52450eb832
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
26 * Copyright 2017 Nexenta Systems, Inc.
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <sys/systm.h>
37 #include <sys/sysmacros.h>
38 #include <sys/resource.h>
39 #include <sys/vfs.h>
40 #include <sys/endian.h>
41 #include <sys/vm.h>
42 #include <sys/vnode.h>
43 #if __FreeBSD_version >= 1300102
44 #include <sys/smr.h>
45 #endif
46 #include <sys/dirent.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include <sys/kmem.h>
50 #include <sys/taskq.h>
51 #include <sys/uio.h>
52 #include <sys/atomic.h>
53 #include <sys/namei.h>
54 #include <sys/mman.h>
55 #include <sys/cmn_err.h>
56 #include <sys/kdb.h>
57 #include <sys/sysproto.h>
58 #include <sys/errno.h>
59 #include <sys/unistd.h>
60 #include <sys/zfs_dir.h>
61 #include <sys/zfs_ioctl.h>
62 #include <sys/fs/zfs.h>
63 #include <sys/dmu.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/spa.h>
66 #include <sys/txg.h>
67 #include <sys/dbuf.h>
68 #include <sys/zap.h>
69 #include <sys/sa.h>
70 #include <sys/policy.h>
71 #include <sys/sunddi.h>
72 #include <sys/filio.h>
73 #include <sys/sid.h>
74 #include <sys/zfs_ctldir.h>
75 #include <sys/zfs_fuid.h>
76 #include <sys/zfs_quota.h>
77 #include <sys/zfs_sa.h>
78 #include <sys/zfs_rlock.h>
79 #include <sys/extdirent.h>
80 #include <sys/bio.h>
81 #include <sys/buf.h>
82 #include <sys/sched.h>
83 #include <sys/acl.h>
84 #include <sys/vmmeter.h>
85 #include <vm/vm_param.h>
86 #include <sys/zil.h>
87 #include <sys/zfs_vnops.h>
89 #include <vm/vm_object.h>
91 #include <sys/extattr.h>
92 #include <sys/priv.h>
94 #ifndef VN_OPEN_INVFS
95 #define VN_OPEN_INVFS 0x0
96 #endif
98 VFS_SMR_DECLARE;
100 #if __FreeBSD_version < 1300103
101 #define NDFREE_PNBUF(ndp) NDFREE((ndp), NDF_ONLY_PNBUF)
102 #endif
104 #if __FreeBSD_version >= 1300047
105 #define vm_page_wire_lock(pp)
106 #define vm_page_wire_unlock(pp)
107 #else
108 #define vm_page_wire_lock(pp) vm_page_lock(pp)
109 #define vm_page_wire_unlock(pp) vm_page_unlock(pp)
110 #endif
112 #ifdef DEBUG_VFS_LOCKS
113 #define VNCHECKREF(vp) \
114 VNASSERT((vp)->v_holdcnt > 0 && (vp)->v_usecount > 0, vp, \
115 ("%s: wrong ref counts", __func__));
116 #else
117 #define VNCHECKREF(vp)
118 #endif
120 #if __FreeBSD_version >= 1400045
121 typedef uint64_t cookie_t;
122 #else
123 typedef ulong_t cookie_t;
124 #endif
127 * Programming rules.
129 * Each vnode op performs some logical unit of work. To do this, the ZPL must
130 * properly lock its in-core state, create a DMU transaction, do the work,
131 * record this work in the intent log (ZIL), commit the DMU transaction,
132 * and wait for the intent log to commit if it is a synchronous operation.
133 * Moreover, the vnode ops must work in both normal and log replay context.
134 * The ordering of events is important to avoid deadlocks and references
135 * to freed memory. The example below illustrates the following Big Rules:
137 * (1) A check must be made in each zfs thread for a mounted file system.
138 * This is done avoiding races using ZFS_ENTER(zfsvfs).
139 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
140 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
141 * can return EIO from the calling function.
143 * (2) VN_RELE() should always be the last thing except for zil_commit()
144 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
145 * First, if it's the last reference, the vnode/znode
146 * can be freed, so the zp may point to freed memory. Second, the last
147 * reference will call zfs_zinactive(), which may induce a lot of work --
148 * pushing cached pages (which acquires range locks) and syncing out
149 * cached atime changes. Third, zfs_zinactive() may require a new tx,
150 * which could deadlock the system if you were already holding one.
151 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
153 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
154 * as they can span dmu_tx_assign() calls.
156 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
157 * dmu_tx_assign(). This is critical because we don't want to block
158 * while holding locks.
160 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
161 * reduces lock contention and CPU usage when we must wait (note that if
162 * throughput is constrained by the storage, nearly every transaction
163 * must wait).
165 * Note, in particular, that if a lock is sometimes acquired before
166 * the tx assigns, and sometimes after (e.g. z_lock), then failing
167 * to use a non-blocking assign can deadlock the system. The scenario:
169 * Thread A has grabbed a lock before calling dmu_tx_assign().
170 * Thread B is in an already-assigned tx, and blocks for this lock.
171 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
172 * forever, because the previous txg can't quiesce until B's tx commits.
174 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
175 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
176 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
177 * to indicate that this operation has already called dmu_tx_wait().
178 * This will ensure that we don't retry forever, waiting a short bit
179 * each time.
181 * (5) If the operation succeeded, generate the intent log entry for it
182 * before dropping locks. This ensures that the ordering of events
183 * in the intent log matches the order in which they actually occurred.
184 * During ZIL replay the zfs_log_* functions will update the sequence
185 * number to indicate the zil transaction has replayed.
187 * (6) At the end of each vnode op, the DMU tx must always commit,
188 * regardless of whether there were any errors.
190 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
191 * to ensure that synchronous semantics are provided when necessary.
193 * In general, this is how things should be ordered in each vnode op:
195 * ZFS_ENTER(zfsvfs); // exit if unmounted
196 * top:
197 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD())
198 * rw_enter(...); // grab any other locks you need
199 * tx = dmu_tx_create(...); // get DMU tx
200 * dmu_tx_hold_*(); // hold each object you might modify
201 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
202 * if (error) {
203 * rw_exit(...); // drop locks
204 * zfs_dirent_unlock(dl); // unlock directory entry
205 * VN_RELE(...); // release held vnodes
206 * if (error == ERESTART) {
207 * waited = B_TRUE;
208 * dmu_tx_wait(tx);
209 * dmu_tx_abort(tx);
210 * goto top;
212 * dmu_tx_abort(tx); // abort DMU tx
213 * ZFS_EXIT(zfsvfs); // finished in zfs
214 * return (error); // really out of space
216 * error = do_real_work(); // do whatever this VOP does
217 * if (error == 0)
218 * zfs_log_*(...); // on success, make ZIL entry
219 * dmu_tx_commit(tx); // commit DMU tx -- error or not
220 * rw_exit(...); // drop locks
221 * zfs_dirent_unlock(dl); // unlock directory entry
222 * VN_RELE(...); // release held vnodes
223 * zil_commit(zilog, foid); // synchronous when necessary
224 * ZFS_EXIT(zfsvfs); // finished in zfs
225 * return (error); // done, report error
227 static int
228 zfs_open(vnode_t **vpp, int flag, cred_t *cr)
230 (void) cr;
231 znode_t *zp = VTOZ(*vpp);
232 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
234 ZFS_ENTER(zfsvfs);
235 ZFS_VERIFY_ZP(zp);
237 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
238 ((flag & FAPPEND) == 0)) {
239 ZFS_EXIT(zfsvfs);
240 return (SET_ERROR(EPERM));
243 /* Keep a count of the synchronous opens in the znode */
244 if (flag & (FSYNC | FDSYNC))
245 atomic_inc_32(&zp->z_sync_cnt);
247 ZFS_EXIT(zfsvfs);
248 return (0);
251 static int
252 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
254 (void) offset, (void) cr;
255 znode_t *zp = VTOZ(vp);
256 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
258 ZFS_ENTER(zfsvfs);
259 ZFS_VERIFY_ZP(zp);
261 /* Decrement the synchronous opens in the znode */
262 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
263 atomic_dec_32(&zp->z_sync_cnt);
265 ZFS_EXIT(zfsvfs);
266 return (0);
269 static int
270 zfs_ioctl(vnode_t *vp, ulong_t com, intptr_t data, int flag, cred_t *cred,
271 int *rvalp)
273 (void) flag, (void) cred, (void) rvalp;
274 loff_t off;
275 int error;
277 switch (com) {
278 case _FIOFFS:
280 return (0);
283 * The following two ioctls are used by bfu. Faking out,
284 * necessary to avoid bfu errors.
287 case _FIOGDIO:
288 case _FIOSDIO:
290 return (0);
293 case F_SEEK_DATA:
294 case F_SEEK_HOLE:
296 off = *(offset_t *)data;
297 /* offset parameter is in/out */
298 error = zfs_holey(VTOZ(vp), com, &off);
299 if (error)
300 return (error);
301 *(offset_t *)data = off;
302 return (0);
305 return (SET_ERROR(ENOTTY));
308 static vm_page_t
309 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
311 vm_object_t obj;
312 vm_page_t pp;
313 int64_t end;
316 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
317 * aligned boundaries, if the range is not aligned. As a result a
318 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
319 * It may happen that all DEV_BSIZE subranges are marked clean and thus
320 * the whole page would be considered clean despite have some
321 * dirty data.
322 * For this reason we should shrink the range to DEV_BSIZE aligned
323 * boundaries before calling vm_page_clear_dirty.
325 end = rounddown2(off + nbytes, DEV_BSIZE);
326 off = roundup2(off, DEV_BSIZE);
327 nbytes = end - off;
329 obj = vp->v_object;
330 zfs_vmobject_assert_wlocked_12(obj);
331 #if __FreeBSD_version < 1300050
332 for (;;) {
333 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
334 pp->valid) {
335 if (vm_page_xbusied(pp)) {
337 * Reference the page before unlocking and
338 * sleeping so that the page daemon is less
339 * likely to reclaim it.
341 vm_page_reference(pp);
342 vm_page_lock(pp);
343 zfs_vmobject_wunlock(obj);
344 vm_page_busy_sleep(pp, "zfsmwb", true);
345 zfs_vmobject_wlock(obj);
346 continue;
348 vm_page_sbusy(pp);
349 } else if (pp != NULL) {
350 ASSERT(!pp->valid);
351 pp = NULL;
353 if (pp != NULL) {
354 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
355 vm_object_pip_add(obj, 1);
356 pmap_remove_write(pp);
357 if (nbytes != 0)
358 vm_page_clear_dirty(pp, off, nbytes);
360 break;
362 #else
363 vm_page_grab_valid_unlocked(&pp, obj, OFF_TO_IDX(start),
364 VM_ALLOC_NOCREAT | VM_ALLOC_SBUSY | VM_ALLOC_NORMAL |
365 VM_ALLOC_IGN_SBUSY);
366 if (pp != NULL) {
367 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
368 vm_object_pip_add(obj, 1);
369 pmap_remove_write(pp);
370 if (nbytes != 0)
371 vm_page_clear_dirty(pp, off, nbytes);
373 #endif
374 return (pp);
377 static void
378 page_unbusy(vm_page_t pp)
381 vm_page_sunbusy(pp);
382 #if __FreeBSD_version >= 1300041
383 vm_object_pip_wakeup(pp->object);
384 #else
385 vm_object_pip_subtract(pp->object, 1);
386 #endif
389 #if __FreeBSD_version > 1300051
390 static vm_page_t
391 page_hold(vnode_t *vp, int64_t start)
393 vm_object_t obj;
394 vm_page_t m;
396 obj = vp->v_object;
397 vm_page_grab_valid_unlocked(&m, obj, OFF_TO_IDX(start),
398 VM_ALLOC_NOCREAT | VM_ALLOC_WIRED | VM_ALLOC_IGN_SBUSY |
399 VM_ALLOC_NOBUSY);
400 return (m);
402 #else
403 static vm_page_t
404 page_hold(vnode_t *vp, int64_t start)
406 vm_object_t obj;
407 vm_page_t pp;
409 obj = vp->v_object;
410 zfs_vmobject_assert_wlocked(obj);
412 for (;;) {
413 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
414 pp->valid) {
415 if (vm_page_xbusied(pp)) {
417 * Reference the page before unlocking and
418 * sleeping so that the page daemon is less
419 * likely to reclaim it.
421 vm_page_reference(pp);
422 vm_page_lock(pp);
423 zfs_vmobject_wunlock(obj);
424 vm_page_busy_sleep(pp, "zfsmwb", true);
425 zfs_vmobject_wlock(obj);
426 continue;
429 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
430 vm_page_wire_lock(pp);
431 vm_page_hold(pp);
432 vm_page_wire_unlock(pp);
434 } else
435 pp = NULL;
436 break;
438 return (pp);
440 #endif
442 static void
443 page_unhold(vm_page_t pp)
446 vm_page_wire_lock(pp);
447 #if __FreeBSD_version >= 1300035
448 vm_page_unwire(pp, PQ_ACTIVE);
449 #else
450 vm_page_unhold(pp);
451 #endif
452 vm_page_wire_unlock(pp);
456 * When a file is memory mapped, we must keep the IO data synchronized
457 * between the DMU cache and the memory mapped pages. What this means:
459 * On Write: If we find a memory mapped page, we write to *both*
460 * the page and the dmu buffer.
462 void
463 update_pages(znode_t *zp, int64_t start, int len, objset_t *os)
465 vm_object_t obj;
466 struct sf_buf *sf;
467 vnode_t *vp = ZTOV(zp);
468 caddr_t va;
469 int off;
471 ASSERT3P(vp->v_mount, !=, NULL);
472 obj = vp->v_object;
473 ASSERT3P(obj, !=, NULL);
475 off = start & PAGEOFFSET;
476 zfs_vmobject_wlock_12(obj);
477 #if __FreeBSD_version >= 1300041
478 vm_object_pip_add(obj, 1);
479 #endif
480 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
481 vm_page_t pp;
482 int nbytes = imin(PAGESIZE - off, len);
484 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
485 zfs_vmobject_wunlock_12(obj);
487 va = zfs_map_page(pp, &sf);
488 (void) dmu_read(os, zp->z_id, start + off, nbytes,
489 va + off, DMU_READ_PREFETCH);
490 zfs_unmap_page(sf);
492 zfs_vmobject_wlock_12(obj);
493 page_unbusy(pp);
495 len -= nbytes;
496 off = 0;
498 #if __FreeBSD_version >= 1300041
499 vm_object_pip_wakeup(obj);
500 #else
501 vm_object_pip_wakeupn(obj, 0);
502 #endif
503 zfs_vmobject_wunlock_12(obj);
507 * Read with UIO_NOCOPY flag means that sendfile(2) requests
508 * ZFS to populate a range of page cache pages with data.
510 * NOTE: this function could be optimized to pre-allocate
511 * all pages in advance, drain exclusive busy on all of them,
512 * map them into contiguous KVA region and populate them
513 * in one single dmu_read() call.
516 mappedread_sf(znode_t *zp, int nbytes, zfs_uio_t *uio)
518 vnode_t *vp = ZTOV(zp);
519 objset_t *os = zp->z_zfsvfs->z_os;
520 struct sf_buf *sf;
521 vm_object_t obj;
522 vm_page_t pp;
523 int64_t start;
524 caddr_t va;
525 int len = nbytes;
526 int error = 0;
528 ASSERT3U(zfs_uio_segflg(uio), ==, UIO_NOCOPY);
529 ASSERT3P(vp->v_mount, !=, NULL);
530 obj = vp->v_object;
531 ASSERT3P(obj, !=, NULL);
532 ASSERT0(zfs_uio_offset(uio) & PAGEOFFSET);
534 zfs_vmobject_wlock_12(obj);
535 for (start = zfs_uio_offset(uio); len > 0; start += PAGESIZE) {
536 int bytes = MIN(PAGESIZE, len);
538 pp = vm_page_grab_unlocked(obj, OFF_TO_IDX(start),
539 VM_ALLOC_SBUSY | VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
540 if (vm_page_none_valid(pp)) {
541 zfs_vmobject_wunlock_12(obj);
542 va = zfs_map_page(pp, &sf);
543 error = dmu_read(os, zp->z_id, start, bytes, va,
544 DMU_READ_PREFETCH);
545 if (bytes != PAGESIZE && error == 0)
546 memset(va + bytes, 0, PAGESIZE - bytes);
547 zfs_unmap_page(sf);
548 zfs_vmobject_wlock_12(obj);
549 #if __FreeBSD_version >= 1300081
550 if (error == 0) {
551 vm_page_valid(pp);
552 vm_page_activate(pp);
553 vm_page_do_sunbusy(pp);
554 } else {
555 zfs_vmobject_wlock(obj);
556 if (!vm_page_wired(pp) && pp->valid == 0 &&
557 vm_page_busy_tryupgrade(pp))
558 vm_page_free(pp);
559 else
560 vm_page_sunbusy(pp);
561 zfs_vmobject_wunlock(obj);
563 #else
564 vm_page_do_sunbusy(pp);
565 vm_page_lock(pp);
566 if (error) {
567 if (pp->wire_count == 0 && pp->valid == 0 &&
568 !vm_page_busied(pp))
569 vm_page_free(pp);
570 } else {
571 pp->valid = VM_PAGE_BITS_ALL;
572 vm_page_activate(pp);
574 vm_page_unlock(pp);
575 #endif
576 } else {
577 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
578 vm_page_do_sunbusy(pp);
580 if (error)
581 break;
582 zfs_uio_advance(uio, bytes);
583 len -= bytes;
585 zfs_vmobject_wunlock_12(obj);
586 return (error);
590 * When a file is memory mapped, we must keep the IO data synchronized
591 * between the DMU cache and the memory mapped pages. What this means:
593 * On Read: We "read" preferentially from memory mapped pages,
594 * else we default from the dmu buffer.
596 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
597 * the file is memory mapped.
600 mappedread(znode_t *zp, int nbytes, zfs_uio_t *uio)
602 vnode_t *vp = ZTOV(zp);
603 vm_object_t obj;
604 int64_t start;
605 int len = nbytes;
606 int off;
607 int error = 0;
609 ASSERT3P(vp->v_mount, !=, NULL);
610 obj = vp->v_object;
611 ASSERT3P(obj, !=, NULL);
613 start = zfs_uio_offset(uio);
614 off = start & PAGEOFFSET;
615 zfs_vmobject_wlock_12(obj);
616 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
617 vm_page_t pp;
618 uint64_t bytes = MIN(PAGESIZE - off, len);
620 if ((pp = page_hold(vp, start))) {
621 struct sf_buf *sf;
622 caddr_t va;
624 zfs_vmobject_wunlock_12(obj);
625 va = zfs_map_page(pp, &sf);
626 error = vn_io_fault_uiomove(va + off, bytes,
627 GET_UIO_STRUCT(uio));
628 zfs_unmap_page(sf);
629 zfs_vmobject_wlock_12(obj);
630 page_unhold(pp);
631 } else {
632 zfs_vmobject_wunlock_12(obj);
633 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
634 uio, bytes);
635 zfs_vmobject_wlock_12(obj);
637 len -= bytes;
638 off = 0;
639 if (error)
640 break;
642 zfs_vmobject_wunlock_12(obj);
643 return (error);
647 zfs_write_simple(znode_t *zp, const void *data, size_t len,
648 loff_t pos, size_t *presid)
650 int error = 0;
651 ssize_t resid;
653 error = vn_rdwr(UIO_WRITE, ZTOV(zp), __DECONST(void *, data), len, pos,
654 UIO_SYSSPACE, IO_SYNC, kcred, NOCRED, &resid, curthread);
656 if (error) {
657 return (SET_ERROR(error));
658 } else if (presid == NULL) {
659 if (resid != 0) {
660 error = SET_ERROR(EIO);
662 } else {
663 *presid = resid;
665 return (error);
668 void
669 zfs_zrele_async(znode_t *zp)
671 vnode_t *vp = ZTOV(zp);
672 objset_t *os = ITOZSB(vp)->z_os;
674 VN_RELE_ASYNC(vp, dsl_pool_zrele_taskq(dmu_objset_pool(os)));
677 static int
678 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
680 int error;
682 *vpp = arg;
683 error = vn_lock(*vpp, lkflags);
684 if (error != 0)
685 vrele(*vpp);
686 return (error);
689 static int
690 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags)
692 znode_t *zdp = VTOZ(dvp);
693 zfsvfs_t *zfsvfs __unused = zdp->z_zfsvfs;
694 int error;
695 int ltype;
697 if (zfsvfs->z_replay == B_FALSE)
698 ASSERT_VOP_LOCKED(dvp, __func__);
700 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
701 ASSERT3P(dvp, ==, vp);
702 vref(dvp);
703 ltype = lkflags & LK_TYPE_MASK;
704 if (ltype != VOP_ISLOCKED(dvp)) {
705 if (ltype == LK_EXCLUSIVE)
706 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
707 else /* if (ltype == LK_SHARED) */
708 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
711 * Relock for the "." case could leave us with
712 * reclaimed vnode.
714 if (VN_IS_DOOMED(dvp)) {
715 vrele(dvp);
716 return (SET_ERROR(ENOENT));
719 return (0);
720 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
722 * Note that in this case, dvp is the child vnode, and we
723 * are looking up the parent vnode - exactly reverse from
724 * normal operation. Unlocking dvp requires some rather
725 * tricky unlock/relock dance to prevent mp from being freed;
726 * use vn_vget_ino_gen() which takes care of all that.
728 * XXX Note that there is a time window when both vnodes are
729 * unlocked. It is possible, although highly unlikely, that
730 * during that window the parent-child relationship between
731 * the vnodes may change, for example, get reversed.
732 * In that case we would have a wrong lock order for the vnodes.
733 * All other filesystems seem to ignore this problem, so we
734 * do the same here.
735 * A potential solution could be implemented as follows:
736 * - using LK_NOWAIT when locking the second vnode and retrying
737 * if necessary
738 * - checking that the parent-child relationship still holds
739 * after locking both vnodes and retrying if it doesn't
741 error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp);
742 return (error);
743 } else {
744 error = vn_lock(vp, lkflags);
745 if (error != 0)
746 vrele(vp);
747 return (error);
752 * Lookup an entry in a directory, or an extended attribute directory.
753 * If it exists, return a held vnode reference for it.
755 * IN: dvp - vnode of directory to search.
756 * nm - name of entry to lookup.
757 * pnp - full pathname to lookup [UNUSED].
758 * flags - LOOKUP_XATTR set if looking for an attribute.
759 * rdir - root directory vnode [UNUSED].
760 * cr - credentials of caller.
761 * ct - caller context
763 * OUT: vpp - vnode of located entry, NULL if not found.
765 * RETURN: 0 on success, error code on failure.
767 * Timestamps:
768 * NA
770 static int
771 zfs_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp,
772 struct componentname *cnp, int nameiop, cred_t *cr, int flags,
773 boolean_t cached)
775 znode_t *zdp = VTOZ(dvp);
776 znode_t *zp;
777 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
778 #if __FreeBSD_version > 1300124
779 seqc_t dvp_seqc;
780 #endif
781 int error = 0;
784 * Fast path lookup, however we must skip DNLC lookup
785 * for case folding or normalizing lookups because the
786 * DNLC code only stores the passed in name. This means
787 * creating 'a' and removing 'A' on a case insensitive
788 * file system would work, but DNLC still thinks 'a'
789 * exists and won't let you create it again on the next
790 * pass through fast path.
792 if (!(flags & LOOKUP_XATTR)) {
793 if (dvp->v_type != VDIR) {
794 return (SET_ERROR(ENOTDIR));
795 } else if (zdp->z_sa_hdl == NULL) {
796 return (SET_ERROR(EIO));
800 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp,
801 const char *, nm);
803 ZFS_ENTER(zfsvfs);
804 ZFS_VERIFY_ZP(zdp);
806 #if __FreeBSD_version > 1300124
807 dvp_seqc = vn_seqc_read_notmodify(dvp);
808 #endif
810 *vpp = NULL;
812 if (flags & LOOKUP_XATTR) {
814 * If the xattr property is off, refuse the lookup request.
816 if (!(zfsvfs->z_flags & ZSB_XATTR)) {
817 ZFS_EXIT(zfsvfs);
818 return (SET_ERROR(EOPNOTSUPP));
822 * We don't allow recursive attributes..
823 * Maybe someday we will.
825 if (zdp->z_pflags & ZFS_XATTR) {
826 ZFS_EXIT(zfsvfs);
827 return (SET_ERROR(EINVAL));
830 if ((error = zfs_get_xattrdir(VTOZ(dvp), &zp, cr, flags))) {
831 ZFS_EXIT(zfsvfs);
832 return (error);
834 *vpp = ZTOV(zp);
837 * Do we have permission to get into attribute directory?
839 error = zfs_zaccess(zp, ACE_EXECUTE, 0, B_FALSE, cr);
840 if (error) {
841 vrele(ZTOV(zp));
844 ZFS_EXIT(zfsvfs);
845 return (error);
849 * Check accessibility of directory if we're not coming in via
850 * VOP_CACHEDLOOKUP.
852 if (!cached) {
853 #ifdef NOEXECCHECK
854 if ((cnp->cn_flags & NOEXECCHECK) != 0) {
855 cnp->cn_flags &= ~NOEXECCHECK;
856 } else
857 #endif
858 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
859 ZFS_EXIT(zfsvfs);
860 return (error);
864 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
865 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
866 ZFS_EXIT(zfsvfs);
867 return (SET_ERROR(EILSEQ));
872 * First handle the special cases.
874 if ((cnp->cn_flags & ISDOTDOT) != 0) {
876 * If we are a snapshot mounted under .zfs, return
877 * the vp for the snapshot directory.
879 if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) {
880 struct componentname cn;
881 vnode_t *zfsctl_vp;
882 int ltype;
884 ZFS_EXIT(zfsvfs);
885 ltype = VOP_ISLOCKED(dvp);
886 VOP_UNLOCK1(dvp);
887 error = zfsctl_root(zfsvfs->z_parent, LK_SHARED,
888 &zfsctl_vp);
889 if (error == 0) {
890 cn.cn_nameptr = "snapshot";
891 cn.cn_namelen = strlen(cn.cn_nameptr);
892 cn.cn_nameiop = cnp->cn_nameiop;
893 cn.cn_flags = cnp->cn_flags & ~ISDOTDOT;
894 cn.cn_lkflags = cnp->cn_lkflags;
895 error = VOP_LOOKUP(zfsctl_vp, vpp, &cn);
896 vput(zfsctl_vp);
898 vn_lock(dvp, ltype | LK_RETRY);
899 return (error);
902 if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) {
903 ZFS_EXIT(zfsvfs);
904 if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP)
905 return (SET_ERROR(ENOTSUP));
906 error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp);
907 return (error);
911 * The loop is retry the lookup if the parent-child relationship
912 * changes during the dot-dot locking complexities.
914 for (;;) {
915 uint64_t parent;
917 error = zfs_dirlook(zdp, nm, &zp);
918 if (error == 0)
919 *vpp = ZTOV(zp);
921 ZFS_EXIT(zfsvfs);
922 if (error != 0)
923 break;
925 error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags);
926 if (error != 0) {
928 * If we've got a locking error, then the vnode
929 * got reclaimed because of a force unmount.
930 * We never enter doomed vnodes into the name cache.
932 *vpp = NULL;
933 return (error);
936 if ((cnp->cn_flags & ISDOTDOT) == 0)
937 break;
939 ZFS_ENTER(zfsvfs);
940 if (zdp->z_sa_hdl == NULL) {
941 error = SET_ERROR(EIO);
942 } else {
943 error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
944 &parent, sizeof (parent));
946 if (error != 0) {
947 ZFS_EXIT(zfsvfs);
948 vput(ZTOV(zp));
949 break;
951 if (zp->z_id == parent) {
952 ZFS_EXIT(zfsvfs);
953 break;
955 vput(ZTOV(zp));
958 if (error != 0)
959 *vpp = NULL;
961 /* Translate errors and add SAVENAME when needed. */
962 if (cnp->cn_flags & ISLASTCN) {
963 switch (nameiop) {
964 case CREATE:
965 case RENAME:
966 if (error == ENOENT) {
967 error = EJUSTRETURN;
968 cnp->cn_flags |= SAVENAME;
969 break;
971 zfs_fallthrough;
972 case DELETE:
973 if (error == 0)
974 cnp->cn_flags |= SAVENAME;
975 break;
979 #if __FreeBSD_version > 1300124
980 if ((cnp->cn_flags & ISDOTDOT) != 0) {
982 * FIXME: zfs_lookup_lock relocks vnodes and does nothing to
983 * handle races. In particular different callers may end up
984 * with different vnodes and will try to add conflicting
985 * entries to the namecache.
987 * While finding different result may be acceptable in face
988 * of concurrent modification, adding conflicting entries
989 * trips over an assert in the namecache.
991 * Ultimately let an entry through once everything settles.
993 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
994 cnp->cn_flags &= ~MAKEENTRY;
997 #endif
999 /* Insert name into cache (as non-existent) if appropriate. */
1000 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1001 error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1002 cache_enter(dvp, NULL, cnp);
1004 /* Insert name into cache if appropriate. */
1005 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1006 error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1007 if (!(cnp->cn_flags & ISLASTCN) ||
1008 (nameiop != DELETE && nameiop != RENAME)) {
1009 cache_enter(dvp, *vpp, cnp);
1013 return (error);
1017 * Attempt to create a new entry in a directory. If the entry
1018 * already exists, truncate the file if permissible, else return
1019 * an error. Return the vp of the created or trunc'd file.
1021 * IN: dvp - vnode of directory to put new file entry in.
1022 * name - name of new file entry.
1023 * vap - attributes of new file.
1024 * excl - flag indicating exclusive or non-exclusive mode.
1025 * mode - mode to open file with.
1026 * cr - credentials of caller.
1027 * flag - large file flag [UNUSED].
1028 * ct - caller context
1029 * vsecp - ACL to be set
1031 * OUT: vpp - vnode of created or trunc'd entry.
1033 * RETURN: 0 on success, error code on failure.
1035 * Timestamps:
1036 * dvp - ctime|mtime updated if new entry created
1037 * vp - ctime|mtime always, atime if new
1040 zfs_create(znode_t *dzp, const char *name, vattr_t *vap, int excl, int mode,
1041 znode_t **zpp, cred_t *cr, int flag, vsecattr_t *vsecp)
1043 (void) excl, (void) mode, (void) flag;
1044 znode_t *zp;
1045 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1046 zilog_t *zilog;
1047 objset_t *os;
1048 dmu_tx_t *tx;
1049 int error;
1050 uid_t uid = crgetuid(cr);
1051 gid_t gid = crgetgid(cr);
1052 uint64_t projid = ZFS_DEFAULT_PROJID;
1053 zfs_acl_ids_t acl_ids;
1054 boolean_t fuid_dirtied;
1055 uint64_t txtype;
1056 #ifdef DEBUG_VFS_LOCKS
1057 vnode_t *dvp = ZTOV(dzp);
1058 #endif
1061 * If we have an ephemeral id, ACL, or XVATTR then
1062 * make sure file system is at proper version
1064 if (zfsvfs->z_use_fuids == B_FALSE &&
1065 (vsecp || (vap->va_mask & AT_XVATTR) ||
1066 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1067 return (SET_ERROR(EINVAL));
1069 ZFS_ENTER(zfsvfs);
1070 ZFS_VERIFY_ZP(dzp);
1071 os = zfsvfs->z_os;
1072 zilog = zfsvfs->z_log;
1074 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1075 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1076 ZFS_EXIT(zfsvfs);
1077 return (SET_ERROR(EILSEQ));
1080 if (vap->va_mask & AT_XVATTR) {
1081 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
1082 crgetuid(cr), cr, vap->va_type)) != 0) {
1083 ZFS_EXIT(zfsvfs);
1084 return (error);
1088 *zpp = NULL;
1090 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1091 vap->va_mode &= ~S_ISVTX;
1093 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
1094 if (error) {
1095 ZFS_EXIT(zfsvfs);
1096 return (error);
1098 ASSERT3P(zp, ==, NULL);
1101 * Create a new file object and update the directory
1102 * to reference it.
1104 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1105 goto out;
1109 * We only support the creation of regular files in
1110 * extended attribute directories.
1113 if ((dzp->z_pflags & ZFS_XATTR) &&
1114 (vap->va_type != VREG)) {
1115 error = SET_ERROR(EINVAL);
1116 goto out;
1119 if ((error = zfs_acl_ids_create(dzp, 0, vap,
1120 cr, vsecp, &acl_ids)) != 0)
1121 goto out;
1123 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode))
1124 projid = zfs_inherit_projid(dzp);
1125 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) {
1126 zfs_acl_ids_free(&acl_ids);
1127 error = SET_ERROR(EDQUOT);
1128 goto out;
1131 getnewvnode_reserve_();
1133 tx = dmu_tx_create(os);
1135 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1136 ZFS_SA_BASE_ATTR_SIZE);
1138 fuid_dirtied = zfsvfs->z_fuid_dirty;
1139 if (fuid_dirtied)
1140 zfs_fuid_txhold(zfsvfs, tx);
1141 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1142 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1143 if (!zfsvfs->z_use_sa &&
1144 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1145 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1146 0, acl_ids.z_aclp->z_acl_bytes);
1148 error = dmu_tx_assign(tx, TXG_WAIT);
1149 if (error) {
1150 zfs_acl_ids_free(&acl_ids);
1151 dmu_tx_abort(tx);
1152 getnewvnode_drop_reserve();
1153 ZFS_EXIT(zfsvfs);
1154 return (error);
1156 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1157 if (fuid_dirtied)
1158 zfs_fuid_sync(zfsvfs, tx);
1160 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
1161 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1162 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1163 vsecp, acl_ids.z_fuidp, vap);
1164 zfs_acl_ids_free(&acl_ids);
1165 dmu_tx_commit(tx);
1167 getnewvnode_drop_reserve();
1169 out:
1170 VNCHECKREF(dvp);
1171 if (error == 0) {
1172 *zpp = zp;
1175 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1176 zil_commit(zilog, 0);
1178 ZFS_EXIT(zfsvfs);
1179 return (error);
1183 * Remove an entry from a directory.
1185 * IN: dvp - vnode of directory to remove entry from.
1186 * name - name of entry to remove.
1187 * cr - credentials of caller.
1188 * ct - caller context
1189 * flags - case flags
1191 * RETURN: 0 on success, error code on failure.
1193 * Timestamps:
1194 * dvp - ctime|mtime
1195 * vp - ctime (if nlink > 0)
1197 static int
1198 zfs_remove_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
1200 znode_t *dzp = VTOZ(dvp);
1201 znode_t *zp;
1202 znode_t *xzp;
1203 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1204 zilog_t *zilog;
1205 uint64_t xattr_obj;
1206 uint64_t obj = 0;
1207 dmu_tx_t *tx;
1208 boolean_t unlinked;
1209 uint64_t txtype;
1210 int error;
1213 ZFS_ENTER(zfsvfs);
1214 ZFS_VERIFY_ZP(dzp);
1215 zp = VTOZ(vp);
1216 ZFS_VERIFY_ZP(zp);
1217 zilog = zfsvfs->z_log;
1219 xattr_obj = 0;
1220 xzp = NULL;
1222 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1223 goto out;
1227 * Need to use rmdir for removing directories.
1229 if (vp->v_type == VDIR) {
1230 error = SET_ERROR(EPERM);
1231 goto out;
1234 vnevent_remove(vp, dvp, name, ct);
1236 obj = zp->z_id;
1238 /* are there any extended attributes? */
1239 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1240 &xattr_obj, sizeof (xattr_obj));
1241 if (error == 0 && xattr_obj) {
1242 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1243 ASSERT0(error);
1247 * We may delete the znode now, or we may put it in the unlinked set;
1248 * it depends on whether we're the last link, and on whether there are
1249 * other holds on the vnode. So we dmu_tx_hold() the right things to
1250 * allow for either case.
1252 tx = dmu_tx_create(zfsvfs->z_os);
1253 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1254 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1255 zfs_sa_upgrade_txholds(tx, zp);
1256 zfs_sa_upgrade_txholds(tx, dzp);
1258 if (xzp) {
1259 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1260 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1263 /* charge as an update -- would be nice not to charge at all */
1264 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1267 * Mark this transaction as typically resulting in a net free of space
1269 dmu_tx_mark_netfree(tx);
1271 error = dmu_tx_assign(tx, TXG_WAIT);
1272 if (error) {
1273 dmu_tx_abort(tx);
1274 ZFS_EXIT(zfsvfs);
1275 return (error);
1279 * Remove the directory entry.
1281 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked);
1283 if (error) {
1284 dmu_tx_commit(tx);
1285 goto out;
1288 if (unlinked) {
1289 zfs_unlinked_add(zp, tx);
1290 vp->v_vflag |= VV_NOSYNC;
1292 /* XXX check changes to linux vnops */
1293 txtype = TX_REMOVE;
1294 zfs_log_remove(zilog, tx, txtype, dzp, name, obj, unlinked);
1296 dmu_tx_commit(tx);
1297 out:
1299 if (xzp)
1300 vrele(ZTOV(xzp));
1302 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1303 zil_commit(zilog, 0);
1306 ZFS_EXIT(zfsvfs);
1307 return (error);
1311 static int
1312 zfs_lookup_internal(znode_t *dzp, const char *name, vnode_t **vpp,
1313 struct componentname *cnp, int nameiop)
1315 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1316 int error;
1318 cnp->cn_nameptr = __DECONST(char *, name);
1319 cnp->cn_namelen = strlen(name);
1320 cnp->cn_nameiop = nameiop;
1321 cnp->cn_flags = ISLASTCN | SAVENAME;
1322 cnp->cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
1323 cnp->cn_cred = kcred;
1324 #if __FreeBSD_version < 1400037
1325 cnp->cn_thread = curthread;
1326 #endif
1328 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay) {
1329 struct vop_lookup_args a;
1331 a.a_gen.a_desc = &vop_lookup_desc;
1332 a.a_dvp = ZTOV(dzp);
1333 a.a_vpp = vpp;
1334 a.a_cnp = cnp;
1335 error = vfs_cache_lookup(&a);
1336 } else {
1337 error = zfs_lookup(ZTOV(dzp), name, vpp, cnp, nameiop, kcred, 0,
1338 B_FALSE);
1340 #ifdef ZFS_DEBUG
1341 if (error) {
1342 printf("got error %d on name %s on op %d\n", error, name,
1343 nameiop);
1344 kdb_backtrace();
1346 #endif
1347 return (error);
1351 zfs_remove(znode_t *dzp, const char *name, cred_t *cr, int flags)
1353 vnode_t *vp;
1354 int error;
1355 struct componentname cn;
1357 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
1358 return (error);
1360 error = zfs_remove_(ZTOV(dzp), vp, name, cr);
1361 vput(vp);
1362 return (error);
1365 * Create a new directory and insert it into dvp using the name
1366 * provided. Return a pointer to the inserted directory.
1368 * IN: dvp - vnode of directory to add subdir to.
1369 * dirname - name of new directory.
1370 * vap - attributes of new directory.
1371 * cr - credentials of caller.
1372 * ct - caller context
1373 * flags - case flags
1374 * vsecp - ACL to be set
1376 * OUT: vpp - vnode of created directory.
1378 * RETURN: 0 on success, error code on failure.
1380 * Timestamps:
1381 * dvp - ctime|mtime updated
1382 * vp - ctime|mtime|atime updated
1385 zfs_mkdir(znode_t *dzp, const char *dirname, vattr_t *vap, znode_t **zpp,
1386 cred_t *cr, int flags, vsecattr_t *vsecp)
1388 (void) flags, (void) vsecp;
1389 znode_t *zp;
1390 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1391 zilog_t *zilog;
1392 uint64_t txtype;
1393 dmu_tx_t *tx;
1394 int error;
1395 uid_t uid = crgetuid(cr);
1396 gid_t gid = crgetgid(cr);
1397 zfs_acl_ids_t acl_ids;
1398 boolean_t fuid_dirtied;
1400 ASSERT3U(vap->va_type, ==, VDIR);
1403 * If we have an ephemeral id, ACL, or XVATTR then
1404 * make sure file system is at proper version
1406 if (zfsvfs->z_use_fuids == B_FALSE &&
1407 ((vap->va_mask & AT_XVATTR) ||
1408 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1409 return (SET_ERROR(EINVAL));
1411 ZFS_ENTER(zfsvfs);
1412 ZFS_VERIFY_ZP(dzp);
1413 zilog = zfsvfs->z_log;
1415 if (dzp->z_pflags & ZFS_XATTR) {
1416 ZFS_EXIT(zfsvfs);
1417 return (SET_ERROR(EINVAL));
1420 if (zfsvfs->z_utf8 && u8_validate(dirname,
1421 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1422 ZFS_EXIT(zfsvfs);
1423 return (SET_ERROR(EILSEQ));
1426 if (vap->va_mask & AT_XVATTR) {
1427 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
1428 crgetuid(cr), cr, vap->va_type)) != 0) {
1429 ZFS_EXIT(zfsvfs);
1430 return (error);
1434 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1435 NULL, &acl_ids)) != 0) {
1436 ZFS_EXIT(zfsvfs);
1437 return (error);
1441 * First make sure the new directory doesn't exist.
1443 * Existence is checked first to make sure we don't return
1444 * EACCES instead of EEXIST which can cause some applications
1445 * to fail.
1447 *zpp = NULL;
1449 if ((error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW))) {
1450 zfs_acl_ids_free(&acl_ids);
1451 ZFS_EXIT(zfsvfs);
1452 return (error);
1454 ASSERT3P(zp, ==, NULL);
1456 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1457 zfs_acl_ids_free(&acl_ids);
1458 ZFS_EXIT(zfsvfs);
1459 return (error);
1462 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) {
1463 zfs_acl_ids_free(&acl_ids);
1464 ZFS_EXIT(zfsvfs);
1465 return (SET_ERROR(EDQUOT));
1469 * Add a new entry to the directory.
1471 getnewvnode_reserve_();
1472 tx = dmu_tx_create(zfsvfs->z_os);
1473 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1474 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1475 fuid_dirtied = zfsvfs->z_fuid_dirty;
1476 if (fuid_dirtied)
1477 zfs_fuid_txhold(zfsvfs, tx);
1478 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1479 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1480 acl_ids.z_aclp->z_acl_bytes);
1483 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1484 ZFS_SA_BASE_ATTR_SIZE);
1486 error = dmu_tx_assign(tx, TXG_WAIT);
1487 if (error) {
1488 zfs_acl_ids_free(&acl_ids);
1489 dmu_tx_abort(tx);
1490 getnewvnode_drop_reserve();
1491 ZFS_EXIT(zfsvfs);
1492 return (error);
1496 * Create new node.
1498 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1500 if (fuid_dirtied)
1501 zfs_fuid_sync(zfsvfs, tx);
1504 * Now put new name in parent dir.
1506 (void) zfs_link_create(dzp, dirname, zp, tx, ZNEW);
1508 *zpp = zp;
1510 txtype = zfs_log_create_txtype(Z_DIR, NULL, vap);
1511 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL,
1512 acl_ids.z_fuidp, vap);
1514 zfs_acl_ids_free(&acl_ids);
1516 dmu_tx_commit(tx);
1518 getnewvnode_drop_reserve();
1520 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1521 zil_commit(zilog, 0);
1523 ZFS_EXIT(zfsvfs);
1524 return (0);
1527 #if __FreeBSD_version < 1300124
1528 static void
1529 cache_vop_rmdir(struct vnode *dvp, struct vnode *vp)
1532 cache_purge(dvp);
1533 cache_purge(vp);
1535 #endif
1538 * Remove a directory subdir entry. If the current working
1539 * directory is the same as the subdir to be removed, the
1540 * remove will fail.
1542 * IN: dvp - vnode of directory to remove from.
1543 * name - name of directory to be removed.
1544 * cwd - vnode of current working directory.
1545 * cr - credentials of caller.
1546 * ct - caller context
1547 * flags - case flags
1549 * RETURN: 0 on success, error code on failure.
1551 * Timestamps:
1552 * dvp - ctime|mtime updated
1554 static int
1555 zfs_rmdir_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
1557 znode_t *dzp = VTOZ(dvp);
1558 znode_t *zp = VTOZ(vp);
1559 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1560 zilog_t *zilog;
1561 dmu_tx_t *tx;
1562 int error;
1564 ZFS_ENTER(zfsvfs);
1565 ZFS_VERIFY_ZP(dzp);
1566 ZFS_VERIFY_ZP(zp);
1567 zilog = zfsvfs->z_log;
1570 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1571 goto out;
1574 if (vp->v_type != VDIR) {
1575 error = SET_ERROR(ENOTDIR);
1576 goto out;
1579 vnevent_rmdir(vp, dvp, name, ct);
1581 tx = dmu_tx_create(zfsvfs->z_os);
1582 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1583 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1584 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1585 zfs_sa_upgrade_txholds(tx, zp);
1586 zfs_sa_upgrade_txholds(tx, dzp);
1587 dmu_tx_mark_netfree(tx);
1588 error = dmu_tx_assign(tx, TXG_WAIT);
1589 if (error) {
1590 dmu_tx_abort(tx);
1591 ZFS_EXIT(zfsvfs);
1592 return (error);
1595 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL);
1597 if (error == 0) {
1598 uint64_t txtype = TX_RMDIR;
1599 zfs_log_remove(zilog, tx, txtype, dzp, name,
1600 ZFS_NO_OBJECT, B_FALSE);
1603 dmu_tx_commit(tx);
1605 cache_vop_rmdir(dvp, vp);
1606 out:
1607 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1608 zil_commit(zilog, 0);
1610 ZFS_EXIT(zfsvfs);
1611 return (error);
1615 zfs_rmdir(znode_t *dzp, const char *name, znode_t *cwd, cred_t *cr, int flags)
1617 struct componentname cn;
1618 vnode_t *vp;
1619 int error;
1621 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
1622 return (error);
1624 error = zfs_rmdir_(ZTOV(dzp), vp, name, cr);
1625 vput(vp);
1626 return (error);
1630 * Read as many directory entries as will fit into the provided
1631 * buffer from the given directory cursor position (specified in
1632 * the uio structure).
1634 * IN: vp - vnode of directory to read.
1635 * uio - structure supplying read location, range info,
1636 * and return buffer.
1637 * cr - credentials of caller.
1638 * ct - caller context
1639 * flags - case flags
1641 * OUT: uio - updated offset and range, buffer filled.
1642 * eofp - set to true if end-of-file detected.
1644 * RETURN: 0 on success, error code on failure.
1646 * Timestamps:
1647 * vp - atime updated
1649 * Note that the low 4 bits of the cookie returned by zap is always zero.
1650 * This allows us to use the low range for "special" directory entries:
1651 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1652 * we use the offset 2 for the '.zfs' directory.
1654 static int
1655 zfs_readdir(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, int *eofp,
1656 int *ncookies, cookie_t **cookies)
1658 znode_t *zp = VTOZ(vp);
1659 iovec_t *iovp;
1660 edirent_t *eodp;
1661 dirent64_t *odp;
1662 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1663 objset_t *os;
1664 caddr_t outbuf;
1665 size_t bufsize;
1666 zap_cursor_t zc;
1667 zap_attribute_t zap;
1668 uint_t bytes_wanted;
1669 uint64_t offset; /* must be unsigned; checks for < 1 */
1670 uint64_t parent;
1671 int local_eof;
1672 int outcount;
1673 int error;
1674 uint8_t prefetch;
1675 boolean_t check_sysattrs;
1676 uint8_t type;
1677 int ncooks;
1678 cookie_t *cooks = NULL;
1679 int flags = 0;
1681 ZFS_ENTER(zfsvfs);
1682 ZFS_VERIFY_ZP(zp);
1684 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1685 &parent, sizeof (parent))) != 0) {
1686 ZFS_EXIT(zfsvfs);
1687 return (error);
1691 * If we are not given an eof variable,
1692 * use a local one.
1694 if (eofp == NULL)
1695 eofp = &local_eof;
1698 * Check for valid iov_len.
1700 if (GET_UIO_STRUCT(uio)->uio_iov->iov_len <= 0) {
1701 ZFS_EXIT(zfsvfs);
1702 return (SET_ERROR(EINVAL));
1706 * Quit if directory has been removed (posix)
1708 if ((*eofp = zp->z_unlinked) != 0) {
1709 ZFS_EXIT(zfsvfs);
1710 return (0);
1713 error = 0;
1714 os = zfsvfs->z_os;
1715 offset = zfs_uio_offset(uio);
1716 prefetch = zp->z_zn_prefetch;
1719 * Initialize the iterator cursor.
1721 if (offset <= 3) {
1723 * Start iteration from the beginning of the directory.
1725 zap_cursor_init(&zc, os, zp->z_id);
1726 } else {
1728 * The offset is a serialized cursor.
1730 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1734 * Get space to change directory entries into fs independent format.
1736 iovp = GET_UIO_STRUCT(uio)->uio_iov;
1737 bytes_wanted = iovp->iov_len;
1738 if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1) {
1739 bufsize = bytes_wanted;
1740 outbuf = kmem_alloc(bufsize, KM_SLEEP);
1741 odp = (struct dirent64 *)outbuf;
1742 } else {
1743 bufsize = bytes_wanted;
1744 outbuf = NULL;
1745 odp = (struct dirent64 *)iovp->iov_base;
1747 eodp = (struct edirent *)odp;
1749 if (ncookies != NULL) {
1751 * Minimum entry size is dirent size and 1 byte for a file name.
1753 ncooks = zfs_uio_resid(uio) / (sizeof (struct dirent) -
1754 sizeof (((struct dirent *)NULL)->d_name) + 1);
1755 cooks = malloc(ncooks * sizeof (*cooks), M_TEMP, M_WAITOK);
1756 *cookies = cooks;
1757 *ncookies = ncooks;
1760 * If this VFS supports the system attribute view interface; and
1761 * we're looking at an extended attribute directory; and we care
1762 * about normalization conflicts on this vfs; then we must check
1763 * for normalization conflicts with the sysattr name space.
1765 #ifdef TODO
1766 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
1767 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
1768 (flags & V_RDDIR_ENTFLAGS);
1769 #else
1770 check_sysattrs = 0;
1771 #endif
1774 * Transform to file-system independent format
1776 outcount = 0;
1777 while (outcount < bytes_wanted) {
1778 ino64_t objnum;
1779 ushort_t reclen;
1780 off64_t *next = NULL;
1783 * Special case `.', `..', and `.zfs'.
1785 if (offset == 0) {
1786 (void) strcpy(zap.za_name, ".");
1787 zap.za_normalization_conflict = 0;
1788 objnum = zp->z_id;
1789 type = DT_DIR;
1790 } else if (offset == 1) {
1791 (void) strcpy(zap.za_name, "..");
1792 zap.za_normalization_conflict = 0;
1793 objnum = parent;
1794 type = DT_DIR;
1795 } else if (offset == 2 && zfs_show_ctldir(zp)) {
1796 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1797 zap.za_normalization_conflict = 0;
1798 objnum = ZFSCTL_INO_ROOT;
1799 type = DT_DIR;
1800 } else {
1802 * Grab next entry.
1804 if ((error = zap_cursor_retrieve(&zc, &zap))) {
1805 if ((*eofp = (error == ENOENT)) != 0)
1806 break;
1807 else
1808 goto update;
1811 if (zap.za_integer_length != 8 ||
1812 zap.za_num_integers != 1) {
1813 cmn_err(CE_WARN, "zap_readdir: bad directory "
1814 "entry, obj = %lld, offset = %lld\n",
1815 (u_longlong_t)zp->z_id,
1816 (u_longlong_t)offset);
1817 error = SET_ERROR(ENXIO);
1818 goto update;
1821 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
1823 * MacOS X can extract the object type here such as:
1824 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1826 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1828 if (check_sysattrs && !zap.za_normalization_conflict) {
1829 #ifdef TODO
1830 zap.za_normalization_conflict =
1831 xattr_sysattr_casechk(zap.za_name);
1832 #else
1833 panic("%s:%u: TODO", __func__, __LINE__);
1834 #endif
1838 if (flags & V_RDDIR_ACCFILTER) {
1840 * If we have no access at all, don't include
1841 * this entry in the returned information
1843 znode_t *ezp;
1844 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
1845 goto skip_entry;
1846 if (!zfs_has_access(ezp, cr)) {
1847 vrele(ZTOV(ezp));
1848 goto skip_entry;
1850 vrele(ZTOV(ezp));
1853 if (flags & V_RDDIR_ENTFLAGS)
1854 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
1855 else
1856 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1859 * Will this entry fit in the buffer?
1861 if (outcount + reclen > bufsize) {
1863 * Did we manage to fit anything in the buffer?
1865 if (!outcount) {
1866 error = SET_ERROR(EINVAL);
1867 goto update;
1869 break;
1871 if (flags & V_RDDIR_ENTFLAGS) {
1873 * Add extended flag entry:
1875 eodp->ed_ino = objnum;
1876 eodp->ed_reclen = reclen;
1877 /* NOTE: ed_off is the offset for the *next* entry */
1878 next = &(eodp->ed_off);
1879 eodp->ed_eflags = zap.za_normalization_conflict ?
1880 ED_CASE_CONFLICT : 0;
1881 (void) strncpy(eodp->ed_name, zap.za_name,
1882 EDIRENT_NAMELEN(reclen));
1883 eodp = (edirent_t *)((intptr_t)eodp + reclen);
1884 } else {
1886 * Add normal entry:
1888 odp->d_ino = objnum;
1889 odp->d_reclen = reclen;
1890 odp->d_namlen = strlen(zap.za_name);
1891 /* NOTE: d_off is the offset for the *next* entry. */
1892 next = &odp->d_off;
1893 strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
1894 odp->d_type = type;
1895 dirent_terminate(odp);
1896 odp = (dirent64_t *)((intptr_t)odp + reclen);
1898 outcount += reclen;
1900 ASSERT3S(outcount, <=, bufsize);
1902 /* Prefetch znode */
1903 if (prefetch)
1904 dmu_prefetch(os, objnum, 0, 0, 0,
1905 ZIO_PRIORITY_SYNC_READ);
1907 skip_entry:
1909 * Move to the next entry, fill in the previous offset.
1911 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1912 zap_cursor_advance(&zc);
1913 offset = zap_cursor_serialize(&zc);
1914 } else {
1915 offset += 1;
1918 /* Fill the offset right after advancing the cursor. */
1919 if (next != NULL)
1920 *next = offset;
1921 if (cooks != NULL) {
1922 *cooks++ = offset;
1923 ncooks--;
1924 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
1927 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1929 /* Subtract unused cookies */
1930 if (ncookies != NULL)
1931 *ncookies -= ncooks;
1933 if (zfs_uio_segflg(uio) == UIO_SYSSPACE && zfs_uio_iovcnt(uio) == 1) {
1934 iovp->iov_base += outcount;
1935 iovp->iov_len -= outcount;
1936 zfs_uio_resid(uio) -= outcount;
1937 } else if ((error =
1938 zfs_uiomove(outbuf, (long)outcount, UIO_READ, uio))) {
1940 * Reset the pointer.
1942 offset = zfs_uio_offset(uio);
1945 update:
1946 zap_cursor_fini(&zc);
1947 if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1)
1948 kmem_free(outbuf, bufsize);
1950 if (error == ENOENT)
1951 error = 0;
1953 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1955 zfs_uio_setoffset(uio, offset);
1956 ZFS_EXIT(zfsvfs);
1957 if (error != 0 && cookies != NULL) {
1958 free(*cookies, M_TEMP);
1959 *cookies = NULL;
1960 *ncookies = 0;
1962 return (error);
1966 * Get the requested file attributes and place them in the provided
1967 * vattr structure.
1969 * IN: vp - vnode of file.
1970 * vap - va_mask identifies requested attributes.
1971 * If AT_XVATTR set, then optional attrs are requested
1972 * flags - ATTR_NOACLCHECK (CIFS server context)
1973 * cr - credentials of caller.
1975 * OUT: vap - attribute values.
1977 * RETURN: 0 (always succeeds).
1979 static int
1980 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1982 znode_t *zp = VTOZ(vp);
1983 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1984 int error = 0;
1985 uint32_t blksize;
1986 u_longlong_t nblocks;
1987 uint64_t mtime[2], ctime[2], crtime[2], rdev;
1988 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
1989 xoptattr_t *xoap = NULL;
1990 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
1991 sa_bulk_attr_t bulk[4];
1992 int count = 0;
1994 ZFS_ENTER(zfsvfs);
1995 ZFS_VERIFY_ZP(zp);
1997 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
1999 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2000 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2001 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2002 if (vp->v_type == VBLK || vp->v_type == VCHR)
2003 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2004 &rdev, 8);
2006 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2007 ZFS_EXIT(zfsvfs);
2008 return (error);
2012 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2013 * Also, if we are the owner don't bother, since owner should
2014 * always be allowed to read basic attributes of file.
2016 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2017 (vap->va_uid != crgetuid(cr))) {
2018 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2019 skipaclchk, cr))) {
2020 ZFS_EXIT(zfsvfs);
2021 return (error);
2026 * Return all attributes. It's cheaper to provide the answer
2027 * than to determine whether we were asked the question.
2030 vap->va_type = IFTOVT(zp->z_mode);
2031 vap->va_mode = zp->z_mode & ~S_IFMT;
2032 vn_fsid(vp, vap);
2033 vap->va_nodeid = zp->z_id;
2034 vap->va_nlink = zp->z_links;
2035 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp) &&
2036 zp->z_links < ZFS_LINK_MAX)
2037 vap->va_nlink++;
2038 vap->va_size = zp->z_size;
2039 if (vp->v_type == VBLK || vp->v_type == VCHR)
2040 vap->va_rdev = zfs_cmpldev(rdev);
2041 vap->va_gen = zp->z_gen;
2042 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2043 vap->va_filerev = zp->z_seq;
2046 * Add in any requested optional attributes and the create time.
2047 * Also set the corresponding bits in the returned attribute bitmap.
2049 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2050 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2051 xoap->xoa_archive =
2052 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2053 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2056 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2057 xoap->xoa_readonly =
2058 ((zp->z_pflags & ZFS_READONLY) != 0);
2059 XVA_SET_RTN(xvap, XAT_READONLY);
2062 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2063 xoap->xoa_system =
2064 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2065 XVA_SET_RTN(xvap, XAT_SYSTEM);
2068 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2069 xoap->xoa_hidden =
2070 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2071 XVA_SET_RTN(xvap, XAT_HIDDEN);
2074 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2075 xoap->xoa_nounlink =
2076 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2077 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2080 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2081 xoap->xoa_immutable =
2082 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2083 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2086 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2087 xoap->xoa_appendonly =
2088 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2089 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2092 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2093 xoap->xoa_nodump =
2094 ((zp->z_pflags & ZFS_NODUMP) != 0);
2095 XVA_SET_RTN(xvap, XAT_NODUMP);
2098 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2099 xoap->xoa_opaque =
2100 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2101 XVA_SET_RTN(xvap, XAT_OPAQUE);
2104 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2105 xoap->xoa_av_quarantined =
2106 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2107 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2110 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2111 xoap->xoa_av_modified =
2112 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2113 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2116 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2117 vp->v_type == VREG) {
2118 zfs_sa_get_scanstamp(zp, xvap);
2121 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2122 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2123 XVA_SET_RTN(xvap, XAT_REPARSE);
2125 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2126 xoap->xoa_generation = zp->z_gen;
2127 XVA_SET_RTN(xvap, XAT_GEN);
2130 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2131 xoap->xoa_offline =
2132 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2133 XVA_SET_RTN(xvap, XAT_OFFLINE);
2136 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2137 xoap->xoa_sparse =
2138 ((zp->z_pflags & ZFS_SPARSE) != 0);
2139 XVA_SET_RTN(xvap, XAT_SPARSE);
2142 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2143 xoap->xoa_projinherit =
2144 ((zp->z_pflags & ZFS_PROJINHERIT) != 0);
2145 XVA_SET_RTN(xvap, XAT_PROJINHERIT);
2148 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2149 xoap->xoa_projid = zp->z_projid;
2150 XVA_SET_RTN(xvap, XAT_PROJID);
2154 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2155 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2156 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2157 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2160 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2161 vap->va_blksize = blksize;
2162 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2164 if (zp->z_blksz == 0) {
2166 * Block size hasn't been set; suggest maximal I/O transfers.
2168 vap->va_blksize = zfsvfs->z_max_blksz;
2171 ZFS_EXIT(zfsvfs);
2172 return (0);
2176 * Set the file attributes to the values contained in the
2177 * vattr structure.
2179 * IN: zp - znode of file to be modified.
2180 * vap - new attribute values.
2181 * If AT_XVATTR set, then optional attrs are being set
2182 * flags - ATTR_UTIME set if non-default time values provided.
2183 * - ATTR_NOACLCHECK (CIFS context only).
2184 * cr - credentials of caller.
2185 * ct - caller context
2187 * RETURN: 0 on success, error code on failure.
2189 * Timestamps:
2190 * vp - ctime updated, mtime updated if size changed.
2193 zfs_setattr(znode_t *zp, vattr_t *vap, int flags, cred_t *cr)
2195 vnode_t *vp = ZTOV(zp);
2196 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2197 objset_t *os;
2198 zilog_t *zilog;
2199 dmu_tx_t *tx;
2200 vattr_t oldva;
2201 xvattr_t tmpxvattr;
2202 uint_t mask = vap->va_mask;
2203 uint_t saved_mask = 0;
2204 uint64_t saved_mode;
2205 int trim_mask = 0;
2206 uint64_t new_mode;
2207 uint64_t new_uid, new_gid;
2208 uint64_t xattr_obj;
2209 uint64_t mtime[2], ctime[2];
2210 uint64_t projid = ZFS_INVALID_PROJID;
2211 znode_t *attrzp;
2212 int need_policy = FALSE;
2213 int err, err2;
2214 zfs_fuid_info_t *fuidp = NULL;
2215 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2216 xoptattr_t *xoap;
2217 zfs_acl_t *aclp;
2218 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2219 boolean_t fuid_dirtied = B_FALSE;
2220 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2221 int count = 0, xattr_count = 0;
2223 if (mask == 0)
2224 return (0);
2226 if (mask & AT_NOSET)
2227 return (SET_ERROR(EINVAL));
2229 ZFS_ENTER(zfsvfs);
2230 ZFS_VERIFY_ZP(zp);
2232 os = zfsvfs->z_os;
2233 zilog = zfsvfs->z_log;
2236 * Make sure that if we have ephemeral uid/gid or xvattr specified
2237 * that file system is at proper version level
2240 if (zfsvfs->z_use_fuids == B_FALSE &&
2241 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2242 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2243 (mask & AT_XVATTR))) {
2244 ZFS_EXIT(zfsvfs);
2245 return (SET_ERROR(EINVAL));
2248 if (mask & AT_SIZE && vp->v_type == VDIR) {
2249 ZFS_EXIT(zfsvfs);
2250 return (SET_ERROR(EISDIR));
2253 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2254 ZFS_EXIT(zfsvfs);
2255 return (SET_ERROR(EINVAL));
2259 * If this is an xvattr_t, then get a pointer to the structure of
2260 * optional attributes. If this is NULL, then we have a vattr_t.
2262 xoap = xva_getxoptattr(xvap);
2264 xva_init(&tmpxvattr);
2267 * Immutable files can only alter immutable bit and atime
2269 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2270 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2271 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2272 ZFS_EXIT(zfsvfs);
2273 return (SET_ERROR(EPERM));
2277 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2281 * Verify timestamps doesn't overflow 32 bits.
2282 * ZFS can handle large timestamps, but 32bit syscalls can't
2283 * handle times greater than 2039. This check should be removed
2284 * once large timestamps are fully supported.
2286 if (mask & (AT_ATIME | AT_MTIME)) {
2287 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2288 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2289 ZFS_EXIT(zfsvfs);
2290 return (SET_ERROR(EOVERFLOW));
2293 if (xoap != NULL && (mask & AT_XVATTR)) {
2294 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME) &&
2295 TIMESPEC_OVERFLOW(&vap->va_birthtime)) {
2296 ZFS_EXIT(zfsvfs);
2297 return (SET_ERROR(EOVERFLOW));
2300 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2301 if (!dmu_objset_projectquota_enabled(os) ||
2302 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode))) {
2303 ZFS_EXIT(zfsvfs);
2304 return (SET_ERROR(EOPNOTSUPP));
2307 projid = xoap->xoa_projid;
2308 if (unlikely(projid == ZFS_INVALID_PROJID)) {
2309 ZFS_EXIT(zfsvfs);
2310 return (SET_ERROR(EINVAL));
2313 if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID)
2314 projid = ZFS_INVALID_PROJID;
2315 else
2316 need_policy = TRUE;
2319 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) &&
2320 (xoap->xoa_projinherit !=
2321 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) &&
2322 (!dmu_objset_projectquota_enabled(os) ||
2323 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode)))) {
2324 ZFS_EXIT(zfsvfs);
2325 return (SET_ERROR(EOPNOTSUPP));
2329 attrzp = NULL;
2330 aclp = NULL;
2332 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2333 ZFS_EXIT(zfsvfs);
2334 return (SET_ERROR(EROFS));
2338 * First validate permissions
2341 if (mask & AT_SIZE) {
2343 * XXX - Note, we are not providing any open
2344 * mode flags here (like FNDELAY), so we may
2345 * block if there are locks present... this
2346 * should be addressed in openat().
2348 /* XXX - would it be OK to generate a log record here? */
2349 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2350 if (err) {
2351 ZFS_EXIT(zfsvfs);
2352 return (err);
2356 if (mask & (AT_ATIME|AT_MTIME) ||
2357 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2358 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2359 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2360 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2361 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2362 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2363 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2364 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2365 skipaclchk, cr);
2368 if (mask & (AT_UID|AT_GID)) {
2369 int idmask = (mask & (AT_UID|AT_GID));
2370 int take_owner;
2371 int take_group;
2374 * NOTE: even if a new mode is being set,
2375 * we may clear S_ISUID/S_ISGID bits.
2378 if (!(mask & AT_MODE))
2379 vap->va_mode = zp->z_mode;
2382 * Take ownership or chgrp to group we are a member of
2385 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2386 take_group = (mask & AT_GID) &&
2387 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2390 * If both AT_UID and AT_GID are set then take_owner and
2391 * take_group must both be set in order to allow taking
2392 * ownership.
2394 * Otherwise, send the check through secpolicy_vnode_setattr()
2398 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2399 ((idmask == AT_UID) && take_owner) ||
2400 ((idmask == AT_GID) && take_group)) {
2401 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2402 skipaclchk, cr) == 0) {
2404 * Remove setuid/setgid for non-privileged users
2406 secpolicy_setid_clear(vap, vp, cr);
2407 trim_mask = (mask & (AT_UID|AT_GID));
2408 } else {
2409 need_policy = TRUE;
2411 } else {
2412 need_policy = TRUE;
2416 oldva.va_mode = zp->z_mode;
2417 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2418 if (mask & AT_XVATTR) {
2420 * Update xvattr mask to include only those attributes
2421 * that are actually changing.
2423 * the bits will be restored prior to actually setting
2424 * the attributes so the caller thinks they were set.
2426 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2427 if (xoap->xoa_appendonly !=
2428 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2429 need_policy = TRUE;
2430 } else {
2431 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2432 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2436 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2437 if (xoap->xoa_projinherit !=
2438 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) {
2439 need_policy = TRUE;
2440 } else {
2441 XVA_CLR_REQ(xvap, XAT_PROJINHERIT);
2442 XVA_SET_REQ(&tmpxvattr, XAT_PROJINHERIT);
2446 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2447 if (xoap->xoa_nounlink !=
2448 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2449 need_policy = TRUE;
2450 } else {
2451 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2452 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2456 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2457 if (xoap->xoa_immutable !=
2458 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2459 need_policy = TRUE;
2460 } else {
2461 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2462 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2466 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2467 if (xoap->xoa_nodump !=
2468 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2469 need_policy = TRUE;
2470 } else {
2471 XVA_CLR_REQ(xvap, XAT_NODUMP);
2472 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2476 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2477 if (xoap->xoa_av_modified !=
2478 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2479 need_policy = TRUE;
2480 } else {
2481 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2482 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2486 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2487 if ((vp->v_type != VREG &&
2488 xoap->xoa_av_quarantined) ||
2489 xoap->xoa_av_quarantined !=
2490 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2491 need_policy = TRUE;
2492 } else {
2493 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2494 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2498 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2499 ZFS_EXIT(zfsvfs);
2500 return (SET_ERROR(EPERM));
2503 if (need_policy == FALSE &&
2504 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2505 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2506 need_policy = TRUE;
2510 if (mask & AT_MODE) {
2511 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2512 err = secpolicy_setid_setsticky_clear(vp, vap,
2513 &oldva, cr);
2514 if (err) {
2515 ZFS_EXIT(zfsvfs);
2516 return (err);
2518 trim_mask |= AT_MODE;
2519 } else {
2520 need_policy = TRUE;
2524 if (need_policy) {
2526 * If trim_mask is set then take ownership
2527 * has been granted or write_acl is present and user
2528 * has the ability to modify mode. In that case remove
2529 * UID|GID and or MODE from mask so that
2530 * secpolicy_vnode_setattr() doesn't revoke it.
2533 if (trim_mask) {
2534 saved_mask = vap->va_mask;
2535 vap->va_mask &= ~trim_mask;
2536 if (trim_mask & AT_MODE) {
2538 * Save the mode, as secpolicy_vnode_setattr()
2539 * will overwrite it with ova.va_mode.
2541 saved_mode = vap->va_mode;
2544 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2545 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2546 if (err) {
2547 ZFS_EXIT(zfsvfs);
2548 return (err);
2551 if (trim_mask) {
2552 vap->va_mask |= saved_mask;
2553 if (trim_mask & AT_MODE) {
2555 * Recover the mode after
2556 * secpolicy_vnode_setattr().
2558 vap->va_mode = saved_mode;
2564 * secpolicy_vnode_setattr, or take ownership may have
2565 * changed va_mask
2567 mask = vap->va_mask;
2569 if ((mask & (AT_UID | AT_GID)) || projid != ZFS_INVALID_PROJID) {
2570 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2571 &xattr_obj, sizeof (xattr_obj));
2573 if (err == 0 && xattr_obj) {
2574 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
2575 if (err == 0) {
2576 err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE);
2577 if (err != 0)
2578 vrele(ZTOV(attrzp));
2580 if (err)
2581 goto out2;
2583 if (mask & AT_UID) {
2584 new_uid = zfs_fuid_create(zfsvfs,
2585 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2586 if (new_uid != zp->z_uid &&
2587 zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT,
2588 new_uid)) {
2589 if (attrzp)
2590 vput(ZTOV(attrzp));
2591 err = SET_ERROR(EDQUOT);
2592 goto out2;
2596 if (mask & AT_GID) {
2597 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
2598 cr, ZFS_GROUP, &fuidp);
2599 if (new_gid != zp->z_gid &&
2600 zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT,
2601 new_gid)) {
2602 if (attrzp)
2603 vput(ZTOV(attrzp));
2604 err = SET_ERROR(EDQUOT);
2605 goto out2;
2609 if (projid != ZFS_INVALID_PROJID &&
2610 zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) {
2611 if (attrzp)
2612 vput(ZTOV(attrzp));
2613 err = SET_ERROR(EDQUOT);
2614 goto out2;
2617 tx = dmu_tx_create(os);
2619 if (mask & AT_MODE) {
2620 uint64_t pmode = zp->z_mode;
2621 uint64_t acl_obj;
2622 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2624 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
2625 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
2626 err = SET_ERROR(EPERM);
2627 goto out;
2630 if ((err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)))
2631 goto out;
2633 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2635 * Are we upgrading ACL from old V0 format
2636 * to V1 format?
2638 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
2639 zfs_znode_acl_version(zp) ==
2640 ZFS_ACL_VERSION_INITIAL) {
2641 dmu_tx_hold_free(tx, acl_obj, 0,
2642 DMU_OBJECT_END);
2643 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2644 0, aclp->z_acl_bytes);
2645 } else {
2646 dmu_tx_hold_write(tx, acl_obj, 0,
2647 aclp->z_acl_bytes);
2649 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2650 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2651 0, aclp->z_acl_bytes);
2653 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2654 } else {
2655 if (((mask & AT_XVATTR) &&
2656 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
2657 (projid != ZFS_INVALID_PROJID &&
2658 !(zp->z_pflags & ZFS_PROJID)))
2659 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2660 else
2661 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2664 if (attrzp) {
2665 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2668 fuid_dirtied = zfsvfs->z_fuid_dirty;
2669 if (fuid_dirtied)
2670 zfs_fuid_txhold(zfsvfs, tx);
2672 zfs_sa_upgrade_txholds(tx, zp);
2674 err = dmu_tx_assign(tx, TXG_WAIT);
2675 if (err)
2676 goto out;
2678 count = 0;
2680 * Set each attribute requested.
2681 * We group settings according to the locks they need to acquire.
2683 * Note: you cannot set ctime directly, although it will be
2684 * updated as a side-effect of calling this function.
2687 if (projid != ZFS_INVALID_PROJID && !(zp->z_pflags & ZFS_PROJID)) {
2689 * For the existed object that is upgraded from old system,
2690 * its on-disk layout has no slot for the project ID attribute.
2691 * But quota accounting logic needs to access related slots by
2692 * offset directly. So we need to adjust old objects' layout
2693 * to make the project ID to some unified and fixed offset.
2695 if (attrzp)
2696 err = sa_add_projid(attrzp->z_sa_hdl, tx, projid);
2697 if (err == 0)
2698 err = sa_add_projid(zp->z_sa_hdl, tx, projid);
2700 if (unlikely(err == EEXIST))
2701 err = 0;
2702 else if (err != 0)
2703 goto out;
2704 else
2705 projid = ZFS_INVALID_PROJID;
2708 if (mask & (AT_UID|AT_GID|AT_MODE))
2709 mutex_enter(&zp->z_acl_lock);
2711 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
2712 &zp->z_pflags, sizeof (zp->z_pflags));
2714 if (attrzp) {
2715 if (mask & (AT_UID|AT_GID|AT_MODE))
2716 mutex_enter(&attrzp->z_acl_lock);
2717 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2718 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
2719 sizeof (attrzp->z_pflags));
2720 if (projid != ZFS_INVALID_PROJID) {
2721 attrzp->z_projid = projid;
2722 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2723 SA_ZPL_PROJID(zfsvfs), NULL, &attrzp->z_projid,
2724 sizeof (attrzp->z_projid));
2728 if (mask & (AT_UID|AT_GID)) {
2730 if (mask & AT_UID) {
2731 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
2732 &new_uid, sizeof (new_uid));
2733 zp->z_uid = new_uid;
2734 if (attrzp) {
2735 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2736 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
2737 sizeof (new_uid));
2738 attrzp->z_uid = new_uid;
2742 if (mask & AT_GID) {
2743 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
2744 NULL, &new_gid, sizeof (new_gid));
2745 zp->z_gid = new_gid;
2746 if (attrzp) {
2747 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2748 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
2749 sizeof (new_gid));
2750 attrzp->z_gid = new_gid;
2753 if (!(mask & AT_MODE)) {
2754 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
2755 NULL, &new_mode, sizeof (new_mode));
2756 new_mode = zp->z_mode;
2758 err = zfs_acl_chown_setattr(zp);
2759 ASSERT0(err);
2760 if (attrzp) {
2761 vn_seqc_write_begin(ZTOV(attrzp));
2762 err = zfs_acl_chown_setattr(attrzp);
2763 vn_seqc_write_end(ZTOV(attrzp));
2764 ASSERT0(err);
2768 if (mask & AT_MODE) {
2769 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
2770 &new_mode, sizeof (new_mode));
2771 zp->z_mode = new_mode;
2772 ASSERT3P(aclp, !=, NULL);
2773 err = zfs_aclset_common(zp, aclp, cr, tx);
2774 ASSERT0(err);
2775 if (zp->z_acl_cached)
2776 zfs_acl_free(zp->z_acl_cached);
2777 zp->z_acl_cached = aclp;
2778 aclp = NULL;
2782 if (mask & AT_ATIME) {
2783 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2784 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
2785 &zp->z_atime, sizeof (zp->z_atime));
2788 if (mask & AT_MTIME) {
2789 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
2790 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
2791 mtime, sizeof (mtime));
2794 if (projid != ZFS_INVALID_PROJID) {
2795 zp->z_projid = projid;
2796 SA_ADD_BULK_ATTR(bulk, count,
2797 SA_ZPL_PROJID(zfsvfs), NULL, &zp->z_projid,
2798 sizeof (zp->z_projid));
2801 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2802 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
2803 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
2804 NULL, mtime, sizeof (mtime));
2805 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
2806 &ctime, sizeof (ctime));
2807 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
2808 } else if (mask != 0) {
2809 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
2810 &ctime, sizeof (ctime));
2811 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime);
2812 if (attrzp) {
2813 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2814 SA_ZPL_CTIME(zfsvfs), NULL,
2815 &ctime, sizeof (ctime));
2816 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2817 mtime, ctime);
2822 * Do this after setting timestamps to prevent timestamp
2823 * update from toggling bit
2826 if (xoap && (mask & AT_XVATTR)) {
2828 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
2829 xoap->xoa_createtime = vap->va_birthtime;
2831 * restore trimmed off masks
2832 * so that return masks can be set for caller.
2835 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
2836 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2838 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
2839 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2841 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
2842 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2844 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
2845 XVA_SET_REQ(xvap, XAT_NODUMP);
2847 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
2848 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2850 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
2851 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2853 if (XVA_ISSET_REQ(&tmpxvattr, XAT_PROJINHERIT)) {
2854 XVA_SET_REQ(xvap, XAT_PROJINHERIT);
2857 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2858 ASSERT3S(vp->v_type, ==, VREG);
2860 zfs_xvattr_set(zp, xvap, tx);
2863 if (fuid_dirtied)
2864 zfs_fuid_sync(zfsvfs, tx);
2866 if (mask != 0)
2867 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2869 if (mask & (AT_UID|AT_GID|AT_MODE))
2870 mutex_exit(&zp->z_acl_lock);
2872 if (attrzp) {
2873 if (mask & (AT_UID|AT_GID|AT_MODE))
2874 mutex_exit(&attrzp->z_acl_lock);
2876 out:
2877 if (err == 0 && attrzp) {
2878 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
2879 xattr_count, tx);
2880 ASSERT0(err2);
2883 if (attrzp)
2884 vput(ZTOV(attrzp));
2886 if (aclp)
2887 zfs_acl_free(aclp);
2889 if (fuidp) {
2890 zfs_fuid_info_free(fuidp);
2891 fuidp = NULL;
2894 if (err) {
2895 dmu_tx_abort(tx);
2896 } else {
2897 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2898 dmu_tx_commit(tx);
2901 out2:
2902 if (os->os_sync == ZFS_SYNC_ALWAYS)
2903 zil_commit(zilog, 0);
2905 ZFS_EXIT(zfsvfs);
2906 return (err);
2910 * Look up the directory entries corresponding to the source and target
2911 * directory/name pairs.
2913 static int
2914 zfs_rename_relock_lookup(znode_t *sdzp, const struct componentname *scnp,
2915 znode_t **szpp, znode_t *tdzp, const struct componentname *tcnp,
2916 znode_t **tzpp)
2918 zfsvfs_t *zfsvfs;
2919 znode_t *szp, *tzp;
2920 int error;
2923 * Before using sdzp and tdzp we must ensure that they are live.
2924 * As a porting legacy from illumos we have two things to worry
2925 * about. One is typical for FreeBSD and it is that the vnode is
2926 * not reclaimed (doomed). The other is that the znode is live.
2927 * The current code can invalidate the znode without acquiring the
2928 * corresponding vnode lock if the object represented by the znode
2929 * and vnode is no longer valid after a rollback or receive operation.
2930 * z_teardown_lock hidden behind ZFS_ENTER and ZFS_EXIT is the lock
2931 * that protects the znodes from the invalidation.
2933 zfsvfs = sdzp->z_zfsvfs;
2934 ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs);
2935 ZFS_ENTER(zfsvfs);
2936 ZFS_VERIFY_ZP(sdzp);
2937 ZFS_VERIFY_ZP(tdzp);
2940 * Re-resolve svp to be certain it still exists and fetch the
2941 * correct vnode.
2943 error = zfs_dirent_lookup(sdzp, scnp->cn_nameptr, &szp, ZEXISTS);
2944 if (error != 0) {
2945 /* Source entry invalid or not there. */
2946 if ((scnp->cn_flags & ISDOTDOT) != 0 ||
2947 (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.'))
2948 error = SET_ERROR(EINVAL);
2949 goto out;
2951 *szpp = szp;
2954 * Re-resolve tvp, if it disappeared we just carry on.
2956 error = zfs_dirent_lookup(tdzp, tcnp->cn_nameptr, &tzp, 0);
2957 if (error != 0) {
2958 vrele(ZTOV(szp));
2959 if ((tcnp->cn_flags & ISDOTDOT) != 0)
2960 error = SET_ERROR(EINVAL);
2961 goto out;
2963 *tzpp = tzp;
2964 out:
2965 ZFS_EXIT(zfsvfs);
2966 return (error);
2970 * We acquire all but fdvp locks using non-blocking acquisitions. If we
2971 * fail to acquire any lock in the path we will drop all held locks,
2972 * acquire the new lock in a blocking fashion, and then release it and
2973 * restart the rename. This acquire/release step ensures that we do not
2974 * spin on a lock waiting for release. On error release all vnode locks
2975 * and decrement references the way tmpfs_rename() would do.
2977 static int
2978 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp,
2979 struct vnode *tdvp, struct vnode **tvpp,
2980 const struct componentname *scnp, const struct componentname *tcnp)
2982 struct vnode *nvp, *svp, *tvp;
2983 znode_t *sdzp, *tdzp, *szp, *tzp;
2984 int error;
2986 VOP_UNLOCK1(tdvp);
2987 if (*tvpp != NULL && *tvpp != tdvp)
2988 VOP_UNLOCK1(*tvpp);
2990 relock:
2991 error = vn_lock(sdvp, LK_EXCLUSIVE);
2992 if (error)
2993 goto out;
2994 error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT);
2995 if (error != 0) {
2996 VOP_UNLOCK1(sdvp);
2997 if (error != EBUSY)
2998 goto out;
2999 error = vn_lock(tdvp, LK_EXCLUSIVE);
3000 if (error)
3001 goto out;
3002 VOP_UNLOCK1(tdvp);
3003 goto relock;
3005 tdzp = VTOZ(tdvp);
3006 sdzp = VTOZ(sdvp);
3008 error = zfs_rename_relock_lookup(sdzp, scnp, &szp, tdzp, tcnp, &tzp);
3009 if (error != 0) {
3010 VOP_UNLOCK1(sdvp);
3011 VOP_UNLOCK1(tdvp);
3012 goto out;
3014 svp = ZTOV(szp);
3015 tvp = tzp != NULL ? ZTOV(tzp) : NULL;
3018 * Now try acquire locks on svp and tvp.
3020 nvp = svp;
3021 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3022 if (error != 0) {
3023 VOP_UNLOCK1(sdvp);
3024 VOP_UNLOCK1(tdvp);
3025 if (tvp != NULL)
3026 vrele(tvp);
3027 if (error != EBUSY) {
3028 vrele(nvp);
3029 goto out;
3031 error = vn_lock(nvp, LK_EXCLUSIVE);
3032 if (error != 0) {
3033 vrele(nvp);
3034 goto out;
3036 VOP_UNLOCK1(nvp);
3038 * Concurrent rename race.
3039 * XXX ?
3041 if (nvp == tdvp) {
3042 vrele(nvp);
3043 error = SET_ERROR(EINVAL);
3044 goto out;
3046 vrele(*svpp);
3047 *svpp = nvp;
3048 goto relock;
3050 vrele(*svpp);
3051 *svpp = nvp;
3053 if (*tvpp != NULL)
3054 vrele(*tvpp);
3055 *tvpp = NULL;
3056 if (tvp != NULL) {
3057 nvp = tvp;
3058 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3059 if (error != 0) {
3060 VOP_UNLOCK1(sdvp);
3061 VOP_UNLOCK1(tdvp);
3062 VOP_UNLOCK1(*svpp);
3063 if (error != EBUSY) {
3064 vrele(nvp);
3065 goto out;
3067 error = vn_lock(nvp, LK_EXCLUSIVE);
3068 if (error != 0) {
3069 vrele(nvp);
3070 goto out;
3072 vput(nvp);
3073 goto relock;
3075 *tvpp = nvp;
3078 return (0);
3080 out:
3081 return (error);
3085 * Note that we must use VRELE_ASYNC in this function as it walks
3086 * up the directory tree and vrele may need to acquire an exclusive
3087 * lock if a last reference to a vnode is dropped.
3089 static int
3090 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp)
3092 zfsvfs_t *zfsvfs;
3093 znode_t *zp, *zp1;
3094 uint64_t parent;
3095 int error;
3097 zfsvfs = tdzp->z_zfsvfs;
3098 if (tdzp == szp)
3099 return (SET_ERROR(EINVAL));
3100 if (tdzp == sdzp)
3101 return (0);
3102 if (tdzp->z_id == zfsvfs->z_root)
3103 return (0);
3104 zp = tdzp;
3105 for (;;) {
3106 ASSERT(!zp->z_unlinked);
3107 if ((error = sa_lookup(zp->z_sa_hdl,
3108 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
3109 break;
3111 if (parent == szp->z_id) {
3112 error = SET_ERROR(EINVAL);
3113 break;
3115 if (parent == zfsvfs->z_root)
3116 break;
3117 if (parent == sdzp->z_id)
3118 break;
3120 error = zfs_zget(zfsvfs, parent, &zp1);
3121 if (error != 0)
3122 break;
3124 if (zp != tdzp)
3125 VN_RELE_ASYNC(ZTOV(zp),
3126 dsl_pool_zrele_taskq(
3127 dmu_objset_pool(zfsvfs->z_os)));
3128 zp = zp1;
3131 if (error == ENOTDIR)
3132 panic("checkpath: .. not a directory\n");
3133 if (zp != tdzp)
3134 VN_RELE_ASYNC(ZTOV(zp),
3135 dsl_pool_zrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3136 return (error);
3139 #if __FreeBSD_version < 1300124
3140 static void
3141 cache_vop_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
3142 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
3145 cache_purge(fvp);
3146 if (tvp != NULL)
3147 cache_purge(tvp);
3148 cache_purge_negative(tdvp);
3150 #endif
3152 static int
3153 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3154 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3155 cred_t *cr);
3158 * Move an entry from the provided source directory to the target
3159 * directory. Change the entry name as indicated.
3161 * IN: sdvp - Source directory containing the "old entry".
3162 * scnp - Old entry name.
3163 * tdvp - Target directory to contain the "new entry".
3164 * tcnp - New entry name.
3165 * cr - credentials of caller.
3166 * INOUT: svpp - Source file
3167 * tvpp - Target file, may point to NULL initially
3169 * RETURN: 0 on success, error code on failure.
3171 * Timestamps:
3172 * sdvp,tdvp - ctime|mtime updated
3174 static int
3175 zfs_do_rename(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3176 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3177 cred_t *cr)
3179 int error;
3181 ASSERT_VOP_ELOCKED(tdvp, __func__);
3182 if (*tvpp != NULL)
3183 ASSERT_VOP_ELOCKED(*tvpp, __func__);
3185 /* Reject renames across filesystems. */
3186 if ((*svpp)->v_mount != tdvp->v_mount ||
3187 ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) {
3188 error = SET_ERROR(EXDEV);
3189 goto out;
3192 if (zfsctl_is_node(tdvp)) {
3193 error = SET_ERROR(EXDEV);
3194 goto out;
3198 * Lock all four vnodes to ensure safety and semantics of renaming.
3200 error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp);
3201 if (error != 0) {
3202 /* no vnodes are locked in the case of error here */
3203 return (error);
3206 error = zfs_do_rename_impl(sdvp, svpp, scnp, tdvp, tvpp, tcnp, cr);
3207 VOP_UNLOCK1(sdvp);
3208 VOP_UNLOCK1(*svpp);
3209 out:
3210 if (*tvpp != NULL)
3211 VOP_UNLOCK1(*tvpp);
3212 if (tdvp != *tvpp)
3213 VOP_UNLOCK1(tdvp);
3215 return (error);
3218 static int
3219 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3220 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3221 cred_t *cr)
3223 dmu_tx_t *tx;
3224 zfsvfs_t *zfsvfs;
3225 zilog_t *zilog;
3226 znode_t *tdzp, *sdzp, *tzp, *szp;
3227 const char *snm = scnp->cn_nameptr;
3228 const char *tnm = tcnp->cn_nameptr;
3229 int error;
3231 tdzp = VTOZ(tdvp);
3232 sdzp = VTOZ(sdvp);
3233 zfsvfs = tdzp->z_zfsvfs;
3235 ZFS_ENTER(zfsvfs);
3236 ZFS_VERIFY_ZP(tdzp);
3237 ZFS_VERIFY_ZP(sdzp);
3238 zilog = zfsvfs->z_log;
3240 if (zfsvfs->z_utf8 && u8_validate(tnm,
3241 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3242 error = SET_ERROR(EILSEQ);
3243 goto out;
3246 /* If source and target are the same file, there is nothing to do. */
3247 if ((*svpp) == (*tvpp)) {
3248 error = 0;
3249 goto out;
3252 if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) ||
3253 ((*tvpp) != NULL && (*tvpp)->v_type == VDIR &&
3254 (*tvpp)->v_mountedhere != NULL)) {
3255 error = SET_ERROR(EXDEV);
3256 goto out;
3259 szp = VTOZ(*svpp);
3260 ZFS_VERIFY_ZP(szp);
3261 tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp);
3262 if (tzp != NULL)
3263 ZFS_VERIFY_ZP(tzp);
3266 * This is to prevent the creation of links into attribute space
3267 * by renaming a linked file into/outof an attribute directory.
3268 * See the comment in zfs_link() for why this is considered bad.
3270 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3271 error = SET_ERROR(EINVAL);
3272 goto out;
3276 * If we are using project inheritance, means if the directory has
3277 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3278 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3279 * such case, we only allow renames into our tree when the project
3280 * IDs are the same.
3282 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3283 tdzp->z_projid != szp->z_projid) {
3284 error = SET_ERROR(EXDEV);
3285 goto out;
3289 * Must have write access at the source to remove the old entry
3290 * and write access at the target to create the new entry.
3291 * Note that if target and source are the same, this can be
3292 * done in a single check.
3294 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3295 goto out;
3297 if ((*svpp)->v_type == VDIR) {
3299 * Avoid ".", "..", and aliases of "." for obvious reasons.
3301 if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') ||
3302 sdzp == szp ||
3303 (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
3304 error = EINVAL;
3305 goto out;
3309 * Check to make sure rename is valid.
3310 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3312 if ((error = zfs_rename_check(szp, sdzp, tdzp)))
3313 goto out;
3317 * Does target exist?
3319 if (tzp) {
3321 * Source and target must be the same type.
3323 if ((*svpp)->v_type == VDIR) {
3324 if ((*tvpp)->v_type != VDIR) {
3325 error = SET_ERROR(ENOTDIR);
3326 goto out;
3327 } else {
3328 cache_purge(tdvp);
3329 if (sdvp != tdvp)
3330 cache_purge(sdvp);
3332 } else {
3333 if ((*tvpp)->v_type == VDIR) {
3334 error = SET_ERROR(EISDIR);
3335 goto out;
3340 vn_seqc_write_begin(*svpp);
3341 vn_seqc_write_begin(sdvp);
3342 if (*tvpp != NULL)
3343 vn_seqc_write_begin(*tvpp);
3344 if (tdvp != *tvpp)
3345 vn_seqc_write_begin(tdvp);
3347 vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct);
3348 if (tzp)
3349 vnevent_rename_dest(*tvpp, tdvp, tnm, ct);
3352 * notify the target directory if it is not the same
3353 * as source directory.
3355 if (tdvp != sdvp) {
3356 vnevent_rename_dest_dir(tdvp, ct);
3359 tx = dmu_tx_create(zfsvfs->z_os);
3360 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3361 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3362 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3363 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3364 if (sdzp != tdzp) {
3365 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3366 zfs_sa_upgrade_txholds(tx, tdzp);
3368 if (tzp) {
3369 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3370 zfs_sa_upgrade_txholds(tx, tzp);
3373 zfs_sa_upgrade_txholds(tx, szp);
3374 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3375 error = dmu_tx_assign(tx, TXG_WAIT);
3376 if (error) {
3377 dmu_tx_abort(tx);
3378 goto out_seq;
3381 if (tzp) /* Attempt to remove the existing target */
3382 error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL);
3384 if (error == 0) {
3385 error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING);
3386 if (error == 0) {
3387 szp->z_pflags |= ZFS_AV_MODIFIED;
3389 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3390 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3391 ASSERT0(error);
3393 error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING,
3394 NULL);
3395 if (error == 0) {
3396 zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
3397 snm, tdzp, tnm, szp);
3400 * Update path information for the target vnode
3402 vn_renamepath(tdvp, *svpp, tnm, strlen(tnm));
3403 } else {
3405 * At this point, we have successfully created
3406 * the target name, but have failed to remove
3407 * the source name. Since the create was done
3408 * with the ZRENAMING flag, there are
3409 * complications; for one, the link count is
3410 * wrong. The easiest way to deal with this
3411 * is to remove the newly created target, and
3412 * return the original error. This must
3413 * succeed; fortunately, it is very unlikely to
3414 * fail, since we just created it.
3416 VERIFY0(zfs_link_destroy(tdzp, tnm, szp, tx,
3417 ZRENAMING, NULL));
3420 if (error == 0) {
3421 cache_vop_rename(sdvp, *svpp, tdvp, *tvpp, scnp, tcnp);
3425 dmu_tx_commit(tx);
3427 out_seq:
3428 vn_seqc_write_end(*svpp);
3429 vn_seqc_write_end(sdvp);
3430 if (*tvpp != NULL)
3431 vn_seqc_write_end(*tvpp);
3432 if (tdvp != *tvpp)
3433 vn_seqc_write_end(tdvp);
3435 out:
3436 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3437 zil_commit(zilog, 0);
3438 ZFS_EXIT(zfsvfs);
3440 return (error);
3444 zfs_rename(znode_t *sdzp, const char *sname, znode_t *tdzp, const char *tname,
3445 cred_t *cr, int flags)
3447 struct componentname scn, tcn;
3448 vnode_t *sdvp, *tdvp;
3449 vnode_t *svp, *tvp;
3450 int error;
3451 svp = tvp = NULL;
3453 sdvp = ZTOV(sdzp);
3454 tdvp = ZTOV(tdzp);
3455 error = zfs_lookup_internal(sdzp, sname, &svp, &scn, DELETE);
3456 if (sdzp->z_zfsvfs->z_replay == B_FALSE)
3457 VOP_UNLOCK1(sdvp);
3458 if (error != 0)
3459 goto fail;
3460 VOP_UNLOCK1(svp);
3462 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
3463 error = zfs_lookup_internal(tdzp, tname, &tvp, &tcn, RENAME);
3464 if (error == EJUSTRETURN)
3465 tvp = NULL;
3466 else if (error != 0) {
3467 VOP_UNLOCK1(tdvp);
3468 goto fail;
3471 error = zfs_do_rename(sdvp, &svp, &scn, tdvp, &tvp, &tcn, cr);
3472 fail:
3473 if (svp != NULL)
3474 vrele(svp);
3475 if (tvp != NULL)
3476 vrele(tvp);
3478 return (error);
3482 * Insert the indicated symbolic reference entry into the directory.
3484 * IN: dvp - Directory to contain new symbolic link.
3485 * link - Name for new symlink entry.
3486 * vap - Attributes of new entry.
3487 * cr - credentials of caller.
3488 * ct - caller context
3489 * flags - case flags
3491 * RETURN: 0 on success, error code on failure.
3493 * Timestamps:
3494 * dvp - ctime|mtime updated
3497 zfs_symlink(znode_t *dzp, const char *name, vattr_t *vap,
3498 const char *link, znode_t **zpp, cred_t *cr, int flags)
3500 (void) flags;
3501 znode_t *zp;
3502 dmu_tx_t *tx;
3503 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3504 zilog_t *zilog;
3505 uint64_t len = strlen(link);
3506 int error;
3507 zfs_acl_ids_t acl_ids;
3508 boolean_t fuid_dirtied;
3509 uint64_t txtype = TX_SYMLINK;
3511 ASSERT3S(vap->va_type, ==, VLNK);
3513 ZFS_ENTER(zfsvfs);
3514 ZFS_VERIFY_ZP(dzp);
3515 zilog = zfsvfs->z_log;
3517 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3518 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3519 ZFS_EXIT(zfsvfs);
3520 return (SET_ERROR(EILSEQ));
3523 if (len > MAXPATHLEN) {
3524 ZFS_EXIT(zfsvfs);
3525 return (SET_ERROR(ENAMETOOLONG));
3528 if ((error = zfs_acl_ids_create(dzp, 0,
3529 vap, cr, NULL, &acl_ids)) != 0) {
3530 ZFS_EXIT(zfsvfs);
3531 return (error);
3535 * Attempt to lock directory; fail if entry already exists.
3537 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
3538 if (error) {
3539 zfs_acl_ids_free(&acl_ids);
3540 ZFS_EXIT(zfsvfs);
3541 return (error);
3544 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3545 zfs_acl_ids_free(&acl_ids);
3546 ZFS_EXIT(zfsvfs);
3547 return (error);
3550 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids,
3551 0 /* projid */)) {
3552 zfs_acl_ids_free(&acl_ids);
3553 ZFS_EXIT(zfsvfs);
3554 return (SET_ERROR(EDQUOT));
3557 getnewvnode_reserve_();
3558 tx = dmu_tx_create(zfsvfs->z_os);
3559 fuid_dirtied = zfsvfs->z_fuid_dirty;
3560 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3561 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3562 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3563 ZFS_SA_BASE_ATTR_SIZE + len);
3564 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3565 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3566 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3567 acl_ids.z_aclp->z_acl_bytes);
3569 if (fuid_dirtied)
3570 zfs_fuid_txhold(zfsvfs, tx);
3571 error = dmu_tx_assign(tx, TXG_WAIT);
3572 if (error) {
3573 zfs_acl_ids_free(&acl_ids);
3574 dmu_tx_abort(tx);
3575 getnewvnode_drop_reserve();
3576 ZFS_EXIT(zfsvfs);
3577 return (error);
3581 * Create a new object for the symlink.
3582 * for version 4 ZPL datasets the symlink will be an SA attribute
3584 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3586 if (fuid_dirtied)
3587 zfs_fuid_sync(zfsvfs, tx);
3589 if (zp->z_is_sa)
3590 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3591 __DECONST(void *, link), len, tx);
3592 else
3593 zfs_sa_symlink(zp, __DECONST(char *, link), len, tx);
3595 zp->z_size = len;
3596 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3597 &zp->z_size, sizeof (zp->z_size), tx);
3599 * Insert the new object into the directory.
3601 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
3603 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3604 *zpp = zp;
3606 zfs_acl_ids_free(&acl_ids);
3608 dmu_tx_commit(tx);
3610 getnewvnode_drop_reserve();
3612 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3613 zil_commit(zilog, 0);
3615 ZFS_EXIT(zfsvfs);
3616 return (error);
3620 * Return, in the buffer contained in the provided uio structure,
3621 * the symbolic path referred to by vp.
3623 * IN: vp - vnode of symbolic link.
3624 * uio - structure to contain the link path.
3625 * cr - credentials of caller.
3626 * ct - caller context
3628 * OUT: uio - structure containing the link path.
3630 * RETURN: 0 on success, error code on failure.
3632 * Timestamps:
3633 * vp - atime updated
3635 static int
3636 zfs_readlink(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, caller_context_t *ct)
3638 (void) cr, (void) ct;
3639 znode_t *zp = VTOZ(vp);
3640 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3641 int error;
3643 ZFS_ENTER(zfsvfs);
3644 ZFS_VERIFY_ZP(zp);
3646 if (zp->z_is_sa)
3647 error = sa_lookup_uio(zp->z_sa_hdl,
3648 SA_ZPL_SYMLINK(zfsvfs), uio);
3649 else
3650 error = zfs_sa_readlink(zp, uio);
3652 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3654 ZFS_EXIT(zfsvfs);
3655 return (error);
3659 * Insert a new entry into directory tdvp referencing svp.
3661 * IN: tdvp - Directory to contain new entry.
3662 * svp - vnode of new entry.
3663 * name - name of new entry.
3664 * cr - credentials of caller.
3666 * RETURN: 0 on success, error code on failure.
3668 * Timestamps:
3669 * tdvp - ctime|mtime updated
3670 * svp - ctime updated
3673 zfs_link(znode_t *tdzp, znode_t *szp, const char *name, cred_t *cr,
3674 int flags)
3676 (void) flags;
3677 znode_t *tzp;
3678 zfsvfs_t *zfsvfs = tdzp->z_zfsvfs;
3679 zilog_t *zilog;
3680 dmu_tx_t *tx;
3681 int error;
3682 uint64_t parent;
3683 uid_t owner;
3685 ASSERT3S(ZTOV(tdzp)->v_type, ==, VDIR);
3687 ZFS_ENTER(zfsvfs);
3688 ZFS_VERIFY_ZP(tdzp);
3689 zilog = zfsvfs->z_log;
3692 * POSIX dictates that we return EPERM here.
3693 * Better choices include ENOTSUP or EISDIR.
3695 if (ZTOV(szp)->v_type == VDIR) {
3696 ZFS_EXIT(zfsvfs);
3697 return (SET_ERROR(EPERM));
3700 ZFS_VERIFY_ZP(szp);
3703 * If we are using project inheritance, means if the directory has
3704 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3705 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3706 * such case, we only allow hard link creation in our tree when the
3707 * project IDs are the same.
3709 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3710 tdzp->z_projid != szp->z_projid) {
3711 ZFS_EXIT(zfsvfs);
3712 return (SET_ERROR(EXDEV));
3715 if (szp->z_pflags & (ZFS_APPENDONLY |
3716 ZFS_IMMUTABLE | ZFS_READONLY)) {
3717 ZFS_EXIT(zfsvfs);
3718 return (SET_ERROR(EPERM));
3721 /* Prevent links to .zfs/shares files */
3723 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
3724 &parent, sizeof (uint64_t))) != 0) {
3725 ZFS_EXIT(zfsvfs);
3726 return (error);
3728 if (parent == zfsvfs->z_shares_dir) {
3729 ZFS_EXIT(zfsvfs);
3730 return (SET_ERROR(EPERM));
3733 if (zfsvfs->z_utf8 && u8_validate(name,
3734 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3735 ZFS_EXIT(zfsvfs);
3736 return (SET_ERROR(EILSEQ));
3740 * We do not support links between attributes and non-attributes
3741 * because of the potential security risk of creating links
3742 * into "normal" file space in order to circumvent restrictions
3743 * imposed in attribute space.
3745 if ((szp->z_pflags & ZFS_XATTR) != (tdzp->z_pflags & ZFS_XATTR)) {
3746 ZFS_EXIT(zfsvfs);
3747 return (SET_ERROR(EINVAL));
3751 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
3752 if (owner != crgetuid(cr) && secpolicy_basic_link(ZTOV(szp), cr) != 0) {
3753 ZFS_EXIT(zfsvfs);
3754 return (SET_ERROR(EPERM));
3757 if ((error = zfs_zaccess(tdzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3758 ZFS_EXIT(zfsvfs);
3759 return (error);
3763 * Attempt to lock directory; fail if entry already exists.
3765 error = zfs_dirent_lookup(tdzp, name, &tzp, ZNEW);
3766 if (error) {
3767 ZFS_EXIT(zfsvfs);
3768 return (error);
3771 tx = dmu_tx_create(zfsvfs->z_os);
3772 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3773 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, name);
3774 zfs_sa_upgrade_txholds(tx, szp);
3775 zfs_sa_upgrade_txholds(tx, tdzp);
3776 error = dmu_tx_assign(tx, TXG_WAIT);
3777 if (error) {
3778 dmu_tx_abort(tx);
3779 ZFS_EXIT(zfsvfs);
3780 return (error);
3783 error = zfs_link_create(tdzp, name, szp, tx, 0);
3785 if (error == 0) {
3786 uint64_t txtype = TX_LINK;
3787 zfs_log_link(zilog, tx, txtype, tdzp, szp, name);
3790 dmu_tx_commit(tx);
3792 if (error == 0) {
3793 vnevent_link(ZTOV(szp), ct);
3796 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3797 zil_commit(zilog, 0);
3799 ZFS_EXIT(zfsvfs);
3800 return (error);
3804 * Free or allocate space in a file. Currently, this function only
3805 * supports the `F_FREESP' command. However, this command is somewhat
3806 * misnamed, as its functionality includes the ability to allocate as
3807 * well as free space.
3809 * IN: ip - inode of file to free data in.
3810 * cmd - action to take (only F_FREESP supported).
3811 * bfp - section of file to free/alloc.
3812 * flag - current file open mode flags.
3813 * offset - current file offset.
3814 * cr - credentials of caller.
3816 * RETURN: 0 on success, error code on failure.
3818 * Timestamps:
3819 * ip - ctime|mtime updated
3822 zfs_space(znode_t *zp, int cmd, flock64_t *bfp, int flag,
3823 offset_t offset, cred_t *cr)
3825 (void) offset;
3826 zfsvfs_t *zfsvfs = ZTOZSB(zp);
3827 uint64_t off, len;
3828 int error;
3830 ZFS_ENTER(zfsvfs);
3831 ZFS_VERIFY_ZP(zp);
3833 if (cmd != F_FREESP) {
3834 ZFS_EXIT(zfsvfs);
3835 return (SET_ERROR(EINVAL));
3839 * Callers might not be able to detect properly that we are read-only,
3840 * so check it explicitly here.
3842 if (zfs_is_readonly(zfsvfs)) {
3843 ZFS_EXIT(zfsvfs);
3844 return (SET_ERROR(EROFS));
3847 if (bfp->l_len < 0) {
3848 ZFS_EXIT(zfsvfs);
3849 return (SET_ERROR(EINVAL));
3853 * Permissions aren't checked on Solaris because on this OS
3854 * zfs_space() can only be called with an opened file handle.
3855 * On Linux we can get here through truncate_range() which
3856 * operates directly on inodes, so we need to check access rights.
3858 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
3859 ZFS_EXIT(zfsvfs);
3860 return (error);
3863 off = bfp->l_start;
3864 len = bfp->l_len; /* 0 means from off to end of file */
3866 error = zfs_freesp(zp, off, len, flag, TRUE);
3868 ZFS_EXIT(zfsvfs);
3869 return (error);
3872 static void
3873 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3875 (void) cr, (void) ct;
3876 znode_t *zp = VTOZ(vp);
3877 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3878 int error;
3880 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
3881 if (zp->z_sa_hdl == NULL) {
3883 * The fs has been unmounted, or we did a
3884 * suspend/resume and this file no longer exists.
3886 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3887 vrecycle(vp);
3888 return;
3891 if (zp->z_unlinked) {
3893 * Fast path to recycle a vnode of a removed file.
3895 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3896 vrecycle(vp);
3897 return;
3900 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3901 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3903 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3904 zfs_sa_upgrade_txholds(tx, zp);
3905 error = dmu_tx_assign(tx, TXG_WAIT);
3906 if (error) {
3907 dmu_tx_abort(tx);
3908 } else {
3909 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
3910 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
3911 zp->z_atime_dirty = 0;
3912 dmu_tx_commit(tx);
3915 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3919 _Static_assert(sizeof (struct zfid_short) <= sizeof (struct fid),
3920 "struct zfid_short bigger than struct fid");
3921 _Static_assert(sizeof (struct zfid_long) <= sizeof (struct fid),
3922 "struct zfid_long bigger than struct fid");
3924 static int
3925 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3927 (void) ct;
3928 znode_t *zp = VTOZ(vp);
3929 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3930 uint32_t gen;
3931 uint64_t gen64;
3932 uint64_t object = zp->z_id;
3933 zfid_short_t *zfid;
3934 int size, i, error;
3936 ZFS_ENTER(zfsvfs);
3937 ZFS_VERIFY_ZP(zp);
3939 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
3940 &gen64, sizeof (uint64_t))) != 0) {
3941 ZFS_EXIT(zfsvfs);
3942 return (error);
3945 gen = (uint32_t)gen64;
3947 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3948 fidp->fid_len = size;
3950 zfid = (zfid_short_t *)fidp;
3952 zfid->zf_len = size;
3954 for (i = 0; i < sizeof (zfid->zf_object); i++)
3955 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3957 /* Must have a non-zero generation number to distinguish from .zfs */
3958 if (gen == 0)
3959 gen = 1;
3960 for (i = 0; i < sizeof (zfid->zf_gen); i++)
3961 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3963 if (size == LONG_FID_LEN) {
3964 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
3965 zfid_long_t *zlfid;
3967 zlfid = (zfid_long_t *)fidp;
3969 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3970 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3972 /* XXX - this should be the generation number for the objset */
3973 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3974 zlfid->zf_setgen[i] = 0;
3977 ZFS_EXIT(zfsvfs);
3978 return (0);
3981 static int
3982 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
3983 caller_context_t *ct)
3985 znode_t *zp;
3986 zfsvfs_t *zfsvfs;
3988 switch (cmd) {
3989 case _PC_LINK_MAX:
3990 *valp = MIN(LONG_MAX, ZFS_LINK_MAX);
3991 return (0);
3993 case _PC_FILESIZEBITS:
3994 *valp = 64;
3995 return (0);
3996 case _PC_MIN_HOLE_SIZE:
3997 *valp = (int)SPA_MINBLOCKSIZE;
3998 return (0);
3999 case _PC_ACL_EXTENDED:
4000 #if 0 /* POSIX ACLs are not implemented for ZFS on FreeBSD yet. */
4001 zp = VTOZ(vp);
4002 zfsvfs = zp->z_zfsvfs;
4003 ZFS_ENTER(zfsvfs);
4004 ZFS_VERIFY_ZP(zp);
4005 *valp = zfsvfs->z_acl_type == ZFSACLTYPE_POSIX ? 1 : 0;
4006 ZFS_EXIT(zfsvfs);
4007 #else
4008 *valp = 0;
4009 #endif
4010 return (0);
4012 case _PC_ACL_NFS4:
4013 zp = VTOZ(vp);
4014 zfsvfs = zp->z_zfsvfs;
4015 ZFS_ENTER(zfsvfs);
4016 ZFS_VERIFY_ZP(zp);
4017 *valp = zfsvfs->z_acl_type == ZFS_ACLTYPE_NFSV4 ? 1 : 0;
4018 ZFS_EXIT(zfsvfs);
4019 return (0);
4021 case _PC_ACL_PATH_MAX:
4022 *valp = ACL_MAX_ENTRIES;
4023 return (0);
4025 default:
4026 return (EOPNOTSUPP);
4030 static int
4031 zfs_getpages(struct vnode *vp, vm_page_t *ma, int count, int *rbehind,
4032 int *rahead)
4034 znode_t *zp = VTOZ(vp);
4035 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4036 zfs_locked_range_t *lr;
4037 vm_object_t object;
4038 off_t start, end, obj_size;
4039 uint_t blksz;
4040 int pgsin_b, pgsin_a;
4041 int error;
4043 ZFS_ENTER(zfsvfs);
4044 ZFS_VERIFY_ZP(zp);
4046 start = IDX_TO_OFF(ma[0]->pindex);
4047 end = IDX_TO_OFF(ma[count - 1]->pindex + 1);
4050 * Lock a range covering all required and optional pages.
4051 * Note that we need to handle the case of the block size growing.
4053 for (;;) {
4054 blksz = zp->z_blksz;
4055 lr = zfs_rangelock_tryenter(&zp->z_rangelock,
4056 rounddown(start, blksz),
4057 roundup(end, blksz) - rounddown(start, blksz), RL_READER);
4058 if (lr == NULL) {
4059 if (rahead != NULL) {
4060 *rahead = 0;
4061 rahead = NULL;
4063 if (rbehind != NULL) {
4064 *rbehind = 0;
4065 rbehind = NULL;
4067 break;
4069 if (blksz == zp->z_blksz)
4070 break;
4071 zfs_rangelock_exit(lr);
4074 object = ma[0]->object;
4075 zfs_vmobject_wlock(object);
4076 obj_size = object->un_pager.vnp.vnp_size;
4077 zfs_vmobject_wunlock(object);
4078 if (IDX_TO_OFF(ma[count - 1]->pindex) >= obj_size) {
4079 if (lr != NULL)
4080 zfs_rangelock_exit(lr);
4081 ZFS_EXIT(zfsvfs);
4082 return (zfs_vm_pagerret_bad);
4085 pgsin_b = 0;
4086 if (rbehind != NULL) {
4087 pgsin_b = OFF_TO_IDX(start - rounddown(start, blksz));
4088 pgsin_b = MIN(*rbehind, pgsin_b);
4091 pgsin_a = 0;
4092 if (rahead != NULL) {
4093 pgsin_a = OFF_TO_IDX(roundup(end, blksz) - end);
4094 if (end + IDX_TO_OFF(pgsin_a) >= obj_size)
4095 pgsin_a = OFF_TO_IDX(round_page(obj_size) - end);
4096 pgsin_a = MIN(*rahead, pgsin_a);
4100 * NB: we need to pass the exact byte size of the data that we expect
4101 * to read after accounting for the file size. This is required because
4102 * ZFS will panic if we request DMU to read beyond the end of the last
4103 * allocated block.
4105 error = dmu_read_pages(zfsvfs->z_os, zp->z_id, ma, count, &pgsin_b,
4106 &pgsin_a, MIN(end, obj_size) - (end - PAGE_SIZE));
4108 if (lr != NULL)
4109 zfs_rangelock_exit(lr);
4110 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4112 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, count*PAGE_SIZE);
4114 ZFS_EXIT(zfsvfs);
4116 if (error != 0)
4117 return (zfs_vm_pagerret_error);
4119 VM_CNT_INC(v_vnodein);
4120 VM_CNT_ADD(v_vnodepgsin, count + pgsin_b + pgsin_a);
4121 if (rbehind != NULL)
4122 *rbehind = pgsin_b;
4123 if (rahead != NULL)
4124 *rahead = pgsin_a;
4125 return (zfs_vm_pagerret_ok);
4128 #ifndef _SYS_SYSPROTO_H_
4129 struct vop_getpages_args {
4130 struct vnode *a_vp;
4131 vm_page_t *a_m;
4132 int a_count;
4133 int *a_rbehind;
4134 int *a_rahead;
4136 #endif
4138 static int
4139 zfs_freebsd_getpages(struct vop_getpages_args *ap)
4142 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4143 ap->a_rahead));
4146 static int
4147 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4148 int *rtvals)
4150 znode_t *zp = VTOZ(vp);
4151 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4152 zfs_locked_range_t *lr;
4153 dmu_tx_t *tx;
4154 struct sf_buf *sf;
4155 vm_object_t object;
4156 vm_page_t m;
4157 caddr_t va;
4158 size_t tocopy;
4159 size_t lo_len;
4160 vm_ooffset_t lo_off;
4161 vm_ooffset_t off;
4162 uint_t blksz;
4163 int ncount;
4164 int pcount;
4165 int err;
4166 int i;
4168 ZFS_ENTER(zfsvfs);
4169 ZFS_VERIFY_ZP(zp);
4171 object = vp->v_object;
4172 pcount = btoc(len);
4173 ncount = pcount;
4175 KASSERT(ma[0]->object == object, ("mismatching object"));
4176 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4178 for (i = 0; i < pcount; i++)
4179 rtvals[i] = zfs_vm_pagerret_error;
4181 off = IDX_TO_OFF(ma[0]->pindex);
4182 blksz = zp->z_blksz;
4183 lo_off = rounddown(off, blksz);
4184 lo_len = roundup(len + (off - lo_off), blksz);
4185 lr = zfs_rangelock_enter(&zp->z_rangelock, lo_off, lo_len, RL_WRITER);
4187 zfs_vmobject_wlock(object);
4188 if (len + off > object->un_pager.vnp.vnp_size) {
4189 if (object->un_pager.vnp.vnp_size > off) {
4190 int pgoff;
4192 len = object->un_pager.vnp.vnp_size - off;
4193 ncount = btoc(len);
4194 if ((pgoff = (int)len & PAGE_MASK) != 0) {
4196 * If the object is locked and the following
4197 * conditions hold, then the page's dirty
4198 * field cannot be concurrently changed by a
4199 * pmap operation.
4201 m = ma[ncount - 1];
4202 vm_page_assert_sbusied(m);
4203 KASSERT(!pmap_page_is_write_mapped(m),
4204 ("zfs_putpages: page %p is not read-only",
4205 m));
4206 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
4207 pgoff);
4209 } else {
4210 len = 0;
4211 ncount = 0;
4213 if (ncount < pcount) {
4214 for (i = ncount; i < pcount; i++) {
4215 rtvals[i] = zfs_vm_pagerret_bad;
4219 zfs_vmobject_wunlock(object);
4221 if (ncount == 0)
4222 goto out;
4224 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, zp->z_uid) ||
4225 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, zp->z_gid) ||
4226 (zp->z_projid != ZFS_DEFAULT_PROJID &&
4227 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
4228 zp->z_projid))) {
4229 goto out;
4232 tx = dmu_tx_create(zfsvfs->z_os);
4233 dmu_tx_hold_write(tx, zp->z_id, off, len);
4235 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4236 zfs_sa_upgrade_txholds(tx, zp);
4237 err = dmu_tx_assign(tx, TXG_WAIT);
4238 if (err != 0) {
4239 dmu_tx_abort(tx);
4240 goto out;
4243 if (zp->z_blksz < PAGE_SIZE) {
4244 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
4245 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
4246 va = zfs_map_page(ma[i], &sf);
4247 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
4248 zfs_unmap_page(sf);
4250 } else {
4251 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
4254 if (err == 0) {
4255 uint64_t mtime[2], ctime[2];
4256 sa_bulk_attr_t bulk[3];
4257 int count = 0;
4259 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4260 &mtime, 16);
4261 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4262 &ctime, 16);
4263 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4264 &zp->z_pflags, 8);
4265 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
4266 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4267 ASSERT0(err);
4269 * XXX we should be passing a callback to undirty
4270 * but that would make the locking messier
4272 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off,
4273 len, 0, NULL, NULL);
4275 zfs_vmobject_wlock(object);
4276 for (i = 0; i < ncount; i++) {
4277 rtvals[i] = zfs_vm_pagerret_ok;
4278 vm_page_undirty(ma[i]);
4280 zfs_vmobject_wunlock(object);
4281 VM_CNT_INC(v_vnodeout);
4282 VM_CNT_ADD(v_vnodepgsout, ncount);
4284 dmu_tx_commit(tx);
4286 out:
4287 zfs_rangelock_exit(lr);
4288 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
4289 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4290 zil_commit(zfsvfs->z_log, zp->z_id);
4292 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, len);
4294 ZFS_EXIT(zfsvfs);
4295 return (rtvals[0]);
4298 #ifndef _SYS_SYSPROTO_H_
4299 struct vop_putpages_args {
4300 struct vnode *a_vp;
4301 vm_page_t *a_m;
4302 int a_count;
4303 int a_sync;
4304 int *a_rtvals;
4306 #endif
4308 static int
4309 zfs_freebsd_putpages(struct vop_putpages_args *ap)
4312 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
4313 ap->a_rtvals));
4316 #ifndef _SYS_SYSPROTO_H_
4317 struct vop_bmap_args {
4318 struct vnode *a_vp;
4319 daddr_t a_bn;
4320 struct bufobj **a_bop;
4321 daddr_t *a_bnp;
4322 int *a_runp;
4323 int *a_runb;
4325 #endif
4327 static int
4328 zfs_freebsd_bmap(struct vop_bmap_args *ap)
4331 if (ap->a_bop != NULL)
4332 *ap->a_bop = &ap->a_vp->v_bufobj;
4333 if (ap->a_bnp != NULL)
4334 *ap->a_bnp = ap->a_bn;
4335 if (ap->a_runp != NULL)
4336 *ap->a_runp = 0;
4337 if (ap->a_runb != NULL)
4338 *ap->a_runb = 0;
4340 return (0);
4343 #ifndef _SYS_SYSPROTO_H_
4344 struct vop_open_args {
4345 struct vnode *a_vp;
4346 int a_mode;
4347 struct ucred *a_cred;
4348 struct thread *a_td;
4350 #endif
4352 static int
4353 zfs_freebsd_open(struct vop_open_args *ap)
4355 vnode_t *vp = ap->a_vp;
4356 znode_t *zp = VTOZ(vp);
4357 int error;
4359 error = zfs_open(&vp, ap->a_mode, ap->a_cred);
4360 if (error == 0)
4361 vnode_create_vobject(vp, zp->z_size, ap->a_td);
4362 return (error);
4365 #ifndef _SYS_SYSPROTO_H_
4366 struct vop_close_args {
4367 struct vnode *a_vp;
4368 int a_fflag;
4369 struct ucred *a_cred;
4370 struct thread *a_td;
4372 #endif
4374 static int
4375 zfs_freebsd_close(struct vop_close_args *ap)
4378 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred));
4381 #ifndef _SYS_SYSPROTO_H_
4382 struct vop_ioctl_args {
4383 struct vnode *a_vp;
4384 ulong_t a_command;
4385 caddr_t a_data;
4386 int a_fflag;
4387 struct ucred *cred;
4388 struct thread *td;
4390 #endif
4392 static int
4393 zfs_freebsd_ioctl(struct vop_ioctl_args *ap)
4396 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4397 ap->a_fflag, ap->a_cred, NULL));
4400 static int
4401 ioflags(int ioflags)
4403 int flags = 0;
4405 if (ioflags & IO_APPEND)
4406 flags |= FAPPEND;
4407 if (ioflags & IO_NDELAY)
4408 flags |= FNONBLOCK;
4409 if (ioflags & IO_SYNC)
4410 flags |= (FSYNC | FDSYNC | FRSYNC);
4412 return (flags);
4415 #ifndef _SYS_SYSPROTO_H_
4416 struct vop_read_args {
4417 struct vnode *a_vp;
4418 struct uio *a_uio;
4419 int a_ioflag;
4420 struct ucred *a_cred;
4422 #endif
4424 static int
4425 zfs_freebsd_read(struct vop_read_args *ap)
4427 zfs_uio_t uio;
4428 zfs_uio_init(&uio, ap->a_uio);
4429 return (zfs_read(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4430 ap->a_cred));
4433 #ifndef _SYS_SYSPROTO_H_
4434 struct vop_write_args {
4435 struct vnode *a_vp;
4436 struct uio *a_uio;
4437 int a_ioflag;
4438 struct ucred *a_cred;
4440 #endif
4442 static int
4443 zfs_freebsd_write(struct vop_write_args *ap)
4445 zfs_uio_t uio;
4446 zfs_uio_init(&uio, ap->a_uio);
4447 return (zfs_write(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4448 ap->a_cred));
4451 #if __FreeBSD_version >= 1300102
4453 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
4454 * the comment above cache_fplookup for details.
4456 static int
4457 zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args *v)
4459 vnode_t *vp;
4460 znode_t *zp;
4461 uint64_t pflags;
4463 vp = v->a_vp;
4464 zp = VTOZ_SMR(vp);
4465 if (__predict_false(zp == NULL))
4466 return (EAGAIN);
4467 pflags = atomic_load_64(&zp->z_pflags);
4468 if (pflags & ZFS_AV_QUARANTINED)
4469 return (EAGAIN);
4470 if (pflags & ZFS_XATTR)
4471 return (EAGAIN);
4472 if ((pflags & ZFS_NO_EXECS_DENIED) == 0)
4473 return (EAGAIN);
4474 return (0);
4476 #endif
4478 #if __FreeBSD_version >= 1300139
4479 static int
4480 zfs_freebsd_fplookup_symlink(struct vop_fplookup_symlink_args *v)
4482 vnode_t *vp;
4483 znode_t *zp;
4484 char *target;
4486 vp = v->a_vp;
4487 zp = VTOZ_SMR(vp);
4488 if (__predict_false(zp == NULL)) {
4489 return (EAGAIN);
4492 target = atomic_load_consume_ptr(&zp->z_cached_symlink);
4493 if (target == NULL) {
4494 return (EAGAIN);
4496 return (cache_symlink_resolve(v->a_fpl, target, strlen(target)));
4498 #endif
4500 #ifndef _SYS_SYSPROTO_H_
4501 struct vop_access_args {
4502 struct vnode *a_vp;
4503 accmode_t a_accmode;
4504 struct ucred *a_cred;
4505 struct thread *a_td;
4507 #endif
4509 static int
4510 zfs_freebsd_access(struct vop_access_args *ap)
4512 vnode_t *vp = ap->a_vp;
4513 znode_t *zp = VTOZ(vp);
4514 accmode_t accmode;
4515 int error = 0;
4518 if (ap->a_accmode == VEXEC) {
4519 if (zfs_fastaccesschk_execute(zp, ap->a_cred) == 0)
4520 return (0);
4524 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4526 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4527 if (accmode != 0)
4528 error = zfs_access(zp, accmode, 0, ap->a_cred);
4531 * VADMIN has to be handled by vaccess().
4533 if (error == 0) {
4534 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4535 if (accmode != 0) {
4536 #if __FreeBSD_version >= 1300105
4537 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4538 zp->z_gid, accmode, ap->a_cred);
4539 #else
4540 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4541 zp->z_gid, accmode, ap->a_cred, NULL);
4542 #endif
4547 * For VEXEC, ensure that at least one execute bit is set for
4548 * non-directories.
4550 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
4551 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
4552 error = EACCES;
4555 return (error);
4558 #ifndef _SYS_SYSPROTO_H_
4559 struct vop_lookup_args {
4560 struct vnode *a_dvp;
4561 struct vnode **a_vpp;
4562 struct componentname *a_cnp;
4564 #endif
4566 static int
4567 zfs_freebsd_lookup(struct vop_lookup_args *ap, boolean_t cached)
4569 struct componentname *cnp = ap->a_cnp;
4570 char nm[NAME_MAX + 1];
4572 ASSERT3U(cnp->cn_namelen, <, sizeof (nm));
4573 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof (nm)));
4575 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4576 cnp->cn_cred, 0, cached));
4579 static int
4580 zfs_freebsd_cachedlookup(struct vop_cachedlookup_args *ap)
4583 return (zfs_freebsd_lookup((struct vop_lookup_args *)ap, B_TRUE));
4586 #ifndef _SYS_SYSPROTO_H_
4587 struct vop_lookup_args {
4588 struct vnode *a_dvp;
4589 struct vnode **a_vpp;
4590 struct componentname *a_cnp;
4592 #endif
4594 static int
4595 zfs_cache_lookup(struct vop_lookup_args *ap)
4597 zfsvfs_t *zfsvfs;
4599 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4600 if (zfsvfs->z_use_namecache)
4601 return (vfs_cache_lookup(ap));
4602 else
4603 return (zfs_freebsd_lookup(ap, B_FALSE));
4606 #ifndef _SYS_SYSPROTO_H_
4607 struct vop_create_args {
4608 struct vnode *a_dvp;
4609 struct vnode **a_vpp;
4610 struct componentname *a_cnp;
4611 struct vattr *a_vap;
4613 #endif
4615 static int
4616 zfs_freebsd_create(struct vop_create_args *ap)
4618 zfsvfs_t *zfsvfs;
4619 struct componentname *cnp = ap->a_cnp;
4620 vattr_t *vap = ap->a_vap;
4621 znode_t *zp = NULL;
4622 int rc, mode;
4624 ASSERT(cnp->cn_flags & SAVENAME);
4626 vattr_init_mask(vap);
4627 mode = vap->va_mode & ALLPERMS;
4628 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4629 *ap->a_vpp = NULL;
4631 rc = zfs_create(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap, !EXCL, mode,
4632 &zp, cnp->cn_cred, 0 /* flag */, NULL /* vsecattr */);
4633 if (rc == 0)
4634 *ap->a_vpp = ZTOV(zp);
4635 if (zfsvfs->z_use_namecache &&
4636 rc == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
4637 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
4639 return (rc);
4642 #ifndef _SYS_SYSPROTO_H_
4643 struct vop_remove_args {
4644 struct vnode *a_dvp;
4645 struct vnode *a_vp;
4646 struct componentname *a_cnp;
4648 #endif
4650 static int
4651 zfs_freebsd_remove(struct vop_remove_args *ap)
4654 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4656 return (zfs_remove_(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
4657 ap->a_cnp->cn_cred));
4660 #ifndef _SYS_SYSPROTO_H_
4661 struct vop_mkdir_args {
4662 struct vnode *a_dvp;
4663 struct vnode **a_vpp;
4664 struct componentname *a_cnp;
4665 struct vattr *a_vap;
4667 #endif
4669 static int
4670 zfs_freebsd_mkdir(struct vop_mkdir_args *ap)
4672 vattr_t *vap = ap->a_vap;
4673 znode_t *zp = NULL;
4674 int rc;
4676 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4678 vattr_init_mask(vap);
4679 *ap->a_vpp = NULL;
4681 rc = zfs_mkdir(VTOZ(ap->a_dvp), ap->a_cnp->cn_nameptr, vap, &zp,
4682 ap->a_cnp->cn_cred, 0, NULL);
4684 if (rc == 0)
4685 *ap->a_vpp = ZTOV(zp);
4686 return (rc);
4689 #ifndef _SYS_SYSPROTO_H_
4690 struct vop_rmdir_args {
4691 struct vnode *a_dvp;
4692 struct vnode *a_vp;
4693 struct componentname *a_cnp;
4695 #endif
4697 static int
4698 zfs_freebsd_rmdir(struct vop_rmdir_args *ap)
4700 struct componentname *cnp = ap->a_cnp;
4702 ASSERT(cnp->cn_flags & SAVENAME);
4704 return (zfs_rmdir_(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
4707 #ifndef _SYS_SYSPROTO_H_
4708 struct vop_readdir_args {
4709 struct vnode *a_vp;
4710 struct uio *a_uio;
4711 struct ucred *a_cred;
4712 int *a_eofflag;
4713 int *a_ncookies;
4714 cookie_t **a_cookies;
4716 #endif
4718 static int
4719 zfs_freebsd_readdir(struct vop_readdir_args *ap)
4721 zfs_uio_t uio;
4722 zfs_uio_init(&uio, ap->a_uio);
4723 return (zfs_readdir(ap->a_vp, &uio, ap->a_cred, ap->a_eofflag,
4724 ap->a_ncookies, ap->a_cookies));
4727 #ifndef _SYS_SYSPROTO_H_
4728 struct vop_fsync_args {
4729 struct vnode *a_vp;
4730 int a_waitfor;
4731 struct thread *a_td;
4733 #endif
4735 static int
4736 zfs_freebsd_fsync(struct vop_fsync_args *ap)
4739 vop_stdfsync(ap);
4740 return (zfs_fsync(VTOZ(ap->a_vp), 0, ap->a_td->td_ucred));
4743 #ifndef _SYS_SYSPROTO_H_
4744 struct vop_getattr_args {
4745 struct vnode *a_vp;
4746 struct vattr *a_vap;
4747 struct ucred *a_cred;
4749 #endif
4751 static int
4752 zfs_freebsd_getattr(struct vop_getattr_args *ap)
4754 vattr_t *vap = ap->a_vap;
4755 xvattr_t xvap;
4756 ulong_t fflags = 0;
4757 int error;
4759 xva_init(&xvap);
4760 xvap.xva_vattr = *vap;
4761 xvap.xva_vattr.va_mask |= AT_XVATTR;
4763 /* Convert chflags into ZFS-type flags. */
4764 /* XXX: what about SF_SETTABLE?. */
4765 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
4766 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
4767 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
4768 XVA_SET_REQ(&xvap, XAT_NODUMP);
4769 XVA_SET_REQ(&xvap, XAT_READONLY);
4770 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
4771 XVA_SET_REQ(&xvap, XAT_SYSTEM);
4772 XVA_SET_REQ(&xvap, XAT_HIDDEN);
4773 XVA_SET_REQ(&xvap, XAT_REPARSE);
4774 XVA_SET_REQ(&xvap, XAT_OFFLINE);
4775 XVA_SET_REQ(&xvap, XAT_SPARSE);
4777 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred);
4778 if (error != 0)
4779 return (error);
4781 /* Convert ZFS xattr into chflags. */
4782 #define FLAG_CHECK(fflag, xflag, xfield) do { \
4783 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
4784 fflags |= (fflag); \
4785 } while (0)
4786 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
4787 xvap.xva_xoptattrs.xoa_immutable);
4788 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
4789 xvap.xva_xoptattrs.xoa_appendonly);
4790 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
4791 xvap.xva_xoptattrs.xoa_nounlink);
4792 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
4793 xvap.xva_xoptattrs.xoa_archive);
4794 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
4795 xvap.xva_xoptattrs.xoa_nodump);
4796 FLAG_CHECK(UF_READONLY, XAT_READONLY,
4797 xvap.xva_xoptattrs.xoa_readonly);
4798 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
4799 xvap.xva_xoptattrs.xoa_system);
4800 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
4801 xvap.xva_xoptattrs.xoa_hidden);
4802 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
4803 xvap.xva_xoptattrs.xoa_reparse);
4804 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
4805 xvap.xva_xoptattrs.xoa_offline);
4806 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
4807 xvap.xva_xoptattrs.xoa_sparse);
4809 #undef FLAG_CHECK
4810 *vap = xvap.xva_vattr;
4811 vap->va_flags = fflags;
4812 return (0);
4815 #ifndef _SYS_SYSPROTO_H_
4816 struct vop_setattr_args {
4817 struct vnode *a_vp;
4818 struct vattr *a_vap;
4819 struct ucred *a_cred;
4821 #endif
4823 static int
4824 zfs_freebsd_setattr(struct vop_setattr_args *ap)
4826 vnode_t *vp = ap->a_vp;
4827 vattr_t *vap = ap->a_vap;
4828 cred_t *cred = ap->a_cred;
4829 xvattr_t xvap;
4830 ulong_t fflags;
4831 uint64_t zflags;
4833 vattr_init_mask(vap);
4834 vap->va_mask &= ~AT_NOSET;
4836 xva_init(&xvap);
4837 xvap.xva_vattr = *vap;
4839 zflags = VTOZ(vp)->z_pflags;
4841 if (vap->va_flags != VNOVAL) {
4842 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
4843 int error;
4845 if (zfsvfs->z_use_fuids == B_FALSE)
4846 return (EOPNOTSUPP);
4848 fflags = vap->va_flags;
4850 * XXX KDM
4851 * We need to figure out whether it makes sense to allow
4852 * UF_REPARSE through, since we don't really have other
4853 * facilities to handle reparse points and zfs_setattr()
4854 * doesn't currently allow setting that attribute anyway.
4856 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
4857 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
4858 UF_OFFLINE|UF_SPARSE)) != 0)
4859 return (EOPNOTSUPP);
4861 * Unprivileged processes are not permitted to unset system
4862 * flags, or modify flags if any system flags are set.
4863 * Privileged non-jail processes may not modify system flags
4864 * if securelevel > 0 and any existing system flags are set.
4865 * Privileged jail processes behave like privileged non-jail
4866 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
4867 * otherwise, they behave like unprivileged processes.
4869 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
4870 spl_priv_check_cred(cred, PRIV_VFS_SYSFLAGS) == 0) {
4871 if (zflags &
4872 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4873 error = securelevel_gt(cred, 0);
4874 if (error != 0)
4875 return (error);
4877 } else {
4879 * Callers may only modify the file flags on
4880 * objects they have VADMIN rights for.
4882 if ((error = VOP_ACCESS(vp, VADMIN, cred,
4883 curthread)) != 0)
4884 return (error);
4885 if (zflags &
4886 (ZFS_IMMUTABLE | ZFS_APPENDONLY |
4887 ZFS_NOUNLINK)) {
4888 return (EPERM);
4890 if (fflags &
4891 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
4892 return (EPERM);
4896 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
4897 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
4898 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
4899 XVA_SET_REQ(&xvap, (xflag)); \
4900 (xfield) = ((fflags & (fflag)) != 0); \
4902 } while (0)
4903 /* Convert chflags into ZFS-type flags. */
4904 /* XXX: what about SF_SETTABLE?. */
4905 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
4906 xvap.xva_xoptattrs.xoa_immutable);
4907 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
4908 xvap.xva_xoptattrs.xoa_appendonly);
4909 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
4910 xvap.xva_xoptattrs.xoa_nounlink);
4911 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
4912 xvap.xva_xoptattrs.xoa_archive);
4913 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
4914 xvap.xva_xoptattrs.xoa_nodump);
4915 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
4916 xvap.xva_xoptattrs.xoa_readonly);
4917 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
4918 xvap.xva_xoptattrs.xoa_system);
4919 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
4920 xvap.xva_xoptattrs.xoa_hidden);
4921 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
4922 xvap.xva_xoptattrs.xoa_reparse);
4923 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
4924 xvap.xva_xoptattrs.xoa_offline);
4925 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
4926 xvap.xva_xoptattrs.xoa_sparse);
4927 #undef FLAG_CHANGE
4929 if (vap->va_birthtime.tv_sec != VNOVAL) {
4930 xvap.xva_vattr.va_mask |= AT_XVATTR;
4931 XVA_SET_REQ(&xvap, XAT_CREATETIME);
4933 return (zfs_setattr(VTOZ(vp), (vattr_t *)&xvap, 0, cred));
4936 #ifndef _SYS_SYSPROTO_H_
4937 struct vop_rename_args {
4938 struct vnode *a_fdvp;
4939 struct vnode *a_fvp;
4940 struct componentname *a_fcnp;
4941 struct vnode *a_tdvp;
4942 struct vnode *a_tvp;
4943 struct componentname *a_tcnp;
4945 #endif
4947 static int
4948 zfs_freebsd_rename(struct vop_rename_args *ap)
4950 vnode_t *fdvp = ap->a_fdvp;
4951 vnode_t *fvp = ap->a_fvp;
4952 vnode_t *tdvp = ap->a_tdvp;
4953 vnode_t *tvp = ap->a_tvp;
4954 int error;
4956 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
4957 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
4959 error = zfs_do_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
4960 ap->a_tcnp, ap->a_fcnp->cn_cred);
4962 vrele(fdvp);
4963 vrele(fvp);
4964 vrele(tdvp);
4965 if (tvp != NULL)
4966 vrele(tvp);
4968 return (error);
4971 #ifndef _SYS_SYSPROTO_H_
4972 struct vop_symlink_args {
4973 struct vnode *a_dvp;
4974 struct vnode **a_vpp;
4975 struct componentname *a_cnp;
4976 struct vattr *a_vap;
4977 char *a_target;
4979 #endif
4981 static int
4982 zfs_freebsd_symlink(struct vop_symlink_args *ap)
4984 struct componentname *cnp = ap->a_cnp;
4985 vattr_t *vap = ap->a_vap;
4986 znode_t *zp = NULL;
4987 #if __FreeBSD_version >= 1300139
4988 char *symlink;
4989 size_t symlink_len;
4990 #endif
4991 int rc;
4993 ASSERT(cnp->cn_flags & SAVENAME);
4995 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
4996 vattr_init_mask(vap);
4997 *ap->a_vpp = NULL;
4999 rc = zfs_symlink(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap,
5000 ap->a_target, &zp, cnp->cn_cred, 0 /* flags */);
5001 if (rc == 0) {
5002 *ap->a_vpp = ZTOV(zp);
5003 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
5004 #if __FreeBSD_version >= 1300139
5005 MPASS(zp->z_cached_symlink == NULL);
5006 symlink_len = strlen(ap->a_target);
5007 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
5008 if (symlink != NULL) {
5009 memcpy(symlink, ap->a_target, symlink_len);
5010 symlink[symlink_len] = '\0';
5011 atomic_store_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5012 (uintptr_t)symlink);
5014 #endif
5016 return (rc);
5019 #ifndef _SYS_SYSPROTO_H_
5020 struct vop_readlink_args {
5021 struct vnode *a_vp;
5022 struct uio *a_uio;
5023 struct ucred *a_cred;
5025 #endif
5027 static int
5028 zfs_freebsd_readlink(struct vop_readlink_args *ap)
5030 zfs_uio_t uio;
5031 int error;
5032 #if __FreeBSD_version >= 1300139
5033 znode_t *zp = VTOZ(ap->a_vp);
5034 char *symlink, *base;
5035 size_t symlink_len;
5036 bool trycache;
5037 #endif
5039 zfs_uio_init(&uio, ap->a_uio);
5040 #if __FreeBSD_version >= 1300139
5041 trycache = false;
5042 if (zfs_uio_segflg(&uio) == UIO_SYSSPACE &&
5043 zfs_uio_iovcnt(&uio) == 1) {
5044 base = zfs_uio_iovbase(&uio, 0);
5045 symlink_len = zfs_uio_iovlen(&uio, 0);
5046 trycache = true;
5048 #endif
5049 error = zfs_readlink(ap->a_vp, &uio, ap->a_cred, NULL);
5050 #if __FreeBSD_version >= 1300139
5051 if (atomic_load_ptr(&zp->z_cached_symlink) != NULL ||
5052 error != 0 || !trycache) {
5053 return (error);
5055 symlink_len -= zfs_uio_resid(&uio);
5056 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
5057 if (symlink != NULL) {
5058 memcpy(symlink, base, symlink_len);
5059 symlink[symlink_len] = '\0';
5060 if (!atomic_cmpset_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5061 (uintptr_t)NULL, (uintptr_t)symlink)) {
5062 cache_symlink_free(symlink, symlink_len + 1);
5065 #endif
5066 return (error);
5069 #ifndef _SYS_SYSPROTO_H_
5070 struct vop_link_args {
5071 struct vnode *a_tdvp;
5072 struct vnode *a_vp;
5073 struct componentname *a_cnp;
5075 #endif
5077 static int
5078 zfs_freebsd_link(struct vop_link_args *ap)
5080 struct componentname *cnp = ap->a_cnp;
5081 vnode_t *vp = ap->a_vp;
5082 vnode_t *tdvp = ap->a_tdvp;
5084 if (tdvp->v_mount != vp->v_mount)
5085 return (EXDEV);
5087 ASSERT(cnp->cn_flags & SAVENAME);
5089 return (zfs_link(VTOZ(tdvp), VTOZ(vp),
5090 cnp->cn_nameptr, cnp->cn_cred, 0));
5093 #ifndef _SYS_SYSPROTO_H_
5094 struct vop_inactive_args {
5095 struct vnode *a_vp;
5096 struct thread *a_td;
5098 #endif
5100 static int
5101 zfs_freebsd_inactive(struct vop_inactive_args *ap)
5103 vnode_t *vp = ap->a_vp;
5105 #if __FreeBSD_version >= 1300123
5106 zfs_inactive(vp, curthread->td_ucred, NULL);
5107 #else
5108 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5109 #endif
5110 return (0);
5113 #if __FreeBSD_version >= 1300042
5114 #ifndef _SYS_SYSPROTO_H_
5115 struct vop_need_inactive_args {
5116 struct vnode *a_vp;
5117 struct thread *a_td;
5119 #endif
5121 static int
5122 zfs_freebsd_need_inactive(struct vop_need_inactive_args *ap)
5124 vnode_t *vp = ap->a_vp;
5125 znode_t *zp = VTOZ(vp);
5126 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5127 int need;
5129 if (vn_need_pageq_flush(vp))
5130 return (1);
5132 if (!ZFS_TEARDOWN_INACTIVE_TRY_ENTER_READ(zfsvfs))
5133 return (1);
5134 need = (zp->z_sa_hdl == NULL || zp->z_unlinked || zp->z_atime_dirty);
5135 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5137 return (need);
5139 #endif
5141 #ifndef _SYS_SYSPROTO_H_
5142 struct vop_reclaim_args {
5143 struct vnode *a_vp;
5144 struct thread *a_td;
5146 #endif
5148 static int
5149 zfs_freebsd_reclaim(struct vop_reclaim_args *ap)
5151 vnode_t *vp = ap->a_vp;
5152 znode_t *zp = VTOZ(vp);
5153 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5155 ASSERT3P(zp, !=, NULL);
5157 #if __FreeBSD_version < 1300042
5158 /* Destroy the vm object and flush associated pages. */
5159 vnode_destroy_vobject(vp);
5160 #endif
5162 * z_teardown_inactive_lock protects from a race with
5163 * zfs_znode_dmu_fini in zfsvfs_teardown during
5164 * force unmount.
5166 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
5167 if (zp->z_sa_hdl == NULL)
5168 zfs_znode_free(zp);
5169 else
5170 zfs_zinactive(zp);
5171 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5173 vp->v_data = NULL;
5174 return (0);
5177 #ifndef _SYS_SYSPROTO_H_
5178 struct vop_fid_args {
5179 struct vnode *a_vp;
5180 struct fid *a_fid;
5182 #endif
5184 static int
5185 zfs_freebsd_fid(struct vop_fid_args *ap)
5188 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5192 #ifndef _SYS_SYSPROTO_H_
5193 struct vop_pathconf_args {
5194 struct vnode *a_vp;
5195 int a_name;
5196 register_t *a_retval;
5197 } *ap;
5198 #endif
5200 static int
5201 zfs_freebsd_pathconf(struct vop_pathconf_args *ap)
5203 ulong_t val;
5204 int error;
5206 error = zfs_pathconf(ap->a_vp, ap->a_name, &val,
5207 curthread->td_ucred, NULL);
5208 if (error == 0) {
5209 *ap->a_retval = val;
5210 return (error);
5212 if (error != EOPNOTSUPP)
5213 return (error);
5215 switch (ap->a_name) {
5216 case _PC_NAME_MAX:
5217 *ap->a_retval = NAME_MAX;
5218 return (0);
5219 #if __FreeBSD_version >= 1400032
5220 case _PC_DEALLOC_PRESENT:
5221 *ap->a_retval = 1;
5222 return (0);
5223 #endif
5224 case _PC_PIPE_BUF:
5225 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO) {
5226 *ap->a_retval = PIPE_BUF;
5227 return (0);
5229 return (EINVAL);
5230 default:
5231 return (vop_stdpathconf(ap));
5235 static int zfs_xattr_compat = 1;
5237 static int
5238 zfs_check_attrname(const char *name)
5240 /* We don't allow '/' character in attribute name. */
5241 if (strchr(name, '/') != NULL)
5242 return (SET_ERROR(EINVAL));
5243 /* We don't allow attribute names that start with a namespace prefix. */
5244 if (ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5245 return (SET_ERROR(EINVAL));
5246 return (0);
5250 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5251 * extended attribute name:
5253 * NAMESPACE XATTR_COMPAT PREFIX
5254 * system * freebsd:system:
5255 * user 1 (none, can be used to access ZFS
5256 * fsattr(5) attributes created on Solaris)
5257 * user 0 user.
5259 static int
5260 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5261 size_t size, boolean_t compat)
5263 const char *namespace, *prefix, *suffix;
5265 memset(attrname, 0, size);
5267 switch (attrnamespace) {
5268 case EXTATTR_NAMESPACE_USER:
5269 if (compat) {
5271 * This is the default namespace by which we can access
5272 * all attributes created on Solaris.
5274 prefix = namespace = suffix = "";
5275 } else {
5277 * This is compatible with the user namespace encoding
5278 * on Linux prior to xattr_compat, but nothing
5279 * else.
5281 prefix = "";
5282 namespace = "user";
5283 suffix = ".";
5285 break;
5286 case EXTATTR_NAMESPACE_SYSTEM:
5287 prefix = "freebsd:";
5288 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5289 suffix = ":";
5290 break;
5291 case EXTATTR_NAMESPACE_EMPTY:
5292 default:
5293 return (SET_ERROR(EINVAL));
5295 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5296 name) >= size) {
5297 return (SET_ERROR(ENAMETOOLONG));
5299 return (0);
5302 static int
5303 zfs_ensure_xattr_cached(znode_t *zp)
5305 int error = 0;
5307 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5309 if (zp->z_xattr_cached != NULL)
5310 return (0);
5312 if (rw_write_held(&zp->z_xattr_lock))
5313 return (zfs_sa_get_xattr(zp));
5315 if (!rw_tryupgrade(&zp->z_xattr_lock)) {
5316 rw_exit(&zp->z_xattr_lock);
5317 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5319 if (zp->z_xattr_cached == NULL)
5320 error = zfs_sa_get_xattr(zp);
5321 rw_downgrade(&zp->z_xattr_lock);
5322 return (error);
5325 #ifndef _SYS_SYSPROTO_H_
5326 struct vop_getextattr {
5327 IN struct vnode *a_vp;
5328 IN int a_attrnamespace;
5329 IN const char *a_name;
5330 INOUT struct uio *a_uio;
5331 OUT size_t *a_size;
5332 IN struct ucred *a_cred;
5333 IN struct thread *a_td;
5335 #endif
5337 static int
5338 zfs_getextattr_dir(struct vop_getextattr_args *ap, const char *attrname)
5340 struct thread *td = ap->a_td;
5341 struct nameidata nd;
5342 struct vattr va;
5343 vnode_t *xvp = NULL, *vp;
5344 int error, flags;
5346 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5347 LOOKUP_XATTR, B_FALSE);
5348 if (error != 0)
5349 return (error);
5351 flags = FREAD;
5352 #if __FreeBSD_version < 1400043
5353 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5354 xvp, td);
5355 #else
5356 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5357 #endif
5358 error = vn_open_cred(&nd, &flags, 0, VN_OPEN_INVFS, ap->a_cred, NULL);
5359 vp = nd.ni_vp;
5360 NDFREE_PNBUF(&nd);
5361 if (error != 0)
5362 return (SET_ERROR(error));
5364 if (ap->a_size != NULL) {
5365 error = VOP_GETATTR(vp, &va, ap->a_cred);
5366 if (error == 0)
5367 *ap->a_size = (size_t)va.va_size;
5368 } else if (ap->a_uio != NULL)
5369 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5371 VOP_UNLOCK1(vp);
5372 vn_close(vp, flags, ap->a_cred, td);
5373 return (error);
5376 static int
5377 zfs_getextattr_sa(struct vop_getextattr_args *ap, const char *attrname)
5379 znode_t *zp = VTOZ(ap->a_vp);
5380 uchar_t *nv_value;
5381 uint_t nv_size;
5382 int error;
5384 error = zfs_ensure_xattr_cached(zp);
5385 if (error != 0)
5386 return (error);
5388 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5389 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5391 error = nvlist_lookup_byte_array(zp->z_xattr_cached, attrname,
5392 &nv_value, &nv_size);
5393 if (error != 0)
5394 return (SET_ERROR(error));
5396 if (ap->a_size != NULL)
5397 *ap->a_size = nv_size;
5398 else if (ap->a_uio != NULL)
5399 error = uiomove(nv_value, nv_size, ap->a_uio);
5400 if (error != 0)
5401 return (SET_ERROR(error));
5403 return (0);
5406 static int
5407 zfs_getextattr_impl(struct vop_getextattr_args *ap, boolean_t compat)
5409 znode_t *zp = VTOZ(ap->a_vp);
5410 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5411 char attrname[EXTATTR_MAXNAMELEN+1];
5412 int error;
5414 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5415 sizeof (attrname), compat);
5416 if (error != 0)
5417 return (error);
5419 error = ENOENT;
5420 if (zfsvfs->z_use_sa && zp->z_is_sa)
5421 error = zfs_getextattr_sa(ap, attrname);
5422 if (error == ENOENT)
5423 error = zfs_getextattr_dir(ap, attrname);
5424 return (error);
5428 * Vnode operation to retrieve a named extended attribute.
5430 static int
5431 zfs_getextattr(struct vop_getextattr_args *ap)
5433 znode_t *zp = VTOZ(ap->a_vp);
5434 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5435 int error;
5438 * If the xattr property is off, refuse the request.
5440 if (!(zfsvfs->z_flags & ZSB_XATTR))
5441 return (SET_ERROR(EOPNOTSUPP));
5443 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5444 ap->a_cred, ap->a_td, VREAD);
5445 if (error != 0)
5446 return (SET_ERROR(error));
5448 error = zfs_check_attrname(ap->a_name);
5449 if (error != 0)
5450 return (error);
5452 error = ENOENT;
5453 ZFS_ENTER(zfsvfs);
5454 ZFS_VERIFY_ZP(zp);
5455 rw_enter(&zp->z_xattr_lock, RW_READER);
5457 error = zfs_getextattr_impl(ap, zfs_xattr_compat);
5458 if ((error == ENOENT || error == ENOATTR) &&
5459 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5461 * Fall back to the alternate namespace format if we failed to
5462 * find a user xattr.
5464 error = zfs_getextattr_impl(ap, !zfs_xattr_compat);
5467 rw_exit(&zp->z_xattr_lock);
5468 ZFS_EXIT(zfsvfs);
5469 if (error == ENOENT)
5470 error = SET_ERROR(ENOATTR);
5471 return (error);
5474 #ifndef _SYS_SYSPROTO_H_
5475 struct vop_deleteextattr {
5476 IN struct vnode *a_vp;
5477 IN int a_attrnamespace;
5478 IN const char *a_name;
5479 IN struct ucred *a_cred;
5480 IN struct thread *a_td;
5482 #endif
5484 static int
5485 zfs_deleteextattr_dir(struct vop_deleteextattr_args *ap, const char *attrname)
5487 struct nameidata nd;
5488 vnode_t *xvp = NULL, *vp;
5489 int error;
5491 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5492 LOOKUP_XATTR, B_FALSE);
5493 if (error != 0)
5494 return (error);
5496 #if __FreeBSD_version < 1400043
5497 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5498 UIO_SYSSPACE, attrname, xvp, ap->a_td);
5499 #else
5500 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5501 UIO_SYSSPACE, attrname, xvp);
5502 #endif
5503 error = namei(&nd);
5504 vp = nd.ni_vp;
5505 if (error != 0) {
5506 NDFREE_PNBUF(&nd);
5507 return (SET_ERROR(error));
5510 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
5511 NDFREE_PNBUF(&nd);
5513 vput(nd.ni_dvp);
5514 if (vp == nd.ni_dvp)
5515 vrele(vp);
5516 else
5517 vput(vp);
5519 return (error);
5522 static int
5523 zfs_deleteextattr_sa(struct vop_deleteextattr_args *ap, const char *attrname)
5525 znode_t *zp = VTOZ(ap->a_vp);
5526 nvlist_t *nvl;
5527 int error;
5529 error = zfs_ensure_xattr_cached(zp);
5530 if (error != 0)
5531 return (error);
5533 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5534 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5536 nvl = zp->z_xattr_cached;
5537 error = nvlist_remove(nvl, attrname, DATA_TYPE_BYTE_ARRAY);
5538 if (error != 0)
5539 error = SET_ERROR(error);
5540 else
5541 error = zfs_sa_set_xattr(zp, attrname, NULL, 0);
5542 if (error != 0) {
5543 zp->z_xattr_cached = NULL;
5544 nvlist_free(nvl);
5546 return (error);
5549 static int
5550 zfs_deleteextattr_impl(struct vop_deleteextattr_args *ap, boolean_t compat)
5552 znode_t *zp = VTOZ(ap->a_vp);
5553 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5554 char attrname[EXTATTR_MAXNAMELEN+1];
5555 int error;
5557 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5558 sizeof (attrname), compat);
5559 if (error != 0)
5560 return (error);
5562 error = ENOENT;
5563 if (zfsvfs->z_use_sa && zp->z_is_sa)
5564 error = zfs_deleteextattr_sa(ap, attrname);
5565 if (error == ENOENT)
5566 error = zfs_deleteextattr_dir(ap, attrname);
5567 return (error);
5571 * Vnode operation to remove a named attribute.
5573 static int
5574 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
5576 znode_t *zp = VTOZ(ap->a_vp);
5577 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5578 int error;
5581 * If the xattr property is off, refuse the request.
5583 if (!(zfsvfs->z_flags & ZSB_XATTR))
5584 return (SET_ERROR(EOPNOTSUPP));
5586 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5587 ap->a_cred, ap->a_td, VWRITE);
5588 if (error != 0)
5589 return (SET_ERROR(error));
5591 error = zfs_check_attrname(ap->a_name);
5592 if (error != 0)
5593 return (error);
5595 ZFS_ENTER(zfsvfs);
5596 ZFS_VERIFY_ZP(zp);
5597 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5599 error = zfs_deleteextattr_impl(ap, zfs_xattr_compat);
5600 if ((error == ENOENT || error == ENOATTR) &&
5601 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5603 * Fall back to the alternate namespace format if we failed to
5604 * find a user xattr.
5606 error = zfs_deleteextattr_impl(ap, !zfs_xattr_compat);
5609 rw_exit(&zp->z_xattr_lock);
5610 ZFS_EXIT(zfsvfs);
5611 if (error == ENOENT)
5612 error = SET_ERROR(ENOATTR);
5613 return (error);
5616 #ifndef _SYS_SYSPROTO_H_
5617 struct vop_setextattr {
5618 IN struct vnode *a_vp;
5619 IN int a_attrnamespace;
5620 IN const char *a_name;
5621 INOUT struct uio *a_uio;
5622 IN struct ucred *a_cred;
5623 IN struct thread *a_td;
5625 #endif
5627 static int
5628 zfs_setextattr_dir(struct vop_setextattr_args *ap, const char *attrname)
5630 struct thread *td = ap->a_td;
5631 struct nameidata nd;
5632 struct vattr va;
5633 vnode_t *xvp = NULL, *vp;
5634 int error, flags;
5636 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5637 LOOKUP_XATTR | CREATE_XATTR_DIR, B_FALSE);
5638 if (error != 0)
5639 return (error);
5641 flags = FFLAGS(O_WRONLY | O_CREAT);
5642 #if __FreeBSD_version < 1400043
5643 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp, td);
5644 #else
5645 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5646 #endif
5647 error = vn_open_cred(&nd, &flags, 0600, VN_OPEN_INVFS, ap->a_cred,
5648 NULL);
5649 vp = nd.ni_vp;
5650 NDFREE_PNBUF(&nd);
5651 if (error != 0)
5652 return (SET_ERROR(error));
5654 VATTR_NULL(&va);
5655 va.va_size = 0;
5656 error = VOP_SETATTR(vp, &va, ap->a_cred);
5657 if (error == 0)
5658 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5660 VOP_UNLOCK1(vp);
5661 vn_close(vp, flags, ap->a_cred, td);
5662 return (error);
5665 static int
5666 zfs_setextattr_sa(struct vop_setextattr_args *ap, const char *attrname)
5668 znode_t *zp = VTOZ(ap->a_vp);
5669 nvlist_t *nvl;
5670 size_t sa_size;
5671 int error;
5673 error = zfs_ensure_xattr_cached(zp);
5674 if (error != 0)
5675 return (error);
5677 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5678 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5680 nvl = zp->z_xattr_cached;
5681 size_t entry_size = ap->a_uio->uio_resid;
5682 if (entry_size > DXATTR_MAX_ENTRY_SIZE)
5683 return (SET_ERROR(EFBIG));
5684 error = nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
5685 if (error != 0)
5686 return (SET_ERROR(error));
5687 if (sa_size > DXATTR_MAX_SA_SIZE)
5688 return (SET_ERROR(EFBIG));
5689 uchar_t *buf = kmem_alloc(entry_size, KM_SLEEP);
5690 error = uiomove(buf, entry_size, ap->a_uio);
5691 if (error != 0) {
5692 error = SET_ERROR(error);
5693 } else {
5694 error = nvlist_add_byte_array(nvl, attrname, buf, entry_size);
5695 if (error != 0)
5696 error = SET_ERROR(error);
5698 if (error == 0)
5699 error = zfs_sa_set_xattr(zp, attrname, buf, entry_size);
5700 kmem_free(buf, entry_size);
5701 if (error != 0) {
5702 zp->z_xattr_cached = NULL;
5703 nvlist_free(nvl);
5705 return (error);
5708 static int
5709 zfs_setextattr_impl(struct vop_setextattr_args *ap, boolean_t compat)
5711 znode_t *zp = VTOZ(ap->a_vp);
5712 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5713 char attrname[EXTATTR_MAXNAMELEN+1];
5714 int error;
5716 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5717 sizeof (attrname), compat);
5718 if (error != 0)
5719 return (error);
5721 struct vop_deleteextattr_args vda = {
5722 .a_vp = ap->a_vp,
5723 .a_attrnamespace = ap->a_attrnamespace,
5724 .a_name = ap->a_name,
5725 .a_cred = ap->a_cred,
5726 .a_td = ap->a_td,
5728 error = ENOENT;
5729 if (zfsvfs->z_use_sa && zp->z_is_sa && zfsvfs->z_xattr_sa) {
5730 error = zfs_setextattr_sa(ap, attrname);
5731 if (error == 0) {
5733 * Successfully put into SA, we need to clear the one
5734 * in dir if present.
5736 zfs_deleteextattr_dir(&vda, attrname);
5739 if (error != 0) {
5740 error = zfs_setextattr_dir(ap, attrname);
5741 if (error == 0 && zp->z_is_sa) {
5743 * Successfully put into dir, we need to clear the one
5744 * in SA if present.
5746 zfs_deleteextattr_sa(&vda, attrname);
5749 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5751 * Also clear all versions of the alternate compat name.
5753 zfs_deleteextattr_impl(&vda, !compat);
5755 return (error);
5759 * Vnode operation to set a named attribute.
5761 static int
5762 zfs_setextattr(struct vop_setextattr_args *ap)
5764 znode_t *zp = VTOZ(ap->a_vp);
5765 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5766 int error;
5769 * If the xattr property is off, refuse the request.
5771 if (!(zfsvfs->z_flags & ZSB_XATTR))
5772 return (SET_ERROR(EOPNOTSUPP));
5774 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5775 ap->a_cred, ap->a_td, VWRITE);
5776 if (error != 0)
5777 return (SET_ERROR(error));
5779 error = zfs_check_attrname(ap->a_name);
5780 if (error != 0)
5781 return (error);
5783 ZFS_ENTER(zfsvfs);
5784 ZFS_VERIFY_ZP(zp);
5785 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5787 error = zfs_setextattr_impl(ap, zfs_xattr_compat);
5789 rw_exit(&zp->z_xattr_lock);
5790 ZFS_EXIT(zfsvfs);
5791 return (error);
5794 #ifndef _SYS_SYSPROTO_H_
5795 struct vop_listextattr {
5796 IN struct vnode *a_vp;
5797 IN int a_attrnamespace;
5798 INOUT struct uio *a_uio;
5799 OUT size_t *a_size;
5800 IN struct ucred *a_cred;
5801 IN struct thread *a_td;
5803 #endif
5805 static int
5806 zfs_listextattr_dir(struct vop_listextattr_args *ap, const char *attrprefix)
5808 struct thread *td = ap->a_td;
5809 struct nameidata nd;
5810 uint8_t dirbuf[sizeof (struct dirent)];
5811 struct iovec aiov;
5812 struct uio auio;
5813 vnode_t *xvp = NULL, *vp;
5814 int error, eof;
5816 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5817 LOOKUP_XATTR, B_FALSE);
5818 if (error != 0) {
5820 * ENOATTR means that the EA directory does not yet exist,
5821 * i.e. there are no extended attributes there.
5823 if (error == ENOATTR)
5824 error = 0;
5825 return (error);
5828 #if __FreeBSD_version < 1400043
5829 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5830 UIO_SYSSPACE, ".", xvp, td);
5831 #else
5832 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5833 UIO_SYSSPACE, ".", xvp);
5834 #endif
5835 error = namei(&nd);
5836 vp = nd.ni_vp;
5837 NDFREE_PNBUF(&nd);
5838 if (error != 0)
5839 return (SET_ERROR(error));
5841 auio.uio_iov = &aiov;
5842 auio.uio_iovcnt = 1;
5843 auio.uio_segflg = UIO_SYSSPACE;
5844 auio.uio_td = td;
5845 auio.uio_rw = UIO_READ;
5846 auio.uio_offset = 0;
5848 size_t plen = strlen(attrprefix);
5850 do {
5851 aiov.iov_base = (void *)dirbuf;
5852 aiov.iov_len = sizeof (dirbuf);
5853 auio.uio_resid = sizeof (dirbuf);
5854 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5855 if (error != 0)
5856 break;
5857 int done = sizeof (dirbuf) - auio.uio_resid;
5858 for (int pos = 0; pos < done; ) {
5859 struct dirent *dp = (struct dirent *)(dirbuf + pos);
5860 pos += dp->d_reclen;
5862 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5863 * is what we get when attribute was created on Solaris.
5865 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5866 continue;
5867 else if (plen == 0 &&
5868 ZFS_XA_NS_PREFIX_FORBIDDEN(dp->d_name))
5869 continue;
5870 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5871 continue;
5872 uint8_t nlen = dp->d_namlen - plen;
5873 if (ap->a_size != NULL) {
5874 *ap->a_size += 1 + nlen;
5875 } else if (ap->a_uio != NULL) {
5877 * Format of extattr name entry is one byte for
5878 * length and the rest for name.
5880 error = uiomove(&nlen, 1, ap->a_uio);
5881 if (error == 0) {
5882 char *namep = dp->d_name + plen;
5883 error = uiomove(namep, nlen, ap->a_uio);
5885 if (error != 0) {
5886 error = SET_ERROR(error);
5887 break;
5891 } while (!eof && error == 0);
5893 vput(vp);
5894 return (error);
5897 static int
5898 zfs_listextattr_sa(struct vop_listextattr_args *ap, const char *attrprefix)
5900 znode_t *zp = VTOZ(ap->a_vp);
5901 int error;
5903 error = zfs_ensure_xattr_cached(zp);
5904 if (error != 0)
5905 return (error);
5907 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5908 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5910 size_t plen = strlen(attrprefix);
5911 nvpair_t *nvp = NULL;
5912 while ((nvp = nvlist_next_nvpair(zp->z_xattr_cached, nvp)) != NULL) {
5913 ASSERT3U(nvpair_type(nvp), ==, DATA_TYPE_BYTE_ARRAY);
5915 const char *name = nvpair_name(nvp);
5916 if (plen == 0 && ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5917 continue;
5918 else if (strncmp(name, attrprefix, plen) != 0)
5919 continue;
5920 uint8_t nlen = strlen(name) - plen;
5921 if (ap->a_size != NULL) {
5922 *ap->a_size += 1 + nlen;
5923 } else if (ap->a_uio != NULL) {
5925 * Format of extattr name entry is one byte for
5926 * length and the rest for name.
5928 error = uiomove(&nlen, 1, ap->a_uio);
5929 if (error == 0) {
5930 char *namep = __DECONST(char *, name) + plen;
5931 error = uiomove(namep, nlen, ap->a_uio);
5933 if (error != 0) {
5934 error = SET_ERROR(error);
5935 break;
5940 return (error);
5943 static int
5944 zfs_listextattr_impl(struct vop_listextattr_args *ap, boolean_t compat)
5946 znode_t *zp = VTOZ(ap->a_vp);
5947 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5948 char attrprefix[16];
5949 int error;
5951 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5952 sizeof (attrprefix), compat);
5953 if (error != 0)
5954 return (error);
5956 if (zfsvfs->z_use_sa && zp->z_is_sa)
5957 error = zfs_listextattr_sa(ap, attrprefix);
5958 if (error == 0)
5959 error = zfs_listextattr_dir(ap, attrprefix);
5960 return (error);
5964 * Vnode operation to retrieve extended attributes on a vnode.
5966 static int
5967 zfs_listextattr(struct vop_listextattr_args *ap)
5969 znode_t *zp = VTOZ(ap->a_vp);
5970 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5971 int error;
5973 if (ap->a_size != NULL)
5974 *ap->a_size = 0;
5977 * If the xattr property is off, refuse the request.
5979 if (!(zfsvfs->z_flags & ZSB_XATTR))
5980 return (SET_ERROR(EOPNOTSUPP));
5982 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5983 ap->a_cred, ap->a_td, VREAD);
5984 if (error != 0)
5985 return (SET_ERROR(error));
5987 ZFS_ENTER(zfsvfs);
5988 ZFS_VERIFY_ZP(zp);
5989 rw_enter(&zp->z_xattr_lock, RW_READER);
5991 error = zfs_listextattr_impl(ap, zfs_xattr_compat);
5992 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5993 /* Also list user xattrs with the alternate format. */
5994 error = zfs_listextattr_impl(ap, !zfs_xattr_compat);
5997 rw_exit(&zp->z_xattr_lock);
5998 ZFS_EXIT(zfsvfs);
5999 return (error);
6002 #ifndef _SYS_SYSPROTO_H_
6003 struct vop_getacl_args {
6004 struct vnode *vp;
6005 acl_type_t type;
6006 struct acl *aclp;
6007 struct ucred *cred;
6008 struct thread *td;
6010 #endif
6012 static int
6013 zfs_freebsd_getacl(struct vop_getacl_args *ap)
6015 int error;
6016 vsecattr_t vsecattr;
6018 if (ap->a_type != ACL_TYPE_NFS4)
6019 return (EINVAL);
6021 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
6022 if ((error = zfs_getsecattr(VTOZ(ap->a_vp),
6023 &vsecattr, 0, ap->a_cred)))
6024 return (error);
6026 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp,
6027 vsecattr.vsa_aclcnt);
6028 if (vsecattr.vsa_aclentp != NULL)
6029 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
6031 return (error);
6034 #ifndef _SYS_SYSPROTO_H_
6035 struct vop_setacl_args {
6036 struct vnode *vp;
6037 acl_type_t type;
6038 struct acl *aclp;
6039 struct ucred *cred;
6040 struct thread *td;
6042 #endif
6044 static int
6045 zfs_freebsd_setacl(struct vop_setacl_args *ap)
6047 int error;
6048 vsecattr_t vsecattr;
6049 int aclbsize; /* size of acl list in bytes */
6050 aclent_t *aaclp;
6052 if (ap->a_type != ACL_TYPE_NFS4)
6053 return (EINVAL);
6055 if (ap->a_aclp == NULL)
6056 return (EINVAL);
6058 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
6059 return (EINVAL);
6062 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
6063 * splitting every entry into two and appending "canonical six"
6064 * entries at the end. Don't allow for setting an ACL that would
6065 * cause chmod(2) to run out of ACL entries.
6067 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
6068 return (ENOSPC);
6070 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
6071 if (error != 0)
6072 return (error);
6074 vsecattr.vsa_mask = VSA_ACE;
6075 aclbsize = ap->a_aclp->acl_cnt * sizeof (ace_t);
6076 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
6077 aaclp = vsecattr.vsa_aclentp;
6078 vsecattr.vsa_aclentsz = aclbsize;
6080 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
6081 error = zfs_setsecattr(VTOZ(ap->a_vp), &vsecattr, 0, ap->a_cred);
6082 kmem_free(aaclp, aclbsize);
6084 return (error);
6087 #ifndef _SYS_SYSPROTO_H_
6088 struct vop_aclcheck_args {
6089 struct vnode *vp;
6090 acl_type_t type;
6091 struct acl *aclp;
6092 struct ucred *cred;
6093 struct thread *td;
6095 #endif
6097 static int
6098 zfs_freebsd_aclcheck(struct vop_aclcheck_args *ap)
6101 return (EOPNOTSUPP);
6104 static int
6105 zfs_vptocnp(struct vop_vptocnp_args *ap)
6107 vnode_t *covered_vp;
6108 vnode_t *vp = ap->a_vp;
6109 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
6110 znode_t *zp = VTOZ(vp);
6111 int ltype;
6112 int error;
6114 ZFS_ENTER(zfsvfs);
6115 ZFS_VERIFY_ZP(zp);
6118 * If we are a snapshot mounted under .zfs, run the operation
6119 * on the covered vnode.
6121 if (zp->z_id != zfsvfs->z_root || zfsvfs->z_parent == zfsvfs) {
6122 char name[MAXNAMLEN + 1];
6123 znode_t *dzp;
6124 size_t len;
6126 error = zfs_znode_parent_and_name(zp, &dzp, name);
6127 if (error == 0) {
6128 len = strlen(name);
6129 if (*ap->a_buflen < len)
6130 error = SET_ERROR(ENOMEM);
6132 if (error == 0) {
6133 *ap->a_buflen -= len;
6134 memcpy(ap->a_buf + *ap->a_buflen, name, len);
6135 *ap->a_vpp = ZTOV(dzp);
6137 ZFS_EXIT(zfsvfs);
6138 return (error);
6140 ZFS_EXIT(zfsvfs);
6142 covered_vp = vp->v_mount->mnt_vnodecovered;
6143 #if __FreeBSD_version >= 1300045
6144 enum vgetstate vs = vget_prep(covered_vp);
6145 #else
6146 vhold(covered_vp);
6147 #endif
6148 ltype = VOP_ISLOCKED(vp);
6149 VOP_UNLOCK1(vp);
6150 #if __FreeBSD_version >= 1300045
6151 error = vget_finish(covered_vp, LK_SHARED, vs);
6152 #else
6153 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
6154 #endif
6155 if (error == 0) {
6156 #if __FreeBSD_version >= 1300123
6157 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_buf,
6158 ap->a_buflen);
6159 #else
6160 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
6161 ap->a_buf, ap->a_buflen);
6162 #endif
6163 vput(covered_vp);
6165 vn_lock(vp, ltype | LK_RETRY);
6166 if (VN_IS_DOOMED(vp))
6167 error = SET_ERROR(ENOENT);
6168 return (error);
6171 #if __FreeBSD_version >= 1400032
6172 static int
6173 zfs_deallocate(struct vop_deallocate_args *ap)
6175 znode_t *zp = VTOZ(ap->a_vp);
6176 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6177 zilog_t *zilog;
6178 off_t off, len, file_sz;
6179 int error;
6181 ZFS_ENTER(zfsvfs);
6182 ZFS_VERIFY_ZP(zp);
6185 * Callers might not be able to detect properly that we are read-only,
6186 * so check it explicitly here.
6188 if (zfs_is_readonly(zfsvfs)) {
6189 ZFS_EXIT(zfsvfs);
6190 return (SET_ERROR(EROFS));
6193 zilog = zfsvfs->z_log;
6194 off = *ap->a_offset;
6195 len = *ap->a_len;
6196 file_sz = zp->z_size;
6197 if (off + len > file_sz)
6198 len = file_sz - off;
6199 /* Fast path for out-of-range request. */
6200 if (len <= 0) {
6201 *ap->a_len = 0;
6202 ZFS_EXIT(zfsvfs);
6203 return (0);
6206 error = zfs_freesp(zp, off, len, O_RDWR, TRUE);
6207 if (error == 0) {
6208 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS ||
6209 (ap->a_ioflag & IO_SYNC) != 0)
6210 zil_commit(zilog, zp->z_id);
6211 *ap->a_offset = off + len;
6212 *ap->a_len = 0;
6215 ZFS_EXIT(zfsvfs);
6216 return (error);
6218 #endif
6220 struct vop_vector zfs_vnodeops;
6221 struct vop_vector zfs_fifoops;
6222 struct vop_vector zfs_shareops;
6224 struct vop_vector zfs_vnodeops = {
6225 .vop_default = &default_vnodeops,
6226 .vop_inactive = zfs_freebsd_inactive,
6227 #if __FreeBSD_version >= 1300042
6228 .vop_need_inactive = zfs_freebsd_need_inactive,
6229 #endif
6230 .vop_reclaim = zfs_freebsd_reclaim,
6231 #if __FreeBSD_version >= 1300102
6232 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6233 #endif
6234 #if __FreeBSD_version >= 1300139
6235 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6236 #endif
6237 .vop_access = zfs_freebsd_access,
6238 .vop_allocate = VOP_EINVAL,
6239 #if __FreeBSD_version >= 1400032
6240 .vop_deallocate = zfs_deallocate,
6241 #endif
6242 .vop_lookup = zfs_cache_lookup,
6243 .vop_cachedlookup = zfs_freebsd_cachedlookup,
6244 .vop_getattr = zfs_freebsd_getattr,
6245 .vop_setattr = zfs_freebsd_setattr,
6246 .vop_create = zfs_freebsd_create,
6247 .vop_mknod = (vop_mknod_t *)zfs_freebsd_create,
6248 .vop_mkdir = zfs_freebsd_mkdir,
6249 .vop_readdir = zfs_freebsd_readdir,
6250 .vop_fsync = zfs_freebsd_fsync,
6251 .vop_open = zfs_freebsd_open,
6252 .vop_close = zfs_freebsd_close,
6253 .vop_rmdir = zfs_freebsd_rmdir,
6254 .vop_ioctl = zfs_freebsd_ioctl,
6255 .vop_link = zfs_freebsd_link,
6256 .vop_symlink = zfs_freebsd_symlink,
6257 .vop_readlink = zfs_freebsd_readlink,
6258 .vop_read = zfs_freebsd_read,
6259 .vop_write = zfs_freebsd_write,
6260 .vop_remove = zfs_freebsd_remove,
6261 .vop_rename = zfs_freebsd_rename,
6262 .vop_pathconf = zfs_freebsd_pathconf,
6263 .vop_bmap = zfs_freebsd_bmap,
6264 .vop_fid = zfs_freebsd_fid,
6265 .vop_getextattr = zfs_getextattr,
6266 .vop_deleteextattr = zfs_deleteextattr,
6267 .vop_setextattr = zfs_setextattr,
6268 .vop_listextattr = zfs_listextattr,
6269 .vop_getacl = zfs_freebsd_getacl,
6270 .vop_setacl = zfs_freebsd_setacl,
6271 .vop_aclcheck = zfs_freebsd_aclcheck,
6272 .vop_getpages = zfs_freebsd_getpages,
6273 .vop_putpages = zfs_freebsd_putpages,
6274 .vop_vptocnp = zfs_vptocnp,
6275 #if __FreeBSD_version >= 1300064
6276 .vop_lock1 = vop_lock,
6277 .vop_unlock = vop_unlock,
6278 .vop_islocked = vop_islocked,
6279 #endif
6280 #if __FreeBSD_version >= 1400043
6281 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6282 #endif
6284 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops);
6286 struct vop_vector zfs_fifoops = {
6287 .vop_default = &fifo_specops,
6288 .vop_fsync = zfs_freebsd_fsync,
6289 #if __FreeBSD_version >= 1300102
6290 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6291 #endif
6292 #if __FreeBSD_version >= 1300139
6293 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6294 #endif
6295 .vop_access = zfs_freebsd_access,
6296 .vop_getattr = zfs_freebsd_getattr,
6297 .vop_inactive = zfs_freebsd_inactive,
6298 .vop_read = VOP_PANIC,
6299 .vop_reclaim = zfs_freebsd_reclaim,
6300 .vop_setattr = zfs_freebsd_setattr,
6301 .vop_write = VOP_PANIC,
6302 .vop_pathconf = zfs_freebsd_pathconf,
6303 .vop_fid = zfs_freebsd_fid,
6304 .vop_getacl = zfs_freebsd_getacl,
6305 .vop_setacl = zfs_freebsd_setacl,
6306 .vop_aclcheck = zfs_freebsd_aclcheck,
6307 #if __FreeBSD_version >= 1400043
6308 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6309 #endif
6311 VFS_VOP_VECTOR_REGISTER(zfs_fifoops);
6314 * special share hidden files vnode operations template
6316 struct vop_vector zfs_shareops = {
6317 .vop_default = &default_vnodeops,
6318 #if __FreeBSD_version >= 1300121
6319 .vop_fplookup_vexec = VOP_EAGAIN,
6320 #endif
6321 #if __FreeBSD_version >= 1300139
6322 .vop_fplookup_symlink = VOP_EAGAIN,
6323 #endif
6324 .vop_access = zfs_freebsd_access,
6325 .vop_inactive = zfs_freebsd_inactive,
6326 .vop_reclaim = zfs_freebsd_reclaim,
6327 .vop_fid = zfs_freebsd_fid,
6328 .vop_pathconf = zfs_freebsd_pathconf,
6329 #if __FreeBSD_version >= 1400043
6330 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6331 #endif
6333 VFS_VOP_VECTOR_REGISTER(zfs_shareops);
6335 ZFS_MODULE_PARAM(zfs, zfs_, xattr_compat, INT, ZMOD_RW,
6336 "Use legacy ZFS xattr naming for writing new user namespace xattrs");