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