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