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