xref: /freebsd/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_znode_os.c (revision d0abb9a6399accc9053e2808052be00a6754ecef)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
25  * Copyright (c) 2014 Integros [integros.com]
26  */
27 
28 /* Portions Copyright 2007 Jeremy Teo */
29 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
30 
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/resource.h>
37 #include <sys/resourcevar.h>
38 #include <sys/mntent.h>
39 #include <sys/u8_textprep.h>
40 #include <sys/dsl_dataset.h>
41 #include <sys/vfs.h>
42 #include <sys/vnode.h>
43 #include <sys/file.h>
44 #include <sys/kmem.h>
45 #include <sys/errno.h>
46 #include <sys/unistd.h>
47 #include <sys/atomic.h>
48 #include <sys/zfs_dir.h>
49 #include <sys/zfs_acl.h>
50 #include <sys/zfs_ioctl.h>
51 #include <sys/zfs_rlock.h>
52 #include <sys/zfs_fuid.h>
53 #include <sys/dnode.h>
54 #include <sys/fs/zfs.h>
55 #include <sys/dmu.h>
56 #include <sys/dmu_objset.h>
57 #include <sys/dmu_tx.h>
58 #include <sys/zfs_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 
66 #include "zfs_prop.h"
67 #include "zfs_comutil.h"
68 
69 /* Used by fstat(1). */
70 #ifdef SYSCTL_SIZEOF
71 SYSCTL_SIZEOF(znode, znode_t);
72 #else
73 SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD,
74 	SYSCTL_NULL_INT_PTR, sizeof (znode_t), "sizeof(znode_t)");
75 #endif
76 
77 /*
78  * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
79  * turned on when DEBUG is also defined.
80  */
81 #ifdef	ZFS_DEBUG
82 #define	ZNODE_STATS
83 #endif	/* DEBUG */
84 
85 #ifdef	ZNODE_STATS
86 #define	ZNODE_STAT_ADD(stat)			((stat)++)
87 #else
88 #define	ZNODE_STAT_ADD(stat)			/* nothing */
89 #endif	/* ZNODE_STATS */
90 
91 #if !defined(KMEM_DEBUG)
92 #define	_ZFS_USE_SMR
93 static uma_zone_t znode_uma_zone;
94 #else
95 static kmem_cache_t *znode_cache = NULL;
96 #endif
97 
98 extern struct vop_vector zfs_vnodeops;
99 extern struct vop_vector zfs_fifoops;
100 extern struct vop_vector zfs_shareops;
101 
102 
103 /*
104  * This callback is invoked when acquiring a RL_WRITER or RL_APPEND lock on
105  * z_rangelock. It will modify the offset and length of the lock to reflect
106  * znode-specific information, and convert RL_APPEND to RL_WRITER.  This is
107  * called with the rangelock_t's rl_lock held, which avoids races.
108  */
109 static void
zfs_rangelock_cb(zfs_locked_range_t * new,void * arg)110 zfs_rangelock_cb(zfs_locked_range_t *new, void *arg)
111 {
112 	znode_t *zp = arg;
113 
114 	/*
115 	 * If in append mode, convert to writer and lock starting at the
116 	 * current end of file.
117 	 */
118 	if (new->lr_type == RL_APPEND) {
119 		new->lr_offset = zp->z_size;
120 		new->lr_type = RL_WRITER;
121 	}
122 
123 	/*
124 	 * If we need to grow the block size then lock the whole file range.
125 	 */
126 	uint64_t end_size = MAX(zp->z_size, new->lr_offset + new->lr_length);
127 	if (end_size > zp->z_blksz && (!ISP2(zp->z_blksz) ||
128 	    zp->z_blksz < ZTOZSB(zp)->z_max_blksz)) {
129 		new->lr_offset = 0;
130 		new->lr_length = UINT64_MAX;
131 	}
132 }
133 
134 static int
zfs_znode_cache_constructor(void * buf,void * arg,int kmflags)135 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
136 {
137 	znode_t *zp = buf;
138 
139 	POINTER_INVALIDATE(&zp->z_zfsvfs);
140 
141 	list_link_init(&zp->z_link_node);
142 
143 	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
144 	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
145 	rw_init(&zp->z_xattr_lock, NULL, RW_DEFAULT, NULL);
146 
147 	zfs_rangelock_init(&zp->z_rangelock, zfs_rangelock_cb, zp);
148 
149 	zp->z_acl_cached = NULL;
150 	zp->z_xattr_cached = NULL;
151 	zp->z_xattr_parent = 0;
152 	zp->z_vnode = NULL;
153 
154 	return (0);
155 }
156 
157 static void
zfs_znode_cache_destructor(void * buf,void * arg)158 zfs_znode_cache_destructor(void *buf, void *arg)
159 {
160 	(void) arg;
161 	znode_t *zp = buf;
162 
163 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
164 	ASSERT0P(zp->z_vnode);
165 	ASSERT(!list_link_active(&zp->z_link_node));
166 	mutex_destroy(&zp->z_lock);
167 	mutex_destroy(&zp->z_acl_lock);
168 	rw_destroy(&zp->z_xattr_lock);
169 	zfs_rangelock_fini(&zp->z_rangelock);
170 
171 	ASSERT0P(zp->z_acl_cached);
172 	ASSERT0P(zp->z_xattr_cached);
173 }
174 
175 
176 #ifdef _ZFS_USE_SMR
177 VFS_SMR_DECLARE;
178 
179 static int
zfs_znode_cache_constructor_smr(void * mem,int size __unused,void * private,int flags)180 zfs_znode_cache_constructor_smr(void *mem, int size __unused, void *private,
181     int flags)
182 {
183 	return (zfs_znode_cache_constructor(mem, private, flags));
184 }
185 
186 static void
zfs_znode_cache_destructor_smr(void * mem,int size __unused,void * private)187 zfs_znode_cache_destructor_smr(void *mem, int size __unused, void *private)
188 {
189 	zfs_znode_cache_destructor(mem, private);
190 }
191 
192 void
zfs_znode_init(void)193 zfs_znode_init(void)
194 {
195 	/*
196 	 * Initialize zcache
197 	 */
198 	ASSERT0P(znode_uma_zone);
199 	znode_uma_zone = uma_zcreate("zfs_znode_cache",
200 	    sizeof (znode_t), zfs_znode_cache_constructor_smr,
201 	    zfs_znode_cache_destructor_smr, NULL, NULL, 0, 0);
202 	VFS_SMR_ZONE_SET(znode_uma_zone);
203 }
204 
205 static znode_t *
zfs_znode_alloc_kmem(int flags)206 zfs_znode_alloc_kmem(int flags)
207 {
208 	return (uma_zalloc_smr(znode_uma_zone, flags));
209 }
210 
211 static void
zfs_znode_free_kmem(znode_t * zp)212 zfs_znode_free_kmem(znode_t *zp)
213 {
214 	if (zp->z_xattr_cached) {
215 		nvlist_free(zp->z_xattr_cached);
216 		zp->z_xattr_cached = NULL;
217 	}
218 	uma_zfree_smr(znode_uma_zone, zp);
219 }
220 #else
221 void
zfs_znode_init(void)222 zfs_znode_init(void)
223 {
224 	/*
225 	 * Initialize zcache
226 	 */
227 	ASSERT0P(znode_cache);
228 	znode_cache = kmem_cache_create("zfs_znode_cache",
229 	    sizeof (znode_t), 0, zfs_znode_cache_constructor,
230 	    zfs_znode_cache_destructor, NULL, NULL, NULL, KMC_RECLAIMABLE);
231 }
232 
233 static znode_t *
zfs_znode_alloc_kmem(int flags)234 zfs_znode_alloc_kmem(int flags)
235 {
236 	return (kmem_cache_alloc(znode_cache, flags));
237 }
238 
239 static void
zfs_znode_free_kmem(znode_t * zp)240 zfs_znode_free_kmem(znode_t *zp)
241 {
242 	if (zp->z_xattr_cached) {
243 		nvlist_free(zp->z_xattr_cached);
244 		zp->z_xattr_cached = NULL;
245 	}
246 	kmem_cache_free(znode_cache, zp);
247 }
248 #endif
249 
250 void
zfs_znode_fini(void)251 zfs_znode_fini(void)
252 {
253 	/*
254 	 * Cleanup zcache
255 	 */
256 #ifdef _ZFS_USE_SMR
257 	if (znode_uma_zone) {
258 		uma_zdestroy(znode_uma_zone);
259 		znode_uma_zone = NULL;
260 	}
261 #else
262 	if (znode_cache) {
263 		kmem_cache_destroy(znode_cache);
264 		znode_cache = NULL;
265 	}
266 #endif
267 }
268 
269 
270 static int
zfs_create_share_dir(zfsvfs_t * zfsvfs,dmu_tx_t * tx)271 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
272 {
273 	zfs_acl_ids_t acl_ids;
274 	vattr_t vattr;
275 	znode_t *sharezp;
276 	znode_t *zp;
277 	int error;
278 
279 	vattr.va_mask = AT_MODE|AT_UID|AT_GID;
280 	vattr.va_type = VDIR;
281 	vattr.va_mode = S_IFDIR|0555;
282 	vattr.va_uid = crgetuid(kcred);
283 	vattr.va_gid = crgetgid(kcred);
284 
285 	sharezp = zfs_znode_alloc_kmem(KM_SLEEP);
286 	ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
287 	sharezp->z_unlinked = 0;
288 	sharezp->z_atime_dirty = 0;
289 	sharezp->z_zfsvfs = zfsvfs;
290 	sharezp->z_is_sa = zfsvfs->z_use_sa;
291 	sharezp->z_pflags = 0;
292 
293 	VERIFY0(zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
294 	    kcred, NULL, &acl_ids, NULL));
295 	zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
296 	ASSERT3P(zp, ==, sharezp);
297 	POINTER_INVALIDATE(&sharezp->z_zfsvfs);
298 	error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
299 	    ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
300 	zfsvfs->z_shares_dir = sharezp->z_id;
301 
302 	zfs_acl_ids_free(&acl_ids);
303 	sa_handle_destroy(sharezp->z_sa_hdl);
304 	zfs_znode_free_kmem(sharezp);
305 
306 	return (error);
307 }
308 
309 /*
310  * define a couple of values we need available
311  * for both 64 and 32 bit environments.
312  */
313 #ifndef NBITSMINOR64
314 #define	NBITSMINOR64	32
315 #endif
316 #ifndef MAXMAJ64
317 #define	MAXMAJ64	0xffffffffUL
318 #endif
319 #ifndef	MAXMIN64
320 #define	MAXMIN64	0xffffffffUL
321 #endif
322 
323 /*
324  * Create special expldev for ZFS private use.
325  * Can't use standard expldev since it doesn't do
326  * what we want.  The standard expldev() takes a
327  * dev32_t in LP64 and expands it to a long dev_t.
328  * We need an interface that takes a dev32_t in ILP32
329  * and expands it to a long dev_t.
330  */
331 static uint64_t
zfs_expldev(dev_t dev)332 zfs_expldev(dev_t dev)
333 {
334 	return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
335 }
336 /*
337  * Special cmpldev for ZFS private use.
338  * Can't use standard cmpldev since it takes
339  * a long dev_t and compresses it to dev32_t in
340  * LP64.  We need to do a compaction of a long dev_t
341  * to a dev32_t in ILP32.
342  */
343 dev_t
zfs_cmpldev(uint64_t dev)344 zfs_cmpldev(uint64_t dev)
345 {
346 	return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
347 }
348 
349 static void
zfs_znode_sa_init(zfsvfs_t * zfsvfs,znode_t * zp,dmu_buf_t * db,dmu_object_type_t obj_type,sa_handle_t * sa_hdl)350 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
351     dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
352 {
353 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
354 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
355 
356 	ASSERT0P(zp->z_sa_hdl);
357 	ASSERT0P(zp->z_acl_cached);
358 	if (sa_hdl == NULL) {
359 		VERIFY0(sa_handle_get_from_db(zfsvfs->z_os, db, zp,
360 		    SA_HDL_SHARED, &zp->z_sa_hdl));
361 	} else {
362 		zp->z_sa_hdl = sa_hdl;
363 		sa_set_userp(sa_hdl, zp);
364 	}
365 
366 	zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
367 
368 	/*
369 	 * Slap on VROOT if we are the root znode unless we are the root
370 	 * node of a snapshot mounted under .zfs.
371 	 */
372 	if (zp->z_id == zfsvfs->z_root && zfsvfs->z_parent == zfsvfs)
373 		ZTOV(zp)->v_flag |= VROOT;
374 }
375 
376 void
zfs_znode_dmu_fini(znode_t * zp)377 zfs_znode_dmu_fini(znode_t *zp)
378 {
379 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
380 	    ZFS_TEARDOWN_INACTIVE_WRITE_HELD(zp->z_zfsvfs));
381 
382 	sa_handle_destroy(zp->z_sa_hdl);
383 	zp->z_sa_hdl = NULL;
384 }
385 
386 static void
zfs_vnode_forget(vnode_t * vp)387 zfs_vnode_forget(vnode_t *vp)
388 {
389 
390 	/* copied from insmntque_stddtr */
391 	vp->v_data = NULL;
392 	vp->v_op = &dead_vnodeops;
393 	vgone(vp);
394 	vput(vp);
395 }
396 
397 /*
398  * Construct a new znode/vnode and initialize.
399  *
400  * This does not do a call to dmu_set_user() that is
401  * up to the caller to do, in case you don't want to
402  * return the znode
403  */
404 static znode_t *
zfs_znode_alloc(zfsvfs_t * zfsvfs,dmu_buf_t * db,int blksz,dmu_object_type_t obj_type,sa_handle_t * hdl)405 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
406     dmu_object_type_t obj_type, sa_handle_t *hdl)
407 {
408 	znode_t	*zp;
409 	vnode_t *vp;
410 	uint64_t mode;
411 	uint64_t parent;
412 #ifdef notyet
413 	uint64_t mtime[2], ctime[2];
414 #endif
415 	uint64_t projid = ZFS_DEFAULT_PROJID;
416 	sa_bulk_attr_t bulk[9];
417 	int count = 0;
418 	int error;
419 
420 	zp = zfs_znode_alloc_kmem(KM_SLEEP);
421 
422 #ifndef _ZFS_USE_SMR
423 	KASSERT((zfsvfs->z_parent->z_vfs->mnt_kern_flag & MNTK_FPLOOKUP) == 0,
424 	    ("%s: fast path lookup enabled without smr", __func__));
425 #endif
426 
427 	KASSERT(curthread->td_vp_reserved != NULL,
428 	    ("zfs_znode_alloc: getnewvnode without any vnodes reserved"));
429 	error = getnewvnode("zfs", zfsvfs->z_parent->z_vfs, &zfs_vnodeops, &vp);
430 	if (error != 0) {
431 		zfs_znode_free_kmem(zp);
432 		return (NULL);
433 	}
434 	zp->z_vnode = vp;
435 	vp->v_data = zp;
436 
437 	/*
438 	 * Acquire the vnode lock before any possible interaction with the
439 	 * outside world.  Specifically, there is an error path that calls
440 	 * zfs_vnode_forget() and the vnode should be exclusively locked.
441 	 */
442 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
443 
444 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
445 
446 	zp->z_sa_hdl = NULL;
447 	zp->z_unlinked = 0;
448 	zp->z_atime_dirty = 0;
449 	zp->z_mapcnt = 0;
450 	zp->z_id = db->db_object;
451 	zp->z_blksz = blksz;
452 	zp->z_seq = 0x7A4653;
453 	zp->z_sync_cnt = 0;
454 	atomic_store_ptr(&zp->z_cached_symlink, NULL);
455 
456 	zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
457 
458 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
459 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
460 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
461 	    &zp->z_size, 8);
462 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
463 	    &zp->z_links, 8);
464 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
465 	    &zp->z_pflags, 8);
466 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
467 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
468 	    &zp->z_atime, 16);
469 #ifdef notyet
470 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
471 	    &mtime, 16);
472 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
473 	    &ctime, 16);
474 #endif
475 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
476 	    &zp->z_uid, 8);
477 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
478 	    &zp->z_gid, 8);
479 
480 	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0 ||
481 	    (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
482 	    (zp->z_pflags & ZFS_PROJID) &&
483 	    sa_lookup(zp->z_sa_hdl, SA_ZPL_PROJID(zfsvfs), &projid, 8) != 0)) {
484 		if (hdl == NULL)
485 			sa_handle_destroy(zp->z_sa_hdl);
486 		zfs_vnode_forget(vp);
487 		zp->z_vnode = NULL;
488 		zfs_znode_free_kmem(zp);
489 		return (NULL);
490 	}
491 
492 	zp->z_projid = projid;
493 	zp->z_mode = mode;
494 
495 	/* Cache the xattr parent id */
496 	if (zp->z_pflags & ZFS_XATTR)
497 		zp->z_xattr_parent = parent;
498 
499 	vp->v_type = IFTOVT((mode_t)mode);
500 
501 	switch (vp->v_type) {
502 	case VDIR:
503 		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
504 		break;
505 	case VFIFO:
506 		vp->v_op = &zfs_fifoops;
507 		break;
508 	case VREG:
509 		if (parent == zfsvfs->z_shares_dir) {
510 			ASSERT0(zp->z_uid);
511 			ASSERT0(zp->z_gid);
512 			vp->v_op = &zfs_shareops;
513 		}
514 		break;
515 	default:
516 			break;
517 	}
518 
519 	mutex_enter(&zfsvfs->z_znodes_lock);
520 	list_insert_tail(&zfsvfs->z_all_znodes, zp);
521 	zp->z_zfsvfs = zfsvfs;
522 	mutex_exit(&zfsvfs->z_znodes_lock);
523 
524 #if __FreeBSD_version >= 1400077
525 	vn_set_state(vp, VSTATE_CONSTRUCTED);
526 #endif
527 	VN_LOCK_AREC(vp);
528 	if (vp->v_type != VFIFO)
529 		VN_LOCK_ASHARE(vp);
530 
531 	return (zp);
532 }
533 
534 static uint64_t empty_xattr;
535 static uint64_t pad[4];
536 static zfs_acl_phys_t acl_phys;
537 /*
538  * Create a new DMU object to hold a zfs znode.
539  *
540  *	IN:	dzp	- parent directory for new znode
541  *		vap	- file attributes for new znode
542  *		tx	- dmu transaction id for zap operations
543  *		cr	- credentials of caller
544  *		flag	- flags:
545  *			  IS_ROOT_NODE	- new object will be root
546  *			  IS_XATTR	- new object is an attribute
547  *		bonuslen - length of bonus buffer
548  *		setaclp  - File/Dir initial ACL
549  *		fuidp	 - Tracks fuid allocation.
550  *
551  *	OUT:	zpp	- allocated znode
552  *
553  */
554 void
zfs_mknode(znode_t * dzp,vattr_t * vap,dmu_tx_t * tx,cred_t * cr,uint_t flag,znode_t ** zpp,zfs_acl_ids_t * acl_ids)555 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
556     uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
557 {
558 	uint64_t	crtime[2], atime[2], mtime[2], ctime[2];
559 	uint64_t	mode, size, links, parent, pflags;
560 	uint64_t	dzp_pflags = 0;
561 	uint64_t	projid = ZFS_DEFAULT_PROJID;
562 	uint64_t	rdev = 0;
563 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
564 	dmu_buf_t	*db;
565 	timestruc_t	now;
566 	uint64_t	gen, obj;
567 	int		bonuslen;
568 	int		dnodesize;
569 	sa_handle_t	*sa_hdl;
570 	dmu_object_type_t obj_type;
571 	sa_bulk_attr_t	*sa_attrs;
572 	int		cnt = 0;
573 	zfs_acl_locator_cb_t locate = { 0 };
574 
575 	ASSERT3P(vap, !=, NULL);
576 	ASSERT3U((vap->va_mask & AT_MODE), ==, AT_MODE);
577 
578 	if (zfsvfs->z_replay) {
579 		obj = vap->va_nodeid;
580 		now = vap->va_ctime;		/* see zfs_replay_create() */
581 		gen = vap->va_nblocks;		/* ditto */
582 		dnodesize = vap->va_fsid;	/* ditto */
583 	} else {
584 		obj = 0;
585 		vfs_timestamp(&now);
586 		gen = dmu_tx_get_txg(tx);
587 		dnodesize = dmu_objset_dnodesize(zfsvfs->z_os);
588 	}
589 
590 	if (dnodesize == 0)
591 		dnodesize = DNODE_MIN_SIZE;
592 
593 	obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
594 	bonuslen = (obj_type == DMU_OT_SA) ?
595 	    DN_BONUS_SIZE(dnodesize) : ZFS_OLD_ZNODE_PHYS_SIZE;
596 
597 	/*
598 	 * Create a new DMU object.
599 	 */
600 	/*
601 	 * There's currently no mechanism for pre-reading the blocks that will
602 	 * be needed to allocate a new object, so we accept the small chance
603 	 * that there will be an i/o error and we will fail one of the
604 	 * assertions below.
605 	 */
606 	if (vap->va_type == VDIR) {
607 		if (zfsvfs->z_replay) {
608 			VERIFY0(zap_create_claim_norm_dnsize(zfsvfs->z_os, obj,
609 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
610 			    obj_type, bonuslen, dnodesize, tx));
611 		} else {
612 			obj = zap_create_norm_dnsize(zfsvfs->z_os,
613 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
614 			    obj_type, bonuslen, dnodesize, tx);
615 		}
616 	} else {
617 		if (zfsvfs->z_replay) {
618 			VERIFY0(dmu_object_claim_dnsize(zfsvfs->z_os, obj,
619 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
620 			    obj_type, bonuslen, dnodesize, tx));
621 		} else {
622 			obj = dmu_object_alloc_dnsize(zfsvfs->z_os,
623 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
624 			    obj_type, bonuslen, dnodesize, tx);
625 		}
626 	}
627 
628 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
629 	VERIFY0(sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
630 
631 	/*
632 	 * If this is the root, fix up the half-initialized parent pointer
633 	 * to reference the just-allocated physical data area.
634 	 */
635 	if (flag & IS_ROOT_NODE) {
636 		dzp->z_id = obj;
637 	} else {
638 		dzp_pflags = dzp->z_pflags;
639 	}
640 
641 	/*
642 	 * If parent is an xattr, so am I.
643 	 */
644 	if (dzp_pflags & ZFS_XATTR) {
645 		flag |= IS_XATTR;
646 	}
647 
648 	if (zfsvfs->z_use_fuids)
649 		pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
650 	else
651 		pflags = 0;
652 
653 	if (vap->va_type == VDIR) {
654 		size = 2;		/* contents ("." and "..") */
655 		links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
656 	} else {
657 		size = links = 0;
658 	}
659 
660 	if (vap->va_type == VBLK || vap->va_type == VCHR) {
661 		rdev = zfs_expldev(vap->va_rdev);
662 	}
663 
664 	parent = dzp->z_id;
665 	mode = acl_ids->z_mode;
666 	if (flag & IS_XATTR)
667 		pflags |= ZFS_XATTR;
668 
669 	if (vap->va_type == VREG || vap->va_type == VDIR) {
670 		/*
671 		 * With ZFS_PROJID flag, we can easily know whether there is
672 		 * project ID stored on disk or not. See zpl_get_file_info().
673 		 */
674 		if (obj_type != DMU_OT_ZNODE &&
675 		    dmu_objset_projectquota_enabled(zfsvfs->z_os))
676 			pflags |= ZFS_PROJID;
677 
678 		/*
679 		 * Inherit project ID from parent if required.
680 		 */
681 		projid = zfs_inherit_projid(dzp);
682 		if (dzp_pflags & ZFS_PROJINHERIT)
683 			pflags |= ZFS_PROJINHERIT;
684 	}
685 
686 	/*
687 	 * No execs denied will be determined when zfs_mode_compute() is called.
688 	 */
689 	pflags |= acl_ids->z_aclp->z_hints &
690 	    (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
691 	    ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
692 
693 	ZFS_TIME_ENCODE(&now, crtime);
694 	ZFS_TIME_ENCODE(&now, ctime);
695 
696 	if (vap->va_mask & AT_ATIME) {
697 		ZFS_TIME_ENCODE(&vap->va_atime, atime);
698 	} else {
699 		ZFS_TIME_ENCODE(&now, atime);
700 	}
701 
702 	if (vap->va_mask & AT_MTIME) {
703 		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
704 	} else {
705 		ZFS_TIME_ENCODE(&now, mtime);
706 	}
707 
708 	/* Now add in all of the "SA" attributes */
709 	VERIFY0(sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
710 	    &sa_hdl));
711 
712 	/*
713 	 * Setup the array of attributes to be replaced/set on the new file
714 	 *
715 	 * order for  DMU_OT_ZNODE is critical since it needs to be constructed
716 	 * in the old znode_phys_t format.  Don't change this ordering
717 	 */
718 	sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
719 
720 	if (obj_type == DMU_OT_ZNODE) {
721 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
722 		    NULL, &atime, 16);
723 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
724 		    NULL, &mtime, 16);
725 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
726 		    NULL, &ctime, 16);
727 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
728 		    NULL, &crtime, 16);
729 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
730 		    NULL, &gen, 8);
731 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
732 		    NULL, &mode, 8);
733 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
734 		    NULL, &size, 8);
735 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
736 		    NULL, &parent, 8);
737 	} else {
738 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
739 		    NULL, &mode, 8);
740 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
741 		    NULL, &size, 8);
742 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
743 		    NULL, &gen, 8);
744 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs),
745 		    NULL, &acl_ids->z_fuid, 8);
746 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs),
747 		    NULL, &acl_ids->z_fgid, 8);
748 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
749 		    NULL, &parent, 8);
750 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
751 		    NULL, &pflags, 8);
752 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
753 		    NULL, &atime, 16);
754 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
755 		    NULL, &mtime, 16);
756 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
757 		    NULL, &ctime, 16);
758 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
759 		    NULL, &crtime, 16);
760 	}
761 
762 	SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
763 
764 	if (obj_type == DMU_OT_ZNODE) {
765 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
766 		    &empty_xattr, 8);
767 	} else if (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
768 	    pflags & ZFS_PROJID) {
769 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PROJID(zfsvfs),
770 		    NULL, &projid, 8);
771 	}
772 	if (obj_type == DMU_OT_ZNODE ||
773 	    (vap->va_type == VBLK || vap->va_type == VCHR)) {
774 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
775 		    NULL, &rdev, 8);
776 
777 	}
778 	if (obj_type == DMU_OT_ZNODE) {
779 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
780 		    NULL, &pflags, 8);
781 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
782 		    &acl_ids->z_fuid, 8);
783 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
784 		    &acl_ids->z_fgid, 8);
785 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
786 		    sizeof (uint64_t) * 4);
787 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
788 		    &acl_phys, sizeof (zfs_acl_phys_t));
789 	} else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
790 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
791 		    &acl_ids->z_aclp->z_acl_count, 8);
792 		locate.cb_aclp = acl_ids->z_aclp;
793 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
794 		    zfs_acl_data_locator, &locate,
795 		    acl_ids->z_aclp->z_acl_bytes);
796 		mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
797 		    acl_ids->z_fuid, acl_ids->z_fgid);
798 	}
799 
800 	VERIFY0(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx));
801 
802 	if (!(flag & IS_ROOT_NODE)) {
803 		*zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
804 		ASSERT3P(*zpp, !=, NULL);
805 	} else {
806 		/*
807 		 * If we are creating the root node, the "parent" we
808 		 * passed in is the znode for the root.
809 		 */
810 		*zpp = dzp;
811 
812 		(*zpp)->z_sa_hdl = sa_hdl;
813 	}
814 
815 	(*zpp)->z_pflags = pflags;
816 	(*zpp)->z_mode = mode;
817 	(*zpp)->z_dnodesize = dnodesize;
818 	(*zpp)->z_projid = projid;
819 
820 	if (vap->va_mask & AT_XVATTR)
821 		zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
822 
823 	if (obj_type == DMU_OT_ZNODE ||
824 	    acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
825 		VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
826 	}
827 	if (!(flag & IS_ROOT_NODE)) {
828 		vnode_t *vp = ZTOV(*zpp);
829 		vp->v_vflag |= VV_FORCEINSMQ;
830 		int err = insmntque(vp, zfsvfs->z_vfs);
831 		vp->v_vflag &= ~VV_FORCEINSMQ;
832 		(void) err;
833 		KASSERT(err == 0, ("insmntque() failed: error %d", err));
834 	}
835 	kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
836 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
837 }
838 
839 /*
840  * Update in-core attributes.  It is assumed the caller will be doing an
841  * sa_bulk_update to push the changes out.
842  */
843 void
zfs_xvattr_set(znode_t * zp,xvattr_t * xvap,dmu_tx_t * tx)844 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
845 {
846 	xoptattr_t *xoap;
847 
848 	xoap = xva_getxoptattr(xvap);
849 	ASSERT3P(xoap, !=, NULL);
850 
851 	if (zp->z_zfsvfs->z_replay == B_FALSE) {
852 		ASSERT_VOP_IN_SEQC(ZTOV(zp));
853 	}
854 
855 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
856 		uint64_t times[2];
857 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
858 		(void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
859 		    &times, sizeof (times), tx);
860 		XVA_SET_RTN(xvap, XAT_CREATETIME);
861 	}
862 	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
863 		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
864 		    zp->z_pflags, tx);
865 		XVA_SET_RTN(xvap, XAT_READONLY);
866 	}
867 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
868 		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
869 		    zp->z_pflags, tx);
870 		XVA_SET_RTN(xvap, XAT_HIDDEN);
871 	}
872 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
873 		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
874 		    zp->z_pflags, tx);
875 		XVA_SET_RTN(xvap, XAT_SYSTEM);
876 	}
877 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
878 		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
879 		    zp->z_pflags, tx);
880 		XVA_SET_RTN(xvap, XAT_ARCHIVE);
881 	}
882 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
883 		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
884 		    zp->z_pflags, tx);
885 		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
886 	}
887 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
888 		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
889 		    zp->z_pflags, tx);
890 		XVA_SET_RTN(xvap, XAT_NOUNLINK);
891 	}
892 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
893 		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
894 		    zp->z_pflags, tx);
895 		XVA_SET_RTN(xvap, XAT_APPENDONLY);
896 	}
897 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
898 		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
899 		    zp->z_pflags, tx);
900 		XVA_SET_RTN(xvap, XAT_NODUMP);
901 	}
902 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
903 		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
904 		    zp->z_pflags, tx);
905 		XVA_SET_RTN(xvap, XAT_OPAQUE);
906 	}
907 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
908 		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
909 		    xoap->xoa_av_quarantined, zp->z_pflags, tx);
910 		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
911 	}
912 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
913 		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
914 		    zp->z_pflags, tx);
915 		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
916 	}
917 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
918 		zfs_sa_set_scanstamp(zp, xvap, tx);
919 		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
920 	}
921 	if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
922 		ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
923 		    zp->z_pflags, tx);
924 		XVA_SET_RTN(xvap, XAT_REPARSE);
925 	}
926 	if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
927 		ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
928 		    zp->z_pflags, tx);
929 		XVA_SET_RTN(xvap, XAT_OFFLINE);
930 	}
931 	if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
932 		ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
933 		    zp->z_pflags, tx);
934 		XVA_SET_RTN(xvap, XAT_SPARSE);
935 	}
936 	if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
937 		ZFS_ATTR_SET(zp, ZFS_PROJINHERIT, xoap->xoa_projinherit,
938 		    zp->z_pflags, tx);
939 		XVA_SET_RTN(xvap, XAT_PROJINHERIT);
940 	}
941 }
942 
943 int
zfs_zget(zfsvfs_t * zfsvfs,uint64_t obj_num,znode_t ** zpp)944 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
945 {
946 	dmu_object_info_t doi;
947 	dmu_buf_t	*db;
948 	znode_t		*zp;
949 	vnode_t		*vp;
950 	sa_handle_t	*hdl;
951 	int locked;
952 	int err;
953 
954 	getnewvnode_reserve();
955 again:
956 	*zpp = NULL;
957 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
958 
959 	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
960 	if (err) {
961 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
962 		getnewvnode_drop_reserve();
963 		return (err);
964 	}
965 
966 	dmu_object_info_from_db(db, &doi);
967 	if (doi.doi_bonus_type != DMU_OT_SA &&
968 	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
969 	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
970 	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
971 		sa_buf_rele(db, NULL);
972 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
973 		getnewvnode_drop_reserve();
974 		return (SET_ERROR(EINVAL));
975 	}
976 
977 	hdl = dmu_buf_get_user(db);
978 	if (hdl != NULL) {
979 		zp = sa_get_userdata(hdl);
980 
981 		/*
982 		 * Since "SA" does immediate eviction we
983 		 * should never find a sa handle that doesn't
984 		 * know about the znode.
985 		 */
986 		ASSERT3P(zp, !=, NULL);
987 		ASSERT3U(zp->z_id, ==, obj_num);
988 		if (zp->z_unlinked) {
989 			err = SET_ERROR(ENOENT);
990 		} else {
991 			vp = ZTOV(zp);
992 			/*
993 			 * Don't let the vnode disappear after
994 			 * ZFS_OBJ_HOLD_EXIT.
995 			 */
996 			VN_HOLD(vp);
997 			*zpp = zp;
998 			err = 0;
999 		}
1000 
1001 		sa_buf_rele(db, NULL);
1002 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1003 
1004 		if (err) {
1005 			getnewvnode_drop_reserve();
1006 			return (err);
1007 		}
1008 
1009 		locked = VOP_ISLOCKED(vp);
1010 		VI_LOCK(vp);
1011 		if (VN_IS_DOOMED(vp) && locked != LK_EXCLUSIVE) {
1012 			/*
1013 			 * The vnode is doomed and this thread doesn't
1014 			 * hold the exclusive lock on it, so the vnode
1015 			 * must be being reclaimed by another thread.
1016 			 * Otherwise the doomed vnode is being reclaimed
1017 			 * by this thread and zfs_zget is called from
1018 			 * ZIL internals.
1019 			 */
1020 			VI_UNLOCK(vp);
1021 
1022 			/*
1023 			 * XXX vrele() locks the vnode when the last reference
1024 			 * is dropped.  Although in this case the vnode is
1025 			 * doomed / dead and so no inactivation is required,
1026 			 * the vnode lock is still acquired.  That could result
1027 			 * in a LOR with z_teardown_lock if another thread holds
1028 			 * the vnode's lock and tries to take z_teardown_lock.
1029 			 * But that is only possible if the other thread peforms
1030 			 * a ZFS vnode operation on the vnode.  That either
1031 			 * should not happen if the vnode is dead or the thread
1032 			 * should also have a reference to the vnode and thus
1033 			 * our reference is not last.
1034 			 */
1035 			VN_RELE(vp);
1036 			goto again;
1037 		}
1038 		VI_UNLOCK(vp);
1039 		getnewvnode_drop_reserve();
1040 		return (err);
1041 	}
1042 
1043 	/*
1044 	 * Not found create new znode/vnode
1045 	 * but only if file exists.
1046 	 *
1047 	 * There is a small window where zfs_vget() could
1048 	 * find this object while a file create is still in
1049 	 * progress.  This is checked for in zfs_znode_alloc()
1050 	 *
1051 	 * if zfs_znode_alloc() fails it will drop the hold on the
1052 	 * bonus buffer.
1053 	 */
1054 	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1055 	    doi.doi_bonus_type, NULL);
1056 	if (zp == NULL) {
1057 		err = SET_ERROR(ENOENT);
1058 	} else {
1059 		*zpp = zp;
1060 	}
1061 	if (err == 0) {
1062 		vnode_t *vp = ZTOV(zp);
1063 
1064 		err = insmntque(vp, zfsvfs->z_vfs);
1065 		if (err == 0) {
1066 			vp->v_hash = obj_num;
1067 			VOP_UNLOCK(vp);
1068 		} else {
1069 			zp->z_vnode = NULL;
1070 			zfs_znode_dmu_fini(zp);
1071 			zfs_znode_free(zp);
1072 			*zpp = NULL;
1073 		}
1074 	}
1075 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1076 	getnewvnode_drop_reserve();
1077 	return (err);
1078 }
1079 
1080 int
zfs_rezget(znode_t * zp)1081 zfs_rezget(znode_t *zp)
1082 {
1083 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1084 	dmu_object_info_t doi;
1085 	dmu_buf_t *db;
1086 	vnode_t *vp;
1087 	uint64_t obj_num = zp->z_id;
1088 	uint64_t mode, size;
1089 	sa_bulk_attr_t bulk[8];
1090 	int err;
1091 	int count = 0;
1092 	uint64_t gen;
1093 	uint64_t projid = ZFS_DEFAULT_PROJID;
1094 
1095 	/*
1096 	 * Remove cached pages before reloading the znode, so that they are not
1097 	 * lingering after we run into any error.  Ideally, we should vgone()
1098 	 * the vnode in case of error, but currently we cannot do that
1099 	 * because of the LOR between the vnode lock and z_teardown_lock.
1100 	 * So, instead, we have to "doom" the znode in the illumos style.
1101 	 *
1102 	 * Ignore invalid pages during the scan.  This is to avoid deadlocks
1103 	 * between page busying and the teardown lock, as pages are busied prior
1104 	 * to a VOP_GETPAGES operation, which acquires the teardown read lock.
1105 	 * Such pages will be invalid and can safely be skipped here.
1106 	 */
1107 	vp = ZTOV(zp);
1108 #if __FreeBSD_version >= 1400042
1109 	vn_pages_remove_valid(vp, 0, 0);
1110 #else
1111 	vn_pages_remove(vp, 0, 0);
1112 #endif
1113 
1114 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1115 
1116 	mutex_enter(&zp->z_acl_lock);
1117 	if (zp->z_acl_cached) {
1118 		zfs_acl_free(zp->z_acl_cached);
1119 		zp->z_acl_cached = NULL;
1120 	}
1121 	mutex_exit(&zp->z_acl_lock);
1122 
1123 	rw_enter(&zp->z_xattr_lock, RW_WRITER);
1124 	if (zp->z_xattr_cached) {
1125 		nvlist_free(zp->z_xattr_cached);
1126 		zp->z_xattr_cached = NULL;
1127 	}
1128 	rw_exit(&zp->z_xattr_lock);
1129 
1130 	ASSERT0P(zp->z_sa_hdl);
1131 	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1132 	if (err) {
1133 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1134 		return (err);
1135 	}
1136 
1137 	dmu_object_info_from_db(db, &doi);
1138 	if (doi.doi_bonus_type != DMU_OT_SA &&
1139 	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
1140 	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
1141 	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1142 		sa_buf_rele(db, NULL);
1143 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1144 		return (SET_ERROR(EINVAL));
1145 	}
1146 
1147 	zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1148 	size = zp->z_size;
1149 
1150 	/* reload cached values */
1151 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1152 	    &gen, sizeof (gen));
1153 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1154 	    &zp->z_size, sizeof (zp->z_size));
1155 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1156 	    &zp->z_links, sizeof (zp->z_links));
1157 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1158 	    &zp->z_pflags, sizeof (zp->z_pflags));
1159 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1160 	    &zp->z_atime, sizeof (zp->z_atime));
1161 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1162 	    &zp->z_uid, sizeof (zp->z_uid));
1163 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1164 	    &zp->z_gid, sizeof (zp->z_gid));
1165 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1166 	    &mode, sizeof (mode));
1167 
1168 	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1169 		zfs_znode_dmu_fini(zp);
1170 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1171 		return (SET_ERROR(EIO));
1172 	}
1173 
1174 	if (dmu_objset_projectquota_enabled(zfsvfs->z_os)) {
1175 		err = sa_lookup(zp->z_sa_hdl, SA_ZPL_PROJID(zfsvfs),
1176 		    &projid, 8);
1177 		if (err != 0 && err != ENOENT) {
1178 			zfs_znode_dmu_fini(zp);
1179 			ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1180 			return (err);
1181 		}
1182 	}
1183 
1184 	zp->z_projid = projid;
1185 	zp->z_mode = mode;
1186 
1187 	if (gen != zp->z_gen) {
1188 		zfs_znode_dmu_fini(zp);
1189 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1190 		return (SET_ERROR(EIO));
1191 	}
1192 
1193 	/*
1194 	 * It is highly improbable but still quite possible that two
1195 	 * objects in different datasets are created with the same
1196 	 * object numbers and in transaction groups with the same
1197 	 * numbers.  znodes corresponding to those objects would
1198 	 * have the same z_id and z_gen, but their other attributes
1199 	 * may be different.
1200 	 * zfs recv -F may replace one of such objects with the other.
1201 	 * As a result file properties recorded in the replaced
1202 	 * object's vnode may no longer match the received object's
1203 	 * properties.  At present the only cached property is the
1204 	 * files type recorded in v_type.
1205 	 * So, handle this case by leaving the old vnode and znode
1206 	 * disassociated from the actual object.  A new vnode and a
1207 	 * znode will be created if the object is accessed
1208 	 * (e.g. via a look-up).  The old vnode and znode will be
1209 	 * recycled when the last vnode reference is dropped.
1210 	 */
1211 	if (vp->v_type != IFTOVT((mode_t)zp->z_mode)) {
1212 		zfs_znode_dmu_fini(zp);
1213 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1214 		return (SET_ERROR(EIO));
1215 	}
1216 
1217 	/*
1218 	 * If the file has zero links, then it has been unlinked on the send
1219 	 * side and it must be in the received unlinked set.
1220 	 * We call zfs_znode_dmu_fini() now to prevent any accesses to the
1221 	 * stale data and to prevent automatically removal of the file in
1222 	 * zfs_zinactive().  The file will be removed either when it is removed
1223 	 * on the send side and the next incremental stream is received or
1224 	 * when the unlinked set gets processed.
1225 	 */
1226 	zp->z_unlinked = (zp->z_links == 0);
1227 	if (zp->z_unlinked) {
1228 		zfs_znode_dmu_fini(zp);
1229 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1230 		return (0);
1231 	}
1232 
1233 	zp->z_blksz = doi.doi_data_block_size;
1234 	if (zp->z_size != size)
1235 		vnode_pager_setsize(vp, zp->z_size);
1236 
1237 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1238 
1239 	return (0);
1240 }
1241 
1242 void
zfs_znode_delete(znode_t * zp,dmu_tx_t * tx)1243 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1244 {
1245 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1246 	objset_t *os = zfsvfs->z_os;
1247 	uint64_t obj = zp->z_id;
1248 	uint64_t acl_obj = zfs_external_acl(zp);
1249 
1250 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1251 	if (acl_obj) {
1252 		VERIFY(!zp->z_is_sa);
1253 		VERIFY0(dmu_object_free(os, acl_obj, tx));
1254 	}
1255 	VERIFY0(dmu_object_free(os, obj, tx));
1256 	zfs_znode_dmu_fini(zp);
1257 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1258 }
1259 
1260 void
zfs_zinactive(znode_t * zp)1261 zfs_zinactive(znode_t *zp)
1262 {
1263 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1264 	uint64_t z_id = zp->z_id;
1265 
1266 	ASSERT3P(zp->z_sa_hdl, !=, NULL);
1267 
1268 	/*
1269 	 * Don't allow a zfs_zget() while were trying to release this znode
1270 	 */
1271 	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1272 
1273 	/*
1274 	 * If this was the last reference to a file with no links, remove
1275 	 * the file from the file system unless the file system is mounted
1276 	 * read-only.  That can happen, for example, if the file system was
1277 	 * originally read-write, the file was opened, then unlinked and
1278 	 * the file system was made read-only before the file was finally
1279 	 * closed.  The file will remain in the unlinked set.
1280 	 */
1281 	if (zp->z_unlinked) {
1282 		ASSERT(!zfsvfs->z_issnap);
1283 		if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0) {
1284 			ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1285 			zfs_rmnode(zp);
1286 			return;
1287 		}
1288 	}
1289 
1290 	zfs_znode_dmu_fini(zp);
1291 	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1292 	zfs_znode_free(zp);
1293 }
1294 
1295 void
zfs_znode_free(znode_t * zp)1296 zfs_znode_free(znode_t *zp)
1297 {
1298 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1299 	char *symlink;
1300 
1301 	ASSERT0P(zp->z_sa_hdl);
1302 	zp->z_vnode = NULL;
1303 	mutex_enter(&zfsvfs->z_znodes_lock);
1304 	POINTER_INVALIDATE(&zp->z_zfsvfs);
1305 	list_remove(&zfsvfs->z_all_znodes, zp);
1306 	mutex_exit(&zfsvfs->z_znodes_lock);
1307 
1308 	symlink = atomic_load_ptr(&zp->z_cached_symlink);
1309 	if (symlink != NULL) {
1310 		atomic_store_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
1311 		    (uintptr_t)NULL);
1312 		cache_symlink_free(symlink, strlen(symlink) + 1);
1313 	}
1314 
1315 	if (zp->z_acl_cached) {
1316 		zfs_acl_free(zp->z_acl_cached);
1317 		zp->z_acl_cached = NULL;
1318 	}
1319 
1320 	zfs_znode_free_kmem(zp);
1321 }
1322 
1323 void
zfs_tstamp_update_setup_ext(znode_t * zp,uint_t flag,uint64_t mtime[2],uint64_t ctime[2],boolean_t have_tx)1324 zfs_tstamp_update_setup_ext(znode_t *zp, uint_t flag, uint64_t mtime[2],
1325     uint64_t ctime[2], boolean_t have_tx)
1326 {
1327 	timestruc_t	now;
1328 
1329 	vfs_timestamp(&now);
1330 
1331 	if (have_tx) {	/* will sa_bulk_update happen really soon? */
1332 		zp->z_atime_dirty = 0;
1333 		zp->z_seq++;
1334 	} else {
1335 		zp->z_atime_dirty = 1;
1336 	}
1337 
1338 	if (flag & AT_ATIME) {
1339 		ZFS_TIME_ENCODE(&now, zp->z_atime);
1340 	}
1341 
1342 	if (flag & AT_MTIME) {
1343 		ZFS_TIME_ENCODE(&now, mtime);
1344 		if (zp->z_zfsvfs->z_use_fuids) {
1345 			zp->z_pflags |= (ZFS_ARCHIVE |
1346 			    ZFS_AV_MODIFIED);
1347 		}
1348 	}
1349 
1350 	if (flag & AT_CTIME) {
1351 		ZFS_TIME_ENCODE(&now, ctime);
1352 		if (zp->z_zfsvfs->z_use_fuids)
1353 			zp->z_pflags |= ZFS_ARCHIVE;
1354 	}
1355 }
1356 
1357 
1358 void
zfs_tstamp_update_setup(znode_t * zp,uint_t flag,uint64_t mtime[2],uint64_t ctime[2])1359 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1360     uint64_t ctime[2])
1361 {
1362 	zfs_tstamp_update_setup_ext(zp, flag, mtime, ctime, B_TRUE);
1363 }
1364 /*
1365  * Grow the block size for a file.
1366  *
1367  *	IN:	zp	- znode of file to free data in.
1368  *		size	- requested block size
1369  *		tx	- open transaction.
1370  *
1371  * NOTE: this function assumes that the znode is write locked.
1372  */
1373 void
zfs_grow_blocksize(znode_t * zp,uint64_t size,dmu_tx_t * tx)1374 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1375 {
1376 	int		error;
1377 	u_longlong_t	dummy;
1378 
1379 	if (size <= zp->z_blksz)
1380 		return;
1381 	/*
1382 	 * If the file size is already greater than the current blocksize,
1383 	 * we will not grow.  If there is more than one block in a file,
1384 	 * the blocksize cannot change.
1385 	 */
1386 	if (zp->z_blksz && zp->z_size > zp->z_blksz)
1387 		return;
1388 
1389 	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1390 	    size, 0, tx);
1391 
1392 	if (error == ENOTSUP)
1393 		return;
1394 	ASSERT0(error);
1395 
1396 	/* What blocksize did we actually get? */
1397 	dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1398 }
1399 
1400 /*
1401  * Increase the file length
1402  *
1403  *	IN:	zp	- znode of file to free data in.
1404  *		end	- new end-of-file
1405  *
1406  *	RETURN:	0 on success, error code on failure
1407  */
1408 static int
zfs_extend(znode_t * zp,uint64_t end)1409 zfs_extend(znode_t *zp, uint64_t end)
1410 {
1411 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1412 	dmu_tx_t *tx;
1413 	zfs_locked_range_t *lr;
1414 	uint64_t newblksz;
1415 	int error;
1416 
1417 	/*
1418 	 * We will change zp_size, lock the whole file.
1419 	 */
1420 	lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
1421 
1422 	/*
1423 	 * Nothing to do if file already at desired length.
1424 	 */
1425 	if (end <= zp->z_size) {
1426 		zfs_rangelock_exit(lr);
1427 		return (0);
1428 	}
1429 	tx = dmu_tx_create(zfsvfs->z_os);
1430 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1431 	zfs_sa_upgrade_txholds(tx, zp);
1432 	if (end > zp->z_blksz &&
1433 	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1434 		/*
1435 		 * We are growing the file past the current block size.
1436 		 */
1437 		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1438 			/*
1439 			 * File's blocksize is already larger than the
1440 			 * "recordsize" property.  Only let it grow to
1441 			 * the next power of 2.
1442 			 */
1443 			ASSERT(!ISP2(zp->z_blksz));
1444 			newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1445 		} else {
1446 			newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1447 		}
1448 		dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1449 	} else {
1450 		newblksz = 0;
1451 	}
1452 
1453 	error = dmu_tx_assign(tx, DMU_TX_WAIT);
1454 	if (error) {
1455 		dmu_tx_abort(tx);
1456 		zfs_rangelock_exit(lr);
1457 		return (error);
1458 	}
1459 
1460 	if (newblksz)
1461 		zfs_grow_blocksize(zp, newblksz, tx);
1462 
1463 	zp->z_size = end;
1464 
1465 	VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1466 	    &zp->z_size, sizeof (zp->z_size), tx));
1467 
1468 	vnode_pager_setsize(ZTOV(zp), end);
1469 
1470 	zfs_rangelock_exit(lr);
1471 
1472 	dmu_tx_commit(tx);
1473 
1474 	return (0);
1475 }
1476 
1477 /*
1478  * Free space in a file.
1479  *
1480  *	IN:	zp	- znode of file to free data in.
1481  *		off	- start of section to free.
1482  *		len	- length of section to free.
1483  *
1484  *	RETURN:	0 on success, error code on failure
1485  */
1486 static int
zfs_free_range(znode_t * zp,uint64_t off,uint64_t len)1487 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1488 {
1489 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1490 	zfs_locked_range_t *lr;
1491 	int error;
1492 
1493 	/*
1494 	 * Lock the range being freed.
1495 	 */
1496 	lr = zfs_rangelock_enter(&zp->z_rangelock, off, len, RL_WRITER);
1497 
1498 	/*
1499 	 * Nothing to do if file already at desired length.
1500 	 */
1501 	if (off >= zp->z_size) {
1502 		zfs_rangelock_exit(lr);
1503 		return (0);
1504 	}
1505 
1506 	if (off + len > zp->z_size)
1507 		len = zp->z_size - off;
1508 
1509 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1510 
1511 	if (error == 0) {
1512 #if __FreeBSD_version >= 1400032
1513 		vnode_pager_purge_range(ZTOV(zp), off, off + len);
1514 #else
1515 		/*
1516 		 * Before __FreeBSD_version 1400032 we cannot free block in the
1517 		 * middle of a file, but only at the end of a file, so this code
1518 		 * path should never happen.
1519 		 */
1520 		vnode_pager_setsize(ZTOV(zp), off);
1521 #endif
1522 	}
1523 
1524 	zfs_rangelock_exit(lr);
1525 
1526 	return (error);
1527 }
1528 
1529 /*
1530  * Truncate a file
1531  *
1532  *	IN:	zp	- znode of file to free data in.
1533  *		end	- new end-of-file.
1534  *
1535  *	RETURN:	0 on success, error code on failure
1536  */
1537 static int
zfs_trunc(znode_t * zp,uint64_t end)1538 zfs_trunc(znode_t *zp, uint64_t end)
1539 {
1540 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1541 	vnode_t *vp = ZTOV(zp);
1542 	dmu_tx_t *tx;
1543 	zfs_locked_range_t *lr;
1544 	int error;
1545 	sa_bulk_attr_t bulk[2];
1546 	int count = 0;
1547 
1548 	/*
1549 	 * We will change zp_size, lock the whole file.
1550 	 */
1551 	lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
1552 
1553 	/*
1554 	 * Nothing to do if file already at desired length.
1555 	 */
1556 	if (end >= zp->z_size) {
1557 		zfs_rangelock_exit(lr);
1558 		return (0);
1559 	}
1560 
1561 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,
1562 	    DMU_OBJECT_END);
1563 	if (error) {
1564 		zfs_rangelock_exit(lr);
1565 		return (error);
1566 	}
1567 	tx = dmu_tx_create(zfsvfs->z_os);
1568 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1569 	zfs_sa_upgrade_txholds(tx, zp);
1570 	dmu_tx_mark_netfree(tx);
1571 	error = dmu_tx_assign(tx, DMU_TX_WAIT);
1572 	if (error) {
1573 		dmu_tx_abort(tx);
1574 		zfs_rangelock_exit(lr);
1575 		return (error);
1576 	}
1577 
1578 	zp->z_size = end;
1579 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1580 	    NULL, &zp->z_size, sizeof (zp->z_size));
1581 
1582 	if (end == 0) {
1583 		zp->z_pflags &= ~ZFS_SPARSE;
1584 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1585 		    NULL, &zp->z_pflags, 8);
1586 	}
1587 	VERIFY0(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx));
1588 
1589 	dmu_tx_commit(tx);
1590 
1591 	/*
1592 	 * Clear any mapped pages in the truncated region.  This has to
1593 	 * happen outside of the transaction to avoid the possibility of
1594 	 * a deadlock with someone trying to push a page that we are
1595 	 * about to invalidate.
1596 	 */
1597 	vnode_pager_setsize(vp, end);
1598 
1599 	zfs_rangelock_exit(lr);
1600 
1601 	return (0);
1602 }
1603 
1604 /*
1605  * Free space in a file
1606  *
1607  *	IN:	zp	- znode of file to free data in.
1608  *		off	- start of range
1609  *		len	- end of range (0 => EOF)
1610  *		flag	- current file open mode flags.
1611  *		log	- TRUE if this action should be logged
1612  *
1613  *	RETURN:	0 on success, error code on failure
1614  */
1615 int
zfs_freesp(znode_t * zp,uint64_t off,uint64_t len,int flag,boolean_t log)1616 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1617 {
1618 	dmu_tx_t *tx;
1619 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1620 	zilog_t *zilog = zfsvfs->z_log;
1621 	uint64_t mode;
1622 	uint64_t mtime[2], ctime[2];
1623 	sa_bulk_attr_t bulk[3];
1624 	int count = 0;
1625 	int error;
1626 
1627 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1628 	    sizeof (mode))) != 0)
1629 		return (error);
1630 
1631 	if (off > zp->z_size) {
1632 		error =  zfs_extend(zp, off+len);
1633 		if (error == 0 && log)
1634 			goto log;
1635 		else
1636 			return (error);
1637 	}
1638 
1639 	if (len == 0) {
1640 		error = zfs_trunc(zp, off);
1641 	} else {
1642 		if ((error = zfs_free_range(zp, off, len)) == 0 &&
1643 		    off + len > zp->z_size)
1644 			error = zfs_extend(zp, off+len);
1645 	}
1646 	if (error || !log)
1647 		return (error);
1648 log:
1649 	tx = dmu_tx_create(zfsvfs->z_os);
1650 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1651 	zfs_sa_upgrade_txholds(tx, zp);
1652 	error = dmu_tx_assign(tx, DMU_TX_WAIT);
1653 	if (error) {
1654 		dmu_tx_abort(tx);
1655 		return (error);
1656 	}
1657 
1658 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1659 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1660 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1661 	    NULL, &zp->z_pflags, 8);
1662 	zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
1663 	error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1664 	ASSERT0(error);
1665 
1666 	zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1667 
1668 	dmu_tx_commit(tx);
1669 	return (0);
1670 }
1671 
1672 void
zfs_create_fs(objset_t * os,cred_t * cr,nvlist_t * zplprops,dmu_tx_t * tx)1673 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1674 {
1675 	uint64_t	moid, obj, sa_obj, version;
1676 	uint64_t	sense = ZFS_CASE_SENSITIVE;
1677 	uint64_t	norm = 0;
1678 	nvpair_t	*elem;
1679 	int		error;
1680 	int		i;
1681 	znode_t		*rootzp = NULL;
1682 	zfsvfs_t	*zfsvfs;
1683 	vattr_t		vattr;
1684 	znode_t		*zp;
1685 	zfs_acl_ids_t	acl_ids;
1686 
1687 	/*
1688 	 * First attempt to create master node.
1689 	 */
1690 	/*
1691 	 * In an empty objset, there are no blocks to read and thus
1692 	 * there can be no i/o errors (which we assert below).
1693 	 */
1694 	moid = MASTER_NODE_OBJ;
1695 	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1696 	    DMU_OT_NONE, 0, tx);
1697 	ASSERT0(error);
1698 
1699 	/*
1700 	 * Set starting attributes.
1701 	 */
1702 	version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1703 	elem = NULL;
1704 	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1705 		/* For the moment we expect all zpl props to be uint64_ts */
1706 		uint64_t val;
1707 		const char *name;
1708 
1709 		ASSERT3S(nvpair_type(elem), ==, DATA_TYPE_UINT64);
1710 		val = fnvpair_value_uint64(elem);
1711 		name = nvpair_name(elem);
1712 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1713 			if (val < version)
1714 				version = val;
1715 		} else {
1716 			error = zap_update(os, moid, name, 8, 1, &val, tx);
1717 		}
1718 		ASSERT0(error);
1719 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1720 			norm = val;
1721 		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1722 			sense = val;
1723 	}
1724 	ASSERT3U(version, !=, 0);
1725 	error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1726 	ASSERT0(error);
1727 
1728 	/*
1729 	 * Create zap object used for SA attribute registration
1730 	 */
1731 
1732 	if (version >= ZPL_VERSION_SA) {
1733 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1734 		    DMU_OT_NONE, 0, tx);
1735 		error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1736 		ASSERT0(error);
1737 	} else {
1738 		sa_obj = 0;
1739 	}
1740 	/*
1741 	 * Create a delete queue.
1742 	 */
1743 	obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1744 
1745 	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1746 	ASSERT0(error);
1747 
1748 	/*
1749 	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1750 	 * to allow zfs_mknode to work.
1751 	 */
1752 	VATTR_NULL(&vattr);
1753 	vattr.va_mask = AT_MODE|AT_UID|AT_GID;
1754 	vattr.va_type = VDIR;
1755 	vattr.va_mode = S_IFDIR|0755;
1756 	vattr.va_uid = crgetuid(cr);
1757 	vattr.va_gid = crgetgid(cr);
1758 
1759 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1760 
1761 	rootzp = zfs_znode_alloc_kmem(KM_SLEEP);
1762 	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1763 	rootzp->z_unlinked = 0;
1764 	rootzp->z_atime_dirty = 0;
1765 	rootzp->z_is_sa = USE_SA(version, os);
1766 	rootzp->z_pflags = 0;
1767 
1768 	zfsvfs->z_os = os;
1769 	zfsvfs->z_parent = zfsvfs;
1770 	zfsvfs->z_version = version;
1771 	zfsvfs->z_use_fuids = USE_FUIDS(version, os);
1772 	zfsvfs->z_use_sa = USE_SA(version, os);
1773 	zfsvfs->z_norm = norm;
1774 
1775 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1776 	    &zfsvfs->z_attr_table);
1777 
1778 	ASSERT0(error);
1779 
1780 	/*
1781 	 * Fold case on file systems that are always or sometimes case
1782 	 * insensitive.
1783 	 */
1784 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1785 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1786 
1787 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1788 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1789 	    offsetof(znode_t, z_link_node));
1790 
1791 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1792 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1793 
1794 	rootzp->z_zfsvfs = zfsvfs;
1795 	VERIFY0(zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1796 	    cr, NULL, &acl_ids, NULL));
1797 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1798 	ASSERT3P(zp, ==, rootzp);
1799 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1800 	ASSERT0(error);
1801 	zfs_acl_ids_free(&acl_ids);
1802 	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1803 
1804 	sa_handle_destroy(rootzp->z_sa_hdl);
1805 	zfs_znode_free_kmem(rootzp);
1806 
1807 	/*
1808 	 * Create shares directory
1809 	 */
1810 
1811 	error = zfs_create_share_dir(zfsvfs, tx);
1812 
1813 	ASSERT0(error);
1814 
1815 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1816 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1817 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1818 }
1819 
1820 void
zfs_znode_update_vfs(znode_t * zp)1821 zfs_znode_update_vfs(znode_t *zp)
1822 {
1823 	vm_object_t object;
1824 
1825 	if ((object = ZTOV(zp)->v_object) == NULL ||
1826 	    zp->z_size == object->un_pager.vnp.vnp_size)
1827 		return;
1828 
1829 	vnode_pager_setsize(ZTOV(zp), zp->z_size);
1830 }
1831 
1832 int
zfs_znode_parent_and_name(znode_t * zp,znode_t ** dzpp,char * buf,uint64_t buflen)1833 zfs_znode_parent_and_name(znode_t *zp, znode_t **dzpp, char *buf,
1834     uint64_t buflen)
1835 {
1836 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1837 	uint64_t parent;
1838 	int is_xattrdir;
1839 	int err;
1840 
1841 	/* Extended attributes should not be visible as regular files. */
1842 	if ((zp->z_pflags & ZFS_XATTR) != 0)
1843 		return (SET_ERROR(EINVAL));
1844 
1845 	err = zfs_obj_to_pobj(zfsvfs->z_os, zp->z_sa_hdl, zfsvfs->z_attr_table,
1846 	    &parent, &is_xattrdir);
1847 	if (err != 0)
1848 		return (err);
1849 	ASSERT0(is_xattrdir);
1850 
1851 	/* No name as this is a root object. */
1852 	if (parent == zp->z_id)
1853 		return (SET_ERROR(EINVAL));
1854 
1855 	err = zap_value_search(zfsvfs->z_os, parent, zp->z_id,
1856 	    ZFS_DIRENT_OBJ(-1ULL), buf, buflen);
1857 	if (err != 0)
1858 		return (err);
1859 	err = zfs_zget(zfsvfs, parent, dzpp);
1860 	return (err);
1861 }
1862 
1863 int
zfs_rlimit_fsize(off_t fsize)1864 zfs_rlimit_fsize(off_t fsize)
1865 {
1866 	struct thread *td = curthread;
1867 	off_t lim;
1868 
1869 	if (td == NULL)
1870 		return (0);
1871 
1872 	lim = lim_cur(td, RLIMIT_FSIZE);
1873 	if (__predict_true((uoff_t)fsize <= lim))
1874 		return (0);
1875 
1876 	/*
1877 	 * The limit is reached.
1878 	 */
1879 	PROC_LOCK(td->td_proc);
1880 	kern_psignal(td->td_proc, SIGXFSZ);
1881 	PROC_UNLOCK(td->td_proc);
1882 
1883 	return (EFBIG);
1884 }
1885