1 /* $NetBSD: dm_target_linear.c,v 1.9 2010/01/04 00:14:41 haad Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
34 * This file implements initial version of device-mapper dklinear target.
37 #include <sys/types.h>
40 #include <sys/malloc.h>
41 #include <sys/vnode.h>
43 #include <dev/disk/dm/dm.h>
44 MALLOC_DEFINE(M_DMLINEAR, "dm_linear", "Device Mapper Target Linear");
46 typedef struct target_linear_config {
49 } dm_target_linear_config_t;
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.
57 * @argv[1] is physical data offset.
60 dm_target_linear_init(dm_table_entry_t *table_en, int argc, char **argv)
62 dm_target_linear_config_t *tlc;
66 kprintf("Linear target takes 2 args\n");
70 aprint_debug("Linear target init function called %s--%s!!\n",
73 /* Insert dmp to global pdev list */
74 if ((dmp = dm_pdev_insert(argv[0])) == NULL)
77 if ((tlc = kmalloc(sizeof(dm_target_linear_config_t), M_DMLINEAR, M_WAITOK))
82 tlc->offset = 0; /* default settings */
84 /* Check user input if it is not leave offset as 0. */
85 tlc->offset = atoi64(argv[1]);
87 dm_table_add_deps(table_en, dmp);
89 dm_table_init_target(table_en, DM_LINEAR_DEV, tlc);
94 * Table routine is called to get params string, which is target
95 * specific. When dm_table_status_ioctl is called with flag
96 * DM_STATUS_TABLE_FLAG I have to sent params string back.
99 dm_target_linear_table(void *target_config)
101 dm_target_linear_config_t *tlc;
105 aprint_debug("Linear target table function called\n");
107 /* target expects use of M_DM */
108 params = kmalloc(DM_MAX_PARAMS_SIZE, M_DM, M_WAITOK);
110 ksnprintf(params, DM_MAX_PARAMS_SIZE, "%s %" PRIu64,
111 tlc->pdev->udev_name, tlc->offset);
116 * Do IO operation, called from dmstrategy routine.
119 dm_target_linear_strategy(dm_table_entry_t *table_en, struct buf *bp)
121 dm_target_linear_config_t *tlc;
123 tlc = table_en->target_config;
125 /* kprintf("Linear target read function called %" PRIu64 "!!\n",
128 bp->b_bio1.bio_offset += tlc->offset * DEV_BSIZE;
130 vn_strategy(tlc->pdev->pdev_vnode, &bp->b_bio1);
137 dm_target_linear_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
139 dm_target_linear_config_t *tlc;
141 tlc = table_en->target_config;
143 offset += tlc->offset * DEV_BSIZE;
144 offset = dm_pdev_correct_dump_offset(tlc->pdev, offset);
146 if (tlc->pdev->pdev_vnode->v_rdev == NULL)
149 return dev_ddump(tlc->pdev->pdev_vnode->v_rdev, data, 0, offset, length);
153 * Destroy target specific data. Decrement table pdevs.
156 dm_target_linear_destroy(dm_table_entry_t *table_en)
158 dm_target_linear_config_t *tlc;
161 * Destroy function is called for every target even if it
162 * doesn't have target_config.
165 if (table_en->target_config == NULL)
168 tlc = table_en->target_config;
170 /* Decrement pdev ref counter if 0 remove it */
171 dm_pdev_decr(tlc->pdev);
173 kfree(table_en->target_config, M_DMLINEAR);
175 table_en->target_config = NULL;
181 dmtl_mod_handler(module_t mod, int type, void *unused)
183 dm_target_t *dmt = NULL;
188 if ((dmt = dm_target_lookup("linear")) != NULL) {
189 dm_target_unbusy(dmt);
192 dmt = dm_target_alloc("linear");
196 strlcpy(dmt->name, "linear", DM_MAX_TYPE_NAME);
197 dmt->init = &dm_target_linear_init;
198 dmt->table = &dm_target_linear_table;
199 dmt->strategy = &dm_target_linear_strategy;
200 dmt->destroy = &dm_target_linear_destroy;
201 dmt->dump = &dm_target_linear_dump;
203 err = dm_target_insert(dmt);
205 kprintf("dm_target_linear: Successfully initialized\n");
209 err = dm_target_rem("linear");
211 kprintf("dm_target_linear: unloaded\n");
221 DM_TARGET_MODULE(dm_target_linear, dmtl_mod_handler);