Annotation of src/sys/dev/dm/dm_target_linear.c, revision 1.30
1.30 ! tkusumi 1: /* $NetBSD: dm_target_linear.c,v 1.29 2019/12/15 05:56:02 tkusumi Exp $ */
1.2 haad 2:
3: /*
4: * Copyright (c) 2008 The NetBSD Foundation, Inc.
5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
8: * by Adam Hamsik.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
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
21: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
29: * POSSIBILITY OF SUCH DAMAGE.
30: */
1.17 christos 31: #include <sys/cdefs.h>
1.30 ! tkusumi 32: __KERNEL_RCSID(0, "$NetBSD: dm_target_linear.c,v 1.29 2019/12/15 05:56:02 tkusumi Exp $");
1.2 haad 33:
34: /*
35: * This file implements initial version of device-mapper dklinear target.
36: */
37:
38: #include <sys/types.h>
39: #include <sys/param.h>
40:
41: #include <sys/buf.h>
42: #include <sys/kmem.h>
1.11 uebayasi 43: #include <sys/lwp.h>
1.2 haad 44:
45: #include <machine/int_fmtio.h>
46:
47: #include "dm.h"
48:
49: /*
50: * Allocate target specific config data, and link them to table.
51: * This function is called only when, flags is not READONLY and
1.9 haad 52: * therefore we can add things to pdev list. This should not a
1.6 yamt 53: * problem because this routine is called only from dm_table_load_ioctl.
1.2 haad 54: * @argv[0] is name,
55: * @argv[1] is physical data offset.
56: */
57: int
1.27 tkusumi 58: dm_target_linear_init(dm_table_entry_t *table_en, int argc, char **argv)
1.2 haad 59: {
60: dm_target_linear_config_t *tlc;
61: dm_pdev_t *dmp;
62:
1.27 tkusumi 63: /*
64: if (argc != 2) {
65: printf("Linear target takes 2 args\n");
1.8 haad 66: return EINVAL;
67: }
1.27 tkusumi 68: */
1.2 haad 69:
1.8 haad 70: aprint_debug("Linear target init function called %s--%s!!\n",
71: argv[0], argv[1]);
1.9 haad 72:
1.2 haad 73: /* Insert dmp to global pdev list */
1.8 haad 74: if ((dmp = dm_pdev_insert(argv[0])) == NULL)
1.2 haad 75: return ENOENT;
1.9 haad 76:
1.15 chs 77: tlc = kmem_alloc(sizeof(dm_target_linear_config_t), KM_SLEEP);
1.2 haad 78: tlc->pdev = dmp;
1.8 haad 79: tlc->offset = atoi(argv[1]);
1.2 haad 80:
1.25 tkusumi 81: table_en->target_config = tlc;
1.2 haad 82:
83: return 0;
84: }
1.17 christos 85:
1.2 haad 86: /*
1.29 tkusumi 87: * Table routine is called to get params string, which is target
1.2 haad 88: * specific. When dm_table_status_ioctl is called with flag
89: * DM_STATUS_TABLE_FLAG I have to sent params string back.
90: */
91: char *
1.29 tkusumi 92: dm_target_linear_table(void *target_config)
1.2 haad 93: {
94: dm_target_linear_config_t *tlc;
95: char *params;
1.8 haad 96: tlc = target_config;
97:
1.29 tkusumi 98: aprint_debug("Linear target table function called\n");
1.2 haad 99:
1.18 chs 100: params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP);
1.9 haad 101: snprintf(params, DM_MAX_PARAMS_SIZE, "%s %" PRIu64,
1.8 haad 102: tlc->pdev->name, tlc->offset);
103:
1.2 haad 104: return params;
105: }
1.17 christos 106:
1.2 haad 107: /*
108: * Do IO operation, called from dmstrategy routine.
109: */
110: int
1.24 tkusumi 111: dm_target_linear_strategy(dm_table_entry_t *table_en, struct buf *bp)
1.2 haad 112: {
113: dm_target_linear_config_t *tlc;
114:
115: tlc = table_en->target_config;
1.9 haad 116:
1.2 haad 117: bp->b_blkno += tlc->offset;
1.9 haad 118:
1.2 haad 119: VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
1.9 haad 120:
1.2 haad 121: return 0;
122: }
1.17 christos 123:
1.2 haad 124: /*
1.10 haad 125: * Sync underlying disk caches.
126: */
127: int
1.24 tkusumi 128: dm_target_linear_sync(dm_table_entry_t *table_en)
1.10 haad 129: {
130: int cmd;
131: dm_target_linear_config_t *tlc;
132:
133: tlc = table_en->target_config;
134:
135: cmd = 1;
1.23 tkusumi 136:
1.10 haad 137: return VOP_IOCTL(tlc->pdev->pdev_vnode, DIOCCACHESYNC, &cmd,
1.23 tkusumi 138: FREAD|FWRITE, kauth_cred_get());
1.10 haad 139: }
1.17 christos 140:
1.10 haad 141: /*
1.2 haad 142: * Destroy target specific data. Decrement table pdevs.
143: */
144: int
1.24 tkusumi 145: dm_target_linear_destroy(dm_table_entry_t *table_en)
1.2 haad 146: {
147:
148: /*
149: * Destroy function is called for every target even if it
150: * doesn't have target_config.
151: */
152: if (table_en->target_config == NULL)
1.17 christos 153: goto out;
1.9 haad 154:
1.17 christos 155: dm_target_linear_config_t *tlc = table_en->target_config;
1.9 haad 156:
1.2 haad 157: /* Decrement pdev ref counter if 0 remove it */
158: dm_pdev_decr(tlc->pdev);
1.9 haad 159:
1.17 christos 160: kmem_free(tlc, sizeof(*tlc));
161:
162: out:
1.3 haad 163: /* Unbusy target so we can unload it */
164: dm_target_unbusy(table_en->target);
1.2 haad 165: return 0;
166: }
1.17 christos 167:
1.16 mbalmer 168: /* Add this target pdev dependencies to prop_array_t */
1.2 haad 169: int
1.24 tkusumi 170: dm_target_linear_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
1.2 haad 171: {
172: dm_target_linear_config_t *tlc;
1.9 haad 173:
1.2 haad 174: if (table_en->target_config == NULL)
175: return ENOENT;
1.9 haad 176:
1.2 haad 177: tlc = table_en->target_config;
1.9 haad 178:
1.14 hannken 179: prop_array_add_uint64(prop_array,
180: (uint64_t) tlc->pdev->pdev_vnode->v_rdev);
1.9 haad 181:
1.2 haad 182: return 0;
183: }
1.17 christos 184:
1.2 haad 185: /*
186: * Register upcall device.
187: * Linear target doesn't need any upcall devices but other targets like
188: * mirror, snapshot, multipath, stripe will use this functionality.
189: */
190: int
1.24 tkusumi 191: dm_target_linear_upcall(dm_table_entry_t *table_en, struct buf *bp)
1.2 haad 192: {
193: return 0;
194: }
1.17 christos 195:
1.2 haad 196: /*
1.12 mlelstv 197: * Query physical block size of this target
198: * For a linear target this is just the sector size of the underlying device
199: */
200: int
1.30 ! tkusumi 201: dm_target_linear_secsize(dm_table_entry_t *table_en, unsigned int *secsizep)
1.12 mlelstv 202: {
203: dm_target_linear_config_t *tlc;
1.30 ! tkusumi 204: unsigned int secsize;
1.12 mlelstv 205:
206: secsize = 0;
207:
208: tlc = table_en->target_config;
209: if (tlc != NULL)
210: secsize = tlc->pdev->pdev_secsize;
211:
212: *secsizep = secsize;
213:
214: return 0;
215: }
1.17 christos 216:
1.12 mlelstv 217: /*
1.2 haad 218: * Transform char s to uint64_t offset number.
219: */
220: uint64_t
221: atoi(const char *s)
222: {
223: uint64_t n;
224: n = 0;
225:
226: while (*s != '\0') {
227: if (!isdigit(*s))
228: break;
229:
230: n = (10 * n) + (*s - '0');
231: s++;
232: }
233:
234: return n;
235: }
CVSweb <webmaster@jp.NetBSD.org>