You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

blockjob.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * QEMU System Emulator block driver
  3. *
  4. * Copyright (c) 2011 IBM Corp.
  5. * Copyright (c) 2012 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 "block/block.h"
  27. #include "block/blockjob_int.h"
  28. #include "block/block_int.h"
  29. #include "block/trace.h"
  30. #include "sysemu/block-backend.h"
  31. #include "qapi/error.h"
  32. #include "qapi/qapi-events-block-core.h"
  33. #include "qapi/qmp/qerror.h"
  34. #include "qemu/coroutine.h"
  35. #include "qemu/main-loop.h"
  36. #include "qemu/timer.h"
  37. /*
  38. * The block job API is composed of two categories of functions.
  39. *
  40. * The first includes functions used by the monitor. The monitor is
  41. * peculiar in that it accesses the block job list with block_job_get, and
  42. * therefore needs consistency across block_job_get and the actual operation
  43. * (e.g. block_job_set_speed). The consistency is achieved with
  44. * aio_context_acquire/release. These functions are declared in blockjob.h.
  45. *
  46. * The second includes functions used by the block job drivers and sometimes
  47. * by the core block layer. These do not care about locking, because the
  48. * whole coroutine runs under the AioContext lock, and are declared in
  49. * blockjob_int.h.
  50. */
  51. static bool is_block_job(Job *job)
  52. {
  53. return job_type(job) == JOB_TYPE_BACKUP ||
  54. job_type(job) == JOB_TYPE_COMMIT ||
  55. job_type(job) == JOB_TYPE_MIRROR ||
  56. job_type(job) == JOB_TYPE_STREAM;
  57. }
  58. BlockJob *block_job_next(BlockJob *bjob)
  59. {
  60. Job *job = bjob ? &bjob->job : NULL;
  61. do {
  62. job = job_next(job);
  63. } while (job && !is_block_job(job));
  64. return job ? container_of(job, BlockJob, job) : NULL;
  65. }
  66. BlockJob *block_job_get(const char *id)
  67. {
  68. Job *job = job_get(id);
  69. if (job && is_block_job(job)) {
  70. return container_of(job, BlockJob, job);
  71. } else {
  72. return NULL;
  73. }
  74. }
  75. void block_job_free(Job *job)
  76. {
  77. BlockJob *bjob = container_of(job, BlockJob, job);
  78. block_job_remove_all_bdrv(bjob);
  79. blk_unref(bjob->blk);
  80. error_free(bjob->blocker);
  81. }
  82. static char *child_job_get_parent_desc(BdrvChild *c)
  83. {
  84. BlockJob *job = c->opaque;
  85. return g_strdup_printf("%s job '%s'", job_type_str(&job->job), job->job.id);
  86. }
  87. static void child_job_drained_begin(BdrvChild *c)
  88. {
  89. BlockJob *job = c->opaque;
  90. job_pause(&job->job);
  91. }
  92. static bool child_job_drained_poll(BdrvChild *c)
  93. {
  94. BlockJob *bjob = c->opaque;
  95. Job *job = &bjob->job;
  96. const BlockJobDriver *drv = block_job_driver(bjob);
  97. /* An inactive or completed job doesn't have any pending requests. Jobs
  98. * with !job->busy are either already paused or have a pause point after
  99. * being reentered, so no job driver code will run before they pause. */
  100. if (!job->busy || job_is_completed(job)) {
  101. return false;
  102. }
  103. /* Otherwise, assume that it isn't fully stopped yet, but allow the job to
  104. * override this assumption. */
  105. if (drv->drained_poll) {
  106. return drv->drained_poll(bjob);
  107. } else {
  108. return true;
  109. }
  110. }
  111. static void child_job_drained_end(BdrvChild *c, int *drained_end_counter)
  112. {
  113. BlockJob *job = c->opaque;
  114. job_resume(&job->job);
  115. }
  116. static bool child_job_can_set_aio_ctx(BdrvChild *c, AioContext *ctx,
  117. GSList **ignore, Error **errp)
  118. {
  119. BlockJob *job = c->opaque;
  120. GSList *l;
  121. for (l = job->nodes; l; l = l->next) {
  122. BdrvChild *sibling = l->data;
  123. if (!bdrv_child_can_set_aio_context(sibling, ctx, ignore, errp)) {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. static void child_job_set_aio_ctx(BdrvChild *c, AioContext *ctx,
  130. GSList **ignore)
  131. {
  132. BlockJob *job = c->opaque;
  133. GSList *l;
  134. for (l = job->nodes; l; l = l->next) {
  135. BdrvChild *sibling = l->data;
  136. if (g_slist_find(*ignore, sibling)) {
  137. continue;
  138. }
  139. *ignore = g_slist_prepend(*ignore, sibling);
  140. bdrv_set_aio_context_ignore(sibling->bs, ctx, ignore);
  141. }
  142. job->job.aio_context = ctx;
  143. }
  144. static const BdrvChildRole child_job = {
  145. .get_parent_desc = child_job_get_parent_desc,
  146. .drained_begin = child_job_drained_begin,
  147. .drained_poll = child_job_drained_poll,
  148. .drained_end = child_job_drained_end,
  149. .can_set_aio_ctx = child_job_can_set_aio_ctx,
  150. .set_aio_ctx = child_job_set_aio_ctx,
  151. .stay_at_node = true,
  152. };
  153. void block_job_remove_all_bdrv(BlockJob *job)
  154. {
  155. /*
  156. * bdrv_root_unref_child() may reach child_job_[can_]set_aio_ctx(),
  157. * which will also traverse job->nodes, so consume the list one by
  158. * one to make sure that such a concurrent access does not attempt
  159. * to process an already freed BdrvChild.
  160. */
  161. while (job->nodes) {
  162. GSList *l = job->nodes;
  163. BdrvChild *c = l->data;
  164. job->nodes = l->next;
  165. bdrv_op_unblock_all(c->bs, job->blocker);
  166. bdrv_root_unref_child(c);
  167. g_slist_free_1(l);
  168. }
  169. }
  170. bool block_job_has_bdrv(BlockJob *job, BlockDriverState *bs)
  171. {
  172. GSList *el;
  173. for (el = job->nodes; el; el = el->next) {
  174. BdrvChild *c = el->data;
  175. if (c->bs == bs) {
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
  182. uint64_t perm, uint64_t shared_perm, Error **errp)
  183. {
  184. BdrvChild *c;
  185. bdrv_ref(bs);
  186. if (job->job.aio_context != qemu_get_aio_context()) {
  187. aio_context_release(job->job.aio_context);
  188. }
  189. c = bdrv_root_attach_child(bs, name, &child_job, job->job.aio_context,
  190. perm, shared_perm, job, errp);
  191. if (job->job.aio_context != qemu_get_aio_context()) {
  192. aio_context_acquire(job->job.aio_context);
  193. }
  194. if (c == NULL) {
  195. return -EPERM;
  196. }
  197. job->nodes = g_slist_prepend(job->nodes, c);
  198. bdrv_op_block_all(bs, job->blocker);
  199. return 0;
  200. }
  201. static void block_job_on_idle(Notifier *n, void *opaque)
  202. {
  203. aio_wait_kick();
  204. }
  205. bool block_job_is_internal(BlockJob *job)
  206. {
  207. return (job->job.id == NULL);
  208. }
  209. const BlockJobDriver *block_job_driver(BlockJob *job)
  210. {
  211. return container_of(job->job.driver, BlockJobDriver, job_driver);
  212. }
  213. /* Assumes the job_mutex is held */
  214. static bool job_timer_pending(Job *job)
  215. {
  216. return timer_pending(&job->sleep_timer);
  217. }
  218. void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
  219. {
  220. int64_t old_speed = job->speed;
  221. if (job_apply_verb(&job->job, JOB_VERB_SET_SPEED, errp)) {
  222. return;
  223. }
  224. if (speed < 0) {
  225. error_setg(errp, QERR_INVALID_PARAMETER, "speed");
  226. return;
  227. }
  228. ratelimit_set_speed(&job->limit, speed, BLOCK_JOB_SLICE_TIME);
  229. job->speed = speed;
  230. if (speed && speed <= old_speed) {
  231. return;
  232. }
  233. /* kick only if a timer is pending */
  234. job_enter_cond(&job->job, job_timer_pending);
  235. }
  236. int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n)
  237. {
  238. if (!job->speed) {
  239. return 0;
  240. }
  241. return ratelimit_calculate_delay(&job->limit, n);
  242. }
  243. BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
  244. {
  245. BlockJobInfo *info;
  246. if (block_job_is_internal(job)) {
  247. error_setg(errp, "Cannot query QEMU internal jobs");
  248. return NULL;
  249. }
  250. info = g_new0(BlockJobInfo, 1);
  251. info->type = g_strdup(job_type_str(&job->job));
  252. info->device = g_strdup(job->job.id);
  253. info->busy = atomic_read(&job->job.busy);
  254. info->paused = job->job.pause_count > 0;
  255. info->offset = job->job.progress_current;
  256. info->len = job->job.progress_total;
  257. info->speed = job->speed;
  258. info->io_status = job->iostatus;
  259. info->ready = job_is_ready(&job->job),
  260. info->status = job->job.status;
  261. info->auto_finalize = job->job.auto_finalize;
  262. info->auto_dismiss = job->job.auto_dismiss;
  263. info->has_error = job->job.ret != 0;
  264. info->error = job->job.ret ? g_strdup(strerror(-job->job.ret)) : NULL;
  265. return info;
  266. }
  267. static void block_job_iostatus_set_err(BlockJob *job, int error)
  268. {
  269. if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
  270. job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
  271. BLOCK_DEVICE_IO_STATUS_FAILED;
  272. }
  273. }
  274. static void block_job_event_cancelled(Notifier *n, void *opaque)
  275. {
  276. BlockJob *job = opaque;
  277. if (block_job_is_internal(job)) {
  278. return;
  279. }
  280. qapi_event_send_block_job_cancelled(job_type(&job->job),
  281. job->job.id,
  282. job->job.progress_total,
  283. job->job.progress_current,
  284. job->speed);
  285. }
  286. static void block_job_event_completed(Notifier *n, void *opaque)
  287. {
  288. BlockJob *job = opaque;
  289. const char *msg = NULL;
  290. if (block_job_is_internal(job)) {
  291. return;
  292. }
  293. if (job->job.ret < 0) {
  294. msg = strerror(-job->job.ret);
  295. }
  296. qapi_event_send_block_job_completed(job_type(&job->job),
  297. job->job.id,
  298. job->job.progress_total,
  299. job->job.progress_current,
  300. job->speed,
  301. !!msg,
  302. msg);
  303. }
  304. static void block_job_event_pending(Notifier *n, void *opaque)
  305. {
  306. BlockJob *job = opaque;
  307. if (block_job_is_internal(job)) {
  308. return;
  309. }
  310. qapi_event_send_block_job_pending(job_type(&job->job),
  311. job->job.id);
  312. }
  313. static void block_job_event_ready(Notifier *n, void *opaque)
  314. {
  315. BlockJob *job = opaque;
  316. if (block_job_is_internal(job)) {
  317. return;
  318. }
  319. qapi_event_send_block_job_ready(job_type(&job->job),
  320. job->job.id,
  321. job->job.progress_total,
  322. job->job.progress_current,
  323. job->speed);
  324. }
  325. /*
  326. * API for block job drivers and the block layer. These functions are
  327. * declared in blockjob_int.h.
  328. */
  329. void *block_job_create(const char *job_id, const BlockJobDriver *driver,
  330. JobTxn *txn, BlockDriverState *bs, uint64_t perm,
  331. uint64_t shared_perm, int64_t speed, int flags,
  332. BlockCompletionFunc *cb, void *opaque, Error **errp)
  333. {
  334. BlockBackend *blk;
  335. BlockJob *job;
  336. int ret;
  337. if (job_id == NULL && !(flags & JOB_INTERNAL)) {
  338. job_id = bdrv_get_device_name(bs);
  339. }
  340. blk = blk_new(bdrv_get_aio_context(bs), perm, shared_perm);
  341. ret = blk_insert_bs(blk, bs, errp);
  342. if (ret < 0) {
  343. blk_unref(blk);
  344. return NULL;
  345. }
  346. job = job_create(job_id, &driver->job_driver, txn, blk_get_aio_context(blk),
  347. flags, cb, opaque, errp);
  348. if (job == NULL) {
  349. blk_unref(blk);
  350. return NULL;
  351. }
  352. assert(is_block_job(&job->job));
  353. assert(job->job.driver->free == &block_job_free);
  354. assert(job->job.driver->user_resume == &block_job_user_resume);
  355. job->blk = blk;
  356. job->finalize_cancelled_notifier.notify = block_job_event_cancelled;
  357. job->finalize_completed_notifier.notify = block_job_event_completed;
  358. job->pending_notifier.notify = block_job_event_pending;
  359. job->ready_notifier.notify = block_job_event_ready;
  360. job->idle_notifier.notify = block_job_on_idle;
  361. notifier_list_add(&job->job.on_finalize_cancelled,
  362. &job->finalize_cancelled_notifier);
  363. notifier_list_add(&job->job.on_finalize_completed,
  364. &job->finalize_completed_notifier);
  365. notifier_list_add(&job->job.on_pending, &job->pending_notifier);
  366. notifier_list_add(&job->job.on_ready, &job->ready_notifier);
  367. notifier_list_add(&job->job.on_idle, &job->idle_notifier);
  368. error_setg(&job->blocker, "block device is in use by block job: %s",
  369. job_type_str(&job->job));
  370. block_job_add_bdrv(job, "main node", bs, 0, BLK_PERM_ALL, &error_abort);
  371. bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
  372. /* Disable request queuing in the BlockBackend to avoid deadlocks on drain:
  373. * The job reports that it's busy until it reaches a pause point. */
  374. blk_set_disable_request_queuing(blk, true);
  375. blk_set_allow_aio_context_change(blk, true);
  376. /* Only set speed when necessary to avoid NotSupported error */
  377. if (speed != 0) {
  378. Error *local_err = NULL;
  379. block_job_set_speed(job, speed, &local_err);
  380. if (local_err) {
  381. job_early_fail(&job->job);
  382. error_propagate(errp, local_err);
  383. return NULL;
  384. }
  385. }
  386. return job;
  387. }
  388. void block_job_iostatus_reset(BlockJob *job)
  389. {
  390. if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
  391. return;
  392. }
  393. assert(job->job.user_paused && job->job.pause_count > 0);
  394. job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
  395. }
  396. void block_job_user_resume(Job *job)
  397. {
  398. BlockJob *bjob = container_of(job, BlockJob, job);
  399. block_job_iostatus_reset(bjob);
  400. }
  401. BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
  402. int is_read, int error)
  403. {
  404. BlockErrorAction action;
  405. switch (on_err) {
  406. case BLOCKDEV_ON_ERROR_ENOSPC:
  407. case BLOCKDEV_ON_ERROR_AUTO:
  408. action = (error == ENOSPC) ?
  409. BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
  410. break;
  411. case BLOCKDEV_ON_ERROR_STOP:
  412. action = BLOCK_ERROR_ACTION_STOP;
  413. break;
  414. case BLOCKDEV_ON_ERROR_REPORT:
  415. action = BLOCK_ERROR_ACTION_REPORT;
  416. break;
  417. case BLOCKDEV_ON_ERROR_IGNORE:
  418. action = BLOCK_ERROR_ACTION_IGNORE;
  419. break;
  420. default:
  421. abort();
  422. }
  423. if (!block_job_is_internal(job)) {
  424. qapi_event_send_block_job_error(job->job.id,
  425. is_read ? IO_OPERATION_TYPE_READ :
  426. IO_OPERATION_TYPE_WRITE,
  427. action);
  428. }
  429. if (action == BLOCK_ERROR_ACTION_STOP) {
  430. if (!job->job.user_paused) {
  431. job_pause(&job->job);
  432. /* make the pause user visible, which will be resumed from QMP. */
  433. job->job.user_paused = true;
  434. }
  435. block_job_iostatus_set_err(job, error);
  436. }
  437. return action;
  438. }