]>
Commit | Line | Data |
---|---|---|
a1d477c2 MA |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * This file and its contents are supplied under the terms of the | |
5 | * Common Development and Distribution License ("CDDL"), version 1.0. | |
6 | * You may only use this file in accordance with the terms of version | |
7 | * 1.0 of the CDDL. | |
8 | * | |
9 | * A full copy of the text of the CDDL should have accompanied this | |
10 | * source. A copy of the CDDL is also available via the Internet at | |
11 | * http://www.illumos.org/license/CDDL. | |
12 | * | |
13 | * CDDL HEADER END | |
14 | */ | |
15 | ||
16 | /* | |
4bf8108e | 17 | * Copyright (c) 2014, 2017 by Delphix. All rights reserved. |
a1d477c2 MA |
18 | */ |
19 | ||
20 | #include <sys/zfs_context.h> | |
21 | #include <sys/spa.h> | |
22 | #include <sys/spa_impl.h> | |
23 | #include <sys/vdev_impl.h> | |
24 | #include <sys/fs/zfs.h> | |
25 | #include <sys/zio.h> | |
9e052db4 | 26 | #include <sys/zio_checksum.h> |
a1d477c2 MA |
27 | #include <sys/metaslab.h> |
28 | #include <sys/refcount.h> | |
29 | #include <sys/dmu.h> | |
30 | #include <sys/vdev_indirect_mapping.h> | |
31 | #include <sys/dmu_tx.h> | |
32 | #include <sys/dsl_synctask.h> | |
33 | #include <sys/zap.h> | |
9d5b5245 SD |
34 | #include <sys/abd.h> |
35 | #include <sys/zthr.h> | |
a1d477c2 MA |
36 | |
37 | /* | |
38 | * An indirect vdev corresponds to a vdev that has been removed. Since | |
39 | * we cannot rewrite block pointers of snapshots, etc., we keep a | |
40 | * mapping from old location on the removed device to the new location | |
41 | * on another device in the pool and use this mapping whenever we need | |
42 | * to access the DVA. Unfortunately, this mapping did not respect | |
43 | * logical block boundaries when it was first created, and so a DVA on | |
44 | * this indirect vdev may be "split" into multiple sections that each | |
45 | * map to a different location. As a consequence, not all DVAs can be | |
46 | * translated to an equivalent new DVA. Instead we must provide a | |
47 | * "vdev_remap" operation that executes a callback on each contiguous | |
48 | * segment of the new location. This function is used in multiple ways: | |
49 | * | |
9e052db4 MA |
50 | * - i/os to this vdev use the callback to determine where the |
51 | * data is now located, and issue child i/os for each segment's new | |
52 | * location. | |
a1d477c2 | 53 | * |
9e052db4 | 54 | * - frees and claims to this vdev use the callback to free or claim |
a1d477c2 MA |
55 | * each mapped segment. (Note that we don't actually need to claim |
56 | * log blocks on indirect vdevs, because we don't allocate to | |
57 | * removing vdevs. However, zdb uses zio_claim() for its leak | |
58 | * detection.) | |
59 | */ | |
60 | ||
61 | /* | |
62 | * "Big theory statement" for how we mark blocks obsolete. | |
63 | * | |
64 | * When a block on an indirect vdev is freed or remapped, a section of | |
65 | * that vdev's mapping may no longer be referenced (aka "obsolete"). We | |
66 | * keep track of how much of each mapping entry is obsolete. When | |
67 | * an entry becomes completely obsolete, we can remove it, thus reducing | |
68 | * the memory used by the mapping. The complete picture of obsolescence | |
69 | * is given by the following data structures, described below: | |
70 | * - the entry-specific obsolete count | |
71 | * - the vdev-specific obsolete spacemap | |
72 | * - the pool-specific obsolete bpobj | |
73 | * | |
74 | * == On disk data structures used == | |
75 | * | |
76 | * We track the obsolete space for the pool using several objects. Each | |
77 | * of these objects is created on demand and freed when no longer | |
78 | * needed, and is assumed to be empty if it does not exist. | |
79 | * SPA_FEATURE_OBSOLETE_COUNTS includes the count of these objects. | |
80 | * | |
81 | * - Each vic_mapping_object (associated with an indirect vdev) can | |
82 | * have a vimp_counts_object. This is an array of uint32_t's | |
83 | * with the same number of entries as the vic_mapping_object. When | |
84 | * the mapping is condensed, entries from the vic_obsolete_sm_object | |
85 | * (see below) are folded into the counts. Therefore, each | |
86 | * obsolete_counts entry tells us the number of bytes in the | |
87 | * corresponding mapping entry that were not referenced when the | |
88 | * mapping was last condensed. | |
89 | * | |
90 | * - Each indirect or removing vdev can have a vic_obsolete_sm_object. | |
91 | * This is a space map containing an alloc entry for every DVA that | |
92 | * has been obsoleted since the last time this indirect vdev was | |
93 | * condensed. We use this object in order to improve performance | |
94 | * when marking a DVA as obsolete. Instead of modifying an arbitrary | |
95 | * offset of the vimp_counts_object, we only need to append an entry | |
96 | * to the end of this object. When a DVA becomes obsolete, it is | |
97 | * added to the obsolete space map. This happens when the DVA is | |
98 | * freed, remapped and not referenced by a snapshot, or the last | |
99 | * snapshot referencing it is destroyed. | |
100 | * | |
101 | * - Each dataset can have a ds_remap_deadlist object. This is a | |
102 | * deadlist object containing all blocks that were remapped in this | |
103 | * dataset but referenced in a previous snapshot. Blocks can *only* | |
104 | * appear on this list if they were remapped (dsl_dataset_block_remapped); | |
105 | * blocks that were killed in a head dataset are put on the normal | |
106 | * ds_deadlist and marked obsolete when they are freed. | |
107 | * | |
108 | * - The pool can have a dp_obsolete_bpobj. This is a list of blocks | |
109 | * in the pool that need to be marked obsolete. When a snapshot is | |
110 | * destroyed, we move some of the ds_remap_deadlist to the obsolete | |
111 | * bpobj (see dsl_destroy_snapshot_handle_remaps()). We then | |
112 | * asynchronously process the obsolete bpobj, moving its entries to | |
113 | * the specific vdevs' obsolete space maps. | |
114 | * | |
115 | * == Summary of how we mark blocks as obsolete == | |
116 | * | |
117 | * - When freeing a block: if any DVA is on an indirect vdev, append to | |
118 | * vic_obsolete_sm_object. | |
119 | * - When remapping a block, add dva to ds_remap_deadlist (if prev snap | |
120 | * references; otherwise append to vic_obsolete_sm_object). | |
121 | * - When freeing a snapshot: move parts of ds_remap_deadlist to | |
122 | * dp_obsolete_bpobj (same algorithm as ds_deadlist). | |
123 | * - When syncing the spa: process dp_obsolete_bpobj, moving ranges to | |
124 | * individual vdev's vic_obsolete_sm_object. | |
125 | */ | |
126 | ||
127 | /* | |
128 | * "Big theory statement" for how we condense indirect vdevs. | |
129 | * | |
130 | * Condensing an indirect vdev's mapping is the process of determining | |
131 | * the precise counts of obsolete space for each mapping entry (by | |
132 | * integrating the obsolete spacemap into the obsolete counts) and | |
133 | * writing out a new mapping that contains only referenced entries. | |
134 | * | |
135 | * We condense a vdev when we expect the mapping to shrink (see | |
136 | * vdev_indirect_should_condense()), but only perform one condense at a | |
137 | * time to limit the memory usage. In addition, we use a separate | |
138 | * open-context thread (spa_condense_indirect_thread) to incrementally | |
139 | * create the new mapping object in a way that minimizes the impact on | |
140 | * the rest of the system. | |
141 | * | |
142 | * == Generating a new mapping == | |
143 | * | |
144 | * To generate a new mapping, we follow these steps: | |
145 | * | |
146 | * 1. Save the old obsolete space map and create a new mapping object | |
147 | * (see spa_condense_indirect_start_sync()). This initializes the | |
148 | * spa_condensing_indirect_phys with the "previous obsolete space map", | |
149 | * which is now read only. Newly obsolete DVAs will be added to a | |
150 | * new (initially empty) obsolete space map, and will not be | |
151 | * considered as part of this condense operation. | |
152 | * | |
153 | * 2. Construct in memory the precise counts of obsolete space for each | |
154 | * mapping entry, by incorporating the obsolete space map into the | |
155 | * counts. (See vdev_indirect_mapping_load_obsolete_{counts,spacemap}().) | |
156 | * | |
157 | * 3. Iterate through each mapping entry, writing to the new mapping any | |
158 | * entries that are not completely obsolete (i.e. which don't have | |
159 | * obsolete count == mapping length). (See | |
160 | * spa_condense_indirect_generate_new_mapping().) | |
161 | * | |
162 | * 4. Destroy the old mapping object and switch over to the new one | |
163 | * (spa_condense_indirect_complete_sync). | |
164 | * | |
165 | * == Restarting from failure == | |
166 | * | |
167 | * To restart the condense when we import/open the pool, we must start | |
168 | * at the 2nd step above: reconstruct the precise counts in memory, | |
169 | * based on the space map + counts. Then in the 3rd step, we start | |
170 | * iterating where we left off: at vimp_max_offset of the new mapping | |
171 | * object. | |
172 | */ | |
173 | ||
0dc2f70c | 174 | int zfs_condense_indirect_vdevs_enable = B_TRUE; |
a1d477c2 MA |
175 | |
176 | /* | |
177 | * Condense if at least this percent of the bytes in the mapping is | |
178 | * obsolete. With the default of 25%, the amount of space mapped | |
179 | * will be reduced to 1% of its original size after at most 16 | |
180 | * condenses. Higher values will condense less often (causing less | |
181 | * i/o); lower values will reduce the mapping size more quickly. | |
182 | */ | |
183 | int zfs_indirect_condense_obsolete_pct = 25; | |
184 | ||
185 | /* | |
186 | * Condense if the obsolete space map takes up more than this amount of | |
187 | * space on disk (logically). This limits the amount of disk space | |
188 | * consumed by the obsolete space map; the default of 1GB is small enough | |
189 | * that we typically don't mind "wasting" it. | |
190 | */ | |
0dc2f70c | 191 | unsigned long zfs_condense_max_obsolete_bytes = 1024 * 1024 * 1024; |
a1d477c2 MA |
192 | |
193 | /* | |
194 | * Don't bother condensing if the mapping uses less than this amount of | |
195 | * memory. The default of 128KB is considered a "trivial" amount of | |
196 | * memory and not worth reducing. | |
197 | */ | |
198 | unsigned long zfs_condense_min_mapping_bytes = 128 * 1024; | |
199 | ||
200 | /* | |
201 | * This is used by the test suite so that it can ensure that certain | |
202 | * actions happen while in the middle of a condense (which might otherwise | |
203 | * complete too quickly). If used to reduce the performance impact of | |
204 | * condensing in production, a maximum value of 1 should be sufficient. | |
205 | */ | |
206 | int zfs_condense_indirect_commit_entry_delay_ms = 0; | |
207 | ||
9e052db4 | 208 | /* |
4589f3ae BB |
209 | * If an indirect split block contains more than this many possible unique |
210 | * combinations when being reconstructed, consider it too computationally | |
211 | * expensive to check them all. Instead, try at most 100 randomly-selected | |
212 | * combinations each time the block is accessed. This allows all segment | |
213 | * copies to participate fairly in the reconstruction when all combinations | |
214 | * cannot be checked and prevents repeated use of one bad copy. | |
9e052db4 | 215 | */ |
1258bd77 BB |
216 | int zfs_reconstruct_indirect_combinations_max = 256; |
217 | ||
218 | ||
219 | /* | |
220 | * Enable to simulate damaged segments and validate reconstruction. This | |
221 | * is intentionally not exposed as a module parameter. | |
222 | */ | |
223 | unsigned long zfs_reconstruct_indirect_damage_fraction = 0; | |
9e052db4 MA |
224 | |
225 | /* | |
226 | * The indirect_child_t represents the vdev that we will read from, when we | |
227 | * need to read all copies of the data (e.g. for scrub or reconstruction). | |
228 | * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror), | |
229 | * ic_vdev is the same as is_vdev. However, for mirror top-level vdevs, | |
230 | * ic_vdev is a child of the mirror. | |
231 | */ | |
232 | typedef struct indirect_child { | |
233 | abd_t *ic_data; | |
234 | vdev_t *ic_vdev; | |
4589f3ae BB |
235 | |
236 | /* | |
1258bd77 BB |
237 | * ic_duplicate is NULL when the ic_data contents are unique, when it |
238 | * is determined to be a duplicate it references the primary child. | |
4589f3ae | 239 | */ |
1258bd77 BB |
240 | struct indirect_child *ic_duplicate; |
241 | list_node_t ic_node; /* node on is_unique_child */ | |
9e052db4 MA |
242 | } indirect_child_t; |
243 | ||
244 | /* | |
245 | * The indirect_split_t represents one mapped segment of an i/o to the | |
246 | * indirect vdev. For non-split (contiguously-mapped) blocks, there will be | |
247 | * only one indirect_split_t, with is_split_offset==0 and is_size==io_size. | |
248 | * For split blocks, there will be several of these. | |
249 | */ | |
250 | typedef struct indirect_split { | |
251 | list_node_t is_node; /* link on iv_splits */ | |
252 | ||
253 | /* | |
254 | * is_split_offset is the offset into the i/o. | |
255 | * This is the sum of the previous splits' is_size's. | |
256 | */ | |
257 | uint64_t is_split_offset; | |
258 | ||
259 | vdev_t *is_vdev; /* top-level vdev */ | |
260 | uint64_t is_target_offset; /* offset on is_vdev */ | |
261 | uint64_t is_size; | |
262 | int is_children; /* number of entries in is_child[] */ | |
1258bd77 BB |
263 | int is_unique_children; /* number of entries in is_unique_child */ |
264 | list_t is_unique_child; | |
9e052db4 MA |
265 | |
266 | /* | |
267 | * is_good_child is the child that we are currently using to | |
268 | * attempt reconstruction. | |
269 | */ | |
1258bd77 | 270 | indirect_child_t *is_good_child; |
9e052db4 MA |
271 | |
272 | indirect_child_t is_child[1]; /* variable-length */ | |
273 | } indirect_split_t; | |
274 | ||
275 | /* | |
276 | * The indirect_vsd_t is associated with each i/o to the indirect vdev. | |
277 | * It is the "Vdev-Specific Data" in the zio_t's io_vsd. | |
278 | */ | |
279 | typedef struct indirect_vsd { | |
280 | boolean_t iv_split_block; | |
281 | boolean_t iv_reconstruct; | |
1258bd77 BB |
282 | uint64_t iv_unique_combinations; |
283 | uint64_t iv_attempts; | |
284 | uint64_t iv_attempts_max; | |
9e052db4 MA |
285 | |
286 | list_t iv_splits; /* list of indirect_split_t's */ | |
287 | } indirect_vsd_t; | |
288 | ||
289 | static void | |
290 | vdev_indirect_map_free(zio_t *zio) | |
291 | { | |
292 | indirect_vsd_t *iv = zio->io_vsd; | |
293 | ||
294 | indirect_split_t *is; | |
295 | while ((is = list_head(&iv->iv_splits)) != NULL) { | |
296 | for (int c = 0; c < is->is_children; c++) { | |
297 | indirect_child_t *ic = &is->is_child[c]; | |
298 | if (ic->ic_data != NULL) | |
299 | abd_free(ic->ic_data); | |
300 | } | |
301 | list_remove(&iv->iv_splits, is); | |
1258bd77 BB |
302 | |
303 | indirect_child_t *ic; | |
304 | while ((ic = list_head(&is->is_unique_child)) != NULL) | |
305 | list_remove(&is->is_unique_child, ic); | |
306 | ||
307 | list_destroy(&is->is_unique_child); | |
308 | ||
9e052db4 MA |
309 | kmem_free(is, |
310 | offsetof(indirect_split_t, is_child[is->is_children])); | |
311 | } | |
312 | kmem_free(iv, sizeof (*iv)); | |
313 | } | |
314 | ||
315 | static const zio_vsd_ops_t vdev_indirect_vsd_ops = { | |
089500e7 MW |
316 | .vsd_free = vdev_indirect_map_free, |
317 | .vsd_cksum_report = zio_vsd_default_cksum_report | |
9e052db4 MA |
318 | }; |
319 | ||
a1d477c2 | 320 | /* |
d2734cce | 321 | * Mark the given offset and size as being obsolete. |
a1d477c2 MA |
322 | */ |
323 | void | |
d2734cce | 324 | vdev_indirect_mark_obsolete(vdev_t *vd, uint64_t offset, uint64_t size) |
a1d477c2 MA |
325 | { |
326 | spa_t *spa = vd->vdev_spa; | |
d2734cce | 327 | |
a1d477c2 MA |
328 | ASSERT3U(vd->vdev_indirect_config.vic_mapping_object, !=, 0); |
329 | ASSERT(vd->vdev_removing || vd->vdev_ops == &vdev_indirect_ops); | |
330 | ASSERT(size > 0); | |
331 | VERIFY(vdev_indirect_mapping_entry_for_offset( | |
332 | vd->vdev_indirect_mapping, offset) != NULL); | |
333 | ||
334 | if (spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS)) { | |
335 | mutex_enter(&vd->vdev_obsolete_lock); | |
336 | range_tree_add(vd->vdev_obsolete_segments, offset, size); | |
337 | mutex_exit(&vd->vdev_obsolete_lock); | |
d2734cce | 338 | vdev_dirty(vd, 0, NULL, spa_syncing_txg(spa)); |
a1d477c2 MA |
339 | } |
340 | } | |
341 | ||
342 | /* | |
343 | * Mark the DVA vdev_id:offset:size as being obsolete in the given tx. This | |
344 | * wrapper is provided because the DMU does not know about vdev_t's and | |
345 | * cannot directly call vdev_indirect_mark_obsolete. | |
346 | */ | |
347 | void | |
348 | spa_vdev_indirect_mark_obsolete(spa_t *spa, uint64_t vdev_id, uint64_t offset, | |
349 | uint64_t size, dmu_tx_t *tx) | |
350 | { | |
351 | vdev_t *vd = vdev_lookup_top(spa, vdev_id); | |
352 | ASSERT(dmu_tx_is_syncing(tx)); | |
353 | ||
354 | /* The DMU can only remap indirect vdevs. */ | |
355 | ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); | |
d2734cce | 356 | vdev_indirect_mark_obsolete(vd, offset, size); |
a1d477c2 MA |
357 | } |
358 | ||
359 | static spa_condensing_indirect_t * | |
360 | spa_condensing_indirect_create(spa_t *spa) | |
361 | { | |
362 | spa_condensing_indirect_phys_t *scip = | |
363 | &spa->spa_condensing_indirect_phys; | |
364 | spa_condensing_indirect_t *sci = kmem_zalloc(sizeof (*sci), KM_SLEEP); | |
365 | objset_t *mos = spa->spa_meta_objset; | |
366 | ||
367 | for (int i = 0; i < TXG_SIZE; i++) { | |
368 | list_create(&sci->sci_new_mapping_entries[i], | |
369 | sizeof (vdev_indirect_mapping_entry_t), | |
370 | offsetof(vdev_indirect_mapping_entry_t, vime_node)); | |
371 | } | |
372 | ||
373 | sci->sci_new_mapping = | |
374 | vdev_indirect_mapping_open(mos, scip->scip_next_mapping_object); | |
375 | ||
376 | return (sci); | |
377 | } | |
378 | ||
379 | static void | |
380 | spa_condensing_indirect_destroy(spa_condensing_indirect_t *sci) | |
381 | { | |
382 | for (int i = 0; i < TXG_SIZE; i++) | |
383 | list_destroy(&sci->sci_new_mapping_entries[i]); | |
384 | ||
385 | if (sci->sci_new_mapping != NULL) | |
386 | vdev_indirect_mapping_close(sci->sci_new_mapping); | |
387 | ||
388 | kmem_free(sci, sizeof (*sci)); | |
389 | } | |
390 | ||
391 | boolean_t | |
392 | vdev_indirect_should_condense(vdev_t *vd) | |
393 | { | |
394 | vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; | |
395 | spa_t *spa = vd->vdev_spa; | |
396 | ||
397 | ASSERT(dsl_pool_sync_context(spa->spa_dsl_pool)); | |
398 | ||
399 | if (!zfs_condense_indirect_vdevs_enable) | |
400 | return (B_FALSE); | |
401 | ||
402 | /* | |
403 | * We can only condense one indirect vdev at a time. | |
404 | */ | |
405 | if (spa->spa_condensing_indirect != NULL) | |
406 | return (B_FALSE); | |
407 | ||
408 | if (spa_shutting_down(spa)) | |
409 | return (B_FALSE); | |
410 | ||
411 | /* | |
412 | * The mapping object size must not change while we are | |
413 | * condensing, so we can only condense indirect vdevs | |
414 | * (not vdevs that are still in the middle of being removed). | |
415 | */ | |
416 | if (vd->vdev_ops != &vdev_indirect_ops) | |
417 | return (B_FALSE); | |
418 | ||
419 | /* | |
420 | * If nothing new has been marked obsolete, there is no | |
421 | * point in condensing. | |
422 | */ | |
27f80e85 BB |
423 | ASSERTV(uint64_t obsolete_sm_obj); |
424 | ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_obj)); | |
a1d477c2 | 425 | if (vd->vdev_obsolete_sm == NULL) { |
27f80e85 | 426 | ASSERT0(obsolete_sm_obj); |
a1d477c2 MA |
427 | return (B_FALSE); |
428 | } | |
429 | ||
430 | ASSERT(vd->vdev_obsolete_sm != NULL); | |
431 | ||
27f80e85 | 432 | ASSERT3U(obsolete_sm_obj, ==, space_map_object(vd->vdev_obsolete_sm)); |
a1d477c2 MA |
433 | |
434 | uint64_t bytes_mapped = vdev_indirect_mapping_bytes_mapped(vim); | |
435 | uint64_t bytes_obsolete = space_map_allocated(vd->vdev_obsolete_sm); | |
436 | uint64_t mapping_size = vdev_indirect_mapping_size(vim); | |
437 | uint64_t obsolete_sm_size = space_map_length(vd->vdev_obsolete_sm); | |
438 | ||
439 | ASSERT3U(bytes_obsolete, <=, bytes_mapped); | |
440 | ||
441 | /* | |
442 | * If a high percentage of the bytes that are mapped have become | |
443 | * obsolete, condense (unless the mapping is already small enough). | |
444 | * This has a good chance of reducing the amount of memory used | |
445 | * by the mapping. | |
446 | */ | |
447 | if (bytes_obsolete * 100 / bytes_mapped >= | |
448 | zfs_indirect_condense_obsolete_pct && | |
449 | mapping_size > zfs_condense_min_mapping_bytes) { | |
450 | zfs_dbgmsg("should condense vdev %llu because obsolete " | |
451 | "spacemap covers %d%% of %lluMB mapping", | |
452 | (u_longlong_t)vd->vdev_id, | |
453 | (int)(bytes_obsolete * 100 / bytes_mapped), | |
454 | (u_longlong_t)bytes_mapped / 1024 / 1024); | |
455 | return (B_TRUE); | |
456 | } | |
457 | ||
458 | /* | |
459 | * If the obsolete space map takes up too much space on disk, | |
460 | * condense in order to free up this disk space. | |
461 | */ | |
462 | if (obsolete_sm_size >= zfs_condense_max_obsolete_bytes) { | |
463 | zfs_dbgmsg("should condense vdev %llu because obsolete sm " | |
464 | "length %lluMB >= max size %lluMB", | |
465 | (u_longlong_t)vd->vdev_id, | |
466 | (u_longlong_t)obsolete_sm_size / 1024 / 1024, | |
467 | (u_longlong_t)zfs_condense_max_obsolete_bytes / | |
468 | 1024 / 1024); | |
469 | return (B_TRUE); | |
470 | } | |
471 | ||
472 | return (B_FALSE); | |
473 | } | |
474 | ||
475 | /* | |
476 | * This sync task completes (finishes) a condense, deleting the old | |
477 | * mapping and replacing it with the new one. | |
478 | */ | |
479 | static void | |
480 | spa_condense_indirect_complete_sync(void *arg, dmu_tx_t *tx) | |
481 | { | |
482 | spa_condensing_indirect_t *sci = arg; | |
483 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
484 | spa_condensing_indirect_phys_t *scip = | |
485 | &spa->spa_condensing_indirect_phys; | |
486 | vdev_t *vd = vdev_lookup_top(spa, scip->scip_vdev); | |
487 | vdev_indirect_config_t *vic = &vd->vdev_indirect_config; | |
488 | objset_t *mos = spa->spa_meta_objset; | |
489 | vdev_indirect_mapping_t *old_mapping = vd->vdev_indirect_mapping; | |
490 | uint64_t old_count = vdev_indirect_mapping_num_entries(old_mapping); | |
491 | uint64_t new_count = | |
492 | vdev_indirect_mapping_num_entries(sci->sci_new_mapping); | |
493 | ||
494 | ASSERT(dmu_tx_is_syncing(tx)); | |
495 | ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); | |
496 | ASSERT3P(sci, ==, spa->spa_condensing_indirect); | |
497 | for (int i = 0; i < TXG_SIZE; i++) { | |
498 | ASSERT(list_is_empty(&sci->sci_new_mapping_entries[i])); | |
499 | } | |
500 | ASSERT(vic->vic_mapping_object != 0); | |
501 | ASSERT3U(vd->vdev_id, ==, scip->scip_vdev); | |
502 | ASSERT(scip->scip_next_mapping_object != 0); | |
503 | ASSERT(scip->scip_prev_obsolete_sm_object != 0); | |
504 | ||
505 | /* | |
506 | * Reset vdev_indirect_mapping to refer to the new object. | |
507 | */ | |
508 | rw_enter(&vd->vdev_indirect_rwlock, RW_WRITER); | |
509 | vdev_indirect_mapping_close(vd->vdev_indirect_mapping); | |
510 | vd->vdev_indirect_mapping = sci->sci_new_mapping; | |
511 | rw_exit(&vd->vdev_indirect_rwlock); | |
512 | ||
513 | sci->sci_new_mapping = NULL; | |
514 | vdev_indirect_mapping_free(mos, vic->vic_mapping_object, tx); | |
515 | vic->vic_mapping_object = scip->scip_next_mapping_object; | |
516 | scip->scip_next_mapping_object = 0; | |
517 | ||
518 | space_map_free_obj(mos, scip->scip_prev_obsolete_sm_object, tx); | |
519 | spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); | |
520 | scip->scip_prev_obsolete_sm_object = 0; | |
521 | ||
522 | scip->scip_vdev = 0; | |
523 | ||
524 | VERIFY0(zap_remove(mos, DMU_POOL_DIRECTORY_OBJECT, | |
525 | DMU_POOL_CONDENSING_INDIRECT, tx)); | |
526 | spa_condensing_indirect_destroy(spa->spa_condensing_indirect); | |
527 | spa->spa_condensing_indirect = NULL; | |
528 | ||
529 | zfs_dbgmsg("finished condense of vdev %llu in txg %llu: " | |
530 | "new mapping object %llu has %llu entries " | |
531 | "(was %llu entries)", | |
532 | vd->vdev_id, dmu_tx_get_txg(tx), vic->vic_mapping_object, | |
533 | new_count, old_count); | |
534 | ||
535 | vdev_config_dirty(spa->spa_root_vdev); | |
536 | } | |
537 | ||
538 | /* | |
539 | * This sync task appends entries to the new mapping object. | |
540 | */ | |
541 | static void | |
542 | spa_condense_indirect_commit_sync(void *arg, dmu_tx_t *tx) | |
543 | { | |
544 | spa_condensing_indirect_t *sci = arg; | |
545 | uint64_t txg = dmu_tx_get_txg(tx); | |
546 | ASSERTV(spa_t *spa = dmu_tx_pool(tx)->dp_spa); | |
547 | ||
548 | ASSERT(dmu_tx_is_syncing(tx)); | |
549 | ASSERT3P(sci, ==, spa->spa_condensing_indirect); | |
550 | ||
551 | vdev_indirect_mapping_add_entries(sci->sci_new_mapping, | |
552 | &sci->sci_new_mapping_entries[txg & TXG_MASK], tx); | |
553 | ASSERT(list_is_empty(&sci->sci_new_mapping_entries[txg & TXG_MASK])); | |
554 | } | |
555 | ||
556 | /* | |
557 | * Open-context function to add one entry to the new mapping. The new | |
558 | * entry will be remembered and written from syncing context. | |
559 | */ | |
560 | static void | |
561 | spa_condense_indirect_commit_entry(spa_t *spa, | |
562 | vdev_indirect_mapping_entry_phys_t *vimep, uint32_t count) | |
563 | { | |
564 | spa_condensing_indirect_t *sci = spa->spa_condensing_indirect; | |
565 | ||
566 | ASSERT3U(count, <, DVA_GET_ASIZE(&vimep->vimep_dst)); | |
567 | ||
568 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
569 | dmu_tx_hold_space(tx, sizeof (*vimep) + sizeof (count)); | |
570 | VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); | |
571 | int txgoff = dmu_tx_get_txg(tx) & TXG_MASK; | |
572 | ||
573 | /* | |
574 | * If we are the first entry committed this txg, kick off the sync | |
575 | * task to write to the MOS on our behalf. | |
576 | */ | |
577 | if (list_is_empty(&sci->sci_new_mapping_entries[txgoff])) { | |
578 | dsl_sync_task_nowait(dmu_tx_pool(tx), | |
579 | spa_condense_indirect_commit_sync, sci, | |
580 | 0, ZFS_SPACE_CHECK_NONE, tx); | |
581 | } | |
582 | ||
583 | vdev_indirect_mapping_entry_t *vime = | |
584 | kmem_alloc(sizeof (*vime), KM_SLEEP); | |
585 | vime->vime_mapping = *vimep; | |
586 | vime->vime_obsolete_count = count; | |
587 | list_insert_tail(&sci->sci_new_mapping_entries[txgoff], vime); | |
588 | ||
589 | dmu_tx_commit(tx); | |
590 | } | |
591 | ||
592 | static void | |
593 | spa_condense_indirect_generate_new_mapping(vdev_t *vd, | |
9d5b5245 | 594 | uint32_t *obsolete_counts, uint64_t start_index, zthr_t *zthr) |
a1d477c2 MA |
595 | { |
596 | spa_t *spa = vd->vdev_spa; | |
597 | uint64_t mapi = start_index; | |
598 | vdev_indirect_mapping_t *old_mapping = vd->vdev_indirect_mapping; | |
599 | uint64_t old_num_entries = | |
600 | vdev_indirect_mapping_num_entries(old_mapping); | |
601 | ||
602 | ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); | |
603 | ASSERT3U(vd->vdev_id, ==, spa->spa_condensing_indirect_phys.scip_vdev); | |
604 | ||
605 | zfs_dbgmsg("starting condense of vdev %llu from index %llu", | |
606 | (u_longlong_t)vd->vdev_id, | |
607 | (u_longlong_t)mapi); | |
608 | ||
9d5b5245 SD |
609 | while (mapi < old_num_entries) { |
610 | ||
611 | if (zthr_iscancelled(zthr)) { | |
612 | zfs_dbgmsg("pausing condense of vdev %llu " | |
613 | "at index %llu", (u_longlong_t)vd->vdev_id, | |
614 | (u_longlong_t)mapi); | |
615 | break; | |
616 | } | |
617 | ||
a1d477c2 MA |
618 | vdev_indirect_mapping_entry_phys_t *entry = |
619 | &old_mapping->vim_entries[mapi]; | |
620 | uint64_t entry_size = DVA_GET_ASIZE(&entry->vimep_dst); | |
621 | ASSERT3U(obsolete_counts[mapi], <=, entry_size); | |
622 | if (obsolete_counts[mapi] < entry_size) { | |
623 | spa_condense_indirect_commit_entry(spa, entry, | |
624 | obsolete_counts[mapi]); | |
625 | ||
626 | /* | |
627 | * This delay may be requested for testing, debugging, | |
628 | * or performance reasons. | |
629 | */ | |
630 | hrtime_t now = gethrtime(); | |
631 | hrtime_t sleep_until = now + MSEC2NSEC( | |
632 | zfs_condense_indirect_commit_entry_delay_ms); | |
633 | zfs_sleep_until(sleep_until); | |
634 | } | |
635 | ||
636 | mapi++; | |
637 | } | |
a1d477c2 MA |
638 | } |
639 | ||
9d5b5245 SD |
640 | /* ARGSUSED */ |
641 | static boolean_t | |
642 | spa_condense_indirect_thread_check(void *arg, zthr_t *zthr) | |
a1d477c2 | 643 | { |
9d5b5245 SD |
644 | spa_t *spa = arg; |
645 | ||
646 | return (spa->spa_condensing_indirect != NULL); | |
647 | } | |
648 | ||
649 | /* ARGSUSED */ | |
650 | static int | |
651 | spa_condense_indirect_thread(void *arg, zthr_t *zthr) | |
652 | { | |
653 | spa_t *spa = arg; | |
654 | vdev_t *vd; | |
655 | ||
656 | ASSERT3P(spa->spa_condensing_indirect, !=, NULL); | |
657 | spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); | |
658 | vd = vdev_lookup_top(spa, spa->spa_condensing_indirect_phys.scip_vdev); | |
659 | ASSERT3P(vd, !=, NULL); | |
660 | spa_config_exit(spa, SCL_VDEV, FTAG); | |
661 | ||
a1d477c2 MA |
662 | spa_condensing_indirect_t *sci = spa->spa_condensing_indirect; |
663 | spa_condensing_indirect_phys_t *scip = | |
664 | &spa->spa_condensing_indirect_phys; | |
665 | uint32_t *counts; | |
666 | uint64_t start_index; | |
667 | vdev_indirect_mapping_t *old_mapping = vd->vdev_indirect_mapping; | |
668 | space_map_t *prev_obsolete_sm = NULL; | |
669 | ||
670 | ASSERT3U(vd->vdev_id, ==, scip->scip_vdev); | |
671 | ASSERT(scip->scip_next_mapping_object != 0); | |
672 | ASSERT(scip->scip_prev_obsolete_sm_object != 0); | |
673 | ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); | |
674 | ||
675 | for (int i = 0; i < TXG_SIZE; i++) { | |
676 | /* | |
677 | * The list must start out empty in order for the | |
678 | * _commit_sync() sync task to be properly registered | |
679 | * on the first call to _commit_entry(); so it's wise | |
680 | * to double check and ensure we actually are starting | |
681 | * with empty lists. | |
682 | */ | |
683 | ASSERT(list_is_empty(&sci->sci_new_mapping_entries[i])); | |
684 | } | |
685 | ||
686 | VERIFY0(space_map_open(&prev_obsolete_sm, spa->spa_meta_objset, | |
687 | scip->scip_prev_obsolete_sm_object, 0, vd->vdev_asize, 0)); | |
688 | space_map_update(prev_obsolete_sm); | |
689 | counts = vdev_indirect_mapping_load_obsolete_counts(old_mapping); | |
690 | if (prev_obsolete_sm != NULL) { | |
691 | vdev_indirect_mapping_load_obsolete_spacemap(old_mapping, | |
692 | counts, prev_obsolete_sm); | |
693 | } | |
694 | space_map_close(prev_obsolete_sm); | |
695 | ||
696 | /* | |
697 | * Generate new mapping. Determine what index to continue from | |
698 | * based on the max offset that we've already written in the | |
699 | * new mapping. | |
700 | */ | |
701 | uint64_t max_offset = | |
702 | vdev_indirect_mapping_max_offset(sci->sci_new_mapping); | |
703 | if (max_offset == 0) { | |
704 | /* We haven't written anything to the new mapping yet. */ | |
705 | start_index = 0; | |
706 | } else { | |
707 | /* | |
708 | * Pick up from where we left off. _entry_for_offset() | |
709 | * returns a pointer into the vim_entries array. If | |
710 | * max_offset is greater than any of the mappings | |
711 | * contained in the table NULL will be returned and | |
712 | * that indicates we've exhausted our iteration of the | |
713 | * old_mapping. | |
714 | */ | |
715 | ||
716 | vdev_indirect_mapping_entry_phys_t *entry = | |
717 | vdev_indirect_mapping_entry_for_offset_or_next(old_mapping, | |
718 | max_offset); | |
719 | ||
720 | if (entry == NULL) { | |
721 | /* | |
722 | * We've already written the whole new mapping. | |
723 | * This special value will cause us to skip the | |
724 | * generate_new_mapping step and just do the sync | |
725 | * task to complete the condense. | |
726 | */ | |
727 | start_index = UINT64_MAX; | |
728 | } else { | |
729 | start_index = entry - old_mapping->vim_entries; | |
730 | ASSERT3U(start_index, <, | |
731 | vdev_indirect_mapping_num_entries(old_mapping)); | |
732 | } | |
733 | } | |
734 | ||
9d5b5245 SD |
735 | spa_condense_indirect_generate_new_mapping(vd, counts, |
736 | start_index, zthr); | |
a1d477c2 MA |
737 | |
738 | vdev_indirect_mapping_free_obsolete_counts(old_mapping, counts); | |
739 | ||
740 | /* | |
9d5b5245 SD |
741 | * If the zthr has received a cancellation signal while running |
742 | * in generate_new_mapping() or at any point after that, then bail | |
743 | * early. We don't want to complete the condense if the spa is | |
744 | * shutting down. | |
a1d477c2 | 745 | */ |
9d5b5245 SD |
746 | if (zthr_iscancelled(zthr)) |
747 | return (0); | |
748 | ||
749 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, | |
d2734cce SD |
750 | spa_condense_indirect_complete_sync, sci, 0, |
751 | ZFS_SPACE_CHECK_EXTRA_RESERVED)); | |
a1d477c2 | 752 | |
9d5b5245 | 753 | return (0); |
a1d477c2 MA |
754 | } |
755 | ||
756 | /* | |
757 | * Sync task to begin the condensing process. | |
758 | */ | |
759 | void | |
760 | spa_condense_indirect_start_sync(vdev_t *vd, dmu_tx_t *tx) | |
761 | { | |
762 | spa_t *spa = vd->vdev_spa; | |
763 | spa_condensing_indirect_phys_t *scip = | |
764 | &spa->spa_condensing_indirect_phys; | |
765 | ||
766 | ASSERT0(scip->scip_next_mapping_object); | |
767 | ASSERT0(scip->scip_prev_obsolete_sm_object); | |
768 | ASSERT0(scip->scip_vdev); | |
769 | ASSERT(dmu_tx_is_syncing(tx)); | |
770 | ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); | |
771 | ASSERT(spa_feature_is_active(spa, SPA_FEATURE_OBSOLETE_COUNTS)); | |
772 | ASSERT(vdev_indirect_mapping_num_entries(vd->vdev_indirect_mapping)); | |
773 | ||
27f80e85 BB |
774 | uint64_t obsolete_sm_obj; |
775 | VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_obj)); | |
776 | ASSERT3U(obsolete_sm_obj, !=, 0); | |
a1d477c2 MA |
777 | |
778 | scip->scip_vdev = vd->vdev_id; | |
779 | scip->scip_next_mapping_object = | |
780 | vdev_indirect_mapping_alloc(spa->spa_meta_objset, tx); | |
781 | ||
782 | scip->scip_prev_obsolete_sm_object = obsolete_sm_obj; | |
783 | ||
784 | /* | |
785 | * We don't need to allocate a new space map object, since | |
786 | * vdev_indirect_sync_obsolete will allocate one when needed. | |
787 | */ | |
788 | space_map_close(vd->vdev_obsolete_sm); | |
789 | vd->vdev_obsolete_sm = NULL; | |
790 | VERIFY0(zap_remove(spa->spa_meta_objset, vd->vdev_top_zap, | |
791 | VDEV_TOP_ZAP_INDIRECT_OBSOLETE_SM, tx)); | |
792 | ||
793 | VERIFY0(zap_add(spa->spa_dsl_pool->dp_meta_objset, | |
794 | DMU_POOL_DIRECTORY_OBJECT, | |
795 | DMU_POOL_CONDENSING_INDIRECT, sizeof (uint64_t), | |
796 | sizeof (*scip) / sizeof (uint64_t), scip, tx)); | |
797 | ||
798 | ASSERT3P(spa->spa_condensing_indirect, ==, NULL); | |
799 | spa->spa_condensing_indirect = spa_condensing_indirect_create(spa); | |
800 | ||
801 | zfs_dbgmsg("starting condense of vdev %llu in txg %llu: " | |
802 | "posm=%llu nm=%llu", | |
803 | vd->vdev_id, dmu_tx_get_txg(tx), | |
804 | (u_longlong_t)scip->scip_prev_obsolete_sm_object, | |
805 | (u_longlong_t)scip->scip_next_mapping_object); | |
806 | ||
9d5b5245 | 807 | zthr_wakeup(spa->spa_condense_zthr); |
a1d477c2 MA |
808 | } |
809 | ||
810 | /* | |
811 | * Sync to the given vdev's obsolete space map any segments that are no longer | |
812 | * referenced as of the given txg. | |
813 | * | |
814 | * If the obsolete space map doesn't exist yet, create and open it. | |
815 | */ | |
816 | void | |
817 | vdev_indirect_sync_obsolete(vdev_t *vd, dmu_tx_t *tx) | |
818 | { | |
819 | spa_t *spa = vd->vdev_spa; | |
820 | ASSERTV(vdev_indirect_config_t *vic = &vd->vdev_indirect_config); | |
821 | ||
822 | ASSERT3U(vic->vic_mapping_object, !=, 0); | |
823 | ASSERT(range_tree_space(vd->vdev_obsolete_segments) > 0); | |
824 | ASSERT(vd->vdev_removing || vd->vdev_ops == &vdev_indirect_ops); | |
825 | ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS)); | |
826 | ||
27f80e85 BB |
827 | uint64_t obsolete_sm_object; |
828 | VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); | |
829 | if (obsolete_sm_object == 0) { | |
830 | obsolete_sm_object = space_map_alloc(spa->spa_meta_objset, | |
d2734cce | 831 | vdev_standard_sm_blksz, tx); |
a1d477c2 MA |
832 | |
833 | ASSERT(vd->vdev_top_zap != 0); | |
834 | VERIFY0(zap_add(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap, | |
835 | VDEV_TOP_ZAP_INDIRECT_OBSOLETE_SM, | |
836 | sizeof (obsolete_sm_object), 1, &obsolete_sm_object, tx)); | |
27f80e85 BB |
837 | ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); |
838 | ASSERT3U(obsolete_sm_object, !=, 0); | |
a1d477c2 MA |
839 | |
840 | spa_feature_incr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); | |
841 | VERIFY0(space_map_open(&vd->vdev_obsolete_sm, | |
842 | spa->spa_meta_objset, obsolete_sm_object, | |
843 | 0, vd->vdev_asize, 0)); | |
844 | space_map_update(vd->vdev_obsolete_sm); | |
845 | } | |
846 | ||
847 | ASSERT(vd->vdev_obsolete_sm != NULL); | |
27f80e85 | 848 | ASSERT3U(obsolete_sm_object, ==, |
a1d477c2 MA |
849 | space_map_object(vd->vdev_obsolete_sm)); |
850 | ||
851 | space_map_write(vd->vdev_obsolete_sm, | |
4d044c4c | 852 | vd->vdev_obsolete_segments, SM_ALLOC, SM_NO_VDEVID, tx); |
a1d477c2 MA |
853 | space_map_update(vd->vdev_obsolete_sm); |
854 | range_tree_vacate(vd->vdev_obsolete_segments, NULL, NULL); | |
855 | } | |
856 | ||
857 | int | |
858 | spa_condense_init(spa_t *spa) | |
859 | { | |
860 | int error = zap_lookup(spa->spa_meta_objset, | |
861 | DMU_POOL_DIRECTORY_OBJECT, | |
862 | DMU_POOL_CONDENSING_INDIRECT, sizeof (uint64_t), | |
863 | sizeof (spa->spa_condensing_indirect_phys) / sizeof (uint64_t), | |
864 | &spa->spa_condensing_indirect_phys); | |
865 | if (error == 0) { | |
866 | if (spa_writeable(spa)) { | |
867 | spa->spa_condensing_indirect = | |
868 | spa_condensing_indirect_create(spa); | |
869 | } | |
870 | return (0); | |
871 | } else if (error == ENOENT) { | |
872 | return (0); | |
873 | } else { | |
874 | return (error); | |
875 | } | |
876 | } | |
877 | ||
878 | void | |
879 | spa_condense_fini(spa_t *spa) | |
880 | { | |
881 | if (spa->spa_condensing_indirect != NULL) { | |
882 | spa_condensing_indirect_destroy(spa->spa_condensing_indirect); | |
883 | spa->spa_condensing_indirect = NULL; | |
884 | } | |
885 | } | |
886 | ||
a1d477c2 | 887 | void |
9d5b5245 | 888 | spa_start_indirect_condensing_thread(spa_t *spa) |
a1d477c2 | 889 | { |
9d5b5245 SD |
890 | ASSERT3P(spa->spa_condense_zthr, ==, NULL); |
891 | spa->spa_condense_zthr = zthr_create(spa_condense_indirect_thread_check, | |
892 | spa_condense_indirect_thread, spa); | |
a1d477c2 MA |
893 | } |
894 | ||
895 | /* | |
27f80e85 BB |
896 | * Gets the obsolete spacemap object from the vdev's ZAP. On success sm_obj |
897 | * will contain either the obsolete spacemap object or zero if none exists. | |
898 | * All other errors are returned to the caller. | |
a1d477c2 MA |
899 | */ |
900 | int | |
27f80e85 | 901 | vdev_obsolete_sm_object(vdev_t *vd, uint64_t *sm_obj) |
a1d477c2 MA |
902 | { |
903 | ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); | |
27f80e85 | 904 | |
a1d477c2 | 905 | if (vd->vdev_top_zap == 0) { |
27f80e85 | 906 | *sm_obj = 0; |
a1d477c2 MA |
907 | return (0); |
908 | } | |
909 | ||
27f80e85 BB |
910 | int error = zap_lookup(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap, |
911 | VDEV_TOP_ZAP_INDIRECT_OBSOLETE_SM, sizeof (sm_obj), 1, sm_obj); | |
912 | if (error == ENOENT) { | |
913 | *sm_obj = 0; | |
914 | error = 0; | |
915 | } | |
a1d477c2 | 916 | |
27f80e85 | 917 | return (error); |
a1d477c2 MA |
918 | } |
919 | ||
27f80e85 BB |
920 | /* |
921 | * Gets the obsolete count are precise spacemap object from the vdev's ZAP. | |
922 | * On success are_precise will be set to reflect if the counts are precise. | |
923 | * All other errors are returned to the caller. | |
924 | */ | |
925 | int | |
926 | vdev_obsolete_counts_are_precise(vdev_t *vd, boolean_t *are_precise) | |
a1d477c2 MA |
927 | { |
928 | ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); | |
27f80e85 | 929 | |
a1d477c2 | 930 | if (vd->vdev_top_zap == 0) { |
27f80e85 BB |
931 | *are_precise = B_FALSE; |
932 | return (0); | |
a1d477c2 MA |
933 | } |
934 | ||
935 | uint64_t val = 0; | |
27f80e85 | 936 | int error = zap_lookup(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap, |
a1d477c2 | 937 | VDEV_TOP_ZAP_OBSOLETE_COUNTS_ARE_PRECISE, sizeof (val), 1, &val); |
27f80e85 BB |
938 | if (error == 0) { |
939 | *are_precise = (val != 0); | |
940 | } else if (error == ENOENT) { | |
941 | *are_precise = B_FALSE; | |
942 | error = 0; | |
943 | } | |
a1d477c2 | 944 | |
27f80e85 | 945 | return (error); |
a1d477c2 MA |
946 | } |
947 | ||
948 | /* ARGSUSED */ | |
949 | static void | |
950 | vdev_indirect_close(vdev_t *vd) | |
951 | { | |
952 | } | |
953 | ||
a1d477c2 MA |
954 | /* ARGSUSED */ |
955 | static int | |
956 | vdev_indirect_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize, | |
957 | uint64_t *ashift) | |
958 | { | |
959 | *psize = *max_psize = vd->vdev_asize + | |
960 | VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; | |
961 | *ashift = vd->vdev_ashift; | |
962 | return (0); | |
963 | } | |
964 | ||
965 | typedef struct remap_segment { | |
966 | vdev_t *rs_vd; | |
967 | uint64_t rs_offset; | |
968 | uint64_t rs_asize; | |
969 | uint64_t rs_split_offset; | |
970 | list_node_t rs_node; | |
971 | } remap_segment_t; | |
972 | ||
973 | remap_segment_t * | |
974 | rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset) | |
975 | { | |
976 | remap_segment_t *rs = kmem_alloc(sizeof (remap_segment_t), KM_SLEEP); | |
977 | rs->rs_vd = vd; | |
978 | rs->rs_offset = offset; | |
979 | rs->rs_asize = asize; | |
980 | rs->rs_split_offset = split_offset; | |
981 | return (rs); | |
982 | } | |
983 | ||
4bf8108e SD |
984 | /* |
985 | * Given an indirect vdev and an extent on that vdev, it duplicates the | |
986 | * physical entries of the indirect mapping that correspond to the extent | |
987 | * to a new array and returns a pointer to it. In addition, copied_entries | |
988 | * is populated with the number of mapping entries that were duplicated. | |
989 | * | |
990 | * Note that the function assumes that the caller holds vdev_indirect_rwlock. | |
991 | * This ensures that the mapping won't change due to condensing as we | |
992 | * copy over its contents. | |
993 | * | |
994 | * Finally, since we are doing an allocation, it is up to the caller to | |
995 | * free the array allocated in this function. | |
996 | */ | |
997 | vdev_indirect_mapping_entry_phys_t * | |
998 | vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset, | |
999 | uint64_t asize, uint64_t *copied_entries) | |
1000 | { | |
1001 | vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL; | |
1002 | vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; | |
1003 | uint64_t entries = 0; | |
1004 | ||
1005 | ASSERT(RW_READ_HELD(&vd->vdev_indirect_rwlock)); | |
1006 | ||
1007 | vdev_indirect_mapping_entry_phys_t *first_mapping = | |
1008 | vdev_indirect_mapping_entry_for_offset(vim, offset); | |
1009 | ASSERT3P(first_mapping, !=, NULL); | |
1010 | ||
1011 | vdev_indirect_mapping_entry_phys_t *m = first_mapping; | |
1012 | while (asize > 0) { | |
1013 | uint64_t size = DVA_GET_ASIZE(&m->vimep_dst); | |
1014 | ||
1015 | ASSERT3U(offset, >=, DVA_MAPPING_GET_SRC_OFFSET(m)); | |
1016 | ASSERT3U(offset, <, DVA_MAPPING_GET_SRC_OFFSET(m) + size); | |
1017 | ||
1018 | uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m); | |
1019 | uint64_t inner_size = MIN(asize, size - inner_offset); | |
1020 | ||
1021 | offset += inner_size; | |
1022 | asize -= inner_size; | |
1023 | entries++; | |
1024 | m++; | |
1025 | } | |
1026 | ||
1027 | size_t copy_length = entries * sizeof (*first_mapping); | |
1028 | duplicate_mappings = kmem_alloc(copy_length, KM_SLEEP); | |
1029 | bcopy(first_mapping, duplicate_mappings, copy_length); | |
1030 | *copied_entries = entries; | |
1031 | ||
1032 | return (duplicate_mappings); | |
1033 | } | |
1034 | ||
a1d477c2 MA |
1035 | /* |
1036 | * Goes through the relevant indirect mappings until it hits a concrete vdev | |
1037 | * and issues the callback. On the way to the concrete vdev, if any other | |
1038 | * indirect vdevs are encountered, then the callback will also be called on | |
1039 | * each of those indirect vdevs. For example, if the segment is mapped to | |
1040 | * segment A on indirect vdev 1, and then segment A on indirect vdev 1 is | |
1041 | * mapped to segment B on concrete vdev 2, then the callback will be called on | |
1042 | * both vdev 1 and vdev 2. | |
1043 | * | |
1044 | * While the callback passed to vdev_indirect_remap() is called on every vdev | |
1045 | * the function encounters, certain callbacks only care about concrete vdevs. | |
1046 | * These types of callbacks should return immediately and explicitly when they | |
1047 | * are called on an indirect vdev. | |
1048 | * | |
1049 | * Because there is a possibility that a DVA section in the indirect device | |
1050 | * has been split into multiple sections in our mapping, we keep track | |
1051 | * of the relevant contiguous segments of the new location (remap_segment_t) | |
1052 | * in a stack. This way we can call the callback for each of the new sections | |
1053 | * created by a single section of the indirect device. Note though, that in | |
1054 | * this scenario the callbacks in each split block won't occur in-order in | |
1055 | * terms of offset, so callers should not make any assumptions about that. | |
1056 | * | |
1057 | * For callbacks that don't handle split blocks and immediately return when | |
1058 | * they encounter them (as is the case for remap_blkptr_cb), the caller can | |
1059 | * assume that its callback will be applied from the first indirect vdev | |
1060 | * encountered to the last one and then the concrete vdev, in that order. | |
1061 | */ | |
1062 | static void | |
1063 | vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, | |
1064 | void (*func)(uint64_t, vdev_t *, uint64_t, uint64_t, void *), void *arg) | |
1065 | { | |
1066 | list_t stack; | |
1067 | spa_t *spa = vd->vdev_spa; | |
1068 | ||
1069 | list_create(&stack, sizeof (remap_segment_t), | |
1070 | offsetof(remap_segment_t, rs_node)); | |
1071 | ||
1072 | for (remap_segment_t *rs = rs_alloc(vd, offset, asize, 0); | |
1073 | rs != NULL; rs = list_remove_head(&stack)) { | |
1074 | vdev_t *v = rs->rs_vd; | |
4bf8108e SD |
1075 | uint64_t num_entries = 0; |
1076 | ||
1077 | ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); | |
1078 | ASSERT(rs->rs_asize > 0); | |
a1d477c2 MA |
1079 | |
1080 | /* | |
4bf8108e SD |
1081 | * Note: As this function can be called from open context |
1082 | * (e.g. zio_read()), we need the following rwlock to | |
1083 | * prevent the mapping from being changed by condensing. | |
1084 | * | |
1085 | * So we grab the lock and we make a copy of the entries | |
1086 | * that are relevant to the extent that we are working on. | |
1087 | * Once that is done, we drop the lock and iterate over | |
1088 | * our copy of the mapping. Once we are done with the with | |
1089 | * the remap segment and we free it, we also free our copy | |
1090 | * of the indirect mapping entries that are relevant to it. | |
1091 | * | |
1092 | * This way we don't need to wait until the function is | |
1093 | * finished with a segment, to condense it. In addition, we | |
1094 | * don't need a recursive rwlock for the case that a call to | |
1095 | * vdev_indirect_remap() needs to call itself (through the | |
1096 | * codepath of its callback) for the same vdev in the middle | |
1097 | * of its execution. | |
a1d477c2 MA |
1098 | */ |
1099 | rw_enter(&v->vdev_indirect_rwlock, RW_READER); | |
4bf8108e | 1100 | ASSERT3P(v->vdev_indirect_mapping, !=, NULL); |
a1d477c2 MA |
1101 | |
1102 | vdev_indirect_mapping_entry_phys_t *mapping = | |
4bf8108e SD |
1103 | vdev_indirect_mapping_duplicate_adjacent_entries(v, |
1104 | rs->rs_offset, rs->rs_asize, &num_entries); | |
a1d477c2 | 1105 | ASSERT3P(mapping, !=, NULL); |
4bf8108e SD |
1106 | ASSERT3U(num_entries, >, 0); |
1107 | rw_exit(&v->vdev_indirect_rwlock); | |
a1d477c2 | 1108 | |
4bf8108e | 1109 | for (uint64_t i = 0; i < num_entries; i++) { |
a1d477c2 MA |
1110 | /* |
1111 | * Note: the vdev_indirect_mapping can not change | |
1112 | * while we are running. It only changes while the | |
1113 | * removal is in progress, and then only from syncing | |
1114 | * context. While a removal is in progress, this | |
1115 | * function is only called for frees, which also only | |
1116 | * happen from syncing context. | |
1117 | */ | |
4bf8108e SD |
1118 | vdev_indirect_mapping_entry_phys_t *m = &mapping[i]; |
1119 | ||
1120 | ASSERT3P(m, !=, NULL); | |
1121 | ASSERT3U(rs->rs_asize, >, 0); | |
a1d477c2 | 1122 | |
4bf8108e SD |
1123 | uint64_t size = DVA_GET_ASIZE(&m->vimep_dst); |
1124 | uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst); | |
1125 | uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst); | |
a1d477c2 MA |
1126 | |
1127 | ASSERT3U(rs->rs_offset, >=, | |
4bf8108e | 1128 | DVA_MAPPING_GET_SRC_OFFSET(m)); |
a1d477c2 | 1129 | ASSERT3U(rs->rs_offset, <, |
4bf8108e | 1130 | DVA_MAPPING_GET_SRC_OFFSET(m) + size); |
a1d477c2 MA |
1131 | ASSERT3U(dst_vdev, !=, v->vdev_id); |
1132 | ||
1133 | uint64_t inner_offset = rs->rs_offset - | |
4bf8108e | 1134 | DVA_MAPPING_GET_SRC_OFFSET(m); |
a1d477c2 MA |
1135 | uint64_t inner_size = |
1136 | MIN(rs->rs_asize, size - inner_offset); | |
1137 | ||
1138 | vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev); | |
1139 | ASSERT3P(dst_v, !=, NULL); | |
1140 | ||
1141 | if (dst_v->vdev_ops == &vdev_indirect_ops) { | |
1142 | list_insert_head(&stack, | |
1143 | rs_alloc(dst_v, dst_offset + inner_offset, | |
1144 | inner_size, rs->rs_split_offset)); | |
1145 | ||
1146 | } | |
1147 | ||
1148 | if ((zfs_flags & ZFS_DEBUG_INDIRECT_REMAP) && | |
1149 | IS_P2ALIGNED(inner_size, 2 * SPA_MINBLOCKSIZE)) { | |
1150 | /* | |
1151 | * Note: This clause exists only solely for | |
1152 | * testing purposes. We use it to ensure that | |
1153 | * split blocks work and that the callbacks | |
1154 | * using them yield the same result if issued | |
1155 | * in reverse order. | |
1156 | */ | |
1157 | uint64_t inner_half = inner_size / 2; | |
1158 | ||
1159 | func(rs->rs_split_offset + inner_half, dst_v, | |
1160 | dst_offset + inner_offset + inner_half, | |
1161 | inner_half, arg); | |
1162 | ||
1163 | func(rs->rs_split_offset, dst_v, | |
1164 | dst_offset + inner_offset, | |
1165 | inner_half, arg); | |
1166 | } else { | |
1167 | func(rs->rs_split_offset, dst_v, | |
1168 | dst_offset + inner_offset, | |
1169 | inner_size, arg); | |
1170 | } | |
1171 | ||
1172 | rs->rs_offset += inner_size; | |
1173 | rs->rs_asize -= inner_size; | |
1174 | rs->rs_split_offset += inner_size; | |
a1d477c2 | 1175 | } |
4bf8108e | 1176 | VERIFY0(rs->rs_asize); |
a1d477c2 | 1177 | |
4bf8108e | 1178 | kmem_free(mapping, num_entries * sizeof (*mapping)); |
a1d477c2 MA |
1179 | kmem_free(rs, sizeof (remap_segment_t)); |
1180 | } | |
1181 | list_destroy(&stack); | |
1182 | } | |
1183 | ||
1184 | static void | |
1185 | vdev_indirect_child_io_done(zio_t *zio) | |
1186 | { | |
1187 | zio_t *pio = zio->io_private; | |
1188 | ||
1189 | mutex_enter(&pio->io_lock); | |
1190 | pio->io_error = zio_worst_error(pio->io_error, zio->io_error); | |
1191 | mutex_exit(&pio->io_lock); | |
1192 | ||
1193 | abd_put(zio->io_abd); | |
1194 | } | |
1195 | ||
9e052db4 MA |
1196 | /* |
1197 | * This is a callback for vdev_indirect_remap() which allocates an | |
1198 | * indirect_split_t for each split segment and adds it to iv_splits. | |
1199 | */ | |
a1d477c2 | 1200 | static void |
9e052db4 | 1201 | vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset, |
a1d477c2 MA |
1202 | uint64_t size, void *arg) |
1203 | { | |
1204 | zio_t *zio = arg; | |
9e052db4 | 1205 | indirect_vsd_t *iv = zio->io_vsd; |
a1d477c2 MA |
1206 | |
1207 | ASSERT3P(vd, !=, NULL); | |
1208 | ||
1209 | if (vd->vdev_ops == &vdev_indirect_ops) | |
1210 | return; | |
1211 | ||
9e052db4 MA |
1212 | int n = 1; |
1213 | if (vd->vdev_ops == &vdev_mirror_ops) | |
1214 | n = vd->vdev_children; | |
1215 | ||
1216 | indirect_split_t *is = | |
1217 | kmem_zalloc(offsetof(indirect_split_t, is_child[n]), KM_SLEEP); | |
1218 | ||
1219 | is->is_children = n; | |
1220 | is->is_size = size; | |
1221 | is->is_split_offset = split_offset; | |
1222 | is->is_target_offset = offset; | |
1223 | is->is_vdev = vd; | |
1258bd77 BB |
1224 | list_create(&is->is_unique_child, sizeof (indirect_child_t), |
1225 | offsetof(indirect_child_t, ic_node)); | |
9e052db4 MA |
1226 | |
1227 | /* | |
1228 | * Note that we only consider multiple copies of the data for | |
1229 | * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even | |
1230 | * though they use the same ops as mirror, because there's only one | |
1231 | * "good" copy under the replacing/spare. | |
1232 | */ | |
1233 | if (vd->vdev_ops == &vdev_mirror_ops) { | |
1234 | for (int i = 0; i < n; i++) { | |
1235 | is->is_child[i].ic_vdev = vd->vdev_child[i]; | |
1258bd77 | 1236 | list_link_init(&is->is_child[i].ic_node); |
9e052db4 MA |
1237 | } |
1238 | } else { | |
1239 | is->is_child[0].ic_vdev = vd; | |
1240 | } | |
1241 | ||
1242 | list_insert_tail(&iv->iv_splits, is); | |
1243 | } | |
1244 | ||
1245 | static void | |
1246 | vdev_indirect_read_split_done(zio_t *zio) | |
1247 | { | |
1248 | indirect_child_t *ic = zio->io_private; | |
1249 | ||
1250 | if (zio->io_error != 0) { | |
1251 | /* | |
1252 | * Clear ic_data to indicate that we do not have data for this | |
1253 | * child. | |
1254 | */ | |
1255 | abd_free(ic->ic_data); | |
1256 | ic->ic_data = NULL; | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | /* | |
1261 | * Issue reads for all copies (mirror children) of all splits. | |
1262 | */ | |
1263 | static void | |
1264 | vdev_indirect_read_all(zio_t *zio) | |
1265 | { | |
1266 | indirect_vsd_t *iv = zio->io_vsd; | |
1267 | ||
5aa95ba0 TC |
1268 | ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ); |
1269 | ||
9e052db4 MA |
1270 | for (indirect_split_t *is = list_head(&iv->iv_splits); |
1271 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1272 | for (int i = 0; i < is->is_children; i++) { | |
1273 | indirect_child_t *ic = &is->is_child[i]; | |
1274 | ||
1275 | if (!vdev_readable(ic->ic_vdev)) | |
1276 | continue; | |
1277 | ||
1278 | /* | |
1279 | * Note, we may read from a child whose DTL | |
1280 | * indicates that the data may not be present here. | |
1281 | * While this might result in a few i/os that will | |
1282 | * likely return incorrect data, it simplifies the | |
1283 | * code since we can treat scrub and resilver | |
1284 | * identically. (The incorrect data will be | |
1285 | * detected and ignored when we verify the | |
1286 | * checksum.) | |
1287 | */ | |
1288 | ||
1289 | ic->ic_data = abd_alloc_sametype(zio->io_abd, | |
1290 | is->is_size); | |
1258bd77 | 1291 | ic->ic_duplicate = NULL; |
9e052db4 MA |
1292 | |
1293 | zio_nowait(zio_vdev_child_io(zio, NULL, | |
1294 | ic->ic_vdev, is->is_target_offset, ic->ic_data, | |
1295 | is->is_size, zio->io_type, zio->io_priority, 0, | |
1296 | vdev_indirect_read_split_done, ic)); | |
1297 | } | |
1298 | } | |
1299 | iv->iv_reconstruct = B_TRUE; | |
a1d477c2 MA |
1300 | } |
1301 | ||
1302 | static void | |
1303 | vdev_indirect_io_start(zio_t *zio) | |
1304 | { | |
1305 | ASSERTV(spa_t *spa = zio->io_spa); | |
9e052db4 MA |
1306 | indirect_vsd_t *iv = kmem_zalloc(sizeof (*iv), KM_SLEEP); |
1307 | list_create(&iv->iv_splits, | |
1308 | sizeof (indirect_split_t), offsetof(indirect_split_t, is_node)); | |
1309 | ||
1310 | zio->io_vsd = iv; | |
1311 | zio->io_vsd_ops = &vdev_indirect_vsd_ops; | |
a1d477c2 MA |
1312 | |
1313 | ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); | |
1314 | if (zio->io_type != ZIO_TYPE_READ) { | |
1315 | ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE); | |
9e052db4 MA |
1316 | /* |
1317 | * Note: this code can handle other kinds of writes, | |
1318 | * but we don't expect them. | |
1319 | */ | |
1320 | ASSERT((zio->io_flags & (ZIO_FLAG_SELF_HEAL | | |
1321 | ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE)) != 0); | |
a1d477c2 MA |
1322 | } |
1323 | ||
1324 | vdev_indirect_remap(zio->io_vd, zio->io_offset, zio->io_size, | |
9e052db4 MA |
1325 | vdev_indirect_gather_splits, zio); |
1326 | ||
1327 | indirect_split_t *first = list_head(&iv->iv_splits); | |
1328 | if (first->is_size == zio->io_size) { | |
1329 | /* | |
1330 | * This is not a split block; we are pointing to the entire | |
1331 | * data, which will checksum the same as the original data. | |
1332 | * Pass the BP down so that the child i/o can verify the | |
1333 | * checksum, and try a different location if available | |
1334 | * (e.g. on a mirror). | |
1335 | * | |
1336 | * While this special case could be handled the same as the | |
1337 | * general (split block) case, doing it this way ensures | |
1338 | * that the vast majority of blocks on indirect vdevs | |
1339 | * (which are not split) are handled identically to blocks | |
1340 | * on non-indirect vdevs. This allows us to be less strict | |
1341 | * about performance in the general (but rare) case. | |
1342 | */ | |
1343 | ASSERT0(first->is_split_offset); | |
1344 | ASSERT3P(list_next(&iv->iv_splits, first), ==, NULL); | |
1345 | zio_nowait(zio_vdev_child_io(zio, zio->io_bp, | |
1346 | first->is_vdev, first->is_target_offset, | |
1347 | abd_get_offset(zio->io_abd, 0), | |
1348 | zio->io_size, zio->io_type, zio->io_priority, 0, | |
1349 | vdev_indirect_child_io_done, zio)); | |
1350 | } else { | |
1351 | iv->iv_split_block = B_TRUE; | |
5aa95ba0 TC |
1352 | if (zio->io_type == ZIO_TYPE_READ && |
1353 | zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER)) { | |
9e052db4 MA |
1354 | /* |
1355 | * Read all copies. Note that for simplicity, | |
1356 | * we don't bother consulting the DTL in the | |
1357 | * resilver case. | |
1358 | */ | |
1359 | vdev_indirect_read_all(zio); | |
1360 | } else { | |
1361 | /* | |
5aa95ba0 TC |
1362 | * If this is a read zio, we read one copy of each |
1363 | * split segment, from the top-level vdev. Since | |
1364 | * we don't know the checksum of each split | |
1365 | * individually, the child zio can't ensure that | |
1366 | * we get the right data. E.g. if it's a mirror, | |
1367 | * it will just read from a random (healthy) leaf | |
1368 | * vdev. We have to verify the checksum in | |
1369 | * vdev_indirect_io_done(). | |
1370 | * | |
1371 | * For write zios, the vdev code will ensure we write | |
1372 | * to all children. | |
9e052db4 MA |
1373 | */ |
1374 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1375 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1376 | zio_nowait(zio_vdev_child_io(zio, NULL, | |
1377 | is->is_vdev, is->is_target_offset, | |
1378 | abd_get_offset(zio->io_abd, | |
1379 | is->is_split_offset), is->is_size, | |
1380 | zio->io_type, zio->io_priority, 0, | |
1381 | vdev_indirect_child_io_done, zio)); | |
1382 | } | |
1383 | ||
1384 | } | |
1385 | } | |
a1d477c2 MA |
1386 | |
1387 | zio_execute(zio); | |
1388 | } | |
1389 | ||
9e052db4 MA |
1390 | /* |
1391 | * Report a checksum error for a child. | |
1392 | */ | |
1393 | static void | |
1394 | vdev_indirect_checksum_error(zio_t *zio, | |
1395 | indirect_split_t *is, indirect_child_t *ic) | |
1396 | { | |
1397 | vdev_t *vd = ic->ic_vdev; | |
1398 | ||
1399 | if (zio->io_flags & ZIO_FLAG_SPECULATIVE) | |
1400 | return; | |
1401 | ||
1402 | mutex_enter(&vd->vdev_stat_lock); | |
1403 | vd->vdev_stat.vs_checksum_errors++; | |
1404 | mutex_exit(&vd->vdev_stat_lock); | |
1405 | ||
1406 | zio_bad_cksum_t zbc = {{{ 0 }}}; | |
1407 | abd_t *bad_abd = ic->ic_data; | |
1258bd77 | 1408 | abd_t *good_abd = is->is_good_child->ic_data; |
9e052db4 MA |
1409 | zfs_ereport_post_checksum(zio->io_spa, vd, NULL, zio, |
1410 | is->is_target_offset, is->is_size, good_abd, bad_abd, &zbc); | |
1411 | } | |
1412 | ||
1413 | /* | |
1414 | * Issue repair i/os for any incorrect copies. We do this by comparing | |
1415 | * each split segment's correct data (is_good_child's ic_data) with each | |
1416 | * other copy of the data. If they differ, then we overwrite the bad data | |
1417 | * with the good copy. Note that we do this without regard for the DTL's, | |
1418 | * which simplifies this code and also issues the optimal number of writes | |
1419 | * (based on which copies actually read bad data, as opposed to which we | |
1420 | * think might be wrong). For the same reason, we always use | |
1421 | * ZIO_FLAG_SELF_HEAL, to bypass the DTL check in zio_vdev_io_start(). | |
1422 | */ | |
1423 | static void | |
1424 | vdev_indirect_repair(zio_t *zio) | |
1425 | { | |
1426 | indirect_vsd_t *iv = zio->io_vsd; | |
1427 | ||
1428 | enum zio_flag flags = ZIO_FLAG_IO_REPAIR; | |
1429 | ||
1430 | if (!(zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) | |
1431 | flags |= ZIO_FLAG_SELF_HEAL; | |
1432 | ||
1433 | if (!spa_writeable(zio->io_spa)) | |
1434 | return; | |
1435 | ||
1436 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1437 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
9e052db4 MA |
1438 | for (int c = 0; c < is->is_children; c++) { |
1439 | indirect_child_t *ic = &is->is_child[c]; | |
1258bd77 | 1440 | if (ic == is->is_good_child) |
9e052db4 MA |
1441 | continue; |
1442 | if (ic->ic_data == NULL) | |
1443 | continue; | |
4589f3ae | 1444 | if (ic->ic_duplicate == is->is_good_child) |
9e052db4 MA |
1445 | continue; |
1446 | ||
1447 | zio_nowait(zio_vdev_child_io(zio, NULL, | |
1448 | ic->ic_vdev, is->is_target_offset, | |
1258bd77 | 1449 | is->is_good_child->ic_data, is->is_size, |
9e052db4 MA |
1450 | ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE, |
1451 | ZIO_FLAG_IO_REPAIR | ZIO_FLAG_SELF_HEAL, | |
1452 | NULL, NULL)); | |
1453 | ||
1454 | vdev_indirect_checksum_error(zio, is, ic); | |
1455 | } | |
1456 | } | |
1457 | } | |
1458 | ||
1459 | /* | |
1460 | * Report checksum errors on all children that we read from. | |
1461 | */ | |
1462 | static void | |
1463 | vdev_indirect_all_checksum_errors(zio_t *zio) | |
1464 | { | |
1465 | indirect_vsd_t *iv = zio->io_vsd; | |
1466 | ||
1467 | if (zio->io_flags & ZIO_FLAG_SPECULATIVE) | |
1468 | return; | |
1469 | ||
1470 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1471 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1472 | for (int c = 0; c < is->is_children; c++) { | |
1473 | indirect_child_t *ic = &is->is_child[c]; | |
1474 | ||
1475 | if (ic->ic_data == NULL) | |
1476 | continue; | |
1477 | ||
1478 | vdev_t *vd = ic->ic_vdev; | |
1479 | ||
1480 | mutex_enter(&vd->vdev_stat_lock); | |
1481 | vd->vdev_stat.vs_checksum_errors++; | |
1482 | mutex_exit(&vd->vdev_stat_lock); | |
1483 | ||
1484 | zfs_ereport_post_checksum(zio->io_spa, vd, NULL, zio, | |
1485 | is->is_target_offset, is->is_size, | |
1486 | NULL, NULL, NULL); | |
1487 | } | |
1488 | } | |
1489 | } | |
1490 | ||
1258bd77 BB |
1491 | /* |
1492 | * Copy data from all the splits to a main zio then validate the checksum. | |
1493 | * If then checksum is successfully validated return success. | |
1494 | */ | |
1495 | static int | |
1496 | vdev_indirect_splits_checksum_validate(indirect_vsd_t *iv, zio_t *zio) | |
1497 | { | |
1498 | zio_bad_cksum_t zbc; | |
1499 | ||
1500 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1501 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1502 | ||
1503 | ASSERT3P(is->is_good_child->ic_data, !=, NULL); | |
1504 | ASSERT3P(is->is_good_child->ic_duplicate, ==, NULL); | |
1505 | ||
1506 | abd_copy_off(zio->io_abd, is->is_good_child->ic_data, | |
1507 | is->is_split_offset, 0, is->is_size); | |
1508 | } | |
1509 | ||
1510 | return (zio_checksum_error(zio, &zbc)); | |
1511 | } | |
1512 | ||
1513 | /* | |
1514 | * There are relatively few possible combinations making it feasible to | |
1515 | * deterministically check them all. We do this by setting the good_child | |
1516 | * to the next unique split version. If we reach the end of the list then | |
1517 | * "carry over" to the next unique split version (like counting in base | |
1518 | * is_unique_children, but each digit can have a different base). | |
1519 | */ | |
1520 | static int | |
1521 | vdev_indirect_splits_enumerate_all(indirect_vsd_t *iv, zio_t *zio) | |
1522 | { | |
1523 | boolean_t more = B_TRUE; | |
1524 | ||
1525 | iv->iv_attempts = 0; | |
1526 | ||
1527 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1528 | is != NULL; is = list_next(&iv->iv_splits, is)) | |
1529 | is->is_good_child = list_head(&is->is_unique_child); | |
1530 | ||
1531 | while (more == B_TRUE) { | |
1532 | iv->iv_attempts++; | |
1533 | more = B_FALSE; | |
1534 | ||
1535 | if (vdev_indirect_splits_checksum_validate(iv, zio) == 0) | |
1536 | return (0); | |
1537 | ||
1538 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1539 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1540 | is->is_good_child = list_next(&is->is_unique_child, | |
1541 | is->is_good_child); | |
1542 | if (is->is_good_child != NULL) { | |
1543 | more = B_TRUE; | |
1544 | break; | |
1545 | } | |
1546 | ||
1547 | is->is_good_child = list_head(&is->is_unique_child); | |
1548 | } | |
1549 | } | |
1550 | ||
1551 | ASSERT3S(iv->iv_attempts, <=, iv->iv_unique_combinations); | |
1552 | ||
1553 | return (SET_ERROR(ECKSUM)); | |
1554 | } | |
1555 | ||
1556 | /* | |
1557 | * There are too many combinations to try all of them in a reasonable amount | |
1558 | * of time. So try a fixed number of random combinations from the unique | |
1559 | * split versions, after which we'll consider the block unrecoverable. | |
1560 | */ | |
1561 | static int | |
1562 | vdev_indirect_splits_enumerate_randomly(indirect_vsd_t *iv, zio_t *zio) | |
1563 | { | |
1564 | iv->iv_attempts = 0; | |
1565 | ||
1566 | while (iv->iv_attempts < iv->iv_attempts_max) { | |
1567 | iv->iv_attempts++; | |
1568 | ||
1569 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1570 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1571 | indirect_child_t *ic = list_head(&is->is_unique_child); | |
1572 | int children = is->is_unique_children; | |
1573 | ||
1574 | for (int i = spa_get_random(children); i > 0; i--) | |
1575 | ic = list_next(&is->is_unique_child, ic); | |
1576 | ||
1577 | ASSERT3P(ic, !=, NULL); | |
1578 | is->is_good_child = ic; | |
1579 | } | |
1580 | ||
1581 | if (vdev_indirect_splits_checksum_validate(iv, zio) == 0) | |
1582 | return (0); | |
1583 | } | |
1584 | ||
1585 | return (SET_ERROR(ECKSUM)); | |
1586 | } | |
1587 | ||
1588 | /* | |
1589 | * This is a validation function for reconstruction. It randomly selects | |
1590 | * a good combination, if one can be found, and then it intentionally | |
1591 | * damages all other segment copes by zeroing them. This forces the | |
1592 | * reconstruction algorithm to locate the one remaining known good copy. | |
1593 | */ | |
1594 | static int | |
1595 | vdev_indirect_splits_damage(indirect_vsd_t *iv, zio_t *zio) | |
1596 | { | |
20eb30d0 TC |
1597 | int error; |
1598 | ||
1258bd77 BB |
1599 | /* Presume all the copies are unique for initial selection. */ |
1600 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1601 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1602 | is->is_unique_children = 0; | |
1603 | ||
1604 | for (int i = 0; i < is->is_children; i++) { | |
1605 | indirect_child_t *ic = &is->is_child[i]; | |
1606 | if (ic->ic_data != NULL) { | |
1607 | is->is_unique_children++; | |
1608 | list_insert_tail(&is->is_unique_child, ic); | |
1609 | } | |
1610 | } | |
20eb30d0 TC |
1611 | |
1612 | if (list_is_empty(&is->is_unique_child)) { | |
1613 | error = SET_ERROR(EIO); | |
1614 | goto out; | |
1615 | } | |
1258bd77 BB |
1616 | } |
1617 | ||
1618 | /* | |
1619 | * Set each is_good_child to a randomly-selected child which | |
1620 | * is known to contain validated data. | |
1621 | */ | |
20eb30d0 | 1622 | error = vdev_indirect_splits_enumerate_randomly(iv, zio); |
1258bd77 BB |
1623 | if (error) |
1624 | goto out; | |
1625 | ||
1626 | /* | |
1627 | * Damage all but the known good copy by zeroing it. This will | |
1628 | * result in two or less unique copies per indirect_child_t. | |
1629 | * Both may need to be checked in order to reconstruct the block. | |
1630 | * Set iv->iv_attempts_max such that all unique combinations will | |
4a7eb69a | 1631 | * enumerated, but limit the damage to at most 12 indirect splits. |
1258bd77 BB |
1632 | */ |
1633 | iv->iv_attempts_max = 1; | |
1634 | ||
1635 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1636 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1637 | for (int c = 0; c < is->is_children; c++) { | |
1638 | indirect_child_t *ic = &is->is_child[c]; | |
1639 | ||
1640 | if (ic == is->is_good_child) | |
1641 | continue; | |
1642 | if (ic->ic_data == NULL) | |
1643 | continue; | |
1644 | ||
1645 | abd_zero(ic->ic_data, ic->ic_data->abd_size); | |
1646 | } | |
1647 | ||
1648 | iv->iv_attempts_max *= 2; | |
4a7eb69a | 1649 | if (iv->iv_attempts_max >= (1ULL << 12)) { |
1258bd77 BB |
1650 | iv->iv_attempts_max = UINT64_MAX; |
1651 | break; | |
1652 | } | |
1653 | } | |
1654 | ||
1655 | out: | |
1656 | /* Empty the unique children lists so they can be reconstructed. */ | |
1657 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1658 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1659 | indirect_child_t *ic; | |
1660 | while ((ic = list_head(&is->is_unique_child)) != NULL) | |
1661 | list_remove(&is->is_unique_child, ic); | |
1662 | ||
1663 | is->is_unique_children = 0; | |
1664 | } | |
1665 | ||
1666 | return (error); | |
1667 | } | |
1668 | ||
9e052db4 MA |
1669 | /* |
1670 | * This function is called when we have read all copies of the data and need | |
1671 | * to try to find a combination of copies that gives us the right checksum. | |
1672 | * | |
1673 | * If we pointed to any mirror vdevs, this effectively does the job of the | |
1674 | * mirror. The mirror vdev code can't do its own job because we don't know | |
4589f3ae | 1675 | * the checksum of each split segment individually. |
9e052db4 | 1676 | * |
4589f3ae BB |
1677 | * We have to try every unique combination of copies of split segments, until |
1678 | * we find one that checksums correctly. Duplicate segment copies are first | |
1258bd77 BB |
1679 | * identified and latter skipped during reconstruction. This optimization |
1680 | * reduces the search space and ensures that of the remaining combinations | |
1681 | * at most one is correct. | |
4589f3ae BB |
1682 | * |
1683 | * When the total number of combinations is small they can all be checked. | |
1684 | * For example, if we have 3 segments in the split, and each points to a | |
1685 | * 2-way mirror with unique copies, we will have the following pieces of data: | |
9e052db4 MA |
1686 | * |
1687 | * | mirror child | |
1688 | * split | [0] [1] | |
1689 | * ======|===================== | |
1690 | * A | data_A_0 data_A_1 | |
1691 | * B | data_B_0 data_B_1 | |
1692 | * C | data_C_0 data_C_1 | |
1693 | * | |
1694 | * We will try the following (mirror children)^(number of splits) (2^3=8) | |
1695 | * combinations, which is similar to bitwise-little-endian counting in | |
1696 | * binary. In general each "digit" corresponds to a split segment, and the | |
1697 | * base of each digit is is_children, which can be different for each | |
1698 | * digit. | |
1699 | * | |
1700 | * "low bit" "high bit" | |
1701 | * v v | |
1702 | * data_A_0 data_B_0 data_C_0 | |
1703 | * data_A_1 data_B_0 data_C_0 | |
1704 | * data_A_0 data_B_1 data_C_0 | |
1705 | * data_A_1 data_B_1 data_C_0 | |
1706 | * data_A_0 data_B_0 data_C_1 | |
1707 | * data_A_1 data_B_0 data_C_1 | |
1708 | * data_A_0 data_B_1 data_C_1 | |
1709 | * data_A_1 data_B_1 data_C_1 | |
1710 | * | |
1711 | * Note that the split segments may be on the same or different top-level | |
1258bd77 BB |
1712 | * vdevs. In either case, we may need to try lots of combinations (see |
1713 | * zfs_reconstruct_indirect_combinations_max). This ensures that if a mirror | |
1714 | * has small silent errors on all of its children, we can still reconstruct | |
1715 | * the correct data, as long as those errors are at sufficiently-separated | |
9e052db4 MA |
1716 | * offsets (specifically, separated by the largest block size - default of |
1717 | * 128KB, but up to 16MB). | |
1718 | */ | |
1719 | static void | |
1720 | vdev_indirect_reconstruct_io_done(zio_t *zio) | |
1721 | { | |
1722 | indirect_vsd_t *iv = zio->io_vsd; | |
1258bd77 BB |
1723 | boolean_t known_good = B_FALSE; |
1724 | int error; | |
1725 | ||
1726 | iv->iv_unique_combinations = 1; | |
1727 | iv->iv_attempts_max = UINT64_MAX; | |
4589f3ae BB |
1728 | |
1729 | if (zfs_reconstruct_indirect_combinations_max > 0) | |
1258bd77 BB |
1730 | iv->iv_attempts_max = zfs_reconstruct_indirect_combinations_max; |
1731 | ||
1732 | /* | |
1733 | * If nonzero, every 1/x blocks will be damaged, in order to validate | |
1734 | * reconstruction when there are split segments with damaged copies. | |
4a7eb69a | 1735 | * Known_good will be TRUE when reconstruction is known to be possible. |
1258bd77 BB |
1736 | */ |
1737 | if (zfs_reconstruct_indirect_damage_fraction != 0 && | |
1738 | spa_get_random(zfs_reconstruct_indirect_damage_fraction) == 0) | |
1739 | known_good = (vdev_indirect_splits_damage(iv, zio) == 0); | |
9e052db4 | 1740 | |
4589f3ae | 1741 | /* |
1258bd77 BB |
1742 | * Determine the unique children for a split segment and add them |
1743 | * to the is_unique_child list. By restricting reconstruction | |
1744 | * to these children, only unique combinations will be considered. | |
1745 | * This can vastly reduce the search space when there are a large | |
1746 | * number of indirect splits. | |
4589f3ae | 1747 | */ |
9e052db4 | 1748 | for (indirect_split_t *is = list_head(&iv->iv_splits); |
4589f3ae | 1749 | is != NULL; is = list_next(&iv->iv_splits, is)) { |
1258bd77 | 1750 | is->is_unique_children = 0; |
4589f3ae BB |
1751 | |
1752 | for (int i = 0; i < is->is_children; i++) { | |
1258bd77 BB |
1753 | indirect_child_t *ic_i = &is->is_child[i]; |
1754 | ||
1755 | if (ic_i->ic_data == NULL || | |
1756 | ic_i->ic_duplicate != NULL) | |
4589f3ae BB |
1757 | continue; |
1758 | ||
1759 | for (int j = i + 1; j < is->is_children; j++) { | |
1258bd77 BB |
1760 | indirect_child_t *ic_j = &is->is_child[j]; |
1761 | ||
1762 | if (ic_j->ic_data == NULL || | |
1763 | ic_j->ic_duplicate != NULL) | |
4589f3ae BB |
1764 | continue; |
1765 | ||
1258bd77 BB |
1766 | if (abd_cmp(ic_i->ic_data, ic_j->ic_data) == 0) |
1767 | ic_j->ic_duplicate = ic_i; | |
4589f3ae BB |
1768 | } |
1769 | ||
1258bd77 BB |
1770 | is->is_unique_children++; |
1771 | list_insert_tail(&is->is_unique_child, ic_i); | |
4589f3ae BB |
1772 | } |
1773 | ||
1258bd77 BB |
1774 | /* Reconstruction is impossible, no valid children */ |
1775 | EQUIV(list_is_empty(&is->is_unique_child), | |
1776 | is->is_unique_children == 0); | |
1777 | if (list_is_empty(&is->is_unique_child)) { | |
4589f3ae BB |
1778 | zio->io_error = EIO; |
1779 | vdev_indirect_all_checksum_errors(zio); | |
1780 | zio_checksum_verified(zio); | |
1781 | return; | |
1782 | } | |
1783 | ||
1258bd77 | 1784 | iv->iv_unique_combinations *= is->is_unique_children; |
4589f3ae | 1785 | } |
9e052db4 | 1786 | |
1258bd77 BB |
1787 | if (iv->iv_unique_combinations <= iv->iv_attempts_max) |
1788 | error = vdev_indirect_splits_enumerate_all(iv, zio); | |
1789 | else | |
1790 | error = vdev_indirect_splits_enumerate_randomly(iv, zio); | |
9e052db4 | 1791 | |
1258bd77 BB |
1792 | if (error != 0) { |
1793 | /* All attempted combinations failed. */ | |
1794 | ASSERT3B(known_good, ==, B_FALSE); | |
1795 | zio->io_error = error; | |
1796 | vdev_indirect_all_checksum_errors(zio); | |
1797 | } else { | |
9e052db4 | 1798 | /* |
1258bd77 BB |
1799 | * The checksum has been successfully validated. Issue |
1800 | * repair I/Os to any copies of splits which don't match | |
1801 | * the validated version. | |
9e052db4 | 1802 | */ |
1258bd77 BB |
1803 | ASSERT0(vdev_indirect_splits_checksum_validate(iv, zio)); |
1804 | vdev_indirect_repair(zio); | |
1805 | zio_checksum_verified(zio); | |
9e052db4 MA |
1806 | } |
1807 | } | |
1808 | ||
1809 | static void | |
1810 | vdev_indirect_io_done(zio_t *zio) | |
1811 | { | |
1812 | indirect_vsd_t *iv = zio->io_vsd; | |
1813 | ||
1814 | if (iv->iv_reconstruct) { | |
1815 | /* | |
1816 | * We have read all copies of the data (e.g. from mirrors), | |
1817 | * either because this was a scrub/resilver, or because the | |
1818 | * one-copy read didn't checksum correctly. | |
1819 | */ | |
1820 | vdev_indirect_reconstruct_io_done(zio); | |
1821 | return; | |
1822 | } | |
1823 | ||
1824 | if (!iv->iv_split_block) { | |
1825 | /* | |
1826 | * This was not a split block, so we passed the BP down, | |
1827 | * and the checksum was handled by the (one) child zio. | |
1828 | */ | |
1829 | return; | |
1830 | } | |
1831 | ||
1832 | zio_bad_cksum_t zbc; | |
1833 | int ret = zio_checksum_error(zio, &zbc); | |
1834 | if (ret == 0) { | |
1835 | zio_checksum_verified(zio); | |
1836 | return; | |
1837 | } | |
1838 | ||
1839 | /* | |
1840 | * The checksum didn't match. Read all copies of all splits, and | |
1841 | * then we will try to reconstruct. The next time | |
1842 | * vdev_indirect_io_done() is called, iv_reconstruct will be set. | |
1843 | */ | |
1844 | vdev_indirect_read_all(zio); | |
1845 | ||
1846 | zio_vdev_io_redone(zio); | |
1847 | } | |
1848 | ||
a1d477c2 MA |
1849 | vdev_ops_t vdev_indirect_ops = { |
1850 | vdev_indirect_open, | |
1851 | vdev_indirect_close, | |
1852 | vdev_default_asize, | |
1853 | vdev_indirect_io_start, | |
1854 | vdev_indirect_io_done, | |
1855 | NULL, | |
1856 | NULL, | |
1857 | NULL, | |
1858 | NULL, | |
1859 | vdev_indirect_remap, | |
619f0976 | 1860 | NULL, |
a1d477c2 MA |
1861 | VDEV_TYPE_INDIRECT, /* name of this vdev type */ |
1862 | B_FALSE /* leaf vdev */ | |
1863 | }; | |
1864 | ||
93ce2b4c | 1865 | #if defined(_KERNEL) |
a1d477c2 MA |
1866 | EXPORT_SYMBOL(rs_alloc); |
1867 | EXPORT_SYMBOL(spa_condense_fini); | |
9d5b5245 | 1868 | EXPORT_SYMBOL(spa_start_indirect_condensing_thread); |
a1d477c2 MA |
1869 | EXPORT_SYMBOL(spa_condense_indirect_start_sync); |
1870 | EXPORT_SYMBOL(spa_condense_init); | |
1871 | EXPORT_SYMBOL(spa_vdev_indirect_mark_obsolete); | |
1872 | EXPORT_SYMBOL(vdev_indirect_mark_obsolete); | |
1873 | EXPORT_SYMBOL(vdev_indirect_should_condense); | |
1874 | EXPORT_SYMBOL(vdev_indirect_sync_obsolete); | |
1875 | EXPORT_SYMBOL(vdev_obsolete_counts_are_precise); | |
1876 | EXPORT_SYMBOL(vdev_obsolete_sm_object); | |
1877 | ||
0dc2f70c MA |
1878 | module_param(zfs_condense_indirect_vdevs_enable, int, 0644); |
1879 | MODULE_PARM_DESC(zfs_condense_indirect_vdevs_enable, | |
1880 | "Whether to attempt condensing indirect vdev mappings"); | |
1881 | ||
a1d477c2 MA |
1882 | /* CSTYLED */ |
1883 | module_param(zfs_condense_min_mapping_bytes, ulong, 0644); | |
1884 | MODULE_PARM_DESC(zfs_condense_min_mapping_bytes, | |
1885 | "Minimum size of vdev mapping to condense"); | |
1886 | ||
0dc2f70c MA |
1887 | /* CSTYLED */ |
1888 | module_param(zfs_condense_max_obsolete_bytes, ulong, 0644); | |
1889 | MODULE_PARM_DESC(zfs_condense_max_obsolete_bytes, | |
1890 | "Minimum size obsolete spacemap to attempt condensing"); | |
1891 | ||
a1d477c2 MA |
1892 | module_param(zfs_condense_indirect_commit_entry_delay_ms, int, 0644); |
1893 | MODULE_PARM_DESC(zfs_condense_indirect_commit_entry_delay_ms, | |
1894 | "Delay while condensing vdev mapping"); | |
9e052db4 | 1895 | |
4589f3ae BB |
1896 | module_param(zfs_reconstruct_indirect_combinations_max, int, 0644); |
1897 | MODULE_PARM_DESC(zfs_reconstruct_indirect_combinations_max, | |
1898 | "Maximum number of combinations when reconstructing split segments"); | |
a1d477c2 | 1899 | #endif |