]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/trim_map.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / trim_map.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
23  * All rights reserved.
24  */
25
26 #include <sys/zfs_context.h>
27 #include <sys/spa_impl.h>
28 #include <sys/vdev_impl.h>
29 #include <sys/trim_map.h>
30 #include <sys/time.h>
31
32 /*
33  * Calculate the zio end, upgrading based on ashift which would be
34  * done by zio_vdev_io_start.
35  *
36  * This makes free range consolidation much more effective
37  * than it would otherwise be as well as ensuring that entire
38  * blocks are invalidated by writes.
39  */
40 #define TRIM_ZIO_END(vd, offset, size)  (offset +               \
41         P2ROUNDUP(size, 1ULL << vd->vdev_top->vdev_ashift))
42
43 #define TRIM_MAP_SINC(tm, size)                                 \
44         atomic_add_64(&(tm)->tm_bytes, (size))
45
46 #define TRIM_MAP_SDEC(tm, size)                                 \
47         atomic_add_64(&(tm)->tm_bytes, -(size))
48
49 #define TRIM_MAP_QINC(tm)                                       \
50         atomic_inc_64(&(tm)->tm_pending);                       \
51
52 #define TRIM_MAP_QDEC(tm)                                       \
53         atomic_dec_64(&(tm)->tm_pending);
54
55 typedef struct trim_map {
56         list_t          tm_head;                /* List of segments sorted by txg. */
57         avl_tree_t      tm_queued_frees;        /* AVL tree of segments waiting for TRIM. */
58         avl_tree_t      tm_inflight_frees;      /* AVL tree of in-flight TRIMs. */
59         avl_tree_t      tm_inflight_writes;     /* AVL tree of in-flight writes. */
60         list_t          tm_pending_writes;      /* Writes blocked on in-flight frees. */
61         kmutex_t        tm_lock;
62         uint64_t        tm_pending;             /* Count of pending TRIMs. */
63         uint64_t        tm_bytes;               /* Total size in bytes of queued TRIMs. */
64 } trim_map_t;
65
66 typedef struct trim_seg {
67         avl_node_t      ts_node;        /* AVL node. */
68         list_node_t     ts_next;        /* List element. */
69         uint64_t        ts_start;       /* Starting offset of this segment. */
70         uint64_t        ts_end;         /* Ending offset (non-inclusive). */
71         uint64_t        ts_txg;         /* Segment creation txg. */
72         hrtime_t        ts_time;        /* Segment creation time. */
73 } trim_seg_t;
74
75 extern boolean_t zfs_trim_enabled;
76
77 static u_int trim_txg_delay = 32;
78 static u_int trim_timeout = 30;
79 static u_int trim_max_interval = 1;
80 /* Limit outstanding TRIMs to 2G (max size for a single TRIM request) */
81 static uint64_t trim_vdev_max_bytes = 2147483648;
82 /* Limit outstanding TRIMs to 64 (max ranges for a single TRIM request) */      
83 static u_int trim_vdev_max_pending = 64;
84
85 SYSCTL_DECL(_vfs_zfs);
86 SYSCTL_NODE(_vfs_zfs, OID_AUTO, trim, CTLFLAG_RD, 0, "ZFS TRIM");
87
88 TUNABLE_INT("vfs.zfs.trim.txg_delay", &trim_txg_delay);
89 SYSCTL_UINT(_vfs_zfs_trim, OID_AUTO, txg_delay, CTLFLAG_RWTUN, &trim_txg_delay,
90     0, "Delay TRIMs by up to this many TXGs");
91
92 TUNABLE_INT("vfs.zfs.trim.timeout", &trim_timeout);
93 SYSCTL_UINT(_vfs_zfs_trim, OID_AUTO, timeout, CTLFLAG_RWTUN, &trim_timeout, 0,
94     "Delay TRIMs by up to this many seconds");
95
96 TUNABLE_INT("vfs.zfs.trim.max_interval", &trim_max_interval);
97 SYSCTL_UINT(_vfs_zfs_trim, OID_AUTO, max_interval, CTLFLAG_RWTUN,
98     &trim_max_interval, 0,
99     "Maximum interval between TRIM queue processing (seconds)");
100
101 SYSCTL_DECL(_vfs_zfs_vdev);
102 TUNABLE_QUAD("vfs.zfs.vdev.trim_max_bytes", &trim_vdev_max_bytes);
103 SYSCTL_QUAD(_vfs_zfs_vdev, OID_AUTO, trim_max_bytes, CTLFLAG_RWTUN,
104     &trim_vdev_max_bytes, 0,
105     "Maximum pending TRIM bytes for a vdev");
106
107 TUNABLE_INT("vfs.zfs.vdev.trim_max_pending", &trim_vdev_max_pending);
108 SYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, trim_max_pending, CTLFLAG_RWTUN,
109     &trim_vdev_max_pending, 0,
110     "Maximum pending TRIM segments for a vdev");
111
112
113 static void trim_map_vdev_commit_done(spa_t *spa, vdev_t *vd);
114
115 static int
116 trim_map_seg_compare(const void *x1, const void *x2)
117 {
118         const trim_seg_t *s1 = x1;
119         const trim_seg_t *s2 = x2;
120
121         if (s1->ts_start < s2->ts_start) {
122                 if (s1->ts_end > s2->ts_start)
123                         return (0);
124                 return (-1);
125         }
126         if (s1->ts_start > s2->ts_start) {
127                 if (s1->ts_start < s2->ts_end)
128                         return (0);
129                 return (1);
130         }
131         return (0);
132 }
133
134 static int
135 trim_map_zio_compare(const void *x1, const void *x2)
136 {
137         const zio_t *z1 = x1;
138         const zio_t *z2 = x2;
139
140         if (z1->io_offset < z2->io_offset) {
141                 if (z1->io_offset + z1->io_size > z2->io_offset)
142                         return (0);
143                 return (-1);
144         }
145         if (z1->io_offset > z2->io_offset) {
146                 if (z1->io_offset < z2->io_offset + z2->io_size)
147                         return (0);
148                 return (1);
149         }
150         return (0);
151 }
152
153 void
154 trim_map_create(vdev_t *vd)
155 {
156         trim_map_t *tm;
157
158         ASSERT(vd->vdev_ops->vdev_op_leaf);
159
160         if (!zfs_trim_enabled)
161                 return;
162
163         tm = kmem_zalloc(sizeof (*tm), KM_SLEEP);
164         mutex_init(&tm->tm_lock, NULL, MUTEX_DEFAULT, NULL);
165         list_create(&tm->tm_head, sizeof (trim_seg_t),
166             offsetof(trim_seg_t, ts_next));
167         list_create(&tm->tm_pending_writes, sizeof (zio_t),
168             offsetof(zio_t, io_trim_link));
169         avl_create(&tm->tm_queued_frees, trim_map_seg_compare,
170             sizeof (trim_seg_t), offsetof(trim_seg_t, ts_node));
171         avl_create(&tm->tm_inflight_frees, trim_map_seg_compare,
172             sizeof (trim_seg_t), offsetof(trim_seg_t, ts_node));
173         avl_create(&tm->tm_inflight_writes, trim_map_zio_compare,
174             sizeof (zio_t), offsetof(zio_t, io_trim_node));
175         vd->vdev_trimmap = tm;
176 }
177
178 void
179 trim_map_destroy(vdev_t *vd)
180 {
181         trim_map_t *tm;
182         trim_seg_t *ts;
183
184         ASSERT(vd->vdev_ops->vdev_op_leaf);
185
186         if (!zfs_trim_enabled)
187                 return;
188
189         tm = vd->vdev_trimmap;
190         if (tm == NULL)
191                 return;
192
193         /*
194          * We may have been called before trim_map_vdev_commit_done()
195          * had a chance to run, so do it now to prune the remaining
196          * inflight frees.
197          */
198         trim_map_vdev_commit_done(vd->vdev_spa, vd);
199
200         mutex_enter(&tm->tm_lock);
201         while ((ts = list_head(&tm->tm_head)) != NULL) {
202                 avl_remove(&tm->tm_queued_frees, ts);
203                 list_remove(&tm->tm_head, ts);
204                 kmem_free(ts, sizeof (*ts));
205                 TRIM_MAP_SDEC(tm, ts->ts_end - ts->ts_start);
206                 TRIM_MAP_QDEC(tm);
207         }
208         mutex_exit(&tm->tm_lock);
209
210         avl_destroy(&tm->tm_queued_frees);
211         avl_destroy(&tm->tm_inflight_frees);
212         avl_destroy(&tm->tm_inflight_writes);
213         list_destroy(&tm->tm_pending_writes);
214         list_destroy(&tm->tm_head);
215         mutex_destroy(&tm->tm_lock);
216         kmem_free(tm, sizeof (*tm));
217         vd->vdev_trimmap = NULL;
218 }
219
220 static void
221 trim_map_segment_add(trim_map_t *tm, uint64_t start, uint64_t end, uint64_t txg)
222 {
223         avl_index_t where;
224         trim_seg_t tsearch, *ts_before, *ts_after, *ts;
225         boolean_t merge_before, merge_after;
226         hrtime_t time;
227
228         ASSERT(MUTEX_HELD(&tm->tm_lock));
229         VERIFY(start < end);
230
231         time = gethrtime();
232         tsearch.ts_start = start;
233         tsearch.ts_end = end;
234
235         ts = avl_find(&tm->tm_queued_frees, &tsearch, &where);
236         if (ts != NULL) {
237                 if (start < ts->ts_start)
238                         trim_map_segment_add(tm, start, ts->ts_start, txg);
239                 if (end > ts->ts_end)
240                         trim_map_segment_add(tm, ts->ts_end, end, txg);
241                 return;
242         }
243
244         ts_before = avl_nearest(&tm->tm_queued_frees, where, AVL_BEFORE);
245         ts_after = avl_nearest(&tm->tm_queued_frees, where, AVL_AFTER);
246
247         merge_before = (ts_before != NULL && ts_before->ts_end == start);
248         merge_after = (ts_after != NULL && ts_after->ts_start == end);
249
250         if (merge_before && merge_after) {
251                 TRIM_MAP_SINC(tm, ts_after->ts_start - ts_before->ts_end);
252                 TRIM_MAP_QDEC(tm);
253                 avl_remove(&tm->tm_queued_frees, ts_before);
254                 list_remove(&tm->tm_head, ts_before);
255                 ts_after->ts_start = ts_before->ts_start;
256                 ts_after->ts_txg = txg;
257                 ts_after->ts_time = time;
258                 kmem_free(ts_before, sizeof (*ts_before));
259         } else if (merge_before) {
260                 TRIM_MAP_SINC(tm, end - ts_before->ts_end);
261                 ts_before->ts_end = end;
262                 ts_before->ts_txg = txg;
263                 ts_before->ts_time = time;
264         } else if (merge_after) {
265                 TRIM_MAP_SINC(tm, ts_after->ts_start - start);
266                 ts_after->ts_start = start;
267                 ts_after->ts_txg = txg;
268                 ts_after->ts_time = time;
269         } else {
270                 TRIM_MAP_SINC(tm, end - start);
271                 TRIM_MAP_QINC(tm);
272                 ts = kmem_alloc(sizeof (*ts), KM_SLEEP);
273                 ts->ts_start = start;
274                 ts->ts_end = end;
275                 ts->ts_txg = txg;
276                 ts->ts_time = time;
277                 avl_insert(&tm->tm_queued_frees, ts, where);
278                 list_insert_tail(&tm->tm_head, ts);
279         }
280 }
281
282 static void
283 trim_map_segment_remove(trim_map_t *tm, trim_seg_t *ts, uint64_t start,
284     uint64_t end)
285 {
286         trim_seg_t *nts;
287         boolean_t left_over, right_over;
288
289         ASSERT(MUTEX_HELD(&tm->tm_lock));
290
291         left_over = (ts->ts_start < start);
292         right_over = (ts->ts_end > end);
293
294         TRIM_MAP_SDEC(tm, end - start);
295         if (left_over && right_over) {
296                 nts = kmem_alloc(sizeof (*nts), KM_SLEEP);
297                 nts->ts_start = end;
298                 nts->ts_end = ts->ts_end;
299                 nts->ts_txg = ts->ts_txg;
300                 nts->ts_time = ts->ts_time;
301                 ts->ts_end = start;
302                 avl_insert_here(&tm->tm_queued_frees, nts, ts, AVL_AFTER);
303                 list_insert_after(&tm->tm_head, ts, nts);
304                 TRIM_MAP_QINC(tm);
305         } else if (left_over) {
306                 ts->ts_end = start;
307         } else if (right_over) {
308                 ts->ts_start = end;
309         } else {
310                 avl_remove(&tm->tm_queued_frees, ts);
311                 list_remove(&tm->tm_head, ts);
312                 TRIM_MAP_QDEC(tm);
313                 kmem_free(ts, sizeof (*ts));
314         }
315 }
316
317 static void
318 trim_map_free_locked(trim_map_t *tm, uint64_t start, uint64_t end, uint64_t txg)
319 {
320         zio_t zsearch, *zs;
321
322         ASSERT(MUTEX_HELD(&tm->tm_lock));
323
324         zsearch.io_offset = start;
325         zsearch.io_size = end - start;
326
327         zs = avl_find(&tm->tm_inflight_writes, &zsearch, NULL);
328         if (zs == NULL) {
329                 trim_map_segment_add(tm, start, end, txg);
330                 return;
331         }
332         if (start < zs->io_offset)
333                 trim_map_free_locked(tm, start, zs->io_offset, txg);
334         if (zs->io_offset + zs->io_size < end)
335                 trim_map_free_locked(tm, zs->io_offset + zs->io_size, end, txg);
336 }
337
338 void
339 trim_map_free(vdev_t *vd, uint64_t offset, uint64_t size, uint64_t txg)
340 {
341         trim_map_t *tm = vd->vdev_trimmap;
342
343         if (!zfs_trim_enabled || vd->vdev_notrim || tm == NULL)
344                 return;
345
346         mutex_enter(&tm->tm_lock);
347         trim_map_free_locked(tm, offset, TRIM_ZIO_END(vd, offset, size), txg);
348         mutex_exit(&tm->tm_lock);
349 }
350
351 boolean_t
352 trim_map_write_start(zio_t *zio)
353 {
354         vdev_t *vd = zio->io_vd;
355         trim_map_t *tm = vd->vdev_trimmap;
356         trim_seg_t tsearch, *ts;
357         boolean_t left_over, right_over;
358         uint64_t start, end;
359
360         if (!zfs_trim_enabled || vd->vdev_notrim || tm == NULL)
361                 return (B_TRUE);
362
363         start = zio->io_offset;
364         end = TRIM_ZIO_END(zio->io_vd, start, zio->io_size);
365         tsearch.ts_start = start;
366         tsearch.ts_end = end;
367
368         mutex_enter(&tm->tm_lock);
369
370         /*
371          * Checking for colliding in-flight frees.
372          */
373         ts = avl_find(&tm->tm_inflight_frees, &tsearch, NULL);
374         if (ts != NULL) {
375                 list_insert_tail(&tm->tm_pending_writes, zio);
376                 mutex_exit(&tm->tm_lock);
377                 return (B_FALSE);
378         }
379
380         ts = avl_find(&tm->tm_queued_frees, &tsearch, NULL);
381         if (ts != NULL) {
382                 /*
383                  * Loop until all overlapping segments are removed.
384                  */
385                 do {
386                         trim_map_segment_remove(tm, ts, start, end);
387                         ts = avl_find(&tm->tm_queued_frees, &tsearch, NULL);
388                 } while (ts != NULL);
389         }
390         avl_add(&tm->tm_inflight_writes, zio);
391
392         mutex_exit(&tm->tm_lock);
393
394         return (B_TRUE);
395 }
396
397 void
398 trim_map_write_done(zio_t *zio)
399 {
400         vdev_t *vd = zio->io_vd;
401         trim_map_t *tm = vd->vdev_trimmap;
402
403         /*
404          * Don't check for vdev_notrim, since the write could have
405          * started before vdev_notrim was set.
406          */
407         if (!zfs_trim_enabled || tm == NULL)
408                 return;
409
410         mutex_enter(&tm->tm_lock);
411         /*
412          * Don't fail if the write isn't in the tree, since the write
413          * could have started after vdev_notrim was set.
414          */
415         if (zio->io_trim_node.avl_child[0] ||
416             zio->io_trim_node.avl_child[1] ||
417             AVL_XPARENT(&zio->io_trim_node) ||
418             tm->tm_inflight_writes.avl_root == &zio->io_trim_node)
419                 avl_remove(&tm->tm_inflight_writes, zio);
420         mutex_exit(&tm->tm_lock);
421 }
422
423 /*
424  * Return the oldest segment (the one with the lowest txg / time) or NULL if:
425  * 1. The list is empty
426  * 2. The first element's txg is greater than txgsafe
427  * 3. The first element's txg is not greater than the txg argument and the
428  *    the first element's time is not greater than time argument
429  */
430 static trim_seg_t *
431 trim_map_first(trim_map_t *tm, uint64_t txg, uint64_t txgsafe, hrtime_t time)
432 {
433         trim_seg_t *ts;
434
435         ASSERT(MUTEX_HELD(&tm->tm_lock));
436         VERIFY(txgsafe >= txg);
437
438         ts = list_head(&tm->tm_head);
439         if (ts != NULL && ts->ts_txg <= txgsafe &&
440             (ts->ts_txg <= txg || ts->ts_time <= time ||
441             tm->tm_bytes > trim_vdev_max_bytes ||
442             tm->tm_pending > trim_vdev_max_pending))
443                 return (ts);
444         return (NULL);
445 }
446
447 static void
448 trim_map_vdev_commit(spa_t *spa, zio_t *zio, vdev_t *vd)
449 {
450         trim_map_t *tm = vd->vdev_trimmap;
451         trim_seg_t *ts;
452         uint64_t size, txgtarget, txgsafe;
453         hrtime_t timelimit;
454
455         ASSERT(vd->vdev_ops->vdev_op_leaf);
456
457         if (tm == NULL)
458                 return;
459
460         timelimit = gethrtime() - trim_timeout * NANOSEC;
461         if (vd->vdev_isl2cache) {
462                 txgsafe = UINT64_MAX;
463                 txgtarget = UINT64_MAX;
464         } else {
465                 txgsafe = MIN(spa_last_synced_txg(spa), spa_freeze_txg(spa));
466                 if (txgsafe > trim_txg_delay)
467                         txgtarget = txgsafe - trim_txg_delay;
468                 else
469                         txgtarget = 0;
470         }
471
472         mutex_enter(&tm->tm_lock);
473         /* Loop until we have sent all outstanding free's */
474         while ((ts = trim_map_first(tm, txgtarget, txgsafe, timelimit))
475             != NULL) {
476                 list_remove(&tm->tm_head, ts);
477                 avl_remove(&tm->tm_queued_frees, ts);
478                 avl_add(&tm->tm_inflight_frees, ts);
479                 size = ts->ts_end - ts->ts_start;
480                 zio_nowait(zio_trim(zio, spa, vd, ts->ts_start, size));
481                 TRIM_MAP_SDEC(tm, size);
482                 TRIM_MAP_QDEC(tm);
483         }
484         mutex_exit(&tm->tm_lock);
485 }
486
487 static void
488 trim_map_vdev_commit_done(spa_t *spa, vdev_t *vd)
489 {
490         trim_map_t *tm = vd->vdev_trimmap;
491         trim_seg_t *ts;
492         list_t pending_writes;
493         zio_t *zio;
494         uint64_t start, size;
495         void *cookie;
496
497         ASSERT(vd->vdev_ops->vdev_op_leaf);
498
499         if (tm == NULL)
500                 return;
501
502         mutex_enter(&tm->tm_lock);
503         if (!avl_is_empty(&tm->tm_inflight_frees)) {
504                 cookie = NULL;
505                 while ((ts = avl_destroy_nodes(&tm->tm_inflight_frees,
506                     &cookie)) != NULL) {
507                         kmem_free(ts, sizeof (*ts));
508                 }
509         }
510         list_create(&pending_writes, sizeof (zio_t), offsetof(zio_t,
511             io_trim_link));
512         list_move_tail(&pending_writes, &tm->tm_pending_writes);
513         mutex_exit(&tm->tm_lock);
514
515         while ((zio = list_remove_head(&pending_writes)) != NULL) {
516                 zio_vdev_io_reissue(zio);
517                 zio_execute(zio);
518         }
519         list_destroy(&pending_writes);
520 }
521
522 static void
523 trim_map_commit(spa_t *spa, zio_t *zio, vdev_t *vd)
524 {
525         int c;
526
527         if (vd == NULL)
528                 return;
529
530         if (vd->vdev_ops->vdev_op_leaf) {
531                 trim_map_vdev_commit(spa, zio, vd);
532         } else {
533                 for (c = 0; c < vd->vdev_children; c++)
534                         trim_map_commit(spa, zio, vd->vdev_child[c]);
535         }
536 }
537
538 static void
539 trim_map_commit_done(spa_t *spa, vdev_t *vd)
540 {
541         int c;
542
543         if (vd == NULL)
544                 return;
545
546         if (vd->vdev_ops->vdev_op_leaf) {
547                 trim_map_vdev_commit_done(spa, vd);
548         } else {
549                 for (c = 0; c < vd->vdev_children; c++)
550                         trim_map_commit_done(spa, vd->vdev_child[c]);
551         }
552 }
553
554 static void
555 trim_thread(void *arg)
556 {
557         spa_t *spa = arg;
558         zio_t *zio;
559
560 #ifdef _KERNEL
561         (void) snprintf(curthread->td_name, sizeof(curthread->td_name),
562             "trim %s", spa_name(spa));
563 #endif
564
565         for (;;) {
566                 mutex_enter(&spa->spa_trim_lock);
567                 if (spa->spa_trim_thread == NULL) {
568                         spa->spa_trim_thread = curthread;
569                         cv_signal(&spa->spa_trim_cv);
570                         mutex_exit(&spa->spa_trim_lock);
571                         thread_exit();
572                 }
573
574                 (void) cv_timedwait(&spa->spa_trim_cv, &spa->spa_trim_lock,
575                     hz * trim_max_interval);
576                 mutex_exit(&spa->spa_trim_lock);
577
578                 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
579
580                 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
581                 trim_map_commit(spa, zio, spa->spa_root_vdev);
582                 (void) zio_wait(zio);
583                 trim_map_commit_done(spa, spa->spa_root_vdev);
584                 spa_config_exit(spa, SCL_STATE, FTAG);
585         }
586 }
587
588 void
589 trim_thread_create(spa_t *spa)
590 {
591
592         if (!zfs_trim_enabled)
593                 return;
594
595         mutex_init(&spa->spa_trim_lock, NULL, MUTEX_DEFAULT, NULL);
596         cv_init(&spa->spa_trim_cv, NULL, CV_DEFAULT, NULL);
597         mutex_enter(&spa->spa_trim_lock);
598         spa->spa_trim_thread = thread_create(NULL, 0, trim_thread, spa, 0, &p0,
599             TS_RUN, minclsyspri);
600         mutex_exit(&spa->spa_trim_lock);
601 }
602
603 void
604 trim_thread_destroy(spa_t *spa)
605 {
606
607         if (!zfs_trim_enabled)
608                 return;
609         if (spa->spa_trim_thread == NULL)
610                 return;
611
612         mutex_enter(&spa->spa_trim_lock);
613         /* Setting spa_trim_thread to NULL tells the thread to stop. */
614         spa->spa_trim_thread = NULL;
615         cv_signal(&spa->spa_trim_cv);
616         /* The thread will set it back to != NULL on exit. */
617         while (spa->spa_trim_thread == NULL)
618                 cv_wait(&spa->spa_trim_cv, &spa->spa_trim_lock);
619         spa->spa_trim_thread = NULL;
620         mutex_exit(&spa->spa_trim_lock);
621
622         cv_destroy(&spa->spa_trim_cv);
623         mutex_destroy(&spa->spa_trim_lock);
624 }
625
626 void
627 trim_thread_wakeup(spa_t *spa)
628 {
629
630         if (!zfs_trim_enabled)
631                 return;
632         if (spa->spa_trim_thread == NULL)
633                 return;
634
635         mutex_enter(&spa->spa_trim_lock);
636         cv_signal(&spa->spa_trim_cv);
637         mutex_exit(&spa->spa_trim_lock);
638 }