job-qmp.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * QMP interface for background jobs
  3. *
  4. * Copyright (c) 2011 IBM Corp.
  5. * Copyright (c) 2012, 2018 Red Hat, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/job.h"
  27. #include "qapi/qapi-commands-job.h"
  28. #include "qapi/error.h"
  29. #include "trace-root.h"
  30. /* Get a job using its ID and acquire its AioContext */
  31. static Job *find_job(const char *id, AioContext **aio_context, Error **errp)
  32. {
  33. Job *job;
  34. *aio_context = NULL;
  35. job = job_get(id);
  36. if (!job) {
  37. error_setg(errp, "Job not found");
  38. return NULL;
  39. }
  40. *aio_context = job->aio_context;
  41. aio_context_acquire(*aio_context);
  42. return job;
  43. }
  44. void qmp_job_cancel(const char *id, Error **errp)
  45. {
  46. AioContext *aio_context;
  47. Job *job = find_job(id, &aio_context, errp);
  48. if (!job) {
  49. return;
  50. }
  51. trace_qmp_job_cancel(job);
  52. job_user_cancel(job, true, errp);
  53. aio_context_release(aio_context);
  54. }
  55. void qmp_job_pause(const char *id, Error **errp)
  56. {
  57. AioContext *aio_context;
  58. Job *job = find_job(id, &aio_context, errp);
  59. if (!job) {
  60. return;
  61. }
  62. trace_qmp_job_pause(job);
  63. job_user_pause(job, errp);
  64. aio_context_release(aio_context);
  65. }
  66. void qmp_job_resume(const char *id, Error **errp)
  67. {
  68. AioContext *aio_context;
  69. Job *job = find_job(id, &aio_context, errp);
  70. if (!job) {
  71. return;
  72. }
  73. trace_qmp_job_resume(job);
  74. job_user_resume(job, errp);
  75. aio_context_release(aio_context);
  76. }
  77. void qmp_job_complete(const char *id, Error **errp)
  78. {
  79. AioContext *aio_context;
  80. Job *job = find_job(id, &aio_context, errp);
  81. if (!job) {
  82. return;
  83. }
  84. trace_qmp_job_complete(job);
  85. job_complete(job, errp);
  86. aio_context_release(aio_context);
  87. }
  88. void qmp_job_finalize(const char *id, Error **errp)
  89. {
  90. AioContext *aio_context;
  91. Job *job = find_job(id, &aio_context, errp);
  92. if (!job) {
  93. return;
  94. }
  95. trace_qmp_job_finalize(job);
  96. job_ref(job);
  97. job_finalize(job, errp);
  98. /*
  99. * Job's context might have changed via job_finalize (and job_txn_apply
  100. * automatically acquires the new one), so make sure we release the correct
  101. * one.
  102. */
  103. aio_context = job->aio_context;
  104. job_unref(job);
  105. aio_context_release(aio_context);
  106. }
  107. void qmp_job_dismiss(const char *id, Error **errp)
  108. {
  109. AioContext *aio_context;
  110. Job *job = find_job(id, &aio_context, errp);
  111. if (!job) {
  112. return;
  113. }
  114. trace_qmp_job_dismiss(job);
  115. job_dismiss(&job, errp);
  116. aio_context_release(aio_context);
  117. }
  118. static JobInfo *job_query_single(Job *job, Error **errp)
  119. {
  120. JobInfo *info;
  121. assert(!job_is_internal(job));
  122. info = g_new(JobInfo, 1);
  123. *info = (JobInfo) {
  124. .id = g_strdup(job->id),
  125. .type = job_type(job),
  126. .status = job->status,
  127. .current_progress = job->progress.current,
  128. .total_progress = job->progress.total,
  129. .has_error = !!job->err,
  130. .error = job->err ? \
  131. g_strdup(error_get_pretty(job->err)) : NULL,
  132. };
  133. return info;
  134. }
  135. JobInfoList *qmp_query_jobs(Error **errp)
  136. {
  137. JobInfoList *head = NULL, **p_next = &head;
  138. Job *job;
  139. for (job = job_next(NULL); job; job = job_next(job)) {
  140. JobInfoList *elem;
  141. AioContext *aio_context;
  142. if (job_is_internal(job)) {
  143. continue;
  144. }
  145. elem = g_new0(JobInfoList, 1);
  146. aio_context = job->aio_context;
  147. aio_context_acquire(aio_context);
  148. elem->value = job_query_single(job, errp);
  149. aio_context_release(aio_context);
  150. if (!elem->value) {
  151. g_free(elem);
  152. qapi_free_JobInfoList(head);
  153. return NULL;
  154. }
  155. *p_next = elem;
  156. p_next = &elem->next;
  157. }
  158. return head;
  159. }