Deleted Added
full compact
blkback.c (230587) blkback.c (231743)
1/*-
2 * Copyright (c) 2009-2011 Spectra Logic Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

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

26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
29 *
30 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
31 * Ken Merry (Spectra Logic Corporation)
32 */
33#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2009-2011 Spectra Logic Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

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

26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
29 *
30 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
31 * Ken Merry (Spectra Logic Corporation)
32 */
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/xen/blkback/blkback.c 230587 2012-01-26 16:35:09Z ken $");
34__FBSDID("$FreeBSD: head/sys/dev/xen/blkback/blkback.c 231743 2012-02-15 06:45:49Z gibbs $");
35
36/**
37 * \file blkback.c
38 *
39 * \brief Device driver supporting the vending of block storage from
40 * a FreeBSD domain to other domains.
41 */
42
35
36/**
37 * \file blkback.c
38 *
39 * \brief Device driver supporting the vending of block storage from
40 * a FreeBSD domain to other domains.
41 */
42
43#include "opt_kdtrace.h"
44
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/malloc.h>
47
48#include <sys/bio.h>
49#include <sys/bus.h>
50#include <sys/conf.h>

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

58#include <sys/proc.h>
59#include <sys/rman.h>
60#include <sys/taskqueue.h>
61#include <sys/types.h>
62#include <sys/vnode.h>
63#include <sys/mount.h>
64#include <sys/sysctl.h>
65#include <sys/bitstring.h>
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/malloc.h>
49
50#include <sys/bio.h>
51#include <sys/bus.h>
52#include <sys/conf.h>

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

60#include <sys/proc.h>
61#include <sys/rman.h>
62#include <sys/taskqueue.h>
63#include <sys/types.h>
64#include <sys/vnode.h>
65#include <sys/mount.h>
66#include <sys/sysctl.h>
67#include <sys/bitstring.h>
68#include <sys/sdt.h>
66
67#include <geom/geom.h>
68
69#include <machine/_inttypes.h>
70#include <machine/xen/xen-os.h>
71
72#include <vm/vm.h>
73#include <vm/vm_extern.h>

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

119
120/*---------------------------------- Macros ----------------------------------*/
121/**
122 * Custom malloc type for all driver allocations.
123 */
124static MALLOC_DEFINE(M_XENBLOCKBACK, "xbbd", "Xen Block Back Driver Data");
125
126#ifdef XBB_DEBUG
69
70#include <geom/geom.h>
71
72#include <machine/_inttypes.h>
73#include <machine/xen/xen-os.h>
74
75#include <vm/vm.h>
76#include <vm/vm_extern.h>

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

