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) 2013 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/refcount.h>
67 #include <sys/zfs_znode.h>
69 #include <sys/zfs_sa.h>
70 #include <sys/zfs_stat.h>
73 #include "zfs_comutil.h"
76 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
77 * turned on when DEBUG is also defined.
84 #define ZNODE_STAT_ADD(stat) ((stat)++)
86 #define ZNODE_STAT_ADD(stat) /* nothing */
87 #endif /* ZNODE_STATS */
90 * Functions needed for userland (ie: libzpool) are not put under
91 * #ifdef_KERNEL; the rest of the functions have dependencies
92 * (such as VFS logic) that will not compile easily in userland.
96 static kmem_cache_t
*znode_cache
= NULL
;
100 zfs_znode_cache_constructor(void *buf
, void *arg
, int kmflags
)
104 inode_init_once(ZTOI(zp
));
105 list_link_init(&zp
->z_link_node
);
107 mutex_init(&zp
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
108 rw_init(&zp
->z_parent_lock
, NULL
, RW_DEFAULT
, NULL
);
109 rw_init(&zp
->z_name_lock
, NULL
, RW_DEFAULT
, NULL
);
110 mutex_init(&zp
->z_acl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
111 rw_init(&zp
->z_xattr_lock
, NULL
, RW_DEFAULT
, NULL
);
113 mutex_init(&zp
->z_range_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
114 avl_create(&zp
->z_range_avl
, zfs_range_compare
,
115 sizeof (rl_t
), offsetof(rl_t
, r_node
));
117 zp
->z_dirlocks
= NULL
;
118 zp
->z_acl_cached
= NULL
;
119 zp
->z_xattr_cached
= NULL
;
120 zp
->z_xattr_parent
= NULL
;
127 zfs_znode_cache_destructor(void *buf
, void *arg
)
131 ASSERT(!list_link_active(&zp
->z_link_node
));
132 mutex_destroy(&zp
->z_lock
);
133 rw_destroy(&zp
->z_parent_lock
);
134 rw_destroy(&zp
->z_name_lock
);
135 mutex_destroy(&zp
->z_acl_lock
);
136 rw_destroy(&zp
->z_xattr_lock
);
137 avl_destroy(&zp
->z_range_avl
);
138 mutex_destroy(&zp
->z_range_lock
);
140 ASSERT(zp
->z_dirlocks
== NULL
);
141 ASSERT(zp
->z_acl_cached
== NULL
);
142 ASSERT(zp
->z_xattr_cached
== NULL
);
143 ASSERT(zp
->z_xattr_parent
== NULL
);
152 ASSERT(znode_cache
== NULL
);
153 znode_cache
= kmem_cache_create("zfs_znode_cache",
154 sizeof (znode_t
), 0, zfs_znode_cache_constructor
,
155 zfs_znode_cache_destructor
, NULL
, NULL
, NULL
, KMC_KMEM
);
165 kmem_cache_destroy(znode_cache
);
170 zfs_create_share_dir(zfs_sb_t
*zsb
, dmu_tx_t
*tx
)
172 #ifdef HAVE_SMB_SHARE
173 zfs_acl_ids_t acl_ids
;
180 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
181 vattr
.va_mode
= S_IFDIR
| 0555;
182 vattr
.va_uid
= crgetuid(kcred
);
183 vattr
.va_gid
= crgetgid(kcred
);
185 sharezp
= kmem_cache_alloc(znode_cache
, KM_PUSHPAGE
);
186 sharezp
->z_moved
= 0;
187 sharezp
->z_unlinked
= 0;
188 sharezp
->z_atime_dirty
= 0;
189 sharezp
->z_zfsvfs
= zfsvfs
;
190 sharezp
->z_is_sa
= zfsvfs
->z_use_sa
;
196 VERIFY(0 == zfs_acl_ids_create(sharezp
, IS_ROOT_NODE
, &vattr
,
197 kcred
, NULL
, &acl_ids
));
198 zfs_mknode(sharezp
, &vattr
, tx
, kcred
, IS_ROOT_NODE
, &zp
, &acl_ids
);
199 ASSERT3P(zp
, ==, sharezp
);
200 ASSERT(!vn_in_dnlc(ZTOV(sharezp
))); /* not valid to move */
201 POINTER_INVALIDATE(&sharezp
->z_zfsvfs
);
202 error
= zap_add(zfsvfs
->z_os
, MASTER_NODE_OBJ
,
203 ZFS_SHARES_DIR
, 8, 1, &sharezp
->z_id
, tx
);
204 zfsvfs
->z_shares_dir
= sharezp
->z_id
;
206 zfs_acl_ids_free(&acl_ids
);
207 // ZTOV(sharezp)->v_count = 0;
208 sa_handle_destroy(sharezp
->z_sa_hdl
);
209 kmem_cache_free(znode_cache
, sharezp
);
214 #endif /* HAVE_SMB_SHARE */
218 zfs_znode_sa_init(zfs_sb_t
*zsb
, znode_t
*zp
,
219 dmu_buf_t
*db
, dmu_object_type_t obj_type
, sa_handle_t
*sa_hdl
)
221 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zsb
, zp
->z_id
)));
223 mutex_enter(&zp
->z_lock
);
225 ASSERT(zp
->z_sa_hdl
== NULL
);
226 ASSERT(zp
->z_acl_cached
== NULL
);
227 if (sa_hdl
== NULL
) {
228 VERIFY(0 == sa_handle_get_from_db(zsb
->z_os
, db
, zp
,
229 SA_HDL_SHARED
, &zp
->z_sa_hdl
));
231 zp
->z_sa_hdl
= sa_hdl
;
232 sa_set_userp(sa_hdl
, zp
);
235 zp
->z_is_sa
= (obj_type
== DMU_OT_SA
) ? B_TRUE
: B_FALSE
;
237 mutex_exit(&zp
->z_lock
);
241 zfs_znode_dmu_fini(znode_t
*zp
)
243 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(ZTOZSB(zp
), zp
->z_id
)) ||
245 RW_WRITE_HELD(&ZTOZSB(zp
)->z_teardown_inactive_lock
));
247 sa_handle_destroy(zp
->z_sa_hdl
);
252 * Called by new_inode() to allocate a new inode.
255 zfs_inode_alloc(struct super_block
*sb
, struct inode
**ip
)
259 zp
= kmem_cache_alloc(znode_cache
, KM_PUSHPAGE
);
266 * Called in multiple places when an inode should be destroyed.
269 zfs_inode_destroy(struct inode
*ip
)
271 znode_t
*zp
= ITOZ(ip
);
272 zfs_sb_t
*zsb
= ZTOZSB(zp
);
274 if (zfsctl_is_node(ip
))
275 zfsctl_inode_destroy(ip
);
277 mutex_enter(&zsb
->z_znodes_lock
);
278 if (list_link_active(&zp
->z_link_node
)) {
279 list_remove(&zsb
->z_all_znodes
, zp
);
282 mutex_exit(&zsb
->z_znodes_lock
);
284 if (zp
->z_acl_cached
) {
285 zfs_acl_free(zp
->z_acl_cached
);
286 zp
->z_acl_cached
= NULL
;
289 if (zp
->z_xattr_cached
) {
290 nvlist_free(zp
->z_xattr_cached
);
291 zp
->z_xattr_cached
= NULL
;
294 if (zp
->z_xattr_parent
) {
295 iput(ZTOI(zp
->z_xattr_parent
));
296 zp
->z_xattr_parent
= NULL
;
299 kmem_cache_free(znode_cache
, zp
);
303 zfs_inode_set_ops(zfs_sb_t
*zsb
, struct inode
*ip
)
307 switch (ip
->i_mode
& S_IFMT
) {
309 ip
->i_op
= &zpl_inode_operations
;
310 ip
->i_fop
= &zpl_file_operations
;
311 ip
->i_mapping
->a_ops
= &zpl_address_space_operations
;
315 ip
->i_op
= &zpl_dir_inode_operations
;
316 ip
->i_fop
= &zpl_dir_file_operations
;
317 ITOZ(ip
)->z_zn_prefetch
= B_TRUE
;
321 ip
->i_op
= &zpl_symlink_inode_operations
;
325 * rdev is only stored in a SA only for device files.
329 VERIFY(sa_lookup(ITOZ(ip
)->z_sa_hdl
, SA_ZPL_RDEV(zsb
),
330 &rdev
, sizeof (rdev
)) == 0);
334 init_special_inode(ip
, ip
->i_mode
, rdev
);
335 ip
->i_op
= &zpl_special_inode_operations
;
339 printk("ZFS: Invalid mode: 0x%x\n", ip
->i_mode
);
345 * Construct a znode+inode and initialize.
347 * This does not do a call to dmu_set_user() that is
348 * up to the caller to do, in case you don't want to
352 zfs_znode_alloc(zfs_sb_t
*zsb
, dmu_buf_t
*db
, int blksz
,
353 dmu_object_type_t obj_type
, uint64_t obj
, sa_handle_t
*hdl
,
360 sa_bulk_attr_t bulk
[9];
365 ip
= new_inode(zsb
->z_sb
);
370 ASSERT(zp
->z_dirlocks
== NULL
);
371 ASSERT3P(zp
->z_acl_cached
, ==, NULL
);
372 ASSERT3P(zp
->z_xattr_cached
, ==, NULL
);
373 ASSERT3P(zp
->z_xattr_parent
, ==, NULL
);
377 zp
->z_atime_dirty
= 0;
379 zp
->z_id
= db
->db_object
;
381 zp
->z_seq
= 0x7A4653;
383 zp
->z_is_zvol
= B_FALSE
;
384 zp
->z_is_mapped
= B_FALSE
;
385 zp
->z_is_ctldir
= B_FALSE
;
386 zp
->z_is_stale
= B_FALSE
;
388 zfs_znode_sa_init(zsb
, zp
, db
, obj_type
, hdl
);
390 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
, &mode
, 8);
391 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zsb
), NULL
, &zp
->z_gen
, 8);
392 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
, &zp
->z_size
, 8);
393 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zsb
), NULL
, &zp
->z_links
, 8);
394 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
396 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PARENT(zsb
), NULL
,
398 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
400 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
, &zp
->z_uid
, 8);
401 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
), NULL
, &zp
->z_gid
, 8);
403 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
) != 0 || zp
->z_gen
== 0) {
405 sa_handle_destroy(zp
->z_sa_hdl
);
413 * xattr znodes hold a reference on their unique parent
415 if (dip
&& zp
->z_pflags
& ZFS_XATTR
) {
417 zp
->z_xattr_parent
= ITOZ(dip
);
421 zfs_inode_update(zp
);
422 zfs_inode_set_ops(zsb
, ip
);
425 * The only way insert_inode_locked() can fail is if the ip->i_ino
426 * number is already hashed for this super block. This can never
427 * happen because the inode numbers map 1:1 with the object numbers.
429 * The one exception is rolling back a mounted file system, but in
430 * this case all the active inode are unhashed during the rollback.
432 VERIFY3S(insert_inode_locked(ip
), ==, 0);
434 mutex_enter(&zsb
->z_znodes_lock
);
435 list_insert_tail(&zsb
->z_all_znodes
, zp
);
438 mutex_exit(&zsb
->z_znodes_lock
);
440 unlock_new_inode(ip
);
444 unlock_new_inode(ip
);
450 zfs_set_inode_flags(znode_t
*zp
, struct inode
*ip
)
453 * Linux and Solaris have different sets of file attributes, so we
454 * restrict this conversion to the intersection of the two.
457 if (zp
->z_pflags
& ZFS_IMMUTABLE
)
458 ip
->i_flags
|= S_IMMUTABLE
;
460 ip
->i_flags
&= ~S_IMMUTABLE
;
462 if (zp
->z_pflags
& ZFS_APPENDONLY
)
463 ip
->i_flags
|= S_APPEND
;
465 ip
->i_flags
&= ~S_APPEND
;
469 * Update the embedded inode given the znode. We should work toward
470 * eliminating this function as soon as possible by removing values
471 * which are duplicated between the znode and inode. If the generic
472 * inode has the correct field it should be used, and the ZFS code
473 * updated to access the inode. This can be done incrementally.
476 zfs_inode_update(znode_t
*zp
)
481 uint64_t atime
[2], mtime
[2], ctime
[2];
487 /* Skip .zfs control nodes which do not exist on disk. */
488 if (zfsctl_is_node(ip
))
491 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
), &atime
, 16);
492 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MTIME(zsb
), &mtime
, 16);
493 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CTIME(zsb
), &ctime
, 16);
495 spin_lock(&ip
->i_lock
);
496 ip
->i_generation
= zp
->z_gen
;
497 ip
->i_uid
= SUID_TO_KUID(zp
->z_uid
);
498 ip
->i_gid
= SGID_TO_KGID(zp
->z_gid
);
499 set_nlink(ip
, zp
->z_links
);
500 ip
->i_mode
= zp
->z_mode
;
501 zfs_set_inode_flags(zp
, ip
);
502 ip
->i_blkbits
= SPA_MINBLOCKSHIFT
;
503 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &blksize
,
504 (u_longlong_t
*)&ip
->i_blocks
);
506 ZFS_TIME_DECODE(&ip
->i_atime
, atime
);
507 ZFS_TIME_DECODE(&ip
->i_mtime
, mtime
);
508 ZFS_TIME_DECODE(&ip
->i_ctime
, ctime
);
510 i_size_write(ip
, zp
->z_size
);
511 spin_unlock(&ip
->i_lock
);
515 * Safely mark an inode dirty. Inodes which are part of a read-only
516 * file system or snapshot may not be dirtied.
519 zfs_mark_inode_dirty(struct inode
*ip
)
521 zfs_sb_t
*zsb
= ITOZSB(ip
);
523 if (zfs_is_readonly(zsb
) || dmu_objset_is_snapshot(zsb
->z_os
))
526 mark_inode_dirty(ip
);
529 static uint64_t empty_xattr
;
530 static uint64_t pad
[4];
531 static zfs_acl_phys_t acl_phys
;
533 * Create a new DMU object to hold a zfs znode.
535 * IN: dzp - parent directory for new znode
536 * vap - file attributes for new znode
537 * tx - dmu transaction id for zap operations
538 * cr - credentials of caller
540 * IS_ROOT_NODE - new object will be root
541 * IS_XATTR - new object is an attribute
542 * bonuslen - length of bonus buffer
543 * setaclp - File/Dir initial ACL
544 * fuidp - Tracks fuid allocation.
546 * OUT: zpp - allocated znode
550 zfs_mknode(znode_t
*dzp
, vattr_t
*vap
, dmu_tx_t
*tx
, cred_t
*cr
,
551 uint_t flag
, znode_t
**zpp
, zfs_acl_ids_t
*acl_ids
)
553 uint64_t crtime
[2], atime
[2], mtime
[2], ctime
[2];
554 uint64_t mode
, size
, links
, parent
, pflags
;
555 uint64_t dzp_pflags
= 0;
557 zfs_sb_t
*zsb
= ZTOZSB(dzp
);
564 dmu_object_type_t obj_type
;
565 sa_bulk_attr_t
*sa_attrs
;
567 zfs_acl_locator_cb_t locate
= { 0 };
570 obj
= vap
->va_nodeid
;
571 now
= vap
->va_ctime
; /* see zfs_replay_create() */
572 gen
= vap
->va_nblocks
; /* ditto */
576 gen
= dmu_tx_get_txg(tx
);
579 obj_type
= zsb
->z_use_sa
? DMU_OT_SA
: DMU_OT_ZNODE
;
580 bonuslen
= (obj_type
== DMU_OT_SA
) ?
581 DN_MAX_BONUSLEN
: ZFS_OLD_ZNODE_PHYS_SIZE
;
584 * Create a new DMU object.
587 * There's currently no mechanism for pre-reading the blocks that will
588 * be needed to allocate a new object, so we accept the small chance
589 * that there will be an i/o error and we will fail one of the
592 if (S_ISDIR(vap
->va_mode
)) {
594 err
= zap_create_claim_norm(zsb
->z_os
, obj
,
595 zsb
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
596 obj_type
, bonuslen
, tx
);
599 obj
= zap_create_norm(zsb
->z_os
,
600 zsb
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
601 obj_type
, bonuslen
, tx
);
605 err
= dmu_object_claim(zsb
->z_os
, obj
,
606 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
607 obj_type
, bonuslen
, tx
);
610 obj
= dmu_object_alloc(zsb
->z_os
,
611 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
612 obj_type
, bonuslen
, tx
);
616 ZFS_OBJ_HOLD_ENTER(zsb
, obj
);
617 VERIFY(0 == sa_buf_hold(zsb
->z_os
, obj
, NULL
, &db
));
620 * If this is the root, fix up the half-initialized parent pointer
621 * to reference the just-allocated physical data area.
623 if (flag
& IS_ROOT_NODE
) {
626 dzp_pflags
= dzp
->z_pflags
;
630 * If parent is an xattr, so am I.
632 if (dzp_pflags
& ZFS_XATTR
) {
636 if (zsb
->z_use_fuids
)
637 pflags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
641 if (S_ISDIR(vap
->va_mode
)) {
642 size
= 2; /* contents ("." and "..") */
643 links
= (flag
& (IS_ROOT_NODE
| IS_XATTR
)) ? 2 : 1;
648 if (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))
652 mode
= acl_ids
->z_mode
;
657 * No execs denied will be deterimed when zfs_mode_compute() is called.
659 pflags
|= acl_ids
->z_aclp
->z_hints
&
660 (ZFS_ACL_TRIVIAL
|ZFS_INHERIT_ACE
|ZFS_ACL_AUTO_INHERIT
|
661 ZFS_ACL_DEFAULTED
|ZFS_ACL_PROTECTED
);
663 ZFS_TIME_ENCODE(&now
, crtime
);
664 ZFS_TIME_ENCODE(&now
, ctime
);
666 if (vap
->va_mask
& ATTR_ATIME
) {
667 ZFS_TIME_ENCODE(&vap
->va_atime
, atime
);
669 ZFS_TIME_ENCODE(&now
, atime
);
672 if (vap
->va_mask
& ATTR_MTIME
) {
673 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
675 ZFS_TIME_ENCODE(&now
, mtime
);
678 /* Now add in all of the "SA" attributes */
679 VERIFY(0 == sa_handle_get_from_db(zsb
->z_os
, db
, NULL
, SA_HDL_SHARED
,
683 * Setup the array of attributes to be replaced/set on the new file
685 * order for DMU_OT_ZNODE is critical since it needs to be constructed
686 * in the old znode_phys_t format. Don't change this ordering
688 sa_attrs
= kmem_alloc(sizeof (sa_bulk_attr_t
) * ZPL_END
, KM_PUSHPAGE
);
690 if (obj_type
== DMU_OT_ZNODE
) {
691 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zsb
),
693 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zsb
),
695 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zsb
),
697 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zsb
),
699 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zsb
),
701 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zsb
),
703 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zsb
),
705 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zsb
),
708 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zsb
),
710 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zsb
),
712 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zsb
),
714 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zsb
),
715 NULL
, &acl_ids
->z_fuid
, 8);
716 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zsb
),
717 NULL
, &acl_ids
->z_fgid
, 8);
718 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zsb
),
720 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zsb
),
722 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zsb
),
724 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zsb
),
726 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zsb
),
728 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zsb
),
732 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_LINKS(zsb
), NULL
, &links
, 8);
734 if (obj_type
== DMU_OT_ZNODE
) {
735 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_XATTR(zsb
), NULL
,
738 if (obj_type
== DMU_OT_ZNODE
||
739 (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))) {
740 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_RDEV(zsb
),
743 if (obj_type
== DMU_OT_ZNODE
) {
744 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zsb
),
746 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zsb
), NULL
,
747 &acl_ids
->z_fuid
, 8);
748 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zsb
), NULL
,
749 &acl_ids
->z_fgid
, 8);
750 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PAD(zsb
), NULL
, pad
,
751 sizeof (uint64_t) * 4);
752 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ZNODE_ACL(zsb
), NULL
,
753 &acl_phys
, sizeof (zfs_acl_phys_t
));
754 } else if (acl_ids
->z_aclp
->z_version
>= ZFS_ACL_VERSION_FUID
) {
755 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_COUNT(zsb
), NULL
,
756 &acl_ids
->z_aclp
->z_acl_count
, 8);
757 locate
.cb_aclp
= acl_ids
->z_aclp
;
758 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_ACES(zsb
),
759 zfs_acl_data_locator
, &locate
,
760 acl_ids
->z_aclp
->z_acl_bytes
);
761 mode
= zfs_mode_compute(mode
, acl_ids
->z_aclp
, &pflags
,
762 acl_ids
->z_fuid
, acl_ids
->z_fgid
);
765 VERIFY(sa_replace_all_by_template(sa_hdl
, sa_attrs
, cnt
, tx
) == 0);
767 if (!(flag
& IS_ROOT_NODE
)) {
768 *zpp
= zfs_znode_alloc(zsb
, db
, 0, obj_type
, obj
, sa_hdl
,
770 VERIFY(*zpp
!= NULL
);
774 * If we are creating the root node, the "parent" we
775 * passed in is the znode for the root.
779 (*zpp
)->z_sa_hdl
= sa_hdl
;
782 (*zpp
)->z_pflags
= pflags
;
783 (*zpp
)->z_mode
= mode
;
785 if (obj_type
== DMU_OT_ZNODE
||
786 acl_ids
->z_aclp
->z_version
< ZFS_ACL_VERSION_FUID
) {
787 err
= zfs_aclset_common(*zpp
, acl_ids
->z_aclp
, cr
, tx
);
790 kmem_free(sa_attrs
, sizeof (sa_bulk_attr_t
) * ZPL_END
);
791 ZFS_OBJ_HOLD_EXIT(zsb
, obj
);
795 * Update in-core attributes. It is assumed the caller will be doing an
796 * sa_bulk_update to push the changes out.
799 zfs_xvattr_set(znode_t
*zp
, xvattr_t
*xvap
, dmu_tx_t
*tx
)
803 xoap
= xva_getxoptattr(xvap
);
806 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
808 ZFS_TIME_ENCODE(&xoap
->xoa_createtime
, times
);
809 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_CRTIME(ZTOZSB(zp
)),
810 ×
, sizeof (times
), tx
);
811 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
813 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
814 ZFS_ATTR_SET(zp
, ZFS_READONLY
, xoap
->xoa_readonly
,
816 XVA_SET_RTN(xvap
, XAT_READONLY
);
818 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
819 ZFS_ATTR_SET(zp
, ZFS_HIDDEN
, xoap
->xoa_hidden
,
821 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
823 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
824 ZFS_ATTR_SET(zp
, ZFS_SYSTEM
, xoap
->xoa_system
,
826 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
828 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
829 ZFS_ATTR_SET(zp
, ZFS_ARCHIVE
, xoap
->xoa_archive
,
831 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
833 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
834 ZFS_ATTR_SET(zp
, ZFS_IMMUTABLE
, xoap
->xoa_immutable
,
836 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
838 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
839 ZFS_ATTR_SET(zp
, ZFS_NOUNLINK
, xoap
->xoa_nounlink
,
841 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
843 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
844 ZFS_ATTR_SET(zp
, ZFS_APPENDONLY
, xoap
->xoa_appendonly
,
846 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
848 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
849 ZFS_ATTR_SET(zp
, ZFS_NODUMP
, xoap
->xoa_nodump
,
851 XVA_SET_RTN(xvap
, XAT_NODUMP
);
853 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
854 ZFS_ATTR_SET(zp
, ZFS_OPAQUE
, xoap
->xoa_opaque
,
856 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
858 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
859 ZFS_ATTR_SET(zp
, ZFS_AV_QUARANTINED
,
860 xoap
->xoa_av_quarantined
, zp
->z_pflags
, tx
);
861 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
863 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
864 ZFS_ATTR_SET(zp
, ZFS_AV_MODIFIED
, xoap
->xoa_av_modified
,
866 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
868 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
869 zfs_sa_set_scanstamp(zp
, xvap
, tx
);
870 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
872 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
873 ZFS_ATTR_SET(zp
, ZFS_REPARSE
, xoap
->xoa_reparse
,
875 XVA_SET_RTN(xvap
, XAT_REPARSE
);
877 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
878 ZFS_ATTR_SET(zp
, ZFS_OFFLINE
, xoap
->xoa_offline
,
880 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
882 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
883 ZFS_ATTR_SET(zp
, ZFS_SPARSE
, xoap
->xoa_sparse
,
885 XVA_SET_RTN(xvap
, XAT_SPARSE
);
890 zfs_zget(zfs_sb_t
*zsb
, uint64_t obj_num
, znode_t
**zpp
)
892 dmu_object_info_t doi
;
901 ZFS_OBJ_HOLD_ENTER(zsb
, obj_num
);
903 err
= sa_buf_hold(zsb
->z_os
, obj_num
, NULL
, &db
);
905 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
909 dmu_object_info_from_db(db
, &doi
);
910 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
911 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
912 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
913 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
914 sa_buf_rele(db
, NULL
);
915 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
916 return (SET_ERROR(EINVAL
));
919 hdl
= dmu_buf_get_user(db
);
921 zp
= sa_get_userdata(hdl
);
925 * Since "SA" does immediate eviction we
926 * should never find a sa handle that doesn't
927 * know about the znode.
930 ASSERT3P(zp
, !=, NULL
);
932 mutex_enter(&zp
->z_lock
);
933 ASSERT3U(zp
->z_id
, ==, obj_num
);
934 if (zp
->z_unlinked
) {
935 err
= SET_ERROR(ENOENT
);
938 * If igrab() returns NULL the VFS has independently
939 * determined the inode should be evicted and has
940 * called iput_final() to start the eviction process.
941 * The SA handle is still valid but because the VFS
942 * requires that the eviction succeed we must drop
943 * our locks and references to allow the eviction to
944 * complete. The zfs_zget() may then be retried.
946 * This unlikely case could be optimized by registering
947 * a sops->drop_inode() callback. The callback would
948 * need to detect the active SA hold thereby informing
949 * the VFS that this inode should not be evicted.
951 if (igrab(ZTOI(zp
)) == NULL
) {
952 mutex_exit(&zp
->z_lock
);
953 sa_buf_rele(db
, NULL
);
954 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
960 mutex_exit(&zp
->z_lock
);
961 sa_buf_rele(db
, NULL
);
962 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
967 * Not found create new znode/vnode but only if file exists.
969 * There is a small window where zfs_vget() could
970 * find this object while a file create is still in
971 * progress. This is checked for in zfs_znode_alloc()
973 * if zfs_znode_alloc() fails it will drop the hold on the
976 zp
= zfs_znode_alloc(zsb
, db
, doi
.doi_data_block_size
,
977 doi
.doi_bonus_type
, obj_num
, NULL
, NULL
);
979 err
= SET_ERROR(ENOENT
);
983 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
988 zfs_rezget(znode_t
*zp
)
990 zfs_sb_t
*zsb
= ZTOZSB(zp
);
991 dmu_object_info_t doi
;
993 uint64_t obj_num
= zp
->z_id
;
995 sa_bulk_attr_t bulk
[8];
1000 ZFS_OBJ_HOLD_ENTER(zsb
, obj_num
);
1002 mutex_enter(&zp
->z_acl_lock
);
1003 if (zp
->z_acl_cached
) {
1004 zfs_acl_free(zp
->z_acl_cached
);
1005 zp
->z_acl_cached
= NULL
;
1007 mutex_exit(&zp
->z_acl_lock
);
1009 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
1010 if (zp
->z_xattr_cached
) {
1011 nvlist_free(zp
->z_xattr_cached
);
1012 zp
->z_xattr_cached
= NULL
;
1015 if (zp
->z_xattr_parent
) {
1016 iput(ZTOI(zp
->z_xattr_parent
));
1017 zp
->z_xattr_parent
= NULL
;
1019 rw_exit(&zp
->z_xattr_lock
);
1021 ASSERT(zp
->z_sa_hdl
== NULL
);
1022 err
= sa_buf_hold(zsb
->z_os
, obj_num
, NULL
, &db
);
1024 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
1028 dmu_object_info_from_db(db
, &doi
);
1029 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
1030 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
1031 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1032 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
1033 sa_buf_rele(db
, NULL
);
1034 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
1035 return (SET_ERROR(EINVAL
));
1038 zfs_znode_sa_init(zsb
, zp
, db
, doi
.doi_bonus_type
, NULL
);
1040 /* reload cached values */
1041 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zsb
), NULL
,
1042 &gen
, sizeof (gen
));
1043 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
,
1044 &zp
->z_size
, sizeof (zp
->z_size
));
1045 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zsb
), NULL
,
1046 &zp
->z_links
, sizeof (zp
->z_links
));
1047 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
1048 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
1049 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
1050 &zp
->z_atime
, sizeof (zp
->z_atime
));
1051 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
1052 &zp
->z_uid
, sizeof (zp
->z_uid
));
1053 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
), NULL
,
1054 &zp
->z_gid
, sizeof (zp
->z_gid
));
1055 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
1056 &mode
, sizeof (mode
));
1058 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) {
1059 zfs_znode_dmu_fini(zp
);
1060 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
1061 return (SET_ERROR(EIO
));
1066 if (gen
!= zp
->z_gen
) {
1067 zfs_znode_dmu_fini(zp
);
1068 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
1069 return (SET_ERROR(EIO
));
1072 zp
->z_unlinked
= (zp
->z_links
== 0);
1073 zp
->z_blksz
= doi
.doi_data_block_size
;
1074 zfs_inode_update(zp
);
1076 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
1082 zfs_znode_delete(znode_t
*zp
, dmu_tx_t
*tx
)
1084 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1085 objset_t
*os
= zsb
->z_os
;
1086 uint64_t obj
= zp
->z_id
;
1087 uint64_t acl_obj
= zfs_external_acl(zp
);
1089 ZFS_OBJ_HOLD_ENTER(zsb
, obj
);
1091 VERIFY(!zp
->z_is_sa
);
1092 VERIFY(0 == dmu_object_free(os
, acl_obj
, tx
));
1094 VERIFY(0 == dmu_object_free(os
, obj
, tx
));
1095 zfs_znode_dmu_fini(zp
);
1096 ZFS_OBJ_HOLD_EXIT(zsb
, obj
);
1100 zfs_zinactive(znode_t
*zp
)
1102 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1103 uint64_t z_id
= zp
->z_id
;
1104 boolean_t drop_mutex
= 0;
1106 ASSERT(zp
->z_sa_hdl
);
1109 * Don't allow a zfs_zget() while were trying to release this znode.
1111 * Linux allows direct memory reclaim which means that any KM_SLEEP
1112 * allocation may trigger inode eviction. This can lead to a deadlock
1113 * through the ->shrink_icache_memory()->evict()->zfs_inactive()->
1114 * zfs_zinactive() call path. To avoid this deadlock the process
1115 * must not reacquire the mutex when it is already holding it.
1117 if (!ZFS_OBJ_HOLD_OWNED(zsb
, z_id
)) {
1118 ZFS_OBJ_HOLD_ENTER(zsb
, z_id
);
1122 mutex_enter(&zp
->z_lock
);
1125 * If this was the last reference to a file with no links,
1126 * remove the file from the file system.
1128 if (zp
->z_unlinked
) {
1129 mutex_exit(&zp
->z_lock
);
1132 ZFS_OBJ_HOLD_EXIT(zsb
, z_id
);
1138 mutex_exit(&zp
->z_lock
);
1139 zfs_znode_dmu_fini(zp
);
1142 ZFS_OBJ_HOLD_EXIT(zsb
, z_id
);
1146 zfs_compare_timespec(struct timespec
*t1
, struct timespec
*t2
)
1148 if (t1
->tv_sec
< t2
->tv_sec
)
1151 if (t1
->tv_sec
> t2
->tv_sec
)
1154 return (t1
->tv_nsec
- t2
->tv_nsec
);
1158 * Determine whether the znode's atime must be updated. The logic mostly
1159 * duplicates the Linux kernel's relatime_need_update() functionality.
1160 * This function is only called if the underlying filesystem actually has
1161 * atime updates enabled.
1163 static inline boolean_t
1164 zfs_atime_need_update(znode_t
*zp
, timestruc_t
*now
)
1166 if (!ZTOZSB(zp
)->z_relatime
)
1170 * In relatime mode, only update the atime if the previous atime
1171 * is earlier than either the ctime or mtime or if at least a day
1172 * has passed since the last update of atime.
1174 if (zfs_compare_timespec(&ZTOI(zp
)->i_mtime
, &ZTOI(zp
)->i_atime
) >= 0)
1177 if (zfs_compare_timespec(&ZTOI(zp
)->i_ctime
, &ZTOI(zp
)->i_atime
) >= 0)
1180 if ((long)now
->tv_sec
- ZTOI(zp
)->i_atime
.tv_sec
>= 24*60*60)
1187 * Prepare to update znode time stamps.
1189 * IN: zp - znode requiring timestamp update
1190 * flag - ATTR_MTIME, ATTR_CTIME, ATTR_ATIME flags
1191 * have_tx - true of caller is creating a new txg
1193 * OUT: zp - new atime (via underlying inode's i_atime)
1197 * NOTE: The arguments are somewhat redundant. The following condition
1200 * have_tx == !(flag & ATTR_ATIME)
1203 zfs_tstamp_update_setup(znode_t
*zp
, uint_t flag
, uint64_t mtime
[2],
1204 uint64_t ctime
[2], boolean_t have_tx
)
1208 ASSERT(have_tx
== !(flag
& ATTR_ATIME
));
1212 * NOTE: The following test intentionally does not update z_atime_dirty
1213 * in the case where an ATIME update has been requested but for which
1214 * the update is omitted due to relatime logic. The rationale being
1215 * that if the flag was set somewhere else, we should leave it alone
1218 if (flag
& ATTR_ATIME
) {
1219 if (zfs_atime_need_update(zp
, &now
)) {
1220 ZFS_TIME_ENCODE(&now
, zp
->z_atime
);
1221 ZTOI(zp
)->i_atime
.tv_sec
= zp
->z_atime
[0];
1222 ZTOI(zp
)->i_atime
.tv_nsec
= zp
->z_atime
[1];
1223 zp
->z_atime_dirty
= 1;
1226 zp
->z_atime_dirty
= 0;
1230 if (flag
& ATTR_MTIME
) {
1231 ZFS_TIME_ENCODE(&now
, mtime
);
1232 if (ZTOZSB(zp
)->z_use_fuids
) {
1233 zp
->z_pflags
|= (ZFS_ARCHIVE
|
1238 if (flag
& ATTR_CTIME
) {
1239 ZFS_TIME_ENCODE(&now
, ctime
);
1240 if (ZTOZSB(zp
)->z_use_fuids
)
1241 zp
->z_pflags
|= ZFS_ARCHIVE
;
1246 * Grow the block size for a file.
1248 * IN: zp - znode of file to free data in.
1249 * size - requested block size
1250 * tx - open transaction.
1252 * NOTE: this function assumes that the znode is write locked.
1255 zfs_grow_blocksize(znode_t
*zp
, uint64_t size
, dmu_tx_t
*tx
)
1260 if (size
<= zp
->z_blksz
)
1263 * If the file size is already greater than the current blocksize,
1264 * we will not grow. If there is more than one block in a file,
1265 * the blocksize cannot change.
1267 if (zp
->z_blksz
&& zp
->z_size
> zp
->z_blksz
)
1270 error
= dmu_object_set_blocksize(ZTOZSB(zp
)->z_os
, zp
->z_id
,
1273 if (error
== ENOTSUP
)
1277 /* What blocksize did we actually get? */
1278 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &zp
->z_blksz
, &dummy
);
1282 * Increase the file length
1284 * IN: zp - znode of file to free data in.
1285 * end - new end-of-file
1287 * RETURN: 0 on success, error code on failure
1290 zfs_extend(znode_t
*zp
, uint64_t end
)
1292 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1299 * We will change zp_size, lock the whole file.
1301 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1304 * Nothing to do if file already at desired length.
1306 if (end
<= zp
->z_size
) {
1307 zfs_range_unlock(rl
);
1310 tx
= dmu_tx_create(zsb
->z_os
);
1311 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1312 zfs_sa_upgrade_txholds(tx
, zp
);
1313 if (end
> zp
->z_blksz
&&
1314 (!ISP2(zp
->z_blksz
) || zp
->z_blksz
< zsb
->z_max_blksz
)) {
1316 * We are growing the file past the current block size.
1318 if (zp
->z_blksz
> ZTOZSB(zp
)->z_max_blksz
) {
1319 ASSERT(!ISP2(zp
->z_blksz
));
1320 newblksz
= MIN(end
, SPA_MAXBLOCKSIZE
);
1322 newblksz
= MIN(end
, ZTOZSB(zp
)->z_max_blksz
);
1324 dmu_tx_hold_write(tx
, zp
->z_id
, 0, newblksz
);
1329 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1332 zfs_range_unlock(rl
);
1337 zfs_grow_blocksize(zp
, newblksz
, tx
);
1341 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(ZTOZSB(zp
)),
1342 &zp
->z_size
, sizeof (zp
->z_size
), tx
));
1344 zfs_range_unlock(rl
);
1352 * Free space in a file.
1354 * IN: zp - znode of file to free data in.
1355 * off - start of section to free.
1356 * len - length of section to free.
1358 * RETURN: 0 on success, error code on failure
1361 zfs_free_range(znode_t
*zp
, uint64_t off
, uint64_t len
)
1363 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1368 * Lock the range being freed.
1370 rl
= zfs_range_lock(zp
, off
, len
, RL_WRITER
);
1373 * Nothing to do if file already at desired length.
1375 if (off
>= zp
->z_size
) {
1376 zfs_range_unlock(rl
);
1380 if (off
+ len
> zp
->z_size
)
1381 len
= zp
->z_size
- off
;
1383 error
= dmu_free_long_range(zsb
->z_os
, zp
->z_id
, off
, len
);
1385 zfs_range_unlock(rl
);
1393 * IN: zp - znode of file to free data in.
1394 * end - new end-of-file.
1396 * RETURN: 0 on success, error code on failure
1399 zfs_trunc(znode_t
*zp
, uint64_t end
)
1401 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1405 sa_bulk_attr_t bulk
[2];
1409 * We will change zp_size, lock the whole file.
1411 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1414 * Nothing to do if file already at desired length.
1416 if (end
>= zp
->z_size
) {
1417 zfs_range_unlock(rl
);
1421 error
= dmu_free_long_range(zsb
->z_os
, zp
->z_id
, end
, -1);
1423 zfs_range_unlock(rl
);
1427 tx
= dmu_tx_create(zsb
->z_os
);
1428 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1429 zfs_sa_upgrade_txholds(tx
, zp
);
1430 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1432 if (error
== ERESTART
) {
1438 zfs_range_unlock(rl
);
1443 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
),
1444 NULL
, &zp
->z_size
, sizeof (zp
->z_size
));
1447 zp
->z_pflags
&= ~ZFS_SPARSE
;
1448 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
),
1449 NULL
, &zp
->z_pflags
, 8);
1451 VERIFY(sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
) == 0);
1455 zfs_range_unlock(rl
);
1461 * Free space in a file
1463 * IN: zp - znode of file to free data in.
1464 * off - start of range
1465 * len - end of range (0 => EOF)
1466 * flag - current file open mode flags.
1467 * log - TRUE if this action should be logged
1469 * RETURN: 0 on success, error code on failure
1472 zfs_freesp(znode_t
*zp
, uint64_t off
, uint64_t len
, int flag
, boolean_t log
)
1474 struct inode
*ip
= ZTOI(zp
);
1476 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1477 zilog_t
*zilog
= zsb
->z_log
;
1479 uint64_t mtime
[2], ctime
[2];
1480 sa_bulk_attr_t bulk
[3];
1484 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
), &mode
,
1485 sizeof (mode
))) != 0)
1488 if (off
> zp
->z_size
) {
1489 error
= zfs_extend(zp
, off
+len
);
1490 if (error
== 0 && log
)
1497 * Check for any locks in the region to be freed.
1499 if (ip
->i_flock
&& mandatory_lock(ip
)) {
1500 uint64_t length
= (len
? len
: zp
->z_size
- off
);
1501 if (!lock_may_write(ip
, off
, length
))
1502 return (SET_ERROR(EAGAIN
));
1506 error
= zfs_trunc(zp
, off
);
1508 if ((error
= zfs_free_range(zp
, off
, len
)) == 0 &&
1509 off
+ len
> zp
->z_size
)
1510 error
= zfs_extend(zp
, off
+len
);
1515 tx
= dmu_tx_create(zsb
->z_os
);
1516 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1517 zfs_sa_upgrade_txholds(tx
, zp
);
1518 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1524 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, mtime
, 16);
1525 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, ctime
, 16);
1526 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
),
1527 NULL
, &zp
->z_pflags
, 8);
1528 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
, B_TRUE
);
1529 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1532 zfs_log_truncate(zilog
, tx
, TX_TRUNCATE
, zp
, off
, len
);
1535 zfs_inode_update(zp
);
1540 zfs_create_fs(objset_t
*os
, cred_t
*cr
, nvlist_t
*zplprops
, dmu_tx_t
*tx
)
1542 struct super_block
*sb
;
1544 uint64_t moid
, obj
, sa_obj
, version
;
1545 uint64_t sense
= ZFS_CASE_SENSITIVE
;
1550 znode_t
*rootzp
= NULL
;
1553 zfs_acl_ids_t acl_ids
;
1556 * First attempt to create master node.
1559 * In an empty objset, there are no blocks to read and thus
1560 * there can be no i/o errors (which we assert below).
1562 moid
= MASTER_NODE_OBJ
;
1563 error
= zap_create_claim(os
, moid
, DMU_OT_MASTER_NODE
,
1564 DMU_OT_NONE
, 0, tx
);
1568 * Set starting attributes.
1570 version
= zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)));
1572 while ((elem
= nvlist_next_nvpair(zplprops
, elem
)) != NULL
) {
1573 /* For the moment we expect all zpl props to be uint64_ts */
1577 ASSERT(nvpair_type(elem
) == DATA_TYPE_UINT64
);
1578 VERIFY(nvpair_value_uint64(elem
, &val
) == 0);
1579 name
= nvpair_name(elem
);
1580 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_VERSION
)) == 0) {
1584 error
= zap_update(os
, moid
, name
, 8, 1, &val
, tx
);
1587 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_NORMALIZE
)) == 0)
1589 else if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_CASE
)) == 0)
1592 ASSERT(version
!= 0);
1593 error
= zap_update(os
, moid
, ZPL_VERSION_STR
, 8, 1, &version
, tx
);
1596 * Create zap object used for SA attribute registration
1599 if (version
>= ZPL_VERSION_SA
) {
1600 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1601 DMU_OT_NONE
, 0, tx
);
1602 error
= zap_add(os
, moid
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1608 * Create a delete queue.
1610 obj
= zap_create(os
, DMU_OT_UNLINKED_SET
, DMU_OT_NONE
, 0, tx
);
1612 error
= zap_add(os
, moid
, ZFS_UNLINKED_SET
, 8, 1, &obj
, tx
);
1616 * Create root znode. Create minimal znode/inode/zsb/sb
1617 * to allow zfs_mknode to work.
1619 vattr
.va_mask
= ATTR_MODE
|ATTR_UID
|ATTR_GID
;
1620 vattr
.va_mode
= S_IFDIR
|0755;
1621 vattr
.va_uid
= crgetuid(cr
);
1622 vattr
.va_gid
= crgetgid(cr
);
1624 rootzp
= kmem_cache_alloc(znode_cache
, KM_PUSHPAGE
);
1625 rootzp
->z_moved
= 0;
1626 rootzp
->z_unlinked
= 0;
1627 rootzp
->z_atime_dirty
= 0;
1628 rootzp
->z_is_sa
= USE_SA(version
, os
);
1630 zsb
= kmem_zalloc(sizeof (zfs_sb_t
), KM_PUSHPAGE
| KM_NODEBUG
);
1632 zsb
->z_parent
= zsb
;
1633 zsb
->z_version
= version
;
1634 zsb
->z_use_fuids
= USE_FUIDS(version
, os
);
1635 zsb
->z_use_sa
= USE_SA(version
, os
);
1638 sb
= kmem_zalloc(sizeof (struct super_block
), KM_PUSHPAGE
);
1639 sb
->s_fs_info
= zsb
;
1641 ZTOI(rootzp
)->i_sb
= sb
;
1643 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
1644 &zsb
->z_attr_table
);
1649 * Fold case on file systems that are always or sometimes case
1652 if (sense
== ZFS_CASE_INSENSITIVE
|| sense
== ZFS_CASE_MIXED
)
1653 zsb
->z_norm
|= U8_TEXTPREP_TOUPPER
;
1655 mutex_init(&zsb
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1656 list_create(&zsb
->z_all_znodes
, sizeof (znode_t
),
1657 offsetof(znode_t
, z_link_node
));
1659 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1660 mutex_init(&zsb
->z_hold_mtx
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
1662 VERIFY(0 == zfs_acl_ids_create(rootzp
, IS_ROOT_NODE
, &vattr
,
1663 cr
, NULL
, &acl_ids
));
1664 zfs_mknode(rootzp
, &vattr
, tx
, cr
, IS_ROOT_NODE
, &zp
, &acl_ids
);
1665 ASSERT3P(zp
, ==, rootzp
);
1666 error
= zap_add(os
, moid
, ZFS_ROOT_OBJ
, 8, 1, &rootzp
->z_id
, tx
);
1668 zfs_acl_ids_free(&acl_ids
);
1670 atomic_set(&ZTOI(rootzp
)->i_count
, 0);
1671 sa_handle_destroy(rootzp
->z_sa_hdl
);
1672 kmem_cache_free(znode_cache
, rootzp
);
1675 * Create shares directory
1677 error
= zfs_create_share_dir(zsb
, tx
);
1680 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1681 mutex_destroy(&zsb
->z_hold_mtx
[i
]);
1683 kmem_free(sb
, sizeof (struct super_block
));
1684 kmem_free(zsb
, sizeof (zfs_sb_t
));
1686 #endif /* _KERNEL */
1689 zfs_sa_setup(objset_t
*osp
, sa_attr_type_t
**sa_table
)
1691 uint64_t sa_obj
= 0;
1694 error
= zap_lookup(osp
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1695 if (error
!= 0 && error
!= ENOENT
)
1698 error
= sa_setup(osp
, sa_obj
, zfs_attr_table
, ZPL_END
, sa_table
);
1703 zfs_grab_sa_handle(objset_t
*osp
, uint64_t obj
, sa_handle_t
**hdlp
,
1704 dmu_buf_t
**db
, void *tag
)
1706 dmu_object_info_t doi
;
1709 if ((error
= sa_buf_hold(osp
, obj
, tag
, db
)) != 0)
1712 dmu_object_info_from_db(*db
, &doi
);
1713 if ((doi
.doi_bonus_type
!= DMU_OT_SA
&&
1714 doi
.doi_bonus_type
!= DMU_OT_ZNODE
) ||
1715 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1716 doi
.doi_bonus_size
< sizeof (znode_phys_t
))) {
1717 sa_buf_rele(*db
, tag
);
1718 return (SET_ERROR(ENOTSUP
));
1721 error
= sa_handle_get(osp
, obj
, NULL
, SA_HDL_PRIVATE
, hdlp
);
1723 sa_buf_rele(*db
, tag
);
1731 zfs_release_sa_handle(sa_handle_t
*hdl
, dmu_buf_t
*db
, void *tag
)
1733 sa_handle_destroy(hdl
);
1734 sa_buf_rele(db
, tag
);
1738 * Given an object number, return its parent object number and whether
1739 * or not the object is an extended attribute directory.
1742 zfs_obj_to_pobj(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
, uint64_t *pobjp
,
1748 sa_bulk_attr_t bulk
[3];
1752 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_PARENT
], NULL
,
1753 &parent
, sizeof (parent
));
1754 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_FLAGS
], NULL
,
1755 &pflags
, sizeof (pflags
));
1756 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1757 &mode
, sizeof (mode
));
1759 if ((error
= sa_bulk_lookup(hdl
, bulk
, count
)) != 0)
1763 *is_xattrdir
= ((pflags
& ZFS_XATTR
) != 0) && S_ISDIR(mode
);
1769 * Given an object number, return some zpl level statistics
1772 zfs_obj_to_stats_impl(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
1775 sa_bulk_attr_t bulk
[4];
1778 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1779 &sb
->zs_mode
, sizeof (sb
->zs_mode
));
1780 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_GEN
], NULL
,
1781 &sb
->zs_gen
, sizeof (sb
->zs_gen
));
1782 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_LINKS
], NULL
,
1783 &sb
->zs_links
, sizeof (sb
->zs_links
));
1784 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_CTIME
], NULL
,
1785 &sb
->zs_ctime
, sizeof (sb
->zs_ctime
));
1787 return (sa_bulk_lookup(hdl
, bulk
, count
));
1791 zfs_obj_to_path_impl(objset_t
*osp
, uint64_t obj
, sa_handle_t
*hdl
,
1792 sa_attr_type_t
*sa_table
, char *buf
, int len
)
1794 sa_handle_t
*sa_hdl
;
1795 sa_handle_t
*prevhdl
= NULL
;
1796 dmu_buf_t
*prevdb
= NULL
;
1797 dmu_buf_t
*sa_db
= NULL
;
1798 char *path
= buf
+ len
- 1;
1806 char component
[MAXNAMELEN
+ 2];
1808 int is_xattrdir
= 0;
1811 zfs_release_sa_handle(prevhdl
, prevdb
, FTAG
);
1813 if ((error
= zfs_obj_to_pobj(sa_hdl
, sa_table
, &pobj
,
1814 &is_xattrdir
)) != 0)
1825 (void) sprintf(component
+ 1, "<xattrdir>");
1827 error
= zap_value_search(osp
, pobj
, obj
,
1828 ZFS_DIRENT_OBJ(-1ULL), component
+ 1);
1833 complen
= strlen(component
);
1835 ASSERT(path
>= buf
);
1836 bcopy(component
, path
, complen
);
1839 if (sa_hdl
!= hdl
) {
1843 error
= zfs_grab_sa_handle(osp
, obj
, &sa_hdl
, &sa_db
, FTAG
);
1851 if (sa_hdl
!= NULL
&& sa_hdl
!= hdl
) {
1852 ASSERT(sa_db
!= NULL
);
1853 zfs_release_sa_handle(sa_hdl
, sa_db
, FTAG
);
1857 (void) memmove(buf
, path
, buf
+ len
- path
);
1863 zfs_obj_to_path(objset_t
*osp
, uint64_t obj
, char *buf
, int len
)
1865 sa_attr_type_t
*sa_table
;
1870 error
= zfs_sa_setup(osp
, &sa_table
);
1874 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
, FTAG
);
1878 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1880 zfs_release_sa_handle(hdl
, db
, FTAG
);
1885 zfs_obj_to_stats(objset_t
*osp
, uint64_t obj
, zfs_stat_t
*sb
,
1888 char *path
= buf
+ len
- 1;
1889 sa_attr_type_t
*sa_table
;
1896 error
= zfs_sa_setup(osp
, &sa_table
);
1900 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
, FTAG
);
1904 error
= zfs_obj_to_stats_impl(hdl
, sa_table
, sb
);
1906 zfs_release_sa_handle(hdl
, db
, FTAG
);
1910 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1912 zfs_release_sa_handle(hdl
, db
, FTAG
);
1916 #if defined(_KERNEL) && defined(HAVE_SPL)
1917 EXPORT_SYMBOL(zfs_create_fs
);
1918 EXPORT_SYMBOL(zfs_obj_to_path
);