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>
115 #include "strutils.h"
119 extern const struct blkid_chaindrv superblocks_drv
;
120 extern const struct blkid_chaindrv topology_drv
;
121 extern const struct blkid_chaindrv partitions_drv
;
124 * All supported chains
126 static const struct blkid_chaindrv
*chains_drvs
[] = {
127 [BLKID_CHAIN_SUBLKS
] = &superblocks_drv
,
128 [BLKID_CHAIN_TOPLGY
] = &topology_drv
,
129 [BLKID_CHAIN_PARTS
] = &partitions_drv
132 static struct blkid_prval
*blkid_probe_new_value(void);
133 static void blkid_probe_reset_values(blkid_probe pr
);
134 static void blkid_probe_reset_buffer(blkid_probe pr
);
139 * Returns: a pointer to the newly allocated probe struct or NULL in case of error.
141 blkid_probe
blkid_new_probe(void)
147 pr
= calloc(1, sizeof(struct blkid_struct_probe
));
151 DBG(LOWPROBE
, ul_debug("allocate a new probe %p", pr
));
153 /* initialize chains */
154 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
155 pr
->chains
[i
].driver
= chains_drvs
[i
];
156 pr
->chains
[i
].flags
= chains_drvs
[i
]->dflt_flags
;
157 pr
->chains
[i
].enabled
= chains_drvs
[i
]->dflt_enabled
;
159 INIT_LIST_HEAD(&pr
->buffers
);
160 INIT_LIST_HEAD(&pr
->values
);
165 * Clone @parent, the new clone shares all, but except:
168 * - buffers if another device (or offset) is set to the prober
170 blkid_probe
blkid_clone_probe(blkid_probe parent
)
177 DBG(LOWPROBE
, ul_debug("allocate a probe clone"));
179 pr
= blkid_new_probe();
184 pr
->off
= parent
->off
;
185 pr
->size
= parent
->size
;
186 pr
->devno
= parent
->devno
;
187 pr
->disk_devno
= parent
->disk_devno
;
188 pr
->blkssz
= parent
->blkssz
;
189 pr
->flags
= parent
->flags
;
192 pr
->flags
&= ~BLKID_FL_PRIVATE_FD
;
200 * blkid_new_probe_from_filename:
201 * @filename: device or regular file
203 * This function is same as call open(filename), blkid_new_probe() and
204 * blkid_probe_set_device(pr, fd, 0, 0).
206 * The @filename is closed by blkid_free_probe() or by the
207 * blkid_probe_set_device() call.
209 * Returns: a pointer to the newly allocated probe struct or NULL in case of
212 blkid_probe
blkid_new_probe_from_filename(const char *filename
)
215 blkid_probe pr
= NULL
;
220 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
);
224 pr
= blkid_new_probe();
228 if (blkid_probe_set_device(pr
, fd
, 0, 0))
231 pr
->flags
|= BLKID_FL_PRIVATE_FD
;
236 blkid_free_probe(pr
);
244 * Deallocates the probe struct, buffers and all allocated
245 * data that are associated with this probing control struct.
247 void blkid_free_probe(blkid_probe pr
)
254 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
255 struct blkid_chain
*ch
= &pr
->chains
[i
];
257 if (ch
->driver
->free_data
)
258 ch
->driver
->free_data(pr
, ch
->data
);
262 if ((pr
->flags
& BLKID_FL_PRIVATE_FD
) && pr
->fd
>= 0)
264 blkid_probe_reset_buffer(pr
);
265 blkid_probe_reset_values(pr
);
266 blkid_free_probe(pr
->disk_probe
);
268 DBG(LOWPROBE
, ul_debug("free probe %p", pr
));
272 void blkid_probe_free_value(struct blkid_prval
*v
)
277 list_del(&v
->prvals
);
280 DBG(LOWPROBE
, ul_debug(" free value %s", v
->name
));
285 * Removes chain values from probing result.
287 void blkid_probe_chain_reset_values(blkid_probe pr
, struct blkid_chain
*chn
)
290 struct list_head
*p
, *pnext
;
292 if (!pr
|| list_empty(&pr
->values
))
295 DBG(LOWPROBE
, ul_debug("Resetting %s values", chn
->driver
->name
));
297 list_for_each_safe(p
, pnext
, &pr
->values
) {
298 struct blkid_prval
*v
= list_entry(p
,
299 struct blkid_prval
, prvals
);
302 blkid_probe_free_value(v
);
306 static void blkid_probe_chain_reset_position(struct blkid_chain
*chn
)
313 static struct blkid_prval *blkid_probe_copy_value(struct blkid_prval *src)
315 struct blkid_prval *dest = blkid_probe_new_value();
320 memcpy(dest, src, sizeof(struct blkid_prval));
322 dest->data = malloc(src->len);
326 memcpy(dest->data, src->data, src->len);
328 INIT_LIST_HEAD(&dest->prvals);
334 * Move chain values from probing result to @vals
336 int blkid_probe_chain_save_values(blkid_probe pr
, struct blkid_chain
*chn
,
337 struct list_head
*vals
)
339 struct list_head
*p
, *pnext
;
340 struct blkid_prval
*v
;
342 DBG(LOWPROBE
, ul_debug("saving %s values", chn
->driver
->name
));
344 list_for_each_safe(p
, pnext
, &pr
->values
) {
346 v
= list_entry(p
, struct blkid_prval
, prvals
);
350 list_del(&v
->prvals
);
351 INIT_LIST_HEAD(&v
->prvals
);
353 list_add_tail(&v
->prvals
, vals
);
359 * Appends values from @vals to the probing result
361 void blkid_probe_append_values_list(blkid_probe pr
, struct list_head
*vals
)
363 DBG(LOWPROBE
, ul_debug("appending values"));
365 list_splice(vals
, &pr
->values
);
366 INIT_LIST_HEAD(vals
);
370 void blkid_probe_free_values_list(struct list_head
*vals
)
375 DBG(LOWPROBE
, ul_debug("freeing values list"));
377 while (!list_empty(vals
)) {
378 struct blkid_prval
*v
= list_entry(vals
->next
, struct blkid_prval
, prvals
);
379 blkid_probe_free_value(v
);
383 struct blkid_chain
*blkid_probe_get_chain(blkid_probe pr
)
385 return pr
->cur_chain
;
388 static const char *blkid_probe_get_probername(blkid_probe pr
)
390 struct blkid_chain
*chn
= blkid_probe_get_chain(pr
);
392 if (chn
&& chn
->idx
>= 0 && (unsigned)chn
->idx
< chn
->driver
->nidinfos
)
393 return chn
->driver
->idinfos
[chn
->idx
]->name
;
398 void *blkid_probe_get_binary_data(blkid_probe pr
, struct blkid_chain
*chn
)
400 int rc
, org_prob_flags
;
401 struct blkid_chain
*org_chn
;
406 /* save the current setting -- the binary API has to be completely
407 * independent on the current probing status
409 org_chn
= pr
->cur_chain
;
410 org_prob_flags
= pr
->prob_flags
;
415 blkid_probe_chain_reset_position(chn
);
417 rc
= chn
->driver
->probe(pr
, chn
);
420 blkid_probe_chain_reset_position(chn
);
422 /* restore the original setting
424 pr
->cur_chain
= org_chn
;
425 pr
->prob_flags
= org_prob_flags
;
430 DBG(LOWPROBE
, ul_debug("returning %s binary data", chn
->driver
->name
));
439 * Zeroize probing results and resets the current probing (this has impact to
440 * blkid_do_probe() only). This function does not touch probing filters and
441 * keeps assigned device.
443 void blkid_reset_probe(blkid_probe pr
)
450 blkid_probe_reset_values(pr
);
451 blkid_probe_set_wiper(pr
, 0, 0);
453 pr
->cur_chain
= NULL
;
455 for (i
= 0; i
< BLKID_NCHAINS
; i
++)
456 blkid_probe_chain_reset_position(&pr
->chains
[i
]);
460 static int blkid_probe_dump_filter(blkid_probe pr, int chain)
462 struct blkid_chain *chn;
465 if (!pr || chain < 0 || chain >= BLKID_NCHAINS)
468 chn = &pr->chains[chain];
473 for (i = 0; i < chn->driver->nidinfos; i++) {
474 const struct blkid_idinfo *id = chn->driver->idinfos[i];
476 DBG(LOWPROBE, ul_debug("%d: %s: %s",
479 blkid_bmp_get_item(chn->fltr, i)
480 ? "disabled" : "enabled <--"));
487 * Returns properly initialized chain filter
489 unsigned long *blkid_probe_get_filter(blkid_probe pr
, int chain
, int create
)
491 struct blkid_chain
*chn
;
493 if (!pr
|| chain
< 0 || chain
>= BLKID_NCHAINS
)
496 chn
= &pr
->chains
[chain
];
498 /* always when you touch the chain filter all indexes are reset and
499 * probing starts from scratch
501 blkid_probe_chain_reset_position(chn
);
502 pr
->cur_chain
= NULL
;
504 if (!chn
->driver
->has_fltr
|| (!chn
->fltr
&& !create
))
508 chn
->fltr
= calloc(1, blkid_bmp_nbytes(chn
->driver
->nidinfos
));
510 memset(chn
->fltr
, 0, blkid_bmp_nbytes(chn
->driver
->nidinfos
));
512 /* blkid_probe_dump_filter(pr, chain); */
517 * Generic private functions for filter setting
519 int __blkid_probe_invert_filter(blkid_probe pr
, int chain
)
522 struct blkid_chain
*chn
;
527 chn
= &pr
->chains
[chain
];
529 if (!chn
->driver
->has_fltr
|| !chn
->fltr
)
532 for (i
= 0; i
< blkid_bmp_nwords(chn
->driver
->nidinfos
); i
++)
533 chn
->fltr
[i
] = ~chn
->fltr
[i
];
535 DBG(LOWPROBE
, ul_debug("probing filter inverted"));
536 /* blkid_probe_dump_filter(pr, chain); */
540 int __blkid_probe_reset_filter(blkid_probe pr
, int chain
)
542 return blkid_probe_get_filter(pr
, chain
, FALSE
) ? 0 : -1;
545 int __blkid_probe_filter_types(blkid_probe pr
, int chain
, int flag
, char *names
[])
548 struct blkid_chain
*chn
;
551 fltr
= blkid_probe_get_filter(pr
, chain
, TRUE
);
555 chn
= &pr
->chains
[chain
];
557 for (i
= 0; i
< chn
->driver
->nidinfos
; i
++) {
559 const struct blkid_idinfo
*id
= chn
->driver
->idinfos
[i
];
562 for (n
= names
; *n
; n
++) {
563 if (!strcmp(id
->name
, *n
)) {
568 if (flag
& BLKID_FLTR_ONLYIN
) {
570 blkid_bmp_set_item(fltr
, i
);
571 } else if (flag
& BLKID_FLTR_NOTIN
) {
573 blkid_bmp_set_item(fltr
, i
);
577 DBG(LOWPROBE
, ul_debug("%s: a new probing type-filter initialized",
579 /* blkid_probe_dump_filter(pr, chain); */
583 static struct blkid_bufinfo
*read_buffer(blkid_probe pr
, uint64_t real_off
, uint64_t len
)
586 struct blkid_bufinfo
*bf
= NULL
;
588 if (blkid_llseek(pr
->fd
, real_off
, SEEK_SET
) < 0) {
593 /* someone trying to overflow some buffers? */
594 if (len
> ULONG_MAX
- sizeof(struct blkid_bufinfo
)) {
599 /* allocate info and space for data by one malloc call */
600 bf
= calloc(1, sizeof(struct blkid_bufinfo
) + len
);
606 bf
->data
= ((unsigned char *) bf
) + sizeof(struct blkid_bufinfo
);
609 INIT_LIST_HEAD(&bf
->bufs
);
611 DBG(LOWPROBE
, ul_debug("\tread %p: off=%"PRIu64
" len=%"PRIu64
"",
612 bf
->data
, real_off
, len
));
614 ret
= read(pr
->fd
, bf
->data
, len
);
615 if (ret
!= (ssize_t
) len
) {
616 DBG(LOWPROBE
, ul_debug("\tread failed: %m"));
619 /* I/O errors on CDROMs are non-fatal to work with hybrid
620 * audio+data disks */
621 if (ret
>= 0 || blkid_probe_is_cdrom(pr
))
630 * Note that @off is offset within probing area, the probing area is defined by
631 * pr->off and pr->size.
633 unsigned char *blkid_probe_get_buffer(blkid_probe pr
, uint64_t off
, uint64_t len
)
636 struct blkid_bufinfo
*bf
= NULL
;
637 uint64_t real_off
= pr
->off
+ off
;
640 DBG(BUFFER, ul_debug("\t>>>> off=%ju, real-off=%ju (probe <%ju..%ju>, len=%ju",
641 off, real_off, pr->off, pr->off + pr->size, len));
649 if (len
== 0 || pr
->off
+ pr
->size
< real_off
+ len
) {
650 DBG(BUFFER
, ul_debug("\t ignore: request out of probing area"));
656 pr
->parent
->devno
== pr
->devno
&&
657 pr
->parent
->off
<= pr
->off
&&
658 pr
->parent
->off
+ pr
->parent
->size
>= pr
->off
+ pr
->size
) {
660 * This is a cloned prober and points to the same area as
661 * parent. Let's use parent's buffers.
663 * Note that pr->off (and pr->parent->off) is always from the
664 * begin of the device.
666 return blkid_probe_get_buffer(pr
->parent
,
667 pr
->off
+ off
- pr
->parent
->off
, len
);
670 /* try buffers we already have in memory */
671 list_for_each(p
, &pr
->buffers
) {
672 struct blkid_bufinfo
*x
=
673 list_entry(p
, struct blkid_bufinfo
, bufs
);
675 if (real_off
>= x
->off
&& real_off
+ len
<= x
->off
+ x
->len
) {
676 DBG(BUFFER
, ul_debug("\treuse %p: off=%"PRIu64
" len=%"PRIu64
" (for off=%"PRIu64
" len=%"PRIu64
")",
677 x
->data
, x
->off
, x
->len
, real_off
, len
));
683 /* not found; read from disk */
685 bf
= read_buffer(pr
, real_off
, len
);
689 list_add_tail(&bf
->bufs
, &pr
->buffers
);
692 assert(bf
->off
<= real_off
);
693 assert(bf
->off
+ bf
->len
>= real_off
+ len
);
696 return real_off
? bf
->data
+ (real_off
- bf
->off
) : bf
->data
;
699 static void blkid_probe_reset_buffer(blkid_probe pr
)
701 uint64_t ct
= 0, len
= 0;
703 if (!pr
|| list_empty(&pr
->buffers
))
706 DBG(BUFFER
, ul_debug("Resetting probing buffers pr=%p", pr
));
708 while (!list_empty(&pr
->buffers
)) {
709 struct blkid_bufinfo
*bf
= list_entry(pr
->buffers
.next
,
710 struct blkid_bufinfo
, bufs
);
715 DBG(BUFFER
, ul_debug(" remove buffer: %p [off=%"PRIu64
", len=%"PRIu64
"]",
716 bf
->data
, bf
->off
, bf
->len
));
720 DBG(LOWPROBE
, ul_debug(" buffers summary: %"PRIu64
" bytes by %"PRIu64
" read() calls",
723 INIT_LIST_HEAD(&pr
->buffers
);
726 static void blkid_probe_reset_values(blkid_probe pr
)
728 if (!pr
|| list_empty(&pr
->values
))
731 DBG(LOWPROBE
, ul_debug("resetting results pr=%p", pr
));
733 while (!list_empty(&pr
->values
)) {
734 struct blkid_prval
*v
= list_entry(pr
->values
.next
,
735 struct blkid_prval
, prvals
);
736 blkid_probe_free_value(v
);
739 INIT_LIST_HEAD(&pr
->values
);
743 * Small devices need a special care.
745 int blkid_probe_is_tiny(blkid_probe pr
)
747 return pr
&& (pr
->flags
& BLKID_FL_TINY_DEV
);
751 * CDROMs may fail when probed for RAID (last sector problem)
753 int blkid_probe_is_cdrom(blkid_probe pr
)
755 return pr
&& (pr
->flags
& BLKID_FL_CDROM_DEV
);
758 static int is_sector_readable(int fd
, uint64_t sector
)
763 if (blkid_llseek(fd
, sector
* 512, SEEK_SET
) < 0)
766 sz
= read(fd
, buf
, sizeof(buf
));
767 if (sz
!= (ssize_t
) sizeof(buf
))
772 DBG(LOWPROBE
, ul_debug("CDROM: read sector %ju failed %m", sector
));
778 * Linux kernel reports (BLKGETSIZE) cdrom device size greater than area
779 * readable by read(2). We have to reduce the probing area to avoid unwanted
780 * I/O errors in probing functions. It seems that unreadable are always last 2
781 * or 3 CD blocks (CD block size is 2048 bytes, it means 12 in 512-byte
784 static void cdrom_size_correction(blkid_probe pr
)
786 uint64_t n
, nsectors
= pr
->size
>> 9;
788 for (n
= nsectors
- 12; n
< nsectors
; n
++) {
789 if (!is_sector_readable(pr
->fd
, n
))
793 DBG(LOWPROBE
, ul_debug("CDROM: full size available"));
796 /* 'n' is the failed sector, reduce device size to n-1; */
797 DBG(LOWPROBE
, ul_debug("CDROM: reduce size from %ju to %ju.",
798 (uintmax_t) pr
->size
,
799 (uintmax_t) n
<< 9));
804 * blkid_probe_set_device:
806 * @fd: device file descriptor
807 * @off: begin of probing area
808 * @size: size of probing area (zero means whole device/file)
810 * Assigns the device to probe control struct, resets internal buffers and
811 * resets the current probing.
813 * Returns: -1 in case of failure, or 0 on success.
815 int blkid_probe_set_device(blkid_probe pr
, int fd
,
816 blkid_loff_t off
, blkid_loff_t size
)
824 blkid_reset_probe(pr
);
825 blkid_probe_reset_buffer(pr
);
827 if ((pr
->flags
& BLKID_FL_PRIVATE_FD
) && pr
->fd
>= 0)
830 pr
->flags
&= ~BLKID_FL_PRIVATE_FD
;
831 pr
->flags
&= ~BLKID_FL_TINY_DEV
;
832 pr
->flags
&= ~BLKID_FL_CDROM_DEV
;
835 pr
->off
= (uint64_t) off
;
843 pr
->wipe_chain
= NULL
;
845 #if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE)
846 /* Disable read-ahead */
847 posix_fadvise(fd
, 0, 0, POSIX_FADV_RANDOM
);
852 if (!S_ISBLK(sb
.st_mode
) && !S_ISCHR(sb
.st_mode
) && !S_ISREG(sb
.st_mode
)) {
857 pr
->mode
= sb
.st_mode
;
858 if (S_ISBLK(sb
.st_mode
) || S_ISCHR(sb
.st_mode
))
859 pr
->devno
= sb
.st_rdev
;
861 if (S_ISBLK(sb
.st_mode
)) {
862 if (blkdev_get_size(fd
, (unsigned long long *) &devsiz
)) {
863 DBG(LOWPROBE
, ul_debug("failed to get device size"));
866 } else if (S_ISCHR(sb
.st_mode
))
867 devsiz
= 1; /* UBI devices are char... */
868 else if (S_ISREG(sb
.st_mode
))
869 devsiz
= sb
.st_size
; /* regular file */
871 pr
->size
= size
? (uint64_t)size
: devsiz
;
873 if (off
&& size
== 0)
874 /* only offset without size specified */
875 pr
->size
-= (uint64_t) off
;
877 if (pr
->off
+ pr
->size
> devsiz
) {
878 DBG(LOWPROBE
, ul_debug("area specified by offset and size is bigger than device"));
883 if (pr
->size
<= 1440 * 1024 && !S_ISCHR(sb
.st_mode
))
884 pr
->flags
|= BLKID_FL_TINY_DEV
;
886 if (S_ISBLK(sb
.st_mode
) && sysfs_devno_is_lvm_private(sb
.st_rdev
)) {
887 DBG(LOWPROBE
, ul_debug("ignore private LVM device"));
888 pr
->flags
|= BLKID_FL_NOSCAN_DEV
;
891 #ifdef CDROM_GET_CAPABILITY
892 else if (S_ISBLK(sb
.st_mode
) &&
893 !blkid_probe_is_tiny(pr
) &&
894 blkid_probe_is_wholedisk(pr
) &&
895 ioctl(fd
, CDROM_GET_CAPABILITY
, NULL
) >= 0) {
897 pr
->flags
|= BLKID_FL_CDROM_DEV
;
898 cdrom_size_correction(pr
);
902 DBG(LOWPROBE
, ul_debug("ready for low-probing, offset=%"PRIu64
", size=%"PRIu64
"",
904 DBG(LOWPROBE
, ul_debug("whole-disk: %s, regfile: %s",
905 blkid_probe_is_wholedisk(pr
) ?"YES" : "NO",
906 S_ISREG(pr
->mode
) ? "YES" : "NO"));
910 DBG(LOWPROBE
, ul_debug("failed to prepare a device for low-probing"));
915 int blkid_probe_get_dimension(blkid_probe pr
, uint64_t *off
, uint64_t *size
)
925 int blkid_probe_set_dimension(blkid_probe pr
, uint64_t off
, uint64_t size
)
930 DBG(LOWPROBE
, ul_debug(
931 "changing probing area pr=%p: size=%"PRIu64
", off=%"PRIu64
" "
932 "-to-> size=%"PRIu64
", off=%"PRIu64
"",
933 pr
, pr
->size
, pr
->off
, size
, off
));
937 pr
->flags
&= ~BLKID_FL_TINY_DEV
;
939 if (pr
->size
<= 1440ULL * 1024ULL && !S_ISCHR(pr
->mode
))
940 pr
->flags
|= BLKID_FL_TINY_DEV
;
942 blkid_probe_reset_buffer(pr
);
948 * Check for matching magic value.
949 * Returns BLKID_PROBE_OK if found, BLKID_PROBE_NONE if not found
950 * or no magic present, or negative value on error.
952 int blkid_probe_get_idmag(blkid_probe pr
, const struct blkid_idinfo
*id
,
953 uint64_t *offset
, const struct blkid_idmag
**res
)
955 const struct blkid_idmag
*mag
= NULL
;
959 mag
= &id
->magics
[0];
963 /* try to detect by magic string */
964 while(mag
&& mag
->magic
) {
967 off
= (mag
->kboff
+ (mag
->sboff
>> 10)) << 10;
968 buf
= blkid_probe_get_buffer(pr
, off
, 1024);
973 if (buf
&& !memcmp(mag
->magic
,
974 buf
+ (mag
->sboff
& 0x3ff), mag
->len
)) {
976 DBG(LOWPROBE
, ul_debug("\tmagic sboff=%u, kboff=%ld",
977 mag
->sboff
, mag
->kboff
));
979 *offset
= off
+ (mag
->sboff
& 0x3ff);
982 return BLKID_PROBE_OK
;
987 if (id
&& id
->magics
[0].magic
)
988 /* magic string(s) defined, but not found */
989 return BLKID_PROBE_NONE
;
991 return BLKID_PROBE_OK
;
994 static inline void blkid_probe_start(blkid_probe pr
)
997 DBG(LOWPROBE
, ul_debug("%p: start probe", pr
));
998 pr
->cur_chain
= NULL
;
1000 blkid_probe_set_wiper(pr
, 0, 0);
1004 static inline void blkid_probe_end(blkid_probe pr
)
1007 DBG(LOWPROBE
, ul_debug("%p: end probe", pr
));
1008 pr
->cur_chain
= NULL
;
1010 blkid_probe_set_wiper(pr
, 0, 0);
1018 * Calls probing functions in all enabled chains. The superblocks chain is
1019 * enabled by default. The blkid_do_probe() stores result from only one
1020 * probing function. It's necessary to call this routine in a loop to get
1021 * results from all probing functions in all chains. The probing is reset
1022 * by blkid_reset_probe() or by filter functions.
1024 * This is string-based NAME=value interface only.
1027 * <title>basic case - use the first result only</title>
1029 * if (blkid_do_probe(pr) == 0) {
1030 * int nvals = blkid_probe_numof_values(pr);
1031 * for (n = 0; n < nvals; n++) {
1032 * if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0)
1033 * printf("%s = %s\n", name, data);
1040 * <title>advanced case - probe for all signatures</title>
1042 * while (blkid_do_probe(pr) == 0) {
1043 * int nvals = blkid_probe_numof_values(pr);
1049 * See also blkid_reset_probe().
1051 * Returns: 0 on success, 1 when probing is done and -1 in case of error.
1053 int blkid_do_probe(blkid_probe pr
)
1060 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1064 struct blkid_chain
*chn
= pr
->cur_chain
;
1067 blkid_probe_start(pr
);
1068 chn
= pr
->cur_chain
= &pr
->chains
[0];
1070 /* we go to the next chain only when the previous probing
1071 * result was nothing (rc == 1) and when the current chain is
1072 * disabled or we are at end of the current chain (chain->idx +
1073 * 1 == sizeof chain) or the current chain bailed out right at
1074 * the start (chain->idx == -1)
1076 else if (rc
== 1 && (chn
->enabled
== FALSE
||
1077 chn
->idx
+ 1 == (int) chn
->driver
->nidinfos
||
1080 size_t idx
= chn
->driver
->id
+ 1;
1082 if (idx
< BLKID_NCHAINS
)
1083 chn
= pr
->cur_chain
= &pr
->chains
[idx
];
1085 blkid_probe_end(pr
);
1086 return 1; /* all chains already probed */
1090 chn
->binary
= FALSE
; /* for sure... */
1092 DBG(LOWPROBE
, ul_debug("chain probe %s %s (idx=%d)",
1094 chn
->enabled
? "ENABLED" : "DISABLED",
1100 /* rc: -1 = error, 0 = success, 1 = no result */
1101 rc
= chn
->driver
->probe(pr
, chn
);
1111 * @dryrun: if TRUE then don't touch the device.
1113 * This function erases the current signature detected by @pr. The @pr has to
1114 * be open in O_RDWR mode, BLKID_SUBLKS_MAGIC or/and BLKID_PARTS_MAGIC flags
1115 * has to be enabled (if you want to erase also superblock with broken check
1116 * sums then use BLKID_SUBLKS_BADCSUM too).
1118 * After successful signature removing the @pr prober will be moved one step
1119 * back and the next blkid_do_probe() call will again call previously called
1123 * <title>wipe all filesystems or raids from the device</title>
1125 * fd = open(devname, O_RDWR|O_CLOEXEC);
1126 * blkid_probe_set_device(pr, fd, 0, 0);
1128 * blkid_probe_enable_superblocks(pr, 1);
1129 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1131 * while (blkid_do_probe(pr) == 0)
1132 * blkid_do_wipe(pr, FALSE);
1136 * See also blkid_probe_step_back() if you cannot use this build-in wipe
1137 * function, but you want to use libblkid probing as a source for wiping.
1139 * Returns: 0 on success, and -1 in case of error.
1141 int blkid_do_wipe(blkid_probe pr
, int dryrun
)
1143 const char *off
= NULL
;
1148 struct blkid_chain
*chn
;
1153 chn
= pr
->cur_chain
;
1157 switch (chn
->driver
->id
) {
1158 case BLKID_CHAIN_SUBLKS
:
1159 rc
= blkid_probe_lookup_value(pr
, "SBMAGIC_OFFSET", &off
, NULL
);
1161 rc
= blkid_probe_lookup_value(pr
, "SBMAGIC", NULL
, &len
);
1163 case BLKID_CHAIN_PARTS
:
1164 rc
= blkid_probe_lookup_value(pr
, "PTMAGIC_OFFSET", &off
, NULL
);
1166 rc
= blkid_probe_lookup_value(pr
, "PTMAGIC", NULL
, &len
);
1172 if (rc
|| len
== 0 || off
== NULL
)
1175 offset
= strtoumax(off
, NULL
, 10) + pr
->off
;
1176 fd
= blkid_probe_get_fd(pr
);
1180 if (len
> sizeof(buf
))
1183 DBG(LOWPROBE
, ul_debug(
1184 "do_wipe [offset=0x%"PRIx64
" (%"PRIu64
"), len=%zu, chain=%s, idx=%d, dryrun=%s]\n",
1185 offset
, offset
, len
, chn
->driver
->name
, chn
->idx
, dryrun
? "yes" : "not"));
1187 l
= blkid_llseek(fd
, offset
, SEEK_SET
);
1188 if ((blkid_loff_t
)l
== (off_t
) -1)
1191 memset(buf
, 0, len
);
1193 if (!dryrun
&& len
) {
1194 if (write_all(fd
, buf
, len
))
1197 return blkid_probe_step_back(pr
);
1204 * blkid_probe_step_back:
1207 * This function move pointer to the probing chain one step back -- it means
1208 * that the previously used probing function will be called again in the next
1209 * blkid_do_probe() call.
1211 * This is necessary for example if you erase or modify on-disk superblock
1212 * according to the current libblkid probing result.
1215 * <title>wipe all superblock, but use libblkid only for probing</title>
1217 * pr = blkid_new_probe_from_filename(devname);
1219 * blkid_probe_enable_superblocks(pr, 1);
1220 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1222 * blkid_probe_enable_partitions(pr, 1);
1223 * blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC);
1225 * while (blkid_do_probe(pr) == 0) {
1226 * const char *ostr = NULL;
1230 * if (blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &ostr, NULL) == 0)
1231 * blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
1233 * // partition tables
1234 * if (len == 0 && blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &ostr, NULL) == 0)
1235 * blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
1240 * // convert ostr to the real offset by off = strtoll(ostr, NULL, 10);
1241 * // use your stuff to erase @len bytes at the @off
1244 * // retry the last probing to check for backup superblocks ..etc.
1245 * blkid_probe_step_back(pr);
1250 * Returns: 0 on success, and -1 in case of error.
1252 int blkid_probe_step_back(blkid_probe pr
)
1254 struct blkid_chain
*chn
;
1259 chn
= pr
->cur_chain
;
1263 blkid_probe_reset_buffer(pr
);
1265 if (chn
->idx
>= 0) {
1267 DBG(LOWPROBE
, ul_debug("step back: moving %s chain index to %d",
1272 if (chn
->idx
== -1) {
1273 /* blkid_do_probe() goes to the next chain if the index
1274 * of the current chain is -1, so we have to set the
1275 * chain pointer to the previous chain.
1277 size_t idx
= chn
->driver
->id
> 0 ? chn
->driver
->id
- 1 : 0;
1279 DBG(LOWPROBE
, ul_debug("step back: moving to previous chain"));
1282 pr
->cur_chain
= &pr
->chains
[idx
];
1284 pr
->cur_chain
= NULL
;
1291 * blkid_do_safeprobe:
1294 * This function gathers probing results from all enabled chains and checks
1295 * for ambivalent results (e.g. more filesystems on the device).
1297 * This is string-based NAME=value interface only.
1299 * Note about superblocks chain -- the function does not check for filesystems
1300 * when a RAID signature is detected. The function also does not check for
1301 * collision between RAIDs. The first detected RAID is returned. The function
1302 * checks for collision between partition table and RAID signature -- it's
1303 * recommended to enable partitions chain together with superblocks chain.
1305 * Returns: 0 on success, 1 if nothing is detected, -2 if ambivalent result is
1306 * detected and -1 on case of error.
1308 int blkid_do_safeprobe(blkid_probe pr
)
1310 int i
, count
= 0, rc
= 0;
1314 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1317 blkid_probe_start(pr
);
1319 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
1320 struct blkid_chain
*chn
;
1322 chn
= pr
->cur_chain
= &pr
->chains
[i
];
1323 chn
->binary
= FALSE
; /* for sure... */
1325 DBG(LOWPROBE
, ul_debug("chain safeprobe %s %s",
1327 chn
->enabled
? "ENABLED" : "DISABLED"));
1332 blkid_probe_chain_reset_position(chn
);
1334 rc
= chn
->driver
->safeprobe(pr
, chn
);
1336 blkid_probe_chain_reset_position(chn
);
1338 /* rc: -2 ambivalent, -1 = error, 0 = success, 1 = no result */
1340 goto done
; /* error */
1342 count
++; /* success */
1346 blkid_probe_end(pr
);
1349 return count
? 0 : 1;
1353 * blkid_do_fullprobe:
1356 * This function gathers probing results from all enabled chains. Same as
1357 * blkid_do_safeprobe() but does not check for collision between probing
1360 * This is string-based NAME=value interface only.
1362 * Returns: 0 on success, 1 if nothing is detected or -1 on case of error.
1364 int blkid_do_fullprobe(blkid_probe pr
)
1366 int i
, count
= 0, rc
= 0;
1370 if (pr
->flags
& BLKID_FL_NOSCAN_DEV
)
1373 blkid_probe_start(pr
);
1375 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
1376 struct blkid_chain
*chn
;
1378 chn
= pr
->cur_chain
= &pr
->chains
[i
];
1379 chn
->binary
= FALSE
; /* for sure... */
1381 DBG(LOWPROBE
, ul_debug("chain fullprobe %s: %s",
1383 chn
->enabled
? "ENABLED" : "DISABLED"));
1388 blkid_probe_chain_reset_position(chn
);
1390 rc
= chn
->driver
->probe(pr
, chn
);
1392 blkid_probe_chain_reset_position(chn
);
1394 /* rc: -1 = error, 0 = success, 1 = no result */
1396 goto done
; /* error */
1398 count
++; /* success */
1402 blkid_probe_end(pr
);
1405 return count
? 0 : 1;
1408 /* same sa blkid_probe_get_buffer() but works with 512-sectors */
1409 unsigned char *blkid_probe_get_sector(blkid_probe pr
, unsigned int sector
)
1411 return pr
? blkid_probe_get_buffer(pr
,
1412 ((uint64_t) sector
) << 9, 0x200) : NULL
;
1415 struct blkid_prval
*blkid_probe_assign_value(
1416 blkid_probe pr
, const char *name
)
1418 struct blkid_prval
*v
;
1422 v
= blkid_probe_new_value();
1427 v
->chain
= pr
->cur_chain
;
1428 list_add_tail(&v
->prvals
, &pr
->values
);
1430 DBG(LOWPROBE
, ul_debug("assigning %s [%s]", name
, v
->chain
->driver
->name
));
1434 static struct blkid_prval
*blkid_probe_new_value(void)
1436 struct blkid_prval
*v
= calloc(1, sizeof(struct blkid_prval
));
1440 INIT_LIST_HEAD(&v
->prvals
);
1445 /* Note that value data is always terminated by zero to keep things robust,
1446 * this extra zero is not count to the value length. It's caller responsibility
1447 * to set proper value length (for strings we count terminator to the length,
1448 * for binary data it's without terminator).
1450 int blkid_probe_value_set_data(struct blkid_prval
*v
,
1451 unsigned char *data
, size_t len
)
1453 v
->data
= calloc(1, len
+ 1); /* always terminate by \0 */
1457 memcpy(v
->data
, data
, len
);
1462 int blkid_probe_set_value(blkid_probe pr
, const char *name
,
1463 unsigned char *data
, size_t len
)
1465 struct blkid_prval
*v
;
1467 v
= blkid_probe_assign_value(pr
, name
);
1471 return blkid_probe_value_set_data(v
, data
, len
);
1474 int blkid_probe_vsprintf_value(blkid_probe pr
, const char *name
,
1475 const char *fmt
, va_list ap
)
1477 struct blkid_prval
*v
;
1480 v
= blkid_probe_assign_value(pr
, name
);
1484 len
= vasprintf((char **) &v
->data
, fmt
, ap
);
1487 blkid_probe_free_value(v
);
1488 return len
== 0 ? -EINVAL
: -ENOMEM
;
1494 int blkid_probe_sprintf_value(blkid_probe pr
, const char *name
,
1495 const char *fmt
, ...)
1501 rc
= blkid_probe_vsprintf_value(pr
, name
, fmt
, ap
);
1507 int blkid_probe_set_magic(blkid_probe pr
, uint64_t offset
,
1508 size_t len
, unsigned char *magic
)
1511 struct blkid_chain
*chn
= blkid_probe_get_chain(pr
);
1513 if (!chn
|| !magic
|| !len
|| chn
->binary
)
1516 switch (chn
->driver
->id
) {
1517 case BLKID_CHAIN_SUBLKS
:
1518 if (!(chn
->flags
& BLKID_SUBLKS_MAGIC
))
1520 rc
= blkid_probe_set_value(pr
, "SBMAGIC", magic
, len
);
1522 rc
= blkid_probe_sprintf_value(pr
,
1523 "SBMAGIC_OFFSET", "%llu", (unsigned long long)offset
);
1525 case BLKID_CHAIN_PARTS
:
1526 if (!(chn
->flags
& BLKID_PARTS_MAGIC
))
1528 rc
= blkid_probe_set_value(pr
, "PTMAGIC", magic
, len
);
1530 rc
= blkid_probe_sprintf_value(pr
,
1531 "PTMAGIC_OFFSET", "%llu", (unsigned long long)offset
);
1540 int blkid_probe_verify_csum(blkid_probe pr
, uint64_t csum
, uint64_t expected
)
1542 if (csum
!= expected
) {
1543 struct blkid_chain
*chn
= blkid_probe_get_chain(pr
);
1545 DBG(LOWPROBE
, ul_debug(
1546 "incorrect checksum for type %s,"
1547 " got %"PRIX64
", expected %"PRIX64
"",
1548 blkid_probe_get_probername(pr
),
1551 * Accept bad checksum if BLKID_SUBLKS_BADCSUM flags is set
1553 if (chn
->driver
->id
== BLKID_CHAIN_SUBLKS
1554 && (chn
->flags
& BLKID_SUBLKS_BADCSUM
)) {
1555 blkid_probe_set_value(pr
, "SBBADCSUM", (unsigned char *) "1", 2);
1558 return 0; /* bad checksum */
1566 * blkid_probe_get_devno:
1569 * Returns: block device number, or 0 for regular files.
1571 dev_t
blkid_probe_get_devno(blkid_probe pr
)
1577 * blkid_probe_get_wholedisk_devno:
1580 * Returns: device number of the wholedisk, or 0 for regular files.
1582 dev_t
blkid_probe_get_wholedisk_devno(blkid_probe pr
)
1584 if (!pr
->disk_devno
) {
1585 dev_t devno
, disk_devno
= 0;
1587 devno
= blkid_probe_get_devno(pr
);
1591 if (blkid_devno_to_wholedisk(devno
, NULL
, 0, &disk_devno
) == 0)
1592 pr
->disk_devno
= disk_devno
;
1594 return pr
->disk_devno
;
1598 * blkid_probe_is_wholedisk:
1601 * Returns: 1 if the device is whole-disk or 0.
1603 int blkid_probe_is_wholedisk(blkid_probe pr
)
1605 dev_t devno
, disk_devno
;
1607 devno
= blkid_probe_get_devno(pr
);
1611 disk_devno
= blkid_probe_get_wholedisk_devno(pr
);
1615 return devno
== disk_devno
;
1618 blkid_probe
blkid_probe_get_wholedisk_probe(blkid_probe pr
)
1622 if (blkid_probe_is_wholedisk(pr
))
1623 return NULL
; /* this is not partition */
1626 /* this is cloned blkid_probe, use parent's stuff */
1627 return blkid_probe_get_wholedisk_probe(pr
->parent
);
1629 disk
= blkid_probe_get_wholedisk_devno(pr
);
1631 if (pr
->disk_probe
&& pr
->disk_probe
->devno
!= disk
) {
1632 /* we have disk prober, but for another disk... close it */
1633 blkid_free_probe(pr
->disk_probe
);
1634 pr
->disk_probe
= NULL
;
1637 if (!pr
->disk_probe
) {
1638 /* Open a new disk prober */
1639 char *disk_path
= blkid_devno_to_devname(disk
);
1644 DBG(LOWPROBE
, ul_debug("allocate a wholedisk probe"));
1646 pr
->disk_probe
= blkid_new_probe_from_filename(disk_path
);
1650 if (!pr
->disk_probe
)
1651 return NULL
; /* ENOMEM? */
1654 return pr
->disk_probe
;
1658 * blkid_probe_get_size:
1661 * This function returns size of probing area as defined by blkid_probe_set_device().
1662 * If the size of the probing area is unrestricted then this function returns
1663 * the real size of device. See also blkid_get_dev_size().
1665 * Returns: size in bytes or -1 in case of error.
1667 blkid_loff_t
blkid_probe_get_size(blkid_probe pr
)
1669 return pr
? (blkid_loff_t
) pr
->size
: -1;
1673 * blkid_probe_get_offset:
1676 * This function returns offset of probing area as defined by blkid_probe_set_device().
1678 * Returns: offset in bytes or -1 in case of error.
1680 blkid_loff_t
blkid_probe_get_offset(blkid_probe pr
)
1682 return pr
? (blkid_loff_t
) pr
->off
: -1;
1686 * blkid_probe_get_fd:
1689 * Returns: file descriptor for assigned device/file or -1 in case of error.
1691 int blkid_probe_get_fd(blkid_probe pr
)
1693 return pr
? pr
->fd
: -1;
1697 * blkid_probe_get_sectorsize:
1698 * @pr: probe or NULL (for NULL returns 512)
1700 * Returns: block device logical sector size (BLKSSZGET ioctl, default 512).
1702 unsigned int blkid_probe_get_sectorsize(blkid_probe pr
)
1705 return DEFAULT_SECTOR_SIZE
; /*... and good luck! */
1710 if (S_ISBLK(pr
->mode
) &&
1711 blkdev_get_sector_size(pr
->fd
, (int *) &pr
->blkssz
) == 0)
1714 pr
->blkssz
= DEFAULT_SECTOR_SIZE
;
1719 * blkid_probe_get_sectors:
1722 * Returns: 512-byte sector count or -1 in case of error.
1724 blkid_loff_t
blkid_probe_get_sectors(blkid_probe pr
)
1726 return pr
? (blkid_loff_t
) (pr
->size
>> 9) : -1;
1730 * blkid_probe_numof_values:
1733 * Returns: number of values in probing result or -1 in case of error.
1735 int blkid_probe_numof_values(blkid_probe pr
)
1738 struct list_head
*p
;
1742 list_for_each(p
, &pr
->values
)
1748 * blkid_probe_get_value:
1750 * @num: wanted value in range 0..N, where N is blkid_probe_numof_values() - 1
1751 * @name: pointer to return value name or NULL
1752 * @data: pointer to return value data or NULL
1753 * @len: pointer to return value length or NULL
1755 * Note, the @len returns length of the @data, including the terminating
1758 * Returns: 0 on success, or -1 in case of error.
1760 int blkid_probe_get_value(blkid_probe pr
, int num
, const char **name
,
1761 const char **data
, size_t *len
)
1763 struct blkid_prval
*v
= __blkid_probe_get_value(pr
, num
);
1770 *data
= (char *) v
->data
;
1774 DBG(LOWPROBE
, ul_debug("returning %s value", v
->name
));
1779 * blkid_probe_lookup_value:
1781 * @name: name of value
1782 * @data: pointer to return value data or NULL
1783 * @len: pointer to return value length or NULL
1785 * Note, the @len returns length of the @data, including the terminating
1788 * Returns: 0 on success, or -1 in case of error.
1790 int blkid_probe_lookup_value(blkid_probe pr
, const char *name
,
1791 const char **data
, size_t *len
)
1793 struct blkid_prval
*v
= __blkid_probe_lookup_value(pr
, name
);
1798 *data
= (char *) v
->data
;
1805 * blkid_probe_has_value:
1807 * @name: name of value
1809 * Returns: 1 if value exist in probing result, otherwise 0.
1811 int blkid_probe_has_value(blkid_probe pr
, const char *name
)
1813 if (blkid_probe_lookup_value(pr
, name
, NULL
, NULL
) == 0)
1818 struct blkid_prval
*blkid_probe_last_value(blkid_probe pr
)
1820 if (!pr
|| list_empty(&pr
->values
))
1823 return list_last_entry(&pr
->values
, struct blkid_prval
, prvals
);
1827 struct blkid_prval
*__blkid_probe_get_value(blkid_probe pr
, int num
)
1830 struct list_head
*p
;
1835 list_for_each(p
, &pr
->values
) {
1838 return list_entry(p
, struct blkid_prval
, prvals
);
1843 struct blkid_prval
*__blkid_probe_lookup_value(blkid_probe pr
, const char *name
)
1845 struct list_head
*p
;
1847 if (!pr
|| list_empty(&pr
->values
) || !name
)
1850 list_for_each(p
, &pr
->values
) {
1851 struct blkid_prval
*v
= list_entry(p
, struct blkid_prval
,
1854 if (v
->name
&& strcmp(name
, v
->name
) == 0) {
1855 DBG(LOWPROBE
, ul_debug("returning %s value", v
->name
));
1863 /* converts DCE UUID (uuid[16]) to human readable string
1864 * - the @len should be always 37 */
1866 void blkid_unparse_uuid(const unsigned char *uuid
, char *str
,
1867 size_t len
__attribute__((__unused__
)))
1869 uuid_unparse(uuid
, str
);
1872 void blkid_unparse_uuid(const unsigned char *uuid
, char *str
, size_t len
)
1875 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1876 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1880 uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14],uuid
[15]);
1884 /* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */
1885 int blkid_uuid_is_empty(const unsigned char *buf
, size_t len
)
1889 for (i
= 0; i
< len
; i
++)
1895 /* Removes whitespace from the right-hand side of a string (trailing
1898 * Returns size of the new string (without \0).
1900 size_t blkid_rtrim_whitespace(unsigned char *str
)
1902 return rtrim_whitespace(str
);
1905 /* Removes whitespace from the left-hand side of a string.
1907 * Returns size of the new string (without \0).
1909 size_t blkid_ltrim_whitespace(unsigned char *str
)
1911 return ltrim_whitespace(str
);
1915 * Some mkfs-like utils wipe some parts (usually begin) of the device.
1916 * For example LVM (pvcreate) or mkswap(8). This information could be used
1917 * for later resolution to conflicts between superblocks.
1919 * For example we found valid LVM superblock, LVM wipes 8KiB at the begin of
1920 * the device. If we found another signature (for example MBR) within the
1921 * wiped area then the signature has been added later and LVM superblock
1924 * Note that this heuristic is not 100% reliable, for example "pvcreate --zero
1925 * n" allows to keep the begin of the device unmodified. It's probably better
1926 * to use this heuristic for conflicts between superblocks and partition tables
1927 * than for conflicts between filesystem superblocks -- existence of unwanted
1928 * partition table is very unusual, because PT is pretty visible (parsed and
1929 * interpreted by kernel).
1931 * Note that we usually expect only one signature on the device, it means that
1932 * we have to remember only one wiped area from previously successfully
1933 * detected signature.
1935 * blkid_probe_set_wiper() -- defines wiped area (e.g. LVM)
1936 * blkid_probe_use_wiper() -- try to use area (e.g. MBR)
1938 * Note that there is not relation between _wiper and blkid_to_wipe().
1941 void blkid_probe_set_wiper(blkid_probe pr
, uint64_t off
, uint64_t size
)
1943 struct blkid_chain
*chn
;
1949 DBG(LOWPROBE
, ul_debug("zeroize wiper"));
1950 pr
->wipe_size
= pr
->wipe_off
= 0;
1951 pr
->wipe_chain
= NULL
;
1955 chn
= pr
->cur_chain
;
1957 if (!chn
|| !chn
->driver
||
1958 chn
->idx
< 0 || (size_t) chn
->idx
>= chn
->driver
->nidinfos
)
1961 pr
->wipe_size
= size
;
1963 pr
->wipe_chain
= chn
;
1966 ul_debug("wiper set to %s::%s off=%"PRIu64
" size=%"PRIu64
"",
1968 chn
->driver
->idinfos
[chn
->idx
]->name
,
1969 pr
->wipe_off
, pr
->wipe_size
));
1974 * Returns 1 if the <@off,@size> area was wiped
1976 int blkid_probe_is_wiped(blkid_probe pr
, struct blkid_chain
**chn
, uint64_t off
, uint64_t size
)
1981 if (pr
->wipe_off
<= off
&& off
+ size
<= pr
->wipe_off
+ pr
->wipe_size
) {
1983 *chn
= pr
->wipe_chain
;
1990 * Try to use any area -- if the area has been previously wiped then the
1991 * previous probing result should be ignored (reset).
1993 void blkid_probe_use_wiper(blkid_probe pr
, uint64_t off
, uint64_t size
)
1995 struct blkid_chain
*chn
= NULL
;
1997 if (blkid_probe_is_wiped(pr
, &chn
, off
, size
) && chn
) {
1998 DBG(LOWPROBE
, ul_debug("previously wiped area modified "
1999 " -- ignore previous results"));
2000 blkid_probe_set_wiper(pr
, 0, 0);
2001 blkid_probe_chain_reset_values(pr
, chn
);