/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*-
 * ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
 * Editor Settings: expandtabs and use 4 spaces for indentation */

/*
 * Copyright Spectra Logic Corporation
 * All rights reserved.
 *
 * 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, see
 * <http://www.gnu.org/licenses/>.
 *
 * LIKEWISE SOFTWARE MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING
 * TERMS AS WELL.  IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT
 * WITH LIKEWISE SOFTWARE, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE
 * TERMS OF THAT SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE GNU
 * GENERAL PUBLIC LICENSE, NOTWITHSTANDING THE ABOVE NOTICE.  IF YOU
 * HAVE QUESTIONS, OR WISH TO REQUEST A COPY OF THE ALTERNATE LICENSING
 * TERMS OFFERED BY LIKEWISE SOFTWARE, PLEASE CONTACT LIKEWISE SOFTWARE AT
 * license@likewisesoftware.com
 */

/*
 * Copyright (C) Spectra Logic Corporation. All rights reserved.
 *
 * Module Name:
 *
 *        marshal_info.c
 *
 * Abstract:
 *
 *        Likewise IO (LWIO) - SRV
 *
 *        Protocols API - Shared SMB
 *
 *        MS-FSCC File System type marshalling code
 */

#include "includes.h"

NTSTATUS
SrvMarshalFileSystemVolumeInfo(
    PSRV_EXEC_CONTEXT               pExecContext,
    PSRV_PROCESS_INFO_STATE         pInfoState,
    PGET_INFO_CLASS_DISPATCH_ENTRY  pInfoEntry,
    PBYTE                           pOutBuffer,
    ULONG                           ulBytesAvailable,
    PULONG                          pulBytesUsed
    )
{
    PFILE_FS_VOLUME_INFORMATION pFSVolInfo    = NULL;
    PSMB_FS_VOLUME_INFO_HEADER  pSMBFSVolInfo = NULL;
    ULONG                       ulVolLabelLen = 0;
    ULONG                       ulBytesNeeded = 0;

    pFSVolInfo    = (PFILE_FS_VOLUME_INFORMATION)pInfoState->pData;
    pSMBFSVolInfo = (PSMB_FS_VOLUME_INFO_HEADER)pOutBuffer;

    /* The Volume label is the dynamic portion of what the FS returned. */
    ulVolLabelLen = pInfoState->ulActualDataLen - pInfoEntry->IOStructSize;
    ulBytesNeeded = pInfoEntry->ProtoStructSize + ulVolLabelLen;

    LW_ASSERT(ulBytesAvailable >= ulBytesNeeded);

    pSMBFSVolInfo->bSupportsObjects     = pFSVolInfo->SupportsObjects;
    pSMBFSVolInfo->llVolumeCreationTime = pFSVolInfo->VolumeCreationTime;
    pSMBFSVolInfo->ulVolumeSerialNumber = pFSVolInfo->VolumeSerialNumber;
    pSMBFSVolInfo->ulVolumeLabelLength  = pFSVolInfo->VolumeLabelLength;
    pSMBFSVolInfo->pad = 0;

    memcpy(
        pSMBFSVolInfo->wszVolumeLabel,
        pFSVolInfo->VolumeLabel,
        ulVolLabelLen);

    *pulBytesUsed = ulBytesNeeded;

    return STATUS_SUCCESS;
}

NTSTATUS
SrvMarshalFileSystemSizeInfo(
    PSRV_EXEC_CONTEXT               pExecContext,
    PSRV_PROCESS_INFO_STATE         pInfoState,
    PGET_INFO_CLASS_DISPATCH_ENTRY  pInfoEntry,
    PBYTE                           pOutBuffer,
    ULONG                           ulBytesAvailable,
    PULONG                          pulBytesUsed
    )
{
    PFILE_FS_SIZE_INFORMATION pFSSizeInfo    = NULL;
    PSMB_FS_SIZE_INFO         pSMBFSSizeInfo = NULL;

    pFSSizeInfo    = (PFILE_FS_SIZE_INFORMATION)pInfoState->pData;
    pSMBFSSizeInfo = (PSMB_FS_SIZE_INFO)pOutBuffer;

    pSMBFSSizeInfo->llTotalAllocationUnits = pFSSizeInfo->TotalAllocationUnits;
    pSMBFSSizeInfo->llAvailableAllocationUnits =
        pFSSizeInfo->AvailableAllocationUnits;
    pSMBFSSizeInfo->ulSectorsPerAllocationUnit =
        pFSSizeInfo->SectorsPerAllocationUnit;
    pSMBFSSizeInfo->ulBytesPerSector = pFSSizeInfo->BytesPerSector;

    *pulBytesUsed = pInfoEntry->ProtoStructSize;

    return STATUS_SUCCESS;
}

