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