sys/dev/disk/dm: Remove aprint_normal()
[dragonfly.git] / sys / dev / disk / dm / device-mapper.c
1 /*        $NetBSD: device-mapper.c,v 1.22 2010/03/26 15:46:04 jakllsch Exp $ */
2
3 /*
4  * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5  * Copyright (c) 2010 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Adam Hamsik.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /*
34  * I want to say thank you to all people who helped me with this project.
35  */
36
37 #include <sys/types.h>
38 #include <sys/ctype.h>
39
40 #include <sys/buf.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43 #include <sys/disk.h>
44 #include <sys/disklabel.h>
45 #include <sys/dtype.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/sysctl.h>
49 #include <dev/disk/dm/dm.h>
50
51 #include "netbsd-dm.h"
52
53 static  d_ioctl_t       dmioctl;
54 static  d_open_t        dmopen;
55 static  d_close_t       dmclose;
56 static  d_psize_t       dmsize;
57 static  d_strategy_t    dmstrategy;
58 static  d_dump_t        dmdump;
59
60 /* New module handle and destroy routines */
61 static int dm_modcmd(module_t mod, int cmd, void *unused);
62 static int dmdestroy(void);
63
64 static void dm_doinit(void);
65
66 static int dm_cmd_to_fun(prop_dictionary_t);
67 static int disk_ioctl_switch(cdev_t, u_long, void *);
68 static int dm_ioctl_switch(u_long);
69 #if 0
70 static void dmminphys(struct buf *);
71 #endif
72
73 /* ***Variable-definitions*** */
74 struct dev_ops dm_ops = {
75         { "dm", 0, D_DISK | D_MPSAFE },
76         .d_open         = dmopen,
77         .d_close        = dmclose,
78         .d_read         = physread,
79         .d_write        = physwrite,
80         .d_ioctl        = dmioctl,
81         .d_strategy     = dmstrategy,
82         .d_psize        = dmsize,
83         .d_dump         = dmdump,
84 /* D_DISK */
85 };
86
87 MALLOC_DEFINE(M_DM, "dm", "Device Mapper allocations");
88
89 int dm_debug_level = 0;
90
91 extern uint64_t dm_dev_counter;
92
93 static cdev_t dmcdev;
94
95 static moduledata_t dm_mod = {
96     "dm",
97     dm_modcmd,
98     NULL
99 };
100 DECLARE_MODULE(dm, dm_mod, SI_SUB_RAID, SI_ORDER_ANY);
101 MODULE_VERSION(dm, 1);
102
103 /*
104  * This structure is used to translate command sent to kernel driver in
105  * <key>command</key>
106  * <value></value>
107  * to function
108  */
109 /*
110  * This array is used to translate cmd to function pointer.
111  *
112  * Interface between libdevmapper and lvm2tools uses different
113  * names for one IOCTL call because libdevmapper do another thing
114  * then. When I run "info" or "mknodes" libdevmapper will send same
115  * ioctl to kernel but will do another things in userspace.
116  */
117 static struct cmd_function {
118         const char *cmd;
119         int (*fn)(prop_dictionary_t);
120 } cmd_fn[] = {
121         {.cmd = "version",    .fn = NULL},
122         {.cmd = "targets",    .fn = dm_list_versions_ioctl},
123         {.cmd = "create",     .fn = dm_dev_create_ioctl},
124         {.cmd = "info",       .fn = dm_dev_status_ioctl},
125         {.cmd = "mknodes",    .fn = dm_dev_status_ioctl},
126         {.cmd = "names",      .fn = dm_dev_list_ioctl},
127         {.cmd = "suspend",    .fn = dm_dev_suspend_ioctl},
128         {.cmd = "remove",     .fn = dm_dev_remove_ioctl},
129         {.cmd = "remove_all", .fn = dm_dev_remove_all_ioctl},
130         {.cmd = "rename",     .fn = dm_dev_rename_ioctl},
131         {.cmd = "resume",     .fn = dm_dev_resume_ioctl},
132         {.cmd = "clear",      .fn = dm_table_clear_ioctl},
133         {.cmd = "deps",       .fn = dm_table_deps_ioctl},
134         {.cmd = "reload",     .fn = dm_table_load_ioctl},
135         {.cmd = "status",     .fn = dm_table_status_ioctl},
136         {.cmd = "table",      .fn = dm_table_status_ioctl},
137         {.cmd = "message",    .fn = dm_message_ioctl},
138 };
139
140 /* New module handle routine */
141 static int
142 dm_modcmd(module_t mod, int cmd, void *unused)
143 {
144         int error;
145
146         error = 0;
147
148         switch (cmd) {
149         case MOD_LOAD:
150                 dm_doinit();
151                 kprintf("Device Mapper version %d.%d.%d loaded\n",
152                     DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL);
153                 break;
154
155         case MOD_UNLOAD:
156                 /*
157                  * Disable unloading of dm module if there are any devices
158                  * defined in driver. This is probably too strong we need
159                  * to disable auto-unload only if there is mounted dm device
160                  * present.
161                  */
162                 if (dm_dev_counter > 0)
163                         return EBUSY;
164
165                 error = dmdestroy();
166                 if (error)
167                         break;
168                 kprintf("Device Mapper unloaded\n");
169                 break;
170
171         default:
172                 break;
173         }
174
175         return error;
176 }
177
178 static void
179 dm_doinit(void)
180 {
181         dm_target_init();
182         dm_dev_init();
183         dm_pdev_init();
184         dmcdev = make_dev(&dm_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control");
185 }
186
187 /* Destroy routine */
188 static int
189 dmdestroy(void)
190 {
191         destroy_dev(dmcdev);
192
193         dm_dev_uninit();
194         dm_pdev_uninit();
195         dm_target_uninit();
196
197         return 0;
198 }
199
200 static int
201 dmopen(struct dev_open_args *ap)
202 {
203         cdev_t dev = ap->a_head.a_dev;
204         dm_dev_t *dmv;
205
206         /* Shortcut for the control device */
207         if (minor(dev) == 0)
208                 return 0;
209
210         if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
211                 return ENXIO;
212
213         dmv->is_open = 1;
214         dm_dev_unbusy(dmv);
215
216         aprint_debug("dm open routine called %" PRIu32 "\n",
217             minor(ap->a_head.a_dev));
218         return 0;
219 }
220
221 static int
222 dmclose(struct dev_close_args *ap)
223 {
224         cdev_t dev = ap->a_head.a_dev;
225         dm_dev_t *dmv;
226
227         /* Shortcut for the control device */
228         if (minor(dev) == 0)
229                 return 0;
230
231         if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
232                 return ENXIO;
233
234         dmv->is_open = 0;
235         dm_dev_unbusy(dmv);
236
237         aprint_debug("dm close routine called %" PRIu32 "\n",
238             minor(ap->a_head.a_dev));
239         return 0;
240 }
241
242
243 static int
244 dmioctl(struct dev_ioctl_args *ap)
245 {
246         cdev_t dev = ap->a_head.a_dev;
247         u_long cmd = ap->a_cmd;
248         void *data = ap->a_data;
249         struct plistref *pref = (struct plistref *)data;
250
251         int r, err;
252         prop_dictionary_t dm_dict_in;
253
254         err = r = 0;
255
256         aprint_debug("dmioctl called\n");
257         KKASSERT(data != NULL);
258
259         if ((r = disk_ioctl_switch(dev, cmd, data)) != ENOTTY)
260                 return r;  /* Handled disk ioctl */
261
262         if ((r = dm_ioctl_switch(cmd)) != 0)
263                 return r;  /* Not NETBSD_DM_IOCTL */
264
265         if ((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
266                 return r;
267
268         if ((r = dm_check_version(dm_dict_in)) == 0)
269                 err = dm_cmd_to_fun(dm_dict_in);
270
271         r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
272         prop_object_release(dm_dict_in);
273
274         /* Return the dm ioctl error if any. */
275         if (err)
276                 return err;
277         return r;
278 }
279
280 /*
281  * Translate command sent from libdevmapper to func.
282  */
283 static int
284 dm_cmd_to_fun(prop_dictionary_t dm_dict)
285 {
286         int i, size;
287         prop_string_t command;
288         struct cmd_function *p = NULL;
289
290         size = sizeof(cmd_fn) / sizeof(cmd_fn[0]);
291
292         if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
293                 return EINVAL;
294
295         for (i = 0; i < size; i++) {
296                 p = &cmd_fn[i];
297                 if (prop_string_equals_cstring(command, p->cmd))
298                         break;
299         }
300
301         KKASSERT(p);
302         if (i == size) {
303                 aprint_debug("Unknown ioctl\n");
304                 return EINVAL;
305         }
306
307         aprint_debug("ioctl %s called %p\n", p->cmd, p->fn);
308         if (p->fn == NULL)
309                 return 0;  /* No handler required */
310
311         return p->fn(dm_dict);
312 }
313
314 /* Call apropriate ioctl handler function. */
315 static int
316 dm_ioctl_switch(u_long cmd)
317 {
318
319         switch(cmd) {
320         case NETBSD_DM_IOCTL:
321                 aprint_debug("dm NETBSD_DM_IOCTL called\n");
322                 break;
323         default:
324                 aprint_debug("dm unknown ioctl called\n");
325                 return ENOTTY;
326                 break; /* NOT REACHED */
327         }
328
329         return 0;
330 }
331
332  /*
333   * Check for disk specific ioctls.
334   */
335
336 static int
337 disk_ioctl_switch(cdev_t dev, u_long cmd, void *data)
338 {
339         dm_dev_t *dmv;
340
341         /* disk ioctls make sense only on block devices */
342         if (minor(dev) == 0)
343                 return ENOTTY;
344
345         switch(cmd) {
346         case DIOCGPART:
347                 if ((dmv = dev->si_drv1) == NULL)
348                         return ENODEV;
349                 if (dmv->diskp->d_info.d_media_blksize == 0) {
350                         return ENOTSUP;
351                 } else {
352                         u_int64_t size;
353                         struct partinfo *dpart = data;
354                         bzero(dpart, sizeof(*dpart));
355
356                         size = dm_table_size(&dmv->table_head);
357                         dpart->media_offset  = 0;
358                         dpart->media_size    = size * DEV_BSIZE;
359                         dpart->media_blocks  = size;
360                         dpart->media_blksize = DEV_BSIZE;
361                         dpart->fstype = FS_BSDFFS;
362                 }
363                 break;
364
365         default:
366                 aprint_debug("unknown disk_ioctl called\n");
367                 return ENOTTY;
368                 break; /* NOT REACHED */
369         }
370
371         return 0;
372 }
373
374 /*
375  * Do all IO operations on dm logical devices.
376  */
377 static int
378 dmstrategy(struct dev_strategy_args *ap)
379 {
380         cdev_t dev = ap->a_head.a_dev;
381         struct bio *bio = ap->a_bio;
382         struct buf *bp = bio->bio_buf;
383         int bypass;
384
385         dm_dev_t *dmv;
386         dm_table_t  *tbl;
387         dm_table_entry_t *table_en;
388         struct buf *nestbuf;
389
390         uint64_t buf_start, buf_len, issued_len;
391         uint64_t table_start, table_end;
392         uint64_t start, end;
393
394         buf_start = bio->bio_offset;
395         buf_len = bp->b_bcount;
396
397         tbl = NULL;
398
399         table_end = 0;
400         issued_len = 0;
401
402         dmv = dev->si_drv1;
403
404         switch(bp->b_cmd) {
405         case BUF_CMD_READ:
406         case BUF_CMD_WRITE:
407         case BUF_CMD_FREEBLKS:
408                 bypass = 0;
409                 break;
410         case BUF_CMD_FLUSH:
411                 bypass = 1;
412                 KKASSERT(buf_len == 0);
413                 break;
414         default:
415                 bp->b_error = EIO;
416                 bp->b_resid = bp->b_bcount;
417                 biodone(bio);
418                 return 0;
419         }
420
421         if (bypass == 0 &&
422             bounds_check_with_mediasize(bio, DEV_BSIZE,
423                                         dm_table_size(&dmv->table_head)) <= 0) {
424                 bp->b_resid = bp->b_bcount;
425                 biodone(bio);
426                 return 0;
427         }
428
429         /* Select active table */
430         tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
431
432         nestiobuf_init(bio);
433         devstat_start_transaction(&dmv->stats);
434
435         /*
436          * Find out what tables I want to select.
437          */
438         SLIST_FOREACH(table_en, tbl, next) {
439                 /*
440                  * I need need number of bytes not blocks.
441                  */
442                 table_start = table_en->start * DEV_BSIZE;
443                 table_end = table_start + (table_en->length) * DEV_BSIZE;
444
445                 /*
446                  * Calculate the start and end
447                  */
448                 start = MAX(table_start, buf_start);
449                 end = MIN(table_end, buf_start + buf_len);
450
451                 if (dm_debug_level) {
452                         kprintf("----------------------------------------\n");
453                         kprintf("table_start %010" PRIu64", table_end %010"
454                             PRIu64 "\n", table_start, table_end);
455                         kprintf("buf_start %010" PRIu64", buf_len %010"
456                             PRIu64"\n", buf_start, buf_len);
457                         kprintf("start-buf_start %010"PRIu64", end %010"
458                             PRIu64"\n", start - buf_start, end);
459                         kprintf("start %010" PRIu64", end %010"
460                             PRIu64"\n", start, end);
461                         kprintf("----------------------------------------\n");
462                 }
463
464                 if (bypass) {
465                         nestbuf = getpbuf(NULL);
466                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
467
468                         nestiobuf_add(bio, nestbuf, 0, 0, &dmv->stats);
469                         nestbuf->b_bio1.bio_offset = 0;
470                         table_en->target->strategy(table_en, nestbuf);
471                 } else if (start < end) {
472                         nestbuf = getpbuf(NULL);
473                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
474
475                         nestiobuf_add(bio, nestbuf,
476                                       start - buf_start, (end - start),
477                                       &dmv->stats);
478                         issued_len += end - start;
479
480                         nestbuf->b_bio1.bio_offset = (start - table_start);
481                         table_en->target->strategy(table_en, nestbuf);
482                 }
483         }
484
485         if (issued_len < buf_len)
486                 nestiobuf_error(bio, EINVAL);
487         nestiobuf_start(bio);
488         dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
489
490         return 0;
491 }
492
493 static int
494 dmdump(struct dev_dump_args *ap)
495 {
496         cdev_t dev = ap->a_head.a_dev;
497         dm_dev_t *dmv;
498         dm_table_t  *tbl;
499         dm_table_entry_t *table_en;
500         uint64_t buf_start, buf_len, issued_len;
501         uint64_t table_start, table_end;
502         uint64_t start, end, data_offset;
503         off_t offset;
504         size_t length;
505         int error = 0;
506
507         buf_start = ap->a_offset;
508         buf_len = ap->a_length;
509
510         tbl = NULL;
511
512         table_end = 0;
513         issued_len = 0;
514
515         dmv = dev->si_drv1;
516
517         /* Select active table */
518         tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
519
520
521         /*
522          * Find out what tables I want to select.
523          */
524         SLIST_FOREACH(table_en, tbl, next) {
525                 /*
526                  * I need need number of bytes not blocks.
527                  */
528                 table_start = table_en->start * DEV_BSIZE;
529                 table_end = table_start + (table_en->length) * DEV_BSIZE;
530
531                 /*
532                  * Calculate the start and end
533                  */
534                 start = MAX(table_start, buf_start);
535                 end = MIN(table_end, buf_start + buf_len);
536
537                 if (ap->a_length == 0) {
538                         if (table_en->target->dump == NULL) {
539                                 error = ENXIO;
540                                 goto out;
541                         }
542
543                         table_en->target->dump(table_en, NULL, 0, 0);
544                 } else if (start < end) {
545                         data_offset = start - buf_start;
546                         offset = start - table_start;
547                         length = end - start;
548
549                         if (table_en->target->dump == NULL) {
550                                 error = ENXIO;
551                                 goto out;
552                         }
553
554                         table_en->target->dump(table_en,
555                             (char *)ap->a_virtual + data_offset,
556                             length, offset);
557
558                         issued_len += end - start;
559                 }
560         }
561
562         if (issued_len < buf_len)
563                 error = EINVAL;
564
565 out:
566         dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
567
568         return error;
569 }
570
571 static int
572 dmsize(struct dev_psize_args *ap)
573 {
574         cdev_t dev = ap->a_head.a_dev;
575         dm_dev_t *dmv;
576         uint64_t size;
577
578         size = 0;
579
580         if ((dmv = dev->si_drv1) == NULL)
581                 return ENXIO;
582
583         size = dm_table_size(&dmv->table_head);
584         ap->a_result = (int64_t)size;
585
586         return 0;
587 }
588
589 #if 0
590 static void
591 dmminphys(struct buf *bp)
592 {
593
594         bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
595 }
596 #endif
597
598 void
599 dmsetdiskinfo(struct disk *disk, dm_table_head_t *head)
600 {
601         struct disk_info info;
602         uint64_t dmp_size;
603
604         dmp_size = dm_table_size(head);
605
606         bzero(&info, sizeof(struct disk_info));
607         info.d_media_blksize = DEV_BSIZE;
608         info.d_media_blocks = dmp_size;
609 #if 0
610         /* this is set by disk_setdiskinfo */
611         info.d_media_size = dmp_size * DEV_BSIZE;
612 #endif
613         info.d_dsflags = DSO_MBRQUIET | DSO_DEVICEMAPPER | DSO_RAWPSIZE;
614
615         info.d_secpertrack = 32;
616         info.d_nheads = 64;
617         info.d_secpercyl = info.d_secpertrack * info.d_nheads;
618         info.d_ncylinders = dmp_size / info.d_secpercyl;
619
620         disk_setdiskinfo(disk, &info);
621 }
622
623 /*
624  * Transform char s to uint64_t offset number.
625  */
626 uint64_t
627 atoi64(const char *s)
628 {
629         uint64_t n;
630         n = 0;
631
632         while (*s != '\0') {
633                 if (!isdigit(*s))
634                         break;
635
636                 n = (10 * n) + (*s - '0');
637                 s++;
638         }
639
640         return n;
641 }
642
643 void
644 dm_builtin_init(void *arg)
645 {
646         modeventhand_t evh = (modeventhand_t)arg;
647
648         KKASSERT(evh != NULL);
649         evh(NULL, MOD_LOAD, NULL);
650 }
651
652 void
653 dm_builtin_uninit(void *arg)
654 {
655         modeventhand_t evh = (modeventhand_t)arg;
656
657         KKASSERT(evh != NULL);
658         evh(NULL, MOD_UNLOAD, NULL);
659 }
660
661 TUNABLE_INT("debug.dm_debug", &dm_debug_level);
662 SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level,
663                0, "Enable device mapper debugging");
664