blob: 77e8b860c870eb4c8b33af31d0a12abae55ee76e [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * Low-level libblkid probing API
3 *
4 * Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com>
5 *
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
8 */
9
10/**
11 * SECTION: lowprobe
12 * @title: Low-level probing
13 * @short_description: low-level prober initialization
14 *
15 * The low-level probing routines always and directly read information from
16 * the selected (see blkid_probe_set_device()) device.
17 *
18 * The probing routines are grouped together into separate chains. Currently,
19 * the library provides superblocks, partitions and topology chains.
20 *
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().
27 *
28 * For more details see the chain specific documentation.
29 *
30 * The low-level API provides two ways how access to probing results.
31 *
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.
34 *
35 * 2. The binary interfaces. These interfaces return data in the native formats.
36 * The interface is always specific to the probing chain.
37 *
38 * Note that the previous probing result (binary or NAME=value) is always
39 * zeroized when a chain probing function is called. For example:
40 *
41 * <informalexample>
42 * <programlisting>
43 * blkid_probe_enable_partitions(pr, TRUE);
44 * blkid_probe_enable_superblocks(pr, FALSE);
45 *
46 * blkid_do_safeprobe(pr);
47 * </programlisting>
48 * </informalexample>
49 *
50 * overwrites the previous probing result for the partitions chain, the superblocks
51 * result is not modified.
52 */
53
54/**
55 * SECTION: lowprobe-tags
56 * @title: Low-level tags
57 * @short_description: generic NAME=value interface.
58 *
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.
63 *
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
66 * documentation.
67 *
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:
71 *
72 * <informalexample>
73 * <programlisting>
74 * while((blkid_do_probe(pr) == 0)
75 * ... use result ...
76 * </programlisting>
77 * </informalexample>
78 *
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
82 * device).
83 *
84 * The probing result is set of NAME=value pairs (the NAME is always unique).
85 */
86
87#include <stdio.h>
88#include <string.h>
89#include <stdlib.h>
90#include <unistd.h>
91#include <fcntl.h>
92#include <ctype.h>
93#include <sys/types.h>
94#ifdef HAVE_LINUX_CDROM_H
95#include <linux/cdrom.h>
96#endif
97#ifdef HAVE_SYS_STAT_H
98#include <sys/stat.h>
99#endif
100#ifdef HAVE_ERRNO_H
101#include <errno.h>
102#endif
103#include <inttypes.h>
104#include <stdint.h>
105#include <stdarg.h>
106
107#ifdef HAVE_LIBUUID
108# include <uuid.h>
109#endif
110
111#include "blkidP.h"
112#include "all-io.h"
113
114/* chains */
115extern const struct blkid_chaindrv superblocks_drv;
116extern const struct blkid_chaindrv topology_drv;
117extern const struct blkid_chaindrv partitions_drv;
118
119/*
120 * All supported chains
121 */
122static const struct blkid_chaindrv *chains_drvs[] = {
123 [BLKID_CHAIN_SUBLKS] = &superblocks_drv,
124 [BLKID_CHAIN_TOPLGY] = &topology_drv,
125 [BLKID_CHAIN_PARTS] = &partitions_drv
126};
127
128static void blkid_probe_reset_vals(blkid_probe pr);
129static void blkid_probe_reset_buffer(blkid_probe pr);
130
131/**
132 * blkid_new_probe:
133 *
134 * Returns: a pointer to the newly allocated probe struct or NULL in case of error.
135 */
136blkid_probe blkid_new_probe(void)
137{
138 int i;
139 blkid_probe pr;
140
141 blkid_init_debug(0);
142 pr = calloc(1, sizeof(struct blkid_struct_probe));
143 if (!pr)
144 return NULL;
145
146 DBG(DEBUG_LOWPROBE, printf("allocate a new probe %p\n", pr));
147
148 /* initialize chains */
149 for (i = 0; i < BLKID_NCHAINS; i++) {
150 pr->chains[i].driver = chains_drvs[i];
151 pr->chains[i].flags = chains_drvs[i]->dflt_flags;
152 pr->chains[i].enabled = chains_drvs[i]->dflt_enabled;
153 }
154 INIT_LIST_HEAD(&pr->buffers);
155 return pr;
156}
157
158/*
159 * Clone @parent, the new clone shares all, but except:
160 *
161 * - probing result
162 * - bufferes if another device (or offset) is set to the prober
163 */
164blkid_probe blkid_clone_probe(blkid_probe parent)
165{
166 blkid_probe pr;
167
168 if (!parent)
169 return NULL;
170
171 DBG(DEBUG_LOWPROBE, printf("allocate a probe clone\n"));
172
173 pr = blkid_new_probe();
174 if (!pr)
175 return NULL;
176
177 pr->fd = parent->fd;
178 pr->off = parent->off;
179 pr->size = parent->size;
180 pr->devno = parent->devno;
181 pr->disk_devno = parent->disk_devno;
182 pr->blkssz = parent->blkssz;
183 pr->flags = parent->flags;
184 pr->parent = parent;
185
186 pr->flags &= ~BLKID_FL_PRIVATE_FD;
187
188 return pr;
189}
190
191
192
193/**
194 * blkid_new_probe_from_filename:
195 * @filename: device or regular file
196 *
197 * This function is same as call open(filename), blkid_new_probe() and
198 * blkid_probe_set_device(pr, fd, 0, 0).
199 *
200 * The @filename is closed by blkid_free_probe() or by the
201 * blkid_probe_set_device() call.
202 *
203 * Returns: a pointer to the newly allocated probe struct or NULL in case of
204 * error.
205 */
206blkid_probe blkid_new_probe_from_filename(const char *filename)
207{
208 int fd = -1;
209 blkid_probe pr = NULL;
210
211 if (!filename)
212 return NULL;
213
214 fd = open(filename, O_RDONLY|O_CLOEXEC);
215 if (fd < 0)
216 return NULL;
217
218 pr = blkid_new_probe();
219 if (!pr)
220 goto err;
221
222 if (blkid_probe_set_device(pr, fd, 0, 0))
223 goto err;
224
225 pr->flags |= BLKID_FL_PRIVATE_FD;
226 return pr;
227err:
228 if (fd >= 0)
229 close(fd);
230 blkid_free_probe(pr);
231 return NULL;
232}
233
234/**
235 * blkid_free_probe:
236 * @pr: probe
237 *
238 * Deallocates the probe struct, buffers and all allocated
239 * data that are associated with this probing control struct.
240 */
241void blkid_free_probe(blkid_probe pr)
242{
243 int i;
244
245 if (!pr)
246 return;
247
248 for (i = 0; i < BLKID_NCHAINS; i++) {
249 struct blkid_chain *ch = &pr->chains[i];
250
251 if (ch->driver->free_data)
252 ch->driver->free_data(pr, ch->data);
253 free(ch->fltr);
254 }
255
256 if ((pr->flags & BLKID_FL_PRIVATE_FD) && pr->fd >= 0)
257 close(pr->fd);
258 blkid_probe_reset_buffer(pr);
259 blkid_free_probe(pr->disk_probe);
260
261 DBG(DEBUG_LOWPROBE, printf("free probe %p\n", pr));
262 free(pr);
263}
264
265
266/*
267 * Removes chain values from probing result.
268 */
269void blkid_probe_chain_reset_vals(blkid_probe pr, struct blkid_chain *chn)
270{
271 int nvals = pr->nvals;
272 int i, x;
273
274 for (x = 0, i = 0; i < pr->nvals; i++) {
275 struct blkid_prval *v = &pr->vals[i];
276
277 if (v->chain != chn && x == i) {
278 x++;
279 continue;
280 }
281 if (v->chain == chn) {
282 --nvals;
283 continue;
284 }
285 memcpy(&pr->vals[x++], v, sizeof(struct blkid_prval));
286 }
287 pr->nvals = nvals;
288}
289
290static void blkid_probe_chain_reset_position(struct blkid_chain *chn)
291{
292 if (chn)
293 chn->idx = -1;
294}
295
296/*
297 * Copies chain values from probing result to @vals, the max size of @vals is
298 * @nvals and returns real number of values.
299 */
300int blkid_probe_chain_copy_vals(blkid_probe pr, struct blkid_chain *chn,
301 struct blkid_prval *vals, int nvals)
302{
303 int i, x;
304
305 for (x = 0, i = 0; i < pr->nvals && x < nvals; i++) {
306 struct blkid_prval *v = &pr->vals[i];
307
308 if (v->chain != chn)
309 continue;
310 memcpy(&vals[x++], v, sizeof(struct blkid_prval));
311 }
312 return x;
313}
314
315/*
316 * Appends values from @vals to the probing result
317 */
318void blkid_probe_append_vals(blkid_probe pr, struct blkid_prval *vals, int nvals)
319{
320 int i = 0;
321
322 while (i < nvals && pr->nvals < BLKID_NVALS) {
323 memcpy(&pr->vals[pr->nvals++], &vals[i++],
324 sizeof(struct blkid_prval));
325 }
326}
327
328static void blkid_probe_reset_vals(blkid_probe pr)
329{
330 memset(pr->vals, 0, sizeof(pr->vals));
331 pr->nvals = 0;
332}
333
334struct blkid_chain *blkid_probe_get_chain(blkid_probe pr)
335{
336 return pr->cur_chain;
337}
338
339void *blkid_probe_get_binary_data(blkid_probe pr, struct blkid_chain *chn)
340{
341 int rc, org_prob_flags;
342 struct blkid_chain *org_chn;
343
344 if (!pr || !chn)
345 return NULL;
346
347 /* save the current setting -- the binary API has to be completely
348 * independent on the current probing status
349 */
350 org_chn = pr->cur_chain;
351 org_prob_flags = pr->prob_flags;
352
353 pr->cur_chain = chn;
354 pr->prob_flags = 0;
355 chn->binary = TRUE;
356 blkid_probe_chain_reset_position(chn);
357
358 rc = chn->driver->probe(pr, chn);
359
360 chn->binary = FALSE;
361 blkid_probe_chain_reset_position(chn);
362
363 /* restore the original setting
364 */
365 pr->cur_chain = org_chn;
366 pr->prob_flags = org_prob_flags;
367
368 if (rc != 0)
369 return NULL;
370
371 DBG(DEBUG_LOWPROBE,
372 printf("returning %s binary data\n", chn->driver->name));
373 return chn->data;
374}
375
376
377/**
378 * blkid_reset_probe:
379 * @pr: probe
380 *
381 * Zeroize probing results and resets the current probing (this has impact to
382 * blkid_do_probe() only). This function does not touch probing filters and
383 * keeps assigned device.
384 */
385void blkid_reset_probe(blkid_probe pr)
386{
387 int i;
388
389 if (!pr)
390 return;
391
392 blkid_probe_reset_vals(pr);
393 blkid_probe_set_wiper(pr, 0, 0);
394
395 pr->cur_chain = NULL;
396
397 for (i = 0; i < BLKID_NCHAINS; i++)
398 blkid_probe_chain_reset_position(&pr->chains[i]);
399}
400
401/***
402static int blkid_probe_dump_filter(blkid_probe pr, int chain)
403{
404 struct blkid_chain *chn;
405 int i;
406
407 if (!pr || chain < 0 || chain >= BLKID_NCHAINS)
408 return -1;
409
410 chn = &pr->chains[chain];
411
412 if (!chn->fltr)
413 return -1;
414
415 for (i = 0; i < chn->driver->nidinfos; i++) {
416 const struct blkid_idinfo *id = chn->driver->idinfos[i];
417
418 DBG(DEBUG_LOWPROBE, printf("%d: %s: %s\n",
419 i,
420 id->name,
421 blkid_bmp_get_item(chn->fltr, i)
422 ? "disabled" : "enabled <--"));
423 }
424 return 0;
425}
426***/
427
428/*
429 * Returns properly initialized chain filter
430 */
431unsigned long *blkid_probe_get_filter(blkid_probe pr, int chain, int create)
432{
433 struct blkid_chain *chn;
434
435 if (!pr || chain < 0 || chain >= BLKID_NCHAINS)
436 return NULL;
437
438 chn = &pr->chains[chain];
439
440 /* always when you touch the chain filter all indexes are reset and
441 * probing starts from scratch
442 */
443 blkid_probe_chain_reset_position(chn);
444 pr->cur_chain = NULL;
445
446 if (!chn->driver->has_fltr || (!chn->fltr && !create))
447 return NULL;
448
449 if (!chn->fltr)
450 chn->fltr = calloc(1, blkid_bmp_nbytes(chn->driver->nidinfos));
451 else
452 memset(chn->fltr, 0, blkid_bmp_nbytes(chn->driver->nidinfos));
453
454 /* blkid_probe_dump_filter(pr, chain); */
455 return chn->fltr;
456}
457
458/*
459 * Generic private functions for filter setting
460 */
461int __blkid_probe_invert_filter(blkid_probe pr, int chain)
462{
463 size_t i;
464 struct blkid_chain *chn;
465
466 if (!pr)
467 return -1;
468
469 chn = &pr->chains[chain];
470
471 if (!chn->driver->has_fltr || !chn->fltr)
472 return -1;
473
474 for (i = 0; i < blkid_bmp_nwords(chn->driver->nidinfos); i++)
475 chn->fltr[i] = ~chn->fltr[i];
476
477 DBG(DEBUG_LOWPROBE, printf("probing filter inverted\n"));
478 /* blkid_probe_dump_filter(pr, chain); */
479 return 0;
480}
481
482int __blkid_probe_reset_filter(blkid_probe pr, int chain)
483{
484 return blkid_probe_get_filter(pr, chain, FALSE) ? 0 : -1;
485}
486
487int __blkid_probe_filter_types(blkid_probe pr, int chain, int flag, char *names[])
488{
489 unsigned long *fltr;
490 struct blkid_chain *chn;
491 size_t i;
492
493 fltr = blkid_probe_get_filter(pr, chain, TRUE);
494 if (!fltr)
495 return -1;
496
497 chn = &pr->chains[chain];
498
499 for (i = 0; i < chn->driver->nidinfos; i++) {
500 int has = 0;
501 const struct blkid_idinfo *id = chn->driver->idinfos[i];
502 char **n;
503
504 for (n = names; *n; n++) {
505 if (!strcmp(id->name, *n)) {
506 has = 1;
507 break;
508 }
509 }
510 if (flag & BLKID_FLTR_ONLYIN) {
511 if (!has)
512 blkid_bmp_set_item(fltr, i);
513 } else if (flag & BLKID_FLTR_NOTIN) {
514 if (has)
515 blkid_bmp_set_item(fltr, i);
516 }
517 }
518
519 DBG(DEBUG_LOWPROBE,
520 printf("%s: a new probing type-filter initialized\n",
521 chn->driver->name));
522 /* blkid_probe_dump_filter(pr, chain); */
523 return 0;
524}
525
526unsigned char *blkid_probe_get_buffer(blkid_probe pr,
527 blkid_loff_t off, blkid_loff_t len)
528{
529 struct list_head *p;
530 struct blkid_bufinfo *bf = NULL;
531
532 if (pr->size <= 0)
533 return NULL;
534
535 if (pr->parent &&
536 pr->parent->devno == pr->devno &&
537 pr->parent->off <= pr->off &&
538 pr->parent->off + pr->parent->size >= pr->off + pr->size) {
539 /*
540 * This is a cloned prober and points to the same area as
541 * parent. Let's use parent's buffers.
542 *
543 * Note that pr->off (and pr->parent->off) is always from the
544 * beginig of the device.
545 */
546 return blkid_probe_get_buffer(pr->parent,
547 pr->off + off - pr->parent->off, len);
548 }
549
550 list_for_each(p, &pr->buffers) {
551 struct blkid_bufinfo *x =
552 list_entry(p, struct blkid_bufinfo, bufs);
553
554 if (x->off <= off && off + len <= x->off + x->len) {
555 DBG(DEBUG_LOWPROBE,
556 printf("\treuse buffer: off=%jd len=%jd pr=%p\n",
557 x->off, x->len, pr));
558 bf = x;
559 break;
560 }
561 }
562 if (!bf) {
563 ssize_t ret;
564
565 if (blkid_llseek(pr->fd, pr->off + off, SEEK_SET) < 0)
566 return NULL;
567
568 /* allocate info and space for data by why call */
569 bf = calloc(1, sizeof(struct blkid_bufinfo) + len);
570 if (!bf)
571 return NULL;
572
573 bf->data = ((unsigned char *) bf) + sizeof(struct blkid_bufinfo);
574 bf->len = len;
575 bf->off = off;
576 INIT_LIST_HEAD(&bf->bufs);
577
578 DBG(DEBUG_LOWPROBE,
579 printf("\tbuffer read: off=%jd len=%jd pr=%p\n",
580 off, len, pr));
581
582 ret = read(pr->fd, bf->data, len);
583 if (ret != (ssize_t) len) {
584 free(bf);
585 return NULL;
586 }
587 list_add_tail(&bf->bufs, &pr->buffers);
588 }
589
590 return off ? bf->data + (off - bf->off) : bf->data;
591}
592
593
594static void blkid_probe_reset_buffer(blkid_probe pr)
595{
596 uint64_t read_ct = 0, len_ct = 0;
597
598 if (!pr || list_empty(&pr->buffers))
599 return;
600
601 DBG(DEBUG_LOWPROBE, printf("reseting probing buffers pr=%p\n", pr));
602
603 while (!list_empty(&pr->buffers)) {
604 struct blkid_bufinfo *bf = list_entry(pr->buffers.next,
605 struct blkid_bufinfo, bufs);
606 read_ct++;
607 len_ct += bf->len;
608 list_del(&bf->bufs);
609 free(bf);
610 }
611
612 DBG(DEBUG_LOWPROBE,
613 printf("buffers summary: %"PRIu64" bytes "
614 "by %"PRIu64" read() call(s)\n",
615 len_ct, read_ct));
616
617 INIT_LIST_HEAD(&pr->buffers);
618}
619
620/*
621 * Small devices need a special care.
622 */
623int blkid_probe_is_tiny(blkid_probe pr)
624{
625 return pr && (pr->flags & BLKID_FL_TINY_DEV);
626}
627
628/*
629 * CDROMs may fail when probed for RAID (last sector problem)
630 */
631int blkid_probe_is_cdrom(blkid_probe pr)
632{
633 return pr && (pr->flags & BLKID_FL_CDROM_DEV);
634}
635
636/**
637 * blkid_probe_set_device:
638 * @pr: probe
639 * @fd: device file descriptor
640 * @off: begin of probing area
641 * @size: size of probing area (zero means whole device/file)
642 *
643 * Assigns the device to probe control struct, resets internal buffers and
644 * resets the current probing.
645 *
646 * Returns: -1 in case of failure, or 0 on success.
647 */
648int blkid_probe_set_device(blkid_probe pr, int fd,
649 blkid_loff_t off, blkid_loff_t size)
650{
651 struct stat sb;
652
653 if (!pr)
654 return -1;
655
656 blkid_reset_probe(pr);
657 blkid_probe_reset_buffer(pr);
658
659 if ((pr->flags & BLKID_FL_PRIVATE_FD) && pr->fd >= 0)
660 close(pr->fd);
661
662 pr->flags &= ~BLKID_FL_PRIVATE_FD;
663 pr->flags &= ~BLKID_FL_TINY_DEV;
664 pr->flags &= ~BLKID_FL_CDROM_DEV;
665 pr->prob_flags = 0;
666 pr->fd = fd;
667 pr->off = off;
668 pr->size = 0;
669 pr->devno = 0;
670 pr->disk_devno = 0;
671 pr->mode = 0;
672 pr->blkssz = 0;
673 pr->wipe_off = 0;
674 pr->wipe_size = 0;
675 pr->wipe_chain = NULL;
676
677#if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE)
678 /* Disable read-ahead */
679 posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
680#endif
681 if (fstat(fd, &sb))
682 goto err;
683
684 if (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode) && !S_ISREG(sb.st_mode))
685 goto err;
686
687 pr->mode = sb.st_mode;
688 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode))
689 pr->devno = sb.st_rdev;
690
691 if (size)
692 pr->size = size;
693 else {
694 if (S_ISBLK(sb.st_mode)) {
695 if (blkdev_get_size(fd, (unsigned long long *) &pr->size)) {
696 DBG(DEBUG_LOWPROBE, printf(
697 "failed to get device size\n"));
698 goto err;
699 }
700 } else if (S_ISCHR(sb.st_mode))
701 pr->size = 1; /* UBI devices are char... */
702 else if (S_ISREG(sb.st_mode))
703 pr->size = sb.st_size; /* regular file */
704
705 if (pr->off > pr->size)
706 goto err;
707
708 /* The probing area cannot be larger than whole device, pr->off
709 * is offset within the device */
710 pr->size -= pr->off;
711 }
712
713 if (pr->size <= 1440 * 1024 && !S_ISCHR(sb.st_mode))
714 pr->flags |= BLKID_FL_TINY_DEV;
715
716#ifdef CDROM_GET_CAPABILITY
717 if (S_ISBLK(sb.st_mode) &&
718 !blkid_probe_is_tiny(pr) &&
719 blkid_probe_is_wholedisk(pr) &&
720 ioctl(fd, CDROM_GET_CAPABILITY, NULL) >= 0)
721 pr->flags |= BLKID_FL_CDROM_DEV;
722#endif
723
724 DBG(DEBUG_LOWPROBE, printf("ready for low-probing, offset=%jd, size=%jd\n",
725 pr->off, pr->size));
726 DBG(DEBUG_LOWPROBE, printf("whole-disk: %s, regfile: %s\n",
727 blkid_probe_is_wholedisk(pr) ?"YES" : "NO",
728 S_ISREG(pr->mode) ? "YES" : "NO"));
729
730 return 0;
731err:
732 DBG(DEBUG_LOWPROBE,
733 printf("failed to prepare a device for low-probing\n"));
734 return -1;
735
736}
737
738int blkid_probe_get_dimension(blkid_probe pr,
739 blkid_loff_t *off, blkid_loff_t *size)
740{
741 if (!pr)
742 return -1;
743
744 *off = pr->off;
745 *size = pr->size;
746 return 0;
747}
748
749int blkid_probe_set_dimension(blkid_probe pr,
750 blkid_loff_t off, blkid_loff_t size)
751{
752 if (!pr)
753 return -1;
754
755 DBG(DEBUG_LOWPROBE, printf(
756 "changing probing area pr=%p: size=%llu, off=%llu "
757 "-to-> size=%llu, off=%llu\n",
758 pr,
759 (unsigned long long) pr->size,
760 (unsigned long long) pr->off,
761 (unsigned long long) size,
762 (unsigned long long) off));
763
764 pr->off = off;
765 pr->size = size;
766 pr->flags &= ~BLKID_FL_TINY_DEV;
767
768 if (pr->size <= 1440 * 1024 && !S_ISCHR(pr->mode))
769 pr->flags |= BLKID_FL_TINY_DEV;
770
771 blkid_probe_reset_buffer(pr);
772
773 return 0;
774}
775
776int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id,
777 blkid_loff_t *offset, const struct blkid_idmag **res)
778{
779 const struct blkid_idmag *mag = NULL;
780 blkid_loff_t off = 0;
781
782 if (id)
783 mag = &id->magics[0];
784 if (res)
785 *res = NULL;
786
787 /* try to detect by magic string */
788 while(mag && mag->magic) {
789 unsigned char *buf;
790
791 off = (mag->kboff + (mag->sboff >> 10)) << 10;
792 buf = blkid_probe_get_buffer(pr, off, 1024);
793
794 if (buf && !memcmp(mag->magic,
795 buf + (mag->sboff & 0x3ff), mag->len)) {
796 DBG(DEBUG_LOWPROBE, printf(
797 "\tmagic sboff=%u, kboff=%ld\n",
798 mag->sboff, mag->kboff));
799 if (offset)
800 *offset = off + (mag->sboff & 0x3ff);
801 if (res)
802 *res = mag;
803 return 0;
804 }
805 mag++;
806 }
807
808 if (id && id->magics[0].magic)
809 /* magic string(s) defined, but not found */
810 return 1;
811
812 return 0;
813}
814
815static inline void blkid_probe_start(blkid_probe pr)
816{
817 if (pr) {
818 DBG(DEBUG_LOWPROBE, printf("%p: start probe\n", pr));
819 pr->cur_chain = NULL;
820 pr->prob_flags = 0;
821 blkid_probe_set_wiper(pr, 0, 0);
822 }
823}
824
825static inline void blkid_probe_end(blkid_probe pr)
826{
827 if (pr) {
828 DBG(DEBUG_LOWPROBE, printf("%p: end probe\n", pr));
829 pr->cur_chain = NULL;
830 pr->prob_flags = 0;
831 blkid_probe_set_wiper(pr, 0, 0);
832 }
833}
834
835/**
836 * blkid_do_probe:
837 * @pr: prober
838 *
839 * Calls probing functions in all enabled chains. The superblocks chain is
840 * enabled by default. The blkid_do_probe() stores result from only one
841 * probing function. It's necessary to call this routine in a loop to get
842 * results from all probing functions in all chains. The probing is reset
843 * by blkid_reset_probe() or by filter functions.
844 *
845 * This is string-based NAME=value interface only.
846 *
847 * <example>
848 * <title>basic case - use the first result only</title>
849 * <programlisting>
850 *
851 * if (blkid_do_probe(pr) == 0) {
852 * int nvals = blkid_probe_numof_values(pr);
853 * for (n = 0; n < nvals; n++) {
854 * if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0)
855 * printf("%s = %s\n", name, data);
856 * }
857 * }
858 * </programlisting>
859 * </example>
860 *
861 * <example>
862 * <title>advanced case - probe for all signatures</title>
863 * <programlisting>
864 *
865 * while (blkid_do_probe(pr) == 0) {
866 * int nvals = blkid_probe_numof_values(pr);
867 * ...
868 * }
869 * </programlisting>
870 * </example>
871 *
872 * See also blkid_reset_probe().
873 *
874 * Returns: 0 on success, 1 when probing is done and -1 in case of error.
875 */
876int blkid_do_probe(blkid_probe pr)
877{
878 int rc = 1;
879
880 if (!pr)
881 return -1;
882
883 do {
884 struct blkid_chain *chn = pr->cur_chain;
885
886 if (!chn) {
887 blkid_probe_start(pr);
888 chn = pr->cur_chain = &pr->chains[0];
889 }
890 /* we go to the next chain only when the previous probing
891 * result was nothing (rc == 1) and when the current chain is
892 * disabled or we are at end of the current chain (chain->idx +
893 * 1 == sizeof chain) or the current chain bailed out right at
894 * the start (chain->idx == -1)
895 */
896 else if (rc == 1 && (chn->enabled == FALSE ||
897 chn->idx + 1 == (int) chn->driver->nidinfos ||
898 chn->idx == -1)) {
899
900 size_t idx = chn->driver->id + 1;
901
902 if (idx < BLKID_NCHAINS)
903 chn = pr->cur_chain = &pr->chains[idx];
904 else {
905 blkid_probe_end(pr);
906 return 1; /* all chains already probed */
907 }
908 }
909
910 chn->binary = FALSE; /* for sure... */
911
912 DBG(DEBUG_LOWPROBE, printf("chain probe %s %s (idx=%d)\n",
913 chn->driver->name,
914 chn->enabled? "ENABLED" : "DISABLED",
915 chn->idx));
916
917 if (!chn->enabled)
918 continue;
919
920 /* rc: -1 = error, 0 = success, 1 = no result */
921 rc = chn->driver->probe(pr, chn);
922
923 } while (rc == 1);
924
925 return rc;
926}
927
928/**
929 * blkid_do_wipe:
930 * @pr: prober
931 * @dryrun: if TRUE then don't touch the device.
932 *
933 * This function erases the current signature detected by @pr. The @pr has to
934 * be open in O_RDWR mode, BLKID_SUBLKS_MAGIC or/and BLKID_PARTS_MAGIC flags
935 * has to be enabled.
936 *
937 * After successful signature removing the @pr prober will be moved one step
938 * back and the next blkid_do_probe() call will again call previously called
939 * probing function.
940 *
941 * <example>
942 * <title>wipe all filesystems or raids from the device</title>
943 * <programlisting>
944 * fd = open(devname, O_RDWR|O_CLOEXEC);
945 * blkid_probe_set_device(pr, fd, 0, 0);
946 *
947 * blkid_probe_enable_superblocks(pr, 1);
948 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
949 *
950 * while (blkid_do_probe(pr) == 0)
951 * blkid_do_wipe(pr, FALSE);
952 * </programlisting>
953 * </example>
954 *
955 * See also blkid_probe_step_back() if you cannot use this build-in wipe
956 * function, but you want to use libblkid probing as a source for wiping.
957 *
958 * Returns: 0 on success, and -1 in case of error.
959 */
960int blkid_do_wipe(blkid_probe pr, int dryrun)
961{
962 const char *off = NULL;
963 size_t len = 0;
964 loff_t offset, l;
965 char buf[BUFSIZ];
966 int fd, rc = 0;
967 struct blkid_chain *chn;
968
969 if (!pr)
970 return -1;
971
972 chn = pr->cur_chain;
973 if (!chn)
974 return -1;
975
976 switch (chn->driver->id) {
977 case BLKID_CHAIN_SUBLKS:
978 rc = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL);
979 if (!rc)
980 rc = blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
981 break;
982 case BLKID_CHAIN_PARTS:
983 rc = blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &off, NULL);
984 if (!rc)
985 rc = blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
986 break;
987 default:
988 return 0;
989 }
990
991 if (rc || len == 0 || off == NULL)
992 return 0;
993
994 offset = strtoll(off, NULL, 10);
995 fd = blkid_probe_get_fd(pr);
996 if (fd < 0)
997 return -1;
998
999 if (len > sizeof(buf))
1000 len = sizeof(buf);
1001
1002 DBG(DEBUG_LOWPROBE, printf(
1003 "do_wipe [offset=0x%jx, len=%zd, chain=%s, idx=%d, dryrun=%s]\n",
1004 offset, len, chn->driver->name, chn->idx, dryrun ? "yes" : "not"));
1005
1006 l = lseek(fd, offset, SEEK_SET);
1007 if (l == (off_t) -1)
1008 return -1;
1009
1010 memset(buf, 0, len);
1011
1012 if (!dryrun && len) {
1013 if (write_all(fd, buf, len))
1014 return -1;
1015 fsync(fd);
1016 return blkid_probe_step_back(pr);
1017 }
1018
1019 return 0;
1020}
1021
1022/**
1023 * blkid_probe_step_back():
1024 * @pr: prober
1025 *
1026 * This function move pointer to the probing chain one step back -- it means
1027 * that the previously used probing function will be called again in the next
1028 * blkid_do_probe() call.
1029 *
1030 * This is necessary for example if you erase or modify on-disk superblock
1031 * according to the current libblkid probing result.
1032 *
1033 * <example>
1034 * <title>wipe all superblock, but use libblkid only for probing</title>
1035 * <programlisting>
1036 * pr = blkid_new_probe_from_filename(devname);
1037 *
1038 * blkid_probe_enable_superblocks(pr, 1);
1039 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1040 *
1041 * while (blkid_do_probe(pr) == 0) {
1042 * const char *ostr = NULL;
1043 * size_t len = 0;
1044 *
1045 * // superblocks
1046 * if (blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &ostr, NULL) == 0)
1047 * blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
1048 *
1049 * // partition tables
1050 * if (len == 0 && blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &ostr, NULL) == 0)
1051 * blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
1052 *
1053 * if (!len || !str)
1054 * continue;
1055 *
1056 * // convert ostr to the real offset by off = strtoll(ostr, NULL, 10);
1057 * // use your stuff to errase @len bytes at the @off
1058 * ....
1059 *
1060 * // retry the last probing to check for backup superblocks ..etc.
1061 * blkid_probe_step_back(pr);
1062 * }
1063 * </programlisting>
1064 * </example>
1065 *
1066 * Returns: 0 on success, and -1 in case of error.
1067 */
1068int blkid_probe_step_back(blkid_probe pr)
1069{
1070 struct blkid_chain *chn;
1071
1072 if (!pr)
1073 return -1;
1074
1075 chn = pr->cur_chain;
1076 if (!chn)
1077 return -1;
1078
1079 blkid_probe_reset_buffer(pr);
1080
1081 if (chn->idx >= 0) {
1082 chn->idx--;
1083 DBG(DEBUG_LOWPROBE,
1084 printf("step back: moving %s chain index to %d\n",
1085 chn->driver->name,
1086 chn->idx));
1087 }
1088
1089 if (chn->idx == -1) {
1090 /* blkid_do_probe() goes to the next chain if the index
1091 * of the current chain is -1, so we have to set the
1092 * chain pointer to the previous chain.
1093 */
1094 size_t idx = chn->driver->id > 0 ? chn->driver->id - 1 : 0;
1095
1096 DBG(DEBUG_LOWPROBE, printf("step back: moving to previous chain\n"));
1097
1098 if (idx > 0)
1099 pr->cur_chain = &pr->chains[idx];
1100 else if (idx == 0)
1101 pr->cur_chain = NULL;
1102 }
1103
1104 return 0;
1105}
1106
1107/**
1108 * blkid_do_safeprobe:
1109 * @pr: prober
1110 *
1111 * This function gathers probing results from all enabled chains and checks
1112 * for ambivalent results (e.g. more filesystems on the device).
1113 *
1114 * This is string-based NAME=value interface only.
1115 *
1116 * Note about suberblocks chain -- the function does not check for filesystems
1117 * when a RAID signature is detected. The function also does not check for
1118 * collision between RAIDs. The first detected RAID is returned. The function
1119 * checks for collision between partition table and RAID signature -- it's
1120 * recommended to enable partitions chain together with superblocks chain.
1121 *
1122 * Returns: 0 on success, 1 if nothing is detected, -2 if ambivalen result is
1123 * detected and -1 on case of error.
1124 */
1125int blkid_do_safeprobe(blkid_probe pr)
1126{
1127 int i, count = 0, rc = 0;
1128
1129 if (!pr)
1130 return -1;
1131
1132 blkid_probe_start(pr);
1133
1134 pr->prob_flags |= BLKID_PROBE_FL_IGNORE_BACKUP;
1135
1136 for (i = 0; i < BLKID_NCHAINS; i++) {
1137 struct blkid_chain *chn;
1138
1139 chn = pr->cur_chain = &pr->chains[i];
1140 chn->binary = FALSE; /* for sure... */
1141
1142 DBG(DEBUG_LOWPROBE, printf("chain safeprobe %s %s\n",
1143 chn->driver->name,
1144 chn->enabled? "ENABLED" : "DISABLED"));
1145
1146 if (!chn->enabled)
1147 continue;
1148
1149 blkid_probe_chain_reset_position(chn);
1150
1151 rc = chn->driver->safeprobe(pr, chn);
1152
1153 blkid_probe_chain_reset_position(chn);
1154
1155 /* rc: -2 ambivalent, -1 = error, 0 = success, 1 = no result */
1156 if (rc < 0)
1157 goto done; /* error */
1158 if (rc == 0)
1159 count++; /* success */
1160 }
1161
1162done:
1163 blkid_probe_end(pr);
1164 if (rc < 0)
1165 return rc;
1166 return count ? 0 : 1;
1167}
1168
1169/**
1170 * blkid_do_fullprobe:
1171 * @pr: prober
1172 *
1173 * This function gathers probing results from all enabled chains. Same as
1174 * blkid_do_safeprobe() but does not check for collision between probing
1175 * result.
1176 *
1177 * This is string-based NAME=value interface only.
1178 *
1179 * Returns: 0 on success, 1 if nothing is detected or -1 on case of error.
1180 */
1181int blkid_do_fullprobe(blkid_probe pr)
1182{
1183 int i, count = 0, rc = 0;
1184
1185 if (!pr)
1186 return -1;
1187
1188 blkid_probe_start(pr);
1189
1190 for (i = 0; i < BLKID_NCHAINS; i++) {
1191 struct blkid_chain *chn;
1192
1193 chn = pr->cur_chain = &pr->chains[i];
1194 chn->binary = FALSE; /* for sure... */
1195
1196 DBG(DEBUG_LOWPROBE, printf("chain fullprobe %s: %s\n",
1197 chn->driver->name,
1198 chn->enabled? "ENABLED" : "DISABLED"));
1199
1200 if (!chn->enabled)
1201 continue;
1202
1203 blkid_probe_chain_reset_position(chn);
1204
1205 rc = chn->driver->probe(pr, chn);
1206
1207 blkid_probe_chain_reset_position(chn);
1208
1209 /* rc: -1 = error, 0 = success, 1 = no result */
1210 if (rc < 0)
1211 goto done; /* error */
1212 if (rc == 0)
1213 count++; /* success */
1214 }
1215
1216done:
1217 blkid_probe_end(pr);
1218 if (rc < 0)
1219 return rc;
1220 return count ? 0 : 1;
1221}
1222
1223/* same sa blkid_probe_get_buffer() but works with 512-sectors */
1224unsigned char *blkid_probe_get_sector(blkid_probe pr, unsigned int sector)
1225{
1226 return pr ? blkid_probe_get_buffer(pr,
1227 ((blkid_loff_t) sector) << 9, 0x200) : NULL;
1228}
1229
1230struct blkid_prval *blkid_probe_assign_value(
1231 blkid_probe pr, const char *name)
1232{
1233 struct blkid_prval *v;
1234
1235 if (!name)
1236 return NULL;
1237 if (pr->nvals >= BLKID_NVALS)
1238 return NULL;
1239
1240 v = &pr->vals[pr->nvals];
1241 v->name = name;
1242 v->chain = pr->cur_chain;
1243 pr->nvals++;
1244
1245 DBG(DEBUG_LOWPROBE,
1246 printf("assigning %s [%s]\n", name, v->chain->driver->name));
1247 return v;
1248}
1249
1250int blkid_probe_reset_last_value(blkid_probe pr)
1251{
1252 struct blkid_prval *v;
1253
1254 if (pr == NULL || pr->nvals == 0)
1255 return -1;
1256
1257 v = &pr->vals[pr->nvals - 1];
1258
1259 DBG(DEBUG_LOWPROBE,
1260 printf("un-assigning %s [%s]\n", v->name, v->chain->driver->name));
1261
1262 memset(v, 0, sizeof(struct blkid_prval));
1263 pr->nvals--;
1264
1265 return 0;
1266
1267}
1268
1269int blkid_probe_set_value(blkid_probe pr, const char *name,
1270 unsigned char *data, size_t len)
1271{
1272 struct blkid_prval *v;
1273
1274 if (len > BLKID_PROBVAL_BUFSIZ)
1275 len = BLKID_PROBVAL_BUFSIZ;
1276
1277 v = blkid_probe_assign_value(pr, name);
1278 if (!v)
1279 return -1;
1280
1281 memcpy(v->data, data, len);
1282 v->len = len;
1283 return 0;
1284}
1285
1286int blkid_probe_vsprintf_value(blkid_probe pr, const char *name,
1287 const char *fmt, va_list ap)
1288{
1289 struct blkid_prval *v;
1290 ssize_t len;
1291
1292 v = blkid_probe_assign_value(pr, name);
1293 if (!v)
1294 return -1;
1295
1296 len = vsnprintf((char *) v->data, sizeof(v->data), fmt, ap);
1297
1298 if (len <= 0 || (size_t) len >= sizeof(v->data)) {
1299 blkid_probe_reset_last_value(pr);
1300 return -1;
1301 }
1302 v->len = len + 1;
1303 return 0;
1304}
1305
1306int blkid_probe_sprintf_value(blkid_probe pr, const char *name,
1307 const char *fmt, ...)
1308{
1309 int rc;
1310 va_list ap;
1311
1312 va_start(ap, fmt);
1313 rc = blkid_probe_vsprintf_value(pr, name, fmt, ap);
1314 va_end(ap);
1315
1316 return rc;
1317}
1318
1319int blkid_probe_set_magic(blkid_probe pr, blkid_loff_t offset,
1320 size_t len, unsigned char *magic)
1321{
1322 int rc = 0;
1323 struct blkid_chain *chn = blkid_probe_get_chain(pr);
1324
1325 if (!chn || !magic || !len || chn->binary)
1326 return 0;
1327
1328 switch (chn->driver->id) {
1329 case BLKID_CHAIN_SUBLKS:
1330 if (!(chn->flags & BLKID_SUBLKS_MAGIC))
1331 return 0;
1332 rc = blkid_probe_set_value(pr, "SBMAGIC", magic, len);
1333 if (!rc)
1334 rc = blkid_probe_sprintf_value(pr,
1335 "SBMAGIC_OFFSET", "%llu", (unsigned long long)offset);
1336 break;
1337 case BLKID_CHAIN_PARTS:
1338 if (!(chn->flags & BLKID_PARTS_MAGIC))
1339 return 0;
1340 rc = blkid_probe_set_value(pr, "PTMAGIC", magic, len);
1341 if (!rc)
1342 rc = blkid_probe_sprintf_value(pr,
1343 "PTMAGIC_OFFSET", "%llu", (unsigned long long)offset);
1344 break;
1345 default:
1346 break;
1347 }
1348
1349 return rc;
1350}
1351
1352/**
1353 * blkid_probe_get_devno:
1354 * @pr: probe
1355 *
1356 * Returns: block device number, or 0 for regular files.
1357 */
1358dev_t blkid_probe_get_devno(blkid_probe pr)
1359{
1360 return pr->devno;
1361}
1362
1363/**
1364 * blkid_probe_get_wholedisk_devno:
1365 * @pr: probe
1366 *
1367 * Returns: device number of the wholedisk, or 0 for regular files.
1368 */
1369dev_t blkid_probe_get_wholedisk_devno(blkid_probe pr)
1370{
1371 if (!pr->disk_devno) {
1372 dev_t devno, disk_devno = 0;
1373
1374 devno = blkid_probe_get_devno(pr);
1375 if (!devno)
1376 return 0;
1377
1378 if (blkid_devno_to_wholedisk(devno, NULL, 0, &disk_devno) == 0)
1379 pr->disk_devno = disk_devno;
1380 }
1381 return pr->disk_devno;
1382}
1383
1384/**
1385 * blkid_probe_is_wholedisk:
1386 * @pr: probe
1387 *
1388 * Returns: 1 if the device is whole-disk or 0.
1389 */
1390int blkid_probe_is_wholedisk(blkid_probe pr)
1391{
1392 dev_t devno, disk_devno;
1393
1394 devno = blkid_probe_get_devno(pr);
1395 if (!devno)
1396 return 0;
1397
1398 disk_devno = blkid_probe_get_wholedisk_devno(pr);
1399 if (!disk_devno)
1400 return 0;
1401
1402 return devno == disk_devno;
1403}
1404
1405blkid_probe blkid_probe_get_wholedisk_probe(blkid_probe pr)
1406{
1407 dev_t disk;
1408
1409 if (blkid_probe_is_wholedisk(pr))
1410 return NULL; /* this is not partition */
1411
1412 if (pr->parent)
1413 /* this is cloned blkid_probe, use parent's stuff */
1414 return blkid_probe_get_wholedisk_probe(pr->parent);
1415
1416 disk = blkid_probe_get_wholedisk_devno(pr);
1417
1418 if (pr->disk_probe && pr->disk_probe->devno != disk) {
1419 /* we have disk prober, but for another disk... close it */
1420 blkid_free_probe(pr->disk_probe);
1421 pr->disk_probe = NULL;
1422 }
1423
1424 if (!pr->disk_probe) {
1425 /* Open a new disk prober */
1426 char *disk_path = blkid_devno_to_devname(disk);
1427
1428 if (!disk_path)
1429 return NULL;
1430
1431 DBG(DEBUG_LOWPROBE, printf("allocate a wholedisk probe\n"));
1432
1433 pr->disk_probe = blkid_new_probe_from_filename(disk_path);
1434
1435 free(disk_path);
1436
1437 if (!pr->disk_probe)
1438 return NULL; /* ENOMEM? */
1439 }
1440
1441 return pr->disk_probe;
1442}
1443
1444/**
1445 * blkid_probe_get_size:
1446 * @pr: probe
1447 *
1448 * This function returns size of probing area as defined by blkid_probe_set_device().
1449 * If the size of the probing area is unrestricted then this function returns
1450 * the real size of device. See also blkid_get_dev_size().
1451 *
1452 * Returns: size in bytes or -1 in case of error.
1453 */
1454blkid_loff_t blkid_probe_get_size(blkid_probe pr)
1455{
1456 return pr ? pr->size : -1;
1457}
1458
1459/**
1460 * blkid_probe_get_offset:
1461 * @pr: probe
1462 *
1463 * This function returns offset of probing area as defined by blkid_probe_set_device().
1464 *
1465 * Returns: offset in bytes or -1 in case of error.
1466 */
1467blkid_loff_t blkid_probe_get_offset(blkid_probe pr)
1468{
1469 return pr ? pr->off : -1;
1470}
1471
1472/**
1473 * blkid_probe_get_fd:
1474 * @pr: probe
1475 *
1476 * Returns: file descriptor for assigned device/file or -1 in case of error.
1477 */
1478int blkid_probe_get_fd(blkid_probe pr)
1479{
1480 return pr ? pr->fd : -1;
1481}
1482
1483/**
1484 * blkid_probe_get_sectorsize:
1485 * @pr: probe or NULL (for NULL returns 512)
1486 *
1487 * Returns: block device logical sector size (BLKSSZGET ioctl, default 512).
1488 */
1489unsigned int blkid_probe_get_sectorsize(blkid_probe pr)
1490{
1491 if (!pr)
1492 return DEFAULT_SECTOR_SIZE; /*... and good luck! */
1493
1494 if (pr->blkssz)
1495 return pr->blkssz;
1496
1497 if (S_ISBLK(pr->mode) &&
1498 blkdev_get_sector_size(pr->fd, (int *) &pr->blkssz) == 0)
1499 return pr->blkssz;
1500
1501 pr->blkssz = DEFAULT_SECTOR_SIZE;
1502 return pr->blkssz;
1503}
1504
1505/**
1506 * blkid_probe_get_sectors:
1507 * @pr: probe
1508 *
1509 * Returns: 512-byte sector count or -1 in case of error.
1510 */
1511blkid_loff_t blkid_probe_get_sectors(blkid_probe pr)
1512{
1513 return pr ? pr->size >> 9 : -1;
1514}
1515
1516/**
1517 * blkid_probe_numof_values:
1518 * @pr: probe
1519 *
1520 * Returns: number of values in probing result or -1 in case of error.
1521 */
1522int blkid_probe_numof_values(blkid_probe pr)
1523{
1524 if (!pr)
1525 return -1;
1526 return pr->nvals;
1527}
1528
1529/**
1530 * blkid_probe_get_value:
1531 * @pr: probe
1532 * @num: wanted value in range 0..N, where N is blkid_probe_numof_values() - 1
1533 * @name: pointer to return value name or NULL
1534 * @data: pointer to return value data or NULL
1535 * @len: pointer to return value length or NULL
1536 *
1537 * Note, the @len returns length of the @data, including the terminating
1538 * '\0' character.
1539 *
1540 * Returns: 0 on success, or -1 in case of error.
1541 */
1542int blkid_probe_get_value(blkid_probe pr, int num, const char **name,
1543 const char **data, size_t *len)
1544{
1545 struct blkid_prval *v = __blkid_probe_get_value(pr, num);
1546
1547 if (!v)
1548 return -1;
1549 if (name)
1550 *name = v->name;
1551 if (data)
1552 *data = (char *) v->data;
1553 if (len)
1554 *len = v->len;
1555
1556 DBG(DEBUG_LOWPROBE, printf("returning %s value\n", v->name));
1557 return 0;
1558}
1559
1560/**
1561 * blkid_probe_lookup_value:
1562 * @pr: probe
1563 * @name: name of value
1564 * @data: pointer to return value data or NULL
1565 * @len: pointer to return value length or NULL
1566 *
1567 * Note, the @len returns length of the @data, including the terminating
1568 * '\0' character.
1569 *
1570 * Returns: 0 on success, or -1 in case of error.
1571 */
1572int blkid_probe_lookup_value(blkid_probe pr, const char *name,
1573 const char **data, size_t *len)
1574{
1575 struct blkid_prval *v = __blkid_probe_lookup_value(pr, name);
1576
1577 if (!v)
1578 return -1;
1579 if (data)
1580 *data = (char *) v->data;
1581 if (len)
1582 *len = v->len;
1583 return 0;
1584}
1585
1586/**
1587 * blkid_probe_has_value:
1588 * @pr: probe
1589 * @name: name of value
1590 *
1591 * Returns: 1 if value exist in probing result, otherwise 0.
1592 */
1593int blkid_probe_has_value(blkid_probe pr, const char *name)
1594{
1595 if (blkid_probe_lookup_value(pr, name, NULL, NULL) == 0)
1596 return 1;
1597 return 0;
1598}
1599
1600struct blkid_prval *__blkid_probe_get_value(blkid_probe pr, int num)
1601{
1602 if (!pr || num < 0 || num >= pr->nvals)
1603 return NULL;
1604
1605 return &pr->vals[num];
1606}
1607
1608struct blkid_prval *__blkid_probe_lookup_value(blkid_probe pr, const char *name)
1609{
1610 int i;
1611
1612 if (!pr || !pr->nvals || !name)
1613 return NULL;
1614
1615 for (i = 0; i < pr->nvals; i++) {
1616 struct blkid_prval *v = &pr->vals[i];
1617
1618 if (v->name && strcmp(name, v->name) == 0) {
1619 DBG(DEBUG_LOWPROBE, printf("returning %s value\n", v->name));
1620 return v;
1621 }
1622 }
1623 return NULL;
1624}
1625
1626
1627/* converts DCE UUID (uuid[16]) to human readable string
1628 * - the @len should be always 37 */
1629#ifdef HAVE_LIBUUID
1630void blkid_unparse_uuid(const unsigned char *uuid, char *str,
1631 size_t len __attribute__((__unused__)))
1632{
1633 uuid_unparse(uuid, str);
1634}
1635#else
1636void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len)
1637{
1638 snprintf(str, len,
1639 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1640 uuid[0], uuid[1], uuid[2], uuid[3],
1641 uuid[4], uuid[5],
1642 uuid[6], uuid[7],
1643 uuid[8], uuid[9],
1644 uuid[10], uuid[11], uuid[12], uuid[13], uuid[14],uuid[15]);
1645}
1646#endif
1647
1648
1649/* Removes whitespace from the right-hand side of a string (trailing
1650 * whitespace).
1651 *
1652 * Returns size of the new string (without \0).
1653 */
1654size_t blkid_rtrim_whitespace(unsigned char *str)
1655{
1656 size_t i = strlen((char *) str);
1657
1658 while (i--) {
1659 if (!isspace(str[i]))
1660 break;
1661 }
1662 str[++i] = '\0';
1663 return i;
1664}
1665
1666/* Removes whitespace from the left-hand side of a string.
1667 *
1668 * Returns size of the new string (without \0).
1669 */
1670size_t blkid_ltrim_whitespace(unsigned char *str)
1671{
1672 size_t len;
1673 unsigned char *p;
1674
1675 for (p = str; p && isspace(*p); p++);
1676
1677 len = strlen((char *) p);
1678
1679 if (len && p > str)
1680 memmove(str, p, len + 1);
1681
1682 return len;
1683}
1684/*
1685 * Some mkfs-like utils wipe some parts (usually begin) of the device.
1686 * For example LVM (pvcreate) or mkswap(8). This information could be used
1687 * for later resolution to conflicts between superblocks.
1688 *
1689 * For example we found valid LVM superblock, LVM wipes 8KiB at the begin of
1690 * the device. If we found another signature (for example MBR) within the
1691 * wiped area then the signature has been added later and LVM superblock
1692 * should be ignore.
1693 *
1694 * Note that this heuristic is not 100% reliable, for example "pvcreate --zero
1695 * n" allows to keep the begin of the device unmodified. It's probably better
1696 * to use this heuristic for conflicts between superblocks and partition tables
1697 * than for conflicts between filesystem superblocks -- existence of unwanted
1698 * partition table is very unusual, because PT is pretty visible (parsed and
1699 * interpreted by kernel).
1700 *
1701 * Note that we usually expect only one signature on the device, it means that
1702 * we have to remember only one wiped area from previously successfully
1703 * detected signature.
1704 *
1705 * blkid_probe_set_wiper() -- defines wiped area (e.g. LVM)
1706 * blkid_probe_use_wiper() -- try to use area (e.g. MBR)
1707 *
1708 * Note that there is not relation between _wiper and blkid_to_wipe().
1709 *
1710 */
1711void blkid_probe_set_wiper(blkid_probe pr, blkid_loff_t off, blkid_loff_t size)
1712{
1713 struct blkid_chain *chn;
1714
1715 if (!pr)
1716 return;
1717
1718 if (!size) {
1719 DBG(DEBUG_LOWPROBE, printf("zeroize wiper\n"));
1720 pr->wipe_size = pr->wipe_off = 0;
1721 pr->wipe_chain = NULL;
1722 return;
1723 }
1724
1725 chn = pr->cur_chain;
1726
1727 if (!chn || !chn->driver ||
1728 chn->idx < 0 || (size_t) chn->idx >= chn->driver->nidinfos)
1729 return;
1730
1731 pr->wipe_size = size;
1732 pr->wipe_off = off;
1733 pr->wipe_chain = chn;
1734
1735 DBG(DEBUG_LOWPROBE,
1736 printf("wiper set to %s::%s off=%jd size=%jd\n",
1737 chn->driver->name,
1738 chn->driver->idinfos[chn->idx]->name,
1739 pr->wipe_off, pr->wipe_size));
1740 return;
1741}
1742
1743/*
1744 * Returns 1 if the <@off,@size> area was wiped
1745 */
1746int blkid_probe_is_wiped(blkid_probe pr, struct blkid_chain **chn,
1747 blkid_loff_t off, blkid_loff_t size)
1748{
1749 if (!pr || !size)
1750 return 0;
1751
1752 if (pr->wipe_off <= off && off + size <= pr->wipe_off + pr->wipe_size) {
1753 if (chn)
1754 *chn = pr->wipe_chain;
1755 return 1;
1756 }
1757 return 0;
1758}
1759
1760/*
1761 * Try to use any area -- if the area has been previously wiped then the
1762 * previous probing result should be ignored (reseted).
1763 */
1764void blkid_probe_use_wiper(blkid_probe pr, blkid_loff_t off, blkid_loff_t size)
1765{
1766 struct blkid_chain *chn = NULL;
1767
1768 if (blkid_probe_is_wiped(pr, &chn, off, size) && chn) {
1769 DBG(DEBUG_LOWPROBE, printf("previously wiped area modified "
1770 " -- ignore previous results\n"));
1771 blkid_probe_set_wiper(pr, 0, 0);
1772 blkid_probe_chain_reset_vals(pr, chn);
1773 }
1774}
1775
1776int blkid_probe_ignore_backup(blkid_probe pr)
1777{
1778 return pr && (pr->prob_flags & BLKID_PROBE_FL_IGNORE_BACKUP);
1779}