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