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