debuggers.hg

view tools/blktap/drivers/block-sync.c @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children 5c0bf00e371d
line source
1 /* block-sync.c
2 *
3 * simple slow synchronous raw disk implementation.
4 *
5 * (c) 2006 Andrew Warfield and Julian Chesterfield
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation; or, when distributed
10 * separately from the Linux kernel or incorporated into other
11 * software packages, subject to the following license:
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this source file (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use, copy, modify,
16 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 * IN THE SOFTWARE.
30 */
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <sys/statvfs.h>
38 #include <sys/stat.h>
39 #include <sys/ioctl.h>
40 #include "tapdisk.h"
41 #include "blk.h"
43 /* *BSD has no O_LARGEFILE */
44 #ifndef O_LARGEFILE
45 #define O_LARGEFILE 0
46 #endif
48 struct tdsync_state {
49 int fd;
50 int poll_pipe[2]; /* dummy fd for polling on */
51 };
53 /*Get Image size, secsize*/
54 static int get_image_info(struct td_state *s, int fd)
55 {
56 int ret;
57 long size;
58 unsigned long total_size;
59 struct statvfs statBuf;
60 struct stat stat;
62 ret = fstat(fd, &stat);
63 if (ret != 0) {
64 DPRINTF("ERROR: fstat failed, Couldn't stat image");
65 return -EINVAL;
66 }
68 if (S_ISBLK(stat.st_mode)) {
69 /*Accessing block device directly*/
70 if (blk_getimagesize(fd, &s->size) != 0)
71 return -EINVAL;
73 DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost "
74 "sector_shift [%llu]\n",
75 (long long unsigned)(s->size << SECTOR_SHIFT),
76 (long long unsigned)s->size);
78 /*Get the sector size*/
79 if (blk_getsectorsize(fd, &s->sector_size) != 0)
80 s->sector_size = DEFAULT_SECTOR_SIZE;
82 } else {
83 /*Local file? try fstat instead*/
84 s->size = (stat.st_size >> SECTOR_SHIFT);
85 s->sector_size = DEFAULT_SECTOR_SIZE;
86 DPRINTF("Image size: \n\tpre sector_shift [%lluu]\n\tpost "
87 "sector_shift [%lluu]\n",
88 (long long unsigned)(s->size << SECTOR_SHIFT),
89 (long long unsigned)s->size);
90 }
92 if (s->size == 0)
93 return -EINVAL;
95 s->info = 0;
97 return 0;
98 }
100 static inline void init_fds(struct disk_driver *dd)
101 {
102 int i;
103 struct tdsync_state *prv = (struct tdsync_state *)dd->private;
105 for(i = 0; i < MAX_IOFD; i++)
106 dd->io_fd[i] = 0;
108 dd->io_fd[0] = prv->poll_pipe[0];
109 }
111 /* Open the disk file and initialize aio state. */
112 int tdsync_open (struct disk_driver *dd, const char *name, td_flag_t flags)
113 {
114 int i, fd, ret = 0, o_flags;
115 struct td_state *s = dd->td_state;
116 struct tdsync_state *prv = (struct tdsync_state *)dd->private;
118 /* set up a pipe so that we can hand back a poll fd that won't fire.*/
119 ret = pipe(prv->poll_pipe);
120 if (ret != 0)
121 return (0 - errno);
123 /* Open the file */
124 o_flags = O_DIRECT | O_LARGEFILE |
125 ((flags == TD_RDONLY) ? O_RDONLY : O_RDWR);
126 fd = open(name, o_flags);
128 if ( (fd == -1) && (errno == EINVAL) ) {
130 /* Maybe O_DIRECT isn't supported. */
131 o_flags &= ~O_DIRECT;
132 fd = open(name, O_RDWR | O_LARGEFILE);
133 if (fd != -1) DPRINTF("WARNING: Accessing image without"
134 "O_DIRECT! (%s)\n", name);
136 } else if (fd != -1) DPRINTF("open(%s) with O_DIRECT\n", name);
138 if (fd == -1) {
139 DPRINTF("Unable to open [%s]!\n",name);
140 ret = 0 - errno;
141 goto done;
142 }
144 prv->fd = fd;
146 init_fds(dd);
147 ret = get_image_info(s, fd);
148 done:
149 return ret;
150 }
152 int tdsync_queue_read(struct disk_driver *dd, uint64_t sector,
153 int nb_sectors, char *buf, td_callback_t cb,
154 int id, void *private)
155 {
156 struct td_state *s = dd->td_state;
157 struct tdsync_state *prv = (struct tdsync_state *)dd->private;
158 int size = nb_sectors * s->sector_size;
159 uint64_t offset = sector * (uint64_t)s->sector_size;
160 int ret;
162 ret = lseek(prv->fd, offset, SEEK_SET);
163 if (ret != (off_t)-1) {
164 ret = read(prv->fd, buf, size);
165 if (ret != size) {
166 ret = 0 - errno;
167 } else {
168 ret = 1;
169 }
170 } else ret = 0 - errno;
172 return cb(dd, (ret < 0) ? ret: 0, sector, nb_sectors, id, private);
173 }
175 int tdsync_queue_write(struct disk_driver *dd, uint64_t sector,
176 int nb_sectors, char *buf, td_callback_t cb,
177 int id, void *private)
178 {
179 struct td_state *s = dd->td_state;
180 struct tdsync_state *prv = (struct tdsync_state *)dd->private;
181 int size = nb_sectors * s->sector_size;
182 uint64_t offset = sector * (uint64_t)s->sector_size;
183 int ret = 0;
185 ret = lseek(prv->fd, offset, SEEK_SET);
186 if (ret != (off_t)-1) {
187 ret = write(prv->fd, buf, size);
188 if (ret != size) {
189 ret = 0 - errno;
190 } else {
191 ret = 1;
192 }
193 } else ret = 0 - errno;
195 return cb(dd, (ret < 0) ? ret : 0, sector, nb_sectors, id, private);
196 }
198 int tdsync_submit(struct disk_driver *dd)
199 {
200 return 0;
201 }
203 int tdsync_close(struct disk_driver *dd)
204 {
205 struct tdsync_state *prv = (struct tdsync_state *)dd->private;
207 close(prv->fd);
208 close(prv->poll_pipe[0]);
209 close(prv->poll_pipe[1]);
211 return 0;
212 }
214 int tdsync_do_callbacks(struct disk_driver *dd, int sid)
215 {
216 /* always ask for a kick */
217 return 1;
218 }
220 int tdsync_get_parent_id(struct disk_driver *dd, struct disk_id *id)
221 {
222 return TD_NO_PARENT;
223 }
225 int tdsync_validate_parent(struct disk_driver *dd,
226 struct disk_driver *parent, td_flag_t flags)
227 {
228 return -EINVAL;
229 }
231 struct tap_disk tapdisk_sync = {
232 .disk_type = "tapdisk_sync",
233 .private_data_size = sizeof(struct tdsync_state),
234 .td_open = tdsync_open,
235 .td_queue_read = tdsync_queue_read,
236 .td_queue_write = tdsync_queue_write,
237 .td_submit = tdsync_submit,
238 .td_close = tdsync_close,
239 .td_do_callbacks = tdsync_do_callbacks,
240 .td_get_parent_id = tdsync_get_parent_id,
241 .td_validate_parent = tdsync_validate_parent
242 };