[SHELL/EXPERIMENTS]
[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: NTFS filesystem driver
23 * PROGRAMMER: Art Yerkes
24 * UPDATE HISTORY:
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include <ntddk.h>
30 #include "ntfs.h"
31
32 #define NDEBUG
33 #include <debug.h>
34
35 /* GLOBALS *******************************************************************/
36
37 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
38 #define ROUND_DOWN(N, S) ((N) - ((N) % (S)))
39
40 /* FUNCTIONS ****************************************************************/
41
42 /*
43 * FUNCTION: Reads data from a file
44 */
45 static
46 NTSTATUS
47 NtfsReadFile(PDEVICE_EXTENSION DeviceExt,
48 PFILE_OBJECT FileObject,
49 PUCHAR Buffer,
50 ULONG Length,
51 ULONG ReadOffset,
52 ULONG IrpFlags,
53 PULONG LengthRead)
54 {
55 #if 0
56 NTSTATUS Status = STATUS_SUCCESS;
57 PUCHAR TempBuffer;
58 ULONG TempLength;
59 PCCB Ccb;
60 PFCB Fcb;
61
62 DPRINT("CdfsReadFile(ReadOffset %lu Length %lu)\n", ReadOffset, Length);
63
64 *LengthRead = 0;
65
66 if (Length == 0)
67 return(STATUS_SUCCESS);
68
69 Ccb = (PCCB)FileObject->FsContext2;
70 Fcb = (PFCB)FileObject->FsContext;
71
72 if (ReadOffset >= Fcb->Entry.DataLengthL)
73 return(STATUS_END_OF_FILE);
74
75 DPRINT("Reading %d bytes at %d\n", Length, ReadOffset);
76
77 if (!(IrpFlags & (IRP_NOCACHE|IRP_PAGING_IO)))
78 {
79 LARGE_INTEGER FileOffset;
80 IO_STATUS_BLOCK IoStatus;
81
82 if (ReadOffset + Length > Fcb->Entry.DataLengthL)
83 Length = Fcb->Entry.DataLengthL - ReadOffset;
84 if (FileObject->PrivateCacheMap == NULL)
85 {
86 CcRosInitializeFileCache(FileObject, PAGE_SIZE);
87 }
88
89 FileOffset.QuadPart = (LONGLONG)ReadOffset;
90 CcCopyRead(FileObject,
91 &FileOffset,
92 Length,
93 TRUE,
94 Buffer,
95 &IoStatus);
96 *LengthRead = IoStatus.Information;
97
98 return(IoStatus.Status);
99 }
100
101 if ((ReadOffset % BLOCKSIZE) != 0 || (Length % BLOCKSIZE) != 0)
102 {
103 return STATUS_INVALID_PARAMETER;
104 }
105 if (ReadOffset + Length > ROUND_UP(Fcb->Entry.DataLengthL, BLOCKSIZE))
106 Length = ROUND_UP(Fcb->Entry.DataLengthL, BLOCKSIZE) - ReadOffset;
107
108 Status = CdfsReadSectors(DeviceExt->StorageDevice,
109 Fcb->Entry.ExtentLocationL + (ReadOffset / BLOCKSIZE),
110 Length / BLOCKSIZE,
111 Buffer);
112 if (NT_SUCCESS(Status))
113 {
114 *LengthRead = Length;
115 if (Length + ReadOffset > Fcb->Entry.DataLengthL)
116 {
117 memset(Buffer + Fcb->Entry.DataLengthL - ReadOffset,
118 0, Length + ReadOffset - Fcb->Entry.DataLengthL);
119 }
120 }
121
122 return(Status);
123 #else
124 UNREFERENCED_PARAMETER(DeviceExt);
125 UNREFERENCED_PARAMETER(FileObject);
126 UNREFERENCED_PARAMETER(Buffer);
127 UNREFERENCED_PARAMETER(Length);
128 UNREFERENCED_PARAMETER(ReadOffset);
129 UNREFERENCED_PARAMETER(IrpFlags);
130 *LengthRead = 0;
131 return STATUS_END_OF_FILE;
132 #endif
133 }
134
135
136 NTSTATUS
137 NTAPI
138 NtfsFsdRead(PDEVICE_OBJECT DeviceObject,
139 PIRP Irp)
140 {
141 PDEVICE_EXTENSION DeviceExt;
142 PIO_STACK_LOCATION Stack;
143 PFILE_OBJECT FileObject;
144 PVOID Buffer;
145 ULONG ReadLength;
146 LARGE_INTEGER ReadOffset;
147 ULONG ReturnedReadLength = 0;
148 NTSTATUS Status = STATUS_SUCCESS;
149
150 DPRINT("NtfsRead(DeviceObject %x, Irp %x)\n",DeviceObject,Irp);
151
152 DeviceExt = DeviceObject->DeviceExtension;
153 Stack = IoGetCurrentIrpStackLocation(Irp);
154 FileObject = Stack->FileObject;
155
156 ReadLength = Stack->Parameters.Read.Length;
157 ReadOffset = Stack->Parameters.Read.ByteOffset;
158 Buffer = MmGetSystemAddressForMdl(Irp->MdlAddress);
159
160 Status = NtfsReadFile(DeviceExt,
161 FileObject,
162 Buffer,
163 ReadLength,
164 ReadOffset.u.LowPart,
165 Irp->Flags,
166 &ReturnedReadLength);
167 if (NT_SUCCESS(Status))
168 {
169 if (FileObject->Flags & FO_SYNCHRONOUS_IO)
170 {
171 FileObject->CurrentByteOffset.QuadPart =
172 ReadOffset.QuadPart + ReturnedReadLength;
173 }
174
175 Irp->IoStatus.Information = ReturnedReadLength;
176 }
177 else
178 {
179 Irp->IoStatus.Information = 0;
180 }
181
182 Irp->IoStatus.Status = Status;
183 IoCompleteRequest(Irp,IO_NO_INCREMENT);
184
185 return Status;
186 }
187
188
189 NTSTATUS
190 NTAPI
191 NtfsFsdWrite(PDEVICE_OBJECT DeviceObject,
192 PIRP Irp)
193 {
194 DPRINT("NtfwWrite(DeviceObject %x Irp %x)\n",DeviceObject,Irp);
195
196 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
197 Irp->IoStatus.Information = 0;
198 return STATUS_NOT_SUPPORTED;
199 }
200
201 /* EOF */