1 /*
2  * Copyright (c)2004 The DragonFly Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *   Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
11  *   Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in
13  *   the documentation and/or other materials provided with the
14  *   distribution.
15  *
16  *   Neither the name of the DragonFly Project nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * diskutil.h
36  * $Id: diskutil.h,v 1.14 2005/02/07 06:41:42 cpressey Exp $
37  */
38 
39 #include <stdio.h>
40 
41 #ifndef __DISKUTIL_H_
42 #define __DISKUTIL_H_
43 
44 #include "functions.h"
45 
46 /*** TYPES ***/
47 
48 struct storage;
49 struct disk;
50 struct slice;
51 struct subpartition;
52 
53 #define FS_HAMMER	0
54 #define FS_UFS		1
55 
56 #ifdef NEEDS_DISKUTIL_STRUCTURE_DEFINITIONS
57 
58 struct storage {
59 	struct disk *disk_head;
60 	struct disk *disk_tail;
61 	struct disk *selected_disk;
62 	struct slice *selected_slice;
63 	long ram;			/* amount of physical memory in MB */
64 };
65 
66 struct disk {
67 	struct disk *next;
68 	struct disk *prev;
69 	struct slice *slice_head;
70 	struct slice *slice_tail;
71 	char *desc;			/* from whereever we get the best */
72 	int number;			/* Position in kern.disks */
73 	char *device;			/* `ad0', `da1', and such */
74 	char *serno;			/* serial number */
75 	int cylinders;			/* geometry information */
76 	int heads;
77 	int sectors;			/* (sectors per track) */
78 	long capacity;			/* capacity in megabytes */
79 	int we_formatted;		/* did we format it ourselves? */
80 };
81 
82 struct slice {
83 	struct disk *parent;
84 	struct slice *next;
85 	struct slice *prev;
86 	struct subpartition *subpartition_head;
87 	struct subpartition *subpartition_tail;
88 	char *desc;			/* description (w/sysid string) */
89 	int number;			/* 1 - 4 (or more?) (from fdisk) */
90 	unsigned long start;		/* start sector (from fdisk) */
91 	unsigned long size;		/* size in sectors (from fdisk) */
92 	int type;			/* sysid of slice (from fdisk) */
93 	int flags;			/* flags (from fdisk) */
94 	unsigned long capacity;		/* capacity in megabytes */
95 };
96 
97 struct subpartition {
98 	struct slice *parent;
99 	struct subpartition *next;
100 	struct subpartition *prev;
101 	char letter;			/* 'a' = root partition */
102 	char *mountpoint;		/* includes leading slash */
103 	long capacity;			/* in megabytes, -1 = "rest of disk" */
104 	int encrypted;
105 	int softupdates;
106 	long fsize;			/* fragment size */
107 	long bsize;			/* block size */
108 	int is_swap;
109 	int tmpfsbacked;		/* TMPFS Backed */
110 	int type;			/* FS type (UFS, HAMMER) */
111 	int pfs;			/* HAMMER pseudo file system */
112 };
113 
114 #endif /* NEEDS_DISKUTIL_STRUCTURE_DEFINITIONS */
115 
116 /*** PROTOTYPES ***/
117 
118 struct storage		*storage_new(void);
119 void			 storage_free(struct storage *);
120 void			 storage_set_memsize(struct storage *, unsigned long);
121 long			 storage_get_memsize(const struct storage *);
122 struct disk		*storage_disk_first(const struct storage *);
123 void			 storage_set_selected_disk(struct storage *, struct disk *);
124 struct disk		*storage_get_selected_disk(const struct storage *);
125 void			 storage_set_selected_slice(struct storage *, struct slice *);
126 struct slice		*storage_get_selected_slice(const struct storage *);
127 int			 storage_get_tmpfs_status(const char *, struct storage *);
128 
129 struct disk		*disk_new(struct storage *, const char *);
130 struct disk		*disk_find(const struct storage *, const char *);
131 struct disk		*disk_next(const struct disk *);
132 void			 disks_free(struct storage *);
133 void			 disk_set_number(struct disk *, const int);
134 void			 disk_set_desc(struct disk *, const char *);
135 void			 disk_set_serno(struct disk *, const char *);
136 int			 disk_get_number(const struct disk *);
137 const char		*disk_get_desc(const struct disk *);
138 const char		*disk_get_device_name(const struct disk *);
139 const char		*disk_get_serno(const struct disk *);
140 struct slice		*disk_slice_first(const struct disk *);
141 void			 disk_set_formatted(struct disk *, int);
142 int			 disk_get_formatted(const struct disk *);
143 void			 disk_set_geometry(struct disk *, int, int, int);
144 void			 disk_get_geometry(const struct disk *, int *, int *, int *);
145 
146 struct slice		*slice_new(struct disk *, int, int, int,
147 				   unsigned long, unsigned long);
148 struct slice		*slice_find(const struct disk *, int);
149 struct slice		*slice_next(const struct slice *);
150 int			 slice_get_number(const struct slice *);
151 const char		*slice_get_desc(const struct slice *);
152 const char		*slice_get_device_name(const struct slice *);
153 unsigned long		 slice_get_capacity(const struct slice *);
154 unsigned long		 slice_get_start(const struct slice *);
155 unsigned long		 slice_get_size(const struct slice *);
156 int			 slice_get_type(const struct slice *);
157 int			 slice_get_flags(const struct slice *);
158 void			 slices_free(struct slice *);
159 struct subpartition	*slice_subpartition_first(const struct slice *);
160 
161 struct subpartition	*subpartition_new_hammer(struct slice *, const char *,
162 						 long, int);
163 struct subpartition	*subpartition_new_ufs(struct slice *, const char *,
164 					      long, int, int, long, long, int);
165 int			 subpartition_count(const struct slice *);
166 struct subpartition	*subpartition_find(const struct slice *, const char *, ...)
167 			     __printflike(2, 3);
168 struct subpartition	*subpartition_of(const struct slice *, const char *, ...)
169 			     __printflike(2, 3);
170 struct subpartition	*subpartition_find_capacity(const struct slice *, long);
171 void		 	 subpartitions_free(struct slice *);
172 struct subpartition	*subpartition_next(const struct subpartition *);
173 int 			 subpartition_get_pfs(const struct subpartition *);
174 const char		*subpartition_get_mountpoint(const struct subpartition *);
175 const char		*subpartition_get_device_name(const struct subpartition *);
176 char			 subpartition_get_letter(const struct subpartition *);
177 unsigned long		 subpartition_get_fsize(const struct subpartition *);
178 unsigned long		 subpartition_get_bsize(const struct subpartition *);
179 long			 subpartition_get_capacity(const struct subpartition *);
180 void			 subpartition_clr_encrypted(struct subpartition *);
181 int			 subpartition_is_encrypted(const struct subpartition *);
182 int			 subpartition_is_swap(const struct subpartition *);
183 int			 subpartition_is_softupdated(const struct subpartition *);
184 int			 subpartition_is_tmpfsbacked(const struct subpartition *);
185 
186 long			 measure_activated_swap(const struct i_fn_args *);
187 long			 measure_activated_swap_from_slice(const struct i_fn_args *,
188 				const struct disk *, const struct slice *);
189 long			 measure_activated_swap_from_disk(const struct i_fn_args *,
190 				const struct disk *);
191 void			*swapoff_all(const struct i_fn_args *);
192 void			*remove_all_mappings(const struct i_fn_args *);
193 
194 int			 survey_storage(struct i_fn_args *);
195 
196 #endif /* !__DISKUTIL_H_ */
197