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