]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/probe.c
2dd87cf9ad3fd8ffa305d1d242eb8f96f07879cf
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) == 0)
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_SYS_STAT_H
103 #include <inttypes.h>
111 #include "strutils.h"
115 * All supported chains
117 static const struct blkid_chaindrv
*chains_drvs
[] = {
118 [BLKID_CHAIN_SUBLKS
] = &superblocks_drv
,
119 [BLKID_CHAIN_TOPLGY
] = &topology_drv
,
120 [BLKID_CHAIN_PARTS
] = &partitions_drv
123 static void blkid_probe_reset_values(blkid_probe pr
);
128 * Returns: a pointer to the newly allocated probe struct or NULL in case of error.
130 blkid_probe
blkid_new_probe(void)
136 pr
= calloc(1, sizeof(struct blkid_struct_probe
));
140 DBG(LOWPROBE
, ul_debug("allocate a new probe"));
142 /* initialize chains */
143 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
144 pr
->chains
[i
].driver
= chains_drvs
[i
];
145 pr
->chains
[i
].flags
= chains_drvs
[i
]->dflt_flags
;
146 pr
->chains
[i
].enabled
= chains_drvs
[i
]->dflt_enabled
;
148 INIT_LIST_HEAD(&pr
->buffers
);
149 INIT_LIST_HEAD(&pr
->values
);
154 * Clone @parent, the new clone shares all, but except:
157 * - buffers if another device (or offset) is set to the prober
159 blkid_probe
blkid_clone_probe(blkid_probe parent
)
166 DBG(LOWPROBE
, ul_debug("allocate a probe clone"));
168 pr
= blkid_new_probe();
173 pr
->off
= parent
->off
;
174 pr
->size
= parent
->size
;
175 pr
->devno
= parent
->devno
;
176 pr
->disk_devno
= parent
->disk_devno
;
177 pr
->blkssz
= parent
->blkssz
;
178 pr
->flags
= parent
->flags
;
181 pr
->flags
&= ~BLKID_FL_PRIVATE_FD
;
189 * blkid_new_probe_from_filename:
190 * @filename: device or regular file
192 * This function is same as call open(filename), blkid_new_probe() and
193 * blkid_probe_set_device(pr, fd, 0, 0).
195 * The @filename is closed by blkid_free_probe() or by the
196 * blkid_probe_set_device() call.
198 * Returns: a pointer to the newly allocated probe struct or NULL in case of
201 blkid_probe
blkid_new_probe_from_filename(const char *filename
)
204 blkid_probe pr
= NULL
;
206 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
);
210 pr
= blkid_new_probe();
214 if (blkid_probe_set_device(pr
, fd
, 0, 0))
217 pr
->flags
|= BLKID_FL_PRIVATE_FD
;
222 blkid_free_probe(pr
);
230 * Deallocates the probe struct, buffers and all allocated
231 * data that are associated with this probing control struct.
233 void blkid_free_probe(blkid_probe pr
)
240 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
241 struct blkid_chain
*ch
= &pr
->chains
[i
];
243 if (ch
->driver
->free_data
)
244 ch
->driver
->free_data(pr
, ch
->data
);
248 if ((pr
->flags
& BLKID_FL_PRIVATE_FD
) && pr
->fd
>= 0)
250 blkid_probe_reset_buffers(pr
);
251 blkid_probe_reset_values(pr
);
252 blkid_free_probe(pr
->disk_probe
);
254 DBG(LOWPROBE
, ul_debug("free probe"));
258 void blkid_probe_free_value(struct blkid_prval
*v
)
263 list_del(&v
->prvals
);
266 DBG(LOWPROBE
, ul_debug(" free value %s", v
->name
));
271 * Removes chain values from probing result.
273 void blkid_probe_chain_reset_values(blkid_probe pr
, struct blkid_chain
*chn
)
276 struct list_head
*p
, *pnext
;
278 if (list_empty(&pr
->values
))
281 DBG(LOWPROBE
, ul_debug("Resetting %s values", chn
->driver
->name
));
283 list_for_each_safe(p
, pnext
, &pr
->values
) {
284 struct blkid_prval
*v
= list_entry(p
,
285 struct blkid_prval
, prvals
);
288 blkid_probe_free_value(v
);
292 static void blkid_probe_chain_reset_position(struct blkid_chain
*chn
)
298 * Move chain values from probing result to @vals
300 int blkid_probe_chain_save_values(blkid_probe pr
, struct blkid_chain
*chn
,
301 struct list_head
*vals
)
303 struct list_head
*p
, *pnext
;
304 struct blkid_prval
*v
;
306 DBG(LOWPROBE
, ul_debug("saving %s values", chn
->driver
->name
));
308 list_for_each_safe(p
, pnext
, &pr
->values
) {
310 v
= list_entry(p
, struct blkid_prval
, prvals
);
314 list_del_init(&v
->prvals
);
315 list_add_tail(&v
->prvals
, vals
);
321 * Appends values from @vals to the probing result
323 void blkid_probe_append_values_list(blkid_probe pr
, struct list_head
*vals
)
325 DBG(LOWPROBE
, ul_debug("appending values"));
327 list_splice(vals
, &pr
->values
);
328 INIT_LIST_HEAD(vals
);
332 void blkid_probe_free_values_list(struct list_head
*vals
)
337 DBG(LOWPROBE
, ul_debug("freeing values list"));
339 while (!list_empty(vals
)) {
340 struct blkid_prval
*v
= list_entry(vals
->next
, struct blkid_prval
, prvals
);
341 blkid_probe_free_value(v
);
345 struct blkid_chain
*blkid_probe_get_chain(blkid_probe pr
)
347 return pr
->cur_chain
;
350 static const char *blkid_probe_get_probername(blkid_probe pr
)
352 struct blkid_chain
*chn
= blkid_probe_get_chain(pr
);
354 if (chn
&& chn
->idx
>= 0 && (unsigned)chn
->idx
< chn
->driver
->nidinfos
)
355 return chn
->driver
->idinfos
[chn
->idx
]->name
;
360 void *blkid_probe_get_binary_data(blkid_probe pr
, struct blkid_chain
*chn
)
362 int rc
, org_prob_flags
;
363 struct blkid_chain
*org_chn
;
365 /* save the current setting -- the binary API has to be completely
366 * independent on the current probing status
368 org_chn
= pr
->cur_chain
;
369 org_prob_flags
= pr
->prob_flags
;
374 blkid_probe_chain_reset_position(chn
);
376 rc
= chn
->driver
->probe(pr
, chn
);
379 blkid_probe_chain_reset_position(chn
);
381 /* restore the original setting
383 pr
->cur_chain
= org_chn
;
384 pr
->prob_flags
= org_prob_flags
;
389 DBG(LOWPROBE
, ul_debug("returning %s binary data", chn
->driver
->name
));
398 * Zeroize probing results and resets the current probing (this has impact to
399 * blkid_do_probe() only). This function does not touch probing filters and
400 * keeps assigned device.
402 void blkid_reset_probe(blkid_probe pr
)
406 blkid_probe_reset_values(pr
);
407 blkid_probe_set_wiper(pr
, 0, 0);
409 pr
->cur_chain
= NULL
;
411 for (i
= 0; i
< BLKID_NCHAINS
; i
++)
412 blkid_probe_chain_reset_position(&pr
->chains
[i
]);
416 static int blkid_probe_dump_filter(blkid_probe pr, int chain)
418 struct blkid_chain *chn;
421 if (!pr || chain < 0 || chain >= BLKID_NCHAINS)
424 chn = &pr->chains[chain];
429 for (i = 0; i < chn->driver->nidinfos; i++) {
430 const struct blkid_idinfo *id = chn->driver->idinfos[i];
432 DBG(LOWPROBE, ul_debug("%d: %s: %s",
435 blkid_bmp_get_item(chn->fltr, i)
436 ? "disabled" : "enabled <--"));
443 * Returns properly initialized chain filter
445 unsigned long *blkid_probe_get_filter(blkid_probe pr
, int chain
, int create
)
447 struct blkid_chain
*chn
;
449 if (chain
< 0 || chain
>= BLKID_NCHAINS
)
452 chn
= &pr
->chains
[chain
];
454 /* always when you touch the chain filter all indexes are reset and
455 * probing starts from scratch
457 blkid_probe_chain_reset_position(chn
);
458 pr
->cur_chain
= NULL
;
460 if (!chn
->driver
->has_fltr
|| (!chn
->fltr
&& !create
))
464 chn
->fltr
= calloc(1, blkid_bmp_nbytes(chn
->driver
->nidinfos
));
466 memset(chn
->fltr
, 0, blkid_bmp_nbytes(chn
->driver
->nidinfos
));
468 /* blkid_probe_dump_filter(pr, chain); */
473 * Generic private functions for filter setting
475 int __blkid_probe_invert_filter(blkid_probe pr
, int chain
)
478 struct blkid_chain
*chn
;
480 chn
= &pr
->chains
[chain
];
482 if (!chn
->driver
->has_fltr
|| !chn
->fltr
)
485 for (i
= 0; i
< blkid_bmp_nwords(chn
->driver
->nidinfos
); i
++)
486 chn
->fltr
[i
] = ~chn
->fltr
[i
];
488 DBG(LOWPROBE
, ul_debug("probing filter inverted"));
489 /* blkid_probe_dump_filter(pr, chain); */
493 int __blkid_probe_reset_filter(blkid_probe pr
, int chain
)
495 return blkid_probe_get_filter(pr
, chain
, FALSE
) ? 0 : -1;
498 int __blkid_probe_filter_types(blkid_probe pr
, int chain
, int flag
, char *names
[])
501 struct blkid_chain
*chn
;
504 fltr
= blkid_probe_get_filter(pr
, chain
, TRUE
);
508 chn
= &pr
->chains
[chain
];
510 for (i
= 0; i
< chn
->driver
->nidinfos
; i
++) {
512 const struct blkid_idinfo
*id
= chn
->driver
->idinfos
[i
];
515 for (n
= names
; *n
; n
++) {
516 if (!strcmp(id
->name
, *n
)) {
522 if (flag
& BLKID_FLTR_NOTIN
)
523 blkid_bmp_set_item(fltr
, i
);
524 } else if (flag
& BLKID_FLTR_ONLYIN
)
525 blkid_bmp_set_item(fltr
, i
);
528 DBG(LOWPROBE
, ul_debug("%s: a new probing type-filter initialized",
530 /* blkid_probe_dump_filter(pr, chain); */
534 static struct blkid_bufinfo
*read_buffer(blkid_probe pr
, uint64_t real_off
, uint64_t len
)
537 struct blkid_bufinfo
*bf
= NULL
;
539 if (blkid_llseek(pr
->fd
, real_off
, SEEK_SET
) < 0) {
544 /* someone trying to overflow some buffers? */
545 if (len
> ULONG_MAX
- sizeof(struct blkid_bufinfo
)) {
550 /* allocate info and space for data by one malloc call */
551 bf
= calloc(1, sizeof(struct blkid_bufinfo
) + len
);
557 bf
->data
= ((unsigned char *) bf
) + sizeof(struct blkid_bufinfo
);
560 INIT_LIST_HEAD(&bf
->bufs
);
562 DBG(LOWPROBE
, ul_debug("\tread: off=%"PRIu64
" len=%"PRIu64
"",
565 ret
= read(pr
->fd
, bf
->data
, len
);
566 if (ret
!= (ssize_t
) len
) {
567 DBG(LOWPROBE
, ul_debug("\tread failed: %m"));
570 /* I/O errors on CDROMs are non-fatal to work with hybrid
571 * audio+data disks */
572 if (ret
>= 0 || blkid_probe_is_cdrom(pr
))
581 * Search in buffers we already in memory
583 static struct blkid_bufinfo
*get_cached_buffer(blkid_probe pr
, uint64_t off
, uint64_t len
)
585 uint64_t real_off
= pr
->off
+ off
;
588 list_for_each(p
, &pr
->buffers
) {
589 struct blkid_bufinfo
*x
=
590 list_entry(p
, struct blkid_bufinfo
, bufs
);
592 if (real_off
>= x
->off
&& real_off
+ len
<= x
->off
+ x
->len
) {
593 DBG(BUFFER
, ul_debug("\treuse: off=%"PRIu64
" len=%"PRIu64
" (for off=%"PRIu64
" len=%"PRIu64
")",
594 x
->off
, x
->len
, real_off
, len
));
602 * Zeroize in-memory data in already read buffer. The next blkid_probe_get_buffer()
603 * will return modified buffer. This is usable when you want to call the same probing
604 * function more than once and hide previously detected magic strings.
606 * See blkid_probe_hide_range().
608 static int hide_buffer(blkid_probe pr
, uint64_t off
, uint64_t len
)
610 uint64_t real_off
= pr
->off
+ off
;
614 list_for_each(p
, &pr
->buffers
) {
615 struct blkid_bufinfo
*x
=
616 list_entry(p
, struct blkid_bufinfo
, bufs
);
619 if (real_off
>= x
->off
&& real_off
+ len
<= x
->off
+ x
->len
) {
621 assert(x
->off
<= real_off
);
622 assert(x
->off
+ x
->len
>= real_off
+ len
);
624 data
= real_off
? x
->data
+ (real_off
- x
->off
) : x
->data
;
626 DBG(BUFFER
, ul_debug("\thiding: off=%"PRIu64
" len=%"PRIu64
,
628 memset(data
, 0, len
);
632 return ct
== 0 ? -EINVAL
: 0;
637 * Note that @off is offset within probing area, the probing area is defined by
638 * pr->off and pr->size.
640 unsigned char *blkid_probe_get_buffer(blkid_probe pr
, uint64_t off
, uint64_t len
)
642 struct blkid_bufinfo
*bf
= NULL
;
643 uint64_t real_off
= pr
->off
+ off
;
646 DBG(BUFFER, ul_debug("\t>>>> off=%ju, real-off=%ju (probe <%ju..%ju>, len=%ju",
647 off, real_off, pr->off, pr->off + pr->size, len));
655 if (len
== 0 || (!S_ISCHR(pr
->mode
) && pr
->off
+ pr
->size
< real_off
+ len
)) {
656 DBG(BUFFER
, ul_debug("\t ignore: request out of probing area"));
662 pr
->parent
->devno
== pr
->devno
&&
663 pr
->parent
->off
<= pr
->off
&&
664 pr
->parent
->off
+ pr
->parent
->size
>= pr
->off
+ pr
->size
) {
666 * This is a cloned prober and points to the same area as
667 * parent. Let's use parent's buffers.
669 * Note that pr->off (and pr->parent->off) is always from the
670 * begin of the device.
672 return blkid_probe_get_buffer(pr
->parent
,
673 pr
->off
+ off
- pr
->parent
->off
, len
);
676 /* try buffers we already have in memory or read from device */
677 bf
= get_cached_buffer(pr
, off
, len
);
679 bf
= read_buffer(pr
, real_off
, len
);
683 list_add_tail(&bf
->bufs
, &pr
->buffers
);
686 assert(bf
->off
<= real_off
);
687 assert(bf
->off
+ bf
->len
>= real_off
+ len
);
690 return real_off
? bf
->data
+ (real_off
- bf
->off
) : bf
->data
;
694 * blkid_probe_reset_buffers:
697 * libblkid reuse all already read buffers from the device. The bufferes may be
698 * modified by blkid_probe_hide_range(). This function reset and free all
699 * cached bufferes. The next blkid_do_probe() will read all data from the
702 * Returns: <0 in case of failure, or 0 on success.
704 int blkid_probe_reset_buffers(blkid_probe pr
)
706 uint64_t ct
= 0, len
= 0;
708 pr
->flags
&= ~BLKID_FL_MODIF_BUFF
;
710 if (list_empty(&pr
->buffers
))
713 DBG(BUFFER
, ul_debug("Resetting probing buffers"));
715 while (!list_empty(&pr
->buffers
)) {
716 struct blkid_bufinfo
*bf
= list_entry(pr
->buffers
.next
,
717 struct blkid_bufinfo
, bufs
);
722 DBG(BUFFER
, ul_debug(" remove buffer: [off=%"PRIu64
", len=%"PRIu64
"]",
727 DBG(LOWPROBE
, ul_debug(" buffers summary: %"PRIu64
" bytes by %"PRIu64
" read() calls",
730 INIT_LIST_HEAD(&pr
->buffers
);
736 * blkid_probe_hide_range:
738 * @off: start of the range
739 * @len: size of the range
741 * This function modifies in-memory cached data from the device. The specified
742 * range is zeroized. This is usable together with blkid_probe_step_back().
743 * The next blkid_do_probe() will not see specified area.
745 * Note that this is usable for already (by library) read data, and this
746 * function is not a way how to hide any large areas on your device.
748 * The function blkid_probe_reset_buffers() reverts all.
750 * Returns: <0 in case of failure, or 0 on success.
752 int blkid_probe_hide_range(blkid_probe pr
, uint64_t off
, uint64_t len
)
754 int rc
= hide_buffer(pr
, off
, len
);
757 pr
->flags
|= BLKID_FL_MODIF_BUFF
;
761 static void blkid_probe_reset_values(blkid_probe pr
)
763 if (list_empty(&pr
->values
))
766 DBG(LOWPROBE
, ul_debug("resetting results"));
768 while (!list_empty(&pr
->values
)) {
769 struct blkid_prval
*v
= list_entry(pr
->values
.next
,
770 struct blkid_prval
, prvals
);
771 blkid_probe_free_value(v
);
774 INIT_LIST_HEAD(&pr
->values
);
778 * Small devices need a special care.
780 int blkid_probe_is_tiny(blkid_probe pr
)
782 return (pr
->flags
& BLKID_FL_TINY_DEV
);
786 * CDROMs may fail when probed for RAID (last sector problem)
788 int blkid_probe_is_cdrom(blkid_probe pr
)
790 return (pr
->flags
& BLKID_FL_CDROM_DEV
);
793 #ifdef CDROM_GET_CAPABILITY
795 static int is_sector_readable(int fd
, uint64_t sector
)
800 if (blkid_llseek(fd
, sector
* 512, SEEK_SET
) < 0)
803 sz
= read(fd
, buf
, sizeof(buf
));
804 if (sz
!= (ssize_t
) sizeof(buf
))
809 DBG(LOWPROBE
, ul_debug("CDROM: read sector %"PRIu64
" failed %m", sector
));
815 * Linux kernel reports (BLKGETSIZE) cdrom device size greater than area
816 * readable by read(2). We have to reduce the probing area to avoid unwanted
817 * I/O errors in probing functions. It seems that unreadable are always last 2
818 * or 3 CD blocks (CD block size is 2048 bytes, it means 12 in 512-byte
821 static void cdrom_size_correction(blkid_probe pr
)
823 uint64_t n
, nsectors
= pr
->size
>> 9;
825 for (n
= nsectors
- 12; n
< nsectors
; n
++) {
826 if (!is_sector_readable(pr
->fd
, n
))
830 DBG(LOWPROBE
, ul_debug("CDROM: full size available"));
833 /* 'n' is the failed sector, reduce device size to n-1; */
834 DBG(LOWPROBE
, ul_debug("CDROM: reduce size from %ju to %ju.",
835 (uintmax_t) pr
->size
,
836 (uintmax_t) n
<< 9));
843 * blkid_probe_set_device:
845 * @fd: device file descriptor
846 * @off: begin of probing area
847 * @size: size of probing area (zero means whole device/file)
849 * Assigns the device to probe control struct, resets internal buffers and
850 * resets the current probing.
852 * Returns: -1 in case of failure, or 0 on success.
854 int blkid_probe_set_device(blkid_probe pr
, int fd
,
855 blkid_loff_t off
, blkid_loff_t size
)
859 char *dm_uuid
= NULL
;
861 blkid_reset_probe(pr
);
862 blkid_probe_reset_buffers(pr
);
864 if ((pr
->flags
& BLKID_FL_PRIVATE_FD
) && pr
->fd
>= 0)
867 pr
->flags
&= ~BLKID_FL_PRIVATE_FD
;
868 pr
->flags
&= ~BLKID_FL_TINY_DEV
;
869 pr
->flags
&= ~BLKID_FL_CDROM_DEV
;
872 pr
->off
= (uint64_t) off
;
880 pr
->wipe_chain
= NULL
;
882 #if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE)
883 /* Disable read-ahead */
884 posix_fadvise(fd
, 0, 0, POSIX_FADV_RANDOM
);
889 if (!S_ISBLK(sb
.st_mode
) && !S_ISCHR(sb
.st_mode
) && !S_ISREG(sb
.st_mode
)) {
894 pr
->mode
= sb
.st_mode
;
895 if (S_ISBLK(sb
.st_mode
) || S_ISCHR(sb
.st_mode
))
896 pr
->devno
= sb
.st_rdev
;
898 if (S_ISBLK(sb
.st_mode
)) {
899 if (blkdev_get_size(fd
, (unsigned long long *) &devsiz
)) {
900 DBG(LOWPROBE
, ul_debug("failed to get device size"));
903 } else if (S_ISCHR(sb
.st_mode
))
904 devsiz
= 1; /* UBI devices are char... */
905 else if (S_ISREG(sb
.st_mode
))
906 devsiz
= sb
.st_size
; /* regular file */
908 pr
->size
= size
? (uint64_t)size
: devsiz
;
910 if (off
&& size
== 0)
911 /* only offset without size specified */
912 pr
->size
-= (uint64_t) off
;
914 if (pr
->off
+ pr
->size
> devsiz
) {
915 DBG(LOWPROBE
, ul_debug("area specified by offset and size is bigger than device"));
920 if (pr
->size
<= 1440 * 1024 && !S_ISCHR(sb
.st_mode
))
921 pr
->flags
|= BLKID_FL_TINY_DEV
;
923 if (S_ISBLK(sb
.st_mode
) &&
924 sysfs_devno_is_dm_private(sb
.st_rdev
, &dm_uuid
)) {
925 DBG(LOWPROBE
, ul_debug("ignore private device mapper device"));
926 pr
->flags
|= BLKID_FL_NOSCAN_DEV
;
929 #ifdef CDROM_GET_CAPABILITY
930 else if (S_ISBLK(sb
.st_mode
) &&
931 !blkid_probe_is_tiny(pr
) &&
933 blkid_probe_is_wholedisk(pr
) &&
934 ioctl(fd
, CDROM_GET_CAPABILITY
, NULL
) >= 0) {
936 pr
->flags
|= BLKID_FL_CDROM_DEV
;
937 cdrom_size_correction(pr
);
942 DBG(LOWPROBE
, ul_debug("ready for low-probing, offset=%"PRIu64
", size=%"PRIu64
"",
944 DBG(LOWPROBE
, ul_debug("whole-disk: %s, regfile: %s",
945 blkid_probe_is_wholedisk(pr
) ?"YES" : "NO",
946 S_ISREG(pr
->mode
) ? "YES" : "NO"));
950 DBG(LOWPROBE
, ul_debug("failed to prepare a device for low-probing"));
955 int blkid_probe_get_dimension(blkid_probe pr
, uint64_t *off
, uint64_t *size
)
962 int blkid_probe_set_dimension(blkid_probe pr
, uint64_t off
, uint64_t size
)
964 DBG(LOWPROBE
, ul_debug(
965 "changing probing area: size=%"PRIu64
", off=%"PRIu64
" "
966 "-to-> size=%"PRIu64
", off=%"PRIu64
"",
967 pr
->size
, pr
->off
, size
, off
));
971 pr
->flags
&= ~BLKID_FL_TINY_DEV
;
973 if (pr
->size
<= 1440ULL * 1024ULL && !S_ISCHR(pr
->mode
))
974 pr
->flags
|= BLKID_FL_TINY_DEV
;
976 blkid_probe_reset_buffers(pr
);
982 * Check for matching magic value.
983 * Returns BLKID_PROBE_OK if found, BLKID_PROBE_NONE if not found
984 * or no magic present, or negative value on error.
986 int blkid_probe_get_idmag(blkid_probe pr
, const struct blkid_idinfo
*id
,
987 uint64_t *offset
, const struct blkid_idmag
**res
)
989 const struct blkid_idmag
*mag
= NULL
;
993 mag
= &id
->magics
[0];
997 /* try to detect by magic string */
998 while(mag
&& mag
->magic
) {
1001 off
= (mag
->kboff
+ (mag
->sboff
>> 10)) << 10;
1002 buf
= blkid_probe_get_buffer(pr
, off
, 1024);
1007 if (buf
&& !memcmp(mag
->magic
,
1008 buf
+ (mag
->sboff
& 0x3ff), mag
->len
)) {
1010 DBG(LOWPROBE
, ul_debug("\tmagic sboff=%u, kboff=%ld",
1011 mag
->sboff
, mag
->kboff
));
1013 *offset
= off
+ (mag
->sboff
& 0x3ff);
1016 return BLKID_PROBE_OK
;
1021 if (id
&& id
->magics
[0].magic
)
1022 /* magic string(s) defined, but not found */
1023 return BLKID_PROBE_NONE
;
1025 return BLKID_PROBE_OK
;
1028 static inline void blkid_probe_start(blkid_probe pr
)
1030 DBG(LOWPROBE
, ul_debug("start probe"));
1031 pr
->cur_chain
= NULL
;
1033 blkid_probe_set_wiper(pr
, 0, 0);
1036 static inline void blkid_probe_end(blkid_probe pr
)
1038 DBG(LOWPROBE
, ul_debug("end probe"));
1039 pr
->cur_chain
= NULL
;
1041 blkid_probe_set_wiper(pr
, 0, 0);
1048 * Calls probing functions in all enabled chains. The superblocks chain is
1049 * enabled by default. The blkid_do_probe() stores result from only one
1050 * probing function. It's necessary to call this routine in a loop to get
1051 * results from all probing functions in all chains. The probing is reset
1052 * by blkid_reset_probe() or by filter functions.
1054 * This is string-based NAME=value interface only.
1057 * <title>basic case - use the first result only</title>
1059 * if (blkid_do_probe(pr) == 0) {
1060 * int nvals = blkid_probe_numof_values(pr);
1061 * for (n = 0; n < nvals; n++) {
1062 * if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0)
1063 * printf("%s = %s\n", name, data);
1070 * <title>advanced case - probe for all signatures</title>
1072 * while (blkid_do_probe(pr) == 0) {
1073 * int nvals = blkid_probe_numof_values(pr);
1079 * See also blkid_reset_probe().
1081 * Returns: 0 on success, 1 when probing is done and -1 in case of error.
1083 int blkid_do_probe(blkid_probe pr
)
1087 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1091 struct blkid_chain
*chn
= pr
->cur_chain
;
1094 blkid_probe_start(pr
);
1095 chn
= pr
->cur_chain
= &pr
->chains
[0];
1097 /* we go to the next chain only when the previous probing
1098 * result was nothing (rc == 1) and when the current chain is
1099 * disabled or we are at end of the current chain (chain->idx +
1100 * 1 == sizeof chain) or the current chain bailed out right at
1101 * the start (chain->idx == -1)
1103 else if (rc
== 1 && (chn
->enabled
== FALSE
||
1104 chn
->idx
+ 1 == (int) chn
->driver
->nidinfos
||
1107 size_t idx
= chn
->driver
->id
+ 1;
1109 if (idx
< BLKID_NCHAINS
)
1110 chn
= pr
->cur_chain
= &pr
->chains
[idx
];
1112 blkid_probe_end(pr
);
1113 return 1; /* all chains already probed */
1117 chn
->binary
= FALSE
; /* for sure... */
1119 DBG(LOWPROBE
, ul_debug("chain probe %s %s (idx=%d)",
1121 chn
->enabled
? "ENABLED" : "DISABLED",
1127 /* rc: -1 = error, 0 = success, 1 = no result */
1128 rc
= chn
->driver
->probe(pr
, chn
);
1138 * @dryrun: if TRUE then don't touch the device.
1140 * This function erases the current signature detected by @pr. The @pr has to
1141 * be open in O_RDWR mode, BLKID_SUBLKS_MAGIC or/and BLKID_PARTS_MAGIC flags
1142 * has to be enabled (if you want to erase also superblock with broken check
1143 * sums then use BLKID_SUBLKS_BADCSUM too).
1145 * After successful signature removing the @pr prober will be moved one step
1146 * back and the next blkid_do_probe() call will again call previously called
1147 * probing function. All in-memory cached data from the device are always
1151 * <title>wipe all filesystems or raids from the device</title>
1153 * fd = open(devname, O_RDWR|O_CLOEXEC);
1154 * blkid_probe_set_device(pr, fd, 0, 0);
1156 * blkid_probe_enable_superblocks(pr, 1);
1157 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1159 * while (blkid_do_probe(pr) == 0)
1160 * blkid_do_wipe(pr, FALSE);
1164 * See also blkid_probe_step_back() if you cannot use this build-in wipe
1165 * function, but you want to use libblkid probing as a source for wiping.
1167 * Returns: 0 on success, and -1 in case of error.
1169 int blkid_do_wipe(blkid_probe pr
, int dryrun
)
1171 const char *off
= NULL
;
1173 uint64_t offset
, magoff
, l
;
1176 struct blkid_chain
*chn
;
1178 chn
= pr
->cur_chain
;
1182 switch (chn
->driver
->id
) {
1183 case BLKID_CHAIN_SUBLKS
:
1184 rc
= blkid_probe_lookup_value(pr
, "SBMAGIC_OFFSET", &off
, NULL
);
1186 rc
= blkid_probe_lookup_value(pr
, "SBMAGIC", NULL
, &len
);
1188 case BLKID_CHAIN_PARTS
:
1189 rc
= blkid_probe_lookup_value(pr
, "PTMAGIC_OFFSET", &off
, NULL
);
1191 rc
= blkid_probe_lookup_value(pr
, "PTMAGIC", NULL
, &len
);
1197 if (rc
|| len
== 0 || off
== NULL
)
1200 magoff
= strtoumax(off
, NULL
, 10);
1201 offset
= magoff
+ pr
->off
;
1202 fd
= blkid_probe_get_fd(pr
);
1206 if (len
> sizeof(buf
))
1209 DBG(LOWPROBE
, ul_debug(
1210 "do_wipe [offset=0x%"PRIx64
" (%"PRIu64
"), len=%zu, chain=%s, idx=%d, dryrun=%s]\n",
1211 offset
, offset
, len
, chn
->driver
->name
, chn
->idx
, dryrun
? "yes" : "not"));
1213 l
= blkid_llseek(fd
, offset
, SEEK_SET
);
1214 if ((blkid_loff_t
)l
== (off_t
) -1)
1217 memset(buf
, 0, len
);
1219 if (!dryrun
&& len
) {
1220 /* wipen on device */
1221 if (write_all(fd
, buf
, len
))
1224 pr
->flags
&= ~BLKID_FL_MODIF_BUFF
; /* be paranoid */
1226 return blkid_probe_step_back(pr
);
1228 } else if (dryrun
) {
1229 /* wipe in memory only */
1230 blkid_probe_hide_range(pr
, magoff
, len
);
1231 return blkid_probe_step_back(pr
);
1238 * blkid_probe_step_back:
1241 * This function move pointer to the probing chain one step back -- it means
1242 * that the previously used probing function will be called again in the next
1243 * blkid_do_probe() call.
1245 * This is necessary for example if you erase or modify on-disk superblock
1246 * according to the current libblkid probing result.
1248 * Note that blkid_probe_hide_range() changes semantic of this function and
1249 * cached bufferes are not reset, but library uses in-memory modified
1250 * buffers to call the next probing function.
1253 * <title>wipe all superblock, but use libblkid only for probing</title>
1255 * pr = blkid_new_probe_from_filename(devname);
1257 * blkid_probe_enable_superblocks(pr, 1);
1258 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1260 * blkid_probe_enable_partitions(pr, 1);
1261 * blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC);
1263 * while (blkid_do_probe(pr) == 0) {
1264 * const char *ostr = NULL;
1268 * if (blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &ostr, NULL) == 0)
1269 * blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
1271 * // partition tables
1272 * if (len == 0 && blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &ostr, NULL) == 0)
1273 * blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
1278 * // convert ostr to the real offset by off = strtoll(ostr, NULL, 10);
1279 * // use your stuff to erase @len bytes at the @off
1282 * // retry the last probing to check for backup superblocks ..etc.
1283 * blkid_probe_step_back(pr);
1288 * Returns: 0 on success, and -1 in case of error.
1290 int blkid_probe_step_back(blkid_probe pr
)
1292 struct blkid_chain
*chn
;
1294 chn
= pr
->cur_chain
;
1298 if (!(pr
->flags
& BLKID_FL_MODIF_BUFF
))
1299 blkid_probe_reset_buffers(pr
);
1301 if (chn
->idx
>= 0) {
1303 DBG(LOWPROBE
, ul_debug("step back: moving %s chain index to %d",
1308 if (chn
->idx
== -1) {
1309 /* blkid_do_probe() goes to the next chain if the index
1310 * of the current chain is -1, so we have to set the
1311 * chain pointer to the previous chain.
1313 size_t idx
= chn
->driver
->id
> 0 ? chn
->driver
->id
- 1 : 0;
1315 DBG(LOWPROBE
, ul_debug("step back: moving to previous chain"));
1318 pr
->cur_chain
= &pr
->chains
[idx
];
1320 pr
->cur_chain
= NULL
;
1327 * blkid_do_safeprobe:
1330 * This function gathers probing results from all enabled chains and checks
1331 * for ambivalent results (e.g. more filesystems on the device).
1333 * This is string-based NAME=value interface only.
1335 * Note about superblocks chain -- the function does not check for filesystems
1336 * when a RAID signature is detected. The function also does not check for
1337 * collision between RAIDs. The first detected RAID is returned. The function
1338 * checks for collision between partition table and RAID signature -- it's
1339 * recommended to enable partitions chain together with superblocks chain.
1341 * Returns: 0 on success, 1 if nothing is detected, -2 if ambivalent result is
1342 * detected and -1 on case of error.
1344 int blkid_do_safeprobe(blkid_probe pr
)
1346 int i
, count
= 0, rc
= 0;
1348 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1351 blkid_probe_start(pr
);
1353 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
1354 struct blkid_chain
*chn
;
1356 chn
= pr
->cur_chain
= &pr
->chains
[i
];
1357 chn
->binary
= FALSE
; /* for sure... */
1359 DBG(LOWPROBE
, ul_debug("chain safeprobe %s %s",
1361 chn
->enabled
? "ENABLED" : "DISABLED"));
1366 blkid_probe_chain_reset_position(chn
);
1368 rc
= chn
->driver
->safeprobe(pr
, chn
);
1370 blkid_probe_chain_reset_position(chn
);
1372 /* rc: -2 ambivalent, -1 = error, 0 = success, 1 = no result */
1374 goto done
; /* error */
1376 count
++; /* success */
1380 blkid_probe_end(pr
);
1383 return count
? 0 : 1;
1387 * blkid_do_fullprobe:
1390 * This function gathers probing results from all enabled chains. Same as
1391 * blkid_do_safeprobe() but does not check for collision between probing
1394 * This is string-based NAME=value interface only.
1396 * Returns: 0 on success, 1 if nothing is detected or -1 on case of error.
1398 int blkid_do_fullprobe(blkid_probe pr
)
1400 int i
, count
= 0, rc
= 0;
1402 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1405 blkid_probe_start(pr
);
1407 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
1408 struct blkid_chain
*chn
;
1410 chn
= pr
->cur_chain
= &pr
->chains
[i
];
1411 chn
->binary
= FALSE
; /* for sure... */
1413 DBG(LOWPROBE
, ul_debug("chain fullprobe %s: %s",
1415 chn
->enabled
? "ENABLED" : "DISABLED"));
1420 blkid_probe_chain_reset_position(chn
);
1422 rc
= chn
->driver
->probe(pr
, chn
);
1424 blkid_probe_chain_reset_position(chn
);
1426 /* rc: -1 = error, 0 = success, 1 = no result */
1428 goto done
; /* error */
1430 count
++; /* success */
1434 blkid_probe_end(pr
);
1437 return count
? 0 : 1;
1440 /* same sa blkid_probe_get_buffer() but works with 512-sectors */
1441 unsigned char *blkid_probe_get_sector(blkid_probe pr
, unsigned int sector
)
1443 return blkid_probe_get_buffer(pr
, ((uint64_t) sector
) << 9, 0x200);
1446 struct blkid_prval
*blkid_probe_assign_value(blkid_probe pr
, const char *name
)
1448 struct blkid_prval
*v
;
1450 v
= calloc(1, sizeof(struct blkid_prval
));
1454 INIT_LIST_HEAD(&v
->prvals
);
1456 v
->chain
= pr
->cur_chain
;
1457 list_add_tail(&v
->prvals
, &pr
->values
);
1459 DBG(LOWPROBE
, ul_debug("assigning %s [%s]", name
, v
->chain
->driver
->name
));
1463 /* Note that value data is always terminated by zero to keep things robust,
1464 * this extra zero is not count to the value length. It's caller responsibility
1465 * to set proper value length (for strings we count terminator to the length,
1466 * for binary data it's without terminator).
1468 int blkid_probe_value_set_data(struct blkid_prval
*v
,
1469 const unsigned char *data
, size_t len
)
1471 v
->data
= calloc(1, len
+ 1); /* always terminate by \0 */
1475 memcpy(v
->data
, data
, len
);
1480 int blkid_probe_set_value(blkid_probe pr
, const char *name
,
1481 const unsigned char *data
, size_t len
)
1483 struct blkid_prval
*v
;
1485 v
= blkid_probe_assign_value(pr
, name
);
1489 return blkid_probe_value_set_data(v
, data
, len
);
1492 int blkid_probe_vsprintf_value(blkid_probe pr
, const char *name
,
1493 const char *fmt
, va_list ap
)
1495 struct blkid_prval
*v
;
1498 v
= blkid_probe_assign_value(pr
, name
);
1502 len
= vasprintf((char **) &v
->data
, fmt
, ap
);
1505 blkid_probe_free_value(v
);
1506 return len
== 0 ? -EINVAL
: -ENOMEM
;
1512 int blkid_probe_sprintf_value(blkid_probe pr
, const char *name
,
1513 const char *fmt
, ...)
1519 rc
= blkid_probe_vsprintf_value(pr
, name
, fmt
, ap
);
1525 int blkid_probe_set_magic(blkid_probe pr
, uint64_t offset
,
1526 size_t len
, const unsigned char *magic
)
1529 struct blkid_chain
*chn
= blkid_probe_get_chain(pr
);
1531 if (!chn
|| !len
|| chn
->binary
)
1534 switch (chn
->driver
->id
) {
1535 case BLKID_CHAIN_SUBLKS
:
1536 if (!(chn
->flags
& BLKID_SUBLKS_MAGIC
))
1538 rc
= blkid_probe_set_value(pr
, "SBMAGIC", magic
, len
);
1540 rc
= blkid_probe_sprintf_value(pr
,
1541 "SBMAGIC_OFFSET", "%llu", (unsigned long long)offset
);
1543 case BLKID_CHAIN_PARTS
:
1544 if (!(chn
->flags
& BLKID_PARTS_MAGIC
))
1546 rc
= blkid_probe_set_value(pr
, "PTMAGIC", magic
, len
);
1548 rc
= blkid_probe_sprintf_value(pr
,
1549 "PTMAGIC_OFFSET", "%llu", (unsigned long long)offset
);
1558 int blkid_probe_verify_csum(blkid_probe pr
, uint64_t csum
, uint64_t expected
)
1560 if (csum
!= expected
) {
1561 struct blkid_chain
*chn
= blkid_probe_get_chain(pr
);
1563 DBG(LOWPROBE
, ul_debug(
1564 "incorrect checksum for type %s,"
1565 " got %"PRIX64
", expected %"PRIX64
"",
1566 blkid_probe_get_probername(pr
),
1569 * Accept bad checksum if BLKID_SUBLKS_BADCSUM flags is set
1571 if (chn
->driver
->id
== BLKID_CHAIN_SUBLKS
1572 && (chn
->flags
& BLKID_SUBLKS_BADCSUM
)) {
1573 blkid_probe_set_value(pr
, "SBBADCSUM", (unsigned char *) "1", 2);
1576 return 0; /* bad checksum */
1584 * blkid_probe_get_devno:
1587 * Returns: block device number, or 0 for regular files.
1589 dev_t
blkid_probe_get_devno(blkid_probe pr
)
1595 * blkid_probe_get_wholedisk_devno:
1598 * Returns: device number of the wholedisk, or 0 for regular files.
1600 dev_t
blkid_probe_get_wholedisk_devno(blkid_probe pr
)
1602 if (!pr
->disk_devno
) {
1603 dev_t devno
, disk_devno
= 0;
1605 devno
= blkid_probe_get_devno(pr
);
1609 if (blkid_devno_to_wholedisk(devno
, NULL
, 0, &disk_devno
) == 0)
1610 pr
->disk_devno
= disk_devno
;
1612 return pr
->disk_devno
;
1616 * blkid_probe_is_wholedisk:
1619 * Returns: 1 if the device is whole-disk or 0.
1621 int blkid_probe_is_wholedisk(blkid_probe pr
)
1623 dev_t devno
, disk_devno
;
1625 devno
= blkid_probe_get_devno(pr
);
1629 disk_devno
= blkid_probe_get_wholedisk_devno(pr
);
1633 return devno
== disk_devno
;
1636 blkid_probe
blkid_probe_get_wholedisk_probe(blkid_probe pr
)
1640 if (blkid_probe_is_wholedisk(pr
))
1641 return NULL
; /* this is not partition */
1644 /* this is cloned blkid_probe, use parent's stuff */
1645 return blkid_probe_get_wholedisk_probe(pr
->parent
);
1647 disk
= blkid_probe_get_wholedisk_devno(pr
);
1649 if (pr
->disk_probe
&& pr
->disk_probe
->devno
!= disk
) {
1650 /* we have disk prober, but for another disk... close it */
1651 blkid_free_probe(pr
->disk_probe
);
1652 pr
->disk_probe
= NULL
;
1655 if (!pr
->disk_probe
) {
1656 /* Open a new disk prober */
1657 char *disk_path
= blkid_devno_to_devname(disk
);
1662 DBG(LOWPROBE
, ul_debug("allocate a wholedisk probe"));
1664 pr
->disk_probe
= blkid_new_probe_from_filename(disk_path
);
1668 if (!pr
->disk_probe
)
1669 return NULL
; /* ENOMEM? */
1672 return pr
->disk_probe
;
1676 * blkid_probe_get_size:
1679 * This function returns size of probing area as defined by blkid_probe_set_device().
1680 * If the size of the probing area is unrestricted then this function returns
1681 * the real size of device. See also blkid_get_dev_size().
1683 * Returns: size in bytes or -1 in case of error.
1685 blkid_loff_t
blkid_probe_get_size(blkid_probe pr
)
1687 return (blkid_loff_t
) pr
->size
;
1691 * blkid_probe_get_offset:
1694 * This function returns offset of probing area as defined by blkid_probe_set_device().
1696 * Returns: offset in bytes or -1 in case of error.
1698 blkid_loff_t
blkid_probe_get_offset(blkid_probe pr
)
1700 return (blkid_loff_t
) pr
->off
;
1704 * blkid_probe_get_fd:
1707 * Returns: file descriptor for assigned device/file or -1 in case of error.
1709 int blkid_probe_get_fd(blkid_probe pr
)
1715 * blkid_probe_get_sectorsize:
1716 * @pr: probe or NULL (for NULL returns 512)
1718 * Returns: block device logical sector size (BLKSSZGET ioctl, default 512).
1720 unsigned int blkid_probe_get_sectorsize(blkid_probe pr
)
1725 if (S_ISBLK(pr
->mode
) &&
1726 blkdev_get_sector_size(pr
->fd
, (int *) &pr
->blkssz
) == 0)
1729 pr
->blkssz
= DEFAULT_SECTOR_SIZE
;
1734 * blkid_probe_set_sectorsize:
1736 * @sz: new size (to overwrite system default)
1738 * Note that blkid_probe_set_device() resets this setting. Use it after
1739 * blkid_probe_set_device() and before any probing call.
1743 * Returns: 0 or <0 in case of error
1745 int blkid_probe_set_sectorsize(blkid_probe pr
, unsigned int sz
)
1752 * blkid_probe_get_sectors:
1755 * Returns: 512-byte sector count or -1 in case of error.
1757 blkid_loff_t
blkid_probe_get_sectors(blkid_probe pr
)
1759 return (blkid_loff_t
) (pr
->size
>> 9);
1763 * blkid_probe_numof_values:
1766 * Returns: number of values in probing result or -1 in case of error.
1768 int blkid_probe_numof_values(blkid_probe pr
)
1771 struct list_head
*p
;
1773 list_for_each(p
, &pr
->values
)
1779 * blkid_probe_get_value:
1781 * @num: wanted value in range 0..N, where N is blkid_probe_numof_values() - 1
1782 * @name: pointer to return value name or NULL
1783 * @data: pointer to return value data or NULL
1784 * @len: pointer to return value length or NULL
1786 * Note, the @len returns length of the @data, including the terminating
1789 * Returns: 0 on success, or -1 in case of error.
1791 int blkid_probe_get_value(blkid_probe pr
, int num
, const char **name
,
1792 const char **data
, size_t *len
)
1794 struct blkid_prval
*v
= __blkid_probe_get_value(pr
, num
);
1801 *data
= (char *) v
->data
;
1805 DBG(LOWPROBE
, ul_debug("returning %s value", v
->name
));
1810 * blkid_probe_lookup_value:
1812 * @name: name of value
1813 * @data: pointer to return value data or NULL
1814 * @len: pointer to return value length or NULL
1816 * Note, the @len returns length of the @data, including the terminating
1819 * Returns: 0 on success, or -1 in case of error.
1821 int blkid_probe_lookup_value(blkid_probe pr
, const char *name
,
1822 const char **data
, size_t *len
)
1824 struct blkid_prval
*v
= __blkid_probe_lookup_value(pr
, name
);
1829 *data
= (char *) v
->data
;
1836 * blkid_probe_has_value:
1838 * @name: name of value
1840 * Returns: 1 if value exist in probing result, otherwise 0.
1842 int blkid_probe_has_value(blkid_probe pr
, const char *name
)
1844 if (blkid_probe_lookup_value(pr
, name
, NULL
, NULL
) == 0)
1849 struct blkid_prval
*__blkid_probe_get_value(blkid_probe pr
, int num
)
1852 struct list_head
*p
;
1857 list_for_each(p
, &pr
->values
) {
1860 return list_entry(p
, struct blkid_prval
, prvals
);
1865 struct blkid_prval
*__blkid_probe_lookup_value(blkid_probe pr
, const char *name
)
1867 struct list_head
*p
;
1869 if (list_empty(&pr
->values
))
1872 list_for_each(p
, &pr
->values
) {
1873 struct blkid_prval
*v
= list_entry(p
, struct blkid_prval
,
1876 if (v
->name
&& strcmp(name
, v
->name
) == 0) {
1877 DBG(LOWPROBE
, ul_debug("returning %s value", v
->name
));
1885 /* converts DCE UUID (uuid[16]) to human readable string
1886 * - the @len should be always 37 */
1887 void blkid_unparse_uuid(const unsigned char *uuid
, char *str
, size_t len
)
1890 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1891 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1895 uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14],uuid
[15]);
1898 /* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */
1899 int blkid_uuid_is_empty(const unsigned char *buf
, size_t len
)
1903 for (i
= 0; i
< len
; i
++)
1909 /* Removes whitespace from the right-hand side of a string (trailing
1912 * Returns size of the new string (without \0).
1914 size_t blkid_rtrim_whitespace(unsigned char *str
)
1916 return rtrim_whitespace(str
);
1919 /* Removes whitespace from the left-hand side of a string.
1921 * Returns size of the new string (without \0).
1923 size_t blkid_ltrim_whitespace(unsigned char *str
)
1925 return ltrim_whitespace(str
);
1929 * Some mkfs-like utils wipe some parts (usually begin) of the device.
1930 * For example LVM (pvcreate) or mkswap(8). This information could be used
1931 * for later resolution to conflicts between superblocks.
1933 * For example we found valid LVM superblock, LVM wipes 8KiB at the begin of
1934 * the device. If we found another signature (for example MBR) within the
1935 * wiped area then the signature has been added later and LVM superblock
1938 * Note that this heuristic is not 100% reliable, for example "pvcreate --zero
1939 * n" allows to keep the begin of the device unmodified. It's probably better
1940 * to use this heuristic for conflicts between superblocks and partition tables
1941 * than for conflicts between filesystem superblocks -- existence of unwanted
1942 * partition table is very unusual, because PT is pretty visible (parsed and
1943 * interpreted by kernel).
1945 * Note that we usually expect only one signature on the device, it means that
1946 * we have to remember only one wiped area from previously successfully
1947 * detected signature.
1949 * blkid_probe_set_wiper() -- defines wiped area (e.g. LVM)
1950 * blkid_probe_use_wiper() -- try to use area (e.g. MBR)
1952 * Note that there is not relation between _wiper and blkid_to_wipe().
1955 void blkid_probe_set_wiper(blkid_probe pr
, uint64_t off
, uint64_t size
)
1957 struct blkid_chain
*chn
;
1960 DBG(LOWPROBE
, ul_debug("zeroize wiper"));
1961 pr
->wipe_size
= pr
->wipe_off
= 0;
1962 pr
->wipe_chain
= NULL
;
1966 chn
= pr
->cur_chain
;
1968 if (!chn
|| !chn
->driver
||
1969 chn
->idx
< 0 || (size_t) chn
->idx
>= chn
->driver
->nidinfos
)
1972 pr
->wipe_size
= size
;
1974 pr
->wipe_chain
= chn
;
1977 ul_debug("wiper set to %s::%s off=%"PRIu64
" size=%"PRIu64
"",
1979 chn
->driver
->idinfos
[chn
->idx
]->name
,
1980 pr
->wipe_off
, pr
->wipe_size
));
1985 * Returns 1 if the <@off,@size> area was wiped
1987 int blkid_probe_is_wiped(blkid_probe pr
, struct blkid_chain
**chn
, uint64_t off
, uint64_t size
)
1992 if (pr
->wipe_off
<= off
&& off
+ size
<= pr
->wipe_off
+ pr
->wipe_size
) {
1993 *chn
= pr
->wipe_chain
;
2000 * Try to use any area -- if the area has been previously wiped then the
2001 * previous probing result should be ignored (reset).
2003 void blkid_probe_use_wiper(blkid_probe pr
, uint64_t off
, uint64_t size
)
2005 struct blkid_chain
*chn
= NULL
;
2007 if (blkid_probe_is_wiped(pr
, &chn
, off
, size
) && chn
) {
2008 DBG(LOWPROBE
, ul_debug("previously wiped area modified "
2009 " -- ignore previous results"));
2010 blkid_probe_set_wiper(pr
, 0, 0);
2011 blkid_probe_chain_reset_values(pr
, chn
);