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