-
Notifications
You must be signed in to change notification settings - Fork 141
/
volume.c
2013 lines (1845 loc) · 52.7 KB
/
volume.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
/**
* volume.c - NTFS volume handling code. Originated from the Linux-NTFS project.
*
* Copyright (c) 2000-2006 Anton Altaparmakov
* Copyright (c) 2002-2009 Szabolcs Szakacsits
* Copyright (c) 2004-2005 Richard Russon
* Copyright (c) 2010 Jean-Pierre Andre
*
* This program/include file is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program/include file is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (in the main directory of the NTFS-3G
* distribution in the file COPYING); if not, write to the Free Software
* Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#if defined(__sun) && defined (__SVR4)
#include <sys/mnttab.h>
#endif
#include "param.h"
#include "compat.h"
#include "volume.h"
#include "attrib.h"
#include "mft.h"
#include "bootsect.h"
#include "device.h"
#include "debug.h"
#include "inode.h"
#include "runlist.h"
#include "logfile.h"
#include "dir.h"
#include "logging.h"
#include "cache.h"
#include "realpath.h"
#include "misc.h"
#include "security.h"
const char *ntfs_home =
"News, support and information: https://github.com/tuxera/ntfs-3g/\n";
static const char *invalid_ntfs_msg =
"The device '%s' doesn't seem to have a valid NTFS.\n"
"Maybe the wrong device is used? Or the whole disk instead of a\n"
"partition (e.g. /dev/sda, not /dev/sda1)? Or the other way around?\n";
static const char *corrupt_volume_msg =
"NTFS is either inconsistent, or there is a hardware fault, or it's a\n"
"SoftRAID/FakeRAID hardware. In the first case run chkdsk /f on Windows\n"
"then reboot into Windows twice. The usage of the /f parameter is very\n"
"important! If the device is a SoftRAID/FakeRAID then first activate\n"
"it and mount a different device under the /dev/mapper/ directory, (e.g.\n"
"/dev/mapper/nvidia_eahaabcc1). Please see the 'dmraid' documentation\n"
"for more details.\n";
static const char *hibernated_volume_msg =
"The NTFS partition is in an unsafe state. Please resume and shutdown\n"
"Windows fully (no hibernation or fast restarting), or mount the volume\n"
"read-only with the 'ro' mount option.\n";
static const char *fallback_readonly_msg =
"Falling back to read-only mount because the NTFS partition is in an\n"
"unsafe state. Please resume and shutdown Windows fully (no hibernation\n"
"or fast restarting.)\n";
static const char *unclean_journal_msg =
"Write access is denied because the disk wasn't safely powered\n"
"off and the 'norecover' mount option was specified.\n";
static const char *opened_volume_msg =
"Mount is denied because the NTFS volume is already exclusively opened.\n"
"The volume may be already mounted, or another software may use it which\n"
"could be identified for example by the help of the 'fuser' command.\n";
static const char *fakeraid_msg =
"Either the device is missing or it's powered down, or you have\n"
"SoftRAID hardware and must use an activated, different device under\n"
"/dev/mapper/, (e.g. /dev/mapper/nvidia_eahaabcc1) to mount NTFS.\n"
"Please see the 'dmraid' documentation for help.\n";
static const char *access_denied_msg =
"Please check '%s' and the ntfs-3g binary permissions,\n"
"and the mounting user ID. More explanation is provided at\n"
"https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ\n";
/**
* ntfs_volume_alloc - Create an NTFS volume object and initialise it
*
* Description...
*
* Returns:
*/
ntfs_volume *ntfs_volume_alloc(void)
{
return ntfs_calloc(sizeof(ntfs_volume));
}
static void ntfs_attr_free(ntfs_attr **na)
{
if (na && *na) {
ntfs_attr_close(*na);
*na = NULL;
}
}
static int ntfs_inode_free(ntfs_inode **ni)
{
int ret = -1;
if (ni && *ni) {
ret = ntfs_inode_close(*ni);
*ni = NULL;
}
return ret;
}
static void ntfs_error_set(int *err)
{
if (!*err)
*err = errno;
}
/**
* __ntfs_volume_release - Destroy an NTFS volume object
* @v:
*
* Description...
*
* Returns:
*/
static int __ntfs_volume_release(ntfs_volume *v)
{
int err = 0;
if (ntfs_close_secure(v))
ntfs_error_set(&err);
if (ntfs_inode_free(&v->vol_ni))
ntfs_error_set(&err);
/*
* FIXME: Inodes must be synced before closing
* attributes, otherwise unmount could fail.
*/
if (v->lcnbmp_ni && NInoDirty(v->lcnbmp_ni))
ntfs_inode_sync(v->lcnbmp_ni);
ntfs_attr_free(&v->lcnbmp_na);
if (ntfs_inode_free(&v->lcnbmp_ni))
ntfs_error_set(&err);
if (v->mft_ni && NInoDirty(v->mft_ni))
ntfs_inode_sync(v->mft_ni);
ntfs_attr_free(&v->mftbmp_na);
ntfs_attr_free(&v->mft_na);
if (ntfs_inode_free(&v->mft_ni))
ntfs_error_set(&err);
if (v->mftmirr_ni && NInoDirty(v->mftmirr_ni))
ntfs_inode_sync(v->mftmirr_ni);
ntfs_attr_free(&v->mftmirr_na);
if (ntfs_inode_free(&v->mftmirr_ni))
ntfs_error_set(&err);
if (v->dev) {
struct ntfs_device *dev = v->dev;
if (dev->d_ops->sync(dev))
ntfs_error_set(&err);
if (dev->d_ops->close(dev))
ntfs_error_set(&err);
}
ntfs_free_lru_caches(v);
free(v->vol_name);
free(v->upcase);
if (v->locase) free(v->locase);
free(v->attrdef);
free(v);
errno = err;
return errno ? -1 : 0;
}
static int ntfs_attr_setup_flag(ntfs_inode *ni)
{
STANDARD_INFORMATION *si;
s64 lth;
int r;
si = (STANDARD_INFORMATION*)ntfs_attr_readall(ni,
AT_STANDARD_INFORMATION, AT_UNNAMED, 0, <h);
if (si) {
if ((u64)lth >= offsetof(STANDARD_INFORMATION, owner_id))
ni->flags = si->file_attributes;
free(si);
r = 0;
} else {
ntfs_log_error("Failed to get standard information of $MFT\n");
r = -1;
}
return (r);
}
/**
* ntfs_mft_load - load the $MFT and setup the ntfs volume with it
* @vol: ntfs volume whose $MFT to load
*
* Load $MFT from @vol and setup @vol with it. After calling this function the
* volume @vol is ready for use by all read access functions provided by the
* ntfs library.
*
* Return 0 on success and -1 on error with errno set to the error code.
*/
static int ntfs_mft_load(ntfs_volume *vol)
{
VCN next_vcn, last_vcn, highest_vcn;
s64 l;
MFT_RECORD *mb = NULL;
ntfs_attr_search_ctx *ctx = NULL;
ATTR_RECORD *a;
int eo;
/* Manually setup an ntfs_inode. */
vol->mft_ni = ntfs_inode_allocate(vol);
mb = ntfs_malloc(vol->mft_record_size);
if (!vol->mft_ni || !mb) {
ntfs_log_perror("Error allocating memory for $MFT");
goto error_exit;
}
vol->mft_ni->mft_no = 0;
vol->mft_ni->mrec = mb;
/* Can't use any of the higher level functions yet! */
l = ntfs_mst_pread(vol->dev, vol->mft_lcn << vol->cluster_size_bits, 1,
vol->mft_record_size, mb);
if (l != 1) {
if (l != -1)
errno = EIO;
ntfs_log_perror("Error reading $MFT");
goto error_exit;
}
if (ntfs_mft_record_check(vol, 0, mb))
goto error_exit;
ctx = ntfs_attr_get_search_ctx(vol->mft_ni, NULL);
if (!ctx)
goto error_exit;
/* Find the $ATTRIBUTE_LIST attribute in $MFT if present. */
if (ntfs_attr_lookup(AT_ATTRIBUTE_LIST, AT_UNNAMED, 0, 0, 0, NULL, 0,
ctx)) {
if (errno != ENOENT) {
ntfs_log_error("$MFT has corrupt attribute list.\n");
goto io_error_exit;
}
goto mft_has_no_attr_list;
}
NInoSetAttrList(vol->mft_ni);
l = ntfs_get_attribute_value_length(ctx->attr);
if (l <= 0 || l > 0x40000) {
ntfs_log_error("$MFT/$ATTR_LIST invalid length (%lld).\n",
(long long)l);
goto io_error_exit;
}
vol->mft_ni->attr_list_size = l;
vol->mft_ni->attr_list = ntfs_malloc(l);
if (!vol->mft_ni->attr_list)
goto error_exit;
l = ntfs_get_attribute_value(vol, ctx->attr, vol->mft_ni->attr_list);
if (!l) {
ntfs_log_error("Failed to get value of $MFT/$ATTR_LIST.\n");
goto io_error_exit;
}
if ((l != vol->mft_ni->attr_list_size)
|| (l < (s64)offsetof(ATTR_LIST_ENTRY, name))) {
ntfs_log_error("Partial read of $MFT/$ATTR_LIST (%lld != "
"%u or < %d).\n", (long long)l,
vol->mft_ni->attr_list_size,
(int)offsetof(ATTR_LIST_ENTRY, name));
goto io_error_exit;
}
mft_has_no_attr_list:
if (ntfs_attr_setup_flag(vol->mft_ni))
goto error_exit;
/* We now have a fully setup ntfs inode for $MFT in vol->mft_ni. */
/* Get an ntfs attribute for $MFT/$DATA and set it up, too. */
vol->mft_na = ntfs_attr_open(vol->mft_ni, AT_DATA, AT_UNNAMED, 0);
if (!vol->mft_na) {
ntfs_log_perror("Failed to open ntfs attribute");
goto error_exit;
}
/* Read all extents from the $DATA attribute in $MFT. */
ntfs_attr_reinit_search_ctx(ctx);
last_vcn = vol->mft_na->allocated_size >> vol->cluster_size_bits;
highest_vcn = next_vcn = 0;
a = NULL;
while (!ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, next_vcn, NULL, 0,
ctx)) {
runlist_element *nrl;
a = ctx->attr;
/* $MFT must be non-resident. */
if (!a->non_resident) {
ntfs_log_error("$MFT must be non-resident.\n");
goto io_error_exit;
}
/* $MFT must be uncompressed and unencrypted. */
if (a->flags & ATTR_COMPRESSION_MASK ||
a->flags & ATTR_IS_ENCRYPTED) {
ntfs_log_error("$MFT must be uncompressed and "
"unencrypted.\n");
goto io_error_exit;
}
/*
* Decompress the mapping pairs array of this extent and merge
* the result into the existing runlist. No need for locking
* as we have exclusive access to the inode at this time and we
* are a mount in progress task, too.
*/
nrl = ntfs_mapping_pairs_decompress(vol, a, vol->mft_na->rl);
if (!nrl) {
ntfs_log_perror("ntfs_mapping_pairs_decompress() failed");
goto error_exit;
}
/* Make sure $DATA is the MFT itself */
if (nrl->lcn != vol->mft_lcn) {
ntfs_log_perror("The MFT is not self-contained");
goto error_exit;
}
vol->mft_na->rl = nrl;
/* Get the lowest vcn for the next extent. */
highest_vcn = sle64_to_cpu(a->highest_vcn);
next_vcn = highest_vcn + 1;
/* Only one extent or error, which we catch below. */
if (next_vcn <= 0)
break;
/* Avoid endless loops due to corruption. */
if (next_vcn < sle64_to_cpu(a->lowest_vcn)) {
ntfs_log_error("$MFT has corrupt attribute list.\n");
goto io_error_exit;
}
}
if (!a) {
ntfs_log_error("$MFT/$DATA attribute not found.\n");
goto io_error_exit;
}
if (highest_vcn && highest_vcn != last_vcn - 1) {
ntfs_log_error("Failed to load runlist for $MFT/$DATA.\n");
ntfs_log_error("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx\n",
(long long)highest_vcn, (long long)last_vcn - 1);
goto io_error_exit;
}
/* Done with the $Mft mft record. */
ntfs_attr_put_search_ctx(ctx);
ctx = NULL;
/* Update the size fields in the inode. */
vol->mft_ni->data_size = vol->mft_na->data_size;
vol->mft_ni->allocated_size = vol->mft_na->allocated_size;
set_nino_flag(vol->mft_ni, KnownSize);
/*
* The volume is now setup so we can use all read access functions.
*/
vol->mftbmp_na = ntfs_attr_open(vol->mft_ni, AT_BITMAP, AT_UNNAMED, 0);
if (!vol->mftbmp_na) {
ntfs_log_perror("Failed to open $MFT/$BITMAP");
goto error_exit;
}
return 0;
io_error_exit:
errno = EIO;
error_exit:
eo = errno;
if (ctx)
ntfs_attr_put_search_ctx(ctx);
if (vol->mft_na) {
ntfs_attr_close(vol->mft_na);
vol->mft_na = NULL;
}
if (vol->mft_ni) {
ntfs_inode_close(vol->mft_ni);
vol->mft_ni = NULL;
}
errno = eo;
return -1;
}
/**
* ntfs_mftmirr_load - load the $MFTMirr and setup the ntfs volume with it
* @vol: ntfs volume whose $MFTMirr to load
*
* Load $MFTMirr from @vol and setup @vol with it. After calling this function
* the volume @vol is ready for use by all write access functions provided by
* the ntfs library (assuming ntfs_mft_load() has been called successfully
* beforehand).
*
* Return 0 on success and -1 on error with errno set to the error code.
*/
static int ntfs_mftmirr_load(ntfs_volume *vol)
{
int err;
vol->mftmirr_ni = ntfs_inode_open(vol, FILE_MFTMirr);
if (!vol->mftmirr_ni) {
ntfs_log_perror("Failed to open inode $MFTMirr");
return -1;
}
vol->mftmirr_na = ntfs_attr_open(vol->mftmirr_ni, AT_DATA, AT_UNNAMED, 0);
if (!vol->mftmirr_na) {
ntfs_log_perror("Failed to open $MFTMirr/$DATA");
goto error_exit;
}
if (ntfs_attr_map_runlist(vol->mftmirr_na, 0) < 0) {
ntfs_log_perror("Failed to map runlist of $MFTMirr/$DATA");
goto error_exit;
}
if (vol->mftmirr_na->rl->lcn != vol->mftmirr_lcn) {
ntfs_log_error("Bad $MFTMirr lcn 0x%llx, want 0x%llx\n",
(long long)vol->mftmirr_na->rl->lcn,
(long long)vol->mftmirr_lcn);
goto error_exit;
}
return 0;
error_exit:
err = errno;
if (vol->mftmirr_na) {
ntfs_attr_close(vol->mftmirr_na);
vol->mftmirr_na = NULL;
}
ntfs_inode_close(vol->mftmirr_ni);
vol->mftmirr_ni = NULL;
errno = err;
return -1;
}
/**
* ntfs_volume_startup - allocate and setup an ntfs volume
* @dev: device to open
* @flags: optional mount flags
*
* Load, verify, and parse bootsector; load and setup $MFT and $MFTMirr. After
* calling this function, the volume is setup sufficiently to call all read
* and write access functions provided by the library.
*
* Return the allocated volume structure on success and NULL on error with
* errno set to the error code.
*/
ntfs_volume *ntfs_volume_startup(struct ntfs_device *dev,
ntfs_mount_flags flags)
{
LCN mft_zone_size, mft_lcn;
s64 br;
ntfs_volume *vol;
NTFS_BOOT_SECTOR *bs;
int eo;
if (!dev || !dev->d_ops || !dev->d_name) {
errno = EINVAL;
ntfs_log_perror("%s: dev = %p", __FUNCTION__, dev);
return NULL;
}
bs = ntfs_malloc(sizeof(NTFS_BOOT_SECTOR));
if (!bs)
return NULL;
/* Allocate the volume structure. */
vol = ntfs_volume_alloc();
if (!vol)
goto error_exit;
/* Create the default upcase table. */
vol->upcase_len = ntfs_upcase_build_default(&vol->upcase);
if (!vol->upcase_len || !vol->upcase)
goto error_exit;
/* Default with no locase table and case sensitive file names */
vol->locase = (ntfschar*)NULL;
NVolSetCaseSensitive(vol);
/* by default, all files are shown and not marked hidden */
NVolSetShowSysFiles(vol);
NVolSetShowHidFiles(vol);
NVolClearHideDotFiles(vol);
/* set default compression */
#if DEFAULT_COMPRESSION
NVolSetCompression(vol);
#else
NVolClearCompression(vol);
#endif
if (flags & NTFS_MNT_RDONLY)
NVolSetReadOnly(vol);
/* ...->open needs bracketing to compile with glibc 2.7 */
if ((dev->d_ops->open)(dev, NVolReadOnly(vol) ? O_RDONLY: O_RDWR)) {
if (!NVolReadOnly(vol) && (errno == EROFS)) {
if ((dev->d_ops->open)(dev, O_RDONLY)) {
ntfs_log_perror("Error opening read-only '%s'",
dev->d_name);
goto error_exit;
} else {
ntfs_log_info("Error opening '%s' read-write\n",
dev->d_name);
NVolSetReadOnly(vol);
}
} else {
ntfs_log_perror("Error opening '%s'", dev->d_name);
goto error_exit;
}
}
/* Attach the device to the volume. */
vol->dev = dev;
/* Now read the bootsector. */
br = ntfs_pread(dev, 0, sizeof(NTFS_BOOT_SECTOR), bs);
if (br != sizeof(NTFS_BOOT_SECTOR)) {
if (br != -1)
errno = EINVAL;
if (!br)
ntfs_log_error("Failed to read bootsector (size=0)\n");
else
ntfs_log_perror("Error reading bootsector");
goto error_exit;
}
if (!ntfs_boot_sector_is_ntfs(bs)) {
errno = EINVAL;
goto error_exit;
}
if (ntfs_boot_sector_parse(vol, bs) < 0)
goto error_exit;
free(bs);
bs = NULL;
/* Now set the device block size to the sector size. */
if (ntfs_device_block_size_set(vol->dev, vol->sector_size))
ntfs_log_debug("Failed to set the device block size to the "
"sector size. This may affect performance "
"but should be harmless otherwise. Error: "
"%s\n", strerror(errno));
/* We now initialize the cluster allocator. */
vol->full_zones = 0;
mft_zone_size = vol->nr_clusters >> 3; /* 12.5% */
/* Setup the mft zone. */
vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
ntfs_log_debug("mft_zone_pos = 0x%llx\n", (long long)vol->mft_zone_pos);
/*
* Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
* source) and if the actual mft_lcn is in the expected place or even
* further to the front of the volume, extend the mft_zone to cover the
* beginning of the volume as well. This is in order to protect the
* area reserved for the mft bitmap as well within the mft_zone itself.
* On non-standard volumes we don't protect it as the overhead would be
* higher than the speed increase we would get by doing it.
*/
mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
if (mft_lcn * vol->cluster_size < 16 * 1024)
mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
vol->cluster_size;
if (vol->mft_zone_start <= mft_lcn)
vol->mft_zone_start = 0;
ntfs_log_debug("mft_zone_start = 0x%llx\n", (long long)vol->mft_zone_start);
/*
* Need to cap the mft zone on non-standard volumes so that it does
* not point outside the boundaries of the volume. We do this by
* halving the zone size until we are inside the volume.
*/
vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
while (vol->mft_zone_end >= vol->nr_clusters) {
mft_zone_size >>= 1;
if (!mft_zone_size) {
errno = EINVAL;
goto error_exit;
}
vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
}
ntfs_log_debug("mft_zone_end = 0x%llx\n", (long long)vol->mft_zone_end);
/*
* Set the current position within each data zone to the start of the
* respective zone.
*/
vol->data1_zone_pos = vol->mft_zone_end;
ntfs_log_debug("data1_zone_pos = %lld\n", (long long)vol->data1_zone_pos);
vol->data2_zone_pos = 0;
ntfs_log_debug("data2_zone_pos = %lld\n", (long long)vol->data2_zone_pos);
/* Set the mft data allocation position to mft record 24. */
vol->mft_data_pos = 24;
/*
* The cluster allocator is now fully operational.
*/
/* Need to setup $MFT so we can use the library read functions. */
if (ntfs_mft_load(vol) < 0) {
ntfs_log_perror("Failed to load $MFT");
goto error_exit;
}
/* Need to setup $MFTMirr so we can use the write functions, too. */
if (ntfs_mftmirr_load(vol) < 0) {
ntfs_log_perror("Failed to load $MFTMirr");
goto error_exit;
}
return vol;
error_exit:
eo = errno;
free(bs);
if (vol)
__ntfs_volume_release(vol);
errno = eo;
return NULL;
}
/**
* ntfs_volume_check_logfile - check logfile on target volume
* @vol: volume on which to check logfile
*
* Return 0 on success and -1 on error with errno set error code.
*/
static int ntfs_volume_check_logfile(ntfs_volume *vol)
{
ntfs_inode *ni;
ntfs_attr *na = NULL;
RESTART_PAGE_HEADER *rp = NULL;
int err = 0;
ni = ntfs_inode_open(vol, FILE_LogFile);
if (!ni) {
ntfs_log_perror("Failed to open inode FILE_LogFile");
errno = EIO;
return -1;
}
na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
if (!na) {
ntfs_log_perror("Failed to open $FILE_LogFile/$DATA");
err = EIO;
goto out;
}
if (!ntfs_check_logfile(na, &rp) || !ntfs_is_logfile_clean(na, rp))
err = EOPNOTSUPP;
/*
* If the latest restart page was identified as version
* 2.0, then Windows may have kept a cached copy of
* metadata for fast restarting, and we should not mount.
* Hibernation will be seen the same way on a non
* Windows-system partition, so we have to use the same
* error code (EPERM).
* The restart page may also be identified as version 2.0
* when access to the file system is terminated abruptly
* by unplugging or power cut, so mounting is also rejected
* after such an event.
*/
if (rp
&& (rp->major_ver == const_cpu_to_le16(2))
&& (rp->minor_ver == const_cpu_to_le16(0))) {
ntfs_log_error("Metadata kept in Windows cache, refused to mount.\n");
err = EPERM;
}
free(rp);
ntfs_attr_close(na);
out:
if (ntfs_inode_close(ni))
ntfs_error_set(&err);
if (err) {
errno = err;
return -1;
}
return 0;
}
/**
* ntfs_hiberfile_open - Find and open '/hiberfil.sys'
* @vol: An ntfs volume obtained from ntfs_mount
*
* Return: inode Success, hiberfil.sys is valid
* NULL hiberfil.sys doesn't exist or some other error occurred
*/
static ntfs_inode *ntfs_hiberfile_open(ntfs_volume *vol)
{
u64 inode;
ntfs_inode *ni_root;
ntfs_inode *ni_hibr = NULL;
ntfschar *unicode = NULL;
int unicode_len;
const char *hiberfile = "hiberfil.sys";
if (!vol) {
errno = EINVAL;
return NULL;
}
ni_root = ntfs_inode_open(vol, FILE_root);
if (!ni_root) {
ntfs_log_debug("Couldn't open the root directory.\n");
return NULL;
}
unicode_len = ntfs_mbstoucs(hiberfile, &unicode);
if (unicode_len < 0) {
ntfs_log_perror("Couldn't convert 'hiberfil.sys' to Unicode");
goto out;
}
inode = ntfs_inode_lookup_by_name(ni_root, unicode, unicode_len);
if (inode == (u64)-1) {
ntfs_log_debug("Couldn't find file '%s'.\n", hiberfile);
goto out;
}
inode = MREF(inode);
ni_hibr = ntfs_inode_open(vol, inode);
if (!ni_hibr) {
ntfs_log_debug("Couldn't open inode %lld.\n", (long long)inode);
goto out;
}
out:
if (ntfs_inode_close(ni_root)) {
ntfs_inode_close(ni_hibr);
ni_hibr = NULL;
}
free(unicode);
return ni_hibr;
}
#define NTFS_HIBERFILE_HEADER_SIZE 4096
/**
* ntfs_volume_check_hiberfile - check hiberfil.sys whether Windows is
* hibernated on the target volume
* @vol: volume on which to check hiberfil.sys
*
* Return: 0 if Windows isn't hibernated for sure
* -1 otherwise and errno is set to the appropriate value
*/
int ntfs_volume_check_hiberfile(ntfs_volume *vol, int verbose)
{
ntfs_inode *ni;
ntfs_attr *na = NULL;
int bytes_read, err;
char *buf = NULL;
ni = ntfs_hiberfile_open(vol);
if (!ni) {
if (errno == ENOENT)
return 0;
return -1;
}
buf = ntfs_malloc(NTFS_HIBERFILE_HEADER_SIZE);
if (!buf)
goto out;
na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
if (!na) {
ntfs_log_perror("Failed to open hiberfil.sys data attribute");
goto out;
}
bytes_read = ntfs_attr_pread(na, 0, NTFS_HIBERFILE_HEADER_SIZE, buf);
if (bytes_read == -1) {
ntfs_log_perror("Failed to read hiberfil.sys");
goto out;
}
if (bytes_read < NTFS_HIBERFILE_HEADER_SIZE) {
if (verbose)
ntfs_log_error("Hibernated non-system partition, "
"refused to mount.\n");
errno = EPERM;
goto out;
}
if ((memcmp(buf, "hibr", 4) == 0)
|| (memcmp(buf, "HIBR", 4) == 0)) {
if (verbose)
ntfs_log_error("Windows is hibernated, refused to mount.\n");
errno = EPERM;
goto out;
}
/* All right, all header bytes are zero */
errno = 0;
out:
if (na)
ntfs_attr_close(na);
free(buf);
err = errno;
if (ntfs_inode_close(ni))
ntfs_error_set(&err);
errno = err;
return errno ? -1 : 0;
}
/*
* Make sure a LOGGED_UTILITY_STREAM attribute named "$TXF_DATA"
* on the root directory is resident.
* When it is non-resident, the partition cannot be mounted on Vista
* (see http://support.microsoft.com/kb/974729)
*
* We take care to avoid this situation, however this can be a
* consequence of having used an older version (including older
* Windows version), so we had better fix it.
*
* Returns 0 if unneeded or successful
* -1 if there was an error, explained by errno
*/
static int fix_txf_data(ntfs_volume *vol)
{
void *txf_data;
s64 txf_data_size;
ntfs_inode *ni;
ntfs_attr *na;
int res;
res = 0;
ntfs_log_debug("Loading root directory\n");
ni = ntfs_inode_open(vol, FILE_root);
if (!ni) {
ntfs_log_perror("Failed to open root directory");
res = -1;
} else {
/* Get the $TXF_DATA attribute */
na = ntfs_attr_open(ni, AT_LOGGED_UTILITY_STREAM, TXF_DATA, 9);
if (na) {
if (NAttrNonResident(na)) {
/*
* Fix the attribute by truncating, then
* rewriting it.
*/
ntfs_log_debug("Making $TXF_DATA resident\n");
txf_data = ntfs_attr_readall(ni,
AT_LOGGED_UTILITY_STREAM,
TXF_DATA, 9, &txf_data_size);
if (txf_data) {
if (ntfs_attr_truncate(na, 0)
|| (ntfs_attr_pwrite(na, 0,
txf_data_size, txf_data)
!= txf_data_size))
res = -1;
free(txf_data);
}
if (res)
ntfs_log_error("Failed to make $TXF_DATA resident\n");
else
ntfs_log_error("$TXF_DATA made resident\n");
}
ntfs_attr_close(na);
}
if (ntfs_inode_close(ni)) {
ntfs_log_perror("Failed to close root");
res = -1;
}
}
return (res);
}
/**
* ntfs_device_mount - open ntfs volume
* @dev: device to open
* @flags: optional mount flags
*
* This function mounts an ntfs volume. @dev should describe the device which
* to mount as the ntfs volume.
*
* @flags is an optional second parameter. The same flags are used as for
* the mount system call (man 2 mount). Currently only the following flag
* is implemented:
* NTFS_MNT_RDONLY - mount volume read-only
*
* The function opens the device @dev and verifies that it contains a valid
* bootsector. Then, it allocates an ntfs_volume structure and initializes
* some of the values inside the structure from the information stored in the
* bootsector. It proceeds to load the necessary system files and completes
* setting up the structure.
*
* Return the allocated volume structure on success and NULL on error with
* errno set to the error code.
*/
ntfs_volume *ntfs_device_mount(struct ntfs_device *dev, ntfs_mount_flags flags)
{
s64 l;
ntfs_volume *vol;
u8 *m = NULL, *m2 = NULL;
ntfs_attr_search_ctx *ctx = NULL;
ntfs_inode *ni;
ntfs_attr *na;
ATTR_RECORD *a;
VOLUME_INFORMATION *vinf;
ntfschar *vname;
u32 record_size;
int i, j, eo;
unsigned int k;
u32 u;
BOOL need_fallback_ro;
need_fallback_ro = FALSE;
vol = ntfs_volume_startup(dev, flags);
if (!vol)
return NULL;
/* Load data from $MFT and $MFTMirr and compare the contents. */
m = ntfs_malloc(vol->mftmirr_size << vol->mft_record_size_bits);
m2 = ntfs_malloc(vol->mftmirr_size << vol->mft_record_size_bits);
if (!m || !m2)
goto error_exit;
l = ntfs_attr_mst_pread(vol->mft_na, 0, vol->mftmirr_size,
vol->mft_record_size, m);
if (l != vol->mftmirr_size) {
if (l == -1)
ntfs_log_perror("Failed to read $MFT");
else {
ntfs_log_error("Failed to read $MFT, unexpected length "
"(%lld != %d).\n", (long long)l,
vol->mftmirr_size);
errno = EIO;
}
goto error_exit;
}
for (i = 0; (i < l) && (i < FILE_first_user); ++i)
if (ntfs_mft_record_check(vol, FILE_MFT + i,
(MFT_RECORD*)(m + i*vol->mft_record_size)))
goto error_exit;
l = ntfs_attr_mst_pread(vol->mftmirr_na, 0, vol->mftmirr_size,
vol->mft_record_size, m2);
if (l != vol->mftmirr_size) {
if (l == -1) {
ntfs_log_perror("Failed to read $MFTMirr");
goto error_exit;
}
vol->mftmirr_size = l;
}
for (i = 0; (i < l) && (i < FILE_first_user); ++i)
if (ntfs_mft_record_check(vol, FILE_MFT + i,
(MFT_RECORD*)(m2 + i*vol->mft_record_size)))
goto error_exit;
ntfs_log_debug("Comparing $MFTMirr to $MFT...\n");
/* Windows 10 does not update the full $MFTMirr any more */
for (i = 0; (i < vol->mftmirr_size) && (i < FILE_first_user); ++i) {
MFT_RECORD *mrec, *mrec2;
const char *ESTR[12] = { "$MFT", "$MFTMirr", "$LogFile",