|
| 1 | +// Copyright 2016 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package qcow2 |
| 6 | + |
| 7 | +import ( |
| 8 | + "log" |
| 9 | + "syscall" |
| 10 | + |
| 11 | + "github.com/zchee/go-qcow2/internal/mem" |
| 12 | +) |
| 13 | + |
| 14 | +const DEBUG_ALLOC2 = false |
| 15 | + |
| 16 | +func growL1Table(bs *BlockDriverState, minSize uint64, exactSize bool) error { |
| 17 | + s := bs.Opaque |
| 18 | + var newL1Size int64 |
| 19 | + |
| 20 | + if minSize <= uint64(s.L1Size) { |
| 21 | + return nil |
| 22 | + } |
| 23 | + |
| 24 | + // Do a sanity check on min_size before trying to calculate new_l1_size |
| 25 | + // (this prevents overflows during the while loop for the calculation of |
| 26 | + // new_l1_size) |
| 27 | + if minSize > INT_MAX/UINT64_SIZE { |
| 28 | + return syscall.EFBIG |
| 29 | + } |
| 30 | + |
| 31 | + if exactSize { |
| 32 | + newL1Size = int64(minSize) |
| 33 | + } else { |
| 34 | + // Bump size up to reduce the number of times we have to grow |
| 35 | + newL1Size = int64(s.L1Size) |
| 36 | + if newL1Size == 0 { |
| 37 | + newL1Size = 1 |
| 38 | + } |
| 39 | + for minSize > uint64(newL1Size) { |
| 40 | + newL1Size = (newL1Size*3 + 1) / 2 |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if newL1Size > MAX_L1_SIZE/UINT64_SIZE { |
| 45 | + return syscall.EFBIG |
| 46 | + } |
| 47 | + |
| 48 | + log.Printf("grow l1_table from %d to %d\n", s.L1Size, newL1Size) |
| 49 | + |
| 50 | + newL1Size2 := UINT64_SIZE * newL1Size |
| 51 | + log.Printf("newL1Size2: %+v\n", newL1Size2) |
| 52 | + align := bdrvOptMemAlign(bs) |
| 53 | + newL1Table := posixMemalign(uint64(align), uint64(alignOffset(newL1Size2, 512))) |
| 54 | + log.Printf("newL1Table: %+v\n", newL1Table) |
| 55 | + if newL1Table == nil { |
| 56 | + return syscall.ENOMEM |
| 57 | + } |
| 58 | + mem.Set(newL1Table, byte(alignOffset(newL1Size2, 512))) |
| 59 | + |
| 60 | + mem.Cpy(newL1Table, []byte{byte(s.L1Table)}, uintptr(s.L1Size*UINT64_SIZE)) |
| 61 | + |
| 62 | + // write new table (align to cluster) |
| 63 | + newL1TableOffset, err := AllocClusters(bs, uint64(newL1Size2)) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + log.Printf("newL1TableOffset: %+v\n", newL1TableOffset) |
| 68 | + |
| 69 | + // ret = qcow2_cache_flush(bs, s->refcount_block_cache); |
| 70 | + // if (ret < 0) { |
| 71 | + // goto fail; |
| 72 | + // } |
| 73 | + // |
| 74 | + // /* the L1 position has not yet been updated, so these clusters must |
| 75 | + // * indeed be completely free */ |
| 76 | + // ret = qcow2_pre_write_overlap_check(bs, 0, new_l1_table_offset, |
| 77 | + // new_l1_size2); |
| 78 | + // if (ret < 0) { |
| 79 | + // goto fail; |
| 80 | + // } |
| 81 | + // |
| 82 | + // BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE); |
| 83 | + // for(i = 0; i < s->l1_size; i++) |
| 84 | + // new_l1_table[i] = cpu_to_be64(new_l1_table[i]); |
| 85 | + // ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset, |
| 86 | + // new_l1_table, new_l1_size2); |
| 87 | + // if (ret < 0) |
| 88 | + // goto fail; |
| 89 | + // for(i = 0; i < s->l1_size; i++) |
| 90 | + // new_l1_table[i] = be64_to_cpu(new_l1_table[i]); |
| 91 | + // |
| 92 | + // /* set new table */ |
| 93 | + // BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE); |
| 94 | + // stl_be_p(data, new_l1_size); |
| 95 | + // stq_be_p(data + 4, new_l1_table_offset); |
| 96 | + // ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size), |
| 97 | + // data, sizeof(data)); |
| 98 | + // if (ret < 0) { |
| 99 | + // goto fail; |
| 100 | + // } |
| 101 | + // qemu_vfree(s->l1_table); |
| 102 | + // old_l1_table_offset = s->l1_table_offset; |
| 103 | + // s->l1_table_offset = new_l1_table_offset; |
| 104 | + // s->l1_table = new_l1_table; |
| 105 | + // old_l1_size = s->l1_size; |
| 106 | + // s->l1_size = new_l1_size; |
| 107 | + // qcow2_free_clusters(bs, old_l1_table_offset, old_l1_size * sizeof(uint64_t), |
| 108 | + // QCOW2_DISCARD_OTHER); |
| 109 | + // return 0; |
| 110 | + // fail: |
| 111 | + // qemu_vfree(new_l1_table); |
| 112 | + // qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2, |
| 113 | + // QCOW2_DISCARD_OTHER); |
| 114 | + // return ret; |
| 115 | + return nil |
| 116 | +} |
0 commit comments