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