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_SLEEP
);
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_SLEEP
);
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 zfs_iput_async(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
);
563 dmu_object_type_t obj_type
;
564 sa_bulk_attr_t
*sa_attrs
;
566 zfs_acl_locator_cb_t locate
= { 0 };
569 obj
= vap
->va_nodeid
;
570 now
= vap
->va_ctime
; /* see zfs_replay_create() */
571 gen
= vap
->va_nblocks
; /* ditto */
575 gen
= dmu_tx_get_txg(tx
);
578 obj_type
= zsb
->z_use_sa
? DMU_OT_SA
: DMU_OT_ZNODE
;
579 bonuslen
= (obj_type
== DMU_OT_SA
) ?
580 DN_MAX_BONUSLEN
: ZFS_OLD_ZNODE_PHYS_SIZE
;
583 * Create a new DMU object.
586 * There's currently no mechanism for pre-reading the blocks that will
587 * be needed to allocate a new object, so we accept the small chance
588 * that there will be an i/o error and we will fail one of the
591 if (S_ISDIR(vap
->va_mode
)) {
593 VERIFY0(zap_create_claim_norm(zsb
->z_os
, obj
,
594 zsb
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
595 obj_type
, bonuslen
, tx
));
597 obj
= zap_create_norm(zsb
->z_os
,
598 zsb
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
599 obj_type
, bonuslen
, tx
);
603 VERIFY0(dmu_object_claim(zsb
->z_os
, obj
,
604 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
605 obj_type
, bonuslen
, tx
));
607 obj
= dmu_object_alloc(zsb
->z_os
,
608 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
609 obj_type
, bonuslen
, tx
);
613 ZFS_OBJ_HOLD_ENTER(zsb
, obj
);
614 VERIFY(0 == sa_buf_hold(zsb
->z_os
, obj
, NULL
, &db
));
617 * If this is the root, fix up the half-initialized parent pointer
618 * to reference the just-allocated physical data area.
620 if (flag
& IS_ROOT_NODE
) {
623 dzp_pflags
= dzp
->z_pflags
;
627 * If parent is an xattr, so am I.
629 if (dzp_pflags
& ZFS_XATTR
) {
633 if (zsb
->z_use_fuids
)
634 pflags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
638 if (S_ISDIR(vap
->va_mode
)) {
639 size
= 2; /* contents ("." and "..") */
640 links
= (flag
& (IS_ROOT_NODE
| IS_XATTR
)) ? 2 : 1;
645 if (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))
649 mode
= acl_ids
->z_mode
;
654 * No execs denied will be deterimed when zfs_mode_compute() is called.
656 pflags
|= acl_ids
->z_aclp
->z_hints
&
657 (ZFS_ACL_TRIVIAL
|ZFS_INHERIT_ACE
|ZFS_ACL_AUTO_INHERIT
|
658 ZFS_ACL_DEFAULTED
|ZFS_ACL_PROTECTED
);
660 ZFS_TIME_ENCODE(&now
, crtime
);
661 ZFS_TIME_ENCODE(&now
, ctime
);
663 if (vap
->va_mask
& ATTR_ATIME
) {
664 ZFS_TIME_ENCODE(&vap
->va_atime
, atime
);
666 ZFS_TIME_ENCODE(&now
, atime
);
669 if (vap
->va_mask
& ATTR_MTIME
) {
670 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
672 ZFS_TIME_ENCODE(&now
, mtime
);
675 /* Now add in all of the "SA" attributes */
676 VERIFY(0 == sa_handle_get_from_db(zsb
->z_os
, db
, NULL
, SA_HDL_SHARED
,
680 * Setup the array of attributes to be replaced/set on the new file
682 * order for DMU_OT_ZNODE is critical since it needs to be constructed
683 * in the old znode_phys_t format. Don't change this ordering
685 sa_attrs
= kmem_alloc(sizeof (sa_bulk_attr_t
) * ZPL_END
, KM_SLEEP
);
687 if (obj_type
== DMU_OT_ZNODE
) {
688 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zsb
),
690 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zsb
),
692 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zsb
),
694 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zsb
),
696 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zsb
),
698 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zsb
),
700 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zsb
),
702 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zsb
),
705 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zsb
),
707 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zsb
),
709 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zsb
),
711 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zsb
),
712 NULL
, &acl_ids
->z_fuid
, 8);
713 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zsb
),
714 NULL
, &acl_ids
->z_fgid
, 8);
715 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zsb
),
717 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zsb
),
719 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zsb
),
721 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zsb
),
723 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zsb
),
725 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zsb
),
729 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_LINKS(zsb
), NULL
, &links
, 8);
731 if (obj_type
== DMU_OT_ZNODE
) {
732 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_XATTR(zsb
), NULL
,
735 if (obj_type
== DMU_OT_ZNODE
||
736 (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))) {
737 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_RDEV(zsb
),
740 if (obj_type
== DMU_OT_ZNODE
) {
741 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zsb
),
743 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zsb
), NULL
,
744 &acl_ids
->z_fuid
, 8);
745 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zsb
), NULL
,
746 &acl_ids
->z_fgid
, 8);
747 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PAD(zsb
), NULL
, pad
,
748 sizeof (uint64_t) * 4);
749 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ZNODE_ACL(zsb
), NULL
,
750 &acl_phys
, sizeof (zfs_acl_phys_t
));
751 } else if (acl_ids
->z_aclp
->z_version
>= ZFS_ACL_VERSION_FUID
) {
752 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_COUNT(zsb
), NULL
,
753 &acl_ids
->z_aclp
->z_acl_count
, 8);
754 locate
.cb_aclp
= acl_ids
->z_aclp
;
755 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_ACES(zsb
),
756 zfs_acl_data_locator
, &locate
,
757 acl_ids
->z_aclp
->z_acl_bytes
);
758 mode
= zfs_mode_compute(mode
, acl_ids
->z_aclp
, &pflags
,
759 acl_ids
->z_fuid
, acl_ids
->z_fgid
);
762 VERIFY(sa_replace_all_by_template(sa_hdl
, sa_attrs
, cnt
, tx
) == 0);
764 if (!(flag
& IS_ROOT_NODE
)) {
765 *zpp
= zfs_znode_alloc(zsb
, db
, 0, obj_type
, obj
, sa_hdl
,
767 VERIFY(*zpp
!= NULL
);
771 * If we are creating the root node, the "parent" we
772 * passed in is the znode for the root.
776 (*zpp
)->z_sa_hdl
= sa_hdl
;
779 (*zpp
)->z_pflags
= pflags
;
780 (*zpp
)->z_mode
= mode
;
782 if (obj_type
== DMU_OT_ZNODE
||
783 acl_ids
->z_aclp
->z_version
< ZFS_ACL_VERSION_FUID
) {
784 VERIFY0(zfs_aclset_common(*zpp
, acl_ids
->z_aclp
, cr
, tx
));
786 kmem_free(sa_attrs
, sizeof (sa_bulk_attr_t
) * ZPL_END
);
787 ZFS_OBJ_HOLD_EXIT(zsb
, obj
);
791 * Update in-core attributes. It is assumed the caller will be doing an
792 * sa_bulk_update to push the changes out.
795 zfs_xvattr_set(znode_t
*zp
, xvattr_t
*xvap
, dmu_tx_t
*tx
)
799 xoap
= xva_getxoptattr(xvap
);
802 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
804 ZFS_TIME_ENCODE(&xoap
->xoa_createtime
, times
);
805 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_CRTIME(ZTOZSB(zp
)),
806 ×
, sizeof (times
), tx
);
807 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
809 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
810 ZFS_ATTR_SET(zp
, ZFS_READONLY
, xoap
->xoa_readonly
,
812 XVA_SET_RTN(xvap
, XAT_READONLY
);
814 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
815 ZFS_ATTR_SET(zp
, ZFS_HIDDEN
, xoap
->xoa_hidden
,
817 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
819 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
820 ZFS_ATTR_SET(zp
, ZFS_SYSTEM
, xoap
->xoa_system
,
822 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
824 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
825 ZFS_ATTR_SET(zp
, ZFS_ARCHIVE
, xoap
->xoa_archive
,
827 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
829 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
830 ZFS_ATTR_SET(zp
, ZFS_IMMUTABLE
, xoap
->xoa_immutable
,
832 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
834 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
835 ZFS_ATTR_SET(zp
, ZFS_NOUNLINK
, xoap
->xoa_nounlink
,
837 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
839 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
840 ZFS_ATTR_SET(zp
, ZFS_APPENDONLY
, xoap
->xoa_appendonly
,
842 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
844 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
845 ZFS_ATTR_SET(zp
, ZFS_NODUMP
, xoap
->xoa_nodump
,
847 XVA_SET_RTN(xvap
, XAT_NODUMP
);
849 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
850 ZFS_ATTR_SET(zp
, ZFS_OPAQUE
, xoap
->xoa_opaque
,
852 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
854 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
855 ZFS_ATTR_SET(zp
, ZFS_AV_QUARANTINED
,
856 xoap
->xoa_av_quarantined
, zp
->z_pflags
, tx
);
857 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
859 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
860 ZFS_ATTR_SET(zp
, ZFS_AV_MODIFIED
, xoap
->xoa_av_modified
,
862 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
864 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
865 zfs_sa_set_scanstamp(zp
, xvap
, tx
);
866 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
868 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
869 ZFS_ATTR_SET(zp
, ZFS_REPARSE
, xoap
->xoa_reparse
,
871 XVA_SET_RTN(xvap
, XAT_REPARSE
);
873 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
874 ZFS_ATTR_SET(zp
, ZFS_OFFLINE
, xoap
->xoa_offline
,
876 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
878 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
879 ZFS_ATTR_SET(zp
, ZFS_SPARSE
, xoap
->xoa_sparse
,
881 XVA_SET_RTN(xvap
, XAT_SPARSE
);
886 zfs_zget(zfs_sb_t
*zsb
, uint64_t obj_num
, znode_t
**zpp
)
888 dmu_object_info_t doi
;
897 ZFS_OBJ_HOLD_ENTER(zsb
, obj_num
);
899 err
= sa_buf_hold(zsb
->z_os
, obj_num
, NULL
, &db
);
901 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
905 dmu_object_info_from_db(db
, &doi
);
906 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
907 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
908 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
909 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
910 sa_buf_rele(db
, NULL
);
911 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
912 return (SET_ERROR(EINVAL
));
915 hdl
= dmu_buf_get_user(db
);
917 zp
= sa_get_userdata(hdl
);
921 * Since "SA" does immediate eviction we
922 * should never find a sa handle that doesn't
923 * know about the znode.
926 ASSERT3P(zp
, !=, NULL
);
928 mutex_enter(&zp
->z_lock
);
929 ASSERT3U(zp
->z_id
, ==, obj_num
);
930 if (zp
->z_unlinked
) {
931 err
= SET_ERROR(ENOENT
);
934 * If igrab() returns NULL the VFS has independently
935 * determined the inode should be evicted and has
936 * called iput_final() to start the eviction process.
937 * The SA handle is still valid but because the VFS
938 * requires that the eviction succeed we must drop
939 * our locks and references to allow the eviction to
940 * complete. The zfs_zget() may then be retried.
942 * This unlikely case could be optimized by registering
943 * a sops->drop_inode() callback. The callback would
944 * need to detect the active SA hold thereby informing
945 * the VFS that this inode should not be evicted.
947 if (igrab(ZTOI(zp
)) == NULL
) {
948 mutex_exit(&zp
->z_lock
);
949 sa_buf_rele(db
, NULL
);
950 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
956 mutex_exit(&zp
->z_lock
);
957 sa_buf_rele(db
, NULL
);
958 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
963 * Not found create new znode/vnode but only if file exists.
965 * There is a small window where zfs_vget() could
966 * find this object while a file create is still in
967 * progress. This is checked for in zfs_znode_alloc()
969 * if zfs_znode_alloc() fails it will drop the hold on the
972 zp
= zfs_znode_alloc(zsb
, db
, doi
.doi_data_block_size
,
973 doi
.doi_bonus_type
, obj_num
, NULL
, NULL
);
975 err
= SET_ERROR(ENOENT
);
979 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
984 zfs_rezget(znode_t
*zp
)
986 zfs_sb_t
*zsb
= ZTOZSB(zp
);
987 dmu_object_info_t doi
;
989 uint64_t obj_num
= zp
->z_id
;
991 sa_bulk_attr_t bulk
[8];
996 ZFS_OBJ_HOLD_ENTER(zsb
, obj_num
);
998 mutex_enter(&zp
->z_acl_lock
);
999 if (zp
->z_acl_cached
) {
1000 zfs_acl_free(zp
->z_acl_cached
);
1001 zp
->z_acl_cached
= NULL
;
1003 mutex_exit(&zp
->z_acl_lock
);
1005 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
1006 if (zp
->z_xattr_cached
) {
1007 nvlist_free(zp
->z_xattr_cached
);
1008 zp
->z_xattr_cached
= NULL
;
1011 if (zp
->z_xattr_parent
) {
1012 zfs_iput_async(ZTOI(zp
->z_xattr_parent
));
1013 zp
->z_xattr_parent
= NULL
;
1015 rw_exit(&zp
->z_xattr_lock
);
1017 ASSERT(zp
->z_sa_hdl
== NULL
);
1018 err
= sa_buf_hold(zsb
->z_os
, obj_num
, NULL
, &db
);
1020 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
1024 dmu_object_info_from_db(db
, &doi
);
1025 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
1026 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
1027 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1028 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
1029 sa_buf_rele(db
, NULL
);
1030 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
1031 return (SET_ERROR(EINVAL
));
1034 zfs_znode_sa_init(zsb
, zp
, db
, doi
.doi_bonus_type
, NULL
);
1036 /* reload cached values */
1037 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zsb
), NULL
,
1038 &gen
, sizeof (gen
));
1039 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
,
1040 &zp
->z_size
, sizeof (zp
->z_size
));
1041 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zsb
), NULL
,
1042 &zp
->z_links
, sizeof (zp
->z_links
));
1043 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
1044 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
1045 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
1046 &zp
->z_atime
, sizeof (zp
->z_atime
));
1047 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
1048 &zp
->z_uid
, sizeof (zp
->z_uid
));
1049 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
), NULL
,
1050 &zp
->z_gid
, sizeof (zp
->z_gid
));
1051 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
1052 &mode
, sizeof (mode
));
1054 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) {
1055 zfs_znode_dmu_fini(zp
);
1056 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
1057 return (SET_ERROR(EIO
));
1062 if (gen
!= zp
->z_gen
) {
1063 zfs_znode_dmu_fini(zp
);
1064 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
1065 return (SET_ERROR(EIO
));
1068 zp
->z_unlinked
= (zp
->z_links
== 0);
1069 zp
->z_blksz
= doi
.doi_data_block_size
;
1070 zfs_inode_update(zp
);
1072 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
1078 zfs_znode_delete(znode_t
*zp
, dmu_tx_t
*tx
)
1080 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1081 objset_t
*os
= zsb
->z_os
;
1082 uint64_t obj
= zp
->z_id
;
1083 uint64_t acl_obj
= zfs_external_acl(zp
);
1085 ZFS_OBJ_HOLD_ENTER(zsb
, obj
);
1087 VERIFY(!zp
->z_is_sa
);
1088 VERIFY(0 == dmu_object_free(os
, acl_obj
, tx
));
1090 VERIFY(0 == dmu_object_free(os
, obj
, tx
));
1091 zfs_znode_dmu_fini(zp
);
1092 ZFS_OBJ_HOLD_EXIT(zsb
, obj
);
1096 zfs_zinactive(znode_t
*zp
)
1098 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1099 uint64_t z_id
= zp
->z_id
;
1101 ASSERT(zp
->z_sa_hdl
);
1104 * Don't allow a zfs_zget() while were trying to release this znode.
1106 ZFS_OBJ_HOLD_ENTER(zsb
, z_id
);
1108 mutex_enter(&zp
->z_lock
);
1111 * If this was the last reference to a file with no links,
1112 * remove the file from the file system.
1114 if (zp
->z_unlinked
) {
1115 mutex_exit(&zp
->z_lock
);
1117 ZFS_OBJ_HOLD_EXIT(zsb
, z_id
);
1123 mutex_exit(&zp
->z_lock
);
1124 zfs_znode_dmu_fini(zp
);
1126 ZFS_OBJ_HOLD_EXIT(zsb
, z_id
);
1130 zfs_compare_timespec(struct timespec
*t1
, struct timespec
*t2
)
1132 if (t1
->tv_sec
< t2
->tv_sec
)
1135 if (t1
->tv_sec
> t2
->tv_sec
)
1138 return (t1
->tv_nsec
- t2
->tv_nsec
);
1142 * Determine whether the znode's atime must be updated. The logic mostly
1143 * duplicates the Linux kernel's relatime_need_update() functionality.
1144 * This function is only called if the underlying filesystem actually has
1145 * atime updates enabled.
1147 static inline boolean_t
1148 zfs_atime_need_update(znode_t
*zp
, timestruc_t
*now
)
1150 if (!ZTOZSB(zp
)->z_relatime
)
1154 * In relatime mode, only update the atime if the previous atime
1155 * is earlier than either the ctime or mtime or if at least a day
1156 * has passed since the last update of atime.
1158 if (zfs_compare_timespec(&ZTOI(zp
)->i_mtime
, &ZTOI(zp
)->i_atime
) >= 0)
1161 if (zfs_compare_timespec(&ZTOI(zp
)->i_ctime
, &ZTOI(zp
)->i_atime
) >= 0)
1164 if ((long)now
->tv_sec
- ZTOI(zp
)->i_atime
.tv_sec
>= 24*60*60)
1171 * Prepare to update znode time stamps.
1173 * IN: zp - znode requiring timestamp update
1174 * flag - ATTR_MTIME, ATTR_CTIME, ATTR_ATIME flags
1175 * have_tx - true of caller is creating a new txg
1177 * OUT: zp - new atime (via underlying inode's i_atime)
1181 * NOTE: The arguments are somewhat redundant. The following condition
1184 * have_tx == !(flag & ATTR_ATIME)
1187 zfs_tstamp_update_setup(znode_t
*zp
, uint_t flag
, uint64_t mtime
[2],
1188 uint64_t ctime
[2], boolean_t have_tx
)
1192 ASSERT(have_tx
== !(flag
& ATTR_ATIME
));
1196 * NOTE: The following test intentionally does not update z_atime_dirty
1197 * in the case where an ATIME update has been requested but for which
1198 * the update is omitted due to relatime logic. The rationale being
1199 * that if the flag was set somewhere else, we should leave it alone
1202 if (flag
& ATTR_ATIME
) {
1203 if (zfs_atime_need_update(zp
, &now
)) {
1204 ZFS_TIME_ENCODE(&now
, zp
->z_atime
);
1205 ZTOI(zp
)->i_atime
.tv_sec
= zp
->z_atime
[0];
1206 ZTOI(zp
)->i_atime
.tv_nsec
= zp
->z_atime
[1];
1207 zp
->z_atime_dirty
= 1;
1210 zp
->z_atime_dirty
= 0;
1214 if (flag
& ATTR_MTIME
) {
1215 ZFS_TIME_ENCODE(&now
, mtime
);
1216 if (ZTOZSB(zp
)->z_use_fuids
) {
1217 zp
->z_pflags
|= (ZFS_ARCHIVE
|
1222 if (flag
& ATTR_CTIME
) {
1223 ZFS_TIME_ENCODE(&now
, ctime
);
1224 if (ZTOZSB(zp
)->z_use_fuids
)
1225 zp
->z_pflags
|= ZFS_ARCHIVE
;
1230 * Grow the block size for a file.
1232 * IN: zp - znode of file to free data in.
1233 * size - requested block size
1234 * tx - open transaction.
1236 * NOTE: this function assumes that the znode is write locked.
1239 zfs_grow_blocksize(znode_t
*zp
, uint64_t size
, dmu_tx_t
*tx
)
1244 if (size
<= zp
->z_blksz
)
1247 * If the file size is already greater than the current blocksize,
1248 * we will not grow. If there is more than one block in a file,
1249 * the blocksize cannot change.
1251 if (zp
->z_blksz
&& zp
->z_size
> zp
->z_blksz
)
1254 error
= dmu_object_set_blocksize(ZTOZSB(zp
)->z_os
, zp
->z_id
,
1257 if (error
== ENOTSUP
)
1261 /* What blocksize did we actually get? */
1262 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &zp
->z_blksz
, &dummy
);
1266 * Increase the file length
1268 * IN: zp - znode of file to free data in.
1269 * end - new end-of-file
1271 * RETURN: 0 on success, error code on failure
1274 zfs_extend(znode_t
*zp
, uint64_t end
)
1276 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1283 * We will change zp_size, lock the whole file.
1285 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1288 * Nothing to do if file already at desired length.
1290 if (end
<= zp
->z_size
) {
1291 zfs_range_unlock(rl
);
1294 tx
= dmu_tx_create(zsb
->z_os
);
1295 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1296 zfs_sa_upgrade_txholds(tx
, zp
);
1297 if (end
> zp
->z_blksz
&&
1298 (!ISP2(zp
->z_blksz
) || zp
->z_blksz
< zsb
->z_max_blksz
)) {
1300 * We are growing the file past the current block size.
1302 if (zp
->z_blksz
> ZTOZSB(zp
)->z_max_blksz
) {
1303 ASSERT(!ISP2(zp
->z_blksz
));
1304 newblksz
= MIN(end
, SPA_MAXBLOCKSIZE
);
1306 newblksz
= MIN(end
, ZTOZSB(zp
)->z_max_blksz
);
1308 dmu_tx_hold_write(tx
, zp
->z_id
, 0, newblksz
);
1313 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1316 zfs_range_unlock(rl
);
1321 zfs_grow_blocksize(zp
, newblksz
, tx
);
1325 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(ZTOZSB(zp
)),
1326 &zp
->z_size
, sizeof (zp
->z_size
), tx
));
1328 zfs_range_unlock(rl
);
1336 * zfs_zero_partial_page - Modeled after update_pages() but
1337 * with different arguments and semantics for use by zfs_freesp().
1339 * Zeroes a piece of a single page cache entry for zp at offset
1340 * start and length len.
1342 * Caller must acquire a range lock on the file for the region
1343 * being zeroed in order that the ARC and page cache stay in sync.
1346 zfs_zero_partial_page(znode_t
*zp
, uint64_t start
, uint64_t len
)
1348 struct address_space
*mp
= ZTOI(zp
)->i_mapping
;
1353 ASSERT((start
& PAGE_CACHE_MASK
) ==
1354 ((start
+ len
- 1) & PAGE_CACHE_MASK
));
1356 off
= start
& (PAGE_CACHE_SIZE
- 1);
1357 start
&= PAGE_CACHE_MASK
;
1359 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_SHIFT
);
1361 if (mapping_writably_mapped(mp
))
1362 flush_dcache_page(pp
);
1365 bzero(pb
+ off
, len
);
1368 if (mapping_writably_mapped(mp
))
1369 flush_dcache_page(pp
);
1371 mark_page_accessed(pp
);
1372 SetPageUptodate(pp
);
1375 page_cache_release(pp
);
1380 * Free space in a file.
1382 * IN: zp - znode of file to free data in.
1383 * off - start of section to free.
1384 * len - length of section to free.
1386 * RETURN: 0 on success, error code on failure
1389 zfs_free_range(znode_t
*zp
, uint64_t off
, uint64_t len
)
1391 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1396 * Lock the range being freed.
1398 rl
= zfs_range_lock(zp
, off
, len
, RL_WRITER
);
1401 * Nothing to do if file already at desired length.
1403 if (off
>= zp
->z_size
) {
1404 zfs_range_unlock(rl
);
1408 if (off
+ len
> zp
->z_size
)
1409 len
= zp
->z_size
- off
;
1411 error
= dmu_free_long_range(zsb
->z_os
, zp
->z_id
, off
, len
);
1414 * Zero partial page cache entries. This must be done under a
1415 * range lock in order to keep the ARC and page cache in sync.
1417 if (zp
->z_is_mapped
) {
1418 loff_t first_page
, last_page
, page_len
;
1419 loff_t first_page_offset
, last_page_offset
;
1421 /* first possible full page in hole */
1422 first_page
= (off
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1423 /* last page of hole */
1424 last_page
= (off
+ len
) >> PAGE_CACHE_SHIFT
;
1426 /* offset of first_page */
1427 first_page_offset
= first_page
<< PAGE_CACHE_SHIFT
;
1428 /* offset of last_page */
1429 last_page_offset
= last_page
<< PAGE_CACHE_SHIFT
;
1431 /* truncate whole pages */
1432 if (last_page_offset
> first_page_offset
) {
1433 truncate_inode_pages_range(ZTOI(zp
)->i_mapping
,
1434 first_page_offset
, last_page_offset
- 1);
1437 /* truncate sub-page ranges */
1438 if (first_page
> last_page
) {
1439 /* entire punched area within a single page */
1440 zfs_zero_partial_page(zp
, off
, len
);
1442 /* beginning of punched area at the end of a page */
1443 page_len
= first_page_offset
- off
;
1445 zfs_zero_partial_page(zp
, off
, page_len
);
1447 /* end of punched area at the beginning of a page */
1448 page_len
= off
+ len
- last_page_offset
;
1450 zfs_zero_partial_page(zp
, last_page_offset
,
1454 zfs_range_unlock(rl
);
1462 * IN: zp - znode of file to free data in.
1463 * end - new end-of-file.
1465 * RETURN: 0 on success, error code on failure
1468 zfs_trunc(znode_t
*zp
, uint64_t end
)
1470 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1474 sa_bulk_attr_t bulk
[2];
1478 * We will change zp_size, lock the whole file.
1480 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1483 * Nothing to do if file already at desired length.
1485 if (end
>= zp
->z_size
) {
1486 zfs_range_unlock(rl
);
1490 error
= dmu_free_long_range(zsb
->z_os
, zp
->z_id
, end
, -1);
1492 zfs_range_unlock(rl
);
1495 tx
= dmu_tx_create(zsb
->z_os
);
1496 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1497 zfs_sa_upgrade_txholds(tx
, zp
);
1498 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1501 zfs_range_unlock(rl
);
1506 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
),
1507 NULL
, &zp
->z_size
, sizeof (zp
->z_size
));
1510 zp
->z_pflags
&= ~ZFS_SPARSE
;
1511 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
),
1512 NULL
, &zp
->z_pflags
, 8);
1514 VERIFY(sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
) == 0);
1518 zfs_range_unlock(rl
);
1524 * Free space in a file
1526 * IN: zp - znode of file to free data in.
1527 * off - start of range
1528 * len - end of range (0 => EOF)
1529 * flag - current file open mode flags.
1530 * log - TRUE if this action should be logged
1532 * RETURN: 0 on success, error code on failure
1535 zfs_freesp(znode_t
*zp
, uint64_t off
, uint64_t len
, int flag
, boolean_t log
)
1538 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1539 zilog_t
*zilog
= zsb
->z_log
;
1541 uint64_t mtime
[2], ctime
[2];
1542 sa_bulk_attr_t bulk
[3];
1546 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
), &mode
,
1547 sizeof (mode
))) != 0)
1550 if (off
> zp
->z_size
) {
1551 error
= zfs_extend(zp
, off
+len
);
1552 if (error
== 0 && log
)
1558 error
= zfs_trunc(zp
, off
);
1560 if ((error
= zfs_free_range(zp
, off
, len
)) == 0 &&
1561 off
+ len
> zp
->z_size
)
1562 error
= zfs_extend(zp
, off
+len
);
1567 tx
= dmu_tx_create(zsb
->z_os
);
1568 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1569 zfs_sa_upgrade_txholds(tx
, zp
);
1570 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1576 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, mtime
, 16);
1577 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, ctime
, 16);
1578 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
),
1579 NULL
, &zp
->z_pflags
, 8);
1580 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
, B_TRUE
);
1581 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1584 zfs_log_truncate(zilog
, tx
, TX_TRUNCATE
, zp
, off
, len
);
1588 zfs_inode_update(zp
);
1593 * Truncate the page cache - for file truncate operations, use
1594 * the purpose-built API for truncations. For punching operations,
1595 * the truncation is handled under a range lock in zfs_free_range.
1598 truncate_setsize(ZTOI(zp
), off
);
1603 zfs_create_fs(objset_t
*os
, cred_t
*cr
, nvlist_t
*zplprops
, dmu_tx_t
*tx
)
1605 struct super_block
*sb
;
1607 uint64_t moid
, obj
, sa_obj
, version
;
1608 uint64_t sense
= ZFS_CASE_SENSITIVE
;
1613 znode_t
*rootzp
= NULL
;
1616 zfs_acl_ids_t acl_ids
;
1619 * First attempt to create master node.
1622 * In an empty objset, there are no blocks to read and thus
1623 * there can be no i/o errors (which we assert below).
1625 moid
= MASTER_NODE_OBJ
;
1626 error
= zap_create_claim(os
, moid
, DMU_OT_MASTER_NODE
,
1627 DMU_OT_NONE
, 0, tx
);
1631 * Set starting attributes.
1633 version
= zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)));
1635 while ((elem
= nvlist_next_nvpair(zplprops
, elem
)) != NULL
) {
1636 /* For the moment we expect all zpl props to be uint64_ts */
1640 ASSERT(nvpair_type(elem
) == DATA_TYPE_UINT64
);
1641 VERIFY(nvpair_value_uint64(elem
, &val
) == 0);
1642 name
= nvpair_name(elem
);
1643 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_VERSION
)) == 0) {
1647 error
= zap_update(os
, moid
, name
, 8, 1, &val
, tx
);
1650 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_NORMALIZE
)) == 0)
1652 else if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_CASE
)) == 0)
1655 ASSERT(version
!= 0);
1656 error
= zap_update(os
, moid
, ZPL_VERSION_STR
, 8, 1, &version
, tx
);
1659 * Create zap object used for SA attribute registration
1662 if (version
>= ZPL_VERSION_SA
) {
1663 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1664 DMU_OT_NONE
, 0, tx
);
1665 error
= zap_add(os
, moid
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1671 * Create a delete queue.
1673 obj
= zap_create(os
, DMU_OT_UNLINKED_SET
, DMU_OT_NONE
, 0, tx
);
1675 error
= zap_add(os
, moid
, ZFS_UNLINKED_SET
, 8, 1, &obj
, tx
);
1679 * Create root znode. Create minimal znode/inode/zsb/sb
1680 * to allow zfs_mknode to work.
1682 vattr
.va_mask
= ATTR_MODE
|ATTR_UID
|ATTR_GID
;
1683 vattr
.va_mode
= S_IFDIR
|0755;
1684 vattr
.va_uid
= crgetuid(cr
);
1685 vattr
.va_gid
= crgetgid(cr
);
1687 rootzp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
1688 rootzp
->z_moved
= 0;
1689 rootzp
->z_unlinked
= 0;
1690 rootzp
->z_atime_dirty
= 0;
1691 rootzp
->z_is_sa
= USE_SA(version
, os
);
1693 zsb
= kmem_zalloc(sizeof (zfs_sb_t
), KM_SLEEP
);
1695 zsb
->z_parent
= zsb
;
1696 zsb
->z_version
= version
;
1697 zsb
->z_use_fuids
= USE_FUIDS(version
, os
);
1698 zsb
->z_use_sa
= USE_SA(version
, os
);
1701 sb
= kmem_zalloc(sizeof (struct super_block
), KM_SLEEP
);
1702 sb
->s_fs_info
= zsb
;
1704 ZTOI(rootzp
)->i_sb
= sb
;
1706 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
1707 &zsb
->z_attr_table
);
1712 * Fold case on file systems that are always or sometimes case
1715 if (sense
== ZFS_CASE_INSENSITIVE
|| sense
== ZFS_CASE_MIXED
)
1716 zsb
->z_norm
|= U8_TEXTPREP_TOUPPER
;
1718 mutex_init(&zsb
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1719 list_create(&zsb
->z_all_znodes
, sizeof (znode_t
),
1720 offsetof(znode_t
, z_link_node
));
1722 zsb
->z_hold_mtx
= vmem_zalloc(sizeof (kmutex_t
) * ZFS_OBJ_MTX_SZ
,
1724 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1725 mutex_init(&zsb
->z_hold_mtx
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
1727 VERIFY(0 == zfs_acl_ids_create(rootzp
, IS_ROOT_NODE
, &vattr
,
1728 cr
, NULL
, &acl_ids
));
1729 zfs_mknode(rootzp
, &vattr
, tx
, cr
, IS_ROOT_NODE
, &zp
, &acl_ids
);
1730 ASSERT3P(zp
, ==, rootzp
);
1731 error
= zap_add(os
, moid
, ZFS_ROOT_OBJ
, 8, 1, &rootzp
->z_id
, tx
);
1733 zfs_acl_ids_free(&acl_ids
);
1735 atomic_set(&ZTOI(rootzp
)->i_count
, 0);
1736 sa_handle_destroy(rootzp
->z_sa_hdl
);
1737 kmem_cache_free(znode_cache
, rootzp
);
1740 * Create shares directory
1742 error
= zfs_create_share_dir(zsb
, tx
);
1745 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1746 mutex_destroy(&zsb
->z_hold_mtx
[i
]);
1748 vmem_free(zsb
->z_hold_mtx
, sizeof (kmutex_t
) * ZFS_OBJ_MTX_SZ
);
1749 kmem_free(sb
, sizeof (struct super_block
));
1750 kmem_free(zsb
, sizeof (zfs_sb_t
));
1752 #endif /* _KERNEL */
1755 zfs_sa_setup(objset_t
*osp
, sa_attr_type_t
**sa_table
)
1757 uint64_t sa_obj
= 0;
1760 error
= zap_lookup(osp
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1761 if (error
!= 0 && error
!= ENOENT
)
1764 error
= sa_setup(osp
, sa_obj
, zfs_attr_table
, ZPL_END
, sa_table
);
1769 zfs_grab_sa_handle(objset_t
*osp
, uint64_t obj
, sa_handle_t
**hdlp
,
1770 dmu_buf_t
**db
, void *tag
)
1772 dmu_object_info_t doi
;
1775 if ((error
= sa_buf_hold(osp
, obj
, tag
, db
)) != 0)
1778 dmu_object_info_from_db(*db
, &doi
);
1779 if ((doi
.doi_bonus_type
!= DMU_OT_SA
&&
1780 doi
.doi_bonus_type
!= DMU_OT_ZNODE
) ||
1781 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1782 doi
.doi_bonus_size
< sizeof (znode_phys_t
))) {
1783 sa_buf_rele(*db
, tag
);
1784 return (SET_ERROR(ENOTSUP
));
1787 error
= sa_handle_get(osp
, obj
, NULL
, SA_HDL_PRIVATE
, hdlp
);
1789 sa_buf_rele(*db
, tag
);
1797 zfs_release_sa_handle(sa_handle_t
*hdl
, dmu_buf_t
*db
, void *tag
)
1799 sa_handle_destroy(hdl
);
1800 sa_buf_rele(db
, tag
);
1804 * Given an object number, return its parent object number and whether
1805 * or not the object is an extended attribute directory.
1808 zfs_obj_to_pobj(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
, uint64_t *pobjp
,
1814 sa_bulk_attr_t bulk
[3];
1818 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_PARENT
], NULL
,
1819 &parent
, sizeof (parent
));
1820 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_FLAGS
], NULL
,
1821 &pflags
, sizeof (pflags
));
1822 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1823 &mode
, sizeof (mode
));
1825 if ((error
= sa_bulk_lookup(hdl
, bulk
, count
)) != 0)
1829 *is_xattrdir
= ((pflags
& ZFS_XATTR
) != 0) && S_ISDIR(mode
);
1835 * Given an object number, return some zpl level statistics
1838 zfs_obj_to_stats_impl(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
1841 sa_bulk_attr_t bulk
[4];
1844 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1845 &sb
->zs_mode
, sizeof (sb
->zs_mode
));
1846 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_GEN
], NULL
,
1847 &sb
->zs_gen
, sizeof (sb
->zs_gen
));
1848 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_LINKS
], NULL
,
1849 &sb
->zs_links
, sizeof (sb
->zs_links
));
1850 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_CTIME
], NULL
,
1851 &sb
->zs_ctime
, sizeof (sb
->zs_ctime
));
1853 return (sa_bulk_lookup(hdl
, bulk
, count
));
1857 zfs_obj_to_path_impl(objset_t
*osp
, uint64_t obj
, sa_handle_t
*hdl
,
1858 sa_attr_type_t
*sa_table
, char *buf
, int len
)
1860 sa_handle_t
*sa_hdl
;
1861 sa_handle_t
*prevhdl
= NULL
;
1862 dmu_buf_t
*prevdb
= NULL
;
1863 dmu_buf_t
*sa_db
= NULL
;
1864 char *path
= buf
+ len
- 1;
1872 char component
[MAXNAMELEN
+ 2];
1874 int is_xattrdir
= 0;
1877 zfs_release_sa_handle(prevhdl
, prevdb
, FTAG
);
1879 if ((error
= zfs_obj_to_pobj(sa_hdl
, sa_table
, &pobj
,
1880 &is_xattrdir
)) != 0)
1891 (void) sprintf(component
+ 1, "<xattrdir>");
1893 error
= zap_value_search(osp
, pobj
, obj
,
1894 ZFS_DIRENT_OBJ(-1ULL), component
+ 1);
1899 complen
= strlen(component
);
1901 ASSERT(path
>= buf
);
1902 bcopy(component
, path
, complen
);
1905 if (sa_hdl
!= hdl
) {
1909 error
= zfs_grab_sa_handle(osp
, obj
, &sa_hdl
, &sa_db
, FTAG
);
1917 if (sa_hdl
!= NULL
&& sa_hdl
!= hdl
) {
1918 ASSERT(sa_db
!= NULL
);
1919 zfs_release_sa_handle(sa_hdl
, sa_db
, FTAG
);
1923 (void) memmove(buf
, path
, buf
+ len
- path
);
1929 zfs_obj_to_path(objset_t
*osp
, uint64_t obj
, char *buf
, int len
)
1931 sa_attr_type_t
*sa_table
;
1936 error
= zfs_sa_setup(osp
, &sa_table
);
1940 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
, FTAG
);
1944 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1946 zfs_release_sa_handle(hdl
, db
, FTAG
);
1951 zfs_obj_to_stats(objset_t
*osp
, uint64_t obj
, zfs_stat_t
*sb
,
1954 char *path
= buf
+ len
- 1;
1955 sa_attr_type_t
*sa_table
;
1962 error
= zfs_sa_setup(osp
, &sa_table
);
1966 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
, FTAG
);
1970 error
= zfs_obj_to_stats_impl(hdl
, sa_table
, sb
);
1972 zfs_release_sa_handle(hdl
, db
, FTAG
);
1976 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1978 zfs_release_sa_handle(hdl
, db
, FTAG
);
1982 #if defined(_KERNEL) && defined(HAVE_SPL)
1983 EXPORT_SYMBOL(zfs_create_fs
);
1984 EXPORT_SYMBOL(zfs_obj_to_path
);