dm_target_linear.c revision 1.1.2.2 1 1.1.2.1 haad /*
2 1.1.2.1 haad * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
3 1.1.2.1 haad * All rights reserved.
4 1.1.2.1 haad *
5 1.1.2.1 haad * This code is derived from software contributed to The NetBSD Foundation
6 1.1.2.1 haad * by Adam Hamsik.
7 1.1.2.1 haad *
8 1.1.2.1 haad * Redistribution and use in source and binary forms, with or without
9 1.1.2.1 haad * modification, are permitted provided that the following conditions
10 1.1.2.1 haad * are met:
11 1.1.2.1 haad * 1. Redistributions of source code must retain the above copyright
12 1.1.2.1 haad * notice, this list of conditions and the following disclaimer.
13 1.1.2.1 haad * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.2.1 haad * notice, this list of conditions and the following disclaimer in the
15 1.1.2.1 haad * documentation and/or other materials provided with the distribution.
16 1.1.2.1 haad *
17 1.1.2.1 haad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1.2.1 haad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1.2.1 haad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1.2.1 haad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1.2.1 haad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1.2.1 haad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1.2.1 haad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1.2.1 haad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1.2.1 haad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1.2.1 haad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1.2.1 haad * POSSIBILITY OF SUCH DAMAGE.
28 1.1.2.1 haad */
29 1.1.2.1 haad
30 1.1.2.1 haad
31 1.1.2.1 haad /*
32 1.1.2.1 haad * This file implements initial version of device-mapper dklinear target.
33 1.1.2.1 haad */
34 1.1.2.1 haad
35 1.1.2.1 haad #include <sys/types.h>
36 1.1.2.1 haad #include <sys/param.h>
37 1.1.2.1 haad
38 1.1.2.1 haad #include <sys/buf.h>
39 1.1.2.1 haad #include <sys/disk.h>
40 1.1.2.1 haad #include <sys/errno.h>
41 1.1.2.1 haad #include <sys/ioctl.h>
42 1.1.2.1 haad #include <sys/ioccom.h>
43 1.1.2.1 haad #include <sys/kmem.h>
44 1.1.2.1 haad #include <sys/types.h>
45 1.1.2.1 haad #include <sys/param.h>
46 1.1.2.1 haad #include <sys/queue.h>
47 1.1.2.1 haad #include <sys/vnode.h>
48 1.1.2.1 haad
49 1.1.2.1 haad #include "dm.h"
50 1.1.2.1 haad
51 1.1.2.1 haad static uint64_t atoi(const char *);
52 1.1.2.1 haad
53 1.1.2.1 haad /*
54 1.1.2.1 haad * Allocate target specific config data, and link them to table.
55 1.1.2.1 haad * argv[0] is name,
56 1.1.2.1 haad * argv[1] is physical data offset.
57 1.1.2.1 haad */
58 1.1.2.1 haad int
59 1.1.2.1 haad dm_target_linear_init(struct dm_dev *dmv, void **target_config,
60 1.1.2.1 haad int argc, const char **argv)
61 1.1.2.1 haad {
62 1.1.2.1 haad struct target_linear_config *tlc;
63 1.1.2.1 haad struct dm_pdev *dmp;
64 1.1.2.1 haad
65 1.1.2.2 haad printf("Linear target init function called %s--%s!!\n",
66 1.1.2.2 haad argv[0], argv[1]);
67 1.1.2.1 haad
68 1.1.2.1 haad dmp = dm_pdev_lookup_name(argv[0]);
69 1.1.2.1 haad
70 1.1.2.2 haad if ((tlc = kmem_alloc(sizeof(struct target_linear_config), KM_NOSLEEP))
71 1.1.2.1 haad == NULL)
72 1.1.2.1 haad return 1;
73 1.1.2.1 haad
74 1.1.2.1 haad /* XXX howto convert char* to number ? tlc->offset = atoi(argv[1]); */
75 1.1.2.1 haad
76 1.1.2.1 haad tlc->pdev = dmp;
77 1.1.2.1 haad tlc->offset = 0; /* default settings */
78 1.1.2.1 haad
79 1.1.2.1 haad /* Check user input if it is not leave offset as 0. */
80 1.1.2.1 haad if (isdigit((int)argv[1]))
81 1.1.2.1 haad tlc->offset = atoi(argv[1]);
82 1.1.2.1 haad
83 1.1.2.1 haad *target_config = tlc;
84 1.1.2.1 haad
85 1.1.2.1 haad return 0;
86 1.1.2.1 haad }
87 1.1.2.1 haad
88 1.1.2.1 haad /*
89 1.1.2.1 haad * Do IO operation, called from dmstrategy routine.
90 1.1.2.1 haad */
91 1.1.2.1 haad int
92 1.1.2.1 haad dm_target_linear_strategy(struct dm_table_entry *table_en, struct buf *bp)
93 1.1.2.1 haad {
94 1.1.2.1 haad struct target_linear_config *tlc;
95 1.1.2.1 haad
96 1.1.2.1 haad tlc = table_en->target_config;
97 1.1.2.1 haad
98 1.1.2.1 haad printf("Linear target read function called!!\n");
99 1.1.2.1 haad
100 1.1.2.1 haad bp->b_blkno += tlc->offset;
101 1.1.2.1 haad
102 1.1.2.1 haad VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
103 1.1.2.1 haad
104 1.1.2.1 haad return 0;
105 1.1.2.1 haad
106 1.1.2.1 haad }
107 1.1.2.1 haad
108 1.1.2.1 haad /*
109 1.1.2.1 haad * Destroy target specific data.
110 1.1.2.1 haad */
111 1.1.2.1 haad int
112 1.1.2.1 haad dm_target_linear_destroy(struct dm_table_entry *table_en)
113 1.1.2.1 haad {
114 1.1.2.1 haad
115 1.1.2.2 haad kmem_free(table_en->target_config, sizeof(struct target_linear_config));
116 1.1.2.1 haad
117 1.1.2.1 haad return 0;
118 1.1.2.1 haad }
119 1.1.2.1 haad
120 1.1.2.1 haad /*
121 1.1.2.1 haad * Register upcall device.
122 1.1.2.1 haad * Linear target doesn't need any upcall devices but other targets like
123 1.1.2.1 haad * mirror, snapshot, multipath, stripe will use this functionality.
124 1.1.2.1 haad */
125 1.1.2.1 haad int
126 1.1.2.1 haad dm_target_linear_upcall(struct dm_table_entry *table_en, struct buf *bp)
127 1.1.2.1 haad {
128 1.1.2.1 haad
129 1.1.2.1 haad return 0;
130 1.1.2.1 haad }
131 1.1.2.1 haad
132 1.1.2.1 haad /*
133 1.1.2.1 haad * Transform char s to uint64_t offset number.
134 1.1.2.1 haad */
135 1.1.2.1 haad static uint64_t
136 1.1.2.1 haad atoi(const char *s)
137 1.1.2.1 haad {
138 1.1.2.1 haad uint64_t n;
139 1.1.2.1 haad
140 1.1.2.1 haad n = 0;
141 1.1.2.1 haad
142 1.1.2.1 haad while (*s != '\0') {
143 1.1.2.1 haad
144 1.1.2.1 haad if (!isdigit(*s))
145 1.1.2.1 haad break;
146 1.1.2.1 haad
147 1.1.2.1 haad n = (10 * n) + (*s - '0');
148 1.1.2.1 haad s++;
149 1.1.2.1 haad }
150 1.1.2.1 haad
151 1.1.2.1 haad return n;
152 1.1.2.1 haad }
153