2008-02-10 11:20:29 +00:00
|
|
|
/*
|
|
|
|
* ReactOS kernel
|
2014-11-02 21:50:40 +00:00
|
|
|
* Copyright (C) 2008, 2014 ReactOS Team
|
2008-02-10 11:20:29 +00:00
|
|
|
*
|
|
|
|
* This program 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 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; if not, write to the Free Software
|
2008-03-08 21:45:51 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
2008-02-10 11:20:29 +00:00
|
|
|
*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
|
|
|
* FILE: drivers/filesystem/ntfs/misc.c
|
|
|
|
* PURPOSE: NTFS filesystem driver
|
|
|
|
* PROGRAMMER: Pierre Schweitzer
|
|
|
|
* UPDATE HISTORY:
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
|
|
|
#include "ntfs.h"
|
|
|
|
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/* FUNCTIONS ****************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FUNCTION: Used with IRP to set them to TopLevelIrp field
|
|
|
|
* ARGUMENTS:
|
|
|
|
* Irp = The IRP to set
|
|
|
|
* RETURNS: TRUE if top level was null, else FALSE
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NtfsIsIrpTopLevel(PIRP Irp)
|
|
|
|
{
|
2013-06-16 12:15:06 +00:00
|
|
|
BOOLEAN ReturnCode = FALSE;
|
2008-02-10 11:20:29 +00:00
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
TRACE_(NTFS, "NtfsIsIrpTopLevel()\n");
|
2008-02-10 11:20:29 +00:00
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
if (IoGetTopLevelIrp() == NULL)
|
|
|
|
{
|
|
|
|
IoSetTopLevelIrp(Irp);
|
|
|
|
ReturnCode = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ReturnCode;
|
2008-02-10 11:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FUNCTION: Allocate and fill an NTFS_IRP_CONTEXT struct in order to use it for IRP
|
|
|
|
* ARGUMENTS:
|
2021-06-11 12:29:21 +00:00
|
|
|
* DeviceObject = Used to fill in struct
|
2008-02-10 11:20:29 +00:00
|
|
|
* Irp = The IRP that need IRP_CONTEXT struct
|
|
|
|
* RETURNS: NULL or PNTFS_IRP_CONTEXT
|
|
|
|
*/
|
|
|
|
PNTFS_IRP_CONTEXT
|
|
|
|
NtfsAllocateIrpContext(PDEVICE_OBJECT DeviceObject,
|
|
|
|
PIRP Irp)
|
|
|
|
{
|
2013-06-16 12:15:06 +00:00
|
|
|
PNTFS_IRP_CONTEXT IrpContext;
|
|
|
|
|
|
|
|
TRACE_(NTFS, "NtfsAllocateIrpContext()\n");
|
|
|
|
|
2015-05-24 15:26:15 +00:00
|
|
|
IrpContext = (PNTFS_IRP_CONTEXT)ExAllocateFromNPagedLookasideList(&NtfsGlobalData->IrpContextLookasideList);
|
2013-06-16 12:15:06 +00:00
|
|
|
if (IrpContext == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
RtlZeroMemory(IrpContext, sizeof(NTFS_IRP_CONTEXT));
|
|
|
|
|
2016-03-26 13:22:32 +00:00
|
|
|
IrpContext->Identifier.Type = NTFS_TYPE_IRP_CONTEXT;
|
2013-06-16 12:15:06 +00:00
|
|
|
IrpContext->Identifier.Size = sizeof(NTFS_IRP_CONTEXT);
|
|
|
|
IrpContext->Irp = Irp;
|
|
|
|
IrpContext->DeviceObject = DeviceObject;
|
2015-05-03 18:25:55 +00:00
|
|
|
IrpContext->Stack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
IrpContext->MajorFunction = IrpContext->Stack->MajorFunction;
|
|
|
|
IrpContext->MinorFunction = IrpContext->Stack->MinorFunction;
|
|
|
|
IrpContext->FileObject = IrpContext->Stack->FileObject;
|
2015-05-03 18:13:16 +00:00
|
|
|
IrpContext->IsTopLevel = (IoGetTopLevelIrp() == Irp);
|
2015-05-10 14:36:00 +00:00
|
|
|
IrpContext->PriorityBoost = IO_NO_INCREMENT;
|
2015-05-10 20:47:44 +00:00
|
|
|
IrpContext->Flags = IRPCONTEXT_COMPLETE;
|
2013-06-16 12:15:06 +00:00
|
|
|
|
2015-05-03 18:25:55 +00:00
|
|
|
if (IrpContext->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL ||
|
|
|
|
IrpContext->MajorFunction == IRP_MJ_DEVICE_CONTROL ||
|
|
|
|
IrpContext->MajorFunction == IRP_MJ_SHUTDOWN ||
|
|
|
|
(IrpContext->MajorFunction != IRP_MJ_CLEANUP &&
|
|
|
|
IrpContext->MajorFunction != IRP_MJ_CLOSE &&
|
2015-05-03 18:17:39 +00:00
|
|
|
IoIsOperationSynchronous(Irp)))
|
|
|
|
{
|
|
|
|
IrpContext->Flags |= IRPCONTEXT_CANWAIT;
|
|
|
|
}
|
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
return IrpContext;
|
2008-02-10 11:20:29 +00:00
|
|
|
}
|
2013-06-16 12:15:06 +00:00
|
|
|
|
2014-10-15 20:36:30 +00:00
|
|
|
VOID
|
|
|
|
NtfsFileFlagsToAttributes(ULONG NtfsAttributes,
|
|
|
|
PULONG FileAttributes)
|
|
|
|
{
|
|
|
|
*FileAttributes = NtfsAttributes;
|
|
|
|
if ((NtfsAttributes & NTFS_FILE_TYPE_DIRECTORY) == NTFS_FILE_TYPE_DIRECTORY)
|
|
|
|
{
|
|
|
|
*FileAttributes = NtfsAttributes & ~NTFS_FILE_TYPE_DIRECTORY;
|
|
|
|
*FileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NtfsAttributes == 0)
|
|
|
|
*FileAttributes = FILE_ATTRIBUTE_NORMAL;
|
|
|
|
}
|
|
|
|
|
2014-12-14 12:24:18 +00:00
|
|
|
PVOID
|
2015-05-25 17:43:04 +00:00
|
|
|
NtfsGetUserBuffer(PIRP Irp,
|
|
|
|
BOOLEAN Paging)
|
2014-12-14 12:24:18 +00:00
|
|
|
{
|
|
|
|
if (Irp->MdlAddress != NULL)
|
|
|
|
{
|
2015-05-25 17:43:04 +00:00
|
|
|
return MmGetSystemAddressForMdlSafe(Irp->MdlAddress, (Paging ? HighPagePriority : NormalPagePriority));
|
2014-12-14 12:24:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Irp->UserBuffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 16:21:53 +00:00
|
|
|
/**
|
|
|
|
* @name NtfsLockUserBuffer
|
|
|
|
* @implemented
|
|
|
|
*
|
|
|
|
* Ensures the IRP has an MDL Address.
|
|
|
|
*
|
|
|
|
* @param Irp
|
|
|
|
* Irp with the UserBuffer that needs locking
|
|
|
|
*
|
|
|
|
* @param Length
|
|
|
|
* Size of the Irp->UserBuffer, in bytes
|
|
|
|
*
|
|
|
|
* @param Operation
|
|
|
|
* What kind of access does the driver need to the buffer. Set to
|
|
|
|
* IoReadAccess, IoWriteAccess, or IoModifyAccess.
|
|
|
|
*
|
|
|
|
* @return
|
2021-06-11 12:29:21 +00:00
|
|
|
* STATUS_SUCCESS in case of success, STATUS_INSUFFICIENT_RESOURCES
|
2016-05-01 16:21:53 +00:00
|
|
|
* or an exception code otherwise.
|
|
|
|
*
|
2021-06-11 12:29:21 +00:00
|
|
|
* @remarks Trevor Thompson shamelessly ripped this from
|
2016-05-01 16:21:53 +00:00
|
|
|
* VfatLockUserBuffer(). Only the name was changed.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NtfsLockUserBuffer(IN PIRP Irp,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN LOCK_OPERATION Operation)
|
|
|
|
{
|
|
|
|
ASSERT(Irp);
|
|
|
|
|
|
|
|
if (Irp->MdlAddress)
|
|
|
|
{
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
IoAllocateMdl(Irp->UserBuffer, Length, FALSE, FALSE, Irp);
|
|
|
|
|
|
|
|
if (!Irp->MdlAddress)
|
|
|
|
{
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
MmProbeAndLockPages(Irp->MdlAddress, Irp->RequestorMode, Operation);
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
IoFreeMdl(Irp->MdlAddress);
|
|
|
|
Irp->MdlAddress = NULL;
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
/* EOF */
|