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