- Update to r53061
[reactos.git] / drivers / filesystems / ntfs / rw.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/rw.c
22 * PURPOSE: CDROM (ISO 9660) filesystem driver
23 * PROGRAMMER: Art Yerkes
24 * UPDATE HISTORY:
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include <ntddk.h>
30
31 #define NDEBUG
32 #include <debug.h>
33
34 #include "ntfs.h"
35
36
37 /* GLOBALS *******************************************************************/
38
39 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
40 #define ROUND_DOWN(N, S) ((N) - ((N) % (S)))
41
42
43 /* FUNCTIONS ****************************************************************/
44
45 static NTSTATUS
46 NtfsReadFile(PDEVICE_EXTENSION DeviceExt,
47 PFILE_OBJECT FileObject,
48 PUCHAR Buffer,
49 ULONG Length,
50 ULONG ReadOffset,
51 ULONG IrpFlags,
52 PULONG LengthRead)
53 /*
54 * FUNCTION: Reads data from a file
55 */
56 {
57 #if 0
58 NTSTATUS Status = STATUS_SUCCESS;
59 PUCHAR TempBuffer;
60 ULONG TempLength;
61 PCCB Ccb;
62 PFCB Fcb;
63
64 DPRINT("CdfsReadFile(ReadOffset %lu Length %lu)\n", ReadOffset, Length);
65
66 *LengthRead = 0;
67
68 if (Length == 0)
69 return(STATUS_SUCCESS);
70
71 Ccb = (PCCB)FileObject->FsContext2;
72 Fcb = (PFCB)FileObject->FsContext;
73
74 if (ReadOffset >= Fcb->Entry.DataLengthL)
75 return(STATUS_END_OF_FILE);
76
77 DPRINT("Reading %d bytes at %d\n", Length, ReadOffset);
78
79 if (!(IrpFlags & (IRP_NOCACHE|IRP_PAGING_IO)))
80 {
81 LARGE_INTEGER FileOffset;
82 IO_STATUS_BLOCK IoStatus;
83
84 if (ReadOffset + Length > Fcb->Entry.DataLengthL)
85 Length = Fcb->Entry.DataLengthL - ReadOffset;
86 if (FileObject->PrivateCacheMap == NULL)
87 {
88 CcRosInitializeFileCache(FileObject, PAGE_SIZE);
89 }
90
91 FileOffset.QuadPart = (LONGLONG)ReadOffset;
92 CcCopyRead(FileObject,
93 &FileOffset,
94 Length,
95 TRUE,
96 Buffer,
97 &IoStatus);
98 *LengthRead = IoStatus.Information;
99
100 return(IoStatus.Status);
101 }
102
103 if ((ReadOffset % BLOCKSIZE) != 0 || (Length % BLOCKSIZE) != 0)
104 {
105 return STATUS_INVALID_PARAMETER;
106 }
107 if (ReadOffset + Length > ROUND_UP(Fcb->Entry.DataLengthL, BLOCKSIZE))
108 Length = ROUND_UP(Fcb->Entry.DataLengthL, BLOCKSIZE) - ReadOffset;
109
110 Status = CdfsReadSectors(DeviceExt->StorageDevice,
111 Fcb->Entry.ExtentLocationL + (ReadOffset / BLOCKSIZE),
112 Length / BLOCKSIZE,
113 Buffer);
114 if (NT_SUCCESS(Status))
115 {
116 *LengthRead = Length;
117 if (Length + ReadOffset > Fcb->Entry.DataLengthL)
118 {
119 memset(Buffer + Fcb->Entry.DataLengthL - ReadOffset,
120 0, Length + ReadOffset - Fcb->Entry.DataLengthL);
121 }
122 }
123
124 return(Status);
125 #else
126 *LengthRead = 0;
127 return STATUS_END_OF_FILE;
128 #endif
129 }
130
131
132 NTSTATUS NTAPI
133 NtfsFsdRead(PDEVICE_OBJECT DeviceObject,
134 PIRP Irp)
135 {
136 PDEVICE_EXTENSION DeviceExt;
137 PIO_STACK_LOCATION Stack;
138 PFILE_OBJECT FileObject;
139 PVOID Buffer;
140 ULONG ReadLength;
141 LARGE_INTEGER ReadOffset;
142 ULONG ReturnedReadLength = 0;
143 NTSTATUS Status = STATUS_SUCCESS;
144
145 DPRINT("NtfsRead(DeviceObject %x, Irp %x)\n",DeviceObject,Irp);
146
147 DeviceExt = DeviceObject->DeviceExtension;
148 Stack = IoGetCurrentIrpStackLocation(Irp);
149 FileObject = Stack->FileObject;
150
151 ReadLength = Stack->Parameters.Read.Length;
152 ReadOffset = Stack->Parameters.Read.ByteOffset;
153 Buffer = MmGetSystemAddressForMdl(Irp->MdlAddress);
154
155 Status = NtfsReadFile(DeviceExt,
156 FileObject,
157 Buffer,
158 ReadLength,
159 ReadOffset.u.LowPart,
160 Irp->Flags,
161 &ReturnedReadLength);
162
163 if (NT_SUCCESS(Status))
164 {
165 if (FileObject->Flags & FO_SYNCHRONOUS_IO)
166 {
167 FileObject->CurrentByteOffset.QuadPart =
168 ReadOffset.QuadPart + ReturnedReadLength;
169 }
170 Irp->IoStatus.Information = ReturnedReadLength;
171 }
172 else
173 {
174 Irp->IoStatus.Information = 0;
175 }
176
177 Irp->IoStatus.Status = Status;
178 IoCompleteRequest(Irp,IO_NO_INCREMENT);
179
180 return(Status);
181 }
182
183
184 NTSTATUS NTAPI
185 NtfsFsdWrite(PDEVICE_OBJECT DeviceObject,
186 PIRP Irp)
187 {
188 DPRINT("NtfwWrite(DeviceObject %x Irp %x)\n",DeviceObject,Irp);
189
190 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
191 Irp->IoStatus.Information = 0;
192 return(STATUS_NOT_SUPPORTED);
193 }
194
195 /* EOF */