1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU General Public License v.2.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "lib.h"
#include "pv_map.h"
#include "pv_alloc.h"
/*
* Areas are maintained in size order, largest first.
*/
static void _insert_area(struct list *head, struct pv_area *a)
{
struct pv_area *pva;
list_iterate_items(pva, head) {
if (a->count > pva->count)
break;
}
list_add(&pva->list, &a->list);
}
static int _create_single_area(struct dm_pool *mem, struct pv_map *pvm,
uint32_t start, uint32_t length)
{
struct pv_area *pva;
if (!(pva = dm_pool_zalloc(mem, sizeof(*pva)))) {
stack;
return 0;
}
log_debug("Allowing allocation on %s start PE %" PRIu32 " length %"
PRIu32, dev_name(pvm->pv->dev), start, length);
pva->map = pvm;
pva->start = start;
pva->count = length;
_insert_area(&pvm->areas, pva);
return 1;
}
static int _create_alloc_areas_for_pv(struct dm_pool *mem, struct pv_map *pvm,
uint32_t start, uint32_t count)
{
struct pv_segment *peg;
uint32_t pe, end, area_len;
/* Only select extents from start to end inclusive */
end = start + count - 1;
if (end > pvm->pv->pe_count - 1)
end = pvm->pv->pe_count - 1;
pe = start;
/* Walk through complete ordered list of device segments */
list_iterate_items(peg, &pvm->pv->segments) {
/* pe holds the next extent we want to check */
/* Beyond the range we're interested in? */
if (pe > end)
break;
/* Skip if we haven't reached the first seg we want yet */
if (pe > peg->pe + peg->len - 1)
continue;
/* Free? */
if (peg->lvseg)
goto next;
/* How much of this peg do we need? */
area_len = (end >= peg->pe + peg->len - 1) ?
peg->len - (pe - peg->pe) : end - pe + 1;
if (!_create_single_area(mem, pvm, pe, area_len)) {
stack;
return 0;
}
next:
pe = peg->pe + peg->len;
}
return 1;
}
static int _create_all_areas_for_pv(struct dm_pool *mem, struct pv_map *pvm,
struct list *pe_ranges)
{
struct pe_range *aa;
if (!pe_ranges) {
/* Use whole PV */
if (!_create_alloc_areas_for_pv(mem, pvm, UINT32_C(0),
pvm->pv->pe_count)) {
stack;
return 0;
}
return 1;
}
list_iterate_items(aa, pe_ranges) {
if (!_create_alloc_areas_for_pv(mem, pvm, aa->start,
aa->count)) {
stack;
return 0;
}
}
return 1;
}
static int _create_maps(struct dm_pool *mem, struct list *pvs, struct list *pvms)
{
struct pv_map *pvm;
struct pv_list *pvl;
list_iterate_items(pvl, pvs) {
if (!(pvl->pv->status & ALLOCATABLE_PV))
continue;
if (!(pvm = dm_pool_zalloc(mem, sizeof(*pvm)))) {
stack;
return 0;
}
pvm->pv = pvl->pv;
list_init(&pvm->areas);
list_add(pvms, &pvm->list);
if (!_create_all_areas_for_pv(mem, pvm, pvl->pe_ranges)) {
stack;
return 0;
}
}
return 1;
}
/*
* Create list of PV areas available for this particular allocation
*/
struct list *create_pv_maps(struct dm_pool *mem, struct volume_group *vg,
struct list *allocatable_pvs)
{
struct list *pvms;
if (!(pvms = dm_pool_zalloc(mem, sizeof(*pvms)))) {
log_error("create_pv_maps alloc failed");
return NULL;
}
list_init(pvms);
if (!_create_maps(mem, allocatable_pvs, pvms)) {
log_error("Couldn't create physical volume maps in %s",
vg->name);
dm_pool_free(mem, pvms);
return NULL;
}
return pvms;
}
void consume_pv_area(struct pv_area *pva, uint32_t to_go)
{
list_del(&pva->list);
assert(to_go <= pva->count);
if (to_go < pva->count) {
/* split the area */
pva->start += to_go;
pva->count -= to_go;
_insert_area(&pva->map->areas, pva);
}
}
|