Sat Dec 7 06:26:31 2019 UTC ()
dm: Simplify list eviction code

taken-from: DragonFlyBSD


(tkusumi)
diff -r1.16 -r1.17 src/sys/dev/dm/dm_pdev.c
diff -r1.10 -r1.11 src/sys/dev/dm/dm_table.c
diff -r1.23 -r1.24 src/sys/dev/dm/dm_target.c

cvs diff -r1.16 -r1.17 src/sys/dev/dm/dm_pdev.c (expand / switch to unified diff)

--- src/sys/dev/dm/dm_pdev.c 2019/12/06 16:46:14 1.16
+++ src/sys/dev/dm/dm_pdev.c 2019/12/07 06:26:31 1.17
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: dm_pdev.c,v 1.16 2019/12/06 16:46:14 tkusumi Exp $ */ 1/* $NetBSD: dm_pdev.c,v 1.17 2019/12/07 06:26:31 tkusumi Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Hamsik. 8 * by Adam Hamsik.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -19,27 +19,27 @@ @@ -19,27 +19,27 @@
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#include <sys/cdefs.h> 31#include <sys/cdefs.h>
32__KERNEL_RCSID(0, "$NetBSD: dm_pdev.c,v 1.16 2019/12/06 16:46:14 tkusumi Exp $"); 32__KERNEL_RCSID(0, "$NetBSD: dm_pdev.c,v 1.17 2019/12/07 06:26:31 tkusumi Exp $");
33 33
34#include <sys/types.h> 34#include <sys/types.h>
35#include <sys/param.h> 35#include <sys/param.h>
36 36
37#include <sys/disk.h> 37#include <sys/disk.h>
38#include <sys/fcntl.h> 38#include <sys/fcntl.h>
39#include <sys/kmem.h> 39#include <sys/kmem.h>
40#include <sys/namei.h> 40#include <sys/namei.h>
41 41
42#include <dev/dkvar.h> 42#include <dev/dkvar.h>
43 43
44#include "dm.h" 44#include "dm.h"
45 45
@@ -178,36 +178,36 @@ dm_pdev_rem(dm_pdev_t * dmp) @@ -178,36 +178,36 @@ dm_pdev_rem(dm_pdev_t * dmp)
178 return error; 178 return error;
179 } 179 }
180 kmem_free(dmp, sizeof(*dmp)); 180 kmem_free(dmp, sizeof(*dmp));
181 181
182 return 0; 182 return 0;
183} 183}
184 184
185/* 185/*
186 * Destroy all existing pdev's in device-mapper. 186 * Destroy all existing pdev's in device-mapper.
187 */ 187 */
188int 188int
189dm_pdev_destroy(void) 189dm_pdev_destroy(void)
190{ 190{
191 dm_pdev_t *dm_pdev; 191 dm_pdev_t *dmp;
192 192
193 mutex_enter(&dm_pdev_mutex); 193 mutex_enter(&dm_pdev_mutex);
194 while (!SLIST_EMPTY(&dm_pdev_list)) { /* List Deletion. */ 
195 dm_pdev = SLIST_FIRST(&dm_pdev_list); 
196 
197 SLIST_REMOVE_HEAD(&dm_pdev_list, next_pdev); 
198 194
199 dm_pdev_rem(dm_pdev); 195 while ((dmp = SLIST_FIRST(&dm_pdev_list)) != NULL) {
 196 SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev);
 197 dm_pdev_rem(dmp);
200 } 198 }
 199 KASSERT(SLIST_EMPTY(&dm_pdev_list));
 200
201 mutex_exit(&dm_pdev_mutex); 201 mutex_exit(&dm_pdev_mutex);
202 202
203 mutex_destroy(&dm_pdev_mutex); 203 mutex_destroy(&dm_pdev_mutex);
204 return 0; 204 return 0;
205} 205}
206 206
207/* 207/*
208 * This funcion is called from dm_dev_remove_ioctl. 208 * This funcion is called from dm_dev_remove_ioctl.
209 * When I'm removing device from list, I have to decrement 209 * When I'm removing device from list, I have to decrement
210 * reference counter. If reference counter is 0 I will remove 210 * reference counter. If reference counter is 0 I will remove
211 * dmp from global list and from device list to. And I will CLOSE 211 * dmp from global list and from device list to. And I will CLOSE
212 * dmp vnode too. 212 * dmp vnode too.
213 */ 213 */

