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.
25 /* Portions Copyright 2007 Jeremy Teo */
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
34 #include <sys/mntent.h>
35 #include <sys/mkdev.h>
36 #include <sys/u8_textprep.h>
37 #include <sys/dsl_dataset.h>
39 #include <sys/vfs_opreg.h>
40 #include <sys/vnode.h>
43 #include <sys/errno.h>
44 #include <sys/unistd.h>
46 #include <sys/atomic.h>
48 #include "fs/fs_subr.h"
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_acl.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/zfs_rlock.h>
53 #include <sys/zfs_fuid.h>
54 #include <sys/dnode.h>
55 #include <sys/fs/zfs.h>
56 #include <sys/kidmap.h>
60 #include <sys/refcount.h>
63 #include <sys/zfs_znode.h>
65 #include <sys/zfs_sa.h>
66 #include <sys/zfs_stat.h>
69 #include "zfs_comutil.h"
72 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
73 * turned on when DEBUG is also defined.
80 #define ZNODE_STAT_ADD(stat) ((stat)++)
82 #define ZNODE_STAT_ADD(stat) /* nothing */
83 #endif /* ZNODE_STATS */
86 * Functions needed for userland (ie: libzpool) are not put under
87 * #ifdef_KERNEL; the rest of the functions have dependencies
88 * (such as VFS logic) that will not compile easily in userland.
92 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
93 * be freed before it can be safely accessed.
95 krwlock_t zfsvfs_lock
;
97 static kmem_cache_t
*znode_cache
= NULL
;
101 zfs_znode_cache_constructor(void *buf
, void *arg
, int kmflags
)
105 ASSERT(!POINTER_IS_VALID(zp
->z_zfsvfs
));
107 zp
->z_vnode
= vn_alloc(kmflags
);
108 if (zp
->z_vnode
== NULL
) {
111 ZTOV(zp
)->v_data
= zp
;
113 list_link_init(&zp
->z_link_node
);
115 mutex_init(&zp
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
116 rw_init(&zp
->z_parent_lock
, NULL
, RW_DEFAULT
, NULL
);
117 rw_init(&zp
->z_name_lock
, NULL
, RW_DEFAULT
, NULL
);
118 mutex_init(&zp
->z_acl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
120 mutex_init(&zp
->z_range_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
121 avl_create(&zp
->z_range_avl
, zfs_range_compare
,
122 sizeof (rl_t
), offsetof(rl_t
, r_node
));
124 zp
->z_dirlocks
= NULL
;
125 zp
->z_acl_cached
= NULL
;
132 zfs_znode_cache_destructor(void *buf
, void *arg
)
136 ASSERT(!POINTER_IS_VALID(zp
->z_zfsvfs
));
137 ASSERT(ZTOV(zp
)->v_data
== zp
);
139 ASSERT(!list_link_active(&zp
->z_link_node
));
140 mutex_destroy(&zp
->z_lock
);
141 rw_destroy(&zp
->z_parent_lock
);
142 rw_destroy(&zp
->z_name_lock
);
143 mutex_destroy(&zp
->z_acl_lock
);
144 avl_destroy(&zp
->z_range_avl
);
145 mutex_destroy(&zp
->z_range_lock
);
147 ASSERT(zp
->z_dirlocks
== NULL
);
148 ASSERT(zp
->z_acl_cached
== NULL
);
157 rw_init(&zfsvfs_lock
, NULL
, RW_DEFAULT
, NULL
);
158 ASSERT(znode_cache
== NULL
);
159 znode_cache
= kmem_cache_create("zfs_znode_cache",
160 sizeof (znode_t
), 0, zfs_znode_cache_constructor
,
161 zfs_znode_cache_destructor
, NULL
, NULL
, NULL
, 0);
171 kmem_cache_destroy(znode_cache
);
173 rw_destroy(&zfsvfs_lock
);
178 zfs_create_share_dir(zfsvfs_t
*zfsvfs
, dmu_tx_t
*tx
)
181 zfs_acl_ids_t acl_ids
;
188 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
189 vattr
.va_type
= VDIR
;
190 vattr
.va_mode
= S_IFDIR
|0555;
191 vattr
.va_uid
= crgetuid(kcred
);
192 vattr
.va_gid
= crgetgid(kcred
);
194 sharezp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
195 ASSERT(!POINTER_IS_VALID(sharezp
->z_zfsvfs
));
196 sharezp
->z_moved
= 0;
197 sharezp
->z_unlinked
= 0;
198 sharezp
->z_atime_dirty
= 0;
199 sharezp
->z_zfsvfs
= zfsvfs
;
200 sharezp
->z_is_sa
= zfsvfs
->z_use_sa
;
206 VERIFY(0 == zfs_acl_ids_create(sharezp
, IS_ROOT_NODE
, &vattr
,
207 kcred
, NULL
, &acl_ids
));
208 zfs_mknode(sharezp
, &vattr
, tx
, kcred
, IS_ROOT_NODE
, &zp
, &acl_ids
);
209 ASSERT3P(zp
, ==, sharezp
);
210 ASSERT(!vn_in_dnlc(ZTOV(sharezp
))); /* not valid to move */
211 POINTER_INVALIDATE(&sharezp
->z_zfsvfs
);
212 error
= zap_add(zfsvfs
->z_os
, MASTER_NODE_OBJ
,
213 ZFS_SHARES_DIR
, 8, 1, &sharezp
->z_id
, tx
);
214 zfsvfs
->z_shares_dir
= sharezp
->z_id
;
216 zfs_acl_ids_free(&acl_ids
);
217 ZTOV(sharezp
)->v_count
= 0;
218 sa_handle_destroy(sharezp
->z_sa_hdl
);
219 kmem_cache_free(znode_cache
, sharezp
);
224 #endif /* HAVE_SHARE */
228 * define a couple of values we need available
229 * for both 64 and 32 bit environments.
232 #define NBITSMINOR64 32
235 #define MAXMAJ64 0xffffffffUL
238 #define MAXMIN64 0xffffffffUL
241 #endif /* HAVE_ZPL */
244 * Create special expldev for ZFS private use.
245 * Can't use standard expldev since it doesn't do
246 * what we want. The standard expldev() takes a
247 * dev32_t in LP64 and expands it to a long dev_t.
248 * We need an interface that takes a dev32_t in ILP32
249 * and expands it to a long dev_t.
252 zfs_expldev(dev_t dev
)
255 major_t major
= (major_t
)dev
>> NBITSMINOR32
& MAXMAJ32
;
256 return (((uint64_t)major
<< NBITSMINOR64
) |
257 ((minor_t
)dev
& MAXMIN32
));
264 * Special cmpldev for ZFS private use.
265 * Can't use standard cmpldev since it takes
266 * a long dev_t and compresses it to dev32_t in
267 * LP64. We need to do a compaction of a long dev_t
268 * to a dev32_t in ILP32.
271 zfs_cmpldev(uint64_t dev
)
274 minor_t minor
= (minor_t
)dev
& MAXMIN64
;
275 major_t major
= (major_t
)(dev
>> NBITSMINOR64
) & MAXMAJ64
;
277 if (major
> MAXMAJ32
|| minor
> MAXMIN32
)
280 return (((dev32_t
)major
<< NBITSMINOR32
) | minor
);
287 zfs_znode_sa_init(zfsvfs_t
*zfsvfs
, znode_t
*zp
,
288 dmu_buf_t
*db
, dmu_object_type_t obj_type
, sa_handle_t
*sa_hdl
)
290 ASSERT(!POINTER_IS_VALID(zp
->z_zfsvfs
) || (zfsvfs
== zp
->z_zfsvfs
));
291 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs
, zp
->z_id
)));
293 mutex_enter(&zp
->z_lock
);
295 ASSERT(zp
->z_sa_hdl
== NULL
);
296 ASSERT(zp
->z_acl_cached
== NULL
);
297 if (sa_hdl
== NULL
) {
298 VERIFY(0 == sa_handle_get_from_db(zfsvfs
->z_os
, db
, zp
,
299 SA_HDL_SHARED
, &zp
->z_sa_hdl
));
301 zp
->z_sa_hdl
= sa_hdl
;
302 sa_set_userp(sa_hdl
, zp
);
305 zp
->z_is_sa
= (obj_type
== DMU_OT_SA
) ? B_TRUE
: B_FALSE
;
308 * Slap on VROOT if we are the root znode
310 if (zp
->z_id
== zfsvfs
->z_root
)
311 ZTOV(zp
)->v_flag
|= VROOT
;
313 mutex_exit(&zp
->z_lock
);
318 zfs_znode_dmu_fini(znode_t
*zp
)
320 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp
->z_zfsvfs
, zp
->z_id
)) ||
322 RW_WRITE_HELD(&zp
->z_zfsvfs
->z_teardown_inactive_lock
));
324 sa_handle_destroy(zp
->z_sa_hdl
);
329 * Construct a new znode+inode and initialize.
331 * This does not do a call to dmu_set_user() that is
332 * up to the caller to do, in case you don't want to
336 zfs_znode_alloc(zfsvfs_t
*zfsvfs
, dmu_buf_t
*db
, int blksz
,
337 dmu_object_type_t obj_type
, sa_handle_t
*hdl
)
342 sa_bulk_attr_t bulk
[9];
345 ASSERT(zfsvfs
!= NULL
);
346 ASSERT(zfsvfs
->z_vfs
!= NULL
);
347 ASSERT(zfsvfs
->z_vfs
->mnt_sb
!= NULL
);
349 inode
= iget_locked(zfsvfs
->z_vfs
->mnt_sb
, db
->db_object
);
352 ASSERT(inode
->i_state
& I_NEW
);
353 ASSERT(zp
->z_dirlocks
== NULL
);
354 ASSERT(!POINTER_IS_VALID(zp
->z_zfsvfs
));
358 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
359 * the zfs_znode_move() callback.
363 zp
->z_atime_dirty
= 0;
365 zp
->z_id
= db
->db_object
;
367 zp
->z_seq
= 0x7A4653;
370 zfs_znode_sa_init(zfsvfs
, zp
, db
, obj_type
, hdl
);
372 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
374 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zfsvfs
), NULL
,
376 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
378 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
), NULL
,
380 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
382 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PARENT(zfsvfs
), NULL
,
384 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
386 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
388 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
391 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
) != 0 || zp
->z_gen
== 0) {
393 sa_handle_destroy(zp
->z_sa_hdl
);
398 inode
->i_mode
= (umode_t
)zp
->z_mode
;
399 if ((S_ISCHR(inode
->i_mode
)) || (S_ISBLK(inode
->i_mode
))) {
401 VERIFY(sa_lookup(zp
->z_sa_hdl
, SA_ZPL_RDEV(zfsvfs
),
402 &rdev
, sizeof (rdev
)) == 0);
403 inode
->i_rdev
= zfs_cmpldev(rdev
);
406 /* zp->z_set_ops_inode() must be set in sb->alloc_inode() */
407 ASSERT(zp
->z_set_ops_inode
!= NULL
);
408 zp
->z_set_ops_inode(inode
);
409 unlock_new_inode(inode
);
411 mutex_enter(&zfsvfs
->z_znodes_lock
);
412 list_insert_tail(&zfsvfs
->z_all_znodes
, zp
);
415 * Everything else must be valid before assigning z_zfsvfs makes the
416 * znode eligible for zfs_znode_move().
418 zp
->z_zfsvfs
= zfsvfs
;
419 mutex_exit(&zfsvfs
->z_znodes_lock
);
421 VFS_HOLD(zfsvfs
->z_vfs
);
426 * Update the embedded inode given the znode. We should work toward
427 * eliminating this function as soon as possible by removing values
428 * which are duplicated between the znode and inode. If the generic
429 * inode has the correct field it should be used, and the ZFS code
430 * updated to access the inode. This can be done incrementally.
433 zfs_inode_update(znode_t
*zp
)
438 uint64_t atime
[2], mtime
[2], ctime
[2];
441 zfsvfs
= zp
->z_zfsvfs
;
444 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
), &atime
, 16);
445 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MTIME(zfsvfs
), &mtime
, 16);
446 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CTIME(zfsvfs
), &ctime
, 16);
448 spin_lock(&inode
->i_lock
);
449 inode
->i_generation
= zp
->z_gen
;
450 inode
->i_uid
= zp
->z_uid
;
451 inode
->i_gid
= zp
->z_gid
;
452 inode
->i_nlink
= zp
->z_links
;
453 inode
->i_mode
= zp
->z_mode
;
454 inode
->i_blkbits
= SPA_MINBLOCKSHIFT
;
455 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &blksize
,
456 (u_longlong_t
*)&inode
->i_blocks
);
458 ZFS_TIME_DECODE(&inode
->i_atime
, atime
);
459 ZFS_TIME_DECODE(&inode
->i_mtime
, mtime
);
460 ZFS_TIME_DECODE(&inode
->i_ctime
, ctime
);
462 i_size_write(inode
, zp
->z_size
);
463 spin_unlock(&inode
->i_lock
);
466 static uint64_t empty_xattr
;
467 static uint64_t pad
[4];
468 static zfs_acl_phys_t acl_phys
;
470 * Create a new DMU object to hold a zfs znode.
472 * IN: dzp - parent directory for new znode
473 * vap - file attributes for new znode
474 * tx - dmu transaction id for zap operations
475 * cr - credentials of caller
477 * IS_ROOT_NODE - new object will be root
478 * IS_XATTR - new object is an attribute
479 * bonuslen - length of bonus buffer
480 * setaclp - File/Dir initial ACL
481 * fuidp - Tracks fuid allocation.
483 * OUT: zpp - allocated znode
487 zfs_mknode(znode_t
*dzp
, vattr_t
*vap
, dmu_tx_t
*tx
, cred_t
*cr
,
488 uint_t flag
, znode_t
**zpp
, zfs_acl_ids_t
*acl_ids
)
490 uint64_t crtime
[2], atime
[2], mtime
[2], ctime
[2];
491 uint64_t mode
, size
, links
, parent
, pflags
;
492 uint64_t dzp_pflags
= 0;
494 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
501 dmu_object_type_t obj_type
;
502 sa_bulk_attr_t
*sa_attrs
;
504 zfs_acl_locator_cb_t locate
= { 0 };
506 ASSERT(vap
&& (vap
->va_mask
& (AT_TYPE
|AT_MODE
)) == (AT_TYPE
|AT_MODE
));
508 if (zfsvfs
->z_replay
) {
509 obj
= vap
->va_nodeid
;
510 now
= vap
->va_ctime
; /* see zfs_replay_create() */
511 gen
= vap
->va_nblocks
; /* ditto */
515 gen
= dmu_tx_get_txg(tx
);
518 obj_type
= zfsvfs
->z_use_sa
? DMU_OT_SA
: DMU_OT_ZNODE
;
519 bonuslen
= (obj_type
== DMU_OT_SA
) ?
520 DN_MAX_BONUSLEN
: ZFS_OLD_ZNODE_PHYS_SIZE
;
523 * Create a new DMU object.
526 * There's currently no mechanism for pre-reading the blocks that will
527 * be needed to allocate a new object, so we accept the small chance
528 * that there will be an i/o error and we will fail one of the
531 if (vap
->va_type
== VDIR
) {
532 if (zfsvfs
->z_replay
) {
533 err
= zap_create_claim_norm(zfsvfs
->z_os
, obj
,
534 zfsvfs
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
535 obj_type
, bonuslen
, tx
);
536 ASSERT3U(err
, ==, 0);
538 obj
= zap_create_norm(zfsvfs
->z_os
,
539 zfsvfs
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
540 obj_type
, bonuslen
, tx
);
543 if (zfsvfs
->z_replay
) {
544 err
= dmu_object_claim(zfsvfs
->z_os
, obj
,
545 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
546 obj_type
, bonuslen
, tx
);
547 ASSERT3U(err
, ==, 0);
549 obj
= dmu_object_alloc(zfsvfs
->z_os
,
550 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
551 obj_type
, bonuslen
, tx
);
555 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj
);
556 VERIFY(0 == sa_buf_hold(zfsvfs
->z_os
, obj
, NULL
, &db
));
559 * If this is the root, fix up the half-initialized parent pointer
560 * to reference the just-allocated physical data area.
562 if (flag
& IS_ROOT_NODE
) {
565 dzp_pflags
= dzp
->z_pflags
;
569 * If parent is an xattr, so am I.
571 if (dzp_pflags
& ZFS_XATTR
) {
575 if (zfsvfs
->z_use_fuids
)
576 pflags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
580 if (vap
->va_type
== VDIR
) {
581 size
= 2; /* contents ("." and "..") */
582 links
= (flag
& (IS_ROOT_NODE
| IS_XATTR
)) ? 2 : 1;
587 if (vap
->va_type
== VBLK
|| vap
->va_type
== VCHR
) {
588 rdev
= zfs_expldev(vap
->va_rdev
);
592 mode
= acl_ids
->z_mode
;
597 * No execs denied will be deterimed when zfs_mode_compute() is called.
599 pflags
|= acl_ids
->z_aclp
->z_hints
&
600 (ZFS_ACL_TRIVIAL
|ZFS_INHERIT_ACE
|ZFS_ACL_AUTO_INHERIT
|
601 ZFS_ACL_DEFAULTED
|ZFS_ACL_PROTECTED
);
603 ZFS_TIME_ENCODE(&now
, crtime
);
604 ZFS_TIME_ENCODE(&now
, ctime
);
606 if (vap
->va_mask
& AT_ATIME
) {
607 ZFS_TIME_ENCODE(&vap
->va_atime
, atime
);
609 ZFS_TIME_ENCODE(&now
, atime
);
612 if (vap
->va_mask
& AT_MTIME
) {
613 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
615 ZFS_TIME_ENCODE(&now
, mtime
);
618 /* Now add in all of the "SA" attributes */
619 VERIFY(0 == sa_handle_get_from_db(zfsvfs
->z_os
, db
, NULL
, SA_HDL_SHARED
,
623 * Setup the array of attributes to be replaced/set on the new file
625 * order for DMU_OT_ZNODE is critical since it needs to be constructed
626 * in the old znode_phys_t format. Don't change this ordering
628 sa_attrs
= kmem_alloc(sizeof(sa_bulk_attr_t
) * ZPL_END
, KM_SLEEP
);
630 if (obj_type
== DMU_OT_ZNODE
) {
631 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zfsvfs
),
633 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zfsvfs
),
635 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zfsvfs
),
637 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zfsvfs
),
639 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zfsvfs
),
641 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zfsvfs
),
643 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zfsvfs
),
645 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zfsvfs
),
648 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zfsvfs
),
650 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zfsvfs
),
652 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zfsvfs
),
654 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zfsvfs
), NULL
,
655 &acl_ids
->z_fuid
, 8);
656 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zfsvfs
), NULL
,
657 &acl_ids
->z_fgid
, 8);
658 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zfsvfs
),
660 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zfsvfs
),
662 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zfsvfs
),
664 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zfsvfs
),
666 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zfsvfs
),
668 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zfsvfs
),
672 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_LINKS(zfsvfs
), NULL
, &links
, 8);
674 if (obj_type
== DMU_OT_ZNODE
) {
675 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_XATTR(zfsvfs
), NULL
,
678 if (obj_type
== DMU_OT_ZNODE
||
679 (vap
->va_type
== VBLK
|| vap
->va_type
== VCHR
)) {
680 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_RDEV(zfsvfs
),
684 if (obj_type
== DMU_OT_ZNODE
) {
685 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zfsvfs
),
687 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zfsvfs
), NULL
,
688 &acl_ids
->z_fuid
, 8);
689 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zfsvfs
), NULL
,
690 &acl_ids
->z_fgid
, 8);
691 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PAD(zfsvfs
), NULL
, pad
,
692 sizeof (uint64_t) * 4);
693 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ZNODE_ACL(zfsvfs
), NULL
,
694 &acl_phys
, sizeof (zfs_acl_phys_t
));
695 } else if (acl_ids
->z_aclp
->z_version
>= ZFS_ACL_VERSION_FUID
) {
696 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_COUNT(zfsvfs
), NULL
,
697 &acl_ids
->z_aclp
->z_acl_count
, 8);
698 locate
.cb_aclp
= acl_ids
->z_aclp
;
699 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_ACES(zfsvfs
),
700 zfs_acl_data_locator
, &locate
,
701 acl_ids
->z_aclp
->z_acl_bytes
);
702 mode
= zfs_mode_compute(mode
, acl_ids
->z_aclp
, &pflags
,
703 acl_ids
->z_fuid
, acl_ids
->z_fgid
);
706 VERIFY(sa_replace_all_by_template(sa_hdl
, sa_attrs
, cnt
, tx
) == 0);
708 if (!(flag
& IS_ROOT_NODE
)) {
709 *zpp
= zfs_znode_alloc(zfsvfs
, db
, 0, obj_type
, sa_hdl
);
710 ASSERT(*zpp
!= NULL
);
713 * If we are creating the root node, the "parent" we
714 * passed in is the znode for the root.
718 (*zpp
)->z_sa_hdl
= sa_hdl
;
721 (*zpp
)->z_pflags
= pflags
;
722 (*zpp
)->z_mode
= mode
;
724 if (vap
->va_mask
& AT_XVATTR
)
725 zfs_xvattr_set(*zpp
, (xvattr_t
*)vap
, tx
);
727 if (obj_type
== DMU_OT_ZNODE
||
728 acl_ids
->z_aclp
->z_version
< ZFS_ACL_VERSION_FUID
) {
729 err
= zfs_aclset_common(*zpp
, acl_ids
->z_aclp
, cr
, tx
);
730 ASSERT3S(err
, ==, 0);
732 kmem_free(sa_attrs
, sizeof(sa_bulk_attr_t
) * ZPL_END
);
733 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj
);
737 * zfs_xvattr_set only updates the in-core attributes
738 * it is assumed the caller will be doing an sa_bulk_update
739 * to push the changes out
742 zfs_xvattr_set(znode_t
*zp
, xvattr_t
*xvap
, dmu_tx_t
*tx
)
747 xoap
= xva_getxoptattr(xvap
);
750 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
752 ZFS_TIME_ENCODE(&xoap
->xoa_createtime
, times
);
753 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zp
->z_zfsvfs
),
754 ×
, sizeof (times
), tx
);
755 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
757 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
758 ZFS_ATTR_SET(zp
, ZFS_READONLY
, xoap
->xoa_readonly
,
760 XVA_SET_RTN(xvap
, XAT_READONLY
);
762 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
763 ZFS_ATTR_SET(zp
, ZFS_HIDDEN
, xoap
->xoa_hidden
,
765 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
767 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
768 ZFS_ATTR_SET(zp
, ZFS_SYSTEM
, xoap
->xoa_system
,
770 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
772 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
773 ZFS_ATTR_SET(zp
, ZFS_ARCHIVE
, xoap
->xoa_archive
,
775 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
777 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
778 ZFS_ATTR_SET(zp
, ZFS_IMMUTABLE
, xoap
->xoa_immutable
,
780 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
782 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
783 ZFS_ATTR_SET(zp
, ZFS_NOUNLINK
, xoap
->xoa_nounlink
,
785 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
787 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
788 ZFS_ATTR_SET(zp
, ZFS_APPENDONLY
, xoap
->xoa_appendonly
,
790 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
792 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
793 ZFS_ATTR_SET(zp
, ZFS_NODUMP
, xoap
->xoa_nodump
,
795 XVA_SET_RTN(xvap
, XAT_NODUMP
);
797 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
798 ZFS_ATTR_SET(zp
, ZFS_OPAQUE
, xoap
->xoa_opaque
,
800 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
802 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
803 ZFS_ATTR_SET(zp
, ZFS_AV_QUARANTINED
,
804 xoap
->xoa_av_quarantined
, zp
->z_pflags
, tx
);
805 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
807 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
808 ZFS_ATTR_SET(zp
, ZFS_AV_MODIFIED
, xoap
->xoa_av_modified
,
810 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
812 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
813 zfs_sa_set_scanstamp(zp
, xvap
, tx
);
814 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
816 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
817 ZFS_ATTR_SET(zp
, ZFS_REPARSE
, xoap
->xoa_reparse
,
819 XVA_SET_RTN(xvap
, XAT_REPARSE
);
821 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
822 ZFS_ATTR_SET(zp
, ZFS_OFFLINE
, xoap
->xoa_offline
,
824 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
826 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
827 ZFS_ATTR_SET(zp
, ZFS_SPARSE
, xoap
->xoa_sparse
,
829 XVA_SET_RTN(xvap
, XAT_SPARSE
);
831 #endif /* HAVE_XVATTR */
835 zfs_zget(zfsvfs_t
*zfsvfs
, uint64_t obj_num
, znode_t
**zpp
)
837 dmu_object_info_t doi
;
845 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj_num
);
847 err
= sa_buf_hold(zfsvfs
->z_os
, obj_num
, NULL
, &db
);
849 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
853 dmu_object_info_from_db(db
, &doi
);
854 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
855 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
856 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
857 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
858 sa_buf_rele(db
, NULL
);
859 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
863 hdl
= dmu_buf_get_user(db
);
865 zp
= sa_get_userdata(hdl
);
869 * Since "SA" does immediate eviction we
870 * should never find a sa handle that doesn't
871 * know about the znode.
874 ASSERT3P(zp
, !=, NULL
);
876 mutex_enter(&zp
->z_lock
);
877 ASSERT3U(zp
->z_id
, ==, obj_num
);
878 if (zp
->z_unlinked
) {
885 sa_buf_rele(db
, NULL
);
886 mutex_exit(&zp
->z_lock
);
887 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
892 * Not found create new znode/vnode
893 * but only if file exists.
895 * There is a small window where zfs_vget() could
896 * find this object while a file create is still in
897 * progress. This is checked for in zfs_znode_alloc()
899 * if zfs_znode_alloc() fails it will drop the hold on the
902 zp
= zfs_znode_alloc(zfsvfs
, db
, doi
.doi_data_block_size
,
903 doi
.doi_bonus_type
, NULL
);
909 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
914 zfs_rezget(znode_t
*zp
)
916 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
917 dmu_object_info_t doi
;
919 uint64_t obj_num
= zp
->z_id
;
921 sa_bulk_attr_t bulk
[8];
926 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj_num
);
928 mutex_enter(&zp
->z_acl_lock
);
929 if (zp
->z_acl_cached
) {
930 zfs_acl_free(zp
->z_acl_cached
);
931 zp
->z_acl_cached
= NULL
;
934 mutex_exit(&zp
->z_acl_lock
);
935 ASSERT(zp
->z_sa_hdl
== NULL
);
936 err
= sa_buf_hold(zfsvfs
->z_os
, obj_num
, NULL
, &db
);
938 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
942 dmu_object_info_from_db(db
, &doi
);
943 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
944 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
945 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
946 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
947 sa_buf_rele(db
, NULL
);
948 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
952 zfs_znode_sa_init(zfsvfs
, zp
, db
, doi
.doi_bonus_type
, NULL
);
954 /* reload cached values */
955 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zfsvfs
), NULL
,
957 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
958 &zp
->z_size
, sizeof (zp
->z_size
));
959 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
), NULL
,
960 &zp
->z_links
, sizeof (zp
->z_links
));
961 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
962 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
963 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
964 &zp
->z_atime
, sizeof (zp
->z_atime
));
965 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
966 &zp
->z_uid
, sizeof (zp
->z_uid
));
967 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
968 &zp
->z_gid
, sizeof (zp
->z_gid
));
969 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
970 &mode
, sizeof (mode
));
972 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) {
973 zfs_znode_dmu_fini(zp
);
974 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
980 if (gen
!= zp
->z_gen
) {
981 zfs_znode_dmu_fini(zp
);
982 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
986 zp
->z_unlinked
= (zp
->z_links
== 0);
987 zp
->z_blksz
= doi
.doi_data_block_size
;
989 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
995 zfs_znode_delete(znode_t
*zp
, dmu_tx_t
*tx
)
997 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
998 objset_t
*os
= zfsvfs
->z_os
;
999 uint64_t obj
= zp
->z_id
;
1000 uint64_t acl_obj
= zfs_external_acl(zp
);
1002 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj
);
1004 VERIFY(!zp
->z_is_sa
);
1005 VERIFY(0 == dmu_object_free(os
, acl_obj
, tx
));
1007 VERIFY(0 == dmu_object_free(os
, obj
, tx
));
1008 zfs_znode_dmu_fini(zp
);
1009 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj
);
1014 zfs_zinactive(znode_t
*zp
)
1016 vnode_t
*vp
= ZTOV(zp
);
1017 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1018 uint64_t z_id
= zp
->z_id
;
1020 ASSERT(zp
->z_sa_hdl
);
1023 * Don't allow a zfs_zget() while were trying to release this znode
1025 ZFS_OBJ_HOLD_ENTER(zfsvfs
, z_id
);
1027 mutex_enter(&zp
->z_lock
);
1028 mutex_enter(&vp
->v_lock
);
1030 if (vp
->v_count
> 0 || vn_has_cached_data(vp
)) {
1032 * If the hold count is greater than zero, somebody has
1033 * obtained a new reference on this znode while we were
1034 * processing it here, so we are done. If we still have
1035 * mapped pages then we are also done, since we don't
1036 * want to inactivate the znode until the pages get pushed.
1038 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1039 * this seems like it would leave the znode hanging with
1040 * no chance to go inactive...
1042 mutex_exit(&vp
->v_lock
);
1043 mutex_exit(&zp
->z_lock
);
1044 ZFS_OBJ_HOLD_EXIT(zfsvfs
, z_id
);
1047 mutex_exit(&vp
->v_lock
);
1050 * If this was the last reference to a file with no links,
1051 * remove the file from the file system.
1053 if (zp
->z_unlinked
) {
1054 mutex_exit(&zp
->z_lock
);
1055 ZFS_OBJ_HOLD_EXIT(zfsvfs
, z_id
);
1060 mutex_exit(&zp
->z_lock
);
1061 zfs_znode_dmu_fini(zp
);
1062 ZFS_OBJ_HOLD_EXIT(zfsvfs
, z_id
);
1067 zfs_znode_free(znode_t
*zp
)
1069 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1071 vn_invalid(ZTOV(zp
));
1073 ASSERT(ZTOV(zp
)->v_count
== 0);
1075 mutex_enter(&zfsvfs
->z_znodes_lock
);
1076 POINTER_INVALIDATE(&zp
->z_zfsvfs
);
1077 list_remove(&zfsvfs
->z_all_znodes
, zp
);
1078 mutex_exit(&zfsvfs
->z_znodes_lock
);
1080 if (zp
->z_acl_cached
) {
1081 zfs_acl_free(zp
->z_acl_cached
);
1082 zp
->z_acl_cached
= NULL
;
1085 kmem_cache_free(znode_cache
, zp
);
1087 VFS_RELE(zfsvfs
->z_vfs
);
1091 zfs_tstamp_update_setup(znode_t
*zp
, uint_t flag
, uint64_t mtime
[2],
1092 uint64_t ctime
[2], boolean_t have_tx
)
1098 if (have_tx
) { /* will sa_bulk_update happen really soon? */
1099 zp
->z_atime_dirty
= 0;
1102 zp
->z_atime_dirty
= 1;
1105 if (flag
& AT_ATIME
) {
1106 ZFS_TIME_ENCODE(&now
, zp
->z_atime
);
1109 if (flag
& AT_MTIME
) {
1110 ZFS_TIME_ENCODE(&now
, mtime
);
1111 if (zp
->z_zfsvfs
->z_use_fuids
) {
1112 zp
->z_pflags
|= (ZFS_ARCHIVE
|
1117 if (flag
& AT_CTIME
) {
1118 ZFS_TIME_ENCODE(&now
, ctime
);
1119 if (zp
->z_zfsvfs
->z_use_fuids
)
1120 zp
->z_pflags
|= ZFS_ARCHIVE
;
1125 * Grow the block size for a file.
1127 * IN: zp - znode of file to free data in.
1128 * size - requested block size
1129 * tx - open transaction.
1131 * NOTE: this function assumes that the znode is write locked.
1134 zfs_grow_blocksize(znode_t
*zp
, uint64_t size
, dmu_tx_t
*tx
)
1139 if (size
<= zp
->z_blksz
)
1142 * If the file size is already greater than the current blocksize,
1143 * we will not grow. If there is more than one block in a file,
1144 * the blocksize cannot change.
1146 if (zp
->z_blksz
&& zp
->z_size
> zp
->z_blksz
)
1149 error
= dmu_object_set_blocksize(zp
->z_zfsvfs
->z_os
, zp
->z_id
,
1152 if (error
== ENOTSUP
)
1154 ASSERT3U(error
, ==, 0);
1156 /* What blocksize did we actually get? */
1157 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &zp
->z_blksz
, &dummy
);
1162 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1163 * be calling back into the fs for a putpage(). E.g.: when truncating
1164 * a file, the pages being "thrown away* don't need to be written out.
1168 zfs_no_putpage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
, size_t *lenp
,
1169 int flags
, cred_t
*cr
)
1174 #endif /* HAVE_ZPL */
1177 * Increase the file length
1179 * IN: zp - znode of file to free data in.
1180 * end - new end-of-file
1182 * RETURN: 0 if success
1183 * error code if failure
1186 zfs_extend(znode_t
*zp
, uint64_t end
)
1188 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1195 * We will change zp_size, lock the whole file.
1197 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1200 * Nothing to do if file already at desired length.
1202 if (end
<= zp
->z_size
) {
1203 zfs_range_unlock(rl
);
1207 tx
= dmu_tx_create(zfsvfs
->z_os
);
1208 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1209 zfs_sa_upgrade_txholds(tx
, zp
);
1210 if (end
> zp
->z_blksz
&&
1211 (!ISP2(zp
->z_blksz
) || zp
->z_blksz
< zfsvfs
->z_max_blksz
)) {
1213 * We are growing the file past the current block size.
1215 if (zp
->z_blksz
> zp
->z_zfsvfs
->z_max_blksz
) {
1216 ASSERT(!ISP2(zp
->z_blksz
));
1217 newblksz
= MIN(end
, SPA_MAXBLOCKSIZE
);
1219 newblksz
= MIN(end
, zp
->z_zfsvfs
->z_max_blksz
);
1221 dmu_tx_hold_write(tx
, zp
->z_id
, 0, newblksz
);
1226 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1228 if (error
== ERESTART
) {
1234 zfs_range_unlock(rl
);
1239 zfs_grow_blocksize(zp
, newblksz
, tx
);
1243 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zp
->z_zfsvfs
),
1244 &zp
->z_size
, sizeof (zp
->z_size
), tx
));
1246 zfs_range_unlock(rl
);
1254 * Free space in a file.
1256 * IN: zp - znode of file to free data in.
1257 * off - start of section to free.
1258 * len - length of section to free.
1260 * RETURN: 0 if success
1261 * error code if failure
1264 zfs_free_range(znode_t
*zp
, uint64_t off
, uint64_t len
)
1266 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1271 * Lock the range being freed.
1273 rl
= zfs_range_lock(zp
, off
, len
, RL_WRITER
);
1276 * Nothing to do if file already at desired length.
1278 if (off
>= zp
->z_size
) {
1279 zfs_range_unlock(rl
);
1283 if (off
+ len
> zp
->z_size
)
1284 len
= zp
->z_size
- off
;
1286 error
= dmu_free_long_range(zfsvfs
->z_os
, zp
->z_id
, off
, len
);
1288 zfs_range_unlock(rl
);
1296 * IN: zp - znode of file to free data in.
1297 * end - new end-of-file.
1299 * RETURN: 0 if success
1300 * error code if failure
1303 zfs_trunc(znode_t
*zp
, uint64_t end
)
1305 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1307 vnode_t
*vp
= ZTOV(zp
);
1308 #endif /* HAVE_ZPL */
1312 sa_bulk_attr_t bulk
[2];
1316 * We will change zp_size, lock the whole file.
1318 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1321 * Nothing to do if file already at desired length.
1323 if (end
>= zp
->z_size
) {
1324 zfs_range_unlock(rl
);
1328 error
= dmu_free_long_range(zfsvfs
->z_os
, zp
->z_id
, end
, -1);
1330 zfs_range_unlock(rl
);
1334 tx
= dmu_tx_create(zfsvfs
->z_os
);
1335 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1336 zfs_sa_upgrade_txholds(tx
, zp
);
1337 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1339 if (error
== ERESTART
) {
1345 zfs_range_unlock(rl
);
1350 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
),
1351 NULL
, &zp
->z_size
, sizeof (zp
->z_size
));
1354 zp
->z_pflags
&= ~ZFS_SPARSE
;
1355 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
),
1356 NULL
, &zp
->z_pflags
, 8);
1358 VERIFY(sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
) == 0);
1364 * Clear any mapped pages in the truncated region. This has to
1365 * happen outside of the transaction to avoid the possibility of
1366 * a deadlock with someone trying to push a page that we are
1367 * about to invalidate.
1369 if (vn_has_cached_data(vp
)) {
1371 uint64_t start
= end
& PAGEMASK
;
1372 int poff
= end
& PAGEOFFSET
;
1374 if (poff
!= 0 && (pp
= page_lookup(vp
, start
, SE_SHARED
))) {
1376 * We need to zero a partial page.
1378 pagezero(pp
, poff
, PAGESIZE
- poff
);
1382 error
= pvn_vplist_dirty(vp
, start
, zfs_no_putpage
,
1383 B_INVAL
| B_TRUNC
, NULL
);
1386 #endif /* HAVE_ZPL */
1388 zfs_range_unlock(rl
);
1394 * Free space in a file
1396 * IN: zp - znode of file to free data in.
1397 * off - start of range
1398 * len - end of range (0 => EOF)
1399 * flag - current file open mode flags.
1400 * log - TRUE if this action should be logged
1402 * RETURN: 0 if success
1403 * error code if failure
1406 zfs_freesp(znode_t
*zp
, uint64_t off
, uint64_t len
, int flag
, boolean_t log
)
1409 vnode_t
*vp
= ZTOV(zp
);
1410 #endif /* HAVE_ZPL */
1412 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1413 zilog_t
*zilog
= zfsvfs
->z_log
;
1415 uint64_t mtime
[2], ctime
[2];
1416 sa_bulk_attr_t bulk
[3];
1420 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MODE(zfsvfs
), &mode
,
1421 sizeof (mode
))) != 0)
1424 if (off
> zp
->z_size
) {
1425 error
= zfs_extend(zp
, off
+len
);
1426 if (error
== 0 && log
)
1434 * Check for any locks in the region to be freed.
1437 if (MANDLOCK(vp
, (mode_t
)mode
)) {
1438 uint64_t length
= (len
? len
: zp
->z_size
- off
);
1439 if (error
= chklock(vp
, FWRITE
, off
, length
, flag
, NULL
))
1442 #endif /* HAVE_ZPL */
1445 error
= zfs_trunc(zp
, off
);
1447 if ((error
= zfs_free_range(zp
, off
, len
)) == 0 &&
1448 off
+ len
> zp
->z_size
)
1449 error
= zfs_extend(zp
, off
+len
);
1454 tx
= dmu_tx_create(zfsvfs
->z_os
);
1455 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1456 zfs_sa_upgrade_txholds(tx
, zp
);
1457 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1459 if (error
== ERESTART
) {
1468 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, mtime
, 16);
1469 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, ctime
, 16);
1470 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
),
1471 NULL
, &zp
->z_pflags
, 8);
1472 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
, B_TRUE
);
1473 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1476 zfs_log_truncate(zilog
, tx
, TX_TRUNCATE
, zp
, off
, len
);
1479 zfs_inode_update(zp
);
1484 zfs_create_fs(objset_t
*os
, cred_t
*cr
, nvlist_t
*zplprops
, dmu_tx_t
*tx
)
1486 uint64_t moid
, obj
, sa_obj
, version
;
1487 uint64_t sense
= ZFS_CASE_SENSITIVE
;
1494 znode_t
*rootzp
= NULL
;
1498 zfs_acl_ids_t acl_ids
;
1503 #endif /* HAVE_ZPL */
1506 * First attempt to create master node.
1509 * In an empty objset, there are no blocks to read and thus
1510 * there can be no i/o errors (which we assert below).
1512 moid
= MASTER_NODE_OBJ
;
1513 error
= zap_create_claim(os
, moid
, DMU_OT_MASTER_NODE
,
1514 DMU_OT_NONE
, 0, tx
);
1518 * Set starting attributes.
1520 version
= zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)));
1522 while ((elem
= nvlist_next_nvpair(zplprops
, elem
)) != NULL
) {
1523 /* For the moment we expect all zpl props to be uint64_ts */
1527 ASSERT(nvpair_type(elem
) == DATA_TYPE_UINT64
);
1528 VERIFY(nvpair_value_uint64(elem
, &val
) == 0);
1529 name
= nvpair_name(elem
);
1530 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_VERSION
)) == 0) {
1534 error
= zap_update(os
, moid
, name
, 8, 1, &val
, tx
);
1537 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_NORMALIZE
)) == 0)
1539 else if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_CASE
)) == 0)
1542 ASSERT(version
!= 0);
1543 error
= zap_update(os
, moid
, ZPL_VERSION_STR
, 8, 1, &version
, tx
);
1546 * Create zap object used for SA attribute registration
1549 if (version
>= ZPL_VERSION_SA
) {
1550 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1551 DMU_OT_NONE
, 0, tx
);
1552 error
= zap_add(os
, moid
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1558 * Create a delete queue.
1560 obj
= zap_create(os
, DMU_OT_UNLINKED_SET
, DMU_OT_NONE
, 0, tx
);
1562 error
= zap_add(os
, moid
, ZFS_UNLINKED_SET
, 8, 1, &obj
, tx
);
1567 * Create root znode. Create minimal znode/vnode/zfsvfs
1568 * to allow zfs_mknode to work.
1570 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
1571 vattr
.va_type
= VDIR
;
1572 vattr
.va_mode
= S_IFDIR
|0755;
1573 vattr
.va_uid
= crgetuid(cr
);
1574 vattr
.va_gid
= crgetgid(cr
);
1576 rootzp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
1577 ASSERT(!POINTER_IS_VALID(rootzp
->z_zfsvfs
));
1578 rootzp
->z_moved
= 0;
1579 rootzp
->z_unlinked
= 0;
1580 rootzp
->z_atime_dirty
= 0;
1581 rootzp
->z_is_sa
= USE_SA(version
, os
);
1587 bzero(&zfsvfs
, sizeof (zfsvfs_t
));
1590 zfsvfs
.z_parent
= &zfsvfs
;
1591 zfsvfs
.z_version
= version
;
1592 zfsvfs
.z_use_fuids
= USE_FUIDS(version
, os
);
1593 zfsvfs
.z_use_sa
= USE_SA(version
, os
);
1594 zfsvfs
.z_norm
= norm
;
1596 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
1597 &zfsvfs
.z_attr_table
);
1602 * Fold case on file systems that are always or sometimes case
1605 if (sense
== ZFS_CASE_INSENSITIVE
|| sense
== ZFS_CASE_MIXED
)
1606 zfsvfs
.z_norm
|= U8_TEXTPREP_TOUPPER
;
1608 /* XXX - This must be destroyed but I'm not quite sure yet so
1609 * I'm just annotating that fact when it's an issue. -Brian */
1610 mutex_init(&zfsvfs
.z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1611 list_create(&zfsvfs
.z_all_znodes
, sizeof (znode_t
),
1612 offsetof(znode_t
, z_link_node
));
1614 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1615 mutex_init(&zfsvfs
.z_hold_mtx
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
1617 rootzp
->z_zfsvfs
= &zfsvfs
;
1618 VERIFY(0 == zfs_acl_ids_create(rootzp
, IS_ROOT_NODE
, &vattr
,
1619 cr
, NULL
, &acl_ids
));
1620 zfs_mknode(rootzp
, &vattr
, tx
, cr
, IS_ROOT_NODE
, &zp
, &acl_ids
);
1621 ASSERT3P(zp
, ==, rootzp
);
1622 ASSERT(!vn_in_dnlc(ZTOV(rootzp
))); /* not valid to move */
1623 error
= zap_add(os
, moid
, ZFS_ROOT_OBJ
, 8, 1, &rootzp
->z_id
, tx
);
1625 zfs_acl_ids_free(&acl_ids
);
1626 POINTER_INVALIDATE(&rootzp
->z_zfsvfs
);
1628 ZTOV(rootzp
)->v_count
= 0;
1629 sa_handle_destroy(rootzp
->z_sa_hdl
);
1630 kmem_cache_free(znode_cache
, rootzp
);
1631 error
= zfs_create_share_dir(&zfsvfs
, tx
);
1633 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1634 mutex_destroy(&zfsvfs
.z_hold_mtx
[i
]);
1637 * Create root znode with code free of VFS dependencies
1639 obj
= zap_create_norm(os
, norm
, DMU_OT_DIRECTORY_CONTENTS
,
1640 DMU_OT_ZNODE
, sizeof (znode_phys_t
), tx
);
1642 VERIFY(0 == dmu_bonus_hold(os
, obj
, FTAG
, &db
));
1643 dmu_buf_will_dirty(db
, tx
);
1646 * Initialize the znode physical data to zero.
1648 ASSERT(db
->db_size
>= sizeof (znode_phys_t
));
1649 bzero(db
->db_data
, db
->db_size
);
1652 if (USE_FUIDS(version
, os
))
1653 pzp
->zp_flags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
1655 pzp
->zp_size
= 2; /* "." and ".." */
1657 pzp
->zp_parent
= obj
;
1658 pzp
->zp_gen
= dmu_tx_get_txg(tx
);
1659 pzp
->zp_mode
= S_IFDIR
| 0755;
1660 pzp
->zp_flags
= ZFS_ACL_TRIVIAL
;
1664 ZFS_TIME_ENCODE(&now
, pzp
->zp_crtime
);
1665 ZFS_TIME_ENCODE(&now
, pzp
->zp_ctime
);
1666 ZFS_TIME_ENCODE(&now
, pzp
->zp_atime
);
1667 ZFS_TIME_ENCODE(&now
, pzp
->zp_mtime
);
1669 error
= zap_add(os
, moid
, ZFS_ROOT_OBJ
, 8, 1, &obj
, tx
);
1672 dmu_buf_rele(db
, FTAG
);
1673 #endif /* HAVE_ZPL */
1676 #endif /* _KERNEL */
1679 zfs_sa_setup(objset_t
*osp
, sa_attr_type_t
**sa_table
)
1681 uint64_t sa_obj
= 0;
1684 error
= zap_lookup(osp
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1685 if (error
!= 0 && error
!= ENOENT
)
1688 error
= sa_setup(osp
, sa_obj
, zfs_attr_table
, ZPL_END
, sa_table
);
1693 zfs_grab_sa_handle(objset_t
*osp
, uint64_t obj
, sa_handle_t
**hdlp
,
1696 dmu_object_info_t doi
;
1699 if ((error
= sa_buf_hold(osp
, obj
, FTAG
, db
)) != 0)
1702 dmu_object_info_from_db(*db
, &doi
);
1703 if ((doi
.doi_bonus_type
!= DMU_OT_SA
&&
1704 doi
.doi_bonus_type
!= DMU_OT_ZNODE
) ||
1705 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1706 doi
.doi_bonus_size
< sizeof (znode_phys_t
))) {
1707 sa_buf_rele(*db
, FTAG
);
1711 error
= sa_handle_get(osp
, obj
, NULL
, SA_HDL_PRIVATE
, hdlp
);
1713 sa_buf_rele(*db
, FTAG
);
1721 zfs_release_sa_handle(sa_handle_t
*hdl
, dmu_buf_t
*db
)
1723 sa_handle_destroy(hdl
);
1724 sa_buf_rele(db
, FTAG
);
1728 * Given an object number, return its parent object number and whether
1729 * or not the object is an extended attribute directory.
1732 zfs_obj_to_pobj(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
, uint64_t *pobjp
,
1738 sa_bulk_attr_t bulk
[3];
1742 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_PARENT
], NULL
,
1743 &parent
, sizeof (parent
));
1744 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_FLAGS
], NULL
,
1745 &pflags
, sizeof (pflags
));
1746 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1747 &mode
, sizeof (mode
));
1749 if ((error
= sa_bulk_lookup(hdl
, bulk
, count
)) != 0)
1753 *is_xattrdir
= ((pflags
& ZFS_XATTR
) != 0) && S_ISDIR(mode
);
1759 * Given an object number, return some zpl level statistics
1762 zfs_obj_to_stats_impl(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
1765 sa_bulk_attr_t bulk
[4];
1768 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1769 &sb
->zs_mode
, sizeof (sb
->zs_mode
));
1770 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_GEN
], NULL
,
1771 &sb
->zs_gen
, sizeof (sb
->zs_gen
));
1772 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_LINKS
], NULL
,
1773 &sb
->zs_links
, sizeof (sb
->zs_links
));
1774 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_CTIME
], NULL
,
1775 &sb
->zs_ctime
, sizeof (sb
->zs_ctime
));
1777 return (sa_bulk_lookup(hdl
, bulk
, count
));
1781 zfs_obj_to_path_impl(objset_t
*osp
, uint64_t obj
, sa_handle_t
*hdl
,
1782 sa_attr_type_t
*sa_table
, char *buf
, int len
)
1784 sa_handle_t
*sa_hdl
;
1785 sa_handle_t
*prevhdl
= NULL
;
1786 dmu_buf_t
*prevdb
= NULL
;
1787 dmu_buf_t
*sa_db
= NULL
;
1788 char *path
= buf
+ len
- 1;
1796 char component
[MAXNAMELEN
+ 2];
1801 zfs_release_sa_handle(prevhdl
, prevdb
);
1803 if ((error
= zfs_obj_to_pobj(sa_hdl
, sa_table
, &pobj
,
1804 &is_xattrdir
)) != 0)
1815 (void) sprintf(component
+ 1, "<xattrdir>");
1817 error
= zap_value_search(osp
, pobj
, obj
,
1818 ZFS_DIRENT_OBJ(-1ULL), component
+ 1);
1823 complen
= strlen(component
);
1825 ASSERT(path
>= buf
);
1826 bcopy(component
, path
, complen
);
1829 if (sa_hdl
!= hdl
) {
1833 error
= zfs_grab_sa_handle(osp
, obj
, &sa_hdl
, &sa_db
);
1841 if (sa_hdl
!= NULL
&& sa_hdl
!= hdl
) {
1842 ASSERT(sa_db
!= NULL
);
1843 zfs_release_sa_handle(sa_hdl
, sa_db
);
1847 (void) memmove(buf
, path
, buf
+ len
- path
);
1853 zfs_obj_to_path(objset_t
*osp
, uint64_t obj
, char *buf
, int len
)
1855 sa_attr_type_t
*sa_table
;
1860 error
= zfs_sa_setup(osp
, &sa_table
);
1864 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
);
1868 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1870 zfs_release_sa_handle(hdl
, db
);
1875 zfs_obj_to_stats(objset_t
*osp
, uint64_t obj
, zfs_stat_t
*sb
,
1878 char *path
= buf
+ len
- 1;
1879 sa_attr_type_t
*sa_table
;
1886 error
= zfs_sa_setup(osp
, &sa_table
);
1890 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
);
1894 error
= zfs_obj_to_stats_impl(hdl
, sa_table
, sb
);
1896 zfs_release_sa_handle(hdl
, db
);
1900 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1902 zfs_release_sa_handle(hdl
, db
);
1906 #if defined(_KERNEL) && defined(HAVE_SPL)
1907 EXPORT_SYMBOL(zfs_create_fs
);
1908 EXPORT_SYMBOL(zfs_obj_to_path
);