]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/probe.c
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, resets
850 * the current probing, and close previously associated device (if open by
853 * If @fd is < 0 than only resets the prober and returns 1. Note that
854 * blkid_reset_probe() keeps the device associated with the prober, but
855 * blkid_probe_set_device() does complete reset.
857 * Returns: -1 in case of failure, 0 on success and 1 on reset.
859 int blkid_probe_set_device(blkid_probe pr
, int fd
,
860 blkid_loff_t off
, blkid_loff_t size
)
864 char *dm_uuid
= NULL
;
866 blkid_reset_probe(pr
);
867 blkid_probe_reset_buffers(pr
);
869 if ((pr
->flags
& BLKID_FL_PRIVATE_FD
) && pr
->fd
>= 0)
872 if (pr
->disk_probe
) {
873 blkid_free_probe(pr
->disk_probe
);
874 pr
->disk_probe
= NULL
;
877 pr
->flags
&= ~BLKID_FL_PRIVATE_FD
;
878 pr
->flags
&= ~BLKID_FL_TINY_DEV
;
879 pr
->flags
&= ~BLKID_FL_CDROM_DEV
;
882 pr
->off
= (uint64_t) off
;
890 pr
->wipe_chain
= NULL
;
895 #if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE)
896 /* Disable read-ahead */
897 posix_fadvise(fd
, 0, 0, POSIX_FADV_RANDOM
);
902 if (!S_ISBLK(sb
.st_mode
) && !S_ISCHR(sb
.st_mode
) && !S_ISREG(sb
.st_mode
)) {
907 pr
->mode
= sb
.st_mode
;
908 if (S_ISBLK(sb
.st_mode
) || S_ISCHR(sb
.st_mode
))
909 pr
->devno
= sb
.st_rdev
;
911 if (S_ISBLK(sb
.st_mode
)) {
912 if (blkdev_get_size(fd
, (unsigned long long *) &devsiz
)) {
913 DBG(LOWPROBE
, ul_debug("failed to get device size"));
916 } else if (S_ISCHR(sb
.st_mode
))
917 devsiz
= 1; /* UBI devices are char... */
918 else if (S_ISREG(sb
.st_mode
))
919 devsiz
= sb
.st_size
; /* regular file */
921 pr
->size
= size
? (uint64_t)size
: devsiz
;
923 if (off
&& size
== 0)
924 /* only offset without size specified */
925 pr
->size
-= (uint64_t) off
;
927 if (pr
->off
+ pr
->size
> devsiz
) {
928 DBG(LOWPROBE
, ul_debug("area specified by offset and size is bigger than device"));
933 if (pr
->size
<= 1440 * 1024 && !S_ISCHR(sb
.st_mode
))
934 pr
->flags
|= BLKID_FL_TINY_DEV
;
936 if (S_ISBLK(sb
.st_mode
) &&
937 sysfs_devno_is_dm_private(sb
.st_rdev
, &dm_uuid
)) {
938 DBG(LOWPROBE
, ul_debug("ignore private device mapper device"));
939 pr
->flags
|= BLKID_FL_NOSCAN_DEV
;
942 #ifdef CDROM_GET_CAPABILITY
943 else if (S_ISBLK(sb
.st_mode
) &&
944 !blkid_probe_is_tiny(pr
) &&
946 blkid_probe_is_wholedisk(pr
) &&
947 ioctl(fd
, CDROM_GET_CAPABILITY
, NULL
) >= 0) {
949 pr
->flags
|= BLKID_FL_CDROM_DEV
;
950 cdrom_size_correction(pr
);
955 DBG(LOWPROBE
, ul_debug("ready for low-probing, offset=%"PRIu64
", size=%"PRIu64
"",
957 DBG(LOWPROBE
, ul_debug("whole-disk: %s, regfile: %s",
958 blkid_probe_is_wholedisk(pr
) ?"YES" : "NO",
959 S_ISREG(pr
->mode
) ? "YES" : "NO"));
963 DBG(LOWPROBE
, ul_debug("failed to prepare a device for low-probing"));
968 int blkid_probe_get_dimension(blkid_probe pr
, uint64_t *off
, uint64_t *size
)
975 int blkid_probe_set_dimension(blkid_probe pr
, uint64_t off
, uint64_t size
)
977 DBG(LOWPROBE
, ul_debug(
978 "changing probing area: size=%"PRIu64
", off=%"PRIu64
" "
979 "-to-> size=%"PRIu64
", off=%"PRIu64
"",
980 pr
->size
, pr
->off
, size
, off
));
984 pr
->flags
&= ~BLKID_FL_TINY_DEV
;
986 if (pr
->size
<= 1440ULL * 1024ULL && !S_ISCHR(pr
->mode
))
987 pr
->flags
|= BLKID_FL_TINY_DEV
;
989 blkid_probe_reset_buffers(pr
);
995 * Check for matching magic value.
996 * Returns BLKID_PROBE_OK if found, BLKID_PROBE_NONE if not found
997 * or no magic present, or negative value on error.
999 int blkid_probe_get_idmag(blkid_probe pr
, const struct blkid_idinfo
*id
,
1000 uint64_t *offset
, const struct blkid_idmag
**res
)
1002 const struct blkid_idmag
*mag
= NULL
;
1006 mag
= &id
->magics
[0];
1010 /* try to detect by magic string */
1011 while(mag
&& mag
->magic
) {
1014 off
= (mag
->kboff
+ (mag
->sboff
>> 10)) << 10;
1015 buf
= blkid_probe_get_buffer(pr
, off
, 1024);
1020 if (buf
&& !memcmp(mag
->magic
,
1021 buf
+ (mag
->sboff
& 0x3ff), mag
->len
)) {
1023 DBG(LOWPROBE
, ul_debug("\tmagic sboff=%u, kboff=%ld",
1024 mag
->sboff
, mag
->kboff
));
1026 *offset
= off
+ (mag
->sboff
& 0x3ff);
1029 return BLKID_PROBE_OK
;
1034 if (id
&& id
->magics
[0].magic
)
1035 /* magic string(s) defined, but not found */
1036 return BLKID_PROBE_NONE
;
1038 return BLKID_PROBE_OK
;
1041 static inline void blkid_probe_start(blkid_probe pr
)
1043 DBG(LOWPROBE
, ul_debug("start probe"));
1044 pr
->cur_chain
= NULL
;
1046 blkid_probe_set_wiper(pr
, 0, 0);
1049 static inline void blkid_probe_end(blkid_probe pr
)
1051 DBG(LOWPROBE
, ul_debug("end probe"));
1052 pr
->cur_chain
= NULL
;
1054 blkid_probe_set_wiper(pr
, 0, 0);
1061 * Calls probing functions in all enabled chains. The superblocks chain is
1062 * enabled by default. The blkid_do_probe() stores result from only one
1063 * probing function. It's necessary to call this routine in a loop to get
1064 * results from all probing functions in all chains. The probing is reset
1065 * by blkid_reset_probe() or by filter functions.
1067 * This is string-based NAME=value interface only.
1070 * <title>basic case - use the first result only</title>
1072 * if (blkid_do_probe(pr) == 0) {
1073 * int nvals = blkid_probe_numof_values(pr);
1074 * for (n = 0; n < nvals; n++) {
1075 * if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0)
1076 * printf("%s = %s\n", name, data);
1083 * <title>advanced case - probe for all signatures</title>
1085 * while (blkid_do_probe(pr) == 0) {
1086 * int nvals = blkid_probe_numof_values(pr);
1092 * See also blkid_reset_probe().
1094 * Returns: 0 on success, 1 when probing is done and -1 in case of error.
1096 int blkid_do_probe(blkid_probe pr
)
1100 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1104 struct blkid_chain
*chn
= pr
->cur_chain
;
1107 blkid_probe_start(pr
);
1108 chn
= pr
->cur_chain
= &pr
->chains
[0];
1110 /* we go to the next chain only when the previous probing
1111 * result was nothing (rc == 1) and when the current chain is
1112 * disabled or we are at end of the current chain (chain->idx +
1113 * 1 == sizeof chain) or the current chain bailed out right at
1114 * the start (chain->idx == -1)
1116 else if (rc
== 1 && (chn
->enabled
== FALSE
||
1117 chn
->idx
+ 1 == (int) chn
->driver
->nidinfos
||
1120 size_t idx
= chn
->driver
->id
+ 1;
1122 if (idx
< BLKID_NCHAINS
)
1123 chn
= pr
->cur_chain
= &pr
->chains
[idx
];
1125 blkid_probe_end(pr
);
1126 return 1; /* all chains already probed */
1130 chn
->binary
= FALSE
; /* for sure... */
1132 DBG(LOWPROBE
, ul_debug("chain probe %s %s (idx=%d)",
1134 chn
->enabled
? "ENABLED" : "DISABLED",
1140 /* rc: -1 = error, 0 = success, 1 = no result */
1141 rc
= chn
->driver
->probe(pr
, chn
);
1151 * @dryrun: if TRUE then don't touch the device.
1153 * This function erases the current signature detected by @pr. The @pr has to
1154 * be open in O_RDWR mode, BLKID_SUBLKS_MAGIC or/and BLKID_PARTS_MAGIC flags
1155 * has to be enabled (if you want to erase also superblock with broken check
1156 * sums then use BLKID_SUBLKS_BADCSUM too).
1158 * After successful signature removing the @pr prober will be moved one step
1159 * back and the next blkid_do_probe() call will again call previously called
1160 * probing function. All in-memory cached data from the device are always
1164 * <title>wipe all filesystems or raids from the device</title>
1166 * fd = open(devname, O_RDWR|O_CLOEXEC);
1167 * blkid_probe_set_device(pr, fd, 0, 0);
1169 * blkid_probe_enable_superblocks(pr, 1);
1170 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1172 * while (blkid_do_probe(pr) == 0)
1173 * blkid_do_wipe(pr, FALSE);
1177 * See also blkid_probe_step_back() if you cannot use this built-in wipe
1178 * function, but you want to use libblkid probing as a source for wiping.
1180 * Returns: 0 on success, and -1 in case of error.
1182 int blkid_do_wipe(blkid_probe pr
, int dryrun
)
1184 const char *off
= NULL
;
1186 uint64_t offset
, magoff
, l
;
1189 struct blkid_chain
*chn
;
1191 chn
= pr
->cur_chain
;
1195 switch (chn
->driver
->id
) {
1196 case BLKID_CHAIN_SUBLKS
:
1197 rc
= blkid_probe_lookup_value(pr
, "SBMAGIC_OFFSET", &off
, NULL
);
1199 rc
= blkid_probe_lookup_value(pr
, "SBMAGIC", NULL
, &len
);
1201 case BLKID_CHAIN_PARTS
:
1202 rc
= blkid_probe_lookup_value(pr
, "PTMAGIC_OFFSET", &off
, NULL
);
1204 rc
= blkid_probe_lookup_value(pr
, "PTMAGIC", NULL
, &len
);
1210 if (rc
|| len
== 0 || off
== NULL
)
1213 magoff
= strtoumax(off
, NULL
, 10);
1214 offset
= magoff
+ pr
->off
;
1215 fd
= blkid_probe_get_fd(pr
);
1219 if (len
> sizeof(buf
))
1222 DBG(LOWPROBE
, ul_debug(
1223 "do_wipe [offset=0x%"PRIx64
" (%"PRIu64
"), len=%zu, chain=%s, idx=%d, dryrun=%s]\n",
1224 offset
, offset
, len
, chn
->driver
->name
, chn
->idx
, dryrun
? "yes" : "not"));
1226 l
= blkid_llseek(fd
, offset
, SEEK_SET
);
1227 if ((blkid_loff_t
)l
== (off_t
) -1)
1230 memset(buf
, 0, len
);
1232 if (!dryrun
&& len
) {
1233 /* wipen on device */
1234 if (write_all(fd
, buf
, len
))
1237 pr
->flags
&= ~BLKID_FL_MODIF_BUFF
; /* be paranoid */
1239 return blkid_probe_step_back(pr
);
1241 } else if (dryrun
) {
1242 /* wipe in memory only */
1243 blkid_probe_hide_range(pr
, magoff
, len
);
1244 return blkid_probe_step_back(pr
);
1251 * blkid_probe_step_back:
1254 * This function move pointer to the probing chain one step back -- it means
1255 * that the previously used probing function will be called again in the next
1256 * blkid_do_probe() call.
1258 * This is necessary for example if you erase or modify on-disk superblock
1259 * according to the current libblkid probing result.
1261 * Note that blkid_probe_hide_range() changes semantic of this function and
1262 * cached bufferes are not reset, but library uses in-memory modified
1263 * buffers to call the next probing function.
1266 * <title>wipe all superblock, but use libblkid only for probing</title>
1268 * pr = blkid_new_probe_from_filename(devname);
1270 * blkid_probe_enable_superblocks(pr, 1);
1271 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1273 * blkid_probe_enable_partitions(pr, 1);
1274 * blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC);
1276 * while (blkid_do_probe(pr) == 0) {
1277 * const char *ostr = NULL;
1281 * if (blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &ostr, NULL) == 0)
1282 * blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
1284 * // partition tables
1285 * if (len == 0 && blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &ostr, NULL) == 0)
1286 * blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
1291 * // convert ostr to the real offset by off = strtoll(ostr, NULL, 10);
1292 * // use your stuff to erase @len bytes at the @off
1295 * // retry the last probing to check for backup superblocks ..etc.
1296 * blkid_probe_step_back(pr);
1301 * Returns: 0 on success, and -1 in case of error.
1303 int blkid_probe_step_back(blkid_probe pr
)
1305 struct blkid_chain
*chn
;
1307 chn
= pr
->cur_chain
;
1311 if (!(pr
->flags
& BLKID_FL_MODIF_BUFF
))
1312 blkid_probe_reset_buffers(pr
);
1314 if (chn
->idx
>= 0) {
1316 DBG(LOWPROBE
, ul_debug("step back: moving %s chain index to %d",
1321 if (chn
->idx
== -1) {
1322 /* blkid_do_probe() goes to the next chain if the index
1323 * of the current chain is -1, so we have to set the
1324 * chain pointer to the previous chain.
1326 size_t idx
= chn
->driver
->id
> 0 ? chn
->driver
->id
- 1 : 0;
1328 DBG(LOWPROBE
, ul_debug("step back: moving to previous chain"));
1331 pr
->cur_chain
= &pr
->chains
[idx
];
1333 pr
->cur_chain
= NULL
;
1340 * blkid_do_safeprobe:
1343 * This function gathers probing results from all enabled chains and checks
1344 * for ambivalent results (e.g. more filesystems on the device).
1346 * This is string-based NAME=value interface only.
1348 * Note about superblocks chain -- the function does not check for filesystems
1349 * when a RAID signature is detected. The function also does not check for
1350 * collision between RAIDs. The first detected RAID is returned. The function
1351 * checks for collision between partition table and RAID signature -- it's
1352 * recommended to enable partitions chain together with superblocks chain.
1354 * Returns: 0 on success, 1 if nothing is detected, -2 if ambivalent result is
1355 * detected and -1 on case of error.
1357 int blkid_do_safeprobe(blkid_probe pr
)
1359 int i
, count
= 0, rc
= 0;
1361 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1364 blkid_probe_start(pr
);
1366 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
1367 struct blkid_chain
*chn
;
1369 chn
= pr
->cur_chain
= &pr
->chains
[i
];
1370 chn
->binary
= FALSE
; /* for sure... */
1372 DBG(LOWPROBE
, ul_debug("chain safeprobe %s %s",
1374 chn
->enabled
? "ENABLED" : "DISABLED"));
1379 blkid_probe_chain_reset_position(chn
);
1381 rc
= chn
->driver
->safeprobe(pr
, chn
);
1383 blkid_probe_chain_reset_position(chn
);
1385 /* rc: -2 ambivalent, -1 = error, 0 = success, 1 = no result */
1387 goto done
; /* error */
1389 count
++; /* success */
1393 blkid_probe_end(pr
);
1396 return count
? 0 : 1;
1400 * blkid_do_fullprobe:
1403 * This function gathers probing results from all enabled chains. Same as
1404 * blkid_do_safeprobe() but does not check for collision between probing
1407 * This is string-based NAME=value interface only.
1409 * Returns: 0 on success, 1 if nothing is detected or -1 on case of error.
1411 int blkid_do_fullprobe(blkid_probe pr
)
1413 int i
, count
= 0, rc
= 0;
1415 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1418 blkid_probe_start(pr
);
1420 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
1421 struct blkid_chain
*chn
;
1423 chn
= pr
->cur_chain
= &pr
->chains
[i
];
1424 chn
->binary
= FALSE
; /* for sure... */
1426 DBG(LOWPROBE
, ul_debug("chain fullprobe %s: %s",
1428 chn
->enabled
? "ENABLED" : "DISABLED"));
1433 blkid_probe_chain_reset_position(chn
);
1435 rc
= chn
->driver
->probe(pr
, chn
);
1437 blkid_probe_chain_reset_position(chn
);
1439 /* rc: -1 = error, 0 = success, 1 = no result */
1441 goto done
; /* error */
1443 count
++; /* success */
1447 blkid_probe_end(pr
);
1450 return count
? 0 : 1;
1453 /* same sa blkid_probe_get_buffer() but works with 512-sectors */
1454 unsigned char *blkid_probe_get_sector(blkid_probe pr
, unsigned int sector
)
1456 return blkid_probe_get_buffer(pr
, ((uint64_t) sector
) << 9, 0x200);
1459 struct blkid_prval
*blkid_probe_assign_value(blkid_probe pr
, const char *name
)
1461 struct blkid_prval
*v
;
1463 v
= calloc(1, sizeof(struct blkid_prval
));
1467 INIT_LIST_HEAD(&v
->prvals
);
1469 v
->chain
= pr
->cur_chain
;
1470 list_add_tail(&v
->prvals
, &pr
->values
);
1472 DBG(LOWPROBE
, ul_debug("assigning %s [%s]", name
, v
->chain
->driver
->name
));
1476 /* Note that value data is always terminated by zero to keep things robust,
1477 * this extra zero is not count to the value length. It's caller responsibility
1478 * to set proper value length (for strings we count terminator to the length,
1479 * for binary data it's without terminator).
1481 int blkid_probe_value_set_data(struct blkid_prval
*v
,
1482 const unsigned char *data
, size_t len
)
1484 v
->data
= calloc(1, len
+ 1); /* always terminate by \0 */
1488 memcpy(v
->data
, data
, len
);
1493 int blkid_probe_set_value(blkid_probe pr
, const char *name
,
1494 const unsigned char *data
, size_t len
)
1496 struct blkid_prval
*v
;
1498 v
= blkid_probe_assign_value(pr
, name
);
1502 return blkid_probe_value_set_data(v
, data
, len
);
1505 int blkid_probe_vsprintf_value(blkid_probe pr
, const char *name
,
1506 const char *fmt
, va_list ap
)
1508 struct blkid_prval
*v
;
1511 v
= blkid_probe_assign_value(pr
, name
);
1515 len
= vasprintf((char **) &v
->data
, fmt
, ap
);
1518 blkid_probe_free_value(v
);
1519 return len
== 0 ? -EINVAL
: -ENOMEM
;
1525 int blkid_probe_sprintf_value(blkid_probe pr
, const char *name
,
1526 const char *fmt
, ...)
1532 rc
= blkid_probe_vsprintf_value(pr
, name
, fmt
, ap
);
1538 int blkid_probe_set_magic(blkid_probe pr
, uint64_t offset
,
1539 size_t len
, const unsigned char *magic
)
1542 struct blkid_chain
*chn
= blkid_probe_get_chain(pr
);
1544 if (!chn
|| !len
|| chn
->binary
)
1547 switch (chn
->driver
->id
) {
1548 case BLKID_CHAIN_SUBLKS
:
1549 if (!(chn
->flags
& BLKID_SUBLKS_MAGIC
))
1551 rc
= blkid_probe_set_value(pr
, "SBMAGIC", magic
, len
);
1553 rc
= blkid_probe_sprintf_value(pr
,
1554 "SBMAGIC_OFFSET", "%llu", (unsigned long long)offset
);
1556 case BLKID_CHAIN_PARTS
:
1557 if (!(chn
->flags
& BLKID_PARTS_MAGIC
))
1559 rc
= blkid_probe_set_value(pr
, "PTMAGIC", magic
, len
);
1561 rc
= blkid_probe_sprintf_value(pr
,
1562 "PTMAGIC_OFFSET", "%llu", (unsigned long long)offset
);
1571 int blkid_probe_verify_csum(blkid_probe pr
, uint64_t csum
, uint64_t expected
)
1573 if (csum
!= expected
) {
1574 struct blkid_chain
*chn
= blkid_probe_get_chain(pr
);
1576 DBG(LOWPROBE
, ul_debug(
1577 "incorrect checksum for type %s,"
1578 " got %"PRIX64
", expected %"PRIX64
"",
1579 blkid_probe_get_probername(pr
),
1582 * Accept bad checksum if BLKID_SUBLKS_BADCSUM flags is set
1584 if (chn
->driver
->id
== BLKID_CHAIN_SUBLKS
1585 && (chn
->flags
& BLKID_SUBLKS_BADCSUM
)) {
1586 blkid_probe_set_value(pr
, "SBBADCSUM", (unsigned char *) "1", 2);
1589 return 0; /* bad checksum */
1597 * blkid_probe_get_devno:
1600 * Returns: block device number, or 0 for regular files.
1602 dev_t
blkid_probe_get_devno(blkid_probe pr
)
1608 * blkid_probe_get_wholedisk_devno:
1611 * Returns: device number of the wholedisk, or 0 for regular files.
1613 dev_t
blkid_probe_get_wholedisk_devno(blkid_probe pr
)
1615 if (!pr
->disk_devno
) {
1616 dev_t devno
, disk_devno
= 0;
1618 devno
= blkid_probe_get_devno(pr
);
1622 if (blkid_devno_to_wholedisk(devno
, NULL
, 0, &disk_devno
) == 0)
1623 pr
->disk_devno
= disk_devno
;
1625 return pr
->disk_devno
;
1629 * blkid_probe_is_wholedisk:
1632 * Returns: 1 if the device is whole-disk or 0.
1634 int blkid_probe_is_wholedisk(blkid_probe pr
)
1636 dev_t devno
, disk_devno
;
1638 devno
= blkid_probe_get_devno(pr
);
1642 disk_devno
= blkid_probe_get_wholedisk_devno(pr
);
1646 return devno
== disk_devno
;
1649 blkid_probe
blkid_probe_get_wholedisk_probe(blkid_probe pr
)
1653 if (blkid_probe_is_wholedisk(pr
))
1654 return NULL
; /* this is not partition */
1657 /* this is cloned blkid_probe, use parent's stuff */
1658 return blkid_probe_get_wholedisk_probe(pr
->parent
);
1660 disk
= blkid_probe_get_wholedisk_devno(pr
);
1662 if (pr
->disk_probe
&& pr
->disk_probe
->devno
!= disk
) {
1663 /* we have disk prober, but for another disk... close it */
1664 blkid_free_probe(pr
->disk_probe
);
1665 pr
->disk_probe
= NULL
;
1668 if (!pr
->disk_probe
) {
1669 /* Open a new disk prober */
1670 char *disk_path
= blkid_devno_to_devname(disk
);
1675 DBG(LOWPROBE
, ul_debug("allocate a wholedisk probe"));
1677 pr
->disk_probe
= blkid_new_probe_from_filename(disk_path
);
1681 if (!pr
->disk_probe
)
1682 return NULL
; /* ENOMEM? */
1685 return pr
->disk_probe
;
1689 * blkid_probe_get_size:
1692 * This function returns size of probing area as defined by blkid_probe_set_device().
1693 * If the size of the probing area is unrestricted then this function returns
1694 * the real size of device. See also blkid_get_dev_size().
1696 * Returns: size in bytes or -1 in case of error.
1698 blkid_loff_t
blkid_probe_get_size(blkid_probe pr
)
1700 return (blkid_loff_t
) pr
->size
;
1704 * blkid_probe_get_offset:
1707 * This function returns offset of probing area as defined by blkid_probe_set_device().
1709 * Returns: offset in bytes or -1 in case of error.
1711 blkid_loff_t
blkid_probe_get_offset(blkid_probe pr
)
1713 return (blkid_loff_t
) pr
->off
;
1717 * blkid_probe_get_fd:
1720 * Returns: file descriptor for assigned device/file or -1 in case of error.
1722 int blkid_probe_get_fd(blkid_probe pr
)
1728 * blkid_probe_get_sectorsize:
1729 * @pr: probe or NULL (for NULL returns 512)
1731 * Returns: block device logical sector size (BLKSSZGET ioctl, default 512).
1733 unsigned int blkid_probe_get_sectorsize(blkid_probe pr
)
1738 if (S_ISBLK(pr
->mode
) &&
1739 blkdev_get_sector_size(pr
->fd
, (int *) &pr
->blkssz
) == 0)
1742 pr
->blkssz
= DEFAULT_SECTOR_SIZE
;
1747 * blkid_probe_set_sectorsize:
1749 * @sz: new size (to overwrite system default)
1751 * Note that blkid_probe_set_device() resets this setting. Use it after
1752 * blkid_probe_set_device() and before any probing call.
1756 * Returns: 0 or <0 in case of error
1758 int blkid_probe_set_sectorsize(blkid_probe pr
, unsigned int sz
)
1765 * blkid_probe_get_sectors:
1768 * Returns: 512-byte sector count or -1 in case of error.
1770 blkid_loff_t
blkid_probe_get_sectors(blkid_probe pr
)
1772 return (blkid_loff_t
) (pr
->size
>> 9);
1776 * blkid_probe_numof_values:
1779 * Returns: number of values in probing result or -1 in case of error.
1781 int blkid_probe_numof_values(blkid_probe pr
)
1784 struct list_head
*p
;
1786 list_for_each(p
, &pr
->values
)
1792 * blkid_probe_get_value:
1794 * @num: wanted value in range 0..N, where N is blkid_probe_numof_values() - 1
1795 * @name: pointer to return value name or NULL
1796 * @data: pointer to return value data or NULL
1797 * @len: pointer to return value length or NULL
1799 * Note, the @len returns length of the @data, including the terminating
1802 * Returns: 0 on success, or -1 in case of error.
1804 int blkid_probe_get_value(blkid_probe pr
, int num
, const char **name
,
1805 const char **data
, size_t *len
)
1807 struct blkid_prval
*v
= __blkid_probe_get_value(pr
, num
);
1814 *data
= (char *) v
->data
;
1818 DBG(LOWPROBE
, ul_debug("returning %s value", v
->name
));
1823 * blkid_probe_lookup_value:
1825 * @name: name of value
1826 * @data: pointer to return value data or NULL
1827 * @len: pointer to return value length or NULL
1829 * Note, the @len returns length of the @data, including the terminating
1832 * Returns: 0 on success, or -1 in case of error.
1834 int blkid_probe_lookup_value(blkid_probe pr
, const char *name
,
1835 const char **data
, size_t *len
)
1837 struct blkid_prval
*v
= __blkid_probe_lookup_value(pr
, name
);
1842 *data
= (char *) v
->data
;
1849 * blkid_probe_has_value:
1851 * @name: name of value
1853 * Returns: 1 if value exist in probing result, otherwise 0.
1855 int blkid_probe_has_value(blkid_probe pr
, const char *name
)
1857 if (blkid_probe_lookup_value(pr
, name
, NULL
, NULL
) == 0)
1862 struct blkid_prval
*__blkid_probe_get_value(blkid_probe pr
, int num
)
1865 struct list_head
*p
;
1870 list_for_each(p
, &pr
->values
) {
1873 return list_entry(p
, struct blkid_prval
, prvals
);
1878 struct blkid_prval
*__blkid_probe_lookup_value(blkid_probe pr
, const char *name
)
1880 struct list_head
*p
;
1882 if (list_empty(&pr
->values
))
1885 list_for_each(p
, &pr
->values
) {
1886 struct blkid_prval
*v
= list_entry(p
, struct blkid_prval
,
1889 if (v
->name
&& strcmp(name
, v
->name
) == 0) {
1890 DBG(LOWPROBE
, ul_debug("returning %s value", v
->name
));
1898 /* converts DCE UUID (uuid[16]) to human readable string
1899 * - the @len should be always 37 */
1900 void blkid_unparse_uuid(const unsigned char *uuid
, char *str
, size_t len
)
1903 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1904 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1908 uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14],uuid
[15]);
1911 /* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */
1912 int blkid_uuid_is_empty(const unsigned char *buf
, size_t len
)
1916 for (i
= 0; i
< len
; i
++)
1922 /* Removes whitespace from the right-hand side of a string (trailing
1925 * Returns size of the new string (without \0).
1927 size_t blkid_rtrim_whitespace(unsigned char *str
)
1929 return rtrim_whitespace(str
);
1932 /* Removes whitespace from the left-hand side of a string.
1934 * Returns size of the new string (without \0).
1936 size_t blkid_ltrim_whitespace(unsigned char *str
)
1938 return ltrim_whitespace(str
);
1942 * Some mkfs-like utils wipe some parts (usually begin) of the device.
1943 * For example LVM (pvcreate) or mkswap(8). This information could be used
1944 * for later resolution to conflicts between superblocks.
1946 * For example we found valid LVM superblock, LVM wipes 8KiB at the begin of
1947 * the device. If we found another signature (for example MBR) within the
1948 * wiped area then the signature has been added later and LVM superblock
1951 * Note that this heuristic is not 100% reliable, for example "pvcreate --zero
1952 * n" allows to keep the begin of the device unmodified. It's probably better
1953 * to use this heuristic for conflicts between superblocks and partition tables
1954 * than for conflicts between filesystem superblocks -- existence of unwanted
1955 * partition table is very unusual, because PT is pretty visible (parsed and
1956 * interpreted by kernel).
1958 * Note that we usually expect only one signature on the device, it means that
1959 * we have to remember only one wiped area from previously successfully
1960 * detected signature.
1962 * blkid_probe_set_wiper() -- defines wiped area (e.g. LVM)
1963 * blkid_probe_use_wiper() -- try to use area (e.g. MBR)
1965 * Note that there is not relation between _wiper and blkid_to_wipe().
1968 void blkid_probe_set_wiper(blkid_probe pr
, uint64_t off
, uint64_t size
)
1970 struct blkid_chain
*chn
;
1973 DBG(LOWPROBE
, ul_debug("zeroize wiper"));
1974 pr
->wipe_size
= pr
->wipe_off
= 0;
1975 pr
->wipe_chain
= NULL
;
1979 chn
= pr
->cur_chain
;
1981 if (!chn
|| !chn
->driver
||
1982 chn
->idx
< 0 || (size_t) chn
->idx
>= chn
->driver
->nidinfos
)
1985 pr
->wipe_size
= size
;
1987 pr
->wipe_chain
= chn
;
1990 ul_debug("wiper set to %s::%s off=%"PRIu64
" size=%"PRIu64
"",
1992 chn
->driver
->idinfos
[chn
->idx
]->name
,
1993 pr
->wipe_off
, pr
->wipe_size
));
1998 * Returns 1 if the <@off,@size> area was wiped
2000 int blkid_probe_is_wiped(blkid_probe pr
, struct blkid_chain
**chn
, uint64_t off
, uint64_t size
)
2005 if (pr
->wipe_off
<= off
&& off
+ size
<= pr
->wipe_off
+ pr
->wipe_size
) {
2006 *chn
= pr
->wipe_chain
;
2013 * Try to use any area -- if the area has been previously wiped then the
2014 * previous probing result should be ignored (reset).
2016 void blkid_probe_use_wiper(blkid_probe pr
, uint64_t off
, uint64_t size
)
2018 struct blkid_chain
*chn
= NULL
;
2020 if (blkid_probe_is_wiped(pr
, &chn
, off
, size
) && chn
) {
2021 DBG(LOWPROBE
, ul_debug("previously wiped area modified "
2022 " -- ignore previous results"));
2023 blkid_probe_set_wiper(pr
, 0, 0);
2024 blkid_probe_chain_reset_values(pr
, chn
);