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