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