]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
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. | |
7 | * | |
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. | |
12 | * | |
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] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
428870ff | 22 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
34dc7c2f | 23 | */ |
22cd4a46 AL |
24 | /* |
25 | * Copyright 2011 Nexenta Systems, Inc. All rights reserved. | |
26 | */ | |
34dc7c2f | 27 | |
34dc7c2f BB |
28 | #include <sys/dmu.h> |
29 | #include <sys/dmu_impl.h> | |
30 | #include <sys/dbuf.h> | |
31 | #include <sys/dmu_tx.h> | |
32 | #include <sys/dmu_objset.h> | |
33 | #include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */ | |
34 | #include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */ | |
35 | #include <sys/dsl_pool.h> | |
36 | #include <sys/zap_impl.h> /* for fzap_default_block_shift */ | |
37 | #include <sys/spa.h> | |
428870ff BB |
38 | #include <sys/sa.h> |
39 | #include <sys/sa_impl.h> | |
34dc7c2f | 40 | #include <sys/zfs_context.h> |
428870ff | 41 | #include <sys/varargs.h> |
34dc7c2f BB |
42 | |
43 | typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn, | |
44 | uint64_t arg1, uint64_t arg2); | |
45 | ||
570827e1 BB |
46 | dmu_tx_stats_t dmu_tx_stats = { |
47 | { "dmu_tx_assigned", KSTAT_DATA_UINT64 }, | |
48 | { "dmu_tx_delay", KSTAT_DATA_UINT64 }, | |
49 | { "dmu_tx_error", KSTAT_DATA_UINT64 }, | |
50 | { "dmu_tx_suspended", KSTAT_DATA_UINT64 }, | |
51 | { "dmu_tx_group", KSTAT_DATA_UINT64 }, | |
52 | { "dmu_tx_how", KSTAT_DATA_UINT64 }, | |
53 | { "dmu_tx_memory_reserve", KSTAT_DATA_UINT64 }, | |
54 | { "dmu_tx_memory_reclaim", KSTAT_DATA_UINT64 }, | |
55 | { "dmu_tx_memory_inflight", KSTAT_DATA_UINT64 }, | |
56 | { "dmu_tx_dirty_throttle", KSTAT_DATA_UINT64 }, | |
57 | { "dmu_tx_write_limit", KSTAT_DATA_UINT64 }, | |
58 | { "dmu_tx_quota", KSTAT_DATA_UINT64 }, | |
59 | }; | |
60 | ||
61 | static kstat_t *dmu_tx_ksp; | |
34dc7c2f BB |
62 | |
63 | dmu_tx_t * | |
64 | dmu_tx_create_dd(dsl_dir_t *dd) | |
65 | { | |
b8d06fca | 66 | dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_PUSHPAGE); |
34dc7c2f BB |
67 | tx->tx_dir = dd; |
68 | if (dd) | |
69 | tx->tx_pool = dd->dd_pool; | |
70 | list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t), | |
71 | offsetof(dmu_tx_hold_t, txh_node)); | |
428870ff BB |
72 | list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t), |
73 | offsetof(dmu_tx_callback_t, dcb_node)); | |
1c5de20a | 74 | #ifdef DEBUG_DMU_TX |
34dc7c2f BB |
75 | refcount_create(&tx->tx_space_written); |
76 | refcount_create(&tx->tx_space_freed); | |
77 | #endif | |
78 | return (tx); | |
79 | } | |
80 | ||
81 | dmu_tx_t * | |
82 | dmu_tx_create(objset_t *os) | |
83 | { | |
428870ff | 84 | dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir); |
34dc7c2f | 85 | tx->tx_objset = os; |
428870ff | 86 | tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset); |
34dc7c2f BB |
87 | return (tx); |
88 | } | |
89 | ||
90 | dmu_tx_t * | |
91 | dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg) | |
92 | { | |
93 | dmu_tx_t *tx = dmu_tx_create_dd(NULL); | |
94 | ||
95 | ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg); | |
96 | tx->tx_pool = dp; | |
97 | tx->tx_txg = txg; | |
98 | tx->tx_anyobj = TRUE; | |
99 | ||
100 | return (tx); | |
101 | } | |
102 | ||
103 | int | |
104 | dmu_tx_is_syncing(dmu_tx_t *tx) | |
105 | { | |
106 | return (tx->tx_anyobj); | |
107 | } | |
108 | ||
109 | int | |
110 | dmu_tx_private_ok(dmu_tx_t *tx) | |
111 | { | |
112 | return (tx->tx_anyobj); | |
113 | } | |
114 | ||
115 | static dmu_tx_hold_t * | |
116 | dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object, | |
117 | enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2) | |
118 | { | |
119 | dmu_tx_hold_t *txh; | |
120 | dnode_t *dn = NULL; | |
121 | int err; | |
122 | ||
123 | if (object != DMU_NEW_OBJECT) { | |
428870ff | 124 | err = dnode_hold(os, object, tx, &dn); |
34dc7c2f BB |
125 | if (err) { |
126 | tx->tx_err = err; | |
127 | return (NULL); | |
128 | } | |
129 | ||
130 | if (err == 0 && tx->tx_txg != 0) { | |
131 | mutex_enter(&dn->dn_mtx); | |
132 | /* | |
133 | * dn->dn_assigned_txg == tx->tx_txg doesn't pose a | |
134 | * problem, but there's no way for it to happen (for | |
135 | * now, at least). | |
136 | */ | |
137 | ASSERT(dn->dn_assigned_txg == 0); | |
138 | dn->dn_assigned_txg = tx->tx_txg; | |
139 | (void) refcount_add(&dn->dn_tx_holds, tx); | |
140 | mutex_exit(&dn->dn_mtx); | |
141 | } | |
142 | } | |
143 | ||
b8d06fca | 144 | txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_PUSHPAGE); |
34dc7c2f BB |
145 | txh->txh_tx = tx; |
146 | txh->txh_dnode = dn; | |
1c5de20a | 147 | #ifdef DEBUG_DMU_TX |
34dc7c2f BB |
148 | txh->txh_type = type; |
149 | txh->txh_arg1 = arg1; | |
150 | txh->txh_arg2 = arg2; | |
151 | #endif | |
152 | list_insert_tail(&tx->tx_holds, txh); | |
153 | ||
154 | return (txh); | |
155 | } | |
156 | ||
157 | void | |
158 | dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object) | |
159 | { | |
160 | /* | |
161 | * If we're syncing, they can manipulate any object anyhow, and | |
162 | * the hold on the dnode_t can cause problems. | |
163 | */ | |
164 | if (!dmu_tx_is_syncing(tx)) { | |
165 | (void) dmu_tx_hold_object_impl(tx, os, | |
166 | object, THT_NEWOBJECT, 0, 0); | |
167 | } | |
168 | } | |
169 | ||
170 | static int | |
171 | dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid) | |
172 | { | |
173 | int err; | |
174 | dmu_buf_impl_t *db; | |
175 | ||
176 | rw_enter(&dn->dn_struct_rwlock, RW_READER); | |
177 | db = dbuf_hold_level(dn, level, blkid, FTAG); | |
178 | rw_exit(&dn->dn_struct_rwlock); | |
179 | if (db == NULL) | |
180 | return (EIO); | |
181 | err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH); | |
182 | dbuf_rele(db, FTAG); | |
183 | return (err); | |
184 | } | |
185 | ||
9babb374 | 186 | static void |
428870ff BB |
187 | dmu_tx_count_twig(dmu_tx_hold_t *txh, dnode_t *dn, dmu_buf_impl_t *db, |
188 | int level, uint64_t blkid, boolean_t freeable, uint64_t *history) | |
9babb374 | 189 | { |
428870ff BB |
190 | objset_t *os = dn->dn_objset; |
191 | dsl_dataset_t *ds = os->os_dsl_dataset; | |
192 | int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; | |
193 | dmu_buf_impl_t *parent = NULL; | |
194 | blkptr_t *bp = NULL; | |
195 | uint64_t space; | |
196 | ||
197 | if (level >= dn->dn_nlevels || history[level] == blkid) | |
9babb374 BB |
198 | return; |
199 | ||
428870ff | 200 | history[level] = blkid; |
9babb374 | 201 | |
428870ff BB |
202 | space = (level == 0) ? dn->dn_datablksz : (1ULL << dn->dn_indblkshift); |
203 | ||
204 | if (db == NULL || db == dn->dn_dbuf) { | |
205 | ASSERT(level != 0); | |
206 | db = NULL; | |
207 | } else { | |
572e2857 | 208 | ASSERT(DB_DNODE(db) == dn); |
428870ff BB |
209 | ASSERT(db->db_level == level); |
210 | ASSERT(db->db.db_size == space); | |
211 | ASSERT(db->db_blkid == blkid); | |
212 | bp = db->db_blkptr; | |
213 | parent = db->db_parent; | |
9babb374 BB |
214 | } |
215 | ||
428870ff BB |
216 | freeable = (bp && (freeable || |
217 | dsl_dataset_block_freeable(ds, bp, bp->blk_birth))); | |
9babb374 | 218 | |
428870ff BB |
219 | if (freeable) |
220 | txh->txh_space_tooverwrite += space; | |
221 | else | |
222 | txh->txh_space_towrite += space; | |
223 | if (bp) | |
224 | txh->txh_space_tounref += bp_get_dsize(os->os_spa, bp); | |
225 | ||
226 | dmu_tx_count_twig(txh, dn, parent, level + 1, | |
227 | blkid >> epbs, freeable, history); | |
9babb374 BB |
228 | } |
229 | ||
34dc7c2f BB |
230 | /* ARGSUSED */ |
231 | static void | |
232 | dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len) | |
233 | { | |
234 | dnode_t *dn = txh->txh_dnode; | |
235 | uint64_t start, end, i; | |
236 | int min_bs, max_bs, min_ibs, max_ibs, epbs, bits; | |
237 | int err = 0; | |
d6320ddb | 238 | int l; |
34dc7c2f BB |
239 | |
240 | if (len == 0) | |
241 | return; | |
242 | ||
243 | min_bs = SPA_MINBLOCKSHIFT; | |
244 | max_bs = SPA_MAXBLOCKSHIFT; | |
245 | min_ibs = DN_MIN_INDBLKSHIFT; | |
246 | max_ibs = DN_MAX_INDBLKSHIFT; | |
247 | ||
34dc7c2f | 248 | if (dn) { |
428870ff | 249 | uint64_t history[DN_MAX_LEVELS]; |
9babb374 BB |
250 | int nlvls = dn->dn_nlevels; |
251 | int delta; | |
252 | ||
253 | /* | |
254 | * For i/o error checking, read the first and last level-0 | |
255 | * blocks (if they are not aligned), and all the level-1 blocks. | |
256 | */ | |
34dc7c2f | 257 | if (dn->dn_maxblkid == 0) { |
9babb374 BB |
258 | delta = dn->dn_datablksz; |
259 | start = (off < dn->dn_datablksz) ? 0 : 1; | |
260 | end = (off+len <= dn->dn_datablksz) ? 0 : 1; | |
261 | if (start == 0 && (off > 0 || len < dn->dn_datablksz)) { | |
b128c09f BB |
262 | err = dmu_tx_check_ioerr(NULL, dn, 0, 0); |
263 | if (err) | |
264 | goto out; | |
9babb374 | 265 | delta -= off; |
b128c09f | 266 | } |
34dc7c2f BB |
267 | } else { |
268 | zio_t *zio = zio_root(dn->dn_objset->os_spa, | |
269 | NULL, NULL, ZIO_FLAG_CANFAIL); | |
270 | ||
271 | /* first level-0 block */ | |
272 | start = off >> dn->dn_datablkshift; | |
273 | if (P2PHASE(off, dn->dn_datablksz) || | |
274 | len < dn->dn_datablksz) { | |
275 | err = dmu_tx_check_ioerr(zio, dn, 0, start); | |
276 | if (err) | |
277 | goto out; | |
278 | } | |
279 | ||
280 | /* last level-0 block */ | |
281 | end = (off+len-1) >> dn->dn_datablkshift; | |
b128c09f | 282 | if (end != start && end <= dn->dn_maxblkid && |
34dc7c2f BB |
283 | P2PHASE(off+len, dn->dn_datablksz)) { |
284 | err = dmu_tx_check_ioerr(zio, dn, 0, end); | |
285 | if (err) | |
286 | goto out; | |
287 | } | |
288 | ||
289 | /* level-1 blocks */ | |
9babb374 BB |
290 | if (nlvls > 1) { |
291 | int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT; | |
292 | for (i = (start>>shft)+1; i < end>>shft; i++) { | |
34dc7c2f BB |
293 | err = dmu_tx_check_ioerr(zio, dn, 1, i); |
294 | if (err) | |
295 | goto out; | |
296 | } | |
297 | } | |
298 | ||
299 | err = zio_wait(zio); | |
300 | if (err) | |
301 | goto out; | |
9babb374 | 302 | delta = P2NPHASE(off, dn->dn_datablksz); |
34dc7c2f | 303 | } |
34dc7c2f | 304 | |
9babb374 BB |
305 | if (dn->dn_maxblkid > 0) { |
306 | /* | |
307 | * The blocksize can't change, | |
308 | * so we can make a more precise estimate. | |
309 | */ | |
310 | ASSERT(dn->dn_datablkshift != 0); | |
34dc7c2f | 311 | min_bs = max_bs = dn->dn_datablkshift; |
9babb374 BB |
312 | min_ibs = max_ibs = dn->dn_indblkshift; |
313 | } else if (dn->dn_indblkshift > max_ibs) { | |
314 | /* | |
315 | * This ensures that if we reduce DN_MAX_INDBLKSHIFT, | |
316 | * the code will still work correctly on older pools. | |
317 | */ | |
318 | min_ibs = max_ibs = dn->dn_indblkshift; | |
319 | } | |
320 | ||
321 | /* | |
322 | * If this write is not off the end of the file | |
323 | * we need to account for overwrites/unref. | |
324 | */ | |
428870ff | 325 | if (start <= dn->dn_maxblkid) { |
d6320ddb | 326 | for (l = 0; l < DN_MAX_LEVELS; l++) |
428870ff BB |
327 | history[l] = -1ULL; |
328 | } | |
9babb374 | 329 | while (start <= dn->dn_maxblkid) { |
9babb374 BB |
330 | dmu_buf_impl_t *db; |
331 | ||
332 | rw_enter(&dn->dn_struct_rwlock, RW_READER); | |
428870ff | 333 | err = dbuf_hold_impl(dn, 0, start, FALSE, FTAG, &db); |
9babb374 | 334 | rw_exit(&dn->dn_struct_rwlock); |
428870ff BB |
335 | |
336 | if (err) { | |
337 | txh->txh_tx->tx_err = err; | |
338 | return; | |
9babb374 | 339 | } |
428870ff BB |
340 | |
341 | dmu_tx_count_twig(txh, dn, db, 0, start, B_FALSE, | |
342 | history); | |
9babb374 BB |
343 | dbuf_rele(db, FTAG); |
344 | if (++start > end) { | |
345 | /* | |
346 | * Account for new indirects appearing | |
347 | * before this IO gets assigned into a txg. | |
348 | */ | |
349 | bits = 64 - min_bs; | |
350 | epbs = min_ibs - SPA_BLKPTRSHIFT; | |
351 | for (bits -= epbs * (nlvls - 1); | |
352 | bits >= 0; bits -= epbs) | |
353 | txh->txh_fudge += 1ULL << max_ibs; | |
354 | goto out; | |
355 | } | |
356 | off += delta; | |
357 | if (len >= delta) | |
358 | len -= delta; | |
359 | delta = dn->dn_datablksz; | |
360 | } | |
34dc7c2f BB |
361 | } |
362 | ||
363 | /* | |
364 | * 'end' is the last thing we will access, not one past. | |
365 | * This way we won't overflow when accessing the last byte. | |
366 | */ | |
367 | start = P2ALIGN(off, 1ULL << max_bs); | |
368 | end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1; | |
369 | txh->txh_space_towrite += end - start + 1; | |
370 | ||
371 | start >>= min_bs; | |
372 | end >>= min_bs; | |
373 | ||
374 | epbs = min_ibs - SPA_BLKPTRSHIFT; | |
375 | ||
376 | /* | |
377 | * The object contains at most 2^(64 - min_bs) blocks, | |
378 | * and each indirect level maps 2^epbs. | |
379 | */ | |
380 | for (bits = 64 - min_bs; bits >= 0; bits -= epbs) { | |
381 | start >>= epbs; | |
382 | end >>= epbs; | |
9babb374 | 383 | ASSERT3U(end, >=, start); |
34dc7c2f | 384 | txh->txh_space_towrite += (end - start + 1) << max_ibs; |
9babb374 BB |
385 | if (start != 0) { |
386 | /* | |
387 | * We also need a new blkid=0 indirect block | |
388 | * to reference any existing file data. | |
389 | */ | |
390 | txh->txh_space_towrite += 1ULL << max_ibs; | |
391 | } | |
34dc7c2f BB |
392 | } |
393 | ||
34dc7c2f | 394 | out: |
9babb374 BB |
395 | if (txh->txh_space_towrite + txh->txh_space_tooverwrite > |
396 | 2 * DMU_MAX_ACCESS) | |
397 | err = EFBIG; | |
398 | ||
34dc7c2f BB |
399 | if (err) |
400 | txh->txh_tx->tx_err = err; | |
401 | } | |
402 | ||
403 | static void | |
404 | dmu_tx_count_dnode(dmu_tx_hold_t *txh) | |
405 | { | |
406 | dnode_t *dn = txh->txh_dnode; | |
572e2857 | 407 | dnode_t *mdn = DMU_META_DNODE(txh->txh_tx->tx_objset); |
34dc7c2f BB |
408 | uint64_t space = mdn->dn_datablksz + |
409 | ((mdn->dn_nlevels-1) << mdn->dn_indblkshift); | |
410 | ||
411 | if (dn && dn->dn_dbuf->db_blkptr && | |
412 | dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, | |
428870ff | 413 | dn->dn_dbuf->db_blkptr, dn->dn_dbuf->db_blkptr->blk_birth)) { |
34dc7c2f | 414 | txh->txh_space_tooverwrite += space; |
9babb374 | 415 | txh->txh_space_tounref += space; |
34dc7c2f BB |
416 | } else { |
417 | txh->txh_space_towrite += space; | |
418 | if (dn && dn->dn_dbuf->db_blkptr) | |
419 | txh->txh_space_tounref += space; | |
420 | } | |
421 | } | |
422 | ||
423 | void | |
424 | dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len) | |
425 | { | |
426 | dmu_tx_hold_t *txh; | |
427 | ||
428 | ASSERT(tx->tx_txg == 0); | |
429 | ASSERT(len < DMU_MAX_ACCESS); | |
430 | ASSERT(len == 0 || UINT64_MAX - off >= len - 1); | |
431 | ||
432 | txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, | |
433 | object, THT_WRITE, off, len); | |
434 | if (txh == NULL) | |
435 | return; | |
436 | ||
437 | dmu_tx_count_write(txh, off, len); | |
438 | dmu_tx_count_dnode(txh); | |
439 | } | |
440 | ||
441 | static void | |
442 | dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len) | |
443 | { | |
b128c09f BB |
444 | uint64_t blkid, nblks, lastblk; |
445 | uint64_t space = 0, unref = 0, skipped = 0; | |
34dc7c2f BB |
446 | dnode_t *dn = txh->txh_dnode; |
447 | dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; | |
448 | spa_t *spa = txh->txh_tx->tx_pool->dp_spa; | |
b128c09f | 449 | int epbs; |
34dc7c2f | 450 | |
b128c09f | 451 | if (dn->dn_nlevels == 0) |
34dc7c2f BB |
452 | return; |
453 | ||
454 | /* | |
b128c09f | 455 | * The struct_rwlock protects us against dn_nlevels |
34dc7c2f BB |
456 | * changing, in case (against all odds) we manage to dirty & |
457 | * sync out the changes after we check for being dirty. | |
428870ff | 458 | * Also, dbuf_hold_impl() wants us to have the struct_rwlock. |
34dc7c2f BB |
459 | */ |
460 | rw_enter(&dn->dn_struct_rwlock, RW_READER); | |
b128c09f BB |
461 | epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; |
462 | if (dn->dn_maxblkid == 0) { | |
34dc7c2f BB |
463 | if (off == 0 && len >= dn->dn_datablksz) { |
464 | blkid = 0; | |
465 | nblks = 1; | |
466 | } else { | |
467 | rw_exit(&dn->dn_struct_rwlock); | |
468 | return; | |
469 | } | |
470 | } else { | |
471 | blkid = off >> dn->dn_datablkshift; | |
b128c09f | 472 | nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift; |
34dc7c2f | 473 | |
b128c09f | 474 | if (blkid >= dn->dn_maxblkid) { |
34dc7c2f BB |
475 | rw_exit(&dn->dn_struct_rwlock); |
476 | return; | |
477 | } | |
b128c09f BB |
478 | if (blkid + nblks > dn->dn_maxblkid) |
479 | nblks = dn->dn_maxblkid - blkid; | |
34dc7c2f | 480 | |
34dc7c2f | 481 | } |
b128c09f | 482 | if (dn->dn_nlevels == 1) { |
34dc7c2f BB |
483 | int i; |
484 | for (i = 0; i < nblks; i++) { | |
485 | blkptr_t *bp = dn->dn_phys->dn_blkptr; | |
b128c09f | 486 | ASSERT3U(blkid + i, <, dn->dn_nblkptr); |
34dc7c2f | 487 | bp += blkid + i; |
428870ff | 488 | if (dsl_dataset_block_freeable(ds, bp, bp->blk_birth)) { |
34dc7c2f | 489 | dprintf_bp(bp, "can free old%s", ""); |
428870ff | 490 | space += bp_get_dsize(spa, bp); |
34dc7c2f BB |
491 | } |
492 | unref += BP_GET_ASIZE(bp); | |
493 | } | |
494 | nblks = 0; | |
495 | } | |
496 | ||
b128c09f BB |
497 | /* |
498 | * Add in memory requirements of higher-level indirects. | |
499 | * This assumes a worst-possible scenario for dn_nlevels. | |
500 | */ | |
501 | { | |
502 | uint64_t blkcnt = 1 + ((nblks >> epbs) >> epbs); | |
503 | int level = (dn->dn_nlevels > 1) ? 2 : 1; | |
504 | ||
505 | while (level++ < DN_MAX_LEVELS) { | |
506 | txh->txh_memory_tohold += blkcnt << dn->dn_indblkshift; | |
507 | blkcnt = 1 + (blkcnt >> epbs); | |
508 | } | |
509 | ASSERT(blkcnt <= dn->dn_nblkptr); | |
510 | } | |
511 | ||
512 | lastblk = blkid + nblks - 1; | |
34dc7c2f BB |
513 | while (nblks) { |
514 | dmu_buf_impl_t *dbuf; | |
b128c09f BB |
515 | uint64_t ibyte, new_blkid; |
516 | int epb = 1 << epbs; | |
517 | int err, i, blkoff, tochk; | |
518 | blkptr_t *bp; | |
519 | ||
520 | ibyte = blkid << dn->dn_datablkshift; | |
521 | err = dnode_next_offset(dn, | |
522 | DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0); | |
523 | new_blkid = ibyte >> dn->dn_datablkshift; | |
524 | if (err == ESRCH) { | |
525 | skipped += (lastblk >> epbs) - (blkid >> epbs) + 1; | |
526 | break; | |
527 | } | |
528 | if (err) { | |
529 | txh->txh_tx->tx_err = err; | |
530 | break; | |
531 | } | |
532 | if (new_blkid > lastblk) { | |
533 | skipped += (lastblk >> epbs) - (blkid >> epbs) + 1; | |
534 | break; | |
535 | } | |
34dc7c2f | 536 | |
b128c09f BB |
537 | if (new_blkid > blkid) { |
538 | ASSERT((new_blkid >> epbs) > (blkid >> epbs)); | |
539 | skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1; | |
540 | nblks -= new_blkid - blkid; | |
541 | blkid = new_blkid; | |
542 | } | |
543 | blkoff = P2PHASE(blkid, epb); | |
544 | tochk = MIN(epb - blkoff, nblks); | |
34dc7c2f | 545 | |
428870ff BB |
546 | err = dbuf_hold_impl(dn, 1, blkid >> epbs, FALSE, FTAG, &dbuf); |
547 | if (err) { | |
548 | txh->txh_tx->tx_err = err; | |
b128c09f | 549 | break; |
34dc7c2f | 550 | } |
428870ff BB |
551 | |
552 | txh->txh_memory_tohold += dbuf->db.db_size; | |
553 | ||
554 | /* | |
555 | * We don't check memory_tohold against DMU_MAX_ACCESS because | |
556 | * memory_tohold is an over-estimation (especially the >L1 | |
557 | * indirect blocks), so it could fail. Callers should have | |
558 | * already verified that they will not be holding too much | |
559 | * memory. | |
560 | */ | |
561 | ||
b128c09f BB |
562 | err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL); |
563 | if (err != 0) { | |
34dc7c2f | 564 | txh->txh_tx->tx_err = err; |
b128c09f | 565 | dbuf_rele(dbuf, FTAG); |
34dc7c2f BB |
566 | break; |
567 | } | |
568 | ||
b128c09f BB |
569 | bp = dbuf->db.db_data; |
570 | bp += blkoff; | |
571 | ||
572 | for (i = 0; i < tochk; i++) { | |
428870ff BB |
573 | if (dsl_dataset_block_freeable(ds, &bp[i], |
574 | bp[i].blk_birth)) { | |
b128c09f | 575 | dprintf_bp(&bp[i], "can free old%s", ""); |
428870ff | 576 | space += bp_get_dsize(spa, &bp[i]); |
b128c09f BB |
577 | } |
578 | unref += BP_GET_ASIZE(bp); | |
579 | } | |
580 | dbuf_rele(dbuf, FTAG); | |
581 | ||
34dc7c2f BB |
582 | blkid += tochk; |
583 | nblks -= tochk; | |
584 | } | |
585 | rw_exit(&dn->dn_struct_rwlock); | |
586 | ||
b128c09f BB |
587 | /* account for new level 1 indirect blocks that might show up */ |
588 | if (skipped > 0) { | |
589 | txh->txh_fudge += skipped << dn->dn_indblkshift; | |
590 | skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs); | |
591 | txh->txh_memory_tohold += skipped << dn->dn_indblkshift; | |
592 | } | |
34dc7c2f BB |
593 | txh->txh_space_tofree += space; |
594 | txh->txh_space_tounref += unref; | |
595 | } | |
596 | ||
597 | void | |
598 | dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len) | |
599 | { | |
600 | dmu_tx_hold_t *txh; | |
601 | dnode_t *dn; | |
602 | uint64_t start, end, i; | |
603 | int err, shift; | |
604 | zio_t *zio; | |
605 | ||
606 | ASSERT(tx->tx_txg == 0); | |
607 | ||
608 | txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, | |
609 | object, THT_FREE, off, len); | |
610 | if (txh == NULL) | |
611 | return; | |
612 | dn = txh->txh_dnode; | |
613 | ||
614 | /* first block */ | |
615 | if (off != 0) | |
616 | dmu_tx_count_write(txh, off, 1); | |
617 | /* last block */ | |
618 | if (len != DMU_OBJECT_END) | |
619 | dmu_tx_count_write(txh, off+len, 1); | |
620 | ||
428870ff BB |
621 | dmu_tx_count_dnode(txh); |
622 | ||
34dc7c2f BB |
623 | if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz) |
624 | return; | |
625 | if (len == DMU_OBJECT_END) | |
626 | len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off; | |
627 | ||
628 | /* | |
629 | * For i/o error checking, read the first and last level-0 | |
630 | * blocks, and all the level-1 blocks. The above count_write's | |
b128c09f | 631 | * have already taken care of the level-0 blocks. |
34dc7c2f BB |
632 | */ |
633 | if (dn->dn_nlevels > 1) { | |
634 | shift = dn->dn_datablkshift + dn->dn_indblkshift - | |
635 | SPA_BLKPTRSHIFT; | |
636 | start = off >> shift; | |
637 | end = dn->dn_datablkshift ? ((off+len) >> shift) : 0; | |
638 | ||
639 | zio = zio_root(tx->tx_pool->dp_spa, | |
640 | NULL, NULL, ZIO_FLAG_CANFAIL); | |
641 | for (i = start; i <= end; i++) { | |
642 | uint64_t ibyte = i << shift; | |
b128c09f | 643 | err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0); |
34dc7c2f BB |
644 | i = ibyte >> shift; |
645 | if (err == ESRCH) | |
646 | break; | |
647 | if (err) { | |
648 | tx->tx_err = err; | |
649 | return; | |
650 | } | |
651 | ||
652 | err = dmu_tx_check_ioerr(zio, dn, 1, i); | |
653 | if (err) { | |
654 | tx->tx_err = err; | |
655 | return; | |
656 | } | |
657 | } | |
658 | err = zio_wait(zio); | |
659 | if (err) { | |
660 | tx->tx_err = err; | |
661 | return; | |
662 | } | |
663 | } | |
664 | ||
34dc7c2f BB |
665 | dmu_tx_count_free(txh, off, len); |
666 | } | |
667 | ||
668 | void | |
9babb374 | 669 | dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name) |
34dc7c2f BB |
670 | { |
671 | dmu_tx_hold_t *txh; | |
672 | dnode_t *dn; | |
673 | uint64_t nblocks; | |
674 | int epbs, err; | |
675 | ||
676 | ASSERT(tx->tx_txg == 0); | |
677 | ||
678 | txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, | |
679 | object, THT_ZAP, add, (uintptr_t)name); | |
680 | if (txh == NULL) | |
681 | return; | |
682 | dn = txh->txh_dnode; | |
683 | ||
684 | dmu_tx_count_dnode(txh); | |
685 | ||
686 | if (dn == NULL) { | |
687 | /* | |
688 | * We will be able to fit a new object's entries into one leaf | |
689 | * block. So there will be at most 2 blocks total, | |
690 | * including the header block. | |
691 | */ | |
692 | dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift); | |
693 | return; | |
694 | } | |
695 | ||
696 | ASSERT3P(dmu_ot[dn->dn_type].ot_byteswap, ==, zap_byteswap); | |
697 | ||
698 | if (dn->dn_maxblkid == 0 && !add) { | |
22cd4a46 AL |
699 | blkptr_t *bp; |
700 | ||
34dc7c2f BB |
701 | /* |
702 | * If there is only one block (i.e. this is a micro-zap) | |
703 | * and we are not adding anything, the accounting is simple. | |
704 | */ | |
705 | err = dmu_tx_check_ioerr(NULL, dn, 0, 0); | |
706 | if (err) { | |
707 | tx->tx_err = err; | |
708 | return; | |
709 | } | |
710 | ||
711 | /* | |
712 | * Use max block size here, since we don't know how much | |
713 | * the size will change between now and the dbuf dirty call. | |
714 | */ | |
22cd4a46 | 715 | bp = &dn->dn_phys->dn_blkptr[0]; |
34dc7c2f | 716 | if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, |
22cd4a46 | 717 | bp, bp->blk_birth)) |
34dc7c2f | 718 | txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE; |
22cd4a46 | 719 | else |
34dc7c2f | 720 | txh->txh_space_towrite += SPA_MAXBLOCKSIZE; |
22cd4a46 | 721 | if (!BP_IS_HOLE(bp)) |
9babb374 | 722 | txh->txh_space_tounref += SPA_MAXBLOCKSIZE; |
34dc7c2f BB |
723 | return; |
724 | } | |
725 | ||
726 | if (dn->dn_maxblkid > 0 && name) { | |
727 | /* | |
728 | * access the name in this fat-zap so that we'll check | |
729 | * for i/o errors to the leaf blocks, etc. | |
730 | */ | |
428870ff | 731 | err = zap_lookup(dn->dn_objset, dn->dn_object, name, |
34dc7c2f BB |
732 | 8, 0, NULL); |
733 | if (err == EIO) { | |
734 | tx->tx_err = err; | |
735 | return; | |
736 | } | |
737 | } | |
738 | ||
428870ff | 739 | err = zap_count_write(dn->dn_objset, dn->dn_object, name, add, |
45d1cae3 | 740 | &txh->txh_space_towrite, &txh->txh_space_tooverwrite); |
34dc7c2f BB |
741 | |
742 | /* | |
743 | * If the modified blocks are scattered to the four winds, | |
744 | * we'll have to modify an indirect twig for each. | |
745 | */ | |
746 | epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; | |
747 | for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs) | |
9babb374 BB |
748 | if (dn->dn_objset->os_dsl_dataset->ds_phys->ds_prev_snap_obj) |
749 | txh->txh_space_towrite += 3 << dn->dn_indblkshift; | |
750 | else | |
751 | txh->txh_space_tooverwrite += 3 << dn->dn_indblkshift; | |
34dc7c2f BB |
752 | } |
753 | ||
754 | void | |
755 | dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object) | |
756 | { | |
757 | dmu_tx_hold_t *txh; | |
758 | ||
759 | ASSERT(tx->tx_txg == 0); | |
760 | ||
761 | txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, | |
762 | object, THT_BONUS, 0, 0); | |
763 | if (txh) | |
764 | dmu_tx_count_dnode(txh); | |
765 | } | |
766 | ||
767 | void | |
768 | dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space) | |
769 | { | |
770 | dmu_tx_hold_t *txh; | |
771 | ASSERT(tx->tx_txg == 0); | |
772 | ||
773 | txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, | |
774 | DMU_NEW_OBJECT, THT_SPACE, space, 0); | |
775 | ||
776 | txh->txh_space_towrite += space; | |
777 | } | |
778 | ||
779 | int | |
780 | dmu_tx_holds(dmu_tx_t *tx, uint64_t object) | |
781 | { | |
782 | dmu_tx_hold_t *txh; | |
783 | int holds = 0; | |
784 | ||
785 | /* | |
786 | * By asserting that the tx is assigned, we're counting the | |
787 | * number of dn_tx_holds, which is the same as the number of | |
788 | * dn_holds. Otherwise, we'd be counting dn_holds, but | |
789 | * dn_tx_holds could be 0. | |
790 | */ | |
791 | ASSERT(tx->tx_txg != 0); | |
792 | ||
793 | /* if (tx->tx_anyobj == TRUE) */ | |
794 | /* return (0); */ | |
795 | ||
796 | for (txh = list_head(&tx->tx_holds); txh; | |
797 | txh = list_next(&tx->tx_holds, txh)) { | |
798 | if (txh->txh_dnode && txh->txh_dnode->dn_object == object) | |
799 | holds++; | |
800 | } | |
801 | ||
802 | return (holds); | |
803 | } | |
804 | ||
1c5de20a | 805 | #ifdef DEBUG_DMU_TX |
34dc7c2f BB |
806 | void |
807 | dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db) | |
808 | { | |
809 | dmu_tx_hold_t *txh; | |
810 | int match_object = FALSE, match_offset = FALSE; | |
572e2857 | 811 | dnode_t *dn; |
34dc7c2f | 812 | |
572e2857 BB |
813 | DB_DNODE_ENTER(db); |
814 | dn = DB_DNODE(db); | |
99ea23c5 | 815 | ASSERT(dn != NULL); |
34dc7c2f | 816 | ASSERT(tx->tx_txg != 0); |
428870ff | 817 | ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset); |
34dc7c2f BB |
818 | ASSERT3U(dn->dn_object, ==, db->db.db_object); |
819 | ||
572e2857 BB |
820 | if (tx->tx_anyobj) { |
821 | DB_DNODE_EXIT(db); | |
34dc7c2f | 822 | return; |
572e2857 | 823 | } |
34dc7c2f BB |
824 | |
825 | /* XXX No checking on the meta dnode for now */ | |
572e2857 BB |
826 | if (db->db.db_object == DMU_META_DNODE_OBJECT) { |
827 | DB_DNODE_EXIT(db); | |
34dc7c2f | 828 | return; |
572e2857 | 829 | } |
34dc7c2f BB |
830 | |
831 | for (txh = list_head(&tx->tx_holds); txh; | |
832 | txh = list_next(&tx->tx_holds, txh)) { | |
99ea23c5 | 833 | ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); |
34dc7c2f BB |
834 | if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT) |
835 | match_object = TRUE; | |
836 | if (txh->txh_dnode == NULL || txh->txh_dnode == dn) { | |
837 | int datablkshift = dn->dn_datablkshift ? | |
838 | dn->dn_datablkshift : SPA_MAXBLOCKSHIFT; | |
839 | int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; | |
840 | int shift = datablkshift + epbs * db->db_level; | |
841 | uint64_t beginblk = shift >= 64 ? 0 : | |
842 | (txh->txh_arg1 >> shift); | |
843 | uint64_t endblk = shift >= 64 ? 0 : | |
844 | ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift); | |
845 | uint64_t blkid = db->db_blkid; | |
846 | ||
847 | /* XXX txh_arg2 better not be zero... */ | |
848 | ||
849 | dprintf("found txh type %x beginblk=%llx endblk=%llx\n", | |
850 | txh->txh_type, beginblk, endblk); | |
851 | ||
852 | switch (txh->txh_type) { | |
853 | case THT_WRITE: | |
854 | if (blkid >= beginblk && blkid <= endblk) | |
855 | match_offset = TRUE; | |
856 | /* | |
857 | * We will let this hold work for the bonus | |
428870ff BB |
858 | * or spill buffer so that we don't need to |
859 | * hold it when creating a new object. | |
34dc7c2f | 860 | */ |
428870ff BB |
861 | if (blkid == DMU_BONUS_BLKID || |
862 | blkid == DMU_SPILL_BLKID) | |
34dc7c2f BB |
863 | match_offset = TRUE; |
864 | /* | |
865 | * They might have to increase nlevels, | |
866 | * thus dirtying the new TLIBs. Or the | |
867 | * might have to change the block size, | |
868 | * thus dirying the new lvl=0 blk=0. | |
869 | */ | |
870 | if (blkid == 0) | |
871 | match_offset = TRUE; | |
872 | break; | |
873 | case THT_FREE: | |
b128c09f BB |
874 | /* |
875 | * We will dirty all the level 1 blocks in | |
876 | * the free range and perhaps the first and | |
877 | * last level 0 block. | |
878 | */ | |
879 | if (blkid >= beginblk && (blkid <= endblk || | |
880 | txh->txh_arg2 == DMU_OBJECT_END)) | |
34dc7c2f BB |
881 | match_offset = TRUE; |
882 | break; | |
428870ff BB |
883 | case THT_SPILL: |
884 | if (blkid == DMU_SPILL_BLKID) | |
885 | match_offset = TRUE; | |
886 | break; | |
34dc7c2f | 887 | case THT_BONUS: |
428870ff | 888 | if (blkid == DMU_BONUS_BLKID) |
34dc7c2f BB |
889 | match_offset = TRUE; |
890 | break; | |
891 | case THT_ZAP: | |
892 | match_offset = TRUE; | |
893 | break; | |
894 | case THT_NEWOBJECT: | |
895 | match_object = TRUE; | |
896 | break; | |
897 | default: | |
898 | ASSERT(!"bad txh_type"); | |
899 | } | |
900 | } | |
572e2857 BB |
901 | if (match_object && match_offset) { |
902 | DB_DNODE_EXIT(db); | |
34dc7c2f | 903 | return; |
572e2857 | 904 | } |
34dc7c2f | 905 | } |
572e2857 | 906 | DB_DNODE_EXIT(db); |
34dc7c2f BB |
907 | panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n", |
908 | (u_longlong_t)db->db.db_object, db->db_level, | |
909 | (u_longlong_t)db->db_blkid); | |
910 | } | |
911 | #endif | |
912 | ||
913 | static int | |
914 | dmu_tx_try_assign(dmu_tx_t *tx, uint64_t txg_how) | |
915 | { | |
916 | dmu_tx_hold_t *txh; | |
917 | spa_t *spa = tx->tx_pool->dp_spa; | |
b128c09f BB |
918 | uint64_t memory, asize, fsize, usize; |
919 | uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge; | |
34dc7c2f BB |
920 | |
921 | ASSERT3U(tx->tx_txg, ==, 0); | |
922 | ||
570827e1 BB |
923 | if (tx->tx_err) { |
924 | DMU_TX_STAT_BUMP(dmu_tx_error); | |
34dc7c2f | 925 | return (tx->tx_err); |
570827e1 | 926 | } |
34dc7c2f | 927 | |
b128c09f | 928 | if (spa_suspended(spa)) { |
570827e1 BB |
929 | DMU_TX_STAT_BUMP(dmu_tx_suspended); |
930 | ||
34dc7c2f BB |
931 | /* |
932 | * If the user has indicated a blocking failure mode | |
933 | * then return ERESTART which will block in dmu_tx_wait(). | |
934 | * Otherwise, return EIO so that an error can get | |
935 | * propagated back to the VOP calls. | |
936 | * | |
937 | * Note that we always honor the txg_how flag regardless | |
938 | * of the failuremode setting. | |
939 | */ | |
940 | if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE && | |
941 | txg_how != TXG_WAIT) | |
942 | return (EIO); | |
943 | ||
944 | return (ERESTART); | |
945 | } | |
946 | ||
947 | tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh); | |
948 | tx->tx_needassign_txh = NULL; | |
949 | ||
950 | /* | |
951 | * NB: No error returns are allowed after txg_hold_open, but | |
952 | * before processing the dnode holds, due to the | |
953 | * dmu_tx_unassign() logic. | |
954 | */ | |
955 | ||
b128c09f | 956 | towrite = tofree = tooverwrite = tounref = tohold = fudge = 0; |
34dc7c2f BB |
957 | for (txh = list_head(&tx->tx_holds); txh; |
958 | txh = list_next(&tx->tx_holds, txh)) { | |
959 | dnode_t *dn = txh->txh_dnode; | |
960 | if (dn != NULL) { | |
961 | mutex_enter(&dn->dn_mtx); | |
962 | if (dn->dn_assigned_txg == tx->tx_txg - 1) { | |
963 | mutex_exit(&dn->dn_mtx); | |
964 | tx->tx_needassign_txh = txh; | |
570827e1 | 965 | DMU_TX_STAT_BUMP(dmu_tx_group); |
34dc7c2f BB |
966 | return (ERESTART); |
967 | } | |
968 | if (dn->dn_assigned_txg == 0) | |
969 | dn->dn_assigned_txg = tx->tx_txg; | |
970 | ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); | |
971 | (void) refcount_add(&dn->dn_tx_holds, tx); | |
972 | mutex_exit(&dn->dn_mtx); | |
973 | } | |
974 | towrite += txh->txh_space_towrite; | |
975 | tofree += txh->txh_space_tofree; | |
976 | tooverwrite += txh->txh_space_tooverwrite; | |
977 | tounref += txh->txh_space_tounref; | |
b128c09f BB |
978 | tohold += txh->txh_memory_tohold; |
979 | fudge += txh->txh_fudge; | |
34dc7c2f BB |
980 | } |
981 | ||
982 | /* | |
983 | * NB: This check must be after we've held the dnodes, so that | |
984 | * the dmu_tx_unassign() logic will work properly | |
985 | */ | |
570827e1 BB |
986 | if (txg_how >= TXG_INITIAL && txg_how != tx->tx_txg) { |
987 | DMU_TX_STAT_BUMP(dmu_tx_how); | |
34dc7c2f | 988 | return (ERESTART); |
570827e1 | 989 | } |
34dc7c2f BB |
990 | |
991 | /* | |
992 | * If a snapshot has been taken since we made our estimates, | |
993 | * assume that we won't be able to free or overwrite anything. | |
994 | */ | |
995 | if (tx->tx_objset && | |
428870ff | 996 | dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) > |
34dc7c2f BB |
997 | tx->tx_lastsnap_txg) { |
998 | towrite += tooverwrite; | |
999 | tooverwrite = tofree = 0; | |
1000 | } | |
1001 | ||
b128c09f BB |
1002 | /* needed allocation: worst-case estimate of write space */ |
1003 | asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite); | |
1004 | /* freed space estimate: worst-case overwrite + free estimate */ | |
34dc7c2f | 1005 | fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree; |
b128c09f | 1006 | /* convert unrefd space to worst-case estimate */ |
34dc7c2f | 1007 | usize = spa_get_asize(tx->tx_pool->dp_spa, tounref); |
b128c09f BB |
1008 | /* calculate memory footprint estimate */ |
1009 | memory = towrite + tooverwrite + tohold; | |
34dc7c2f | 1010 | |
1c5de20a | 1011 | #ifdef DEBUG_DMU_TX |
b128c09f BB |
1012 | /* |
1013 | * Add in 'tohold' to account for our dirty holds on this memory | |
1014 | * XXX - the "fudge" factor is to account for skipped blocks that | |
1015 | * we missed because dnode_next_offset() misses in-core-only blocks. | |
1016 | */ | |
1017 | tx->tx_space_towrite = asize + | |
1018 | spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge); | |
34dc7c2f BB |
1019 | tx->tx_space_tofree = tofree; |
1020 | tx->tx_space_tooverwrite = tooverwrite; | |
1021 | tx->tx_space_tounref = tounref; | |
1022 | #endif | |
1023 | ||
1024 | if (tx->tx_dir && asize != 0) { | |
b128c09f BB |
1025 | int err = dsl_dir_tempreserve_space(tx->tx_dir, memory, |
1026 | asize, fsize, usize, &tx->tx_tempreserve_cookie, tx); | |
34dc7c2f BB |
1027 | if (err) |
1028 | return (err); | |
1029 | } | |
1030 | ||
570827e1 BB |
1031 | DMU_TX_STAT_BUMP(dmu_tx_assigned); |
1032 | ||
34dc7c2f BB |
1033 | return (0); |
1034 | } | |
1035 | ||
1036 | static void | |
1037 | dmu_tx_unassign(dmu_tx_t *tx) | |
1038 | { | |
1039 | dmu_tx_hold_t *txh; | |
1040 | ||
1041 | if (tx->tx_txg == 0) | |
1042 | return; | |
1043 | ||
1044 | txg_rele_to_quiesce(&tx->tx_txgh); | |
1045 | ||
1046 | for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh; | |
1047 | txh = list_next(&tx->tx_holds, txh)) { | |
1048 | dnode_t *dn = txh->txh_dnode; | |
1049 | ||
1050 | if (dn == NULL) | |
1051 | continue; | |
1052 | mutex_enter(&dn->dn_mtx); | |
1053 | ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); | |
1054 | ||
1055 | if (refcount_remove(&dn->dn_tx_holds, tx) == 0) { | |
1056 | dn->dn_assigned_txg = 0; | |
1057 | cv_broadcast(&dn->dn_notxholds); | |
1058 | } | |
1059 | mutex_exit(&dn->dn_mtx); | |
1060 | } | |
1061 | ||
1062 | txg_rele_to_sync(&tx->tx_txgh); | |
1063 | ||
1064 | tx->tx_lasttried_txg = tx->tx_txg; | |
1065 | tx->tx_txg = 0; | |
1066 | } | |
1067 | ||
1068 | /* | |
1069 | * Assign tx to a transaction group. txg_how can be one of: | |
1070 | * | |
1071 | * (1) TXG_WAIT. If the current open txg is full, waits until there's | |
1072 | * a new one. This should be used when you're not holding locks. | |
1073 | * If will only fail if we're truly out of space (or over quota). | |
1074 | * | |
1075 | * (2) TXG_NOWAIT. If we can't assign into the current open txg without | |
1076 | * blocking, returns immediately with ERESTART. This should be used | |
1077 | * whenever you're holding locks. On an ERESTART error, the caller | |
1078 | * should drop locks, do a dmu_tx_wait(tx), and try again. | |
1079 | * | |
1080 | * (3) A specific txg. Use this if you need to ensure that multiple | |
1081 | * transactions all sync in the same txg. Like TXG_NOWAIT, it | |
1082 | * returns ERESTART if it can't assign you into the requested txg. | |
1083 | */ | |
1084 | int | |
1085 | dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how) | |
1086 | { | |
1087 | int err; | |
1088 | ||
1089 | ASSERT(tx->tx_txg == 0); | |
1090 | ASSERT(txg_how != 0); | |
1091 | ASSERT(!dsl_pool_sync_context(tx->tx_pool)); | |
1092 | ||
1093 | while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) { | |
1094 | dmu_tx_unassign(tx); | |
1095 | ||
1096 | if (err != ERESTART || txg_how != TXG_WAIT) | |
1097 | return (err); | |
1098 | ||
1099 | dmu_tx_wait(tx); | |
1100 | } | |
1101 | ||
1102 | txg_rele_to_quiesce(&tx->tx_txgh); | |
1103 | ||
1104 | return (0); | |
1105 | } | |
1106 | ||
1107 | void | |
1108 | dmu_tx_wait(dmu_tx_t *tx) | |
1109 | { | |
1110 | spa_t *spa = tx->tx_pool->dp_spa; | |
1111 | ||
1112 | ASSERT(tx->tx_txg == 0); | |
1113 | ||
1114 | /* | |
1115 | * It's possible that the pool has become active after this thread | |
1116 | * has tried to obtain a tx. If that's the case then his | |
1117 | * tx_lasttried_txg would not have been assigned. | |
1118 | */ | |
b128c09f | 1119 | if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) { |
34dc7c2f BB |
1120 | txg_wait_synced(tx->tx_pool, spa_last_synced_txg(spa) + 1); |
1121 | } else if (tx->tx_needassign_txh) { | |
1122 | dnode_t *dn = tx->tx_needassign_txh->txh_dnode; | |
1123 | ||
1124 | mutex_enter(&dn->dn_mtx); | |
1125 | while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1) | |
1126 | cv_wait(&dn->dn_notxholds, &dn->dn_mtx); | |
1127 | mutex_exit(&dn->dn_mtx); | |
1128 | tx->tx_needassign_txh = NULL; | |
1129 | } else { | |
1130 | txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1); | |
1131 | } | |
1132 | } | |
1133 | ||
1134 | void | |
1135 | dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta) | |
1136 | { | |
1c5de20a | 1137 | #ifdef DEBUG_DMU_TX |
34dc7c2f BB |
1138 | if (tx->tx_dir == NULL || delta == 0) |
1139 | return; | |
1140 | ||
1141 | if (delta > 0) { | |
1142 | ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=, | |
1143 | tx->tx_space_towrite); | |
1144 | (void) refcount_add_many(&tx->tx_space_written, delta, NULL); | |
1145 | } else { | |
1146 | (void) refcount_add_many(&tx->tx_space_freed, -delta, NULL); | |
1147 | } | |
1148 | #endif | |
1149 | } | |
1150 | ||
1151 | void | |
1152 | dmu_tx_commit(dmu_tx_t *tx) | |
1153 | { | |
1154 | dmu_tx_hold_t *txh; | |
1155 | ||
1156 | ASSERT(tx->tx_txg != 0); | |
1157 | ||
c65aa5b2 | 1158 | while ((txh = list_head(&tx->tx_holds))) { |
34dc7c2f BB |
1159 | dnode_t *dn = txh->txh_dnode; |
1160 | ||
1161 | list_remove(&tx->tx_holds, txh); | |
1162 | kmem_free(txh, sizeof (dmu_tx_hold_t)); | |
1163 | if (dn == NULL) | |
1164 | continue; | |
1165 | mutex_enter(&dn->dn_mtx); | |
1166 | ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); | |
1167 | ||
1168 | if (refcount_remove(&dn->dn_tx_holds, tx) == 0) { | |
1169 | dn->dn_assigned_txg = 0; | |
1170 | cv_broadcast(&dn->dn_notxholds); | |
1171 | } | |
1172 | mutex_exit(&dn->dn_mtx); | |
1173 | dnode_rele(dn, tx); | |
1174 | } | |
1175 | ||
1176 | if (tx->tx_tempreserve_cookie) | |
1177 | dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx); | |
1178 | ||
428870ff BB |
1179 | if (!list_is_empty(&tx->tx_callbacks)) |
1180 | txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks); | |
1181 | ||
34dc7c2f BB |
1182 | if (tx->tx_anyobj == FALSE) |
1183 | txg_rele_to_sync(&tx->tx_txgh); | |
428870ff BB |
1184 | |
1185 | list_destroy(&tx->tx_callbacks); | |
34dc7c2f | 1186 | list_destroy(&tx->tx_holds); |
1c5de20a | 1187 | #ifdef DEBUG_DMU_TX |
34dc7c2f BB |
1188 | dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n", |
1189 | tx->tx_space_towrite, refcount_count(&tx->tx_space_written), | |
1190 | tx->tx_space_tofree, refcount_count(&tx->tx_space_freed)); | |
1191 | refcount_destroy_many(&tx->tx_space_written, | |
1192 | refcount_count(&tx->tx_space_written)); | |
1193 | refcount_destroy_many(&tx->tx_space_freed, | |
1194 | refcount_count(&tx->tx_space_freed)); | |
1195 | #endif | |
1196 | kmem_free(tx, sizeof (dmu_tx_t)); | |
1197 | } | |
1198 | ||
1199 | void | |
1200 | dmu_tx_abort(dmu_tx_t *tx) | |
1201 | { | |
1202 | dmu_tx_hold_t *txh; | |
1203 | ||
1204 | ASSERT(tx->tx_txg == 0); | |
1205 | ||
c65aa5b2 | 1206 | while ((txh = list_head(&tx->tx_holds))) { |
34dc7c2f BB |
1207 | dnode_t *dn = txh->txh_dnode; |
1208 | ||
1209 | list_remove(&tx->tx_holds, txh); | |
1210 | kmem_free(txh, sizeof (dmu_tx_hold_t)); | |
1211 | if (dn != NULL) | |
1212 | dnode_rele(dn, tx); | |
1213 | } | |
428870ff BB |
1214 | |
1215 | /* | |
1216 | * Call any registered callbacks with an error code. | |
1217 | */ | |
1218 | if (!list_is_empty(&tx->tx_callbacks)) | |
1219 | dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED); | |
1220 | ||
1221 | list_destroy(&tx->tx_callbacks); | |
34dc7c2f | 1222 | list_destroy(&tx->tx_holds); |
1c5de20a | 1223 | #ifdef DEBUG_DMU_TX |
34dc7c2f BB |
1224 | refcount_destroy_many(&tx->tx_space_written, |
1225 | refcount_count(&tx->tx_space_written)); | |
1226 | refcount_destroy_many(&tx->tx_space_freed, | |
1227 | refcount_count(&tx->tx_space_freed)); | |
1228 | #endif | |
1229 | kmem_free(tx, sizeof (dmu_tx_t)); | |
1230 | } | |
1231 | ||
1232 | uint64_t | |
1233 | dmu_tx_get_txg(dmu_tx_t *tx) | |
1234 | { | |
1235 | ASSERT(tx->tx_txg != 0); | |
1236 | return (tx->tx_txg); | |
1237 | } | |
428870ff BB |
1238 | |
1239 | void | |
1240 | dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data) | |
1241 | { | |
1242 | dmu_tx_callback_t *dcb; | |
1243 | ||
b8d06fca | 1244 | dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_PUSHPAGE); |
428870ff BB |
1245 | |
1246 | dcb->dcb_func = func; | |
1247 | dcb->dcb_data = data; | |
1248 | ||
1249 | list_insert_tail(&tx->tx_callbacks, dcb); | |
1250 | } | |
1251 | ||
1252 | /* | |
1253 | * Call all the commit callbacks on a list, with a given error code. | |
1254 | */ | |
1255 | void | |
1256 | dmu_tx_do_callbacks(list_t *cb_list, int error) | |
1257 | { | |
1258 | dmu_tx_callback_t *dcb; | |
1259 | ||
c65aa5b2 | 1260 | while ((dcb = list_head(cb_list))) { |
428870ff BB |
1261 | list_remove(cb_list, dcb); |
1262 | dcb->dcb_func(dcb->dcb_data, error); | |
1263 | kmem_free(dcb, sizeof (dmu_tx_callback_t)); | |
1264 | } | |
1265 | } | |
1266 | ||
1267 | /* | |
1268 | * Interface to hold a bunch of attributes. | |
1269 | * used for creating new files. | |
1270 | * attrsize is the total size of all attributes | |
1271 | * to be added during object creation | |
1272 | * | |
1273 | * For updating/adding a single attribute dmu_tx_hold_sa() should be used. | |
1274 | */ | |
1275 | ||
1276 | /* | |
1277 | * hold necessary attribute name for attribute registration. | |
1278 | * should be a very rare case where this is needed. If it does | |
1279 | * happen it would only happen on the first write to the file system. | |
1280 | */ | |
1281 | static void | |
1282 | dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx) | |
1283 | { | |
1284 | int i; | |
1285 | ||
1286 | if (!sa->sa_need_attr_registration) | |
1287 | return; | |
1288 | ||
1289 | for (i = 0; i != sa->sa_num_attrs; i++) { | |
1290 | if (!sa->sa_attr_table[i].sa_registered) { | |
1291 | if (sa->sa_reg_attr_obj) | |
1292 | dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj, | |
1293 | B_TRUE, sa->sa_attr_table[i].sa_name); | |
1294 | else | |
1295 | dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, | |
1296 | B_TRUE, sa->sa_attr_table[i].sa_name); | |
1297 | } | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | ||
1302 | void | |
1303 | dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object) | |
1304 | { | |
1305 | dnode_t *dn; | |
1306 | dmu_tx_hold_t *txh; | |
428870ff BB |
1307 | |
1308 | txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object, | |
1309 | THT_SPILL, 0, 0); | |
1310 | ||
1311 | dn = txh->txh_dnode; | |
1312 | ||
1313 | if (dn == NULL) | |
1314 | return; | |
1315 | ||
1316 | /* If blkptr doesn't exist then add space to towrite */ | |
22cd4a46 | 1317 | if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) { |
428870ff | 1318 | txh->txh_space_towrite += SPA_MAXBLOCKSIZE; |
428870ff | 1319 | } else { |
22cd4a46 AL |
1320 | blkptr_t *bp; |
1321 | ||
1322 | bp = &dn->dn_phys->dn_spill; | |
428870ff BB |
1323 | if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, |
1324 | bp, bp->blk_birth)) | |
1325 | txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE; | |
1326 | else | |
1327 | txh->txh_space_towrite += SPA_MAXBLOCKSIZE; | |
22cd4a46 | 1328 | if (!BP_IS_HOLE(bp)) |
428870ff BB |
1329 | txh->txh_space_tounref += SPA_MAXBLOCKSIZE; |
1330 | } | |
1331 | } | |
1332 | ||
1333 | void | |
1334 | dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize) | |
1335 | { | |
1336 | sa_os_t *sa = tx->tx_objset->os_sa; | |
1337 | ||
1338 | dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); | |
1339 | ||
1340 | if (tx->tx_objset->os_sa->sa_master_obj == 0) | |
1341 | return; | |
1342 | ||
1343 | if (tx->tx_objset->os_sa->sa_layout_attr_obj) | |
1344 | dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL); | |
1345 | else { | |
1346 | dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS); | |
1347 | dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY); | |
1348 | dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); | |
1349 | dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); | |
1350 | } | |
1351 | ||
1352 | dmu_tx_sa_registration_hold(sa, tx); | |
1353 | ||
1354 | if (attrsize <= DN_MAX_BONUSLEN && !sa->sa_force_spill) | |
1355 | return; | |
1356 | ||
1357 | (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT, | |
1358 | THT_SPILL, 0, 0); | |
1359 | } | |
1360 | ||
1361 | /* | |
1362 | * Hold SA attribute | |
1363 | * | |
1364 | * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size) | |
1365 | * | |
1366 | * variable_size is the total size of all variable sized attributes | |
1367 | * passed to this function. It is not the total size of all | |
1368 | * variable size attributes that *may* exist on this object. | |
1369 | */ | |
1370 | void | |
1371 | dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow) | |
1372 | { | |
1373 | uint64_t object; | |
1374 | sa_os_t *sa = tx->tx_objset->os_sa; | |
1375 | ||
1376 | ASSERT(hdl != NULL); | |
1377 | ||
1378 | object = sa_handle_object(hdl); | |
1379 | ||
1380 | dmu_tx_hold_bonus(tx, object); | |
1381 | ||
1382 | if (tx->tx_objset->os_sa->sa_master_obj == 0) | |
1383 | return; | |
1384 | ||
1385 | if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 || | |
1386 | tx->tx_objset->os_sa->sa_layout_attr_obj == 0) { | |
1387 | dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS); | |
1388 | dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY); | |
1389 | dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); | |
1390 | dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); | |
1391 | } | |
1392 | ||
1393 | dmu_tx_sa_registration_hold(sa, tx); | |
1394 | ||
1395 | if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj) | |
1396 | dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL); | |
1397 | ||
572e2857 | 1398 | if (sa->sa_force_spill || may_grow || hdl->sa_spill) { |
428870ff BB |
1399 | ASSERT(tx->tx_txg == 0); |
1400 | dmu_tx_hold_spill(tx, object); | |
572e2857 BB |
1401 | } else { |
1402 | dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus; | |
1403 | dnode_t *dn; | |
1404 | ||
1405 | DB_DNODE_ENTER(db); | |
1406 | dn = DB_DNODE(db); | |
1407 | if (dn->dn_have_spill) { | |
1408 | ASSERT(tx->tx_txg == 0); | |
1409 | dmu_tx_hold_spill(tx, object); | |
1410 | } | |
1411 | DB_DNODE_EXIT(db); | |
428870ff BB |
1412 | } |
1413 | } | |
c28b2279 | 1414 | |
570827e1 BB |
1415 | void |
1416 | dmu_tx_init(void) | |
1417 | { | |
1418 | dmu_tx_ksp = kstat_create("zfs", 0, "dmu_tx", "misc", | |
1419 | KSTAT_TYPE_NAMED, sizeof (dmu_tx_stats) / sizeof (kstat_named_t), | |
1420 | KSTAT_FLAG_VIRTUAL); | |
1421 | ||
1422 | if (dmu_tx_ksp != NULL) { | |
1423 | dmu_tx_ksp->ks_data = &dmu_tx_stats; | |
1424 | kstat_install(dmu_tx_ksp); | |
1425 | } | |
1426 | } | |
1427 | ||
1428 | void | |
1429 | dmu_tx_fini(void) | |
1430 | { | |
1431 | if (dmu_tx_ksp != NULL) { | |
1432 | kstat_delete(dmu_tx_ksp); | |
1433 | dmu_tx_ksp = NULL; | |
1434 | } | |
1435 | } | |
1436 | ||
c28b2279 BB |
1437 | #if defined(_KERNEL) && defined(HAVE_SPL) |
1438 | EXPORT_SYMBOL(dmu_tx_create); | |
1439 | EXPORT_SYMBOL(dmu_tx_hold_write); | |
1440 | EXPORT_SYMBOL(dmu_tx_hold_free); | |
1441 | EXPORT_SYMBOL(dmu_tx_hold_zap); | |
1442 | EXPORT_SYMBOL(dmu_tx_hold_bonus); | |
1443 | EXPORT_SYMBOL(dmu_tx_abort); | |
1444 | EXPORT_SYMBOL(dmu_tx_assign); | |
1445 | EXPORT_SYMBOL(dmu_tx_wait); | |
1446 | EXPORT_SYMBOL(dmu_tx_commit); | |
1447 | EXPORT_SYMBOL(dmu_tx_get_txg); | |
1448 | EXPORT_SYMBOL(dmu_tx_callback_register); | |
1449 | EXPORT_SYMBOL(dmu_tx_do_callbacks); | |
1450 | EXPORT_SYMBOL(dmu_tx_hold_spill); | |
1451 | EXPORT_SYMBOL(dmu_tx_hold_sa_create); | |
1452 | EXPORT_SYMBOL(dmu_tx_hold_sa); | |
1453 | #endif |