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