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 <jerome.duval@gmail.com>
This commit is contained in:
X512 2021-01-17 10:51:09 +09:00 committed by Jérôme Duval
parent a78cc92a16
commit b4d3d516c7

View File

@ -18,6 +18,7 @@
#include <kernel.h>
#include <fs/devfs.h>
#include <util/AutoLock.h>
#include "scsi_sense.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;
disk_device *device = lun->device;
mutex_lock(&device->lock);
if (device->removed) {
mutex_unlock(&device->lock);
MutexLocker locker(&device->lock);
if (device->removed)
return B_DEV_NOT_READY;
}
status_t result = B_DEV_INVALID_IOCTL;
switch (op) {
case B_GET_DEVICE_SIZE: {
if (lun->media_changed) {
result = usb_disk_update_capacity(lun);
status_t result = usb_disk_update_capacity(lun);
if (result != B_OK)
break;
return result;
}
size_t size = lun->block_size * lun->block_count;
result = user_memcpy(buffer, &size, sizeof(size));
break;
return user_memcpy(buffer, &size, sizeof(size));
}
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",
*(status_t *)buffer);
result = B_OK;
break;
return B_OK;
}
case B_GET_GEOMETRY:
{
if (lun->media_changed) {
result = usb_disk_update_capacity(lun);
status_t result = usb_disk_update_capacity(lun);
if (result != B_OK)
break;
return result;
}
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
" bytes per sector\n", geometry.cylinder_count,
geometry.bytes_per_sector);
result = user_memcpy(buffer, &geometry, sizeof(device_geometry));
break;
return user_memcpy(buffer, &geometry, sizeof(device_geometry));
}
case B_FLUSH_DRIVE_CACHE:
TRACE("B_FLUSH_DRIVE_CACHE\n");
result = usb_disk_synchronize(lun, true);
break;
return usb_disk_synchronize(lun, true);
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[4] = 2;
result = usb_disk_operation(lun, commandBlock, 6, NULL, NULL,
return usb_disk_operation(lun, commandBlock, 6, NULL, NULL,
false);
break;
}
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[4] = 3;
result = usb_disk_operation(lun, commandBlock, 6, NULL, NULL,
return usb_disk_operation(lun, commandBlock, 6, NULL, NULL,
false);
break;
}
case B_GET_ICON:
@ -1907,9 +1898,8 @@ usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
}
break;
}
result = user_strlcpy((char *)buffer, iconName,
return user_strlcpy((char *)buffer, iconName,
B_FILE_NAME_LENGTH);
break;
}
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 product[sizeof(lun->product_name)+1];
if (length != sizeof(device_icon)) {
result = B_BAD_VALUE;
break;
}
if (length != sizeof(device_icon))
return B_BAD_VALUE;
if (device->is_ufi) {
// 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;
if (user_memcpy(&iconData, buffer, sizeof(device_icon)) != B_OK) {
result = B_BAD_ADDRESS;
break;
}
if (user_memcpy(&iconData, buffer, sizeof(device_icon)) != B_OK)
return B_BAD_ADDRESS;
if (iconData.icon_size >= icon->icon_size) {
if (user_memcpy(iconData.icon_data, icon->icon_data,
(size_t)icon->icon_size) != B_OK) {
result = B_BAD_ADDRESS;
break;
}
(size_t)icon->icon_size) != B_OK)
return B_BAD_ADDRESS;
}
iconData.icon_size = icon->icon_size;
result = user_memcpy(buffer, &iconData, sizeof(device_icon));
break;
return user_memcpy(buffer, &iconData, sizeof(device_icon));
}
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);
result = user_strlcpy((char *)buffer, name, length);
status_t result = user_strlcpy((char *)buffer, name, length);
if (result > 0)
result = B_OK;
TRACE_ALWAYS("got device name \"%s\": %s\n", name,
strerror(result));
break;
return result;
}
default:
TRACE_ALWAYS("unhandled ioctl %" B_PRId32 "\n", op);
break;
}
mutex_unlock(&device->lock);
return result;
TRACE_ALWAYS("unhandled ioctl %" B_PRId32 "\n", op);
return B_DEV_INVALID_IOCTL;
}