+
+/*
+ * See if we can acquire all the necessary resources for the job (JCR)
+ *
+ * Returns: true if successful
+ * false if resource failure
+ */
+static bool acquire_resources(JCR *jcr)
+{
+ bool skip_this_jcr = false;
+
+ jcr->acquired_resource_locks = false;
+ if (jcr->rstore) {
+ Dmsg1(200, "Rstore=%s\n", jcr->rstore->name());
+ /*
+ * Let only one Restore/Verify job run at a time regardless
+ * of MaxConcurrentjobs.
+ */
+ if (jcr->rstore->NumConcurrentJobs == 0) {
+ jcr->rstore->NumConcurrentJobs = 1;
+ Dmsg0(200, "Set rncj=1\n");
+ } else {
+ Dmsg1(200, "Fail rncj=%d\n", jcr->rstore->NumConcurrentJobs);
+ set_jcr_job_status(jcr, JS_WaitStoreRes);
+ return false;
+ }
+ }
+
+ if (jcr->wstore) {
+ Dmsg1(200, "Wstore=%s\n", jcr->wstore->name());
+ if (jcr->rstore == jcr->wstore) { /* deadlock */
+ jcr->rstore->NumConcurrentJobs = 0; /* back out rstore */
+ Jmsg(jcr, M_FATAL, 0, _("Job canceled. Attempt to read and write same device.\n"
+ " Read storage \"%s\" (From %s) -- Write storage \"%s\" (From %s)\n"),
+ jcr->rstore->name(), jcr->rstore_source, jcr->wstore->name(), jcr->wstore_source);
+ set_jcr_job_status(jcr, JS_Canceled);
+ return false;
+ }
+ if (jcr->wstore->NumConcurrentJobs == 0 &&
+ jcr->wstore->NumConcurrentJobs < jcr->wstore->MaxConcurrentJobs) {
+ /* Simple case, first job */
+ jcr->wstore->NumConcurrentJobs = 1;
+ Dmsg0(200, "Set wncj=1\n");
+ } else if (jcr->wstore->NumConcurrentJobs < jcr->wstore->MaxConcurrentJobs) {
+ jcr->wstore->NumConcurrentJobs++;
+ Dmsg1(200, "Inc wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+ } else if (jcr->rstore) {
+ jcr->rstore->NumConcurrentJobs = 0; /* back out rstore */
+ Dmsg1(200, "Fail wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+ skip_this_jcr = true;
+ } else {
+ Dmsg1(200, "Fail wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+ skip_this_jcr = true;
+ }
+ }
+ if (skip_this_jcr) {
+ set_jcr_job_status(jcr, JS_WaitStoreRes);
+ return false;
+ }
+
+ if (jcr->client->NumConcurrentJobs < jcr->client->MaxConcurrentJobs) {
+ jcr->client->NumConcurrentJobs++;
+ } else {
+ /* Back out previous locks */
+ if (jcr->wstore) {
+ jcr->wstore->NumConcurrentJobs--;
+ Dmsg1(200, "Dec wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+ }
+ if (jcr->rstore) {
+ jcr->rstore->NumConcurrentJobs = 0;
+ Dmsg1(200, "Dec rncj=%d\n", jcr->rstore->NumConcurrentJobs);
+ }
+ set_jcr_job_status(jcr, JS_WaitClientRes);
+ return false;
+ }
+ if (jcr->job->NumConcurrentJobs < jcr->job->MaxConcurrentJobs) {
+ jcr->job->NumConcurrentJobs++;
+ } else {
+ /* Back out previous locks */
+ if (jcr->wstore) {
+ jcr->wstore->NumConcurrentJobs--;
+ Dmsg1(200, "Dec wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+ }
+ if (jcr->rstore) {
+ jcr->rstore->NumConcurrentJobs = 0;
+ Dmsg1(200, "Dec rncj=%d\n", jcr->rstore->NumConcurrentJobs);
+ }
+ jcr->client->NumConcurrentJobs--;
+ set_jcr_job_status(jcr, JS_WaitJobRes);
+ return false;
+ }
+
+ jcr->acquired_resource_locks = true;
+ return true;
+}