dm - properly encapsulate disable_dev
[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 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/devfs.h>
43 #include <sys/disk.h>
44 #include <sys/disklabel.h>
45 #include <sys/dtype.h>
46 #include <sys/ioccom.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/sysctl.h>
50
51 #include "netbsd-dm.h"
52 #include "dm.h"
53
54 static  d_ioctl_t       dmioctl;
55 static  d_open_t        dmopen;
56 static  d_close_t       dmclose;
57 static  d_psize_t       dmsize;
58 static  d_strategy_t    dmstrategy;
59 static  d_dump_t        dmdump;
60
61 /* attach and detach routines */
62 void dmattach(int);
63 static int dm_modcmd(module_t mod, int cmd, void *unused);
64 static int dmdestroy(void);
65
66 static void dm_doinit(void);
67
68 static int dm_cmd_to_fun(prop_dictionary_t);
69 static int disk_ioctl_switch(cdev_t, u_long, void *);
70 static int dm_ioctl_switch(u_long);
71 #if 0
72 static void dmminphys(struct buf *);
73 #endif
74
75 struct devfs_bitmap dm_minor_bitmap;
76
77 /* ***Variable-definitions*** */
78 struct dev_ops dm_ops = {
79         { "dm", 0, D_DISK | D_MPSAFE },
80         .d_open         = dmopen,
81         .d_close        = dmclose,
82         .d_read         = physread,
83         .d_write        = physwrite,
84         .d_ioctl        = dmioctl,
85         .d_strategy     = dmstrategy,
86         .d_psize        = dmsize,
87         .d_dump         = dmdump,
88 /* D_DISK */
89 };
90
91 MALLOC_DEFINE(M_DM, "dm", "Device Mapper allocations");
92
93 int dm_debug_level = 0;
94
95 extern uint64_t dm_dev_counter;
96
97 static cdev_t dmcdev;
98
99 static moduledata_t dm_mod = {
100     "dm",
101     dm_modcmd,
102     NULL
103 };
104 DECLARE_MODULE(dm, dm_mod, SI_SUB_RAID, SI_ORDER_ANY);
105
106 /*
107  * This array is used to translate cmd to function pointer.
108  *
109  * Interface between libdevmapper and lvm2tools uses different
110  * names for one IOCTL call because libdevmapper do another thing
111  * then. When I run "info" or "mknodes" libdevmapper will send same
112  * ioctl to kernel but will do another things in userspace.
113  *
114  */
115 static struct cmd_function cmd_fn[] = {
116                 { .cmd = "version", .fn = dm_get_version_ioctl},
117                 { .cmd = "targets", .fn = dm_list_versions_ioctl},
118                 { .cmd = "create",  .fn = dm_dev_create_ioctl},
119                 { .cmd = "info",    .fn = dm_dev_status_ioctl},
120                 { .cmd = "mknodes", .fn = dm_dev_status_ioctl},
121                 { .cmd = "names",   .fn = dm_dev_list_ioctl},
122                 { .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
123                 { .cmd = "remove",  .fn = dm_dev_remove_ioctl},
124                 { .cmd = "rename",  .fn = dm_dev_rename_ioctl},
125                 { .cmd = "resume",  .fn = dm_dev_resume_ioctl},
126                 { .cmd = "clear",   .fn = dm_table_clear_ioctl},
127                 { .cmd = "deps",    .fn = dm_table_deps_ioctl},
128                 { .cmd = "reload",  .fn = dm_table_load_ioctl},
129                 { .cmd = "status",  .fn = dm_table_status_ioctl},
130                 { .cmd = "table",   .fn = dm_table_status_ioctl},
131                 {NULL, NULL}
132 };
133
134 /* New module handle routine */
135 static int
136 dm_modcmd(module_t mod, int cmd, void *unused)
137 {
138         int error, bmajor, cmajor;
139
140         error = 0;
141         bmajor = -1;
142         cmajor = -1;
143
144         switch (cmd) {
145         case MOD_LOAD:
146                 devfs_clone_bitmap_init(&dm_minor_bitmap);
147                 dm_doinit();
148                 kprintf("Device Mapper version %d.%d.%d loaded\n",
149                     DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL);
150                 break;
151
152         case MOD_UNLOAD:
153                 /*
154                  * Disable unloading of dm module if there are any devices
155                  * defined in driver. This is probably too strong we need
156                  * to disable auto-unload only if there is mounted dm device
157                  * present.
158                  */
159                 if (dm_dev_counter > 0)
160                         return EBUSY;
161
162                 error = dmdestroy();
163                 if (error)
164                         break;
165                 kprintf("Device Mapper unloaded\n");
166                 break;
167
168         default:
169                 break;
170         }
171
172         return error;
173 }
174
175 /*
176  * dm_detach:
177  *
178  *      Autoconfiguration detach function for pseudo-device glue.
179  * This routine is called by dm_ioctl::dm_dev_remove_ioctl and by autoconf to
180  * remove devices created in device-mapper.
181  */
182 int
183 dm_detach(dm_dev_t *dmv)
184 {
185         int minor;
186
187         /* Remove device from list and wait for refcnt to drop to zero */
188         dm_dev_rem(dmv, NULL, NULL, -1);
189
190         /* Destroy active table first.  */
191         dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
192
193         /* Destroy inactive table if exits, too. */
194         dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
195
196         dm_table_head_destroy(&dmv->table_head);
197
198         minor = dkunit(dmv->devt);
199         disk_destroy(dmv->diskp);
200         devstat_remove_entry(&dmv->stats);
201
202         release_dev(dmv->devt);
203         devfs_clone_bitmap_put(&dm_minor_bitmap, minor);
204
205         /* Destroy device */
206         (void)dm_dev_free(dmv);
207
208         /* Decrement device counter After removing device */
209         --dm_dev_counter; /* XXX: was atomic 64 */
210
211         return 0;
212 }
213
214 static void
215 dm_doinit(void)
216 {
217         dm_target_init();
218         dm_dev_init();
219         dm_pdev_init();
220         dmcdev = make_dev(&dm_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control");
221 }
222
223 /* Destroy routine */
224 static int
225 dmdestroy(void)
226 {
227         destroy_dev(dmcdev);
228
229         dm_dev_destroy();
230         dm_pdev_destroy();
231         dm_target_destroy();
232
233         return 0;
234 }
235
236 static int
237 dmopen(struct dev_open_args *ap)
238 {
239         cdev_t dev = ap->a_head.a_dev;
240         dm_dev_t *dmv;
241
242         /* Shortcut for the control device */
243         if (minor(dev) == 0)
244                 return 0;
245
246         if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
247                 return ENXIO;
248
249         dmv->is_open = 1;
250         dm_dev_unbusy(dmv);
251
252         aprint_debug("dm open routine called %" PRIu32 "\n",
253             minor(ap->a_head.a_dev));
254         return 0;
255 }
256
257 static int
258 dmclose(struct dev_close_args *ap)
259 {
260         cdev_t dev = ap->a_head.a_dev;
261         dm_dev_t *dmv;
262
263         /* Shortcut for the control device */
264         if (minor(dev) == 0)
265                 return 0;
266
267         if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
268                 return ENXIO;
269
270         dmv->is_open = 0;
271         dm_dev_unbusy(dmv);
272
273         aprint_debug("dm close routine called %" PRIu32 "\n",
274             minor(ap->a_head.a_dev));
275         return 0;
276 }
277
278
279 static int
280 dmioctl(struct dev_ioctl_args *ap)
281 {
282         cdev_t dev = ap->a_head.a_dev;
283         u_long cmd = ap->a_cmd;
284         void *data = ap->a_data;
285
286         int r, err;
287         prop_dictionary_t dm_dict_in;
288
289         err = r = 0;
290
291         aprint_debug("dmioctl called\n");
292
293         KKASSERT(data != NULL);
294
295         if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) {
296                 struct plistref *pref = (struct plistref *) data;
297
298                 /* Check if we were called with NETBSD_DM_IOCTL ioctl
299                    otherwise quit. */
300                 if ((r = dm_ioctl_switch(cmd)) != 0)
301                         return r;
302
303                 if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
304                         return r;
305
306                 if ((r = dm_check_version(dm_dict_in)) != 0)
307                         goto cleanup_exit;
308
309                 /* run ioctl routine */
310                 if ((err = dm_cmd_to_fun(dm_dict_in)) != 0)
311                         goto cleanup_exit;
312
313 cleanup_exit:
314                 r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
315                 prop_object_release(dm_dict_in);
316         }
317
318         /*
319          * Return the error of the actual command if one one has
320          * happened. Otherwise return 'r' which indicates errors
321          * that occurred during helper operations.
322          */
323         return (err != 0)?err:r;
324 }
325
326 /*
327  * Translate command sent from libdevmapper to func.
328  */
329 static int
330 dm_cmd_to_fun(prop_dictionary_t dm_dict){
331         int i, r;
332         prop_string_t command;
333
334         r = 0;
335
336         if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
337                 return EINVAL;
338
339         for(i = 0; cmd_fn[i].cmd != NULL; i++)
340                 if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
341                         break;
342
343         if (cmd_fn[i].cmd == NULL)
344                 return EINVAL;
345
346         aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
347         r = cmd_fn[i].fn(dm_dict);
348
349         return r;
350 }
351
352 /* Call apropriate ioctl handler function. */
353 static int
354 dm_ioctl_switch(u_long cmd)
355 {
356
357         switch(cmd) {
358
359         case NETBSD_DM_IOCTL:
360                 aprint_debug("dm NetBSD_DM_IOCTL called\n");
361                 break;
362         default:
363                  aprint_debug("dm unknown ioctl called\n");
364                  return ENOTTY;
365                  break; /* NOT REACHED */
366         }
367
368          return 0;
369 }
370
371  /*
372   * Check for disk specific ioctls.
373   */
374
375 static int
376 disk_ioctl_switch(cdev_t dev, u_long cmd, void *data)
377 {
378         dm_dev_t *dmv;
379
380         /* disk ioctls make sense only on block devices */
381         if (minor(dev) == 0)
382                 return ENOTTY;
383
384         switch(cmd) {
385         case DIOCGPART:
386         {
387                 struct partinfo *dpart;
388                 u_int64_t size;
389                 dpart = (void *)data;
390                 bzero(dpart, sizeof(*dpart));
391
392                 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
393                         return ENODEV;
394                 if (dmv->diskp->d_info.d_media_blksize == 0) {
395                         dm_dev_unbusy(dmv);
396                         return ENOTSUP;
397                 } else {
398                         size = dm_table_size(&dmv->table_head);
399                         dpart->media_offset  = 0;
400                         dpart->media_size    = size * DEV_BSIZE;
401                         dpart->media_blocks  = size;
402                         dpart->media_blksize = DEV_BSIZE;
403                         dpart->fstype = FS_BSDFFS;
404                 }
405                 dm_dev_unbusy(dmv);
406                 break;
407         }
408
409         default:
410                 aprint_debug("unknown disk_ioctl called\n");
411                 return ENOTTY;
412                 break; /* NOT REACHED */
413         }
414
415         return 0;
416 }
417
418 /*
419  * Do all IO operations on dm logical devices.
420  */
421 static int
422 dmstrategy(struct dev_strategy_args *ap)
423 {
424         cdev_t dev = ap->a_head.a_dev;
425         struct bio *bio = ap->a_bio;
426         struct buf *bp = bio->bio_buf;
427         int bypass;
428
429         dm_dev_t *dmv;
430         dm_table_t  *tbl;
431         dm_table_entry_t *table_en;
432         struct buf *nestbuf;
433
434         uint32_t dev_type;
435
436         uint64_t buf_start, buf_len, issued_len;
437         uint64_t table_start, table_end;
438         uint64_t start, end;
439
440         buf_start = bio->bio_offset;
441         buf_len = bp->b_bcount;
442
443         tbl = NULL;
444
445         table_end = 0;
446         dev_type = 0;
447         issued_len = 0;
448
449         if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) {
450                 bp->b_error = EIO;
451                 bp->b_resid = bp->b_bcount;
452                 biodone(bio);
453                 return 0;
454         }
455
456         switch(bp->b_cmd) {
457         case BUF_CMD_READ:
458         case BUF_CMD_WRITE:
459         case BUF_CMD_FREEBLKS:
460                 bypass = 0;
461                 break;
462         case BUF_CMD_FLUSH:
463                 bypass = 1;
464                 KKASSERT(buf_len == 0);
465                 break;
466         default:
467                 dm_dev_unbusy(dmv);
468                 bp->b_error = EIO;
469                 bp->b_resid = bp->b_bcount;
470                 biodone(bio);
471                 return 0;
472         }
473
474         if (bypass == 0 &&
475             bounds_check_with_mediasize(bio, DEV_BSIZE,
476                                         dm_table_size(&dmv->table_head)) <= 0) {
477                 dm_dev_unbusy(dmv);
478                 bp->b_resid = bp->b_bcount;
479                 biodone(bio);
480                 return 0;
481         }
482
483         /* Select active table */
484         tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
485
486         nestiobuf_init(bio);
487         devstat_start_transaction(&dmv->stats);
488
489         /*
490          * Find out what tables I want to select.
491          */
492         SLIST_FOREACH(table_en, tbl, next) {
493                 /*
494                  * I need need number of bytes not blocks.
495                  */
496                 table_start = table_en->start * DEV_BSIZE;
497                 table_end = table_start + (table_en->length) * DEV_BSIZE;
498
499                 /*
500                  * Calculate the start and end
501                  */
502                 start = MAX(table_start, buf_start);
503                 end = MIN(table_end, buf_start + buf_len);
504
505                 aprint_debug("----------------------------------------\n");
506                 aprint_debug("table_start %010" PRIu64", table_end %010"
507                     PRIu64 "\n", table_start, table_end);
508                 aprint_debug("buf_start %010" PRIu64", buf_len %010"
509                     PRIu64"\n", buf_start, buf_len);
510                 aprint_debug("start-buf_start %010"PRIu64", end %010"
511                     PRIu64"\n", start - buf_start, end);
512                 aprint_debug("start %010" PRIu64" , end %010"
513                     PRIu64"\n", start, end);
514                 aprint_debug("\n----------------------------------------\n");
515
516                 if (bypass) {
517                         nestbuf = getpbuf(NULL);
518                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
519
520                         nestiobuf_add(bio, nestbuf, 0, 0, &dmv->stats);
521                         nestbuf->b_bio1.bio_offset = 0;
522                         table_en->target->strategy(table_en, nestbuf);
523                 } else if (start < end) {
524                         nestbuf = getpbuf(NULL);
525                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
526
527                         nestiobuf_add(bio, nestbuf,
528                                       start - buf_start, (end - start),
529                                       &dmv->stats);
530                         issued_len += end - start;
531
532                         nestbuf->b_bio1.bio_offset = (start - table_start);
533                         table_en->target->strategy(table_en, nestbuf);
534                 }
535         }
536
537         if (issued_len < buf_len)
538                 nestiobuf_error(bio, EINVAL);
539         nestiobuf_start(bio);
540         dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
541         dm_dev_unbusy(dmv);
542
543         return 0;
544 }
545
546 static int
547 dmdump(struct dev_dump_args *ap)
548 {
549         cdev_t dev = ap->a_head.a_dev;
550         dm_dev_t *dmv;
551         dm_table_t  *tbl;
552         dm_table_entry_t *table_en;
553         uint32_t dev_type;
554         uint64_t buf_start, buf_len, issued_len;
555         uint64_t table_start, table_end;
556         uint64_t start, end, data_offset;
557         off_t offset;
558         size_t length;
559         int error = 0;
560
561         buf_start = ap->a_offset;
562         buf_len = ap->a_length;
563
564         tbl = NULL;
565
566         table_end = 0;
567         dev_type = 0;
568         issued_len = 0;
569
570         if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) {
571                 return EIO;
572         }
573
574         /* Select active table */
575         tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
576
577
578         /*
579          * Find out what tables I want to select.
580          */
581         SLIST_FOREACH(table_en, tbl, next) {
582                 /*
583                  * I need need number of bytes not blocks.
584                  */
585                 table_start = table_en->start * DEV_BSIZE;
586                 table_end = table_start + (table_en->length) * DEV_BSIZE;
587
588                 /*
589                  * Calculate the start and end
590                  */
591                 start = MAX(table_start, buf_start);
592                 end = MIN(table_end, buf_start + buf_len);
593
594                 if (ap->a_length == 0) {
595                         if (table_en->target->dump == NULL) {
596                                 error = ENXIO;
597                                 goto out;
598                         }
599
600                         table_en->target->dump(table_en, NULL, 0, 0);
601                 } else if (start < end) {
602                         data_offset = start - buf_start;
603                         offset = start - table_start;
604                         length = end - start;
605
606                         if (table_en->target->dump == NULL) {
607                                 error = ENXIO;
608                                 goto out;
609                         }
610
611                         table_en->target->dump(table_en,
612                             (char *)ap->a_virtual + data_offset,
613                             length, offset);
614
615                         issued_len += end - start;
616                 }
617         }
618
619         if (issued_len < buf_len)
620                 error = EINVAL;
621
622 out:
623         dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
624         dm_dev_unbusy(dmv);
625
626         return error;
627 }
628
629 static int
630 dmsize(struct dev_psize_args *ap)
631 {
632         cdev_t dev = ap->a_head.a_dev;
633         dm_dev_t *dmv;
634         uint64_t size;
635
636         size = 0;
637
638         if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
639                         return ENOENT;
640
641         size = dm_table_size(&dmv->table_head);
642         dm_dev_unbusy(dmv);
643
644         ap->a_result = (int64_t)size;
645
646         return 0;
647 }
648
649 #if 0
650 static void
651 dmminphys(struct buf *bp)
652 {
653
654         bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
655 }
656 #endif
657
658 void
659 dmsetdiskinfo(struct disk *disk, dm_table_head_t *head)
660 {
661         struct disk_info info;
662         uint64_t dmp_size;
663
664         dmp_size = dm_table_size(head);
665
666         bzero(&info, sizeof(struct disk_info));
667         info.d_media_blksize = DEV_BSIZE;
668         info.d_media_blocks = dmp_size;
669 #if 0
670         /* this is set by disk_setdiskinfo */
671         info.d_media_size = dmp_size * DEV_BSIZE;
672 #endif
673         info.d_dsflags = DSO_MBRQUIET | DSO_DEVICEMAPPER;
674
675         info.d_secpertrack = 32;
676         info.d_nheads = 64;
677         info.d_secpercyl = info.d_secpertrack * info.d_nheads;
678         info.d_ncylinders = dmp_size / info.d_secpercyl;
679
680         disk_setdiskinfo(disk, &info);
681 }
682
683 TUNABLE_INT("debug.dm_debug", &dm_debug_level);
684 SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level,
685                0, "Eanble device mapper debugging");
686