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