device-mapper.c revision 1.4 1 /* $NetBSD: device-mapper.c,v 1.4 2009/01/16 00:46:12 haad Exp $ */
2
3 /*
4 * Copyright (c) 2008 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/dkio.h>
42 #include <sys/disk.h>
43 #include <sys/disklabel.h>
44 #include <sys/ioctl.h>
45 #include <sys/ioccom.h>
46 #include <sys/kmem.h>
47 #include <sys/module.h>
48
49 #include "netbsd-dm.h"
50 #include "dm.h"
51
52 static dev_type_open(dmopen);
53 static dev_type_close(dmclose);
54 static dev_type_read(dmread);
55 static dev_type_write(dmwrite);
56 static dev_type_ioctl(dmioctl);
57 static dev_type_strategy(dmstrategy);
58 static dev_type_dump(dmdump);
59 static dev_type_size(dmsize);
60
61 /* attach and detach routines */
62 int dmattach(void);
63 int dmdestroy(void);
64
65 static int dm_cmd_to_fun(prop_dictionary_t);
66 static int disk_ioctl_switch(dev_t, u_long, void *);
67 static int dm_ioctl_switch(u_long);
68 static void dmminphys(struct buf *);
69
70 /* ***Variable-definitions*** */
71 const struct bdevsw dm_bdevsw = {
72 dmopen, dmclose, dmstrategy, dmioctl, dmdump, dmsize, D_DISK | D_MPSAFE
73 };
74
75 const struct cdevsw dm_cdevsw = {
76 dmopen, dmclose, dmread, dmwrite, dmioctl,
77 nostop, notty, nopoll, nommap, nokqfilter, D_DISK | D_MPSAFE
78 };
79
80 extern uint64_t dev_counter;
81
82 /*
83 * This array is used to translate cmd to function pointer.
84 *
85 * Interface between libdevmapper and lvm2tools uses different
86 * names for one IOCTL call because libdevmapper do another thing
87 * then. When I run "info" or "mknodes" libdevmapper will send same
88 * ioctl to kernel but will do another things in userspace.
89 *
90 */
91 struct cmd_function cmd_fn[] = {
92 {"version", dm_get_version_ioctl},
93 {"targets", dm_list_versions_ioctl},
94 {"create", dm_dev_create_ioctl},
95 {"info", dm_dev_status_ioctl},
96 {"mknodes", dm_dev_status_ioctl},
97 {"names", dm_dev_list_ioctl},
98 {"suspend", dm_dev_suspend_ioctl},
99 {"remove", dm_dev_remove_ioctl},
100 {"rename", dm_dev_rename_ioctl},
101 {"resume", dm_dev_resume_ioctl},
102 {"clear", dm_table_clear_ioctl},
103 {"deps", dm_table_deps_ioctl},
104 {"reload", dm_table_load_ioctl},
105 {"status", dm_table_status_ioctl},
106 {"table", dm_table_status_ioctl},
107 {NULL, NULL}
108 };
109
110
111 MODULE(MODULE_CLASS_DRIVER, dm, NULL);
112
113 /* New module handle routine */
114 static int
115 dm_modcmd(modcmd_t cmd, void *arg)
116 {
117 #ifdef _MODULE
118 int bmajor = -1, cmajor = -1;
119
120 switch (cmd) {
121 case MODULE_CMD_INIT:
122 dmattach();
123 return devsw_attach("dm", &dm_bdevsw, &bmajor,
124 &dm_cdevsw, &cmajor);
125 break;
126
127 case MODULE_CMD_FINI:
128 /*
129 * Disable unloading of dm module if there are any devices
130 * defined in driver. This is probably too strong we need
131 * to disable auto-unload only if there is mounted dm device
132 * present.
133 */
134 if (dev_counter > 0)
135 return EBUSY;
136 dmdestroy();
137 return devsw_detach(&dm_bdevsw, &dm_cdevsw);
138 break;
139 case MODULE_CMD_STAT:
140 return ENOTTY;
141
142 default:
143 return ENOTTY;
144 }
145
146 return 0;
147 #else
148
149 if (cmd == MODULE_CMD_INIT)
150 return 0;
151 return ENOTTY;
152
153 #endif /* _MODULE */
154 }
155
156
157 /* attach routine */
158 int
159 dmattach(void)
160 {
161 dm_target_init();
162 dm_dev_init();
163 dm_pdev_init();
164
165 return 0;
166 }
167
168 /* Destroy routine */
169 int
170 dmdestroy(void)
171 {
172 dm_dev_destroy();
173 dm_pdev_destroy();
174 dm_target_destroy();
175
176 return 0;
177 }
178
179 static int
180 dmopen(dev_t dev, int flags, int mode, struct lwp *l)
181 {
182 aprint_debug("open routine called %llu\n", minor(dev));
183 return 0;
184 }
185
186 static int
187 dmclose(dev_t dev, int flags, int mode, struct lwp *l)
188 {
189 aprint_debug("CLOSE routine called\n");
190
191 return 0;
192 }
193
194
195 static int
196 dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
197 {
198 int r;
199 prop_dictionary_t dm_dict_in;
200
201 r = 0;
202
203 aprint_debug("dmioctl called\n");
204
205 KASSERT(data != NULL);
206
207 if (disk_ioctl_switch(dev, cmd, data) != 0) {
208 struct plistref *pref = (struct plistref *) data;
209
210 if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
211 return r;
212
213 dm_check_version(dm_dict_in);
214
215 /* call cmd selected function */
216 if ((r = dm_ioctl_switch(cmd)) != 0) {
217 prop_object_release(dm_dict_in);
218 return r;
219 }
220
221 /* run ioctl routine */
222 if ((r = dm_cmd_to_fun(dm_dict_in)) != 0) {
223 prop_object_release(dm_dict_in);
224 return r;
225 }
226
227 r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
228
229 prop_object_release(dm_dict_in);
230 }
231
232 return r;
233 }
234
235 /*
236 * Translate command sent from libdevmapper to func.
237 */
238 static int
239 dm_cmd_to_fun(prop_dictionary_t dm_dict){
240 int i, r;
241 prop_string_t command;
242
243 r = 0;
244
245 if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
246 return EINVAL;
247
248 for(i = 0; cmd_fn[i].cmd != NULL; i++)
249 if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
250 break;
251
252 if (cmd_fn[i].cmd == NULL)
253 return EINVAL;
254
255 aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
256 r = cmd_fn[i].fn(dm_dict);
257
258 return r;
259 }
260
261 /* Call apropriate ioctl handler function. */
262 static int
263 dm_ioctl_switch(u_long cmd)
264 {
265 int r;
266
267 r = 0;
268
269 switch(cmd) {
270
271 case NETBSD_DM_IOCTL:
272 aprint_debug("NetBSD_DM_IOCTL called\n");
273 break;
274
275 default:
276 aprint_debug("unknown ioctl called\n");
277 return ENOTTY;
278 break; /* NOT REACHED */
279 }
280
281 return r;
282 }
283
284 /*
285 * Check for disk specific ioctls.
286 */
287
288 static int
289 disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
290 {
291 dm_dev_t *dmv;
292
293 switch(cmd) {
294 case DIOCGWEDGEINFO:
295 {
296 struct dkwedge_info *dkw = (void *) data;
297
298 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
299 return ENOENT;
300
301 aprint_normal("DIOCGWEDGEINFO ioctl called\n");
302
303 strlcpy(dkw->dkw_devname, dmv->name, 16);
304 strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
305 strlcpy(dkw->dkw_parent, dmv->name, 16);
306
307 dkw->dkw_offset = 0;
308 dkw->dkw_size = dm_table_size(&dmv->table_head);
309 strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
310
311 dm_dev_unbusy(dmv);
312 break;
313 }
314
315 case DIOCGDINFO:
316 {
317 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
318 return ENOENT;
319
320 aprint_debug("DIOCGDINFO %d\n", dmv->diskp->dk_label->d_secsize);
321
322 *(struct disklabel *)data = *(dmv->diskp->dk_label);
323
324 dm_dev_unbusy(dmv);
325 break;
326 }
327
328 case DIOCGPART:
329 {
330 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
331 return ENOENT;
332
333 ((struct partinfo *)data)->disklab = dmv->diskp->dk_label;
334 ((struct partinfo *)data)->part = &dmv->diskp->dk_label->d_partitions[0];
335
336 dm_dev_unbusy(dmv);
337 break;
338 }
339 case DIOCWDINFO:
340 case DIOCSDINFO:
341 case DIOCKLABEL:
342 case DIOCWLABEL:
343 case DIOCGDEFLABEL:
344
345 default:
346 aprint_debug("unknown disk_ioctl called\n");
347 return 1;
348 break; /* NOT REACHED */
349 }
350
351 return 0;
352 }
353
354 /*
355 * Do all IO operations on dm logical devices.
356 */
357 static void
358 dmstrategy(struct buf *bp)
359 {
360 dm_dev_t *dmv;
361 dm_table_t *tbl;
362 dm_table_entry_t *table_en;
363 struct buf *nestbuf;
364
365 uint32_t dev_type;
366
367 uint64_t buf_start, buf_len, issued_len;
368 uint64_t table_start, table_end;
369 uint64_t start, end;
370
371 buf_start = bp->b_blkno * DEV_BSIZE;
372 buf_len = bp->b_bcount;
373
374 tbl = NULL;
375
376 table_end = 0;
377 dev_type = 0;
378 issued_len = 0;
379
380 if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
381 bp->b_error = EIO;
382 bp->b_resid = bp->b_bcount;
383 biodone(bp);
384 return;
385 }
386
387 /* Select active table */
388 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
389
390 /* Nested buffers count down to zero therefore I have
391 to set bp->b_resid to maximal value. */
392 bp->b_resid = bp->b_bcount;
393
394 /*
395 * Find out what tables I want to select.
396 */
397 SLIST_FOREACH(table_en, tbl, next)
398 {
399 /* I need need number of bytes not blocks. */
400 table_start = table_en->start * DEV_BSIZE;
401 /*
402 * I have to sub 1 from table_en->length to prevent
403 * off by one error
404 */
405 table_end = table_start + (table_en->length)* DEV_BSIZE;
406
407 start = MAX(table_start, buf_start);
408
409 end = MIN(table_end, buf_start + buf_len);
410
411 aprint_debug("----------------------------------------\n");
412 aprint_debug("table_start %010" PRIu64", table_end %010"
413 PRIu64 "\n", table_start, table_end);
414 aprint_debug("buf_start %010" PRIu64", buf_len %010"
415 PRIu64"\n", buf_start, buf_len);
416 aprint_debug("start-buf_start %010"PRIu64", end %010"
417 PRIu64"\n", start - buf_start, end);
418 aprint_debug("start %010" PRIu64" , end %010"
419 PRIu64"\n", start, end);
420 aprint_debug("\n----------------------------------------\n");
421
422 if (start < end) {
423 /* create nested buffer */
424 nestbuf = getiobuf(NULL, true);
425
426 nestiobuf_setup(bp, nestbuf, start - buf_start,
427 (end - start));
428
429 issued_len += end - start;
430
431 /* I need number of blocks. */
432 nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
433
434 table_en->target->strategy(table_en, nestbuf);
435 }
436 }
437
438 if (issued_len < buf_len)
439 nestiobuf_done(bp, buf_len - issued_len, EINVAL);
440
441 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
442 dm_dev_unbusy(dmv);
443
444 return;
445 }
446
447
448 static int
449 dmread(dev_t dev, struct uio *uio, int flag)
450 {
451 return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
452 }
453
454 static int
455 dmwrite(dev_t dev, struct uio *uio, int flag)
456 {
457 return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
458 }
459
460 static int
461 dmdump(dev_t dev, daddr_t blkno, void *va, size_t size)
462 {
463 return ENODEV;
464 }
465
466 static int
467 dmsize(dev_t dev)
468 {
469 dm_dev_t *dmv;
470 uint64_t size;
471
472 size = 0;
473
474 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
475 return -ENOENT;
476
477 size = dm_table_size(&dmv->table_head);
478 dm_dev_unbusy(dmv);
479
480 return size;
481 }
482
483 static void
484 dmminphys(struct buf *bp)
485 {
486 bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
487 }
488
489 /*
490 * Load the label information on the named device
491 * Actually fabricate a disklabel.
492 *
493 * EVENTUALLY take information about different
494 * data tracks from the TOC and put it in the disklabel
495 *
496 * Copied from vnd code.
497 */
498 void
499 dmgetdisklabel(struct disklabel *lp, dm_table_head_t *head)
500 {
501 struct partition *pp;
502 int dmp_size;
503
504 dmp_size = dm_table_size(head);
505
506 /*
507 * Size must be at least 2048 DEV_BSIZE blocks
508 * (1M) in order to use this geometry.
509 */
510
511 lp->d_secperunit = dmp_size;
512 lp->d_secsize = DEV_BSIZE;
513 lp->d_nsectors = 32;
514 lp->d_ntracks = 64;
515 lp->d_ncylinders = dmp_size / (lp->d_nsectors * lp->d_ntracks);
516 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
517
518 strncpy(lp->d_typename, "lvm", sizeof(lp->d_typename));
519 lp->d_type = DTYPE_DM;
520 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
521 lp->d_rpm = 3600;
522 lp->d_interleave = 1;
523 lp->d_flags = 0;
524
525 pp = &lp->d_partitions[0];
526 /*
527 * This is logical offset and therefore it can be 0
528 * I will consider table offsets later in dmstrategy.
529 */
530 pp->p_offset = 0;
531 pp->p_size = dmp_size * DEV_BSIZE;
532 pp->p_fstype = FS_BSDFFS; /* default value */
533 lp->d_npartitions = 1;
534
535 lp->d_magic = DISKMAGIC;
536 lp->d_magic2 = DISKMAGIC;
537 lp->d_checksum = dkcksum(lp);
538
539 return;
540 }
541