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