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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
26 /* Portions Copyright 2007 Jeremy Teo */
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <sys/sysmacros.h>
33 #include <sys/mntent.h>
34 #include <sys/u8_textprep.h>
35 #include <sys/dsl_dataset.h>
37 #include <sys/vnode.h>
40 #include <sys/errno.h>
41 #include <sys/atomic.h>
42 #include <sys/zfs_dir.h>
43 #include <sys/zfs_acl.h>
44 #include <sys/zfs_ioctl.h>
45 #include <sys/zfs_rlock.h>
46 #include <sys/zfs_fuid.h>
47 #include <sys/zfs_vnops.h>
48 #include <sys/zfs_ctldir.h>
49 #include <sys/dnode.h>
50 #include <sys/fs/zfs.h>
55 #include <sys/dmu_objset.h>
56 #include <sys/dmu_tx.h>
57 #include <sys/zfs_refcount.h>
60 #include <sys/zfs_znode.h>
62 #include <sys/zfs_sa.h>
63 #include <sys/zfs_stat.h>
66 #include "zfs_comutil.h"
69 * Functions needed for userland (ie: libzpool) are not put under
70 * #ifdef_KERNEL; the rest of the functions have dependencies
71 * (such as VFS logic) that will not compile easily in userland.
75 static kmem_cache_t
*znode_cache
= NULL
;
76 static kmem_cache_t
*znode_hold_cache
= NULL
;
77 unsigned int zfs_object_mutex_size
= ZFS_OBJ_MTX_SZ
;
80 * This is used by the test suite so that it can delay znodes from being
81 * freed in order to inspect the unlinked set.
83 static int zfs_unlink_suspend_progress
= 0;
86 * This callback is invoked when acquiring a RL_WRITER or RL_APPEND lock on
87 * z_rangelock. It will modify the offset and length of the lock to reflect
88 * znode-specific information, and convert RL_APPEND to RL_WRITER. This is
89 * called with the rangelock_t's rl_lock held, which avoids races.
92 zfs_rangelock_cb(zfs_locked_range_t
*new, void *arg
)
97 * If in append mode, convert to writer and lock starting at the
98 * current end of file.
100 if (new->lr_type
== RL_APPEND
) {
101 new->lr_offset
= zp
->z_size
;
102 new->lr_type
= RL_WRITER
;
106 * If we need to grow the block size then lock the whole file range.
108 uint64_t end_size
= MAX(zp
->z_size
, new->lr_offset
+ new->lr_length
);
109 if (end_size
> zp
->z_blksz
&& (!ISP2(zp
->z_blksz
) ||
110 zp
->z_blksz
< ZTOZSB(zp
)->z_max_blksz
)) {
112 new->lr_length
= UINT64_MAX
;
117 zfs_znode_cache_constructor(void *buf
, void *arg
, int kmflags
)
119 (void) arg
, (void) kmflags
;
122 inode_init_once(ZTOI(zp
));
123 list_link_init(&zp
->z_link_node
);
125 mutex_init(&zp
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
126 rw_init(&zp
->z_parent_lock
, NULL
, RW_DEFAULT
, NULL
);
127 rw_init(&zp
->z_name_lock
, NULL
, RW_NOLOCKDEP
, NULL
);
128 mutex_init(&zp
->z_acl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
129 rw_init(&zp
->z_xattr_lock
, NULL
, RW_DEFAULT
, NULL
);
131 zfs_rangelock_init(&zp
->z_rangelock
, zfs_rangelock_cb
, zp
);
133 zp
->z_dirlocks
= NULL
;
134 zp
->z_acl_cached
= NULL
;
135 zp
->z_xattr_cached
= NULL
;
136 zp
->z_xattr_parent
= 0;
141 zfs_znode_cache_destructor(void *buf
, void *arg
)
146 ASSERT(!list_link_active(&zp
->z_link_node
));
147 mutex_destroy(&zp
->z_lock
);
148 rw_destroy(&zp
->z_parent_lock
);
149 rw_destroy(&zp
->z_name_lock
);
150 mutex_destroy(&zp
->z_acl_lock
);
151 rw_destroy(&zp
->z_xattr_lock
);
152 zfs_rangelock_fini(&zp
->z_rangelock
);
154 ASSERT3P(zp
->z_dirlocks
, ==, NULL
);
155 ASSERT3P(zp
->z_acl_cached
, ==, NULL
);
156 ASSERT3P(zp
->z_xattr_cached
, ==, NULL
);
160 zfs_znode_hold_cache_constructor(void *buf
, void *arg
, int kmflags
)
162 (void) arg
, (void) kmflags
;
163 znode_hold_t
*zh
= buf
;
165 mutex_init(&zh
->zh_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
166 zfs_refcount_create(&zh
->zh_refcount
);
167 zh
->zh_obj
= ZFS_NO_OBJECT
;
173 zfs_znode_hold_cache_destructor(void *buf
, void *arg
)
176 znode_hold_t
*zh
= buf
;
178 mutex_destroy(&zh
->zh_lock
);
179 zfs_refcount_destroy(&zh
->zh_refcount
);
186 * Initialize zcache. The KMC_SLAB hint is used in order that it be
187 * backed by kmalloc() when on the Linux slab in order that any
188 * wait_on_bit() operations on the related inode operate properly.
190 ASSERT(znode_cache
== NULL
);
191 znode_cache
= kmem_cache_create("zfs_znode_cache",
192 sizeof (znode_t
), 0, zfs_znode_cache_constructor
,
193 zfs_znode_cache_destructor
, NULL
, NULL
, NULL
, KMC_SLAB
);
195 ASSERT(znode_hold_cache
== NULL
);
196 znode_hold_cache
= kmem_cache_create("zfs_znode_hold_cache",
197 sizeof (znode_hold_t
), 0, zfs_znode_hold_cache_constructor
,
198 zfs_znode_hold_cache_destructor
, NULL
, NULL
, NULL
, 0);
208 kmem_cache_destroy(znode_cache
);
211 if (znode_hold_cache
)
212 kmem_cache_destroy(znode_hold_cache
);
213 znode_hold_cache
= NULL
;
217 * The zfs_znode_hold_enter() / zfs_znode_hold_exit() functions are used to
218 * serialize access to a znode and its SA buffer while the object is being
219 * created or destroyed. This kind of locking would normally reside in the
220 * znode itself but in this case that's impossible because the znode and SA
221 * buffer may not yet exist. Therefore the locking is handled externally
222 * with an array of mutexes and AVLs trees which contain per-object locks.
224 * In zfs_znode_hold_enter() a per-object lock is created as needed, inserted
225 * in to the correct AVL tree and finally the per-object lock is held. In
226 * zfs_znode_hold_exit() the process is reversed. The per-object lock is
227 * released, removed from the AVL tree and destroyed if there are no waiters.
229 * This scheme has two important properties:
231 * 1) No memory allocations are performed while holding one of the z_hold_locks.
232 * This ensures evict(), which can be called from direct memory reclaim, will
233 * never block waiting on a z_hold_locks which just happens to have hashed
236 * 2) All locks used to serialize access to an object are per-object and never
237 * shared. This minimizes lock contention without creating a large number
238 * of dedicated locks.
240 * On the downside it does require znode_lock_t structures to be frequently
241 * allocated and freed. However, because these are backed by a kmem cache
242 * and very short lived this cost is minimal.
245 zfs_znode_hold_compare(const void *a
, const void *b
)
247 const znode_hold_t
*zh_a
= (const znode_hold_t
*)a
;
248 const znode_hold_t
*zh_b
= (const znode_hold_t
*)b
;
250 return (TREE_CMP(zh_a
->zh_obj
, zh_b
->zh_obj
));
253 static boolean_t __maybe_unused
254 zfs_znode_held(zfsvfs_t
*zfsvfs
, uint64_t obj
)
256 znode_hold_t
*zh
, search
;
257 int i
= ZFS_OBJ_HASH(zfsvfs
, obj
);
262 mutex_enter(&zfsvfs
->z_hold_locks
[i
]);
263 zh
= avl_find(&zfsvfs
->z_hold_trees
[i
], &search
, NULL
);
264 held
= (zh
&& MUTEX_HELD(&zh
->zh_lock
)) ? B_TRUE
: B_FALSE
;
265 mutex_exit(&zfsvfs
->z_hold_locks
[i
]);
270 static znode_hold_t
*
271 zfs_znode_hold_enter(zfsvfs_t
*zfsvfs
, uint64_t obj
)
273 znode_hold_t
*zh
, *zh_new
, search
;
274 int i
= ZFS_OBJ_HASH(zfsvfs
, obj
);
275 boolean_t found
= B_FALSE
;
277 zh_new
= kmem_cache_alloc(znode_hold_cache
, KM_SLEEP
);
278 zh_new
->zh_obj
= obj
;
281 mutex_enter(&zfsvfs
->z_hold_locks
[i
]);
282 zh
= avl_find(&zfsvfs
->z_hold_trees
[i
], &search
, NULL
);
283 if (likely(zh
== NULL
)) {
285 avl_add(&zfsvfs
->z_hold_trees
[i
], zh
);
287 ASSERT3U(zh
->zh_obj
, ==, obj
);
290 zfs_refcount_add(&zh
->zh_refcount
, NULL
);
291 mutex_exit(&zfsvfs
->z_hold_locks
[i
]);
294 kmem_cache_free(znode_hold_cache
, zh_new
);
296 ASSERT(MUTEX_NOT_HELD(&zh
->zh_lock
));
297 ASSERT3S(zfs_refcount_count(&zh
->zh_refcount
), >, 0);
298 mutex_enter(&zh
->zh_lock
);
304 zfs_znode_hold_exit(zfsvfs_t
*zfsvfs
, znode_hold_t
*zh
)
306 int i
= ZFS_OBJ_HASH(zfsvfs
, zh
->zh_obj
);
307 boolean_t remove
= B_FALSE
;
309 ASSERT(zfs_znode_held(zfsvfs
, zh
->zh_obj
));
310 ASSERT3S(zfs_refcount_count(&zh
->zh_refcount
), >, 0);
311 mutex_exit(&zh
->zh_lock
);
313 mutex_enter(&zfsvfs
->z_hold_locks
[i
]);
314 if (zfs_refcount_remove(&zh
->zh_refcount
, NULL
) == 0) {
315 avl_remove(&zfsvfs
->z_hold_trees
[i
], zh
);
318 mutex_exit(&zfsvfs
->z_hold_locks
[i
]);
320 if (remove
== B_TRUE
)
321 kmem_cache_free(znode_hold_cache
, zh
);
325 zfs_cmpldev(uint64_t dev
)
331 zfs_znode_sa_init(zfsvfs_t
*zfsvfs
, znode_t
*zp
,
332 dmu_buf_t
*db
, dmu_object_type_t obj_type
, sa_handle_t
*sa_hdl
)
334 ASSERT(zfs_znode_held(zfsvfs
, zp
->z_id
));
336 mutex_enter(&zp
->z_lock
);
338 ASSERT(zp
->z_sa_hdl
== NULL
);
339 ASSERT(zp
->z_acl_cached
== NULL
);
340 if (sa_hdl
== NULL
) {
341 VERIFY(0 == sa_handle_get_from_db(zfsvfs
->z_os
, db
, zp
,
342 SA_HDL_SHARED
, &zp
->z_sa_hdl
));
344 zp
->z_sa_hdl
= sa_hdl
;
345 sa_set_userp(sa_hdl
, zp
);
348 zp
->z_is_sa
= (obj_type
== DMU_OT_SA
) ? B_TRUE
: B_FALSE
;
350 mutex_exit(&zp
->z_lock
);
354 zfs_znode_dmu_fini(znode_t
*zp
)
356 ASSERT(zfs_znode_held(ZTOZSB(zp
), zp
->z_id
) || zp
->z_unlinked
||
357 RW_WRITE_HELD(&ZTOZSB(zp
)->z_teardown_inactive_lock
));
359 sa_handle_destroy(zp
->z_sa_hdl
);
364 * Called by new_inode() to allocate a new inode.
367 zfs_inode_alloc(struct super_block
*sb
, struct inode
**ip
)
371 zp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
378 * Called in multiple places when an inode should be destroyed.
381 zfs_inode_destroy(struct inode
*ip
)
383 znode_t
*zp
= ITOZ(ip
);
384 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
386 mutex_enter(&zfsvfs
->z_znodes_lock
);
387 if (list_link_active(&zp
->z_link_node
)) {
388 list_remove(&zfsvfs
->z_all_znodes
, zp
);
389 zfsvfs
->z_nr_znodes
--;
391 mutex_exit(&zfsvfs
->z_znodes_lock
);
393 if (zp
->z_acl_cached
) {
394 zfs_acl_free(zp
->z_acl_cached
);
395 zp
->z_acl_cached
= NULL
;
398 if (zp
->z_xattr_cached
) {
399 nvlist_free(zp
->z_xattr_cached
);
400 zp
->z_xattr_cached
= NULL
;
403 kmem_cache_free(znode_cache
, zp
);
407 zfs_inode_set_ops(zfsvfs_t
*zfsvfs
, struct inode
*ip
)
411 switch (ip
->i_mode
& S_IFMT
) {
413 ip
->i_op
= &zpl_inode_operations
;
414 ip
->i_fop
= &zpl_file_operations
;
415 ip
->i_mapping
->a_ops
= &zpl_address_space_operations
;
419 ip
->i_op
= &zpl_dir_inode_operations
;
420 ip
->i_fop
= &zpl_dir_file_operations
;
421 ITOZ(ip
)->z_zn_prefetch
= B_TRUE
;
425 ip
->i_op
= &zpl_symlink_inode_operations
;
429 * rdev is only stored in a SA only for device files.
433 (void) sa_lookup(ITOZ(ip
)->z_sa_hdl
, SA_ZPL_RDEV(zfsvfs
), &rdev
,
438 init_special_inode(ip
, ip
->i_mode
, rdev
);
439 ip
->i_op
= &zpl_special_inode_operations
;
443 zfs_panic_recover("inode %llu has invalid mode: 0x%x\n",
444 (u_longlong_t
)ip
->i_ino
, ip
->i_mode
);
446 /* Assume the inode is a file and attempt to continue */
447 ip
->i_mode
= S_IFREG
| 0644;
448 ip
->i_op
= &zpl_inode_operations
;
449 ip
->i_fop
= &zpl_file_operations
;
450 ip
->i_mapping
->a_ops
= &zpl_address_space_operations
;
456 zfs_set_inode_flags(znode_t
*zp
, struct inode
*ip
)
459 * Linux and Solaris have different sets of file attributes, so we
460 * restrict this conversion to the intersection of the two.
462 #ifdef HAVE_INODE_SET_FLAGS
463 unsigned int flags
= 0;
464 if (zp
->z_pflags
& ZFS_IMMUTABLE
)
465 flags
|= S_IMMUTABLE
;
466 if (zp
->z_pflags
& ZFS_APPENDONLY
)
469 inode_set_flags(ip
, flags
, S_IMMUTABLE
|S_APPEND
);
471 if (zp
->z_pflags
& ZFS_IMMUTABLE
)
472 ip
->i_flags
|= S_IMMUTABLE
;
474 ip
->i_flags
&= ~S_IMMUTABLE
;
476 if (zp
->z_pflags
& ZFS_APPENDONLY
)
477 ip
->i_flags
|= S_APPEND
;
479 ip
->i_flags
&= ~S_APPEND
;
484 * Update the embedded inode given the znode.
487 zfs_znode_update_vfs(znode_t
*zp
)
492 u_longlong_t i_blocks
;
498 /* Skip .zfs control nodes which do not exist on disk. */
499 if (zfsctl_is_node(ip
))
502 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &blksize
, &i_blocks
);
504 spin_lock(&ip
->i_lock
);
505 ip
->i_mode
= zp
->z_mode
;
506 ip
->i_blocks
= i_blocks
;
507 i_size_write(ip
, zp
->z_size
);
508 spin_unlock(&ip
->i_lock
);
513 * Construct a znode+inode and initialize.
515 * This does not do a call to dmu_set_user() that is
516 * up to the caller to do, in case you don't want to
520 zfs_znode_alloc(zfsvfs_t
*zfsvfs
, dmu_buf_t
*db
, int blksz
,
521 dmu_object_type_t obj_type
, sa_handle_t
*hdl
)
529 uint64_t z_uid
, z_gid
;
530 uint64_t atime
[2], mtime
[2], ctime
[2], btime
[2];
531 uint64_t projid
= ZFS_DEFAULT_PROJID
;
532 sa_bulk_attr_t bulk
[12];
535 ASSERT(zfsvfs
!= NULL
);
537 ip
= new_inode(zfsvfs
->z_sb
);
542 ASSERT(zp
->z_dirlocks
== NULL
);
543 ASSERT3P(zp
->z_acl_cached
, ==, NULL
);
544 ASSERT3P(zp
->z_xattr_cached
, ==, NULL
);
545 zp
->z_unlinked
= B_FALSE
;
546 zp
->z_atime_dirty
= B_FALSE
;
547 zp
->z_is_mapped
= B_FALSE
;
548 zp
->z_is_ctldir
= B_FALSE
;
549 zp
->z_is_stale
= B_FALSE
;
550 zp
->z_suspended
= B_FALSE
;
553 zp
->z_id
= db
->db_object
;
555 zp
->z_seq
= 0x7A4653;
558 zfs_znode_sa_init(zfsvfs
, zp
, db
, obj_type
, hdl
);
560 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
, &mode
, 8);
561 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zfsvfs
), NULL
, &tmp_gen
, 8);
562 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
564 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
), NULL
, &links
, 8);
565 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
567 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PARENT(zfsvfs
), NULL
,
569 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
, &z_uid
, 8);
570 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
, &z_gid
, 8);
571 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
572 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
573 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
574 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CRTIME(zfsvfs
), NULL
, &btime
, 16);
576 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
) != 0 || tmp_gen
== 0 ||
577 (dmu_objset_projectquota_enabled(zfsvfs
->z_os
) &&
578 (zp
->z_pflags
& ZFS_PROJID
) &&
579 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PROJID(zfsvfs
), &projid
, 8) != 0)) {
581 sa_handle_destroy(zp
->z_sa_hdl
);
586 zp
->z_projid
= projid
;
587 zp
->z_mode
= ip
->i_mode
= mode
;
588 ip
->i_generation
= (uint32_t)tmp_gen
;
589 ip
->i_blkbits
= SPA_MINBLOCKSHIFT
;
590 set_nlink(ip
, (uint32_t)links
);
591 zfs_uid_write(ip
, z_uid
);
592 zfs_gid_write(ip
, z_gid
);
593 zfs_set_inode_flags(zp
, ip
);
595 /* Cache the xattr parent id */
596 if (zp
->z_pflags
& ZFS_XATTR
)
597 zp
->z_xattr_parent
= parent
;
599 ZFS_TIME_DECODE(&ip
->i_atime
, atime
);
600 ZFS_TIME_DECODE(&ip
->i_mtime
, mtime
);
601 ZFS_TIME_DECODE(&ip
->i_ctime
, ctime
);
602 ZFS_TIME_DECODE(&zp
->z_btime
, btime
);
604 ip
->i_ino
= zp
->z_id
;
605 zfs_znode_update_vfs(zp
);
606 zfs_inode_set_ops(zfsvfs
, ip
);
609 * The only way insert_inode_locked() can fail is if the ip->i_ino
610 * number is already hashed for this super block. This can never
611 * happen because the inode numbers map 1:1 with the object numbers.
613 * Exceptions include rolling back a mounted file system, either
614 * from the zfs rollback or zfs recv command.
616 * Active inodes are unhashed during the rollback, but since zrele
617 * can happen asynchronously, we can't guarantee they've been
618 * unhashed. This can cause hash collisions in unlinked drain
619 * processing so do not hash unlinked znodes.
622 VERIFY3S(insert_inode_locked(ip
), ==, 0);
624 mutex_enter(&zfsvfs
->z_znodes_lock
);
625 list_insert_tail(&zfsvfs
->z_all_znodes
, zp
);
626 zfsvfs
->z_nr_znodes
++;
627 mutex_exit(&zfsvfs
->z_znodes_lock
);
630 unlock_new_inode(ip
);
639 * Safely mark an inode dirty. Inodes which are part of a read-only
640 * file system or snapshot may not be dirtied.
643 zfs_mark_inode_dirty(struct inode
*ip
)
645 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
647 if (zfs_is_readonly(zfsvfs
) || dmu_objset_is_snapshot(zfsvfs
->z_os
))
650 mark_inode_dirty(ip
);
653 static uint64_t empty_xattr
;
654 static uint64_t pad
[4];
655 static zfs_acl_phys_t acl_phys
;
657 * Create a new DMU object to hold a zfs znode.
659 * IN: dzp - parent directory for new znode
660 * vap - file attributes for new znode
661 * tx - dmu transaction id for zap operations
662 * cr - credentials of caller
664 * IS_ROOT_NODE - new object will be root
665 * IS_TMPFILE - new object is of O_TMPFILE
666 * IS_XATTR - new object is an attribute
667 * acl_ids - ACL related attributes
669 * OUT: zpp - allocated znode (set to dzp if IS_ROOT_NODE)
673 zfs_mknode(znode_t
*dzp
, vattr_t
*vap
, dmu_tx_t
*tx
, cred_t
*cr
,
674 uint_t flag
, znode_t
**zpp
, zfs_acl_ids_t
*acl_ids
)
676 uint64_t crtime
[2], atime
[2], mtime
[2], ctime
[2];
677 uint64_t mode
, size
, links
, parent
, pflags
;
678 uint64_t projid
= ZFS_DEFAULT_PROJID
;
680 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
682 inode_timespec_t now
;
687 dmu_object_type_t obj_type
;
688 sa_bulk_attr_t
*sa_attrs
;
690 zfs_acl_locator_cb_t locate
= { 0 };
693 if (zfsvfs
->z_replay
) {
694 obj
= vap
->va_nodeid
;
695 now
= vap
->va_ctime
; /* see zfs_replay_create() */
696 gen
= vap
->va_nblocks
; /* ditto */
697 dnodesize
= vap
->va_fsid
; /* ditto */
701 gen
= dmu_tx_get_txg(tx
);
702 dnodesize
= dmu_objset_dnodesize(zfsvfs
->z_os
);
706 dnodesize
= DNODE_MIN_SIZE
;
708 obj_type
= zfsvfs
->z_use_sa
? DMU_OT_SA
: DMU_OT_ZNODE
;
710 bonuslen
= (obj_type
== DMU_OT_SA
) ?
711 DN_BONUS_SIZE(dnodesize
) : ZFS_OLD_ZNODE_PHYS_SIZE
;
714 * Create a new DMU object.
717 * There's currently no mechanism for pre-reading the blocks that will
718 * be needed to allocate a new object, so we accept the small chance
719 * that there will be an i/o error and we will fail one of the
722 if (S_ISDIR(vap
->va_mode
)) {
723 if (zfsvfs
->z_replay
) {
724 VERIFY0(zap_create_claim_norm_dnsize(zfsvfs
->z_os
, obj
,
725 zfsvfs
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
726 obj_type
, bonuslen
, dnodesize
, tx
));
728 obj
= zap_create_norm_dnsize(zfsvfs
->z_os
,
729 zfsvfs
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
730 obj_type
, bonuslen
, dnodesize
, tx
);
733 if (zfsvfs
->z_replay
) {
734 VERIFY0(dmu_object_claim_dnsize(zfsvfs
->z_os
, obj
,
735 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
736 obj_type
, bonuslen
, dnodesize
, tx
));
738 obj
= dmu_object_alloc_dnsize(zfsvfs
->z_os
,
739 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
740 obj_type
, bonuslen
, dnodesize
, tx
);
744 zh
= zfs_znode_hold_enter(zfsvfs
, obj
);
745 VERIFY0(sa_buf_hold(zfsvfs
->z_os
, obj
, NULL
, &db
));
748 * If this is the root, fix up the half-initialized parent pointer
749 * to reference the just-allocated physical data area.
751 if (flag
& IS_ROOT_NODE
) {
756 * If parent is an xattr, so am I.
758 if (dzp
->z_pflags
& ZFS_XATTR
) {
762 if (zfsvfs
->z_use_fuids
)
763 pflags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
767 if (S_ISDIR(vap
->va_mode
)) {
768 size
= 2; /* contents ("." and "..") */
772 links
= (flag
& IS_TMPFILE
) ? 0 : 1;
775 if (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))
779 mode
= acl_ids
->z_mode
;
783 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
)) {
785 * With ZFS_PROJID flag, we can easily know whether there is
786 * project ID stored on disk or not. See zfs_space_delta_cb().
788 if (obj_type
!= DMU_OT_ZNODE
&&
789 dmu_objset_projectquota_enabled(zfsvfs
->z_os
))
790 pflags
|= ZFS_PROJID
;
793 * Inherit project ID from parent if required.
795 projid
= zfs_inherit_projid(dzp
);
796 if (dzp
->z_pflags
& ZFS_PROJINHERIT
)
797 pflags
|= ZFS_PROJINHERIT
;
801 * No execs denied will be determined when zfs_mode_compute() is called.
803 pflags
|= acl_ids
->z_aclp
->z_hints
&
804 (ZFS_ACL_TRIVIAL
|ZFS_INHERIT_ACE
|ZFS_ACL_AUTO_INHERIT
|
805 ZFS_ACL_DEFAULTED
|ZFS_ACL_PROTECTED
);
807 ZFS_TIME_ENCODE(&now
, crtime
);
808 ZFS_TIME_ENCODE(&now
, ctime
);
810 if (vap
->va_mask
& ATTR_ATIME
) {
811 ZFS_TIME_ENCODE(&vap
->va_atime
, atime
);
813 ZFS_TIME_ENCODE(&now
, atime
);
816 if (vap
->va_mask
& ATTR_MTIME
) {
817 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
819 ZFS_TIME_ENCODE(&now
, mtime
);
822 /* Now add in all of the "SA" attributes */
823 VERIFY(0 == sa_handle_get_from_db(zfsvfs
->z_os
, db
, NULL
, SA_HDL_SHARED
,
827 * Setup the array of attributes to be replaced/set on the new file
829 * order for DMU_OT_ZNODE is critical since it needs to be constructed
830 * in the old znode_phys_t format. Don't change this ordering
832 sa_attrs
= kmem_alloc(sizeof (sa_bulk_attr_t
) * ZPL_END
, KM_SLEEP
);
834 if (obj_type
== DMU_OT_ZNODE
) {
835 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zfsvfs
),
837 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zfsvfs
),
839 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zfsvfs
),
841 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zfsvfs
),
843 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zfsvfs
),
845 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zfsvfs
),
847 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zfsvfs
),
849 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zfsvfs
),
852 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zfsvfs
),
854 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zfsvfs
),
856 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zfsvfs
),
858 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zfsvfs
),
859 NULL
, &acl_ids
->z_fuid
, 8);
860 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zfsvfs
),
861 NULL
, &acl_ids
->z_fgid
, 8);
862 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zfsvfs
),
864 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zfsvfs
),
866 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zfsvfs
),
868 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zfsvfs
),
870 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zfsvfs
),
872 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zfsvfs
),
876 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_LINKS(zfsvfs
), NULL
, &links
, 8);
878 if (obj_type
== DMU_OT_ZNODE
) {
879 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_XATTR(zfsvfs
), NULL
,
881 } else if (dmu_objset_projectquota_enabled(zfsvfs
->z_os
) &&
882 pflags
& ZFS_PROJID
) {
883 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PROJID(zfsvfs
),
886 if (obj_type
== DMU_OT_ZNODE
||
887 (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))) {
888 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_RDEV(zfsvfs
),
891 if (obj_type
== DMU_OT_ZNODE
) {
892 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zfsvfs
),
894 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zfsvfs
), NULL
,
895 &acl_ids
->z_fuid
, 8);
896 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zfsvfs
), NULL
,
897 &acl_ids
->z_fgid
, 8);
898 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PAD(zfsvfs
), NULL
, pad
,
899 sizeof (uint64_t) * 4);
900 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ZNODE_ACL(zfsvfs
), NULL
,
901 &acl_phys
, sizeof (zfs_acl_phys_t
));
902 } else if (acl_ids
->z_aclp
->z_version
>= ZFS_ACL_VERSION_FUID
) {
903 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_COUNT(zfsvfs
), NULL
,
904 &acl_ids
->z_aclp
->z_acl_count
, 8);
905 locate
.cb_aclp
= acl_ids
->z_aclp
;
906 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_ACES(zfsvfs
),
907 zfs_acl_data_locator
, &locate
,
908 acl_ids
->z_aclp
->z_acl_bytes
);
909 mode
= zfs_mode_compute(mode
, acl_ids
->z_aclp
, &pflags
,
910 acl_ids
->z_fuid
, acl_ids
->z_fgid
);
913 VERIFY(sa_replace_all_by_template(sa_hdl
, sa_attrs
, cnt
, tx
) == 0);
915 if (!(flag
& IS_ROOT_NODE
)) {
917 * The call to zfs_znode_alloc() may fail if memory is low
918 * via the call path: alloc_inode() -> inode_init_always() ->
919 * security_inode_alloc() -> inode_alloc_security(). Since
920 * the existing code is written such that zfs_mknode() can
921 * not fail retry until sufficient memory has been reclaimed.
924 *zpp
= zfs_znode_alloc(zfsvfs
, db
, 0, obj_type
, sa_hdl
);
925 } while (*zpp
== NULL
);
927 VERIFY(*zpp
!= NULL
);
931 * If we are creating the root node, the "parent" we
932 * passed in is the znode for the root.
936 (*zpp
)->z_sa_hdl
= sa_hdl
;
939 (*zpp
)->z_pflags
= pflags
;
940 (*zpp
)->z_mode
= ZTOI(*zpp
)->i_mode
= mode
;
941 (*zpp
)->z_dnodesize
= dnodesize
;
942 (*zpp
)->z_projid
= projid
;
944 if (obj_type
== DMU_OT_ZNODE
||
945 acl_ids
->z_aclp
->z_version
< ZFS_ACL_VERSION_FUID
) {
946 VERIFY0(zfs_aclset_common(*zpp
, acl_ids
->z_aclp
, cr
, tx
));
948 kmem_free(sa_attrs
, sizeof (sa_bulk_attr_t
) * ZPL_END
);
949 zfs_znode_hold_exit(zfsvfs
, zh
);
953 * Update in-core attributes. It is assumed the caller will be doing an
954 * sa_bulk_update to push the changes out.
957 zfs_xvattr_set(znode_t
*zp
, xvattr_t
*xvap
, dmu_tx_t
*tx
)
960 boolean_t update_inode
= B_FALSE
;
962 xoap
= xva_getxoptattr(xvap
);
965 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
967 ZFS_TIME_ENCODE(&xoap
->xoa_createtime
, times
);
968 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_CRTIME(ZTOZSB(zp
)),
969 ×
, sizeof (times
), tx
);
970 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
972 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
973 ZFS_ATTR_SET(zp
, ZFS_READONLY
, xoap
->xoa_readonly
,
975 XVA_SET_RTN(xvap
, XAT_READONLY
);
977 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
978 ZFS_ATTR_SET(zp
, ZFS_HIDDEN
, xoap
->xoa_hidden
,
980 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
982 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
983 ZFS_ATTR_SET(zp
, ZFS_SYSTEM
, xoap
->xoa_system
,
985 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
987 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
988 ZFS_ATTR_SET(zp
, ZFS_ARCHIVE
, xoap
->xoa_archive
,
990 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
992 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
993 ZFS_ATTR_SET(zp
, ZFS_IMMUTABLE
, xoap
->xoa_immutable
,
995 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
997 update_inode
= B_TRUE
;
999 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
1000 ZFS_ATTR_SET(zp
, ZFS_NOUNLINK
, xoap
->xoa_nounlink
,
1002 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
1004 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
1005 ZFS_ATTR_SET(zp
, ZFS_APPENDONLY
, xoap
->xoa_appendonly
,
1007 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
1009 update_inode
= B_TRUE
;
1011 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
1012 ZFS_ATTR_SET(zp
, ZFS_NODUMP
, xoap
->xoa_nodump
,
1014 XVA_SET_RTN(xvap
, XAT_NODUMP
);
1016 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
1017 ZFS_ATTR_SET(zp
, ZFS_OPAQUE
, xoap
->xoa_opaque
,
1019 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
1021 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
1022 ZFS_ATTR_SET(zp
, ZFS_AV_QUARANTINED
,
1023 xoap
->xoa_av_quarantined
, zp
->z_pflags
, tx
);
1024 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
1026 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
1027 ZFS_ATTR_SET(zp
, ZFS_AV_MODIFIED
, xoap
->xoa_av_modified
,
1029 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
1031 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
1032 zfs_sa_set_scanstamp(zp
, xvap
, tx
);
1033 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
1035 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
1036 ZFS_ATTR_SET(zp
, ZFS_REPARSE
, xoap
->xoa_reparse
,
1038 XVA_SET_RTN(xvap
, XAT_REPARSE
);
1040 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
1041 ZFS_ATTR_SET(zp
, ZFS_OFFLINE
, xoap
->xoa_offline
,
1043 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
1045 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
1046 ZFS_ATTR_SET(zp
, ZFS_SPARSE
, xoap
->xoa_sparse
,
1048 XVA_SET_RTN(xvap
, XAT_SPARSE
);
1050 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
1051 ZFS_ATTR_SET(zp
, ZFS_PROJINHERIT
, xoap
->xoa_projinherit
,
1053 XVA_SET_RTN(xvap
, XAT_PROJINHERIT
);
1057 zfs_set_inode_flags(zp
, ZTOI(zp
));
1061 zfs_zget(zfsvfs_t
*zfsvfs
, uint64_t obj_num
, znode_t
**zpp
)
1063 dmu_object_info_t doi
;
1073 zh
= zfs_znode_hold_enter(zfsvfs
, obj_num
);
1075 err
= sa_buf_hold(zfsvfs
->z_os
, obj_num
, NULL
, &db
);
1077 zfs_znode_hold_exit(zfsvfs
, zh
);
1081 dmu_object_info_from_db(db
, &doi
);
1082 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
1083 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
1084 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1085 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
1086 sa_buf_rele(db
, NULL
);
1087 zfs_znode_hold_exit(zfsvfs
, zh
);
1088 return (SET_ERROR(EINVAL
));
1091 hdl
= dmu_buf_get_user(db
);
1093 zp
= sa_get_userdata(hdl
);
1097 * Since "SA" does immediate eviction we
1098 * should never find a sa handle that doesn't
1099 * know about the znode.
1102 ASSERT3P(zp
, !=, NULL
);
1104 mutex_enter(&zp
->z_lock
);
1105 ASSERT3U(zp
->z_id
, ==, obj_num
);
1107 * If zp->z_unlinked is set, the znode is already marked
1108 * for deletion and should not be discovered. Check this
1109 * after checking igrab() due to fsetxattr() & O_TMPFILE.
1111 * If igrab() returns NULL the VFS has independently
1112 * determined the inode should be evicted and has
1113 * called iput_final() to start the eviction process.
1114 * The SA handle is still valid but because the VFS
1115 * requires that the eviction succeed we must drop
1116 * our locks and references to allow the eviction to
1117 * complete. The zfs_zget() may then be retried.
1119 * This unlikely case could be optimized by registering
1120 * a sops->drop_inode() callback. The callback would
1121 * need to detect the active SA hold thereby informing
1122 * the VFS that this inode should not be evicted.
1124 if (igrab(ZTOI(zp
)) == NULL
) {
1126 err
= SET_ERROR(ENOENT
);
1128 err
= SET_ERROR(EAGAIN
);
1134 mutex_exit(&zp
->z_lock
);
1135 sa_buf_rele(db
, NULL
);
1136 zfs_znode_hold_exit(zfsvfs
, zh
);
1138 if (err
== EAGAIN
) {
1139 /* inode might need this to finish evict */
1147 * Not found create new znode/vnode but only if file exists.
1149 * There is a small window where zfs_vget() could
1150 * find this object while a file create is still in
1151 * progress. This is checked for in zfs_znode_alloc()
1153 * if zfs_znode_alloc() fails it will drop the hold on the
1156 zp
= zfs_znode_alloc(zfsvfs
, db
, doi
.doi_data_block_size
,
1157 doi
.doi_bonus_type
, NULL
);
1159 err
= SET_ERROR(ENOENT
);
1163 zfs_znode_hold_exit(zfsvfs
, zh
);
1168 zfs_rezget(znode_t
*zp
)
1170 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1171 dmu_object_info_t doi
;
1173 uint64_t obj_num
= zp
->z_id
;
1176 sa_bulk_attr_t bulk
[11];
1180 uint64_t z_uid
, z_gid
;
1181 uint64_t atime
[2], mtime
[2], ctime
[2], btime
[2];
1182 uint64_t projid
= ZFS_DEFAULT_PROJID
;
1186 * skip ctldir, otherwise they will always get invalidated. This will
1187 * cause funny behaviour for the mounted snapdirs. Especially for
1188 * Linux >= 3.18, d_invalidate will detach the mountpoint and prevent
1189 * anyone automount it again as long as someone is still using the
1192 if (zp
->z_is_ctldir
)
1195 zh
= zfs_znode_hold_enter(zfsvfs
, obj_num
);
1197 mutex_enter(&zp
->z_acl_lock
);
1198 if (zp
->z_acl_cached
) {
1199 zfs_acl_free(zp
->z_acl_cached
);
1200 zp
->z_acl_cached
= NULL
;
1202 mutex_exit(&zp
->z_acl_lock
);
1204 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
1205 if (zp
->z_xattr_cached
) {
1206 nvlist_free(zp
->z_xattr_cached
);
1207 zp
->z_xattr_cached
= NULL
;
1209 rw_exit(&zp
->z_xattr_lock
);
1211 ASSERT(zp
->z_sa_hdl
== NULL
);
1212 err
= sa_buf_hold(zfsvfs
->z_os
, obj_num
, NULL
, &db
);
1214 zfs_znode_hold_exit(zfsvfs
, zh
);
1218 dmu_object_info_from_db(db
, &doi
);
1219 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
1220 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
1221 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1222 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
1223 sa_buf_rele(db
, NULL
);
1224 zfs_znode_hold_exit(zfsvfs
, zh
);
1225 return (SET_ERROR(EINVAL
));
1228 zfs_znode_sa_init(zfsvfs
, zp
, db
, doi
.doi_bonus_type
, NULL
);
1230 /* reload cached values */
1231 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zfsvfs
), NULL
,
1232 &gen
, sizeof (gen
));
1233 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
1234 &zp
->z_size
, sizeof (zp
->z_size
));
1235 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
), NULL
,
1236 &links
, sizeof (links
));
1237 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
1238 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
1239 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
1240 &z_uid
, sizeof (z_uid
));
1241 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
1242 &z_gid
, sizeof (z_gid
));
1243 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
1244 &mode
, sizeof (mode
));
1245 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
1247 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
1249 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
1251 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CRTIME(zfsvfs
), NULL
, &btime
, 16);
1253 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) {
1254 zfs_znode_dmu_fini(zp
);
1255 zfs_znode_hold_exit(zfsvfs
, zh
);
1256 return (SET_ERROR(EIO
));
1259 if (dmu_objset_projectquota_enabled(zfsvfs
->z_os
)) {
1260 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PROJID(zfsvfs
),
1262 if (err
!= 0 && err
!= ENOENT
) {
1263 zfs_znode_dmu_fini(zp
);
1264 zfs_znode_hold_exit(zfsvfs
, zh
);
1265 return (SET_ERROR(err
));
1269 zp
->z_projid
= projid
;
1270 zp
->z_mode
= ZTOI(zp
)->i_mode
= mode
;
1271 zfs_uid_write(ZTOI(zp
), z_uid
);
1272 zfs_gid_write(ZTOI(zp
), z_gid
);
1274 ZFS_TIME_DECODE(&ZTOI(zp
)->i_atime
, atime
);
1275 ZFS_TIME_DECODE(&ZTOI(zp
)->i_mtime
, mtime
);
1276 ZFS_TIME_DECODE(&ZTOI(zp
)->i_ctime
, ctime
);
1277 ZFS_TIME_DECODE(&zp
->z_btime
, btime
);
1279 if ((uint32_t)gen
!= ZTOI(zp
)->i_generation
) {
1280 zfs_znode_dmu_fini(zp
);
1281 zfs_znode_hold_exit(zfsvfs
, zh
);
1282 return (SET_ERROR(EIO
));
1285 set_nlink(ZTOI(zp
), (uint32_t)links
);
1286 zfs_set_inode_flags(zp
, ZTOI(zp
));
1288 zp
->z_blksz
= doi
.doi_data_block_size
;
1289 zp
->z_atime_dirty
= B_FALSE
;
1290 zfs_znode_update_vfs(zp
);
1293 * If the file has zero links, then it has been unlinked on the send
1294 * side and it must be in the received unlinked set.
1295 * We call zfs_znode_dmu_fini() now to prevent any accesses to the
1296 * stale data and to prevent automatic removal of the file in
1297 * zfs_zinactive(). The file will be removed either when it is removed
1298 * on the send side and the next incremental stream is received or
1299 * when the unlinked set gets processed.
1301 zp
->z_unlinked
= (ZTOI(zp
)->i_nlink
== 0);
1303 zfs_znode_dmu_fini(zp
);
1305 zfs_znode_hold_exit(zfsvfs
, zh
);
1311 zfs_znode_delete(znode_t
*zp
, dmu_tx_t
*tx
)
1313 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1314 objset_t
*os
= zfsvfs
->z_os
;
1315 uint64_t obj
= zp
->z_id
;
1316 uint64_t acl_obj
= zfs_external_acl(zp
);
1319 zh
= zfs_znode_hold_enter(zfsvfs
, obj
);
1321 VERIFY(!zp
->z_is_sa
);
1322 VERIFY(0 == dmu_object_free(os
, acl_obj
, tx
));
1324 VERIFY(0 == dmu_object_free(os
, obj
, tx
));
1325 zfs_znode_dmu_fini(zp
);
1326 zfs_znode_hold_exit(zfsvfs
, zh
);
1330 zfs_zinactive(znode_t
*zp
)
1332 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1333 uint64_t z_id
= zp
->z_id
;
1336 ASSERT(zp
->z_sa_hdl
);
1339 * Don't allow a zfs_zget() while were trying to release this znode.
1341 zh
= zfs_znode_hold_enter(zfsvfs
, z_id
);
1343 mutex_enter(&zp
->z_lock
);
1346 * If this was the last reference to a file with no links, remove
1347 * the file from the file system unless the file system is mounted
1348 * read-only. That can happen, for example, if the file system was
1349 * originally read-write, the file was opened, then unlinked and
1350 * the file system was made read-only before the file was finally
1351 * closed. The file will remain in the unlinked set.
1353 if (zp
->z_unlinked
) {
1354 ASSERT(!zfsvfs
->z_issnap
);
1355 if (!zfs_is_readonly(zfsvfs
) && !zfs_unlink_suspend_progress
) {
1356 mutex_exit(&zp
->z_lock
);
1357 zfs_znode_hold_exit(zfsvfs
, zh
);
1363 mutex_exit(&zp
->z_lock
);
1364 zfs_znode_dmu_fini(zp
);
1366 zfs_znode_hold_exit(zfsvfs
, zh
);
1369 #if defined(HAVE_INODE_TIMESPEC64_TIMES)
1370 #define zfs_compare_timespec timespec64_compare
1372 #define zfs_compare_timespec timespec_compare
1376 * Determine whether the znode's atime must be updated. The logic mostly
1377 * duplicates the Linux kernel's relatime_need_update() functionality.
1378 * This function is only called if the underlying filesystem actually has
1379 * atime updates enabled.
1382 zfs_relatime_need_update(const struct inode
*ip
)
1384 inode_timespec_t now
;
1388 * In relatime mode, only update the atime if the previous atime
1389 * is earlier than either the ctime or mtime or if at least a day
1390 * has passed since the last update of atime.
1392 if (zfs_compare_timespec(&ip
->i_mtime
, &ip
->i_atime
) >= 0)
1395 if (zfs_compare_timespec(&ip
->i_ctime
, &ip
->i_atime
) >= 0)
1398 if ((hrtime_t
)now
.tv_sec
- (hrtime_t
)ip
->i_atime
.tv_sec
>= 24*60*60)
1405 * Prepare to update znode time stamps.
1407 * IN: zp - znode requiring timestamp update
1408 * flag - ATTR_MTIME, ATTR_CTIME flags
1414 * Note: We don't update atime here, because we rely on Linux VFS to do
1418 zfs_tstamp_update_setup(znode_t
*zp
, uint_t flag
, uint64_t mtime
[2],
1421 inode_timespec_t now
;
1427 if (flag
& ATTR_MTIME
) {
1428 ZFS_TIME_ENCODE(&now
, mtime
);
1429 ZFS_TIME_DECODE(&(ZTOI(zp
)->i_mtime
), mtime
);
1430 if (ZTOZSB(zp
)->z_use_fuids
) {
1431 zp
->z_pflags
|= (ZFS_ARCHIVE
|
1436 if (flag
& ATTR_CTIME
) {
1437 ZFS_TIME_ENCODE(&now
, ctime
);
1438 ZFS_TIME_DECODE(&(ZTOI(zp
)->i_ctime
), ctime
);
1439 if (ZTOZSB(zp
)->z_use_fuids
)
1440 zp
->z_pflags
|= ZFS_ARCHIVE
;
1445 * Grow the block size for a file.
1447 * IN: zp - znode of file to free data in.
1448 * size - requested block size
1449 * tx - open transaction.
1451 * NOTE: this function assumes that the znode is write locked.
1454 zfs_grow_blocksize(znode_t
*zp
, uint64_t size
, dmu_tx_t
*tx
)
1459 if (size
<= zp
->z_blksz
)
1462 * If the file size is already greater than the current blocksize,
1463 * we will not grow. If there is more than one block in a file,
1464 * the blocksize cannot change.
1466 if (zp
->z_blksz
&& zp
->z_size
> zp
->z_blksz
)
1469 error
= dmu_object_set_blocksize(ZTOZSB(zp
)->z_os
, zp
->z_id
,
1472 if (error
== ENOTSUP
)
1476 /* What blocksize did we actually get? */
1477 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &zp
->z_blksz
, &dummy
);
1481 * Increase the file length
1483 * IN: zp - znode of file to free data in.
1484 * end - new end-of-file
1486 * RETURN: 0 on success, error code on failure
1489 zfs_extend(znode_t
*zp
, uint64_t end
)
1491 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1493 zfs_locked_range_t
*lr
;
1498 * We will change zp_size, lock the whole file.
1500 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, 0, UINT64_MAX
, RL_WRITER
);
1503 * Nothing to do if file already at desired length.
1505 if (end
<= zp
->z_size
) {
1506 zfs_rangelock_exit(lr
);
1509 tx
= dmu_tx_create(zfsvfs
->z_os
);
1510 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1511 zfs_sa_upgrade_txholds(tx
, zp
);
1512 if (end
> zp
->z_blksz
&&
1513 (!ISP2(zp
->z_blksz
) || zp
->z_blksz
< zfsvfs
->z_max_blksz
)) {
1515 * We are growing the file past the current block size.
1517 if (zp
->z_blksz
> ZTOZSB(zp
)->z_max_blksz
) {
1519 * File's blocksize is already larger than the
1520 * "recordsize" property. Only let it grow to
1521 * the next power of 2.
1523 ASSERT(!ISP2(zp
->z_blksz
));
1524 newblksz
= MIN(end
, 1 << highbit64(zp
->z_blksz
));
1526 newblksz
= MIN(end
, ZTOZSB(zp
)->z_max_blksz
);
1528 dmu_tx_hold_write(tx
, zp
->z_id
, 0, newblksz
);
1533 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1536 zfs_rangelock_exit(lr
);
1541 zfs_grow_blocksize(zp
, newblksz
, tx
);
1545 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(ZTOZSB(zp
)),
1546 &zp
->z_size
, sizeof (zp
->z_size
), tx
));
1548 zfs_rangelock_exit(lr
);
1556 * zfs_zero_partial_page - Modeled after update_pages() but
1557 * with different arguments and semantics for use by zfs_freesp().
1559 * Zeroes a piece of a single page cache entry for zp at offset
1560 * start and length len.
1562 * Caller must acquire a range lock on the file for the region
1563 * being zeroed in order that the ARC and page cache stay in sync.
1566 zfs_zero_partial_page(znode_t
*zp
, uint64_t start
, uint64_t len
)
1568 struct address_space
*mp
= ZTOI(zp
)->i_mapping
;
1573 ASSERT((start
& PAGE_MASK
) == ((start
+ len
- 1) & PAGE_MASK
));
1575 off
= start
& (PAGE_SIZE
- 1);
1578 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
1580 if (mapping_writably_mapped(mp
))
1581 flush_dcache_page(pp
);
1584 memset(pb
+ off
, 0, len
);
1587 if (mapping_writably_mapped(mp
))
1588 flush_dcache_page(pp
);
1590 mark_page_accessed(pp
);
1591 SetPageUptodate(pp
);
1599 * Free space in a file.
1601 * IN: zp - znode of file to free data in.
1602 * off - start of section to free.
1603 * len - length of section to free.
1605 * RETURN: 0 on success, error code on failure
1608 zfs_free_range(znode_t
*zp
, uint64_t off
, uint64_t len
)
1610 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1611 zfs_locked_range_t
*lr
;
1615 * Lock the range being freed.
1617 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, off
, len
, RL_WRITER
);
1620 * Nothing to do if file already at desired length.
1622 if (off
>= zp
->z_size
) {
1623 zfs_rangelock_exit(lr
);
1627 if (off
+ len
> zp
->z_size
)
1628 len
= zp
->z_size
- off
;
1630 error
= dmu_free_long_range(zfsvfs
->z_os
, zp
->z_id
, off
, len
);
1633 * Zero partial page cache entries. This must be done under a
1634 * range lock in order to keep the ARC and page cache in sync.
1636 if (zp
->z_is_mapped
) {
1637 loff_t first_page
, last_page
, page_len
;
1638 loff_t first_page_offset
, last_page_offset
;
1640 /* first possible full page in hole */
1641 first_page
= (off
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1642 /* last page of hole */
1643 last_page
= (off
+ len
) >> PAGE_SHIFT
;
1645 /* offset of first_page */
1646 first_page_offset
= first_page
<< PAGE_SHIFT
;
1647 /* offset of last_page */
1648 last_page_offset
= last_page
<< PAGE_SHIFT
;
1650 /* truncate whole pages */
1651 if (last_page_offset
> first_page_offset
) {
1652 truncate_inode_pages_range(ZTOI(zp
)->i_mapping
,
1653 first_page_offset
, last_page_offset
- 1);
1656 /* truncate sub-page ranges */
1657 if (first_page
> last_page
) {
1658 /* entire punched area within a single page */
1659 zfs_zero_partial_page(zp
, off
, len
);
1661 /* beginning of punched area at the end of a page */
1662 page_len
= first_page_offset
- off
;
1664 zfs_zero_partial_page(zp
, off
, page_len
);
1666 /* end of punched area at the beginning of a page */
1667 page_len
= off
+ len
- last_page_offset
;
1669 zfs_zero_partial_page(zp
, last_page_offset
,
1673 zfs_rangelock_exit(lr
);
1681 * IN: zp - znode of file to free data in.
1682 * end - new end-of-file.
1684 * RETURN: 0 on success, error code on failure
1687 zfs_trunc(znode_t
*zp
, uint64_t end
)
1689 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1691 zfs_locked_range_t
*lr
;
1693 sa_bulk_attr_t bulk
[2];
1697 * We will change zp_size, lock the whole file.
1699 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, 0, UINT64_MAX
, RL_WRITER
);
1702 * Nothing to do if file already at desired length.
1704 if (end
>= zp
->z_size
) {
1705 zfs_rangelock_exit(lr
);
1709 error
= dmu_free_long_range(zfsvfs
->z_os
, zp
->z_id
, end
,
1712 zfs_rangelock_exit(lr
);
1715 tx
= dmu_tx_create(zfsvfs
->z_os
);
1716 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1717 zfs_sa_upgrade_txholds(tx
, zp
);
1718 dmu_tx_mark_netfree(tx
);
1719 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1722 zfs_rangelock_exit(lr
);
1727 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
),
1728 NULL
, &zp
->z_size
, sizeof (zp
->z_size
));
1731 zp
->z_pflags
&= ~ZFS_SPARSE
;
1732 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
),
1733 NULL
, &zp
->z_pflags
, 8);
1735 VERIFY(sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
) == 0);
1738 zfs_rangelock_exit(lr
);
1744 * Free space in a file
1746 * IN: zp - znode of file to free data in.
1747 * off - start of range
1748 * len - end of range (0 => EOF)
1749 * flag - current file open mode flags.
1750 * log - TRUE if this action should be logged
1752 * RETURN: 0 on success, error code on failure
1755 zfs_freesp(znode_t
*zp
, uint64_t off
, uint64_t len
, int flag
, boolean_t log
)
1758 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1759 zilog_t
*zilog
= zfsvfs
->z_log
;
1761 uint64_t mtime
[2], ctime
[2];
1762 sa_bulk_attr_t bulk
[3];
1766 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MODE(zfsvfs
), &mode
,
1767 sizeof (mode
))) != 0)
1770 if (off
> zp
->z_size
) {
1771 error
= zfs_extend(zp
, off
+len
);
1772 if (error
== 0 && log
)
1778 error
= zfs_trunc(zp
, off
);
1780 if ((error
= zfs_free_range(zp
, off
, len
)) == 0 &&
1781 off
+ len
> zp
->z_size
)
1782 error
= zfs_extend(zp
, off
+len
);
1787 tx
= dmu_tx_create(zfsvfs
->z_os
);
1788 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1789 zfs_sa_upgrade_txholds(tx
, zp
);
1790 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1796 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, mtime
, 16);
1797 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, ctime
, 16);
1798 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
),
1799 NULL
, &zp
->z_pflags
, 8);
1800 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
1801 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1804 zfs_log_truncate(zilog
, tx
, TX_TRUNCATE
, zp
, off
, len
);
1808 zfs_znode_update_vfs(zp
);
1813 * Truncate the page cache - for file truncate operations, use
1814 * the purpose-built API for truncations. For punching operations,
1815 * the truncation is handled under a range lock in zfs_free_range.
1818 truncate_setsize(ZTOI(zp
), off
);
1823 zfs_create_fs(objset_t
*os
, cred_t
*cr
, nvlist_t
*zplprops
, dmu_tx_t
*tx
)
1825 struct super_block
*sb
;
1827 uint64_t moid
, obj
, sa_obj
, version
;
1828 uint64_t sense
= ZFS_CASE_SENSITIVE
;
1834 znode_t
*rootzp
= NULL
;
1837 zfs_acl_ids_t acl_ids
;
1840 * First attempt to create master node.
1843 * In an empty objset, there are no blocks to read and thus
1844 * there can be no i/o errors (which we assert below).
1846 moid
= MASTER_NODE_OBJ
;
1847 error
= zap_create_claim(os
, moid
, DMU_OT_MASTER_NODE
,
1848 DMU_OT_NONE
, 0, tx
);
1852 * Set starting attributes.
1854 version
= zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)));
1856 while ((elem
= nvlist_next_nvpair(zplprops
, elem
)) != NULL
) {
1857 /* For the moment we expect all zpl props to be uint64_ts */
1861 ASSERT(nvpair_type(elem
) == DATA_TYPE_UINT64
);
1862 VERIFY(nvpair_value_uint64(elem
, &val
) == 0);
1863 name
= nvpair_name(elem
);
1864 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_VERSION
)) == 0) {
1868 error
= zap_update(os
, moid
, name
, 8, 1, &val
, tx
);
1871 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_NORMALIZE
)) == 0)
1873 else if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_CASE
)) == 0)
1876 ASSERT(version
!= 0);
1877 error
= zap_update(os
, moid
, ZPL_VERSION_STR
, 8, 1, &version
, tx
);
1880 * Create zap object used for SA attribute registration
1883 if (version
>= ZPL_VERSION_SA
) {
1884 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1885 DMU_OT_NONE
, 0, tx
);
1886 error
= zap_add(os
, moid
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1892 * Create a delete queue.
1894 obj
= zap_create(os
, DMU_OT_UNLINKED_SET
, DMU_OT_NONE
, 0, tx
);
1896 error
= zap_add(os
, moid
, ZFS_UNLINKED_SET
, 8, 1, &obj
, tx
);
1900 * Create root znode. Create minimal znode/inode/zfsvfs/sb
1901 * to allow zfs_mknode to work.
1903 vattr
.va_mask
= ATTR_MODE
|ATTR_UID
|ATTR_GID
;
1904 vattr
.va_mode
= S_IFDIR
|0755;
1905 vattr
.va_uid
= crgetuid(cr
);
1906 vattr
.va_gid
= crgetgid(cr
);
1908 rootzp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
1909 rootzp
->z_unlinked
= B_FALSE
;
1910 rootzp
->z_atime_dirty
= B_FALSE
;
1911 rootzp
->z_is_sa
= USE_SA(version
, os
);
1912 rootzp
->z_pflags
= 0;
1914 zfsvfs
= kmem_zalloc(sizeof (zfsvfs_t
), KM_SLEEP
);
1916 zfsvfs
->z_parent
= zfsvfs
;
1917 zfsvfs
->z_version
= version
;
1918 zfsvfs
->z_use_fuids
= USE_FUIDS(version
, os
);
1919 zfsvfs
->z_use_sa
= USE_SA(version
, os
);
1920 zfsvfs
->z_norm
= norm
;
1922 sb
= kmem_zalloc(sizeof (struct super_block
), KM_SLEEP
);
1923 sb
->s_fs_info
= zfsvfs
;
1925 ZTOI(rootzp
)->i_sb
= sb
;
1927 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
1928 &zfsvfs
->z_attr_table
);
1933 * Fold case on file systems that are always or sometimes case
1936 if (sense
== ZFS_CASE_INSENSITIVE
|| sense
== ZFS_CASE_MIXED
)
1937 zfsvfs
->z_norm
|= U8_TEXTPREP_TOUPPER
;
1939 mutex_init(&zfsvfs
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1940 list_create(&zfsvfs
->z_all_znodes
, sizeof (znode_t
),
1941 offsetof(znode_t
, z_link_node
));
1943 size
= MIN(1 << (highbit64(zfs_object_mutex_size
)-1), ZFS_OBJ_MTX_MAX
);
1944 zfsvfs
->z_hold_size
= size
;
1945 zfsvfs
->z_hold_trees
= vmem_zalloc(sizeof (avl_tree_t
) * size
,
1947 zfsvfs
->z_hold_locks
= vmem_zalloc(sizeof (kmutex_t
) * size
, KM_SLEEP
);
1948 for (i
= 0; i
!= size
; i
++) {
1949 avl_create(&zfsvfs
->z_hold_trees
[i
], zfs_znode_hold_compare
,
1950 sizeof (znode_hold_t
), offsetof(znode_hold_t
, zh_node
));
1951 mutex_init(&zfsvfs
->z_hold_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
1954 VERIFY(0 == zfs_acl_ids_create(rootzp
, IS_ROOT_NODE
, &vattr
,
1955 cr
, NULL
, &acl_ids
));
1956 zfs_mknode(rootzp
, &vattr
, tx
, cr
, IS_ROOT_NODE
, &zp
, &acl_ids
);
1957 ASSERT3P(zp
, ==, rootzp
);
1958 error
= zap_add(os
, moid
, ZFS_ROOT_OBJ
, 8, 1, &rootzp
->z_id
, tx
);
1960 zfs_acl_ids_free(&acl_ids
);
1962 atomic_set(&ZTOI(rootzp
)->i_count
, 0);
1963 sa_handle_destroy(rootzp
->z_sa_hdl
);
1964 kmem_cache_free(znode_cache
, rootzp
);
1966 for (i
= 0; i
!= size
; i
++) {
1967 avl_destroy(&zfsvfs
->z_hold_trees
[i
]);
1968 mutex_destroy(&zfsvfs
->z_hold_locks
[i
]);
1971 mutex_destroy(&zfsvfs
->z_znodes_lock
);
1973 vmem_free(zfsvfs
->z_hold_trees
, sizeof (avl_tree_t
) * size
);
1974 vmem_free(zfsvfs
->z_hold_locks
, sizeof (kmutex_t
) * size
);
1975 kmem_free(sb
, sizeof (struct super_block
));
1976 kmem_free(zfsvfs
, sizeof (zfsvfs_t
));
1978 #endif /* _KERNEL */
1981 zfs_sa_setup(objset_t
*osp
, sa_attr_type_t
**sa_table
)
1983 uint64_t sa_obj
= 0;
1986 error
= zap_lookup(osp
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1987 if (error
!= 0 && error
!= ENOENT
)
1990 error
= sa_setup(osp
, sa_obj
, zfs_attr_table
, ZPL_END
, sa_table
);
1995 zfs_grab_sa_handle(objset_t
*osp
, uint64_t obj
, sa_handle_t
**hdlp
,
1996 dmu_buf_t
**db
, void *tag
)
1998 dmu_object_info_t doi
;
2001 if ((error
= sa_buf_hold(osp
, obj
, tag
, db
)) != 0)
2004 dmu_object_info_from_db(*db
, &doi
);
2005 if ((doi
.doi_bonus_type
!= DMU_OT_SA
&&
2006 doi
.doi_bonus_type
!= DMU_OT_ZNODE
) ||
2007 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
2008 doi
.doi_bonus_size
< sizeof (znode_phys_t
))) {
2009 sa_buf_rele(*db
, tag
);
2010 return (SET_ERROR(ENOTSUP
));
2013 error
= sa_handle_get(osp
, obj
, NULL
, SA_HDL_PRIVATE
, hdlp
);
2015 sa_buf_rele(*db
, tag
);
2023 zfs_release_sa_handle(sa_handle_t
*hdl
, dmu_buf_t
*db
, void *tag
)
2025 sa_handle_destroy(hdl
);
2026 sa_buf_rele(db
, tag
);
2030 * Given an object number, return its parent object number and whether
2031 * or not the object is an extended attribute directory.
2034 zfs_obj_to_pobj(objset_t
*osp
, sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
2035 uint64_t *pobjp
, int *is_xattrdir
)
2040 uint64_t parent_mode
;
2041 sa_bulk_attr_t bulk
[3];
2042 sa_handle_t
*sa_hdl
;
2047 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_PARENT
], NULL
,
2048 &parent
, sizeof (parent
));
2049 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_FLAGS
], NULL
,
2050 &pflags
, sizeof (pflags
));
2051 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
2052 &mode
, sizeof (mode
));
2054 if ((error
= sa_bulk_lookup(hdl
, bulk
, count
)) != 0)
2058 * When a link is removed its parent pointer is not changed and will
2059 * be invalid. There are two cases where a link is removed but the
2060 * file stays around, when it goes to the delete queue and when there
2061 * are additional links.
2063 error
= zfs_grab_sa_handle(osp
, parent
, &sa_hdl
, &sa_db
, FTAG
);
2067 error
= sa_lookup(sa_hdl
, ZPL_MODE
, &parent_mode
, sizeof (parent_mode
));
2068 zfs_release_sa_handle(sa_hdl
, sa_db
, FTAG
);
2072 *is_xattrdir
= ((pflags
& ZFS_XATTR
) != 0) && S_ISDIR(mode
);
2075 * Extended attributes can be applied to files, directories, etc.
2076 * Otherwise the parent must be a directory.
2078 if (!*is_xattrdir
&& !S_ISDIR(parent_mode
))
2079 return (SET_ERROR(EINVAL
));
2087 * Given an object number, return some zpl level statistics
2090 zfs_obj_to_stats_impl(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
2093 sa_bulk_attr_t bulk
[4];
2096 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
2097 &sb
->zs_mode
, sizeof (sb
->zs_mode
));
2098 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_GEN
], NULL
,
2099 &sb
->zs_gen
, sizeof (sb
->zs_gen
));
2100 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_LINKS
], NULL
,
2101 &sb
->zs_links
, sizeof (sb
->zs_links
));
2102 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_CTIME
], NULL
,
2103 &sb
->zs_ctime
, sizeof (sb
->zs_ctime
));
2105 return (sa_bulk_lookup(hdl
, bulk
, count
));
2109 zfs_obj_to_path_impl(objset_t
*osp
, uint64_t obj
, sa_handle_t
*hdl
,
2110 sa_attr_type_t
*sa_table
, char *buf
, int len
)
2112 sa_handle_t
*sa_hdl
;
2113 sa_handle_t
*prevhdl
= NULL
;
2114 dmu_buf_t
*prevdb
= NULL
;
2115 dmu_buf_t
*sa_db
= NULL
;
2116 char *path
= buf
+ len
- 1;
2122 uint64_t deleteq_obj
;
2123 VERIFY0(zap_lookup(osp
, MASTER_NODE_OBJ
,
2124 ZFS_UNLINKED_SET
, sizeof (uint64_t), 1, &deleteq_obj
));
2125 error
= zap_lookup_int(osp
, deleteq_obj
, obj
);
2128 } else if (error
!= ENOENT
) {
2135 char component
[MAXNAMELEN
+ 2];
2137 int is_xattrdir
= 0;
2140 ASSERT(prevhdl
!= NULL
);
2141 zfs_release_sa_handle(prevhdl
, prevdb
, FTAG
);
2144 if ((error
= zfs_obj_to_pobj(osp
, sa_hdl
, sa_table
, &pobj
,
2145 &is_xattrdir
)) != 0)
2156 strcpy(component
+ 1, "<xattrdir>");
2158 error
= zap_value_search(osp
, pobj
, obj
,
2159 ZFS_DIRENT_OBJ(-1ULL), component
+ 1);
2164 complen
= strlen(component
);
2166 ASSERT(path
>= buf
);
2167 memcpy(path
, component
, complen
);
2170 if (sa_hdl
!= hdl
) {
2174 error
= zfs_grab_sa_handle(osp
, obj
, &sa_hdl
, &sa_db
, FTAG
);
2182 if (sa_hdl
!= NULL
&& sa_hdl
!= hdl
) {
2183 ASSERT(sa_db
!= NULL
);
2184 zfs_release_sa_handle(sa_hdl
, sa_db
, FTAG
);
2188 (void) memmove(buf
, path
, buf
+ len
- path
);
2194 zfs_obj_to_path(objset_t
*osp
, uint64_t obj
, char *buf
, int len
)
2196 sa_attr_type_t
*sa_table
;
2201 error
= zfs_sa_setup(osp
, &sa_table
);
2205 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
, FTAG
);
2209 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
2211 zfs_release_sa_handle(hdl
, db
, FTAG
);
2216 zfs_obj_to_stats(objset_t
*osp
, uint64_t obj
, zfs_stat_t
*sb
,
2219 char *path
= buf
+ len
- 1;
2220 sa_attr_type_t
*sa_table
;
2227 error
= zfs_sa_setup(osp
, &sa_table
);
2231 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
, FTAG
);
2235 error
= zfs_obj_to_stats_impl(hdl
, sa_table
, sb
);
2237 zfs_release_sa_handle(hdl
, db
, FTAG
);
2241 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
2243 zfs_release_sa_handle(hdl
, db
, FTAG
);
2247 #if defined(_KERNEL)
2248 EXPORT_SYMBOL(zfs_create_fs
);
2249 EXPORT_SYMBOL(zfs_obj_to_path
);
2252 module_param(zfs_object_mutex_size
, uint
, 0644);
2253 MODULE_PARM_DESC(zfs_object_mutex_size
, "Size of znode hold array");
2254 module_param(zfs_unlink_suspend_progress
, int, 0644);
2255 MODULE_PARM_DESC(zfs_unlink_suspend_progress
, "Set to prevent async unlinks "
2256 "(debug - leaks space into the unlinked set)");