device-mapper.c revision 1.1.2.16 1 /* $NetBSD: device-mapper.c,v 1.1.2.16 2008/11/05 13:45:02 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 int unload;
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_MISC, 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 dmdestroy();
129 return devsw_detach(&dm_bdevsw, &dm_cdevsw);
130 break;
131
132 case MODULE_CMD_STAT:
133 return ENOTTY;
134
135 default:
136 return ENOTTY;
137 }
138
139 return 0;
140 #else
141
142 if (cmd == MODULE_CMD_INIT)
143 return 0;
144 return ENOTTY;
145
146 #endif /* _MODULE */
147 }
148
149
150 /* attach routine */
151 int
152 dmattach(void)
153 {
154 dm_target_init();
155 dm_dev_init();
156 dm_pdev_init();
157
158 return 0;
159 }
160
161 /* Destroy routine */
162 int
163 dmdestroy(void)
164 {
165 atomic_inc_32(&unload);
166
167 dm_dev_destroy();
168 dm_pdev_destroy();
169 dm_target_destroy();
170
171 return 0;
172 }
173
174 static int
175 dmopen(dev_t dev, int flags, int mode, struct lwp *l)
176 {
177 aprint_debug("open routine called %d\n", minor(dev));
178
179 if (unload == 1)
180 return EBUSY;
181
182 return 0;
183 }
184
185 static int
186 dmclose(dev_t dev, int flags, int mode, struct lwp *l)
187 {
188 aprint_debug("CLOSE routine called\n");
189
190 return 0;
191 }
192
193
194 static int
195 dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
196 {
197 int r;
198 prop_dictionary_t dm_dict_in;
199
200 r = 0;
201
202 aprint_debug("dmioctl called\n");
203
204 KASSERT(data != NULL);
205
206 if (disk_ioctl_switch(dev, cmd, data) != 0) {
207 struct plistref *pref = (struct plistref *) data;
208
209 if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
210 return r;
211
212 dm_check_version(dm_dict_in);
213
214 /* call cmd selected function */
215 if ((r = dm_ioctl_switch(cmd)) != 0)
216 return r;
217
218 char *xml;
219 xml = prop_dictionary_externalize(dm_dict_in);
220 aprint_verbose("%s\n",xml);
221
222 /* run ioctl routine */
223 if ((r = dm_cmd_to_fun(dm_dict_in)) != 0)
224 return r;
225
226 r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
227 }
228
229 return r;
230 }
231
232 /*
233 * Translate command sent from libdevmapper to func.
234 */
235 static int
236 dm_cmd_to_fun(prop_dictionary_t dm_dict){
237 int i,len,slen;
238 int r;
239 const char *command;
240
241 r = 0;
242
243 (void)prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_COMMAND,
244 &command);
245
246 len = strlen(command);
247
248 for(i=0; cmd_fn[i].cmd != NULL; i++){
249 slen = strlen(cmd_fn[i].cmd);
250
251 if (len != slen)
252 continue;
253
254 if ((strncmp(command, cmd_fn[i].cmd, slen)) == 0) {
255 aprint_normal("ioctl command: %s\n", command);
256 r = cmd_fn[i].fn(dm_dict);
257 break;
258 }
259 }
260
261 return r;
262 }
263
264 /* Call apropriate ioctl handler function. */
265 static int
266 dm_ioctl_switch(u_long cmd)
267 {
268 int r;
269
270 r = 0;
271
272 switch(cmd) {
273
274 case NETBSD_DM_IOCTL:
275 aprint_debug("NetBSD_DM_IOCTL called\n");
276 break;
277
278 default:
279 aprint_debug("unknown ioctl called\n");
280 return ENOTTY;
281 break; /* NOT REACHED */
282 }
283
284 return r;
285 }
286
287 /*
288 * Check for disk specific ioctls.
289 */
290
291 static int
292 disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
293 {
294 dm_dev_t *dmv;
295
296 switch(cmd) {
297 case DIOCGWEDGEINFO:
298 {
299 struct dkwedge_info *dkw = (void *) data;
300
301 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
302 return ENOENT;
303
304 aprint_normal("DIOCGWEDGEINFO ioctl called\n");
305
306 strlcpy(dkw->dkw_devname, dmv->name, 16);
307 strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
308 strlcpy(dkw->dkw_parent, dmv->name, 16);
309
310 dkw->dkw_offset = 0;
311 dkw->dkw_size = dm_table_size(&dmv->table_head);
312 strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
313
314 dm_dev_unbusy(dmv);
315 break;
316 }
317
318 case DIOCGDINFO:
319 {
320
321 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
322 return ENOENT;
323
324 aprint_debug("DIOCGDINFO %d\n", dmv->dk_label->d_secsize);
325
326 *(struct disklabel *)data = *(dmv->dk_label);
327
328 dm_dev_unbusy(dmv);
329 break;
330 }
331
332 case DIOCGPART:
333 {
334 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
335 return ENOENT;
336
337 ((struct partinfo *)data)->disklab = dmv->dk_label;
338 ((struct partinfo *)data)->part = &dmv->dk_label->d_partitions[0];
339
340 dm_dev_unbusy(dmv);
341 break;
342 }
343 case DIOCWDINFO:
344 case DIOCSDINFO:
345 case DIOCKLABEL:
346 case DIOCWLABEL:
347 case DIOCGDEFLABEL:
348
349 default:
350 aprint_debug("unknown disk_ioctl called\n");
351 return 1;
352 break; /* NOT REACHED */
353 }
354
355 return 0;
356 }
357
358 /*
359 * Do all IO operations on dm logical devices.
360 */
361 static void
362 dmstrategy(struct buf *bp)
363 {
364 dm_dev_t *dmv;
365 dm_table_t *tbl;
366 dm_table_entry_t *table_en;
367 struct buf *nestbuf;
368
369 uint32_t dev_type;
370
371 uint64_t buf_start, buf_len, issued_len;
372 uint64_t table_start, table_end;
373 uint64_t start, end;
374
375 buf_start = bp->b_blkno * DEV_BSIZE;
376 buf_len = bp->b_bcount;
377
378 tbl = NULL;
379
380 table_end = 0;
381 dev_type = 0;
382 issued_len = 0;
383
384 if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
385 bp->b_error = EIO;
386 bp->b_resid = bp->b_bcount;
387 biodone(bp);
388 return;
389 }
390
391 /* Select active table */
392 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
393
394 /* Nested buffers count down to zero therefore I have
395 to set bp->b_resid to maximal value. */
396 bp->b_resid = bp->b_bcount;
397
398 /*
399 * Find out what tables I want to select.
400 */
401 SLIST_FOREACH(table_en, tbl, next)
402 {
403 /* I need need number of bytes not blocks. */
404 table_start = table_en->start * DEV_BSIZE;
405 /*
406 * I have to sub 1 from table_en->length to prevent
407 * off by one error
408 */
409 table_end = table_start + (table_en->length)* DEV_BSIZE;
410
411 start = MAX(table_start, buf_start);
412
413 end = MIN(table_end, buf_start + buf_len);
414
415 aprint_debug("----------------------------------------\n");
416 aprint_debug("table_start %010" PRIu64", table_end %010"
417 PRIu64 "\n", table_start, table_end);
418 aprint_debug("buf_start %010" PRIu64", buf_len %010"
419 PRIu64"\n", buf_start, buf_len);
420 aprint_debug("start-buf_start %010"PRIu64", end %010"
421 PRIu64"\n", start - buf_start, end);
422 aprint_debug("start %010" PRIu64" , end %010"
423 PRIu64"\n", start, end);
424 aprint_debug("\n----------------------------------------\n");
425
426 if (start < end) {
427 /* create nested buffer */
428 nestbuf = getiobuf(NULL, true);
429
430 nestiobuf_setup(bp, nestbuf, start - buf_start,
431 (end - start));
432
433 issued_len += end - start;
434
435 /* I need number of blocks. */
436 nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
437
438 table_en->target->strategy(table_en, nestbuf);
439 }
440 }
441
442 if (issued_len < buf_len)
443 nestiobuf_done(bp, buf_len - issued_len, EINVAL);
444
445 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
446 dm_dev_unbusy(dmv);
447
448 return;
449 }
450
451
452 static int
453 dmread(dev_t dev, struct uio *uio, int flag)
454 {
455 return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
456 }
457
458 static int
459 dmwrite(dev_t dev, struct uio *uio, int flag)
460 {
461 return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
462 }
463
464 static int
465 dmdump(dev_t dev, daddr_t blkno, void *va, size_t size)
466 {
467 return ENODEV;
468 }
469
470 static int
471 dmsize(dev_t dev)
472 {
473 dm_dev_t *dmv;
474 uint64_t size;
475
476 size = 0;
477
478 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
479 return -ENOENT;
480
481 size = dm_table_size(&dmv->table_head);
482 dm_dev_unbusy(dmv);
483
484 return size;
485 }
486
487 static void
488 dmminphys(struct buf *bp)
489 {
490 bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
491 }
492
493 /*
494 * Load the label information on the named device
495 * Actually fabricate a disklabel.
496 *
497 * EVENTUALLY take information about different
498 * data tracks from the TOC and put it in the disklabel
499 *
500 * Copied from vnd code.
501 */
502 void
503 dmgetdisklabel(struct disklabel *lp, dm_table_head_t *head)
504 {
505 struct partition *pp;
506 int dmp_size;
507
508 dmp_size = dm_table_size(head);
509
510 /*
511 * Size must be at least 2048 DEV_BSIZE blocks
512 * (1M) in order to use this geometry.
513 */
514
515 lp->d_secperunit = dmp_size;
516 lp->d_secsize = DEV_BSIZE;
517 lp->d_nsectors = 32;
518 lp->d_ntracks = 64;
519 lp->d_ncylinders = dmp_size / (lp->d_nsectors * lp->d_ntracks);
520 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
521
522 strncpy(lp->d_typename, "lvm", sizeof(lp->d_typename));
523 lp->d_type = DTYPE_DM;
524 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
525 lp->d_rpm = 3600;
526 lp->d_interleave = 1;
527 lp->d_flags = 0;
528
529 pp = &lp->d_partitions[0];
530 /*
531 * This is logical offset and therefore it can be 0
532 * I will consider table offsets later in dmstrategy.
533 */
534 pp->p_offset = 0;
535 pp->p_size = dmp_size * DEV_BSIZE;
536 pp->p_fstype = FS_BSDFFS; /* default value */
537 lp->d_npartitions = 1;
538
539 lp->d_magic = DISKMAGIC;
540 lp->d_magic2 = DISKMAGIC;
541 lp->d_checksum = dkcksum(lp);
542
543 return;
544 }
545