Skip to content

Commit 20eaf1b

Browse files
committed
file-posix: Move check_hdev_writable() up
We'll need to call it in raw_open_common(), so move the function to avoid a forward declaration. Signed-off-by: Kevin Wolf <[email protected]> Message-Id: <[email protected]> Reviewed-by: Max Reitz <[email protected]> Signed-off-by: Kevin Wolf <[email protected]>
1 parent 5edc855 commit 20eaf1b

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

block/file-posix.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,39 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
425425
}
426426
}
427427

428+
static int check_hdev_writable(BDRVRawState *s)
429+
{
430+
#if defined(BLKROGET)
431+
/* Linux block devices can be configured "read-only" using blockdev(8).
432+
* This is independent of device node permissions and therefore open(2)
433+
* with O_RDWR succeeds. Actual writes fail with EPERM.
434+
*
435+
* bdrv_open() is supposed to fail if the disk is read-only. Explicitly
436+
* check for read-only block devices so that Linux block devices behave
437+
* properly.
438+
*/
439+
struct stat st;
440+
int readonly = 0;
441+
442+
if (fstat(s->fd, &st)) {
443+
return -errno;
444+
}
445+
446+
if (!S_ISBLK(st.st_mode)) {
447+
return 0;
448+
}
449+
450+
if (ioctl(s->fd, BLKROGET, &readonly) < 0) {
451+
return -errno;
452+
}
453+
454+
if (readonly) {
455+
return -EACCES;
456+
}
457+
#endif /* defined(BLKROGET) */
458+
return 0;
459+
}
460+
428461
static void raw_parse_flags(int bdrv_flags, int *open_flags, bool has_writers)
429462
{
430463
bool read_write = false;
@@ -3323,39 +3356,6 @@ static int hdev_probe_device(const char *filename)
33233356
return 0;
33243357
}
33253358

3326-
static int check_hdev_writable(BDRVRawState *s)
3327-
{
3328-
#if defined(BLKROGET)
3329-
/* Linux block devices can be configured "read-only" using blockdev(8).
3330-
* This is independent of device node permissions and therefore open(2)
3331-
* with O_RDWR succeeds. Actual writes fail with EPERM.
3332-
*
3333-
* bdrv_open() is supposed to fail if the disk is read-only. Explicitly
3334-
* check for read-only block devices so that Linux block devices behave
3335-
* properly.
3336-
*/
3337-
struct stat st;
3338-
int readonly = 0;
3339-
3340-
if (fstat(s->fd, &st)) {
3341-
return -errno;
3342-
}
3343-
3344-
if (!S_ISBLK(st.st_mode)) {
3345-
return 0;
3346-
}
3347-
3348-
if (ioctl(s->fd, BLKROGET, &readonly) < 0) {
3349-
return -errno;
3350-
}
3351-
3352-
if (readonly) {
3353-
return -EACCES;
3354-
}
3355-
#endif /* defined(BLKROGET) */
3356-
return 0;
3357-
}
3358-
33593359
static void hdev_parse_filename(const char *filename, QDict *options,
33603360
Error **errp)
33613361
{

0 commit comments

Comments
 (0)