cvs diff -r1.10 -r1.11 src/sys/dev/dm/dm_table.c (expand / switch to unified diff)

--- src/sys/dev/dm/dm_table.c 2019/12/05 16:59:43 1.10
+++ src/sys/dev/dm/dm_table.c 2019/12/07 06:26:31 1.11
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: dm_table.c,v 1.10 2019/12/05 16:59:43 tkusumi Exp $ */ 1/* $NetBSD: dm_table.c,v 1.11 2019/12/07 06:26:31 tkusumi Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Hamsik. 8 * by Adam Hamsik.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -19,27 +19,27 @@ @@ -19,27 +19,27 @@
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#include <sys/cdefs.h> 31#include <sys/cdefs.h>
32__KERNEL_RCSID(0, "$NetBSD: dm_table.c,v 1.10 2019/12/05 16:59:43 tkusumi Exp $"); 32__KERNEL_RCSID(0, "$NetBSD: dm_table.c,v 1.11 2019/12/07 06:26:31 tkusumi Exp $");
33 33
34#include <sys/types.h> 34#include <sys/types.h>
35#include <sys/param.h> 35#include <sys/param.h>
36 36
37#include <sys/kmem.h> 37#include <sys/kmem.h>
38 38
39#include "dm.h" 39#include "dm.h"
40 40
41/* 41/*
42 * There are two types of users of this interface: 42 * There are two types of users of this interface:
43 * 43 *
44 * a) Readers such as 44 * a) Readers such as
45 * dmstrategy, dmgetdisklabel, dmsize, dm_dev_status_ioctl, 45 * dmstrategy, dmgetdisklabel, dmsize, dm_dev_status_ioctl,
@@ -150,35 +150,34 @@ dm_table_destroy(dm_table_head_t * head, @@ -150,35 +150,34 @@ dm_table_destroy(dm_table_head_t * head,
150 150
151 aprint_debug("dm_Table_destroy called with %d--%d\n", table_id, head->io_cnt); 151 aprint_debug("dm_Table_destroy called with %d--%d\n", table_id, head->io_cnt);
152 152
153 while (head->io_cnt != 0) 153 while (head->io_cnt != 0)
154 cv_wait(&head->table_cv, &head->table_mtx); 154 cv_wait(&head->table_cv, &head->table_mtx);
155 155
156 if (table_id == DM_TABLE_ACTIVE) 156 if (table_id == DM_TABLE_ACTIVE)
157 id = head->cur_active_table; 157 id = head->cur_active_table;
158 else 158 else
159 id = 1 - head->cur_active_table; 159 id = 1 - head->cur_active_table;
160 160
161 tbl = &head->tables[id]; 161 tbl = &head->tables[id];
162 162
163 while (!SLIST_EMPTY(tbl)) { /* List Deletion. */ 163 while ((table_en = SLIST_FIRST(tbl)) != NULL) {
164 table_en = SLIST_FIRST(tbl); 164 SLIST_REMOVE(tbl, table_en, dm_table_entry, next);
165 if (table_en->target->destroy(table_en) == 0) 165 if (table_en->target->destroy(table_en) == 0)
166 table_en->target_config = NULL; 166 table_en->target_config = NULL;
167 167
168 SLIST_REMOVE_HEAD(tbl, next); 
169 
170 kmem_free(table_en, sizeof(*table_en)); 168 kmem_free(table_en, sizeof(*table_en));
171 } 169 }
 170 KASSERT(SLIST_EMPTY(tbl));
