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