Rev 4534 | Rev 4557 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4534 | Rev 4545 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Jiri Svoboda |
2 | * Copyright (c) 2009 Jiri Svoboda |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup bd |
29 | /** @addtogroup bd |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief File-backed block device driver |
35 | * @brief File-backed block device driver |
36 | * |
36 | * |
37 | * Allows accessing a file as a block device. Useful for, e.g., mounting |
37 | * Allows accessing a file as a block device. Useful for, e.g., mounting |
38 | * a disk image. |
38 | * a disk image. |
39 | */ |
39 | */ |
40 | 40 | ||
41 | #include <stdio.h> |
41 | #include <stdio.h> |
42 | #include <unistd.h> |
42 | #include <unistd.h> |
43 | #include <ipc/ipc.h> |
43 | #include <ipc/ipc.h> |
44 | #include <ipc/bd.h> |
44 | #include <ipc/bd.h> |
45 | #include <async.h> |
45 | #include <async.h> |
46 | #include <as.h> |
46 | #include <as.h> |
47 | #include <futex.h> |
47 | #include <futex.h> |
48 | #include <devmap.h> |
48 | #include <devmap.h> |
49 | #include <sys/types.h> |
49 | #include <sys/types.h> |
50 | #include <errno.h> |
50 | #include <errno.h> |
51 | #include <bool.h> |
51 | #include <bool.h> |
52 | 52 | ||
53 | #define NAME "file_bd" |
53 | #define NAME "file_bd" |
54 | 54 | ||
55 | static size_t comm_size; |
55 | static size_t comm_size; |
56 | static FILE *img; |
56 | static FILE *img; |
57 | 57 | ||
58 | static dev_handle_t dev_handle; |
58 | static dev_handle_t dev_handle; |
59 | static atomic_t dev_futex = FUTEX_INITIALIZER; |
59 | static atomic_t dev_futex = FUTEX_INITIALIZER; |
60 | 60 | ||
61 | static int file_bd_init(const char *fname); |
61 | static int file_bd_init(const char *fname); |
62 | static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
62 | static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
63 | static int file_bd_read(off_t blk_idx, off_t size, void *buf); |
63 | static int file_bd_read(off_t blk_idx, size_t size, void *buf); |
64 | static int file_bd_write(off_t blk_idx, off_t size, void *buf); |
64 | static int file_bd_write(off_t blk_idx, size_t size, void *buf); |
65 | 65 | ||
66 | int main(int argc, char **argv) |
66 | int main(int argc, char **argv) |
67 | { |
67 | { |
68 | int rc; |
68 | int rc; |
69 | 69 | ||
70 | printf(NAME ": File-backed block device driver\n"); |
70 | printf(NAME ": File-backed block device driver\n"); |
71 | 71 | ||
72 | if (argc != 3) { |
72 | if (argc != 3) { |
73 | printf("Expected two arguments (image name, device name).\n"); |
73 | printf("Expected two arguments (image name, device name).\n"); |
74 | return -1; |
74 | return -1; |
75 | } |
75 | } |
76 | 76 | ||
77 | if (file_bd_init(argv[1]) != EOK) |
77 | if (file_bd_init(argv[1]) != EOK) |
78 | return -1; |
78 | return -1; |
79 | 79 | ||
80 | rc = devmap_device_register(argv[2], &dev_handle); |
80 | rc = devmap_device_register(argv[2], &dev_handle); |
81 | if (rc != EOK) { |
81 | if (rc != EOK) { |
82 | devmap_hangup_phone(DEVMAP_DRIVER); |
82 | devmap_hangup_phone(DEVMAP_DRIVER); |
83 | printf(NAME ": Unable to register device %s.\n", |
83 | printf(NAME ": Unable to register device %s.\n", |
84 | argv[2]); |
84 | argv[2]); |
85 | return rc; |
85 | return rc; |
86 | } |
86 | } |
87 | 87 | ||
88 | printf(NAME ": Accepting connections\n"); |
88 | printf(NAME ": Accepting connections\n"); |
89 | async_manager(); |
89 | async_manager(); |
90 | 90 | ||
91 | /* Not reached */ |
91 | /* Not reached */ |
92 | return 0; |
92 | return 0; |
93 | } |
93 | } |
94 | 94 | ||
95 | static int file_bd_init(const char *fname) |
95 | static int file_bd_init(const char *fname) |
96 | { |
96 | { |
97 | int rc; |
97 | int rc; |
98 | 98 | ||
99 | rc = devmap_driver_register(NAME, file_bd_connection); |
99 | rc = devmap_driver_register(NAME, file_bd_connection); |
100 | if (rc < 0) { |
100 | if (rc < 0) { |
101 | printf(NAME ": Unable to register driver.\n"); |
101 | printf(NAME ": Unable to register driver.\n"); |
102 | return rc; |
102 | return rc; |
103 | } |
103 | } |
104 | 104 | ||
105 | img = fopen(fname, "rb+"); |
105 | img = fopen(fname, "rb+"); |
106 | if (img == NULL) |
106 | if (img == NULL) |
107 | return EINVAL; |
107 | return EINVAL; |
108 | 108 | ||
109 | return EOK; |
109 | return EOK; |
110 | } |
110 | } |
111 | 111 | ||
112 | static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
112 | static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
113 | { |
113 | { |
114 | void *fs_va = NULL; |
114 | void *fs_va = NULL; |
115 | ipc_callid_t callid; |
115 | ipc_callid_t callid; |
116 | ipc_call_t call; |
116 | ipc_call_t call; |
117 | ipcarg_t method; |
117 | ipcarg_t method; |
118 | int flags; |
118 | int flags; |
119 | int retval; |
119 | int retval; |
120 | off_t idx; |
120 | off_t idx; |
121 | off_t size; |
121 | size_t size; |
122 | 122 | ||
123 | /* Answer the IPC_M_CONNECT_ME_TO call. */ |
123 | /* Answer the IPC_M_CONNECT_ME_TO call. */ |
124 | ipc_answer_0(iid, EOK); |
124 | ipc_answer_0(iid, EOK); |
125 | 125 | ||
126 | if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
126 | if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
127 | ipc_answer_0(callid, EHANGUP); |
127 | ipc_answer_0(callid, EHANGUP); |
128 | return; |
128 | return; |
129 | } |
129 | } |
130 | 130 | ||
131 | fs_va = as_get_mappable_page(comm_size); |
131 | fs_va = as_get_mappable_page(comm_size); |
132 | if (fs_va == NULL) { |
132 | if (fs_va == NULL) { |
133 | ipc_answer_0(callid, EHANGUP); |
133 | ipc_answer_0(callid, EHANGUP); |
134 | return; |
134 | return; |
135 | } |
135 | } |
136 | 136 | ||
137 | (void) ipc_share_out_finalize(callid, fs_va); |
137 | (void) ipc_share_out_finalize(callid, fs_va); |
138 | 138 | ||
139 | while (1) { |
139 | while (1) { |
140 | callid = async_get_call(&call); |
140 | callid = async_get_call(&call); |
141 | method = IPC_GET_METHOD(call); |
141 | method = IPC_GET_METHOD(call); |
142 | switch (method) { |
142 | switch (method) { |
143 | case IPC_M_PHONE_HUNGUP: |
143 | case IPC_M_PHONE_HUNGUP: |
144 | /* The other side has hung up. */ |
144 | /* The other side has hung up. */ |
145 | ipc_answer_0(callid, EOK); |
145 | ipc_answer_0(callid, EOK); |
146 | return; |
146 | return; |
147 | case BD_READ_BLOCK: |
147 | case BD_READ_BLOCK: |
148 | case BD_WRITE_BLOCK: |
148 | case BD_WRITE_BLOCK: |
149 | idx = IPC_GET_ARG1(call); |
149 | idx = IPC_GET_ARG1(call); |
150 | size = IPC_GET_ARG2(call); |
150 | size = IPC_GET_ARG2(call); |
151 | if (size > comm_size) { |
151 | if (size > comm_size) { |
152 | retval = EINVAL; |
152 | retval = EINVAL; |
153 | break; |
153 | break; |
154 | } |
154 | } |
155 | if (method == BD_READ_BLOCK) |
155 | if (method == BD_READ_BLOCK) |
156 | retval = file_bd_read(idx, size, fs_va); |
156 | retval = file_bd_read(idx, size, fs_va); |
157 | else |
157 | else |
158 | retval = file_bd_write(idx, size, fs_va); |
158 | retval = file_bd_write(idx, size, fs_va); |
159 | break; |
159 | break; |
160 | default: |
160 | default: |
161 | retval = EINVAL; |
161 | retval = EINVAL; |
162 | break; |
162 | break; |
163 | } |
163 | } |
164 | ipc_answer_0(callid, retval); |
164 | ipc_answer_0(callid, retval); |
165 | } |
165 | } |
166 | } |
166 | } |
167 | 167 | ||
168 | static int file_bd_read(off_t blk_idx, off_t size, void *buf) |
168 | static int file_bd_read(off_t blk_idx, size_t size, void *buf) |
169 | { |
169 | { |
170 | size_t n_rd; |
170 | size_t n_rd; |
171 | 171 | ||
172 | printf("file_bd_read\n"); |
172 | printf("file_bd_read\n"); |
173 | futex_down(&dev_futex); |
173 | futex_down(&dev_futex); |
174 | 174 | ||
175 | printf("seek\n"); |
175 | printf("seek\n"); |
176 | fseek(img, blk_idx * size, SEEK_SET); |
176 | fseek(img, blk_idx * size, SEEK_SET); |
177 | printf("read\n"); |
177 | printf("read\n"); |
178 | n_rd = fread(buf, 1, size, img); |
178 | n_rd = fread(buf, 1, size, img); |
179 | printf("done\n"); |
179 | printf("done\n"); |
180 | 180 | ||
181 | printf("done\n"); |
181 | printf("done\n"); |
182 | 182 | ||
183 | if (ferror(img)) { |
183 | if (ferror(img)) { |
184 | futex_up(&dev_futex); |
184 | futex_up(&dev_futex); |
185 | return EIO; /* Read error */ |
185 | return EIO; /* Read error */ |
186 | } |
186 | } |
187 | 187 | ||
188 | futex_up(&dev_futex); |
188 | futex_up(&dev_futex); |
189 | 189 | ||
190 | if (n_rd < size) |
190 | if (n_rd < size) |
191 | return EINVAL; /* Read beyond end of disk */ |
191 | return EINVAL; /* Read beyond end of disk */ |
192 | 192 | ||
193 | return EOK; |
193 | return EOK; |
194 | } |
194 | } |
195 | 195 | ||
196 | static int file_bd_write(off_t blk_idx, off_t size, void *buf) |
196 | static int file_bd_write(off_t blk_idx, size_t size, void *buf) |
197 | { |
197 | { |
198 | size_t n_wr; |
198 | size_t n_wr; |
199 | 199 | ||
200 | futex_down(&dev_futex); |
200 | futex_down(&dev_futex); |
201 | 201 | ||
202 | fseek(img, blk_idx * size, SEEK_SET); |
202 | fseek(img, blk_idx * size, SEEK_SET); |
203 | n_wr = fread(buf, 1, size, img); |
203 | n_wr = fread(buf, 1, size, img); |
204 | 204 | ||
205 | if (ferror(img) || n_wr < size) { |
205 | if (ferror(img) || n_wr < size) { |
206 | futex_up(&dev_futex); |
206 | futex_up(&dev_futex); |
207 | return EIO; /* Write error */ |
207 | return EIO; /* Write error */ |
208 | } |
208 | } |
209 | 209 | ||
210 | futex_up(&dev_futex); |
210 | futex_up(&dev_futex); |
211 | 211 | ||
212 | return EOK; |
212 | return EOK; |
213 | } |
213 | } |
214 | 214 | ||
215 | /** |
215 | /** |
216 | * @} |
216 | * @} |
217 | */ |
217 | */ |
218 | 218 |