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