1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 1995-2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Just in case we're not in a build environment, make sure that
* TEXT_DOMAIN gets set to something.
*/
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
/*
* libmeta wrappers for event notification
*/
#include <meta.h>
#include <sys/lvm/md_notify.h>
#if defined(DEBUG)
#include <assert.h>
#endif /* DEBUG */
struct tag2obj_type {
md_tags_t tag;
ev_obj_t obj;
} tag2obj_typetab[] =
{
{ TAG_EMPTY, EVO_EMPTY },
{ TAG_METADEVICE, EVO_METADEV },
{ TAG_REPLICA, EVO_REPLICA },
{ TAG_HSP, EVO_HSP },
{ TAG_HS, EVO_HS },
{ TAG_SET, EVO_SET },
{ TAG_DRIVE, EVO_DRIVE },
{ TAG_HOST, EVO_HOST },
{ TAG_MEDIATOR, EVO_MEDIATOR },
{ TAG_UNK, EVO_UNSPECIFIED },
{ TAG_LAST, EVO_LAST }
};
struct evdrv2evlib_type {
md_event_type_t drv;
evid_t lib;
} evdrv2evlib_typetab[] =
{
{ EQ_EMPTY, EV_EMPTY },
{ EQ_CREATE, EV_CREATE },
{ EQ_DELETE, EV_DELETE },
{ EQ_ADD, EV_ADD },
{ EQ_REMOVE, EV_REMOVE },
{ EQ_REPLACE, EV_REPLACE },
{ EQ_MEDIATOR_ADD, EV_MEDIATOR_ADD },
{ EQ_MEDIATOR_DELETE, EV_MEDIATOR_DELETE },
{ EQ_HOST_ADD, EV_HOST_ADD },
{ EQ_HOST_DELETE, EV_HOST_DELETE },
{ EQ_DRIVE_ADD, EV_DRIVE_ADD },
{ EQ_DRIVE_DELETE, EV_DRIVE_DELETE },
{ EQ_RENAME_SRC, EV_RENAME_SRC },
{ EQ_RENAME_DST, EV_RENAME_DST },
{ EQ_INIT_START, EV_INIT_START },
{ EQ_INIT_FAILED, EV_INIT_FAILED },
{ EQ_INIT_FATAL, EV_INIT_FATAL },
{ EQ_INIT_SUCCESS, EV_INIT_SUCCESS },
{ EQ_IOERR, EV_IOERR },
{ EQ_ERRED, EV_ERRED },
{ EQ_LASTERRED, EV_LASTERRED },
{ EQ_OK, EV_OK },
{ EQ_ENABLE, EV_ENABLE },
{ EQ_RESYNC_START, EV_RESYNC_START },
{ EQ_RESYNC_FAILED, EV_RESYNC_FAILED },
{ EQ_RESYNC_SUCCESS, EV_RESYNC_SUCCESS },
{ EQ_RESYNC_DONE, EV_RESYNC_DONE },
{ EQ_HOTSPARED, EV_HOTSPARED },
{ EQ_HS_FREED, EV_HS_FREED },
{ EQ_TAKEOVER, EV_TAKEOVER },
{ EQ_RELEASE, EV_RELEASE },
{ EQ_OPEN_FAIL, EV_OPEN_FAIL },
{ EQ_OFFLINE, EV_OFFLINE },
{ EQ_ONLINE, EV_ONLINE },
{ EQ_GROW, EV_GROW },
{ EQ_DETACH, EV_DETACH },
{ EQ_DETACHING, EV_DETACHING },
{ EQ_ATTACH, EV_ATTACH },
{ EQ_ATTACHING, EV_ATTACHING },
{ EQ_CHANGE, EV_CHANGE },
{ EQ_EXCHANGE, EV_EXCHANGE },
{ EQ_REGEN_START, EV_REGEN_START },
{ EQ_REGEN_DONE, EV_REGEN_DONE },
{ EQ_REGEN_FAILED, EV_REGEN_FAILED },
{ EQ_USER, EV_USER },
{ EQ_NOTIFY_LOST, EV_NOTIFY_LOST },
{ EQ_LAST, EV_LAST }
};
static ev_obj_t
dev2tag(md_dev64_t dev, set_t setno, md_error_t *ep)
{
mdname_t *np = NULL;
mdsetname_t *sp = NULL;
ev_obj_t obj = EVO_METADEV;
char *miscname;
if ((sp = metasetnosetname(setno, ep)) == NULL) {
goto out;
}
if (!(np = metamnumname(&sp, meta_getminor(dev), 0, ep))) {
goto out;
}
/* need to invalidate name in case rename or delete/create done */
meta_invalidate_name(np);
if (!(miscname = metagetmiscname(np, ep))) {
goto out;
}
if (strcmp(miscname, MD_STRIPE) == 0) {
obj = EVO_STRIPE;
} else if (strcmp(miscname, MD_MIRROR) == 0) {
obj = EVO_MIRROR;
} else if (strcmp(miscname, MD_RAID) == 0) {
obj = EVO_RAID5;
} else if (strcmp(miscname, MD_TRANS) == 0) {
obj = EVO_TRANS;
}
out:
return (obj);
}
static ev_obj_t
tagdrv_2_objlib(md_tags_t tag)
{
int i;
for (i = 0; tag2obj_typetab[i].tag != TAG_LAST; i++) {
if (tag2obj_typetab[i].tag == tag)
return (tag2obj_typetab[i].obj);
}
return (EVO_UNSPECIFIED);
}
static md_tags_t
objlib_2_tagdrv(ev_obj_t obj)
{
int i;
for (i = 0; tag2obj_typetab[i].tag != TAG_LAST; i++) {
if (tag2obj_typetab[i].obj == obj)
return (tag2obj_typetab[i].tag);
}
return (TAG_UNK);
}
static evid_t
evdrv_2_evlib(md_event_type_t drv_ev)
{
int i;
for (i = 0; evdrv2evlib_typetab[i].drv != EQ_LAST; i++) {
if (evdrv2evlib_typetab[i].drv == drv_ev)
return (evdrv2evlib_typetab[i].lib);
}
return (EV_UNK);
}
static md_event_type_t
evlib_2_evdrv(evid_t lib_ev)
{
int i;
for (i = 0; evdrv2evlib_typetab[i].drv != EQ_LAST; i++) {
if (evdrv2evlib_typetab[i].lib == lib_ev)
return (evdrv2evlib_typetab[i].drv);
}
return (EQ_EMPTY);
}
/*
* meta_event
* returns 0 on succcess or < 0 to indicate error.
* abs(return code) = errno
*/
static int
meta_event(md_event_ioctl_t *evctl, md_error_t *ep)
{
int l;
if (!evctl || !ep)
return (-EINVAL);
l = strlen(evctl->mdn_name);
if ((l == 0 && evctl->mdn_cmd != EQ_PUT) || l >= MD_NOTIFY_NAME_SIZE) {
return (-EINVAL);
}
MD_SETDRIVERNAME(evctl, MD_NOTIFY, 0);
mdclrerror(ep);
errno = 0;
if (metaioctl(MD_IOCNOTIFY, evctl, ep, evctl->mdn_name) != 0) {
if (errno == 0) {
errno = EINVAL;
}
if (mdisok(ep)) {
(void) mdsyserror(ep, errno, evctl->mdn_name);
}
return (-errno);
}
return (0);
}
static void
init_evctl(char *qname,
md_tags_t tag,
md_event_type_t ev,
uint_t flags,
set_t set,
md_dev64_t dev,
md_event_cmds_t cmd,
u_longlong_t udata,
md_event_ioctl_t *evctlp)
{
assert(evctlp);
(void) memset(evctlp, 0, sizeof (md_event_ioctl_t));
evctlp->mdn_magic = MD_EVENT_ID;
evctlp->mdn_rev = MD_NOTIFY_REVISION;
if (qname)
(void) strncpy(evctlp->mdn_name, qname, MD_NOTIFY_NAME_SIZE-1);
else
(void) memset(evctlp->mdn_name, 0, MD_NOTIFY_NAME_SIZE);
evctlp->mdn_tag = tag;
evctlp->mdn_event = ev;
evctlp->mdn_flags = flags;
evctlp->mdn_set = set;
evctlp->mdn_dev = dev;
evctlp->mdn_cmd = cmd;
evctlp->mdn_user = udata;
}
/*
* meta_notify_createq
* - creates an eventq
* - returns 0 on success or errno and sets ep
*/
int
meta_notify_createq(char *qname, ulong_t flags, md_error_t *ep)
{
md_event_ioctl_t evctl;
int err = 0;
mdclrerror(ep);
if (!qname || strlen(qname) == 0) {
(void) mdsyserror(ep, EINVAL,
dgettext(TEXT_DOMAIN,
"null or zero-length queue name"));
return (EINVAL);
}
init_evctl(qname,
TAG_EMPTY,
EQ_EMPTY,
(flags & EVFLG_PERMANENT) != 0? EQ_Q_PERM: 0,
/* set */ 0,
/* dev */ 0,
EQ_ON,
/* user-defined event data */ 0,
&evctl);
err = meta_event(&evctl, ep);
if (err == -EEXIST && !(flags & EVFLG_EXISTERR)) {
err = 0;
mdclrerror(ep);
}
if (!mdisok(ep) && mdanysyserror(ep)) {
err = (ep)->info.md_error_info_t_u.ds_error.errnum;
}
return (-err);
}
/*
* meta_notify_deleteq
* - deletes an eventq
* - free's any underlying resources
* - returns 0 on success or errno and sets ep
*/
int
meta_notify_deleteq(char *qname, md_error_t *ep)
{
md_event_ioctl_t evctl;
int err;
init_evctl(qname,
TAG_EMPTY,
EQ_EMPTY,
/* flags */ 0,
/* set */ 0,
/* dev */ 0,
EQ_OFF,
/* user-defined event data */ 0,
&evctl);
err = meta_event(&evctl, ep);
return (-err);
}
/*
* meta_notify_validq
* - verifies that the queue exists
* - returns true or false, ep may be changed as a side-effect
*/
bool_t
meta_notify_validq(char *qname, md_error_t *ep)
{
md_event_ioctl_t evctl;
init_evctl(qname,
TAG_EMPTY,
EQ_EMPTY,
/* flags */ 0,
/* set */ 0,
/* dev */ 0,
EQ_ON,
/* user-defined event data */ 0,
&evctl);
return (meta_event(&evctl, ep) == -EEXIST);
}
/*
* meta_notify_listq
* - returns number of (currently) active queus or -errno
* - allocates qnames array and sets user's pointer to it,
* fills in array with vector of qnames
*/
int
meta_notify_listq(char ***qnames, md_error_t *ep)
{
#ifdef lint
qnames = qnames;
#endif /* lint */
mdclrerror(ep);
(void) mdsyserror(ep, EOPNOTSUPP, "EOPNOTSUPP");
return (-EOPNOTSUPP);
}
/*
* meta_notify_flushq
* - calls the underlying notify driver to flush all events
* from the named queue
* - returns 0 on success or errno and sets ep as necessary
*/
int
meta_notify_flushq(char *qname, md_error_t *ep)
{
#ifdef lint
qname = qname;
#endif /* lint */
mdclrerror(ep);
(void) mdsyserror(ep, EOPNOTSUPP, "EOPNOTSUPP");
return (EOPNOTSUPP);
}
static void
cook_ev(md_event_ioctl_t *evctlp, md_ev_t *evp, md_error_t *ep)
{
assert(evctlp);
assert(evp);
evp->obj_type = tagdrv_2_objlib(evctlp->mdn_tag);
if (evp->obj_type == EVO_METADEV) {
evp->obj_type = dev2tag(evctlp->mdn_dev, evctlp->mdn_set, ep);
}
evp->setno = evctlp->mdn_set;
evp->ev = evdrv_2_evlib(evctlp->mdn_event);
evp->obj = evctlp->mdn_dev;
evp->uev = evctlp->mdn_user;
}
/*
* meta_notify_getev
* - collects up to 1 event and stores it into md_ev_t
* - returns number of events found (0 or 1) on success or -errno
* - flags governs whether an empty queue is waited upon (EVFLG_WAIT)
*/
int
meta_notify_getev(char *qname, ulong_t flags, md_ev_t *evp, md_error_t *ep)
{
md_event_ioctl_t evctl;
int n_ev;
int err = -EINVAL;
if (!evp) {
goto out;
}
init_evctl(qname,
TAG_EMPTY,
EQ_EMPTY,
/* flags (unused in get) */ 0,
(evp->setno == EV_ALLSETS)? MD_ALLSETS: evp->setno,
(evp->obj == EV_ALLOBJS)? MD_ALLDEVS: evp->obj,
(flags & EVFLG_WAIT) != 0? EQ_GET_WAIT: EQ_GET_NOWAIT,
/* user-defined event data */ 0,
&evctl);
err = meta_event(&evctl, ep);
/*
* trap EAGAIN so that EV_EMPTY events get returned, but
* be sure n_ev = 0 so that users who just watch the count
* will also work
*/
switch (err) {
case -EAGAIN:
err = n_ev = 0;
cook_ev(&evctl, evp, ep);
break;
case 0:
n_ev = 1;
cook_ev(&evctl, evp, ep);
break;
}
out:
return (err == 0? n_ev: err);
}
/*
* meta_notify_getevlist
* - collects all pending events in the named queue and allocates
* an md_evlist_t * to return them
* - returns the number of events found (may be 0 if !WAIT) on success
* or -errno and sets ep as necessary
*/
int
meta_notify_getevlist(char *qname,
ulong_t flags,
md_evlist_t **evpp_arg,
md_error_t *ep)
{
md_ev_t *evp = NULL;
md_evlist_t *evlp = NULL;
md_evlist_t *evlp_head = NULL;
md_evlist_t *new = NULL;
int n_ev = 0;
int err = -EINVAL;
mdclrerror(ep);
if (!evpp_arg) {
(void) mdsyserror(ep, EINVAL, dgettext(TEXT_DOMAIN,
"No event list pointer"));
goto out;
}
if (!qname || strlen(qname) == 0) {
(void) mdsyserror(ep, EINVAL, dgettext(TEXT_DOMAIN,
"Null or zero-length queue name"));
goto out;
}
do {
if (!(evp = (md_ev_t *)Malloc(sizeof (md_ev_t)))) {
(void) mdsyserror(ep, ENOMEM, qname);
continue;
}
evp->obj_type = EVO_EMPTY;
evp->setno = EV_ALLSETS;
evp->ev = EV_EMPTY;
evp->obj = EV_ALLOBJS;
evp->uev = 0ULL;
err = meta_notify_getev(qname, flags, evp, ep);
if (evp->ev != EV_EMPTY) {
new = (md_evlist_t *)Zalloc(sizeof (md_evlist_t));
if (evlp_head == NULL) {
evlp = evlp_head = new;
} else {
evlp->next = new;
evlp = new;
}
evlp->evp = evp;
n_ev++;
}
} while (err >= 0 && evp && evp->ev != EV_EMPTY);
out:
if (err == -EAGAIN) {
err = 0;
}
if (err < 0) {
meta_notify_freeevlist(evlp_head);
evlp_head = NULL;
return (err);
} else if ((err == 0) && (evp->ev == EV_EMPTY)) {
Free(evp);
evp = NULL;
}
if (evpp_arg) {
*evpp_arg = evlp_head;
}
return (n_ev);
}
/*
* the guts of meta_notify_putev() and meta_notify_sendev()
* are within this function.
*
* meta_notify_putev() is intended for general use by user-level code,
* such as the GUI, to send user-defined events.
*
* meta_notify_sendev() is for "user-level driver" code, such as
* set manipulation and the multi-host daemon to generate events.
*
* Note- only convention enforces this usage.
*/
int
meta_notify_doputev(md_ev_t *evp, md_error_t *ep)
{
md_event_ioctl_t evctl;
if (!evp || !ep) {
return (EINVAL);
}
/*
* users may only put events of type EQ_USER
*/
init_evctl(/* qname (unused in put) */ NULL,
TAG_EMPTY,
EQ_EMPTY,
/* flags (unused in put) */ 0,
(evp->setno == EV_ALLSETS)? MD_ALLSETS: evp->setno,
(evp->obj == EV_ALLOBJS)? MD_ALLDEVS: evp->obj,
EQ_PUT,
evp->uev,
&evctl);
evctl.mdn_tag = objlib_2_tagdrv(evp->obj_type);
evctl.mdn_event = evlib_2_evdrv(evp->ev);
return (-meta_event(&evctl, ep));
}
/*
* meta_notify_putev
* - sends an event down to the notify driver (hence, all queues)
* - returns 0 on success or errno
*/
int
meta_notify_putev(md_ev_t *evp, md_error_t *ep)
{
if (!evp || !ep) {
return (EINVAL);
}
evp->ev = EV_USER; /* by definition */
return (meta_notify_doputev(evp, ep));
}
/*
* alternate put event entry point which allows
* more control of event innards (for use by md "user-level drivers")
*
* Since this routine isn't for use by clients, the user event data
* is always forced to be 0. That is only meaningful for events
* of type EQ_USER (and those go through meta_notify_putev()), so
* this is consistent.
*/
int
meta_notify_sendev(
ev_obj_t tag,
set_t set,
md_dev64_t dev,
evid_t ev)
{
md_error_t status = mdnullerror;
md_error_t *ep = &status;
md_ev_t ev_packet;
int rc;
ev_packet.obj_type = tag;
ev_packet.setno = set;
ev_packet.obj = dev;
ev_packet.ev = ev;
ev_packet.uev = 0ULL;
rc = meta_notify_doputev(&ev_packet, ep);
if (0 == rc && !mdisok(ep)) {
rc = EINVAL;
mdclrerror(ep);
}
return (rc);
}
/*
* meta_notify_putevlist
* - sends all of the events in the event list
* - returns number of events sent (>= 0) on success or -errno
*/
int
meta_notify_putevlist(md_evlist_t *evlp, md_error_t *ep)
{
md_evlist_t *evlpi;
int n_ev = 0;
int err;
if (!evlp) {
err = 0;
goto out; /* that was easy */
}
for (n_ev = 0, evlpi = evlp; evlpi; evlpi = evlpi->next) {
if ((err = meta_notify_putev(evlpi->evp, ep)) < 0) {
goto out;
}
n_ev++;
}
out:
return (err != 0? err: n_ev);
}
/*
* meta_notify_freevlist
* - frees any memory allocated within the event list
* - returns 0 on success or errno and sets ep as necessary
*/
void
meta_notify_freeevlist(md_evlist_t *evlp)
{
md_evlist_t *i;
md_evlist_t *next;
for (i = evlp; i; i = i->next) {
if (i && i->evp) {
Free(i->evp);
i->evp = NULL;
}
}
for (i = evlp; i; /* NULL */) {
next = i->next;
Free(i);
i = next;
}
}
|