From: Karel Zak Date: Sat, 6 Sep 2008 13:28:40 +0000 (+0200) Subject: blkid: add low level probing API X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=51410fc6deb29cae54a2669aafabae6c49f964fc;p=util-linux blkid: add low level probing API (sorry from the huge patch..) Signed-off-by: Karel Zak --- diff --git a/config/include-Makefile.am b/config/include-Makefile.am index f9c2409d..876fa396 100644 --- a/config/include-Makefile.am +++ b/config/include-Makefile.am @@ -5,6 +5,7 @@ usrsbinexecdir = ${exec_prefix}/sbin AM_CPPFLAGS = -include $(top_builddir)/config.h -I$(top_srcdir)/include \ -DLOCALEDIR=\"$(localedir)\" AM_CFLAGS = -fsigned-char +AM_LDFLAGS = # Automake (at least up to 1.10) mishandles dist_man_MANS inside conditionals. # Unlike with other dist primaries, the files are not distributed if the diff --git a/configure.ac b/configure.ac index 490e84f3..4186d40d 100644 --- a/configure.ac +++ b/configure.ac @@ -792,9 +792,12 @@ lib/Makefile libs/Makefile libs/blkid/blkid.pc libs/blkid/libblkid.3 +libs/blkid/blkid.8 libs/blkid/Makefile +libs/blkid/bin/Makefile libs/blkid/src/blkid_types.h libs/blkid/src/Makefile +libs/blkid/src/probers/Makefile login-utils/Makefile misc-utils/Makefile mount/Makefile diff --git a/libs/blkid/.gitignore b/libs/blkid/.gitignore index 96ecde5b..e7334150 100644 --- a/libs/blkid/.gitignore +++ b/libs/blkid/.gitignore @@ -1,2 +1,3 @@ -*.3 +*.[38] blkid_types.h +*.sh diff --git a/libs/blkid/Makefile.am b/libs/blkid/Makefile.am index 05ab8daa..fdf89feb 100644 --- a/libs/blkid/Makefile.am +++ b/libs/blkid/Makefile.am @@ -1,12 +1,12 @@ include $(top_srcdir)/config/include-Makefile.am -SUBDIRS = src +SUBDIRS = src bin # pkg-config stuff pkgconfigdir = $(usrlibexecdir)/pkgconfig pkgconfig_DATA = blkid.pc -dist_man_MANS = libblkid.3 +dist_man_MANS = libblkid.3 blkid.8 -EXTRA_DIST = README.blkid blkid.pc blkid.pc.in libblkid.3.in +EXTRA_DIST = README.blkid blkid.pc blkid.pc.in libblkid.3.in blkid.8.in diff --git a/libs/blkid/bin/Makefile.am b/libs/blkid/bin/Makefile.am new file mode 100644 index 00000000..bcb6da4a --- /dev/null +++ b/libs/blkid/bin/Makefile.am @@ -0,0 +1,18 @@ +include $(top_srcdir)/config/include-Makefile.am + +AM_LDFLAGS += -L$(top_builddir)/libs/bkid/src -lblkid +AM_CPPFLAGS += -I$(top_builddir)/libs/blkid/src/ + +sbin_PROGRAMS = blkid + +shell_in_files = blkid.sh.in +noinst_SCRIPTS = $(shell_in_files:.sh.in=.sh) + +blkid_SOURCES = blkid.c + +CLEANFILES = $(noinst_SCRIPTS) + +%.sh: %.sh.in + sed -e "s|\@SRCDIR\@|$(abs_top_srcdir)|" $< > $@ + chmod +x $@ + diff --git a/libs/blkid/bin/blkid.c b/libs/blkid/bin/blkid.c new file mode 100644 index 00000000..51d3b348 --- /dev/null +++ b/libs/blkid/bin/blkid.c @@ -0,0 +1,432 @@ +/* + * blkid.c - User command-line interface for libblkid + * + * Copyright (C) 2001 Andreas Dilger + * + * %Begin-Header% + * This file may be redistributed under the terms of the + * GNU Lesser General Public License. + * %End-Header% + */ + +#include +#include +#include +#include +#ifdef HAVE_TERMIOS_H +#include +#endif +#ifdef HAVE_TERMIO_H +#include +#endif +#ifdef HAVE_SYS_IOCTL_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#else +extern int getopt(int argc, char * const argv[], const char *optstring); +extern char *optarg; +extern int optind; +#endif + +#define OUTPUT_VALUE_ONLY 0x0001 +#define OUTPUT_DEVICE_ONLY 0x0002 +#define OUTPUT_PRETTY_LIST 0x0004 + +#include + +const char *progname = "blkid"; + +static void print_version(FILE *out) +{ + fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE); +} + +static void usage(int error) +{ + FILE *out = error ? stderr : stdout; + + print_version(out); + fprintf(out, + "usage:\t%s [-c ] [-ghlLv] [-o format] " + "[-s ] [-t ]\n [-w ] [dev ...]\n" + "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n" + "\t-h\tprint this usage message and exit\n" + "\t-g\tgarbage collect the blkid cache\n" + "\t-s\tshow specified tag(s) (default show all tags)\n" + "\t-t\tfind device with a specific token (NAME=value pair)\n" + "\t-l\tlookup the the first device with arguments specified by -t\n" + "\t-v\tprint version and exit\n" + "\t-w\twrite cache to different file (/dev/null = no write)\n" + "\tdev\tspecify device(s) to probe (default: all devices)\n", + progname); + exit(error); +} + +/* + * This function does "safe" printing. It will convert non-printable + * ASCII characters using '^' and M- notation. + */ +static void safe_print(const char *cp, int len) +{ + unsigned char ch; + + if (len < 0) + len = strlen(cp); + + while (len--) { + ch = *cp++; + if (ch > 128) { + fputs("M-", stdout); + ch -= 128; + } + if ((ch < 32) || (ch == 0x7f)) { + fputc('^', stdout); + ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */ + } + fputc(ch, stdout); + } +} + +static int get_terminal_width(void) +{ +#ifdef TIOCGSIZE + struct ttysize t_win; +#endif +#ifdef TIOCGWINSZ + struct winsize w_win; +#endif + const char *cp; + +#ifdef TIOCGSIZE + if (ioctl (0, TIOCGSIZE, &t_win) == 0) + return (t_win.ts_cols); +#endif +#ifdef TIOCGWINSZ + if (ioctl (0, TIOCGWINSZ, &w_win) == 0) + return (w_win.ws_col); +#endif + cp = getenv("COLUMNS"); + if (cp) + return strtol(cp, NULL, 10); + return 80; +} + +static int pretty_print_word(const char *str, int max_len, + int left_len, int overflow_nl) +{ + int len = strlen(str) + left_len; + int ret = 0; + + fputs(str, stdout); + if (overflow_nl && len > max_len) { + fputc('\n', stdout); + len = 0; + } else if (len > max_len) + ret = len - max_len; + do + fputc(' ', stdout); + while (len++ < max_len); + return ret; +} + +static void pretty_print_line(const char *device, const char *fs_type, + const char *label, const char *mtpt, + const char *uuid) +{ + static int device_len = 10, fs_type_len = 7; + static int label_len = 8, mtpt_len = 14; + static int term_width = -1; + int len, w; + + if (term_width < 0) + term_width = get_terminal_width(); + + if (term_width > 80) { + term_width -= 80; + w = term_width / 10; + if (w > 8) + w = 8; + term_width -= 2*w; + label_len += w; + fs_type_len += w; + w = term_width/2; + device_len += w; + mtpt_len +=w; + } + + len = pretty_print_word(device, device_len, 0, 1); + len = pretty_print_word(fs_type, fs_type_len, len, 0); + len = pretty_print_word(label, label_len, len, 0); + len = pretty_print_word(mtpt, mtpt_len, len, 0); + fputs(uuid, stdout); + fputc('\n', stdout); +} + +static void pretty_print_dev(blkid_dev dev) +{ + fprintf(stderr, "pretty print not implemented yet\n"); + +#ifdef NOT_IMPLEMENTED + blkid_tag_iterate iter; + const char *type, *value, *devname; + const char *uuid = "", *fs_type = "", *label = ""; + char *cp; + int len, mount_flags; + char mtpt[80]; + errcode_t retval; + + if (dev == NULL) { + pretty_print_line("device", "fs_type", "label", + "mount point", "UUID"); + for (len=get_terminal_width()-1; len > 0; len--) + fputc('-', stdout); + fputc('\n', stdout); + return; + } + + devname = blkid_dev_devname(dev); + if (access(devname, F_OK)) + return; + + /* Get the uuid, label, type */ + iter = blkid_tag_iterate_begin(dev); + while (blkid_tag_next(iter, &type, &value) == 0) { + if (!strcmp(type, "UUID")) + uuid = value; + if (!strcmp(type, "TYPE")) + fs_type = value; + if (!strcmp(type, "LABEL")) + label = value; + } + blkid_tag_iterate_end(iter); + + /* Get the mount point */ + mtpt[0] = 0; + retval = ext2fs_check_mount_point(devname, &mount_flags, + mtpt, sizeof(mtpt)); + if (retval == 0) { + if (mount_flags & EXT2_MF_MOUNTED) { + if (!mtpt[0]) + strcpy(mtpt, "(mounted, mtpt unknown)"); + } else if (mount_flags & EXT2_MF_BUSY) + strcpy(mtpt, "(in use)"); + else + strcpy(mtpt, "(not mounted)"); + } + + pretty_print_line(devname, fs_type, label, mtpt, uuid); +#endif +} + +static void print_tags(blkid_dev dev, char *show[], int numtag, int output) +{ + blkid_tag_iterate iter; + const char *type, *value; + int i, first = 1; + + if (!dev) + return; + + if (output & OUTPUT_PRETTY_LIST) { + pretty_print_dev(dev); + return; + } + + if (output & OUTPUT_DEVICE_ONLY) { + printf("%s\n", blkid_dev_devname(dev)); + return; + } + + iter = blkid_tag_iterate_begin(dev); + while (blkid_tag_next(iter, &type, &value) == 0) { + if (numtag && show) { + for (i=0; i < numtag; i++) + if (!strcmp(type, show[i])) + break; + if (i >= numtag) + continue; + } + if (output & OUTPUT_VALUE_ONLY) { + fputs(value, stdout); + fputc('\n', stdout); + } else { + if (first) { + printf("%s: ", blkid_dev_devname(dev)); + first = 0; + } + fputs(type, stdout); + fputs("=\"", stdout); + safe_print(value, -1); + fputs("\" ", stdout); + } + } + blkid_tag_iterate_end(iter); + + if (!first && !(output & OUTPUT_VALUE_ONLY)) + printf("\n"); +} + +int main(int argc, char **argv) +{ + blkid_cache cache = NULL; + char *devices[128] = { NULL, }; + char *show[128] = { NULL, }; + char *search_type = NULL, *search_value = NULL; + char *read = NULL; + char *write = NULL; + unsigned int numdev = 0, numtag = 0; + int version = 0; + int err = 4; + unsigned int i; + int output_format = 0; + int lookup = 0, gc = 0; + int c; + + while ((c = getopt (argc, argv, "c:f:ghlLo:s:t:w:v")) != EOF) + switch (c) { + case 'c': + if (optarg && !*optarg) + read = NULL; + else + read = optarg; + if (!write) + write = read; + break; + case 'l': + lookup++; + break; + case 'L': + output_format = OUTPUT_PRETTY_LIST; + break; + case 'g': + gc = 1; + break; + case 'o': + if (!strcmp(optarg, "value")) + output_format = OUTPUT_VALUE_ONLY; + else if (!strcmp(optarg, "device")) + output_format = OUTPUT_DEVICE_ONLY; + else if (!strcmp(optarg, "list")) + output_format = OUTPUT_PRETTY_LIST; + else if (!strcmp(optarg, "full")) + output_format = 0; + else { + fprintf(stderr, "Invalid output format %s. " + "Choose from value,\n\t" + "device, list, or full\n", optarg); + exit(1); + } + break; + case 's': + if (numtag >= sizeof(show) / sizeof(*show)) { + fprintf(stderr, "Too many tags specified\n"); + usage(err); + } + show[numtag++] = optarg; + break; + case 't': + if (search_type) { + fprintf(stderr, "Can only search for " + "one NAME=value pair\n"); + usage(err); + } + if (blkid_parse_tag_string(optarg, + &search_type, + &search_value)) { + fprintf(stderr, "-t needs NAME=value pair\n"); + usage(err); + } + break; + case 'v': + version = 1; + break; + case 'w': + if (optarg && !*optarg) + write = NULL; + else + write = optarg; + break; + case 'h': + err = 0; + default: + usage(err); + } + + while (optind < argc) + devices[numdev++] = argv[optind++]; + + if (version) { + print_version(stdout); + goto exit; + } + + if (blkid_get_cache(&cache, read) < 0) + goto exit; + + err = 2; + if (gc) { + blkid_gc_cache(cache); + goto exit; + } + if (output_format & OUTPUT_PRETTY_LIST) + pretty_print_dev(NULL); + + if (lookup) { + blkid_dev dev; + + if (!search_type) { + fprintf(stderr, "The lookup option requires a " + "search type specified using -t\n"); + exit(1); + } + /* Load any additional devices not in the cache */ + for (i = 0; i < numdev; i++) + blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL); + + if ((dev = blkid_find_dev_with_tag(cache, search_type, + search_value))) { + print_tags(dev, show, numtag, output_format); + err = 0; + } + /* If we didn't specify a single device, show all available devices */ + } else if (!numdev) { + blkid_dev_iterate iter; + blkid_dev dev; + + blkid_probe_all(cache); + + iter = blkid_dev_iterate_begin(cache); + blkid_dev_set_search(iter, search_type, search_value); + while (blkid_dev_next(iter, &dev) == 0) { + dev = blkid_verify(cache, dev); + if (!dev) + continue; + print_tags(dev, show, numtag, output_format); + err = 0; + } + blkid_dev_iterate_end(iter); + /* Add all specified devices to cache (optionally display tags) */ + } else for (i = 0; i < numdev; i++) { + blkid_dev dev = blkid_get_dev(cache, devices[i], + BLKID_DEV_NORMAL); + + if (dev) { + if (search_type && + !blkid_dev_has_tag(dev, search_type, + search_value)) + continue; + print_tags(dev, show, numtag, output_format); + err = 0; + } + } + +exit: + if (search_type) + free(search_type); + if (search_value) + free(search_value); + blkid_put_cache(cache); + return err; +} diff --git a/libs/blkid/bin/blkid.sh.in b/libs/blkid/bin/blkid.sh.in new file mode 100644 index 00000000..3e10add1 --- /dev/null +++ b/libs/blkid/bin/blkid.sh.in @@ -0,0 +1,10 @@ +#!/bin/bash + +# libtool? Don't ask! + +BLKIDDIR=@SRCDIR@/libs/blkid +BINARY=$BLKIDDIR/bin/blkid +LD_LIBRARY_PATH=$BLKIDDIR/src/ + +export LD_LIBRARY_PATH +exec $BINARY $@ diff --git a/libs/blkid/blkid.8.in b/libs/blkid/blkid.8.in new file mode 100644 index 00000000..9c42686e --- /dev/null +++ b/libs/blkid/blkid.8.in @@ -0,0 +1,164 @@ +.\" Copyright 2000 Andreas Dilger (adilger@turbolinux.com) +.\" +.\" This man page was created for blkid from e2fsprogs-1.25. +.\" +.\" This file may be copied under the terms of the GNU Public License. +.\" +.\" Based on uuidgen, Mon Sep 17 10:42:12 2000, Andreas Dilger +.TH BLKID 8 "@E2FSPROGS_MONTH@ @E2FSPROGS_YEAR@" "E2fsprogs version @E2FSPROGS_VERSION@" +.SH NAME +blkid \- command\-line utility to locate/print block device attributes +.SH SYNOPSIS +.B blkid +[ +.B \-ghlLv +] +[ +[ +.B \-c +.I cachefile +] +.B \-w +.I writecachefile +] +[ +.B \-o +.I format +] +[ +.B \-s +.I tag +] +[ +.B \-t +.IR NAME = value +] +[ +.I device ... +] +.SH DESCRIPTION +The +.B blkid +program is the command-line interface to working with +.BR libblkid (3) +library. It can determine the type of content (e.g. filesystem, swap) +a block device holds, and also attributes (tokens, NAME=value pairs) +from the content metadata (e.g. LABEL or UUID fields). +.PP +.B blkid +has two main forms of operation: either searching for a device with a +specific NAME=value pair, or displaying NAME=value pairs for one or +more devices. +.SH OPTIONS +.TP +.BI \-c " cachefile" +Read from +.I cachefile +instead of reading from the default cache file +.IR /etc/blkid.tab . +If you want to start with a clean cache (i.e. don't report devices previously +scanned but not necessarily available at this time), specify +.IR /dev/null . +.TP +.B \-g +Perform a garbage collection pass on the blkid cache to remove +devices which no longer exist. +.TP +.B \-h +Display a usage message and exit. +.TP +.B \-l +Look up one device that matches the search parameter specified using +the +.B \-t +option. If there are multiple devices that match the specified search +parameter, then the device with the highest priority is returned, and/or +the first device found at a given priority. Device types in order of +decreasing priority are Device Mapper, EVMS, LVM, MD, and finally regular +block devices. If this option is not specified, +.B blkid +will print all of the devices that match the search parameter. +.TP +.BI \-o " format" +Display +.BR blkid 's +output using the specified format. The +.I format +parameter may be +.I full +(the default), +.I value +(only print the value of the tags), +.I list +(print the devices in a user-friendly format), +or +.I device +(only print the device name). +.TP +.B \-L +Print the devices in a user-friendly list format. This is the +equivalent of using the option \fB-o list\fR. +.TP +.BI \-s " tag" +For each (specified) device, show only the tags that match +.IR tag . +It is possible to specify multiple +.B \-s +options. If no tag is specified, then all tokens are shown for all +(specified) devices. +In order to just refresh the cache without showing any tokens, use +.B "-s none" +with no other options. +.TP +.BI \-t " NAME" = "value" +Search for block devices with tokens named +.I NAME +that have the value +.IR value , +and display any devices which are found. +Common values for +.I NAME +include +.BR TYPE , +.BR LABEL , +and +.BR UUID . +If there are no devices specified on the command line, all block devices +will be searched; otherwise only the specified devices are searched. +.TP +.B \-v +Display version number and exit. +.TP +.BI \-w " writecachefile" +Write the device cache to +.I writecachefile +instead of writing it to the default cache file +.IR /etc/blkid.tab . +If you don't want to save the cache to the default file, specify +.IR /dev/null. +If not specified it will be the same file as that given by the +.B \-c +option. +.TP +.I device +Display tokens from only the specified device. It is possible to +give multiple +.I device +options on the command line. If none is given, all devices which +appear in +.I /proc/partitions +are shown, if they are recognized. +.SH "RETURN CODE" +If the specified token was found, or if any tags were shown from (specified) +devices, 0 is returned. If the specified token was not found, or no +(specified) devices could be identified, an exit code of 2 is returned. +For usage or other errors, an exit code of 4 is returned. +.SH AUTHOR +.B blkid +was written by Andreas Dilger for libblkid. +.SH AVAILABILITY +.B blkid +is part the e2fsprogs package since version 1.26 and is available from +http://e2fsprogs.sourceforge.net. +.SH "SEE ALSO" +.BR libblkid (3) diff --git a/libs/blkid/src/Makefile.am b/libs/blkid/src/Makefile.am index 78416938..e9b782fd 100644 --- a/libs/blkid/src/Makefile.am +++ b/libs/blkid/src/Makefile.am @@ -1,12 +1,17 @@ include $(top_srcdir)/config/include-Makefile.am -AM_CPPFLAGS += -I$(top_builddir)/blkid/src +SUBDIRS = probers . + +AM_CPPFLAGS += -I$(top_builddir)/libs/blkid/src + +probers = prober-cramfs.c # static library lib_LIBRARIES = libblkid.a libblkid_a_SOURCES = cache.c dev.c devname.c devno.c getsize.c llseek.c \ - probe.c read.c resolve.c save.c tag.c version.c \ - blkid.h blkidP.h list.h blkid_types.h + probe.c read.c resolve.c save.c tag.c version.c verify.c \ + blkid.h blkidP.h list.h blkid_types.h probers/probers.h +libblkid_a_LIBADD = probers/libprobers.a libblkid_a_CFLAGS = -fPIC # shared library (note that we don't use LIBTOOL!) @@ -15,9 +20,13 @@ blkid_LDSCRIPT = $(srcdir)/blkid.sym blkid_LIB = $(blkid_IMAGE).$(BLKID_VERSION) blkid_SONAME = $(blkid_IMAGE).$(BLKID_VERSION_MAJOR) blkid_LINKS = $(blkid_IMAGE) $(blkid_SONAME) +blkid_OTHERLDADD = if HAVE_DEVMAPPER -blkid_OTHERLDADD = $(DEVMAPPER_LIBS) +blkid_OTHERLDADD += $(DEVMAPPER_LIBS) +endif +if HAVE_UUID +blkid_OTHERLDADD += -luuid #TODO $(UUID_LIBS) endif EXTRA_DIST = blkid.sym blkid_types.h.in @@ -27,7 +36,7 @@ all-local: $(blkid_LIB) $(blkid_LIB): $(lib_LIBRARIES) $(blkid_LDSCRIPT) $(CC) --shared -o $(blkid_LIB) $(AM_LDFLAGS) \ -Wl,-soname,$(blkid_SONAME),--version-script,$(blkid_LDSCRIPT) \ - $(libblkid_a_OBJECTS) $(blkid_OTHERLDADD) + $(libblkid_a_OBJECTS) probers/libprobers.a $(blkid_OTHERLDADD) for I in $(blkid_LINKS); do \ ln -sf $(blkid_LIB) $$I; \ done diff --git a/libs/blkid/src/blkid.h b/libs/blkid/src/blkid.h index 4582879b..9691a162 100644 --- a/libs/blkid/src/blkid.h +++ b/libs/blkid/src/blkid.h @@ -22,6 +22,8 @@ extern "C" { typedef struct blkid_struct_dev *blkid_dev; typedef struct blkid_struct_cache *blkid_cache; +typedef struct blkid_struct_probe *blkid_probe; + typedef __s64 blkid_loff_t; typedef struct blkid_struct_tag_iterate *blkid_tag_iterate; @@ -70,8 +72,7 @@ extern blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, /* getsize.c */ extern blkid_loff_t blkid_get_dev_size(int fd); -/* probe.c */ -int blkid_known_fstype(const char *fstype); +/* verify.c */ extern blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev); /* read.c */ @@ -100,6 +101,50 @@ extern int blkid_parse_version_string(const char *ver_string); extern int blkid_get_library_version(const char **ver_string, const char **date_string); + + +/* probe.c */ +extern int blkid_known_fstype(const char *fstype); +extern blkid_probe blkid_new_probe(void); +extern void blkid_free_probe(blkid_probe pr); +extern void blkid_reset_probe(blkid_probe pr); + +extern int blkid_probe_set_device(blkid_probe pr, int fd, + blkid_loff_t off, blkid_loff_t size); + +#define BLKID_PROBREQ_LABEL (1 << 1) +#define BLKID_PROBREQ_LABELRAW (1 << 2) +#define BLKID_PROBREQ_UUID (1 << 3) +#define BLKID_PROBREQ_UUIDRAW (1 << 4) +#define BLKID_PROBREQ_TYPE (1 << 5) +#define BLKID_PROBREQ_SECTYPE (1 << 6) +#define BLKID_PROBREQ_USAGE (1 << 7) +#define BLKID_PROBREQ_VERSION (1 << 8) +extern int blkid_probe_set_request(blkid_probe pr, int flags); + +#define BLKID_USAGE_FILESYSTEM (1 << 1) +#define BLKID_USAGE_RAID (1 << 2) +#define BLKID_USAGE_CRYPTO (1 << 3) +#define BLKID_USAGE_OTHER (1 << 4) +extern int blkid_probe_filter_usage(blkid_probe pr, int flag, int usage); + +#define BLKID_FLTR_NOTIN 1 +#define BLKID_FLTR_ONLYIN 2 +extern int blkid_probe_filter_types(blkid_probe pr, + int flag, char *names[]); + + +extern int blkid_probe_invert_filter(blkid_probe pr); +extern int blkid_probe_reset_filter(blkid_probe pr); + +extern int blkid_do_probe(blkid_probe pr); +extern int blkid_probe_numof_values(blkid_probe pr); +extern int blkid_probe_get_value(blkid_probe pr, int num, const char **name, + unsigned char **data, size_t *len); +extern int blkid_probe_lookup_value(blkid_probe pr, const char *name, + unsigned char **data, size_t *len); +extern int blkid_probe_has_value(blkid_probe pr, const char *name); + #ifdef __cplusplus } #endif diff --git a/libs/blkid/src/blkid.sym b/libs/blkid/src/blkid.sym index 3687ae0b..51bdde97 100644 --- a/libs/blkid/src/blkid.sym +++ b/libs/blkid/src/blkid.sym @@ -6,7 +6,9 @@ blkid_dev_next; blkid_devno_to_devname; blkid_dev_set_search; + blkid_do_probe; blkid_find_dev_with_tag; + blkid_free_probe; blkid_gc_cache; blkid_get_cache; blkid_get_dev; @@ -15,11 +17,23 @@ blkid_get_library_version; blkid_get_tag_value; blkid_known_fstype; + blkid_new_probe; blkid_parse_tag_string; blkid_parse_version_string; blkid_probe_all; blkid_probe_all_new; + blkid_probe_filter_types; + blkid_probe_filter_usage; + blkid_probe_get_value; + blkid_probe_has_value; + blkid_probe_invert_filter; + blkid_probe_lookup_value; + blkid_probe_numof_values; + blkid_probe_reset_filter; + blkid_probe_set_device; + blkid_probe_set_request; blkid_put_cache; + blkid_reset_probe; blkid_tag_iterate_begin; blkid_tag_iterate_end; blkid_tag_next; diff --git a/libs/blkid/src/blkidP.h b/libs/blkid/src/blkidP.h index 7ac2b82f..f81a4399 100644 --- a/libs/blkid/src/blkidP.h +++ b/libs/blkid/src/blkidP.h @@ -16,8 +16,8 @@ #include #include -#include -#include +#include "blkid.h" +#include "list.h" #ifdef __GNUC__ #define __BLKID_ATTR(x) __attribute__(x) @@ -67,6 +67,73 @@ struct blkid_struct_tag }; typedef struct blkid_struct_tag *blkid_tag; +/* + * Low-level probe result + */ +#define BLKID_PROBVAL_BUFSIZ 64 +#define BLKID_PROBVAL_NVALS 8 /* see blkid.h BLKID_PROBREQ_* */ + +struct blkid_prval +{ + const char *name; /* value name */ + unsigned char data[BLKID_PROBVAL_BUFSIZ]; /* value data */ + size_t len; /* length of value data */ +}; + +/* + * Low-level probing control struct + */ +struct blkid_struct_probe +{ + int fd; /* device file descriptor */ + blkid_loff_t off; /* begin of data on the device */ + blkid_loff_t size; /* end of data on the device */ + + unsigned char *sbbuf; /* superblok buffer */ + size_t sbbuf_len; /* size of data in superblock buffer */ + + unsigned char *buf; /* seek buffer */ + off_t buf_off; /* offset of seek buffer */ + size_t buf_len; /* size of data in seek buffer */ + size_t buf_max; /* allocated size of seek buffer */ + + struct blkid_prval vals[BLKID_PROBVAL_NVALS]; + int nvals; + + int probreq; /* BLKID_PROBREQ_* flags */ + + unsigned long *fltr; /* filter */ +}; + +#define BLKID_SB_BUFSIZ 0x11000 + +/* + * Filesystem / Raid magic strings + */ +struct blkid_idmag +{ + const char *magic; /* magic string */ + unsigned len; /* length of magic */ + + long kboff; /* kilobyte offset of superblock */ + unsigned sboff; /* byte offset within superblock */ +}; + +/* + * Filesystem / Raid description + */ +struct blkid_idinfo +{ + const char *name; /* FS/RAID name */ + const char *secname; /* second FS/RAID name */ + int usage; /* BLKID_USAGE_* flag */ + + /* probe function */ + int (*probefunc)(blkid_probe pr, const struct blkid_idmag *mag); + + struct blkid_idmag magics[]; /* NULL or array with magic strings */ +}; + /* * Minimum number of seconds between device probes, even when reading * from the cache. This is to avoid re-probing all devices which were @@ -92,9 +159,10 @@ struct blkid_struct_cache struct list_head bic_devs; /* List head of all devices */ struct list_head bic_tags; /* List head of all tag types */ time_t bic_time; /* Last probe time */ - time_t bic_ftime; /* Mod time of the cachefile */ + time_t bic_ftime; /* Mod time of the cachefile */ unsigned int bic_flags; /* Status flags of the cache */ char *bic_filename; /* filename of cache */ + blkid_probe probe; /* low-level probing stuff */ }; #define BLKID_BIC_FL_PROBED 0x0002 /* We probed /proc/partition devices */ @@ -175,6 +243,26 @@ extern int blkid_set_tag(blkid_dev dev, const char *name, extern blkid_dev blkid_new_dev(void); extern void blkid_free_dev(blkid_dev dev); +/* probe.c */ +unsigned char *blkid_probe_get_buffer(blkid_probe pr, + blkid_loff_t off, blkid_loff_t len); +extern int blkid_probe_set_value(blkid_probe pr, const char *name, + unsigned char *data, size_t len); +extern int blkid_probe_vsprintf_value(blkid_probe pr, const char *name, + const char *fmt, va_list ap); +extern int blkid_probe_set_version(blkid_probe pr, const char *version); +extern int blkid_probe_sprintf_version(blkid_probe pr, const char *fmt, ...); +extern int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len); +extern int blkid_probe_set_utf8label(blkid_probe pr, unsigned char *label, + size_t len, int enc); +extern int blkid_probe_sprintf_uuid(blkid_probe pr, unsigned char *uuid, + size_t len, const char *fmt, ...); +extern int blkid_probe_set_uuid(blkid_probe pr, unsigned char *uuid); + + +#define BLKID_ENC_UTF16BE 0 +#define BLKID_ENC_UTF16LE 1 + #ifdef __cplusplus } #endif diff --git a/libs/blkid/src/probe.c b/libs/blkid/src/probe.c index ec721538..42e8a725 100644 --- a/libs/blkid/src/probe.c +++ b/libs/blkid/src/probe.c @@ -1,16 +1,10 @@ /* - * probe.c - identify a block device by its contents, and return a dev - * struct with the details + * probe.c - reads tags (LABEL, UUID, FS type, ..) from a block device * - * Copyright (C) 1999 by Andries Brouwer - * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o - * Copyright (C) 2001 by Andreas Dilger - * Copyright (C) 2004 Kay Sievers + * Copyright (C) 2008 Karel Zak * - * %Begin-Header% * This file may be redistributed under the terms of the * GNU Lesser General Public License. - * %End-Header% */ #include @@ -30,1552 +24,663 @@ #ifdef HAVE_ERRNO_H #include #endif -#include "blkidP.h" -#include "uuid/uuid.h" -#include "probe.h" - -static int figure_label_len(const unsigned char *label, int len) -{ - const unsigned char *end = label + len - 1; +#include +#ifdef HAVE_LIBUUID +#include +#endif +#include - while ((*end == ' ' || *end == 0) && end >= label) - --end; - if (end >= label) { - label = label; - return end - label + 1; - } - return 0; -} +#include "blkidP.h" +#include "probers/probers.h" -static unsigned char *get_buffer(struct blkid_probe *pr, - blkid_loff_t off, size_t len) +static const struct blkid_idinfo *idinfos[] = { - ssize_t ret_read; - unsigned char *newbuf; - - if (off + len <= SB_BUFFER_SIZE) { - if (!pr->sbbuf) { - pr->sbbuf = malloc(SB_BUFFER_SIZE); - if (!pr->sbbuf) - return NULL; - if (lseek(pr->fd, 0, SEEK_SET) < 0) - return NULL; - ret_read = read(pr->fd, pr->sbbuf, SB_BUFFER_SIZE); - if (ret_read < 0) - ret_read = 0; - pr->sb_valid = ret_read; - } - if (off+len > pr->sb_valid) - return NULL; - return pr->sbbuf + off; - } else { - if (len > pr->buf_max) { - newbuf = realloc(pr->buf, len); - if (newbuf == NULL) - return NULL; - pr->buf = newbuf; - pr->buf_max = len; - } - if (blkid_llseek(pr->fd, off, SEEK_SET) < 0) - return NULL; - ret_read = read(pr->fd, pr->buf, len); - if (ret_read != (ssize_t) len) - return NULL; - return pr->buf; - } -} + &swsuspend_idinfo, + &cramfs_idinfo, + &swap_idinfo +}; -/* - * This is a special case code to check for an MDRAID device. We do - * this special since it requires checking for a superblock at the end - * of the device. - */ -static int check_mdraid(int fd, unsigned char *ret_uuid) -{ - struct mdp_superblock_s *md; - blkid_loff_t offset; - char buf[4096]; +#ifndef ARRAY_SIZE +# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +#endif - if (fd < 0) - return -BLKID_ERR_PARAM; +/* filter bitmap macros */ +#define blkid_bmp_wordsize (8 * sizeof(unsigned long)) +#define blkid_bmp_idx_bit(item) (1UL << ((item) % blkid_bmp_wordsize)) +#define blkid_bmp_idx_byte(item) ((item) / blkid_bmp_wordsize) - offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536; +#define blkid_bmp_set_item(bmp, item) \ + ((bmp)[ blkid_bmp_idx_byte(item) ] |= blkid_bmp_idx_bit(item)) - if (blkid_llseek(fd, offset, 0) < 0 || - read(fd, buf, 4096) != 4096) - return -BLKID_ERR_IO; +#define blkid_bmp_unset_item(bmp, item) \ + ((bmp)[ bmp_idx_byte(item) ] &= ~bmp_idx_bit(item)) - /* Check for magic number */ - if (memcmp("\251+N\374", buf, 4) && memcmp("\374N+\251", buf, 4)) - return -BLKID_ERR_PARAM; +#define blkid_bmp_get_item(bmp, item) \ + ((bmp)[ blkid_bmp_idx_byte(item) ] & blkid_bmp_idx_bit(item)) - if (!ret_uuid) - return 0; - *ret_uuid = 0; +#define blkid_bmp_size(max_items) \ + (((max_items) + blkid_bmp_wordsize) / blkid_bmp_wordsize) - /* The MD UUID is not contiguous in the superblock, make it so */ - md = (struct mdp_superblock_s *)buf; - if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) { - memcpy(ret_uuid, &md->set_uuid0, 4); - memcpy(ret_uuid + 4, &md->set_uuid1, 12); - } - return 0; -} +#define BLKID_FLTR_ITEMS ARRAY_SIZE(idinfos) +#define BLKID_FLTR_SIZE blkid_bmp_size(BLKID_FLTR_ITEMS) -static void set_uuid(blkid_dev dev, uuid_t uuid, const char *tag) -{ - char str[37]; - if (!uuid_is_null(uuid)) { - uuid_unparse(uuid, str); - blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str)); - } -} +static int blkid_probe_set_usage(blkid_probe pr, int usage); -static void get_ext2_info(blkid_dev dev, struct blkid_magic *id, - unsigned char *buf) +int blkid_known_fstype(const char *fstype) { - struct ext2_super_block *es = (struct ext2_super_block *) buf; - const char *label = 0; - - DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n", - blkid_le32(es->s_feature_compat), - blkid_le32(es->s_feature_incompat), - blkid_le32(es->s_feature_ro_compat))); - - if (strlen(es->s_volume_name)) - label = es->s_volume_name; - blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name)); - - set_uuid(dev, es->s_uuid, 0); + int i; - if ((es->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) && - !uuid_is_null(es->s_journal_uuid)) - set_uuid(dev, es->s_journal_uuid, "EXT_JOURNAL"); + if (!fstype) + return 0; - if (strcmp(id->bim_type, "ext2") && - ((blkid_le32(es->s_feature_incompat) & - EXT2_FEATURE_INCOMPAT_UNSUPPORTED) == 0)) - blkid_set_tag(dev, "SEC_TYPE", "ext2", sizeof("ext2")); + for (i = 0; i < ARRAY_SIZE(idinfos); i++) { + const struct blkid_idinfo *id = idinfos[i]; + if (strcmp(id->name, fstype) == 0) + return 1; + } + return 0; } /* - * Check to see if a filesystem is in /proc/filesystems. - * Returns 1 if found, 0 if not + * Returns a pointer to the newly allocated probe struct */ -static int fs_proc_check(const char *fs_name) +blkid_probe blkid_new_probe(void) { - FILE *f; - char buf[80], *cp, *t; - - f = fopen("/proc/filesystems", "r"); - if (!f) - return (0); - while (!feof(f)) { - if (!fgets(buf, sizeof(buf), f)) - break; - cp = buf; - if (!isspace(*cp)) { - while (*cp && !isspace(*cp)) - cp++; - } - while (*cp && isspace(*cp)) - cp++; - if ((t = strchr(cp, '\n')) != NULL) - *t = 0; - if ((t = strchr(cp, '\t')) != NULL) - *t = 0; - if ((t = strchr(cp, ' ')) != NULL) - *t = 0; - if (!strcmp(fs_name, cp)) { - fclose(f); - return (1); - } - } - fclose(f); - return (0); + return calloc(1, sizeof(struct blkid_struct_probe)); } /* - * Check to see if a filesystem is available as a module - * Returns 1 if found, 0 if not + * Deallocates probe struct, buffers and all allocated + * data that are associated with this probing control struct. */ -static int check_for_modules(const char *fs_name) +void blkid_free_probe(blkid_probe pr) { - struct utsname uts; - FILE *f; - char buf[1024], *cp, *t; - int i; - - if (uname(&uts)) - return (0); - snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release); - - f = fopen(buf, "r"); - if (!f) - return (0); - while (!feof(f)) { - if (!fgets(buf, sizeof(buf), f)) - break; - if ((cp = strchr(buf, ':')) != NULL) - *cp = 0; - else - continue; - if ((cp = strrchr(buf, '/')) != NULL) - cp++; - i = strlen(cp); - if (i > 3) { - t = cp + i - 3; - if (!strcmp(t, ".ko")) - *t = 0; - } - if (!strcmp(cp, fs_name)) - return (1); - } - fclose(f); - return (0); + if (!pr) + return; + free(pr->fltr); + free(pr->buf); + free(pr->sbbuf); + free(pr); } -static int system_supports_ext4(void) +static void blkid_probe_reset_vals(blkid_probe pr) { - static time_t last_check = 0; - static int ret = -1; - time_t now = time(0); - - if (ret != -1 || (last_check - now) < 5) - return ret; - last_check = now; - ret = (fs_proc_check("ext4") || check_for_modules("ext4")); - return ret; + memset(pr->vals, 0, sizeof(pr->vals)); + pr->nvals = 0; } -static int system_supports_ext4dev(void) +void blkid_reset_probe(blkid_probe pr) { - static time_t last_check = 0; - static int ret = -1; - time_t now = time(0); - - if (ret != -1 || (last_check - now) < 5) - return ret; - last_check = now; - ret = (fs_proc_check("ext4dev") || check_for_modules("ext4dev")); - return ret; + if (!pr) + return; + if (pr->buf) + memset(pr->buf, 0, pr->buf_max); + pr->buf_off = 0; + pr->buf_len = 0; + if (pr->sbbuf) + memset(pr->sbbuf, 0, BLKID_SB_BUFSIZ); + pr->sbbuf_len = 0; + blkid_probe_reset_vals(pr); } -static int probe_ext4dev(struct blkid_probe *probe, - struct blkid_magic *id, - unsigned char *buf) -{ - struct ext2_super_block *es; - es = (struct ext2_super_block *)buf; - - /* Distinguish from jbd */ - if (blkid_le32(es->s_feature_incompat) & - EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) - return -BLKID_ERR_PARAM; - - /* ext4dev requires a journal */ - if (!(blkid_le32(es->s_feature_compat) & - EXT3_FEATURE_COMPAT_HAS_JOURNAL)) - return -BLKID_ERR_PARAM; - - /* - * If the filesystem is marked as OK for use by in-development - * filesystem code, but ext4dev is not supported, and ext4 is, - * then don't call ourselves ext4dev, since we should be - * detected as ext4 in that case. - * - * If the filesystem is marked as in use by production - * filesystem, then it can only be used by ext4 and NOT by - * ext4dev, so always disclaim we are ext4dev in that case. - */ - if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) { - if (!system_supports_ext4dev() && system_supports_ext4()) - return -BLKID_ERR_PARAM; - } else - return -BLKID_ERR_PARAM; - - get_ext2_info(probe->dev, id, buf); - return 0; -} -static int probe_ext4(struct blkid_probe *probe, struct blkid_magic *id, - unsigned char *buf) +/* + * Note that we have two offsets: + * + * 1/ general device offset (pr->off), that's useful for example when we + * probe a partition from whole disk image: + * blkid-low --offset disk.img + * + * 2/ buffer offset (the 'off' argument), that useful for offsets in + * superbloks, ... + * + * That means never use lseek(fd, 0, SEEK_SET), the zero position is always + * pr->off, so lseek(fd, pr->off, SEEK_SET). + * + */ +unsigned char *blkid_probe_get_buffer(blkid_probe pr, + blkid_loff_t off, blkid_loff_t len) { - struct ext2_super_block *es; - es = (struct ext2_super_block *)buf; - - /* Distinguish from jbd */ - if (blkid_le32(es->s_feature_incompat) & - EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) - return -BLKID_ERR_PARAM; - - /* ext4 requires journal */ - if (!(blkid_le32(es->s_feature_compat) & - EXT3_FEATURE_COMPAT_HAS_JOURNAL)) - return -BLKID_ERR_PARAM; - - /* Ext4 has at least one feature which ext3 doesn't understand */ - if (!(blkid_le32(es->s_feature_ro_compat) & - EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) && - !(blkid_le32(es->s_feature_incompat) & - EXT3_FEATURE_INCOMPAT_UNSUPPORTED)) - return -BLKID_ERR_PARAM; - - /* - * If the filesystem is a OK for use by in-development - * filesystem code, and ext4dev is supported or ext4 is not - * supported, then don't call ourselves ext4, so we can redo - * the detection and mark the filesystem as ext4dev. - * - * If the filesystem is marked as in use by production - * filesystem, then it can only be used by ext4 and NOT by - * ext4dev. - */ - if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) { - if (system_supports_ext4dev() || !system_supports_ext4()) - return -BLKID_ERR_PARAM; - } - get_ext2_info(probe->dev, id, buf); - return 0; -} + ssize_t ret_read = 0; -static int probe_ext3(struct blkid_probe *probe, struct blkid_magic *id, - unsigned char *buf) -{ - struct ext2_super_block *es; - es = (struct ext2_super_block *)buf; - - /* Distinguish from ext4dev */ - if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) - return -BLKID_ERR_PARAM; - - /* ext3 requires journal */ - if (!(blkid_le32(es->s_feature_compat) & - EXT3_FEATURE_COMPAT_HAS_JOURNAL)) - return -BLKID_ERR_PARAM; - - /* Any features which ext3 doesn't understand */ - if ((blkid_le32(es->s_feature_ro_compat) & - EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) || - (blkid_le32(es->s_feature_incompat) & - EXT3_FEATURE_INCOMPAT_UNSUPPORTED)) - return -BLKID_ERR_PARAM; - - get_ext2_info(probe->dev, id, buf); - return 0; + if (off + len <= BLKID_SB_BUFSIZ) { + if (!pr->sbbuf) { + pr->sbbuf = malloc(BLKID_SB_BUFSIZ); + if (!pr->sbbuf) + return NULL; + } + if (!pr->sbbuf_len) { + if (lseek(pr->fd, pr->off, SEEK_SET) < 0) + return NULL; + ret_read = read(pr->fd, pr->sbbuf, BLKID_SB_BUFSIZ); + if (ret_read < 0) + ret_read = 0; + pr->sbbuf_len = ret_read; + } + if (off + len > pr->sbbuf_len) + return NULL; + return pr->sbbuf + off; + } else { + unsigned char *newbuf = NULL; + + if (len > pr->buf_max) { + newbuf = realloc(pr->buf, len); + if (!newbuf) + return NULL; + pr->buf = newbuf; + pr->buf_max = len; + pr->buf_off = 0; + pr->buf_len = 0; + } + if (newbuf || off < pr->buf_off || + off + len > pr->buf_off + pr->buf_len) { + + if (blkid_llseek(pr->fd, pr->off + off, SEEK_SET) < 0) + return NULL; + + ret_read = read(pr->fd, pr->buf, len); + if (ret_read != (ssize_t) len) + return NULL; + pr->buf_off = off; + pr->buf_len = len; + } + return off ? pr->buf + (off - pr->buf_off) : pr->buf; + } } -static int probe_ext2(struct blkid_probe *probe, struct blkid_magic *id, - unsigned char *buf) +/* + * Assignes the device to probe control struct, resets internal buffers and + * reads 512 bytes from device to the buffers. + * + * Returns -1 in case of failure, or 0 on success. + */ +int blkid_probe_set_device(blkid_probe pr, int fd, + blkid_loff_t off, blkid_loff_t size) { - struct ext2_super_block *es; + if (!pr) + return -1; - es = (struct ext2_super_block *)buf; + blkid_reset_probe(pr); - /* Distinguish between ext3 and ext2 */ - if ((blkid_le32(es->s_feature_compat) & - EXT3_FEATURE_COMPAT_HAS_JOURNAL)) - return -BLKID_ERR_PARAM; + pr->fd = fd; + pr->off = off; + pr->size = size; - /* Any features which ext2 doesn't understand */ - if ((blkid_le32(es->s_feature_ro_compat) & - EXT2_FEATURE_RO_COMPAT_UNSUPPORTED) || - (blkid_le32(es->s_feature_incompat) & - EXT2_FEATURE_INCOMPAT_UNSUPPORTED)) - return -BLKID_ERR_PARAM; + /* read SB to test if the device is readable */ + if (!blkid_probe_get_buffer(pr, 0, 0x200)) + return -1; - get_ext2_info(probe->dev, id, buf); return 0; } -static int probe_jbd(struct blkid_probe *probe, struct blkid_magic *id, - unsigned char *buf) +int blkid_probe_set_request(blkid_probe pr, int flags) { - struct ext2_super_block *es = (struct ext2_super_block *) buf; - - if (!(blkid_le32(es->s_feature_incompat) & - EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) - return -BLKID_ERR_PARAM; - - get_ext2_info(probe->dev, id, buf); - + if (!pr) + return -1; + pr->probreq = flags; return 0; } -#define FAT_ATTR_VOLUME_ID 0x08 -#define FAT_ATTR_DIR 0x10 -#define FAT_ATTR_LONG_NAME 0x0f -#define FAT_ATTR_MASK 0x3f -#define FAT_ENTRY_FREE 0xe5 - -static const char *no_name = "NO NAME "; - -static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count) +int blkid_probe_reset_filter(blkid_probe pr) { - int i; - - for (i = 0; i < count; i++) { - if (dir[i].name[0] == 0x00) - break; - - if ((dir[i].name[0] == FAT_ENTRY_FREE) || - (dir[i].cluster_high != 0 || dir[i].cluster_low != 0) || - ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)) - continue; - - if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == - FAT_ATTR_VOLUME_ID) { - return dir[i].name; - } - } + if (!pr) + return -1; + if (pr->fltr) + memset(pr->fltr, 0, BLKID_FLTR_SIZE * sizeof(unsigned long)); return 0; } -/* FAT label extraction from the root directory taken from Kay - * Sievers's volume_id library */ -static int probe_fat(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) +/* + * flag: + * + * BLKID_FLTR_NOTIN - probe all filesystems which are NOT IN names[] + * + * BLKID_FLTR_ONLYIN - probe filesystem which are IN names[] + */ +int blkid_probe_filter_types(blkid_probe pr, int flag, char *names[]) { - struct vfat_super_block *vs = (struct vfat_super_block *) buf; - struct msdos_super_block *ms = (struct msdos_super_block *) buf; - struct vfat_dir_entry *dir; - char serno[10]; - const unsigned char *label = 0, *vol_label = 0, *tmp; - unsigned char *vol_serno; - int label_len = 0, maxloop = 100; - __u16 sector_size, dir_entries, reserved; - __u32 sect_count, fat_size, dir_size, cluster_count, fat_length; - __u32 buf_size, start_data_sect, next, root_start, root_dir_entries; - - /* sector size check */ - tmp = (unsigned char *)&ms->ms_sector_size; - sector_size = tmp[0] + (tmp[1] << 8); - if (sector_size != 0x200 && sector_size != 0x400 && - sector_size != 0x800 && sector_size != 0x1000) - return 1; - - tmp = (unsigned char *)&ms->ms_dir_entries; - dir_entries = tmp[0] + (tmp[1] << 8); - reserved = blkid_le16(ms->ms_reserved); - tmp = (unsigned char *)&ms->ms_sectors; - sect_count = tmp[0] + (tmp[1] << 8); - if (sect_count == 0) - sect_count = blkid_le32(ms->ms_total_sect); - - fat_length = blkid_le16(ms->ms_fat_length); - if (fat_length == 0) - fat_length = blkid_le32(vs->vs_fat32_length); - - fat_size = fat_length * ms->ms_fats; - dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) + - (sector_size-1)) / sector_size; - - cluster_count = sect_count - (reserved + fat_size + dir_size); - if (ms->ms_cluster_size == 0) - return 1; - cluster_count /= ms->ms_cluster_size; - - if (cluster_count > FAT32_MAX) - return 1; - - if (ms->ms_fat_length) { - /* the label may be an attribute in the root directory */ - root_start = (reserved + fat_size) * sector_size; - root_dir_entries = vs->vs_dir_entries[0] + - (vs->vs_dir_entries[1] << 8); - - buf_size = root_dir_entries * sizeof(struct vfat_dir_entry); - dir = (struct vfat_dir_entry *) get_buffer(probe, root_start, - buf_size); - if (dir) - vol_label = search_fat_label(dir, root_dir_entries); - - if (!vol_label || !memcmp(vol_label, no_name, 11)) - vol_label = ms->ms_label; - vol_serno = ms->ms_serno; - - blkid_set_tag(probe->dev, "SEC_TYPE", "msdos", - sizeof("msdos")); - } else { - /* Search the FAT32 root dir for the label attribute */ - buf_size = vs->vs_cluster_size * sector_size; - start_data_sect = reserved + fat_size; - - next = blkid_le32(vs->vs_root_cluster); - while (next && --maxloop) { - __u32 next_sect_off; - __u64 next_off, fat_entry_off; - int count; - - next_sect_off = (next - 2) * vs->vs_cluster_size; - next_off = (start_data_sect + next_sect_off) * - sector_size; - - dir = (struct vfat_dir_entry *) - get_buffer(probe, next_off, buf_size); - if (dir == NULL) - break; + int i; - count = buf_size / sizeof(struct vfat_dir_entry); + if (!pr || !names) + return -1; + if (!pr->fltr) + pr->fltr = calloc(BLKID_FLTR_SIZE, sizeof(unsigned long)); + else + blkid_probe_reset_filter(pr); - vol_label = search_fat_label(dir, count); - if (vol_label) - break; + for (i = 0; i < ARRAY_SIZE(idinfos); i++) { + int has = 0; + const struct blkid_idinfo *id = idinfos[i]; + char **n; - /* get FAT entry */ - fat_entry_off = (reserved * sector_size) + - (next * sizeof(__u32)); - buf = get_buffer(probe, fat_entry_off, buf_size); - if (buf == NULL) + for (n = names; *n; n++) { + if (!strcmp(id->name, *n)) { + has = 1; break; - - /* set next cluster */ - next = blkid_le32(*((__u32 *) buf) & 0x0fffffff); + } + } + /* The default is enable all filesystems, + * set relevant bitmap bit means disable the filesystem. + */ + if (flag & BLKID_FLTR_ONLYIN) { + if (!has) + blkid_bmp_set_item(pr->fltr, i); + } else if (flag & BLKID_FLTR_NOTIN) { + if (has) + blkid_bmp_set_item(pr->fltr, i); } - - if (!vol_label || !memcmp(vol_label, no_name, 11)) - vol_label = vs->vs_label; - vol_serno = vs->vs_serno; - } - - if (vol_label && memcmp(vol_label, no_name, 11)) { - if ((label_len = figure_label_len(vol_label, 11))) - label = vol_label; } - - /* We can't just print them as %04X, because they are unaligned */ - sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2], - vol_serno[1], vol_serno[0]); - - blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len); - blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1); - return 0; } /* - * The FAT filesystem could be without a magic string in superblock - * (e.g. old floppies). This heuristic for FAT detection is inspired - * by http://vrfy.org/projects/volume_id/ and Linux kernel. - * [7-Jul-2005, Karel Zak ] + * flag: + * + * BLKID_FLTR_NOTIN - probe all filesystems which are NOT IN "usage" + * + * BLKID_FLTR_ONLYIN - probe filesystem which are IN "usage" + * + * where the "usage" is a set of filesystem according the usage flag (crypto, + * raid, filesystem, ...) */ -static int probe_fat_nomagic(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) +int blkid_probe_filter_usage(blkid_probe pr, int flag, int usage) { - struct msdos_super_block *ms; - - ms = (struct msdos_super_block *)buf; - - /* heads check */ - if (ms->ms_heads == 0) - return 1; - - /* cluster size check*/ - if (ms->ms_cluster_size == 0 || - (ms->ms_cluster_size & (ms->ms_cluster_size-1))) - return 1; - - /* media check */ - if (ms->ms_media < 0xf8 && ms->ms_media != 0xf0) - return 1; - - /* fat counts(Linux kernel expects at least 1 FAT table) */ - if (!ms->ms_fats) - return 1; - - /* - * OS/2 and apparently DFSee will place a FAT12/16-like - * pseudo-superblock in the first 512 bytes of non-FAT - * filesystems --- at least JFS and HPFS, and possibly others. - * So we explicitly check for those filesystems at the - * FAT12/16 filesystem magic field identifier, and if they are - * present, we rule this out as a FAT filesystem, despite the - * FAT-like pseudo-header. - */ - if ((memcmp(ms->ms_magic, "JFS ", 8) == 0) || - (memcmp(ms->ms_magic, "HPFS ", 8) == 0)) - return 1; - - return probe_fat(probe, id, buf); -} - -static int probe_ntfs(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) -{ - struct ntfs_super_block *ns; - struct master_file_table_record *mft; - struct file_attribute *attr; - char uuid_str[17], label_str[129], *cp; - int bytes_per_sector, sectors_per_cluster; - int mft_record_size, attr_off, attr_len; - unsigned int i, attr_type, val_len; - int val_off; - __u64 nr_clusters; - blkid_loff_t off; - unsigned char *buf_mft, *val; - - ns = (struct ntfs_super_block *) buf; - - bytes_per_sector = ns->bios_parameter_block[0] + - (ns->bios_parameter_block[1] << 8); - sectors_per_cluster = ns->bios_parameter_block[2]; - - if ((bytes_per_sector < 512) || (sectors_per_cluster == 0)) - return 1; + int i; - if (ns->cluster_per_mft_record < 0) - mft_record_size = 1 << (0-ns->cluster_per_mft_record); + if (!pr || !usage) + return -1; + if (!pr->fltr) + pr->fltr = calloc(BLKID_FLTR_SIZE, sizeof(unsigned long)); else - mft_record_size = ns->cluster_per_mft_record * - sectors_per_cluster * bytes_per_sector; - nr_clusters = blkid_le64(ns->number_of_sectors) / sectors_per_cluster; - - if ((blkid_le64(ns->mft_cluster_location) > nr_clusters) || - (blkid_le64(ns->mft_mirror_cluster_location) > nr_clusters)) - return 1; - - off = blkid_le64(ns->mft_mirror_cluster_location) * - bytes_per_sector * sectors_per_cluster; - - buf_mft = get_buffer(probe, off, mft_record_size); - if (!buf_mft) - return 1; - - if (memcmp(buf_mft, "FILE", 4)) - return 1; - - off = blkid_le64(ns->mft_cluster_location) * bytes_per_sector * - sectors_per_cluster; - - buf_mft = get_buffer(probe, off, mft_record_size); - if (!buf_mft) - return 1; - - if (memcmp(buf_mft, "FILE", 4)) - return 1; - - off += MFT_RECORD_VOLUME * mft_record_size; - - buf_mft = get_buffer(probe, off, mft_record_size); - if (!buf_mft) - return 1; - - if (memcmp(buf_mft, "FILE", 4)) - return 1; + blkid_probe_reset_filter(pr); - mft = (struct master_file_table_record *) buf_mft; + for (i = 0; i < ARRAY_SIZE(idinfos); i++) { + const struct blkid_idinfo *id = idinfos[i]; - attr_off = blkid_le16(mft->attrs_offset); - label_str[0] = 0; - - while (1) { - attr = (struct file_attribute *) (buf_mft + attr_off); - attr_len = blkid_le16(attr->len); - attr_type = blkid_le32(attr->type); - val_off = blkid_le16(attr->value_offset); - val_len = blkid_le32(attr->value_len); - - attr_off += attr_len; - - if ((attr_off > mft_record_size) || - (attr_len == 0)) - break; - - if (attr_type == MFT_RECORD_ATTR_END) - break; - - if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) { - if (val_len > sizeof(label_str)) - val_len = sizeof(label_str)-1; - - for (i=0, cp=label_str; i < val_len; i+=2,cp++) { - val = ((__u8 *) attr) + val_off + i; - *cp = val[0]; - if (val[1]) - *cp = '?'; - } - *cp = 0; - } + if (id->usage & usage) { + if (flag & BLKID_FLTR_NOTIN) + blkid_bmp_set_item(pr->fltr, i); + } else if (flag & BLKID_FLTR_ONLYIN) + blkid_bmp_set_item(pr->fltr, i); } - - sprintf(uuid_str, "%016llX", blkid_le64(ns->volume_serial)); - blkid_set_tag(probe->dev, "UUID", uuid_str, 0); - if (label_str[0]) - blkid_set_tag(probe->dev, "LABEL", label_str, 0); return 0; } -static int probe_xfs(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) +int blkid_probe_invert_filter(blkid_probe pr) { - struct xfs_super_block *xs; - const char *label = 0; - - xs = (struct xfs_super_block *)buf; + int i; - if (strlen(xs->xs_fname)) - label = xs->xs_fname; - blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname)); - set_uuid(probe->dev, xs->xs_uuid, 0); + if (!pr || !pr->fltr) + return -1; + for (i = 0; i < BLKID_FLTR_SIZE; i++) + pr->fltr[i] = ~pr->fltr[i]; return 0; } -static int probe_reiserfs(struct blkid_probe *probe, - struct blkid_magic *id, unsigned char *buf) +int blkid_do_probe(blkid_probe pr) { - struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf; - unsigned int blocksize; - const char *label = 0; - - blocksize = blkid_le16(rs->rs_blocksize); - - /* The blocksize must be at least 1k */ - if ((blocksize >> 10) == 0) - return -BLKID_ERR_PARAM; - - /* If the superblock is inside the journal, we have the wrong one */ - if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block)) - return -BLKID_ERR_BIG; - - /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */ - if (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') { - if (strlen(rs->rs_label)) - label = rs->rs_label; - set_uuid(probe->dev, rs->rs_uuid, 0); - } - blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label)); + int i; - return 0; -} + if (!pr) + return -1; -static int probe_reiserfs4(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) -{ - struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf; - const unsigned char *label = 0; + blkid_probe_reset_vals(pr); - if (strlen((char *) rs4->rs4_label)) - label = rs4->rs4_label; - set_uuid(probe->dev, rs4->rs4_uuid, 0); - blkid_set_tag(probe->dev, "LABEL", (const char *) label, - sizeof(rs4->rs4_label)); + for (i = 0; i < ARRAY_SIZE(idinfos); i++) { + const struct blkid_idinfo *id; + const struct blkid_idmag *mag; - return 0; -} + if (pr->fltr && blkid_bmp_get_item(pr->fltr, i)) + continue; -static int probe_jfs(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) -{ - struct jfs_super_block *js; - const char *label = 0; + id = idinfos[i]; + mag = id->magics ? &id->magics[0] : NULL; - js = (struct jfs_super_block *)buf; + /* try to detect by magic string */ + while(mag && mag->magic) { + int idx; + unsigned char *buf; - if (blkid_le32(js->js_bsize) != (1 << blkid_le16(js->js_l2bsize))) - return 1; + idx = mag->kboff + (mag->sboff >> 10); + buf = blkid_probe_get_buffer(pr, idx << 10, 1024); - if (blkid_le32(js->js_pbsize) != (1 << blkid_le16(js->js_l2pbsize))) - return 1; + if (buf && !memcmp(mag->magic, + buf + (mag->sboff & 0x3ff), mag->len)) + break; + mag++; + } - if ((blkid_le16(js->js_l2bsize) - blkid_le16(js->js_l2pbsize)) != - blkid_le16(js->js_l2bfactor)) - return 1; + if (mag && mag->magic == NULL) + /* magic string(s) defined, but not found */ + continue; - if (strlen((char *) js->js_label)) - label = (char *) js->js_label; - blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label)); - set_uuid(probe->dev, js->js_uuid, 0); - return 0; -} + /* final check by probing function */ + if (id->probefunc && id->probefunc(pr, mag) != 0) + continue; -static int probe_zfs(struct blkid_probe *probe, struct blkid_magic *id, - unsigned char *buf) -{ -#if 0 - char *vdev_label; - const char *pool_name = 0; + /* all cheks passed */ + if (pr->probreq & BLKID_PROBREQ_TYPE) + blkid_probe_set_value(pr, "TYPE", + (unsigned char *) id->name, + strlen(id->name) + 1); + if ((pr->probreq & BLKID_PROBREQ_SECTYPE) && id->secname) + blkid_probe_set_value(pr, "SEC_TYPE", + (unsigned char *) id->secname, + strlen(id->secname) + 1); + if (pr->probreq & BLKID_PROBREQ_USAGE) + blkid_probe_set_usage(pr, id->usage); - /* read nvpair data for pool name, pool GUID (complex) */ - blkid_set_tag(probe->dev, "LABEL", pool_name, sizeof(pool_name)); - set_uuid(probe->dev, pool_guid, 0); -#endif - return 0; + return 0; + } + return 1; } -static int probe_luks(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) +int blkid_probe_numof_values(blkid_probe pr) { - char uuid[40]; - - /* 168 is the offset to the 40 character uuid: - * http://luks.endorphin.org/LUKS-on-disk-format.pdf */ - strncpy(uuid, (char *) buf+168, 40); - blkid_set_tag(probe->dev, "UUID", uuid, sizeof(uuid)); - return 0; + if (!pr) + return -1; + return pr->nvals; } -static int probe_romfs(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) -{ - struct romfs_super_block *ros; - const char *label = 0; - - ros = (struct romfs_super_block *)buf; - if (strlen((char *) ros->ros_volume)) - label = (char *) ros->ros_volume; - blkid_set_tag(probe->dev, "LABEL", label, 0); - return 0; -} - -static int probe_cramfs(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) +static struct blkid_prval *blkid_probe_assign_value( + blkid_probe pr, const char *name) { - struct cramfs_super_block *csb; - const char *label = 0; + struct blkid_prval *v; - csb = (struct cramfs_super_block *)buf; + if (!name) + return NULL; + if (pr->nvals >= BLKID_PROBVAL_NVALS) + return NULL; - if (strlen((char *) csb->name)) - label = (char *) csb->name; - blkid_set_tag(probe->dev, "LABEL", label, 0); - return 0; + v = &pr->vals[pr->nvals]; + v->name = name; + pr->nvals++; + return v; } -static int probe_swap0(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf __BLKID_ATTR((unused))) +int blkid_probe_set_value(blkid_probe pr, const char *name, + unsigned char *data, size_t len) { - blkid_set_tag(probe->dev, "UUID", 0, 0); - blkid_set_tag(probe->dev, "LABEL", 0, 0); - return 0; -} + struct blkid_prval *v; -static int probe_swap1(struct blkid_probe *probe, - struct blkid_magic *id, - unsigned char *buf __BLKID_ATTR((unused))) -{ - struct swap_id_block *sws; - - probe_swap0(probe, id, buf); - /* - * Version 1 swap headers are always located at offset of 1024 - * bytes, although the swap signature itself is located at the - * end of the page (which may vary depending on hardware - * pagesize). - */ - sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024); - if (!sws) - return 1; + if (len > BLKID_PROBVAL_BUFSIZ) + len = BLKID_PROBVAL_BUFSIZ; - /* check for wrong version or zeroed pagecount, for sanity */ - if (!memcmp(id->bim_magic, "SWAPSPACE2", id->bim_len) && - (sws->sws_version != 1 || sws->sws_lastpage == 0)) - return 1; + v = blkid_probe_assign_value(pr, name); + if (!v) + return -1; - /* arbitrary sanity check.. is there any garbage down there? */ - if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0) { - if (sws->sws_volume[0]) - blkid_set_tag(probe->dev, "LABEL", sws->sws_volume, - sizeof(sws->sws_volume)); - if (sws->sws_uuid[0]) - set_uuid(probe->dev, sws->sws_uuid, 0); - } + memcpy(v->data, data, len); + v->len = len; return 0; } -static int probe_iso9660(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) +int blkid_probe_vsprintf_value(blkid_probe pr, const char *name, + const char *fmt, va_list ap) { - struct iso_volume_descriptor *iso; - const unsigned char *label; + struct blkid_prval *v; + size_t len; - iso = (struct iso_volume_descriptor *) buf; - label = iso->volume_id; - - blkid_set_tag(probe->dev, "LABEL", (const char *) label, - figure_label_len(label, 32)); - return 0; -} + v = blkid_probe_assign_value(pr, name); + if (!v) + return -1; + len = vsnprintf((char *) v->data, sizeof(v->data), fmt, ap); -static const char -*udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02", - "NSR03", "TEA01", 0 }; - -static int probe_udf(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf __BLKID_ATTR((unused))) -{ - int j, bs; - struct iso_volume_descriptor *isosb; - const char ** m; - - /* determine the block size by scanning in 2K increments - (block sizes larger than 2K will be null padded) */ - for (bs = 1; bs < 16; bs++) { - isosb = (struct iso_volume_descriptor *) - get_buffer(probe, bs*2048+32768, sizeof(isosb)); - if (!isosb) - return 1; - if (isosb->vd_id[0]) - break; + if (len <= 0) { + pr->nvals--; /* reset the latest assigned value */ + return -1; } - - /* Scan up to another 64 blocks looking for additional VSD's */ - for (j = 1; j < 64; j++) { - if (j > 1) { - isosb = (struct iso_volume_descriptor *) - get_buffer(probe, j*bs*2048+32768, - sizeof(isosb)); - if (!isosb) - return 1; - } - /* If we find NSR0x then call it udf: - NSR01 for UDF 1.00 - NSR02 for UDF 1.50 - NSR03 for UDF 2.00 */ - if (!memcmp(isosb->vd_id, "NSR0", 4)) - return probe_iso9660(probe, id, buf); - for (m = udf_magic; *m; m++) - if (!memcmp(*m, isosb->vd_id, 5)) - break; - if (*m == 0) - return 1; - } - return 1; -} - -static int probe_ocfs(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) -{ - struct ocfs_volume_header ovh; - struct ocfs_volume_label ovl; - __u32 major; - - memcpy(&ovh, buf, sizeof(ovh)); - memcpy(&ovl, buf+512, sizeof(ovl)); - - major = ocfsmajor(ovh); - if (major == 1) - blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1")); - else if (major >= 9) - blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs")); - - blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl)); - blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh)); - set_uuid(probe->dev, ovl.vol_id, 0); + v->len = len + 1; return 0; } -static int probe_ocfs2(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) +int blkid_probe_set_version(blkid_probe pr, const char *version) { - struct ocfs2_super_block *osb; - - osb = (struct ocfs2_super_block *)buf; - - blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label)); - set_uuid(probe->dev, osb->s_uuid, 0); + if (pr->probreq & BLKID_PROBREQ_VERSION) + return blkid_probe_set_value(pr, "VERSION", + (unsigned char *) version, strlen(version) + 1); return 0; } -static int probe_oracleasm(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) +int blkid_probe_sprintf_version(blkid_probe pr, const char *fmt, ...) { - struct oracle_asm_disk_label *dl; + int rc = 0; - dl = (struct oracle_asm_disk_label *)buf; + if (pr->probreq & BLKID_PROBREQ_VERSION) { + va_list ap; - blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id)); - return 0; + va_start(ap, fmt); + rc = blkid_probe_vsprintf_value(pr, "VERSION", fmt, ap); + va_end(ap); + } + return rc; } -static int probe_gfs(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) +static int blkid_probe_set_usage(blkid_probe pr, int usage) { - struct gfs2_sb *sbd; - const char *label = 0; + char *u = NULL; - sbd = (struct gfs2_sb *)buf; - - if (blkid_be32(sbd->sb_fs_format) == GFS_FORMAT_FS && - blkid_be32(sbd->sb_multihost_format) == GFS_FORMAT_MULTI) - { - blkid_set_tag(probe->dev, "UUID", 0, 0); + if (usage & BLKID_USAGE_FILESYSTEM) + u = "filesystem"; + else if (usage & BLKID_USAGE_RAID) + u = "raid"; + else if (usage & BLKID_USAGE_CRYPTO) + u = "crypto"; + else if (usage & BLKID_USAGE_OTHER) + u = "other"; + else + u = "unknown"; - if (strlen(sbd->sb_locktable)) - label = sbd->sb_locktable; - blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable)); - return 0; - } - return 1; + return blkid_probe_set_value(pr, "USAGE", (unsigned char *) u, strlen(u) + 1); } -static int probe_gfs2(struct blkid_probe *probe, - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) +int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len) { - struct gfs2_sb *sbd; - const char *label = 0; + struct blkid_prval *v; + int i; - sbd = (struct gfs2_sb *)buf; + if ((pr->probreq & BLKID_PROBREQ_LABELRAW) && + blkid_probe_set_value(pr, "LABEL_RAW", label, len) < 0) + return -1; + if (!(pr->probreq & BLKID_PROBREQ_LABEL)) + return 0; + v = blkid_probe_assign_value(pr, "LABEL"); + if (!v) + return -1; - if (blkid_be32(sbd->sb_fs_format) == GFS2_FORMAT_FS && - blkid_be32(sbd->sb_multihost_format) == GFS2_FORMAT_MULTI) - { - blkid_set_tag(probe->dev, "UUID", 0, 0); + memcpy(v->data, label, len); + v->data[len] = '\0'; - if (strlen(sbd->sb_locktable)) - label = sbd->sb_locktable; - blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable)); - return 0; + /* remove trailing whitespace */ + i = strnlen((char *) v->data, len); + while (i--) { + if (!isspace(v->data[i])) + break; } - return 1; + v->data[++i] = '\0'; + v->len = i + 1; + return 0; } -static void unicode_16be_to_utf8(unsigned char *str, int out_len, - const unsigned char *buf, int in_len) +static size_t encode_to_utf8(int enc, unsigned char *dest, size_t len, + unsigned char *src, size_t count) { - int i, j; - unsigned int c; + size_t i, j; + uint16_t c; - for (i = j = 0; i + 2 <= in_len; i += 2) { - c = (buf[i] << 8) | buf[i+1]; + j = 0; + for (i = 0; i + 2 <= count; i += 2) { + if (enc == BLKID_ENC_UTF16LE) + c = (src[i+1] << 8) | src[i]; + else /* BLKID_ENC_UTF16BE */ + c = (src[i] << 8) | src[i+1]; if (c == 0) { - str[j] = '\0'; + dest[j] = '\0'; break; } else if (c < 0x80) { - if (j+1 >= out_len) + if (j+1 >= len) break; - str[j++] = (unsigned char) c; + dest[j++] = (uint8_t) c; } else if (c < 0x800) { - if (j+2 >= out_len) + if (j+2 >= len) break; - str[j++] = (unsigned char) (0xc0 | (c >> 6)); - str[j++] = (unsigned char) (0x80 | (c & 0x3f)); + dest[j++] = (uint8_t) (0xc0 | (c >> 6)); + dest[j++] = (uint8_t) (0x80 | (c & 0x3f)); } else { - if (j+3 >= out_len) + if (j+3 >= len) break; - str[j++] = (unsigned char) (0xe0 | (c >> 12)); - str[j++] = (unsigned char) (0x80 | ((c >> 6) & 0x3f)); - str[j++] = (unsigned char) (0x80 | (c & 0x3f)); + dest[j++] = (uint8_t) (0xe0 | (c >> 12)); + dest[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f)); + dest[j++] = (uint8_t) (0x80 | (c & 0x3f)); } } - str[j] = '\0'; + dest[j] = '\0'; + return j; } -static int probe_hfs(struct blkid_probe *probe __BLKID_ATTR((unused)), - struct blkid_magic *id __BLKID_ATTR((unused)), - unsigned char *buf) +int blkid_probe_set_utf8label(blkid_probe pr, unsigned char *label, + size_t len, int enc) { - struct hfs_mdb *hfs = (struct hfs_mdb *) buf; - char uuid_str[17]; - __u64 uuid; - - if ((memcmp(hfs->embed_sig, "H+", 2) == 0) || - (memcmp(hfs->embed_sig, "HX", 2) == 0)) - return 1; /* Not hfs, but an embedded HFS+ */ - - uuid = blkid_le64(*((unsigned long long *) hfs->finder_info.id)); - if (uuid) { - sprintf(uuid_str, "%016llX", uuid); - blkid_set_tag(probe->dev, "UUID", uuid_str, 0); - } - blkid_set_tag(probe->dev, "LABEL", hfs->label, hfs->label_len); + struct blkid_prval *v; + + if ((pr->probreq & BLKID_PROBREQ_LABELRAW) && + blkid_probe_set_value(pr, "LABEL_RAW", label, len) < 0) + return -1; + if (!(pr->probreq & BLKID_PROBREQ_LABEL)) + return 0; + v = blkid_probe_assign_value(pr, "LABEL"); + if (!v) + return -1; + v->len = encode_to_utf8(enc, v->data, sizeof(v->data), label, len); + v->len++; /* \0 */ return 0; } - -static int probe_hfsplus(struct blkid_probe *probe, - struct blkid_magic *id, - unsigned char *buf) +/* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */ +static int uuid_is_empty(const unsigned char *buf, size_t len) { - struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT]; - struct hfsplus_bnode_descriptor *descr; - struct hfsplus_bheader_record *bnode; - struct hfsplus_catalog_key *key; - struct hfsplus_vol_header *hfsplus; - struct hfs_mdb *sbd = (struct hfs_mdb *) buf; - unsigned int alloc_block_size; - unsigned int alloc_first_block; - unsigned int embed_first_block; - unsigned int off = 0; - unsigned int blocksize; - unsigned int cat_block; - unsigned int ext_block_start; - unsigned int ext_block_count; - unsigned int record_count; - unsigned int leaf_node_head; - unsigned int leaf_node_count; - unsigned int leaf_node_size; - unsigned int leaf_block; - unsigned int label_len; - int ext; - __u64 leaf_off, uuid; - char uuid_str[17], label[512]; - - /* Check for a HFS+ volume embedded in a HFS volume */ - if (memcmp(sbd->signature, "BD", 2) == 0) { - if ((memcmp(sbd->embed_sig, "H+", 2) != 0) && - (memcmp(sbd->embed_sig, "HX", 2) != 0)) - /* This must be an HFS volume, so fail */ - return 1; - - alloc_block_size = blkid_be32(sbd->al_blk_size); - alloc_first_block = blkid_be16(sbd->al_bl_st); - embed_first_block = blkid_be16(sbd->embed_startblock); - off = (alloc_first_block * 512) + - (embed_first_block * alloc_block_size); - buf = get_buffer(probe, off + (id->bim_kboff * 1024), - sizeof(sbd)); - if (!buf) - return 1; - - hfsplus = (struct hfsplus_vol_header *) buf; - } - - hfsplus = (struct hfsplus_vol_header *) buf; - - if ((memcmp(hfsplus->signature, "H+", 2) != 0) && - (memcmp(hfsplus->signature, "HX", 2) != 0)) - return 1; + int i; - uuid = blkid_le64(*((unsigned long long *) hfsplus->finder_info.id)); - if (uuid) { - sprintf(uuid_str, "%016llX", uuid); - blkid_set_tag(probe->dev, "UUID", uuid_str, 0); - } + for (i = 0; i < len; i++) + if (buf[i]) + return 0; + return 1; +} - blocksize = blkid_be32(hfsplus->blocksize); - memcpy(extents, hfsplus->cat_file.extents, sizeof(extents)); - cat_block = blkid_be32(extents[0].start_block); +int blkid_probe_sprintf_uuid(blkid_probe pr, unsigned char *uuid, + size_t len, const char *fmt, ...) +{ + va_list ap; + int rc; - buf = get_buffer(probe, off + (cat_block * blocksize), 0x2000); - if (!buf) + if (uuid_is_empty(uuid, len)) return 0; - bnode = (struct hfsplus_bheader_record *) - &buf[sizeof(struct hfsplus_bnode_descriptor)]; - - leaf_node_head = blkid_be32(bnode->leaf_head); - leaf_node_size = blkid_be16(bnode->node_size); - leaf_node_count = blkid_be32(bnode->leaf_count); - if (leaf_node_count == 0) + if ((pr->probreq & BLKID_PROBREQ_UUIDRAW) && + blkid_probe_set_value(pr, "UUID_RAW", uuid, len) < 0) + return -1; + if (!(pr->probreq & BLKID_PROBREQ_UUID)) return 0; - leaf_block = (leaf_node_head * leaf_node_size) / blocksize; + va_start(ap, fmt); + rc = blkid_probe_vsprintf_value(pr, "UUID", fmt, ap); + va_end(ap); - /* get physical location */ - for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) { - ext_block_start = blkid_be32(extents[ext].start_block); - ext_block_count = blkid_be32(extents[ext].block_count); - if (ext_block_count == 0) - return 0; + /* convert to lower case (..be paranoid) */ + if (!rc) { + int i; + struct blkid_prval *v = &pr->vals[pr->nvals]; - /* this is our extent */ - if (leaf_block < ext_block_count) - break; - - leaf_block -= ext_block_count; + for (i = 0; i < v->len; i++) + if (v->data[i] >= 'A' && v->data[i] <= 'F') + v->data[i] = (v->data[i] - 'A') + 'a'; } - if (ext == HFSPLUS_EXTENT_COUNT) - return 0; - - leaf_off = (ext_block_start + leaf_block) * blocksize; + return rc; +} - buf = get_buffer(probe, off + leaf_off, leaf_node_size); - if (!buf) - return 0; +/* default _set_uuid function to set DCE UUIDs */ +int blkid_probe_set_uuid(blkid_probe pr, unsigned char *uuid) +{ + struct blkid_prval *v; - descr = (struct hfsplus_bnode_descriptor *) buf; - record_count = blkid_be16(descr->num_recs); - if (record_count == 0) + if (uuid_is_empty(uuid, 16)) return 0; - if (descr->type != HFS_NODE_LEAF) + if ((pr->probreq & BLKID_PROBREQ_UUIDRAW) && + blkid_probe_set_value(pr, "UUID_RAW", uuid, 16) < 0) + return -1; + if (!(pr->probreq & BLKID_PROBREQ_UUID)) return 0; - key = (struct hfsplus_catalog_key *) - &buf[sizeof(struct hfsplus_bnode_descriptor)]; - - if (blkid_be32(key->parent_id) != HFSPLUS_POR_CNID) - return 0; + v = blkid_probe_assign_value(pr, "UUID"); - label_len = blkid_be16(key->unicode_len) * 2; - unicode_16be_to_utf8(label, sizeof(label), key->unicode, label_len); - blkid_set_tag(probe->dev, "LABEL", label, 0); +#ifdef HAVE_LIBUUID + { + uuid_unparse(uuid, (char *) v->data); + v->len = 37; + } +#else + v->len = snprintf(v->data, sizeof(v->data), + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + uuid[0], uuid[1], uuid[2], uuid[3], + uuid[4], uuid[5], + uuid[6], uuid[7], + uuid[8], uuid[9], + uuid[10], uuid[11], uuid[12], uuid[13], uuid[14],uuid[15]); + v->len++; +#endif return 0; } -#define LVM2_LABEL_SIZE 512 -static unsigned int lvm2_calc_crc(const void *buf, unsigned int size) -{ - static const unsigned int crctab[] = { - 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, - 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, - 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, - 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c - }; - unsigned int i, crc = 0xf597a6cf; - const __u8 *data = (const __u8 *) buf; - - for (i = 0; i < size; i++) { - crc ^= *data++; - crc = (crc >> 4) ^ crctab[crc & 0xf]; - crc = (crc >> 4) ^ crctab[crc & 0xf]; - } - return crc; -} - -static int probe_lvm2(struct blkid_probe *probe, - struct blkid_magic *id, - unsigned char *buf) +int blkid_probe_get_value(blkid_probe pr, int num, const char **name, + unsigned char **data, size_t *len) { - int sector = (id->bim_kboff) << 1; - struct lvm2_pv_label_header *label= (struct lvm2_pv_label_header *)buf; - char *p, *q, uuid[40]; - unsigned int i, b; - - /* buf is at 0k or 1k offset; find label inside */ - if (memcmp(buf, "LABELONE", 8) == 0) { - label = (struct lvm2_pv_label_header *)buf; - } else if (memcmp(buf + 512, "LABELONE", 8) == 0) { - label = (struct lvm2_pv_label_header *)(buf + 512); - sector++; - } else { - return 1; - } + struct blkid_prval *v; - if (blkid_le64(label->sector_xl) != (unsigned) sector) { - DBG(DEBUG_PROBE, - printf("LVM2: label for sector %llu found at sector %d\n", - blkid_le64(label->sector_xl), sector)); - return 1; - } - - if (lvm2_calc_crc(&label->offset_xl, LVM2_LABEL_SIZE - - ((char *)&label->offset_xl - (char *)label)) != - blkid_le32(label->crc_xl)) { - DBG(DEBUG_PROBE, - printf("LVM2: label checksum incorrect at sector %d\n", - sector)); - return 1; - } - - for (i=0, b=1, p=uuid, q= (char *) label->pv_uuid; i <= 32; - i++, b <<= 1) { - if (b & 0x4444440) - *p++ = '-'; - *p++ = *q++; - } - - blkid_set_tag(probe->dev, "UUID", uuid, LVM2_ID_LEN+6); + if (pr == NULL || num < 0 || num >= pr->nvals) + return -1; + v = &pr->vals[num]; + if (name) + *name = v->name; + if (data) + *data = v->data; + if (len) + *len = v->len; return 0; } -/* - * Various filesystem magics that we can check for. Note that kboff and - * sboff are in kilobytes and bytes respectively. All magics are in - * byte strings so we don't worry about endian issues. - */ -static struct blkid_magic type_array[] = { -/* type kboff sboff len magic probe */ - { "oracleasm", 0, 32, 8, "ORCLDISK", probe_oracleasm }, - { "ntfs", 0, 3, 8, "NTFS ", probe_ntfs }, - { "jbd", 1, 0x38, 2, "\123\357", probe_jbd }, - { "ext4dev", 1, 0x38, 2, "\123\357", probe_ext4dev }, - { "ext4", 1, 0x38, 2, "\123\357", probe_ext4 }, - { "ext3", 1, 0x38, 2, "\123\357", probe_ext3 }, - { "ext2", 1, 0x38, 2, "\123\357", probe_ext2 }, - { "reiserfs", 8, 0x34, 8, "ReIsErFs", probe_reiserfs }, - { "reiserfs", 64, 0x34, 9, "ReIsEr2Fs", probe_reiserfs }, - { "reiserfs", 64, 0x34, 9, "ReIsEr3Fs", probe_reiserfs }, - { "reiserfs", 64, 0x34, 8, "ReIsErFs", probe_reiserfs }, - { "reiserfs", 8, 20, 8, "ReIsErFs", probe_reiserfs }, - { "reiser4", 64, 0, 7, "ReIsEr4", probe_reiserfs4 }, - { "gfs2", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs2 }, - { "gfs", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs }, - { "vfat", 0, 0x52, 5, "MSWIN", probe_fat }, - { "vfat", 0, 0x52, 8, "FAT32 ", probe_fat }, - { "vfat", 0, 0x36, 5, "MSDOS", probe_fat }, - { "vfat", 0, 0x36, 8, "FAT16 ", probe_fat }, - { "vfat", 0, 0x36, 8, "FAT12 ", probe_fat }, - { "vfat", 0, 0, 1, "\353", probe_fat_nomagic }, - { "vfat", 0, 0, 1, "\351", probe_fat_nomagic }, - { "vfat", 0, 0x1fe, 2, "\125\252", probe_fat_nomagic }, - { "minix", 1, 0x10, 2, "\177\023", 0 }, - { "minix", 1, 0x10, 2, "\217\023", 0 }, - { "minix", 1, 0x10, 2, "\150\044", 0 }, - { "minix", 1, 0x10, 2, "\170\044", 0 }, - { "vxfs", 1, 0, 4, "\365\374\001\245", 0 }, - { "xfs", 0, 0, 4, "XFSB", probe_xfs }, - { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs }, - { "bfs", 0, 0, 4, "\316\372\173\033", 0 }, - { "cramfs", 0, 0, 4, "E=\315\050", probe_cramfs }, - { "qnx4", 0, 4, 6, "QNX4FS", 0 }, - { "udf", 32, 1, 5, "BEA01", probe_udf }, - { "udf", 32, 1, 5, "BOOT2", probe_udf }, - { "udf", 32, 1, 5, "CD001", probe_udf }, - { "udf", 32, 1, 5, "CDW02", probe_udf }, - { "udf", 32, 1, 5, "NSR02", probe_udf }, - { "udf", 32, 1, 5, "NSR03", probe_udf }, - { "udf", 32, 1, 5, "TEA01", probe_udf }, - { "iso9660", 32, 1, 5, "CD001", probe_iso9660 }, - { "iso9660", 32, 9, 5, "CDROM", probe_iso9660 }, - { "jfs", 32, 0, 4, "JFS1", probe_jfs }, - { "zfs", 8, 0, 8, "\0\0\x02\xf5\xb0\x07\xb1\x0c", probe_zfs }, - { "zfs", 8, 0, 8, "\x0c\xb1\x07\xb0\xf5\x02\0\0", probe_zfs }, - { "zfs", 264, 0, 8, "\0\0\x02\xf5\xb0\x07\xb1\x0c", probe_zfs }, - { "zfs", 264, 0, 8, "\x0c\xb1\x07\xb0\xf5\x02\0\0", probe_zfs }, - { "hfsplus", 1, 0, 2, "BD", probe_hfsplus }, - { "hfsplus", 1, 0, 2, "H+", probe_hfsplus }, - { "hfsplus", 1, 0, 2, "HX", probe_hfsplus }, - { "hfs", 1, 0, 2, "BD", probe_hfs }, - { "ufs", 8, 0x55c, 4, "T\031\001\000", 0 }, - { "hpfs", 8, 0, 4, "I\350\225\371", 0 }, - { "sysv", 0, 0x3f8, 4, "\020~\030\375", 0 }, - { "swap", 0, 0xff6, 10, "SWAP-SPACE", probe_swap0 }, - { "swap", 0, 0xff6, 10, "SWAPSPACE2", probe_swap1 }, - { "swsuspend", 0, 0xff6, 9, "S1SUSPEND", probe_swap1 }, - { "swsuspend", 0, 0xff6, 9, "S2SUSPEND", probe_swap1 }, - { "swap", 0, 0x1ff6, 10, "SWAP-SPACE", probe_swap0 }, - { "swap", 0, 0x1ff6, 10, "SWAPSPACE2", probe_swap1 }, - { "swsuspend", 0, 0x1ff6, 9, "S1SUSPEND", probe_swap1 }, - { "swsuspend", 0, 0x1ff6, 9, "S2SUSPEND", probe_swap1 }, - { "swap", 0, 0x3ff6, 10, "SWAP-SPACE", probe_swap0 }, - { "swap", 0, 0x3ff6, 10, "SWAPSPACE2", probe_swap1 }, - { "swsuspend", 0, 0x3ff6, 9, "S1SUSPEND", probe_swap1 }, - { "swsuspend", 0, 0x3ff6, 9, "S2SUSPEND", probe_swap1 }, - { "swap", 0, 0x7ff6, 10, "SWAP-SPACE", probe_swap0 }, - { "swap", 0, 0x7ff6, 10, "SWAPSPACE2", probe_swap1 }, - { "swsuspend", 0, 0x7ff6, 9, "S1SUSPEND", probe_swap1 }, - { "swsuspend", 0, 0x7ff6, 9, "S2SUSPEND", probe_swap1 }, - { "swap", 0, 0xfff6, 10, "SWAP-SPACE", probe_swap0 }, - { "swap", 0, 0xfff6, 10, "SWAPSPACE2", probe_swap1 }, - { "swsuspend", 0, 0xfff6, 9, "S1SUSPEND", probe_swap1 }, - { "swsuspend", 0, 0xfff6, 9, "S2SUSPEND", probe_swap1 }, - { "ocfs", 0, 8, 9, "OracleCFS", probe_ocfs }, - { "ocfs2", 1, 0, 6, "OCFSV2", probe_ocfs2 }, - { "ocfs2", 2, 0, 6, "OCFSV2", probe_ocfs2 }, - { "ocfs2", 4, 0, 6, "OCFSV2", probe_ocfs2 }, - { "ocfs2", 8, 0, 6, "OCFSV2", probe_ocfs2 }, - { "crypt_LUKS", 0, 0, 6, "LUKS\xba\xbe", probe_luks }, - { "squashfs", 0, 0, 4, "sqsh", 0 }, - { "squashfs", 0, 0, 4, "hsqs", 0 }, - { "lvm2pv", 0, 0x218, 8, "LVM2 001", probe_lvm2 }, - { "lvm2pv", 0, 0x018, 8, "LVM2 001", probe_lvm2 }, - { "lvm2pv", 1, 0x018, 8, "LVM2 001", probe_lvm2 }, - { "lvm2pv", 1, 0x218, 8, "LVM2 001", probe_lvm2 }, - { NULL, 0, 0, 0, NULL, NULL } -}; -/* - * Verify that the data in dev is consistent with what is on the actual - * block device (using the devname field only). Normally this will be - * called when finding items in the cache, but for long running processes - * is also desirable to revalidate an item before use. - * - * If we are unable to revalidate the data, we return the old data and - * do not set the BLKID_BID_FL_VERIFIED flag on it. - */ -blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev) +int blkid_probe_lookup_value(blkid_probe pr, const char *name, + unsigned char **data, size_t *len) { - struct blkid_magic *id; - struct blkid_probe probe; - blkid_tag_iterate iter; - unsigned char *buf; - const char *type, *value; - struct stat st; - time_t diff, now; - int idx; - - if (!dev) - return NULL; - - now = time(0); - diff = now - dev->bid_time; - - if (stat(dev->bid_name, &st) < 0) { - DBG(DEBUG_PROBE, - printf("blkid_verify: error %s (%d) while " - "trying to stat %s\n", strerror(errno), errno, - dev->bid_name)); - open_err: - if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) { - /* We don't have read permission, just return cache data. */ - DBG(DEBUG_PROBE, printf("returning unverified data for %s\n", - dev->bid_name)); - return dev; - } - blkid_free_dev(dev); - return NULL; - } - - if ((now >= dev->bid_time) && - (st.st_mtime <= dev->bid_time) && - ((diff < BLKID_PROBE_MIN) || - (dev->bid_flags & BLKID_BID_FL_VERIFIED && - diff < BLKID_PROBE_INTERVAL))) - return dev; - - DBG(DEBUG_PROBE, - printf("need to revalidate %s (cache time %lu, stat time %lu,\n\t" - "time since last check %lu)\n", - dev->bid_name, (unsigned long)dev->bid_time, - (unsigned long)st.st_mtime, (unsigned long)diff)); - - if ((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) { - DBG(DEBUG_PROBE, printf("blkid_verify: error %s (%d) while " - "opening %s\n", strerror(errno), errno, - dev->bid_name)); - goto open_err; - } - - probe.cache = cache; - probe.dev = dev; - probe.sbbuf = 0; - probe.buf = 0; - probe.buf_max = 0; - - /* - * Iterate over the type array. If we already know the type, - * then try that first. If it doesn't work, then blow away - * the type information, and try again. - * - */ -try_again: - type = 0; - if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) { - uuid_t uuid; - - if (check_mdraid(probe.fd, uuid) == 0) { - set_uuid(dev, uuid, 0); - type = "mdraid"; - goto found_type; - } - } - for (id = type_array; id->bim_type; id++) { - if (dev->bid_type && - strcmp(id->bim_type, dev->bid_type)) - continue; + int i; - idx = id->bim_kboff + (id->bim_sboff >> 10); - buf = get_buffer(&probe, idx << 10, 1024); - if (!buf) - continue; + if (pr == NULL || pr->nvals == 0 || name == NULL) + return -1; - if (memcmp(id->bim_magic, buf + (id->bim_sboff & 0x3ff), - id->bim_len)) - continue; + for (i = 0; i < pr->nvals; i++) { + struct blkid_prval *v = &pr->vals[i]; - if ((id->bim_probe == NULL) || - (id->bim_probe(&probe, id, buf) == 0)) { - type = id->bim_type; - goto found_type; + if (v->name && strcmp(name, v->name) == 0) { + if (data) + *data = v->data; + if (len) + *len = v->len; + return 0; } } - - if (!id->bim_type && dev->bid_type) { - /* - * Zap the device filesystem information and try again - */ - DBG(DEBUG_PROBE, - printf("previous fs type %s not valid, " - "trying full probe\n", dev->bid_type)); - iter = blkid_tag_iterate_begin(dev); - while (blkid_tag_next(iter, &type, &value) == 0) - blkid_set_tag(dev, type, 0, 0); - blkid_tag_iterate_end(iter); - goto try_again; - } - - if (!dev->bid_type) { - blkid_free_dev(dev); - dev = 0; - goto found_type; - } - -found_type: - if (dev && type) { - dev->bid_devno = st.st_rdev; - dev->bid_time = time(0); - dev->bid_flags |= BLKID_BID_FL_VERIFIED; - cache->bic_flags |= BLKID_BIC_FL_CHANGED; - - blkid_set_tag(dev, "TYPE", type, 0); - - DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n", - dev->bid_name, (long long)st.st_rdev, type)); - } - - if (probe.sbbuf) - free(probe.sbbuf); - if (probe.buf) - free(probe.buf); - if (probe.fd >= 0) - close(probe.fd); - - return dev; + return -1; } -int blkid_known_fstype(const char *fstype) +int blkid_probe_has_value(blkid_probe pr, const char *name) { - struct blkid_magic *id; - - for (id = type_array; id->bim_type; id++) { - if (strcmp(fstype, id->bim_type) == 0) - return 1; - } + if (blkid_probe_lookup_value(pr, name, NULL, NULL) == 0) + return 1; return 0; } -#ifdef TEST_PROGRAM -int main(int argc, char **argv) -{ - blkid_dev dev; - blkid_cache cache; - int ret; - - if (argc != 2) { - fprintf(stderr, "Usage: %s device\n" - "Probe a single device to determine type\n", argv[0]); - exit(1); - } - if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) { - fprintf(stderr, "%s: error creating cache (%d)\n", - argv[0], ret); - exit(1); - } - dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL); - if (!dev) { - printf("%s: %s has an unsupported type\n", argv[0], argv[1]); - return (1); - } - printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)"); - if (dev->bid_label) - printf("LABEL='%s'\n", dev->bid_label); - if (dev->bid_uuid) - printf("UUID='%s'\n", dev->bid_uuid); - - blkid_free_dev(dev); - return (0); -} -#endif diff --git a/libs/blkid/src/probe.h b/libs/blkid/src/probe.h deleted file mode 100644 index bee5a518..00000000 --- a/libs/blkid/src/probe.h +++ /dev/null @@ -1,711 +0,0 @@ -/* - * probe.h - constants and on-disk structures for extracting device data - * - * Copyright (C) 1999 by Andries Brouwer - * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o - * Copyright (C) 2001 by Andreas Dilger - * - * %Begin-Header% - * This file may be redistributed under the terms of the - * GNU Lesser General Public License. - * %End-Header% - */ - -#ifndef _BLKID_PROBE_H -#define _BLKID_PROBE_H - -#include - -struct blkid_magic; - -#define SB_BUFFER_SIZE 0x11000 - -struct blkid_probe { - int fd; - blkid_cache cache; - blkid_dev dev; - unsigned char *sbbuf; - size_t sb_valid; - unsigned char *buf; - size_t buf_max; -}; - -typedef int (*blkid_probe_t)(struct blkid_probe *probe, - struct blkid_magic *id, unsigned char *buf); - -struct blkid_magic { - const char *bim_type; /* type name for this magic */ - long bim_kboff; /* kilobyte offset of superblock */ - unsigned bim_sboff; /* byte offset within superblock */ - unsigned bim_len; /* length of magic */ - const char *bim_magic; /* magic string */ - blkid_probe_t bim_probe; /* probe function */ -}; - -/* - * Structures for each of the content types we want to extract information - * from. We do not necessarily need the magic field here, because we have - * already identified the content type before we get this far. It may still - * be useful if there are probe functions which handle multiple content types. - */ -struct ext2_super_block { - __u32 s_inodes_count; - __u32 s_blocks_count; - __u32 s_r_blocks_count; - __u32 s_free_blocks_count; - __u32 s_free_inodes_count; - __u32 s_first_data_block; - __u32 s_log_block_size; - __u32 s_dummy3[7]; - unsigned char s_magic[2]; - __u16 s_state; - __u32 s_dummy5[8]; - __u32 s_feature_compat; - __u32 s_feature_incompat; - __u32 s_feature_ro_compat; - unsigned char s_uuid[16]; - char s_volume_name[16]; - char s_last_mounted[64]; - __u32 s_algorithm_usage_bitmap; - __u8 s_prealloc_blocks; - __u8 s_prealloc_dir_blocks; - __u16 s_reserved_gdt_blocks; - __u8 s_journal_uuid[16]; - __u32 s_journal_inum; - __u32 s_journal_dev; - __u32 s_last_orphan; - __u32 s_hash_seed[4]; - __u8 s_def_hash_version; - __u8 s_jnl_backup_type; - __u16 s_reserved_word_pad; - __u32 s_default_mount_opts; - __u32 s_first_meta_bg; - __u32 s_mkfs_time; - __u32 s_jnl_blocks[17]; - __u32 s_blocks_count_hi; - __u32 s_r_blocks_count_hi; - __u32 s_free_blocks_hi; - __u16 s_min_extra_isize; - __u16 s_want_extra_isize; - __u32 s_flags; - __u16 s_raid_stride; - __u16 s_mmp_interval; - __u64 s_mmp_block; - __u32 s_raid_stripe_width; - __u32 s_reserved[163]; -}; - -/* for s_flags */ -#define EXT2_FLAGS_TEST_FILESYS 0x0004 - -/* for s_feature_compat */ -#define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004 - -/* for s_feature_ro_compat */ -#define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001 -#define EXT2_FEATURE_RO_COMPAT_LARGE_FILE 0x0002 -#define EXT2_FEATURE_RO_COMPAT_BTREE_DIR 0x0004 -#define EXT4_FEATURE_RO_COMPAT_HUGE_FILE 0x0008 -#define EXT4_FEATURE_RO_COMPAT_GDT_CSUM 0x0010 -#define EXT4_FEATURE_RO_COMPAT_DIR_NLINK 0x0020 -#define EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE 0x0040 - -/* for s_feature_incompat */ -#define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002 -#define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 -#define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 -#define EXT2_FEATURE_INCOMPAT_META_BG 0x0010 -#define EXT4_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */ -#define EXT4_FEATURE_INCOMPAT_64BIT 0x0080 -#define EXT4_FEATURE_INCOMPAT_MMP 0x0100 -#define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200 - -#define EXT2_FEATURE_RO_COMPAT_SUPP (EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER| \ - EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \ - EXT2_FEATURE_RO_COMPAT_BTREE_DIR) -#define EXT2_FEATURE_INCOMPAT_SUPP (EXT2_FEATURE_INCOMPAT_FILETYPE| \ - EXT2_FEATURE_INCOMPAT_META_BG) -#define EXT2_FEATURE_INCOMPAT_UNSUPPORTED ~EXT2_FEATURE_INCOMPAT_SUPP -#define EXT2_FEATURE_RO_COMPAT_UNSUPPORTED ~EXT2_FEATURE_RO_COMPAT_SUPP - -#define EXT3_FEATURE_RO_COMPAT_SUPP (EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER| \ - EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \ - EXT2_FEATURE_RO_COMPAT_BTREE_DIR) -#define EXT3_FEATURE_INCOMPAT_SUPP (EXT2_FEATURE_INCOMPAT_FILETYPE| \ - EXT3_FEATURE_INCOMPAT_RECOVER| \ - EXT2_FEATURE_INCOMPAT_META_BG) -#define EXT3_FEATURE_INCOMPAT_UNSUPPORTED ~EXT3_FEATURE_INCOMPAT_SUPP -#define EXT3_FEATURE_RO_COMPAT_UNSUPPORTED ~EXT3_FEATURE_RO_COMPAT_SUPP - - -struct xfs_super_block { - unsigned char xs_magic[4]; - __u32 xs_blocksize; - __u64 xs_dblocks; - __u64 xs_rblocks; - __u32 xs_dummy1[2]; - unsigned char xs_uuid[16]; - __u32 xs_dummy2[15]; - char xs_fname[12]; - __u32 xs_dummy3[2]; - __u64 xs_icount; - __u64 xs_ifree; - __u64 xs_fdblocks; -}; - -struct reiserfs_super_block { - __u32 rs_blocks_count; - __u32 rs_free_blocks; - __u32 rs_root_block; - __u32 rs_journal_block; - __u32 rs_journal_dev; - __u32 rs_orig_journal_size; - __u32 rs_dummy2[5]; - __u16 rs_blocksize; - __u16 rs_dummy3[3]; - unsigned char rs_magic[12]; - __u32 rs_dummy4[5]; - unsigned char rs_uuid[16]; - char rs_label[16]; -}; - -struct reiser4_super_block { - unsigned char rs4_magic[16]; - __u16 rs4_dummy[2]; - unsigned char rs4_uuid[16]; - unsigned char rs4_label[16]; - __u64 rs4_dummy2; -}; - -struct jfs_super_block { - unsigned char js_magic[4]; - __u32 js_version; - __u64 js_size; - __u32 js_bsize; /* 4: aggregate block size in bytes */ - __u16 js_l2bsize; /* 2: log2 of s_bsize */ - __u16 js_l2bfactor; /* 2: log2(s_bsize/hardware block size) */ - __u32 js_pbsize; /* 4: hardware/LVM block size in bytes */ - __u16 js_l2pbsize; /* 2: log2 of s_pbsize */ - __u16 js_pad; /* 2: padding necessary for alignment */ - __u32 js_dummy2[26]; - unsigned char js_uuid[16]; - unsigned char js_label[16]; - unsigned char js_loguuid[16]; -}; - -struct romfs_super_block { - unsigned char ros_magic[8]; - __u32 ros_dummy1[2]; - unsigned char ros_volume[16]; -}; - -struct cramfs_super_block { - __u8 magic[4]; - __u32 size; - __u32 flags; - __u32 future; - __u8 signature[16]; - struct cramfs_info { - __u32 crc; - __u32 edition; - __u32 blocks; - __u32 files; - } info; - __u8 name[16]; -}; - -struct swap_id_block { -/* unsigned char sws_boot[1024]; */ - __u32 sws_version; - __u32 sws_lastpage; - __u32 sws_nrbad; - unsigned char sws_uuid[16]; - char sws_volume[16]; - unsigned char sws_pad[117]; - __u32 sws_badpg; -}; - -/* Yucky misaligned values */ -struct vfat_super_block { -/* 00*/ unsigned char vs_ignored[3]; -/* 03*/ unsigned char vs_sysid[8]; -/* 0b*/ unsigned char vs_sector_size[2]; -/* 0d*/ __u8 vs_cluster_size; -/* 0e*/ __u16 vs_reserved; -/* 10*/ __u8 vs_fats; -/* 11*/ unsigned char vs_dir_entries[2]; -/* 13*/ unsigned char vs_sectors[2]; -/* 15*/ unsigned char vs_media; -/* 16*/ __u16 vs_fat_length; -/* 18*/ __u16 vs_secs_track; -/* 1a*/ __u16 vs_heads; -/* 1c*/ __u32 vs_hidden; -/* 20*/ __u32 vs_total_sect; -/* 24*/ __u32 vs_fat32_length; -/* 28*/ __u16 vs_flags; -/* 2a*/ __u8 vs_version[2]; -/* 2c*/ __u32 vs_root_cluster; -/* 30*/ __u16 vs_insfo_sector; -/* 32*/ __u16 vs_backup_boot; -/* 34*/ __u16 vs_reserved2[6]; -/* 40*/ unsigned char vs_unknown[3]; -/* 43*/ unsigned char vs_serno[4]; -/* 47*/ unsigned char vs_label[11]; -/* 52*/ unsigned char vs_magic[8]; -/* 5a*/ unsigned char vs_dummy2[164]; -/*1fe*/ unsigned char vs_pmagic[2]; -}; - -/* Yucky misaligned values */ -struct msdos_super_block { -/* 00*/ unsigned char ms_ignored[3]; -/* 03*/ unsigned char ms_sysid[8]; -/* 0b*/ unsigned char ms_sector_size[2]; -/* 0d*/ __u8 ms_cluster_size; -/* 0e*/ __u16 ms_reserved; -/* 10*/ __u8 ms_fats; -/* 11*/ unsigned char ms_dir_entries[2]; -/* 13*/ unsigned char ms_sectors[2]; -/* 15*/ unsigned char ms_media; -/* 16*/ __u16 ms_fat_length; -/* 18*/ __u16 ms_secs_track; -/* 1a*/ __u16 ms_heads; -/* 1c*/ __u32 ms_hidden; -/* 20*/ __u32 ms_total_sect; -/* 24*/ unsigned char ms_unknown[3]; -/* 27*/ unsigned char ms_serno[4]; -/* 2b*/ unsigned char ms_label[11]; -/* 36*/ unsigned char ms_magic[8]; -/* 3d*/ unsigned char ms_dummy2[192]; -/*1fe*/ unsigned char ms_pmagic[2]; -}; - -struct vfat_dir_entry { - __u8 name[11]; - __u8 attr; - __u16 time_creat; - __u16 date_creat; - __u16 time_acc; - __u16 date_acc; - __u16 cluster_high; - __u16 time_write; - __u16 date_write; - __u16 cluster_low; - __u32 size; -}; - -/* maximum number of clusters */ -#define FAT12_MAX 0xFF4 -#define FAT16_MAX 0xFFF4 -#define FAT32_MAX 0x0FFFFFF6 - -struct minix_super_block { - __u16 ms_ninodes; - __u16 ms_nzones; - __u16 ms_imap_blocks; - __u16 ms_zmap_blocks; - __u16 ms_firstdatazone; - __u16 ms_log_zone_size; - __u32 ms_max_size; - unsigned char ms_magic[2]; - __u16 ms_state; - __u32 ms_zones; -}; - -struct mdp_superblock_s { - __u32 md_magic; - __u32 major_version; - __u32 minor_version; - __u32 patch_version; - __u32 gvalid_words; - __u32 set_uuid0; - __u32 ctime; - __u32 level; - __u32 size; - __u32 nr_disks; - __u32 raid_disks; - __u32 md_minor; - __u32 not_persistent; - __u32 set_uuid1; - __u32 set_uuid2; - __u32 set_uuid3; -}; - -struct hfs_super_block { - char h_magic[2]; - char h_dummy[18]; - __u32 h_blksize; -}; - -struct ocfs_volume_header { - unsigned char minor_version[4]; - unsigned char major_version[4]; - unsigned char signature[128]; - char mount[128]; - unsigned char mount_len[2]; -}; - -struct ocfs_volume_label { - unsigned char disk_lock[48]; - char label[64]; - unsigned char label_len[2]; - unsigned char vol_id[16]; - unsigned char vol_id_len[2]; -}; - -#define ocfsmajor(o) ((__u32)o.major_version[0] \ - + (((__u32) o.major_version[1]) << 8) \ - + (((__u32) o.major_version[2]) << 16) \ - + (((__u32) o.major_version[3]) << 24)) -#define ocfslabellen(o) ((__u32)o.label_len[0] + (((__u32) o.label_len[1]) << 8)) -#define ocfsmountlen(o) ((__u32)o.mount_len[0] + (((__u32) o.mount_len[1])<<8)) - -#define OCFS_MAGIC "OracleCFS" - -struct ocfs2_super_block { - unsigned char signature[8]; - unsigned char s_dummy1[184]; - unsigned char s_dummy2[80]; - char s_label[64]; - unsigned char s_uuid[16]; -}; - -#define OCFS2_MIN_BLOCKSIZE 512 -#define OCFS2_MAX_BLOCKSIZE 4096 - -#define OCFS2_SUPER_BLOCK_BLKNO 2 - -#define OCFS2_SUPER_BLOCK_SIGNATURE "OCFSV2" - -struct oracle_asm_disk_label { - char dummy[32]; - char dl_tag[8]; - char dl_id[24]; -}; - -#define ORACLE_ASM_DISK_LABEL_MARKED "ORCLDISK" -#define ORACLE_ASM_DISK_LABEL_OFFSET 32 - -struct iso_volume_descriptor { - unsigned char vd_type; - unsigned char vd_id[5]; - unsigned char vd_version; - unsigned char flags; - unsigned char system_id[32]; - unsigned char volume_id[32]; - unsigned char unused[8]; - unsigned char space_size[8]; - unsigned char escape_sequences[8]; -}; - -/* Common gfs/gfs2 constants: */ -#define GFS_MAGIC 0x01161970 -#define GFS_DEFAULT_BSIZE 4096 -#define GFS_SUPERBLOCK_OFFSET (0x10 * GFS_DEFAULT_BSIZE) -#define GFS_METATYPE_SB 1 -#define GFS_FORMAT_SB 100 -#define GFS_LOCKNAME_LEN 64 - -/* gfs1 constants: */ -#define GFS_FORMAT_FS 1309 -#define GFS_FORMAT_MULTI 1401 -/* gfs2 constants: */ -#define GFS2_FORMAT_FS 1801 -#define GFS2_FORMAT_MULTI 1900 - -struct gfs2_meta_header { - __u32 mh_magic; - __u32 mh_type; - __u64 __pad0; /* Was generation number in gfs1 */ - __u32 mh_format; - __u32 __pad1; /* Was incarnation number in gfs1 */ -}; - -struct gfs2_inum { - __u64 no_formal_ino; - __u64 no_addr; -}; - -struct gfs2_sb { - struct gfs2_meta_header sb_header; - - __u32 sb_fs_format; - __u32 sb_multihost_format; - __u32 __pad0; /* Was superblock flags in gfs1 */ - - __u32 sb_bsize; - __u32 sb_bsize_shift; - __u32 __pad1; /* Was journal segment size in gfs1 */ - - struct gfs2_inum sb_master_dir; /* Was jindex dinode in gfs1 */ - struct gfs2_inum __pad2; /* Was rindex dinode in gfs1 */ - struct gfs2_inum sb_root_dir; - - char sb_lockproto[GFS_LOCKNAME_LEN]; - char sb_locktable[GFS_LOCKNAME_LEN]; - /* In gfs1, quota and license dinodes followed */ -}; - -struct ntfs_super_block { - __u8 jump[3]; - __u8 oem_id[8]; - __u8 bios_parameter_block[25]; - __u16 unused[2]; - __u64 number_of_sectors; - __u64 mft_cluster_location; - __u64 mft_mirror_cluster_location; - __s8 cluster_per_mft_record; - __u8 reserved1[3]; - __s8 cluster_per_index_record; - __u8 reserved2[3]; - __u64 volume_serial; - __u16 checksum; -}; - -struct master_file_table_record { - __u32 magic; - __u16 usa_ofs; - __u16 usa_count; - __u64 lsn; - __u16 sequence_number; - __u16 link_count; - __u16 attrs_offset; - __u16 flags; - __u32 bytes_in_use; - __u32 bytes_allocated; -} __attribute__((__packed__)); - -struct file_attribute { - __u32 type; - __u32 len; - __u8 non_resident; - __u8 name_len; - __u16 name_offset; - __u16 flags; - __u16 instance; - __u32 value_len; - __u16 value_offset; -} __attribute__((__packed__)); - -#define MFT_RECORD_VOLUME 3 -#define MFT_RECORD_ATTR_VOLUME_NAME 0x60 -#define MFT_RECORD_ATTR_VOLUME_INFO 0x70 -#define MFT_RECORD_ATTR_OBJECT_ID 0x40 -#define MFT_RECORD_ATTR_END 0xffffffffu - -/* HFS / HFS+ */ -struct hfs_finder_info { - __u32 boot_folder; - __u32 start_app; - __u32 open_folder; - __u32 os9_folder; - __u32 reserved; - __u32 osx_folder; - __u8 id[8]; -} __attribute__((packed)); - -struct hfs_mdb { - __u8 signature[2]; - __u32 cr_date; - __u32 ls_Mod; - __u16 atrb; - __u16 nm_fls; - __u16 vbm_st; - __u16 alloc_ptr; - __u16 nm_al_blks; - __u32 al_blk_size; - __u32 clp_size; - __u16 al_bl_st; - __u32 nxt_cnid; - __u16 free_bks; - __u8 label_len; - __u8 label[27]; - __u32 vol_bkup; - __u16 vol_seq_num; - __u32 wr_cnt; - __u32 xt_clump_size; - __u32 ct_clump_size; - __u16 num_root_dirs; - __u32 file_count; - __u32 dir_count; - struct hfs_finder_info finder_info; - __u8 embed_sig[2]; - __u16 embed_startblock; - __u16 embed_blockcount; -} __attribute__((packed)); - - -#define HFS_NODE_LEAF 0xff -#define HFSPLUS_POR_CNID 1 - -struct hfsplus_bnode_descriptor { - __u32 next; - __u32 prev; - __u8 type; - __u8 height; - __u16 num_recs; - __u16 reserved; -} __attribute__((packed)); - -struct hfsplus_bheader_record { - __u16 depth; - __u32 root; - __u32 leaf_count; - __u32 leaf_head; - __u32 leaf_tail; - __u16 node_size; -} __attribute__((packed)); - -struct hfsplus_catalog_key { - __u16 key_len; - __u32 parent_id; - __u16 unicode_len; - __u8 unicode[255 * 2]; -} __attribute__((packed)); - -struct hfsplus_extent { - __u32 start_block; - __u32 block_count; -} __attribute__((packed)); - -#define HFSPLUS_EXTENT_COUNT 8 -struct hfsplus_fork { - __u64 total_size; - __u32 clump_size; - __u32 total_blocks; - struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT]; -} __attribute__((packed)); - -struct hfsplus_vol_header { - __u8 signature[2]; - __u16 version; - __u32 attributes; - __u32 last_mount_vers; - __u32 reserved; - __u32 create_date; - __u32 modify_date; - __u32 backup_date; - __u32 checked_date; - __u32 file_count; - __u32 folder_count; - __u32 blocksize; - __u32 total_blocks; - __u32 free_blocks; - __u32 next_alloc; - __u32 rsrc_clump_sz; - __u32 data_clump_sz; - __u32 next_cnid; - __u32 write_count; - __u64 encodings_bmp; - struct hfs_finder_info finder_info; - struct hfsplus_fork alloc_file; - struct hfsplus_fork ext_file; - struct hfsplus_fork cat_file; - struct hfsplus_fork attr_file; - struct hfsplus_fork start_file; -} __attribute__((packed)); - - -/* this is lvm's label_header & pv_header combined. */ - -#define LVM2_ID_LEN 32 - -struct lvm2_pv_label_header { - /* label_header */ - __u8 id[8]; /* LABELONE */ - __u64 sector_xl; /* Sector number of this label */ - __u32 crc_xl; /* From next field to end of sector */ - __u32 offset_xl; /* Offset from start of struct to contents */ - __u8 type[8]; /* LVM2 001 */ - /* pv_header */ - __u8 pv_uuid[LVM2_ID_LEN]; -} __attribute__ ((packed)); - -/* - * Byte swap functions - */ -#ifdef __GNUC__ -#define _INLINE_ static __inline__ -#else /* For Watcom C */ -#define _INLINE_ static inline -#endif - -static __u16 blkid_swab16(__u16 val); -static __u32 blkid_swab32(__u32 val); -static __u64 blkid_swab64(__u64 val); - -#if ((defined __GNUC__) && \ - (defined(__i386__) || defined(__i486__) || defined(__i586__))) - -#define _BLKID_HAVE_ASM_BITOPS_ - -_INLINE_ __u32 blkid_swab32(__u32 val) -{ -#ifdef EXT2FS_REQUIRE_486 - __asm__("bswap %0" : "=r" (val) : "0" (val)); -#else - __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */ - "rorl $16,%0\n\t" /* swap words */ - "xchgb %b0,%h0" /* swap higher bytes */ - :"=q" (val) - : "0" (val)); -#endif - return val; -} - -_INLINE_ __u16 blkid_swab16(__u16 val) -{ - __asm__("xchgb %b0,%h0" /* swap bytes */ \ - : "=q" (val) \ - : "0" (val)); \ - return val; -} - -_INLINE_ __u64 blkid_swab64(__u64 val) -{ - return (blkid_swab32(val >> 32) | - (((__u64) blkid_swab32(val & 0xFFFFFFFFUL)) << 32)); -} -#endif - -#if !defined(_BLKID_HAVE_ASM_BITOPS_) - -_INLINE_ __u16 blkid_swab16(__u16 val) -{ - return (val >> 8) | (val << 8); -} - -_INLINE_ __u32 blkid_swab32(__u32 val) -{ - return ((val>>24) | ((val>>8)&0xFF00) | - ((val<<8)&0xFF0000) | (val<<24)); -} - -_INLINE_ __u64 blkid_swab64(__u64 val) -{ - return (blkid_swab32(val >> 32) | - (((__u64) blkid_swab32(val & 0xFFFFFFFFUL)) << 32)); -} -#endif - - - -#ifdef WORDS_BIGENDIAN -#define blkid_le16(x) blkid_swab16(x) -#define blkid_le32(x) blkid_swab32(x) -#define blkid_le64(x) blkid_swab64(x) -#define blkid_be16(x) (x) -#define blkid_be32(x) (x) -#define blkid_be64(x) (x) -#else -#define blkid_le16(x) (x) -#define blkid_le32(x) (x) -#define blkid_le64(x) (x) -#define blkid_be16(x) blkid_swab16(x) -#define blkid_be32(x) blkid_swab32(x) -#define blkid_be64(x) blkid_swab64(x) -#endif - -#undef _INLINE_ - -#endif /* _BLKID_PROBE_H */ diff --git a/libs/blkid/src/probers/Makefile.am b/libs/blkid/src/probers/Makefile.am new file mode 100644 index 00000000..31bc2daf --- /dev/null +++ b/libs/blkid/src/probers/Makefile.am @@ -0,0 +1,9 @@ +include $(top_srcdir)/config/include-Makefile.am + +AM_CPPFLAGS += -I$(top_builddir)/libs/blkid/src + +lib_LIBRARIES = libprobers.a +libprobers_a_SOURCES = probers.h cramfs.c swap.c +libprobers_a_CFLAGS = -fPIC + +all-local: $(lib_LIBRARIES) diff --git a/libs/blkid/src/probers/cramfs.c b/libs/blkid/src/probers/cramfs.c new file mode 100644 index 00000000..33b6566f --- /dev/null +++ b/libs/blkid/src/probers/cramfs.c @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2008 Karel Zak + * Copyright (C) 2004-2007 Kay Sievers + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "blkidP.h" + +struct cramfs_super +{ + uint8_t magic[4]; + uint32_t size; + uint32_t flags; + uint32_t future; + uint8_t signature[16]; + struct cramfs_info + { + uint32_t crc; + uint32_t edition; + uint32_t blocks; + uint32_t files; + } info; + uint8_t name[16]; +}; + +static int probe_cramfs(blkid_probe pr, const struct blkid_idmag *mag) +{ + struct cramfs_super *cs; + + cs = (struct cramfs_super *) blkid_probe_get_buffer(pr, 0, 0x200); + if (cs == NULL) + return -1; + + blkid_probe_set_label(pr, cs->name, sizeof(cs->name)); + return 0; +} + +const struct blkid_idinfo cramfs_idinfo = +{ + .name = "cramfs", + .usage = BLKID_USAGE_FILESYSTEM, + .probefunc = probe_cramfs, + .magics = + { + { "\x45\x3d\xcd\x28", 4, 0, 0 }, + { "\x28\xcd\x3d\x45", 4, 0, 0 }, + { NULL } + } +}; + + diff --git a/libs/blkid/src/probers/probers.h b/libs/blkid/src/probers/probers.h new file mode 100644 index 00000000..148800d2 --- /dev/null +++ b/libs/blkid/src/probers/probers.h @@ -0,0 +1,22 @@ +/* + * probe.h - constants and on-disk structures for extracting device data + * + * Copyright (C) 1999 by Andries Brouwer + * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o + * Copyright (C) 2001 by Andreas Dilger + * Copyright (C) 2008 Karel Zak + * + * %Begin-Header% + * This file may be redistributed under the terms of the + * GNU Lesser General Public License. + * %End-Header% + */ + +#ifndef _BLKID_PROBE_H +#define _BLKID_PROBE_H + +extern const struct blkid_idinfo cramfs_idinfo; +extern const struct blkid_idinfo swap_idinfo; +extern const struct blkid_idinfo swsuspend_idinfo; + +#endif /* _BLKID_PROBE_H */ diff --git a/libs/blkid/src/probers/swap.c b/libs/blkid/src/probers/swap.c new file mode 100644 index 00000000..6a887a8b --- /dev/null +++ b/libs/blkid/src/probers/swap.c @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2008 Karel Zak + * + * This is a derivative work from libvolume_id (udev) and libblkid (e2fsprogs). + * + * Copyright (C) 2004 Kay Sievers + * Copyright (C) 1999 by Andries Brouwer + * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o + * Copyright (C) 2001 by Andreas Dilger + * + */ +#include +#include +#include +#include +#include +#include +#include + +#include "blkidP.h" + +/* linux-2.6/include/linux/swap.h */ +struct swap_header_v1_2 { + /* char bootbits[1024]; */ /* Space for disklabel etc. */ + uint32_t version; + uint32_t lastpage; + uint32_t nr_badpages; + unsigned char uuid[16]; + unsigned char volume[16]; + uint32_t padding[117]; + uint32_t badpages[1]; +}; + +#define PAGESIZE_MIN 0xff6 /* 4086 (arm, i386, ...) */ +#define PAGESIZE_MAX 0xfff6 /* 65526 (ia64) */ + + +static int swap_set_info(blkid_probe pr, const char *version) +{ + struct swap_header_v1_2 *hdr; + + /* Swap header always located at offset of 1024 bytes */ + hdr = (struct swap_header_v1_2 *) blkid_probe_get_buffer(pr, 1024, + sizeof(struct swap_header_v1_2)); + if (!hdr) + return -1; + + /* SWAPSPACE2 - check for wrong version or zeroed pagecount */ + if (strcmp(version, "2") == 0 && + (hdr->version != 1 || hdr->lastpage == 0)) + return -1; + + /* arbitrary sanity check.. is there any garbage down there? */ + if (hdr->padding[32] == 0 && hdr->padding[33] == 0) { + if (hdr->volume[0] && blkid_probe_set_label(pr, hdr->volume, + sizeof(hdr->volume)) < 0) + return -1; + if (hdr->uuid[0] && blkid_probe_set_uuid(pr, hdr->uuid) < 0) + return -1; + } + + blkid_probe_set_version(pr, version); + return 0; +} + +static int probe_swap(blkid_probe pr, const struct blkid_idmag *mag) +{ + if (!mag) + return -1; + + if (!memcmp(mag->magic, "SWAP-SPACE", mag->len)) { + /* swap v0 doesn't support LABEL or UUID */ + blkid_probe_set_version(pr, "1"); + return 0; + + } else if (!memcmp(mag->magic, "SWAPSPACE2", mag->len)) + return swap_set_info(pr, "2"); + + return -1; +} + +static int probe_swsuspend(blkid_probe pr, const struct blkid_idmag *mag) +{ + if (!mag) + return -1; + if (!memcmp(mag->magic, "S1SUSPEND", mag->len)) + return swap_set_info(pr, "s1suspend"); + if (!memcmp(mag->magic, "S2SUSPEND", mag->len)) + return swap_set_info(pr, "s2suspend"); + if (!memcmp(mag->magic, "ULSUSPEND", mag->len)) + return swap_set_info(pr, "ulsuspend"); + if (!memcmp(mag->magic, "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", mag->len)) + return swap_set_info(pr, "tuxonice"); + + return -1; /* no signature detected */ +} + +const struct blkid_idinfo swap_idinfo = +{ + .name = "swap", + .usage = BLKID_USAGE_OTHER, + .probefunc = probe_swap, + .magics = + { + { "SWAP-SPACE", 10, 0, 0xff6 }, + { "SWAPSPACE2", 10, 0, 0xff6 }, + { "SWAP-SPACE", 10, 0, 0x1ff6 }, + { "SWAPSPACE2", 10, 0, 0x1ff6 }, + { "SWAP-SPACE", 10, 0, 0x3ff6 }, + { "SWAPSPACE2", 10, 0, 0x3ff6 }, + { "SWAP-SPACE", 10, 0, 0x7ff6 }, + { "SWAPSPACE2", 10, 0, 0x7ff6 }, + { "SWAP-SPACE", 10, 0, 0xfff6 }, + { "SWAPSPACE2", 10, 0, 0xfff6 }, + { NULL } + } +}; + + +const struct blkid_idinfo swsuspend_idinfo = +{ + .name = "swsupend", + .usage = BLKID_USAGE_OTHER, + .probefunc = probe_swsuspend, + .magics = + { + { "S1SUSPEND", 9, 0, 0xff6 }, + { "S2SUSPEND", 9, 0, 0xff6 }, + { "ULSUSPEND", 9, 0, 0xff6 }, + { "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8, 0, 0xff6 }, + { "S1SUSPEND", 9, 0, 0x1ff6 }, + { "S2SUSPEND", 9, 0, 0x1ff6 }, + { "ULSUSPEND", 9, 0, 0x1ff6 }, + { "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8, 0, 0x1ff6 }, + { "S1SUSPEND", 9, 0, 0x3ff6 }, + { "S2SUSPEND", 9, 0, 0x3ff6 }, + { "ULSUSPEND", 9, 0, 0x3ff6 }, + { "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8, 0, 0x3ff6 }, + { "S1SUSPEND", 9, 0, 0x7ff6 }, + { "S2SUSPEND", 9, 0, 0x7ff6 }, + { "ULSUSPEND", 9, 0, 0x7ff6 }, + { "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8, 0, 0x7ff6 }, + { "S1SUSPEND", 9, 0, 0xfff6 }, + { "S2SUSPEND", 9, 0, 0xfff6 }, + { "ULSUSPEND", 9, 0, 0xfff6 }, + { "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8, 0, 0xfff6 }, + { NULL } + } +}; + diff --git a/libs/blkid/src/verify.c b/libs/blkid/src/verify.c new file mode 100644 index 00000000..4db8d7b2 --- /dev/null +++ b/libs/blkid/src/verify.c @@ -0,0 +1,202 @@ +/* + * Copyright (C) 2008 Karel Zak + * + * %Begin-Header% + * This file may be redistributed under the terms of the + * GNU Lesser General Public License. + * %End-Header% + */ + +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#include "blkidP.h" + +static void blkid_probe_to_tags(blkid_probe pr, blkid_dev dev) +{ + unsigned char *data; + const char *name; + int nvals, n; + size_t len; + + nvals = blkid_probe_numof_values(pr); + + for (n = 0; n < nvals; n++) { + if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0) + blkid_set_tag(dev, name, (char *) data, len); + } +} + +/* + * Verify that the data in dev is consistent with what is on the actual + * block device (using the devname field only). Normally this will be + * called when finding items in the cache, but for long running processes + * is also desirable to revalidate an item before use. + * + * If we are unable to revalidate the data, we return the old data and + * do not set the BLKID_BID_FL_VERIFIED flag on it. + */ +blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev) +{ + struct stat st; + time_t diff, now; + char *fltr[2]; + int fd; + + if (!dev) + return NULL; + + now = time(0); + diff = now - dev->bid_time; + + if (stat(dev->bid_name, &st) < 0) { + DBG(DEBUG_PROBE, + printf("blkid_verify: error %s (%d) while " + "trying to stat %s\n", strerror(errno), errno, + dev->bid_name)); + open_err: + if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) { + /* We don't have read permission, just return cache data. */ + DBG(DEBUG_PROBE, printf("returning unverified data for %s\n", + dev->bid_name)); + return dev; + } + blkid_free_dev(dev); + return NULL; + } + + if ((now >= dev->bid_time) && + (st.st_mtime <= dev->bid_time) && + ((diff < BLKID_PROBE_MIN) || + (dev->bid_flags & BLKID_BID_FL_VERIFIED && + diff < BLKID_PROBE_INTERVAL))) + return dev; + + DBG(DEBUG_PROBE, + printf("need to revalidate %s (cache time %lu, stat time %lu,\n\t" + "time since last check %lu)\n", + dev->bid_name, (unsigned long)dev->bid_time, + (unsigned long)st.st_mtime, (unsigned long)diff)); + + + if (!cache->probe) + cache->probe = blkid_new_probe(); + + fd = open(dev->bid_name, O_RDONLY); + if (fd < 0) { + DBG(DEBUG_PROBE, printf("blkid_verify: error %s (%d) while " + "opening %s\n", strerror(errno), errno, + dev->bid_name)); + goto open_err; + } + + if (blkid_probe_set_device(cache->probe, fd, 0, 0)) { + /* failed to read the device */ + close(fd); + blkid_free_dev(dev); + return NULL; + } + + blkid_probe_set_request(cache->probe, + BLKID_PROBREQ_LABEL | BLKID_PROBREQ_UUID | + BLKID_PROBREQ_TYPE | BLKID_PROBREQ_SECTYPE); + + /* + * If we already know the type, then try that first. + */ + if (dev->bid_type) { + blkid_tag_iterate iter; + const char *type, *value; + + fltr[0] = dev->bid_type; + fltr[1] = NULL; + + blkid_probe_filter_types(cache->probe, + BLKID_FLTR_ONLYIN, fltr); + + if (!blkid_do_probe(cache->probe)) + goto found_type; + blkid_probe_invert_filter(cache->probe); + + /* + * Zap the device filesystem information and try again + */ + DBG(DEBUG_PROBE, + printf("previous fs type %s not valid, " + "trying full probe\n", dev->bid_type)); + iter = blkid_tag_iterate_begin(dev); + while (blkid_tag_next(iter, &type, &value) == 0) + blkid_set_tag(dev, type, 0, 0); + blkid_tag_iterate_end(iter); + } + + /* + * Probe for all types. + */ + if (blkid_do_probe(cache->probe)) { + /* found nothing */ + blkid_free_dev(dev); + dev = NULL; + } + +found_type: + if (dev) { + dev->bid_devno = st.st_rdev; + dev->bid_time = time(0); + dev->bid_flags |= BLKID_BID_FL_VERIFIED; + cache->bic_flags |= BLKID_BIC_FL_CHANGED; + + blkid_probe_to_tags(cache->probe, dev); + + DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n", + dev->bid_name, (long long)st.st_rdev, type)); + } + + blkid_reset_probe(cache->probe); + blkid_probe_reset_filter(cache->probe); + close(fd); + return dev; +} + +#ifdef TEST_PROGRAM +int main(int argc, char **argv) +{ + blkid_dev dev; + blkid_cache cache; + int ret; + + if (argc != 2) { + fprintf(stderr, "Usage: %s device\n" + "Probe a single device to determine type\n", argv[0]); + exit(1); + } + if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) { + fprintf(stderr, "%s: error creating cache (%d)\n", + argv[0], ret); + exit(1); + } + dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL); + if (!dev) { + printf("%s: %s has an unsupported type\n", argv[0], argv[1]); + return (1); + } + printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)"); + if (dev->bid_label) + printf("LABEL='%s'\n", dev->bid_label); + if (dev->bid_uuid) + printf("UUID='%s'\n", dev->bid_uuid); + + blkid_free_dev(dev); + return (0); +} +#endif