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