NTSTATUS
SrvMarshalFileSystemAttributeInfo(
    PSRV_EXEC_CONTEXT               pExecContext,
    PSRV_PROCESS_INFO_STATE         pInfoState,
    PGET_INFO_CLASS_DISPATCH_ENTRY  pInfoEntry,
    PBYTE                           pOutBuffer,
    ULONG                           ulBytesAvailable,
    PULONG                          pulBytesUsed
    )
{
    PFILE_FS_ATTRIBUTE_INFORMATION pFSAttrInfo    = NULL;
    PSMB_FS_ATTRIBUTE_INFO_HEADER  pSMBFSAttrInfo = NULL;
    ULONG                          ulFSNameLen    = 0;
    ULONG                          ulBytesNeeded  = 0;

    pFSAttrInfo    = (PFILE_FS_ATTRIBUTE_INFORMATION)pInfoState->pData;
    pSMBFSAttrInfo = (PSMB_FS_ATTRIBUTE_INFO_HEADER)pOutBuffer;

    /* The FS name is the dynamic portion of what the FS returned. */
    ulFSNameLen = pInfoState->ulActualDataLen - pInfoEntry->IOStructSize;
    ulBytesNeeded = pInfoEntry->ProtoStructSize + ulFSNameLen;

    LW_ASSERT(ulBytesAvailable >= ulBytesNeeded);

    pSMBFSAttrInfo->ulFSAttributes = pFSAttrInfo->FileSystemAttributes;
    pSMBFSAttrInfo->lMaxFilenameLen = pFSAttrInfo->MaximumComponentNameLength;
    pSMBFSAttrInfo->ulFileSystemNameLen = pFSAttrInfo->FileSystemNameLength;

    memcpy(
        pSMBFSAttrInfo->wszFileSystemName,
        pFSAttrInfo->FileSystemName,
        ulFSNameLen);

    *pulBytesUsed = ulBytesNeeded;

    return STATUS_SUCCESS;
}

NTSTATUS
SrvMarshalFileSystemDeviceInfo(
    PSRV_EXEC_CONTEXT               pExecContext,
    PSRV_PROCESS_INFO_STATE         pInfoState,
    PGET_INFO_CLASS_DISPATCH_ENTRY  pInfoEntry,
    PBYTE                           pOutBuffer,
    ULONG                           ulBytesAvailable,
    PULONG                          pulBytesUsed
    )
{
    PFILE_FS_DEVICE_INFORMATION pFSDeviceInfo    = NULL;
    PSMB_FS_DEVICE_INFO         pSMBFSDeviceInfo = NULL;

    pFSDeviceInfo    = (PFILE_FS_DEVICE_INFORMATION)pInfoState->pData;
    pSMBFSDeviceInfo = (PSMB_FS_DEVICE_INFO)pOutBuffer;

    pSMBFSDeviceInfo->ulDeviceType      = pFSDeviceInfo->DeviceType;
    pSMBFSDeviceInfo->ulCharacteristics = pFSDeviceInfo->DeviceCharacteristics;

    *pulBytesUsed = pInfoEntry->ProtoStructSize;

    return STATUS_SUCCESS;
}

NTSTATUS
SrvMarshalFileSystemFullSizeInfo(
    PSRV_EXEC_CONTEXT               pExecContext,
    PSRV_PROCESS_INFO_STATE         pInfoState,
    PGET_INFO_CLASS_DISPATCH_ENTRY  pInfoEntry,
    PBYTE                           pOutBuffer,
    ULONG                           ulBytesAvailable,
    PULONG                          pulBytesUsed
    )
{
    PFILE_FS_FULL_SIZE_INFORMATION  pFSFullSizeInfo = NULL;
    PSMB_FS_FULL_INFO               pSMBFSFullInfo  = NULL;

    pFSFullSizeInfo = (PFILE_FS_FULL_SIZE_INFORMATION)pInfoState->pData;
    pSMBFSFullInfo  = (PSMB_FS_FULL_INFO)pOutBuffer;

    pSMBFSFullInfo->ullTotalAllocationUnits =
        pFSFullSizeInfo->TotalAllocationUnits;
    pSMBFSFullInfo->ullCallerAvailableAllocationUnits =
        pFSFullSizeInfo->CallerAvailableAllocationUnits;
    pSMBFSFullInfo->ullActualAvailableAllocationUnits =
        pFSFullSizeInfo->ActualAvailableAllocationUnits;
    pSMBFSFullInfo->ulSectorsPerAllocationUnit =
        pFSFullSizeInfo->SectorsPerAllocationUnit;
    pSMBFSFullInfo->ulBytesPerSector = pFSFullSizeInfo->BytesPerSector;

    *pulBytesUsed = pInfoEntry->ProtoStructSize;

    return STATUS_SUCCESS;
}
