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