xref: /dflybsd-src/sys/dev/disk/dm/device-mapper.c (revision dddde3e17bc4375bf5a7c778c776b11ca28a1908)
1 /*        $NetBSD: device-mapper.c,v 1.22 2010/03/26 15:46:04 jakllsch Exp $ */
2 
3 /*
4  * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5  * Copyright (c) 2010 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Adam Hamsik.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * I want to say thank you to all people who helped me with this project.
35  */
36 
37 #include <sys/types.h>
38 #include <sys/ctype.h>
39 
40 #include <sys/buf.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43 #include <sys/disk.h>
44 #include <sys/disklabel.h>
45 #include <sys/dtype.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/sysctl.h>
49 #include <dev/disk/dm/dm.h>
50 
51 #include "netbsd-dm.h"
52 
53 static	d_ioctl_t	dmioctl;
54 static	d_open_t	dmopen;
55 static	d_close_t	dmclose;
56 static	d_psize_t	dmsize;
57 static	d_strategy_t	dmstrategy;
58 static	d_dump_t	dmdump;
59 
60 /* New module handle and destroy routines */
61 static int dm_modcmd(module_t mod, int cmd, void *unused);
62 static int dmdestroy(void);
63 
64 static void dm_doinit(void);
65 
66 static int dm_cmd_to_fun(prop_dictionary_t);
67 static int disk_ioctl_switch(cdev_t, u_long, void *);
68 static int dm_ioctl_switch(u_long);
69 #if 0
70 static void dmminphys(struct buf *);
71 #endif
72 
73 /* ***Variable-definitions*** */
74 struct dev_ops dm_ops = {
75 	{ "dm", 0, D_DISK | D_MPSAFE },
76 	.d_open		= dmopen,
77 	.d_close	= dmclose,
78 	.d_read		= physread,
79 	.d_write	= physwrite,
80 	.d_ioctl	= dmioctl,
81 	.d_strategy	= dmstrategy,
82 	.d_psize	= dmsize,
83 	.d_dump		= dmdump,
84 /* D_DISK */
85 };
86 
87 MALLOC_DEFINE(M_DM, "dm", "Device Mapper allocations");
88 
89 int dm_debug_level = 0;
90 
91 extern uint64_t dm_dev_counter;
92 
93 static cdev_t dmcdev;
94 
95 static moduledata_t dm_mod = {
96     "dm",
97     dm_modcmd,
98     NULL
99 };
100 DECLARE_MODULE(dm, dm_mod, SI_SUB_RAID, SI_ORDER_ANY);
101 MODULE_VERSION(dm, 1);
102 
103 /*
104  * This array is used to translate cmd to function pointer.
105  *
106  * Interface between libdevmapper and lvm2tools uses different
107  * names for one IOCTL call because libdevmapper do another thing
108  * then. When I run "info" or "mknodes" libdevmapper will send same
109  * ioctl to kernel but will do another things in userspace.
110  *
111  */
112 static struct cmd_function cmd_fn[] = {
113 		{ .cmd = "version", .fn = NULL},
114 		{ .cmd = "targets", .fn = dm_list_versions_ioctl},
115 		{ .cmd = "create",  .fn = dm_dev_create_ioctl},
116 		{ .cmd = "info",    .fn = dm_dev_status_ioctl},
117 		{ .cmd = "mknodes", .fn = dm_dev_status_ioctl},
118 		{ .cmd = "names",   .fn = dm_dev_list_ioctl},
119 		{ .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
120 		{ .cmd = "remove",  .fn = dm_dev_remove_ioctl},
121 		{ .cmd = "remove_all", .fn = dm_dev_remove_all_ioctl},
122 		{ .cmd = "rename",  .fn = dm_dev_rename_ioctl},
123 		{ .cmd = "resume",  .fn = dm_dev_resume_ioctl},
124 		{ .cmd = "clear",   .fn = dm_table_clear_ioctl},
125 		{ .cmd = "deps",    .fn = dm_table_deps_ioctl},
126 		{ .cmd = "reload",  .fn = dm_table_load_ioctl},
127 		{ .cmd = "status",  .fn = dm_table_status_ioctl},
128 		{ .cmd = "table",   .fn = dm_table_status_ioctl},
129 		{ .cmd = "message", .fn = dm_message_ioctl},
130 		{NULL, NULL}
131 };
132 
133 /* New module handle routine */
134 static int
135 dm_modcmd(module_t mod, int cmd, void *unused)
136 {
137 	int error;
138 
139 	error = 0;
140 
141 	switch (cmd) {
142 	case MOD_LOAD:
143 		dm_doinit();
144 		kprintf("Device Mapper version %d.%d.%d loaded\n",
145 		    DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL);
146 		break;
147 
148 	case MOD_UNLOAD:
149 		/*
150 		 * Disable unloading of dm module if there are any devices
151 		 * defined in driver. This is probably too strong we need
152 		 * to disable auto-unload only if there is mounted dm device
153 		 * present.
154 		 */
155 		if (dm_dev_counter > 0)
156 			return EBUSY;
157 
158 		error = dmdestroy();
159 		if (error)
160 			break;
161 		kprintf("Device Mapper unloaded\n");
162 		break;
163 
164 	default:
165 		break;
166 	}
167 
168 	return error;
169 }
170 
171 static void
172 dm_doinit(void)
173 {
174 	dm_target_init();
175 	dm_dev_init();
176 	dm_pdev_init();
177 	dmcdev = make_dev(&dm_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control");
178 }
179 
180 /* Destroy routine */
181 static int
182 dmdestroy(void)
183 {
184 	destroy_dev(dmcdev);
185 
186 	dm_dev_uninit();
187 	dm_pdev_uninit();
188 	dm_target_uninit();
189 
190 	return 0;
191 }
192 
193 static int
194 dmopen(struct dev_open_args *ap)
195 {
196 	cdev_t dev = ap->a_head.a_dev;
197 	dm_dev_t *dmv;
198 
199 	/* Shortcut for the control device */
200 	if (minor(dev) == 0)
201 		return 0;
202 
203 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
204 		return ENXIO;
205 
206 	dmv->is_open = 1;
207 	dm_dev_unbusy(dmv);
208 
209 	aprint_debug("dm open routine called %" PRIu32 "\n",
210 	    minor(ap->a_head.a_dev));
211 	return 0;
212 }
213 
214 static int
215 dmclose(struct dev_close_args *ap)
216 {
217 	cdev_t dev = ap->a_head.a_dev;
218 	dm_dev_t *dmv;
219 
220 	/* Shortcut for the control device */
221 	if (minor(dev) == 0)
222 		return 0;
223 
224 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
225 		return ENXIO;
226 
227 	dmv->is_open = 0;
228 	dm_dev_unbusy(dmv);
229 
230 	aprint_debug("dm close routine called %" PRIu32 "\n",
231 	    minor(ap->a_head.a_dev));
232 	return 0;
233 }
234 
235 
236 static int
237 dmioctl(struct dev_ioctl_args *ap)
238 {
239 	cdev_t dev = ap->a_head.a_dev;
240 	u_long cmd = ap->a_cmd;
241 	void *data = ap->a_data;
242 	struct plistref *pref = (struct plistref *)data;
243 
244 	int r, err;
245 	prop_dictionary_t dm_dict_in;
246 
247 	err = r = 0;
248 
249 	aprint_debug("dmioctl called\n");
250 	KKASSERT(data != NULL);
251 
252 	if ((r = disk_ioctl_switch(dev, cmd, data)) != ENOTTY)
253 		return r;  /* Handled disk ioctl */
254 
255 	if ((r = dm_ioctl_switch(cmd)) != 0)
256 		return r;  /* Not NETBSD_DM_IOCTL */
257 
258 	if ((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
259 		return r;
260 
261 	if ((r = dm_check_version(dm_dict_in)) == 0)
262 		err = dm_cmd_to_fun(dm_dict_in);
263 
264 	r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
265 	prop_object_release(dm_dict_in);
266 
267 	/* Return the dm ioctl error if any. */
268 	if (err)
269 		return err;
270 	return r;
271 }
272 
273 /*
274  * Translate command sent from libdevmapper to func.
275  */
276 static int
277 dm_cmd_to_fun(prop_dictionary_t dm_dict)
278 {
279 	int i, r;
280 	prop_string_t command;
281 
282 	r = 0;
283 
284 	if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
285 		return EINVAL;
286 
287 	for (i = 0; cmd_fn[i].cmd != NULL; i++)
288 		if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
289 			break;
290 
291 	if (cmd_fn[i].cmd == NULL)
292 		return EINVAL;
293 
294 	aprint_debug("ioctl %s called %p\n", cmd_fn[i].cmd, cmd_fn[i].fn);
295 	if (cmd_fn[i].fn == NULL)
296 		return 0;  /* No handler required */
297 	r = cmd_fn[i].fn(dm_dict);
298 
299 	return r;
300 }
301 
302 /* Call apropriate ioctl handler function. */
303 static int
304 dm_ioctl_switch(u_long cmd)
305 {
306 
307 	switch(cmd) {
308 	case NETBSD_DM_IOCTL:
309 		aprint_debug("dm NETBSD_DM_IOCTL called\n");
310 		break;
311 	default:
312 		aprint_debug("dm unknown ioctl called\n");
313 		return ENOTTY;
314 		break; /* NOT REACHED */
315 	}
316 
317 	return 0;
318 }
319 
320  /*
321   * Check for disk specific ioctls.
322   */
323 
324 static int
325 disk_ioctl_switch(cdev_t dev, u_long cmd, void *data)
326 {
327 	dm_dev_t *dmv;
328 
329 	/* disk ioctls make sense only on block devices */
330 	if (minor(dev) == 0)
331 		return ENOTTY;
332 
333 	switch(cmd) {
334 	case DIOCGPART:
335 		if ((dmv = dev->si_drv1) == NULL)
336 			return ENODEV;
337 		if (dmv->diskp->d_info.d_media_blksize == 0) {
338 			return ENOTSUP;
339 		} else {
340 			u_int64_t size;
341 			struct partinfo *dpart = data;
342 			bzero(dpart, sizeof(*dpart));
343 
344 			size = dm_table_size(&dmv->table_head);
345 			dpart->media_offset  = 0;
346 			dpart->media_size    = size * DEV_BSIZE;
347 			dpart->media_blocks  = size;
348 			dpart->media_blksize = DEV_BSIZE;
349 			dpart->fstype = FS_BSDFFS;
350 		}
351 		break;
352 
353 	default:
354 		aprint_debug("unknown disk_ioctl called\n");
355 		return ENOTTY;
356 		break; /* NOT REACHED */
357 	}
358 
359 	return 0;
360 }
361 
362 /*
363  * Do all IO operations on dm logical devices.
364  */
365 static int
366 dmstrategy(struct dev_strategy_args *ap)
367 {
368 	cdev_t dev = ap->a_head.a_dev;
369 	struct bio *bio = ap->a_bio;
370 	struct buf *bp = bio->bio_buf;
371 	int bypass;
372 
373 	dm_dev_t *dmv;
374 	dm_table_t  *tbl;
375 	dm_table_entry_t *table_en;
376 	struct buf *nestbuf;
377 
378 	uint64_t buf_start, buf_len, issued_len;
379 	uint64_t table_start, table_end;
380 	uint64_t start, end;
381 
382 	buf_start = bio->bio_offset;
383 	buf_len = bp->b_bcount;
384 
385 	tbl = NULL;
386 
387 	table_end = 0;
388 	issued_len = 0;
389 
390 	dmv = dev->si_drv1;
391 
392 	switch(bp->b_cmd) {
393 	case BUF_CMD_READ:
394 	case BUF_CMD_WRITE:
395 	case BUF_CMD_FREEBLKS:
396 		bypass = 0;
397 		break;
398 	case BUF_CMD_FLUSH:
399 		bypass = 1;
400 		KKASSERT(buf_len == 0);
401 		break;
402 	default:
403 		bp->b_error = EIO;
404 		bp->b_resid = bp->b_bcount;
405 		biodone(bio);
406 		return 0;
407 	}
408 
409 	if (bypass == 0 &&
410 	    bounds_check_with_mediasize(bio, DEV_BSIZE,
411 					dm_table_size(&dmv->table_head)) <= 0) {
412 		bp->b_resid = bp->b_bcount;
413 		biodone(bio);
414 		return 0;
415 	}
416 
417 	/* Select active table */
418 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
419 
420 	nestiobuf_init(bio);
421 	devstat_start_transaction(&dmv->stats);
422 
423 	/*
424 	 * Find out what tables I want to select.
425 	 */
426 	SLIST_FOREACH(table_en, tbl, next) {
427 		/*
428 		 * I need need number of bytes not blocks.
429 		 */
430 		table_start = table_en->start * DEV_BSIZE;
431 		table_end = table_start + (table_en->length) * DEV_BSIZE;
432 
433 		/*
434 		 * Calculate the start and end
435 		 */
436 		start = MAX(table_start, buf_start);
437 		end = MIN(table_end, buf_start + buf_len);
438 
439 		if (dm_debug_level) {
440 			aprint_normal("----------------------------------------\n");
441 			aprint_normal("table_start %010" PRIu64", table_end %010"
442 			    PRIu64 "\n", table_start, table_end);
443 			aprint_normal("buf_start %010" PRIu64", buf_len %010"
444 			    PRIu64"\n", buf_start, buf_len);
445 			aprint_normal("start-buf_start %010"PRIu64", end %010"
446 			    PRIu64"\n", start - buf_start, end);
447 			aprint_normal("start %010" PRIu64", end %010"
448 			    PRIu64"\n", start, end);
449 			aprint_normal("----------------------------------------\n");
450 		}
451 
452 		if (bypass) {
453 			nestbuf = getpbuf(NULL);
454 			nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
455 
456 			nestiobuf_add(bio, nestbuf, 0, 0, &dmv->stats);
457 			nestbuf->b_bio1.bio_offset = 0;
458 			table_en->target->strategy(table_en, nestbuf);
459 		} else if (start < end) {
460 			nestbuf = getpbuf(NULL);
461 			nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
462 
463 			nestiobuf_add(bio, nestbuf,
464 				      start - buf_start, (end - start),
465 				      &dmv->stats);
466 			issued_len += end - start;
467 
468 			nestbuf->b_bio1.bio_offset = (start - table_start);
469 			table_en->target->strategy(table_en, nestbuf);
470 		}
471 	}
472 
473 	if (issued_len < buf_len)
474 		nestiobuf_error(bio, EINVAL);
475 	nestiobuf_start(bio);
476 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
477 
478 	return 0;
479 }
480 
481 static int
482 dmdump(struct dev_dump_args *ap)
483 {
484 	cdev_t dev = ap->a_head.a_dev;
485 	dm_dev_t *dmv;
486 	dm_table_t  *tbl;
487 	dm_table_entry_t *table_en;
488 	uint64_t buf_start, buf_len, issued_len;
489 	uint64_t table_start, table_end;
490 	uint64_t start, end, data_offset;
491 	off_t offset;
492 	size_t length;
493 	int error = 0;
494 
495 	buf_start = ap->a_offset;
496 	buf_len = ap->a_length;
497 
498 	tbl = NULL;
499 
500 	table_end = 0;
501 	issued_len = 0;
502 
503 	dmv = dev->si_drv1;
504 
505 	/* Select active table */
506 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
507 
508 
509 	/*
510 	 * Find out what tables I want to select.
511 	 */
512 	SLIST_FOREACH(table_en, tbl, next) {
513 		/*
514 		 * I need need number of bytes not blocks.
515 		 */
516 		table_start = table_en->start * DEV_BSIZE;
517 		table_end = table_start + (table_en->length) * DEV_BSIZE;
518 
519 		/*
520 		 * Calculate the start and end
521 		 */
522 		start = MAX(table_start, buf_start);
523 		end = MIN(table_end, buf_start + buf_len);
524 
525 		if (ap->a_length == 0) {
526 			if (table_en->target->dump == NULL) {
527 				error = ENXIO;
528 				goto out;
529 			}
530 
531 			table_en->target->dump(table_en, NULL, 0, 0);
532 		} else if (start < end) {
533 			data_offset = start - buf_start;
534 			offset = start - table_start;
535 			length = end - start;
536 
537 			if (table_en->target->dump == NULL) {
538 				error = ENXIO;
539 				goto out;
540 			}
541 
542 			table_en->target->dump(table_en,
543 			    (char *)ap->a_virtual + data_offset,
544 			    length, offset);
545 
546 			issued_len += end - start;
547 		}
548 	}
549 
550 	if (issued_len < buf_len)
551 		error = EINVAL;
552 
553 out:
554 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
555 
556 	return error;
557 }
558 
559 static int
560 dmsize(struct dev_psize_args *ap)
561 {
562 	cdev_t dev = ap->a_head.a_dev;
563 	dm_dev_t *dmv;
564 	uint64_t size;
565 
566 	size = 0;
567 
568 	if ((dmv = dev->si_drv1) == NULL)
569 		return ENXIO;
570 
571 	size = dm_table_size(&dmv->table_head);
572 	ap->a_result = (int64_t)size;
573 
574 	return 0;
575 }
576 
577 #if 0
578 static void
579 dmminphys(struct buf *bp)
580 {
581 
582 	bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
583 }
584 #endif
585 
586 void
587 dmsetdiskinfo(struct disk *disk, dm_table_head_t *head)
588 {
589 	struct disk_info info;
590 	uint64_t dmp_size;
591 
592 	dmp_size = dm_table_size(head);
593 
594 	bzero(&info, sizeof(struct disk_info));
595 	info.d_media_blksize = DEV_BSIZE;
596 	info.d_media_blocks = dmp_size;
597 #if 0
598 	/* this is set by disk_setdiskinfo */
599 	info.d_media_size = dmp_size * DEV_BSIZE;
600 #endif
601 	info.d_dsflags = DSO_MBRQUIET | DSO_DEVICEMAPPER | DSO_RAWPSIZE;
602 
603 	info.d_secpertrack = 32;
604 	info.d_nheads = 64;
605 	info.d_secpercyl = info.d_secpertrack * info.d_nheads;
606 	info.d_ncylinders = dmp_size / info.d_secpercyl;
607 
608 	disk_setdiskinfo(disk, &info);
609 }
610 
611 /*
612  * Transform char s to uint64_t offset number.
613  */
614 uint64_t
615 atoi64(const char *s)
616 {
617 	uint64_t n;
618 	n = 0;
619 
620 	while (*s != '\0') {
621 		if (!isdigit(*s))
622 			break;
623 
624 		n = (10 * n) + (*s - '0');
625 		s++;
626 	}
627 
628 	return n;
629 }
630 
631 void
632 dm_builtin_init(void *arg)
633 {
634 	modeventhand_t evh = (modeventhand_t)arg;
635 
636 	KKASSERT(evh != NULL);
637 	evh(NULL, MOD_LOAD, NULL);
638 }
639 
640 void
641 dm_builtin_uninit(void *arg)
642 {
643 	modeventhand_t evh = (modeventhand_t)arg;
644 
645 	KKASSERT(evh != NULL);
646 	evh(NULL, MOD_UNLOAD, NULL);
647 }
648 
649 TUNABLE_INT("debug.dm_debug", &dm_debug_level);
650 SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level,
651 	       0, "Enable device mapper debugging");
652 
653