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 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 214 if (is_windows_drive_prefix(filename) &&
215 215 filename[2] == '\0')
... ... @@ -253,43 +253,23 @@ static BlockDriver *find_protocol(const char *filename)
253 253 * Detect host devices. By convention, /dev/cdrom[N] is always
254 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 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 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 953  
954 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 972 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
957 973 {
958 974 BDRVRawState *s = bs->opaque;
... ... @@ -1152,6 +1168,7 @@ static int hdev_create(const char *filename, QEMUOptionParameter *options)
1152 1168 static BlockDriver bdrv_host_device = {
1153 1169 .format_name = "host_device",
1154 1170 .instance_size = sizeof(BDRVRawState),
  1171 + .bdrv_probe_device = hdev_probe_device,
1155 1172 .bdrv_open = hdev_open,
1156 1173 .bdrv_close = raw_close,
1157 1174 .bdrv_create = hdev_create,
... ... @@ -1197,6 +1214,14 @@ static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1197 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 1225 static int floppy_is_inserted(BlockDriverState *bs)
1201 1226 {
1202 1227 return fd_open(bs) >= 0;
... ... @@ -1242,6 +1267,7 @@ static int floppy_eject(BlockDriverState *bs, int eject_flag)
1242 1267 static BlockDriver bdrv_host_floppy = {
1243 1268 .format_name = "host_floppy",
1244 1269 .instance_size = sizeof(BDRVRawState),
  1270 + .bdrv_probe_device = floppy_probe_device,
1245 1271 .bdrv_open = floppy_open,
1246 1272 .bdrv_close = raw_close,
1247 1273 .bdrv_create = hdev_create,
... ... @@ -1279,6 +1305,13 @@ static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1279 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 1315 static int cdrom_is_inserted(BlockDriverState *bs)
1283 1316 {
1284 1317 BDRVRawState *s = bs->opaque;
... ... @@ -1323,6 +1356,7 @@ static int cdrom_set_locked(BlockDriverState *bs, int locked)
1323 1356 static BlockDriver bdrv_host_cdrom = {
1324 1357 .format_name = "host_cdrom",
1325 1358 .instance_size = sizeof(BDRVRawState),
  1359 + .bdrv_probe_device = cdrom_probe_device,
1326 1360 .bdrv_open = cdrom_open,
1327 1361 .bdrv_close = raw_close,
1328 1362 .bdrv_create = hdev_create,
... ... @@ -1367,6 +1401,14 @@ static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1367 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 1412 static int cdrom_reopen(BlockDriverState *bs)
1371 1413 {
1372 1414 BDRVRawState *s = bs->opaque;
... ... @@ -1437,6 +1479,7 @@ static int cdrom_set_locked(BlockDriverState *bs, int locked)
1437 1479 static BlockDriver bdrv_host_cdrom = {
1438 1480 .format_name = "host_cdrom",
1439 1481 .instance_size = sizeof(BDRVRawState),
  1482 + .bdrv_probe_device = cdrom_probe_device,
1440 1483 .bdrv_open = cdrom_open,
1441 1484 .bdrv_close = raw_close,
1442 1485 .bdrv_create = hdev_create,
... ... @@ -1466,6 +1509,10 @@ static BlockDriver bdrv_host_cdrom = {
1466 1509  
1467 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 1516 bdrv_register(&bdrv_raw);
1470 1517 bdrv_register(&bdrv_host_device);
1471 1518 #ifdef __linux__
... ...
block/raw-win32.c
... ... @@ -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 318 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
310 319 {
311 320 BDRVRawState *s = bs->opaque;
... ... @@ -391,6 +400,7 @@ static int raw_set_locked(BlockDriverState *bs, int locked)
391 400 static BlockDriver bdrv_host_device = {
392 401 .format_name = "host_device",
393 402 .instance_size = sizeof(BDRVRawState),
  403 + .bdrv_probe_device = hdev_probe_device,
394 404 .bdrv_open = hdev_open,
395 405 .bdrv_close = raw_close,
396 406 .bdrv_flush = raw_flush,
... ...
block_int.h
... ... @@ -48,6 +48,7 @@ struct BlockDriver {
48 48 const char *format_name;
49 49 int instance_size;
50 50 int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
  51 + int (*bdrv_probe_device)(const char *filename);
51 52 int (*bdrv_open)(BlockDriverState *bs, const char *filename, int flags);
52 53 int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num,
53 54 uint8_t *buf, int nb_sectors);
... ... @@ -177,4 +178,8 @@ void *qemu_blockalign(BlockDriverState *bs, size_t size);
177 178  
178 179 extern BlockDriverState *bdrv_first;
179 180  
  181 +#ifdef _WIN32
  182 +int is_windows_drive(const char *filename);
  183 +#endif
  184 +
180 185 #endif /* BLOCK_INT_H */
... ...