blob: 2d5e8cb78ac9e0eda364552bcd4a9f4e109cb754 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * blkidP.h - Internal interfaces for libblkid
3 *
4 * Copyright (C) 2001 Andreas Dilger
5 * Copyright (C) 2003 Theodore Ts'o
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
10 * %End-Header%
11 */
12
13#ifndef _BLKID_BLKIDP_H
14#define _BLKID_BLKIDP_H
15
16/* support debug output if LIBBLKID_DEBUG env. variable is set */
17#define CONFIG_BLKID_DEBUG 1
18
19/* Always confirm that /dev/disk-by symlinks match with LABEL/UUID on device */
20/* #define CONFIG_BLKID_VERIFY_UDEV 1 */
21
22#include <sys/types.h>
23#include <dirent.h>
24#include <sys/stat.h>
25#include <stdio.h>
26#include <stdarg.h>
27#include <stdint.h>
28
29#include "c.h"
30#include "bitops.h" /* $(top_srcdir)/include/ */
31#include "blkdev.h"
32
33#include "blkid.h"
34#include "list.h"
35
36/*
37 * This describes the attributes of a specific device.
38 * We can traverse all of the tags by bid_tags (linking to the tag bit_names).
39 * The bid_label and bid_uuid fields are shortcuts to the LABEL and UUID tag
40 * values, if they exist.
41 */
42struct blkid_struct_dev
43{
44 struct list_head bid_devs; /* All devices in the cache */
45 struct list_head bid_tags; /* All tags for this device */
46 blkid_cache bid_cache; /* Dev belongs to this cache */
47 char *bid_name; /* Device inode pathname */
48 char *bid_type; /* Preferred device TYPE */
49 int bid_pri; /* Device priority */
50 dev_t bid_devno; /* Device major/minor number */
51 time_t bid_time; /* Last update time of device */
52 suseconds_t bid_utime; /* Last update time (microseconds) */
53 unsigned int bid_flags; /* Device status bitflags */
54 char *bid_label; /* Shortcut to device LABEL */
55 char *bid_uuid; /* Shortcut to binary UUID */
56};
57
58#define BLKID_BID_FL_VERIFIED 0x0001 /* Device data validated from disk */
59#define BLKID_BID_FL_INVALID 0x0004 /* Device is invalid */
60#define BLKID_BID_FL_REMOVABLE 0x0008 /* Device added by blkid_probe_all_removable() */
61
62/*
63 * Each tag defines a NAME=value pair for a particular device. The tags
64 * are linked via bit_names for a single device, so that traversing the
65 * names list will get you a list of all tags associated with a device.
66 * They are also linked via bit_values for all devices, so one can easily
67 * search all tags with a given NAME for a specific value.
68 */
69struct blkid_struct_tag
70{
71 struct list_head bit_tags; /* All tags for this device */
72 struct list_head bit_names; /* All tags with given NAME */
73 char *bit_name; /* NAME of tag (shared) */
74 char *bit_val; /* value of tag */
75 blkid_dev bit_dev; /* pointer to device */
76};
77typedef struct blkid_struct_tag *blkid_tag;
78
79/*
80 * Chain IDs
81 */
82enum {
83 BLKID_CHAIN_SUBLKS, /* FS/RAID superblocks (enabled by default) */
84 BLKID_CHAIN_TOPLGY, /* Block device topology */
85 BLKID_CHAIN_PARTS, /* Partition tables */
86
87 BLKID_NCHAINS /* number of chains */
88};
89
90struct blkid_chain {
91 const struct blkid_chaindrv *driver; /* chain driver */
92
93 int enabled; /* boolean */
94 int flags; /* BLKID_<chain>_* */
95 int binary; /* boolean */
96 int idx; /* index of the current prober (or -1) */
97 unsigned long *fltr; /* filter or NULL */
98 void *data; /* private chain data or NULL */
99};
100
101/*
102 * Chain driver
103 */
104struct blkid_chaindrv {
105 const size_t id; /* BLKID_CHAIN_* */
106 const char *name; /* name of chain (for debug purpose) */
107 const int dflt_flags; /* default chain flags */
108 const int dflt_enabled; /* default enabled boolean */
109 int has_fltr; /* boolean */
110
111 const struct blkid_idinfo **idinfos; /* description of probing functions */
112 const size_t nidinfos; /* number of idinfos */
113
114 /* driver operations */
115 int (*probe)(blkid_probe, struct blkid_chain *);
116 int (*safeprobe)(blkid_probe, struct blkid_chain *);
117 void (*free_data)(blkid_probe, void *);
118};
119
120/*
121 * Low-level probe result
122 */
123#define BLKID_PROBVAL_BUFSIZ 128
124
125#define BLKID_NVALS_SUBLKS 18
126#define BLKID_NVALS_TOPLGY 5
127#define BLKID_NVALS_PARTS 13
128
129/* Max number of all values in probing result */
130#define BLKID_NVALS (BLKID_NVALS_SUBLKS + \
131 BLKID_NVALS_TOPLGY + \
132 BLKID_NVALS_PARTS)
133
134struct blkid_prval
135{
136 const char *name; /* value name */
137 unsigned char data[BLKID_PROBVAL_BUFSIZ]; /* value data */
138 size_t len; /* length of value data */
139
140 struct blkid_chain *chain; /* owner */
141};
142
143/*
144 * Filesystem / Raid magic strings
145 */
146struct blkid_idmag
147{
148 const char *magic; /* magic string */
149 unsigned int len; /* length of magic */
150
151 long kboff; /* kilobyte offset of superblock */
152 unsigned int sboff; /* byte offset within superblock */
153};
154
155/*
156 * Filesystem / Raid description
157 */
158struct blkid_idinfo
159{
160 const char *name; /* fs, raid or partition table name */
161 int usage; /* BLKID_USAGE_* flag */
162 int flags; /* BLKID_IDINFO_* flags */
163 int minsz; /* minimal device size */
164
165 /* probe function */
166 int (*probefunc)(blkid_probe pr, const struct blkid_idmag *mag);
167
168 struct blkid_idmag magics[]; /* NULL or array with magic strings */
169};
170
171#define BLKID_NONE_MAGIC {{ NULL }}
172
173/*
174 * tolerant FS - can share the same device with more filesystems (e.g. typical
175 * on CD-ROMs). We need this flag to detect ambivalent results (e.g. valid fat
176 * and valid linux swap on the same device).
177 */
178#define BLKID_IDINFO_TOLERANT (1 << 1)
179
180struct blkid_bufinfo {
181 unsigned char *data;
182 blkid_loff_t off;
183 blkid_loff_t len;
184 struct list_head bufs; /* list of buffers */
185};
186
187/*
188 * Low-level probing control struct
189 */
190struct blkid_struct_probe
191{
192 int fd; /* device file descriptor */
193 blkid_loff_t off; /* begin of data on the device */
194 blkid_loff_t size; /* end of data on the device */
195
196 dev_t devno; /* device number (st.st_rdev) */
197 dev_t disk_devno; /* devno of the whole-disk or 0 */
198 unsigned int blkssz; /* sector size (BLKSSZGET ioctl) */
199 mode_t mode; /* struct stat.sb_mode */
200
201 int flags; /* private libray flags */
202 int prob_flags; /* always zeroized by blkid_do_*() */
203
204 blkid_loff_t wipe_off; /* begin of the wiped area */
205 blkid_loff_t wipe_size; /* size of the wiped area */
206 struct blkid_chain *wipe_chain; /* superblock, partition, ... */
207
208 struct list_head buffers; /* list of buffers */
209
210 struct blkid_chain chains[BLKID_NCHAINS]; /* array of chains */
211 struct blkid_chain *cur_chain; /* current chain */
212
213 struct blkid_prval vals[BLKID_NVALS]; /* results */
214 int nvals; /* number of assigned vals */
215
216 struct blkid_struct_probe *parent; /* for clones */
217 struct blkid_struct_probe *disk_probe; /* whole-disk probing */
218};
219
220/* private flags library flags */
221#define BLKID_FL_PRIVATE_FD (1 << 1) /* see blkid_new_probe_from_filename() */
222#define BLKID_FL_TINY_DEV (1 << 2) /* <= 1.47MiB (floppy or so) */
223#define BLKID_FL_CDROM_DEV (1 << 3) /* is a CD/DVD drive */
224
225/* private per-probing flags */
226#define BLKID_PROBE_FL_IGNORE_PT (1 << 1) /* ignore partition table */
227#define BLKID_PROBE_FL_IGNORE_BACKUP (1 << 2) /* ignore backup superblocks or PT */
228
229extern int blkid_probe_ignore_backup(blkid_probe pr);
230
231extern blkid_probe blkid_clone_probe(blkid_probe parent);
232extern blkid_probe blkid_probe_get_wholedisk_probe(blkid_probe pr);
233
234/*
235 * Evaluation methods (for blkid_eval_* API)
236 */
237enum {
238 BLKID_EVAL_UDEV = 0,
239 BLKID_EVAL_SCAN,
240
241 __BLKID_EVAL_LAST
242};
243
244/*
245 * Library config options
246 */
247struct blkid_config {
248 int eval[__BLKID_EVAL_LAST]; /* array with EVALUATION=<udev,cache> options */
249 int nevals; /* number of elems in eval array */
250 int uevent; /* SEND_UEVENT=<yes|not> option */
251 char *cachefile; /* CACHE_FILE=<path> option */
252};
253
254extern struct blkid_config *blkid_read_config(const char *filename)
255 __ul_attribute__((warn_unused_result));
256extern void blkid_free_config(struct blkid_config *conf);
257
258/*
259 * Minimum number of seconds between device probes, even when reading
260 * from the cache. This is to avoid re-probing all devices which were
261 * just probed by another program that does not share the cache.
262 */
263#define BLKID_PROBE_MIN 2
264
265/*
266 * Time in seconds an entry remains verified in the in-memory cache
267 * before being reverified (in case of long-running processes that
268 * keep a cache in memory and continue to use it for a long time).
269 */
270#define BLKID_PROBE_INTERVAL 200
271
272/* This describes an entire blkid cache file and probed devices.
273 * We can traverse all of the found devices via bic_list.
274 * We can traverse all of the tag types by bic_tags, which hold empty tags
275 * for each tag type. Those tags can be used as list_heads for iterating
276 * through all devices with a specific tag type (e.g. LABEL).
277 */
278struct blkid_struct_cache
279{
280 struct list_head bic_devs; /* List head of all devices */
281 struct list_head bic_tags; /* List head of all tag types */
282 time_t bic_time; /* Last probe time */
283 time_t bic_ftime; /* Mod time of the cachefile */
284 unsigned int bic_flags; /* Status flags of the cache */
285 char *bic_filename; /* filename of cache */
286 blkid_probe probe; /* low-level probing stuff */
287};
288
289#define BLKID_BIC_FL_PROBED 0x0002 /* We probed /proc/partition devices */
290#define BLKID_BIC_FL_CHANGED 0x0004 /* Cache has changed from disk */
291
292/* config file */
293#define BLKID_CONFIG_FILE "/etc/blkid.conf"
294
295/* cache file on systemds with /run */
296#define BLKID_RUNTIME_TOPDIR "/run"
297#define BLKID_RUNTIME_DIR BLKID_RUNTIME_TOPDIR "/blkid"
298#define BLKID_CACHE_FILE BLKID_RUNTIME_DIR "/blkid.tab"
299
300/* old systems */
301#define BLKID_CACHE_FILE_OLD "/etc/blkid.tab"
302
303#define BLKID_ERR_IO 5
304#define BLKID_ERR_PROC 9
305#define BLKID_ERR_MEM 12
306#define BLKID_ERR_CACHE 14
307#define BLKID_ERR_DEV 19
308#define BLKID_ERR_PARAM 22
309#define BLKID_ERR_BIG 27
310
311/*
312 * Priority settings for different types of devices
313 */
314#define BLKID_PRI_UBI 50
315#define BLKID_PRI_DM 40
316#define BLKID_PRI_EVMS 30
317#define BLKID_PRI_LVM 20
318#define BLKID_PRI_MD 10
319
320#if defined(TEST_PROGRAM) && !defined(CONFIG_BLKID_DEBUG)
321#define CONFIG_BLKID_DEBUG
322#endif
323
324#define DEBUG_CACHE 0x0001
325#define DEBUG_DUMP 0x0002
326#define DEBUG_DEV 0x0004
327#define DEBUG_DEVNAME 0x0008
328#define DEBUG_DEVNO 0x0010
329#define DEBUG_PROBE 0x0020
330#define DEBUG_READ 0x0040
331#define DEBUG_RESOLVE 0x0080
332#define DEBUG_SAVE 0x0100
333#define DEBUG_TAG 0x0200
334#define DEBUG_LOWPROBE 0x0400
335#define DEBUG_CONFIG 0x0800
336#define DEBUG_EVALUATE 0x1000
337#define DEBUG_INIT 0x8000
338#define DEBUG_ALL 0xFFFF
339
340#ifdef CONFIG_BLKID_DEBUG
341extern int blkid_debug_mask;
342extern void blkid_init_debug(int mask);
343extern void blkid_debug_dump_dev(blkid_dev dev);
344extern void blkid_debug_dump_tag(blkid_tag tag);
345
346#define DBG(m,x) do { if ((m) & blkid_debug_mask) x; } while (0)
347
348#else /* !CONFIG_BLKID_DEBUG */
349#define DBG(m,x)
350#define blkid_init_debug(x)
351#endif /* CONFIG_BLKID_DEBUG */
352
353/* devno.c */
354struct dir_list {
355 char *name;
356 struct dir_list *next;
357};
358extern void blkid__scan_dir(char *, dev_t, struct dir_list **, char **)
359 __attribute__((nonnull(1,4)));
360extern int blkid_driver_has_major(const char *drvname, int major)
361 __attribute__((warn_unused_result));
362
363/* lseek.c */
364extern blkid_loff_t blkid_llseek(int fd, blkid_loff_t offset, int whence);
365
366/* read.c */
367extern void blkid_read_cache(blkid_cache cache)
368 __attribute__((nonnull));
369
370/* save.c */
371extern int blkid_flush_cache(blkid_cache cache)
372 __attribute__((nonnull));
373
374/* cache */
375extern char *blkid_safe_getenv(const char *arg)
376 __attribute__((nonnull))
377 __attribute__((warn_unused_result));
378
379extern char *blkid_get_cache_filename(struct blkid_config *conf)
380 __attribute__((warn_unused_result));
381/*
382 * Functions to create and find a specific tag type: tag.c
383 */
384extern void blkid_free_tag(blkid_tag tag);
385extern blkid_tag blkid_find_tag_dev(blkid_dev dev, const char *type)
386 __attribute__((nonnull))
387 __attribute__((warn_unused_result));
388
389extern int blkid_set_tag(blkid_dev dev, const char *name,
390 const char *value, const int vlength)
391 __attribute__((nonnull(1,2)));
392
393/*
394 * Functions to create and find a specific tag type: dev.c
395 */
396extern blkid_dev blkid_new_dev(void)
397 __attribute__((warn_unused_result));
398extern void blkid_free_dev(blkid_dev dev);
399
400/* probe.c */
401extern int blkid_probe_is_tiny(blkid_probe pr)
402 __attribute__((nonnull))
403 __attribute__((warn_unused_result));
404extern int blkid_probe_is_cdrom(blkid_probe pr)
405 __attribute__((nonnull))
406 __attribute__((warn_unused_result));
407
408extern unsigned char *blkid_probe_get_buffer(blkid_probe pr,
409 blkid_loff_t off, blkid_loff_t len)
410 __attribute__((nonnull))
411 __attribute__((warn_unused_result));
412
413extern unsigned char *blkid_probe_get_sector(blkid_probe pr, unsigned int sector)
414 __attribute__((nonnull))
415 __attribute__((warn_unused_result));
416
417extern int blkid_probe_get_dimension(blkid_probe pr,
418 blkid_loff_t *off, blkid_loff_t *size)
419 __attribute__((nonnull));
420
421extern int blkid_probe_set_dimension(blkid_probe pr,
422 blkid_loff_t off, blkid_loff_t size)
423 __attribute__((nonnull));
424
425extern int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id,
426 blkid_loff_t *offset, const struct blkid_idmag **res)
427 __attribute__((nonnull(1)));
428
429/* returns superblok according to 'struct blkid_idmag' */
430#define blkid_probe_get_sb(_pr, _mag, type) \
431 ((type *) blkid_probe_get_buffer((_pr),\
432 (_mag)->kboff << 10, sizeof(type)))
433
434extern blkid_partlist blkid_probe_get_partlist(blkid_probe pr)
435 __attribute__((nonnull))
436 __attribute__((warn_unused_result));
437
438extern int blkid_probe_is_covered_by_pt(blkid_probe pr,
439 blkid_loff_t offset, blkid_loff_t size)
440 __attribute__((warn_unused_result));
441
442extern void blkid_probe_chain_reset_vals(blkid_probe pr, struct blkid_chain *chn)
443 __attribute__((nonnull));
444extern int blkid_probe_chain_copy_vals(blkid_probe pr,
445 struct blkid_chain *chn,
446 struct blkid_prval *vals,
447 int nvals)
448 __attribute__((nonnull));
449
450extern struct blkid_prval *blkid_probe_assign_value(blkid_probe pr,
451 const char *name)
452 __attribute__((nonnull))
453 __attribute__((warn_unused_result));
454
455extern int blkid_probe_reset_last_value(blkid_probe pr)
456 __attribute__((nonnull));
457extern void blkid_probe_append_vals(blkid_probe pr,
458 struct blkid_prval *vals,
459 int nvals)
460 __attribute__((nonnull));
461
462extern struct blkid_chain *blkid_probe_get_chain(blkid_probe pr)
463 __attribute__((nonnull))
464 __attribute__((warn_unused_result));
465
466extern struct blkid_prval *__blkid_probe_get_value(blkid_probe pr, int num)
467 __attribute__((nonnull))
468 __attribute__((warn_unused_result));
469
470extern struct blkid_prval *__blkid_probe_lookup_value(blkid_probe pr, const char *name)
471 __attribute__((nonnull))
472 __attribute__((warn_unused_result));
473
474extern unsigned long *blkid_probe_get_filter(blkid_probe pr, int chain, int create)
475 __attribute__((nonnull))
476 __attribute__((warn_unused_result));
477
478extern int __blkid_probe_invert_filter(blkid_probe pr, int chain)
479 __attribute__((nonnull));
480extern int __blkid_probe_reset_filter(blkid_probe pr, int chain)
481 __attribute__((nonnull));
482extern int __blkid_probe_filter_types(blkid_probe pr, int chain, int flag, char *names[])
483 __attribute__((nonnull));
484
485extern void *blkid_probe_get_binary_data(blkid_probe pr, struct blkid_chain *chn)
486 __attribute__((nonnull))
487 __attribute__((warn_unused_result));
488
489extern int blkid_probe_set_value(blkid_probe pr, const char *name,
490 unsigned char *data, size_t len)
491 __attribute__((nonnull));
492
493extern int blkid_probe_vsprintf_value(blkid_probe pr, const char *name,
494 const char *fmt, va_list ap)
495 __attribute__((nonnull));
496
497extern int blkid_probe_sprintf_value(blkid_probe pr, const char *name,
498 const char *fmt, ...)
499 __attribute__((nonnull))
500 __attribute__ ((__format__ (__printf__, 3, 4)));
501
502extern int blkid_probe_set_magic(blkid_probe pr, blkid_loff_t offset,
503 size_t len, unsigned char *magic)
504 __attribute__((nonnull));
505
506extern void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len)
507 __attribute__((nonnull));
508extern size_t blkid_rtrim_whitespace(unsigned char *str)
509 __attribute__((nonnull));
510extern size_t blkid_ltrim_whitespace(unsigned char *str)
511 __attribute__((nonnull));
512
513extern void blkid_probe_set_wiper(blkid_probe pr, blkid_loff_t off,
514 blkid_loff_t size)
515 __attribute__((nonnull));
516extern int blkid_probe_is_wiped(blkid_probe pr, struct blkid_chain **chn,
517 blkid_loff_t off, blkid_loff_t size)
518 __attribute__((nonnull))
519 __attribute__((warn_unused_result));
520extern void blkid_probe_use_wiper(blkid_probe pr, blkid_loff_t off, blkid_loff_t size)
521 __attribute__((nonnull));
522
523/* filter bitmap macros */
524#define blkid_bmp_wordsize (8 * sizeof(unsigned long))
525#define blkid_bmp_idx_bit(item) (1UL << ((item) % blkid_bmp_wordsize))
526#define blkid_bmp_idx_byte(item) ((item) / blkid_bmp_wordsize)
527
528#define blkid_bmp_set_item(bmp, item) \
529 ((bmp)[ blkid_bmp_idx_byte(item) ] |= blkid_bmp_idx_bit(item))
530
531#define blkid_bmp_unset_item(bmp, item) \
532 ((bmp)[ blkid_bmp_idx_byte(item) ] &= ~blkid_bmp_idx_bit(item))
533
534#define blkid_bmp_get_item(bmp, item) \
535 ((bmp)[ blkid_bmp_idx_byte(item) ] & blkid_bmp_idx_bit(item))
536
537#define blkid_bmp_nwords(max_items) \
538 (((max_items) + blkid_bmp_wordsize) / blkid_bmp_wordsize)
539
540#define blkid_bmp_nbytes(max_items) \
541 (blkid_bmp_nwords(max_items) * sizeof(unsigned long))
542
543/* encode.c */
544extern size_t blkid_encode_to_utf8(int enc, unsigned char *dest, size_t len,
545 const unsigned char *src, size_t count)
546 __attribute__((nonnull));
547
548#define BLKID_ENC_UTF16BE 0
549#define BLKID_ENC_UTF16LE 1
550
551#endif /* _BLKID_BLKIDP_H */