Home | History | Annotate | Line # | Download | only in gpt
map.c revision 1.2
      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.2  christos __RCSID("$NetBSD: map.c,v 1.2 2006/10/15 22:36:29 christos 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.1  christos 		warnx("error: bogus map");
     76  1.1  christos 		return (0);
     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.1  christos map_alloc(off_t start, off_t size)
    145  1.1  christos {
    146  1.1  christos 	off_t delta;
    147  1.1  christos 	map_t *m;
    148  1.1  christos 
    149  1.1  christos 	for (m = mediamap; m != NULL; m = m->map_next) {
    150  1.1  christos 		if (m->map_type != MAP_TYPE_UNUSED || m->map_start < 2)
    151  1.1  christos 			continue;
    152  1.1  christos 		if (start != 0 && m->map_start > start)
    153  1.1  christos 			return (NULL);
    154  1.1  christos 		delta = (start != 0) ? start - m->map_start : 0;
    155  1.1  christos 		if (size == 0 || m->map_size - delta >= size) {
    156  1.1  christos 			if (m->map_size - delta <= 0)
    157  1.1  christos 				continue;
    158  1.1  christos 			if (size == 0)
    159  1.1  christos 				size = m->map_size - delta;
    160  1.1  christos 			return (map_add(m->map_start + delta, size,
    161  1.1  christos 				    MAP_TYPE_GPT_PART, NULL));
    162  1.1  christos 		}
    163  1.1  christos 	}
    164  1.1  christos 
    165  1.1  christos 	return (NULL);
    166  1.1  christos }
    167  1.1  christos 
    168  1.1  christos map_t *
    169  1.1  christos map_find(int type)
    170  1.1  christos {
    171  1.1  christos 	map_t *m;
    172  1.1  christos 
    173  1.1  christos 	m = mediamap;
    174  1.1  christos 	while (m != NULL && m->map_type != type)
    175  1.1  christos 		m = m->map_next;
    176  1.1  christos 	return (m);
    177  1.1  christos }
    178  1.1  christos 
    179  1.1  christos map_t *
    180  1.1  christos map_first(void)
    181  1.1  christos {
    182  1.1  christos 	return mediamap;
    183  1.1  christos }
    184  1.1  christos 
    185  1.1  christos map_t *
    186  1.1  christos map_last(void)
    187  1.1  christos {
    188  1.1  christos 	map_t *m;
    189  1.1  christos 
    190  1.1  christos 	m = mediamap;
    191  1.1  christos 	while (m != NULL && m->map_next != NULL)
    192  1.1  christos 		m = m->map_next;
    193  1.1  christos 	return (m);
    194  1.1  christos }
    195  1.1  christos 
    196  1.1  christos off_t
    197  1.1  christos map_free(off_t start, off_t size)
    198  1.1  christos {
    199  1.1  christos 	map_t *m;
    200  1.1  christos 
    201  1.1  christos 	m = mediamap;
    202  1.1  christos 
    203  1.1  christos 	while (m != NULL && m->map_start + m->map_size <= start)
    204  1.1  christos 		m = m->map_next;
    205  1.1  christos 	if (m == NULL || m->map_type != MAP_TYPE_UNUSED)
    206  1.1  christos 		return (0LL);
    207  1.1  christos 	if (size)
    208  1.1  christos 		return ((m->map_start + m->map_size >= start + size) ? 1 : 0);
    209  1.1  christos 	return (m->map_size - (start - m->map_start));
    210  1.1  christos }
    211  1.1  christos 
    212  1.1  christos void
    213  1.1  christos map_init(off_t size)
    214  1.1  christos {
    215  1.1  christos 	char buf[32];
    216  1.1  christos 
    217  1.1  christos 	mediamap = mkmap(0LL, size, MAP_TYPE_UNUSED);
    218  1.1  christos 	lbawidth = sprintf(buf, "%llu", (long long)size);
    219  1.1  christos 	if (lbawidth < 5)
    220  1.1  christos 		lbawidth = 5;
    221  1.1  christos }
    222