2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Subroutines to handle waiting for operator intervention
30 * or waiting for a Device to be released
32 * Code for wait_for_sysop() pulled from askdir.c
34 * Kern Sibbald, March 2005
40 #include "bacula.h" /* pull in global headers */
41 #include "stored.h" /* pull in Storage Deamon headers */
43 const int dbglvl = 400;
46 * Wait for SysOp to mount a tape on a specific device
48 * Returns: W_ERROR, W_TIMEOUT, W_POLL, W_MOUNT, or W_WAKE
50 int wait_for_sysop(DCR *dcr)
54 struct timespec timeout;
55 time_t last_heartbeat = 0;
56 time_t first_start = time(NULL);
60 DEVICE *dev = dcr->dev;
64 Dmsg1(dbglvl, "Enter blocked=%s\n", dev->print_blocked());
65 unmounted = is_device_unmounted(dev);
69 * Wait requested time (dev->rem_wait_sec). However, we also wake up every
70 * HB_TIME seconds and send a heartbeat to the FD and the Director
71 * to keep stateful firewalls from closing them down while waiting
74 add_wait = dev->rem_wait_sec;
75 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
76 add_wait = me->heartbeat_interval;
78 /* If the user did not unmount the tape and we are polling, ensure
79 * that we poll at the correct interval.
81 if (!unmounted && dev->vol_poll_interval && add_wait > dev->vol_poll_interval) {
82 add_wait = dev->vol_poll_interval;
86 Dmsg1(dbglvl, "blocked=%s\n", dev->print_blocked());
87 dev->dev_prev_blocked = dev->blocked();
88 dev->set_blocked(BST_WAITING_FOR_SYSOP); /* indicate waiting for mount */
91 for ( ; !job_canceled(jcr); ) {
92 time_t now, start, total_waited;
94 gettimeofday(&tv, &tz);
95 timeout.tv_nsec = tv.tv_usec * 1000;
96 timeout.tv_sec = tv.tv_sec + add_wait;
98 Dmsg4(dbglvl, "I'm going to sleep on device %s. HB=%d rem_wait=%d add_wait=%d\n",
99 dev->print_name(), (int)me->heartbeat_interval, dev->rem_wait_sec, add_wait);
101 /* Wait required time */
102 stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->m_mutex, &timeout);
103 Dmsg2(dbglvl, "Wokeup from sleep on device stat=%d blocked=%s\n", stat,
104 dev->print_blocked());
107 total_waited = now - first_start;
108 dev->rem_wait_sec -= (now - start);
110 /* Note, this always triggers the first time. We want that. */
111 if (me->heartbeat_interval) {
112 if (now - last_heartbeat >= me->heartbeat_interval) {
113 /* send heartbeats */
114 if (jcr->file_bsock) {
115 jcr->file_bsock->signal(BNET_HEARTBEAT);
116 Dmsg0(dbglvl, "Send heartbeat to FD.\n");
118 if (jcr->dir_bsock) {
119 jcr->dir_bsock->signal(BNET_HEARTBEAT);
121 last_heartbeat = now;
125 if (stat == EINVAL) {
127 Jmsg1(jcr, M_FATAL, 0, _("pthread timedwait error. ERR=%s\n"), be.bstrerror(stat));
128 stat = W_ERROR; /* error */
133 if (dev->rem_wait_sec <= 0) { /* on exceeding wait time return */
134 Dmsg0(dbglvl, "Exceed wait time.\n");
140 * Check if user unmounted the device while we were waiting
142 unmounted = is_device_unmounted(dev);
144 if (!unmounted && dev->vol_poll_interval &&
145 (total_waited >= dev->vol_poll_interval)) {
146 Dmsg1(dbglvl, "poll return in wait blocked=%s\n", dev->print_blocked());
147 dev->poll = true; /* returning a poll event */
152 * Check if user mounted the device while we were waiting
154 if (dev->blocked() == BST_MOUNT) { /* mount request ? */
155 Dmsg0(dbglvl, "Mounted return.\n");
161 * If we did not timeout, then some event happened, so
162 * return to check if state changed.
164 if (stat != ETIMEDOUT) {
166 Dmsg2(dbglvl, "Wake return. stat=%d. ERR=%s\n", stat, be.bstrerror(stat));
167 stat = W_WAKE; /* someone woke us */
172 * At this point, we know we woke up because of a timeout,
173 * that was due to a heartbeat, because any other reason would
174 * have caused us to return, so update the wait counters and continue.
176 add_wait = dev->rem_wait_sec;
177 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
178 add_wait = me->heartbeat_interval;
180 /* If the user did not unmount the tape and we are polling, ensure
181 * that we poll at the correct interval.
183 if (!unmounted && dev->vol_poll_interval &&
184 add_wait > dev->vol_poll_interval - total_waited) {
185 add_wait = dev->vol_poll_interval - total_waited;
193 dev->set_blocked(dev->dev_prev_blocked); /* restore entry state */
194 Dmsg1(dbglvl, "set %s\n", dev->print_blocked());
196 Dmsg1(dbglvl, "Exit blocked=%s\n", dev->print_blocked());
203 * Wait for any device to be released, then we return, so
204 * higher level code can rescan possible devices. Since there
205 * could be a job waiting for a drive to free up, we wait a maximum
206 * of 1 minute then retry just in case a broadcast was lost, and
207 * we return to rescan the devices.
209 * Returns: true if a device has changed state
210 * false if the total wait time has expired.
212 bool wait_for_device(JCR *jcr, int &retries)
216 struct timespec timeout;
219 const int max_wait_time = 1 * 60; /* wait 1 minute */
222 Dmsg0(dbglvl, "Enter wait_for_device\n");
223 P(device_release_mutex);
225 if (++retries % 5 == 0) {
226 /* Print message every 5 minutes */
227 Jmsg(jcr, M_MOUNT, 0, _("JobId=%s, Job %s waiting to reserve a device.\n"),
228 edit_uint64(jcr->JobId, ed1), jcr->Job);
231 gettimeofday(&tv, &tz);
232 timeout.tv_nsec = tv.tv_usec * 1000;
233 timeout.tv_sec = tv.tv_sec + max_wait_time;
235 Dmsg0(dbglvl, "Going to wait for a device.\n");
237 /* Wait required time */
238 stat = pthread_cond_timedwait(&wait_device_release, &device_release_mutex, &timeout);
239 Dmsg1(dbglvl, "Wokeup from sleep on device stat=%d\n", stat);
241 V(device_release_mutex);
242 Dmsg1(dbglvl, "Return from wait_device ok=%d\n", ok);
248 * The jcr timers are used for waiting on any device *
249 * Returns: true if time doubled
250 * false if max time expired
252 static bool double_jcr_wait_time(JCR *jcr)
254 jcr->wait_sec *= 2; /* double wait time */
255 if (jcr->wait_sec > jcr->max_wait) { /* but not longer than maxtime */
256 jcr->wait_sec = jcr->max_wait;
259 jcr->rem_wait_sec = jcr->wait_sec;
260 if (jcr->num_wait >= jcr->max_num_wait) {