blob: 4d7ec257cbbd703c5daca5f6b2aa84d12a0b7a3d [file] [log] [blame]
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001/*
2 * blkid.h - Interface for libblkid, a library to identify block devices
3 *
4 * Copyright (C) 2001 Andreas Dilger
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
9 * %End-Header%
10 */
11
12#ifndef _BLKID_BLKID_H
13#define _BLKID_BLKID_H
14
15#include <sys/types.h>
16#include <stdio.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22#define BLKID_VERSION "1.2.0"
23#define BLKID_DATE "22-Nov-2001"
24
25#include "blkid/list.h"
26#include "blkid/blkid_types.h"
27
28typedef __s64 blkid_loff_t;
29
30/*
31 * This describes the attributes of a specific device.
32 * We can traverse all of the tags by bid_tags (linking to the tag bit_names).
33 * The bid_label and bid_uuid fields are shortcuts to the LABEL and UUID tag
34 * values, if they exist.
35 */
36typedef struct blkid_dev
37{
38 struct list_head bid_devs; /* All devices in the cache */
39 struct list_head bid_tags; /* All tags for this device */
40 char *bid_name; /* Device inode pathname */
41 char *bid_type; /* Preferred device TYPE */
42 blkid_loff_t bid_size; /* Filesystem size in bytes */
43 blkid_loff_t bid_free; /* Filesystem free in bytes */
44 blkid_loff_t bid_devsize; /* Device size in bytes */
45 dev_t bid_devno; /* Device major/minor number */
46 time_t bid_time; /* Last update time of device */
47 unsigned int bid_id; /* Unique cache id for device */
48 unsigned int bid_flags; /* Device status bitflags */
49 char *bid_label; /* Shortcut to device LABEL */
50 char *bid_uuid; /* Shortcut to binary UUID */
51 unsigned long bid_unused[13]; /* Fields for future use */
52} blkid_dev;
53
54#define BLKID_BID_FL_VERIFIED 0x0001 /* Device data validated from disk */
55#define BLKID_BID_FL_MTYPE 0x0002 /* Device has multiple type matches */
56
57/*
58 * Each tag defines a NAME=value pair for a particular device. The tags
59 * are linked via bit_names for a single device, so that traversing the
60 * names list will get you a list of all tags associated with a device.
61 * They are also linked via bit_values for all devices, so one can easily
62 * search all tags with a given NAME for a specific value.
63 */
64typedef struct blkid_tag
65{
66 struct list_head bit_tags; /* All tags for this device */
67 struct list_head bit_names; /* All tags with given NAME */
68 char *bit_name; /* NAME of tag (shared) */
69 char *bit_val; /* value of tag */
70 struct blkid_dev *bit_dev; /* pointer to device */
71 unsigned long bit_unused[9]; /* Fields for future use */
72} blkid_tag;
73
74/*
75 * Minimum number of seconds between device probes, even when reading
76 * from the cache. This is to avoid re-probing all devices which were
77 * just probed by another program that does not share the cache.
78 */
79#define BLKID_PROBE_MIN 2
80
81/*
82 * Time in seconds an entry remains verified in the in-memory cache
83 * before being reverified (in case of long-running processes that
84 * keep a cache in memory and continue to use it for a long time).
85 */
86#define BLKID_PROBE_INTERVAL 200
87
88/* This describes an entire blkid cache file and probed devices.
89 * We can traverse all of the found devices via bic_list.
90 * We can traverse all of the tag types by bic_tags, which hold empty tags
91 * for each tag type. Those tags can be used as list_heads for iterating
92 * through all devices with a specific tag type (e.g. LABEL).
93 */
94typedef struct blkid_cache
95{
96 struct list_head bic_devs; /* List head of all devices */
97 struct list_head bic_tags; /* List head of all tag types */
98 time_t bic_time; /* Last probe time */
99 unsigned int bic_idmax; /* Highest ID assigned */
100 unsigned int bic_flags; /* Status flags of the cache */
101 unsigned long bic_unused[9]; /* Fields for future use */
102} blkid_cache;
103
104#define BLKID_BIC_FL_PARSED 0x0001 /* We parsed a cache file */
105#define BLKID_BIC_FL_PROBED 0x0002 /* We probed /proc/partition devices */
106#define BLKID_BIC_FL_CHANGED 0x0004 /* Cache has changed from disk */
107
108extern char *string_copy(const char *s);
109extern char *stringn_copy(const char *s, const int length);
110extern void string_free(char *s);
111extern blkid_cache *blkid_new_cache(void);
112extern void blkid_free_cache(blkid_cache *cache);
113
114#define BLKID_CACHE_FILE "/etc/blkid.tab"
115extern const char *devdirs[];
116
117#define BLKID_ERR_IO 5
118#define BLKID_ERR_PROC 9
119#define BLKID_ERR_MEM 12
120#define BLKID_ERR_CACHE 14
121#define BLKID_ERR_DEV 19
122#define BLKID_ERR_PARAM 22
123#define BLKID_ERR_BIG 27
124
125#ifdef DEBUG
126#define DEBUG_CACHE
127#define DEBUG_DUMP
128#define DEBUG_DEV
129#define DEBUG_DEVNAME
130#define DEBUG_DEVNO
131#define DEBUG_PROBE
132#define DEBUG_READ
133#define DEBUG_RESOLVE
134#define DEBUG_SAVE
135#define DEBUG_TAG
136#define CHECK_TAG
137#endif
138
139#if defined(TEST_PROGRAM) && !defined(DEBUG_DUMP)
140#define DEBUG_DUMP
141#endif
142
143#ifdef DEBUG_DUMP
144static inline void DEB_DUMP_TAG(blkid_tag *tag)
145{
146 if (!tag) {
147 printf(" tag: NULL\n");
148 return;
149 }
150
151 printf(" tag: %s=\"%s\"\n", tag->bit_name, tag->bit_val);
152}
153
154static inline void DEB_DUMP_DEV(blkid_dev *dev)
155{
156 struct list_head *p;
157
158 if (!dev) {
159 printf(" dev: NULL\n");
160 return;
161 }
162
163 printf(" dev: name = %s\n", dev->bid_name);
164 printf(" dev: DEVNO=\"0x%0Lx\"\n", dev->bid_devno);
165 printf(" dev: ID=\"%u\"\n", dev->bid_id);
166 printf(" dev: TIME=\"%lu\"\n", dev->bid_time);
167 printf(" dev: size = %Lu\n", dev->bid_size);
168 printf(" dev: flags = 0x%08X\n", dev->bid_flags);
169
170 list_for_each(p, &dev->bid_tags) {
171 blkid_tag *tag = list_entry(p, blkid_tag, bit_tags);
172 DEB_DUMP_TAG(tag);
173 }
174 printf("\n");
175}
176
177static inline void DEB_DUMP_CACHE(blkid_cache *cache)
178{
179 struct list_head *p;
180
181 if (!cache) {
182 printf("cache: NULL\n");
183 return;
184 }
185
186 printf("cache: time = %lu\n", cache->bic_time);
187 printf("cache: idmax = %u\n", cache->bic_idmax);
188 printf("cache: flags = 0x%08X\n", cache->bic_flags);
189
190 list_for_each(p, &cache->bic_devs) {
191 blkid_dev *dev = list_entry(p, blkid_dev, bid_devs);
192 DEB_DUMP_DEV(dev);
193 }
194}
195#else
196#define DEB_DUMP_TAG(tag) do {} while (0)
197#define DEB_DUMP_DEV(dev) do {} while (0)
198#define DEB_DUMP_CACHE(cache) do {} while (0)
199#endif
200
201/*
202 * Primitive disk functions: llseek.c, getsize.c
203 */
204extern blkid_loff_t blkid_llseek(int fd, blkid_loff_t offset, int whence);
205extern blkid_loff_t blkid_get_dev_size(int fd);
206
207/*
208 * Getting data from the cache file: read.c
209 */
210int blkid_read_cache_line(blkid_cache *cache, blkid_dev **dev_p, char *cp);
211int blkid_read_cache_file(blkid_cache **cache, FILE *file);
212int blkid_read_cache(blkid_cache **cache, const char *filename);
213
214/*
215 * Save data to the cache file: save.c
216 */
217int blkid_save_cache_file(blkid_cache *cache, FILE *file);
218int blkid_save_cache(blkid_cache *cache, char *filename);
219
220/*
221 * Identify a device by inode name: probe.c
222 */
223extern blkid_dev *blkid_devname_to_dev(const char *devname,
224 blkid_loff_t size);
225
226/*
227 * Locate a device by inode name: devname.c
228 */
229extern blkid_dev *blkid_find_devname(blkid_cache *cache, const char *devname);
230extern blkid_dev *blkid_verify_devname(blkid_cache *cache, blkid_dev *dev);
231extern blkid_dev *blkid_get_devname(blkid_cache *cache, const char *devname);
232extern int blkid_probe_all(blkid_cache **cache);
233
234/*
235 * Locate a device by device major/minor number: devno.c
236 */
237extern char *blkid_devno_to_devname(dev_t devno);
238extern blkid_dev *blkid_find_devno(blkid_cache *cache, dev_t devno);
239extern blkid_dev *blkid_get_devno(blkid_cache *cache, dev_t devno);
240
241/*
242 * Functions to create and find a specific tag type: tag.c
243 */
244extern blkid_tag *blkid_new_tag(void);
245extern void blkid_free_tag(blkid_tag *tag);
246extern int blkid_create_tag(blkid_dev *dev, blkid_tag **tag,
247 const char *name, const char *value,
248 const int vlength);
249extern blkid_tag *blkid_token_to_tag(const char *token);
250extern blkid_tag *blkid_find_tv_tags(blkid_tag *head, const char *value);
251extern blkid_tag *blkid_find_tag_dev(blkid_dev *dev, blkid_tag *tag);
252extern blkid_tag *blkid_find_head_cache(blkid_cache *cache, blkid_tag *tag);
253extern blkid_tag *blkid_find_tag_cache(blkid_cache *cache, blkid_tag *tag);
254extern blkid_tag *blkid_get_tag_cache(blkid_cache *cache, blkid_tag *tag);
255
256/*
257 * Functions to create and find a specific tag type: dev.c
258 */
259extern blkid_dev *blkid_new_dev(void);
260extern void blkid_free_dev(blkid_dev *dev);
261extern blkid_dev *blkid_add_dev_to_cache(blkid_cache *cache, blkid_dev *dev);
262
263/*
264 * Helper functions for primarily single use: resolve.c
265 */
266extern char *blkid_get_tagname_devname(blkid_cache *cache, const char *tagname,
267 const char *devname);
268extern char *blkid_get_token(blkid_cache *cache, const char *token,
269 const char *value);
270
271#ifdef __cplusplus
272}
273#endif
274
275#endif /* _BLKID_BLKID_H */