Home | History | Annotate | Line # | Download | only in gpt
map.c revision 1.5
      1 /*-
      2  * Copyright (c) 2002 Marcel Moolenaar
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 #ifdef __FBSDID
     29 __FBSDID("$FreeBSD: src/sbin/gpt/map.c,v 1.6 2005/08/31 01:47:19 marcel Exp $");
     30 #endif
     31 #ifdef __RCSID
     32 __RCSID("$NetBSD: map.c,v 1.5 2013/11/20 08:08:47 jnemeth Exp $");
     33 #endif
     34 
     35 #include <sys/types.h>
     36 #include <err.h>
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 
     40 #include "map.h"
     41 
     42 int lbawidth;
     43 
     44 static map_t *mediamap;
     45 
     46 static map_t *
     47 mkmap(off_t start, off_t size, int type)
     48 {
     49 	map_t *m;
     50 
     51 	m = malloc(sizeof(*m));
     52 	if (m == NULL)
     53 		return (NULL);
     54 	m->map_start = start;
     55 	m->map_size = size;
     56 	m->map_next = m->map_prev = NULL;
     57 	m->map_type = type;
     58 	m->map_index = 0;
     59 	m->map_data = NULL;
     60 	return (m);
     61 }
     62 
     63 map_t *
     64 map_add(off_t start, off_t size, int type, void *data)
     65 {
     66 	map_t *m, *n, *p;
     67 
     68 	n = mediamap;
     69 	while (n != NULL && n->map_start + n->map_size <= start)
     70 		n = n->map_next;
     71 	if (n == NULL)
     72 		return (NULL);
     73 
     74 	if (n->map_start + n->map_size < start + size) {
     75 		warnx("error: map entry doesn't fit media");
     76 		return (NULL);
     77 	}
     78 
     79 	if (n->map_start == start && n->map_size == size) {
     80 		if (n->map_type != MAP_TYPE_UNUSED) {
     81 			if (n->map_type != MAP_TYPE_MBR_PART ||
     82 			    type != MAP_TYPE_GPT_PART) {
     83 				warnx("warning: partition(%llu,%llu) mirrored",
     84 				    (long long)start, (long long)size);
     85 			}
     86 		}
     87 		n->map_type = type;
     88 		n->map_data = data;
     89 		return (n);
     90 	}
     91 
     92 	if (n->map_type != MAP_TYPE_UNUSED) {
     93 		if (n->map_type != MAP_TYPE_MBR_PART ||
     94 		    type != MAP_TYPE_GPT_PART) {
     95 			warnx("error: bogus map");
     96 			return (0);
     97 		}
     98 		n->map_type = MAP_TYPE_UNUSED;
     99 	}
    100 
    101 	m = mkmap(start, size, type);
    102 	if (m == NULL)
    103 		return (NULL);
    104 
    105 	m->map_data = data;
    106 
    107 	if (start == n->map_start) {
    108 		m->map_prev = n->map_prev;
    109 		m->map_next = n;
    110 		if (m->map_prev != NULL)
    111 			m->map_prev->map_next = m;
    112 		else
    113 			mediamap = m;
    114 		n->map_prev = m;
    115 		n->map_start += size;
    116 		n->map_size -= size;
    117 	} else if (start + size == n->map_start + n->map_size) {
    118 		p = n;
    119 		m->map_next = p->map_next;
    120 		m->map_prev = p;
    121 		if (m->map_next != NULL)
    122 			m->map_next->map_prev = m;
    123 		p->map_next = m;
    124 		p->map_size -= size;
    125 	} else {
    126 		p = mkmap(n->map_start, start - n->map_start, n->map_type);
    127 		n->map_start += p->map_size + m->map_size;
    128 		n->map_size -= (p->map_size + m->map_size);
    129 		p->map_prev = n->map_prev;
    130 		m->map_prev = p;
    131 		n->map_prev = m;
    132 		m->map_next = n;
    133 		p->map_next = m;
    134 		if (p->map_prev != NULL)
    135 			p->map_prev->map_next = p;
    136 		else
    137 			mediamap = p;
    138 	}
    139 
    140 	return (m);
    141 }
    142 
    143 map_t *
    144 map_alloc(off_t start, off_t size, off_t alignment)
    145 {
    146 	off_t delta;
    147 	map_t *m;
    148 
    149 	if (alignment > 0) {
    150 		if ((start % alignment) != 0)
    151 			start = (start + alignment) / alignment * alignment;
    152 		if ((size % alignment) != 0)
    153 			size = (size + alignment) / alignment * alignment;
    154 	}
    155 
    156 	for (m = mediamap; m != NULL; m = m->map_next) {
    157 		if (m->map_type != MAP_TYPE_UNUSED || m->map_start < 2)
    158 			continue;
    159 		if (start != 0 && m->map_start > start)
    160 			return (NULL);
    161 
    162 		if (start != 0)
    163 			delta = start - m->map_start;
    164 		else if (alignment > 0 && m->map_start % alignment != 0)
    165 			delta = (m->map_start + alignment) /
    166 			        alignment * alignment - m->map_start;
    167 		else
    168 			delta = 0;
    169 
    170                 if (size == 0 || m->map_size - delta >= size) {
    171 			if (m->map_size - delta < alignment)
    172 				continue;
    173 			if (size == 0) {
    174 				if (alignment > 0 &&
    175 				    (m->map_size - delta) % alignment != 0)
    176 					size = (m->map_size - delta) /
    177 					    alignment * alignment;
    178 				else
    179 					size = m->map_size - delta;
    180 			}
    181 			return map_add(m->map_start + delta, size,
    182 				    MAP_TYPE_GPT_PART, NULL);
    183 		}
    184 	}
    185 
    186 	return NULL;
    187 }
    188 
    189 off_t
    190 map_resize(map_t *m, off_t size, off_t alignment)
    191 {
    192 	map_t *n, *o;
    193 	off_t alignsize, prevsize;
    194 
    195 	n = m->map_next;
    196 
    197 	if (size == 0 && alignment == 0) {
    198 		if (n == NULL || n->map_type != MAP_TYPE_UNUSED)
    199 			return 0;
    200 		else {
    201 			size = m->map_size + n->map_size;
    202 			m->map_size = size;
    203 			m->map_next = n->map_next;
    204 			if (n->map_next != NULL)
    205 				n->map_next->map_prev = m;
    206 			if (n->map_data != NULL)
    207 				free(n->map_data);
    208 			free(n);
    209 			return size;
    210 		}
    211 	}
    212 
    213 	if (size == 0 && alignment > 0) {
    214 		if (n == NULL || n->map_type != MAP_TYPE_UNUSED)
    215 			return 0;
    216 		else {
    217 			prevsize = m->map_size;
    218 			size = (m->map_size + n->map_size) /
    219 			       alignment * alignment;
    220 			if (size <= prevsize)
    221 				return 0;
    222 			m->map_size = size;
    223 			n->map_start += size - prevsize;
    224 			n->map_size -= size - prevsize;
    225 			if (n->map_size == 0) {
    226 				m->map_next = n->map_next;
    227 				if (n->map_next != NULL)
    228 					n->map_next->map_prev = m;
    229 				if (n->map_data != NULL)
    230 					free(n->map_data);
    231 				free(n);
    232 			}
    233 			return size;
    234 		}
    235 	}
    236 
    237 	alignsize = size;
    238 	if (alignment % size != 0)
    239 		alignsize = (size + alignment) / alignment * alignment;
    240 
    241 	if (alignsize < m->map_size) {		/* shrinking */
    242 		prevsize = m->map_size;
    243 		m->map_size = alignsize;
    244 		if (n == NULL || n->map_type != MAP_TYPE_UNUSED) {
    245 			o = mkmap(m->map_start + alignsize,
    246 				  prevsize - alignsize, MAP_TYPE_UNUSED);
    247 			m->map_next = o;
    248 			o->map_prev = m;
    249 			o->map_next = n;
    250 			if (n != NULL)
    251 				n->map_prev = o;
    252 			return alignsize;
    253 		} else {
    254 			n->map_start -= alignsize;
    255 			n->map_size += alignsize;
    256 			return alignsize;
    257 		}
    258 	} else if (alignsize > m->map_size) {		/* expanding */
    259 		if (n == NULL || n->map_type != MAP_TYPE_UNUSED ||
    260 		    n->map_size < alignsize - m->map_size) {
    261 			return 0;
    262 		}
    263 		n->map_size -= alignsize - m->map_size;
    264 		n->map_start += alignsize - m->map_size;
    265 		if (n->map_size == 0) {
    266 			m->map_next = n->map_next;
    267 			if (n->map_next != NULL)
    268 				n->map_next->map_prev = m;
    269 			if (n->map_data != NULL)
    270 				free(n->map_data);
    271 			free(n);
    272 		}
    273 		m->map_size = alignsize;
    274 		return alignsize;
    275 	} else						/* correct size */
    276 		return alignsize;
    277 }
    278 
    279 map_t *
    280 map_find(int type)
    281 {
    282 	map_t *m;
    283 
    284 	m = mediamap;
    285 	while (m != NULL && m->map_type != type)
    286 		m = m->map_next;
    287 	return (m);
    288 }
    289 
    290 map_t *
    291 map_first(void)
    292 {
    293 	return mediamap;
    294 }
    295 
    296 map_t *
    297 map_last(void)
    298 {
    299 	map_t *m;
    300 
    301 	m = mediamap;
    302 	while (m != NULL && m->map_next != NULL)
    303 		m = m->map_next;
    304 	return (m);
    305 }
    306 
    307 off_t
    308 map_free(off_t start, off_t size)
    309 {
    310 	map_t *m;
    311 
    312 	m = mediamap;
    313 
    314 	while (m != NULL && m->map_start + m->map_size <= start)
    315 		m = m->map_next;
    316 	if (m == NULL || m->map_type != MAP_TYPE_UNUSED)
    317 		return (0LL);
    318 	if (size)
    319 		return ((m->map_start + m->map_size >= start + size) ? 1 : 0);
    320 	return (m->map_size - (start - m->map_start));
    321 }
    322 
    323 void
    324 map_init(off_t size)
    325 {
    326 	char buf[32];
    327 
    328 	mediamap = mkmap(0LL, size, MAP_TYPE_UNUSED);
    329 	lbawidth = sprintf(buf, "%llu", (long long)size);
    330 	if (lbawidth < 5)
    331 		lbawidth = 5;
    332 }
    333