]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_znode.c
Fix hangs with processes stuck sleeping on btalloc on i386.
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zfs_znode.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) 2012, 2018 by Delphix. All rights reserved.
24  * Copyright (c) 2014 Integros [integros.com]
25  */
26
27 /* Portions Copyright 2007 Jeremy Teo */
28 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
29
30 #ifdef _KERNEL
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/resource.h>
37 #include <sys/mntent.h>
38 #include <sys/u8_textprep.h>
39 #include <sys/dsl_dataset.h>
40 #include <sys/vfs.h>
41 #include <sys/vnode.h>
42 #include <sys/file.h>
43 #include <sys/kmem.h>
44 #include <sys/errno.h>
45 #include <sys/unistd.h>
46 #include <sys/atomic.h>
47 #include <sys/zfs_dir.h>
48 #include <sys/zfs_acl.h>
49 #include <sys/zfs_ioctl.h>
50 #include <sys/zfs_rlock.h>
51 #include <sys/zfs_fuid.h>
52 #include <sys/dnode.h>
53 #include <sys/fs/zfs.h>
54 #include <sys/kidmap.h>
55 #endif /* _KERNEL */
56
57 #include <sys/dmu.h>
58 #include <sys/dmu_objset.h>
59 #include <sys/dmu_tx.h>
60 #include <sys/refcount.h>
61 #include <sys/stat.h>
62 #include <sys/zap.h>
63 #include <sys/zfs_znode.h>
64 #include <sys/sa.h>
65 #include <sys/zfs_sa.h>
66 #include <sys/zfs_stat.h>
67 #include <sys/refcount.h>
68
69 #include "zfs_prop.h"
70 #include "zfs_comutil.h"
71
72 /* Used by fstat(1). */
73 SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD,
74     SYSCTL_NULL_INT_PTR, sizeof(znode_t), "sizeof(znode_t)");
75
76 /*
77  * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
78  * turned on when DEBUG is also defined.
79  */
80 #ifdef  DEBUG
81 #define ZNODE_STATS
82 #endif  /* DEBUG */
83
84 #ifdef  ZNODE_STATS
85 #define ZNODE_STAT_ADD(stat)                    ((stat)++)
86 #else
87 #define ZNODE_STAT_ADD(stat)                    /* nothing */
88 #endif  /* ZNODE_STATS */
89
90 /*
91  * Functions needed for userland (ie: libzpool) are not put under
92  * #ifdef_KERNEL; the rest of the functions have dependencies
93  * (such as VFS logic) that will not compile easily in userland.
94  */
95 #ifdef _KERNEL
96 /*
97  * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
98  * be freed before it can be safely accessed.
99  */
100 krwlock_t zfsvfs_lock;
101
102 #if defined(_KERNEL) && !defined(KMEM_DEBUG)
103 #define _ZFS_USE_SMR
104 static uma_zone_t znode_uma_zone;
105 #else
106 static kmem_cache_t *znode_cache = NULL;
107 #endif
108
109 /*ARGSUSED*/
110 static void
111 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
112 {
113         /*
114          * We should never drop all dbuf refs without first clearing
115          * the eviction callback.
116          */
117         panic("evicting znode %p\n", user_ptr);
118 }
119
120 extern struct vop_vector zfs_vnodeops;
121 extern struct vop_vector zfs_fifoops;
122 extern struct vop_vector zfs_shareops;
123
124 /*
125  * This callback is invoked when acquiring a RL_WRITER or RL_APPEND lock on
126  * z_rangelock. It will modify the offset and length of the lock to reflect
127  * znode-specific information, and convert RL_APPEND to RL_WRITER.  This is
128  * called with the rangelock_t's rl_lock held, which avoids races.
129  */
130 static void
131 zfs_rangelock_cb(locked_range_t *new, void *arg)
132 {
133         znode_t *zp = arg;
134
135         /*
136          * If in append mode, convert to writer and lock starting at the
137          * current end of file.
138          */
139         if (new->lr_type == RL_APPEND) {
140                 new->lr_offset = zp->z_size;
141                 new->lr_type = RL_WRITER;
142         }
143
144         /*
145          * If we need to grow the block size then lock the whole file range.
146          */
147         uint64_t end_size = MAX(zp->z_size, new->lr_offset + new->lr_length);
148         if (end_size > zp->z_blksz && (!ISP2(zp->z_blksz) ||
149             zp->z_blksz < zp->z_zfsvfs->z_max_blksz)) {
150                 new->lr_offset = 0;
151                 new->lr_length = UINT64_MAX;
152         }
153 }
154
155 /*ARGSUSED*/
156 static int
157 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
158 {
159         znode_t *zp = buf;
160
161         POINTER_INVALIDATE(&zp->z_zfsvfs);
162
163         list_link_init(&zp->z_link_node);
164
165         mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
166
167         rangelock_init(&zp->z_rangelock, zfs_rangelock_cb, zp);
168
169         zp->z_acl_cached = NULL;
170         zp->z_vnode = NULL;
171         zp->z_moved = 0;
172         return (0);
173 }
174
175 /*ARGSUSED*/
176 static void
177 zfs_znode_cache_destructor(void *buf, void *arg)
178 {
179         znode_t *zp = buf;
180
181         ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
182         ASSERT3P(zp->z_vnode, ==, NULL);
183         ASSERT(!list_link_active(&zp->z_link_node));
184         mutex_destroy(&zp->z_acl_lock);
185         rangelock_fini(&zp->z_rangelock);
186
187         ASSERT(zp->z_acl_cached == NULL);
188 }
189
190 #ifdef  ZNODE_STATS
191 static struct {
192         uint64_t zms_zfsvfs_invalid;
193         uint64_t zms_zfsvfs_recheck1;
194         uint64_t zms_zfsvfs_unmounted;
195         uint64_t zms_zfsvfs_recheck2;
196         uint64_t zms_obj_held;
197         uint64_t zms_vnode_locked;
198         uint64_t zms_not_only_dnlc;
199 } znode_move_stats;
200 #endif  /* ZNODE_STATS */
201
202 #ifdef illumos
203 static void
204 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
205 {
206         vnode_t *vp;
207
208         /* Copy fields. */
209         nzp->z_zfsvfs = ozp->z_zfsvfs;
210
211         /* Swap vnodes. */
212         vp = nzp->z_vnode;
213         nzp->z_vnode = ozp->z_vnode;
214         ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
215         ZTOV(ozp)->v_data = ozp;
216         ZTOV(nzp)->v_data = nzp;
217
218         nzp->z_id = ozp->z_id;
219         ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
220         nzp->z_unlinked = ozp->z_unlinked;
221         nzp->z_atime_dirty = ozp->z_atime_dirty;
222         nzp->z_zn_prefetch = ozp->z_zn_prefetch;
223         nzp->z_blksz = ozp->z_blksz;
224         nzp->z_seq = ozp->z_seq;
225         nzp->z_mapcnt = ozp->z_mapcnt;
226         nzp->z_gen = ozp->z_gen;
227         nzp->z_sync_cnt = ozp->z_sync_cnt;
228         nzp->z_is_sa = ozp->z_is_sa;
229         nzp->z_sa_hdl = ozp->z_sa_hdl;
230         bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
231         nzp->z_links = ozp->z_links;
232         nzp->z_size = ozp->z_size;
233         nzp->z_pflags = ozp->z_pflags;
234         nzp->z_uid = ozp->z_uid;
235         nzp->z_gid = ozp->z_gid;
236         nzp->z_mode = ozp->z_mode;
237
238         /*
239          * Since this is just an idle znode and kmem is already dealing with
240          * memory pressure, release any cached ACL.
241          */
242         if (ozp->z_acl_cached) {
243                 zfs_acl_free(ozp->z_acl_cached);
244                 ozp->z_acl_cached = NULL;
245         }
246
247         sa_set_userp(nzp->z_sa_hdl, nzp);
248
249         /*
250          * Invalidate the original znode by clearing fields that provide a
251          * pointer back to the znode. Set the low bit of the vfs pointer to
252          * ensure that zfs_znode_move() recognizes the znode as invalid in any
253          * subsequent callback.
254          */
255         ozp->z_sa_hdl = NULL;
256         POINTER_INVALIDATE(&ozp->z_zfsvfs);
257
258         /*
259          * Mark the znode.
260          */
261         nzp->z_moved = 1;
262         ozp->z_moved = (uint8_t)-1;
263 }
264
265 /*ARGSUSED*/
266 static kmem_cbrc_t
267 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
268 {
269         znode_t *ozp = buf, *nzp = newbuf;
270         zfsvfs_t *zfsvfs;
271         vnode_t *vp;
272
273         /*
274          * The znode is on the file system's list of known znodes if the vfs
275          * pointer is valid. We set the low bit of the vfs pointer when freeing
276          * the znode to invalidate it, and the memory patterns written by kmem
277          * (baddcafe and deadbeef) set at least one of the two low bits. A newly
278          * created znode sets the vfs pointer last of all to indicate that the
279          * znode is known and in a valid state to be moved by this function.
280          */
281         zfsvfs = ozp->z_zfsvfs;
282         if (!POINTER_IS_VALID(zfsvfs)) {
283                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
284                 return (KMEM_CBRC_DONT_KNOW);
285         }
286
287         /*
288          * Close a small window in which it's possible that the filesystem could
289          * be unmounted and freed, and zfsvfs, though valid in the previous
290          * statement, could point to unrelated memory by the time we try to
291          * prevent the filesystem from being unmounted.
292          */
293         rw_enter(&zfsvfs_lock, RW_WRITER);
294         if (zfsvfs != ozp->z_zfsvfs) {
295                 rw_exit(&zfsvfs_lock);
296                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
297                 return (KMEM_CBRC_DONT_KNOW);
298         }
299
300         /*
301          * If the znode is still valid, then so is the file system. We know that
302          * no valid file system can be freed while we hold zfsvfs_lock, so we
303          * can safely ensure that the filesystem is not and will not be
304          * unmounted. The next statement is equivalent to ZFS_ENTER().
305          */
306         rrm_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
307         if (zfsvfs->z_unmounted) {
308                 ZFS_EXIT(zfsvfs);
309                 rw_exit(&zfsvfs_lock);
310                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
311                 return (KMEM_CBRC_DONT_KNOW);
312         }
313         rw_exit(&zfsvfs_lock);
314
315         mutex_enter(&zfsvfs->z_znodes_lock);
316         /*
317          * Recheck the vfs pointer in case the znode was removed just before
318          * acquiring the lock.
319          */
320         if (zfsvfs != ozp->z_zfsvfs) {
321                 mutex_exit(&zfsvfs->z_znodes_lock);
322                 ZFS_EXIT(zfsvfs);
323                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
324                 return (KMEM_CBRC_DONT_KNOW);
325         }
326
327         /*
328          * At this point we know that as long as we hold z_znodes_lock, the
329          * znode cannot be freed and fields within the znode can be safely
330          * accessed. Now, prevent a race with zfs_zget().
331          */
332         if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
333                 mutex_exit(&zfsvfs->z_znodes_lock);
334                 ZFS_EXIT(zfsvfs);
335                 ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
336                 return (KMEM_CBRC_LATER);
337         }
338
339         vp = ZTOV(ozp);
340         if (mutex_tryenter(&vp->v_lock) == 0) {
341                 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
342                 mutex_exit(&zfsvfs->z_znodes_lock);
343                 ZFS_EXIT(zfsvfs);
344                 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
345                 return (KMEM_CBRC_LATER);
346         }
347
348         /* Only move znodes that are referenced _only_ by the DNLC. */
349         if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
350                 mutex_exit(&vp->v_lock);
351                 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
352                 mutex_exit(&zfsvfs->z_znodes_lock);
353                 ZFS_EXIT(zfsvfs);
354                 ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
355                 return (KMEM_CBRC_LATER);
356         }
357
358         /*
359          * The znode is known and in a valid state to move. We're holding the
360          * locks needed to execute the critical section.
361          */
362         zfs_znode_move_impl(ozp, nzp);
363         mutex_exit(&vp->v_lock);
364         ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
365
366         list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
367         mutex_exit(&zfsvfs->z_znodes_lock);
368         ZFS_EXIT(zfsvfs);
369
370         return (KMEM_CBRC_YES);
371 }
372 #endif /* illumos */
373
374 #ifdef _ZFS_USE_SMR
375 VFS_SMR_DECLARE;
376
377 static int
378 zfs_znode_cache_constructor_smr(void *mem, int size __unused, void *private, int flags)
379 {
380
381         return (zfs_znode_cache_constructor(mem, private, flags));
382 }
383
384 static void
385 zfs_znode_cache_destructor_smr(void *mem, int size __unused, void *private)
386 {
387
388         zfs_znode_cache_destructor(mem, private);
389 }
390
391 void
392 zfs_znode_init(void)
393 {
394         /*
395          * Initialize zcache
396          */
397         rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
398         ASSERT(znode_uma_zone == NULL);
399         znode_uma_zone = uma_zcreate("zfs_znode_cache",
400             sizeof (znode_t), zfs_znode_cache_constructor_smr,
401             zfs_znode_cache_destructor_smr, NULL, NULL, 0, 0);
402         VFS_SMR_ZONE_SET(znode_uma_zone);
403 }
404
405 static znode_t *
406 zfs_znode_alloc_kmem(int flags)
407 {
408
409         return (uma_zalloc_smr(znode_uma_zone, flags));
410 }
411
412 static void
413 zfs_znode_free_kmem(znode_t *zp)
414 {
415
416         uma_zfree_smr(znode_uma_zone, zp);
417 }
418 #else
419 void
420 zfs_znode_init(void)
421 {
422         /*
423          * Initialize zcache
424          */
425         rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
426         ASSERT(znode_cache == NULL);
427         znode_cache = kmem_cache_create("zfs_znode_cache",
428             sizeof (znode_t), 0, zfs_znode_cache_constructor,
429             zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
430         kmem_cache_set_move(znode_cache, zfs_znode_move);
431 }
432
433 static znode_t *
434 zfs_znode_alloc_kmem(int flags)
435 {
436
437         return (kmem_cache_alloc(znode_cache, flags));
438 }
439
440 static void
441 zfs_znode_free_kmem(znode_t *zp)
442 {
443
444         kmem_cache_free(znode_cache, zp);
445 }
446 #endif
447
448 void
449 zfs_znode_fini(void)
450 {
451 #ifdef illumos
452         /*
453          * Cleanup vfs & vnode ops
454          */
455         zfs_remove_op_tables();
456 #endif
457
458         /*
459          * Cleanup zcache
460          */
461 #ifdef _ZFS_USE_SMR
462         if (znode_uma_zone) {
463                 uma_zdestroy(znode_uma_zone);
464                 znode_uma_zone = NULL;
465         }
466 #else
467         if (znode_cache) {
468                 kmem_cache_destroy(znode_cache);
469                 znode_cache = NULL;
470         }
471 #endif
472         rw_destroy(&zfsvfs_lock);
473 }
474
475 #ifdef illumos
476 struct vnodeops *zfs_dvnodeops;
477 struct vnodeops *zfs_fvnodeops;
478 struct vnodeops *zfs_symvnodeops;
479 struct vnodeops *zfs_xdvnodeops;
480 struct vnodeops *zfs_evnodeops;
481 struct vnodeops *zfs_sharevnodeops;
482
483 void
484 zfs_remove_op_tables()
485 {
486         /*
487          * Remove vfs ops
488          */
489         ASSERT(zfsfstype);
490         (void) vfs_freevfsops_by_type(zfsfstype);
491         zfsfstype = 0;
492
493         /*
494          * Remove vnode ops
495          */
496         if (zfs_dvnodeops)
497                 vn_freevnodeops(zfs_dvnodeops);
498         if (zfs_fvnodeops)
499                 vn_freevnodeops(zfs_fvnodeops);
500         if (zfs_symvnodeops)
501                 vn_freevnodeops(zfs_symvnodeops);
502         if (zfs_xdvnodeops)
503                 vn_freevnodeops(zfs_xdvnodeops);
504         if (zfs_evnodeops)
505                 vn_freevnodeops(zfs_evnodeops);
506         if (zfs_sharevnodeops)
507                 vn_freevnodeops(zfs_sharevnodeops);
508
509         zfs_dvnodeops = NULL;
510         zfs_fvnodeops = NULL;
511         zfs_symvnodeops = NULL;
512         zfs_xdvnodeops = NULL;
513         zfs_evnodeops = NULL;
514         zfs_sharevnodeops = NULL;
515 }
516
517 extern const fs_operation_def_t zfs_dvnodeops_template[];
518 extern const fs_operation_def_t zfs_fvnodeops_template[];
519 extern const fs_operation_def_t zfs_xdvnodeops_template[];
520 extern const fs_operation_def_t zfs_symvnodeops_template[];
521 extern const fs_operation_def_t zfs_evnodeops_template[];
522 extern const fs_operation_def_t zfs_sharevnodeops_template[];
523
524 int
525 zfs_create_op_tables()
526 {
527         int error;
528
529         /*
530          * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
531          * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
532          * In this case we just return as the ops vectors are already set up.
533          */
534         if (zfs_dvnodeops)
535                 return (0);
536
537         error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
538             &zfs_dvnodeops);
539         if (error)
540                 return (error);
541
542         error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
543             &zfs_fvnodeops);
544         if (error)
545                 return (error);
546
547         error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
548             &zfs_symvnodeops);
549         if (error)
550                 return (error);
551
552         error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
553             &zfs_xdvnodeops);
554         if (error)
555                 return (error);
556
557         error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
558             &zfs_evnodeops);
559         if (error)
560                 return (error);
561
562         error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
563             &zfs_sharevnodeops);
564
565         return (error);
566 }
567 #endif  /* illumos */
568
569 int
570 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
571 {
572         zfs_acl_ids_t acl_ids;
573         vattr_t vattr;
574         znode_t *sharezp;
575         znode_t *zp;
576         int error;
577
578         vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
579         vattr.va_type = VDIR;
580         vattr.va_mode = S_IFDIR|0555;
581         vattr.va_uid = crgetuid(kcred);
582         vattr.va_gid = crgetgid(kcred);
583
584         sharezp = zfs_znode_alloc_kmem(KM_SLEEP);
585         ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
586         sharezp->z_moved = 0;
587         sharezp->z_unlinked = 0;
588         sharezp->z_atime_dirty = 0;
589         sharezp->z_zfsvfs = zfsvfs;
590         sharezp->z_is_sa = zfsvfs->z_use_sa;
591
592         VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
593             kcred, NULL, &acl_ids));
594         zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
595         ASSERT3P(zp, ==, sharezp);
596         POINTER_INVALIDATE(&sharezp->z_zfsvfs);
597         error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
598             ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
599         zfsvfs->z_shares_dir = sharezp->z_id;
600
601         zfs_acl_ids_free(&acl_ids);
602         sa_handle_destroy(sharezp->z_sa_hdl);
603         zfs_znode_free_kmem(sharezp);
604
605         return (error);
606 }
607
608 /*
609  * define a couple of values we need available
610  * for both 64 and 32 bit environments.
611  */
612 #ifndef NBITSMINOR64
613 #define NBITSMINOR64    32
614 #endif
615 #ifndef MAXMAJ64
616 #define MAXMAJ64        0xffffffffUL
617 #endif
618 #ifndef MAXMIN64
619 #define MAXMIN64        0xffffffffUL
620 #endif
621
622 /*
623  * Create special expldev for ZFS private use.
624  * Can't use standard expldev since it doesn't do
625  * what we want.  The standard expldev() takes a
626  * dev32_t in LP64 and expands it to a long dev_t.
627  * We need an interface that takes a dev32_t in ILP32
628  * and expands it to a long dev_t.
629  */
630 static uint64_t
631 zfs_expldev(dev_t dev)
632 {
633         return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
634 }
635 /*
636  * Special cmpldev for ZFS private use.
637  * Can't use standard cmpldev since it takes
638  * a long dev_t and compresses it to dev32_t in
639  * LP64.  We need to do a compaction of a long dev_t
640  * to a dev32_t in ILP32.
641  */
642 dev_t
643 zfs_cmpldev(uint64_t dev)
644 {
645         return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
646 }
647
648 static void
649 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
650     dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
651 {
652         ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
653         ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
654
655         ASSERT(zp->z_sa_hdl == NULL);
656         ASSERT(zp->z_acl_cached == NULL);
657         if (sa_hdl == NULL) {
658                 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
659                     SA_HDL_SHARED, &zp->z_sa_hdl));
660         } else {
661                 zp->z_sa_hdl = sa_hdl;
662                 sa_set_userp(sa_hdl, zp);
663         }
664
665         zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
666
667         /*
668          * Slap on VROOT if we are the root znode unless we are the root
669          * node of a snapshot mounted under .zfs.
670          */
671         if (zp->z_id == zfsvfs->z_root && zfsvfs->z_parent == zfsvfs)
672                 ZTOV(zp)->v_flag |= VROOT;
673
674         vn_exists(ZTOV(zp));
675 }
676
677 void
678 zfs_znode_dmu_fini(znode_t *zp)
679 {
680         ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
681             zp->z_unlinked ||
682             ZFS_TEARDOWN_INACTIVE_WLOCKED(zp->z_zfsvfs));
683
684         sa_handle_destroy(zp->z_sa_hdl);
685         zp->z_sa_hdl = NULL;
686 }
687
688 static void
689 zfs_vnode_forget(vnode_t *vp)
690 {
691
692         /* copied from insmntque_stddtr */
693         vp->v_data = NULL;
694         vp->v_op = &dead_vnodeops;
695         vgone(vp);
696         vput(vp);
697 }
698
699 /*
700  * Construct a new znode/vnode and intialize.
701  *
702  * This does not do a call to dmu_set_user() that is
703  * up to the caller to do, in case you don't want to
704  * return the znode
705  */
706 static znode_t *
707 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
708     dmu_object_type_t obj_type, sa_handle_t *hdl)
709 {
710         znode_t *zp;
711         vnode_t *vp;
712         uint64_t mode;
713         uint64_t parent;
714         sa_bulk_attr_t bulk[9];
715         int count = 0;
716         int error;
717
718         zp = zfs_znode_alloc_kmem(KM_SLEEP);
719
720 #ifndef _ZFS_USE_SMR
721         KASSERT((zfsvfs->z_parent->z_vfs->mnt_kern_flag & MNTK_FPLOOKUP) == 0,
722             ("%s: fast path lookup enabled without smr", __func__));
723 #endif
724
725         KASSERT(curthread->td_vp_reserved != NULL,
726             ("zfs_znode_alloc: getnewvnode without preallocated vnode"));
727         error = getnewvnode("zfs", zfsvfs->z_parent->z_vfs, &zfs_vnodeops, &vp);
728         if (error != 0) {
729                 zfs_znode_free_kmem(zp);
730                 return (NULL);
731         }
732         zp->z_vnode = vp;
733         vp->v_data = zp;
734
735         ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
736         zp->z_moved = 0;
737
738         /*
739          * Defer setting z_zfsvfs until the znode is ready to be a candidate for
740          * the zfs_znode_move() callback.
741          */
742         zp->z_sa_hdl = NULL;
743         zp->z_unlinked = 0;
744         zp->z_atime_dirty = 0;
745         zp->z_mapcnt = 0;
746         zp->z_id = db->db_object;
747         zp->z_blksz = blksz;
748         zp->z_seq = 0x7A4653;
749         zp->z_sync_cnt = 0;
750
751         vp = ZTOV(zp);
752
753         zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
754
755         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
756         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
757         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
758             &zp->z_size, 8);
759         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
760             &zp->z_links, 8);
761         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
762             &zp->z_pflags, 8);
763         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
764         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
765             &zp->z_atime, 16);
766         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
767             &zp->z_uid, 8);
768         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
769             &zp->z_gid, 8);
770
771         if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
772                 if (hdl == NULL)
773                         sa_handle_destroy(zp->z_sa_hdl);
774                 zfs_vnode_forget(vp);
775                 zp->z_vnode = NULL;
776                 zfs_znode_free_kmem(zp);
777                 return (NULL);
778         }
779
780         zp->z_mode = mode;
781
782         vp->v_type = IFTOVT((mode_t)mode);
783
784         switch (vp->v_type) {
785         case VDIR:
786                 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
787                 break;
788 #ifdef illumos
789         case VBLK:
790         case VCHR:
791                 {
792                         uint64_t rdev;
793                         VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
794                             &rdev, sizeof (rdev)) == 0);
795
796                         vp->v_rdev = zfs_cmpldev(rdev);
797                 }
798                 break;
799 #endif
800         case VFIFO:
801 #ifdef illumos
802         case VSOCK:
803         case VDOOR:
804 #endif
805                 vp->v_op = &zfs_fifoops;
806                 break;
807         case VREG:
808                 if (parent == zfsvfs->z_shares_dir) {
809                         ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
810                         vp->v_op = &zfs_shareops;
811                 }
812                 break;
813 #ifdef illumos
814         case VLNK:
815                 vn_setops(vp, zfs_symvnodeops);
816                 break;
817         default:
818                 vn_setops(vp, zfs_evnodeops);
819                 break;
820 #endif
821         }
822
823         mutex_enter(&zfsvfs->z_znodes_lock);
824         list_insert_tail(&zfsvfs->z_all_znodes, zp);
825         membar_producer();
826         /*
827          * Everything else must be valid before assigning z_zfsvfs makes the
828          * znode eligible for zfs_znode_move().
829          */
830         zp->z_zfsvfs = zfsvfs;
831         mutex_exit(&zfsvfs->z_znodes_lock);
832
833         /*
834          * Acquire vnode lock before making it available to the world.
835          */
836         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
837         VN_LOCK_AREC(vp);
838         if (vp->v_type != VFIFO)
839                 VN_LOCK_ASHARE(vp);
840
841 #ifdef illumos
842         VFS_HOLD(zfsvfs->z_vfs);
843 #endif
844         return (zp);
845 }
846
847 static uint64_t empty_xattr;
848 static uint64_t pad[4];
849 static zfs_acl_phys_t acl_phys;
850 /*
851  * Create a new DMU object to hold a zfs znode.
852  *
853  *      IN:     dzp     - parent directory for new znode
854  *              vap     - file attributes for new znode
855  *              tx      - dmu transaction id for zap operations
856  *              cr      - credentials of caller
857  *              flag    - flags:
858  *                        IS_ROOT_NODE  - new object will be root
859  *                        IS_XATTR      - new object is an attribute
860  *              bonuslen - length of bonus buffer
861  *              setaclp  - File/Dir initial ACL
862  *              fuidp    - Tracks fuid allocation.
863  *
864  *      OUT:    zpp     - allocated znode
865  *
866  */
867 void
868 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
869     uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
870 {
871         uint64_t        crtime[2], atime[2], mtime[2], ctime[2];
872         uint64_t        mode, size, links, parent, pflags;
873         uint64_t        dzp_pflags = 0;
874         uint64_t        rdev = 0;
875         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
876         dmu_buf_t       *db;
877         timestruc_t     now;
878         uint64_t        gen, obj;
879         int             err;
880         int             bonuslen;
881         int             dnodesize;
882         sa_handle_t     *sa_hdl;
883         dmu_object_type_t obj_type;
884         sa_bulk_attr_t  *sa_attrs;
885         int             cnt = 0;
886         zfs_acl_locator_cb_t locate = { 0 };
887
888         ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
889
890         if (zfsvfs->z_replay) {
891                 obj = vap->va_nodeid;
892                 now = vap->va_ctime;            /* see zfs_replay_create() */
893                 gen = vap->va_nblocks;          /* ditto */
894                 dnodesize = vap->va_fsid;       /* ditto */
895         } else {
896                 obj = 0;
897                 vfs_timestamp(&now);
898                 gen = dmu_tx_get_txg(tx);
899                 dnodesize = dmu_objset_dnodesize(zfsvfs->z_os);
900         }
901
902         if (dnodesize == 0)
903                 dnodesize = DNODE_MIN_SIZE;
904
905         obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
906         bonuslen = (obj_type == DMU_OT_SA) ?
907             DN_BONUS_SIZE(dnodesize) : ZFS_OLD_ZNODE_PHYS_SIZE;
908
909         /*
910          * Create a new DMU object.
911          */
912         /*
913          * There's currently no mechanism for pre-reading the blocks that will
914          * be needed to allocate a new object, so we accept the small chance
915          * that there will be an i/o error and we will fail one of the
916          * assertions below.
917          */
918         if (vap->va_type == VDIR) {
919                 if (zfsvfs->z_replay) {
920                         VERIFY0(zap_create_claim_norm_dnsize(zfsvfs->z_os, obj,
921                             zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
922                             obj_type, bonuslen, dnodesize, tx));
923                 } else {
924                         obj = zap_create_norm_dnsize(zfsvfs->z_os,
925                             zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
926                             obj_type, bonuslen, dnodesize, tx);
927                 }
928         } else {
929                 if (zfsvfs->z_replay) {
930                         VERIFY0(dmu_object_claim_dnsize(zfsvfs->z_os, obj,
931                             DMU_OT_PLAIN_FILE_CONTENTS, 0,
932                             obj_type, bonuslen, dnodesize, tx));
933                 } else {
934                         obj = dmu_object_alloc_dnsize(zfsvfs->z_os,
935                             DMU_OT_PLAIN_FILE_CONTENTS, 0,
936                             obj_type, bonuslen, dnodesize, tx);
937                 }
938         }
939
940         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
941         VERIFY0(sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
942
943         /*
944          * If this is the root, fix up the half-initialized parent pointer
945          * to reference the just-allocated physical data area.
946          */
947         if (flag & IS_ROOT_NODE) {
948                 dzp->z_id = obj;
949         } else {
950                 dzp_pflags = dzp->z_pflags;
951         }
952
953         /*
954          * If parent is an xattr, so am I.
955          */
956         if (dzp_pflags & ZFS_XATTR) {
957                 flag |= IS_XATTR;
958         }
959
960         if (zfsvfs->z_use_fuids)
961                 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
962         else
963                 pflags = 0;
964
965         if (vap->va_type == VDIR) {
966                 size = 2;               /* contents ("." and "..") */
967                 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
968         } else {
969                 size = links = 0;
970         }
971
972         if (vap->va_type == VBLK || vap->va_type == VCHR) {
973                 rdev = zfs_expldev(vap->va_rdev);
974         }
975
976         parent = dzp->z_id;
977         mode = acl_ids->z_mode;
978         if (flag & IS_XATTR)
979                 pflags |= ZFS_XATTR;
980
981         /*
982          * No execs denied will be deterimed when zfs_mode_compute() is called.
983          */
984         pflags |= acl_ids->z_aclp->z_hints &
985             (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
986             ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
987
988         ZFS_TIME_ENCODE(&now, crtime);
989         ZFS_TIME_ENCODE(&now, ctime);
990
991         if (vap->va_mask & AT_ATIME) {
992                 ZFS_TIME_ENCODE(&vap->va_atime, atime);
993         } else {
994                 ZFS_TIME_ENCODE(&now, atime);
995         }
996
997         if (vap->va_mask & AT_MTIME) {
998                 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
999         } else {
1000                 ZFS_TIME_ENCODE(&now, mtime);
1001         }
1002
1003         /* Now add in all of the "SA" attributes */
1004         VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
1005             &sa_hdl));
1006
1007         /*
1008          * Setup the array of attributes to be replaced/set on the new file
1009          *
1010          * order for  DMU_OT_ZNODE is critical since it needs to be constructed
1011          * in the old znode_phys_t format.  Don't change this ordering
1012          */
1013         sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
1014
1015         if (obj_type == DMU_OT_ZNODE) {
1016                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
1017                     NULL, &atime, 16);
1018                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
1019                     NULL, &mtime, 16);
1020                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
1021                     NULL, &ctime, 16);
1022                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
1023                     NULL, &crtime, 16);
1024                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
1025                     NULL, &gen, 8);
1026                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
1027                     NULL, &mode, 8);
1028                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
1029                     NULL, &size, 8);
1030                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
1031                     NULL, &parent, 8);
1032         } else {
1033                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
1034                     NULL, &mode, 8);
1035                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
1036                     NULL, &size, 8);
1037                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
1038                     NULL, &gen, 8);
1039                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs),
1040                     NULL, &acl_ids->z_fuid, 8);
1041                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs),
1042                     NULL, &acl_ids->z_fgid, 8);
1043                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
1044                     NULL, &parent, 8);
1045                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
1046                     NULL, &pflags, 8);
1047                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
1048                     NULL, &atime, 16);
1049                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
1050                     NULL, &mtime, 16);
1051                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
1052                     NULL, &ctime, 16);
1053                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
1054                     NULL, &crtime, 16);
1055         }
1056
1057         SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
1058
1059         if (obj_type == DMU_OT_ZNODE) {
1060                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
1061                     &empty_xattr, 8);
1062         }
1063         if (obj_type == DMU_OT_ZNODE ||
1064             (vap->va_type == VBLK || vap->va_type == VCHR)) {
1065                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
1066                     NULL, &rdev, 8);
1067
1068         }
1069         if (obj_type == DMU_OT_ZNODE) {
1070                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
1071                     NULL, &pflags, 8);
1072                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
1073                     &acl_ids->z_fuid, 8);
1074                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
1075                     &acl_ids->z_fgid, 8);
1076                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
1077                     sizeof (uint64_t) * 4);
1078                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
1079                     &acl_phys, sizeof (zfs_acl_phys_t));
1080         } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
1081                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
1082                     &acl_ids->z_aclp->z_acl_count, 8);
1083                 locate.cb_aclp = acl_ids->z_aclp;
1084                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
1085                     zfs_acl_data_locator, &locate,
1086                     acl_ids->z_aclp->z_acl_bytes);
1087                 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
1088                     acl_ids->z_fuid, acl_ids->z_fgid);
1089         }
1090
1091         VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
1092
1093         if (!(flag & IS_ROOT_NODE)) {
1094                 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
1095                 ASSERT(*zpp != NULL);
1096         } else {
1097                 /*
1098                  * If we are creating the root node, the "parent" we
1099                  * passed in is the znode for the root.
1100                  */
1101                 *zpp = dzp;
1102
1103                 (*zpp)->z_sa_hdl = sa_hdl;
1104         }
1105
1106         (*zpp)->z_pflags = pflags;
1107         (*zpp)->z_mode = mode;
1108         (*zpp)->z_dnodesize = dnodesize;
1109
1110         if (vap->va_mask & AT_XVATTR)
1111                 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
1112
1113         if (obj_type == DMU_OT_ZNODE ||
1114             acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
1115                 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
1116         }
1117         if (!(flag & IS_ROOT_NODE)) {
1118                 vnode_t *vp;
1119
1120                 vp = ZTOV(*zpp);
1121                 vp->v_vflag |= VV_FORCEINSMQ;
1122                 err = insmntque(vp, zfsvfs->z_vfs);
1123                 vp->v_vflag &= ~VV_FORCEINSMQ;
1124                 KASSERT(err == 0, ("insmntque() failed: error %d", err));
1125         }
1126         kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
1127         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1128 }
1129
1130 /*
1131  * Update in-core attributes.  It is assumed the caller will be doing an
1132  * sa_bulk_update to push the changes out.
1133  */
1134 void
1135 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1136 {
1137         xoptattr_t *xoap;
1138
1139         xoap = xva_getxoptattr(xvap);
1140         ASSERT(xoap);
1141
1142         ASSERT_VOP_IN_SEQC(ZTOV(zp));
1143
1144         if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1145                 uint64_t times[2];
1146                 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1147                 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1148                     &times, sizeof (times), tx);
1149                 XVA_SET_RTN(xvap, XAT_CREATETIME);
1150         }
1151         if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1152                 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1153                     zp->z_pflags, tx);
1154                 XVA_SET_RTN(xvap, XAT_READONLY);
1155         }
1156         if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
1157                 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
1158                     zp->z_pflags, tx);
1159                 XVA_SET_RTN(xvap, XAT_HIDDEN);
1160         }
1161         if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
1162                 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
1163                     zp->z_pflags, tx);
1164                 XVA_SET_RTN(xvap, XAT_SYSTEM);
1165         }
1166         if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
1167                 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
1168                     zp->z_pflags, tx);
1169                 XVA_SET_RTN(xvap, XAT_ARCHIVE);
1170         }
1171         if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1172                 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1173                     zp->z_pflags, tx);
1174                 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1175         }
1176         if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1177                 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1178                     zp->z_pflags, tx);
1179                 XVA_SET_RTN(xvap, XAT_NOUNLINK);
1180         }
1181         if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1182                 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1183                     zp->z_pflags, tx);
1184                 XVA_SET_RTN(xvap, XAT_APPENDONLY);
1185         }
1186         if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1187                 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1188                     zp->z_pflags, tx);
1189                 XVA_SET_RTN(xvap, XAT_NODUMP);
1190         }
1191         if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1192                 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1193                     zp->z_pflags, tx);
1194                 XVA_SET_RTN(xvap, XAT_OPAQUE);
1195         }
1196         if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1197                 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1198                     xoap->xoa_av_quarantined, zp->z_pflags, tx);
1199                 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1200         }
1201         if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1202                 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1203                     zp->z_pflags, tx);
1204                 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1205         }
1206         if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1207                 zfs_sa_set_scanstamp(zp, xvap, tx);
1208                 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1209         }
1210         if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1211                 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1212                     zp->z_pflags, tx);
1213                 XVA_SET_RTN(xvap, XAT_REPARSE);
1214         }
1215         if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1216                 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1217                     zp->z_pflags, tx);
1218                 XVA_SET_RTN(xvap, XAT_OFFLINE);
1219         }
1220         if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1221                 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1222                     zp->z_pflags, tx);
1223                 XVA_SET_RTN(xvap, XAT_SPARSE);
1224         }
1225 }
1226
1227 int
1228 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1229 {
1230         dmu_object_info_t doi;
1231         dmu_buf_t       *db;
1232         znode_t         *zp;
1233         vnode_t         *vp;
1234         sa_handle_t     *hdl;
1235         struct thread   *td;
1236         int locked;
1237         int err;
1238
1239         td = curthread;
1240         getnewvnode_reserve();
1241 again:
1242         *zpp = NULL;
1243         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1244
1245         err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1246         if (err) {
1247                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1248                 getnewvnode_drop_reserve();
1249                 return (err);
1250         }
1251
1252         dmu_object_info_from_db(db, &doi);
1253         if (doi.doi_bonus_type != DMU_OT_SA &&
1254             (doi.doi_bonus_type != DMU_OT_ZNODE ||
1255             (doi.doi_bonus_type == DMU_OT_ZNODE &&
1256             doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1257                 sa_buf_rele(db, NULL);
1258                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1259 #ifdef __FreeBSD__
1260                 getnewvnode_drop_reserve();
1261 #endif
1262                 return (SET_ERROR(EINVAL));
1263         }
1264
1265         hdl = dmu_buf_get_user(db);
1266         if (hdl != NULL) {
1267                 zp  = sa_get_userdata(hdl);
1268
1269                 /*
1270                  * Since "SA" does immediate eviction we
1271                  * should never find a sa handle that doesn't
1272                  * know about the znode.
1273                  */
1274                 ASSERT3P(zp, !=, NULL);
1275                 ASSERT3U(zp->z_id, ==, obj_num);
1276                 if (zp->z_unlinked) {
1277                         err = SET_ERROR(ENOENT);
1278                 } else {
1279                         vp = ZTOV(zp);
1280                         /*
1281                          * Don't let the vnode disappear after
1282                          * ZFS_OBJ_HOLD_EXIT.
1283                          */
1284                         VN_HOLD(vp);
1285                         *zpp = zp;
1286                         err = 0;
1287                 }
1288
1289                 sa_buf_rele(db, NULL);
1290                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1291
1292                 if (err) {
1293                         getnewvnode_drop_reserve();
1294                         return (err);
1295                 }
1296
1297                 locked = VOP_ISLOCKED(vp);
1298                 VI_LOCK(vp);
1299                 if (VN_IS_DOOMED(vp) && locked != LK_EXCLUSIVE) {
1300                         /*
1301                          * The vnode is doomed and this thread doesn't
1302                          * hold the exclusive lock on it, so the vnode
1303                          * must be being reclaimed by another thread.
1304                          * Otherwise the doomed vnode is being reclaimed
1305                          * by this thread and zfs_zget is called from
1306                          * ZIL internals.
1307                          */
1308                         VI_UNLOCK(vp);
1309
1310                         /*
1311                          * XXX vrele() locks the vnode when the last reference
1312                          * is dropped.  Although in this case the vnode is
1313                          * doomed / dead and so no inactivation is required,
1314                          * the vnode lock is still acquired.  That could result
1315                          * in a LOR with z_teardown_lock if another thread holds
1316                          * the vnode's lock and tries to take z_teardown_lock.
1317                          * But that is only possible if the other thread peforms
1318                          * a ZFS vnode operation on the vnode.  That either
1319                          * should not happen if the vnode is dead or the thread
1320                          * should also have a refrence to the vnode and thus
1321                          * our reference is not last.
1322                          */
1323                         VN_RELE(vp);
1324                         goto again;
1325                 }
1326                 VI_UNLOCK(vp);
1327                 getnewvnode_drop_reserve();
1328                 return (err);
1329         }
1330
1331         /*
1332          * Not found create new znode/vnode
1333          * but only if file exists.
1334          *
1335          * There is a small window where zfs_vget() could
1336          * find this object while a file create is still in
1337          * progress.  This is checked for in zfs_znode_alloc()
1338          *
1339          * if zfs_znode_alloc() fails it will drop the hold on the
1340          * bonus buffer.
1341          */
1342         zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1343             doi.doi_bonus_type, NULL);
1344         if (zp == NULL) {
1345                 err = SET_ERROR(ENOENT);
1346         } else {
1347                 *zpp = zp;
1348         }
1349         if (err == 0) {
1350                 vnode_t *vp = ZTOV(zp);
1351
1352                 err = insmntque(vp, zfsvfs->z_vfs);
1353                 if (err == 0) {
1354                         vp->v_hash = obj_num;
1355                         VOP_UNLOCK(vp);
1356                 } else {
1357                         zp->z_vnode = NULL;
1358                         zfs_znode_dmu_fini(zp);
1359                         zfs_znode_free(zp);
1360                         *zpp = NULL;
1361                 }
1362         }
1363         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1364         getnewvnode_drop_reserve();
1365         return (err);
1366 }
1367
1368 int
1369 zfs_rezget(znode_t *zp)
1370 {
1371         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1372         dmu_object_info_t doi;
1373         dmu_buf_t *db;
1374         vnode_t *vp;
1375         uint64_t obj_num = zp->z_id;
1376         uint64_t mode, size;
1377         sa_bulk_attr_t bulk[8];
1378         int err;
1379         int count = 0;
1380         uint64_t gen;
1381
1382         /*
1383          * Remove cached pages before reloading the znode, so that they are not
1384          * lingering after we run into any error.  Ideally, we should vgone()
1385          * the vnode in case of error, but currently we cannot do that
1386          * because of the LOR between the vnode lock and z_teardown_lock.
1387          * So, instead, we have to "doom" the znode in the illumos style.
1388          */
1389         vp = ZTOV(zp);
1390         vn_pages_remove(vp, 0, 0);
1391
1392         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1393
1394         mutex_enter(&zp->z_acl_lock);
1395         if (zp->z_acl_cached) {
1396                 zfs_acl_free(zp->z_acl_cached);
1397                 zp->z_acl_cached = NULL;
1398         }
1399
1400         mutex_exit(&zp->z_acl_lock);
1401         ASSERT(zp->z_sa_hdl == NULL);
1402         err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1403         if (err) {
1404                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1405                 return (err);
1406         }
1407
1408         dmu_object_info_from_db(db, &doi);
1409         if (doi.doi_bonus_type != DMU_OT_SA &&
1410             (doi.doi_bonus_type != DMU_OT_ZNODE ||
1411             (doi.doi_bonus_type == DMU_OT_ZNODE &&
1412             doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1413                 sa_buf_rele(db, NULL);
1414                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1415                 return (SET_ERROR(EINVAL));
1416         }
1417
1418         zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1419         size = zp->z_size;
1420
1421         /* reload cached values */
1422         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1423             &gen, sizeof (gen));
1424         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1425             &zp->z_size, sizeof (zp->z_size));
1426         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1427             &zp->z_links, sizeof (zp->z_links));
1428         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1429             &zp->z_pflags, sizeof (zp->z_pflags));
1430         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1431             &zp->z_atime, sizeof (zp->z_atime));
1432         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1433             &zp->z_uid, sizeof (zp->z_uid));
1434         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1435             &zp->z_gid, sizeof (zp->z_gid));
1436         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1437             &mode, sizeof (mode));
1438
1439         if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1440                 zfs_znode_dmu_fini(zp);
1441                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1442                 return (SET_ERROR(EIO));
1443         }
1444
1445         zp->z_mode = mode;
1446
1447         if (gen != zp->z_gen) {
1448                 zfs_znode_dmu_fini(zp);
1449                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1450                 return (SET_ERROR(EIO));
1451         }
1452
1453         /*
1454          * It is highly improbable but still quite possible that two
1455          * objects in different datasets are created with the same
1456          * object numbers and in transaction groups with the same
1457          * numbers.  znodes corresponding to those objects would
1458          * have the same z_id and z_gen, but their other attributes
1459          * may be different.
1460          * zfs recv -F may replace one of such objects with the other.
1461          * As a result file properties recorded in the replaced
1462          * object's vnode may no longer match the received object's
1463          * properties.  At present the only cached property is the
1464          * files type recorded in v_type.
1465          * So, handle this case by leaving the old vnode and znode
1466          * disassociated from the actual object.  A new vnode and a
1467          * znode will be created if the object is accessed
1468          * (e.g. via a look-up).  The old vnode and znode will be
1469          * recycled when the last vnode reference is dropped.
1470          */
1471         if (vp->v_type != IFTOVT((mode_t)zp->z_mode)) {
1472                 zfs_znode_dmu_fini(zp);
1473                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1474                 return (SET_ERROR(EIO));
1475         }
1476
1477         /*
1478          * If the file has zero links, then it has been unlinked on the send
1479          * side and it must be in the received unlinked set.
1480          * We call zfs_znode_dmu_fini() now to prevent any accesses to the
1481          * stale data and to prevent automatical removal of the file in
1482          * zfs_zinactive().  The file will be removed either when it is removed
1483          * on the send side and the next incremental stream is received or
1484          * when the unlinked set gets processed.
1485          */
1486         zp->z_unlinked = (zp->z_links == 0);
1487         if (zp->z_unlinked) {
1488                 zfs_znode_dmu_fini(zp);
1489                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1490                 return (0);
1491         }
1492
1493         zp->z_blksz = doi.doi_data_block_size;
1494         if (zp->z_size != size)
1495                 vnode_pager_setsize(vp, zp->z_size);
1496
1497         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1498
1499         return (0);
1500 }
1501
1502 void
1503 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1504 {
1505         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1506         objset_t *os = zfsvfs->z_os;
1507         uint64_t obj = zp->z_id;
1508         uint64_t acl_obj = zfs_external_acl(zp);
1509
1510         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1511         if (acl_obj) {
1512                 VERIFY(!zp->z_is_sa);
1513                 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1514         }
1515         VERIFY(0 == dmu_object_free(os, obj, tx));
1516         zfs_znode_dmu_fini(zp);
1517         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1518         zfs_znode_free(zp);
1519 }
1520
1521 void
1522 zfs_zinactive(znode_t *zp)
1523 {
1524         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1525         uint64_t z_id = zp->z_id;
1526
1527         ASSERT(zp->z_sa_hdl);
1528
1529         /*
1530          * Don't allow a zfs_zget() while were trying to release this znode
1531          */
1532         ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1533
1534         /*
1535          * If this was the last reference to a file with no links, remove
1536          * the file from the file system unless the file system is mounted
1537          * read-only.  That can happen, for example, if the file system was
1538          * originally read-write, the file was opened, then unlinked and
1539          * the file system was made read-only before the file was finally
1540          * closed.  The file will remain in the unlinked set.
1541          */
1542         if (zp->z_unlinked) {
1543                 ASSERT(!zfsvfs->z_issnap);
1544                 if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0) {
1545                         ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1546                         zfs_rmnode(zp);
1547                         return;
1548                 }
1549         }
1550
1551         zfs_znode_dmu_fini(zp);
1552         ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1553         zfs_znode_free(zp);
1554 }
1555
1556 void
1557 zfs_znode_free(znode_t *zp)
1558 {
1559         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1560
1561         ASSERT(zp->z_sa_hdl == NULL);
1562         zp->z_vnode = NULL;
1563         mutex_enter(&zfsvfs->z_znodes_lock);
1564         POINTER_INVALIDATE(&zp->z_zfsvfs);
1565         list_remove(&zfsvfs->z_all_znodes, zp);
1566         mutex_exit(&zfsvfs->z_znodes_lock);
1567
1568         if (zp->z_acl_cached) {
1569                 zfs_acl_free(zp->z_acl_cached);
1570                 zp->z_acl_cached = NULL;
1571         }
1572
1573         zfs_znode_free_kmem(zp);
1574
1575 #ifdef illumos
1576         VFS_RELE(zfsvfs->z_vfs);
1577 #endif
1578 }
1579
1580 void
1581 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1582     uint64_t ctime[2], boolean_t have_tx)
1583 {
1584         timestruc_t     now;
1585
1586         vfs_timestamp(&now);
1587
1588         if (have_tx) {  /* will sa_bulk_update happen really soon? */
1589                 zp->z_atime_dirty = 0;
1590                 zp->z_seq++;
1591         } else {
1592                 zp->z_atime_dirty = 1;
1593         }
1594
1595         if (flag & AT_ATIME) {
1596                 ZFS_TIME_ENCODE(&now, zp->z_atime);
1597         }
1598
1599         if (flag & AT_MTIME) {
1600                 ZFS_TIME_ENCODE(&now, mtime);
1601                 if (zp->z_zfsvfs->z_use_fuids) {
1602                         zp->z_pflags |= (ZFS_ARCHIVE |
1603                             ZFS_AV_MODIFIED);
1604                 }
1605         }
1606
1607         if (flag & AT_CTIME) {
1608                 ZFS_TIME_ENCODE(&now, ctime);
1609                 if (zp->z_zfsvfs->z_use_fuids)
1610                         zp->z_pflags |= ZFS_ARCHIVE;
1611         }
1612 }
1613
1614 /*
1615  * Grow the block size for a file.
1616  *
1617  *      IN:     zp      - znode of file to free data in.
1618  *              size    - requested block size
1619  *              tx      - open transaction.
1620  *
1621  * NOTE: this function assumes that the znode is write locked.
1622  */
1623 void
1624 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1625 {
1626         int             error;
1627         u_longlong_t    dummy;
1628
1629         if (size <= zp->z_blksz)
1630                 return;
1631         /*
1632          * If the file size is already greater than the current blocksize,
1633          * we will not grow.  If there is more than one block in a file,
1634          * the blocksize cannot change.
1635          */
1636         if (zp->z_blksz && zp->z_size > zp->z_blksz)
1637                 return;
1638
1639         error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1640             size, 0, tx);
1641
1642         if (error == ENOTSUP)
1643                 return;
1644         ASSERT0(error);
1645
1646         /* What blocksize did we actually get? */
1647         dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1648 }
1649
1650 #ifdef illumos
1651 /*
1652  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1653  * be calling back into the fs for a putpage().  E.g.: when truncating
1654  * a file, the pages being "thrown away* don't need to be written out.
1655  */
1656 /* ARGSUSED */
1657 static int
1658 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1659     int flags, cred_t *cr)
1660 {
1661         ASSERT(0);
1662         return (0);
1663 }
1664 #endif
1665
1666 /*
1667  * Increase the file length
1668  *
1669  *      IN:     zp      - znode of file to free data in.
1670  *              end     - new end-of-file
1671  *
1672  *      RETURN: 0 on success, error code on failure
1673  */
1674 static int
1675 zfs_extend(znode_t *zp, uint64_t end)
1676 {
1677         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1678         dmu_tx_t *tx;
1679         locked_range_t *lr;
1680         uint64_t newblksz;
1681         int error;
1682
1683         /*
1684          * We will change zp_size, lock the whole file.
1685          */
1686         lr = rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
1687
1688         /*
1689          * Nothing to do if file already at desired length.
1690          */
1691         if (end <= zp->z_size) {
1692                 rangelock_exit(lr);
1693                 return (0);
1694         }
1695         tx = dmu_tx_create(zfsvfs->z_os);
1696         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1697         zfs_sa_upgrade_txholds(tx, zp);
1698         if (end > zp->z_blksz &&
1699             (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1700                 /*
1701                  * We are growing the file past the current block size.
1702                  */
1703                 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1704                         /*
1705                          * File's blocksize is already larger than the
1706                          * "recordsize" property.  Only let it grow to
1707                          * the next power of 2.
1708                          */
1709                         ASSERT(!ISP2(zp->z_blksz));
1710                         newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1711                 } else {
1712                         newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1713                 }
1714                 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1715         } else {
1716                 newblksz = 0;
1717         }
1718
1719         error = dmu_tx_assign(tx, TXG_WAIT);
1720         if (error) {
1721                 dmu_tx_abort(tx);
1722                 rangelock_exit(lr);
1723                 return (error);
1724         }
1725
1726         if (newblksz)
1727                 zfs_grow_blocksize(zp, newblksz, tx);
1728
1729         zp->z_size = end;
1730
1731         VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1732             &zp->z_size, sizeof (zp->z_size), tx));
1733
1734         vnode_pager_setsize(ZTOV(zp), end);
1735
1736         rangelock_exit(lr);
1737
1738         dmu_tx_commit(tx);
1739
1740         return (0);
1741 }
1742
1743 /*
1744  * Free space in a file.
1745  *
1746  *      IN:     zp      - znode of file to free data in.
1747  *              off     - start of section to free.
1748  *              len     - length of section to free.
1749  *
1750  *      RETURN: 0 on success, error code on failure
1751  */
1752 static int
1753 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1754 {
1755         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1756         locked_range_t *lr;
1757         int error;
1758
1759         /*
1760          * Lock the range being freed.
1761          */
1762         lr = rangelock_enter(&zp->z_rangelock, off, len, RL_WRITER);
1763
1764         /*
1765          * Nothing to do if file already at desired length.
1766          */
1767         if (off >= zp->z_size) {
1768                 rangelock_exit(lr);
1769                 return (0);
1770         }
1771
1772         if (off + len > zp->z_size)
1773                 len = zp->z_size - off;
1774
1775         error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1776
1777         if (error == 0) {
1778                 /*
1779                  * In FreeBSD we cannot free block in the middle of a file,
1780                  * but only at the end of a file, so this code path should
1781                  * never happen.
1782                  */
1783                 vnode_pager_setsize(ZTOV(zp), off);
1784         }
1785
1786         rangelock_exit(lr);
1787
1788         return (error);
1789 }
1790
1791 /*
1792  * Truncate a file
1793  *
1794  *      IN:     zp      - znode of file to free data in.
1795  *              end     - new end-of-file.
1796  *
1797  *      RETURN: 0 on success, error code on failure
1798  */
1799 static int
1800 zfs_trunc(znode_t *zp, uint64_t end)
1801 {
1802         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1803         vnode_t *vp = ZTOV(zp);
1804         dmu_tx_t *tx;
1805         locked_range_t *lr;
1806         int error;
1807         sa_bulk_attr_t bulk[2];
1808         int count = 0;
1809
1810         /*
1811          * We will change zp_size, lock the whole file.
1812          */
1813         lr = rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
1814
1815         /*
1816          * Nothing to do if file already at desired length.
1817          */
1818         if (end >= zp->z_size) {
1819                 rangelock_exit(lr);
1820                 return (0);
1821         }
1822
1823         error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,
1824             DMU_OBJECT_END);
1825         if (error) {
1826                 rangelock_exit(lr);
1827                 return (error);
1828         }
1829         tx = dmu_tx_create(zfsvfs->z_os);
1830         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1831         zfs_sa_upgrade_txholds(tx, zp);
1832         dmu_tx_mark_netfree(tx);
1833         error = dmu_tx_assign(tx, TXG_WAIT);
1834         if (error) {
1835                 dmu_tx_abort(tx);
1836                 rangelock_exit(lr);
1837                 return (error);
1838         }
1839
1840         zp->z_size = end;
1841         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1842             NULL, &zp->z_size, sizeof (zp->z_size));
1843
1844         if (end == 0) {
1845                 zp->z_pflags &= ~ZFS_SPARSE;
1846                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1847                     NULL, &zp->z_pflags, 8);
1848         }
1849         VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1850
1851         dmu_tx_commit(tx);
1852
1853         /*
1854          * Clear any mapped pages in the truncated region.  This has to
1855          * happen outside of the transaction to avoid the possibility of
1856          * a deadlock with someone trying to push a page that we are
1857          * about to invalidate.
1858          */
1859         vnode_pager_setsize(vp, end);
1860
1861         rangelock_exit(lr);
1862
1863         return (0);
1864 }
1865
1866 /*
1867  * Free space in a file
1868  *
1869  *      IN:     zp      - znode of file to free data in.
1870  *              off     - start of range
1871  *              len     - end of range (0 => EOF)
1872  *              flag    - current file open mode flags.
1873  *              log     - TRUE if this action should be logged
1874  *
1875  *      RETURN: 0 on success, error code on failure
1876  */
1877 int
1878 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1879 {
1880         vnode_t *vp = ZTOV(zp);
1881         dmu_tx_t *tx;
1882         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1883         zilog_t *zilog = zfsvfs->z_log;
1884         uint64_t mode;
1885         uint64_t mtime[2], ctime[2];
1886         sa_bulk_attr_t bulk[3];
1887         int count = 0;
1888         int error;
1889
1890         if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1891             sizeof (mode))) != 0)
1892                 return (error);
1893
1894         if (off > zp->z_size) {
1895                 error =  zfs_extend(zp, off+len);
1896                 if (error == 0 && log)
1897                         goto log;
1898                 else
1899                         return (error);
1900         }
1901
1902         /*
1903          * Check for any locks in the region to be freed.
1904          */
1905
1906         if (MANDLOCK(vp, (mode_t)mode)) {
1907                 uint64_t length = (len ? len : zp->z_size - off);
1908                 if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1909                         return (error);
1910         }
1911
1912         if (len == 0) {
1913                 error = zfs_trunc(zp, off);
1914         } else {
1915                 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1916                     off + len > zp->z_size)
1917                         error = zfs_extend(zp, off+len);
1918         }
1919         if (error || !log)
1920                 return (error);
1921 log:
1922         tx = dmu_tx_create(zfsvfs->z_os);
1923         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1924         zfs_sa_upgrade_txholds(tx, zp);
1925         error = dmu_tx_assign(tx, TXG_WAIT);
1926         if (error) {
1927                 dmu_tx_abort(tx);
1928                 return (error);
1929         }
1930
1931         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1932         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1933         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1934             NULL, &zp->z_pflags, 8);
1935         zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1936         error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1937         ASSERT(error == 0);
1938
1939         zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1940
1941         dmu_tx_commit(tx);
1942         return (0);
1943 }
1944
1945 void
1946 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1947 {
1948         uint64_t        moid, obj, sa_obj, version;
1949         uint64_t        sense = ZFS_CASE_SENSITIVE;
1950         uint64_t        norm = 0;
1951         nvpair_t        *elem;
1952         int             error;
1953         int             i;
1954         znode_t         *rootzp = NULL;
1955         zfsvfs_t        *zfsvfs;
1956         vattr_t         vattr;
1957         znode_t         *zp;
1958         zfs_acl_ids_t   acl_ids;
1959
1960         /*
1961          * First attempt to create master node.
1962          */
1963         /*
1964          * In an empty objset, there are no blocks to read and thus
1965          * there can be no i/o errors (which we assert below).
1966          */
1967         moid = MASTER_NODE_OBJ;
1968         error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1969             DMU_OT_NONE, 0, tx);
1970         ASSERT(error == 0);
1971
1972         /*
1973          * Set starting attributes.
1974          */
1975         version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1976         elem = NULL;
1977         while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1978                 /* For the moment we expect all zpl props to be uint64_ts */
1979                 uint64_t val;
1980                 char *name;
1981
1982                 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1983                 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1984                 name = nvpair_name(elem);
1985                 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1986                         if (val < version)
1987                                 version = val;
1988                 } else {
1989                         error = zap_update(os, moid, name, 8, 1, &val, tx);
1990                 }
1991                 ASSERT(error == 0);
1992                 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1993                         norm = val;
1994                 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1995                         sense = val;
1996         }
1997         ASSERT(version != 0);
1998         error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1999
2000         /*
2001          * Create zap object used for SA attribute registration
2002          */
2003
2004         if (version >= ZPL_VERSION_SA) {
2005                 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2006                     DMU_OT_NONE, 0, tx);
2007                 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2008                 ASSERT(error == 0);
2009         } else {
2010                 sa_obj = 0;
2011         }
2012         /*
2013          * Create a delete queue.
2014          */
2015         obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
2016
2017         error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
2018         ASSERT(error == 0);
2019
2020         /*
2021          * Create root znode.  Create minimal znode/vnode/zfsvfs
2022          * to allow zfs_mknode to work.
2023          */
2024         VATTR_NULL(&vattr);
2025         vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
2026         vattr.va_type = VDIR;
2027         vattr.va_mode = S_IFDIR|0755;
2028         vattr.va_uid = crgetuid(cr);
2029         vattr.va_gid = crgetgid(cr);
2030
2031         zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
2032
2033         rootzp = zfs_znode_alloc_kmem(KM_SLEEP);
2034         ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
2035         rootzp->z_moved = 0;
2036         rootzp->z_unlinked = 0;
2037         rootzp->z_atime_dirty = 0;
2038         rootzp->z_is_sa = USE_SA(version, os);
2039
2040         zfsvfs->z_os = os;
2041         zfsvfs->z_parent = zfsvfs;
2042         zfsvfs->z_version = version;
2043         zfsvfs->z_use_fuids = USE_FUIDS(version, os);
2044         zfsvfs->z_use_sa = USE_SA(version, os);
2045         zfsvfs->z_norm = norm;
2046
2047         error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
2048             &zfsvfs->z_attr_table);
2049
2050         ASSERT(error == 0);
2051
2052         /*
2053          * Fold case on file systems that are always or sometimes case
2054          * insensitive.
2055          */
2056         if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
2057                 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
2058
2059         mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
2060         list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
2061             offsetof(znode_t, z_link_node));
2062
2063         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
2064                 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
2065
2066         rootzp->z_zfsvfs = zfsvfs;
2067         VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
2068             cr, NULL, &acl_ids));
2069         zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
2070         ASSERT3P(zp, ==, rootzp);
2071         error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
2072         ASSERT(error == 0);
2073         zfs_acl_ids_free(&acl_ids);
2074         POINTER_INVALIDATE(&rootzp->z_zfsvfs);
2075
2076         sa_handle_destroy(rootzp->z_sa_hdl);
2077         zfs_znode_free_kmem(rootzp);
2078
2079         /*
2080          * Create shares directory
2081          */
2082
2083         error = zfs_create_share_dir(zfsvfs, tx);
2084
2085         ASSERT(error == 0);
2086
2087         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
2088                 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
2089         kmem_free(zfsvfs, sizeof (zfsvfs_t));
2090 }
2091 #endif /* _KERNEL */
2092
2093 static int
2094 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
2095 {
2096         uint64_t sa_obj = 0;
2097         int error;
2098
2099         error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
2100         if (error != 0 && error != ENOENT)
2101                 return (error);
2102
2103         error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
2104         return (error);
2105 }
2106
2107 static int
2108 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
2109     dmu_buf_t **db, void *tag)
2110 {
2111         dmu_object_info_t doi;
2112         int error;
2113
2114         if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
2115                 return (error);
2116
2117         dmu_object_info_from_db(*db, &doi);
2118         if ((doi.doi_bonus_type != DMU_OT_SA &&
2119             doi.doi_bonus_type != DMU_OT_ZNODE) ||
2120             doi.doi_bonus_type == DMU_OT_ZNODE &&
2121             doi.doi_bonus_size < sizeof (znode_phys_t)) {
2122                 sa_buf_rele(*db, tag);
2123                 return (SET_ERROR(ENOTSUP));
2124         }
2125
2126         error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
2127         if (error != 0) {
2128                 sa_buf_rele(*db, tag);
2129                 return (error);
2130         }
2131
2132         return (0);
2133 }
2134
2135 void
2136 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
2137 {
2138         sa_handle_destroy(hdl);
2139         sa_buf_rele(db, tag);
2140 }
2141
2142 /*
2143  * Given an object number, return its parent object number and whether
2144  * or not the object is an extended attribute directory.
2145  */
2146 static int
2147 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
2148     uint64_t *pobjp, int *is_xattrdir)
2149 {
2150         uint64_t parent;
2151         uint64_t pflags;
2152         uint64_t mode;
2153         uint64_t parent_mode;
2154         sa_bulk_attr_t bulk[3];
2155         sa_handle_t *sa_hdl;
2156         dmu_buf_t *sa_db;
2157         int count = 0;
2158         int error;
2159
2160         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
2161             &parent, sizeof (parent));
2162         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
2163             &pflags, sizeof (pflags));
2164         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2165             &mode, sizeof (mode));
2166
2167         if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
2168                 return (error);
2169
2170         /*
2171          * When a link is removed its parent pointer is not changed and will
2172          * be invalid.  There are two cases where a link is removed but the
2173          * file stays around, when it goes to the delete queue and when there
2174          * are additional links.
2175          */
2176         error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
2177         if (error != 0)
2178                 return (error);
2179
2180         error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
2181         zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2182         if (error != 0)
2183                 return (error);
2184
2185         *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
2186
2187         /*
2188          * Extended attributes can be applied to files, directories, etc.
2189          * Otherwise the parent must be a directory.
2190          */
2191         if (!*is_xattrdir && !S_ISDIR(parent_mode))
2192                 return (SET_ERROR(EINVAL));
2193
2194         *pobjp = parent;
2195
2196         return (0);
2197 }
2198
2199 /*
2200  * Given an object number, return some zpl level statistics
2201  */
2202 static int
2203 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2204     zfs_stat_t *sb)
2205 {
2206         sa_bulk_attr_t bulk[4];
2207         int count = 0;
2208
2209         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2210             &sb->zs_mode, sizeof (sb->zs_mode));
2211         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2212             &sb->zs_gen, sizeof (sb->zs_gen));
2213         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2214             &sb->zs_links, sizeof (sb->zs_links));
2215         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2216             &sb->zs_ctime, sizeof (sb->zs_ctime));
2217
2218         return (sa_bulk_lookup(hdl, bulk, count));
2219 }
2220
2221 static int
2222 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2223     sa_attr_type_t *sa_table, char *buf, int len)
2224 {
2225         sa_handle_t *sa_hdl;
2226         sa_handle_t *prevhdl = NULL;
2227         dmu_buf_t *prevdb = NULL;
2228         dmu_buf_t *sa_db = NULL;
2229         char *path = buf + len - 1;
2230         int error;
2231
2232         *path = '\0';
2233         sa_hdl = hdl;
2234
2235         uint64_t deleteq_obj;
2236         VERIFY0(zap_lookup(osp, MASTER_NODE_OBJ,
2237             ZFS_UNLINKED_SET, sizeof (uint64_t), 1, &deleteq_obj));
2238         error = zap_lookup_int(osp, deleteq_obj, obj);
2239         if (error == 0) {
2240                 return (ESTALE);
2241         } else if (error != ENOENT) {
2242                 return (error);
2243         }
2244         error = 0;
2245
2246         for (;;) {
2247                 uint64_t pobj;
2248                 char component[MAXNAMELEN + 2];
2249                 size_t complen;
2250                 int is_xattrdir;
2251
2252                 if (prevdb)
2253                         zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2254
2255                 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2256                     &is_xattrdir)) != 0)
2257                         break;
2258
2259                 if (pobj == obj) {
2260                         if (path[0] != '/')
2261                                 *--path = '/';
2262                         break;
2263                 }
2264
2265                 component[0] = '/';
2266                 if (is_xattrdir) {
2267                         (void) sprintf(component + 1, "<xattrdir>");
2268                 } else {
2269                         error = zap_value_search(osp, pobj, obj,
2270                             ZFS_DIRENT_OBJ(-1ULL), component + 1);
2271                         if (error != 0)
2272                                 break;
2273                 }
2274
2275                 complen = strlen(component);
2276                 path -= complen;
2277                 ASSERT(path >= buf);
2278                 bcopy(component, path, complen);
2279                 obj = pobj;
2280
2281                 if (sa_hdl != hdl) {
2282                         prevhdl = sa_hdl;
2283                         prevdb = sa_db;
2284                 }
2285                 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2286                 if (error != 0) {
2287                         sa_hdl = prevhdl;
2288                         sa_db = prevdb;
2289                         break;
2290                 }
2291         }
2292
2293         if (sa_hdl != NULL && sa_hdl != hdl) {
2294                 ASSERT(sa_db != NULL);
2295                 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2296         }
2297
2298         if (error == 0)
2299                 (void) memmove(buf, path, buf + len - path);
2300
2301         return (error);
2302 }
2303
2304 int
2305 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2306 {
2307         sa_attr_type_t *sa_table;
2308         sa_handle_t *hdl;
2309         dmu_buf_t *db;
2310         int error;
2311
2312         error = zfs_sa_setup(osp, &sa_table);
2313         if (error != 0)
2314                 return (error);
2315
2316         error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2317         if (error != 0)
2318                 return (error);
2319
2320         error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2321
2322         zfs_release_sa_handle(hdl, db, FTAG);
2323         return (error);
2324 }
2325
2326 int
2327 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2328     char *buf, int len)
2329 {
2330         char *path = buf + len - 1;
2331         sa_attr_type_t *sa_table;
2332         sa_handle_t *hdl;
2333         dmu_buf_t *db;
2334         int error;
2335
2336         *path = '\0';
2337
2338         error = zfs_sa_setup(osp, &sa_table);
2339         if (error != 0)
2340                 return (error);
2341
2342         error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2343         if (error != 0)
2344                 return (error);
2345
2346         error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2347         if (error != 0) {
2348                 zfs_release_sa_handle(hdl, db, FTAG);
2349                 return (error);
2350         }
2351
2352         error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2353
2354         zfs_release_sa_handle(hdl, db, FTAG);
2355         return (error);
2356 }
2357
2358 #ifdef _KERNEL
2359 int
2360 zfs_znode_parent_and_name(znode_t *zp, znode_t **dzpp, char *buf)
2361 {
2362         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2363         uint64_t parent;
2364         int is_xattrdir;
2365         int err;
2366
2367         /* Extended attributes should not be visible as regular files. */
2368         if ((zp->z_pflags & ZFS_XATTR) != 0)
2369                 return (SET_ERROR(EINVAL));
2370
2371         err = zfs_obj_to_pobj(zfsvfs->z_os, zp->z_sa_hdl, zfsvfs->z_attr_table,
2372             &parent, &is_xattrdir);
2373         if (err != 0)
2374                 return (err);
2375         ASSERT0(is_xattrdir);
2376
2377         /* No name as this is a root object. */
2378         if (parent == zp->z_id)
2379                 return (SET_ERROR(EINVAL));
2380
2381         err = zap_value_search(zfsvfs->z_os, parent, zp->z_id,
2382             ZFS_DIRENT_OBJ(-1ULL), buf);
2383         if (err != 0)
2384                 return (err);
2385         err = zfs_zget(zfsvfs, parent, dzpp);
2386         return (err);
2387 }
2388 #endif /* _KERNEL */