Commit 0e1d8f4c549e51fd19793a154862979fdc199477

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