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