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