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