1 /* 2 * QEMU Enhanced Disk Format Table I/O 3 * 4 * Copyright IBM, Corp. 2010 5 * 6 * Authors: 7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 8 * Anthony Liguori <aliguori@us.ibm.com> 9 * 10 * This work is licensed under the terms of the GNU LGPL, version 2 or later. 11 * See the COPYING.LIB file in the top-level directory. 12 * 13 */ 14 15 #include "trace.h" 16 #include "qemu/sockets.h" /* for EINPROGRESS on Windows */ 17 #include "qed.h" 18 19 typedef struct { 20 GenericCB gencb; 21 BDRVQEDState *s; 22 QEDTable *table; 23 24 struct iovec iov; 25 QEMUIOVector qiov; 26 } QEDReadTableCB; 27 28 static void qed_read_table_cb(void *opaque, int ret) 29 { 30 QEDReadTableCB *read_table_cb = opaque; 31 QEDTable *table = read_table_cb->table; 32 int noffsets = read_table_cb->qiov.size / sizeof(uint64_t); 33 int i; 34 35 /* Handle I/O error */ 36 if (ret) { 37 goto out; 38 } 39 40 /* Byteswap offsets */ 41 for (i = 0; i < noffsets; i++) { 42 table->offsets[i] = le64_to_cpu(table->offsets[i]); 43 } 44 45 out: 46 /* Completion */ 47 trace_qed_read_table_cb(read_table_cb->s, read_table_cb->table, ret); 48 gencb_complete(&read_table_cb->gencb, ret); 49 } 50 51 static void qed_read_table(BDRVQEDState *s, uint64_t offset, QEDTable *table, 52 BlockDriverCompletionFunc *cb, void *opaque) 53 { 54 QEDReadTableCB *read_table_cb = gencb_alloc(sizeof(*read_table_cb), 55 cb, opaque); 56 QEMUIOVector *qiov = &read_table_cb->qiov; 57 58 trace_qed_read_table(s, offset, table); 59 60 read_table_cb->s = s; 61 read_table_cb->table = table; 62 read_table_cb->iov.iov_base = table->offsets, 63 read_table_cb->iov.iov_len = s->header.cluster_size * s->header.table_size, 64 65 qemu_iovec_init_external(qiov, &read_table_cb->iov, 1); 66 bdrv_aio_readv(s->bs->file, offset / BDRV_SECTOR_SIZE, qiov, 67 qiov->size / BDRV_SECTOR_SIZE, 68 qed_read_table_cb, read_table_cb); 69 } 70 71 typedef struct { 72 GenericCB gencb; 73 BDRVQEDState *s; 74 QEDTable *orig_table; 75 QEDTable *table; 76 bool flush; /* flush after write? */ 77 78 struct iovec iov; 79 QEMUIOVector qiov; 80 } QEDWriteTableCB; 81 82 static void qed_write_table_cb(void *opaque, int ret) 83 { 84 QEDWriteTableCB *write_table_cb = opaque; 85 86 trace_qed_write_table_cb(write_table_cb->s, 87 write_table_cb->orig_table, 88 write_table_cb->flush, 89 ret); 90 91 if (ret) { 92 goto out; 93 } 94 95 if (write_table_cb->flush) { 96 /* We still need to flush first */ 97 write_table_cb->flush = false; 98 bdrv_aio_flush(write_table_cb->s->bs, qed_write_table_cb, 99 write_table_cb); 100 return; 101 } 102 103 out: 104 qemu_vfree(write_table_cb->table); 105 gencb_complete(&write_table_cb->gencb, ret); 106 } 107 108 /** 109 * Write out an updated part or all of a table 110 * 111 * @s: QED state 112 * @offset: Offset of table in image file, in bytes 113 * @table: Table 114 * @index: Index of first element 115 * @n: Number of elements 116 * @flush: Whether or not to sync to disk 117 * @cb: Completion function 118 * @opaque: Argument for completion function 119 */ 120 static void qed_write_table(BDRVQEDState *s, uint64_t offset, QEDTable *table, 121 unsigned int index, unsigned int n, bool flush, 122 BlockDriverCompletionFunc *cb, void *opaque) 123 { 124 QEDWriteTableCB *write_table_cb; 125 unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1; 126 unsigned int start, end, i; 127 size_t len_bytes; 128 129 trace_qed_write_table(s, offset, table, index, n); 130 131 /* Calculate indices of the first and one after last elements */ 132 start = index & ~sector_mask; 133 end = (index + n + sector_mask) & ~sector_mask; 134 135 len_bytes = (end - start) * sizeof(uint64_t); 136 137 write_table_cb = gencb_alloc(sizeof(*write_table_cb), cb, opaque); 138 write_table_cb->s = s; 139 write_table_cb->orig_table = table; 140 write_table_cb->flush = flush; 141 write_table_cb->table = qemu_blockalign(s->bs, len_bytes); 142 write_table_cb->iov.iov_base = write_table_cb->table->offsets; 143 write_table_cb->iov.iov_len = len_bytes; 144 qemu_iovec_init_external(&write_table_cb->qiov, &write_table_cb->iov, 1); 145 146 /* Byteswap table */ 147 for (i = start; i < end; i++) { 148 uint64_t le_offset = cpu_to_le64(table->offsets[i]); 149 write_table_cb->table->offsets[i - start] = le_offset; 150 } 151 152 /* Adjust for offset into table */ 153 offset += start * sizeof(uint64_t); 154 155 bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE, 156 &write_table_cb->qiov, 157 write_table_cb->qiov.size / BDRV_SECTOR_SIZE, 158 qed_write_table_cb, write_table_cb); 159 } 160 161 /** 162 * Propagate return value from async callback 163 */ 164 static void qed_sync_cb(void *opaque, int ret) 165 { 166 *(int *)opaque = ret; 167 } 168 169 int qed_read_l1_table_sync(BDRVQEDState *s) 170 { 171 int ret = -EINPROGRESS; 172 173 qed_read_table(s, s->header.l1_table_offset, 174 s->l1_table, qed_sync_cb, &ret); 175 while (ret == -EINPROGRESS) { 176 aio_poll(bdrv_get_aio_context(s->bs), true); 177 } 178 179 return ret; 180 } 181 182 void qed_write_l1_table(BDRVQEDState *s, unsigned int index, unsigned int n, 183 BlockDriverCompletionFunc *cb, void *opaque) 184 { 185 BLKDBG_EVENT(s->bs->file, BLKDBG_L1_UPDATE); 186 qed_write_table(s, s->header.l1_table_offset, 187 s->l1_table, index, n, false, cb, opaque); 188 } 189 190 int qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index, 191 unsigned int n) 192 { 193 int ret = -EINPROGRESS; 194 195 qed_write_l1_table(s, index, n, qed_sync_cb, &ret); 196 while (ret == -EINPROGRESS) { 197 aio_poll(bdrv_get_aio_context(s->bs), true); 198 } 199 200 return ret; 201 } 202 203 typedef struct { 204 GenericCB gencb; 205 BDRVQEDState *s; 206 uint64_t l2_offset; 207 QEDRequest *request; 208 } QEDReadL2TableCB; 209 210 static void qed_read_l2_table_cb(void *opaque, int ret) 211 { 212 QEDReadL2TableCB *read_l2_table_cb = opaque; 213 QEDRequest *request = read_l2_table_cb->request; 214 BDRVQEDState *s = read_l2_table_cb->s; 215 CachedL2Table *l2_table = request->l2_table; 216 uint64_t l2_offset = read_l2_table_cb->l2_offset; 217 218 if (ret) { 219 /* can't trust loaded L2 table anymore */ 220 qed_unref_l2_cache_entry(l2_table); 221 request->l2_table = NULL; 222 } else { 223 l2_table->offset = l2_offset; 224 225 qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 226 227 /* This is guaranteed to succeed because we just committed the entry 228 * to the cache. 229 */ 230 request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); 231 assert(request->l2_table != NULL); 232 } 233 234 gencb_complete(&read_l2_table_cb->gencb, ret); 235 } 236 237 void qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, uint64_t offset, 238 BlockDriverCompletionFunc *cb, void *opaque) 239 { 240 QEDReadL2TableCB *read_l2_table_cb; 241 242 qed_unref_l2_cache_entry(request->l2_table); 243 244 /* Check for cached L2 entry */ 245 request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset); 246 if (request->l2_table) { 247 cb(opaque, 0); 248 return; 249 } 250 251 request->l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 252 request->l2_table->table = qed_alloc_table(s); 253 254 read_l2_table_cb = gencb_alloc(sizeof(*read_l2_table_cb), cb, opaque); 255 read_l2_table_cb->s = s; 256 read_l2_table_cb->l2_offset = offset; 257 read_l2_table_cb->request = request; 258 259 BLKDBG_EVENT(s->bs->file, BLKDBG_L2_LOAD); 260 qed_read_table(s, offset, request->l2_table->table, 261 qed_read_l2_table_cb, read_l2_table_cb); 262 } 263 264 int qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request, uint64_t offset) 265 { 266 int ret = -EINPROGRESS; 267 268 qed_read_l2_table(s, request, offset, qed_sync_cb, &ret); 269 while (ret == -EINPROGRESS) { 270 aio_poll(bdrv_get_aio_context(s->bs), true); 271 } 272 273 return ret; 274 } 275 276 void qed_write_l2_table(BDRVQEDState *s, QEDRequest *request, 277 unsigned int index, unsigned int n, bool flush, 278 BlockDriverCompletionFunc *cb, void *opaque) 279 { 280 BLKDBG_EVENT(s->bs->file, BLKDBG_L2_UPDATE); 281 qed_write_table(s, request->l2_table->offset, 282 request->l2_table->table, index, n, flush, cb, opaque); 283 } 284 285 int qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request, 286 unsigned int index, unsigned int n, bool flush) 287 { 288 int ret = -EINPROGRESS; 289 290 qed_write_l2_table(s, request, index, n, flush, qed_sync_cb, &ret); 291 while (ret == -EINPROGRESS) { 292 aio_poll(bdrv_get_aio_context(s->bs), true); 293 } 294 295 return ret; 296 } 297