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/byteorder.h> 43 #include <sys/time.h> 44 45 #include <signal.h> 46 #include <syslog.h> 47 48 #include <climits> 49 #include <list> 50 #include <map> 51 #include <string> 52 53 #include <devdctl/guid.h> 54 #include <devdctl/event.h> 55 #include <devdctl/event_factory.h> 56 #include <devdctl/consumer.h> 57 #include <devdctl/exception.h> 58 59 #include "callout.h" 60 #include "vdev_iterator.h" 61 #include "zfsd.h" 62 #include "zfsd_exception.h" 63 64 std::list<Callout *> Callout::s_activeCallouts; 65 bool Callout::s_alarmFired(false); 66 67 void 68 Callout::Init() 69 { 70 signal(SIGALRM, Callout::AlarmSignalHandler); 71 } 72 73 bool 74 Callout::Stop() 75 { 76 if (!IsPending()) 77 return (false); 78 79 for (std::list<Callout *>::iterator it(s_activeCallouts.begin()); 80 it != s_activeCallouts.end(); it++) { 81 if (*it != this) 82 continue; 83 84 it = s_activeCallouts.erase(it); 85 if (it != s_activeCallouts.end()) { 86 87 /* 88 * Maintain correct interval for the 89 * callouts that follow the just removed 90 * entry. 91 */ 92 timeradd(&(*it)->m_interval, &m_interval, 93 &(*it)->m_interval); 94 } 95 break; 96 } 97 m_pending = false; 98 return (true); 99 } 100 101 bool 102 Callout::Reset(const timeval &interval, CalloutFunc_t *func, void *arg) 103 { 104 bool cancelled(false); 105 106 if (!timerisset(&interval)) 107 throw ZfsdException("Callout::Reset: interval of 0"); 108 109 cancelled = Stop(); 110 111 m_interval = interval; 112 m_func = func; 113 m_arg = arg; 114 m_pending = true; 115 116 std::list<Callout *>::iterator it(s_activeCallouts.begin()); 117 for (; it != s_activeCallouts.end(); it++) { 118 119 if (timercmp(&(*it)->m_interval, &m_interval, <=)) { 120 /* 121 * Decrease our interval by those that come 122 * before us. 123 */ 124 timersub(&m_interval, &(*it)->m_interval, &m_interval); 125 } else { 126 /* 127 * Account for the time between the newly 128 * inserted event and those that follow. 129 */ 130 timersub(&(*it)->m_interval, &m_interval, 131 &(*it)->m_interval); 132 break; 133 } 134 } 135 s_activeCallouts.insert(it, this); 136 137 138 if (s_activeCallouts.front() == this) { 139 itimerval timerval = { {0, 0}, m_interval }; 140 141 setitimer(ITIMER_REAL, &timerval, NULL); 142 } 143 144 return (cancelled); 145 } 146 147 void 148 Callout::AlarmSignalHandler(int) 149 { 150 s_alarmFired = true; 151 ZfsDaemon::WakeEventLoop(); 152 } 153 154 void 155 Callout::ExpireCallouts() 156 { 157 if (!s_alarmFired) 158 return; 159 160 s_alarmFired = false; 161 if (s_activeCallouts.empty()) { 162 /* Callout removal/SIGALRM race was lost. */ 163 return; 164 } 165 166 /* 167 * Expire the first callout (the one we used to set the 168 * interval timer) as well as any callouts following that 169 * expire at the same time (have a zero interval from 170 * the callout before it). 171 */ 172 do { 173 Callout *cur(s_activeCallouts.front()); 174 s_activeCallouts.pop_front(); 175 cur->m_pending = false; 176 cur->m_func(cur->m_arg); 177 } while (!s_activeCallouts.empty() 178 && timerisset(&s_activeCallouts.front()->m_interval) == 0); 179 180 if (!s_activeCallouts.empty()) { 181 Callout *next(s_activeCallouts.front()); 182 itimerval timerval = { { 0, 0 }, next->m_interval }; 183 184 setitimer(ITIMER_REAL, &timerval, NULL); 185 } 186 } 187 188 timeval 189 Callout::TimeRemaining() const 190 { 191 /* 192 * Outline: Add the m_interval for each callout in s_activeCallouts 193 * ahead of this, except for the first callout. Add to that the result 194 * of getitimer (That's because the first callout stores its original 195 * interval setting while the timer is ticking). 196 */ 197 itimerval timervalToAlarm; 198 timeval timeToExpiry; 199 std::list<Callout *>::iterator it; 200 201 if (!IsPending()) { 202 timeToExpiry.tv_sec = INT_MAX; 203 timeToExpiry.tv_usec = 999999; /*maximum normalized value*/ 204 return (timeToExpiry); 205 } 206 207 timerclear(&timeToExpiry); 208 getitimer(ITIMER_REAL, &timervalToAlarm); 209 timeval& timeToAlarm = timervalToAlarm.it_value; 210 timeradd(&timeToExpiry, &timeToAlarm, &timeToExpiry); 211 212 it =s_activeCallouts.begin(); 213 it++; /*skip the first callout in the list*/ 214 for (; it != s_activeCallouts.end(); it++) { 215 timeradd(&timeToExpiry, &(*it)->m_interval, &timeToExpiry); 216 if ((*it) == this) 217 break; 218 } 219 return (timeToExpiry); 220 } 221