Commit 508c7cb3fa666f0c4723946869f318ec7751ecbd

Authored by Christoph Hellwig
Committed by Christoph Hellwig
1 parent f3a5d3f8

block: add bdrv_probe_device method

Add a bdrv_probe_device method to all BlockDriver instances implementing
host devices to move matching of host device types into the actual drivers.
For now we keep exacly the old matching behaviour based on the devices names,
although we really should have better detetion methods based on device
information in the future.

Signed-off-by: Christoph Hellwig <hch@lst.de>
@@ -209,7 +209,7 @@ static int is_windows_drive_prefix(const char *filename) @@ -209,7 +209,7 @@ static int is_windows_drive_prefix(const char *filename)
209 filename[1] == ':'); 209 filename[1] == ':');
210 } 210 }
211 211
212 -static int is_windows_drive(const char *filename) 212 +int is_windows_drive(const char *filename)
213 { 213 {
214 if (is_windows_drive_prefix(filename) && 214 if (is_windows_drive_prefix(filename) &&
215 filename[2] == '\0') 215 filename[2] == '\0')
@@ -253,43 +253,23 @@ static BlockDriver *find_protocol(const char *filename) @@ -253,43 +253,23 @@ static BlockDriver *find_protocol(const char *filename)
253 * Detect host devices. By convention, /dev/cdrom[N] is always 253 * Detect host devices. By convention, /dev/cdrom[N] is always
254 * recognized as a host CDROM. 254 * recognized as a host CDROM.
255 */ 255 */
256 -#ifdef _WIN32  
257 -static BlockDriver *find_hdev_driver(const char *filename)  
258 -{  
259 - if (strstart(filename, "/dev/cdrom", NULL))  
260 - return bdrv_find_format("host_device");  
261 - if (is_windows_drive(filename))  
262 - return bdrv_find_format("host_device");  
263 - return NULL;  
264 -}  
265 -#else  
266 static BlockDriver *find_hdev_driver(const char *filename) 256 static BlockDriver *find_hdev_driver(const char *filename)
267 { 257 {
268 - struct stat st;  
269 -  
270 -#ifdef __linux__  
271 - if (strstart(filename, "/dev/fd", NULL))  
272 - return bdrv_find_format("host_floppy");  
273 - if (strstart(filename, "/dev/cd", NULL))  
274 - return bdrv_find_format("host_cdrom");  
275 -#elif defined(__FreeBSD__)  
276 - if (strstart(filename, "/dev/cd", NULL) ||  
277 - strstart(filename, "/dev/acd", NULL)) {  
278 - return bdrv_find_format("host_cdrom");  
279 - }  
280 -#else  
281 - if (strstart(filename, "/dev/cdrom", NULL))  
282 - return bdrv_find_format("host_device");  
283 -#endif 258 + int score_max = 0, score;
  259 + BlockDriver *drv = NULL, *d;
284 260
285 - if (stat(filename, &st) >= 0 &&  
286 - (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {  
287 - return bdrv_find_format("host_device"); 261 + for (d = first_drv; d; d = d->next) {
  262 + if (d->bdrv_probe_device) {
  263 + score = d->bdrv_probe_device(filename);
  264 + if (score > score_max) {
  265 + score_max = score;
  266 + drv = d;
  267 + }
  268 + }
288 } 269 }
289 270
290 - return NULL; 271 + return drv;
291 } 272 }
292 -#endif  
293 273
294 static BlockDriver *find_image_format(const char *filename) 274 static BlockDriver *find_image_format(const char *filename)
295 { 275 {
block/raw-posix.c
@@ -953,6 +953,22 @@ kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex ma @@ -953,6 +953,22 @@ kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex ma
953 953
954 #endif 954 #endif
955 955
  956 +static int hdev_probe_device(const char *filename)
  957 +{
  958 + struct stat st;
  959 +
  960 + /* allow a dedicated CD-ROM driver to match with a higher priority */
  961 + if (strstart(filename, "/dev/cdrom", NULL))
  962 + return 50;
  963 +
  964 + if (stat(filename, &st) >= 0 &&
  965 + (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
  966 + return 100;
  967 + }
  968 +
  969 + return 0;
  970 +}
  971 +
956 static int hdev_open(BlockDriverState *bs, const char *filename, int flags) 972 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
957 { 973 {
958 BDRVRawState *s = bs->opaque; 974 BDRVRawState *s = bs->opaque;
@@ -1152,6 +1168,7 @@ static int hdev_create(const char *filename, QEMUOptionParameter *options) @@ -1152,6 +1168,7 @@ static int hdev_create(const char *filename, QEMUOptionParameter *options)
1152 static BlockDriver bdrv_host_device = { 1168 static BlockDriver bdrv_host_device = {
1153 .format_name = "host_device", 1169 .format_name = "host_device",
1154 .instance_size = sizeof(BDRVRawState), 1170 .instance_size = sizeof(BDRVRawState),
  1171 + .bdrv_probe_device = hdev_probe_device,
1155 .bdrv_open = hdev_open, 1172 .bdrv_open = hdev_open,
1156 .bdrv_close = raw_close, 1173 .bdrv_close = raw_close,
1157 .bdrv_create = hdev_create, 1174 .bdrv_create = hdev_create,
@@ -1197,6 +1214,14 @@ static int floppy_open(BlockDriverState *bs, const char *filename, int flags) @@ -1197,6 +1214,14 @@ static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1197 return 0; 1214 return 0;
1198 } 1215 }
1199 1216
  1217 +static int floppy_probe_device(const char *filename)
  1218 +{
  1219 + if (strstart(filename, "/dev/fd", NULL))
  1220 + return 100;
  1221 + return 0;
  1222 +}
  1223 +
  1224 +
1200 static int floppy_is_inserted(BlockDriverState *bs) 1225 static int floppy_is_inserted(BlockDriverState *bs)
1201 { 1226 {
1202 return fd_open(bs) >= 0; 1227 return fd_open(bs) >= 0;
@@ -1242,6 +1267,7 @@ static int floppy_eject(BlockDriverState *bs, int eject_flag) @@ -1242,6 +1267,7 @@ static int floppy_eject(BlockDriverState *bs, int eject_flag)
1242 static BlockDriver bdrv_host_floppy = { 1267 static BlockDriver bdrv_host_floppy = {
1243 .format_name = "host_floppy", 1268 .format_name = "host_floppy",
1244 .instance_size = sizeof(BDRVRawState), 1269 .instance_size = sizeof(BDRVRawState),
  1270 + .bdrv_probe_device = floppy_probe_device,
1245 .bdrv_open = floppy_open, 1271 .bdrv_open = floppy_open,
1246 .bdrv_close = raw_close, 1272 .bdrv_close = raw_close,
1247 .bdrv_create = hdev_create, 1273 .bdrv_create = hdev_create,
@@ -1279,6 +1305,13 @@ static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) @@ -1279,6 +1305,13 @@ static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1279 return raw_open_common(bs, filename, flags); 1305 return raw_open_common(bs, filename, flags);
1280 } 1306 }
1281 1307
  1308 +static int cdrom_probe_device(const char *filename)
  1309 +{
  1310 + if (strstart(filename, "/dev/cd", NULL))
  1311 + return 100;
  1312 + return 0;
  1313 +}
  1314 +
1282 static int cdrom_is_inserted(BlockDriverState *bs) 1315 static int cdrom_is_inserted(BlockDriverState *bs)
1283 { 1316 {
1284 BDRVRawState *s = bs->opaque; 1317 BDRVRawState *s = bs->opaque;
@@ -1323,6 +1356,7 @@ static int cdrom_set_locked(BlockDriverState *bs, int locked) @@ -1323,6 +1356,7 @@ static int cdrom_set_locked(BlockDriverState *bs, int locked)
1323 static BlockDriver bdrv_host_cdrom = { 1356 static BlockDriver bdrv_host_cdrom = {
1324 .format_name = "host_cdrom", 1357 .format_name = "host_cdrom",
1325 .instance_size = sizeof(BDRVRawState), 1358 .instance_size = sizeof(BDRVRawState),
  1359 + .bdrv_probe_device = cdrom_probe_device,
1326 .bdrv_open = cdrom_open, 1360 .bdrv_open = cdrom_open,
1327 .bdrv_close = raw_close, 1361 .bdrv_close = raw_close,
1328 .bdrv_create = hdev_create, 1362 .bdrv_create = hdev_create,
@@ -1367,6 +1401,14 @@ static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) @@ -1367,6 +1401,14 @@ static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1367 return 0; 1401 return 0;
1368 } 1402 }
1369 1403
  1404 +static int cdrom_probe_device(const char *filename)
  1405 +{
  1406 + if (strstart(filename, "/dev/cd", NULL) ||
  1407 + strstart(filename, "/dev/acd", NULL))
  1408 + return 100;
  1409 + return 0;
  1410 +}
  1411 +
1370 static int cdrom_reopen(BlockDriverState *bs) 1412 static int cdrom_reopen(BlockDriverState *bs)
1371 { 1413 {
1372 BDRVRawState *s = bs->opaque; 1414 BDRVRawState *s = bs->opaque;
@@ -1437,6 +1479,7 @@ static int cdrom_set_locked(BlockDriverState *bs, int locked) @@ -1437,6 +1479,7 @@ static int cdrom_set_locked(BlockDriverState *bs, int locked)
1437 static BlockDriver bdrv_host_cdrom = { 1479 static BlockDriver bdrv_host_cdrom = {
1438 .format_name = "host_cdrom", 1480 .format_name = "host_cdrom",
1439 .instance_size = sizeof(BDRVRawState), 1481 .instance_size = sizeof(BDRVRawState),
  1482 + .bdrv_probe_device = cdrom_probe_device,
1440 .bdrv_open = cdrom_open, 1483 .bdrv_open = cdrom_open,
1441 .bdrv_close = raw_close, 1484 .bdrv_close = raw_close,
1442 .bdrv_create = hdev_create, 1485 .bdrv_create = hdev_create,
@@ -1466,6 +1509,10 @@ static BlockDriver bdrv_host_cdrom = { @@ -1466,6 +1509,10 @@ static BlockDriver bdrv_host_cdrom = {
1466 1509
1467 static void bdrv_raw_init(void) 1510 static void bdrv_raw_init(void)
1468 { 1511 {
  1512 + /*
  1513 + * Register all the drivers. Note that order is important, the driver
  1514 + * registered last will get probed first.
  1515 + */
1469 bdrv_register(&bdrv_raw); 1516 bdrv_register(&bdrv_raw);
1470 bdrv_register(&bdrv_host_device); 1517 bdrv_register(&bdrv_host_device);
1471 #ifdef __linux__ 1518 #ifdef __linux__
block/raw-win32.c
@@ -306,6 +306,15 @@ static int find_device_type(BlockDriverState *bs, const char *filename) @@ -306,6 +306,15 @@ static int find_device_type(BlockDriverState *bs, const char *filename)
306 } 306 }
307 } 307 }
308 308
  309 +static int hdev_probe_device(const char *filename)
  310 +{
  311 + if (strstart(filename, "/dev/cdrom", NULL))
  312 + return 100;
  313 + if (is_windows_drive(filename))
  314 + return 100;
  315 + return 0;
  316 +}
  317 +
309 static int hdev_open(BlockDriverState *bs, const char *filename, int flags) 318 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
310 { 319 {
311 BDRVRawState *s = bs->opaque; 320 BDRVRawState *s = bs->opaque;
@@ -391,6 +400,7 @@ static int raw_set_locked(BlockDriverState *bs, int locked) @@ -391,6 +400,7 @@ static int raw_set_locked(BlockDriverState *bs, int locked)
391 static BlockDriver bdrv_host_device = { 400 static BlockDriver bdrv_host_device = {
392 .format_name = "host_device", 401 .format_name = "host_device",
393 .instance_size = sizeof(BDRVRawState), 402 .instance_size = sizeof(BDRVRawState),
  403 + .bdrv_probe_device = hdev_probe_device,
394 .bdrv_open = hdev_open, 404 .bdrv_open = hdev_open,
395 .bdrv_close = raw_close, 405 .bdrv_close = raw_close,
396 .bdrv_flush = raw_flush, 406 .bdrv_flush = raw_flush,
block_int.h
@@ -48,6 +48,7 @@ struct BlockDriver { @@ -48,6 +48,7 @@ struct BlockDriver {
48 const char *format_name; 48 const char *format_name;
49 int instance_size; 49 int instance_size;
50 int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename); 50 int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
  51 + int (*bdrv_probe_device)(const char *filename);
51 int (*bdrv_open)(BlockDriverState *bs, const char *filename, int flags); 52 int (*bdrv_open)(BlockDriverState *bs, const char *filename, int flags);
52 int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num, 53 int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num,
53 uint8_t *buf, int nb_sectors); 54 uint8_t *buf, int nb_sectors);
@@ -177,4 +178,8 @@ void *qemu_blockalign(BlockDriverState *bs, size_t size); @@ -177,4 +178,8 @@ void *qemu_blockalign(BlockDriverState *bs, size_t size);
177 178
178 extern BlockDriverState *bdrv_first; 179 extern BlockDriverState *bdrv_first;
179 180
  181 +#ifdef _WIN32
  182 +int is_windows_drive(const char *filename);
  183 +#endif
  184 +
180 #endif /* BLOCK_INT_H */ 185 #endif /* BLOCK_INT_H */