device-mapper.c revision 1.24 1 /* $NetBSD: device-mapper.c,v 1.24 2010/10/09 12:56:06 haad 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/dkio.h>
43 #include <sys/disk.h>
44 #include <sys/disklabel.h>
45 #include <sys/ioctl.h>
46 #include <sys/ioccom.h>
47 #include <sys/kmem.h>
48
49 #include "netbsd-dm.h"
50 #include "dm.h"
51
52 static dev_type_open(dmopen);
53 static dev_type_close(dmclose);
54 static dev_type_read(dmread);
55 static dev_type_write(dmwrite);
56 static dev_type_ioctl(dmioctl);
57 static dev_type_strategy(dmstrategy);
58 static dev_type_size(dmsize);
59
60 /* attach and detach routines */
61 void dmattach(int);
62 #ifdef _MODULE
63 static int dmdestroy(void);
64 #endif
65
66 static void dm_doinit(void);
67
68 static int dm_cmd_to_fun(prop_dictionary_t);
69 static int disk_ioctl_switch(dev_t, u_long, void *);
70 static int dm_ioctl_switch(u_long);
71 static void dmminphys(struct buf *);
72
73 /* CF attach/detach functions used for power management */
74 static int dm_detach(device_t, int);
75 static void dm_attach(device_t, device_t, void *);
76 static int dm_match(device_t, cfdata_t, void *);
77
78 /* ***Variable-definitions*** */
79 const struct bdevsw dm_bdevsw = {
80 .d_open = dmopen,
81 .d_close = dmclose,
82 .d_strategy = dmstrategy,
83 .d_ioctl = dmioctl,
84 .d_dump = nodump,
85 .d_psize = dmsize,
86 .d_flag = D_DISK | D_MPSAFE
87 };
88
89 const struct cdevsw dm_cdevsw = {
90 .d_open = dmopen,
91 .d_close = dmclose,
92 .d_read = dmread,
93 .d_write = dmwrite,
94 .d_ioctl = dmioctl,
95 .d_stop = nostop,
96 .d_tty = notty,
97 .d_poll = nopoll,
98 .d_mmap = nommap,
99 .d_kqfilter = nokqfilter,
100 .d_flag = D_DISK | D_MPSAFE
101 };
102
103 const struct dkdriver dmdkdriver = {
104 .d_strategy = dmstrategy
105 };
106
107 CFATTACH_DECL3_NEW(dm, 0,
108 dm_match, dm_attach, dm_detach, NULL, NULL, NULL,
109 DVF_DETACH_SHUTDOWN);
110
111 extern struct cfdriver dm_cd;
112
113 extern uint64_t dm_dev_counter;
114
115 /*
116 * This array is used to translate cmd to function pointer.
117 *
118 * Interface between libdevmapper and lvm2tools uses different
119 * names for one IOCTL call because libdevmapper do another thing
120 * then. When I run "info" or "mknodes" libdevmapper will send same
121 * ioctl to kernel but will do another things in userspace.
122 *
123 */
124 struct cmd_function cmd_fn[] = {
125 { .cmd = "version", .fn = dm_get_version_ioctl},
126 { .cmd = "targets", .fn = dm_list_versions_ioctl},
127 { .cmd = "create", .fn = dm_dev_create_ioctl},
128 { .cmd = "info", .fn = dm_dev_status_ioctl},
129 { .cmd = "mknodes", .fn = dm_dev_status_ioctl},
130 { .cmd = "names", .fn = dm_dev_list_ioctl},
131 { .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
132 { .cmd = "remove", .fn = dm_dev_remove_ioctl},
133 { .cmd = "rename", .fn = dm_dev_rename_ioctl},
134 { .cmd = "resume", .fn = dm_dev_resume_ioctl},
135 { .cmd = "clear", .fn = dm_table_clear_ioctl},
136 { .cmd = "deps", .fn = dm_table_deps_ioctl},
137 { .cmd = "reload", .fn = dm_table_load_ioctl},
138 { .cmd = "status", .fn = dm_table_status_ioctl},
139 { .cmd = "table", .fn = dm_table_status_ioctl},
140 {NULL, NULL}
141 };
142
143 #ifdef _MODULE
144 #include <sys/module.h>
145
146 /* Autoconf defines */
147 CFDRIVER_DECL(dm, DV_DISK, NULL);
148
149 MODULE(MODULE_CLASS_DRIVER, dm, NULL);
150
151 /* New module handle routine */
152 static int
153 dm_modcmd(modcmd_t cmd, void *arg)
154 {
155 #ifdef _MODULE
156 int error, bmajor, cmajor;
157
158 error = 0;
159 bmajor = -1;
160 cmajor = -1;
161
162 switch (cmd) {
163 case MODULE_CMD_INIT:
164 error = config_cfdriver_attach(&dm_cd);
165 if (error)
166 break;
167
168 error = config_cfattach_attach(dm_cd.cd_name, &dm_ca);
169 if (error) {
170 aprint_error("%s: unable to register cfattach\n",
171 dm_cd.cd_name);
172 return error;
173 }
174
175 error = devsw_attach(dm_cd.cd_name, &dm_bdevsw, &bmajor,
176 &dm_cdevsw, &cmajor);
177 if (error == EEXIST)
178 error = 0;
179 if (error) {
180 config_cfattach_detach(dm_cd.cd_name, &dm_ca);
181 config_cfdriver_detach(&dm_cd);
182 break;
183 }
184
185 dm_doinit();
186
187 break;
188
189 case MODULE_CMD_FINI:
190 /*
191 * Disable unloading of dm module if there are any devices
192 * defined in driver. This is probably too strong we need
193 * to disable auto-unload only if there is mounted dm device
194 * present.
195 */
196 if (dm_dev_counter > 0)
197 return EBUSY;
198
199 error = dmdestroy();
200 if (error)
201 break;
202
203 config_cfdriver_detach(&dm_cd);
204
205 devsw_detach(&dm_bdevsw, &dm_cdevsw);
206 break;
207 case MODULE_CMD_STAT:
208 return ENOTTY;
209
210 default:
211 return ENOTTY;
212 }
213
214 return error;
215 #else
216 return ENOTTY;
217 #endif
218 }
219 #endif /* _MODULE */
220
221 /*
222 * dm_match:
223 *
224 * Autoconfiguration match function for pseudo-device glue.
225 */
226 static int
227 dm_match(device_t parent, cfdata_t match,
228 void *aux)
229 {
230
231 /* Pseudo-device; always present. */
232 return (1);
233 }
234
235 /*
236 * dm_attach:
237 *
238 * Autoconfiguration attach function for pseudo-device glue.
239 */
240 static void
241 dm_attach(device_t parent, device_t self,
242 void *aux)
243 {
244 return;
245 }
246
247
248 /*
249 * dm_detach:
250 *
251 * Autoconfiguration detach function for pseudo-device glue.
252 * This routine is called by dm_ioctl::dm_dev_remove_ioctl and by autoconf to
253 * remove devices created in device-mapper.
254 */
255 static int
256 dm_detach(device_t self, int flags)
257 {
258 dm_dev_t *dmv;
259
260 /* Detach device from global device list */
261 if ((dmv = dm_dev_detach(self)) == NULL)
262 return ENOENT;
263
264 /* Destroy active table first. */
265 dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
266
267 /* Destroy inactive table if exits, too. */
268 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
269
270 dm_table_head_destroy(&dmv->table_head);
271
272 /* Destroy disk device structure */
273 disk_detach(dmv->diskp);
274 disk_destroy(dmv->diskp);
275
276 /* Destroy device */
277 (void)dm_dev_free(dmv);
278
279 /* Decrement device counter After removing device */
280 atomic_dec_64(&dm_dev_counter);
281
282 return 0;
283 }
284
285 static void
286 dm_doinit(void)
287 {
288 dm_target_init();
289 dm_dev_init();
290 dm_pdev_init();
291 }
292
293 /* attach routine */
294 void
295 dmattach(int n)
296 {
297 int error;
298
299 error = config_cfattach_attach(dm_cd.cd_name, &dm_ca);
300 if (error) {
301 aprint_error("%s: unable to register cfattach\n",
302 dm_cd.cd_name);
303 } else {
304 dm_doinit();
305 }
306 }
307
308 #ifdef _MODULE
309 /* Destroy routine */
310 static int
311 dmdestroy(void)
312 {
313 int error;
314
315 error = config_cfattach_detach(dm_cd.cd_name, &dm_ca);
316 if (error)
317 return error;
318
319 dm_dev_destroy();
320 dm_pdev_destroy();
321 dm_target_destroy();
322
323 return 0;
324 }
325 #endif /* _MODULE */
326
327 static int
328 dmopen(dev_t dev, int flags, int mode, struct lwp *l)
329 {
330
331 aprint_debug("dm open routine called %" PRIu32 "\n", minor(dev));
332 return 0;
333 }
334
335 static int
336 dmclose(dev_t dev, int flags, int mode, struct lwp *l)
337 {
338
339 aprint_debug("dm close routine called %" PRIu32 "\n", minor(dev));
340 return 0;
341 }
342
343
344 static int
345 dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
346 {
347 int r;
348 prop_dictionary_t dm_dict_in;
349
350 r = 0;
351
352 aprint_debug("dmioctl called\n");
353
354 KASSERT(data != NULL);
355
356 if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) {
357 struct plistref *pref = (struct plistref *) data;
358
359 /* Check if we were called with NETBSD_DM_IOCTL ioctl
360 otherwise quit. */
361 if ((r = dm_ioctl_switch(cmd)) != 0)
362 return r;
363
364 if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
365 return r;
366
367 if ((r = dm_check_version(dm_dict_in)) != 0)
368 goto cleanup_exit;
369
370 /* run ioctl routine */
371 if ((r = dm_cmd_to_fun(dm_dict_in)) != 0)
372 goto cleanup_exit;
373
374 cleanup_exit:
375 r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
376 prop_object_release(dm_dict_in);
377 }
378
379 return r;
380 }
381
382 /*
383 * Translate command sent from libdevmapper to func.
384 */
385 static int
386 dm_cmd_to_fun(prop_dictionary_t dm_dict){
387 int i, r;
388 prop_string_t command;
389
390 r = 0;
391
392 if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
393 return EINVAL;
394
395 for(i = 0; cmd_fn[i].cmd != NULL; i++)
396 if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
397 break;
398
399 if (cmd_fn[i].cmd == NULL)
400 return EINVAL;
401
402 aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
403 r = cmd_fn[i].fn(dm_dict);
404
405 return r;
406 }
407
408 /* Call apropriate ioctl handler function. */
409 static int
410 dm_ioctl_switch(u_long cmd)
411 {
412
413 switch(cmd) {
414
415 case NETBSD_DM_IOCTL:
416 aprint_debug("dm NetBSD_DM_IOCTL called\n");
417 break;
418 default:
419 aprint_debug("dm unknown ioctl called\n");
420 return ENOTTY;
421 break; /* NOT REACHED */
422 }
423
424 return 0;
425 }
426
427 /*
428 * Check for disk specific ioctls.
429 */
430
431 static int
432 disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
433 {
434 dm_dev_t *dmv;
435
436 /* disk ioctls make sense only on block devices */
437 if (minor(dev) == 0)
438 return ENOTTY;
439
440 switch(cmd) {
441 case DIOCGWEDGEINFO:
442 {
443 struct dkwedge_info *dkw = (void *) data;
444
445 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
446 return ENODEV;
447
448 aprint_debug("DIOCGWEDGEINFO ioctl called\n");
449
450 strlcpy(dkw->dkw_devname, dmv->name, 16);
451 strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
452 strlcpy(dkw->dkw_parent, dmv->name, 16);
453
454 dkw->dkw_offset = 0;
455 dkw->dkw_size = dm_table_size(&dmv->table_head);
456 strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
457
458 dm_dev_unbusy(dmv);
459 break;
460 }
461
462 case DIOCGDISKINFO:
463 {
464 struct plistref *pref = (struct plistref *) data;
465
466 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
467 return ENODEV;
468
469 if (dmv->diskp->dk_info == NULL) {
470 dm_dev_unbusy(dmv);
471 return ENOTSUP;
472 } else
473 prop_dictionary_copyout_ioctl(pref, cmd,
474 dmv->diskp->dk_info);
475
476 dm_dev_unbusy(dmv);
477 break;
478 }
479
480 case DIOCCACHESYNC:
481 {
482 dm_table_entry_t *table_en;
483 dm_table_t *tbl;
484 int err;
485
486 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
487 return ENODEV;
488
489 /* Select active table */
490 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
491
492 /*
493 * Call sync target routine for all table entries. Target sync
494 * routine basically call DIOCCACHESYNC on underlying devices.
495 */
496 SLIST_FOREACH(table_en, tbl, next)
497 {
498 err = table_en->target->sync(table_en);
499 }
500 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
501 dm_dev_unbusy(dmv);
502 break;
503 }
504
505
506 default:
507 aprint_debug("unknown disk_ioctl called\n");
508 return ENOTTY;
509 break; /* NOT REACHED */
510 }
511
512 return 0;
513 }
514
515 /*
516 * Do all IO operations on dm logical devices.
517 */
518 static void
519 dmstrategy(struct buf *bp)
520 {
521 dm_dev_t *dmv;
522 dm_table_t *tbl;
523 dm_table_entry_t *table_en;
524 struct buf *nestbuf;
525
526 uint32_t dev_type;
527
528 uint64_t buf_start, buf_len, issued_len;
529 uint64_t table_start, table_end;
530 uint64_t start, end;
531
532 buf_start = bp->b_blkno * DEV_BSIZE;
533 buf_len = bp->b_bcount;
534
535 tbl = NULL;
536
537 table_end = 0;
538 dev_type = 0;
539 issued_len = 0;
540
541 if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
542 bp->b_error = EIO;
543 bp->b_resid = bp->b_bcount;
544 biodone(bp);
545 return;
546 }
547
548 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
549 dm_table_size(&dmv->table_head)) <= 0) {
550 dm_dev_unbusy(dmv);
551 bp->b_resid = bp->b_bcount;
552 biodone(bp);
553 return;
554 }
555
556 /*
557 * disk(9) is part of device structure and it can't be used without
558 * mutual exclusion, use diskp_mtx until it will be fixed.
559 */
560 mutex_enter(&dmv->diskp_mtx);
561 disk_busy(dmv->diskp);
562 mutex_exit(&dmv->diskp_mtx);
563
564 /* Select active table */
565 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
566
567 /* Nested buffers count down to zero therefore I have
568 to set bp->b_resid to maximal value. */
569 bp->b_resid = bp->b_bcount;
570
571 /*
572 * Find out what tables I want to select.
573 */
574 SLIST_FOREACH(table_en, tbl, next)
575 {
576 /* I need need number of bytes not blocks. */
577 table_start = table_en->start * DEV_BSIZE;
578 /*
579 * I have to sub 1 from table_en->length to prevent
580 * off by one error
581 */
582 table_end = table_start + (table_en->length)* DEV_BSIZE;
583
584 start = MAX(table_start, buf_start);
585
586 end = MIN(table_end, buf_start + buf_len);
587
588 aprint_debug("----------------------------------------\n");
589 aprint_debug("table_start %010" PRIu64", table_end %010"
590 PRIu64 "\n", table_start, table_end);
591 aprint_debug("buf_start %010" PRIu64", buf_len %010"
592 PRIu64"\n", buf_start, buf_len);
593 aprint_debug("start-buf_start %010"PRIu64", end %010"
594 PRIu64"\n", start - buf_start, end);
595 aprint_debug("start %010" PRIu64" , end %010"
596 PRIu64"\n", start, end);
597 aprint_debug("\n----------------------------------------\n");
598
599 if (start < end) {
600 /* create nested buffer */
601 nestbuf = getiobuf(NULL, true);
602
603 nestiobuf_setup(bp, nestbuf, start - buf_start,
604 (end - start));
605
606 issued_len += end - start;
607
608 /* I need number of blocks. */
609 nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
610
611 table_en->target->strategy(table_en, nestbuf);
612 }
613 }
614
615 if (issued_len < buf_len)
616 nestiobuf_done(bp, buf_len - issued_len, EINVAL);
617
618 mutex_enter(&dmv->diskp_mtx);
619 disk_unbusy(dmv->diskp, buf_len, bp != NULL ? bp->b_flags & B_READ : 0);
620 mutex_exit(&dmv->diskp_mtx);
621
622 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
623 dm_dev_unbusy(dmv);
624
625 return;
626 }
627
628
629 static int
630 dmread(dev_t dev, struct uio *uio, int flag)
631 {
632
633 return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
634 }
635
636 static int
637 dmwrite(dev_t dev, struct uio *uio, int flag)
638 {
639
640 return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
641 }
642
643 static int
644 dmsize(dev_t dev)
645 {
646 dm_dev_t *dmv;
647 uint64_t size;
648
649 size = 0;
650
651 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
652 return -ENOENT;
653
654 size = dm_table_size(&dmv->table_head);
655 dm_dev_unbusy(dmv);
656
657 return size;
658 }
659
660 static void
661 dmminphys(struct buf *bp)
662 {
663
664 bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
665 }
666
667 void
668 dmgetproperties(struct disk *disk, dm_table_head_t *head)
669 {
670 prop_dictionary_t disk_info, odisk_info, geom;
671 int dmp_size;
672
673 dmp_size = dm_table_size(head);
674 disk_info = prop_dictionary_create();
675 geom = prop_dictionary_create();
676
677 prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI");
678 prop_dictionary_set_uint64(geom, "sectors-per-unit", dmp_size);
679 prop_dictionary_set_uint32(geom, "sector-size",
680 DEV_BSIZE /* XXX 512? */);
681 prop_dictionary_set_uint32(geom, "sectors-per-track", 32);
682 prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64);
683 prop_dictionary_set_uint32(geom, "cylinders-per-unit", dmp_size / 2048);
684 prop_dictionary_set(disk_info, "geometry", geom);
685 prop_object_release(geom);
686
687 odisk_info = disk->dk_info;
688 disk->dk_info = disk_info;
689
690 if (odisk_info != NULL)
691 prop_object_release(odisk_info);
692 }
693