1995-04-28 23:57:04 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
2002-03-25 13:52:45 +00:00
|
|
|
* <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
|
1995-04-28 23:57:04 +00:00
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2001-09-30 21:16:57 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1995-04-28 23:57:04 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2001-04-01 10:44:52 +00:00
|
|
|
#include <err.h>
|
1995-04-28 23:57:04 +00:00
|
|
|
#include "libdisk.h"
|
|
|
|
|
2002-10-22 09:13:02 +00:00
|
|
|
static struct chunk *
|
2002-10-23 20:35:42 +00:00
|
|
|
New_Chunk(void)
|
2002-10-22 09:13:02 +00:00
|
|
|
{
|
|
|
|
struct chunk *c;
|
|
|
|
|
|
|
|
c = malloc(sizeof *c);
|
2002-10-23 20:35:42 +00:00
|
|
|
if (c != NULL)
|
|
|
|
memset(c, 0, sizeof *c);
|
2002-10-22 09:13:02 +00:00
|
|
|
return (c);
|
|
|
|
}
|
1995-04-28 23:57:04 +00:00
|
|
|
|
|
|
|
/* Is c2 completely inside c1 ? */
|
|
|
|
|
|
|
|
static int
|
2002-10-22 09:13:02 +00:00
|
|
|
Chunk_Inside(const struct chunk *c1, const struct chunk *c2)
|
1995-04-28 23:57:04 +00:00
|
|
|
{
|
|
|
|
/* if c1 ends before c2 do */
|
|
|
|
if (c1->end < c2->end)
|
|
|
|
return 0;
|
|
|
|
/* if c1 starts after c2 do */
|
|
|
|
if (c1->offset > c2->offset)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-10-22 09:13:02 +00:00
|
|
|
static struct chunk *
|
2002-11-15 13:24:29 +00:00
|
|
|
Find_Mother_Chunk(struct chunk *chunks, u_long offset, u_long end,
|
|
|
|
chunk_e type)
|
1995-04-28 23:57:04 +00:00
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
struct chunk *c1, *c2, ct;
|
2001-04-01 12:18:20 +00:00
|
|
|
|
1995-04-28 23:57:04 +00:00
|
|
|
ct.offset = offset;
|
|
|
|
ct.end = end;
|
|
|
|
switch (type) {
|
2002-11-15 13:24:29 +00:00
|
|
|
case whole:
|
|
|
|
if (Chunk_Inside(chunks, &ct))
|
|
|
|
return chunks;
|
|
|
|
case extended:
|
|
|
|
for (c1 = chunks->part; c1; c1 = c1->next) {
|
|
|
|
if (c1->type != type)
|
|
|
|
continue;
|
|
|
|
if (Chunk_Inside(c1, &ct))
|
|
|
|
return c1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case freebsd:
|
|
|
|
for (c1 = chunks->part; c1; c1 = c1->next) {
|
|
|
|
if (c1->type == type)
|
2001-04-01 12:18:20 +00:00
|
|
|
if (Chunk_Inside(c1, &ct))
|
1995-04-28 23:57:04 +00:00
|
|
|
return c1;
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c1->type != extended)
|
|
|
|
continue;
|
|
|
|
for (c2 = c1->part; c2; c2 = c2->next)
|
|
|
|
if (c2->type == type && Chunk_Inside(c2, &ct))
|
|
|
|
return c2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
warn("Unsupported mother type in Find_Mother_Chunk");
|
|
|
|
return 0;
|
1995-04-28 23:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Free_Chunk(struct chunk *c1)
|
|
|
|
{
|
2002-10-23 20:35:42 +00:00
|
|
|
if(c1 == NULL)
|
|
|
|
return;
|
1996-03-24 18:55:39 +00:00
|
|
|
if(c1->private_data && c1->private_free)
|
|
|
|
(*c1->private_free)(c1->private_data);
|
2002-10-23 20:35:42 +00:00
|
|
|
if(c1->part != NULL)
|
1995-04-28 23:57:04 +00:00
|
|
|
Free_Chunk(c1->part);
|
2002-10-23 20:35:42 +00:00
|
|
|
if(c1->next != NULL)
|
1995-04-28 23:57:04 +00:00
|
|
|
Free_Chunk(c1->next);
|
2002-10-23 20:35:42 +00:00
|
|
|
if (c1->name != NULL)
|
|
|
|
free(c1->name);
|
|
|
|
if (c1->sname != NULL)
|
|
|
|
free(c1->sname);
|
1995-04-28 23:57:04 +00:00
|
|
|
free(c1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct chunk *
|
2002-10-22 09:13:02 +00:00
|
|
|
Clone_Chunk(const struct chunk *c1)
|
1995-04-28 23:57:04 +00:00
|
|
|
{
|
|
|
|
struct chunk *c2;
|
2001-04-01 12:18:20 +00:00
|
|
|
|
1995-04-28 23:57:04 +00:00
|
|
|
if(!c1)
|
2002-10-23 20:35:42 +00:00
|
|
|
return NULL;
|
|
|
|
c2 = New_Chunk();
|
|
|
|
if (c2 == NULL)
|
|
|
|
return NULL;
|
1995-04-28 23:57:04 +00:00
|
|
|
*c2 = *c1;
|
1996-03-24 18:55:39 +00:00
|
|
|
if (c1->private_data && c1->private_clone)
|
|
|
|
c2->private_data = c2->private_clone(c2->private_data);
|
1995-04-28 23:57:04 +00:00
|
|
|
c2->name = strdup(c2->name);
|
2002-10-23 20:35:42 +00:00
|
|
|
if (c2->sname != NULL)
|
|
|
|
c2->sname = strdup(c2->sname);
|
1995-04-28 23:57:04 +00:00
|
|
|
c2->next = Clone_Chunk(c2->next);
|
|
|
|
c2->part = Clone_Chunk(c2->part);
|
|
|
|
return c2;
|
|
|
|
}
|
|
|
|
|
2002-10-29 12:13:36 +00:00
|
|
|
int
|
2000-03-29 15:10:28 +00:00
|
|
|
Insert_Chunk(struct chunk *c2, u_long offset, u_long size, const char *name,
|
2002-10-23 20:35:42 +00:00
|
|
|
chunk_e type, int subtype, u_long flags, const char *sname)
|
1995-04-28 23:57:04 +00:00
|
|
|
{
|
|
|
|
struct chunk *ct,*cs;
|
|
|
|
|
1995-05-06 03:28:32 +00:00
|
|
|
/* We will only insert into empty spaces */
|
|
|
|
if (c2->type != unused)
|
|
|
|
return __LINE__;
|
|
|
|
|
2002-10-23 20:35:42 +00:00
|
|
|
ct = New_Chunk();
|
|
|
|
if (ct == NULL)
|
|
|
|
return __LINE__;
|
1995-05-25 06:14:49 +00:00
|
|
|
ct->disk = c2->disk;
|
1995-04-28 23:57:04 +00:00
|
|
|
ct->offset = offset;
|
|
|
|
ct->size = size;
|
|
|
|
ct->end = offset + size - 1;
|
|
|
|
ct->type = type;
|
2002-10-23 20:35:42 +00:00
|
|
|
if (sname != NULL)
|
|
|
|
ct->sname = strdup(sname);
|
1995-04-28 23:57:04 +00:00
|
|
|
ct->name = strdup(name);
|
|
|
|
ct->subtype = subtype;
|
|
|
|
ct->flags = flags;
|
|
|
|
|
2001-04-01 12:18:20 +00:00
|
|
|
if (!Chunk_Inside(c2, ct)) {
|
1995-05-06 03:28:32 +00:00
|
|
|
Free_Chunk(ct);
|
|
|
|
return __LINE__;
|
|
|
|
}
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
if (type == freebsd || type == extended) {
|
2002-10-23 20:35:42 +00:00
|
|
|
cs = New_Chunk();
|
|
|
|
if (cs == NULL)
|
|
|
|
return __LINE__;
|
1995-05-25 06:14:49 +00:00
|
|
|
cs->disk = c2->disk;
|
1995-04-28 23:57:04 +00:00
|
|
|
cs->offset = offset;
|
|
|
|
cs->size = size;
|
|
|
|
cs->end = offset + size - 1;
|
|
|
|
cs->type = unused;
|
2002-10-23 20:35:42 +00:00
|
|
|
if (sname != NULL)
|
|
|
|
cs->sname = strdup(sname);
|
1995-04-28 23:57:04 +00:00
|
|
|
cs->name = strdup("-");
|
|
|
|
ct->part = cs;
|
|
|
|
}
|
|
|
|
|
1995-05-06 03:28:32 +00:00
|
|
|
/* Make a new chunk for any trailing unused space */
|
|
|
|
if (c2->end > ct->end) {
|
2002-10-23 20:35:42 +00:00
|
|
|
cs = New_Chunk();
|
|
|
|
if (cs == NULL)
|
|
|
|
return __LINE__;
|
1995-05-06 03:28:32 +00:00
|
|
|
*cs = *c2;
|
1995-05-25 06:14:49 +00:00
|
|
|
cs->disk = c2->disk;
|
1995-05-06 03:28:32 +00:00
|
|
|
cs->offset = ct->end + 1;
|
|
|
|
cs->size = c2->end - ct->end;
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c2->sname != NULL)
|
2000-03-29 15:10:28 +00:00
|
|
|
cs->sname = strdup(c2->sname);
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c2->name)
|
1995-05-06 03:28:32 +00:00
|
|
|
cs->name = strdup(c2->name);
|
|
|
|
c2->next = cs;
|
|
|
|
c2->size -= c2->end - ct->end;
|
|
|
|
c2->end = ct->end;
|
|
|
|
}
|
|
|
|
/* If no leading unused space just occupy the old chunk */
|
|
|
|
if (c2->offset == ct->offset) {
|
2000-03-29 15:10:28 +00:00
|
|
|
c2->sname = ct->sname;
|
1995-05-06 03:28:32 +00:00
|
|
|
c2->name = ct->name;
|
|
|
|
c2->type = ct->type;
|
|
|
|
c2->part = ct->part;
|
|
|
|
c2->subtype = ct->subtype;
|
|
|
|
c2->flags = ct->flags;
|
2002-10-23 20:35:42 +00:00
|
|
|
ct->sname = NULL;
|
|
|
|
ct->name = NULL;
|
1995-05-06 03:28:32 +00:00
|
|
|
ct->part = 0;
|
|
|
|
Free_Chunk(ct);
|
1995-04-28 23:57:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1995-05-06 03:28:32 +00:00
|
|
|
/* else insert new chunk and adjust old one */
|
|
|
|
c2->end = ct->offset - 1;
|
|
|
|
c2->size -= ct->size;
|
|
|
|
ct->next = c2->next;
|
|
|
|
c2->next = ct;
|
|
|
|
return 0;
|
1995-04-28 23:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-03-29 15:10:28 +00:00
|
|
|
Add_Chunk(struct disk *d, long offset, u_long size, const char *name,
|
2002-11-15 13:24:29 +00:00
|
|
|
chunk_e type, int subtype, u_long flags, const char *sname)
|
1995-04-28 23:57:04 +00:00
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
struct chunk *c1, *c2, ct;
|
1995-04-28 23:57:04 +00:00
|
|
|
u_long end = offset + size - 1;
|
|
|
|
ct.offset = offset;
|
|
|
|
ct.end = end;
|
|
|
|
ct.size = size;
|
|
|
|
|
|
|
|
if (type == whole) {
|
2002-10-23 20:35:42 +00:00
|
|
|
d->chunks = c1 = New_Chunk();
|
|
|
|
if (c1 == NULL)
|
|
|
|
return __LINE__;
|
|
|
|
c2 = c1->part = New_Chunk();
|
|
|
|
if (c2 == NULL)
|
|
|
|
return __LINE__;
|
1995-05-25 06:14:49 +00:00
|
|
|
c2->disk = c1->disk = d;
|
1995-04-28 23:57:04 +00:00
|
|
|
c2->offset = c1->offset = offset;
|
|
|
|
c2->size = c1->size = size;
|
|
|
|
c2->end = c1->end = end;
|
2000-03-29 15:10:28 +00:00
|
|
|
c1->sname = strdup(sname);
|
|
|
|
c2->sname = strdup("-");
|
1995-04-28 23:57:04 +00:00
|
|
|
c1->name = strdup(name);
|
1995-04-30 06:09:29 +00:00
|
|
|
c2->name = strdup("-");
|
1995-04-28 23:57:04 +00:00
|
|
|
c1->type = type;
|
|
|
|
c2->type = unused;
|
|
|
|
c1->flags = flags;
|
|
|
|
c1->subtype = subtype;
|
1995-05-30 08:29:07 +00:00
|
|
|
return 0;
|
1995-04-28 23:57:04 +00:00
|
|
|
}
|
2002-10-29 12:13:36 +00:00
|
|
|
|
1995-04-28 23:57:04 +00:00
|
|
|
c1 = 0;
|
2002-10-29 12:13:36 +00:00
|
|
|
/* PLATFORM POLICY BEGIN ------------------------------------- */
|
|
|
|
switch(platform) {
|
|
|
|
case p_i386:
|
|
|
|
switch (type) {
|
|
|
|
case fat:
|
|
|
|
case mbr:
|
|
|
|
case extended:
|
|
|
|
case freebsd:
|
|
|
|
c1 = Find_Mother_Chunk(d->chunks, offset, end, whole);
|
|
|
|
break;
|
|
|
|
case part:
|
|
|
|
c1 = Find_Mother_Chunk(d->chunks, offset, end, freebsd);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
break;
|
2002-11-02 12:05:16 +00:00
|
|
|
case p_ia64:
|
|
|
|
switch (type) {
|
|
|
|
case freebsd:
|
|
|
|
subtype = 0xa5;
|
|
|
|
/* FALL THROUGH */
|
|
|
|
case fat:
|
2002-11-13 05:31:32 +00:00
|
|
|
case efi:
|
2002-11-02 12:05:16 +00:00
|
|
|
case mbr:
|
|
|
|
c1 = Find_Mother_Chunk(d->chunks, offset, end, whole);
|
|
|
|
break;
|
|
|
|
case part:
|
|
|
|
c1 = Find_Mother_Chunk(d->chunks, offset, end,
|
|
|
|
freebsd);
|
|
|
|
if (!c1)
|
|
|
|
c1 = Find_Mother_Chunk(d->chunks, offset, end,
|
|
|
|
whole);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
break;
|
2002-10-29 12:13:36 +00:00
|
|
|
case p_pc98:
|
2002-10-31 05:51:25 +00:00
|
|
|
switch (type) {
|
|
|
|
case fat:
|
2002-11-07 14:54:53 +00:00
|
|
|
case pc98:
|
|
|
|
case freebsd:
|
2002-10-31 05:51:25 +00:00
|
|
|
c1 = Find_Mother_Chunk(d->chunks, offset, end, whole);
|
|
|
|
break;
|
|
|
|
case part:
|
|
|
|
c1 = Find_Mother_Chunk(d->chunks, offset, end, freebsd);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return(-1);
|
|
|
|
}
|
2002-10-29 12:13:36 +00:00
|
|
|
break;
|
|
|
|
case p_sparc64:
|
|
|
|
case p_alpha:
|
|
|
|
switch (type) {
|
|
|
|
case freebsd:
|
|
|
|
c1 = Find_Mother_Chunk(d->chunks, offset, end, whole);
|
|
|
|
break;
|
|
|
|
case part:
|
|
|
|
c1 = Find_Mother_Chunk(d->chunks, offset, end, freebsd);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/* PLATFORM POLICY END ---------------------------------------- */
|
|
|
|
|
1995-04-28 23:57:04 +00:00
|
|
|
if(!c1)
|
|
|
|
return __LINE__;
|
2001-04-01 12:18:20 +00:00
|
|
|
for(c2 = c1->part; c2; c2 = c2->next) {
|
1995-04-28 23:57:04 +00:00
|
|
|
if (c2->type != unused)
|
|
|
|
continue;
|
2002-10-29 12:13:36 +00:00
|
|
|
if(!Chunk_Inside(c2, &ct))
|
|
|
|
continue;
|
|
|
|
/* PLATFORM POLICY BEGIN ------------------------------------- */
|
|
|
|
if (platform == p_sparc64) {
|
|
|
|
offset = Prev_Cyl_Aligned(d, offset);
|
|
|
|
size = Next_Cyl_Aligned(d, size);
|
2002-10-31 05:51:25 +00:00
|
|
|
} else if (platform == p_i386 || platform == p_pc98) {
|
1995-05-30 08:29:07 +00:00
|
|
|
if (type != freebsd)
|
2002-10-29 12:13:36 +00:00
|
|
|
break;
|
1995-05-30 08:29:07 +00:00
|
|
|
if (!(flags & CHUNK_ALIGN))
|
2002-10-29 12:13:36 +00:00
|
|
|
break;
|
2002-11-15 13:24:29 +00:00
|
|
|
if (offset == d->chunks->offset &&
|
|
|
|
end == d->chunks->end)
|
2002-10-29 12:13:36 +00:00
|
|
|
break;
|
1995-05-05 07:07:45 +00:00
|
|
|
|
|
|
|
/* Round down to prev cylinder */
|
|
|
|
offset = Prev_Cyl_Aligned(d,offset);
|
|
|
|
/* Stay inside the parent */
|
|
|
|
if (offset < c2->offset)
|
|
|
|
offset = c2->offset;
|
|
|
|
/* Round up to next cylinder */
|
2001-04-01 12:18:20 +00:00
|
|
|
offset = Next_Cyl_Aligned(d, offset);
|
1995-05-05 07:07:45 +00:00
|
|
|
/* Keep one track clear in front of parent */
|
|
|
|
if (offset == c1->offset)
|
2001-04-01 12:18:20 +00:00
|
|
|
offset = Next_Track_Aligned(d, offset + 1);
|
1995-05-05 07:07:45 +00:00
|
|
|
/* Work on the (end+1) */
|
|
|
|
size += offset;
|
|
|
|
/* Round up to cylinder */
|
2001-04-01 12:18:20 +00:00
|
|
|
size = Next_Cyl_Aligned(d, size);
|
1995-05-05 07:07:45 +00:00
|
|
|
/* Stay inside parent */
|
|
|
|
if ((size-1) > c2->end)
|
2001-04-01 12:18:20 +00:00
|
|
|
size = c2->end + 1;
|
1995-05-05 07:07:45 +00:00
|
|
|
/* Round down to cylinder */
|
2001-04-01 12:18:20 +00:00
|
|
|
size = Prev_Cyl_Aligned(d, size);
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-05 07:07:45 +00:00
|
|
|
/* Convert back to size */
|
|
|
|
size -= offset;
|
|
|
|
}
|
2002-10-29 17:12:02 +00:00
|
|
|
break;
|
2002-10-29 12:13:36 +00:00
|
|
|
|
|
|
|
/* PLATFORM POLICY END ------------------------------------- */
|
1995-04-28 23:57:04 +00:00
|
|
|
}
|
2002-10-29 12:13:36 +00:00
|
|
|
if (c2 == NULL)
|
|
|
|
return (__LINE__);
|
2002-11-15 13:24:29 +00:00
|
|
|
return Insert_Chunk(c2, offset, size, name, type, subtype, flags,
|
|
|
|
sname);
|
1995-04-28 23:57:04 +00:00
|
|
|
}
|
|
|
|
|
1995-06-11 19:33:05 +00:00
|
|
|
char *
|
|
|
|
ShowChunkFlags(struct chunk *c)
|
|
|
|
{
|
|
|
|
static char ret[10];
|
2002-11-15 13:24:29 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (c->flags & CHUNK_ACTIVE)
|
|
|
|
ret[i++] = 'A';
|
|
|
|
if (c->flags & CHUNK_ALIGN)
|
|
|
|
ret[i++] = '=';
|
|
|
|
if (c->flags & CHUNK_IS_ROOT)
|
|
|
|
ret[i++] = 'R';
|
1995-06-11 19:33:05 +00:00
|
|
|
ret[i++] = '\0';
|
2002-11-15 13:24:29 +00:00
|
|
|
|
1995-06-11 19:33:05 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-10-23 19:52:32 +00:00
|
|
|
static void
|
1995-04-28 23:57:04 +00:00
|
|
|
Print_Chunk(struct chunk *c1,int offset)
|
|
|
|
{
|
|
|
|
int i;
|
2002-11-15 13:24:29 +00:00
|
|
|
|
|
|
|
if (!c1)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < offset - 2; i++)
|
|
|
|
putchar(' ');
|
|
|
|
for (; i < offset; i++)
|
|
|
|
putchar('-');
|
1995-05-06 03:28:32 +00:00
|
|
|
putchar('>');
|
2002-11-15 13:24:29 +00:00
|
|
|
for (; i < 10; i++)
|
|
|
|
putchar(' ');
|
2000-03-29 15:10:28 +00:00
|
|
|
printf("%p %8ld %8lu %8lu %-8s %-16s %-8s 0x%02x %s",
|
|
|
|
c1, c1->offset, c1->size, c1->end, c1->name, c1->sname,
|
2002-10-29 12:13:36 +00:00
|
|
|
chunk_name(c1->type), c1->subtype,
|
1995-06-11 19:33:05 +00:00
|
|
|
ShowChunkFlags(c1));
|
1995-05-06 03:28:32 +00:00
|
|
|
putchar('\n');
|
2001-04-01 12:18:20 +00:00
|
|
|
Print_Chunk(c1->part, offset + 2);
|
|
|
|
Print_Chunk(c1->next, offset);
|
1995-04-28 23:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Debug_Chunk(struct chunk *c1)
|
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
|
1995-04-28 23:57:04 +00:00
|
|
|
Print_Chunk(c1,2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1995-04-29 01:55:25 +00:00
|
|
|
Delete_Chunk(struct disk *d, struct chunk *c)
|
2002-01-07 07:47:25 +00:00
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
|
2002-01-07 07:47:25 +00:00
|
|
|
return(Delete_Chunk2(d, c, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Delete_Chunk2(struct disk *d, struct chunk *c, int rflags)
|
1995-04-28 23:57:04 +00:00
|
|
|
{
|
2002-12-02 21:42:29 +00:00
|
|
|
struct chunk *c1, *c2, *c3;
|
2002-10-23 20:02:33 +00:00
|
|
|
u_long offset = c->offset;
|
1995-04-28 23:57:04 +00:00
|
|
|
|
2002-12-02 21:42:29 +00:00
|
|
|
switch (c->type) {
|
|
|
|
case whole:
|
|
|
|
case unused:
|
1995-04-28 23:57:04 +00:00
|
|
|
return 1;
|
2002-12-02 21:42:29 +00:00
|
|
|
case extended:
|
2001-04-01 12:18:20 +00:00
|
|
|
c1 = Find_Mother_Chunk(d->chunks, c->offset, c->end, whole);
|
2002-12-02 21:42:29 +00:00
|
|
|
break;
|
|
|
|
case part:
|
2001-04-01 12:18:20 +00:00
|
|
|
c1 = Find_Mother_Chunk(d->chunks, c->offset, c->end, freebsd);
|
2002-12-02 21:42:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
c1 = Find_Mother_Chunk(d->chunks, c->offset, c->end, extended);
|
|
|
|
if (c1 == NULL)
|
|
|
|
c1 = Find_Mother_Chunk(d->chunks, c->offset, c->end,
|
|
|
|
whole);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c1 == NULL)
|
1995-04-28 23:57:04 +00:00
|
|
|
return 1;
|
2002-11-15 13:24:29 +00:00
|
|
|
for (c2 = c1->part; c2; c2 = c2->next) {
|
1995-04-29 01:55:25 +00:00
|
|
|
if (c2 == c) {
|
1995-04-28 23:57:04 +00:00
|
|
|
c2->type = unused;
|
|
|
|
c2->subtype = 0;
|
|
|
|
c2->flags = 0;
|
2002-10-23 20:35:42 +00:00
|
|
|
if (c2->sname != NULL)
|
|
|
|
free(c2->sname);
|
2000-03-29 15:10:28 +00:00
|
|
|
c2->sname = strdup("-");
|
1995-04-28 23:57:04 +00:00
|
|
|
free(c2->name);
|
|
|
|
c2->name = strdup("-");
|
|
|
|
Free_Chunk(c2->part);
|
|
|
|
c2->part =0;
|
|
|
|
goto scan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2002-11-15 13:24:29 +00:00
|
|
|
scan:
|
2001-12-09 23:40:02 +00:00
|
|
|
/*
|
|
|
|
* Collapse multiple unused elements together, and attempt
|
|
|
|
* to extend the previous chunk into the freed chunk.
|
2002-01-07 07:47:25 +00:00
|
|
|
*
|
|
|
|
* We only extend non-unused elements which are marked
|
|
|
|
* for newfs (we can't extend working filesystems), and
|
|
|
|
* only if we are called with DELCHUNK_RECOVER.
|
2001-12-09 23:40:02 +00:00
|
|
|
*/
|
2002-11-15 13:24:29 +00:00
|
|
|
for (c2 = c1->part; c2; c2 = c2->next) {
|
2001-12-09 23:40:02 +00:00
|
|
|
if (c2->type != unused) {
|
|
|
|
if (c2->offset + c2->size != offset ||
|
2002-01-07 07:47:25 +00:00
|
|
|
(rflags & DELCHUNK_RECOVER) == 0 ||
|
2001-12-09 23:40:02 +00:00
|
|
|
(c2->flags & CHUNK_NEWFS) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* else extend into free area */
|
|
|
|
}
|
1995-04-28 23:57:04 +00:00
|
|
|
if (!c2->next)
|
|
|
|
continue;
|
|
|
|
if (c2->next->type != unused)
|
|
|
|
continue;
|
|
|
|
c3 = c2->next;
|
|
|
|
c2->size += c3->size;
|
|
|
|
c2->end = c3->end;
|
|
|
|
c2->next = c3->next;
|
|
|
|
c3->next = 0;
|
|
|
|
Free_Chunk(c3);
|
|
|
|
goto scan;
|
|
|
|
}
|
1995-04-30 06:09:29 +00:00
|
|
|
Fixup_Names(d);
|
1995-04-28 23:57:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-06-11 19:33:05 +00:00
|
|
|
#if 0
|
1995-04-28 23:57:04 +00:00
|
|
|
int
|
|
|
|
Collapse_Chunk(struct disk *d, struct chunk *c1)
|
|
|
|
{
|
|
|
|
struct chunk *c2, *c3;
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c1->next && Collapse_Chunk(d, c1->next))
|
1995-04-28 23:57:04 +00:00
|
|
|
return 1;
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c1->type == unused && c1->next && c1->next->type == unused) {
|
1995-04-28 23:57:04 +00:00
|
|
|
c3 = c1->next;
|
|
|
|
c1->size += c3->size;
|
|
|
|
c1->end = c3->end;
|
|
|
|
c1->next = c3->next;
|
|
|
|
c3->next = 0;
|
|
|
|
Free_Chunk(c3);
|
|
|
|
return 1;
|
1995-05-30 08:29:07 +00:00
|
|
|
}
|
1995-04-28 23:57:04 +00:00
|
|
|
c3 = c1->part;
|
2002-11-15 13:24:29 +00:00
|
|
|
if (!c3)
|
1995-04-28 23:57:04 +00:00
|
|
|
return 0;
|
2001-04-01 12:18:20 +00:00
|
|
|
if (Collapse_Chunk(d, c1->part))
|
1995-04-28 23:57:04 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (c1->type == whole)
|
|
|
|
return 0;
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c3->type == unused && c3->size == c1->size) {
|
2001-04-01 12:18:20 +00:00
|
|
|
Delete_Chunk(d, c1);
|
1995-04-28 23:57:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c3->type == unused) {
|
2002-10-23 20:35:42 +00:00
|
|
|
c2 = New_Chunk();
|
|
|
|
if (c2 == NULL)
|
|
|
|
barfout(1, "malloc failed");
|
1995-04-28 23:57:04 +00:00
|
|
|
*c2 = *c1;
|
|
|
|
c1->next = c2;
|
1995-05-25 06:14:49 +00:00
|
|
|
c1->disk = d;
|
2000-03-29 15:10:28 +00:00
|
|
|
c1->sname = strdup("-");
|
1995-04-28 23:57:04 +00:00
|
|
|
c1->name = strdup("-");
|
|
|
|
c1->part = 0;
|
|
|
|
c1->type = unused;
|
|
|
|
c1->flags = 0;
|
|
|
|
c1->subtype = 0;
|
|
|
|
c1->size = c3->size;
|
|
|
|
c1->end = c3->end;
|
|
|
|
c2->offset += c1->size;
|
|
|
|
c2->size -= c1->size;
|
|
|
|
c2->part = c3->next;
|
|
|
|
c3->next = 0;
|
|
|
|
Free_Chunk(c3);
|
|
|
|
return 1;
|
|
|
|
}
|
2002-11-15 13:24:29 +00:00
|
|
|
for (c2 = c3; c2->next; c2 = c2->next)
|
1995-04-28 23:57:04 +00:00
|
|
|
c3 = c2;
|
|
|
|
if (c2 && c2->type == unused) {
|
|
|
|
c3->next = 0;
|
|
|
|
c2->next = c1->next;
|
|
|
|
c1->next = c2;
|
|
|
|
c1->size -= c2->size;
|
|
|
|
c1->end -= c2->size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
1995-06-11 19:33:05 +00:00
|
|
|
#endif
|