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