Home | History | Annotate | Line # | Download | only in gpt
map.c revision 1.7
      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 #if HAVE_NBTOOL_CONFIG_H
     28 #include "nbtool_config.h"
     29 #endif
     30 
     31 #include <sys/cdefs.h>
     32 #ifdef __FBSDID
     33 __FBSDID("$FreeBSD: src/sbin/gpt/map.c,v 1.6 2005/08/31 01:47:19 marcel Exp $");
     34 #endif
     35 #ifdef __RCSID
     36 __RCSID("$NetBSD: map.c,v 1.7 2014/09/29 20:28:57 christos Exp $");
     37 #endif
     38 
     39 #include <sys/types.h>
     40 #include <err.h>
     41 #include <stdio.h>
     42 #include <stdlib.h>
     43 
     44 #include "map.h"
     45 
     46 int lbawidth;
     47 
     48 static map_t *mediamap;
     49 
     50 static map_t *
     51 mkmap(off_t start, off_t size, int type)
     52 {
     53 	map_t *m;
     54 
     55 	m = malloc(sizeof(*m));
     56 	if (m == NULL)
     57 		return (NULL);
     58 	m->map_start = start;
     59 	m->map_size = size;
     60 	m->map_next = m->map_prev = NULL;
     61 	m->map_type = type;
     62 	m->map_index = 0;
     63 	m->map_data = NULL;
     64 	return (m);
     65 }
     66 
     67 map_t *
     68 map_add(off_t start, off_t size, int type, void *data)
     69 {
     70 	map_t *m, *n, *p;
     71 
     72 	n = mediamap;
     73 	while (n != NULL && n->map_start + n->map_size <= start)
     74 		n = n->map_next;
     75 	if (n == NULL)
     76 		return (NULL);
     77 
     78 	if (n->map_start + n->map_size < start + size) {
     79 		warnx("error: map entry doesn't fit media");
     80 		return (NULL);
     81 	}
     82 
     83 	if (n->map_start == start && n->map_size == size) {
     84 		if (n->map_type != MAP_TYPE_UNUSED) {
     85 			if (n->map_type != MAP_TYPE_MBR_PART ||
     86 			    type != MAP_TYPE_GPT_PART) {
     87 				warnx("warning: partition(%llu,%llu) mirrored",
     88 				    (long long)start, (long long)size);
     89 			}
     90 		}
     91 		n->map_type = type;
     92 		n->map_data = data;
     93 		return (n);
     94 	}
     95 
     96 	if (n->map_type != MAP_TYPE_UNUSED) {
     97 		if (n->map_type != MAP_TYPE_MBR_PART ||
     98 		    type != MAP_TYPE_GPT_PART) {
     99 			warnx("error: bogus map");
    100 			return (0);
    101 		}
    102 		n->map_type = MAP_TYPE_UNUSED;
    103 	}
    104 
    105 	m = mkmap(start, size, type);
    106 	if (m == NULL)
    107 		return (NULL);
    108 
    109 	m->map_data = data;
    110 
    111 	if (start == n->map_start) {
    112 		m->map_prev = n->map_prev;
    113 		m->map_next = n;
    114 		if (m->map_prev != NULL)
    115 			m->map_prev->map_next = m;
    116 		else
    117 			mediamap = m;
    118 		n->map_prev = m;
    119 		n->map_start += size;
    120 		n->map_size -= size;
    121 	} else if (start + size == n->map_start + n->map_size) {
    122 		p = n;
    123 		m->map_next = p->map_next;
    124 		m->map_prev = p;
    125 		if (m->map_next != NULL)
    126 			m->map_next->map_prev = m;
    127 		p->map_next = m;
    128 		p->map_size -= size;
    129 	} else {
    130 		p = mkmap(n->map_start, start - n->map_start, n->map_type);
    131 		n->map_start += p->map_size + m->map_size;
    132 		n->map_size -= (p->map_size + m->map_size);
    133 		p->map_prev = n->map_prev;
    134 		m->map_prev = p;
    135 		n->map_prev = m;
    136 		m->map_next = n;
    137 		p->map_next = m;
    138 		if (p->map_prev != NULL)
    139 			p->map_prev->map_next = p;
    140 		else
    141 			mediamap = p;
    142 	}
    143 
    144 	return (m);
    145 }
    146 
    147 map_t *
    148 map_alloc(off_t start, off_t size, off_t alignment)
    149 {
    150 	off_t delta;
    151 	map_t *m;
    152 
    153 	if (alignment > 0) {
    154 		if ((start % alignment) != 0)
    155 			start = (start + alignment) / alignment * alignment;
    156 		if ((size % alignment) != 0)
    157 			size = (size + alignment) / alignment * alignment;
    158 	}
    159 
    160 	for (m = mediamap; m != NULL; m = m->map_next) {
    161 		if (m->map_type != MAP_TYPE_UNUSED || m->map_start < 2)
    162 			continue;
    163 		if (start != 0 && m->map_start > start)
    164 			return (NULL);
    165 
    166 		if (start != 0)
    167 			delta = start - m->map_start;
    168 		else if (alignment > 0 && m->map_start % alignment != 0)
    169 			delta = (m->map_start + alignment) /
    170 			        alignment * alignment - m->map_start;
    171 		else
    172 			delta = 0;
    173 
    174                 if (size == 0 || m->map_size - delta >= size) {
    175 			if (m->map_size - delta < alignment)
    176 				continue;
    177 			if (size == 0) {
    178 				if (alignment > 0 &&
    179 				    (m->map_size - delta) % alignment != 0)
    180 					size = (m->map_size - delta) /
    181 					    alignment * alignment;
    182 				else
    183 					size = m->map_size - delta;
    184 			}
    185 			return map_add(m->map_start + delta, size,
    186 				    MAP_TYPE_GPT_PART, NULL);
    187 		}
    188 	}
    189 
    190 	return NULL;
    191 }
    192 
    193 off_t
    194 map_resize(map_t *m, off_t size, off_t alignment)
    195 {
    196 	map_t *n, *o;
    197 	off_t alignsize, prevsize;
    198 
    199 	n = m->map_next;
    200 
    201 	if (size < 0 || alignment < 0) {
    202 		warnx("negative size or alignment");
    203 		return 0;
    204 	}
    205 	if (size == 0 && alignment == 0) {
    206 		if (n == NULL || n->map_type != MAP_TYPE_UNUSED)
    207 			return 0;
    208 		else {
    209 			size = m->map_size + n->map_size;
    210 			m->map_size = size;
    211 			m->map_next = n->map_next;
    212 			if (n->map_next != NULL)
    213 				n->map_next->map_prev = m;
    214 			if (n->map_data != NULL)
    215 				free(n->map_data);
    216 			free(n);
    217 			return size;
    218 		}
    219 	}
    220 
    221 	if (size == 0 && alignment > 0) {
    222 		if (n == NULL || n->map_type != MAP_TYPE_UNUSED)
    223 			return 0;
    224 		else {
    225 			prevsize = m->map_size;
    226 			size = (m->map_size + n->map_size) /
    227 			       alignment * alignment;
    228 			if (size <= prevsize)
    229 				return 0;
    230 			m->map_size = size;
    231 			n->map_start += size - prevsize;
    232 			n->map_size -= size - prevsize;
    233 			if (n->map_size == 0) {
    234 				m->map_next = n->map_next;
    235 				if (n->map_next != NULL)
    236 					n->map_next->map_prev = m;
    237 				if (n->map_data != NULL)
    238 					free(n->map_data);
    239 				free(n);
    240 			}
    241 			return size;
    242 		}
    243 	}
    244 
    245 	alignsize = size;
    246 	if (alignment % size != 0)
    247 		alignsize = (size + alignment) / alignment * alignment;
    248 
    249 	if (alignsize < m->map_size) {		/* shrinking */
    250 		prevsize = m->map_size;
    251 		m->map_size = alignsize;
    252 		if (n == NULL || n->map_type != MAP_TYPE_UNUSED) {
    253 			o = mkmap(m->map_start + alignsize,
    254 				  prevsize - alignsize, MAP_TYPE_UNUSED);
    255 			m->map_next = o;
    256 			o->map_prev = m;
    257 			o->map_next = n;
    258 			if (n != NULL)
    259 				n->map_prev = o;
    260 			return alignsize;
    261 		} else {
    262 			n->map_start -= alignsize;
    263 			n->map_size += alignsize;
    264 			return alignsize;
    265 		}
    266 	} else if (alignsize > m->map_size) {		/* expanding */
    267 		if (n == NULL || n->map_type != MAP_TYPE_UNUSED ||
    268 		    n->map_size < alignsize - m->map_size) {
    269 			return 0;
    270 		}
    271 		n->map_size -= alignsize - m->map_size;
    272 		n->map_start += alignsize - m->map_size;
    273 		if (n->map_size == 0) {
    274 			m->map_next = n->map_next;
    275 			if (n->map_next != NULL)
    276 				n->map_next->map_prev = m;
    277 			if (n->map_data != NULL)
    278 				free(n->map_data);
    279 			free(n);
    280 		}
    281 		m->map_size = alignsize;
    282 		return alignsize;
    283 	} else						/* correct size */
    284 		return alignsize;
    285 }
    286 
    287 map_t *
    288 map_find(int type)
    289 {
    290 	map_t *m;
    291 
    292 	m = mediamap;
    293 	while (m != NULL && m->map_type != type)
    294 		m = m->map_next;
    295 	return (m);
    296 }
    297 
    298 map_t *
    299 map_first(void)
    300 {
    301 	return mediamap;
    302 }
    303 
    304 map_t *
    305 map_last(void)
    306 {
    307 	map_t *m;
    308 
    309 	m = mediamap;
    310 	while (m != NULL && m->map_next != NULL)
    311 		m = m->map_next;
    312 	return (m);
    313 }
    314 
    315 off_t
    316 map_free(off_t start, off_t size)
    317 {
    318 	map_t *m;
    319 
    320 	m = mediamap;
    321 
    322 	while (m != NULL && m->map_start + m->map_size <= start)
    323 		m = m->map_next;
    324 	if (m == NULL || m->map_type != MAP_TYPE_UNUSED)
    325 		return (0LL);
    326 	if (size)
    327 		return ((m->map_start + m->map_size >= start + size) ? 1 : 0);
    328 	return (m->map_size - (start - m->map_start));
    329 }
    330 
    331 void
    332 map_init(off_t size)
    333 {
    334 	char buf[32];
    335 
    336 	mediamap = mkmap(0LL, size, MAP_TYPE_UNUSED);
    337 	lbawidth = sprintf(buf, "%llu", (long long)size);
    338 	if (lbawidth < 5)
    339 		lbawidth = 5;
    340 }
    341