zfs_znode.c revision 173268
118334Speter/*
218334Speter * CDDL HEADER START
318334Speter *
418334Speter * The contents of this file are subject to the terms of the
518334Speter * Common Development and Distribution License (the "License").
618334Speter * You may not use this file except in compliance with the License.
718334Speter *
818334Speter * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
918334Speter * or http://www.opensolaris.org/os/licensing.
1018334Speter * See the License for the specific language governing permissions
1118334Speter * and limitations under the License.
1218334Speter *
1318334Speter * When distributing Covered Code, include this CDDL HEADER in each
1418334Speter * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1518334Speter * If applicable, add the following below this CDDL HEADER, with the
1618334Speter * fields enclosed by brackets "[]" replaced with your own identifying
1718334Speter * information: Portions Copyright [yyyy] [name of copyright owner]
1818334Speter *
1918334Speter * CDDL HEADER END
2018334Speter */
2118334Speter/*
2218334Speter * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2318334Speter * Use is subject to license terms.
2418334Speter */
2518334Speter
2618334Speter/* Portions Copyright 2007 Jeremy Teo */
2718334Speter
2818334Speter#pragma ident	"%Z%%M%	%I%	%E% SMI"
2918334Speter
3018334Speter#ifdef _KERNEL
3118334Speter#include <sys/types.h>
3218334Speter#include <sys/param.h>
3318334Speter#include <sys/time.h>
3418334Speter#include <sys/systm.h>
3518334Speter#include <sys/sysmacros.h>
3618334Speter#include <sys/resource.h>
3718334Speter#include <sys/mntent.h>
3818334Speter#include <sys/vfs.h>
3918334Speter#include <sys/vnode.h>
4018334Speter#include <sys/file.h>
4118334Speter#include <sys/kmem.h>
4218334Speter#include <sys/cmn_err.h>
4318334Speter#include <sys/errno.h>
4418334Speter#include <sys/unistd.h>
4518334Speter#include <sys/atomic.h>
4618334Speter#include <sys/zfs_dir.h>
4718334Speter#include <sys/zfs_acl.h>
4818334Speter#include <sys/zfs_ioctl.h>
4918334Speter#include <sys/zfs_rlock.h>
5018334Speter#include <sys/fs/zfs.h>
5118334Speter#endif /* _KERNEL */
5218334Speter
5318334Speter#include <sys/dmu.h>
5418334Speter#include <sys/refcount.h>
5518334Speter#include <sys/stat.h>
5618334Speter#include <sys/zap.h>
5750397Sobrien#include <sys/zfs_znode.h>
5850397Sobrien#include <sys/refcount.h>
5950397Sobrien
6018334Speter/* Used by fstat(1). */
6118334SpeterSYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD, 0, sizeof(znode_t),
6218334Speter    "sizeof(znode_t)");
6318334Speter
6418334Speter/*
6518334Speter * Functions needed for userland (ie: libzpool) are not put under
6618334Speter * #ifdef_KERNEL; the rest of the functions have dependencies
6718334Speter * (such as VFS logic) that will not compile easily in userland.
6818334Speter */
6918334Speter#ifdef _KERNEL
7018334Speterstruct kmem_cache *znode_cache = NULL;
7118334Speter
7250397Sobrien/*ARGSUSED*/
7350397Sobrienstatic void
7450397Sobrienznode_pageout_func(dmu_buf_t *dbuf, void *user_ptr)
7550397Sobrien{
7650397Sobrien	znode_t *zp = user_ptr;
7750397Sobrien	vnode_t *vp;
7850397Sobrien
7918334Speter	mutex_enter(&zp->z_lock);
8018334Speter	vp = ZTOV(zp);
8118334Speter	if (vp == NULL) {
8218334Speter		mutex_exit(&zp->z_lock);
8318334Speter		zfs_znode_free(zp);
8450397Sobrien	} else if (vp->v_count == 0) {
8518334Speter		ZTOV(zp) = NULL;
8618334Speter		vhold(vp);
87		mutex_exit(&zp->z_lock);
88		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
89		vrecycle(vp, curthread);
90		VOP_UNLOCK(vp, 0, curthread);
91		vdrop(vp);
92		zfs_znode_free(zp);
93	} else {
94		/* signal force unmount that this znode can be freed */
95		zp->z_dbuf = NULL;
96		mutex_exit(&zp->z_lock);
97	}
98}
99
100extern struct vop_vector zfs_vnodeops;
101extern struct vop_vector zfs_fifoops;
102
103/*
104 * XXX: We cannot use this function as a cache constructor, because
105 *      there is one global cache for all file systems and we need
106 *      to pass vfsp here, which is not possible, because argument
107 *      'cdrarg' is defined at kmem_cache_create() time.
108 */
109static int
110zfs_znode_cache_constructor(void *buf, void *cdrarg, int kmflags)
111{
112	znode_t *zp = buf;
113	vnode_t *vp;
114	vfs_t *vfsp = cdrarg;
115	int error;
116
117	if (cdrarg != NULL) {
118		error = getnewvnode("zfs", vfsp, &zfs_vnodeops, &vp);
119		ASSERT(error == 0);
120		zp->z_vnode = vp;
121		vp->v_data = (caddr_t)zp;
122		vp->v_vnlock->lk_flags |= LK_CANRECURSE;
123		vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
124	} else {
125		zp->z_vnode = NULL;
126	}
127	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
128	rw_init(&zp->z_map_lock, NULL, RW_DEFAULT, NULL);
129	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
130	rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
131	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
132
133	mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
134	avl_create(&zp->z_range_avl, zfs_range_compare,
135	    sizeof (rl_t), offsetof(rl_t, r_node));
136
137	zp->z_dbuf_held = 0;
138	zp->z_dirlocks = 0;
139	zp->z_lockf = NULL;
140	return (0);
141}
142
143/*ARGSUSED*/
144static void
145zfs_znode_cache_destructor(void *buf, void *cdarg)
146{
147	znode_t *zp = buf;
148
149	ASSERT(zp->z_dirlocks == 0);
150	mutex_destroy(&zp->z_lock);
151	rw_destroy(&zp->z_map_lock);
152	rw_destroy(&zp->z_parent_lock);
153	rw_destroy(&zp->z_name_lock);
154	mutex_destroy(&zp->z_acl_lock);
155	mutex_destroy(&zp->z_range_lock);
156	avl_destroy(&zp->z_range_avl);
157
158	ASSERT(zp->z_dbuf_held == 0);
159}
160
161void
162zfs_znode_init(void)
163{
164	/*
165	 * Initialize zcache
166	 */
167	ASSERT(znode_cache == NULL);
168	znode_cache = kmem_cache_create("zfs_znode_cache",
169	    sizeof (znode_t), 0, /* zfs_znode_cache_constructor */ NULL,
170	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
171}
172
173void
174zfs_znode_fini(void)
175{
176	/*
177	 * Cleanup zcache
178	 */
179	if (znode_cache)
180		kmem_cache_destroy(znode_cache);
181	znode_cache = NULL;
182}
183
184/*
185 * zfs_init_fs - Initialize the zfsvfs struct and the file system
186 *	incore "master" object.  Verify version compatibility.
187 */
188int
189zfs_init_fs(zfsvfs_t *zfsvfs, znode_t **zpp, cred_t *cr)
190{
191	objset_t	*os = zfsvfs->z_os;
192	uint64_t	version = ZPL_VERSION;
193	int		i, error;
194	dmu_object_info_t doi;
195	uint64_t fsid_guid;
196
197	*zpp = NULL;
198
199	/*
200	 * XXX - hack to auto-create the pool root filesystem at
201	 * the first attempted mount.
202	 */
203	if (dmu_object_info(os, MASTER_NODE_OBJ, &doi) == ENOENT) {
204		dmu_tx_t *tx = dmu_tx_create(os);
205
206		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* master */
207		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* del queue */
208		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); /* root node */
209		error = dmu_tx_assign(tx, TXG_WAIT);
210		ASSERT3U(error, ==, 0);
211		zfs_create_fs(os, cr, tx);
212		dmu_tx_commit(tx);
213	}
214
215	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_OBJ, 8, 1,
216	    &version);
217	if (error) {
218		return (error);
219	} else if (version != ZPL_VERSION) {
220		(void) printf("Mismatched versions:  File system "
221		    "is version %lld on-disk format, which is "
222		    "incompatible with this software version %lld!",
223		    (u_longlong_t)version, ZPL_VERSION);
224		return (ENOTSUP);
225	}
226
227	/*
228	 * The fsid is 64 bits, composed of an 8-bit fs type, which
229	 * separates our fsid from any other filesystem types, and a
230	 * 56-bit objset unique ID.  The objset unique ID is unique to
231	 * all objsets open on this system, provided by unique_create().
232	 * The 8-bit fs type must be put in the low bits of fsid[1]
233	 * because that's where other Solaris filesystems put it.
234	 */
235	fsid_guid = dmu_objset_fsid_guid(os);
236	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
237	zfsvfs->z_vfs->vfs_fsid.val[0] = fsid_guid;
238	zfsvfs->z_vfs->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
239	    zfsvfs->z_vfs->mnt_vfc->vfc_typenum & 0xFF;
240
241	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
242	    &zfsvfs->z_root);
243	if (error)
244		return (error);
245	ASSERT(zfsvfs->z_root != 0);
246
247	/*
248	 * Create the per mount vop tables.
249	 */
250
251	/*
252	 * Initialize zget mutex's
253	 */
254	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
255		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
256
257	error = zfs_zget(zfsvfs, zfsvfs->z_root, zpp);
258	if (error)
259		return (error);
260	ASSERT3U((*zpp)->z_id, ==, zfsvfs->z_root);
261
262	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
263	    &zfsvfs->z_unlinkedobj);
264	if (error)
265		return (error);
266
267	return (0);
268}
269
270/*
271 * define a couple of values we need available
272 * for both 64 and 32 bit environments.
273 */
274#ifndef NBITSMINOR64
275#define	NBITSMINOR64	32
276#endif
277#ifndef MAXMAJ64
278#define	MAXMAJ64	0xffffffffUL
279#endif
280#ifndef	MAXMIN64
281#define	MAXMIN64	0xffffffffUL
282#endif
283#ifndef major
284#define	major(x)	((int)(((u_int)(x) >> 8)&0xff))	/* major number */
285#endif
286#ifndef minor
287#define	minor(x)	((int)((x)&0xffff00ff))		/* minor number */
288#endif
289
290/*
291 * Create special expldev for ZFS private use.
292 * Can't use standard expldev since it doesn't do
293 * what we want.  The standard expldev() takes a
294 * dev32_t in LP64 and expands it to a long dev_t.
295 * We need an interface that takes a dev32_t in ILP32
296 * and expands it to a long dev_t.
297 */
298static uint64_t
299zfs_expldev(dev_t dev)
300{
301	return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
302}
303/*
304 * Special cmpldev for ZFS private use.
305 * Can't use standard cmpldev since it takes
306 * a long dev_t and compresses it to dev32_t in
307 * LP64.  We need to do a compaction of a long dev_t
308 * to a dev32_t in ILP32.
309 */
310dev_t
311zfs_cmpldev(uint64_t dev)
312{
313	return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
314}
315
316/*
317 * Construct a new znode/vnode and intialize.
318 *
319 * This does not do a call to dmu_set_user() that is
320 * up to the caller to do, in case you don't want to
321 * return the znode
322 */
323static znode_t *
324zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, uint64_t obj_num, int blksz)
325{
326	znode_t	*zp;
327	vnode_t *vp;
328	int error;
329
330	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
331	zfs_znode_cache_constructor(zp, zfsvfs->z_vfs, 0);
332
333	ASSERT(zp->z_dirlocks == NULL);
334
335	zp->z_phys = db->db_data;
336	zp->z_zfsvfs = zfsvfs;
337	zp->z_unlinked = 0;
338	zp->z_atime_dirty = 0;
339	zp->z_dbuf_held = 0;
340	zp->z_mapcnt = 0;
341	zp->z_last_itx = 0;
342	zp->z_dbuf = db;
343	zp->z_id = obj_num;
344	zp->z_blksz = blksz;
345	zp->z_seq = 0x7A4653;
346	zp->z_sync_cnt = 0;
347
348	mutex_enter(&zfsvfs->z_znodes_lock);
349	list_insert_tail(&zfsvfs->z_all_znodes, zp);
350	mutex_exit(&zfsvfs->z_znodes_lock);
351
352	vp = ZTOV(zp);
353	if (vp == NULL)
354		return (zp);
355
356	error = insmntque(vp, zfsvfs->z_vfs);
357	KASSERT(error == 0, ("insmntque() failed: error %d", error));
358
359	vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
360	switch (vp->v_type) {
361	case VDIR:
362		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
363		break;
364	case VFIFO:
365		vp->v_op = &zfs_fifoops;
366		break;
367	}
368
369	return (zp);
370}
371
372static void
373zfs_znode_dmu_init(znode_t *zp)
374{
375	znode_t		*nzp;
376	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
377	dmu_buf_t	*db = zp->z_dbuf;
378
379	mutex_enter(&zp->z_lock);
380
381	nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_pageout_func);
382
383	/*
384	 * there should be no
385	 * concurrent zgets on this object.
386	 */
387	ASSERT3P(nzp, ==, NULL);
388
389	/*
390	 * Slap on VROOT if we are the root znode
391	 */
392	if (zp->z_id == zfsvfs->z_root) {
393		ZTOV(zp)->v_flag |= VROOT;
394	}
395
396	ASSERT(zp->z_dbuf_held == 0);
397	zp->z_dbuf_held = 1;
398	VFS_HOLD(zfsvfs->z_vfs);
399	mutex_exit(&zp->z_lock);
400}
401
402/*
403 * Create a new DMU object to hold a zfs znode.
404 *
405 *	IN:	dzp	- parent directory for new znode
406 *		vap	- file attributes for new znode
407 *		tx	- dmu transaction id for zap operations
408 *		cr	- credentials of caller
409 *		flag	- flags:
410 *			  IS_ROOT_NODE	- new object will be root
411 *			  IS_XATTR	- new object is an attribute
412 *			  IS_REPLAY	- intent log replay
413 *
414 *	OUT:	oid	- ID of created object
415 *
416 */
417void
418zfs_mknode(znode_t *dzp, vattr_t *vap, uint64_t *oid, dmu_tx_t *tx, cred_t *cr,
419	uint_t flag, znode_t **zpp, int bonuslen)
420{
421	dmu_buf_t	*dbp;
422	znode_phys_t	*pzp;
423	znode_t		*zp;
424	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
425	timestruc_t	now;
426	uint64_t	gen;
427	int		err;
428
429	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
430
431	if (zfsvfs->z_assign >= TXG_INITIAL) {		/* ZIL replay */
432		*oid = vap->va_nodeid;
433		flag |= IS_REPLAY;
434		now = vap->va_ctime;		/* see zfs_replay_create() */
435		gen = vap->va_nblocks;		/* ditto */
436	} else {
437		*oid = 0;
438		gethrestime(&now);
439		gen = dmu_tx_get_txg(tx);
440	}
441
442	/*
443	 * Create a new DMU object.
444	 */
445	/*
446	 * There's currently no mechanism for pre-reading the blocks that will
447	 * be to needed allocate a new object, so we accept the small chance
448	 * that there will be an i/o error and we will fail one of the
449	 * assertions below.
450	 */
451	if (vap->va_type == VDIR) {
452		if (flag & IS_REPLAY) {
453			err = zap_create_claim(zfsvfs->z_os, *oid,
454			    DMU_OT_DIRECTORY_CONTENTS,
455			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
456			ASSERT3U(err, ==, 0);
457		} else {
458			*oid = zap_create(zfsvfs->z_os,
459			    DMU_OT_DIRECTORY_CONTENTS,
460			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
461		}
462	} else {
463		if (flag & IS_REPLAY) {
464			err = dmu_object_claim(zfsvfs->z_os, *oid,
465			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
466			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
467			ASSERT3U(err, ==, 0);
468		} else {
469			*oid = dmu_object_alloc(zfsvfs->z_os,
470			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
471			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
472		}
473	}
474	VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, *oid, NULL, &dbp));
475	dmu_buf_will_dirty(dbp, tx);
476
477	/*
478	 * Initialize the znode physical data to zero.
479	 */
480	ASSERT(dbp->db_size >= sizeof (znode_phys_t));
481	bzero(dbp->db_data, dbp->db_size);
482	pzp = dbp->db_data;
483
484	/*
485	 * If this is the root, fix up the half-initialized parent pointer
486	 * to reference the just-allocated physical data area.
487	 */
488	if (flag & IS_ROOT_NODE) {
489		dzp->z_phys = pzp;
490		dzp->z_id = *oid;
491	}
492
493	/*
494	 * If parent is an xattr, so am I.
495	 */
496	if (dzp->z_phys->zp_flags & ZFS_XATTR)
497		flag |= IS_XATTR;
498
499	if (vap->va_type == VBLK || vap->va_type == VCHR) {
500		pzp->zp_rdev = zfs_expldev(vap->va_rdev);
501	}
502
503	if (vap->va_type == VDIR) {
504		pzp->zp_size = 2;		/* contents ("." and "..") */
505		pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
506	}
507
508	pzp->zp_parent = dzp->z_id;
509	if (flag & IS_XATTR)
510		pzp->zp_flags |= ZFS_XATTR;
511
512	pzp->zp_gen = gen;
513
514	ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
515	ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
516
517	if (vap->va_mask & AT_ATIME) {
518		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
519	} else {
520		ZFS_TIME_ENCODE(&now, pzp->zp_atime);
521	}
522
523	if (vap->va_mask & AT_MTIME) {
524		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
525	} else {
526		ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
527	}
528
529	pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
530	zp = zfs_znode_alloc(zfsvfs, dbp, *oid, 0);
531
532	zfs_perm_init(zp, dzp, flag, vap, tx, cr);
533
534	if (zpp) {
535		kmutex_t *hash_mtx = ZFS_OBJ_MUTEX(zp);
536
537		mutex_enter(hash_mtx);
538		zfs_znode_dmu_init(zp);
539		mutex_exit(hash_mtx);
540
541		*zpp = zp;
542	} else {
543		if (ZTOV(zp) != NULL)
544			ZTOV(zp)->v_count = 0;
545		dmu_buf_rele(dbp, NULL);
546		zfs_znode_free(zp);
547	}
548}
549
550int
551zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
552{
553	dmu_object_info_t doi;
554	dmu_buf_t	*db;
555	znode_t		*zp;
556	vnode_t		*vp;
557	int err;
558
559	*zpp = NULL;
560
561	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
562
563	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
564	if (err) {
565		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
566		return (err);
567	}
568
569	dmu_object_info_from_db(db, &doi);
570	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
571	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
572		dmu_buf_rele(db, NULL);
573		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
574		return (EINVAL);
575	}
576
577	ASSERT(db->db_object == obj_num);
578	ASSERT(db->db_offset == -1);
579	ASSERT(db->db_data != NULL);
580
581	zp = dmu_buf_get_user(db);
582
583	if (zp != NULL) {
584		mutex_enter(&zp->z_lock);
585
586		ASSERT3U(zp->z_id, ==, obj_num);
587		if (zp->z_unlinked) {
588			dmu_buf_rele(db, NULL);
589			mutex_exit(&zp->z_lock);
590			ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
591			return (ENOENT);
592		} else if (zp->z_dbuf_held) {
593			dmu_buf_rele(db, NULL);
594		} else {
595			zp->z_dbuf_held = 1;
596			VFS_HOLD(zfsvfs->z_vfs);
597		}
598
599		if (ZTOV(zp) != NULL)
600			VN_HOLD(ZTOV(zp));
601		else {
602			err = getnewvnode("zfs", zfsvfs->z_vfs, &zfs_vnodeops,
603			    &zp->z_vnode);
604			ASSERT(err == 0);
605			vp = ZTOV(zp);
606			vp->v_data = (caddr_t)zp;
607			vp->v_vnlock->lk_flags |= LK_CANRECURSE;
608			vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
609			vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
610			if (vp->v_type == VDIR)
611				zp->z_zn_prefetch = B_TRUE;	/* z_prefetch default is enabled */
612			err = insmntque(vp, zfsvfs->z_vfs);
613			KASSERT(err == 0, ("insmntque() failed: error %d", err));
614		}
615		mutex_exit(&zp->z_lock);
616		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
617		*zpp = zp;
618		return (0);
619	}
620
621	/*
622	 * Not found create new znode/vnode
623	 */
624	zp = zfs_znode_alloc(zfsvfs, db, obj_num, doi.doi_data_block_size);
625	ASSERT3U(zp->z_id, ==, obj_num);
626	zfs_znode_dmu_init(zp);
627	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
628	*zpp = zp;
629	return (0);
630}
631
632void
633zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
634{
635	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
636	int error;
637
638	ZFS_OBJ_HOLD_ENTER(zfsvfs, zp->z_id);
639	if (zp->z_phys->zp_acl.z_acl_extern_obj) {
640		error = dmu_object_free(zfsvfs->z_os,
641		    zp->z_phys->zp_acl.z_acl_extern_obj, tx);
642		ASSERT3U(error, ==, 0);
643	}
644	error = dmu_object_free(zfsvfs->z_os, zp->z_id, tx);
645	ASSERT3U(error, ==, 0);
646	zp->z_dbuf_held = 0;
647	ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
648	dmu_buf_rele(zp->z_dbuf, NULL);
649}
650
651void
652zfs_zinactive(znode_t *zp)
653{
654	vnode_t	*vp = ZTOV(zp);
655	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
656	uint64_t z_id = zp->z_id;
657
658	ASSERT(zp->z_dbuf_held && zp->z_phys);
659
660	/*
661	 * Don't allow a zfs_zget() while were trying to release this znode
662	 */
663	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
664
665	mutex_enter(&zp->z_lock);
666	VI_LOCK(vp);
667	if (vp->v_count > 0) {
668		/*
669		 * If the hold count is greater than zero, somebody has
670		 * obtained a new reference on this znode while we were
671		 * processing it here, so we are done.
672		 */
673		VI_UNLOCK(vp);
674		mutex_exit(&zp->z_lock);
675		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
676		return;
677	}
678	VI_UNLOCK(vp);
679
680	/*
681	 * If this was the last reference to a file with no links,
682	 * remove the file from the file system.
683	 */
684	if (zp->z_unlinked) {
685		ZTOV(zp) = NULL;
686		mutex_exit(&zp->z_lock);
687		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
688		ASSERT(vp->v_count == 0);
689		vrecycle(vp, curthread);
690		zfs_rmnode(zp);
691		VFS_RELE(zfsvfs->z_vfs);
692		return;
693	}
694	ASSERT(zp->z_phys);
695	ASSERT(zp->z_dbuf_held);
696	mutex_exit(&zp->z_lock);
697	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
698}
699
700void
701zfs_znode_free(znode_t *zp)
702{
703	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
704
705	mutex_enter(&zfsvfs->z_znodes_lock);
706	list_remove(&zfsvfs->z_all_znodes, zp);
707	mutex_exit(&zfsvfs->z_znodes_lock);
708
709	kmem_cache_free(znode_cache, zp);
710}
711
712void
713zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
714{
715	timestruc_t	now;
716
717	ASSERT(MUTEX_HELD(&zp->z_lock));
718
719	gethrestime(&now);
720
721	if (tx) {
722		dmu_buf_will_dirty(zp->z_dbuf, tx);
723		zp->z_atime_dirty = 0;
724		zp->z_seq++;
725	} else {
726		zp->z_atime_dirty = 1;
727	}
728
729	if (flag & AT_ATIME)
730		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
731
732	if (flag & AT_MTIME)
733		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
734
735	if (flag & AT_CTIME)
736		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
737}
738
739/*
740 * Update the requested znode timestamps with the current time.
741 * If we are in a transaction, then go ahead and mark the znode
742 * dirty in the transaction so the timestamps will go to disk.
743 * Otherwise, we will get pushed next time the znode is updated
744 * in a transaction, or when this znode eventually goes inactive.
745 *
746 * Why is this OK?
747 *  1 - Only the ACCESS time is ever updated outside of a transaction.
748 *  2 - Multiple consecutive updates will be collapsed into a single
749 *	znode update by the transaction grouping semantics of the DMU.
750 */
751void
752zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
753{
754	mutex_enter(&zp->z_lock);
755	zfs_time_stamper_locked(zp, flag, tx);
756	mutex_exit(&zp->z_lock);
757}
758
759/*
760 * Grow the block size for a file.
761 *
762 *	IN:	zp	- znode of file to free data in.
763 *		size	- requested block size
764 *		tx	- open transaction.
765 *
766 * NOTE: this function assumes that the znode is write locked.
767 */
768void
769zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
770{
771	int		error;
772	u_longlong_t	dummy;
773
774	if (size <= zp->z_blksz)
775		return;
776	/*
777	 * If the file size is already greater than the current blocksize,
778	 * we will not grow.  If there is more than one block in a file,
779	 * the blocksize cannot change.
780	 */
781	if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
782		return;
783
784	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
785	    size, 0, tx);
786	if (error == ENOTSUP)
787		return;
788	ASSERT3U(error, ==, 0);
789
790	/* What blocksize did we actually get? */
791	dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
792}
793
794/*
795 * Free space in a file.
796 *
797 *	IN:	zp	- znode of file to free data in.
798 *		off	- start of section to free.
799 *		len	- length of section to free (0 => to EOF).
800 *		flag	- current file open mode flags.
801 *
802 * 	RETURN:	0 if success
803 *		error code if failure
804 */
805int
806zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
807{
808	vnode_t *vp = ZTOV(zp);
809	dmu_tx_t *tx;
810	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
811	zilog_t *zilog = zfsvfs->z_log;
812	rl_t *rl;
813	uint64_t end = off + len;
814	uint64_t size, new_blksz;
815	int error;
816
817	if (ZTOV(zp)->v_type == VFIFO)
818		return (0);
819
820	/*
821	 * If we will change zp_size then lock the whole file,
822	 * otherwise just lock the range being freed.
823	 */
824	if (len == 0 || off + len > zp->z_phys->zp_size) {
825		rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
826	} else {
827		rl = zfs_range_lock(zp, off, len, RL_WRITER);
828		/* recheck, in case zp_size changed */
829		if (off + len > zp->z_phys->zp_size) {
830			/* lost race: file size changed, lock whole file */
831			zfs_range_unlock(rl);
832			rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
833		}
834	}
835
836	/*
837	 * Nothing to do if file already at desired length.
838	 */
839	size = zp->z_phys->zp_size;
840	if (len == 0 && size == off && off != 0) {
841		zfs_range_unlock(rl);
842		return (0);
843	}
844
845	tx = dmu_tx_create(zfsvfs->z_os);
846	dmu_tx_hold_bonus(tx, zp->z_id);
847	new_blksz = 0;
848	if (end > size &&
849	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
850		/*
851		 * We are growing the file past the current block size.
852		 */
853		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
854			ASSERT(!ISP2(zp->z_blksz));
855			new_blksz = MIN(end, SPA_MAXBLOCKSIZE);
856		} else {
857			new_blksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
858		}
859		dmu_tx_hold_write(tx, zp->z_id, 0, MIN(end, new_blksz));
860	} else if (off < size) {
861		/*
862		 * If len == 0, we are truncating the file.
863		 */
864		dmu_tx_hold_free(tx, zp->z_id, off, len ? len : DMU_OBJECT_END);
865	}
866
867	error = dmu_tx_assign(tx, zfsvfs->z_assign);
868	if (error) {
869		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
870			dmu_tx_wait(tx);
871		dmu_tx_abort(tx);
872		zfs_range_unlock(rl);
873		return (error);
874	}
875
876	if (new_blksz)
877		zfs_grow_blocksize(zp, new_blksz, tx);
878
879	if (end > size || len == 0)
880		zp->z_phys->zp_size = end;
881
882	if (off < size) {
883		objset_t *os = zfsvfs->z_os;
884		uint64_t rlen = len;
885
886		if (len == 0)
887			rlen = -1;
888		else if (end > size)
889			rlen = size - off;
890		VERIFY(0 == dmu_free_range(os, zp->z_id, off, rlen, tx));
891	}
892
893	if (log) {
894		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
895		zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
896	}
897
898	zfs_range_unlock(rl);
899
900	dmu_tx_commit(tx);
901
902	/*
903	 * Clear any mapped pages in the truncated region.  This has to
904	 * happen outside of the transaction to avoid the possibility of
905	 * a deadlock with someone trying to push a page that we are
906	 * about to invalidate.
907	 */
908	rw_enter(&zp->z_map_lock, RW_WRITER);
909	if (end > size)
910		vnode_pager_setsize(vp, end);
911	else if (len == 0) {
912#if 0
913		error = vtruncbuf(vp, curthread->td_ucred, curthread, end, PAGE_SIZE);
914#else
915		error = vinvalbuf(vp, V_SAVE, curthread, 0, 0);
916		vnode_pager_setsize(vp, end);
917#endif
918	}
919	rw_exit(&zp->z_map_lock);
920
921	return (0);
922}
923
924void
925zfs_create_fs(objset_t *os, cred_t *cr, dmu_tx_t *tx)
926{
927	zfsvfs_t	zfsvfs;
928	uint64_t	moid, doid, roid = 0;
929	uint64_t	version = ZPL_VERSION;
930	int		error;
931	znode_t		*rootzp = NULL;
932	vattr_t		vattr;
933
934	/*
935	 * First attempt to create master node.
936	 */
937	/*
938	 * In an empty objset, there are no blocks to read and thus
939	 * there can be no i/o errors (which we assert below).
940	 */
941	moid = MASTER_NODE_OBJ;
942	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
943	    DMU_OT_NONE, 0, tx);
944	ASSERT(error == 0);
945
946	/*
947	 * Set starting attributes.
948	 */
949
950	error = zap_update(os, moid, ZPL_VERSION_OBJ, 8, 1, &version, tx);
951	ASSERT(error == 0);
952
953	/*
954	 * Create a delete queue.
955	 */
956	doid = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
957
958	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &doid, tx);
959	ASSERT(error == 0);
960
961	/*
962	 * Create root znode.  Create minimal znode/vnode/zfsvfs
963	 * to allow zfs_mknode to work.
964	 */
965	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
966	vattr.va_type = VDIR;
967	vattr.va_mode = S_IFDIR|0755;
968	vattr.va_uid = UID_ROOT;
969	vattr.va_gid = GID_WHEEL;
970
971	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
972	zfs_znode_cache_constructor(rootzp, NULL, 0);
973	rootzp->z_zfsvfs = &zfsvfs;
974	rootzp->z_unlinked = 0;
975	rootzp->z_atime_dirty = 0;
976	rootzp->z_dbuf_held = 0;
977
978	bzero(&zfsvfs, sizeof (zfsvfs_t));
979
980	zfsvfs.z_os = os;
981	zfsvfs.z_assign = TXG_NOWAIT;
982	zfsvfs.z_parent = &zfsvfs;
983
984	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
985	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
986	    offsetof(znode_t, z_link_node));
987
988	zfs_mknode(rootzp, &vattr, &roid, tx, cr, IS_ROOT_NODE, NULL, 0);
989	ASSERT3U(rootzp->z_id, ==, roid);
990	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &roid, tx);
991	ASSERT(error == 0);
992
993	mutex_destroy(&zfsvfs.z_znodes_lock);
994	kmem_cache_free(znode_cache, rootzp);
995}
996#endif /* _KERNEL */
997
998/*
999 * Given an object number, return its parent object number and whether
1000 * or not the object is an extended attribute directory.
1001 */
1002static int
1003zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
1004{
1005	dmu_buf_t *db;
1006	dmu_object_info_t doi;
1007	znode_phys_t *zp;
1008	int error;
1009
1010	if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
1011		return (error);
1012
1013	dmu_object_info_from_db(db, &doi);
1014	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1015	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1016		dmu_buf_rele(db, FTAG);
1017		return (EINVAL);
1018	}
1019
1020	zp = db->db_data;
1021	*pobjp = zp->zp_parent;
1022	*is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
1023	    S_ISDIR(zp->zp_mode);
1024	dmu_buf_rele(db, FTAG);
1025
1026	return (0);
1027}
1028
1029int
1030zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1031{
1032	char *path = buf + len - 1;
1033	int error;
1034
1035	*path = '\0';
1036
1037	for (;;) {
1038		uint64_t pobj;
1039		char component[MAXNAMELEN + 2];
1040		size_t complen;
1041		int is_xattrdir;
1042
1043		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
1044		    &is_xattrdir)) != 0)
1045			break;
1046
1047		if (pobj == obj) {
1048			if (path[0] != '/')
1049				*--path = '/';
1050			break;
1051		}
1052
1053		component[0] = '/';
1054		if (is_xattrdir) {
1055			(void) sprintf(component + 1, "<xattrdir>");
1056		} else {
1057			error = zap_value_search(osp, pobj, obj, component + 1);
1058			if (error != 0)
1059				break;
1060		}
1061
1062		complen = strlen(component);
1063		path -= complen;
1064		ASSERT(path >= buf);
1065		bcopy(component, path, complen);
1066		obj = pobj;
1067	}
1068
1069	if (error == 0)
1070		(void) memmove(buf, path, buf + len - path);
1071	return (error);
1072}
1073