]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/probe.c
2dd87cf9ad3fd8ffa305d1d242eb8f96f07879cf
[thirdparty/util-linux.git] / libblkid / src / probe.c
1 /*
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 #include <limits.h>
107
108 #include "blkidP.h"
109 #include "all-io.h"
110 #include "sysfs.h"
111 #include "strutils.h"
112 #include "list.h"
113
114 /*
115 * All supported chains
116 */
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
121 };
122
123 static void blkid_probe_reset_values(blkid_probe pr);
124
125 /**
126 * blkid_new_probe:
127 *
128 * Returns: a pointer to the newly allocated probe struct or NULL in case of error.
129 */
130 blkid_probe blkid_new_probe(void)
131 {
132 int i;
133 blkid_probe pr;
134
135 blkid_init_debug(0);
136 pr = calloc(1, sizeof(struct blkid_struct_probe));
137 if (!pr)
138 return NULL;
139
140 DBG(LOWPROBE, ul_debug("allocate a new probe"));
141
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;
147 }
148 INIT_LIST_HEAD(&pr->buffers);
149 INIT_LIST_HEAD(&pr->values);
150 return pr;
151 }
152
153 /*
154 * Clone @parent, the new clone shares all, but except:
155 *
156 * - probing result
157 * - buffers if another device (or offset) is set to the prober
158 */
159 blkid_probe blkid_clone_probe(blkid_probe parent)
160 {
161 blkid_probe pr;
162
163 if (!parent)
164 return NULL;
165
166 DBG(LOWPROBE, ul_debug("allocate a probe clone"));
167
168 pr = blkid_new_probe();
169 if (!pr)
170 return NULL;
171
172 pr->fd = parent->fd;
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;
179 pr->parent = parent;
180
181 pr->flags &= ~BLKID_FL_PRIVATE_FD;
182
183 return pr;
184 }
185
186
187
188 /**
189 * blkid_new_probe_from_filename:
190 * @filename: device or regular file
191 *
192 * This function is same as call open(filename), blkid_new_probe() and
193 * blkid_probe_set_device(pr, fd, 0, 0).
194 *
195 * The @filename is closed by blkid_free_probe() or by the
196 * blkid_probe_set_device() call.
197 *
198 * Returns: a pointer to the newly allocated probe struct or NULL in case of
199 * error.
200 */
201 blkid_probe blkid_new_probe_from_filename(const char *filename)
202 {
203 int fd;
204 blkid_probe pr = NULL;
205
206 fd = open(filename, O_RDONLY|O_CLOEXEC);
207 if (fd < 0)
208 return NULL;
209
210 pr = blkid_new_probe();
211 if (!pr)
212 goto err;
213
214 if (blkid_probe_set_device(pr, fd, 0, 0))
215 goto err;
216
217 pr->flags |= BLKID_FL_PRIVATE_FD;
218 return pr;
219 err:
220 if (fd >= 0)
221 close(fd);
222 blkid_free_probe(pr);
223 return NULL;
224 }
225
226 /**
227 * blkid_free_probe:
228 * @pr: probe
229 *
230 * Deallocates the probe struct, buffers and all allocated
231 * data that are associated with this probing control struct.
232 */
233 void blkid_free_probe(blkid_probe pr)
234 {
235 int i;
236
237 if (!pr)
238 return;
239
240 for (i = 0; i < BLKID_NCHAINS; i++) {
241 struct blkid_chain *ch = &pr->chains[i];
242
243 if (ch->driver->free_data)
244 ch->driver->free_data(pr, ch->data);
245 free(ch->fltr);
246 }
247
248 if ((pr->flags & BLKID_FL_PRIVATE_FD) && pr->fd >= 0)
249 close(pr->fd);
250 blkid_probe_reset_buffers(pr);
251 blkid_probe_reset_values(pr);
252 blkid_free_probe(pr->disk_probe);
253
254 DBG(LOWPROBE, ul_debug("free probe"));
255 free(pr);
256 }
257
258 void blkid_probe_free_value(struct blkid_prval *v)
259 {
260 if (!v)
261 return;
262
263 list_del(&v->prvals);
264 free(v->data);
265
266 DBG(LOWPROBE, ul_debug(" free value %s", v->name));
267 free(v);
268 }
269
270 /*
271 * Removes chain values from probing result.
272 */
273 void blkid_probe_chain_reset_values(blkid_probe pr, struct blkid_chain *chn)
274 {
275
276 struct list_head *p, *pnext;
277
278 if (list_empty(&pr->values))
279 return;
280
281 DBG(LOWPROBE, ul_debug("Resetting %s values", chn->driver->name));
282
283 list_for_each_safe(p, pnext, &pr->values) {
284 struct blkid_prval *v = list_entry(p,
285 struct blkid_prval, prvals);
286
287 if (v->chain == chn)
288 blkid_probe_free_value(v);
289 }
290 }
291
292 static void blkid_probe_chain_reset_position(struct blkid_chain *chn)
293 {
294 chn->idx = -1;
295 }
296
297 /*
298 * Move chain values from probing result to @vals
299 */
300 int blkid_probe_chain_save_values(blkid_probe pr, struct blkid_chain *chn,
301 struct list_head *vals)
302 {
303 struct list_head *p, *pnext;
304 struct blkid_prval *v;
305
306 DBG(LOWPROBE, ul_debug("saving %s values", chn->driver->name));
307
308 list_for_each_safe(p, pnext, &pr->values) {
309
310 v = list_entry(p, struct blkid_prval, prvals);
311 if (v->chain != chn)
312 continue;
313
314 list_del_init(&v->prvals);
315 list_add_tail(&v->prvals, vals);
316 }
317 return 0;
318 }
319
320 /*
321 * Appends values from @vals to the probing result
322 */
323 void blkid_probe_append_values_list(blkid_probe pr, struct list_head *vals)
324 {
325 DBG(LOWPROBE, ul_debug("appending values"));
326
327 list_splice(vals, &pr->values);
328 INIT_LIST_HEAD(vals);
329 }
330
331
332 void blkid_probe_free_values_list(struct list_head *vals)
333 {
334 if (!vals)
335 return;
336
337 DBG(LOWPROBE, ul_debug("freeing values list"));
338
339 while (!list_empty(vals)) {
340 struct blkid_prval *v = list_entry(vals->next, struct blkid_prval, prvals);
341 blkid_probe_free_value(v);
342 }
343 }
344
345 struct blkid_chain *blkid_probe_get_chain(blkid_probe pr)
346 {
347 return pr->cur_chain;
348 }
349
350 static const char *blkid_probe_get_probername(blkid_probe pr)
351 {
352 struct blkid_chain *chn = blkid_probe_get_chain(pr);
353
354 if (chn && chn->idx >= 0 && (unsigned)chn->idx < chn->driver->nidinfos)
355 return chn->driver->idinfos[chn->idx]->name;
356
357 return NULL;
358 }
359
360 void *blkid_probe_get_binary_data(blkid_probe pr, struct blkid_chain *chn)
361 {
362 int rc, org_prob_flags;
363 struct blkid_chain *org_chn;
364
365 /* save the current setting -- the binary API has to be completely
366 * independent on the current probing status
367 */
368 org_chn = pr->cur_chain;
369 org_prob_flags = pr->prob_flags;
370
371 pr->cur_chain = chn;
372 pr->prob_flags = 0;
373 chn->binary = TRUE;
374 blkid_probe_chain_reset_position(chn);
375
376 rc = chn->driver->probe(pr, chn);
377
378 chn->binary = FALSE;
379 blkid_probe_chain_reset_position(chn);
380
381 /* restore the original setting
382 */
383 pr->cur_chain = org_chn;
384 pr->prob_flags = org_prob_flags;
385
386 if (rc != 0)
387 return NULL;
388
389 DBG(LOWPROBE, ul_debug("returning %s binary data", chn->driver->name));
390 return chn->data;
391 }
392
393
394 /**
395 * blkid_reset_probe:
396 * @pr: probe
397 *
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.
401 */
402 void blkid_reset_probe(blkid_probe pr)
403 {
404 int i;
405
406 blkid_probe_reset_values(pr);
407 blkid_probe_set_wiper(pr, 0, 0);
408
409 pr->cur_chain = NULL;
410
411 for (i = 0; i < BLKID_NCHAINS; i++)
412 blkid_probe_chain_reset_position(&pr->chains[i]);
413 }
414
415 /***
416 static int blkid_probe_dump_filter(blkid_probe pr, int chain)
417 {
418 struct blkid_chain *chn;
419 int i;
420
421 if (!pr || chain < 0 || chain >= BLKID_NCHAINS)
422 return -1;
423
424 chn = &pr->chains[chain];
425
426 if (!chn->fltr)
427 return -1;
428
429 for (i = 0; i < chn->driver->nidinfos; i++) {
430 const struct blkid_idinfo *id = chn->driver->idinfos[i];
431
432 DBG(LOWPROBE, ul_debug("%d: %s: %s",
433 i,
434 id->name,
435 blkid_bmp_get_item(chn->fltr, i)
436 ? "disabled" : "enabled <--"));
437 }
438 return 0;
439 }
440 ***/
441
442 /*
443 * Returns properly initialized chain filter
444 */
445 unsigned long *blkid_probe_get_filter(blkid_probe pr, int chain, int create)
446 {
447 struct blkid_chain *chn;
448
449 if (chain < 0 || chain >= BLKID_NCHAINS)
450 return NULL;
451
452 chn = &pr->chains[chain];
453
454 /* always when you touch the chain filter all indexes are reset and
455 * probing starts from scratch
456 */
457 blkid_probe_chain_reset_position(chn);
458 pr->cur_chain = NULL;
459
460 if (!chn->driver->has_fltr || (!chn->fltr && !create))
461 return NULL;
462
463 if (!chn->fltr)
464 chn->fltr = calloc(1, blkid_bmp_nbytes(chn->driver->nidinfos));
465 else
466 memset(chn->fltr, 0, blkid_bmp_nbytes(chn->driver->nidinfos));
467
468 /* blkid_probe_dump_filter(pr, chain); */
469 return chn->fltr;
470 }
471
472 /*
473 * Generic private functions for filter setting
474 */
475 int __blkid_probe_invert_filter(blkid_probe pr, int chain)
476 {
477 size_t i;
478 struct blkid_chain *chn;
479
480 chn = &pr->chains[chain];
481
482 if (!chn->driver->has_fltr || !chn->fltr)
483 return -1;
484
485 for (i = 0; i < blkid_bmp_nwords(chn->driver->nidinfos); i++)
486 chn->fltr[i] = ~chn->fltr[i];
487
488 DBG(LOWPROBE, ul_debug("probing filter inverted"));
489 /* blkid_probe_dump_filter(pr, chain); */
490 return 0;
491 }
492
493 int __blkid_probe_reset_filter(blkid_probe pr, int chain)
494 {
495 return blkid_probe_get_filter(pr, chain, FALSE) ? 0 : -1;
496 }
497
498 int __blkid_probe_filter_types(blkid_probe pr, int chain, int flag, char *names[])
499 {
500 unsigned long *fltr;
501 struct blkid_chain *chn;
502 size_t i;
503
504 fltr = blkid_probe_get_filter(pr, chain, TRUE);
505 if (!fltr)
506 return -1;
507
508 chn = &pr->chains[chain];
509
510 for (i = 0; i < chn->driver->nidinfos; i++) {
511 int has = 0;
512 const struct blkid_idinfo *id = chn->driver->idinfos[i];
513 char **n;
514
515 for (n = names; *n; n++) {
516 if (!strcmp(id->name, *n)) {
517 has = 1;
518 break;
519 }
520 }
521 if (has) {
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);
526 }
527
528 DBG(LOWPROBE, ul_debug("%s: a new probing type-filter initialized",
529 chn->driver->name));
530 /* blkid_probe_dump_filter(pr, chain); */
531 return 0;
532 }
533
534 static struct blkid_bufinfo *read_buffer(blkid_probe pr, uint64_t real_off, uint64_t len)
535 {
536 ssize_t ret;
537 struct blkid_bufinfo *bf = NULL;
538
539 if (blkid_llseek(pr->fd, real_off, SEEK_SET) < 0) {
540 errno = 0;
541 return NULL;
542 }
543
544 /* someone trying to overflow some buffers? */
545 if (len > ULONG_MAX - sizeof(struct blkid_bufinfo)) {
546 errno = ENOMEM;
547 return NULL;
548 }
549
550 /* allocate info and space for data by one malloc call */
551 bf = calloc(1, sizeof(struct blkid_bufinfo) + len);
552 if (!bf) {
553 errno = ENOMEM;
554 return NULL;
555 }
556
557 bf->data = ((unsigned char *) bf) + sizeof(struct blkid_bufinfo);
558 bf->len = len;
559 bf->off = real_off;
560 INIT_LIST_HEAD(&bf->bufs);
561
562 DBG(LOWPROBE, ul_debug("\tread: off=%"PRIu64" len=%"PRIu64"",
563 real_off, len));
564
565 ret = read(pr->fd, bf->data, len);
566 if (ret != (ssize_t) len) {
567 DBG(LOWPROBE, ul_debug("\tread failed: %m"));
568 free(bf);
569
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))
573 errno = 0;
574 return NULL;
575 }
576
577 return bf;
578 }
579
580 /*
581 * Search in buffers we already in memory
582 */
583 static struct blkid_bufinfo *get_cached_buffer(blkid_probe pr, uint64_t off, uint64_t len)
584 {
585 uint64_t real_off = pr->off + off;
586 struct list_head *p;
587
588 list_for_each(p, &pr->buffers) {
589 struct blkid_bufinfo *x =
590 list_entry(p, struct blkid_bufinfo, bufs);
591
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));
595 return x;
596 }
597 }
598 return NULL;
599 }
600
601 /*
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.
605 *
606 * See blkid_probe_hide_range().
607 */
608 static int hide_buffer(blkid_probe pr, uint64_t off, uint64_t len)
609 {
610 uint64_t real_off = pr->off + off;
611 struct list_head *p;
612 int ct = 0;
613
614 list_for_each(p, &pr->buffers) {
615 struct blkid_bufinfo *x =
616 list_entry(p, struct blkid_bufinfo, bufs);
617 unsigned char *data;
618
619 if (real_off >= x->off && real_off + len <= x->off + x->len) {
620
621 assert(x->off <= real_off);
622 assert(x->off + x->len >= real_off + len);
623
624 data = real_off ? x->data + (real_off - x->off) : x->data;
625
626 DBG(BUFFER, ul_debug("\thiding: off=%"PRIu64" len=%"PRIu64,
627 off, len));
628 memset(data, 0, len);
629 ct++;
630 }
631 }
632 return ct == 0 ? -EINVAL : 0;
633 }
634
635
636 /*
637 * Note that @off is offset within probing area, the probing area is defined by
638 * pr->off and pr->size.
639 */
640 unsigned char *blkid_probe_get_buffer(blkid_probe pr, uint64_t off, uint64_t len)
641 {
642 struct blkid_bufinfo *bf = NULL;
643 uint64_t real_off = pr->off + off;
644
645 /*
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));
648 */
649
650 if (pr->size == 0) {
651 errno = EINVAL;
652 return NULL;
653 }
654
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"));
657 errno = 0;
658 return NULL;
659 }
660
661 if (pr->parent &&
662 pr->parent->devno == pr->devno &&
663 pr->parent->off <= pr->off &&
664 pr->parent->off + pr->parent->size >= pr->off + pr->size) {
665 /*
666 * This is a cloned prober and points to the same area as
667 * parent. Let's use parent's buffers.
668 *
669 * Note that pr->off (and pr->parent->off) is always from the
670 * begin of the device.
671 */
672 return blkid_probe_get_buffer(pr->parent,
673 pr->off + off - pr->parent->off, len);
674 }
675
676 /* try buffers we already have in memory or read from device */
677 bf = get_cached_buffer(pr, off, len);
678 if (!bf) {
679 bf = read_buffer(pr, real_off, len);
680 if (!bf)
681 return NULL;
682
683 list_add_tail(&bf->bufs, &pr->buffers);
684 }
685
686 assert(bf->off <= real_off);
687 assert(bf->off + bf->len >= real_off + len);
688
689 errno = 0;
690 return real_off ? bf->data + (real_off - bf->off) : bf->data;
691 }
692
693 /**
694 * blkid_probe_reset_buffers:
695 * @pr: prober
696 *
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
700 * device.
701 *
702 * Returns: <0 in case of failure, or 0 on success.
703 */
704 int blkid_probe_reset_buffers(blkid_probe pr)
705 {
706 uint64_t ct = 0, len = 0;
707
708 pr->flags &= ~BLKID_FL_MODIF_BUFF;
709
710 if (list_empty(&pr->buffers))
711 return 0;
712
713 DBG(BUFFER, ul_debug("Resetting probing buffers"));
714
715 while (!list_empty(&pr->buffers)) {
716 struct blkid_bufinfo *bf = list_entry(pr->buffers.next,
717 struct blkid_bufinfo, bufs);
718 ct++;
719 len += bf->len;
720 list_del(&bf->bufs);
721
722 DBG(BUFFER, ul_debug(" remove buffer: [off=%"PRIu64", len=%"PRIu64"]",
723 bf->off, bf->len));
724 free(bf);
725 }
726
727 DBG(LOWPROBE, ul_debug(" buffers summary: %"PRIu64" bytes by %"PRIu64" read() calls",
728 len, ct));
729
730 INIT_LIST_HEAD(&pr->buffers);
731
732 return 0;
733 }
734
735 /**
736 * blkid_probe_hide_range:
737 * @pr: prober
738 * @off: start of the range
739 * @len: size of the range
740 *
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.
744 *
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.
747 *
748 * The function blkid_probe_reset_buffers() reverts all.
749 *
750 * Returns: <0 in case of failure, or 0 on success.
751 */
752 int blkid_probe_hide_range(blkid_probe pr, uint64_t off, uint64_t len)
753 {
754 int rc = hide_buffer(pr, off, len);
755
756 if (rc == 0)
757 pr->flags |= BLKID_FL_MODIF_BUFF;
758 return rc;
759 }
760
761 static void blkid_probe_reset_values(blkid_probe pr)
762 {
763 if (list_empty(&pr->values))
764 return;
765
766 DBG(LOWPROBE, ul_debug("resetting results"));
767
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);
772 }
773
774 INIT_LIST_HEAD(&pr->values);
775 }
776
777 /*
778 * Small devices need a special care.
779 */
780 int blkid_probe_is_tiny(blkid_probe pr)
781 {
782 return (pr->flags & BLKID_FL_TINY_DEV);
783 }
784
785 /*
786 * CDROMs may fail when probed for RAID (last sector problem)
787 */
788 int blkid_probe_is_cdrom(blkid_probe pr)
789 {
790 return (pr->flags & BLKID_FL_CDROM_DEV);
791 }
792
793 #ifdef CDROM_GET_CAPABILITY
794
795 static int is_sector_readable(int fd, uint64_t sector)
796 {
797 char buf[512];
798 ssize_t sz;
799
800 if (blkid_llseek(fd, sector * 512, SEEK_SET) < 0)
801 goto failed;
802
803 sz = read(fd, buf, sizeof(buf));
804 if (sz != (ssize_t) sizeof(buf))
805 goto failed;
806
807 return 1;
808 failed:
809 DBG(LOWPROBE, ul_debug("CDROM: read sector %"PRIu64" failed %m", sector));
810 errno = 0;
811 return 0;
812 }
813
814 /*
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
819 * sectors).
820 */
821 static void cdrom_size_correction(blkid_probe pr)
822 {
823 uint64_t n, nsectors = pr->size >> 9;
824
825 for (n = nsectors - 12; n < nsectors; n++) {
826 if (!is_sector_readable(pr->fd, n))
827 goto failed;
828 }
829
830 DBG(LOWPROBE, ul_debug("CDROM: full size available"));
831 return;
832 failed:
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));
837 pr->size = n << 9;
838 }
839
840 #endif
841
842 /**
843 * blkid_probe_set_device:
844 * @pr: probe
845 * @fd: device file descriptor
846 * @off: begin of probing area
847 * @size: size of probing area (zero means whole device/file)
848 *
849 * Assigns the device to probe control struct, resets internal buffers and
850 * resets the current probing.
851 *
852 * Returns: -1 in case of failure, or 0 on success.
853 */
854 int blkid_probe_set_device(blkid_probe pr, int fd,
855 blkid_loff_t off, blkid_loff_t size)
856 {
857 struct stat sb;
858 uint64_t devsiz = 0;
859 char *dm_uuid = NULL;
860
861 blkid_reset_probe(pr);
862 blkid_probe_reset_buffers(pr);
863
864 if ((pr->flags & BLKID_FL_PRIVATE_FD) && pr->fd >= 0)
865 close(pr->fd);
866
867 pr->flags &= ~BLKID_FL_PRIVATE_FD;
868 pr->flags &= ~BLKID_FL_TINY_DEV;
869 pr->flags &= ~BLKID_FL_CDROM_DEV;
870 pr->prob_flags = 0;
871 pr->fd = fd;
872 pr->off = (uint64_t) off;
873 pr->size = 0;
874 pr->devno = 0;
875 pr->disk_devno = 0;
876 pr->mode = 0;
877 pr->blkssz = 0;
878 pr->wipe_off = 0;
879 pr->wipe_size = 0;
880 pr->wipe_chain = NULL;
881
882 #if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE)
883 /* Disable read-ahead */
884 posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
885 #endif
886 if (fstat(fd, &sb))
887 goto err;
888
889 if (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
890 errno = EINVAL;
891 goto err;
892 }
893
894 pr->mode = sb.st_mode;
895 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode))
896 pr->devno = sb.st_rdev;
897
898 if (S_ISBLK(sb.st_mode)) {
899 if (blkdev_get_size(fd, (unsigned long long *) &devsiz)) {
900 DBG(LOWPROBE, ul_debug("failed to get device size"));
901 goto err;
902 }
903 } else if (S_ISCHR(sb.st_mode))
904 devsiz = 1; /* UBI devices are char... */
905 else if (S_ISREG(sb.st_mode))
906 devsiz = sb.st_size; /* regular file */
907
908 pr->size = size ? (uint64_t)size : devsiz;
909
910 if (off && size == 0)
911 /* only offset without size specified */
912 pr->size -= (uint64_t) off;
913
914 if (pr->off + pr->size > devsiz) {
915 DBG(LOWPROBE, ul_debug("area specified by offset and size is bigger than device"));
916 errno = EINVAL;
917 goto err;
918 }
919
920 if (pr->size <= 1440 * 1024 && !S_ISCHR(sb.st_mode))
921 pr->flags |= BLKID_FL_TINY_DEV;
922
923 if (S_ISBLK(sb.st_mode) &&
924 sysfs_devno_is_dm_private(sb.st_rdev, &dm_uuid)) {
925 DBG(LOWPROBE, ul_debug("ignore private device mapper device"));
926 pr->flags |= BLKID_FL_NOSCAN_DEV;
927 }
928
929 #ifdef CDROM_GET_CAPABILITY
930 else if (S_ISBLK(sb.st_mode) &&
931 !blkid_probe_is_tiny(pr) &&
932 !dm_uuid &&
933 blkid_probe_is_wholedisk(pr) &&
934 ioctl(fd, CDROM_GET_CAPABILITY, NULL) >= 0) {
935
936 pr->flags |= BLKID_FL_CDROM_DEV;
937 cdrom_size_correction(pr);
938 }
939 #endif
940 free(dm_uuid);
941
942 DBG(LOWPROBE, ul_debug("ready for low-probing, offset=%"PRIu64", size=%"PRIu64"",
943 pr->off, pr->size));
944 DBG(LOWPROBE, ul_debug("whole-disk: %s, regfile: %s",
945 blkid_probe_is_wholedisk(pr) ?"YES" : "NO",
946 S_ISREG(pr->mode) ? "YES" : "NO"));
947
948 return 0;
949 err:
950 DBG(LOWPROBE, ul_debug("failed to prepare a device for low-probing"));
951 return -1;
952
953 }
954
955 int blkid_probe_get_dimension(blkid_probe pr, uint64_t *off, uint64_t *size)
956 {
957 *off = pr->off;
958 *size = pr->size;
959 return 0;
960 }
961
962 int blkid_probe_set_dimension(blkid_probe pr, uint64_t off, uint64_t size)
963 {
964 DBG(LOWPROBE, ul_debug(
965 "changing probing area: size=%"PRIu64", off=%"PRIu64" "
966 "-to-> size=%"PRIu64", off=%"PRIu64"",
967 pr->size, pr->off, size, off));
968
969 pr->off = off;
970 pr->size = size;
971 pr->flags &= ~BLKID_FL_TINY_DEV;
972
973 if (pr->size <= 1440ULL * 1024ULL && !S_ISCHR(pr->mode))
974 pr->flags |= BLKID_FL_TINY_DEV;
975
976 blkid_probe_reset_buffers(pr);
977
978 return 0;
979 }
980
981 /*
982 * Check for matching magic value.
983 * Returns BLKID_PROBE_OK if found, BLKID_PROBE_NONE if not found
984 * or no magic present, or negative value on error.
985 */
986 int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id,
987 uint64_t *offset, const struct blkid_idmag **res)
988 {
989 const struct blkid_idmag *mag = NULL;
990 uint64_t off = 0;
991
992 if (id)
993 mag = &id->magics[0];
994 if (res)
995 *res = NULL;
996
997 /* try to detect by magic string */
998 while(mag && mag->magic) {
999 unsigned char *buf;
1000
1001 off = (mag->kboff + (mag->sboff >> 10)) << 10;
1002 buf = blkid_probe_get_buffer(pr, off, 1024);
1003
1004 if (!buf && errno)
1005 return -errno;
1006
1007 if (buf && !memcmp(mag->magic,
1008 buf + (mag->sboff & 0x3ff), mag->len)) {
1009
1010 DBG(LOWPROBE, ul_debug("\tmagic sboff=%u, kboff=%ld",
1011 mag->sboff, mag->kboff));
1012 if (offset)
1013 *offset = off + (mag->sboff & 0x3ff);
1014 if (res)
1015 *res = mag;
1016 return BLKID_PROBE_OK;
1017 }
1018 mag++;
1019 }
1020
1021 if (id && id->magics[0].magic)
1022 /* magic string(s) defined, but not found */
1023 return BLKID_PROBE_NONE;
1024
1025 return BLKID_PROBE_OK;
1026 }
1027
1028 static inline void blkid_probe_start(blkid_probe pr)
1029 {
1030 DBG(LOWPROBE, ul_debug("start probe"));
1031 pr->cur_chain = NULL;
1032 pr->prob_flags = 0;
1033 blkid_probe_set_wiper(pr, 0, 0);
1034 }
1035
1036 static inline void blkid_probe_end(blkid_probe pr)
1037 {
1038 DBG(LOWPROBE, ul_debug("end probe"));
1039 pr->cur_chain = NULL;
1040 pr->prob_flags = 0;
1041 blkid_probe_set_wiper(pr, 0, 0);
1042 }
1043
1044 /**
1045 * blkid_do_probe:
1046 * @pr: prober
1047 *
1048 * Calls probing functions in all enabled chains. The superblocks chain is
1049 * enabled by default. The blkid_do_probe() stores result from only one
1050 * probing function. It's necessary to call this routine in a loop to get
1051 * results from all probing functions in all chains. The probing is reset
1052 * by blkid_reset_probe() or by filter functions.
1053 *
1054 * This is string-based NAME=value interface only.
1055 *
1056 * <example>
1057 * <title>basic case - use the first result only</title>
1058 * <programlisting>
1059 * if (blkid_do_probe(pr) == 0) {
1060 * int nvals = blkid_probe_numof_values(pr);
1061 * for (n = 0; n < nvals; n++) {
1062 * if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0)
1063 * printf("%s = %s\n", name, data);
1064 * }
1065 * }
1066 * </programlisting>
1067 * </example>
1068 *
1069 * <example>
1070 * <title>advanced case - probe for all signatures</title>
1071 * <programlisting>
1072 * while (blkid_do_probe(pr) == 0) {
1073 * int nvals = blkid_probe_numof_values(pr);
1074 * ...
1075 * }
1076 * </programlisting>
1077 * </example>
1078 *
1079 * See also blkid_reset_probe().
1080 *
1081 * Returns: 0 on success, 1 when probing is done and -1 in case of error.
1082 */
1083 int blkid_do_probe(blkid_probe pr)
1084 {
1085 int rc = 1;
1086
1087 if (pr->flags & BLKID_FL_NOSCAN_DEV)
1088 return 1;
1089
1090 do {
1091 struct blkid_chain *chn = pr->cur_chain;
1092
1093 if (!chn) {
1094 blkid_probe_start(pr);
1095 chn = pr->cur_chain = &pr->chains[0];
1096 }
1097 /* we go to the next chain only when the previous probing
1098 * result was nothing (rc == 1) and when the current chain is
1099 * disabled or we are at end of the current chain (chain->idx +
1100 * 1 == sizeof chain) or the current chain bailed out right at
1101 * the start (chain->idx == -1)
1102 */
1103 else if (rc == 1 && (chn->enabled == FALSE ||
1104 chn->idx + 1 == (int) chn->driver->nidinfos ||
1105 chn->idx == -1)) {
1106
1107 size_t idx = chn->driver->id + 1;
1108
1109 if (idx < BLKID_NCHAINS)
1110 chn = pr->cur_chain = &pr->chains[idx];
1111 else {
1112 blkid_probe_end(pr);
1113 return 1; /* all chains already probed */
1114 }
1115 }
1116
1117 chn->binary = FALSE; /* for sure... */
1118
1119 DBG(LOWPROBE, ul_debug("chain probe %s %s (idx=%d)",
1120 chn->driver->name,
1121 chn->enabled? "ENABLED" : "DISABLED",
1122 chn->idx));
1123
1124 if (!chn->enabled)
1125 continue;
1126
1127 /* rc: -1 = error, 0 = success, 1 = no result */
1128 rc = chn->driver->probe(pr, chn);
1129
1130 } while (rc == 1);
1131
1132 return rc;
1133 }
1134
1135 /**
1136 * blkid_do_wipe:
1137 * @pr: prober
1138 * @dryrun: if TRUE then don't touch the device.
1139 *
1140 * This function erases the current signature detected by @pr. The @pr has to
1141 * be open in O_RDWR mode, BLKID_SUBLKS_MAGIC or/and BLKID_PARTS_MAGIC flags
1142 * has to be enabled (if you want to erase also superblock with broken check
1143 * sums then use BLKID_SUBLKS_BADCSUM too).
1144 *
1145 * After successful signature removing the @pr prober will be moved one step
1146 * back and the next blkid_do_probe() call will again call previously called
1147 * probing function. All in-memory cached data from the device are always
1148 * reset.
1149 *
1150 * <example>
1151 * <title>wipe all filesystems or raids from the device</title>
1152 * <programlisting>
1153 * fd = open(devname, O_RDWR|O_CLOEXEC);
1154 * blkid_probe_set_device(pr, fd, 0, 0);
1155 *
1156 * blkid_probe_enable_superblocks(pr, 1);
1157 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1158 *
1159 * while (blkid_do_probe(pr) == 0)
1160 * blkid_do_wipe(pr, FALSE);
1161 * </programlisting>
1162 * </example>
1163 *
1164 * See also blkid_probe_step_back() if you cannot use this build-in wipe
1165 * function, but you want to use libblkid probing as a source for wiping.
1166 *
1167 * Returns: 0 on success, and -1 in case of error.
1168 */
1169 int blkid_do_wipe(blkid_probe pr, int dryrun)
1170 {
1171 const char *off = NULL;
1172 size_t len = 0;
1173 uint64_t offset, magoff, l;
1174 char buf[BUFSIZ];
1175 int fd, rc = 0;
1176 struct blkid_chain *chn;
1177
1178 chn = pr->cur_chain;
1179 if (!chn)
1180 return -1;
1181
1182 switch (chn->driver->id) {
1183 case BLKID_CHAIN_SUBLKS:
1184 rc = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL);
1185 if (!rc)
1186 rc = blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
1187 break;
1188 case BLKID_CHAIN_PARTS:
1189 rc = blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &off, NULL);
1190 if (!rc)
1191 rc = blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
1192 break;
1193 default:
1194 return 0;
1195 }
1196
1197 if (rc || len == 0 || off == NULL)
1198 return 0;
1199
1200 magoff = strtoumax(off, NULL, 10);
1201 offset = magoff + pr->off;
1202 fd = blkid_probe_get_fd(pr);
1203 if (fd < 0)
1204 return -1;
1205
1206 if (len > sizeof(buf))
1207 len = sizeof(buf);
1208
1209 DBG(LOWPROBE, ul_debug(
1210 "do_wipe [offset=0x%"PRIx64" (%"PRIu64"), len=%zu, chain=%s, idx=%d, dryrun=%s]\n",
1211 offset, offset, len, chn->driver->name, chn->idx, dryrun ? "yes" : "not"));
1212
1213 l = blkid_llseek(fd, offset, SEEK_SET);
1214 if ((blkid_loff_t)l == (off_t) -1)
1215 return -1;
1216
1217 memset(buf, 0, len);
1218
1219 if (!dryrun && len) {
1220 /* wipen on device */
1221 if (write_all(fd, buf, len))
1222 return -1;
1223 fsync(fd);
1224 pr->flags &= ~BLKID_FL_MODIF_BUFF; /* be paranoid */
1225
1226 return blkid_probe_step_back(pr);
1227
1228 } else if (dryrun) {
1229 /* wipe in memory only */
1230 blkid_probe_hide_range(pr, magoff, len);
1231 return blkid_probe_step_back(pr);
1232 }
1233
1234 return 0;
1235 }
1236
1237 /**
1238 * blkid_probe_step_back:
1239 * @pr: prober
1240 *
1241 * This function move pointer to the probing chain one step back -- it means
1242 * that the previously used probing function will be called again in the next
1243 * blkid_do_probe() call.
1244 *
1245 * This is necessary for example if you erase or modify on-disk superblock
1246 * according to the current libblkid probing result.
1247 *
1248 * Note that blkid_probe_hide_range() changes semantic of this function and
1249 * cached bufferes are not reset, but library uses in-memory modified
1250 * buffers to call the next probing function.
1251 *
1252 * <example>
1253 * <title>wipe all superblock, but use libblkid only for probing</title>
1254 * <programlisting>
1255 * pr = blkid_new_probe_from_filename(devname);
1256 *
1257 * blkid_probe_enable_superblocks(pr, 1);
1258 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1259 *
1260 * blkid_probe_enable_partitions(pr, 1);
1261 * blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC);
1262 *
1263 * while (blkid_do_probe(pr) == 0) {
1264 * const char *ostr = NULL;
1265 * size_t len = 0;
1266 *
1267 * // superblocks
1268 * if (blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &ostr, NULL) == 0)
1269 * blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
1270 *
1271 * // partition tables
1272 * if (len == 0 && blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &ostr, NULL) == 0)
1273 * blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
1274 *
1275 * if (!len || !str)
1276 * continue;
1277 *
1278 * // convert ostr to the real offset by off = strtoll(ostr, NULL, 10);
1279 * // use your stuff to erase @len bytes at the @off
1280 * ....
1281 *
1282 * // retry the last probing to check for backup superblocks ..etc.
1283 * blkid_probe_step_back(pr);
1284 * }
1285 * </programlisting>
1286 * </example>
1287 *
1288 * Returns: 0 on success, and -1 in case of error.
1289 */
1290 int blkid_probe_step_back(blkid_probe pr)
1291 {
1292 struct blkid_chain *chn;
1293
1294 chn = pr->cur_chain;
1295 if (!chn)
1296 return -1;
1297
1298 if (!(pr->flags & BLKID_FL_MODIF_BUFF))
1299 blkid_probe_reset_buffers(pr);
1300
1301 if (chn->idx >= 0) {
1302 chn->idx--;
1303 DBG(LOWPROBE, ul_debug("step back: moving %s chain index to %d",
1304 chn->driver->name,
1305 chn->idx));
1306 }
1307
1308 if (chn->idx == -1) {
1309 /* blkid_do_probe() goes to the next chain if the index
1310 * of the current chain is -1, so we have to set the
1311 * chain pointer to the previous chain.
1312 */
1313 size_t idx = chn->driver->id > 0 ? chn->driver->id - 1 : 0;
1314
1315 DBG(LOWPROBE, ul_debug("step back: moving to previous chain"));
1316
1317 if (idx > 0)
1318 pr->cur_chain = &pr->chains[idx];
1319 else if (idx == 0)
1320 pr->cur_chain = NULL;
1321 }
1322
1323 return 0;
1324 }
1325
1326 /**
1327 * blkid_do_safeprobe:
1328 * @pr: prober
1329 *
1330 * This function gathers probing results from all enabled chains and checks
1331 * for ambivalent results (e.g. more filesystems on the device).
1332 *
1333 * This is string-based NAME=value interface only.
1334 *
1335 * Note about superblocks chain -- the function does not check for filesystems
1336 * when a RAID signature is detected. The function also does not check for
1337 * collision between RAIDs. The first detected RAID is returned. The function
1338 * checks for collision between partition table and RAID signature -- it's
1339 * recommended to enable partitions chain together with superblocks chain.
1340 *
1341 * Returns: 0 on success, 1 if nothing is detected, -2 if ambivalent result is
1342 * detected and -1 on case of error.
1343 */
1344 int blkid_do_safeprobe(blkid_probe pr)
1345 {
1346 int i, count = 0, rc = 0;
1347
1348 if (pr->flags & BLKID_FL_NOSCAN_DEV)
1349 return 1;
1350
1351 blkid_probe_start(pr);
1352
1353 for (i = 0; i < BLKID_NCHAINS; i++) {
1354 struct blkid_chain *chn;
1355
1356 chn = pr->cur_chain = &pr->chains[i];
1357 chn->binary = FALSE; /* for sure... */
1358
1359 DBG(LOWPROBE, ul_debug("chain safeprobe %s %s",
1360 chn->driver->name,
1361 chn->enabled? "ENABLED" : "DISABLED"));
1362
1363 if (!chn->enabled)
1364 continue;
1365
1366 blkid_probe_chain_reset_position(chn);
1367
1368 rc = chn->driver->safeprobe(pr, chn);
1369
1370 blkid_probe_chain_reset_position(chn);
1371
1372 /* rc: -2 ambivalent, -1 = error, 0 = success, 1 = no result */
1373 if (rc < 0)
1374 goto done; /* error */
1375 if (rc == 0)
1376 count++; /* success */
1377 }
1378
1379 done:
1380 blkid_probe_end(pr);
1381 if (rc < 0)
1382 return rc;
1383 return count ? 0 : 1;
1384 }
1385
1386 /**
1387 * blkid_do_fullprobe:
1388 * @pr: prober
1389 *
1390 * This function gathers probing results from all enabled chains. Same as
1391 * blkid_do_safeprobe() but does not check for collision between probing
1392 * result.
1393 *
1394 * This is string-based NAME=value interface only.
1395 *
1396 * Returns: 0 on success, 1 if nothing is detected or -1 on case of error.
1397 */
1398 int blkid_do_fullprobe(blkid_probe pr)
1399 {
1400 int i, count = 0, rc = 0;
1401
1402 if (pr->flags & BLKID_FL_NOSCAN_DEV)
1403 return 1;
1404
1405 blkid_probe_start(pr);
1406
1407 for (i = 0; i < BLKID_NCHAINS; i++) {
1408 struct blkid_chain *chn;
1409
1410 chn = pr->cur_chain = &pr->chains[i];
1411 chn->binary = FALSE; /* for sure... */
1412
1413 DBG(LOWPROBE, ul_debug("chain fullprobe %s: %s",
1414 chn->driver->name,
1415 chn->enabled? "ENABLED" : "DISABLED"));
1416
1417 if (!chn->enabled)
1418 continue;
1419
1420 blkid_probe_chain_reset_position(chn);
1421
1422 rc = chn->driver->probe(pr, chn);
1423
1424 blkid_probe_chain_reset_position(chn);
1425
1426 /* rc: -1 = error, 0 = success, 1 = no result */
1427 if (rc < 0)
1428 goto done; /* error */
1429 if (rc == 0)
1430 count++; /* success */
1431 }
1432
1433 done:
1434 blkid_probe_end(pr);
1435 if (rc < 0)
1436 return rc;
1437 return count ? 0 : 1;
1438 }
1439
1440 /* same sa blkid_probe_get_buffer() but works with 512-sectors */
1441 unsigned char *blkid_probe_get_sector(blkid_probe pr, unsigned int sector)
1442 {
1443 return blkid_probe_get_buffer(pr, ((uint64_t) sector) << 9, 0x200);
1444 }
1445
1446 struct blkid_prval *blkid_probe_assign_value(blkid_probe pr, const char *name)
1447 {
1448 struct blkid_prval *v;
1449
1450 v = calloc(1, sizeof(struct blkid_prval));
1451 if (!v)
1452 return NULL;
1453
1454 INIT_LIST_HEAD(&v->prvals);
1455 v->name = name;
1456 v->chain = pr->cur_chain;
1457 list_add_tail(&v->prvals, &pr->values);
1458
1459 DBG(LOWPROBE, ul_debug("assigning %s [%s]", name, v->chain->driver->name));
1460 return v;
1461 }
1462
1463 /* Note that value data is always terminated by zero to keep things robust,
1464 * this extra zero is not count to the value length. It's caller responsibility
1465 * to set proper value length (for strings we count terminator to the length,
1466 * for binary data it's without terminator).
1467 */
1468 int blkid_probe_value_set_data(struct blkid_prval *v,
1469 const unsigned char *data, size_t len)
1470 {
1471 v->data = calloc(1, len + 1); /* always terminate by \0 */
1472
1473 if (!v->data)
1474 return -ENOMEM;
1475 memcpy(v->data, data, len);
1476 v->len = len;
1477 return 0;
1478 }
1479
1480 int blkid_probe_set_value(blkid_probe pr, const char *name,
1481 const unsigned char *data, size_t len)
1482 {
1483 struct blkid_prval *v;
1484
1485 v = blkid_probe_assign_value(pr, name);
1486 if (!v)
1487 return -1;
1488
1489 return blkid_probe_value_set_data(v, data, len);
1490 }
1491
1492 int blkid_probe_vsprintf_value(blkid_probe pr, const char *name,
1493 const char *fmt, va_list ap)
1494 {
1495 struct blkid_prval *v;
1496 ssize_t len;
1497
1498 v = blkid_probe_assign_value(pr, name);
1499 if (!v)
1500 return -ENOMEM;
1501
1502 len = vasprintf((char **) &v->data, fmt, ap);
1503
1504 if (len <= 0) {
1505 blkid_probe_free_value(v);
1506 return len == 0 ? -EINVAL : -ENOMEM;
1507 }
1508 v->len = len + 1;
1509 return 0;
1510 }
1511
1512 int blkid_probe_sprintf_value(blkid_probe pr, const char *name,
1513 const char *fmt, ...)
1514 {
1515 int rc;
1516 va_list ap;
1517
1518 va_start(ap, fmt);
1519 rc = blkid_probe_vsprintf_value(pr, name, fmt, ap);
1520 va_end(ap);
1521
1522 return rc;
1523 }
1524
1525 int blkid_probe_set_magic(blkid_probe pr, uint64_t offset,
1526 size_t len, const unsigned char *magic)
1527 {
1528 int rc = 0;
1529 struct blkid_chain *chn = blkid_probe_get_chain(pr);
1530
1531 if (!chn || !len || chn->binary)
1532 return 0;
1533
1534 switch (chn->driver->id) {
1535 case BLKID_CHAIN_SUBLKS:
1536 if (!(chn->flags & BLKID_SUBLKS_MAGIC))
1537 return 0;
1538 rc = blkid_probe_set_value(pr, "SBMAGIC", magic, len);
1539 if (!rc)
1540 rc = blkid_probe_sprintf_value(pr,
1541 "SBMAGIC_OFFSET", "%llu", (unsigned long long)offset);
1542 break;
1543 case BLKID_CHAIN_PARTS:
1544 if (!(chn->flags & BLKID_PARTS_MAGIC))
1545 return 0;
1546 rc = blkid_probe_set_value(pr, "PTMAGIC", magic, len);
1547 if (!rc)
1548 rc = blkid_probe_sprintf_value(pr,
1549 "PTMAGIC_OFFSET", "%llu", (unsigned long long)offset);
1550 break;
1551 default:
1552 break;
1553 }
1554
1555 return rc;
1556 }
1557
1558 int blkid_probe_verify_csum(blkid_probe pr, uint64_t csum, uint64_t expected)
1559 {
1560 if (csum != expected) {
1561 struct blkid_chain *chn = blkid_probe_get_chain(pr);
1562
1563 DBG(LOWPROBE, ul_debug(
1564 "incorrect checksum for type %s,"
1565 " got %"PRIX64", expected %"PRIX64"",
1566 blkid_probe_get_probername(pr),
1567 csum, expected));
1568 /*
1569 * Accept bad checksum if BLKID_SUBLKS_BADCSUM flags is set
1570 */
1571 if (chn->driver->id == BLKID_CHAIN_SUBLKS
1572 && (chn->flags & BLKID_SUBLKS_BADCSUM)) {
1573 blkid_probe_set_value(pr, "SBBADCSUM", (unsigned char *) "1", 2);
1574 goto accept;
1575 }
1576 return 0; /* bad checksum */
1577 }
1578
1579 accept:
1580 return 1;
1581 }
1582
1583 /**
1584 * blkid_probe_get_devno:
1585 * @pr: probe
1586 *
1587 * Returns: block device number, or 0 for regular files.
1588 */
1589 dev_t blkid_probe_get_devno(blkid_probe pr)
1590 {
1591 return pr->devno;
1592 }
1593
1594 /**
1595 * blkid_probe_get_wholedisk_devno:
1596 * @pr: probe
1597 *
1598 * Returns: device number of the wholedisk, or 0 for regular files.
1599 */
1600 dev_t blkid_probe_get_wholedisk_devno(blkid_probe pr)
1601 {
1602 if (!pr->disk_devno) {
1603 dev_t devno, disk_devno = 0;
1604
1605 devno = blkid_probe_get_devno(pr);
1606 if (!devno)
1607 return 0;
1608
1609 if (blkid_devno_to_wholedisk(devno, NULL, 0, &disk_devno) == 0)
1610 pr->disk_devno = disk_devno;
1611 }
1612 return pr->disk_devno;
1613 }
1614
1615 /**
1616 * blkid_probe_is_wholedisk:
1617 * @pr: probe
1618 *
1619 * Returns: 1 if the device is whole-disk or 0.
1620 */
1621 int blkid_probe_is_wholedisk(blkid_probe pr)
1622 {
1623 dev_t devno, disk_devno;
1624
1625 devno = blkid_probe_get_devno(pr);
1626 if (!devno)
1627 return 0;
1628
1629 disk_devno = blkid_probe_get_wholedisk_devno(pr);
1630 if (!disk_devno)
1631 return 0;
1632
1633 return devno == disk_devno;
1634 }
1635
1636 blkid_probe blkid_probe_get_wholedisk_probe(blkid_probe pr)
1637 {
1638 dev_t disk;
1639
1640 if (blkid_probe_is_wholedisk(pr))
1641 return NULL; /* this is not partition */
1642
1643 if (pr->parent)
1644 /* this is cloned blkid_probe, use parent's stuff */
1645 return blkid_probe_get_wholedisk_probe(pr->parent);
1646
1647 disk = blkid_probe_get_wholedisk_devno(pr);
1648
1649 if (pr->disk_probe && pr->disk_probe->devno != disk) {
1650 /* we have disk prober, but for another disk... close it */
1651 blkid_free_probe(pr->disk_probe);
1652 pr->disk_probe = NULL;
1653 }
1654
1655 if (!pr->disk_probe) {
1656 /* Open a new disk prober */
1657 char *disk_path = blkid_devno_to_devname(disk);
1658
1659 if (!disk_path)
1660 return NULL;
1661
1662 DBG(LOWPROBE, ul_debug("allocate a wholedisk probe"));
1663
1664 pr->disk_probe = blkid_new_probe_from_filename(disk_path);
1665
1666 free(disk_path);
1667
1668 if (!pr->disk_probe)
1669 return NULL; /* ENOMEM? */
1670 }
1671
1672 return pr->disk_probe;
1673 }
1674
1675 /**
1676 * blkid_probe_get_size:
1677 * @pr: probe
1678 *
1679 * This function returns size of probing area as defined by blkid_probe_set_device().
1680 * If the size of the probing area is unrestricted then this function returns
1681 * the real size of device. See also blkid_get_dev_size().
1682 *
1683 * Returns: size in bytes or -1 in case of error.
1684 */
1685 blkid_loff_t blkid_probe_get_size(blkid_probe pr)
1686 {
1687 return (blkid_loff_t) pr->size;
1688 }
1689
1690 /**
1691 * blkid_probe_get_offset:
1692 * @pr: probe
1693 *
1694 * This function returns offset of probing area as defined by blkid_probe_set_device().
1695 *
1696 * Returns: offset in bytes or -1 in case of error.
1697 */
1698 blkid_loff_t blkid_probe_get_offset(blkid_probe pr)
1699 {
1700 return (blkid_loff_t) pr->off;
1701 }
1702
1703 /**
1704 * blkid_probe_get_fd:
1705 * @pr: probe
1706 *
1707 * Returns: file descriptor for assigned device/file or -1 in case of error.
1708 */
1709 int blkid_probe_get_fd(blkid_probe pr)
1710 {
1711 return pr->fd;
1712 }
1713
1714 /**
1715 * blkid_probe_get_sectorsize:
1716 * @pr: probe or NULL (for NULL returns 512)
1717 *
1718 * Returns: block device logical sector size (BLKSSZGET ioctl, default 512).
1719 */
1720 unsigned int blkid_probe_get_sectorsize(blkid_probe pr)
1721 {
1722 if (pr->blkssz)
1723 return pr->blkssz;
1724
1725 if (S_ISBLK(pr->mode) &&
1726 blkdev_get_sector_size(pr->fd, (int *) &pr->blkssz) == 0)
1727 return pr->blkssz;
1728
1729 pr->blkssz = DEFAULT_SECTOR_SIZE;
1730 return pr->blkssz;
1731 }
1732
1733 /**
1734 * blkid_probe_set_sectorsize:
1735 * @pr: probe
1736 * @sz: new size (to overwrite system default)
1737 *
1738 * Note that blkid_probe_set_device() resets this setting. Use it after
1739 * blkid_probe_set_device() and before any probing call.
1740 *
1741 * Since: 2.30
1742 *
1743 * Returns: 0 or <0 in case of error
1744 */
1745 int blkid_probe_set_sectorsize(blkid_probe pr, unsigned int sz)
1746 {
1747 pr->blkssz = sz;
1748 return 0;
1749 }
1750
1751 /**
1752 * blkid_probe_get_sectors:
1753 * @pr: probe
1754 *
1755 * Returns: 512-byte sector count or -1 in case of error.
1756 */
1757 blkid_loff_t blkid_probe_get_sectors(blkid_probe pr)
1758 {
1759 return (blkid_loff_t) (pr->size >> 9);
1760 }
1761
1762 /**
1763 * blkid_probe_numof_values:
1764 * @pr: probe
1765 *
1766 * Returns: number of values in probing result or -1 in case of error.
1767 */
1768 int blkid_probe_numof_values(blkid_probe pr)
1769 {
1770 int i = 0;
1771 struct list_head *p;
1772
1773 list_for_each(p, &pr->values)
1774 ++i;
1775 return i;
1776 }
1777
1778 /**
1779 * blkid_probe_get_value:
1780 * @pr: probe
1781 * @num: wanted value in range 0..N, where N is blkid_probe_numof_values() - 1
1782 * @name: pointer to return value name or NULL
1783 * @data: pointer to return value data or NULL
1784 * @len: pointer to return value length or NULL
1785 *
1786 * Note, the @len returns length of the @data, including the terminating
1787 * '\0' character.
1788 *
1789 * Returns: 0 on success, or -1 in case of error.
1790 */
1791 int blkid_probe_get_value(blkid_probe pr, int num, const char **name,
1792 const char **data, size_t *len)
1793 {
1794 struct blkid_prval *v = __blkid_probe_get_value(pr, num);
1795
1796 if (!v)
1797 return -1;
1798 if (name)
1799 *name = v->name;
1800 if (data)
1801 *data = (char *) v->data;
1802 if (len)
1803 *len = v->len;
1804
1805 DBG(LOWPROBE, ul_debug("returning %s value", v->name));
1806 return 0;
1807 }
1808
1809 /**
1810 * blkid_probe_lookup_value:
1811 * @pr: probe
1812 * @name: name of value
1813 * @data: pointer to return value data or NULL
1814 * @len: pointer to return value length or NULL
1815 *
1816 * Note, the @len returns length of the @data, including the terminating
1817 * '\0' character.
1818 *
1819 * Returns: 0 on success, or -1 in case of error.
1820 */
1821 int blkid_probe_lookup_value(blkid_probe pr, const char *name,
1822 const char **data, size_t *len)
1823 {
1824 struct blkid_prval *v = __blkid_probe_lookup_value(pr, name);
1825
1826 if (!v)
1827 return -1;
1828 if (data)
1829 *data = (char *) v->data;
1830 if (len)
1831 *len = v->len;
1832 return 0;
1833 }
1834
1835 /**
1836 * blkid_probe_has_value:
1837 * @pr: probe
1838 * @name: name of value
1839 *
1840 * Returns: 1 if value exist in probing result, otherwise 0.
1841 */
1842 int blkid_probe_has_value(blkid_probe pr, const char *name)
1843 {
1844 if (blkid_probe_lookup_value(pr, name, NULL, NULL) == 0)
1845 return 1;
1846 return 0;
1847 }
1848
1849 struct blkid_prval *__blkid_probe_get_value(blkid_probe pr, int num)
1850 {
1851 int i = 0;
1852 struct list_head *p;
1853
1854 if (num < 0)
1855 return NULL;
1856
1857 list_for_each(p, &pr->values) {
1858 if (i++ != num)
1859 continue;
1860 return list_entry(p, struct blkid_prval, prvals);
1861 }
1862 return NULL;
1863 }
1864
1865 struct blkid_prval *__blkid_probe_lookup_value(blkid_probe pr, const char *name)
1866 {
1867 struct list_head *p;
1868
1869 if (list_empty(&pr->values))
1870 return NULL;
1871
1872 list_for_each(p, &pr->values) {
1873 struct blkid_prval *v = list_entry(p, struct blkid_prval,
1874 prvals);
1875
1876 if (v->name && strcmp(name, v->name) == 0) {
1877 DBG(LOWPROBE, ul_debug("returning %s value", v->name));
1878 return v;
1879 }
1880 }
1881 return NULL;
1882 }
1883
1884
1885 /* converts DCE UUID (uuid[16]) to human readable string
1886 * - the @len should be always 37 */
1887 void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len)
1888 {
1889 snprintf(str, len,
1890 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1891 uuid[0], uuid[1], uuid[2], uuid[3],
1892 uuid[4], uuid[5],
1893 uuid[6], uuid[7],
1894 uuid[8], uuid[9],
1895 uuid[10], uuid[11], uuid[12], uuid[13], uuid[14],uuid[15]);
1896 }
1897
1898 /* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */
1899 int blkid_uuid_is_empty(const unsigned char *buf, size_t len)
1900 {
1901 size_t i;
1902
1903 for (i = 0; i < len; i++)
1904 if (buf[i])
1905 return 0;
1906 return 1;
1907 }
1908
1909 /* Removes whitespace from the right-hand side of a string (trailing
1910 * whitespace).
1911 *
1912 * Returns size of the new string (without \0).
1913 */
1914 size_t blkid_rtrim_whitespace(unsigned char *str)
1915 {
1916 return rtrim_whitespace(str);
1917 }
1918
1919 /* Removes whitespace from the left-hand side of a string.
1920 *
1921 * Returns size of the new string (without \0).
1922 */
1923 size_t blkid_ltrim_whitespace(unsigned char *str)
1924 {
1925 return ltrim_whitespace(str);
1926 }
1927
1928 /*
1929 * Some mkfs-like utils wipe some parts (usually begin) of the device.
1930 * For example LVM (pvcreate) or mkswap(8). This information could be used
1931 * for later resolution to conflicts between superblocks.
1932 *
1933 * For example we found valid LVM superblock, LVM wipes 8KiB at the begin of
1934 * the device. If we found another signature (for example MBR) within the
1935 * wiped area then the signature has been added later and LVM superblock
1936 * should be ignore.
1937 *
1938 * Note that this heuristic is not 100% reliable, for example "pvcreate --zero
1939 * n" allows to keep the begin of the device unmodified. It's probably better
1940 * to use this heuristic for conflicts between superblocks and partition tables
1941 * than for conflicts between filesystem superblocks -- existence of unwanted
1942 * partition table is very unusual, because PT is pretty visible (parsed and
1943 * interpreted by kernel).
1944 *
1945 * Note that we usually expect only one signature on the device, it means that
1946 * we have to remember only one wiped area from previously successfully
1947 * detected signature.
1948 *
1949 * blkid_probe_set_wiper() -- defines wiped area (e.g. LVM)
1950 * blkid_probe_use_wiper() -- try to use area (e.g. MBR)
1951 *
1952 * Note that there is not relation between _wiper and blkid_to_wipe().
1953 *
1954 */
1955 void blkid_probe_set_wiper(blkid_probe pr, uint64_t off, uint64_t size)
1956 {
1957 struct blkid_chain *chn;
1958
1959 if (!size) {
1960 DBG(LOWPROBE, ul_debug("zeroize wiper"));
1961 pr->wipe_size = pr->wipe_off = 0;
1962 pr->wipe_chain = NULL;
1963 return;
1964 }
1965
1966 chn = pr->cur_chain;
1967
1968 if (!chn || !chn->driver ||
1969 chn->idx < 0 || (size_t) chn->idx >= chn->driver->nidinfos)
1970 return;
1971
1972 pr->wipe_size = size;
1973 pr->wipe_off = off;
1974 pr->wipe_chain = chn;
1975
1976 DBG(LOWPROBE,
1977 ul_debug("wiper set to %s::%s off=%"PRIu64" size=%"PRIu64"",
1978 chn->driver->name,
1979 chn->driver->idinfos[chn->idx]->name,
1980 pr->wipe_off, pr->wipe_size));
1981 return;
1982 }
1983
1984 /*
1985 * Returns 1 if the <@off,@size> area was wiped
1986 */
1987 int blkid_probe_is_wiped(blkid_probe pr, struct blkid_chain **chn, uint64_t off, uint64_t size)
1988 {
1989 if (!size)
1990 return 0;
1991
1992 if (pr->wipe_off <= off && off + size <= pr->wipe_off + pr->wipe_size) {
1993 *chn = pr->wipe_chain;
1994 return 1;
1995 }
1996 return 0;
1997 }
1998
1999 /*
2000 * Try to use any area -- if the area has been previously wiped then the
2001 * previous probing result should be ignored (reset).
2002 */
2003 void blkid_probe_use_wiper(blkid_probe pr, uint64_t off, uint64_t size)
2004 {
2005 struct blkid_chain *chn = NULL;
2006
2007 if (blkid_probe_is_wiped(pr, &chn, off, size) && chn) {
2008 DBG(LOWPROBE, ul_debug("previously wiped area modified "
2009 " -- ignore previous results"));
2010 blkid_probe_set_wiper(pr, 0, 0);
2011 blkid_probe_chain_reset_values(pr, chn);
2012 }
2013 }