sys/dev/disk/dm: Check if target has registered handlers
[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
39 #include <sys/buf.h>
40 #include <sys/malloc.h>
41 #include <sys/vnode.h>
42
43 #include <dev/disk/dm/dm.h>
44 MALLOC_DEFINE(M_DMLINEAR, "dm_linear", "Device Mapper Target Linear");
45
46 typedef struct target_linear_config {
47         dm_pdev_t *pdev;
48         uint64_t offset;
49 } dm_target_linear_config_t;
50
51 /*
52  * Allocate target specific config data, and link them to table.
53  * This function is called only when, flags is not READONLY and
54  * therefore we can add things to pdev list. This should not a
55  * problem because this routine is called only from dm_table_load_ioctl.
56  * @argv[0] is name,
57  * @argv[1] is physical data offset.
58  */
59 static int
60 dm_target_linear_init(dm_table_entry_t *table_en, int argc, char **argv)
61 {
62         dm_target_linear_config_t *tlc;
63         dm_pdev_t *dmp;
64
65         if (argc != 2) {
66                 kprintf("Linear target takes 2 args\n");
67                 return EINVAL;
68         }
69
70         dmdebug("Linear target init function called %s--%s!!\n",
71             argv[0], argv[1]);
72
73         /* Insert dmp to global pdev list */
74         if ((dmp = dm_pdev_insert(argv[0])) == NULL)
75                 return ENOENT;
76
77         if ((tlc = kmalloc(sizeof(dm_target_linear_config_t), M_DMLINEAR, M_WAITOK))
78             == NULL)
79                 return ENOMEM;
80
81         tlc->pdev = dmp;
82         tlc->offset = atoi64(argv[1]);
83
84         dm_table_add_deps(table_en, dmp);
85
86         dm_table_init_target(table_en, DM_LINEAR_DEV, tlc);
87
88         return 0;
89 }
90 /*
91  * Table routine is called to get params string, which is target
92  * specific. When dm_table_status_ioctl is called with flag
93  * DM_STATUS_TABLE_FLAG I have to sent params string back.
94  */
95 static char *
96 dm_target_linear_table(void *target_config)
97 {
98         dm_target_linear_config_t *tlc;
99         char *params;
100         tlc = target_config;
101
102         dmdebug("Linear target table function called\n");
103
104         params = dm_alloc_string(DM_MAX_PARAMS_SIZE);
105
106         ksnprintf(params, DM_MAX_PARAMS_SIZE, "%s %" PRIu64,
107             tlc->pdev->udev_name, tlc->offset);
108
109         return params;
110 }
111 /*
112  * Do IO operation, called from dmstrategy routine.
113  */
114 static int
115 dm_target_linear_strategy(dm_table_entry_t *table_en, struct buf *bp)
116 {
117         dm_target_linear_config_t *tlc;
118
119         tlc = table_en->target_config;
120
121 /*      kprintf("Linear target read function called %" PRIu64 "!!\n",
122         tlc->offset);*/
123
124         bp->b_bio1.bio_offset += tlc->offset * DEV_BSIZE;
125
126         vn_strategy(tlc->pdev->pdev_vnode, &bp->b_bio1);
127
128         return 0;
129
130 }
131
132 static int
133 dm_target_linear_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
134 {
135         dm_target_linear_config_t *tlc;
136
137         tlc = table_en->target_config;
138
139         offset += tlc->offset * DEV_BSIZE;
140         offset = dm_pdev_correct_dump_offset(tlc->pdev, offset);
141
142         if (tlc->pdev->pdev_vnode->v_rdev == NULL)
143                 return ENXIO;
144
145         return dev_ddump(tlc->pdev->pdev_vnode->v_rdev, data, 0, offset, length);
146 }
147
148 /*
149  * Destroy target specific data. Decrement table pdevs.
150  */
151 static int
152 dm_target_linear_destroy(dm_table_entry_t *table_en)
153 {
154         dm_target_linear_config_t *tlc;
155
156         /*
157          * Destroy function is called for every target even if it
158          * doesn't have target_config.
159          */
160
161         if (table_en->target_config == NULL)
162                 return 0;
163
164         tlc = table_en->target_config;
165
166         /* Decrement pdev ref counter if 0 remove it */
167         dm_pdev_decr(tlc->pdev);
168
169         kfree(table_en->target_config, M_DMLINEAR);
170
171         table_en->target_config = NULL;
172
173         return 0;
174 }
175
176 static int
177 dmtl_mod_handler(module_t mod, int type, void *unused)
178 {
179         dm_target_t *dmt = NULL;
180         int err = 0;
181
182         switch(type) {
183         case MOD_LOAD:
184                 if ((dmt = dm_target_lookup("linear")) != NULL) {
185                         dm_target_unbusy(dmt);
186                         return EEXIST;
187                 }
188                 dmt = dm_target_alloc("linear");
189                 dmt->version[0] = 1;
190                 dmt->version[1] = 0;
191                 dmt->version[2] = 2;
192                 strlcpy(dmt->name, "linear", DM_MAX_TYPE_NAME);
193                 dmt->init = &dm_target_linear_init;
194                 dmt->destroy = &dm_target_linear_destroy;
195                 dmt->strategy = &dm_target_linear_strategy;
196                 dmt->table = &dm_target_linear_table;
197                 dmt->dump = &dm_target_linear_dump;
198
199                 err = dm_target_insert(dmt);
200                 if (err == 0)
201                         kprintf("dm_target_linear: Successfully initialized\n");
202                 break;
203
204         case MOD_UNLOAD:
205                 err = dm_target_rem("linear");
206                 if (err == 0)
207                         kprintf("dm_target_linear: unloaded\n");
208                 break;
209
210         default:
211                 break;
212         }
213
214         return err;
215 }
216
217 DM_TARGET_MODULE(dm_target_linear, dmtl_mod_handler);