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