OSDN Git Service

1113213247c8373d44cf938def839687eca7003d
[android-x86/external-parted.git] / include / parted / disk.h
1 /*
2     libparted - a library for manipulating disk partitions
3     Copyright (C) 1999-2002, 2007-2011 Free Software Foundation, Inc.
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * \addtogroup PedDisk
21  * @{
22  */
23
24 /** \file disk.h */
25
26 #ifndef PED_DISK_H_INCLUDED
27 #define PED_DISK_H_INCLUDED
28
29 /**
30  * Disk flags
31  */
32 enum _PedDiskFlag {
33         /* This flag (which defaults to true) controls if disk types for
34            which cylinder alignment is optional do cylinder alignment when a
35            new partition gets added.
36            This flag is available for msdos and sun disklabels (for sun labels
37            it only controls the aligning of the end of the partition) */
38         PED_DISK_CYLINDER_ALIGNMENT=1,
39 };
40 #define PED_DISK_FIRST_FLAG             PED_DISK_CYLINDER_ALIGNMENT
41 #define PED_DISK_LAST_FLAG              PED_DISK_CYLINDER_ALIGNMENT
42
43 /**
44  * Partition types
45  */
46 enum _PedPartitionType {
47         PED_PARTITION_NORMAL            = 0x00,
48         PED_PARTITION_LOGICAL           = 0x01,
49         PED_PARTITION_EXTENDED          = 0x02,
50         PED_PARTITION_FREESPACE         = 0x04,
51         PED_PARTITION_METADATA          = 0x08,
52         PED_PARTITION_PROTECTED         = 0x10
53 };
54
55 /**
56  * Partition flags.
57  */
58 enum _PedPartitionFlag {
59         PED_PARTITION_BOOT=1,
60         PED_PARTITION_ROOT=2,
61         PED_PARTITION_SWAP=3,
62         PED_PARTITION_HIDDEN=4,
63         PED_PARTITION_RAID=5,
64         PED_PARTITION_LVM=6,
65         PED_PARTITION_LBA=7,
66         PED_PARTITION_HPSERVICE=8,
67         PED_PARTITION_PALO=9,
68         PED_PARTITION_PREP=10,
69         PED_PARTITION_MSFT_RESERVED=11,
70         PED_PARTITION_BIOS_GRUB=12,
71         PED_PARTITION_APPLE_TV_RECOVERY=13,
72         PED_PARTITION_DIAG=14,
73         PED_PARTITION_LEGACY_BOOT=15
74 };
75 #define PED_PARTITION_FIRST_FLAG        PED_PARTITION_BOOT
76 #define PED_PARTITION_LAST_FLAG         PED_PARTITION_LEGACY_BOOT
77
78 enum _PedDiskTypeFeature {
79         PED_DISK_TYPE_EXTENDED=1,       /**< supports extended partitions */
80         PED_DISK_TYPE_PARTITION_NAME=2  /**< supports partition names */
81 };
82 #define PED_DISK_TYPE_FIRST_FEATURE    PED_DISK_TYPE_EXTENDED
83 #define PED_DISK_TYPE_LAST_FEATURE     PED_DISK_TYPE_PARTITION_NAME
84
85 struct _PedDisk;
86 struct _PedPartition;
87 struct _PedDiskOps;
88 struct _PedDiskType;
89 struct _PedDiskArchOps;
90
91 typedef enum _PedDiskFlag               PedDiskFlag;
92 typedef enum _PedPartitionType          PedPartitionType;
93 typedef enum _PedPartitionFlag          PedPartitionFlag;
94 typedef enum _PedDiskTypeFeature        PedDiskTypeFeature;
95 typedef struct _PedDisk                 PedDisk;
96 typedef struct _PedPartition            PedPartition;
97 typedef const struct _PedDiskOps        PedDiskOps;
98 typedef struct _PedDiskType             PedDiskType;
99 typedef const struct _PedDiskArchOps    PedDiskArchOps;
100
101 #include <parted/device.h>
102 #include <parted/filesys.h>
103 #include <parted/natmath.h>
104 #include <parted/geom.h>
105 #include <stdbool.h>
106
107 /** @} */
108
109 /**
110  * \addtogroup PedPartition
111  *
112  * @{
113  */
114
115 /** \file disk.h */
116
117 /**
118  * PedPartition structure represents a partition.
119  */
120 struct _PedPartition {
121         PedPartition*           prev;
122         PedPartition*           next;
123
124         /**< the partition table of the partition */
125         PedDisk*                disk;
126         PedGeometry             geom;   /**< geometry of the partition */
127
128         /**< the partition number:  In Linux, this is the
129              same as the minor number. No assumption
130              should be made about "num" and "type"
131              - different disk labels have different rules. */
132
133         int                     num;
134         PedPartitionType        type;   /**< the type of partition: a bit field of
135                                                 PED_PARTITION_LOGICAL, PED_PARTITION_EXTENDED,
136                                                 PED_PARTITION_METADATA
137                                                 and PED_PARTITION_FREESPACE.
138                                                 Both the first two, and the last two are
139                                                 mutually exclusive.
140                                                         An extended partition is a primary
141                                                 partition that may contain logical partitions.
142                                                 There is at most one extended partition on
143                                                 a disk.
144                                                         A logical partition is like a primary
145                                                 partition, except it's inside an extended
146                                                 partition. Internally, pseudo partitions are
147                                                 allocated to represent free space, or disk
148                                                 label meta-data.  These have the
149                                                 PED_PARTITION_FREESPACE or
150                                                 PED_PARTITION_METADATA bit set. */
151
152         /**< The type of file system on the partition. NULL if unknown. */
153         const PedFileSystemType* fs_type;
154
155         /**< Only used for an extended partition.  The list of logical
156              partitions (and free space and metadata within the extended
157              partition). */
158         PedPartition*           part_list;
159
160         void*                   disk_specific;
161 };
162
163 /** @} */
164
165 /**
166  * \addtogroup PedDisk
167  * @{
168  */
169
170 /**
171  * Represents a disk label (partition table).
172  */
173 struct _PedDisk {
174         PedDevice*          dev;         /**< the device where the
175                                               partition table lies */
176         const PedDiskType*  type;        /**< type of disk label */
177         const int*          block_sizes; /**< block sizes supported
178                                               by this label */
179         PedPartition*       part_list;   /**< list of partitions. Access with
180                                               ped_disk_next_partition() */
181
182         void*               disk_specific;
183
184 /* office use only ;-) */
185         int                 needs_clobber;      /**< clobber before write? */
186         int                 update_mode;        /**< mode without free/metadata
187                                                    partitions, for easier
188                                                    update */
189 };
190
191 struct _PedDiskOps {
192         /* disk label operations */
193         int (*probe) (const PedDevice *dev);
194         int (*clobber) (PedDevice* dev);
195         PedDisk* (*alloc) (const PedDevice* dev);
196         PedDisk* (*duplicate) (const PedDisk* disk);
197         void (*free) (PedDisk* disk);
198         int (*read) (PedDisk* disk);
199         int (*write) (const PedDisk* disk);
200         int (*disk_set_flag) (
201                 PedDisk *disk,
202                 PedDiskFlag flag,
203                 int state);
204         int (*disk_get_flag) (
205                 const PedDisk *disk,
206                 PedDiskFlag flag);
207         int (*disk_is_flag_available) (
208                 const PedDisk *disk,
209                 PedDiskFlag flag);
210         /** \todo add label guessing op here */
211
212         /* partition operations */
213         PedPartition* (*partition_new) (
214                 const PedDisk* disk,
215                 PedPartitionType part_type,
216                 const PedFileSystemType* fs_type,
217                 PedSector start,
218                 PedSector end);
219         PedPartition* (*partition_duplicate) (const PedPartition* part);
220         void (*partition_destroy) (PedPartition* part);
221         int (*partition_set_system) (PedPartition* part,
222                                      const PedFileSystemType* fs_type);
223         int (*partition_set_flag) (
224                 PedPartition* part,
225                 PedPartitionFlag flag,
226                 int state);
227         int (*partition_get_flag) (
228                 const PedPartition* part,
229                 PedPartitionFlag flag);
230         int (*partition_is_flag_available) (
231                 const PedPartition* part,
232                 PedPartitionFlag flag);
233         void (*partition_set_name) (PedPartition* part, const char* name);
234         const char* (*partition_get_name) (const PedPartition* part);
235         int (*partition_align) (PedPartition* part,
236                                 const PedConstraint* constraint);
237         int (*partition_enumerate) (PedPartition* part);
238         bool (*partition_check) (const PedPartition* part);
239
240         /* other */
241         int (*alloc_metadata) (PedDisk* disk);
242         int (*get_max_primary_partition_count) (const PedDisk* disk);
243         bool (*get_max_supported_partition_count) (const PedDisk* disk,
244                                                    int* supported);
245         PedAlignment *(*get_partition_alignment)(const PedDisk *disk);
246         PedSector (*max_length) (void);
247         PedSector (*max_start_sector) (void);
248 };
249
250 struct _PedDiskType {
251         PedDiskType*            next;
252         const char*             name; /**< the name of the partition table type.
253                                            \todo not very intuitive name */
254         PedDiskOps* const       ops;
255
256         PedDiskTypeFeature      features;   /**< bitmap of supported features */
257 };
258
259 /**
260  * Architecture-specific operations.  i.e. communication with kernel (or
261  * whatever) about changes, etc.
262  */
263 struct _PedDiskArchOps {
264         char* (*partition_get_path) (const PedPartition* part);
265         int (*partition_is_busy) (const PedPartition* part);
266         int (*disk_commit) (PedDisk* disk);
267 };
268
269 extern void ped_disk_type_register (PedDiskType* type);
270 extern void ped_disk_type_unregister (PedDiskType* type);
271
272 extern PedDiskType* ped_disk_type_get_next (PedDiskType const *type)
273   _GL_ATTRIBUTE_PURE;
274 extern PedDiskType* ped_disk_type_get (const char* name)
275   _GL_ATTRIBUTE_PURE;
276 extern int ped_disk_type_check_feature (const PedDiskType* disk_type,
277                                         PedDiskTypeFeature feature)
278   _GL_ATTRIBUTE_PURE;
279
280 extern PedDiskType* ped_disk_probe (PedDevice* dev);
281 extern int ped_disk_clobber (PedDevice* dev);
282 extern PedDisk* ped_disk_new (PedDevice* dev);
283 extern PedDisk* ped_disk_new_fresh (PedDevice* dev,
284                                     const PedDiskType* disk_type);
285 extern PedDisk* ped_disk_duplicate (const PedDisk* old_disk);
286 extern void ped_disk_destroy (PedDisk* disk);
287 extern int ped_disk_commit (PedDisk* disk);
288 extern int ped_disk_commit_to_dev (PedDisk* disk);
289 extern int ped_disk_commit_to_os (PedDisk* disk);
290 extern int ped_disk_check (const PedDisk* disk);
291 extern void ped_disk_print (const PedDisk* disk);
292
293 extern int ped_disk_get_primary_partition_count (const PedDisk* disk)
294   _GL_ATTRIBUTE_PURE;
295 extern int ped_disk_get_last_partition_num (const PedDisk* disk)
296   _GL_ATTRIBUTE_PURE;
297 extern int ped_disk_get_max_primary_partition_count (const PedDisk* disk);
298 extern bool ped_disk_get_max_supported_partition_count(const PedDisk* disk,
299                                                        int* supported);
300 extern PedAlignment *ped_disk_get_partition_alignment(const PedDisk *disk);
301
302 extern int ped_disk_set_flag(PedDisk *disk, PedDiskFlag flag, int state);
303 extern int ped_disk_get_flag(const PedDisk *disk, PedDiskFlag flag);
304 extern int ped_disk_is_flag_available(const PedDisk *disk, PedDiskFlag flag);
305
306 extern const char *ped_disk_flag_get_name(PedDiskFlag flag);
307 extern PedDiskFlag ped_disk_flag_get_by_name(const char *name);
308 extern PedDiskFlag ped_disk_flag_next(PedDiskFlag flag) _GL_ATTRIBUTE_CONST;
309
310 /** @} */
311
312 /**
313  * \addtogroup PedPartition
314  *
315  * @{
316  */
317
318 extern PedPartition* ped_partition_new (const PedDisk* disk,
319                                         PedPartitionType type,
320                                         const PedFileSystemType* fs_type,
321                                         PedSector start,
322                                         PedSector end);
323 extern void ped_partition_destroy (PedPartition* part);
324 extern int ped_partition_is_active (const PedPartition* part) _GL_ATTRIBUTE_PURE;
325 extern int ped_partition_set_flag (PedPartition* part, PedPartitionFlag flag,
326                                    int state);
327 extern int ped_partition_get_flag (const PedPartition* part,
328                                    PedPartitionFlag flag);
329 extern int ped_partition_is_flag_available (const PedPartition* part,
330                                             PedPartitionFlag flag);
331 extern int ped_partition_set_system (PedPartition* part,
332                                      const PedFileSystemType* fs_type);
333 extern int ped_partition_set_name (PedPartition* part, const char* name);
334 extern const char* ped_partition_get_name (const PedPartition* part);
335 extern int ped_partition_is_busy (const PedPartition* part);
336 extern char* ped_partition_get_path (const PedPartition* part);
337
338 extern const char* ped_partition_type_get_name (PedPartitionType part_type)
339   _GL_ATTRIBUTE_CONST;
340 extern const char* ped_partition_flag_get_name (PedPartitionFlag flag);
341 extern PedPartitionFlag ped_partition_flag_get_by_name (const char* name);
342 extern PedPartitionFlag ped_partition_flag_next (PedPartitionFlag flag)
343   _GL_ATTRIBUTE_CONST;
344
345 /** @} */
346
347 /**
348  * \addtogroup PedDisk
349  * @{
350  */
351
352 extern int ped_disk_add_partition (PedDisk* disk, PedPartition* part,
353                                    const PedConstraint* constraint);
354 extern int ped_disk_remove_partition (PedDisk* disk, PedPartition* part);
355 extern int ped_disk_delete_partition (PedDisk* disk, PedPartition* part);
356 extern int ped_disk_delete_all (PedDisk* disk);
357 extern int ped_disk_set_partition_geom (PedDisk* disk, PedPartition* part,
358                                         const PedConstraint* constraint,
359                                         PedSector start, PedSector end);
360 extern int ped_disk_maximize_partition (PedDisk* disk, PedPartition* part,
361                                         const PedConstraint* constraint);
362 extern PedGeometry* ped_disk_get_max_partition_geometry (PedDisk* disk,
363                 PedPartition* part, const PedConstraint* constraint);
364 extern int ped_disk_minimize_extended_partition (PedDisk* disk);
365
366 extern PedPartition* ped_disk_next_partition (const PedDisk* disk,
367                                               const PedPartition* part)
368   _GL_ATTRIBUTE_PURE;
369 extern PedPartition* ped_disk_get_partition (const PedDisk* disk, int num)
370   _GL_ATTRIBUTE_PURE;
371 extern PedPartition* ped_disk_get_partition_by_sector (const PedDisk* disk,
372                                                        PedSector sect)
373   _GL_ATTRIBUTE_PURE;
374 extern PedPartition* ped_disk_extended_partition (const PedDisk* disk)
375   _GL_ATTRIBUTE_PURE;
376
377 extern PedSector ped_disk_max_partition_length (const PedDisk *disk);
378 extern PedSector ped_disk_max_partition_start_sector (const PedDisk *disk);
379
380 /* internal functions */
381 extern PedDisk* _ped_disk_alloc (const PedDevice* dev, const PedDiskType* type);
382 extern void _ped_disk_free (PedDisk* disk);
383
384
385 /** @} */
386
387 /**
388  * \addtogroup PedPartition
389  *
390  * @{
391  */
392
393 extern PedPartition* _ped_partition_alloc (const PedDisk* disk,
394                                            PedPartitionType type,
395                                            const PedFileSystemType* fs_type,
396                                            PedSector start,
397                                            PedSector end);
398 extern void _ped_partition_free (PedPartition* part);
399
400 extern int _ped_partition_attempt_align (
401                 PedPartition* part, const PedConstraint* external,
402                 PedConstraint* internal);
403
404 #endif /* PED_DISK_H_INCLUDED */
405
406 /** @} */