Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 : : * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
4 : : *
5 : : * This file is part of LVM2.
6 : : *
7 : : * This copyrighted material is made available to anyone wishing to use,
8 : : * modify, copy, or redistribute it subject to the terms and conditions
9 : : * of the GNU Lesser General Public License v.2.1.
10 : : *
11 : : * You should have received a copy of the GNU Lesser General Public License
12 : : * along with this program; if not, write to the Free Software Foundation,
13 : : * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 : : */
15 : :
16 : : #include "lib.h"
17 : : #include "pv_map.h"
18 : : #include "pv_alloc.h"
19 : :
20 : : #include <assert.h>
21 : :
22 : : /*
23 : : * Areas are maintained in size order, largest first.
24 : : *
25 : : * FIXME Cope with overlap.
26 : : */
27 : 0 : static void _insert_area(struct dm_list *head, struct pv_area *a, unsigned reduced)
28 : : {
29 : : struct pv_area *pva;
30 [ # # ]: 0 : uint32_t count = reduced ? a->unreserved : a->count;
31 : :
32 [ # # ]: 0 : dm_list_iterate_items(pva, head)
33 [ # # ]: 0 : if (count > pva->count)
34 : 0 : break;
35 : :
36 : 0 : dm_list_add(&pva->list, &a->list);
37 : 0 : a->map->pe_count += a->count;
38 : 0 : }
39 : :
40 : 0 : static void _remove_area(struct pv_area *a)
41 : : {
42 : 0 : dm_list_del(&a->list);
43 : 0 : a->map->pe_count -= a->count;
44 : 0 : }
45 : :
46 : 0 : static int _create_single_area(struct dm_pool *mem, struct pv_map *pvm,
47 : : uint32_t start, uint32_t length)
48 : : {
49 : : struct pv_area *pva;
50 : :
51 [ # # ]: 0 : if (!(pva = dm_pool_zalloc(mem, sizeof(*pva))))
52 : 0 : return_0;
53 : :
54 : 0 : log_debug("Allowing allocation on %s start PE %" PRIu32 " length %"
55 : : PRIu32, pv_dev_name(pvm->pv), start, length);
56 : 0 : pva->map = pvm;
57 : 0 : pva->start = start;
58 : 0 : pva->count = length;
59 : 0 : pva->unreserved = pva->count;
60 : 0 : _insert_area(&pvm->areas, pva, 0);
61 : :
62 : 0 : return 1;
63 : : }
64 : :
65 : 0 : static int _create_alloc_areas_for_pv(struct dm_pool *mem, struct pv_map *pvm,
66 : : uint32_t start, uint32_t count)
67 : : {
68 : : struct pv_segment *peg;
69 : : uint32_t pe, end, area_len;
70 : :
71 : : /* Only select extents from start to end inclusive */
72 : 0 : end = start + count - 1;
73 [ # # ]: 0 : if (end > pvm->pv->pe_count - 1)
74 : 0 : end = pvm->pv->pe_count - 1;
75 : :
76 : 0 : pe = start;
77 : :
78 : : /* Walk through complete ordered list of device segments */
79 [ # # ]: 0 : dm_list_iterate_items(peg, &pvm->pv->segments) {
80 : : /* pe holds the next extent we want to check */
81 : :
82 : : /* Beyond the range we're interested in? */
83 [ # # ]: 0 : if (pe > end)
84 : 0 : break;
85 : :
86 : : /* Skip if we haven't reached the first seg we want yet */
87 [ # # ]: 0 : if (pe > peg->pe + peg->len - 1)
88 : 0 : continue;
89 : :
90 : : /* Free? */
91 [ # # ]: 0 : if (peg->lvseg)
92 : 0 : goto next;
93 : :
94 : : /* How much of this peg do we need? */
95 [ # # ]: 0 : area_len = (end >= peg->pe + peg->len - 1) ?
96 : 0 : peg->len - (pe - peg->pe) : end - pe + 1;
97 : :
98 [ # # ]: 0 : if (!_create_single_area(mem, pvm, pe, area_len))
99 : 0 : return_0;
100 : :
101 : : next:
102 : 0 : pe = peg->pe + peg->len;
103 : : }
104 : :
105 : 0 : return 1;
106 : : }
107 : :
108 : 0 : static int _create_all_areas_for_pv(struct dm_pool *mem, struct pv_map *pvm,
109 : : struct dm_list *pe_ranges)
110 : : {
111 : : struct pe_range *aa;
112 : :
113 [ # # ]: 0 : if (!pe_ranges) {
114 : : /* Use whole PV */
115 [ # # ]: 0 : if (!_create_alloc_areas_for_pv(mem, pvm, UINT32_C(0),
116 : 0 : pvm->pv->pe_count))
117 : 0 : return_0;
118 : :
119 : 0 : return 1;
120 : : }
121 : :
122 [ # # ]: 0 : dm_list_iterate_items(aa, pe_ranges) {
123 [ # # ]: 0 : if (!_create_alloc_areas_for_pv(mem, pvm, aa->start,
124 : : aa->count))
125 : 0 : return_0;
126 : : }
127 : :
128 : 0 : return 1;
129 : : }
130 : :
131 : 0 : static int _create_maps(struct dm_pool *mem, struct dm_list *pvs, struct dm_list *pvms)
132 : : {
133 : : struct pv_map *pvm, *pvm2;
134 : : struct pv_list *pvl;
135 : :
136 [ # # ]: 0 : dm_list_iterate_items(pvl, pvs) {
137 [ # # ]: 0 : if (!(pvl->pv->status & ALLOCATABLE_PV))
138 : 0 : continue;
139 [ # # ]: 0 : if (is_missing_pv(pvl->pv))
140 : 0 : continue;
141 [ # # ]: 0 : assert(pvl->pv->dev);
142 : :
143 : 0 : pvm = NULL;
144 : :
145 [ # # ]: 0 : dm_list_iterate_items(pvm2, pvms)
146 [ # # ]: 0 : if (pvm2->pv->dev == pvl->pv->dev) {
147 : 0 : pvm = pvm2;
148 : 0 : break;
149 : : }
150 : :
151 [ # # ]: 0 : if (!pvm) {
152 [ # # ]: 0 : if (!(pvm = dm_pool_zalloc(mem, sizeof(*pvm))))
153 : 0 : return_0;
154 : :
155 : 0 : pvm->pv = pvl->pv;
156 : 0 : dm_list_init(&pvm->areas);
157 : 0 : dm_list_add(pvms, &pvm->list);
158 : : }
159 : :
160 [ # # ]: 0 : if (!_create_all_areas_for_pv(mem, pvm, pvl->pe_ranges))
161 : 0 : return_0;
162 : : }
163 : :
164 : 0 : return 1;
165 : : }
166 : :
167 : : /*
168 : : * Create list of PV areas available for this particular allocation
169 : : */
170 : 0 : struct dm_list *create_pv_maps(struct dm_pool *mem, struct volume_group *vg,
171 : : struct dm_list *allocatable_pvs)
172 : : {
173 : : struct dm_list *pvms;
174 : :
175 [ # # ]: 0 : if (!(pvms = dm_pool_zalloc(mem, sizeof(*pvms)))) {
176 : 0 : log_error("create_pv_maps alloc failed");
177 : 0 : return NULL;
178 : : }
179 : :
180 : 0 : dm_list_init(pvms);
181 : :
182 [ # # ]: 0 : if (!_create_maps(mem, allocatable_pvs, pvms)) {
183 : 0 : log_error("Couldn't create physical volume maps in %s",
184 : : vg->name);
185 : 0 : dm_pool_free(mem, pvms);
186 : 0 : return NULL;
187 : : }
188 : :
189 : 0 : return pvms;
190 : : }
191 : :
192 : 0 : void consume_pv_area(struct pv_area *pva, uint32_t to_go)
193 : : {
194 : 0 : _remove_area(pva);
195 : :
196 [ # # ]: 0 : assert(to_go <= pva->count);
197 : :
198 [ # # ]: 0 : if (to_go < pva->count) {
199 : : /* split the area */
200 : 0 : pva->start += to_go;
201 : 0 : pva->count -= to_go;
202 : 0 : pva->unreserved = pva->count;
203 : 0 : _insert_area(&pva->map->areas, pva, 0);
204 : : }
205 : 0 : }
206 : :
207 : : /*
208 : : * Remove an area from list and reinsert it based on its new smaller size
209 : : * after a provisional allocation.
210 : : */
211 : 0 : void reinsert_reduced_pv_area(struct pv_area *pva)
212 : : {
213 : 0 : _remove_area(pva);
214 : 0 : _insert_area(&pva->map->areas, pva, 1);
215 : 0 : }
216 : :
217 : 0 : uint32_t pv_maps_size(struct dm_list *pvms)
218 : : {
219 : : struct pv_map *pvm;
220 : 0 : uint32_t pe_count = 0;
221 : :
222 [ # # ]: 0 : dm_list_iterate_items(pvm, pvms)
223 : 0 : pe_count += pvm->pe_count;
224 : :
225 : 0 : return pe_count;
226 : : }
|