VirtualBox

Changeset 78629 in vbox for trunk/src


Ignore:
Timestamp:
May 21, 2019 11:37:43 AM (6 years ago)
Author:
vboxsync
Message:

winnt/vboxsf: DriverEntry cleaning up... bugref:9172

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/WINNT/SharedFolders/driver/vbsf.cpp

    r78628 r78629  
    6565/** Shared folders features (SHFL_FEATURE_XXX). */
    6666uint64_t     g_fSfFeatures = 0;
    67 
    68 
    69 /*********************************************************************************************************************************
    70 *   Exported Functions                                                                                                           *
    71 *********************************************************************************************************************************/
    72 RT_C_DECLS_BEGIN
    73 NTSTATUS _stdcall DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath);
    74 RT_C_DECLS_END
    7567
    7668
     
    597589}
    598590
    599 NTSTATUS DriverEntry(IN PDRIVER_OBJECT  DriverObject,
    600                      IN PUNICODE_STRING RegistryPath)
    601 {
    602     NTSTATUS Status;
    603     UNICODE_STRING VBoxMRxName;
    604     UNICODE_STRING UserModeDeviceName;
    605     PMRX_VBOX_DEVICE_EXTENSION pDeviceExtension = NULL;
    606     ULONG i;
    607     int vrc;
    608 
    609     Log(("VBOXSF: DriverEntry: Driver object %p\n", DriverObject));
    610 
    611     if (!DriverObject)
    612     {
    613         Log(("VBOXSF: DriverEntry: driver object is NULL.\n"));
    614         return STATUS_UNSUCCESSFUL;
    615     }
    616 
    617     /*
    618      * Initialize IPRT.
    619      */
    620     vrc = RTR0Init(0);
    621     if (RT_FAILURE(vrc))
    622     {
    623         Log(("VBOXSF: DriverEntry: RTR0Init failed! %Rrc!\n", vrc));
    624         return STATUS_UNSUCCESSFUL;
    625     }
    626 
    627     /* Initialize VBox subsystem. */
    628     vrc = VbglR0SfInit();
    629     if (RT_FAILURE(vrc))
    630     {
    631         Log(("VBOXSF: DriverEntry: ERROR while initializing VBox subsystem (%Rrc)!\n", vrc));
    632         RTR0Term();
    633         return STATUS_UNSUCCESSFUL;
    634     }
    635 
    636     /* Connect the HGCM client */
    637     vrc = VbglR0SfConnect(&g_SfClient);
    638     if (RT_FAILURE(vrc))
    639     {
    640         Log(("VBOXSF: DriverEntry: ERROR while connecting to host (%Rrc)!\n",
    641              vrc));
    642         VbglR0SfTerm();
    643         RTR0Term();
    644         return STATUS_UNSUCCESSFUL;
    645     }
    646 
    647     vrc = VbglR0QueryHostFeatures(&g_fHostFeatures);
    648     if (RT_FAILURE(vrc))
    649     {
    650         LogRel(("vboxsf: VbglR0QueryHostFeatures failed: vrc=%Rrc (ignored)\n", vrc));
    651         g_fHostFeatures = 0;
    652     }
    653     VbglR0SfHostReqQueryFeaturesSimple(&g_fSfFeatures, &g_uSfLastFunction);
    654     LogRel(("VBoxSF: g_fHostFeatures=%#x g_fSfFeatures=%#RX64 g_uSfLastFunction=%u\n",
    655             g_fHostFeatures, g_fSfFeatures, g_uSfLastFunction));
    656 
    657     if (!VbglR0CanUsePhysPageList())
    658     {
    659         LogRel(("vboxsf: Host does not support physical page lists.  Refuses to load!\n"));
    660         VbglR0SfTerm();
    661         RTR0Term();
    662         return STATUS_UNSUCCESSFUL;
    663     }
    664 
    665     /*
    666      * Resolve newer kernel APIs we might want to use.
    667      * Note! Because of http://www.osronline.com/article.cfm%5eid=494.htm we cannot
    668      *       use MmGetSystemRoutineAddress here as it will crash on xpsp2.
    669      */
    670     RTDBGKRNLINFO hKrnlInfo;
    671     int rc = RTR0DbgKrnlInfoOpen(&hKrnlInfo, 0/*fFlags*/);
    672     AssertLogRelRC(rc);
    673     if (RT_SUCCESS(rc))
    674     {
    675         g_pfnCcCoherencyFlushAndPurgeCache
    676             = (PFNCCCOHERENCYFLUSHANDPURGECACHE)RTR0DbgKrnlInfoGetSymbol(hKrnlInfo, NULL, "CcCoherencyFlushAndPurgeCache");
    677         RTR0DbgKrnlInfoRelease(hKrnlInfo);
    678     }
    679 
    680     /* Init the driver object. */
    681     DriverObject->DriverUnload = VBoxMRxUnload;
    682     for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
    683     {
    684         DriverObject->MajorFunction[i] = (PDRIVER_DISPATCH)VBoxMRxFsdDispatch;
    685     }
    686 
    687     /* Forward to RDBSS. */
    688     Status = RxDriverEntry(DriverObject, RegistryPath);
    689     if (Status != STATUS_SUCCESS)
    690     {
    691         Log(("VBOXSF: DriverEntry: RxDriverEntry failed: 0x%08X\n", Status));
    692         goto failure;
    693     }
    694 
    695     __try
    696     {
    697         Log(("VBOXSF: DriverEntry: RxRegisterMinirdr: calling VBoxMRxDeviceObject %p\n",
    698              VBoxMRxDeviceObject));
    699 
    700         RtlInitUnicodeString(&VBoxMRxName, DD_MRX_VBOX_FS_DEVICE_NAME_U);
    701 
    702         /* Don use RX_REGISTERMINI_FLAG_DONT_PROVIDE_UNCS or else
    703          * UNC mappings don't work (including Windows explorer browsing).
    704          */
    705         Status = RxRegisterMinirdr(&VBoxMRxDeviceObject,
    706                                    DriverObject,
    707                                    &VBoxMRxDispatch,
    708                                    RX_REGISTERMINI_FLAG_DONT_PROVIDE_MAILSLOTS,
    709                                    &VBoxMRxName,
    710                                    sizeof(MRX_VBOX_DEVICE_EXTENSION),
    711                                    FILE_DEVICE_NETWORK_FILE_SYSTEM,
    712                                    FILE_REMOTE_DEVICE);
    713 
    714         Log(("VBOXSF: DriverEntry: RxRegisterMinirdr: returned 0x%08X VBoxMRxDeviceObject %p\n",
    715              Status, VBoxMRxDeviceObject));
    716 
    717         if (Status!=STATUS_SUCCESS)
    718         {
    719             Log(("VBOXSF: DriverEntry: RxRegisterMinirdr failed: 0x%08X\n", Status ));
    720             try_return((void)Status);
    721         }
    722 
    723         /* Init the device extension.
    724          * NOTE: the device extension actually points to fields
    725          * in the RDBSS_DEVICE_OBJECT. Our space is past the end
    726          * of this struct!!
    727          */
    728         pDeviceExtension = (PMRX_VBOX_DEVICE_EXTENSION)((PBYTE)VBoxMRxDeviceObject + sizeof(RDBSS_DEVICE_OBJECT));
    729 
    730         pDeviceExtension->pDeviceObject = VBoxMRxDeviceObject;
    731 
    732         for (i = 0; i < RT_ELEMENTS(pDeviceExtension->cLocalConnections); i++)
    733         {
    734             pDeviceExtension->cLocalConnections[i] = FALSE;
    735         }
    736 
    737         /* Mutex for synchronizining our connection list */
    738         ExInitializeFastMutex(&pDeviceExtension->mtxLocalCon);
    739 
    740         /* The device object has been created. Need to setup a symbolic
    741          * link so that the device may be accessed from a Win32 user mode
    742          * application.
    743          */
    744 
    745         RtlInitUnicodeString(&UserModeDeviceName, DD_MRX_VBOX_USERMODE_SHADOW_DEV_NAME_U);
    746         Log(("VBOXSF: DriverEntry: Calling IoCreateSymbolicLink\n"));
    747         Status = IoCreateSymbolicLink(&UserModeDeviceName, &VBoxMRxName);
    748         if (Status != STATUS_SUCCESS)
    749         {
    750             Log(("VBOXSF: DriverEntry: IoCreateSymbolicLink: 0x%08X\n",
    751                  Status));
    752             try_return((void)Status);
    753         }
    754         Log(("VBOXSF: DriverEntry: Symbolic link created.\n"));
    755 
    756         /*
    757          * Build the dispatch tables for the minirdr
    758          */
    759         vbsfInitMRxDispatch();
    760 
    761     try_exit:
    762          ;
    763     }
    764     __finally
    765     {
    766         ;
    767     }
    768 
    769     if (Status != STATUS_SUCCESS)
    770     {
    771         Log(("VBOXSF: DriverEntry: VBoxSF.sys failed to start with Status = 0x%08X\n",
    772              Status));
    773         goto failure;
    774     }
    775 
    776     AssertPtr(pDeviceExtension);
    777 
    778     /* The redirector driver must intercept the IOCTL to avoid VBOXSVR name resolution
    779      * by other redirectors. These additional name resolutions cause long delays.
    780      */
    781     Log(("VBOXSF: DriverEntry: VBoxMRxDeviceObject = %p, rdbss %p, devext %p\n",
    782          VBoxMRxDeviceObject, DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL], pDeviceExtension));
    783     pDeviceExtension->pfnRDBSSDeviceControl = DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL];
    784     DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VBoxMRXDeviceControl;
    785 
    786     /* Intercept IRP_MJ_CREATE to fix incorrect (wrt NTFS, FAT, ++) return
    787      * codes for NtOpenFile("r:\\asdf\\", FILE_NON_DIRECTORY_FILE).
    788      */
    789     pDeviceExtension->pfnRDBSSCreate = DriverObject->MajorFunction[IRP_MJ_CREATE];
    790     DriverObject->MajorFunction[IRP_MJ_CREATE] = VBoxHookMjCreate;
    791 
    792     /* Intercept IRP_MJ_SET_INFORMATION to ensure we call the host for all
    793      * FileEndOfFileInformation requestes, even if the new size matches the
    794      * old one.  We don't know if someone else might have modified the file
    795      * size cached in the FCB since the last time we update it.
    796      */
    797     pDeviceExtension->pfnRDBSSSetInformation = DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION];
    798     DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] = VBoxHookMjSetInformation;
    799 
    800 
    801     /** @todo start the redirector here RxStartMiniRdr. */
    802 
    803     Log(("VBOXSF: DriverEntry: Init successful!\n"));
    804     return STATUS_SUCCESS;
    805 
    806 failure:
    807 
    808     Log(("VBOXSF: DriverEntry: Failure! Status = 0x%08X\n", Status));
    809 
    810     VbglR0SfDisconnect(&g_SfClient);
    811     VbglR0SfTerm();
    812     RTR0Term();
    813 
    814     if (VBoxMRxDeviceObject)
    815     {
    816         RxUnregisterMinirdr(VBoxMRxDeviceObject);
    817         VBoxMRxDeviceObject = NULL;
    818     }
    819 
    820     return Status;
    821 }
    822591
    823592NTSTATUS VBoxMRxStart(PRX_CONTEXT RxContext, IN OUT PRDBSS_DEVICE_OBJECT RxDeviceObject)
     
    18541623}
    18551624
     1625
     1626/**
     1627 * The "main" function for a driver binary.
     1628 */
     1629extern "C" NTSTATUS NTAPI DriverEntry(IN PDRIVER_OBJECT  DriverObject, IN PUNICODE_STRING RegistryPath)
     1630{
     1631    NTSTATUS Status;
     1632    UNICODE_STRING VBoxMRxName;
     1633    UNICODE_STRING UserModeDeviceName;
     1634    PMRX_VBOX_DEVICE_EXTENSION pDeviceExtension = NULL;
     1635    ULONG i;
     1636    int vrc;
     1637
     1638    Log(("VBOXSF: DriverEntry: Driver object %p\n", DriverObject));
     1639
     1640    if (!DriverObject)
     1641    {
     1642        Log(("VBOXSF: DriverEntry: driver object is NULL.\n"));
     1643        return STATUS_UNSUCCESSFUL;
     1644    }
     1645
     1646    /*
     1647     * Initialize IPRT.
     1648     */
     1649    vrc = RTR0Init(0);
     1650    if (RT_FAILURE(vrc))
     1651    {
     1652        Log(("VBOXSF: DriverEntry: RTR0Init failed! %Rrc!\n", vrc));
     1653        return STATUS_UNSUCCESSFUL;
     1654    }
     1655
     1656    /* Initialize VBox subsystem. */
     1657    vrc = VbglR0SfInit();
     1658    if (RT_FAILURE(vrc))
     1659    {
     1660        Log(("VBOXSF: DriverEntry: ERROR while initializing VBox subsystem (%Rrc)!\n", vrc));
     1661        RTR0Term();
     1662        return STATUS_UNSUCCESSFUL;
     1663    }
     1664
     1665    /* Connect the HGCM client */
     1666    vrc = VbglR0SfConnect(&g_SfClient);
     1667    if (RT_FAILURE(vrc))
     1668    {
     1669        Log(("VBOXSF: DriverEntry: ERROR while connecting to host (%Rrc)!\n",
     1670             vrc));
     1671        VbglR0SfTerm();
     1672        RTR0Term();
     1673        return STATUS_UNSUCCESSFUL;
     1674    }
     1675
     1676    vrc = VbglR0QueryHostFeatures(&g_fHostFeatures);
     1677    if (RT_FAILURE(vrc))
     1678    {
     1679        LogRel(("vboxsf: VbglR0QueryHostFeatures failed: vrc=%Rrc (ignored)\n", vrc));
     1680        g_fHostFeatures = 0;
     1681    }
     1682    VbglR0SfHostReqQueryFeaturesSimple(&g_fSfFeatures, &g_uSfLastFunction);
     1683    LogRel(("VBoxSF: g_fHostFeatures=%#x g_fSfFeatures=%#RX64 g_uSfLastFunction=%u\n",
     1684            g_fHostFeatures, g_fSfFeatures, g_uSfLastFunction));
     1685
     1686    if (!VbglR0CanUsePhysPageList())
     1687    {
     1688        LogRel(("vboxsf: Host does not support physical page lists.  Refuses to load!\n"));
     1689        VbglR0SfTerm();
     1690        RTR0Term();
     1691        return STATUS_UNSUCCESSFUL;
     1692    }
     1693
     1694    /*
     1695     * Resolve newer kernel APIs we might want to use.
     1696     * Note! Because of http://www.osronline.com/article.cfm%5eid=494.htm we cannot
     1697     *       use MmGetSystemRoutineAddress here as it will crash on xpsp2.
     1698     */
     1699    RTDBGKRNLINFO hKrnlInfo;
     1700    int rc = RTR0DbgKrnlInfoOpen(&hKrnlInfo, 0/*fFlags*/);
     1701    AssertLogRelRC(rc);
     1702    if (RT_SUCCESS(rc))
     1703    {
     1704        g_pfnCcCoherencyFlushAndPurgeCache
     1705            = (PFNCCCOHERENCYFLUSHANDPURGECACHE)RTR0DbgKrnlInfoGetSymbol(hKrnlInfo, NULL, "CcCoherencyFlushAndPurgeCache");
     1706        RTR0DbgKrnlInfoRelease(hKrnlInfo);
     1707    }
     1708
     1709    /* Init the driver object. */
     1710    DriverObject->DriverUnload = VBoxMRxUnload;
     1711    for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
     1712    {
     1713        DriverObject->MajorFunction[i] = (PDRIVER_DISPATCH)VBoxMRxFsdDispatch;
     1714    }
     1715
     1716    /* Forward to RDBSS. */
     1717    Status = RxDriverEntry(DriverObject, RegistryPath);
     1718    if (Status != STATUS_SUCCESS)
     1719    {
     1720        Log(("VBOXSF: DriverEntry: RxDriverEntry failed: 0x%08X\n", Status));
     1721        goto failure;
     1722    }
     1723
     1724    __try
     1725    {
     1726        Log(("VBOXSF: DriverEntry: RxRegisterMinirdr: calling VBoxMRxDeviceObject %p\n",
     1727             VBoxMRxDeviceObject));
     1728
     1729        RtlInitUnicodeString(&VBoxMRxName, DD_MRX_VBOX_FS_DEVICE_NAME_U);
     1730
     1731        /* Don use RX_REGISTERMINI_FLAG_DONT_PROVIDE_UNCS or else
     1732         * UNC mappings don't work (including Windows explorer browsing).
     1733         */
     1734        Status = RxRegisterMinirdr(&VBoxMRxDeviceObject,
     1735                                   DriverObject,
     1736                                   &VBoxMRxDispatch,
     1737                                   RX_REGISTERMINI_FLAG_DONT_PROVIDE_MAILSLOTS,
     1738                                   &VBoxMRxName,
     1739                                   sizeof(MRX_VBOX_DEVICE_EXTENSION),
     1740                                   FILE_DEVICE_NETWORK_FILE_SYSTEM,
     1741                                   FILE_REMOTE_DEVICE);
     1742
     1743        Log(("VBOXSF: DriverEntry: RxRegisterMinirdr: returned 0x%08X VBoxMRxDeviceObject %p\n",
     1744             Status, VBoxMRxDeviceObject));
     1745
     1746        if (Status!=STATUS_SUCCESS)
     1747        {
     1748            Log(("VBOXSF: DriverEntry: RxRegisterMinirdr failed: 0x%08X\n", Status ));
     1749            try_return((void)Status);
     1750        }
     1751
     1752        /* Init the device extension.
     1753         * NOTE: the device extension actually points to fields
     1754         * in the RDBSS_DEVICE_OBJECT. Our space is past the end
     1755         * of this struct!!
     1756         */
     1757        pDeviceExtension = (PMRX_VBOX_DEVICE_EXTENSION)((PBYTE)VBoxMRxDeviceObject + sizeof(RDBSS_DEVICE_OBJECT));
     1758
     1759        pDeviceExtension->pDeviceObject = VBoxMRxDeviceObject;
     1760
     1761        for (i = 0; i < RT_ELEMENTS(pDeviceExtension->cLocalConnections); i++)
     1762        {
     1763            pDeviceExtension->cLocalConnections[i] = FALSE;
     1764        }
     1765
     1766        /* Mutex for synchronizining our connection list */
     1767        ExInitializeFastMutex(&pDeviceExtension->mtxLocalCon);
     1768
     1769        /* The device object has been created. Need to setup a symbolic
     1770         * link so that the device may be accessed from a Win32 user mode
     1771         * application.
     1772         */
     1773
     1774        RtlInitUnicodeString(&UserModeDeviceName, DD_MRX_VBOX_USERMODE_SHADOW_DEV_NAME_U);
     1775        Log(("VBOXSF: DriverEntry: Calling IoCreateSymbolicLink\n"));
     1776        Status = IoCreateSymbolicLink(&UserModeDeviceName, &VBoxMRxName);
     1777        if (Status != STATUS_SUCCESS)
     1778        {
     1779            Log(("VBOXSF: DriverEntry: IoCreateSymbolicLink: 0x%08X\n",
     1780                 Status));
     1781            try_return((void)Status);
     1782        }
     1783        Log(("VBOXSF: DriverEntry: Symbolic link created.\n"));
     1784
     1785        /*
     1786         * Build the dispatch tables for the minirdr
     1787         */
     1788        vbsfInitMRxDispatch();
     1789
     1790    try_exit:
     1791         ;
     1792    }
     1793    __finally
     1794    {
     1795        ;
     1796    }
     1797
     1798    if (Status != STATUS_SUCCESS)
     1799    {
     1800        Log(("VBOXSF: DriverEntry: VBoxSF.sys failed to start with Status = 0x%08X\n",
     1801             Status));
     1802        goto failure;
     1803    }
     1804
     1805    AssertPtr(pDeviceExtension);
     1806
     1807    /* The redirector driver must intercept the IOCTL to avoid VBOXSVR name resolution
     1808     * by other redirectors. These additional name resolutions cause long delays.
     1809     */
     1810    Log(("VBOXSF: DriverEntry: VBoxMRxDeviceObject = %p, rdbss %p, devext %p\n",
     1811         VBoxMRxDeviceObject, DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL], pDeviceExtension));
     1812    pDeviceExtension->pfnRDBSSDeviceControl = DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL];
     1813    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VBoxMRXDeviceControl;
     1814
     1815    /* Intercept IRP_MJ_CREATE to fix incorrect (wrt NTFS, FAT, ++) return
     1816     * codes for NtOpenFile("r:\\asdf\\", FILE_NON_DIRECTORY_FILE).
     1817     */
     1818    pDeviceExtension->pfnRDBSSCreate = DriverObject->MajorFunction[IRP_MJ_CREATE];
     1819    DriverObject->MajorFunction[IRP_MJ_CREATE] = VBoxHookMjCreate;
     1820
     1821    /* Intercept IRP_MJ_SET_INFORMATION to ensure we call the host for all
     1822     * FileEndOfFileInformation requestes, even if the new size matches the
     1823     * old one.  We don't know if someone else might have modified the file
     1824     * size cached in the FCB since the last time we update it.
     1825     */
     1826    pDeviceExtension->pfnRDBSSSetInformation = DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION];
     1827    DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] = VBoxHookMjSetInformation;
     1828
     1829
     1830    /** @todo start the redirector here RxStartMiniRdr. */
     1831
     1832    Log(("VBOXSF: DriverEntry: Init successful!\n"));
     1833    return STATUS_SUCCESS;
     1834
     1835failure:
     1836
     1837    Log(("VBOXSF: DriverEntry: Failure! Status = 0x%08X\n", Status));
     1838
     1839    VbglR0SfDisconnect(&g_SfClient);
     1840    VbglR0SfTerm();
     1841    RTR0Term();
     1842
     1843    if (VBoxMRxDeviceObject)
     1844    {
     1845        RxUnregisterMinirdr(VBoxMRxDeviceObject);
     1846        VBoxMRxDeviceObject = NULL;
     1847    }
     1848
     1849    return Status;
     1850}
     1851
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette