source: trunk/packages/xen-common/xen-common/tools/blktap/drivers/block-sync.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 17 years ago

Add xen and xen-common

File size: 6.8 KB
Line 
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 */
31
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 <linux/fs.h>
41#include "tapdisk.h"
42
43struct tdsync_state {
44        int fd;
45        int poll_pipe[2]; /* dummy fd for polling on */
46};
47       
48/*Get Image size, secsize*/
49static int get_image_info(struct td_state *s, int fd)
50{
51        int ret;
52        long size;
53        unsigned long total_size;
54        struct statvfs statBuf;
55        struct stat stat;
56
57        ret = fstat(fd, &stat);
58        if (ret != 0) {
59                DPRINTF("ERROR: fstat failed, Couldn't stat image");
60                return -EINVAL;
61        }
62
63        if (S_ISBLK(stat.st_mode)) {
64                /*Accessing block device directly*/
65                s->size = 0;
66                if (ioctl(fd,BLKGETSIZE,&s->size)!=0) {
67                        DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
68                        return -EINVAL;
69                }
70
71                DPRINTF("Image size: \n\tpre sector_shift  [%llu]\n\tpost "
72                        "sector_shift [%llu]\n",
73                        (long long unsigned)(s->size << SECTOR_SHIFT),
74                        (long long unsigned)s->size);
75
76                /*Get the sector size*/
77#if defined(BLKSSZGET)
78                {
79                        int arg;
80                        s->sector_size = DEFAULT_SECTOR_SIZE;
81                        ioctl(fd, BLKSSZGET, &s->sector_size);
82                       
83                        if (s->sector_size != DEFAULT_SECTOR_SIZE)
84                                DPRINTF("Note: sector size is %ld (not %d)\n",
85                                        s->sector_size, DEFAULT_SECTOR_SIZE);
86                }
87#else
88                s->sector_size = DEFAULT_SECTOR_SIZE;
89#endif
90
91        } else {
92                /*Local file? try fstat instead*/
93                s->size = (stat.st_size >> SECTOR_SHIFT);
94                s->sector_size = DEFAULT_SECTOR_SIZE;
95                DPRINTF("Image size: \n\tpre sector_shift  [%lluu]\n\tpost "
96                        "sector_shift [%lluu]\n",
97                        (long long unsigned)(s->size << SECTOR_SHIFT),
98                        (long long unsigned)s->size);
99        }
100
101        if (s->size == 0)
102                return -EINVAL;
103
104        s->info = 0;
105
106        return 0;
107}
108
109static inline void init_fds(struct disk_driver *dd)
110{
111        int i;
112        struct tdsync_state *prv = (struct tdsync_state *)dd->private;
113       
114        for(i = 0; i < MAX_IOFD; i++)
115                dd->io_fd[i] = 0;
116
117        dd->io_fd[0] = prv->poll_pipe[0];
118}
119
120/* Open the disk file and initialize aio state. */
121int tdsync_open (struct disk_driver *dd, const char *name, td_flag_t flags)
122{
123        int i, fd, ret = 0, o_flags;
124        struct td_state     *s   = dd->td_state;
125        struct tdsync_state *prv = (struct tdsync_state *)dd->private;
126       
127        /* set up a pipe so that we can hand back a poll fd that won't fire.*/
128        ret = pipe(prv->poll_pipe);
129        if (ret != 0)
130                return (0 - errno);
131       
132        /* Open the file */
133        o_flags = O_DIRECT | O_LARGEFILE | 
134                ((flags == TD_RDONLY) ? O_RDONLY : O_RDWR);
135        fd = open(name, o_flags);
136
137        if ( (fd == -1) && (errno == EINVAL) ) {
138
139                /* Maybe O_DIRECT isn't supported. */
140                o_flags &= ~O_DIRECT;
141                fd = open(name, O_RDWR | O_LARGEFILE);
142                if (fd != -1) DPRINTF("WARNING: Accessing image without"
143                                     "O_DIRECT! (%s)\n", name);
144
145        } else if (fd != -1) DPRINTF("open(%s) with O_DIRECT\n", name);
146       
147        if (fd == -1) {
148                DPRINTF("Unable to open [%s]!\n",name);
149                ret = 0 - errno;
150                goto done;
151        }
152
153        prv->fd = fd;
154
155        init_fds(dd);
156        ret = get_image_info(s, fd);
157done:
158        return ret;     
159}
160
161 int tdsync_queue_read(struct disk_driver *dd, uint64_t sector,
162                               int nb_sectors, char *buf, td_callback_t cb,
163                               int id, void *private)
164{
165        struct td_state     *s   = dd->td_state;
166        struct tdsync_state *prv = (struct tdsync_state *)dd->private;
167        int      size    = nb_sectors * s->sector_size;
168        uint64_t offset  = sector * (uint64_t)s->sector_size;
169        int ret;
170       
171        ret = lseek(prv->fd, offset, SEEK_SET);
172        if (ret != (off_t)-1) {
173                ret = read(prv->fd, buf, size);
174                if (ret != size) {
175                        ret = 0 - errno;
176                } else {
177                        ret = 1;
178                } 
179        } else ret = 0 - errno;
180               
181        return cb(dd, (ret < 0) ? ret: 0, sector, nb_sectors, id, private);
182}
183
184 int tdsync_queue_write(struct disk_driver *dd, uint64_t sector,
185                               int nb_sectors, char *buf, td_callback_t cb,
186                               int id, void *private)
187{
188        struct td_state     *s   = dd->td_state;
189        struct tdsync_state *prv = (struct tdsync_state *)dd->private;
190        int      size    = nb_sectors * s->sector_size;
191        uint64_t offset  = sector * (uint64_t)s->sector_size;
192        int ret = 0;
193       
194        ret = lseek(prv->fd, offset, SEEK_SET);
195        if (ret != (off_t)-1) {
196                ret = write(prv->fd, buf, size);
197                if (ret != size) {
198                        ret = 0 - errno;
199                } else {
200                        ret = 1;
201                }
202        } else ret = 0 - errno;
203               
204        return cb(dd, (ret < 0) ? ret : 0, sector, nb_sectors, id, private);
205}
206               
207int tdsync_submit(struct disk_driver *dd)
208{
209        return 0;       
210}
211
212int tdsync_close(struct disk_driver *dd)
213{
214        struct tdsync_state *prv = (struct tdsync_state *)dd->private;
215       
216        close(prv->fd);
217        close(prv->poll_pipe[0]);
218        close(prv->poll_pipe[1]);
219       
220        return 0;
221}
222
223int tdsync_do_callbacks(struct disk_driver *dd, int sid)
224{
225        /* always ask for a kick */
226        return 1;
227}
228
229int tdsync_get_parent_id(struct disk_driver *dd, struct disk_id *id)
230{
231        return TD_NO_PARENT;
232}
233
234int tdsync_validate_parent(struct disk_driver *dd, 
235                           struct disk_driver *parent, td_flag_t flags)
236{
237        return -EINVAL;
238}
239
240struct tap_disk tapdisk_sync = {
241        .disk_type           = "tapdisk_sync",
242        .private_data_size   = sizeof(struct tdsync_state),
243        .td_open             = tdsync_open,
244        .td_queue_read       = tdsync_queue_read,
245        .td_queue_write      = tdsync_queue_write,
246        .td_submit           = tdsync_submit,
247        .td_close            = tdsync_close,
248        .td_do_callbacks     = tdsync_do_callbacks,
249        .td_get_parent_id    = tdsync_get_parent_id,
250        .td_validate_parent  = tdsync_validate_parent
251};
Note: See TracBrowser for help on using the repository browser.