map.c revision 1.4 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.2 christos #ifdef __FBSDID
29 1.1 christos __FBSDID("$FreeBSD: src/sbin/gpt/map.c,v 1.6 2005/08/31 01:47:19 marcel Exp $");
30 1.2 christos #endif
31 1.2 christos #ifdef __RCSID
32 1.4 jnemeth __RCSID("$NetBSD: map.c,v 1.4 2013/11/19 05:03:41 jnemeth Exp $");
33 1.2 christos #endif
34 1.1 christos
35 1.1 christos #include <sys/types.h>
36 1.1 christos #include <err.h>
37 1.1 christos #include <stdio.h>
38 1.1 christos #include <stdlib.h>
39 1.1 christos
40 1.1 christos #include "map.h"
41 1.1 christos
42 1.1 christos int lbawidth;
43 1.1 christos
44 1.1 christos static map_t *mediamap;
45 1.1 christos
46 1.1 christos static map_t *
47 1.1 christos mkmap(off_t start, off_t size, int type)
48 1.1 christos {
49 1.1 christos map_t *m;
50 1.1 christos
51 1.1 christos m = malloc(sizeof(*m));
52 1.1 christos if (m == NULL)
53 1.1 christos return (NULL);
54 1.1 christos m->map_start = start;
55 1.1 christos m->map_size = size;
56 1.1 christos m->map_next = m->map_prev = NULL;
57 1.1 christos m->map_type = type;
58 1.1 christos m->map_index = 0;
59 1.1 christos m->map_data = NULL;
60 1.1 christos return (m);
61 1.1 christos }
62 1.1 christos
63 1.1 christos map_t *
64 1.1 christos map_add(off_t start, off_t size, int type, void *data)
65 1.1 christos {
66 1.1 christos map_t *m, *n, *p;
67 1.1 christos
68 1.1 christos n = mediamap;
69 1.1 christos while (n != NULL && n->map_start + n->map_size <= start)
70 1.1 christos n = n->map_next;
71 1.1 christos if (n == NULL)
72 1.1 christos return (NULL);
73 1.1 christos
74 1.1 christos if (n->map_start + n->map_size < start + size) {
75 1.3 jnemeth warnx("error: map entry doesn't fit media");
76 1.3 jnemeth return (NULL);
77 1.1 christos }
78 1.1 christos
79 1.1 christos if (n->map_start == start && n->map_size == size) {
80 1.1 christos if (n->map_type != MAP_TYPE_UNUSED) {
81 1.1 christos if (n->map_type != MAP_TYPE_MBR_PART ||
82 1.1 christos type != MAP_TYPE_GPT_PART) {
83 1.1 christos warnx("warning: partition(%llu,%llu) mirrored",
84 1.1 christos (long long)start, (long long)size);
85 1.1 christos }
86 1.1 christos }
87 1.1 christos n->map_type = type;
88 1.1 christos n->map_data = data;
89 1.1 christos return (n);
90 1.1 christos }
91 1.1 christos
92 1.1 christos if (n->map_type != MAP_TYPE_UNUSED) {
93 1.1 christos if (n->map_type != MAP_TYPE_MBR_PART ||
94 1.1 christos type != MAP_TYPE_GPT_PART) {
95 1.1 christos warnx("error: bogus map");
96 1.1 christos return (0);
97 1.1 christos }
98 1.1 christos n->map_type = MAP_TYPE_UNUSED;
99 1.1 christos }
100 1.1 christos
101 1.1 christos m = mkmap(start, size, type);
102 1.1 christos if (m == NULL)
103 1.1 christos return (NULL);
104 1.1 christos
105 1.1 christos m->map_data = data;
106 1.1 christos
107 1.1 christos if (start == n->map_start) {
108 1.1 christos m->map_prev = n->map_prev;
109 1.1 christos m->map_next = n;
110 1.1 christos if (m->map_prev != NULL)
111 1.1 christos m->map_prev->map_next = m;
112 1.1 christos else
113 1.1 christos mediamap = m;
114 1.1 christos n->map_prev = m;
115 1.1 christos n->map_start += size;
116 1.1 christos n->map_size -= size;
117 1.1 christos } else if (start + size == n->map_start + n->map_size) {
118 1.1 christos p = n;
119 1.1 christos m->map_next = p->map_next;
120 1.1 christos m->map_prev = p;
121 1.1 christos if (m->map_next != NULL)
122 1.1 christos m->map_next->map_prev = m;
123 1.1 christos p->map_next = m;
124 1.1 christos p->map_size -= size;
125 1.1 christos } else {
126 1.1 christos p = mkmap(n->map_start, start - n->map_start, n->map_type);
127 1.1 christos n->map_start += p->map_size + m->map_size;
128 1.1 christos n->map_size -= (p->map_size + m->map_size);
129 1.1 christos p->map_prev = n->map_prev;
130 1.1 christos m->map_prev = p;
131 1.1 christos n->map_prev = m;
132 1.1 christos m->map_next = n;
133 1.1 christos p->map_next = m;
134 1.1 christos if (p->map_prev != NULL)
135 1.1 christos p->map_prev->map_next = p;
136 1.1 christos else
137 1.1 christos mediamap = p;
138 1.1 christos }
139 1.1 christos
140 1.1 christos return (m);
141 1.1 christos }
142 1.1 christos
143 1.1 christos map_t *
144 1.4 jnemeth map_alloc(off_t start, off_t size, off_t alignment)
145 1.1 christos {
146 1.1 christos off_t delta;
147 1.1 christos map_t *m;
148 1.1 christos
149 1.4 jnemeth if (alignment > 0) {
150 1.4 jnemeth if ((start % alignment) != 0)
151 1.4 jnemeth start = (start + alignment) / alignment * alignment;
152 1.4 jnemeth if ((size % alignment) != 0)
153 1.4 jnemeth size = (size + alignment) / alignment * alignment;
154 1.4 jnemeth }
155 1.4 jnemeth
156 1.1 christos for (m = mediamap; m != NULL; m = m->map_next) {
157 1.1 christos if (m->map_type != MAP_TYPE_UNUSED || m->map_start < 2)
158 1.1 christos continue;
159 1.1 christos if (start != 0 && m->map_start > start)
160 1.1 christos return (NULL);
161 1.4 jnemeth
162 1.4 jnemeth if (start != 0)
163 1.4 jnemeth delta = start - m->map_start;
164 1.4 jnemeth else if (alignment > 0 && m->map_start % alignment != 0)
165 1.4 jnemeth delta = (m->map_start + alignment) /
166 1.4 jnemeth alignment * alignment - m->map_start;
167 1.4 jnemeth else
168 1.4 jnemeth delta = 0;
169 1.4 jnemeth
170 1.4 jnemeth if (size == 0 || m->map_size - delta >= size) {
171 1.4 jnemeth if (m->map_size - delta < alignment)
172 1.1 christos continue;
173 1.4 jnemeth if (size == 0) {
174 1.4 jnemeth if (alignment > 0 &&
175 1.4 jnemeth (m->map_size - delta) % alignment != 0)
176 1.4 jnemeth size = (m->map_size - delta) /
177 1.4 jnemeth alignment * alignment;
178 1.4 jnemeth else
179 1.4 jnemeth size = m->map_size - delta;
180 1.4 jnemeth }
181 1.4 jnemeth return map_add(m->map_start + delta, size,
182 1.4 jnemeth MAP_TYPE_GPT_PART, NULL);
183 1.1 christos }
184 1.1 christos }
185 1.1 christos
186 1.4 jnemeth return NULL;
187 1.1 christos }
188 1.1 christos
189 1.1 christos map_t *
190 1.1 christos map_find(int type)
191 1.1 christos {
192 1.1 christos map_t *m;
193 1.1 christos
194 1.1 christos m = mediamap;
195 1.1 christos while (m != NULL && m->map_type != type)
196 1.1 christos m = m->map_next;
197 1.1 christos return (m);
198 1.1 christos }
199 1.1 christos
200 1.1 christos map_t *
201 1.1 christos map_first(void)
202 1.1 christos {
203 1.1 christos return mediamap;
204 1.1 christos }
205 1.1 christos
206 1.1 christos map_t *
207 1.1 christos map_last(void)
208 1.1 christos {
209 1.1 christos map_t *m;
210 1.1 christos
211 1.1 christos m = mediamap;
212 1.1 christos while (m != NULL && m->map_next != NULL)
213 1.1 christos m = m->map_next;
214 1.1 christos return (m);
215 1.1 christos }
216 1.1 christos
217 1.1 christos off_t
218 1.1 christos map_free(off_t start, off_t size)
219 1.1 christos {
220 1.1 christos map_t *m;
221 1.1 christos
222 1.1 christos m = mediamap;
223 1.1 christos
224 1.1 christos while (m != NULL && m->map_start + m->map_size <= start)
225 1.1 christos m = m->map_next;
226 1.1 christos if (m == NULL || m->map_type != MAP_TYPE_UNUSED)
227 1.1 christos return (0LL);
228 1.1 christos if (size)
229 1.1 christos return ((m->map_start + m->map_size >= start + size) ? 1 : 0);
230 1.1 christos return (m->map_size - (start - m->map_start));
231 1.1 christos }
232 1.1 christos
233 1.1 christos void
234 1.1 christos map_init(off_t size)
235 1.1 christos {
236 1.1 christos char buf[32];
237 1.1 christos
238 1.1 christos mediamap = mkmap(0LL, size, MAP_TYPE_UNUSED);
239 1.1 christos lbawidth = sprintf(buf, "%llu", (long long)size);
240 1.1 christos if (lbawidth < 5)
241 1.1 christos lbawidth = 5;
242 1.1 christos }
243