1789Sahrens /*
2789Sahrens * CDDL HEADER START
3789Sahrens *
4789Sahrens * The contents of this file are subject to the terms of the
51544Seschrock * Common Development and Distribution License (the "License").
61544Seschrock * You may not use this file except in compliance with the License.
7789Sahrens *
8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens * or http://www.opensolaris.org/os/licensing.
10789Sahrens * See the License for the specific language governing permissions
11789Sahrens * and limitations under the License.
12789Sahrens *
13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens *
19789Sahrens * CDDL HEADER END
20789Sahrens */
21789Sahrens /*
2212178SMark.Shellenbaum@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens */
24789Sahrens
25789Sahrens #include <sys/dmu.h>
26789Sahrens #include <sys/dmu_objset.h>
27789Sahrens #include <sys/dmu_tx.h>
28789Sahrens #include <sys/dnode.h>
29789Sahrens
30789Sahrens uint64_t
dmu_object_alloc(objset_t * os,dmu_object_type_t ot,int blocksize,dmu_object_type_t bonustype,int bonuslen,dmu_tx_t * tx)31789Sahrens dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize,
32789Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
33789Sahrens {
34789Sahrens uint64_t object;
35789Sahrens uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
36*12684STom.Erickson@Sun.COM (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
371544Seschrock dnode_t *dn = NULL;
38789Sahrens int restarted = B_FALSE;
39789Sahrens
4010298SMatthew.Ahrens@Sun.COM mutex_enter(&os->os_obj_lock);
41789Sahrens for (;;) {
4210298SMatthew.Ahrens@Sun.COM object = os->os_obj_next;
43789Sahrens /*
44789Sahrens * Each time we polish off an L2 bp worth of dnodes
45789Sahrens * (2^13 objects), move to another L2 bp that's still
46789Sahrens * reasonably sparse (at most 1/4 full). Look from the
47789Sahrens * beginning once, but after that keep looking from here.
48789Sahrens * If we can't find one, just keep going from here.
49789Sahrens */
50789Sahrens if (P2PHASE(object, L2_dnode_count) == 0) {
51789Sahrens uint64_t offset = restarted ? object << DNODE_SHIFT : 0;
52*12684STom.Erickson@Sun.COM int error = dnode_next_offset(DMU_META_DNODE(os),
536992Smaybee DNODE_FIND_HOLE,
546992Smaybee &offset, 2, DNODES_PER_BLOCK >> 2, 0);
55789Sahrens restarted = B_TRUE;
56789Sahrens if (error == 0)
57789Sahrens object = offset >> DNODE_SHIFT;
58789Sahrens }
5910298SMatthew.Ahrens@Sun.COM os->os_obj_next = ++object;
60789Sahrens
611544Seschrock /*
621544Seschrock * XXX We should check for an i/o error here and return
631544Seschrock * up to our caller. Actually we should pre-read it in
641544Seschrock * dmu_tx_assign(), but there is currently no mechanism
651544Seschrock * to do so.
661544Seschrock */
6710298SMatthew.Ahrens@Sun.COM (void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
681544Seschrock FTAG, &dn);
69789Sahrens if (dn)
70789Sahrens break;
71789Sahrens
723025Sahrens if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
7310298SMatthew.Ahrens@Sun.COM os->os_obj_next = object - 1;
74789Sahrens }
75789Sahrens
76789Sahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
77789Sahrens dnode_rele(dn, FTAG);
78789Sahrens
7910298SMatthew.Ahrens@Sun.COM mutex_exit(&os->os_obj_lock);
80789Sahrens
81789Sahrens dmu_tx_add_new_object(tx, os, object);
82789Sahrens return (object);
83789Sahrens }
84789Sahrens
85789Sahrens int
dmu_object_claim(objset_t * os,uint64_t object,dmu_object_type_t ot,int blocksize,dmu_object_type_t bonustype,int bonuslen,dmu_tx_t * tx)86789Sahrens dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
87789Sahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
88789Sahrens {
89789Sahrens dnode_t *dn;
901544Seschrock int err;
91789Sahrens
921544Seschrock if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
93789Sahrens return (EBADF);
94789Sahrens
9510298SMatthew.Ahrens@Sun.COM err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
961544Seschrock if (err)
971544Seschrock return (err);
98789Sahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
99789Sahrens dnode_rele(dn, FTAG);
100789Sahrens
101789Sahrens dmu_tx_add_new_object(tx, os, object);
102789Sahrens return (0);
103789Sahrens }
104789Sahrens
105789Sahrens int
dmu_object_reclaim(objset_t * os,uint64_t object,dmu_object_type_t ot,int blocksize,dmu_object_type_t bonustype,int bonuslen)106789Sahrens dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
1078986SMark.Maybee@Sun.COM int blocksize, dmu_object_type_t bonustype, int bonuslen)
108789Sahrens {
109789Sahrens dnode_t *dn;
1108986SMark.Maybee@Sun.COM dmu_tx_t *tx;
1118986SMark.Maybee@Sun.COM int nblkptr;
1121544Seschrock int err;
113789Sahrens
1148986SMark.Maybee@Sun.COM if (object == DMU_META_DNODE_OBJECT)
115789Sahrens return (EBADF);
116789Sahrens
11710298SMatthew.Ahrens@Sun.COM err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
1181544Seschrock FTAG, &dn);
1191544Seschrock if (err)
1201544Seschrock return (err);
1218986SMark.Maybee@Sun.COM
1228986SMark.Maybee@Sun.COM if (dn->dn_type == ot && dn->dn_datablksz == blocksize &&
1238986SMark.Maybee@Sun.COM dn->dn_bonustype == bonustype && dn->dn_bonuslen == bonuslen) {
1248986SMark.Maybee@Sun.COM /* nothing is changing, this is a noop */
1258986SMark.Maybee@Sun.COM dnode_rele(dn, FTAG);
1268986SMark.Maybee@Sun.COM return (0);
1278986SMark.Maybee@Sun.COM }
1288986SMark.Maybee@Sun.COM
12912178SMark.Shellenbaum@Sun.COM if (bonustype == DMU_OT_SA) {
13012178SMark.Shellenbaum@Sun.COM nblkptr = 1;
13112178SMark.Shellenbaum@Sun.COM } else {
13212178SMark.Shellenbaum@Sun.COM nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
13312178SMark.Shellenbaum@Sun.COM }
1348986SMark.Maybee@Sun.COM
1358986SMark.Maybee@Sun.COM /*
1368986SMark.Maybee@Sun.COM * If we are losing blkptrs or changing the block size this must
1378986SMark.Maybee@Sun.COM * be a new file instance. We must clear out the previous file
1388986SMark.Maybee@Sun.COM * contents before we can change this type of metadata in the dnode.
1398986SMark.Maybee@Sun.COM */
1409299SMark.Maybee@Sun.COM if (dn->dn_nblkptr > nblkptr || dn->dn_datablksz != blocksize) {
1419299SMark.Maybee@Sun.COM err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
1429299SMark.Maybee@Sun.COM if (err)
1439299SMark.Maybee@Sun.COM goto out;
1449299SMark.Maybee@Sun.COM }
1459299SMark.Maybee@Sun.COM
1469299SMark.Maybee@Sun.COM tx = dmu_tx_create(os);
1479299SMark.Maybee@Sun.COM dmu_tx_hold_bonus(tx, object);
1489299SMark.Maybee@Sun.COM err = dmu_tx_assign(tx, TXG_WAIT);
1499299SMark.Maybee@Sun.COM if (err) {
1509299SMark.Maybee@Sun.COM dmu_tx_abort(tx);
1519299SMark.Maybee@Sun.COM goto out;
1529299SMark.Maybee@Sun.COM }
1538986SMark.Maybee@Sun.COM
154789Sahrens dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
1558986SMark.Maybee@Sun.COM
1568986SMark.Maybee@Sun.COM dmu_tx_commit(tx);
1579299SMark.Maybee@Sun.COM out:
158789Sahrens dnode_rele(dn, FTAG);
159789Sahrens
1609299SMark.Maybee@Sun.COM return (err);
161789Sahrens }
162789Sahrens
163789Sahrens int
dmu_object_free(objset_t * os,uint64_t object,dmu_tx_t * tx)164789Sahrens dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
165789Sahrens {
166789Sahrens dnode_t *dn;
1671544Seschrock int err;
168789Sahrens
1691544Seschrock ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
170789Sahrens
17110298SMatthew.Ahrens@Sun.COM err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
1721544Seschrock FTAG, &dn);
1731544Seschrock if (err)
1741544Seschrock return (err);
175789Sahrens
176789Sahrens ASSERT(dn->dn_type != DMU_OT_NONE);
1776992Smaybee dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
178789Sahrens dnode_free(dn, tx);
179789Sahrens dnode_rele(dn, FTAG);
180789Sahrens
181789Sahrens return (0);
182789Sahrens }
183789Sahrens
184789Sahrens int
dmu_object_next(objset_t * os,uint64_t * objectp,boolean_t hole,uint64_t txg)1853025Sahrens dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
186789Sahrens {
187789Sahrens uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
188789Sahrens int error;
189789Sahrens
190*12684STom.Erickson@Sun.COM error = dnode_next_offset(DMU_META_DNODE(os),
1916992Smaybee (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
192789Sahrens
193789Sahrens *objectp = offset >> DNODE_SHIFT;
194789Sahrens
195789Sahrens return (error);
196789Sahrens }
197