xref: /netbsd-src/sys/dev/dm/device-mapper.c (revision 10ad5ffa714ce1a679dcc9dd8159648df2d67b5a)
1 /*        $NetBSD: device-mapper.c,v 1.8 2009/06/27 16:10:25 jakllsch 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  * I want to say thank you to all people who helped me with this project.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/param.h>
38 
39 #include <sys/buf.h>
40 #include <sys/conf.h>
41 #include <sys/dkio.h>
42 #include <sys/disk.h>
43 #include <sys/disklabel.h>
44 #include <sys/ioctl.h>
45 #include <sys/ioccom.h>
46 #include <sys/kmem.h>
47 #include <sys/module.h>
48 
49 #include "netbsd-dm.h"
50 #include "dm.h"
51 
52 static dev_type_open(dmopen);
53 static dev_type_close(dmclose);
54 static dev_type_read(dmread);
55 static dev_type_write(dmwrite);
56 static dev_type_ioctl(dmioctl);
57 static dev_type_strategy(dmstrategy);
58 static dev_type_size(dmsize);
59 
60 /* attach and detach routines */
61 int dmattach(void);
62 int dmdestroy(void);
63 
64 static int dm_cmd_to_fun(prop_dictionary_t);
65 static int disk_ioctl_switch(dev_t, u_long, void *);
66 static int dm_ioctl_switch(u_long);
67 static void dmminphys(struct buf *);
68 
69 /* ***Variable-definitions*** */
70 const struct bdevsw dm_bdevsw = {
71 	.d_open = dmopen,
72 	.d_close = dmclose,
73 	.d_strategy = dmstrategy,
74 	.d_ioctl = dmioctl,
75 	.d_dump = nodump,
76 	.d_psize = dmsize,
77 	.d_flag = D_DISK | D_MPSAFE
78 };
79 
80 const struct cdevsw dm_cdevsw = {
81 	.d_open = dmopen,
82 	.d_close = dmclose,
83 	.d_read = dmread,
84 	.d_write = dmwrite,
85 	.d_ioctl = dmioctl,
86 	.d_stop = nostop,
87 	.d_tty = notty,
88 	.d_poll = nopoll,
89 	.d_mmap = nommap,
90 	.d_kqfilter = nokqfilter,
91 	.d_flag = D_DISK | D_MPSAFE
92 };
93 
94 const struct dkdriver dmdkdriver = {
95 	.d_strategy = dmstrategy
96 };
97 
98 extern uint64_t dev_counter;
99 
100 /*
101  * This array is used to translate cmd to function pointer.
102  *
103  * Interface between libdevmapper and lvm2tools uses different
104  * names for one IOCTL call because libdevmapper do another thing
105  * then. When I run "info" or "mknodes" libdevmapper will send same
106  * ioctl to kernel but will do another things in userspace.
107  *
108  */
109 struct cmd_function cmd_fn[] = {
110 		{ .cmd = "version", .fn = dm_get_version_ioctl},
111 		{ .cmd = "targets", .fn = dm_list_versions_ioctl},
112 		{ .cmd = "create",  .fn = dm_dev_create_ioctl},
113 		{ .cmd = "info",    .fn = dm_dev_status_ioctl},
114 		{ .cmd = "mknodes", .fn = dm_dev_status_ioctl},
115 		{ .cmd = "names",   .fn = dm_dev_list_ioctl},
116 		{ .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
117 		{ .cmd = "remove",  .fn = dm_dev_remove_ioctl},
118 		{ .cmd = "rename",  .fn = dm_dev_rename_ioctl},
119 		{ .cmd = "resume",  .fn = dm_dev_resume_ioctl},
120 		{ .cmd = "clear",   .fn = dm_table_clear_ioctl},
121 		{ .cmd = "deps",    .fn = dm_table_deps_ioctl},
122 		{ .cmd = "reload",  .fn = dm_table_load_ioctl},
123 		{ .cmd = "status",  .fn = dm_table_status_ioctl},
124 		{ .cmd = "table",   .fn = dm_table_status_ioctl},
125 		{NULL, NULL}
126 };
127 
128 
129 MODULE(MODULE_CLASS_DRIVER, dm, NULL);
130 
131 /* New module handle routine */
132 static int
133 dm_modcmd(modcmd_t cmd, void *arg)
134 {
135 #ifdef _MODULE
136 	int bmajor = -1, cmajor = -1;
137 
138 	switch (cmd) {
139 	case MODULE_CMD_INIT:
140 		dmattach();
141 		return devsw_attach("dm", &dm_bdevsw, &bmajor,
142 		    &dm_cdevsw, &cmajor);
143 		break;
144 
145 	case MODULE_CMD_FINI:
146 		/*
147 		 * Disable unloading of dm module if there are any devices
148 		 * defined in driver. This is probably too strong we need
149 		 * to disable auto-unload only if there is mounted dm device
150 		 * present.
151 		 */
152 		if (dev_counter > 0)
153 			return EBUSY;
154 		dmdestroy();
155 		return devsw_detach(&dm_bdevsw, &dm_cdevsw);
156 		break;
157 	case MODULE_CMD_STAT:
158 		return ENOTTY;
159 
160 	default:
161 		return ENOTTY;
162 	}
163 
164 	return 0;
165 #else
166 
167 	if (cmd == MODULE_CMD_INIT)
168 		return 0;
169 	return ENOTTY;
170 
171 #endif /* _MODULE */
172 }
173 
174 
175 /* attach routine */
176 int
177 dmattach(void)
178 {
179 	dm_target_init();
180 	dm_dev_init();
181 	dm_pdev_init();
182 
183 	return 0;
184 }
185 
186 /* Destroy routine */
187 int
188 dmdestroy(void)
189 {
190 	dm_dev_destroy();
191 	dm_pdev_destroy();
192 	dm_target_destroy();
193 
194 	return 0;
195 }
196 
197 static int
198 dmopen(dev_t dev, int flags, int mode, struct lwp *l)
199 {
200 	aprint_debug("open routine called %" PRIu32 "\n", minor(dev));
201 	return 0;
202 }
203 
204 static int
205 dmclose(dev_t dev, int flags, int mode, struct lwp *l)
206 {
207 	aprint_debug("CLOSE routine called\n");
208 
209 	return 0;
210 }
211 
212 
213 static int
214 dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
215 {
216 	int r;
217 	prop_dictionary_t dm_dict_in;
218 
219 	r = 0;
220 
221 	aprint_debug("dmioctl called\n");
222 
223 	KASSERT(data != NULL);
224 
225 	if (disk_ioctl_switch(dev, cmd, data) != 0) {
226 		struct plistref *pref = (struct plistref *) data;
227 
228 		if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
229 			return r;
230 
231 		dm_check_version(dm_dict_in);
232 
233 		/* call cmd selected function */
234 		if ((r = dm_ioctl_switch(cmd)) != 0) {
235 			prop_object_release(dm_dict_in);
236 			return r;
237 		}
238 
239 		/* run ioctl routine */
240 		if ((r = dm_cmd_to_fun(dm_dict_in)) != 0) {
241 			prop_object_release(dm_dict_in);
242 			return r;
243 		}
244 
245 		r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
246 
247 		prop_object_release(dm_dict_in);
248 	}
249 
250 	return r;
251 }
252 
253 /*
254  * Translate command sent from libdevmapper to func.
255  */
256 static int
257 dm_cmd_to_fun(prop_dictionary_t dm_dict){
258 	int i, r;
259 	prop_string_t command;
260 
261 	r = 0;
262 
263 	if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
264 		return EINVAL;
265 
266 	for(i = 0; cmd_fn[i].cmd != NULL; i++)
267 		if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
268 			break;
269 
270 	if (cmd_fn[i].cmd == NULL)
271 		return EINVAL;
272 
273 	aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
274 	r = cmd_fn[i].fn(dm_dict);
275 
276 	return r;
277 }
278 
279 /* Call apropriate ioctl handler function. */
280 static int
281 dm_ioctl_switch(u_long cmd)
282 {
283 	int r;
284 
285 	r = 0;
286 
287 	switch(cmd) {
288 
289 	case NETBSD_DM_IOCTL:
290 		aprint_debug("NetBSD_DM_IOCTL called\n");
291 		break;
292 
293 	default:
294 		 aprint_debug("unknown ioctl called\n");
295 		 return ENOTTY;
296 		 break; /* NOT REACHED */
297 	}
298 
299 	 return r;
300 }
301 
302  /*
303   * Check for disk specific ioctls.
304   */
305 
306 static int
307 disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
308 {
309 	dm_dev_t *dmv;
310 
311 	switch(cmd) {
312 	case DIOCGWEDGEINFO:
313 	{
314 		struct dkwedge_info *dkw = (void *) data;
315 
316 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
317 			return ENOENT;
318 
319 		aprint_normal("DIOCGWEDGEINFO ioctl called\n");
320 
321 		strlcpy(dkw->dkw_devname, dmv->name, 16);
322 		strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
323 		strlcpy(dkw->dkw_parent, dmv->name, 16);
324 
325 		dkw->dkw_offset = 0;
326 		dkw->dkw_size = dm_table_size(&dmv->table_head);
327 		strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
328 
329 		dm_dev_unbusy(dmv);
330 		break;
331 	}
332 
333 	case DIOCGDISKINFO:
334 	{
335 		struct plistref *pref = (struct plistref *) data;
336 
337 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
338 			return ENOENT;
339 
340 		if (dmv->diskp->dk_info == NULL) {
341 			dm_dev_unbusy(dmv);
342 			return ENOTSUP;
343 		} else
344 			prop_dictionary_copyout_ioctl(pref, cmd,
345 			    dmv->diskp->dk_info);
346 
347 		dm_dev_unbusy(dmv);
348 
349 		break;
350 	}
351 
352 	default:
353 		aprint_debug("unknown disk_ioctl called\n");
354 		return 1;
355 		break; /* NOT REACHED */
356 	}
357 
358 	return 0;
359 }
360 
361 /*
362  * Do all IO operations on dm logical devices.
363  */
364 static void
365 dmstrategy(struct buf *bp)
366 {
367 	dm_dev_t *dmv;
368 	dm_table_t  *tbl;
369 	dm_table_entry_t *table_en;
370 	struct buf *nestbuf;
371 
372 	uint32_t dev_type;
373 
374 	uint64_t buf_start, buf_len, issued_len;
375 	uint64_t table_start, table_end;
376 	uint64_t start, end;
377 
378 	buf_start = bp->b_blkno * DEV_BSIZE;
379 	buf_len = bp->b_bcount;
380 
381 	tbl = NULL;
382 
383 	table_end = 0;
384 	dev_type = 0;
385 	issued_len = 0;
386 
387 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
388 		bp->b_error = EIO;
389 		bp->b_resid = bp->b_bcount;
390 		biodone(bp);
391 		return;
392 	}
393 
394 	if (bounds_check_with_mediasize(bp, DEV_BSIZE,
395 	    dm_table_size(&dmv->table_head)) <= 0) {
396 		dm_dev_unbusy(dmv);
397 		bp->b_resid = bp->b_bcount;
398 		biodone(bp);
399 		return;
400 	}
401 
402 	/* FIXME: have to be called with IPL_BIO*/
403 	disk_busy(dmv->diskp);
404 
405 	/* Select active table */
406 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
407 
408 	 /* Nested buffers count down to zero therefore I have
409 	    to set bp->b_resid to maximal value. */
410 	bp->b_resid = bp->b_bcount;
411 
412 	/*
413 	 * Find out what tables I want to select.
414 	 */
415 	SLIST_FOREACH(table_en, tbl, next)
416 	{
417 		/* I need need number of bytes not blocks. */
418 		table_start = table_en->start * DEV_BSIZE;
419 		/*
420 		 * I have to sub 1 from table_en->length to prevent
421 		 * off by one error
422 		 */
423 		table_end = table_start + (table_en->length)* DEV_BSIZE;
424 
425 		start = MAX(table_start, buf_start);
426 
427 		end = MIN(table_end, buf_start + buf_len);
428 
429 		aprint_debug("----------------------------------------\n");
430 		aprint_debug("table_start %010" PRIu64", table_end %010"
431 		    PRIu64 "\n", table_start, table_end);
432 		aprint_debug("buf_start %010" PRIu64", buf_len %010"
433 		    PRIu64"\n", buf_start, buf_len);
434 		aprint_debug("start-buf_start %010"PRIu64", end %010"
435 		    PRIu64"\n", start - buf_start, end);
436 		aprint_debug("start %010" PRIu64" , end %010"
437                     PRIu64"\n", start, end);
438 		aprint_debug("\n----------------------------------------\n");
439 
440 		if (start < end) {
441 			/* create nested buffer  */
442 			nestbuf = getiobuf(NULL, true);
443 
444 			nestiobuf_setup(bp, nestbuf, start - buf_start,
445 			    (end - start));
446 
447 			issued_len += end - start;
448 
449 			/* I need number of blocks. */
450 			nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
451 
452 			table_en->target->strategy(table_en, nestbuf);
453 		}
454 	}
455 
456 	if (issued_len < buf_len)
457 		nestiobuf_done(bp, buf_len - issued_len, EINVAL);
458 
459 	/* FIXME have to be called with SPL_BIO*/
460 	disk_unbusy(dmv->diskp, buf_len, bp != NULL ? bp->b_flags & B_READ : 0);
461 
462 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
463 	dm_dev_unbusy(dmv);
464 
465 	return;
466 }
467 
468 
469 static int
470 dmread(dev_t dev, struct uio *uio, int flag)
471 {
472 	return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
473 }
474 
475 static int
476 dmwrite(dev_t dev, struct uio *uio, int flag)
477 {
478 	return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
479 }
480 
481 static int
482 dmsize(dev_t dev)
483 {
484 	dm_dev_t *dmv;
485 	uint64_t size;
486 
487 	size = 0;
488 
489 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
490 			return -ENOENT;
491 
492 	size = dm_table_size(&dmv->table_head);
493 	dm_dev_unbusy(dmv);
494 
495   	return size;
496 }
497 
498 static void
499 dmminphys(struct buf *bp)
500 {
501 	bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
502 }
503 
504 void
505 dmgetproperties(struct disk *disk, dm_table_head_t *head)
506 {
507 	prop_dictionary_t disk_info, odisk_info, geom;
508 	int dmp_size;
509 
510 	dmp_size = dm_table_size(head);
511 
512 	disk_info = prop_dictionary_create();
513 
514 	prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI");
515 
516 	geom = prop_dictionary_create();
517 
518 	prop_dictionary_set_uint64(geom, "sectors-per-unit", dmp_size);
519 
520 	prop_dictionary_set_uint32(geom, "sector-size",
521 	    DEV_BSIZE /* XXX 512? */);
522 
523 	prop_dictionary_set_uint32(geom, "sectors-per-track", 32);
524 
525 	prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64);
526 
527 	prop_dictionary_set_uint32(geom, "cylinders-per-unit", dmp_size / 2048);
528 
529 	prop_dictionary_set(disk_info, "geometry", geom);
530 	prop_object_release(geom);
531 
532 	odisk_info = disk->dk_info;
533 
534 	disk->dk_info = disk_info;
535 
536 	if (odisk_info != NULL)
537 		prop_object_release(odisk_info);
538 }
539