-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathlxtfs.c
More file actions
3804 lines (2871 loc) · 109 KB
/
lxtfs.c
File metadata and controls
3804 lines (2871 loc) · 109 KB
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
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
lxtfs.c
Abstract:
This file contains common test functions for file system tests.
--*/
#include "lxtcommon.h"
#include "lxtfs.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/epoll.h>
#include <sys/inotify.h>
#include <sys/mount.h>
#include <sys/xattr.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <limits.h>
#include <dirent.h>
#include <stdlib.h>
#include <libmount/libmount.h>
#include "lxtmount.h"
#define FS_RENAMEAT_TEST_FILE "file"
#define FS_RENAMEAT_TEST_FILE2 "file2"
#define FS_UTIME_TESTFILE "testfile"
#define FS_UTIME_TESTLINK "testlink"
#define FS_NS_PER_SEC (1000000000ull)
#define FS_NS_PER_NT_UNIT (100ull)
#define FS_UNIX_TIME_2000 (946684800)
#define FS_SECONDS_PER_DAY (86400)
#define FS_FAT_MODIFIED_TIME_PRECISION (2)
#define FS_CURRENT_TIME_ALLOWED_VARIANCE (2)
//
// The following macros are used by LxtFsDeleteCurrentWorkingDirectoryCommon
// and LxtFsDeleteOpenFileCommon.
//
#define FS_DELETE_TEST_DIR_NAME "delete_test"
#define FS_DELETE_TEST_DIR FS_TEST_DIR_PARENT "/" FS_DELETE_TEST_DIR_NAME
#define FS_DELETE_TEST_RENAME_FILE_NAME "/delete_test_file"
#define FS_DELETE_TEST_RENAME_FILE FS_TEST_DIR_PARENT FS_DELETE_TEST_RENAME_FILE_NAME
#define FS_DELETE_TEST_DIR_AT "../" FS_DELETE_TEST_DIR_NAME
#define FS_DELETE_TEST_CHILD "child"
#define FS_DELETE_LINK_SUFFIX " (deleted)"
#define FS_CHILD_PATH_FORMAT "%s/%s"
#define FS_FD_PATH_FORMAT "/proc/self/fd/%d"
#define FS_PROC_SELF_CWD "/proc/self/cwd"
//
// The following macros are used by LxtFsRenameDirCommon.
//
#define FS_RENAME_TEST_DIR "/rename_test"
#define FS_RENAME_TEST_DIR2 "/rename_test2"
#define FS_RENAME_TEST_DIR3 "/rename_test3"
#define FS_RENAME_TEST_FILE "/rename_test_file"
#define FS_RENAME_TEST_DIR_CHILD FS_RENAME_TEST_DIR "/child"
#define FS_RENAME_TEST_DIR_CHILD2 FS_RENAME_TEST_DIR "/child2"
#define FS_RENAME_TEST_DIR2_CHILD FS_RENAME_TEST_DIR2 "/child"
#define FS_RENAME_TEST_DIR2_CHILD2 FS_RENAME_TEST_DIR2 "/child2"
#define FS_RENAME_TEST_DIR_GRANDCHILD FS_RENAME_TEST_DIR_CHILD "/child2"
#define FS_RENAME_TEST_DIR_SLASH "/rename_test_slash/"
#define FS_RENAME_TEST_DIR_SLASH2 "/rename_test_slash2"
#define FS_RENAME_TEST_DIR_SLASH_LINK "/rename_test_slash_link"
#define FS_RENAME_TEST_DIR_SLASH_LINK2 "/rename_test_slash_link2"
//
// Flags for LxtFsTimestampCheckUpdate
//
#define FS_TIMESTAMP_ACCESS (0x1)
#define FS_TIMESTAMP_MODIFY (0x2)
#define FS_TIMESTAMP_CHANGE (0x4)
#define FS_TIMESTAMP_SLEEP_TIME (100000)
#define FS_MOUNT_DRVFS_COMMAND_FORMAT "mount -t drvfs %s %s -o rw,noatime"
#define FS_MOUNT_DRVFS_OPTIONS_COMMAND_FORMAT FS_MOUNT_DRVFS_COMMAND_FORMAT ",%s"
#define FS_PLAN9_UNC_PREFIX "UNC\\"
#define FS_PLAN9_UNC_PREFIX_LENGTH (sizeof(FS_PLAN9_UNC_PREFIX) - 1)
#define FS_UNC_PATH_PREFIX_LENGTH (2)
typedef struct _BASIC_TEST_CASE
{
struct timespec SetTime[2];
struct timespec ExpectTime[2];
} BASIC_TEST_CASE, *PBASIC_TEST_CASE;
enum
{
NameVariationFullName,
NameVariationCwdRelative,
NameVariationRelative,
NameVariationDescriptor,
NameVariationFullFileViaLink,
NameVariationCwdRelativeViaLink,
NameVariationRelativeViaLink,
NameVariationDescriptorViaLink,
NameVariationFullFileOnLink,
NameVariationCwdRelativeOnLink,
NameVariationRelativeOnLink,
NameVariationMax = NameVariationRelativeOnLink,
NameVariationFatMax = NameVariationDescriptor
};
struct linux_dirent
{
unsigned long d_ino;
unsigned long d_off;
unsigned short d_reclen;
char d_name[];
// char pad;
// char d_type;
};
int LxtFsDeleteCurrentWorkingDirectoryHelper(char* BaseDir, char* DeleteTestDir, int Flags);
int LxtFsDeleteOpenFileHelper(int Fd, char* BaseDir, char* DeleteTestDir, int Flags);
int LxtFsTimestampCheckCurrent(const struct timespec* Timestamp);
int LxtFsTimestampCheckEqual(const struct timespec* Timestamp1, const struct timespec* Timestamp2);
int LxtFsTimestampCheckGreater(const struct timespec* Timestamp1, const struct timespec* Timestamp2);
int LxtFsTimestampCheckUpdate(const char* Path, struct stat* PreviousStat, int Flags);
long long LxtFsTimestampDiff(const struct timespec* Timestamp1, const struct timespec* Timestamp2);
bool LxtFsUtimeDoTimesMatch(const struct timespec* Actual, const struct timespec* Expected, int AllowedVarianceSeconds);
void LxtFsUtimeRoundToFatAccessTime(struct timespec* Timespec);
void LxtFsUtimeRoundToFatModifiedTime(struct timespec* Timespec);
void LxtFsUtimeRoundToNt(struct timespec* Timespec);
//
// All real timestamps are offset from the year 2000 because FAT can only
// accept timestamps afer 1980.
BASIC_TEST_CASE BasicTestCases[] = {
{{{FS_UNIX_TIME_2000 + 1111111, 2222222}, {FS_UNIX_TIME_2000 + 3333333, 4444444}},
{{FS_UNIX_TIME_2000 + 1111111, 2222222}, {FS_UNIX_TIME_2000 + 3333333, 4444444}}},
{{{5555555, UTIME_OMIT}, {FS_UNIX_TIME_2000 + 6666666, 7777777}},
{{FS_UNIX_TIME_2000 + 1111111, 2222222}, {FS_UNIX_TIME_2000 + 6666666, 7777777}}},
{{{FS_UNIX_TIME_2000 + 5555555, 8888888}, {9999999, UTIME_OMIT}},
{{FS_UNIX_TIME_2000 + 5555555, 8888888}, {FS_UNIX_TIME_2000 + 6666666, 7777777}}},
{{{1111111, UTIME_NOW}, {FS_UNIX_TIME_2000 + 2222222, 3333333}}, {{5555555, UTIME_NOW}, {FS_UNIX_TIME_2000 + 2222222, 3333333}}},
{{{FS_UNIX_TIME_2000 + 1111111, 22222222}, {3333333, UTIME_NOW}}, {{FS_UNIX_TIME_2000 + 1111111, 22222222}, {4444444, UTIME_NOW}}},
{{{1111111, UTIME_NOW}, {3333333, UTIME_NOW}}, {{2222222, UTIME_NOW}, {4444444, UTIME_NOW}}},
{{{0, UTIME_NOW}, {3333333, UTIME_NOW}}, {{0, UTIME_NOW}, {4444444, UTIME_NOW}}},
//
// This time is at 1am UTC, which is likely to be in the previous day local
// time (if the test is run on a system in the US). Having this value here
// ensures the test handles that correctly for FAT timestamp rounding in
// case it occurs for the current time.
//
{{{1498440508, 22222222}, {3333333, UTIME_NOW}}, {{1498440508, 22222222}, {4444444, UTIME_NOW}}},
};
LXT_FS_INFO g_LxtFsInfo;
int LxtFsCheckDrvFsMount(const char* Source, const char* Target, const char* Options, int ParentId, const char* MountRoot)
/*++
Description:
This routine verifies the mount options for a drvfs mount.
N.B. On WSL 2 this uses 9p mount options.
Arguments:
Source - Supplies the mount source.
Target - Supplies the mount target.
Options - Supplies optional mount options.
ParentId - Supplies the expected parent ID of the mount.
MountRoot - Supplies the expected mount root.
Return Value:
Returns 0 on success, -1 on failure.
--*/
{
char ExpectedOptions[1024];
char ExpectedCombinedOptions[1024];
LXT_FS_INFO FsInfo;
size_t Index;
size_t Length;
char Plan9Options[1024];
const char* Plan9Source;
int Result;
char Temp[1024];
char* UncSource = NULL;
LxtCheckResult(LxtFsGetFsInfo(Target, &FsInfo));
if (FsInfo.FsType == LxtFsTypeDrvFs)
{
if (Options == NULL)
{
Options = "case=off";
}
snprintf(ExpectedOptions, sizeof(ExpectedOptions), "rw,%s", Options);
snprintf(ExpectedCombinedOptions, sizeof(ExpectedOptions), "rw,noatime,%s", Options);
LxtCheckResult(MountCheckIsMount(Target, ParentId, Source, "drvfs", MountRoot, "rw,noatime", ExpectedOptions, ExpectedCombinedOptions, 0));
}
else if (FsInfo.FsType == LxtFsTypePlan9)
{
if (Options == NULL)
{
Temp[0] = '\0';
}
else
{
Temp[0] = ';';
strncpy(Temp + 1, Options, sizeof(Temp) - 1);
for (Index = 0; Index < strlen(Temp); Index += 1)
{
if (Temp[Index] == ',')
{
Temp[Index] = ';';
}
}
}
Length = strlen(Source);
if ((Length >= FS_UNC_PATH_PREFIX_LENGTH) && ((Source[0] == '/') || (Source[0] == '\\')) &&
((Source[1] == '/') || (Source[1] == '\\')))
{
Length -= FS_UNC_PATH_PREFIX_LENGTH;
UncSource = malloc(Length + FS_PLAN9_UNC_PREFIX_LENGTH + 1);
if (UncSource == NULL)
{
LxtLogError("malloc");
Result = -1;
goto ErrorExit;
}
memcpy(UncSource, FS_PLAN9_UNC_PREFIX, FS_PLAN9_UNC_PREFIX_LENGTH);
memcpy(&UncSource[FS_PLAN9_UNC_PREFIX_LENGTH], &Source[FS_UNC_PATH_PREFIX_LENGTH], Length);
UncSource[Length + FS_PLAN9_UNC_PREFIX_LENGTH] = '\0';
Plan9Source = UncSource;
}
else
{
Plan9Source = Source;
}
if (FsInfo.Flags.VirtIo != 0)
{
Source = "drvfsa";
snprintf(
Plan9Options,
sizeof(Plan9Options),
"aname=drvfs;path=%s%s;symlinkroot=/mnt/,cache=0x5,access=client,msize=262144,trans=virtio",
Plan9Source,
Temp);
}
else
{
snprintf(
Plan9Options,
sizeof(Plan9Options),
"aname=drvfs;path=%s%s;symlinkroot=/mnt/,cache=0x5,access=client,msize=65536,trans=fd,rfd=*,wfd=*",
Plan9Source,
Temp);
}
//
// The combined options aren't checked for 9p because the placement of
// noatime by libmount is inconsistent.
//
snprintf(ExpectedOptions, sizeof(ExpectedOptions), "rw,%s", Plan9Options);
LxtCheckResult(MountCheckIsMount(Target, ParentId, Source, "9p", MountRoot, "rw,noatime", ExpectedOptions, NULL, 0));
}
else if (FsInfo.FsType == LxtFsTypeVirtioFs)
{
snprintf(ExpectedOptions, sizeof(ExpectedOptions), "rw");
LxtCheckResult(MountCheckIsMount(Target, ParentId, NULL, "virtiofs", MountRoot, "rw,noatime", ExpectedOptions, NULL, 0));
}
ErrorExit:
free(UncSource);
return Result;
}
int LxtFsCreateTestDir(char* Directory)
/*++
Description:
This routine creates a test directory, succeeding if it already exists.
Arguments:
Directory - Supplies the directory name.
Return Value:
Returns 0 on success, -1 on failure.
--*/
{
int Result;
Result = mkdir(Directory, 0777);
if ((Result < 0) && (errno != EEXIST))
{
LxtLogError("Failed to create directory %s", Directory);
goto ErrorExit;
}
Result = 0;
ErrorExit:
return Result;
}
int LxtFsDeleteCurrentWorkingDirectoryCommon(char* BaseDir, int Flags)
/*++
Description:
This routine tests the behavior if the current working directory is
unlinked.
Arguments:
BaseDir - Supplies the top directory to use for the test.
Flags - Supplies various flags.
Return Value:
Returns 0 on success, -1 on failure.
--*/
{
char DeleteTestDir[PATH_MAX];
char DeleteTestRenameFile[PATH_MAX];
int Fd;
char Path[PATH_MAX];
char* PointerResult;
int Result;
sprintf(DeleteTestDir, "%s/%s", BaseDir, FS_DELETE_TEST_DIR_NAME);
sprintf(DeleteTestRenameFile, "%s/%s", BaseDir, FS_DELETE_TEST_RENAME_FILE_NAME);
//
// Create the directory, change to it, and do a sanity check on the normal
// return values of these functions.
//
Fd = -1;
memset(Path, 0, sizeof(Path));
LxtCheckErrno(Fd = creat(DeleteTestRenameFile, 0666));
LxtCheckErrno(close(Fd));
LxtCheckErrnoZeroSuccess(mkdir(DeleteTestDir, 0777));
LxtCheckErrnoZeroSuccess(chdir(DeleteTestDir));
LxtCheckErrno(LxtGetcwd(Path, sizeof(Path)));
LxtCheckStringEqual(Path, DeleteTestDir);
memset(Path, 0, sizeof(Path));
LxtCheckErrno(readlink(FS_PROC_SELF_CWD, Path, sizeof(Path)));
LxtCheckStringEqual(Path, DeleteTestDir);
//
// Unlinking the directory with "." should fail.
//
LxtCheckErrnoFailure(rmdir("."), EINVAL);
//
// Unlink the directory.
//
LxtCheckErrnoZeroSuccess(rmdir(DeleteTestDir));
//
// Check the behavior.
//
LxtCheckResult(LxtFsDeleteCurrentWorkingDirectoryHelper(BaseDir, DeleteTestDir, Flags));
//
// Nothing should change if a new directory is created with the same name.
//
LxtCheckErrnoZeroSuccess(mkdir(DeleteTestDir, 0777));
LxtCheckResult(LxtFsDeleteCurrentWorkingDirectoryHelper(BaseDir, DeleteTestDir, Flags));
rmdir(DeleteTestDir);
//
// Opening the deleted directory should succeed.
//
// N.B. This currently doesn't work on Plan 9 or virtiofs.
//
if (g_LxtFsInfo.FsType != LxtFsTypePlan9 && g_LxtFsInfo.FsType != LxtFsTypeVirtioFs)
{
LxtCheckErrno(Fd = open(".", O_DIRECTORY | O_RDONLY));
LxtCheckResult(LxtFsDeleteOpenFileHelper(Fd, BaseDir, DeleteTestDir, Flags));
}
//
// Try to chdir to the parent.
//
// N.B. This currently doesn't work on virtiofs.
//
if (g_LxtFsInfo.FsType != LxtFsTypeVirtioFs)
{
LxtCheckErrnoZeroSuccess(chdir(".."));
LxtCheckErrno(LxtGetcwd(Path, sizeof(Path)));
LxtCheckStringEqual(Path, BaseDir);
memset(Path, 0, sizeof(Path));
LxtCheckErrno(readlink(FS_PROC_SELF_CWD, Path, sizeof(Path)));
LxtCheckStringEqual(Path, BaseDir);
}
//
// Try to chdir back to the deleted directory.
//
// N.B. This currently does not work on Plan 9 or virtiofs.
//
if (g_LxtFsInfo.FsType != LxtFsTypePlan9 && g_LxtFsInfo.FsType != LxtFsTypeVirtioFs)
{
LxtCheckErrnoZeroSuccess(fchdir(Fd));
LxtCheckResult(LxtFsDeleteCurrentWorkingDirectoryHelper(BaseDir, DeleteTestDir, Flags));
}
ErrorExit:
if (Fd >= 0)
{
close(Fd);
}
rmdir(DeleteTestDir);
unlink(DeleteTestRenameFile);
return Result;
}
int LxtFsDeleteCurrentWorkingDirectoryHelper(char* BaseDir, char* DeleteTestDir, int Flags)
/*++
Description:
This routine checks if a deleted working directory behaves as expected.
Arguments:
BaseDir - Supplies the top directory to use for the test.
DeleteTestDir - Supplies the path to the delete test root directory.
Flags - Supplies various flags.
Return Value:
Returns 0 on success, -1 on failure.
--*/
{
char DeleteTestDirDeleteSuffix[PATH_MAX];
char DeleteTestRenameFile[PATH_MAX];
char Path[PATH_MAX];
int ParentFd;
char* PointerResult;
int Result;
sprintf(DeleteTestDirDeleteSuffix, "%s%s", DeleteTestDir, FS_DELETE_LINK_SUFFIX);
sprintf(DeleteTestRenameFile, "%s/%s", BaseDir, FS_DELETE_TEST_RENAME_FILE_NAME);
ParentFd = -1;
//
// Check the result of getcwd and /proc/self/cwd
//
memset(Path, 0, sizeof(Path));
LxtCheckErrnoFailure(LxtGetcwd(Path, sizeof(Path)), ENOENT);
memset(Path, 0, sizeof(Path));
LxtCheckErrno(readlink(FS_PROC_SELF_CWD, Path, sizeof(Path)));
LxtCheckStringEqual(Path, DeleteTestDirDeleteSuffix);
//
// Creating a new item in the current working directory should fail.
//
LxtCheckErrnoFailure(open(FS_DELETE_TEST_CHILD, O_CREAT | O_WRONLY, 0777), ENOENT);
LxtCheckErrnoFailure(mkdir(FS_DELETE_TEST_CHILD, 0777), ENOENT);
LxtCheckErrnoFailure(link(BaseDir, FS_DELETE_TEST_CHILD), ENOENT);
LxtCheckErrnoFailure(symlink("/proc", FS_DELETE_TEST_CHILD), ENOENT);
LxtCheckErrnoFailure(rename(DeleteTestRenameFile, "./" FS_DELETE_TEST_CHILD), ENOENT);
if ((Flags & FS_DELETE_DRVFS) == 0)
{
LxtCheckErrnoFailure(mknod(FS_DELETE_TEST_CHILD, S_IFIFO | 0777, 0), ENOENT);
}
//
// Opening the parent should succeed.
//
LxtCheckErrno(ParentFd = open("..", O_DIRECTORY | O_RDONLY));
LxtCheckResult(LxtCheckFdPath(ParentFd, BaseDir));
ErrorExit:
if (ParentFd >= 0)
{
close(ParentFd);
}
return Result;
}
int LxtFsDeleteOpenFileCommon(char* BaseDir, int Flags)
/*++
Description:
This routine tests using unlink and rmdir on a file/directory that's open.
Arguments:
BaseDir - Supplies the top directory to use for the test.
Flags - Supplies various flags.
Return Value:
Returns 0 on success, -1 on failure.
--*/
{
int ChildFd;
char ChildPath[PATH_MAX];
char ChildPathDeleteSuffix[PATH_MAX];
char ChildPathSubpath[PATH_MAX];
char DeleteTestDir[PATH_MAX];
char DeleteTestDirAt[PATH_MAX];
char DeleteTestRenameFile[PATH_MAX];
int Fd;
char Path[PATH_MAX];
int ReopenFd;
int Result;
struct stat Stat;
sprintf(DeleteTestDir, "%s/%s", BaseDir, FS_DELETE_TEST_DIR_NAME);
sprintf(DeleteTestDirAt, "../%s", FS_DELETE_TEST_DIR_NAME);
sprintf(DeleteTestRenameFile, "%s/%s", BaseDir, FS_DELETE_TEST_RENAME_FILE_NAME);
sprintf(ChildPath, "%s/%s", DeleteTestDir, FS_DELETE_TEST_CHILD);
sprintf(ChildPathDeleteSuffix, "%s%s", ChildPath, FS_DELETE_LINK_SUFFIX);
Fd = -1;
ChildFd = -1;
ReopenFd = -1;
LxtCheckErrno(Fd = creat(DeleteTestRenameFile, 0666));
LxtCheckErrno(close(Fd));
LxtCheckErrnoZeroSuccess(mkdir(DeleteTestDir, 0777));
LxtCheckErrno(Fd = open(DeleteTestDir, O_DIRECTORY | O_RDONLY));
//
// It should be possible to create a child in the directory.
//
LxtCheckErrno(ChildFd = openat(Fd, FS_DELETE_TEST_CHILD, O_CREAT | O_WRONLY, 0777));
LxtCheckResult(LxtCheckFdPath(ChildFd, ChildPath));
//
// Unlink the file and check the path indicates it's deleted.
//
LxtCheckErrnoZeroSuccess(unlinkat(Fd, FS_DELETE_TEST_CHILD, 0));
LxtCheckResult(LxtCheckFdPath(ChildFd, ChildPathDeleteSuffix));
//
// Reopening through the file descriptor should work.
//
// N.B Reopening currently doesn't work on Plan 9 or virtiofs.
//
if (g_LxtFsInfo.FsType != LxtFsTypePlan9 && g_LxtFsInfo.FsType != LxtFsTypeVirtioFs)
{
sprintf(Path, FS_FD_PATH_FORMAT, ChildFd);
LxtCheckErrno(ReopenFd = open(Path, O_RDONLY));
LxtCheckResult(LxtCheckFdPath(ReopenFd, ChildPathDeleteSuffix));
//
// Check that fstat on the deleted file returns 0 link count.
//
// N.B. Plan 9 will return ENOENT instead for all the below.
//
LxtCheckErrnoZeroSuccess(fstat(ChildFd, &Stat));
LxtCheckEqual(Stat.st_nlink, 0, "%d");
//
// The target is a file so subpaths don't work.
//
sprintf(ChildPathSubpath, FS_CHILD_PATH_FORMAT, Path, ".");
LxtCheckErrnoFailure(open(ChildPathSubpath, O_RDONLY), ENOTDIR);
sprintf(ChildPathSubpath, FS_CHILD_PATH_FORMAT, Path, "..");
LxtCheckErrnoFailure(open(ChildPathSubpath, O_RDONLY), ENOTDIR);
sprintf(ChildPathSubpath, FS_CHILD_PATH_FORMAT, Path, "foo");
LxtCheckErrnoFailure(open(ChildPathSubpath, O_RDONLY), ENOTDIR);
LxtCheckErrnoZeroSuccess(close(ReopenFd));
ReopenFd = -1;
}
LxtCheckErrnoZeroSuccess(close(ChildFd));
ChildFd = -1;
//
// Unlinking the directory through "." should fail.
//
LxtCheckErrnoFailure(unlinkat(Fd, ".", AT_REMOVEDIR), EINVAL);
//
// Unlink the directory.
//
LxtCheckErrnoZeroSuccess(rmdir(DeleteTestDir));
//
// Trying to re-open the deleted directory should fail.
//
LxtCheckErrnoFailure(ChildFd = openat(Fd, DeleteTestDirAt, O_DIRECTORY | O_RDONLY), ENOENT);
//
// Check behavior is correct after deleting.
//
LxtCheckResult(LxtFsDeleteOpenFileHelper(Fd, BaseDir, DeleteTestDir, Flags));
//
// Even if a directory with the same name is created.
//
LxtCheckErrnoZeroSuccess(mkdir(DeleteTestDir, 0777));
LxtCheckResult(LxtFsDeleteOpenFileHelper(Fd, BaseDir, DeleteTestDir, Flags));
ErrorExit:
if (ReopenFd >= 0)
{
close(ReopenFd);
}
if (ChildFd >= 0)
{
close(ChildFd);
}
if (Fd >= 0)
{
close(Fd);
}
unlink(DeleteTestRenameFile);
unlink(ChildPath);
rmdir(DeleteTestDir);
return Result;
}
int LxtFsDeleteOpenFileHelper(int Fd, char* BaseDir, char* DeleteTestDir, int Flags)
/*++
Description:
This routine checks if a file descriptor pointing to a deleted directory
behaves as expected.
Arguments:
Fd - Supplies the file descriptor.
BaseDir - Supplies the top directory to use for the test.
DeleteTestDir - Supplies the path to the delete test root directory.
Flags - Supplies various flags.
Return Value:
Returns 0 on success, -1 on failure.
--*/
{
char ChildPathSubpath[PATH_MAX];
char DeleteTestDirDeleteSuffix[PATH_MAX];
char DeleteTestRenameFile[PATH_MAX];
int ParentFd;
char Path[PATH_MAX];
int ReopenFd;
int Result;
sprintf(DeleteTestDirDeleteSuffix, "%s%s", DeleteTestDir, FS_DELETE_LINK_SUFFIX);
sprintf(DeleteTestRenameFile, "%s/%s", BaseDir, FS_DELETE_TEST_RENAME_FILE_NAME);
ParentFd = -1;
ReopenFd = -1;
//
// Check the path indicates it's deleted.
//
LxtCheckResult(LxtCheckFdPath(Fd, DeleteTestDirDeleteSuffix));
//
// Check that creating new items fails as expected.
//
LxtCheckErrnoFailure(openat(Fd, FS_DELETE_TEST_CHILD, O_CREAT | O_WRONLY, 0666), ENOENT);
LxtCheckErrnoFailure(mkdirat(Fd, FS_DELETE_TEST_CHILD, 0777), ENOENT);
LxtCheckErrnoFailure(linkat(AT_FDCWD, BaseDir, Fd, FS_DELETE_TEST_CHILD, 0), ENOENT);
LxtCheckErrnoFailure(symlinkat("/proc", Fd, FS_DELETE_TEST_CHILD), ENOENT);
LxtCheckErrnoFailure(renameat(AT_FDCWD, DeleteTestRenameFile, Fd, FS_DELETE_TEST_CHILD), ENOENT);
//
// Drvfs doesn't support mknod.
//
if ((Flags & FS_DELETE_DRVFS) == 0)
{
LxtCheckErrnoFailure(mknodat(Fd, FS_DELETE_TEST_CHILD, S_IFIFO | 0777, 0), ENOENT);
}
//
// Navigating to the parent from the deleted directory should succeed.
//
LxtCheckErrno(ParentFd = openat(Fd, "..", O_DIRECTORY | O_RDONLY));
LxtCheckResult(LxtCheckFdPath(ParentFd, BaseDir));
//
// Reopening through the file descriptor should work.
//
sprintf(ChildPathSubpath, FS_CHILD_PATH_FORMAT, Path, "foo");
LxtCheckErrnoFailure(open(ChildPathSubpath, O_RDONLY), ENOENT);
ErrorExit:
if (ParentFd >= 0)
{
close(ParentFd);
}
if (ReopenFd >= 0)
{
close(ReopenFd);
}
return Result;
}
int LxtFsDeleteLoopCommon(const char* BaseDir)
/*++
Description:
This routine tests deleting files in a loop with multiple getdents calls.
Arguments:
BaseDir - Supplies the base directory.
Return Value:
Returns 0 on success, -1 on failure.
--*/
{
char Buffer[512];
int BytesRead;
int Calls;
int Count;
struct dirent64* Entry;
int Fd;
const int FileCount = 500;
int Index;
char Path[PATH_MAX];
int Result;
Fd = -1;
//
// Create the directory and the test files.
//
LxtCheckErrnoZeroSuccess(mkdir(BaseDir, 0777));
for (Index = 0; Index < FileCount; Index += 1)
{
snprintf(Path, sizeof(Path), "%s/file%d", BaseDir, Index);
LxtCheckErrno(Fd = creat(Path, 0666));
LxtCheckClose(Fd);
}
//
// List the directory, and delete the files in between calls.
//
Calls = 0;
Count = 0;
LxtCheckErrno(Fd = open(BaseDir, O_RDONLY | O_DIRECTORY));
LxtCheckErrno(BytesRead = LxtGetdents64(Fd, Buffer, sizeof(Buffer)));
while (BytesRead != 0)
{
Calls += 1;
Index = 0;
while (Index < BytesRead)
{
Entry = (struct dirent64*)&Buffer[Index];
if ((strcmp(Entry->d_name, ".") != 0) && (strcmp(Entry->d_name, "..") != 0))
{
snprintf(Path, sizeof(Path), "%s/%s", BaseDir, Entry->d_name);
LxtCheckErrnoZeroSuccess(unlink(Path));
Count += 1;
}
Index += Entry->d_reclen;
}
LxtCheckErrno(BytesRead = LxtGetdents64(Fd, Buffer, sizeof(Buffer)));
}
//
// Make sure all files were deleted, and that more than one getdents call
// was used (otherwise the test is meaningless).
//
LxtCheckEqual(Count, FileCount, "%d");
LxtCheckGreater(Calls, 1, "%d");
LxtLogInfo("Calls: %d", Calls);
LxtCheckClose(Fd);
//
// The directory is now empty so it can be removed.
//
LxtCheckErrnoZeroSuccess(rmdir(BaseDir));
ErrorExit:
if (Fd >= 0)
{
close(Fd);
}
rmdir(BaseDir);
return Result;
}
int LxtFsGetDentsAlignmentCommon(const char* BaseDir, int Flags)
/*++
Description:
This routine tests the alignment and padding of the entries returned by
getdents.
Arguments:
BaseDir - Supplies the directory to use.
Flags - Supplies the flags.
Return Value:
Returns 0 on success, -1 on failure.
--*/
{
char Buffer[4096];
int Count;
struct linux_dirent* Entry;
struct dirent64* Entry64;
int Fd;
int Fd2;
int Index;
int Length;
const int MaxChildLength = 16;
char Name[MaxChildLength + 1];
int Offset;
int Result;
int Size;
Fd = -1;
Fd2 = -1;
LxtCheckErrnoZeroSuccess(mkdir(BaseDir, 0777));
LxtCheckErrno(Fd = open(BaseDir, O_RDONLY | O_DIRECTORY));
//
// No need to create entries with length 1 and 2 since the . and .. entries
// already take care of that.
//
for (Length = 3; Length <= MaxChildLength; Length += 1)
{
for (Index = 0; Index < Length; Index += 1)
{
Name[Index] = 'a' + Index;
}
Name[Length] = '\0';
LxtCheckErrnoZeroSuccess(mkdirat(Fd, Name, 0777));
}
if ((Flags & FS_TEST_GETDENTS64) == 0)
{
#ifdef __NR_getdents
LxtCheckErrno(Size = LxtGetdents(Fd, Buffer, sizeof(Buffer)));
#else
LxtLogError("Test not supported on this architecture.");
Result = LXT_RESULT_FAILURE;
goto ErrorExit;
#endif
}
else
{
LxtCheckErrno(Size = LxtGetdents64(Fd, Buffer, sizeof(Buffer)));
}
LxtCheckGreater(Size, 0, "%d");
LxtCheckEqual(Size % sizeof(long), 0, "%d");
Offset = 0;
Count = 0;
while (Offset < Size)
{
//
// Verify the record length of each entry.
//
// N.B. These sizes are precalculated for amd64; they may need to be
// adjusted on different architectures.
//