usb_disk: use MutexLocker in usb_disk_ioctl()

Change-Id: Ic3994f6267ea24cf776cb067e0d94226132f4c7e
Reviewed-on: https://review.haiku-os.org/c/haiku/+/3638
Reviewed-by: Jérôme Duval <[email protected]>
This commit is contained in:
X512
2021-01-21 08:07:05 +00:00
committed by Jérôme Duval
parent a78cc92a16
commit b4d3d516c7
@@ -18,6 +18,7 @@
#include <kernel.h> #include <kernel.h>
#include <fs/devfs.h> #include <fs/devfs.h>
#include <util/AutoLock.h>
#include "scsi_sense.h" #include "scsi_sense.h"
#include "usb_disk_scsi.h" #include "usb_disk_scsi.h"
@@ -1774,25 +1775,20 @@ usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
{ {
device_lun *lun = (device_lun *)cookie; device_lun *lun = (device_lun *)cookie;
disk_device *device = lun->device; disk_device *device = lun->device;
mutex_lock(&device->lock); MutexLocker locker(&device->lock);
if (device->removed) { if (device->removed)
mutex_unlock(&device->lock);
return B_DEV_NOT_READY; return B_DEV_NOT_READY;
}
status_t result = B_DEV_INVALID_IOCTL;
switch (op) { switch (op) {
case B_GET_DEVICE_SIZE: { case B_GET_DEVICE_SIZE: {
if (lun->media_changed) { if (lun->media_changed) {
result = usb_disk_update_capacity(lun); status_t result = usb_disk_update_capacity(lun);
if (result != B_OK) if (result != B_OK)
break; return result;
} }
size_t size = lun->block_size * lun->block_count; size_t size = lun->block_size * lun->block_count;
result = user_memcpy(buffer, &size, sizeof(size)); return user_memcpy(buffer, &size, sizeof(size));
break;
} }
case B_GET_MEDIA_STATUS: case B_GET_MEDIA_STATUS:
@@ -1810,16 +1806,15 @@ usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
} }
TRACE("B_GET_MEDIA_STATUS: 0x%08" B_PRIx32 "\n", TRACE("B_GET_MEDIA_STATUS: 0x%08" B_PRIx32 "\n",
*(status_t *)buffer); *(status_t *)buffer);
result = B_OK; return B_OK;
break;
} }
case B_GET_GEOMETRY: case B_GET_GEOMETRY:
{ {
if (lun->media_changed) { if (lun->media_changed) {
result = usb_disk_update_capacity(lun); status_t result = usb_disk_update_capacity(lun);
if (result != B_OK) if (result != B_OK)
break; return result;
} }
device_geometry geometry; device_geometry geometry;
@@ -1833,14 +1828,12 @@ usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
TRACE("B_GET_GEOMETRY: %" B_PRId32 " sectors at %" B_PRId32 TRACE("B_GET_GEOMETRY: %" B_PRId32 " sectors at %" B_PRId32
" bytes per sector\n", geometry.cylinder_count, " bytes per sector\n", geometry.cylinder_count,
geometry.bytes_per_sector); geometry.bytes_per_sector);
result = user_memcpy(buffer, &geometry, sizeof(device_geometry)); return user_memcpy(buffer, &geometry, sizeof(device_geometry));
break;
} }
case B_FLUSH_DRIVE_CACHE: case B_FLUSH_DRIVE_CACHE:
TRACE("B_FLUSH_DRIVE_CACHE\n"); TRACE("B_FLUSH_DRIVE_CACHE\n");
result = usb_disk_synchronize(lun, true); return usb_disk_synchronize(lun, true);
break;
case B_EJECT_DEVICE: case B_EJECT_DEVICE:
{ {
@@ -1851,9 +1844,8 @@ usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
commandBlock[1] = lun->logical_unit_number << 5; commandBlock[1] = lun->logical_unit_number << 5;
commandBlock[4] = 2; commandBlock[4] = 2;
result = usb_disk_operation(lun, commandBlock, 6, NULL, NULL, return usb_disk_operation(lun, commandBlock, 6, NULL, NULL,
false); false);
break;
} }
case B_LOAD_MEDIA: case B_LOAD_MEDIA:
@@ -1865,9 +1857,8 @@ usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
commandBlock[1] = lun->logical_unit_number << 5; commandBlock[1] = lun->logical_unit_number << 5;
commandBlock[4] = 3; commandBlock[4] = 3;
result = usb_disk_operation(lun, commandBlock, 6, NULL, NULL, return usb_disk_operation(lun, commandBlock, 6, NULL, NULL,
false); false);
break;
} }
case B_GET_ICON: case B_GET_ICON:
@@ -1907,9 +1898,8 @@ usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
} }
break; break;
} }
result = user_strlcpy((char *)buffer, iconName, return user_strlcpy((char *)buffer, iconName,
B_FILE_NAME_LENGTH); B_FILE_NAME_LENGTH);
break;
} }
case B_GET_VECTOR_ICON: case B_GET_VECTOR_ICON:
@@ -1918,10 +1908,8 @@ usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
char vendor[sizeof(lun->vendor_name)+1]; char vendor[sizeof(lun->vendor_name)+1];
char product[sizeof(lun->product_name)+1]; char product[sizeof(lun->product_name)+1];
if (length != sizeof(device_icon)) { if (length != sizeof(device_icon))
result = B_BAD_VALUE; return B_BAD_VALUE;
break;
}
if (device->is_ufi) { if (device->is_ufi) {
// UFI is specific for floppy drives // UFI is specific for floppy drives
@@ -1954,22 +1942,17 @@ usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
} }
device_icon iconData; device_icon iconData;
if (user_memcpy(&iconData, buffer, sizeof(device_icon)) != B_OK) { if (user_memcpy(&iconData, buffer, sizeof(device_icon)) != B_OK)
result = B_BAD_ADDRESS; return B_BAD_ADDRESS;
break;
}
if (iconData.icon_size >= icon->icon_size) { if (iconData.icon_size >= icon->icon_size) {
if (user_memcpy(iconData.icon_data, icon->icon_data, if (user_memcpy(iconData.icon_data, icon->icon_data,
(size_t)icon->icon_size) != B_OK) { (size_t)icon->icon_size) != B_OK)
result = B_BAD_ADDRESS; return B_BAD_ADDRESS;
break;
}
} }
iconData.icon_size = icon->icon_size; iconData.icon_size = icon->icon_size;
result = user_memcpy(buffer, &iconData, sizeof(device_icon)); return user_memcpy(buffer, &iconData, sizeof(device_icon));
break;
} }
case B_GET_DEVICE_NAME: case B_GET_DEVICE_NAME:
@@ -1983,22 +1966,18 @@ usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
normalize_name(name, nameLength); normalize_name(name, nameLength);
result = user_strlcpy((char *)buffer, name, length); status_t result = user_strlcpy((char *)buffer, name, length);
if (result > 0) if (result > 0)
result = B_OK; result = B_OK;
TRACE_ALWAYS("got device name \"%s\": %s\n", name, TRACE_ALWAYS("got device name \"%s\": %s\n", name,
strerror(result)); strerror(result));
break; return result;
} }
default:
TRACE_ALWAYS("unhandled ioctl %" B_PRId32 "\n", op);
break;
} }
mutex_unlock(&device->lock); TRACE_ALWAYS("unhandled ioctl %" B_PRId32 "\n", op);
return result; return B_DEV_INVALID_IOCTL;
} }