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.
24 * Copyright (c) 2014 Integros [integros.com]
27 /* Portions Copyright 2007 Jeremy Teo */
28 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
31 #include <sys/types.h>
32 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/resource.h>
37 #include <sys/mntent.h>
38 #include <sys/u8_textprep.h>
39 #include <sys/dsl_dataset.h>
41 #include <sys/vnode.h>
44 #include <sys/errno.h>
45 #include <sys/unistd.h>
46 #include <sys/atomic.h>
47 #include <sys/zfs_dir.h>
48 #include <sys/zfs_acl.h>
49 #include <sys/zfs_ioctl.h>
50 #include <sys/zfs_rlock.h>
51 #include <sys/zfs_fuid.h>
52 #include <sys/dnode.h>
53 #include <sys/fs/zfs.h>
57 #include <sys/dmu_objset.h>
58 #include <sys/dmu_tx.h>
59 #include <sys/refcount.h>
62 #include <sys/zfs_znode.h>
64 #include <sys/zfs_sa.h>
65 #include <sys/zfs_stat.h>
66 #include <sys/refcount.h>
69 #include "zfs_comutil.h"
71 /* Used by fstat(1). */
72 SYSCTL_INT(_debug_sizeof
, OID_AUTO
, znode
, CTLFLAG_RD
,
73 SYSCTL_NULL_INT_PTR
, sizeof (znode_t
), "sizeof(znode_t)");
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 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
97 * be freed before it can be safely accessed.
99 krwlock_t zfsvfs_lock
;
101 static kmem_cache_t
*znode_cache
= NULL
;
103 extern struct vop_vector zfs_vnodeops
;
104 extern struct vop_vector zfs_fifoops
;
105 extern struct vop_vector zfs_shareops
;
109 * This callback is invoked when acquiring a RL_WRITER or RL_APPEND lock on
110 * z_rangelock. It will modify the offset and length of the lock to reflect
111 * znode-specific information, and convert RL_APPEND to RL_WRITER. This is
112 * called with the rangelock_t's rl_lock held, which avoids races.
115 zfs_rangelock_cb(zfs_locked_range_t
*new, void *arg
)
120 * If in append mode, convert to writer and lock starting at the
121 * current end of file.
123 if (new->lr_type
== RL_APPEND
) {
124 new->lr_offset
= zp
->z_size
;
125 new->lr_type
= RL_WRITER
;
129 * If we need to grow the block size then lock the whole file range.
131 uint64_t end_size
= MAX(zp
->z_size
, new->lr_offset
+ new->lr_length
);
132 if (end_size
> zp
->z_blksz
&& (!ISP2(zp
->z_blksz
) ||
133 zp
->z_blksz
< ZTOZSB(zp
)->z_max_blksz
)) {
135 new->lr_length
= UINT64_MAX
;
140 zfs_znode_cache_constructor(void *buf
, void *arg
, int kmflags
)
144 POINTER_INVALIDATE(&zp
->z_zfsvfs
);
146 list_link_init(&zp
->z_link_node
);
148 mutex_init(&zp
->z_acl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
150 zfs_rangelock_init(&zp
->z_rangelock
, zfs_rangelock_cb
, zp
);
152 zp
->z_acl_cached
= NULL
;
160 zfs_znode_cache_destructor(void *buf
, void *arg
)
164 ASSERT(!POINTER_IS_VALID(zp
->z_zfsvfs
));
165 ASSERT3P(zp
->z_vnode
, ==, NULL
);
166 ASSERT(!list_link_active(&zp
->z_link_node
));
167 mutex_destroy(&zp
->z_acl_lock
);
168 zfs_rangelock_fini(&zp
->z_rangelock
);
170 ASSERT(zp
->z_acl_cached
== NULL
);
179 rw_init(&zfsvfs_lock
, NULL
, RW_DEFAULT
, NULL
);
180 ASSERT(znode_cache
== NULL
);
181 znode_cache
= kmem_cache_create("zfs_znode_cache",
182 sizeof (znode_t
), 0, zfs_znode_cache_constructor
,
183 zfs_znode_cache_destructor
, NULL
, NULL
, NULL
, 0);
184 // kmem_cache_set_move(znode_cache, zfs_znode_move);
194 kmem_cache_destroy(znode_cache
);
196 rw_destroy(&zfsvfs_lock
);
201 zfs_create_share_dir(zfsvfs_t
*zfsvfs
, dmu_tx_t
*tx
)
203 zfs_acl_ids_t acl_ids
;
209 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
;
210 vattr
.va_type
= VDIR
;
211 vattr
.va_mode
= S_IFDIR
|0555;
212 vattr
.va_uid
= crgetuid(kcred
);
213 vattr
.va_gid
= crgetgid(kcred
);
215 sharezp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
216 ASSERT(!POINTER_IS_VALID(sharezp
->z_zfsvfs
));
217 sharezp
->z_moved
= 0;
218 sharezp
->z_unlinked
= 0;
219 sharezp
->z_atime_dirty
= 0;
220 sharezp
->z_zfsvfs
= zfsvfs
;
221 sharezp
->z_is_sa
= zfsvfs
->z_use_sa
;
223 VERIFY(0 == zfs_acl_ids_create(sharezp
, IS_ROOT_NODE
, &vattr
,
224 kcred
, NULL
, &acl_ids
));
225 zfs_mknode(sharezp
, &vattr
, tx
, kcred
, IS_ROOT_NODE
, &zp
, &acl_ids
);
226 ASSERT3P(zp
, ==, sharezp
);
227 POINTER_INVALIDATE(&sharezp
->z_zfsvfs
);
228 error
= zap_add(zfsvfs
->z_os
, MASTER_NODE_OBJ
,
229 ZFS_SHARES_DIR
, 8, 1, &sharezp
->z_id
, tx
);
230 zfsvfs
->z_shares_dir
= sharezp
->z_id
;
232 zfs_acl_ids_free(&acl_ids
);
233 sa_handle_destroy(sharezp
->z_sa_hdl
);
234 kmem_cache_free(znode_cache
, sharezp
);
240 * define a couple of values we need available
241 * for both 64 and 32 bit environments.
244 #define NBITSMINOR64 32
247 #define MAXMAJ64 0xffffffffUL
250 #define MAXMIN64 0xffffffffUL
254 * Create special expldev for ZFS private use.
255 * Can't use standard expldev since it doesn't do
256 * what we want. The standard expldev() takes a
257 * dev32_t in LP64 and expands it to a long dev_t.
258 * We need an interface that takes a dev32_t in ILP32
259 * and expands it to a long dev_t.
262 zfs_expldev(dev_t dev
)
264 return (((uint64_t)major(dev
) << NBITSMINOR64
) | minor(dev
));
267 * Special cmpldev for ZFS private use.
268 * Can't use standard cmpldev since it takes
269 * a long dev_t and compresses it to dev32_t in
270 * LP64. We need to do a compaction of a long dev_t
271 * to a dev32_t in ILP32.
274 zfs_cmpldev(uint64_t dev
)
276 return (makedev((dev
>> NBITSMINOR64
), (dev
& MAXMIN64
)));
280 zfs_znode_sa_init(zfsvfs_t
*zfsvfs
, znode_t
*zp
,
281 dmu_buf_t
*db
, dmu_object_type_t obj_type
, sa_handle_t
*sa_hdl
)
283 ASSERT(!POINTER_IS_VALID(zp
->z_zfsvfs
) || (zfsvfs
== zp
->z_zfsvfs
));
284 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs
, zp
->z_id
)));
286 ASSERT(zp
->z_sa_hdl
== NULL
);
287 ASSERT(zp
->z_acl_cached
== NULL
);
288 if (sa_hdl
== NULL
) {
289 VERIFY(0 == sa_handle_get_from_db(zfsvfs
->z_os
, db
, zp
,
290 SA_HDL_SHARED
, &zp
->z_sa_hdl
));
292 zp
->z_sa_hdl
= sa_hdl
;
293 sa_set_userp(sa_hdl
, zp
);
296 zp
->z_is_sa
= (obj_type
== DMU_OT_SA
) ? B_TRUE
: B_FALSE
;
299 * Slap on VROOT if we are the root znode unless we are the root
300 * node of a snapshot mounted under .zfs.
302 if (zp
->z_id
== zfsvfs
->z_root
&& zfsvfs
->z_parent
== zfsvfs
)
303 ZTOV(zp
)->v_flag
|= VROOT
;
309 zfs_znode_dmu_fini(znode_t
*zp
)
311 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp
->z_zfsvfs
, zp
->z_id
)) ||
313 RW_WRITE_HELD(&zp
->z_zfsvfs
->z_teardown_inactive_lock
));
315 sa_handle_destroy(zp
->z_sa_hdl
);
320 zfs_vnode_forget(vnode_t
*vp
)
323 /* copied from insmntque_stddtr */
325 vp
->v_op
= &dead_vnodeops
;
331 * Construct a new znode/vnode and intialize.
333 * This does not do a call to dmu_set_user() that is
334 * up to the caller to do, in case you don't want to
338 zfs_znode_alloc(zfsvfs_t
*zfsvfs
, dmu_buf_t
*db
, int blksz
,
339 dmu_object_type_t obj_type
, sa_handle_t
*hdl
)
346 uint64_t mtime
[2], ctime
[2];
348 uint64_t projid
= ZFS_DEFAULT_PROJID
;
349 sa_bulk_attr_t bulk
[9];
353 zp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
355 #if __FreeBSD_version >= 1300076
356 KASSERT(curthread
->td_vp_reserved
!= NULL
,
357 ("zfs_znode_alloc: getnewvnode without any vnodes reserved"));
359 KASSERT(curthread
->td_vp_reserv
> 0,
360 ("zfs_znode_alloc: getnewvnode without any vnodes reserved"));
362 error
= getnewvnode("zfs", zfsvfs
->z_parent
->z_vfs
, &zfs_vnodeops
, &vp
);
364 kmem_cache_free(znode_cache
, zp
);
370 ASSERT(!POINTER_IS_VALID(zp
->z_zfsvfs
));
374 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
375 * the zfs_znode_move() callback.
379 zp
->z_atime_dirty
= 0;
381 zp
->z_id
= db
->db_object
;
383 zp
->z_seq
= 0x7A4653;
388 zfs_znode_sa_init(zfsvfs
, zp
, db
, obj_type
, hdl
);
390 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
, &mode
, 8);
391 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zfsvfs
), NULL
, &zp
->z_gen
, 8);
392 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
394 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
), NULL
,
396 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
398 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PARENT(zfsvfs
), NULL
, &parent
, 8);
399 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
402 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
404 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
407 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
409 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
412 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
) != 0 || zp
->z_gen
== 0 ||
413 (dmu_objset_projectquota_enabled(zfsvfs
->z_os
) &&
414 (zp
->z_pflags
& ZFS_PROJID
) &&
415 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PROJID(zfsvfs
), &projid
, 8) != 0)) {
417 sa_handle_destroy(zp
->z_sa_hdl
);
418 zfs_vnode_forget(vp
);
420 kmem_cache_free(znode_cache
, zp
);
424 zp
->z_projid
= projid
;
427 /* Cache the xattr parent id */
428 if (zp
->z_pflags
& ZFS_XATTR
)
429 zp
->z_xattr_parent
= parent
;
431 vp
->v_type
= IFTOVT((mode_t
)mode
);
433 switch (vp
->v_type
) {
435 zp
->z_zn_prefetch
= B_TRUE
; /* z_prefetch default is enabled */
438 vp
->v_op
= &zfs_fifoops
;
441 if (parent
== zfsvfs
->z_shares_dir
) {
442 ASSERT(zp
->z_uid
== 0 && zp
->z_gid
== 0);
443 vp
->v_op
= &zfs_shareops
;
450 mutex_enter(&zfsvfs
->z_znodes_lock
);
451 list_insert_tail(&zfsvfs
->z_all_znodes
, zp
);
452 zfsvfs
->z_nr_znodes
++;
455 * Everything else must be valid before assigning z_zfsvfs makes the
456 * znode eligible for zfs_znode_move().
458 zp
->z_zfsvfs
= zfsvfs
;
459 mutex_exit(&zfsvfs
->z_znodes_lock
);
462 * Acquire vnode lock before making it available to the world.
464 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
466 if (vp
->v_type
!= VFIFO
)
472 static uint64_t empty_xattr
;
473 static uint64_t pad
[4];
474 static zfs_acl_phys_t acl_phys
;
476 * Create a new DMU object to hold a zfs znode.
478 * IN: dzp - parent directory for new znode
479 * vap - file attributes for new znode
480 * tx - dmu transaction id for zap operations
481 * cr - credentials of caller
483 * IS_ROOT_NODE - new object will be root
484 * IS_XATTR - new object is an attribute
485 * bonuslen - length of bonus buffer
486 * setaclp - File/Dir initial ACL
487 * fuidp - Tracks fuid allocation.
489 * OUT: zpp - allocated znode
493 zfs_mknode(znode_t
*dzp
, vattr_t
*vap
, dmu_tx_t
*tx
, cred_t
*cr
,
494 uint_t flag
, znode_t
**zpp
, zfs_acl_ids_t
*acl_ids
)
496 uint64_t crtime
[2], atime
[2], mtime
[2], ctime
[2];
497 uint64_t mode
, size
, links
, parent
, pflags
;
498 uint64_t dzp_pflags
= 0;
500 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
508 dmu_object_type_t obj_type
;
509 sa_bulk_attr_t
*sa_attrs
;
511 zfs_acl_locator_cb_t locate
= { 0 };
513 ASSERT(vap
&& ((vap
->va_mask
& AT_MODE
) == AT_MODE
));
515 if (zfsvfs
->z_replay
) {
516 obj
= vap
->va_nodeid
;
517 now
= vap
->va_ctime
; /* see zfs_replay_create() */
518 gen
= vap
->va_nblocks
; /* ditto */
519 dnodesize
= vap
->va_fsid
; /* ditto */
523 gen
= dmu_tx_get_txg(tx
);
524 dnodesize
= dmu_objset_dnodesize(zfsvfs
->z_os
);
528 dnodesize
= DNODE_MIN_SIZE
;
530 obj_type
= zfsvfs
->z_use_sa
? DMU_OT_SA
: DMU_OT_ZNODE
;
531 bonuslen
= (obj_type
== DMU_OT_SA
) ?
532 DN_BONUS_SIZE(dnodesize
) : ZFS_OLD_ZNODE_PHYS_SIZE
;
535 * Create a new DMU object.
538 * There's currently no mechanism for pre-reading the blocks that will
539 * be needed to allocate a new object, so we accept the small chance
540 * that there will be an i/o error and we will fail one of the
543 if (vap
->va_type
== VDIR
) {
544 if (zfsvfs
->z_replay
) {
545 VERIFY0(zap_create_claim_norm_dnsize(zfsvfs
->z_os
, obj
,
546 zfsvfs
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
547 obj_type
, bonuslen
, dnodesize
, tx
));
549 obj
= zap_create_norm_dnsize(zfsvfs
->z_os
,
550 zfsvfs
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
551 obj_type
, bonuslen
, dnodesize
, tx
);
554 if (zfsvfs
->z_replay
) {
555 VERIFY0(dmu_object_claim_dnsize(zfsvfs
->z_os
, obj
,
556 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
557 obj_type
, bonuslen
, dnodesize
, tx
));
559 obj
= dmu_object_alloc_dnsize(zfsvfs
->z_os
,
560 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
561 obj_type
, bonuslen
, dnodesize
, tx
);
565 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj
);
566 VERIFY(0 == sa_buf_hold(zfsvfs
->z_os
, obj
, NULL
, &db
));
569 * If this is the root, fix up the half-initialized parent pointer
570 * to reference the just-allocated physical data area.
572 if (flag
& IS_ROOT_NODE
) {
575 dzp_pflags
= dzp
->z_pflags
;
579 * If parent is an xattr, so am I.
581 if (dzp_pflags
& ZFS_XATTR
) {
585 if (zfsvfs
->z_use_fuids
)
586 pflags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
590 if (vap
->va_type
== VDIR
) {
591 size
= 2; /* contents ("." and "..") */
592 links
= (flag
& (IS_ROOT_NODE
| IS_XATTR
)) ? 2 : 1;
597 if (vap
->va_type
== VBLK
|| vap
->va_type
== VCHR
) {
598 rdev
= zfs_expldev(vap
->va_rdev
);
602 mode
= acl_ids
->z_mode
;
607 * No execs denied will be deterimed when zfs_mode_compute() is called.
609 pflags
|= acl_ids
->z_aclp
->z_hints
&
610 (ZFS_ACL_TRIVIAL
|ZFS_INHERIT_ACE
|ZFS_ACL_AUTO_INHERIT
|
611 ZFS_ACL_DEFAULTED
|ZFS_ACL_PROTECTED
);
613 ZFS_TIME_ENCODE(&now
, crtime
);
614 ZFS_TIME_ENCODE(&now
, ctime
);
616 if (vap
->va_mask
& AT_ATIME
) {
617 ZFS_TIME_ENCODE(&vap
->va_atime
, atime
);
619 ZFS_TIME_ENCODE(&now
, atime
);
622 if (vap
->va_mask
& AT_MTIME
) {
623 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
625 ZFS_TIME_ENCODE(&now
, mtime
);
628 /* Now add in all of the "SA" attributes */
629 VERIFY(0 == sa_handle_get_from_db(zfsvfs
->z_os
, db
, NULL
, SA_HDL_SHARED
,
633 * Setup the array of attributes to be replaced/set on the new file
635 * order for DMU_OT_ZNODE is critical since it needs to be constructed
636 * in the old znode_phys_t format. Don't change this ordering
638 sa_attrs
= kmem_alloc(sizeof (sa_bulk_attr_t
) * ZPL_END
, KM_SLEEP
);
640 if (obj_type
== DMU_OT_ZNODE
) {
641 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zfsvfs
),
643 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zfsvfs
),
645 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zfsvfs
),
647 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zfsvfs
),
649 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zfsvfs
),
651 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zfsvfs
),
653 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zfsvfs
),
655 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zfsvfs
),
658 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zfsvfs
),
660 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zfsvfs
),
662 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zfsvfs
),
664 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zfsvfs
),
665 NULL
, &acl_ids
->z_fuid
, 8);
666 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zfsvfs
),
667 NULL
, &acl_ids
->z_fgid
, 8);
668 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zfsvfs
),
670 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zfsvfs
),
672 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zfsvfs
),
674 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zfsvfs
),
676 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zfsvfs
),
678 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zfsvfs
),
682 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_LINKS(zfsvfs
), NULL
, &links
, 8);
684 if (obj_type
== DMU_OT_ZNODE
) {
685 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_XATTR(zfsvfs
), NULL
,
688 if (obj_type
== DMU_OT_ZNODE
||
689 (vap
->va_type
== VBLK
|| vap
->va_type
== VCHR
)) {
690 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_RDEV(zfsvfs
),
694 if (obj_type
== DMU_OT_ZNODE
) {
695 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zfsvfs
),
697 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zfsvfs
), NULL
,
698 &acl_ids
->z_fuid
, 8);
699 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zfsvfs
), NULL
,
700 &acl_ids
->z_fgid
, 8);
701 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PAD(zfsvfs
), NULL
, pad
,
702 sizeof (uint64_t) * 4);
703 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ZNODE_ACL(zfsvfs
), NULL
,
704 &acl_phys
, sizeof (zfs_acl_phys_t
));
705 } else if (acl_ids
->z_aclp
->z_version
>= ZFS_ACL_VERSION_FUID
) {
706 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_COUNT(zfsvfs
), NULL
,
707 &acl_ids
->z_aclp
->z_acl_count
, 8);
708 locate
.cb_aclp
= acl_ids
->z_aclp
;
709 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_ACES(zfsvfs
),
710 zfs_acl_data_locator
, &locate
,
711 acl_ids
->z_aclp
->z_acl_bytes
);
712 mode
= zfs_mode_compute(mode
, acl_ids
->z_aclp
, &pflags
,
713 acl_ids
->z_fuid
, acl_ids
->z_fgid
);
716 VERIFY(sa_replace_all_by_template(sa_hdl
, sa_attrs
, cnt
, tx
) == 0);
718 if (!(flag
& IS_ROOT_NODE
)) {
719 *zpp
= zfs_znode_alloc(zfsvfs
, db
, 0, obj_type
, sa_hdl
);
720 ASSERT(*zpp
!= NULL
);
723 * If we are creating the root node, the "parent" we
724 * passed in is the znode for the root.
728 (*zpp
)->z_sa_hdl
= sa_hdl
;
731 (*zpp
)->z_pflags
= pflags
;
732 (*zpp
)->z_mode
= mode
;
733 (*zpp
)->z_dnodesize
= dnodesize
;
735 if (vap
->va_mask
& AT_XVATTR
)
736 zfs_xvattr_set(*zpp
, (xvattr_t
*)vap
, tx
);
738 if (obj_type
== DMU_OT_ZNODE
||
739 acl_ids
->z_aclp
->z_version
< ZFS_ACL_VERSION_FUID
) {
740 VERIFY0(zfs_aclset_common(*zpp
, acl_ids
->z_aclp
, cr
, tx
));
742 if (!(flag
& IS_ROOT_NODE
)) {
746 vp
->v_vflag
|= VV_FORCEINSMQ
;
747 err
= insmntque(vp
, zfsvfs
->z_vfs
);
748 vp
->v_vflag
&= ~VV_FORCEINSMQ
;
749 KASSERT(err
== 0, ("insmntque() failed: error %d", err
));
751 kmem_free(sa_attrs
, sizeof (sa_bulk_attr_t
) * ZPL_END
);
752 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj
);
756 * Update in-core attributes. It is assumed the caller will be doing an
757 * sa_bulk_update to push the changes out.
760 zfs_xvattr_set(znode_t
*zp
, xvattr_t
*xvap
, dmu_tx_t
*tx
)
764 xoap
= xva_getxoptattr(xvap
);
767 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
769 ZFS_TIME_ENCODE(&xoap
->xoa_createtime
, times
);
770 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zp
->z_zfsvfs
),
771 ×
, sizeof (times
), tx
);
772 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
774 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
775 ZFS_ATTR_SET(zp
, ZFS_READONLY
, xoap
->xoa_readonly
,
777 XVA_SET_RTN(xvap
, XAT_READONLY
);
779 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
780 ZFS_ATTR_SET(zp
, ZFS_HIDDEN
, xoap
->xoa_hidden
,
782 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
784 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
785 ZFS_ATTR_SET(zp
, ZFS_SYSTEM
, xoap
->xoa_system
,
787 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
789 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
790 ZFS_ATTR_SET(zp
, ZFS_ARCHIVE
, xoap
->xoa_archive
,
792 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
794 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
795 ZFS_ATTR_SET(zp
, ZFS_IMMUTABLE
, xoap
->xoa_immutable
,
797 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
799 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
800 ZFS_ATTR_SET(zp
, ZFS_NOUNLINK
, xoap
->xoa_nounlink
,
802 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
804 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
805 ZFS_ATTR_SET(zp
, ZFS_APPENDONLY
, xoap
->xoa_appendonly
,
807 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
809 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
810 ZFS_ATTR_SET(zp
, ZFS_NODUMP
, xoap
->xoa_nodump
,
812 XVA_SET_RTN(xvap
, XAT_NODUMP
);
814 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
815 ZFS_ATTR_SET(zp
, ZFS_OPAQUE
, xoap
->xoa_opaque
,
817 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
819 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
820 ZFS_ATTR_SET(zp
, ZFS_AV_QUARANTINED
,
821 xoap
->xoa_av_quarantined
, zp
->z_pflags
, tx
);
822 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
824 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
825 ZFS_ATTR_SET(zp
, ZFS_AV_MODIFIED
, xoap
->xoa_av_modified
,
827 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
829 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
830 zfs_sa_set_scanstamp(zp
, xvap
, tx
);
831 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
833 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
834 ZFS_ATTR_SET(zp
, ZFS_REPARSE
, xoap
->xoa_reparse
,
836 XVA_SET_RTN(xvap
, XAT_REPARSE
);
838 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
839 ZFS_ATTR_SET(zp
, ZFS_OFFLINE
, xoap
->xoa_offline
,
841 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
843 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
844 ZFS_ATTR_SET(zp
, ZFS_SPARSE
, xoap
->xoa_sparse
,
846 XVA_SET_RTN(xvap
, XAT_SPARSE
);
851 zfs_zget(zfsvfs_t
*zfsvfs
, uint64_t obj_num
, znode_t
**zpp
)
853 dmu_object_info_t doi
;
863 getnewvnode_reserve_();
866 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj_num
);
868 err
= sa_buf_hold(zfsvfs
->z_os
, obj_num
, NULL
, &db
);
870 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
871 getnewvnode_drop_reserve();
875 dmu_object_info_from_db(db
, &doi
);
876 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
877 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
878 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
879 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
880 sa_buf_rele(db
, NULL
);
881 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
882 getnewvnode_drop_reserve();
883 return (SET_ERROR(EINVAL
));
886 hdl
= dmu_buf_get_user(db
);
888 zp
= sa_get_userdata(hdl
);
891 * Since "SA" does immediate eviction we
892 * should never find a sa handle that doesn't
893 * know about the znode.
895 ASSERT3P(zp
, !=, NULL
);
896 ASSERT3U(zp
->z_id
, ==, obj_num
);
897 if (zp
->z_unlinked
) {
898 err
= SET_ERROR(ENOENT
);
902 * Don't let the vnode disappear after
910 sa_buf_rele(db
, NULL
);
911 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
914 getnewvnode_drop_reserve();
918 locked
= VOP_ISLOCKED(vp
);
920 if (VN_IS_DOOMED(vp
) && locked
!= LK_EXCLUSIVE
) {
922 * The vnode is doomed and this thread doesn't
923 * hold the exclusive lock on it, so the vnode
924 * must be being reclaimed by another thread.
925 * Otherwise the doomed vnode is being reclaimed
926 * by this thread and zfs_zget is called from
932 * XXX vrele() locks the vnode when the last reference
933 * is dropped. Although in this case the vnode is
934 * doomed / dead and so no inactivation is required,
935 * the vnode lock is still acquired. That could result
936 * in a LOR with z_teardown_lock if another thread holds
937 * the vnode's lock and tries to take z_teardown_lock.
938 * But that is only possible if the other thread peforms
939 * a ZFS vnode operation on the vnode. That either
940 * should not happen if the vnode is dead or the thread
941 * should also have a refrence to the vnode and thus
942 * our reference is not last.
948 getnewvnode_drop_reserve();
953 * Not found create new znode/vnode
954 * but only if file exists.
956 * There is a small window where zfs_vget() could
957 * find this object while a file create is still in
958 * progress. This is checked for in zfs_znode_alloc()
960 * if zfs_znode_alloc() fails it will drop the hold on the
963 zp
= zfs_znode_alloc(zfsvfs
, db
, doi
.doi_data_block_size
,
964 doi
.doi_bonus_type
, NULL
);
966 err
= SET_ERROR(ENOENT
);
971 vnode_t
*vp
= ZTOV(zp
);
973 err
= insmntque(vp
, zfsvfs
->z_vfs
);
975 vp
->v_hash
= obj_num
;
979 zfs_znode_dmu_fini(zp
);
984 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
985 getnewvnode_drop_reserve();
990 zfs_rezget(znode_t
*zp
)
992 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
993 dmu_object_info_t doi
;
996 uint64_t obj_num
= zp
->z_id
;
998 sa_bulk_attr_t bulk
[8];
1004 * Remove cached pages before reloading the znode, so that they are not
1005 * lingering after we run into any error. Ideally, we should vgone()
1006 * the vnode in case of error, but currently we cannot do that
1007 * because of the LOR between the vnode lock and z_teardown_lock.
1008 * So, instead, we have to "doom" the znode in the illumos style.
1011 vn_pages_remove(vp
, 0, 0);
1013 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj_num
);
1015 mutex_enter(&zp
->z_acl_lock
);
1016 if (zp
->z_acl_cached
) {
1017 zfs_acl_free(zp
->z_acl_cached
);
1018 zp
->z_acl_cached
= NULL
;
1021 mutex_exit(&zp
->z_acl_lock
);
1022 ASSERT(zp
->z_sa_hdl
== NULL
);
1023 err
= sa_buf_hold(zfsvfs
->z_os
, obj_num
, NULL
, &db
);
1025 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
1029 dmu_object_info_from_db(db
, &doi
);
1030 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
1031 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
1032 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1033 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
1034 sa_buf_rele(db
, NULL
);
1035 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
1036 return (SET_ERROR(EINVAL
));
1039 zfs_znode_sa_init(zfsvfs
, zp
, db
, doi
.doi_bonus_type
, NULL
);
1042 /* reload cached values */
1043 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zfsvfs
), NULL
,
1044 &gen
, sizeof (gen
));
1045 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
1046 &zp
->z_size
, sizeof (zp
->z_size
));
1047 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
), NULL
,
1048 &zp
->z_links
, sizeof (zp
->z_links
));
1049 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
1050 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
1051 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
1052 &zp
->z_atime
, sizeof (zp
->z_atime
));
1053 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
1054 &zp
->z_uid
, sizeof (zp
->z_uid
));
1055 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
1056 &zp
->z_gid
, sizeof (zp
->z_gid
));
1057 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
1058 &mode
, sizeof (mode
));
1060 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) {
1061 zfs_znode_dmu_fini(zp
);
1062 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
1063 return (SET_ERROR(EIO
));
1068 if (gen
!= zp
->z_gen
) {
1069 zfs_znode_dmu_fini(zp
);
1070 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
1071 return (SET_ERROR(EIO
));
1075 * It is highly improbable but still quite possible that two
1076 * objects in different datasets are created with the same
1077 * object numbers and in transaction groups with the same
1078 * numbers. znodes corresponding to those objects would
1079 * have the same z_id and z_gen, but their other attributes
1081 * zfs recv -F may replace one of such objects with the other.
1082 * As a result file properties recorded in the replaced
1083 * object's vnode may no longer match the received object's
1084 * properties. At present the only cached property is the
1085 * files type recorded in v_type.
1086 * So, handle this case by leaving the old vnode and znode
1087 * disassociated from the actual object. A new vnode and a
1088 * znode will be created if the object is accessed
1089 * (e.g. via a look-up). The old vnode and znode will be
1090 * recycled when the last vnode reference is dropped.
1092 if (vp
->v_type
!= IFTOVT((mode_t
)zp
->z_mode
)) {
1093 zfs_znode_dmu_fini(zp
);
1094 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
1095 return (SET_ERROR(EIO
));
1099 * If the file has zero links, then it has been unlinked on the send
1100 * side and it must be in the received unlinked set.
1101 * We call zfs_znode_dmu_fini() now to prevent any accesses to the
1102 * stale data and to prevent automatical removal of the file in
1103 * zfs_zinactive(). The file will be removed either when it is removed
1104 * on the send side and the next incremental stream is received or
1105 * when the unlinked set gets processed.
1107 zp
->z_unlinked
= (zp
->z_links
== 0);
1108 if (zp
->z_unlinked
) {
1109 zfs_znode_dmu_fini(zp
);
1110 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
1114 zp
->z_blksz
= doi
.doi_data_block_size
;
1115 if (zp
->z_size
!= size
)
1116 vnode_pager_setsize(vp
, zp
->z_size
);
1118 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
1124 zfs_znode_delete(znode_t
*zp
, dmu_tx_t
*tx
)
1126 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1127 objset_t
*os
= zfsvfs
->z_os
;
1128 uint64_t obj
= zp
->z_id
;
1129 uint64_t acl_obj
= zfs_external_acl(zp
);
1131 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj
);
1133 VERIFY(!zp
->z_is_sa
);
1134 VERIFY(0 == dmu_object_free(os
, acl_obj
, tx
));
1136 VERIFY(0 == dmu_object_free(os
, obj
, tx
));
1137 zfs_znode_dmu_fini(zp
);
1138 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj
);
1143 zfs_zinactive(znode_t
*zp
)
1145 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1146 uint64_t z_id
= zp
->z_id
;
1148 ASSERT(zp
->z_sa_hdl
);
1151 * Don't allow a zfs_zget() while were trying to release this znode
1153 ZFS_OBJ_HOLD_ENTER(zfsvfs
, z_id
);
1156 * If this was the last reference to a file with no links, remove
1157 * the file from the file system unless the file system is mounted
1158 * read-only. That can happen, for example, if the file system was
1159 * originally read-write, the file was opened, then unlinked and
1160 * the file system was made read-only before the file was finally
1161 * closed. The file will remain in the unlinked set.
1163 if (zp
->z_unlinked
) {
1164 ASSERT(!zfsvfs
->z_issnap
);
1165 if ((zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) == 0) {
1166 ZFS_OBJ_HOLD_EXIT(zfsvfs
, z_id
);
1172 zfs_znode_dmu_fini(zp
);
1173 ZFS_OBJ_HOLD_EXIT(zfsvfs
, z_id
);
1178 zfs_znode_free(znode_t
*zp
)
1180 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1182 ASSERT(zp
->z_sa_hdl
== NULL
);
1184 mutex_enter(&zfsvfs
->z_znodes_lock
);
1185 POINTER_INVALIDATE(&zp
->z_zfsvfs
);
1186 list_remove(&zfsvfs
->z_all_znodes
, zp
);
1187 zfsvfs
->z_nr_znodes
--;
1188 mutex_exit(&zfsvfs
->z_znodes_lock
);
1190 if (zp
->z_acl_cached
) {
1191 zfs_acl_free(zp
->z_acl_cached
);
1192 zp
->z_acl_cached
= NULL
;
1195 kmem_cache_free(znode_cache
, zp
);
1200 zfs_tstamp_update_setup_ext(znode_t
*zp
, uint_t flag
, uint64_t mtime
[2],
1201 uint64_t ctime
[2], boolean_t have_tx
)
1205 vfs_timestamp(&now
);
1207 if (have_tx
) { /* will sa_bulk_update happen really soon? */
1208 zp
->z_atime_dirty
= 0;
1211 zp
->z_atime_dirty
= 1;
1214 if (flag
& AT_ATIME
) {
1215 ZFS_TIME_ENCODE(&now
, zp
->z_atime
);
1218 if (flag
& AT_MTIME
) {
1219 ZFS_TIME_ENCODE(&now
, mtime
);
1220 if (zp
->z_zfsvfs
->z_use_fuids
) {
1221 zp
->z_pflags
|= (ZFS_ARCHIVE
|
1226 if (flag
& AT_CTIME
) {
1227 ZFS_TIME_ENCODE(&now
, ctime
);
1228 if (zp
->z_zfsvfs
->z_use_fuids
)
1229 zp
->z_pflags
|= ZFS_ARCHIVE
;
1235 zfs_tstamp_update_setup(znode_t
*zp
, uint_t flag
, uint64_t mtime
[2],
1238 zfs_tstamp_update_setup_ext(zp
, flag
, mtime
, ctime
, B_TRUE
);
1241 * Grow the block size for a file.
1243 * IN: zp - znode of file to free data in.
1244 * size - requested block size
1245 * tx - open transaction.
1247 * NOTE: this function assumes that the znode is write locked.
1250 zfs_grow_blocksize(znode_t
*zp
, uint64_t size
, dmu_tx_t
*tx
)
1255 if (size
<= zp
->z_blksz
)
1258 * If the file size is already greater than the current blocksize,
1259 * we will not grow. If there is more than one block in a file,
1260 * the blocksize cannot change.
1262 if (zp
->z_blksz
&& zp
->z_size
> zp
->z_blksz
)
1265 error
= dmu_object_set_blocksize(zp
->z_zfsvfs
->z_os
, zp
->z_id
,
1268 if (error
== ENOTSUP
)
1272 /* What blocksize did we actually get? */
1273 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &zp
->z_blksz
, &dummy
);
1277 * Increase the file length
1279 * IN: zp - znode of file to free data in.
1280 * end - new end-of-file
1282 * RETURN: 0 on success, error code on failure
1285 zfs_extend(znode_t
*zp
, uint64_t end
)
1287 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1289 zfs_locked_range_t
*lr
;
1294 * We will change zp_size, lock the whole file.
1296 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, 0, UINT64_MAX
, RL_WRITER
);
1299 * Nothing to do if file already at desired length.
1301 if (end
<= zp
->z_size
) {
1302 zfs_rangelock_exit(lr
);
1305 tx
= dmu_tx_create(zfsvfs
->z_os
);
1306 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1307 zfs_sa_upgrade_txholds(tx
, zp
);
1308 if (end
> zp
->z_blksz
&&
1309 (!ISP2(zp
->z_blksz
) || zp
->z_blksz
< zfsvfs
->z_max_blksz
)) {
1311 * We are growing the file past the current block size.
1313 if (zp
->z_blksz
> zp
->z_zfsvfs
->z_max_blksz
) {
1315 * File's blocksize is already larger than the
1316 * "recordsize" property. Only let it grow to
1317 * the next power of 2.
1319 ASSERT(!ISP2(zp
->z_blksz
));
1320 newblksz
= MIN(end
, 1 << highbit64(zp
->z_blksz
));
1322 newblksz
= MIN(end
, zp
->z_zfsvfs
->z_max_blksz
);
1324 dmu_tx_hold_write(tx
, zp
->z_id
, 0, newblksz
);
1329 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1332 zfs_rangelock_exit(lr
);
1337 zfs_grow_blocksize(zp
, newblksz
, tx
);
1341 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zp
->z_zfsvfs
),
1342 &zp
->z_size
, sizeof (zp
->z_size
), tx
));
1344 vnode_pager_setsize(ZTOV(zp
), end
);
1346 zfs_rangelock_exit(lr
);
1354 * Free space in a file.
1356 * IN: zp - znode of file to free data in.
1357 * off - start of section to free.
1358 * len - length of section to free.
1360 * RETURN: 0 on success, error code on failure
1363 zfs_free_range(znode_t
*zp
, uint64_t off
, uint64_t len
)
1365 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1366 zfs_locked_range_t
*lr
;
1370 * Lock the range being freed.
1372 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, off
, len
, RL_WRITER
);
1375 * Nothing to do if file already at desired length.
1377 if (off
>= zp
->z_size
) {
1378 zfs_rangelock_exit(lr
);
1382 if (off
+ len
> zp
->z_size
)
1383 len
= zp
->z_size
- off
;
1385 error
= dmu_free_long_range(zfsvfs
->z_os
, zp
->z_id
, off
, len
);
1389 * In FreeBSD we cannot free block in the middle of a file,
1390 * but only at the end of a file, so this code path should
1393 vnode_pager_setsize(ZTOV(zp
), off
);
1396 zfs_rangelock_exit(lr
);
1404 * IN: zp - znode of file to free data in.
1405 * end - new end-of-file.
1407 * RETURN: 0 on success, error code on failure
1410 zfs_trunc(znode_t
*zp
, uint64_t end
)
1412 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1413 vnode_t
*vp
= ZTOV(zp
);
1415 zfs_locked_range_t
*lr
;
1417 sa_bulk_attr_t bulk
[2];
1421 * We will change zp_size, lock the whole file.
1423 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, 0, UINT64_MAX
, RL_WRITER
);
1426 * Nothing to do if file already at desired length.
1428 if (end
>= zp
->z_size
) {
1429 zfs_rangelock_exit(lr
);
1433 error
= dmu_free_long_range(zfsvfs
->z_os
, zp
->z_id
, end
,
1436 zfs_rangelock_exit(lr
);
1439 tx
= dmu_tx_create(zfsvfs
->z_os
);
1440 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1441 zfs_sa_upgrade_txholds(tx
, zp
);
1442 dmu_tx_mark_netfree(tx
);
1443 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1446 zfs_rangelock_exit(lr
);
1451 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
),
1452 NULL
, &zp
->z_size
, sizeof (zp
->z_size
));
1455 zp
->z_pflags
&= ~ZFS_SPARSE
;
1456 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
),
1457 NULL
, &zp
->z_pflags
, 8);
1459 VERIFY(sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
) == 0);
1464 * Clear any mapped pages in the truncated region. This has to
1465 * happen outside of the transaction to avoid the possibility of
1466 * a deadlock with someone trying to push a page that we are
1467 * about to invalidate.
1469 vnode_pager_setsize(vp
, end
);
1471 zfs_rangelock_exit(lr
);
1477 * Free space in a file
1479 * IN: zp - znode of file to free data in.
1480 * off - start of range
1481 * len - end of range (0 => EOF)
1482 * flag - current file open mode flags.
1483 * log - TRUE if this action should be logged
1485 * RETURN: 0 on success, error code on failure
1488 zfs_freesp(znode_t
*zp
, uint64_t off
, uint64_t len
, int flag
, boolean_t log
)
1491 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1492 zilog_t
*zilog
= zfsvfs
->z_log
;
1494 uint64_t mtime
[2], ctime
[2];
1495 sa_bulk_attr_t bulk
[3];
1499 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MODE(zfsvfs
), &mode
,
1500 sizeof (mode
))) != 0)
1503 if (off
> zp
->z_size
) {
1504 error
= zfs_extend(zp
, off
+len
);
1505 if (error
== 0 && log
)
1512 error
= zfs_trunc(zp
, off
);
1514 if ((error
= zfs_free_range(zp
, off
, len
)) == 0 &&
1515 off
+ len
> zp
->z_size
)
1516 error
= zfs_extend(zp
, off
+len
);
1521 tx
= dmu_tx_create(zfsvfs
->z_os
);
1522 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1523 zfs_sa_upgrade_txholds(tx
, zp
);
1524 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1530 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, mtime
, 16);
1531 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, ctime
, 16);
1532 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
),
1533 NULL
, &zp
->z_pflags
, 8);
1534 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
1535 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1538 zfs_log_truncate(zilog
, tx
, TX_TRUNCATE
, zp
, off
, len
);
1545 zfs_create_fs(objset_t
*os
, cred_t
*cr
, nvlist_t
*zplprops
, dmu_tx_t
*tx
)
1547 uint64_t moid
, obj
, sa_obj
, version
;
1548 uint64_t sense
= ZFS_CASE_SENSITIVE
;
1553 znode_t
*rootzp
= NULL
;
1557 zfs_acl_ids_t acl_ids
;
1560 * First attempt to create master node.
1563 * In an empty objset, there are no blocks to read and thus
1564 * there can be no i/o errors (which we assert below).
1566 moid
= MASTER_NODE_OBJ
;
1567 error
= zap_create_claim(os
, moid
, DMU_OT_MASTER_NODE
,
1568 DMU_OT_NONE
, 0, tx
);
1572 * Set starting attributes.
1574 version
= zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)));
1576 while ((elem
= nvlist_next_nvpair(zplprops
, elem
)) != NULL
) {
1577 /* For the moment we expect all zpl props to be uint64_ts */
1581 ASSERT(nvpair_type(elem
) == DATA_TYPE_UINT64
);
1582 VERIFY(nvpair_value_uint64(elem
, &val
) == 0);
1583 name
= nvpair_name(elem
);
1584 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_VERSION
)) == 0) {
1588 error
= zap_update(os
, moid
, name
, 8, 1, &val
, tx
);
1591 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_NORMALIZE
)) == 0)
1593 else if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_CASE
)) == 0)
1596 ASSERT(version
!= 0);
1597 error
= zap_update(os
, moid
, ZPL_VERSION_STR
, 8, 1, &version
, tx
);
1600 * Create zap object used for SA attribute registration
1603 if (version
>= ZPL_VERSION_SA
) {
1604 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1605 DMU_OT_NONE
, 0, tx
);
1606 error
= zap_add(os
, moid
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1612 * Create a delete queue.
1614 obj
= zap_create(os
, DMU_OT_UNLINKED_SET
, DMU_OT_NONE
, 0, tx
);
1616 error
= zap_add(os
, moid
, ZFS_UNLINKED_SET
, 8, 1, &obj
, tx
);
1620 * Create root znode. Create minimal znode/vnode/zfsvfs
1621 * to allow zfs_mknode to work.
1624 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
;
1625 vattr
.va_type
= VDIR
;
1626 vattr
.va_mode
= S_IFDIR
|0755;
1627 vattr
.va_uid
= crgetuid(cr
);
1628 vattr
.va_gid
= crgetgid(cr
);
1630 zfsvfs
= kmem_zalloc(sizeof (zfsvfs_t
), KM_SLEEP
);
1632 rootzp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
1633 ASSERT(!POINTER_IS_VALID(rootzp
->z_zfsvfs
));
1634 rootzp
->z_moved
= 0;
1635 rootzp
->z_unlinked
= 0;
1636 rootzp
->z_atime_dirty
= 0;
1637 rootzp
->z_is_sa
= USE_SA(version
, os
);
1640 zfsvfs
->z_parent
= zfsvfs
;
1641 zfsvfs
->z_version
= version
;
1642 zfsvfs
->z_use_fuids
= USE_FUIDS(version
, os
);
1643 zfsvfs
->z_use_sa
= USE_SA(version
, os
);
1644 zfsvfs
->z_norm
= norm
;
1646 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
1647 &zfsvfs
->z_attr_table
);
1652 * Fold case on file systems that are always or sometimes case
1655 if (sense
== ZFS_CASE_INSENSITIVE
|| sense
== ZFS_CASE_MIXED
)
1656 zfsvfs
->z_norm
|= U8_TEXTPREP_TOUPPER
;
1658 mutex_init(&zfsvfs
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1659 list_create(&zfsvfs
->z_all_znodes
, sizeof (znode_t
),
1660 offsetof(znode_t
, z_link_node
));
1662 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1663 mutex_init(&zfsvfs
->z_hold_mtx
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
1665 rootzp
->z_zfsvfs
= zfsvfs
;
1666 VERIFY(0 == zfs_acl_ids_create(rootzp
, IS_ROOT_NODE
, &vattr
,
1667 cr
, NULL
, &acl_ids
));
1668 zfs_mknode(rootzp
, &vattr
, tx
, cr
, IS_ROOT_NODE
, &zp
, &acl_ids
);
1669 ASSERT3P(zp
, ==, rootzp
);
1670 error
= zap_add(os
, moid
, ZFS_ROOT_OBJ
, 8, 1, &rootzp
->z_id
, tx
);
1672 zfs_acl_ids_free(&acl_ids
);
1673 POINTER_INVALIDATE(&rootzp
->z_zfsvfs
);
1675 sa_handle_destroy(rootzp
->z_sa_hdl
);
1676 kmem_cache_free(znode_cache
, rootzp
);
1679 * Create shares directory
1682 error
= zfs_create_share_dir(zfsvfs
, tx
);
1686 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1687 mutex_destroy(&zfsvfs
->z_hold_mtx
[i
]);
1688 kmem_free(zfsvfs
, sizeof (zfsvfs_t
));
1690 #endif /* _KERNEL */
1693 zfs_sa_setup(objset_t
*osp
, sa_attr_type_t
**sa_table
)
1695 uint64_t sa_obj
= 0;
1698 error
= zap_lookup(osp
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1699 if (error
!= 0 && error
!= ENOENT
)
1702 error
= sa_setup(osp
, sa_obj
, zfs_attr_table
, ZPL_END
, sa_table
);
1707 zfs_grab_sa_handle(objset_t
*osp
, uint64_t obj
, sa_handle_t
**hdlp
,
1708 dmu_buf_t
**db
, void *tag
)
1710 dmu_object_info_t doi
;
1713 if ((error
= sa_buf_hold(osp
, obj
, tag
, db
)) != 0)
1716 dmu_object_info_from_db(*db
, &doi
);
1717 if ((doi
.doi_bonus_type
!= DMU_OT_SA
&&
1718 doi
.doi_bonus_type
!= DMU_OT_ZNODE
) ||
1719 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1720 doi
.doi_bonus_size
< sizeof (znode_phys_t
))) {
1721 sa_buf_rele(*db
, tag
);
1722 return (SET_ERROR(ENOTSUP
));
1725 error
= sa_handle_get(osp
, obj
, NULL
, SA_HDL_PRIVATE
, hdlp
);
1727 sa_buf_rele(*db
, tag
);
1735 zfs_release_sa_handle(sa_handle_t
*hdl
, dmu_buf_t
*db
, void *tag
)
1737 sa_handle_destroy(hdl
);
1738 sa_buf_rele(db
, tag
);
1742 * Given an object number, return its parent object number and whether
1743 * or not the object is an extended attribute directory.
1746 zfs_obj_to_pobj(objset_t
*osp
, sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
1747 uint64_t *pobjp
, int *is_xattrdir
)
1752 uint64_t parent_mode
;
1753 sa_bulk_attr_t bulk
[3];
1754 sa_handle_t
*sa_hdl
;
1759 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_PARENT
], NULL
,
1760 &parent
, sizeof (parent
));
1761 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_FLAGS
], NULL
,
1762 &pflags
, sizeof (pflags
));
1763 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1764 &mode
, sizeof (mode
));
1766 if ((error
= sa_bulk_lookup(hdl
, bulk
, count
)) != 0)
1770 * When a link is removed its parent pointer is not changed and will
1771 * be invalid. There are two cases where a link is removed but the
1772 * file stays around, when it goes to the delete queue and when there
1773 * are additional links.
1775 error
= zfs_grab_sa_handle(osp
, parent
, &sa_hdl
, &sa_db
, FTAG
);
1779 error
= sa_lookup(sa_hdl
, ZPL_MODE
, &parent_mode
, sizeof (parent_mode
));
1780 zfs_release_sa_handle(sa_hdl
, sa_db
, FTAG
);
1784 *is_xattrdir
= ((pflags
& ZFS_XATTR
) != 0) && S_ISDIR(mode
);
1787 * Extended attributes can be applied to files, directories, etc.
1788 * Otherwise the parent must be a directory.
1790 if (!*is_xattrdir
&& !S_ISDIR(parent_mode
))
1791 return (SET_ERROR(EINVAL
));
1799 * Given an object number, return some zpl level statistics
1802 zfs_obj_to_stats_impl(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
1805 sa_bulk_attr_t bulk
[4];
1808 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1809 &sb
->zs_mode
, sizeof (sb
->zs_mode
));
1810 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_GEN
], NULL
,
1811 &sb
->zs_gen
, sizeof (sb
->zs_gen
));
1812 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_LINKS
], NULL
,
1813 &sb
->zs_links
, sizeof (sb
->zs_links
));
1814 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_CTIME
], NULL
,
1815 &sb
->zs_ctime
, sizeof (sb
->zs_ctime
));
1817 return (sa_bulk_lookup(hdl
, bulk
, count
));
1821 zfs_obj_to_path_impl(objset_t
*osp
, uint64_t obj
, sa_handle_t
*hdl
,
1822 sa_attr_type_t
*sa_table
, char *buf
, int len
)
1824 sa_handle_t
*sa_hdl
;
1825 sa_handle_t
*prevhdl
= NULL
;
1826 dmu_buf_t
*prevdb
= NULL
;
1827 dmu_buf_t
*sa_db
= NULL
;
1828 char *path
= buf
+ len
- 1;
1834 uint64_t deleteq_obj
;
1835 VERIFY0(zap_lookup(osp
, MASTER_NODE_OBJ
,
1836 ZFS_UNLINKED_SET
, sizeof (uint64_t), 1, &deleteq_obj
));
1837 error
= zap_lookup_int(osp
, deleteq_obj
, obj
);
1840 } else if (error
!= ENOENT
) {
1847 char component
[MAXNAMELEN
+ 2];
1852 zfs_release_sa_handle(prevhdl
, prevdb
, FTAG
);
1854 if ((error
= zfs_obj_to_pobj(osp
, sa_hdl
, sa_table
, &pobj
,
1855 &is_xattrdir
)) != 0)
1866 (void) sprintf(component
+ 1, "<xattrdir>");
1868 error
= zap_value_search(osp
, pobj
, obj
,
1869 ZFS_DIRENT_OBJ(-1ULL), component
+ 1);
1874 complen
= strlen(component
);
1876 ASSERT(path
>= buf
);
1877 bcopy(component
, path
, complen
);
1880 if (sa_hdl
!= hdl
) {
1884 error
= zfs_grab_sa_handle(osp
, obj
, &sa_hdl
, &sa_db
, FTAG
);
1892 if (sa_hdl
!= NULL
&& sa_hdl
!= hdl
) {
1893 ASSERT(sa_db
!= NULL
);
1894 zfs_release_sa_handle(sa_hdl
, sa_db
, FTAG
);
1898 (void) memmove(buf
, path
, buf
+ len
- path
);
1904 zfs_obj_to_path(objset_t
*osp
, uint64_t obj
, char *buf
, int len
)
1906 sa_attr_type_t
*sa_table
;
1911 error
= zfs_sa_setup(osp
, &sa_table
);
1915 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
, FTAG
);
1919 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1921 zfs_release_sa_handle(hdl
, db
, FTAG
);
1926 zfs_obj_to_stats(objset_t
*osp
, uint64_t obj
, zfs_stat_t
*sb
,
1929 char *path
= buf
+ len
- 1;
1930 sa_attr_type_t
*sa_table
;
1937 error
= zfs_sa_setup(osp
, &sa_table
);
1941 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
, FTAG
);
1945 error
= zfs_obj_to_stats_impl(hdl
, sa_table
, sb
);
1947 zfs_release_sa_handle(hdl
, db
, FTAG
);
1951 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1953 zfs_release_sa_handle(hdl
, db
, FTAG
);
1959 zfs_znode_parent_and_name(znode_t
*zp
, znode_t
**dzpp
, char *buf
)
1961 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1966 /* Extended attributes should not be visible as regular files. */
1967 if ((zp
->z_pflags
& ZFS_XATTR
) != 0)
1968 return (SET_ERROR(EINVAL
));
1970 err
= zfs_obj_to_pobj(zfsvfs
->z_os
, zp
->z_sa_hdl
, zfsvfs
->z_attr_table
,
1971 &parent
, &is_xattrdir
);
1974 ASSERT0(is_xattrdir
);
1976 /* No name as this is a root object. */
1977 if (parent
== zp
->z_id
)
1978 return (SET_ERROR(EINVAL
));
1980 err
= zap_value_search(zfsvfs
->z_os
, parent
, zp
->z_id
,
1981 ZFS_DIRENT_OBJ(-1ULL), buf
);
1984 err
= zfs_zget(zfsvfs
, parent
, dzpp
);
1987 #endif /* _KERNEL */