Commit 0e1d8f4c549e51fd19793a154862979fdc199477
Committed by
Christoph Hellwig
1 parent
986c28d6
raw-posix: always store open flags
Both the Linux floppy and the FreeBSD CDROM host device need to store the open flags so that they can re-open the device later. Store the open flags unconditionally to remove the ifdef mess and simply the calling conventions for the later patches in the series. Signed-off-by: Christoph Hellwig <hch@lst.de>
Showing
1 changed file
with
21 additions
and
26 deletions
block/raw-posix.c
... | ... | @@ -103,17 +103,14 @@ typedef struct BDRVRawState { |
103 | 103 | int fd; |
104 | 104 | int type; |
105 | 105 | unsigned int lseek_err_cnt; |
106 | + int open_flags; | |
106 | 107 | #if defined(__linux__) |
107 | 108 | /* linux floppy specific */ |
108 | - int fd_open_flags; | |
109 | 109 | int64_t fd_open_time; |
110 | 110 | int64_t fd_error_time; |
111 | 111 | int fd_got_error; |
112 | 112 | int fd_media_changed; |
113 | 113 | #endif |
114 | -#if defined(__FreeBSD__) | |
115 | - int cd_open_flags; | |
116 | -#endif | |
117 | 114 | uint8_t* aligned_buf; |
118 | 115 | } BDRVRawState; |
119 | 116 | |
... | ... | @@ -130,32 +127,32 @@ static int raw_is_inserted(BlockDriverState *bs); |
130 | 127 | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
131 | 128 | { |
132 | 129 | BDRVRawState *s = bs->opaque; |
133 | - int fd, open_flags, ret; | |
130 | + int fd, ret; | |
134 | 131 | |
135 | 132 | posix_aio_init(); |
136 | 133 | |
137 | 134 | s->lseek_err_cnt = 0; |
138 | 135 | |
139 | - open_flags = O_BINARY; | |
136 | + s->open_flags |= O_BINARY; | |
140 | 137 | if ((flags & BDRV_O_ACCESS) == O_RDWR) { |
141 | - open_flags |= O_RDWR; | |
138 | + s->open_flags |= O_RDWR; | |
142 | 139 | } else { |
143 | - open_flags |= O_RDONLY; | |
140 | + s->open_flags |= O_RDONLY; | |
144 | 141 | bs->read_only = 1; |
145 | 142 | } |
146 | 143 | if (flags & BDRV_O_CREAT) |
147 | - open_flags |= O_CREAT | O_TRUNC; | |
144 | + s->open_flags |= O_CREAT | O_TRUNC; | |
148 | 145 | |
149 | 146 | /* Use O_DSYNC for write-through caching, no flags for write-back caching, |
150 | 147 | * and O_DIRECT for no caching. */ |
151 | 148 | if ((flags & BDRV_O_NOCACHE)) |
152 | - open_flags |= O_DIRECT; | |
149 | + s->open_flags |= O_DIRECT; | |
153 | 150 | else if (!(flags & BDRV_O_CACHE_WB)) |
154 | - open_flags |= O_DSYNC; | |
151 | + s->open_flags |= O_DSYNC; | |
155 | 152 | |
156 | 153 | s->type = FTYPE_FILE; |
157 | 154 | |
158 | - fd = open(filename, open_flags, 0644); | |
155 | + fd = open(filename, s->open_flags, 0644); | |
159 | 156 | if (fd < 0) { |
160 | 157 | ret = -errno; |
161 | 158 | if (ret == -EROFS) |
... | ... | @@ -952,7 +949,7 @@ kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex ma |
952 | 949 | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
953 | 950 | { |
954 | 951 | BDRVRawState *s = bs->opaque; |
955 | - int fd, open_flags, ret; | |
952 | + int fd, ret; | |
956 | 953 | |
957 | 954 | posix_aio_init(); |
958 | 955 | |
... | ... | @@ -982,31 +979,30 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
982 | 979 | IOObjectRelease( mediaIterator ); |
983 | 980 | } |
984 | 981 | #endif |
985 | - open_flags = O_BINARY; | |
982 | + s->open_flags |= O_BINARY; | |
986 | 983 | if ((flags & BDRV_O_ACCESS) == O_RDWR) { |
987 | - open_flags |= O_RDWR; | |
984 | + s->open_flags |= O_RDWR; | |
988 | 985 | } else { |
989 | - open_flags |= O_RDONLY; | |
986 | + s->open_flags |= O_RDONLY; | |
990 | 987 | bs->read_only = 1; |
991 | 988 | } |
992 | 989 | /* Use O_DSYNC for write-through caching, no flags for write-back caching, |
993 | 990 | * and O_DIRECT for no caching. */ |
994 | 991 | if ((flags & BDRV_O_NOCACHE)) |
995 | - open_flags |= O_DIRECT; | |
992 | + s->open_flags |= O_DIRECT; | |
996 | 993 | else if (!(flags & BDRV_O_CACHE_WB)) |
997 | - open_flags |= O_DSYNC; | |
994 | + s->open_flags |= O_DSYNC; | |
998 | 995 | |
999 | 996 | s->type = FTYPE_FILE; |
1000 | 997 | #if defined(__linux__) |
1001 | 998 | if (strstart(filename, "/dev/cd", NULL)) { |
1002 | 999 | /* open will not fail even if no CD is inserted */ |
1003 | - open_flags |= O_NONBLOCK; | |
1000 | + s->open_flags |= O_NONBLOCK; | |
1004 | 1001 | s->type = FTYPE_CD; |
1005 | 1002 | } else if (strstart(filename, "/dev/fd", NULL)) { |
1006 | 1003 | s->type = FTYPE_FD; |
1007 | - s->fd_open_flags = open_flags; | |
1008 | 1004 | /* open will not fail even if no floppy is inserted */ |
1009 | - open_flags |= O_NONBLOCK; | |
1005 | + s->open_flags |= O_NONBLOCK; | |
1010 | 1006 | #ifdef CONFIG_AIO |
1011 | 1007 | } else if (strstart(filename, "/dev/sg", NULL)) { |
1012 | 1008 | bs->sg = 1; |
... | ... | @@ -1017,11 +1013,10 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
1017 | 1013 | if (strstart(filename, "/dev/cd", NULL) || |
1018 | 1014 | strstart(filename, "/dev/acd", NULL)) { |
1019 | 1015 | s->type = FTYPE_CD; |
1020 | - s->cd_open_flags = open_flags; | |
1021 | 1016 | } |
1022 | 1017 | #endif |
1023 | 1018 | s->fd = -1; |
1024 | - fd = open(filename, open_flags, 0644); | |
1019 | + fd = open(filename, s->open_flags, 0644); | |
1025 | 1020 | if (fd < 0) { |
1026 | 1021 | ret = -errno; |
1027 | 1022 | if (ret == -EROFS) |
... | ... | @@ -1073,7 +1068,7 @@ static int fd_open(BlockDriverState *bs) |
1073 | 1068 | #endif |
1074 | 1069 | return -EIO; |
1075 | 1070 | } |
1076 | - s->fd = open(bs->filename, s->fd_open_flags); | |
1071 | + s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK); | |
1077 | 1072 | if (s->fd < 0) { |
1078 | 1073 | s->fd_error_time = qemu_get_clock(rt_clock); |
1079 | 1074 | s->fd_got_error = 1; |
... | ... | @@ -1162,7 +1157,7 @@ static int raw_eject(BlockDriverState *bs, int eject_flag) |
1162 | 1157 | close(s->fd); |
1163 | 1158 | s->fd = -1; |
1164 | 1159 | } |
1165 | - fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK); | |
1160 | + fd = open(bs->filename, s->open_flags | O_NONBLOCK); | |
1166 | 1161 | if (fd >= 0) { |
1167 | 1162 | if (ioctl(fd, FDEJECT, 0) < 0) |
1168 | 1163 | perror("FDEJECT"); |
... | ... | @@ -1258,7 +1253,7 @@ static int cd_open(BlockDriverState *bs) |
1258 | 1253 | * FreeBSD seems to not notice sometimes... */ |
1259 | 1254 | if (s->fd >= 0) |
1260 | 1255 | close (s->fd); |
1261 | - fd = open(bs->filename, s->cd_open_flags, 0644); | |
1256 | + fd = open(bs->filename, s->open_flags, 0644); | |
1262 | 1257 | if (fd < 0) { |
1263 | 1258 | s->fd = -1; |
1264 | 1259 | return -EIO; | ... | ... |