2 * Low-level libblkid probing API
4 * Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com>
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
12 * @title: Low-level probing
13 * @short_description: low-level prober initialization
15 * The low-level probing routines always and directly read information from
16 * the selected (see blkid_probe_set_device()) device.
18 * The probing routines are grouped together into separate chains. Currently,
19 * the library provides superblocks, partitions and topology chains.
21 * The probing routines is possible to filter (enable/disable) by type (e.g.
22 * fstype "vfat" or partype "gpt") or by usage flags (e.g. BLKID_USAGE_RAID).
23 * These filters are per-chain. Note that always when you touch the chain
24 * filter the current probing position is reset and probing starts from
25 * scratch. It means that the chain filter should not be modified during
26 * probing, for example in loop where you call blkid_do_probe().
28 * For more details see the chain specific documentation.
30 * The low-level API provides two ways how access to probing results.
32 * 1. The NAME=value (tag) interface. This interface is older and returns all data
33 * as strings. This interface is generic for all chains.
35 * 2. The binary interfaces. These interfaces return data in the native formats.
36 * The interface is always specific to the probing chain.
38 * Note that the previous probing result (binary or NAME=value) is always
39 * zeroized when a chain probing function is called. For example:
43 * blkid_probe_enable_partitions(pr, TRUE);
44 * blkid_probe_enable_superblocks(pr, FALSE);
46 * blkid_do_safeprobe(pr);
50 * overwrites the previous probing result for the partitions chain, the superblocks
51 * result is not modified.
55 * SECTION: lowprobe-tags
56 * @title: Low-level tags
57 * @short_description: generic NAME=value interface.
59 * The probing routines inside the chain are mutually exclusive by default --
60 * only few probing routines are marked as "tolerant". The "tolerant" probing
61 * routines are used for filesystem which can share the same device with any
62 * other filesystem. The blkid_do_safeprobe() checks for the "tolerant" flag.
64 * The SUPERBLOCKS chain is enabled by default. The all others chains is
65 * necessary to enable by blkid_probe_enable_'CHAINNAME'(). See chains specific
68 * The blkid_do_probe() function returns a result from only one probing
69 * routine, and the next call from the next probing routine. It means you need
70 * to call the function in loop, for example:
74 * while((blkid_do_probe(pr) == BLKID_PROBE_OK)
79 * The blkid_do_safeprobe() is the same as blkid_do_probe(), but returns only
80 * first probing result for every enabled chain. This function checks for
81 * ambivalent results (e.g. more "intolerant" filesystems superblocks on the
84 * The probing result is set of NAME=value pairs (the NAME is always unique).
93 #include <sys/types.h>
94 #ifdef HAVE_LINUX_CDROM_H
95 #include <linux/cdrom.h>
97 #ifdef HAVE_LINUX_BLKZONED_H
98 #include <linux/blkzoned.h>
100 #ifdef HAVE_SYS_STAT_H
101 #include <sys/stat.h>
106 #ifdef HAVE_LINUX_FD_H
107 #include <linux/fd.h>
109 #include <inttypes.h>
117 #include "strutils.h"
119 #include "fileutils.h"
122 * All supported chains
124 static const struct blkid_chaindrv
*chains_drvs
[] = {
125 [BLKID_CHAIN_SUBLKS
] = &superblocks_drv
,
126 [BLKID_CHAIN_TOPLGY
] = &topology_drv
,
127 [BLKID_CHAIN_PARTS
] = &partitions_drv
130 static void blkid_probe_reset_values(blkid_probe pr
);
135 * Returns: a pointer to the newly allocated probe struct or NULL in case of error.
137 blkid_probe
blkid_new_probe(void)
142 pr
= calloc(1, sizeof(struct blkid_struct_probe
));
146 DBG(LOWPROBE
, ul_debug("allocate a new probe"));
148 /* initialize chains */
149 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
150 pr
->chains
[i
].driver
= chains_drvs
[i
];
151 pr
->chains
[i
].flags
= chains_drvs
[i
]->dflt_flags
;
152 pr
->chains
[i
].enabled
= chains_drvs
[i
]->dflt_enabled
;
154 INIT_LIST_HEAD(&pr
->buffers
);
155 INIT_LIST_HEAD(&pr
->values
);
156 INIT_LIST_HEAD(&pr
->hints
);
161 * Clone @parent, the new clone shares all, but except:
164 * - buffers if another device (or offset) is set to the prober
166 blkid_probe
blkid_clone_probe(blkid_probe parent
)
173 DBG(LOWPROBE
, ul_debug("allocate a probe clone"));
175 pr
= blkid_new_probe();
180 pr
->off
= parent
->off
;
181 pr
->size
= parent
->size
;
182 pr
->devno
= parent
->devno
;
183 pr
->disk_devno
= parent
->disk_devno
;
184 pr
->blkssz
= parent
->blkssz
;
185 pr
->flags
= parent
->flags
;
186 pr
->zone_size
= parent
->zone_size
;
189 pr
->flags
&= ~BLKID_FL_PRIVATE_FD
;
197 * blkid_new_probe_from_filename:
198 * @filename: device or regular file
200 * This function is same as call open(filename), blkid_new_probe() and
201 * blkid_probe_set_device(pr, fd, 0, 0).
203 * The @filename is closed by blkid_free_probe() or by the
204 * blkid_probe_set_device() call.
206 * Returns: a pointer to the newly allocated probe struct or NULL in case of
209 blkid_probe
blkid_new_probe_from_filename(const char *filename
)
212 blkid_probe pr
= NULL
;
214 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
218 pr
= blkid_new_probe();
222 if (blkid_probe_set_device(pr
, fd
, 0, 0))
225 pr
->flags
|= BLKID_FL_PRIVATE_FD
;
229 blkid_free_probe(pr
);
237 * Deallocates the probe struct, buffers and all allocated
238 * data that are associated with this probing control struct.
240 void blkid_free_probe(blkid_probe pr
)
247 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
248 struct blkid_chain
*ch
= &pr
->chains
[i
];
250 if (ch
->driver
->free_data
)
251 ch
->driver
->free_data(pr
, ch
->data
);
256 if ((pr
->flags
& BLKID_FL_PRIVATE_FD
) && pr
->fd
>= 0)
258 blkid_probe_reset_buffers(pr
);
259 blkid_probe_reset_values(pr
);
260 blkid_probe_reset_hints(pr
);
261 blkid_free_probe(pr
->disk_probe
);
263 DBG(LOWPROBE
, ul_debug("free probe"));
267 void blkid_probe_free_value(struct blkid_prval
*v
)
272 list_del(&v
->prvals
);
275 DBG(LOWPROBE
, ul_debug(" free value %s", v
->name
));
280 * Removes chain values from probing result.
282 void blkid_probe_chain_reset_values(blkid_probe pr
, struct blkid_chain
*chn
)
285 struct list_head
*p
, *pnext
;
287 if (list_empty(&pr
->values
))
290 DBG(LOWPROBE
, ul_debug("Resetting %s values", chn
->driver
->name
));
292 list_for_each_safe(p
, pnext
, &pr
->values
) {
293 struct blkid_prval
*v
= list_entry(p
,
294 struct blkid_prval
, prvals
);
297 blkid_probe_free_value(v
);
301 static void blkid_probe_chain_reset_position(struct blkid_chain
*chn
)
307 * Move chain values from probing result to @vals
309 int blkid_probe_chain_save_values(blkid_probe pr
, struct blkid_chain
*chn
,
310 struct list_head
*vals
)
312 struct list_head
*p
, *pnext
;
313 struct blkid_prval
*v
;
315 DBG(LOWPROBE
, ul_debug("saving %s values", chn
->driver
->name
));
317 list_for_each_safe(p
, pnext
, &pr
->values
) {
319 v
= list_entry(p
, struct blkid_prval
, prvals
);
323 list_del_init(&v
->prvals
);
324 list_add_tail(&v
->prvals
, vals
);
330 * Appends values from @vals to the probing result
332 void blkid_probe_append_values_list(blkid_probe pr
, struct list_head
*vals
)
334 DBG(LOWPROBE
, ul_debug("appending values"));
336 list_splice(vals
, &pr
->values
);
337 INIT_LIST_HEAD(vals
);
341 void blkid_probe_free_values_list(struct list_head
*vals
)
346 DBG(LOWPROBE
, ul_debug("freeing values list"));
348 while (!list_empty(vals
)) {
349 struct blkid_prval
*v
= list_entry(vals
->next
, struct blkid_prval
, prvals
);
350 blkid_probe_free_value(v
);
354 struct blkid_chain
*blkid_probe_get_chain(blkid_probe pr
)
356 return pr
->cur_chain
;
359 static const char *blkid_probe_get_probername(blkid_probe pr
)
361 struct blkid_chain
*chn
= blkid_probe_get_chain(pr
);
363 if (chn
&& chn
->idx
>= 0 && (unsigned)chn
->idx
< chn
->driver
->nidinfos
)
364 return chn
->driver
->idinfos
[chn
->idx
]->name
;
369 void *blkid_probe_get_binary_data(blkid_probe pr
, struct blkid_chain
*chn
)
371 int rc
, org_prob_flags
;
372 struct blkid_chain
*org_chn
;
374 /* save the current setting -- the binary API has to be completely
375 * independent on the current probing status
377 org_chn
= pr
->cur_chain
;
378 org_prob_flags
= pr
->prob_flags
;
383 blkid_probe_chain_reset_position(chn
);
385 rc
= chn
->driver
->probe(pr
, chn
);
388 blkid_probe_chain_reset_position(chn
);
390 /* restore the original setting
392 pr
->cur_chain
= org_chn
;
393 pr
->prob_flags
= org_prob_flags
;
398 DBG(LOWPROBE
, ul_debug("returning %s binary data", chn
->driver
->name
));
407 * Zeroize probing results and resets the current probing (this has impact to
408 * blkid_do_probe() only). This function does not touch probing filters and
409 * keeps assigned device.
411 void blkid_reset_probe(blkid_probe pr
)
415 blkid_probe_reset_values(pr
);
416 blkid_probe_set_wiper(pr
, 0, 0);
418 pr
->cur_chain
= NULL
;
420 for (i
= 0; i
< BLKID_NCHAINS
; i
++)
421 blkid_probe_chain_reset_position(&pr
->chains
[i
]);
425 static int blkid_probe_dump_filter(blkid_probe pr, int chain)
427 struct blkid_chain *chn;
430 if (!pr || chain < 0 || chain >= BLKID_NCHAINS)
433 chn = &pr->chains[chain];
438 for (i = 0; i < chn->driver->nidinfos; i++) {
439 const struct blkid_idinfo *id = chn->driver->idinfos[i];
441 DBG(LOWPROBE, ul_debug("%d: %s: %s",
444 blkid_bmp_get_item(chn->fltr, i)
445 ? "disabled" : "enabled <--"));
452 * Returns properly initialized chain filter
454 unsigned long *blkid_probe_get_filter(blkid_probe pr
, int chain
, int create
)
456 struct blkid_chain
*chn
;
458 if (chain
< 0 || chain
>= BLKID_NCHAINS
)
461 chn
= &pr
->chains
[chain
];
463 /* always when you touch the chain filter all indexes are reset and
464 * probing starts from scratch
466 blkid_probe_chain_reset_position(chn
);
467 pr
->cur_chain
= NULL
;
469 if (!chn
->driver
->has_fltr
|| (!chn
->fltr
&& !create
))
473 chn
->fltr
= calloc(1, blkid_bmp_nbytes(chn
->driver
->nidinfos
));
475 memset(chn
->fltr
, 0, blkid_bmp_nbytes(chn
->driver
->nidinfos
));
477 /* blkid_probe_dump_filter(pr, chain); */
482 * Generic private functions for filter setting
484 int __blkid_probe_invert_filter(blkid_probe pr
, int chain
)
487 struct blkid_chain
*chn
;
489 chn
= &pr
->chains
[chain
];
491 if (!chn
->driver
->has_fltr
|| !chn
->fltr
)
494 for (i
= 0; i
< blkid_bmp_nwords(chn
->driver
->nidinfos
); i
++)
495 chn
->fltr
[i
] = ~chn
->fltr
[i
];
497 DBG(LOWPROBE
, ul_debug("probing filter inverted"));
498 /* blkid_probe_dump_filter(pr, chain); */
502 int __blkid_probe_reset_filter(blkid_probe pr
, int chain
)
504 return blkid_probe_get_filter(pr
, chain
, FALSE
) ? 0 : -1;
507 int __blkid_probe_filter_types(blkid_probe pr
, int chain
, int flag
, char *names
[])
510 struct blkid_chain
*chn
;
513 fltr
= blkid_probe_get_filter(pr
, chain
, TRUE
);
517 chn
= &pr
->chains
[chain
];
519 for (i
= 0; i
< chn
->driver
->nidinfos
; i
++) {
521 const struct blkid_idinfo
*id
= chn
->driver
->idinfos
[i
];
524 for (n
= names
; *n
; n
++) {
525 if (!strcmp(id
->name
, *n
)) {
531 if (flag
& BLKID_FLTR_NOTIN
)
532 blkid_bmp_set_item(fltr
, i
);
533 } else if (flag
& BLKID_FLTR_ONLYIN
)
534 blkid_bmp_set_item(fltr
, i
);
537 DBG(LOWPROBE
, ul_debug("%s: a new probing type-filter initialized",
539 /* blkid_probe_dump_filter(pr, chain); */
543 static struct blkid_bufinfo
*read_buffer(blkid_probe pr
, uint64_t real_off
, uint64_t len
)
546 struct blkid_bufinfo
*bf
= NULL
;
548 if (lseek(pr
->fd
, real_off
, SEEK_SET
) == (off_t
) -1) {
553 /* someone trying to overflow some buffers? */
554 if (len
> ULONG_MAX
- sizeof(struct blkid_bufinfo
)) {
559 /* allocate info and space for data by one malloc call */
560 bf
= calloc(1, sizeof(struct blkid_bufinfo
) + len
);
566 bf
->data
= ((unsigned char *) bf
) + sizeof(struct blkid_bufinfo
);
569 INIT_LIST_HEAD(&bf
->bufs
);
571 DBG(LOWPROBE
, ul_debug("\tread: off=%"PRIu64
" len=%"PRIu64
"",
574 ret
= read(pr
->fd
, bf
->data
, len
);
575 if (ret
!= (ssize_t
) len
) {
576 DBG(LOWPROBE
, ul_debug("\tread failed: %m"));
579 /* I/O errors on CDROMs are non-fatal to work with hybrid
580 * audio+data disks */
581 if (ret
>= 0 || blkid_probe_is_cdrom(pr
))
590 * Search in buffers we already have in memory
592 static struct blkid_bufinfo
*get_cached_buffer(blkid_probe pr
, uint64_t off
, uint64_t len
)
594 uint64_t real_off
= pr
->off
+ off
;
597 list_for_each(p
, &pr
->buffers
) {
598 struct blkid_bufinfo
*x
=
599 list_entry(p
, struct blkid_bufinfo
, bufs
);
601 if (real_off
>= x
->off
&& real_off
+ len
<= x
->off
+ x
->len
) {
602 DBG(BUFFER
, ul_debug("\treuse: off=%"PRIu64
" len=%"PRIu64
" (for off=%"PRIu64
" len=%"PRIu64
")",
603 x
->off
, x
->len
, real_off
, len
));
611 * Zeroize in-memory data in already read buffer. The next blkid_probe_get_buffer()
612 * will return modified buffer. This is usable when you want to call the same probing
613 * function more than once and hide previously detected magic strings.
615 * See blkid_probe_hide_range().
617 static int hide_buffer(blkid_probe pr
, uint64_t off
, uint64_t len
)
619 uint64_t real_off
= pr
->off
+ off
;
623 if (UINT64_MAX
- len
< off
) {
624 DBG(BUFFER
, ul_debug("\t hide-buffer overflow (ignore)"));
628 list_for_each(p
, &pr
->buffers
) {
629 struct blkid_bufinfo
*x
=
630 list_entry(p
, struct blkid_bufinfo
, bufs
);
633 if (real_off
>= x
->off
&& real_off
+ len
<= x
->off
+ x
->len
) {
635 assert(x
->off
<= real_off
);
636 assert(x
->off
+ x
->len
>= real_off
+ len
);
638 data
= real_off
? x
->data
+ (real_off
- x
->off
) : x
->data
;
640 DBG(BUFFER
, ul_debug("\thiding: off=%"PRIu64
" len=%"PRIu64
,
642 memset(data
, 0, len
);
646 return ct
== 0 ? -EINVAL
: 0;
651 * Note that @off is offset within probing area, the probing area is defined by
652 * pr->off and pr->size.
654 unsigned char *blkid_probe_get_buffer(blkid_probe pr
, uint64_t off
, uint64_t len
)
656 struct blkid_bufinfo
*bf
= NULL
;
657 uint64_t real_off
= pr
->off
+ off
;
660 DBG(BUFFER, ul_debug("\t>>>> off=%ju, real-off=%ju (probe <%ju..%ju>, len=%ju",
661 off, real_off, pr->off, pr->off + pr->size, len));
668 if (UINT64_MAX
- len
< off
|| UINT64_MAX
- len
< real_off
) {
669 DBG(BUFFER
, ul_debug("\t read-buffer overflow (ignore)"));
674 || (!S_ISCHR(pr
->mode
) && (pr
->size
< off
|| pr
->size
< len
))
675 || (!S_ISCHR(pr
->mode
) && (pr
->off
+ pr
->size
< real_off
+ len
))) {
676 DBG(BUFFER
, ul_debug("\t read-buffer out of probing area (ignore)"));
682 pr
->parent
->devno
== pr
->devno
&&
683 pr
->parent
->off
<= pr
->off
&&
684 pr
->parent
->off
+ pr
->parent
->size
>= pr
->off
+ pr
->size
) {
686 * This is a cloned prober and points to the same area as
687 * parent. Let's use parent's buffers.
689 * Note that pr->off (and pr->parent->off) is always from the
690 * begin of the device.
692 return blkid_probe_get_buffer(pr
->parent
,
693 pr
->off
+ off
- pr
->parent
->off
, len
);
696 /* try buffers we already have in memory or read from device */
697 bf
= get_cached_buffer(pr
, off
, len
);
699 bf
= read_buffer(pr
, real_off
, len
);
703 list_add_tail(&bf
->bufs
, &pr
->buffers
);
706 assert(bf
->off
<= real_off
);
707 assert(bf
->off
+ bf
->len
>= real_off
+ len
);
710 return real_off
? bf
->data
+ (real_off
- bf
->off
) : bf
->data
;
714 * blkid_probe_reset_buffers:
717 * libblkid reuse all already read buffers from the device. The buffers may be
718 * modified by blkid_probe_hide_range(). This function reset and free all
719 * cached buffers. The next blkid_do_probe() will read all data from the
724 * Returns: <0 in case of failure, or 0 on success.
726 int blkid_probe_reset_buffers(blkid_probe pr
)
728 uint64_t ct
= 0, len
= 0;
730 pr
->flags
&= ~BLKID_FL_MODIF_BUFF
;
732 if (list_empty(&pr
->buffers
))
735 DBG(BUFFER
, ul_debug("Resetting probing buffers"));
737 while (!list_empty(&pr
->buffers
)) {
738 struct blkid_bufinfo
*bf
= list_entry(pr
->buffers
.next
,
739 struct blkid_bufinfo
, bufs
);
744 DBG(BUFFER
, ul_debug(" remove buffer: [off=%"PRIu64
", len=%"PRIu64
"]",
749 DBG(LOWPROBE
, ul_debug(" buffers summary: %"PRIu64
" bytes by %"PRIu64
" read() calls",
752 INIT_LIST_HEAD(&pr
->buffers
);
758 * blkid_probe_hide_range:
760 * @off: start of the range
761 * @len: size of the range
763 * This function modifies in-memory cached data from the device. The specified
764 * range is zeroized. This is usable together with blkid_probe_step_back().
765 * The next blkid_do_probe() will not see specified area.
767 * Note that this is usable for already (by library) read data, and this
768 * function is not a way how to hide any large areas on your device.
770 * The function blkid_probe_reset_buffers() reverts all.
774 * Returns: <0 in case of failure, or 0 on success.
776 int blkid_probe_hide_range(blkid_probe pr
, uint64_t off
, uint64_t len
)
778 int rc
= hide_buffer(pr
, off
, len
);
781 pr
->flags
|= BLKID_FL_MODIF_BUFF
;
786 static void blkid_probe_reset_values(blkid_probe pr
)
788 if (list_empty(&pr
->values
))
791 DBG(LOWPROBE
, ul_debug("resetting results"));
793 while (!list_empty(&pr
->values
)) {
794 struct blkid_prval
*v
= list_entry(pr
->values
.next
,
795 struct blkid_prval
, prvals
);
796 blkid_probe_free_value(v
);
799 INIT_LIST_HEAD(&pr
->values
);
803 * Small devices need a special care.
805 int blkid_probe_is_tiny(blkid_probe pr
)
807 return (pr
->flags
& BLKID_FL_TINY_DEV
);
811 * CDROMs may fail when probed for RAID (last sector problem)
813 int blkid_probe_is_cdrom(blkid_probe pr
)
815 return (pr
->flags
& BLKID_FL_CDROM_DEV
);
818 #ifdef CDROM_GET_CAPABILITY
820 static int is_sector_readable(int fd
, uint64_t sector
)
825 if (lseek(fd
, sector
* 512, SEEK_SET
) == (off_t
) -1)
828 sz
= read(fd
, buf
, sizeof(buf
));
829 if (sz
!= (ssize_t
) sizeof(buf
))
834 DBG(LOWPROBE
, ul_debug("CDROM: read sector %"PRIu64
" failed %m", sector
));
840 * Linux kernel reports (BLKGETSIZE) cdrom device size greater than area
841 * readable by read(2). We have to reduce the probing area to avoid unwanted
842 * I/O errors in probing functions. It seems that unreadable are always last 2
843 * or 3 CD blocks (CD block size is 2048 bytes, it means 12 in 512-byte
844 * sectors). Linux kernel reports (CDROM_LAST_WRITTEN) also location of last
845 * written block, so we will reduce size based on it too.
847 static void cdrom_size_correction(blkid_probe pr
, uint64_t last_written
)
849 uint64_t n
, nsectors
= pr
->size
>> 9;
851 if (last_written
&& nsectors
> ((last_written
+1) << 2))
852 nsectors
= (last_written
+1) << 2;
854 for (n
= nsectors
- 12; n
< nsectors
; n
++) {
855 if (!is_sector_readable(pr
->fd
, n
))
859 DBG(LOWPROBE
, ul_debug("CDROM: full size available"));
862 /* 'n' is the failed sector, reduce device size to n-1; */
863 DBG(LOWPROBE
, ul_debug("CDROM: reduce size from %ju to %ju.",
864 (uintmax_t) pr
->size
,
865 (uintmax_t) n
<< 9));
872 * blkid_probe_set_device:
874 * @fd: device file descriptor
875 * @off: begin of probing area
876 * @size: size of probing area (zero means whole device/file)
878 * Assigns the device to probe control struct, resets internal buffers, resets
879 * the current probing, and close previously associated device (if open by
882 * If @fd is < 0 than only resets the prober and returns 1. Note that
883 * blkid_reset_probe() keeps the device associated with the prober, but
884 * blkid_probe_set_device() does complete reset.
886 * Returns: -1 in case of failure, 0 on success and 1 on reset.
888 int blkid_probe_set_device(blkid_probe pr
, int fd
,
889 blkid_loff_t off
, blkid_loff_t size
)
893 char *dm_uuid
= NULL
;
896 blkid_reset_probe(pr
);
897 blkid_probe_reset_buffers(pr
);
899 if ((pr
->flags
& BLKID_FL_PRIVATE_FD
) && pr
->fd
>= 0)
902 if (pr
->disk_probe
) {
903 blkid_free_probe(pr
->disk_probe
);
904 pr
->disk_probe
= NULL
;
907 pr
->flags
&= ~BLKID_FL_PRIVATE_FD
;
908 pr
->flags
&= ~BLKID_FL_TINY_DEV
;
909 pr
->flags
&= ~BLKID_FL_CDROM_DEV
;
912 pr
->off
= (uint64_t) off
;
920 pr
->wipe_chain
= NULL
;
927 #if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE)
928 /* Disable read-ahead */
929 posix_fadvise(fd
, 0, 0, POSIX_FADV_RANDOM
);
934 if (!S_ISBLK(sb
.st_mode
) && !S_ISCHR(sb
.st_mode
) && !S_ISREG(sb
.st_mode
)) {
939 pr
->mode
= sb
.st_mode
;
940 if (S_ISBLK(sb
.st_mode
) || S_ISCHR(sb
.st_mode
))
941 pr
->devno
= sb
.st_rdev
;
943 if (S_ISBLK(sb
.st_mode
)) {
944 if (blkdev_get_size(fd
, (unsigned long long *) &devsiz
)) {
945 DBG(LOWPROBE
, ul_debug("failed to get device size"));
948 } else if (S_ISCHR(sb
.st_mode
)) {
951 if (!sysfs_chrdev_devno_to_devname(sb
.st_rdev
, buf
, sizeof(buf
))
952 || strncmp(buf
, "ubi", 3) != 0) {
953 DBG(LOWPROBE
, ul_debug("no UBI char device"));
957 devsiz
= 1; /* UBI devices are char... */
958 } else if (S_ISREG(sb
.st_mode
))
959 devsiz
= sb
.st_size
; /* regular file */
961 pr
->size
= size
? (uint64_t)size
: devsiz
;
963 if (off
&& size
== 0)
964 /* only offset without size specified */
965 pr
->size
-= (uint64_t) off
;
967 if (pr
->off
+ pr
->size
> devsiz
) {
968 DBG(LOWPROBE
, ul_debug("area specified by offset and size is bigger than device"));
973 if (pr
->size
<= 1440 * 1024 && !S_ISCHR(sb
.st_mode
))
974 pr
->flags
|= BLKID_FL_TINY_DEV
;
977 if (S_ISBLK(sb
.st_mode
)) {
979 * Re-open without O_NONBLOCK for floppy device.
981 * Since kernel commit c7e9d0020361f4308a70cdfd6d5335e273eb8717
982 * floppy drive works bad when opened with O_NONBLOCK.
984 struct floppy_fdc_state flst
;
986 if (ioctl(fd
, FDGETFDCSTAT
, &flst
) >= 0) {
987 int flags
= fcntl(fd
, F_GETFL
, 0);
991 if (flags
& O_NONBLOCK
) {
992 flags
&= ~O_NONBLOCK
;
994 fd
= ul_reopen(fd
, flags
| O_CLOEXEC
);
998 pr
->flags
|= BLKID_FL_PRIVATE_FD
;
1006 if (S_ISBLK(sb
.st_mode
) &&
1008 sysfs_devno_is_dm_private(sb
.st_rdev
, &dm_uuid
)) {
1009 DBG(LOWPROBE
, ul_debug("ignore private device mapper device"));
1010 pr
->flags
|= BLKID_FL_NOSCAN_DEV
;
1013 #ifdef CDROM_GET_CAPABILITY
1014 else if (S_ISBLK(sb
.st_mode
) &&
1015 !blkid_probe_is_tiny(pr
) &&
1018 blkid_probe_is_wholedisk(pr
)) {
1020 long last_written
= 0;
1023 * pktcdvd.ko accepts only these ioctls:
1024 * CDROMEJECT CDROMMULTISESSION CDROMREADTOCENTRY
1025 * CDROM_LAST_WRITTEN CDROM_SEND_PACKET SCSI_IOCTL_SEND_COMMAND
1026 * So CDROM_GET_CAPABILITY cannot be used for detecting pktcdvd
1027 * devices. But CDROM_GET_CAPABILITY and CDROM_DRIVE_STATUS are
1028 * fast so use them for detecting if medium is present. In any
1029 * case use last written block form CDROM_LAST_WRITTEN.
1032 if (ioctl(fd
, CDROM_GET_CAPABILITY
, NULL
) >= 0) {
1033 # ifdef CDROM_DRIVE_STATUS
1034 switch (ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
)) {
1041 pr
->flags
|= BLKID_FL_CDROM_DEV
;
1044 # ifdef CDROM_LAST_WRITTEN
1045 if (ioctl(fd
, CDROM_LAST_WRITTEN
, &last_written
) == 0) {
1046 pr
->flags
|= BLKID_FL_CDROM_DEV
;
1048 if (errno
== ENOMEDIUM
)
1053 if (pr
->flags
& BLKID_FL_CDROM_DEV
) {
1054 cdrom_size_correction(pr
, last_written
);
1056 # ifdef CDROMMULTISESSION
1057 if (!pr
->off
&& blkid_probe_get_hint(pr
, "session_offset", NULL
) < 0) {
1058 struct cdrom_multisession multisession
= { .addr_format
= CDROM_LBA
};
1059 if (ioctl(fd
, CDROMMULTISESSION
, &multisession
) == 0 && multisession
.xa_flag
)
1060 blkid_probe_set_hint(pr
, "session_offset", (multisession
.addr
.lba
<< 11));
1068 # ifdef BLKGETZONESZ
1069 if (S_ISBLK(sb
.st_mode
) && !is_floppy
) {
1070 uint32_t zone_size_sector
;
1072 if (!ioctl(pr
->fd
, BLKGETZONESZ
, &zone_size_sector
))
1073 pr
->zone_size
= zone_size_sector
<< 9;
1077 DBG(LOWPROBE
, ul_debug("ready for low-probing, offset=%"PRIu64
", size=%"PRIu64
", zonesize=%"PRIu64
,
1078 pr
->off
, pr
->size
, pr
->zone_size
));
1079 DBG(LOWPROBE
, ul_debug("whole-disk: %s, regfile: %s",
1080 blkid_probe_is_wholedisk(pr
) ?"YES" : "NO",
1081 S_ISREG(pr
->mode
) ? "YES" : "NO"));
1085 DBG(LOWPROBE
, ul_debug("failed to prepare a device for low-probing"));
1090 int blkid_probe_get_dimension(blkid_probe pr
, uint64_t *off
, uint64_t *size
)
1097 int blkid_probe_set_dimension(blkid_probe pr
, uint64_t off
, uint64_t size
)
1099 DBG(LOWPROBE
, ul_debug(
1100 "changing probing area: size=%"PRIu64
", off=%"PRIu64
" "
1101 "-to-> size=%"PRIu64
", off=%"PRIu64
"",
1102 pr
->size
, pr
->off
, size
, off
));
1106 pr
->flags
&= ~BLKID_FL_TINY_DEV
;
1108 if (pr
->size
<= 1440ULL * 1024ULL && !S_ISCHR(pr
->mode
))
1109 pr
->flags
|= BLKID_FL_TINY_DEV
;
1111 blkid_probe_reset_buffers(pr
);
1116 unsigned char *blkid_probe_get_sb_buffer(blkid_probe pr
, const struct blkid_idmag
*mag
, size_t size
)
1118 uint64_t hint_offset
;
1120 if (!mag
->hoff
|| blkid_probe_get_hint(pr
, mag
->hoff
, &hint_offset
) < 0)
1123 return blkid_probe_get_buffer(pr
, hint_offset
+ (mag
->kboff
<< 10), size
);
1127 * Check for matching magic value.
1128 * Returns BLKID_PROBE_OK if found, BLKID_PROBE_NONE if not found
1129 * or no magic present, or negative value on error.
1131 int blkid_probe_get_idmag(blkid_probe pr
, const struct blkid_idinfo
*id
,
1132 uint64_t *offset
, const struct blkid_idmag
**res
)
1134 const struct blkid_idmag
*mag
= NULL
;
1138 mag
= &id
->magics
[0];
1142 /* try to detect by magic string */
1143 while(mag
&& mag
->magic
) {
1146 uint64_t hint_offset
;
1148 if (!mag
->hoff
|| blkid_probe_get_hint(pr
, mag
->hoff
, &hint_offset
) < 0)
1151 /* If the magic is for zoned device, skip non-zoned device */
1152 if (mag
->is_zoned
&& !pr
->zone_size
) {
1160 kboff
= ((mag
->zonenum
* pr
->zone_size
) >> 10) + mag
->kboff_inzone
;
1162 off
= hint_offset
+ ((kboff
+ (mag
->sboff
>> 10)) << 10);
1163 buf
= blkid_probe_get_buffer(pr
, off
, 1024);
1168 if (buf
&& !memcmp(mag
->magic
,
1169 buf
+ (mag
->sboff
& 0x3ff), mag
->len
)) {
1171 DBG(LOWPROBE
, ul_debug("\tmagic sboff=%u, kboff=%" PRIu64
,
1172 mag
->sboff
, kboff
));
1174 *offset
= off
+ (mag
->sboff
& 0x3ff);
1177 return BLKID_PROBE_OK
;
1182 if (id
&& id
->magics
[0].magic
)
1183 /* magic string(s) defined, but not found */
1184 return BLKID_PROBE_NONE
;
1186 return BLKID_PROBE_OK
;
1189 static inline void blkid_probe_start(blkid_probe pr
)
1191 DBG(LOWPROBE
, ul_debug("start probe"));
1192 pr
->cur_chain
= NULL
;
1194 blkid_probe_set_wiper(pr
, 0, 0);
1197 static inline void blkid_probe_end(blkid_probe pr
)
1199 DBG(LOWPROBE
, ul_debug("end probe"));
1200 pr
->cur_chain
= NULL
;
1202 blkid_probe_set_wiper(pr
, 0, 0);
1209 * Calls probing functions in all enabled chains. The superblocks chain is
1210 * enabled by default. The blkid_do_probe() stores result from only one
1211 * probing function. It's necessary to call this routine in a loop to get
1212 * results from all probing functions in all chains. The probing is reset
1213 * by blkid_reset_probe() or by filter functions.
1215 * This is string-based NAME=value interface only.
1218 * <title>basic case - use the first result only</title>
1220 * if (blkid_do_probe(pr) == BLKID_PROBE_OK) {
1221 * int nvals = blkid_probe_numof_values(pr);
1222 * for (n = 0; n < nvals; n++) {
1223 * if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0)
1224 * printf("%s = %s\n", name, data);
1231 * <title>advanced case - probe for all signatures</title>
1233 * while (blkid_do_probe(pr) == BLKID_PROBE_OK) {
1234 * int nvals = blkid_probe_numof_values(pr);
1240 * See also blkid_reset_probe().
1242 * Returns: 0 on success, 1 when probing is done and -1 in case of error.
1244 int blkid_do_probe(blkid_probe pr
)
1248 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1249 return BLKID_PROBE_NONE
;
1252 struct blkid_chain
*chn
= pr
->cur_chain
;
1255 blkid_probe_start(pr
);
1256 chn
= pr
->cur_chain
= &pr
->chains
[0];
1258 /* we go to the next chain only when the previous probing
1259 * result was nothing (rc == 1) and when the current chain is
1260 * disabled or we are at end of the current chain (chain->idx +
1261 * 1 == sizeof chain) or the current chain bailed out right at
1262 * the start (chain->idx == -1)
1264 else if (rc
== 1 && (chn
->enabled
== FALSE
||
1265 chn
->idx
+ 1 == (int) chn
->driver
->nidinfos
||
1268 size_t idx
= chn
->driver
->id
+ 1;
1270 if (idx
< BLKID_NCHAINS
)
1271 chn
= pr
->cur_chain
= &pr
->chains
[idx
];
1273 blkid_probe_end(pr
);
1274 return BLKID_PROBE_NONE
; /* all chains already probed */
1278 chn
->binary
= FALSE
; /* for sure... */
1280 DBG(LOWPROBE
, ul_debug("chain probe %s %s (idx=%d)",
1282 chn
->enabled
? "ENABLED" : "DISABLED",
1288 /* rc: -1 = error, 0 = success, 1 = no result */
1289 rc
= chn
->driver
->probe(pr
, chn
);
1291 } while (rc
== BLKID_PROBE_NONE
);
1294 return BLKID_PROBE_ERROR
;
1299 #ifdef HAVE_LINUX_BLKZONED_H
1300 static int is_conventional(blkid_probe pr
, uint64_t offset
)
1302 struct blk_zone_report
*rep
= NULL
;
1309 zone_mask
= ~(pr
->zone_size
- 1);
1310 rep
= blkdev_get_zonereport(blkid_probe_get_fd(pr
),
1311 (offset
& zone_mask
) >> 9, 1);
1315 if (rep
->zones
[0].type
== BLK_ZONE_TYPE_CONVENTIONAL
)
1325 static inline int is_conventional(blkid_probe pr
__attribute__((__unused__
)),
1326 uint64_t offset
__attribute__((__unused__
)))
1335 * @dryrun: if TRUE then don't touch the device.
1337 * This function erases the current signature detected by @pr. The @pr has to
1338 * be open in O_RDWR mode, BLKID_SUBLKS_MAGIC or/and BLKID_PARTS_MAGIC flags
1339 * has to be enabled (if you want to erase also superblock with broken check
1340 * sums then use BLKID_SUBLKS_BADCSUM too).
1342 * After successful signature removing the @pr prober will be moved one step
1343 * back and the next blkid_do_probe() call will again call previously called
1344 * probing function. All in-memory cached data from the device are always
1348 * <title>wipe all filesystems or raids from the device</title>
1350 * fd = open(devname, O_RDWR|O_CLOEXEC);
1351 * blkid_probe_set_device(pr, fd, 0, 0);
1353 * blkid_probe_enable_superblocks(pr, 1);
1354 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1356 * while (blkid_do_probe(pr) == 0)
1357 * blkid_do_wipe(pr, FALSE);
1361 * See also blkid_probe_step_back() if you cannot use this built-in wipe
1362 * function, but you want to use libblkid probing as a source for wiping.
1364 * Returns: 0 on success, and -1 in case of error.
1366 int blkid_do_wipe(blkid_probe pr
, int dryrun
)
1368 const char *off
= NULL
;
1370 uint64_t offset
, magoff
;
1374 struct blkid_chain
*chn
;
1376 chn
= pr
->cur_chain
;
1378 return BLKID_PROBE_ERROR
;
1380 switch (chn
->driver
->id
) {
1381 case BLKID_CHAIN_SUBLKS
:
1382 rc
= blkid_probe_lookup_value(pr
, "SBMAGIC_OFFSET", &off
, NULL
);
1384 rc
= blkid_probe_lookup_value(pr
, "SBMAGIC", NULL
, &len
);
1386 case BLKID_CHAIN_PARTS
:
1387 rc
= blkid_probe_lookup_value(pr
, "PTMAGIC_OFFSET", &off
, NULL
);
1389 rc
= blkid_probe_lookup_value(pr
, "PTMAGIC", NULL
, &len
);
1392 return BLKID_PROBE_OK
;
1395 if (rc
|| len
== 0 || off
== NULL
)
1396 return BLKID_PROBE_OK
;
1399 magoff
= strtoumax(off
, NULL
, 10);
1401 return BLKID_PROBE_OK
;
1403 offset
= magoff
+ pr
->off
;
1404 fd
= blkid_probe_get_fd(pr
);
1406 return BLKID_PROBE_ERROR
;
1408 if (len
> sizeof(buf
))
1411 rc
= is_conventional(pr
, offset
);
1413 return BLKID_PROBE_ERROR
;
1414 conventional
= rc
== 1;
1416 DBG(LOWPROBE
, ul_debug(
1417 "do_wipe [offset=0x%"PRIx64
" (%"PRIu64
"), len=%zu, chain=%s, idx=%d, dryrun=%s]\n",
1418 offset
, offset
, len
, chn
->driver
->name
, chn
->idx
, dryrun
? "yes" : "not"));
1420 if (lseek(fd
, offset
, SEEK_SET
) == (off_t
) -1)
1421 return BLKID_PROBE_ERROR
;
1423 if (!dryrun
&& len
) {
1425 memset(buf
, 0, len
);
1427 /* wipen on device */
1428 if (write_all(fd
, buf
, len
))
1429 return BLKID_PROBE_ERROR
;
1431 return BLKID_PROBE_ERROR
;
1433 #ifdef HAVE_LINUX_BLKZONED_H
1434 uint64_t zone_mask
= ~(pr
->zone_size
- 1);
1435 struct blk_zone_range range
= {
1436 .sector
= (offset
& zone_mask
) >> 9,
1437 .nr_sectors
= pr
->zone_size
>> 9,
1440 rc
= ioctl(fd
, BLKRESETZONE
, &range
);
1442 return BLKID_PROBE_ERROR
;
1444 /* Should not reach here */
1449 pr
->flags
&= ~BLKID_FL_MODIF_BUFF
; /* be paranoid */
1451 return blkid_probe_step_back(pr
);
1456 /* wipe in memory only */
1457 blkid_probe_hide_range(pr
, magoff
, len
);
1458 return blkid_probe_step_back(pr
);
1461 return BLKID_PROBE_OK
;
1465 * blkid_probe_step_back:
1468 * This function move pointer to the probing chain one step back -- it means
1469 * that the previously used probing function will be called again in the next
1470 * blkid_do_probe() call.
1472 * This is necessary for example if you erase or modify on-disk superblock
1473 * according to the current libblkid probing result.
1475 * Note that blkid_probe_hide_range() changes semantic of this function and
1476 * cached buffers are not reset, but library uses in-memory modified
1477 * buffers to call the next probing function.
1480 * <title>wipe all superblock, but use libblkid only for probing</title>
1482 * pr = blkid_new_probe_from_filename(devname);
1484 * blkid_probe_enable_superblocks(pr, 1);
1485 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1487 * blkid_probe_enable_partitions(pr, 1);
1488 * blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC);
1490 * while (blkid_do_probe(pr) == BLKID_PROBE_OK) {
1491 * const char *ostr = NULL;
1495 * if (blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &ostr, NULL) == 0)
1496 * blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
1498 * // partition tables
1499 * if (len == 0 && blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &ostr, NULL) == 0)
1500 * blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
1505 * // convert ostr to the real offset by off = strtoll(ostr, NULL, 10);
1506 * // use your stuff to erase @len bytes at the @off
1509 * // retry the last probing to check for backup superblocks ..etc.
1510 * blkid_probe_step_back(pr);
1515 * Returns: 0 on success, and -1 in case of error.
1517 int blkid_probe_step_back(blkid_probe pr
)
1519 struct blkid_chain
*chn
;
1521 chn
= pr
->cur_chain
;
1525 if (!(pr
->flags
& BLKID_FL_MODIF_BUFF
))
1526 blkid_probe_reset_buffers(pr
);
1528 if (chn
->idx
>= 0) {
1530 DBG(LOWPROBE
, ul_debug("step back: moving %s chain index to %d",
1535 if (chn
->idx
== -1) {
1536 /* blkid_do_probe() goes to the next chain if the index
1537 * of the current chain is -1, so we have to set the
1538 * chain pointer to the previous chain.
1540 size_t idx
= chn
->driver
->id
> 0 ? chn
->driver
->id
- 1 : 0;
1542 DBG(LOWPROBE
, ul_debug("step back: moving to previous chain"));
1545 pr
->cur_chain
= &pr
->chains
[idx
];
1547 pr
->cur_chain
= NULL
;
1554 * blkid_do_safeprobe:
1557 * This function gathers probing results from all enabled chains and checks
1558 * for ambivalent results (e.g. more filesystems on the device).
1560 * This is string-based NAME=value interface only.
1562 * Note about superblocks chain -- the function does not check for filesystems
1563 * when a RAID signature is detected. The function also does not check for
1564 * collision between RAIDs. The first detected RAID is returned. The function
1565 * checks for collision between partition table and RAID signature -- it's
1566 * recommended to enable partitions chain together with superblocks chain.
1568 * Returns: 0 on success, 1 if nothing is detected, -2 if ambivalent result is
1569 * detected and -1 on case of error.
1571 int blkid_do_safeprobe(blkid_probe pr
)
1573 int i
, count
= 0, rc
= 0;
1575 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1576 return BLKID_PROBE_NONE
;
1578 blkid_probe_start(pr
);
1580 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
1581 struct blkid_chain
*chn
;
1583 chn
= pr
->cur_chain
= &pr
->chains
[i
];
1584 chn
->binary
= FALSE
; /* for sure... */
1586 DBG(LOWPROBE
, ul_debug("chain safeprobe %s %s",
1588 chn
->enabled
? "ENABLED" : "DISABLED"));
1593 blkid_probe_chain_reset_position(chn
);
1595 rc
= chn
->driver
->safeprobe(pr
, chn
);
1597 blkid_probe_chain_reset_position(chn
);
1599 /* rc: -2 ambivalent, -1 = error, 0 = success, 1 = no result */
1601 goto done
; /* error */
1603 count
++; /* success */
1607 blkid_probe_end(pr
);
1609 return BLKID_PROBE_ERROR
;
1611 return count
== 0 ? BLKID_PROBE_NONE
: BLKID_PROBE_OK
;
1615 * blkid_do_fullprobe:
1618 * This function gathers probing results from all enabled chains. Same as
1619 * blkid_do_safeprobe() but does not check for collision between probing
1622 * This is string-based NAME=value interface only.
1624 * Returns: 0 on success, 1 if nothing is detected or -1 on case of error.
1626 int blkid_do_fullprobe(blkid_probe pr
)
1628 int i
, count
= 0, rc
= 0;
1630 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1631 return BLKID_PROBE_NONE
;
1633 blkid_probe_start(pr
);
1635 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
1636 struct blkid_chain
*chn
;
1638 chn
= pr
->cur_chain
= &pr
->chains
[i
];
1639 chn
->binary
= FALSE
; /* for sure... */
1641 DBG(LOWPROBE
, ul_debug("chain fullprobe %s: %s",
1643 chn
->enabled
? "ENABLED" : "DISABLED"));
1648 blkid_probe_chain_reset_position(chn
);
1650 rc
= chn
->driver
->probe(pr
, chn
);
1652 blkid_probe_chain_reset_position(chn
);
1654 /* rc: -1 = error, 0 = success, 1 = no result */
1656 goto done
; /* error */
1658 count
++; /* success */
1662 blkid_probe_end(pr
);
1664 return BLKID_PROBE_ERROR
;
1666 return count
== 0 ? BLKID_PROBE_NONE
: BLKID_PROBE_OK
;
1669 /* same sa blkid_probe_get_buffer() but works with 512-sectors */
1670 unsigned char *blkid_probe_get_sector(blkid_probe pr
, unsigned int sector
)
1672 return blkid_probe_get_buffer(pr
, ((uint64_t) sector
) << 9, 0x200);
1675 struct blkid_prval
*blkid_probe_assign_value(blkid_probe pr
, const char *name
)
1677 struct blkid_prval
*v
;
1679 v
= calloc(1, sizeof(struct blkid_prval
));
1683 INIT_LIST_HEAD(&v
->prvals
);
1685 v
->chain
= pr
->cur_chain
;
1686 list_add_tail(&v
->prvals
, &pr
->values
);
1688 DBG(LOWPROBE
, ul_debug("assigning %s [%s]", name
, v
->chain
->driver
->name
));
1692 /* Note that value data is always terminated by zero to keep things robust,
1693 * this extra zero is not count to the value length. It's caller responsibility
1694 * to set proper value length (for strings we count terminator to the length,
1695 * for binary data it's without terminator).
1697 int blkid_probe_value_set_data(struct blkid_prval
*v
,
1698 const unsigned char *data
, size_t len
)
1700 v
->data
= calloc(1, len
+ 1); /* always terminate by \0 */
1704 memcpy(v
->data
, data
, len
);
1709 int blkid_probe_set_value(blkid_probe pr
, const char *name
,
1710 const unsigned char *data
, size_t len
)
1712 struct blkid_prval
*v
;
1714 v
= blkid_probe_assign_value(pr
, name
);
1718 return blkid_probe_value_set_data(v
, data
, len
);
1721 int blkid_probe_vsprintf_value(blkid_probe pr
, const char *name
,
1722 const char *fmt
, va_list ap
)
1724 struct blkid_prval
*v
;
1727 v
= blkid_probe_assign_value(pr
, name
);
1731 len
= vasprintf((char **) &v
->data
, fmt
, ap
);
1734 blkid_probe_free_value(v
);
1735 return len
== 0 ? -EINVAL
: -ENOMEM
;
1741 int blkid_probe_sprintf_value(blkid_probe pr
, const char *name
,
1742 const char *fmt
, ...)
1748 rc
= blkid_probe_vsprintf_value(pr
, name
, fmt
, ap
);
1754 int blkid_probe_set_magic(blkid_probe pr
, uint64_t offset
,
1755 size_t len
, const unsigned char *magic
)
1758 struct blkid_chain
*chn
= blkid_probe_get_chain(pr
);
1760 if (!chn
|| !len
|| chn
->binary
)
1763 switch (chn
->driver
->id
) {
1764 case BLKID_CHAIN_SUBLKS
:
1765 if (!(chn
->flags
& BLKID_SUBLKS_MAGIC
))
1767 rc
= blkid_probe_set_value(pr
, "SBMAGIC", magic
, len
);
1769 rc
= blkid_probe_sprintf_value(pr
,
1770 "SBMAGIC_OFFSET", "%llu", (unsigned long long)offset
);
1772 case BLKID_CHAIN_PARTS
:
1773 if (!(chn
->flags
& BLKID_PARTS_MAGIC
))
1775 rc
= blkid_probe_set_value(pr
, "PTMAGIC", magic
, len
);
1777 rc
= blkid_probe_sprintf_value(pr
,
1778 "PTMAGIC_OFFSET", "%llu", (unsigned long long)offset
);
1787 static void blkid_probe_log_csum_mismatch(blkid_probe pr
, size_t n
, const void *csum
,
1788 const void *expected
)
1791 char expected_hex
[sizeof(csum_hex
)];
1792 int hex_size
= min(sizeof(csum_hex
), n
* 2);
1794 for (int i
= 0; i
< hex_size
; i
+=2) {
1795 sprintf(&csum_hex
[i
], "%02X", ((const unsigned char *) csum
)[i
/ 2]);
1796 sprintf(&expected_hex
[i
], "%02X", ((const unsigned char *) expected
)[i
/ 2]);
1800 "incorrect checksum for type %s,"
1801 " got %*s, expected %*s",
1802 blkid_probe_get_probername(pr
),
1803 hex_size
, csum_hex
, hex_size
, expected_hex
);
1807 int blkid_probe_verify_csum_buf(blkid_probe pr
, size_t n
, const void *csum
,
1808 const void *expected
)
1810 if (memcmp(csum
, expected
, n
) != 0) {
1811 struct blkid_chain
*chn
= blkid_probe_get_chain(pr
);
1813 ON_DBG(LOWPROBE
, blkid_probe_log_csum_mismatch(pr
, n
, csum
, expected
));
1816 * Accept bad checksum if BLKID_SUBLKS_BADCSUM flags is set
1818 if (chn
->driver
->id
== BLKID_CHAIN_SUBLKS
1819 && (chn
->flags
& BLKID_SUBLKS_BADCSUM
)) {
1820 blkid_probe_set_value(pr
, "SBBADCSUM", (unsigned char *) "1", 2);
1823 return 0; /* bad checksum */
1830 int blkid_probe_verify_csum(blkid_probe pr
, uint64_t csum
, uint64_t expected
)
1832 return blkid_probe_verify_csum_buf(pr
, sizeof(csum
), &csum
, &expected
);
1836 * blkid_probe_get_devno:
1839 * Returns: block device number, or 0 for regular files.
1841 dev_t
blkid_probe_get_devno(blkid_probe pr
)
1847 * blkid_probe_get_wholedisk_devno:
1850 * Returns: device number of the wholedisk, or 0 for regular files.
1852 dev_t
blkid_probe_get_wholedisk_devno(blkid_probe pr
)
1854 if (!pr
->disk_devno
) {
1855 dev_t devno
, disk_devno
= 0;
1857 devno
= blkid_probe_get_devno(pr
);
1861 if (blkid_devno_to_wholedisk(devno
, NULL
, 0, &disk_devno
) == 0)
1862 pr
->disk_devno
= disk_devno
;
1864 return pr
->disk_devno
;
1868 * blkid_probe_is_wholedisk:
1871 * Returns: 1 if the device is whole-disk or 0.
1873 int blkid_probe_is_wholedisk(blkid_probe pr
)
1875 dev_t devno
, disk_devno
;
1877 devno
= blkid_probe_get_devno(pr
);
1881 disk_devno
= blkid_probe_get_wholedisk_devno(pr
);
1885 return devno
== disk_devno
;
1888 blkid_probe
blkid_probe_get_wholedisk_probe(blkid_probe pr
)
1892 if (blkid_probe_is_wholedisk(pr
))
1893 return NULL
; /* this is not partition */
1896 /* this is cloned blkid_probe, use parent's stuff */
1897 return blkid_probe_get_wholedisk_probe(pr
->parent
);
1899 disk
= blkid_probe_get_wholedisk_devno(pr
);
1901 if (pr
->disk_probe
&& pr
->disk_probe
->devno
!= disk
) {
1902 /* we have disk prober, but for another disk... close it */
1903 blkid_free_probe(pr
->disk_probe
);
1904 pr
->disk_probe
= NULL
;
1907 if (!pr
->disk_probe
) {
1908 /* Open a new disk prober */
1909 char *disk_path
= blkid_devno_to_devname(disk
);
1914 DBG(LOWPROBE
, ul_debug("allocate a wholedisk probe"));
1916 pr
->disk_probe
= blkid_new_probe_from_filename(disk_path
);
1920 if (!pr
->disk_probe
)
1921 return NULL
; /* ENOMEM? */
1924 return pr
->disk_probe
;
1928 * blkid_probe_get_size:
1931 * This function returns size of probing area as defined by blkid_probe_set_device().
1932 * If the size of the probing area is unrestricted then this function returns
1933 * the real size of device. See also blkid_get_dev_size().
1935 * Returns: size in bytes or -1 in case of error.
1937 blkid_loff_t
blkid_probe_get_size(blkid_probe pr
)
1939 return (blkid_loff_t
) pr
->size
;
1943 * blkid_probe_get_offset:
1946 * This function returns offset of probing area as defined by blkid_probe_set_device().
1948 * Returns: offset in bytes or -1 in case of error.
1950 blkid_loff_t
blkid_probe_get_offset(blkid_probe pr
)
1952 return (blkid_loff_t
) pr
->off
;
1956 * blkid_probe_get_fd:
1959 * Returns: file descriptor for assigned device/file or -1 in case of error.
1961 int blkid_probe_get_fd(blkid_probe pr
)
1967 * blkid_probe_get_sectorsize:
1968 * @pr: probe or NULL (for NULL returns 512)
1970 * Returns: block device logical sector size (BLKSSZGET ioctl, default 512).
1972 unsigned int blkid_probe_get_sectorsize(blkid_probe pr
)
1977 if (S_ISBLK(pr
->mode
) &&
1978 blkdev_get_sector_size(pr
->fd
, (int *) &pr
->blkssz
) == 0)
1981 pr
->blkssz
= DEFAULT_SECTOR_SIZE
;
1986 * blkid_probe_set_sectorsize:
1988 * @sz: new size (to overwrite system default)
1990 * Note that blkid_probe_set_device() resets this setting. Use it after
1991 * blkid_probe_set_device() and before any probing call.
1995 * Returns: 0 or <0 in case of error
1997 int blkid_probe_set_sectorsize(blkid_probe pr
, unsigned int sz
)
2004 * blkid_probe_get_sectors:
2007 * Returns: 512-byte sector count or -1 in case of error.
2009 blkid_loff_t
blkid_probe_get_sectors(blkid_probe pr
)
2011 return (blkid_loff_t
) (pr
->size
>> 9);
2015 * blkid_probe_numof_values:
2018 * Returns: number of values in probing result or -1 in case of error.
2020 int blkid_probe_numof_values(blkid_probe pr
)
2023 struct list_head
*p
;
2025 list_for_each(p
, &pr
->values
)
2031 * blkid_probe_get_value:
2033 * @num: wanted value in range 0..N, where N is blkid_probe_numof_values() - 1
2034 * @name: pointer to return value name or NULL
2035 * @data: pointer to return value data or NULL
2036 * @len: pointer to return value length or NULL
2038 * Note, the @len returns length of the @data, including the terminating
2041 * Returns: 0 on success, or -1 in case of error.
2043 int blkid_probe_get_value(blkid_probe pr
, int num
, const char **name
,
2044 const char **data
, size_t *len
)
2046 struct blkid_prval
*v
= __blkid_probe_get_value(pr
, num
);
2053 *data
= (char *) v
->data
;
2057 DBG(LOWPROBE
, ul_debug("returning %s value", v
->name
));
2062 * blkid_probe_lookup_value:
2064 * @name: name of value
2065 * @data: pointer to return value data or NULL
2066 * @len: pointer to return value length or NULL
2068 * Note, the @len returns length of the @data, including the terminating
2071 * Returns: 0 on success, or -1 in case of error.
2073 int blkid_probe_lookup_value(blkid_probe pr
, const char *name
,
2074 const char **data
, size_t *len
)
2076 struct blkid_prval
*v
= __blkid_probe_lookup_value(pr
, name
);
2081 *data
= (char *) v
->data
;
2088 * blkid_probe_has_value:
2090 * @name: name of value
2092 * Returns: 1 if value exist in probing result, otherwise 0.
2094 int blkid_probe_has_value(blkid_probe pr
, const char *name
)
2096 if (blkid_probe_lookup_value(pr
, name
, NULL
, NULL
) == 0)
2101 struct blkid_prval
*__blkid_probe_get_value(blkid_probe pr
, int num
)
2104 struct list_head
*p
;
2109 list_for_each(p
, &pr
->values
) {
2112 return list_entry(p
, struct blkid_prval
, prvals
);
2117 struct blkid_prval
*__blkid_probe_lookup_value(blkid_probe pr
, const char *name
)
2119 struct list_head
*p
;
2121 if (list_empty(&pr
->values
))
2124 list_for_each(p
, &pr
->values
) {
2125 struct blkid_prval
*v
= list_entry(p
, struct blkid_prval
,
2128 if (v
->name
&& strcmp(name
, v
->name
) == 0) {
2129 DBG(LOWPROBE
, ul_debug("returning %s value", v
->name
));
2137 /* converts DCE UUID (uuid[16]) to human readable string
2138 * - the @len should be always 37 */
2139 void blkid_unparse_uuid(const unsigned char *uuid
, char *str
, size_t len
)
2142 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2143 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
2147 uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14],uuid
[15]);
2150 /* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */
2151 int blkid_uuid_is_empty(const unsigned char *buf
, size_t len
)
2155 for (i
= 0; i
< len
; i
++)
2161 /* Removes whitespace from the right-hand side of a string (trailing
2164 * Returns size of the new string (without \0).
2166 size_t blkid_rtrim_whitespace(unsigned char *str
)
2168 return rtrim_whitespace(str
);
2171 /* Removes whitespace from the left-hand side of a string.
2173 * Returns size of the new string (without \0).
2175 size_t blkid_ltrim_whitespace(unsigned char *str
)
2177 return ltrim_whitespace(str
);
2181 * Some mkfs-like utils wipe some parts (usually begin) of the device.
2182 * For example LVM (pvcreate) or mkswap(8). This information could be used
2183 * for later resolution to conflicts between superblocks.
2185 * For example we found valid LVM superblock, LVM wipes 8KiB at the begin of
2186 * the device. If we found another signature (for example MBR) within the
2187 * wiped area then the signature has been added later and LVM superblock
2190 * Note that this heuristic is not 100% reliable, for example "pvcreate --zero n"
2191 * can be used to keep the begin of the device unmodified. It's probably better
2192 * to use this heuristic for conflicts between superblocks and partition tables
2193 * than for conflicts between filesystem superblocks -- existence of unwanted
2194 * partition table is very unusual, because PT is pretty visible (parsed and
2195 * interpreted by kernel).
2197 * Note that we usually expect only one signature on the device, it means that
2198 * we have to remember only one wiped area from previously successfully
2199 * detected signature.
2201 * blkid_probe_set_wiper() -- defines wiped area (e.g. LVM)
2202 * blkid_probe_use_wiper() -- try to use area (e.g. MBR)
2204 * Note that there is not relation between _wiper and blkid_to_wipe().
2207 void blkid_probe_set_wiper(blkid_probe pr
, uint64_t off
, uint64_t size
)
2209 struct blkid_chain
*chn
;
2212 DBG(LOWPROBE
, ul_debug("zeroize wiper"));
2213 pr
->wipe_size
= pr
->wipe_off
= 0;
2214 pr
->wipe_chain
= NULL
;
2218 chn
= pr
->cur_chain
;
2220 if (!chn
|| !chn
->driver
||
2221 chn
->idx
< 0 || (size_t) chn
->idx
>= chn
->driver
->nidinfos
)
2224 pr
->wipe_size
= size
;
2226 pr
->wipe_chain
= chn
;
2229 ul_debug("wiper set to %s::%s off=%"PRIu64
" size=%"PRIu64
"",
2231 chn
->driver
->idinfos
[chn
->idx
]->name
,
2232 pr
->wipe_off
, pr
->wipe_size
));
2236 * Returns 1 if the <@off,@size> area was wiped
2238 int blkid_probe_is_wiped(blkid_probe pr
, struct blkid_chain
**chn
, uint64_t off
, uint64_t size
)
2243 if (pr
->wipe_off
<= off
&& off
+ size
<= pr
->wipe_off
+ pr
->wipe_size
) {
2244 *chn
= pr
->wipe_chain
;
2251 * Try to use any area -- if the area has been previously wiped then the
2252 * previous probing result should be ignored (reset).
2254 void blkid_probe_use_wiper(blkid_probe pr
, uint64_t off
, uint64_t size
)
2256 struct blkid_chain
*chn
= NULL
;
2258 if (blkid_probe_is_wiped(pr
, &chn
, off
, size
) && chn
) {
2259 DBG(LOWPROBE
, ul_debug("previously wiped area modified "
2260 " -- ignore previous results"));
2261 blkid_probe_set_wiper(pr
, 0, 0);
2262 blkid_probe_chain_reset_values(pr
, chn
);
2266 static struct blkid_hint
*get_hint(blkid_probe pr
, const char *name
)
2268 struct list_head
*p
;
2270 if (list_empty(&pr
->hints
))
2273 list_for_each(p
, &pr
->hints
) {
2274 struct blkid_hint
*h
= list_entry(p
, struct blkid_hint
, hints
);
2276 if (h
->name
&& strcmp(name
, h
->name
) == 0)
2283 * blkid_probe_set_hint:
2285 * @name: hint name or NAME=value
2286 * @value: offset or another number
2288 * Sets extra hint for low-level prober. If the hint is set by NAME=value
2289 * notation than @value is ignored. The functions blkid_probe_set_device()
2290 * and blkid_reset_probe() resets all hints.
2292 * The hints are optional way how to force libblkid probing functions to check
2293 * for example another location.
2295 * Returns: 0 on success, or -1 in case of error.
2297 int blkid_probe_set_hint(blkid_probe pr
, const char *name
, uint64_t value
)
2299 struct blkid_hint
*hint
= NULL
;
2300 char *n
= NULL
, *v
= NULL
;
2302 if (strchr(name
, '=')) {
2305 if (blkid_parse_tag_string(name
, &n
, &v
) != 0)
2309 value
= strtoumax(v
, &end
, 10);
2311 if (errno
|| v
== end
|| (end
&& *end
))
2315 hint
= get_hint(pr
, n
? n
: name
);
2317 /* alter old hint */
2318 hint
->value
= value
;
2320 ul_debug("updated hint '%s' to %"PRIu64
"", hint
->name
, hint
->value
));
2322 /* add a new hint */
2328 hint
= malloc(sizeof(*hint
));
2333 hint
->value
= value
;
2335 INIT_LIST_HEAD(&hint
->hints
);
2336 list_add_tail(&hint
->hints
, &pr
->hints
);
2339 ul_debug("new hint '%s' is %"PRIu64
"", hint
->name
, hint
->value
));
2347 return errno
? -errno
: -EINVAL
;
2351 int blkid_probe_get_hint(blkid_probe pr
, const char *name
, uint64_t *value
)
2353 struct blkid_hint
*h
= get_hint(pr
, name
);
2363 * blkid_probe_reset_hints:
2366 * Removes all previously defined probinig hints. See also blkid_probe_set_hint().
2368 void blkid_probe_reset_hints(blkid_probe pr
)
2370 if (list_empty(&pr
->hints
))
2373 DBG(LOWPROBE
, ul_debug("resetting hints"));
2375 while (!list_empty(&pr
->hints
)) {
2376 struct blkid_hint
*h
= list_entry(pr
->hints
.next
,
2377 struct blkid_hint
, hints
);
2378 list_del(&h
->hints
);
2383 INIT_LIST_HEAD(&pr
->hints
);