1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock * You may not use this file except in compliance with the License.
7fa9e4066Sahrens *
8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens * See the License for the specific language governing permissions
11fa9e4066Sahrens * and limitations under the License.
12fa9e4066Sahrens *
13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens *
19fa9e4066Sahrens * CDDL HEADER END
20fa9e4066Sahrens */
21fa9e4066Sahrens /*
228f2529deSMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
234bb73804SMatthew Ahrens * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24fa9e4066Sahrens */
25fa9e4066Sahrens
2675c76197Speteh /* Portions Copyright 2007 Jeremy Teo */
2775c76197Speteh
2855434c77Sek110237 #ifdef _KERNEL
29fa9e4066Sahrens #include <sys/types.h>
30fa9e4066Sahrens #include <sys/param.h>
31fa9e4066Sahrens #include <sys/time.h>
32fa9e4066Sahrens #include <sys/systm.h>
33fa9e4066Sahrens #include <sys/sysmacros.h>
34fa9e4066Sahrens #include <sys/resource.h>
35fa9e4066Sahrens #include <sys/mntent.h>
3672fc53bcSmarks #include <sys/mkdev.h>
37de8267e0Stimh #include <sys/u8_textprep.h>
38ab04eb8eStimh #include <sys/dsl_dataset.h>
39fa9e4066Sahrens #include <sys/vfs.h>
40aa59c4cbSrsb #include <sys/vfs_opreg.h>
41fa9e4066Sahrens #include <sys/vnode.h>
42fa9e4066Sahrens #include <sys/file.h>
43fa9e4066Sahrens #include <sys/kmem.h>
44fa9e4066Sahrens #include <sys/errno.h>
45fa9e4066Sahrens #include <sys/unistd.h>
46fa9e4066Sahrens #include <sys/mode.h>
47fa9e4066Sahrens #include <sys/atomic.h>
48fa9e4066Sahrens #include <vm/pvn.h>
49fa9e4066Sahrens #include "fs/fs_subr.h"
50fa9e4066Sahrens #include <sys/zfs_dir.h>
51fa9e4066Sahrens #include <sys/zfs_acl.h>
52fa9e4066Sahrens #include <sys/zfs_ioctl.h>
53104e2ed7Sperrin #include <sys/zfs_rlock.h>
54da6c28aaSamw #include <sys/zfs_fuid.h>
550a586ceaSMark Shellenbaum #include <sys/dnode.h>
56fa9e4066Sahrens #include <sys/fs/zfs.h>
57da6c28aaSamw #include <sys/kidmap.h>
5855434c77Sek110237 #endif /* _KERNEL */
59fa9e4066Sahrens
6055434c77Sek110237 #include <sys/dmu.h>
61b5152584SMatthew Ahrens #include <sys/dmu_objset.h>
6255434c77Sek110237 #include <sys/refcount.h>
6355434c77Sek110237 #include <sys/stat.h>
6455434c77Sek110237 #include <sys/zap.h>
6555434c77Sek110237 #include <sys/zfs_znode.h>
660a586ceaSMark Shellenbaum #include <sys/sa.h>
670a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
6899d5e173STim Haley #include <sys/zfs_stat.h>
69205ed6bfSAndreas Jaekel #include <sys/zfs_events.h>
70c080fd8cSAndreas Jaekel #include <zev/zev.h>
7155434c77Sek110237
72de8267e0Stimh #include "zfs_prop.h"
730a586ceaSMark Shellenbaum #include "zfs_comutil.h"
74de8267e0Stimh
7555434c77Sek110237 /*
76b5fca8f8Stomee * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
77b5fca8f8Stomee * turned on when DEBUG is also defined.
78b5fca8f8Stomee */
79b5fca8f8Stomee #ifdef DEBUG
80b5fca8f8Stomee #define ZNODE_STATS
81b5fca8f8Stomee #endif /* DEBUG */
82b5fca8f8Stomee
83b5fca8f8Stomee #ifdef ZNODE_STATS
84b5fca8f8Stomee #define ZNODE_STAT_ADD(stat) ((stat)++)
85b5fca8f8Stomee #else
86b5fca8f8Stomee #define ZNODE_STAT_ADD(stat) /* nothing */
87b5fca8f8Stomee #endif /* ZNODE_STATS */
88b5fca8f8Stomee
89b5fca8f8Stomee /*
9055434c77Sek110237 * Functions needed for userland (ie: libzpool) are not put under
9155434c77Sek110237 * #ifdef_KERNEL; the rest of the functions have dependencies
9255434c77Sek110237 * (such as VFS logic) that will not compile easily in userland.
9355434c77Sek110237 */
9455434c77Sek110237 #ifdef _KERNEL
954e9583b2STom Erickson /*
964e9583b2STom Erickson * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
974e9583b2STom Erickson * be freed before it can be safely accessed.
984e9583b2STom Erickson */
994e9583b2STom Erickson krwlock_t zfsvfs_lock;
1004e9583b2STom Erickson
101b5fca8f8Stomee static kmem_cache_t *znode_cache = NULL;
102fa9e4066Sahrens
103fa9e4066Sahrens /*ARGSUSED*/
104fa9e4066Sahrens static void
znode_evict_error(dmu_buf_t * dbuf,void * user_ptr)105874395d5Smaybee znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
106fa9e4066Sahrens {
107874395d5Smaybee /*
108874395d5Smaybee * We should never drop all dbuf refs without first clearing
109874395d5Smaybee * the eviction callback.
110874395d5Smaybee */
111874395d5Smaybee panic("evicting znode %p\n", user_ptr);
112fa9e4066Sahrens }
113fa9e4066Sahrens
114fa9e4066Sahrens /*ARGSUSED*/
115fa9e4066Sahrens static int
zfs_znode_cache_constructor(void * buf,void * arg,int kmflags)116b5fca8f8Stomee zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
117fa9e4066Sahrens {
118fa9e4066Sahrens znode_t *zp = buf;
119fa9e4066Sahrens
120b5fca8f8Stomee ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
121b5fca8f8Stomee
122b5fca8f8Stomee zp->z_vnode = vn_alloc(kmflags);
123b5fca8f8Stomee if (zp->z_vnode == NULL) {
124b5fca8f8Stomee return (-1);
125b5fca8f8Stomee }
126b5fca8f8Stomee ZTOV(zp)->v_data = zp;
127b5fca8f8Stomee
128b5fca8f8Stomee list_link_init(&zp->z_link_node);
129b5fca8f8Stomee
130fa9e4066Sahrens mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
131104e2ed7Sperrin rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
132af2c4821Smaybee rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
133fa9e4066Sahrens mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
134104e2ed7Sperrin
135104e2ed7Sperrin mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
136104e2ed7Sperrin avl_create(&zp->z_range_avl, zfs_range_compare,
137104e2ed7Sperrin sizeof (rl_t), offsetof(rl_t, r_node));
138104e2ed7Sperrin
139b5fca8f8Stomee zp->z_dirlocks = NULL;
140d47621a4STim Haley zp->z_acl_cached = NULL;
141744947dcSTom Erickson zp->z_moved = 0;
142fa9e4066Sahrens return (0);
143fa9e4066Sahrens }
144fa9e4066Sahrens
145fa9e4066Sahrens /*ARGSUSED*/
146fa9e4066Sahrens static void
zfs_znode_cache_destructor(void * buf,void * arg)147b5fca8f8Stomee zfs_znode_cache_destructor(void *buf, void *arg)
148fa9e4066Sahrens {
149fa9e4066Sahrens znode_t *zp = buf;
150fa9e4066Sahrens
151b5fca8f8Stomee ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
152b5fca8f8Stomee ASSERT(ZTOV(zp)->v_data == zp);
153b5fca8f8Stomee vn_free(ZTOV(zp));
154b5fca8f8Stomee ASSERT(!list_link_active(&zp->z_link_node));
155fa9e4066Sahrens mutex_destroy(&zp->z_lock);
156104e2ed7Sperrin rw_destroy(&zp->z_parent_lock);
157af2c4821Smaybee rw_destroy(&zp->z_name_lock);
158fa9e4066Sahrens mutex_destroy(&zp->z_acl_lock);
159104e2ed7Sperrin avl_destroy(&zp->z_range_avl);
160c25056deSgw25295 mutex_destroy(&zp->z_range_lock);
161fa9e4066Sahrens
162b5fca8f8Stomee ASSERT(zp->z_dirlocks == NULL);
1634929fd5eSTim Haley ASSERT(zp->z_acl_cached == NULL);
164b5fca8f8Stomee }
165b5fca8f8Stomee
166b5fca8f8Stomee #ifdef ZNODE_STATS
167b5fca8f8Stomee static struct {
168b5fca8f8Stomee uint64_t zms_zfsvfs_invalid;
1694e9583b2STom Erickson uint64_t zms_zfsvfs_recheck1;
170b5fca8f8Stomee uint64_t zms_zfsvfs_unmounted;
1714e9583b2STom Erickson uint64_t zms_zfsvfs_recheck2;
172a66b2b35STom Erickson uint64_t zms_obj_held;
173b5fca8f8Stomee uint64_t zms_vnode_locked;
174a66b2b35STom Erickson uint64_t zms_not_only_dnlc;
175b5fca8f8Stomee } znode_move_stats;
176b5fca8f8Stomee #endif /* ZNODE_STATS */
177b5fca8f8Stomee
178b5fca8f8Stomee static void
zfs_znode_move_impl(znode_t * ozp,znode_t * nzp)179b5fca8f8Stomee zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
180b5fca8f8Stomee {
181b5fca8f8Stomee vnode_t *vp;
182b5fca8f8Stomee
183b5fca8f8Stomee /* Copy fields. */
184b5fca8f8Stomee nzp->z_zfsvfs = ozp->z_zfsvfs;
185b5fca8f8Stomee
186b5fca8f8Stomee /* Swap vnodes. */
187b5fca8f8Stomee vp = nzp->z_vnode;
188b5fca8f8Stomee nzp->z_vnode = ozp->z_vnode;
189b5fca8f8Stomee ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
190b5fca8f8Stomee ZTOV(ozp)->v_data = ozp;
191b5fca8f8Stomee ZTOV(nzp)->v_data = nzp;
192b5fca8f8Stomee
193b5fca8f8Stomee nzp->z_id = ozp->z_id;
194b5fca8f8Stomee ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
195b5fca8f8Stomee ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
196b5fca8f8Stomee nzp->z_unlinked = ozp->z_unlinked;
197b5fca8f8Stomee nzp->z_atime_dirty = ozp->z_atime_dirty;
198b5fca8f8Stomee nzp->z_zn_prefetch = ozp->z_zn_prefetch;
199b5fca8f8Stomee nzp->z_blksz = ozp->z_blksz;
200b5fca8f8Stomee nzp->z_seq = ozp->z_seq;
201b5fca8f8Stomee nzp->z_mapcnt = ozp->z_mapcnt;
202b5fca8f8Stomee nzp->z_gen = ozp->z_gen;
203b5fca8f8Stomee nzp->z_sync_cnt = ozp->z_sync_cnt;
2040a586ceaSMark Shellenbaum nzp->z_is_sa = ozp->z_is_sa;
2050a586ceaSMark Shellenbaum nzp->z_sa_hdl = ozp->z_sa_hdl;
2060a586ceaSMark Shellenbaum bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
2070a586ceaSMark Shellenbaum nzp->z_links = ozp->z_links;
2080a586ceaSMark Shellenbaum nzp->z_size = ozp->z_size;
2090a586ceaSMark Shellenbaum nzp->z_pflags = ozp->z_pflags;
2100a586ceaSMark Shellenbaum nzp->z_uid = ozp->z_uid;
2110a586ceaSMark Shellenbaum nzp->z_gid = ozp->z_gid;
2120a586ceaSMark Shellenbaum nzp->z_mode = ozp->z_mode;
21337c9dfdcSAndreas Jaekel nzp->z_new_content = ozp->z_new_content;
214d98a6232SMark Shellenbaum
215d98a6232SMark Shellenbaum /*
2166638ae1dSMark Shellenbaum * Since this is just an idle znode and kmem is already dealing with
2176638ae1dSMark Shellenbaum * memory pressure, release any cached ACL.
218d98a6232SMark Shellenbaum */
219d98a6232SMark Shellenbaum if (ozp->z_acl_cached) {
220d98a6232SMark Shellenbaum zfs_acl_free(ozp->z_acl_cached);
221d98a6232SMark Shellenbaum ozp->z_acl_cached = NULL;
222d98a6232SMark Shellenbaum }
223b5fca8f8Stomee
2240a586ceaSMark Shellenbaum sa_set_userp(nzp->z_sa_hdl, nzp);
225b5fca8f8Stomee
226b5fca8f8Stomee /*
227b5fca8f8Stomee * Invalidate the original znode by clearing fields that provide a
228b5fca8f8Stomee * pointer back to the znode. Set the low bit of the vfs pointer to
229b5fca8f8Stomee * ensure that zfs_znode_move() recognizes the znode as invalid in any
230b5fca8f8Stomee * subsequent callback.
231b5fca8f8Stomee */
2320a586ceaSMark Shellenbaum ozp->z_sa_hdl = NULL;
233b5fca8f8Stomee POINTER_INVALIDATE(&ozp->z_zfsvfs);
234744947dcSTom Erickson
235744947dcSTom Erickson /*
236744947dcSTom Erickson * Mark the znode.
237744947dcSTom Erickson */
238744947dcSTom Erickson nzp->z_moved = 1;
239744947dcSTom Erickson ozp->z_moved = (uint8_t)-1;
240b5fca8f8Stomee }
241b5fca8f8Stomee
242b5fca8f8Stomee /*ARGSUSED*/
243b5fca8f8Stomee static kmem_cbrc_t
zfs_znode_move(void * buf,void * newbuf,size_t size,void * arg)244b5fca8f8Stomee zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
245b5fca8f8Stomee {
246b5fca8f8Stomee znode_t *ozp = buf, *nzp = newbuf;
247b5fca8f8Stomee zfsvfs_t *zfsvfs;
248b5fca8f8Stomee vnode_t *vp;
249b5fca8f8Stomee
250b5fca8f8Stomee /*
251b5fca8f8Stomee * The znode is on the file system's list of known znodes if the vfs
252b5fca8f8Stomee * pointer is valid. We set the low bit of the vfs pointer when freeing
253b5fca8f8Stomee * the znode to invalidate it, and the memory patterns written by kmem
254b5fca8f8Stomee * (baddcafe and deadbeef) set at least one of the two low bits. A newly
255b5fca8f8Stomee * created znode sets the vfs pointer last of all to indicate that the
256b5fca8f8Stomee * znode is known and in a valid state to be moved by this function.
257b5fca8f8Stomee */
258b5fca8f8Stomee zfsvfs = ozp->z_zfsvfs;
259b5fca8f8Stomee if (!POINTER_IS_VALID(zfsvfs)) {
260b5fca8f8Stomee ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
261b5fca8f8Stomee return (KMEM_CBRC_DONT_KNOW);
262b5fca8f8Stomee }
263b5fca8f8Stomee
264b5fca8f8Stomee /*
2654e9583b2STom Erickson * Close a small window in which it's possible that the filesystem could
2664e9583b2STom Erickson * be unmounted and freed, and zfsvfs, though valid in the previous
2674e9583b2STom Erickson * statement, could point to unrelated memory by the time we try to
2684e9583b2STom Erickson * prevent the filesystem from being unmounted.
2694e9583b2STom Erickson */
2704e9583b2STom Erickson rw_enter(&zfsvfs_lock, RW_WRITER);
2714e9583b2STom Erickson if (zfsvfs != ozp->z_zfsvfs) {
2724e9583b2STom Erickson rw_exit(&zfsvfs_lock);
2734e9583b2STom Erickson ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
2744e9583b2STom Erickson return (KMEM_CBRC_DONT_KNOW);
2754e9583b2STom Erickson }
2764e9583b2STom Erickson
2774e9583b2STom Erickson /*
2784e9583b2STom Erickson * If the znode is still valid, then so is the file system. We know that
2794e9583b2STom Erickson * no valid file system can be freed while we hold zfsvfs_lock, so we
2804e9583b2STom Erickson * can safely ensure that the filesystem is not and will not be
2814e9583b2STom Erickson * unmounted. The next statement is equivalent to ZFS_ENTER().
282b5fca8f8Stomee */
283c9030f6cSAlexander Motin rrm_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
28414843421SMatthew Ahrens if (zfsvfs->z_unmounted) {
28514843421SMatthew Ahrens ZFS_EXIT(zfsvfs);
2864e9583b2STom Erickson rw_exit(&zfsvfs_lock);
287b5fca8f8Stomee ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
288b5fca8f8Stomee return (KMEM_CBRC_DONT_KNOW);
289b5fca8f8Stomee }
2904e9583b2STom Erickson rw_exit(&zfsvfs_lock);
291b5fca8f8Stomee
292b5fca8f8Stomee mutex_enter(&zfsvfs->z_znodes_lock);
293b5fca8f8Stomee /*
294b5fca8f8Stomee * Recheck the vfs pointer in case the znode was removed just before
295b5fca8f8Stomee * acquiring the lock.
296b5fca8f8Stomee */
297b5fca8f8Stomee if (zfsvfs != ozp->z_zfsvfs) {
298b5fca8f8Stomee mutex_exit(&zfsvfs->z_znodes_lock);
299b5fca8f8Stomee ZFS_EXIT(zfsvfs);
3004e9583b2STom Erickson ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
301b5fca8f8Stomee return (KMEM_CBRC_DONT_KNOW);
302b5fca8f8Stomee }
303b5fca8f8Stomee
304b5fca8f8Stomee /*
305b5fca8f8Stomee * At this point we know that as long as we hold z_znodes_lock, the
306b5fca8f8Stomee * znode cannot be freed and fields within the znode can be safely
307a66b2b35STom Erickson * accessed. Now, prevent a race with zfs_zget().
308b5fca8f8Stomee */
309a66b2b35STom Erickson if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
310a66b2b35STom Erickson mutex_exit(&zfsvfs->z_znodes_lock);
311a66b2b35STom Erickson ZFS_EXIT(zfsvfs);
312a66b2b35STom Erickson ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
313a66b2b35STom Erickson return (KMEM_CBRC_LATER);
314a66b2b35STom Erickson }
315a66b2b35STom Erickson
316b5fca8f8Stomee vp = ZTOV(ozp);
317b5fca8f8Stomee if (mutex_tryenter(&vp->v_lock) == 0) {
318a66b2b35STom Erickson ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
319b5fca8f8Stomee mutex_exit(&zfsvfs->z_znodes_lock);
320b5fca8f8Stomee ZFS_EXIT(zfsvfs);
321b5fca8f8Stomee ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
322b5fca8f8Stomee return (KMEM_CBRC_LATER);
323b5fca8f8Stomee }
324a66b2b35STom Erickson
325b5fca8f8Stomee /* Only move znodes that are referenced _only_ by the DNLC. */
326b5fca8f8Stomee if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
327b5fca8f8Stomee mutex_exit(&vp->v_lock);
328a66b2b35STom Erickson ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
329b5fca8f8Stomee mutex_exit(&zfsvfs->z_znodes_lock);
330b5fca8f8Stomee ZFS_EXIT(zfsvfs);
331a66b2b35STom Erickson ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
332b5fca8f8Stomee return (KMEM_CBRC_LATER);
333b5fca8f8Stomee }
334b5fca8f8Stomee
335b5fca8f8Stomee /*
336b5fca8f8Stomee * The znode is known and in a valid state to move. We're holding the
337b5fca8f8Stomee * locks needed to execute the critical section.
338b5fca8f8Stomee */
339b5fca8f8Stomee zfs_znode_move_impl(ozp, nzp);
340b5fca8f8Stomee mutex_exit(&vp->v_lock);
341a66b2b35STom Erickson ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
342b5fca8f8Stomee
343b5fca8f8Stomee list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
344b5fca8f8Stomee mutex_exit(&zfsvfs->z_znodes_lock);
345b5fca8f8Stomee ZFS_EXIT(zfsvfs);
346b5fca8f8Stomee
347b5fca8f8Stomee return (KMEM_CBRC_YES);
348fa9e4066Sahrens }
349fa9e4066Sahrens
350fa9e4066Sahrens void
zfs_znode_init(void)351fa9e4066Sahrens zfs_znode_init(void)
352fa9e4066Sahrens {
353fa9e4066Sahrens /*
354fa9e4066Sahrens * Initialize zcache
355fa9e4066Sahrens */
3564e9583b2STom Erickson rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
357fa9e4066Sahrens ASSERT(znode_cache == NULL);
358fa9e4066Sahrens znode_cache = kmem_cache_create("zfs_znode_cache",
359fa9e4066Sahrens sizeof (znode_t), 0, zfs_znode_cache_constructor,
360fa9e4066Sahrens zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
361b5fca8f8Stomee kmem_cache_set_move(znode_cache, zfs_znode_move);
362fa9e4066Sahrens }
363fa9e4066Sahrens
364fa9e4066Sahrens void
zfs_znode_fini(void)365fa9e4066Sahrens zfs_znode_fini(void)
366fa9e4066Sahrens {
367fa9e4066Sahrens /*
368fa9e4066Sahrens * Cleanup vfs & vnode ops
369fa9e4066Sahrens */
370fa9e4066Sahrens zfs_remove_op_tables();
371fa9e4066Sahrens
372fa9e4066Sahrens /*
373fa9e4066Sahrens * Cleanup zcache
374fa9e4066Sahrens */
375fa9e4066Sahrens if (znode_cache)
376fa9e4066Sahrens kmem_cache_destroy(znode_cache);
377fa9e4066Sahrens znode_cache = NULL;
3784e9583b2STom Erickson rw_destroy(&zfsvfs_lock);
379fa9e4066Sahrens }
380fa9e4066Sahrens
381fa9e4066Sahrens struct vnodeops *zfs_dvnodeops;
382fa9e4066Sahrens struct vnodeops *zfs_fvnodeops;
383fa9e4066Sahrens struct vnodeops *zfs_symvnodeops;
384fa9e4066Sahrens struct vnodeops *zfs_xdvnodeops;
385fa9e4066Sahrens struct vnodeops *zfs_evnodeops;
386743a77edSAlan Wright struct vnodeops *zfs_sharevnodeops;
387fa9e4066Sahrens
388fa9e4066Sahrens void
zfs_remove_op_tables()389fa9e4066Sahrens zfs_remove_op_tables()
390fa9e4066Sahrens {
391fa9e4066Sahrens /*
392fa9e4066Sahrens * Remove vfs ops
393fa9e4066Sahrens */
394fa9e4066Sahrens ASSERT(zfsfstype);
395fa9e4066Sahrens (void) vfs_freevfsops_by_type(zfsfstype);
396fa9e4066Sahrens zfsfstype = 0;
397fa9e4066Sahrens
398fa9e4066Sahrens /*
399fa9e4066Sahrens * Remove vnode ops
400fa9e4066Sahrens */
401fa9e4066Sahrens if (zfs_dvnodeops)
402fa9e4066Sahrens vn_freevnodeops(zfs_dvnodeops);
403fa9e4066Sahrens if (zfs_fvnodeops)
404fa9e4066Sahrens vn_freevnodeops(zfs_fvnodeops);
405fa9e4066Sahrens if (zfs_symvnodeops)
406fa9e4066Sahrens vn_freevnodeops(zfs_symvnodeops);
407fa9e4066Sahrens if (zfs_xdvnodeops)
408fa9e4066Sahrens vn_freevnodeops(zfs_xdvnodeops);
409fa9e4066Sahrens if (zfs_evnodeops)
410fa9e4066Sahrens vn_freevnodeops(zfs_evnodeops);
411743a77edSAlan Wright if (zfs_sharevnodeops)
412743a77edSAlan Wright vn_freevnodeops(zfs_sharevnodeops);
413fa9e4066Sahrens
414fa9e4066Sahrens zfs_dvnodeops = NULL;
415fa9e4066Sahrens zfs_fvnodeops = NULL;
416fa9e4066Sahrens zfs_symvnodeops = NULL;
417fa9e4066Sahrens zfs_xdvnodeops = NULL;
418fa9e4066Sahrens zfs_evnodeops = NULL;
419743a77edSAlan Wright zfs_sharevnodeops = NULL;
420fa9e4066Sahrens }
421fa9e4066Sahrens
422fa9e4066Sahrens extern const fs_operation_def_t zfs_dvnodeops_template[];
423fa9e4066Sahrens extern const fs_operation_def_t zfs_fvnodeops_template[];
424fa9e4066Sahrens extern const fs_operation_def_t zfs_xdvnodeops_template[];
425fa9e4066Sahrens extern const fs_operation_def_t zfs_symvnodeops_template[];
426fa9e4066Sahrens extern const fs_operation_def_t zfs_evnodeops_template[];
427743a77edSAlan Wright extern const fs_operation_def_t zfs_sharevnodeops_template[];
428fa9e4066Sahrens
429fa9e4066Sahrens int
zfs_create_op_tables()430fa9e4066Sahrens zfs_create_op_tables()
431fa9e4066Sahrens {
432fa9e4066Sahrens int error;
433fa9e4066Sahrens
434fa9e4066Sahrens /*
435fa9e4066Sahrens * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
436fa9e4066Sahrens * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
437fa9e4066Sahrens * In this case we just return as the ops vectors are already set up.
438fa9e4066Sahrens */
439fa9e4066Sahrens if (zfs_dvnodeops)
440fa9e4066Sahrens return (0);
441fa9e4066Sahrens
442fa9e4066Sahrens error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
443fa9e4066Sahrens &zfs_dvnodeops);
444fa9e4066Sahrens if (error)
445fa9e4066Sahrens return (error);
446fa9e4066Sahrens
447fa9e4066Sahrens error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
448fa9e4066Sahrens &zfs_fvnodeops);
449fa9e4066Sahrens if (error)
450fa9e4066Sahrens return (error);
451fa9e4066Sahrens
452fa9e4066Sahrens error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
453fa9e4066Sahrens &zfs_symvnodeops);
454fa9e4066Sahrens if (error)
455fa9e4066Sahrens return (error);
456fa9e4066Sahrens
457fa9e4066Sahrens error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
458fa9e4066Sahrens &zfs_xdvnodeops);
459fa9e4066Sahrens if (error)
460fa9e4066Sahrens return (error);
461fa9e4066Sahrens
462fa9e4066Sahrens error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
463fa9e4066Sahrens &zfs_evnodeops);
464743a77edSAlan Wright if (error)
465743a77edSAlan Wright return (error);
466743a77edSAlan Wright
467743a77edSAlan Wright error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
468743a77edSAlan Wright &zfs_sharevnodeops);
469743a77edSAlan Wright
470743a77edSAlan Wright return (error);
471743a77edSAlan Wright }
472743a77edSAlan Wright
4739e1320c0SMark Shellenbaum int
zfs_create_share_dir(zfsvfs_t * zfsvfs,dmu_tx_t * tx)474743a77edSAlan Wright zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
475743a77edSAlan Wright {
47689459e17SMark Shellenbaum zfs_acl_ids_t acl_ids;
477743a77edSAlan Wright vattr_t vattr;
478743a77edSAlan Wright znode_t *sharezp;
479743a77edSAlan Wright vnode_t *vp;
480743a77edSAlan Wright znode_t *zp;
481743a77edSAlan Wright int error;
482743a77edSAlan Wright
483743a77edSAlan Wright vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
484743a77edSAlan Wright vattr.va_type = VDIR;
485743a77edSAlan Wright vattr.va_mode = S_IFDIR|0555;
486743a77edSAlan Wright vattr.va_uid = crgetuid(kcred);
487743a77edSAlan Wright vattr.va_gid = crgetgid(kcred);
488743a77edSAlan Wright
489743a77edSAlan Wright sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
490744947dcSTom Erickson ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
491744947dcSTom Erickson sharezp->z_moved = 0;
492743a77edSAlan Wright sharezp->z_unlinked = 0;
493743a77edSAlan Wright sharezp->z_atime_dirty = 0;
494743a77edSAlan Wright sharezp->z_zfsvfs = zfsvfs;
4950a586ceaSMark Shellenbaum sharezp->z_is_sa = zfsvfs->z_use_sa;
496743a77edSAlan Wright
497743a77edSAlan Wright vp = ZTOV(sharezp);
498743a77edSAlan Wright vn_reinit(vp);
499743a77edSAlan Wright vp->v_type = VDIR;
500743a77edSAlan Wright
50189459e17SMark Shellenbaum VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
50289459e17SMark Shellenbaum kcred, NULL, &acl_ids));
5030a586ceaSMark Shellenbaum zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
504743a77edSAlan Wright ASSERT3P(zp, ==, sharezp);
505743a77edSAlan Wright ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
506743a77edSAlan Wright POINTER_INVALIDATE(&sharezp->z_zfsvfs);
507743a77edSAlan Wright error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
508743a77edSAlan Wright ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
509743a77edSAlan Wright zfsvfs->z_shares_dir = sharezp->z_id;
510743a77edSAlan Wright
51189459e17SMark Shellenbaum zfs_acl_ids_free(&acl_ids);
512743a77edSAlan Wright ZTOV(sharezp)->v_count = 0;
5130a586ceaSMark Shellenbaum sa_handle_destroy(sharezp->z_sa_hdl);
514743a77edSAlan Wright kmem_cache_free(znode_cache, sharezp);
515fa9e4066Sahrens
516fa9e4066Sahrens return (error);
517fa9e4066Sahrens }
518fa9e4066Sahrens
519fa9e4066Sahrens /*
52072fc53bcSmarks * define a couple of values we need available
52172fc53bcSmarks * for both 64 and 32 bit environments.
52272fc53bcSmarks */
52372fc53bcSmarks #ifndef NBITSMINOR64
52472fc53bcSmarks #define NBITSMINOR64 32
52572fc53bcSmarks #endif
52672fc53bcSmarks #ifndef MAXMAJ64
52772fc53bcSmarks #define MAXMAJ64 0xffffffffUL
52872fc53bcSmarks #endif
52972fc53bcSmarks #ifndef MAXMIN64
53072fc53bcSmarks #define MAXMIN64 0xffffffffUL
53172fc53bcSmarks #endif
53272fc53bcSmarks
53372fc53bcSmarks /*
53472fc53bcSmarks * Create special expldev for ZFS private use.
53572fc53bcSmarks * Can't use standard expldev since it doesn't do
53672fc53bcSmarks * what we want. The standard expldev() takes a
53772fc53bcSmarks * dev32_t in LP64 and expands it to a long dev_t.
53872fc53bcSmarks * We need an interface that takes a dev32_t in ILP32
53972fc53bcSmarks * and expands it to a long dev_t.
54072fc53bcSmarks */
54172fc53bcSmarks static uint64_t
zfs_expldev(dev_t dev)54272fc53bcSmarks zfs_expldev(dev_t dev)
54372fc53bcSmarks {
54472fc53bcSmarks #ifndef _LP64
54572fc53bcSmarks major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
54672fc53bcSmarks return (((uint64_t)major << NBITSMINOR64) |
54772fc53bcSmarks ((minor_t)dev & MAXMIN32));
54872fc53bcSmarks #else
54972fc53bcSmarks return (dev);
55072fc53bcSmarks #endif
55172fc53bcSmarks }
55272fc53bcSmarks
55372fc53bcSmarks /*
55472fc53bcSmarks * Special cmpldev for ZFS private use.
55572fc53bcSmarks * Can't use standard cmpldev since it takes
55672fc53bcSmarks * a long dev_t and compresses it to dev32_t in
55772fc53bcSmarks * LP64. We need to do a compaction of a long dev_t
55872fc53bcSmarks * to a dev32_t in ILP32.
55972fc53bcSmarks */
56072fc53bcSmarks dev_t
zfs_cmpldev(uint64_t dev)56172fc53bcSmarks zfs_cmpldev(uint64_t dev)
56272fc53bcSmarks {
56372fc53bcSmarks #ifndef _LP64
56472fc53bcSmarks minor_t minor = (minor_t)dev & MAXMIN64;
56572fc53bcSmarks major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
56672fc53bcSmarks
56772fc53bcSmarks if (major > MAXMAJ32 || minor > MAXMIN32)
56872fc53bcSmarks return (NODEV32);
56972fc53bcSmarks
57072fc53bcSmarks return (((dev32_t)major << NBITSMINOR32) | minor);
57172fc53bcSmarks #else
57272fc53bcSmarks return (dev);
57372fc53bcSmarks #endif
57472fc53bcSmarks }
57572fc53bcSmarks
5764ccbb6e7Sahrens static void
zfs_znode_sa_init(zfsvfs_t * zfsvfs,znode_t * zp,dmu_buf_t * db,dmu_object_type_t obj_type,sa_handle_t * sa_hdl)5770a586ceaSMark Shellenbaum zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
5780a586ceaSMark Shellenbaum dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
5794ccbb6e7Sahrens {
580b5fca8f8Stomee ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
581b5fca8f8Stomee ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
5824ccbb6e7Sahrens
5834ccbb6e7Sahrens mutex_enter(&zp->z_lock);
5844ccbb6e7Sahrens
5850a586ceaSMark Shellenbaum ASSERT(zp->z_sa_hdl == NULL);
5866638ae1dSMark Shellenbaum ASSERT(zp->z_acl_cached == NULL);
5870a586ceaSMark Shellenbaum if (sa_hdl == NULL) {
5880a586ceaSMark Shellenbaum VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
5890a586ceaSMark Shellenbaum SA_HDL_SHARED, &zp->z_sa_hdl));
5900a586ceaSMark Shellenbaum } else {
5910a586ceaSMark Shellenbaum zp->z_sa_hdl = sa_hdl;
5920a586ceaSMark Shellenbaum sa_set_userp(sa_hdl, zp);
5930a586ceaSMark Shellenbaum }
5944ccbb6e7Sahrens
5950a586ceaSMark Shellenbaum zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
5964ccbb6e7Sahrens
5974ccbb6e7Sahrens /*
5984ccbb6e7Sahrens * Slap on VROOT if we are the root znode
5994ccbb6e7Sahrens */
6004ccbb6e7Sahrens if (zp->z_id == zfsvfs->z_root)
6014ccbb6e7Sahrens ZTOV(zp)->v_flag |= VROOT;
6024ccbb6e7Sahrens
6034ccbb6e7Sahrens mutex_exit(&zp->z_lock);
6044ccbb6e7Sahrens vn_exists(ZTOV(zp));
6054ccbb6e7Sahrens }
6064ccbb6e7Sahrens
607874395d5Smaybee void
zfs_znode_dmu_fini(znode_t * zp)6084ccbb6e7Sahrens zfs_znode_dmu_fini(znode_t *zp)
6094ccbb6e7Sahrens {
610b5fca8f8Stomee ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
611b5fca8f8Stomee zp->z_unlinked ||
612874395d5Smaybee RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
6130a586ceaSMark Shellenbaum
6140a586ceaSMark Shellenbaum sa_handle_destroy(zp->z_sa_hdl);
6150a586ceaSMark Shellenbaum zp->z_sa_hdl = NULL;
6164ccbb6e7Sahrens }
6174ccbb6e7Sahrens
61872fc53bcSmarks /*
619fa9e4066Sahrens * Construct a new znode/vnode and intialize.
620fa9e4066Sahrens *
621fa9e4066Sahrens * This does not do a call to dmu_set_user() that is
622fa9e4066Sahrens * up to the caller to do, in case you don't want to
623fa9e4066Sahrens * return the znode
624fa9e4066Sahrens */
625ea8dc4b6Seschrock static znode_t *
zfs_znode_alloc(zfsvfs_t * zfsvfs,dmu_buf_t * db,int blksz,dmu_object_type_t obj_type,sa_handle_t * hdl)6260a586ceaSMark Shellenbaum zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
6270a586ceaSMark Shellenbaum dmu_object_type_t obj_type, sa_handle_t *hdl)
628fa9e4066Sahrens {
629fa9e4066Sahrens znode_t *zp;
630fa9e4066Sahrens vnode_t *vp;
6310a586ceaSMark Shellenbaum uint64_t mode;
6320a586ceaSMark Shellenbaum uint64_t parent;
6330a586ceaSMark Shellenbaum sa_bulk_attr_t bulk[9];
6340a586ceaSMark Shellenbaum int count = 0;
635fa9e4066Sahrens
636fa9e4066Sahrens zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
637fa9e4066Sahrens
638fa9e4066Sahrens ASSERT(zp->z_dirlocks == NULL);
639b5fca8f8Stomee ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
640744947dcSTom Erickson zp->z_moved = 0;
641fa9e4066Sahrens
642b5fca8f8Stomee /*
643b5fca8f8Stomee * Defer setting z_zfsvfs until the znode is ready to be a candidate for
644b5fca8f8Stomee * the zfs_znode_move() callback.
645b5fca8f8Stomee */
6460a586ceaSMark Shellenbaum zp->z_sa_hdl = NULL;
647893a6d32Sahrens zp->z_unlinked = 0;
648fa9e4066Sahrens zp->z_atime_dirty = 0;
649fa9e4066Sahrens zp->z_mapcnt = 0;
6504ccbb6e7Sahrens zp->z_id = db->db_object;
651fa9e4066Sahrens zp->z_blksz = blksz;
652fa9e4066Sahrens zp->z_seq = 0x7A4653;
65367bd71c6Sperrin zp->z_sync_cnt = 0;
65437c9dfdcSAndreas Jaekel zp->z_new_content = 0;
6554ccbb6e7Sahrens
6564ccbb6e7Sahrens vp = ZTOV(zp);
6574ccbb6e7Sahrens vn_reinit(vp);
6584ccbb6e7Sahrens
6590a586ceaSMark Shellenbaum zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
6604ccbb6e7Sahrens
6610a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
6620a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
6630a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
6640a586ceaSMark Shellenbaum &zp->z_size, 8);
6650a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
6660a586ceaSMark Shellenbaum &zp->z_links, 8);
6670a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
6680a586ceaSMark Shellenbaum &zp->z_pflags, 8);
6690a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
6700a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
6710a586ceaSMark Shellenbaum &zp->z_atime, 16);
6720a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
673f1696b23SMark Shellenbaum &zp->z_uid, 8);
6740a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
675f1696b23SMark Shellenbaum &zp->z_gid, 8);
676fa9e4066Sahrens
6770a586ceaSMark Shellenbaum if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
6780a586ceaSMark Shellenbaum if (hdl == NULL)
6790a586ceaSMark Shellenbaum sa_handle_destroy(zp->z_sa_hdl);
6800a586ceaSMark Shellenbaum kmem_cache_free(znode_cache, zp);
6810a586ceaSMark Shellenbaum return (NULL);
6820a586ceaSMark Shellenbaum }
6830a586ceaSMark Shellenbaum
6840a586ceaSMark Shellenbaum zp->z_mode = mode;
685fa9e4066Sahrens vp->v_vfsp = zfsvfs->z_parent->z_vfs;
6860a586ceaSMark Shellenbaum
6870a586ceaSMark Shellenbaum vp->v_type = IFTOVT((mode_t)mode);
688fa9e4066Sahrens
689fa9e4066Sahrens switch (vp->v_type) {
690fa9e4066Sahrens case VDIR:
6910a586ceaSMark Shellenbaum if (zp->z_pflags & ZFS_XATTR) {
692fa9e4066Sahrens vn_setops(vp, zfs_xdvnodeops);
693fa9e4066Sahrens vp->v_flag |= V_XATTRDIR;
6944ccbb6e7Sahrens } else {
695fa9e4066Sahrens vn_setops(vp, zfs_dvnodeops);
6964ccbb6e7Sahrens }
6977f6e3e7dSperrin zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
698fa9e4066Sahrens break;
699fa9e4066Sahrens case VBLK:
700fa9e4066Sahrens case VCHR:
7010a586ceaSMark Shellenbaum {
7020a586ceaSMark Shellenbaum uint64_t rdev;
7030a586ceaSMark Shellenbaum VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
7040a586ceaSMark Shellenbaum &rdev, sizeof (rdev)) == 0);
7050a586ceaSMark Shellenbaum
7060a586ceaSMark Shellenbaum vp->v_rdev = zfs_cmpldev(rdev);
7070a586ceaSMark Shellenbaum }
708fa9e4066Sahrens /*FALLTHROUGH*/
709fa9e4066Sahrens case VFIFO:
710fa9e4066Sahrens case VSOCK:
711fa9e4066Sahrens case VDOOR:
712fa9e4066Sahrens vn_setops(vp, zfs_fvnodeops);
713fa9e4066Sahrens break;
714fa9e4066Sahrens case VREG:
715fa9e4066Sahrens vp->v_flag |= VMODSORT;
7160a586ceaSMark Shellenbaum if (parent == zfsvfs->z_shares_dir) {
717f1696b23SMark Shellenbaum ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
718743a77edSAlan Wright vn_setops(vp, zfs_sharevnodeops);
7190a586ceaSMark Shellenbaum } else {
720fa9e4066Sahrens vn_setops(vp, zfs_fvnodeops);
7210a586ceaSMark Shellenbaum }
722fa9e4066Sahrens break;
723fa9e4066Sahrens case VLNK:
724fa9e4066Sahrens vn_setops(vp, zfs_symvnodeops);
725fa9e4066Sahrens break;
726fa9e4066Sahrens default:
727fa9e4066Sahrens vn_setops(vp, zfs_evnodeops);
728fa9e4066Sahrens break;
729fa9e4066Sahrens }
730fa9e4066Sahrens
731b5fca8f8Stomee mutex_enter(&zfsvfs->z_znodes_lock);
732b5fca8f8Stomee list_insert_tail(&zfsvfs->z_all_znodes, zp);
733b5fca8f8Stomee membar_producer();
734b5fca8f8Stomee /*
735b5fca8f8Stomee * Everything else must be valid before assigning z_zfsvfs makes the
736b5fca8f8Stomee * znode eligible for zfs_znode_move().
737b5fca8f8Stomee */
738b5fca8f8Stomee zp->z_zfsvfs = zfsvfs;
739b5fca8f8Stomee mutex_exit(&zfsvfs->z_znodes_lock);
740b5fca8f8Stomee
741fa9e4066Sahrens VFS_HOLD(zfsvfs->z_vfs);
7424ccbb6e7Sahrens return (zp);
743fa9e4066Sahrens }
744fa9e4066Sahrens
7450a586ceaSMark Shellenbaum static uint64_t empty_xattr;
7460a586ceaSMark Shellenbaum static uint64_t pad[4];
7470a586ceaSMark Shellenbaum static zfs_acl_phys_t acl_phys;
748fa9e4066Sahrens /*
749fa9e4066Sahrens * Create a new DMU object to hold a zfs znode.
750fa9e4066Sahrens *
751fa9e4066Sahrens * IN: dzp - parent directory for new znode
752fa9e4066Sahrens * vap - file attributes for new znode
753fa9e4066Sahrens * tx - dmu transaction id for zap operations
754fa9e4066Sahrens * cr - credentials of caller
755fa9e4066Sahrens * flag - flags:
756fa9e4066Sahrens * IS_ROOT_NODE - new object will be root
757fa9e4066Sahrens * IS_XATTR - new object is an attribute
758da6c28aaSamw * bonuslen - length of bonus buffer
759da6c28aaSamw * setaclp - File/Dir initial ACL
760da6c28aaSamw * fuidp - Tracks fuid allocation.
761fa9e4066Sahrens *
7624ccbb6e7Sahrens * OUT: zpp - allocated znode
763fa9e4066Sahrens *
764fa9e4066Sahrens */
765fa9e4066Sahrens void
zfs_mknode(znode_t * dzp,vattr_t * vap,dmu_tx_t * tx,cred_t * cr,uint_t flag,znode_t ** zpp,zfs_acl_ids_t * acl_ids)7664ccbb6e7Sahrens zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
7670a586ceaSMark Shellenbaum uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
768fa9e4066Sahrens {
7690a586ceaSMark Shellenbaum uint64_t crtime[2], atime[2], mtime[2], ctime[2];
7700a586ceaSMark Shellenbaum uint64_t mode, size, links, parent, pflags;
7710a586ceaSMark Shellenbaum uint64_t dzp_pflags = 0;
7720a586ceaSMark Shellenbaum uint64_t rdev = 0;
773fa9e4066Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
7740a586ceaSMark Shellenbaum dmu_buf_t *db;
775fa9e4066Sahrens timestruc_t now;
7764ccbb6e7Sahrens uint64_t gen, obj;
7770a586ceaSMark Shellenbaum int bonuslen;
7780a586ceaSMark Shellenbaum sa_handle_t *sa_hdl;
7790a586ceaSMark Shellenbaum dmu_object_type_t obj_type;
7800a586ceaSMark Shellenbaum sa_bulk_attr_t sa_attrs[ZPL_END];
7810a586ceaSMark Shellenbaum int cnt = 0;
7820a586ceaSMark Shellenbaum zfs_acl_locator_cb_t locate = { 0 };
783fa9e4066Sahrens
784fa9e4066Sahrens ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
785fa9e4066Sahrens
7861209a471SNeil Perrin if (zfsvfs->z_replay) {
7874ccbb6e7Sahrens obj = vap->va_nodeid;
788fa9e4066Sahrens now = vap->va_ctime; /* see zfs_replay_create() */
789fa9e4066Sahrens gen = vap->va_nblocks; /* ditto */
790fa9e4066Sahrens } else {
7914ccbb6e7Sahrens obj = 0;
792fa9e4066Sahrens gethrestime(&now);
793fa9e4066Sahrens gen = dmu_tx_get_txg(tx);
794fa9e4066Sahrens }
795fa9e4066Sahrens
7960a586ceaSMark Shellenbaum obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
7970a586ceaSMark Shellenbaum bonuslen = (obj_type == DMU_OT_SA) ?
7980a586ceaSMark Shellenbaum DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
7990a586ceaSMark Shellenbaum
800fa9e4066Sahrens /*
801fa9e4066Sahrens * Create a new DMU object.
802fa9e4066Sahrens */
803ea8dc4b6Seschrock /*
804ea8dc4b6Seschrock * There's currently no mechanism for pre-reading the blocks that will
805744947dcSTom Erickson * be needed to allocate a new object, so we accept the small chance
806ea8dc4b6Seschrock * that there will be an i/o error and we will fail one of the
807ea8dc4b6Seschrock * assertions below.
808ea8dc4b6Seschrock */
809fa9e4066Sahrens if (vap->va_type == VDIR) {
8104a1f0cc9SMark Shellenbaum if (zfsvfs->z_replay) {
81143466aaeSMax Grossman VERIFY0(zap_create_claim_norm(zfsvfs->z_os, obj,
812da6c28aaSamw zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
81343466aaeSMax Grossman obj_type, bonuslen, tx));
814fa9e4066Sahrens } else {
8154ccbb6e7Sahrens obj = zap_create_norm(zfsvfs->z_os,
816da6c28aaSamw zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
8170a586ceaSMark Shellenbaum obj_type, bonuslen, tx);
818fa9e4066Sahrens }
819fa9e4066Sahrens } else {
8204a1f0cc9SMark Shellenbaum if (zfsvfs->z_replay) {
82143466aaeSMax Grossman VERIFY0(dmu_object_claim(zfsvfs->z_os, obj,
822fa9e4066Sahrens DMU_OT_PLAIN_FILE_CONTENTS, 0,
82343466aaeSMax Grossman obj_type, bonuslen, tx));
824fa9e4066Sahrens } else {
8254ccbb6e7Sahrens obj = dmu_object_alloc(zfsvfs->z_os,
826fa9e4066Sahrens DMU_OT_PLAIN_FILE_CONTENTS, 0,
8270a586ceaSMark Shellenbaum obj_type, bonuslen, tx);
828fa9e4066Sahrens }
829fa9e4066Sahrens }
83059e7834dSMark Shellenbaum
83159e7834dSMark Shellenbaum ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
8320a586ceaSMark Shellenbaum VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
833fa9e4066Sahrens
834fa9e4066Sahrens /*
835fa9e4066Sahrens * If this is the root, fix up the half-initialized parent pointer
836fa9e4066Sahrens * to reference the just-allocated physical data area.
837fa9e4066Sahrens */
838fa9e4066Sahrens if (flag & IS_ROOT_NODE) {
8394ccbb6e7Sahrens dzp->z_id = obj;
8400a586ceaSMark Shellenbaum } else {
8410a586ceaSMark Shellenbaum dzp_pflags = dzp->z_pflags;
842fa9e4066Sahrens }
843fa9e4066Sahrens
844fa9e4066Sahrens /*
845fa9e4066Sahrens * If parent is an xattr, so am I.
846fa9e4066Sahrens */
8470a586ceaSMark Shellenbaum if (dzp_pflags & ZFS_XATTR) {
848fa9e4066Sahrens flag |= IS_XATTR;
849fa9e4066Sahrens }
850fa9e4066Sahrens
851da6c28aaSamw if (zfsvfs->z_use_fuids)
8520a586ceaSMark Shellenbaum pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
8530a586ceaSMark Shellenbaum else
8540a586ceaSMark Shellenbaum pflags = 0;
855da6c28aaSamw
856fa9e4066Sahrens if (vap->va_type == VDIR) {
8570a586ceaSMark Shellenbaum size = 2; /* contents ("." and "..") */
8580a586ceaSMark Shellenbaum links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
8590a586ceaSMark Shellenbaum } else {
8600a586ceaSMark Shellenbaum size = links = 0;
861fa9e4066Sahrens }
862fa9e4066Sahrens
8630a586ceaSMark Shellenbaum if (vap->va_type == VBLK || vap->va_type == VCHR) {
8640a586ceaSMark Shellenbaum rdev = zfs_expldev(vap->va_rdev);
8650a586ceaSMark Shellenbaum }
8660a586ceaSMark Shellenbaum
8670a586ceaSMark Shellenbaum parent = dzp->z_id;
8680a586ceaSMark Shellenbaum mode = acl_ids->z_mode;
869fa9e4066Sahrens if (flag & IS_XATTR)
8700a586ceaSMark Shellenbaum pflags |= ZFS_XATTR;
871fa9e4066Sahrens
8720a586ceaSMark Shellenbaum /*
8730a586ceaSMark Shellenbaum * No execs denied will be deterimed when zfs_mode_compute() is called.
8740a586ceaSMark Shellenbaum */
8750a586ceaSMark Shellenbaum pflags |= acl_ids->z_aclp->z_hints &
8760a586ceaSMark Shellenbaum (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
8770a586ceaSMark Shellenbaum ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
878fa9e4066Sahrens
8790a586ceaSMark Shellenbaum ZFS_TIME_ENCODE(&now, crtime);
8800a586ceaSMark Shellenbaum ZFS_TIME_ENCODE(&now, ctime);
881fa9e4066Sahrens
882fa9e4066Sahrens if (vap->va_mask & AT_ATIME) {
8830a586ceaSMark Shellenbaum ZFS_TIME_ENCODE(&vap->va_atime, atime);
884fa9e4066Sahrens } else {
8850a586ceaSMark Shellenbaum ZFS_TIME_ENCODE(&now, atime);
886fa9e4066Sahrens }
887fa9e4066Sahrens
888fa9e4066Sahrens if (vap->va_mask & AT_MTIME) {
8890a586ceaSMark Shellenbaum ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
890fa9e4066Sahrens } else {
8910a586ceaSMark Shellenbaum ZFS_TIME_ENCODE(&now, mtime);
892fa9e4066Sahrens }
8930a586ceaSMark Shellenbaum
8940a586ceaSMark Shellenbaum /* Now add in all of the "SA" attributes */
8950a586ceaSMark Shellenbaum VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
8960a586ceaSMark Shellenbaum &sa_hdl));
8970a586ceaSMark Shellenbaum
8980a586ceaSMark Shellenbaum /*
8990a586ceaSMark Shellenbaum * Setup the array of attributes to be replaced/set on the new file
9000a586ceaSMark Shellenbaum *
9010a586ceaSMark Shellenbaum * order for DMU_OT_ZNODE is critical since it needs to be constructed
9020a586ceaSMark Shellenbaum * in the old znode_phys_t format. Don't change this ordering
9030a586ceaSMark Shellenbaum */
9040a586ceaSMark Shellenbaum
9050a586ceaSMark Shellenbaum if (obj_type == DMU_OT_ZNODE) {
9060a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
9070a586ceaSMark Shellenbaum NULL, &atime, 16);
9080a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
9090a586ceaSMark Shellenbaum NULL, &mtime, 16);
9100a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
9110a586ceaSMark Shellenbaum NULL, &ctime, 16);
9120a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
9130a586ceaSMark Shellenbaum NULL, &crtime, 16);
9140a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
9150a586ceaSMark Shellenbaum NULL, &gen, 8);
9160a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
9170a586ceaSMark Shellenbaum NULL, &mode, 8);
9180a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
9190a586ceaSMark Shellenbaum NULL, &size, 8);
9200a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
9210a586ceaSMark Shellenbaum NULL, &parent, 8);
9220a586ceaSMark Shellenbaum } else {
9230a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
9240a586ceaSMark Shellenbaum NULL, &mode, 8);
9250a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
9260a586ceaSMark Shellenbaum NULL, &size, 8);
9270a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
9280a586ceaSMark Shellenbaum NULL, &gen, 8);
9290a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
9300a586ceaSMark Shellenbaum &acl_ids->z_fuid, 8);
9310a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
9320a586ceaSMark Shellenbaum &acl_ids->z_fgid, 8);
9330a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
9340a586ceaSMark Shellenbaum NULL, &parent, 8);
9350a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
9360a586ceaSMark Shellenbaum NULL, &pflags, 8);
9370a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
9380a586ceaSMark Shellenbaum NULL, &atime, 16);
9390a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
9400a586ceaSMark Shellenbaum NULL, &mtime, 16);
9410a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
9420a586ceaSMark Shellenbaum NULL, &ctime, 16);
9430a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
9440a586ceaSMark Shellenbaum NULL, &crtime, 16);
9450a586ceaSMark Shellenbaum }
9460a586ceaSMark Shellenbaum
9470a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
9480a586ceaSMark Shellenbaum
9490a586ceaSMark Shellenbaum if (obj_type == DMU_OT_ZNODE) {
9500a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
9510a586ceaSMark Shellenbaum &empty_xattr, 8);
9520a586ceaSMark Shellenbaum }
9530a586ceaSMark Shellenbaum if (obj_type == DMU_OT_ZNODE ||
9540a586ceaSMark Shellenbaum (vap->va_type == VBLK || vap->va_type == VCHR)) {
9550a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
9560a586ceaSMark Shellenbaum NULL, &rdev, 8);
9570a586ceaSMark Shellenbaum
9580a586ceaSMark Shellenbaum }
9590a586ceaSMark Shellenbaum if (obj_type == DMU_OT_ZNODE) {
9600a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
9610a586ceaSMark Shellenbaum NULL, &pflags, 8);
9620a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
9630a586ceaSMark Shellenbaum &acl_ids->z_fuid, 8);
9640a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
9650a586ceaSMark Shellenbaum &acl_ids->z_fgid, 8);
9660a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
9670a586ceaSMark Shellenbaum sizeof (uint64_t) * 4);
9680a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
9690a586ceaSMark Shellenbaum &acl_phys, sizeof (zfs_acl_phys_t));
9700a586ceaSMark Shellenbaum } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
9710a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
9720a586ceaSMark Shellenbaum &acl_ids->z_aclp->z_acl_count, 8);
9730a586ceaSMark Shellenbaum locate.cb_aclp = acl_ids->z_aclp;
9740a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
9750a586ceaSMark Shellenbaum zfs_acl_data_locator, &locate,
9760a586ceaSMark Shellenbaum acl_ids->z_aclp->z_acl_bytes);
97727dd1e87SMark Shellenbaum mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
97827dd1e87SMark Shellenbaum acl_ids->z_fuid, acl_ids->z_fgid);
9790a586ceaSMark Shellenbaum }
9800a586ceaSMark Shellenbaum
9810a586ceaSMark Shellenbaum VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
9820a586ceaSMark Shellenbaum
983874395d5Smaybee if (!(flag & IS_ROOT_NODE)) {
9840a586ceaSMark Shellenbaum *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
9850a586ceaSMark Shellenbaum ASSERT(*zpp != NULL);
986874395d5Smaybee } else {
987874395d5Smaybee /*
988874395d5Smaybee * If we are creating the root node, the "parent" we
989874395d5Smaybee * passed in is the znode for the root.
990874395d5Smaybee */
991874395d5Smaybee *zpp = dzp;
99259e7834dSMark Shellenbaum
9930a586ceaSMark Shellenbaum (*zpp)->z_sa_hdl = sa_hdl;
9940a586ceaSMark Shellenbaum }
9950a586ceaSMark Shellenbaum
9960a586ceaSMark Shellenbaum (*zpp)->z_pflags = pflags;
9970a586ceaSMark Shellenbaum (*zpp)->z_mode = mode;
9980a586ceaSMark Shellenbaum
9998f2529deSMark Shellenbaum if (vap->va_mask & AT_XVATTR)
10008f2529deSMark Shellenbaum zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
10018f2529deSMark Shellenbaum
10020a586ceaSMark Shellenbaum if (obj_type == DMU_OT_ZNODE ||
10030a586ceaSMark Shellenbaum acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
100443466aaeSMax Grossman VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
10050a586ceaSMark Shellenbaum }
100659e7834dSMark Shellenbaum ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1007fa9e4066Sahrens }
1008fa9e4066Sahrens
10090a586ceaSMark Shellenbaum /*
1010f7170741SWill Andrews * Update in-core attributes. It is assumed the caller will be doing an
1011f7170741SWill Andrews * sa_bulk_update to push the changes out.
10120a586ceaSMark Shellenbaum */
1013da6c28aaSamw void
zfs_xvattr_set(znode_t * zp,xvattr_t * xvap,dmu_tx_t * tx)10140a586ceaSMark Shellenbaum zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1015da6c28aaSamw {
1016da6c28aaSamw xoptattr_t *xoap;
1017da6c28aaSamw
1018da6c28aaSamw xoap = xva_getxoptattr(xvap);
1019da6c28aaSamw ASSERT(xoap);
1020da6c28aaSamw
1021da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
10220a586ceaSMark Shellenbaum uint64_t times[2];
10230a586ceaSMark Shellenbaum ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
10240a586ceaSMark Shellenbaum (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
10250a586ceaSMark Shellenbaum ×, sizeof (times), tx);
1026da6c28aaSamw XVA_SET_RTN(xvap, XAT_CREATETIME);
1027da6c28aaSamw }
1028da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
10290a586ceaSMark Shellenbaum ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
10300a586ceaSMark Shellenbaum zp->z_pflags, tx);
1031da6c28aaSamw XVA_SET_RTN(xvap, XAT_READONLY);
1032da6c28aaSamw }
1033da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
10340a586ceaSMark Shellenbaum ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
10350a586ceaSMark Shellenbaum zp->z_pflags, tx);
1036da6c28aaSamw XVA_SET_RTN(xvap, XAT_HIDDEN);
1037da6c28aaSamw }
1038da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
10390a586ceaSMark Shellenbaum ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
10400a586ceaSMark Shellenbaum zp->z_pflags, tx);
1041da6c28aaSamw XVA_SET_RTN(xvap, XAT_SYSTEM);
1042da6c28aaSamw }
1043da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
10440a586ceaSMark Shellenbaum ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
10450a586ceaSMark Shellenbaum zp->z_pflags, tx);
1046da6c28aaSamw XVA_SET_RTN(xvap, XAT_ARCHIVE);
1047da6c28aaSamw }
1048da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
10490a586ceaSMark Shellenbaum ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
10500a586ceaSMark Shellenbaum zp->z_pflags, tx);
1051da6c28aaSamw XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1052da6c28aaSamw }
1053da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
10540a586ceaSMark Shellenbaum ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
10550a586ceaSMark Shellenbaum zp->z_pflags, tx);
1056da6c28aaSamw XVA_SET_RTN(xvap, XAT_NOUNLINK);
1057da6c28aaSamw }
1058da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
10590a586ceaSMark Shellenbaum ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
10600a586ceaSMark Shellenbaum zp->z_pflags, tx);
1061da6c28aaSamw XVA_SET_RTN(xvap, XAT_APPENDONLY);
1062da6c28aaSamw }
1063da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
10640a586ceaSMark Shellenbaum ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
10650a586ceaSMark Shellenbaum zp->z_pflags, tx);
1066da6c28aaSamw XVA_SET_RTN(xvap, XAT_NODUMP);
1067da6c28aaSamw }
1068da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
10690a586ceaSMark Shellenbaum ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
10700a586ceaSMark Shellenbaum zp->z_pflags, tx);
1071da6c28aaSamw XVA_SET_RTN(xvap, XAT_OPAQUE);
1072da6c28aaSamw }
1073da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1074da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
10750a586ceaSMark Shellenbaum xoap->xoa_av_quarantined, zp->z_pflags, tx);
1076da6c28aaSamw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1077da6c28aaSamw }
1078da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
10790a586ceaSMark Shellenbaum ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
10800a586ceaSMark Shellenbaum zp->z_pflags, tx);
1081da6c28aaSamw XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1082da6c28aaSamw }
1083da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
10840a586ceaSMark Shellenbaum zfs_sa_set_scanstamp(zp, xvap, tx);
1085da6c28aaSamw XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1086da6c28aaSamw }
10877a286c47SDai Ngo if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
10880a586ceaSMark Shellenbaum ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
10890a586ceaSMark Shellenbaum zp->z_pflags, tx);
10907a286c47SDai Ngo XVA_SET_RTN(xvap, XAT_REPARSE);
10917a286c47SDai Ngo }
1092fd9ee8b5Sjoyce mcintosh if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1093fd9ee8b5Sjoyce mcintosh ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1094fd9ee8b5Sjoyce mcintosh zp->z_pflags, tx);
1095fd9ee8b5Sjoyce mcintosh XVA_SET_RTN(xvap, XAT_OFFLINE);
1096fd9ee8b5Sjoyce mcintosh }
1097fd9ee8b5Sjoyce mcintosh if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1098fd9ee8b5Sjoyce mcintosh ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1099fd9ee8b5Sjoyce mcintosh zp->z_pflags, tx);
1100fd9ee8b5Sjoyce mcintosh XVA_SET_RTN(xvap, XAT_SPARSE);
1101fd9ee8b5Sjoyce mcintosh }
1102da6c28aaSamw }
1103da6c28aaSamw
1104fa9e4066Sahrens int
zfs_zget(zfsvfs_t * zfsvfs,uint64_t obj_num,znode_t ** zpp)1105fa9e4066Sahrens zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1106fa9e4066Sahrens {
1107fa9e4066Sahrens dmu_object_info_t doi;
1108fa9e4066Sahrens dmu_buf_t *db;
1109fa9e4066Sahrens znode_t *zp;
1110ea8dc4b6Seschrock int err;
11110a586ceaSMark Shellenbaum sa_handle_t *hdl;
1112fa9e4066Sahrens
1113fa9e4066Sahrens *zpp = NULL;
1114fa9e4066Sahrens
1115fa9e4066Sahrens ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1116fa9e4066Sahrens
11170a586ceaSMark Shellenbaum err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1118ea8dc4b6Seschrock if (err) {
1119fa9e4066Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1120ea8dc4b6Seschrock return (err);
1121fa9e4066Sahrens }
1122fa9e4066Sahrens
1123fa9e4066Sahrens dmu_object_info_from_db(db, &doi);
11240a586ceaSMark Shellenbaum if (doi.doi_bonus_type != DMU_OT_SA &&
11250a586ceaSMark Shellenbaum (doi.doi_bonus_type != DMU_OT_ZNODE ||
11260a586ceaSMark Shellenbaum (doi.doi_bonus_type == DMU_OT_ZNODE &&
11270a586ceaSMark Shellenbaum doi.doi_bonus_size < sizeof (znode_phys_t)))) {
11280a586ceaSMark Shellenbaum sa_buf_rele(db, NULL);
1129fa9e4066Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1130be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
1131fa9e4066Sahrens }
1132fa9e4066Sahrens
11330a586ceaSMark Shellenbaum hdl = dmu_buf_get_user(db);
11340a586ceaSMark Shellenbaum if (hdl != NULL) {
11350a586ceaSMark Shellenbaum zp = sa_get_userdata(hdl);
11360a586ceaSMark Shellenbaum
1137fa9e4066Sahrens
11384ccbb6e7Sahrens /*
11390a586ceaSMark Shellenbaum * Since "SA" does immediate eviction we
11400a586ceaSMark Shellenbaum * should never find a sa handle that doesn't
11410a586ceaSMark Shellenbaum * know about the znode.
11424ccbb6e7Sahrens */
11430a586ceaSMark Shellenbaum
11440a586ceaSMark Shellenbaum ASSERT3P(zp, !=, NULL);
11450a586ceaSMark Shellenbaum
11460a586ceaSMark Shellenbaum mutex_enter(&zp->z_lock);
1147fa9e4066Sahrens ASSERT3U(zp->z_id, ==, obj_num);
1148893a6d32Sahrens if (zp->z_unlinked) {
1149be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT);
1150fa9e4066Sahrens } else {
1151fa9e4066Sahrens VN_HOLD(ZTOV(zp));
11524ccbb6e7Sahrens *zpp = zp;
11534ccbb6e7Sahrens err = 0;
11544ccbb6e7Sahrens }
11550a586ceaSMark Shellenbaum sa_buf_rele(db, NULL);
1156fa9e4066Sahrens mutex_exit(&zp->z_lock);
1157ea8dc4b6Seschrock ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
11584ccbb6e7Sahrens return (err);
1159fa9e4066Sahrens }
1160fa9e4066Sahrens
1161fa9e4066Sahrens /*
1162fa9e4066Sahrens * Not found create new znode/vnode
116359e7834dSMark Shellenbaum * but only if file exists.
116459e7834dSMark Shellenbaum *
116559e7834dSMark Shellenbaum * There is a small window where zfs_vget() could
116659e7834dSMark Shellenbaum * find this object while a file create is still in
11670a586ceaSMark Shellenbaum * progress. This is checked for in zfs_znode_alloc()
11680a586ceaSMark Shellenbaum *
11690a586ceaSMark Shellenbaum * if zfs_znode_alloc() fails it will drop the hold on the
11700a586ceaSMark Shellenbaum * bonus buffer.
1171fa9e4066Sahrens */
11720a586ceaSMark Shellenbaum zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
11730a586ceaSMark Shellenbaum doi.doi_bonus_type, NULL);
11740a586ceaSMark Shellenbaum if (zp == NULL) {
1175be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT);
11760a586ceaSMark Shellenbaum } else {
1177*4f23c764SArne Jansen if (zp->z_links == 0)
1178*4f23c764SArne Jansen zp->z_unlinked = B_TRUE;
11790a586ceaSMark Shellenbaum *zpp = zp;
118059e7834dSMark Shellenbaum }
118159e7834dSMark Shellenbaum ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
118259e7834dSMark Shellenbaum return (err);
1183fa9e4066Sahrens }
1184fa9e4066Sahrens
1185f18faf3fSek110237 int
zfs_rezget(znode_t * zp)1186f18faf3fSek110237 zfs_rezget(znode_t *zp)
1187f18faf3fSek110237 {
1188f18faf3fSek110237 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1189f18faf3fSek110237 dmu_object_info_t doi;
1190f18faf3fSek110237 dmu_buf_t *db;
1191f18faf3fSek110237 uint64_t obj_num = zp->z_id;
11920a586ceaSMark Shellenbaum uint64_t mode;
11930a586ceaSMark Shellenbaum sa_bulk_attr_t bulk[8];
1194f18faf3fSek110237 int err;
11950a586ceaSMark Shellenbaum int count = 0;
11960a586ceaSMark Shellenbaum uint64_t gen;
1197f18faf3fSek110237
1198f18faf3fSek110237 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1199f18faf3fSek110237
12006638ae1dSMark Shellenbaum mutex_enter(&zp->z_acl_lock);
12016638ae1dSMark Shellenbaum if (zp->z_acl_cached) {
12026638ae1dSMark Shellenbaum zfs_acl_free(zp->z_acl_cached);
12036638ae1dSMark Shellenbaum zp->z_acl_cached = NULL;
12046638ae1dSMark Shellenbaum }
12056638ae1dSMark Shellenbaum
12060a586ceaSMark Shellenbaum mutex_exit(&zp->z_acl_lock);
12070a586ceaSMark Shellenbaum ASSERT(zp->z_sa_hdl == NULL);
12080a586ceaSMark Shellenbaum err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
12090a586ceaSMark Shellenbaum if (err) {
12100a586ceaSMark Shellenbaum ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
12110a586ceaSMark Shellenbaum return (err);
12120a586ceaSMark Shellenbaum }
12130a586ceaSMark Shellenbaum
12140a586ceaSMark Shellenbaum dmu_object_info_from_db(db, &doi);
12150a586ceaSMark Shellenbaum if (doi.doi_bonus_type != DMU_OT_SA &&
12160a586ceaSMark Shellenbaum (doi.doi_bonus_type != DMU_OT_ZNODE ||
12170a586ceaSMark Shellenbaum (doi.doi_bonus_type == DMU_OT_ZNODE &&
12180a586ceaSMark Shellenbaum doi.doi_bonus_size < sizeof (znode_phys_t)))) {
12190a586ceaSMark Shellenbaum sa_buf_rele(db, NULL);
12200a586ceaSMark Shellenbaum ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1221be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
12220a586ceaSMark Shellenbaum }
12230a586ceaSMark Shellenbaum
12240a586ceaSMark Shellenbaum zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
12250a586ceaSMark Shellenbaum
12260a586ceaSMark Shellenbaum /* reload cached values */
12270a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
12280a586ceaSMark Shellenbaum &gen, sizeof (gen));
12290a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
12300a586ceaSMark Shellenbaum &zp->z_size, sizeof (zp->z_size));
12310a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
12320a586ceaSMark Shellenbaum &zp->z_links, sizeof (zp->z_links));
12330a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
12340a586ceaSMark Shellenbaum &zp->z_pflags, sizeof (zp->z_pflags));
12350a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
12360a586ceaSMark Shellenbaum &zp->z_atime, sizeof (zp->z_atime));
12370a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1238f1696b23SMark Shellenbaum &zp->z_uid, sizeof (zp->z_uid));
12390a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1240f1696b23SMark Shellenbaum &zp->z_gid, sizeof (zp->z_gid));
12410a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
12420a586ceaSMark Shellenbaum &mode, sizeof (mode));
12430a586ceaSMark Shellenbaum
12440a586ceaSMark Shellenbaum if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
12450a586ceaSMark Shellenbaum zfs_znode_dmu_fini(zp);
12460a586ceaSMark Shellenbaum ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1247be6fd75aSMatthew Ahrens return (SET_ERROR(EIO));
12480a586ceaSMark Shellenbaum }
12490a586ceaSMark Shellenbaum
12501412a1a2SMark Shellenbaum zp->z_mode = mode;
12511412a1a2SMark Shellenbaum
12520a586ceaSMark Shellenbaum if (gen != zp->z_gen) {
12530a586ceaSMark Shellenbaum zfs_znode_dmu_fini(zp);
12540a586ceaSMark Shellenbaum ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1255be6fd75aSMatthew Ahrens return (SET_ERROR(EIO));
12560a586ceaSMark Shellenbaum }
12570a586ceaSMark Shellenbaum
12580a586ceaSMark Shellenbaum zp->z_unlinked = (zp->z_links == 0);
12596166ad1cSek110237 zp->z_blksz = doi.doi_data_block_size;
1260f18faf3fSek110237
1261f18faf3fSek110237 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1262f18faf3fSek110237
1263f18faf3fSek110237 return (0);
1264f18faf3fSek110237 }
1265f18faf3fSek110237
1266fa9e4066Sahrens void
zfs_znode_delete(znode_t * zp,dmu_tx_t * tx)1267fa9e4066Sahrens zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1268fa9e4066Sahrens {
1269fa9e4066Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1270cdb0ab79Smaybee objset_t *os = zfsvfs->z_os;
12714ccbb6e7Sahrens uint64_t obj = zp->z_id;
12721412a1a2SMark Shellenbaum uint64_t acl_obj = zfs_external_acl(zp);
1273fa9e4066Sahrens
12744ccbb6e7Sahrens ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
12751412a1a2SMark Shellenbaum if (acl_obj) {
12761412a1a2SMark Shellenbaum VERIFY(!zp->z_is_sa);
1277cdb0ab79Smaybee VERIFY(0 == dmu_object_free(os, acl_obj, tx));
12781412a1a2SMark Shellenbaum }
1279cdb0ab79Smaybee VERIFY(0 == dmu_object_free(os, obj, tx));
12804ccbb6e7Sahrens zfs_znode_dmu_fini(zp);
12814ccbb6e7Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1282874395d5Smaybee zfs_znode_free(zp);
1283fa9e4066Sahrens }
1284fa9e4066Sahrens
1285fa9e4066Sahrens void
zfs_zinactive(znode_t * zp)1286fa9e4066Sahrens zfs_zinactive(znode_t *zp)
1287fa9e4066Sahrens {
1288fa9e4066Sahrens vnode_t *vp = ZTOV(zp);
1289fa9e4066Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1290fa9e4066Sahrens uint64_t z_id = zp->z_id;
1291fa9e4066Sahrens
12920a586ceaSMark Shellenbaum ASSERT(zp->z_sa_hdl);
1293fa9e4066Sahrens
1294fa9e4066Sahrens /*
1295fa9e4066Sahrens * Don't allow a zfs_zget() while were trying to release this znode
1296fa9e4066Sahrens */
1297fa9e4066Sahrens ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1298fa9e4066Sahrens
1299fa9e4066Sahrens mutex_enter(&zp->z_lock);
1300fa9e4066Sahrens mutex_enter(&vp->v_lock);
1301fa9e4066Sahrens vp->v_count--;
1302fa9e4066Sahrens if (vp->v_count > 0 || vn_has_cached_data(vp)) {
1303fa9e4066Sahrens /*
1304fa9e4066Sahrens * If the hold count is greater than zero, somebody has
1305fa9e4066Sahrens * obtained a new reference on this znode while we were
1306fa9e4066Sahrens * processing it here, so we are done. If we still have
1307fa9e4066Sahrens * mapped pages then we are also done, since we don't
1308fa9e4066Sahrens * want to inactivate the znode until the pages get pushed.
1309fa9e4066Sahrens *
1310fa9e4066Sahrens * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1311fa9e4066Sahrens * this seems like it would leave the znode hanging with
1312fa9e4066Sahrens * no chance to go inactive...
1313fa9e4066Sahrens */
1314fa9e4066Sahrens mutex_exit(&vp->v_lock);
1315fa9e4066Sahrens mutex_exit(&zp->z_lock);
1316fa9e4066Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1317fa9e4066Sahrens return;
1318fa9e4066Sahrens }
1319fa9e4066Sahrens mutex_exit(&vp->v_lock);
1320fa9e4066Sahrens
1321fa9e4066Sahrens /*
1322fa9e4066Sahrens * If this was the last reference to a file with no links,
1323fa9e4066Sahrens * remove the file from the file system.
1324fa9e4066Sahrens */
1325893a6d32Sahrens if (zp->z_unlinked) {
1326fa9e4066Sahrens mutex_exit(&zp->z_lock);
1327fa9e4066Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1328fa9e4066Sahrens zfs_rmnode(zp);
1329fa9e4066Sahrens return;
1330fa9e4066Sahrens }
13310a586ceaSMark Shellenbaum
1332fa9e4066Sahrens mutex_exit(&zp->z_lock);
13334ccbb6e7Sahrens zfs_znode_dmu_fini(zp);
1334fa9e4066Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1335874395d5Smaybee zfs_znode_free(zp);
1336fa9e4066Sahrens }
1337fa9e4066Sahrens
1338fa9e4066Sahrens void
zfs_znode_free(znode_t * zp)1339fa9e4066Sahrens zfs_znode_free(znode_t *zp)
1340fa9e4066Sahrens {
1341fa9e4066Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1342fa9e4066Sahrens
1343874395d5Smaybee vn_invalid(ZTOV(zp));
1344874395d5Smaybee
1345b5fca8f8Stomee ASSERT(ZTOV(zp)->v_count == 0);
1346b5fca8f8Stomee
1347fa9e4066Sahrens mutex_enter(&zfsvfs->z_znodes_lock);
1348b5fca8f8Stomee POINTER_INVALIDATE(&zp->z_zfsvfs);
1349fa9e4066Sahrens list_remove(&zfsvfs->z_all_znodes, zp);
1350fa9e4066Sahrens mutex_exit(&zfsvfs->z_znodes_lock);
1351fa9e4066Sahrens
1352d47621a4STim Haley if (zp->z_acl_cached) {
1353d47621a4STim Haley zfs_acl_free(zp->z_acl_cached);
1354d47621a4STim Haley zp->z_acl_cached = NULL;
1355d47621a4STim Haley }
1356d47621a4STim Haley
1357fa9e4066Sahrens kmem_cache_free(znode_cache, zp);
1358874395d5Smaybee
1359874395d5Smaybee VFS_RELE(zfsvfs->z_vfs);
1360fa9e4066Sahrens }
1361fa9e4066Sahrens
1362fa9e4066Sahrens void
zfs_tstamp_update_setup(znode_t * zp,uint_t flag,uint64_t mtime[2],uint64_t ctime[2],boolean_t have_tx)13630a586ceaSMark Shellenbaum zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
13640a586ceaSMark Shellenbaum uint64_t ctime[2], boolean_t have_tx)
1365fa9e4066Sahrens {
1366fa9e4066Sahrens timestruc_t now;
1367fa9e4066Sahrens
1368fa9e4066Sahrens gethrestime(&now);
1369fa9e4066Sahrens
13700a586ceaSMark Shellenbaum if (have_tx) { /* will sa_bulk_update happen really soon? */
1371fa9e4066Sahrens zp->z_atime_dirty = 0;
1372fa9e4066Sahrens zp->z_seq++;
1373fa9e4066Sahrens } else {
1374fa9e4066Sahrens zp->z_atime_dirty = 1;
1375fa9e4066Sahrens }
1376fa9e4066Sahrens
13770a586ceaSMark Shellenbaum if (flag & AT_ATIME) {
13780a586ceaSMark Shellenbaum ZFS_TIME_ENCODE(&now, zp->z_atime);
13790a586ceaSMark Shellenbaum }
1380fa9e4066Sahrens
1381da6c28aaSamw if (flag & AT_MTIME) {
13820a586ceaSMark Shellenbaum ZFS_TIME_ENCODE(&now, mtime);
13830a586ceaSMark Shellenbaum if (zp->z_zfsvfs->z_use_fuids) {
13840a586ceaSMark Shellenbaum zp->z_pflags |= (ZFS_ARCHIVE |
13850a586ceaSMark Shellenbaum ZFS_AV_MODIFIED);
13860a586ceaSMark Shellenbaum }
1387da6c28aaSamw }
1388fa9e4066Sahrens
1389da6c28aaSamw if (flag & AT_CTIME) {
13900a586ceaSMark Shellenbaum ZFS_TIME_ENCODE(&now, ctime);
1391da6c28aaSamw if (zp->z_zfsvfs->z_use_fuids)
13920a586ceaSMark Shellenbaum zp->z_pflags |= ZFS_ARCHIVE;
1393da6c28aaSamw }
1394fa9e4066Sahrens }
1395fa9e4066Sahrens
1396fa9e4066Sahrens /*
1397104e2ed7Sperrin * Grow the block size for a file.
1398fa9e4066Sahrens *
1399fa9e4066Sahrens * IN: zp - znode of file to free data in.
1400fa9e4066Sahrens * size - requested block size
1401fa9e4066Sahrens * tx - open transaction.
1402fa9e4066Sahrens *
1403fa9e4066Sahrens * NOTE: this function assumes that the znode is write locked.
1404fa9e4066Sahrens */
1405104e2ed7Sperrin void
zfs_grow_blocksize(znode_t * zp,uint64_t size,dmu_tx_t * tx)1406fa9e4066Sahrens zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1407fa9e4066Sahrens {
1408fa9e4066Sahrens int error;
1409fa9e4066Sahrens u_longlong_t dummy;
1410fa9e4066Sahrens
1411fa9e4066Sahrens if (size <= zp->z_blksz)
1412104e2ed7Sperrin return;
1413fa9e4066Sahrens /*
1414fa9e4066Sahrens * If the file size is already greater than the current blocksize,
1415fa9e4066Sahrens * we will not grow. If there is more than one block in a file,
1416fa9e4066Sahrens * the blocksize cannot change.
1417fa9e4066Sahrens */
14180a586ceaSMark Shellenbaum if (zp->z_blksz && zp->z_size > zp->z_blksz)
1419104e2ed7Sperrin return;
1420fa9e4066Sahrens
1421fa9e4066Sahrens error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1422fa9e4066Sahrens size, 0, tx);
14230a586ceaSMark Shellenbaum
1424fa9e4066Sahrens if (error == ENOTSUP)
1425104e2ed7Sperrin return;
1426fb09f5aaSMadhav Suresh ASSERT0(error);
1427fa9e4066Sahrens
1428fa9e4066Sahrens /* What blocksize did we actually get? */
14290a586ceaSMark Shellenbaum dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1430fa9e4066Sahrens }
1431fa9e4066Sahrens
1432fa9e4066Sahrens /*
1433fa9e4066Sahrens * This is a dummy interface used when pvn_vplist_dirty() should *not*
1434fa9e4066Sahrens * be calling back into the fs for a putpage(). E.g.: when truncating
1435fa9e4066Sahrens * a file, the pages being "thrown away* don't need to be written out.
1436fa9e4066Sahrens */
1437fa9e4066Sahrens /* ARGSUSED */
1438fa9e4066Sahrens static int
zfs_no_putpage(vnode_t * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,cred_t * cr)1439fa9e4066Sahrens zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1440fa9e4066Sahrens int flags, cred_t *cr)
1441fa9e4066Sahrens {
1442fa9e4066Sahrens ASSERT(0);
1443fa9e4066Sahrens return (0);
1444fa9e4066Sahrens }
1445fa9e4066Sahrens
1446fa9e4066Sahrens /*
1447cdb0ab79Smaybee * Increase the file length
1448fa9e4066Sahrens *
1449fa9e4066Sahrens * IN: zp - znode of file to free data in.
1450cdb0ab79Smaybee * end - new end-of-file
1451fa9e4066Sahrens *
1452f7170741SWill Andrews * RETURN: 0 on success, error code on failure
145379ab756aSSimon Klinkert *
145479ab756aSSimon Klinkert * Assumption: File is already range-locked for changing zp_size
1455fa9e4066Sahrens */
1456cdb0ab79Smaybee static int
zfs_extend(znode_t * zp,uint64_t end)1457cdb0ab79Smaybee zfs_extend(znode_t *zp, uint64_t end)
1458fa9e4066Sahrens {
14595730cc9aSmaybee zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1460cdb0ab79Smaybee dmu_tx_t *tx;
1461cdb0ab79Smaybee uint64_t newblksz;
1462104e2ed7Sperrin int error;
1463fa9e4066Sahrens
1464fa9e4066Sahrens /*
1465fa9e4066Sahrens * Nothing to do if file already at desired length.
1466fa9e4066Sahrens */
146779ab756aSSimon Klinkert if (end <= zp->z_size)
1468fa9e4066Sahrens return (0);
146979ab756aSSimon Klinkert
14705730cc9aSmaybee tx = dmu_tx_create(zfsvfs->z_os);
14710a586ceaSMark Shellenbaum dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
14720a586ceaSMark Shellenbaum zfs_sa_upgrade_txholds(tx, zp);
1473cdb0ab79Smaybee if (end > zp->z_blksz &&
14745730cc9aSmaybee (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1475fa9e4066Sahrens /*
1476fa9e4066Sahrens * We are growing the file past the current block size.
1477fa9e4066Sahrens */
1478fa9e4066Sahrens if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1479b5152584SMatthew Ahrens /*
1480b5152584SMatthew Ahrens * File's blocksize is already larger than the
1481b5152584SMatthew Ahrens * "recordsize" property. Only let it grow to
1482b5152584SMatthew Ahrens * the next power of 2.
1483b5152584SMatthew Ahrens */
1484fa9e4066Sahrens ASSERT(!ISP2(zp->z_blksz));
1485b5152584SMatthew Ahrens newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1486fa9e4066Sahrens } else {
1487cdb0ab79Smaybee newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1488fa9e4066Sahrens }
1489cdb0ab79Smaybee dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1490cdb0ab79Smaybee } else {
1491cdb0ab79Smaybee newblksz = 0;
1492fa9e4066Sahrens }
14935730cc9aSmaybee
1494e722410cSMatthew Ahrens error = dmu_tx_assign(tx, TXG_WAIT);
14955730cc9aSmaybee if (error) {
1496cdb0ab79Smaybee dmu_tx_abort(tx);
14975730cc9aSmaybee return (error);
14985730cc9aSmaybee }
14995730cc9aSmaybee
1500cdb0ab79Smaybee if (newblksz)
1501cdb0ab79Smaybee zfs_grow_blocksize(zp, newblksz, tx);
15025730cc9aSmaybee
15030a586ceaSMark Shellenbaum zp->z_size = end;
15040a586ceaSMark Shellenbaum
15050a586ceaSMark Shellenbaum VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
15060a586ceaSMark Shellenbaum &zp->z_size, sizeof (zp->z_size), tx));
1507fa9e4066Sahrens
15085730cc9aSmaybee dmu_tx_commit(tx);
15095730cc9aSmaybee
1510cdb0ab79Smaybee return (0);
1511cdb0ab79Smaybee }
1512cdb0ab79Smaybee
1513cdb0ab79Smaybee /*
1514cdb0ab79Smaybee * Free space in a file.
1515cdb0ab79Smaybee *
1516cdb0ab79Smaybee * IN: zp - znode of file to free data in.
1517cdb0ab79Smaybee * off - start of section to free.
1518cdb0ab79Smaybee * len - length of section to free.
1519cdb0ab79Smaybee *
1520f7170741SWill Andrews * RETURN: 0 on success, error code on failure
152179ab756aSSimon Klinkert *
152279ab756aSSimon Klinkert * Assumption: file is already range-locked for changing zp_size
1523cdb0ab79Smaybee */
1524cdb0ab79Smaybee static int
zfs_free_range(znode_t * zp,uint64_t off,uint64_t len)1525cdb0ab79Smaybee zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1526cdb0ab79Smaybee {
1527cdb0ab79Smaybee zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1528cdb0ab79Smaybee int error;
1529cdb0ab79Smaybee
1530cdb0ab79Smaybee /*
1531cdb0ab79Smaybee * Nothing to do if file already at desired length.
1532cdb0ab79Smaybee */
153379ab756aSSimon Klinkert if (off >= zp->z_size)
1534cdb0ab79Smaybee return (0);
1535cdb0ab79Smaybee
15360a586ceaSMark Shellenbaum if (off + len > zp->z_size)
15370a586ceaSMark Shellenbaum len = zp->z_size - off;
1538cdb0ab79Smaybee
1539cdb0ab79Smaybee error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1540cdb0ab79Smaybee
1541cdb0ab79Smaybee return (error);
1542cdb0ab79Smaybee }
1543cdb0ab79Smaybee
1544cdb0ab79Smaybee /*
1545cdb0ab79Smaybee * Truncate a file
1546cdb0ab79Smaybee *
1547cdb0ab79Smaybee * IN: zp - znode of file to free data in.
1548cdb0ab79Smaybee * end - new end-of-file.
1549cdb0ab79Smaybee *
1550f7170741SWill Andrews * RETURN: 0 on success, error code on failure
155179ab756aSSimon Klinkert *
155279ab756aSSimon Klinkert * Assumption: file is already range-locked for changing zp_size
1553cdb0ab79Smaybee */
1554cdb0ab79Smaybee static int
zfs_trunc(znode_t * zp,uint64_t end)1555cdb0ab79Smaybee zfs_trunc(znode_t *zp, uint64_t end)
1556cdb0ab79Smaybee {
1557cdb0ab79Smaybee zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1558cdb0ab79Smaybee vnode_t *vp = ZTOV(zp);
1559cdb0ab79Smaybee dmu_tx_t *tx;
1560cdb0ab79Smaybee int error;
1561fd9ee8b5Sjoyce mcintosh sa_bulk_attr_t bulk[2];
1562fd9ee8b5Sjoyce mcintosh int count = 0;
1563cdb0ab79Smaybee
1564cdb0ab79Smaybee /*
1565cdb0ab79Smaybee * Nothing to do if file already at desired length.
1566cdb0ab79Smaybee */
156779ab756aSSimon Klinkert if (end >= zp->z_size)
1568cdb0ab79Smaybee return (0);
1569cdb0ab79Smaybee
1570cdb0ab79Smaybee error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1);
157179ab756aSSimon Klinkert if (error)
1572cdb0ab79Smaybee return (error);
157379ab756aSSimon Klinkert
1574cdb0ab79Smaybee tx = dmu_tx_create(zfsvfs->z_os);
15750a586ceaSMark Shellenbaum dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
15760a586ceaSMark Shellenbaum zfs_sa_upgrade_txholds(tx, zp);
15774bb73804SMatthew Ahrens dmu_tx_mark_netfree(tx);
1578e722410cSMatthew Ahrens error = dmu_tx_assign(tx, TXG_WAIT);
1579cdb0ab79Smaybee if (error) {
1580cdb0ab79Smaybee dmu_tx_abort(tx);
1581cdb0ab79Smaybee return (error);
1582cdb0ab79Smaybee }
1583cdb0ab79Smaybee
15840a586ceaSMark Shellenbaum zp->z_size = end;
1585fd9ee8b5Sjoyce mcintosh SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1586fd9ee8b5Sjoyce mcintosh NULL, &zp->z_size, sizeof (zp->z_size));
15870a586ceaSMark Shellenbaum
1588fd9ee8b5Sjoyce mcintosh if (end == 0) {
1589fd9ee8b5Sjoyce mcintosh zp->z_pflags &= ~ZFS_SPARSE;
1590fd9ee8b5Sjoyce mcintosh SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1591fd9ee8b5Sjoyce mcintosh NULL, &zp->z_pflags, 8);
1592fd9ee8b5Sjoyce mcintosh }
1593fd9ee8b5Sjoyce mcintosh VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1594cdb0ab79Smaybee
1595cdb0ab79Smaybee dmu_tx_commit(tx);
1596cdb0ab79Smaybee
15975730cc9aSmaybee /*
15985730cc9aSmaybee * Clear any mapped pages in the truncated region. This has to
15995730cc9aSmaybee * happen outside of the transaction to avoid the possibility of
16005730cc9aSmaybee * a deadlock with someone trying to push a page that we are
16015730cc9aSmaybee * about to invalidate.
16025730cc9aSmaybee */
1603cdb0ab79Smaybee if (vn_has_cached_data(vp)) {
16045730cc9aSmaybee page_t *pp;
1605cdb0ab79Smaybee uint64_t start = end & PAGEMASK;
1606cdb0ab79Smaybee int poff = end & PAGEOFFSET;
16075730cc9aSmaybee
16085730cc9aSmaybee if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
16095730cc9aSmaybee /*
16105730cc9aSmaybee * We need to zero a partial page.
16115730cc9aSmaybee */
16125730cc9aSmaybee pagezero(pp, poff, PAGESIZE - poff);
16135730cc9aSmaybee start += PAGESIZE;
16145730cc9aSmaybee page_unlock(pp);
16155730cc9aSmaybee }
16165730cc9aSmaybee error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
16175730cc9aSmaybee B_INVAL | B_TRUNC, NULL);
16185730cc9aSmaybee ASSERT(error == 0);
16195730cc9aSmaybee }
1620ac05c741SMark Maybee
1621fa9e4066Sahrens return (0);
1622fa9e4066Sahrens }
1623fa9e4066Sahrens
1624cdb0ab79Smaybee /*
1625cdb0ab79Smaybee * Free space in a file
1626cdb0ab79Smaybee *
1627cdb0ab79Smaybee * IN: zp - znode of file to free data in.
1628cdb0ab79Smaybee * off - start of range
1629cdb0ab79Smaybee * len - end of range (0 => EOF)
1630cdb0ab79Smaybee * flag - current file open mode flags.
1631cdb0ab79Smaybee * log - TRUE if this action should be logged
1632cdb0ab79Smaybee *
1633f7170741SWill Andrews * RETURN: 0 on success, error code on failure
1634cdb0ab79Smaybee */
1635cdb0ab79Smaybee int
zfs_freesp(znode_t * zp,uint64_t off,uint64_t len,int flag,boolean_t log)1636cdb0ab79Smaybee zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1637cdb0ab79Smaybee {
1638cdb0ab79Smaybee vnode_t *vp = ZTOV(zp);
1639cdb0ab79Smaybee dmu_tx_t *tx;
1640cdb0ab79Smaybee zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1641cdb0ab79Smaybee zilog_t *zilog = zfsvfs->z_log;
16420a586ceaSMark Shellenbaum uint64_t mode;
16430a586ceaSMark Shellenbaum uint64_t mtime[2], ctime[2];
1644db9986c7SMark Shellenbaum sa_bulk_attr_t bulk[3];
16450a586ceaSMark Shellenbaum int count = 0;
1646cdb0ab79Smaybee int error;
164779ab756aSSimon Klinkert rl_t *rl;
1648cdb0ab79Smaybee
16490a586ceaSMark Shellenbaum if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
16500a586ceaSMark Shellenbaum sizeof (mode))) != 0)
16510a586ceaSMark Shellenbaum return (error);
16520a586ceaSMark Shellenbaum
16530a586ceaSMark Shellenbaum if (off > zp->z_size) {
165479ab756aSSimon Klinkert rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1655cdb0ab79Smaybee error = zfs_extend(zp, off+len);
1656cdb0ab79Smaybee if (error == 0 && log)
1657cdb0ab79Smaybee goto log;
165879ab756aSSimon Klinkert
165979ab756aSSimon Klinkert zfs_range_unlock(rl);
1660cdb0ab79Smaybee return (error);
1661cdb0ab79Smaybee }
1662cdb0ab79Smaybee
1663cdb0ab79Smaybee /*
1664cdb0ab79Smaybee * Check for any locks in the region to be freed.
1665cdb0ab79Smaybee */
16660a586ceaSMark Shellenbaum if (MANDLOCK(vp, (mode_t)mode)) {
16670a586ceaSMark Shellenbaum uint64_t length = (len ? len : zp->z_size - off);
1668cdb0ab79Smaybee if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1669cdb0ab79Smaybee return (error);
1670cdb0ab79Smaybee }
1671cdb0ab79Smaybee
167279ab756aSSimon Klinkert if (len == 0 || off + len > zp->z_size) {
167379ab756aSSimon Klinkert rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
167479ab756aSSimon Klinkert } else {
167579ab756aSSimon Klinkert ssize_t lock_off;
167679ab756aSSimon Klinkert ssize_t lock_len;
167779ab756aSSimon Klinkert #ifdef _KERNEL
167879ab756aSSimon Klinkert if (rz_zev_active()) {
167979ab756aSSimon Klinkert /* start of this megabyte */
168079ab756aSSimon Klinkert lock_off = P2ALIGN(off, ZEV_L1_SIZE);
168179ab756aSSimon Klinkert /* full megabytes */
168279ab756aSSimon Klinkert lock_len = len + (off - lock_off);
168379ab756aSSimon Klinkert lock_len = P2ROUNDUP(lock_len, ZEV_L1_SIZE);
168479ab756aSSimon Klinkert } else {
168579ab756aSSimon Klinkert lock_off = off;
168679ab756aSSimon Klinkert lock_len = len;
168779ab756aSSimon Klinkert }
168879ab756aSSimon Klinkert #else
168979ab756aSSimon Klinkert lock_off = off;
169079ab756aSSimon Klinkert lock_len = len;
169179ab756aSSimon Klinkert #endif
169279ab756aSSimon Klinkert rl = zfs_range_lock(zp, lock_off, lock_len, RL_WRITER);
169379ab756aSSimon Klinkert }
169479ab756aSSimon Klinkert
1695cdb0ab79Smaybee if (len == 0) {
1696cdb0ab79Smaybee error = zfs_trunc(zp, off);
1697cdb0ab79Smaybee } else {
1698cdb0ab79Smaybee if ((error = zfs_free_range(zp, off, len)) == 0 &&
16990a586ceaSMark Shellenbaum off + len > zp->z_size)
1700cdb0ab79Smaybee error = zfs_extend(zp, off+len);
1701cdb0ab79Smaybee }
170279ab756aSSimon Klinkert
170379ab756aSSimon Klinkert if (error || !log) {
170479ab756aSSimon Klinkert zfs_range_unlock(rl);
1705cdb0ab79Smaybee return (error);
170679ab756aSSimon Klinkert }
1707cdb0ab79Smaybee log:
1708cdb0ab79Smaybee tx = dmu_tx_create(zfsvfs->z_os);
17090a586ceaSMark Shellenbaum dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
17100a586ceaSMark Shellenbaum zfs_sa_upgrade_txholds(tx, zp);
1711e722410cSMatthew Ahrens error = dmu_tx_assign(tx, TXG_WAIT);
1712cdb0ab79Smaybee if (error) {
1713cdb0ab79Smaybee dmu_tx_abort(tx);
171479ab756aSSimon Klinkert zfs_range_unlock(rl);
1715cdb0ab79Smaybee return (error);
1716cdb0ab79Smaybee }
1717cdb0ab79Smaybee
17180a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
17190a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1720db9986c7SMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1721db9986c7SMark Shellenbaum NULL, &zp->z_pflags, 8);
17220a586ceaSMark Shellenbaum zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
17230a586ceaSMark Shellenbaum error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
17240a586ceaSMark Shellenbaum ASSERT(error == 0);
17250a586ceaSMark Shellenbaum
1726cdb0ab79Smaybee zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
172779ab756aSSimon Klinkert zfs_range_unlock(rl);
1728cdb0ab79Smaybee
1729cdb0ab79Smaybee dmu_tx_commit(tx);
1730cdb0ab79Smaybee return (0);
1731cdb0ab79Smaybee }
1732cdb0ab79Smaybee
1733fa9e4066Sahrens void
zfs_create_fs(objset_t * os,cred_t * cr,nvlist_t * zplprops,dmu_tx_t * tx)1734de8267e0Stimh zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1735fa9e4066Sahrens {
17360a586ceaSMark Shellenbaum uint64_t moid, obj, sa_obj, version;
1737de8267e0Stimh uint64_t sense = ZFS_CASE_SENSITIVE;
1738de8267e0Stimh uint64_t norm = 0;
1739de8267e0Stimh nvpair_t *elem;
1740fa9e4066Sahrens int error;
174159e7834dSMark Shellenbaum int i;
1742fa9e4066Sahrens znode_t *rootzp = NULL;
1743c701fde6SGleb Smirnoff zfsvfs_t *zfsvfs;
1744fa9e4066Sahrens vnode_t *vp;
1745fa9e4066Sahrens vattr_t vattr;
17464ccbb6e7Sahrens znode_t *zp;
174789459e17SMark Shellenbaum zfs_acl_ids_t acl_ids;
1748fa9e4066Sahrens
1749fa9e4066Sahrens /*
1750fa9e4066Sahrens * First attempt to create master node.
1751fa9e4066Sahrens */
1752ea8dc4b6Seschrock /*
1753ea8dc4b6Seschrock * In an empty objset, there are no blocks to read and thus
1754ea8dc4b6Seschrock * there can be no i/o errors (which we assert below).
1755ea8dc4b6Seschrock */
1756fa9e4066Sahrens moid = MASTER_NODE_OBJ;
1757fa9e4066Sahrens error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1758fa9e4066Sahrens DMU_OT_NONE, 0, tx);
1759fa9e4066Sahrens ASSERT(error == 0);
1760fa9e4066Sahrens
1761fa9e4066Sahrens /*
1762fa9e4066Sahrens * Set starting attributes.
1763fa9e4066Sahrens */
17640a586ceaSMark Shellenbaum version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1765de8267e0Stimh elem = NULL;
1766de8267e0Stimh while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1767de8267e0Stimh /* For the moment we expect all zpl props to be uint64_ts */
1768de8267e0Stimh uint64_t val;
1769de8267e0Stimh char *name;
1770fa9e4066Sahrens
1771de8267e0Stimh ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1772b3d911cbStimh VERIFY(nvpair_value_uint64(elem, &val) == 0);
1773de8267e0Stimh name = nvpair_name(elem);
1774de8267e0Stimh if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
177514843421SMatthew Ahrens if (val < version)
1776de8267e0Stimh version = val;
1777de8267e0Stimh } else {
1778de8267e0Stimh error = zap_update(os, moid, name, 8, 1, &val, tx);
1779de8267e0Stimh }
1780fa9e4066Sahrens ASSERT(error == 0);
1781de8267e0Stimh if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1782de8267e0Stimh norm = val;
1783de8267e0Stimh else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1784de8267e0Stimh sense = val;
1785de8267e0Stimh }
1786de8267e0Stimh ASSERT(version != 0);
178714843421SMatthew Ahrens error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1788fa9e4066Sahrens
1789fa9e4066Sahrens /*
17900a586ceaSMark Shellenbaum * Create zap object used for SA attribute registration
17910a586ceaSMark Shellenbaum */
17920a586ceaSMark Shellenbaum
17930a586ceaSMark Shellenbaum if (version >= ZPL_VERSION_SA) {
17940a586ceaSMark Shellenbaum sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
17950a586ceaSMark Shellenbaum DMU_OT_NONE, 0, tx);
17960a586ceaSMark Shellenbaum error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
17970a586ceaSMark Shellenbaum ASSERT(error == 0);
17980a586ceaSMark Shellenbaum } else {
17990a586ceaSMark Shellenbaum sa_obj = 0;
18000a586ceaSMark Shellenbaum }
18010a586ceaSMark Shellenbaum /*
1802fa9e4066Sahrens * Create a delete queue.
1803fa9e4066Sahrens */
180414843421SMatthew Ahrens obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1805fa9e4066Sahrens
180614843421SMatthew Ahrens error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1807fa9e4066Sahrens ASSERT(error == 0);
1808fa9e4066Sahrens
1809fa9e4066Sahrens /*
1810fa9e4066Sahrens * Create root znode. Create minimal znode/vnode/zfsvfs
1811fa9e4066Sahrens * to allow zfs_mknode to work.
1812fa9e4066Sahrens */
1813fa9e4066Sahrens vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1814fa9e4066Sahrens vattr.va_type = VDIR;
1815fa9e4066Sahrens vattr.va_mode = S_IFDIR|0755;
1816ecd6cf80Smarks vattr.va_uid = crgetuid(cr);
1817ecd6cf80Smarks vattr.va_gid = crgetgid(cr);
1818fa9e4066Sahrens
1819fa9e4066Sahrens rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1820744947dcSTom Erickson ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1821744947dcSTom Erickson rootzp->z_moved = 0;
1822893a6d32Sahrens rootzp->z_unlinked = 0;
1823fa9e4066Sahrens rootzp->z_atime_dirty = 0;
18240a586ceaSMark Shellenbaum rootzp->z_is_sa = USE_SA(version, os);
1825fa9e4066Sahrens
1826fa9e4066Sahrens vp = ZTOV(rootzp);
1827fa9e4066Sahrens vn_reinit(vp);
1828fa9e4066Sahrens vp->v_type = VDIR;
1829fa9e4066Sahrens
1830c701fde6SGleb Smirnoff zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1831c701fde6SGleb Smirnoff zfsvfs->z_os = os;
1832c701fde6SGleb Smirnoff zfsvfs->z_parent = zfsvfs;
1833c701fde6SGleb Smirnoff zfsvfs->z_version = version;
1834c701fde6SGleb Smirnoff zfsvfs->z_use_fuids = USE_FUIDS(version, os);
1835c701fde6SGleb Smirnoff zfsvfs->z_use_sa = USE_SA(version, os);
1836c701fde6SGleb Smirnoff zfsvfs->z_norm = norm;
18370a586ceaSMark Shellenbaum
18381d8ccc7bSMark Shellenbaum error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1839c701fde6SGleb Smirnoff &zfsvfs->z_attr_table);
18401d8ccc7bSMark Shellenbaum
18411d8ccc7bSMark Shellenbaum ASSERT(error == 0);
18420a586ceaSMark Shellenbaum
1843de8267e0Stimh /*
1844de8267e0Stimh * Fold case on file systems that are always or sometimes case
1845de8267e0Stimh * insensitive.
1846de8267e0Stimh */
1847de8267e0Stimh if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1848c701fde6SGleb Smirnoff zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1849fa9e4066Sahrens
1850c701fde6SGleb Smirnoff mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1851c701fde6SGleb Smirnoff list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1852fa9e4066Sahrens offsetof(znode_t, z_link_node));
1853fa9e4066Sahrens
185459e7834dSMark Shellenbaum for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1855c701fde6SGleb Smirnoff mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
185659e7834dSMark Shellenbaum
1857c701fde6SGleb Smirnoff rootzp->z_zfsvfs = zfsvfs;
185889459e17SMark Shellenbaum VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
185989459e17SMark Shellenbaum cr, NULL, &acl_ids));
18600a586ceaSMark Shellenbaum zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1861874395d5Smaybee ASSERT3P(zp, ==, rootzp);
1862b5fca8f8Stomee ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
18634ccbb6e7Sahrens error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1864fa9e4066Sahrens ASSERT(error == 0);
186589459e17SMark Shellenbaum zfs_acl_ids_free(&acl_ids);
1866b5fca8f8Stomee POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1867fa9e4066Sahrens
1868fa9e4066Sahrens ZTOV(rootzp)->v_count = 0;
18690a586ceaSMark Shellenbaum sa_handle_destroy(rootzp->z_sa_hdl);
1870fa9e4066Sahrens kmem_cache_free(znode_cache, rootzp);
1871743a77edSAlan Wright
1872743a77edSAlan Wright /*
1873743a77edSAlan Wright * Create shares directory
1874743a77edSAlan Wright */
1875743a77edSAlan Wright
1876c701fde6SGleb Smirnoff error = zfs_create_share_dir(zfsvfs, tx);
187789459e17SMark Shellenbaum
1878743a77edSAlan Wright ASSERT(error == 0);
187959e7834dSMark Shellenbaum
188059e7834dSMark Shellenbaum for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1881c701fde6SGleb Smirnoff mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1882c701fde6SGleb Smirnoff kmem_free(zfsvfs, sizeof (zfsvfs_t));
1883fa9e4066Sahrens }
188455434c77Sek110237
1885da6c28aaSamw #endif /* _KERNEL */
18860a586ceaSMark Shellenbaum
188799d5e173STim Haley static int
zfs_sa_setup(objset_t * osp,sa_attr_type_t ** sa_table)188899d5e173STim Haley zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
188999d5e173STim Haley {
189099d5e173STim Haley uint64_t sa_obj = 0;
189199d5e173STim Haley int error;
189299d5e173STim Haley
189399d5e173STim Haley error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
189499d5e173STim Haley if (error != 0 && error != ENOENT)
189599d5e173STim Haley return (error);
189699d5e173STim Haley
189799d5e173STim Haley error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
189899d5e173STim Haley return (error);
189999d5e173STim Haley }
190099d5e173STim Haley
190199d5e173STim Haley static int
zfs_grab_sa_handle(objset_t * osp,uint64_t obj,sa_handle_t ** hdlp,dmu_buf_t ** db,void * tag)190299d5e173STim Haley zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1903163eb7ffSTim Haley dmu_buf_t **db, void *tag)
190499d5e173STim Haley {
190599d5e173STim Haley dmu_object_info_t doi;
190699d5e173STim Haley int error;
190799d5e173STim Haley
1908163eb7ffSTim Haley if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
190999d5e173STim Haley return (error);
191099d5e173STim Haley
191199d5e173STim Haley dmu_object_info_from_db(*db, &doi);
191299d5e173STim Haley if ((doi.doi_bonus_type != DMU_OT_SA &&
191399d5e173STim Haley doi.doi_bonus_type != DMU_OT_ZNODE) ||
191499d5e173STim Haley doi.doi_bonus_type == DMU_OT_ZNODE &&
191599d5e173STim Haley doi.doi_bonus_size < sizeof (znode_phys_t)) {
1916163eb7ffSTim Haley sa_buf_rele(*db, tag);
1917be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
191899d5e173STim Haley }
191999d5e173STim Haley
192099d5e173STim Haley error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
192199d5e173STim Haley if (error != 0) {
1922163eb7ffSTim Haley sa_buf_rele(*db, tag);
192399d5e173STim Haley return (error);
192499d5e173STim Haley }
192599d5e173STim Haley
192699d5e173STim Haley return (0);
192799d5e173STim Haley }
192899d5e173STim Haley
192999d5e173STim Haley void
zfs_release_sa_handle(sa_handle_t * hdl,dmu_buf_t * db,void * tag)1930163eb7ffSTim Haley zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
193199d5e173STim Haley {
193299d5e173STim Haley sa_handle_destroy(hdl);
1933163eb7ffSTim Haley sa_buf_rele(db, tag);
193499d5e173STim Haley }
193599d5e173STim Haley
193655434c77Sek110237 /*
193755434c77Sek110237 * Given an object number, return its parent object number and whether
193855434c77Sek110237 * or not the object is an extended attribute directory.
193955434c77Sek110237 */
194055434c77Sek110237 static int
zfs_obj_to_pobj(objset_t * osp,sa_handle_t * hdl,sa_attr_type_t * sa_table,uint64_t * pobjp,int * is_xattrdir)19411ce39b5fSJeremy Jones zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
19421ce39b5fSJeremy Jones uint64_t *pobjp, int *is_xattrdir)
194355434c77Sek110237 {
19440a586ceaSMark Shellenbaum uint64_t parent;
19450a586ceaSMark Shellenbaum uint64_t pflags;
19460a586ceaSMark Shellenbaum uint64_t mode;
19471ce39b5fSJeremy Jones uint64_t parent_mode;
19480a586ceaSMark Shellenbaum sa_bulk_attr_t bulk[3];
19491ce39b5fSJeremy Jones sa_handle_t *sa_hdl;
19501ce39b5fSJeremy Jones dmu_buf_t *sa_db;
19510a586ceaSMark Shellenbaum int count = 0;
195299d5e173STim Haley int error;
195355434c77Sek110237
195499d5e173STim Haley SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
195599d5e173STim Haley &parent, sizeof (parent));
19560a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
195799d5e173STim Haley &pflags, sizeof (pflags));
19580a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
195999d5e173STim Haley &mode, sizeof (mode));
19600a586ceaSMark Shellenbaum
196199d5e173STim Haley if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
19620a586ceaSMark Shellenbaum return (error);
196399d5e173STim Haley
19641ce39b5fSJeremy Jones /*
19651ce39b5fSJeremy Jones * When a link is removed its parent pointer is not changed and will
19661ce39b5fSJeremy Jones * be invalid. There are two cases where a link is removed but the
19671ce39b5fSJeremy Jones * file stays around, when it goes to the delete queue and when there
19681ce39b5fSJeremy Jones * are additional links.
19691ce39b5fSJeremy Jones */
19701ce39b5fSJeremy Jones error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
19711ce39b5fSJeremy Jones if (error != 0)
19721ce39b5fSJeremy Jones return (error);
19731ce39b5fSJeremy Jones
19741ce39b5fSJeremy Jones error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
19751ce39b5fSJeremy Jones zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
19761ce39b5fSJeremy Jones if (error != 0)
19771ce39b5fSJeremy Jones return (error);
19781ce39b5fSJeremy Jones
19790a586ceaSMark Shellenbaum *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
198055434c77Sek110237
19811ce39b5fSJeremy Jones /*
19821ce39b5fSJeremy Jones * Extended attributes can be applied to files, directories, etc.
19831ce39b5fSJeremy Jones * Otherwise the parent must be a directory.
19841ce39b5fSJeremy Jones */
19851ce39b5fSJeremy Jones if (!*is_xattrdir && !S_ISDIR(parent_mode))
1986be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
19871ce39b5fSJeremy Jones
19881ce39b5fSJeremy Jones *pobjp = parent;
19891ce39b5fSJeremy Jones
199055434c77Sek110237 return (0);
199155434c77Sek110237 }
199255434c77Sek110237
199399d5e173STim Haley /*
199499d5e173STim Haley * Given an object number, return some zpl level statistics
199599d5e173STim Haley */
199699d5e173STim Haley static int
zfs_obj_to_stats_impl(sa_handle_t * hdl,sa_attr_type_t * sa_table,zfs_stat_t * sb)199799d5e173STim Haley zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
199899d5e173STim Haley zfs_stat_t *sb)
199955434c77Sek110237 {
200099d5e173STim Haley sa_bulk_attr_t bulk[4];
200199d5e173STim Haley int count = 0;
200299d5e173STim Haley
200399d5e173STim Haley SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
200499d5e173STim Haley &sb->zs_mode, sizeof (sb->zs_mode));
200599d5e173STim Haley SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
200699d5e173STim Haley &sb->zs_gen, sizeof (sb->zs_gen));
200799d5e173STim Haley SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
200899d5e173STim Haley &sb->zs_links, sizeof (sb->zs_links));
200999d5e173STim Haley SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
201099d5e173STim Haley &sb->zs_ctime, sizeof (sb->zs_ctime));
201199d5e173STim Haley
201299d5e173STim Haley return (sa_bulk_lookup(hdl, bulk, count));
201399d5e173STim Haley }
201499d5e173STim Haley
201599d5e173STim Haley static int
zfs_obj_to_path_impl(objset_t * osp,uint64_t obj,sa_handle_t * hdl,sa_attr_type_t * sa_table,char * buf,int len)201699d5e173STim Haley zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
201799d5e173STim Haley sa_attr_type_t *sa_table, char *buf, int len)
201899d5e173STim Haley {
201999d5e173STim Haley sa_handle_t *sa_hdl;
202099d5e173STim Haley sa_handle_t *prevhdl = NULL;
202199d5e173STim Haley dmu_buf_t *prevdb = NULL;
202299d5e173STim Haley dmu_buf_t *sa_db = NULL;
202355434c77Sek110237 char *path = buf + len - 1;
202455434c77Sek110237 int error;
202555434c77Sek110237
202655434c77Sek110237 *path = '\0';
202799d5e173STim Haley sa_hdl = hdl;
20280a586ceaSMark Shellenbaum
202955434c77Sek110237 for (;;) {
203055434c77Sek110237 uint64_t pobj;
203155434c77Sek110237 char component[MAXNAMELEN + 2];
203255434c77Sek110237 size_t complen;
203355434c77Sek110237 int is_xattrdir;
203455434c77Sek110237
203599d5e173STim Haley if (prevdb)
2036163eb7ffSTim Haley zfs_release_sa_handle(prevhdl, prevdb, FTAG);
203799d5e173STim Haley
20381ce39b5fSJeremy Jones if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
203999d5e173STim Haley &is_xattrdir)) != 0)
204055434c77Sek110237 break;
204155434c77Sek110237
204255434c77Sek110237 if (pobj == obj) {
204355434c77Sek110237 if (path[0] != '/')
204455434c77Sek110237 *--path = '/';
204555434c77Sek110237 break;
204655434c77Sek110237 }
204755434c77Sek110237
204855434c77Sek110237 component[0] = '/';
204955434c77Sek110237 if (is_xattrdir) {
205055434c77Sek110237 (void) sprintf(component + 1, "<xattrdir>");
205155434c77Sek110237 } else {
2052e7437265Sahrens error = zap_value_search(osp, pobj, obj,
2053e7437265Sahrens ZFS_DIRENT_OBJ(-1ULL), component + 1);
205455434c77Sek110237 if (error != 0)
205555434c77Sek110237 break;
205655434c77Sek110237 }
205755434c77Sek110237
205855434c77Sek110237 complen = strlen(component);
205955434c77Sek110237 path -= complen;
206055434c77Sek110237 ASSERT(path >= buf);
206155434c77Sek110237 bcopy(component, path, complen);
206255434c77Sek110237 obj = pobj;
206399d5e173STim Haley
206499d5e173STim Haley if (sa_hdl != hdl) {
206599d5e173STim Haley prevhdl = sa_hdl;
206699d5e173STim Haley prevdb = sa_db;
206799d5e173STim Haley }
2068163eb7ffSTim Haley error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
206999d5e173STim Haley if (error != 0) {
207099d5e173STim Haley sa_hdl = prevhdl;
207199d5e173STim Haley sa_db = prevdb;
207299d5e173STim Haley break;
207399d5e173STim Haley }
207499d5e173STim Haley }
207599d5e173STim Haley
207699d5e173STim Haley if (sa_hdl != NULL && sa_hdl != hdl) {
207799d5e173STim Haley ASSERT(sa_db != NULL);
2078163eb7ffSTim Haley zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
207955434c77Sek110237 }
208055434c77Sek110237
208155434c77Sek110237 if (error == 0)
208255434c77Sek110237 (void) memmove(buf, path, buf + len - path);
20830a586ceaSMark Shellenbaum
208455434c77Sek110237 return (error);
208555434c77Sek110237 }
208699d5e173STim Haley
208799d5e173STim Haley int
zfs_obj_to_path(objset_t * osp,uint64_t obj,char * buf,int len)208899d5e173STim Haley zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
208999d5e173STim Haley {
209099d5e173STim Haley sa_attr_type_t *sa_table;
209199d5e173STim Haley sa_handle_t *hdl;
209299d5e173STim Haley dmu_buf_t *db;
209399d5e173STim Haley int error;
209499d5e173STim Haley
209599d5e173STim Haley error = zfs_sa_setup(osp, &sa_table);
209699d5e173STim Haley if (error != 0)
209799d5e173STim Haley return (error);
209899d5e173STim Haley
2099163eb7ffSTim Haley error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
210099d5e173STim Haley if (error != 0)
210199d5e173STim Haley return (error);
210299d5e173STim Haley
210399d5e173STim Haley error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
210499d5e173STim Haley
2105163eb7ffSTim Haley zfs_release_sa_handle(hdl, db, FTAG);
210699d5e173STim Haley return (error);
210799d5e173STim Haley }
210899d5e173STim Haley
210999d5e173STim Haley int
zfs_obj_to_stats(objset_t * osp,uint64_t obj,zfs_stat_t * sb,char * buf,int len)211099d5e173STim Haley zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
211199d5e173STim Haley char *buf, int len)
211299d5e173STim Haley {
211399d5e173STim Haley char *path = buf + len - 1;
211499d5e173STim Haley sa_attr_type_t *sa_table;
211599d5e173STim Haley sa_handle_t *hdl;
211699d5e173STim Haley dmu_buf_t *db;
211799d5e173STim Haley int error;
211899d5e173STim Haley
211999d5e173STim Haley *path = '\0';
212099d5e173STim Haley
212199d5e173STim Haley error = zfs_sa_setup(osp, &sa_table);
212299d5e173STim Haley if (error != 0)
212399d5e173STim Haley return (error);
212499d5e173STim Haley
2125163eb7ffSTim Haley error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
212699d5e173STim Haley if (error != 0)
212799d5e173STim Haley return (error);
212899d5e173STim Haley
212999d5e173STim Haley error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
213099d5e173STim Haley if (error != 0) {
2131163eb7ffSTim Haley zfs_release_sa_handle(hdl, db, FTAG);
213299d5e173STim Haley return (error);
213399d5e173STim Haley }
213499d5e173STim Haley
213599d5e173STim Haley error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
213699d5e173STim Haley
2137163eb7ffSTim Haley zfs_release_sa_handle(hdl, db, FTAG);
213899d5e173STim Haley return (error);
213999d5e173STim Haley }
2140