⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pnp.c

📁 title=Windows NT/2000/XP 一个RAM-DISK文件系统驱动 memo=SwapFs is a driver for Windows NT/2000/XP that let yo
💻 C
字号:
/*
    Functions for Plug and Play and Power Management.
    Copyright (C) 2002 Bo Brant閚.
    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
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <ntddk.h>
#include <ntverp.h>
#include "swapfs.h"

#if (VER_PRODUCTBUILD >= 2195)

#pragma alloc_text(PAGE, SwapFsForwardIrpSynchronously)
#pragma alloc_text(PAGE, SwapFsPnp)

NTSTATUS
SwapFsForwardIrpSynchronously (
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp
    )
{
    KEVENT              event;
    PDEVICE_EXTENSION   device_extension;
    NTSTATUS            status;

    IoCopyCurrentIrpStackLocationToNext(Irp);

    KeInitializeEvent(&event, NotificationEvent, FALSE);

    IoSetCompletionRoutine(
        Irp,
        SwapFsIrpCompletion,
        &event,
        TRUE,
        TRUE,
        TRUE
        );

    device_extension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;

    status = IoCallDriver(device_extension->TargetDeviceObject, Irp);

    if (status == STATUS_PENDING)
    {
        KeWaitForSingleObject(
            &event,
            Executive,
            KernelMode,
            FALSE,
            NULL
            );
        status = Irp->IoStatus.Status;
    }

    return status;
}

NTSTATUS
SwapFsIrpCompletion (
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp,
    IN PVOID            Context
    )
{
    if (Irp->PendingReturned)
    {
        KeSetEvent((PKEVENT) Context, IO_NO_INCREMENT, FALSE);
    }

    return STATUS_MORE_PROCESSING_REQUIRED;
}

NTSTATUS
SwapFsPnp (
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp
    )
{
    PDEVICE_EXTENSION   device_extension;
    PIO_STACK_LOCATION  io_stack;
    NTSTATUS            status;
    BOOLEAN             setPageable;
    BOOLEAN             addPageFile;

    device_extension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;

    io_stack = IoGetCurrentIrpStackLocation(Irp);

    switch (io_stack->MinorFunction)
    {
    case IRP_MN_START_DEVICE:

        status = SwapFsForwardIrpSynchronously(DeviceObject, Irp);
        DeviceObject->Characteristics &= ~FILE_CHARACTERISTICS_PROPAGATED;
        DeviceObject->Characteristics |=
            (device_extension->TargetDeviceObject->Characteristics &
             FILE_CHARACTERISTICS_PROPAGATED);
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
        break;

    case IRP_MN_REMOVE_DEVICE:

        status = SwapFsForwardIrpSynchronously(DeviceObject, Irp);
        IoDetachDevice(device_extension->TargetDeviceObject);
        IoDeleteDevice(DeviceObject);
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
        break;

    case IRP_MN_DEVICE_USAGE_NOTIFICATION:

        if (io_stack->Parameters.UsageNotification.Type != DeviceUsageTypePaging)
        {
            status = SwapFsSendToNextDriver(DeviceObject, Irp);
            break;
        }

        setPageable = FALSE;
        addPageFile = io_stack->Parameters.UsageNotification.InPath;

        status = KeWaitForSingleObject(
            &device_extension->PagingPathCountEvent,
            Executive,
            KernelMode,
            FALSE,
            NULL
            );

        if (!addPageFile &&
            device_extension->PagingPathCount == 1 &&
            !(DeviceObject->Flags & DO_POWER_INRUSH))
        {
            DeviceObject->Flags |= DO_POWER_PAGABLE;
            setPageable = TRUE;
        }

        status = SwapFsForwardIrpSynchronously(DeviceObject, Irp);

        if (NT_SUCCESS(status))
        {
            IoAdjustPagingPathCount(
                &device_extension->PagingPathCount,
                addPageFile
                );

            if (addPageFile && device_extension->PagingPathCount == 1)
            {
                DeviceObject->Flags &= ~DO_POWER_PAGABLE;
            }
        }
        else if (setPageable)
        {
            DeviceObject->Flags &= ~DO_POWER_PAGABLE;
        }

        KeSetEvent(
            &device_extension->PagingPathCountEvent,
            IO_NO_INCREMENT,
            FALSE
            );

        IoCompleteRequest(Irp, IO_NO_INCREMENT);

        break;

    default:
        status = SwapFsSendToNextDriver(DeviceObject, Irp);
    }

    return status;
}

NTSTATUS
SwapFsPower (
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp
    )
{
    PDEVICE_EXTENSION device_extension;

    PoStartNextPowerIrp(Irp);

    IoSkipCurrentIrpStackLocation(Irp);

    device_extension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;

    return PoCallDriver(device_extension->TargetDeviceObject, Irp);
}

#endif // (VER_PRODUCTBUILD >= 2195)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -