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