dm - Move the target unbusy to the dm_table_destroy function
[dragonfly.git] / sys / dev / disk / dm / targets / linear / dm_target_linear.c
1 /*        $NetBSD: dm_target_linear.c,v 1.9 2010/01/04 00:14:41 haad Exp $      */
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  */
31
32
33 /*
34  * This file implements initial version of device-mapper dklinear target.
35  */
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39
40 #include <sys/buf.h>
41 #include <sys/malloc.h>
42 #include <sys/vnode.h>
43
44 #include <dev/disk/dm/dm.h>
45 MALLOC_DEFINE(M_DMLINEAR, "dm_linear", "Device Mapper Target Linear");
46
47 /*
48  * Allocate target specific config data, and link them to table.
49  * This function is called only when, flags is not READONLY and
50  * therefore we can add things to pdev list. This should not a
51  * problem because this routine is called only from dm_table_load_ioctl.
52  * @argv[0] is name,
53  * @argv[1] is physical data offset.
54  */
55 static int
56 dm_target_linear_init(dm_dev_t * dmv, void **target_config, char *params)
57 {
58         dm_target_linear_config_t *tlc;
59         dm_pdev_t *dmp;
60
61         char **ap, *argv[3];
62
63         if (params == NULL)
64                 return EINVAL;
65
66         /*
67          * Parse a string, containing tokens delimited by white space,
68          * into an argument vector
69          */
70         for (ap = argv; ap < &argv[2] &&
71             (*ap = strsep(&params, " \t")) != NULL;) {
72                 if (**ap != '\0')
73                         ap++;
74         }
75
76         aprint_debug("Linear target init function called %s--%s!!\n",
77             argv[0], argv[1]);
78
79         /* XXX: temp hack */
80         if (argv[0] == NULL)
81                 return EINVAL;
82
83         /* Insert dmp to global pdev list */
84         if ((dmp = dm_pdev_insert(argv[0])) == NULL)
85                 return ENOENT;
86
87         if ((tlc = kmalloc(sizeof(dm_target_linear_config_t), M_DMLINEAR, M_WAITOK))
88             == NULL)
89                 return ENOMEM;
90
91         tlc->pdev = dmp;
92         tlc->offset = 0;        /* default settings */
93
94         /* Check user input if it is not leave offset as 0. */
95         tlc->offset = atoi64(argv[1]);
96
97         *target_config = tlc;
98
99         dmv->dev_type = DM_LINEAR_DEV;
100
101         return 0;
102 }
103 /*
104  * Status routine is called to get params string, which is target
105  * specific. When dm_table_status_ioctl is called with flag
106  * DM_STATUS_TABLE_FLAG I have to sent params string back.
107  */
108 static char *
109 dm_target_linear_status(void *target_config)
110 {
111         dm_target_linear_config_t *tlc;
112         char *params;
113         tlc = target_config;
114
115         aprint_debug("Linear target status function called\n");
116
117         /* target expects use of M_DM */
118         params = kmalloc(DM_MAX_PARAMS_SIZE, M_DM, M_WAITOK);
119
120         aprint_normal("%s %" PRIu64, tlc->pdev->name, tlc->offset);
121         ksnprintf(params, DM_MAX_PARAMS_SIZE, "%s %" PRIu64,
122             tlc->pdev->name, tlc->offset);
123
124         return params;
125 }
126 /*
127  * Do IO operation, called from dmstrategy routine.
128  */
129 static int
130 dm_target_linear_strategy(dm_table_entry_t * table_en, struct buf * bp)
131 {
132         dm_target_linear_config_t *tlc;
133
134         tlc = table_en->target_config;
135
136 /*      printf("Linear target read function called %" PRIu64 "!!\n",
137         tlc->offset);*/
138
139 #if 0
140         bp->b_blkno += tlc->offset;
141 #endif
142         bp->b_bio1.bio_offset += tlc->offset * DEV_BSIZE;
143
144         vn_strategy(tlc->pdev->pdev_vnode, &bp->b_bio1);
145
146         return 0;
147
148 }
149
150 static int
151 dm_target_linear_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
152 {
153         dm_target_linear_config_t *tlc;
154
155         tlc = table_en->target_config;
156
157         offset += tlc->offset * DEV_BSIZE;
158         offset = dm_pdev_correct_dump_offset(tlc->pdev, offset);
159
160         if (tlc->pdev->pdev_vnode->v_rdev == NULL)
161                 return ENXIO;
162
163         return dev_ddump(tlc->pdev->pdev_vnode->v_rdev, data, 0, offset, length);
164 }
165
166 /*
167  * Destroy target specific data. Decrement table pdevs.
168  */
169 static int
170 dm_target_linear_destroy(dm_table_entry_t * table_en)
171 {
172         dm_target_linear_config_t *tlc;
173
174         /*
175          * Destroy function is called for every target even if it
176          * doesn't have target_config.
177          */
178
179         if (table_en->target_config == NULL)
180                 return 0;
181
182         tlc = table_en->target_config;
183
184         /* Decrement pdev ref counter if 0 remove it */
185         dm_pdev_decr(tlc->pdev);
186
187         kfree(table_en->target_config, M_DMLINEAR);
188
189         table_en->target_config = NULL;
190
191         return 0;
192 }
193 /* Add this target pdev dependiences to prop_array_t */
194 static int
195 dm_target_linear_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
196 {
197         dm_target_linear_config_t *tlc;
198         struct vattr va;
199
200         int error;
201
202         if (table_en->target_config == NULL)
203                 return ENOENT;
204
205         tlc = table_en->target_config;
206
207         if ((error = VOP_GETATTR(tlc->pdev->pdev_vnode, &va)) != 0)
208                 return error;
209
210         prop_array_add_uint64(prop_array,
211             (uint64_t) makeudev(va.va_rmajor, va.va_rminor));
212
213         return 0;
214 }
215 /*
216  * Register upcall device.
217  * Linear target doesn't need any upcall devices but other targets like
218  * mirror, snapshot, multipath, stripe will use this functionality.
219  */
220 static int
221 dm_target_linear_upcall(dm_table_entry_t * table_en, struct buf * bp)
222 {
223         return 0;
224 }
225
226 static int
227 dmtl_mod_handler(module_t mod, int type, void *unused)
228 {
229         dm_target_t *dmt = NULL;
230         int err = 0;
231
232         switch(type) {
233         case MOD_LOAD:
234                 if ((dmt = dm_target_lookup("linear")) != NULL) {
235                         dm_target_unbusy(dmt);
236                         return EEXIST;
237                 }
238                 dmt = dm_target_alloc("linear");
239                 dmt->version[0] = 1;
240                 dmt->version[1] = 0;
241                 dmt->version[2] = 2;
242                 strlcpy(dmt->name, "linear", DM_MAX_TYPE_NAME);
243                 dmt->init = &dm_target_linear_init;
244                 dmt->status = &dm_target_linear_status;
245                 dmt->strategy = &dm_target_linear_strategy;
246                 dmt->deps = &dm_target_linear_deps;
247                 dmt->destroy = &dm_target_linear_destroy;
248                 dmt->upcall = &dm_target_linear_upcall;
249                 dmt->dump = &dm_target_linear_dump;
250
251                 err = dm_target_insert(dmt);
252                 if (err == 0)
253                         kprintf("dm_target_linear: Successfully initialized\n");
254                 break;
255
256         case MOD_UNLOAD:
257                 err = dm_target_rem("linear");
258                 if (err == 0)
259                         kprintf("dm_target_linear: unloaded\n");
260                 break;
261
262         default:
263                 break;
264         }
265
266         return err;
267 }
268
269 DM_TARGET_MODULE(dm_target_linear, dmtl_mod_handler);