1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include "qemu/osdep.h"
16#include "qed.h"
17
18
19
20
21
22
23
24
25
26
27
28
29
30static unsigned int qed_count_contiguous_clusters(BDRVQEDState *s,
31 QEDTable *table,
32 unsigned int index,
33 unsigned int n,
34 uint64_t *offset)
35{
36 unsigned int end = MIN(index + n, s->table_nelems);
37 uint64_t last = table->offsets[index];
38 unsigned int i;
39
40 *offset = last;
41
42 for (i = index + 1; i < end; i++) {
43 if (qed_offset_is_unalloc_cluster(last)) {
44
45 if (!qed_offset_is_unalloc_cluster(table->offsets[i])) {
46 break;
47 }
48 } else if (qed_offset_is_zero_cluster(last)) {
49
50 if (!qed_offset_is_zero_cluster(table->offsets[i])) {
51 break;
52 }
53 } else {
54
55 if (table->offsets[i] != last + s->header.cluster_size) {
56 break;
57 }
58 last = table->offsets[i];
59 }
60 }
61 return i - index;
62}
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91int coroutine_fn qed_find_cluster(BDRVQEDState *s, QEDRequest *request,
92 uint64_t pos, size_t *len,
93 uint64_t *img_offset)
94{
95 uint64_t l2_offset;
96 uint64_t offset = 0;
97 unsigned int index;
98 unsigned int n;
99 int ret;
100
101
102
103
104 *len = MIN(*len, (((pos >> s->l1_shift) + 1) << s->l1_shift) - pos);
105
106 l2_offset = s->l1_table->offsets[qed_l1_index(s, pos)];
107 if (qed_offset_is_unalloc_cluster(l2_offset)) {
108 *img_offset = 0;
109 return QED_CLUSTER_L1;
110 }
111 if (!qed_check_table_offset(s, l2_offset)) {
112 *img_offset = *len = 0;
113 return -EINVAL;
114 }
115
116 ret = qed_read_l2_table(s, request, l2_offset);
117 if (ret) {
118 goto out;
119 }
120
121 index = qed_l2_index(s, pos);
122 n = qed_bytes_to_clusters(s, qed_offset_into_cluster(s, pos) + *len);
123 n = qed_count_contiguous_clusters(s, request->l2_table->table,
124 index, n, &offset);
125
126 if (qed_offset_is_unalloc_cluster(offset)) {
127 ret = QED_CLUSTER_L2;
128 } else if (qed_offset_is_zero_cluster(offset)) {
129 ret = QED_CLUSTER_ZERO;
130 } else if (qed_check_cluster_offset(s, offset)) {
131 ret = QED_CLUSTER_FOUND;
132 } else {
133 ret = -EINVAL;
134 }
135
136 *len = MIN(*len,
137 n * s->header.cluster_size - qed_offset_into_cluster(s, pos));
138
139out:
140 *img_offset = offset;
141 return ret;
142}
143