Annotation of src/sys/dev/dm/dm_ioctl.c, revision 1.52
1.52 ! christos 1: /* $NetBSD: dm_ioctl.c,v 1.51 2021/05/07 09:53:39 hannken Exp $ */
1.2 haad 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: */
1.32 christos 31: #include <sys/cdefs.h>
1.52 ! christos 32: __KERNEL_RCSID(0, "$NetBSD: dm_ioctl.c,v 1.51 2021/05/07 09:53:39 hannken Exp $");
1.2 haad 33:
34: /*
35: * Locking is used to synchronise between ioctl calls and between dm_table's
36: * users.
37: *
38: * ioctl locking:
39: * Simple reference counting, to count users of device will be used routines
40: * dm_dev_busy/dm_dev_unbusy are used for that.
41: * dm_dev_lookup/dm_dev_rem call dm_dev_busy before return(caller is therefore
42: * holder of reference_counter last).
43: *
44: * ioctl routines which change/remove dm_dev parameters must wait on
45: * dm_dev::dev_cv and when last user will call dm_dev_unbusy they will wake
46: * up them.
47: *
48: * table_head locking:
1.20 haad 49: * To access table entries dm_table_* routines must be used.
1.2 haad 50: *
51: * dm_table_get_entry will increment table users reference
1.22 haad 52: * counter. It will return active or inactive table depends
1.2 haad 53: * on uint8_t argument.
54: *
55: * dm_table_release must be called for every table_entry from
56: * dm_table_get_entry. Between these to calls tables can'tbe switched
1.20 haad 57: * or destroyed.
1.2 haad 58: *
59: * dm_table_head_init initialize talbe_entries SLISTS and io_cv.
60: *
61: * dm_table_head_destroy destroy cv.
62: *
63: * There are two types of users for dm_table_head first type will
64: * only read list and try to do anything with it e.g. dmstrategy,
65: * dm_table_size etc. There is another user for table_head which wants
66: * to change table lists e.g. dm_dev_resume_ioctl, dm_dev_remove_ioctl,
67: * dm_table_clear_ioctl.
1.20 haad 68: *
1.2 haad 69: * NOTE: It is not allowed to call dm_table_destroy, dm_table_switch_tables
70: * with hold table reference counter. Table reference counter is hold
71: * after calling dm_table_get_entry routine. After calling this
72: * function user must call dm_table_release before any writer table
73: * operation.
74: *
75: * Example: dm_table_get_entry
76: * dm_table_destroy/dm_table_switch_tables
77: * This exaple will lead to deadlock situation because after dm_table_get_entry
78: * table reference counter is != 0 and dm_table_destroy have to wait on cv until
79: * reference counter is 0.
80: *
81: */
82:
83: #include <sys/types.h>
84: #include <sys/param.h>
1.19 haad 85: #include <sys/device.h>
1.6 haad 86: #include <sys/disk.h>
1.2 haad 87: #include <sys/disklabel.h>
88: #include <sys/kmem.h>
89: #include <sys/malloc.h>
90:
91: #include <machine/int_fmtio.h>
92:
93: #include "netbsd-dm.h"
94: #include "dm.h"
1.51 hannken 95: #include "ioconf.h"
1.2 haad 96:
1.51 hannken 97: extern struct cfattach dm_ca;
1.23 haad 98: static uint32_t sc_minor_num;
1.24 haad 99: uint32_t dm_dev_counter;
1.2 haad 100:
101: #define DM_REMOVE_FLAG(flag, name) do { \
1.47 tkusumi 102: prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
103: flag &= ~name; \
104: prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
1.20 haad 105: } while (/*CONSTCOND*/0)
1.2 haad 106:
107: #define DM_ADD_FLAG(flag, name) do { \
1.47 tkusumi 108: prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
109: flag |= name; \
110: prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
1.20 haad 111: } while (/*CONSTCOND*/0)
1.2 haad 112:
1.48 tkusumi 113: static int dm_table_deps(dm_table_entry_t *, prop_array_t);
1.41 tkusumi 114: static int dm_table_init(dm_target_t *, dm_table_entry_t *, char *);
1.2 haad 115:
116: /*
1.47 tkusumi 117: * Print flags sent to the kernel from libdevmapper.
1.2 haad 118: */
119: static int
1.43 tkusumi 120: dm_dbg_print_flags(uint32_t flags)
1.2 haad 121: {
1.46 tkusumi 122:
1.20 haad 123: aprint_debug("dbg_print --- %d\n", flags);
124:
1.2 haad 125: if (flags & DM_READONLY_FLAG)
126: aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
127:
128: if (flags & DM_SUSPEND_FLAG)
1.39 tkusumi 129: aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out\n");
1.2 haad 130:
131: if (flags & DM_PERSISTENT_DEV_FLAG)
1.39 tkusumi 132: aprint_debug("dbg_flags: DM_PERSISTENT_DEV_FLAG set In\n");
1.2 haad 133:
134: if (flags & DM_STATUS_TABLE_FLAG)
135: aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
136:
137: if (flags & DM_ACTIVE_PRESENT_FLAG)
138: aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
139:
140: if (flags & DM_INACTIVE_PRESENT_FLAG)
141: aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
142:
143: if (flags & DM_BUFFER_FULL_FLAG)
144: aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
145:
146: if (flags & DM_SKIP_BDGET_FLAG)
147: aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
148:
149: if (flags & DM_SKIP_LOCKFS_FLAG)
150: aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
151:
152: if (flags & DM_NOFLUSH_FLAG)
153: aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
1.20 haad 154:
1.2 haad 155: return 0;
156: }
1.32 christos 157:
1.2 haad 158: /*
159: * Get list of all available targets from global
160: * target list and sent them back to libdevmapper.
161: */
162: int
163: dm_list_versions_ioctl(prop_dictionary_t dm_dict)
164: {
165: prop_array_t target_list;
166: uint32_t flags;
167:
168: flags = 0;
169:
170: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
1.20 haad 171:
1.2 haad 172: dm_dbg_print_flags(flags);
173: target_list = dm_target_prop_list();
174:
175: prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
1.20 haad 176: prop_object_release(target_list);
1.2 haad 177:
178: return 0;
179: }
1.32 christos 180:
1.2 haad 181: /*
182: * Create in-kernel entry for device. Device attributes such as name, uuid are
183: * taken from proplib dictionary.
184: */
185: int
186: dm_dev_create_ioctl(prop_dictionary_t dm_dict)
187: {
188: dm_dev_t *dmv;
189: const char *name, *uuid;
1.29 justin 190: int r;
191: uint32_t flags;
1.19 haad 192: device_t devt;
1.51 hannken 193: cfdata_t cf;
1.20 haad 194:
1.2 haad 195: flags = 0;
1.20 haad 196: name = NULL;
1.2 haad 197: uuid = NULL;
198:
199: /* Get needed values from dictionary. */
1.50 thorpej 200: prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
201: prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
1.2 haad 202: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
1.20 haad 203:
1.2 haad 204: dm_dbg_print_flags(flags);
205:
206: /* Lookup name and uuid if device already exist quit. */
207: if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
1.20 haad 208: DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
1.2 haad 209: dm_dev_unbusy(dmv);
210: return EEXIST;
211: }
1.51 hannken 212: cf = kmem_alloc(sizeof(*cf), KM_SLEEP);
213: cf->cf_name = dm_cd.cd_name;
214: cf->cf_atname = dm_ca.ca_name;
215: cf->cf_unit = atomic_inc_32_nv(&sc_minor_num);
216: cf->cf_fstate = FSTATE_NOTFOUND;
217: if ((devt = config_attach_pseudo(cf)) == NULL) {
218: kmem_free(cf, sizeof(*cf));
1.19 haad 219: aprint_error("Unable to attach pseudo device dm/%s\n", name);
220: return (ENOMEM);
221: }
1.2 haad 222: if ((dmv = dm_dev_alloc()) == NULL)
223: return ENOMEM;
1.19 haad 224:
1.2 haad 225: if (uuid)
226: strncpy(dmv->uuid, uuid, DM_UUID_LEN);
1.20 haad 227: else
1.2 haad 228: dmv->uuid[0] = '\0';
1.20 haad 229:
1.2 haad 230: if (name)
231: strlcpy(dmv->name, name, DM_NAME_LEN);
232:
1.51 hannken 233: dmv->minor = cf->cf_unit;
1.20 haad 234: dmv->flags = 0; /* device flags are set when needed */
1.2 haad 235: dmv->ref_cnt = 0;
236: dmv->event_nr = 0;
1.19 haad 237: dmv->devt = devt;
1.20 haad 238:
1.19 haad 239: dm_table_head_init(&dmv->table_head);
240:
1.2 haad 241: mutex_init(&dmv->dev_mtx, MUTEX_DEFAULT, IPL_NONE);
1.18 haad 242: mutex_init(&dmv->diskp_mtx, MUTEX_DEFAULT, IPL_NONE);
1.2 haad 243: cv_init(&dmv->dev_cv, "dm_dev");
244:
245: if (flags & DM_READONLY_FLAG)
246: dmv->flags |= DM_READONLY_FLAG;
247:
248: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
1.6 haad 249:
1.10 haad 250: disk_init(dmv->diskp, dmv->name, &dmdkdriver);
251: disk_attach(dmv->diskp);
1.13 haad 252:
253: dmv->diskp->dk_info = NULL;
1.20 haad 254:
255: if ((r = dm_dev_insert(dmv)) != 0)
1.2 haad 256: dm_dev_free(dmv);
1.20 haad 257:
1.2 haad 258: DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
259: DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
1.6 haad 260:
261: /* Increment device counter After creating device */
1.24 haad 262: atomic_inc_32(&dm_dev_counter);
1.20 haad 263:
1.2 haad 264: return r;
265: }
1.32 christos 266:
1.2 haad 267: /*
268: * Get list of created device-mapper devices fromglobal list and
269: * send it to kernel.
270: *
271: * Output dictionary:
272: *
273: * <key>cmd_data</key>
274: * <array>
275: * <dict>
276: * <key>name<key>
277: * <string>...</string>
278: *
279: * <key>dev</key>
280: * <integer>...</integer>
281: * </dict>
282: * </array>
283: */
284: int
285: dm_dev_list_ioctl(prop_dictionary_t dm_dict)
286: {
287: prop_array_t dev_list;
288: uint32_t flags;
1.20 haad 289:
1.2 haad 290: flags = 0;
1.20 haad 291:
1.2 haad 292: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
1.20 haad 293:
1.2 haad 294: dm_dbg_print_flags(flags);
1.20 haad 295:
1.2 haad 296: dev_list = dm_dev_prop_list();
297:
298: prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
299: prop_object_release(dev_list);
1.20 haad 300:
1.2 haad 301: return 0;
302: }
1.32 christos 303:
1.2 haad 304: /*
305: * Rename selected devices old name is in struct dm_ioctl.
306: * newname is taken from dictionary
307: *
308: * <key>cmd_data</key>
309: * <array>
310: * <string>...</string>
311: * </array>
312: */
313: int
314: dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
315: {
316: prop_array_t cmd_array;
317: dm_dev_t *dmv;
1.20 haad 318:
1.2 haad 319: const char *name, *uuid, *n_name;
320: uint32_t flags, minor;
321:
322: name = NULL;
323: uuid = NULL;
324: minor = 0;
1.20 haad 325:
1.2 haad 326: /* Get needed values from dictionary. */
1.50 thorpej 327: prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
328: prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
1.2 haad 329: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
330: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
331:
332: dm_dbg_print_flags(flags);
1.20 haad 333:
1.2 haad 334: cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
335:
1.50 thorpej 336: prop_array_get_string(cmd_array, 0, &n_name);
1.20 haad 337:
1.2 haad 338: if (strlen(n_name) + 1 > DM_NAME_LEN)
339: return EINVAL;
1.20 haad 340:
1.2 haad 341: if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL) {
342: DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
343: return ENOENT;
344: }
345: /* change device name */
1.20 haad 346: /*
347: * XXX How to deal with this change, name only used in
348: * dm_dev_routines, should I add dm_dev_change_name which will run
349: * under the dm_dev_list mutex ?
1.2 haad 350: */
351: strlcpy(dmv->name, n_name, DM_NAME_LEN);
1.20 haad 352:
1.2 haad 353: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
354: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
1.50 thorpej 355: prop_dictionary_set_string(dm_dict, DM_IOCTL_UUID, dmv->uuid);
1.2 haad 356:
357: dm_dev_insert(dmv);
1.20 haad 358:
1.2 haad 359: return 0;
360: }
1.32 christos 361:
1.2 haad 362: /*
363: * Remove device from global list I have to remove active
364: * and inactive tables first.
365: */
366: int
367: dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
368: {
1.51 hannken 369: int error;
370: cfdata_t cf;
1.2 haad 371: dm_dev_t *dmv;
372: const char *name, *uuid;
373: uint32_t flags, minor;
1.19 haad 374: device_t devt;
1.20 haad 375:
1.2 haad 376: flags = 0;
377: name = NULL;
378: uuid = NULL;
1.20 haad 379:
1.2 haad 380: /* Get needed values from dictionary. */
1.50 thorpej 381: prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
382: prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
1.2 haad 383: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
384: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
1.20 haad 385:
1.2 haad 386: dm_dbg_print_flags(flags);
1.19 haad 387:
1.20 haad 388: /*
389: * This seems as hack to me, probably use routine dm_dev_get_devt to
390: * atomicaly get devt from device.
391: */
1.19 haad 392: if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
1.2 haad 393: DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
394: return ENOENT;
395: }
1.19 haad 396: devt = dmv->devt;
1.2 haad 397:
1.19 haad 398: dm_dev_unbusy(dmv);
1.20 haad 399:
400: /*
401: * This will call dm_detach routine which will actually removes
402: * device.
403: */
1.51 hannken 404: cf = device_cfdata(devt);
405: error = config_detach(devt, DETACH_QUIET);
406: if (error == 0)
407: kmem_free(cf, sizeof(*cf));
408: return error;
1.2 haad 409: }
1.32 christos 410:
1.2 haad 411: /*
412: * Return actual state of device to libdevmapper.
413: */
414: int
415: dm_dev_status_ioctl(prop_dictionary_t dm_dict)
416: {
417: dm_dev_t *dmv;
418: const char *name, *uuid;
419: uint32_t flags, j, minor;
1.20 haad 420:
1.2 haad 421: name = NULL;
422: uuid = NULL;
423: flags = 0;
1.20 haad 424:
1.50 thorpej 425: prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
426: prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
1.2 haad 427: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
428: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
1.20 haad 429:
1.2 haad 430: if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
431: DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
432: return ENOENT;
433: }
1.20 haad 434: dm_dbg_print_flags(dmv->flags);
1.2 haad 435:
436: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
437: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
1.50 thorpej 438: prop_dictionary_set_string(dm_dict, DM_IOCTL_UUID, dmv->uuid);
1.2 haad 439:
440: if (dmv->flags & DM_SUSPEND_FLAG)
441: DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
442:
1.20 haad 443: /*
444: * Add status flags for tables I have to check both active and
445: * inactive tables.
446: */
1.37 tkusumi 447: if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE)))
1.2 haad 448: DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
1.37 tkusumi 449: else
1.2 haad 450: DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
1.20 haad 451:
1.2 haad 452: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
1.20 haad 453:
1.2 haad 454: if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
455: DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
456: else
457: DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
458:
459: dm_dev_unbusy(dmv);
1.20 haad 460:
1.2 haad 461: return 0;
462: }
1.32 christos 463:
1.2 haad 464: /*
465: * Set only flag to suggest that device is suspended. This call is
466: * not supported in NetBSD.
467: */
468: int
469: dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
470: {
471: dm_dev_t *dmv;
472: const char *name, *uuid;
473: uint32_t flags, minor;
1.20 haad 474:
1.2 haad 475: name = NULL;
476: uuid = NULL;
477: flags = 0;
1.20 haad 478:
1.50 thorpej 479: prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
480: prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
1.2 haad 481: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
482: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
1.20 haad 483:
484: if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
1.2 haad 485: DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
486: return ENOENT;
487: }
488: atomic_or_32(&dmv->flags, DM_SUSPEND_FLAG);
1.20 haad 489:
1.2 haad 490: dm_dbg_print_flags(dmv->flags);
1.20 haad 491:
1.2 haad 492: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
493: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
494: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
1.20 haad 495:
1.2 haad 496: dm_dev_unbusy(dmv);
1.20 haad 497:
1.2 haad 498: /* Add flags to dictionary flag after dmv -> dict copy */
1.20 haad 499: DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
500:
1.2 haad 501: return 0;
502: }
1.32 christos 503:
1.2 haad 504: /*
505: * Simulate Linux behaviour better and switch tables here and not in
1.20 haad 506: * dm_table_load_ioctl.
1.2 haad 507: */
508: int
509: dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
510: {
511: dm_dev_t *dmv;
512: const char *name, *uuid;
513: uint32_t flags, minor;
514:
515: name = NULL;
516: uuid = NULL;
517: flags = 0;
1.20 haad 518:
519: /*
520: * char *xml; xml = prop_dictionary_externalize(dm_dict);
521: * printf("%s\n",xml);
522: */
523:
1.50 thorpej 524: prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
525: prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
1.2 haad 526: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
527: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
1.20 haad 528:
1.2 haad 529: /* Remove device from global device list */
1.20 haad 530: if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
1.2 haad 531: DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
532: return ENOENT;
533: }
1.33 mlelstv 534:
535: /* Make inactive table active, if it exists */
536: if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
537: dm_table_switch_tables(&dmv->table_head);
538:
1.2 haad 539: atomic_and_32(&dmv->flags, ~(DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
540: atomic_or_32(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
1.20 haad 541:
542: DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
1.2 haad 543:
1.13 haad 544: dmgetproperties(dmv->diskp, &dmv->table_head);
1.20 haad 545:
1.2 haad 546: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
1.16 haad 547: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
1.2 haad 548: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
549:
550: dm_dev_unbusy(dmv);
551:
552: /* Destroy inactive table after resume. */
553: dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
1.20 haad 554:
1.2 haad 555: return 0;
556: }
1.32 christos 557:
1.2 haad 558: /*
559: * Table management routines
560: * lvm2tools doens't send name/uuid to kernel with table
561: * for lookup I have to use minor number.
562: */
563:
564: /*
565: * Remove inactive table from device. Routines which work's with inactive tables
566: * doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
567: */
568: int
569: dm_table_clear_ioctl(prop_dictionary_t dm_dict)
570: {
571: dm_dev_t *dmv;
572: const char *name, *uuid;
573: uint32_t flags, minor;
574:
575: name = NULL;
576: uuid = NULL;
577: flags = 0;
578: minor = 0;
1.20 haad 579:
1.50 thorpej 580: prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
581: prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
1.2 haad 582: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
583: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
1.20 haad 584:
1.2 haad 585: aprint_debug("Clearing inactive table from device: %s--%s\n",
586: name, uuid);
1.20 haad 587:
588: if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
1.2 haad 589: DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
590: return ENOENT;
591: }
592: /* Select unused table */
593: dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
594:
595: atomic_and_32(&dmv->flags, ~DM_INACTIVE_PRESENT_FLAG);
596:
597: dm_dev_unbusy(dmv);
1.20 haad 598:
1.2 haad 599: return 0;
600: }
1.32 christos 601:
1.2 haad 602: /*
603: * Get list of physical devices for active table.
604: * Get dev_t from pdev vnode and insert it into cmd_array.
605: *
606: * XXX. This function is called from lvm2tools to get information
1.20 haad 607: * about physical devices, too e.g. during vgcreate.
1.2 haad 608: */
609: int
610: dm_table_deps_ioctl(prop_dictionary_t dm_dict)
611: {
612: dm_dev_t *dmv;
613: dm_table_t *tbl;
614: dm_table_entry_t *table_en;
615:
616: prop_array_t cmd_array;
617: const char *name, *uuid;
618: uint32_t flags, minor;
1.17 haad 619: int table_type;
1.2 haad 620:
621: name = NULL;
622: uuid = NULL;
623: flags = 0;
1.20 haad 624:
1.50 thorpej 625: prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
626: prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
1.2 haad 627: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
628: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
629:
630: /* create array for dev_t's */
631: cmd_array = prop_array_create();
632:
1.20 haad 633: if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
1.2 haad 634: DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
635: return ENOENT;
636: }
637: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
1.50 thorpej 638: prop_dictionary_set_string(dm_dict, DM_IOCTL_NAME, dmv->name);
639: prop_dictionary_set_string(dm_dict, DM_IOCTL_UUID, dmv->uuid);
1.20 haad 640:
1.2 haad 641: aprint_debug("Getting table deps for device: %s\n", dmv->name);
642:
1.20 haad 643: /*
644: * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
645: * INACTIVE TABLE
646: */
1.17 haad 647: if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
648: table_type = DM_TABLE_INACTIVE;
649: else
650: table_type = DM_TABLE_ACTIVE;
1.20 haad 651:
1.17 haad 652: tbl = dm_table_get_entry(&dmv->table_head, table_type);
1.2 haad 653:
654: SLIST_FOREACH(table_en, tbl, next)
1.48 tkusumi 655: dm_table_deps(table_en, cmd_array);
1.2 haad 656:
1.17 haad 657: dm_table_release(&dmv->table_head, table_type);
1.2 haad 658: dm_dev_unbusy(dmv);
1.20 haad 659:
1.2 haad 660: prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
661: prop_object_release(cmd_array);
1.20 haad 662:
1.2 haad 663: return 0;
664: }
1.32 christos 665:
1.48 tkusumi 666: static int
667: dm_table_deps(dm_table_entry_t *table_en, prop_array_t array)
668: {
669: dm_mapping_t *map;
670: int i, size;
671: uint64_t rdev, tmp;
672:
673: size = prop_array_count(array);
674:
675: TAILQ_FOREACH(map, &table_en->pdev_maps, next) {
676: rdev = map->data.pdev->pdev_vnode->v_rdev;
677: for (i = 0; i < size; i++) {
678: if (prop_array_get_uint64(array, i, &tmp) == true)
679: if (rdev == tmp)
680: break; /* exists */
681: }
682: /*
683: * Ignore if the device has already been added by
684: * other tables.
685: */
686: if (i == size) {
687: prop_array_add_uint64(array, rdev);
688: aprint_debug("%s: %d:%d\n", __func__, major(rdev),
689: minor(rdev));
690: }
691: }
692:
693: return 0;
694: }
695:
1.2 haad 696: /*
697: * Load new table/tables to device.
698: * Call apropriate target init routine open all physical pdev's and
699: * link them to device. For other targets mirror, strip, snapshot
700: * etc. also add dependency devices to upcalls list.
701: *
702: * Load table to inactive slot table are switched in dm_device_resume_ioctl.
703: * This simulates Linux behaviour better there should not be any difference.
704: */
705: int
706: dm_table_load_ioctl(prop_dictionary_t dm_dict)
707: {
708: dm_dev_t *dmv;
709: dm_table_entry_t *table_en, *last_table;
1.20 haad 710: dm_table_t *tbl;
1.2 haad 711: dm_target_t *target;
712:
713: prop_object_iterator_t iter;
714: prop_array_t cmd_array;
1.15 haad 715: prop_dictionary_t target_dict;
1.20 haad 716:
1.2 haad 717: const char *name, *uuid, *type;
1.46 tkusumi 718: uint32_t flags, minor;
1.15 haad 719:
1.2 haad 720: flags = 0;
721: name = NULL;
722: uuid = NULL;
723: last_table = NULL;
724:
1.20 haad 725: /*
726: * char *xml; xml = prop_dictionary_externalize(dm_dict);
727: * printf("%s\n",xml);
728: */
729:
1.50 thorpej 730: prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
731: prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
1.2 haad 732: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
733: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
1.20 haad 734:
1.2 haad 735: cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
736: iter = prop_array_iterator(cmd_array);
1.20 haad 737: dm_dbg_print_flags(flags);
738:
739: if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
1.2 haad 740: DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
1.28 mlelstv 741: prop_object_iterator_release(iter);
1.2 haad 742: return ENOENT;
743: }
1.7 haad 744: aprint_debug("Loading table to device: %s--%d\n", name,
745: dmv->table_head.cur_active_table);
1.20 haad 746:
1.2 haad 747: /*
748: * I have to check if this table slot is not used by another table list.
749: * if it is used I should free them.
750: */
751: if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
752: dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
753:
754: dm_dbg_print_flags(dmv->flags);
755: tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
1.20 haad 756:
1.2 haad 757: aprint_debug("dmv->name = %s\n", dmv->name);
1.20 haad 758:
1.2 haad 759: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
760:
1.20 haad 761: while ((target_dict = prop_object_iterator_next(iter)) != NULL) {
1.46 tkusumi 762: int ret;
1.50 thorpej 763: const char *cp;
764: char *str;
765: size_t strsz;
1.46 tkusumi 766:
1.50 thorpej 767: prop_dictionary_get_string(target_dict,
1.2 haad 768: DM_TABLE_TYPE, &type);
769: /*
770: * If we want to deny table with 2 or more different
771: * target we should do it here
772: */
1.7 haad 773: if (((target = dm_target_lookup(type)) == NULL) &&
774: ((target = dm_target_autoload(type)) == NULL)) {
1.8 haad 775: dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
1.7 haad 776: dm_dev_unbusy(dmv);
1.28 mlelstv 777: prop_object_iterator_release(iter);
1.2 haad 778: return ENOENT;
1.7 haad 779: }
1.31 chs 780: table_en = kmem_alloc(sizeof(dm_table_entry_t), KM_SLEEP);
1.2 haad 781: prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
782: &table_en->start);
783: prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
784: &table_en->length);
785:
786: table_en->target = target;
787: table_en->dm_dev = dmv;
788: table_en->target_config = NULL;
1.48 tkusumi 789: TAILQ_INIT(&table_en->pdev_maps);
1.20 haad 790:
1.2 haad 791: /*
792: * There is a parameter string after dm_target_spec
793: * structure which points to /dev/wd0a 284 part of
794: * table. String str points to this text. This can be
795: * null and therefore it should be checked before we try to
796: * use it.
797: */
1.50 thorpej 798: cp = NULL;
799: prop_dictionary_get_string(target_dict,
800: DM_TABLE_PARAMS, &cp);
801: if (cp == NULL)
802: str = NULL;
803: else
804: str = kmem_strdupsize(cp, &strsz, KM_SLEEP);
1.20 haad 805:
1.30 christos 806: if (SLIST_EMPTY(tbl) || last_table == NULL)
1.2 haad 807: /* insert this table to head */
808: SLIST_INSERT_HEAD(tbl, table_en, next);
809: else
810: SLIST_INSERT_AFTER(last_table, table_en, next);
1.20 haad 811:
1.41 tkusumi 812: if ((ret = dm_table_init(target, table_en, str)) != 0) {
1.4 haad 813: dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
1.2 haad 814: dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
1.50 thorpej 815:
816: if (str != NULL)
817: kmem_free(str, strsz);
1.15 haad 818:
1.4 haad 819: dm_dev_unbusy(dmv);
1.28 mlelstv 820: prop_object_iterator_release(iter);
1.2 haad 821: return ret;
822: }
823: last_table = table_en;
1.50 thorpej 824: if (str != NULL)
825: kmem_free(str, strsz);
1.2 haad 826: }
827: prop_object_iterator_release(iter);
1.20 haad 828:
1.2 haad 829: DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
830: atomic_or_32(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
1.20 haad 831:
1.2 haad 832: dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
833: dm_dev_unbusy(dmv);
834: return 0;
835: }
1.32 christos 836:
1.41 tkusumi 837: static int
838: dm_table_init(dm_target_t *target, dm_table_entry_t *table_en, char *params)
839: {
840: int i, n, ret, argc;
841: char **ap, **argv;
842:
843: if (params == NULL)
844: return EINVAL;
845:
846: n = target->max_argc;
847: if (n)
848: aprint_debug("Max argc %d for %s target\n", n, target->name);
849: else
850: n = 32; /* large enough for most targets */
851:
852: argv = kmem_alloc(sizeof(*argv) * n, KM_SLEEP);
853:
854: for (ap = argv;
855: ap < &argv[n] && (*ap = strsep(¶ms, " \t")) != NULL;) {
856: if (**ap != '\0')
857: ap++;
858: }
859: argc = ap - argv;
860:
861: for (i = 0; i < argc; i++)
862: aprint_debug("DM: argv[%d] = \"%s\"\n", i, argv[i]);
863:
864: KASSERT(target->init);
865: ret = target->init(table_en, argc, argv);
866:
867: kmem_free(argv, sizeof(*argv) * n);
868:
869: return ret;
870: }
871:
1.2 haad 872: /*
873: * Get description of all tables loaded to device from kernel
874: * and send it to libdevmapper.
875: *
876: * Output dictionary for every table:
877: *
878: * <key>cmd_data</key>
879: * <array>
880: * <dict>
881: * <key>type<key>
882: * <string>...</string>
883: *
884: * <key>start</key>
885: * <integer>...</integer>
886: *
887: * <key>length</key>
888: * <integer>...</integer>
889: *
890: * <key>params</key>
891: * <string>...</string>
892: * </dict>
893: * </array>
894: */
895: int
896: dm_table_status_ioctl(prop_dictionary_t dm_dict)
897: {
898: dm_dev_t *dmv;
899: dm_table_t *tbl;
900: dm_table_entry_t *table_en;
901:
902: prop_array_t cmd_array;
903: prop_dictionary_t target_dict;
1.20 haad 904:
1.29 justin 905: uint32_t minor, flags;
1.2 haad 906: const char *name, *uuid;
1.17 haad 907: int table_type;
1.20 haad 908:
1.2 haad 909: uuid = NULL;
910: name = NULL;
911: flags = 0;
912:
1.52 ! christos 913: prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
! 914: prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
1.2 haad 915: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
916: prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
917:
918: cmd_array = prop_array_create();
1.20 haad 919:
920: if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
1.2 haad 921: DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
922: return ENOENT;
923: }
1.20 haad 924: /*
925: * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
926: * INACTIVE TABLE
927: */
1.17 haad 928: if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
929: table_type = DM_TABLE_INACTIVE;
930: else
931: table_type = DM_TABLE_ACTIVE;
1.20 haad 932:
1.37 tkusumi 933: if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE)) {
1.2 haad 934: DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
1.37 tkusumi 935: } else {
1.2 haad 936: DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
1.20 haad 937:
1.2 haad 938: if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
939: DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
1.37 tkusumi 940: else
1.2 haad 941: DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
942: }
1.20 haad 943:
1.2 haad 944: if (dmv->flags & DM_SUSPEND_FLAG)
945: DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
1.20 haad 946:
1.2 haad 947: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
1.20 haad 948:
1.2 haad 949: aprint_debug("Status of device tables: %s--%d\n",
950: name, dmv->table_head.cur_active_table);
1.20 haad 951:
1.17 haad 952: tbl = dm_table_get_entry(&dmv->table_head, table_type);
1.20 haad 953:
954: SLIST_FOREACH(table_en, tbl, next) {
1.46 tkusumi 955: char *params;
1.49 tkusumi 956: int is_table;
1.46 tkusumi 957:
1.2 haad 958: target_dict = prop_dictionary_create();
959: aprint_debug("%016" PRIu64 ", length %016" PRIu64
960: ", target %s\n", table_en->start, table_en->length,
961: table_en->target->name);
962:
963: prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
964: table_en->start);
965: prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
966: table_en->length);
967:
1.50 thorpej 968: prop_dictionary_set_string(target_dict, DM_TABLE_TYPE,
1.2 haad 969: table_en->target->name);
970:
971: /* dm_table_get_cur_actv.table ?? */
972: prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
973: dmv->table_head.cur_active_table);
974:
1.34 tkusumi 975: /*
976: * Explicitly clear DM_TABLE_PARAMS to prevent dmsetup(8) from
977: * printing junk when DM_TABLE_PARAMS was never initialized.
978: */
1.50 thorpej 979: prop_dictionary_set_string(target_dict, DM_TABLE_PARAMS, "");
1.34 tkusumi 980:
1.49 tkusumi 981: is_table = (flags & DM_STATUS_TABLE_FLAG) ? 1 : 0;
982: if (is_table && table_en->target->table)
1.45 tkusumi 983: params = table_en->target->table(
1.44 tkusumi 984: table_en->target_config);
1.49 tkusumi 985: else if (!is_table && table_en->target->info)
1.44 tkusumi 986: params = table_en->target->info(
987: table_en->target_config);
988: else
989: params = NULL;
1.20 haad 990:
1.44 tkusumi 991: if (params != NULL) {
1.50 thorpej 992: prop_dictionary_set_string(target_dict,
1.44 tkusumi 993: DM_TABLE_PARAMS, params);
994: kmem_free(params, DM_MAX_PARAMS_SIZE);
1.2 haad 995: }
1.44 tkusumi 996:
1.2 haad 997: prop_array_add(cmd_array, target_dict);
998: prop_object_release(target_dict);
999: }
1000:
1.17 haad 1001: dm_table_release(&dmv->table_head, table_type);
1.2 haad 1002: dm_dev_unbusy(dmv);
1.17 haad 1003:
1004: prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
1.2 haad 1005: prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
1006: prop_object_release(cmd_array);
1.20 haad 1007:
1.2 haad 1008: return 0;
1009: }
1010:
1011: /*
1012: * For every call I have to set kernel driver version.
1013: * Because I can have commands supported only in other
1.20 haad 1014: * newer/later version. This routine is called for every
1.2 haad 1015: * ioctl command.
1016: */
1017: int
1018: dm_check_version(prop_dictionary_t dm_dict)
1019: {
1.47 tkusumi 1020: int i;
1.29 justin 1021: uint32_t dm_version[3];
1.2 haad 1022: prop_array_t ver;
1.20 haad 1023:
1.2 haad 1024: ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
1.20 haad 1025:
1026: for (i = 0; i < 3; i++)
1.2 haad 1027: prop_array_get_uint32(ver, i, &dm_version[i]);
1028:
1.20 haad 1029: if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]) {
1.2 haad 1030: aprint_debug("libdevmapper/kernel version mismatch "
1031: "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
1032: DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
1033: dm_version[0], dm_version[1], dm_version[2]);
1.20 haad 1034:
1.2 haad 1035: return EIO;
1036: }
1037: prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
1038: prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
1039: prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
1040:
1.17 haad 1041: prop_dictionary_set(dm_dict, DM_IOCTL_VERSION, ver);
1.20 haad 1042:
1.2 haad 1043: return 0;
1044: }
CVSweb <webmaster@jp.NetBSD.org>