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.

1234 lines
40 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. dir.c
  5. Abstract:
  6. This module contains the code to implement the NtQueryDirectoryFile,
  7. and the NtNotifyChangeDirectoryFile system services for the NT I/O system.
  8. Author:
  9. Darryl E. Havens (darrylh) 21-Jun-1989
  10. Environment:
  11. Kernel mode only
  12. Revision History:
  13. --*/
  14. #include "iomgr.h"
  15. NTSTATUS
  16. BuildQueryDirectoryIrp(
  17. IN HANDLE FileHandle,
  18. IN HANDLE Event OPTIONAL,
  19. IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
  20. IN PVOID ApcContext OPTIONAL,
  21. OUT PIO_STATUS_BLOCK IoStatusBlock,
  22. OUT PVOID FileInformation,
  23. IN ULONG Length,
  24. IN FILE_INFORMATION_CLASS FileInformationClass,
  25. IN BOOLEAN ReturnSingleEntry,
  26. IN PUNICODE_STRING FileName OPTIONAL,
  27. IN BOOLEAN RestartScan,
  28. IN UCHAR MinorFunction,
  29. OUT BOOLEAN *SynchronousIo,
  30. OUT PDEVICE_OBJECT *DeviceObject,
  31. OUT PIRP *Irp,
  32. OUT PFILE_OBJECT *FileObject,
  33. OUT KPROCESSOR_MODE *RequestorMode
  34. );
  35. #ifdef ALLOC_PRAGMA
  36. #pragma alloc_text(PAGE, BuildQueryDirectoryIrp)
  37. #pragma alloc_text(PAGE, NtQueryDirectoryFile)
  38. #pragma alloc_text(PAGE, NtNotifyChangeDirectoryFile)
  39. #endif
  40. NTSTATUS
  41. BuildQueryDirectoryIrp(
  42. IN HANDLE FileHandle,
  43. IN HANDLE Event OPTIONAL,
  44. IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
  45. IN PVOID ApcContext OPTIONAL,
  46. OUT PIO_STATUS_BLOCK IoStatusBlock,
  47. OUT PVOID FileInformation,
  48. IN ULONG Length,
  49. IN FILE_INFORMATION_CLASS FileInformationClass,
  50. IN BOOLEAN ReturnSingleEntry,
  51. IN PUNICODE_STRING FileName OPTIONAL,
  52. IN BOOLEAN RestartScan,
  53. IN UCHAR MinorFunction,
  54. OUT BOOLEAN *SynchronousIo,
  55. OUT PDEVICE_OBJECT *DeviceObject,
  56. OUT PIRP *Irp,
  57. OUT PFILE_OBJECT *FileObject,
  58. OUT KPROCESSOR_MODE *RequestorMode
  59. )
  60. /*++
  61. Routine Description:
  62. This service operates on a directory file or OLE container specified by the
  63. FileHandle parameter. The service returns information about files in the
  64. directory or embeddings and streams in the container specified by the file
  65. handle. The ReturnSingleEntry parameter specifies that only a single entry
  66. should be returned rather than filling the buffer. The actual number of
  67. files whose information is returned, is the smallest of the following:
  68. o One entry, if the ReturnSingleEntry parameter is TRUE.
  69. o The number of entries whose information fits into the specified
  70. buffer.
  71. o The number of entries that exist.
  72. o One entry if the optional FileName parameter is specified.
  73. If the optional FileName parameter is specified, then the only information
  74. that is returned is for that single entries, if it exists. Note that the
  75. file name may not specify any wildcard characters according to the naming
  76. conventions of the target file system. The ReturnSingleEntry parameter is
  77. simply ignored.
  78. The information that is obtained about the entries in the directory or OLE
  79. container is based on the FileInformationClass parameter. Legal values are
  80. hard coded based on the MinorFunction.
  81. Arguments:
  82. FileHandle - Supplies a handle to the directory file or OLE container for
  83. which information should be returned.
  84. Event - Supplies an optional event to be set to the Signaled state when
  85. the query is complete.
  86. ApcRoutine - Supplies an optional APC routine to be executed when the
  87. query is complete.
  88. ApcContext - Supplies a context parameter to be passed to the ApcRoutine,
  89. if an ApcRoutine was specified.
  90. IoStatusBlock - Address of the caller's I/O status block.
  91. FileInformation - Supplies a buffer to receive the requested information
  92. returned about the contents of the directory.
  93. Length - Supplies the length, in bytes, of the FileInformation buffer.
  94. FileInformationClass - Specfies the type of information that is to be
  95. returned about the files in the specified directory or OLE container.
  96. ReturnSingleEntry - Supplies a BOOLEAN value that, if TRUE, indicates that
  97. only a single entry should be returned.
  98. FileName - Optionally supplies a file name within the specified directory
  99. or OLE container.
  100. RestartScan - Supplies a BOOLEAN value that, if TRUE, indicates that the
  101. scan should be restarted from the beginning. This parameter must be
  102. set to TRUE by the caller the first time the service is invoked.
  103. MinorFunction - IRP_MN_QUERY_DIRECTORY or IRP_MN_QUERY_OLE_DIRECTORY
  104. SynchronousIo - pointer to returned BOOLEAN; TRUE if synchronous I/O
  105. DeviceObject - pointer to returned pointer to device object
  106. Irp - pointer to returned pointer to device object
  107. FileObject - pointer to returned pointer to file object
  108. RequestorMode - pointer to returned requestor mode
  109. Return Value:
  110. The status returned is STATUS_SUCCESS if a valid irp was created for the
  111. query operation.
  112. --*/
  113. {
  114. PIRP irp;
  115. NTSTATUS status;
  116. PFILE_OBJECT fileObject;
  117. PDEVICE_OBJECT deviceObject;
  118. PKEVENT eventObject = (PKEVENT) NULL;
  119. KPROCESSOR_MODE requestorMode;
  120. PCHAR auxiliaryBuffer = (PCHAR) NULL;
  121. PIO_STACK_LOCATION irpSp;
  122. PMDL mdl;
  123. PETHREAD CurrentThread;
  124. PAGED_CODE();
  125. //
  126. // Get the previous mode; i.e., the mode of the caller.
  127. //
  128. CurrentThread = PsGetCurrentThread ();
  129. requestorMode = KeGetPreviousModeByThread(&CurrentThread->Tcb);
  130. *RequestorMode = requestorMode;
  131. try {
  132. if (requestorMode != KernelMode) {
  133. ULONG operationlength = 0; // assume invalid
  134. //
  135. // The caller's access mode is not kernel so probe and validate
  136. // each of the arguments as necessary. If any failures occur,
  137. // the condition handler will be invoked to handle them. It
  138. // will simply cleanup and return an access violation status
  139. // code back to the system service dispatcher.
  140. //
  141. //
  142. // The IoStatusBlock parameter must be writeable by the caller.
  143. //
  144. ProbeForWriteIoStatusEx( IoStatusBlock, ApcRoutine);
  145. //
  146. // Ensure that the FileInformationClass parameter is legal for
  147. // querying information about files in the directory or object.
  148. //
  149. if (FileInformationClass == FileDirectoryInformation) {
  150. operationlength = sizeof(FILE_DIRECTORY_INFORMATION);
  151. } else if (MinorFunction == IRP_MN_QUERY_DIRECTORY) {
  152. switch (FileInformationClass)
  153. {
  154. case FileFullDirectoryInformation:
  155. operationlength = sizeof(FILE_FULL_DIR_INFORMATION);
  156. break;
  157. case FileIdFullDirectoryInformation:
  158. operationlength = sizeof(FILE_ID_FULL_DIR_INFORMATION);
  159. break;
  160. case FileBothDirectoryInformation:
  161. operationlength = sizeof(FILE_BOTH_DIR_INFORMATION);
  162. break;
  163. case FileIdBothDirectoryInformation:
  164. operationlength = sizeof(FILE_ID_BOTH_DIR_INFORMATION);
  165. break;
  166. case FileNamesInformation:
  167. operationlength = sizeof(FILE_NAMES_INFORMATION);
  168. break;
  169. case FileObjectIdInformation:
  170. operationlength = sizeof(FILE_OBJECTID_INFORMATION);
  171. break;
  172. case FileQuotaInformation:
  173. operationlength = sizeof(FILE_QUOTA_INFORMATION);
  174. break;
  175. case FileReparsePointInformation:
  176. operationlength = sizeof(FILE_REPARSE_POINT_INFORMATION);
  177. break;
  178. }
  179. }
  180. //
  181. // If the FileInformationClass parameter is illegal, fail now.
  182. //
  183. if (operationlength == 0) {
  184. return STATUS_INVALID_INFO_CLASS;
  185. }
  186. //
  187. // Ensure that the caller's supplied buffer is at least large enough
  188. // to contain the fixed part of the structure required for this
  189. // query.
  190. //
  191. if (Length < operationlength) {
  192. return STATUS_INFO_LENGTH_MISMATCH;
  193. }
  194. //
  195. // The FileInformation buffer must be writeable by the caller.
  196. //
  197. #if defined(_X86_)
  198. ProbeForWrite( FileInformation, Length, sizeof( ULONG ) );
  199. #elif defined(_WIN64)
  200. //
  201. // If we are a wow64 process, follow the X86 rules
  202. //
  203. if (PsGetCurrentProcessByThread(CurrentThread)->Wow64Process) {
  204. ProbeForWrite( FileInformation, Length, sizeof( ULONG ) );
  205. } else {
  206. ProbeForWrite( FileInformation,
  207. Length,
  208. IopQuerySetAlignmentRequirement[FileInformationClass] );
  209. }
  210. #else
  211. ProbeForWrite( FileInformation,
  212. Length,
  213. IopQuerySetAlignmentRequirement[FileInformationClass] );
  214. #endif
  215. }
  216. //
  217. // If the optional FileName parameter was specified, then it must be
  218. // readable by the caller. Capture the file name string in a pool
  219. // block. Note that if an error occurs during the copy, the cleanup
  220. // code in the exception handler will deallocate the pool before
  221. // returning an access violation status.
  222. //
  223. if (ARGUMENT_PRESENT( FileName )) {
  224. UNICODE_STRING fileName;
  225. PUNICODE_STRING nameBuffer;
  226. //
  227. // Capture the string descriptor itself to ensure that the
  228. // string is readable by the caller without the caller being
  229. // able to change the memory while its being checked.
  230. //
  231. if (requestorMode != KernelMode) {
  232. fileName = ProbeAndReadUnicodeString( FileName );
  233. } else {
  234. fileName = *FileName;
  235. }
  236. if (fileName.Length) {
  237. //
  238. // The length of the string is non-zero, so probe the
  239. // buffer described by the descriptor if the caller was
  240. // not kernel mode. Likewise, if the caller's mode was
  241. // not kernel, then check the length of the name string
  242. // to ensure that it is not too long.
  243. //
  244. if (requestorMode != KernelMode) {
  245. ProbeForRead( fileName.Buffer,
  246. fileName.Length,
  247. sizeof( UCHAR ) );
  248. //
  249. // account for unicode
  250. //
  251. if (fileName.Length > MAXIMUM_FILENAME_LENGTH<<1) {
  252. ExRaiseStatus( STATUS_INVALID_PARAMETER );
  253. }
  254. }
  255. //
  256. // Allocate an auxiliary buffer large enough to contain
  257. // a file name descriptor and to hold the entire file
  258. // name itself. Copy the body of the string into the
  259. // buffer.
  260. //
  261. auxiliaryBuffer = ExAllocatePoolWithQuota( NonPagedPool,
  262. fileName.Length + sizeof( UNICODE_STRING ) );
  263. RtlCopyMemory( auxiliaryBuffer + sizeof( UNICODE_STRING ),
  264. fileName.Buffer,
  265. fileName.Length );
  266. //
  267. // Finally, build the Unicode string descriptor in the
  268. // auxiliary buffer.
  269. //
  270. nameBuffer = (PUNICODE_STRING) auxiliaryBuffer;
  271. nameBuffer->Length = fileName.Length;
  272. nameBuffer->MaximumLength = fileName.Length;
  273. nameBuffer->Buffer = (PWSTR) (auxiliaryBuffer + sizeof( UNICODE_STRING ) );
  274. }
  275. }
  276. } except(EXCEPTION_EXECUTE_HANDLER) {
  277. //
  278. // An exception was incurred while probing the caller's buffers,
  279. // attempting to allocate a pool buffer, or while trying to copy
  280. // the caller's data. Determine what happened, clean everything
  281. // up, and return an appropriate error status code.
  282. //
  283. if (auxiliaryBuffer) {
  284. ExFreePool( auxiliaryBuffer );
  285. }
  286. return GetExceptionCode();
  287. }
  288. //
  289. // There were no blatant errors so far, so reference the file object so
  290. // the target device object can be found. Note that if the handle does
  291. // not refer to a file object, or if the caller does not have the required
  292. // access to the file, then it will fail.
  293. //
  294. status = ObReferenceObjectByHandle( FileHandle,
  295. FILE_LIST_DIRECTORY,
  296. IoFileObjectType,
  297. requestorMode,
  298. (PVOID *) &fileObject,
  299. (POBJECT_HANDLE_INFORMATION) NULL );
  300. if (!NT_SUCCESS( status )) {
  301. if (auxiliaryBuffer) {
  302. ExFreePool( auxiliaryBuffer );
  303. }
  304. return status;
  305. }
  306. *FileObject = fileObject;
  307. //
  308. // If this file has an I/O completion port associated w/it, then ensure
  309. // that the caller did not supply an APC routine, as the two are mutually
  310. // exclusive methods for I/O completion notification.
  311. //
  312. if (fileObject->CompletionContext && IopApcRoutinePresent( ApcRoutine )) {
  313. ObDereferenceObject( fileObject );
  314. if (auxiliaryBuffer) {
  315. ExFreePool( auxiliaryBuffer );
  316. }
  317. return STATUS_INVALID_PARAMETER;
  318. }
  319. //
  320. // Get the address of the event object and set the event to the Not-
  321. // Signaled state, if an event was specified. Note here, too, that if
  322. // the handle does not refer to an event, or if the event cannot be
  323. // written, then the reference will fail.
  324. //
  325. if (ARGUMENT_PRESENT( Event )) {
  326. status = ObReferenceObjectByHandle( Event,
  327. EVENT_MODIFY_STATE,
  328. ExEventObjectType,
  329. requestorMode,
  330. (PVOID *) &eventObject,
  331. (POBJECT_HANDLE_INFORMATION) NULL );
  332. if (!NT_SUCCESS( status )) {
  333. if (auxiliaryBuffer) {
  334. ExFreePool( auxiliaryBuffer );
  335. }
  336. ObDereferenceObject( fileObject );
  337. return status;
  338. } else {
  339. KeClearEvent( eventObject );
  340. }
  341. }
  342. //
  343. // Make a special check here to determine whether this is a synchronous
  344. // I/O operation. If it is, then wait here until the file is owned by
  345. // the current thread.
  346. //
  347. if (fileObject->Flags & FO_SYNCHRONOUS_IO) {
  348. BOOLEAN interrupted;
  349. if (!IopAcquireFastLock( fileObject )) {
  350. status = IopAcquireFileObjectLock( fileObject,
  351. requestorMode,
  352. (BOOLEAN) ((fileObject->Flags & FO_ALERTABLE_IO) != 0),
  353. &interrupted );
  354. if (interrupted) {
  355. if (auxiliaryBuffer != NULL) {
  356. ExFreePool( auxiliaryBuffer );
  357. }
  358. if (eventObject != NULL) {
  359. ObDereferenceObject( eventObject );
  360. }
  361. ObDereferenceObject( fileObject );
  362. return status;
  363. }
  364. }
  365. *SynchronousIo = TRUE;
  366. } else {
  367. *SynchronousIo = FALSE;
  368. }
  369. //
  370. // Set the file object to the Not-Signaled state.
  371. //
  372. KeClearEvent( &fileObject->Event );
  373. //
  374. // Get the address of the target device object.
  375. //
  376. deviceObject = IoGetRelatedDeviceObject( fileObject );
  377. *DeviceObject = deviceObject;
  378. //
  379. // Allocate and initialize the I/O Request Packet (IRP) for this operation.
  380. // The allocation is performed with an exception handler in case the
  381. // caller does not have enough quota to allocate the packet.
  382. irp = IoAllocateIrp( deviceObject->StackSize, TRUE );
  383. if (!irp) {
  384. //
  385. // An IRP could not be allocated. Cleanup and return an appropriate
  386. // error status code.
  387. //
  388. IopAllocateIrpCleanup( fileObject, eventObject );
  389. if (auxiliaryBuffer) {
  390. ExFreePool( auxiliaryBuffer );
  391. }
  392. return STATUS_INSUFFICIENT_RESOURCES;
  393. }
  394. *Irp = irp;
  395. irp->Tail.Overlay.OriginalFileObject = fileObject;
  396. irp->Tail.Overlay.Thread = CurrentThread;
  397. irp->RequestorMode = requestorMode;
  398. //
  399. // Fill in the service independent parameters in the IRP.
  400. //
  401. irp->UserEvent = eventObject;
  402. irp->UserIosb = IoStatusBlock;
  403. irp->Overlay.AsynchronousParameters.UserApcRoutine = ApcRoutine;
  404. irp->Overlay.AsynchronousParameters.UserApcContext = ApcContext;
  405. //
  406. // Get a pointer to the stack location for the first driver. This will be
  407. // used to pass the original function codes and parameters.
  408. //
  409. irpSp = IoGetNextIrpStackLocation( irp );
  410. irpSp->MajorFunction = IRP_MJ_DIRECTORY_CONTROL;
  411. irpSp->MinorFunction = MinorFunction;
  412. irpSp->FileObject = fileObject;
  413. // Also, copy the caller's parameters to the service-specific portion of
  414. // the IRP.
  415. //
  416. irp->Tail.Overlay.AuxiliaryBuffer = auxiliaryBuffer;
  417. irp->AssociatedIrp.SystemBuffer = (PVOID) NULL;
  418. irp->MdlAddress = (PMDL) NULL;
  419. //
  420. // Now determine whether this driver expects to have data buffered to it
  421. // or whether it performs direct I/O. This is based on the DO_BUFFERED_IO
  422. // flag in the device object. If the flag is set, then a system buffer is
  423. // allocated and the driver's data will be copied into it. Otherwise, a
  424. // Memory Descriptor List (MDL) is allocated and the caller's buffer is
  425. // locked down using it.
  426. //
  427. if (deviceObject->Flags & DO_BUFFERED_IO) {
  428. //
  429. // The device does not support direct I/O. Allocate a system buffer
  430. // and specify that it should be deallocated on completion. Also
  431. // indicate that this is an input operation so the data will be copied
  432. // into the caller's buffer. This is done using an exception handler
  433. // that will perform cleanup if the operation fails.
  434. //
  435. try {
  436. //
  437. // Allocate the intermediary system buffer from nonpaged pool and
  438. // charge quota for it.
  439. //
  440. irp->AssociatedIrp.SystemBuffer =
  441. ExAllocatePoolWithQuota( NonPagedPool, Length );
  442. } except(EXCEPTION_EXECUTE_HANDLER) {
  443. //
  444. // An exception was incurred while either probing the caller's
  445. // buffer or allocate the system buffer. Determine what actually
  446. // happened, clean everything up, and return an appropriate error
  447. // status code.
  448. //
  449. IopExceptionCleanup( fileObject,
  450. irp,
  451. eventObject,
  452. (PKEVENT) NULL );
  453. if (auxiliaryBuffer != NULL) {
  454. ExFreePool( auxiliaryBuffer );
  455. }
  456. return GetExceptionCode();
  457. }
  458. //
  459. // Remember the address of the caller's buffer so the copy can take
  460. // place during I/O completion. Also, set the flags so that the
  461. // completion code knows to do the copy and to deallocate the buffer.
  462. //
  463. irp->UserBuffer = FileInformation;
  464. irp->Flags = (ULONG) (IRP_BUFFERED_IO |
  465. IRP_DEALLOCATE_BUFFER |
  466. IRP_INPUT_OPERATION);
  467. } else if (deviceObject->Flags & DO_DIRECT_IO) {
  468. //
  469. // This is a direct I/O operation. Allocate an MDL and invoke the
  470. // memory management routine to lock the buffer into memory. This is
  471. // done using an exception handler that will perform cleanup if the
  472. // operation fails.
  473. //
  474. mdl = (PMDL) NULL;
  475. try {
  476. //
  477. // Allocate an MDL, charging quota for it, and hang it off of the
  478. // IRP. Probe and lock the pages associated with the caller's
  479. // buffer for write access and fill in the MDL with the PFNs of
  480. // those pages.
  481. //
  482. mdl = IoAllocateMdl( FileInformation, Length, FALSE, TRUE, irp );
  483. if (mdl == NULL) {
  484. ExRaiseStatus( STATUS_INSUFFICIENT_RESOURCES );
  485. }
  486. MmProbeAndLockPages( mdl, requestorMode, IoWriteAccess );
  487. } except(EXCEPTION_EXECUTE_HANDLER) {
  488. //
  489. // An exception was incurred while either probing the caller's
  490. // buffer or allocating the MDL. Determine what actually happened,
  491. // clean everything up, and return an appropriate error status code.
  492. //
  493. IopExceptionCleanup( fileObject,
  494. irp,
  495. eventObject,
  496. (PKEVENT) NULL );
  497. if (auxiliaryBuffer != NULL) {
  498. ExFreePool( auxiliaryBuffer );
  499. }
  500. return GetExceptionCode();
  501. }
  502. } else {
  503. //
  504. // Pass the address of the user's buffer so the driver has access to
  505. // it. It is now the driver's responsibility to do everything.
  506. //
  507. irp->UserBuffer = FileInformation;
  508. }
  509. //
  510. // Copy the caller's parameters to the service-specific portion of the
  511. // IRP.
  512. //
  513. irpSp->Parameters.QueryDirectory.Length = Length;
  514. irpSp->Parameters.QueryDirectory.FileInformationClass = FileInformationClass;
  515. irpSp->Parameters.QueryDirectory.FileIndex = 0;
  516. irpSp->Parameters.QueryDirectory.FileName = (PSTRING) auxiliaryBuffer;
  517. irpSp->Flags = 0;
  518. if (RestartScan) {
  519. irpSp->Flags = SL_RESTART_SCAN;
  520. }
  521. if (ReturnSingleEntry) {
  522. irpSp->Flags |= SL_RETURN_SINGLE_ENTRY;
  523. }
  524. irp->Flags |= IRP_DEFER_IO_COMPLETION;
  525. //
  526. // Return with everything set up for the caller to complete the I/O.
  527. //
  528. return STATUS_SUCCESS;
  529. }
  530. NTSTATUS
  531. NtQueryDirectoryFile(
  532. IN HANDLE FileHandle,
  533. IN HANDLE Event OPTIONAL,
  534. IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
  535. IN PVOID ApcContext OPTIONAL,
  536. OUT PIO_STATUS_BLOCK IoStatusBlock,
  537. OUT PVOID FileInformation,
  538. IN ULONG Length,
  539. IN FILE_INFORMATION_CLASS FileInformationClass,
  540. IN BOOLEAN ReturnSingleEntry,
  541. IN PUNICODE_STRING FileName OPTIONAL,
  542. IN BOOLEAN RestartScan
  543. )
  544. /*++
  545. Routine Description:
  546. This service operates on a directory file specified by the FileHandle
  547. parameter. The service returns information about files in the directory
  548. specified by the file handle. The ReturnSingleEntry parameter specifies
  549. that only a single entry should be returned rather than filling the buffer.
  550. The actual number of files whose information is returned, is the smallest
  551. of the following:
  552. o One entry, if the ReturnSingleEntry parameter is TRUE.
  553. o The number of files whose information fits into the specified
  554. buffer.
  555. o The number of files that exist.
  556. o One entry if the optional FileName parameter is specified.
  557. If the optional FileName parameter is specified, then the only information
  558. that is returned is for that single file, if it exists. Note that the
  559. file name may not specify any wildcard characters according to the naming
  560. conventions of the target file system. The ReturnSingleEntry parameter is
  561. simply ignored.
  562. The information that is obtained about the files in the directory is based
  563. on the FileInformationClass parameter. The legal values are as follows:
  564. o FileNamesInformation
  565. o FileDirectoryInformation
  566. o FileFullDirectoryInformation
  567. o FileBothDirectoryInformation
  568. o FileIdBothDirectoryInformation
  569. Arguments:
  570. FileHandle - Supplies a handle to the directory file for which information
  571. should be returned.
  572. Event - Supplies an optional event to be set to the Signaled state when
  573. the query is complete.
  574. ApcRoutine - Supplies an optional APC routine to be executed when the
  575. query is complete.
  576. ApcContext - Supplies a context parameter to be passed to the ApcRoutine,
  577. if an ApcRoutine was specified.
  578. IoStatusBlock - Address of the caller's I/O status block.
  579. FileInformation - Supplies a buffer to receive the requested information
  580. returned about the contents of the directory.
  581. Length - Supplies the length, in bytes, of the FileInformation buffer.
  582. FileInformationClass - Specfies the type of information that is to be
  583. returned about the files in the specified directory.
  584. ReturnSingleEntry - Supplies a BOOLEAN value that, if TRUE, indicates that
  585. only a single entry should be returned.
  586. FileName - Optionally supplies a file name within the specified directory.
  587. RestartScan - Supplies a BOOLEAN value that, if TRUE, indicates that the
  588. scan should be restarted from the beginning. This parameter must be
  589. set to TRUE by the caller the first time the service is invoked.
  590. Return Value:
  591. The status returned is success if the query operation was properly queued
  592. to the I/O system. Once the operation completes, the status of the query
  593. can be determined by examining the Status field of the I/O status block.
  594. --*/
  595. {
  596. NTSTATUS status;
  597. BOOLEAN synchronousIo;
  598. PDEVICE_OBJECT deviceObject;
  599. PIRP irp;
  600. PFILE_OBJECT fileObject;
  601. KPROCESSOR_MODE requestorMode;
  602. PAGED_CODE();
  603. //
  604. // Build the irp with the appropriate minor function & allowed info levels.
  605. //
  606. status = BuildQueryDirectoryIrp( FileHandle,
  607. Event,
  608. ApcRoutine,
  609. ApcContext,
  610. IoStatusBlock,
  611. FileInformation,
  612. Length,
  613. FileInformationClass,
  614. ReturnSingleEntry,
  615. FileName,
  616. RestartScan,
  617. IRP_MN_QUERY_DIRECTORY,
  618. &synchronousIo,
  619. &deviceObject,
  620. &irp,
  621. &fileObject,
  622. &requestorMode);
  623. if (status == STATUS_SUCCESS) {
  624. //
  625. // Queue the packet, call the driver, and synchronize appopriately with
  626. // I/O completion.
  627. //
  628. status = IopSynchronousServiceTail( deviceObject,
  629. irp,
  630. fileObject,
  631. TRUE,
  632. requestorMode,
  633. synchronousIo,
  634. OtherTransfer );
  635. }
  636. return status;
  637. }
  638. NTSTATUS
  639. NtNotifyChangeDirectoryFile(
  640. IN HANDLE FileHandle,
  641. IN HANDLE Event OPTIONAL,
  642. IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
  643. IN PVOID ApcContext OPTIONAL,
  644. OUT PIO_STATUS_BLOCK IoStatusBlock,
  645. OUT PVOID Buffer,
  646. IN ULONG Length,
  647. IN ULONG CompletionFilter,
  648. IN BOOLEAN WatchTree
  649. )
  650. /*++
  651. Routine Description:
  652. This service monitors a directory file for changes. Once a change is
  653. made to the directory specified by the FileHandle parameter, the I/O
  654. operation is completed.
  655. Arguments:
  656. FileHandle - Supplies a handle to the file whose EAs should be changed.
  657. Event - Supplies an optional event to be set to the Signaled state when the
  658. change is complete.
  659. ApcRoutine - Supplies an optional APC routine to be executed when the change
  660. is complete.
  661. ApcContext - Supplies a context parameter to be passed to the ApcRoutine,
  662. if an ApcRoutine was specified.
  663. IoStatusBlock - Address of the caller's I/O status block.
  664. Buffer - Address of variable to receive the names of the files or
  665. directories that have changed since the last time that the service
  666. was invoked.
  667. Length - Length of the output buffer. On the first call, this parameter
  668. also serves as a guideline for how large to make the system's
  669. internal buffer. Specifying a buffer length of zero causes the request
  670. to complete when changes are made, but no information about the
  671. changes are returned.
  672. CompletionFilter - Indicates the types of changes to files or directories
  673. within the directory that will complete the I/O operation.
  674. WatchTree - A BOOLEAN value that indicates whether or not changes to
  675. directories below the directory referred to by the FileHandle
  676. parameter cause the operation to complete.
  677. Return Value:
  678. The status returned is success if the operation was properly queued to the
  679. I/O system. Once the operation completes, the status of the operation can
  680. be determined by examining the Status field of the I/O status block.
  681. --*/
  682. {
  683. PIRP irp;
  684. NTSTATUS status;
  685. PFILE_OBJECT fileObject;
  686. PDEVICE_OBJECT deviceObject;
  687. PKEVENT eventObject = (PKEVENT) NULL;
  688. KPROCESSOR_MODE requestorMode;
  689. PIO_STACK_LOCATION irpSp;
  690. BOOLEAN synchronousIo;
  691. PETHREAD CurrentThread;
  692. PAGED_CODE();
  693. //
  694. // Get the previous mode; i.e., the mode of the caller.
  695. //
  696. CurrentThread = PsGetCurrentThread ();
  697. requestorMode = KeGetPreviousModeByThread(&CurrentThread->Tcb);
  698. if (requestorMode != KernelMode) {
  699. //
  700. // The caller's access mode is user, so probe each of the arguments
  701. // and capture them as necessary. If any failures occur, the condition
  702. // handler will be invoked to handle them. It will simply cleanup and
  703. // return an access violation status code back to the system service
  704. // dispatcher.
  705. //
  706. try {
  707. //
  708. // The IoStatusBlock parameter must be writeable by the caller.
  709. //
  710. ProbeForWriteIoStatusEx( IoStatusBlock , ApcRoutine);
  711. //
  712. // The Buffer parameter must be writeable by the caller.
  713. //
  714. if (Length != 0) {
  715. ProbeForWrite( Buffer,
  716. Length,
  717. sizeof( ULONG ) );
  718. }
  719. } except(EXCEPTION_EXECUTE_HANDLER) {
  720. //
  721. // An exception was incurred probing the caller's I/O status
  722. // block. Simply return the appropriate error status code.
  723. //
  724. return GetExceptionCode();
  725. }
  726. //
  727. // The CompletionFilter parameter must not contain any values which
  728. // are illegal, nor may it not specifiy anything at all. Likewise,
  729. // the caller must supply a non-null buffer.
  730. //
  731. if (((CompletionFilter & ~FILE_NOTIFY_VALID_MASK) ||
  732. !CompletionFilter)) {
  733. return STATUS_INVALID_PARAMETER;
  734. }
  735. }
  736. //
  737. // There were no blatant errors so far, so reference the file object so
  738. // the target device object can be found. Note that if the handle does
  739. // not refer to a file object, or if the caller does not have the required
  740. // access to the file, then it will fail.
  741. //
  742. status = ObReferenceObjectByHandle( FileHandle,
  743. FILE_LIST_DIRECTORY,
  744. IoFileObjectType,
  745. requestorMode,
  746. (PVOID *) &fileObject,
  747. (POBJECT_HANDLE_INFORMATION) NULL );
  748. if (!NT_SUCCESS( status )) {
  749. return status;
  750. }
  751. //
  752. // If this file has an I/O completion port associated w/it, then ensure
  753. // that the caller did not supply an APC routine, as the two are mutually
  754. // exclusive methods for I/O completion notification.
  755. //
  756. if (fileObject->CompletionContext && IopApcRoutinePresent( ApcRoutine )) {
  757. ObDereferenceObject( fileObject );
  758. return STATUS_INVALID_PARAMETER;
  759. }
  760. //
  761. // Get the address of the event object and set the event to the Not-
  762. // Signaled state, if an event was specified. Note here too, that if
  763. // the handle does not refer to an event, or if the event cannot be
  764. // written, then the reference will fail.
  765. //
  766. if (ARGUMENT_PRESENT( Event )) {
  767. status = ObReferenceObjectByHandle( Event,
  768. EVENT_MODIFY_STATE,
  769. ExEventObjectType,
  770. requestorMode,
  771. (PVOID *) &eventObject,
  772. (POBJECT_HANDLE_INFORMATION) NULL );
  773. if (!NT_SUCCESS( status )) {
  774. ObDereferenceObject( fileObject );
  775. return status;
  776. } else {
  777. KeClearEvent( eventObject );
  778. }
  779. }
  780. //
  781. // Make a special check here to determine whether this is a synchronous
  782. // I/O operation. If it is, then wait here until the file is owned by
  783. // the current thread.
  784. //
  785. if (fileObject->Flags & FO_SYNCHRONOUS_IO) {
  786. BOOLEAN interrupted;
  787. if (!IopAcquireFastLock( fileObject )) {
  788. status = IopAcquireFileObjectLock( fileObject,
  789. requestorMode,
  790. (BOOLEAN) ((fileObject->Flags & FO_ALERTABLE_IO) != 0),
  791. &interrupted );
  792. if (interrupted) {
  793. if (eventObject != NULL) {
  794. ObDereferenceObject( eventObject );
  795. }
  796. ObDereferenceObject( fileObject );
  797. return status;
  798. }
  799. }
  800. synchronousIo = TRUE;
  801. } else {
  802. synchronousIo = FALSE;
  803. }
  804. //
  805. // Set the file object to the Not-Signaled state.
  806. //
  807. KeClearEvent( &fileObject->Event );
  808. //
  809. // Get the address of the target device object.
  810. //
  811. deviceObject = IoGetRelatedDeviceObject( fileObject );
  812. //
  813. // Allocate and initialize the I/O Request Packet (IRP) for this operation.
  814. // The allocation is performed with an exception handler in case the
  815. // caller does not have enough quota to allocate the packet.
  816. irp = IoAllocateIrp( deviceObject->StackSize, TRUE );
  817. if (!irp) {
  818. //
  819. // An IRP could not be allocated. Cleanup and return an appropriate
  820. // error status code.
  821. //
  822. IopAllocateIrpCleanup( fileObject, eventObject );
  823. return STATUS_INSUFFICIENT_RESOURCES;
  824. }
  825. irp->Tail.Overlay.OriginalFileObject = fileObject;
  826. irp->Tail.Overlay.Thread = CurrentThread;
  827. irp->RequestorMode = requestorMode;
  828. //
  829. // Fill in the service independent parameters in the IRP.
  830. //
  831. irp->UserEvent = eventObject;
  832. irp->UserIosb = IoStatusBlock;
  833. irp->Overlay.AsynchronousParameters.UserApcRoutine = ApcRoutine;
  834. irp->Overlay.AsynchronousParameters.UserApcContext = ApcContext;
  835. //
  836. // Get a pointer to the stack location for the first driver. This will be
  837. // used to pass the original function codes and the parameters.
  838. //
  839. irpSp = IoGetNextIrpStackLocation( irp );
  840. irpSp->MajorFunction = IRP_MJ_DIRECTORY_CONTROL;
  841. irpSp->MinorFunction = IRP_MN_NOTIFY_CHANGE_DIRECTORY;
  842. irpSp->FileObject = fileObject;
  843. //
  844. // Now determine whether this device expects to have data buffered to it
  845. // or whether it performs direct I/O. This is based on the DO_BUFFERED_IO
  846. // flag in the device object. If the flag is set, then a system buffer is
  847. // allocated and the driver's data will be copied into it. Otherwise, a
  848. // Memory Descriptor List (MDL) is allocated and the caller's buffer is
  849. // locked down using it.
  850. //
  851. if (Length != 0) {
  852. if (deviceObject->Flags & DO_BUFFERED_IO) {
  853. //
  854. // The device does not support direct I/O. Allocate a system
  855. // buffer and specify that it should be deallocated on completion.
  856. // Also indicate that this is an input operation so the data will
  857. // be copied into the caller's buffer. This is done using an
  858. // exception handler that will perform cleanup if the operation
  859. // fails.
  860. //
  861. try {
  862. //
  863. // Allocate the intermediary system buffer from nonpaged pool
  864. // and charge quota for it.
  865. //
  866. irp->AssociatedIrp.SystemBuffer =
  867. ExAllocatePoolWithQuota( NonPagedPool, Length );
  868. } except(EXCEPTION_EXECUTE_HANDLER) {
  869. //
  870. // An exception was incurred while attempting to allocate the
  871. // intermediary system buffer. Clean everything up and return
  872. // an appropriate error status code.
  873. //
  874. IopExceptionCleanup( fileObject,
  875. irp,
  876. eventObject,
  877. (PKEVENT) NULL );
  878. return GetExceptionCode();
  879. }
  880. //
  881. // Remember the address of the caller's buffer so the copy can take
  882. // place during I/O completion. Also, set the flags so that the
  883. // completion code knows to do the copy and to deallocate the
  884. // buffer.
  885. //
  886. irp->UserBuffer = Buffer;
  887. irp->Flags = IRP_BUFFERED_IO |
  888. IRP_DEALLOCATE_BUFFER |
  889. IRP_INPUT_OPERATION;
  890. } else if (deviceObject->Flags & DO_DIRECT_IO) {
  891. //
  892. // This is a direct I/O operation. Allocate an MDL and invoke the
  893. // memory management routine to lock the buffer into memory. This
  894. // is done using an exception handler that will perform cleanup if
  895. // the operation fails.
  896. //
  897. PMDL mdl;
  898. mdl = (PMDL) NULL;
  899. try {
  900. //
  901. // Allocate an MDL, charging quota for it, and hang it off of
  902. // the IRP. Probe and lock the pages associated with the
  903. // caller's buffer for write access and fill in the MDL with
  904. // the PFNs of those pages.
  905. //
  906. mdl = IoAllocateMdl( Buffer, Length, FALSE, TRUE, irp );
  907. if (mdl == NULL) {
  908. ExRaiseStatus( STATUS_INSUFFICIENT_RESOURCES );
  909. }
  910. MmProbeAndLockPages( mdl, requestorMode, IoWriteAccess );
  911. } except(EXCEPTION_EXECUTE_HANDLER) {
  912. //
  913. // An exception was incurred while either probing the caller's
  914. // buffer of allocating the MDL. Determine what actually
  915. // happened, clean everything up, and return an appropriate
  916. // error status code.
  917. //
  918. IopExceptionCleanup( fileObject,
  919. irp,
  920. eventObject,
  921. (PKEVENT) NULL );
  922. return GetExceptionCode();
  923. }
  924. } else {
  925. //
  926. // Pass the address of the user's buffer so the driver has access
  927. // to it. It is now the driver's responsibility to do everything.
  928. //
  929. irp->UserBuffer = Buffer;
  930. }
  931. }
  932. //
  933. // Copy the caller's parameters to the service-specific portion of the
  934. // IRP.
  935. //
  936. irpSp->Parameters.NotifyDirectory.Length = Length;
  937. irpSp->Parameters.NotifyDirectory.CompletionFilter = CompletionFilter;
  938. if (WatchTree) {
  939. irpSp->Flags = SL_WATCH_TREE;
  940. }
  941. //
  942. // Queue the packet, call the driver, and synchronize appopriately with
  943. // I/O completion.
  944. //
  945. return IopSynchronousServiceTail( deviceObject,
  946. irp,
  947. fileObject,
  948. FALSE,
  949. requestorMode,
  950. synchronousIo,
  951. OtherTransfer );
  952. }