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