Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1248 lines
37 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. persist.c
  5. Abstract:
  6. This module contains routines for handling the state file for persistent
  7. file handles.
  8. Author:
  9. Andy Herron (andyhe) 17-Nov-1999
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #include "persist.tmh"
  14. #pragma hdrstop
  15. //
  16. // structures specific to this module only.
  17. //
  18. #ifdef INCLUDE_SMB_PERSISTENT
  19. typedef enum _FLUSH_STATE {
  20. FlushStateNotPosted,
  21. FlushStatePosted,
  22. FlushStateCompleted,
  23. FlushStateInError
  24. } FLUSH_STATE, *PFLUSH_STATE;
  25. typedef struct _PERSISTENT_RECORD_LIST_ENTRY {
  26. LIST_ENTRY PendingListEntry;
  27. ULONG FileOffset;
  28. FLUSH_STATE FlushState;
  29. PWORK_CONTEXT WorkContext;
  30. PERSISTENT_RECORD;
  31. } PERSISTENT_RECORD_LIST_ENTRY, *PPERSISTENT_RECORD_LIST_ENTRY;
  32. #define INITIAL_BITMAP_LENGTH 512
  33. typedef struct _PERSISTENT_SHARE_INFO {
  34. PSHARE Share;
  35. LIST_ENTRY UpdatesPendingList;
  36. UNICODE_STRING StateFileName;
  37. HANDLE FileHandle;
  38. RTL_BITMAP BitMap;
  39. ULONG TotalEntries;
  40. ULONG FreeEntries;
  41. ULONG HintIndex;
  42. ULONG LastIdAccepted;
  43. ULONG LastIdCommitted;
  44. SRV_LOCK Lock;
  45. ULONG FirstRecord;
  46. WCHAR NameBuffer[1];
  47. } PERSISTENT_SHARE_INFO, *PPERSISTENT_SHARE_INFO;
  48. #define BITMAP_TO_STATE_FILE_INDEX( _shareInfo, _index ) \
  49. ( (_shareInfo)->FirstRecord + ((_index) * sizeof( PERSISTENT_RECORD )))
  50. PPERSISTENT_RECORD_LIST_ENTRY
  51. SrvGetPersistentRecord (
  52. IN PWORK_CONTEXT WorkContext
  53. );
  54. VOID
  55. SrvFreePersistentRecord (
  56. PPERSISTENT_RECORD_LIST_ENTRY Record
  57. );
  58. NTSTATUS
  59. SrvPostPersistentRecord (
  60. IN PWORK_CONTEXT WorkContext,
  61. IN PPERSISTENT_RECORD_LIST_ENTRY PersistentState,
  62. OUT PULONG TransactionId
  63. );
  64. NTSTATUS
  65. SrvFlushPersistentRecords (
  66. IN PWORK_CONTEXT WorkContext,
  67. IN ULONG TransactionId
  68. );
  69. NTSTATUS
  70. SrvPersistSession(
  71. IN PWORK_CONTEXT WorkContext,
  72. PPERSISTENT_RECORD_LIST_ENTRY PersistentState,
  73. ULONG *SessionId,
  74. ULONG *ClientId
  75. );
  76. NTSTATUS
  77. SrvPersistConnection(
  78. IN PWORK_CONTEXT WorkContext,
  79. PPERSISTENT_RECORD_LIST_ENTRY PersistentState,
  80. ULONG *ClientId
  81. );
  82. unsigned long
  83. Crc32(
  84. IN unsigned long cbBuffer,
  85. IN unsigned char *pbBuffer
  86. );
  87. #if 0
  88. VOID
  89. Crc32( unsigned long dwCrc,
  90. unsigned long cbBuffer,
  91. LPVOID pvBuffer,
  92. unsigned long *pNewCrc);
  93. #endif
  94. #ifdef ALLOC_PRAGMA
  95. #pragma alloc_text( PAGE, SrvPostPersistentOpen )
  96. #pragma alloc_text( PAGE, SrvPersistSession )
  97. #pragma alloc_text( PAGE, SrvPersistConnection )
  98. #pragma alloc_text( PAGE, SrvGetPersistentRecord )
  99. #pragma alloc_text( PAGE, SrvPostPersistentRecord )
  100. #pragma alloc_text( PAGE, SrvFlushPersistentRecords )
  101. #pragma alloc_text( PAGE, SrvSetupPersistentShare )
  102. #pragma alloc_text( PAGE, SrvClosePersistentShare )
  103. #pragma alloc_text( PAGE, Crc32 )
  104. #endif
  105. SMB_STATUS
  106. SrvPostPersistentOpen (
  107. IN OUT PWORK_CONTEXT WorkContext,
  108. IN SMB_STATUS SmbStatus
  109. )
  110. /*++
  111. Routine Description:
  112. Writes an open for the given RFCB to the state file.
  113. Arguments:
  114. WorkContext - has the RFCB
  115. SmbStatus - status that the calling routine wants to pass back to
  116. SrvProcessSmb et al.
  117. Return Value:
  118. Return value to pass back to SrvProcessSmb et al.
  119. --*/
  120. {
  121. PPERSISTENT_RECORD_LIST_ENTRY persistentRecord = NULL;
  122. ULONG clientId;
  123. ULONG sessionId;
  124. NTSTATUS status;
  125. PRFCB rfcb = WorkContext->Rfcb;
  126. PLFCB lfcb = rfcb->Lfcb;
  127. PMFCB mfcb = lfcb->Mfcb;
  128. ULONG transactionId;
  129. PAGED_CODE( );
  130. if (WorkContext->Session == NULL ||
  131. WorkContext->Session->PersistentId == 0 ||
  132. lfcb->TreeConnect->Share->PersistentState == PersistentStateInError) {
  133. IF_DEBUG(PERSISTENT) {
  134. KdPrint(( "SrvPostPersistOpen: session 0x%x doesn't support persistent handles.\n", WorkContext->Session ));
  135. }
  136. return SmbStatus;
  137. }
  138. persistentRecord = SrvGetPersistentRecord( WorkContext );
  139. if (persistentRecord == NULL) {
  140. IF_DEBUG(PERSISTENT) {
  141. KdPrint(( "SrvPostPersistOpen: Unable to allocate record for 0x%lx\n", WorkContext ));
  142. }
  143. return SmbStatus;
  144. }
  145. IF_DEBUG(PERSISTENT) {
  146. KdPrint(( "SrvPostPersistOpen: Post persistent open wc=0x%lx, record 0x%lx\n",
  147. WorkContext, persistentRecord ));
  148. }
  149. status = SrvPersistSession( WorkContext,
  150. persistentRecord,
  151. &sessionId,
  152. &clientId
  153. );
  154. if (!NT_SUCCESS(status)) {
  155. IF_DEBUG(PERSISTENT) {
  156. KdPrint(( "SrvPostPersistOpen: failed to persist sess, status 0x%lx\n", status ));
  157. }
  158. SrvFreePersistentRecord( persistentRecord );
  159. return SmbStatus;
  160. }
  161. ASSERT( clientId != 0 );
  162. ASSERT( sessionId != 0 );
  163. ASSERT( rfcb != NULL );
  164. if (rfcb->PagedRfcb->PersistentId == 0) {
  165. ACQUIRE_LOCK( &rfcb->Lfcb->Mfcb->NonpagedMfcb->Lock );
  166. if (rfcb->PagedRfcb->PersistentId == 0) {
  167. LONG uniqueId = 0;
  168. while (uniqueId == 0) {
  169. uniqueId = InterlockedIncrement( &SrvGlobalPersistentRfcbId );
  170. }
  171. rfcb->PagedRfcb->PersistentId = uniqueId;
  172. rfcb->PagedRfcb->PersistentState = PersistentStateActive;
  173. }
  174. RELEASE_LOCK( &rfcb->Lfcb->Mfcb->NonpagedMfcb->Lock );
  175. }
  176. //
  177. // now setup the persistent record as it should exist on disk for this
  178. // file open.
  179. //
  180. persistentRecord->PersistState = PersistentStateActive;
  181. persistentRecord->PersistOperation = PersistentFileOpen;
  182. persistentRecord->FileOpen.ClientId = clientId;
  183. persistentRecord->FileOpen.SessionNumber = sessionId;
  184. persistentRecord->FileOpen.Rfcb = rfcb;
  185. persistentRecord->FileOpen.PersistentFileId = rfcb->PagedRfcb->PersistentId;
  186. persistentRecord->FileOpen.FileMode = lfcb->FileMode;
  187. persistentRecord->FileOpen.JobId = lfcb->JobId;
  188. persistentRecord->FileOpen.FcbOpenCount = rfcb->PagedRfcb->FcbOpenCount;
  189. persistentRecord->FileOpen.Fid = rfcb->Fid;
  190. persistentRecord->FileOpen.Pid = rfcb->Pid;
  191. persistentRecord->FileOpen.Tid = rfcb->Tid;
  192. persistentRecord->FileOpen.GrantedAccess = rfcb->GrantedAccess;
  193. persistentRecord->FileOpen.ShareAccess = rfcb->ShareAccess;
  194. persistentRecord->FileOpen.OplockState = rfcb->OplockState;
  195. persistentRecord->FileOpen.CompatibilityOpen = mfcb->CompatibilityOpen;
  196. persistentRecord->FileOpen.OpenFileAttributes= mfcb->NonpagedMfcb->OpenFileAttributes;
  197. status = SrvIssueQueryUsnInfoRequest( rfcb,
  198. FALSE,
  199. &persistentRecord->FileOpen.UsnValue,
  200. &persistentRecord->FileOpen.FileReferenceNumber );
  201. //
  202. // Save off the current state of the workcontext within the workcontext.
  203. //
  204. WorkContext->Parameters.MakePersistent.ResumeSmbStatus = SmbStatus;
  205. WorkContext->Parameters.MakePersistent.FspRestartRoutine = WorkContext->FspRestartRoutine;
  206. WorkContext->Parameters.MakePersistent.FsdRestartRoutine = WorkContext->FsdRestartRoutine;
  207. WorkContext->FsdRestartRoutine = SrvQueueWorkToFspAtDpcLevel;
  208. // WorkContext->FspRestartRoutine = SrvRestartWritePersistentOpen;
  209. status = SrvPostPersistentRecord( WorkContext,
  210. persistentRecord,
  211. &transactionId
  212. );
  213. if (NT_SUCCESS(status)) {
  214. status = SrvFlushPersistentRecords( WorkContext, transactionId );
  215. if (NT_SUCCESS(status)) {
  216. return SmbStatusInProgress;
  217. } else {
  218. IF_DEBUG(PERSISTENT) {
  219. KdPrint(( "SrvPostPersistOpen: failed to flush record, status 0x%lx\n", status ));
  220. }
  221. }
  222. } else {
  223. IF_DEBUG(PERSISTENT) {
  224. KdPrint(( "SrvPostPersistOpen: failed to post record, status 0x%lx\n", status ));
  225. }
  226. }
  227. WorkContext->FsdRestartRoutine = WorkContext->Parameters.MakePersistent.FsdRestartRoutine;
  228. WorkContext->FspRestartRoutine = WorkContext->Parameters.MakePersistent.FspRestartRoutine;
  229. // whoops, something didn't go right. clean up.
  230. SrvFreePersistentRecord( persistentRecord );
  231. return SmbStatus;
  232. } // SrvPostPersistentOpen
  233. NTSTATUS
  234. SrvPersistSession(
  235. IN PWORK_CONTEXT WorkContext,
  236. PPERSISTENT_RECORD_LIST_ENTRY PersistentState,
  237. ULONG *SessionId,
  238. ULONG *ClientId
  239. )
  240. {
  241. PPERSISTENT_RECORD_LIST_ENTRY persistentRecord = NULL;
  242. ULONG sessionId;
  243. NTSTATUS status;
  244. PSESSION session = WorkContext->Session;
  245. PCONNECTION connection = WorkContext->Connection;
  246. BOOLEAN createRecord = FALSE;
  247. PAGED_CODE( );
  248. ASSERT(session != NULL);
  249. ASSERT(session->PersistentId != 0);
  250. if (session->PersistentState == PersistentStateFreed) {
  251. ACQUIRE_LOCK( &connection->Lock );
  252. if (session->PersistentState == PersistentStateFreed) {
  253. session->PersistentState = PersistentStateActive;
  254. createRecord = TRUE;
  255. }
  256. RELEASE_LOCK( &connection->Lock );
  257. }
  258. //
  259. // if there's not already a record for this session, create one now
  260. //
  261. if (createRecord) {
  262. persistentRecord = SrvGetPersistentRecord( WorkContext );
  263. if (persistentRecord == NULL) {
  264. IF_DEBUG(PERSISTENT) {
  265. KdPrint(( "SrvPersistSession: Unable to allocate record for 0x%lx\n", WorkContext ));
  266. }
  267. status = STATUS_INSUFF_SERVER_RESOURCES;
  268. goto exitPersistSession;
  269. }
  270. IF_DEBUG(PERSISTENT) {
  271. KdPrint(( "SrvPersistSession: persistent sess 0x%lx, record 0x%lx\n",
  272. session, persistentRecord ));
  273. }
  274. status = SrvPersistConnection( WorkContext,
  275. PersistentState,
  276. ClientId
  277. );
  278. if (!NT_SUCCESS(status)) {
  279. IF_DEBUG(PERSISTENT) {
  280. KdPrint(( "SrvPersistSession: failed to persist conn, status 0x%lx\n", status ));
  281. }
  282. goto exitPersistSession;
  283. }
  284. //
  285. // now setup the persistent record as it should exist on disk for this
  286. // file open.
  287. //
  288. persistentRecord->PersistState = PersistentStateActive;
  289. persistentRecord->PersistOperation = PersistentSession;
  290. persistentRecord->Session.ClientId = *ClientId;
  291. persistentRecord->Session.Session = session;
  292. persistentRecord->Session.SessionNumber = session->PersistentId;
  293. persistentRecord->Session.Uid = session->Uid;
  294. persistentRecord->Session.CreateTime.QuadPart = session->StartTime.QuadPart;
  295. persistentRecord->Session.LogOffTime.QuadPart = session->LogOffTime.QuadPart;
  296. persistentRecord->Session.KickOffTime.QuadPart = session->KickOffTime.QuadPart;
  297. //
  298. // now we add this record onto the list of pending state file updates.
  299. //
  300. InsertTailList( &PersistentState->PendingListEntry,
  301. &persistentRecord->PendingListEntry );
  302. // now it's time to copy the user's name and domain name to the
  303. // entries going out to the state file.
  304. {
  305. ULONG totalLength;
  306. ULONG lengthNameCopiedSoFar = 0;
  307. ULONG lengthCopiedSoFar = 0;
  308. totalLength = session->NtUserName.Length +
  309. session->NtUserDomain.Length +
  310. sizeof(WCHAR) ;
  311. while (lengthCopiedSoFar < totalLength) {
  312. PPERSISTENT_RECORD_LIST_ENTRY persistentName;
  313. ULONG bufferRemaining;
  314. PWCHAR nextAvailable;
  315. persistentName = SrvGetPersistentRecord( WorkContext );
  316. if (persistentName == NULL) {
  317. IF_DEBUG(PERSISTENT) {
  318. KdPrint(( "SrvPersistSession: Unable to allocate name record for 0x%lx\n", WorkContext ));
  319. }
  320. status = STATUS_INSUFF_SERVER_RESOURCES;
  321. goto exitPersistSession;
  322. }
  323. persistentName->PersistState = PersistentStateActive;
  324. persistentName->PersistOperation = PersistentUserName;
  325. persistentName->UserName.ContinuationRecord = (ULONG) -1;
  326. persistentName->UserName.RecordLength = totalLength - lengthCopiedSoFar;
  327. if (persistentName->UserName.RecordLength > PERSISTENT_USER_NAME_BUFFER_LENGTH) {
  328. persistentName->UserName.RecordLength = PERSISTENT_USER_NAME_BUFFER_LENGTH;
  329. }
  330. bufferRemaining = persistentName->UserName.RecordLength;
  331. nextAvailable = (WCHAR *) &(persistentName->UserName.Buffer[0]);
  332. //
  333. // first we copy in the user's domain name
  334. //
  335. if (lengthCopiedSoFar < session->NtUserDomain.Length) {
  336. ULONG bytesToCopy = session->NtUserDomain.Length - lengthCopiedSoFar;
  337. if (bytesToCopy > PERSISTENT_USER_NAME_BUFFER_LENGTH) {
  338. bytesToCopy = PERSISTENT_USER_NAME_BUFFER_LENGTH;
  339. }
  340. RtlCopyMemory( nextAvailable,
  341. session->NtUserDomain.Buffer +
  342. ( lengthCopiedSoFar / sizeof(WCHAR) ),
  343. bytesToCopy );
  344. nextAvailable += bytesToCopy / sizeof(WCHAR);
  345. bufferRemaining -= bytesToCopy;
  346. lengthCopiedSoFar += bytesToCopy;
  347. }
  348. //
  349. // we separate the domain name and user name with a backslash
  350. //
  351. if (lengthCopiedSoFar == session->NtUserDomain.Length &&
  352. bufferRemaining > 0) {
  353. bufferRemaining -= sizeof(WCHAR);
  354. *nextAvailable = L'\\';
  355. nextAvailable++;
  356. lengthCopiedSoFar += sizeof(WCHAR);
  357. }
  358. //
  359. // lastly we copy in the user's user name
  360. //
  361. if (lengthNameCopiedSoFar < session->NtUserName.Length &&
  362. bufferRemaining > 0) {
  363. ULONG bytesToCopy = session->NtUserName.Length - lengthNameCopiedSoFar;
  364. if (bytesToCopy > PERSISTENT_USER_NAME_BUFFER_LENGTH) {
  365. bytesToCopy = PERSISTENT_USER_NAME_BUFFER_LENGTH;
  366. }
  367. RtlCopyMemory( nextAvailable,
  368. session->NtUserName.Buffer +
  369. ( lengthNameCopiedSoFar / sizeof(WCHAR) ),
  370. bytesToCopy );
  371. lengthNameCopiedSoFar += bytesToCopy;
  372. lengthCopiedSoFar += bytesToCopy;
  373. }
  374. InsertTailList( &PersistentState->PendingListEntry,
  375. &persistentName->PendingListEntry );
  376. }
  377. }
  378. }
  379. *SessionId = session->PersistentId;
  380. *ClientId = connection->PagedConnection->PersistentId;
  381. status = STATUS_SUCCESS;
  382. exitPersistSession:
  383. if (status != STATUS_SUCCESS) {
  384. // whoops, something didn't go right. clean up.
  385. *SessionId = 0;
  386. *ClientId = 0;
  387. }
  388. return status;
  389. }
  390. NTSTATUS
  391. SrvPersistConnection(
  392. IN PWORK_CONTEXT WorkContext,
  393. PPERSISTENT_RECORD_LIST_ENTRY PersistentState,
  394. ULONG *ClientId
  395. )
  396. {
  397. PPERSISTENT_RECORD_LIST_ENTRY persistentRecord = NULL;
  398. ULONG clientId;
  399. NTSTATUS status;
  400. PCONNECTION connection = WorkContext->Connection;
  401. BOOLEAN createRecord = FALSE;
  402. PAGED_CODE( );
  403. ASSERT(connection != NULL);
  404. if (connection->PagedConnection->PersistentId == 0) {
  405. ACQUIRE_LOCK( &connection->Lock );
  406. if (connection->PagedConnection->PersistentId == 0) {
  407. LONG uniqueId = 0;
  408. while (uniqueId == 0) {
  409. uniqueId = InterlockedIncrement( &SrvGlobalPersistentSessionId );
  410. }
  411. connection->PagedConnection->PersistentId = uniqueId;
  412. connection->PagedConnection->PersistentState = PersistentStateActive;
  413. createRecord = TRUE;
  414. }
  415. RELEASE_LOCK( &connection->Lock );
  416. }
  417. //
  418. // if there's not already a record for this connection, create one now
  419. //
  420. if (createRecord) {
  421. persistentRecord = SrvGetPersistentRecord( WorkContext );
  422. if (persistentRecord == NULL) {
  423. IF_DEBUG(PERSISTENT) {
  424. KdPrint(( "SrvPersistConnection: Unable to allocate record for 0x%lx\n", WorkContext ));
  425. }
  426. status = STATUS_INSUFF_SERVER_RESOURCES;
  427. goto exitPersistConnection;
  428. }
  429. IF_DEBUG(PERSISTENT) {
  430. KdPrint(( "SrvPersistConnection: persistent conn 0x%lx, record 0x%lx\n",
  431. connection, persistentRecord ));
  432. }
  433. //
  434. // now setup the persistent record as it should exist on disk for this
  435. // file open.
  436. //
  437. persistentRecord->PersistState = PersistentStateActive;
  438. persistentRecord->PersistOperation = PersistentConnection;
  439. persistentRecord->Connection.ClientId = *ClientId;
  440. persistentRecord->Connection.Connection = connection;
  441. persistentRecord->Connection.DirectHostIpx = connection->DirectHostIpx;
  442. RtlCopyMemory( persistentRecord->Connection.OemClientMachineName,
  443. connection->OemClientMachineName,
  444. COMPUTER_NAME_LENGTH+1
  445. );
  446. if (connection->DirectHostIpx) {
  447. RtlCopyMemory( &persistentRecord->Connection.IpxAddress,
  448. &connection->IpxAddress,
  449. sizeof( TDI_ADDRESS_IPX )
  450. );
  451. } else {
  452. persistentRecord->Connection.ClientIPAddress = connection->ClientIPAddress;
  453. }
  454. InsertTailList( &PersistentState->PendingListEntry,
  455. &persistentRecord->PendingListEntry );
  456. }
  457. *ClientId = connection->PagedConnection->PersistentId;
  458. return STATUS_SUCCESS;
  459. exitPersistConnection:
  460. // whoops, something didn't go right. clean up.
  461. *ClientId = 0;
  462. if (persistentRecord) {
  463. SrvFreePersistentRecord( persistentRecord );
  464. }
  465. return status;
  466. }
  467. PPERSISTENT_RECORD_LIST_ENTRY
  468. SrvGetPersistentRecord (
  469. IN PWORK_CONTEXT WorkContext
  470. )
  471. //
  472. // This allocates persistent state buffers that we write out to the persistent
  473. // state file. Free them with SrvFreePersistentRecord.
  474. //
  475. {
  476. PPERSISTENT_RECORD_LIST_ENTRY record;
  477. PAGED_CODE( );
  478. record = ALLOCATE_HEAP( sizeof( PERSISTENT_RECORD_LIST_ENTRY ), BlockTypePersistentState );
  479. if (record == NULL) {
  480. IF_DEBUG(PERSISTENT) {
  481. KdPrint(( "SrvGetPersistentRecord: failed to alloc record for wc= 0x%lx\n", WorkContext ));
  482. }
  483. return NULL;
  484. }
  485. RtlZeroMemory( record, sizeof( PERSISTENT_RECORD_LIST_ENTRY ));
  486. InitializeListHead( &record->PendingListEntry );
  487. record->FileOffset = (ULONG) -1;
  488. record->FlushState = FlushStateNotPosted;
  489. record->WorkContext = WorkContext;
  490. IF_DEBUG(PERSISTENT) {
  491. KdPrint(( "SrvGetPersistentRecord: allocated record at 0x%x for wc= 0x%lx\n", record, WorkContext ));
  492. }
  493. return record;
  494. }
  495. VOID
  496. SrvFreePersistentRecord (
  497. PPERSISTENT_RECORD_LIST_ENTRY Record
  498. )
  499. {
  500. PPERSISTENT_RECORD_LIST_ENTRY subRecord;
  501. PLIST_ENTRY listEntry = Record->PendingListEntry.Flink;
  502. PAGED_CODE( );
  503. while (listEntry != &Record->PendingListEntry) {
  504. subRecord = CONTAINING_RECORD( listEntry, PERSISTENT_RECORD_LIST_ENTRY, PendingListEntry );
  505. listEntry = listEntry->Flink;
  506. IF_DEBUG(PERSISTENT) {
  507. KdPrint(( "SrvGetPersistentRecord: freed record (1) at 0x%x\n", subRecord ));
  508. }
  509. FREE_HEAP( subRecord );
  510. }
  511. IF_DEBUG(PERSISTENT) {
  512. KdPrint(( "SrvGetPersistentRecord: freed record (2) at 0x%x\n", Record ));
  513. }
  514. FREE_HEAP( Record );
  515. return;
  516. }
  517. NTSTATUS
  518. SrvPostPersistentRecord (
  519. IN PWORK_CONTEXT WorkContext,
  520. IN PPERSISTENT_RECORD_LIST_ENTRY PersistentState,
  521. OUT PULONG TransactionId
  522. )
  523. {
  524. PPERSISTENT_RECORD_LIST_ENTRY record;
  525. ULONG numberRecordsRequired = 1;
  526. PLIST_ENTRY listEntry;
  527. PSHARE share = WorkContext->Share;
  528. PPERSISTENT_SHARE_INFO shareInfo;
  529. NTSTATUS status;
  530. ULONG bitMapIndex;
  531. PPERSISTENT_RECORD_LIST_ENTRY firstUserNameRecord = NULL;
  532. PPERSISTENT_RECORD_LIST_ENTRY lastUserNameRecord = NULL;
  533. PPERSISTENT_RECORD_LIST_ENTRY sessionRecord = NULL;
  534. PAGED_CODE( );
  535. if (share == NULL) {
  536. ASSERT(WorkContext->Rfcb != NULL);
  537. share = WorkContext->Rfcb->Lfcb->TreeConnect->Share;
  538. }
  539. ASSERT(share != NULL);
  540. //
  541. // determine how many records we need to write.
  542. //
  543. listEntry = PersistentState->PendingListEntry.Flink;
  544. while (listEntry != &PersistentState->PendingListEntry) {
  545. record = CONTAINING_RECORD( listEntry, PERSISTENT_RECORD_LIST_ENTRY, PendingListEntry );
  546. if (record->FileOffset == (ULONG) -1) {
  547. numberRecordsRequired++;
  548. }
  549. listEntry = listEntry->Flink;
  550. }
  551. //
  552. // if the persistent state file on the share isn't set up, do so now.
  553. //
  554. if (share->PersistentStateFile == NULL) {
  555. status = SrvSetupPersistentShare( share, FALSE );
  556. if (! NT_SUCCESS(status)) {
  557. IF_DEBUG(PERSISTENT) {
  558. KdPrint(( "SrvPostPersistentRecord: error 0x%x for share 0x%x\n", status, share ));
  559. }
  560. // need to log an error here
  561. share->PersistentState = PersistentStateInError;
  562. return status;
  563. }
  564. ASSERT( share->PersistentStateFile );
  565. }
  566. shareInfo = (PPERSISTENT_SHARE_INFO) (share->PersistentStateFile);
  567. //
  568. // if the bitmap is too small, try to double it's size.
  569. //
  570. ACQUIRE_LOCK( &shareInfo->Lock );
  571. if (shareInfo->FreeEntries <= numberRecordsRequired) {
  572. PULONG newBuffer;
  573. ULONG newSize;
  574. ULONG newBits;
  575. //
  576. // looks like our bitmap isn't big enough, let's expand it by two
  577. //
  578. if (shareInfo->BitMap.SizeOfBitMap == 0) {
  579. newSize = INITIAL_BITMAP_LENGTH;
  580. } else {
  581. newSize = shareInfo->BitMap.SizeOfBitMap * 2;
  582. }
  583. newBuffer = ALLOCATE_HEAP( newSize, BlockTypePersistentBitMap );
  584. if (newBuffer == NULL) {
  585. IF_DEBUG(PERSISTENT) {
  586. KdPrint(( "SrvGetPersistentRecord: failed to alloc record for wc= 0x%lx\n", WorkContext ));
  587. }
  588. RELEASE_LOCK( &shareInfo->Lock );
  589. return STATUS_INSUFF_SERVER_RESOURCES;
  590. }
  591. // optimize by only zeroing the second half of bitmap before copy.
  592. RtlZeroMemory( newBuffer, newSize );
  593. if (shareInfo->BitMap.Buffer != NULL) {
  594. RtlCopyMemory( newBuffer,
  595. shareInfo->BitMap.Buffer,
  596. shareInfo->BitMap.SizeOfBitMap
  597. );
  598. FREE_HEAP( shareInfo->BitMap.Buffer );
  599. newBits = ( newSize - shareInfo->BitMap.SizeOfBitMap ) * 8;
  600. shareInfo->BitMap.Buffer = newBuffer;
  601. shareInfo->BitMap.SizeOfBitMap = newSize;
  602. } else {
  603. newBits = newSize * 8;
  604. RtlInitializeBitMap( &shareInfo->BitMap, newBuffer, newSize );
  605. }
  606. IF_DEBUG(PERSISTENT) {
  607. KdPrint(( "SrvGetPersistentRecord: new bitmap 0x%x for share 0x%lx\n",
  608. newBuffer, share ));
  609. }
  610. shareInfo->FreeEntries += newBits;
  611. shareInfo->TotalEntries += newBits;
  612. }
  613. //
  614. // for each record we need to post, find a slot in the bitmap and map it
  615. // to a file index
  616. //
  617. listEntry = &PersistentState->PendingListEntry; // start at the head of the
  618. // list which is on the first entry
  619. while (numberRecordsRequired > 0) {
  620. ULONG numberEntries = numberRecordsRequired;
  621. bitMapIndex = (ULONG) -1;
  622. while (bitMapIndex == (ULONG) -1) {
  623. bitMapIndex = RtlFindClearBitsAndSet( &shareInfo->BitMap,
  624. numberEntries,
  625. shareInfo->HintIndex );
  626. if (bitMapIndex == (ULONG) -1) {
  627. numberEntries = RtlFindLongestRunClear( &shareInfo->BitMap,
  628. &shareInfo->HintIndex );
  629. if (numberEntries == 0 || numberEntries == (ULONG) -1) {
  630. shareInfo->FreeEntries = 0;
  631. IF_DEBUG(PERSISTENT) {
  632. KdPrint(( "SrvGetPersistentRecord: bitmap is full for share 0x%lx\n", share ));
  633. }
  634. RELEASE_LOCK( &shareInfo->Lock );
  635. return STATUS_INSUFF_SERVER_RESOURCES;
  636. }
  637. } else {
  638. shareInfo->HintIndex = bitMapIndex;
  639. }
  640. }
  641. numberRecordsRequired -= numberEntries;
  642. while (numberEntries > 0) {
  643. record = CONTAINING_RECORD( listEntry, PERSISTENT_RECORD_LIST_ENTRY, PendingListEntry );
  644. if (record->FileOffset == (ULONG) -1) {
  645. record->FileOffset = BITMAP_TO_STATE_FILE_INDEX( shareInfo, bitMapIndex );
  646. }
  647. record->PersistIndex = record->FileOffset;
  648. //
  649. // track the file indexes in records that need to refer to
  650. // other records.
  651. //
  652. if (record->PersistOperation == PersistentSession) {
  653. sessionRecord = record;
  654. } else if (record->PersistOperation == PersistentUserName) {
  655. //
  656. // chain up the user name records into a list
  657. //
  658. if (firstUserNameRecord == NULL) {
  659. firstUserNameRecord = record;
  660. } else {
  661. lastUserNameRecord->UserName.ContinuationRecord = record->FileOffset;
  662. }
  663. lastUserNameRecord = record;
  664. }
  665. //
  666. // on to next concurrent record, reduce number remaining, etc
  667. //
  668. bitMapIndex++;
  669. numberEntries--;
  670. listEntry = listEntry->Flink;
  671. }
  672. }
  673. //
  674. // fix up the file indexes in records that need to refer to other records
  675. //
  676. if (sessionRecord != NULL) {
  677. sessionRecord->Session.UserNameRecord = (firstUserNameRecord != NULL) ?
  678. firstUserNameRecord->FileOffset : (ULONG) -1;
  679. }
  680. //
  681. // with the records now well formed, complete them by calculating the CRC
  682. // values and putting them on the pending write list for the share.
  683. //
  684. // this is a do..while since we need to be sure to pick up the first record
  685. // which is also the head of the list.
  686. listEntry = &PersistentState->PendingListEntry;
  687. do {
  688. record = CONTAINING_RECORD( listEntry, PERSISTENT_RECORD_LIST_ENTRY, PendingListEntry );
  689. // Warning :
  690. // We calculate the CRC on the rest of the record excluding the CRC
  691. // value itself. If the CRC value is not stored in the first dword,
  692. // this breaks!
  693. //
  694. record->PersistConsistencyCheck = Crc32(
  695. sizeof( PERSISTENT_RECORD_LIST_ENTRY ) - sizeof(ULONG),
  696. (PUCHAR)((PUCHAR)record+sizeof(ULONG)) );
  697. listEntry = listEntry->Flink;
  698. //
  699. // we can add it to the updatesPending list without removing it
  700. // from the linked list it's in because we're destroying the list
  701. // safely as we traverse it.
  702. //
  703. InsertTailList( &shareInfo->UpdatesPendingList,
  704. &record->PendingListEntry );
  705. } while (listEntry != &PersistentState->PendingListEntry);
  706. *TransactionId = ++(shareInfo->LastIdAccepted);
  707. RELEASE_LOCK( &shareInfo->Lock );
  708. return STATUS_SUCCESS;
  709. }
  710. NTSTATUS
  711. SrvFlushPersistentRecords (
  712. IN PWORK_CONTEXT WorkContext,
  713. IN ULONG TransactionId
  714. )
  715. {
  716. return STATUS_INSUFF_SERVER_RESOURCES;
  717. }
  718. NTSTATUS
  719. SrvSetupPersistentShare (
  720. IN OUT PSHARE Share,
  721. IN BOOLEAN Restore
  722. )
  723. {
  724. ULONG bytesRequired;
  725. PPERSISTENT_SHARE_INFO shareInfo;
  726. PWCHAR destString;
  727. HANDLE h;
  728. NTSTATUS status;
  729. OBJECT_ATTRIBUTES objectAttributes;
  730. IO_STATUS_BLOCK iosb;
  731. LARGE_INTEGER allocationSize;
  732. PAGED_CODE( );
  733. ACQUIRE_LOCK( &SrvShareLock );
  734. if ( ! Share->AllowPersistentHandles ) {
  735. IF_DEBUG(PERSISTENT) {
  736. KdPrint(( "SrvSetupPersistentShare: share %ws not configured for persistent handles\n", Share->ShareName.Buffer ));
  737. }
  738. RELEASE_LOCK( &SrvShareLock );
  739. return STATUS_INVALID_SERVER_STATE;
  740. }
  741. if ( Share->PersistentStateFile != NULL ) {
  742. RELEASE_LOCK( &SrvShareLock );
  743. return STATUS_SUCCESS;
  744. }
  745. bytesRequired = sizeof( PERSISTENT_SHARE_INFO ) + sizeof( WCHAR ) +
  746. Share->NtPathName.Length + sizeof( WCHAR ) +
  747. Share->ShareName.Length + sizeof( WCHAR );
  748. shareInfo = ALLOCATE_HEAP( bytesRequired, BlockTypePersistShareState );
  749. if (shareInfo == NULL) {
  750. IF_DEBUG(PERSISTENT) {
  751. KdPrint(( "SrvSetupPersistentShare: failed to alloc block for share %ws\n", Share->ShareName.Buffer ));
  752. }
  753. RELEASE_LOCK( &SrvShareLock );
  754. return STATUS_INSUFF_SERVER_RESOURCES;
  755. }
  756. RtlZeroMemory( shareInfo, bytesRequired );
  757. shareInfo->Share = Share;
  758. InitializeListHead( &shareInfo->UpdatesPendingList );
  759. shareInfo->LastIdAccepted = 1;
  760. shareInfo->LastIdCommitted = 1;
  761. shareInfo->FirstRecord = sizeof( PERSISTENT_FILE_HEADER );
  762. INITIALIZE_LOCK( &shareInfo->Lock, SHARE_LOCK_LEVEL, "PersistentShareLock" );
  763. destString = &shareInfo->NameBuffer[0];
  764. RtlCopyMemory( destString,
  765. Share->NtPathName.Buffer,
  766. Share->NtPathName.Length
  767. );
  768. destString += (Share->NtPathName.Length / sizeof(WCHAR));
  769. *destString = L':'; // form up stream name
  770. destString++;
  771. *destString = L'$';
  772. destString++;
  773. RtlCopyMemory( destString,
  774. Share->ShareName.Buffer,
  775. Share->ShareName.Length
  776. );
  777. destString += (Share->ShareName.Length / sizeof(WCHAR));
  778. *destString = L'\0';
  779. RtlInitUnicodeString( &shareInfo->StateFileName,
  780. &shareInfo->NameBuffer[0] );
  781. Share->PersistentStateFile = shareInfo;
  782. Share->PersistentState = PersistentStateActive;
  783. RELEASE_LOCK( &SrvShareLock );
  784. SrvInitializeObjectAttributes_U(
  785. &objectAttributes,
  786. &shareInfo->StateFileName,
  787. OBJ_CASE_INSENSITIVE,
  788. NULL,
  789. NULL
  790. );
  791. allocationSize.QuadPart = shareInfo->FirstRecord +
  792. ( INITIAL_BITMAP_LENGTH * sizeof( PERSISTENT_RECORD ) );
  793. status = IoCreateFile( &h,
  794. GENERIC_READ | GENERIC_WRITE,
  795. &objectAttributes,
  796. &iosb,
  797. &allocationSize,
  798. 0,
  799. 0, // no sharing for this file
  800. FILE_OPEN_IF,
  801. FILE_NO_COMPRESSION,
  802. NULL,
  803. 0,
  804. CreateFileTypeNone,
  805. NULL,
  806. 0 );
  807. if ( NT_SUCCESS( status ) ) {
  808. status = iosb.Status;
  809. }
  810. if ( NT_SUCCESS( status ) ) {
  811. shareInfo->FileHandle = h;
  812. if (Restore) {
  813. // restore settings from file here.
  814. }
  815. // write out the header record
  816. } else {
  817. //
  818. // the create failed. hmmm. looks like we can't support persistent
  819. // handles on this share.
  820. //
  821. Share->PersistentState = PersistentStateInError;
  822. }
  823. return status;
  824. }
  825. NTSTATUS
  826. SrvClosePersistentShare (
  827. IN OUT PSHARE Share,
  828. IN BOOLEAN ClearState
  829. )
  830. {
  831. PAGED_CODE( );
  832. return 0;
  833. }
  834. //
  835. // This code comes from Dr. Dobbs Journal, May 1992
  836. //
  837. unsigned long SrvCRCTable[256] = {
  838. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
  839. 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
  840. 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
  841. 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  842. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
  843. 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
  844. 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
  845. 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  846. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
  847. 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  848. 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
  849. 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  850. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
  851. 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
  852. 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
  853. 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  854. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
  855. 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
  856. 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
  857. 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  858. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
  859. 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
  860. 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
  861. 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  862. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
  863. 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
  864. 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
  865. 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  866. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
  867. 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  868. 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
  869. 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  870. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
  871. 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
  872. 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
  873. 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  874. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
  875. 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
  876. 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
  877. 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  878. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
  879. 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
  880. 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D };
  881. //
  882. // Since we never need to have a "rolling crc" across records, we have
  883. // simplified the crc routine abit by always assuming the starting CRC is
  884. // -1 and we return the crc value rather than stuff it into passed arguement.
  885. //
  886. unsigned long
  887. Crc32(
  888. IN unsigned long cbBuffer,
  889. IN unsigned char *pbBuffer
  890. )
  891. {
  892. unsigned long dwCrc = (unsigned long) -1;
  893. PAGED_CODE( );
  894. while (cbBuffer-- != 0)
  895. {
  896. dwCrc = (dwCrc >> 8) ^ SrvCRCTable[(unsigned char) dwCrc ^ *pbBuffer++];
  897. }
  898. return dwCrc;
  899. }
  900. #if 0
  901. // here's the original, in case anyone needs to refer to it.
  902. VOID
  903. Crc32( unsigned long dwCrc,
  904. unsigned long cbBuffer,
  905. LPVOID pvBuffer,
  906. unsigned long *pNewCrc)
  907. {
  908. unsigned char * pbBuffer = (unsigned char *) pvBuffer;
  909. while (cbBuffer-- != 0)
  910. {
  911. dwCrc = (dwCrc >> 8) ^ SrvCRCTable[(unsigned char) dwCrc ^ *pbBuffer++];
  912. }
  913. *pNewCrc = dwCrc;
  914. }
  915. #endif
  916. #endif // def INCLUDE_SMB_PERSISTENT