4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
27 #include <sys/dmu_objset.h>
28 #include <sys/dmu_tx.h>
29 #include <sys/dnode.h>
32 dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize,
33 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
36 uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
37 (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
39 int restarted = B_FALSE;
41 mutex_enter(&os->os_obj_lock);
43 object = os->os_obj_next;
45 * Each time we polish off an L2 bp worth of dnodes
46 * (2^13 objects), move to another L2 bp that's still
47 * reasonably sparse (at most 1/4 full). Look from the
48 * beginning once, but after that keep looking from here.
49 * If we can't find one, just keep going from here.
51 if (P2PHASE(object, L2_dnode_count) == 0) {
52 uint64_t offset = restarted ? object << DNODE_SHIFT : 0;
53 int error = dnode_next_offset(DMU_META_DNODE(os),
55 &offset, 2, DNODES_PER_BLOCK >> 2, 0);
58 object = offset >> DNODE_SHIFT;
60 os->os_obj_next = ++object;
63 * XXX We should check for an i/o error here and return
64 * up to our caller. Actually we should pre-read it in
65 * dmu_tx_assign(), but there is currently no mechanism
68 (void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
73 if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
74 os->os_obj_next = object - 1;
77 dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
80 mutex_exit(&os->os_obj_lock);
82 dmu_tx_add_new_object(tx, os, object);
87 dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
88 int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
93 if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
94 return (SET_ERROR(EBADF));
96 err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
99 dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
100 dnode_rele(dn, FTAG);
102 dmu_tx_add_new_object(tx, os, object);
107 dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
108 int blocksize, dmu_object_type_t bonustype, int bonuslen)
115 if (object == DMU_META_DNODE_OBJECT)
116 return (SET_ERROR(EBADF));
118 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
123 if (dn->dn_type == ot && dn->dn_datablksz == blocksize &&
124 dn->dn_bonustype == bonustype && dn->dn_bonuslen == bonuslen) {
125 /* nothing is changing, this is a noop */
126 dnode_rele(dn, FTAG);
130 if (bonustype == DMU_OT_SA) {
133 nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
137 * If we are losing blkptrs or changing the block size this must
138 * be a new file instance. We must clear out the previous file
139 * contents before we can change this type of metadata in the dnode.
141 if (dn->dn_nblkptr > nblkptr || dn->dn_datablksz != blocksize) {
142 err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
147 tx = dmu_tx_create(os);
148 dmu_tx_hold_bonus(tx, object);
149 err = dmu_tx_assign(tx, TXG_WAIT);
155 dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
159 dnode_rele(dn, FTAG);
165 dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
170 ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
172 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
177 ASSERT(dn->dn_type != DMU_OT_NONE);
178 dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
180 dnode_rele(dn, FTAG);
186 dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
188 uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
191 error = dnode_next_offset(DMU_META_DNODE(os),
192 (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
194 *objectp = offset >> DNODE_SHIFT;