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