172 171
173 mutex_exit(&head->table_mtx); 172 mutex_exit(&head->table_mtx);
174 173
175 return 0; 174 return 0;
176} 175}
177 176
178/* 177/*
179 * Return length of active table in device. 178 * Return length of active table in device.
180 */ 179 */
181static inline uint64_t 180static inline uint64_t
182dm_table_size_impl(dm_table_head_t * head, int table) 181dm_table_size_impl(dm_table_head_t * head, int table)
183{ 182{
184 dm_table_t *tbl; 183 dm_table_t *tbl;

cvs diff -r1.23 -r1.24 src/sys/dev/dm/dm_target.c (expand / switch to unified diff)

--- src/sys/dev/dm/dm_target.c 2019/12/06 16:11:59 1.23
+++ src/sys/dev/dm/dm_target.c 2019/12/07 06:26:31 1.24
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: dm_target.c,v 1.23 2019/12/06 16:11:59 tkusumi Exp $ */ 1/* $NetBSD: dm_target.c,v 1.24 2019/12/07 06:26:31 tkusumi Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Hamsik. 8 * by Adam Hamsik.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code Must retain the above copyright 13 * 1. Redistributions of source code Must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -19,27 +19,27 @@ @@ -19,27 +19,27 @@
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#include <sys/cdefs.h> 31#include <sys/cdefs.h>
32__KERNEL_RCSID(0, "$NetBSD: dm_target.c,v 1.23 2019/12/06 16:11:59 tkusumi Exp $"); 32__KERNEL_RCSID(0, "$NetBSD: dm_target.c,v 1.24 2019/12/07 06:26:31 tkusumi Exp $");
33 33
34#include <sys/types.h> 34#include <sys/types.h>
35#include <sys/param.h> 35#include <sys/param.h>
36 36
37#include <sys/kmem.h> 37#include <sys/kmem.h>
38#include <sys/module.h> 38#include <sys/module.h>
39 39
40 40
41#include "netbsd-dm.h" 41#include "netbsd-dm.h"
42#include "dm.h" 42#include "dm.h"
43 43
44static dm_target_t *dm_target_lookup_name(const char *); 44static dm_target_t *dm_target_lookup_name(const char *);
45 45
@@ -210,34 +210,33 @@ dm_target_rem(char *dm_target_name) @@ -210,34 +210,33 @@ dm_target_rem(char *dm_target_name)
210} 210}
211 211
212/* 212/*
213 * Destroy all targets and remove them from queue. 213 * Destroy all targets and remove them from queue.
214 * This routine is called from dm_detach, before module 214 * This routine is called from dm_detach, before module
215 * is unloaded. 215 * is unloaded.
216 */ 216 */
217int 217int
218dm_target_destroy(void) 218dm_target_destroy(void)
219{ 219{
220 dm_target_t *dm_target; 220 dm_target_t *dm_target;
221 221
222 mutex_enter(&dm_target_mutex); 222 mutex_enter(&dm_target_mutex);
223 while (TAILQ_FIRST(&dm_target_list) != NULL) { 
224 dm_target = TAILQ_FIRST(&dm_target_list); 
225 
226 TAILQ_REMOVE(&dm_target_list, TAILQ_FIRST(&dm_target_list), 
227 dm_target_next); 
228 223
 224 while ((dm_target = TAILQ_FIRST(&dm_target_list)) != NULL) {
 225 TAILQ_REMOVE(&dm_target_list, dm_target, dm_target_next);
229 (void)kmem_free(dm_target, sizeof(dm_target_t)); 226 (void)kmem_free(dm_target, sizeof(dm_target_t));
230 } 227 }
 228 KASSERT(TAILQ_EMPTY(&dm_target_list));
 229
231 mutex_exit(&dm_target_mutex); 230 mutex_exit(&dm_target_mutex);
232 231
233 mutex_destroy(&dm_target_mutex); 232 mutex_destroy(&dm_target_mutex);
234 233
235 return 0; 234 return 0;
236} 235}
237 236
238/* 237/*
239 * Allocate new target entry. 238 * Allocate new target entry.
240 */ 239 */
241dm_target_t * 240dm_target_t *
242dm_target_alloc(const char *name) 241dm_target_alloc(const char *name)
243{ 242{