Merge commit '7e3488dc6cdcb0c04e1ce167a1a3bfef83b5f2e0'
[unleashed.git] / kernel / fs / zfs / zfs_znode.c
blob014a14d60d447520f37dffbf2698fa5be17b8b6f
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, 2017 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Integros [integros.com]
27 /* Portions Copyright 2007 Jeremy Teo */
29 #ifdef _KERNEL
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
36 #include <sys/mntent.h>
37 #include <sys/mkdev.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/mode.h>
47 #include <sys/atomic.h>
48 #include <vm/pvn.h>
49 #include "sys/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/dmu_objset.h>
62 #include <sys/refcount.h>
63 #include <sys/stat.h>
64 #include <sys/zap.h>
65 #include <sys/zfs_znode.h>
66 #include <sys/sa.h>
67 #include <sys/zfs_sa.h>
68 #include <sys/zfs_stat.h>
70 #include "zfs_prop.h"
71 #include "zfs_comutil.h"
74 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
75 * turned on when DEBUG is also defined.
77 #ifdef DEBUG
78 #define ZNODE_STATS
79 #endif /* DEBUG */
81 #ifdef ZNODE_STATS
82 #define ZNODE_STAT_ADD(stat) ((stat)++)
83 #else
84 #define ZNODE_STAT_ADD(stat) /* nothing */
85 #endif /* ZNODE_STATS */
88 * Functions needed for userland (ie: libzpool) are not put under
89 * #ifdef_KERNEL; the rest of the functions have dependencies
90 * (such as VFS logic) that will not compile easily in userland.
92 #ifdef _KERNEL
94 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
95 * be freed before it can be safely accessed.
97 krwlock_t zfsvfs_lock;
99 static kmem_cache_t *znode_cache = NULL;
101 /*ARGSUSED*/
102 static void
103 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
106 * We should never drop all dbuf refs without first clearing
107 * the eviction callback.
109 panic("evicting znode %p\n", user_ptr);
112 /*ARGSUSED*/
113 static int
114 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
116 znode_t *zp = buf;
118 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
120 zp->z_vnode = vn_alloc(kmflags);
121 if (zp->z_vnode == NULL) {
122 return (-1);
124 ZTOV(zp)->v_data = zp;
126 list_link_init(&zp->z_link_node);
128 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
129 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
130 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
131 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
133 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
134 avl_create(&zp->z_range_avl, zfs_range_compare,
135 sizeof (rl_t), offsetof(rl_t, r_node));
137 zp->z_dirlocks = NULL;
138 zp->z_acl_cached = NULL;
139 zp->z_moved = 0;
140 return (0);
143 /*ARGSUSED*/
144 static void
145 zfs_znode_cache_destructor(void *buf, void *arg)
147 znode_t *zp = buf;
149 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
150 ASSERT(ZTOV(zp)->v_data == zp);
151 vn_free(ZTOV(zp));
152 ASSERT(!list_link_active(&zp->z_link_node));
153 mutex_destroy(&zp->z_lock);
154 rw_destroy(&zp->z_parent_lock);
155 rw_destroy(&zp->z_name_lock);
156 mutex_destroy(&zp->z_acl_lock);
157 avl_destroy(&zp->z_range_avl);
158 mutex_destroy(&zp->z_range_lock);
160 ASSERT(zp->z_dirlocks == NULL);
161 ASSERT(zp->z_acl_cached == NULL);
164 #ifdef ZNODE_STATS
165 static struct {
166 uint64_t zms_zfsvfs_invalid;
167 uint64_t zms_zfsvfs_recheck1;
168 uint64_t zms_zfsvfs_unmounted;
169 uint64_t zms_zfsvfs_recheck2;
170 uint64_t zms_obj_held;
171 uint64_t zms_vnode_locked;
172 uint64_t zms_not_only_dnlc;
173 } znode_move_stats;
174 #endif /* ZNODE_STATS */
176 static void
177 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
179 vnode_t *vp;
181 /* Copy fields. */
182 nzp->z_zfsvfs = ozp->z_zfsvfs;
184 /* Swap vnodes. */
185 vp = nzp->z_vnode;
186 nzp->z_vnode = ozp->z_vnode;
187 ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
188 ZTOV(ozp)->v_data = ozp;
189 ZTOV(nzp)->v_data = nzp;
191 nzp->z_id = ozp->z_id;
192 ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
193 ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
194 nzp->z_unlinked = ozp->z_unlinked;
195 nzp->z_atime_dirty = ozp->z_atime_dirty;
196 nzp->z_zn_prefetch = ozp->z_zn_prefetch;
197 nzp->z_blksz = ozp->z_blksz;
198 nzp->z_seq = ozp->z_seq;
199 nzp->z_mapcnt = ozp->z_mapcnt;
200 nzp->z_gen = ozp->z_gen;
201 nzp->z_sync_cnt = ozp->z_sync_cnt;
202 nzp->z_is_sa = ozp->z_is_sa;
203 nzp->z_sa_hdl = ozp->z_sa_hdl;
204 bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
205 nzp->z_links = ozp->z_links;
206 nzp->z_size = ozp->z_size;
207 nzp->z_pflags = ozp->z_pflags;
208 nzp->z_uid = ozp->z_uid;
209 nzp->z_gid = ozp->z_gid;
210 nzp->z_mode = ozp->z_mode;
213 * Since this is just an idle znode and kmem is already dealing with
214 * memory pressure, release any cached ACL.
216 if (ozp->z_acl_cached) {
217 zfs_acl_free(ozp->z_acl_cached);
218 ozp->z_acl_cached = NULL;
221 sa_set_userp(nzp->z_sa_hdl, nzp);
224 * Invalidate the original znode by clearing fields that provide a
225 * pointer back to the znode. Set the low bit of the vfs pointer to
226 * ensure that zfs_znode_move() recognizes the znode as invalid in any
227 * subsequent callback.
229 ozp->z_sa_hdl = NULL;
230 POINTER_INVALIDATE(&ozp->z_zfsvfs);
233 * Mark the znode.
235 nzp->z_moved = 1;
236 ozp->z_moved = (uint8_t)-1;
239 /*ARGSUSED*/
240 static kmem_cbrc_t
241 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
243 znode_t *ozp = buf, *nzp = newbuf;
244 zfsvfs_t *zfsvfs;
245 vnode_t *vp;
248 * The znode is on the file system's list of known znodes if the vfs
249 * pointer is valid. We set the low bit of the vfs pointer when freeing
250 * the znode to invalidate it, and the memory patterns written by kmem
251 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
252 * created znode sets the vfs pointer last of all to indicate that the
253 * znode is known and in a valid state to be moved by this function.
255 zfsvfs = ozp->z_zfsvfs;
256 if (!POINTER_IS_VALID(zfsvfs)) {
257 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
258 return (KMEM_CBRC_DONT_KNOW);
262 * Close a small window in which it's possible that the filesystem could
263 * be unmounted and freed, and zfsvfs, though valid in the previous
264 * statement, could point to unrelated memory by the time we try to
265 * prevent the filesystem from being unmounted.
267 rw_enter(&zfsvfs_lock, RW_WRITER);
268 if (zfsvfs != ozp->z_zfsvfs) {
269 rw_exit(&zfsvfs_lock);
270 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
271 return (KMEM_CBRC_DONT_KNOW);
275 * If the znode is still valid, then so is the file system. We know that
276 * no valid file system can be freed while we hold zfsvfs_lock, so we
277 * can safely ensure that the filesystem is not and will not be
278 * unmounted. The next statement is equivalent to ZFS_ENTER().
280 rrm_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
281 if (zfsvfs->z_unmounted) {
282 ZFS_EXIT(zfsvfs);
283 rw_exit(&zfsvfs_lock);
284 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
285 return (KMEM_CBRC_DONT_KNOW);
287 rw_exit(&zfsvfs_lock);
289 mutex_enter(&zfsvfs->z_znodes_lock);
291 * Recheck the vfs pointer in case the znode was removed just before
292 * acquiring the lock.
294 if (zfsvfs != ozp->z_zfsvfs) {
295 mutex_exit(&zfsvfs->z_znodes_lock);
296 ZFS_EXIT(zfsvfs);
297 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
298 return (KMEM_CBRC_DONT_KNOW);
302 * At this point we know that as long as we hold z_znodes_lock, the
303 * znode cannot be freed and fields within the znode can be safely
304 * accessed. Now, prevent a race with zfs_zget().
306 if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
307 mutex_exit(&zfsvfs->z_znodes_lock);
308 ZFS_EXIT(zfsvfs);
309 ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
310 return (KMEM_CBRC_LATER);
313 vp = ZTOV(ozp);
314 if (mutex_tryenter(&vp->v_lock) == 0) {
315 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
316 mutex_exit(&zfsvfs->z_znodes_lock);
317 ZFS_EXIT(zfsvfs);
318 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
319 return (KMEM_CBRC_LATER);
322 /* Only move znodes that are referenced _only_ by the DNLC. */
323 if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
324 mutex_exit(&vp->v_lock);
325 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
326 mutex_exit(&zfsvfs->z_znodes_lock);
327 ZFS_EXIT(zfsvfs);
328 ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
329 return (KMEM_CBRC_LATER);
333 * The znode is known and in a valid state to move. We're holding the
334 * locks needed to execute the critical section.
336 zfs_znode_move_impl(ozp, nzp);
337 mutex_exit(&vp->v_lock);
338 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
340 list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
341 mutex_exit(&zfsvfs->z_znodes_lock);
342 ZFS_EXIT(zfsvfs);
344 return (KMEM_CBRC_YES);
347 void
348 zfs_znode_init(void)
351 * Initialize zcache
353 rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
354 ASSERT(znode_cache == NULL);
355 znode_cache = kmem_cache_create("zfs_znode_cache",
356 sizeof (znode_t), 0, zfs_znode_cache_constructor,
357 zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
358 kmem_cache_set_move(znode_cache, zfs_znode_move);
361 void
362 zfs_znode_fini(void)
365 * Cleanup vfs & vnode ops
367 zfs_remove_op_tables();
370 * Cleanup zcache
372 if (znode_cache)
373 kmem_cache_destroy(znode_cache);
374 znode_cache = NULL;
375 rw_destroy(&zfsvfs_lock);
378 extern const struct vnodeops zfs_dvnodeops;
379 extern const struct vnodeops zfs_fvnodeops;
380 extern const struct vnodeops zfs_symvnodeops;
381 extern const struct vnodeops zfs_xdvnodeops;
382 extern const struct vnodeops zfs_evnodeops;
383 extern const struct vnodeops zfs_sharevnodeops;
385 void
386 zfs_remove_op_tables()
389 * Remove vfs ops
391 ASSERT(zfsfstype);
392 (void) vfs_freevfsops_by_type(zfsfstype);
393 zfsfstype = 0;
397 zfs_create_op_tables()
399 return (0);
403 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
405 zfs_acl_ids_t acl_ids;
406 vattr_t vattr;
407 znode_t *sharezp;
408 vnode_t *vp;
409 znode_t *zp;
410 int error;
412 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
413 vattr.va_type = VDIR;
414 vattr.va_mode = S_IFDIR|0555;
415 vattr.va_uid = crgetuid(kcred);
416 vattr.va_gid = crgetgid(kcred);
418 sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
419 ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
420 sharezp->z_moved = 0;
421 sharezp->z_unlinked = 0;
422 sharezp->z_atime_dirty = 0;
423 sharezp->z_zfsvfs = zfsvfs;
424 sharezp->z_is_sa = zfsvfs->z_use_sa;
426 vp = ZTOV(sharezp);
427 vn_reinit(vp);
428 vp->v_type = VDIR;
430 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
431 kcred, NULL, &acl_ids));
432 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
433 ASSERT3P(zp, ==, sharezp);
434 ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
435 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
436 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
437 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
438 zfsvfs->z_shares_dir = sharezp->z_id;
440 zfs_acl_ids_free(&acl_ids);
441 ZTOV(sharezp)->v_count = 0;
442 sa_handle_destroy(sharezp->z_sa_hdl);
443 kmem_cache_free(znode_cache, sharezp);
445 return (error);
449 * define a couple of values we need available
450 * for both 64 and 32 bit environments.
452 #ifndef NBITSMINOR64
453 #define NBITSMINOR64 32
454 #endif
455 #ifndef MAXMAJ64
456 #define MAXMAJ64 0xffffffffUL
457 #endif
458 #ifndef MAXMIN64
459 #define MAXMIN64 0xffffffffUL
460 #endif
463 * Create special expldev for ZFS private use.
464 * Can't use standard expldev since it doesn't do
465 * what we want. The standard expldev() takes a
466 * dev32_t in LP64 and expands it to a long dev_t.
467 * We need an interface that takes a dev32_t in ILP32
468 * and expands it to a long dev_t.
470 static uint64_t
471 zfs_expldev(dev_t dev)
473 #ifndef _LP64
474 major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
475 return (((uint64_t)major << NBITSMINOR64) |
476 ((minor_t)dev & MAXMIN32));
477 #else
478 return (dev);
479 #endif
483 * Special cmpldev for ZFS private use.
484 * Can't use standard cmpldev since it takes
485 * a long dev_t and compresses it to dev32_t in
486 * LP64. We need to do a compaction of a long dev_t
487 * to a dev32_t in ILP32.
489 dev_t
490 zfs_cmpldev(uint64_t dev)
492 #ifndef _LP64
493 minor_t minor = (minor_t)dev & MAXMIN64;
494 major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
496 if (major > MAXMAJ32 || minor > MAXMIN32)
497 return (NODEV32);
499 return (((dev32_t)major << NBITSMINOR32) | minor);
500 #else
501 return (dev);
502 #endif
505 static void
506 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
507 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
509 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
510 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
512 mutex_enter(&zp->z_lock);
514 ASSERT(zp->z_sa_hdl == NULL);
515 ASSERT(zp->z_acl_cached == NULL);
516 if (sa_hdl == NULL) {
517 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
518 SA_HDL_SHARED, &zp->z_sa_hdl));
519 } else {
520 zp->z_sa_hdl = sa_hdl;
521 sa_set_userp(sa_hdl, zp);
524 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
527 * Slap on VROOT if we are the root znode
529 if (zp->z_id == zfsvfs->z_root)
530 ZTOV(zp)->v_flag |= VROOT;
532 mutex_exit(&zp->z_lock);
533 vn_exists(ZTOV(zp));
536 void
537 zfs_znode_dmu_fini(znode_t *zp)
539 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
540 zp->z_unlinked ||
541 RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
543 sa_handle_destroy(zp->z_sa_hdl);
544 zp->z_sa_hdl = NULL;
548 * Construct a new znode/vnode and intialize.
550 * This does not do a call to dmu_set_user() that is
551 * up to the caller to do, in case you don't want to
552 * return the znode
554 static znode_t *
555 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
556 dmu_object_type_t obj_type, sa_handle_t *hdl)
558 znode_t *zp;
559 vnode_t *vp;
560 uint64_t mode;
561 uint64_t parent;
562 sa_bulk_attr_t bulk[9];
563 int count = 0;
565 zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
567 ASSERT(zp->z_dirlocks == NULL);
568 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
569 zp->z_moved = 0;
572 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
573 * the zfs_znode_move() callback.
575 zp->z_sa_hdl = NULL;
576 zp->z_unlinked = 0;
577 zp->z_atime_dirty = 0;
578 zp->z_mapcnt = 0;
579 zp->z_id = db->db_object;
580 zp->z_blksz = blksz;
581 zp->z_seq = 0x7A4653;
582 zp->z_sync_cnt = 0;
584 vp = ZTOV(zp);
585 vn_reinit(vp);
587 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
589 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
590 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
591 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
592 &zp->z_size, 8);
593 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
594 &zp->z_links, 8);
595 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
596 &zp->z_pflags, 8);
597 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
598 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
599 &zp->z_atime, 16);
600 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
601 &zp->z_uid, 8);
602 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
603 &zp->z_gid, 8);
605 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
606 if (hdl == NULL)
607 sa_handle_destroy(zp->z_sa_hdl);
608 kmem_cache_free(znode_cache, zp);
609 return (NULL);
612 zp->z_mode = mode;
613 vp->v_vfsp = zfsvfs->z_parent->z_vfs;
615 vp->v_type = IFTOVT((mode_t)mode);
617 switch (vp->v_type) {
618 case VDIR:
619 if (zp->z_pflags & ZFS_XATTR) {
620 vn_setops(vp, &zfs_xdvnodeops);
621 vp->v_flag |= V_XATTRDIR;
622 } else {
623 vn_setops(vp, &zfs_dvnodeops);
625 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
626 break;
627 case VBLK:
628 case VCHR:
630 uint64_t rdev;
631 VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
632 &rdev, sizeof (rdev)) == 0);
634 vp->v_rdev = zfs_cmpldev(rdev);
636 /*FALLTHROUGH*/
637 case VFIFO:
638 case VSOCK:
639 case VDOOR:
640 vn_setops(vp, &zfs_fvnodeops);
641 break;
642 case VREG:
643 vp->v_flag |= VMODSORT;
644 if (parent == zfsvfs->z_shares_dir) {
645 ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
646 vn_setops(vp, &zfs_sharevnodeops);
647 } else {
648 vn_setops(vp, &zfs_fvnodeops);
650 break;
651 case VLNK:
652 vn_setops(vp, &zfs_symvnodeops);
653 break;
654 default:
655 vn_setops(vp, &zfs_evnodeops);
656 break;
659 mutex_enter(&zfsvfs->z_znodes_lock);
660 list_insert_tail(&zfsvfs->z_all_znodes, zp);
661 membar_producer();
663 * Everything else must be valid before assigning z_zfsvfs makes the
664 * znode eligible for zfs_znode_move().
666 zp->z_zfsvfs = zfsvfs;
667 mutex_exit(&zfsvfs->z_znodes_lock);
669 VFS_HOLD(zfsvfs->z_vfs);
670 return (zp);
673 static uint64_t empty_xattr;
674 static uint64_t pad[4];
675 static zfs_acl_phys_t acl_phys;
677 * Create a new DMU object to hold a zfs znode.
679 * IN: dzp - parent directory for new znode
680 * vap - file attributes for new znode
681 * tx - dmu transaction id for zap operations
682 * cr - credentials of caller
683 * flag - flags:
684 * IS_ROOT_NODE - new object will be root
685 * IS_XATTR - new object is an attribute
686 * bonuslen - length of bonus buffer
687 * setaclp - File/Dir initial ACL
688 * fuidp - Tracks fuid allocation.
690 * OUT: zpp - allocated znode
693 void
694 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
695 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
697 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
698 uint64_t mode, size, links, parent, pflags;
699 uint64_t dzp_pflags = 0;
700 uint64_t rdev = 0;
701 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
702 dmu_buf_t *db;
703 timestruc_t now;
704 uint64_t gen, obj;
705 int bonuslen;
706 sa_handle_t *sa_hdl;
707 dmu_object_type_t obj_type;
708 sa_bulk_attr_t sa_attrs[ZPL_END];
709 int cnt = 0;
710 zfs_acl_locator_cb_t locate = { 0 };
712 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
714 if (zfsvfs->z_replay) {
715 obj = vap->va_nodeid;
716 now = vap->va_ctime; /* see zfs_replay_create() */
717 gen = vap->va_nblocks; /* ditto */
718 } else {
719 obj = 0;
720 gethrestime(&now);
721 gen = dmu_tx_get_txg(tx);
724 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
725 bonuslen = (obj_type == DMU_OT_SA) ?
726 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
729 * Create a new DMU object.
732 * There's currently no mechanism for pre-reading the blocks that will
733 * be needed to allocate a new object, so we accept the small chance
734 * that there will be an i/o error and we will fail one of the
735 * assertions below.
737 if (vap->va_type == VDIR) {
738 if (zfsvfs->z_replay) {
739 VERIFY0(zap_create_claim_norm(zfsvfs->z_os, obj,
740 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
741 obj_type, bonuslen, tx));
742 } else {
743 obj = zap_create_norm(zfsvfs->z_os,
744 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
745 obj_type, bonuslen, tx);
747 } else {
748 if (zfsvfs->z_replay) {
749 VERIFY0(dmu_object_claim(zfsvfs->z_os, obj,
750 DMU_OT_PLAIN_FILE_CONTENTS, 0,
751 obj_type, bonuslen, tx));
752 } else {
753 obj = dmu_object_alloc(zfsvfs->z_os,
754 DMU_OT_PLAIN_FILE_CONTENTS, 0,
755 obj_type, bonuslen, tx);
759 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
760 VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
763 * If this is the root, fix up the half-initialized parent pointer
764 * to reference the just-allocated physical data area.
766 if (flag & IS_ROOT_NODE) {
767 dzp->z_id = obj;
768 } else {
769 dzp_pflags = dzp->z_pflags;
773 * If parent is an xattr, so am I.
775 if (dzp_pflags & ZFS_XATTR) {
776 flag |= IS_XATTR;
779 if (zfsvfs->z_use_fuids)
780 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
781 else
782 pflags = 0;
784 if (vap->va_type == VDIR) {
785 size = 2; /* contents ("." and "..") */
786 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
787 } else {
788 size = links = 0;
791 if (vap->va_type == VBLK || vap->va_type == VCHR) {
792 rdev = zfs_expldev(vap->va_rdev);
795 parent = dzp->z_id;
796 mode = acl_ids->z_mode;
797 if (flag & IS_XATTR)
798 pflags |= ZFS_XATTR;
801 * No execs denied will be deterimed when zfs_mode_compute() is called.
803 pflags |= acl_ids->z_aclp->z_hints &
804 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
805 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
807 ZFS_TIME_ENCODE(&now, crtime);
808 ZFS_TIME_ENCODE(&now, ctime);
810 if (vap->va_mask & AT_ATIME) {
811 ZFS_TIME_ENCODE(&vap->va_atime, atime);
812 } else {
813 ZFS_TIME_ENCODE(&now, atime);
816 if (vap->va_mask & AT_MTIME) {
817 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
818 } else {
819 ZFS_TIME_ENCODE(&now, mtime);
822 /* Now add in all of the "SA" attributes */
823 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
824 &sa_hdl));
827 * Setup the array of attributes to be replaced/set on the new file
829 * order for DMU_OT_ZNODE is critical since it needs to be constructed
830 * in the old znode_phys_t format. Don't change this ordering
833 if (obj_type == DMU_OT_ZNODE) {
834 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
835 NULL, &atime, 16);
836 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
837 NULL, &mtime, 16);
838 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
839 NULL, &ctime, 16);
840 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
841 NULL, &crtime, 16);
842 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
843 NULL, &gen, 8);
844 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
845 NULL, &mode, 8);
846 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
847 NULL, &size, 8);
848 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
849 NULL, &parent, 8);
850 } else {
851 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
852 NULL, &mode, 8);
853 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
854 NULL, &size, 8);
855 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
856 NULL, &gen, 8);
857 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
858 &acl_ids->z_fuid, 8);
859 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
860 &acl_ids->z_fgid, 8);
861 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
862 NULL, &parent, 8);
863 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
864 NULL, &pflags, 8);
865 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
866 NULL, &atime, 16);
867 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
868 NULL, &mtime, 16);
869 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
870 NULL, &ctime, 16);
871 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
872 NULL, &crtime, 16);
875 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
877 if (obj_type == DMU_OT_ZNODE) {
878 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
879 &empty_xattr, 8);
881 if (obj_type == DMU_OT_ZNODE ||
882 (vap->va_type == VBLK || vap->va_type == VCHR)) {
883 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
884 NULL, &rdev, 8);
887 if (obj_type == DMU_OT_ZNODE) {
888 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
889 NULL, &pflags, 8);
890 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
891 &acl_ids->z_fuid, 8);
892 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
893 &acl_ids->z_fgid, 8);
894 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
895 sizeof (uint64_t) * 4);
896 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
897 &acl_phys, sizeof (zfs_acl_phys_t));
898 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
899 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
900 &acl_ids->z_aclp->z_acl_count, 8);
901 locate.cb_aclp = acl_ids->z_aclp;
902 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
903 zfs_acl_data_locator, &locate,
904 acl_ids->z_aclp->z_acl_bytes);
905 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
906 acl_ids->z_fuid, acl_ids->z_fgid);
909 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
911 if (!(flag & IS_ROOT_NODE)) {
912 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
913 ASSERT(*zpp != NULL);
914 } else {
916 * If we are creating the root node, the "parent" we
917 * passed in is the znode for the root.
919 *zpp = dzp;
921 (*zpp)->z_sa_hdl = sa_hdl;
924 (*zpp)->z_pflags = pflags;
925 (*zpp)->z_mode = mode;
927 if (vap->va_mask & AT_XVATTR)
928 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
930 if (obj_type == DMU_OT_ZNODE ||
931 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
932 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
934 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
938 * Update in-core attributes. It is assumed the caller will be doing an
939 * sa_bulk_update to push the changes out.
941 void
942 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
944 xoptattr_t *xoap;
946 xoap = xva_getxoptattr(xvap);
947 ASSERT(xoap);
949 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
950 uint64_t times[2];
951 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
952 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
953 &times, sizeof (times), tx);
954 XVA_SET_RTN(xvap, XAT_CREATETIME);
956 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
957 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
958 zp->z_pflags, tx);
959 XVA_SET_RTN(xvap, XAT_READONLY);
961 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
962 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
963 zp->z_pflags, tx);
964 XVA_SET_RTN(xvap, XAT_HIDDEN);
966 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
967 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
968 zp->z_pflags, tx);
969 XVA_SET_RTN(xvap, XAT_SYSTEM);
971 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
972 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
973 zp->z_pflags, tx);
974 XVA_SET_RTN(xvap, XAT_ARCHIVE);
976 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
977 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
978 zp->z_pflags, tx);
979 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
981 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
982 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
983 zp->z_pflags, tx);
984 XVA_SET_RTN(xvap, XAT_NOUNLINK);
986 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
987 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
988 zp->z_pflags, tx);
989 XVA_SET_RTN(xvap, XAT_APPENDONLY);
991 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
992 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
993 zp->z_pflags, tx);
994 XVA_SET_RTN(xvap, XAT_NODUMP);
996 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
997 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
998 zp->z_pflags, tx);
999 XVA_SET_RTN(xvap, XAT_OPAQUE);
1001 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1002 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1003 xoap->xoa_av_quarantined, zp->z_pflags, tx);
1004 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1006 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1007 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1008 zp->z_pflags, tx);
1009 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1011 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1012 zfs_sa_set_scanstamp(zp, xvap, tx);
1013 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1015 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1016 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1017 zp->z_pflags, tx);
1018 XVA_SET_RTN(xvap, XAT_REPARSE);
1020 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1021 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1022 zp->z_pflags, tx);
1023 XVA_SET_RTN(xvap, XAT_OFFLINE);
1025 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1026 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1027 zp->z_pflags, tx);
1028 XVA_SET_RTN(xvap, XAT_SPARSE);
1033 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1035 dmu_object_info_t doi;
1036 dmu_buf_t *db;
1037 znode_t *zp;
1038 int err;
1039 sa_handle_t *hdl;
1041 *zpp = NULL;
1043 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1045 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1046 if (err) {
1047 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1048 return (err);
1051 dmu_object_info_from_db(db, &doi);
1052 if (doi.doi_bonus_type != DMU_OT_SA &&
1053 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1054 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1055 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1056 sa_buf_rele(db, NULL);
1057 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1058 return (SET_ERROR(EINVAL));
1061 hdl = dmu_buf_get_user(db);
1062 if (hdl != NULL) {
1063 zp = sa_get_userdata(hdl);
1067 * Since "SA" does immediate eviction we
1068 * should never find a sa handle that doesn't
1069 * know about the znode.
1072 ASSERT3P(zp, !=, NULL);
1074 mutex_enter(&zp->z_lock);
1075 ASSERT3U(zp->z_id, ==, obj_num);
1076 if (zp->z_unlinked) {
1077 err = SET_ERROR(ENOENT);
1078 } else {
1079 VN_HOLD(ZTOV(zp));
1080 *zpp = zp;
1081 err = 0;
1083 mutex_exit(&zp->z_lock);
1084 sa_buf_rele(db, NULL);
1085 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1086 return (err);
1090 * Not found create new znode/vnode
1091 * but only if file exists.
1093 * There is a small window where zfs_vget() could
1094 * find this object while a file create is still in
1095 * progress. This is checked for in zfs_znode_alloc()
1097 * if zfs_znode_alloc() fails it will drop the hold on the
1098 * bonus buffer.
1100 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1101 doi.doi_bonus_type, NULL);
1102 if (zp == NULL) {
1103 err = SET_ERROR(ENOENT);
1104 } else {
1105 *zpp = zp;
1107 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1108 return (err);
1112 zfs_rezget(znode_t *zp)
1114 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1115 dmu_object_info_t doi;
1116 dmu_buf_t *db;
1117 uint64_t obj_num = zp->z_id;
1118 uint64_t mode;
1119 sa_bulk_attr_t bulk[8];
1120 int err;
1121 int count = 0;
1122 uint64_t gen;
1124 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1126 mutex_enter(&zp->z_acl_lock);
1127 if (zp->z_acl_cached) {
1128 zfs_acl_free(zp->z_acl_cached);
1129 zp->z_acl_cached = NULL;
1132 mutex_exit(&zp->z_acl_lock);
1133 ASSERT(zp->z_sa_hdl == NULL);
1134 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1135 if (err) {
1136 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1137 return (err);
1140 dmu_object_info_from_db(db, &doi);
1141 if (doi.doi_bonus_type != DMU_OT_SA &&
1142 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1143 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1144 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1145 sa_buf_rele(db, NULL);
1146 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1147 return (SET_ERROR(EINVAL));
1150 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1152 /* reload cached values */
1153 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1154 &gen, sizeof (gen));
1155 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1156 &zp->z_size, sizeof (zp->z_size));
1157 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1158 &zp->z_links, sizeof (zp->z_links));
1159 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1160 &zp->z_pflags, sizeof (zp->z_pflags));
1161 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1162 &zp->z_atime, sizeof (zp->z_atime));
1163 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1164 &zp->z_uid, sizeof (zp->z_uid));
1165 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1166 &zp->z_gid, sizeof (zp->z_gid));
1167 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1168 &mode, sizeof (mode));
1170 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1171 zfs_znode_dmu_fini(zp);
1172 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1173 return (SET_ERROR(EIO));
1176 zp->z_mode = mode;
1178 if (gen != zp->z_gen) {
1179 zfs_znode_dmu_fini(zp);
1180 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1181 return (SET_ERROR(EIO));
1184 zp->z_blksz = doi.doi_data_block_size;
1187 * If the file has zero links, then it has been unlinked on the send
1188 * side and it must be in the received unlinked set.
1189 * We call zfs_znode_dmu_fini() now to prevent any accesses to the
1190 * stale data and to prevent automatical removal of the file in
1191 * zfs_zinactive(). The file will be removed either when it is removed
1192 * on the send side and the next incremental stream is received or
1193 * when the unlinked set gets processed.
1195 zp->z_unlinked = (zp->z_links == 0);
1196 if (zp->z_unlinked)
1197 zfs_znode_dmu_fini(zp);
1199 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1201 return (0);
1204 void
1205 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1207 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1208 objset_t *os = zfsvfs->z_os;
1209 uint64_t obj = zp->z_id;
1210 uint64_t acl_obj = zfs_external_acl(zp);
1212 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1213 if (acl_obj) {
1214 VERIFY(!zp->z_is_sa);
1215 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1217 VERIFY(0 == dmu_object_free(os, obj, tx));
1218 zfs_znode_dmu_fini(zp);
1219 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1220 zfs_znode_free(zp);
1223 void
1224 zfs_zinactive(znode_t *zp)
1226 vnode_t *vp = ZTOV(zp);
1227 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1228 uint64_t z_id = zp->z_id;
1230 ASSERT(zp->z_sa_hdl);
1233 * Don't allow a zfs_zget() while were trying to release this znode
1235 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1237 mutex_enter(&zp->z_lock);
1238 mutex_enter(&vp->v_lock);
1239 VN_RELE_LOCKED(vp);
1240 if (vp->v_count > 0 || vn_has_cached_data(vp)) {
1242 * If the hold count is greater than zero, somebody has
1243 * obtained a new reference on this znode while we were
1244 * processing it here, so we are done. If we still have
1245 * mapped pages then we are also done, since we don't
1246 * want to inactivate the znode until the pages get pushed.
1248 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1249 * this seems like it would leave the znode hanging with
1250 * no chance to go inactive...
1252 mutex_exit(&vp->v_lock);
1253 mutex_exit(&zp->z_lock);
1254 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1255 return;
1257 mutex_exit(&vp->v_lock);
1260 * If this was the last reference to a file with no links, remove
1261 * the file from the file system unless the file system is mounted
1262 * read-only. That can happen, for example, if the file system was
1263 * originally read-write, the file was opened, then unlinked and
1264 * the file system was made read-only before the file was finally
1265 * closed. The file will remain in the unlinked set.
1267 if (zp->z_unlinked) {
1268 ASSERT(!zfsvfs->z_issnap);
1269 if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0) {
1270 mutex_exit(&zp->z_lock);
1271 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1272 zfs_rmnode(zp);
1273 return;
1277 mutex_exit(&zp->z_lock);
1278 zfs_znode_dmu_fini(zp);
1279 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1280 zfs_znode_free(zp);
1283 void
1284 zfs_znode_free(znode_t *zp)
1286 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1288 vn_invalid(ZTOV(zp));
1290 ASSERT(ZTOV(zp)->v_count == 0);
1292 mutex_enter(&zfsvfs->z_znodes_lock);
1293 POINTER_INVALIDATE(&zp->z_zfsvfs);
1294 list_remove(&zfsvfs->z_all_znodes, zp);
1295 mutex_exit(&zfsvfs->z_znodes_lock);
1297 if (zp->z_acl_cached) {
1298 zfs_acl_free(zp->z_acl_cached);
1299 zp->z_acl_cached = NULL;
1302 kmem_cache_free(znode_cache, zp);
1304 VFS_RELE(zfsvfs->z_vfs);
1307 void
1308 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1309 uint64_t ctime[2], boolean_t have_tx)
1311 timestruc_t now;
1313 gethrestime(&now);
1315 if (have_tx) { /* will sa_bulk_update happen really soon? */
1316 zp->z_atime_dirty = 0;
1317 zp->z_seq++;
1318 } else {
1319 zp->z_atime_dirty = 1;
1322 if (flag & AT_ATIME) {
1323 ZFS_TIME_ENCODE(&now, zp->z_atime);
1326 if (flag & AT_MTIME) {
1327 ZFS_TIME_ENCODE(&now, mtime);
1328 if (zp->z_zfsvfs->z_use_fuids) {
1329 zp->z_pflags |= (ZFS_ARCHIVE |
1330 ZFS_AV_MODIFIED);
1334 if (flag & AT_CTIME) {
1335 ZFS_TIME_ENCODE(&now, ctime);
1336 if (zp->z_zfsvfs->z_use_fuids)
1337 zp->z_pflags |= ZFS_ARCHIVE;
1342 * Grow the block size for a file.
1344 * IN: zp - znode of file to free data in.
1345 * size - requested block size
1346 * tx - open transaction.
1348 * NOTE: this function assumes that the znode is write locked.
1350 void
1351 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1353 int error;
1354 u_longlong_t dummy;
1356 if (size <= zp->z_blksz)
1357 return;
1359 * If the file size is already greater than the current blocksize,
1360 * we will not grow. If there is more than one block in a file,
1361 * the blocksize cannot change.
1363 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1364 return;
1366 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1367 size, 0, tx);
1369 if (error == ENOTSUP)
1370 return;
1371 ASSERT0(error);
1373 /* What blocksize did we actually get? */
1374 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1378 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1379 * be calling back into the fs for a putpage(). E.g.: when truncating
1380 * a file, the pages being "thrown away* don't need to be written out.
1382 /* ARGSUSED */
1383 static int
1384 zfs_no_putpage(vnode_t *vp, page_t *pp, uoff_t *offp, size_t *lenp,
1385 int flags, cred_t *cr)
1387 ASSERT(0);
1388 return (0);
1392 * Increase the file length
1394 * IN: zp - znode of file to free data in.
1395 * end - new end-of-file
1397 * RETURN: 0 on success, error code on failure
1399 static int
1400 zfs_extend(znode_t *zp, uint64_t end)
1402 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1403 dmu_tx_t *tx;
1404 rl_t *rl;
1405 uint64_t newblksz;
1406 int error;
1409 * We will change zp_size, lock the whole file.
1411 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1414 * Nothing to do if file already at desired length.
1416 if (end <= zp->z_size) {
1417 zfs_range_unlock(rl);
1418 return (0);
1420 tx = dmu_tx_create(zfsvfs->z_os);
1421 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1422 zfs_sa_upgrade_txholds(tx, zp);
1423 if (end > zp->z_blksz &&
1424 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1426 * We are growing the file past the current block size.
1428 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1430 * File's blocksize is already larger than the
1431 * "recordsize" property. Only let it grow to
1432 * the next power of 2.
1434 ASSERT(!ISP2(zp->z_blksz));
1435 newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1436 } else {
1437 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1439 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1440 } else {
1441 newblksz = 0;
1444 error = dmu_tx_assign(tx, TXG_WAIT);
1445 if (error) {
1446 dmu_tx_abort(tx);
1447 zfs_range_unlock(rl);
1448 return (error);
1451 if (newblksz)
1452 zfs_grow_blocksize(zp, newblksz, tx);
1454 zp->z_size = end;
1456 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1457 &zp->z_size, sizeof (zp->z_size), tx));
1459 zfs_range_unlock(rl);
1461 dmu_tx_commit(tx);
1463 return (0);
1467 * Free space in a file.
1469 * IN: zp - znode of file to free data in.
1470 * off - start of section to free.
1471 * len - length of section to free.
1473 * RETURN: 0 on success, error code on failure
1475 static int
1476 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1478 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1479 rl_t *rl;
1480 int error;
1483 * Lock the range being freed.
1485 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1488 * Nothing to do if file already at desired length.
1490 if (off >= zp->z_size) {
1491 zfs_range_unlock(rl);
1492 return (0);
1495 if (off + len > zp->z_size)
1496 len = zp->z_size - off;
1498 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1500 zfs_range_unlock(rl);
1502 return (error);
1506 * Truncate a file
1508 * IN: zp - znode of file to free data in.
1509 * end - new end-of-file.
1511 * RETURN: 0 on success, error code on failure
1513 static int
1514 zfs_trunc(znode_t *zp, uint64_t end)
1516 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1517 vnode_t *vp = ZTOV(zp);
1518 dmu_tx_t *tx;
1519 rl_t *rl;
1520 int error;
1521 sa_bulk_attr_t bulk[2];
1522 int count = 0;
1525 * We will change zp_size, lock the whole file.
1527 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1530 * Nothing to do if file already at desired length.
1532 if (end >= zp->z_size) {
1533 zfs_range_unlock(rl);
1534 return (0);
1537 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,
1538 DMU_OBJECT_END);
1539 if (error) {
1540 zfs_range_unlock(rl);
1541 return (error);
1543 tx = dmu_tx_create(zfsvfs->z_os);
1544 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1545 zfs_sa_upgrade_txholds(tx, zp);
1546 dmu_tx_mark_netfree(tx);
1547 error = dmu_tx_assign(tx, TXG_WAIT);
1548 if (error) {
1549 dmu_tx_abort(tx);
1550 zfs_range_unlock(rl);
1551 return (error);
1554 zp->z_size = end;
1555 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1556 NULL, &zp->z_size, sizeof (zp->z_size));
1558 if (end == 0) {
1559 zp->z_pflags &= ~ZFS_SPARSE;
1560 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1561 NULL, &zp->z_pflags, 8);
1563 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1565 dmu_tx_commit(tx);
1568 * Clear any mapped pages in the truncated region. This has to
1569 * happen outside of the transaction to avoid the possibility of
1570 * a deadlock with someone trying to push a page that we are
1571 * about to invalidate.
1573 if (vn_has_cached_data(vp)) {
1574 page_t *pp;
1575 uint64_t start = end & PAGEMASK;
1576 int poff = end & PAGEOFFSET;
1578 if (poff != 0 && (pp = page_lookup(&vp->v_object, start, SE_SHARED))) {
1580 * We need to zero a partial page.
1582 pagezero(pp, poff, PAGESIZE - poff);
1583 start += PAGESIZE;
1584 page_unlock(pp);
1586 error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
1587 B_INVAL | B_TRUNC, NULL);
1588 ASSERT(error == 0);
1591 zfs_range_unlock(rl);
1593 return (0);
1597 * Free space in a file
1599 * IN: zp - znode of file to free data in.
1600 * off - start of range
1601 * len - end of range (0 => EOF)
1602 * flag - current file open mode flags.
1603 * log - TRUE if this action should be logged
1605 * RETURN: 0 on success, error code on failure
1608 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1610 vnode_t *vp = ZTOV(zp);
1611 dmu_tx_t *tx;
1612 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1613 zilog_t *zilog = zfsvfs->z_log;
1614 uint64_t mode;
1615 uint64_t mtime[2], ctime[2];
1616 sa_bulk_attr_t bulk[3];
1617 int count = 0;
1618 int error;
1620 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1621 sizeof (mode))) != 0)
1622 return (error);
1624 if (off > zp->z_size) {
1625 error = zfs_extend(zp, off+len);
1626 if (error == 0 && log)
1627 goto log;
1628 else
1629 return (error);
1633 * Check for any locks in the region to be freed.
1636 if (MANDLOCK(vp, (mode_t)mode)) {
1637 uint64_t length = (len ? len : zp->z_size - off);
1638 if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1639 return (error);
1642 if (len == 0) {
1643 error = zfs_trunc(zp, off);
1644 } else {
1645 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1646 off + len > zp->z_size)
1647 error = zfs_extend(zp, off+len);
1649 if (error || !log)
1650 return (error);
1651 log:
1652 tx = dmu_tx_create(zfsvfs->z_os);
1653 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1654 zfs_sa_upgrade_txholds(tx, zp);
1655 error = dmu_tx_assign(tx, TXG_WAIT);
1656 if (error) {
1657 dmu_tx_abort(tx);
1658 return (error);
1661 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1662 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1663 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1664 NULL, &zp->z_pflags, 8);
1665 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1666 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1667 ASSERT(error == 0);
1669 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1671 dmu_tx_commit(tx);
1672 return (0);
1675 void
1676 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1678 uint64_t moid, obj, sa_obj, version;
1679 uint64_t sense = ZFS_CASE_SENSITIVE;
1680 uint64_t norm = 0;
1681 nvpair_t *elem;
1682 int error;
1683 int i;
1684 znode_t *rootzp = NULL;
1685 zfsvfs_t *zfsvfs;
1686 vnode_t *vp;
1687 vattr_t vattr;
1688 znode_t *zp;
1689 zfs_acl_ids_t acl_ids;
1692 * First attempt to create master node.
1695 * In an empty objset, there are no blocks to read and thus
1696 * there can be no i/o errors (which we assert below).
1698 moid = MASTER_NODE_OBJ;
1699 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1700 DMU_OT_NONE, 0, tx);
1701 ASSERT(error == 0);
1704 * Set starting attributes.
1706 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1707 elem = NULL;
1708 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1709 /* For the moment we expect all zpl props to be uint64_ts */
1710 uint64_t val;
1711 char *name;
1713 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1714 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1715 name = nvpair_name(elem);
1716 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1717 if (val < version)
1718 version = val;
1719 } else {
1720 error = zap_update(os, moid, name, 8, 1, &val, tx);
1722 ASSERT(error == 0);
1723 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1724 norm = val;
1725 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1726 sense = val;
1728 ASSERT(version != 0);
1729 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1732 * Create zap object used for SA attribute registration
1735 if (version >= ZPL_VERSION_SA) {
1736 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1737 DMU_OT_NONE, 0, tx);
1738 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1739 ASSERT(error == 0);
1740 } else {
1741 sa_obj = 0;
1744 * Create a delete queue.
1746 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1748 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1749 ASSERT(error == 0);
1752 * Create root znode. Create minimal znode/vnode/zfsvfs
1753 * to allow zfs_mknode to work.
1755 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1756 vattr.va_type = VDIR;
1757 vattr.va_mode = S_IFDIR|0755;
1758 vattr.va_uid = crgetuid(cr);
1759 vattr.va_gid = crgetgid(cr);
1761 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1762 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1763 rootzp->z_moved = 0;
1764 rootzp->z_unlinked = 0;
1765 rootzp->z_atime_dirty = 0;
1766 rootzp->z_is_sa = USE_SA(version, os);
1768 vp = ZTOV(rootzp);
1769 vn_reinit(vp);
1770 vp->v_type = VDIR;
1772 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1773 zfsvfs->z_os = os;
1774 zfsvfs->z_parent = zfsvfs;
1775 zfsvfs->z_version = version;
1776 zfsvfs->z_use_fuids = USE_FUIDS(version, os);
1777 zfsvfs->z_use_sa = USE_SA(version, os);
1778 zfsvfs->z_norm = norm;
1780 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1781 &zfsvfs->z_attr_table);
1783 ASSERT(error == 0);
1786 * Fold case on file systems that are always or sometimes case
1787 * insensitive.
1789 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1790 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1792 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1793 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1794 offsetof(znode_t, z_link_node));
1796 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1797 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1799 rootzp->z_zfsvfs = zfsvfs;
1800 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1801 cr, NULL, &acl_ids));
1802 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1803 ASSERT3P(zp, ==, rootzp);
1804 ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
1805 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1806 ASSERT(error == 0);
1807 zfs_acl_ids_free(&acl_ids);
1808 POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1810 ZTOV(rootzp)->v_count = 0;
1811 sa_handle_destroy(rootzp->z_sa_hdl);
1812 kmem_cache_free(znode_cache, rootzp);
1815 * Create shares directory
1818 error = zfs_create_share_dir(zfsvfs, tx);
1820 ASSERT(error == 0);
1822 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1823 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1824 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1827 #endif /* _KERNEL */
1829 static int
1830 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1832 uint64_t sa_obj = 0;
1833 int error;
1835 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1836 if (error != 0 && error != ENOENT)
1837 return (error);
1839 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1840 return (error);
1843 static int
1844 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1845 dmu_buf_t **db, void *tag)
1847 dmu_object_info_t doi;
1848 int error;
1850 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1851 return (error);
1853 dmu_object_info_from_db(*db, &doi);
1854 if ((doi.doi_bonus_type != DMU_OT_SA &&
1855 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1856 doi.doi_bonus_type == DMU_OT_ZNODE &&
1857 doi.doi_bonus_size < sizeof (znode_phys_t)) {
1858 sa_buf_rele(*db, tag);
1859 return (SET_ERROR(ENOTSUP));
1862 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1863 if (error != 0) {
1864 sa_buf_rele(*db, tag);
1865 return (error);
1868 return (0);
1871 void
1872 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1874 sa_handle_destroy(hdl);
1875 sa_buf_rele(db, tag);
1879 * Given an object number, return its parent object number and whether
1880 * or not the object is an extended attribute directory.
1882 static int
1883 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
1884 uint64_t *pobjp, int *is_xattrdir)
1886 uint64_t parent;
1887 uint64_t pflags;
1888 uint64_t mode;
1889 uint64_t parent_mode;
1890 sa_bulk_attr_t bulk[3];
1891 sa_handle_t *sa_hdl;
1892 dmu_buf_t *sa_db;
1893 int count = 0;
1894 int error;
1896 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1897 &parent, sizeof (parent));
1898 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1899 &pflags, sizeof (pflags));
1900 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1901 &mode, sizeof (mode));
1903 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
1904 return (error);
1907 * When a link is removed its parent pointer is not changed and will
1908 * be invalid. There are two cases where a link is removed but the
1909 * file stays around, when it goes to the delete queue and when there
1910 * are additional links.
1912 error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
1913 if (error != 0)
1914 return (error);
1916 error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
1917 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
1918 if (error != 0)
1919 return (error);
1921 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1924 * Extended attributes can be applied to files, directories, etc.
1925 * Otherwise the parent must be a directory.
1927 if (!*is_xattrdir && !S_ISDIR(parent_mode))
1928 return (SET_ERROR(EINVAL));
1930 *pobjp = parent;
1932 return (0);
1936 * Given an object number, return some zpl level statistics
1938 static int
1939 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1940 zfs_stat_t *sb)
1942 sa_bulk_attr_t bulk[4];
1943 int count = 0;
1945 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1946 &sb->zs_mode, sizeof (sb->zs_mode));
1947 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
1948 &sb->zs_gen, sizeof (sb->zs_gen));
1949 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
1950 &sb->zs_links, sizeof (sb->zs_links));
1951 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
1952 &sb->zs_ctime, sizeof (sb->zs_ctime));
1954 return (sa_bulk_lookup(hdl, bulk, count));
1957 static int
1958 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
1959 sa_attr_type_t *sa_table, char *buf, int len)
1961 sa_handle_t *sa_hdl;
1962 sa_handle_t *prevhdl = NULL;
1963 dmu_buf_t *prevdb = NULL;
1964 dmu_buf_t *sa_db = NULL;
1965 char *path = buf + len - 1;
1966 int error;
1968 *path = '\0';
1969 sa_hdl = hdl;
1971 uint64_t deleteq_obj;
1972 VERIFY0(zap_lookup(osp, MASTER_NODE_OBJ,
1973 ZFS_UNLINKED_SET, sizeof (uint64_t), 1, &deleteq_obj));
1974 error = zap_lookup_int(osp, deleteq_obj, obj);
1975 if (error == 0) {
1976 return (ESTALE);
1977 } else if (error != ENOENT) {
1978 return (error);
1980 error = 0;
1982 for (;;) {
1983 uint64_t pobj;
1984 char component[MAXNAMELEN + 2];
1985 size_t complen;
1986 int is_xattrdir;
1988 if (prevdb)
1989 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
1991 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
1992 &is_xattrdir)) != 0)
1993 break;
1995 if (pobj == obj) {
1996 if (path[0] != '/')
1997 *--path = '/';
1998 break;
2001 component[0] = '/';
2002 if (is_xattrdir) {
2003 (void) sprintf(component + 1, "<xattrdir>");
2004 } else {
2005 error = zap_value_search(osp, pobj, obj,
2006 ZFS_DIRENT_OBJ(-1ULL), component + 1);
2007 if (error != 0)
2008 break;
2011 complen = strlen(component);
2012 path -= complen;
2013 ASSERT(path >= buf);
2014 bcopy(component, path, complen);
2015 obj = pobj;
2017 if (sa_hdl != hdl) {
2018 prevhdl = sa_hdl;
2019 prevdb = sa_db;
2021 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2022 if (error != 0) {
2023 sa_hdl = prevhdl;
2024 sa_db = prevdb;
2025 break;
2029 if (sa_hdl != NULL && sa_hdl != hdl) {
2030 ASSERT(sa_db != NULL);
2031 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2034 if (error == 0)
2035 (void) memmove(buf, path, buf + len - path);
2037 return (error);
2041 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2043 sa_attr_type_t *sa_table;
2044 sa_handle_t *hdl;
2045 dmu_buf_t *db;
2046 int error;
2048 error = zfs_sa_setup(osp, &sa_table);
2049 if (error != 0)
2050 return (error);
2052 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2053 if (error != 0)
2054 return (error);
2056 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2058 zfs_release_sa_handle(hdl, db, FTAG);
2059 return (error);
2063 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2064 char *buf, int len)
2066 char *path = buf + len - 1;
2067 sa_attr_type_t *sa_table;
2068 sa_handle_t *hdl;
2069 dmu_buf_t *db;
2070 int error;
2072 *path = '\0';
2074 error = zfs_sa_setup(osp, &sa_table);
2075 if (error != 0)
2076 return (error);
2078 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2079 if (error != 0)
2080 return (error);
2082 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2083 if (error != 0) {
2084 zfs_release_sa_handle(hdl, db, FTAG);
2085 return (error);
2088 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2090 zfs_release_sa_handle(hdl, db, FTAG);
2091 return (error);