xref: /freebsd-13-stable/sys/contrib/openzfs/module/os/linux/zfs/zfs_znode.c (revision b9c2c366db1beb2ed276947056f45938ad8f57ec)
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, 2018 by Delphix. All rights reserved.
24  */
25 
26 /* Portions Copyright 2007 Jeremy Teo */
27 
28 #ifdef _KERNEL
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/sysmacros.h>
33 #include <sys/mntent.h>
34 #include <sys/u8_textprep.h>
35 #include <sys/dsl_dataset.h>
36 #include <sys/vfs.h>
37 #include <sys/vnode.h>
38 #include <sys/file.h>
39 #include <sys/kmem.h>
40 #include <sys/errno.h>
41 #include <sys/atomic.h>
42 #include <sys/zfs_dir.h>
43 #include <sys/zfs_acl.h>
44 #include <sys/zfs_ioctl.h>
45 #include <sys/zfs_rlock.h>
46 #include <sys/zfs_fuid.h>
47 #include <sys/zfs_vnops.h>
48 #include <sys/zfs_ctldir.h>
49 #include <sys/dnode.h>
50 #include <sys/fs/zfs.h>
51 #include <sys/zpl.h>
52 #endif /* _KERNEL */
53 
54 #include <sys/dmu.h>
55 #include <sys/dmu_objset.h>
56 #include <sys/dmu_tx.h>
57 #include <sys/zfs_refcount.h>
58 #include <sys/stat.h>
59 #include <sys/zap.h>
60 #include <sys/zfs_znode.h>
61 #include <sys/sa.h>
62 #include <sys/zfs_sa.h>
63 #include <sys/zfs_stat.h>
64 
65 #include "zfs_prop.h"
66 #include "zfs_comutil.h"
67 
68 /*
69  * Functions needed for userland (ie: libzpool) are not put under
70  * #ifdef_KERNEL; the rest of the functions have dependencies
71  * (such as VFS logic) that will not compile easily in userland.
72  */
73 #ifdef _KERNEL
74 
75 static kmem_cache_t *znode_cache = NULL;
76 static kmem_cache_t *znode_hold_cache = NULL;
77 unsigned int zfs_object_mutex_size = ZFS_OBJ_MTX_SZ;
78 
79 /*
80  * This is used by the test suite so that it can delay znodes from being
81  * freed in order to inspect the unlinked set.
82  */
83 int zfs_unlink_suspend_progress = 0;
84 
85 /*
86  * This callback is invoked when acquiring a RL_WRITER or RL_APPEND lock on
87  * z_rangelock. It will modify the offset and length of the lock to reflect
88  * znode-specific information, and convert RL_APPEND to RL_WRITER.  This is
89  * called with the rangelock_t's rl_lock held, which avoids races.
90  */
91 static void
zfs_rangelock_cb(zfs_locked_range_t * new,void * arg)92 zfs_rangelock_cb(zfs_locked_range_t *new, void *arg)
93 {
94 	znode_t *zp = arg;
95 
96 	/*
97 	 * If in append mode, convert to writer and lock starting at the
98 	 * current end of file.
99 	 */
100 	if (new->lr_type == RL_APPEND) {
101 		new->lr_offset = zp->z_size;
102 		new->lr_type = RL_WRITER;
103 	}
104 
105 	/*
106 	 * If we need to grow the block size then lock the whole file range.
107 	 */
108 	uint64_t end_size = MAX(zp->z_size, new->lr_offset + new->lr_length);
109 	if (end_size > zp->z_blksz && (!ISP2(zp->z_blksz) ||
110 	    zp->z_blksz < ZTOZSB(zp)->z_max_blksz)) {
111 		new->lr_offset = 0;
112 		new->lr_length = UINT64_MAX;
113 	}
114 }
115 
116 /*ARGSUSED*/
117 static int
zfs_znode_cache_constructor(void * buf,void * arg,int kmflags)118 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
119 {
120 	znode_t *zp = buf;
121 
122 	inode_init_once(ZTOI(zp));
123 	list_link_init(&zp->z_link_node);
124 
125 	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
126 	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
127 	rw_init(&zp->z_name_lock, NULL, RW_NOLOCKDEP, NULL);
128 	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
129 	rw_init(&zp->z_xattr_lock, NULL, RW_DEFAULT, NULL);
130 
131 	zfs_rangelock_init(&zp->z_rangelock, zfs_rangelock_cb, zp);
132 
133 	zp->z_dirlocks = NULL;
134 	zp->z_acl_cached = NULL;
135 	zp->z_xattr_cached = NULL;
136 	zp->z_xattr_parent = 0;
137 	zp->z_sync_writes_cnt = 0;
138 	zp->z_async_writes_cnt = 0;
139 
140 	return (0);
141 }
142 
143 /*ARGSUSED*/
144 static void
zfs_znode_cache_destructor(void * buf,void * arg)145 zfs_znode_cache_destructor(void *buf, void *arg)
146 {
147 	znode_t *zp = buf;
148 
149 	ASSERT(!list_link_active(&zp->z_link_node));
150 	mutex_destroy(&zp->z_lock);
151 	rw_destroy(&zp->z_parent_lock);
152 	rw_destroy(&zp->z_name_lock);
153 	mutex_destroy(&zp->z_acl_lock);
154 	rw_destroy(&zp->z_xattr_lock);
155 	zfs_rangelock_fini(&zp->z_rangelock);
156 
157 	ASSERT3P(zp->z_dirlocks, ==, NULL);
158 	ASSERT3P(zp->z_acl_cached, ==, NULL);
159 	ASSERT3P(zp->z_xattr_cached, ==, NULL);
160 
161 	ASSERT0(atomic_load_32(&zp->z_sync_writes_cnt));
162 	ASSERT0(atomic_load_32(&zp->z_async_writes_cnt));
163 }
164 
165 static int
zfs_znode_hold_cache_constructor(void * buf,void * arg,int kmflags)166 zfs_znode_hold_cache_constructor(void *buf, void *arg, int kmflags)
167 {
168 	znode_hold_t *zh = buf;
169 
170 	mutex_init(&zh->zh_lock, NULL, MUTEX_DEFAULT, NULL);
171 	zh->zh_refcount = 0;
172 
173 	return (0);
174 }
175 
176 static void
zfs_znode_hold_cache_destructor(void * buf,void * arg)177 zfs_znode_hold_cache_destructor(void *buf, void *arg)
178 {
179 	znode_hold_t *zh = buf;
180 
181 	mutex_destroy(&zh->zh_lock);
182 }
183 
184 void
zfs_znode_init(void)185 zfs_znode_init(void)
186 {
187 	/*
188 	 * Initialize zcache.  The KMC_SLAB hint is used in order that it be
189 	 * backed by kmalloc() when on the Linux slab in order that any
190 	 * wait_on_bit() operations on the related inode operate properly.
191 	 */
192 	ASSERT(znode_cache == NULL);
193 	znode_cache = kmem_cache_create("zfs_znode_cache",
194 	    sizeof (znode_t), 0, zfs_znode_cache_constructor,
195 	    zfs_znode_cache_destructor, NULL, NULL, NULL, KMC_SLAB);
196 
197 	ASSERT(znode_hold_cache == NULL);
198 	znode_hold_cache = kmem_cache_create("zfs_znode_hold_cache",
199 	    sizeof (znode_hold_t), 0, zfs_znode_hold_cache_constructor,
200 	    zfs_znode_hold_cache_destructor, NULL, NULL, NULL, 0);
201 }
202 
203 void
zfs_znode_fini(void)204 zfs_znode_fini(void)
205 {
206 	/*
207 	 * Cleanup zcache
208 	 */
209 	if (znode_cache)
210 		kmem_cache_destroy(znode_cache);
211 	znode_cache = NULL;
212 
213 	if (znode_hold_cache)
214 		kmem_cache_destroy(znode_hold_cache);
215 	znode_hold_cache = NULL;
216 }
217 
218 /*
219  * The zfs_znode_hold_enter() / zfs_znode_hold_exit() functions are used to
220  * serialize access to a znode and its SA buffer while the object is being
221  * created or destroyed.  This kind of locking would normally reside in the
222  * znode itself but in this case that's impossible because the znode and SA
223  * buffer may not yet exist.  Therefore the locking is handled externally
224  * with an array of mutexes and AVLs trees which contain per-object locks.
225  *
226  * In zfs_znode_hold_enter() a per-object lock is created as needed, inserted
227  * in to the correct AVL tree and finally the per-object lock is held.  In
228  * zfs_znode_hold_exit() the process is reversed.  The per-object lock is
229  * released, removed from the AVL tree and destroyed if there are no waiters.
230  *
231  * This scheme has two important properties:
232  *
233  * 1) No memory allocations are performed while holding one of the z_hold_locks.
234  *    This ensures evict(), which can be called from direct memory reclaim, will
235  *    never block waiting on a z_hold_locks which just happens to have hashed
236  *    to the same index.
237  *
238  * 2) All locks used to serialize access to an object are per-object and never
239  *    shared.  This minimizes lock contention without creating a large number
240  *    of dedicated locks.
241  *
242  * On the downside it does require znode_lock_t structures to be frequently
243  * allocated and freed.  However, because these are backed by a kmem cache
244  * and very short lived this cost is minimal.
245  */
246 int
zfs_znode_hold_compare(const void * a,const void * b)247 zfs_znode_hold_compare(const void *a, const void *b)
248 {
249 	const znode_hold_t *zh_a = (const znode_hold_t *)a;
250 	const znode_hold_t *zh_b = (const znode_hold_t *)b;
251 
252 	return (TREE_CMP(zh_a->zh_obj, zh_b->zh_obj));
253 }
254 
255 static boolean_t __maybe_unused
zfs_znode_held(zfsvfs_t * zfsvfs,uint64_t obj)256 zfs_znode_held(zfsvfs_t *zfsvfs, uint64_t obj)
257 {
258 	znode_hold_t *zh, search;
259 	int i = ZFS_OBJ_HASH(zfsvfs, obj);
260 	boolean_t held;
261 
262 	search.zh_obj = obj;
263 
264 	mutex_enter(&zfsvfs->z_hold_locks[i]);
265 	zh = avl_find(&zfsvfs->z_hold_trees[i], &search, NULL);
266 	held = (zh && MUTEX_HELD(&zh->zh_lock)) ? B_TRUE : B_FALSE;
267 	mutex_exit(&zfsvfs->z_hold_locks[i]);
268 
269 	return (held);
270 }
271 
272 static znode_hold_t *
zfs_znode_hold_enter(zfsvfs_t * zfsvfs,uint64_t obj)273 zfs_znode_hold_enter(zfsvfs_t *zfsvfs, uint64_t obj)
274 {
275 	znode_hold_t *zh, *zh_new, search;
276 	int i = ZFS_OBJ_HASH(zfsvfs, obj);
277 	boolean_t found = B_FALSE;
278 
279 	zh_new = kmem_cache_alloc(znode_hold_cache, KM_SLEEP);
280 	search.zh_obj = obj;
281 
282 	mutex_enter(&zfsvfs->z_hold_locks[i]);
283 	zh = avl_find(&zfsvfs->z_hold_trees[i], &search, NULL);
284 	if (likely(zh == NULL)) {
285 		zh = zh_new;
286 		zh->zh_obj = obj;
287 		avl_add(&zfsvfs->z_hold_trees[i], zh);
288 	} else {
289 		ASSERT3U(zh->zh_obj, ==, obj);
290 		found = B_TRUE;
291 	}
292 	zh->zh_refcount++;
293 	ASSERT3S(zh->zh_refcount, >, 0);
294 	mutex_exit(&zfsvfs->z_hold_locks[i]);
295 
296 	if (found == B_TRUE)
297 		kmem_cache_free(znode_hold_cache, zh_new);
298 
299 	ASSERT(MUTEX_NOT_HELD(&zh->zh_lock));
300 	mutex_enter(&zh->zh_lock);
301 
302 	return (zh);
303 }
304 
305 static void
zfs_znode_hold_exit(zfsvfs_t * zfsvfs,znode_hold_t * zh)306 zfs_znode_hold_exit(zfsvfs_t *zfsvfs, znode_hold_t *zh)
307 {
308 	int i = ZFS_OBJ_HASH(zfsvfs, zh->zh_obj);
309 	boolean_t remove = B_FALSE;
310 
311 	ASSERT(zfs_znode_held(zfsvfs, zh->zh_obj));
312 	mutex_exit(&zh->zh_lock);
313 
314 	mutex_enter(&zfsvfs->z_hold_locks[i]);
315 	ASSERT3S(zh->zh_refcount, >, 0);
316 	if (--zh->zh_refcount == 0) {
317 		avl_remove(&zfsvfs->z_hold_trees[i], zh);
318 		remove = B_TRUE;
319 	}
320 	mutex_exit(&zfsvfs->z_hold_locks[i]);
321 
322 	if (remove == B_TRUE)
323 		kmem_cache_free(znode_hold_cache, zh);
324 }
325 
326 dev_t
zfs_cmpldev(uint64_t dev)327 zfs_cmpldev(uint64_t dev)
328 {
329 	return (dev);
330 }
331 
332 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)333 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
334     dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
335 {
336 	ASSERT(zfs_znode_held(zfsvfs, zp->z_id));
337 
338 	mutex_enter(&zp->z_lock);
339 
340 	ASSERT(zp->z_sa_hdl == NULL);
341 	ASSERT(zp->z_acl_cached == NULL);
342 	if (sa_hdl == NULL) {
343 		VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
344 		    SA_HDL_SHARED, &zp->z_sa_hdl));
345 	} else {
346 		zp->z_sa_hdl = sa_hdl;
347 		sa_set_userp(sa_hdl, zp);
348 	}
349 
350 	zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
351 
352 	mutex_exit(&zp->z_lock);
353 }
354 
355 void
zfs_znode_dmu_fini(znode_t * zp)356 zfs_znode_dmu_fini(znode_t *zp)
357 {
358 	ASSERT(zfs_znode_held(ZTOZSB(zp), zp->z_id) || zp->z_unlinked ||
359 	    RW_WRITE_HELD(&ZTOZSB(zp)->z_teardown_inactive_lock));
360 
361 	sa_handle_destroy(zp->z_sa_hdl);
362 	zp->z_sa_hdl = NULL;
363 }
364 
365 /*
366  * Called by new_inode() to allocate a new inode.
367  */
368 int
zfs_inode_alloc(struct super_block * sb,struct inode ** ip)369 zfs_inode_alloc(struct super_block *sb, struct inode **ip)
370 {
371 	znode_t *zp;
372 
373 	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
374 	*ip = ZTOI(zp);
375 
376 	return (0);
377 }
378 
379 /*
380  * Called in multiple places when an inode should be destroyed.
381  */
382 void
zfs_inode_destroy(struct inode * ip)383 zfs_inode_destroy(struct inode *ip)
384 {
385 	znode_t *zp = ITOZ(ip);
386 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
387 
388 	mutex_enter(&zfsvfs->z_znodes_lock);
389 	if (list_link_active(&zp->z_link_node)) {
390 		list_remove(&zfsvfs->z_all_znodes, zp);
391 		zfsvfs->z_nr_znodes--;
392 	}
393 	mutex_exit(&zfsvfs->z_znodes_lock);
394 
395 	if (zp->z_acl_cached) {
396 		zfs_acl_free(zp->z_acl_cached);
397 		zp->z_acl_cached = NULL;
398 	}
399 
400 	if (zp->z_xattr_cached) {
401 		nvlist_free(zp->z_xattr_cached);
402 		zp->z_xattr_cached = NULL;
403 	}
404 
405 	kmem_cache_free(znode_cache, zp);
406 }
407 
408 static void
zfs_inode_set_ops(zfsvfs_t * zfsvfs,struct inode * ip)409 zfs_inode_set_ops(zfsvfs_t *zfsvfs, struct inode *ip)
410 {
411 	uint64_t rdev = 0;
412 
413 	switch (ip->i_mode & S_IFMT) {
414 	case S_IFREG:
415 		ip->i_op = &zpl_inode_operations;
416 		ip->i_fop = &zpl_file_operations;
417 		ip->i_mapping->a_ops = &zpl_address_space_operations;
418 		break;
419 
420 	case S_IFDIR:
421 		ip->i_op = &zpl_dir_inode_operations;
422 		ip->i_fop = &zpl_dir_file_operations;
423 		ITOZ(ip)->z_zn_prefetch = B_TRUE;
424 		break;
425 
426 	case S_IFLNK:
427 		ip->i_op = &zpl_symlink_inode_operations;
428 		break;
429 
430 	/*
431 	 * rdev is only stored in a SA only for device files.
432 	 */
433 	case S_IFCHR:
434 	case S_IFBLK:
435 		(void) sa_lookup(ITOZ(ip)->z_sa_hdl, SA_ZPL_RDEV(zfsvfs), &rdev,
436 		    sizeof (rdev));
437 		fallthrough;
438 	case S_IFIFO:
439 	case S_IFSOCK:
440 		init_special_inode(ip, ip->i_mode, rdev);
441 		ip->i_op = &zpl_special_inode_operations;
442 		break;
443 
444 	default:
445 		zfs_panic_recover("inode %llu has invalid mode: 0x%x\n",
446 		    (u_longlong_t)ip->i_ino, ip->i_mode);
447 
448 		/* Assume the inode is a file and attempt to continue */
449 		ip->i_mode = S_IFREG | 0644;
450 		ip->i_op = &zpl_inode_operations;
451 		ip->i_fop = &zpl_file_operations;
452 		ip->i_mapping->a_ops = &zpl_address_space_operations;
453 		break;
454 	}
455 }
456 
457 static void
zfs_set_inode_flags(znode_t * zp,struct inode * ip)458 zfs_set_inode_flags(znode_t *zp, struct inode *ip)
459 {
460 	/*
461 	 * Linux and Solaris have different sets of file attributes, so we
462 	 * restrict this conversion to the intersection of the two.
463 	 */
464 #ifdef HAVE_INODE_SET_FLAGS
465 	unsigned int flags = 0;
466 	if (zp->z_pflags & ZFS_IMMUTABLE)
467 		flags |= S_IMMUTABLE;
468 	if (zp->z_pflags & ZFS_APPENDONLY)
469 		flags |= S_APPEND;
470 
471 	inode_set_flags(ip, flags, S_IMMUTABLE|S_APPEND);
472 #else
473 	if (zp->z_pflags & ZFS_IMMUTABLE)
474 		ip->i_flags |= S_IMMUTABLE;
475 	else
476 		ip->i_flags &= ~S_IMMUTABLE;
477 
478 	if (zp->z_pflags & ZFS_APPENDONLY)
479 		ip->i_flags |= S_APPEND;
480 	else
481 		ip->i_flags &= ~S_APPEND;
482 #endif
483 }
484 
485 /*
486  * Update the embedded inode given the znode.
487  */
488 void
zfs_znode_update_vfs(znode_t * zp)489 zfs_znode_update_vfs(znode_t *zp)
490 {
491 	zfsvfs_t	*zfsvfs;
492 	struct inode	*ip;
493 	uint32_t	blksize;
494 	u_longlong_t	i_blocks;
495 
496 	ASSERT(zp != NULL);
497 	zfsvfs = ZTOZSB(zp);
498 	ip = ZTOI(zp);
499 
500 	/* Skip .zfs control nodes which do not exist on disk. */
501 	if (zfsctl_is_node(ip))
502 		return;
503 
504 	dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &blksize, &i_blocks);
505 
506 	spin_lock(&ip->i_lock);
507 	ip->i_mode = zp->z_mode;
508 	ip->i_blocks = i_blocks;
509 	i_size_write(ip, zp->z_size);
510 	spin_unlock(&ip->i_lock);
511 }
512 
513 
514 /*
515  * Construct a znode+inode and initialize.
516  *
517  * This does not do a call to dmu_set_user() that is
518  * up to the caller to do, in case you don't want to
519  * return the znode
520  */
521 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)522 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
523     dmu_object_type_t obj_type, sa_handle_t *hdl)
524 {
525 	znode_t	*zp;
526 	struct inode *ip;
527 	uint64_t mode;
528 	uint64_t parent;
529 	uint64_t tmp_gen;
530 	uint64_t links;
531 	uint64_t z_uid, z_gid;
532 	uint64_t atime[2], mtime[2], ctime[2], btime[2];
533 	inode_timespec_t tmp_ts;
534 	uint64_t projid = ZFS_DEFAULT_PROJID;
535 	sa_bulk_attr_t bulk[12];
536 	int count = 0;
537 
538 	ASSERT(zfsvfs != NULL);
539 
540 	ip = new_inode(zfsvfs->z_sb);
541 	if (ip == NULL)
542 		return (NULL);
543 
544 	zp = ITOZ(ip);
545 	ASSERT(zp->z_dirlocks == NULL);
546 	ASSERT3P(zp->z_acl_cached, ==, NULL);
547 	ASSERT3P(zp->z_xattr_cached, ==, NULL);
548 	zp->z_unlinked = B_FALSE;
549 	zp->z_atime_dirty = B_FALSE;
550 #if !defined(HAVE_FILEMAP_RANGE_HAS_PAGE)
551 	zp->z_is_mapped = B_FALSE;
552 #endif
553 	zp->z_is_ctldir = B_FALSE;
554 	zp->z_suspended = B_FALSE;
555 	zp->z_sa_hdl = NULL;
556 	zp->z_mapcnt = 0;
557 	zp->z_id = db->db_object;
558 	zp->z_blksz = blksz;
559 	zp->z_seq = 0x7A4653;
560 	zp->z_sync_cnt = 0;
561 	zp->z_sync_writes_cnt = 0;
562 	zp->z_async_writes_cnt = 0;
563 
564 	zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
565 
566 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
567 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &tmp_gen, 8);
568 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
569 	    &zp->z_size, 8);
570 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
571 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
572 	    &zp->z_pflags, 8);
573 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
574 	    &parent, 8);
575 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, &z_uid, 8);
576 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, &z_gid, 8);
577 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
578 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
579 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
580 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &btime, 16);
581 
582 	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || tmp_gen == 0 ||
583 	    (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
584 	    (zp->z_pflags & ZFS_PROJID) &&
585 	    sa_lookup(zp->z_sa_hdl, SA_ZPL_PROJID(zfsvfs), &projid, 8) != 0)) {
586 		if (hdl == NULL)
587 			sa_handle_destroy(zp->z_sa_hdl);
588 		zp->z_sa_hdl = NULL;
589 		goto error;
590 	}
591 
592 	zp->z_projid = projid;
593 	zp->z_mode = ip->i_mode = mode;
594 	ip->i_generation = (uint32_t)tmp_gen;
595 	ip->i_blkbits = SPA_MINBLOCKSHIFT;
596 	set_nlink(ip, (uint32_t)links);
597 	zfs_uid_write(ip, z_uid);
598 	zfs_gid_write(ip, z_gid);
599 	zfs_set_inode_flags(zp, ip);
600 
601 	/* Cache the xattr parent id */
602 	if (zp->z_pflags & ZFS_XATTR)
603 		zp->z_xattr_parent = parent;
604 
605 	ZFS_TIME_DECODE(&tmp_ts, atime);
606 	zpl_inode_set_atime_to_ts(ip, tmp_ts);
607 	ZFS_TIME_DECODE(&tmp_ts, mtime);
608 	zpl_inode_set_mtime_to_ts(ip, tmp_ts);
609 	ZFS_TIME_DECODE(&tmp_ts, ctime);
610 	zpl_inode_set_ctime_to_ts(ip, tmp_ts);
611 	ZFS_TIME_DECODE(&zp->z_btime, btime);
612 
613 	ip->i_ino = zp->z_id;
614 	zfs_znode_update_vfs(zp);
615 	zfs_inode_set_ops(zfsvfs, ip);
616 
617 	/*
618 	 * The only way insert_inode_locked() can fail is if the ip->i_ino
619 	 * number is already hashed for this super block.  This can never
620 	 * happen because the inode numbers map 1:1 with the object numbers.
621 	 *
622 	 * Exceptions include rolling back a mounted file system, either
623 	 * from the zfs rollback or zfs recv command.
624 	 *
625 	 * Active inodes are unhashed during the rollback, but since zrele
626 	 * can happen asynchronously, we can't guarantee they've been
627 	 * unhashed.  This can cause hash collisions in unlinked drain
628 	 * processing so do not hash unlinked znodes.
629 	 */
630 	if (links > 0)
631 		VERIFY3S(insert_inode_locked(ip), ==, 0);
632 
633 	mutex_enter(&zfsvfs->z_znodes_lock);
634 	list_insert_tail(&zfsvfs->z_all_znodes, zp);
635 	zfsvfs->z_nr_znodes++;
636 	mutex_exit(&zfsvfs->z_znodes_lock);
637 
638 	if (links > 0)
639 		unlock_new_inode(ip);
640 	return (zp);
641 
642 error:
643 	iput(ip);
644 	return (NULL);
645 }
646 
647 /*
648  * Safely mark an inode dirty.  Inodes which are part of a read-only
649  * file system or snapshot may not be dirtied.
650  */
651 void
zfs_mark_inode_dirty(struct inode * ip)652 zfs_mark_inode_dirty(struct inode *ip)
653 {
654 	zfsvfs_t *zfsvfs = ITOZSB(ip);
655 
656 	if (zfs_is_readonly(zfsvfs) || dmu_objset_is_snapshot(zfsvfs->z_os))
657 		return;
658 
659 	mark_inode_dirty(ip);
660 }
661 
662 static uint64_t empty_xattr;
663 static uint64_t pad[4];
664 static zfs_acl_phys_t acl_phys;
665 /*
666  * Create a new DMU object to hold a zfs znode.
667  *
668  *	IN:	dzp	- parent directory for new znode
669  *		vap	- file attributes for new znode
670  *		tx	- dmu transaction id for zap operations
671  *		cr	- credentials of caller
672  *		flag	- flags:
673  *			  IS_ROOT_NODE	- new object will be root
674  *			  IS_TMPFILE	- new object is of O_TMPFILE
675  *			  IS_XATTR	- new object is an attribute
676  *		acl_ids	- ACL related attributes
677  *
678  *	OUT:	zpp	- allocated znode (set to dzp if IS_ROOT_NODE)
679  *
680  */
681 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)682 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
683     uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
684 {
685 	uint64_t	crtime[2], atime[2], mtime[2], ctime[2];
686 	uint64_t	mode, size, links, parent, pflags;
687 	uint64_t	projid = ZFS_DEFAULT_PROJID;
688 	uint64_t	rdev = 0;
689 	zfsvfs_t	*zfsvfs = ZTOZSB(dzp);
690 	dmu_buf_t	*db;
691 	inode_timespec_t now;
692 	uint64_t	gen, obj;
693 	int		bonuslen;
694 	int		dnodesize;
695 	sa_handle_t	*sa_hdl;
696 	dmu_object_type_t obj_type;
697 	sa_bulk_attr_t	*sa_attrs;
698 	int		cnt = 0;
699 	zfs_acl_locator_cb_t locate = { 0 };
700 	znode_hold_t	*zh;
701 
702 	if (zfsvfs->z_replay) {
703 		obj = vap->va_nodeid;
704 		now = vap->va_ctime;		/* see zfs_replay_create() */
705 		gen = vap->va_nblocks;		/* ditto */
706 		dnodesize = vap->va_fsid;	/* ditto */
707 	} else {
708 		obj = 0;
709 		gethrestime(&now);
710 		gen = dmu_tx_get_txg(tx);
711 		dnodesize = dmu_objset_dnodesize(zfsvfs->z_os);
712 	}
713 
714 	if (dnodesize == 0)
715 		dnodesize = DNODE_MIN_SIZE;
716 
717 	obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
718 
719 	bonuslen = (obj_type == DMU_OT_SA) ?
720 	    DN_BONUS_SIZE(dnodesize) : ZFS_OLD_ZNODE_PHYS_SIZE;
721 
722 	/*
723 	 * Create a new DMU object.
724 	 */
725 	/*
726 	 * There's currently no mechanism for pre-reading the blocks that will
727 	 * be needed to allocate a new object, so we accept the small chance
728 	 * that there will be an i/o error and we will fail one of the
729 	 * assertions below.
730 	 */
731 	if (S_ISDIR(vap->va_mode)) {
732 		if (zfsvfs->z_replay) {
733 			VERIFY0(zap_create_claim_norm_dnsize(zfsvfs->z_os, obj,
734 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
735 			    obj_type, bonuslen, dnodesize, tx));
736 		} else {
737 			obj = zap_create_norm_dnsize(zfsvfs->z_os,
738 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
739 			    obj_type, bonuslen, dnodesize, tx);
740 		}
741 	} else {
742 		if (zfsvfs->z_replay) {
743 			VERIFY0(dmu_object_claim_dnsize(zfsvfs->z_os, obj,
744 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
745 			    obj_type, bonuslen, dnodesize, tx));
746 		} else {
747 			obj = dmu_object_alloc_dnsize(zfsvfs->z_os,
748 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
749 			    obj_type, bonuslen, dnodesize, tx);
750 		}
751 	}
752 
753 	zh = zfs_znode_hold_enter(zfsvfs, obj);
754 	VERIFY0(sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
755 
756 	/*
757 	 * If this is the root, fix up the half-initialized parent pointer
758 	 * to reference the just-allocated physical data area.
759 	 */
760 	if (flag & IS_ROOT_NODE) {
761 		dzp->z_id = obj;
762 	}
763 
764 	/*
765 	 * If parent is an xattr, so am I.
766 	 */
767 	if (dzp->z_pflags & ZFS_XATTR) {
768 		flag |= IS_XATTR;
769 	}
770 
771 	if (zfsvfs->z_use_fuids)
772 		pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
773 	else
774 		pflags = 0;
775 
776 	if (S_ISDIR(vap->va_mode)) {
777 		size = 2;		/* contents ("." and "..") */
778 		links = 2;
779 	} else {
780 		size = 0;
781 		links = (flag & IS_TMPFILE) ? 0 : 1;
782 	}
783 
784 	if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))
785 		rdev = vap->va_rdev;
786 
787 	parent = dzp->z_id;
788 	mode = acl_ids->z_mode;
789 	if (flag & IS_XATTR)
790 		pflags |= ZFS_XATTR;
791 
792 	if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode)) {
793 		/*
794 		 * With ZFS_PROJID flag, we can easily know whether there is
795 		 * project ID stored on disk or not. See zfs_space_delta_cb().
796 		 */
797 		if (obj_type != DMU_OT_ZNODE &&
798 		    dmu_objset_projectquota_enabled(zfsvfs->z_os))
799 			pflags |= ZFS_PROJID;
800 
801 		/*
802 		 * Inherit project ID from parent if required.
803 		 */
804 		projid = zfs_inherit_projid(dzp);
805 		if (dzp->z_pflags & ZFS_PROJINHERIT)
806 			pflags |= ZFS_PROJINHERIT;
807 	}
808 
809 	/*
810 	 * No execs denied will be determined when zfs_mode_compute() is called.
811 	 */
812 	pflags |= acl_ids->z_aclp->z_hints &
813 	    (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
814 	    ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
815 
816 	ZFS_TIME_ENCODE(&now, crtime);
817 	ZFS_TIME_ENCODE(&now, ctime);
818 
819 	if (vap->va_mask & ATTR_ATIME) {
820 		ZFS_TIME_ENCODE(&vap->va_atime, atime);
821 	} else {
822 		ZFS_TIME_ENCODE(&now, atime);
823 	}
824 
825 	if (vap->va_mask & ATTR_MTIME) {
826 		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
827 	} else {
828 		ZFS_TIME_ENCODE(&now, mtime);
829 	}
830 
831 	/* Now add in all of the "SA" attributes */
832 	VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
833 	    &sa_hdl));
834 
835 	/*
836 	 * Setup the array of attributes to be replaced/set on the new file
837 	 *
838 	 * order for  DMU_OT_ZNODE is critical since it needs to be constructed
839 	 * in the old znode_phys_t format.  Don't change this ordering
840 	 */
841 	sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
842 
843 	if (obj_type == DMU_OT_ZNODE) {
844 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
845 		    NULL, &atime, 16);
846 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
847 		    NULL, &mtime, 16);
848 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
849 		    NULL, &ctime, 16);
850 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
851 		    NULL, &crtime, 16);
852 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
853 		    NULL, &gen, 8);
854 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
855 		    NULL, &mode, 8);
856 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
857 		    NULL, &size, 8);
858 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
859 		    NULL, &parent, 8);
860 	} else {
861 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
862 		    NULL, &mode, 8);
863 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
864 		    NULL, &size, 8);
865 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
866 		    NULL, &gen, 8);
867 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs),
868 		    NULL, &acl_ids->z_fuid, 8);
869 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs),
870 		    NULL, &acl_ids->z_fgid, 8);
871 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
872 		    NULL, &parent, 8);
873 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
874 		    NULL, &pflags, 8);
875 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
876 		    NULL, &atime, 16);
877 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
878 		    NULL, &mtime, 16);
879 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
880 		    NULL, &ctime, 16);
881 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
882 		    NULL, &crtime, 16);
883 	}
884 
885 	SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
886 
887 	if (obj_type == DMU_OT_ZNODE) {
888 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
889 		    &empty_xattr, 8);
890 	} else if (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
891 	    pflags & ZFS_PROJID) {
892 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PROJID(zfsvfs),
893 		    NULL, &projid, 8);
894 	}
895 	if (obj_type == DMU_OT_ZNODE ||
896 	    (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))) {
897 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
898 		    NULL, &rdev, 8);
899 	}
900 	if (obj_type == DMU_OT_ZNODE) {
901 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
902 		    NULL, &pflags, 8);
903 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
904 		    &acl_ids->z_fuid, 8);
905 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
906 		    &acl_ids->z_fgid, 8);
907 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
908 		    sizeof (uint64_t) * 4);
909 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
910 		    &acl_phys, sizeof (zfs_acl_phys_t));
911 	} else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
912 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
913 		    &acl_ids->z_aclp->z_acl_count, 8);
914 		locate.cb_aclp = acl_ids->z_aclp;
915 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
916 		    zfs_acl_data_locator, &locate,
917 		    acl_ids->z_aclp->z_acl_bytes);
918 		mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
919 		    acl_ids->z_fuid, acl_ids->z_fgid);
920 	}
921 
922 	VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
923 
924 	if (!(flag & IS_ROOT_NODE)) {
925 		/*
926 		 * The call to zfs_znode_alloc() may fail if memory is low
927 		 * via the call path: alloc_inode() -> inode_init_always() ->
928 		 * security_inode_alloc() -> inode_alloc_security().  Since
929 		 * the existing code is written such that zfs_mknode() can
930 		 * not fail retry until sufficient memory has been reclaimed.
931 		 */
932 		do {
933 			*zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
934 		} while (*zpp == NULL);
935 
936 		VERIFY(*zpp != NULL);
937 		VERIFY(dzp != NULL);
938 	} else {
939 		/*
940 		 * If we are creating the root node, the "parent" we
941 		 * passed in is the znode for the root.
942 		 */
943 		*zpp = dzp;
944 
945 		(*zpp)->z_sa_hdl = sa_hdl;
946 	}
947 
948 	(*zpp)->z_pflags = pflags;
949 	(*zpp)->z_mode = ZTOI(*zpp)->i_mode = mode;
950 	(*zpp)->z_dnodesize = dnodesize;
951 	(*zpp)->z_projid = projid;
952 
953 	if (obj_type == DMU_OT_ZNODE ||
954 	    acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
955 		VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
956 	}
957 	kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
958 	zfs_znode_hold_exit(zfsvfs, zh);
959 }
960 
961 /*
962  * Update in-core attributes.  It is assumed the caller will be doing an
963  * sa_bulk_update to push the changes out.
964  */
965 void
zfs_xvattr_set(znode_t * zp,xvattr_t * xvap,dmu_tx_t * tx)966 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
967 {
968 	xoptattr_t *xoap;
969 	boolean_t update_inode = B_FALSE;
970 
971 	xoap = xva_getxoptattr(xvap);
972 	ASSERT(xoap);
973 
974 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
975 		uint64_t times[2];
976 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
977 		(void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
978 		    &times, sizeof (times), tx);
979 		XVA_SET_RTN(xvap, XAT_CREATETIME);
980 	}
981 	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
982 		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
983 		    zp->z_pflags, tx);
984 		XVA_SET_RTN(xvap, XAT_READONLY);
985 	}
986 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
987 		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
988 		    zp->z_pflags, tx);
989 		XVA_SET_RTN(xvap, XAT_HIDDEN);
990 	}
991 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
992 		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
993 		    zp->z_pflags, tx);
994 		XVA_SET_RTN(xvap, XAT_SYSTEM);
995 	}
996 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
997 		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
998 		    zp->z_pflags, tx);
999 		XVA_SET_RTN(xvap, XAT_ARCHIVE);
1000 	}
1001 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1002 		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1003 		    zp->z_pflags, tx);
1004 		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1005 
1006 		update_inode = B_TRUE;
1007 	}
1008 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1009 		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1010 		    zp->z_pflags, tx);
1011 		XVA_SET_RTN(xvap, XAT_NOUNLINK);
1012 	}
1013 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1014 		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1015 		    zp->z_pflags, tx);
1016 		XVA_SET_RTN(xvap, XAT_APPENDONLY);
1017 
1018 		update_inode = B_TRUE;
1019 	}
1020 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1021 		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1022 		    zp->z_pflags, tx);
1023 		XVA_SET_RTN(xvap, XAT_NODUMP);
1024 	}
1025 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1026 		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1027 		    zp->z_pflags, tx);
1028 		XVA_SET_RTN(xvap, XAT_OPAQUE);
1029 	}
1030 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1031 		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1032 		    xoap->xoa_av_quarantined, zp->z_pflags, tx);
1033 		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1034 	}
1035 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1036 		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1037 		    zp->z_pflags, tx);
1038 		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1039 	}
1040 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1041 		zfs_sa_set_scanstamp(zp, xvap, tx);
1042 		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1043 	}
1044 	if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1045 		ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1046 		    zp->z_pflags, tx);
1047 		XVA_SET_RTN(xvap, XAT_REPARSE);
1048 	}
1049 	if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1050 		ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1051 		    zp->z_pflags, tx);
1052 		XVA_SET_RTN(xvap, XAT_OFFLINE);
1053 	}
1054 	if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1055 		ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1056 		    zp->z_pflags, tx);
1057 		XVA_SET_RTN(xvap, XAT_SPARSE);
1058 	}
1059 	if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
1060 		ZFS_ATTR_SET(zp, ZFS_PROJINHERIT, xoap->xoa_projinherit,
1061 		    zp->z_pflags, tx);
1062 		XVA_SET_RTN(xvap, XAT_PROJINHERIT);
1063 	}
1064 
1065 	if (update_inode)
1066 		zfs_set_inode_flags(zp, ZTOI(zp));
1067 }
1068 
1069 int
zfs_zget(zfsvfs_t * zfsvfs,uint64_t obj_num,znode_t ** zpp)1070 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1071 {
1072 	dmu_object_info_t doi;
1073 	dmu_buf_t	*db;
1074 	znode_t		*zp;
1075 	znode_hold_t	*zh;
1076 	int err;
1077 	sa_handle_t	*hdl;
1078 
1079 	*zpp = NULL;
1080 
1081 again:
1082 	zh = zfs_znode_hold_enter(zfsvfs, obj_num);
1083 
1084 	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1085 	if (err) {
1086 		zfs_znode_hold_exit(zfsvfs, zh);
1087 		return (err);
1088 	}
1089 
1090 	dmu_object_info_from_db(db, &doi);
1091 	if (doi.doi_bonus_type != DMU_OT_SA &&
1092 	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
1093 	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
1094 	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1095 		sa_buf_rele(db, NULL);
1096 		zfs_znode_hold_exit(zfsvfs, zh);
1097 		return (SET_ERROR(EINVAL));
1098 	}
1099 
1100 	hdl = dmu_buf_get_user(db);
1101 	if (hdl != NULL) {
1102 		zp = sa_get_userdata(hdl);
1103 
1104 
1105 		/*
1106 		 * Since "SA" does immediate eviction we
1107 		 * should never find a sa handle that doesn't
1108 		 * know about the znode.
1109 		 */
1110 
1111 		ASSERT3P(zp, !=, NULL);
1112 
1113 		mutex_enter(&zp->z_lock);
1114 		ASSERT3U(zp->z_id, ==, obj_num);
1115 		/*
1116 		 * If zp->z_unlinked is set, the znode is already marked
1117 		 * for deletion and should not be discovered. Check this
1118 		 * after checking igrab() due to fsetxattr() & O_TMPFILE.
1119 		 *
1120 		 * If igrab() returns NULL the VFS has independently
1121 		 * determined the inode should be evicted and has
1122 		 * called iput_final() to start the eviction process.
1123 		 * The SA handle is still valid but because the VFS
1124 		 * requires that the eviction succeed we must drop
1125 		 * our locks and references to allow the eviction to
1126 		 * complete.  The zfs_zget() may then be retried.
1127 		 *
1128 		 * This unlikely case could be optimized by registering
1129 		 * a sops->drop_inode() callback.  The callback would
1130 		 * need to detect the active SA hold thereby informing
1131 		 * the VFS that this inode should not be evicted.
1132 		 */
1133 		if (igrab(ZTOI(zp)) == NULL) {
1134 			if (zp->z_unlinked)
1135 				err = SET_ERROR(ENOENT);
1136 			else
1137 				err = SET_ERROR(EAGAIN);
1138 		} else {
1139 			*zpp = zp;
1140 			err = 0;
1141 		}
1142 
1143 		mutex_exit(&zp->z_lock);
1144 		sa_buf_rele(db, NULL);
1145 		zfs_znode_hold_exit(zfsvfs, zh);
1146 
1147 		if (err == EAGAIN) {
1148 			/* inode might need this to finish evict */
1149 			cond_resched();
1150 			goto again;
1151 		}
1152 		return (err);
1153 	}
1154 
1155 	/*
1156 	 * Not found create new znode/vnode but only if file exists.
1157 	 *
1158 	 * There is a small window where zfs_vget() could
1159 	 * find this object while a file create is still in
1160 	 * progress.  This is checked for in zfs_znode_alloc()
1161 	 *
1162 	 * if zfs_znode_alloc() fails it will drop the hold on the
1163 	 * bonus buffer.
1164 	 */
1165 	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1166 	    doi.doi_bonus_type, NULL);
1167 	if (zp == NULL) {
1168 		err = SET_ERROR(ENOENT);
1169 	} else {
1170 		*zpp = zp;
1171 	}
1172 	zfs_znode_hold_exit(zfsvfs, zh);
1173 	return (err);
1174 }
1175 
1176 int
zfs_rezget(znode_t * zp)1177 zfs_rezget(znode_t *zp)
1178 {
1179 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
1180 	dmu_object_info_t doi;
1181 	dmu_buf_t *db;
1182 	uint64_t obj_num = zp->z_id;
1183 	uint64_t mode;
1184 	uint64_t links;
1185 	sa_bulk_attr_t bulk[11];
1186 	int err;
1187 	int count = 0;
1188 	uint64_t gen;
1189 	uint64_t z_uid, z_gid;
1190 	uint64_t atime[2], mtime[2], ctime[2], btime[2];
1191 	inode_timespec_t tmp_ts;
1192 	uint64_t projid = ZFS_DEFAULT_PROJID;
1193 	znode_hold_t *zh;
1194 
1195 	/*
1196 	 * skip ctldir, otherwise they will always get invalidated. This will
1197 	 * cause funny behaviour for the mounted snapdirs. Especially for
1198 	 * Linux >= 3.18, d_invalidate will detach the mountpoint and prevent
1199 	 * anyone automount it again as long as someone is still using the
1200 	 * detached mount.
1201 	 */
1202 	if (zp->z_is_ctldir)
1203 		return (0);
1204 
1205 	zh = zfs_znode_hold_enter(zfsvfs, obj_num);
1206 
1207 	mutex_enter(&zp->z_acl_lock);
1208 	if (zp->z_acl_cached) {
1209 		zfs_acl_free(zp->z_acl_cached);
1210 		zp->z_acl_cached = NULL;
1211 	}
1212 	mutex_exit(&zp->z_acl_lock);
1213 
1214 	rw_enter(&zp->z_xattr_lock, RW_WRITER);
1215 	if (zp->z_xattr_cached) {
1216 		nvlist_free(zp->z_xattr_cached);
1217 		zp->z_xattr_cached = NULL;
1218 	}
1219 	rw_exit(&zp->z_xattr_lock);
1220 
1221 	ASSERT(zp->z_sa_hdl == NULL);
1222 	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1223 	if (err) {
1224 		zfs_znode_hold_exit(zfsvfs, zh);
1225 		return (err);
1226 	}
1227 
1228 	dmu_object_info_from_db(db, &doi);
1229 	if (doi.doi_bonus_type != DMU_OT_SA &&
1230 	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
1231 	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
1232 	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1233 		sa_buf_rele(db, NULL);
1234 		zfs_znode_hold_exit(zfsvfs, zh);
1235 		return (SET_ERROR(EINVAL));
1236 	}
1237 
1238 	zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1239 
1240 	/* reload cached values */
1241 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1242 	    &gen, sizeof (gen));
1243 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1244 	    &zp->z_size, sizeof (zp->z_size));
1245 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1246 	    &links, sizeof (links));
1247 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1248 	    &zp->z_pflags, sizeof (zp->z_pflags));
1249 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1250 	    &z_uid, sizeof (z_uid));
1251 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1252 	    &z_gid, sizeof (z_gid));
1253 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1254 	    &mode, sizeof (mode));
1255 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1256 	    &atime, 16);
1257 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
1258 	    &mtime, 16);
1259 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
1260 	    &ctime, 16);
1261 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &btime, 16);
1262 
1263 	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1264 		zfs_znode_dmu_fini(zp);
1265 		zfs_znode_hold_exit(zfsvfs, zh);
1266 		return (SET_ERROR(EIO));
1267 	}
1268 
1269 	if (dmu_objset_projectquota_enabled(zfsvfs->z_os)) {
1270 		err = sa_lookup(zp->z_sa_hdl, SA_ZPL_PROJID(zfsvfs),
1271 		    &projid, 8);
1272 		if (err != 0 && err != ENOENT) {
1273 			zfs_znode_dmu_fini(zp);
1274 			zfs_znode_hold_exit(zfsvfs, zh);
1275 			return (SET_ERROR(err));
1276 		}
1277 	}
1278 
1279 	zp->z_projid = projid;
1280 	zp->z_mode = ZTOI(zp)->i_mode = mode;
1281 	zfs_uid_write(ZTOI(zp), z_uid);
1282 	zfs_gid_write(ZTOI(zp), z_gid);
1283 
1284 	ZFS_TIME_DECODE(&tmp_ts, atime);
1285 	zpl_inode_set_atime_to_ts(ZTOI(zp), tmp_ts);
1286 	ZFS_TIME_DECODE(&tmp_ts, mtime);
1287 	zpl_inode_set_mtime_to_ts(ZTOI(zp), tmp_ts);
1288 	ZFS_TIME_DECODE(&tmp_ts, ctime);
1289 	zpl_inode_set_ctime_to_ts(ZTOI(zp), tmp_ts);
1290 	ZFS_TIME_DECODE(&zp->z_btime, btime);
1291 
1292 	if ((uint32_t)gen != ZTOI(zp)->i_generation) {
1293 		zfs_znode_dmu_fini(zp);
1294 		zfs_znode_hold_exit(zfsvfs, zh);
1295 		return (SET_ERROR(EIO));
1296 	}
1297 
1298 	set_nlink(ZTOI(zp), (uint32_t)links);
1299 	zfs_set_inode_flags(zp, ZTOI(zp));
1300 
1301 	zp->z_blksz = doi.doi_data_block_size;
1302 	zp->z_atime_dirty = B_FALSE;
1303 	zfs_znode_update_vfs(zp);
1304 
1305 	/*
1306 	 * If the file has zero links, then it has been unlinked on the send
1307 	 * side and it must be in the received unlinked set.
1308 	 * We call zfs_znode_dmu_fini() now to prevent any accesses to the
1309 	 * stale data and to prevent automatic removal of the file in
1310 	 * zfs_zinactive().  The file will be removed either when it is removed
1311 	 * on the send side and the next incremental stream is received or
1312 	 * when the unlinked set gets processed.
1313 	 */
1314 	zp->z_unlinked = (ZTOI(zp)->i_nlink == 0);
1315 	if (zp->z_unlinked)
1316 		zfs_znode_dmu_fini(zp);
1317 
1318 	zfs_znode_hold_exit(zfsvfs, zh);
1319 
1320 	return (0);
1321 }
1322 
1323 void
zfs_znode_delete(znode_t * zp,dmu_tx_t * tx)1324 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1325 {
1326 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
1327 	objset_t *os = zfsvfs->z_os;
1328 	uint64_t obj = zp->z_id;
1329 	uint64_t acl_obj = zfs_external_acl(zp);
1330 	znode_hold_t *zh;
1331 
1332 	zh = zfs_znode_hold_enter(zfsvfs, obj);
1333 	if (acl_obj) {
1334 		VERIFY(!zp->z_is_sa);
1335 		VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1336 	}
1337 	VERIFY(0 == dmu_object_free(os, obj, tx));
1338 	zfs_znode_dmu_fini(zp);
1339 	zfs_znode_hold_exit(zfsvfs, zh);
1340 }
1341 
1342 void
zfs_zinactive(znode_t * zp)1343 zfs_zinactive(znode_t *zp)
1344 {
1345 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
1346 	uint64_t z_id = zp->z_id;
1347 	znode_hold_t *zh;
1348 
1349 	ASSERT(zp->z_sa_hdl);
1350 
1351 	/*
1352 	 * Don't allow a zfs_zget() while were trying to release this znode.
1353 	 */
1354 	zh = zfs_znode_hold_enter(zfsvfs, z_id);
1355 
1356 	mutex_enter(&zp->z_lock);
1357 
1358 	/*
1359 	 * If this was the last reference to a file with no links, remove
1360 	 * the file from the file system unless the file system is mounted
1361 	 * read-only.  That can happen, for example, if the file system was
1362 	 * originally read-write, the file was opened, then unlinked and
1363 	 * the file system was made read-only before the file was finally
1364 	 * closed.  The file will remain in the unlinked set.
1365 	 */
1366 	if (zp->z_unlinked) {
1367 		ASSERT(!zfsvfs->z_issnap);
1368 		if (!zfs_is_readonly(zfsvfs) && !zfs_unlink_suspend_progress) {
1369 			mutex_exit(&zp->z_lock);
1370 			zfs_znode_hold_exit(zfsvfs, zh);
1371 			zfs_rmnode(zp);
1372 			return;
1373 		}
1374 	}
1375 
1376 	mutex_exit(&zp->z_lock);
1377 	zfs_znode_dmu_fini(zp);
1378 
1379 	zfs_znode_hold_exit(zfsvfs, zh);
1380 }
1381 
1382 #if defined(HAVE_INODE_TIMESPEC64_TIMES)
1383 #define	zfs_compare_timespec timespec64_compare
1384 #else
1385 #define	zfs_compare_timespec timespec_compare
1386 #endif
1387 
1388 /*
1389  * Determine whether the znode's atime must be updated.  The logic mostly
1390  * duplicates the Linux kernel's relatime_need_update() functionality.
1391  * This function is only called if the underlying filesystem actually has
1392  * atime updates enabled.
1393  */
1394 boolean_t
zfs_relatime_need_update(const struct inode * ip)1395 zfs_relatime_need_update(const struct inode *ip)
1396 {
1397 	inode_timespec_t now, tmp_atime, tmp_ts;
1398 
1399 	gethrestime(&now);
1400 	tmp_atime = zpl_inode_get_atime(ip);
1401 	/*
1402 	 * In relatime mode, only update the atime if the previous atime
1403 	 * is earlier than either the ctime or mtime or if at least a day
1404 	 * has passed since the last update of atime.
1405 	 */
1406 	tmp_ts = zpl_inode_get_mtime(ip);
1407 	if (zfs_compare_timespec(&tmp_ts, &tmp_atime) >= 0)
1408 		return (B_TRUE);
1409 
1410 	tmp_ts = zpl_inode_get_ctime(ip);
1411 	if (zfs_compare_timespec(&tmp_ts, &tmp_atime) >= 0)
1412 		return (B_TRUE);
1413 
1414 	if ((hrtime_t)now.tv_sec - (hrtime_t)tmp_atime.tv_sec >= 24*60*60)
1415 		return (B_TRUE);
1416 
1417 	return (B_FALSE);
1418 }
1419 
1420 /*
1421  * Prepare to update znode time stamps.
1422  *
1423  *	IN:	zp	- znode requiring timestamp update
1424  *		flag	- ATTR_MTIME, ATTR_CTIME flags
1425  *
1426  *	OUT:	zp	- z_seq
1427  *		mtime	- new mtime
1428  *		ctime	- new ctime
1429  *
1430  *	Note: We don't update atime here, because we rely on Linux VFS to do
1431  *	atime updating.
1432  */
1433 void
zfs_tstamp_update_setup(znode_t * zp,uint_t flag,uint64_t mtime[2],uint64_t ctime[2])1434 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1435     uint64_t ctime[2])
1436 {
1437 	inode_timespec_t now, tmp_ts;
1438 
1439 	gethrestime(&now);
1440 
1441 	zp->z_seq++;
1442 
1443 	if (flag & ATTR_MTIME) {
1444 		ZFS_TIME_ENCODE(&now, mtime);
1445 		ZFS_TIME_DECODE(&tmp_ts, mtime);
1446 		zpl_inode_set_mtime_to_ts(ZTOI(zp), tmp_ts);
1447 		if (ZTOZSB(zp)->z_use_fuids) {
1448 			zp->z_pflags |= (ZFS_ARCHIVE |
1449 			    ZFS_AV_MODIFIED);
1450 		}
1451 	}
1452 
1453 	if (flag & ATTR_CTIME) {
1454 		ZFS_TIME_ENCODE(&now, ctime);
1455 		ZFS_TIME_DECODE(&tmp_ts, ctime);
1456 		zpl_inode_set_ctime_to_ts(ZTOI(zp), tmp_ts);
1457 		if (ZTOZSB(zp)->z_use_fuids)
1458 			zp->z_pflags |= ZFS_ARCHIVE;
1459 	}
1460 }
1461 
1462 /*
1463  * Grow the block size for a file.
1464  *
1465  *	IN:	zp	- znode of file to free data in.
1466  *		size	- requested block size
1467  *		tx	- open transaction.
1468  *
1469  * NOTE: this function assumes that the znode is write locked.
1470  */
1471 void
zfs_grow_blocksize(znode_t * zp,uint64_t size,dmu_tx_t * tx)1472 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1473 {
1474 	int		error;
1475 	u_longlong_t	dummy;
1476 
1477 	if (size <= zp->z_blksz)
1478 		return;
1479 	/*
1480 	 * If the file size is already greater than the current blocksize,
1481 	 * we will not grow.  If there is more than one block in a file,
1482 	 * the blocksize cannot change.
1483 	 */
1484 	if (zp->z_blksz && zp->z_size > zp->z_blksz)
1485 		return;
1486 
1487 	error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id,
1488 	    size, 0, tx);
1489 
1490 	if (error == ENOTSUP)
1491 		return;
1492 	ASSERT0(error);
1493 
1494 	/* What blocksize did we actually get? */
1495 	dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1496 }
1497 
1498 /*
1499  * Increase the file length
1500  *
1501  *	IN:	zp	- znode of file to free data in.
1502  *		end	- new end-of-file
1503  *
1504  *	RETURN:	0 on success, error code on failure
1505  */
1506 static int
zfs_extend(znode_t * zp,uint64_t end)1507 zfs_extend(znode_t *zp, uint64_t end)
1508 {
1509 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
1510 	dmu_tx_t *tx;
1511 	zfs_locked_range_t *lr;
1512 	uint64_t newblksz;
1513 	int error;
1514 
1515 	/*
1516 	 * We will change zp_size, lock the whole file.
1517 	 */
1518 	lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
1519 
1520 	/*
1521 	 * Nothing to do if file already at desired length.
1522 	 */
1523 	if (end <= zp->z_size) {
1524 		zfs_rangelock_exit(lr);
1525 		return (0);
1526 	}
1527 	tx = dmu_tx_create(zfsvfs->z_os);
1528 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1529 	zfs_sa_upgrade_txholds(tx, zp);
1530 	if (end > zp->z_blksz &&
1531 	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1532 		/*
1533 		 * We are growing the file past the current block size.
1534 		 */
1535 		if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) {
1536 			/*
1537 			 * File's blocksize is already larger than the
1538 			 * "recordsize" property.  Only let it grow to
1539 			 * the next power of 2.
1540 			 */
1541 			ASSERT(!ISP2(zp->z_blksz));
1542 			newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1543 		} else {
1544 			newblksz = MIN(end, ZTOZSB(zp)->z_max_blksz);
1545 		}
1546 		dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1547 	} else {
1548 		newblksz = 0;
1549 	}
1550 
1551 	error = dmu_tx_assign(tx, TXG_WAIT);
1552 	if (error) {
1553 		dmu_tx_abort(tx);
1554 		zfs_rangelock_exit(lr);
1555 		return (error);
1556 	}
1557 
1558 	if (newblksz)
1559 		zfs_grow_blocksize(zp, newblksz, tx);
1560 
1561 	zp->z_size = end;
1562 
1563 	VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(ZTOZSB(zp)),
1564 	    &zp->z_size, sizeof (zp->z_size), tx));
1565 
1566 	zfs_rangelock_exit(lr);
1567 
1568 	dmu_tx_commit(tx);
1569 
1570 	return (0);
1571 }
1572 
1573 /*
1574  * zfs_zero_partial_page - Modeled after update_pages() but
1575  * with different arguments and semantics for use by zfs_freesp().
1576  *
1577  * Zeroes a piece of a single page cache entry for zp at offset
1578  * start and length len.
1579  *
1580  * Caller must acquire a range lock on the file for the region
1581  * being zeroed in order that the ARC and page cache stay in sync.
1582  */
1583 static void
zfs_zero_partial_page(znode_t * zp,uint64_t start,uint64_t len)1584 zfs_zero_partial_page(znode_t *zp, uint64_t start, uint64_t len)
1585 {
1586 	struct address_space *mp = ZTOI(zp)->i_mapping;
1587 	struct page *pp;
1588 	int64_t	off;
1589 	void *pb;
1590 
1591 	ASSERT((start & PAGE_MASK) == ((start + len - 1) & PAGE_MASK));
1592 
1593 	off = start & (PAGE_SIZE - 1);
1594 	start &= PAGE_MASK;
1595 
1596 	pp = find_lock_page(mp, start >> PAGE_SHIFT);
1597 	if (pp) {
1598 		if (mapping_writably_mapped(mp))
1599 			flush_dcache_page(pp);
1600 
1601 		pb = kmap(pp);
1602 		bzero(pb + off, len);
1603 		kunmap(pp);
1604 
1605 		if (mapping_writably_mapped(mp))
1606 			flush_dcache_page(pp);
1607 
1608 		mark_page_accessed(pp);
1609 		SetPageUptodate(pp);
1610 		ClearPageError(pp);
1611 		unlock_page(pp);
1612 		put_page(pp);
1613 	}
1614 }
1615 
1616 /*
1617  * Free space in a file.
1618  *
1619  *	IN:	zp	- znode of file to free data in.
1620  *		off	- start of section to free.
1621  *		len	- length of section to free.
1622  *
1623  *	RETURN:	0 on success, error code on failure
1624  */
1625 static int
zfs_free_range(znode_t * zp,uint64_t off,uint64_t len)1626 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1627 {
1628 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
1629 	zfs_locked_range_t *lr;
1630 	int error;
1631 
1632 	/*
1633 	 * Lock the range being freed.
1634 	 */
1635 	lr = zfs_rangelock_enter(&zp->z_rangelock, off, len, RL_WRITER);
1636 
1637 	/*
1638 	 * Nothing to do if file already at desired length.
1639 	 */
1640 	if (off >= zp->z_size) {
1641 		zfs_rangelock_exit(lr);
1642 		return (0);
1643 	}
1644 
1645 	if (off + len > zp->z_size)
1646 		len = zp->z_size - off;
1647 
1648 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1649 
1650 	/*
1651 	 * Zero partial page cache entries.  This must be done under a
1652 	 * range lock in order to keep the ARC and page cache in sync.
1653 	 */
1654 	if (zn_has_cached_data(zp, off, off + len - 1)) {
1655 		loff_t first_page, last_page, page_len;
1656 		loff_t first_page_offset, last_page_offset;
1657 
1658 		/* first possible full page in hole */
1659 		first_page = (off + PAGE_SIZE - 1) >> PAGE_SHIFT;
1660 		/* last page of hole */
1661 		last_page = (off + len) >> PAGE_SHIFT;
1662 
1663 		/* offset of first_page */
1664 		first_page_offset = first_page << PAGE_SHIFT;
1665 		/* offset of last_page */
1666 		last_page_offset = last_page << PAGE_SHIFT;
1667 
1668 		/* truncate whole pages */
1669 		if (last_page_offset > first_page_offset) {
1670 			truncate_inode_pages_range(ZTOI(zp)->i_mapping,
1671 			    first_page_offset, last_page_offset - 1);
1672 		}
1673 
1674 		/* truncate sub-page ranges */
1675 		if (first_page > last_page) {
1676 			/* entire punched area within a single page */
1677 			zfs_zero_partial_page(zp, off, len);
1678 		} else {
1679 			/* beginning of punched area at the end of a page */
1680 			page_len  = first_page_offset - off;
1681 			if (page_len > 0)
1682 				zfs_zero_partial_page(zp, off, page_len);
1683 
1684 			/* end of punched area at the beginning of a page */
1685 			page_len = off + len - last_page_offset;
1686 			if (page_len > 0)
1687 				zfs_zero_partial_page(zp, last_page_offset,
1688 				    page_len);
1689 		}
1690 	}
1691 	zfs_rangelock_exit(lr);
1692 
1693 	return (error);
1694 }
1695 
1696 /*
1697  * Truncate a file
1698  *
1699  *	IN:	zp	- znode of file to free data in.
1700  *		end	- new end-of-file.
1701  *
1702  *	RETURN:	0 on success, error code on failure
1703  */
1704 static int
zfs_trunc(znode_t * zp,uint64_t end)1705 zfs_trunc(znode_t *zp, uint64_t end)
1706 {
1707 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
1708 	dmu_tx_t *tx;
1709 	zfs_locked_range_t *lr;
1710 	int error;
1711 	sa_bulk_attr_t bulk[2];
1712 	int count = 0;
1713 
1714 	/*
1715 	 * We will change zp_size, lock the whole file.
1716 	 */
1717 	lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
1718 
1719 	/*
1720 	 * Nothing to do if file already at desired length.
1721 	 */
1722 	if (end >= zp->z_size) {
1723 		zfs_rangelock_exit(lr);
1724 		return (0);
1725 	}
1726 
1727 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,
1728 	    DMU_OBJECT_END);
1729 	if (error) {
1730 		zfs_rangelock_exit(lr);
1731 		return (error);
1732 	}
1733 	tx = dmu_tx_create(zfsvfs->z_os);
1734 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1735 	zfs_sa_upgrade_txholds(tx, zp);
1736 	dmu_tx_mark_netfree(tx);
1737 	error = dmu_tx_assign(tx, TXG_WAIT);
1738 	if (error) {
1739 		dmu_tx_abort(tx);
1740 		zfs_rangelock_exit(lr);
1741 		return (error);
1742 	}
1743 
1744 	zp->z_size = end;
1745 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1746 	    NULL, &zp->z_size, sizeof (zp->z_size));
1747 
1748 	if (end == 0) {
1749 		zp->z_pflags &= ~ZFS_SPARSE;
1750 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1751 		    NULL, &zp->z_pflags, 8);
1752 	}
1753 	VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1754 
1755 	dmu_tx_commit(tx);
1756 	zfs_rangelock_exit(lr);
1757 
1758 	return (0);
1759 }
1760 
1761 /*
1762  * Free space in a file
1763  *
1764  *	IN:	zp	- znode of file to free data in.
1765  *		off	- start of range
1766  *		len	- end of range (0 => EOF)
1767  *		flag	- current file open mode flags.
1768  *		log	- TRUE if this action should be logged
1769  *
1770  *	RETURN:	0 on success, error code on failure
1771  */
1772 int
zfs_freesp(znode_t * zp,uint64_t off,uint64_t len,int flag,boolean_t log)1773 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1774 {
1775 	dmu_tx_t *tx;
1776 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
1777 	zilog_t *zilog = zfsvfs->z_log;
1778 	uint64_t mode;
1779 	uint64_t mtime[2], ctime[2];
1780 	sa_bulk_attr_t bulk[3];
1781 	int count = 0;
1782 	int error;
1783 
1784 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1785 	    sizeof (mode))) != 0)
1786 		return (error);
1787 
1788 	if (off > zp->z_size) {
1789 		error =  zfs_extend(zp, off+len);
1790 		if (error == 0 && log)
1791 			goto log;
1792 		goto out;
1793 	}
1794 
1795 	if (len == 0) {
1796 		error = zfs_trunc(zp, off);
1797 	} else {
1798 		if ((error = zfs_free_range(zp, off, len)) == 0 &&
1799 		    off + len > zp->z_size)
1800 			error = zfs_extend(zp, off+len);
1801 	}
1802 	if (error || !log)
1803 		goto out;
1804 log:
1805 	tx = dmu_tx_create(zfsvfs->z_os);
1806 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1807 	zfs_sa_upgrade_txholds(tx, zp);
1808 	error = dmu_tx_assign(tx, TXG_WAIT);
1809 	if (error) {
1810 		dmu_tx_abort(tx);
1811 		goto out;
1812 	}
1813 
1814 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1815 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1816 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1817 	    NULL, &zp->z_pflags, 8);
1818 	zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
1819 	error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1820 	ASSERT(error == 0);
1821 
1822 	zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1823 
1824 	dmu_tx_commit(tx);
1825 
1826 	zfs_znode_update_vfs(zp);
1827 	error = 0;
1828 
1829 out:
1830 	/*
1831 	 * Truncate the page cache - for file truncate operations, use
1832 	 * the purpose-built API for truncations.  For punching operations,
1833 	 * the truncation is handled under a range lock in zfs_free_range.
1834 	 */
1835 	if (len == 0)
1836 		truncate_setsize(ZTOI(zp), off);
1837 	return (error);
1838 }
1839 
1840 void
zfs_create_fs(objset_t * os,cred_t * cr,nvlist_t * zplprops,dmu_tx_t * tx)1841 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1842 {
1843 	struct super_block *sb;
1844 	zfsvfs_t	*zfsvfs;
1845 	uint64_t	moid, obj, sa_obj, version;
1846 	uint64_t	sense = ZFS_CASE_SENSITIVE;
1847 	uint64_t	norm = 0;
1848 	nvpair_t	*elem;
1849 	int		size;
1850 	int		error;
1851 	int		i;
1852 	znode_t		*rootzp = NULL;
1853 	vattr_t		vattr;
1854 	znode_t		*zp;
1855 	zfs_acl_ids_t	acl_ids;
1856 
1857 	/*
1858 	 * First attempt to create master node.
1859 	 */
1860 	/*
1861 	 * In an empty objset, there are no blocks to read and thus
1862 	 * there can be no i/o errors (which we assert below).
1863 	 */
1864 	moid = MASTER_NODE_OBJ;
1865 	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1866 	    DMU_OT_NONE, 0, tx);
1867 	ASSERT(error == 0);
1868 
1869 	/*
1870 	 * Set starting attributes.
1871 	 */
1872 	version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1873 	elem = NULL;
1874 	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1875 		/* For the moment we expect all zpl props to be uint64_ts */
1876 		uint64_t val;
1877 		char *name;
1878 
1879 		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1880 		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1881 		name = nvpair_name(elem);
1882 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1883 			if (val < version)
1884 				version = val;
1885 		} else {
1886 			error = zap_update(os, moid, name, 8, 1, &val, tx);
1887 		}
1888 		ASSERT(error == 0);
1889 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1890 			norm = val;
1891 		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1892 			sense = val;
1893 	}
1894 	ASSERT(version != 0);
1895 	error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1896 
1897 	/*
1898 	 * Create zap object used for SA attribute registration
1899 	 */
1900 
1901 	if (version >= ZPL_VERSION_SA) {
1902 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1903 		    DMU_OT_NONE, 0, tx);
1904 		error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1905 		ASSERT(error == 0);
1906 	} else {
1907 		sa_obj = 0;
1908 	}
1909 	/*
1910 	 * Create a delete queue.
1911 	 */
1912 	obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1913 
1914 	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1915 	ASSERT(error == 0);
1916 
1917 	/*
1918 	 * Create root znode.  Create minimal znode/inode/zfsvfs/sb
1919 	 * to allow zfs_mknode to work.
1920 	 */
1921 	vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID;
1922 	vattr.va_mode = S_IFDIR|0755;
1923 	vattr.va_uid = crgetuid(cr);
1924 	vattr.va_gid = crgetgid(cr);
1925 
1926 	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1927 	rootzp->z_unlinked = B_FALSE;
1928 	rootzp->z_atime_dirty = B_FALSE;
1929 	rootzp->z_is_sa = USE_SA(version, os);
1930 	rootzp->z_pflags = 0;
1931 
1932 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1933 	zfsvfs->z_os = os;
1934 	zfsvfs->z_parent = zfsvfs;
1935 	zfsvfs->z_version = version;
1936 	zfsvfs->z_use_fuids = USE_FUIDS(version, os);
1937 	zfsvfs->z_use_sa = USE_SA(version, os);
1938 	zfsvfs->z_norm = norm;
1939 
1940 	sb = kmem_zalloc(sizeof (struct super_block), KM_SLEEP);
1941 	sb->s_fs_info = zfsvfs;
1942 
1943 	ZTOI(rootzp)->i_sb = sb;
1944 
1945 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1946 	    &zfsvfs->z_attr_table);
1947 
1948 	ASSERT(error == 0);
1949 
1950 	/*
1951 	 * Fold case on file systems that are always or sometimes case
1952 	 * insensitive.
1953 	 */
1954 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1955 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1956 
1957 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1958 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1959 	    offsetof(znode_t, z_link_node));
1960 
1961 	size = MIN(1 << (highbit64(zfs_object_mutex_size)-1), ZFS_OBJ_MTX_MAX);
1962 	zfsvfs->z_hold_size = size;
1963 	zfsvfs->z_hold_trees = vmem_zalloc(sizeof (avl_tree_t) * size,
1964 	    KM_SLEEP);
1965 	zfsvfs->z_hold_locks = vmem_zalloc(sizeof (kmutex_t) * size, KM_SLEEP);
1966 	for (i = 0; i != size; i++) {
1967 		avl_create(&zfsvfs->z_hold_trees[i], zfs_znode_hold_compare,
1968 		    sizeof (znode_hold_t), offsetof(znode_hold_t, zh_node));
1969 		mutex_init(&zfsvfs->z_hold_locks[i], NULL, MUTEX_DEFAULT, NULL);
1970 	}
1971 
1972 	VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1973 	    cr, NULL, &acl_ids));
1974 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1975 	ASSERT3P(zp, ==, rootzp);
1976 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1977 	ASSERT(error == 0);
1978 	zfs_acl_ids_free(&acl_ids);
1979 
1980 	atomic_set(&ZTOI(rootzp)->i_count, 0);
1981 	sa_handle_destroy(rootzp->z_sa_hdl);
1982 	kmem_cache_free(znode_cache, rootzp);
1983 
1984 	for (i = 0; i != size; i++) {
1985 		avl_destroy(&zfsvfs->z_hold_trees[i]);
1986 		mutex_destroy(&zfsvfs->z_hold_locks[i]);
1987 	}
1988 
1989 	mutex_destroy(&zfsvfs->z_znodes_lock);
1990 
1991 	vmem_free(zfsvfs->z_hold_trees, sizeof (avl_tree_t) * size);
1992 	vmem_free(zfsvfs->z_hold_locks, sizeof (kmutex_t) * size);
1993 	kmem_free(sb, sizeof (struct super_block));
1994 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1995 }
1996 #endif /* _KERNEL */
1997 
1998 static int
zfs_sa_setup(objset_t * osp,sa_attr_type_t ** sa_table)1999 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
2000 {
2001 	uint64_t sa_obj = 0;
2002 	int error;
2003 
2004 	error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
2005 	if (error != 0 && error != ENOENT)
2006 		return (error);
2007 
2008 	error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
2009 	return (error);
2010 }
2011 
2012 static int
zfs_grab_sa_handle(objset_t * osp,uint64_t obj,sa_handle_t ** hdlp,dmu_buf_t ** db,void * tag)2013 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
2014     dmu_buf_t **db, void *tag)
2015 {
2016 	dmu_object_info_t doi;
2017 	int error;
2018 
2019 	if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
2020 		return (error);
2021 
2022 	dmu_object_info_from_db(*db, &doi);
2023 	if ((doi.doi_bonus_type != DMU_OT_SA &&
2024 	    doi.doi_bonus_type != DMU_OT_ZNODE) ||
2025 	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
2026 	    doi.doi_bonus_size < sizeof (znode_phys_t))) {
2027 		sa_buf_rele(*db, tag);
2028 		return (SET_ERROR(ENOTSUP));
2029 	}
2030 
2031 	error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
2032 	if (error != 0) {
2033 		sa_buf_rele(*db, tag);
2034 		return (error);
2035 	}
2036 
2037 	return (0);
2038 }
2039 
2040 static void
zfs_release_sa_handle(sa_handle_t * hdl,dmu_buf_t * db,void * tag)2041 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
2042 {
2043 	sa_handle_destroy(hdl);
2044 	sa_buf_rele(db, tag);
2045 }
2046 
2047 /*
2048  * Given an object number, return its parent object number and whether
2049  * or not the object is an extended attribute directory.
2050  */
2051 static int
zfs_obj_to_pobj(objset_t * osp,sa_handle_t * hdl,sa_attr_type_t * sa_table,uint64_t * pobjp,int * is_xattrdir)2052 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
2053     uint64_t *pobjp, int *is_xattrdir)
2054 {
2055 	uint64_t parent;
2056 	uint64_t pflags;
2057 	uint64_t mode;
2058 	uint64_t parent_mode;
2059 	sa_bulk_attr_t bulk[3];
2060 	sa_handle_t *sa_hdl;
2061 	dmu_buf_t *sa_db;
2062 	int count = 0;
2063 	int error;
2064 
2065 	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
2066 	    &parent, sizeof (parent));
2067 	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
2068 	    &pflags, sizeof (pflags));
2069 	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2070 	    &mode, sizeof (mode));
2071 
2072 	if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
2073 		return (error);
2074 
2075 	/*
2076 	 * When a link is removed its parent pointer is not changed and will
2077 	 * be invalid.  There are two cases where a link is removed but the
2078 	 * file stays around, when it goes to the delete queue and when there
2079 	 * are additional links.
2080 	 */
2081 	error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
2082 	if (error != 0)
2083 		return (error);
2084 
2085 	error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
2086 	zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2087 	if (error != 0)
2088 		return (error);
2089 
2090 	*is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
2091 
2092 	/*
2093 	 * Extended attributes can be applied to files, directories, etc.
2094 	 * Otherwise the parent must be a directory.
2095 	 */
2096 	if (!*is_xattrdir && !S_ISDIR(parent_mode))
2097 		return (SET_ERROR(EINVAL));
2098 
2099 	*pobjp = parent;
2100 
2101 	return (0);
2102 }
2103 
2104 /*
2105  * Given an object number, return some zpl level statistics
2106  */
2107 static int
zfs_obj_to_stats_impl(sa_handle_t * hdl,sa_attr_type_t * sa_table,zfs_stat_t * sb)2108 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2109     zfs_stat_t *sb)
2110 {
2111 	sa_bulk_attr_t bulk[4];
2112 	int count = 0;
2113 
2114 	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2115 	    &sb->zs_mode, sizeof (sb->zs_mode));
2116 	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2117 	    &sb->zs_gen, sizeof (sb->zs_gen));
2118 	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2119 	    &sb->zs_links, sizeof (sb->zs_links));
2120 	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2121 	    &sb->zs_ctime, sizeof (sb->zs_ctime));
2122 
2123 	return (sa_bulk_lookup(hdl, bulk, count));
2124 }
2125 
2126 static int
zfs_obj_to_path_impl(objset_t * osp,uint64_t obj,sa_handle_t * hdl,sa_attr_type_t * sa_table,char * buf,int len)2127 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2128     sa_attr_type_t *sa_table, char *buf, int len)
2129 {
2130 	sa_handle_t *sa_hdl;
2131 	sa_handle_t *prevhdl = NULL;
2132 	dmu_buf_t *prevdb = NULL;
2133 	dmu_buf_t *sa_db = NULL;
2134 	char *path = buf + len - 1;
2135 	int error;
2136 
2137 	*path = '\0';
2138 	sa_hdl = hdl;
2139 
2140 	uint64_t deleteq_obj;
2141 	VERIFY0(zap_lookup(osp, MASTER_NODE_OBJ,
2142 	    ZFS_UNLINKED_SET, sizeof (uint64_t), 1, &deleteq_obj));
2143 	error = zap_lookup_int(osp, deleteq_obj, obj);
2144 	if (error == 0) {
2145 		return (ESTALE);
2146 	} else if (error != ENOENT) {
2147 		return (error);
2148 	}
2149 	error = 0;
2150 
2151 	for (;;) {
2152 		uint64_t pobj = 0;
2153 		char component[MAXNAMELEN + 2];
2154 		size_t complen;
2155 		int is_xattrdir = 0;
2156 
2157 		if (prevdb) {
2158 			ASSERT(prevhdl != NULL);
2159 			zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2160 		}
2161 
2162 		if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2163 		    &is_xattrdir)) != 0)
2164 			break;
2165 
2166 		if (pobj == obj) {
2167 			if (path[0] != '/')
2168 				*--path = '/';
2169 			break;
2170 		}
2171 
2172 		component[0] = '/';
2173 		if (is_xattrdir) {
2174 			(void) sprintf(component + 1, "<xattrdir>");
2175 		} else {
2176 			error = zap_value_search(osp, pobj, obj,
2177 			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
2178 			if (error != 0)
2179 				break;
2180 		}
2181 
2182 		complen = strlen(component);
2183 		path -= complen;
2184 		ASSERT(path >= buf);
2185 		bcopy(component, path, complen);
2186 		obj = pobj;
2187 
2188 		if (sa_hdl != hdl) {
2189 			prevhdl = sa_hdl;
2190 			prevdb = sa_db;
2191 		}
2192 		error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2193 		if (error != 0) {
2194 			sa_hdl = prevhdl;
2195 			sa_db = prevdb;
2196 			break;
2197 		}
2198 	}
2199 
2200 	if (sa_hdl != NULL && sa_hdl != hdl) {
2201 		ASSERT(sa_db != NULL);
2202 		zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2203 	}
2204 
2205 	if (error == 0)
2206 		(void) memmove(buf, path, buf + len - path);
2207 
2208 	return (error);
2209 }
2210 
2211 int
zfs_obj_to_path(objset_t * osp,uint64_t obj,char * buf,int len)2212 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2213 {
2214 	sa_attr_type_t *sa_table;
2215 	sa_handle_t *hdl;
2216 	dmu_buf_t *db;
2217 	int error;
2218 
2219 	error = zfs_sa_setup(osp, &sa_table);
2220 	if (error != 0)
2221 		return (error);
2222 
2223 	error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2224 	if (error != 0)
2225 		return (error);
2226 
2227 	error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2228 
2229 	zfs_release_sa_handle(hdl, db, FTAG);
2230 	return (error);
2231 }
2232 
2233 int
zfs_obj_to_stats(objset_t * osp,uint64_t obj,zfs_stat_t * sb,char * buf,int len)2234 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2235     char *buf, int len)
2236 {
2237 	char *path = buf + len - 1;
2238 	sa_attr_type_t *sa_table;
2239 	sa_handle_t *hdl;
2240 	dmu_buf_t *db;
2241 	int error;
2242 
2243 	*path = '\0';
2244 
2245 	error = zfs_sa_setup(osp, &sa_table);
2246 	if (error != 0)
2247 		return (error);
2248 
2249 	error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2250 	if (error != 0)
2251 		return (error);
2252 
2253 	error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2254 	if (error != 0) {
2255 		zfs_release_sa_handle(hdl, db, FTAG);
2256 		return (error);
2257 	}
2258 
2259 	error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2260 
2261 	zfs_release_sa_handle(hdl, db, FTAG);
2262 	return (error);
2263 }
2264 
2265 #if defined(_KERNEL)
2266 EXPORT_SYMBOL(zfs_create_fs);
2267 EXPORT_SYMBOL(zfs_obj_to_path);
2268 
2269 /* CSTYLED */
2270 module_param(zfs_object_mutex_size, uint, 0644);
2271 MODULE_PARM_DESC(zfs_object_mutex_size, "Size of znode hold array");
2272 module_param(zfs_unlink_suspend_progress, int, 0644);
2273 MODULE_PARM_DESC(zfs_unlink_suspend_progress, "Set to prevent async unlinks "
2274 "(debug - leaks space into the unlinked set)");
2275 #endif
2276