]>
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 | ||
174 | boolean_t zfs_condense_indirect_vdevs_enable = B_TRUE; | |
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 | */ | |
191 | uint64_t zfs_condense_max_obsolete_bytes = 1024 * 1024 * 1024; | |
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 | */ |
4589f3ae | 216 | int zfs_reconstruct_indirect_combinations_max = 100; |
9e052db4 MA |
217 | |
218 | /* | |
219 | * The indirect_child_t represents the vdev that we will read from, when we | |
220 | * need to read all copies of the data (e.g. for scrub or reconstruction). | |
221 | * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror), | |
222 | * ic_vdev is the same as is_vdev. However, for mirror top-level vdevs, | |
223 | * ic_vdev is a child of the mirror. | |
224 | */ | |
225 | typedef struct indirect_child { | |
226 | abd_t *ic_data; | |
227 | vdev_t *ic_vdev; | |
4589f3ae BB |
228 | |
229 | /* | |
230 | * ic_duplicate is -1 when the ic_data contents are unique, when it | |
231 | * is determined to be a duplicate it refers to the primary child. | |
232 | */ | |
233 | int ic_duplicate; | |
9e052db4 MA |
234 | } indirect_child_t; |
235 | ||
236 | /* | |
237 | * The indirect_split_t represents one mapped segment of an i/o to the | |
238 | * indirect vdev. For non-split (contiguously-mapped) blocks, there will be | |
239 | * only one indirect_split_t, with is_split_offset==0 and is_size==io_size. | |
240 | * For split blocks, there will be several of these. | |
241 | */ | |
242 | typedef struct indirect_split { | |
243 | list_node_t is_node; /* link on iv_splits */ | |
244 | ||
245 | /* | |
246 | * is_split_offset is the offset into the i/o. | |
247 | * This is the sum of the previous splits' is_size's. | |
248 | */ | |
249 | uint64_t is_split_offset; | |
250 | ||
251 | vdev_t *is_vdev; /* top-level vdev */ | |
252 | uint64_t is_target_offset; /* offset on is_vdev */ | |
253 | uint64_t is_size; | |
254 | int is_children; /* number of entries in is_child[] */ | |
255 | ||
256 | /* | |
257 | * is_good_child is the child that we are currently using to | |
258 | * attempt reconstruction. | |
259 | */ | |
260 | int is_good_child; | |
261 | ||
262 | indirect_child_t is_child[1]; /* variable-length */ | |
263 | } indirect_split_t; | |
264 | ||
265 | /* | |
266 | * The indirect_vsd_t is associated with each i/o to the indirect vdev. | |
267 | * It is the "Vdev-Specific Data" in the zio_t's io_vsd. | |
268 | */ | |
269 | typedef struct indirect_vsd { | |
270 | boolean_t iv_split_block; | |
271 | boolean_t iv_reconstruct; | |
272 | ||
273 | list_t iv_splits; /* list of indirect_split_t's */ | |
274 | } indirect_vsd_t; | |
275 | ||
276 | static void | |
277 | vdev_indirect_map_free(zio_t *zio) | |
278 | { | |
279 | indirect_vsd_t *iv = zio->io_vsd; | |
280 | ||
281 | indirect_split_t *is; | |
282 | while ((is = list_head(&iv->iv_splits)) != NULL) { | |
283 | for (int c = 0; c < is->is_children; c++) { | |
284 | indirect_child_t *ic = &is->is_child[c]; | |
285 | if (ic->ic_data != NULL) | |
286 | abd_free(ic->ic_data); | |
287 | } | |
288 | list_remove(&iv->iv_splits, is); | |
289 | kmem_free(is, | |
290 | offsetof(indirect_split_t, is_child[is->is_children])); | |
291 | } | |
292 | kmem_free(iv, sizeof (*iv)); | |
293 | } | |
294 | ||
295 | static const zio_vsd_ops_t vdev_indirect_vsd_ops = { | |
089500e7 MW |
296 | .vsd_free = vdev_indirect_map_free, |
297 | .vsd_cksum_report = zio_vsd_default_cksum_report | |
9e052db4 MA |
298 | }; |
299 | ||
a1d477c2 MA |
300 | /* |
301 | * Mark the given offset and size as being obsolete in the given txg. | |
302 | */ | |
303 | void | |
304 | vdev_indirect_mark_obsolete(vdev_t *vd, uint64_t offset, uint64_t size, | |
305 | uint64_t txg) | |
306 | { | |
307 | spa_t *spa = vd->vdev_spa; | |
308 | ASSERT3U(spa_syncing_txg(spa), ==, txg); | |
309 | ASSERT3U(vd->vdev_indirect_config.vic_mapping_object, !=, 0); | |
310 | ASSERT(vd->vdev_removing || vd->vdev_ops == &vdev_indirect_ops); | |
311 | ASSERT(size > 0); | |
312 | VERIFY(vdev_indirect_mapping_entry_for_offset( | |
313 | vd->vdev_indirect_mapping, offset) != NULL); | |
314 | ||
315 | if (spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS)) { | |
316 | mutex_enter(&vd->vdev_obsolete_lock); | |
317 | range_tree_add(vd->vdev_obsolete_segments, offset, size); | |
318 | mutex_exit(&vd->vdev_obsolete_lock); | |
319 | vdev_dirty(vd, 0, NULL, txg); | |
320 | } | |
321 | } | |
322 | ||
323 | /* | |
324 | * Mark the DVA vdev_id:offset:size as being obsolete in the given tx. This | |
325 | * wrapper is provided because the DMU does not know about vdev_t's and | |
326 | * cannot directly call vdev_indirect_mark_obsolete. | |
327 | */ | |
328 | void | |
329 | spa_vdev_indirect_mark_obsolete(spa_t *spa, uint64_t vdev_id, uint64_t offset, | |
330 | uint64_t size, dmu_tx_t *tx) | |
331 | { | |
332 | vdev_t *vd = vdev_lookup_top(spa, vdev_id); | |
333 | ASSERT(dmu_tx_is_syncing(tx)); | |
334 | ||
335 | /* The DMU can only remap indirect vdevs. */ | |
336 | ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); | |
337 | vdev_indirect_mark_obsolete(vd, offset, size, dmu_tx_get_txg(tx)); | |
338 | } | |
339 | ||
340 | static spa_condensing_indirect_t * | |
341 | spa_condensing_indirect_create(spa_t *spa) | |
342 | { | |
343 | spa_condensing_indirect_phys_t *scip = | |
344 | &spa->spa_condensing_indirect_phys; | |
345 | spa_condensing_indirect_t *sci = kmem_zalloc(sizeof (*sci), KM_SLEEP); | |
346 | objset_t *mos = spa->spa_meta_objset; | |
347 | ||
348 | for (int i = 0; i < TXG_SIZE; i++) { | |
349 | list_create(&sci->sci_new_mapping_entries[i], | |
350 | sizeof (vdev_indirect_mapping_entry_t), | |
351 | offsetof(vdev_indirect_mapping_entry_t, vime_node)); | |
352 | } | |
353 | ||
354 | sci->sci_new_mapping = | |
355 | vdev_indirect_mapping_open(mos, scip->scip_next_mapping_object); | |
356 | ||
357 | return (sci); | |
358 | } | |
359 | ||
360 | static void | |
361 | spa_condensing_indirect_destroy(spa_condensing_indirect_t *sci) | |
362 | { | |
363 | for (int i = 0; i < TXG_SIZE; i++) | |
364 | list_destroy(&sci->sci_new_mapping_entries[i]); | |
365 | ||
366 | if (sci->sci_new_mapping != NULL) | |
367 | vdev_indirect_mapping_close(sci->sci_new_mapping); | |
368 | ||
369 | kmem_free(sci, sizeof (*sci)); | |
370 | } | |
371 | ||
372 | boolean_t | |
373 | vdev_indirect_should_condense(vdev_t *vd) | |
374 | { | |
375 | vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; | |
376 | spa_t *spa = vd->vdev_spa; | |
377 | ||
378 | ASSERT(dsl_pool_sync_context(spa->spa_dsl_pool)); | |
379 | ||
380 | if (!zfs_condense_indirect_vdevs_enable) | |
381 | return (B_FALSE); | |
382 | ||
383 | /* | |
384 | * We can only condense one indirect vdev at a time. | |
385 | */ | |
386 | if (spa->spa_condensing_indirect != NULL) | |
387 | return (B_FALSE); | |
388 | ||
389 | if (spa_shutting_down(spa)) | |
390 | return (B_FALSE); | |
391 | ||
392 | /* | |
393 | * The mapping object size must not change while we are | |
394 | * condensing, so we can only condense indirect vdevs | |
395 | * (not vdevs that are still in the middle of being removed). | |
396 | */ | |
397 | if (vd->vdev_ops != &vdev_indirect_ops) | |
398 | return (B_FALSE); | |
399 | ||
400 | /* | |
401 | * If nothing new has been marked obsolete, there is no | |
402 | * point in condensing. | |
403 | */ | |
404 | if (vd->vdev_obsolete_sm == NULL) { | |
405 | ASSERT0(vdev_obsolete_sm_object(vd)); | |
406 | return (B_FALSE); | |
407 | } | |
408 | ||
409 | ASSERT(vd->vdev_obsolete_sm != NULL); | |
410 | ||
411 | ASSERT3U(vdev_obsolete_sm_object(vd), ==, | |
412 | space_map_object(vd->vdev_obsolete_sm)); | |
413 | ||
414 | uint64_t bytes_mapped = vdev_indirect_mapping_bytes_mapped(vim); | |
415 | uint64_t bytes_obsolete = space_map_allocated(vd->vdev_obsolete_sm); | |
416 | uint64_t mapping_size = vdev_indirect_mapping_size(vim); | |
417 | uint64_t obsolete_sm_size = space_map_length(vd->vdev_obsolete_sm); | |
418 | ||
419 | ASSERT3U(bytes_obsolete, <=, bytes_mapped); | |
420 | ||
421 | /* | |
422 | * If a high percentage of the bytes that are mapped have become | |
423 | * obsolete, condense (unless the mapping is already small enough). | |
424 | * This has a good chance of reducing the amount of memory used | |
425 | * by the mapping. | |
426 | */ | |
427 | if (bytes_obsolete * 100 / bytes_mapped >= | |
428 | zfs_indirect_condense_obsolete_pct && | |
429 | mapping_size > zfs_condense_min_mapping_bytes) { | |
430 | zfs_dbgmsg("should condense vdev %llu because obsolete " | |
431 | "spacemap covers %d%% of %lluMB mapping", | |
432 | (u_longlong_t)vd->vdev_id, | |
433 | (int)(bytes_obsolete * 100 / bytes_mapped), | |
434 | (u_longlong_t)bytes_mapped / 1024 / 1024); | |
435 | return (B_TRUE); | |
436 | } | |
437 | ||
438 | /* | |
439 | * If the obsolete space map takes up too much space on disk, | |
440 | * condense in order to free up this disk space. | |
441 | */ | |
442 | if (obsolete_sm_size >= zfs_condense_max_obsolete_bytes) { | |
443 | zfs_dbgmsg("should condense vdev %llu because obsolete sm " | |
444 | "length %lluMB >= max size %lluMB", | |
445 | (u_longlong_t)vd->vdev_id, | |
446 | (u_longlong_t)obsolete_sm_size / 1024 / 1024, | |
447 | (u_longlong_t)zfs_condense_max_obsolete_bytes / | |
448 | 1024 / 1024); | |
449 | return (B_TRUE); | |
450 | } | |
451 | ||
452 | return (B_FALSE); | |
453 | } | |
454 | ||
455 | /* | |
456 | * This sync task completes (finishes) a condense, deleting the old | |
457 | * mapping and replacing it with the new one. | |
458 | */ | |
459 | static void | |
460 | spa_condense_indirect_complete_sync(void *arg, dmu_tx_t *tx) | |
461 | { | |
462 | spa_condensing_indirect_t *sci = arg; | |
463 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
464 | spa_condensing_indirect_phys_t *scip = | |
465 | &spa->spa_condensing_indirect_phys; | |
466 | vdev_t *vd = vdev_lookup_top(spa, scip->scip_vdev); | |
467 | vdev_indirect_config_t *vic = &vd->vdev_indirect_config; | |
468 | objset_t *mos = spa->spa_meta_objset; | |
469 | vdev_indirect_mapping_t *old_mapping = vd->vdev_indirect_mapping; | |
470 | uint64_t old_count = vdev_indirect_mapping_num_entries(old_mapping); | |
471 | uint64_t new_count = | |
472 | vdev_indirect_mapping_num_entries(sci->sci_new_mapping); | |
473 | ||
474 | ASSERT(dmu_tx_is_syncing(tx)); | |
475 | ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); | |
476 | ASSERT3P(sci, ==, spa->spa_condensing_indirect); | |
477 | for (int i = 0; i < TXG_SIZE; i++) { | |
478 | ASSERT(list_is_empty(&sci->sci_new_mapping_entries[i])); | |
479 | } | |
480 | ASSERT(vic->vic_mapping_object != 0); | |
481 | ASSERT3U(vd->vdev_id, ==, scip->scip_vdev); | |
482 | ASSERT(scip->scip_next_mapping_object != 0); | |
483 | ASSERT(scip->scip_prev_obsolete_sm_object != 0); | |
484 | ||
485 | /* | |
486 | * Reset vdev_indirect_mapping to refer to the new object. | |
487 | */ | |
488 | rw_enter(&vd->vdev_indirect_rwlock, RW_WRITER); | |
489 | vdev_indirect_mapping_close(vd->vdev_indirect_mapping); | |
490 | vd->vdev_indirect_mapping = sci->sci_new_mapping; | |
491 | rw_exit(&vd->vdev_indirect_rwlock); | |
492 | ||
493 | sci->sci_new_mapping = NULL; | |
494 | vdev_indirect_mapping_free(mos, vic->vic_mapping_object, tx); | |
495 | vic->vic_mapping_object = scip->scip_next_mapping_object; | |
496 | scip->scip_next_mapping_object = 0; | |
497 | ||
498 | space_map_free_obj(mos, scip->scip_prev_obsolete_sm_object, tx); | |
499 | spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); | |
500 | scip->scip_prev_obsolete_sm_object = 0; | |
501 | ||
502 | scip->scip_vdev = 0; | |
503 | ||
504 | VERIFY0(zap_remove(mos, DMU_POOL_DIRECTORY_OBJECT, | |
505 | DMU_POOL_CONDENSING_INDIRECT, tx)); | |
506 | spa_condensing_indirect_destroy(spa->spa_condensing_indirect); | |
507 | spa->spa_condensing_indirect = NULL; | |
508 | ||
509 | zfs_dbgmsg("finished condense of vdev %llu in txg %llu: " | |
510 | "new mapping object %llu has %llu entries " | |
511 | "(was %llu entries)", | |
512 | vd->vdev_id, dmu_tx_get_txg(tx), vic->vic_mapping_object, | |
513 | new_count, old_count); | |
514 | ||
515 | vdev_config_dirty(spa->spa_root_vdev); | |
516 | } | |
517 | ||
518 | /* | |
519 | * This sync task appends entries to the new mapping object. | |
520 | */ | |
521 | static void | |
522 | spa_condense_indirect_commit_sync(void *arg, dmu_tx_t *tx) | |
523 | { | |
524 | spa_condensing_indirect_t *sci = arg; | |
525 | uint64_t txg = dmu_tx_get_txg(tx); | |
526 | ASSERTV(spa_t *spa = dmu_tx_pool(tx)->dp_spa); | |
527 | ||
528 | ASSERT(dmu_tx_is_syncing(tx)); | |
529 | ASSERT3P(sci, ==, spa->spa_condensing_indirect); | |
530 | ||
531 | vdev_indirect_mapping_add_entries(sci->sci_new_mapping, | |
532 | &sci->sci_new_mapping_entries[txg & TXG_MASK], tx); | |
533 | ASSERT(list_is_empty(&sci->sci_new_mapping_entries[txg & TXG_MASK])); | |
534 | } | |
535 | ||
536 | /* | |
537 | * Open-context function to add one entry to the new mapping. The new | |
538 | * entry will be remembered and written from syncing context. | |
539 | */ | |
540 | static void | |
541 | spa_condense_indirect_commit_entry(spa_t *spa, | |
542 | vdev_indirect_mapping_entry_phys_t *vimep, uint32_t count) | |
543 | { | |
544 | spa_condensing_indirect_t *sci = spa->spa_condensing_indirect; | |
545 | ||
546 | ASSERT3U(count, <, DVA_GET_ASIZE(&vimep->vimep_dst)); | |
547 | ||
548 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
549 | dmu_tx_hold_space(tx, sizeof (*vimep) + sizeof (count)); | |
550 | VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); | |
551 | int txgoff = dmu_tx_get_txg(tx) & TXG_MASK; | |
552 | ||
553 | /* | |
554 | * If we are the first entry committed this txg, kick off the sync | |
555 | * task to write to the MOS on our behalf. | |
556 | */ | |
557 | if (list_is_empty(&sci->sci_new_mapping_entries[txgoff])) { | |
558 | dsl_sync_task_nowait(dmu_tx_pool(tx), | |
559 | spa_condense_indirect_commit_sync, sci, | |
560 | 0, ZFS_SPACE_CHECK_NONE, tx); | |
561 | } | |
562 | ||
563 | vdev_indirect_mapping_entry_t *vime = | |
564 | kmem_alloc(sizeof (*vime), KM_SLEEP); | |
565 | vime->vime_mapping = *vimep; | |
566 | vime->vime_obsolete_count = count; | |
567 | list_insert_tail(&sci->sci_new_mapping_entries[txgoff], vime); | |
568 | ||
569 | dmu_tx_commit(tx); | |
570 | } | |
571 | ||
572 | static void | |
573 | spa_condense_indirect_generate_new_mapping(vdev_t *vd, | |
9d5b5245 | 574 | uint32_t *obsolete_counts, uint64_t start_index, zthr_t *zthr) |
a1d477c2 MA |
575 | { |
576 | spa_t *spa = vd->vdev_spa; | |
577 | uint64_t mapi = start_index; | |
578 | vdev_indirect_mapping_t *old_mapping = vd->vdev_indirect_mapping; | |
579 | uint64_t old_num_entries = | |
580 | vdev_indirect_mapping_num_entries(old_mapping); | |
581 | ||
582 | ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); | |
583 | ASSERT3U(vd->vdev_id, ==, spa->spa_condensing_indirect_phys.scip_vdev); | |
584 | ||
585 | zfs_dbgmsg("starting condense of vdev %llu from index %llu", | |
586 | (u_longlong_t)vd->vdev_id, | |
587 | (u_longlong_t)mapi); | |
588 | ||
9d5b5245 SD |
589 | while (mapi < old_num_entries) { |
590 | ||
591 | if (zthr_iscancelled(zthr)) { | |
592 | zfs_dbgmsg("pausing condense of vdev %llu " | |
593 | "at index %llu", (u_longlong_t)vd->vdev_id, | |
594 | (u_longlong_t)mapi); | |
595 | break; | |
596 | } | |
597 | ||
a1d477c2 MA |
598 | vdev_indirect_mapping_entry_phys_t *entry = |
599 | &old_mapping->vim_entries[mapi]; | |
600 | uint64_t entry_size = DVA_GET_ASIZE(&entry->vimep_dst); | |
601 | ASSERT3U(obsolete_counts[mapi], <=, entry_size); | |
602 | if (obsolete_counts[mapi] < entry_size) { | |
603 | spa_condense_indirect_commit_entry(spa, entry, | |
604 | obsolete_counts[mapi]); | |
605 | ||
606 | /* | |
607 | * This delay may be requested for testing, debugging, | |
608 | * or performance reasons. | |
609 | */ | |
610 | hrtime_t now = gethrtime(); | |
611 | hrtime_t sleep_until = now + MSEC2NSEC( | |
612 | zfs_condense_indirect_commit_entry_delay_ms); | |
613 | zfs_sleep_until(sleep_until); | |
614 | } | |
615 | ||
616 | mapi++; | |
617 | } | |
a1d477c2 MA |
618 | } |
619 | ||
9d5b5245 SD |
620 | /* ARGSUSED */ |
621 | static boolean_t | |
622 | spa_condense_indirect_thread_check(void *arg, zthr_t *zthr) | |
a1d477c2 | 623 | { |
9d5b5245 SD |
624 | spa_t *spa = arg; |
625 | ||
626 | return (spa->spa_condensing_indirect != NULL); | |
627 | } | |
628 | ||
629 | /* ARGSUSED */ | |
630 | static int | |
631 | spa_condense_indirect_thread(void *arg, zthr_t *zthr) | |
632 | { | |
633 | spa_t *spa = arg; | |
634 | vdev_t *vd; | |
635 | ||
636 | ASSERT3P(spa->spa_condensing_indirect, !=, NULL); | |
637 | spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); | |
638 | vd = vdev_lookup_top(spa, spa->spa_condensing_indirect_phys.scip_vdev); | |
639 | ASSERT3P(vd, !=, NULL); | |
640 | spa_config_exit(spa, SCL_VDEV, FTAG); | |
641 | ||
a1d477c2 MA |
642 | spa_condensing_indirect_t *sci = spa->spa_condensing_indirect; |
643 | spa_condensing_indirect_phys_t *scip = | |
644 | &spa->spa_condensing_indirect_phys; | |
645 | uint32_t *counts; | |
646 | uint64_t start_index; | |
647 | vdev_indirect_mapping_t *old_mapping = vd->vdev_indirect_mapping; | |
648 | space_map_t *prev_obsolete_sm = NULL; | |
649 | ||
650 | ASSERT3U(vd->vdev_id, ==, scip->scip_vdev); | |
651 | ASSERT(scip->scip_next_mapping_object != 0); | |
652 | ASSERT(scip->scip_prev_obsolete_sm_object != 0); | |
653 | ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); | |
654 | ||
655 | for (int i = 0; i < TXG_SIZE; i++) { | |
656 | /* | |
657 | * The list must start out empty in order for the | |
658 | * _commit_sync() sync task to be properly registered | |
659 | * on the first call to _commit_entry(); so it's wise | |
660 | * to double check and ensure we actually are starting | |
661 | * with empty lists. | |
662 | */ | |
663 | ASSERT(list_is_empty(&sci->sci_new_mapping_entries[i])); | |
664 | } | |
665 | ||
666 | VERIFY0(space_map_open(&prev_obsolete_sm, spa->spa_meta_objset, | |
667 | scip->scip_prev_obsolete_sm_object, 0, vd->vdev_asize, 0)); | |
668 | space_map_update(prev_obsolete_sm); | |
669 | counts = vdev_indirect_mapping_load_obsolete_counts(old_mapping); | |
670 | if (prev_obsolete_sm != NULL) { | |
671 | vdev_indirect_mapping_load_obsolete_spacemap(old_mapping, | |
672 | counts, prev_obsolete_sm); | |
673 | } | |
674 | space_map_close(prev_obsolete_sm); | |
675 | ||
676 | /* | |
677 | * Generate new mapping. Determine what index to continue from | |
678 | * based on the max offset that we've already written in the | |
679 | * new mapping. | |
680 | */ | |
681 | uint64_t max_offset = | |
682 | vdev_indirect_mapping_max_offset(sci->sci_new_mapping); | |
683 | if (max_offset == 0) { | |
684 | /* We haven't written anything to the new mapping yet. */ | |
685 | start_index = 0; | |
686 | } else { | |
687 | /* | |
688 | * Pick up from where we left off. _entry_for_offset() | |
689 | * returns a pointer into the vim_entries array. If | |
690 | * max_offset is greater than any of the mappings | |
691 | * contained in the table NULL will be returned and | |
692 | * that indicates we've exhausted our iteration of the | |
693 | * old_mapping. | |
694 | */ | |
695 | ||
696 | vdev_indirect_mapping_entry_phys_t *entry = | |
697 | vdev_indirect_mapping_entry_for_offset_or_next(old_mapping, | |
698 | max_offset); | |
699 | ||
700 | if (entry == NULL) { | |
701 | /* | |
702 | * We've already written the whole new mapping. | |
703 | * This special value will cause us to skip the | |
704 | * generate_new_mapping step and just do the sync | |
705 | * task to complete the condense. | |
706 | */ | |
707 | start_index = UINT64_MAX; | |
708 | } else { | |
709 | start_index = entry - old_mapping->vim_entries; | |
710 | ASSERT3U(start_index, <, | |
711 | vdev_indirect_mapping_num_entries(old_mapping)); | |
712 | } | |
713 | } | |
714 | ||
9d5b5245 SD |
715 | spa_condense_indirect_generate_new_mapping(vd, counts, |
716 | start_index, zthr); | |
a1d477c2 MA |
717 | |
718 | vdev_indirect_mapping_free_obsolete_counts(old_mapping, counts); | |
719 | ||
720 | /* | |
9d5b5245 SD |
721 | * If the zthr has received a cancellation signal while running |
722 | * in generate_new_mapping() or at any point after that, then bail | |
723 | * early. We don't want to complete the condense if the spa is | |
724 | * shutting down. | |
a1d477c2 | 725 | */ |
9d5b5245 SD |
726 | if (zthr_iscancelled(zthr)) |
727 | return (0); | |
728 | ||
729 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, | |
730 | spa_condense_indirect_complete_sync, sci, 0, ZFS_SPACE_CHECK_NONE)); | |
a1d477c2 | 731 | |
9d5b5245 | 732 | return (0); |
a1d477c2 MA |
733 | } |
734 | ||
735 | /* | |
736 | * Sync task to begin the condensing process. | |
737 | */ | |
738 | void | |
739 | spa_condense_indirect_start_sync(vdev_t *vd, dmu_tx_t *tx) | |
740 | { | |
741 | spa_t *spa = vd->vdev_spa; | |
742 | spa_condensing_indirect_phys_t *scip = | |
743 | &spa->spa_condensing_indirect_phys; | |
744 | ||
745 | ASSERT0(scip->scip_next_mapping_object); | |
746 | ASSERT0(scip->scip_prev_obsolete_sm_object); | |
747 | ASSERT0(scip->scip_vdev); | |
748 | ASSERT(dmu_tx_is_syncing(tx)); | |
749 | ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); | |
750 | ASSERT(spa_feature_is_active(spa, SPA_FEATURE_OBSOLETE_COUNTS)); | |
751 | ASSERT(vdev_indirect_mapping_num_entries(vd->vdev_indirect_mapping)); | |
752 | ||
753 | uint64_t obsolete_sm_obj = vdev_obsolete_sm_object(vd); | |
754 | ASSERT(obsolete_sm_obj != 0); | |
755 | ||
756 | scip->scip_vdev = vd->vdev_id; | |
757 | scip->scip_next_mapping_object = | |
758 | vdev_indirect_mapping_alloc(spa->spa_meta_objset, tx); | |
759 | ||
760 | scip->scip_prev_obsolete_sm_object = obsolete_sm_obj; | |
761 | ||
762 | /* | |
763 | * We don't need to allocate a new space map object, since | |
764 | * vdev_indirect_sync_obsolete will allocate one when needed. | |
765 | */ | |
766 | space_map_close(vd->vdev_obsolete_sm); | |
767 | vd->vdev_obsolete_sm = NULL; | |
768 | VERIFY0(zap_remove(spa->spa_meta_objset, vd->vdev_top_zap, | |
769 | VDEV_TOP_ZAP_INDIRECT_OBSOLETE_SM, tx)); | |
770 | ||
771 | VERIFY0(zap_add(spa->spa_dsl_pool->dp_meta_objset, | |
772 | DMU_POOL_DIRECTORY_OBJECT, | |
773 | DMU_POOL_CONDENSING_INDIRECT, sizeof (uint64_t), | |
774 | sizeof (*scip) / sizeof (uint64_t), scip, tx)); | |
775 | ||
776 | ASSERT3P(spa->spa_condensing_indirect, ==, NULL); | |
777 | spa->spa_condensing_indirect = spa_condensing_indirect_create(spa); | |
778 | ||
779 | zfs_dbgmsg("starting condense of vdev %llu in txg %llu: " | |
780 | "posm=%llu nm=%llu", | |
781 | vd->vdev_id, dmu_tx_get_txg(tx), | |
782 | (u_longlong_t)scip->scip_prev_obsolete_sm_object, | |
783 | (u_longlong_t)scip->scip_next_mapping_object); | |
784 | ||
9d5b5245 | 785 | zthr_wakeup(spa->spa_condense_zthr); |
a1d477c2 MA |
786 | } |
787 | ||
788 | /* | |
789 | * Sync to the given vdev's obsolete space map any segments that are no longer | |
790 | * referenced as of the given txg. | |
791 | * | |
792 | * If the obsolete space map doesn't exist yet, create and open it. | |
793 | */ | |
794 | void | |
795 | vdev_indirect_sync_obsolete(vdev_t *vd, dmu_tx_t *tx) | |
796 | { | |
797 | spa_t *spa = vd->vdev_spa; | |
798 | ASSERTV(vdev_indirect_config_t *vic = &vd->vdev_indirect_config); | |
799 | ||
800 | ASSERT3U(vic->vic_mapping_object, !=, 0); | |
801 | ASSERT(range_tree_space(vd->vdev_obsolete_segments) > 0); | |
802 | ASSERT(vd->vdev_removing || vd->vdev_ops == &vdev_indirect_ops); | |
803 | ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS)); | |
804 | ||
805 | if (vdev_obsolete_sm_object(vd) == 0) { | |
806 | uint64_t obsolete_sm_object = | |
807 | space_map_alloc(spa->spa_meta_objset, tx); | |
808 | ||
809 | ASSERT(vd->vdev_top_zap != 0); | |
810 | VERIFY0(zap_add(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap, | |
811 | VDEV_TOP_ZAP_INDIRECT_OBSOLETE_SM, | |
812 | sizeof (obsolete_sm_object), 1, &obsolete_sm_object, tx)); | |
813 | ASSERT3U(vdev_obsolete_sm_object(vd), !=, 0); | |
814 | ||
815 | spa_feature_incr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); | |
816 | VERIFY0(space_map_open(&vd->vdev_obsolete_sm, | |
817 | spa->spa_meta_objset, obsolete_sm_object, | |
818 | 0, vd->vdev_asize, 0)); | |
819 | space_map_update(vd->vdev_obsolete_sm); | |
820 | } | |
821 | ||
822 | ASSERT(vd->vdev_obsolete_sm != NULL); | |
823 | ASSERT3U(vdev_obsolete_sm_object(vd), ==, | |
824 | space_map_object(vd->vdev_obsolete_sm)); | |
825 | ||
826 | space_map_write(vd->vdev_obsolete_sm, | |
827 | vd->vdev_obsolete_segments, SM_ALLOC, tx); | |
828 | space_map_update(vd->vdev_obsolete_sm); | |
829 | range_tree_vacate(vd->vdev_obsolete_segments, NULL, NULL); | |
830 | } | |
831 | ||
832 | int | |
833 | spa_condense_init(spa_t *spa) | |
834 | { | |
835 | int error = zap_lookup(spa->spa_meta_objset, | |
836 | DMU_POOL_DIRECTORY_OBJECT, | |
837 | DMU_POOL_CONDENSING_INDIRECT, sizeof (uint64_t), | |
838 | sizeof (spa->spa_condensing_indirect_phys) / sizeof (uint64_t), | |
839 | &spa->spa_condensing_indirect_phys); | |
840 | if (error == 0) { | |
841 | if (spa_writeable(spa)) { | |
842 | spa->spa_condensing_indirect = | |
843 | spa_condensing_indirect_create(spa); | |
844 | } | |
845 | return (0); | |
846 | } else if (error == ENOENT) { | |
847 | return (0); | |
848 | } else { | |
849 | return (error); | |
850 | } | |
851 | } | |
852 | ||
853 | void | |
854 | spa_condense_fini(spa_t *spa) | |
855 | { | |
856 | if (spa->spa_condensing_indirect != NULL) { | |
857 | spa_condensing_indirect_destroy(spa->spa_condensing_indirect); | |
858 | spa->spa_condensing_indirect = NULL; | |
859 | } | |
860 | } | |
861 | ||
a1d477c2 | 862 | void |
9d5b5245 | 863 | spa_start_indirect_condensing_thread(spa_t *spa) |
a1d477c2 | 864 | { |
9d5b5245 SD |
865 | ASSERT3P(spa->spa_condense_zthr, ==, NULL); |
866 | spa->spa_condense_zthr = zthr_create(spa_condense_indirect_thread_check, | |
867 | spa_condense_indirect_thread, spa); | |
a1d477c2 MA |
868 | } |
869 | ||
870 | /* | |
871 | * Gets the obsolete spacemap object from the vdev's ZAP. | |
872 | * Returns the spacemap object, or 0 if it wasn't in the ZAP or the ZAP doesn't | |
873 | * exist yet. | |
874 | */ | |
875 | int | |
876 | vdev_obsolete_sm_object(vdev_t *vd) | |
877 | { | |
878 | ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); | |
879 | if (vd->vdev_top_zap == 0) { | |
880 | return (0); | |
881 | } | |
882 | ||
883 | uint64_t sm_obj = 0; | |
884 | int err; | |
885 | err = zap_lookup(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap, | |
886 | VDEV_TOP_ZAP_INDIRECT_OBSOLETE_SM, sizeof (sm_obj), 1, &sm_obj); | |
887 | ||
888 | ASSERT(err == 0 || err == ENOENT); | |
889 | ||
890 | return (sm_obj); | |
891 | } | |
892 | ||
893 | boolean_t | |
894 | vdev_obsolete_counts_are_precise(vdev_t *vd) | |
895 | { | |
896 | ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); | |
897 | if (vd->vdev_top_zap == 0) { | |
898 | return (B_FALSE); | |
899 | } | |
900 | ||
901 | uint64_t val = 0; | |
902 | int err; | |
903 | err = zap_lookup(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap, | |
904 | VDEV_TOP_ZAP_OBSOLETE_COUNTS_ARE_PRECISE, sizeof (val), 1, &val); | |
905 | ||
906 | ASSERT(err == 0 || err == ENOENT); | |
907 | ||
908 | return (val != 0); | |
909 | } | |
910 | ||
911 | /* ARGSUSED */ | |
912 | static void | |
913 | vdev_indirect_close(vdev_t *vd) | |
914 | { | |
915 | } | |
916 | ||
a1d477c2 MA |
917 | /* ARGSUSED */ |
918 | static int | |
919 | vdev_indirect_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize, | |
920 | uint64_t *ashift) | |
921 | { | |
922 | *psize = *max_psize = vd->vdev_asize + | |
923 | VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; | |
924 | *ashift = vd->vdev_ashift; | |
925 | return (0); | |
926 | } | |
927 | ||
928 | typedef struct remap_segment { | |
929 | vdev_t *rs_vd; | |
930 | uint64_t rs_offset; | |
931 | uint64_t rs_asize; | |
932 | uint64_t rs_split_offset; | |
933 | list_node_t rs_node; | |
934 | } remap_segment_t; | |
935 | ||
936 | remap_segment_t * | |
937 | rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset) | |
938 | { | |
939 | remap_segment_t *rs = kmem_alloc(sizeof (remap_segment_t), KM_SLEEP); | |
940 | rs->rs_vd = vd; | |
941 | rs->rs_offset = offset; | |
942 | rs->rs_asize = asize; | |
943 | rs->rs_split_offset = split_offset; | |
944 | return (rs); | |
945 | } | |
946 | ||
4bf8108e SD |
947 | /* |
948 | * Given an indirect vdev and an extent on that vdev, it duplicates the | |
949 | * physical entries of the indirect mapping that correspond to the extent | |
950 | * to a new array and returns a pointer to it. In addition, copied_entries | |
951 | * is populated with the number of mapping entries that were duplicated. | |
952 | * | |
953 | * Note that the function assumes that the caller holds vdev_indirect_rwlock. | |
954 | * This ensures that the mapping won't change due to condensing as we | |
955 | * copy over its contents. | |
956 | * | |
957 | * Finally, since we are doing an allocation, it is up to the caller to | |
958 | * free the array allocated in this function. | |
959 | */ | |
960 | vdev_indirect_mapping_entry_phys_t * | |
961 | vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset, | |
962 | uint64_t asize, uint64_t *copied_entries) | |
963 | { | |
964 | vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL; | |
965 | vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; | |
966 | uint64_t entries = 0; | |
967 | ||
968 | ASSERT(RW_READ_HELD(&vd->vdev_indirect_rwlock)); | |
969 | ||
970 | vdev_indirect_mapping_entry_phys_t *first_mapping = | |
971 | vdev_indirect_mapping_entry_for_offset(vim, offset); | |
972 | ASSERT3P(first_mapping, !=, NULL); | |
973 | ||
974 | vdev_indirect_mapping_entry_phys_t *m = first_mapping; | |
975 | while (asize > 0) { | |
976 | uint64_t size = DVA_GET_ASIZE(&m->vimep_dst); | |
977 | ||
978 | ASSERT3U(offset, >=, DVA_MAPPING_GET_SRC_OFFSET(m)); | |
979 | ASSERT3U(offset, <, DVA_MAPPING_GET_SRC_OFFSET(m) + size); | |
980 | ||
981 | uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m); | |
982 | uint64_t inner_size = MIN(asize, size - inner_offset); | |
983 | ||
984 | offset += inner_size; | |
985 | asize -= inner_size; | |
986 | entries++; | |
987 | m++; | |
988 | } | |
989 | ||
990 | size_t copy_length = entries * sizeof (*first_mapping); | |
991 | duplicate_mappings = kmem_alloc(copy_length, KM_SLEEP); | |
992 | bcopy(first_mapping, duplicate_mappings, copy_length); | |
993 | *copied_entries = entries; | |
994 | ||
995 | return (duplicate_mappings); | |
996 | } | |
997 | ||
a1d477c2 MA |
998 | /* |
999 | * Goes through the relevant indirect mappings until it hits a concrete vdev | |
1000 | * and issues the callback. On the way to the concrete vdev, if any other | |
1001 | * indirect vdevs are encountered, then the callback will also be called on | |
1002 | * each of those indirect vdevs. For example, if the segment is mapped to | |
1003 | * segment A on indirect vdev 1, and then segment A on indirect vdev 1 is | |
1004 | * mapped to segment B on concrete vdev 2, then the callback will be called on | |
1005 | * both vdev 1 and vdev 2. | |
1006 | * | |
1007 | * While the callback passed to vdev_indirect_remap() is called on every vdev | |
1008 | * the function encounters, certain callbacks only care about concrete vdevs. | |
1009 | * These types of callbacks should return immediately and explicitly when they | |
1010 | * are called on an indirect vdev. | |
1011 | * | |
1012 | * Because there is a possibility that a DVA section in the indirect device | |
1013 | * has been split into multiple sections in our mapping, we keep track | |
1014 | * of the relevant contiguous segments of the new location (remap_segment_t) | |
1015 | * in a stack. This way we can call the callback for each of the new sections | |
1016 | * created by a single section of the indirect device. Note though, that in | |
1017 | * this scenario the callbacks in each split block won't occur in-order in | |
1018 | * terms of offset, so callers should not make any assumptions about that. | |
1019 | * | |
1020 | * For callbacks that don't handle split blocks and immediately return when | |
1021 | * they encounter them (as is the case for remap_blkptr_cb), the caller can | |
1022 | * assume that its callback will be applied from the first indirect vdev | |
1023 | * encountered to the last one and then the concrete vdev, in that order. | |
1024 | */ | |
1025 | static void | |
1026 | vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, | |
1027 | void (*func)(uint64_t, vdev_t *, uint64_t, uint64_t, void *), void *arg) | |
1028 | { | |
1029 | list_t stack; | |
1030 | spa_t *spa = vd->vdev_spa; | |
1031 | ||
1032 | list_create(&stack, sizeof (remap_segment_t), | |
1033 | offsetof(remap_segment_t, rs_node)); | |
1034 | ||
1035 | for (remap_segment_t *rs = rs_alloc(vd, offset, asize, 0); | |
1036 | rs != NULL; rs = list_remove_head(&stack)) { | |
1037 | vdev_t *v = rs->rs_vd; | |
4bf8108e SD |
1038 | uint64_t num_entries = 0; |
1039 | ||
1040 | ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); | |
1041 | ASSERT(rs->rs_asize > 0); | |
a1d477c2 MA |
1042 | |
1043 | /* | |
4bf8108e SD |
1044 | * Note: As this function can be called from open context |
1045 | * (e.g. zio_read()), we need the following rwlock to | |
1046 | * prevent the mapping from being changed by condensing. | |
1047 | * | |
1048 | * So we grab the lock and we make a copy of the entries | |
1049 | * that are relevant to the extent that we are working on. | |
1050 | * Once that is done, we drop the lock and iterate over | |
1051 | * our copy of the mapping. Once we are done with the with | |
1052 | * the remap segment and we free it, we also free our copy | |
1053 | * of the indirect mapping entries that are relevant to it. | |
1054 | * | |
1055 | * This way we don't need to wait until the function is | |
1056 | * finished with a segment, to condense it. In addition, we | |
1057 | * don't need a recursive rwlock for the case that a call to | |
1058 | * vdev_indirect_remap() needs to call itself (through the | |
1059 | * codepath of its callback) for the same vdev in the middle | |
1060 | * of its execution. | |
a1d477c2 MA |
1061 | */ |
1062 | rw_enter(&v->vdev_indirect_rwlock, RW_READER); | |
4bf8108e | 1063 | ASSERT3P(v->vdev_indirect_mapping, !=, NULL); |
a1d477c2 MA |
1064 | |
1065 | vdev_indirect_mapping_entry_phys_t *mapping = | |
4bf8108e SD |
1066 | vdev_indirect_mapping_duplicate_adjacent_entries(v, |
1067 | rs->rs_offset, rs->rs_asize, &num_entries); | |
a1d477c2 | 1068 | ASSERT3P(mapping, !=, NULL); |
4bf8108e SD |
1069 | ASSERT3U(num_entries, >, 0); |
1070 | rw_exit(&v->vdev_indirect_rwlock); | |
a1d477c2 | 1071 | |
4bf8108e | 1072 | for (uint64_t i = 0; i < num_entries; i++) { |
a1d477c2 MA |
1073 | /* |
1074 | * Note: the vdev_indirect_mapping can not change | |
1075 | * while we are running. It only changes while the | |
1076 | * removal is in progress, and then only from syncing | |
1077 | * context. While a removal is in progress, this | |
1078 | * function is only called for frees, which also only | |
1079 | * happen from syncing context. | |
1080 | */ | |
4bf8108e SD |
1081 | vdev_indirect_mapping_entry_phys_t *m = &mapping[i]; |
1082 | ||
1083 | ASSERT3P(m, !=, NULL); | |
1084 | ASSERT3U(rs->rs_asize, >, 0); | |
a1d477c2 | 1085 | |
4bf8108e SD |
1086 | uint64_t size = DVA_GET_ASIZE(&m->vimep_dst); |
1087 | uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst); | |
1088 | uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst); | |
a1d477c2 MA |
1089 | |
1090 | ASSERT3U(rs->rs_offset, >=, | |
4bf8108e | 1091 | DVA_MAPPING_GET_SRC_OFFSET(m)); |
a1d477c2 | 1092 | ASSERT3U(rs->rs_offset, <, |
4bf8108e | 1093 | DVA_MAPPING_GET_SRC_OFFSET(m) + size); |
a1d477c2 MA |
1094 | ASSERT3U(dst_vdev, !=, v->vdev_id); |
1095 | ||
1096 | uint64_t inner_offset = rs->rs_offset - | |
4bf8108e | 1097 | DVA_MAPPING_GET_SRC_OFFSET(m); |
a1d477c2 MA |
1098 | uint64_t inner_size = |
1099 | MIN(rs->rs_asize, size - inner_offset); | |
1100 | ||
1101 | vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev); | |
1102 | ASSERT3P(dst_v, !=, NULL); | |
1103 | ||
1104 | if (dst_v->vdev_ops == &vdev_indirect_ops) { | |
1105 | list_insert_head(&stack, | |
1106 | rs_alloc(dst_v, dst_offset + inner_offset, | |
1107 | inner_size, rs->rs_split_offset)); | |
1108 | ||
1109 | } | |
1110 | ||
1111 | if ((zfs_flags & ZFS_DEBUG_INDIRECT_REMAP) && | |
1112 | IS_P2ALIGNED(inner_size, 2 * SPA_MINBLOCKSIZE)) { | |
1113 | /* | |
1114 | * Note: This clause exists only solely for | |
1115 | * testing purposes. We use it to ensure that | |
1116 | * split blocks work and that the callbacks | |
1117 | * using them yield the same result if issued | |
1118 | * in reverse order. | |
1119 | */ | |
1120 | uint64_t inner_half = inner_size / 2; | |
1121 | ||
1122 | func(rs->rs_split_offset + inner_half, dst_v, | |
1123 | dst_offset + inner_offset + inner_half, | |
1124 | inner_half, arg); | |
1125 | ||
1126 | func(rs->rs_split_offset, dst_v, | |
1127 | dst_offset + inner_offset, | |
1128 | inner_half, arg); | |
1129 | } else { | |
1130 | func(rs->rs_split_offset, dst_v, | |
1131 | dst_offset + inner_offset, | |
1132 | inner_size, arg); | |
1133 | } | |
1134 | ||
1135 | rs->rs_offset += inner_size; | |
1136 | rs->rs_asize -= inner_size; | |
1137 | rs->rs_split_offset += inner_size; | |
a1d477c2 | 1138 | } |
4bf8108e | 1139 | VERIFY0(rs->rs_asize); |
a1d477c2 | 1140 | |
4bf8108e | 1141 | kmem_free(mapping, num_entries * sizeof (*mapping)); |
a1d477c2 MA |
1142 | kmem_free(rs, sizeof (remap_segment_t)); |
1143 | } | |
1144 | list_destroy(&stack); | |
1145 | } | |
1146 | ||
1147 | static void | |
1148 | vdev_indirect_child_io_done(zio_t *zio) | |
1149 | { | |
1150 | zio_t *pio = zio->io_private; | |
1151 | ||
1152 | mutex_enter(&pio->io_lock); | |
1153 | pio->io_error = zio_worst_error(pio->io_error, zio->io_error); | |
1154 | mutex_exit(&pio->io_lock); | |
1155 | ||
1156 | abd_put(zio->io_abd); | |
1157 | } | |
1158 | ||
9e052db4 MA |
1159 | /* |
1160 | * This is a callback for vdev_indirect_remap() which allocates an | |
1161 | * indirect_split_t for each split segment and adds it to iv_splits. | |
1162 | */ | |
a1d477c2 | 1163 | static void |
9e052db4 | 1164 | vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset, |
a1d477c2 MA |
1165 | uint64_t size, void *arg) |
1166 | { | |
1167 | zio_t *zio = arg; | |
9e052db4 | 1168 | indirect_vsd_t *iv = zio->io_vsd; |
a1d477c2 MA |
1169 | |
1170 | ASSERT3P(vd, !=, NULL); | |
1171 | ||
1172 | if (vd->vdev_ops == &vdev_indirect_ops) | |
1173 | return; | |
1174 | ||
9e052db4 MA |
1175 | int n = 1; |
1176 | if (vd->vdev_ops == &vdev_mirror_ops) | |
1177 | n = vd->vdev_children; | |
1178 | ||
1179 | indirect_split_t *is = | |
1180 | kmem_zalloc(offsetof(indirect_split_t, is_child[n]), KM_SLEEP); | |
1181 | ||
1182 | is->is_children = n; | |
1183 | is->is_size = size; | |
1184 | is->is_split_offset = split_offset; | |
1185 | is->is_target_offset = offset; | |
1186 | is->is_vdev = vd; | |
1187 | ||
1188 | /* | |
1189 | * Note that we only consider multiple copies of the data for | |
1190 | * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even | |
1191 | * though they use the same ops as mirror, because there's only one | |
1192 | * "good" copy under the replacing/spare. | |
1193 | */ | |
1194 | if (vd->vdev_ops == &vdev_mirror_ops) { | |
1195 | for (int i = 0; i < n; i++) { | |
1196 | is->is_child[i].ic_vdev = vd->vdev_child[i]; | |
1197 | } | |
1198 | } else { | |
1199 | is->is_child[0].ic_vdev = vd; | |
1200 | } | |
1201 | ||
1202 | list_insert_tail(&iv->iv_splits, is); | |
1203 | } | |
1204 | ||
1205 | static void | |
1206 | vdev_indirect_read_split_done(zio_t *zio) | |
1207 | { | |
1208 | indirect_child_t *ic = zio->io_private; | |
1209 | ||
1210 | if (zio->io_error != 0) { | |
1211 | /* | |
1212 | * Clear ic_data to indicate that we do not have data for this | |
1213 | * child. | |
1214 | */ | |
1215 | abd_free(ic->ic_data); | |
1216 | ic->ic_data = NULL; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | /* | |
1221 | * Issue reads for all copies (mirror children) of all splits. | |
1222 | */ | |
1223 | static void | |
1224 | vdev_indirect_read_all(zio_t *zio) | |
1225 | { | |
1226 | indirect_vsd_t *iv = zio->io_vsd; | |
1227 | ||
1228 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1229 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1230 | for (int i = 0; i < is->is_children; i++) { | |
1231 | indirect_child_t *ic = &is->is_child[i]; | |
1232 | ||
1233 | if (!vdev_readable(ic->ic_vdev)) | |
1234 | continue; | |
1235 | ||
1236 | /* | |
1237 | * Note, we may read from a child whose DTL | |
1238 | * indicates that the data may not be present here. | |
1239 | * While this might result in a few i/os that will | |
1240 | * likely return incorrect data, it simplifies the | |
1241 | * code since we can treat scrub and resilver | |
1242 | * identically. (The incorrect data will be | |
1243 | * detected and ignored when we verify the | |
1244 | * checksum.) | |
1245 | */ | |
1246 | ||
1247 | ic->ic_data = abd_alloc_sametype(zio->io_abd, | |
1248 | is->is_size); | |
4589f3ae | 1249 | ic->ic_duplicate = -1; |
9e052db4 MA |
1250 | |
1251 | zio_nowait(zio_vdev_child_io(zio, NULL, | |
1252 | ic->ic_vdev, is->is_target_offset, ic->ic_data, | |
1253 | is->is_size, zio->io_type, zio->io_priority, 0, | |
1254 | vdev_indirect_read_split_done, ic)); | |
1255 | } | |
1256 | } | |
1257 | iv->iv_reconstruct = B_TRUE; | |
a1d477c2 MA |
1258 | } |
1259 | ||
1260 | static void | |
1261 | vdev_indirect_io_start(zio_t *zio) | |
1262 | { | |
1263 | ASSERTV(spa_t *spa = zio->io_spa); | |
9e052db4 MA |
1264 | indirect_vsd_t *iv = kmem_zalloc(sizeof (*iv), KM_SLEEP); |
1265 | list_create(&iv->iv_splits, | |
1266 | sizeof (indirect_split_t), offsetof(indirect_split_t, is_node)); | |
1267 | ||
1268 | zio->io_vsd = iv; | |
1269 | zio->io_vsd_ops = &vdev_indirect_vsd_ops; | |
a1d477c2 MA |
1270 | |
1271 | ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); | |
1272 | if (zio->io_type != ZIO_TYPE_READ) { | |
1273 | ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE); | |
9e052db4 MA |
1274 | /* |
1275 | * Note: this code can handle other kinds of writes, | |
1276 | * but we don't expect them. | |
1277 | */ | |
1278 | ASSERT((zio->io_flags & (ZIO_FLAG_SELF_HEAL | | |
1279 | ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE)) != 0); | |
a1d477c2 MA |
1280 | } |
1281 | ||
1282 | vdev_indirect_remap(zio->io_vd, zio->io_offset, zio->io_size, | |
9e052db4 MA |
1283 | vdev_indirect_gather_splits, zio); |
1284 | ||
1285 | indirect_split_t *first = list_head(&iv->iv_splits); | |
1286 | if (first->is_size == zio->io_size) { | |
1287 | /* | |
1288 | * This is not a split block; we are pointing to the entire | |
1289 | * data, which will checksum the same as the original data. | |
1290 | * Pass the BP down so that the child i/o can verify the | |
1291 | * checksum, and try a different location if available | |
1292 | * (e.g. on a mirror). | |
1293 | * | |
1294 | * While this special case could be handled the same as the | |
1295 | * general (split block) case, doing it this way ensures | |
1296 | * that the vast majority of blocks on indirect vdevs | |
1297 | * (which are not split) are handled identically to blocks | |
1298 | * on non-indirect vdevs. This allows us to be less strict | |
1299 | * about performance in the general (but rare) case. | |
1300 | */ | |
1301 | ASSERT0(first->is_split_offset); | |
1302 | ASSERT3P(list_next(&iv->iv_splits, first), ==, NULL); | |
1303 | zio_nowait(zio_vdev_child_io(zio, zio->io_bp, | |
1304 | first->is_vdev, first->is_target_offset, | |
1305 | abd_get_offset(zio->io_abd, 0), | |
1306 | zio->io_size, zio->io_type, zio->io_priority, 0, | |
1307 | vdev_indirect_child_io_done, zio)); | |
1308 | } else { | |
1309 | iv->iv_split_block = B_TRUE; | |
1310 | if (zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER)) { | |
1311 | /* | |
1312 | * Read all copies. Note that for simplicity, | |
1313 | * we don't bother consulting the DTL in the | |
1314 | * resilver case. | |
1315 | */ | |
1316 | vdev_indirect_read_all(zio); | |
1317 | } else { | |
1318 | /* | |
1319 | * Read one copy of each split segment, from the | |
1320 | * top-level vdev. Since we don't know the | |
1321 | * checksum of each split individually, the child | |
1322 | * zio can't ensure that we get the right data. | |
1323 | * E.g. if it's a mirror, it will just read from a | |
1324 | * random (healthy) leaf vdev. We have to verify | |
1325 | * the checksum in vdev_indirect_io_done(). | |
1326 | */ | |
1327 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1328 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1329 | zio_nowait(zio_vdev_child_io(zio, NULL, | |
1330 | is->is_vdev, is->is_target_offset, | |
1331 | abd_get_offset(zio->io_abd, | |
1332 | is->is_split_offset), is->is_size, | |
1333 | zio->io_type, zio->io_priority, 0, | |
1334 | vdev_indirect_child_io_done, zio)); | |
1335 | } | |
1336 | ||
1337 | } | |
1338 | } | |
a1d477c2 MA |
1339 | |
1340 | zio_execute(zio); | |
1341 | } | |
1342 | ||
9e052db4 MA |
1343 | /* |
1344 | * Report a checksum error for a child. | |
1345 | */ | |
1346 | static void | |
1347 | vdev_indirect_checksum_error(zio_t *zio, | |
1348 | indirect_split_t *is, indirect_child_t *ic) | |
1349 | { | |
1350 | vdev_t *vd = ic->ic_vdev; | |
1351 | ||
1352 | if (zio->io_flags & ZIO_FLAG_SPECULATIVE) | |
1353 | return; | |
1354 | ||
1355 | mutex_enter(&vd->vdev_stat_lock); | |
1356 | vd->vdev_stat.vs_checksum_errors++; | |
1357 | mutex_exit(&vd->vdev_stat_lock); | |
1358 | ||
1359 | zio_bad_cksum_t zbc = {{{ 0 }}}; | |
1360 | abd_t *bad_abd = ic->ic_data; | |
1361 | abd_t *good_abd = is->is_child[is->is_good_child].ic_data; | |
1362 | zfs_ereport_post_checksum(zio->io_spa, vd, NULL, zio, | |
1363 | is->is_target_offset, is->is_size, good_abd, bad_abd, &zbc); | |
1364 | } | |
1365 | ||
1366 | /* | |
1367 | * Issue repair i/os for any incorrect copies. We do this by comparing | |
1368 | * each split segment's correct data (is_good_child's ic_data) with each | |
1369 | * other copy of the data. If they differ, then we overwrite the bad data | |
1370 | * with the good copy. Note that we do this without regard for the DTL's, | |
1371 | * which simplifies this code and also issues the optimal number of writes | |
1372 | * (based on which copies actually read bad data, as opposed to which we | |
1373 | * think might be wrong). For the same reason, we always use | |
1374 | * ZIO_FLAG_SELF_HEAL, to bypass the DTL check in zio_vdev_io_start(). | |
1375 | */ | |
1376 | static void | |
1377 | vdev_indirect_repair(zio_t *zio) | |
1378 | { | |
1379 | indirect_vsd_t *iv = zio->io_vsd; | |
1380 | ||
1381 | enum zio_flag flags = ZIO_FLAG_IO_REPAIR; | |
1382 | ||
1383 | if (!(zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) | |
1384 | flags |= ZIO_FLAG_SELF_HEAL; | |
1385 | ||
1386 | if (!spa_writeable(zio->io_spa)) | |
1387 | return; | |
1388 | ||
1389 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1390 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1391 | indirect_child_t *good_child = &is->is_child[is->is_good_child]; | |
1392 | ||
1393 | for (int c = 0; c < is->is_children; c++) { | |
1394 | indirect_child_t *ic = &is->is_child[c]; | |
1395 | if (ic == good_child) | |
1396 | continue; | |
1397 | if (ic->ic_data == NULL) | |
1398 | continue; | |
4589f3ae | 1399 | if (ic->ic_duplicate == is->is_good_child) |
9e052db4 MA |
1400 | continue; |
1401 | ||
1402 | zio_nowait(zio_vdev_child_io(zio, NULL, | |
1403 | ic->ic_vdev, is->is_target_offset, | |
1404 | good_child->ic_data, is->is_size, | |
1405 | ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE, | |
1406 | ZIO_FLAG_IO_REPAIR | ZIO_FLAG_SELF_HEAL, | |
1407 | NULL, NULL)); | |
1408 | ||
1409 | vdev_indirect_checksum_error(zio, is, ic); | |
1410 | } | |
1411 | } | |
1412 | } | |
1413 | ||
1414 | /* | |
1415 | * Report checksum errors on all children that we read from. | |
1416 | */ | |
1417 | static void | |
1418 | vdev_indirect_all_checksum_errors(zio_t *zio) | |
1419 | { | |
1420 | indirect_vsd_t *iv = zio->io_vsd; | |
1421 | ||
1422 | if (zio->io_flags & ZIO_FLAG_SPECULATIVE) | |
1423 | return; | |
1424 | ||
1425 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1426 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1427 | for (int c = 0; c < is->is_children; c++) { | |
1428 | indirect_child_t *ic = &is->is_child[c]; | |
1429 | ||
1430 | if (ic->ic_data == NULL) | |
1431 | continue; | |
1432 | ||
1433 | vdev_t *vd = ic->ic_vdev; | |
1434 | ||
1435 | mutex_enter(&vd->vdev_stat_lock); | |
1436 | vd->vdev_stat.vs_checksum_errors++; | |
1437 | mutex_exit(&vd->vdev_stat_lock); | |
1438 | ||
1439 | zfs_ereport_post_checksum(zio->io_spa, vd, NULL, zio, | |
1440 | is->is_target_offset, is->is_size, | |
1441 | NULL, NULL, NULL); | |
1442 | } | |
1443 | } | |
1444 | } | |
1445 | ||
1446 | /* | |
1447 | * This function is called when we have read all copies of the data and need | |
1448 | * to try to find a combination of copies that gives us the right checksum. | |
1449 | * | |
1450 | * If we pointed to any mirror vdevs, this effectively does the job of the | |
1451 | * mirror. The mirror vdev code can't do its own job because we don't know | |
4589f3ae | 1452 | * the checksum of each split segment individually. |
9e052db4 | 1453 | * |
4589f3ae BB |
1454 | * We have to try every unique combination of copies of split segments, until |
1455 | * we find one that checksums correctly. Duplicate segment copies are first | |
1456 | * discarded as an optimization to reduce the search space. After pruning | |
1457 | * there will exist at most one valid combination. | |
1458 | * | |
1459 | * When the total number of combinations is small they can all be checked. | |
1460 | * For example, if we have 3 segments in the split, and each points to a | |
1461 | * 2-way mirror with unique copies, we will have the following pieces of data: | |
9e052db4 MA |
1462 | * |
1463 | * | mirror child | |
1464 | * split | [0] [1] | |
1465 | * ======|===================== | |
1466 | * A | data_A_0 data_A_1 | |
1467 | * B | data_B_0 data_B_1 | |
1468 | * C | data_C_0 data_C_1 | |
1469 | * | |
1470 | * We will try the following (mirror children)^(number of splits) (2^3=8) | |
1471 | * combinations, which is similar to bitwise-little-endian counting in | |
1472 | * binary. In general each "digit" corresponds to a split segment, and the | |
1473 | * base of each digit is is_children, which can be different for each | |
1474 | * digit. | |
1475 | * | |
1476 | * "low bit" "high bit" | |
1477 | * v v | |
1478 | * data_A_0 data_B_0 data_C_0 | |
1479 | * data_A_1 data_B_0 data_C_0 | |
1480 | * data_A_0 data_B_1 data_C_0 | |
1481 | * data_A_1 data_B_1 data_C_0 | |
1482 | * data_A_0 data_B_0 data_C_1 | |
1483 | * data_A_1 data_B_0 data_C_1 | |
1484 | * data_A_0 data_B_1 data_C_1 | |
1485 | * data_A_1 data_B_1 data_C_1 | |
1486 | * | |
1487 | * Note that the split segments may be on the same or different top-level | |
1488 | * vdevs. In either case, we try lots of combinations (see | |
1489 | * zfs_reconstruct_indirect_segments_max). This ensures that if a mirror has | |
1490 | * small silent errors on all of its children, we can still reconstruct the | |
1491 | * correct data, as long as those errors are at sufficiently-separated | |
1492 | * offsets (specifically, separated by the largest block size - default of | |
1493 | * 128KB, but up to 16MB). | |
1494 | */ | |
1495 | static void | |
1496 | vdev_indirect_reconstruct_io_done(zio_t *zio) | |
1497 | { | |
1498 | indirect_vsd_t *iv = zio->io_vsd; | |
1499 | uint64_t attempts = 0; | |
4589f3ae BB |
1500 | uint64_t attempts_max = UINT64_MAX; |
1501 | uint64_t combinations = 1; | |
1502 | ||
1503 | if (zfs_reconstruct_indirect_combinations_max > 0) | |
1504 | attempts_max = zfs_reconstruct_indirect_combinations_max; | |
9e052db4 | 1505 | |
4589f3ae BB |
1506 | /* |
1507 | * Discard duplicate copies of split segments to minimize the | |
1508 | * number of unique combinations when attempting reconstruction. | |
1509 | */ | |
9e052db4 | 1510 | for (indirect_split_t *is = list_head(&iv->iv_splits); |
4589f3ae BB |
1511 | is != NULL; is = list_next(&iv->iv_splits, is)) { |
1512 | uint64_t is_copies = 0; | |
1513 | ||
1514 | for (int i = 0; i < is->is_children; i++) { | |
1515 | if (is->is_child[i].ic_data == NULL) | |
1516 | continue; | |
1517 | ||
1518 | for (int j = i + 1; j < is->is_children; j++) { | |
1519 | if (is->is_child[j].ic_data == NULL) | |
1520 | continue; | |
1521 | ||
1522 | if (is->is_child[j].ic_duplicate == -1 && | |
1523 | abd_cmp(is->is_child[i].ic_data, | |
1524 | is->is_child[j].ic_data) == 0) { | |
1525 | is->is_child[j].ic_duplicate = i; | |
1526 | } | |
1527 | } | |
1528 | ||
1529 | is_copies++; | |
1530 | } | |
1531 | ||
1532 | /* Reconstruction is impossible, no valid is->is_child[] */ | |
1533 | if (is_copies == 0) { | |
1534 | zio->io_error = EIO; | |
1535 | vdev_indirect_all_checksum_errors(zio); | |
1536 | zio_checksum_verified(zio); | |
1537 | return; | |
1538 | } | |
1539 | ||
1540 | combinations *= is_copies; | |
1541 | } | |
9e052db4 MA |
1542 | |
1543 | for (;;) { | |
1544 | /* copy data from splits to main zio */ | |
1545 | int ret; | |
1546 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1547 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1548 | ||
1549 | /* | |
1550 | * If this child failed, its ic_data will be NULL. | |
1551 | * Skip this combination. | |
1552 | */ | |
1553 | if (is->is_child[is->is_good_child].ic_data == NULL) { | |
1554 | ret = EIO; | |
1555 | goto next; | |
1556 | } | |
1557 | ||
4589f3ae BB |
1558 | /* |
1559 | * If this child is a duplicate, its is_duplicate will | |
1560 | * refer to the primary copy. Skip this combination. | |
1561 | */ | |
1562 | if (is->is_child[is->is_good_child].ic_duplicate >= 0) { | |
1563 | ret = ECKSUM; | |
1564 | goto next; | |
1565 | } | |
1566 | ||
9e052db4 MA |
1567 | abd_copy_off(zio->io_abd, |
1568 | is->is_child[is->is_good_child].ic_data, | |
1569 | is->is_split_offset, 0, is->is_size); | |
1570 | } | |
1571 | ||
1572 | /* See if this checksum matches. */ | |
1573 | zio_bad_cksum_t zbc; | |
1574 | ret = zio_checksum_error(zio, &zbc); | |
1575 | if (ret == 0) { | |
1576 | /* Found a matching checksum. Issue repair i/os. */ | |
1577 | vdev_indirect_repair(zio); | |
1578 | zio_checksum_verified(zio); | |
1579 | return; | |
1580 | } | |
1581 | ||
1582 | /* | |
1583 | * Checksum failed; try a different combination of split | |
1584 | * children. | |
1585 | */ | |
1586 | boolean_t more; | |
1587 | next: | |
1588 | more = B_FALSE; | |
4589f3ae | 1589 | if (combinations <= attempts_max) { |
9e052db4 | 1590 | /* |
4589f3ae BB |
1591 | * There are relatively few possible combinations, so |
1592 | * deterministically check them all. We do this by | |
1593 | * adding one to the first split's good_child. If it | |
1594 | * overflows, then "carry over" to the next split | |
1595 | * (like counting in base is_children, but each | |
1596 | * digit can have a different base). | |
9e052db4 MA |
1597 | */ |
1598 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1599 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
1600 | is->is_good_child++; | |
1601 | if (is->is_good_child < is->is_children) { | |
1602 | more = B_TRUE; | |
1603 | break; | |
1604 | } | |
1605 | is->is_good_child = 0; | |
1606 | } | |
1607 | } else if (++attempts < attempts_max) { | |
1608 | /* | |
1609 | * There are too many combinations to try all of them | |
1610 | * in a reasonable amount of time, so try a fixed | |
1611 | * number of random combinations, after which we'll | |
1612 | * consider the block unrecoverable. | |
1613 | */ | |
1614 | for (indirect_split_t *is = list_head(&iv->iv_splits); | |
1615 | is != NULL; is = list_next(&iv->iv_splits, is)) { | |
4589f3ae BB |
1616 | int c = spa_get_random(is->is_children); |
1617 | ||
1618 | while (is->is_child[c].ic_duplicate >= 0) | |
1619 | c = (c + 1) % is->is_children; | |
1620 | ||
1621 | is->is_good_child = c; | |
9e052db4 MA |
1622 | } |
1623 | more = B_TRUE; | |
1624 | } | |
1625 | if (!more) { | |
1626 | /* All combinations failed. */ | |
1627 | zio->io_error = ret; | |
1628 | vdev_indirect_all_checksum_errors(zio); | |
1629 | zio_checksum_verified(zio); | |
1630 | return; | |
1631 | } | |
1632 | } | |
1633 | } | |
1634 | ||
1635 | static void | |
1636 | vdev_indirect_io_done(zio_t *zio) | |
1637 | { | |
1638 | indirect_vsd_t *iv = zio->io_vsd; | |
1639 | ||
1640 | if (iv->iv_reconstruct) { | |
1641 | /* | |
1642 | * We have read all copies of the data (e.g. from mirrors), | |
1643 | * either because this was a scrub/resilver, or because the | |
1644 | * one-copy read didn't checksum correctly. | |
1645 | */ | |
1646 | vdev_indirect_reconstruct_io_done(zio); | |
1647 | return; | |
1648 | } | |
1649 | ||
1650 | if (!iv->iv_split_block) { | |
1651 | /* | |
1652 | * This was not a split block, so we passed the BP down, | |
1653 | * and the checksum was handled by the (one) child zio. | |
1654 | */ | |
1655 | return; | |
1656 | } | |
1657 | ||
1658 | zio_bad_cksum_t zbc; | |
1659 | int ret = zio_checksum_error(zio, &zbc); | |
1660 | if (ret == 0) { | |
1661 | zio_checksum_verified(zio); | |
1662 | return; | |
1663 | } | |
1664 | ||
1665 | /* | |
1666 | * The checksum didn't match. Read all copies of all splits, and | |
1667 | * then we will try to reconstruct. The next time | |
1668 | * vdev_indirect_io_done() is called, iv_reconstruct will be set. | |
1669 | */ | |
1670 | vdev_indirect_read_all(zio); | |
1671 | ||
1672 | zio_vdev_io_redone(zio); | |
1673 | } | |
1674 | ||
a1d477c2 MA |
1675 | vdev_ops_t vdev_indirect_ops = { |
1676 | vdev_indirect_open, | |
1677 | vdev_indirect_close, | |
1678 | vdev_default_asize, | |
1679 | vdev_indirect_io_start, | |
1680 | vdev_indirect_io_done, | |
1681 | NULL, | |
1682 | NULL, | |
1683 | NULL, | |
1684 | NULL, | |
1685 | vdev_indirect_remap, | |
1686 | VDEV_TYPE_INDIRECT, /* name of this vdev type */ | |
1687 | B_FALSE /* leaf vdev */ | |
1688 | }; | |
1689 | ||
1690 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
1691 | EXPORT_SYMBOL(rs_alloc); | |
1692 | EXPORT_SYMBOL(spa_condense_fini); | |
9d5b5245 | 1693 | EXPORT_SYMBOL(spa_start_indirect_condensing_thread); |
a1d477c2 MA |
1694 | EXPORT_SYMBOL(spa_condense_indirect_start_sync); |
1695 | EXPORT_SYMBOL(spa_condense_init); | |
1696 | EXPORT_SYMBOL(spa_vdev_indirect_mark_obsolete); | |
1697 | EXPORT_SYMBOL(vdev_indirect_mark_obsolete); | |
1698 | EXPORT_SYMBOL(vdev_indirect_should_condense); | |
1699 | EXPORT_SYMBOL(vdev_indirect_sync_obsolete); | |
1700 | EXPORT_SYMBOL(vdev_obsolete_counts_are_precise); | |
1701 | EXPORT_SYMBOL(vdev_obsolete_sm_object); | |
1702 | ||
1703 | /* CSTYLED */ | |
1704 | module_param(zfs_condense_min_mapping_bytes, ulong, 0644); | |
1705 | MODULE_PARM_DESC(zfs_condense_min_mapping_bytes, | |
1706 | "Minimum size of vdev mapping to condense"); | |
1707 | ||
1708 | module_param(zfs_condense_indirect_commit_entry_delay_ms, int, 0644); | |
1709 | MODULE_PARM_DESC(zfs_condense_indirect_commit_entry_delay_ms, | |
1710 | "Delay while condensing vdev mapping"); | |
9e052db4 | 1711 | |
4589f3ae BB |
1712 | module_param(zfs_reconstruct_indirect_combinations_max, int, 0644); |
1713 | MODULE_PARM_DESC(zfs_reconstruct_indirect_combinations_max, | |
1714 | "Maximum number of combinations when reconstructing split segments"); | |
a1d477c2 | 1715 | #endif |