Changeset 78631 in vbox for trunk/src/VBox
- Timestamp:
- May 21, 2019 1:42:38 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/WINNT/SharedFolders/driver/vbsf.cpp
r78629 r78631 1627 1627 * The "main" function for a driver binary. 1628 1628 */ 1629 extern "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 1629 extern "C" NTSTATUS NTAPI DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) 1630 { 1638 1631 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 } 1632 AssertLogRelReturn(DriverObject, STATUS_UNSUCCESSFUL); 1645 1633 1646 1634 /* 1647 * Initialize IPRT .1635 * Initialize IPRT and Vbgl. 1648 1636 */ 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)); 1637 NTSTATUS rcNt = STATUS_UNSUCCESSFUL; 1638 int vrc = RTR0Init(0); 1639 if (RT_SUCCESS(vrc)) 1640 { 1641 vrc = VbglR0SfInit(); 1642 if (RT_SUCCESS(vrc)) 1643 { 1644 /* 1645 * Connect to the shared folder service on the host. 1646 */ 1647 vrc = VbglR0SfConnect(&g_SfClient); 1648 if (RT_SUCCESS(vrc)) 1649 { 1650 /* 1651 * Query the features and check that the host does page lists as we need those 1652 * for reading and writing. 1653 */ 1654 vrc = VbglR0QueryHostFeatures(&g_fHostFeatures); 1655 if (RT_FAILURE(vrc)) 1656 { 1657 LogRel(("vboxsf: VbglR0QueryHostFeatures failed: vrc=%Rrc (ignored)\n", vrc)); 1658 g_fHostFeatures = 0; 1659 } 1660 VbglR0SfHostReqQueryFeaturesSimple(&g_fSfFeatures, &g_uSfLastFunction); 1661 LogRel(("VBoxSF: g_fHostFeatures=%#x g_fSfFeatures=%#RX64 g_uSfLastFunction=%u\n", 1662 g_fHostFeatures, g_fSfFeatures, g_uSfLastFunction)); 1663 1664 if (VbglR0CanUsePhysPageList()) 1665 { 1666 /* 1667 * Resolve newer kernel APIs we might want to use. 1668 * Note! Because of http://www.osronline.com/article.cfm%5eid=494.htm we cannot 1669 * use MmGetSystemRoutineAddress here as it will crash on xpsp2. 1670 */ 1671 RTDBGKRNLINFO hKrnlInfo; 1672 int rc = RTR0DbgKrnlInfoOpen(&hKrnlInfo, 0/*fFlags*/); 1673 AssertLogRelRC(rc); 1674 if (RT_SUCCESS(rc)) 1675 { 1676 g_pfnCcCoherencyFlushAndPurgeCache 1677 = (PFNCCCOHERENCYFLUSHANDPURGECACHE)RTR0DbgKrnlInfoGetSymbol(hKrnlInfo, NULL, 1678 "CcCoherencyFlushAndPurgeCache"); 1679 RTR0DbgKrnlInfoRelease(hKrnlInfo); 1680 } 1681 1682 /* 1683 * Init the driver object. 1684 */ 1685 DriverObject->DriverUnload = VBoxMRxUnload; 1686 for (size_t i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++) 1687 DriverObject->MajorFunction[i] = (PDRIVER_DISPATCH)VBoxMRxFsdDispatch; 1688 1689 /* 1690 * Do RDBSS driver entry processing. 1691 */ 1692 rcNt = RxDriverEntry(DriverObject, RegistryPath); 1693 if (rcNt == STATUS_SUCCESS) 1694 { 1695 /* 1696 * Do the mini redirector registration. 1697 * Note! Don't use RX_REGISTERMINI_FLAG_DONT_PROVIDE_UNCS or else UNC 1698 * mappings don't work (including Windows explorer browsing). 1699 */ 1700 Log(("VBOXSF: DriverEntry: RxRegisterMinirdr: calling VBoxMRxDeviceObject %p\n", VBoxMRxDeviceObject)); 1701 UNICODE_STRING VBoxMRxName; 1702 RtlInitUnicodeString(&VBoxMRxName, DD_MRX_VBOX_FS_DEVICE_NAME_U); 1703 rcNt = RxRegisterMinirdr(&VBoxMRxDeviceObject, 1704 DriverObject, 1705 &VBoxMRxDispatch, 1706 RX_REGISTERMINI_FLAG_DONT_PROVIDE_MAILSLOTS, 1707 &VBoxMRxName, 1708 sizeof(MRX_VBOX_DEVICE_EXTENSION), 1709 FILE_DEVICE_NETWORK_FILE_SYSTEM, 1710 FILE_REMOTE_DEVICE); 1711 Log(("VBOXSF: DriverEntry: RxRegisterMinirdr: returned 0x%08X VBoxMRxDeviceObject %p\n", 1712 rcNt, VBoxMRxDeviceObject)); 1713 if (rcNt == STATUS_SUCCESS) 1714 { 1715 /* 1716 * Init the device extension. 1717 * 1718 * Note! The device extension actually points to fields in the RDBSS_DEVICE_OBJECT. 1719 * Our space is past the end of that struct!! 1720 */ 1721 PMRX_VBOX_DEVICE_EXTENSION pVBoxDevX = (PMRX_VBOX_DEVICE_EXTENSION)( (PBYTE)VBoxMRxDeviceObject 1722 + sizeof(RDBSS_DEVICE_OBJECT)); 1723 pVBoxDevX->pDeviceObject = VBoxMRxDeviceObject; 1724 for (size_t i = 0; i < RT_ELEMENTS(pVBoxDevX->cLocalConnections); i++) 1725 pVBoxDevX->cLocalConnections[i] = FALSE; 1726 1727 /* Mutex for synchronizining our connection list */ 1728 ExInitializeFastMutex(&pVBoxDevX->mtxLocalCon); 1729 1730 /* 1731 * The device object has been created. Need to setup a symbolic link 1732 * in the Win32 name space for user mode applications. 1733 */ 1734 UNICODE_STRING UserModeDeviceName; 1735 RtlInitUnicodeString(&UserModeDeviceName, DD_MRX_VBOX_USERMODE_SHADOW_DEV_NAME_U); 1736 Log(("VBOXSF: DriverEntry: Calling IoCreateSymbolicLink\n")); 1737 rcNt = IoCreateSymbolicLink(&UserModeDeviceName, &VBoxMRxName); 1738 if (rcNt == STATUS_SUCCESS) 1739 { 1740 Log(("VBOXSF: DriverEntry: Symbolic link created.\n")); 1741 1742 /* 1743 * Build the dispatch tables for the minirdr 1744 */ 1745 vbsfInitMRxDispatch(); 1746 1747 /* 1748 * The redirector driver must intercept the IOCTL to avoid VBOXSVR name resolution 1749 * by other redirectors. These additional name resolutions cause long delays. 1750 */ 1751 Log(("VBOXSF: DriverEntry: VBoxMRxDeviceObject = %p, rdbss %p, devext %p\n", 1752 VBoxMRxDeviceObject, DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL], pVBoxDevX)); 1753 pVBoxDevX->pfnRDBSSDeviceControl = DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL]; 1754 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VBoxMRXDeviceControl; 1755 1756 /* 1757 * Intercept IRP_MJ_CREATE to fix incorrect (wrt NTFS, FAT, ++) return 1758 * codes for NtOpenFile("r:\\asdf\\", FILE_NON_DIRECTORY_FILE). 1759 */ 1760 pVBoxDevX->pfnRDBSSCreate = DriverObject->MajorFunction[IRP_MJ_CREATE]; 1761 DriverObject->MajorFunction[IRP_MJ_CREATE] = VBoxHookMjCreate; 1762 1763 /* 1764 * Intercept IRP_MJ_SET_INFORMATION to ensure we call the host for all 1765 * FileEndOfFileInformation requestes, even if the new size matches the 1766 * old one. We don't know if someone else might have modified the file 1767 * size cached in the FCB since the last time we update it. 1768 */ 1769 pVBoxDevX->pfnRDBSSSetInformation = DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION]; 1770 DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] = VBoxHookMjSetInformation; 1771 1772 /** @todo start the redirector here RxStartMiniRdr. */ 1773 1774 Log(("VBOXSF: DriverEntry: Init successful!\n")); 1775 return STATUS_SUCCESS; 1776 } 1777 LogRel(("VBOXSF: DriverEntry: IoCreateSymbolicLink: %#x\n", rcNt)); 1778 1779 RxUnregisterMinirdr(VBoxMRxDeviceObject); 1780 VBoxMRxDeviceObject = NULL; 1781 } 1782 else 1783 LogRel(("VBOXSF: DriverEntry: RxRegisterMinirdr failed: %#x\n", rcNt)); 1784 } 1785 else 1786 LogRel(("VBOXSF: DriverEntry: RxDriverEntry failed: 0x%08X\n", rcNt)); 1787 } 1788 else 1789 LogRel(("VBOXSF: Host does not support physical page lists. Refusing to load!\n")); 1790 VbglR0SfDisconnect(&g_SfClient); 1791 } 1792 else 1793 LogRel(("VBOXSF: DriverEntry: Failed to connect to the host: %Rrc!\n", vrc)); 1794 VbglR0SfTerm(); 1795 } 1796 else 1797 LogRel(("VBOXSF: DriverEntry: VbglR0SfInit! %Rrc!\n", vrc)); 1661 1798 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 1835 failure: 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 1799 } 1800 else 1801 RTLogRelPrintf("VBOXSF: DriverEntry: RTR0Init failed! %Rrc!\n", vrc); 1802 return rcNt; 1803 } 1804
Note:
See TracChangeset
for help on using the changeset viewer.