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