ramdisk: implement trim and icon, add to image

- B_TRIM_DEVICE on a ram disk frees all requested pages. Reading from a
  trimmed page returns all 0s. This can be used with fstrim to release
  memory for the parts not used by the filesystem, without unregistering
  then registering the device.
- Add icon and ioctl to get it.
- Add it to the image, because it works reasonably well and there is no
  reason not to include it.
This commit is contained in:
Adrien Destugues 2016-11-03 17:33:00 +01:00
parent b96788c95d
commit 96fef5d19f
3 changed files with 181 additions and 8 deletions

View File

@ -19,11 +19,9 @@ SYSTEM_BIN = [ FFilterByBuildFeatures
modifiers mount mountvolume
netstat notify
open
package package_repo passwd pc ping ping6 pkgman
prio profile ps
package package_repo passwd pc ping ping6 pkgman prio profile ps
query quit
rc reindex release renice resattr rmattr rmindex roster
route
ramdisk rc reindex release renice resattr rmattr rmindex roster route
safemode screen_blanker screeninfo screenmode setarch setmime settype
setversion setvolume shutdown
strace su sysinfo system_time

View File

@ -64,7 +64,7 @@ if $(TARGET_ARCH) = x86 || $(TARGET_ARCH) = x86_64 {
# drivers
AddNewDriversToPackage disk scsi : scsi_cd scsi_disk ;
AddNewDriversToPackage disk virtual : virtio_block ;
AddNewDriversToPackage disk virtual : virtio_block ram_disk ;
AddNewDriversToPackage power : $(SYSTEM_ADD_ONS_DRIVERS_POWER) ;
#AddNewDriversToPackage display : display_adapter@x86 ;

View File

@ -24,6 +24,7 @@
#include <fs/KPath.h>
#include <lock.h>
#include <util/fs_trim_support.h>
#include <vm/vm.h>
#include <vm/VMCache.h>
#include <vm/vm_page.h>
@ -33,14 +34,94 @@
#include "IOSchedulerSimple.h"
//#define TRACE_CHECK_SUM_DEVICE
#ifdef TRACE_CHECK_SUM_DEVICE
//#define TRACE_RAM_DISK
#ifdef TRACE_RAM_DISK
# define TRACE(x...) dprintf(x)
#else
# define TRACE(x) do {} while (false)
# define TRACE(x...) do {} while (false)
#endif
static const unsigned char kRamdiskIcon[] = {
0x6e, 0x63, 0x69, 0x66, 0x0e, 0x03, 0x01, 0x00, 0x00, 0x02, 0x00, 0x16,
0x02, 0x3c, 0xc7, 0xee, 0x38, 0x9b, 0xc0, 0xba, 0x16, 0x57, 0x3e, 0x39,
0xb0, 0x49, 0x77, 0xc8, 0x42, 0xad, 0xc7, 0x00, 0xff, 0xff, 0xd3, 0x02,
0x00, 0x06, 0x02, 0x3c, 0x96, 0x32, 0x3a, 0x4d, 0x3f, 0xba, 0xfc, 0x01,
0x3d, 0x5a, 0x97, 0x4b, 0x57, 0xa5, 0x49, 0x84, 0x4d, 0x00, 0x47, 0x47,
0x47, 0xff, 0xa5, 0xa0, 0xa0, 0x02, 0x00, 0x16, 0x02, 0xbc, 0x59, 0x2f,
0xbb, 0x29, 0xa7, 0x3c, 0x0c, 0xe4, 0xbd, 0x0b, 0x7c, 0x48, 0x92, 0xc0,
0x4b, 0x79, 0x66, 0x00, 0x7d, 0xff, 0xd4, 0x02, 0x00, 0x06, 0x02, 0x38,
0xdb, 0xb4, 0x39, 0x97, 0x33, 0xbc, 0x4a, 0x33, 0x3b, 0xa5, 0x42, 0x48,
0x6e, 0x66, 0x49, 0xee, 0x7b, 0x00, 0x59, 0x67, 0x56, 0xff, 0xeb, 0xb2,
0xb2, 0x03, 0xa7, 0xff, 0x00, 0x03, 0xff, 0x00, 0x00, 0x04, 0x01, 0x80,
0x03, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x6a, 0x05, 0x33, 0x02,
0x00, 0x06, 0x02, 0x3a, 0x5d, 0x2c, 0x39, 0xf8, 0xb1, 0xb9, 0xdb, 0xf1,
0x3a, 0x4c, 0x0f, 0x48, 0xae, 0xea, 0x4a, 0xc0, 0x91, 0x00, 0x74, 0x74,
0x74, 0xff, 0x3e, 0x3d, 0x3d, 0x02, 0x00, 0x16, 0x02, 0x38, 0x22, 0x1b,
0x3b, 0x11, 0x73, 0xbc, 0x5e, 0xb5, 0x39, 0x4b, 0xaa, 0x4a, 0x47, 0xf1,
0x49, 0xc2, 0x1d, 0x00, 0xb0, 0xff, 0x83, 0x02, 0x00, 0x16, 0x03, 0x36,
0xed, 0xe9, 0x36, 0xb9, 0x49, 0xba, 0x0a, 0xf6, 0x3a, 0x32, 0x6f, 0x4a,
0x79, 0xef, 0x4b, 0x03, 0xe7, 0x00, 0x5a, 0x38, 0xdc, 0xff, 0x7e, 0x0d,
0x0a, 0x06, 0x22, 0x3c, 0x22, 0x49, 0x44, 0x5b, 0x5a, 0x3e, 0x5a, 0x31,
0x39, 0x25, 0x0a, 0x04, 0x22, 0x3c, 0x44, 0x4b, 0x5a, 0x31, 0x39, 0x25,
0x0a, 0x04, 0x44, 0x4b, 0x44, 0x5b, 0x5a, 0x3e, 0x5a, 0x31, 0x0a, 0x04,
0x22, 0x3c, 0x22, 0x49, 0x44, 0x5b, 0x44, 0x4b, 0x08, 0x02, 0x27, 0x43,
0xb8, 0x14, 0xc1, 0xf1, 0x08, 0x02, 0x26, 0x43, 0x29, 0x44, 0x0a, 0x05,
0x44, 0x5d, 0x49, 0x5d, 0x60, 0x3e, 0x5a, 0x3b, 0x5b, 0x3f, 0x0a, 0x04,
0x3c, 0x5a, 0x5a, 0x3c, 0x5a, 0x36, 0x3c, 0x52, 0x0a, 0x04, 0x24, 0x4e,
0x3c, 0x5a, 0x3c, 0x52, 0x24, 0x48, 0x06, 0x07, 0xaa, 0x3f, 0x42, 0x2e,
0x24, 0x48, 0x3c, 0x52, 0x5a, 0x36, 0x51, 0x33, 0x51, 0x33, 0x50, 0x34,
0x4b, 0x33, 0x4d, 0x34, 0x49, 0x32, 0x49, 0x30, 0x48, 0x31, 0x49, 0x30,
0x06, 0x08, 0xfa, 0xfa, 0x42, 0x50, 0x3e, 0x54, 0x40, 0x55, 0x3f, 0xc7,
0xeb, 0x41, 0xc8, 0x51, 0x42, 0xc9, 0x4f, 0x42, 0xc8, 0xda, 0x42, 0xca,
0x41, 0xc0, 0xf1, 0x5d, 0x45, 0xca, 0x81, 0x46, 0xc7, 0xb7, 0x46, 0xc8,
0xa9, 0x46, 0xc7, 0x42, 0x44, 0x51, 0x45, 0xc6, 0xb9, 0x43, 0xc6, 0x53,
0x0a, 0x07, 0x3c, 0x5c, 0x40, 0x5c, 0x42, 0x5e, 0x48, 0x5e, 0x4a, 0x5c,
0x46, 0x5a, 0x45, 0x4b, 0x06, 0x09, 0x9a, 0xf6, 0x03, 0x42, 0x2e, 0x24,
0x48, 0x4e, 0x3c, 0x5a, 0x5a, 0x3c, 0x36, 0x51, 0x33, 0x51, 0x33, 0x50,
0x34, 0x4b, 0x33, 0x4d, 0x34, 0x49, 0x32, 0x49, 0x30, 0x48, 0x31, 0x49,
0x30, 0x18, 0x0a, 0x07, 0x01, 0x06, 0x00, 0x0a, 0x00, 0x01, 0x00, 0x10,
0x01, 0x17, 0x84, 0x00, 0x04, 0x0a, 0x01, 0x01, 0x01, 0x00, 0x0a, 0x02,
0x01, 0x02, 0x00, 0x0a, 0x03, 0x01, 0x03, 0x00, 0x0a, 0x04, 0x01, 0x04,
0x10, 0x01, 0x17, 0x85, 0x20, 0x04, 0x0a, 0x06, 0x01, 0x05, 0x30, 0x24,
0xb3, 0x99, 0x01, 0x17, 0x82, 0x00, 0x04, 0x0a, 0x05, 0x01, 0x05, 0x30,
0x20, 0xb2, 0xe6, 0x01, 0x17, 0x82, 0x00, 0x04, 0x0a, 0x09, 0x01, 0x0b,
0x02, 0x3e, 0x9b, 0x12, 0xb5, 0xf9, 0x99, 0x36, 0x19, 0x10, 0x3e, 0xc0,
0x21, 0x48, 0xed, 0x4d, 0xc8, 0x5a, 0x02, 0x0a, 0x09, 0x01, 0x0b, 0x02,
0x3e, 0x9b, 0x12, 0xb5, 0xf9, 0x99, 0x36, 0x19, 0x10, 0x3e, 0xc0, 0x21,
0x48, 0x4c, 0xd4, 0xc7, 0x9c, 0x11, 0x0a, 0x09, 0x01, 0x0b, 0x02, 0x3e,
0x9b, 0x12, 0xb5, 0xf9, 0x99, 0x36, 0x19, 0x10, 0x3e, 0xc0, 0x21, 0x47,
0x5c, 0xe7, 0xc6, 0x2c, 0x1a, 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 0x7b,
0x5e, 0x48, 0x53, 0xa1, 0xc6, 0xa0, 0xb6, 0x01, 0x17, 0x84, 0x22, 0x04,
0x0a, 0x0d, 0x01, 0x0a, 0x02, 0x3e, 0x98, 0xfd, 0xb5, 0xf6, 0x6c, 0x35,
0xc9, 0x3d, 0x3e, 0x7b, 0x5e, 0x48, 0x53, 0xa1, 0xc6, 0xa0, 0xb6, 0x0a,
0x08, 0x01, 0x0a, 0x12, 0x3e, 0x98, 0xfd, 0xb5, 0xf6, 0x6c, 0x35, 0xc9,
0x3d, 0x3e, 0x7b, 0x5e, 0x47, 0x69, 0xe9, 0xc4, 0xa6, 0x5a, 0x01, 0x17,
0x84, 0x22, 0x04, 0x0a, 0x0d, 0x01, 0x0a, 0x02, 0x3e, 0x98, 0xfd, 0xb5,
0xf6, 0x6c, 0x35, 0xc9, 0x3d, 0x3e, 0x7b, 0x5e, 0x47, 0x69, 0xe9, 0xc4,
0xa6, 0x5a, 0x0a, 0x08, 0x01, 0x0a, 0x12, 0x3e, 0x98, 0xfd, 0xb5, 0xf6,
0x6c, 0x35, 0xc9, 0x3d, 0x3e, 0x7b, 0x5e, 0x46, 0x2c, 0x90, 0xb8, 0xd1,
0xff, 0x01, 0x17, 0x84, 0x22, 0x04, 0x0a, 0x0d, 0x01, 0x0a, 0x02, 0x3e,
0x98, 0xfd, 0xb5, 0xf6, 0x6c, 0x35, 0xc9, 0x3d, 0x3e, 0x7b, 0x5e, 0x46,
0x2c, 0x90, 0xb8, 0xd1, 0xff
};
// parameters for the DMA resource
static const uint32 kDMAResourceBufferCount = 16;
static const uint32 kDMAResourceBounceBufferCount = 16;
@ -446,6 +527,62 @@ struct RawDevice : Device, DoublyLinkedListLinkImpl<RawDevice> {
return error;
}
status_t Trim(fs_trim_data* trimData)
{
TRACE("trim_device()\n");
uint64 trimmedSize = 0;
for (uint32 i = 0; i < trimData->range_count; i++) {
trimmedSize += trimData->ranges[i].size;
off_t offset = trimData->ranges[i].offset;
off_t length = trimData->ranges[i].size;
// Round up offset and length to multiple of the page size
// The offset is rounded up, so some space may be left
// (not trimmed) at the start of the range.
offset = (offset + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1);
// Adjust the length for the possibly skipped range
length -= trimData->ranges[i].offset - offset;
// The length is rounded down, so some space at the end may also
// be left (not trimmed).
length &= ~(B_PAGE_SIZE - 1);
TRACE("ramdisk: trim %" B_PRIdOFF " bytes from %" B_PRIdOFF "\n",
length, offset);
ASSERT(offset % B_PAGE_SIZE == 0);
ASSERT(length % B_PAGE_SIZE == 0);
vm_page** pages = new(std::nothrow) vm_page*[length / B_PAGE_SIZE];
if (pages == NULL)
return B_NO_MEMORY;
ArrayDeleter<vm_page*> pagesDeleter(pages);
_GetPages(offset, length, false, pages);
AutoLocker<VMCache> locker(fCache);
uint32 j;
for (j = 0; j < length / B_PAGE_SIZE; j++) {
// If we run out of pages (some may already be trimmed), stop.
if (pages[j] == NULL)
break;
TRACE("free range %" B_PRIu32 ", page %" B_PRIu32 ", offset %"
B_PRIdOFF "\n", i, j, offset);
if (pages[j]->Cache())
fCache->RemovePage(pages[j]);
vm_page_free(NULL, pages[j]);
}
}
trimData->trimmed_size = trimmedSize;
return B_OK;
}
status_t DoIO(IORequest* request)
{
return fIOScheduler->ScheduleRequest(request);
@ -1290,6 +1427,28 @@ ram_disk_raw_device_control(void* _cookie, uint32 op, void* buffer,
return user_memcpy(buffer, &status, sizeof(status_t));
}
case B_GET_ICON_NAME:
return user_strlcpy((char*)buffer, "devices/drive-ramdisk",
B_FILE_NAME_LENGTH);
case B_GET_VECTOR_ICON:
{
device_icon iconData;
if (length != sizeof(device_icon))
return B_BAD_VALUE;
if (user_memcpy(&iconData, buffer, sizeof(device_icon)) != B_OK)
return B_BAD_ADDRESS;
if (iconData.icon_size >= (int32)sizeof(kRamdiskIcon)) {
if (user_memcpy(iconData.icon_data, kRamdiskIcon,
sizeof(kRamdiskIcon)) != B_OK)
return B_BAD_ADDRESS;
}
iconData.icon_size = sizeof(kRamdiskIcon);
return user_memcpy(buffer, &iconData, sizeof(device_icon));
}
case B_SET_UNINTERRUPTABLE_IO:
case B_SET_INTERRUPTABLE_IO:
case B_FLUSH_DRIVE_CACHE:
@ -1307,6 +1466,22 @@ ram_disk_raw_device_control(void* _cookie, uint32 op, void* buffer,
return B_OK;
}
case B_TRIM_DEVICE:
{
fs_trim_data* trimData;
MemoryDeleter deleter;
status_t status = get_trim_data_from_user(buffer, length, deleter,
trimData);
if (status != B_OK)
return status;
status = device->Trim(trimData);
if (status != B_OK)
return status;
return copy_trim_data_to_user(buffer, trimData);
}
case RAM_DISK_IOCTL_INFO:
return handle_ioctl(device, &ioctl_info, buffer);
}