2008-11-20 23:01:55 +03:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
|
|
* or http://www.opensolaris.org/os/licensing.
|
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
|
|
|
/*
|
2010-05-29 00:45:14 +04:00
|
|
|
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
|
2008-11-20 23:01:55 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/dmu.h>
|
|
|
|
#include <sys/dmu_objset.h>
|
|
|
|
#include <sys/dmu_tx.h>
|
|
|
|
#include <sys/dnode.h>
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
uint64_t object;
|
|
|
|
uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
|
2010-05-29 00:45:14 +04:00
|
|
|
(os->os_meta_dnode->dn_indblkshift - SPA_BLKPTRSHIFT);
|
2008-11-20 23:01:55 +03:00
|
|
|
dnode_t *dn = NULL;
|
|
|
|
int restarted = B_FALSE;
|
|
|
|
|
2010-05-29 00:45:14 +04:00
|
|
|
mutex_enter(&os->os_obj_lock);
|
2008-11-20 23:01:55 +03:00
|
|
|
for (;;) {
|
2010-05-29 00:45:14 +04:00
|
|
|
object = os->os_obj_next;
|
2008-11-20 23:01:55 +03:00
|
|
|
/*
|
|
|
|
* Each time we polish off an L2 bp worth of dnodes
|
|
|
|
* (2^13 objects), move to another L2 bp that's still
|
|
|
|
* reasonably sparse (at most 1/4 full). Look from the
|
|
|
|
* beginning once, but after that keep looking from here.
|
|
|
|
* If we can't find one, just keep going from here.
|
|
|
|
*/
|
|
|
|
if (P2PHASE(object, L2_dnode_count) == 0) {
|
|
|
|
uint64_t offset = restarted ? object << DNODE_SHIFT : 0;
|
2010-05-29 00:45:14 +04:00
|
|
|
int error = dnode_next_offset(os->os_meta_dnode,
|
2008-12-03 23:09:06 +03:00
|
|
|
DNODE_FIND_HOLE,
|
|
|
|
&offset, 2, DNODES_PER_BLOCK >> 2, 0);
|
2008-11-20 23:01:55 +03:00
|
|
|
restarted = B_TRUE;
|
|
|
|
if (error == 0)
|
|
|
|
object = offset >> DNODE_SHIFT;
|
|
|
|
}
|
2010-05-29 00:45:14 +04:00
|
|
|
os->os_obj_next = ++object;
|
2008-11-20 23:01:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX We should check for an i/o error here and return
|
|
|
|
* up to our caller. Actually we should pre-read it in
|
|
|
|
* dmu_tx_assign(), but there is currently no mechanism
|
|
|
|
* to do so.
|
|
|
|
*/
|
2010-05-29 00:45:14 +04:00
|
|
|
(void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
|
2008-11-20 23:01:55 +03:00
|
|
|
FTAG, &dn);
|
|
|
|
if (dn)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
|
2010-05-29 00:45:14 +04:00
|
|
|
os->os_obj_next = object - 1;
|
2008-11-20 23:01:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
|
|
|
|
dnode_rele(dn, FTAG);
|
|
|
|
|
2010-05-29 00:45:14 +04:00
|
|
|
mutex_exit(&os->os_obj_lock);
|
2008-11-20 23:01:55 +03:00
|
|
|
|
|
|
|
dmu_tx_add_new_object(tx, os, object);
|
|
|
|
return (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
dnode_t *dn;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
|
|
|
|
return (EBADF);
|
|
|
|
|
2010-05-29 00:45:14 +04:00
|
|
|
err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
|
2008-11-20 23:01:55 +03:00
|
|
|
if (err)
|
|
|
|
return (err);
|
|
|
|
dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
|
|
|
|
dnode_rele(dn, FTAG);
|
|
|
|
|
|
|
|
dmu_tx_add_new_object(tx, os, object);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
|
2009-07-03 02:44:48 +04:00
|
|
|
int blocksize, dmu_object_type_t bonustype, int bonuslen)
|
2008-11-20 23:01:55 +03:00
|
|
|
{
|
|
|
|
dnode_t *dn;
|
2009-07-03 02:44:48 +04:00
|
|
|
dmu_tx_t *tx;
|
|
|
|
int nblkptr;
|
2008-11-20 23:01:55 +03:00
|
|
|
int err;
|
|
|
|
|
2009-07-03 02:44:48 +04:00
|
|
|
if (object == DMU_META_DNODE_OBJECT)
|
2008-11-20 23:01:55 +03:00
|
|
|
return (EBADF);
|
|
|
|
|
2010-05-29 00:45:14 +04:00
|
|
|
err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
|
2008-11-20 23:01:55 +03:00
|
|
|
FTAG, &dn);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
2009-07-03 02:44:48 +04:00
|
|
|
|
|
|
|
if (dn->dn_type == ot && dn->dn_datablksz == blocksize &&
|
|
|
|
dn->dn_bonustype == bonustype && dn->dn_bonuslen == bonuslen) {
|
|
|
|
/* nothing is changing, this is a noop */
|
|
|
|
dnode_rele(dn, FTAG);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-05-29 00:45:14 +04:00
|
|
|
if (bonustype == DMU_OT_SA) {
|
|
|
|
nblkptr = 1;
|
|
|
|
} else {
|
|
|
|
nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
|
|
|
|
}
|
2009-07-03 02:44:48 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are losing blkptrs or changing the block size this must
|
|
|
|
* be a new file instance. We must clear out the previous file
|
|
|
|
* contents before we can change this type of metadata in the dnode.
|
|
|
|
*/
|
|
|
|
if (dn->dn_nblkptr > nblkptr || dn->dn_datablksz != blocksize) {
|
|
|
|
err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
tx = dmu_tx_create(os);
|
|
|
|
dmu_tx_hold_bonus(tx, object);
|
|
|
|
err = dmu_tx_assign(tx, TXG_WAIT);
|
|
|
|
if (err) {
|
|
|
|
dmu_tx_abort(tx);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-11-20 23:01:55 +03:00
|
|
|
dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
|
2009-07-03 02:44:48 +04:00
|
|
|
|
|
|
|
dmu_tx_commit(tx);
|
|
|
|
out:
|
2008-11-20 23:01:55 +03:00
|
|
|
dnode_rele(dn, FTAG);
|
|
|
|
|
2009-07-03 02:44:48 +04:00
|
|
|
return (err);
|
2008-11-20 23:01:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
|
|
|
|
{
|
|
|
|
dnode_t *dn;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
|
|
|
|
|
2010-05-29 00:45:14 +04:00
|
|
|
err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
|
2008-11-20 23:01:55 +03:00
|
|
|
FTAG, &dn);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
|
|
|
|
|
|
|
ASSERT(dn->dn_type != DMU_OT_NONE);
|
2008-12-03 23:09:06 +03:00
|
|
|
dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
|
2008-11-20 23:01:55 +03:00
|
|
|
dnode_free(dn, tx);
|
|
|
|
dnode_rele(dn, FTAG);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
|
|
|
|
{
|
|
|
|
uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
|
|
|
|
int error;
|
|
|
|
|
2010-05-29 00:45:14 +04:00
|
|
|
error = dnode_next_offset(os->os_meta_dnode,
|
2008-12-03 23:09:06 +03:00
|
|
|
(hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
|
2008-11-20 23:01:55 +03:00
|
|
|
|
|
|
|
*objectp = offset >> DNODE_SHIFT;
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|