]> git.ipfire.org Git - thirdparty/util-linux.git/blame - libblkid/src/probe.c
libblkid: add magic and probing for zoned btrfs
[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 *
707 * Returns: <0 in case of failure, or 0 on success.
708 */
709int blkid_probe_reset_buffers(blkid_probe pr)
a0948ffe 710{
2355dd6a 711 uint64_t ct = 0, len = 0;
15a8fb42 712
d2b0c658
KZ
713 pr->flags &= ~BLKID_FL_MODIF_BUFF;
714
7f787ced 715 if (list_empty(&pr->buffers))
d2b0c658 716 return 0;
15a8fb42 717
63c9c05d 718 DBG(BUFFER, ul_debug("Resetting probing buffers"));
15a8fb42
KZ
719
720 while (!list_empty(&pr->buffers)) {
721 struct blkid_bufinfo *bf = list_entry(pr->buffers.next,
722 struct blkid_bufinfo, bufs);
2355dd6a
KZ
723 ct++;
724 len += bf->len;
15a8fb42 725 list_del(&bf->bufs);
a674a0ab 726
63c9c05d
KZ
727 DBG(BUFFER, ul_debug(" remove buffer: [off=%"PRIu64", len=%"PRIu64"]",
728 bf->off, bf->len));
15a8fb42 729 free(bf);
4884729a 730 }
15a8fb42 731
e04f3860 732 DBG(LOWPROBE, ul_debug(" buffers summary: %"PRIu64" bytes by %"PRIu64" read() calls",
2355dd6a 733 len, ct));
15a8fb42
KZ
734
735 INIT_LIST_HEAD(&pr->buffers);
d2b0c658
KZ
736
737 return 0;
738}
739
740/**
741 * blkid_probe_hide_range:
742 * @pr: prober
743 * @off: start of the range
744 * @len: size of the range
745 *
746 * This function modifies in-memory cached data from the device. The specified
747 * range is zeroized. This is usable together with blkid_probe_step_back().
748 * The next blkid_do_probe() will not see specified area.
749 *
750 * Note that this is usable for already (by library) read data, and this
751 * function is not a way how to hide any large areas on your device.
752 *
753 * The function blkid_probe_reset_buffers() reverts all.
754 *
755 * Returns: <0 in case of failure, or 0 on success.
756 */
757int blkid_probe_hide_range(blkid_probe pr, uint64_t off, uint64_t len)
758{
759 int rc = hide_buffer(pr, off, len);
760
761 if (rc == 0)
762 pr->flags |= BLKID_FL_MODIF_BUFF;
763 return rc;
a0948ffe
KZ
764}
765
67719fbb 766
af17d349 767static void blkid_probe_reset_values(blkid_probe pr)
6c4a7811 768{
7f787ced 769 if (list_empty(&pr->values))
6c4a7811
OO
770 return;
771
63c9c05d 772 DBG(LOWPROBE, ul_debug("resetting results"));
6c4a7811 773
af17d349
KZ
774 while (!list_empty(&pr->values)) {
775 struct blkid_prval *v = list_entry(pr->values.next,
6c4a7811 776 struct blkid_prval, prvals);
af17d349 777 blkid_probe_free_value(v);
6c4a7811
OO
778 }
779
af17d349 780 INIT_LIST_HEAD(&pr->values);
6c4a7811
OO
781}
782
108013b4
KZ
783/*
784 * Small devices need a special care.
785 */
786int blkid_probe_is_tiny(blkid_probe pr)
787{
7f787ced 788 return (pr->flags & BLKID_FL_TINY_DEV);
108013b4
KZ
789}
790
55113b15
C
791/*
792 * CDROMs may fail when probed for RAID (last sector problem)
793 */
794int blkid_probe_is_cdrom(blkid_probe pr)
795{
7f787ced 796 return (pr->flags & BLKID_FL_CDROM_DEV);
55113b15
C
797}
798
332123f2
RM
799#ifdef CDROM_GET_CAPABILITY
800
bfebe74e
KZ
801static int is_sector_readable(int fd, uint64_t sector)
802{
803 char buf[512];
804 ssize_t sz;
805
a732e4a1 806 if (lseek(fd, sector * 512, SEEK_SET) == (off_t) -1)
bfebe74e
KZ
807 goto failed;
808
809 sz = read(fd, buf, sizeof(buf));
810 if (sz != (ssize_t) sizeof(buf))
811 goto failed;
812
813 return 1;
814failed:
eaaf0e7e 815 DBG(LOWPROBE, ul_debug("CDROM: read sector %"PRIu64" failed %m", sector));
bfebe74e
KZ
816 errno = 0;
817 return 0;
818}
819
820/*
6548ac6a
KZ
821 * Linux kernel reports (BLKGETSIZE) cdrom device size greater than area
822 * readable by read(2). We have to reduce the probing area to avoid unwanted
823 * I/O errors in probing functions. It seems that unreadable are always last 2
824 * or 3 CD blocks (CD block size is 2048 bytes, it means 12 in 512-byte
bfd4da56
PR
825 * sectors). Linux kernel reports (CDROM_LAST_WRITTEN) also location of last
826 * written block, so we will reduce size based on it too.
bfebe74e 827 */
bfd4da56 828static void cdrom_size_correction(blkid_probe pr, uint64_t last_written)
bfebe74e 829{
6548ac6a 830 uint64_t n, nsectors = pr->size >> 9;
bfebe74e 831
bfd4da56
PR
832 if (last_written && nsectors > ((last_written+1) << 2))
833 nsectors = (last_written+1) << 2;
834
bfebe74e
KZ
835 for (n = nsectors - 12; n < nsectors; n++) {
836 if (!is_sector_readable(pr->fd, n))
837 goto failed;
838 }
839
840 DBG(LOWPROBE, ul_debug("CDROM: full size available"));
841 return;
842failed:
843 /* 'n' is the failed sector, reduce device size to n-1; */
844 DBG(LOWPROBE, ul_debug("CDROM: reduce size from %ju to %ju.",
845 (uintmax_t) pr->size,
1bd62f72 846 (uintmax_t) n << 9));
bfebe74e
KZ
847 pr->size = n << 9;
848}
849
332123f2
RM
850#endif
851
52448df8
KZ
852/**
853 * blkid_probe_set_device:
854 * @pr: probe
855 * @fd: device file descriptor
856 * @off: begin of probing area
f38db0cf 857 * @size: size of probing area (zero means whole device/file)
52448df8 858 *
c4d6d1c5
KZ
859 * Assigns the device to probe control struct, resets internal buffers, resets
860 * the current probing, and close previously associated device (if open by
861 * libblkid).
51410fc6 862 *
c4d6d1c5
KZ
863 * If @fd is < 0 than only resets the prober and returns 1. Note that
864 * blkid_reset_probe() keeps the device associated with the prober, but
865 * blkid_probe_set_device() does complete reset.
866 *
867 * Returns: -1 in case of failure, 0 on success and 1 on reset.
51410fc6
KZ
868 */
869int blkid_probe_set_device(blkid_probe pr, int fd,
870 blkid_loff_t off, blkid_loff_t size)
a0948ffe 871{
90ec8d9c 872 struct stat sb;
f12cd8d1 873 uint64_t devsiz = 0;
884659b3 874 char *dm_uuid = NULL;
6b88a410
PR
875#ifdef CDROM_GET_CAPABILITY
876 long last_written = 0;
877#endif
90ec8d9c 878
51410fc6 879 blkid_reset_probe(pr);
d2b0c658 880 blkid_probe_reset_buffers(pr);
a0948ffe 881
a9eef56c 882 if ((pr->flags & BLKID_FL_PRIVATE_FD) && pr->fd >= 0)
f38db0cf
KZ
883 close(pr->fd);
884
c4d6d1c5
KZ
885 if (pr->disk_probe) {
886 blkid_free_probe(pr->disk_probe);
887 pr->disk_probe = NULL;
888 }
889
a9eef56c
KZ
890 pr->flags &= ~BLKID_FL_PRIVATE_FD;
891 pr->flags &= ~BLKID_FL_TINY_DEV;
892 pr->flags &= ~BLKID_FL_CDROM_DEV;
ccdf9fda 893 pr->prob_flags = 0;
51410fc6 894 pr->fd = fd;
f12cd8d1 895 pr->off = (uint64_t) off;
bb6c6673 896 pr->size = 0;
52448df8 897 pr->devno = 0;
ccdf9fda 898 pr->disk_devno = 0;
52448df8
KZ
899 pr->mode = 0;
900 pr->blkssz = 0;
ccdf9fda
KZ
901 pr->wipe_off = 0;
902 pr->wipe_size = 0;
903 pr->wipe_chain = NULL;
b250fbbf 904 pr->zone_size = 0;
dc61d909 905
c4d6d1c5
KZ
906 if (fd < 0)
907 return 1;
908
a67bb3bf
LT
909#if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE)
910 /* Disable read-ahead */
911 posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
912#endif
90ec8d9c
KZ
913 if (fstat(fd, &sb))
914 goto err;
915
a674a0ab 916 if (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
2355dd6a 917 errno = EINVAL;
26eb5a59 918 goto err;
a674a0ab 919 }
20e1c3dc 920
90ec8d9c
KZ
921 pr->mode = sb.st_mode;
922 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode))
923 pr->devno = sb.st_rdev;
924
a674a0ab
KZ
925 if (S_ISBLK(sb.st_mode)) {
926 if (blkdev_get_size(fd, (unsigned long long *) &devsiz)) {
927 DBG(LOWPROBE, ul_debug("failed to get device size"));
108013b4 928 goto err;
a674a0ab
KZ
929 }
930 } else if (S_ISCHR(sb.st_mode))
931 devsiz = 1; /* UBI devices are char... */
932 else if (S_ISREG(sb.st_mode))
933 devsiz = sb.st_size; /* regular file */
934
b9710f1f 935 pr->size = size ? (uint64_t)size : devsiz;
108013b4 936
a674a0ab
KZ
937 if (off && size == 0)
938 /* only offset without size specified */
f12cd8d1 939 pr->size -= (uint64_t) off;
a674a0ab
KZ
940
941 if (pr->off + pr->size > devsiz) {
942 DBG(LOWPROBE, ul_debug("area specified by offset and size is bigger than device"));
943 errno = EINVAL;
944 goto err;
bb6c6673 945 }
c1ba7962 946
90ec8d9c 947 if (pr->size <= 1440 * 1024 && !S_ISCHR(sb.st_mode))
a9eef56c 948 pr->flags |= BLKID_FL_TINY_DEV;
d0465c3c 949
884659b3 950 if (S_ISBLK(sb.st_mode) &&
80ec018c
TA
951 sysfs_devno_is_dm_private(sb.st_rdev, &dm_uuid)) {
952 DBG(LOWPROBE, ul_debug("ignore private device mapper device"));
20e1c3dc
KZ
953 pr->flags |= BLKID_FL_NOSCAN_DEV;
954 }
955
55113b15 956#ifdef CDROM_GET_CAPABILITY
20e1c3dc 957 else if (S_ISBLK(sb.st_mode) &&
a3ab71cf 958 !blkid_probe_is_tiny(pr) &&
884659b3 959 !dm_uuid &&
6b88a410
PR
960 blkid_probe_is_wholedisk(pr)) {
961
3d725afa 962 /*
6b88a410
PR
963 * pktcdvd.ko accepts only these ioctls:
964 * CDROMEJECT CDROMMULTISESSION CDROMREADTOCENTRY
965 * CDROM_LAST_WRITTEN CDROM_SEND_PACKET SCSI_IOCTL_SEND_COMMAND
966 * So CDROM_GET_CAPABILITY cannot be used for detecting pktcdvd
967 * devices. But CDROM_GET_CAPABILITY and CDROM_DRIVE_STATUS are
968 * fast so use them for detecting if medium is present. In any
969 * case use last written block form CDROM_LAST_WRITTEN.
970 */
bfebe74e 971
6b88a410 972 if (ioctl(fd, CDROM_GET_CAPABILITY, NULL) >= 0) {
dc30fd43 973# ifdef CDROM_DRIVE_STATUS
6b88a410
PR
974 switch (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT)) {
975 case CDS_TRAY_OPEN:
976 case CDS_NO_DISC:
977 errno = ENOMEDIUM;
978 goto err;
979 }
980# endif
981 pr->flags |= BLKID_FL_CDROM_DEV;
dc30fd43 982 }
6b88a410
PR
983
984# ifdef CDROM_LAST_WRITTEN
985 if (ioctl(fd, CDROM_LAST_WRITTEN, &last_written) == 0)
986 pr->flags |= BLKID_FL_CDROM_DEV;
dc30fd43 987# endif
6b88a410
PR
988
989 if (pr->flags & BLKID_FL_CDROM_DEV) {
bfd4da56 990 cdrom_size_correction(pr, last_written);
427ea355
PR
991
992# ifdef CDROMMULTISESSION
993 if (!pr->off && blkid_probe_get_hint(pr, "session_offset", NULL) < 0) {
994 struct cdrom_multisession multisession = { .addr_format = CDROM_LBA };
995 if (ioctl(fd, CDROMMULTISESSION, &multisession) == 0 && multisession.xa_flag)
996 blkid_probe_set_hint(pr, "session_offset", (multisession.addr.lba << 11));
997 }
998# endif
6b88a410 999 }
bfebe74e 1000 }
55113b15 1001#endif
884659b3 1002 free(dm_uuid);
508e438b 1003
b250fbbf
NA
1004# ifdef HAVE_LINUX_BLKZONED_H
1005 if (S_ISBLK(sb.st_mode)) {
1006 uint32_t zone_size_sector;
1007
1008 if (!ioctl(pr->fd, BLKGETZONESZ, &zone_size_sector))
1009 pr->zone_size = zone_size_sector << 9;
1010 }
1011# endif
1012
fdbd7bb9 1013 DBG(LOWPROBE, ul_debug("ready for low-probing, offset=%"PRIu64", size=%"PRIu64"",
508e438b 1014 pr->off, pr->size));
c62a6311 1015 DBG(LOWPROBE, ul_debug("whole-disk: %s, regfile: %s",
508e438b
KZ
1016 blkid_probe_is_wholedisk(pr) ?"YES" : "NO",
1017 S_ISREG(pr->mode) ? "YES" : "NO"));
1018
a0948ffe 1019 return 0;
0d17b1cf 1020err:
c62a6311 1021 DBG(LOWPROBE, ul_debug("failed to prepare a device for low-probing"));
0d17b1cf
KZ
1022 return -1;
1023
a0948ffe
KZ
1024}
1025
f12cd8d1 1026int blkid_probe_get_dimension(blkid_probe pr, uint64_t *off, uint64_t *size)
f319b5ca 1027{
f319b5ca
KZ
1028 *off = pr->off;
1029 *size = pr->size;
1030 return 0;
1031}
1032
f12cd8d1 1033int blkid_probe_set_dimension(blkid_probe pr, uint64_t off, uint64_t size)
f319b5ca 1034{
c62a6311 1035 DBG(LOWPROBE, ul_debug(
63c9c05d 1036 "changing probing area: size=%"PRIu64", off=%"PRIu64" "
fdbd7bb9 1037 "-to-> size=%"PRIu64", off=%"PRIu64"",
63c9c05d 1038 pr->size, pr->off, size, off));
f319b5ca
KZ
1039
1040 pr->off = off;
1041 pr->size = size;
a9eef56c 1042 pr->flags &= ~BLKID_FL_TINY_DEV;
88923b08 1043
f12cd8d1 1044 if (pr->size <= 1440ULL * 1024ULL && !S_ISCHR(pr->mode))
a9eef56c 1045 pr->flags |= BLKID_FL_TINY_DEV;
f319b5ca 1046
d2b0c658 1047 blkid_probe_reset_buffers(pr);
f319b5ca
KZ
1048
1049 return 0;
1050}
1051
b5c5b420
PR
1052unsigned char *_blkid_probe_get_sb(blkid_probe pr, const struct blkid_idmag *mag, size_t size)
1053{
1054 uint64_t hint_offset;
1055
1056 if (!mag->hoff || blkid_probe_get_hint(pr, mag->hoff, &hint_offset) < 0)
1057 hint_offset = 0;
1058
1059 return blkid_probe_get_buffer(pr, hint_offset + (mag->kboff << 10), size);
1060}
1061
8d0ce083 1062/*
296d96e2
HR
1063 * Check for matching magic value.
1064 * Returns BLKID_PROBE_OK if found, BLKID_PROBE_NONE if not found
1065 * or no magic present, or negative value on error.
1066 */
c76e710b 1067int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id,
f12cd8d1 1068 uint64_t *offset, const struct blkid_idmag **res)
c76e710b
KZ
1069{
1070 const struct blkid_idmag *mag = NULL;
f12cd8d1 1071 uint64_t off = 0;
c76e710b
KZ
1072
1073 if (id)
c03d12d8 1074 mag = &id->magics[0];
c76e710b
KZ
1075 if (res)
1076 *res = NULL;
1077
1078 /* try to detect by magic string */
1079 while(mag && mag->magic) {
1080 unsigned char *buf;
b250fbbf 1081 uint64_t kboff;
3e31657d 1082 uint64_t hint_offset;
c76e710b 1083
3e31657d
PR
1084 if (!mag->hoff || blkid_probe_get_hint(pr, mag->hoff, &hint_offset) < 0)
1085 hint_offset = 0;
c76e710b 1086
b250fbbf
NA
1087 /* If the magic is for zoned device, skip non-zoned device */
1088 if (mag->is_zoned && !pr->zone_size) {
1089 mag++;
1090 continue;
1091 }
1092
1093 if (!mag->is_zoned)
1094 kboff = mag->kboff;
1095 else
1096 kboff = ((mag->zonenum * pr->zone_size) >> 10) + mag->kboff_inzone;
1097
1098 off = hint_offset + ((kboff + (mag->sboff >> 10)) << 10);
c76e710b
KZ
1099 buf = blkid_probe_get_buffer(pr, off, 1024);
1100
296d96e2
HR
1101 if (!buf && errno)
1102 return -errno;
a674a0ab 1103
c76e710b
KZ
1104 if (buf && !memcmp(mag->magic,
1105 buf + (mag->sboff & 0x3ff), mag->len)) {
a674a0ab 1106
c62a6311 1107 DBG(LOWPROBE, ul_debug("\tmagic sboff=%u, kboff=%ld",
b250fbbf 1108 mag->sboff, kboff));
c76e710b
KZ
1109 if (offset)
1110 *offset = off + (mag->sboff & 0x3ff);
1111 if (res)
1112 *res = mag;
296d96e2 1113 return BLKID_PROBE_OK;
c76e710b
KZ
1114 }
1115 mag++;
1116 }
1117
c03d12d8 1118 if (id && id->magics[0].magic)
c76e710b 1119 /* magic string(s) defined, but not found */
296d96e2 1120 return BLKID_PROBE_NONE;
c76e710b 1121
296d96e2 1122 return BLKID_PROBE_OK;
c76e710b
KZ
1123}
1124
c81e7008
KZ
1125static inline void blkid_probe_start(blkid_probe pr)
1126{
63c9c05d 1127 DBG(LOWPROBE, ul_debug("start probe"));
7f787ced
KZ
1128 pr->cur_chain = NULL;
1129 pr->prob_flags = 0;
1130 blkid_probe_set_wiper(pr, 0, 0);
c81e7008
KZ
1131}
1132
1133static inline void blkid_probe_end(blkid_probe pr)
1134{
63c9c05d 1135 DBG(LOWPROBE, ul_debug("end probe"));
7f787ced
KZ
1136 pr->cur_chain = NULL;
1137 pr->prob_flags = 0;
1138 blkid_probe_set_wiper(pr, 0, 0);
c81e7008
KZ
1139}
1140
0bffad47
KZ
1141/**
1142 * blkid_do_probe:
1143 * @pr: prober
1144 *
1145 * Calls probing functions in all enabled chains. The superblocks chain is
1146 * enabled by default. The blkid_do_probe() stores result from only one
1147 * probing function. It's necessary to call this routine in a loop to get
3b159691 1148 * results from all probing functions in all chains. The probing is reset
44ef90bc 1149 * by blkid_reset_probe() or by filter functions.
a0fc685c 1150 *
0bffad47
KZ
1151 * This is string-based NAME=value interface only.
1152 *
1153 * <example>
1154 * <title>basic case - use the first result only</title>
1155 * <programlisting>
a0fc685c
KZ
1156 * if (blkid_do_probe(pr) == 0) {
1157 * int nvals = blkid_probe_numof_values(pr);
1158 * for (n = 0; n < nvals; n++) {
1159 * if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0)
1160 * printf("%s = %s\n", name, data);
1161 * }
1162 * }
0bffad47
KZ
1163 * </programlisting>
1164 * </example>
a0fc685c 1165 *
0bffad47
KZ
1166 * <example>
1167 * <title>advanced case - probe for all signatures</title>
1168 * <programlisting>
a0fc685c
KZ
1169 * while (blkid_do_probe(pr) == 0) {
1170 * int nvals = blkid_probe_numof_values(pr);
1171 * ...
1172 * }
0bffad47
KZ
1173 * </programlisting>
1174 * </example>
a0fc685c 1175 *
0bffad47 1176 * See also blkid_reset_probe().
a0fc685c 1177 *
0bffad47 1178 * Returns: 0 on success, 1 when probing is done and -1 in case of error.
a0fc685c 1179 */
51410fc6 1180int blkid_do_probe(blkid_probe pr)
a0948ffe 1181{
0bffad47 1182 int rc = 1;
a0948ffe 1183
20e1c3dc
KZ
1184 if (pr->flags & BLKID_FL_NOSCAN_DEV)
1185 return 1;
1186
0bffad47 1187 do {
44ef90bc
KZ
1188 struct blkid_chain *chn = pr->cur_chain;
1189
c81e7008
KZ
1190 if (!chn) {
1191 blkid_probe_start(pr);
44ef90bc 1192 chn = pr->cur_chain = &pr->chains[0];
c81e7008 1193 }
44ef90bc
KZ
1194 /* we go to the next chain only when the previous probing
1195 * result was nothing (rc == 1) and when the current chain is
1196 * disabled or we are at end of the current chain (chain->idx +
046959cc
CW
1197 * 1 == sizeof chain) or the current chain bailed out right at
1198 * the start (chain->idx == -1)
44ef90bc
KZ
1199 */
1200 else if (rc == 1 && (chn->enabled == FALSE ||
c9f51c71 1201 chn->idx + 1 == (int) chn->driver->nidinfos ||
046959cc 1202 chn->idx == -1)) {
a0948ffe 1203
c9f51c71 1204 size_t idx = chn->driver->id + 1;
bd635f86
KZ
1205
1206 if (idx < BLKID_NCHAINS)
44ef90bc 1207 chn = pr->cur_chain = &pr->chains[idx];
c81e7008
KZ
1208 else {
1209 blkid_probe_end(pr);
bd635f86 1210 return 1; /* all chains already probed */
c81e7008 1211 }
bd635f86 1212 }
a0fc685c 1213
0bffad47 1214 chn->binary = FALSE; /* for sure... */
6644688a 1215
c62a6311 1216 DBG(LOWPROBE, ul_debug("chain probe %s %s (idx=%d)",
0bffad47 1217 chn->driver->name,
44ef90bc
KZ
1218 chn->enabled? "ENABLED" : "DISABLED",
1219 chn->idx));
a0948ffe 1220
0bffad47 1221 if (!chn->enabled)
51410fc6 1222 continue;
a0948ffe 1223
0bffad47
KZ
1224 /* rc: -1 = error, 0 = success, 1 = no result */
1225 rc = chn->driver->probe(pr, chn);
a0948ffe 1226
0bffad47 1227 } while (rc == 1);
a0948ffe 1228
0bffad47
KZ
1229 return rc;
1230}
a0948ffe 1231
2b89be6c
KZ
1232/**
1233 * blkid_do_wipe:
1234 * @pr: prober
1235 * @dryrun: if TRUE then don't touch the device.
1236 *
1237 * This function erases the current signature detected by @pr. The @pr has to
476b508e 1238 * be open in O_RDWR mode, BLKID_SUBLKS_MAGIC or/and BLKID_PARTS_MAGIC flags
9e930041 1239 * has to be enabled (if you want to erase also superblock with broken check
c93c2030 1240 * sums then use BLKID_SUBLKS_BADCSUM too).
2b89be6c
KZ
1241 *
1242 * After successful signature removing the @pr prober will be moved one step
1243 * back and the next blkid_do_probe() call will again call previously called
d2b0c658 1244 * probing function. All in-memory cached data from the device are always
73afd3f8 1245 * reset.
2b89be6c
KZ
1246 *
1247 * <example>
1248 * <title>wipe all filesystems or raids from the device</title>
1249 * <programlisting>
49a8f58e 1250 * fd = open(devname, O_RDWR|O_CLOEXEC);
2b89be6c
KZ
1251 * blkid_probe_set_device(pr, fd, 0, 0);
1252 *
1253 * blkid_probe_enable_superblocks(pr, 1);
1254 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1255 *
1256 * while (blkid_do_probe(pr) == 0)
1257 * blkid_do_wipe(pr, FALSE);
1258 * </programlisting>
1259 * </example>
1260 *
11026083 1261 * See also blkid_probe_step_back() if you cannot use this built-in wipe
cd0fe5c1
KZ
1262 * function, but you want to use libblkid probing as a source for wiping.
1263 *
1264 * Returns: 0 on success, and -1 in case of error.
2b89be6c
KZ
1265 */
1266int blkid_do_wipe(blkid_probe pr, int dryrun)
1267{
44765fdd 1268 const char *off = NULL;
2b89be6c 1269 size_t len = 0;
a732e4a1 1270 uint64_t offset, magoff;
2b89be6c 1271 char buf[BUFSIZ];
f8054232 1272 int fd, rc = 0;
2b89be6c
KZ
1273 struct blkid_chain *chn;
1274
2b89be6c
KZ
1275 chn = pr->cur_chain;
1276 if (!chn)
1277 return -1;
1278
44765fdd
KZ
1279 switch (chn->driver->id) {
1280 case BLKID_CHAIN_SUBLKS:
1281 rc = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL);
1282 if (!rc)
1283 rc = blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
1284 break;
1285 case BLKID_CHAIN_PARTS:
1286 rc = blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &off, NULL);
1287 if (!rc)
1288 rc = blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
1289 break;
1290 default:
1291 return 0;
1292 }
1293
1294 if (rc || len == 0 || off == NULL)
2b89be6c
KZ
1295 return 0;
1296
d2b0c658
KZ
1297 magoff = strtoumax(off, NULL, 10);
1298 offset = magoff + pr->off;
2b89be6c
KZ
1299 fd = blkid_probe_get_fd(pr);
1300 if (fd < 0)
1301 return -1;
1302
1303 if (len > sizeof(buf))
1304 len = sizeof(buf);
1305
c62a6311 1306 DBG(LOWPROBE, ul_debug(
fdbd7bb9 1307 "do_wipe [offset=0x%"PRIx64" (%"PRIu64"), len=%zu, chain=%s, idx=%d, dryrun=%s]\n",
f12cd8d1 1308 offset, offset, len, chn->driver->name, chn->idx, dryrun ? "yes" : "not"));
2b89be6c 1309
a732e4a1 1310 if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
2b89be6c
KZ
1311 return -1;
1312
1313 memset(buf, 0, len);
1314
1315 if (!dryrun && len) {
d2b0c658 1316 /* wipen on device */
2b89be6c
KZ
1317 if (write_all(fd, buf, len))
1318 return -1;
1319 fsync(fd);
d2b0c658
KZ
1320 pr->flags &= ~BLKID_FL_MODIF_BUFF; /* be paranoid */
1321
1322 return blkid_probe_step_back(pr);
1323
042f62df
RP
1324 }
1325
1326 if (dryrun) {
d2b0c658
KZ
1327 /* wipe in memory only */
1328 blkid_probe_hide_range(pr, magoff, len);
cd0fe5c1
KZ
1329 return blkid_probe_step_back(pr);
1330 }
2b89be6c 1331
cd0fe5c1
KZ
1332 return 0;
1333}
2b89be6c 1334
cd0fe5c1 1335/**
c0055e2a 1336 * blkid_probe_step_back:
cd0fe5c1
KZ
1337 * @pr: prober
1338 *
1339 * This function move pointer to the probing chain one step back -- it means
1340 * that the previously used probing function will be called again in the next
1341 * blkid_do_probe() call.
1342 *
1343 * This is necessary for example if you erase or modify on-disk superblock
1344 * according to the current libblkid probing result.
1345 *
d2b0c658 1346 * Note that blkid_probe_hide_range() changes semantic of this function and
311e33af 1347 * cached buffers are not reset, but library uses in-memory modified
d2b0c658
KZ
1348 * buffers to call the next probing function.
1349 *
cd0fe5c1
KZ
1350 * <example>
1351 * <title>wipe all superblock, but use libblkid only for probing</title>
1352 * <programlisting>
1353 * pr = blkid_new_probe_from_filename(devname);
1354 *
1355 * blkid_probe_enable_superblocks(pr, 1);
1356 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
1357 *
2bb7a706
KZ
1358 * blkid_probe_enable_partitions(pr, 1);
1359 * blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC);
1360 *
cd0fe5c1
KZ
1361 * while (blkid_do_probe(pr) == 0) {
1362 * const char *ostr = NULL;
1363 * size_t len = 0;
1364 *
1365 * // superblocks
1366 * if (blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &ostr, NULL) == 0)
1367 * blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
1368 *
1369 * // partition tables
1370 * if (len == 0 && blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &ostr, NULL) == 0)
1371 * blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
1372 *
1373 * if (!len || !str)
1374 * continue;
1375 *
1376 * // convert ostr to the real offset by off = strtoll(ostr, NULL, 10);
9e930041 1377 * // use your stuff to erase @len bytes at the @off
cd0fe5c1
KZ
1378 * ....
1379 *
1380 * // retry the last probing to check for backup superblocks ..etc.
1381 * blkid_probe_step_back(pr);
1382 * }
1383 * </programlisting>
1384 * </example>
1385 *
1386 * Returns: 0 on success, and -1 in case of error.
1387 */
1388int blkid_probe_step_back(blkid_probe pr)
1389{
1390 struct blkid_chain *chn;
1391
cd0fe5c1
KZ
1392 chn = pr->cur_chain;
1393 if (!chn)
1394 return -1;
1395
d2b0c658
KZ
1396 if (!(pr->flags & BLKID_FL_MODIF_BUFF))
1397 blkid_probe_reset_buffers(pr);
cd0fe5c1
KZ
1398
1399 if (chn->idx >= 0) {
1400 chn->idx--;
c62a6311 1401 DBG(LOWPROBE, ul_debug("step back: moving %s chain index to %d",
cd0fe5c1
KZ
1402 chn->driver->name,
1403 chn->idx));
2b89be6c 1404 }
cd0fe5c1
KZ
1405
1406 if (chn->idx == -1) {
1407 /* blkid_do_probe() goes to the next chain if the index
1408 * of the current chain is -1, so we have to set the
1409 * chain pointer to the previous chain.
1410 */
1411 size_t idx = chn->driver->id > 0 ? chn->driver->id - 1 : 0;
1412
c62a6311 1413 DBG(LOWPROBE, ul_debug("step back: moving to previous chain"));
cd0fe5c1
KZ
1414
1415 if (idx > 0)
1416 pr->cur_chain = &pr->chains[idx];
1417 else if (idx == 0)
1418 pr->cur_chain = NULL;
1419 }
1420
2b89be6c
KZ
1421 return 0;
1422}
1423
0bffad47
KZ
1424/**
1425 * blkid_do_safeprobe:
1426 * @pr: prober
1427 *
1428 * This function gathers probing results from all enabled chains and checks
1429 * for ambivalent results (e.g. more filesystems on the device).
1430 *
1431 * This is string-based NAME=value interface only.
1432 *
9e930041 1433 * Note about superblocks chain -- the function does not check for filesystems
0bffad47 1434 * when a RAID signature is detected. The function also does not check for
c81e7008
KZ
1435 * collision between RAIDs. The first detected RAID is returned. The function
1436 * checks for collision between partition table and RAID signature -- it's
1437 * recommended to enable partitions chain together with superblocks chain.
0bffad47 1438 *
9e930041 1439 * Returns: 0 on success, 1 if nothing is detected, -2 if ambivalent result is
0bffad47
KZ
1440 * detected and -1 on case of error.
1441 */
1442int blkid_do_safeprobe(blkid_probe pr)
1443{
1444 int i, count = 0, rc = 0;
a0948ffe 1445
20e1c3dc
KZ
1446 if (pr->flags & BLKID_FL_NOSCAN_DEV)
1447 return 1;
a0948ffe 1448
c81e7008
KZ
1449 blkid_probe_start(pr);
1450
0bffad47
KZ
1451 for (i = 0; i < BLKID_NCHAINS; i++) {
1452 struct blkid_chain *chn;
a0948ffe 1453
0bffad47
KZ
1454 chn = pr->cur_chain = &pr->chains[i];
1455 chn->binary = FALSE; /* for sure... */
a0948ffe 1456
c62a6311 1457 DBG(LOWPROBE, ul_debug("chain safeprobe %s %s",
0bffad47
KZ
1458 chn->driver->name,
1459 chn->enabled? "ENABLED" : "DISABLED"));
1460
1461 if (!chn->enabled)
1462 continue;
1463
9e0f7bda 1464 blkid_probe_chain_reset_position(chn);
0bffad47 1465
0bffad47 1466 rc = chn->driver->safeprobe(pr, chn);
9e0f7bda
KZ
1467
1468 blkid_probe_chain_reset_position(chn);
1469
1470 /* rc: -2 ambivalent, -1 = error, 0 = success, 1 = no result */
0bffad47
KZ
1471 if (rc < 0)
1472 goto done; /* error */
1473 if (rc == 0)
1474 count++; /* success */
51410fc6 1475 }
0bffad47
KZ
1476
1477done:
c81e7008 1478 blkid_probe_end(pr);
0bffad47
KZ
1479 if (rc < 0)
1480 return rc;
1481 return count ? 0 : 1;
a0948ffe
KZ
1482}
1483
0bffad47
KZ
1484/**
1485 * blkid_do_fullprobe:
1486 * @pr: prober
1487 *
1488 * This function gathers probing results from all enabled chains. Same as
fd7c9e35 1489 * blkid_do_safeprobe() but does not check for collision between probing
0bffad47
KZ
1490 * result.
1491 *
1492 * This is string-based NAME=value interface only.
7103157c 1493 *
0bffad47 1494 * Returns: 0 on success, 1 if nothing is detected or -1 on case of error.
a2f01a1c 1495 */
0bffad47 1496int blkid_do_fullprobe(blkid_probe pr)
a2f01a1c 1497{
0bffad47 1498 int i, count = 0, rc = 0;
7103157c 1499
20e1c3dc
KZ
1500 if (pr->flags & BLKID_FL_NOSCAN_DEV)
1501 return 1;
a2f01a1c 1502
c81e7008
KZ
1503 blkid_probe_start(pr);
1504
0bffad47 1505 for (i = 0; i < BLKID_NCHAINS; i++) {
0bffad47 1506 struct blkid_chain *chn;
a2f01a1c 1507
0bffad47
KZ
1508 chn = pr->cur_chain = &pr->chains[i];
1509 chn->binary = FALSE; /* for sure... */
1510
c62a6311 1511 DBG(LOWPROBE, ul_debug("chain fullprobe %s: %s",
0bffad47
KZ
1512 chn->driver->name,
1513 chn->enabled? "ENABLED" : "DISABLED"));
1514
1515 if (!chn->enabled)
1516 continue;
1517
9e0f7bda 1518 blkid_probe_chain_reset_position(chn);
0bffad47 1519
0bffad47 1520 rc = chn->driver->probe(pr, chn);
9e0f7bda
KZ
1521
1522 blkid_probe_chain_reset_position(chn);
1523
1524 /* rc: -1 = error, 0 = success, 1 = no result */
0bffad47
KZ
1525 if (rc < 0)
1526 goto done; /* error */
1527 if (rc == 0)
1528 count++; /* success */
1529 }
1530
1531done:
c81e7008 1532 blkid_probe_end(pr);
0bffad47
KZ
1533 if (rc < 0)
1534 return rc;
1535 return count ? 0 : 1;
a2f01a1c
KZ
1536}
1537
ce011388
KZ
1538/* same sa blkid_probe_get_buffer() but works with 512-sectors */
1539unsigned char *blkid_probe_get_sector(blkid_probe pr, unsigned int sector)
1540{
7f787ced 1541 return blkid_probe_get_buffer(pr, ((uint64_t) sector) << 9, 0x200);
ce011388
KZ
1542}
1543
7f787ced 1544struct blkid_prval *blkid_probe_assign_value(blkid_probe pr, const char *name)
a0948ffe 1545{
51410fc6 1546 struct blkid_prval *v;
6c4a7811 1547
08029093 1548 v = calloc(1, sizeof(struct blkid_prval));
6c4a7811 1549 if (!v)
51410fc6 1550 return NULL;
a0948ffe 1551
08029093 1552 INIT_LIST_HEAD(&v->prvals);
51410fc6 1553 v->name = name;
9bdf6885 1554 v->chain = pr->cur_chain;
af17d349 1555 list_add_tail(&v->prvals, &pr->values);
6644688a 1556
c62a6311 1557 DBG(LOWPROBE, ul_debug("assigning %s [%s]", name, v->chain->driver->name));
51410fc6 1558 return v;
a0948ffe
KZ
1559}
1560
6c4a7811 1561/* Note that value data is always terminated by zero to keep things robust,
3fd1f771
RM
1562 * this extra zero is not count to the value length. It's caller responsibility
1563 * to set proper value length (for strings we count terminator to the length,
6c4a7811
OO
1564 * for binary data it's without terminator).
1565 */
1566int blkid_probe_value_set_data(struct blkid_prval *v,
47afae0c 1567 const unsigned char *data, size_t len)
6c4a7811
OO
1568{
1569 v->data = calloc(1, len + 1); /* always terminate by \0 */
cdd5bada 1570
6c4a7811
OO
1571 if (!v->data)
1572 return -ENOMEM;
1573 memcpy(v->data, data, len);
1574 v->len = len;
cdd5bada 1575 return 0;
cdd5bada
KZ
1576}
1577
51410fc6 1578int blkid_probe_set_value(blkid_probe pr, const char *name,
47afae0c 1579 const unsigned char *data, size_t len)
a0948ffe 1580{
51410fc6 1581 struct blkid_prval *v;
a0948ffe 1582
51410fc6
KZ
1583 v = blkid_probe_assign_value(pr, name);
1584 if (!v)
1585 return -1;
a0948ffe 1586
6c4a7811 1587 return blkid_probe_value_set_data(v, data, len);
a0948ffe
KZ
1588}
1589
51410fc6
KZ
1590int blkid_probe_vsprintf_value(blkid_probe pr, const char *name,
1591 const char *fmt, va_list ap)
a0948ffe 1592{
51410fc6 1593 struct blkid_prval *v;
a23facd6 1594 ssize_t len;
a0948ffe 1595
51410fc6
KZ
1596 v = blkid_probe_assign_value(pr, name);
1597 if (!v)
6c4a7811 1598 return -ENOMEM;
a0948ffe 1599
6c4a7811 1600 len = vasprintf((char **) &v->data, fmt, ap);
a0948ffe 1601
6c4a7811 1602 if (len <= 0) {
af17d349 1603 blkid_probe_free_value(v);
6c4a7811 1604 return len == 0 ? -EINVAL : -ENOMEM;
a0948ffe 1605 }
51410fc6 1606 v->len = len + 1;
a0948ffe
KZ
1607 return 0;
1608}
1609
cc33d693
KZ
1610int blkid_probe_sprintf_value(blkid_probe pr, const char *name,
1611 const char *fmt, ...)
1612{
1613 int rc;
1614 va_list ap;
1615
1616 va_start(ap, fmt);
1617 rc = blkid_probe_vsprintf_value(pr, name, fmt, ap);
1618 va_end(ap);
1619
1620 return rc;
1621}
1622
f12cd8d1 1623int blkid_probe_set_magic(blkid_probe pr, uint64_t offset,
47afae0c 1624 size_t len, const unsigned char *magic)
3c83b3b2
KZ
1625{
1626 int rc = 0;
1627 struct blkid_chain *chn = blkid_probe_get_chain(pr);
1628
7f787ced 1629 if (!chn || !len || chn->binary)
3c83b3b2
KZ
1630 return 0;
1631
1632 switch (chn->driver->id) {
1633 case BLKID_CHAIN_SUBLKS:
1634 if (!(chn->flags & BLKID_SUBLKS_MAGIC))
1635 return 0;
1636 rc = blkid_probe_set_value(pr, "SBMAGIC", magic, len);
1637 if (!rc)
1638 rc = blkid_probe_sprintf_value(pr,
44064b3c 1639 "SBMAGIC_OFFSET", "%llu", (unsigned long long)offset);
3c83b3b2
KZ
1640 break;
1641 case BLKID_CHAIN_PARTS:
1642 if (!(chn->flags & BLKID_PARTS_MAGIC))
1643 return 0;
1644 rc = blkid_probe_set_value(pr, "PTMAGIC", magic, len);
1645 if (!rc)
1646 rc = blkid_probe_sprintf_value(pr,
44064b3c 1647 "PTMAGIC_OFFSET", "%llu", (unsigned long long)offset);
3c83b3b2
KZ
1648 break;
1649 default:
1650 break;
1651 }
1652
1653 return rc;
1654}
1655
c89a1def
KZ
1656int blkid_probe_verify_csum(blkid_probe pr, uint64_t csum, uint64_t expected)
1657{
1658 if (csum != expected) {
02f3c12a
GP
1659 struct blkid_chain *chn = blkid_probe_get_chain(pr);
1660
c62a6311 1661 DBG(LOWPROBE, ul_debug(
c89a1def 1662 "incorrect checksum for type %s,"
fdbd7bb9 1663 " got %"PRIX64", expected %"PRIX64"",
c89a1def
KZ
1664 blkid_probe_get_probername(pr),
1665 csum, expected));
d88803a4
KZ
1666 /*
1667 * Accept bad checksum if BLKID_SUBLKS_BADCSUM flags is set
1668 */
1669 if (chn->driver->id == BLKID_CHAIN_SUBLKS
1670 && (chn->flags & BLKID_SUBLKS_BADCSUM)) {
1671 blkid_probe_set_value(pr, "SBBADCSUM", (unsigned char *) "1", 2);
1672 goto accept;
1673 }
1674 return 0; /* bad checksum */
c89a1def
KZ
1675 }
1676
d88803a4
KZ
1677accept:
1678 return 1;
c89a1def
KZ
1679}
1680
b3ee97a3
KZ
1681/**
1682 * blkid_probe_get_devno:
1683 * @pr: probe
1684 *
3b159691 1685 * Returns: block device number, or 0 for regular files.
b3ee97a3
KZ
1686 */
1687dev_t blkid_probe_get_devno(blkid_probe pr)
1688{
b3ee97a3
KZ
1689 return pr->devno;
1690}
1691
601fb1c1
KZ
1692/**
1693 * blkid_probe_get_wholedisk_devno:
1694 * @pr: probe
1695 *
3b159691 1696 * Returns: device number of the wholedisk, or 0 for regular files.
601fb1c1
KZ
1697 */
1698dev_t blkid_probe_get_wholedisk_devno(blkid_probe pr)
1699{
1700 if (!pr->disk_devno) {
1701 dev_t devno, disk_devno = 0;
1702
1703 devno = blkid_probe_get_devno(pr);
1704 if (!devno)
1705 return 0;
1706
2bf68c93 1707 if (blkid_devno_to_wholedisk(devno, NULL, 0, &disk_devno) == 0)
601fb1c1
KZ
1708 pr->disk_devno = disk_devno;
1709 }
1710 return pr->disk_devno;
1711}
1712
1713/**
1714 * blkid_probe_is_wholedisk:
1715 * @pr: probe
1716 *
1717 * Returns: 1 if the device is whole-disk or 0.
1718 */
1719int blkid_probe_is_wholedisk(blkid_probe pr)
1720{
1721 dev_t devno, disk_devno;
1722
1723 devno = blkid_probe_get_devno(pr);
1724 if (!devno)
1725 return 0;
1726
1727 disk_devno = blkid_probe_get_wholedisk_devno(pr);
1728 if (!disk_devno)
1729 return 0;
1730
1731 return devno == disk_devno;
1732}
1733
fd9f45e1
KZ
1734blkid_probe blkid_probe_get_wholedisk_probe(blkid_probe pr)
1735{
1736 dev_t disk;
1737
1738 if (blkid_probe_is_wholedisk(pr))
1739 return NULL; /* this is not partition */
1740
1741 if (pr->parent)
1742 /* this is cloned blkid_probe, use parent's stuff */
1743 return blkid_probe_get_wholedisk_probe(pr->parent);
1744
1745 disk = blkid_probe_get_wholedisk_devno(pr);
1746
1747 if (pr->disk_probe && pr->disk_probe->devno != disk) {
1748 /* we have disk prober, but for another disk... close it */
1749 blkid_free_probe(pr->disk_probe);
1750 pr->disk_probe = NULL;
1751 }
1752
1753 if (!pr->disk_probe) {
1754 /* Open a new disk prober */
1755 char *disk_path = blkid_devno_to_devname(disk);
1756
1757 if (!disk_path)
1758 return NULL;
1759
c62a6311 1760 DBG(LOWPROBE, ul_debug("allocate a wholedisk probe"));
fd9f45e1
KZ
1761
1762 pr->disk_probe = blkid_new_probe_from_filename(disk_path);
7eac65fc
KZ
1763
1764 free(disk_path);
1765
fd9f45e1
KZ
1766 if (!pr->disk_probe)
1767 return NULL; /* ENOMEM? */
1768 }
1769
1770 return pr->disk_probe;
1771}
1772
b3ee97a3
KZ
1773/**
1774 * blkid_probe_get_size:
1775 * @pr: probe
1776 *
30696241
KZ
1777 * This function returns size of probing area as defined by blkid_probe_set_device().
1778 * If the size of the probing area is unrestricted then this function returns
1779 * the real size of device. See also blkid_get_dev_size().
1780 *
1781 * Returns: size in bytes or -1 in case of error.
b3ee97a3
KZ
1782 */
1783blkid_loff_t blkid_probe_get_size(blkid_probe pr)
1784{
7f787ced 1785 return (blkid_loff_t) pr->size;
b3ee97a3
KZ
1786}
1787
56e961e2
KZ
1788/**
1789 * blkid_probe_get_offset:
1790 * @pr: probe
1791 *
1792 * This function returns offset of probing area as defined by blkid_probe_set_device().
1793 *
1794 * Returns: offset in bytes or -1 in case of error.
1795 */
1796blkid_loff_t blkid_probe_get_offset(blkid_probe pr)
1797{
7f787ced 1798 return (blkid_loff_t) pr->off;
56e961e2
KZ
1799}
1800
1801/**
1802 * blkid_probe_get_fd:
1803 * @pr: probe
1804 *
e3436956 1805 * Returns: file descriptor for assigned device/file or -1 in case of error.
56e961e2
KZ
1806 */
1807int blkid_probe_get_fd(blkid_probe pr)
1808{
7f787ced 1809 return pr->fd;
56e961e2
KZ
1810}
1811
b3ee97a3
KZ
1812/**
1813 * blkid_probe_get_sectorsize:
90ec8d9c 1814 * @pr: probe or NULL (for NULL returns 512)
b3ee97a3 1815 *
2a1dfbad 1816 * Returns: block device logical sector size (BLKSSZGET ioctl, default 512).
b3ee97a3
KZ
1817 */
1818unsigned int blkid_probe_get_sectorsize(blkid_probe pr)
1819{
b3ee97a3
KZ
1820 if (pr->blkssz)
1821 return pr->blkssz;
b3ee97a3 1822
90ec8d9c
KZ
1823 if (S_ISBLK(pr->mode) &&
1824 blkdev_get_sector_size(pr->fd, (int *) &pr->blkssz) == 0)
1825 return pr->blkssz;
b3ee97a3 1826
b3ee97a3
KZ
1827 pr->blkssz = DEFAULT_SECTOR_SIZE;
1828 return pr->blkssz;
1829}
1830
76fab513
KZ
1831/**
1832 * blkid_probe_set_sectorsize:
1833 * @pr: probe
1834 * @sz: new size (to overwrite system default)
1835 *
1836 * Note that blkid_probe_set_device() resets this setting. Use it after
1837 * blkid_probe_set_device() and before any probing call.
1838 *
ae4e2abc
KZ
1839 * Since: 2.30
1840 *
76fab513
KZ
1841 * Returns: 0 or <0 in case of error
1842 */
1843int blkid_probe_set_sectorsize(blkid_probe pr, unsigned int sz)
1844{
1845 pr->blkssz = sz;
1846 return 0;
1847}
1848
e8ae4947
DB
1849/**
1850 * blkid_probe_get_sectors:
1851 * @pr: probe
1852 *
1853 * Returns: 512-byte sector count or -1 in case of error.
1854 */
1855blkid_loff_t blkid_probe_get_sectors(blkid_probe pr)
1856{
7f787ced 1857 return (blkid_loff_t) (pr->size >> 9);
e8ae4947
DB
1858}
1859
4d72b337
KZ
1860/**
1861 * blkid_probe_numof_values:
1862 * @pr: probe
1863 *
1864 * Returns: number of values in probing result or -1 in case of error.
1865 */
1866int blkid_probe_numof_values(blkid_probe pr)
1867{
6c4a7811
OO
1868 int i = 0;
1869 struct list_head *p;
6c4a7811 1870
af17d349 1871 list_for_each(p, &pr->values)
6c4a7811
OO
1872 ++i;
1873 return i;
4d72b337
KZ
1874}
1875
81f73792
KZ
1876/**
1877 * blkid_probe_get_value:
1878 * @pr: probe
1879 * @num: wanted value in range 0..N, where N is blkid_probe_numof_values() - 1
1880 * @name: pointer to return value name or NULL
1881 * @data: pointer to return value data or NULL
1882 * @len: pointer to return value length or NULL
1883 *
c2dbd49b
KZ
1884 * Note, the @len returns length of the @data, including the terminating
1885 * '\0' character.
1886 *
81f73792
KZ
1887 * Returns: 0 on success, or -1 in case of error.
1888 */
51410fc6 1889int blkid_probe_get_value(blkid_probe pr, int num, const char **name,
6d042d0d 1890 const char **data, size_t *len)
a0948ffe 1891{
81f73792 1892 struct blkid_prval *v = __blkid_probe_get_value(pr, num);
a0948ffe 1893
81f73792 1894 if (!v)
51410fc6 1895 return -1;
51410fc6
KZ
1896 if (name)
1897 *name = v->name;
1898 if (data)
6d042d0d 1899 *data = (char *) v->data;
51410fc6
KZ
1900 if (len)
1901 *len = v->len;
6644688a 1902
c62a6311 1903 DBG(LOWPROBE, ul_debug("returning %s value", v->name));
a0948ffe
KZ
1904 return 0;
1905}
a0948ffe 1906
81f73792
KZ
1907/**
1908 * blkid_probe_lookup_value:
1909 * @pr: probe
1910 * @name: name of value
1911 * @data: pointer to return value data or NULL
1912 * @len: pointer to return value length or NULL
1913 *
c2dbd49b
KZ
1914 * Note, the @len returns length of the @data, including the terminating
1915 * '\0' character.
1916 *
81f73792
KZ
1917 * Returns: 0 on success, or -1 in case of error.
1918 */
51410fc6 1919int blkid_probe_lookup_value(blkid_probe pr, const char *name,
6d042d0d 1920 const char **data, size_t *len)
a0948ffe 1921{
81f73792 1922 struct blkid_prval *v = __blkid_probe_lookup_value(pr, name);
a0948ffe 1923
81f73792 1924 if (!v)
51410fc6 1925 return -1;
81f73792
KZ
1926 if (data)
1927 *data = (char *) v->data;
1928 if (len)
1929 *len = v->len;
81f73792 1930 return 0;
a0948ffe
KZ
1931}
1932
81f73792
KZ
1933/**
1934 * blkid_probe_has_value:
1935 * @pr: probe
1936 * @name: name of value
1937 *
1938 * Returns: 1 if value exist in probing result, otherwise 0.
1939 */
51410fc6 1940int blkid_probe_has_value(blkid_probe pr, const char *name)
a0948ffe 1941{
51410fc6
KZ
1942 if (blkid_probe_lookup_value(pr, name, NULL, NULL) == 0)
1943 return 1;
a0948ffe
KZ
1944 return 0;
1945}
1946
1c1726a7
KZ
1947struct blkid_prval *__blkid_probe_get_value(blkid_probe pr, int num)
1948{
6c4a7811
OO
1949 int i = 0;
1950 struct list_head *p;
1951
7f787ced 1952 if (num < 0)
1c1726a7
KZ
1953 return NULL;
1954
af17d349 1955 list_for_each(p, &pr->values) {
6c4a7811
OO
1956 if (i++ != num)
1957 continue;
1958 return list_entry(p, struct blkid_prval, prvals);
1959 }
1960 return NULL;
1c1726a7
KZ
1961}
1962
1963struct blkid_prval *__blkid_probe_lookup_value(blkid_probe pr, const char *name)
1964{
6c4a7811 1965 struct list_head *p;
1c1726a7 1966
7f787ced 1967 if (list_empty(&pr->values))
1c1726a7
KZ
1968 return NULL;
1969
af17d349 1970 list_for_each(p, &pr->values) {
6c4a7811
OO
1971 struct blkid_prval *v = list_entry(p, struct blkid_prval,
1972 prvals);
1c1726a7
KZ
1973
1974 if (v->name && strcmp(name, v->name) == 0) {
c62a6311 1975 DBG(LOWPROBE, ul_debug("returning %s value", v->name));
1c1726a7
KZ
1976 return v;
1977 }
1978 }
1979 return NULL;
1980}
1981
201529bd
KZ
1982
1983/* converts DCE UUID (uuid[16]) to human readable string
1984 * - the @len should be always 37 */
c9f51c71
KZ
1985void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len)
1986{
201529bd
KZ
1987 snprintf(str, len,
1988 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1989 uuid[0], uuid[1], uuid[2], uuid[3],
1990 uuid[4], uuid[5],
1991 uuid[6], uuid[7],
1992 uuid[8], uuid[9],
1993 uuid[10], uuid[11], uuid[12], uuid[13], uuid[14],uuid[15]);
201529bd
KZ
1994}
1995
9c06cdbf
KZ
1996/* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */
1997int blkid_uuid_is_empty(const unsigned char *buf, size_t len)
1998{
1999 size_t i;
2000
2001 for (i = 0; i < len; i++)
2002 if (buf[i])
2003 return 0;
2004 return 1;
2005}
c2dbd49b
KZ
2006
2007/* Removes whitespace from the right-hand side of a string (trailing
2008 * whitespace).
2009 *
2010 * Returns size of the new string (without \0).
2011 */
2012size_t blkid_rtrim_whitespace(unsigned char *str)
2013{
22e9e9c8 2014 return rtrim_whitespace(str);
c2dbd49b
KZ
2015}
2016
fafe46bc
ZAK
2017/* Removes whitespace from the left-hand side of a string.
2018 *
2019 * Returns size of the new string (without \0).
2020 */
2021size_t blkid_ltrim_whitespace(unsigned char *str)
2022{
22e9e9c8 2023 return ltrim_whitespace(str);
fafe46bc 2024}
22e9e9c8 2025
8b7eae45
KZ
2026/*
2027 * Some mkfs-like utils wipe some parts (usually begin) of the device.
2028 * For example LVM (pvcreate) or mkswap(8). This information could be used
2029 * for later resolution to conflicts between superblocks.
2030 *
2031 * For example we found valid LVM superblock, LVM wipes 8KiB at the begin of
89d39d22
KZ
2032 * the device. If we found another signature (for example MBR) within the
2033 * wiped area then the signature has been added later and LVM superblock
2034 * should be ignore.
8b7eae45 2035 *
29e204d1
KZ
2036 * Note that this heuristic is not 100% reliable, for example "pvcreate --zero n"
2037 * can be used to keep the begin of the device unmodified. It's probably better
8b7eae45
KZ
2038 * to use this heuristic for conflicts between superblocks and partition tables
2039 * than for conflicts between filesystem superblocks -- existence of unwanted
2040 * partition table is very unusual, because PT is pretty visible (parsed and
2041 * interpreted by kernel).
89d39d22
KZ
2042 *
2043 * Note that we usually expect only one signature on the device, it means that
2044 * we have to remember only one wiped area from previously successfully
2045 * detected signature.
2046 *
2047 * blkid_probe_set_wiper() -- defines wiped area (e.g. LVM)
2048 * blkid_probe_use_wiper() -- try to use area (e.g. MBR)
2049 *
2050 * Note that there is not relation between _wiper and blkid_to_wipe().
2051 *
8b7eae45 2052 */
f12cd8d1 2053void blkid_probe_set_wiper(blkid_probe pr, uint64_t off, uint64_t size)
8b7eae45
KZ
2054{
2055 struct blkid_chain *chn;
2056
8b7eae45 2057 if (!size) {
c62a6311 2058 DBG(LOWPROBE, ul_debug("zeroize wiper"));
8b7eae45
KZ
2059 pr->wipe_size = pr->wipe_off = 0;
2060 pr->wipe_chain = NULL;
2061 return;
2062 }
2063
2064 chn = pr->cur_chain;
2065
2066 if (!chn || !chn->driver ||
c9f51c71 2067 chn->idx < 0 || (size_t) chn->idx >= chn->driver->nidinfos)
8b7eae45
KZ
2068 return;
2069
2070 pr->wipe_size = size;
2071 pr->wipe_off = off;
2072 pr->wipe_chain = chn;
2073
0540ea54 2074 DBG(LOWPROBE,
fdbd7bb9 2075 ul_debug("wiper set to %s::%s off=%"PRIu64" size=%"PRIu64"",
8b7eae45
KZ
2076 chn->driver->name,
2077 chn->driver->idinfos[chn->idx]->name,
2078 pr->wipe_off, pr->wipe_size));
8b7eae45
KZ
2079}
2080
2081/*
2082 * Returns 1 if the <@off,@size> area was wiped
2083 */
f12cd8d1 2084int blkid_probe_is_wiped(blkid_probe pr, struct blkid_chain **chn, uint64_t off, uint64_t size)
8b7eae45 2085{
7f787ced 2086 if (!size)
8b7eae45
KZ
2087 return 0;
2088
2089 if (pr->wipe_off <= off && off + size <= pr->wipe_off + pr->wipe_size) {
7f787ced 2090 *chn = pr->wipe_chain;
8b7eae45
KZ
2091 return 1;
2092 }
2093 return 0;
2094}
2095
89d39d22
KZ
2096/*
2097 * Try to use any area -- if the area has been previously wiped then the
9e930041 2098 * previous probing result should be ignored (reset).
89d39d22 2099 */
f12cd8d1 2100void blkid_probe_use_wiper(blkid_probe pr, uint64_t off, uint64_t size)
8b7eae45
KZ
2101{
2102 struct blkid_chain *chn = NULL;
2103
2104 if (blkid_probe_is_wiped(pr, &chn, off, size) && chn) {
c62a6311 2105 DBG(LOWPROBE, ul_debug("previously wiped area modified "
0540ea54 2106 " -- ignore previous results"));
8b7eae45 2107 blkid_probe_set_wiper(pr, 0, 0);
af17d349 2108 blkid_probe_chain_reset_values(pr, chn);
8b7eae45
KZ
2109 }
2110}
67719fbb 2111
b7bca244
KZ
2112static struct blkid_hint *get_hint(blkid_probe pr, const char *name)
2113{
2114 struct list_head *p;
2115
2116 if (list_empty(&pr->hints))
2117 return NULL;
2118
2119 list_for_each(p, &pr->hints) {
2120 struct blkid_hint *h = list_entry(p, struct blkid_hint, hints);
2121
2122 if (h->name && strcmp(name, h->name) == 0)
2123 return h;
2124 }
2125 return NULL;
2126}
2127
67719fbb 2128/**
248c239b 2129 * blkid_probe_set_hint:
67719fbb
KZ
2130 * @pr: probe
2131 * @name: hint name or NAME=value
2132 * @value: offset or another number
2133 *
2134 * Sets extra hint for low-level prober. If the hint is set by NAME=value
2135 * notation than @value is ignored. The functions blkid_probe_set_device()
2136 * and blkid_reset_probe() resets all hints.
2137 *
2138 * The hints are optional way how to force libblkid probing functions to check
2139 * for example another location.
2140 *
2141 * Returns: 0 on success, or -1 in case of error.
2142 */
2143int blkid_probe_set_hint(blkid_probe pr, const char *name, uint64_t value)
2144{
2145 struct blkid_hint *hint = NULL;
2146 char *n = NULL, *v = NULL;
2147
2148 if (strchr(name, '=')) {
2149 char *end = NULL;
2150
2151 if (blkid_parse_tag_string(name, &n, &v) != 0)
2152 goto done;
2153
2154 errno = 0;
2155 value = strtoumax(v, &end, 10);
2156
2157 if (errno || v == end || (end && *end))
2158 goto done;
b7bca244
KZ
2159 }
2160
2161 hint = get_hint(pr, n ? n : name);
2162 if (hint) {
2163 /* alter old hint */
2164 hint->value = value;
2165 DBG(LOWPROBE,
2166 ul_debug("updated hint '%s' to %"PRIu64"", hint->name, hint->value));
67719fbb 2167 } else {
b7bca244
KZ
2168 /* add a new hint */
2169 if (!n) {
2170 n = strdup(name);
2171 if (!n)
2172 goto done;
2173 }
2174 hint = malloc(sizeof(*hint));
2175 if (!hint)
67719fbb 2176 goto done;
67719fbb 2177
b7bca244
KZ
2178 hint->name = n;
2179 hint->value = value;
67719fbb 2180
b7bca244
KZ
2181 INIT_LIST_HEAD(&hint->hints);
2182 list_add_tail(&hint->hints, &pr->hints);
67719fbb 2183
b7bca244
KZ
2184 DBG(LOWPROBE,
2185 ul_debug("new hint '%s' is %"PRIu64"", hint->name, hint->value));
2186 n = NULL;
2187 }
67719fbb
KZ
2188done:
2189 free(n);
2190 free(v);
b7bca244 2191
67719fbb
KZ
2192 if (!hint)
2193 return errno ? -errno : -EINVAL;
2194 return 0;
2195}
2196
2197int blkid_probe_get_hint(blkid_probe pr, const char *name, uint64_t *value)
2198{
b7bca244 2199 struct blkid_hint *h = get_hint(pr, name);
67719fbb 2200
b7bca244 2201 if (!h)
67719fbb 2202 return -EINVAL;
b7bca244
KZ
2203 if (value)
2204 *value = h->value;
2205 return 0;
67719fbb
KZ
2206}
2207
248c239b
KZ
2208/**
2209 * blkid_probe_reset_hints:
2210 * @pr probe
2211 *
2212 * Removes all previously defined probinig hints. See also blkid_probe_set_hint().
2213 */
2214void blkid_probe_reset_hints(blkid_probe pr)
67719fbb
KZ
2215{
2216 if (list_empty(&pr->hints))
2217 return;
2218
2219 DBG(LOWPROBE, ul_debug("resetting hints"));
2220
2221 while (!list_empty(&pr->hints)) {
2222 struct blkid_hint *h = list_entry(pr->hints.next,
2223 struct blkid_hint, hints);
2224 list_del(&h->hints);
2225 free(h->name);
2226 free(h);
2227 }
2228
2229 INIT_LIST_HEAD(&pr->hints);
2230}