4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * RCM module for managing dump device during dynamic
41 #include <sys/dumpadm.h>
42 #include <sys/param.h>
44 #include <sys/types.h>
46 #include "rcm_module.h"
49 #define DUMP_CACHE_NEW 0x01
50 #define DUMP_CACHE_STALE 0x02
51 #define DUMP_CACHE_OFFLINED 0x04
53 #define DUMPADM "/usr/sbin/dumpadm -d "
54 #define DUMPADM_SWAP DUMPADM"swap"
56 typedef struct dump_conf
{
57 char device
[MAXPATHLEN
];
58 int conf_flags
; /* defs in <sys/dumpadm.h> */
60 struct dump_conf
*next
;
61 struct dump_conf
*prev
;
67 * N.B. Although we currently only support a single
68 * dump device, the cache is multi-entry since there
69 * may be multiple outstanding registrations.
71 static dump_conf_t
*cache
;
72 static mutex_t cache_lock
;
74 static int dump_register(rcm_handle_t
*);
75 static int dump_unregister(rcm_handle_t
*);
76 static int dump_getinfo(rcm_handle_t
*, char *, id_t
, uint_t
,
77 char **, char **, nvlist_t
*, rcm_info_t
**);
78 static int dump_suspend(rcm_handle_t
*, char *, id_t
, timespec_t
*,
79 uint_t
, char **, rcm_info_t
**);
80 static int dump_resume(rcm_handle_t
*, char *, id_t
, uint_t
,
81 char **, rcm_info_t
**);
82 static int dump_offline(rcm_handle_t
*, char *, id_t
, uint_t
,
83 char **, rcm_info_t
**);
84 static int dump_online(rcm_handle_t
*, char *, id_t
, uint_t
,
85 char **, rcm_info_t
**);
86 static int dump_remove(rcm_handle_t
*, char *, id_t
, uint_t
,
87 char **, rcm_info_t
**);
89 static int alloc_usage(char **, int);
90 static void cache_insert(dump_conf_t
*);
91 static dump_conf_t
*cache_lookup(char *);
92 static void cache_remove(dump_conf_t
*);
93 static dump_conf_t
*dump_conf_alloc(void);
94 static int dump_configure(dump_conf_t
*, char **);
95 static int dump_relocate(dump_conf_t
*, char **);
96 static void free_cache(void);
97 static void log_cmd_status(int);
98 static int update_cache(rcm_handle_t
*);
100 static struct rcm_mod_ops dump_ops
=
125 return ("RCM Dump module 1.3");
132 (void) mutex_destroy(&cache_lock
);
134 return (RCM_SUCCESS
);
138 dump_register(rcm_handle_t
*hdl
)
140 return (update_cache(hdl
));
144 dump_unregister(rcm_handle_t
*hdl
)
148 (void) mutex_lock(&cache_lock
);
149 while ((dc
= cache
) != NULL
) {
151 (void) rcm_unregister_interest(hdl
, dc
->device
, 0);
154 (void) mutex_unlock(&cache_lock
);
156 return (RCM_SUCCESS
);
161 dump_getinfo(rcm_handle_t
*hdl
, char *rsrcname
, id_t id
, uint_t flags
,
162 char **infostr
, char **errstr
, nvlist_t
*props
, rcm_info_t
**dependent
)
167 assert(rsrcname
!= NULL
&& infostr
!= NULL
);
169 (void) mutex_lock(&cache_lock
);
170 if ((dc
= cache_lookup(rsrcname
)) == NULL
) {
171 (void) mutex_unlock(&cache_lock
);
172 rcm_log_message(RCM_ERROR
, "unknown resource: %s\n",
174 return (RCM_FAILURE
);
176 conf_flags
= dc
->conf_flags
;
177 (void) mutex_unlock(&cache_lock
);
179 return ((alloc_usage(infostr
, conf_flags
) == 0) ?
180 RCM_SUCCESS
: RCM_FAILURE
);
184 * Relocate dump device to maintain availability during suspension.
185 * Fail request if unable to relocate.
189 dump_suspend(rcm_handle_t
*hdl
, char *rsrcname
, id_t id
, timespec_t
*interval
,
190 uint_t flags
, char **errstr
, rcm_info_t
**dependent
)
195 assert(rsrcname
!= NULL
&& errstr
!= NULL
);
197 if (flags
& RCM_QUERY
)
198 return (RCM_SUCCESS
);
200 (void) mutex_lock(&cache_lock
);
201 if ((dc
= cache_lookup(rsrcname
)) == NULL
) {
202 (void) mutex_unlock(&cache_lock
);
203 return (RCM_SUCCESS
);
206 rv
= dump_relocate(dc
, errstr
);
207 (void) mutex_unlock(&cache_lock
);
214 dump_resume(rcm_handle_t
*hdl
, char *rsrcname
, id_t id
, uint_t flags
,
215 char **errstr
, rcm_info_t
**dependent
)
220 assert(rsrcname
!= NULL
&& errstr
!= NULL
);
222 (void) mutex_lock(&cache_lock
);
223 if ((dc
= cache_lookup(rsrcname
)) == NULL
) {
224 (void) mutex_unlock(&cache_lock
);
225 return (RCM_SUCCESS
);
228 rv
= dump_configure(dc
, errstr
);
229 (void) mutex_unlock(&cache_lock
);
235 * By default, reject offline. If offline request is
236 * forced, attempt to relocate the dump device.
240 dump_offline(rcm_handle_t
*hdl
, char *rsrcname
, id_t id
, uint_t flags
,
241 char **errstr
, rcm_info_t
**dependent
)
247 assert(rsrcname
!= NULL
&& errstr
!= NULL
);
249 if ((flags
& RCM_FORCE
) && (flags
& RCM_QUERY
))
250 return (RCM_SUCCESS
);
252 (void) mutex_lock(&cache_lock
);
253 if ((dc
= cache_lookup(rsrcname
)) == NULL
) {
254 (void) mutex_unlock(&cache_lock
);
255 return (RCM_SUCCESS
);
258 if (flags
& RCM_FORCE
) {
259 rv
= dump_relocate(dc
, errstr
);
260 (void) mutex_unlock(&cache_lock
);
265 conf_flags
= dc
->conf_flags
;
266 (void) mutex_unlock(&cache_lock
);
267 (void) alloc_usage(errstr
, conf_flags
);
269 return (RCM_FAILURE
);
274 dump_online(rcm_handle_t
*hdl
, char *rsrcname
, id_t id
, uint_t flags
,
275 char **errstr
, rcm_info_t
**dependent
)
280 assert(rsrcname
!= NULL
&& errstr
!= NULL
);
282 (void) mutex_lock(&cache_lock
);
283 if ((dc
= cache_lookup(rsrcname
)) == NULL
) {
284 (void) mutex_unlock(&cache_lock
);
285 return (RCM_SUCCESS
);
288 rv
= dump_configure(dc
, errstr
);
289 (void) mutex_unlock(&cache_lock
);
296 dump_remove(rcm_handle_t
*hdl
, char *rsrcname
, id_t id
, uint_t flags
,
297 char **errstr
, rcm_info_t
**dependent
)
301 assert(rsrcname
!= NULL
&& errstr
!= NULL
);
303 (void) mutex_lock(&cache_lock
);
304 if ((dc
= cache_lookup(rsrcname
)) == NULL
) {
305 (void) mutex_unlock(&cache_lock
);
306 return (RCM_SUCCESS
);
310 (void) mutex_unlock(&cache_lock
);
312 return (RCM_SUCCESS
);
316 * For dedicated dump devices, invoke dumpadm(1M)
317 * to relocate dump to swap. For dump device on
318 * swap, this is a no-op as the RCM swap module
319 * will relocate by invoking swap(1M).
321 * Call with cache_lock held.
324 dump_relocate(dump_conf_t
*dc
, char **errstr
)
329 * This state may get out of sync for a dump device on swap,
330 * since we will will not know if the swap module succeeds.
331 * Worst case is we end up invoking dumpadm to configure
332 * the same device during a rollback.
334 dc
->cache_flags
|= DUMP_CACHE_OFFLINED
;
336 /* RCM swap module will handle non-dedicated */
337 if (!(dc
->conf_flags
& DUMP_EXCL
))
338 return (RCM_SUCCESS
);
340 rcm_log_message(RCM_TRACE1
, "%s\n", DUMPADM_SWAP
);
341 if ((stat
= rcm_exec_cmd(DUMPADM_SWAP
)) != 0) {
342 log_cmd_status(stat
);
343 *errstr
= strdup(gettext("unable to relocate dump device"));
344 dc
->cache_flags
&= ~DUMP_CACHE_OFFLINED
;
345 return (RCM_FAILURE
);
348 return (RCM_SUCCESS
);
352 * (Re)Configure dump device.
353 * Call with cache_lock held.
356 dump_configure(dump_conf_t
*dc
, char **errstr
)
358 char cmd
[sizeof (DUMPADM
) + MAXPATHLEN
];
361 assert(dc
!= NULL
&& dc
->device
!= NULL
);
363 /* minor optimization */
364 if (!(dc
->cache_flags
& DUMP_CACHE_OFFLINED
))
365 return (RCM_SUCCESS
);
367 (void) snprintf(cmd
, sizeof (cmd
), "%s%s", DUMPADM
, dc
->device
);
368 rcm_log_message(RCM_TRACE1
, "%s\n", cmd
);
369 if ((stat
= rcm_exec_cmd(cmd
)) != 0) {
370 log_cmd_status(stat
);
371 *errstr
= strdup(gettext("unable to configure dump device"));
372 return (RCM_FAILURE
);
374 dc
->cache_flags
&= ~DUMP_CACHE_OFFLINED
;
376 return (RCM_SUCCESS
);
380 * Returns current dump configuration
383 dump_conf_alloc(void)
390 if ((dc
= calloc(1, sizeof (*dc
))) == NULL
) {
391 rcm_log_message(RCM_ERROR
, "calloc failure\n");
395 if ((fd
= open("/dev/dump", O_RDONLY
)) == -1) {
397 * Suppress reporting if no logical link.
399 if (stat("/dev/dump", &sbuf
) == 0 &&
400 (fd
= open("/dev/dump", O_RDONLY
)) == -1) {
401 rcm_log_message(RCM_ERROR
,
402 "failed to open /dev/dump: %s\n",
403 ((err
= strerror(errno
)) == NULL
) ? "" : err
);
412 if (ioctl(fd
, DIOCGETDEV
, dc
->device
) == -1) {
413 if (errno
== ENODEV
) {
414 dc
->device
[0] = '\0';
416 rcm_log_message(RCM_ERROR
, "ioctl: %s\n",
417 ((err
= strerror(errno
)) == NULL
) ? "" : err
);
424 if (dc
->device
[0] != '\0') {
425 if ((dc
->conf_flags
= ioctl(fd
, DIOCGETCONF
, 0)) == -1) {
426 rcm_log_message(RCM_ERROR
, "ioctl: %s\n",
427 ((err
= strerror(errno
)) == NULL
) ? "" : err
);
439 update_cache(rcm_handle_t
*hdl
)
441 dump_conf_t
*ent
, *curr_dump
, *tmp
;
442 int rv
= RCM_SUCCESS
;
444 if ((curr_dump
= dump_conf_alloc()) == NULL
)
445 return (RCM_FAILURE
);
447 (void) mutex_lock(&cache_lock
);
450 * pass 1 - mark all current registrations stale
452 for (ent
= cache
; ent
!= NULL
; ent
= ent
->next
) {
453 ent
->cache_flags
|= DUMP_CACHE_STALE
;
457 * update current dump conf
459 if (curr_dump
->device
[0] == '\0') {
461 } else if ((ent
= cache_lookup(curr_dump
->device
)) != NULL
) {
462 ent
->cache_flags
&= ~DUMP_CACHE_STALE
;
463 ent
->conf_flags
= curr_dump
->conf_flags
;
466 curr_dump
->cache_flags
|= DUMP_CACHE_NEW
;
467 cache_insert(curr_dump
);
471 * pass 2 - register, unregister, or no-op based on cache flags
474 while (ent
!= NULL
) {
475 if (ent
->cache_flags
& DUMP_CACHE_OFFLINED
) {
480 if (ent
->cache_flags
& DUMP_CACHE_STALE
) {
481 if (rcm_unregister_interest(hdl
, ent
->device
, 0) !=
483 rcm_log_message(RCM_ERROR
, "failed to "
484 "unregister %s\n", ent
->device
);
493 if (!(ent
->cache_flags
& DUMP_CACHE_NEW
)) {
498 if (rcm_register_interest(hdl
, ent
->device
, 0, NULL
) !=
500 rcm_log_message(RCM_ERROR
, "failed to register "
501 "%s\n", ent
->device
);
504 rcm_log_message(RCM_DEBUG
, "registered %s\n",
506 ent
->cache_flags
&= ~DUMP_CACHE_NEW
;
510 (void) mutex_unlock(&cache_lock
);
516 * Call with cache_lock held.
519 cache_lookup(char *rsrc
)
523 for (dc
= cache
; dc
!= NULL
; dc
= dc
->next
) {
524 if (strcmp(rsrc
, dc
->device
) == 0) {
532 * Link to front of list.
533 * Call with cache_lock held.
536 cache_insert(dump_conf_t
*ent
)
540 ent
->next
->prev
= ent
;
546 * Call with cache_lock held.
549 cache_remove(dump_conf_t
*ent
)
551 if (ent
->next
!= NULL
) {
552 ent
->next
->prev
= ent
->prev
;
554 if (ent
->prev
!= NULL
) {
555 ent
->prev
->next
= ent
->next
;
568 (void) mutex_lock(&cache_lock
);
569 while ((dc
= cache
) != NULL
) {
573 (void) mutex_unlock(&cache_lock
);
577 alloc_usage(char **cpp
, int conf_flags
)
579 /* simplifies message translation */
580 if (conf_flags
& DUMP_EXCL
) {
581 *cpp
= strdup(gettext("dump device (dedicated)"));
583 *cpp
= strdup(gettext("dump device (swap)"));
587 rcm_log_message(RCM_ERROR
, "strdup failure\n");
594 log_cmd_status(int stat
)
599 rcm_log_message(RCM_ERROR
, "wait: %s\n",
600 ((err
= strerror(errno
)) == NULL
) ? "" : err
);
601 } else if (WIFEXITED(stat
)) {
602 rcm_log_message(RCM_ERROR
, "exit status: %d\n",
605 rcm_log_message(RCM_ERROR
, "wait status: %d\n", stat
);