device-mapper.c revision 1.26 1 /* $NetBSD: device-mapper.c,v 1.26 2010/12/06 09:12:23 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 uint32_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_32(&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 KASSERT(data != NULL);
354
355 if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) {
356 struct plistref *pref = (struct plistref *) data;
357
358 /* Check if we were called with NETBSD_DM_IOCTL ioctl
359 otherwise quit. */
360 if ((r = dm_ioctl_switch(cmd)) != 0)
361 return r;
362
363 if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
364 return r;
365
366 if ((r = dm_check_version(dm_dict_in)) != 0)
367 goto cleanup_exit;
368
369 /* run ioctl routine */
370 if ((r = dm_cmd_to_fun(dm_dict_in)) != 0)
371 goto cleanup_exit;
372
373 cleanup_exit:
374 r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
375 prop_object_release(dm_dict_in);
376 }
377
378 return r;
379 }
380
381 /*
382 * Translate command sent from libdevmapper to func.
383 */
384 static int
385 dm_cmd_to_fun(prop_dictionary_t dm_dict){
386 int i, r;
387 prop_string_t command;
388
389 r = 0;
390
391 if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
392 return EINVAL;
393
394 for(i = 0; cmd_fn[i].cmd != NULL; i++)
395 if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
396 break;
397
398 if (cmd_fn[i].cmd == NULL)
399 return EINVAL;
400
401 aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
402 r = cmd_fn[i].fn(dm_dict);
403
404 return r;
405 }
406
407 /* Call apropriate ioctl handler function. */
408 static int
409 dm_ioctl_switch(u_long cmd)
410 {
411
412 switch(cmd) {
413
414 case NETBSD_DM_IOCTL:
415 aprint_debug("dm NetBSD_DM_IOCTL called\n");
416 break;
417 default:
418 aprint_debug("dm unknown ioctl called\n");
419 return ENOTTY;
420 break; /* NOT REACHED */
421 }
422
423 return 0;
424 }
425
426 /*
427 * Check for disk specific ioctls.
428 */
429
430 static int
431 disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
432 {
433 dm_dev_t *dmv;
434
435 /* disk ioctls make sense only on block devices */
436 if (minor(dev) == 0)
437 return ENOTTY;
438
439 switch(cmd) {
440 case DIOCGWEDGEINFO:
441 {
442 struct dkwedge_info *dkw = (void *) data;
443
444 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
445 return ENODEV;
446
447 aprint_debug("DIOCGWEDGEINFO ioctl called\n");
448
449 strlcpy(dkw->dkw_devname, dmv->name, 16);
450 strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
451 strlcpy(dkw->dkw_parent, dmv->name, 16);
452
453 dkw->dkw_offset = 0;
454 dkw->dkw_size = dm_table_size(&dmv->table_head);
455 strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
456
457 dm_dev_unbusy(dmv);
458 break;
459 }
460
461 case DIOCGDISKINFO:
462 {
463 struct plistref *pref = (struct plistref *) data;
464
465 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
466 return ENODEV;
467
468 if (dmv->diskp->dk_info == NULL) {
469 dm_dev_unbusy(dmv);
470 return ENOTSUP;
471 } else
472 prop_dictionary_copyout_ioctl(pref, cmd,
473 dmv->diskp->dk_info);
474
475 dm_dev_unbusy(dmv);
476 break;
477 }
478
479 case DIOCCACHESYNC:
480 {
481 dm_table_entry_t *table_en;
482 dm_table_t *tbl;
483 int err;
484
485 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
486 return ENODEV;
487
488 /* Select active table */
489 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
490
491 /*
492 * Call sync target routine for all table entries. Target sync
493 * routine basically call DIOCCACHESYNC on underlying devices.
494 */
495 SLIST_FOREACH(table_en, tbl, next)
496 {
497 err = table_en->target->sync(table_en);
498 }
499 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
500 dm_dev_unbusy(dmv);
501 break;
502 }
503
504
505 default:
506 aprint_debug("unknown disk_ioctl called\n");
507 return ENOTTY;
508 break; /* NOT REACHED */
509 }
510
511 return 0;
512 }
513
514 /*
515 * Do all IO operations on dm logical devices.
516 */
517 static void
518 dmstrategy(struct buf *bp)
519 {
520 dm_dev_t *dmv;
521 dm_table_t *tbl;
522 dm_table_entry_t *table_en;
523 struct buf *nestbuf;
524
525 uint32_t dev_type;
526
527 uint64_t buf_start, buf_len, issued_len;
528 uint64_t table_start, table_end;
529 uint64_t start, end;
530
531 buf_start = bp->b_blkno * DEV_BSIZE;
532 buf_len = bp->b_bcount;
533
534 tbl = NULL;
535
536 table_end = 0;
537 dev_type = 0;
538 issued_len = 0;
539
540 if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
541 bp->b_error = EIO;
542 bp->b_resid = bp->b_bcount;
543 biodone(bp);
544 return;
545 }
546
547 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
548 dm_table_size(&dmv->table_head)) <= 0) {
549 dm_dev_unbusy(dmv);
550 bp->b_resid = bp->b_bcount;
551 biodone(bp);
552 return;
553 }
554
555 /*
556 * disk(9) is part of device structure and it can't be used without
557 * mutual exclusion, use diskp_mtx until it will be fixed.
558 */
559 mutex_enter(&dmv->diskp_mtx);
560 disk_busy(dmv->diskp);
561 mutex_exit(&dmv->diskp_mtx);
562
563 /* Select active table */
564 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
565
566 /* Nested buffers count down to zero therefore I have
567 to set bp->b_resid to maximal value. */
568 bp->b_resid = bp->b_bcount;
569
570 /*
571 * Find out what tables I want to select.
572 */
573 SLIST_FOREACH(table_en, tbl, next)
574 {
575 /* I need need number of bytes not blocks. */
576 table_start = table_en->start * DEV_BSIZE;
577 /*
578 * I have to sub 1 from table_en->length to prevent
579 * off by one error
580 */
581 table_end = table_start + (table_en->length)* DEV_BSIZE;
582
583 start = MAX(table_start, buf_start);
584
585 end = MIN(table_end, buf_start + buf_len);
586
587 aprint_debug("----------------------------------------\n");
588 aprint_debug("table_start %010" PRIu64", table_end %010"
589 PRIu64 "\n", table_start, table_end);
590 aprint_debug("buf_start %010" PRIu64", buf_len %010"
591 PRIu64"\n", buf_start, buf_len);
592 aprint_debug("start-buf_start %010"PRIu64", end %010"
593 PRIu64"\n", start - buf_start, end);
594 aprint_debug("start %010" PRIu64" , end %010"
595 PRIu64"\n", start, end);
596 aprint_debug("\n----------------------------------------\n");
597
598 if (start < end) {
599 /* create nested buffer */
600 nestbuf = getiobuf(NULL, true);
601
602 nestiobuf_setup(bp, nestbuf, start - buf_start,
603 (end - start));
604
605 issued_len += end - start;
606
607 /* I need number of blocks. */
608 nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
609
610 table_en->target->strategy(table_en, nestbuf);
611 }
612 }
613
614 if (issued_len < buf_len)
615 nestiobuf_done(bp, buf_len - issued_len, EINVAL);
616
617 mutex_enter(&dmv->diskp_mtx);
618 disk_unbusy(dmv->diskp, buf_len, bp != NULL ? bp->b_flags & B_READ : 0);
619 mutex_exit(&dmv->diskp_mtx);
620
621 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
622 dm_dev_unbusy(dmv);
623
624 return;
625 }
626
627
628 static int
629 dmread(dev_t dev, struct uio *uio, int flag)
630 {
631
632 return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
633 }
634
635 static int
636 dmwrite(dev_t dev, struct uio *uio, int flag)
637 {
638
639 return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
640 }
641
642 static int
643 dmsize(dev_t dev)
644 {
645 dm_dev_t *dmv;
646 uint64_t size;
647
648 size = 0;
649
650 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
651 return -ENOENT;
652
653 size = dm_table_size(&dmv->table_head);
654 dm_dev_unbusy(dmv);
655
656 return size;
657 }
658
659 static void
660 dmminphys(struct buf *bp)
661 {
662
663 bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
664 }
665
666 void
667 dmgetproperties(struct disk *disk, dm_table_head_t *head)
668 {
669 prop_dictionary_t disk_info, odisk_info, geom;
670 int dmp_size;
671
672 dmp_size = dm_table_size(head);
673 disk_info = prop_dictionary_create();
674 geom = prop_dictionary_create();
675
676 prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI");
677 prop_dictionary_set_uint64(geom, "sectors-per-unit", dmp_size);
678 prop_dictionary_set_uint32(geom, "sector-size",
679 DEV_BSIZE /* XXX 512? */);
680 prop_dictionary_set_uint32(geom, "sectors-per-track", 32);
681 prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64);
682 prop_dictionary_set_uint32(geom, "cylinders-per-unit", dmp_size / 2048);
683 prop_dictionary_set(disk_info, "geometry", geom);
684 prop_object_release(geom);
685
686 odisk_info = disk->dk_info;
687 disk->dk_info = disk_info;
688
689 if (odisk_info != NULL)
690 prop_object_release(odisk_info);
691 }
692