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