/
probe.c
2575 lines (2156 loc) · 60.8 KB
/
probe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Low-level libblkid probing API
*
* Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com>
*
* This file may be redistributed under the terms of the
* GNU Lesser General Public License.
*/
/**
* SECTION: lowprobe
* @title: Low-level probing
* @short_description: low-level prober initialization
*
* The low-level probing routines always and directly read information from
* the selected (see blkid_probe_set_device()) device.
*
* The probing routines are grouped together into separate chains. Currently,
* the library provides superblocks, partitions and topology chains.
*
* The probing routines is possible to filter (enable/disable) by type (e.g.
* fstype "vfat" or partype "gpt") or by usage flags (e.g. BLKID_USAGE_RAID).
* These filters are per-chain. Note that always when you touch the chain
* filter the current probing position is reset and probing starts from
* scratch. It means that the chain filter should not be modified during
* probing, for example in loop where you call blkid_do_probe().
*
* For more details see the chain specific documentation.
*
* The low-level API provides two ways how access to probing results.
*
* 1. The NAME=value (tag) interface. This interface is older and returns all data
* as strings. This interface is generic for all chains.
*
* 2. The binary interfaces. These interfaces return data in the native formats.
* The interface is always specific to the probing chain.
*
* Note that the previous probing result (binary or NAME=value) is always
* zeroized when a chain probing function is called. For example:
*
* <informalexample>
* <programlisting>
* blkid_probe_enable_partitions(pr, TRUE);
* blkid_probe_enable_superblocks(pr, FALSE);
*
* blkid_do_safeprobe(pr);
* </programlisting>
* </informalexample>
*
* overwrites the previous probing result for the partitions chain, the superblocks
* result is not modified.
*/
/**
* SECTION: lowprobe-tags
* @title: Low-level tags
* @short_description: generic NAME=value interface.
*
* The probing routines inside the chain are mutually exclusive by default --
* only few probing routines are marked as "tolerant". The "tolerant" probing
* routines are used for filesystem which can share the same device with any
* other filesystem. The blkid_do_safeprobe() checks for the "tolerant" flag.
*
* The SUPERBLOCKS chain is enabled by default. The all others chains is
* necessary to enable by blkid_probe_enable_'CHAINNAME'(). See chains specific
* documentation.
*
* The blkid_do_probe() function returns a result from only one probing
* routine, and the next call from the next probing routine. It means you need
* to call the function in loop, for example:
*
* <informalexample>
* <programlisting>
* while((blkid_do_probe(pr) == BLKID_PROBE_OK)
* ... use result ...
* </programlisting>
* </informalexample>
*
* The blkid_do_safeprobe() is the same as blkid_do_probe(), but returns only
* first probing result for every enabled chain. This function checks for
* ambivalent results (e.g. more "intolerant" filesystems superblocks on the
* device).
*
* The probing result is set of NAME=value pairs (the NAME is always unique).
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/mman.h>
#include <sys/types.h>
#ifdef HAVE_LINUX_CDROM_H
#include <linux/cdrom.h>
#endif
#ifdef HAVE_LINUX_BLKZONED_H
#include <linux/blkzoned.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_LINUX_FD_H
#include <linux/fd.h>
#endif
#include <inttypes.h>
#include <stdint.h>
#include <stdarg.h>
#include <limits.h>
#ifdef HAVE_OPAL_GET_STATUS
#include <linux/sed-opal.h>
#endif
#include "blkidP.h"
#include "all-io.h"
#include "sysfs.h"
#include "strutils.h"
#include "list.h"
#include "fileutils.h"
/*
* All supported chains
*/
static const struct blkid_chaindrv *chains_drvs[] = {
[BLKID_CHAIN_SUBLKS] = &superblocks_drv,
[BLKID_CHAIN_TOPLGY] = &topology_drv,
[BLKID_CHAIN_PARTS] = &partitions_drv
};
static void blkid_probe_reset_values(blkid_probe pr);
/**
* blkid_new_probe:
*
* Returns: a pointer to the newly allocated probe struct or NULL in case of error.
*/
blkid_probe blkid_new_probe(void)
{
int i;
blkid_probe pr;
pr = calloc(1, sizeof(struct blkid_struct_probe));
if (!pr)
return NULL;
DBG(LOWPROBE, ul_debug("allocate a new probe"));
/* initialize chains */
for (i = 0; i < BLKID_NCHAINS; i++) {
pr->chains[i].driver = chains_drvs[i];
pr->chains[i].flags = chains_drvs[i]->dflt_flags;
pr->chains[i].enabled = chains_drvs[i]->dflt_enabled;
}
INIT_LIST_HEAD(&pr->buffers);
INIT_LIST_HEAD(&pr->prunable_buffers);
INIT_LIST_HEAD(&pr->values);
INIT_LIST_HEAD(&pr->hints);
return pr;
}
/*
* Clone @parent, the new clone shares all, but except:
*
* - probing result
* - buffers if another device (or offset) is set to the prober
*/
blkid_probe blkid_clone_probe(blkid_probe parent)
{
blkid_probe pr;
if (!parent)
return NULL;
DBG(LOWPROBE, ul_debug("allocate a probe clone"));
pr = blkid_new_probe();
if (!pr)
return NULL;
pr->fd = parent->fd;
pr->off = parent->off;
pr->size = parent->size;
pr->io_size = parent->io_size;
pr->devno = parent->devno;
pr->disk_devno = parent->disk_devno;
pr->blkssz = parent->blkssz;
pr->flags = parent->flags;
pr->zone_size = parent->zone_size;
pr->parent = parent;
pr->flags &= ~BLKID_FL_PRIVATE_FD;
return pr;
}
/**
* blkid_new_probe_from_filename:
* @filename: device or regular file
*
* This function is same as call open(filename), blkid_new_probe() and
* blkid_probe_set_device(pr, fd, 0, 0).
*
* The @filename is closed by blkid_free_probe() or by the
* blkid_probe_set_device() call.
*
* Returns: a pointer to the newly allocated probe struct or NULL in case of
* error.
*/
blkid_probe blkid_new_probe_from_filename(const char *filename)
{
int fd;
blkid_probe pr = NULL;
fd = open(filename, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
if (fd < 0)
return NULL;
pr = blkid_new_probe();
if (!pr)
goto err;
if (blkid_probe_set_device(pr, fd, 0, 0))
goto err;
pr->flags |= BLKID_FL_PRIVATE_FD;
return pr;
err:
close(fd);
blkid_free_probe(pr);
return NULL;
}
/**
* blkid_free_probe:
* @pr: probe
*
* Deallocates the probe struct, buffers and all allocated
* data that are associated with this probing control struct.
*/
void blkid_free_probe(blkid_probe pr)
{
int i;
if (!pr)
return;
for (i = 0; i < BLKID_NCHAINS; i++) {
struct blkid_chain *ch = &pr->chains[i];
if (ch->driver->free_data)
ch->driver->free_data(pr, ch->data);
free(ch->fltr);
ch->fltr = NULL;
}
if ((pr->flags & BLKID_FL_PRIVATE_FD) && pr->fd >= 0)
close(pr->fd);
blkid_probe_reset_buffers(pr);
blkid_probe_reset_values(pr);
blkid_probe_reset_hints(pr);
blkid_free_probe(pr->disk_probe);
DBG(LOWPROBE, ul_debug("free probe"));
free(pr);
}
void blkid_probe_free_value(struct blkid_prval *v)
{
if (!v)
return;
list_del(&v->prvals);
free(v->data);
DBG(LOWPROBE, ul_debug(" free value %s", v->name));
free(v);
}
/*
* Removes chain values from probing result.
*/
void blkid_probe_chain_reset_values(blkid_probe pr, struct blkid_chain *chn)
{
struct list_head *p, *pnext;
if (list_empty(&pr->values))
return;
DBG(LOWPROBE, ul_debug("Resetting %s values", chn->driver->name));
list_for_each_safe(p, pnext, &pr->values) {
struct blkid_prval *v = list_entry(p,
struct blkid_prval, prvals);
if (v->chain == chn)
blkid_probe_free_value(v);
}
}
static void blkid_probe_chain_reset_position(struct blkid_chain *chn)
{
chn->idx = -1;
}
/*
* Move chain values from probing result to @vals
*/
int blkid_probe_chain_save_values(blkid_probe pr, struct blkid_chain *chn,
struct list_head *vals)
{
struct list_head *p, *pnext;
struct blkid_prval *v;
DBG(LOWPROBE, ul_debug("saving %s values", chn->driver->name));
list_for_each_safe(p, pnext, &pr->values) {
v = list_entry(p, struct blkid_prval, prvals);
if (v->chain != chn)
continue;
list_del_init(&v->prvals);
list_add_tail(&v->prvals, vals);
}
return 0;
}
/*
* Appends values from @vals to the probing result
*/
void blkid_probe_append_values_list(blkid_probe pr, struct list_head *vals)
{
DBG(LOWPROBE, ul_debug("appending values"));
list_splice(vals, &pr->values);
INIT_LIST_HEAD(vals);
}
void blkid_probe_free_values_list(struct list_head *vals)
{
if (!vals)
return;
DBG(LOWPROBE, ul_debug("freeing values list"));
while (!list_empty(vals)) {
struct blkid_prval *v = list_entry(vals->next, struct blkid_prval, prvals);
blkid_probe_free_value(v);
}
}
struct blkid_chain *blkid_probe_get_chain(blkid_probe pr)
{
return pr->cur_chain;
}
static const char *blkid_probe_get_probername(blkid_probe pr)
{
struct blkid_chain *chn = blkid_probe_get_chain(pr);
if (chn && chn->idx >= 0 && (unsigned)chn->idx < chn->driver->nidinfos)
return chn->driver->idinfos[chn->idx]->name;
return NULL;
}
void *blkid_probe_get_binary_data(blkid_probe pr, struct blkid_chain *chn)
{
int rc, org_prob_flags;
struct blkid_chain *org_chn;
/* save the current setting -- the binary API has to be completely
* independent on the current probing status
*/
org_chn = pr->cur_chain;
org_prob_flags = pr->prob_flags;
pr->cur_chain = chn;
pr->prob_flags = 0;
chn->binary = TRUE;
blkid_probe_chain_reset_position(chn);
rc = chn->driver->probe(pr, chn);
chn->binary = FALSE;
blkid_probe_chain_reset_position(chn);
/* restore the original setting
*/
pr->cur_chain = org_chn;
pr->prob_flags = org_prob_flags;
if (rc != 0)
return NULL;
DBG(LOWPROBE, ul_debug("returning %s binary data", chn->driver->name));
return chn->data;
}
/**
* blkid_reset_probe:
* @pr: probe
*
* Zeroize probing results and resets the current probing (this has impact to
* blkid_do_probe() only). This function does not touch probing filters and
* keeps assigned device.
*/
void blkid_reset_probe(blkid_probe pr)
{
int i;
blkid_probe_reset_values(pr);
blkid_probe_set_wiper(pr, 0, 0);
pr->cur_chain = NULL;
for (i = 0; i < BLKID_NCHAINS; i++)
blkid_probe_chain_reset_position(&pr->chains[i]);
}
/***
static int blkid_probe_dump_filter(blkid_probe pr, int chain)
{
struct blkid_chain *chn;
int i;
if (!pr || chain < 0 || chain >= BLKID_NCHAINS)
return -1;
chn = &pr->chains[chain];
if (!chn->fltr)
return -1;
for (i = 0; i < chn->driver->nidinfos; i++) {
const struct blkid_idinfo *id = chn->driver->idinfos[i];
DBG(LOWPROBE, ul_debug("%d: %s: %s",
i,
id->name,
blkid_bmp_get_item(chn->fltr, i)
? "disabled" : "enabled <--"));
}
return 0;
}
***/
/*
* Returns properly initialized chain filter
*/
unsigned long *blkid_probe_get_filter(blkid_probe pr, int chain, int create)
{
struct blkid_chain *chn;
if (chain < 0 || chain >= BLKID_NCHAINS)
return NULL;
chn = &pr->chains[chain];
/* always when you touch the chain filter all indexes are reset and
* probing starts from scratch
*/
blkid_probe_chain_reset_position(chn);
pr->cur_chain = NULL;
if (!chn->driver->has_fltr || (!chn->fltr && !create))
return NULL;
if (!chn->fltr)
chn->fltr = calloc(1, blkid_bmp_nbytes(chn->driver->nidinfos));
else
memset(chn->fltr, 0, blkid_bmp_nbytes(chn->driver->nidinfos));
/* blkid_probe_dump_filter(pr, chain); */
return chn->fltr;
}
/*
* Generic private functions for filter setting
*/
int __blkid_probe_invert_filter(blkid_probe pr, int chain)
{
size_t i;
struct blkid_chain *chn;
chn = &pr->chains[chain];
if (!chn->driver->has_fltr || !chn->fltr)
return -1;
for (i = 0; i < blkid_bmp_nwords(chn->driver->nidinfos); i++)
chn->fltr[i] = ~chn->fltr[i];
DBG(LOWPROBE, ul_debug("probing filter inverted"));
/* blkid_probe_dump_filter(pr, chain); */
return 0;
}
int __blkid_probe_reset_filter(blkid_probe pr, int chain)
{
return blkid_probe_get_filter(pr, chain, FALSE) ? 0 : -1;
}
int __blkid_probe_filter_types(blkid_probe pr, int chain, int flag, char *names[])
{
unsigned long *fltr;
struct blkid_chain *chn;
size_t i;
fltr = blkid_probe_get_filter(pr, chain, TRUE);
if (!fltr)
return -1;
chn = &pr->chains[chain];
for (i = 0; i < chn->driver->nidinfos; i++) {
int has = 0;
const struct blkid_idinfo *id = chn->driver->idinfos[i];
char **n;
for (n = names; *n; n++) {
if (!strcmp(id->name, *n)) {
has = 1;
break;
}
}
if (has) {
if (flag & BLKID_FLTR_NOTIN)
blkid_bmp_set_item(fltr, i);
} else if (flag & BLKID_FLTR_ONLYIN)
blkid_bmp_set_item(fltr, i);
}
DBG(LOWPROBE, ul_debug("%s: a new probing type-filter initialized",
chn->driver->name));
/* blkid_probe_dump_filter(pr, chain); */
return 0;
}
static void remove_buffer(struct blkid_bufinfo *bf)
{
list_del(&bf->bufs);
DBG(BUFFER, ul_debug(" remove buffer: [off=%"PRIu64", len=%"PRIu64"]",
bf->off, bf->len));
munmap(bf->data, bf->len);
free(bf);
}
static struct blkid_bufinfo *read_buffer(blkid_probe pr, uint64_t real_off, uint64_t len)
{
ssize_t ret;
struct blkid_bufinfo *bf = NULL;
if (lseek(pr->fd, real_off, SEEK_SET) == (off_t) -1) {
errno = 0;
return NULL;
}
/* someone trying to overflow some buffers? */
if (len > ULONG_MAX - sizeof(struct blkid_bufinfo)) {
errno = ENOMEM;
return NULL;
}
/* allocate info and space for data by one malloc call */
bf = calloc(1, sizeof(struct blkid_bufinfo));
if (!bf) {
errno = ENOMEM;
return NULL;
}
bf->data = mmap(NULL, len, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (bf->data == MAP_FAILED) {
free(bf);
errno = ENOMEM;
return NULL;
}
bf->len = len;
bf->off = real_off;
INIT_LIST_HEAD(&bf->bufs);
DBG(LOWPROBE, ul_debug("\tread: off=%"PRIu64" len=%"PRIu64"",
real_off, len));
ret = read(pr->fd, bf->data, len);
if (ret != (ssize_t) len) {
DBG(LOWPROBE, ul_debug("\tread failed: %m"));
remove_buffer(bf);
/* I/O errors on CDROMs are non-fatal to work with hybrid
* audio+data disks */
if (ret >= 0 || blkid_probe_is_cdrom(pr) || blkdid_probe_is_opal_locked(pr))
errno = 0;
return NULL;
}
if (mprotect(bf->data, len, PROT_READ))
DBG(LOWPROBE, ul_debug("\tmprotect failed: %m"));
return bf;
}
/*
* Search in buffers we already have in memory
*/
static struct blkid_bufinfo *get_cached_buffer(blkid_probe pr, uint64_t off, uint64_t len)
{
uint64_t real_off = pr->off + off;
struct list_head *p;
list_for_each(p, &pr->buffers) {
struct blkid_bufinfo *x =
list_entry(p, struct blkid_bufinfo, bufs);
if (real_off >= x->off && real_off + len <= x->off + x->len) {
DBG(BUFFER, ul_debug("\treuse: off=%"PRIu64" len=%"PRIu64" (for off=%"PRIu64" len=%"PRIu64")",
x->off, x->len, real_off, len));
return x;
}
}
return NULL;
}
/*
* Mark smaller buffers that can be satisfied by bf as prunable
*/
static void mark_prunable_buffers(blkid_probe pr, const struct blkid_bufinfo *bf)
{
struct list_head *p, *next;
list_for_each_safe(p, next, &pr->buffers) {
struct blkid_bufinfo *x =
list_entry(p, struct blkid_bufinfo, bufs);
if (bf->off <= x->off && bf->off + bf->len >= x->off + x->len) {
list_del(&x->bufs);
list_add(&x->bufs, &pr->prunable_buffers);
}
}
}
/*
* Remove buffers that are marked as prunable
*/
void blkid_probe_prune_buffers(blkid_probe pr)
{
struct list_head *p, *next;
list_for_each_safe(p, next, &pr->prunable_buffers) {
struct blkid_bufinfo *x =
list_entry(p, struct blkid_bufinfo, bufs);
remove_buffer(x);
}
}
/*
* Zeroize in-memory data in already read buffer. The next blkid_probe_get_buffer()
* will return modified buffer. This is usable when you want to call the same probing
* function more than once and hide previously detected magic strings.
*
* See blkid_probe_hide_range().
*/
static int hide_buffer(blkid_probe pr, uint64_t off, uint64_t len)
{
uint64_t real_off = pr->off + off;
struct list_head *p;
int ct = 0;
if (UINT64_MAX - len < off) {
DBG(BUFFER, ul_debug("\t hide-buffer overflow (ignore)"));
return -EINVAL;
}
list_for_each(p, &pr->buffers) {
struct blkid_bufinfo *x =
list_entry(p, struct blkid_bufinfo, bufs);
unsigned char *data;
if (real_off >= x->off && real_off + len <= x->off + x->len) {
assert(x->off <= real_off);
assert(x->off + x->len >= real_off + len);
data = real_off ? x->data + (real_off - x->off) : x->data;
DBG(BUFFER, ul_debug("\thiding: off=%"PRIu64" len=%"PRIu64,
off, len));
mprotect(x->data, x->len, PROT_READ | PROT_WRITE);
memset(data, 0, len);
mprotect(x->data, x->len, PROT_READ);
ct++;
}
}
return ct == 0 ? -EINVAL : 0;
}
/*
* Note that @off is offset within probing area, the probing area is defined by
* pr->off and pr->size.
*/
const unsigned char *blkid_probe_get_buffer(blkid_probe pr, uint64_t off, uint64_t len)
{
struct blkid_bufinfo *bf = NULL;
uint64_t real_off, bias, len_align;
bias = off % pr->io_size;
off -= bias;
len += bias;
if (len % pr->io_size) {
len_align = pr->io_size - (len % pr->io_size);
if (pr->off + off + len + len_align <= pr->size)
len += len_align;
}
real_off = pr->off + off;
/*
DBG(BUFFER, ul_debug("\t>>>> off=%ju, real-off=%ju (probe <%ju..%ju>, len=%ju",
off, real_off, pr->off, pr->off + pr->size, len));
*/
if (pr->size == 0 || pr->io_size == 0) {
errno = EINVAL;
return NULL;
}
if (UINT64_MAX - len < off || UINT64_MAX - len < real_off) {
DBG(BUFFER, ul_debug("\t read-buffer overflow (ignore)"));
return NULL;
}
if (len > 8388608 /* 8 Mib */ ) {
DBG(BUFFER, ul_debug("\t too large read request (ignore)"));
return NULL;
}
if (len == 0
|| (!S_ISCHR(pr->mode) && (pr->size < off || pr->size < len))
|| (!S_ISCHR(pr->mode) && (pr->off + pr->size < real_off + len))) {
DBG(BUFFER, ul_debug("\t read-buffer out of probing area (ignore)"));
errno = 0;
return NULL;
}
if (pr->parent &&
pr->parent->devno == pr->devno &&
pr->parent->off <= pr->off &&
pr->parent->off + pr->parent->size >= pr->off + pr->size) {
/*
* This is a cloned prober and points to the same area as
* parent. Let's use parent's buffers.
*
* Note that pr->off (and pr->parent->off) is always from the
* begin of the device.
*/
return blkid_probe_get_buffer(pr->parent,
pr->off + off - pr->parent->off, len);
}
/* try buffers we already have in memory or read from device */
bf = get_cached_buffer(pr, off, len);
if (!bf) {
bf = read_buffer(pr, real_off, len);
if (!bf)
return NULL;
mark_prunable_buffers(pr, bf);
list_add_tail(&bf->bufs, &pr->buffers);
}
assert(bf->off <= real_off);
assert(bf->off + bf->len >= real_off + len);
errno = 0;
return real_off ? bf->data + (real_off - bf->off + bias) : bf->data + bias;
}
/**
* blkid_probe_reset_buffers:
* @pr: prober
*
* libblkid reuse all already read buffers from the device. The buffers may be
* modified by blkid_probe_hide_range(). This function reset and free all
* cached buffers. The next blkid_do_probe() will read all data from the
* device.
*
* Since: 2.31
*
* Returns: <0 in case of failure, or 0 on success.
*/
int blkid_probe_reset_buffers(blkid_probe pr)
{
uint64_t ct = 0, len = 0;
pr->flags &= ~BLKID_FL_MODIF_BUFF;
blkid_probe_prune_buffers(pr);
if (list_empty(&pr->buffers))
return 0;
DBG(BUFFER, ul_debug("Resetting probing buffers"));
while (!list_empty(&pr->buffers)) {
struct blkid_bufinfo *bf = list_entry(pr->buffers.next,
struct blkid_bufinfo, bufs);
ct++;
len += bf->len;
remove_buffer(bf);
}
DBG(LOWPROBE, ul_debug(" buffers summary: %"PRIu64" bytes by %"PRIu64" read() calls",
len, ct));
INIT_LIST_HEAD(&pr->buffers);
return 0;
}
/**
* blkid_probe_hide_range:
* @pr: prober
* @off: start of the range
* @len: size of the range
*
* This function modifies in-memory cached data from the device. The specified
* range is zeroized. This is usable together with blkid_probe_step_back().
* The next blkid_do_probe() will not see specified area.
*
* Note that this is usable for already (by library) read data, and this
* function is not a way how to hide any large areas on your device.
*
* The function blkid_probe_reset_buffers() reverts all.
*
* Since: 2.31
*
* Returns: <0 in case of failure, or 0 on success.
*/
int blkid_probe_hide_range(blkid_probe pr, uint64_t off, uint64_t len)
{
int rc = hide_buffer(pr, off, len);
if (rc == 0)
pr->flags |= BLKID_FL_MODIF_BUFF;
return rc;
}
static void blkid_probe_reset_values(blkid_probe pr)
{
if (list_empty(&pr->values))
return;
DBG(LOWPROBE, ul_debug("resetting results"));
while (!list_empty(&pr->values)) {
struct blkid_prval *v = list_entry(pr->values.next,
struct blkid_prval, prvals);
blkid_probe_free_value(v);
}
INIT_LIST_HEAD(&pr->values);
}
/*
* Small devices need a special care.
*/
int blkid_probe_is_tiny(blkid_probe pr)
{
return (pr->flags & BLKID_FL_TINY_DEV);
}
/*
* CDROMs may fail when probed for RAID (last sector problem)
*/
int blkid_probe_is_cdrom(blkid_probe pr)
{
return (pr->flags & BLKID_FL_CDROM_DEV);
}
int blkdid_probe_is_opal_locked(blkid_probe pr)
{
if (!(pr->flags & BLKID_FL_OPAL_CHECKED)) {
pr->flags |= BLKID_FL_OPAL_CHECKED;
#ifdef HAVE_OPAL_GET_STATUS
ssize_t ret;
struct opal_status st = { };
int errsv = errno;
/* If the device is locked with OPAL, we'll fail to read with I/O
* errors when probing deep into the block device. */
ret = ioctl(pr->fd, IOC_OPAL_GET_STATUS, &st);
if (ret == 0 && (st.flags & OPAL_FL_LOCKED)) {
pr->flags |= BLKID_FL_OPAL_LOCKED;
}
errno = errsv;
#endif
}
return (pr->flags & BLKID_FL_OPAL_LOCKED);
}
#ifdef CDROM_GET_CAPABILITY
static int is_sector_readable(int fd, uint64_t sector)
{
char buf[512];
ssize_t sz;
if (lseek(fd, sector * 512, SEEK_SET) == (off_t) -1)
goto failed;
sz = read(fd, buf, sizeof(buf));
if (sz != (ssize_t) sizeof(buf))
goto failed;
return 1;
failed:
DBG(LOWPROBE, ul_debug("CDROM: read sector %"PRIu64" failed %m", sector));
errno = 0;
return 0;
}
/*
* Linux kernel reports (BLKGETSIZE) cdrom device size greater than area
* readable by read(2). We have to reduce the probing area to avoid unwanted
* I/O errors in probing functions. It seems that unreadable are always last 2
* or 3 CD blocks (CD block size is 2048 bytes, it means 12 in 512-byte
* sectors). Linux kernel reports (CDROM_LAST_WRITTEN) also location of last
* written block, so we will reduce size based on it too.
*/
static void cdrom_size_correction(blkid_probe pr, uint64_t last_written)
{
uint64_t n, nsectors = pr->size >> 9;
if (last_written && nsectors > ((last_written+1) << 2))
nsectors = (last_written+1) << 2;
for (n = nsectors - 12; n < nsectors; n++) {
if (!is_sector_readable(pr->fd, n))
goto failed;
}
DBG(LOWPROBE, ul_debug("CDROM: full size available"));
return;
failed:
/* 'n' is the failed sector, reduce device size to n-1; */
DBG(LOWPROBE, ul_debug("CDROM: reduce size from %ju to %ju.",
(uintmax_t) pr->size,
(uintmax_t) n << 9));
pr->size = n << 9;
}
#endif
#ifdef BLKIOOPT
static uint64_t blkid_get_io_size(int fd)
{
static const int ioctls[] = { BLKIOOPT, BLKIOMIN, BLKBSZGET };
unsigned int s;
size_t i;
int r;
for (i = 0; i < ARRAY_SIZE(ioctls); i++) {
r = ioctl(fd, ioctls[i], &s);
if (r == 0 && is_power_of_2(s) && s >= DEFAULT_SECTOR_SIZE)
return min(s, 1U << 16);
}
return DEFAULT_SECTOR_SIZE;
}
#endif
/**
* blkid_probe_set_device:
* @pr: probe
* @fd: device file descriptor
* @off: begin of probing area
* @size: size of probing area (zero means whole device/file)
*
* Assigns the device to probe control struct, resets internal buffers, resets