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