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