Commit 90babde0cad8a485e5f74a2113c0425c08395a47

Authored by Christoph Hellwig
Committed by Christoph Hellwig
1 parent 0e1d8f4c

raw-posix: add a raw_open_common helper

raw_open and hdev_open contain the same basic logic.  Add a new
raw_open_common helper containing the guts of the open routine
and call it from raw_open and hdev_open.

We use the new open_flags field in BDRVRawState to allow passing
additional open flags to raw_open_common from both.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Showing 1 changed file with 19 additions and 29 deletions
block/raw-posix.c
... ... @@ -124,7 +124,8 @@ static int cd_open(BlockDriverState *bs);
124 124  
125 125 static int raw_is_inserted(BlockDriverState *bs);
126 126  
127   -static int raw_open(BlockDriverState *bs, const char *filename, int flags)
  127 +static int raw_open_common(BlockDriverState *bs, const char *filename,
  128 + int flags)
128 129 {
129 130 BDRVRawState *s = bs->opaque;
130 131 int fd, ret;
... ... @@ -140,8 +141,6 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
140 141 s->open_flags |= O_RDONLY;
141 142 bs->read_only = 1;
142 143 }
143   - if (flags & BDRV_O_CREAT)
144   - s->open_flags |= O_CREAT | O_TRUNC;
145 144  
146 145 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
147 146 * and O_DIRECT for no caching. */
... ... @@ -150,8 +149,7 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
150 149 else if (!(flags & BDRV_O_CACHE_WB))
151 150 s->open_flags |= O_DSYNC;
152 151  
153   - s->type = FTYPE_FILE;
154   -
  152 + s->fd = -1;
155 153 fd = open(filename, s->open_flags, 0644);
156 154 if (fd < 0) {
157 155 ret = -errno;
... ... @@ -172,6 +170,17 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
172 170 return 0;
173 171 }
174 172  
  173 +static int raw_open(BlockDriverState *bs, const char *filename, int flags)
  174 +{
  175 + BDRVRawState *s = bs->opaque;
  176 +
  177 + s->type = FTYPE_FILE;
  178 + if (flags & BDRV_O_CREAT)
  179 + s->open_flags |= O_CREAT | O_TRUNC;
  180 +
  181 + return raw_open_common(bs, filename, flags);
  182 +}
  183 +
175 184 /* XXX: use host sector size if necessary with:
176 185 #ifdef DIOCGSECTORSIZE
177 186 {
... ... @@ -949,9 +958,7 @@ kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex ma
949 958 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
950 959 {
951 960 BDRVRawState *s = bs->opaque;
952   - int fd, ret;
953   -
954   - posix_aio_init();
  961 + int ret;
955 962  
956 963 #ifdef CONFIG_COCOA
957 964 if (strstart(filename, "/dev/cdrom", NULL)) {
... ... @@ -979,19 +986,6 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
979 986 IOObjectRelease( mediaIterator );
980 987 }
981 988 #endif
982   - s->open_flags |= O_BINARY;
983   - if ((flags & BDRV_O_ACCESS) == O_RDWR) {
984   - s->open_flags |= O_RDWR;
985   - } else {
986   - s->open_flags |= O_RDONLY;
987   - bs->read_only = 1;
988   - }
989   - /* Use O_DSYNC for write-through caching, no flags for write-back caching,
990   - * and O_DIRECT for no caching. */
991   - if ((flags & BDRV_O_NOCACHE))
992   - s->open_flags |= O_DIRECT;
993   - else if (!(flags & BDRV_O_CACHE_WB))
994   - s->open_flags |= O_DSYNC;
995 989  
996 990 s->type = FTYPE_FILE;
997 991 #if defined(__linux__)
... ... @@ -1015,15 +1009,11 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1015 1009 s->type = FTYPE_CD;
1016 1010 }
1017 1011 #endif
1018   - s->fd = -1;
1019   - fd = open(filename, s->open_flags, 0644);
1020   - if (fd < 0) {
1021   - ret = -errno;
1022   - if (ret == -EROFS)
1023   - ret = -EACCES;
  1012 +
  1013 + ret = raw_open_common(bs, filename, flags);
  1014 + if (ret)
1024 1015 return ret;
1025   - }
1026   - s->fd = fd;
  1016 +
1027 1017 #if defined(__FreeBSD__)
1028 1018 /* make sure the door isnt locked at this time */
1029 1019 if (s->type == FTYPE_CD)
... ...