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