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>
38 #include <sys/param.h>
39 #include <sys/ctype.h>
42 #include <sys/malloc.h>
43 #include <sys/vnode.h>
46 MALLOC_DEFINE(M_DMLINEAR, "dm_linear", "Device Mapper Target Linear");
49 * Allocate target specific config data, and link them to table.
50 * This function is called only when, flags is not READONLY and
51 * therefore we can add things to pdev list. This should not a
52 * problem because this routine is called only from dm_table_load_ioctl.
54 * @argv[1] is physical data offset.
57 dm_target_linear_init(dm_dev_t * dmv, void **target_config, char *params)
59 dm_target_linear_config_t *tlc;
68 * Parse a string, containing tokens delimited by white space,
69 * into an argument vector
71 for (ap = argv; ap < &argv[2] &&
72 (*ap = strsep(¶ms, " \t")) != NULL;) {
77 aprint_debug("Linear target init function called %s--%s!!\n",
84 /* Insert dmp to global pdev list */
85 if ((dmp = dm_pdev_insert(argv[0])) == NULL)
88 if ((tlc = kmalloc(sizeof(dm_target_linear_config_t), M_DMLINEAR, M_WAITOK))
93 tlc->offset = 0; /* default settings */
95 /* Check user input if it is not leave offset as 0. */
96 tlc->offset = atoi64(argv[1]);
100 dmv->dev_type = DM_LINEAR_DEV;
105 * Status routine is called to get params string, which is target
106 * specific. When dm_table_status_ioctl is called with flag
107 * DM_STATUS_TABLE_FLAG I have to sent params string back.
110 dm_target_linear_status(void *target_config)
112 dm_target_linear_config_t *tlc;
116 aprint_debug("Linear target status function called\n");
118 /* target expects use of M_DM */
119 params = kmalloc(DM_MAX_PARAMS_SIZE, M_DM, M_WAITOK);
121 aprint_normal("%s %" PRIu64, tlc->pdev->name, tlc->offset);
122 ksnprintf(params, DM_MAX_PARAMS_SIZE, "%s %" PRIu64,
123 tlc->pdev->name, tlc->offset);
128 * Do IO operation, called from dmstrategy routine.
131 dm_target_linear_strategy(dm_table_entry_t * table_en, struct buf * bp)
133 dm_target_linear_config_t *tlc;
135 tlc = table_en->target_config;
137 /* printf("Linear target read function called %" PRIu64 "!!\n",
141 bp->b_blkno += tlc->offset;
143 bp->b_bio1.bio_offset += tlc->offset * DEV_BSIZE;
145 vn_strategy(tlc->pdev->pdev_vnode, &bp->b_bio1);
151 * Destroy target specific data. Decrement table pdevs.
154 dm_target_linear_destroy(dm_table_entry_t * table_en)
156 dm_target_linear_config_t *tlc;
159 * Destroy function is called for every target even if it
160 * doesn't have target_config.
163 if (table_en->target_config == NULL)
166 tlc = table_en->target_config;
168 /* Decrement pdev ref counter if 0 remove it */
169 dm_pdev_decr(tlc->pdev);
171 /* Unbusy target so we can unload it */
172 dm_target_unbusy(table_en->target);
174 kfree(table_en->target_config, M_DMLINEAR);
176 table_en->target_config = NULL;
180 /* Add this target pdev dependiences to prop_array_t */
182 dm_target_linear_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
184 dm_target_linear_config_t *tlc;
189 if (table_en->target_config == NULL)
192 tlc = table_en->target_config;
194 if ((error = VOP_GETATTR(tlc->pdev->pdev_vnode, &va)) != 0)
197 prop_array_add_uint64(prop_array,
198 (uint64_t) makeudev(va.va_rmajor, va.va_rminor));
203 * Register upcall device.
204 * Linear target doesn't need any upcall devices but other targets like
205 * mirror, snapshot, multipath, stripe will use this functionality.
208 dm_target_linear_upcall(dm_table_entry_t * table_en, struct buf * bp)
213 * Transform char s to uint64_t offset number.
216 atoi64(const char *s)
225 n = (10 * n) + (*s - '0');