5008 lock contention (rrw_exit) while running a read only load
[unleashed.git] / usr / src / uts / common / fs / zfs / zfs_znode.c
blob7577250408dfe7534e6b726f43a62e02e355cb62
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
26 /* Portions Copyright 2007 Jeremy Teo */
28 #ifdef _KERNEL
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/mntent.h>
36 #include <sys/mkdev.h>
37 #include <sys/u8_textprep.h>
38 #include <sys/dsl_dataset.h>
39 #include <sys/vfs.h>
40 #include <sys/vfs_opreg.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/mode.h>
47 #include <sys/atomic.h>
48 #include <vm/pvn.h>
49 #include "fs/fs_subr.h"
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_acl.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/zfs_rlock.h>
54 #include <sys/zfs_fuid.h>
55 #include <sys/dnode.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/kidmap.h>
58 #endif /* _KERNEL */
60 #include <sys/dmu.h>
61 #include <sys/refcount.h>
62 #include <sys/stat.h>
63 #include <sys/zap.h>
64 #include <sys/zfs_znode.h>
65 #include <sys/sa.h>
66 #include <sys/zfs_sa.h>
67 #include <sys/zfs_stat.h>
69 #include "zfs_prop.h"
70 #include "zfs_comutil.h"
73 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
74 * turned on when DEBUG is also defined.
76 #ifdef DEBUG
77 #define ZNODE_STATS
78 #endif /* DEBUG */
80 #ifdef ZNODE_STATS
81 #define ZNODE_STAT_ADD(stat) ((stat)++)
82 #else
83 #define ZNODE_STAT_ADD(stat) /* nothing */
84 #endif /* ZNODE_STATS */
87 * Functions needed for userland (ie: libzpool) are not put under
88 * #ifdef_KERNEL; the rest of the functions have dependencies
89 * (such as VFS logic) that will not compile easily in userland.
91 #ifdef _KERNEL
93 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
94 * be freed before it can be safely accessed.
96 krwlock_t zfsvfs_lock;
98 static kmem_cache_t *znode_cache = NULL;
100 /*ARGSUSED*/
101 static void
102 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
105 * We should never drop all dbuf refs without first clearing
106 * the eviction callback.
108 panic("evicting znode %p\n", user_ptr);
111 /*ARGSUSED*/
112 static int
113 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
115 znode_t *zp = buf;
117 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
119 zp->z_vnode = vn_alloc(kmflags);
120 if (zp->z_vnode == NULL) {
121 return (-1);
123 ZTOV(zp)->v_data = zp;
125 list_link_init(&zp->z_link_node);
127 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
128 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
129 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
130 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
132 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
133 avl_create(&zp->z_range_avl, zfs_range_compare,
134 sizeof (rl_t), offsetof(rl_t, r_node));
136 zp->z_dirlocks = NULL;
137 zp->z_acl_cached = NULL;
138 zp->z_moved = 0;
139 return (0);
142 /*ARGSUSED*/
143 static void
144 zfs_znode_cache_destructor(void *buf, void *arg)
146 znode_t *zp = buf;
148 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
149 ASSERT(ZTOV(zp)->v_data == zp);
150 vn_free(ZTOV(zp));
151 ASSERT(!list_link_active(&zp->z_link_node));
152 mutex_destroy(&zp->z_lock);
153 rw_destroy(&zp->z_parent_lock);
154 rw_destroy(&zp->z_name_lock);
155 mutex_destroy(&zp->z_acl_lock);
156 avl_destroy(&zp->z_range_avl);
157 mutex_destroy(&zp->z_range_lock);
159 ASSERT(zp->z_dirlocks == NULL);
160 ASSERT(zp->z_acl_cached == NULL);
163 #ifdef ZNODE_STATS
164 static struct {
165 uint64_t zms_zfsvfs_invalid;
166 uint64_t zms_zfsvfs_recheck1;
167 uint64_t zms_zfsvfs_unmounted;
168 uint64_t zms_zfsvfs_recheck2;
169 uint64_t zms_obj_held;
170 uint64_t zms_vnode_locked;
171 uint64_t zms_not_only_dnlc;
172 } znode_move_stats;
173 #endif /* ZNODE_STATS */
175 static void
176 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
178 vnode_t *vp;
180 /* Copy fields. */
181 nzp->z_zfsvfs = ozp->z_zfsvfs;
183 /* Swap vnodes. */
184 vp = nzp->z_vnode;
185 nzp->z_vnode = ozp->z_vnode;
186 ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
187 ZTOV(ozp)->v_data = ozp;
188 ZTOV(nzp)->v_data = nzp;
190 nzp->z_id = ozp->z_id;
191 ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
192 ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
193 nzp->z_unlinked = ozp->z_unlinked;
194 nzp->z_atime_dirty = ozp->z_atime_dirty;
195 nzp->z_zn_prefetch = ozp->z_zn_prefetch;
196 nzp->z_blksz = ozp->z_blksz;
197 nzp->z_seq = ozp->z_seq;
198 nzp->z_mapcnt = ozp->z_mapcnt;
199 nzp->z_gen = ozp->z_gen;
200 nzp->z_sync_cnt = ozp->z_sync_cnt;
201 nzp->z_is_sa = ozp->z_is_sa;
202 nzp->z_sa_hdl = ozp->z_sa_hdl;
203 bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
204 nzp->z_links = ozp->z_links;
205 nzp->z_size = ozp->z_size;
206 nzp->z_pflags = ozp->z_pflags;
207 nzp->z_uid = ozp->z_uid;
208 nzp->z_gid = ozp->z_gid;
209 nzp->z_mode = ozp->z_mode;
212 * Since this is just an idle znode and kmem is already dealing with
213 * memory pressure, release any cached ACL.
215 if (ozp->z_acl_cached) {
216 zfs_acl_free(ozp->z_acl_cached);
217 ozp->z_acl_cached = NULL;
220 sa_set_userp(nzp->z_sa_hdl, nzp);
223 * Invalidate the original znode by clearing fields that provide a
224 * pointer back to the znode. Set the low bit of the vfs pointer to
225 * ensure that zfs_znode_move() recognizes the znode as invalid in any
226 * subsequent callback.
228 ozp->z_sa_hdl = NULL;
229 POINTER_INVALIDATE(&ozp->z_zfsvfs);
232 * Mark the znode.
234 nzp->z_moved = 1;
235 ozp->z_moved = (uint8_t)-1;
238 /*ARGSUSED*/
239 static kmem_cbrc_t
240 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
242 znode_t *ozp = buf, *nzp = newbuf;
243 zfsvfs_t *zfsvfs;
244 vnode_t *vp;
247 * The znode is on the file system's list of known znodes if the vfs
248 * pointer is valid. We set the low bit of the vfs pointer when freeing
249 * the znode to invalidate it, and the memory patterns written by kmem
250 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
251 * created znode sets the vfs pointer last of all to indicate that the
252 * znode is known and in a valid state to be moved by this function.
254 zfsvfs = ozp->z_zfsvfs;
255 if (!POINTER_IS_VALID(zfsvfs)) {
256 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
257 return (KMEM_CBRC_DONT_KNOW);
261 * Close a small window in which it's possible that the filesystem could
262 * be unmounted and freed, and zfsvfs, though valid in the previous
263 * statement, could point to unrelated memory by the time we try to
264 * prevent the filesystem from being unmounted.
266 rw_enter(&zfsvfs_lock, RW_WRITER);
267 if (zfsvfs != ozp->z_zfsvfs) {
268 rw_exit(&zfsvfs_lock);
269 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
270 return (KMEM_CBRC_DONT_KNOW);
274 * If the znode is still valid, then so is the file system. We know that
275 * no valid file system can be freed while we hold zfsvfs_lock, so we
276 * can safely ensure that the filesystem is not and will not be
277 * unmounted. The next statement is equivalent to ZFS_ENTER().
279 rrm_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
280 if (zfsvfs->z_unmounted) {
281 ZFS_EXIT(zfsvfs);
282 rw_exit(&zfsvfs_lock);
283 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
284 return (KMEM_CBRC_DONT_KNOW);
286 rw_exit(&zfsvfs_lock);
288 mutex_enter(&zfsvfs->z_znodes_lock);
290 * Recheck the vfs pointer in case the znode was removed just before
291 * acquiring the lock.
293 if (zfsvfs != ozp->z_zfsvfs) {
294 mutex_exit(&zfsvfs->z_znodes_lock);
295 ZFS_EXIT(zfsvfs);
296 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
297 return (KMEM_CBRC_DONT_KNOW);
301 * At this point we know that as long as we hold z_znodes_lock, the
302 * znode cannot be freed and fields within the znode can be safely
303 * accessed. Now, prevent a race with zfs_zget().
305 if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
306 mutex_exit(&zfsvfs->z_znodes_lock);
307 ZFS_EXIT(zfsvfs);
308 ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
309 return (KMEM_CBRC_LATER);
312 vp = ZTOV(ozp);
313 if (mutex_tryenter(&vp->v_lock) == 0) {
314 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
315 mutex_exit(&zfsvfs->z_znodes_lock);
316 ZFS_EXIT(zfsvfs);
317 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
318 return (KMEM_CBRC_LATER);
321 /* Only move znodes that are referenced _only_ by the DNLC. */
322 if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
323 mutex_exit(&vp->v_lock);
324 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
325 mutex_exit(&zfsvfs->z_znodes_lock);
326 ZFS_EXIT(zfsvfs);
327 ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
328 return (KMEM_CBRC_LATER);
332 * The znode is known and in a valid state to move. We're holding the
333 * locks needed to execute the critical section.
335 zfs_znode_move_impl(ozp, nzp);
336 mutex_exit(&vp->v_lock);
337 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
339 list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
340 mutex_exit(&zfsvfs->z_znodes_lock);
341 ZFS_EXIT(zfsvfs);
343 return (KMEM_CBRC_YES);
346 void
347 zfs_znode_init(void)
350 * Initialize zcache
352 rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
353 ASSERT(znode_cache == NULL);
354 znode_cache = kmem_cache_create("zfs_znode_cache",
355 sizeof (znode_t), 0, zfs_znode_cache_constructor,
356 zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
357 kmem_cache_set_move(znode_cache, zfs_znode_move);
360 void
361 zfs_znode_fini(void)
364 * Cleanup vfs & vnode ops
366 zfs_remove_op_tables();
369 * Cleanup zcache
371 if (znode_cache)
372 kmem_cache_destroy(znode_cache);
373 znode_cache = NULL;
374 rw_destroy(&zfsvfs_lock);
377 struct vnodeops *zfs_dvnodeops;
378 struct vnodeops *zfs_fvnodeops;
379 struct vnodeops *zfs_symvnodeops;
380 struct vnodeops *zfs_xdvnodeops;
381 struct vnodeops *zfs_evnodeops;
382 struct vnodeops *zfs_sharevnodeops;
384 void
385 zfs_remove_op_tables()
388 * Remove vfs ops
390 ASSERT(zfsfstype);
391 (void) vfs_freevfsops_by_type(zfsfstype);
392 zfsfstype = 0;
395 * Remove vnode ops
397 if (zfs_dvnodeops)
398 vn_freevnodeops(zfs_dvnodeops);
399 if (zfs_fvnodeops)
400 vn_freevnodeops(zfs_fvnodeops);
401 if (zfs_symvnodeops)
402 vn_freevnodeops(zfs_symvnodeops);
403 if (zfs_xdvnodeops)
404 vn_freevnodeops(zfs_xdvnodeops);
405 if (zfs_evnodeops)
406 vn_freevnodeops(zfs_evnodeops);
407 if (zfs_sharevnodeops)
408 vn_freevnodeops(zfs_sharevnodeops);
410 zfs_dvnodeops = NULL;
411 zfs_fvnodeops = NULL;
412 zfs_symvnodeops = NULL;
413 zfs_xdvnodeops = NULL;
414 zfs_evnodeops = NULL;
415 zfs_sharevnodeops = NULL;
418 extern const fs_operation_def_t zfs_dvnodeops_template[];
419 extern const fs_operation_def_t zfs_fvnodeops_template[];
420 extern const fs_operation_def_t zfs_xdvnodeops_template[];
421 extern const fs_operation_def_t zfs_symvnodeops_template[];
422 extern const fs_operation_def_t zfs_evnodeops_template[];
423 extern const fs_operation_def_t zfs_sharevnodeops_template[];
426 zfs_create_op_tables()
428 int error;
431 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
432 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
433 * In this case we just return as the ops vectors are already set up.
435 if (zfs_dvnodeops)
436 return (0);
438 error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
439 &zfs_dvnodeops);
440 if (error)
441 return (error);
443 error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
444 &zfs_fvnodeops);
445 if (error)
446 return (error);
448 error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
449 &zfs_symvnodeops);
450 if (error)
451 return (error);
453 error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
454 &zfs_xdvnodeops);
455 if (error)
456 return (error);
458 error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
459 &zfs_evnodeops);
460 if (error)
461 return (error);
463 error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
464 &zfs_sharevnodeops);
466 return (error);
470 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
472 zfs_acl_ids_t acl_ids;
473 vattr_t vattr;
474 znode_t *sharezp;
475 vnode_t *vp;
476 znode_t *zp;
477 int error;
479 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
480 vattr.va_type = VDIR;
481 vattr.va_mode = S_IFDIR|0555;
482 vattr.va_uid = crgetuid(kcred);
483 vattr.va_gid = crgetgid(kcred);
485 sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
486 ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
487 sharezp->z_moved = 0;
488 sharezp->z_unlinked = 0;
489 sharezp->z_atime_dirty = 0;
490 sharezp->z_zfsvfs = zfsvfs;
491 sharezp->z_is_sa = zfsvfs->z_use_sa;
493 vp = ZTOV(sharezp);
494 vn_reinit(vp);
495 vp->v_type = VDIR;
497 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
498 kcred, NULL, &acl_ids));
499 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
500 ASSERT3P(zp, ==, sharezp);
501 ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
502 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
503 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
504 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
505 zfsvfs->z_shares_dir = sharezp->z_id;
507 zfs_acl_ids_free(&acl_ids);
508 ZTOV(sharezp)->v_count = 0;
509 sa_handle_destroy(sharezp->z_sa_hdl);
510 kmem_cache_free(znode_cache, sharezp);
512 return (error);
516 * define a couple of values we need available
517 * for both 64 and 32 bit environments.
519 #ifndef NBITSMINOR64
520 #define NBITSMINOR64 32
521 #endif
522 #ifndef MAXMAJ64
523 #define MAXMAJ64 0xffffffffUL
524 #endif
525 #ifndef MAXMIN64
526 #define MAXMIN64 0xffffffffUL
527 #endif
530 * Create special expldev for ZFS private use.
531 * Can't use standard expldev since it doesn't do
532 * what we want. The standard expldev() takes a
533 * dev32_t in LP64 and expands it to a long dev_t.
534 * We need an interface that takes a dev32_t in ILP32
535 * and expands it to a long dev_t.
537 static uint64_t
538 zfs_expldev(dev_t dev)
540 #ifndef _LP64
541 major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
542 return (((uint64_t)major << NBITSMINOR64) |
543 ((minor_t)dev & MAXMIN32));
544 #else
545 return (dev);
546 #endif
550 * Special cmpldev for ZFS private use.
551 * Can't use standard cmpldev since it takes
552 * a long dev_t and compresses it to dev32_t in
553 * LP64. We need to do a compaction of a long dev_t
554 * to a dev32_t in ILP32.
556 dev_t
557 zfs_cmpldev(uint64_t dev)
559 #ifndef _LP64
560 minor_t minor = (minor_t)dev & MAXMIN64;
561 major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
563 if (major > MAXMAJ32 || minor > MAXMIN32)
564 return (NODEV32);
566 return (((dev32_t)major << NBITSMINOR32) | minor);
567 #else
568 return (dev);
569 #endif
572 static void
573 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
574 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
576 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
577 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
579 mutex_enter(&zp->z_lock);
581 ASSERT(zp->z_sa_hdl == NULL);
582 ASSERT(zp->z_acl_cached == NULL);
583 if (sa_hdl == NULL) {
584 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
585 SA_HDL_SHARED, &zp->z_sa_hdl));
586 } else {
587 zp->z_sa_hdl = sa_hdl;
588 sa_set_userp(sa_hdl, zp);
591 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
594 * Slap on VROOT if we are the root znode
596 if (zp->z_id == zfsvfs->z_root)
597 ZTOV(zp)->v_flag |= VROOT;
599 mutex_exit(&zp->z_lock);
600 vn_exists(ZTOV(zp));
603 void
604 zfs_znode_dmu_fini(znode_t *zp)
606 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
607 zp->z_unlinked ||
608 RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
610 sa_handle_destroy(zp->z_sa_hdl);
611 zp->z_sa_hdl = NULL;
615 * Construct a new znode/vnode and intialize.
617 * This does not do a call to dmu_set_user() that is
618 * up to the caller to do, in case you don't want to
619 * return the znode
621 static znode_t *
622 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
623 dmu_object_type_t obj_type, sa_handle_t *hdl)
625 znode_t *zp;
626 vnode_t *vp;
627 uint64_t mode;
628 uint64_t parent;
629 sa_bulk_attr_t bulk[9];
630 int count = 0;
632 zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
634 ASSERT(zp->z_dirlocks == NULL);
635 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
636 zp->z_moved = 0;
639 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
640 * the zfs_znode_move() callback.
642 zp->z_sa_hdl = NULL;
643 zp->z_unlinked = 0;
644 zp->z_atime_dirty = 0;
645 zp->z_mapcnt = 0;
646 zp->z_id = db->db_object;
647 zp->z_blksz = blksz;
648 zp->z_seq = 0x7A4653;
649 zp->z_sync_cnt = 0;
651 vp = ZTOV(zp);
652 vn_reinit(vp);
654 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
656 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
657 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
658 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
659 &zp->z_size, 8);
660 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
661 &zp->z_links, 8);
662 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
663 &zp->z_pflags, 8);
664 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
665 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
666 &zp->z_atime, 16);
667 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
668 &zp->z_uid, 8);
669 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
670 &zp->z_gid, 8);
672 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
673 if (hdl == NULL)
674 sa_handle_destroy(zp->z_sa_hdl);
675 kmem_cache_free(znode_cache, zp);
676 return (NULL);
679 zp->z_mode = mode;
680 vp->v_vfsp = zfsvfs->z_parent->z_vfs;
682 vp->v_type = IFTOVT((mode_t)mode);
684 switch (vp->v_type) {
685 case VDIR:
686 if (zp->z_pflags & ZFS_XATTR) {
687 vn_setops(vp, zfs_xdvnodeops);
688 vp->v_flag |= V_XATTRDIR;
689 } else {
690 vn_setops(vp, zfs_dvnodeops);
692 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
693 break;
694 case VBLK:
695 case VCHR:
697 uint64_t rdev;
698 VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
699 &rdev, sizeof (rdev)) == 0);
701 vp->v_rdev = zfs_cmpldev(rdev);
703 /*FALLTHROUGH*/
704 case VFIFO:
705 case VSOCK:
706 case VDOOR:
707 vn_setops(vp, zfs_fvnodeops);
708 break;
709 case VREG:
710 vp->v_flag |= VMODSORT;
711 if (parent == zfsvfs->z_shares_dir) {
712 ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
713 vn_setops(vp, zfs_sharevnodeops);
714 } else {
715 vn_setops(vp, zfs_fvnodeops);
717 break;
718 case VLNK:
719 vn_setops(vp, zfs_symvnodeops);
720 break;
721 default:
722 vn_setops(vp, zfs_evnodeops);
723 break;
726 mutex_enter(&zfsvfs->z_znodes_lock);
727 list_insert_tail(&zfsvfs->z_all_znodes, zp);
728 membar_producer();
730 * Everything else must be valid before assigning z_zfsvfs makes the
731 * znode eligible for zfs_znode_move().
733 zp->z_zfsvfs = zfsvfs;
734 mutex_exit(&zfsvfs->z_znodes_lock);
736 VFS_HOLD(zfsvfs->z_vfs);
737 return (zp);
740 static uint64_t empty_xattr;
741 static uint64_t pad[4];
742 static zfs_acl_phys_t acl_phys;
744 * Create a new DMU object to hold a zfs znode.
746 * IN: dzp - parent directory for new znode
747 * vap - file attributes for new znode
748 * tx - dmu transaction id for zap operations
749 * cr - credentials of caller
750 * flag - flags:
751 * IS_ROOT_NODE - new object will be root
752 * IS_XATTR - new object is an attribute
753 * bonuslen - length of bonus buffer
754 * setaclp - File/Dir initial ACL
755 * fuidp - Tracks fuid allocation.
757 * OUT: zpp - allocated znode
760 void
761 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
762 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
764 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
765 uint64_t mode, size, links, parent, pflags;
766 uint64_t dzp_pflags = 0;
767 uint64_t rdev = 0;
768 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
769 dmu_buf_t *db;
770 timestruc_t now;
771 uint64_t gen, obj;
772 int bonuslen;
773 sa_handle_t *sa_hdl;
774 dmu_object_type_t obj_type;
775 sa_bulk_attr_t sa_attrs[ZPL_END];
776 int cnt = 0;
777 zfs_acl_locator_cb_t locate = { 0 };
779 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
781 if (zfsvfs->z_replay) {
782 obj = vap->va_nodeid;
783 now = vap->va_ctime; /* see zfs_replay_create() */
784 gen = vap->va_nblocks; /* ditto */
785 } else {
786 obj = 0;
787 gethrestime(&now);
788 gen = dmu_tx_get_txg(tx);
791 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
792 bonuslen = (obj_type == DMU_OT_SA) ?
793 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
796 * Create a new DMU object.
799 * There's currently no mechanism for pre-reading the blocks that will
800 * be needed to allocate a new object, so we accept the small chance
801 * that there will be an i/o error and we will fail one of the
802 * assertions below.
804 if (vap->va_type == VDIR) {
805 if (zfsvfs->z_replay) {
806 VERIFY0(zap_create_claim_norm(zfsvfs->z_os, obj,
807 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
808 obj_type, bonuslen, tx));
809 } else {
810 obj = zap_create_norm(zfsvfs->z_os,
811 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
812 obj_type, bonuslen, tx);
814 } else {
815 if (zfsvfs->z_replay) {
816 VERIFY0(dmu_object_claim(zfsvfs->z_os, obj,
817 DMU_OT_PLAIN_FILE_CONTENTS, 0,
818 obj_type, bonuslen, tx));
819 } else {
820 obj = dmu_object_alloc(zfsvfs->z_os,
821 DMU_OT_PLAIN_FILE_CONTENTS, 0,
822 obj_type, bonuslen, tx);
826 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
827 VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
830 * If this is the root, fix up the half-initialized parent pointer
831 * to reference the just-allocated physical data area.
833 if (flag & IS_ROOT_NODE) {
834 dzp->z_id = obj;
835 } else {
836 dzp_pflags = dzp->z_pflags;
840 * If parent is an xattr, so am I.
842 if (dzp_pflags & ZFS_XATTR) {
843 flag |= IS_XATTR;
846 if (zfsvfs->z_use_fuids)
847 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
848 else
849 pflags = 0;
851 if (vap->va_type == VDIR) {
852 size = 2; /* contents ("." and "..") */
853 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
854 } else {
855 size = links = 0;
858 if (vap->va_type == VBLK || vap->va_type == VCHR) {
859 rdev = zfs_expldev(vap->va_rdev);
862 parent = dzp->z_id;
863 mode = acl_ids->z_mode;
864 if (flag & IS_XATTR)
865 pflags |= ZFS_XATTR;
868 * No execs denied will be deterimed when zfs_mode_compute() is called.
870 pflags |= acl_ids->z_aclp->z_hints &
871 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
872 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
874 ZFS_TIME_ENCODE(&now, crtime);
875 ZFS_TIME_ENCODE(&now, ctime);
877 if (vap->va_mask & AT_ATIME) {
878 ZFS_TIME_ENCODE(&vap->va_atime, atime);
879 } else {
880 ZFS_TIME_ENCODE(&now, atime);
883 if (vap->va_mask & AT_MTIME) {
884 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
885 } else {
886 ZFS_TIME_ENCODE(&now, mtime);
889 /* Now add in all of the "SA" attributes */
890 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
891 &sa_hdl));
894 * Setup the array of attributes to be replaced/set on the new file
896 * order for DMU_OT_ZNODE is critical since it needs to be constructed
897 * in the old znode_phys_t format. Don't change this ordering
900 if (obj_type == DMU_OT_ZNODE) {
901 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
902 NULL, &atime, 16);
903 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
904 NULL, &mtime, 16);
905 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
906 NULL, &ctime, 16);
907 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
908 NULL, &crtime, 16);
909 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
910 NULL, &gen, 8);
911 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
912 NULL, &mode, 8);
913 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
914 NULL, &size, 8);
915 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
916 NULL, &parent, 8);
917 } else {
918 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
919 NULL, &mode, 8);
920 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
921 NULL, &size, 8);
922 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
923 NULL, &gen, 8);
924 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
925 &acl_ids->z_fuid, 8);
926 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
927 &acl_ids->z_fgid, 8);
928 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
929 NULL, &parent, 8);
930 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
931 NULL, &pflags, 8);
932 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
933 NULL, &atime, 16);
934 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
935 NULL, &mtime, 16);
936 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
937 NULL, &ctime, 16);
938 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
939 NULL, &crtime, 16);
942 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
944 if (obj_type == DMU_OT_ZNODE) {
945 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
946 &empty_xattr, 8);
948 if (obj_type == DMU_OT_ZNODE ||
949 (vap->va_type == VBLK || vap->va_type == VCHR)) {
950 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
951 NULL, &rdev, 8);
954 if (obj_type == DMU_OT_ZNODE) {
955 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
956 NULL, &pflags, 8);
957 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
958 &acl_ids->z_fuid, 8);
959 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
960 &acl_ids->z_fgid, 8);
961 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
962 sizeof (uint64_t) * 4);
963 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
964 &acl_phys, sizeof (zfs_acl_phys_t));
965 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
966 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
967 &acl_ids->z_aclp->z_acl_count, 8);
968 locate.cb_aclp = acl_ids->z_aclp;
969 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
970 zfs_acl_data_locator, &locate,
971 acl_ids->z_aclp->z_acl_bytes);
972 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
973 acl_ids->z_fuid, acl_ids->z_fgid);
976 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
978 if (!(flag & IS_ROOT_NODE)) {
979 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
980 ASSERT(*zpp != NULL);
981 } else {
983 * If we are creating the root node, the "parent" we
984 * passed in is the znode for the root.
986 *zpp = dzp;
988 (*zpp)->z_sa_hdl = sa_hdl;
991 (*zpp)->z_pflags = pflags;
992 (*zpp)->z_mode = mode;
994 if (vap->va_mask & AT_XVATTR)
995 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
997 if (obj_type == DMU_OT_ZNODE ||
998 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
999 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
1001 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1005 * Update in-core attributes. It is assumed the caller will be doing an
1006 * sa_bulk_update to push the changes out.
1008 void
1009 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1011 xoptattr_t *xoap;
1013 xoap = xva_getxoptattr(xvap);
1014 ASSERT(xoap);
1016 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1017 uint64_t times[2];
1018 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1019 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1020 &times, sizeof (times), tx);
1021 XVA_SET_RTN(xvap, XAT_CREATETIME);
1023 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1024 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1025 zp->z_pflags, tx);
1026 XVA_SET_RTN(xvap, XAT_READONLY);
1028 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
1029 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
1030 zp->z_pflags, tx);
1031 XVA_SET_RTN(xvap, XAT_HIDDEN);
1033 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
1034 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
1035 zp->z_pflags, tx);
1036 XVA_SET_RTN(xvap, XAT_SYSTEM);
1038 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
1039 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
1040 zp->z_pflags, tx);
1041 XVA_SET_RTN(xvap, XAT_ARCHIVE);
1043 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1044 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1045 zp->z_pflags, tx);
1046 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1048 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1049 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1050 zp->z_pflags, tx);
1051 XVA_SET_RTN(xvap, XAT_NOUNLINK);
1053 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1054 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1055 zp->z_pflags, tx);
1056 XVA_SET_RTN(xvap, XAT_APPENDONLY);
1058 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1059 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1060 zp->z_pflags, tx);
1061 XVA_SET_RTN(xvap, XAT_NODUMP);
1063 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1064 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1065 zp->z_pflags, tx);
1066 XVA_SET_RTN(xvap, XAT_OPAQUE);
1068 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1069 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1070 xoap->xoa_av_quarantined, zp->z_pflags, tx);
1071 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1073 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1074 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1075 zp->z_pflags, tx);
1076 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1078 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1079 zfs_sa_set_scanstamp(zp, xvap, tx);
1080 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1082 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1083 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1084 zp->z_pflags, tx);
1085 XVA_SET_RTN(xvap, XAT_REPARSE);
1087 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1088 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1089 zp->z_pflags, tx);
1090 XVA_SET_RTN(xvap, XAT_OFFLINE);
1092 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1093 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1094 zp->z_pflags, tx);
1095 XVA_SET_RTN(xvap, XAT_SPARSE);
1100 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1102 dmu_object_info_t doi;
1103 dmu_buf_t *db;
1104 znode_t *zp;
1105 int err;
1106 sa_handle_t *hdl;
1108 *zpp = NULL;
1110 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1112 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1113 if (err) {
1114 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1115 return (err);
1118 dmu_object_info_from_db(db, &doi);
1119 if (doi.doi_bonus_type != DMU_OT_SA &&
1120 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1121 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1122 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1123 sa_buf_rele(db, NULL);
1124 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1125 return (SET_ERROR(EINVAL));
1128 hdl = dmu_buf_get_user(db);
1129 if (hdl != NULL) {
1130 zp = sa_get_userdata(hdl);
1134 * Since "SA" does immediate eviction we
1135 * should never find a sa handle that doesn't
1136 * know about the znode.
1139 ASSERT3P(zp, !=, NULL);
1141 mutex_enter(&zp->z_lock);
1142 ASSERT3U(zp->z_id, ==, obj_num);
1143 if (zp->z_unlinked) {
1144 err = SET_ERROR(ENOENT);
1145 } else {
1146 VN_HOLD(ZTOV(zp));
1147 *zpp = zp;
1148 err = 0;
1150 sa_buf_rele(db, NULL);
1151 mutex_exit(&zp->z_lock);
1152 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1153 return (err);
1157 * Not found create new znode/vnode
1158 * but only if file exists.
1160 * There is a small window where zfs_vget() could
1161 * find this object while a file create is still in
1162 * progress. This is checked for in zfs_znode_alloc()
1164 * if zfs_znode_alloc() fails it will drop the hold on the
1165 * bonus buffer.
1167 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1168 doi.doi_bonus_type, NULL);
1169 if (zp == NULL) {
1170 err = SET_ERROR(ENOENT);
1171 } else {
1172 *zpp = zp;
1174 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1175 return (err);
1179 zfs_rezget(znode_t *zp)
1181 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1182 dmu_object_info_t doi;
1183 dmu_buf_t *db;
1184 uint64_t obj_num = zp->z_id;
1185 uint64_t mode;
1186 sa_bulk_attr_t bulk[8];
1187 int err;
1188 int count = 0;
1189 uint64_t gen;
1191 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1193 mutex_enter(&zp->z_acl_lock);
1194 if (zp->z_acl_cached) {
1195 zfs_acl_free(zp->z_acl_cached);
1196 zp->z_acl_cached = NULL;
1199 mutex_exit(&zp->z_acl_lock);
1200 ASSERT(zp->z_sa_hdl == NULL);
1201 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1202 if (err) {
1203 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1204 return (err);
1207 dmu_object_info_from_db(db, &doi);
1208 if (doi.doi_bonus_type != DMU_OT_SA &&
1209 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1210 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1211 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1212 sa_buf_rele(db, NULL);
1213 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1214 return (SET_ERROR(EINVAL));
1217 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1219 /* reload cached values */
1220 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1221 &gen, sizeof (gen));
1222 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1223 &zp->z_size, sizeof (zp->z_size));
1224 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1225 &zp->z_links, sizeof (zp->z_links));
1226 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1227 &zp->z_pflags, sizeof (zp->z_pflags));
1228 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1229 &zp->z_atime, sizeof (zp->z_atime));
1230 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1231 &zp->z_uid, sizeof (zp->z_uid));
1232 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1233 &zp->z_gid, sizeof (zp->z_gid));
1234 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1235 &mode, sizeof (mode));
1237 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1238 zfs_znode_dmu_fini(zp);
1239 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1240 return (SET_ERROR(EIO));
1243 zp->z_mode = mode;
1245 if (gen != zp->z_gen) {
1246 zfs_znode_dmu_fini(zp);
1247 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1248 return (SET_ERROR(EIO));
1251 zp->z_unlinked = (zp->z_links == 0);
1252 zp->z_blksz = doi.doi_data_block_size;
1254 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1256 return (0);
1259 void
1260 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1262 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1263 objset_t *os = zfsvfs->z_os;
1264 uint64_t obj = zp->z_id;
1265 uint64_t acl_obj = zfs_external_acl(zp);
1267 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1268 if (acl_obj) {
1269 VERIFY(!zp->z_is_sa);
1270 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1272 VERIFY(0 == dmu_object_free(os, obj, tx));
1273 zfs_znode_dmu_fini(zp);
1274 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1275 zfs_znode_free(zp);
1278 void
1279 zfs_zinactive(znode_t *zp)
1281 vnode_t *vp = ZTOV(zp);
1282 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1283 uint64_t z_id = zp->z_id;
1285 ASSERT(zp->z_sa_hdl);
1288 * Don't allow a zfs_zget() while were trying to release this znode
1290 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1292 mutex_enter(&zp->z_lock);
1293 mutex_enter(&vp->v_lock);
1294 vp->v_count--;
1295 if (vp->v_count > 0 || vn_has_cached_data(vp)) {
1297 * If the hold count is greater than zero, somebody has
1298 * obtained a new reference on this znode while we were
1299 * processing it here, so we are done. If we still have
1300 * mapped pages then we are also done, since we don't
1301 * want to inactivate the znode until the pages get pushed.
1303 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1304 * this seems like it would leave the znode hanging with
1305 * no chance to go inactive...
1307 mutex_exit(&vp->v_lock);
1308 mutex_exit(&zp->z_lock);
1309 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1310 return;
1312 mutex_exit(&vp->v_lock);
1315 * If this was the last reference to a file with no links,
1316 * remove the file from the file system.
1318 if (zp->z_unlinked) {
1319 mutex_exit(&zp->z_lock);
1320 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1321 zfs_rmnode(zp);
1322 return;
1325 mutex_exit(&zp->z_lock);
1326 zfs_znode_dmu_fini(zp);
1327 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1328 zfs_znode_free(zp);
1331 void
1332 zfs_znode_free(znode_t *zp)
1334 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1336 vn_invalid(ZTOV(zp));
1338 ASSERT(ZTOV(zp)->v_count == 0);
1340 mutex_enter(&zfsvfs->z_znodes_lock);
1341 POINTER_INVALIDATE(&zp->z_zfsvfs);
1342 list_remove(&zfsvfs->z_all_znodes, zp);
1343 mutex_exit(&zfsvfs->z_znodes_lock);
1345 if (zp->z_acl_cached) {
1346 zfs_acl_free(zp->z_acl_cached);
1347 zp->z_acl_cached = NULL;
1350 kmem_cache_free(znode_cache, zp);
1352 VFS_RELE(zfsvfs->z_vfs);
1355 void
1356 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1357 uint64_t ctime[2], boolean_t have_tx)
1359 timestruc_t now;
1361 gethrestime(&now);
1363 if (have_tx) { /* will sa_bulk_update happen really soon? */
1364 zp->z_atime_dirty = 0;
1365 zp->z_seq++;
1366 } else {
1367 zp->z_atime_dirty = 1;
1370 if (flag & AT_ATIME) {
1371 ZFS_TIME_ENCODE(&now, zp->z_atime);
1374 if (flag & AT_MTIME) {
1375 ZFS_TIME_ENCODE(&now, mtime);
1376 if (zp->z_zfsvfs->z_use_fuids) {
1377 zp->z_pflags |= (ZFS_ARCHIVE |
1378 ZFS_AV_MODIFIED);
1382 if (flag & AT_CTIME) {
1383 ZFS_TIME_ENCODE(&now, ctime);
1384 if (zp->z_zfsvfs->z_use_fuids)
1385 zp->z_pflags |= ZFS_ARCHIVE;
1390 * Grow the block size for a file.
1392 * IN: zp - znode of file to free data in.
1393 * size - requested block size
1394 * tx - open transaction.
1396 * NOTE: this function assumes that the znode is write locked.
1398 void
1399 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1401 int error;
1402 u_longlong_t dummy;
1404 if (size <= zp->z_blksz)
1405 return;
1407 * If the file size is already greater than the current blocksize,
1408 * we will not grow. If there is more than one block in a file,
1409 * the blocksize cannot change.
1411 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1412 return;
1414 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1415 size, 0, tx);
1417 if (error == ENOTSUP)
1418 return;
1419 ASSERT0(error);
1421 /* What blocksize did we actually get? */
1422 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1426 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1427 * be calling back into the fs for a putpage(). E.g.: when truncating
1428 * a file, the pages being "thrown away* don't need to be written out.
1430 /* ARGSUSED */
1431 static int
1432 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1433 int flags, cred_t *cr)
1435 ASSERT(0);
1436 return (0);
1440 * Increase the file length
1442 * IN: zp - znode of file to free data in.
1443 * end - new end-of-file
1445 * RETURN: 0 on success, error code on failure
1447 static int
1448 zfs_extend(znode_t *zp, uint64_t end)
1450 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1451 dmu_tx_t *tx;
1452 rl_t *rl;
1453 uint64_t newblksz;
1454 int error;
1457 * We will change zp_size, lock the whole file.
1459 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1462 * Nothing to do if file already at desired length.
1464 if (end <= zp->z_size) {
1465 zfs_range_unlock(rl);
1466 return (0);
1468 tx = dmu_tx_create(zfsvfs->z_os);
1469 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1470 zfs_sa_upgrade_txholds(tx, zp);
1471 if (end > zp->z_blksz &&
1472 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1474 * We are growing the file past the current block size.
1476 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1477 ASSERT(!ISP2(zp->z_blksz));
1478 newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1479 } else {
1480 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1482 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1483 } else {
1484 newblksz = 0;
1487 error = dmu_tx_assign(tx, TXG_WAIT);
1488 if (error) {
1489 dmu_tx_abort(tx);
1490 zfs_range_unlock(rl);
1491 return (error);
1494 if (newblksz)
1495 zfs_grow_blocksize(zp, newblksz, tx);
1497 zp->z_size = end;
1499 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1500 &zp->z_size, sizeof (zp->z_size), tx));
1502 zfs_range_unlock(rl);
1504 dmu_tx_commit(tx);
1506 return (0);
1510 * Free space in a file.
1512 * IN: zp - znode of file to free data in.
1513 * off - start of section to free.
1514 * len - length of section to free.
1516 * RETURN: 0 on success, error code on failure
1518 static int
1519 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1521 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1522 rl_t *rl;
1523 int error;
1526 * Lock the range being freed.
1528 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1531 * Nothing to do if file already at desired length.
1533 if (off >= zp->z_size) {
1534 zfs_range_unlock(rl);
1535 return (0);
1538 if (off + len > zp->z_size)
1539 len = zp->z_size - off;
1541 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1543 zfs_range_unlock(rl);
1545 return (error);
1549 * Truncate a file
1551 * IN: zp - znode of file to free data in.
1552 * end - new end-of-file.
1554 * RETURN: 0 on success, error code on failure
1556 static int
1557 zfs_trunc(znode_t *zp, uint64_t end)
1559 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1560 vnode_t *vp = ZTOV(zp);
1561 dmu_tx_t *tx;
1562 rl_t *rl;
1563 int error;
1564 sa_bulk_attr_t bulk[2];
1565 int count = 0;
1568 * We will change zp_size, lock the whole file.
1570 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1573 * Nothing to do if file already at desired length.
1575 if (end >= zp->z_size) {
1576 zfs_range_unlock(rl);
1577 return (0);
1580 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1);
1581 if (error) {
1582 zfs_range_unlock(rl);
1583 return (error);
1585 tx = dmu_tx_create(zfsvfs->z_os);
1586 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1587 zfs_sa_upgrade_txholds(tx, zp);
1588 dmu_tx_mark_netfree(tx);
1589 error = dmu_tx_assign(tx, TXG_WAIT);
1590 if (error) {
1591 dmu_tx_abort(tx);
1592 zfs_range_unlock(rl);
1593 return (error);
1596 zp->z_size = end;
1597 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1598 NULL, &zp->z_size, sizeof (zp->z_size));
1600 if (end == 0) {
1601 zp->z_pflags &= ~ZFS_SPARSE;
1602 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1603 NULL, &zp->z_pflags, 8);
1605 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1607 dmu_tx_commit(tx);
1610 * Clear any mapped pages in the truncated region. This has to
1611 * happen outside of the transaction to avoid the possibility of
1612 * a deadlock with someone trying to push a page that we are
1613 * about to invalidate.
1615 if (vn_has_cached_data(vp)) {
1616 page_t *pp;
1617 uint64_t start = end & PAGEMASK;
1618 int poff = end & PAGEOFFSET;
1620 if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
1622 * We need to zero a partial page.
1624 pagezero(pp, poff, PAGESIZE - poff);
1625 start += PAGESIZE;
1626 page_unlock(pp);
1628 error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
1629 B_INVAL | B_TRUNC, NULL);
1630 ASSERT(error == 0);
1633 zfs_range_unlock(rl);
1635 return (0);
1639 * Free space in a file
1641 * IN: zp - znode of file to free data in.
1642 * off - start of range
1643 * len - end of range (0 => EOF)
1644 * flag - current file open mode flags.
1645 * log - TRUE if this action should be logged
1647 * RETURN: 0 on success, error code on failure
1650 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1652 vnode_t *vp = ZTOV(zp);
1653 dmu_tx_t *tx;
1654 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1655 zilog_t *zilog = zfsvfs->z_log;
1656 uint64_t mode;
1657 uint64_t mtime[2], ctime[2];
1658 sa_bulk_attr_t bulk[3];
1659 int count = 0;
1660 int error;
1662 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1663 sizeof (mode))) != 0)
1664 return (error);
1666 if (off > zp->z_size) {
1667 error = zfs_extend(zp, off+len);
1668 if (error == 0 && log)
1669 goto log;
1670 else
1671 return (error);
1675 * Check for any locks in the region to be freed.
1678 if (MANDLOCK(vp, (mode_t)mode)) {
1679 uint64_t length = (len ? len : zp->z_size - off);
1680 if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1681 return (error);
1684 if (len == 0) {
1685 error = zfs_trunc(zp, off);
1686 } else {
1687 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1688 off + len > zp->z_size)
1689 error = zfs_extend(zp, off+len);
1691 if (error || !log)
1692 return (error);
1693 log:
1694 tx = dmu_tx_create(zfsvfs->z_os);
1695 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1696 zfs_sa_upgrade_txholds(tx, zp);
1697 error = dmu_tx_assign(tx, TXG_WAIT);
1698 if (error) {
1699 dmu_tx_abort(tx);
1700 return (error);
1703 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1704 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1705 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1706 NULL, &zp->z_pflags, 8);
1707 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1708 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1709 ASSERT(error == 0);
1711 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1713 dmu_tx_commit(tx);
1714 return (0);
1717 void
1718 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1720 zfsvfs_t zfsvfs;
1721 uint64_t moid, obj, sa_obj, version;
1722 uint64_t sense = ZFS_CASE_SENSITIVE;
1723 uint64_t norm = 0;
1724 nvpair_t *elem;
1725 int error;
1726 int i;
1727 znode_t *rootzp = NULL;
1728 vnode_t *vp;
1729 vattr_t vattr;
1730 znode_t *zp;
1731 zfs_acl_ids_t acl_ids;
1734 * First attempt to create master node.
1737 * In an empty objset, there are no blocks to read and thus
1738 * there can be no i/o errors (which we assert below).
1740 moid = MASTER_NODE_OBJ;
1741 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1742 DMU_OT_NONE, 0, tx);
1743 ASSERT(error == 0);
1746 * Set starting attributes.
1748 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1749 elem = NULL;
1750 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1751 /* For the moment we expect all zpl props to be uint64_ts */
1752 uint64_t val;
1753 char *name;
1755 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1756 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1757 name = nvpair_name(elem);
1758 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1759 if (val < version)
1760 version = val;
1761 } else {
1762 error = zap_update(os, moid, name, 8, 1, &val, tx);
1764 ASSERT(error == 0);
1765 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1766 norm = val;
1767 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1768 sense = val;
1770 ASSERT(version != 0);
1771 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1774 * Create zap object used for SA attribute registration
1777 if (version >= ZPL_VERSION_SA) {
1778 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1779 DMU_OT_NONE, 0, tx);
1780 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1781 ASSERT(error == 0);
1782 } else {
1783 sa_obj = 0;
1786 * Create a delete queue.
1788 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1790 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1791 ASSERT(error == 0);
1794 * Create root znode. Create minimal znode/vnode/zfsvfs
1795 * to allow zfs_mknode to work.
1797 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1798 vattr.va_type = VDIR;
1799 vattr.va_mode = S_IFDIR|0755;
1800 vattr.va_uid = crgetuid(cr);
1801 vattr.va_gid = crgetgid(cr);
1803 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1804 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1805 rootzp->z_moved = 0;
1806 rootzp->z_unlinked = 0;
1807 rootzp->z_atime_dirty = 0;
1808 rootzp->z_is_sa = USE_SA(version, os);
1810 vp = ZTOV(rootzp);
1811 vn_reinit(vp);
1812 vp->v_type = VDIR;
1814 bzero(&zfsvfs, sizeof (zfsvfs_t));
1816 zfsvfs.z_os = os;
1817 zfsvfs.z_parent = &zfsvfs;
1818 zfsvfs.z_version = version;
1819 zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1820 zfsvfs.z_use_sa = USE_SA(version, os);
1821 zfsvfs.z_norm = norm;
1823 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1824 &zfsvfs.z_attr_table);
1826 ASSERT(error == 0);
1829 * Fold case on file systems that are always or sometimes case
1830 * insensitive.
1832 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1833 zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1835 mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1836 list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1837 offsetof(znode_t, z_link_node));
1839 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1840 mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1842 rootzp->z_zfsvfs = &zfsvfs;
1843 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1844 cr, NULL, &acl_ids));
1845 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1846 ASSERT3P(zp, ==, rootzp);
1847 ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
1848 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1849 ASSERT(error == 0);
1850 zfs_acl_ids_free(&acl_ids);
1851 POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1853 ZTOV(rootzp)->v_count = 0;
1854 sa_handle_destroy(rootzp->z_sa_hdl);
1855 kmem_cache_free(znode_cache, rootzp);
1858 * Create shares directory
1861 error = zfs_create_share_dir(&zfsvfs, tx);
1863 ASSERT(error == 0);
1865 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1866 mutex_destroy(&zfsvfs.z_hold_mtx[i]);
1869 #endif /* _KERNEL */
1871 static int
1872 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1874 uint64_t sa_obj = 0;
1875 int error;
1877 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1878 if (error != 0 && error != ENOENT)
1879 return (error);
1881 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1882 return (error);
1885 static int
1886 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1887 dmu_buf_t **db, void *tag)
1889 dmu_object_info_t doi;
1890 int error;
1892 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1893 return (error);
1895 dmu_object_info_from_db(*db, &doi);
1896 if ((doi.doi_bonus_type != DMU_OT_SA &&
1897 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1898 doi.doi_bonus_type == DMU_OT_ZNODE &&
1899 doi.doi_bonus_size < sizeof (znode_phys_t)) {
1900 sa_buf_rele(*db, tag);
1901 return (SET_ERROR(ENOTSUP));
1904 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1905 if (error != 0) {
1906 sa_buf_rele(*db, tag);
1907 return (error);
1910 return (0);
1913 void
1914 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1916 sa_handle_destroy(hdl);
1917 sa_buf_rele(db, tag);
1921 * Given an object number, return its parent object number and whether
1922 * or not the object is an extended attribute directory.
1924 static int
1925 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
1926 uint64_t *pobjp, int *is_xattrdir)
1928 uint64_t parent;
1929 uint64_t pflags;
1930 uint64_t mode;
1931 uint64_t parent_mode;
1932 sa_bulk_attr_t bulk[3];
1933 sa_handle_t *sa_hdl;
1934 dmu_buf_t *sa_db;
1935 int count = 0;
1936 int error;
1938 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1939 &parent, sizeof (parent));
1940 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1941 &pflags, sizeof (pflags));
1942 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1943 &mode, sizeof (mode));
1945 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
1946 return (error);
1949 * When a link is removed its parent pointer is not changed and will
1950 * be invalid. There are two cases where a link is removed but the
1951 * file stays around, when it goes to the delete queue and when there
1952 * are additional links.
1954 error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
1955 if (error != 0)
1956 return (error);
1958 error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
1959 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
1960 if (error != 0)
1961 return (error);
1963 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1966 * Extended attributes can be applied to files, directories, etc.
1967 * Otherwise the parent must be a directory.
1969 if (!*is_xattrdir && !S_ISDIR(parent_mode))
1970 return (SET_ERROR(EINVAL));
1972 *pobjp = parent;
1974 return (0);
1978 * Given an object number, return some zpl level statistics
1980 static int
1981 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1982 zfs_stat_t *sb)
1984 sa_bulk_attr_t bulk[4];
1985 int count = 0;
1987 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1988 &sb->zs_mode, sizeof (sb->zs_mode));
1989 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
1990 &sb->zs_gen, sizeof (sb->zs_gen));
1991 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
1992 &sb->zs_links, sizeof (sb->zs_links));
1993 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
1994 &sb->zs_ctime, sizeof (sb->zs_ctime));
1996 return (sa_bulk_lookup(hdl, bulk, count));
1999 static int
2000 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2001 sa_attr_type_t *sa_table, char *buf, int len)
2003 sa_handle_t *sa_hdl;
2004 sa_handle_t *prevhdl = NULL;
2005 dmu_buf_t *prevdb = NULL;
2006 dmu_buf_t *sa_db = NULL;
2007 char *path = buf + len - 1;
2008 int error;
2010 *path = '\0';
2011 sa_hdl = hdl;
2013 for (;;) {
2014 uint64_t pobj;
2015 char component[MAXNAMELEN + 2];
2016 size_t complen;
2017 int is_xattrdir;
2019 if (prevdb)
2020 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2022 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2023 &is_xattrdir)) != 0)
2024 break;
2026 if (pobj == obj) {
2027 if (path[0] != '/')
2028 *--path = '/';
2029 break;
2032 component[0] = '/';
2033 if (is_xattrdir) {
2034 (void) sprintf(component + 1, "<xattrdir>");
2035 } else {
2036 error = zap_value_search(osp, pobj, obj,
2037 ZFS_DIRENT_OBJ(-1ULL), component + 1);
2038 if (error != 0)
2039 break;
2042 complen = strlen(component);
2043 path -= complen;
2044 ASSERT(path >= buf);
2045 bcopy(component, path, complen);
2046 obj = pobj;
2048 if (sa_hdl != hdl) {
2049 prevhdl = sa_hdl;
2050 prevdb = sa_db;
2052 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2053 if (error != 0) {
2054 sa_hdl = prevhdl;
2055 sa_db = prevdb;
2056 break;
2060 if (sa_hdl != NULL && sa_hdl != hdl) {
2061 ASSERT(sa_db != NULL);
2062 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2065 if (error == 0)
2066 (void) memmove(buf, path, buf + len - path);
2068 return (error);
2072 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2074 sa_attr_type_t *sa_table;
2075 sa_handle_t *hdl;
2076 dmu_buf_t *db;
2077 int error;
2079 error = zfs_sa_setup(osp, &sa_table);
2080 if (error != 0)
2081 return (error);
2083 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2084 if (error != 0)
2085 return (error);
2087 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2089 zfs_release_sa_handle(hdl, db, FTAG);
2090 return (error);
2094 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2095 char *buf, int len)
2097 char *path = buf + len - 1;
2098 sa_attr_type_t *sa_table;
2099 sa_handle_t *hdl;
2100 dmu_buf_t *db;
2101 int error;
2103 *path = '\0';
2105 error = zfs_sa_setup(osp, &sa_table);
2106 if (error != 0)
2107 return (error);
2109 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2110 if (error != 0)
2111 return (error);
2113 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2114 if (error != 0) {
2115 zfs_release_sa_handle(hdl, db, FTAG);
2116 return (error);
2119 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2121 zfs_release_sa_handle(hdl, db, FTAG);
2122 return (error);