sys/dev/disk/dm: Remove NULL element for array termination
[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 array is used to translate cmd to function pointer.
105  *
106  * Interface between libdevmapper and lvm2tools uses different
107  * names for one IOCTL call because libdevmapper do another thing
108  * then. When I run "info" or "mknodes" libdevmapper will send same
109  * ioctl to kernel but will do another things in userspace.
110  *
111  */
112 static struct cmd_function cmd_fn[] = {
113                 { .cmd = "version", .fn = NULL},
114                 { .cmd = "targets", .fn = dm_list_versions_ioctl},
115                 { .cmd = "create",  .fn = dm_dev_create_ioctl},
116                 { .cmd = "info",    .fn = dm_dev_status_ioctl},
117                 { .cmd = "mknodes", .fn = dm_dev_status_ioctl},
118                 { .cmd = "names",   .fn = dm_dev_list_ioctl},
119                 { .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
120                 { .cmd = "remove",  .fn = dm_dev_remove_ioctl},
121                 { .cmd = "remove_all", .fn = dm_dev_remove_all_ioctl},
122                 { .cmd = "rename",  .fn = dm_dev_rename_ioctl},
123                 { .cmd = "resume",  .fn = dm_dev_resume_ioctl},
124                 { .cmd = "clear",   .fn = dm_table_clear_ioctl},
125                 { .cmd = "deps",    .fn = dm_table_deps_ioctl},
126                 { .cmd = "reload",  .fn = dm_table_load_ioctl},
127                 { .cmd = "status",  .fn = dm_table_status_ioctl},
128                 { .cmd = "table",   .fn = dm_table_status_ioctl},
129                 { .cmd = "message", .fn = dm_message_ioctl},
130 };
131
132 /* New module handle routine */
133 static int
134 dm_modcmd(module_t mod, int cmd, void *unused)
135 {
136         int error;
137
138         error = 0;
139
140         switch (cmd) {
141         case MOD_LOAD:
142                 dm_doinit();
143                 kprintf("Device Mapper version %d.%d.%d loaded\n",
144                     DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL);
145                 break;
146
147         case MOD_UNLOAD:
148                 /*
149                  * Disable unloading of dm module if there are any devices
150                  * defined in driver. This is probably too strong we need
151                  * to disable auto-unload only if there is mounted dm device
152                  * present.
153                  */
154                 if (dm_dev_counter > 0)
155                         return EBUSY;
156
157                 error = dmdestroy();
158                 if (error)
159                         break;
160                 kprintf("Device Mapper unloaded\n");
161                 break;
162
163         default:
164                 break;
165         }
166
167         return error;
168 }
169
170 static void
171 dm_doinit(void)
172 {
173         dm_target_init();
174         dm_dev_init();
175         dm_pdev_init();
176         dmcdev = make_dev(&dm_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control");
177 }
178
179 /* Destroy routine */
180 static int
181 dmdestroy(void)
182 {
183         destroy_dev(dmcdev);
184
185         dm_dev_uninit();
186         dm_pdev_uninit();
187         dm_target_uninit();
188
189         return 0;
190 }
191
192 static int
193 dmopen(struct dev_open_args *ap)
194 {
195         cdev_t dev = ap->a_head.a_dev;
196         dm_dev_t *dmv;
197
198         /* Shortcut for the control device */
199         if (minor(dev) == 0)
200                 return 0;
201
202         if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
203                 return ENXIO;
204
205         dmv->is_open = 1;
206         dm_dev_unbusy(dmv);
207
208         aprint_debug("dm open routine called %" PRIu32 "\n",
209             minor(ap->a_head.a_dev));
210         return 0;
211 }
212
213 static int
214 dmclose(struct dev_close_args *ap)
215 {
216         cdev_t dev = ap->a_head.a_dev;
217         dm_dev_t *dmv;
218
219         /* Shortcut for the control device */
220         if (minor(dev) == 0)
221                 return 0;
222
223         if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
224                 return ENXIO;
225
226         dmv->is_open = 0;
227         dm_dev_unbusy(dmv);
228
229         aprint_debug("dm close routine called %" PRIu32 "\n",
230             minor(ap->a_head.a_dev));
231         return 0;
232 }
233
234
235 static int
236 dmioctl(struct dev_ioctl_args *ap)
237 {
238         cdev_t dev = ap->a_head.a_dev;
239         u_long cmd = ap->a_cmd;
240         void *data = ap->a_data;
241         struct plistref *pref = (struct plistref *)data;
242
243         int r, err;
244         prop_dictionary_t dm_dict_in;
245
246         err = r = 0;
247
248         aprint_debug("dmioctl called\n");
249         KKASSERT(data != NULL);
250
251         if ((r = disk_ioctl_switch(dev, cmd, data)) != ENOTTY)
252                 return r;  /* Handled disk ioctl */
253
254         if ((r = dm_ioctl_switch(cmd)) != 0)
255                 return r;  /* Not NETBSD_DM_IOCTL */
256
257         if ((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
258                 return r;
259
260         if ((r = dm_check_version(dm_dict_in)) == 0)
261                 err = dm_cmd_to_fun(dm_dict_in);
262
263         r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
264         prop_object_release(dm_dict_in);
265
266         /* Return the dm ioctl error if any. */
267         if (err)
268                 return err;
269         return r;
270 }
271
272 /*
273  * Translate command sent from libdevmapper to func.
274  */
275 static int
276 dm_cmd_to_fun(prop_dictionary_t dm_dict)
277 {
278         int i, size;
279         prop_string_t command;
280         struct cmd_function *p = NULL;
281
282         size = sizeof(cmd_fn) / sizeof(cmd_fn[0]);
283
284         if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
285                 return EINVAL;
286
287         for (i = 0; i < size; i++) {
288                 p = &cmd_fn[i];
289                 if (prop_string_equals_cstring(command, p->cmd))
290                         break;
291         }
292
293         KKASSERT(p);
294         if (i == size) {
295                 aprint_debug("Unknown ioctl\n");
296                 return EINVAL;
297         }
298
299         aprint_debug("ioctl %s called %p\n", p->cmd, p->fn);
300         if (p->fn == NULL)
301                 return 0;  /* No handler required */
302
303         return p->fn(dm_dict);
304 }
305
306 /* Call apropriate ioctl handler function. */
307 static int
308 dm_ioctl_switch(u_long cmd)
309 {
310
311         switch(cmd) {
312         case NETBSD_DM_IOCTL:
313                 aprint_debug("dm NETBSD_DM_IOCTL called\n");
314                 break;
315         default:
316                 aprint_debug("dm unknown ioctl called\n");
317                 return ENOTTY;
318                 break; /* NOT REACHED */
319         }
320
321         return 0;
322 }
323
324  /*
325   * Check for disk specific ioctls.
326   */
327
328 static int
329 disk_ioctl_switch(cdev_t dev, u_long cmd, void *data)
330 {
331         dm_dev_t *dmv;
332
333         /* disk ioctls make sense only on block devices */
334         if (minor(dev) == 0)
335                 return ENOTTY;
336
337         switch(cmd) {
338         case DIOCGPART:
339                 if ((dmv = dev->si_drv1) == NULL)
340                         return ENODEV;
341                 if (dmv->diskp->d_info.d_media_blksize == 0) {
342                         return ENOTSUP;
343                 } else {
344                         u_int64_t size;
345                         struct partinfo *dpart = data;
346                         bzero(dpart, sizeof(*dpart));
347
348                         size = dm_table_size(&dmv->table_head);
349                         dpart->media_offset  = 0;
350                         dpart->media_size    = size * DEV_BSIZE;
351                         dpart->media_blocks  = size;
352                         dpart->media_blksize = DEV_BSIZE;
353                         dpart->fstype = FS_BSDFFS;
354                 }
355                 break;
356
357         default:
358                 aprint_debug("unknown disk_ioctl called\n");
359                 return ENOTTY;
360                 break; /* NOT REACHED */
361         }
362
363         return 0;
364 }
365
366 /*
367  * Do all IO operations on dm logical devices.
368  */
369 static int
370 dmstrategy(struct dev_strategy_args *ap)
371 {
372         cdev_t dev = ap->a_head.a_dev;
373         struct bio *bio = ap->a_bio;
374         struct buf *bp = bio->bio_buf;
375         int bypass;
376
377         dm_dev_t *dmv;
378         dm_table_t  *tbl;
379         dm_table_entry_t *table_en;
380         struct buf *nestbuf;
381
382         uint64_t buf_start, buf_len, issued_len;
383         uint64_t table_start, table_end;
384         uint64_t start, end;
385
386         buf_start = bio->bio_offset;
387         buf_len = bp->b_bcount;
388
389         tbl = NULL;
390
391         table_end = 0;
392         issued_len = 0;
393
394         dmv = dev->si_drv1;
395
396         switch(bp->b_cmd) {
397         case BUF_CMD_READ:
398         case BUF_CMD_WRITE:
399         case BUF_CMD_FREEBLKS:
400                 bypass = 0;
401                 break;
402         case BUF_CMD_FLUSH:
403                 bypass = 1;
404                 KKASSERT(buf_len == 0);
405                 break;
406         default:
407                 bp->b_error = EIO;
408                 bp->b_resid = bp->b_bcount;
409                 biodone(bio);
410                 return 0;
411         }
412
413         if (bypass == 0 &&
414             bounds_check_with_mediasize(bio, DEV_BSIZE,
415                                         dm_table_size(&dmv->table_head)) <= 0) {
416                 bp->b_resid = bp->b_bcount;
417                 biodone(bio);
418                 return 0;
419         }
420
421         /* Select active table */
422         tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
423
424         nestiobuf_init(bio);
425         devstat_start_transaction(&dmv->stats);
426
427         /*
428          * Find out what tables I want to select.
429          */
430         SLIST_FOREACH(table_en, tbl, next) {
431                 /*
432                  * I need need number of bytes not blocks.
433                  */
434                 table_start = table_en->start * DEV_BSIZE;
435                 table_end = table_start + (table_en->length) * DEV_BSIZE;
436
437                 /*
438                  * Calculate the start and end
439                  */
440                 start = MAX(table_start, buf_start);
441                 end = MIN(table_end, buf_start + buf_len);
442
443                 if (dm_debug_level) {
444                         aprint_normal("----------------------------------------\n");
445                         aprint_normal("table_start %010" PRIu64", table_end %010"
446                             PRIu64 "\n", table_start, table_end);
447                         aprint_normal("buf_start %010" PRIu64", buf_len %010"
448                             PRIu64"\n", buf_start, buf_len);
449                         aprint_normal("start-buf_start %010"PRIu64", end %010"
450                             PRIu64"\n", start - buf_start, end);
451                         aprint_normal("start %010" PRIu64", end %010"
452                             PRIu64"\n", start, end);
453                         aprint_normal("----------------------------------------\n");
454                 }
455
456                 if (bypass) {
457                         nestbuf = getpbuf(NULL);
458                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
459
460                         nestiobuf_add(bio, nestbuf, 0, 0, &dmv->stats);
461                         nestbuf->b_bio1.bio_offset = 0;
462                         table_en->target->strategy(table_en, nestbuf);
463                 } else if (start < end) {
464                         nestbuf = getpbuf(NULL);
465                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
466
467                         nestiobuf_add(bio, nestbuf,
468                                       start - buf_start, (end - start),
469                                       &dmv->stats);
470                         issued_len += end - start;
471
472                         nestbuf->b_bio1.bio_offset = (start - table_start);
473                         table_en->target->strategy(table_en, nestbuf);
474                 }
475         }
476
477         if (issued_len < buf_len)
478                 nestiobuf_error(bio, EINVAL);
479         nestiobuf_start(bio);
480         dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
481
482         return 0;
483 }
484
485 static int
486 dmdump(struct dev_dump_args *ap)
487 {
488         cdev_t dev = ap->a_head.a_dev;
489         dm_dev_t *dmv;
490         dm_table_t  *tbl;
491         dm_table_entry_t *table_en;
492         uint64_t buf_start, buf_len, issued_len;
493         uint64_t table_start, table_end;
494         uint64_t start, end, data_offset;
495         off_t offset;
496         size_t length;
497         int error = 0;
498
499         buf_start = ap->a_offset;
500         buf_len = ap->a_length;
501
502         tbl = NULL;
503
504         table_end = 0;
505         issued_len = 0;
506
507         dmv = dev->si_drv1;
508
509         /* Select active table */
510         tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
511
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                  */
520                 table_start = table_en->start * DEV_BSIZE;
521                 table_end = table_start + (table_en->length) * DEV_BSIZE;
522
523                 /*
524                  * Calculate the start and end
525                  */
526                 start = MAX(table_start, buf_start);
527                 end = MIN(table_end, buf_start + buf_len);
528
529                 if (ap->a_length == 0) {
530                         if (table_en->target->dump == NULL) {
531                                 error = ENXIO;
532                                 goto out;
533                         }
534
535                         table_en->target->dump(table_en, NULL, 0, 0);
536                 } else if (start < end) {
537                         data_offset = start - buf_start;
538                         offset = start - table_start;
539                         length = end - start;
540
541                         if (table_en->target->dump == NULL) {
542                                 error = ENXIO;
543                                 goto out;
544                         }
545
546                         table_en->target->dump(table_en,
547                             (char *)ap->a_virtual + data_offset,
548                             length, offset);
549
550                         issued_len += end - start;
551                 }
552         }
553
554         if (issued_len < buf_len)
555                 error = EINVAL;
556
557 out:
558         dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
559
560         return error;
561 }
562
563 static int
564 dmsize(struct dev_psize_args *ap)
565 {
566         cdev_t dev = ap->a_head.a_dev;
567         dm_dev_t *dmv;
568         uint64_t size;
569
570         size = 0;
571
572         if ((dmv = dev->si_drv1) == NULL)
573                 return ENXIO;
574
575         size = dm_table_size(&dmv->table_head);
576         ap->a_result = (int64_t)size;
577
578         return 0;
579 }
580
581 #if 0
582 static void
583 dmminphys(struct buf *bp)
584 {
585
586         bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
587 }
588 #endif
589
590 void
591 dmsetdiskinfo(struct disk *disk, dm_table_head_t *head)
592 {
593         struct disk_info info;
594         uint64_t dmp_size;
595
596         dmp_size = dm_table_size(head);
597
598         bzero(&info, sizeof(struct disk_info));
599         info.d_media_blksize = DEV_BSIZE;
600         info.d_media_blocks = dmp_size;
601 #if 0
602         /* this is set by disk_setdiskinfo */
603         info.d_media_size = dmp_size * DEV_BSIZE;
604 #endif
605         info.d_dsflags = DSO_MBRQUIET | DSO_DEVICEMAPPER | DSO_RAWPSIZE;
606
607         info.d_secpertrack = 32;
608         info.d_nheads = 64;
609         info.d_secpercyl = info.d_secpertrack * info.d_nheads;
610         info.d_ncylinders = dmp_size / info.d_secpercyl;
611
612         disk_setdiskinfo(disk, &info);
613 }
614
615 /*
616  * Transform char s to uint64_t offset number.
617  */
618 uint64_t
619 atoi64(const char *s)
620 {
621         uint64_t n;
622         n = 0;
623
624         while (*s != '\0') {
625                 if (!isdigit(*s))
626                         break;
627
628                 n = (10 * n) + (*s - '0');
629                 s++;
630         }
631
632         return n;
633 }
634
635 void
636 dm_builtin_init(void *arg)
637 {
638         modeventhand_t evh = (modeventhand_t)arg;
639
640         KKASSERT(evh != NULL);
641         evh(NULL, MOD_LOAD, NULL);
642 }
643
644 void
645 dm_builtin_uninit(void *arg)
646 {
647         modeventhand_t evh = (modeventhand_t)arg;
648
649         KKASSERT(evh != NULL);
650         evh(NULL, MOD_UNLOAD, NULL);
651 }
652
653 TUNABLE_INT("debug.dm_debug", &dm_debug_level);
654 SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level,
655                0, "Enable device mapper debugging");
656