1/*-
2 * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions, and the following disclaimer,
10 *    without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 *    substantially similar to the "NO WARRANTY" disclaimer below
13 *    ("Disclaimer") and any redistribution must be conditioned upon
14 *    including a substantially similar Disclaimer requirement for further
15 *    binary redistribution.
16 *
17 * NO WARRANTY
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
29 *
30 * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31 *
32 * $FreeBSD$
33 */
34
35/**
36 * \file callout.cc
37 *
38 * \brief Implementation of the Callout class - multi-client
39 *        timer services built on top of the POSIX interval timer.
40 */
41
42#include <sys/time.h>
43
44#include <signal.h>
45#include <syslog.h>
46
47#include <climits>
48#include <list>
49#include <map>
50#include <string>
51
52#include <devdctl/guid.h>
53#include <devdctl/event.h>
54#include <devdctl/event_factory.h>
55#include <devdctl/consumer.h>
56#include <devdctl/exception.h>
57
58#include "callout.h"
59#include "vdev_iterator.h"
60#include "zfsd.h"
61#include "zfsd_exception.h"
62
63std::list<Callout *> Callout::s_activeCallouts;
64bool		     Callout::s_alarmFired(false);
65
66void
67Callout::Init()
68{
69	signal(SIGALRM,  Callout::AlarmSignalHandler);
70}
71
72bool
73Callout::Stop()
74{
75	if (!IsPending())
76		return (false);
77
78	for (std::list<Callout *>::iterator it(s_activeCallouts.begin());
79	     it != s_activeCallouts.end(); it++) {
80		if (*it != this)
81			continue;
82
83		it = s_activeCallouts.erase(it);
84		if (it != s_activeCallouts.end()) {
85
86			/*
87			 * Maintain correct interval for the
88			 * callouts that follow the just removed
89			 * entry.
90			 */
91			timeradd(&(*it)->m_interval, &m_interval,
92				 &(*it)->m_interval);
93		}
94		break;
95	}
96	m_pending = false;
97	return (true);
98}
99
100bool
101Callout::Reset(const timeval &interval, CalloutFunc_t *func, void *arg)
102{
103	bool cancelled(false);
104
105	if (!timerisset(&interval))
106		throw ZfsdException("Callout::Reset: interval of 0");
107
108	cancelled = Stop();
109
110	m_interval = interval;
111	m_func     = func;
112	m_arg      = arg;
113	m_pending  = true;
114
115	std::list<Callout *>::iterator it(s_activeCallouts.begin());
116	for (; it != s_activeCallouts.end(); it++) {
117
118		if (timercmp(&(*it)->m_interval, &m_interval, <=)) {
119			/*
120			 * Decrease our interval by those that come
121			 * before us.
122			 */
123			timersub(&m_interval, &(*it)->m_interval, &m_interval);
124		} else {
125			/*
126			 * Account for the time between the newly
127			 * inserted event and those that follow.
128			 */
129			timersub(&(*it)->m_interval, &m_interval,
130				 &(*it)->m_interval);
131			break;
132		}
133	}
134	s_activeCallouts.insert(it, this);
135
136
137	if (s_activeCallouts.front() == this) {
138		itimerval timerval = { {0, 0}, m_interval };
139
140		setitimer(ITIMER_REAL, &timerval, NULL);
141	}
142
143	return (cancelled);
144}
145
146void
147Callout::AlarmSignalHandler(int)
148{
149	s_alarmFired = true;
150	ZfsDaemon::WakeEventLoop();
151}
152
153void
154Callout::ExpireCallouts()
155{
156	if (!s_alarmFired)
157		return;
158
159	s_alarmFired = false;
160	if (s_activeCallouts.empty()) {
161		/* Callout removal/SIGALRM race was lost. */
162		return;
163	}
164
165	/*
166	 * Expire the first callout (the one we used to set the
167	 * interval timer) as well as any callouts following that
168	 * expire at the same time (have a zero interval from
169	 * the callout before it).
170	 */
171	do {
172		Callout *cur(s_activeCallouts.front());
173		s_activeCallouts.pop_front();
174		cur->m_pending = false;
175		cur->m_func(cur->m_arg);
176	} while (!s_activeCallouts.empty()
177	      && timerisset(&s_activeCallouts.front()->m_interval) == 0);
178
179	if (!s_activeCallouts.empty()) {
180		Callout *next(s_activeCallouts.front());
181		itimerval timerval = { { 0, 0 }, next->m_interval };
182
183		setitimer(ITIMER_REAL, &timerval, NULL);
184	}
185}
186
187timeval
188Callout::TimeRemaining() const
189{
190	/*
191	 * Outline: Add the m_interval for each callout in s_activeCallouts
192	 * ahead of this, except for the first callout.  Add to that the result
193	 * of getitimer (That's because the first callout stores its original
194	 * interval setting while the timer is ticking).
195	 */
196	itimerval timervalToAlarm;
197	timeval timeToExpiry;
198	std::list<Callout *>::iterator it;
199
200	if (!IsPending()) {
201		timeToExpiry.tv_sec = INT_MAX;
202		timeToExpiry.tv_usec = 999999;	/*maximum normalized value*/
203		return (timeToExpiry);
204	}
205
206	timerclear(&timeToExpiry);
207	getitimer(ITIMER_REAL, &timervalToAlarm);
208	timeval& timeToAlarm = timervalToAlarm.it_value;
209	timeradd(&timeToExpiry, &timeToAlarm, &timeToExpiry);
210
211	it =s_activeCallouts.begin();
212	it++;	/*skip the first callout in the list*/
213	for (; it != s_activeCallouts.end(); it++) {
214		timeradd(&timeToExpiry, &(*it)->m_interval, &timeToExpiry);
215		if ((*it) == this)
216			break;
217	}
218	return (timeToExpiry);
219}
220