]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/jobq.c
- Make JCR a class and implement inc_use_count() and
[bacula/bacula] / bacula / src / dird / jobq.c
1 /*
2  * Bacula job queue routines.
3  *
4  *  This code consists of three queues, the waiting_jobs
5  *  queue, where jobs are initially queued, the ready_jobs
6  *  queue, where jobs are placed when all the resources are
7  *  allocated and they can immediately be run, and the
8  *  running queue where jobs are placed when they are
9  *  running.
10  *
11  *  Kern Sibbald, July MMIII
12  *
13  *   Version $Id$
14  *
15  *  This code was adapted from the Bacula workq, which was
16  *    adapted from "Programming with POSIX Threads", by
17  *    David R. Butenhof
18  *
19  */
20 /*
21    Copyright (C) 2003-2005 Kern Sibbald
22
23    This program is free software; you can redistribute it and/or
24    modify it under the terms of the GNU General Public License as
25    published by the Free Software Foundation; either version 2 of
26    the License, or (at your option) any later version.
27
28    This program is distributed in the hope that it will be useful,
29    but WITHOUT ANY WARRANTY; without even the implied warranty of
30    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31    General Public License for more details.
32
33    You should have received a copy of the GNU General Public
34    License along with this program; if not, write to the Free
35    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
36    MA 02111-1307, USA.
37
38  */
39
40 #include "bacula.h"
41 #include "dird.h"
42
43 extern JCR *jobs;
44
45 /* Forward referenced functions */
46 extern "C" void *jobq_server(void *arg);
47 extern "C" void *sched_wait(void *arg);
48
49 static int  start_server(jobq_t *jq);
50 static bool acquire_resources(JCR *jcr);
51
52
53
54 /*
55  * Initialize a job queue
56  *
57  *  Returns: 0 on success
58  *           errno on failure
59  */
60 int jobq_init(jobq_t *jq, int threads, void *(*engine)(void *arg))
61 {
62    int stat;
63    jobq_item_t *item = NULL;
64
65    if ((stat = pthread_attr_init(&jq->attr)) != 0) {
66       berrno be;
67       Jmsg1(NULL, M_ERROR, 0, "pthread_attr_init: ERR=%s\n", be.strerror(stat));
68       return stat;
69    }
70    if ((stat = pthread_attr_setdetachstate(&jq->attr, PTHREAD_CREATE_DETACHED)) != 0) {
71       pthread_attr_destroy(&jq->attr);
72       return stat;
73    }
74    if ((stat = pthread_mutex_init(&jq->mutex, NULL)) != 0) {
75       berrno be;
76       Jmsg1(NULL, M_ERROR, 0, "pthread_mutex_init: ERR=%s\n", be.strerror(stat));
77       pthread_attr_destroy(&jq->attr);
78       return stat;
79    }
80    if ((stat = pthread_cond_init(&jq->work, NULL)) != 0) {
81       berrno be;
82       Jmsg1(NULL, M_ERROR, 0, "pthread_cond_init: ERR=%s\n", be.strerror(stat));
83       pthread_mutex_destroy(&jq->mutex);
84       pthread_attr_destroy(&jq->attr);
85       return stat;
86    }
87    jq->quit = false;
88    jq->max_workers = threads;         /* max threads to create */
89    jq->num_workers = 0;               /* no threads yet */
90    jq->idle_workers = 0;              /* no idle threads */
91    jq->engine = engine;               /* routine to run */
92    jq->valid = JOBQ_VALID;
93    /* Initialize the job queues */
94    jq->waiting_jobs = New(dlist(item, &item->link));
95    jq->running_jobs = New(dlist(item, &item->link));
96    jq->ready_jobs = New(dlist(item, &item->link));
97    return 0;
98 }
99
100 /*
101  * Destroy the job queue
102  *
103  * Returns: 0 on success
104  *          errno on failure
105  */
106 int jobq_destroy(jobq_t *jq)
107 {
108    int stat, stat1, stat2;
109
110    if (jq->valid != JOBQ_VALID) {
111       return EINVAL;
112    }
113    if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
114       berrno be;
115       Jmsg1(NULL, M_ERROR, 0, "pthread_mutex_lock: ERR=%s\n", be.strerror(stat));
116       return stat;
117    }
118    jq->valid = 0;                      /* prevent any more operations */
119
120    /* 
121     * If any threads are active, wake them 
122     */
123    if (jq->num_workers > 0) {
124       jq->quit = true;
125       if (jq->idle_workers) {
126          if ((stat = pthread_cond_broadcast(&jq->work)) != 0) {
127             berrno be;
128             Jmsg1(NULL, M_ERROR, 0, "pthread_cond_broadcast: ERR=%s\n", be.strerror(stat));
129             pthread_mutex_unlock(&jq->mutex);
130             return stat;
131          }
132       }
133       while (jq->num_workers > 0) {
134          if ((stat = pthread_cond_wait(&jq->work, &jq->mutex)) != 0) {
135             berrno be;
136             Jmsg1(NULL, M_ERROR, 0, "pthread_cond_wait: ERR=%s\n", be.strerror(stat));
137             pthread_mutex_unlock(&jq->mutex);
138             return stat;
139          }
140       }
141    }
142    if ((stat = pthread_mutex_unlock(&jq->mutex)) != 0) {
143       berrno be;
144       Jmsg1(NULL, M_ERROR, 0, "pthread_mutex_unlock: ERR=%s\n", be.strerror(stat));
145       return stat;
146    }
147    stat  = pthread_mutex_destroy(&jq->mutex);
148    stat1 = pthread_cond_destroy(&jq->work);
149    stat2 = pthread_attr_destroy(&jq->attr);
150    delete jq->waiting_jobs;
151    delete jq->running_jobs;
152    delete jq->ready_jobs;
153    return (stat != 0 ? stat : (stat1 != 0 ? stat1 : stat2));
154 }
155
156 struct wait_pkt {
157    JCR *jcr;
158    jobq_t *jq;
159 };
160
161 /*
162  * Wait until schedule time arrives before starting. Normally
163  *  this routine is only used for jobs started from the console
164  *  for which the user explicitly specified a start time. Otherwise
165  *  most jobs are put into the job queue only when their
166  *  scheduled time arives.
167  */
168 extern "C"
169 void *sched_wait(void *arg)
170 {
171    JCR *jcr = ((wait_pkt *)arg)->jcr;
172    jobq_t *jq = ((wait_pkt *)arg)->jq;
173
174    Dmsg0(2300, "Enter sched_wait.\n");
175    free(arg);
176    time_t wtime = jcr->sched_time - time(NULL);
177    set_jcr_job_status(jcr, JS_WaitStartTime);
178    /* Wait until scheduled time arrives */
179    if (wtime > 0) {
180       Jmsg(jcr, M_INFO, 0, _("Job %s waiting %d seconds for scheduled start time.\n"),
181          jcr->Job, wtime);
182    }
183    /* Check every 30 seconds if canceled */
184    while (wtime > 0) {
185       Dmsg2(2300, "Waiting on sched time, jobid=%d secs=%d\n", jcr->JobId, wtime);
186       if (wtime > 30) {
187          wtime = 30;
188       }
189       bmicrosleep(wtime, 0);
190       if (job_canceled(jcr)) {
191          break;
192       }
193       wtime = jcr->sched_time - time(NULL);
194    }
195    P(jcr->mutex);                     /* lock jcr */
196    jobq_add(jq, jcr);
197    V(jcr->mutex);
198    free_jcr(jcr);                     /* we are done with jcr */
199    Dmsg0(2300, "Exit sched_wait\n");
200    return NULL;
201 }
202
203 /*
204  *  Add a job to the queue
205  *    jq is a queue that was created with jobq_init
206  *
207  *  On entry jcr->mutex must be locked.
208  *
209  */
210 int jobq_add(jobq_t *jq, JCR *jcr)
211 {
212    int stat;
213    jobq_item_t *item, *li;
214    bool inserted = false;
215    time_t wtime = jcr->sched_time - time(NULL);
216    pthread_t id;
217    wait_pkt *sched_pkt;
218
219    Dmsg3(2300, "jobq_add jobid=%d jcr=0x%x use_count=%d\n", jcr->JobId, jcr, jcr->use_count);
220    if (jq->valid != JOBQ_VALID) {
221       Jmsg0(jcr, M_ERROR, 0, "Jobq_add queue not initialized.\n");
222       return EINVAL;
223    }
224
225    jcr->use_count++;                  /* mark jcr in use by us */
226    Dmsg3(2300, "jobq_add jobid=%d jcr=0x%x use_count=%d\n", jcr->JobId, jcr, jcr->use_count);
227    if (!job_canceled(jcr) && wtime > 0) {
228       set_thread_concurrency(jq->max_workers + 2);
229       sched_pkt = (wait_pkt *)malloc(sizeof(wait_pkt));
230       sched_pkt->jcr = jcr;
231       sched_pkt->jq = jq;
232       stat = pthread_create(&id, &jq->attr, sched_wait, (void *)sched_pkt);        
233       if (stat != 0) {                /* thread not created */
234          berrno be;
235          Jmsg1(jcr, M_ERROR, 0, "pthread_thread_create: ERR=%s\n", be.strerror(stat));
236       }
237       return stat;
238    }
239
240    if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
241       berrno be;
242       Jmsg1(jcr, M_ERROR, 0, "pthread_mutex_lock: ERR=%s\n", be.strerror(stat));
243       jcr->use_count--;               /* release jcr */
244       return stat;
245    }
246
247    if ((item = (jobq_item_t *)malloc(sizeof(jobq_item_t))) == NULL) {
248       jcr->use_count--;               /* release jcr */
249       return ENOMEM;
250    }
251    item->jcr = jcr;
252
253    if (job_canceled(jcr)) {
254       /* Add job to ready queue so that it is canceled quickly */
255       jq->ready_jobs->prepend(item);
256       Dmsg1(2300, "Prepended job=%d to ready queue\n", jcr->JobId);
257    } else {
258       /* Add this job to the wait queue in priority sorted order */
259       foreach_dlist(li, jq->waiting_jobs) {
260          Dmsg2(2300, "waiting item jobid=%d priority=%d\n",
261             li->jcr->JobId, li->jcr->JobPriority);
262          if (li->jcr->JobPriority > jcr->JobPriority) {
263             jq->waiting_jobs->insert_before(item, li);
264             Dmsg2(2300, "insert_before jobid=%d before waiting job=%d\n",
265                li->jcr->JobId, jcr->JobId);
266             inserted = true;
267             break;
268          }
269       }
270       /* If not jobs in wait queue, append it */
271       if (!inserted) {
272          jq->waiting_jobs->append(item);
273          Dmsg1(2300, "Appended item jobid=%d to waiting queue\n", jcr->JobId);
274       }
275    }
276
277    /* Ensure that at least one server looks at the queue. */
278    stat = start_server(jq);
279
280    pthread_mutex_unlock(&jq->mutex);
281    Dmsg0(2300, "Return jobq_add\n");
282    return stat;
283 }
284
285 /*
286  *  Remove a job from the job queue. Used only by cancel_job().
287  *    jq is a queue that was created with jobq_init
288  *    work_item is an element of work
289  *
290  *   Note, it is "removed" from the job queue.
291  *    If you want to cancel it, you need to provide some external means
292  *    of doing so (e.g. pthread_kill()).
293  */
294 int jobq_remove(jobq_t *jq, JCR *jcr)
295 {
296    int stat;
297    bool found = false;
298    jobq_item_t *item;
299
300    Dmsg2(2300, "jobq_remove jobid=%d jcr=0x%x\n", jcr->JobId, jcr);
301    if (jq->valid != JOBQ_VALID) {
302       return EINVAL;
303    }
304
305    if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
306       berrno be;
307       Jmsg1(NULL, M_ERROR, 0, "pthread_mutex_lock: ERR=%s\n", be.strerror(stat));
308       return stat;
309    }
310
311    foreach_dlist(item, jq->waiting_jobs) {
312       if (jcr == item->jcr) {
313          found = true;
314          break;
315       }
316    }
317    if (!found) {
318       pthread_mutex_unlock(&jq->mutex);
319       Dmsg2(2300, "jobq_remove jobid=%d jcr=0x%x not in wait queue\n", jcr->JobId, jcr);
320       return EINVAL;
321    }
322
323    /* Move item to be the first on the list */
324    jq->waiting_jobs->remove(item);
325    jq->ready_jobs->prepend(item);
326    Dmsg2(2300, "jobq_remove jobid=%d jcr=0x%x moved to ready queue\n", jcr->JobId, jcr);
327
328    stat = start_server(jq);
329
330    pthread_mutex_unlock(&jq->mutex);
331    Dmsg0(2300, "Return jobq_remove\n");
332    return stat;
333 }
334
335
336 /*
337  * Start the server thread if it isn't already running
338  */
339 static int start_server(jobq_t *jq)
340 {
341    int stat = 0;
342    pthread_t id;
343
344    /*
345     * if any threads are idle, wake one --                
346     *   actually we do a broadcast because on /lib/tls 
347     *   these signals seem to get lost from time to time.
348     */
349    if (jq->idle_workers > 0) {
350       Dmsg0(2300, "Signal worker to wake up\n");
351       if ((stat = pthread_cond_broadcast(&jq->work)) != 0) {
352          berrno be;
353          Jmsg1(NULL, M_ERROR, 0, "pthread_cond_signal: ERR=%s\n", be.strerror(stat));
354          return stat;
355       }
356    } else if (jq->num_workers < jq->max_workers) {
357       Dmsg0(2300, "Create worker thread\n");
358       /* No idle threads so create a new one */
359       set_thread_concurrency(jq->max_workers + 1);
360       if ((stat = pthread_create(&id, &jq->attr, jobq_server, (void *)jq)) != 0) {
361          berrno be;
362          Jmsg1(NULL, M_ERROR, 0, "pthread_create: ERR=%s\n", be.strerror(stat));
363          return stat;
364       }
365    }
366    return stat;
367 }
368
369
370 /*
371  * This is the worker thread that serves the job queue.
372  * When all the resources are acquired for the job,
373  *  it will call the user's engine.
374  */
375 extern "C"
376 void *jobq_server(void *arg)
377 {
378    struct timespec timeout;
379    jobq_t *jq = (jobq_t *)arg;
380    jobq_item_t *je;                   /* job entry in queue */
381    int stat;
382    bool timedout = false;
383    bool work = true;
384
385    Dmsg0(2300, "Start jobq_server\n");
386    if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
387       berrno be;
388       Jmsg1(NULL, M_ERROR, 0, "pthread_mutex_lock: ERR=%s\n", be.strerror(stat));
389       return NULL;
390    }
391    jq->num_workers++;
392
393    for (;;) {
394       struct timeval tv;
395       struct timezone tz;
396
397       Dmsg0(2300, "Top of for loop\n");
398       if (!work && !jq->quit) {
399          gettimeofday(&tv, &tz);
400          timeout.tv_nsec = 0;
401          timeout.tv_sec = tv.tv_sec + 4;
402
403          while (!jq->quit) {
404             /*
405              * Wait 4 seconds, then if no more work, exit
406              */
407             Dmsg0(2300, "pthread_cond_timedwait()\n");
408             stat = pthread_cond_timedwait(&jq->work, &jq->mutex, &timeout);
409             if (stat == ETIMEDOUT) {
410                Dmsg0(2300, "timedwait timedout.\n");
411                timedout = true;
412                break;
413             } else if (stat != 0) {
414                /* This shouldn't happen */
415                Dmsg0(2300, "This shouldn't happen\n");
416                jq->num_workers--;
417                pthread_mutex_unlock(&jq->mutex);
418                return NULL;
419             }
420             break;
421          }
422       }
423       /*
424        * If anything is in the ready queue, run it
425        */
426       Dmsg0(2300, "Checking ready queue.\n");
427       while (!jq->ready_jobs->empty() && !jq->quit) {
428          JCR *jcr;
429          je = (jobq_item_t *)jq->ready_jobs->first();
430          jcr = je->jcr;
431          jq->ready_jobs->remove(je);
432          if (!jq->ready_jobs->empty()) {
433             Dmsg0(2300, "ready queue not empty start server\n");
434             if (start_server(jq) != 0) {
435                jq->num_workers--;
436                pthread_mutex_unlock(&jq->mutex);
437                return NULL;
438             }
439          }
440          jq->running_jobs->append(je);
441          Dmsg1(2300, "Took jobid=%d from ready and appended to run\n", jcr->JobId);
442
443          /* Release job queue lock */
444          V(jq->mutex);
445
446          /* Call user's routine here */
447          Dmsg1(2300, "Calling user engine for jobid=%d\n", jcr->JobId);
448          jq->engine(je->jcr);
449
450          Dmsg1(2300, "Back from user engine jobid=%d.\n", jcr->JobId);
451
452          /* Reacquire job queue lock */
453          P(jq->mutex);
454          Dmsg0(200, "Done lock mutex after running job. Release locks.\n");
455          jq->running_jobs->remove(je);
456          /*
457           * Release locks if acquired. Note, they will not have
458           *  been acquired for jobs canceled before they were
459           *  put into the ready queue.
460           */
461          if (jcr->acquired_resource_locks) {
462             jcr->store->NumConcurrentJobs--;
463             jcr->client->NumConcurrentJobs--;
464             jcr->job->NumConcurrentJobs--;
465          }
466
467          /*
468           * Reschedule the job if necessary and requested
469           */
470          if (jcr->job->RescheduleOnError &&
471              jcr->JobStatus != JS_Terminated &&
472              jcr->JobStatus != JS_Canceled &&
473              jcr->job->RescheduleTimes > 0 &&
474              jcr->reschedule_count < jcr->job->RescheduleTimes) {
475              char dt[50];
476
477              /*
478               * Reschedule this job by cleaning it up, but
479               *  reuse the same JobId if possible.
480               */
481             jcr->reschedule_count++;
482             jcr->sched_time = time(NULL) + jcr->job->RescheduleInterval;
483             Dmsg2(2300, "Rescheduled Job %s to re-run in %d seconds.\n", jcr->Job,
484                (int)jcr->job->RescheduleInterval);
485             bstrftime(dt, sizeof(dt), time(NULL));
486             Jmsg(jcr, M_INFO, 0, _("Rescheduled Job %s at %s to re-run in %d seconds.\n"),
487                jcr->Job, dt, (int)jcr->job->RescheduleInterval);
488             dird_free_jcr_pointers(jcr);     /* partial cleanup old stuff */
489             jcr->JobStatus = JS_WaitStartTime;
490             jcr->SDJobStatus = 0;
491             if (jcr->JobBytes == 0) {
492                Dmsg1(2300, "Requeue job=%d\n", jcr->JobId);
493                jcr->JobStatus = JS_WaitStartTime;
494                V(jq->mutex);
495                jobq_add(jq, jcr);     /* queue the job to run again */
496                P(jq->mutex);
497                free(je);              /* free the job entry */
498                continue;              /* look for another job to run */
499             }
500             /*
501              * Something was actually backed up, so we cannot reuse
502              *   the old JobId or there will be database record
503              *   conflicts.  We now create a new job, copying the
504              *   appropriate fields.
505              */
506             JCR *njcr = new_jcr(sizeof(JCR), dird_free_jcr);
507             set_jcr_defaults(njcr, jcr->job);
508             njcr->reschedule_count = jcr->reschedule_count;
509             njcr->JobLevel = jcr->JobLevel;
510             njcr->JobStatus = jcr->JobStatus;
511             copy_storage(njcr, jcr);
512             njcr->messages = jcr->messages;
513             Dmsg0(2300, "Call to run new job\n");
514             V(jq->mutex);
515             run_job(njcr);            /* This creates a "new" job */
516             free_jcr(njcr);           /* release "new" jcr */
517             P(jq->mutex);
518             Dmsg0(2300, "Back from running new job.\n");
519          }
520          /* Clean up and release old jcr */
521          if (jcr->db) {
522             db_close_database(jcr, jcr->db);
523             jcr->db = NULL;
524          }
525          Dmsg2(2300, "====== Termination job=%d use_cnt=%d\n", jcr->JobId, jcr->use_count);
526          jcr->SDJobStatus = 0;
527          V(jq->mutex);                /* release internal lock */
528          free_jcr(jcr);
529          free(je);                    /* release job entry */
530          P(jq->mutex);                /* reacquire job queue lock */
531       }
532       /*
533        * If any job in the wait queue can be run,
534        *  move it to the ready queue
535        */
536       Dmsg0(2300, "Done check ready, now check wait queue.\n");
537       if (!jq->waiting_jobs->empty() && !jq->quit) {
538          int Priority;
539          je = (jobq_item_t *)jq->waiting_jobs->first();
540          jobq_item_t *re = (jobq_item_t *)jq->running_jobs->first();
541          if (re) {
542             Priority = re->jcr->JobPriority;
543             Dmsg2(2300, "JobId %d is running. Look for pri=%d\n", re->jcr->JobId, Priority);
544          } else {
545             Priority = je->jcr->JobPriority;
546             Dmsg1(2300, "No job running. Look for Job pri=%d\n", Priority);
547          }
548          /*
549           * Walk down the list of waiting jobs and attempt
550           *   to acquire the resources it needs.
551           */
552          for ( ; je;  ) {
553             /* je is current job item on the queue, jn is the next one */
554             JCR *jcr = je->jcr;
555             jobq_item_t *jn = (jobq_item_t *)jq->waiting_jobs->next(je);
556
557             Dmsg3(2300, "Examining Job=%d JobPri=%d want Pri=%d\n",
558                jcr->JobId, jcr->JobPriority, Priority);
559
560             /* Take only jobs of correct Priority */
561             if (jcr->JobPriority != Priority) {
562                set_jcr_job_status(jcr, JS_WaitPriority);
563                break;
564             }
565
566             if (!acquire_resources(jcr)) {
567                je = jn;            /* point to next waiting job */
568                continue;
569             }
570
571             /* Got all locks, now remove it from wait queue and append it
572              *   to the ready queue
573              */
574             jcr->acquired_resource_locks = true;
575             jq->waiting_jobs->remove(je);
576             jq->ready_jobs->append(je);
577             Dmsg1(2300, "moved JobId=%d from wait to ready queue\n", je->jcr->JobId);
578             je = jn;                  /* Point to next waiting job */
579          } /* end for loop */
580
581       } /* end if */
582
583       Dmsg0(2300, "Done checking wait queue.\n");
584       /*
585        * If no more ready work and we are asked to quit, then do it
586        */
587       if (jq->ready_jobs->empty() && jq->quit) {
588          jq->num_workers--;
589          if (jq->num_workers == 0) {
590             Dmsg0(2300, "Wake up destroy routine\n");
591             /* Wake up destroy routine if he is waiting */
592             pthread_cond_broadcast(&jq->work);
593          }
594          break;
595       }
596       Dmsg0(2300, "Check for work request\n");
597       /*
598        * If no more work requests, and we waited long enough, quit
599        */
600       Dmsg2(2300, "timedout=%d read empty=%d\n", timedout,
601          jq->ready_jobs->empty());
602       if (jq->ready_jobs->empty() && timedout) {
603          Dmsg0(2300, "break big loop\n");
604          jq->num_workers--;
605          break;
606       }
607
608       work = !jq->ready_jobs->empty() || !jq->waiting_jobs->empty();
609       if (work) {
610          /*
611           * If a job is waiting on a Resource, don't consume all
612           *   the CPU time looping looking for work, and even more
613           *   important, release the lock so that a job that has
614           *   terminated can give us the resource.
615           */
616          V(jq->mutex);
617          bmicrosleep(2, 0);              /* pause for 2 seconds */
618          P(jq->mutex);
619          /* Recompute work as something may have changed in last 2 secs */
620          work = !jq->ready_jobs->empty() || !jq->waiting_jobs->empty();
621       }
622       Dmsg1(2300, "Loop again. work=%d\n", work);
623    } /* end of big for loop */
624
625    Dmsg0(200, "unlock mutex\n");
626    V(jq->mutex);
627    Dmsg0(2300, "End jobq_server\n");
628    return NULL;
629 }
630
631 /*
632  * See if we can acquire all the necessary resources for the job (JCR)
633  *
634  *  Returns: true  if successful
635  *           false if resource failure
636  */
637 static bool acquire_resources(JCR *jcr)
638 {
639    bool skip_this_jcr = false;
640
641    if (jcr->JobType == JT_RESTORE || jcr->JobType == JT_VERIFY) {
642       /* 
643        * Let only one Restore/verify job run at a time regardless
644        *   of MaxConcurrentJobs.
645        */ 
646       if (jcr->store->NumConcurrentJobs == 0) {
647          jcr->store->NumConcurrentJobs = 1;
648       } else {
649          set_jcr_job_status(jcr, JS_WaitStoreRes);
650          return false;
651       }
652    /* We are not doing a Restore or Verify */
653    } else if (jcr->store->NumConcurrentJobs == 0 &&
654               jcr->store->NumConcurrentJobs < jcr->store->MaxConcurrentJobs) {
655        /* Simple case, first job */
656        jcr->store->NumConcurrentJobs = 1;
657    } else if (jcr->store->NumConcurrentJobs < jcr->store->MaxConcurrentJobs) {
658        jcr->store->NumConcurrentJobs++;
659    } else {
660       skip_this_jcr = true;
661    }
662    if (skip_this_jcr) {
663       set_jcr_job_status(jcr, JS_WaitStoreRes);
664       return false;
665    }
666
667    if (jcr->client->NumConcurrentJobs < jcr->client->MaxConcurrentJobs) {
668       jcr->client->NumConcurrentJobs++;
669    } else {
670       /* Back out previous locks */
671       jcr->store->NumConcurrentJobs--;
672       set_jcr_job_status(jcr, JS_WaitClientRes);
673       return false;
674    }
675    if (jcr->job->NumConcurrentJobs < jcr->job->MaxConcurrentJobs) {
676       jcr->job->NumConcurrentJobs++;
677    } else {
678       /* Back out previous locks */
679       jcr->store->NumConcurrentJobs--;
680       jcr->client->NumConcurrentJobs--;
681       set_jcr_job_status(jcr, JS_WaitJobRes);
682       return false;
683    }
684    /* Check actual device availability */
685    /* ***FIXME****/
686    return true;
687 }