xen-blkback: don't leak stack data via response ring
[pandora-kernel.git] / drivers / block / xen-blkback / common.h
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License version 2
4  * as published by the Free Software Foundation; or, when distributed
5  * separately from the Linux kernel or incorporated into other
6  * software packages, subject to the following license:
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this source file (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24  * IN THE SOFTWARE.
25  */
26
27 #ifndef __XEN_BLKIF__BACKEND__COMMON_H__
28 #define __XEN_BLKIF__BACKEND__COMMON_H__
29
30 #include <linux/module.h>
31 #include <linux/interrupt.h>
32 #include <linux/slab.h>
33 #include <linux/blkdev.h>
34 #include <linux/vmalloc.h>
35 #include <linux/wait.h>
36 #include <linux/io.h>
37 #include <asm/setup.h>
38 #include <asm/pgalloc.h>
39 #include <asm/hypervisor.h>
40 #include <xen/grant_table.h>
41 #include <xen/xenbus.h>
42 #include <xen/interface/io/ring.h>
43 #include <xen/interface/io/blkif.h>
44 #include <xen/interface/io/protocols.h>
45
46 #define DRV_PFX "xen-blkback:"
47 #define DPRINTK(fmt, args...)                           \
48         pr_debug(DRV_PFX "(%s:%d) " fmt ".\n",          \
49                  __func__, __LINE__, ##args)
50
51
52 /* Not a real protocol.  Used to generate ring structs which contain
53  * the elements common to all protocols only.  This way we get a
54  * compiler-checkable way to use common struct elements, so we can
55  * avoid using switch(protocol) in a number of places.  */
56 struct blkif_common_request {
57         char dummy;
58 };
59
60 /* i386 protocol version */
61 #pragma pack(push, 4)
62
63 struct blkif_x86_32_request_rw {
64         blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
65         struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
66 };
67
68 struct blkif_x86_32_request_discard {
69         blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
70         uint64_t nr_sectors;
71 };
72
73 struct blkif_x86_32_request {
74         uint8_t        operation;    /* BLKIF_OP_???                         */
75         uint8_t        nr_segments;  /* number of segments                   */
76         blkif_vdev_t   handle;       /* only for read/write requests         */
77         uint64_t       id;           /* private guest value, echoed in resp  */
78         union {
79                 struct blkif_x86_32_request_rw rw;
80                 struct blkif_x86_32_request_discard discard;
81         } u;
82 };
83 #pragma pack(pop)
84
85 /* x86_64 protocol version */
86
87 struct blkif_x86_64_request_rw {
88         blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
89         struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
90 };
91
92 struct blkif_x86_64_request_discard {
93         blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
94         uint64_t nr_sectors;
95 };
96
97 struct blkif_x86_64_request {
98         uint8_t        operation;    /* BLKIF_OP_???                         */
99         uint8_t        nr_segments;  /* number of segments                   */
100         blkif_vdev_t   handle;       /* only for read/write requests         */
101         uint64_t       __attribute__((__aligned__(8))) id;
102         union {
103                 struct blkif_x86_64_request_rw rw;
104                 struct blkif_x86_64_request_discard discard;
105         } u;
106 };
107
108 DEFINE_RING_TYPES(blkif_common, struct blkif_common_request,
109                   struct blkif_response);
110 DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request,
111                   struct blkif_response __packed);
112 DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request,
113                   struct blkif_response);
114
115 union blkif_back_rings {
116         struct blkif_back_ring        native;
117         struct blkif_common_back_ring common;
118         struct blkif_x86_32_back_ring x86_32;
119         struct blkif_x86_64_back_ring x86_64;
120 };
121
122 enum blkif_protocol {
123         BLKIF_PROTOCOL_NATIVE = 1,
124         BLKIF_PROTOCOL_X86_32 = 2,
125         BLKIF_PROTOCOL_X86_64 = 3,
126 };
127
128 enum blkif_backend_type {
129         BLKIF_BACKEND_PHY  = 1,
130         BLKIF_BACKEND_FILE = 2,
131 };
132
133 struct xen_vbd {
134         /* What the domain refers to this vbd as. */
135         blkif_vdev_t            handle;
136         /* Non-zero -> read-only */
137         unsigned char           readonly;
138         /* VDISK_xxx */
139         unsigned char           type;
140         /* phys device that this vbd maps to. */
141         u32                     pdevice;
142         struct block_device     *bdev;
143         /* Cached size parameter. */
144         sector_t                size;
145         bool                    flush_support;
146 };
147
148 struct backend_info;
149
150 struct xen_blkif {
151         /* Unique identifier for this interface. */
152         domid_t                 domid;
153         unsigned int            handle;
154         /* Physical parameters of the comms window. */
155         unsigned int            irq;
156         /* Comms information. */
157         enum blkif_protocol     blk_protocol;
158         enum blkif_backend_type blk_backend_type;
159         union blkif_back_rings  blk_rings;
160         void                    *blk_ring;
161         /* The VBD attached to this interface. */
162         struct xen_vbd          vbd;
163         /* Back pointer to the backend_info. */
164         struct backend_info     *be;
165         /* Private fields. */
166         spinlock_t              blk_ring_lock;
167         atomic_t                refcnt;
168
169         wait_queue_head_t       wq;
170         /* for barrier (drain) requests */
171         struct completion       drain_complete;
172         atomic_t                drain;
173         /* One thread per one blkif. */
174         struct task_struct      *xenblkd;
175         unsigned int            waiting_reqs;
176
177         /* statistics */
178         unsigned long           st_print;
179         int                     st_rd_req;
180         int                     st_wr_req;
181         int                     st_oo_req;
182         int                     st_f_req;
183         int                     st_ds_req;
184         int                     st_rd_sect;
185         int                     st_wr_sect;
186
187         wait_queue_head_t       waiting_to_free;
188         /* Thread shutdown wait queue. */
189         wait_queue_head_t       shutdown_wq;
190 };
191
192
193 #define vbd_sz(_v)      ((_v)->bdev->bd_part ? \
194                          (_v)->bdev->bd_part->nr_sects : \
195                           get_capacity((_v)->bdev->bd_disk))
196
197 #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
198 #define xen_blkif_put(_b)                               \
199         do {                                            \
200                 if (atomic_dec_and_test(&(_b)->refcnt)) \
201                         wake_up(&(_b)->waiting_to_free);\
202         } while (0)
203
204 struct phys_req {
205         unsigned short          dev;
206         blkif_sector_t          nr_sects;
207         struct block_device     *bdev;
208         blkif_sector_t          sector_number;
209 };
210 int xen_blkif_interface_init(void);
211
212 int xen_blkif_xenbus_init(void);
213
214 irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
215 int xen_blkif_schedule(void *arg);
216
217 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
218                               struct backend_info *be, int state);
219
220 int xen_blkbk_barrier(struct xenbus_transaction xbt,
221                       struct backend_info *be, int state);
222 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
223
224 static inline void blkif_get_x86_32_req(struct blkif_request *dst,
225                                         struct blkif_x86_32_request *src)
226 {
227         int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
228         dst->operation = ACCESS_ONCE(src->operation);
229         dst->nr_segments = src->nr_segments;
230         dst->handle = src->handle;
231         dst->id = src->id;
232         switch (dst->operation) {
233         case BLKIF_OP_READ:
234         case BLKIF_OP_WRITE:
235         case BLKIF_OP_WRITE_BARRIER:
236         case BLKIF_OP_FLUSH_DISKCACHE:
237                 dst->u.rw.sector_number = src->u.rw.sector_number;
238                 barrier();
239                 if (n > dst->nr_segments)
240                         n = dst->nr_segments;
241                 for (i = 0; i < n; i++)
242                         dst->u.rw.seg[i] = src->u.rw.seg[i];
243                 break;
244         case BLKIF_OP_DISCARD:
245                 dst->u.discard.sector_number = src->u.discard.sector_number;
246                 dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
247                 break;
248         default:
249                 break;
250         }
251 }
252
253 static inline void blkif_get_x86_64_req(struct blkif_request *dst,
254                                         struct blkif_x86_64_request *src)
255 {
256         int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
257         dst->operation = ACCESS_ONCE(src->operation);
258         dst->nr_segments = src->nr_segments;
259         dst->handle = src->handle;
260         dst->id = src->id;
261         switch (dst->operation) {
262         case BLKIF_OP_READ:
263         case BLKIF_OP_WRITE:
264         case BLKIF_OP_WRITE_BARRIER:
265         case BLKIF_OP_FLUSH_DISKCACHE:
266                 dst->u.rw.sector_number = src->u.rw.sector_number;
267                 barrier();
268                 if (n > dst->nr_segments)
269                         n = dst->nr_segments;
270                 for (i = 0; i < n; i++)
271                         dst->u.rw.seg[i] = src->u.rw.seg[i];
272                 break;
273         case BLKIF_OP_DISCARD:
274                 dst->u.discard.sector_number = src->u.discard.sector_number;
275                 dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
276                 break;
277         default:
278                 break;
279         }
280 }
281
282 #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */