]> code.delx.au - refind/blobdiff - filesystems/fsw_efi.c
Version 0.8.1 release.
[refind] / filesystems / fsw_efi.c
index 55709d43ac52910b2eac3f1118e74e2f6452860f..385883147cc5806d655d614ada81c1b72902157e 100644 (file)
@@ -98,7 +98,7 @@ EFI_GUID gEfiFileSystemVolumeLabelInfoIdGuid = EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
 /** Helper macro for stringification. */
 #define FSW_EFI_STRINGIFY(x) #x
 /** Expands to the EFI driver name given the file system type name. */
-#define FSW_EFI_DRIVER_NAME(t) L"rEFInd 0.6.10 " FSW_EFI_STRINGIFY(t) L" File System Driver"
+#define FSW_EFI_DRIVER_NAME(t) L"rEFInd 0.8.1 " FSW_EFI_STRINGIFY(t) L" File System Driver"
 
 // function prototypes
 
@@ -125,7 +125,7 @@ EFI_STATUS EFIAPI fsw_efi_ComponentName_GetControllerName(IN  EFI_COMPONENT_NAME
 void fsw_efi_change_blocksize(struct fsw_volume *vol,
                               fsw_u32 old_phys_blocksize, fsw_u32 old_log_blocksize,
                               fsw_u32 new_phys_blocksize, fsw_u32 new_log_blocksize);
-fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u32 phys_bno, void *buffer);
+fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *buffer);
 
 EFI_STATUS fsw_efi_map_status(fsw_status_t fsw_status, FSW_VOLUME_DATA *Volume);
 
@@ -162,6 +162,22 @@ EFI_STATUS fsw_efi_dnode_fill_FileInfo(IN FSW_VOLUME_DATA *Volume,
                                        IN OUT UINTN *BufferSize,
                                        OUT VOID *Buffer);
 
+/**
+ * Structure for holding disk cache data.
+ */
+
+#define CACHE_SIZE 131072 /* 128KiB */
+struct cache_data {
+   fsw_u8            *Cache;
+   fsw_u64           CacheStart;
+   BOOLEAN           CacheValid;
+   FSW_VOLUME_DATA   *Volume; // NOTE: Do not deallocate; copied here to ID volume
+};
+
+#define NUM_CACHES 2 /* Don't increase without modifying fsw_efi_read_block() */
+static struct cache_data    Caches[NUM_CACHES];
+static int LastRead = -1;
+
 /**
  * Interface structure for the EFI Driver Binding protocol.
  */
@@ -200,6 +216,22 @@ extern struct fsw_fstype_table   FSW_FSTYPE_TABLE_NAME(FSTYPE);
 
 //#include "OverrideFunctions-kabyl.edk2.c.include"
 
+static VOID EFIAPI fsw_efi_clear_cache(VOID) {
+   int i;
+
+   // clear the cache
+   for (i = 0; i < NUM_CACHES; i++) {
+      if (Caches[i].Cache != NULL) {
+         FreePool(Caches[i].Cache);
+         Caches[i].Cache = NULL;
+      } // if
+            Caches[i].CacheStart = 0;
+            Caches[i].CacheValid = FALSE;
+            Caches[i].Volume = NULL;
+   }
+   LastRead = -1;
+} // VOID EFIAPI fsw_efi_clear_cache();
+
 /**
  * Image entry point. Installs the Driver Binding and Component Name protocols
  * on the image's handle. Actually mounting a file system is initiated through
@@ -339,7 +371,6 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL  *T
                               ControllerHandle,
                               EFI_OPEN_PROTOCOL_BY_DRIVER);
     if (EFI_ERROR(Status)) {
-        Print(L"Fsw ERROR: OpenProtocol(DiskIo) returned %r\n", Status);
         return Status;
     }
 
@@ -443,6 +474,9 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN  EFI_DRIVER_BINDING_PROTOCOL  *T
                                This->DriverBindingHandle,
                                ControllerHandle);
 
+    // clear the cache
+    fsw_efi_clear_cache();
+
     return Status;
 }
 
@@ -495,25 +529,81 @@ void fsw_efi_change_blocksize(struct fsw_volume *vol,
 /**
  * FSW interface function to read data blocks. This function is called by the FSW core
  * to read a block of data from the device. The buffer is allocated by the core code.
+ * Two caches are maintained, so as to improve performance on some systems. (VirtualBox
+ * is particularly susceptible to performance problems with an uncached driver -- the
+ * ext2 driver can take 200 seconds to load a Linux kernel under VirtualBox, whereas
+ * the time is more like 3 seconds with a cache!) Two independent caches are maintained
+ * because the ext2fs driver tends to alternate between accessing two parts of the
+ * disk.
  */
 
-fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u32 phys_bno, void *buffer)
-{
-    EFI_STATUS          Status;
-    FSW_VOLUME_DATA     *Volume = (FSW_VOLUME_DATA *)vol->host_data;
-
-//    FSW_MSG_DEBUGV((FSW_MSGSTR("fsw_efi_read_block: %d  (%d)\n"), phys_bno, vol->phys_blocksize));
-
-    // read from disk
-    Status = refit_call5_wrapper(Volume->DiskIo->ReadDisk, Volume->DiskIo, Volume->MediaId,
-                                      (UINT64)phys_bno * vol->phys_blocksize,
-                                      vol->phys_blocksize,
-                                      buffer);
-    Volume->LastIOStatus = Status;
-    if (EFI_ERROR(Status))
-        return FSW_IO_ERROR;
-    return FSW_SUCCESS;
-}
+fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *buffer) {
+   int              i, ReadCache = -1;
+   FSW_VOLUME_DATA  *Volume = (FSW_VOLUME_DATA *)vol->host_data;
+   EFI_STATUS       Status = EFI_SUCCESS;
+   BOOLEAN          ReadOneBlock = FALSE;
+   fsw_u64          StartRead = phys_bno * vol->phys_blocksize;
+
+   if (buffer == NULL)
+      return (fsw_status_t) EFI_BAD_BUFFER_SIZE;
+
+   // Initialize static data structures, if necessary....
+   if (LastRead < 0) {
+      fsw_efi_clear_cache();
+   } // if
+
+   // Look for a cache hit on the current query....
+   i = 0;
+   do {
+      if ((Caches[i].Volume == Volume) &&
+         (Caches[i].CacheValid == TRUE) &&
+          (StartRead >= Caches[i].CacheStart) &&
+          ((StartRead + vol->phys_blocksize) <= (Caches[i].CacheStart + CACHE_SIZE))) {
+         ReadCache = i;
+      }
+      i++;
+   } while ((i < NUM_CACHES) && (ReadCache < 0));
+
+   // No cache hit found; load new cache and pass it on....
+   if (ReadCache < 0) {
+      if (LastRead == -1)
+         LastRead = 1;
+      ReadCache = 1 - LastRead; // NOTE: If NUM_CACHES > 2, this must become more complex
+      Caches[ReadCache].CacheValid = FALSE;
+      if (Caches[ReadCache].Cache == NULL)
+         Caches[ReadCache].Cache = AllocatePool(CACHE_SIZE);
+      if (Caches[ReadCache].Cache != NULL) {
+         Status = refit_call5_wrapper(Volume->DiskIo->ReadDisk, Volume->DiskIo, Volume->MediaId,
+                                      StartRead, CACHE_SIZE, Caches[ReadCache].Cache);
+         if (!EFI_ERROR(Status)) {
+            Caches[ReadCache].CacheStart = StartRead;
+            Caches[ReadCache].CacheValid = TRUE;
+            Caches[ReadCache].Volume = Volume;
+            LastRead = ReadCache;
+         } else {
+            ReadOneBlock = TRUE;
+         }
+      } else {
+         ReadOneBlock = TRUE;
+      } // if cache memory allocated
+   } // if (ReadCache < 0)
+
+   if (Caches[ReadCache].Cache != NULL && Caches[ReadCache].CacheValid == TRUE) {
+      CopyMem(buffer, &Caches[ReadCache].Cache[StartRead - Caches[ReadCache].CacheStart], vol->phys_blocksize);
+   } else {
+      ReadOneBlock = TRUE;
+   }
+
+   if (ReadOneBlock) { // Something's failed, so try a simple disk read of one block....
+      Status = refit_call5_wrapper(Volume->DiskIo->ReadDisk, Volume->DiskIo, Volume->MediaId,
+                                   phys_bno * vol->phys_blocksize,
+                                   vol->phys_blocksize,
+                                   buffer);
+   }
+   Volume->LastIOStatus = Status;
+
+   return Status;
+} // fsw_status_t *fsw_efi_read_block()
 
 /**
  * Map FSW status codes to EFI status codes. The FSW_IO_ERROR code is only produced
@@ -558,6 +648,7 @@ EFI_STATUS EFIAPI fsw_efi_FileSystem_OpenVolume(IN EFI_FILE_IO_INTERFACE *This,
     Print(L"fsw_efi_FileSystem_OpenVolume\n");
 #endif
 
+    fsw_efi_clear_cache();
     Status = fsw_efi_dnode_to_FileHandle(Volume->vol->root, Root);
 
     return Status;