xref: /dflybsd-src/sys/dev/disk/dm/device-mapper.c (revision 9d275733e661f39fb2a0484f2a1464e5af3f1e7d)
1ff56536eSAlex Hornung /*        $NetBSD: device-mapper.c,v 1.22 2010/03/26 15:46:04 jakllsch Exp $ */
2ff56536eSAlex Hornung 
3ff56536eSAlex Hornung /*
4ba2ce341SAlex Hornung  * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5ff56536eSAlex Hornung  * Copyright (c) 2010 The NetBSD Foundation, Inc.
6ff56536eSAlex Hornung  * All rights reserved.
7ff56536eSAlex Hornung  *
8ff56536eSAlex Hornung  * This code is derived from software contributed to The NetBSD Foundation
9ff56536eSAlex Hornung  * by Adam Hamsik.
10ff56536eSAlex Hornung  *
11ff56536eSAlex Hornung  * Redistribution and use in source and binary forms, with or without
12ff56536eSAlex Hornung  * modification, are permitted provided that the following conditions
13ff56536eSAlex Hornung  * are met:
14ff56536eSAlex Hornung  * 1. Redistributions of source code must retain the above copyright
15ff56536eSAlex Hornung  *    notice, this list of conditions and the following disclaimer.
16ff56536eSAlex Hornung  * 2. Redistributions in binary form must reproduce the above copyright
17ff56536eSAlex Hornung  *    notice, this list of conditions and the following disclaimer in the
18ff56536eSAlex Hornung  *    documentation and/or other materials provided with the distribution.
19ff56536eSAlex Hornung  *
20ff56536eSAlex Hornung  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21ff56536eSAlex Hornung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22ff56536eSAlex Hornung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23ff56536eSAlex Hornung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24ff56536eSAlex Hornung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25ff56536eSAlex Hornung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26ff56536eSAlex Hornung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27ff56536eSAlex Hornung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28ff56536eSAlex Hornung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29ff56536eSAlex Hornung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30ff56536eSAlex Hornung  * POSSIBILITY OF SUCH DAMAGE.
31ff56536eSAlex Hornung  */
32ff56536eSAlex Hornung 
33ff56536eSAlex Hornung /*
34ff56536eSAlex Hornung  * I want to say thank you to all people who helped me with this project.
35ff56536eSAlex Hornung  */
36ff56536eSAlex Hornung 
370ba7b1a4SSascha Wildner #include <sys/ctype.h>
38ff56536eSAlex Hornung #include <sys/conf.h>
39ff56536eSAlex Hornung #include <sys/device.h>
40ff56536eSAlex Hornung #include <sys/disk.h>
41ff56536eSAlex Hornung #include <sys/disklabel.h>
425b279a20SAlex Hornung #include <sys/dtype.h>
435b279a20SAlex Hornung #include <sys/malloc.h>
445b279a20SAlex Hornung #include <sys/module.h>
45aadb5a11SAlex Hornung #include <sys/sysctl.h>
46a84e173eSAlex Hornung #include <dev/disk/dm/dm.h>
4730ef4508STomohiro Kusumi #include <dev/disk/dm/netbsd-dm.h>
48ff56536eSAlex Hornung 
495b279a20SAlex Hornung static	d_ioctl_t	dmioctl;
505b279a20SAlex Hornung static	d_open_t	dmopen;
515b279a20SAlex Hornung static	d_close_t	dmclose;
525b279a20SAlex Hornung static	d_psize_t	dmsize;
535b279a20SAlex Hornung static	d_strategy_t	dmstrategy;
5479b7159fSAlex Hornung static	d_dump_t	dmdump;
55ff56536eSAlex Hornung 
567350b8b6STomohiro Kusumi /* New module handle and destroy routines */
575b279a20SAlex Hornung static int dm_modcmd(module_t mod, int cmd, void *unused);
58ff56536eSAlex Hornung static int dmdestroy(void);
59ff56536eSAlex Hornung 
60ff56536eSAlex Hornung static void dm_doinit(void);
61ff56536eSAlex Hornung 
62ff56536eSAlex Hornung static int dm_cmd_to_fun(prop_dictionary_t);
635b279a20SAlex Hornung static int disk_ioctl_switch(cdev_t, u_long, void *);
64ff56536eSAlex Hornung 
65963efb75STomohiro Kusumi static struct dev_ops dmctl_ops = {
66963efb75STomohiro Kusumi 	{ "dm", 0, D_MPSAFE },
67963efb75STomohiro Kusumi 	.d_open		= dmopen,
68963efb75STomohiro Kusumi 	.d_close	= dmclose,
69963efb75STomohiro Kusumi 	.d_ioctl	= dmioctl,
70963efb75STomohiro Kusumi };
71963efb75STomohiro Kusumi 
725b279a20SAlex Hornung struct dev_ops dm_ops = {
739f889dc4SMatthew Dillon 	{ "dm", 0, D_DISK | D_MPSAFE },
74ff56536eSAlex Hornung 	.d_open		= dmopen,
75ff56536eSAlex Hornung 	.d_close	= dmclose,
765b279a20SAlex Hornung 	.d_read		= physread,
775b279a20SAlex Hornung 	.d_write	= physwrite,
785b279a20SAlex Hornung 	.d_ioctl	= dmioctl,
79ff56536eSAlex Hornung 	.d_strategy	= dmstrategy,
80ff56536eSAlex Hornung 	.d_psize	= dmsize,
8179b7159fSAlex Hornung 	.d_dump		= dmdump,
82ff56536eSAlex Hornung };
83ff56536eSAlex Hornung 
845b279a20SAlex Hornung MALLOC_DEFINE(M_DM, "dm", "Device Mapper allocations");
85ff56536eSAlex Hornung 
86aadb5a11SAlex Hornung int dm_debug_level = 0;
87aadb5a11SAlex Hornung 
88ff56536eSAlex Hornung extern uint64_t dm_dev_counter;
89ff56536eSAlex Hornung 
905b279a20SAlex Hornung static cdev_t dmcdev;
915b279a20SAlex Hornung 
925b279a20SAlex Hornung static moduledata_t dm_mod = {
935b279a20SAlex Hornung     "dm",
945b279a20SAlex Hornung     dm_modcmd,
955b279a20SAlex Hornung     NULL
965b279a20SAlex Hornung };
975b279a20SAlex Hornung DECLARE_MODULE(dm, dm_mod, SI_SUB_RAID, SI_ORDER_ANY);
987115a22bSAlex Hornung MODULE_VERSION(dm, 1);
995b279a20SAlex Hornung 
100ff56536eSAlex Hornung /*
1011a584652STomohiro Kusumi  * This structure is used to translate command sent to kernel driver in
1021a584652STomohiro Kusumi  * <key>command</key>
1031a584652STomohiro Kusumi  * <value></value>
1041a584652STomohiro Kusumi  * to function
1051a584652STomohiro Kusumi  */
1061a584652STomohiro Kusumi /*
107ff56536eSAlex Hornung  * This array is used to translate cmd to function pointer.
108ff56536eSAlex Hornung  *
109ff56536eSAlex Hornung  * Interface between libdevmapper and lvm2tools uses different
110ff56536eSAlex Hornung  * names for one IOCTL call because libdevmapper do another thing
111ff56536eSAlex Hornung  * then. When I run "info" or "mknodes" libdevmapper will send same
112ff56536eSAlex Hornung  * ioctl to kernel but will do another things in userspace.
113ff56536eSAlex Hornung  */
1141a584652STomohiro Kusumi static struct cmd_function {
1151a584652STomohiro Kusumi 	const char *cmd;
1161a584652STomohiro Kusumi 	int (*fn)(prop_dictionary_t);
1171a584652STomohiro Kusumi } cmd_fn[] = {
118dddde3e1STomohiro Kusumi 	{.cmd = "version",    .fn = NULL},
119ff56536eSAlex Hornung 	{.cmd = "targets",    .fn = dm_list_versions_ioctl},
120ff56536eSAlex Hornung 	{.cmd = "create",     .fn = dm_dev_create_ioctl},
121ff56536eSAlex Hornung 	{.cmd = "info",       .fn = dm_dev_status_ioctl},
122ff56536eSAlex Hornung 	{.cmd = "mknodes",    .fn = dm_dev_status_ioctl},
123ff56536eSAlex Hornung 	{.cmd = "names",      .fn = dm_dev_list_ioctl},
124ff56536eSAlex Hornung 	{.cmd = "suspend",    .fn = dm_dev_suspend_ioctl},
125ff56536eSAlex Hornung 	{.cmd = "remove",     .fn = dm_dev_remove_ioctl},
1269fada28aSAlex Hornung 	{.cmd = "remove_all", .fn = dm_dev_remove_all_ioctl},
127ff56536eSAlex Hornung 	{.cmd = "rename",     .fn = dm_dev_rename_ioctl},
128ff56536eSAlex Hornung 	{.cmd = "resume",     .fn = dm_dev_resume_ioctl},
129ff56536eSAlex Hornung 	{.cmd = "clear",      .fn = dm_table_clear_ioctl},
130ff56536eSAlex Hornung 	{.cmd = "deps",       .fn = dm_table_deps_ioctl},
131ff56536eSAlex Hornung 	{.cmd = "reload",     .fn = dm_table_load_ioctl},
132ff56536eSAlex Hornung 	{.cmd = "status",     .fn = dm_table_status_ioctl},
133ff56536eSAlex Hornung 	{.cmd = "table",      .fn = dm_table_status_ioctl},
13451292ec6SAlex Hornung 	{.cmd = "message",    .fn = dm_message_ioctl},
135ff56536eSAlex Hornung };
136ff56536eSAlex Hornung 
137b7c11cdaSTomohiro Kusumi /*
138b7c11cdaSTomohiro Kusumi  * New module handle routine
139b7c11cdaSTomohiro Kusumi  */
140ff56536eSAlex Hornung static int
dm_modcmd(module_t mod,int cmd,void * unused)1415b279a20SAlex Hornung dm_modcmd(module_t mod, int cmd, void *unused)
142ff56536eSAlex Hornung {
1433d089c23SSascha Wildner 	int error;
144ff56536eSAlex Hornung 
145ff56536eSAlex Hornung 	error = 0;
146ff56536eSAlex Hornung 
147ff56536eSAlex Hornung 	switch (cmd) {
1485b279a20SAlex Hornung 	case MOD_LOAD:
149ff56536eSAlex Hornung 		dm_doinit();
150e8e2bcdaSAlex Hornung 		kprintf("Device Mapper version %d.%d.%d loaded\n",
1515b279a20SAlex Hornung 		    DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL);
152ff56536eSAlex Hornung 		break;
153ff56536eSAlex Hornung 
1545b279a20SAlex Hornung 	case MOD_UNLOAD:
155ff56536eSAlex Hornung 		/*
156ff56536eSAlex Hornung 		 * Disable unloading of dm module if there are any devices
157ff56536eSAlex Hornung 		 * defined in driver. This is probably too strong we need
158ff56536eSAlex Hornung 		 * to disable auto-unload only if there is mounted dm device
159ff56536eSAlex Hornung 		 * present.
160ff56536eSAlex Hornung 		 */
161ff56536eSAlex Hornung 		if (dm_dev_counter > 0)
162ff56536eSAlex Hornung 			return EBUSY;
16353a07f3aSTomohiro Kusumi 		/* race window here */
164ff56536eSAlex Hornung 
165ff56536eSAlex Hornung 		error = dmdestroy();
166ff56536eSAlex Hornung 		if (error)
167ff56536eSAlex Hornung 			break;
168e8e2bcdaSAlex Hornung 		kprintf("Device Mapper unloaded\n");
169ff56536eSAlex Hornung 		break;
170ff56536eSAlex Hornung 	}
171ff56536eSAlex Hornung 
172ff56536eSAlex Hornung 	return error;
173ff56536eSAlex Hornung }
174ff56536eSAlex Hornung 
175ff56536eSAlex Hornung static void
dm_doinit(void)176ff56536eSAlex Hornung dm_doinit(void)
177ff56536eSAlex Hornung {
178ff56536eSAlex Hornung 	dm_target_init();
179ff56536eSAlex Hornung 	dm_dev_init();
180ff56536eSAlex Hornung 	dm_pdev_init();
181963efb75STomohiro Kusumi 	dmcdev = make_dev(&dmctl_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control");
182ff56536eSAlex Hornung }
183ff56536eSAlex Hornung 
184b7c11cdaSTomohiro Kusumi /*
185b7c11cdaSTomohiro Kusumi  * Destroy routine
186b7c11cdaSTomohiro Kusumi  */
187ff56536eSAlex Hornung static int
dmdestroy(void)188ff56536eSAlex Hornung dmdestroy(void)
189ff56536eSAlex Hornung {
1905b279a20SAlex Hornung 	destroy_dev(dmcdev);
191ff56536eSAlex Hornung 
1929fada28aSAlex Hornung 	dm_dev_uninit();
1939fada28aSAlex Hornung 	dm_pdev_uninit();
1949fada28aSAlex Hornung 	dm_target_uninit();
195ff56536eSAlex Hornung 
196ff56536eSAlex Hornung 	return 0;
197ff56536eSAlex Hornung }
198ff56536eSAlex Hornung 
199ff56536eSAlex Hornung static int
dmopen(struct dev_open_args * ap)2005b279a20SAlex Hornung dmopen(struct dev_open_args *ap)
201ff56536eSAlex Hornung {
202956c8d71SAlex Hornung 	cdev_t dev = ap->a_head.a_dev;
203956c8d71SAlex Hornung 	dm_dev_t *dmv;
204956c8d71SAlex Hornung 
205956c8d71SAlex Hornung 	/* Shortcut for the control device */
206956c8d71SAlex Hornung 	if (minor(dev) == 0)
207956c8d71SAlex Hornung 		return 0;
208956c8d71SAlex Hornung 
209956c8d71SAlex Hornung 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
210956c8d71SAlex Hornung 		return ENXIO;
211956c8d71SAlex Hornung 
212956c8d71SAlex Hornung 	dmv->is_open = 1;
213956c8d71SAlex Hornung 	dm_dev_unbusy(dmv);
214ff56536eSAlex Hornung 
21528d082ddSTomohiro Kusumi 	dmdebug("minor=%" PRIu32 "\n", minor(ap->a_head.a_dev));
216ff56536eSAlex Hornung 	return 0;
217ff56536eSAlex Hornung }
218ff56536eSAlex Hornung 
219ff56536eSAlex Hornung static int
dmclose(struct dev_close_args * ap)2205b279a20SAlex Hornung dmclose(struct dev_close_args *ap)
221ff56536eSAlex Hornung {
222956c8d71SAlex Hornung 	cdev_t dev = ap->a_head.a_dev;
223956c8d71SAlex Hornung 	dm_dev_t *dmv;
224956c8d71SAlex Hornung 
225956c8d71SAlex Hornung 	/* Shortcut for the control device */
226956c8d71SAlex Hornung 	if (minor(dev) == 0)
227956c8d71SAlex Hornung 		return 0;
228956c8d71SAlex Hornung 
229956c8d71SAlex Hornung 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
230956c8d71SAlex Hornung 		return ENXIO;
231956c8d71SAlex Hornung 
232956c8d71SAlex Hornung 	dmv->is_open = 0;
233956c8d71SAlex Hornung 	dm_dev_unbusy(dmv);
234ff56536eSAlex Hornung 
23528d082ddSTomohiro Kusumi 	dmdebug("minor=%" PRIu32 "\n", minor(ap->a_head.a_dev));
236ff56536eSAlex Hornung 	return 0;
237ff56536eSAlex Hornung }
238ff56536eSAlex Hornung 
239ff56536eSAlex Hornung 
240ff56536eSAlex Hornung static int
dmioctl(struct dev_ioctl_args * ap)2415b279a20SAlex Hornung dmioctl(struct dev_ioctl_args *ap)
242ff56536eSAlex Hornung {
2435b279a20SAlex Hornung 	cdev_t dev = ap->a_head.a_dev;
2445b279a20SAlex Hornung 	u_long cmd = ap->a_cmd;
2455b279a20SAlex Hornung 	void *data = ap->a_data;
246b7c11cdaSTomohiro Kusumi 	struct plistref *pref;
2475b279a20SAlex Hornung 
248956c8d71SAlex Hornung 	int r, err;
249ff56536eSAlex Hornung 	prop_dictionary_t dm_dict_in;
250ff56536eSAlex Hornung 
251956c8d71SAlex Hornung 	err = r = 0;
2525b279a20SAlex Hornung 	KKASSERT(data != NULL);
253ff56536eSAlex Hornung 
254033bbf9eSTomohiro Kusumi 	if ((r = disk_ioctl_switch(dev, cmd, data)) != ENOTTY)
255033bbf9eSTomohiro Kusumi 		return r;  /* Handled disk ioctl */
256ff56536eSAlex Hornung 
257*9d275733STomohiro Kusumi 	switch(cmd) {
258*9d275733STomohiro Kusumi 	case NETBSD_DM_IOCTL:
259*9d275733STomohiro Kusumi 		dmdebug("NETBSD_DM_IOCTL called\n");
260*9d275733STomohiro Kusumi 		break;
261*9d275733STomohiro Kusumi 	default:
262*9d275733STomohiro Kusumi 		dmdebug("Unknown ioctl %lu called\n", cmd);
263*9d275733STomohiro Kusumi 		return ENOTTY;
264*9d275733STomohiro Kusumi 	}
265ff56536eSAlex Hornung 
266b7c11cdaSTomohiro Kusumi 	pref = (struct plistref *)data;  /* data is for libprop */
267ff56536eSAlex Hornung 	if ((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
268ff56536eSAlex Hornung 		return r;
269ff56536eSAlex Hornung 
270033bbf9eSTomohiro Kusumi 	if ((r = dm_check_version(dm_dict_in)) == 0)
271033bbf9eSTomohiro Kusumi 		err = dm_cmd_to_fun(dm_dict_in);
272ff56536eSAlex Hornung 
273ff56536eSAlex Hornung 	r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
274ff56536eSAlex Hornung 	prop_object_release(dm_dict_in);
275ff56536eSAlex Hornung 
276033bbf9eSTomohiro Kusumi 	/* Return the dm ioctl error if any. */
277033bbf9eSTomohiro Kusumi 	if (err)
278033bbf9eSTomohiro Kusumi 		return err;
279033bbf9eSTomohiro Kusumi 	return r;
280ff56536eSAlex Hornung }
281ff56536eSAlex Hornung 
282ff56536eSAlex Hornung /*
283ff56536eSAlex Hornung  * Translate command sent from libdevmapper to func.
284ff56536eSAlex Hornung  */
285ff56536eSAlex Hornung static int
dm_cmd_to_fun(prop_dictionary_t dm_dict)28678869e36STomohiro Kusumi dm_cmd_to_fun(prop_dictionary_t dm_dict)
28778869e36STomohiro Kusumi {
288f9751b33STomohiro Kusumi 	int i, size;
289ff56536eSAlex Hornung 	prop_string_t command;
290f9751b33STomohiro Kusumi 	struct cmd_function *p = NULL;
291ff56536eSAlex Hornung 
292f9751b33STomohiro Kusumi 	size = sizeof(cmd_fn) / sizeof(cmd_fn[0]);
293ff56536eSAlex Hornung 
294ff56536eSAlex Hornung 	if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
295ff56536eSAlex Hornung 		return EINVAL;
296ff56536eSAlex Hornung 
297f9751b33STomohiro Kusumi 	for (i = 0; i < size; i++) {
298f9751b33STomohiro Kusumi 		p = &cmd_fn[i];
299f9751b33STomohiro Kusumi 		if (prop_string_equals_cstring(command, p->cmd))
300ff56536eSAlex Hornung 			break;
301f9751b33STomohiro Kusumi 	}
302ff56536eSAlex Hornung 
303f9751b33STomohiro Kusumi 	KKASSERT(p);
304f9751b33STomohiro Kusumi 	if (i == size) {
30526798264STomohiro Kusumi 		dmdebug("Unknown ioctl\n");
306ff56536eSAlex Hornung 		return EINVAL;
307f9751b33STomohiro Kusumi 	}
308ff56536eSAlex Hornung 
30926798264STomohiro Kusumi 	dmdebug("ioctl %s called %p\n", p->cmd, p->fn);
310f9751b33STomohiro Kusumi 	if (p->fn == NULL)
311dddde3e1STomohiro Kusumi 		return 0;  /* No handler required */
312ff56536eSAlex Hornung 
313f9751b33STomohiro Kusumi 	return p->fn(dm_dict);
314ff56536eSAlex Hornung }
315ff56536eSAlex Hornung 
316b7c11cdaSTomohiro Kusumi /*
317ff56536eSAlex Hornung  * Check for disk specific ioctls.
318ff56536eSAlex Hornung  */
319ff56536eSAlex Hornung static int
disk_ioctl_switch(cdev_t dev,u_long cmd,void * data)3205b279a20SAlex Hornung disk_ioctl_switch(cdev_t dev, u_long cmd, void *data)
321ff56536eSAlex Hornung {
322ff56536eSAlex Hornung 	dm_dev_t *dmv;
323ff56536eSAlex Hornung 
324ff56536eSAlex Hornung 	/* disk ioctls make sense only on block devices */
325ff56536eSAlex Hornung 	if (minor(dev) == 0)
326ff56536eSAlex Hornung 		return ENOTTY;
327ff56536eSAlex Hornung 
328ff56536eSAlex Hornung 	switch(cmd) {
3295b279a20SAlex Hornung 	case DIOCGPART:
330ba65987cSAlex Hornung 		if ((dmv = dev->si_drv1) == NULL)
331ff56536eSAlex Hornung 			return ENODEV;
3325b279a20SAlex Hornung 		if (dmv->diskp->d_info.d_media_blksize == 0) {
333ff56536eSAlex Hornung 			return ENOTSUP;
3345b279a20SAlex Hornung 		} else {
335cec31096STomohiro Kusumi 			u_int64_t size;
336cec31096STomohiro Kusumi 			struct partinfo *dpart = data;
337cec31096STomohiro Kusumi 			bzero(dpart, sizeof(*dpart));
338cec31096STomohiro Kusumi 
3395b279a20SAlex Hornung 			size = dm_table_size(&dmv->table_head);
3405b279a20SAlex Hornung 			dpart->media_offset  = 0;
3415b279a20SAlex Hornung 			dpart->media_size    = size * DEV_BSIZE;
3425b279a20SAlex Hornung 			dpart->media_blocks  = size;
3435b279a20SAlex Hornung 			dpart->media_blksize = DEV_BSIZE;
3445b279a20SAlex Hornung 			dpart->fstype = FS_BSDFFS;
3455b279a20SAlex Hornung 		}
34628d082ddSTomohiro Kusumi 		dmdebug("DIOCGPART called\n");
347ff56536eSAlex Hornung 		break;
348ff56536eSAlex Hornung 
349ff56536eSAlex Hornung 	default:
35028d082ddSTomohiro Kusumi 		dmdebug("Unknown disk ioctl %lu called\n", cmd);
351ff56536eSAlex Hornung 		return ENOTTY;
352ff56536eSAlex Hornung 		break; /* NOT REACHED */
353ff56536eSAlex Hornung 	}
354ff56536eSAlex Hornung 
355ff56536eSAlex Hornung 	return 0;
356ff56536eSAlex Hornung }
357ff56536eSAlex Hornung 
358ff56536eSAlex Hornung /*
359ff56536eSAlex Hornung  * Do all IO operations on dm logical devices.
360ff56536eSAlex Hornung  */
3615b279a20SAlex Hornung static int
dmstrategy(struct dev_strategy_args * ap)3625b279a20SAlex Hornung dmstrategy(struct dev_strategy_args *ap)
363ff56536eSAlex Hornung {
3645b279a20SAlex Hornung 	cdev_t dev = ap->a_head.a_dev;
36500e94364STomohiro Kusumi 	dm_dev_t *dmv = dev->si_drv1;
366ff56536eSAlex Hornung 	dm_table_t *tbl;
367ff56536eSAlex Hornung 	dm_table_entry_t *table_en;
36800e94364STomohiro Kusumi 
36900e94364STomohiro Kusumi 	struct bio *bio = ap->a_bio;
37000e94364STomohiro Kusumi 	struct buf *bp = bio->bio_buf;
371ff56536eSAlex Hornung 	struct buf *nestbuf;
372ff56536eSAlex Hornung 
373ff56536eSAlex Hornung 	uint64_t buf_start, buf_len, issued_len;
374ff56536eSAlex Hornung 	uint64_t table_start, table_end;
375ff56536eSAlex Hornung 	uint64_t start, end;
37600e94364STomohiro Kusumi 	int bypass;
377ff56536eSAlex Hornung 
3785b279a20SAlex Hornung 	buf_start = bio->bio_offset;
379ff56536eSAlex Hornung 	buf_len = bp->b_bcount;
380ff56536eSAlex Hornung 	issued_len = 0;
381ff56536eSAlex Hornung 
3823adc52bcSMatthew Dillon 	switch(bp->b_cmd) {
3833adc52bcSMatthew Dillon 	case BUF_CMD_READ:
3843adc52bcSMatthew Dillon 	case BUF_CMD_WRITE:
3853adc52bcSMatthew Dillon 	case BUF_CMD_FREEBLKS:
3863adc52bcSMatthew Dillon 		bypass = 0;
3873adc52bcSMatthew Dillon 		break;
3883adc52bcSMatthew Dillon 	case BUF_CMD_FLUSH:
3893adc52bcSMatthew Dillon 		bypass = 1;
3903adc52bcSMatthew Dillon 		KKASSERT(buf_len == 0);
3913adc52bcSMatthew Dillon 		break;
3923adc52bcSMatthew Dillon 	default:
3933adc52bcSMatthew Dillon 		bp->b_error = EIO;
3943adc52bcSMatthew Dillon 		bp->b_resid = bp->b_bcount;
3953adc52bcSMatthew Dillon 		biodone(bio);
3963adc52bcSMatthew Dillon 		return 0;
3973adc52bcSMatthew Dillon 	}
3983adc52bcSMatthew Dillon 
3993adc52bcSMatthew Dillon 	if (bypass == 0 &&
4003adc52bcSMatthew Dillon 	    bounds_check_with_mediasize(bio, DEV_BSIZE,
401ff56536eSAlex Hornung 					dm_table_size(&dmv->table_head)) <= 0) {
402ff56536eSAlex Hornung 		bp->b_resid = bp->b_bcount;
4035b279a20SAlex Hornung 		biodone(bio);
4045b279a20SAlex Hornung 		return 0;
405ff56536eSAlex Hornung 	}
406ff56536eSAlex Hornung 
407ff56536eSAlex Hornung 	/* Select active table */
408ff56536eSAlex Hornung 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
409ff56536eSAlex Hornung 
4103adc52bcSMatthew Dillon 	nestiobuf_init(bio);
4113b48c3c1SAlex Hornung 	devstat_start_transaction(&dmv->stats);
412ff56536eSAlex Hornung 
413ff56536eSAlex Hornung 	/*
414ff56536eSAlex Hornung 	 * Find out what tables I want to select.
415ff56536eSAlex Hornung 	 */
4163cd1dc08STomohiro Kusumi 	TAILQ_FOREACH(table_en, tbl, next) {
417ff56536eSAlex Hornung 		/*
4183adc52bcSMatthew Dillon 		 * I need need number of bytes not blocks.
419ff56536eSAlex Hornung 		 */
4203adc52bcSMatthew Dillon 		table_start = table_en->start * DEV_BSIZE;
4210338193eSTomohiro Kusumi 		table_end = table_start + table_en->length * DEV_BSIZE;
422ff56536eSAlex Hornung 
4233adc52bcSMatthew Dillon 		/*
4243adc52bcSMatthew Dillon 		 * Calculate the start and end
4253adc52bcSMatthew Dillon 		 */
426ff56536eSAlex Hornung 		start = MAX(table_start, buf_start);
427ff56536eSAlex Hornung 		end = MIN(table_end, buf_start + buf_len);
428ff56536eSAlex Hornung 
429a3c6b8ecSTomohiro Kusumi 		if (dm_debug_level) {
4309842ce30STomohiro Kusumi 			kprintf("----------------------------------------\n");
4319842ce30STomohiro Kusumi 			kprintf("table_start %010" PRIu64", table_end %010"
432ff56536eSAlex Hornung 			    PRIu64 "\n", table_start, table_end);
4339842ce30STomohiro Kusumi 			kprintf("buf_start %010" PRIu64", buf_len %010"
434ff56536eSAlex Hornung 			    PRIu64"\n", buf_start, buf_len);
4359842ce30STomohiro Kusumi 			kprintf("start-buf_start %010"PRIu64", end %010"
436ff56536eSAlex Hornung 			    PRIu64"\n", start - buf_start, end);
4379842ce30STomohiro Kusumi 			kprintf("start %010" PRIu64", end %010"
438ff56536eSAlex Hornung 			    PRIu64"\n", start, end);
439a3c6b8ecSTomohiro Kusumi 		}
440ff56536eSAlex Hornung 
4413adc52bcSMatthew Dillon 		if (bypass) {
4425b279a20SAlex Hornung 			nestbuf = getpbuf(NULL);
443f6221ad1SMatthew Dillon 			nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
444ff56536eSAlex Hornung 
4453b48c3c1SAlex Hornung 			nestiobuf_add(bio, nestbuf, 0, 0, &dmv->stats);
4463adc52bcSMatthew Dillon 			nestbuf->b_bio1.bio_offset = 0;
4473adc52bcSMatthew Dillon 			table_en->target->strategy(table_en, nestbuf);
4483adc52bcSMatthew Dillon 		} else if (start < end) {
4493adc52bcSMatthew Dillon 			nestbuf = getpbuf(NULL);
450f6221ad1SMatthew Dillon 			nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
451f6221ad1SMatthew Dillon 
4523adc52bcSMatthew Dillon 			nestiobuf_add(bio, nestbuf,
45300e94364STomohiro Kusumi 				      start - buf_start, end - start,
4543b48c3c1SAlex Hornung 				      &dmv->stats);
455ff56536eSAlex Hornung 			issued_len += end - start;
456ff56536eSAlex Hornung 
45700e94364STomohiro Kusumi 			nestbuf->b_bio1.bio_offset = start - table_start;
458ff56536eSAlex Hornung 			table_en->target->strategy(table_en, nestbuf);
459ff56536eSAlex Hornung 		}
460ff56536eSAlex Hornung 	}
461ff56536eSAlex Hornung 
462ff56536eSAlex Hornung 	if (issued_len < buf_len)
4633adc52bcSMatthew Dillon 		nestiobuf_error(bio, EINVAL);
4643adc52bcSMatthew Dillon 	nestiobuf_start(bio);
465ff56536eSAlex Hornung 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
466ff56536eSAlex Hornung 
4675b279a20SAlex Hornung 	return 0;
468ff56536eSAlex Hornung }
469ff56536eSAlex Hornung 
470ff56536eSAlex Hornung static int
dmdump(struct dev_dump_args * ap)47179b7159fSAlex Hornung dmdump(struct dev_dump_args *ap)
47279b7159fSAlex Hornung {
47379b7159fSAlex Hornung 	cdev_t dev = ap->a_head.a_dev;
47400e94364STomohiro Kusumi 	dm_dev_t *dmv = dev->si_drv1;
47579b7159fSAlex Hornung 	dm_table_t *tbl;
47679b7159fSAlex Hornung 	dm_table_entry_t *table_en;
47700e94364STomohiro Kusumi 
47879b7159fSAlex Hornung 	uint64_t buf_start, buf_len, issued_len;
47979b7159fSAlex Hornung 	uint64_t table_start, table_end;
48000e94364STomohiro Kusumi 	uint64_t start, end;
48179b7159fSAlex Hornung 	int error = 0;
48279b7159fSAlex Hornung 
48379b7159fSAlex Hornung 	buf_start = ap->a_offset;
48479b7159fSAlex Hornung 	buf_len = ap->a_length;
48579b7159fSAlex Hornung 	issued_len = 0;
48679b7159fSAlex Hornung 
48779b7159fSAlex Hornung 	/* Select active table */
48879b7159fSAlex Hornung 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
48979b7159fSAlex Hornung 
49079b7159fSAlex Hornung 	/*
49179b7159fSAlex Hornung 	 * Find out what tables I want to select.
49279b7159fSAlex Hornung 	 */
4933cd1dc08STomohiro Kusumi 	TAILQ_FOREACH(table_en, tbl, next) {
49479b7159fSAlex Hornung 		/*
49579b7159fSAlex Hornung 		 * I need need number of bytes not blocks.
49679b7159fSAlex Hornung 		 */
49779b7159fSAlex Hornung 		table_start = table_en->start * DEV_BSIZE;
4980338193eSTomohiro Kusumi 		table_end = table_start + table_en->length * DEV_BSIZE;
49979b7159fSAlex Hornung 
50079b7159fSAlex Hornung 		/*
50179b7159fSAlex Hornung 		 * Calculate the start and end
50279b7159fSAlex Hornung 		 */
50379b7159fSAlex Hornung 		start = MAX(table_start, buf_start);
50479b7159fSAlex Hornung 		end = MIN(table_end, buf_start + buf_len);
50579b7159fSAlex Hornung 
50679b7159fSAlex Hornung 		if (ap->a_length == 0) {
50779b7159fSAlex Hornung 			if (table_en->target->dump == NULL) {
50879b7159fSAlex Hornung 				error = ENXIO;
50979b7159fSAlex Hornung 				goto out;
51079b7159fSAlex Hornung 			}
51179b7159fSAlex Hornung 
51279b7159fSAlex Hornung 			table_en->target->dump(table_en, NULL, 0, 0);
51379b7159fSAlex Hornung 		} else if (start < end) {
51479b7159fSAlex Hornung 			if (table_en->target->dump == NULL) {
51579b7159fSAlex Hornung 				error = ENXIO;
51679b7159fSAlex Hornung 				goto out;
51779b7159fSAlex Hornung 			}
51879b7159fSAlex Hornung 
51979b7159fSAlex Hornung 			table_en->target->dump(table_en,
52000e94364STomohiro Kusumi 			    (char *)ap->a_virtual + start - buf_start,
52100e94364STomohiro Kusumi 			    end - start, start - table_start);
52279b7159fSAlex Hornung 
52379b7159fSAlex Hornung 			issued_len += end - start;
52479b7159fSAlex Hornung 		}
52579b7159fSAlex Hornung 	}
52679b7159fSAlex Hornung 
52779b7159fSAlex Hornung 	if (issued_len < buf_len)
52879b7159fSAlex Hornung 		error = EINVAL;
52979b7159fSAlex Hornung 
53079b7159fSAlex Hornung out:
53179b7159fSAlex Hornung 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
53279b7159fSAlex Hornung 
53379b7159fSAlex Hornung 	return error;
53479b7159fSAlex Hornung }
53579b7159fSAlex Hornung 
53679b7159fSAlex Hornung static int
dmsize(struct dev_psize_args * ap)5375b279a20SAlex Hornung dmsize(struct dev_psize_args *ap)
538ff56536eSAlex Hornung {
5395b279a20SAlex Hornung 	cdev_t dev = ap->a_head.a_dev;
540ff56536eSAlex Hornung 	dm_dev_t *dmv;
541ff56536eSAlex Hornung 
542ba65987cSAlex Hornung 	if ((dmv = dev->si_drv1) == NULL)
543ba65987cSAlex Hornung 		return ENXIO;
544ff56536eSAlex Hornung 
5450b985ce1STomohiro Kusumi 	ap->a_result = (int64_t)dm_table_size(&dmv->table_head);
54627d04cb3SAlex Hornung 
54727d04cb3SAlex Hornung 	return 0;
548ff56536eSAlex Hornung }
549ff56536eSAlex Hornung 
5505b279a20SAlex Hornung void
dmsetdiskinfo(struct disk * disk,dm_table_head_t * head)5515b279a20SAlex Hornung dmsetdiskinfo(struct disk *disk, dm_table_head_t *head)
5525b279a20SAlex Hornung {
5535b279a20SAlex Hornung 	struct disk_info info;
5543b48c3c1SAlex Hornung 	uint64_t dmp_size;
5555b279a20SAlex Hornung 
5565b279a20SAlex Hornung 	dmp_size = dm_table_size(head);
5575b279a20SAlex Hornung 
5585b279a20SAlex Hornung 	bzero(&info, sizeof(struct disk_info));
5595b279a20SAlex Hornung 	info.d_media_blksize = DEV_BSIZE;
5605b279a20SAlex Hornung 	info.d_media_blocks = dmp_size;
56108cba728SAlex Hornung #if 0
5623af01d56SAlex Hornung 	/* this is set by disk_setdiskinfo */
5635b279a20SAlex Hornung 	info.d_media_size = dmp_size * DEV_BSIZE;
56408cba728SAlex Hornung #endif
565c6bbf777SAlex Hornung 	info.d_dsflags = DSO_MBRQUIET | DSO_DEVICEMAPPER | DSO_RAWPSIZE;
5663af01d56SAlex Hornung 
5675b279a20SAlex Hornung 	info.d_secpertrack = 32;
5685b279a20SAlex Hornung 	info.d_nheads = 64;
5695b279a20SAlex Hornung 	info.d_secpercyl = info.d_secpertrack * info.d_nheads;
5703af01d56SAlex Hornung 	info.d_ncylinders = dmp_size / info.d_secpercyl;
57108cba728SAlex Hornung 
5728a5197ddSMatthew Dillon 	/*
5738a5197ddSMatthew Dillon 	 * The probe is asynchronous so call disk_config() to
5748a5197ddSMatthew Dillon 	 * wait for it to complete.
5758a5197ddSMatthew Dillon 	 */
57608cba728SAlex Hornung 	disk_setdiskinfo(disk, &info);
5778a5197ddSMatthew Dillon 	disk_config(NULL);
578ff56536eSAlex Hornung }
579aadb5a11SAlex Hornung 
5809fada28aSAlex Hornung /*
5819fada28aSAlex Hornung  * Transform char s to uint64_t offset number.
5829fada28aSAlex Hornung  */
5839fada28aSAlex Hornung uint64_t
atoi64(const char * s)5849fada28aSAlex Hornung atoi64(const char *s)
5859fada28aSAlex Hornung {
5869fada28aSAlex Hornung 	uint64_t n;
5879fada28aSAlex Hornung 	n = 0;
5889fada28aSAlex Hornung 
5899fada28aSAlex Hornung 	while (*s != '\0') {
5909fada28aSAlex Hornung 		if (!isdigit(*s))
5919fada28aSAlex Hornung 			break;
5929fada28aSAlex Hornung 
5939fada28aSAlex Hornung 		n = (10 * n) + (*s - '0');
5949fada28aSAlex Hornung 		s++;
5959fada28aSAlex Hornung 	}
5969fada28aSAlex Hornung 
5979fada28aSAlex Hornung 	return n;
5989fada28aSAlex Hornung }
5999fada28aSAlex Hornung 
6001849c72cSTomohiro Kusumi char *
dm_alloc_string(int len)6011849c72cSTomohiro Kusumi dm_alloc_string(int len)
6021849c72cSTomohiro Kusumi {
6031849c72cSTomohiro Kusumi 	if (len <= 0)
6041849c72cSTomohiro Kusumi 		len = DM_MAX_PARAMS_SIZE;
6051849c72cSTomohiro Kusumi 	return kmalloc(len, M_DM, M_WAITOK | M_ZERO);
6061849c72cSTomohiro Kusumi }
6071849c72cSTomohiro Kusumi 
6087115a22bSAlex Hornung void
dm_builtin_init(void * arg)6097115a22bSAlex Hornung dm_builtin_init(void *arg)
6107115a22bSAlex Hornung {
6117115a22bSAlex Hornung 	modeventhand_t evh = (modeventhand_t)arg;
6127115a22bSAlex Hornung 
6137115a22bSAlex Hornung 	KKASSERT(evh != NULL);
6147115a22bSAlex Hornung 	evh(NULL, MOD_LOAD, NULL);
6157115a22bSAlex Hornung }
6167115a22bSAlex Hornung 
6177115a22bSAlex Hornung void
dm_builtin_uninit(void * arg)6187115a22bSAlex Hornung dm_builtin_uninit(void *arg)
6197115a22bSAlex Hornung {
6207115a22bSAlex Hornung 	modeventhand_t evh = (modeventhand_t)arg;
6217115a22bSAlex Hornung 
6227115a22bSAlex Hornung 	KKASSERT(evh != NULL);
6237115a22bSAlex Hornung 	evh(NULL, MOD_UNLOAD, NULL);
6247115a22bSAlex Hornung }
6257115a22bSAlex Hornung 
626aadb5a11SAlex Hornung TUNABLE_INT("debug.dm_debug", &dm_debug_level);
627aadb5a11SAlex Hornung SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level,
6282250180cSSascha Wildner 	       0, "Enable device mapper debugging");
629