xref: /dflybsd-src/sys/dev/disk/dm/device-mapper.c (revision db5960361ed268edf9f80dfac2622c68e5254c0c)
1ff56536eSAlex Hornung /*        $NetBSD: device-mapper.c,v 1.22 2010/03/26 15:46:04 jakllsch Exp $ */
2ff56536eSAlex Hornung 
3ff56536eSAlex Hornung /*
4ff56536eSAlex Hornung  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5ff56536eSAlex Hornung  * All rights reserved.
6ff56536eSAlex Hornung  *
7ff56536eSAlex Hornung  * This code is derived from software contributed to The NetBSD Foundation
8ff56536eSAlex Hornung  * by Adam Hamsik.
9ff56536eSAlex Hornung  *
10ff56536eSAlex Hornung  * Redistribution and use in source and binary forms, with or without
11ff56536eSAlex Hornung  * modification, are permitted provided that the following conditions
12ff56536eSAlex Hornung  * are met:
13ff56536eSAlex Hornung  * 1. Redistributions of source code must retain the above copyright
14ff56536eSAlex Hornung  *    notice, this list of conditions and the following disclaimer.
15ff56536eSAlex Hornung  * 2. Redistributions in binary form must reproduce the above copyright
16ff56536eSAlex Hornung  *    notice, this list of conditions and the following disclaimer in the
17ff56536eSAlex Hornung  *    documentation and/or other materials provided with the distribution.
18ff56536eSAlex Hornung  *
19ff56536eSAlex Hornung  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20ff56536eSAlex Hornung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21ff56536eSAlex Hornung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22ff56536eSAlex Hornung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23ff56536eSAlex Hornung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24ff56536eSAlex Hornung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25ff56536eSAlex Hornung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26ff56536eSAlex Hornung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27ff56536eSAlex Hornung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ff56536eSAlex Hornung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29ff56536eSAlex Hornung  * POSSIBILITY OF SUCH DAMAGE.
30ff56536eSAlex Hornung  */
31ff56536eSAlex Hornung 
32ff56536eSAlex Hornung /*
33ff56536eSAlex Hornung  * I want to say thank you to all people who helped me with this project.
34ff56536eSAlex Hornung  */
35ff56536eSAlex Hornung 
36ff56536eSAlex Hornung #include <sys/types.h>
37ff56536eSAlex Hornung #include <sys/param.h>
38ff56536eSAlex Hornung 
39ff56536eSAlex Hornung #include <sys/buf.h>
40ff56536eSAlex Hornung #include <sys/conf.h>
41ff56536eSAlex Hornung #include <sys/device.h>
42*db596036SAlex Hornung #include <sys/devfs.h>
43ff56536eSAlex Hornung #include <sys/disk.h>
44ff56536eSAlex Hornung #include <sys/disklabel.h>
455b279a20SAlex Hornung #include <sys/dtype.h>
46ff56536eSAlex Hornung #include <sys/ioccom.h>
475b279a20SAlex Hornung #include <sys/malloc.h>
485b279a20SAlex Hornung #include <sys/module.h>
49aadb5a11SAlex Hornung #include <sys/sysctl.h>
50ff56536eSAlex Hornung 
51ff56536eSAlex Hornung #include "netbsd-dm.h"
52ff56536eSAlex Hornung #include "dm.h"
53ff56536eSAlex Hornung 
545b279a20SAlex Hornung static	d_ioctl_t	dmioctl;
555b279a20SAlex Hornung static	d_open_t	dmopen;
565b279a20SAlex Hornung static	d_close_t	dmclose;
575b279a20SAlex Hornung static	d_psize_t	dmsize;
585b279a20SAlex Hornung static	d_strategy_t	dmstrategy;
5979b7159fSAlex Hornung static	d_dump_t	dmdump;
60ff56536eSAlex Hornung 
61ff56536eSAlex Hornung /* attach and detach routines */
62ff56536eSAlex Hornung void dmattach(int);
635b279a20SAlex Hornung static int dm_modcmd(module_t mod, int cmd, void *unused);
64ff56536eSAlex Hornung static int dmdestroy(void);
65ff56536eSAlex Hornung 
66ff56536eSAlex Hornung static void dm_doinit(void);
67ff56536eSAlex Hornung 
68ff56536eSAlex Hornung static int dm_cmd_to_fun(prop_dictionary_t);
695b279a20SAlex Hornung static int disk_ioctl_switch(cdev_t, u_long, void *);
70ff56536eSAlex Hornung static int dm_ioctl_switch(u_long);
715b279a20SAlex Hornung #if 0
72ff56536eSAlex Hornung static void dmminphys(struct buf *);
735b279a20SAlex Hornung #endif
74ff56536eSAlex Hornung 
75*db596036SAlex Hornung struct devfs_bitmap dm_minor_bitmap;
76*db596036SAlex Hornung 
77ff56536eSAlex Hornung /* ***Variable-definitions*** */
785b279a20SAlex Hornung struct dev_ops dm_ops = {
799f889dc4SMatthew Dillon 	{ "dm", 0, D_DISK | D_MPSAFE },
80ff56536eSAlex Hornung 	.d_open		= dmopen,
81ff56536eSAlex Hornung 	.d_close	= dmclose,
825b279a20SAlex Hornung 	.d_read 	= physread,
835b279a20SAlex Hornung 	.d_write 	= physwrite,
845b279a20SAlex Hornung 	.d_ioctl	= dmioctl,
85ff56536eSAlex Hornung 	.d_strategy	= dmstrategy,
86ff56536eSAlex Hornung 	.d_psize	= dmsize,
8779b7159fSAlex Hornung 	.d_dump		= dmdump,
885b279a20SAlex Hornung /* D_DISK */
89ff56536eSAlex Hornung };
90ff56536eSAlex Hornung 
915b279a20SAlex Hornung MALLOC_DEFINE(M_DM, "dm", "Device Mapper allocations");
92ff56536eSAlex Hornung 
93aadb5a11SAlex Hornung int dm_debug_level = 0;
94aadb5a11SAlex Hornung 
95ff56536eSAlex Hornung extern uint64_t dm_dev_counter;
96ff56536eSAlex Hornung 
975b279a20SAlex Hornung static cdev_t dmcdev;
985b279a20SAlex Hornung 
995b279a20SAlex Hornung static moduledata_t dm_mod = {
1005b279a20SAlex Hornung     "dm",
1015b279a20SAlex Hornung     dm_modcmd,
1025b279a20SAlex Hornung     NULL
1035b279a20SAlex Hornung };
1045b279a20SAlex Hornung DECLARE_MODULE(dm, dm_mod, SI_SUB_RAID, SI_ORDER_ANY);
1055b279a20SAlex Hornung 
106ff56536eSAlex Hornung /*
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  *
114ff56536eSAlex Hornung  */
11561413047SAlex Hornung static struct cmd_function cmd_fn[] = {
116ff56536eSAlex Hornung 		{ .cmd = "version", .fn = dm_get_version_ioctl},
117ff56536eSAlex Hornung 		{ .cmd = "targets", .fn = dm_list_versions_ioctl},
118ff56536eSAlex Hornung 		{ .cmd = "create",  .fn = dm_dev_create_ioctl},
119ff56536eSAlex Hornung 		{ .cmd = "info",    .fn = dm_dev_status_ioctl},
120ff56536eSAlex Hornung 		{ .cmd = "mknodes", .fn = dm_dev_status_ioctl},
121ff56536eSAlex Hornung 		{ .cmd = "names",   .fn = dm_dev_list_ioctl},
122ff56536eSAlex Hornung 		{ .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
123ff56536eSAlex Hornung 		{ .cmd = "remove",  .fn = dm_dev_remove_ioctl},
124ff56536eSAlex Hornung 		{ .cmd = "rename",  .fn = dm_dev_rename_ioctl},
125ff56536eSAlex Hornung 		{ .cmd = "resume",  .fn = dm_dev_resume_ioctl},
126ff56536eSAlex Hornung 		{ .cmd = "clear",   .fn = dm_table_clear_ioctl},
127ff56536eSAlex Hornung 		{ .cmd = "deps",    .fn = dm_table_deps_ioctl},
128ff56536eSAlex Hornung 		{ .cmd = "reload",  .fn = dm_table_load_ioctl},
129ff56536eSAlex Hornung 		{ .cmd = "status",  .fn = dm_table_status_ioctl},
130ff56536eSAlex Hornung 		{ .cmd = "table",   .fn = dm_table_status_ioctl},
131ff56536eSAlex Hornung 		{NULL, NULL}
132ff56536eSAlex Hornung };
133ff56536eSAlex Hornung 
134ff56536eSAlex Hornung /* New module handle routine */
135ff56536eSAlex Hornung static int
1365b279a20SAlex Hornung dm_modcmd(module_t mod, int cmd, void *unused)
137ff56536eSAlex Hornung {
138ff56536eSAlex Hornung 	int error, bmajor, cmajor;
139ff56536eSAlex Hornung 
140ff56536eSAlex Hornung 	error = 0;
141ff56536eSAlex Hornung 	bmajor = -1;
142ff56536eSAlex Hornung 	cmajor = -1;
143ff56536eSAlex Hornung 
144ff56536eSAlex Hornung 	switch (cmd) {
1455b279a20SAlex Hornung 	case MOD_LOAD:
146*db596036SAlex Hornung 		devfs_clone_bitmap_init(&dm_minor_bitmap);
147ff56536eSAlex Hornung 		dm_doinit();
148e8e2bcdaSAlex Hornung 		kprintf("Device Mapper version %d.%d.%d loaded\n",
1495b279a20SAlex Hornung 		    DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL);
150ff56536eSAlex Hornung 		break;
151ff56536eSAlex Hornung 
1525b279a20SAlex Hornung 	case MOD_UNLOAD:
153ff56536eSAlex Hornung 		/*
154ff56536eSAlex Hornung 		 * Disable unloading of dm module if there are any devices
155ff56536eSAlex Hornung 		 * defined in driver. This is probably too strong we need
156ff56536eSAlex Hornung 		 * to disable auto-unload only if there is mounted dm device
157ff56536eSAlex Hornung 		 * present.
158ff56536eSAlex Hornung 		 */
159ff56536eSAlex Hornung 		if (dm_dev_counter > 0)
160ff56536eSAlex Hornung 			return EBUSY;
161ff56536eSAlex Hornung 
162ff56536eSAlex Hornung 		error = dmdestroy();
163ff56536eSAlex Hornung 		if (error)
164ff56536eSAlex Hornung 			break;
165e8e2bcdaSAlex Hornung 		kprintf("Device Mapper unloaded\n");
166ff56536eSAlex Hornung 		break;
167ff56536eSAlex Hornung 
168ff56536eSAlex Hornung 	default:
1695b279a20SAlex Hornung 		break;
170ff56536eSAlex Hornung 	}
171ff56536eSAlex Hornung 
172ff56536eSAlex Hornung 	return error;
173ff56536eSAlex Hornung }
174ff56536eSAlex Hornung 
175ff56536eSAlex Hornung /*
176ff56536eSAlex Hornung  * dm_detach:
177ff56536eSAlex Hornung  *
178ff56536eSAlex Hornung  *	Autoconfiguration detach function for pseudo-device glue.
179ff56536eSAlex Hornung  * This routine is called by dm_ioctl::dm_dev_remove_ioctl and by autoconf to
180ff56536eSAlex Hornung  * remove devices created in device-mapper.
181ff56536eSAlex Hornung  */
1825b279a20SAlex Hornung int
1835b279a20SAlex Hornung dm_detach(dm_dev_t *dmv)
184ff56536eSAlex Hornung {
185*db596036SAlex Hornung 	int minor;
186*db596036SAlex Hornung 
1875b279a20SAlex Hornung 	disable_dev(dmv);
188ff56536eSAlex Hornung 
189ff56536eSAlex Hornung 	/* Destroy active table first.  */
190ff56536eSAlex Hornung 	dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
191ff56536eSAlex Hornung 
192ff56536eSAlex Hornung 	/* Destroy inactive table if exits, too. */
193ff56536eSAlex Hornung 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
194ff56536eSAlex Hornung 
195ff56536eSAlex Hornung 	dm_table_head_destroy(&dmv->table_head);
196ff56536eSAlex Hornung 
197*db596036SAlex Hornung 	minor = dkunit(dmv->devt);
19808cba728SAlex Hornung 	disk_destroy(dmv->diskp);
1993b48c3c1SAlex Hornung 	devstat_remove_entry(&dmv->stats);
200*db596036SAlex Hornung 	devfs_clone_bitmap_put(&dm_minor_bitmap, minor);
201ff56536eSAlex Hornung 
202ff56536eSAlex Hornung 	/* Destroy device */
203ff56536eSAlex Hornung 	(void)dm_dev_free(dmv);
204ff56536eSAlex Hornung 
205ff56536eSAlex Hornung 	/* Decrement device counter After removing device */
2065b279a20SAlex Hornung 	--dm_dev_counter; /* XXX: was atomic 64 */
207ff56536eSAlex Hornung 
208ff56536eSAlex Hornung 	return 0;
209ff56536eSAlex Hornung }
210ff56536eSAlex Hornung 
211ff56536eSAlex Hornung static void
212ff56536eSAlex Hornung dm_doinit(void)
213ff56536eSAlex Hornung {
214ff56536eSAlex Hornung 	dm_target_init();
215ff56536eSAlex Hornung 	dm_dev_init();
216ff56536eSAlex Hornung 	dm_pdev_init();
2175b279a20SAlex Hornung 	dmcdev = make_dev(&dm_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control");
218ff56536eSAlex Hornung }
219ff56536eSAlex Hornung 
220ff56536eSAlex Hornung /* Destroy routine */
221ff56536eSAlex Hornung static int
222ff56536eSAlex Hornung dmdestroy(void)
223ff56536eSAlex Hornung {
2245b279a20SAlex Hornung 	destroy_dev(dmcdev);
225ff56536eSAlex Hornung 
226ff56536eSAlex Hornung 	dm_dev_destroy();
227ff56536eSAlex Hornung 	dm_pdev_destroy();
228ff56536eSAlex Hornung 	dm_target_destroy();
229ff56536eSAlex Hornung 
230ff56536eSAlex Hornung 	return 0;
231ff56536eSAlex Hornung }
232ff56536eSAlex Hornung 
233ff56536eSAlex Hornung static int
2345b279a20SAlex Hornung dmopen(struct dev_open_args *ap)
235ff56536eSAlex Hornung {
236ff56536eSAlex Hornung 
2375b279a20SAlex Hornung 	aprint_debug("dm open routine called %" PRIu32 "\n",
2385b279a20SAlex Hornung 	    minor(ap->a_head.a_dev));
239ff56536eSAlex Hornung 	return 0;
240ff56536eSAlex Hornung }
241ff56536eSAlex Hornung 
242ff56536eSAlex Hornung static int
2435b279a20SAlex Hornung dmclose(struct dev_close_args *ap)
244ff56536eSAlex Hornung {
245ff56536eSAlex Hornung 
2465b279a20SAlex Hornung 	aprint_debug("dm close routine called %" PRIu32 "\n",
2475b279a20SAlex Hornung 	    minor(ap->a_head.a_dev));
248ff56536eSAlex Hornung 	return 0;
249ff56536eSAlex Hornung }
250ff56536eSAlex Hornung 
251ff56536eSAlex Hornung 
252ff56536eSAlex Hornung static int
2535b279a20SAlex Hornung dmioctl(struct dev_ioctl_args *ap)
254ff56536eSAlex Hornung {
2555b279a20SAlex Hornung 	cdev_t dev = ap->a_head.a_dev;
2565b279a20SAlex Hornung 	u_long cmd = ap->a_cmd;
2575b279a20SAlex Hornung 	void *data = ap->a_data;
2585b279a20SAlex Hornung 
259ff56536eSAlex Hornung 	int r;
260ff56536eSAlex Hornung 	prop_dictionary_t dm_dict_in;
261ff56536eSAlex Hornung 
262ff56536eSAlex Hornung 	r = 0;
263ff56536eSAlex Hornung 
264ff56536eSAlex Hornung 	aprint_debug("dmioctl called\n");
265ff56536eSAlex Hornung 
2665b279a20SAlex Hornung 	KKASSERT(data != NULL);
267ff56536eSAlex Hornung 
268ff56536eSAlex Hornung 	if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) {
269ff56536eSAlex Hornung 		struct plistref *pref = (struct plistref *) data;
270ff56536eSAlex Hornung 
271ff56536eSAlex Hornung 		/* Check if we were called with NETBSD_DM_IOCTL ioctl
272ff56536eSAlex Hornung 		   otherwise quit. */
273ff56536eSAlex Hornung 		if ((r = dm_ioctl_switch(cmd)) != 0)
274ff56536eSAlex Hornung 			return r;
275ff56536eSAlex Hornung 
276ff56536eSAlex Hornung 		if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
277ff56536eSAlex Hornung 			return r;
278ff56536eSAlex Hornung 
279ff56536eSAlex Hornung 		if ((r = dm_check_version(dm_dict_in)) != 0)
280ff56536eSAlex Hornung 			goto cleanup_exit;
281ff56536eSAlex Hornung 
282ff56536eSAlex Hornung 		/* run ioctl routine */
283ff56536eSAlex Hornung 		if ((r = dm_cmd_to_fun(dm_dict_in)) != 0)
284ff56536eSAlex Hornung 			goto cleanup_exit;
285ff56536eSAlex Hornung 
286ff56536eSAlex Hornung cleanup_exit:
287ff56536eSAlex Hornung 		r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
288ff56536eSAlex Hornung 		prop_object_release(dm_dict_in);
289ff56536eSAlex Hornung 	}
290ff56536eSAlex Hornung 
291ff56536eSAlex Hornung 	return r;
292ff56536eSAlex Hornung }
293ff56536eSAlex Hornung 
294ff56536eSAlex Hornung /*
295ff56536eSAlex Hornung  * Translate command sent from libdevmapper to func.
296ff56536eSAlex Hornung  */
297ff56536eSAlex Hornung static int
298ff56536eSAlex Hornung dm_cmd_to_fun(prop_dictionary_t dm_dict){
299ff56536eSAlex Hornung 	int i, r;
300ff56536eSAlex Hornung 	prop_string_t command;
301ff56536eSAlex Hornung 
302ff56536eSAlex Hornung 	r = 0;
303ff56536eSAlex Hornung 
304ff56536eSAlex Hornung 	if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
305ff56536eSAlex Hornung 		return EINVAL;
306ff56536eSAlex Hornung 
307ff56536eSAlex Hornung 	for(i = 0; cmd_fn[i].cmd != NULL; i++)
308ff56536eSAlex Hornung 		if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
309ff56536eSAlex Hornung 			break;
310ff56536eSAlex Hornung 
311ff56536eSAlex Hornung 	if (cmd_fn[i].cmd == NULL)
312ff56536eSAlex Hornung 		return EINVAL;
313ff56536eSAlex Hornung 
314ff56536eSAlex Hornung 	aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
315ff56536eSAlex Hornung 	r = cmd_fn[i].fn(dm_dict);
316ff56536eSAlex Hornung 
317ff56536eSAlex Hornung 	return r;
318ff56536eSAlex Hornung }
319ff56536eSAlex Hornung 
320ff56536eSAlex Hornung /* Call apropriate ioctl handler function. */
321ff56536eSAlex Hornung static int
322ff56536eSAlex Hornung dm_ioctl_switch(u_long cmd)
323ff56536eSAlex Hornung {
324ff56536eSAlex Hornung 
325ff56536eSAlex Hornung 	switch(cmd) {
326ff56536eSAlex Hornung 
327ff56536eSAlex Hornung 	case NETBSD_DM_IOCTL:
328ff56536eSAlex Hornung 		aprint_debug("dm NetBSD_DM_IOCTL called\n");
329ff56536eSAlex Hornung 		break;
330ff56536eSAlex Hornung 	default:
331ff56536eSAlex Hornung 		 aprint_debug("dm unknown ioctl called\n");
332ff56536eSAlex Hornung 		 return ENOTTY;
333ff56536eSAlex Hornung 		 break; /* NOT REACHED */
334ff56536eSAlex Hornung 	}
335ff56536eSAlex Hornung 
336ff56536eSAlex Hornung 	 return 0;
337ff56536eSAlex Hornung }
338ff56536eSAlex Hornung 
339ff56536eSAlex Hornung  /*
340ff56536eSAlex Hornung   * Check for disk specific ioctls.
341ff56536eSAlex Hornung   */
342ff56536eSAlex Hornung 
343ff56536eSAlex Hornung static int
3445b279a20SAlex Hornung disk_ioctl_switch(cdev_t dev, u_long cmd, void *data)
345ff56536eSAlex Hornung {
346ff56536eSAlex Hornung 	dm_dev_t *dmv;
347ff56536eSAlex Hornung 
348ff56536eSAlex Hornung 	/* disk ioctls make sense only on block devices */
349ff56536eSAlex Hornung 	if (minor(dev) == 0)
350ff56536eSAlex Hornung 		return ENOTTY;
351ff56536eSAlex Hornung 
352ff56536eSAlex Hornung 	switch(cmd) {
3535b279a20SAlex Hornung 	case DIOCGPART:
354ff56536eSAlex Hornung 	{
3555b279a20SAlex Hornung 		struct partinfo *dpart;
3565b279a20SAlex Hornung 		u_int64_t size;
3575b279a20SAlex Hornung 		dpart = (void *)data;
3585b279a20SAlex Hornung 		bzero(dpart, sizeof(*dpart));
359ff56536eSAlex Hornung 
360ff56536eSAlex Hornung 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
361ff56536eSAlex Hornung 			return ENODEV;
3625b279a20SAlex Hornung 		if (dmv->diskp->d_info.d_media_blksize == 0) {
363ff56536eSAlex Hornung 			dm_dev_unbusy(dmv);
364ff56536eSAlex Hornung 			return ENOTSUP;
3655b279a20SAlex Hornung 		} else {
3665b279a20SAlex Hornung 			size = dm_table_size(&dmv->table_head);
3675b279a20SAlex Hornung 			dpart->media_offset  = 0;
3685b279a20SAlex Hornung 			dpart->media_size    = size * DEV_BSIZE;
3695b279a20SAlex Hornung 			dpart->media_blocks  = size;
3705b279a20SAlex Hornung 			dpart->media_blksize = DEV_BSIZE;
3715b279a20SAlex Hornung 			dpart->fstype = FS_BSDFFS;
3725b279a20SAlex Hornung 		}
373ff56536eSAlex Hornung 		dm_dev_unbusy(dmv);
374ff56536eSAlex Hornung 		break;
375ff56536eSAlex Hornung 	}
376ff56536eSAlex Hornung 
377ff56536eSAlex Hornung 	default:
378ff56536eSAlex Hornung 		aprint_debug("unknown disk_ioctl called\n");
379ff56536eSAlex Hornung 		return ENOTTY;
380ff56536eSAlex Hornung 		break; /* NOT REACHED */
381ff56536eSAlex Hornung 	}
382ff56536eSAlex Hornung 
383ff56536eSAlex Hornung 	return 0;
384ff56536eSAlex Hornung }
385ff56536eSAlex Hornung 
386ff56536eSAlex Hornung /*
387ff56536eSAlex Hornung  * Do all IO operations on dm logical devices.
388ff56536eSAlex Hornung  */
3895b279a20SAlex Hornung static int
3905b279a20SAlex Hornung dmstrategy(struct dev_strategy_args *ap)
391ff56536eSAlex Hornung {
3925b279a20SAlex Hornung 	cdev_t dev = ap->a_head.a_dev;
3935b279a20SAlex Hornung 	struct bio *bio = ap->a_bio;
3945b279a20SAlex Hornung 	struct buf *bp = bio->bio_buf;
3953adc52bcSMatthew Dillon 	int bypass;
3965b279a20SAlex Hornung 
397ff56536eSAlex Hornung 	dm_dev_t *dmv;
398ff56536eSAlex Hornung 	dm_table_t  *tbl;
399ff56536eSAlex Hornung 	dm_table_entry_t *table_en;
400ff56536eSAlex Hornung 	struct buf *nestbuf;
401ff56536eSAlex Hornung 
402ff56536eSAlex Hornung 	uint32_t dev_type;
403ff56536eSAlex Hornung 
404ff56536eSAlex Hornung 	uint64_t buf_start, buf_len, issued_len;
405ff56536eSAlex Hornung 	uint64_t table_start, table_end;
406ff56536eSAlex Hornung 	uint64_t start, end;
407ff56536eSAlex Hornung 
4085b279a20SAlex Hornung 	buf_start = bio->bio_offset;
409ff56536eSAlex Hornung 	buf_len = bp->b_bcount;
410ff56536eSAlex Hornung 
411ff56536eSAlex Hornung 	tbl = NULL;
412ff56536eSAlex Hornung 
413ff56536eSAlex Hornung 	table_end = 0;
414ff56536eSAlex Hornung 	dev_type = 0;
415ff56536eSAlex Hornung 	issued_len = 0;
416ff56536eSAlex Hornung 
417*db596036SAlex Hornung 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) {
418ff56536eSAlex Hornung 		bp->b_error = EIO;
419ff56536eSAlex Hornung 		bp->b_resid = bp->b_bcount;
4205b279a20SAlex Hornung 		biodone(bio);
4215b279a20SAlex Hornung 		return 0;
422ff56536eSAlex Hornung 	}
423ff56536eSAlex Hornung 
4243adc52bcSMatthew Dillon 	switch(bp->b_cmd) {
4253adc52bcSMatthew Dillon 	case BUF_CMD_READ:
4263adc52bcSMatthew Dillon 	case BUF_CMD_WRITE:
4273adc52bcSMatthew Dillon 	case BUF_CMD_FREEBLKS:
4283adc52bcSMatthew Dillon 		bypass = 0;
4293adc52bcSMatthew Dillon 		break;
4303adc52bcSMatthew Dillon 	case BUF_CMD_FLUSH:
4313adc52bcSMatthew Dillon 		bypass = 1;
4323adc52bcSMatthew Dillon 		KKASSERT(buf_len == 0);
4333adc52bcSMatthew Dillon 		break;
4343adc52bcSMatthew Dillon 	default:
4353adc52bcSMatthew Dillon 		dm_dev_unbusy(dmv);
4363adc52bcSMatthew Dillon 		bp->b_error = EIO;
4373adc52bcSMatthew Dillon 		bp->b_resid = bp->b_bcount;
4383adc52bcSMatthew Dillon 		biodone(bio);
4393adc52bcSMatthew Dillon 		return 0;
4403adc52bcSMatthew Dillon 	}
4413adc52bcSMatthew Dillon 
4423adc52bcSMatthew Dillon 	if (bypass == 0 &&
4433adc52bcSMatthew Dillon 	    bounds_check_with_mediasize(bio, DEV_BSIZE,
444ff56536eSAlex Hornung 					dm_table_size(&dmv->table_head)) <= 0) {
445ff56536eSAlex Hornung 		dm_dev_unbusy(dmv);
446ff56536eSAlex Hornung 		bp->b_resid = bp->b_bcount;
4475b279a20SAlex Hornung 		biodone(bio);
4485b279a20SAlex Hornung 		return 0;
449ff56536eSAlex Hornung 	}
450ff56536eSAlex Hornung 
451ff56536eSAlex Hornung 	/* Select active table */
452ff56536eSAlex Hornung 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
453ff56536eSAlex Hornung 
4543adc52bcSMatthew Dillon 	nestiobuf_init(bio);
4553b48c3c1SAlex Hornung 	devstat_start_transaction(&dmv->stats);
456ff56536eSAlex Hornung 
457ff56536eSAlex Hornung 	/*
458ff56536eSAlex Hornung 	 * Find out what tables I want to select.
459ff56536eSAlex Hornung 	 */
4603adc52bcSMatthew Dillon 	SLIST_FOREACH(table_en, tbl, next) {
461ff56536eSAlex Hornung 		/*
4623adc52bcSMatthew Dillon 		 * I need need number of bytes not blocks.
463ff56536eSAlex Hornung 		 */
4643adc52bcSMatthew Dillon 		table_start = table_en->start * DEV_BSIZE;
465ff56536eSAlex Hornung 		table_end = table_start + (table_en->length) * DEV_BSIZE;
466ff56536eSAlex Hornung 
4673adc52bcSMatthew Dillon 		/*
4683adc52bcSMatthew Dillon 		 * Calculate the start and end
4693adc52bcSMatthew Dillon 		 */
470ff56536eSAlex Hornung 		start = MAX(table_start, buf_start);
471ff56536eSAlex Hornung 		end = MIN(table_end, buf_start + buf_len);
472ff56536eSAlex Hornung 
473ff56536eSAlex Hornung 		aprint_debug("----------------------------------------\n");
474ff56536eSAlex Hornung 		aprint_debug("table_start %010" PRIu64", table_end %010"
475ff56536eSAlex Hornung 		    PRIu64 "\n", table_start, table_end);
476ff56536eSAlex Hornung 		aprint_debug("buf_start %010" PRIu64", buf_len %010"
477ff56536eSAlex Hornung 		    PRIu64"\n", buf_start, buf_len);
478ff56536eSAlex Hornung 		aprint_debug("start-buf_start %010"PRIu64", end %010"
479ff56536eSAlex Hornung 		    PRIu64"\n", start - buf_start, end);
480ff56536eSAlex Hornung 		aprint_debug("start %010" PRIu64" , end %010"
481ff56536eSAlex Hornung                     PRIu64"\n", start, end);
482ff56536eSAlex Hornung 		aprint_debug("\n----------------------------------------\n");
483ff56536eSAlex Hornung 
4843adc52bcSMatthew Dillon 		if (bypass) {
4855b279a20SAlex Hornung 			nestbuf = getpbuf(NULL);
486f6221ad1SMatthew Dillon 			nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
487ff56536eSAlex Hornung 
4883b48c3c1SAlex Hornung 			nestiobuf_add(bio, nestbuf, 0, 0, &dmv->stats);
4893adc52bcSMatthew Dillon 			nestbuf->b_bio1.bio_offset = 0;
4903adc52bcSMatthew Dillon 			table_en->target->strategy(table_en, nestbuf);
4913adc52bcSMatthew Dillon 		} else if (start < end) {
4923adc52bcSMatthew Dillon 			nestbuf = getpbuf(NULL);
493f6221ad1SMatthew Dillon 			nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
494f6221ad1SMatthew Dillon 
4953adc52bcSMatthew Dillon 			nestiobuf_add(bio, nestbuf,
4963b48c3c1SAlex Hornung 				      start - buf_start, (end - start),
4973b48c3c1SAlex Hornung 				      &dmv->stats);
498ff56536eSAlex Hornung 			issued_len += end - start;
499ff56536eSAlex Hornung 
5005b279a20SAlex Hornung 			nestbuf->b_bio1.bio_offset = (start - table_start);
501ff56536eSAlex Hornung 			table_en->target->strategy(table_en, nestbuf);
502ff56536eSAlex Hornung 		}
503ff56536eSAlex Hornung 	}
504ff56536eSAlex Hornung 
505ff56536eSAlex Hornung 	if (issued_len < buf_len)
5063adc52bcSMatthew Dillon 		nestiobuf_error(bio, EINVAL);
5073adc52bcSMatthew Dillon 	nestiobuf_start(bio);
508ff56536eSAlex Hornung 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
509ff56536eSAlex Hornung 	dm_dev_unbusy(dmv);
510ff56536eSAlex Hornung 
5115b279a20SAlex Hornung 	return 0;
512ff56536eSAlex Hornung }
513ff56536eSAlex Hornung 
514ff56536eSAlex Hornung static int
51579b7159fSAlex Hornung dmdump(struct dev_dump_args *ap)
51679b7159fSAlex Hornung {
51779b7159fSAlex Hornung 	cdev_t dev = ap->a_head.a_dev;
51879b7159fSAlex Hornung 	dm_dev_t *dmv;
51979b7159fSAlex Hornung 	dm_table_t  *tbl;
52079b7159fSAlex Hornung 	dm_table_entry_t *table_en;
52179b7159fSAlex Hornung 	uint32_t dev_type;
52279b7159fSAlex Hornung 	uint64_t buf_start, buf_len, issued_len;
52379b7159fSAlex Hornung 	uint64_t table_start, table_end;
52479b7159fSAlex Hornung 	uint64_t start, end, data_offset;
52579b7159fSAlex Hornung 	off_t offset;
52679b7159fSAlex Hornung 	size_t length;
52779b7159fSAlex Hornung 	int error = 0;
52879b7159fSAlex Hornung 
52979b7159fSAlex Hornung 	buf_start = ap->a_offset;
53079b7159fSAlex Hornung 	buf_len = ap->a_length;
53179b7159fSAlex Hornung 
53279b7159fSAlex Hornung 	tbl = NULL;
53379b7159fSAlex Hornung 
53479b7159fSAlex Hornung 	table_end = 0;
53579b7159fSAlex Hornung 	dev_type = 0;
53679b7159fSAlex Hornung 	issued_len = 0;
53779b7159fSAlex Hornung 
53879b7159fSAlex Hornung 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) {
53979b7159fSAlex Hornung 		return EIO;
54079b7159fSAlex Hornung 	}
54179b7159fSAlex Hornung 
54279b7159fSAlex Hornung 	/* Select active table */
54379b7159fSAlex Hornung 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
54479b7159fSAlex Hornung 
54579b7159fSAlex Hornung 
54679b7159fSAlex Hornung 	/*
54779b7159fSAlex Hornung 	 * Find out what tables I want to select.
54879b7159fSAlex Hornung 	 */
54979b7159fSAlex Hornung 	SLIST_FOREACH(table_en, tbl, next) {
55079b7159fSAlex Hornung 		/*
55179b7159fSAlex Hornung 		 * I need need number of bytes not blocks.
55279b7159fSAlex Hornung 		 */
55379b7159fSAlex Hornung 		table_start = table_en->start * DEV_BSIZE;
55479b7159fSAlex Hornung 		table_end = table_start + (table_en->length) * DEV_BSIZE;
55579b7159fSAlex Hornung 
55679b7159fSAlex Hornung 		/*
55779b7159fSAlex Hornung 		 * Calculate the start and end
55879b7159fSAlex Hornung 		 */
55979b7159fSAlex Hornung 		start = MAX(table_start, buf_start);
56079b7159fSAlex Hornung 		end = MIN(table_end, buf_start + buf_len);
56179b7159fSAlex Hornung 
56279b7159fSAlex Hornung 		if (ap->a_length == 0) {
56379b7159fSAlex Hornung 			if (table_en->target->dump == NULL) {
56479b7159fSAlex Hornung 				error = ENXIO;
56579b7159fSAlex Hornung 				goto out;
56679b7159fSAlex Hornung 			}
56779b7159fSAlex Hornung 
56879b7159fSAlex Hornung 			table_en->target->dump(table_en, NULL, 0, 0);
56979b7159fSAlex Hornung 		} else if (start < end) {
57079b7159fSAlex Hornung 			data_offset = start - buf_start;
57179b7159fSAlex Hornung 			offset = start - table_start;
57279b7159fSAlex Hornung 			length = end - start;
57379b7159fSAlex Hornung 
57479b7159fSAlex Hornung 			if (table_en->target->dump == NULL) {
57579b7159fSAlex Hornung 				error = ENXIO;
57679b7159fSAlex Hornung 				goto out;
57779b7159fSAlex Hornung 			}
57879b7159fSAlex Hornung 
57979b7159fSAlex Hornung 			table_en->target->dump(table_en,
58079b7159fSAlex Hornung 			    (char *)ap->a_virtual + data_offset,
58179b7159fSAlex Hornung 			    length, offset);
58279b7159fSAlex Hornung 
58379b7159fSAlex Hornung 			issued_len += end - start;
58479b7159fSAlex Hornung 		}
58579b7159fSAlex Hornung 	}
58679b7159fSAlex Hornung 
58779b7159fSAlex Hornung 	if (issued_len < buf_len)
58879b7159fSAlex Hornung 		error = EINVAL;
58979b7159fSAlex Hornung 
59079b7159fSAlex Hornung out:
59179b7159fSAlex Hornung 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
59279b7159fSAlex Hornung 	dm_dev_unbusy(dmv);
59379b7159fSAlex Hornung 
59479b7159fSAlex Hornung 	return error;
59579b7159fSAlex Hornung }
59679b7159fSAlex Hornung 
59779b7159fSAlex Hornung static int
5985b279a20SAlex Hornung dmsize(struct dev_psize_args *ap)
599ff56536eSAlex Hornung {
6005b279a20SAlex Hornung 	cdev_t dev = ap->a_head.a_dev;
601ff56536eSAlex Hornung 	dm_dev_t *dmv;
602ff56536eSAlex Hornung 	uint64_t size;
603ff56536eSAlex Hornung 
604ff56536eSAlex Hornung 	size = 0;
605ff56536eSAlex Hornung 
606ff56536eSAlex Hornung 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
60727d04cb3SAlex Hornung 			return ENOENT;
608ff56536eSAlex Hornung 
609ff56536eSAlex Hornung 	size = dm_table_size(&dmv->table_head);
610ff56536eSAlex Hornung 	dm_dev_unbusy(dmv);
611ff56536eSAlex Hornung 
61227d04cb3SAlex Hornung 	ap->a_result = (int64_t)size;
61327d04cb3SAlex Hornung 
61427d04cb3SAlex Hornung 	return 0;
615ff56536eSAlex Hornung }
616ff56536eSAlex Hornung 
6175b279a20SAlex Hornung #if 0
618ff56536eSAlex Hornung static void
619ff56536eSAlex Hornung dmminphys(struct buf *bp)
620ff56536eSAlex Hornung {
621ff56536eSAlex Hornung 
622ff56536eSAlex Hornung 	bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
623ff56536eSAlex Hornung }
6245b279a20SAlex Hornung #endif
6255b279a20SAlex Hornung 
6265b279a20SAlex Hornung void
6275b279a20SAlex Hornung dmsetdiskinfo(struct disk *disk, dm_table_head_t *head)
6285b279a20SAlex Hornung {
6295b279a20SAlex Hornung 	struct disk_info info;
6303b48c3c1SAlex Hornung 	uint64_t dmp_size;
6315b279a20SAlex Hornung 
6325b279a20SAlex Hornung 	dmp_size = dm_table_size(head);
6335b279a20SAlex Hornung 
6345b279a20SAlex Hornung 	bzero(&info, sizeof(struct disk_info));
6355b279a20SAlex Hornung 	info.d_media_blksize = DEV_BSIZE;
6365b279a20SAlex Hornung 	info.d_media_blocks = dmp_size;
63708cba728SAlex Hornung #if 0
6383af01d56SAlex Hornung 	/* this is set by disk_setdiskinfo */
6395b279a20SAlex Hornung 	info.d_media_size = dmp_size * DEV_BSIZE;
64008cba728SAlex Hornung #endif
6413af01d56SAlex Hornung 	info.d_dsflags = DSO_MBRQUIET | DSO_DEVICEMAPPER;
6423af01d56SAlex Hornung 
6435b279a20SAlex Hornung 	info.d_secpertrack = 32;
6445b279a20SAlex Hornung 	info.d_nheads = 64;
6455b279a20SAlex Hornung 	info.d_secpercyl = info.d_secpertrack * info.d_nheads;
6463af01d56SAlex Hornung 	info.d_ncylinders = dmp_size / info.d_secpercyl;
64708cba728SAlex Hornung 
64808cba728SAlex Hornung 	disk_setdiskinfo(disk, &info);
6495b279a20SAlex Hornung }
6505b279a20SAlex Hornung 
6515b279a20SAlex Hornung prop_dictionary_t
6525b279a20SAlex Hornung dmgetdiskinfo(struct disk *disk)
6535b279a20SAlex Hornung {
6545b279a20SAlex Hornung 	prop_dictionary_t disk_info, geom;
6555b279a20SAlex Hornung 	struct disk_info *pinfo;
6565b279a20SAlex Hornung 
6575b279a20SAlex Hornung 	pinfo = &disk->d_info;
6585b279a20SAlex Hornung 
6595b279a20SAlex Hornung 	disk_info = prop_dictionary_create();
6605b279a20SAlex Hornung 	geom = prop_dictionary_create();
6615b279a20SAlex Hornung 
6625b279a20SAlex Hornung 	prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI");
6635b279a20SAlex Hornung 	prop_dictionary_set_uint64(geom, "sectors-per-unit", pinfo->d_media_blocks);
6645b279a20SAlex Hornung 	prop_dictionary_set_uint32(geom, "sector-size",
6655b279a20SAlex Hornung 	    DEV_BSIZE /* XXX 512? */);
6665b279a20SAlex Hornung 	prop_dictionary_set_uint32(geom, "sectors-per-track", 32);
6675b279a20SAlex Hornung 	prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64);
6685b279a20SAlex Hornung 	prop_dictionary_set_uint32(geom, "cylinders-per-unit",
6695b279a20SAlex Hornung 	    pinfo->d_media_blocks / 2048);
6705b279a20SAlex Hornung 	prop_dictionary_set(disk_info, "geometry", geom);
6715b279a20SAlex Hornung 	prop_object_release(geom);
6725b279a20SAlex Hornung 
6735b279a20SAlex Hornung 	return disk_info;
6745b279a20SAlex Hornung }
675ff56536eSAlex Hornung 
676ff56536eSAlex Hornung void
677ff56536eSAlex Hornung dmgetproperties(struct disk *disk, dm_table_head_t *head)
678ff56536eSAlex Hornung {
6795b279a20SAlex Hornung #if 0
680ff56536eSAlex Hornung 	prop_dictionary_t disk_info, odisk_info, geom;
681ff56536eSAlex Hornung 	int dmp_size;
682ff56536eSAlex Hornung 
683ff56536eSAlex Hornung 	dmp_size = dm_table_size(head);
684ff56536eSAlex Hornung 	disk_info = prop_dictionary_create();
685ff56536eSAlex Hornung 	geom = prop_dictionary_create();
686ff56536eSAlex Hornung 
687ff56536eSAlex Hornung 	prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI");
688ff56536eSAlex Hornung 	prop_dictionary_set_uint64(geom, "sectors-per-unit", dmp_size);
689ff56536eSAlex Hornung 	prop_dictionary_set_uint32(geom, "sector-size",
690ff56536eSAlex Hornung 	    DEV_BSIZE /* XXX 512? */);
691ff56536eSAlex Hornung 	prop_dictionary_set_uint32(geom, "sectors-per-track", 32);
692ff56536eSAlex Hornung 	prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64);
693ff56536eSAlex Hornung 	prop_dictionary_set_uint32(geom, "cylinders-per-unit", dmp_size / 2048);
694ff56536eSAlex Hornung 	prop_dictionary_set(disk_info, "geometry", geom);
695ff56536eSAlex Hornung 	prop_object_release(geom);
696ff56536eSAlex Hornung 
697ff56536eSAlex Hornung 	odisk_info = disk->dk_info;
698ff56536eSAlex Hornung 	disk->dk_info = disk_info;
699ff56536eSAlex Hornung 
700ff56536eSAlex Hornung 	if (odisk_info != NULL)
701ff56536eSAlex Hornung 		prop_object_release(odisk_info);
7025b279a20SAlex Hornung #endif
703ff56536eSAlex Hornung }
704aadb5a11SAlex Hornung 
705aadb5a11SAlex Hornung TUNABLE_INT("debug.dm_debug", &dm_debug_level);
706aadb5a11SAlex Hornung SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level,
707aadb5a11SAlex Hornung 	       0, "Eanble device mapper debugging");
708aadb5a11SAlex Hornung 
709