]> git.proxmox.com Git - mirror_zfs.git/blob - module/os/freebsd/zfs/zfs_vnops_os.c
362e02751ee440c9e14f6f13a3a39d437f307ff2
[mirror_zfs.git] / module / os / freebsd / zfs / zfs_vnops_os.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
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.
27 */
28
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
31
32
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/bio.h>
80 #include <sys/buf.h>
81 #include <sys/sched.h>
82 #include <sys/acl.h>
83 #include <sys/vmmeter.h>
84 #include <vm/vm_param.h>
85 #include <sys/zil.h>
86 #include <sys/zfs_vnops.h>
87
88 #include <vm/vm_object.h>
89
90 #include <sys/extattr.h>
91 #include <sys/priv.h>
92
93 #ifndef VN_OPEN_INVFS
94 #define VN_OPEN_INVFS 0x0
95 #endif
96
97 VFS_SMR_DECLARE;
98
99 #if __FreeBSD_version < 1300103
100 #define NDFREE_PNBUF(ndp) NDFREE((ndp), NDF_ONLY_PNBUF)
101 #endif
102
103 #if __FreeBSD_version >= 1300047
104 #define vm_page_wire_lock(pp)
105 #define vm_page_wire_unlock(pp)
106 #else
107 #define vm_page_wire_lock(pp) vm_page_lock(pp)
108 #define vm_page_wire_unlock(pp) vm_page_unlock(pp)
109 #endif
110
111 #ifdef DEBUG_VFS_LOCKS
112 #define VNCHECKREF(vp) \
113 VNASSERT((vp)->v_holdcnt > 0 && (vp)->v_usecount > 0, vp, \
114 ("%s: wrong ref counts", __func__));
115 #else
116 #define VNCHECKREF(vp)
117 #endif
118
119 #if __FreeBSD_version >= 1400045
120 typedef uint64_t cookie_t;
121 #else
122 typedef ulong_t cookie_t;
123 #endif
124
125 /*
126 * Programming rules.
127 *
128 * Each vnode op performs some logical unit of work. To do this, the ZPL must
129 * properly lock its in-core state, create a DMU transaction, do the work,
130 * record this work in the intent log (ZIL), commit the DMU transaction,
131 * and wait for the intent log to commit if it is a synchronous operation.
132 * Moreover, the vnode ops must work in both normal and log replay context.
133 * The ordering of events is important to avoid deadlocks and references
134 * to freed memory. The example below illustrates the following Big Rules:
135 *
136 * (1) A check must be made in each zfs thread for a mounted file system.
137 * This is done avoiding races using zfs_enter(zfsvfs).
138 * A zfs_exit(zfsvfs) is needed before all returns. Any znodes
139 * must be checked with zfs_verify_zp(zp). Both of these macros
140 * can return EIO from the calling function.
141 *
142 * (2) VN_RELE() should always be the last thing except for zil_commit()
143 * (if necessary) and zfs_exit(). This is for 3 reasons:
144 * First, if it's the last reference, the vnode/znode
145 * can be freed, so the zp may point to freed memory. Second, the last
146 * reference will call zfs_zinactive(), which may induce a lot of work --
147 * pushing cached pages (which acquires range locks) and syncing out
148 * cached atime changes. Third, zfs_zinactive() may require a new tx,
149 * which could deadlock the system if you were already holding one.
150 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
151 *
152 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
153 * as they can span dmu_tx_assign() calls.
154 *
155 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
156 * dmu_tx_assign(). This is critical because we don't want to block
157 * while holding locks.
158 *
159 * If no ZPL locks are held (aside from zfs_enter()), use TXG_WAIT. This
160 * reduces lock contention and CPU usage when we must wait (note that if
161 * throughput is constrained by the storage, nearly every transaction
162 * must wait).
163 *
164 * Note, in particular, that if a lock is sometimes acquired before
165 * the tx assigns, and sometimes after (e.g. z_lock), then failing
166 * to use a non-blocking assign can deadlock the system. The scenario:
167 *
168 * Thread A has grabbed a lock before calling dmu_tx_assign().
169 * Thread B is in an already-assigned tx, and blocks for this lock.
170 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
171 * forever, because the previous txg can't quiesce until B's tx commits.
172 *
173 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
174 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
175 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
176 * to indicate that this operation has already called dmu_tx_wait().
177 * This will ensure that we don't retry forever, waiting a short bit
178 * each time.
179 *
180 * (5) If the operation succeeded, generate the intent log entry for it
181 * before dropping locks. This ensures that the ordering of events
182 * in the intent log matches the order in which they actually occurred.
183 * During ZIL replay the zfs_log_* functions will update the sequence
184 * number to indicate the zil transaction has replayed.
185 *
186 * (6) At the end of each vnode op, the DMU tx must always commit,
187 * regardless of whether there were any errors.
188 *
189 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
190 * to ensure that synchronous semantics are provided when necessary.
191 *
192 * In general, this is how things should be ordered in each vnode op:
193 *
194 * zfs_enter(zfsvfs); // exit if unmounted
195 * top:
196 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD())
197 * rw_enter(...); // grab any other locks you need
198 * tx = dmu_tx_create(...); // get DMU tx
199 * dmu_tx_hold_*(); // hold each object you might modify
200 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
201 * if (error) {
202 * rw_exit(...); // drop locks
203 * zfs_dirent_unlock(dl); // unlock directory entry
204 * VN_RELE(...); // release held vnodes
205 * if (error == ERESTART) {
206 * waited = B_TRUE;
207 * dmu_tx_wait(tx);
208 * dmu_tx_abort(tx);
209 * goto top;
210 * }
211 * dmu_tx_abort(tx); // abort DMU tx
212 * zfs_exit(zfsvfs); // finished in zfs
213 * return (error); // really out of space
214 * }
215 * error = do_real_work(); // do whatever this VOP does
216 * if (error == 0)
217 * zfs_log_*(...); // on success, make ZIL entry
218 * dmu_tx_commit(tx); // commit DMU tx -- error or not
219 * rw_exit(...); // drop locks
220 * zfs_dirent_unlock(dl); // unlock directory entry
221 * VN_RELE(...); // release held vnodes
222 * zil_commit(zilog, foid); // synchronous when necessary
223 * zfs_exit(zfsvfs); // finished in zfs
224 * return (error); // done, report error
225 */
226 static int
227 zfs_open(vnode_t **vpp, int flag, cred_t *cr)
228 {
229 (void) cr;
230 znode_t *zp = VTOZ(*vpp);
231 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
232 int error;
233
234 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
235 return (error);
236
237 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
238 ((flag & FAPPEND) == 0)) {
239 zfs_exit(zfsvfs, FTAG);
240 return (SET_ERROR(EPERM));
241 }
242
243 /* Keep a count of the synchronous opens in the znode */
244 if (flag & O_SYNC)
245 atomic_inc_32(&zp->z_sync_cnt);
246
247 zfs_exit(zfsvfs, FTAG);
248 return (0);
249 }
250
251 static int
252 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
253 {
254 (void) offset, (void) cr;
255 znode_t *zp = VTOZ(vp);
256 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
257 int error;
258
259 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
260 return (error);
261
262 /* Decrement the synchronous opens in the znode */
263 if ((flag & O_SYNC) && (count == 1))
264 atomic_dec_32(&zp->z_sync_cnt);
265
266 zfs_exit(zfsvfs, FTAG);
267 return (0);
268 }
269
270 static int
271 zfs_ioctl(vnode_t *vp, ulong_t com, intptr_t data, int flag, cred_t *cred,
272 int *rvalp)
273 {
274 (void) flag, (void) cred, (void) rvalp;
275 loff_t off;
276 int error;
277
278 switch (com) {
279 case _FIOFFS:
280 {
281 return (0);
282
283 /*
284 * The following two ioctls are used by bfu. Faking out,
285 * necessary to avoid bfu errors.
286 */
287 }
288 case _FIOGDIO:
289 case _FIOSDIO:
290 {
291 return (0);
292 }
293
294 case F_SEEK_DATA:
295 case F_SEEK_HOLE:
296 {
297 off = *(offset_t *)data;
298 /* offset parameter is in/out */
299 error = zfs_holey(VTOZ(vp), com, &off);
300 if (error)
301 return (error);
302 *(offset_t *)data = off;
303 return (0);
304 }
305 }
306 return (SET_ERROR(ENOTTY));
307 }
308
309 static vm_page_t
310 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
311 {
312 vm_object_t obj;
313 vm_page_t pp;
314 int64_t end;
315
316 /*
317 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
318 * aligned boundaries, if the range is not aligned. As a result a
319 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
320 * It may happen that all DEV_BSIZE subranges are marked clean and thus
321 * the whole page would be considered clean despite have some
322 * dirty data.
323 * For this reason we should shrink the range to DEV_BSIZE aligned
324 * boundaries before calling vm_page_clear_dirty.
325 */
326 end = rounddown2(off + nbytes, DEV_BSIZE);
327 off = roundup2(off, DEV_BSIZE);
328 nbytes = end - off;
329
330 obj = vp->v_object;
331 zfs_vmobject_assert_wlocked_12(obj);
332 #if __FreeBSD_version < 1300050
333 for (;;) {
334 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
335 pp->valid) {
336 if (vm_page_xbusied(pp)) {
337 /*
338 * Reference the page before unlocking and
339 * sleeping so that the page daemon is less
340 * likely to reclaim it.
341 */
342 vm_page_reference(pp);
343 vm_page_lock(pp);
344 zfs_vmobject_wunlock(obj);
345 vm_page_busy_sleep(pp, "zfsmwb", true);
346 zfs_vmobject_wlock(obj);
347 continue;
348 }
349 vm_page_sbusy(pp);
350 } else if (pp != NULL) {
351 ASSERT(!pp->valid);
352 pp = NULL;
353 }
354 if (pp != NULL) {
355 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
356 vm_object_pip_add(obj, 1);
357 pmap_remove_write(pp);
358 if (nbytes != 0)
359 vm_page_clear_dirty(pp, off, nbytes);
360 }
361 break;
362 }
363 #else
364 vm_page_grab_valid_unlocked(&pp, obj, OFF_TO_IDX(start),
365 VM_ALLOC_NOCREAT | VM_ALLOC_SBUSY | VM_ALLOC_NORMAL |
366 VM_ALLOC_IGN_SBUSY);
367 if (pp != NULL) {
368 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
369 vm_object_pip_add(obj, 1);
370 pmap_remove_write(pp);
371 if (nbytes != 0)
372 vm_page_clear_dirty(pp, off, nbytes);
373 }
374 #endif
375 return (pp);
376 }
377
378 static void
379 page_unbusy(vm_page_t pp)
380 {
381
382 vm_page_sunbusy(pp);
383 #if __FreeBSD_version >= 1300041
384 vm_object_pip_wakeup(pp->object);
385 #else
386 vm_object_pip_subtract(pp->object, 1);
387 #endif
388 }
389
390 #if __FreeBSD_version > 1300051
391 static vm_page_t
392 page_hold(vnode_t *vp, int64_t start)
393 {
394 vm_object_t obj;
395 vm_page_t m;
396
397 obj = vp->v_object;
398 vm_page_grab_valid_unlocked(&m, obj, OFF_TO_IDX(start),
399 VM_ALLOC_NOCREAT | VM_ALLOC_WIRED | VM_ALLOC_IGN_SBUSY |
400 VM_ALLOC_NOBUSY);
401 return (m);
402 }
403 #else
404 static vm_page_t
405 page_hold(vnode_t *vp, int64_t start)
406 {
407 vm_object_t obj;
408 vm_page_t pp;
409
410 obj = vp->v_object;
411 zfs_vmobject_assert_wlocked(obj);
412
413 for (;;) {
414 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
415 pp->valid) {
416 if (vm_page_xbusied(pp)) {
417 /*
418 * Reference the page before unlocking and
419 * sleeping so that the page daemon is less
420 * likely to reclaim it.
421 */
422 vm_page_reference(pp);
423 vm_page_lock(pp);
424 zfs_vmobject_wunlock(obj);
425 vm_page_busy_sleep(pp, "zfsmwb", true);
426 zfs_vmobject_wlock(obj);
427 continue;
428 }
429
430 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
431 vm_page_wire_lock(pp);
432 vm_page_hold(pp);
433 vm_page_wire_unlock(pp);
434
435 } else
436 pp = NULL;
437 break;
438 }
439 return (pp);
440 }
441 #endif
442
443 static void
444 page_unhold(vm_page_t pp)
445 {
446
447 vm_page_wire_lock(pp);
448 #if __FreeBSD_version >= 1300035
449 vm_page_unwire(pp, PQ_ACTIVE);
450 #else
451 vm_page_unhold(pp);
452 #endif
453 vm_page_wire_unlock(pp);
454 }
455
456 /*
457 * When a file is memory mapped, we must keep the IO data synchronized
458 * between the DMU cache and the memory mapped pages. What this means:
459 *
460 * On Write: If we find a memory mapped page, we write to *both*
461 * the page and the dmu buffer.
462 */
463 void
464 update_pages(znode_t *zp, int64_t start, int len, objset_t *os)
465 {
466 vm_object_t obj;
467 struct sf_buf *sf;
468 vnode_t *vp = ZTOV(zp);
469 caddr_t va;
470 int off;
471
472 ASSERT3P(vp->v_mount, !=, NULL);
473 obj = vp->v_object;
474 ASSERT3P(obj, !=, NULL);
475
476 off = start & PAGEOFFSET;
477 zfs_vmobject_wlock_12(obj);
478 #if __FreeBSD_version >= 1300041
479 vm_object_pip_add(obj, 1);
480 #endif
481 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
482 vm_page_t pp;
483 int nbytes = imin(PAGESIZE - off, len);
484
485 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
486 zfs_vmobject_wunlock_12(obj);
487
488 va = zfs_map_page(pp, &sf);
489 (void) dmu_read(os, zp->z_id, start + off, nbytes,
490 va + off, DMU_READ_PREFETCH);
491 zfs_unmap_page(sf);
492
493 zfs_vmobject_wlock_12(obj);
494 page_unbusy(pp);
495 }
496 len -= nbytes;
497 off = 0;
498 }
499 #if __FreeBSD_version >= 1300041
500 vm_object_pip_wakeup(obj);
501 #else
502 vm_object_pip_wakeupn(obj, 0);
503 #endif
504 zfs_vmobject_wunlock_12(obj);
505 }
506
507 /*
508 * Read with UIO_NOCOPY flag means that sendfile(2) requests
509 * ZFS to populate a range of page cache pages with data.
510 *
511 * NOTE: this function could be optimized to pre-allocate
512 * all pages in advance, drain exclusive busy on all of them,
513 * map them into contiguous KVA region and populate them
514 * in one single dmu_read() call.
515 */
516 int
517 mappedread_sf(znode_t *zp, int nbytes, zfs_uio_t *uio)
518 {
519 vnode_t *vp = ZTOV(zp);
520 objset_t *os = zp->z_zfsvfs->z_os;
521 struct sf_buf *sf;
522 vm_object_t obj;
523 vm_page_t pp;
524 int64_t start;
525 caddr_t va;
526 int len = nbytes;
527 int error = 0;
528
529 ASSERT3U(zfs_uio_segflg(uio), ==, UIO_NOCOPY);
530 ASSERT3P(vp->v_mount, !=, NULL);
531 obj = vp->v_object;
532 ASSERT3P(obj, !=, NULL);
533 ASSERT0(zfs_uio_offset(uio) & PAGEOFFSET);
534
535 zfs_vmobject_wlock_12(obj);
536 for (start = zfs_uio_offset(uio); len > 0; start += PAGESIZE) {
537 int bytes = MIN(PAGESIZE, len);
538
539 pp = vm_page_grab_unlocked(obj, OFF_TO_IDX(start),
540 VM_ALLOC_SBUSY | VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
541 if (vm_page_none_valid(pp)) {
542 zfs_vmobject_wunlock_12(obj);
543 va = zfs_map_page(pp, &sf);
544 error = dmu_read(os, zp->z_id, start, bytes, va,
545 DMU_READ_PREFETCH);
546 if (bytes != PAGESIZE && error == 0)
547 memset(va + bytes, 0, PAGESIZE - bytes);
548 zfs_unmap_page(sf);
549 zfs_vmobject_wlock_12(obj);
550 #if __FreeBSD_version >= 1300081
551 if (error == 0) {
552 vm_page_valid(pp);
553 vm_page_activate(pp);
554 vm_page_do_sunbusy(pp);
555 } else {
556 zfs_vmobject_wlock(obj);
557 if (!vm_page_wired(pp) && pp->valid == 0 &&
558 vm_page_busy_tryupgrade(pp))
559 vm_page_free(pp);
560 else
561 vm_page_sunbusy(pp);
562 zfs_vmobject_wunlock(obj);
563 }
564 #else
565 vm_page_do_sunbusy(pp);
566 vm_page_lock(pp);
567 if (error) {
568 if (pp->wire_count == 0 && pp->valid == 0 &&
569 !vm_page_busied(pp))
570 vm_page_free(pp);
571 } else {
572 pp->valid = VM_PAGE_BITS_ALL;
573 vm_page_activate(pp);
574 }
575 vm_page_unlock(pp);
576 #endif
577 } else {
578 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
579 vm_page_do_sunbusy(pp);
580 }
581 if (error)
582 break;
583 zfs_uio_advance(uio, bytes);
584 len -= bytes;
585 }
586 zfs_vmobject_wunlock_12(obj);
587 return (error);
588 }
589
590 /*
591 * When a file is memory mapped, we must keep the IO data synchronized
592 * between the DMU cache and the memory mapped pages. What this means:
593 *
594 * On Read: We "read" preferentially from memory mapped pages,
595 * else we default from the dmu buffer.
596 *
597 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
598 * the file is memory mapped.
599 */
600 int
601 mappedread(znode_t *zp, int nbytes, zfs_uio_t *uio)
602 {
603 vnode_t *vp = ZTOV(zp);
604 vm_object_t obj;
605 int64_t start;
606 int len = nbytes;
607 int off;
608 int error = 0;
609
610 ASSERT3P(vp->v_mount, !=, NULL);
611 obj = vp->v_object;
612 ASSERT3P(obj, !=, NULL);
613
614 start = zfs_uio_offset(uio);
615 off = start & PAGEOFFSET;
616 zfs_vmobject_wlock_12(obj);
617 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
618 vm_page_t pp;
619 uint64_t bytes = MIN(PAGESIZE - off, len);
620
621 if ((pp = page_hold(vp, start))) {
622 struct sf_buf *sf;
623 caddr_t va;
624
625 zfs_vmobject_wunlock_12(obj);
626 va = zfs_map_page(pp, &sf);
627 error = vn_io_fault_uiomove(va + off, bytes,
628 GET_UIO_STRUCT(uio));
629 zfs_unmap_page(sf);
630 zfs_vmobject_wlock_12(obj);
631 page_unhold(pp);
632 } else {
633 zfs_vmobject_wunlock_12(obj);
634 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
635 uio, bytes);
636 zfs_vmobject_wlock_12(obj);
637 }
638 len -= bytes;
639 off = 0;
640 if (error)
641 break;
642 }
643 zfs_vmobject_wunlock_12(obj);
644 return (error);
645 }
646
647 int
648 zfs_write_simple(znode_t *zp, const void *data, size_t len,
649 loff_t pos, size_t *presid)
650 {
651 int error = 0;
652 ssize_t resid;
653
654 error = vn_rdwr(UIO_WRITE, ZTOV(zp), __DECONST(void *, data), len, pos,
655 UIO_SYSSPACE, IO_SYNC, kcred, NOCRED, &resid, curthread);
656
657 if (error) {
658 return (SET_ERROR(error));
659 } else if (presid == NULL) {
660 if (resid != 0) {
661 error = SET_ERROR(EIO);
662 }
663 } else {
664 *presid = resid;
665 }
666 return (error);
667 }
668
669 void
670 zfs_zrele_async(znode_t *zp)
671 {
672 vnode_t *vp = ZTOV(zp);
673 objset_t *os = ITOZSB(vp)->z_os;
674
675 VN_RELE_ASYNC(vp, dsl_pool_zrele_taskq(dmu_objset_pool(os)));
676 }
677
678 static int
679 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
680 {
681 int error;
682
683 *vpp = arg;
684 error = vn_lock(*vpp, lkflags);
685 if (error != 0)
686 vrele(*vpp);
687 return (error);
688 }
689
690 static int
691 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags)
692 {
693 znode_t *zdp = VTOZ(dvp);
694 zfsvfs_t *zfsvfs __unused = zdp->z_zfsvfs;
695 int error;
696 int ltype;
697
698 if (zfsvfs->z_replay == B_FALSE)
699 ASSERT_VOP_LOCKED(dvp, __func__);
700
701 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
702 ASSERT3P(dvp, ==, vp);
703 vref(dvp);
704 ltype = lkflags & LK_TYPE_MASK;
705 if (ltype != VOP_ISLOCKED(dvp)) {
706 if (ltype == LK_EXCLUSIVE)
707 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
708 else /* if (ltype == LK_SHARED) */
709 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
710
711 /*
712 * Relock for the "." case could leave us with
713 * reclaimed vnode.
714 */
715 if (VN_IS_DOOMED(dvp)) {
716 vrele(dvp);
717 return (SET_ERROR(ENOENT));
718 }
719 }
720 return (0);
721 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
722 /*
723 * Note that in this case, dvp is the child vnode, and we
724 * are looking up the parent vnode - exactly reverse from
725 * normal operation. Unlocking dvp requires some rather
726 * tricky unlock/relock dance to prevent mp from being freed;
727 * use vn_vget_ino_gen() which takes care of all that.
728 *
729 * XXX Note that there is a time window when both vnodes are
730 * unlocked. It is possible, although highly unlikely, that
731 * during that window the parent-child relationship between
732 * the vnodes may change, for example, get reversed.
733 * In that case we would have a wrong lock order for the vnodes.
734 * All other filesystems seem to ignore this problem, so we
735 * do the same here.
736 * A potential solution could be implemented as follows:
737 * - using LK_NOWAIT when locking the second vnode and retrying
738 * if necessary
739 * - checking that the parent-child relationship still holds
740 * after locking both vnodes and retrying if it doesn't
741 */
742 error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp);
743 return (error);
744 } else {
745 error = vn_lock(vp, lkflags);
746 if (error != 0)
747 vrele(vp);
748 return (error);
749 }
750 }
751
752 /*
753 * Lookup an entry in a directory, or an extended attribute directory.
754 * If it exists, return a held vnode reference for it.
755 *
756 * IN: dvp - vnode of directory to search.
757 * nm - name of entry to lookup.
758 * pnp - full pathname to lookup [UNUSED].
759 * flags - LOOKUP_XATTR set if looking for an attribute.
760 * rdir - root directory vnode [UNUSED].
761 * cr - credentials of caller.
762 * ct - caller context
763 *
764 * OUT: vpp - vnode of located entry, NULL if not found.
765 *
766 * RETURN: 0 on success, error code on failure.
767 *
768 * Timestamps:
769 * NA
770 */
771 static int
772 zfs_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp,
773 struct componentname *cnp, int nameiop, cred_t *cr, int flags,
774 boolean_t cached)
775 {
776 znode_t *zdp = VTOZ(dvp);
777 znode_t *zp;
778 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
779 #if __FreeBSD_version > 1300124
780 seqc_t dvp_seqc;
781 #endif
782 int error = 0;
783
784 /*
785 * Fast path lookup, however we must skip DNLC lookup
786 * for case folding or normalizing lookups because the
787 * DNLC code only stores the passed in name. This means
788 * creating 'a' and removing 'A' on a case insensitive
789 * file system would work, but DNLC still thinks 'a'
790 * exists and won't let you create it again on the next
791 * pass through fast path.
792 */
793 if (!(flags & LOOKUP_XATTR)) {
794 if (dvp->v_type != VDIR) {
795 return (SET_ERROR(ENOTDIR));
796 } else if (zdp->z_sa_hdl == NULL) {
797 return (SET_ERROR(EIO));
798 }
799 }
800
801 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp,
802 const char *, nm);
803
804 if ((error = zfs_enter_verify_zp(zfsvfs, zdp, FTAG)) != 0)
805 return (error);
806
807 #if __FreeBSD_version > 1300124
808 dvp_seqc = vn_seqc_read_notmodify(dvp);
809 #endif
810
811 *vpp = NULL;
812
813 if (flags & LOOKUP_XATTR) {
814 /*
815 * If the xattr property is off, refuse the lookup request.
816 */
817 if (!(zfsvfs->z_flags & ZSB_XATTR)) {
818 zfs_exit(zfsvfs, FTAG);
819 return (SET_ERROR(EOPNOTSUPP));
820 }
821
822 /*
823 * We don't allow recursive attributes..
824 * Maybe someday we will.
825 */
826 if (zdp->z_pflags & ZFS_XATTR) {
827 zfs_exit(zfsvfs, FTAG);
828 return (SET_ERROR(EINVAL));
829 }
830
831 if ((error = zfs_get_xattrdir(VTOZ(dvp), &zp, cr, flags))) {
832 zfs_exit(zfsvfs, FTAG);
833 return (error);
834 }
835 *vpp = ZTOV(zp);
836
837 /*
838 * Do we have permission to get into attribute directory?
839 */
840 error = zfs_zaccess(zp, ACE_EXECUTE, 0, B_FALSE, cr, NULL);
841 if (error) {
842 vrele(ZTOV(zp));
843 }
844
845 zfs_exit(zfsvfs, FTAG);
846 return (error);
847 }
848
849 /*
850 * Check accessibility of directory if we're not coming in via
851 * VOP_CACHEDLOOKUP.
852 */
853 if (!cached) {
854 #ifdef NOEXECCHECK
855 if ((cnp->cn_flags & NOEXECCHECK) != 0) {
856 cnp->cn_flags &= ~NOEXECCHECK;
857 } else
858 #endif
859 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr,
860 NULL))) {
861 zfs_exit(zfsvfs, FTAG);
862 return (error);
863 }
864 }
865
866 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
867 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
868 zfs_exit(zfsvfs, FTAG);
869 return (SET_ERROR(EILSEQ));
870 }
871
872
873 /*
874 * First handle the special cases.
875 */
876 if ((cnp->cn_flags & ISDOTDOT) != 0) {
877 /*
878 * If we are a snapshot mounted under .zfs, return
879 * the vp for the snapshot directory.
880 */
881 if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) {
882 struct componentname cn;
883 vnode_t *zfsctl_vp;
884 int ltype;
885
886 zfs_exit(zfsvfs, FTAG);
887 ltype = VOP_ISLOCKED(dvp);
888 VOP_UNLOCK1(dvp);
889 error = zfsctl_root(zfsvfs->z_parent, LK_SHARED,
890 &zfsctl_vp);
891 if (error == 0) {
892 cn.cn_nameptr = "snapshot";
893 cn.cn_namelen = strlen(cn.cn_nameptr);
894 cn.cn_nameiop = cnp->cn_nameiop;
895 cn.cn_flags = cnp->cn_flags & ~ISDOTDOT;
896 cn.cn_lkflags = cnp->cn_lkflags;
897 error = VOP_LOOKUP(zfsctl_vp, vpp, &cn);
898 vput(zfsctl_vp);
899 }
900 vn_lock(dvp, ltype | LK_RETRY);
901 return (error);
902 }
903 }
904 if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) {
905 zfs_exit(zfsvfs, FTAG);
906 if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP)
907 return (SET_ERROR(ENOTSUP));
908 error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp);
909 return (error);
910 }
911
912 /*
913 * The loop is retry the lookup if the parent-child relationship
914 * changes during the dot-dot locking complexities.
915 */
916 for (;;) {
917 uint64_t parent;
918
919 error = zfs_dirlook(zdp, nm, &zp);
920 if (error == 0)
921 *vpp = ZTOV(zp);
922
923 zfs_exit(zfsvfs, FTAG);
924 if (error != 0)
925 break;
926
927 error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags);
928 if (error != 0) {
929 /*
930 * If we've got a locking error, then the vnode
931 * got reclaimed because of a force unmount.
932 * We never enter doomed vnodes into the name cache.
933 */
934 *vpp = NULL;
935 return (error);
936 }
937
938 if ((cnp->cn_flags & ISDOTDOT) == 0)
939 break;
940
941 if ((error = zfs_enter(zfsvfs, FTAG)) != 0) {
942 vput(ZTOV(zp));
943 *vpp = NULL;
944 return (error);
945 }
946 if (zdp->z_sa_hdl == NULL) {
947 error = SET_ERROR(EIO);
948 } else {
949 error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
950 &parent, sizeof (parent));
951 }
952 if (error != 0) {
953 zfs_exit(zfsvfs, FTAG);
954 vput(ZTOV(zp));
955 break;
956 }
957 if (zp->z_id == parent) {
958 zfs_exit(zfsvfs, FTAG);
959 break;
960 }
961 vput(ZTOV(zp));
962 }
963
964 if (error != 0)
965 *vpp = NULL;
966
967 /* Translate errors and add SAVENAME when needed. */
968 if (cnp->cn_flags & ISLASTCN) {
969 switch (nameiop) {
970 case CREATE:
971 case RENAME:
972 if (error == ENOENT) {
973 error = EJUSTRETURN;
974 #if __FreeBSD_version < 1400068
975 cnp->cn_flags |= SAVENAME;
976 #endif
977 break;
978 }
979 zfs_fallthrough;
980 case DELETE:
981 #if __FreeBSD_version < 1400068
982 if (error == 0)
983 cnp->cn_flags |= SAVENAME;
984 #endif
985 break;
986 }
987 }
988
989 #if __FreeBSD_version > 1300124
990 if ((cnp->cn_flags & ISDOTDOT) != 0) {
991 /*
992 * FIXME: zfs_lookup_lock relocks vnodes and does nothing to
993 * handle races. In particular different callers may end up
994 * with different vnodes and will try to add conflicting
995 * entries to the namecache.
996 *
997 * While finding different result may be acceptable in face
998 * of concurrent modification, adding conflicting entries
999 * trips over an assert in the namecache.
1000 *
1001 * Ultimately let an entry through once everything settles.
1002 */
1003 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
1004 cnp->cn_flags &= ~MAKEENTRY;
1005 }
1006 }
1007 #endif
1008
1009 /* Insert name into cache (as non-existent) if appropriate. */
1010 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1011 error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1012 cache_enter(dvp, NULL, cnp);
1013
1014 /* Insert name into cache if appropriate. */
1015 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1016 error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1017 if (!(cnp->cn_flags & ISLASTCN) ||
1018 (nameiop != DELETE && nameiop != RENAME)) {
1019 cache_enter(dvp, *vpp, cnp);
1020 }
1021 }
1022
1023 return (error);
1024 }
1025
1026 /*
1027 * Attempt to create a new entry in a directory. If the entry
1028 * already exists, truncate the file if permissible, else return
1029 * an error. Return the vp of the created or trunc'd file.
1030 *
1031 * IN: dvp - vnode of directory to put new file entry in.
1032 * name - name of new file entry.
1033 * vap - attributes of new file.
1034 * excl - flag indicating exclusive or non-exclusive mode.
1035 * mode - mode to open file with.
1036 * cr - credentials of caller.
1037 * flag - large file flag [UNUSED].
1038 * ct - caller context
1039 * vsecp - ACL to be set
1040 * mnt_ns - Unused on FreeBSD
1041 *
1042 * OUT: vpp - vnode of created or trunc'd entry.
1043 *
1044 * RETURN: 0 on success, error code on failure.
1045 *
1046 * Timestamps:
1047 * dvp - ctime|mtime updated if new entry created
1048 * vp - ctime|mtime always, atime if new
1049 */
1050 int
1051 zfs_create(znode_t *dzp, const char *name, vattr_t *vap, int excl, int mode,
1052 znode_t **zpp, cred_t *cr, int flag, vsecattr_t *vsecp, zuserns_t *mnt_ns)
1053 {
1054 (void) excl, (void) mode, (void) flag;
1055 znode_t *zp;
1056 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1057 zilog_t *zilog;
1058 objset_t *os;
1059 dmu_tx_t *tx;
1060 int error;
1061 uid_t uid = crgetuid(cr);
1062 gid_t gid = crgetgid(cr);
1063 uint64_t projid = ZFS_DEFAULT_PROJID;
1064 zfs_acl_ids_t acl_ids;
1065 boolean_t fuid_dirtied;
1066 uint64_t txtype;
1067 #ifdef DEBUG_VFS_LOCKS
1068 vnode_t *dvp = ZTOV(dzp);
1069 #endif
1070
1071 /*
1072 * If we have an ephemeral id, ACL, or XVATTR then
1073 * make sure file system is at proper version
1074 */
1075 if (zfsvfs->z_use_fuids == B_FALSE &&
1076 (vsecp || (vap->va_mask & AT_XVATTR) ||
1077 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1078 return (SET_ERROR(EINVAL));
1079
1080 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1081 return (error);
1082 os = zfsvfs->z_os;
1083 zilog = zfsvfs->z_log;
1084
1085 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1086 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1087 zfs_exit(zfsvfs, FTAG);
1088 return (SET_ERROR(EILSEQ));
1089 }
1090
1091 if (vap->va_mask & AT_XVATTR) {
1092 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
1093 crgetuid(cr), cr, vap->va_type)) != 0) {
1094 zfs_exit(zfsvfs, FTAG);
1095 return (error);
1096 }
1097 }
1098
1099 *zpp = NULL;
1100
1101 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1102 vap->va_mode &= ~S_ISVTX;
1103
1104 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
1105 if (error) {
1106 zfs_exit(zfsvfs, FTAG);
1107 return (error);
1108 }
1109 ASSERT3P(zp, ==, NULL);
1110
1111 /*
1112 * Create a new file object and update the directory
1113 * to reference it.
1114 */
1115 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) {
1116 goto out;
1117 }
1118
1119 /*
1120 * We only support the creation of regular files in
1121 * extended attribute directories.
1122 */
1123
1124 if ((dzp->z_pflags & ZFS_XATTR) &&
1125 (vap->va_type != VREG)) {
1126 error = SET_ERROR(EINVAL);
1127 goto out;
1128 }
1129
1130 if ((error = zfs_acl_ids_create(dzp, 0, vap,
1131 cr, vsecp, &acl_ids, NULL)) != 0)
1132 goto out;
1133
1134 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode))
1135 projid = zfs_inherit_projid(dzp);
1136 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) {
1137 zfs_acl_ids_free(&acl_ids);
1138 error = SET_ERROR(EDQUOT);
1139 goto out;
1140 }
1141
1142 getnewvnode_reserve_();
1143
1144 tx = dmu_tx_create(os);
1145
1146 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1147 ZFS_SA_BASE_ATTR_SIZE);
1148
1149 fuid_dirtied = zfsvfs->z_fuid_dirty;
1150 if (fuid_dirtied)
1151 zfs_fuid_txhold(zfsvfs, tx);
1152 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1153 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1154 if (!zfsvfs->z_use_sa &&
1155 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1156 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1157 0, acl_ids.z_aclp->z_acl_bytes);
1158 }
1159 error = dmu_tx_assign(tx, TXG_WAIT);
1160 if (error) {
1161 zfs_acl_ids_free(&acl_ids);
1162 dmu_tx_abort(tx);
1163 getnewvnode_drop_reserve();
1164 zfs_exit(zfsvfs, FTAG);
1165 return (error);
1166 }
1167 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1168 if (fuid_dirtied)
1169 zfs_fuid_sync(zfsvfs, tx);
1170
1171 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
1172 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1173 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1174 vsecp, acl_ids.z_fuidp, vap);
1175 zfs_acl_ids_free(&acl_ids);
1176 dmu_tx_commit(tx);
1177
1178 getnewvnode_drop_reserve();
1179
1180 out:
1181 VNCHECKREF(dvp);
1182 if (error == 0) {
1183 *zpp = zp;
1184 }
1185
1186 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1187 zil_commit(zilog, 0);
1188
1189 zfs_exit(zfsvfs, FTAG);
1190 return (error);
1191 }
1192
1193 /*
1194 * Remove an entry from a directory.
1195 *
1196 * IN: dvp - vnode of directory to remove entry from.
1197 * name - name of entry to remove.
1198 * cr - credentials of caller.
1199 * ct - caller context
1200 * flags - case flags
1201 *
1202 * RETURN: 0 on success, error code on failure.
1203 *
1204 * Timestamps:
1205 * dvp - ctime|mtime
1206 * vp - ctime (if nlink > 0)
1207 */
1208 static int
1209 zfs_remove_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
1210 {
1211 znode_t *dzp = VTOZ(dvp);
1212 znode_t *zp;
1213 znode_t *xzp;
1214 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1215 zilog_t *zilog;
1216 uint64_t xattr_obj;
1217 uint64_t obj = 0;
1218 dmu_tx_t *tx;
1219 boolean_t unlinked;
1220 uint64_t txtype;
1221 int error;
1222
1223
1224 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1225 return (error);
1226 zp = VTOZ(vp);
1227 if ((error = zfs_verify_zp(zp)) != 0) {
1228 zfs_exit(zfsvfs, FTAG);
1229 return (error);
1230 }
1231 zilog = zfsvfs->z_log;
1232
1233 xattr_obj = 0;
1234 xzp = NULL;
1235
1236 if ((error = zfs_zaccess_delete(dzp, zp, cr, NULL))) {
1237 goto out;
1238 }
1239
1240 /*
1241 * Need to use rmdir for removing directories.
1242 */
1243 if (vp->v_type == VDIR) {
1244 error = SET_ERROR(EPERM);
1245 goto out;
1246 }
1247
1248 vnevent_remove(vp, dvp, name, ct);
1249
1250 obj = zp->z_id;
1251
1252 /* are there any extended attributes? */
1253 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1254 &xattr_obj, sizeof (xattr_obj));
1255 if (error == 0 && xattr_obj) {
1256 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1257 ASSERT0(error);
1258 }
1259
1260 /*
1261 * We may delete the znode now, or we may put it in the unlinked set;
1262 * it depends on whether we're the last link, and on whether there are
1263 * other holds on the vnode. So we dmu_tx_hold() the right things to
1264 * allow for either case.
1265 */
1266 tx = dmu_tx_create(zfsvfs->z_os);
1267 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1268 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1269 zfs_sa_upgrade_txholds(tx, zp);
1270 zfs_sa_upgrade_txholds(tx, dzp);
1271
1272 if (xzp) {
1273 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1274 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1275 }
1276
1277 /* charge as an update -- would be nice not to charge at all */
1278 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1279
1280 /*
1281 * Mark this transaction as typically resulting in a net free of space
1282 */
1283 dmu_tx_mark_netfree(tx);
1284
1285 error = dmu_tx_assign(tx, TXG_WAIT);
1286 if (error) {
1287 dmu_tx_abort(tx);
1288 zfs_exit(zfsvfs, FTAG);
1289 return (error);
1290 }
1291
1292 /*
1293 * Remove the directory entry.
1294 */
1295 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked);
1296
1297 if (error) {
1298 dmu_tx_commit(tx);
1299 goto out;
1300 }
1301
1302 if (unlinked) {
1303 zfs_unlinked_add(zp, tx);
1304 vp->v_vflag |= VV_NOSYNC;
1305 }
1306 /* XXX check changes to linux vnops */
1307 txtype = TX_REMOVE;
1308 zfs_log_remove(zilog, tx, txtype, dzp, name, obj, unlinked);
1309
1310 dmu_tx_commit(tx);
1311 out:
1312
1313 if (xzp)
1314 vrele(ZTOV(xzp));
1315
1316 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1317 zil_commit(zilog, 0);
1318
1319
1320 zfs_exit(zfsvfs, FTAG);
1321 return (error);
1322 }
1323
1324
1325 static int
1326 zfs_lookup_internal(znode_t *dzp, const char *name, vnode_t **vpp,
1327 struct componentname *cnp, int nameiop)
1328 {
1329 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1330 int error;
1331
1332 cnp->cn_nameptr = __DECONST(char *, name);
1333 cnp->cn_namelen = strlen(name);
1334 cnp->cn_nameiop = nameiop;
1335 cnp->cn_flags = ISLASTCN;
1336 #if __FreeBSD_version < 1400068
1337 cnp->cn_flags |= SAVENAME;
1338 #endif
1339 cnp->cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
1340 cnp->cn_cred = kcred;
1341 #if __FreeBSD_version < 1400037
1342 cnp->cn_thread = curthread;
1343 #endif
1344
1345 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay) {
1346 struct vop_lookup_args a;
1347
1348 a.a_gen.a_desc = &vop_lookup_desc;
1349 a.a_dvp = ZTOV(dzp);
1350 a.a_vpp = vpp;
1351 a.a_cnp = cnp;
1352 error = vfs_cache_lookup(&a);
1353 } else {
1354 error = zfs_lookup(ZTOV(dzp), name, vpp, cnp, nameiop, kcred, 0,
1355 B_FALSE);
1356 }
1357 #ifdef ZFS_DEBUG
1358 if (error) {
1359 printf("got error %d on name %s on op %d\n", error, name,
1360 nameiop);
1361 kdb_backtrace();
1362 }
1363 #endif
1364 return (error);
1365 }
1366
1367 int
1368 zfs_remove(znode_t *dzp, const char *name, cred_t *cr, int flags)
1369 {
1370 vnode_t *vp;
1371 int error;
1372 struct componentname cn;
1373
1374 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
1375 return (error);
1376
1377 error = zfs_remove_(ZTOV(dzp), vp, name, cr);
1378 vput(vp);
1379 return (error);
1380 }
1381 /*
1382 * Create a new directory and insert it into dvp using the name
1383 * provided. Return a pointer to the inserted directory.
1384 *
1385 * IN: dvp - vnode of directory to add subdir to.
1386 * dirname - name of new directory.
1387 * vap - attributes of new directory.
1388 * cr - credentials of caller.
1389 * ct - caller context
1390 * flags - case flags
1391 * vsecp - ACL to be set
1392 * mnt_ns - Unused on FreeBSD
1393 *
1394 * OUT: vpp - vnode of created directory.
1395 *
1396 * RETURN: 0 on success, error code on failure.
1397 *
1398 * Timestamps:
1399 * dvp - ctime|mtime updated
1400 * vp - ctime|mtime|atime updated
1401 */
1402 int
1403 zfs_mkdir(znode_t *dzp, const char *dirname, vattr_t *vap, znode_t **zpp,
1404 cred_t *cr, int flags, vsecattr_t *vsecp, zuserns_t *mnt_ns)
1405 {
1406 (void) flags, (void) vsecp;
1407 znode_t *zp;
1408 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1409 zilog_t *zilog;
1410 uint64_t txtype;
1411 dmu_tx_t *tx;
1412 int error;
1413 uid_t uid = crgetuid(cr);
1414 gid_t gid = crgetgid(cr);
1415 zfs_acl_ids_t acl_ids;
1416 boolean_t fuid_dirtied;
1417
1418 ASSERT3U(vap->va_type, ==, VDIR);
1419
1420 /*
1421 * If we have an ephemeral id, ACL, or XVATTR then
1422 * make sure file system is at proper version
1423 */
1424 if (zfsvfs->z_use_fuids == B_FALSE &&
1425 ((vap->va_mask & AT_XVATTR) ||
1426 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1427 return (SET_ERROR(EINVAL));
1428
1429 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1430 return (error);
1431 zilog = zfsvfs->z_log;
1432
1433 if (dzp->z_pflags & ZFS_XATTR) {
1434 zfs_exit(zfsvfs, FTAG);
1435 return (SET_ERROR(EINVAL));
1436 }
1437
1438 if (zfsvfs->z_utf8 && u8_validate(dirname,
1439 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1440 zfs_exit(zfsvfs, FTAG);
1441 return (SET_ERROR(EILSEQ));
1442 }
1443
1444 if (vap->va_mask & AT_XVATTR) {
1445 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
1446 crgetuid(cr), cr, vap->va_type)) != 0) {
1447 zfs_exit(zfsvfs, FTAG);
1448 return (error);
1449 }
1450 }
1451
1452 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1453 NULL, &acl_ids, NULL)) != 0) {
1454 zfs_exit(zfsvfs, FTAG);
1455 return (error);
1456 }
1457
1458 /*
1459 * First make sure the new directory doesn't exist.
1460 *
1461 * Existence is checked first to make sure we don't return
1462 * EACCES instead of EEXIST which can cause some applications
1463 * to fail.
1464 */
1465 *zpp = NULL;
1466
1467 if ((error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW))) {
1468 zfs_acl_ids_free(&acl_ids);
1469 zfs_exit(zfsvfs, FTAG);
1470 return (error);
1471 }
1472 ASSERT3P(zp, ==, NULL);
1473
1474 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr,
1475 mnt_ns))) {
1476 zfs_acl_ids_free(&acl_ids);
1477 zfs_exit(zfsvfs, FTAG);
1478 return (error);
1479 }
1480
1481 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) {
1482 zfs_acl_ids_free(&acl_ids);
1483 zfs_exit(zfsvfs, FTAG);
1484 return (SET_ERROR(EDQUOT));
1485 }
1486
1487 /*
1488 * Add a new entry to the directory.
1489 */
1490 getnewvnode_reserve_();
1491 tx = dmu_tx_create(zfsvfs->z_os);
1492 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1493 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1494 fuid_dirtied = zfsvfs->z_fuid_dirty;
1495 if (fuid_dirtied)
1496 zfs_fuid_txhold(zfsvfs, tx);
1497 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1498 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1499 acl_ids.z_aclp->z_acl_bytes);
1500 }
1501
1502 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1503 ZFS_SA_BASE_ATTR_SIZE);
1504
1505 error = dmu_tx_assign(tx, TXG_WAIT);
1506 if (error) {
1507 zfs_acl_ids_free(&acl_ids);
1508 dmu_tx_abort(tx);
1509 getnewvnode_drop_reserve();
1510 zfs_exit(zfsvfs, FTAG);
1511 return (error);
1512 }
1513
1514 /*
1515 * Create new node.
1516 */
1517 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1518
1519 if (fuid_dirtied)
1520 zfs_fuid_sync(zfsvfs, tx);
1521
1522 /*
1523 * Now put new name in parent dir.
1524 */
1525 (void) zfs_link_create(dzp, dirname, zp, tx, ZNEW);
1526
1527 *zpp = zp;
1528
1529 txtype = zfs_log_create_txtype(Z_DIR, NULL, vap);
1530 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL,
1531 acl_ids.z_fuidp, vap);
1532
1533 zfs_acl_ids_free(&acl_ids);
1534
1535 dmu_tx_commit(tx);
1536
1537 getnewvnode_drop_reserve();
1538
1539 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1540 zil_commit(zilog, 0);
1541
1542 zfs_exit(zfsvfs, FTAG);
1543 return (0);
1544 }
1545
1546 #if __FreeBSD_version < 1300124
1547 static void
1548 cache_vop_rmdir(struct vnode *dvp, struct vnode *vp)
1549 {
1550
1551 cache_purge(dvp);
1552 cache_purge(vp);
1553 }
1554 #endif
1555
1556 /*
1557 * Remove a directory subdir entry. If the current working
1558 * directory is the same as the subdir to be removed, the
1559 * remove will fail.
1560 *
1561 * IN: dvp - vnode of directory to remove from.
1562 * name - name of directory to be removed.
1563 * cwd - vnode of current working directory.
1564 * cr - credentials of caller.
1565 * ct - caller context
1566 * flags - case flags
1567 *
1568 * RETURN: 0 on success, error code on failure.
1569 *
1570 * Timestamps:
1571 * dvp - ctime|mtime updated
1572 */
1573 static int
1574 zfs_rmdir_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
1575 {
1576 znode_t *dzp = VTOZ(dvp);
1577 znode_t *zp = VTOZ(vp);
1578 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1579 zilog_t *zilog;
1580 dmu_tx_t *tx;
1581 int error;
1582
1583 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1584 return (error);
1585 if ((error = zfs_verify_zp(zp)) != 0) {
1586 zfs_exit(zfsvfs, FTAG);
1587 return (error);
1588 }
1589 zilog = zfsvfs->z_log;
1590
1591
1592 if ((error = zfs_zaccess_delete(dzp, zp, cr, NULL))) {
1593 goto out;
1594 }
1595
1596 if (vp->v_type != VDIR) {
1597 error = SET_ERROR(ENOTDIR);
1598 goto out;
1599 }
1600
1601 vnevent_rmdir(vp, dvp, name, ct);
1602
1603 tx = dmu_tx_create(zfsvfs->z_os);
1604 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1605 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1606 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1607 zfs_sa_upgrade_txholds(tx, zp);
1608 zfs_sa_upgrade_txholds(tx, dzp);
1609 dmu_tx_mark_netfree(tx);
1610 error = dmu_tx_assign(tx, TXG_WAIT);
1611 if (error) {
1612 dmu_tx_abort(tx);
1613 zfs_exit(zfsvfs, FTAG);
1614 return (error);
1615 }
1616
1617 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL);
1618
1619 if (error == 0) {
1620 uint64_t txtype = TX_RMDIR;
1621 zfs_log_remove(zilog, tx, txtype, dzp, name,
1622 ZFS_NO_OBJECT, B_FALSE);
1623 }
1624
1625 dmu_tx_commit(tx);
1626
1627 cache_vop_rmdir(dvp, vp);
1628 out:
1629 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1630 zil_commit(zilog, 0);
1631
1632 zfs_exit(zfsvfs, FTAG);
1633 return (error);
1634 }
1635
1636 int
1637 zfs_rmdir(znode_t *dzp, const char *name, znode_t *cwd, cred_t *cr, int flags)
1638 {
1639 struct componentname cn;
1640 vnode_t *vp;
1641 int error;
1642
1643 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
1644 return (error);
1645
1646 error = zfs_rmdir_(ZTOV(dzp), vp, name, cr);
1647 vput(vp);
1648 return (error);
1649 }
1650
1651 /*
1652 * Read as many directory entries as will fit into the provided
1653 * buffer from the given directory cursor position (specified in
1654 * the uio structure).
1655 *
1656 * IN: vp - vnode of directory to read.
1657 * uio - structure supplying read location, range info,
1658 * and return buffer.
1659 * cr - credentials of caller.
1660 * ct - caller context
1661 *
1662 * OUT: uio - updated offset and range, buffer filled.
1663 * eofp - set to true if end-of-file detected.
1664 * ncookies- number of entries in cookies
1665 * cookies - offsets to directory entries
1666 *
1667 * RETURN: 0 on success, error code on failure.
1668 *
1669 * Timestamps:
1670 * vp - atime updated
1671 *
1672 * Note that the low 4 bits of the cookie returned by zap is always zero.
1673 * This allows us to use the low range for "special" directory entries:
1674 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1675 * we use the offset 2 for the '.zfs' directory.
1676 */
1677 static int
1678 zfs_readdir(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, int *eofp,
1679 int *ncookies, cookie_t **cookies)
1680 {
1681 znode_t *zp = VTOZ(vp);
1682 iovec_t *iovp;
1683 dirent64_t *odp;
1684 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1685 objset_t *os;
1686 caddr_t outbuf;
1687 size_t bufsize;
1688 zap_cursor_t zc;
1689 zap_attribute_t zap;
1690 uint_t bytes_wanted;
1691 uint64_t offset; /* must be unsigned; checks for < 1 */
1692 uint64_t parent;
1693 int local_eof;
1694 int outcount;
1695 int error;
1696 uint8_t prefetch;
1697 uint8_t type;
1698 int ncooks;
1699 cookie_t *cooks = NULL;
1700
1701 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
1702 return (error);
1703
1704 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1705 &parent, sizeof (parent))) != 0) {
1706 zfs_exit(zfsvfs, FTAG);
1707 return (error);
1708 }
1709
1710 /*
1711 * If we are not given an eof variable,
1712 * use a local one.
1713 */
1714 if (eofp == NULL)
1715 eofp = &local_eof;
1716
1717 /*
1718 * Check for valid iov_len.
1719 */
1720 if (GET_UIO_STRUCT(uio)->uio_iov->iov_len <= 0) {
1721 zfs_exit(zfsvfs, FTAG);
1722 return (SET_ERROR(EINVAL));
1723 }
1724
1725 /*
1726 * Quit if directory has been removed (posix)
1727 */
1728 if ((*eofp = zp->z_unlinked) != 0) {
1729 zfs_exit(zfsvfs, FTAG);
1730 return (0);
1731 }
1732
1733 error = 0;
1734 os = zfsvfs->z_os;
1735 offset = zfs_uio_offset(uio);
1736 prefetch = zp->z_zn_prefetch;
1737
1738 /*
1739 * Initialize the iterator cursor.
1740 */
1741 if (offset <= 3) {
1742 /*
1743 * Start iteration from the beginning of the directory.
1744 */
1745 zap_cursor_init(&zc, os, zp->z_id);
1746 } else {
1747 /*
1748 * The offset is a serialized cursor.
1749 */
1750 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1751 }
1752
1753 /*
1754 * Get space to change directory entries into fs independent format.
1755 */
1756 iovp = GET_UIO_STRUCT(uio)->uio_iov;
1757 bytes_wanted = iovp->iov_len;
1758 if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1) {
1759 bufsize = bytes_wanted;
1760 outbuf = kmem_alloc(bufsize, KM_SLEEP);
1761 odp = (struct dirent64 *)outbuf;
1762 } else {
1763 bufsize = bytes_wanted;
1764 outbuf = NULL;
1765 odp = (struct dirent64 *)iovp->iov_base;
1766 }
1767
1768 if (ncookies != NULL) {
1769 /*
1770 * Minimum entry size is dirent size and 1 byte for a file name.
1771 */
1772 ncooks = zfs_uio_resid(uio) / (sizeof (struct dirent) -
1773 sizeof (((struct dirent *)NULL)->d_name) + 1);
1774 cooks = malloc(ncooks * sizeof (*cooks), M_TEMP, M_WAITOK);
1775 *cookies = cooks;
1776 *ncookies = ncooks;
1777 }
1778
1779 /*
1780 * Transform to file-system independent format
1781 */
1782 outcount = 0;
1783 while (outcount < bytes_wanted) {
1784 ino64_t objnum;
1785 ushort_t reclen;
1786 off64_t *next = NULL;
1787
1788 /*
1789 * Special case `.', `..', and `.zfs'.
1790 */
1791 if (offset == 0) {
1792 (void) strcpy(zap.za_name, ".");
1793 zap.za_normalization_conflict = 0;
1794 objnum = zp->z_id;
1795 type = DT_DIR;
1796 } else if (offset == 1) {
1797 (void) strcpy(zap.za_name, "..");
1798 zap.za_normalization_conflict = 0;
1799 objnum = parent;
1800 type = DT_DIR;
1801 } else if (offset == 2 && zfs_show_ctldir(zp)) {
1802 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1803 zap.za_normalization_conflict = 0;
1804 objnum = ZFSCTL_INO_ROOT;
1805 type = DT_DIR;
1806 } else {
1807 /*
1808 * Grab next entry.
1809 */
1810 if ((error = zap_cursor_retrieve(&zc, &zap))) {
1811 if ((*eofp = (error == ENOENT)) != 0)
1812 break;
1813 else
1814 goto update;
1815 }
1816
1817 if (zap.za_integer_length != 8 ||
1818 zap.za_num_integers != 1) {
1819 cmn_err(CE_WARN, "zap_readdir: bad directory "
1820 "entry, obj = %lld, offset = %lld\n",
1821 (u_longlong_t)zp->z_id,
1822 (u_longlong_t)offset);
1823 error = SET_ERROR(ENXIO);
1824 goto update;
1825 }
1826
1827 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
1828 /*
1829 * MacOS X can extract the object type here such as:
1830 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1831 */
1832 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1833 }
1834
1835 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1836
1837 /*
1838 * Will this entry fit in the buffer?
1839 */
1840 if (outcount + reclen > bufsize) {
1841 /*
1842 * Did we manage to fit anything in the buffer?
1843 */
1844 if (!outcount) {
1845 error = SET_ERROR(EINVAL);
1846 goto update;
1847 }
1848 break;
1849 }
1850 /*
1851 * Add normal entry:
1852 */
1853 odp->d_ino = objnum;
1854 odp->d_reclen = reclen;
1855 odp->d_namlen = strlen(zap.za_name);
1856 /* NOTE: d_off is the offset for the *next* entry. */
1857 next = &odp->d_off;
1858 strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
1859 odp->d_type = type;
1860 dirent_terminate(odp);
1861 odp = (dirent64_t *)((intptr_t)odp + reclen);
1862
1863 outcount += reclen;
1864
1865 ASSERT3S(outcount, <=, bufsize);
1866
1867 /* Prefetch znode */
1868 if (prefetch)
1869 dmu_prefetch(os, objnum, 0, 0, 0,
1870 ZIO_PRIORITY_SYNC_READ);
1871
1872 /*
1873 * Move to the next entry, fill in the previous offset.
1874 */
1875 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1876 zap_cursor_advance(&zc);
1877 offset = zap_cursor_serialize(&zc);
1878 } else {
1879 offset += 1;
1880 }
1881
1882 /* Fill the offset right after advancing the cursor. */
1883 if (next != NULL)
1884 *next = offset;
1885 if (cooks != NULL) {
1886 *cooks++ = offset;
1887 ncooks--;
1888 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
1889 }
1890 }
1891 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1892
1893 /* Subtract unused cookies */
1894 if (ncookies != NULL)
1895 *ncookies -= ncooks;
1896
1897 if (zfs_uio_segflg(uio) == UIO_SYSSPACE && zfs_uio_iovcnt(uio) == 1) {
1898 iovp->iov_base += outcount;
1899 iovp->iov_len -= outcount;
1900 zfs_uio_resid(uio) -= outcount;
1901 } else if ((error =
1902 zfs_uiomove(outbuf, (long)outcount, UIO_READ, uio))) {
1903 /*
1904 * Reset the pointer.
1905 */
1906 offset = zfs_uio_offset(uio);
1907 }
1908
1909 update:
1910 zap_cursor_fini(&zc);
1911 if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1)
1912 kmem_free(outbuf, bufsize);
1913
1914 if (error == ENOENT)
1915 error = 0;
1916
1917 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1918
1919 zfs_uio_setoffset(uio, offset);
1920 zfs_exit(zfsvfs, FTAG);
1921 if (error != 0 && cookies != NULL) {
1922 free(*cookies, M_TEMP);
1923 *cookies = NULL;
1924 *ncookies = 0;
1925 }
1926 return (error);
1927 }
1928
1929 /*
1930 * Get the requested file attributes and place them in the provided
1931 * vattr structure.
1932 *
1933 * IN: vp - vnode of file.
1934 * vap - va_mask identifies requested attributes.
1935 * If AT_XVATTR set, then optional attrs are requested
1936 * flags - ATTR_NOACLCHECK (CIFS server context)
1937 * cr - credentials of caller.
1938 *
1939 * OUT: vap - attribute values.
1940 *
1941 * RETURN: 0 (always succeeds).
1942 */
1943 static int
1944 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1945 {
1946 znode_t *zp = VTOZ(vp);
1947 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1948 int error = 0;
1949 uint32_t blksize;
1950 u_longlong_t nblocks;
1951 uint64_t mtime[2], ctime[2], crtime[2], rdev;
1952 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
1953 xoptattr_t *xoap = NULL;
1954 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
1955 sa_bulk_attr_t bulk[4];
1956 int count = 0;
1957
1958 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
1959 return (error);
1960
1961 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
1962
1963 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
1964 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
1965 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
1966 if (vp->v_type == VBLK || vp->v_type == VCHR)
1967 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
1968 &rdev, 8);
1969
1970 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
1971 zfs_exit(zfsvfs, FTAG);
1972 return (error);
1973 }
1974
1975 /*
1976 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1977 * Also, if we are the owner don't bother, since owner should
1978 * always be allowed to read basic attributes of file.
1979 */
1980 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
1981 (vap->va_uid != crgetuid(cr))) {
1982 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
1983 skipaclchk, cr, NULL))) {
1984 zfs_exit(zfsvfs, FTAG);
1985 return (error);
1986 }
1987 }
1988
1989 /*
1990 * Return all attributes. It's cheaper to provide the answer
1991 * than to determine whether we were asked the question.
1992 */
1993
1994 vap->va_type = IFTOVT(zp->z_mode);
1995 vap->va_mode = zp->z_mode & ~S_IFMT;
1996 vn_fsid(vp, vap);
1997 vap->va_nodeid = zp->z_id;
1998 vap->va_nlink = zp->z_links;
1999 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp) &&
2000 zp->z_links < ZFS_LINK_MAX)
2001 vap->va_nlink++;
2002 vap->va_size = zp->z_size;
2003 if (vp->v_type == VBLK || vp->v_type == VCHR)
2004 vap->va_rdev = zfs_cmpldev(rdev);
2005 vap->va_gen = zp->z_gen;
2006 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2007 vap->va_filerev = zp->z_seq;
2008
2009 /*
2010 * Add in any requested optional attributes and the create time.
2011 * Also set the corresponding bits in the returned attribute bitmap.
2012 */
2013 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2014 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2015 xoap->xoa_archive =
2016 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2017 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2018 }
2019
2020 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2021 xoap->xoa_readonly =
2022 ((zp->z_pflags & ZFS_READONLY) != 0);
2023 XVA_SET_RTN(xvap, XAT_READONLY);
2024 }
2025
2026 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2027 xoap->xoa_system =
2028 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2029 XVA_SET_RTN(xvap, XAT_SYSTEM);
2030 }
2031
2032 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2033 xoap->xoa_hidden =
2034 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2035 XVA_SET_RTN(xvap, XAT_HIDDEN);
2036 }
2037
2038 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2039 xoap->xoa_nounlink =
2040 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2041 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2042 }
2043
2044 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2045 xoap->xoa_immutable =
2046 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2047 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2048 }
2049
2050 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2051 xoap->xoa_appendonly =
2052 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2053 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2054 }
2055
2056 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2057 xoap->xoa_nodump =
2058 ((zp->z_pflags & ZFS_NODUMP) != 0);
2059 XVA_SET_RTN(xvap, XAT_NODUMP);
2060 }
2061
2062 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2063 xoap->xoa_opaque =
2064 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2065 XVA_SET_RTN(xvap, XAT_OPAQUE);
2066 }
2067
2068 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2069 xoap->xoa_av_quarantined =
2070 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2071 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2072 }
2073
2074 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2075 xoap->xoa_av_modified =
2076 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2077 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2078 }
2079
2080 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2081 vp->v_type == VREG) {
2082 zfs_sa_get_scanstamp(zp, xvap);
2083 }
2084
2085 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2086 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2087 XVA_SET_RTN(xvap, XAT_REPARSE);
2088 }
2089 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2090 xoap->xoa_generation = zp->z_gen;
2091 XVA_SET_RTN(xvap, XAT_GEN);
2092 }
2093
2094 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2095 xoap->xoa_offline =
2096 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2097 XVA_SET_RTN(xvap, XAT_OFFLINE);
2098 }
2099
2100 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2101 xoap->xoa_sparse =
2102 ((zp->z_pflags & ZFS_SPARSE) != 0);
2103 XVA_SET_RTN(xvap, XAT_SPARSE);
2104 }
2105
2106 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2107 xoap->xoa_projinherit =
2108 ((zp->z_pflags & ZFS_PROJINHERIT) != 0);
2109 XVA_SET_RTN(xvap, XAT_PROJINHERIT);
2110 }
2111
2112 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2113 xoap->xoa_projid = zp->z_projid;
2114 XVA_SET_RTN(xvap, XAT_PROJID);
2115 }
2116 }
2117
2118 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2119 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2120 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2121 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2122
2123
2124 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2125 vap->va_blksize = blksize;
2126 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2127
2128 if (zp->z_blksz == 0) {
2129 /*
2130 * Block size hasn't been set; suggest maximal I/O transfers.
2131 */
2132 vap->va_blksize = zfsvfs->z_max_blksz;
2133 }
2134
2135 zfs_exit(zfsvfs, FTAG);
2136 return (0);
2137 }
2138
2139 /*
2140 * Set the file attributes to the values contained in the
2141 * vattr structure.
2142 *
2143 * IN: zp - znode of file to be modified.
2144 * vap - new attribute values.
2145 * If AT_XVATTR set, then optional attrs are being set
2146 * flags - ATTR_UTIME set if non-default time values provided.
2147 * - ATTR_NOACLCHECK (CIFS context only).
2148 * cr - credentials of caller.
2149 * mnt_ns - Unused on FreeBSD
2150 *
2151 * RETURN: 0 on success, error code on failure.
2152 *
2153 * Timestamps:
2154 * vp - ctime updated, mtime updated if size changed.
2155 */
2156 int
2157 zfs_setattr(znode_t *zp, vattr_t *vap, int flags, cred_t *cr, zuserns_t *mnt_ns)
2158 {
2159 vnode_t *vp = ZTOV(zp);
2160 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2161 objset_t *os;
2162 zilog_t *zilog;
2163 dmu_tx_t *tx;
2164 vattr_t oldva;
2165 xvattr_t tmpxvattr;
2166 uint_t mask = vap->va_mask;
2167 uint_t saved_mask = 0;
2168 uint64_t saved_mode;
2169 int trim_mask = 0;
2170 uint64_t new_mode;
2171 uint64_t new_uid, new_gid;
2172 uint64_t xattr_obj;
2173 uint64_t mtime[2], ctime[2];
2174 uint64_t projid = ZFS_INVALID_PROJID;
2175 znode_t *attrzp;
2176 int need_policy = FALSE;
2177 int err, err2;
2178 zfs_fuid_info_t *fuidp = NULL;
2179 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2180 xoptattr_t *xoap;
2181 zfs_acl_t *aclp;
2182 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2183 boolean_t fuid_dirtied = B_FALSE;
2184 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2185 int count = 0, xattr_count = 0;
2186
2187 if (mask == 0)
2188 return (0);
2189
2190 if (mask & AT_NOSET)
2191 return (SET_ERROR(EINVAL));
2192
2193 if ((err = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
2194 return (err);
2195
2196 os = zfsvfs->z_os;
2197 zilog = zfsvfs->z_log;
2198
2199 /*
2200 * Make sure that if we have ephemeral uid/gid or xvattr specified
2201 * that file system is at proper version level
2202 */
2203
2204 if (zfsvfs->z_use_fuids == B_FALSE &&
2205 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2206 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2207 (mask & AT_XVATTR))) {
2208 zfs_exit(zfsvfs, FTAG);
2209 return (SET_ERROR(EINVAL));
2210 }
2211
2212 if (mask & AT_SIZE && vp->v_type == VDIR) {
2213 zfs_exit(zfsvfs, FTAG);
2214 return (SET_ERROR(EISDIR));
2215 }
2216
2217 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2218 zfs_exit(zfsvfs, FTAG);
2219 return (SET_ERROR(EINVAL));
2220 }
2221
2222 /*
2223 * If this is an xvattr_t, then get a pointer to the structure of
2224 * optional attributes. If this is NULL, then we have a vattr_t.
2225 */
2226 xoap = xva_getxoptattr(xvap);
2227
2228 xva_init(&tmpxvattr);
2229
2230 /*
2231 * Immutable files can only alter immutable bit and atime
2232 */
2233 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2234 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2235 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2236 zfs_exit(zfsvfs, FTAG);
2237 return (SET_ERROR(EPERM));
2238 }
2239
2240 /*
2241 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2242 */
2243
2244 /*
2245 * Verify timestamps doesn't overflow 32 bits.
2246 * ZFS can handle large timestamps, but 32bit syscalls can't
2247 * handle times greater than 2039. This check should be removed
2248 * once large timestamps are fully supported.
2249 */
2250 if (mask & (AT_ATIME | AT_MTIME)) {
2251 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2252 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2253 zfs_exit(zfsvfs, FTAG);
2254 return (SET_ERROR(EOVERFLOW));
2255 }
2256 }
2257 if (xoap != NULL && (mask & AT_XVATTR)) {
2258 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME) &&
2259 TIMESPEC_OVERFLOW(&vap->va_birthtime)) {
2260 zfs_exit(zfsvfs, FTAG);
2261 return (SET_ERROR(EOVERFLOW));
2262 }
2263
2264 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2265 if (!dmu_objset_projectquota_enabled(os) ||
2266 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode))) {
2267 zfs_exit(zfsvfs, FTAG);
2268 return (SET_ERROR(EOPNOTSUPP));
2269 }
2270
2271 projid = xoap->xoa_projid;
2272 if (unlikely(projid == ZFS_INVALID_PROJID)) {
2273 zfs_exit(zfsvfs, FTAG);
2274 return (SET_ERROR(EINVAL));
2275 }
2276
2277 if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID)
2278 projid = ZFS_INVALID_PROJID;
2279 else
2280 need_policy = TRUE;
2281 }
2282
2283 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) &&
2284 (xoap->xoa_projinherit !=
2285 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) &&
2286 (!dmu_objset_projectquota_enabled(os) ||
2287 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode)))) {
2288 zfs_exit(zfsvfs, FTAG);
2289 return (SET_ERROR(EOPNOTSUPP));
2290 }
2291 }
2292
2293 attrzp = NULL;
2294 aclp = NULL;
2295
2296 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2297 zfs_exit(zfsvfs, FTAG);
2298 return (SET_ERROR(EROFS));
2299 }
2300
2301 /*
2302 * First validate permissions
2303 */
2304
2305 if (mask & AT_SIZE) {
2306 /*
2307 * XXX - Note, we are not providing any open
2308 * mode flags here (like FNDELAY), so we may
2309 * block if there are locks present... this
2310 * should be addressed in openat().
2311 */
2312 /* XXX - would it be OK to generate a log record here? */
2313 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2314 if (err) {
2315 zfs_exit(zfsvfs, FTAG);
2316 return (err);
2317 }
2318 }
2319
2320 if (mask & (AT_ATIME|AT_MTIME) ||
2321 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2322 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2323 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2324 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2325 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2326 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2327 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2328 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2329 skipaclchk, cr, mnt_ns);
2330 }
2331
2332 if (mask & (AT_UID|AT_GID)) {
2333 int idmask = (mask & (AT_UID|AT_GID));
2334 int take_owner;
2335 int take_group;
2336
2337 /*
2338 * NOTE: even if a new mode is being set,
2339 * we may clear S_ISUID/S_ISGID bits.
2340 */
2341
2342 if (!(mask & AT_MODE))
2343 vap->va_mode = zp->z_mode;
2344
2345 /*
2346 * Take ownership or chgrp to group we are a member of
2347 */
2348
2349 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2350 take_group = (mask & AT_GID) &&
2351 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2352
2353 /*
2354 * If both AT_UID and AT_GID are set then take_owner and
2355 * take_group must both be set in order to allow taking
2356 * ownership.
2357 *
2358 * Otherwise, send the check through secpolicy_vnode_setattr()
2359 *
2360 */
2361
2362 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2363 ((idmask == AT_UID) && take_owner) ||
2364 ((idmask == AT_GID) && take_group)) {
2365 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2366 skipaclchk, cr, mnt_ns) == 0) {
2367 /*
2368 * Remove setuid/setgid for non-privileged users
2369 */
2370 secpolicy_setid_clear(vap, vp, cr);
2371 trim_mask = (mask & (AT_UID|AT_GID));
2372 } else {
2373 need_policy = TRUE;
2374 }
2375 } else {
2376 need_policy = TRUE;
2377 }
2378 }
2379
2380 oldva.va_mode = zp->z_mode;
2381 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2382 if (mask & AT_XVATTR) {
2383 /*
2384 * Update xvattr mask to include only those attributes
2385 * that are actually changing.
2386 *
2387 * the bits will be restored prior to actually setting
2388 * the attributes so the caller thinks they were set.
2389 */
2390 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2391 if (xoap->xoa_appendonly !=
2392 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2393 need_policy = TRUE;
2394 } else {
2395 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2396 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2397 }
2398 }
2399
2400 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2401 if (xoap->xoa_projinherit !=
2402 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) {
2403 need_policy = TRUE;
2404 } else {
2405 XVA_CLR_REQ(xvap, XAT_PROJINHERIT);
2406 XVA_SET_REQ(&tmpxvattr, XAT_PROJINHERIT);
2407 }
2408 }
2409
2410 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2411 if (xoap->xoa_nounlink !=
2412 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2413 need_policy = TRUE;
2414 } else {
2415 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2416 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2417 }
2418 }
2419
2420 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2421 if (xoap->xoa_immutable !=
2422 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2423 need_policy = TRUE;
2424 } else {
2425 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2426 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2427 }
2428 }
2429
2430 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2431 if (xoap->xoa_nodump !=
2432 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2433 need_policy = TRUE;
2434 } else {
2435 XVA_CLR_REQ(xvap, XAT_NODUMP);
2436 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2437 }
2438 }
2439
2440 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2441 if (xoap->xoa_av_modified !=
2442 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2443 need_policy = TRUE;
2444 } else {
2445 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2446 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2447 }
2448 }
2449
2450 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2451 if ((vp->v_type != VREG &&
2452 xoap->xoa_av_quarantined) ||
2453 xoap->xoa_av_quarantined !=
2454 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2455 need_policy = TRUE;
2456 } else {
2457 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2458 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2459 }
2460 }
2461
2462 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2463 zfs_exit(zfsvfs, FTAG);
2464 return (SET_ERROR(EPERM));
2465 }
2466
2467 if (need_policy == FALSE &&
2468 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2469 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2470 need_policy = TRUE;
2471 }
2472 }
2473
2474 if (mask & AT_MODE) {
2475 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr,
2476 mnt_ns) == 0) {
2477 err = secpolicy_setid_setsticky_clear(vp, vap,
2478 &oldva, cr);
2479 if (err) {
2480 zfs_exit(zfsvfs, FTAG);
2481 return (err);
2482 }
2483 trim_mask |= AT_MODE;
2484 } else {
2485 need_policy = TRUE;
2486 }
2487 }
2488
2489 if (need_policy) {
2490 /*
2491 * If trim_mask is set then take ownership
2492 * has been granted or write_acl is present and user
2493 * has the ability to modify mode. In that case remove
2494 * UID|GID and or MODE from mask so that
2495 * secpolicy_vnode_setattr() doesn't revoke it.
2496 */
2497
2498 if (trim_mask) {
2499 saved_mask = vap->va_mask;
2500 vap->va_mask &= ~trim_mask;
2501 if (trim_mask & AT_MODE) {
2502 /*
2503 * Save the mode, as secpolicy_vnode_setattr()
2504 * will overwrite it with ova.va_mode.
2505 */
2506 saved_mode = vap->va_mode;
2507 }
2508 }
2509 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2510 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2511 if (err) {
2512 zfs_exit(zfsvfs, FTAG);
2513 return (err);
2514 }
2515
2516 if (trim_mask) {
2517 vap->va_mask |= saved_mask;
2518 if (trim_mask & AT_MODE) {
2519 /*
2520 * Recover the mode after
2521 * secpolicy_vnode_setattr().
2522 */
2523 vap->va_mode = saved_mode;
2524 }
2525 }
2526 }
2527
2528 /*
2529 * secpolicy_vnode_setattr, or take ownership may have
2530 * changed va_mask
2531 */
2532 mask = vap->va_mask;
2533
2534 if ((mask & (AT_UID | AT_GID)) || projid != ZFS_INVALID_PROJID) {
2535 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2536 &xattr_obj, sizeof (xattr_obj));
2537
2538 if (err == 0 && xattr_obj) {
2539 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
2540 if (err == 0) {
2541 err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE);
2542 if (err != 0)
2543 vrele(ZTOV(attrzp));
2544 }
2545 if (err)
2546 goto out2;
2547 }
2548 if (mask & AT_UID) {
2549 new_uid = zfs_fuid_create(zfsvfs,
2550 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2551 if (new_uid != zp->z_uid &&
2552 zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT,
2553 new_uid)) {
2554 if (attrzp)
2555 vput(ZTOV(attrzp));
2556 err = SET_ERROR(EDQUOT);
2557 goto out2;
2558 }
2559 }
2560
2561 if (mask & AT_GID) {
2562 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
2563 cr, ZFS_GROUP, &fuidp);
2564 if (new_gid != zp->z_gid &&
2565 zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT,
2566 new_gid)) {
2567 if (attrzp)
2568 vput(ZTOV(attrzp));
2569 err = SET_ERROR(EDQUOT);
2570 goto out2;
2571 }
2572 }
2573
2574 if (projid != ZFS_INVALID_PROJID &&
2575 zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) {
2576 if (attrzp)
2577 vput(ZTOV(attrzp));
2578 err = SET_ERROR(EDQUOT);
2579 goto out2;
2580 }
2581 }
2582 tx = dmu_tx_create(os);
2583
2584 if (mask & AT_MODE) {
2585 uint64_t pmode = zp->z_mode;
2586 uint64_t acl_obj;
2587 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2588
2589 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
2590 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
2591 err = SET_ERROR(EPERM);
2592 goto out;
2593 }
2594
2595 if ((err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)))
2596 goto out;
2597
2598 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2599 /*
2600 * Are we upgrading ACL from old V0 format
2601 * to V1 format?
2602 */
2603 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
2604 zfs_znode_acl_version(zp) ==
2605 ZFS_ACL_VERSION_INITIAL) {
2606 dmu_tx_hold_free(tx, acl_obj, 0,
2607 DMU_OBJECT_END);
2608 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2609 0, aclp->z_acl_bytes);
2610 } else {
2611 dmu_tx_hold_write(tx, acl_obj, 0,
2612 aclp->z_acl_bytes);
2613 }
2614 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2615 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2616 0, aclp->z_acl_bytes);
2617 }
2618 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2619 } else {
2620 if (((mask & AT_XVATTR) &&
2621 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
2622 (projid != ZFS_INVALID_PROJID &&
2623 !(zp->z_pflags & ZFS_PROJID)))
2624 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2625 else
2626 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2627 }
2628
2629 if (attrzp) {
2630 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2631 }
2632
2633 fuid_dirtied = zfsvfs->z_fuid_dirty;
2634 if (fuid_dirtied)
2635 zfs_fuid_txhold(zfsvfs, tx);
2636
2637 zfs_sa_upgrade_txholds(tx, zp);
2638
2639 err = dmu_tx_assign(tx, TXG_WAIT);
2640 if (err)
2641 goto out;
2642
2643 count = 0;
2644 /*
2645 * Set each attribute requested.
2646 * We group settings according to the locks they need to acquire.
2647 *
2648 * Note: you cannot set ctime directly, although it will be
2649 * updated as a side-effect of calling this function.
2650 */
2651
2652 if (projid != ZFS_INVALID_PROJID && !(zp->z_pflags & ZFS_PROJID)) {
2653 /*
2654 * For the existed object that is upgraded from old system,
2655 * its on-disk layout has no slot for the project ID attribute.
2656 * But quota accounting logic needs to access related slots by
2657 * offset directly. So we need to adjust old objects' layout
2658 * to make the project ID to some unified and fixed offset.
2659 */
2660 if (attrzp)
2661 err = sa_add_projid(attrzp->z_sa_hdl, tx, projid);
2662 if (err == 0)
2663 err = sa_add_projid(zp->z_sa_hdl, tx, projid);
2664
2665 if (unlikely(err == EEXIST))
2666 err = 0;
2667 else if (err != 0)
2668 goto out;
2669 else
2670 projid = ZFS_INVALID_PROJID;
2671 }
2672
2673 if (mask & (AT_UID|AT_GID|AT_MODE))
2674 mutex_enter(&zp->z_acl_lock);
2675
2676 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
2677 &zp->z_pflags, sizeof (zp->z_pflags));
2678
2679 if (attrzp) {
2680 if (mask & (AT_UID|AT_GID|AT_MODE))
2681 mutex_enter(&attrzp->z_acl_lock);
2682 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2683 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
2684 sizeof (attrzp->z_pflags));
2685 if (projid != ZFS_INVALID_PROJID) {
2686 attrzp->z_projid = projid;
2687 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2688 SA_ZPL_PROJID(zfsvfs), NULL, &attrzp->z_projid,
2689 sizeof (attrzp->z_projid));
2690 }
2691 }
2692
2693 if (mask & (AT_UID|AT_GID)) {
2694
2695 if (mask & AT_UID) {
2696 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
2697 &new_uid, sizeof (new_uid));
2698 zp->z_uid = new_uid;
2699 if (attrzp) {
2700 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2701 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
2702 sizeof (new_uid));
2703 attrzp->z_uid = new_uid;
2704 }
2705 }
2706
2707 if (mask & AT_GID) {
2708 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
2709 NULL, &new_gid, sizeof (new_gid));
2710 zp->z_gid = new_gid;
2711 if (attrzp) {
2712 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2713 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
2714 sizeof (new_gid));
2715 attrzp->z_gid = new_gid;
2716 }
2717 }
2718 if (!(mask & AT_MODE)) {
2719 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
2720 NULL, &new_mode, sizeof (new_mode));
2721 new_mode = zp->z_mode;
2722 }
2723 err = zfs_acl_chown_setattr(zp);
2724 ASSERT0(err);
2725 if (attrzp) {
2726 vn_seqc_write_begin(ZTOV(attrzp));
2727 err = zfs_acl_chown_setattr(attrzp);
2728 vn_seqc_write_end(ZTOV(attrzp));
2729 ASSERT0(err);
2730 }
2731 }
2732
2733 if (mask & AT_MODE) {
2734 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
2735 &new_mode, sizeof (new_mode));
2736 zp->z_mode = new_mode;
2737 ASSERT3P(aclp, !=, NULL);
2738 err = zfs_aclset_common(zp, aclp, cr, tx);
2739 ASSERT0(err);
2740 if (zp->z_acl_cached)
2741 zfs_acl_free(zp->z_acl_cached);
2742 zp->z_acl_cached = aclp;
2743 aclp = NULL;
2744 }
2745
2746
2747 if (mask & AT_ATIME) {
2748 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2749 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
2750 &zp->z_atime, sizeof (zp->z_atime));
2751 }
2752
2753 if (mask & AT_MTIME) {
2754 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
2755 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
2756 mtime, sizeof (mtime));
2757 }
2758
2759 if (projid != ZFS_INVALID_PROJID) {
2760 zp->z_projid = projid;
2761 SA_ADD_BULK_ATTR(bulk, count,
2762 SA_ZPL_PROJID(zfsvfs), NULL, &zp->z_projid,
2763 sizeof (zp->z_projid));
2764 }
2765
2766 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2767 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
2768 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
2769 NULL, mtime, sizeof (mtime));
2770 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
2771 &ctime, sizeof (ctime));
2772 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
2773 } else if (mask != 0) {
2774 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
2775 &ctime, sizeof (ctime));
2776 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime);
2777 if (attrzp) {
2778 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2779 SA_ZPL_CTIME(zfsvfs), NULL,
2780 &ctime, sizeof (ctime));
2781 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2782 mtime, ctime);
2783 }
2784 }
2785
2786 /*
2787 * Do this after setting timestamps to prevent timestamp
2788 * update from toggling bit
2789 */
2790
2791 if (xoap && (mask & AT_XVATTR)) {
2792
2793 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
2794 xoap->xoa_createtime = vap->va_birthtime;
2795 /*
2796 * restore trimmed off masks
2797 * so that return masks can be set for caller.
2798 */
2799
2800 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
2801 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2802 }
2803 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
2804 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2805 }
2806 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
2807 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2808 }
2809 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
2810 XVA_SET_REQ(xvap, XAT_NODUMP);
2811 }
2812 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
2813 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2814 }
2815 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
2816 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2817 }
2818 if (XVA_ISSET_REQ(&tmpxvattr, XAT_PROJINHERIT)) {
2819 XVA_SET_REQ(xvap, XAT_PROJINHERIT);
2820 }
2821
2822 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2823 ASSERT3S(vp->v_type, ==, VREG);
2824
2825 zfs_xvattr_set(zp, xvap, tx);
2826 }
2827
2828 if (fuid_dirtied)
2829 zfs_fuid_sync(zfsvfs, tx);
2830
2831 if (mask != 0)
2832 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2833
2834 if (mask & (AT_UID|AT_GID|AT_MODE))
2835 mutex_exit(&zp->z_acl_lock);
2836
2837 if (attrzp) {
2838 if (mask & (AT_UID|AT_GID|AT_MODE))
2839 mutex_exit(&attrzp->z_acl_lock);
2840 }
2841 out:
2842 if (err == 0 && attrzp) {
2843 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
2844 xattr_count, tx);
2845 ASSERT0(err2);
2846 }
2847
2848 if (attrzp)
2849 vput(ZTOV(attrzp));
2850
2851 if (aclp)
2852 zfs_acl_free(aclp);
2853
2854 if (fuidp) {
2855 zfs_fuid_info_free(fuidp);
2856 fuidp = NULL;
2857 }
2858
2859 if (err) {
2860 dmu_tx_abort(tx);
2861 } else {
2862 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2863 dmu_tx_commit(tx);
2864 }
2865
2866 out2:
2867 if (os->os_sync == ZFS_SYNC_ALWAYS)
2868 zil_commit(zilog, 0);
2869
2870 zfs_exit(zfsvfs, FTAG);
2871 return (err);
2872 }
2873
2874 /*
2875 * Look up the directory entries corresponding to the source and target
2876 * directory/name pairs.
2877 */
2878 static int
2879 zfs_rename_relock_lookup(znode_t *sdzp, const struct componentname *scnp,
2880 znode_t **szpp, znode_t *tdzp, const struct componentname *tcnp,
2881 znode_t **tzpp)
2882 {
2883 zfsvfs_t *zfsvfs;
2884 znode_t *szp, *tzp;
2885 int error;
2886
2887 /*
2888 * Before using sdzp and tdzp we must ensure that they are live.
2889 * As a porting legacy from illumos we have two things to worry
2890 * about. One is typical for FreeBSD and it is that the vnode is
2891 * not reclaimed (doomed). The other is that the znode is live.
2892 * The current code can invalidate the znode without acquiring the
2893 * corresponding vnode lock if the object represented by the znode
2894 * and vnode is no longer valid after a rollback or receive operation.
2895 * z_teardown_lock hidden behind zfs_enter and zfs_exit is the lock
2896 * that protects the znodes from the invalidation.
2897 */
2898 zfsvfs = sdzp->z_zfsvfs;
2899 ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs);
2900 if ((error = zfs_enter_verify_zp(zfsvfs, sdzp, FTAG)) != 0)
2901 return (error);
2902 if ((error = zfs_verify_zp(tdzp)) != 0) {
2903 zfs_exit(zfsvfs, FTAG);
2904 return (error);
2905 }
2906
2907 /*
2908 * Re-resolve svp to be certain it still exists and fetch the
2909 * correct vnode.
2910 */
2911 error = zfs_dirent_lookup(sdzp, scnp->cn_nameptr, &szp, ZEXISTS);
2912 if (error != 0) {
2913 /* Source entry invalid or not there. */
2914 if ((scnp->cn_flags & ISDOTDOT) != 0 ||
2915 (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.'))
2916 error = SET_ERROR(EINVAL);
2917 goto out;
2918 }
2919 *szpp = szp;
2920
2921 /*
2922 * Re-resolve tvp, if it disappeared we just carry on.
2923 */
2924 error = zfs_dirent_lookup(tdzp, tcnp->cn_nameptr, &tzp, 0);
2925 if (error != 0) {
2926 vrele(ZTOV(szp));
2927 if ((tcnp->cn_flags & ISDOTDOT) != 0)
2928 error = SET_ERROR(EINVAL);
2929 goto out;
2930 }
2931 *tzpp = tzp;
2932 out:
2933 zfs_exit(zfsvfs, FTAG);
2934 return (error);
2935 }
2936
2937 /*
2938 * We acquire all but fdvp locks using non-blocking acquisitions. If we
2939 * fail to acquire any lock in the path we will drop all held locks,
2940 * acquire the new lock in a blocking fashion, and then release it and
2941 * restart the rename. This acquire/release step ensures that we do not
2942 * spin on a lock waiting for release. On error release all vnode locks
2943 * and decrement references the way tmpfs_rename() would do.
2944 */
2945 static int
2946 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp,
2947 struct vnode *tdvp, struct vnode **tvpp,
2948 const struct componentname *scnp, const struct componentname *tcnp)
2949 {
2950 struct vnode *nvp, *svp, *tvp;
2951 znode_t *sdzp, *tdzp, *szp, *tzp;
2952 int error;
2953
2954 VOP_UNLOCK1(tdvp);
2955 if (*tvpp != NULL && *tvpp != tdvp)
2956 VOP_UNLOCK1(*tvpp);
2957
2958 relock:
2959 error = vn_lock(sdvp, LK_EXCLUSIVE);
2960 if (error)
2961 goto out;
2962 error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT);
2963 if (error != 0) {
2964 VOP_UNLOCK1(sdvp);
2965 if (error != EBUSY)
2966 goto out;
2967 error = vn_lock(tdvp, LK_EXCLUSIVE);
2968 if (error)
2969 goto out;
2970 VOP_UNLOCK1(tdvp);
2971 goto relock;
2972 }
2973 tdzp = VTOZ(tdvp);
2974 sdzp = VTOZ(sdvp);
2975
2976 error = zfs_rename_relock_lookup(sdzp, scnp, &szp, tdzp, tcnp, &tzp);
2977 if (error != 0) {
2978 VOP_UNLOCK1(sdvp);
2979 VOP_UNLOCK1(tdvp);
2980 goto out;
2981 }
2982 svp = ZTOV(szp);
2983 tvp = tzp != NULL ? ZTOV(tzp) : NULL;
2984
2985 /*
2986 * Now try acquire locks on svp and tvp.
2987 */
2988 nvp = svp;
2989 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
2990 if (error != 0) {
2991 VOP_UNLOCK1(sdvp);
2992 VOP_UNLOCK1(tdvp);
2993 if (tvp != NULL)
2994 vrele(tvp);
2995 if (error != EBUSY) {
2996 vrele(nvp);
2997 goto out;
2998 }
2999 error = vn_lock(nvp, LK_EXCLUSIVE);
3000 if (error != 0) {
3001 vrele(nvp);
3002 goto out;
3003 }
3004 VOP_UNLOCK1(nvp);
3005 /*
3006 * Concurrent rename race.
3007 * XXX ?
3008 */
3009 if (nvp == tdvp) {
3010 vrele(nvp);
3011 error = SET_ERROR(EINVAL);
3012 goto out;
3013 }
3014 vrele(*svpp);
3015 *svpp = nvp;
3016 goto relock;
3017 }
3018 vrele(*svpp);
3019 *svpp = nvp;
3020
3021 if (*tvpp != NULL)
3022 vrele(*tvpp);
3023 *tvpp = NULL;
3024 if (tvp != NULL) {
3025 nvp = tvp;
3026 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3027 if (error != 0) {
3028 VOP_UNLOCK1(sdvp);
3029 VOP_UNLOCK1(tdvp);
3030 VOP_UNLOCK1(*svpp);
3031 if (error != EBUSY) {
3032 vrele(nvp);
3033 goto out;
3034 }
3035 error = vn_lock(nvp, LK_EXCLUSIVE);
3036 if (error != 0) {
3037 vrele(nvp);
3038 goto out;
3039 }
3040 vput(nvp);
3041 goto relock;
3042 }
3043 *tvpp = nvp;
3044 }
3045
3046 return (0);
3047
3048 out:
3049 return (error);
3050 }
3051
3052 /*
3053 * Note that we must use VRELE_ASYNC in this function as it walks
3054 * up the directory tree and vrele may need to acquire an exclusive
3055 * lock if a last reference to a vnode is dropped.
3056 */
3057 static int
3058 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp)
3059 {
3060 zfsvfs_t *zfsvfs;
3061 znode_t *zp, *zp1;
3062 uint64_t parent;
3063 int error;
3064
3065 zfsvfs = tdzp->z_zfsvfs;
3066 if (tdzp == szp)
3067 return (SET_ERROR(EINVAL));
3068 if (tdzp == sdzp)
3069 return (0);
3070 if (tdzp->z_id == zfsvfs->z_root)
3071 return (0);
3072 zp = tdzp;
3073 for (;;) {
3074 ASSERT(!zp->z_unlinked);
3075 if ((error = sa_lookup(zp->z_sa_hdl,
3076 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
3077 break;
3078
3079 if (parent == szp->z_id) {
3080 error = SET_ERROR(EINVAL);
3081 break;
3082 }
3083 if (parent == zfsvfs->z_root)
3084 break;
3085 if (parent == sdzp->z_id)
3086 break;
3087
3088 error = zfs_zget(zfsvfs, parent, &zp1);
3089 if (error != 0)
3090 break;
3091
3092 if (zp != tdzp)
3093 VN_RELE_ASYNC(ZTOV(zp),
3094 dsl_pool_zrele_taskq(
3095 dmu_objset_pool(zfsvfs->z_os)));
3096 zp = zp1;
3097 }
3098
3099 if (error == ENOTDIR)
3100 panic("checkpath: .. not a directory\n");
3101 if (zp != tdzp)
3102 VN_RELE_ASYNC(ZTOV(zp),
3103 dsl_pool_zrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3104 return (error);
3105 }
3106
3107 #if __FreeBSD_version < 1300124
3108 static void
3109 cache_vop_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
3110 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
3111 {
3112
3113 cache_purge(fvp);
3114 if (tvp != NULL)
3115 cache_purge(tvp);
3116 cache_purge_negative(tdvp);
3117 }
3118 #endif
3119
3120 static int
3121 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3122 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3123 cred_t *cr);
3124
3125 /*
3126 * Move an entry from the provided source directory to the target
3127 * directory. Change the entry name as indicated.
3128 *
3129 * IN: sdvp - Source directory containing the "old entry".
3130 * scnp - Old entry name.
3131 * tdvp - Target directory to contain the "new entry".
3132 * tcnp - New entry name.
3133 * cr - credentials of caller.
3134 * INOUT: svpp - Source file
3135 * tvpp - Target file, may point to NULL initially
3136 *
3137 * RETURN: 0 on success, error code on failure.
3138 *
3139 * Timestamps:
3140 * sdvp,tdvp - ctime|mtime updated
3141 */
3142 static int
3143 zfs_do_rename(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3144 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3145 cred_t *cr)
3146 {
3147 int error;
3148
3149 ASSERT_VOP_ELOCKED(tdvp, __func__);
3150 if (*tvpp != NULL)
3151 ASSERT_VOP_ELOCKED(*tvpp, __func__);
3152
3153 /* Reject renames across filesystems. */
3154 if ((*svpp)->v_mount != tdvp->v_mount ||
3155 ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) {
3156 error = SET_ERROR(EXDEV);
3157 goto out;
3158 }
3159
3160 if (zfsctl_is_node(tdvp)) {
3161 error = SET_ERROR(EXDEV);
3162 goto out;
3163 }
3164
3165 /*
3166 * Lock all four vnodes to ensure safety and semantics of renaming.
3167 */
3168 error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp);
3169 if (error != 0) {
3170 /* no vnodes are locked in the case of error here */
3171 return (error);
3172 }
3173
3174 error = zfs_do_rename_impl(sdvp, svpp, scnp, tdvp, tvpp, tcnp, cr);
3175 VOP_UNLOCK1(sdvp);
3176 VOP_UNLOCK1(*svpp);
3177 out:
3178 if (*tvpp != NULL)
3179 VOP_UNLOCK1(*tvpp);
3180 if (tdvp != *tvpp)
3181 VOP_UNLOCK1(tdvp);
3182
3183 return (error);
3184 }
3185
3186 static int
3187 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3188 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3189 cred_t *cr)
3190 {
3191 dmu_tx_t *tx;
3192 zfsvfs_t *zfsvfs;
3193 zilog_t *zilog;
3194 znode_t *tdzp, *sdzp, *tzp, *szp;
3195 const char *snm = scnp->cn_nameptr;
3196 const char *tnm = tcnp->cn_nameptr;
3197 int error;
3198
3199 tdzp = VTOZ(tdvp);
3200 sdzp = VTOZ(sdvp);
3201 zfsvfs = tdzp->z_zfsvfs;
3202
3203 if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0)
3204 return (error);
3205 if ((error = zfs_verify_zp(sdzp)) != 0) {
3206 zfs_exit(zfsvfs, FTAG);
3207 return (error);
3208 }
3209 zilog = zfsvfs->z_log;
3210
3211 if (zfsvfs->z_utf8 && u8_validate(tnm,
3212 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3213 error = SET_ERROR(EILSEQ);
3214 goto out;
3215 }
3216
3217 /* If source and target are the same file, there is nothing to do. */
3218 if ((*svpp) == (*tvpp)) {
3219 error = 0;
3220 goto out;
3221 }
3222
3223 if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) ||
3224 ((*tvpp) != NULL && (*tvpp)->v_type == VDIR &&
3225 (*tvpp)->v_mountedhere != NULL)) {
3226 error = SET_ERROR(EXDEV);
3227 goto out;
3228 }
3229
3230 szp = VTOZ(*svpp);
3231 if ((error = zfs_verify_zp(szp)) != 0) {
3232 zfs_exit(zfsvfs, FTAG);
3233 return (error);
3234 }
3235 tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp);
3236 if (tzp != NULL) {
3237 if ((error = zfs_verify_zp(tzp)) != 0) {
3238 zfs_exit(zfsvfs, FTAG);
3239 return (error);
3240 }
3241 }
3242
3243 /*
3244 * This is to prevent the creation of links into attribute space
3245 * by renaming a linked file into/outof an attribute directory.
3246 * See the comment in zfs_link() for why this is considered bad.
3247 */
3248 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3249 error = SET_ERROR(EINVAL);
3250 goto out;
3251 }
3252
3253 /*
3254 * If we are using project inheritance, means if the directory has
3255 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3256 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3257 * such case, we only allow renames into our tree when the project
3258 * IDs are the same.
3259 */
3260 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3261 tdzp->z_projid != szp->z_projid) {
3262 error = SET_ERROR(EXDEV);
3263 goto out;
3264 }
3265
3266 /*
3267 * Must have write access at the source to remove the old entry
3268 * and write access at the target to create the new entry.
3269 * Note that if target and source are the same, this can be
3270 * done in a single check.
3271 */
3272 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr, NULL)))
3273 goto out;
3274
3275 if ((*svpp)->v_type == VDIR) {
3276 /*
3277 * Avoid ".", "..", and aliases of "." for obvious reasons.
3278 */
3279 if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') ||
3280 sdzp == szp ||
3281 (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
3282 error = EINVAL;
3283 goto out;
3284 }
3285
3286 /*
3287 * Check to make sure rename is valid.
3288 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3289 */
3290 if ((error = zfs_rename_check(szp, sdzp, tdzp)))
3291 goto out;
3292 }
3293
3294 /*
3295 * Does target exist?
3296 */
3297 if (tzp) {
3298 /*
3299 * Source and target must be the same type.
3300 */
3301 if ((*svpp)->v_type == VDIR) {
3302 if ((*tvpp)->v_type != VDIR) {
3303 error = SET_ERROR(ENOTDIR);
3304 goto out;
3305 } else {
3306 cache_purge(tdvp);
3307 if (sdvp != tdvp)
3308 cache_purge(sdvp);
3309 }
3310 } else {
3311 if ((*tvpp)->v_type == VDIR) {
3312 error = SET_ERROR(EISDIR);
3313 goto out;
3314 }
3315 }
3316 }
3317
3318 vn_seqc_write_begin(*svpp);
3319 vn_seqc_write_begin(sdvp);
3320 if (*tvpp != NULL)
3321 vn_seqc_write_begin(*tvpp);
3322 if (tdvp != *tvpp)
3323 vn_seqc_write_begin(tdvp);
3324
3325 vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct);
3326 if (tzp)
3327 vnevent_rename_dest(*tvpp, tdvp, tnm, ct);
3328
3329 /*
3330 * notify the target directory if it is not the same
3331 * as source directory.
3332 */
3333 if (tdvp != sdvp) {
3334 vnevent_rename_dest_dir(tdvp, ct);
3335 }
3336
3337 tx = dmu_tx_create(zfsvfs->z_os);
3338 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3339 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3340 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3341 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3342 if (sdzp != tdzp) {
3343 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3344 zfs_sa_upgrade_txholds(tx, tdzp);
3345 }
3346 if (tzp) {
3347 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3348 zfs_sa_upgrade_txholds(tx, tzp);
3349 }
3350
3351 zfs_sa_upgrade_txholds(tx, szp);
3352 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3353 error = dmu_tx_assign(tx, TXG_WAIT);
3354 if (error) {
3355 dmu_tx_abort(tx);
3356 goto out_seq;
3357 }
3358
3359 if (tzp) /* Attempt to remove the existing target */
3360 error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL);
3361
3362 if (error == 0) {
3363 error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING);
3364 if (error == 0) {
3365 szp->z_pflags |= ZFS_AV_MODIFIED;
3366
3367 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3368 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3369 ASSERT0(error);
3370
3371 error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING,
3372 NULL);
3373 if (error == 0) {
3374 zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
3375 snm, tdzp, tnm, szp);
3376
3377 /*
3378 * Update path information for the target vnode
3379 */
3380 vn_renamepath(tdvp, *svpp, tnm, strlen(tnm));
3381 } else {
3382 /*
3383 * At this point, we have successfully created
3384 * the target name, but have failed to remove
3385 * the source name. Since the create was done
3386 * with the ZRENAMING flag, there are
3387 * complications; for one, the link count is
3388 * wrong. The easiest way to deal with this
3389 * is to remove the newly created target, and
3390 * return the original error. This must
3391 * succeed; fortunately, it is very unlikely to
3392 * fail, since we just created it.
3393 */
3394 VERIFY0(zfs_link_destroy(tdzp, tnm, szp, tx,
3395 ZRENAMING, NULL));
3396 }
3397 }
3398 if (error == 0) {
3399 cache_vop_rename(sdvp, *svpp, tdvp, *tvpp, scnp, tcnp);
3400 }
3401 }
3402
3403 dmu_tx_commit(tx);
3404
3405 out_seq:
3406 vn_seqc_write_end(*svpp);
3407 vn_seqc_write_end(sdvp);
3408 if (*tvpp != NULL)
3409 vn_seqc_write_end(*tvpp);
3410 if (tdvp != *tvpp)
3411 vn_seqc_write_end(tdvp);
3412
3413 out:
3414 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3415 zil_commit(zilog, 0);
3416 zfs_exit(zfsvfs, FTAG);
3417
3418 return (error);
3419 }
3420
3421 int
3422 zfs_rename(znode_t *sdzp, const char *sname, znode_t *tdzp, const char *tname,
3423 cred_t *cr, int flags, zuserns_t *mnt_ns)
3424 {
3425 struct componentname scn, tcn;
3426 vnode_t *sdvp, *tdvp;
3427 vnode_t *svp, *tvp;
3428 int error;
3429 svp = tvp = NULL;
3430
3431 sdvp = ZTOV(sdzp);
3432 tdvp = ZTOV(tdzp);
3433 error = zfs_lookup_internal(sdzp, sname, &svp, &scn, DELETE);
3434 if (sdzp->z_zfsvfs->z_replay == B_FALSE)
3435 VOP_UNLOCK1(sdvp);
3436 if (error != 0)
3437 goto fail;
3438 VOP_UNLOCK1(svp);
3439
3440 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
3441 error = zfs_lookup_internal(tdzp, tname, &tvp, &tcn, RENAME);
3442 if (error == EJUSTRETURN)
3443 tvp = NULL;
3444 else if (error != 0) {
3445 VOP_UNLOCK1(tdvp);
3446 goto fail;
3447 }
3448
3449 error = zfs_do_rename(sdvp, &svp, &scn, tdvp, &tvp, &tcn, cr);
3450 fail:
3451 if (svp != NULL)
3452 vrele(svp);
3453 if (tvp != NULL)
3454 vrele(tvp);
3455
3456 return (error);
3457 }
3458
3459 /*
3460 * Insert the indicated symbolic reference entry into the directory.
3461 *
3462 * IN: dvp - Directory to contain new symbolic link.
3463 * link - Name for new symlink entry.
3464 * vap - Attributes of new entry.
3465 * cr - credentials of caller.
3466 * ct - caller context
3467 * flags - case flags
3468 * mnt_ns - Unused on FreeBSD
3469 *
3470 * RETURN: 0 on success, error code on failure.
3471 *
3472 * Timestamps:
3473 * dvp - ctime|mtime updated
3474 */
3475 int
3476 zfs_symlink(znode_t *dzp, const char *name, vattr_t *vap,
3477 const char *link, znode_t **zpp, cred_t *cr, int flags, zuserns_t *mnt_ns)
3478 {
3479 (void) flags;
3480 znode_t *zp;
3481 dmu_tx_t *tx;
3482 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3483 zilog_t *zilog;
3484 uint64_t len = strlen(link);
3485 int error;
3486 zfs_acl_ids_t acl_ids;
3487 boolean_t fuid_dirtied;
3488 uint64_t txtype = TX_SYMLINK;
3489
3490 ASSERT3S(vap->va_type, ==, VLNK);
3491
3492 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
3493 return (error);
3494 zilog = zfsvfs->z_log;
3495
3496 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3497 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3498 zfs_exit(zfsvfs, FTAG);
3499 return (SET_ERROR(EILSEQ));
3500 }
3501
3502 if (len > MAXPATHLEN) {
3503 zfs_exit(zfsvfs, FTAG);
3504 return (SET_ERROR(ENAMETOOLONG));
3505 }
3506
3507 if ((error = zfs_acl_ids_create(dzp, 0,
3508 vap, cr, NULL, &acl_ids, NULL)) != 0) {
3509 zfs_exit(zfsvfs, FTAG);
3510 return (error);
3511 }
3512
3513 /*
3514 * Attempt to lock directory; fail if entry already exists.
3515 */
3516 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
3517 if (error) {
3518 zfs_acl_ids_free(&acl_ids);
3519 zfs_exit(zfsvfs, FTAG);
3520 return (error);
3521 }
3522
3523 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) {
3524 zfs_acl_ids_free(&acl_ids);
3525 zfs_exit(zfsvfs, FTAG);
3526 return (error);
3527 }
3528
3529 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids,
3530 0 /* projid */)) {
3531 zfs_acl_ids_free(&acl_ids);
3532 zfs_exit(zfsvfs, FTAG);
3533 return (SET_ERROR(EDQUOT));
3534 }
3535
3536 getnewvnode_reserve_();
3537 tx = dmu_tx_create(zfsvfs->z_os);
3538 fuid_dirtied = zfsvfs->z_fuid_dirty;
3539 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3540 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3541 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3542 ZFS_SA_BASE_ATTR_SIZE + len);
3543 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3544 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3545 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3546 acl_ids.z_aclp->z_acl_bytes);
3547 }
3548 if (fuid_dirtied)
3549 zfs_fuid_txhold(zfsvfs, tx);
3550 error = dmu_tx_assign(tx, TXG_WAIT);
3551 if (error) {
3552 zfs_acl_ids_free(&acl_ids);
3553 dmu_tx_abort(tx);
3554 getnewvnode_drop_reserve();
3555 zfs_exit(zfsvfs, FTAG);
3556 return (error);
3557 }
3558
3559 /*
3560 * Create a new object for the symlink.
3561 * for version 4 ZPL datasets the symlink will be an SA attribute
3562 */
3563 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3564
3565 if (fuid_dirtied)
3566 zfs_fuid_sync(zfsvfs, tx);
3567
3568 if (zp->z_is_sa)
3569 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3570 __DECONST(void *, link), len, tx);
3571 else
3572 zfs_sa_symlink(zp, __DECONST(char *, link), len, tx);
3573
3574 zp->z_size = len;
3575 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3576 &zp->z_size, sizeof (zp->z_size), tx);
3577 /*
3578 * Insert the new object into the directory.
3579 */
3580 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
3581
3582 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3583 *zpp = zp;
3584
3585 zfs_acl_ids_free(&acl_ids);
3586
3587 dmu_tx_commit(tx);
3588
3589 getnewvnode_drop_reserve();
3590
3591 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3592 zil_commit(zilog, 0);
3593
3594 zfs_exit(zfsvfs, FTAG);
3595 return (error);
3596 }
3597
3598 /*
3599 * Return, in the buffer contained in the provided uio structure,
3600 * the symbolic path referred to by vp.
3601 *
3602 * IN: vp - vnode of symbolic link.
3603 * uio - structure to contain the link path.
3604 * cr - credentials of caller.
3605 * ct - caller context
3606 *
3607 * OUT: uio - structure containing the link path.
3608 *
3609 * RETURN: 0 on success, error code on failure.
3610 *
3611 * Timestamps:
3612 * vp - atime updated
3613 */
3614 static int
3615 zfs_readlink(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, caller_context_t *ct)
3616 {
3617 (void) cr, (void) ct;
3618 znode_t *zp = VTOZ(vp);
3619 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3620 int error;
3621
3622 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3623 return (error);
3624
3625 if (zp->z_is_sa)
3626 error = sa_lookup_uio(zp->z_sa_hdl,
3627 SA_ZPL_SYMLINK(zfsvfs), uio);
3628 else
3629 error = zfs_sa_readlink(zp, uio);
3630
3631 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3632
3633 zfs_exit(zfsvfs, FTAG);
3634 return (error);
3635 }
3636
3637 /*
3638 * Insert a new entry into directory tdvp referencing svp.
3639 *
3640 * IN: tdvp - Directory to contain new entry.
3641 * svp - vnode of new entry.
3642 * name - name of new entry.
3643 * cr - credentials of caller.
3644 *
3645 * RETURN: 0 on success, error code on failure.
3646 *
3647 * Timestamps:
3648 * tdvp - ctime|mtime updated
3649 * svp - ctime updated
3650 */
3651 int
3652 zfs_link(znode_t *tdzp, znode_t *szp, const char *name, cred_t *cr,
3653 int flags)
3654 {
3655 (void) flags;
3656 znode_t *tzp;
3657 zfsvfs_t *zfsvfs = tdzp->z_zfsvfs;
3658 zilog_t *zilog;
3659 dmu_tx_t *tx;
3660 int error;
3661 uint64_t parent;
3662 uid_t owner;
3663
3664 ASSERT3S(ZTOV(tdzp)->v_type, ==, VDIR);
3665
3666 if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0)
3667 return (error);
3668 zilog = zfsvfs->z_log;
3669
3670 /*
3671 * POSIX dictates that we return EPERM here.
3672 * Better choices include ENOTSUP or EISDIR.
3673 */
3674 if (ZTOV(szp)->v_type == VDIR) {
3675 zfs_exit(zfsvfs, FTAG);
3676 return (SET_ERROR(EPERM));
3677 }
3678
3679 if ((error = zfs_verify_zp(szp)) != 0) {
3680 zfs_exit(zfsvfs, FTAG);
3681 return (error);
3682 }
3683
3684 /*
3685 * If we are using project inheritance, means if the directory has
3686 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3687 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3688 * such case, we only allow hard link creation in our tree when the
3689 * project IDs are the same.
3690 */
3691 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3692 tdzp->z_projid != szp->z_projid) {
3693 zfs_exit(zfsvfs, FTAG);
3694 return (SET_ERROR(EXDEV));
3695 }
3696
3697 if (szp->z_pflags & (ZFS_APPENDONLY |
3698 ZFS_IMMUTABLE | ZFS_READONLY)) {
3699 zfs_exit(zfsvfs, FTAG);
3700 return (SET_ERROR(EPERM));
3701 }
3702
3703 /* Prevent links to .zfs/shares files */
3704
3705 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
3706 &parent, sizeof (uint64_t))) != 0) {
3707 zfs_exit(zfsvfs, FTAG);
3708 return (error);
3709 }
3710 if (parent == zfsvfs->z_shares_dir) {
3711 zfs_exit(zfsvfs, FTAG);
3712 return (SET_ERROR(EPERM));
3713 }
3714
3715 if (zfsvfs->z_utf8 && u8_validate(name,
3716 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3717 zfs_exit(zfsvfs, FTAG);
3718 return (SET_ERROR(EILSEQ));
3719 }
3720
3721 /*
3722 * We do not support links between attributes and non-attributes
3723 * because of the potential security risk of creating links
3724 * into "normal" file space in order to circumvent restrictions
3725 * imposed in attribute space.
3726 */
3727 if ((szp->z_pflags & ZFS_XATTR) != (tdzp->z_pflags & ZFS_XATTR)) {
3728 zfs_exit(zfsvfs, FTAG);
3729 return (SET_ERROR(EINVAL));
3730 }
3731
3732
3733 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
3734 if (owner != crgetuid(cr) && secpolicy_basic_link(ZTOV(szp), cr) != 0) {
3735 zfs_exit(zfsvfs, FTAG);
3736 return (SET_ERROR(EPERM));
3737 }
3738
3739 if ((error = zfs_zaccess(tdzp, ACE_ADD_FILE, 0, B_FALSE, cr, NULL))) {
3740 zfs_exit(zfsvfs, FTAG);
3741 return (error);
3742 }
3743
3744 /*
3745 * Attempt to lock directory; fail if entry already exists.
3746 */
3747 error = zfs_dirent_lookup(tdzp, name, &tzp, ZNEW);
3748 if (error) {
3749 zfs_exit(zfsvfs, FTAG);
3750 return (error);
3751 }
3752
3753 tx = dmu_tx_create(zfsvfs->z_os);
3754 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3755 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, name);
3756 zfs_sa_upgrade_txholds(tx, szp);
3757 zfs_sa_upgrade_txholds(tx, tdzp);
3758 error = dmu_tx_assign(tx, TXG_WAIT);
3759 if (error) {
3760 dmu_tx_abort(tx);
3761 zfs_exit(zfsvfs, FTAG);
3762 return (error);
3763 }
3764
3765 error = zfs_link_create(tdzp, name, szp, tx, 0);
3766
3767 if (error == 0) {
3768 uint64_t txtype = TX_LINK;
3769 zfs_log_link(zilog, tx, txtype, tdzp, szp, name);
3770 }
3771
3772 dmu_tx_commit(tx);
3773
3774 if (error == 0) {
3775 vnevent_link(ZTOV(szp), ct);
3776 }
3777
3778 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3779 zil_commit(zilog, 0);
3780
3781 zfs_exit(zfsvfs, FTAG);
3782 return (error);
3783 }
3784
3785 /*
3786 * Free or allocate space in a file. Currently, this function only
3787 * supports the `F_FREESP' command. However, this command is somewhat
3788 * misnamed, as its functionality includes the ability to allocate as
3789 * well as free space.
3790 *
3791 * IN: ip - inode of file to free data in.
3792 * cmd - action to take (only F_FREESP supported).
3793 * bfp - section of file to free/alloc.
3794 * flag - current file open mode flags.
3795 * offset - current file offset.
3796 * cr - credentials of caller.
3797 *
3798 * RETURN: 0 on success, error code on failure.
3799 *
3800 * Timestamps:
3801 * ip - ctime|mtime updated
3802 */
3803 int
3804 zfs_space(znode_t *zp, int cmd, flock64_t *bfp, int flag,
3805 offset_t offset, cred_t *cr)
3806 {
3807 (void) offset;
3808 zfsvfs_t *zfsvfs = ZTOZSB(zp);
3809 uint64_t off, len;
3810 int error;
3811
3812 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3813 return (error);
3814
3815 if (cmd != F_FREESP) {
3816 zfs_exit(zfsvfs, FTAG);
3817 return (SET_ERROR(EINVAL));
3818 }
3819
3820 /*
3821 * Callers might not be able to detect properly that we are read-only,
3822 * so check it explicitly here.
3823 */
3824 if (zfs_is_readonly(zfsvfs)) {
3825 zfs_exit(zfsvfs, FTAG);
3826 return (SET_ERROR(EROFS));
3827 }
3828
3829 if (bfp->l_len < 0) {
3830 zfs_exit(zfsvfs, FTAG);
3831 return (SET_ERROR(EINVAL));
3832 }
3833
3834 /*
3835 * Permissions aren't checked on Solaris because on this OS
3836 * zfs_space() can only be called with an opened file handle.
3837 * On Linux we can get here through truncate_range() which
3838 * operates directly on inodes, so we need to check access rights.
3839 */
3840 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr, NULL))) {
3841 zfs_exit(zfsvfs, FTAG);
3842 return (error);
3843 }
3844
3845 off = bfp->l_start;
3846 len = bfp->l_len; /* 0 means from off to end of file */
3847
3848 error = zfs_freesp(zp, off, len, flag, TRUE);
3849
3850 zfs_exit(zfsvfs, FTAG);
3851 return (error);
3852 }
3853
3854 static void
3855 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3856 {
3857 (void) cr, (void) ct;
3858 znode_t *zp = VTOZ(vp);
3859 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3860 int error;
3861
3862 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
3863 if (zp->z_sa_hdl == NULL) {
3864 /*
3865 * The fs has been unmounted, or we did a
3866 * suspend/resume and this file no longer exists.
3867 */
3868 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3869 vrecycle(vp);
3870 return;
3871 }
3872
3873 if (zp->z_unlinked) {
3874 /*
3875 * Fast path to recycle a vnode of a removed file.
3876 */
3877 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3878 vrecycle(vp);
3879 return;
3880 }
3881
3882 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3883 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3884
3885 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3886 zfs_sa_upgrade_txholds(tx, zp);
3887 error = dmu_tx_assign(tx, TXG_WAIT);
3888 if (error) {
3889 dmu_tx_abort(tx);
3890 } else {
3891 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
3892 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
3893 zp->z_atime_dirty = 0;
3894 dmu_tx_commit(tx);
3895 }
3896 }
3897 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3898 }
3899
3900
3901 _Static_assert(sizeof (struct zfid_short) <= sizeof (struct fid),
3902 "struct zfid_short bigger than struct fid");
3903 _Static_assert(sizeof (struct zfid_long) <= sizeof (struct fid),
3904 "struct zfid_long bigger than struct fid");
3905
3906 static int
3907 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3908 {
3909 (void) ct;
3910 znode_t *zp = VTOZ(vp);
3911 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3912 uint32_t gen;
3913 uint64_t gen64;
3914 uint64_t object = zp->z_id;
3915 zfid_short_t *zfid;
3916 int size, i, error;
3917
3918 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3919 return (error);
3920
3921 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
3922 &gen64, sizeof (uint64_t))) != 0) {
3923 zfs_exit(zfsvfs, FTAG);
3924 return (error);
3925 }
3926
3927 gen = (uint32_t)gen64;
3928
3929 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3930 fidp->fid_len = size;
3931
3932 zfid = (zfid_short_t *)fidp;
3933
3934 zfid->zf_len = size;
3935
3936 for (i = 0; i < sizeof (zfid->zf_object); i++)
3937 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3938
3939 /* Must have a non-zero generation number to distinguish from .zfs */
3940 if (gen == 0)
3941 gen = 1;
3942 for (i = 0; i < sizeof (zfid->zf_gen); i++)
3943 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3944
3945 if (size == LONG_FID_LEN) {
3946 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
3947 zfid_long_t *zlfid;
3948
3949 zlfid = (zfid_long_t *)fidp;
3950
3951 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3952 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3953
3954 /* XXX - this should be the generation number for the objset */
3955 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3956 zlfid->zf_setgen[i] = 0;
3957 }
3958
3959 zfs_exit(zfsvfs, FTAG);
3960 return (0);
3961 }
3962
3963 static int
3964 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
3965 caller_context_t *ct)
3966 {
3967 znode_t *zp;
3968 zfsvfs_t *zfsvfs;
3969 int error;
3970
3971 switch (cmd) {
3972 case _PC_LINK_MAX:
3973 *valp = MIN(LONG_MAX, ZFS_LINK_MAX);
3974 return (0);
3975
3976 case _PC_FILESIZEBITS:
3977 *valp = 64;
3978 return (0);
3979 case _PC_MIN_HOLE_SIZE:
3980 *valp = (int)SPA_MINBLOCKSIZE;
3981 return (0);
3982 case _PC_ACL_EXTENDED:
3983 #if 0 /* POSIX ACLs are not implemented for ZFS on FreeBSD yet. */
3984 zp = VTOZ(vp);
3985 zfsvfs = zp->z_zfsvfs;
3986 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3987 return (error);
3988 *valp = zfsvfs->z_acl_type == ZFSACLTYPE_POSIX ? 1 : 0;
3989 zfs_exit(zfsvfs, FTAG);
3990 #else
3991 *valp = 0;
3992 #endif
3993 return (0);
3994
3995 case _PC_ACL_NFS4:
3996 zp = VTOZ(vp);
3997 zfsvfs = zp->z_zfsvfs;
3998 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3999 return (error);
4000 *valp = zfsvfs->z_acl_type == ZFS_ACLTYPE_NFSV4 ? 1 : 0;
4001 zfs_exit(zfsvfs, FTAG);
4002 return (0);
4003
4004 case _PC_ACL_PATH_MAX:
4005 *valp = ACL_MAX_ENTRIES;
4006 return (0);
4007
4008 default:
4009 return (EOPNOTSUPP);
4010 }
4011 }
4012
4013 static int
4014 zfs_getpages(struct vnode *vp, vm_page_t *ma, int count, int *rbehind,
4015 int *rahead)
4016 {
4017 znode_t *zp = VTOZ(vp);
4018 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4019 zfs_locked_range_t *lr;
4020 vm_object_t object;
4021 off_t start, end, obj_size;
4022 uint_t blksz;
4023 int pgsin_b, pgsin_a;
4024 int error;
4025
4026 if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0)
4027 return (zfs_vm_pagerret_error);
4028
4029 start = IDX_TO_OFF(ma[0]->pindex);
4030 end = IDX_TO_OFF(ma[count - 1]->pindex + 1);
4031
4032 /*
4033 * Lock a range covering all required and optional pages.
4034 * Note that we need to handle the case of the block size growing.
4035 */
4036 for (;;) {
4037 blksz = zp->z_blksz;
4038 lr = zfs_rangelock_tryenter(&zp->z_rangelock,
4039 rounddown(start, blksz),
4040 roundup(end, blksz) - rounddown(start, blksz), RL_READER);
4041 if (lr == NULL) {
4042 if (rahead != NULL) {
4043 *rahead = 0;
4044 rahead = NULL;
4045 }
4046 if (rbehind != NULL) {
4047 *rbehind = 0;
4048 rbehind = NULL;
4049 }
4050 break;
4051 }
4052 if (blksz == zp->z_blksz)
4053 break;
4054 zfs_rangelock_exit(lr);
4055 }
4056
4057 object = ma[0]->object;
4058 zfs_vmobject_wlock(object);
4059 obj_size = object->un_pager.vnp.vnp_size;
4060 zfs_vmobject_wunlock(object);
4061 if (IDX_TO_OFF(ma[count - 1]->pindex) >= obj_size) {
4062 if (lr != NULL)
4063 zfs_rangelock_exit(lr);
4064 zfs_exit(zfsvfs, FTAG);
4065 return (zfs_vm_pagerret_bad);
4066 }
4067
4068 pgsin_b = 0;
4069 if (rbehind != NULL) {
4070 pgsin_b = OFF_TO_IDX(start - rounddown(start, blksz));
4071 pgsin_b = MIN(*rbehind, pgsin_b);
4072 }
4073
4074 pgsin_a = 0;
4075 if (rahead != NULL) {
4076 pgsin_a = OFF_TO_IDX(roundup(end, blksz) - end);
4077 if (end + IDX_TO_OFF(pgsin_a) >= obj_size)
4078 pgsin_a = OFF_TO_IDX(round_page(obj_size) - end);
4079 pgsin_a = MIN(*rahead, pgsin_a);
4080 }
4081
4082 /*
4083 * NB: we need to pass the exact byte size of the data that we expect
4084 * to read after accounting for the file size. This is required because
4085 * ZFS will panic if we request DMU to read beyond the end of the last
4086 * allocated block.
4087 */
4088 error = dmu_read_pages(zfsvfs->z_os, zp->z_id, ma, count, &pgsin_b,
4089 &pgsin_a, MIN(end, obj_size) - (end - PAGE_SIZE));
4090
4091 if (lr != NULL)
4092 zfs_rangelock_exit(lr);
4093 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4094
4095 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, count*PAGE_SIZE);
4096
4097 zfs_exit(zfsvfs, FTAG);
4098
4099 if (error != 0)
4100 return (zfs_vm_pagerret_error);
4101
4102 VM_CNT_INC(v_vnodein);
4103 VM_CNT_ADD(v_vnodepgsin, count + pgsin_b + pgsin_a);
4104 if (rbehind != NULL)
4105 *rbehind = pgsin_b;
4106 if (rahead != NULL)
4107 *rahead = pgsin_a;
4108 return (zfs_vm_pagerret_ok);
4109 }
4110
4111 #ifndef _SYS_SYSPROTO_H_
4112 struct vop_getpages_args {
4113 struct vnode *a_vp;
4114 vm_page_t *a_m;
4115 int a_count;
4116 int *a_rbehind;
4117 int *a_rahead;
4118 };
4119 #endif
4120
4121 static int
4122 zfs_freebsd_getpages(struct vop_getpages_args *ap)
4123 {
4124
4125 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4126 ap->a_rahead));
4127 }
4128
4129 static int
4130 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4131 int *rtvals)
4132 {
4133 znode_t *zp = VTOZ(vp);
4134 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4135 zfs_locked_range_t *lr;
4136 dmu_tx_t *tx;
4137 struct sf_buf *sf;
4138 vm_object_t object;
4139 vm_page_t m;
4140 caddr_t va;
4141 size_t tocopy;
4142 size_t lo_len;
4143 vm_ooffset_t lo_off;
4144 vm_ooffset_t off;
4145 uint_t blksz;
4146 int ncount;
4147 int pcount;
4148 int err;
4149 int i;
4150
4151 object = vp->v_object;
4152 KASSERT(ma[0]->object == object, ("mismatching object"));
4153 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4154
4155 pcount = btoc(len);
4156 ncount = pcount;
4157 for (i = 0; i < pcount; i++)
4158 rtvals[i] = zfs_vm_pagerret_error;
4159
4160 if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0)
4161 return (zfs_vm_pagerret_error);
4162
4163 off = IDX_TO_OFF(ma[0]->pindex);
4164 blksz = zp->z_blksz;
4165 lo_off = rounddown(off, blksz);
4166 lo_len = roundup(len + (off - lo_off), blksz);
4167 lr = zfs_rangelock_enter(&zp->z_rangelock, lo_off, lo_len, RL_WRITER);
4168
4169 zfs_vmobject_wlock(object);
4170 if (len + off > object->un_pager.vnp.vnp_size) {
4171 if (object->un_pager.vnp.vnp_size > off) {
4172 int pgoff;
4173
4174 len = object->un_pager.vnp.vnp_size - off;
4175 ncount = btoc(len);
4176 if ((pgoff = (int)len & PAGE_MASK) != 0) {
4177 /*
4178 * If the object is locked and the following
4179 * conditions hold, then the page's dirty
4180 * field cannot be concurrently changed by a
4181 * pmap operation.
4182 */
4183 m = ma[ncount - 1];
4184 vm_page_assert_sbusied(m);
4185 KASSERT(!pmap_page_is_write_mapped(m),
4186 ("zfs_putpages: page %p is not read-only",
4187 m));
4188 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
4189 pgoff);
4190 }
4191 } else {
4192 len = 0;
4193 ncount = 0;
4194 }
4195 if (ncount < pcount) {
4196 for (i = ncount; i < pcount; i++) {
4197 rtvals[i] = zfs_vm_pagerret_bad;
4198 }
4199 }
4200 }
4201 zfs_vmobject_wunlock(object);
4202
4203 if (ncount == 0)
4204 goto out;
4205
4206 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, zp->z_uid) ||
4207 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, zp->z_gid) ||
4208 (zp->z_projid != ZFS_DEFAULT_PROJID &&
4209 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
4210 zp->z_projid))) {
4211 goto out;
4212 }
4213
4214 tx = dmu_tx_create(zfsvfs->z_os);
4215 dmu_tx_hold_write(tx, zp->z_id, off, len);
4216
4217 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4218 zfs_sa_upgrade_txholds(tx, zp);
4219 err = dmu_tx_assign(tx, TXG_WAIT);
4220 if (err != 0) {
4221 dmu_tx_abort(tx);
4222 goto out;
4223 }
4224
4225 if (zp->z_blksz < PAGE_SIZE) {
4226 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
4227 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
4228 va = zfs_map_page(ma[i], &sf);
4229 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
4230 zfs_unmap_page(sf);
4231 }
4232 } else {
4233 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
4234 }
4235
4236 if (err == 0) {
4237 uint64_t mtime[2], ctime[2];
4238 sa_bulk_attr_t bulk[3];
4239 int count = 0;
4240
4241 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4242 &mtime, 16);
4243 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4244 &ctime, 16);
4245 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4246 &zp->z_pflags, 8);
4247 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
4248 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4249 ASSERT0(err);
4250 /*
4251 * XXX we should be passing a callback to undirty
4252 * but that would make the locking messier
4253 */
4254 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off,
4255 len, 0, NULL, NULL);
4256
4257 zfs_vmobject_wlock(object);
4258 for (i = 0; i < ncount; i++) {
4259 rtvals[i] = zfs_vm_pagerret_ok;
4260 vm_page_undirty(ma[i]);
4261 }
4262 zfs_vmobject_wunlock(object);
4263 VM_CNT_INC(v_vnodeout);
4264 VM_CNT_ADD(v_vnodepgsout, ncount);
4265 }
4266 dmu_tx_commit(tx);
4267
4268 out:
4269 zfs_rangelock_exit(lr);
4270 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
4271 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4272 zil_commit(zfsvfs->z_log, zp->z_id);
4273
4274 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, len);
4275
4276 zfs_exit(zfsvfs, FTAG);
4277 return (rtvals[0]);
4278 }
4279
4280 #ifndef _SYS_SYSPROTO_H_
4281 struct vop_putpages_args {
4282 struct vnode *a_vp;
4283 vm_page_t *a_m;
4284 int a_count;
4285 int a_sync;
4286 int *a_rtvals;
4287 };
4288 #endif
4289
4290 static int
4291 zfs_freebsd_putpages(struct vop_putpages_args *ap)
4292 {
4293
4294 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
4295 ap->a_rtvals));
4296 }
4297
4298 #ifndef _SYS_SYSPROTO_H_
4299 struct vop_bmap_args {
4300 struct vnode *a_vp;
4301 daddr_t a_bn;
4302 struct bufobj **a_bop;
4303 daddr_t *a_bnp;
4304 int *a_runp;
4305 int *a_runb;
4306 };
4307 #endif
4308
4309 static int
4310 zfs_freebsd_bmap(struct vop_bmap_args *ap)
4311 {
4312
4313 if (ap->a_bop != NULL)
4314 *ap->a_bop = &ap->a_vp->v_bufobj;
4315 if (ap->a_bnp != NULL)
4316 *ap->a_bnp = ap->a_bn;
4317 if (ap->a_runp != NULL)
4318 *ap->a_runp = 0;
4319 if (ap->a_runb != NULL)
4320 *ap->a_runb = 0;
4321
4322 return (0);
4323 }
4324
4325 #ifndef _SYS_SYSPROTO_H_
4326 struct vop_open_args {
4327 struct vnode *a_vp;
4328 int a_mode;
4329 struct ucred *a_cred;
4330 struct thread *a_td;
4331 };
4332 #endif
4333
4334 static int
4335 zfs_freebsd_open(struct vop_open_args *ap)
4336 {
4337 vnode_t *vp = ap->a_vp;
4338 znode_t *zp = VTOZ(vp);
4339 int error;
4340
4341 error = zfs_open(&vp, ap->a_mode, ap->a_cred);
4342 if (error == 0)
4343 vnode_create_vobject(vp, zp->z_size, ap->a_td);
4344 return (error);
4345 }
4346
4347 #ifndef _SYS_SYSPROTO_H_
4348 struct vop_close_args {
4349 struct vnode *a_vp;
4350 int a_fflag;
4351 struct ucred *a_cred;
4352 struct thread *a_td;
4353 };
4354 #endif
4355
4356 static int
4357 zfs_freebsd_close(struct vop_close_args *ap)
4358 {
4359
4360 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred));
4361 }
4362
4363 #ifndef _SYS_SYSPROTO_H_
4364 struct vop_ioctl_args {
4365 struct vnode *a_vp;
4366 ulong_t a_command;
4367 caddr_t a_data;
4368 int a_fflag;
4369 struct ucred *cred;
4370 struct thread *td;
4371 };
4372 #endif
4373
4374 static int
4375 zfs_freebsd_ioctl(struct vop_ioctl_args *ap)
4376 {
4377
4378 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4379 ap->a_fflag, ap->a_cred, NULL));
4380 }
4381
4382 static int
4383 ioflags(int ioflags)
4384 {
4385 int flags = 0;
4386
4387 if (ioflags & IO_APPEND)
4388 flags |= O_APPEND;
4389 if (ioflags & IO_NDELAY)
4390 flags |= O_NONBLOCK;
4391 if (ioflags & IO_SYNC)
4392 flags |= O_SYNC;
4393
4394 return (flags);
4395 }
4396
4397 #ifndef _SYS_SYSPROTO_H_
4398 struct vop_read_args {
4399 struct vnode *a_vp;
4400 struct uio *a_uio;
4401 int a_ioflag;
4402 struct ucred *a_cred;
4403 };
4404 #endif
4405
4406 static int
4407 zfs_freebsd_read(struct vop_read_args *ap)
4408 {
4409 zfs_uio_t uio;
4410 zfs_uio_init(&uio, ap->a_uio);
4411 return (zfs_read(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4412 ap->a_cred));
4413 }
4414
4415 #ifndef _SYS_SYSPROTO_H_
4416 struct vop_write_args {
4417 struct vnode *a_vp;
4418 struct uio *a_uio;
4419 int a_ioflag;
4420 struct ucred *a_cred;
4421 };
4422 #endif
4423
4424 static int
4425 zfs_freebsd_write(struct vop_write_args *ap)
4426 {
4427 zfs_uio_t uio;
4428 zfs_uio_init(&uio, ap->a_uio);
4429 return (zfs_write(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4430 ap->a_cred));
4431 }
4432
4433 #if __FreeBSD_version >= 1300102
4434 /*
4435 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
4436 * the comment above cache_fplookup for details.
4437 */
4438 static int
4439 zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args *v)
4440 {
4441 vnode_t *vp;
4442 znode_t *zp;
4443 uint64_t pflags;
4444
4445 vp = v->a_vp;
4446 zp = VTOZ_SMR(vp);
4447 if (__predict_false(zp == NULL))
4448 return (EAGAIN);
4449 pflags = atomic_load_64(&zp->z_pflags);
4450 if (pflags & ZFS_AV_QUARANTINED)
4451 return (EAGAIN);
4452 if (pflags & ZFS_XATTR)
4453 return (EAGAIN);
4454 if ((pflags & ZFS_NO_EXECS_DENIED) == 0)
4455 return (EAGAIN);
4456 return (0);
4457 }
4458 #endif
4459
4460 #if __FreeBSD_version >= 1300139
4461 static int
4462 zfs_freebsd_fplookup_symlink(struct vop_fplookup_symlink_args *v)
4463 {
4464 vnode_t *vp;
4465 znode_t *zp;
4466 char *target;
4467
4468 vp = v->a_vp;
4469 zp = VTOZ_SMR(vp);
4470 if (__predict_false(zp == NULL)) {
4471 return (EAGAIN);
4472 }
4473
4474 target = atomic_load_consume_ptr(&zp->z_cached_symlink);
4475 if (target == NULL) {
4476 return (EAGAIN);
4477 }
4478 return (cache_symlink_resolve(v->a_fpl, target, strlen(target)));
4479 }
4480 #endif
4481
4482 #ifndef _SYS_SYSPROTO_H_
4483 struct vop_access_args {
4484 struct vnode *a_vp;
4485 accmode_t a_accmode;
4486 struct ucred *a_cred;
4487 struct thread *a_td;
4488 };
4489 #endif
4490
4491 static int
4492 zfs_freebsd_access(struct vop_access_args *ap)
4493 {
4494 vnode_t *vp = ap->a_vp;
4495 znode_t *zp = VTOZ(vp);
4496 accmode_t accmode;
4497 int error = 0;
4498
4499
4500 if (ap->a_accmode == VEXEC) {
4501 if (zfs_fastaccesschk_execute(zp, ap->a_cred) == 0)
4502 return (0);
4503 }
4504
4505 /*
4506 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4507 */
4508 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4509 if (accmode != 0)
4510 error = zfs_access(zp, accmode, 0, ap->a_cred);
4511
4512 /*
4513 * VADMIN has to be handled by vaccess().
4514 */
4515 if (error == 0) {
4516 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4517 if (accmode != 0) {
4518 #if __FreeBSD_version >= 1300105
4519 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4520 zp->z_gid, accmode, ap->a_cred);
4521 #else
4522 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4523 zp->z_gid, accmode, ap->a_cred, NULL);
4524 #endif
4525 }
4526 }
4527
4528 /*
4529 * For VEXEC, ensure that at least one execute bit is set for
4530 * non-directories.
4531 */
4532 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
4533 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
4534 error = EACCES;
4535 }
4536
4537 return (error);
4538 }
4539
4540 #ifndef _SYS_SYSPROTO_H_
4541 struct vop_lookup_args {
4542 struct vnode *a_dvp;
4543 struct vnode **a_vpp;
4544 struct componentname *a_cnp;
4545 };
4546 #endif
4547
4548 static int
4549 zfs_freebsd_lookup(struct vop_lookup_args *ap, boolean_t cached)
4550 {
4551 struct componentname *cnp = ap->a_cnp;
4552 char nm[NAME_MAX + 1];
4553
4554 ASSERT3U(cnp->cn_namelen, <, sizeof (nm));
4555 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof (nm)));
4556
4557 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4558 cnp->cn_cred, 0, cached));
4559 }
4560
4561 static int
4562 zfs_freebsd_cachedlookup(struct vop_cachedlookup_args *ap)
4563 {
4564
4565 return (zfs_freebsd_lookup((struct vop_lookup_args *)ap, B_TRUE));
4566 }
4567
4568 #ifndef _SYS_SYSPROTO_H_
4569 struct vop_lookup_args {
4570 struct vnode *a_dvp;
4571 struct vnode **a_vpp;
4572 struct componentname *a_cnp;
4573 };
4574 #endif
4575
4576 static int
4577 zfs_cache_lookup(struct vop_lookup_args *ap)
4578 {
4579 zfsvfs_t *zfsvfs;
4580
4581 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4582 if (zfsvfs->z_use_namecache)
4583 return (vfs_cache_lookup(ap));
4584 else
4585 return (zfs_freebsd_lookup(ap, B_FALSE));
4586 }
4587
4588 #ifndef _SYS_SYSPROTO_H_
4589 struct vop_create_args {
4590 struct vnode *a_dvp;
4591 struct vnode **a_vpp;
4592 struct componentname *a_cnp;
4593 struct vattr *a_vap;
4594 };
4595 #endif
4596
4597 static int
4598 zfs_freebsd_create(struct vop_create_args *ap)
4599 {
4600 zfsvfs_t *zfsvfs;
4601 struct componentname *cnp = ap->a_cnp;
4602 vattr_t *vap = ap->a_vap;
4603 znode_t *zp = NULL;
4604 int rc, mode;
4605
4606 #if __FreeBSD_version < 1400068
4607 ASSERT(cnp->cn_flags & SAVENAME);
4608 #endif
4609
4610 vattr_init_mask(vap);
4611 mode = vap->va_mode & ALLPERMS;
4612 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4613 *ap->a_vpp = NULL;
4614
4615 rc = zfs_create(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap, 0, mode,
4616 &zp, cnp->cn_cred, 0 /* flag */, NULL /* vsecattr */, NULL);
4617 if (rc == 0)
4618 *ap->a_vpp = ZTOV(zp);
4619 if (zfsvfs->z_use_namecache &&
4620 rc == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
4621 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
4622
4623 return (rc);
4624 }
4625
4626 #ifndef _SYS_SYSPROTO_H_
4627 struct vop_remove_args {
4628 struct vnode *a_dvp;
4629 struct vnode *a_vp;
4630 struct componentname *a_cnp;
4631 };
4632 #endif
4633
4634 static int
4635 zfs_freebsd_remove(struct vop_remove_args *ap)
4636 {
4637
4638 #if __FreeBSD_version < 1400068
4639 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4640 #endif
4641
4642 return (zfs_remove_(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
4643 ap->a_cnp->cn_cred));
4644 }
4645
4646 #ifndef _SYS_SYSPROTO_H_
4647 struct vop_mkdir_args {
4648 struct vnode *a_dvp;
4649 struct vnode **a_vpp;
4650 struct componentname *a_cnp;
4651 struct vattr *a_vap;
4652 };
4653 #endif
4654
4655 static int
4656 zfs_freebsd_mkdir(struct vop_mkdir_args *ap)
4657 {
4658 vattr_t *vap = ap->a_vap;
4659 znode_t *zp = NULL;
4660 int rc;
4661
4662 #if __FreeBSD_version < 1400068
4663 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4664 #endif
4665
4666 vattr_init_mask(vap);
4667 *ap->a_vpp = NULL;
4668
4669 rc = zfs_mkdir(VTOZ(ap->a_dvp), ap->a_cnp->cn_nameptr, vap, &zp,
4670 ap->a_cnp->cn_cred, 0, NULL, NULL);
4671
4672 if (rc == 0)
4673 *ap->a_vpp = ZTOV(zp);
4674 return (rc);
4675 }
4676
4677 #ifndef _SYS_SYSPROTO_H_
4678 struct vop_rmdir_args {
4679 struct vnode *a_dvp;
4680 struct vnode *a_vp;
4681 struct componentname *a_cnp;
4682 };
4683 #endif
4684
4685 static int
4686 zfs_freebsd_rmdir(struct vop_rmdir_args *ap)
4687 {
4688 struct componentname *cnp = ap->a_cnp;
4689
4690 #if __FreeBSD_version < 1400068
4691 ASSERT(cnp->cn_flags & SAVENAME);
4692 #endif
4693
4694 return (zfs_rmdir_(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
4695 }
4696
4697 #ifndef _SYS_SYSPROTO_H_
4698 struct vop_readdir_args {
4699 struct vnode *a_vp;
4700 struct uio *a_uio;
4701 struct ucred *a_cred;
4702 int *a_eofflag;
4703 int *a_ncookies;
4704 cookie_t **a_cookies;
4705 };
4706 #endif
4707
4708 static int
4709 zfs_freebsd_readdir(struct vop_readdir_args *ap)
4710 {
4711 zfs_uio_t uio;
4712 zfs_uio_init(&uio, ap->a_uio);
4713 return (zfs_readdir(ap->a_vp, &uio, ap->a_cred, ap->a_eofflag,
4714 ap->a_ncookies, ap->a_cookies));
4715 }
4716
4717 #ifndef _SYS_SYSPROTO_H_
4718 struct vop_fsync_args {
4719 struct vnode *a_vp;
4720 int a_waitfor;
4721 struct thread *a_td;
4722 };
4723 #endif
4724
4725 static int
4726 zfs_freebsd_fsync(struct vop_fsync_args *ap)
4727 {
4728
4729 vop_stdfsync(ap);
4730 return (zfs_fsync(VTOZ(ap->a_vp), 0, ap->a_td->td_ucred));
4731 }
4732
4733 #ifndef _SYS_SYSPROTO_H_
4734 struct vop_getattr_args {
4735 struct vnode *a_vp;
4736 struct vattr *a_vap;
4737 struct ucred *a_cred;
4738 };
4739 #endif
4740
4741 static int
4742 zfs_freebsd_getattr(struct vop_getattr_args *ap)
4743 {
4744 vattr_t *vap = ap->a_vap;
4745 xvattr_t xvap;
4746 ulong_t fflags = 0;
4747 int error;
4748
4749 xva_init(&xvap);
4750 xvap.xva_vattr = *vap;
4751 xvap.xva_vattr.va_mask |= AT_XVATTR;
4752
4753 /* Convert chflags into ZFS-type flags. */
4754 /* XXX: what about SF_SETTABLE?. */
4755 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
4756 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
4757 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
4758 XVA_SET_REQ(&xvap, XAT_NODUMP);
4759 XVA_SET_REQ(&xvap, XAT_READONLY);
4760 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
4761 XVA_SET_REQ(&xvap, XAT_SYSTEM);
4762 XVA_SET_REQ(&xvap, XAT_HIDDEN);
4763 XVA_SET_REQ(&xvap, XAT_REPARSE);
4764 XVA_SET_REQ(&xvap, XAT_OFFLINE);
4765 XVA_SET_REQ(&xvap, XAT_SPARSE);
4766
4767 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred);
4768 if (error != 0)
4769 return (error);
4770
4771 /* Convert ZFS xattr into chflags. */
4772 #define FLAG_CHECK(fflag, xflag, xfield) do { \
4773 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
4774 fflags |= (fflag); \
4775 } while (0)
4776 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
4777 xvap.xva_xoptattrs.xoa_immutable);
4778 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
4779 xvap.xva_xoptattrs.xoa_appendonly);
4780 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
4781 xvap.xva_xoptattrs.xoa_nounlink);
4782 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
4783 xvap.xva_xoptattrs.xoa_archive);
4784 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
4785 xvap.xva_xoptattrs.xoa_nodump);
4786 FLAG_CHECK(UF_READONLY, XAT_READONLY,
4787 xvap.xva_xoptattrs.xoa_readonly);
4788 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
4789 xvap.xva_xoptattrs.xoa_system);
4790 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
4791 xvap.xva_xoptattrs.xoa_hidden);
4792 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
4793 xvap.xva_xoptattrs.xoa_reparse);
4794 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
4795 xvap.xva_xoptattrs.xoa_offline);
4796 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
4797 xvap.xva_xoptattrs.xoa_sparse);
4798
4799 #undef FLAG_CHECK
4800 *vap = xvap.xva_vattr;
4801 vap->va_flags = fflags;
4802 return (0);
4803 }
4804
4805 #ifndef _SYS_SYSPROTO_H_
4806 struct vop_setattr_args {
4807 struct vnode *a_vp;
4808 struct vattr *a_vap;
4809 struct ucred *a_cred;
4810 };
4811 #endif
4812
4813 static int
4814 zfs_freebsd_setattr(struct vop_setattr_args *ap)
4815 {
4816 vnode_t *vp = ap->a_vp;
4817 vattr_t *vap = ap->a_vap;
4818 cred_t *cred = ap->a_cred;
4819 xvattr_t xvap;
4820 ulong_t fflags;
4821 uint64_t zflags;
4822
4823 vattr_init_mask(vap);
4824 vap->va_mask &= ~AT_NOSET;
4825
4826 xva_init(&xvap);
4827 xvap.xva_vattr = *vap;
4828
4829 zflags = VTOZ(vp)->z_pflags;
4830
4831 if (vap->va_flags != VNOVAL) {
4832 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
4833 int error;
4834
4835 if (zfsvfs->z_use_fuids == B_FALSE)
4836 return (EOPNOTSUPP);
4837
4838 fflags = vap->va_flags;
4839 /*
4840 * XXX KDM
4841 * We need to figure out whether it makes sense to allow
4842 * UF_REPARSE through, since we don't really have other
4843 * facilities to handle reparse points and zfs_setattr()
4844 * doesn't currently allow setting that attribute anyway.
4845 */
4846 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
4847 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
4848 UF_OFFLINE|UF_SPARSE)) != 0)
4849 return (EOPNOTSUPP);
4850 /*
4851 * Unprivileged processes are not permitted to unset system
4852 * flags, or modify flags if any system flags are set.
4853 * Privileged non-jail processes may not modify system flags
4854 * if securelevel > 0 and any existing system flags are set.
4855 * Privileged jail processes behave like privileged non-jail
4856 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
4857 * otherwise, they behave like unprivileged processes.
4858 */
4859 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
4860 spl_priv_check_cred(cred, PRIV_VFS_SYSFLAGS) == 0) {
4861 if (zflags &
4862 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4863 error = securelevel_gt(cred, 0);
4864 if (error != 0)
4865 return (error);
4866 }
4867 } else {
4868 /*
4869 * Callers may only modify the file flags on
4870 * objects they have VADMIN rights for.
4871 */
4872 if ((error = VOP_ACCESS(vp, VADMIN, cred,
4873 curthread)) != 0)
4874 return (error);
4875 if (zflags &
4876 (ZFS_IMMUTABLE | ZFS_APPENDONLY |
4877 ZFS_NOUNLINK)) {
4878 return (EPERM);
4879 }
4880 if (fflags &
4881 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
4882 return (EPERM);
4883 }
4884 }
4885
4886 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
4887 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
4888 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
4889 XVA_SET_REQ(&xvap, (xflag)); \
4890 (xfield) = ((fflags & (fflag)) != 0); \
4891 } \
4892 } while (0)
4893 /* Convert chflags into ZFS-type flags. */
4894 /* XXX: what about SF_SETTABLE?. */
4895 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
4896 xvap.xva_xoptattrs.xoa_immutable);
4897 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
4898 xvap.xva_xoptattrs.xoa_appendonly);
4899 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
4900 xvap.xva_xoptattrs.xoa_nounlink);
4901 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
4902 xvap.xva_xoptattrs.xoa_archive);
4903 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
4904 xvap.xva_xoptattrs.xoa_nodump);
4905 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
4906 xvap.xva_xoptattrs.xoa_readonly);
4907 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
4908 xvap.xva_xoptattrs.xoa_system);
4909 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
4910 xvap.xva_xoptattrs.xoa_hidden);
4911 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
4912 xvap.xva_xoptattrs.xoa_reparse);
4913 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
4914 xvap.xva_xoptattrs.xoa_offline);
4915 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
4916 xvap.xva_xoptattrs.xoa_sparse);
4917 #undef FLAG_CHANGE
4918 }
4919 if (vap->va_birthtime.tv_sec != VNOVAL) {
4920 xvap.xva_vattr.va_mask |= AT_XVATTR;
4921 XVA_SET_REQ(&xvap, XAT_CREATETIME);
4922 }
4923 return (zfs_setattr(VTOZ(vp), (vattr_t *)&xvap, 0, cred, NULL));
4924 }
4925
4926 #ifndef _SYS_SYSPROTO_H_
4927 struct vop_rename_args {
4928 struct vnode *a_fdvp;
4929 struct vnode *a_fvp;
4930 struct componentname *a_fcnp;
4931 struct vnode *a_tdvp;
4932 struct vnode *a_tvp;
4933 struct componentname *a_tcnp;
4934 };
4935 #endif
4936
4937 static int
4938 zfs_freebsd_rename(struct vop_rename_args *ap)
4939 {
4940 vnode_t *fdvp = ap->a_fdvp;
4941 vnode_t *fvp = ap->a_fvp;
4942 vnode_t *tdvp = ap->a_tdvp;
4943 vnode_t *tvp = ap->a_tvp;
4944 int error;
4945
4946 #if __FreeBSD_version < 1400068
4947 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
4948 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
4949 #endif
4950
4951 error = zfs_do_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
4952 ap->a_tcnp, ap->a_fcnp->cn_cred);
4953
4954 vrele(fdvp);
4955 vrele(fvp);
4956 vrele(tdvp);
4957 if (tvp != NULL)
4958 vrele(tvp);
4959
4960 return (error);
4961 }
4962
4963 #ifndef _SYS_SYSPROTO_H_
4964 struct vop_symlink_args {
4965 struct vnode *a_dvp;
4966 struct vnode **a_vpp;
4967 struct componentname *a_cnp;
4968 struct vattr *a_vap;
4969 char *a_target;
4970 };
4971 #endif
4972
4973 static int
4974 zfs_freebsd_symlink(struct vop_symlink_args *ap)
4975 {
4976 struct componentname *cnp = ap->a_cnp;
4977 vattr_t *vap = ap->a_vap;
4978 znode_t *zp = NULL;
4979 #if __FreeBSD_version >= 1300139
4980 char *symlink;
4981 size_t symlink_len;
4982 #endif
4983 int rc;
4984
4985 #if __FreeBSD_version < 1400068
4986 ASSERT(cnp->cn_flags & SAVENAME);
4987 #endif
4988
4989 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
4990 vattr_init_mask(vap);
4991 *ap->a_vpp = NULL;
4992
4993 rc = zfs_symlink(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap,
4994 ap->a_target, &zp, cnp->cn_cred, 0 /* flags */, NULL);
4995 if (rc == 0) {
4996 *ap->a_vpp = ZTOV(zp);
4997 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
4998 #if __FreeBSD_version >= 1300139
4999 MPASS(zp->z_cached_symlink == NULL);
5000 symlink_len = strlen(ap->a_target);
5001 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
5002 if (symlink != NULL) {
5003 memcpy(symlink, ap->a_target, symlink_len);
5004 symlink[symlink_len] = '\0';
5005 atomic_store_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5006 (uintptr_t)symlink);
5007 }
5008 #endif
5009 }
5010 return (rc);
5011 }
5012
5013 #ifndef _SYS_SYSPROTO_H_
5014 struct vop_readlink_args {
5015 struct vnode *a_vp;
5016 struct uio *a_uio;
5017 struct ucred *a_cred;
5018 };
5019 #endif
5020
5021 static int
5022 zfs_freebsd_readlink(struct vop_readlink_args *ap)
5023 {
5024 zfs_uio_t uio;
5025 int error;
5026 #if __FreeBSD_version >= 1300139
5027 znode_t *zp = VTOZ(ap->a_vp);
5028 char *symlink, *base;
5029 size_t symlink_len;
5030 bool trycache;
5031 #endif
5032
5033 zfs_uio_init(&uio, ap->a_uio);
5034 #if __FreeBSD_version >= 1300139
5035 trycache = false;
5036 if (zfs_uio_segflg(&uio) == UIO_SYSSPACE &&
5037 zfs_uio_iovcnt(&uio) == 1) {
5038 base = zfs_uio_iovbase(&uio, 0);
5039 symlink_len = zfs_uio_iovlen(&uio, 0);
5040 trycache = true;
5041 }
5042 #endif
5043 error = zfs_readlink(ap->a_vp, &uio, ap->a_cred, NULL);
5044 #if __FreeBSD_version >= 1300139
5045 if (atomic_load_ptr(&zp->z_cached_symlink) != NULL ||
5046 error != 0 || !trycache) {
5047 return (error);
5048 }
5049 symlink_len -= zfs_uio_resid(&uio);
5050 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
5051 if (symlink != NULL) {
5052 memcpy(symlink, base, symlink_len);
5053 symlink[symlink_len] = '\0';
5054 if (!atomic_cmpset_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5055 (uintptr_t)NULL, (uintptr_t)symlink)) {
5056 cache_symlink_free(symlink, symlink_len + 1);
5057 }
5058 }
5059 #endif
5060 return (error);
5061 }
5062
5063 #ifndef _SYS_SYSPROTO_H_
5064 struct vop_link_args {
5065 struct vnode *a_tdvp;
5066 struct vnode *a_vp;
5067 struct componentname *a_cnp;
5068 };
5069 #endif
5070
5071 static int
5072 zfs_freebsd_link(struct vop_link_args *ap)
5073 {
5074 struct componentname *cnp = ap->a_cnp;
5075 vnode_t *vp = ap->a_vp;
5076 vnode_t *tdvp = ap->a_tdvp;
5077
5078 if (tdvp->v_mount != vp->v_mount)
5079 return (EXDEV);
5080
5081 #if __FreeBSD_version < 1400068
5082 ASSERT(cnp->cn_flags & SAVENAME);
5083 #endif
5084
5085 return (zfs_link(VTOZ(tdvp), VTOZ(vp),
5086 cnp->cn_nameptr, cnp->cn_cred, 0));
5087 }
5088
5089 #ifndef _SYS_SYSPROTO_H_
5090 struct vop_inactive_args {
5091 struct vnode *a_vp;
5092 struct thread *a_td;
5093 };
5094 #endif
5095
5096 static int
5097 zfs_freebsd_inactive(struct vop_inactive_args *ap)
5098 {
5099 vnode_t *vp = ap->a_vp;
5100
5101 #if __FreeBSD_version >= 1300123
5102 zfs_inactive(vp, curthread->td_ucred, NULL);
5103 #else
5104 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5105 #endif
5106 return (0);
5107 }
5108
5109 #if __FreeBSD_version >= 1300042
5110 #ifndef _SYS_SYSPROTO_H_
5111 struct vop_need_inactive_args {
5112 struct vnode *a_vp;
5113 struct thread *a_td;
5114 };
5115 #endif
5116
5117 static int
5118 zfs_freebsd_need_inactive(struct vop_need_inactive_args *ap)
5119 {
5120 vnode_t *vp = ap->a_vp;
5121 znode_t *zp = VTOZ(vp);
5122 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5123 int need;
5124
5125 if (vn_need_pageq_flush(vp))
5126 return (1);
5127
5128 if (!ZFS_TEARDOWN_INACTIVE_TRY_ENTER_READ(zfsvfs))
5129 return (1);
5130 need = (zp->z_sa_hdl == NULL || zp->z_unlinked || zp->z_atime_dirty);
5131 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5132
5133 return (need);
5134 }
5135 #endif
5136
5137 #ifndef _SYS_SYSPROTO_H_
5138 struct vop_reclaim_args {
5139 struct vnode *a_vp;
5140 struct thread *a_td;
5141 };
5142 #endif
5143
5144 static int
5145 zfs_freebsd_reclaim(struct vop_reclaim_args *ap)
5146 {
5147 vnode_t *vp = ap->a_vp;
5148 znode_t *zp = VTOZ(vp);
5149 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5150
5151 ASSERT3P(zp, !=, NULL);
5152
5153 #if __FreeBSD_version < 1300042
5154 /* Destroy the vm object and flush associated pages. */
5155 vnode_destroy_vobject(vp);
5156 #endif
5157 /*
5158 * z_teardown_inactive_lock protects from a race with
5159 * zfs_znode_dmu_fini in zfsvfs_teardown during
5160 * force unmount.
5161 */
5162 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
5163 if (zp->z_sa_hdl == NULL)
5164 zfs_znode_free(zp);
5165 else
5166 zfs_zinactive(zp);
5167 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5168
5169 vp->v_data = NULL;
5170 return (0);
5171 }
5172
5173 #ifndef _SYS_SYSPROTO_H_
5174 struct vop_fid_args {
5175 struct vnode *a_vp;
5176 struct fid *a_fid;
5177 };
5178 #endif
5179
5180 static int
5181 zfs_freebsd_fid(struct vop_fid_args *ap)
5182 {
5183
5184 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5185 }
5186
5187
5188 #ifndef _SYS_SYSPROTO_H_
5189 struct vop_pathconf_args {
5190 struct vnode *a_vp;
5191 int a_name;
5192 register_t *a_retval;
5193 } *ap;
5194 #endif
5195
5196 static int
5197 zfs_freebsd_pathconf(struct vop_pathconf_args *ap)
5198 {
5199 ulong_t val;
5200 int error;
5201
5202 error = zfs_pathconf(ap->a_vp, ap->a_name, &val,
5203 curthread->td_ucred, NULL);
5204 if (error == 0) {
5205 *ap->a_retval = val;
5206 return (error);
5207 }
5208 if (error != EOPNOTSUPP)
5209 return (error);
5210
5211 switch (ap->a_name) {
5212 case _PC_NAME_MAX:
5213 *ap->a_retval = NAME_MAX;
5214 return (0);
5215 #if __FreeBSD_version >= 1400032
5216 case _PC_DEALLOC_PRESENT:
5217 *ap->a_retval = 1;
5218 return (0);
5219 #endif
5220 case _PC_PIPE_BUF:
5221 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO) {
5222 *ap->a_retval = PIPE_BUF;
5223 return (0);
5224 }
5225 return (EINVAL);
5226 default:
5227 return (vop_stdpathconf(ap));
5228 }
5229 }
5230
5231 static int zfs_xattr_compat = 1;
5232
5233 static int
5234 zfs_check_attrname(const char *name)
5235 {
5236 /* We don't allow '/' character in attribute name. */
5237 if (strchr(name, '/') != NULL)
5238 return (SET_ERROR(EINVAL));
5239 /* We don't allow attribute names that start with a namespace prefix. */
5240 if (ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5241 return (SET_ERROR(EINVAL));
5242 return (0);
5243 }
5244
5245 /*
5246 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5247 * extended attribute name:
5248 *
5249 * NAMESPACE XATTR_COMPAT PREFIX
5250 * system * freebsd:system:
5251 * user 1 (none, can be used to access ZFS
5252 * fsattr(5) attributes created on Solaris)
5253 * user 0 user.
5254 */
5255 static int
5256 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5257 size_t size, boolean_t compat)
5258 {
5259 const char *namespace, *prefix, *suffix;
5260
5261 memset(attrname, 0, size);
5262
5263 switch (attrnamespace) {
5264 case EXTATTR_NAMESPACE_USER:
5265 if (compat) {
5266 /*
5267 * This is the default namespace by which we can access
5268 * all attributes created on Solaris.
5269 */
5270 prefix = namespace = suffix = "";
5271 } else {
5272 /*
5273 * This is compatible with the user namespace encoding
5274 * on Linux prior to xattr_compat, but nothing
5275 * else.
5276 */
5277 prefix = "";
5278 namespace = "user";
5279 suffix = ".";
5280 }
5281 break;
5282 case EXTATTR_NAMESPACE_SYSTEM:
5283 prefix = "freebsd:";
5284 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5285 suffix = ":";
5286 break;
5287 case EXTATTR_NAMESPACE_EMPTY:
5288 default:
5289 return (SET_ERROR(EINVAL));
5290 }
5291 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5292 name) >= size) {
5293 return (SET_ERROR(ENAMETOOLONG));
5294 }
5295 return (0);
5296 }
5297
5298 static int
5299 zfs_ensure_xattr_cached(znode_t *zp)
5300 {
5301 int error = 0;
5302
5303 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5304
5305 if (zp->z_xattr_cached != NULL)
5306 return (0);
5307
5308 if (rw_write_held(&zp->z_xattr_lock))
5309 return (zfs_sa_get_xattr(zp));
5310
5311 if (!rw_tryupgrade(&zp->z_xattr_lock)) {
5312 rw_exit(&zp->z_xattr_lock);
5313 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5314 }
5315 if (zp->z_xattr_cached == NULL)
5316 error = zfs_sa_get_xattr(zp);
5317 rw_downgrade(&zp->z_xattr_lock);
5318 return (error);
5319 }
5320
5321 #ifndef _SYS_SYSPROTO_H_
5322 struct vop_getextattr {
5323 IN struct vnode *a_vp;
5324 IN int a_attrnamespace;
5325 IN const char *a_name;
5326 INOUT struct uio *a_uio;
5327 OUT size_t *a_size;
5328 IN struct ucred *a_cred;
5329 IN struct thread *a_td;
5330 };
5331 #endif
5332
5333 static int
5334 zfs_getextattr_dir(struct vop_getextattr_args *ap, const char *attrname)
5335 {
5336 struct thread *td = ap->a_td;
5337 struct nameidata nd;
5338 struct vattr va;
5339 vnode_t *xvp = NULL, *vp;
5340 int error, flags;
5341
5342 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5343 LOOKUP_XATTR, B_FALSE);
5344 if (error != 0)
5345 return (error);
5346
5347 flags = FREAD;
5348 #if __FreeBSD_version < 1400043
5349 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5350 xvp, td);
5351 #else
5352 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5353 #endif
5354 error = vn_open_cred(&nd, &flags, 0, VN_OPEN_INVFS, ap->a_cred, NULL);
5355 if (error != 0)
5356 return (SET_ERROR(error));
5357 vp = nd.ni_vp;
5358 NDFREE_PNBUF(&nd);
5359
5360 if (ap->a_size != NULL) {
5361 error = VOP_GETATTR(vp, &va, ap->a_cred);
5362 if (error == 0)
5363 *ap->a_size = (size_t)va.va_size;
5364 } else if (ap->a_uio != NULL)
5365 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5366
5367 VOP_UNLOCK1(vp);
5368 vn_close(vp, flags, ap->a_cred, td);
5369 return (error);
5370 }
5371
5372 static int
5373 zfs_getextattr_sa(struct vop_getextattr_args *ap, const char *attrname)
5374 {
5375 znode_t *zp = VTOZ(ap->a_vp);
5376 uchar_t *nv_value;
5377 uint_t nv_size;
5378 int error;
5379
5380 error = zfs_ensure_xattr_cached(zp);
5381 if (error != 0)
5382 return (error);
5383
5384 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5385 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5386
5387 error = nvlist_lookup_byte_array(zp->z_xattr_cached, attrname,
5388 &nv_value, &nv_size);
5389 if (error != 0)
5390 return (SET_ERROR(error));
5391
5392 if (ap->a_size != NULL)
5393 *ap->a_size = nv_size;
5394 else if (ap->a_uio != NULL)
5395 error = uiomove(nv_value, nv_size, ap->a_uio);
5396 if (error != 0)
5397 return (SET_ERROR(error));
5398
5399 return (0);
5400 }
5401
5402 static int
5403 zfs_getextattr_impl(struct vop_getextattr_args *ap, boolean_t compat)
5404 {
5405 znode_t *zp = VTOZ(ap->a_vp);
5406 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5407 char attrname[EXTATTR_MAXNAMELEN+1];
5408 int error;
5409
5410 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5411 sizeof (attrname), compat);
5412 if (error != 0)
5413 return (error);
5414
5415 error = ENOENT;
5416 if (zfsvfs->z_use_sa && zp->z_is_sa)
5417 error = zfs_getextattr_sa(ap, attrname);
5418 if (error == ENOENT)
5419 error = zfs_getextattr_dir(ap, attrname);
5420 return (error);
5421 }
5422
5423 /*
5424 * Vnode operation to retrieve a named extended attribute.
5425 */
5426 static int
5427 zfs_getextattr(struct vop_getextattr_args *ap)
5428 {
5429 znode_t *zp = VTOZ(ap->a_vp);
5430 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5431 int error;
5432
5433 /*
5434 * If the xattr property is off, refuse the request.
5435 */
5436 if (!(zfsvfs->z_flags & ZSB_XATTR))
5437 return (SET_ERROR(EOPNOTSUPP));
5438
5439 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5440 ap->a_cred, ap->a_td, VREAD);
5441 if (error != 0)
5442 return (SET_ERROR(error));
5443
5444 error = zfs_check_attrname(ap->a_name);
5445 if (error != 0)
5446 return (error);
5447
5448 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5449 return (error);
5450 error = ENOENT;
5451 rw_enter(&zp->z_xattr_lock, RW_READER);
5452
5453 error = zfs_getextattr_impl(ap, zfs_xattr_compat);
5454 if ((error == ENOENT || error == ENOATTR) &&
5455 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5456 /*
5457 * Fall back to the alternate namespace format if we failed to
5458 * find a user xattr.
5459 */
5460 error = zfs_getextattr_impl(ap, !zfs_xattr_compat);
5461 }
5462
5463 rw_exit(&zp->z_xattr_lock);
5464 zfs_exit(zfsvfs, FTAG);
5465 if (error == ENOENT)
5466 error = SET_ERROR(ENOATTR);
5467 return (error);
5468 }
5469
5470 #ifndef _SYS_SYSPROTO_H_
5471 struct vop_deleteextattr {
5472 IN struct vnode *a_vp;
5473 IN int a_attrnamespace;
5474 IN const char *a_name;
5475 IN struct ucred *a_cred;
5476 IN struct thread *a_td;
5477 };
5478 #endif
5479
5480 static int
5481 zfs_deleteextattr_dir(struct vop_deleteextattr_args *ap, const char *attrname)
5482 {
5483 struct nameidata nd;
5484 vnode_t *xvp = NULL, *vp;
5485 int error;
5486
5487 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5488 LOOKUP_XATTR, B_FALSE);
5489 if (error != 0)
5490 return (error);
5491
5492 #if __FreeBSD_version < 1400043
5493 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5494 UIO_SYSSPACE, attrname, xvp, ap->a_td);
5495 #else
5496 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5497 UIO_SYSSPACE, attrname, xvp);
5498 #endif
5499 error = namei(&nd);
5500 if (error != 0)
5501 return (SET_ERROR(error));
5502
5503 vp = nd.ni_vp;
5504 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
5505 NDFREE_PNBUF(&nd);
5506
5507 vput(nd.ni_dvp);
5508 if (vp == nd.ni_dvp)
5509 vrele(vp);
5510 else
5511 vput(vp);
5512
5513 return (error);
5514 }
5515
5516 static int
5517 zfs_deleteextattr_sa(struct vop_deleteextattr_args *ap, const char *attrname)
5518 {
5519 znode_t *zp = VTOZ(ap->a_vp);
5520 nvlist_t *nvl;
5521 int error;
5522
5523 error = zfs_ensure_xattr_cached(zp);
5524 if (error != 0)
5525 return (error);
5526
5527 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5528 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5529
5530 nvl = zp->z_xattr_cached;
5531 error = nvlist_remove(nvl, attrname, DATA_TYPE_BYTE_ARRAY);
5532 if (error != 0)
5533 error = SET_ERROR(error);
5534 else
5535 error = zfs_sa_set_xattr(zp, attrname, NULL, 0);
5536 if (error != 0) {
5537 zp->z_xattr_cached = NULL;
5538 nvlist_free(nvl);
5539 }
5540 return (error);
5541 }
5542
5543 static int
5544 zfs_deleteextattr_impl(struct vop_deleteextattr_args *ap, boolean_t compat)
5545 {
5546 znode_t *zp = VTOZ(ap->a_vp);
5547 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5548 char attrname[EXTATTR_MAXNAMELEN+1];
5549 int error;
5550
5551 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5552 sizeof (attrname), compat);
5553 if (error != 0)
5554 return (error);
5555
5556 error = ENOENT;
5557 if (zfsvfs->z_use_sa && zp->z_is_sa)
5558 error = zfs_deleteextattr_sa(ap, attrname);
5559 if (error == ENOENT)
5560 error = zfs_deleteextattr_dir(ap, attrname);
5561 return (error);
5562 }
5563
5564 /*
5565 * Vnode operation to remove a named attribute.
5566 */
5567 static int
5568 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
5569 {
5570 znode_t *zp = VTOZ(ap->a_vp);
5571 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5572 int error;
5573
5574 /*
5575 * If the xattr property is off, refuse the request.
5576 */
5577 if (!(zfsvfs->z_flags & ZSB_XATTR))
5578 return (SET_ERROR(EOPNOTSUPP));
5579
5580 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5581 ap->a_cred, ap->a_td, VWRITE);
5582 if (error != 0)
5583 return (SET_ERROR(error));
5584
5585 error = zfs_check_attrname(ap->a_name);
5586 if (error != 0)
5587 return (error);
5588
5589 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5590 return (error);
5591 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5592
5593 error = zfs_deleteextattr_impl(ap, zfs_xattr_compat);
5594 if ((error == ENOENT || error == ENOATTR) &&
5595 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5596 /*
5597 * Fall back to the alternate namespace format if we failed to
5598 * find a user xattr.
5599 */
5600 error = zfs_deleteextattr_impl(ap, !zfs_xattr_compat);
5601 }
5602
5603 rw_exit(&zp->z_xattr_lock);
5604 zfs_exit(zfsvfs, FTAG);
5605 if (error == ENOENT)
5606 error = SET_ERROR(ENOATTR);
5607 return (error);
5608 }
5609
5610 #ifndef _SYS_SYSPROTO_H_
5611 struct vop_setextattr {
5612 IN struct vnode *a_vp;
5613 IN int a_attrnamespace;
5614 IN const char *a_name;
5615 INOUT struct uio *a_uio;
5616 IN struct ucred *a_cred;
5617 IN struct thread *a_td;
5618 };
5619 #endif
5620
5621 static int
5622 zfs_setextattr_dir(struct vop_setextattr_args *ap, const char *attrname)
5623 {
5624 struct thread *td = ap->a_td;
5625 struct nameidata nd;
5626 struct vattr va;
5627 vnode_t *xvp = NULL, *vp;
5628 int error, flags;
5629
5630 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5631 LOOKUP_XATTR | CREATE_XATTR_DIR, B_FALSE);
5632 if (error != 0)
5633 return (error);
5634
5635 flags = FFLAGS(O_WRONLY | O_CREAT);
5636 #if __FreeBSD_version < 1400043
5637 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp, td);
5638 #else
5639 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5640 #endif
5641 error = vn_open_cred(&nd, &flags, 0600, VN_OPEN_INVFS, ap->a_cred,
5642 NULL);
5643 if (error != 0)
5644 return (SET_ERROR(error));
5645 vp = nd.ni_vp;
5646 NDFREE_PNBUF(&nd);
5647
5648 VATTR_NULL(&va);
5649 va.va_size = 0;
5650 error = VOP_SETATTR(vp, &va, ap->a_cred);
5651 if (error == 0)
5652 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5653
5654 VOP_UNLOCK1(vp);
5655 vn_close(vp, flags, ap->a_cred, td);
5656 return (error);
5657 }
5658
5659 static int
5660 zfs_setextattr_sa(struct vop_setextattr_args *ap, const char *attrname)
5661 {
5662 znode_t *zp = VTOZ(ap->a_vp);
5663 nvlist_t *nvl;
5664 size_t sa_size;
5665 int error;
5666
5667 error = zfs_ensure_xattr_cached(zp);
5668 if (error != 0)
5669 return (error);
5670
5671 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5672 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5673
5674 nvl = zp->z_xattr_cached;
5675 size_t entry_size = ap->a_uio->uio_resid;
5676 if (entry_size > DXATTR_MAX_ENTRY_SIZE)
5677 return (SET_ERROR(EFBIG));
5678 error = nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
5679 if (error != 0)
5680 return (SET_ERROR(error));
5681 if (sa_size > DXATTR_MAX_SA_SIZE)
5682 return (SET_ERROR(EFBIG));
5683 uchar_t *buf = kmem_alloc(entry_size, KM_SLEEP);
5684 error = uiomove(buf, entry_size, ap->a_uio);
5685 if (error != 0) {
5686 error = SET_ERROR(error);
5687 } else {
5688 error = nvlist_add_byte_array(nvl, attrname, buf, entry_size);
5689 if (error != 0)
5690 error = SET_ERROR(error);
5691 }
5692 if (error == 0)
5693 error = zfs_sa_set_xattr(zp, attrname, buf, entry_size);
5694 kmem_free(buf, entry_size);
5695 if (error != 0) {
5696 zp->z_xattr_cached = NULL;
5697 nvlist_free(nvl);
5698 }
5699 return (error);
5700 }
5701
5702 static int
5703 zfs_setextattr_impl(struct vop_setextattr_args *ap, boolean_t compat)
5704 {
5705 znode_t *zp = VTOZ(ap->a_vp);
5706 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5707 char attrname[EXTATTR_MAXNAMELEN+1];
5708 int error;
5709
5710 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5711 sizeof (attrname), compat);
5712 if (error != 0)
5713 return (error);
5714
5715 struct vop_deleteextattr_args vda = {
5716 .a_vp = ap->a_vp,
5717 .a_attrnamespace = ap->a_attrnamespace,
5718 .a_name = ap->a_name,
5719 .a_cred = ap->a_cred,
5720 .a_td = ap->a_td,
5721 };
5722 error = ENOENT;
5723 if (zfsvfs->z_use_sa && zp->z_is_sa && zfsvfs->z_xattr_sa) {
5724 error = zfs_setextattr_sa(ap, attrname);
5725 if (error == 0) {
5726 /*
5727 * Successfully put into SA, we need to clear the one
5728 * in dir if present.
5729 */
5730 zfs_deleteextattr_dir(&vda, attrname);
5731 }
5732 }
5733 if (error != 0) {
5734 error = zfs_setextattr_dir(ap, attrname);
5735 if (error == 0 && zp->z_is_sa) {
5736 /*
5737 * Successfully put into dir, we need to clear the one
5738 * in SA if present.
5739 */
5740 zfs_deleteextattr_sa(&vda, attrname);
5741 }
5742 }
5743 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5744 /*
5745 * Also clear all versions of the alternate compat name.
5746 */
5747 zfs_deleteextattr_impl(&vda, !compat);
5748 }
5749 return (error);
5750 }
5751
5752 /*
5753 * Vnode operation to set a named attribute.
5754 */
5755 static int
5756 zfs_setextattr(struct vop_setextattr_args *ap)
5757 {
5758 znode_t *zp = VTOZ(ap->a_vp);
5759 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5760 int error;
5761
5762 /*
5763 * If the xattr property is off, refuse the request.
5764 */
5765 if (!(zfsvfs->z_flags & ZSB_XATTR))
5766 return (SET_ERROR(EOPNOTSUPP));
5767
5768 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5769 ap->a_cred, ap->a_td, VWRITE);
5770 if (error != 0)
5771 return (SET_ERROR(error));
5772
5773 error = zfs_check_attrname(ap->a_name);
5774 if (error != 0)
5775 return (error);
5776
5777 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5778 return (error);
5779 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5780
5781 error = zfs_setextattr_impl(ap, zfs_xattr_compat);
5782
5783 rw_exit(&zp->z_xattr_lock);
5784 zfs_exit(zfsvfs, FTAG);
5785 return (error);
5786 }
5787
5788 #ifndef _SYS_SYSPROTO_H_
5789 struct vop_listextattr {
5790 IN struct vnode *a_vp;
5791 IN int a_attrnamespace;
5792 INOUT struct uio *a_uio;
5793 OUT size_t *a_size;
5794 IN struct ucred *a_cred;
5795 IN struct thread *a_td;
5796 };
5797 #endif
5798
5799 static int
5800 zfs_listextattr_dir(struct vop_listextattr_args *ap, const char *attrprefix)
5801 {
5802 struct thread *td = ap->a_td;
5803 struct nameidata nd;
5804 uint8_t dirbuf[sizeof (struct dirent)];
5805 struct iovec aiov;
5806 struct uio auio;
5807 vnode_t *xvp = NULL, *vp;
5808 int error, eof;
5809
5810 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5811 LOOKUP_XATTR, B_FALSE);
5812 if (error != 0) {
5813 /*
5814 * ENOATTR means that the EA directory does not yet exist,
5815 * i.e. there are no extended attributes there.
5816 */
5817 if (error == ENOATTR)
5818 error = 0;
5819 return (error);
5820 }
5821
5822 #if __FreeBSD_version < 1400043
5823 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5824 UIO_SYSSPACE, ".", xvp, td);
5825 #else
5826 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5827 UIO_SYSSPACE, ".", xvp);
5828 #endif
5829 error = namei(&nd);
5830 if (error != 0)
5831 return (SET_ERROR(error));
5832 vp = nd.ni_vp;
5833 NDFREE_PNBUF(&nd);
5834
5835 auio.uio_iov = &aiov;
5836 auio.uio_iovcnt = 1;
5837 auio.uio_segflg = UIO_SYSSPACE;
5838 auio.uio_td = td;
5839 auio.uio_rw = UIO_READ;
5840 auio.uio_offset = 0;
5841
5842 size_t plen = strlen(attrprefix);
5843
5844 do {
5845 aiov.iov_base = (void *)dirbuf;
5846 aiov.iov_len = sizeof (dirbuf);
5847 auio.uio_resid = sizeof (dirbuf);
5848 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5849 if (error != 0)
5850 break;
5851 int done = sizeof (dirbuf) - auio.uio_resid;
5852 for (int pos = 0; pos < done; ) {
5853 struct dirent *dp = (struct dirent *)(dirbuf + pos);
5854 pos += dp->d_reclen;
5855 /*
5856 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5857 * is what we get when attribute was created on Solaris.
5858 */
5859 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5860 continue;
5861 else if (plen == 0 &&
5862 ZFS_XA_NS_PREFIX_FORBIDDEN(dp->d_name))
5863 continue;
5864 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5865 continue;
5866 uint8_t nlen = dp->d_namlen - plen;
5867 if (ap->a_size != NULL) {
5868 *ap->a_size += 1 + nlen;
5869 } else if (ap->a_uio != NULL) {
5870 /*
5871 * Format of extattr name entry is one byte for
5872 * length and the rest for name.
5873 */
5874 error = uiomove(&nlen, 1, ap->a_uio);
5875 if (error == 0) {
5876 char *namep = dp->d_name + plen;
5877 error = uiomove(namep, nlen, ap->a_uio);
5878 }
5879 if (error != 0) {
5880 error = SET_ERROR(error);
5881 break;
5882 }
5883 }
5884 }
5885 } while (!eof && error == 0);
5886
5887 vput(vp);
5888 return (error);
5889 }
5890
5891 static int
5892 zfs_listextattr_sa(struct vop_listextattr_args *ap, const char *attrprefix)
5893 {
5894 znode_t *zp = VTOZ(ap->a_vp);
5895 int error;
5896
5897 error = zfs_ensure_xattr_cached(zp);
5898 if (error != 0)
5899 return (error);
5900
5901 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5902 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5903
5904 size_t plen = strlen(attrprefix);
5905 nvpair_t *nvp = NULL;
5906 while ((nvp = nvlist_next_nvpair(zp->z_xattr_cached, nvp)) != NULL) {
5907 ASSERT3U(nvpair_type(nvp), ==, DATA_TYPE_BYTE_ARRAY);
5908
5909 const char *name = nvpair_name(nvp);
5910 if (plen == 0 && ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5911 continue;
5912 else if (strncmp(name, attrprefix, plen) != 0)
5913 continue;
5914 uint8_t nlen = strlen(name) - plen;
5915 if (ap->a_size != NULL) {
5916 *ap->a_size += 1 + nlen;
5917 } else if (ap->a_uio != NULL) {
5918 /*
5919 * Format of extattr name entry is one byte for
5920 * length and the rest for name.
5921 */
5922 error = uiomove(&nlen, 1, ap->a_uio);
5923 if (error == 0) {
5924 char *namep = __DECONST(char *, name) + plen;
5925 error = uiomove(namep, nlen, ap->a_uio);
5926 }
5927 if (error != 0) {
5928 error = SET_ERROR(error);
5929 break;
5930 }
5931 }
5932 }
5933
5934 return (error);
5935 }
5936
5937 static int
5938 zfs_listextattr_impl(struct vop_listextattr_args *ap, boolean_t compat)
5939 {
5940 znode_t *zp = VTOZ(ap->a_vp);
5941 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5942 char attrprefix[16];
5943 int error;
5944
5945 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5946 sizeof (attrprefix), compat);
5947 if (error != 0)
5948 return (error);
5949
5950 if (zfsvfs->z_use_sa && zp->z_is_sa)
5951 error = zfs_listextattr_sa(ap, attrprefix);
5952 if (error == 0)
5953 error = zfs_listextattr_dir(ap, attrprefix);
5954 return (error);
5955 }
5956
5957 /*
5958 * Vnode operation to retrieve extended attributes on a vnode.
5959 */
5960 static int
5961 zfs_listextattr(struct vop_listextattr_args *ap)
5962 {
5963 znode_t *zp = VTOZ(ap->a_vp);
5964 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5965 int error;
5966
5967 if (ap->a_size != NULL)
5968 *ap->a_size = 0;
5969
5970 /*
5971 * If the xattr property is off, refuse the request.
5972 */
5973 if (!(zfsvfs->z_flags & ZSB_XATTR))
5974 return (SET_ERROR(EOPNOTSUPP));
5975
5976 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5977 ap->a_cred, ap->a_td, VREAD);
5978 if (error != 0)
5979 return (SET_ERROR(error));
5980
5981 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5982 return (error);
5983 rw_enter(&zp->z_xattr_lock, RW_READER);
5984
5985 error = zfs_listextattr_impl(ap, zfs_xattr_compat);
5986 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5987 /* Also list user xattrs with the alternate format. */
5988 error = zfs_listextattr_impl(ap, !zfs_xattr_compat);
5989 }
5990
5991 rw_exit(&zp->z_xattr_lock);
5992 zfs_exit(zfsvfs, FTAG);
5993 return (error);
5994 }
5995
5996 #ifndef _SYS_SYSPROTO_H_
5997 struct vop_getacl_args {
5998 struct vnode *vp;
5999 acl_type_t type;
6000 struct acl *aclp;
6001 struct ucred *cred;
6002 struct thread *td;
6003 };
6004 #endif
6005
6006 static int
6007 zfs_freebsd_getacl(struct vop_getacl_args *ap)
6008 {
6009 int error;
6010 vsecattr_t vsecattr;
6011
6012 if (ap->a_type != ACL_TYPE_NFS4)
6013 return (EINVAL);
6014
6015 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
6016 if ((error = zfs_getsecattr(VTOZ(ap->a_vp),
6017 &vsecattr, 0, ap->a_cred)))
6018 return (error);
6019
6020 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp,
6021 vsecattr.vsa_aclcnt);
6022 if (vsecattr.vsa_aclentp != NULL)
6023 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
6024
6025 return (error);
6026 }
6027
6028 #ifndef _SYS_SYSPROTO_H_
6029 struct vop_setacl_args {
6030 struct vnode *vp;
6031 acl_type_t type;
6032 struct acl *aclp;
6033 struct ucred *cred;
6034 struct thread *td;
6035 };
6036 #endif
6037
6038 static int
6039 zfs_freebsd_setacl(struct vop_setacl_args *ap)
6040 {
6041 int error;
6042 vsecattr_t vsecattr;
6043 int aclbsize; /* size of acl list in bytes */
6044 aclent_t *aaclp;
6045
6046 if (ap->a_type != ACL_TYPE_NFS4)
6047 return (EINVAL);
6048
6049 if (ap->a_aclp == NULL)
6050 return (EINVAL);
6051
6052 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
6053 return (EINVAL);
6054
6055 /*
6056 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
6057 * splitting every entry into two and appending "canonical six"
6058 * entries at the end. Don't allow for setting an ACL that would
6059 * cause chmod(2) to run out of ACL entries.
6060 */
6061 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
6062 return (ENOSPC);
6063
6064 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
6065 if (error != 0)
6066 return (error);
6067
6068 vsecattr.vsa_mask = VSA_ACE;
6069 aclbsize = ap->a_aclp->acl_cnt * sizeof (ace_t);
6070 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
6071 aaclp = vsecattr.vsa_aclentp;
6072 vsecattr.vsa_aclentsz = aclbsize;
6073
6074 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
6075 error = zfs_setsecattr(VTOZ(ap->a_vp), &vsecattr, 0, ap->a_cred);
6076 kmem_free(aaclp, aclbsize);
6077
6078 return (error);
6079 }
6080
6081 #ifndef _SYS_SYSPROTO_H_
6082 struct vop_aclcheck_args {
6083 struct vnode *vp;
6084 acl_type_t type;
6085 struct acl *aclp;
6086 struct ucred *cred;
6087 struct thread *td;
6088 };
6089 #endif
6090
6091 static int
6092 zfs_freebsd_aclcheck(struct vop_aclcheck_args *ap)
6093 {
6094
6095 return (EOPNOTSUPP);
6096 }
6097
6098 static int
6099 zfs_vptocnp(struct vop_vptocnp_args *ap)
6100 {
6101 vnode_t *covered_vp;
6102 vnode_t *vp = ap->a_vp;
6103 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
6104 znode_t *zp = VTOZ(vp);
6105 int ltype;
6106 int error;
6107
6108 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
6109 return (error);
6110
6111 /*
6112 * If we are a snapshot mounted under .zfs, run the operation
6113 * on the covered vnode.
6114 */
6115 if (zp->z_id != zfsvfs->z_root || zfsvfs->z_parent == zfsvfs) {
6116 char name[MAXNAMLEN + 1];
6117 znode_t *dzp;
6118 size_t len;
6119
6120 error = zfs_znode_parent_and_name(zp, &dzp, name);
6121 if (error == 0) {
6122 len = strlen(name);
6123 if (*ap->a_buflen < len)
6124 error = SET_ERROR(ENOMEM);
6125 }
6126 if (error == 0) {
6127 *ap->a_buflen -= len;
6128 memcpy(ap->a_buf + *ap->a_buflen, name, len);
6129 *ap->a_vpp = ZTOV(dzp);
6130 }
6131 zfs_exit(zfsvfs, FTAG);
6132 return (error);
6133 }
6134 zfs_exit(zfsvfs, FTAG);
6135
6136 covered_vp = vp->v_mount->mnt_vnodecovered;
6137 #if __FreeBSD_version >= 1300045
6138 enum vgetstate vs = vget_prep(covered_vp);
6139 #else
6140 vhold(covered_vp);
6141 #endif
6142 ltype = VOP_ISLOCKED(vp);
6143 VOP_UNLOCK1(vp);
6144 #if __FreeBSD_version >= 1300045
6145 error = vget_finish(covered_vp, LK_SHARED, vs);
6146 #else
6147 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
6148 #endif
6149 if (error == 0) {
6150 #if __FreeBSD_version >= 1300123
6151 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_buf,
6152 ap->a_buflen);
6153 #else
6154 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
6155 ap->a_buf, ap->a_buflen);
6156 #endif
6157 vput(covered_vp);
6158 }
6159 vn_lock(vp, ltype | LK_RETRY);
6160 if (VN_IS_DOOMED(vp))
6161 error = SET_ERROR(ENOENT);
6162 return (error);
6163 }
6164
6165 #if __FreeBSD_version >= 1400032
6166 static int
6167 zfs_deallocate(struct vop_deallocate_args *ap)
6168 {
6169 znode_t *zp = VTOZ(ap->a_vp);
6170 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6171 zilog_t *zilog;
6172 off_t off, len, file_sz;
6173 int error;
6174
6175 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
6176 return (error);
6177
6178 /*
6179 * Callers might not be able to detect properly that we are read-only,
6180 * so check it explicitly here.
6181 */
6182 if (zfs_is_readonly(zfsvfs)) {
6183 zfs_exit(zfsvfs, FTAG);
6184 return (SET_ERROR(EROFS));
6185 }
6186
6187 zilog = zfsvfs->z_log;
6188 off = *ap->a_offset;
6189 len = *ap->a_len;
6190 file_sz = zp->z_size;
6191 if (off + len > file_sz)
6192 len = file_sz - off;
6193 /* Fast path for out-of-range request. */
6194 if (len <= 0) {
6195 *ap->a_len = 0;
6196 zfs_exit(zfsvfs, FTAG);
6197 return (0);
6198 }
6199
6200 error = zfs_freesp(zp, off, len, O_RDWR, TRUE);
6201 if (error == 0) {
6202 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS ||
6203 (ap->a_ioflag & IO_SYNC) != 0)
6204 zil_commit(zilog, zp->z_id);
6205 *ap->a_offset = off + len;
6206 *ap->a_len = 0;
6207 }
6208
6209 zfs_exit(zfsvfs, FTAG);
6210 return (error);
6211 }
6212 #endif
6213
6214 struct vop_vector zfs_vnodeops;
6215 struct vop_vector zfs_fifoops;
6216 struct vop_vector zfs_shareops;
6217
6218 struct vop_vector zfs_vnodeops = {
6219 .vop_default = &default_vnodeops,
6220 .vop_inactive = zfs_freebsd_inactive,
6221 #if __FreeBSD_version >= 1300042
6222 .vop_need_inactive = zfs_freebsd_need_inactive,
6223 #endif
6224 .vop_reclaim = zfs_freebsd_reclaim,
6225 #if __FreeBSD_version >= 1300102
6226 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6227 #endif
6228 #if __FreeBSD_version >= 1300139
6229 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6230 #endif
6231 .vop_access = zfs_freebsd_access,
6232 .vop_allocate = VOP_EINVAL,
6233 #if __FreeBSD_version >= 1400032
6234 .vop_deallocate = zfs_deallocate,
6235 #endif
6236 .vop_lookup = zfs_cache_lookup,
6237 .vop_cachedlookup = zfs_freebsd_cachedlookup,
6238 .vop_getattr = zfs_freebsd_getattr,
6239 .vop_setattr = zfs_freebsd_setattr,
6240 .vop_create = zfs_freebsd_create,
6241 .vop_mknod = (vop_mknod_t *)zfs_freebsd_create,
6242 .vop_mkdir = zfs_freebsd_mkdir,
6243 .vop_readdir = zfs_freebsd_readdir,
6244 .vop_fsync = zfs_freebsd_fsync,
6245 .vop_open = zfs_freebsd_open,
6246 .vop_close = zfs_freebsd_close,
6247 .vop_rmdir = zfs_freebsd_rmdir,
6248 .vop_ioctl = zfs_freebsd_ioctl,
6249 .vop_link = zfs_freebsd_link,
6250 .vop_symlink = zfs_freebsd_symlink,
6251 .vop_readlink = zfs_freebsd_readlink,
6252 .vop_read = zfs_freebsd_read,
6253 .vop_write = zfs_freebsd_write,
6254 .vop_remove = zfs_freebsd_remove,
6255 .vop_rename = zfs_freebsd_rename,
6256 .vop_pathconf = zfs_freebsd_pathconf,
6257 .vop_bmap = zfs_freebsd_bmap,
6258 .vop_fid = zfs_freebsd_fid,
6259 .vop_getextattr = zfs_getextattr,
6260 .vop_deleteextattr = zfs_deleteextattr,
6261 .vop_setextattr = zfs_setextattr,
6262 .vop_listextattr = zfs_listextattr,
6263 .vop_getacl = zfs_freebsd_getacl,
6264 .vop_setacl = zfs_freebsd_setacl,
6265 .vop_aclcheck = zfs_freebsd_aclcheck,
6266 .vop_getpages = zfs_freebsd_getpages,
6267 .vop_putpages = zfs_freebsd_putpages,
6268 .vop_vptocnp = zfs_vptocnp,
6269 #if __FreeBSD_version >= 1300064
6270 .vop_lock1 = vop_lock,
6271 .vop_unlock = vop_unlock,
6272 .vop_islocked = vop_islocked,
6273 #endif
6274 #if __FreeBSD_version >= 1400043
6275 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6276 #endif
6277 };
6278 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops);
6279
6280 struct vop_vector zfs_fifoops = {
6281 .vop_default = &fifo_specops,
6282 .vop_fsync = zfs_freebsd_fsync,
6283 #if __FreeBSD_version >= 1300102
6284 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6285 #endif
6286 #if __FreeBSD_version >= 1300139
6287 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6288 #endif
6289 .vop_access = zfs_freebsd_access,
6290 .vop_getattr = zfs_freebsd_getattr,
6291 .vop_inactive = zfs_freebsd_inactive,
6292 .vop_read = VOP_PANIC,
6293 .vop_reclaim = zfs_freebsd_reclaim,
6294 .vop_setattr = zfs_freebsd_setattr,
6295 .vop_write = VOP_PANIC,
6296 .vop_pathconf = zfs_freebsd_pathconf,
6297 .vop_fid = zfs_freebsd_fid,
6298 .vop_getacl = zfs_freebsd_getacl,
6299 .vop_setacl = zfs_freebsd_setacl,
6300 .vop_aclcheck = zfs_freebsd_aclcheck,
6301 #if __FreeBSD_version >= 1400043
6302 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6303 #endif
6304 };
6305 VFS_VOP_VECTOR_REGISTER(zfs_fifoops);
6306
6307 /*
6308 * special share hidden files vnode operations template
6309 */
6310 struct vop_vector zfs_shareops = {
6311 .vop_default = &default_vnodeops,
6312 #if __FreeBSD_version >= 1300121
6313 .vop_fplookup_vexec = VOP_EAGAIN,
6314 #endif
6315 #if __FreeBSD_version >= 1300139
6316 .vop_fplookup_symlink = VOP_EAGAIN,
6317 #endif
6318 .vop_access = zfs_freebsd_access,
6319 .vop_inactive = zfs_freebsd_inactive,
6320 .vop_reclaim = zfs_freebsd_reclaim,
6321 .vop_fid = zfs_freebsd_fid,
6322 .vop_pathconf = zfs_freebsd_pathconf,
6323 #if __FreeBSD_version >= 1400043
6324 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6325 #endif
6326 };
6327 VFS_VOP_VECTOR_REGISTER(zfs_shareops);
6328
6329 ZFS_MODULE_PARAM(zfs, zfs_, xattr_compat, INT, ZMOD_RW,
6330 "Use legacy ZFS xattr naming for writing new user namespace xattrs");