6801979 zfs recv can fail with E2BIG
[opensolaris.git] / usr / src / uts / common / fs / zfs / dmu_object.c
blob25dfafd4f2c1bbb869763509a2df92a97d92145c
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/dmu.h>
27 #include <sys/dmu_objset.h>
28 #include <sys/dmu_tx.h>
29 #include <sys/dnode.h>
31 uint64_t
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)
35 objset_impl_t *osi = os->os;
36 uint64_t object;
37 uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
38 (osi->os_meta_dnode->dn_indblkshift - SPA_BLKPTRSHIFT);
39 dnode_t *dn = NULL;
40 int restarted = B_FALSE;
42 mutex_enter(&osi->os_obj_lock);
43 for (;;) {
44 object = osi->os_obj_next;
46 * Each time we polish off an L2 bp worth of dnodes
47 * (2^13 objects), move to another L2 bp that's still
48 * reasonably sparse (at most 1/4 full). Look from the
49 * beginning once, but after that keep looking from here.
50 * If we can't find one, just keep going from here.
52 if (P2PHASE(object, L2_dnode_count) == 0) {
53 uint64_t offset = restarted ? object << DNODE_SHIFT : 0;
54 int error = dnode_next_offset(osi->os_meta_dnode,
55 DNODE_FIND_HOLE,
56 &offset, 2, DNODES_PER_BLOCK >> 2, 0);
57 restarted = B_TRUE;
58 if (error == 0)
59 object = offset >> DNODE_SHIFT;
61 osi->os_obj_next = ++object;
64 * XXX We should check for an i/o error here and return
65 * up to our caller. Actually we should pre-read it in
66 * dmu_tx_assign(), but there is currently no mechanism
67 * to do so.
69 (void) dnode_hold_impl(os->os, object, DNODE_MUST_BE_FREE,
70 FTAG, &dn);
71 if (dn)
72 break;
74 if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
75 osi->os_obj_next = object - 1;
78 dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
79 dnode_rele(dn, FTAG);
81 mutex_exit(&osi->os_obj_lock);
83 dmu_tx_add_new_object(tx, os, object);
84 return (object);
87 int
88 dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
89 int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
91 dnode_t *dn;
92 int err;
94 if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
95 return (EBADF);
97 err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
98 if (err)
99 return (err);
100 dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
101 dnode_rele(dn, FTAG);
103 dmu_tx_add_new_object(tx, os, object);
104 return (0);
108 dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
109 int blocksize, dmu_object_type_t bonustype, int bonuslen)
111 dnode_t *dn;
112 dmu_tx_t *tx;
113 int nblkptr;
114 int err;
116 if (object == DMU_META_DNODE_OBJECT)
117 return (EBADF);
119 err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
120 FTAG, &dn);
121 if (err)
122 return (err);
124 if (dn->dn_type == ot && dn->dn_datablksz == blocksize &&
125 dn->dn_bonustype == bonustype && dn->dn_bonuslen == bonuslen) {
126 /* nothing is changing, this is a noop */
127 dnode_rele(dn, FTAG);
128 return (0);
131 tx = dmu_tx_create(os);
132 dmu_tx_hold_bonus(tx, object);
133 err = dmu_tx_assign(tx, TXG_WAIT);
134 if (err) {
135 dmu_tx_abort(tx);
136 dnode_rele(dn, FTAG);
137 return (err);
140 nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
143 * If we are losing blkptrs or changing the block size this must
144 * be a new file instance. We must clear out the previous file
145 * contents before we can change this type of metadata in the dnode.
147 if (dn->dn_nblkptr > nblkptr || dn->dn_datablksz != blocksize)
148 dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
150 dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
152 dmu_tx_commit(tx);
154 dnode_rele(dn, FTAG);
156 return (0);
160 dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
162 dnode_t *dn;
163 int err;
165 ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
167 err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
168 FTAG, &dn);
169 if (err)
170 return (err);
172 ASSERT(dn->dn_type != DMU_OT_NONE);
173 dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
174 dnode_free(dn, tx);
175 dnode_rele(dn, FTAG);
177 return (0);
181 dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
183 uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
184 int error;
186 error = dnode_next_offset(os->os->os_meta_dnode,
187 (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
189 *objectp = offset >> DNODE_SHIFT;
191 return (error);