Commit 6583e3c7e811b13530b7b69fffbe29a7a28cd1f7

Authored by Kevin Wolf
Committed by Anthony Liguori
1 parent ed6ccf0f

l2_allocate: Write complete sectors

When modifying the L1 table, l2_allocate() needs to write complete sectors
instead of single entries. The L1 table is already in memory, reading it from
disk in the block layer to align the request is wasted performance.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing 1 changed file with 28 additions and 5 deletions
block/qcow2-cluster.c
@@ -171,6 +171,31 @@ static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset) @@ -171,6 +171,31 @@ static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
171 } 171 }
172 172
173 /* 173 /*
  174 + * Writes one sector of the L1 table to the disk (can't update single entries
  175 + * and we really don't want bdrv_pread to perform a read-modify-write)
  176 + */
  177 +#define L1_ENTRIES_PER_SECTOR (512 / 8)
  178 +static int write_l1_entry(BDRVQcowState *s, int l1_index)
  179 +{
  180 + uint64_t buf[L1_ENTRIES_PER_SECTOR];
  181 + int l1_start_index;
  182 + int i;
  183 +
  184 + l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
  185 + for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) {
  186 + buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
  187 + }
  188 +
  189 + if (bdrv_pwrite(s->hd, s->l1_table_offset + 8 * l1_start_index,
  190 + buf, sizeof(buf)) != sizeof(buf))
  191 + {
  192 + return -1;
  193 + }
  194 +
  195 + return 0;
  196 +}
  197 +
  198 +/*
174 * l2_allocate 199 * l2_allocate
175 * 200 *
176 * Allocate a new l2 entry in the file. If l1_index points to an already 201 * Allocate a new l2 entry in the file. If l1_index points to an already
@@ -184,7 +209,7 @@ static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index) @@ -184,7 +209,7 @@ static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
184 { 209 {
185 BDRVQcowState *s = bs->opaque; 210 BDRVQcowState *s = bs->opaque;
186 int min_index; 211 int min_index;
187 - uint64_t old_l2_offset, tmp; 212 + uint64_t old_l2_offset;
188 uint64_t *l2_table, l2_offset; 213 uint64_t *l2_table, l2_offset;
189 214
190 old_l2_offset = s->l1_table[l1_index]; 215 old_l2_offset = s->l1_table[l1_index];
@@ -196,11 +221,9 @@ static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index) @@ -196,11 +221,9 @@ static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
196 /* update the L1 entry */ 221 /* update the L1 entry */
197 222
198 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED; 223 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
199 -  
200 - tmp = cpu_to_be64(l2_offset | QCOW_OFLAG_COPIED);  
201 - if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp),  
202 - &tmp, sizeof(tmp)) != sizeof(tmp)) 224 + if (write_l1_entry(s, l1_index) < 0) {
203 return NULL; 225 return NULL;
  226 + }
204 227
205 /* allocate a new entry in the l2 cache */ 228 /* allocate a new entry in the l2 cache */
206 229