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