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