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