-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathiobase.c
More file actions
5981 lines (4435 loc) · 156 KB
/
Copy pathiobase.c
File metadata and controls
5981 lines (4435 loc) · 156 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) 2013 Minoca Corp.
This file is licensed under the terms of the GNU General Public License
version 3. Alternative licensing terms are available. Contact
info@minocacorp.com for details. See the LICENSE file at the root of this
project for complete licensing information.
Module Name:
iobase.c
Abstract:
This module implements the base I/O API (open, close, read, write).
Author:
Evan Green 25-Apr-2013
Environment:
Kernel
--*/
//
// ------------------------------------------------------------------- Includes
//
#include <minoca/kernel/kernel.h>
#include <minoca/intrface/disk.h>
#include "iop.h"
#include "pagecach.h"
//
// ---------------------------------------------------------------- Definitions
//
#define IO_RENAME_ATTEMPTS_MAX 10000
//
// ------------------------------------------------------ Data Type Definitions
//
//
// ----------------------------------------------- Internal Function Prototypes
//
KSTATUS
IopOpenPagingDevice (
PDEVICE Device,
ULONG Access,
ULONG Flags,
PPAGING_IO_HANDLE *Handle,
PULONG IoOffsetAlignment,
PULONG IoSizeAlignment,
PULONGLONG IoCapacity
);
KSTATUS
IopClosePagingObject (
PPAGING_IO_HANDLE Handle
);
KSTATUS
IopCreateAnonymousObject (
BOOL FromKernelMode,
ULONG Access,
ULONG Flags,
IO_OBJECT_TYPE TypeOverride,
PVOID OverrideParameter,
FILE_PERMISSIONS CreatePermissions,
PPATH_POINT PathPoint
);
KSTATUS
IopPerformIoOperation (
PIO_HANDLE Handle,
PIO_CONTEXT Context
);
KSTATUS
IopPerformPagingIoOperation (
PPAGING_IO_HANDLE Handle,
PIO_CONTEXT Context,
PIRP Irp
);
KSTATUS
IopPerformCharacterDeviceIoOperation (
PIO_HANDLE Handle,
PIO_CONTEXT Context
);
KSTATUS
IopPerformDirectoryIoOperation (
PIO_HANDLE Handle,
PIO_CONTEXT Context
);
KSTATUS
IopAddRelativeDirectoryEntries (
PIO_HANDLE Handle,
PIO_OFFSET Offset,
PIO_BUFFER IoBuffer,
UINTN BufferSize,
PUINTN BytesConsumed
);
VOID
IopFixMountPointDirectoryEntries (
PIO_HANDLE Handle,
PIO_BUFFER IoBuffer,
UINTN BufferSize
);
//
// -------------------------------------------------------------------- Globals
//
//
// Store the global I/O statistics.
//
IO_GLOBAL_STATISTICS IoGlobalStatistics;
//
// Set this boolean to print all open calls.
//
BOOL IoDebugPrintOpens;
//
// ------------------------------------------------------------------ Functions
//
KERNEL_API
KSTATUS
IoOpen (
BOOL FromKernelMode,
PIO_HANDLE Directory,
PCSTR Path,
ULONG PathLength,
ULONG Access,
ULONG Flags,
FILE_PERMISSIONS CreatePermissions,
PIO_HANDLE *Handle
)
/*++
Routine Description:
This routine opens a file, device, pipe, or other I/O object.
Arguments:
FromKernelMode - Supplies a boolean indicating the request is coming from
kernel mode.
Directory - Supplies an optional pointer to an open handle to a directory
for relative paths. Supply NULL to use the current working directory.
Path - Supplies a pointer to the path to open.
PathLength - Supplies the length of the path buffer in bytes, including the
null terminator.
Access - Supplies the desired access permissions to the object. See
IO_ACCESS_* definitions.
Flags - Supplies a bitfield of flags governing the behavior of the handle.
See OPEN_FLAG_* definitions.
CreatePermissions - Supplies the permissions to apply for a created file.
Handle - Supplies a pointer where a pointer to the open I/O handle will be
returned on success.
Return Value:
Status code.
--*/
{
PSTR Separator;
KSTATUS Status;
//
// Do not allow shared memory object names with more than a leading slash.
//
if ((Flags & OPEN_FLAG_SHARED_MEMORY) != 0) {
Separator = RtlStringFindCharacterRight(Path,
PATH_SEPARATOR,
PathLength);
if ((Separator != NULL) && (Separator != Path)) {
Status = STATUS_INVALID_PARAMETER;
goto OpenEnd;
}
}
Status = IopOpen(FromKernelMode,
Directory,
Path,
PathLength,
Access,
Flags,
IoObjectInvalid,
NULL,
CreatePermissions,
Handle);
OpenEnd:
if (IoDebugPrintOpens != FALSE) {
RtlDebugPrint("Open %s: %d\n", Path, Status);
}
return Status;
}
KERNEL_API
KSTATUS
IoOpenDevice (
PDEVICE Device,
ULONG Access,
ULONG Flags,
PIO_HANDLE *Handle,
PULONG IoOffsetAlignment,
PULONG IoSizeAlignment,
PULONGLONG IoCapacity
)
/*++
Routine Description:
This routine opens a device. If the given device is the device meant to
hold the page file, this routine does not prepare the returned I/O handle
for paging operations.
Arguments:
Device - Supplies a pointer to a device to open.
Access - Supplies the desired access permissions to the object. See
IO_ACCESS_* definitions.
Flags - Supplies a bitfield of flags governing the behavior of the handle.
See OPEN_FLAG_* definitions.
Handle - Supplies a pointer that receives the open I/O handle.
IoOffsetAlignment - Supplies a pointer where the alignment requirement in
bytes will be returned for all I/O offsets.
IoSizeAlignment - Supplies a pointer where the alignment requirement for
the size of all transfers (the block size) will be returned for all
I/O requests.
IoCapacity - Supplies the device's total size, in bytes.
Return Value:
Status code.
--*/
{
PFILE_OBJECT FileObject;
PIO_HANDLE IoHandle;
ULONGLONG LocalFileSize;
KSTATUS Status;
ASSERT(KeGetRunLevel() == RunLevelLow);
IoHandle = NULL;
if ((Flags & OPEN_FLAG_PAGING_DEVICE) != 0) {
Status = IopOpenPagingDevice(Device,
Access,
Flags,
(PPAGING_IO_HANDLE *)&IoHandle,
IoOffsetAlignment,
IoSizeAlignment,
IoCapacity);
} else {
//
// Open the device normally.
//
Status = IopOpenDevice(Device, Access, Flags, &IoHandle);
if (!KSUCCESS(Status)) {
goto OpenDeviceEnd;
}
//
// Return the requested data.
//
FileObject = IoHandle->FileObject;
READ_INT64_SYNC(&(FileObject->Properties.FileSize), &LocalFileSize);
if (IoOffsetAlignment != NULL) {
*IoOffsetAlignment = FileObject->Properties.BlockSize;
}
if (IoSizeAlignment != NULL) {
*IoSizeAlignment = FileObject->Properties.BlockSize;
}
if (IoCapacity != NULL) {
*IoCapacity = LocalFileSize;
}
Status = STATUS_SUCCESS;
}
OpenDeviceEnd:
ASSERT((KSUCCESS(Status)) || (IoHandle == NULL));
*Handle = IoHandle;
return Status;
}
KERNEL_API
BOOL
IoIsPagingDevice (
PDEVICE Device
)
/*++
Routine Description:
This routine determines whether or not paging is enabled on the given
device.
Arguments:
Device - Supplies a pointer to a device.
Return Value:
Returns TRUE if paging is enabled on the device, or FALSE otherwise.
--*/
{
if ((Device->Flags & DEVICE_FLAG_PAGING_DEVICE) != 0) {
return TRUE;
}
return FALSE;
}
KERNEL_API
KSTATUS
IoClose (
PIO_HANDLE IoHandle
)
/*++
Routine Description:
This routine closes a file or device.
Arguments:
IoHandle - Supplies a pointer to the I/O handle returned when the file was
opened.
Return Value:
Status code. Close operations can fail if their associated flushes to
the file system fail.
--*/
{
KSTATUS Status;
if (IoHandle == NULL) {
return STATUS_INVALID_PARAMETER;
}
switch (IoHandle->HandleType) {
case IoHandleTypeDefault:
Status = IoIoHandleReleaseReference(IoHandle);
break;
case IoHandleTypePaging:
Status = IopClosePagingObject((PPAGING_IO_HANDLE)IoHandle);
break;
default:
ASSERT(FALSE);
Status = STATUS_INVALID_HANDLE;
break;
}
return Status;
}
KERNEL_API
KSTATUS
IoRead (
PIO_HANDLE Handle,
PIO_BUFFER IoBuffer,
UINTN SizeInBytes,
ULONG Flags,
ULONG TimeoutInMilliseconds,
PUINTN BytesCompleted
)
/*++
Routine Description:
This routine reads from an I/O object.
Arguments:
Handle - Supplies the open I/O handle.
IoBuffer - Supplies a pointer to an I/O buffer where the read data will be
returned on success.
SizeInBytes - Supplies the number of bytes to read.
Flags - Supplies flags regarding the I/O operation. See IO_FLAG_*
definitions.
TimeoutInMilliseconds - Supplies the number of milliseconds that the I/O
operation should be waited on before timing out. Use
WAIT_TIME_INDEFINITE to wait forever on the I/O.
BytesCompleted - Supplies the a pointer where the number of bytes actually
read will be returned.
Return Value:
Status code. A failing status code does not necessarily mean no I/O made it
in or out. Check the bytes completed value to find out how much occurred.
--*/
{
IO_CONTEXT Context;
PIO_HANDLE ReadHandle;
KSTATUS Status;
//
// No-allocate code paths should not be calling I/O read. They should use
// the offset-based read routine.
//
ASSERT((Flags & IO_FLAG_NO_ALLOCATE) == 0);
//
// The special page file no-allocate read operation is not supported by
// this routine. An offset must be supplied for said reads.
//
if ((Flags & IO_FLAG_NO_ALLOCATE) != 0) {
Status = STATUS_INVALID_PARAMETER;
goto ReadEnd;
}
//
// Find the correct I/O handle.
//
if (Handle->HandleType == IoHandleTypePaging) {
ReadHandle = ((PPAGING_IO_HANDLE)Handle)->IoHandle;
} else {
ReadHandle = Handle;
}
Context.IoBuffer = IoBuffer;
Context.Offset = IO_OFFSET_NONE;
Context.SizeInBytes = SizeInBytes;
Context.BytesCompleted = 0;
Context.Flags = Flags;
Context.TimeoutInMilliseconds = TimeoutInMilliseconds;
Context.Write = FALSE;
Status = IopPerformIoOperation(ReadHandle, &Context);
*BytesCompleted = Context.BytesCompleted;
ReadEnd:
return Status;
}
KERNEL_API
KSTATUS
IoWrite (
PIO_HANDLE Handle,
PIO_BUFFER IoBuffer,
UINTN SizeInBytes,
ULONG Flags,
ULONG TimeoutInMilliseconds,
PUINTN BytesCompleted
)
/*++
Routine Description:
This routine writes to an I/O object.
Arguments:
Handle - Supplies the open I/O handle.
IoBuffer - Supplies a pointer to an I/O buffer containing the data to write.
SizeInBytes - Supplies the number of bytes to write.
Flags - Supplies flags regarding the I/O operation. See IO_FLAG_*
definitions.
TimeoutInMilliseconds - Supplies the number of milliseconds that the I/O
operation should be waited on before timing out. Use
WAIT_TIME_INDEFINITE to wait forever on the I/O.
BytesCompleted - Supplies the a pointer where the number of bytes actually
written will be returned.
Return Value:
Status code. A failing status code does not necessarily mean no I/O made it
in or out. Check the bytes completed value to find out how much occurred.
--*/
{
IO_CONTEXT Context;
KSTATUS Status;
PIO_HANDLE WriteHandle;
//
// No-allocate code paths should not be calling I/O write. They should use
// the offset-based write routine.
//
ASSERT((Flags & IO_FLAG_NO_ALLOCATE) == 0);
//
// The special page file no-allocate write operation is not supported by
// this routine. An offset must be supplied for said writes.
//
if ((Flags & IO_FLAG_NO_ALLOCATE) != 0) {
Status = STATUS_INVALID_PARAMETER;
goto WriteEnd;
}
//
// Find the correct I/O handle.
//
if (Handle->HandleType == IoHandleTypePaging) {
WriteHandle = ((PPAGING_IO_HANDLE)Handle)->IoHandle;
} else {
WriteHandle = Handle;
}
Context.IoBuffer = IoBuffer;
Context.Offset = IO_OFFSET_NONE;
Context.SizeInBytes = SizeInBytes;
Context.BytesCompleted = 0;
Context.Flags = Flags;
Context.TimeoutInMilliseconds = TimeoutInMilliseconds;
Context.Write = TRUE;
Status = IopPerformIoOperation(WriteHandle, &Context);
*BytesCompleted = Context.BytesCompleted;
WriteEnd:
return Status;
}
KERNEL_API
KSTATUS
IoReadAtOffset (
PIO_HANDLE Handle,
PIO_BUFFER IoBuffer,
IO_OFFSET Offset,
UINTN SizeInBytes,
ULONG Flags,
ULONG TimeoutInMilliseconds,
PUINTN BytesCompleted,
PIRP Irp
)
/*++
Routine Description:
This routine reads from an I/O object at a specific offset.
Arguments:
Handle - Supplies the open I/O handle.
IoBuffer - Supplies a pointer to an I/O buffer where the read data will be
returned on success.
Offset - Supplies the offset from the beginning of the file or device where
the I/O should be done.
SizeInBytes - Supplies the number of bytes to read.
Flags - Supplies flags regarding the I/O operation. See IO_FLAG_*
definitions.
TimeoutInMilliseconds - Supplies the number of milliseconds that the I/O
operation should be waited on before timing out. Use
WAIT_TIME_INDEFINITE to wait forever on the I/O.
BytesCompleted - Supplies the a pointer where the number of bytes actually
read will be returned.
Irp - Supplies a pointer to the IRP to use for this I/O. This is required
when doing operations on the page file.
Return Value:
Status code. A failing status code does not necessarily mean no I/O made it
in or out. Check the bytes completed value to find out how much occurred.
--*/
{
IO_CONTEXT Context;
PIO_HANDLE ReadHandle;
KSTATUS Status;
//
// Determine the correct read handle. Only perform paging I/O operations
// when operating on the page file. It is not enough to look at the I/O
// handle's open flags. There could be reads from the page file or paging
// device that are not in no-allocate code paths. The caller must dictate
// the no-allocate code path.
//
if ((Handle->HandleType == IoHandleTypePaging) &&
((Flags & IO_FLAG_NO_ALLOCATE) == 0)) {
ReadHandle = ((PPAGING_IO_HANDLE)Handle)->IoHandle;
} else {
ReadHandle = Handle;
}
Context.IoBuffer = IoBuffer;
Context.Offset = Offset;
Context.SizeInBytes = SizeInBytes;
Context.BytesCompleted = 0;
Context.Flags = Flags;
Context.TimeoutInMilliseconds = TimeoutInMilliseconds;
Context.Write = FALSE;
//
// Perform the read operation based on the read handle.
//
switch (ReadHandle->HandleType) {
case IoHandleTypeDefault:
Status = IopPerformIoOperation(ReadHandle, &Context);
break;
case IoHandleTypePaging:
if ((Irp == NULL) || (TimeoutInMilliseconds != WAIT_TIME_INDEFINITE)) {
Status = STATUS_INVALID_PARAMETER;
break;
}
Status = IopPerformPagingIoOperation((PPAGING_IO_HANDLE)ReadHandle,
&Context,
Irp);
break;
default:
ASSERT(FALSE);
Status = STATUS_INVALID_HANDLE;
break;
}
*BytesCompleted = Context.BytesCompleted;
return Status;
}
KERNEL_API
KSTATUS
IoWriteAtOffset (
PIO_HANDLE Handle,
PIO_BUFFER IoBuffer,
IO_OFFSET Offset,
UINTN SizeInBytes,
ULONG Flags,
ULONG TimeoutInMilliseconds,
PUINTN BytesCompleted,
PIRP Irp
)
/*++
Routine Description:
This routine writes to an I/O object at a specific offset.
Arguments:
Handle - Supplies the open I/O handle.
IoBuffer - Supplies a pointer to an I/O buffer containing the data to write.
Offset - Supplies the offset from the beginning of the file or device where
the I/O should be done.
SizeInBytes - Supplies the number of bytes to write.
Flags - Supplies flags regarding the I/O operation. See IO_FLAG_*
definitions.
TimeoutInMilliseconds - Supplies the number of milliseconds that the I/O
operation should be waited on before timing out. Use
WAIT_TIME_INDEFINITE to wait forever on the I/O.
BytesCompleted - Supplies the a pointer where the number of bytes actually
written will be returned.
Irp - Supplies a pointer to the IRP to use for this I/O. This is required
when doing operations on the page file.
Return Value:
Status code. A failing status code does not necessarily mean no I/O made it
in or out. Check the bytes completed value to find out how much occurred.
--*/
{
IO_CONTEXT Context;
KSTATUS Status;
PIO_HANDLE WriteHandle;
//
// Determine the correct write handle. Only perform paging I/O operations
// when operating on the page file. It is not enough to look at the I/O
// handle's open flags. There could be writes to the page file or paging
// device that are not in no-allocate code paths. The caller must dictate
// the no-allocate code path.
//
if ((Handle->HandleType == IoHandleTypePaging) &&
((Flags & IO_FLAG_NO_ALLOCATE) == 0)) {
WriteHandle = ((PPAGING_IO_HANDLE)Handle)->IoHandle;
} else {
WriteHandle = Handle;
}
Context.IoBuffer = IoBuffer;
Context.Offset = Offset;
Context.SizeInBytes = SizeInBytes;
Context.BytesCompleted = 0;
Context.Flags = Flags;
Context.TimeoutInMilliseconds = TimeoutInMilliseconds;
Context.Write = TRUE;
switch (WriteHandle->HandleType) {
case IoHandleTypeDefault:
Status = IopPerformIoOperation(WriteHandle, &Context);
break;
case IoHandleTypePaging:
if ((Irp == NULL) || (TimeoutInMilliseconds != WAIT_TIME_INDEFINITE)) {
Status = STATUS_INVALID_PARAMETER;
break;
}
Status = IopPerformPagingIoOperation((PPAGING_IO_HANDLE)WriteHandle,
&Context,
Irp);
break;
default:
ASSERT(FALSE);
Status = STATUS_INVALID_HANDLE;
break;
}
*BytesCompleted = Context.BytesCompleted;
return Status;
}
KERNEL_API
KSTATUS
IoFlush (
PIO_HANDLE Handle,
IO_OFFSET Offset,
ULONGLONG Size,
ULONG Flags
)
/*++
Routine Description:
This routine flushes I/O data to its appropriate backing device.
Arguments:
Handle - Supplies an open I/O handle. This parameters is not required if
the FLUSH_FLAG_ALL flag is set.
Offset - Supplies the offset from the beginning of the file or device where
the flush should be done.
Size - Supplies the size, in bytes, of the region to flush. Supply a value
of -1 to flush from the given offset to the end of the file.
Flags - Supplies flags regarding the flush operation. See FLUSH_FLAG_*
definitions.
Return Value:
Status code.
--*/
{
PFILE_OBJECT FileObject;
ULONG IoFlags;
KSTATUS Status;
IoFlags = IO_FLAG_DATA_SYNCHRONIZED | IO_FLAG_METADATA_SYNCHRONIZED;
//
// Handle the flush-all synchronous case, where this routine will not
// return until all dirty data made it out to disk.
//
if ((Flags & FLUSH_FLAG_ALL_SYNCHRONOUS) != 0) {
if (Handle != INVALID_HANDLE) {
Status = STATUS_INVALID_PARAMETER;
goto FlushEnd;
}
//
// Flushing synchronously will get all dirty file data and meta-data to
// its underlying block device. It will also flush any dirty block
// device data that has no association with the file layer.
//
Status = IopFlushFileObjects(0, IoFlags, NULL);
goto FlushEnd;
//
// Handle the flush-all case. Just notify the page cache worker to run and
// exit. This does not need to wait until the writes complete.
//
} else if ((Flags & FLUSH_FLAG_ALL) != 0) {
//
// If a handle was provided, something isn't right.
//
if (Handle != INVALID_HANDLE) {
Status = STATUS_INVALID_PARAMETER;
goto FlushEnd;
}
IopSchedulePageCacheThread();
Status = STATUS_SUCCESS;
goto FlushEnd;
}
//
// Otherwise, flush the data for the specific handle. If the data for the
// handle is not in the cache because it's not cacheable, exit successfully.
//
FileObject = Handle->FileObject;
if ((FileObject->Properties.Type == IoObjectTerminalMaster) ||
(FileObject->Properties.Type == IoObjectTerminalSlave)) {
Status = IopTerminalFlush(FileObject, Flags);
if (!KSUCCESS(Status)) {
goto FlushEnd;
}
} else if (IO_IS_FILE_OBJECT_CACHEABLE(FileObject) != FALSE) {
if ((Flags &
(FLUSH_FLAG_READ | FLUSH_FLAG_WRITE | FLUSH_FLAG_DISCARD)) != 0) {
Status = STATUS_INVALID_PARAMETER;
goto FlushEnd;
}
Status = IopFlushFileObject(FileObject,
Offset,
Size,
IoFlags,
TRUE,
NULL);
if (!KSUCCESS(Status)) {
goto FlushEnd;
}
} else {
Status = STATUS_SUCCESS;
goto FlushEnd;
}
FlushEnd:
return Status;
}
KERNEL_API
KSTATUS
IoSeek (
PIO_HANDLE Handle,
SEEK_COMMAND SeekCommand,
IO_OFFSET Offset,
PIO_OFFSET NewOffset
)
/*++
Routine Description:
This routine seeks to the given position in a file. This routine is only
relevant for normal file or block based devices.
Arguments:
Handle - Supplies the open I/O handle.
SeekCommand - Supplies the reference point for the seek offset. Usual
reference points are the beginning of the file, current file position,
and the end of the file.
Offset - Supplies the offset from the reference point to move in bytes.
NewOffset - Supplies an optional pointer where the file position after the
move will be returned on success.
Return Value:
Status code.
--*/
{
PFILE_OBJECT FileObject;
IO_OFFSET FileSize;
IO_OFFSET LocalNewOffset;
IO_OFFSET OldOffset;
IO_OFFSET PreviousOffset;
KSTATUS Status;
ASSERT(KeGetRunLevel() == RunLevelLow);
FileObject = Handle->FileObject;
switch (FileObject->Properties.Type) {
case IoObjectRegularFile:
case IoObjectRegularDirectory:
case IoObjectBlockDevice:
case IoObjectObjectDirectory:
case IoObjectSharedMemoryObject: