xref: /netbsd-src/sys/dev/dm/device-mapper.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*        $NetBSD: device-mapper.c,v 1.2 2008/12/19 15:24:03 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 			prop_object_release(dm_dict_in);
217 			return r;
218 		}
219 
220 		/* run ioctl routine */
221 		if ((r = dm_cmd_to_fun(dm_dict_in)) != 0) {
222 			prop_object_release(dm_dict_in);
223 			return r;
224 		}
225 
226 		r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
227 
228 		prop_object_release(dm_dict_in);
229 	}
230 
231 	return r;
232 }
233 
234 /*
235  * Translate command sent from libdevmapper to func.
236  */
237 static int
238 dm_cmd_to_fun(prop_dictionary_t dm_dict){
239 	int i, r;
240 	prop_string_t command;
241 
242 	r = 0;
243 
244 	if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
245 		return EINVAL;
246 
247 	for(i = 0; cmd_fn[i].cmd != NULL; i++)
248 		if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
249 			break;
250 
251 	if (cmd_fn[i].cmd == NULL)
252 		return EINVAL;
253 
254 	aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
255 	r = cmd_fn[i].fn(dm_dict);
256 
257 	return r;
258 }
259 
260 /* Call apropriate ioctl handler function. */
261 static int
262 dm_ioctl_switch(u_long cmd)
263 {
264 	int r;
265 
266 	r = 0;
267 
268 	switch(cmd) {
269 
270 	case NETBSD_DM_IOCTL:
271 		aprint_debug("NetBSD_DM_IOCTL called\n");
272 		break;
273 
274 	default:
275 		 aprint_debug("unknown ioctl called\n");
276 		 return ENOTTY;
277 		 break; /* NOT REACHED */
278 	}
279 
280 	 return r;
281 }
282 
283  /*
284   * Check for disk specific ioctls.
285   */
286 
287 static int
288 disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
289 {
290 	dm_dev_t *dmv;
291 
292 	switch(cmd) {
293 	case DIOCGWEDGEINFO:
294 	{
295 		struct dkwedge_info *dkw = (void *) data;
296 
297 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
298 			return ENOENT;
299 
300 		aprint_normal("DIOCGWEDGEINFO ioctl called\n");
301 
302 		strlcpy(dkw->dkw_devname, dmv->name, 16);
303 		strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
304 		strlcpy(dkw->dkw_parent, dmv->name, 16);
305 
306 		dkw->dkw_offset = 0;
307 		dkw->dkw_size = dm_table_size(&dmv->table_head);
308 		strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
309 
310 		dm_dev_unbusy(dmv);
311 		break;
312 	}
313 
314 	case DIOCGDINFO:
315 	{
316 
317 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
318 			return ENOENT;
319 
320 		aprint_debug("DIOCGDINFO %d\n", dmv->dk_label->d_secsize);
321 
322 		*(struct disklabel *)data = *(dmv->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->dk_label;
334 		((struct partinfo *)data)->part = &dmv->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