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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/dmu_objset.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dnode.h>
34 dmu_object_alloc(objset_t
*os
, dmu_object_type_t ot
, int blocksize
,
35 dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
37 objset_impl_t
*osi
= os
->os
;
39 uint64_t L2_dnode_count
= DNODES_PER_BLOCK
<<
40 (osi
->os_meta_dnode
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
42 int restarted
= B_FALSE
;
44 mutex_enter(&osi
->os_obj_lock
);
46 object
= osi
->os_obj_next
;
48 * Each time we polish off an L2 bp worth of dnodes
49 * (2^13 objects), move to another L2 bp that's still
50 * reasonably sparse (at most 1/4 full). Look from the
51 * beginning once, but after that keep looking from here.
52 * If we can't find one, just keep going from here.
54 if (P2PHASE(object
, L2_dnode_count
) == 0) {
55 uint64_t offset
= restarted
? object
<< DNODE_SHIFT
: 0;
56 int error
= dnode_next_offset(osi
->os_meta_dnode
,
58 &offset
, 2, DNODES_PER_BLOCK
>> 2, 0);
61 object
= offset
>> DNODE_SHIFT
;
63 osi
->os_obj_next
= ++object
;
66 * XXX We should check for an i/o error here and return
67 * up to our caller. Actually we should pre-read it in
68 * dmu_tx_assign(), but there is currently no mechanism
71 (void) dnode_hold_impl(os
->os
, object
, DNODE_MUST_BE_FREE
,
76 if (dmu_object_next(os
, &object
, B_TRUE
, 0) == 0)
77 osi
->os_obj_next
= object
- 1;
80 dnode_allocate(dn
, ot
, blocksize
, 0, bonustype
, bonuslen
, tx
);
83 mutex_exit(&osi
->os_obj_lock
);
85 dmu_tx_add_new_object(tx
, os
, object
);
90 dmu_object_claim(objset_t
*os
, uint64_t object
, dmu_object_type_t ot
,
91 int blocksize
, dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
96 if (object
== DMU_META_DNODE_OBJECT
&& !dmu_tx_private_ok(tx
))
99 err
= dnode_hold_impl(os
->os
, object
, DNODE_MUST_BE_FREE
, FTAG
, &dn
);
102 dnode_allocate(dn
, ot
, blocksize
, 0, bonustype
, bonuslen
, tx
);
103 dnode_rele(dn
, FTAG
);
105 dmu_tx_add_new_object(tx
, os
, object
);
110 dmu_object_reclaim(objset_t
*os
, uint64_t object
, dmu_object_type_t ot
,
111 int blocksize
, dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
116 if (object
== DMU_META_DNODE_OBJECT
&& !dmu_tx_private_ok(tx
))
119 err
= dnode_hold_impl(os
->os
, object
, DNODE_MUST_BE_ALLOCATED
,
123 dnode_reallocate(dn
, ot
, blocksize
, bonustype
, bonuslen
, tx
);
124 dnode_rele(dn
, FTAG
);
130 dmu_object_free(objset_t
*os
, uint64_t object
, dmu_tx_t
*tx
)
135 ASSERT(object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
137 err
= dnode_hold_impl(os
->os
, object
, DNODE_MUST_BE_ALLOCATED
,
142 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
143 dnode_free_range(dn
, 0, DMU_OBJECT_END
, tx
);
145 dnode_rele(dn
, FTAG
);
151 dmu_object_next(objset_t
*os
, uint64_t *objectp
, boolean_t hole
, uint64_t txg
)
153 uint64_t offset
= (*objectp
+ 1) << DNODE_SHIFT
;
156 error
= dnode_next_offset(os
->os
->os_meta_dnode
,
157 (hole
? DNODE_FIND_HOLE
: 0), &offset
, 0, DNODES_PER_BLOCK
, txg
);
159 *objectp
= offset
>> DNODE_SHIFT
;