]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/probe.c
libblkid: fix file descriptor leak in blkid_verify()
[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, resets
850 * the current probing, and close previously associated device (if open by
851 * libblkid).
852 *
853 * If @fd is < 0 than only resets the prober and returns 1. Note that
854 * blkid_reset_probe() keeps the device associated with the prober, but
855 * blkid_probe_set_device() does complete reset.
856 *
857 * Returns: -1 in case of failure, 0 on success and 1 on reset.
858 */
859 int blkid_probe_set_device(blkid_probe pr, int fd,
860 blkid_loff_t off, blkid_loff_t size)
861 {
862 struct stat sb;
863 uint64_t devsiz = 0;
864 char *dm_uuid = NULL;
865
866 blkid_reset_probe(pr);
867 blkid_probe_reset_buffers(pr);
868
869 if ((pr->flags & BLKID_FL_PRIVATE_FD) && pr->fd >= 0)
870 close(pr->fd);
871
872 if (pr->disk_probe) {
873 blkid_free_probe(pr->disk_probe);
874 pr->disk_probe = NULL;
875 }
876
877 pr->flags &= ~BLKID_FL_PRIVATE_FD;
878 pr->flags &= ~BLKID_FL_TINY_DEV;
879 pr->flags &= ~BLKID_FL_CDROM_DEV;
880 pr->prob_flags = 0;
881 pr->fd = fd;
882 pr->off = (uint64_t) off;
883 pr->size = 0;
884 pr->devno = 0;
885 pr->disk_devno = 0;
886 pr->mode = 0;
887 pr->blkssz = 0;
888 pr->wipe_off = 0;
889 pr->wipe_size = 0;
890 pr->wipe_chain = NULL;
891
892 if (fd < 0)
893 return 1;
894
895 #if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE)
896 /* Disable read-ahead */
897 posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
898 #endif
899 if (fstat(fd, &sb))
900 goto err;
901
902 if (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
903 errno = EINVAL;
904 goto err;
905 }
906
907 pr->mode = sb.st_mode;
908 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode))
909 pr->devno = sb.st_rdev;
910
911 if (S_ISBLK(sb.st_mode)) {
912 if (blkdev_get_size(fd, (unsigned long long *) &devsiz)) {
913 DBG(LOWPROBE, ul_debug("failed to get device size"));
914 goto err;
915 }
916 } else if (S_ISCHR(sb.st_mode))
917 devsiz = 1; /* UBI devices are char... */
918 else if (S_ISREG(sb.st_mode))
919 devsiz = sb.st_size; /* regular file */
920
921 pr->size = size ? (uint64_t)size : devsiz;
922
923 if (off && size == 0)
924 /* only offset without size specified */
925 pr->size -= (uint64_t) off;
926
927 if (pr->off + pr->size > devsiz) {
928 DBG(LOWPROBE, ul_debug("area specified by offset and size is bigger than device"));
929 errno = EINVAL;
930 goto err;
931 }
932
933 if (pr->size <= 1440 * 1024 && !S_ISCHR(sb.st_mode))
934 pr->flags |= BLKID_FL_TINY_DEV;
935
936 if (S_ISBLK(sb.st_mode) &&
937 sysfs_devno_is_dm_private(sb.st_rdev, &dm_uuid)) {
938 DBG(LOWPROBE, ul_debug("ignore private device mapper device"));
939 pr->flags |= BLKID_FL_NOSCAN_DEV;
940 }
941
942 #ifdef CDROM_GET_CAPABILITY
943 else if (S_ISBLK(sb.st_mode) &&
944 !blkid_probe_is_tiny(pr) &&
945 !dm_uuid &&
946 blkid_probe_is_wholedisk(pr) &&
947 ioctl(fd, CDROM_GET_CAPABILITY, NULL) >= 0) {
948
949 pr->flags |= BLKID_FL_CDROM_DEV;
950 cdrom_size_correction(pr);
951 }
952 #endif
953 free(dm_uuid);
954
955 DBG(LOWPROBE, ul_debug("ready for low-probing, offset=%"PRIu64", size=%"PRIu64"",
956 pr->off, pr->size));
957 DBG(LOWPROBE, ul_debug("whole-disk: %s, regfile: %s",
958 blkid_probe_is_wholedisk(pr) ?"YES" : "NO",
959 S_ISREG(pr->mode) ? "YES" : "NO"));
960
961 return 0;
962 err:
963 DBG(LOWPROBE, ul_debug("failed to prepare a device for low-probing"));
964 return -1;
965
966 }
967
968 int blkid_probe_get_dimension(blkid_probe pr, uint64_t *off, uint64_t *size)
969 {
970 *off = pr->off;
971 *size = pr->size;
972 return 0;
973 }
974
975 int blkid_probe_set_dimension(blkid_probe pr, uint64_t off, uint64_t size)
976 {
977 DBG(LOWPROBE, ul_debug(
978 "changing probing area: size=%"PRIu64", off=%"PRIu64" "
979 "-to-> size=%"PRIu64", off=%"PRIu64"",
980 pr->size, pr->off, size, off));
981
982 pr->off = off;
983 pr->size = size;
984 pr->flags &= ~BLKID_FL_TINY_DEV;
985
986 if (pr->size <= 1440ULL * 1024ULL && !S_ISCHR(pr->mode))
987 pr->flags |= BLKID_FL_TINY_DEV;
988
989 blkid_probe_reset_buffers(pr);
990
991 return 0;
992 }
993
994 /*
995 * Check for matching magic value.
996 * Returns BLKID_PROBE_OK if found, BLKID_PROBE_NONE if not found
997 * or no magic present, or negative value on error.
998 */
999 int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id,
1000 uint64_t *offset, const struct blkid_idmag **res)
1001 {
1002 const struct blkid_idmag *mag = NULL;
1003 uint64_t off = 0;
1004
1005 if (id)
1006 mag = &id->magics[0];
1007 if (res)
1008 *res = NULL;
1009
1010 /* try to detect by magic string */
1011 while(mag && mag->magic) {
1012 unsigned char *buf;
1013
1014 off = (mag->kboff + (mag->sboff >> 10)) << 10;
1015 buf = blkid_probe_get_buffer(pr, off, 1024);
1016
1017 if (!buf && errno)
1018 return -errno;
1019
1020 if (buf && !memcmp(mag->magic,
1021 buf + (mag->sboff & 0x3ff), mag->len)) {
1022
1023 DBG(LOWPROBE, ul_debug("\tmagic sboff=%u, kboff=%ld",
1024 mag->sboff, mag->kboff));
1025 if (offset)
1026 *offset = off + (mag->sboff & 0x3ff);
1027 if (res)
1028 *res = mag;
1029 return BLKID_PROBE_OK;
1030 }
1031 mag++;
1032 }
1033
1034 if (id && id->magics[0].magic)
1035 /* magic string(s) defined, but not found */
1036 return BLKID_PROBE_NONE;
1037
1038 return BLKID_PROBE_OK;
1039 }
1040
1041 static inline void blkid_probe_start(blkid_probe pr)
1042 {
1043 DBG(LOWPROBE, ul_debug("start probe"));
1044 pr->cur_chain = NULL;
1045 pr->prob_flags = 0;
1046 blkid_probe_set_wiper(pr, 0, 0);
1047 }
1048
1049 static inline void blkid_probe_end(blkid_probe pr)
1050 {
1051 DBG(LOWPROBE, ul_debug("end probe"));
1052 pr->cur_chain = NULL;
1053 pr->prob_flags = 0;
1054 blkid_probe_set_wiper(pr, 0, 0);
1055 }
1056
1057 /**
1058 * blkid_do_probe:
1059 * @pr: prober
1060 *
1061 * Calls probing functions in all enabled chains. The superblocks chain is
1062 * enabled by default. The blkid_do_probe() stores result from only one
1063 * probing function. It's necessary to call this routine in a loop to get
1064 * results from all probing functions in all chains. The probing is reset
1065 * by blkid_reset_probe() or by filter functions.
1066 *
1067 * This is string-based NAME=value interface only.
1068 *
1069 * <example>
1070 * <title>basic case - use the first result only</title>
1071 * <programlisting>
1072 * if (blkid_do_probe(pr) == 0) {
1073 * int nvals = blkid_probe_numof_values(pr);
1074 * for (n = 0; n < nvals; n++) {
1075 * if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0)
1076 * printf("%s = %s\n", name, data);
1077 * }
1078 * }
1079 * </programlisting>
1080 * </example>
1081 *
1082 * <example>
1083 * <title>advanced case - probe for all signatures</title>
1084 * <programlisting>
1085 * while (blkid_do_probe(pr) == 0) {
1086 * int nvals = blkid_probe_numof_values(pr);
1087 * ...
1088 * }
1089 * </programlisting>
1090 * </example>
1091 *
1092 * See also blkid_reset_probe().
1093 *
1094 * Returns: 0 on success, 1 when probing is done and -1 in case of error.
1095 */
1096 int blkid_do_probe(blkid_probe pr)
1097 {
1098 int rc = 1;
1099
1100 if (pr->flags & BLKID_FL_NOSCAN_DEV)
1101 return 1;
1102
1103 do {
1104 struct blkid_chain *chn = pr->cur_chain;
1105
1106 if (!chn) {
1107 blkid_probe_start(pr);
1108 chn = pr->cur_chain = &pr->chains[0];
1109 }
1110 /* we go to the next chain only when the previous probing
1111 * result was nothing (rc == 1) and when the current chain is
1112 * disabled or we are at end of the current chain (chain->idx +
1113 * 1 == sizeof chain) or the current chain bailed out right at
1114 * the start (chain->idx == -1)
1115 */
1116 else if (rc == 1 && (chn->enabled == FALSE ||
1117 chn->idx + 1 == (int) chn->driver->nidinfos ||
1118 chn->idx == -1)) {
1119
1120 size_t idx = chn->driver->id + 1;
1121
1122 if (idx < BLKID_NCHAINS)
1123 chn = pr->cur_chain = &pr->chains[idx];
1124 else {
1125 blkid_probe_end(pr);
1126 return 1; /* all chains already probed */
1127 }
1128 }
1129
1130 chn->binary = FALSE; /* for sure... */
1131
1132 DBG(LOWPROBE, ul_debug("chain probe %s %s (idx=%d)",
1133 chn->driver->name,
1134 chn->enabled? "ENABLED" : "DISABLED",
1135 chn->idx));
1136
1137 if (!chn->enabled)
1138 continue;
1139
1140 /* rc: -1 = error, 0 = success, 1 = no result */
1141 rc = chn->driver->probe(pr, chn);
1142
1143 } while (rc == 1);
1144
1145 return rc;
1146 }
1147
1148 /**
1149 * blkid_do_wipe:
1150 * @pr: prober
1151 * @dryrun: if TRUE then don't touch the device.
1152 *
1153 * This function erases the current signature detected by @pr. The @pr has to
1154 * be open in O_RDWR mode, BLKID_SUBLKS_MAGIC or/and BLKID_PARTS_MAGIC flags
1155 * has to be enabled (if you want to erase also superblock with broken check
1156 * sums then use BLKID_SUBLKS_BADCSUM too).
1157 *
1158 * After successful signature removing the @pr prober will be moved one step
1159 * back and the next blkid_do_probe() call will again call previously called
1160 * probing function. All in-memory cached data from the device are always
1161 * reset.
1162 *
1163 * <example>
1164 * <title>wipe all filesystems or raids from the device</title>
1165 * <programlisting>
1166 * fd = open(devname, O_RDWR|O_CLOEXEC);
1167 * blkid_probe_set_device(pr, fd, 0, 0);
1168 *
1169 * blkid_probe_enable_superblocks(pr, 1);
1170 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1171 *
1172 * while (blkid_do_probe(pr) == 0)
1173 * blkid_do_wipe(pr, FALSE);
1174 * </programlisting>
1175 * </example>
1176 *
1177 * See also blkid_probe_step_back() if you cannot use this built-in wipe
1178 * function, but you want to use libblkid probing as a source for wiping.
1179 *
1180 * Returns: 0 on success, and -1 in case of error.
1181 */
1182 int blkid_do_wipe(blkid_probe pr, int dryrun)
1183 {
1184 const char *off = NULL;
1185 size_t len = 0;
1186 uint64_t offset, magoff, l;
1187 char buf[BUFSIZ];
1188 int fd, rc = 0;
1189 struct blkid_chain *chn;
1190
1191 chn = pr->cur_chain;
1192 if (!chn)
1193 return -1;
1194
1195 switch (chn->driver->id) {
1196 case BLKID_CHAIN_SUBLKS:
1197 rc = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL);
1198 if (!rc)
1199 rc = blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
1200 break;
1201 case BLKID_CHAIN_PARTS:
1202 rc = blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &off, NULL);
1203 if (!rc)
1204 rc = blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
1205 break;
1206 default:
1207 return 0;
1208 }
1209
1210 if (rc || len == 0 || off == NULL)
1211 return 0;
1212
1213 magoff = strtoumax(off, NULL, 10);
1214 offset = magoff + pr->off;
1215 fd = blkid_probe_get_fd(pr);
1216 if (fd < 0)
1217 return -1;
1218
1219 if (len > sizeof(buf))
1220 len = sizeof(buf);
1221
1222 DBG(LOWPROBE, ul_debug(
1223 "do_wipe [offset=0x%"PRIx64" (%"PRIu64"), len=%zu, chain=%s, idx=%d, dryrun=%s]\n",
1224 offset, offset, len, chn->driver->name, chn->idx, dryrun ? "yes" : "not"));
1225
1226 l = blkid_llseek(fd, offset, SEEK_SET);
1227 if ((blkid_loff_t)l == (off_t) -1)
1228 return -1;
1229
1230 memset(buf, 0, len);
1231
1232 if (!dryrun && len) {
1233 /* wipen on device */
1234 if (write_all(fd, buf, len))
1235 return -1;
1236 fsync(fd);
1237 pr->flags &= ~BLKID_FL_MODIF_BUFF; /* be paranoid */
1238
1239 return blkid_probe_step_back(pr);
1240
1241 } else if (dryrun) {
1242 /* wipe in memory only */
1243 blkid_probe_hide_range(pr, magoff, len);
1244 return blkid_probe_step_back(pr);
1245 }
1246
1247 return 0;
1248 }
1249
1250 /**
1251 * blkid_probe_step_back:
1252 * @pr: prober
1253 *
1254 * This function move pointer to the probing chain one step back -- it means
1255 * that the previously used probing function will be called again in the next
1256 * blkid_do_probe() call.
1257 *
1258 * This is necessary for example if you erase or modify on-disk superblock
1259 * according to the current libblkid probing result.
1260 *
1261 * Note that blkid_probe_hide_range() changes semantic of this function and
1262 * cached bufferes are not reset, but library uses in-memory modified
1263 * buffers to call the next probing function.
1264 *
1265 * <example>
1266 * <title>wipe all superblock, but use libblkid only for probing</title>
1267 * <programlisting>
1268 * pr = blkid_new_probe_from_filename(devname);
1269 *
1270 * blkid_probe_enable_superblocks(pr, 1);
1271 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1272 *
1273 * blkid_probe_enable_partitions(pr, 1);
1274 * blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC);
1275 *
1276 * while (blkid_do_probe(pr) == 0) {
1277 * const char *ostr = NULL;
1278 * size_t len = 0;
1279 *
1280 * // superblocks
1281 * if (blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &ostr, NULL) == 0)
1282 * blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
1283 *
1284 * // partition tables
1285 * if (len == 0 && blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &ostr, NULL) == 0)
1286 * blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
1287 *
1288 * if (!len || !str)
1289 * continue;
1290 *
1291 * // convert ostr to the real offset by off = strtoll(ostr, NULL, 10);
1292 * // use your stuff to erase @len bytes at the @off
1293 * ....
1294 *
1295 * // retry the last probing to check for backup superblocks ..etc.
1296 * blkid_probe_step_back(pr);
1297 * }
1298 * </programlisting>
1299 * </example>
1300 *
1301 * Returns: 0 on success, and -1 in case of error.
1302 */
1303 int blkid_probe_step_back(blkid_probe pr)
1304 {
1305 struct blkid_chain *chn;
1306
1307 chn = pr->cur_chain;
1308 if (!chn)
1309 return -1;
1310
1311 if (!(pr->flags & BLKID_FL_MODIF_BUFF))
1312 blkid_probe_reset_buffers(pr);
1313
1314 if (chn->idx >= 0) {
1315 chn->idx--;
1316 DBG(LOWPROBE, ul_debug("step back: moving %s chain index to %d",
1317 chn->driver->name,
1318 chn->idx));
1319 }
1320
1321 if (chn->idx == -1) {
1322 /* blkid_do_probe() goes to the next chain if the index
1323 * of the current chain is -1, so we have to set the
1324 * chain pointer to the previous chain.
1325 */
1326 size_t idx = chn->driver->id > 0 ? chn->driver->id - 1 : 0;
1327
1328 DBG(LOWPROBE, ul_debug("step back: moving to previous chain"));
1329
1330 if (idx > 0)
1331 pr->cur_chain = &pr->chains[idx];
1332 else if (idx == 0)
1333 pr->cur_chain = NULL;
1334 }
1335
1336 return 0;
1337 }
1338
1339 /**
1340 * blkid_do_safeprobe:
1341 * @pr: prober
1342 *
1343 * This function gathers probing results from all enabled chains and checks
1344 * for ambivalent results (e.g. more filesystems on the device).
1345 *
1346 * This is string-based NAME=value interface only.
1347 *
1348 * Note about superblocks chain -- the function does not check for filesystems
1349 * when a RAID signature is detected. The function also does not check for
1350 * collision between RAIDs. The first detected RAID is returned. The function
1351 * checks for collision between partition table and RAID signature -- it's
1352 * recommended to enable partitions chain together with superblocks chain.
1353 *
1354 * Returns: 0 on success, 1 if nothing is detected, -2 if ambivalent result is
1355 * detected and -1 on case of error.
1356 */
1357 int blkid_do_safeprobe(blkid_probe pr)
1358 {
1359 int i, count = 0, rc = 0;
1360
1361 if (pr->flags & BLKID_FL_NOSCAN_DEV)
1362 return 1;
1363
1364 blkid_probe_start(pr);
1365
1366 for (i = 0; i < BLKID_NCHAINS; i++) {
1367 struct blkid_chain *chn;
1368
1369 chn = pr->cur_chain = &pr->chains[i];
1370 chn->binary = FALSE; /* for sure... */
1371
1372 DBG(LOWPROBE, ul_debug("chain safeprobe %s %s",
1373 chn->driver->name,
1374 chn->enabled? "ENABLED" : "DISABLED"));
1375
1376 if (!chn->enabled)
1377 continue;
1378
1379 blkid_probe_chain_reset_position(chn);
1380
1381 rc = chn->driver->safeprobe(pr, chn);
1382
1383 blkid_probe_chain_reset_position(chn);
1384
1385 /* rc: -2 ambivalent, -1 = error, 0 = success, 1 = no result */
1386 if (rc < 0)
1387 goto done; /* error */
1388 if (rc == 0)
1389 count++; /* success */
1390 }
1391
1392 done:
1393 blkid_probe_end(pr);
1394 if (rc < 0)
1395 return rc;
1396 return count ? 0 : 1;
1397 }
1398
1399 /**
1400 * blkid_do_fullprobe:
1401 * @pr: prober
1402 *
1403 * This function gathers probing results from all enabled chains. Same as
1404 * blkid_do_safeprobe() but does not check for collision between probing
1405 * result.
1406 *
1407 * This is string-based NAME=value interface only.
1408 *
1409 * Returns: 0 on success, 1 if nothing is detected or -1 on case of error.
1410 */
1411 int blkid_do_fullprobe(blkid_probe pr)
1412 {
1413 int i, count = 0, rc = 0;
1414
1415 if (pr->flags & BLKID_FL_NOSCAN_DEV)
1416 return 1;
1417
1418 blkid_probe_start(pr);
1419
1420 for (i = 0; i < BLKID_NCHAINS; i++) {
1421 struct blkid_chain *chn;
1422
1423 chn = pr->cur_chain = &pr->chains[i];
1424 chn->binary = FALSE; /* for sure... */
1425
1426 DBG(LOWPROBE, ul_debug("chain fullprobe %s: %s",
1427 chn->driver->name,
1428 chn->enabled? "ENABLED" : "DISABLED"));
1429
1430 if (!chn->enabled)
1431 continue;
1432
1433 blkid_probe_chain_reset_position(chn);
1434
1435 rc = chn->driver->probe(pr, chn);
1436
1437 blkid_probe_chain_reset_position(chn);
1438
1439 /* rc: -1 = error, 0 = success, 1 = no result */
1440 if (rc < 0)
1441 goto done; /* error */
1442 if (rc == 0)
1443 count++; /* success */
1444 }
1445
1446 done:
1447 blkid_probe_end(pr);
1448 if (rc < 0)
1449 return rc;
1450 return count ? 0 : 1;
1451 }
1452
1453 /* same sa blkid_probe_get_buffer() but works with 512-sectors */
1454 unsigned char *blkid_probe_get_sector(blkid_probe pr, unsigned int sector)
1455 {
1456 return blkid_probe_get_buffer(pr, ((uint64_t) sector) << 9, 0x200);
1457 }
1458
1459 struct blkid_prval *blkid_probe_assign_value(blkid_probe pr, const char *name)
1460 {
1461 struct blkid_prval *v;
1462
1463 v = calloc(1, sizeof(struct blkid_prval));
1464 if (!v)
1465 return NULL;
1466
1467 INIT_LIST_HEAD(&v->prvals);
1468 v->name = name;
1469 v->chain = pr->cur_chain;
1470 list_add_tail(&v->prvals, &pr->values);
1471
1472 DBG(LOWPROBE, ul_debug("assigning %s [%s]", name, v->chain->driver->name));
1473 return v;
1474 }
1475
1476 /* Note that value data is always terminated by zero to keep things robust,
1477 * this extra zero is not count to the value length. It's caller responsibility
1478 * to set proper value length (for strings we count terminator to the length,
1479 * for binary data it's without terminator).
1480 */
1481 int blkid_probe_value_set_data(struct blkid_prval *v,
1482 const unsigned char *data, size_t len)
1483 {
1484 v->data = calloc(1, len + 1); /* always terminate by \0 */
1485
1486 if (!v->data)
1487 return -ENOMEM;
1488 memcpy(v->data, data, len);
1489 v->len = len;
1490 return 0;
1491 }
1492
1493 int blkid_probe_set_value(blkid_probe pr, const char *name,
1494 const unsigned char *data, size_t len)
1495 {
1496 struct blkid_prval *v;
1497
1498 v = blkid_probe_assign_value(pr, name);
1499 if (!v)
1500 return -1;
1501
1502 return blkid_probe_value_set_data(v, data, len);
1503 }
1504
1505 int blkid_probe_vsprintf_value(blkid_probe pr, const char *name,
1506 const char *fmt, va_list ap)
1507 {
1508 struct blkid_prval *v;
1509 ssize_t len;
1510
1511 v = blkid_probe_assign_value(pr, name);
1512 if (!v)
1513 return -ENOMEM;
1514
1515 len = vasprintf((char **) &v->data, fmt, ap);
1516
1517 if (len <= 0) {
1518 blkid_probe_free_value(v);
1519 return len == 0 ? -EINVAL : -ENOMEM;
1520 }
1521 v->len = len + 1;
1522 return 0;
1523 }
1524
1525 int blkid_probe_sprintf_value(blkid_probe pr, const char *name,
1526 const char *fmt, ...)
1527 {
1528 int rc;
1529 va_list ap;
1530
1531 va_start(ap, fmt);
1532 rc = blkid_probe_vsprintf_value(pr, name, fmt, ap);
1533 va_end(ap);
1534
1535 return rc;
1536 }
1537
1538 int blkid_probe_set_magic(blkid_probe pr, uint64_t offset,
1539 size_t len, const unsigned char *magic)
1540 {
1541 int rc = 0;
1542 struct blkid_chain *chn = blkid_probe_get_chain(pr);
1543
1544 if (!chn || !len || chn->binary)
1545 return 0;
1546
1547 switch (chn->driver->id) {
1548 case BLKID_CHAIN_SUBLKS:
1549 if (!(chn->flags & BLKID_SUBLKS_MAGIC))
1550 return 0;
1551 rc = blkid_probe_set_value(pr, "SBMAGIC", magic, len);
1552 if (!rc)
1553 rc = blkid_probe_sprintf_value(pr,
1554 "SBMAGIC_OFFSET", "%llu", (unsigned long long)offset);
1555 break;
1556 case BLKID_CHAIN_PARTS:
1557 if (!(chn->flags & BLKID_PARTS_MAGIC))
1558 return 0;
1559 rc = blkid_probe_set_value(pr, "PTMAGIC", magic, len);
1560 if (!rc)
1561 rc = blkid_probe_sprintf_value(pr,
1562 "PTMAGIC_OFFSET", "%llu", (unsigned long long)offset);
1563 break;
1564 default:
1565 break;
1566 }
1567
1568 return rc;
1569 }
1570
1571 int blkid_probe_verify_csum(blkid_probe pr, uint64_t csum, uint64_t expected)
1572 {
1573 if (csum != expected) {
1574 struct blkid_chain *chn = blkid_probe_get_chain(pr);
1575
1576 DBG(LOWPROBE, ul_debug(
1577 "incorrect checksum for type %s,"
1578 " got %"PRIX64", expected %"PRIX64"",
1579 blkid_probe_get_probername(pr),
1580 csum, expected));
1581 /*
1582 * Accept bad checksum if BLKID_SUBLKS_BADCSUM flags is set
1583 */
1584 if (chn->driver->id == BLKID_CHAIN_SUBLKS
1585 && (chn->flags & BLKID_SUBLKS_BADCSUM)) {
1586 blkid_probe_set_value(pr, "SBBADCSUM", (unsigned char *) "1", 2);
1587 goto accept;
1588 }
1589 return 0; /* bad checksum */
1590 }
1591
1592 accept:
1593 return 1;
1594 }
1595
1596 /**
1597 * blkid_probe_get_devno:
1598 * @pr: probe
1599 *
1600 * Returns: block device number, or 0 for regular files.
1601 */
1602 dev_t blkid_probe_get_devno(blkid_probe pr)
1603 {
1604 return pr->devno;
1605 }
1606
1607 /**
1608 * blkid_probe_get_wholedisk_devno:
1609 * @pr: probe
1610 *
1611 * Returns: device number of the wholedisk, or 0 for regular files.
1612 */
1613 dev_t blkid_probe_get_wholedisk_devno(blkid_probe pr)
1614 {
1615 if (!pr->disk_devno) {
1616 dev_t devno, disk_devno = 0;
1617
1618 devno = blkid_probe_get_devno(pr);
1619 if (!devno)
1620 return 0;
1621
1622 if (blkid_devno_to_wholedisk(devno, NULL, 0, &disk_devno) == 0)
1623 pr->disk_devno = disk_devno;
1624 }
1625 return pr->disk_devno;
1626 }
1627
1628 /**
1629 * blkid_probe_is_wholedisk:
1630 * @pr: probe
1631 *
1632 * Returns: 1 if the device is whole-disk or 0.
1633 */
1634 int blkid_probe_is_wholedisk(blkid_probe pr)
1635 {
1636 dev_t devno, disk_devno;
1637
1638 devno = blkid_probe_get_devno(pr);
1639 if (!devno)
1640 return 0;
1641
1642 disk_devno = blkid_probe_get_wholedisk_devno(pr);
1643 if (!disk_devno)
1644 return 0;
1645
1646 return devno == disk_devno;
1647 }
1648
1649 blkid_probe blkid_probe_get_wholedisk_probe(blkid_probe pr)
1650 {
1651 dev_t disk;
1652
1653 if (blkid_probe_is_wholedisk(pr))
1654 return NULL; /* this is not partition */
1655
1656 if (pr->parent)
1657 /* this is cloned blkid_probe, use parent's stuff */
1658 return blkid_probe_get_wholedisk_probe(pr->parent);
1659
1660 disk = blkid_probe_get_wholedisk_devno(pr);
1661
1662 if (pr->disk_probe && pr->disk_probe->devno != disk) {
1663 /* we have disk prober, but for another disk... close it */
1664 blkid_free_probe(pr->disk_probe);
1665 pr->disk_probe = NULL;
1666 }
1667
1668 if (!pr->disk_probe) {
1669 /* Open a new disk prober */
1670 char *disk_path = blkid_devno_to_devname(disk);
1671
1672 if (!disk_path)
1673 return NULL;
1674
1675 DBG(LOWPROBE, ul_debug("allocate a wholedisk probe"));
1676
1677 pr->disk_probe = blkid_new_probe_from_filename(disk_path);
1678
1679 free(disk_path);
1680
1681 if (!pr->disk_probe)
1682 return NULL; /* ENOMEM? */
1683 }
1684
1685 return pr->disk_probe;
1686 }
1687
1688 /**
1689 * blkid_probe_get_size:
1690 * @pr: probe
1691 *
1692 * This function returns size of probing area as defined by blkid_probe_set_device().
1693 * If the size of the probing area is unrestricted then this function returns
1694 * the real size of device. See also blkid_get_dev_size().
1695 *
1696 * Returns: size in bytes or -1 in case of error.
1697 */
1698 blkid_loff_t blkid_probe_get_size(blkid_probe pr)
1699 {
1700 return (blkid_loff_t) pr->size;
1701 }
1702
1703 /**
1704 * blkid_probe_get_offset:
1705 * @pr: probe
1706 *
1707 * This function returns offset of probing area as defined by blkid_probe_set_device().
1708 *
1709 * Returns: offset in bytes or -1 in case of error.
1710 */
1711 blkid_loff_t blkid_probe_get_offset(blkid_probe pr)
1712 {
1713 return (blkid_loff_t) pr->off;
1714 }
1715
1716 /**
1717 * blkid_probe_get_fd:
1718 * @pr: probe
1719 *
1720 * Returns: file descriptor for assigned device/file or -1 in case of error.
1721 */
1722 int blkid_probe_get_fd(blkid_probe pr)
1723 {
1724 return pr->fd;
1725 }
1726
1727 /**
1728 * blkid_probe_get_sectorsize:
1729 * @pr: probe or NULL (for NULL returns 512)
1730 *
1731 * Returns: block device logical sector size (BLKSSZGET ioctl, default 512).
1732 */
1733 unsigned int blkid_probe_get_sectorsize(blkid_probe pr)
1734 {
1735 if (pr->blkssz)
1736 return pr->blkssz;
1737
1738 if (S_ISBLK(pr->mode) &&
1739 blkdev_get_sector_size(pr->fd, (int *) &pr->blkssz) == 0)
1740 return pr->blkssz;
1741
1742 pr->blkssz = DEFAULT_SECTOR_SIZE;
1743 return pr->blkssz;
1744 }
1745
1746 /**
1747 * blkid_probe_set_sectorsize:
1748 * @pr: probe
1749 * @sz: new size (to overwrite system default)
1750 *
1751 * Note that blkid_probe_set_device() resets this setting. Use it after
1752 * blkid_probe_set_device() and before any probing call.
1753 *
1754 * Since: 2.30
1755 *
1756 * Returns: 0 or <0 in case of error
1757 */
1758 int blkid_probe_set_sectorsize(blkid_probe pr, unsigned int sz)
1759 {
1760 pr->blkssz = sz;
1761 return 0;
1762 }
1763
1764 /**
1765 * blkid_probe_get_sectors:
1766 * @pr: probe
1767 *
1768 * Returns: 512-byte sector count or -1 in case of error.
1769 */
1770 blkid_loff_t blkid_probe_get_sectors(blkid_probe pr)
1771 {
1772 return (blkid_loff_t) (pr->size >> 9);
1773 }
1774
1775 /**
1776 * blkid_probe_numof_values:
1777 * @pr: probe
1778 *
1779 * Returns: number of values in probing result or -1 in case of error.
1780 */
1781 int blkid_probe_numof_values(blkid_probe pr)
1782 {
1783 int i = 0;
1784 struct list_head *p;
1785
1786 list_for_each(p, &pr->values)
1787 ++i;
1788 return i;
1789 }
1790
1791 /**
1792 * blkid_probe_get_value:
1793 * @pr: probe
1794 * @num: wanted value in range 0..N, where N is blkid_probe_numof_values() - 1
1795 * @name: pointer to return value name or NULL
1796 * @data: pointer to return value data or NULL
1797 * @len: pointer to return value length or NULL
1798 *
1799 * Note, the @len returns length of the @data, including the terminating
1800 * '\0' character.
1801 *
1802 * Returns: 0 on success, or -1 in case of error.
1803 */
1804 int blkid_probe_get_value(blkid_probe pr, int num, const char **name,
1805 const char **data, size_t *len)
1806 {
1807 struct blkid_prval *v = __blkid_probe_get_value(pr, num);
1808
1809 if (!v)
1810 return -1;
1811 if (name)
1812 *name = v->name;
1813 if (data)
1814 *data = (char *) v->data;
1815 if (len)
1816 *len = v->len;
1817
1818 DBG(LOWPROBE, ul_debug("returning %s value", v->name));
1819 return 0;
1820 }
1821
1822 /**
1823 * blkid_probe_lookup_value:
1824 * @pr: probe
1825 * @name: name of value
1826 * @data: pointer to return value data or NULL
1827 * @len: pointer to return value length or NULL
1828 *
1829 * Note, the @len returns length of the @data, including the terminating
1830 * '\0' character.
1831 *
1832 * Returns: 0 on success, or -1 in case of error.
1833 */
1834 int blkid_probe_lookup_value(blkid_probe pr, const char *name,
1835 const char **data, size_t *len)
1836 {
1837 struct blkid_prval *v = __blkid_probe_lookup_value(pr, name);
1838
1839 if (!v)
1840 return -1;
1841 if (data)
1842 *data = (char *) v->data;
1843 if (len)
1844 *len = v->len;
1845 return 0;
1846 }
1847
1848 /**
1849 * blkid_probe_has_value:
1850 * @pr: probe
1851 * @name: name of value
1852 *
1853 * Returns: 1 if value exist in probing result, otherwise 0.
1854 */
1855 int blkid_probe_has_value(blkid_probe pr, const char *name)
1856 {
1857 if (blkid_probe_lookup_value(pr, name, NULL, NULL) == 0)
1858 return 1;
1859 return 0;
1860 }
1861
1862 struct blkid_prval *__blkid_probe_get_value(blkid_probe pr, int num)
1863 {
1864 int i = 0;
1865 struct list_head *p;
1866
1867 if (num < 0)
1868 return NULL;
1869
1870 list_for_each(p, &pr->values) {
1871 if (i++ != num)
1872 continue;
1873 return list_entry(p, struct blkid_prval, prvals);
1874 }
1875 return NULL;
1876 }
1877
1878 struct blkid_prval *__blkid_probe_lookup_value(blkid_probe pr, const char *name)
1879 {
1880 struct list_head *p;
1881
1882 if (list_empty(&pr->values))
1883 return NULL;
1884
1885 list_for_each(p, &pr->values) {
1886 struct blkid_prval *v = list_entry(p, struct blkid_prval,
1887 prvals);
1888
1889 if (v->name && strcmp(name, v->name) == 0) {
1890 DBG(LOWPROBE, ul_debug("returning %s value", v->name));
1891 return v;
1892 }
1893 }
1894 return NULL;
1895 }
1896
1897
1898 /* converts DCE UUID (uuid[16]) to human readable string
1899 * - the @len should be always 37 */
1900 void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len)
1901 {
1902 snprintf(str, len,
1903 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1904 uuid[0], uuid[1], uuid[2], uuid[3],
1905 uuid[4], uuid[5],
1906 uuid[6], uuid[7],
1907 uuid[8], uuid[9],
1908 uuid[10], uuid[11], uuid[12], uuid[13], uuid[14],uuid[15]);
1909 }
1910
1911 /* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */
1912 int blkid_uuid_is_empty(const unsigned char *buf, size_t len)
1913 {
1914 size_t i;
1915
1916 for (i = 0; i < len; i++)
1917 if (buf[i])
1918 return 0;
1919 return 1;
1920 }
1921
1922 /* Removes whitespace from the right-hand side of a string (trailing
1923 * whitespace).
1924 *
1925 * Returns size of the new string (without \0).
1926 */
1927 size_t blkid_rtrim_whitespace(unsigned char *str)
1928 {
1929 return rtrim_whitespace(str);
1930 }
1931
1932 /* Removes whitespace from the left-hand side of a string.
1933 *
1934 * Returns size of the new string (without \0).
1935 */
1936 size_t blkid_ltrim_whitespace(unsigned char *str)
1937 {
1938 return ltrim_whitespace(str);
1939 }
1940
1941 /*
1942 * Some mkfs-like utils wipe some parts (usually begin) of the device.
1943 * For example LVM (pvcreate) or mkswap(8). This information could be used
1944 * for later resolution to conflicts between superblocks.
1945 *
1946 * For example we found valid LVM superblock, LVM wipes 8KiB at the begin of
1947 * the device. If we found another signature (for example MBR) within the
1948 * wiped area then the signature has been added later and LVM superblock
1949 * should be ignore.
1950 *
1951 * Note that this heuristic is not 100% reliable, for example "pvcreate --zero
1952 * n" allows to keep the begin of the device unmodified. It's probably better
1953 * to use this heuristic for conflicts between superblocks and partition tables
1954 * than for conflicts between filesystem superblocks -- existence of unwanted
1955 * partition table is very unusual, because PT is pretty visible (parsed and
1956 * interpreted by kernel).
1957 *
1958 * Note that we usually expect only one signature on the device, it means that
1959 * we have to remember only one wiped area from previously successfully
1960 * detected signature.
1961 *
1962 * blkid_probe_set_wiper() -- defines wiped area (e.g. LVM)
1963 * blkid_probe_use_wiper() -- try to use area (e.g. MBR)
1964 *
1965 * Note that there is not relation between _wiper and blkid_to_wipe().
1966 *
1967 */
1968 void blkid_probe_set_wiper(blkid_probe pr, uint64_t off, uint64_t size)
1969 {
1970 struct blkid_chain *chn;
1971
1972 if (!size) {
1973 DBG(LOWPROBE, ul_debug("zeroize wiper"));
1974 pr->wipe_size = pr->wipe_off = 0;
1975 pr->wipe_chain = NULL;
1976 return;
1977 }
1978
1979 chn = pr->cur_chain;
1980
1981 if (!chn || !chn->driver ||
1982 chn->idx < 0 || (size_t) chn->idx >= chn->driver->nidinfos)
1983 return;
1984
1985 pr->wipe_size = size;
1986 pr->wipe_off = off;
1987 pr->wipe_chain = chn;
1988
1989 DBG(LOWPROBE,
1990 ul_debug("wiper set to %s::%s off=%"PRIu64" size=%"PRIu64"",
1991 chn->driver->name,
1992 chn->driver->idinfos[chn->idx]->name,
1993 pr->wipe_off, pr->wipe_size));
1994 return;
1995 }
1996
1997 /*
1998 * Returns 1 if the <@off,@size> area was wiped
1999 */
2000 int blkid_probe_is_wiped(blkid_probe pr, struct blkid_chain **chn, uint64_t off, uint64_t size)
2001 {
2002 if (!size)
2003 return 0;
2004
2005 if (pr->wipe_off <= off && off + size <= pr->wipe_off + pr->wipe_size) {
2006 *chn = pr->wipe_chain;
2007 return 1;
2008 }
2009 return 0;
2010 }
2011
2012 /*
2013 * Try to use any area -- if the area has been previously wiped then the
2014 * previous probing result should be ignored (reset).
2015 */
2016 void blkid_probe_use_wiper(blkid_probe pr, uint64_t off, uint64_t size)
2017 {
2018 struct blkid_chain *chn = NULL;
2019
2020 if (blkid_probe_is_wiped(pr, &chn, off, size) && chn) {
2021 DBG(LOWPROBE, ul_debug("previously wiped area modified "
2022 " -- ignore previous results"));
2023 blkid_probe_set_wiper(pr, 0, 0);
2024 blkid_probe_chain_reset_values(pr, chn);
2025 }
2026 }