1fb9cffefSMatthew Dillon /*-
2fb9cffefSMatthew Dillon * Copyright (c) 2002 Marcel Moolenaar
3fb9cffefSMatthew Dillon * All rights reserved.
4fb9cffefSMatthew Dillon *
5fb9cffefSMatthew Dillon * Redistribution and use in source and binary forms, with or without
6fb9cffefSMatthew Dillon * modification, are permitted provided that the following conditions
7fb9cffefSMatthew Dillon * are met:
8fb9cffefSMatthew Dillon *
9fb9cffefSMatthew Dillon * 1. Redistributions of source code must retain the above copyright
10fb9cffefSMatthew Dillon * notice, this list of conditions and the following disclaimer.
11fb9cffefSMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
12fb9cffefSMatthew Dillon * notice, this list of conditions and the following disclaimer in the
13fb9cffefSMatthew Dillon * documentation and/or other materials provided with the distribution.
14fb9cffefSMatthew Dillon *
15fb9cffefSMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16fb9cffefSMatthew Dillon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17fb9cffefSMatthew Dillon * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18fb9cffefSMatthew Dillon * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19fb9cffefSMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20fb9cffefSMatthew Dillon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21fb9cffefSMatthew Dillon * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22fb9cffefSMatthew Dillon * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23fb9cffefSMatthew Dillon * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24fb9cffefSMatthew Dillon * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25fb9cffefSMatthew Dillon *
26fb9cffefSMatthew Dillon * $FreeBSD: src/sbin/gpt/map.c,v 1.6 2005/08/31 01:47:19 marcel Exp $
27fb9cffefSMatthew Dillon */
28fb9cffefSMatthew Dillon
293f7b7260SSascha Wildner #include <sys/param.h>
30fb9cffefSMatthew Dillon #include <err.h>
31fb9cffefSMatthew Dillon #include <stdio.h>
32fb9cffefSMatthew Dillon #include <stdlib.h>
33fb9cffefSMatthew Dillon
34fb9cffefSMatthew Dillon #include "map.h"
35fb9cffefSMatthew Dillon
36a844b566SFrançois Tigeot #define ROUNDTO 2048 // must be a power of two
373f7b7260SSascha Wildner #define ROUNDDOWN(x) rounddown2(x - 1, ROUNDTO)
38a844b566SFrançois Tigeot #define ROUNDUP(x) (ROUNDDOWN(x) + ROUNDTO)
39a844b566SFrançois Tigeot
40fb9cffefSMatthew Dillon int lbawidth;
41fb9cffefSMatthew Dillon
42fb9cffefSMatthew Dillon static map_t *mediamap;
43fb9cffefSMatthew Dillon
44*35a9ab8aSMatthew Dillon map_t *
mkmap(off_t start,off_t size,int type)45fb9cffefSMatthew Dillon mkmap(off_t start, off_t size, int type)
46fb9cffefSMatthew Dillon {
47fb9cffefSMatthew Dillon map_t *m;
48fb9cffefSMatthew Dillon
49fb9cffefSMatthew Dillon m = malloc(sizeof(*m));
50fb9cffefSMatthew Dillon if (m == NULL)
51fb9cffefSMatthew Dillon return (NULL);
52fb9cffefSMatthew Dillon m->map_start = start;
53fb9cffefSMatthew Dillon m->map_size = size;
54fb9cffefSMatthew Dillon m->map_next = m->map_prev = NULL;
55fb9cffefSMatthew Dillon m->map_type = type;
5671cfd094SMatthew Dillon m->map_index = NOENTRY;
57fb9cffefSMatthew Dillon m->map_data = NULL;
58fb9cffefSMatthew Dillon return (m);
59fb9cffefSMatthew Dillon }
60fb9cffefSMatthew Dillon
61fb9cffefSMatthew Dillon map_t *
map_add(off_t start,off_t size,int type,void * data)62fb9cffefSMatthew Dillon map_add(off_t start, off_t size, int type, void *data)
63fb9cffefSMatthew Dillon {
64fb9cffefSMatthew Dillon map_t *m, *n, *p;
65fb9cffefSMatthew Dillon
66fb9cffefSMatthew Dillon n = mediamap;
67fb9cffefSMatthew Dillon while (n != NULL && n->map_start + n->map_size <= start)
68fb9cffefSMatthew Dillon n = n->map_next;
69fb9cffefSMatthew Dillon if (n == NULL)
70fb9cffefSMatthew Dillon return (NULL);
71fb9cffefSMatthew Dillon
72fb9cffefSMatthew Dillon if (n->map_start + n->map_size < start + size) {
73fb9cffefSMatthew Dillon warnx("error: bogus map");
74fb9cffefSMatthew Dillon return (0);
75fb9cffefSMatthew Dillon }
76fb9cffefSMatthew Dillon
77fb9cffefSMatthew Dillon if (n->map_start == start && n->map_size == size) {
78fb9cffefSMatthew Dillon if (n->map_type != MAP_TYPE_UNUSED) {
79fb9cffefSMatthew Dillon if (n->map_type != MAP_TYPE_MBR_PART ||
80fb9cffefSMatthew Dillon type != MAP_TYPE_GPT_PART) {
81fb9cffefSMatthew Dillon warnx("warning: partition(%llu,%llu) mirrored",
82fb9cffefSMatthew Dillon (long long)start, (long long)size);
83fb9cffefSMatthew Dillon }
84fb9cffefSMatthew Dillon }
85fb9cffefSMatthew Dillon n->map_type = type;
86fb9cffefSMatthew Dillon n->map_data = data;
87fb9cffefSMatthew Dillon return (n);
88fb9cffefSMatthew Dillon }
89fb9cffefSMatthew Dillon
90fb9cffefSMatthew Dillon if (n->map_type != MAP_TYPE_UNUSED) {
91fb9cffefSMatthew Dillon if (n->map_type != MAP_TYPE_MBR_PART ||
92fb9cffefSMatthew Dillon type != MAP_TYPE_GPT_PART) {
93fb9cffefSMatthew Dillon warnx("error: bogus map");
94fb9cffefSMatthew Dillon return (0);
95fb9cffefSMatthew Dillon }
96fb9cffefSMatthew Dillon n->map_type = MAP_TYPE_UNUSED;
97fb9cffefSMatthew Dillon }
98fb9cffefSMatthew Dillon
99fb9cffefSMatthew Dillon m = mkmap(start, size, type);
100fb9cffefSMatthew Dillon if (m == NULL)
101fb9cffefSMatthew Dillon return (NULL);
102fb9cffefSMatthew Dillon
103fb9cffefSMatthew Dillon m->map_data = data;
104fb9cffefSMatthew Dillon
105fb9cffefSMatthew Dillon if (start == n->map_start) {
106fb9cffefSMatthew Dillon m->map_prev = n->map_prev;
107fb9cffefSMatthew Dillon m->map_next = n;
108fb9cffefSMatthew Dillon if (m->map_prev != NULL)
109fb9cffefSMatthew Dillon m->map_prev->map_next = m;
110fb9cffefSMatthew Dillon else
111fb9cffefSMatthew Dillon mediamap = m;
112fb9cffefSMatthew Dillon n->map_prev = m;
113fb9cffefSMatthew Dillon n->map_start += size;
114fb9cffefSMatthew Dillon n->map_size -= size;
115fb9cffefSMatthew Dillon } else if (start + size == n->map_start + n->map_size) {
116fb9cffefSMatthew Dillon p = n;
117fb9cffefSMatthew Dillon m->map_next = p->map_next;
118fb9cffefSMatthew Dillon m->map_prev = p;
119fb9cffefSMatthew Dillon if (m->map_next != NULL)
120fb9cffefSMatthew Dillon m->map_next->map_prev = m;
121fb9cffefSMatthew Dillon p->map_next = m;
122fb9cffefSMatthew Dillon p->map_size -= size;
123fb9cffefSMatthew Dillon } else {
124fb9cffefSMatthew Dillon p = mkmap(n->map_start, start - n->map_start, n->map_type);
125fb9cffefSMatthew Dillon n->map_start += p->map_size + m->map_size;
126fb9cffefSMatthew Dillon n->map_size -= (p->map_size + m->map_size);
127fb9cffefSMatthew Dillon p->map_prev = n->map_prev;
128fb9cffefSMatthew Dillon m->map_prev = p;
129fb9cffefSMatthew Dillon n->map_prev = m;
130fb9cffefSMatthew Dillon m->map_next = n;
131fb9cffefSMatthew Dillon p->map_next = m;
132fb9cffefSMatthew Dillon if (p->map_prev != NULL)
133fb9cffefSMatthew Dillon p->map_prev->map_next = p;
134fb9cffefSMatthew Dillon else
135fb9cffefSMatthew Dillon mediamap = p;
136fb9cffefSMatthew Dillon }
137fb9cffefSMatthew Dillon
138fb9cffefSMatthew Dillon return (m);
139fb9cffefSMatthew Dillon }
140fb9cffefSMatthew Dillon
141fb9cffefSMatthew Dillon map_t *
map_alloc(off_t start,off_t size)142fb9cffefSMatthew Dillon map_alloc(off_t start, off_t size)
143fb9cffefSMatthew Dillon {
144fb9cffefSMatthew Dillon off_t delta;
145fb9cffefSMatthew Dillon map_t *m;
146fb9cffefSMatthew Dillon
147a844b566SFrançois Tigeot if (start == 0 && size != 0)
148a844b566SFrançois Tigeot size = ROUNDUP(size);
149fb9cffefSMatthew Dillon for (m = mediamap; m != NULL; m = m->map_next) {
150fb9cffefSMatthew Dillon if (m->map_type != MAP_TYPE_UNUSED || m->map_start < 2)
151fb9cffefSMatthew Dillon continue;
152fb9cffefSMatthew Dillon if (start != 0 && m->map_start > start)
153fb9cffefSMatthew Dillon return (NULL);
154a844b566SFrançois Tigeot delta = (start != 0) ? start - m->map_start : ROUNDUP(m->map_start) - m->map_start;
155fb9cffefSMatthew Dillon if (size == 0 || m->map_size - delta >= size) {
156fb9cffefSMatthew Dillon if (m->map_size - delta <= 0)
157fb9cffefSMatthew Dillon continue;
158a844b566SFrançois Tigeot if (size == 0) {
159fb9cffefSMatthew Dillon size = m->map_size - delta;
160a844b566SFrançois Tigeot if (start == 0)
161a844b566SFrançois Tigeot size = ROUNDDOWN(size);
162a844b566SFrançois Tigeot }
163fb9cffefSMatthew Dillon return (map_add(m->map_start + delta, size,
164fb9cffefSMatthew Dillon MAP_TYPE_GPT_PART, NULL));
165fb9cffefSMatthew Dillon }
166fb9cffefSMatthew Dillon }
167fb9cffefSMatthew Dillon
168fb9cffefSMatthew Dillon return (NULL);
169fb9cffefSMatthew Dillon }
170fb9cffefSMatthew Dillon
171fb9cffefSMatthew Dillon map_t *
map_find(int type)172fb9cffefSMatthew Dillon map_find(int type)
173fb9cffefSMatthew Dillon {
174fb9cffefSMatthew Dillon map_t *m;
175fb9cffefSMatthew Dillon
176fb9cffefSMatthew Dillon m = mediamap;
177fb9cffefSMatthew Dillon while (m != NULL && m->map_type != type)
178fb9cffefSMatthew Dillon m = m->map_next;
179fb9cffefSMatthew Dillon return (m);
180fb9cffefSMatthew Dillon }
181fb9cffefSMatthew Dillon
182fb9cffefSMatthew Dillon map_t *
map_first(void)183fb9cffefSMatthew Dillon map_first(void)
184fb9cffefSMatthew Dillon {
185fb9cffefSMatthew Dillon return mediamap;
186fb9cffefSMatthew Dillon }
187fb9cffefSMatthew Dillon
188fb9cffefSMatthew Dillon map_t *
map_last(void)189fb9cffefSMatthew Dillon map_last(void)
190fb9cffefSMatthew Dillon {
191fb9cffefSMatthew Dillon map_t *m;
192fb9cffefSMatthew Dillon
193fb9cffefSMatthew Dillon m = mediamap;
194fb9cffefSMatthew Dillon while (m != NULL && m->map_next != NULL)
195fb9cffefSMatthew Dillon m = m->map_next;
196fb9cffefSMatthew Dillon return (m);
197fb9cffefSMatthew Dillon }
198fb9cffefSMatthew Dillon
199fb9cffefSMatthew Dillon off_t
map_free(off_t start,off_t size)200fb9cffefSMatthew Dillon map_free(off_t start, off_t size)
201fb9cffefSMatthew Dillon {
202fb9cffefSMatthew Dillon map_t *m;
203fb9cffefSMatthew Dillon
204fb9cffefSMatthew Dillon m = mediamap;
205fb9cffefSMatthew Dillon
206fb9cffefSMatthew Dillon while (m != NULL && m->map_start + m->map_size <= start)
207fb9cffefSMatthew Dillon m = m->map_next;
208fb9cffefSMatthew Dillon if (m == NULL || m->map_type != MAP_TYPE_UNUSED)
209fb9cffefSMatthew Dillon return (0LL);
210fb9cffefSMatthew Dillon if (size)
211fb9cffefSMatthew Dillon return ((m->map_start + m->map_size >= start + size) ? 1 : 0);
212fb9cffefSMatthew Dillon return (m->map_size - (start - m->map_start));
213fb9cffefSMatthew Dillon }
214fb9cffefSMatthew Dillon
215fb9cffefSMatthew Dillon void
map_init(off_t size)216fb9cffefSMatthew Dillon map_init(off_t size)
217fb9cffefSMatthew Dillon {
218fb9cffefSMatthew Dillon char buf[32];
219fb9cffefSMatthew Dillon
220fb9cffefSMatthew Dillon mediamap = mkmap(0LL, size, MAP_TYPE_UNUSED);
221fb9cffefSMatthew Dillon lbawidth = sprintf(buf, "%llu", (long long)size);
222fb9cffefSMatthew Dillon if (lbawidth < 5)
223fb9cffefSMatthew Dillon lbawidth = 5;
224fb9cffefSMatthew Dillon }
225