-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
4530 lines (3891 loc) · 119 KB
/
main.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
/* $NetBSD: main.c,v 1.6 2022/04/09 09:59:16 riastradh Exp $ */
/*
* Copyright (c) 2022 Reinoud Zandijk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* Note to reader:
*
* fsck_udf uses the common udf_core.c file with newfs and makefs. It does use
* some of the layout structure values but not all.
*/
#include <sys/cdefs.h>
#ifndef lint
//__RCSID("$NetBSD: main.c,v 1.6 2022/04/09 09:59:16 riastradh Exp $");
#endif /* not lint */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <dirent.h>
#include <inttypes.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "util.h"
#include <time.h>
//#include <tzfile.h>
#include <math.h>
#include <assert.h>
#include <err.h>
#if !HAVE_NBTOOL_CONFIG_H
#define _EXPOSE_MMC
#include "cdio.h"
#else
#include "udf/cdio_mmc_structs.h"
#endif
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
//#include <sys/disklabel.h>
//#include <sys/dkio.h>
#include <sys/param.h>
#include <sys/queue.h>
#include "udf/ecma167-udf.h"
#include "udf/udf_mount.h"
#include "fsutil.h"
#include "exitvalues.h"
#include "udf_core.h"
/* Identifying myself */
#define IMPL_NAME "*NetBSD fsck_udf 10.0"
#define APP_VERSION_MAIN 0
#define APP_VERSION_SUB 5
/* allocation walker actions */
#define AD_LOAD_FILE (1<<0)
#define AD_SAVE_FILE (1<<1)
#define AD_CHECK_FIDS (1<<2)
#define AD_ADJUST_FIDS (1<<3)
#define AD_GATHER_STATS (1<<4)
#define AD_CHECK_USED (1<<5)
#define AD_MARK_AS_USED (1<<6)
#define AD_FIND_OVERLAP_PAIR (1<<7)
#define MONSPERYEAR 12
#define DAYSPERNYEAR 365
#define DAYSPERLYEAR 366
#define SECSPERMIN 60
#define SECSPERHOUR (60*60)
#define SECSPERDAY (24*60*60)
#define DAYSPERWEEK 7
struct udf_fsck_file_stats {
uint64_t inf_len;
uint64_t obj_size;
uint64_t logblks_rec;
};
struct udf_fsck_fid_context {
uint64_t fid_offset;
uint64_t data_left;
};
/* basic node administration for passes */
#define FSCK_NODE_FLAG_HARDLINK (1<< 0) /* hardlink, for accounting */
#define FSCK_NODE_FLAG_DIRECTORY (1<< 1) /* is a normal directory */
#define FSCK_NODE_FLAG_HAS_STREAM_DIR (1<< 2) /* has a stream directory */
#define FSCK_NODE_FLAG_STREAM_ENTRY (1<< 3) /* is a stream file */
#define FSCK_NODE_FLAG_STREAM_DIR (1<< 4) /* is a stream directory */
#define FSCK_NODE_FLAG_OK(f) (((f) >> 5) == 0)
#define FSCK_NODE_FLAG_KEEP (1<< 5) /* don't discard */
#define FSCK_NODE_FLAG_DIRTY (1<< 6) /* descriptor needs writeout */
#define FSCK_NODE_FLAG_REPAIRDIR (1<< 7) /* repair bad FID entries */
#define FSCK_NODE_FLAG_NEW_UNIQUE_ID (1<< 8) /* repair bad FID entries */
#define FSCK_NODE_FLAG_COPY_PARENT_ID (1<< 9) /* repair bad FID entries */
#define FSCK_NODE_FLAG_WIPE_STREAM_DIR (1<<10) /* wipe stream directory */
#define FSCK_NODE_FLAG_NOTFOUND (1<<11) /* FID pointing to garbage */
#define FSCK_NODE_FLAG_PAR_NOT_FOUND (1<<12) /* parent node not found! */
#define FSCK_NODE_FLAG_OVERLAP (1<<13) /* node has overlaps */
#define FSCK_NODE_FLAG_STREAM (FSCK_NODE_FLAG_STREAM_ENTRY | FSCK_NODE_FLAG_STREAM_DIR)
#define HASH_HASHBITS 5
#define HASH_HASHSIZE (1 << HASH_HASHBITS)
#define HASH_HASHMASK (HASH_HASHSIZE - 1)
/* fsck node for accounting checks */
struct udf_fsck_node {
struct udf_fsck_node *parent;
char *fname;
struct long_ad loc;
struct long_ad streamdir_loc;
int fsck_flags;
int link_count;
int found_link_count;
uint64_t unique_id;
struct udf_fsck_file_stats declared;
struct udf_fsck_file_stats found;
uint8_t *directory; /* directory contents */
LIST_ENTRY(udf_fsck_node) next_hash;
TAILQ_ENTRY(udf_fsck_node) next;
};
TAILQ_HEAD(udf_fsck_node_list, udf_fsck_node) fs_nodes;
LIST_HEAD(udf_fsck_node_hash_list, udf_fsck_node) fs_nodes_hash[HASH_HASHSIZE];
/* fsck used space bitmap conflict list */
#define FSCK_OVERLAP_MAIN_NODE (1<<0)
#define FSCK_OVERLAP_EXTALLOC (1<<1)
#define FSCK_OVERLAP_EXTENT (1<<2)
struct udf_fsck_overlap {
struct udf_fsck_node *node;
struct udf_fsck_node *node2;
struct long_ad loc;
struct long_ad loc2;
int flags;
int flags2;
TAILQ_ENTRY(udf_fsck_overlap) next;
};
TAILQ_HEAD(udf_fsck_overlap_list, udf_fsck_overlap) fsck_overlaps;
/* backup of old read in free space bitmaps */
struct space_bitmap_desc *recorded_part_unalloc_bits[UDF_PARTITIONS];
uint32_t recorded_part_free[UDF_PARTITIONS];
/* shadow VAT build */
uint8_t *shadow_vat_contents;
/* options */
int alwaysno = 0; /* assume "no" for all questions */
int alwaysyes = 0; /* assume "yes" for all questions */
int search_older_vat = 0; /* search for older VATs */
int force = 0; /* do check even if its marked clean */
int preen = 0; /* set when preening, doing automatic small repairs */
int rdonly = 0; /* open device/image read-only */
int rdonly_flag = 0; /* as passed on command line */
int heuristics = 0; /* use heuristics to fix esoteric corruptions */
int target_session = 0; /* offset to last session to check */
/* actions to undertake */
int undo_opening_session = 0; /* trying to undo opening of last crippled session */
int open_integrity = 0; /* should be open the integrity ie close later */
int vat_writeout = 0; /* write out the VAT anyway */
/* SIGINFO */
static sig_atomic_t print_info = 0; /* request for information on progress */
/* prototypes */
static void usage(void);
static int checkfilesys(char *given_dev);
static int ask(int def, const char *fmt, ...);
static int ask_noauto(int def, const char *fmt, ...);
static void udf_recursive_keep(struct udf_fsck_node *node);
static char *udf_node_path(struct udf_fsck_node *node);
static void udf_shadow_VAT_in_use(struct long_ad *loc);
static int udf_quick_check_fids(struct udf_fsck_node *node, union dscrptr *dscr);
/* --------------------------------------------------------------------- */
/* from bin/ls */
static void
printtime(time_t ftime)
{
struct timespec clock;
const char *longstring;
time_t now;
int i;
clock_gettime(CLOCK_REALTIME, &clock);
now = clock.tv_sec;
if ((longstring = ctime(&ftime)) == NULL) {
/* 012345678901234567890123 */
longstring = "????????????????????????";
}
for (i = 4; i < 11; ++i)
(void)putchar(longstring[i]);
#define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
for (i = 11; i < 16; ++i)
(void)putchar(longstring[i]);
else {
(void)putchar(' ');
for (i = 20; i < 24; ++i)
(void)putchar(longstring[i]);
}
(void)putchar(' ');
}
static void
udf_print_timestamp(const char *prefix, struct timestamp *timestamp, const char *suffix)
{
struct timespec timespec;
udf_timestamp_to_timespec(timestamp, ×pec);
printf("%s", prefix);
printtime(timespec.tv_sec);
printf("%s", suffix);
}
static int
udf_compare_mtimes(struct timestamp *t1, struct timestamp *t2)
{
struct timespec t1_tsp, t2_tsp;
udf_timestamp_to_timespec(t1, &t1_tsp);
udf_timestamp_to_timespec(t2, &t2_tsp);
if (t1_tsp.tv_sec < t2_tsp.tv_sec)
return -1;
if (t1_tsp.tv_sec > t2_tsp.tv_sec)
return 1;
if (t1_tsp.tv_nsec < t2_tsp.tv_nsec)
return -1;
if (t1_tsp.tv_nsec > t2_tsp.tv_nsec)
return 1;
return 0;
}
/* --------------------------------------------------------------------- */
static int
udf_calc_node_hash(struct long_ad *icb)
{
uint32_t lb_num = udf_rw32(icb->loc.lb_num);
uint16_t vpart = udf_rw16(icb->loc.part_num);
return ((uint64_t) (vpart + lb_num * 257)) & HASH_HASHMASK;
}
static struct udf_fsck_node *
udf_node_lookup(struct long_ad *icb)
{
struct udf_fsck_node *pos;
int entry = udf_calc_node_hash(icb);
pos = LIST_FIRST(&fs_nodes_hash[entry]);
while (pos) {
if (pos->loc.loc.part_num == icb->loc.part_num)
if (pos->loc.loc.lb_num == icb->loc.lb_num)
return pos;
pos = LIST_NEXT(pos, next_hash);
}
return NULL;
}
/* --------------------------------------------------------------------- */
/* Note: only for VAT media since we don't allocate in bitmap */
static void
udf_wipe_and_reallocate(union dscrptr *dscrptr, int vpart_num, uint32_t *l_adp)
{
struct file_entry *fe = &dscrptr->fe;
struct extfile_entry *efe = &dscrptr->efe;
struct desc_tag *tag = &dscrptr->tag;
struct icb_tag *icb;
struct long_ad allocated;
struct long_ad *long_adp = NULL;
struct short_ad *short_adp = NULL;
uint64_t inf_len;
uint32_t l_ea, l_ad;
uint8_t *bpos;
int bpos_start, ad_type, id;
assert(context.format_flags & FORMAT_VAT);
id = udf_rw16(tag->id);
assert(id == TAGID_FENTRY || id == TAGID_EXTFENTRY);
if (id == TAGID_FENTRY) {
icb = &fe->icbtag;
inf_len = udf_rw64(fe->inf_len);
l_ea = udf_rw32(fe->l_ea);
bpos = (uint8_t *) fe->data + l_ea;
bpos_start = offsetof(struct file_entry, data) + l_ea;
} else {
icb = &efe->icbtag;
inf_len = udf_rw64(efe->inf_len);
l_ea = udf_rw32(efe->l_ea);
bpos = (uint8_t *) efe->data + l_ea;
bpos_start = offsetof(struct extfile_entry, data) + l_ea;
}
/* inf_len should be correct for one slot */
assert(inf_len < UDF_EXT_MAXLEN);
ad_type = udf_rw16(icb->flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
if (ad_type == UDF_ICB_INTERN_ALLOC) {
/* no action needed */
return;
}
assert(vpart_num == context.data_part);
udf_data_alloc(udf_bytes_to_sectors(inf_len), &allocated);
memset(bpos, 0, context.sector_size - bpos_start);
/* create one short_ad or one long_ad */
if (ad_type == UDF_ICB_SHORT_ALLOC) {
short_adp = (struct short_ad *) bpos;
short_adp->len = udf_rw32(inf_len);
short_adp->lb_num = allocated.loc.lb_num;
l_ad = sizeof(struct short_ad);
} else {
long_adp = (struct long_ad *) bpos;
memcpy(long_adp, &allocated, sizeof(struct long_ad));
long_adp->len = udf_rw32(inf_len);
l_ad = sizeof(struct long_ad);
}
if (id == TAGID_FENTRY)
fe->l_ad = udf_rw32(l_ad);
else
efe->l_ad = udf_rw32(l_ad);
;
*l_adp = l_ad;
}
static void
udf_copy_fid_verbatim(struct fileid_desc *sfid, struct fileid_desc *dfid,
uint64_t dfpos, uint64_t drest)
{
uint64_t endfid;
uint32_t minlen, lb_rest, fidsize;
if (udf_rw16(sfid->l_iu) == 0) {
memcpy(dfid, sfid, udf_fidsize(sfid));
return;
}
/* see if we can reduce its size */
minlen = udf_fidsize(sfid) - udf_rw16(sfid->l_iu);
/*
* OK, tricky part: we need to pad so the next descriptor header won't
* cross the sector boundary
*/
endfid = dfpos + minlen;
lb_rest = context.sector_size - (endfid % context.sector_size);
memcpy(dfid, sfid, UDF_FID_SIZE);
if (lb_rest < sizeof(struct desc_tag)) {
/* add at least 32 */
dfid->l_iu = udf_rw16(32);
udf_set_regid((struct regid *) dfid->data, context.impl_name);
udf_add_impl_regid((struct regid *) dfid->data);
}
memcpy( dfid->data + udf_rw16(dfid->l_iu),
sfid->data + udf_rw16(sfid->l_iu),
minlen - UDF_FID_SIZE);
fidsize = udf_fidsize(dfid);
dfid->tag.desc_crc_len = udf_rw16(fidsize - UDF_DESC_TAG_LENGTH);
}
static int
udf_rebuild_fid_stream(struct udf_fsck_node *node, int64_t *rest_lenp)
{
struct fileid_desc *sfid, *dfid;
uint64_t inf_len;
uint64_t sfpos, dfpos;
int64_t srest, drest;
// uint32_t sfid_len, dfid_len;
uint8_t *directory, *rebuild_dir;
// int namelen;
int error, streaming, was_streaming, warned, error_in_stream;
directory = node->directory;
inf_len = node->found.inf_len;
rebuild_dir = calloc(1, inf_len);
assert(rebuild_dir);
sfpos = 0;
srest = inf_len;
dfpos = 0;
drest = inf_len;
error_in_stream = 0;
streaming = 1;
was_streaming = 1;
warned = 0;
while (srest > 0) {
if (was_streaming & !streaming) {
if (!warned) {
pwarn("%s : BROKEN directory\n",
udf_node_path(node));
udf_recursive_keep(node);
node->fsck_flags |= FSCK_NODE_FLAG_REPAIRDIR;
}
warned = 1;
pwarn("%s : <directory resync>\n",
udf_node_path(node));
}
was_streaming = streaming;
assert(drest >= UDF_FID_SIZE);
sfid = (struct fileid_desc *) (directory + sfpos);
dfid = (struct fileid_desc *) (rebuild_dir + dfpos);
/* check if we can read/salvage the next source fid */
if (udf_rw16(sfid->tag.id) != TAGID_FID) {
streaming = 0;
sfpos += 4;
srest -= 4;
error_in_stream = 1;
continue;
}
error = udf_check_tag(sfid);
if (error) {
/* unlikely to be recoverable */
streaming = 0;
sfpos += 4;
srest -= 4;
error_in_stream = 1;
continue;
}
error = udf_check_tag_payload(
(union dscrptr *) sfid,
context.sector_size);
if (!error) {
streaming = 1;
/* all OK, just copy verbatim, shrinking if possible */
udf_copy_fid_verbatim(sfid, dfid, dfpos, drest);
sfpos += udf_fidsize(sfid);
srest -= udf_fidsize(sfid);
dfpos += udf_fidsize(dfid);
drest -= udf_fidsize(dfid);
assert(udf_fidsize(sfid) == udf_fidsize(dfid));
continue;
}
/*
* The hard part, we need to try to recover of what is
* deductible of the bad source fid. The tag itself is OK, but
* that doesn't say much; its contents can still be off.
*/
/* TODO NOT IMPLEMENTED YET, skip this entry the blunt way */
streaming = 0;
sfpos += 4;
srest -= 4;
error_in_stream = 1;
}
/* if we could shrink/fix the node, mark it for repair */
if (error_in_stream) {
udf_recursive_keep(node);
node->fsck_flags |= FSCK_NODE_FLAG_REPAIRDIR;
}
if (sfpos != dfpos)
printf("%s: could save %" PRIi64 " bytes in directory\n", udf_node_path(node), sfpos - dfpos);
memset(directory, 0, inf_len);
memcpy(directory, rebuild_dir, dfpos);
free(rebuild_dir);
*rest_lenp = dfpos;
return error_in_stream;
}
static int
udf_quick_check_fids_piece(uint8_t *piece, uint32_t piece_len,
struct udf_fsck_fid_context *fid_context,
uint32_t lb_num)
{
int error;
struct fileid_desc *fid;
uint32_t location;
uint32_t offset, fidsize;
offset = fid_context->fid_offset % context.sector_size;
while (fid_context->data_left && (offset < piece_len)) {
fid = (struct fileid_desc *) (piece + offset);
if (udf_rw16(fid->tag.id) == TAGID_FID) {
error = udf_check_tag_payload(
(union dscrptr *) fid,
context.sector_size);
if (error)
return error;
} else {
return EINVAL;
}
assert(udf_rw16(fid->tag.id) == TAGID_FID);
location = lb_num + offset / context.sector_size;
if (udf_rw32(fid->tag.tag_loc) != location)
return EINVAL;
if (context.dscrver == 2) {
/* compression IDs should be preserved in UDF < 2.00 */
if (*(fid->data + udf_rw16(fid->l_iu)) > 16)
return EINVAL;
}
fidsize = udf_fidsize(fid);
offset += fidsize;
fid_context->fid_offset += fidsize;
fid_context->data_left -= fidsize;
}
return 0;
}
static void
udf_fids_fixup(uint8_t *piece, uint32_t piece_len,
struct udf_fsck_fid_context *fid_context,
uint32_t lb_num)
{
struct fileid_desc *fid;
uint32_t location;
uint32_t offset, fidsize;
offset = fid_context->fid_offset % context.sector_size;
while (fid_context->data_left && (offset < piece_len)) {
fid = (struct fileid_desc *) (piece + offset);
assert(udf_rw16(fid->tag.id) == TAGID_FID);
location = lb_num + offset / context.sector_size;
fid->tag.tag_loc = udf_rw32(location);
udf_validate_tag_and_crc_sums((union dscrptr *) fid);
fidsize = udf_fidsize(fid);
offset += fidsize;
fid_context->fid_offset += fidsize;
fid_context->data_left -= fidsize;
}
}
/* NOTE returns non 0 for overlap, not an error code */
static int
udf_check_if_allocated(struct udf_fsck_node *node, int flags,
uint32_t start_lb, int partnr, uint32_t piece_len)
{
union dscrptr *dscr;
struct udf_fsck_overlap *new_overlap;
uint8_t *bpos;
uint32_t cnt, bit;
uint32_t blocks = udf_bytes_to_sectors(piece_len);
int overlap = 0;
/* account for space used on underlying partition */
#ifdef DEBUG
printf("check allocated : node %p, flags %d, partnr %d, start_lb %d for %d blocks\n",
node, flags, partnr, start_lb, blocks);
#endif
switch (context.vtop_tp[partnr]) {
case UDF_VTOP_TYPE_VIRT:
/* nothing */
break;
case UDF_VTOP_TYPE_PHYS:
case UDF_VTOP_TYPE_SPAREABLE:
case UDF_VTOP_TYPE_META:
if (context.part_unalloc_bits[context.vtop[partnr]] == NULL)
break;
#ifdef DEBUG
printf("checking allocation of %d+%d for being used\n", start_lb, blocks);
#endif
dscr = (union dscrptr *) (context.part_unalloc_bits[partnr]);
for (cnt = start_lb; cnt < start_lb + blocks; cnt++) {
bpos = &dscr->sbd.data[cnt / 8];
bit = cnt % 8;
/* only account for bits marked free */
if ((*bpos & (1 << bit)) == 0)
overlap++;
}
if (overlap == 0)
break;
/* overlap */
// pwarn("%s allocation OVERLAP found, type %d\n",
// udf_node_path(node), flags);
udf_recursive_keep(node);
node->fsck_flags |= FSCK_NODE_FLAG_OVERLAP;
new_overlap = calloc(1, sizeof(struct udf_fsck_overlap));
assert(new_overlap);
new_overlap->node = node;
new_overlap->node2 = NULL;
new_overlap->flags = flags;
new_overlap->flags2 = 0;
new_overlap->loc.len = udf_rw32(piece_len);
new_overlap->loc.loc.lb_num = udf_rw32(start_lb);
new_overlap->loc.loc.part_num = udf_rw16(partnr);
TAILQ_INSERT_TAIL(&fsck_overlaps, new_overlap, next);
return overlap;
break;
default:
errx(1, "internal error: bad mapping type %d in %s",
context.vtop_tp[partnr], __func__);
}
/* no overlap */
return 0;
}
/* NOTE returns non 0 for overlap, not an error code */
static void
udf_check_overlap_pair(struct udf_fsck_node *node, int flags,
uint32_t start_lb, int partnr, uint32_t piece_len)
{
struct udf_fsck_overlap *overlap;
uint32_t ostart_lb, opiece_len, oblocks;
uint32_t blocks = udf_bytes_to_sectors(piece_len);
int opartnr;
/* account for space used on underlying partition */
#ifdef DEBUG
printf("check overlap pair : node %p, flags %d, partnr %d, start_lb %d for %d blocks\n",
node, flags, partnr, start_lb, blocks);
#endif
switch (context.vtop_tp[partnr]) {
case UDF_VTOP_TYPE_VIRT:
/* nothing */
break;
case UDF_VTOP_TYPE_PHYS:
case UDF_VTOP_TYPE_SPAREABLE:
case UDF_VTOP_TYPE_META:
if (context.part_unalloc_bits[context.vtop[partnr]] == NULL)
break;
#ifdef DEBUG
printf("checking overlap of %d+%d for being used\n", start_lb, blocks);
#endif
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = TAILQ_FIRST((head)); \
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
(var) = (tvar))
/* check all current overlaps with the piece we have here */
TAILQ_FOREACH(overlap, &fsck_overlaps, next) {
opiece_len = udf_rw32(overlap->loc.len);
ostart_lb = udf_rw32(overlap->loc.loc.lb_num);
opartnr = udf_rw16(overlap->loc.loc.part_num);
oblocks = udf_bytes_to_sectors(opiece_len);
if (partnr != opartnr)
continue;
/* piece before overlap? */
if (start_lb + blocks < ostart_lb)
continue;
/* piece after overlap? */
if (start_lb > ostart_lb + oblocks)
continue;
/* overlap, mark conflict */
overlap->node2 = node;
overlap->flags2 = flags;
overlap->loc2.len = udf_rw32(piece_len);
overlap->loc2.loc.lb_num = udf_rw32(start_lb);
overlap->loc2.loc.part_num = udf_rw16(partnr);
udf_recursive_keep(node);
node->fsck_flags |= FSCK_NODE_FLAG_OVERLAP;
}
return;
default:
errx(1, "internal error: bad mapping type %d in %s",
context.vtop_tp[partnr], __func__);
}
/* no overlap */
return;
}
#if !HAVE_REALLOCARR
#define SQRT_SIZE_MAX (((size_t)1) << (sizeof(size_t) * CHAR_BIT / 2))
int reallocarr(void *ptr, size_t number, size_t size);
int
reallocarr(void *ptr, size_t number, size_t size)
{
int saved_errno, result;
void *optr;
void *nptr;
saved_errno = errno;
memcpy(&optr, ptr, sizeof(ptr));
if (number == 0 || size == 0) {
free(optr);
nptr = NULL;
memcpy(ptr, &nptr, sizeof(ptr));
errno = saved_errno;
return 0;
}
/*
* Try to avoid division here.
*
* It isn't possible to overflow during multiplication if neither
* operand uses any of the most significant half of the bits.
*/
if (((number|size) >= SQRT_SIZE_MAX &&
number > SIZE_MAX / size)) {
errno = saved_errno;
return EOVERFLOW;
}
nptr = realloc(optr, number * size);
if ((nptr == NULL)) {
result = errno;
} else {
result = 0;
memcpy(ptr, &nptr, sizeof(ptr));
}
errno = saved_errno;
return result;
}
#endif
static int
udf_process_ad(union dscrptr *dscrptr, int action, uint8_t **resultp,
int vpart_num, uint64_t fpos,
struct short_ad *short_adp, struct long_ad *long_adp, void *process_context)
{
struct file_entry *fe = &dscrptr->fe;
struct extfile_entry *efe = &dscrptr->efe;
struct desc_tag *tag = &dscrptr->tag;
struct icb_tag *icb;
struct udf_fsck_file_stats *stats;
uint64_t inf_len;
uint32_t l_ea, piece_len, piece_alloc_len, piece_sectors, lb_num, flags;
uint32_t dscr_lb_num;
uint32_t i;
uint8_t *bpos, *piece;
int id, ad_type;
int error, piece_error, return_error;
assert(dscrptr);
stats = (struct udf_fsck_file_stats *) process_context;
id = udf_rw16(tag->id);
assert(id == TAGID_FENTRY || id == TAGID_EXTFENTRY);
if (id == TAGID_FENTRY) {
icb = &fe->icbtag;
dscr_lb_num = udf_rw32(fe->tag.tag_loc);
inf_len = udf_rw64(fe->inf_len);
l_ea = udf_rw32(fe->l_ea);
bpos = (uint8_t *) fe->data + l_ea;
} else {
icb = &efe->icbtag;
dscr_lb_num = udf_rw32(efe->tag.tag_loc);
inf_len = udf_rw64(efe->inf_len);
l_ea = udf_rw32(efe->l_ea);
bpos = (uint8_t *) efe->data + l_ea;
}
lb_num = 0;
piece_len = 0;
ad_type = udf_rw16(icb->flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
if (ad_type == UDF_ICB_INTERN_ALLOC) {
piece_len = inf_len;
}
if (short_adp) {
piece_len = udf_rw32(short_adp->len);
lb_num = udf_rw32(short_adp->lb_num);
}
if (long_adp) {
piece_len = udf_rw32(long_adp->len);
lb_num = udf_rw32(long_adp->loc.lb_num);
vpart_num = udf_rw16(long_adp->loc.part_num);
}
flags = UDF_EXT_FLAGS(piece_len);
piece_len = UDF_EXT_LEN(piece_len);
piece_alloc_len = UDF_ROUNDUP(piece_len, context.sector_size);
piece_sectors = piece_alloc_len / context.sector_size;
return_error = 0;
if (action & AD_GATHER_STATS) {
if (ad_type == UDF_ICB_INTERN_ALLOC) {
stats->inf_len = piece_len;
stats->obj_size = piece_len;
stats->logblks_rec = 0;
} else if (flags == UDF_EXT_ALLOCATED) {
stats->inf_len += piece_len;
stats->obj_size += piece_len;
stats->logblks_rec += piece_sectors;
} else if (flags == UDF_EXT_FREED) {
stats->inf_len += piece_len;
stats->obj_size += piece_len;
stats->logblks_rec += piece_sectors;
} else if (flags == UDF_EXT_FREE) {
stats->inf_len += piece_len;
stats->obj_size += piece_len;
}
}
if (action & AD_LOAD_FILE) {
uint32_t alloc_len;
piece = calloc(1, piece_alloc_len);
if (piece == NULL)
return errno;
if (ad_type == UDF_ICB_INTERN_ALLOC) {
memcpy(piece, bpos, piece_len);
} else if (flags == 0) {
/* not empty */
/* read sector by sector reading as much as possible */
for (i = 0; i < piece_sectors; i++) {
piece_error = udf_read_virt(
piece + i * context.sector_size,
lb_num + i, vpart_num, 1);
if (piece_error)
return_error = piece_error;
}
}
alloc_len = UDF_ROUNDUP(fpos + piece_len, context.sector_size);
error = reallocarr(resultp, 1, alloc_len);
if (error) {
/* fatal */
free(piece);
free(*resultp);
return errno;
}
memcpy(*resultp + fpos, piece, piece_alloc_len);
free(piece);
}
if (action & AD_ADJUST_FIDS) {
piece = *resultp + fpos;
if (ad_type == UDF_ICB_INTERN_ALLOC) {
udf_fids_fixup(piece, piece_len, process_context,
dscr_lb_num);
} else if (flags == 0) {
udf_fids_fixup(piece, piece_len, process_context,
lb_num);
}
}
if (action & AD_CHECK_FIDS) {
piece = *resultp + fpos;
if (ad_type == UDF_ICB_INTERN_ALLOC) {
error = udf_quick_check_fids_piece(piece, piece_len,
process_context, dscr_lb_num);
} else if (flags == 0) {
error = udf_quick_check_fids_piece(piece, piece_len,
process_context, lb_num);
}
if (error)
return error;
}
if (action & AD_SAVE_FILE) {
/*
* Note: only used for directory contents.
*/
piece = *resultp + fpos;
if (ad_type == UDF_ICB_INTERN_ALLOC) {
memcpy(bpos, piece, piece_len);
/* nothing */
} else if (flags == 0) {
/* not empty */
error = udf_write_virt(
piece, lb_num, vpart_num,
piece_sectors);
if (error) {
pwarn("Got error writing piece\n");
return error;
}
} else {
/* allocated but not written piece, skip */
}
}
if (action & AD_CHECK_USED) {
if (ad_type == UDF_ICB_INTERN_ALLOC) {
/* nothing */
} else if (flags != UDF_EXT_FREE) {
struct udf_fsck_node *node = process_context;
(void) udf_check_if_allocated(
node,
FSCK_OVERLAP_EXTENT,
lb_num, vpart_num,
piece_len);
}
}
if (action & AD_FIND_OVERLAP_PAIR) {
if (ad_type == UDF_ICB_INTERN_ALLOC) {
/* nothing */
} else if (flags != UDF_EXT_FREE) {
struct udf_fsck_node *node = process_context;
udf_check_overlap_pair(
node,
FSCK_OVERLAP_EXTENT,
lb_num, vpart_num,
piece_len);
}
}
if (action & AD_MARK_AS_USED) {
if (ad_type == UDF_ICB_INTERN_ALLOC) {
/* nothing */
} else if (flags != UDF_EXT_FREE) {
udf_mark_allocated(lb_num, vpart_num,
udf_bytes_to_sectors(piece_len));
}
}
return return_error;
}
static int
udf_process_file(union dscrptr *dscrptr, int vpart_num, uint8_t **resultp,
int action, void *process_context)
{
struct file_entry *fe = &dscrptr->fe;
struct extfile_entry *efe = &dscrptr->efe;
struct desc_tag *tag = &dscrptr->tag;
struct alloc_ext_entry *ext;
struct icb_tag *icb;
struct long_ad *long_adp = NULL;
struct short_ad *short_adp = NULL;
union dscrptr *extdscr = NULL;