122
123/*---------------------------------- Macros ----------------------------------*/
124/**
125 * Custom malloc type for all driver allocations.
126 */
127static MALLOC_DEFINE(M_XENBLOCKBACK, "xbbd", "Xen Block Back Driver Data");
128
129#ifdef XBB_DEBUG
127#define DPRINTF(fmt, args...) \
130#define DPRINTF(fmt, args...) \
128 printf("xbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
129#else
130#define DPRINTF(fmt, args...) do {} while(0)
131#endif
132
133/**
134 * The maximum mapped region size per request we will allow in a negotiated
135 * block-front/back communication channel.
136 */
131 printf("xbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
132#else
133#define DPRINTF(fmt, args...) do {} while(0)
134#endif
135
136/**
137 * The maximum mapped region size per request we will allow in a negotiated
138 * block-front/back communication channel.
139 */
137#define XBB_MAX_REQUEST_SIZE \
140#define XBB_MAX_REQUEST_SIZE \
138 MIN(MAXPHYS, BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE)
139
140/**
141 * The maximum number of segments (within a request header and accompanying
142 * segment blocks) per request we will allow in a negotiated block-front/back
143 * communication channel.
144 */
141 MIN(MAXPHYS, BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE)
142
143/**
144 * The maximum number of segments (within a request header and accompanying
145 * segment blocks) per request we will allow in a negotiated block-front/back
146 * communication channel.
147 */
145#define XBB_MAX_SEGMENTS_PER_REQUEST \
146 (MIN(UIO_MAXIOV, \
147 MIN(BLKIF_MAX_SEGMENTS_PER_REQUEST, \
148#define XBB_MAX_SEGMENTS_PER_REQUEST \
149 (MIN(UIO_MAXIOV, \
150 MIN(BLKIF_MAX_SEGMENTS_PER_REQUEST, \
148 (XBB_MAX_REQUEST_SIZE / PAGE_SIZE) + 1)))
149
150/**
151 * The maximum number of shared memory ring pages we will allow in a
152 * negotiated block-front/back communication channel. Allow enough
153 * ring space for all requests to be XBB_MAX_REQUEST_SIZE'd.
154 */
155#define XBB_MAX_RING_PAGES \

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

975 * Note: This should be unnecessary once we have either chaining or
976 * scatter/gather support for struct bio. At that point we'll be able to
977 * put multiple addresses and lengths in one bio/bio chain and won't need
978 * to map everything into one virtual segment.
979 */
980static uint8_t *
981xbb_get_kva(struct xbb_softc *xbb, int nr_pages)
982{
151 (XBB_MAX_REQUEST_SIZE / PAGE_SIZE) + 1)))
152
153/**
154 * The maximum number of shared memory ring pages we will allow in a
155 * negotiated block-front/back communication channel. Allow enough
156 * ring space for all requests to be XBB_MAX_REQUEST_SIZE'd.
157 */
158#define XBB_MAX_RING_PAGES \

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

978 * Note: This should be unnecessary once we have either chaining or
979 * scatter/gather support for struct bio. At that point we'll be able to
980 * put multiple addresses and lengths in one bio/bio chain and won't need
981 * to map everything into one virtual segment.
982 */
983static uint8_t *
984xbb_get_kva(struct xbb_softc *xbb, int nr_pages)
985{
983 intptr_t first_clear, num_clear;
986 intptr_t first_clear;
987 intptr_t num_clear;
984 uint8_t *free_kva;
988 uint8_t *free_kva;
985 int i;
989 int i;
986
987 KASSERT(nr_pages != 0, ("xbb_get_kva of zero length"));
988
989 first_clear = 0;
990 free_kva = NULL;
991
992 mtx_lock(&xbb->lock);
993

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

1676 * Fetch the next request block full of SG elements.
1677 * For now, only the spacing between entries is
1678 * different in the different ABIs, not the sg entry
1679 * layout.
1680 */
1681 req_ring_idx++;
1682 switch (xbb->abi) {
1683 case BLKIF_PROTOCOL_NATIVE:
990
991 KASSERT(nr_pages != 0, ("xbb_get_kva of zero length"));
992
993 first_clear = 0;
994 free_kva = NULL;
995
996 mtx_lock(&xbb->lock);
997

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

1680 * Fetch the next request block full of SG elements.
1681 * For now, only the spacing between entries is
1682 * different in the different ABIs, not the sg entry
1683 * layout.
1684 */
1685 req_ring_idx++;
1686 switch (xbb->abi) {
1687 case BLKIF_PROTOCOL_NATIVE:
1684 sg = BLKRING_GET_SG_REQUEST(&xbb->rings.native,
1685 req_ring_idx);
1688 sg = BLKRING_GET_SEG_BLOCK(&xbb->rings.native,
1689 req_ring_idx);
1686 break;
1687 case BLKIF_PROTOCOL_X86_32:
1688 {
1690 break;
1691 case BLKIF_PROTOCOL_X86_32:
1692 {
1689 sg = BLKRING_GET_SG_REQUEST(&xbb->rings.x86_32,
1690 req_ring_idx);
1693 sg = BLKRING_GET_SEG_BLOCK(&xbb->rings.x86_32,
1694 req_ring_idx);
1691 break;
1692 }
1693 case BLKIF_PROTOCOL_X86_64:
1694 {
1695 break;
1696 }
1697 case BLKIF_PROTOCOL_X86_64:
1698 {
1695 sg = BLKRING_GET_SG_REQUEST(&xbb->rings.x86_64,
1696 req_ring_idx);
1699 sg = BLKRING_GET_SEG_BLOCK(&xbb->rings.x86_64,
1700 req_ring_idx);
1697 break;
1698 }
1699 default:
1700 panic("Unexpected blkif protocol ABI.");
1701 /* NOTREACHED */
1702 }
1703 last_block_sg = sg + block_segs;
1704 }

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

1812 struct xbb_softc *xbb;
1813 blkif_back_rings_t *rings;
1814 RING_IDX rp;
1815 uint64_t cur_sector;
1816 int cur_operation;
1817 struct xbb_xen_reqlist *reqlist;
1818
1819
1701 break;
1702 }
1703 default:
1704 panic("Unexpected blkif protocol ABI.");
1705 /* NOTREACHED */
1706 }
1707 last_block_sg = sg + block_segs;
1708 }

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

1816 struct xbb_softc *xbb;
1817 blkif_back_rings_t *rings;
1818 RING_IDX rp;
1819 uint64_t cur_sector;
1820 int cur_operation;
1821 struct xbb_xen_reqlist *reqlist;
1822
1823
1820 xbb = (struct xbb_softc *)context;
1821 rings = &xbb->rings;
1824 xbb = (struct xbb_softc *)context;
1825 rings = &xbb->rings;
1822
1823 /*
1824 * Work gather and dispatch loop. Note that we have a bias here
1825 * towards gathering I/O sent by blockfront. We first gather up
1826 * everything in the ring, as long as we have resources. Then we
1827 * dispatch one request, and then attempt to gather up any
1828 * additional requests that have come in while we were dispatching
1829 * the request.

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

2027{
2028 struct xbb_softc *xbb;
2029
2030 /* Defer to kernel thread. */
2031 xbb = (struct xbb_softc *)arg;
2032 taskqueue_enqueue(xbb->io_taskqueue, &xbb->io_task);
2033}
2034
1826
1827 /*
1828 * Work gather and dispatch loop. Note that we have a bias here
1829 * towards gathering I/O sent by blockfront. We first gather up
1830 * everything in the ring, as long as we have resources. Then we
1831 * dispatch one request, and then attempt to gather up any
1832 * additional requests that have come in while we were dispatching
1833 * the request.

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

2031{
2032 struct xbb_softc *xbb;
2033
2034 /* Defer to kernel thread. */
2035 xbb = (struct xbb_softc *)arg;
2036 taskqueue_enqueue(xbb->io_taskqueue, &xbb->io_task);
2037}
2038
2039SDT_PROVIDER_DEFINE(xbb);
2040SDT_PROBE_DEFINE1(xbb, kernel, xbb_dispatch_dev, flush, flush, "int");
2041SDT_PROBE_DEFINE3(xbb, kernel, xbb_dispatch_dev, read, read, "int", "uint64_t",
2042 "uint64_t");
2043SDT_PROBE_DEFINE3(xbb, kernel, xbb_dispatch_dev, write, write, "int",
2044 "uint64_t", "uint64_t");
2045
2035/*----------------------------- Backend Handlers -----------------------------*/
2036/**
2037 * Backend handler for character device access.
2038 *
2039 * \param xbb Per-instance xbb configuration structure.
2040 * \param reqlist Allocated internal request list structure.
2041 * \param operation BIO_* I/O operation code.
2042 * \param bio_flags Additional bio_flag data to pass to any generated

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

2082 bio->bio_offset = 0;
2083 bio->bio_data = 0;
2084 bio->bio_done = xbb_bio_done;
2085 bio->bio_caller1 = nreq;
2086 bio->bio_pblkno = 0;
2087
2088 nreq->pendcnt = 1;
2089
2046/*----------------------------- Backend Handlers -----------------------------*/
2047/**
2048 * Backend handler for character device access.
2049 *
2050 * \param xbb Per-instance xbb configuration structure.
2051 * \param reqlist Allocated internal request list structure.
2052 * \param operation BIO_* I/O operation code.
2053 * \param bio_flags Additional bio_flag data to pass to any generated

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

2093 bio->bio_offset = 0;
2094 bio->bio_data = 0;
2095 bio->bio_done = xbb_bio_done;
2096 bio->bio_caller1 = nreq;
2097 bio->bio_pblkno = 0;
2098
2099 nreq->pendcnt = 1;
2100
2101 SDT_PROBE1(xbb, kernel, xbb_dispatch_dev, flush,
2102 device_get_unit(xbb->dev));
2103
2090 (*dev_data->csw->d_strategy)(bio);
2091
2092 return (0);
2093 }
2094
2095 xbb_sg = xbb->xbb_sgs;
2096 bio = NULL;
2097 nseg = reqlist->nr_segments;

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

2176 kva_offset = (vm_offset_t)bios[bio_idx]->bio_data
2177 - (vm_offset_t)reqlist->bounce;
2178 if (operation == BIO_WRITE) {
2179 memcpy(bios[bio_idx]->bio_data,
2180 (uint8_t *)reqlist->kva + kva_offset,
2181 bios[bio_idx]->bio_bcount);
2182 }
2183#endif
2104 (*dev_data->csw->d_strategy)(bio);
2105
2106 return (0);
2107 }
2108
2109 xbb_sg = xbb->xbb_sgs;
2110 bio = NULL;
2111 nseg = reqlist->nr_segments;

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

2190 kva_offset = (vm_offset_t)bios[bio_idx]->bio_data
2191 - (vm_offset_t)reqlist->bounce;
2192 if (operation == BIO_WRITE) {
2193 memcpy(bios[bio_idx]->bio_data,
2194 (uint8_t *)reqlist->kva + kva_offset,
2195 bios[bio_idx]->bio_bcount);
2196 }
2197#endif
2198 if (operation == BIO_READ) {
2199 SDT_PROBE3(xbb, kernel, xbb_dispatch_dev, read,
2200 device_get_unit(xbb->dev),
2201 bios[bio_idx]->bio_offset,
2202 bios[bio_idx]->bio_length);
2203 } else if (operation == BIO_WRITE) {
2204 SDT_PROBE3(xbb, kernel, xbb_dispatch_dev, write,
2205 device_get_unit(xbb->dev),
2206 bios[bio_idx]->bio_offset,
2207 bios[bio_idx]->bio_length);
2208 }
2184 (*dev_data->csw->d_strategy)(bios[bio_idx]);
2185 }
2186
2187 return (error);
2188
2189fail_free_bios:
2190 for (bio_idx = 0; bio_idx < (nbio-1); bio_idx++)
2191 g_destroy_bio(bios[bio_idx]);
2192
2193 return (error);
2194}
2195
2209 (*dev_data->csw->d_strategy)(bios[bio_idx]);
2210 }
2211
2212 return (error);
2213
2214fail_free_bios:
2215 for (bio_idx = 0; bio_idx < (nbio-1); bio_idx++)
2216 g_destroy_bio(bios[bio_idx]);
2217
2218 return (error);
2219}
2220
2221SDT_PROBE_DEFINE1(xbb, kernel, xbb_dispatch_file, flush, flush, "int");
2222SDT_PROBE_DEFINE3(xbb, kernel, xbb_dispatch_file, read, read, "int", "uint64_t",
2223 "uint64_t");
2224SDT_PROBE_DEFINE3(xbb, kernel, xbb_dispatch_file, write, write, "int",
2225 "uint64_t", "uint64_t");
2226
2196/**
2197 * Backend handler for file access.
2198 *
2199 * \param xbb Per-instance xbb configuration structure.
2200 * \param reqlist Allocated internal request list.
2201 * \param operation BIO_* I/O operation code.
2202 * \param flags Additional bio_flag data to pass to any generated bios
2203 * (e.g. BIO_ORDERED)..

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

2232 xuio.uio_rw = UIO_READ;
2233 break;
2234 case BIO_WRITE:
2235 xuio.uio_rw = UIO_WRITE;
2236 break;
2237 case BIO_FLUSH: {
2238 struct mount *mountpoint;
2239
2227/**
2228 * Backend handler for file access.
2229 *
2230 * \param xbb Per-instance xbb configuration structure.
2231 * \param reqlist Allocated internal request list.
2232 * \param operation BIO_* I/O operation code.
2233 * \param flags Additional bio_flag data to pass to any generated bios
2234 * (e.g. BIO_ORDERED)..

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

2263 xuio.uio_rw = UIO_READ;
2264 break;
2265 case BIO_WRITE:
2266 xuio.uio_rw = UIO_WRITE;
2267 break;
2268 case BIO_FLUSH: {
2269 struct mount *mountpoint;
2270
2271 SDT_PROBE1(xbb, kernel, xbb_dispatch_file, flush,
2272 device_get_unit(xbb->dev));
2273
2240 vfs_is_locked = VFS_LOCK_GIANT(xbb->vn->v_mount);
2241
2242 (void) vn_start_write(xbb->vn, &mountpoint, V_WAIT);
2243
2244 vn_lock(xbb->vn, LK_EXCLUSIVE | LK_RETRY);
2245 error = VOP_FSYNC(xbb->vn, MNT_WAIT, curthread);
2246 VOP_UNLOCK(xbb->vn, 0);
2247

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

2331 xuio.uio_iovcnt * sizeof(xuio.uio_iov[0]));
2332 }
2333#endif /* XBB_USE_BOUNCE_BUFFERS */
2334
2335 vfs_is_locked = VFS_LOCK_GIANT(xbb->vn->v_mount);
2336 switch (operation) {
2337 case BIO_READ:
2338
2274 vfs_is_locked = VFS_LOCK_GIANT(xbb->vn->v_mount);
2275
2276 (void) vn_start_write(xbb->vn, &mountpoint, V_WAIT);
2277
2278 vn_lock(xbb->vn, LK_EXCLUSIVE | LK_RETRY);
2279 error = VOP_FSYNC(xbb->vn, MNT_WAIT, curthread);
2280 VOP_UNLOCK(xbb->vn, 0);
2281

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

2365 xuio.uio_iovcnt * sizeof(xuio.uio_iov[0]));
2366 }
2367#endif /* XBB_USE_BOUNCE_BUFFERS */
2368
2369 vfs_is_locked = VFS_LOCK_GIANT(xbb->vn->v_mount);
2370 switch (operation) {
2371 case BIO_READ:
2372
2373 SDT_PROBE3(xbb, kernel, xbb_dispatch_file, read,
2374 device_get_unit(xbb->dev), xuio.uio_offset,
2375 xuio.uio_resid);
2376
2339 vn_lock(xbb->vn, LK_EXCLUSIVE | LK_RETRY);
2340
2341 /*
2342 * UFS pays attention to IO_DIRECT for reads. If the
2343 * DIRECTIO option is configured into the kernel, it calls
2344 * ffs_rawread(). But that only works for single-segment
2345 * uios with user space addresses. In our case, with a
2346 * kernel uio, it still reads into the buffer cache, but it

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

2361 error = VOP_READ(xbb->vn, &xuio, (flags & BIO_ORDERED) ?
2362 (IO_DIRECT|IO_SYNC) : 0, file_data->cred);
2363
2364 VOP_UNLOCK(xbb->vn, 0);
2365 break;
2366 case BIO_WRITE: {
2367 struct mount *mountpoint;
2368
2377 vn_lock(xbb->vn, LK_EXCLUSIVE | LK_RETRY);
2378
2379 /*
2380 * UFS pays attention to IO_DIRECT for reads. If the
2381 * DIRECTIO option is configured into the kernel, it calls
2382 * ffs_rawread(). But that only works for single-segment
2383 * uios with user space addresses. In our case, with a
2384 * kernel uio, it still reads into the buffer cache, but it

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

2399 error = VOP_READ(xbb->vn, &xuio, (flags & BIO_ORDERED) ?
2400 (IO_DIRECT|IO_SYNC) : 0, file_data->cred);
2401
2402 VOP_UNLOCK(xbb->vn, 0);
2403 break;
2404 case BIO_WRITE: {
2405 struct mount *mountpoint;
2406
2407 SDT_PROBE3(xbb, kernel, xbb_dispatch_file, write,
2408 device_get_unit(xbb->dev), xuio.uio_offset,
2409 xuio.uio_resid);
2410
2369 (void)vn_start_write(xbb->vn, &mountpoint, V_WAIT);
2370
2371 vn_lock(xbb->vn, LK_EXCLUSIVE | LK_RETRY);
2372
2373 /*
2374 * UFS pays attention to IO_DIRECT for writes. The write
2375 * is done asynchronously. (Normally the write would just
2376 * get put into cache.

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

3023 */
3024static int
3025xbb_collect_frontend_info(struct xbb_softc *xbb)
3026{
3027 char protocol_abi[64];
3028 const char *otherend_path;
3029 int error;
3030 u_int ring_idx;
2411 (void)vn_start_write(xbb->vn, &mountpoint, V_WAIT);
2412
2413 vn_lock(xbb->vn, LK_EXCLUSIVE | LK_RETRY);
2414
2415 /*
2416 * UFS pays attention to IO_DIRECT for writes. The write
2417 * is done asynchronously. (Normally the write would just
2418 * get put into cache.

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

3065 */
3066static int
3067xbb_collect_frontend_info(struct xbb_softc *xbb)
3068{
3069 char protocol_abi[64];
3070 const char *otherend_path;
3071 int error;
3072 u_int ring_idx;
3073 u_int ring_page_order;
3074 size_t ring_size;
3031
3032 otherend_path = xenbus_get_otherend_path(xbb->dev);
3033
3034 /*
3035 * Protocol defaults valid even if all negotiation fails.
3036 */
3037 xbb->ring_config.ring_pages = 1;
3075
3076 otherend_path = xenbus_get_otherend_path(xbb->dev);
3077
3078 /*
3079 * Protocol defaults valid even if all negotiation fails.
3080 */
3081 xbb->ring_config.ring_pages = 1;
3038 xbb->max_requests = BLKIF_MAX_RING_REQUESTS(PAGE_SIZE);
3039 xbb->max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK;
3040 xbb->max_request_size = xbb->max_request_segments * PAGE_SIZE;
3041
3042 /*
3043 * Mandatory data (used in all versions of the protocol) first.
3044 */
3082 xbb->max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK;
3083 xbb->max_request_size = xbb->max_request_segments * PAGE_SIZE;
3084
3085 /*
3086 * Mandatory data (used in all versions of the protocol) first.
3087 */
3045 error = xs_gather(XST_NIL, otherend_path,
3046 "ring-ref", "%" PRIu32,
3047 &xbb->ring_config.ring_ref[0],
3048 "event-channel", "%" PRIu32,
3049 &xbb->ring_config.evtchn,
3050 NULL);
3088 error = xs_scanf(XST_NIL, otherend_path,
3089 "event-channel", NULL, "%" PRIu32,
3090 &xbb->ring_config.evtchn);
3051 if (error != 0) {
3052 xenbus_dev_fatal(xbb->dev, error,
3091 if (error != 0) {
3092 xenbus_dev_fatal(xbb->dev, error,
3053 "Unable to retrieve ring information from "
3054 "frontend %s. Unable to connect.",
3093 "Unable to retrieve event-channel information "
3094 "from frontend %s. Unable to connect.",
3055 xenbus_get_otherend_path(xbb->dev));
3056 return (error);
3057 }
3058
3059 /*
3060 * These fields are initialized to legacy protocol defaults
3061 * so we only need to fail if reading the updated value succeeds
3062 * and the new value is outside of its allowed range.
3063 *
3064 * \note xs_gather() returns on the first encountered error, so
3065 * we must use independant calls in order to guarantee
3066 * we don't miss information in a sparsly populated front-end
3067 * tree.
3095 xenbus_get_otherend_path(xbb->dev));
3096 return (error);
3097 }
3098
3099 /*
3100 * These fields are initialized to legacy protocol defaults
3101 * so we only need to fail if reading the updated value succeeds
3102 * and the new value is outside of its allowed range.
3103 *
3104 * \note xs_gather() returns on the first encountered error, so
3105 * we must use independant calls in order to guarantee
3106 * we don't miss information in a sparsly populated front-end
3107 * tree.
3108 *
3109 * \note xs_scanf() does not update variables for unmatched
3110 * fields.
3068 */
3111 */
3112 ring_page_order = 0;
3069 (void)xs_scanf(XST_NIL, otherend_path,
3113 (void)xs_scanf(XST_NIL, otherend_path,
3070 "ring-pages", NULL, "%u",
3114 "ring-page-order", NULL, "%u",
3115 &ring_page_order);
3116 xbb->ring_config.ring_pages = 1 << ring_page_order;
3117 (void)xs_scanf(XST_NIL, otherend_path,
3118 "num-ring-pages", NULL, "%u",
3071 &xbb->ring_config.ring_pages);
3119 &xbb->ring_config.ring_pages);
3120 ring_size = PAGE_SIZE * xbb->ring_config.ring_pages;
3121 xbb->max_requests = BLKIF_MAX_RING_REQUESTS(ring_size);
3072
3073 (void)xs_scanf(XST_NIL, otherend_path,
3074 "max-requests", NULL, "%u",
3075 &xbb->max_requests);
3076
3077 (void)xs_scanf(XST_NIL, otherend_path,
3078 "max-request-segments", NULL, "%u",
3079 &xbb->max_request_segments);

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

3111 "Front-end specificed max_request_size "
3112 "of %u exceeds backend limit of %u. "
3113 "Unable to connect.",
3114 xbb->max_request_size,
3115 XBB_MAX_REQUEST_SIZE);
3116 return (EINVAL);
3117 }
3118
3122
3123 (void)xs_scanf(XST_NIL, otherend_path,
3124 "max-requests", NULL, "%u",
3125 &xbb->max_requests);
3126
3127 (void)xs_scanf(XST_NIL, otherend_path,
3128 "max-request-segments", NULL, "%u",
3129 &xbb->max_request_segments);

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

3161 "Front-end specificed max_request_size "
3162 "of %u exceeds backend limit of %u. "
3163 "Unable to connect.",
3164 xbb->max_request_size,
3165 XBB_MAX_REQUEST_SIZE);
3166 return (EINVAL);
3167 }
3168
3119 /* If using a multi-page ring, pull in the remaining references. */
3120 for (ring_idx = 1; ring_idx < xbb->ring_config.ring_pages; ring_idx++) {
3121 char ring_ref_name[]= "ring_refXX";
3122
3123 snprintf(ring_ref_name, sizeof(ring_ref_name),
3124 "ring-ref%u", ring_idx);
3125 error = xs_scanf(XST_NIL, otherend_path,
3126 ring_ref_name, NULL, "%" PRIu32,
3127 &xbb->ring_config.ring_ref[ring_idx]);
3169 if (xbb->ring_config.ring_pages == 1) {
3170 error = xs_gather(XST_NIL, otherend_path,
3171 "ring-ref", "%" PRIu32,
3172 &xbb->ring_config.ring_ref[0],
3173 NULL);
3128 if (error != 0) {
3129 xenbus_dev_fatal(xbb->dev, error,
3174 if (error != 0) {
3175 xenbus_dev_fatal(xbb->dev, error,
3130 "Failed to retriev grant reference "
3131 "for page %u of shared ring. Unable "
3132 "to connect.", ring_idx);
3176 "Unable to retrieve ring information "
3177 "from frontend %s. Unable to "
3178 "connect.",
3179 xenbus_get_otherend_path(xbb->dev));
3133 return (error);
3134 }
3180 return (error);
3181 }
3182 } else {
3183 /* Multi-page ring format. */
3184 for (ring_idx = 0; ring_idx < xbb->ring_config.ring_pages;
3185 ring_idx++) {
3186 char ring_ref_name[]= "ring_refXX";
3187
3188 snprintf(ring_ref_name, sizeof(ring_ref_name),
3189 "ring-ref%u", ring_idx);
3190 error = xs_scanf(XST_NIL, otherend_path,
3191 ring_ref_name, NULL, "%" PRIu32,
3192 &xbb->ring_config.ring_ref[ring_idx]);
3193 if (error != 0) {
3194 xenbus_dev_fatal(xbb->dev, error,
3195 "Failed to retriev grant "
3196 "reference for page %u of "
3197 "shared ring. Unable "
3198 "to connect.", ring_idx);
3199 return (error);
3200 }
3201 }
3135 }
3136
3137 error = xs_gather(XST_NIL, otherend_path,
3138 "protocol", "%63s", protocol_abi,
3139 NULL);
3140 if (error != 0
3141 || !strcmp(protocol_abi, XEN_IO_PROTO_ABI_NATIVE)) {
3142 /*

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

3192 req++;
3193 }
3194 return (0);
3195}
3196
3197static int
3198xbb_alloc_request_lists(struct xbb_softc *xbb)
3199{
3202 }
3203
3204 error = xs_gather(XST_NIL, otherend_path,
3205 "protocol", "%63s", protocol_abi,
3206 NULL);
3207 if (error != 0
3208 || !strcmp(protocol_abi, XEN_IO_PROTO_ABI_NATIVE)) {
3209 /*

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

3259 req++;
3260 }
3261 return (0);
3262}
3263
3264static int
3265xbb_alloc_request_lists(struct xbb_softc *xbb)
3266{
3200 int i;
3201 struct xbb_xen_reqlist *reqlist;
3267 struct xbb_xen_reqlist *reqlist;
3268 int i;
3202
3203 /*
3204 * If no requests can be merged, we need 1 request list per
3205 * in flight request.
3206 */
3207 xbb->request_lists = malloc(xbb->max_requests *
3208 sizeof(*xbb->request_lists), M_XENBLOCKBACK, M_NOWAIT|M_ZERO);
3209 if (xbb->request_lists == NULL) {

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

3313 * Connect to our blkfront peer now that it has completed publishing
3314 * its configuration into the XenStore.
3315 *
3316 * \param xbb Per-instance xbb configuration structure.
3317 */
3318static void
3319xbb_connect(struct xbb_softc *xbb)
3320{
3269
3270 /*
3271 * If no requests can be merged, we need 1 request list per
3272 * in flight request.
3273 */
3274 xbb->request_lists = malloc(xbb->max_requests *
3275 sizeof(*xbb->request_lists), M_XENBLOCKBACK, M_NOWAIT|M_ZERO);
3276 if (xbb->request_lists == NULL) {

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

3380 * Connect to our blkfront peer now that it has completed publishing
3381 * its configuration into the XenStore.
3382 *
3383 * \param xbb Per-instance xbb configuration structure.
3384 */
3385static void
3386xbb_connect(struct xbb_softc *xbb)
3387{
3321 int error;
3388 int error;
3322
3323 if (xenbus_get_state(xbb->dev) == XenbusStateConnected)
3324 return;
3325
3326 if (xbb_collect_frontend_info(xbb) != 0)
3327 return;
3328
3329 xbb->flags &= ~XBBF_SHUTDOWN;

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

3394 * Mark this instance as shutting down, wait for any active I/O on the
3395 * backend device/file to drain, disconnect from the front-end, and notify
3396 * any waiters (e.g. a thread invoking our detach method) that detach can
3397 * now proceed.
3398 */
3399static int
3400xbb_shutdown(struct xbb_softc *xbb)
3401{
3389
3390 if (xenbus_get_state(xbb->dev) == XenbusStateConnected)
3391 return;
3392
3393 if (xbb_collect_frontend_info(xbb) != 0)
3394 return;
3395
3396 xbb->flags &= ~XBBF_SHUTDOWN;

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

3461 * Mark this instance as shutting down, wait for any active I/O on the
3462 * backend device/file to drain, disconnect from the front-end, and notify
3463 * any waiters (e.g. a thread invoking our detach method) that detach can
3464 * now proceed.
3465 */
3466static int
3467xbb_shutdown(struct xbb_softc *xbb)
3468{
3402 int error;
3469 XenbusState frontState;
3470 int error;
3403
3404 DPRINTF("\n");
3405
3406 /*
3407 * Due to the need to drop our mutex during some
3408 * xenbus operations, it is possible for two threads
3409 * to attempt to close out shutdown processing at
3410 * the same time. Tell the caller that hits this
3411 * race to try back later.
3412 */
3413 if ((xbb->flags & XBBF_IN_SHUTDOWN) != 0)
3414 return (EAGAIN);
3415
3471
3472 DPRINTF("\n");
3473
3474 /*
3475 * Due to the need to drop our mutex during some
3476 * xenbus operations, it is possible for two threads
3477 * to attempt to close out shutdown processing at
3478 * the same time. Tell the caller that hits this
3479 * race to try back later.
3480 */
3481 if ((xbb->flags & XBBF_IN_SHUTDOWN) != 0)
3482 return (EAGAIN);
3483
3484 xbb->flags |= XBBF_IN_SHUTDOWN;
3485 mtx_unlock(&xbb->lock);
3486
3487 if (xenbus_get_state(xbb->dev) < XenbusStateClosing)
3488 xenbus_set_state(xbb->dev, XenbusStateClosing);
3489
3490 frontState = xenbus_get_otherend_state(xbb->dev);
3491 mtx_lock(&xbb->lock);
3492 xbb->flags &= ~XBBF_IN_SHUTDOWN;
3493
3494 /* The front can submit I/O until entering the closed state. */
3495 if (frontState < XenbusStateClosed)
3496 return (EAGAIN);
3497
3416 DPRINTF("\n");
3417
3418 /* Indicate shutdown is in progress. */
3419 xbb->flags |= XBBF_SHUTDOWN;
3420
3421 /* Disconnect from the front-end. */
3422 error = xbb_disconnect(xbb);
3423 if (error != 0) {

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

3429 ("%s: Unexpected xbb_disconnect() failure %d",
3430 __func__, error));
3431
3432 return (error);
3433 }
3434
3435 DPRINTF("\n");
3436
3498 DPRINTF("\n");
3499
3500 /* Indicate shutdown is in progress. */
3501 xbb->flags |= XBBF_SHUTDOWN;
3502
3503 /* Disconnect from the front-end. */
3504 error = xbb_disconnect(xbb);
3505 if (error != 0) {

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

3511 ("%s: Unexpected xbb_disconnect() failure %d",
3512 __func__, error));
3513
3514 return (error);
3515 }
3516
3517 DPRINTF("\n");
3518
3437 /*
3438 * Before unlocking mutex, set this flag to prevent other threads from
3439 * getting into this function
3440 */
3441 xbb->flags |= XBBF_IN_SHUTDOWN;
3442 mtx_unlock(&xbb->lock);
3443
3444 if (xenbus_get_state(xbb->dev) < XenbusStateClosing)
3445 xenbus_set_state(xbb->dev, XenbusStateClosing);
3446
3447 mtx_lock(&xbb->lock);
3448 xbb->flags &= ~XBBF_IN_SHUTDOWN;
3449
3450 /* Indicate to xbb_detach() that is it safe to proceed. */
3451 wakeup(xbb);
3452
3453 return (0);
3454}
3455
3456/**
3457 * Report an attach time error to the console and Xen, and cleanup

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

3568 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3569 "max_requests", CTLFLAG_RD, &xbb->max_requests, 0,
3570 "maximum outstanding requests (negotiated)");
3571
3572 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3573 "max_request_segments", CTLFLAG_RD,
3574 &xbb->max_request_segments, 0,
3575 "maximum number of pages per requests (negotiated)");
3519 /* Indicate to xbb_detach() that is it safe to proceed. */
3520 wakeup(xbb);
3521
3522 return (0);
3523}
3524
3525/**
3526 * Report an attach time error to the console and Xen, and cleanup

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

3637 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3638 "max_requests", CTLFLAG_RD, &xbb->max_requests, 0,
3639 "maximum outstanding requests (negotiated)");
3640
3641 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3642 "max_request_segments", CTLFLAG_RD,
3643 &xbb->max_request_segments, 0,
3644 "maximum number of pages per requests (negotiated)");
3645
3646 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3647 "max_request_size", CTLFLAG_RD,
3648 &xbb->max_request_size, 0,
3649 "maximum size in bytes of a request (negotiated)");
3650
3651 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3652 "ring_pages", CTLFLAG_RD,
3653 &xbb->ring_config.ring_pages, 0,
3654 "communication channel pages (negotiated)");
3576}
3577
3578/**
3579 * Attach to a XenBus device that has been claimed by our probe routine.
3580 *
3581 * \param dev NewBus device object representing this Xen Block Back instance.
3582 *
3583 * \return 0 for success, errno codes for failure.
3584 */
3585static int
3586xbb_attach(device_t dev)
3587{
3588 struct xbb_softc *xbb;
3589 int error;
3655}
3656
3657/**
3658 * Attach to a XenBus device that has been claimed by our probe routine.
3659 *
3660 * \param dev NewBus device object representing this Xen Block Back instance.
3661 *
3662 * \return 0 for success, errno codes for failure.
3663 */
3664static int
3665xbb_attach(device_t dev)
3666{
3667 struct xbb_softc *xbb;
3668 int error;
3669 u_int max_ring_page_order;
3590
3591 DPRINTF("Attaching to %s\n", xenbus_get_node(dev));
3592
3593 /*
3594 * Basic initialization.
3595 * After this block it is safe to call xbb_detach()
3596 * to clean up any allocated data for this instance.
3597 */

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

3616 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3617 "feature-flush-cache", "1");
3618 if (error) {
3619 xbb_attach_failed(xbb, error, "writing %s/feature-flush-cache",
3620 xenbus_get_node(xbb->dev));
3621 return (error);
3622 }
3623
3670
3671 DPRINTF("Attaching to %s\n", xenbus_get_node(dev));
3672
3673 /*
3674 * Basic initialization.
3675 * After this block it is safe to call xbb_detach()
3676 * to clean up any allocated data for this instance.
3677 */

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

3696 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3697 "feature-flush-cache", "1");
3698 if (error) {
3699 xbb_attach_failed(xbb, error, "writing %s/feature-flush-cache",
3700 xenbus_get_node(xbb->dev));
3701 return (error);
3702 }
3703
3704 /*
3705 * Amazon EC2 client compatility. They refer to max-ring-pages
3706 * instead of to max-ring-page-order.
3707 */
3624 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3625 "max-ring-pages", "%zu", XBB_MAX_RING_PAGES);
3626 if (error) {
3627 xbb_attach_failed(xbb, error, "writing %s/max-ring-pages",
3628 xenbus_get_node(xbb->dev));
3629 return (error);
3630 }
3631
3708 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3709 "max-ring-pages", "%zu", XBB_MAX_RING_PAGES);
3710 if (error) {
3711 xbb_attach_failed(xbb, error, "writing %s/max-ring-pages",
3712 xenbus_get_node(xbb->dev));
3713 return (error);
3714 }
3715
3716 max_ring_page_order = flsl(XBB_MAX_RING_PAGES) - 1;
3632 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3717 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3718 "max-ring-page-order", "%u", max_ring_page_order);
3719 if (error) {
3720 xbb_attach_failed(xbb, error, "writing %s/max-ring-page-order",
3721 xenbus_get_node(xbb->dev));
3722 return (error);
3723 }
3724
3725 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3633 "max-requests", "%u", XBB_MAX_REQUESTS);
3634 if (error) {
3635 xbb_attach_failed(xbb, error, "writing %s/max-requests",
3636 xenbus_get_node(xbb->dev));
3637 return (error);
3638 }
3639
3640 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),

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

