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 } else {
3377 /*
3378 * At this point, we have successfully created
3379 * the target name, but have failed to remove
3380 * the source name. Since the create was done
3381 * with the ZRENAMING flag, there are
3382 * complications; for one, the link count is
3383 * wrong. The easiest way to deal with this
3384 * is to remove the newly created target, and
3385 * return the original error. This must
3386 * succeed; fortunately, it is very unlikely to
3387 * fail, since we just created it.
3388 */
3389 VERIFY0(zfs_link_destroy(tdzp, tnm, szp, tx,
3390 ZRENAMING, NULL));
3391 }
3392 }
3393 if (error == 0) {
3394 cache_vop_rename(sdvp, *svpp, tdvp, *tvpp, scnp, tcnp);
3395 }
3396 }
3397
3398 dmu_tx_commit(tx);
3399
3400 out_seq:
3401 vn_seqc_write_end(*svpp);
3402 vn_seqc_write_end(sdvp);
3403 if (*tvpp != NULL)
3404 vn_seqc_write_end(*tvpp);
3405 if (tdvp != *tvpp)
3406 vn_seqc_write_end(tdvp);
3407
3408 out:
3409 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3410 zil_commit(zilog, 0);
3411 zfs_exit(zfsvfs, FTAG);
3412
3413 return (error);
3414 }
3415
3416 int
3417 zfs_rename(znode_t *sdzp, const char *sname, znode_t *tdzp, const char *tname,
3418 cred_t *cr, int flags, uint64_t rflags, vattr_t *wo_vap, zuserns_t *mnt_ns)
3419 {
3420 struct componentname scn, tcn;
3421 vnode_t *sdvp, *tdvp;
3422 vnode_t *svp, *tvp;
3423 int error;
3424 svp = tvp = NULL;
3425
3426 if (rflags != 0 || wo_vap != NULL)
3427 return (SET_ERROR(EINVAL));
3428
3429 sdvp = ZTOV(sdzp);
3430 tdvp = ZTOV(tdzp);
3431 error = zfs_lookup_internal(sdzp, sname, &svp, &scn, DELETE);
3432 if (sdzp->z_zfsvfs->z_replay == B_FALSE)
3433 VOP_UNLOCK1(sdvp);
3434 if (error != 0)
3435 goto fail;
3436 VOP_UNLOCK1(svp);
3437
3438 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
3439 error = zfs_lookup_internal(tdzp, tname, &tvp, &tcn, RENAME);
3440 if (error == EJUSTRETURN)
3441 tvp = NULL;
3442 else if (error != 0) {
3443 VOP_UNLOCK1(tdvp);
3444 goto fail;
3445 }
3446
3447 error = zfs_do_rename(sdvp, &svp, &scn, tdvp, &tvp, &tcn, cr);
3448 fail:
3449 if (svp != NULL)
3450 vrele(svp);
3451 if (tvp != NULL)
3452 vrele(tvp);
3453
3454 return (error);
3455 }
3456
3457 /*
3458 * Insert the indicated symbolic reference entry into the directory.
3459 *
3460 * IN: dvp - Directory to contain new symbolic link.
3461 * link - Name for new symlink entry.
3462 * vap - Attributes of new entry.
3463 * cr - credentials of caller.
3464 * ct - caller context
3465 * flags - case flags
3466 * mnt_ns - Unused on FreeBSD
3467 *
3468 * RETURN: 0 on success, error code on failure.
3469 *
3470 * Timestamps:
3471 * dvp - ctime|mtime updated
3472 */
3473 int
3474 zfs_symlink(znode_t *dzp, const char *name, vattr_t *vap,
3475 const char *link, znode_t **zpp, cred_t *cr, int flags, zuserns_t *mnt_ns)
3476 {
3477 (void) flags;
3478 znode_t *zp;
3479 dmu_tx_t *tx;
3480 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3481 zilog_t *zilog;
3482 uint64_t len = strlen(link);
3483 int error;
3484 zfs_acl_ids_t acl_ids;
3485 boolean_t fuid_dirtied;
3486 uint64_t txtype = TX_SYMLINK;
3487
3488 ASSERT3S(vap->va_type, ==, VLNK);
3489
3490 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
3491 return (error);
3492 zilog = zfsvfs->z_log;
3493
3494 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3495 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3496 zfs_exit(zfsvfs, FTAG);
3497 return (SET_ERROR(EILSEQ));
3498 }
3499
3500 if (len > MAXPATHLEN) {
3501 zfs_exit(zfsvfs, FTAG);
3502 return (SET_ERROR(ENAMETOOLONG));
3503 }
3504
3505 if ((error = zfs_acl_ids_create(dzp, 0,
3506 vap, cr, NULL, &acl_ids, NULL)) != 0) {
3507 zfs_exit(zfsvfs, FTAG);
3508 return (error);
3509 }
3510
3511 /*
3512 * Attempt to lock directory; fail if entry already exists.
3513 */
3514 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
3515 if (error) {
3516 zfs_acl_ids_free(&acl_ids);
3517 zfs_exit(zfsvfs, FTAG);
3518 return (error);
3519 }
3520
3521 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) {
3522 zfs_acl_ids_free(&acl_ids);
3523 zfs_exit(zfsvfs, FTAG);
3524 return (error);
3525 }
3526
3527 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids,
3528 0 /* projid */)) {
3529 zfs_acl_ids_free(&acl_ids);
3530 zfs_exit(zfsvfs, FTAG);
3531 return (SET_ERROR(EDQUOT));
3532 }
3533
3534 getnewvnode_reserve_();
3535 tx = dmu_tx_create(zfsvfs->z_os);
3536 fuid_dirtied = zfsvfs->z_fuid_dirty;
3537 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3538 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3539 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3540 ZFS_SA_BASE_ATTR_SIZE + len);
3541 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3542 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3543 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3544 acl_ids.z_aclp->z_acl_bytes);
3545 }
3546 if (fuid_dirtied)
3547 zfs_fuid_txhold(zfsvfs, tx);
3548 error = dmu_tx_assign(tx, TXG_WAIT);
3549 if (error) {
3550 zfs_acl_ids_free(&acl_ids);
3551 dmu_tx_abort(tx);
3552 getnewvnode_drop_reserve();
3553 zfs_exit(zfsvfs, FTAG);
3554 return (error);
3555 }
3556
3557 /*
3558 * Create a new object for the symlink.
3559 * for version 4 ZPL datasets the symlink will be an SA attribute
3560 */
3561 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3562
3563 if (fuid_dirtied)
3564 zfs_fuid_sync(zfsvfs, tx);
3565
3566 if (zp->z_is_sa)
3567 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3568 __DECONST(void *, link), len, tx);
3569 else
3570 zfs_sa_symlink(zp, __DECONST(char *, link), len, tx);
3571
3572 zp->z_size = len;
3573 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3574 &zp->z_size, sizeof (zp->z_size), tx);
3575 /*
3576 * Insert the new object into the directory.
3577 */
3578 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
3579
3580 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3581 *zpp = zp;
3582
3583 zfs_acl_ids_free(&acl_ids);
3584
3585 dmu_tx_commit(tx);
3586
3587 getnewvnode_drop_reserve();
3588
3589 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3590 zil_commit(zilog, 0);
3591
3592 zfs_exit(zfsvfs, FTAG);
3593 return (error);
3594 }
3595
3596 /*
3597 * Return, in the buffer contained in the provided uio structure,
3598 * the symbolic path referred to by vp.
3599 *
3600 * IN: vp - vnode of symbolic link.
3601 * uio - structure to contain the link path.
3602 * cr - credentials of caller.
3603 * ct - caller context
3604 *
3605 * OUT: uio - structure containing the link path.
3606 *
3607 * RETURN: 0 on success, error code on failure.
3608 *
3609 * Timestamps:
3610 * vp - atime updated
3611 */
3612 static int
3613 zfs_readlink(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, caller_context_t *ct)
3614 {
3615 (void) cr, (void) ct;
3616 znode_t *zp = VTOZ(vp);
3617 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3618 int error;
3619
3620 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3621 return (error);
3622
3623 if (zp->z_is_sa)
3624 error = sa_lookup_uio(zp->z_sa_hdl,
3625 SA_ZPL_SYMLINK(zfsvfs), uio);
3626 else
3627 error = zfs_sa_readlink(zp, uio);
3628
3629 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3630
3631 zfs_exit(zfsvfs, FTAG);
3632 return (error);
3633 }
3634
3635 /*
3636 * Insert a new entry into directory tdvp referencing svp.
3637 *
3638 * IN: tdvp - Directory to contain new entry.
3639 * svp - vnode of new entry.
3640 * name - name of new entry.
3641 * cr - credentials of caller.
3642 *
3643 * RETURN: 0 on success, error code on failure.
3644 *
3645 * Timestamps:
3646 * tdvp - ctime|mtime updated
3647 * svp - ctime updated
3648 */
3649 int
3650 zfs_link(znode_t *tdzp, znode_t *szp, const char *name, cred_t *cr,
3651 int flags)
3652 {
3653 (void) flags;
3654 znode_t *tzp;
3655 zfsvfs_t *zfsvfs = tdzp->z_zfsvfs;
3656 zilog_t *zilog;
3657 dmu_tx_t *tx;
3658 int error;
3659 uint64_t parent;
3660 uid_t owner;
3661
3662 ASSERT3S(ZTOV(tdzp)->v_type, ==, VDIR);
3663
3664 if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0)
3665 return (error);
3666 zilog = zfsvfs->z_log;
3667
3668 /*
3669 * POSIX dictates that we return EPERM here.
3670 * Better choices include ENOTSUP or EISDIR.
3671 */
3672 if (ZTOV(szp)->v_type == VDIR) {
3673 zfs_exit(zfsvfs, FTAG);
3674 return (SET_ERROR(EPERM));
3675 }
3676
3677 if ((error = zfs_verify_zp(szp)) != 0) {
3678 zfs_exit(zfsvfs, FTAG);
3679 return (error);
3680 }
3681
3682 /*
3683 * If we are using project inheritance, means if the directory has
3684 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3685 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3686 * such case, we only allow hard link creation in our tree when the
3687 * project IDs are the same.
3688 */
3689 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3690 tdzp->z_projid != szp->z_projid) {
3691 zfs_exit(zfsvfs, FTAG);
3692 return (SET_ERROR(EXDEV));
3693 }
3694
3695 if (szp->z_pflags & (ZFS_APPENDONLY |
3696 ZFS_IMMUTABLE | ZFS_READONLY)) {
3697 zfs_exit(zfsvfs, FTAG);
3698 return (SET_ERROR(EPERM));
3699 }
3700
3701 /* Prevent links to .zfs/shares files */
3702
3703 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
3704 &parent, sizeof (uint64_t))) != 0) {
3705 zfs_exit(zfsvfs, FTAG);
3706 return (error);
3707 }
3708 if (parent == zfsvfs->z_shares_dir) {
3709 zfs_exit(zfsvfs, FTAG);
3710 return (SET_ERROR(EPERM));
3711 }
3712
3713 if (zfsvfs->z_utf8 && u8_validate(name,
3714 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3715 zfs_exit(zfsvfs, FTAG);
3716 return (SET_ERROR(EILSEQ));
3717 }
3718
3719 /*
3720 * We do not support links between attributes and non-attributes
3721 * because of the potential security risk of creating links
3722 * into "normal" file space in order to circumvent restrictions
3723 * imposed in attribute space.
3724 */
3725 if ((szp->z_pflags & ZFS_XATTR) != (tdzp->z_pflags & ZFS_XATTR)) {
3726 zfs_exit(zfsvfs, FTAG);
3727 return (SET_ERROR(EINVAL));
3728 }
3729
3730
3731 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
3732 if (owner != crgetuid(cr) && secpolicy_basic_link(ZTOV(szp), cr) != 0) {
3733 zfs_exit(zfsvfs, FTAG);
3734 return (SET_ERROR(EPERM));
3735 }
3736
3737 if ((error = zfs_zaccess(tdzp, ACE_ADD_FILE, 0, B_FALSE, cr, NULL))) {
3738 zfs_exit(zfsvfs, FTAG);
3739 return (error);
3740 }
3741
3742 /*
3743 * Attempt to lock directory; fail if entry already exists.
3744 */
3745 error = zfs_dirent_lookup(tdzp, name, &tzp, ZNEW);
3746 if (error) {
3747 zfs_exit(zfsvfs, FTAG);
3748 return (error);
3749 }
3750
3751 tx = dmu_tx_create(zfsvfs->z_os);
3752 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3753 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, name);
3754 zfs_sa_upgrade_txholds(tx, szp);
3755 zfs_sa_upgrade_txholds(tx, tdzp);
3756 error = dmu_tx_assign(tx, TXG_WAIT);
3757 if (error) {
3758 dmu_tx_abort(tx);
3759 zfs_exit(zfsvfs, FTAG);
3760 return (error);
3761 }
3762
3763 error = zfs_link_create(tdzp, name, szp, tx, 0);
3764
3765 if (error == 0) {
3766 uint64_t txtype = TX_LINK;
3767 zfs_log_link(zilog, tx, txtype, tdzp, szp, name);
3768 }
3769
3770 dmu_tx_commit(tx);
3771
3772 if (error == 0) {
3773 vnevent_link(ZTOV(szp), ct);
3774 }
3775
3776 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3777 zil_commit(zilog, 0);
3778
3779 zfs_exit(zfsvfs, FTAG);
3780 return (error);
3781 }
3782
3783 /*
3784 * Free or allocate space in a file. Currently, this function only
3785 * supports the `F_FREESP' command. However, this command is somewhat
3786 * misnamed, as its functionality includes the ability to allocate as
3787 * well as free space.
3788 *
3789 * IN: ip - inode of file to free data in.
3790 * cmd - action to take (only F_FREESP supported).
3791 * bfp - section of file to free/alloc.
3792 * flag - current file open mode flags.
3793 * offset - current file offset.
3794 * cr - credentials of caller.
3795 *
3796 * RETURN: 0 on success, error code on failure.
3797 *
3798 * Timestamps:
3799 * ip - ctime|mtime updated
3800 */
3801 int
3802 zfs_space(znode_t *zp, int cmd, flock64_t *bfp, int flag,
3803 offset_t offset, cred_t *cr)
3804 {
3805 (void) offset;
3806 zfsvfs_t *zfsvfs = ZTOZSB(zp);
3807 uint64_t off, len;
3808 int error;
3809
3810 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3811 return (error);
3812
3813 if (cmd != F_FREESP) {
3814 zfs_exit(zfsvfs, FTAG);
3815 return (SET_ERROR(EINVAL));
3816 }
3817
3818 /*
3819 * Callers might not be able to detect properly that we are read-only,
3820 * so check it explicitly here.
3821 */
3822 if (zfs_is_readonly(zfsvfs)) {
3823 zfs_exit(zfsvfs, FTAG);
3824 return (SET_ERROR(EROFS));
3825 }
3826
3827 if (bfp->l_len < 0) {
3828 zfs_exit(zfsvfs, FTAG);
3829 return (SET_ERROR(EINVAL));
3830 }
3831
3832 /*
3833 * Permissions aren't checked on Solaris because on this OS
3834 * zfs_space() can only be called with an opened file handle.
3835 * On Linux we can get here through truncate_range() which
3836 * operates directly on inodes, so we need to check access rights.
3837 */
3838 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr, NULL))) {
3839 zfs_exit(zfsvfs, FTAG);
3840 return (error);
3841 }
3842
3843 off = bfp->l_start;
3844 len = bfp->l_len; /* 0 means from off to end of file */
3845
3846 error = zfs_freesp(zp, off, len, flag, TRUE);
3847
3848 zfs_exit(zfsvfs, FTAG);
3849 return (error);
3850 }
3851
3852 static void
3853 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3854 {
3855 (void) cr, (void) ct;
3856 znode_t *zp = VTOZ(vp);
3857 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3858 int error;
3859
3860 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
3861 if (zp->z_sa_hdl == NULL) {
3862 /*
3863 * The fs has been unmounted, or we did a
3864 * suspend/resume and this file no longer exists.
3865 */
3866 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3867 vrecycle(vp);
3868 return;
3869 }
3870
3871 if (zp->z_unlinked) {
3872 /*
3873 * Fast path to recycle a vnode of a removed file.
3874 */
3875 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3876 vrecycle(vp);
3877 return;
3878 }
3879
3880 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3881 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3882
3883 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3884 zfs_sa_upgrade_txholds(tx, zp);
3885 error = dmu_tx_assign(tx, TXG_WAIT);
3886 if (error) {
3887 dmu_tx_abort(tx);
3888 } else {
3889 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
3890 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
3891 zp->z_atime_dirty = 0;
3892 dmu_tx_commit(tx);
3893 }
3894 }
3895 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3896 }
3897
3898
3899 _Static_assert(sizeof (struct zfid_short) <= sizeof (struct fid),
3900 "struct zfid_short bigger than struct fid");
3901 _Static_assert(sizeof (struct zfid_long) <= sizeof (struct fid),
3902 "struct zfid_long bigger than struct fid");
3903
3904 static int
3905 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3906 {
3907 (void) ct;
3908 znode_t *zp = VTOZ(vp);
3909 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3910 uint32_t gen;
3911 uint64_t gen64;
3912 uint64_t object = zp->z_id;
3913 zfid_short_t *zfid;
3914 int size, i, error;
3915
3916 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3917 return (error);
3918
3919 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
3920 &gen64, sizeof (uint64_t))) != 0) {
3921 zfs_exit(zfsvfs, FTAG);
3922 return (error);
3923 }
3924
3925 gen = (uint32_t)gen64;
3926
3927 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3928 fidp->fid_len = size;
3929
3930 zfid = (zfid_short_t *)fidp;
3931
3932 zfid->zf_len = size;
3933
3934 for (i = 0; i < sizeof (zfid->zf_object); i++)
3935 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3936
3937 /* Must have a non-zero generation number to distinguish from .zfs */
3938 if (gen == 0)
3939 gen = 1;
3940 for (i = 0; i < sizeof (zfid->zf_gen); i++)
3941 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3942
3943 if (size == LONG_FID_LEN) {
3944 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
3945 zfid_long_t *zlfid;
3946
3947 zlfid = (zfid_long_t *)fidp;
3948
3949 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3950 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3951
3952 /* XXX - this should be the generation number for the objset */
3953 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3954 zlfid->zf_setgen[i] = 0;
3955 }
3956
3957 zfs_exit(zfsvfs, FTAG);
3958 return (0);
3959 }
3960
3961 static int
3962 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
3963 caller_context_t *ct)
3964 {
3965 znode_t *zp;
3966 zfsvfs_t *zfsvfs;
3967 int error;
3968
3969 switch (cmd) {
3970 case _PC_LINK_MAX:
3971 *valp = MIN(LONG_MAX, ZFS_LINK_MAX);
3972 return (0);
3973
3974 case _PC_FILESIZEBITS:
3975 *valp = 64;
3976 return (0);
3977 case _PC_MIN_HOLE_SIZE:
3978 *valp = (int)SPA_MINBLOCKSIZE;
3979 return (0);
3980 case _PC_ACL_EXTENDED:
3981 #if 0 /* POSIX ACLs are not implemented for ZFS on FreeBSD yet. */
3982 zp = VTOZ(vp);
3983 zfsvfs = zp->z_zfsvfs;
3984 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3985 return (error);
3986 *valp = zfsvfs->z_acl_type == ZFSACLTYPE_POSIX ? 1 : 0;
3987 zfs_exit(zfsvfs, FTAG);
3988 #else
3989 *valp = 0;
3990 #endif
3991 return (0);
3992
3993 case _PC_ACL_NFS4:
3994 zp = VTOZ(vp);
3995 zfsvfs = zp->z_zfsvfs;
3996 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3997 return (error);
3998 *valp = zfsvfs->z_acl_type == ZFS_ACLTYPE_NFSV4 ? 1 : 0;
3999 zfs_exit(zfsvfs, FTAG);
4000 return (0);
4001
4002 case _PC_ACL_PATH_MAX:
4003 *valp = ACL_MAX_ENTRIES;
4004 return (0);
4005
4006 default:
4007 return (EOPNOTSUPP);
4008 }
4009 }
4010
4011 static int
4012 zfs_getpages(struct vnode *vp, vm_page_t *ma, int count, int *rbehind,
4013 int *rahead)
4014 {
4015 znode_t *zp = VTOZ(vp);
4016 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4017 zfs_locked_range_t *lr;
4018 vm_object_t object;
4019 off_t start, end, obj_size;
4020 uint_t blksz;
4021 int pgsin_b, pgsin_a;
4022 int error;
4023
4024 if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0)
4025 return (zfs_vm_pagerret_error);
4026
4027 start = IDX_TO_OFF(ma[0]->pindex);
4028 end = IDX_TO_OFF(ma[count - 1]->pindex + 1);
4029
4030 /*
4031 * Lock a range covering all required and optional pages.
4032 * Note that we need to handle the case of the block size growing.
4033 */
4034 for (;;) {
4035 blksz = zp->z_blksz;
4036 lr = zfs_rangelock_tryenter(&zp->z_rangelock,
4037 rounddown(start, blksz),
4038 roundup(end, blksz) - rounddown(start, blksz), RL_READER);
4039 if (lr == NULL) {
4040 if (rahead != NULL) {
4041 *rahead = 0;
4042 rahead = NULL;
4043 }
4044 if (rbehind != NULL) {
4045 *rbehind = 0;
4046 rbehind = NULL;
4047 }
4048 break;
4049 }
4050 if (blksz == zp->z_blksz)
4051 break;
4052 zfs_rangelock_exit(lr);
4053 }
4054
4055 object = ma[0]->object;
4056 zfs_vmobject_wlock(object);
4057 obj_size = object->un_pager.vnp.vnp_size;
4058 zfs_vmobject_wunlock(object);
4059 if (IDX_TO_OFF(ma[count - 1]->pindex) >= obj_size) {
4060 if (lr != NULL)
4061 zfs_rangelock_exit(lr);
4062 zfs_exit(zfsvfs, FTAG);
4063 return (zfs_vm_pagerret_bad);
4064 }
4065
4066 pgsin_b = 0;
4067 if (rbehind != NULL) {
4068 pgsin_b = OFF_TO_IDX(start - rounddown(start, blksz));
4069 pgsin_b = MIN(*rbehind, pgsin_b);
4070 }
4071
4072 pgsin_a = 0;
4073 if (rahead != NULL) {
4074 pgsin_a = OFF_TO_IDX(roundup(end, blksz) - end);
4075 if (end + IDX_TO_OFF(pgsin_a) >= obj_size)
4076 pgsin_a = OFF_TO_IDX(round_page(obj_size) - end);
4077 pgsin_a = MIN(*rahead, pgsin_a);
4078 }
4079
4080 /*
4081 * NB: we need to pass the exact byte size of the data that we expect
4082 * to read after accounting for the file size. This is required because
4083 * ZFS will panic if we request DMU to read beyond the end of the last
4084 * allocated block.
4085 */
4086 error = dmu_read_pages(zfsvfs->z_os, zp->z_id, ma, count, &pgsin_b,
4087 &pgsin_a, MIN(end, obj_size) - (end - PAGE_SIZE));
4088
4089 if (lr != NULL)
4090 zfs_rangelock_exit(lr);
4091 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4092
4093 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, count*PAGE_SIZE);
4094
4095 zfs_exit(zfsvfs, FTAG);
4096
4097 if (error != 0)
4098 return (zfs_vm_pagerret_error);
4099
4100 VM_CNT_INC(v_vnodein);
4101 VM_CNT_ADD(v_vnodepgsin, count + pgsin_b + pgsin_a);
4102 if (rbehind != NULL)
4103 *rbehind = pgsin_b;
4104 if (rahead != NULL)
4105 *rahead = pgsin_a;
4106 return (zfs_vm_pagerret_ok);
4107 }
4108
4109 #ifndef _SYS_SYSPROTO_H_
4110 struct vop_getpages_args {
4111 struct vnode *a_vp;
4112 vm_page_t *a_m;
4113 int a_count;
4114 int *a_rbehind;
4115 int *a_rahead;
4116 };
4117 #endif
4118
4119 static int
4120 zfs_freebsd_getpages(struct vop_getpages_args *ap)
4121 {
4122
4123 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4124 ap->a_rahead));
4125 }
4126
4127 static int
4128 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4129 int *rtvals)
4130 {
4131 znode_t *zp = VTOZ(vp);
4132 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4133 zfs_locked_range_t *lr;
4134 dmu_tx_t *tx;
4135 struct sf_buf *sf;
4136 vm_object_t object;
4137 vm_page_t m;
4138 caddr_t va;
4139 size_t tocopy;
4140 size_t lo_len;
4141 vm_ooffset_t lo_off;
4142 vm_ooffset_t off;
4143 uint_t blksz;
4144 int ncount;
4145 int pcount;
4146 int err;
4147 int i;
4148
4149 object = vp->v_object;
4150 KASSERT(ma[0]->object == object, ("mismatching object"));
4151 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4152
4153 pcount = btoc(len);
4154 ncount = pcount;
4155 for (i = 0; i < pcount; i++)
4156 rtvals[i] = zfs_vm_pagerret_error;
4157
4158 if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0)
4159 return (zfs_vm_pagerret_error);
4160
4161 off = IDX_TO_OFF(ma[0]->pindex);
4162 blksz = zp->z_blksz;
4163 lo_off = rounddown(off, blksz);
4164 lo_len = roundup(len + (off - lo_off), blksz);
4165 lr = zfs_rangelock_enter(&zp->z_rangelock, lo_off, lo_len, RL_WRITER);
4166
4167 zfs_vmobject_wlock(object);
4168 if (len + off > object->un_pager.vnp.vnp_size) {
4169 if (object->un_pager.vnp.vnp_size > off) {
4170 int pgoff;
4171
4172 len = object->un_pager.vnp.vnp_size - off;
4173 ncount = btoc(len);
4174 if ((pgoff = (int)len & PAGE_MASK) != 0) {
4175 /*
4176 * If the object is locked and the following
4177 * conditions hold, then the page's dirty
4178 * field cannot be concurrently changed by a
4179 * pmap operation.
4180 */
4181 m = ma[ncount - 1];
4182 vm_page_assert_sbusied(m);
4183 KASSERT(!pmap_page_is_write_mapped(m),
4184 ("zfs_putpages: page %p is not read-only",
4185 m));
4186 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
4187 pgoff);
4188 }
4189 } else {
4190 len = 0;
4191 ncount = 0;
4192 }
4193 if (ncount < pcount) {
4194 for (i = ncount; i < pcount; i++) {
4195 rtvals[i] = zfs_vm_pagerret_bad;
4196 }
4197 }
4198 }
4199 zfs_vmobject_wunlock(object);
4200
4201 if (ncount == 0)
4202 goto out;
4203
4204 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, zp->z_uid) ||
4205 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, zp->z_gid) ||
4206 (zp->z_projid != ZFS_DEFAULT_PROJID &&
4207 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
4208 zp->z_projid))) {
4209 goto out;
4210 }
4211
4212 tx = dmu_tx_create(zfsvfs->z_os);
4213 dmu_tx_hold_write(tx, zp->z_id, off, len);
4214
4215 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4216 zfs_sa_upgrade_txholds(tx, zp);
4217 err = dmu_tx_assign(tx, TXG_WAIT);
4218 if (err != 0) {
4219 dmu_tx_abort(tx);
4220 goto out;
4221 }
4222
4223 if (zp->z_blksz < PAGE_SIZE) {
4224 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
4225 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
4226 va = zfs_map_page(ma[i], &sf);
4227 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
4228 zfs_unmap_page(sf);
4229 }
4230 } else {
4231 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
4232 }
4233
4234 if (err == 0) {
4235 uint64_t mtime[2], ctime[2];
4236 sa_bulk_attr_t bulk[3];
4237 int count = 0;
4238
4239 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4240 &mtime, 16);
4241 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4242 &ctime, 16);
4243 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4244 &zp->z_pflags, 8);
4245 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
4246 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4247 ASSERT0(err);
4248 /*
4249 * XXX we should be passing a callback to undirty
4250 * but that would make the locking messier
4251 */
4252 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off,
4253 len, 0, NULL, NULL);
4254
4255 zfs_vmobject_wlock(object);
4256 for (i = 0; i < ncount; i++) {
4257 rtvals[i] = zfs_vm_pagerret_ok;
4258 vm_page_undirty(ma[i]);
4259 }
4260 zfs_vmobject_wunlock(object);
4261 VM_CNT_INC(v_vnodeout);
4262 VM_CNT_ADD(v_vnodepgsout, ncount);
4263 }
4264 dmu_tx_commit(tx);
4265
4266 out:
4267 zfs_rangelock_exit(lr);
4268 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
4269 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4270 zil_commit(zfsvfs->z_log, zp->z_id);
4271
4272 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, len);
4273
4274 zfs_exit(zfsvfs, FTAG);
4275 return (rtvals[0]);
4276 }
4277
4278 #ifndef _SYS_SYSPROTO_H_
4279 struct vop_putpages_args {
4280 struct vnode *a_vp;
4281 vm_page_t *a_m;
4282 int a_count;
4283 int a_sync;
4284 int *a_rtvals;
4285 };
4286 #endif
4287
4288 static int
4289 zfs_freebsd_putpages(struct vop_putpages_args *ap)
4290 {
4291
4292 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
4293 ap->a_rtvals));
4294 }
4295
4296 #ifndef _SYS_SYSPROTO_H_
4297 struct vop_bmap_args {
4298 struct vnode *a_vp;
4299 daddr_t a_bn;
4300 struct bufobj **a_bop;
4301 daddr_t *a_bnp;
4302 int *a_runp;
4303 int *a_runb;
4304 };
4305 #endif
4306
4307 static int
4308 zfs_freebsd_bmap(struct vop_bmap_args *ap)
4309 {
4310
4311 if (ap->a_bop != NULL)
4312 *ap->a_bop = &ap->a_vp->v_bufobj;
4313 if (ap->a_bnp != NULL)
4314 *ap->a_bnp = ap->a_bn;
4315 if (ap->a_runp != NULL)
4316 *ap->a_runp = 0;
4317 if (ap->a_runb != NULL)
4318 *ap->a_runb = 0;
4319
4320 return (0);
4321 }
4322
4323 #ifndef _SYS_SYSPROTO_H_
4324 struct vop_open_args {
4325 struct vnode *a_vp;
4326 int a_mode;
4327 struct ucred *a_cred;
4328 struct thread *a_td;
4329 };
4330 #endif
4331
4332 static int
4333 zfs_freebsd_open(struct vop_open_args *ap)
4334 {
4335 vnode_t *vp = ap->a_vp;
4336 znode_t *zp = VTOZ(vp);
4337 int error;
4338
4339 error = zfs_open(&vp, ap->a_mode, ap->a_cred);
4340 if (error == 0)
4341 vnode_create_vobject(vp, zp->z_size, ap->a_td);
4342 return (error);
4343 }
4344
4345 #ifndef _SYS_SYSPROTO_H_
4346 struct vop_close_args {
4347 struct vnode *a_vp;
4348 int a_fflag;
4349 struct ucred *a_cred;
4350 struct thread *a_td;
4351 };
4352 #endif
4353
4354 static int
4355 zfs_freebsd_close(struct vop_close_args *ap)
4356 {
4357
4358 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred));
4359 }
4360
4361 #ifndef _SYS_SYSPROTO_H_
4362 struct vop_ioctl_args {
4363 struct vnode *a_vp;
4364 ulong_t a_command;
4365 caddr_t a_data;
4366 int a_fflag;
4367 struct ucred *cred;
4368 struct thread *td;
4369 };
4370 #endif
4371
4372 static int
4373 zfs_freebsd_ioctl(struct vop_ioctl_args *ap)
4374 {
4375
4376 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4377 ap->a_fflag, ap->a_cred, NULL));
4378 }
4379
4380 static int
4381 ioflags(int ioflags)
4382 {
4383 int flags = 0;
4384
4385 if (ioflags & IO_APPEND)
4386 flags |= O_APPEND;
4387 if (ioflags & IO_NDELAY)
4388 flags |= O_NONBLOCK;
4389 if (ioflags & IO_SYNC)
4390 flags |= O_SYNC;
4391
4392 return (flags);
4393 }
4394
4395 #ifndef _SYS_SYSPROTO_H_
4396 struct vop_read_args {
4397 struct vnode *a_vp;
4398 struct uio *a_uio;
4399 int a_ioflag;
4400 struct ucred *a_cred;
4401 };
4402 #endif
4403
4404 static int
4405 zfs_freebsd_read(struct vop_read_args *ap)
4406 {
4407 zfs_uio_t uio;
4408 zfs_uio_init(&uio, ap->a_uio);
4409 return (zfs_read(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4410 ap->a_cred));
4411 }
4412
4413 #ifndef _SYS_SYSPROTO_H_
4414 struct vop_write_args {
4415 struct vnode *a_vp;
4416 struct uio *a_uio;
4417 int a_ioflag;
4418 struct ucred *a_cred;
4419 };
4420 #endif
4421
4422 static int
4423 zfs_freebsd_write(struct vop_write_args *ap)
4424 {
4425 zfs_uio_t uio;
4426 zfs_uio_init(&uio, ap->a_uio);
4427 return (zfs_write(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4428 ap->a_cred));
4429 }
4430
4431 #if __FreeBSD_version >= 1300102
4432 /*
4433 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
4434 * the comment above cache_fplookup for details.
4435 */
4436 static int
4437 zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args *v)
4438 {
4439 vnode_t *vp;
4440 znode_t *zp;
4441 uint64_t pflags;
4442
4443 vp = v->a_vp;
4444 zp = VTOZ_SMR(vp);
4445 if (__predict_false(zp == NULL))
4446 return (EAGAIN);
4447 pflags = atomic_load_64(&zp->z_pflags);
4448 if (pflags & ZFS_AV_QUARANTINED)
4449 return (EAGAIN);
4450 if (pflags & ZFS_XATTR)
4451 return (EAGAIN);
4452 if ((pflags & ZFS_NO_EXECS_DENIED) == 0)
4453 return (EAGAIN);
4454 return (0);
4455 }
4456 #endif
4457
4458 #if __FreeBSD_version >= 1300139
4459 static int
4460 zfs_freebsd_fplookup_symlink(struct vop_fplookup_symlink_args *v)
4461 {
4462 vnode_t *vp;
4463 znode_t *zp;
4464 char *target;
4465
4466 vp = v->a_vp;
4467 zp = VTOZ_SMR(vp);
4468 if (__predict_false(zp == NULL)) {
4469 return (EAGAIN);
4470 }
4471
4472 target = atomic_load_consume_ptr(&zp->z_cached_symlink);
4473 if (target == NULL) {
4474 return (EAGAIN);
4475 }
4476 return (cache_symlink_resolve(v->a_fpl, target, strlen(target)));
4477 }
4478 #endif
4479
4480 #ifndef _SYS_SYSPROTO_H_
4481 struct vop_access_args {
4482 struct vnode *a_vp;
4483 accmode_t a_accmode;
4484 struct ucred *a_cred;
4485 struct thread *a_td;
4486 };
4487 #endif
4488
4489 static int
4490 zfs_freebsd_access(struct vop_access_args *ap)
4491 {
4492 vnode_t *vp = ap->a_vp;
4493 znode_t *zp = VTOZ(vp);
4494 accmode_t accmode;
4495 int error = 0;
4496
4497
4498 if (ap->a_accmode == VEXEC) {
4499 if (zfs_fastaccesschk_execute(zp, ap->a_cred) == 0)
4500 return (0);
4501 }
4502
4503 /*
4504 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4505 */
4506 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4507 if (accmode != 0)
4508 error = zfs_access(zp, accmode, 0, ap->a_cred);
4509
4510 /*
4511 * VADMIN has to be handled by vaccess().
4512 */
4513 if (error == 0) {
4514 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4515 if (accmode != 0) {
4516 #if __FreeBSD_version >= 1300105
4517 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4518 zp->z_gid, accmode, ap->a_cred);
4519 #else
4520 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4521 zp->z_gid, accmode, ap->a_cred, NULL);
4522 #endif
4523 }
4524 }
4525
4526 /*
4527 * For VEXEC, ensure that at least one execute bit is set for
4528 * non-directories.
4529 */
4530 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
4531 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
4532 error = EACCES;
4533 }
4534
4535 return (error);
4536 }
4537
4538 #ifndef _SYS_SYSPROTO_H_
4539 struct vop_lookup_args {
4540 struct vnode *a_dvp;
4541 struct vnode **a_vpp;
4542 struct componentname *a_cnp;
4543 };
4544 #endif
4545
4546 static int
4547 zfs_freebsd_lookup(struct vop_lookup_args *ap, boolean_t cached)
4548 {
4549 struct componentname *cnp = ap->a_cnp;
4550 char nm[NAME_MAX + 1];
4551
4552 ASSERT3U(cnp->cn_namelen, <, sizeof (nm));
4553 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof (nm)));
4554
4555 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4556 cnp->cn_cred, 0, cached));
4557 }
4558
4559 static int
4560 zfs_freebsd_cachedlookup(struct vop_cachedlookup_args *ap)
4561 {
4562
4563 return (zfs_freebsd_lookup((struct vop_lookup_args *)ap, B_TRUE));
4564 }
4565
4566 #ifndef _SYS_SYSPROTO_H_
4567 struct vop_lookup_args {
4568 struct vnode *a_dvp;
4569 struct vnode **a_vpp;
4570 struct componentname *a_cnp;
4571 };
4572 #endif
4573
4574 static int
4575 zfs_cache_lookup(struct vop_lookup_args *ap)
4576 {
4577 zfsvfs_t *zfsvfs;
4578
4579 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4580 if (zfsvfs->z_use_namecache)
4581 return (vfs_cache_lookup(ap));
4582 else
4583 return (zfs_freebsd_lookup(ap, B_FALSE));
4584 }
4585
4586 #ifndef _SYS_SYSPROTO_H_
4587 struct vop_create_args {
4588 struct vnode *a_dvp;
4589 struct vnode **a_vpp;
4590 struct componentname *a_cnp;
4591 struct vattr *a_vap;
4592 };
4593 #endif
4594
4595 static int
4596 zfs_freebsd_create(struct vop_create_args *ap)
4597 {
4598 zfsvfs_t *zfsvfs;
4599 struct componentname *cnp = ap->a_cnp;
4600 vattr_t *vap = ap->a_vap;
4601 znode_t *zp = NULL;
4602 int rc, mode;
4603
4604 #if __FreeBSD_version < 1400068
4605 ASSERT(cnp->cn_flags & SAVENAME);
4606 #endif
4607
4608 vattr_init_mask(vap);
4609 mode = vap->va_mode & ALLPERMS;
4610 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4611 *ap->a_vpp = NULL;
4612
4613 rc = zfs_create(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap, 0, mode,
4614 &zp, cnp->cn_cred, 0 /* flag */, NULL /* vsecattr */, NULL);
4615 if (rc == 0)
4616 *ap->a_vpp = ZTOV(zp);
4617 if (zfsvfs->z_use_namecache &&
4618 rc == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
4619 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
4620
4621 return (rc);
4622 }
4623
4624 #ifndef _SYS_SYSPROTO_H_
4625 struct vop_remove_args {
4626 struct vnode *a_dvp;
4627 struct vnode *a_vp;
4628 struct componentname *a_cnp;
4629 };
4630 #endif
4631
4632 static int
4633 zfs_freebsd_remove(struct vop_remove_args *ap)
4634 {
4635
4636 #if __FreeBSD_version < 1400068
4637 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4638 #endif
4639
4640 return (zfs_remove_(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
4641 ap->a_cnp->cn_cred));
4642 }
4643
4644 #ifndef _SYS_SYSPROTO_H_
4645 struct vop_mkdir_args {
4646 struct vnode *a_dvp;
4647 struct vnode **a_vpp;
4648 struct componentname *a_cnp;
4649 struct vattr *a_vap;
4650 };
4651 #endif
4652
4653 static int
4654 zfs_freebsd_mkdir(struct vop_mkdir_args *ap)
4655 {
4656 vattr_t *vap = ap->a_vap;
4657 znode_t *zp = NULL;
4658 int rc;
4659
4660 #if __FreeBSD_version < 1400068
4661 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4662 #endif
4663
4664 vattr_init_mask(vap);
4665 *ap->a_vpp = NULL;
4666
4667 rc = zfs_mkdir(VTOZ(ap->a_dvp), ap->a_cnp->cn_nameptr, vap, &zp,
4668 ap->a_cnp->cn_cred, 0, NULL, NULL);
4669
4670 if (rc == 0)
4671 *ap->a_vpp = ZTOV(zp);
4672 return (rc);
4673 }
4674
4675 #ifndef _SYS_SYSPROTO_H_
4676 struct vop_rmdir_args {
4677 struct vnode *a_dvp;
4678 struct vnode *a_vp;
4679 struct componentname *a_cnp;
4680 };
4681 #endif
4682
4683 static int
4684 zfs_freebsd_rmdir(struct vop_rmdir_args *ap)
4685 {
4686 struct componentname *cnp = ap->a_cnp;
4687
4688 #if __FreeBSD_version < 1400068
4689 ASSERT(cnp->cn_flags & SAVENAME);
4690 #endif
4691
4692 return (zfs_rmdir_(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
4693 }
4694
4695 #ifndef _SYS_SYSPROTO_H_
4696 struct vop_readdir_args {
4697 struct vnode *a_vp;
4698 struct uio *a_uio;
4699 struct ucred *a_cred;
4700 int *a_eofflag;
4701 int *a_ncookies;
4702 cookie_t **a_cookies;
4703 };
4704 #endif
4705
4706 static int
4707 zfs_freebsd_readdir(struct vop_readdir_args *ap)
4708 {
4709 zfs_uio_t uio;
4710 zfs_uio_init(&uio, ap->a_uio);
4711 return (zfs_readdir(ap->a_vp, &uio, ap->a_cred, ap->a_eofflag,
4712 ap->a_ncookies, ap->a_cookies));
4713 }
4714
4715 #ifndef _SYS_SYSPROTO_H_
4716 struct vop_fsync_args {
4717 struct vnode *a_vp;
4718 int a_waitfor;
4719 struct thread *a_td;
4720 };
4721 #endif
4722
4723 static int
4724 zfs_freebsd_fsync(struct vop_fsync_args *ap)
4725 {
4726
4727 return (zfs_fsync(VTOZ(ap->a_vp), 0, ap->a_td->td_ucred));
4728 }
4729
4730 #ifndef _SYS_SYSPROTO_H_
4731 struct vop_getattr_args {
4732 struct vnode *a_vp;
4733 struct vattr *a_vap;
4734 struct ucred *a_cred;
4735 };
4736 #endif
4737
4738 static int
4739 zfs_freebsd_getattr(struct vop_getattr_args *ap)
4740 {
4741 vattr_t *vap = ap->a_vap;
4742 xvattr_t xvap;
4743 ulong_t fflags = 0;
4744 int error;
4745
4746 xva_init(&xvap);
4747 xvap.xva_vattr = *vap;
4748 xvap.xva_vattr.va_mask |= AT_XVATTR;
4749
4750 /* Convert chflags into ZFS-type flags. */
4751 /* XXX: what about SF_SETTABLE?. */
4752 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
4753 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
4754 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
4755 XVA_SET_REQ(&xvap, XAT_NODUMP);
4756 XVA_SET_REQ(&xvap, XAT_READONLY);
4757 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
4758 XVA_SET_REQ(&xvap, XAT_SYSTEM);
4759 XVA_SET_REQ(&xvap, XAT_HIDDEN);
4760 XVA_SET_REQ(&xvap, XAT_REPARSE);
4761 XVA_SET_REQ(&xvap, XAT_OFFLINE);
4762 XVA_SET_REQ(&xvap, XAT_SPARSE);
4763
4764 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred);
4765 if (error != 0)
4766 return (error);
4767
4768 /* Convert ZFS xattr into chflags. */
4769 #define FLAG_CHECK(fflag, xflag, xfield) do { \
4770 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
4771 fflags |= (fflag); \
4772 } while (0)
4773 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
4774 xvap.xva_xoptattrs.xoa_immutable);
4775 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
4776 xvap.xva_xoptattrs.xoa_appendonly);
4777 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
4778 xvap.xva_xoptattrs.xoa_nounlink);
4779 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
4780 xvap.xva_xoptattrs.xoa_archive);
4781 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
4782 xvap.xva_xoptattrs.xoa_nodump);
4783 FLAG_CHECK(UF_READONLY, XAT_READONLY,
4784 xvap.xva_xoptattrs.xoa_readonly);
4785 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
4786 xvap.xva_xoptattrs.xoa_system);
4787 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
4788 xvap.xva_xoptattrs.xoa_hidden);
4789 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
4790 xvap.xva_xoptattrs.xoa_reparse);
4791 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
4792 xvap.xva_xoptattrs.xoa_offline);
4793 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
4794 xvap.xva_xoptattrs.xoa_sparse);
4795
4796 #undef FLAG_CHECK
4797 *vap = xvap.xva_vattr;
4798 vap->va_flags = fflags;
4799 return (0);
4800 }
4801
4802 #ifndef _SYS_SYSPROTO_H_
4803 struct vop_setattr_args {
4804 struct vnode *a_vp;
4805 struct vattr *a_vap;
4806 struct ucred *a_cred;
4807 };
4808 #endif
4809
4810 static int
4811 zfs_freebsd_setattr(struct vop_setattr_args *ap)
4812 {
4813 vnode_t *vp = ap->a_vp;
4814 vattr_t *vap = ap->a_vap;
4815 cred_t *cred = ap->a_cred;
4816 xvattr_t xvap;
4817 ulong_t fflags;
4818 uint64_t zflags;
4819
4820 vattr_init_mask(vap);
4821 vap->va_mask &= ~AT_NOSET;
4822
4823 xva_init(&xvap);
4824 xvap.xva_vattr = *vap;
4825
4826 zflags = VTOZ(vp)->z_pflags;
4827
4828 if (vap->va_flags != VNOVAL) {
4829 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
4830 int error;
4831
4832 if (zfsvfs->z_use_fuids == B_FALSE)
4833 return (EOPNOTSUPP);
4834
4835 fflags = vap->va_flags;
4836 /*
4837 * XXX KDM
4838 * We need to figure out whether it makes sense to allow
4839 * UF_REPARSE through, since we don't really have other
4840 * facilities to handle reparse points and zfs_setattr()
4841 * doesn't currently allow setting that attribute anyway.
4842 */
4843 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
4844 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
4845 UF_OFFLINE|UF_SPARSE)) != 0)
4846 return (EOPNOTSUPP);
4847 /*
4848 * Unprivileged processes are not permitted to unset system
4849 * flags, or modify flags if any system flags are set.
4850 * Privileged non-jail processes may not modify system flags
4851 * if securelevel > 0 and any existing system flags are set.
4852 * Privileged jail processes behave like privileged non-jail
4853 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
4854 * otherwise, they behave like unprivileged processes.
4855 */
4856 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
4857 spl_priv_check_cred(cred, PRIV_VFS_SYSFLAGS) == 0) {
4858 if (zflags &
4859 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4860 error = securelevel_gt(cred, 0);
4861 if (error != 0)
4862 return (error);
4863 }
4864 } else {
4865 /*
4866 * Callers may only modify the file flags on
4867 * objects they have VADMIN rights for.
4868 */
4869 if ((error = VOP_ACCESS(vp, VADMIN, cred,
4870 curthread)) != 0)
4871 return (error);
4872 if (zflags &
4873 (ZFS_IMMUTABLE | ZFS_APPENDONLY |
4874 ZFS_NOUNLINK)) {
4875 return (EPERM);
4876 }
4877 if (fflags &
4878 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
4879 return (EPERM);
4880 }
4881 }
4882
4883 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
4884 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
4885 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
4886 XVA_SET_REQ(&xvap, (xflag)); \
4887 (xfield) = ((fflags & (fflag)) != 0); \
4888 } \
4889 } while (0)
4890 /* Convert chflags into ZFS-type flags. */
4891 /* XXX: what about SF_SETTABLE?. */
4892 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
4893 xvap.xva_xoptattrs.xoa_immutable);
4894 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
4895 xvap.xva_xoptattrs.xoa_appendonly);
4896 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
4897 xvap.xva_xoptattrs.xoa_nounlink);
4898 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
4899 xvap.xva_xoptattrs.xoa_archive);
4900 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
4901 xvap.xva_xoptattrs.xoa_nodump);
4902 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
4903 xvap.xva_xoptattrs.xoa_readonly);
4904 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
4905 xvap.xva_xoptattrs.xoa_system);
4906 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
4907 xvap.xva_xoptattrs.xoa_hidden);
4908 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
4909 xvap.xva_xoptattrs.xoa_reparse);
4910 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
4911 xvap.xva_xoptattrs.xoa_offline);
4912 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
4913 xvap.xva_xoptattrs.xoa_sparse);
4914 #undef FLAG_CHANGE
4915 }
4916 if (vap->va_birthtime.tv_sec != VNOVAL) {
4917 xvap.xva_vattr.va_mask |= AT_XVATTR;
4918 XVA_SET_REQ(&xvap, XAT_CREATETIME);
4919 }
4920 return (zfs_setattr(VTOZ(vp), (vattr_t *)&xvap, 0, cred, NULL));
4921 }
4922
4923 #ifndef _SYS_SYSPROTO_H_
4924 struct vop_rename_args {
4925 struct vnode *a_fdvp;
4926 struct vnode *a_fvp;
4927 struct componentname *a_fcnp;
4928 struct vnode *a_tdvp;
4929 struct vnode *a_tvp;
4930 struct componentname *a_tcnp;
4931 };
4932 #endif
4933
4934 static int
4935 zfs_freebsd_rename(struct vop_rename_args *ap)
4936 {
4937 vnode_t *fdvp = ap->a_fdvp;
4938 vnode_t *fvp = ap->a_fvp;
4939 vnode_t *tdvp = ap->a_tdvp;
4940 vnode_t *tvp = ap->a_tvp;
4941 int error;
4942
4943 #if __FreeBSD_version < 1400068
4944 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
4945 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
4946 #endif
4947
4948 error = zfs_do_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
4949 ap->a_tcnp, ap->a_fcnp->cn_cred);
4950
4951 vrele(fdvp);
4952 vrele(fvp);
4953 vrele(tdvp);
4954 if (tvp != NULL)
4955 vrele(tvp);
4956
4957 return (error);
4958 }
4959
4960 #ifndef _SYS_SYSPROTO_H_
4961 struct vop_symlink_args {
4962 struct vnode *a_dvp;
4963 struct vnode **a_vpp;
4964 struct componentname *a_cnp;
4965 struct vattr *a_vap;
4966 char *a_target;
4967 };
4968 #endif
4969
4970 static int
4971 zfs_freebsd_symlink(struct vop_symlink_args *ap)
4972 {
4973 struct componentname *cnp = ap->a_cnp;
4974 vattr_t *vap = ap->a_vap;
4975 znode_t *zp = NULL;
4976 #if __FreeBSD_version >= 1300139
4977 char *symlink;
4978 size_t symlink_len;
4979 #endif
4980 int rc;
4981
4982 #if __FreeBSD_version < 1400068
4983 ASSERT(cnp->cn_flags & SAVENAME);
4984 #endif
4985
4986 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
4987 vattr_init_mask(vap);
4988 *ap->a_vpp = NULL;
4989
4990 rc = zfs_symlink(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap,
4991 ap->a_target, &zp, cnp->cn_cred, 0 /* flags */, NULL);
4992 if (rc == 0) {
4993 *ap->a_vpp = ZTOV(zp);
4994 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
4995 #if __FreeBSD_version >= 1300139
4996 MPASS(zp->z_cached_symlink == NULL);
4997 symlink_len = strlen(ap->a_target);
4998 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
4999 if (symlink != NULL) {
5000 memcpy(symlink, ap->a_target, symlink_len);
5001 symlink[symlink_len] = '\0';
5002 atomic_store_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5003 (uintptr_t)symlink);
5004 }
5005 #endif
5006 }
5007 return (rc);
5008 }
5009
5010 #ifndef _SYS_SYSPROTO_H_
5011 struct vop_readlink_args {
5012 struct vnode *a_vp;
5013 struct uio *a_uio;
5014 struct ucred *a_cred;
5015 };
5016 #endif
5017
5018 static int
5019 zfs_freebsd_readlink(struct vop_readlink_args *ap)
5020 {
5021 zfs_uio_t uio;
5022 int error;
5023 #if __FreeBSD_version >= 1300139
5024 znode_t *zp = VTOZ(ap->a_vp);
5025 char *symlink, *base;
5026 size_t symlink_len;
5027 bool trycache;
5028 #endif
5029
5030 zfs_uio_init(&uio, ap->a_uio);
5031 #if __FreeBSD_version >= 1300139
5032 trycache = false;
5033 if (zfs_uio_segflg(&uio) == UIO_SYSSPACE &&
5034 zfs_uio_iovcnt(&uio) == 1) {
5035 base = zfs_uio_iovbase(&uio, 0);
5036 symlink_len = zfs_uio_iovlen(&uio, 0);
5037 trycache = true;
5038 }
5039 #endif
5040 error = zfs_readlink(ap->a_vp, &uio, ap->a_cred, NULL);
5041 #if __FreeBSD_version >= 1300139
5042 if (atomic_load_ptr(&zp->z_cached_symlink) != NULL ||
5043 error != 0 || !trycache) {
5044 return (error);
5045 }
5046 symlink_len -= zfs_uio_resid(&uio);
5047 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
5048 if (symlink != NULL) {
5049 memcpy(symlink, base, symlink_len);
5050 symlink[symlink_len] = '\0';
5051 if (!atomic_cmpset_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5052 (uintptr_t)NULL, (uintptr_t)symlink)) {
5053 cache_symlink_free(symlink, symlink_len + 1);
5054 }
5055 }
5056 #endif
5057 return (error);
5058 }
5059
5060 #ifndef _SYS_SYSPROTO_H_
5061 struct vop_link_args {
5062 struct vnode *a_tdvp;
5063 struct vnode *a_vp;
5064 struct componentname *a_cnp;
5065 };
5066 #endif
5067
5068 static int
5069 zfs_freebsd_link(struct vop_link_args *ap)
5070 {
5071 struct componentname *cnp = ap->a_cnp;
5072 vnode_t *vp = ap->a_vp;
5073 vnode_t *tdvp = ap->a_tdvp;
5074
5075 if (tdvp->v_mount != vp->v_mount)
5076 return (EXDEV);
5077
5078 #if __FreeBSD_version < 1400068
5079 ASSERT(cnp->cn_flags & SAVENAME);
5080 #endif
5081
5082 return (zfs_link(VTOZ(tdvp), VTOZ(vp),
5083 cnp->cn_nameptr, cnp->cn_cred, 0));
5084 }
5085
5086 #ifndef _SYS_SYSPROTO_H_
5087 struct vop_inactive_args {
5088 struct vnode *a_vp;
5089 struct thread *a_td;
5090 };
5091 #endif
5092
5093 static int
5094 zfs_freebsd_inactive(struct vop_inactive_args *ap)
5095 {
5096 vnode_t *vp = ap->a_vp;
5097
5098 #if __FreeBSD_version >= 1300123
5099 zfs_inactive(vp, curthread->td_ucred, NULL);
5100 #else
5101 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5102 #endif
5103 return (0);
5104 }
5105
5106 #if __FreeBSD_version >= 1300042
5107 #ifndef _SYS_SYSPROTO_H_
5108 struct vop_need_inactive_args {
5109 struct vnode *a_vp;
5110 struct thread *a_td;
5111 };
5112 #endif
5113
5114 static int
5115 zfs_freebsd_need_inactive(struct vop_need_inactive_args *ap)
5116 {
5117 vnode_t *vp = ap->a_vp;
5118 znode_t *zp = VTOZ(vp);
5119 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5120 int need;
5121
5122 if (vn_need_pageq_flush(vp))
5123 return (1);
5124
5125 if (!ZFS_TEARDOWN_INACTIVE_TRY_ENTER_READ(zfsvfs))
5126 return (1);
5127 need = (zp->z_sa_hdl == NULL || zp->z_unlinked || zp->z_atime_dirty);
5128 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5129
5130 return (need);
5131 }
5132 #endif
5133
5134 #ifndef _SYS_SYSPROTO_H_
5135 struct vop_reclaim_args {
5136 struct vnode *a_vp;
5137 struct thread *a_td;
5138 };
5139 #endif
5140
5141 static int
5142 zfs_freebsd_reclaim(struct vop_reclaim_args *ap)
5143 {
5144 vnode_t *vp = ap->a_vp;
5145 znode_t *zp = VTOZ(vp);
5146 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5147
5148 ASSERT3P(zp, !=, NULL);
5149
5150 #if __FreeBSD_version < 1300042
5151 /* Destroy the vm object and flush associated pages. */
5152 vnode_destroy_vobject(vp);
5153 #endif
5154 /*
5155 * z_teardown_inactive_lock protects from a race with
5156 * zfs_znode_dmu_fini in zfsvfs_teardown during
5157 * force unmount.
5158 */
5159 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
5160 if (zp->z_sa_hdl == NULL)
5161 zfs_znode_free(zp);
5162 else
5163 zfs_zinactive(zp);
5164 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5165
5166 vp->v_data = NULL;
5167 return (0);
5168 }
5169
5170 #ifndef _SYS_SYSPROTO_H_
5171 struct vop_fid_args {
5172 struct vnode *a_vp;
5173 struct fid *a_fid;
5174 };
5175 #endif
5176
5177 static int
5178 zfs_freebsd_fid(struct vop_fid_args *ap)
5179 {
5180
5181 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5182 }
5183
5184
5185 #ifndef _SYS_SYSPROTO_H_
5186 struct vop_pathconf_args {
5187 struct vnode *a_vp;
5188 int a_name;
5189 register_t *a_retval;
5190 } *ap;
5191 #endif
5192
5193 static int
5194 zfs_freebsd_pathconf(struct vop_pathconf_args *ap)
5195 {
5196 ulong_t val;
5197 int error;
5198
5199 error = zfs_pathconf(ap->a_vp, ap->a_name, &val,
5200 curthread->td_ucred, NULL);
5201 if (error == 0) {
5202 *ap->a_retval = val;
5203 return (error);
5204 }
5205 if (error != EOPNOTSUPP)
5206 return (error);
5207
5208 switch (ap->a_name) {
5209 case _PC_NAME_MAX:
5210 *ap->a_retval = NAME_MAX;
5211 return (0);
5212 #if __FreeBSD_version >= 1400032
5213 case _PC_DEALLOC_PRESENT:
5214 *ap->a_retval = 1;
5215 return (0);
5216 #endif
5217 case _PC_PIPE_BUF:
5218 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO) {
5219 *ap->a_retval = PIPE_BUF;
5220 return (0);
5221 }
5222 return (EINVAL);
5223 default:
5224 return (vop_stdpathconf(ap));
5225 }
5226 }
5227
5228 static int zfs_xattr_compat = 1;
5229
5230 static int
5231 zfs_check_attrname(const char *name)
5232 {
5233 /* We don't allow '/' character in attribute name. */
5234 if (strchr(name, '/') != NULL)
5235 return (SET_ERROR(EINVAL));
5236 /* We don't allow attribute names that start with a namespace prefix. */
5237 if (ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5238 return (SET_ERROR(EINVAL));
5239 return (0);
5240 }
5241
5242 /*
5243 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5244 * extended attribute name:
5245 *
5246 * NAMESPACE XATTR_COMPAT PREFIX
5247 * system * freebsd:system:
5248 * user 1 (none, can be used to access ZFS
5249 * fsattr(5) attributes created on Solaris)
5250 * user 0 user.
5251 */
5252 static int
5253 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5254 size_t size, boolean_t compat)
5255 {
5256 const char *namespace, *prefix, *suffix;
5257
5258 memset(attrname, 0, size);
5259
5260 switch (attrnamespace) {
5261 case EXTATTR_NAMESPACE_USER:
5262 if (compat) {
5263 /*
5264 * This is the default namespace by which we can access
5265 * all attributes created on Solaris.
5266 */
5267 prefix = namespace = suffix = "";
5268 } else {
5269 /*
5270 * This is compatible with the user namespace encoding
5271 * on Linux prior to xattr_compat, but nothing
5272 * else.
5273 */
5274 prefix = "";
5275 namespace = "user";
5276 suffix = ".";
5277 }
5278 break;
5279 case EXTATTR_NAMESPACE_SYSTEM:
5280 prefix = "freebsd:";
5281 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5282 suffix = ":";
5283 break;
5284 case EXTATTR_NAMESPACE_EMPTY:
5285 default:
5286 return (SET_ERROR(EINVAL));
5287 }
5288 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5289 name) >= size) {
5290 return (SET_ERROR(ENAMETOOLONG));
5291 }
5292 return (0);
5293 }
5294
5295 static int
5296 zfs_ensure_xattr_cached(znode_t *zp)
5297 {
5298 int error = 0;
5299
5300 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5301
5302 if (zp->z_xattr_cached != NULL)
5303 return (0);
5304
5305 if (rw_write_held(&zp->z_xattr_lock))
5306 return (zfs_sa_get_xattr(zp));
5307
5308 if (!rw_tryupgrade(&zp->z_xattr_lock)) {
5309 rw_exit(&zp->z_xattr_lock);
5310 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5311 }
5312 if (zp->z_xattr_cached == NULL)
5313 error = zfs_sa_get_xattr(zp);
5314 rw_downgrade(&zp->z_xattr_lock);
5315 return (error);
5316 }
5317
5318 #ifndef _SYS_SYSPROTO_H_
5319 struct vop_getextattr {
5320 IN struct vnode *a_vp;
5321 IN int a_attrnamespace;
5322 IN const char *a_name;
5323 INOUT struct uio *a_uio;
5324 OUT size_t *a_size;
5325 IN struct ucred *a_cred;
5326 IN struct thread *a_td;
5327 };
5328 #endif
5329
5330 static int
5331 zfs_getextattr_dir(struct vop_getextattr_args *ap, const char *attrname)
5332 {
5333 struct thread *td = ap->a_td;
5334 struct nameidata nd;
5335 struct vattr va;
5336 vnode_t *xvp = NULL, *vp;
5337 int error, flags;
5338
5339 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5340 LOOKUP_XATTR, B_FALSE);
5341 if (error != 0)
5342 return (error);
5343
5344 flags = FREAD;
5345 #if __FreeBSD_version < 1400043
5346 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5347 xvp, td);
5348 #else
5349 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5350 #endif
5351 error = vn_open_cred(&nd, &flags, 0, VN_OPEN_INVFS, ap->a_cred, NULL);
5352 if (error != 0)
5353 return (SET_ERROR(error));
5354 vp = nd.ni_vp;
5355 NDFREE_PNBUF(&nd);
5356
5357 if (ap->a_size != NULL) {
5358 error = VOP_GETATTR(vp, &va, ap->a_cred);
5359 if (error == 0)
5360 *ap->a_size = (size_t)va.va_size;
5361 } else if (ap->a_uio != NULL)
5362 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5363
5364 VOP_UNLOCK1(vp);
5365 vn_close(vp, flags, ap->a_cred, td);
5366 return (error);
5367 }
5368
5369 static int
5370 zfs_getextattr_sa(struct vop_getextattr_args *ap, const char *attrname)
5371 {
5372 znode_t *zp = VTOZ(ap->a_vp);
5373 uchar_t *nv_value;
5374 uint_t nv_size;
5375 int error;
5376
5377 error = zfs_ensure_xattr_cached(zp);
5378 if (error != 0)
5379 return (error);
5380
5381 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5382 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5383
5384 error = nvlist_lookup_byte_array(zp->z_xattr_cached, attrname,
5385 &nv_value, &nv_size);
5386 if (error != 0)
5387 return (SET_ERROR(error));
5388
5389 if (ap->a_size != NULL)
5390 *ap->a_size = nv_size;
5391 else if (ap->a_uio != NULL)
5392 error = uiomove(nv_value, nv_size, ap->a_uio);
5393 if (error != 0)
5394 return (SET_ERROR(error));
5395
5396 return (0);
5397 }
5398
5399 static int
5400 zfs_getextattr_impl(struct vop_getextattr_args *ap, boolean_t compat)
5401 {
5402 znode_t *zp = VTOZ(ap->a_vp);
5403 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5404 char attrname[EXTATTR_MAXNAMELEN+1];
5405 int error;
5406
5407 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5408 sizeof (attrname), compat);
5409 if (error != 0)
5410 return (error);
5411
5412 error = ENOENT;
5413 if (zfsvfs->z_use_sa && zp->z_is_sa)
5414 error = zfs_getextattr_sa(ap, attrname);
5415 if (error == ENOENT)
5416 error = zfs_getextattr_dir(ap, attrname);
5417 return (error);
5418 }
5419
5420 /*
5421 * Vnode operation to retrieve a named extended attribute.
5422 */
5423 static int
5424 zfs_getextattr(struct vop_getextattr_args *ap)
5425 {
5426 znode_t *zp = VTOZ(ap->a_vp);
5427 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5428 int error;
5429
5430 /*
5431 * If the xattr property is off, refuse the request.
5432 */
5433 if (!(zfsvfs->z_flags & ZSB_XATTR))
5434 return (SET_ERROR(EOPNOTSUPP));
5435
5436 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5437 ap->a_cred, ap->a_td, VREAD);
5438 if (error != 0)
5439 return (SET_ERROR(error));
5440
5441 error = zfs_check_attrname(ap->a_name);
5442 if (error != 0)
5443 return (error);
5444
5445 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5446 return (error);
5447 error = ENOENT;
5448 rw_enter(&zp->z_xattr_lock, RW_READER);
5449
5450 error = zfs_getextattr_impl(ap, zfs_xattr_compat);
5451 if ((error == ENOENT || error == ENOATTR) &&
5452 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5453 /*
5454 * Fall back to the alternate namespace format if we failed to
5455 * find a user xattr.
5456 */
5457 error = zfs_getextattr_impl(ap, !zfs_xattr_compat);
5458 }
5459
5460 rw_exit(&zp->z_xattr_lock);
5461 zfs_exit(zfsvfs, FTAG);
5462 if (error == ENOENT)
5463 error = SET_ERROR(ENOATTR);
5464 return (error);
5465 }
5466
5467 #ifndef _SYS_SYSPROTO_H_
5468 struct vop_deleteextattr {
5469 IN struct vnode *a_vp;
5470 IN int a_attrnamespace;
5471 IN const char *a_name;
5472 IN struct ucred *a_cred;
5473 IN struct thread *a_td;
5474 };
5475 #endif
5476
5477 static int
5478 zfs_deleteextattr_dir(struct vop_deleteextattr_args *ap, const char *attrname)
5479 {
5480 struct nameidata nd;
5481 vnode_t *xvp = NULL, *vp;
5482 int error;
5483
5484 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5485 LOOKUP_XATTR, B_FALSE);
5486 if (error != 0)
5487 return (error);
5488
5489 #if __FreeBSD_version < 1400043
5490 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5491 UIO_SYSSPACE, attrname, xvp, ap->a_td);
5492 #else
5493 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5494 UIO_SYSSPACE, attrname, xvp);
5495 #endif
5496 error = namei(&nd);
5497 if (error != 0)
5498 return (SET_ERROR(error));
5499
5500 vp = nd.ni_vp;
5501 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
5502 NDFREE_PNBUF(&nd);
5503
5504 vput(nd.ni_dvp);
5505 if (vp == nd.ni_dvp)
5506 vrele(vp);
5507 else
5508 vput(vp);
5509
5510 return (error);
5511 }
5512
5513 static int
5514 zfs_deleteextattr_sa(struct vop_deleteextattr_args *ap, const char *attrname)
5515 {
5516 znode_t *zp = VTOZ(ap->a_vp);
5517 nvlist_t *nvl;
5518 int error;
5519
5520 error = zfs_ensure_xattr_cached(zp);
5521 if (error != 0)
5522 return (error);
5523
5524 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5525 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5526
5527 nvl = zp->z_xattr_cached;
5528 error = nvlist_remove(nvl, attrname, DATA_TYPE_BYTE_ARRAY);
5529 if (error != 0)
5530 error = SET_ERROR(error);
5531 else
5532 error = zfs_sa_set_xattr(zp, attrname, NULL, 0);
5533 if (error != 0) {
5534 zp->z_xattr_cached = NULL;
5535 nvlist_free(nvl);
5536 }
5537 return (error);
5538 }
5539
5540 static int
5541 zfs_deleteextattr_impl(struct vop_deleteextattr_args *ap, boolean_t compat)
5542 {
5543 znode_t *zp = VTOZ(ap->a_vp);
5544 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5545 char attrname[EXTATTR_MAXNAMELEN+1];
5546 int error;
5547
5548 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5549 sizeof (attrname), compat);
5550 if (error != 0)
5551 return (error);
5552
5553 error = ENOENT;
5554 if (zfsvfs->z_use_sa && zp->z_is_sa)
5555 error = zfs_deleteextattr_sa(ap, attrname);
5556 if (error == ENOENT)
5557 error = zfs_deleteextattr_dir(ap, attrname);
5558 return (error);
5559 }
5560
5561 /*
5562 * Vnode operation to remove a named attribute.
5563 */
5564 static int
5565 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
5566 {
5567 znode_t *zp = VTOZ(ap->a_vp);
5568 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5569 int error;
5570
5571 /*
5572 * If the xattr property is off, refuse the request.
5573 */
5574 if (!(zfsvfs->z_flags & ZSB_XATTR))
5575 return (SET_ERROR(EOPNOTSUPP));
5576
5577 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5578 ap->a_cred, ap->a_td, VWRITE);
5579 if (error != 0)
5580 return (SET_ERROR(error));
5581
5582 error = zfs_check_attrname(ap->a_name);
5583 if (error != 0)
5584 return (error);
5585
5586 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5587 return (error);
5588 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5589
5590 error = zfs_deleteextattr_impl(ap, zfs_xattr_compat);
5591 if ((error == ENOENT || error == ENOATTR) &&
5592 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5593 /*
5594 * Fall back to the alternate namespace format if we failed to
5595 * find a user xattr.
5596 */
5597 error = zfs_deleteextattr_impl(ap, !zfs_xattr_compat);
5598 }
5599
5600 rw_exit(&zp->z_xattr_lock);
5601 zfs_exit(zfsvfs, FTAG);
5602 if (error == ENOENT)
5603 error = SET_ERROR(ENOATTR);
5604 return (error);
5605 }
5606
5607 #ifndef _SYS_SYSPROTO_H_
5608 struct vop_setextattr {
5609 IN struct vnode *a_vp;
5610 IN int a_attrnamespace;
5611 IN const char *a_name;
5612 INOUT struct uio *a_uio;
5613 IN struct ucred *a_cred;
5614 IN struct thread *a_td;
5615 };
5616 #endif
5617
5618 static int
5619 zfs_setextattr_dir(struct vop_setextattr_args *ap, const char *attrname)
5620 {
5621 struct thread *td = ap->a_td;
5622 struct nameidata nd;
5623 struct vattr va;
5624 vnode_t *xvp = NULL, *vp;
5625 int error, flags;
5626
5627 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5628 LOOKUP_XATTR | CREATE_XATTR_DIR, B_FALSE);
5629 if (error != 0)
5630 return (error);
5631
5632 flags = FFLAGS(O_WRONLY | O_CREAT);
5633 #if __FreeBSD_version < 1400043
5634 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp, td);
5635 #else
5636 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5637 #endif
5638 error = vn_open_cred(&nd, &flags, 0600, VN_OPEN_INVFS, ap->a_cred,
5639 NULL);
5640 if (error != 0)
5641 return (SET_ERROR(error));
5642 vp = nd.ni_vp;
5643 NDFREE_PNBUF(&nd);
5644
5645 VATTR_NULL(&va);
5646 va.va_size = 0;
5647 error = VOP_SETATTR(vp, &va, ap->a_cred);
5648 if (error == 0)
5649 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5650
5651 VOP_UNLOCK1(vp);
5652 vn_close(vp, flags, ap->a_cred, td);
5653 return (error);
5654 }
5655
5656 static int
5657 zfs_setextattr_sa(struct vop_setextattr_args *ap, const char *attrname)
5658 {
5659 znode_t *zp = VTOZ(ap->a_vp);
5660 nvlist_t *nvl;
5661 size_t sa_size;
5662 int error;
5663
5664 error = zfs_ensure_xattr_cached(zp);
5665 if (error != 0)
5666 return (error);
5667
5668 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5669 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5670
5671 nvl = zp->z_xattr_cached;
5672 size_t entry_size = ap->a_uio->uio_resid;
5673 if (entry_size > DXATTR_MAX_ENTRY_SIZE)
5674 return (SET_ERROR(EFBIG));
5675 error = nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
5676 if (error != 0)
5677 return (SET_ERROR(error));
5678 if (sa_size > DXATTR_MAX_SA_SIZE)
5679 return (SET_ERROR(EFBIG));
5680 uchar_t *buf = kmem_alloc(entry_size, KM_SLEEP);
5681 error = uiomove(buf, entry_size, ap->a_uio);
5682 if (error != 0) {
5683 error = SET_ERROR(error);
5684 } else {
5685 error = nvlist_add_byte_array(nvl, attrname, buf, entry_size);
5686 if (error != 0)
5687 error = SET_ERROR(error);
5688 }
5689 if (error == 0)
5690 error = zfs_sa_set_xattr(zp, attrname, buf, entry_size);
5691 kmem_free(buf, entry_size);
5692 if (error != 0) {
5693 zp->z_xattr_cached = NULL;
5694 nvlist_free(nvl);
5695 }
5696 return (error);
5697 }
5698
5699 static int
5700 zfs_setextattr_impl(struct vop_setextattr_args *ap, boolean_t compat)
5701 {
5702 znode_t *zp = VTOZ(ap->a_vp);
5703 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5704 char attrname[EXTATTR_MAXNAMELEN+1];
5705 int error;
5706
5707 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5708 sizeof (attrname), compat);
5709 if (error != 0)
5710 return (error);
5711
5712 struct vop_deleteextattr_args vda = {
5713 .a_vp = ap->a_vp,
5714 .a_attrnamespace = ap->a_attrnamespace,
5715 .a_name = ap->a_name,
5716 .a_cred = ap->a_cred,
5717 .a_td = ap->a_td,
5718 };
5719 error = ENOENT;
5720 if (zfsvfs->z_use_sa && zp->z_is_sa && zfsvfs->z_xattr_sa) {
5721 error = zfs_setextattr_sa(ap, attrname);
5722 if (error == 0) {
5723 /*
5724 * Successfully put into SA, we need to clear the one
5725 * in dir if present.
5726 */
5727 zfs_deleteextattr_dir(&vda, attrname);
5728 }
5729 }
5730 if (error != 0) {
5731 error = zfs_setextattr_dir(ap, attrname);
5732 if (error == 0 && zp->z_is_sa) {
5733 /*
5734 * Successfully put into dir, we need to clear the one
5735 * in SA if present.
5736 */
5737 zfs_deleteextattr_sa(&vda, attrname);
5738 }
5739 }
5740 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5741 /*
5742 * Also clear all versions of the alternate compat name.
5743 */
5744 zfs_deleteextattr_impl(&vda, !compat);
5745 }
5746 return (error);
5747 }
5748
5749 /*
5750 * Vnode operation to set a named attribute.
5751 */
5752 static int
5753 zfs_setextattr(struct vop_setextattr_args *ap)
5754 {
5755 znode_t *zp = VTOZ(ap->a_vp);
5756 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5757 int error;
5758
5759 /*
5760 * If the xattr property is off, refuse the request.
5761 */
5762 if (!(zfsvfs->z_flags & ZSB_XATTR))
5763 return (SET_ERROR(EOPNOTSUPP));
5764
5765 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5766 ap->a_cred, ap->a_td, VWRITE);
5767 if (error != 0)
5768 return (SET_ERROR(error));
5769
5770 error = zfs_check_attrname(ap->a_name);
5771 if (error != 0)
5772 return (error);
5773
5774 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5775 return (error);
5776 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5777
5778 error = zfs_setextattr_impl(ap, zfs_xattr_compat);
5779
5780 rw_exit(&zp->z_xattr_lock);
5781 zfs_exit(zfsvfs, FTAG);
5782 return (error);
5783 }
5784
5785 #ifndef _SYS_SYSPROTO_H_
5786 struct vop_listextattr {
5787 IN struct vnode *a_vp;
5788 IN int a_attrnamespace;
5789 INOUT struct uio *a_uio;
5790 OUT size_t *a_size;
5791 IN struct ucred *a_cred;
5792 IN struct thread *a_td;
5793 };
5794 #endif
5795
5796 static int
5797 zfs_listextattr_dir(struct vop_listextattr_args *ap, const char *attrprefix)
5798 {
5799 struct thread *td = ap->a_td;
5800 struct nameidata nd;
5801 uint8_t dirbuf[sizeof (struct dirent)];
5802 struct iovec aiov;
5803 struct uio auio;
5804 vnode_t *xvp = NULL, *vp;
5805 int error, eof;
5806
5807 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5808 LOOKUP_XATTR, B_FALSE);
5809 if (error != 0) {
5810 /*
5811 * ENOATTR means that the EA directory does not yet exist,
5812 * i.e. there are no extended attributes there.
5813 */
5814 if (error == ENOATTR)
5815 error = 0;
5816 return (error);
5817 }
5818
5819 #if __FreeBSD_version < 1400043
5820 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5821 UIO_SYSSPACE, ".", xvp, td);
5822 #else
5823 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5824 UIO_SYSSPACE, ".", xvp);
5825 #endif
5826 error = namei(&nd);
5827 if (error != 0)
5828 return (SET_ERROR(error));
5829 vp = nd.ni_vp;
5830 NDFREE_PNBUF(&nd);
5831
5832 auio.uio_iov = &aiov;
5833 auio.uio_iovcnt = 1;
5834 auio.uio_segflg = UIO_SYSSPACE;
5835 auio.uio_td = td;
5836 auio.uio_rw = UIO_READ;
5837 auio.uio_offset = 0;
5838
5839 size_t plen = strlen(attrprefix);
5840
5841 do {
5842 aiov.iov_base = (void *)dirbuf;
5843 aiov.iov_len = sizeof (dirbuf);
5844 auio.uio_resid = sizeof (dirbuf);
5845 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5846 if (error != 0)
5847 break;
5848 int done = sizeof (dirbuf) - auio.uio_resid;
5849 for (int pos = 0; pos < done; ) {
5850 struct dirent *dp = (struct dirent *)(dirbuf + pos);
5851 pos += dp->d_reclen;
5852 /*
5853 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5854 * is what we get when attribute was created on Solaris.
5855 */
5856 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5857 continue;
5858 else if (plen == 0 &&
5859 ZFS_XA_NS_PREFIX_FORBIDDEN(dp->d_name))
5860 continue;
5861 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5862 continue;
5863 uint8_t nlen = dp->d_namlen - plen;
5864 if (ap->a_size != NULL) {
5865 *ap->a_size += 1 + nlen;
5866 } else if (ap->a_uio != NULL) {
5867 /*
5868 * Format of extattr name entry is one byte for
5869 * length and the rest for name.
5870 */
5871 error = uiomove(&nlen, 1, ap->a_uio);
5872 if (error == 0) {
5873 char *namep = dp->d_name + plen;
5874 error = uiomove(namep, nlen, ap->a_uio);
5875 }
5876 if (error != 0) {
5877 error = SET_ERROR(error);
5878 break;
5879 }
5880 }
5881 }
5882 } while (!eof && error == 0);
5883
5884 vput(vp);
5885 return (error);
5886 }
5887
5888 static int
5889 zfs_listextattr_sa(struct vop_listextattr_args *ap, const char *attrprefix)
5890 {
5891 znode_t *zp = VTOZ(ap->a_vp);
5892 int error;
5893
5894 error = zfs_ensure_xattr_cached(zp);
5895 if (error != 0)
5896 return (error);
5897
5898 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5899 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5900
5901 size_t plen = strlen(attrprefix);
5902 nvpair_t *nvp = NULL;
5903 while ((nvp = nvlist_next_nvpair(zp->z_xattr_cached, nvp)) != NULL) {
5904 ASSERT3U(nvpair_type(nvp), ==, DATA_TYPE_BYTE_ARRAY);
5905
5906 const char *name = nvpair_name(nvp);
5907 if (plen == 0 && ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5908 continue;
5909 else if (strncmp(name, attrprefix, plen) != 0)
5910 continue;
5911 uint8_t nlen = strlen(name) - plen;
5912 if (ap->a_size != NULL) {
5913 *ap->a_size += 1 + nlen;
5914 } else if (ap->a_uio != NULL) {
5915 /*
5916 * Format of extattr name entry is one byte for
5917 * length and the rest for name.
5918 */
5919 error = uiomove(&nlen, 1, ap->a_uio);
5920 if (error == 0) {
5921 char *namep = __DECONST(char *, name) + plen;
5922 error = uiomove(namep, nlen, ap->a_uio);
5923 }
5924 if (error != 0) {
5925 error = SET_ERROR(error);
5926 break;
5927 }
5928 }
5929 }
5930
5931 return (error);
5932 }
5933
5934 static int
5935 zfs_listextattr_impl(struct vop_listextattr_args *ap, boolean_t compat)
5936 {
5937 znode_t *zp = VTOZ(ap->a_vp);
5938 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5939 char attrprefix[16];
5940 int error;
5941
5942 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5943 sizeof (attrprefix), compat);
5944 if (error != 0)
5945 return (error);
5946
5947 if (zfsvfs->z_use_sa && zp->z_is_sa)
5948 error = zfs_listextattr_sa(ap, attrprefix);
5949 if (error == 0)
5950 error = zfs_listextattr_dir(ap, attrprefix);
5951 return (error);
5952 }
5953
5954 /*
5955 * Vnode operation to retrieve extended attributes on a vnode.
5956 */
5957 static int
5958 zfs_listextattr(struct vop_listextattr_args *ap)
5959 {
5960 znode_t *zp = VTOZ(ap->a_vp);
5961 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5962 int error;
5963
5964 if (ap->a_size != NULL)
5965 *ap->a_size = 0;
5966
5967 /*
5968 * If the xattr property is off, refuse the request.
5969 */
5970 if (!(zfsvfs->z_flags & ZSB_XATTR))
5971 return (SET_ERROR(EOPNOTSUPP));
5972
5973 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5974 ap->a_cred, ap->a_td, VREAD);
5975 if (error != 0)
5976 return (SET_ERROR(error));
5977
5978 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5979 return (error);
5980 rw_enter(&zp->z_xattr_lock, RW_READER);
5981
5982 error = zfs_listextattr_impl(ap, zfs_xattr_compat);
5983 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5984 /* Also list user xattrs with the alternate format. */
5985 error = zfs_listextattr_impl(ap, !zfs_xattr_compat);
5986 }
5987
5988 rw_exit(&zp->z_xattr_lock);
5989 zfs_exit(zfsvfs, FTAG);
5990 return (error);
5991 }
5992
5993 #ifndef _SYS_SYSPROTO_H_
5994 struct vop_getacl_args {
5995 struct vnode *vp;
5996 acl_type_t type;
5997 struct acl *aclp;
5998 struct ucred *cred;
5999 struct thread *td;
6000 };
6001 #endif
6002
6003 static int
6004 zfs_freebsd_getacl(struct vop_getacl_args *ap)
6005 {
6006 int error;
6007 vsecattr_t vsecattr;
6008
6009 if (ap->a_type != ACL_TYPE_NFS4)
6010 return (EINVAL);
6011
6012 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
6013 if ((error = zfs_getsecattr(VTOZ(ap->a_vp),
6014 &vsecattr, 0, ap->a_cred)))
6015 return (error);
6016
6017 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp,
6018 vsecattr.vsa_aclcnt);
6019 if (vsecattr.vsa_aclentp != NULL)
6020 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
6021
6022 return (error);
6023 }
6024
6025 #ifndef _SYS_SYSPROTO_H_
6026 struct vop_setacl_args {
6027 struct vnode *vp;
6028 acl_type_t type;
6029 struct acl *aclp;
6030 struct ucred *cred;
6031 struct thread *td;
6032 };
6033 #endif
6034
6035 static int
6036 zfs_freebsd_setacl(struct vop_setacl_args *ap)
6037 {
6038 int error;
6039 vsecattr_t vsecattr;
6040 int aclbsize; /* size of acl list in bytes */
6041 aclent_t *aaclp;
6042
6043 if (ap->a_type != ACL_TYPE_NFS4)
6044 return (EINVAL);
6045
6046 if (ap->a_aclp == NULL)
6047 return (EINVAL);
6048
6049 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
6050 return (EINVAL);
6051
6052 /*
6053 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
6054 * splitting every entry into two and appending "canonical six"
6055 * entries at the end. Don't allow for setting an ACL that would
6056 * cause chmod(2) to run out of ACL entries.
6057 */
6058 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
6059 return (ENOSPC);
6060
6061 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
6062 if (error != 0)
6063 return (error);
6064
6065 vsecattr.vsa_mask = VSA_ACE;
6066 aclbsize = ap->a_aclp->acl_cnt * sizeof (ace_t);
6067 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
6068 aaclp = vsecattr.vsa_aclentp;
6069 vsecattr.vsa_aclentsz = aclbsize;
6070
6071 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
6072 error = zfs_setsecattr(VTOZ(ap->a_vp), &vsecattr, 0, ap->a_cred);
6073 kmem_free(aaclp, aclbsize);
6074
6075 return (error);
6076 }
6077
6078 #ifndef _SYS_SYSPROTO_H_
6079 struct vop_aclcheck_args {
6080 struct vnode *vp;
6081 acl_type_t type;
6082 struct acl *aclp;
6083 struct ucred *cred;
6084 struct thread *td;
6085 };
6086 #endif
6087
6088 static int
6089 zfs_freebsd_aclcheck(struct vop_aclcheck_args *ap)
6090 {
6091
6092 return (EOPNOTSUPP);
6093 }
6094
6095 static int
6096 zfs_vptocnp(struct vop_vptocnp_args *ap)
6097 {
6098 vnode_t *covered_vp;
6099 vnode_t *vp = ap->a_vp;
6100 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
6101 znode_t *zp = VTOZ(vp);
6102 int ltype;
6103 int error;
6104
6105 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
6106 return (error);
6107
6108 /*
6109 * If we are a snapshot mounted under .zfs, run the operation
6110 * on the covered vnode.
6111 */
6112 if (zp->z_id != zfsvfs->z_root || zfsvfs->z_parent == zfsvfs) {
6113 char name[MAXNAMLEN + 1];
6114 znode_t *dzp;
6115 size_t len;
6116
6117 error = zfs_znode_parent_and_name(zp, &dzp, name);
6118 if (error == 0) {
6119 len = strlen(name);
6120 if (*ap->a_buflen < len)
6121 error = SET_ERROR(ENOMEM);
6122 }
6123 if (error == 0) {
6124 *ap->a_buflen -= len;
6125 memcpy(ap->a_buf + *ap->a_buflen, name, len);
6126 *ap->a_vpp = ZTOV(dzp);
6127 }
6128 zfs_exit(zfsvfs, FTAG);
6129 return (error);
6130 }
6131 zfs_exit(zfsvfs, FTAG);
6132
6133 covered_vp = vp->v_mount->mnt_vnodecovered;
6134 #if __FreeBSD_version >= 1300045
6135 enum vgetstate vs = vget_prep(covered_vp);
6136 #else
6137 vhold(covered_vp);
6138 #endif
6139 ltype = VOP_ISLOCKED(vp);
6140 VOP_UNLOCK1(vp);
6141 #if __FreeBSD_version >= 1300045
6142 error = vget_finish(covered_vp, LK_SHARED, vs);
6143 #else
6144 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
6145 #endif
6146 if (error == 0) {
6147 #if __FreeBSD_version >= 1300123
6148 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_buf,
6149 ap->a_buflen);
6150 #else
6151 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
6152 ap->a_buf, ap->a_buflen);
6153 #endif
6154 vput(covered_vp);
6155 }
6156 vn_lock(vp, ltype | LK_RETRY);
6157 if (VN_IS_DOOMED(vp))
6158 error = SET_ERROR(ENOENT);
6159 return (error);
6160 }
6161
6162 #if __FreeBSD_version >= 1400032
6163 static int
6164 zfs_deallocate(struct vop_deallocate_args *ap)
6165 {
6166 znode_t *zp = VTOZ(ap->a_vp);
6167 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6168 zilog_t *zilog;
6169 off_t off, len, file_sz;
6170 int error;
6171
6172 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
6173 return (error);
6174
6175 /*
6176 * Callers might not be able to detect properly that we are read-only,
6177 * so check it explicitly here.
6178 */
6179 if (zfs_is_readonly(zfsvfs)) {
6180 zfs_exit(zfsvfs, FTAG);
6181 return (SET_ERROR(EROFS));
6182 }
6183
6184 zilog = zfsvfs->z_log;
6185 off = *ap->a_offset;
6186 len = *ap->a_len;
6187 file_sz = zp->z_size;
6188 if (off + len > file_sz)
6189 len = file_sz - off;
6190 /* Fast path for out-of-range request. */
6191 if (len <= 0) {
6192 *ap->a_len = 0;
6193 zfs_exit(zfsvfs, FTAG);
6194 return (0);
6195 }
6196
6197 error = zfs_freesp(zp, off, len, O_RDWR, TRUE);
6198 if (error == 0) {
6199 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS ||
6200 (ap->a_ioflag & IO_SYNC) != 0)
6201 zil_commit(zilog, zp->z_id);
6202 *ap->a_offset = off + len;
6203 *ap->a_len = 0;
6204 }
6205
6206 zfs_exit(zfsvfs, FTAG);
6207 return (error);
6208 }
6209 #endif
6210
6211 struct vop_vector zfs_vnodeops;
6212 struct vop_vector zfs_fifoops;
6213 struct vop_vector zfs_shareops;
6214
6215 struct vop_vector zfs_vnodeops = {
6216 .vop_default = &default_vnodeops,
6217 .vop_inactive = zfs_freebsd_inactive,
6218 #if __FreeBSD_version >= 1300042
6219 .vop_need_inactive = zfs_freebsd_need_inactive,
6220 #endif
6221 .vop_reclaim = zfs_freebsd_reclaim,
6222 #if __FreeBSD_version >= 1300102
6223 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6224 #endif
6225 #if __FreeBSD_version >= 1300139
6226 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6227 #endif
6228 .vop_access = zfs_freebsd_access,
6229 .vop_allocate = VOP_EINVAL,
6230 #if __FreeBSD_version >= 1400032
6231 .vop_deallocate = zfs_deallocate,
6232 #endif
6233 .vop_lookup = zfs_cache_lookup,
6234 .vop_cachedlookup = zfs_freebsd_cachedlookup,
6235 .vop_getattr = zfs_freebsd_getattr,
6236 .vop_setattr = zfs_freebsd_setattr,
6237 .vop_create = zfs_freebsd_create,
6238 .vop_mknod = (vop_mknod_t *)zfs_freebsd_create,
6239 .vop_mkdir = zfs_freebsd_mkdir,
6240 .vop_readdir = zfs_freebsd_readdir,
6241 .vop_fsync = zfs_freebsd_fsync,
6242 .vop_open = zfs_freebsd_open,
6243 .vop_close = zfs_freebsd_close,
6244 .vop_rmdir = zfs_freebsd_rmdir,
6245 .vop_ioctl = zfs_freebsd_ioctl,
6246 .vop_link = zfs_freebsd_link,
6247 .vop_symlink = zfs_freebsd_symlink,
6248 .vop_readlink = zfs_freebsd_readlink,
6249 .vop_read = zfs_freebsd_read,
6250 .vop_write = zfs_freebsd_write,
6251 .vop_remove = zfs_freebsd_remove,
6252 .vop_rename = zfs_freebsd_rename,
6253 .vop_pathconf = zfs_freebsd_pathconf,
6254 .vop_bmap = zfs_freebsd_bmap,
6255 .vop_fid = zfs_freebsd_fid,
6256 .vop_getextattr = zfs_getextattr,
6257 .vop_deleteextattr = zfs_deleteextattr,
6258 .vop_setextattr = zfs_setextattr,
6259 .vop_listextattr = zfs_listextattr,
6260 .vop_getacl = zfs_freebsd_getacl,
6261 .vop_setacl = zfs_freebsd_setacl,
6262 .vop_aclcheck = zfs_freebsd_aclcheck,
6263 .vop_getpages = zfs_freebsd_getpages,
6264 .vop_putpages = zfs_freebsd_putpages,
6265 .vop_vptocnp = zfs_vptocnp,
6266 #if __FreeBSD_version >= 1300064
6267 .vop_lock1 = vop_lock,
6268 .vop_unlock = vop_unlock,
6269 .vop_islocked = vop_islocked,
6270 #endif
6271 #if __FreeBSD_version >= 1400043
6272 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6273 #endif
6274 };
6275 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops);
6276
6277 struct vop_vector zfs_fifoops = {
6278 .vop_default = &fifo_specops,
6279 .vop_fsync = zfs_freebsd_fsync,
6280 #if __FreeBSD_version >= 1300102
6281 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6282 #endif
6283 #if __FreeBSD_version >= 1300139
6284 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6285 #endif
6286 .vop_access = zfs_freebsd_access,
6287 .vop_getattr = zfs_freebsd_getattr,
6288 .vop_inactive = zfs_freebsd_inactive,
6289 .vop_read = VOP_PANIC,
6290 .vop_reclaim = zfs_freebsd_reclaim,
6291 .vop_setattr = zfs_freebsd_setattr,
6292 .vop_write = VOP_PANIC,
6293 .vop_pathconf = zfs_freebsd_pathconf,
6294 .vop_fid = zfs_freebsd_fid,
6295 .vop_getacl = zfs_freebsd_getacl,
6296 .vop_setacl = zfs_freebsd_setacl,
6297 .vop_aclcheck = zfs_freebsd_aclcheck,
6298 #if __FreeBSD_version >= 1400043
6299 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6300 #endif
6301 };
6302 VFS_VOP_VECTOR_REGISTER(zfs_fifoops);
6303
6304 /*
6305 * special share hidden files vnode operations template
6306 */
6307 struct vop_vector zfs_shareops = {
6308 .vop_default = &default_vnodeops,
6309 #if __FreeBSD_version >= 1300121
6310 .vop_fplookup_vexec = VOP_EAGAIN,
6311 #endif
6312 #if __FreeBSD_version >= 1300139
6313 .vop_fplookup_symlink = VOP_EAGAIN,
6314 #endif
6315 .vop_access = zfs_freebsd_access,
6316 .vop_inactive = zfs_freebsd_inactive,
6317 .vop_reclaim = zfs_freebsd_reclaim,
6318 .vop_fid = zfs_freebsd_fid,
6319 .vop_pathconf = zfs_freebsd_pathconf,
6320 #if __FreeBSD_version >= 1400043
6321 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6322 #endif
6323 };
6324 VFS_VOP_VECTOR_REGISTER(zfs_shareops);
6325
6326 ZFS_MODULE_PARAM(zfs, zfs_, xattr_compat, INT, ZMOD_RW,
6327 "Use legacy ZFS xattr naming for writing new user namespace xattrs");
Cache object: 4a94ece3b0f3cbd20bdd95e96d570966
|