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, 2014 by Delphix. All rights reserved.
26 /* Portions Copyright 2007 Jeremy Teo */
29 #include <sys/types.h>
30 #include <sys/param.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>
40 #include <sys/vfs_opreg.h>
41 #include <sys/vnode.h>
44 #include <sys/errno.h>
45 #include <sys/unistd.h>
47 #include <sys/atomic.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>
55 #include <sys/zfs_vnops.h>
56 #include <sys/zfs_ctldir.h>
57 #include <sys/dnode.h>
58 #include <sys/fs/zfs.h>
59 #include <sys/kidmap.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/dmu_tx.h>
66 #include <sys/refcount.h>
69 #include <sys/zfs_znode.h>
71 #include <sys/zfs_sa.h>
72 #include <sys/zfs_stat.h>
75 #include "zfs_comutil.h"
78 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
79 * turned on when DEBUG is also defined.
86 #define ZNODE_STAT_ADD(stat) ((stat)++)
88 #define ZNODE_STAT_ADD(stat) /* nothing */
89 #endif /* ZNODE_STATS */
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.
98 static kmem_cache_t
*znode_cache
= NULL
;
99 static kmem_cache_t
*znode_hold_cache
= NULL
;
100 unsigned int zfs_object_mutex_size
= ZFS_OBJ_MTX_SZ
;
104 zfs_znode_cache_constructor(void *buf
, void *arg
, int kmflags
)
108 inode_init_once(ZTOI(zp
));
109 list_link_init(&zp
->z_link_node
);
111 mutex_init(&zp
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
112 rw_init(&zp
->z_parent_lock
, NULL
, RW_DEFAULT
, NULL
);
113 rw_init(&zp
->z_name_lock
, NULL
, RW_NOLOCKDEP
, NULL
);
114 mutex_init(&zp
->z_acl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
115 rw_init(&zp
->z_xattr_lock
, NULL
, RW_DEFAULT
, NULL
);
117 zfs_rlock_init(&zp
->z_range_lock
);
119 zp
->z_dirlocks
= NULL
;
120 zp
->z_acl_cached
= NULL
;
121 zp
->z_xattr_cached
= NULL
;
122 zp
->z_xattr_parent
= 0;
129 zfs_znode_cache_destructor(void *buf
, void *arg
)
133 ASSERT(!list_link_active(&zp
->z_link_node
));
134 mutex_destroy(&zp
->z_lock
);
135 rw_destroy(&zp
->z_parent_lock
);
136 rw_destroy(&zp
->z_name_lock
);
137 mutex_destroy(&zp
->z_acl_lock
);
138 rw_destroy(&zp
->z_xattr_lock
);
139 zfs_rlock_destroy(&zp
->z_range_lock
);
141 ASSERT(zp
->z_dirlocks
== NULL
);
142 ASSERT(zp
->z_acl_cached
== NULL
);
143 ASSERT(zp
->z_xattr_cached
== NULL
);
147 zfs_znode_hold_cache_constructor(void *buf
, void *arg
, int kmflags
)
149 znode_hold_t
*zh
= buf
;
151 mutex_init(&zh
->zh_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
152 refcount_create(&zh
->zh_refcount
);
153 zh
->zh_obj
= ZFS_NO_OBJECT
;
159 zfs_znode_hold_cache_destructor(void *buf
, void *arg
)
161 znode_hold_t
*zh
= buf
;
163 mutex_destroy(&zh
->zh_lock
);
164 refcount_destroy(&zh
->zh_refcount
);
171 * Initialize zcache. The KMC_SLAB hint is used in order that it be
172 * backed by kmalloc() when on the Linux slab in order that any
173 * wait_on_bit() operations on the related inode operate properly.
175 ASSERT(znode_cache
== NULL
);
176 znode_cache
= kmem_cache_create("zfs_znode_cache",
177 sizeof (znode_t
), 0, zfs_znode_cache_constructor
,
178 zfs_znode_cache_destructor
, NULL
, NULL
, NULL
, KMC_SLAB
);
180 ASSERT(znode_hold_cache
== NULL
);
181 znode_hold_cache
= kmem_cache_create("zfs_znode_hold_cache",
182 sizeof (znode_hold_t
), 0, zfs_znode_hold_cache_constructor
,
183 zfs_znode_hold_cache_destructor
, NULL
, NULL
, NULL
, 0);
193 kmem_cache_destroy(znode_cache
);
196 if (znode_hold_cache
)
197 kmem_cache_destroy(znode_hold_cache
);
198 znode_hold_cache
= NULL
;
202 * The zfs_znode_hold_enter() / zfs_znode_hold_exit() functions are used to
203 * serialize access to a znode and its SA buffer while the object is being
204 * created or destroyed. This kind of locking would normally reside in the
205 * znode itself but in this case that's impossible because the znode and SA
206 * buffer may not yet exist. Therefore the locking is handled externally
207 * with an array of mutexs and AVLs trees which contain per-object locks.
209 * In zfs_znode_hold_enter() a per-object lock is created as needed, inserted
210 * in to the correct AVL tree and finally the per-object lock is held. In
211 * zfs_znode_hold_exit() the process is reversed. The per-object lock is
212 * released, removed from the AVL tree and destroyed if there are no waiters.
214 * This scheme has two important properties:
216 * 1) No memory allocations are performed while holding one of the z_hold_locks.
217 * This ensures evict(), which can be called from direct memory reclaim, will
218 * never block waiting on a z_hold_locks which just happens to have hashed
221 * 2) All locks used to serialize access to an object are per-object and never
222 * shared. This minimizes lock contention without creating a large number
223 * of dedicated locks.
225 * On the downside it does require znode_lock_t structures to be frequently
226 * allocated and freed. However, because these are backed by a kmem cache
227 * and very short lived this cost is minimal.
230 zfs_znode_hold_compare(const void *a
, const void *b
)
232 const znode_hold_t
*zh_a
= (const znode_hold_t
*)a
;
233 const znode_hold_t
*zh_b
= (const znode_hold_t
*)b
;
235 return (AVL_CMP(zh_a
->zh_obj
, zh_b
->zh_obj
));
239 zfs_znode_held(zfsvfs_t
*zfsvfs
, uint64_t obj
)
241 znode_hold_t
*zh
, search
;
242 int i
= ZFS_OBJ_HASH(zfsvfs
, obj
);
247 mutex_enter(&zfsvfs
->z_hold_locks
[i
]);
248 zh
= avl_find(&zfsvfs
->z_hold_trees
[i
], &search
, NULL
);
249 held
= (zh
&& MUTEX_HELD(&zh
->zh_lock
)) ? B_TRUE
: B_FALSE
;
250 mutex_exit(&zfsvfs
->z_hold_locks
[i
]);
255 static znode_hold_t
*
256 zfs_znode_hold_enter(zfsvfs_t
*zfsvfs
, uint64_t obj
)
258 znode_hold_t
*zh
, *zh_new
, search
;
259 int i
= ZFS_OBJ_HASH(zfsvfs
, obj
);
260 boolean_t found
= B_FALSE
;
262 zh_new
= kmem_cache_alloc(znode_hold_cache
, KM_SLEEP
);
263 zh_new
->zh_obj
= obj
;
266 mutex_enter(&zfsvfs
->z_hold_locks
[i
]);
267 zh
= avl_find(&zfsvfs
->z_hold_trees
[i
], &search
, NULL
);
268 if (likely(zh
== NULL
)) {
270 avl_add(&zfsvfs
->z_hold_trees
[i
], zh
);
272 ASSERT3U(zh
->zh_obj
, ==, obj
);
275 refcount_add(&zh
->zh_refcount
, NULL
);
276 mutex_exit(&zfsvfs
->z_hold_locks
[i
]);
279 kmem_cache_free(znode_hold_cache
, zh_new
);
281 ASSERT(MUTEX_NOT_HELD(&zh
->zh_lock
));
282 ASSERT3S(refcount_count(&zh
->zh_refcount
), >, 0);
283 mutex_enter(&zh
->zh_lock
);
289 zfs_znode_hold_exit(zfsvfs_t
*zfsvfs
, znode_hold_t
*zh
)
291 int i
= ZFS_OBJ_HASH(zfsvfs
, zh
->zh_obj
);
292 boolean_t remove
= B_FALSE
;
294 ASSERT(zfs_znode_held(zfsvfs
, zh
->zh_obj
));
295 ASSERT3S(refcount_count(&zh
->zh_refcount
), >, 0);
296 mutex_exit(&zh
->zh_lock
);
298 mutex_enter(&zfsvfs
->z_hold_locks
[i
]);
299 if (refcount_remove(&zh
->zh_refcount
, NULL
) == 0) {
300 avl_remove(&zfsvfs
->z_hold_trees
[i
], zh
);
303 mutex_exit(&zfsvfs
->z_hold_locks
[i
]);
305 if (remove
== B_TRUE
)
306 kmem_cache_free(znode_hold_cache
, zh
);
310 zfs_create_share_dir(zfsvfs_t
*zfsvfs
, dmu_tx_t
*tx
)
312 #ifdef HAVE_SMB_SHARE
313 zfs_acl_ids_t acl_ids
;
320 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
321 vattr
.va_mode
= S_IFDIR
| 0555;
322 vattr
.va_uid
= crgetuid(kcred
);
323 vattr
.va_gid
= crgetgid(kcred
);
325 sharezp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
326 sharezp
->z_moved
= 0;
327 sharezp
->z_unlinked
= 0;
328 sharezp
->z_atime_dirty
= 0;
329 sharezp
->z_zfsvfs
= zfsvfs
;
330 sharezp
->z_is_sa
= zfsvfs
->z_use_sa
;
331 sharezp
->z_pflags
= 0;
337 VERIFY(0 == zfs_acl_ids_create(sharezp
, IS_ROOT_NODE
, &vattr
,
338 kcred
, NULL
, &acl_ids
));
339 zfs_mknode(sharezp
, &vattr
, tx
, kcred
, IS_ROOT_NODE
, &zp
, &acl_ids
);
340 ASSERT3P(zp
, ==, sharezp
);
341 ASSERT(!vn_in_dnlc(ZTOV(sharezp
))); /* not valid to move */
342 POINTER_INVALIDATE(&sharezp
->z_zfsvfs
);
343 error
= zap_add(zfsvfs
->z_os
, MASTER_NODE_OBJ
,
344 ZFS_SHARES_DIR
, 8, 1, &sharezp
->z_id
, tx
);
345 zfsvfs
->z_shares_dir
= sharezp
->z_id
;
347 zfs_acl_ids_free(&acl_ids
);
348 // ZTOV(sharezp)->v_count = 0;
349 sa_handle_destroy(sharezp
->z_sa_hdl
);
350 kmem_cache_free(znode_cache
, sharezp
);
355 #endif /* HAVE_SMB_SHARE */
359 zfs_znode_sa_init(zfsvfs_t
*zfsvfs
, znode_t
*zp
,
360 dmu_buf_t
*db
, dmu_object_type_t obj_type
, sa_handle_t
*sa_hdl
)
362 ASSERT(zfs_znode_held(zfsvfs
, zp
->z_id
));
364 mutex_enter(&zp
->z_lock
);
366 ASSERT(zp
->z_sa_hdl
== NULL
);
367 ASSERT(zp
->z_acl_cached
== NULL
);
368 if (sa_hdl
== NULL
) {
369 VERIFY(0 == sa_handle_get_from_db(zfsvfs
->z_os
, db
, zp
,
370 SA_HDL_SHARED
, &zp
->z_sa_hdl
));
372 zp
->z_sa_hdl
= sa_hdl
;
373 sa_set_userp(sa_hdl
, zp
);
376 zp
->z_is_sa
= (obj_type
== DMU_OT_SA
) ? B_TRUE
: B_FALSE
;
378 mutex_exit(&zp
->z_lock
);
382 zfs_znode_dmu_fini(znode_t
*zp
)
384 ASSERT(zfs_znode_held(ZTOZSB(zp
), zp
->z_id
) || zp
->z_unlinked
||
385 RW_WRITE_HELD(&ZTOZSB(zp
)->z_teardown_inactive_lock
));
387 sa_handle_destroy(zp
->z_sa_hdl
);
392 * Called by new_inode() to allocate a new inode.
395 zfs_inode_alloc(struct super_block
*sb
, struct inode
**ip
)
399 zp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
406 * Called in multiple places when an inode should be destroyed.
409 zfs_inode_destroy(struct inode
*ip
)
411 znode_t
*zp
= ITOZ(ip
);
412 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
414 mutex_enter(&zfsvfs
->z_znodes_lock
);
415 if (list_link_active(&zp
->z_link_node
)) {
416 list_remove(&zfsvfs
->z_all_znodes
, zp
);
417 zfsvfs
->z_nr_znodes
--;
419 mutex_exit(&zfsvfs
->z_znodes_lock
);
421 if (zp
->z_acl_cached
) {
422 zfs_acl_free(zp
->z_acl_cached
);
423 zp
->z_acl_cached
= NULL
;
426 if (zp
->z_xattr_cached
) {
427 nvlist_free(zp
->z_xattr_cached
);
428 zp
->z_xattr_cached
= NULL
;
431 kmem_cache_free(znode_cache
, zp
);
435 zfs_inode_set_ops(zfsvfs_t
*zfsvfs
, struct inode
*ip
)
439 switch (ip
->i_mode
& S_IFMT
) {
441 ip
->i_op
= &zpl_inode_operations
;
442 ip
->i_fop
= &zpl_file_operations
;
443 ip
->i_mapping
->a_ops
= &zpl_address_space_operations
;
447 ip
->i_op
= &zpl_dir_inode_operations
;
448 ip
->i_fop
= &zpl_dir_file_operations
;
449 ITOZ(ip
)->z_zn_prefetch
= B_TRUE
;
453 ip
->i_op
= &zpl_symlink_inode_operations
;
457 * rdev is only stored in a SA only for device files.
461 (void) sa_lookup(ITOZ(ip
)->z_sa_hdl
, SA_ZPL_RDEV(zfsvfs
), &rdev
,
466 init_special_inode(ip
, ip
->i_mode
, rdev
);
467 ip
->i_op
= &zpl_special_inode_operations
;
471 zfs_panic_recover("inode %llu has invalid mode: 0x%x\n",
472 (u_longlong_t
)ip
->i_ino
, ip
->i_mode
);
474 /* Assume the inode is a file and attempt to continue */
475 ip
->i_mode
= S_IFREG
| 0644;
476 ip
->i_op
= &zpl_inode_operations
;
477 ip
->i_fop
= &zpl_file_operations
;
478 ip
->i_mapping
->a_ops
= &zpl_address_space_operations
;
484 zfs_set_inode_flags(znode_t
*zp
, struct inode
*ip
)
487 * Linux and Solaris have different sets of file attributes, so we
488 * restrict this conversion to the intersection of the two.
490 #ifdef HAVE_INODE_SET_FLAGS
491 unsigned int flags
= 0;
492 if (zp
->z_pflags
& ZFS_IMMUTABLE
)
493 flags
|= S_IMMUTABLE
;
494 if (zp
->z_pflags
& ZFS_APPENDONLY
)
497 inode_set_flags(ip
, flags
, S_IMMUTABLE
|S_APPEND
);
499 if (zp
->z_pflags
& ZFS_IMMUTABLE
)
500 ip
->i_flags
|= S_IMMUTABLE
;
502 ip
->i_flags
&= ~S_IMMUTABLE
;
504 if (zp
->z_pflags
& ZFS_APPENDONLY
)
505 ip
->i_flags
|= S_APPEND
;
507 ip
->i_flags
&= ~S_APPEND
;
512 * Update the embedded inode given the znode. We should work toward
513 * eliminating this function as soon as possible by removing values
514 * which are duplicated between the znode and inode. If the generic
515 * inode has the correct field it should be used, and the ZFS code
516 * updated to access the inode. This can be done incrementally.
519 zfs_inode_update(znode_t
*zp
)
524 u_longlong_t i_blocks
;
530 /* Skip .zfs control nodes which do not exist on disk. */
531 if (zfsctl_is_node(ip
))
534 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &blksize
, &i_blocks
);
536 spin_lock(&ip
->i_lock
);
537 ip
->i_blocks
= i_blocks
;
538 i_size_write(ip
, zp
->z_size
);
539 spin_unlock(&ip
->i_lock
);
544 * Construct a znode+inode and initialize.
546 * This does not do a call to dmu_set_user() that is
547 * up to the caller to do, in case you don't want to
551 zfs_znode_alloc(zfsvfs_t
*zfsvfs
, dmu_buf_t
*db
, int blksz
,
552 dmu_object_type_t obj_type
, uint64_t obj
, sa_handle_t
*hdl
)
560 uint64_t z_uid
, z_gid
;
561 uint64_t atime
[2], mtime
[2], ctime
[2];
562 uint64_t projid
= ZFS_DEFAULT_PROJID
;
563 sa_bulk_attr_t bulk
[11];
566 ASSERT(zfsvfs
!= NULL
);
568 ip
= new_inode(zfsvfs
->z_sb
);
573 ASSERT(zp
->z_dirlocks
== NULL
);
574 ASSERT3P(zp
->z_acl_cached
, ==, NULL
);
575 ASSERT3P(zp
->z_xattr_cached
, ==, NULL
);
579 zp
->z_atime_dirty
= 0;
581 zp
->z_id
= db
->db_object
;
583 zp
->z_seq
= 0x7A4653;
585 zp
->z_is_mapped
= B_FALSE
;
586 zp
->z_is_ctldir
= B_FALSE
;
587 zp
->z_is_stale
= B_FALSE
;
588 zp
->z_range_lock
.zr_size
= &zp
->z_size
;
589 zp
->z_range_lock
.zr_blksz
= &zp
->z_blksz
;
590 zp
->z_range_lock
.zr_max_blksz
= &ZTOZSB(zp
)->z_max_blksz
;
592 zfs_znode_sa_init(zfsvfs
, zp
, db
, obj_type
, hdl
);
594 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
, &mode
, 8);
595 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zfsvfs
), NULL
, &tmp_gen
, 8);
596 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
598 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
), NULL
, &links
, 8);
599 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
601 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PARENT(zfsvfs
), NULL
,
603 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
, &z_uid
, 8);
604 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
, &z_gid
, 8);
605 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
606 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
607 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
609 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
) != 0 || tmp_gen
== 0 ||
610 (dmu_objset_projectquota_enabled(zfsvfs
->z_os
) &&
611 (zp
->z_pflags
& ZFS_PROJID
) &&
612 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PROJID(zfsvfs
), &projid
, 8) != 0)) {
614 sa_handle_destroy(zp
->z_sa_hdl
);
619 zp
->z_projid
= projid
;
620 zp
->z_mode
= ip
->i_mode
= mode
;
621 ip
->i_generation
= (uint32_t)tmp_gen
;
622 ip
->i_blkbits
= SPA_MINBLOCKSHIFT
;
623 set_nlink(ip
, (uint32_t)links
);
624 zfs_uid_write(ip
, z_uid
);
625 zfs_gid_write(ip
, z_gid
);
626 zfs_set_inode_flags(zp
, ip
);
628 /* Cache the xattr parent id */
629 if (zp
->z_pflags
& ZFS_XATTR
)
630 zp
->z_xattr_parent
= parent
;
632 ZFS_TIME_DECODE(&ip
->i_atime
, atime
);
633 ZFS_TIME_DECODE(&ip
->i_mtime
, mtime
);
634 ZFS_TIME_DECODE(&ip
->i_ctime
, ctime
);
637 zfs_inode_update(zp
);
638 zfs_inode_set_ops(zfsvfs
, ip
);
641 * The only way insert_inode_locked() can fail is if the ip->i_ino
642 * number is already hashed for this super block. This can never
643 * happen because the inode numbers map 1:1 with the object numbers.
645 * The one exception is rolling back a mounted file system, but in
646 * this case all the active inode are unhashed during the rollback.
648 VERIFY3S(insert_inode_locked(ip
), ==, 0);
650 mutex_enter(&zfsvfs
->z_znodes_lock
);
651 list_insert_tail(&zfsvfs
->z_all_znodes
, zp
);
652 zfsvfs
->z_nr_znodes
++;
654 mutex_exit(&zfsvfs
->z_znodes_lock
);
656 unlock_new_inode(ip
);
665 * Safely mark an inode dirty. Inodes which are part of a read-only
666 * file system or snapshot may not be dirtied.
669 zfs_mark_inode_dirty(struct inode
*ip
)
671 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
673 if (zfs_is_readonly(zfsvfs
) || dmu_objset_is_snapshot(zfsvfs
->z_os
))
676 mark_inode_dirty(ip
);
679 static uint64_t empty_xattr
;
680 static uint64_t pad
[4];
681 static zfs_acl_phys_t acl_phys
;
683 * Create a new DMU object to hold a zfs znode.
685 * IN: dzp - parent directory for new znode
686 * vap - file attributes for new znode
687 * tx - dmu transaction id for zap operations
688 * cr - credentials of caller
690 * IS_ROOT_NODE - new object will be root
691 * IS_XATTR - new object is an attribute
692 * bonuslen - length of bonus buffer
693 * setaclp - File/Dir initial ACL
694 * fuidp - Tracks fuid allocation.
696 * OUT: zpp - allocated znode
700 zfs_mknode(znode_t
*dzp
, vattr_t
*vap
, dmu_tx_t
*tx
, cred_t
*cr
,
701 uint_t flag
, znode_t
**zpp
, zfs_acl_ids_t
*acl_ids
)
703 uint64_t crtime
[2], atime
[2], mtime
[2], ctime
[2];
704 uint64_t mode
, size
, links
, parent
, pflags
;
705 uint64_t projid
= ZFS_DEFAULT_PROJID
;
707 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
714 dmu_object_type_t obj_type
;
715 sa_bulk_attr_t
*sa_attrs
;
717 zfs_acl_locator_cb_t locate
= { 0 };
720 if (zfsvfs
->z_replay
) {
721 obj
= vap
->va_nodeid
;
722 now
= vap
->va_ctime
; /* see zfs_replay_create() */
723 gen
= vap
->va_nblocks
; /* ditto */
724 dnodesize
= vap
->va_fsid
; /* ditto */
728 gen
= dmu_tx_get_txg(tx
);
729 dnodesize
= dmu_objset_dnodesize(zfsvfs
->z_os
);
733 dnodesize
= DNODE_MIN_SIZE
;
735 obj_type
= zfsvfs
->z_use_sa
? DMU_OT_SA
: DMU_OT_ZNODE
;
737 bonuslen
= (obj_type
== DMU_OT_SA
) ?
738 DN_BONUS_SIZE(dnodesize
) : ZFS_OLD_ZNODE_PHYS_SIZE
;
741 * Create a new DMU object.
744 * There's currently no mechanism for pre-reading the blocks that will
745 * be needed to allocate a new object, so we accept the small chance
746 * that there will be an i/o error and we will fail one of the
749 if (S_ISDIR(vap
->va_mode
)) {
750 if (zfsvfs
->z_replay
) {
751 VERIFY0(zap_create_claim_norm_dnsize(zfsvfs
->z_os
, obj
,
752 zfsvfs
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
753 obj_type
, bonuslen
, dnodesize
, tx
));
755 obj
= zap_create_norm_dnsize(zfsvfs
->z_os
,
756 zfsvfs
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
757 obj_type
, bonuslen
, dnodesize
, tx
);
760 if (zfsvfs
->z_replay
) {
761 VERIFY0(dmu_object_claim_dnsize(zfsvfs
->z_os
, obj
,
762 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
763 obj_type
, bonuslen
, dnodesize
, tx
));
765 obj
= dmu_object_alloc_dnsize(zfsvfs
->z_os
,
766 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
767 obj_type
, bonuslen
, dnodesize
, tx
);
771 zh
= zfs_znode_hold_enter(zfsvfs
, obj
);
772 VERIFY0(sa_buf_hold(zfsvfs
->z_os
, obj
, NULL
, &db
));
775 * If this is the root, fix up the half-initialized parent pointer
776 * to reference the just-allocated physical data area.
778 if (flag
& IS_ROOT_NODE
) {
783 * If parent is an xattr, so am I.
785 if (dzp
->z_pflags
& ZFS_XATTR
) {
789 if (zfsvfs
->z_use_fuids
)
790 pflags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
794 if (S_ISDIR(vap
->va_mode
)) {
795 size
= 2; /* contents ("." and "..") */
799 links
= (flag
& IS_TMPFILE
) ? 0 : 1;
802 if (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))
806 mode
= acl_ids
->z_mode
;
810 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
)) {
812 * With ZFS_PROJID flag, we can easily know whether there is
813 * project ID stored on disk or not. See zfs_space_delta_cb().
815 if (obj_type
!= DMU_OT_ZNODE
&&
816 dmu_objset_projectquota_enabled(zfsvfs
->z_os
))
817 pflags
|= ZFS_PROJID
;
820 * Inherit project ID from parent if required.
822 projid
= zfs_inherit_projid(dzp
);
823 if (dzp
->z_pflags
& ZFS_PROJINHERIT
)
824 pflags
|= ZFS_PROJINHERIT
;
828 * No execs denied will be deterimed when zfs_mode_compute() is called.
830 pflags
|= acl_ids
->z_aclp
->z_hints
&
831 (ZFS_ACL_TRIVIAL
|ZFS_INHERIT_ACE
|ZFS_ACL_AUTO_INHERIT
|
832 ZFS_ACL_DEFAULTED
|ZFS_ACL_PROTECTED
);
834 ZFS_TIME_ENCODE(&now
, crtime
);
835 ZFS_TIME_ENCODE(&now
, ctime
);
837 if (vap
->va_mask
& ATTR_ATIME
) {
838 ZFS_TIME_ENCODE(&vap
->va_atime
, atime
);
840 ZFS_TIME_ENCODE(&now
, atime
);
843 if (vap
->va_mask
& ATTR_MTIME
) {
844 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
846 ZFS_TIME_ENCODE(&now
, mtime
);
849 /* Now add in all of the "SA" attributes */
850 VERIFY(0 == sa_handle_get_from_db(zfsvfs
->z_os
, db
, NULL
, SA_HDL_SHARED
,
854 * Setup the array of attributes to be replaced/set on the new file
856 * order for DMU_OT_ZNODE is critical since it needs to be constructed
857 * in the old znode_phys_t format. Don't change this ordering
859 sa_attrs
= kmem_alloc(sizeof (sa_bulk_attr_t
) * ZPL_END
, KM_SLEEP
);
861 if (obj_type
== DMU_OT_ZNODE
) {
862 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zfsvfs
),
864 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zfsvfs
),
866 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zfsvfs
),
868 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zfsvfs
),
870 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zfsvfs
),
872 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zfsvfs
),
874 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zfsvfs
),
876 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zfsvfs
),
879 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zfsvfs
),
881 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zfsvfs
),
883 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zfsvfs
),
885 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zfsvfs
),
886 NULL
, &acl_ids
->z_fuid
, 8);
887 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zfsvfs
),
888 NULL
, &acl_ids
->z_fgid
, 8);
889 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zfsvfs
),
891 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zfsvfs
),
893 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zfsvfs
),
895 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zfsvfs
),
897 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zfsvfs
),
899 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zfsvfs
),
903 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_LINKS(zfsvfs
), NULL
, &links
, 8);
905 if (obj_type
== DMU_OT_ZNODE
) {
906 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_XATTR(zfsvfs
), NULL
,
908 } else if (dmu_objset_projectquota_enabled(zfsvfs
->z_os
) &&
909 pflags
& ZFS_PROJID
) {
910 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PROJID(zfsvfs
),
913 if (obj_type
== DMU_OT_ZNODE
||
914 (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))) {
915 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_RDEV(zfsvfs
),
918 if (obj_type
== DMU_OT_ZNODE
) {
919 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zfsvfs
),
921 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zfsvfs
), NULL
,
922 &acl_ids
->z_fuid
, 8);
923 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zfsvfs
), NULL
,
924 &acl_ids
->z_fgid
, 8);
925 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PAD(zfsvfs
), NULL
, pad
,
926 sizeof (uint64_t) * 4);
927 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ZNODE_ACL(zfsvfs
), NULL
,
928 &acl_phys
, sizeof (zfs_acl_phys_t
));
929 } else if (acl_ids
->z_aclp
->z_version
>= ZFS_ACL_VERSION_FUID
) {
930 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_COUNT(zfsvfs
), NULL
,
931 &acl_ids
->z_aclp
->z_acl_count
, 8);
932 locate
.cb_aclp
= acl_ids
->z_aclp
;
933 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_ACES(zfsvfs
),
934 zfs_acl_data_locator
, &locate
,
935 acl_ids
->z_aclp
->z_acl_bytes
);
936 mode
= zfs_mode_compute(mode
, acl_ids
->z_aclp
, &pflags
,
937 acl_ids
->z_fuid
, acl_ids
->z_fgid
);
940 VERIFY(sa_replace_all_by_template(sa_hdl
, sa_attrs
, cnt
, tx
) == 0);
942 if (!(flag
& IS_ROOT_NODE
)) {
944 * The call to zfs_znode_alloc() may fail if memory is low
945 * via the call path: alloc_inode() -> inode_init_always() ->
946 * security_inode_alloc() -> inode_alloc_security(). Since
947 * the existing code is written such that zfs_mknode() can
948 * not fail retry until sufficient memory has been reclaimed.
951 *zpp
= zfs_znode_alloc(zfsvfs
, db
, 0, obj_type
, obj
,
953 } while (*zpp
== NULL
);
955 VERIFY(*zpp
!= NULL
);
959 * If we are creating the root node, the "parent" we
960 * passed in is the znode for the root.
964 (*zpp
)->z_sa_hdl
= sa_hdl
;
967 (*zpp
)->z_pflags
= pflags
;
968 (*zpp
)->z_mode
= ZTOI(*zpp
)->i_mode
= mode
;
969 (*zpp
)->z_dnodesize
= dnodesize
;
970 (*zpp
)->z_projid
= projid
;
972 if (obj_type
== DMU_OT_ZNODE
||
973 acl_ids
->z_aclp
->z_version
< ZFS_ACL_VERSION_FUID
) {
974 VERIFY0(zfs_aclset_common(*zpp
, acl_ids
->z_aclp
, cr
, tx
));
976 kmem_free(sa_attrs
, sizeof (sa_bulk_attr_t
) * ZPL_END
);
977 zfs_znode_hold_exit(zfsvfs
, zh
);
981 * Update in-core attributes. It is assumed the caller will be doing an
982 * sa_bulk_update to push the changes out.
985 zfs_xvattr_set(znode_t
*zp
, xvattr_t
*xvap
, dmu_tx_t
*tx
)
988 boolean_t update_inode
= B_FALSE
;
990 xoap
= xva_getxoptattr(xvap
);
993 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
995 ZFS_TIME_ENCODE(&xoap
->xoa_createtime
, times
);
996 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_CRTIME(ZTOZSB(zp
)),
997 ×
, sizeof (times
), tx
);
998 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
1000 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
1001 ZFS_ATTR_SET(zp
, ZFS_READONLY
, xoap
->xoa_readonly
,
1003 XVA_SET_RTN(xvap
, XAT_READONLY
);
1005 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
1006 ZFS_ATTR_SET(zp
, ZFS_HIDDEN
, xoap
->xoa_hidden
,
1008 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
1010 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
1011 ZFS_ATTR_SET(zp
, ZFS_SYSTEM
, xoap
->xoa_system
,
1013 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
1015 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
1016 ZFS_ATTR_SET(zp
, ZFS_ARCHIVE
, xoap
->xoa_archive
,
1018 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
1020 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
1021 ZFS_ATTR_SET(zp
, ZFS_IMMUTABLE
, xoap
->xoa_immutable
,
1023 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
1025 update_inode
= B_TRUE
;
1027 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
1028 ZFS_ATTR_SET(zp
, ZFS_NOUNLINK
, xoap
->xoa_nounlink
,
1030 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
1032 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
1033 ZFS_ATTR_SET(zp
, ZFS_APPENDONLY
, xoap
->xoa_appendonly
,
1035 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
1037 update_inode
= B_TRUE
;
1039 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
1040 ZFS_ATTR_SET(zp
, ZFS_NODUMP
, xoap
->xoa_nodump
,
1042 XVA_SET_RTN(xvap
, XAT_NODUMP
);
1044 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
1045 ZFS_ATTR_SET(zp
, ZFS_OPAQUE
, xoap
->xoa_opaque
,
1047 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
1049 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
1050 ZFS_ATTR_SET(zp
, ZFS_AV_QUARANTINED
,
1051 xoap
->xoa_av_quarantined
, zp
->z_pflags
, tx
);
1052 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
1054 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
1055 ZFS_ATTR_SET(zp
, ZFS_AV_MODIFIED
, xoap
->xoa_av_modified
,
1057 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
1059 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
1060 zfs_sa_set_scanstamp(zp
, xvap
, tx
);
1061 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
1063 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
1064 ZFS_ATTR_SET(zp
, ZFS_REPARSE
, xoap
->xoa_reparse
,
1066 XVA_SET_RTN(xvap
, XAT_REPARSE
);
1068 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
1069 ZFS_ATTR_SET(zp
, ZFS_OFFLINE
, xoap
->xoa_offline
,
1071 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
1073 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
1074 ZFS_ATTR_SET(zp
, ZFS_SPARSE
, xoap
->xoa_sparse
,
1076 XVA_SET_RTN(xvap
, XAT_SPARSE
);
1078 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
1079 ZFS_ATTR_SET(zp
, ZFS_PROJINHERIT
, xoap
->xoa_projinherit
,
1081 XVA_SET_RTN(xvap
, XAT_PROJINHERIT
);
1085 zfs_set_inode_flags(zp
, ZTOI(zp
));
1089 zfs_zget(zfsvfs_t
*zfsvfs
, uint64_t obj_num
, znode_t
**zpp
)
1091 dmu_object_info_t doi
;
1101 zh
= zfs_znode_hold_enter(zfsvfs
, obj_num
);
1103 err
= sa_buf_hold(zfsvfs
->z_os
, obj_num
, NULL
, &db
);
1105 zfs_znode_hold_exit(zfsvfs
, zh
);
1109 dmu_object_info_from_db(db
, &doi
);
1110 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
1111 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
1112 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1113 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
1114 sa_buf_rele(db
, NULL
);
1115 zfs_znode_hold_exit(zfsvfs
, zh
);
1116 return (SET_ERROR(EINVAL
));
1119 hdl
= dmu_buf_get_user(db
);
1121 zp
= sa_get_userdata(hdl
);
1125 * Since "SA" does immediate eviction we
1126 * should never find a sa handle that doesn't
1127 * know about the znode.
1130 ASSERT3P(zp
, !=, NULL
);
1132 mutex_enter(&zp
->z_lock
);
1133 ASSERT3U(zp
->z_id
, ==, obj_num
);
1135 * If igrab() returns NULL the VFS has independently
1136 * determined the inode should be evicted and has
1137 * called iput_final() to start the eviction process.
1138 * The SA handle is still valid but because the VFS
1139 * requires that the eviction succeed we must drop
1140 * our locks and references to allow the eviction to
1141 * complete. The zfs_zget() may then be retried.
1143 * This unlikely case could be optimized by registering
1144 * a sops->drop_inode() callback. The callback would
1145 * need to detect the active SA hold thereby informing
1146 * the VFS that this inode should not be evicted.
1148 if (igrab(ZTOI(zp
)) == NULL
) {
1149 mutex_exit(&zp
->z_lock
);
1150 sa_buf_rele(db
, NULL
);
1151 zfs_znode_hold_exit(zfsvfs
, zh
);
1152 /* inode might need this to finish evict */
1158 mutex_exit(&zp
->z_lock
);
1159 sa_buf_rele(db
, NULL
);
1160 zfs_znode_hold_exit(zfsvfs
, zh
);
1165 * Not found create new znode/vnode but only if file exists.
1167 * There is a small window where zfs_vget() could
1168 * find this object while a file create is still in
1169 * progress. This is checked for in zfs_znode_alloc()
1171 * if zfs_znode_alloc() fails it will drop the hold on the
1174 zp
= zfs_znode_alloc(zfsvfs
, db
, doi
.doi_data_block_size
,
1175 doi
.doi_bonus_type
, obj_num
, NULL
);
1177 err
= SET_ERROR(ENOENT
);
1181 zfs_znode_hold_exit(zfsvfs
, zh
);
1186 zfs_rezget(znode_t
*zp
)
1188 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1189 dmu_object_info_t doi
;
1191 uint64_t obj_num
= zp
->z_id
;
1194 sa_bulk_attr_t bulk
[10];
1198 uint64_t z_uid
, z_gid
;
1199 uint64_t atime
[2], mtime
[2], ctime
[2];
1200 uint64_t projid
= ZFS_DEFAULT_PROJID
;
1204 * skip ctldir, otherwise they will always get invalidated. This will
1205 * cause funny behaviour for the mounted snapdirs. Especially for
1206 * Linux >= 3.18, d_invalidate will detach the mountpoint and prevent
1207 * anyone automount it again as long as someone is still using the
1210 if (zp
->z_is_ctldir
)
1213 zh
= zfs_znode_hold_enter(zfsvfs
, obj_num
);
1215 mutex_enter(&zp
->z_acl_lock
);
1216 if (zp
->z_acl_cached
) {
1217 zfs_acl_free(zp
->z_acl_cached
);
1218 zp
->z_acl_cached
= NULL
;
1220 mutex_exit(&zp
->z_acl_lock
);
1222 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
1223 if (zp
->z_xattr_cached
) {
1224 nvlist_free(zp
->z_xattr_cached
);
1225 zp
->z_xattr_cached
= NULL
;
1227 rw_exit(&zp
->z_xattr_lock
);
1229 ASSERT(zp
->z_sa_hdl
== NULL
);
1230 err
= sa_buf_hold(zfsvfs
->z_os
, obj_num
, NULL
, &db
);
1232 zfs_znode_hold_exit(zfsvfs
, zh
);
1236 dmu_object_info_from_db(db
, &doi
);
1237 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
1238 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
1239 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1240 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
1241 sa_buf_rele(db
, NULL
);
1242 zfs_znode_hold_exit(zfsvfs
, zh
);
1243 return (SET_ERROR(EINVAL
));
1246 zfs_znode_sa_init(zfsvfs
, zp
, db
, doi
.doi_bonus_type
, NULL
);
1248 /* reload cached values */
1249 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zfsvfs
), NULL
,
1250 &gen
, sizeof (gen
));
1251 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
1252 &zp
->z_size
, sizeof (zp
->z_size
));
1253 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
), NULL
,
1254 &links
, sizeof (links
));
1255 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
1256 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
1257 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
1258 &z_uid
, sizeof (z_uid
));
1259 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
1260 &z_gid
, sizeof (z_gid
));
1261 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
1262 &mode
, sizeof (mode
));
1263 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
1265 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
1267 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
1270 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) {
1271 zfs_znode_dmu_fini(zp
);
1272 zfs_znode_hold_exit(zfsvfs
, zh
);
1273 return (SET_ERROR(EIO
));
1276 if (dmu_objset_projectquota_enabled(zfsvfs
->z_os
)) {
1277 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PROJID(zfsvfs
),
1279 if (err
!= 0 && err
!= ENOENT
) {
1280 zfs_znode_dmu_fini(zp
);
1281 zfs_znode_hold_exit(zfsvfs
, zh
);
1282 return (SET_ERROR(err
));
1286 zp
->z_projid
= projid
;
1287 zp
->z_mode
= ZTOI(zp
)->i_mode
= mode
;
1288 zfs_uid_write(ZTOI(zp
), z_uid
);
1289 zfs_gid_write(ZTOI(zp
), z_gid
);
1291 ZFS_TIME_DECODE(&ZTOI(zp
)->i_atime
, atime
);
1292 ZFS_TIME_DECODE(&ZTOI(zp
)->i_mtime
, mtime
);
1293 ZFS_TIME_DECODE(&ZTOI(zp
)->i_ctime
, ctime
);
1295 if (gen
!= ZTOI(zp
)->i_generation
) {
1296 zfs_znode_dmu_fini(zp
);
1297 zfs_znode_hold_exit(zfsvfs
, zh
);
1298 return (SET_ERROR(EIO
));
1301 set_nlink(ZTOI(zp
), (uint32_t)links
);
1302 zfs_set_inode_flags(zp
, ZTOI(zp
));
1304 zp
->z_blksz
= doi
.doi_data_block_size
;
1305 zp
->z_atime_dirty
= 0;
1306 zfs_inode_update(zp
);
1309 * If the file has zero links, then it has been unlinked on the send
1310 * side and it must be in the received unlinked set.
1311 * We call zfs_znode_dmu_fini() now to prevent any accesses to the
1312 * stale data and to prevent automatical removal of the file in
1313 * zfs_zinactive(). The file will be removed either when it is removed
1314 * on the send side and the next incremental stream is received or
1315 * when the unlinked set gets processed.
1317 zp
->z_unlinked
= (ZTOI(zp
)->i_nlink
== 0);
1319 zfs_znode_dmu_fini(zp
);
1321 zfs_znode_hold_exit(zfsvfs
, zh
);
1327 zfs_znode_delete(znode_t
*zp
, dmu_tx_t
*tx
)
1329 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1330 objset_t
*os
= zfsvfs
->z_os
;
1331 uint64_t obj
= zp
->z_id
;
1332 uint64_t acl_obj
= zfs_external_acl(zp
);
1335 zh
= zfs_znode_hold_enter(zfsvfs
, obj
);
1337 VERIFY(!zp
->z_is_sa
);
1338 VERIFY(0 == dmu_object_free(os
, acl_obj
, tx
));
1340 VERIFY(0 == dmu_object_free(os
, obj
, tx
));
1341 zfs_znode_dmu_fini(zp
);
1342 zfs_znode_hold_exit(zfsvfs
, zh
);
1346 zfs_zinactive(znode_t
*zp
)
1348 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1349 uint64_t z_id
= zp
->z_id
;
1352 ASSERT(zp
->z_sa_hdl
);
1355 * Don't allow a zfs_zget() while were trying to release this znode.
1357 zh
= zfs_znode_hold_enter(zfsvfs
, z_id
);
1359 mutex_enter(&zp
->z_lock
);
1362 * If this was the last reference to a file with no links, remove
1363 * the file from the file system unless the file system is mounted
1364 * read-only. That can happen, for example, if the file system was
1365 * originally read-write, the file was opened, then unlinked and
1366 * the file system was made read-only before the file was finally
1367 * closed. The file will remain in the unlinked set.
1369 if (zp
->z_unlinked
) {
1370 ASSERT(!zfsvfs
->z_issnap
);
1371 if (!zfs_is_readonly(zfsvfs
)) {
1372 mutex_exit(&zp
->z_lock
);
1373 zfs_znode_hold_exit(zfsvfs
, zh
);
1379 mutex_exit(&zp
->z_lock
);
1380 zfs_znode_dmu_fini(zp
);
1382 zfs_znode_hold_exit(zfsvfs
, zh
);
1386 zfs_compare_timespec(struct timespec
*t1
, struct timespec
*t2
)
1388 if (t1
->tv_sec
< t2
->tv_sec
)
1391 if (t1
->tv_sec
> t2
->tv_sec
)
1394 return (t1
->tv_nsec
- t2
->tv_nsec
);
1398 * Prepare to update znode time stamps.
1400 * IN: zp - znode requiring timestamp update
1401 * flag - ATTR_MTIME, ATTR_CTIME flags
1407 * Note: We don't update atime here, because we rely on Linux VFS to do
1411 zfs_tstamp_update_setup(znode_t
*zp
, uint_t flag
, uint64_t mtime
[2],
1420 if (flag
& ATTR_MTIME
) {
1421 ZFS_TIME_ENCODE(&now
, mtime
);
1422 ZFS_TIME_DECODE(&(ZTOI(zp
)->i_mtime
), mtime
);
1423 if (ZTOZSB(zp
)->z_use_fuids
) {
1424 zp
->z_pflags
|= (ZFS_ARCHIVE
|
1429 if (flag
& ATTR_CTIME
) {
1430 ZFS_TIME_ENCODE(&now
, ctime
);
1431 ZFS_TIME_DECODE(&(ZTOI(zp
)->i_ctime
), ctime
);
1432 if (ZTOZSB(zp
)->z_use_fuids
)
1433 zp
->z_pflags
|= ZFS_ARCHIVE
;
1438 * Grow the block size for a file.
1440 * IN: zp - znode of file to free data in.
1441 * size - requested block size
1442 * tx - open transaction.
1444 * NOTE: this function assumes that the znode is write locked.
1447 zfs_grow_blocksize(znode_t
*zp
, uint64_t size
, dmu_tx_t
*tx
)
1452 if (size
<= zp
->z_blksz
)
1455 * If the file size is already greater than the current blocksize,
1456 * we will not grow. If there is more than one block in a file,
1457 * the blocksize cannot change.
1459 if (zp
->z_blksz
&& zp
->z_size
> zp
->z_blksz
)
1462 error
= dmu_object_set_blocksize(ZTOZSB(zp
)->z_os
, zp
->z_id
,
1465 if (error
== ENOTSUP
)
1469 /* What blocksize did we actually get? */
1470 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &zp
->z_blksz
, &dummy
);
1474 * Increase the file length
1476 * IN: zp - znode of file to free data in.
1477 * end - new end-of-file
1479 * RETURN: 0 on success, error code on failure
1482 zfs_extend(znode_t
*zp
, uint64_t end
)
1484 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1491 * We will change zp_size, lock the whole file.
1493 rl
= zfs_range_lock(&zp
->z_range_lock
, 0, UINT64_MAX
, RL_WRITER
);
1496 * Nothing to do if file already at desired length.
1498 if (end
<= zp
->z_size
) {
1499 zfs_range_unlock(rl
);
1502 tx
= dmu_tx_create(zfsvfs
->z_os
);
1503 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1504 zfs_sa_upgrade_txholds(tx
, zp
);
1505 if (end
> zp
->z_blksz
&&
1506 (!ISP2(zp
->z_blksz
) || zp
->z_blksz
< zfsvfs
->z_max_blksz
)) {
1508 * We are growing the file past the current block size.
1510 if (zp
->z_blksz
> ZTOZSB(zp
)->z_max_blksz
) {
1512 * File's blocksize is already larger than the
1513 * "recordsize" property. Only let it grow to
1514 * the next power of 2.
1516 ASSERT(!ISP2(zp
->z_blksz
));
1517 newblksz
= MIN(end
, 1 << highbit64(zp
->z_blksz
));
1519 newblksz
= MIN(end
, ZTOZSB(zp
)->z_max_blksz
);
1521 dmu_tx_hold_write(tx
, zp
->z_id
, 0, newblksz
);
1526 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1529 zfs_range_unlock(rl
);
1534 zfs_grow_blocksize(zp
, newblksz
, tx
);
1538 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(ZTOZSB(zp
)),
1539 &zp
->z_size
, sizeof (zp
->z_size
), tx
));
1541 zfs_range_unlock(rl
);
1549 * zfs_zero_partial_page - Modeled after update_pages() but
1550 * with different arguments and semantics for use by zfs_freesp().
1552 * Zeroes a piece of a single page cache entry for zp at offset
1553 * start and length len.
1555 * Caller must acquire a range lock on the file for the region
1556 * being zeroed in order that the ARC and page cache stay in sync.
1559 zfs_zero_partial_page(znode_t
*zp
, uint64_t start
, uint64_t len
)
1561 struct address_space
*mp
= ZTOI(zp
)->i_mapping
;
1566 ASSERT((start
& PAGE_MASK
) == ((start
+ len
- 1) & PAGE_MASK
));
1568 off
= start
& (PAGE_SIZE
- 1);
1571 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
1573 if (mapping_writably_mapped(mp
))
1574 flush_dcache_page(pp
);
1577 bzero(pb
+ off
, len
);
1580 if (mapping_writably_mapped(mp
))
1581 flush_dcache_page(pp
);
1583 mark_page_accessed(pp
);
1584 SetPageUptodate(pp
);
1592 * Free space in a file.
1594 * IN: zp - znode of file to free data in.
1595 * off - start of section to free.
1596 * len - length of section to free.
1598 * RETURN: 0 on success, error code on failure
1601 zfs_free_range(znode_t
*zp
, uint64_t off
, uint64_t len
)
1603 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1608 * Lock the range being freed.
1610 rl
= zfs_range_lock(&zp
->z_range_lock
, off
, len
, RL_WRITER
);
1613 * Nothing to do if file already at desired length.
1615 if (off
>= zp
->z_size
) {
1616 zfs_range_unlock(rl
);
1620 if (off
+ len
> zp
->z_size
)
1621 len
= zp
->z_size
- off
;
1623 error
= dmu_free_long_range(zfsvfs
->z_os
, zp
->z_id
, off
, len
);
1626 * Zero partial page cache entries. This must be done under a
1627 * range lock in order to keep the ARC and page cache in sync.
1629 if (zp
->z_is_mapped
) {
1630 loff_t first_page
, last_page
, page_len
;
1631 loff_t first_page_offset
, last_page_offset
;
1633 /* first possible full page in hole */
1634 first_page
= (off
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1635 /* last page of hole */
1636 last_page
= (off
+ len
) >> PAGE_SHIFT
;
1638 /* offset of first_page */
1639 first_page_offset
= first_page
<< PAGE_SHIFT
;
1640 /* offset of last_page */
1641 last_page_offset
= last_page
<< PAGE_SHIFT
;
1643 /* truncate whole pages */
1644 if (last_page_offset
> first_page_offset
) {
1645 truncate_inode_pages_range(ZTOI(zp
)->i_mapping
,
1646 first_page_offset
, last_page_offset
- 1);
1649 /* truncate sub-page ranges */
1650 if (first_page
> last_page
) {
1651 /* entire punched area within a single page */
1652 zfs_zero_partial_page(zp
, off
, len
);
1654 /* beginning of punched area at the end of a page */
1655 page_len
= first_page_offset
- off
;
1657 zfs_zero_partial_page(zp
, off
, page_len
);
1659 /* end of punched area at the beginning of a page */
1660 page_len
= off
+ len
- last_page_offset
;
1662 zfs_zero_partial_page(zp
, last_page_offset
,
1666 zfs_range_unlock(rl
);
1674 * IN: zp - znode of file to free data in.
1675 * end - new end-of-file.
1677 * RETURN: 0 on success, error code on failure
1680 zfs_trunc(znode_t
*zp
, uint64_t end
)
1682 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1686 sa_bulk_attr_t bulk
[2];
1690 * We will change zp_size, lock the whole file.
1692 rl
= zfs_range_lock(&zp
->z_range_lock
, 0, UINT64_MAX
, RL_WRITER
);
1695 * Nothing to do if file already at desired length.
1697 if (end
>= zp
->z_size
) {
1698 zfs_range_unlock(rl
);
1702 error
= dmu_free_long_range(zfsvfs
->z_os
, zp
->z_id
, end
,
1705 zfs_range_unlock(rl
);
1708 tx
= dmu_tx_create(zfsvfs
->z_os
);
1709 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1710 zfs_sa_upgrade_txholds(tx
, zp
);
1711 dmu_tx_mark_netfree(tx
);
1712 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1715 zfs_range_unlock(rl
);
1720 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
),
1721 NULL
, &zp
->z_size
, sizeof (zp
->z_size
));
1724 zp
->z_pflags
&= ~ZFS_SPARSE
;
1725 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
),
1726 NULL
, &zp
->z_pflags
, 8);
1728 VERIFY(sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
) == 0);
1732 zfs_range_unlock(rl
);
1738 * Free space in a file
1740 * IN: zp - znode of file to free data in.
1741 * off - start of range
1742 * len - end of range (0 => EOF)
1743 * flag - current file open mode flags.
1744 * log - TRUE if this action should be logged
1746 * RETURN: 0 on success, error code on failure
1749 zfs_freesp(znode_t
*zp
, uint64_t off
, uint64_t len
, int flag
, boolean_t log
)
1752 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1753 zilog_t
*zilog
= zfsvfs
->z_log
;
1755 uint64_t mtime
[2], ctime
[2];
1756 sa_bulk_attr_t bulk
[3];
1760 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MODE(zfsvfs
), &mode
,
1761 sizeof (mode
))) != 0)
1764 if (off
> zp
->z_size
) {
1765 error
= zfs_extend(zp
, off
+len
);
1766 if (error
== 0 && log
)
1772 error
= zfs_trunc(zp
, off
);
1774 if ((error
= zfs_free_range(zp
, off
, len
)) == 0 &&
1775 off
+ len
> zp
->z_size
)
1776 error
= zfs_extend(zp
, off
+len
);
1781 tx
= dmu_tx_create(zfsvfs
->z_os
);
1782 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1783 zfs_sa_upgrade_txholds(tx
, zp
);
1784 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1790 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, mtime
, 16);
1791 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, ctime
, 16);
1792 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
),
1793 NULL
, &zp
->z_pflags
, 8);
1794 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
1795 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1798 zfs_log_truncate(zilog
, tx
, TX_TRUNCATE
, zp
, off
, len
);
1802 zfs_inode_update(zp
);
1807 * Truncate the page cache - for file truncate operations, use
1808 * the purpose-built API for truncations. For punching operations,
1809 * the truncation is handled under a range lock in zfs_free_range.
1812 truncate_setsize(ZTOI(zp
), off
);
1817 zfs_create_fs(objset_t
*os
, cred_t
*cr
, nvlist_t
*zplprops
, dmu_tx_t
*tx
)
1819 struct super_block
*sb
;
1821 uint64_t moid
, obj
, sa_obj
, version
;
1822 uint64_t sense
= ZFS_CASE_SENSITIVE
;
1828 znode_t
*rootzp
= NULL
;
1831 zfs_acl_ids_t acl_ids
;
1834 * First attempt to create master node.
1837 * In an empty objset, there are no blocks to read and thus
1838 * there can be no i/o errors (which we assert below).
1840 moid
= MASTER_NODE_OBJ
;
1841 error
= zap_create_claim(os
, moid
, DMU_OT_MASTER_NODE
,
1842 DMU_OT_NONE
, 0, tx
);
1846 * Set starting attributes.
1848 version
= zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)));
1850 while ((elem
= nvlist_next_nvpair(zplprops
, elem
)) != NULL
) {
1851 /* For the moment we expect all zpl props to be uint64_ts */
1855 ASSERT(nvpair_type(elem
) == DATA_TYPE_UINT64
);
1856 VERIFY(nvpair_value_uint64(elem
, &val
) == 0);
1857 name
= nvpair_name(elem
);
1858 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_VERSION
)) == 0) {
1862 error
= zap_update(os
, moid
, name
, 8, 1, &val
, tx
);
1865 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_NORMALIZE
)) == 0)
1867 else if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_CASE
)) == 0)
1870 ASSERT(version
!= 0);
1871 error
= zap_update(os
, moid
, ZPL_VERSION_STR
, 8, 1, &version
, tx
);
1874 * Create zap object used for SA attribute registration
1877 if (version
>= ZPL_VERSION_SA
) {
1878 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1879 DMU_OT_NONE
, 0, tx
);
1880 error
= zap_add(os
, moid
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1886 * Create a delete queue.
1888 obj
= zap_create(os
, DMU_OT_UNLINKED_SET
, DMU_OT_NONE
, 0, tx
);
1890 error
= zap_add(os
, moid
, ZFS_UNLINKED_SET
, 8, 1, &obj
, tx
);
1894 * Create root znode. Create minimal znode/inode/zfsvfs/sb
1895 * to allow zfs_mknode to work.
1897 vattr
.va_mask
= ATTR_MODE
|ATTR_UID
|ATTR_GID
;
1898 vattr
.va_mode
= S_IFDIR
|0755;
1899 vattr
.va_uid
= crgetuid(cr
);
1900 vattr
.va_gid
= crgetgid(cr
);
1902 rootzp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
1903 rootzp
->z_moved
= 0;
1904 rootzp
->z_unlinked
= 0;
1905 rootzp
->z_atime_dirty
= 0;
1906 rootzp
->z_is_sa
= USE_SA(version
, os
);
1907 rootzp
->z_pflags
= 0;
1909 zfsvfs
= kmem_zalloc(sizeof (zfsvfs_t
), KM_SLEEP
);
1911 zfsvfs
->z_parent
= zfsvfs
;
1912 zfsvfs
->z_version
= version
;
1913 zfsvfs
->z_use_fuids
= USE_FUIDS(version
, os
);
1914 zfsvfs
->z_use_sa
= USE_SA(version
, os
);
1915 zfsvfs
->z_norm
= norm
;
1917 sb
= kmem_zalloc(sizeof (struct super_block
), KM_SLEEP
);
1918 sb
->s_fs_info
= zfsvfs
;
1920 ZTOI(rootzp
)->i_sb
= sb
;
1922 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
1923 &zfsvfs
->z_attr_table
);
1928 * Fold case on file systems that are always or sometimes case
1931 if (sense
== ZFS_CASE_INSENSITIVE
|| sense
== ZFS_CASE_MIXED
)
1932 zfsvfs
->z_norm
|= U8_TEXTPREP_TOUPPER
;
1934 mutex_init(&zfsvfs
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1935 list_create(&zfsvfs
->z_all_znodes
, sizeof (znode_t
),
1936 offsetof(znode_t
, z_link_node
));
1938 size
= MIN(1 << (highbit64(zfs_object_mutex_size
)-1), ZFS_OBJ_MTX_MAX
);
1939 zfsvfs
->z_hold_size
= size
;
1940 zfsvfs
->z_hold_trees
= vmem_zalloc(sizeof (avl_tree_t
) * size
,
1942 zfsvfs
->z_hold_locks
= vmem_zalloc(sizeof (kmutex_t
) * size
, KM_SLEEP
);
1943 for (i
= 0; i
!= size
; i
++) {
1944 avl_create(&zfsvfs
->z_hold_trees
[i
], zfs_znode_hold_compare
,
1945 sizeof (znode_hold_t
), offsetof(znode_hold_t
, zh_node
));
1946 mutex_init(&zfsvfs
->z_hold_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
1949 VERIFY(0 == zfs_acl_ids_create(rootzp
, IS_ROOT_NODE
, &vattr
,
1950 cr
, NULL
, &acl_ids
));
1951 zfs_mknode(rootzp
, &vattr
, tx
, cr
, IS_ROOT_NODE
, &zp
, &acl_ids
);
1952 ASSERT3P(zp
, ==, rootzp
);
1953 error
= zap_add(os
, moid
, ZFS_ROOT_OBJ
, 8, 1, &rootzp
->z_id
, tx
);
1955 zfs_acl_ids_free(&acl_ids
);
1957 atomic_set(&ZTOI(rootzp
)->i_count
, 0);
1958 sa_handle_destroy(rootzp
->z_sa_hdl
);
1959 kmem_cache_free(znode_cache
, rootzp
);
1962 * Create shares directory
1964 error
= zfs_create_share_dir(zfsvfs
, tx
);
1967 for (i
= 0; i
!= size
; i
++) {
1968 avl_destroy(&zfsvfs
->z_hold_trees
[i
]);
1969 mutex_destroy(&zfsvfs
->z_hold_locks
[i
]);
1972 mutex_destroy(&zfsvfs
->z_znodes_lock
);
1974 vmem_free(zfsvfs
->z_hold_trees
, sizeof (avl_tree_t
) * size
);
1975 vmem_free(zfsvfs
->z_hold_locks
, sizeof (kmutex_t
) * size
);
1976 kmem_free(sb
, sizeof (struct super_block
));
1977 kmem_free(zfsvfs
, sizeof (zfsvfs_t
));
1979 #endif /* _KERNEL */
1982 zfs_sa_setup(objset_t
*osp
, sa_attr_type_t
**sa_table
)
1984 uint64_t sa_obj
= 0;
1987 error
= zap_lookup(osp
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1988 if (error
!= 0 && error
!= ENOENT
)
1991 error
= sa_setup(osp
, sa_obj
, zfs_attr_table
, ZPL_END
, sa_table
);
1996 zfs_grab_sa_handle(objset_t
*osp
, uint64_t obj
, sa_handle_t
**hdlp
,
1997 dmu_buf_t
**db
, void *tag
)
1999 dmu_object_info_t doi
;
2002 if ((error
= sa_buf_hold(osp
, obj
, tag
, db
)) != 0)
2005 dmu_object_info_from_db(*db
, &doi
);
2006 if ((doi
.doi_bonus_type
!= DMU_OT_SA
&&
2007 doi
.doi_bonus_type
!= DMU_OT_ZNODE
) ||
2008 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
2009 doi
.doi_bonus_size
< sizeof (znode_phys_t
))) {
2010 sa_buf_rele(*db
, tag
);
2011 return (SET_ERROR(ENOTSUP
));
2014 error
= sa_handle_get(osp
, obj
, NULL
, SA_HDL_PRIVATE
, hdlp
);
2016 sa_buf_rele(*db
, tag
);
2024 zfs_release_sa_handle(sa_handle_t
*hdl
, dmu_buf_t
*db
, void *tag
)
2026 sa_handle_destroy(hdl
);
2027 sa_buf_rele(db
, tag
);
2031 * Given an object number, return its parent object number and whether
2032 * or not the object is an extended attribute directory.
2035 zfs_obj_to_pobj(objset_t
*osp
, sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
2036 uint64_t *pobjp
, int *is_xattrdir
)
2041 uint64_t parent_mode
;
2042 sa_bulk_attr_t bulk
[3];
2043 sa_handle_t
*sa_hdl
;
2048 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_PARENT
], NULL
,
2049 &parent
, sizeof (parent
));
2050 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_FLAGS
], NULL
,
2051 &pflags
, sizeof (pflags
));
2052 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
2053 &mode
, sizeof (mode
));
2055 if ((error
= sa_bulk_lookup(hdl
, bulk
, count
)) != 0)
2059 * When a link is removed its parent pointer is not changed and will
2060 * be invalid. There are two cases where a link is removed but the
2061 * file stays around, when it goes to the delete queue and when there
2062 * are additional links.
2064 error
= zfs_grab_sa_handle(osp
, parent
, &sa_hdl
, &sa_db
, FTAG
);
2068 error
= sa_lookup(sa_hdl
, ZPL_MODE
, &parent_mode
, sizeof (parent_mode
));
2069 zfs_release_sa_handle(sa_hdl
, sa_db
, FTAG
);
2073 *is_xattrdir
= ((pflags
& ZFS_XATTR
) != 0) && S_ISDIR(mode
);
2076 * Extended attributes can be applied to files, directories, etc.
2077 * Otherwise the parent must be a directory.
2079 if (!*is_xattrdir
&& !S_ISDIR(parent_mode
))
2080 return (SET_ERROR(EINVAL
));
2088 * Given an object number, return some zpl level statistics
2091 zfs_obj_to_stats_impl(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
2094 sa_bulk_attr_t bulk
[4];
2097 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
2098 &sb
->zs_mode
, sizeof (sb
->zs_mode
));
2099 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_GEN
], NULL
,
2100 &sb
->zs_gen
, sizeof (sb
->zs_gen
));
2101 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_LINKS
], NULL
,
2102 &sb
->zs_links
, sizeof (sb
->zs_links
));
2103 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_CTIME
], NULL
,
2104 &sb
->zs_ctime
, sizeof (sb
->zs_ctime
));
2106 return (sa_bulk_lookup(hdl
, bulk
, count
));
2110 zfs_obj_to_path_impl(objset_t
*osp
, uint64_t obj
, sa_handle_t
*hdl
,
2111 sa_attr_type_t
*sa_table
, char *buf
, int len
)
2113 sa_handle_t
*sa_hdl
;
2114 sa_handle_t
*prevhdl
= NULL
;
2115 dmu_buf_t
*prevdb
= NULL
;
2116 dmu_buf_t
*sa_db
= NULL
;
2117 char *path
= buf
+ len
- 1;
2123 uint64_t deleteq_obj
;
2124 VERIFY0(zap_lookup(osp
, MASTER_NODE_OBJ
,
2125 ZFS_UNLINKED_SET
, sizeof (uint64_t), 1, &deleteq_obj
));
2126 error
= zap_lookup_int(osp
, deleteq_obj
, obj
);
2129 } else if (error
!= ENOENT
) {
2136 char component
[MAXNAMELEN
+ 2];
2138 int is_xattrdir
= 0;
2141 zfs_release_sa_handle(prevhdl
, prevdb
, FTAG
);
2143 if ((error
= zfs_obj_to_pobj(osp
, sa_hdl
, sa_table
, &pobj
,
2144 &is_xattrdir
)) != 0)
2155 (void) sprintf(component
+ 1, "<xattrdir>");
2157 error
= zap_value_search(osp
, pobj
, obj
,
2158 ZFS_DIRENT_OBJ(-1ULL), component
+ 1);
2163 complen
= strlen(component
);
2165 ASSERT(path
>= buf
);
2166 bcopy(component
, path
, complen
);
2169 if (sa_hdl
!= hdl
) {
2173 error
= zfs_grab_sa_handle(osp
, obj
, &sa_hdl
, &sa_db
, FTAG
);
2181 if (sa_hdl
!= NULL
&& sa_hdl
!= hdl
) {
2182 ASSERT(sa_db
!= NULL
);
2183 zfs_release_sa_handle(sa_hdl
, sa_db
, FTAG
);
2187 (void) memmove(buf
, path
, buf
+ len
- path
);
2193 zfs_obj_to_path(objset_t
*osp
, uint64_t obj
, char *buf
, int len
)
2195 sa_attr_type_t
*sa_table
;
2200 error
= zfs_sa_setup(osp
, &sa_table
);
2204 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
, FTAG
);
2208 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
2210 zfs_release_sa_handle(hdl
, db
, FTAG
);
2215 zfs_obj_to_stats(objset_t
*osp
, uint64_t obj
, zfs_stat_t
*sb
,
2218 char *path
= buf
+ len
- 1;
2219 sa_attr_type_t
*sa_table
;
2226 error
= zfs_sa_setup(osp
, &sa_table
);
2230 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
, FTAG
);
2234 error
= zfs_obj_to_stats_impl(hdl
, sa_table
, sb
);
2236 zfs_release_sa_handle(hdl
, db
, FTAG
);
2240 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
2242 zfs_release_sa_handle(hdl
, db
, FTAG
);
2246 #if defined(_KERNEL) && defined(HAVE_SPL)
2247 EXPORT_SYMBOL(zfs_create_fs
);
2248 EXPORT_SYMBOL(zfs_obj_to_path
);
2251 module_param(zfs_object_mutex_size
, uint
, 0644);
2252 MODULE_PARM_DESC(zfs_object_mutex_size
, "Size of znode hold array");