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