server.c (09704e6ded83fa0bec14baf32f800f6512156ca0) server.c (cd7fca952ce8456955f7f4e11df9ced14204c2f1)
1/*
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device Server Side
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.

--- 55 unchanged lines hidden (view full) ---

64 off_t dev_offset;
65 off_t size;
66 uint16_t nbdflags;
67 QTAILQ_HEAD(, NBDClient) clients;
68 QTAILQ_ENTRY(NBDExport) next;
69
70 AioContext *ctx;
71
1/*
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device Server Side
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.

--- 55 unchanged lines hidden (view full) ---

64 off_t dev_offset;
65 off_t size;
66 uint16_t nbdflags;
67 QTAILQ_HEAD(, NBDClient) clients;
68 QTAILQ_ENTRY(NBDExport) next;
69
70 AioContext *ctx;
71
72 BlockBackend *eject_notifier_blk;
72 Notifier eject_notifier;
73};
74
75static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
76
77struct NBDClient {
78 int refcount;
79 void (*close)(NBDClient *client);

--- 722 unchanged lines hidden (view full) ---

802}
803
804static void nbd_eject_notifier(Notifier *n, void *data)
805{
806 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
807 nbd_export_close(exp);
808}
809
73 Notifier eject_notifier;
74};
75
76static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
77
78struct NBDClient {
79 int refcount;
80 void (*close)(NBDClient *client);

--- 722 unchanged lines hidden (view full) ---

803}
804
805static void nbd_eject_notifier(Notifier *n, void *data)
806{
807 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
808 nbd_export_close(exp);
809}
810
810NBDExport *nbd_export_new(BlockBackend *blk, off_t dev_offset, off_t size,
811NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
811 uint16_t nbdflags, void (*close)(NBDExport *),
812 uint16_t nbdflags, void (*close)(NBDExport *),
813 bool writethrough, BlockBackend *on_eject_blk,
812 Error **errp)
813{
814 Error **errp)
815{
816 BlockBackend *blk;
814 NBDExport *exp = g_malloc0(sizeof(NBDExport));
817 NBDExport *exp = g_malloc0(sizeof(NBDExport));
818
819 blk = blk_new();
820 blk_insert_bs(blk, bs);
821 blk_set_enable_write_cache(blk, !writethrough);
822
815 exp->refcount = 1;
816 QTAILQ_INIT(&exp->clients);
817 exp->blk = blk;
818 exp->dev_offset = dev_offset;
819 exp->nbdflags = nbdflags;
820 exp->size = size < 0 ? blk_getlength(blk) : size;
821 if (exp->size < 0) {
822 error_setg_errno(errp, -exp->size,
823 "Failed to determine the NBD export's length");
824 goto fail;
825 }
826 exp->size -= exp->size % BDRV_SECTOR_SIZE;
827
828 exp->close = close;
829 exp->ctx = blk_get_aio_context(blk);
823 exp->refcount = 1;
824 QTAILQ_INIT(&exp->clients);
825 exp->blk = blk;
826 exp->dev_offset = dev_offset;
827 exp->nbdflags = nbdflags;
828 exp->size = size < 0 ? blk_getlength(blk) : size;
829 if (exp->size < 0) {
830 error_setg_errno(errp, -exp->size,
831 "Failed to determine the NBD export's length");
832 goto fail;
833 }
834 exp->size -= exp->size % BDRV_SECTOR_SIZE;
835
836 exp->close = close;
837 exp->ctx = blk_get_aio_context(blk);
830 blk_ref(blk);
831 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
832
838 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
839
833 exp->eject_notifier.notify = nbd_eject_notifier;
834 blk_add_remove_bs_notifier(blk, &exp->eject_notifier);
840 if (on_eject_blk) {
841 blk_ref(on_eject_blk);
842 exp->eject_notifier_blk = on_eject_blk;
843 exp->eject_notifier.notify = nbd_eject_notifier;
844 blk_add_remove_bs_notifier(on_eject_blk, &exp->eject_notifier);
845 }
835
836 /*
837 * NBD exports are used for non-shared storage migration. Make sure
838 * that BDRV_O_INACTIVE is cleared and the image is ready for write
839 * access since the export could be available before migration handover.
840 */
841 aio_context_acquire(exp->ctx);
842 blk_invalidate_cache(blk, NULL);
843 aio_context_release(exp->ctx);
844 return exp;
845
846fail:
846
847 /*
848 * NBD exports are used for non-shared storage migration. Make sure
849 * that BDRV_O_INACTIVE is cleared and the image is ready for write
850 * access since the export could be available before migration handover.
851 */
852 aio_context_acquire(exp->ctx);
853 blk_invalidate_cache(blk, NULL);
854 aio_context_release(exp->ctx);
855 return exp;
856
857fail:
858 blk_unref(blk);
847 g_free(exp);
848 return NULL;
849}
850
851NBDExport *nbd_export_find(const char *name)
852{
853 NBDExport *exp;
854 QTAILQ_FOREACH(exp, &exports, next) {

--- 54 unchanged lines hidden (view full) ---

909 if (--exp->refcount == 0) {
910 assert(exp->name == NULL);
911
912 if (exp->close) {
913 exp->close(exp);
914 }
915
916 if (exp->blk) {
859 g_free(exp);
860 return NULL;
861}
862
863NBDExport *nbd_export_find(const char *name)
864{
865 NBDExport *exp;
866 QTAILQ_FOREACH(exp, &exports, next) {

--- 54 unchanged lines hidden (view full) ---

921 if (--exp->refcount == 0) {
922 assert(exp->name == NULL);
923
924 if (exp->close) {
925 exp->close(exp);
926 }
927
928 if (exp->blk) {
917 notifier_remove(&exp->eject_notifier);
929 if (exp->eject_notifier_blk) {
930 notifier_remove(&exp->eject_notifier);
931 blk_unref(exp->eject_notifier_blk);
932 }
918 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
919 blk_aio_detach, exp);
920 blk_unref(exp->blk);
921 exp->blk = NULL;
922 }
923
924 g_free(exp);
925 }

--- 394 unchanged lines hidden ---
933 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
934 blk_aio_detach, exp);
935 blk_unref(exp->blk);
936 exp->blk = NULL;
937 }
938
939 g_free(exp);
940 }

--- 394 unchanged lines hidden ---