Skip to content

Commit

Permalink
[FREELDR] fs.c: Fix handling of file handles, (de)referencing, and di…
Browse files Browse the repository at this point in the history
…rect-device access (reactos#7414)

- The original code was just incrementing the reference counts (RefCounts)
  of the device objects or the device/file handles, without decrementing
  them when closing the handles. This is now fixed.

  Notice the following:

  * When opening a file on a device (disk), the device's (and its
    handle's) RefCount is incremented, and the file handle's RefCount
    is incremented as well.

  * When closing a file, the file handle's RefCount is decremented
    (and the file closed if the RefCount reaches zero), and the file's
    parent device handle is also closed, recursively.
    This has the effect of decrementing the parent device handle's
    RefCount, and the device's own RefCount is decremented as well.

  IMPORTANT NOTE: The usefulness of handle-level RefCount is still
  under question, and might be (consistently) removed in the future.

- Fix opening a device (disk) in direct access, when this device is
  already opened. Indeed, we previously allowed direct access only if
  the device was opened as such for the very first time (its RefCount
  = 0 originally); no filesystem mounting was attempted as well.
  Then for any later open-operations on this device (while keeping an
  already-opened handle to it), filesystem access was assumed.

  Thus, this problem would show up in two ways:

  * Either the device is first opened for direct access, this succeeded
    and no filesystem was mounted. Then, for any other open-operations,
    the filesystem was NOT mounted, and opening files on it would fail.
    Direct accesses would succeed but would create an unnecessary second
    file handle.

  * Or, the device is first opened for file-access: a filesystem was
    mounted and file opening would succeed. Any other file opening
    operation would succeed as well (if the file exists). But, a direct
    access open-operation would fail, because now any open-operations on
    the device would be assumed to be a file opening.

  This is now correctly fixed. If direct-open is requested, just do it.
  If this is a file opening, we open the device, then try to mount a
  filesystem on it (if not already done), then we try to open the file.

  If file opening fails, derereference the device.

- Pass the file path to the filesystem-specific Open() functions without
  truncating the leading path separator, if any. This has to be handled
  by the filesystem routines themselves.
  • Loading branch information
HBelusca committed Oct 16, 2024
1 parent 7dcfda8 commit 5d361b6
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 56 deletions.
10 changes: 7 additions & 3 deletions boot/freeldr/freeldr/include/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ typedef struct tagDEVVTBL
#define INVALID_FILE_ID ((ULONG)-1)

ARC_STATUS ArcOpen(CHAR* Path, OPENMODE OpenMode, ULONG* FileId);
ARC_STATUS ArcClose(ULONG FileId);

ARC_STATUS
ArcClose(
_In_ ULONG FileId);

ARC_STATUS ArcRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count);
ARC_STATUS ArcSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode);
ARC_STATUS ArcGetFileInformation(ULONG FileId, FILEINFORMATION* Information);
Expand All @@ -58,7 +62,7 @@ FsRegisterDevice(
_In_ const DEVVTBL* FuncTable);

PCWSTR FsGetServiceName(ULONG FileId);
VOID FsSetDeviceSpecific(ULONG FileId, VOID* Specific);
VOID* FsGetDeviceSpecific(ULONG FileId);
VOID FsSetDeviceSpecific(ULONG FileId, PVOID Specific);
PVOID FsGetDeviceSpecific(ULONG FileId);
ULONG FsGetDeviceId(ULONG FileId);
VOID FsInit(VOID);
3 changes: 3 additions & 0 deletions boot/freeldr/freeldr/lib/fs/ext2.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ BOOLEAN Ext2LookupFile(PEXT2_VOLUME_INFO Volume, PCSTR FileName, PEXT2_FILE_INFO

RtlZeroMemory(Ext2FileInfo, sizeof(EXT2_FILE_INFO));

/* Skip leading path separator, if any */
if (*FileName == '\\' || *FileName == '/')
++FileName;
//
// Figure out how many sub-directories we are nested in
//
Expand Down
3 changes: 3 additions & 0 deletions boot/freeldr/freeldr/lib/fs/fat.c
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,9 @@ ARC_STATUS FatLookupFile(PFAT_VOLUME_INFO Volume, PCSTR FileName, PFAT_FILE_INFO

RtlZeroMemory(FatFileInfoPointer, sizeof(FAT_FILE_INFO));

/* Skip leading path separator, if any */
if (*FileName == '\\' || *FileName == '/')
++FileName;
//
// Figure out how many sub-directories we are nested in
//
Expand Down
Loading

0 comments on commit 5d361b6

Please sign in to comment.