3857 switch (frontend_state) {
3858 case XenbusStateInitialising:
3859 break;
3860 case XenbusStateInitialised:
3861 case XenbusStateConnected:
3862 xbb_connect(xbb);
3863 break;
3864 case XenbusStateClosing:
3726 "max-requests", "%u", XBB_MAX_REQUESTS);
3727 if (error) {
3728 xbb_attach_failed(xbb, error, "writing %s/max-requests",
3729 xenbus_get_node(xbb->dev));
3730 return (error);
3731 }
3732
3733 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),

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

3950 switch (frontend_state) {
3951 case XenbusStateInitialising:
3952 break;
3953 case XenbusStateInitialised:
3954 case XenbusStateConnected:
3955 xbb_connect(xbb);
3956 break;
3957 case XenbusStateClosing:
3958 /*
3959 * Frontend has acknowledged Closing request.
3960 * Wait for Closed state.
3961 */
3962 break;
3865 case XenbusStateClosed:
3866 mtx_lock(&xbb->lock);
3867 xbb_shutdown(xbb);
3868 mtx_unlock(&xbb->lock);
3963 case XenbusStateClosed:
3964 mtx_lock(&xbb->lock);
3965 xbb_shutdown(xbb);
3966 mtx_unlock(&xbb->lock);
3869 if (frontend_state == XenbusStateClosed)
3870 xenbus_set_state(xbb->dev, XenbusStateClosed);
3967 xenbus_set_state(xbb->dev, XenbusStateClosed);
3871 break;
3872 default:
3873 xenbus_dev_fatal(xbb->dev, EINVAL, "saw state %d at frontend",
3874 frontend_state);
3875 break;
3876 }
3877}
3878

--- 24 unchanged lines hidden ---
3968 break;
3969 default:
3970 xenbus_dev_fatal(xbb->dev, EINVAL, "saw state %d at frontend",
3971 frontend_state);
3972 break;
3973 }
3974}
3975

--- 24 unchanged lines hidden ---