1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Block Device IMFS |
---|
5 | * @ingroup libblock |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * Copyright (c) 2012 embedded brains GmbH. All rights reserved. |
---|
10 | * |
---|
11 | * embedded brains GmbH |
---|
12 | * Obere Lagerstr. 30 |
---|
13 | * 82178 Puchheim |
---|
14 | * Germany |
---|
15 | * <rtems@embedded-brains.de> |
---|
16 | * |
---|
17 | * The license and distribution terms for this file may be |
---|
18 | * found in the file LICENSE in this distribution or at |
---|
19 | * http://www.rtems.org/license/LICENSE. |
---|
20 | */ |
---|
21 | |
---|
22 | #if HAVE_CONFIG_H |
---|
23 | #include "config.h" |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <sys/stat.h> |
---|
27 | #include <stdlib.h> |
---|
28 | #include <unistd.h> |
---|
29 | #include <fcntl.h> |
---|
30 | #include <string.h> |
---|
31 | |
---|
32 | #include <rtems/blkdev.h> |
---|
33 | #include <rtems/bdbuf.h> |
---|
34 | #include <rtems/imfs.h> |
---|
35 | |
---|
36 | typedef struct { |
---|
37 | rtems_disk_device dd; |
---|
38 | int fd; |
---|
39 | } rtems_blkdev_imfs_context; |
---|
40 | |
---|
41 | static ssize_t rtems_blkdev_imfs_read( |
---|
42 | rtems_libio_t *iop, |
---|
43 | void *buffer, |
---|
44 | size_t count |
---|
45 | ) |
---|
46 | { |
---|
47 | int rv; |
---|
48 | rtems_blkdev_imfs_context *ctx = IMFS_generic_get_context_by_iop(iop); |
---|
49 | rtems_disk_device *dd = &ctx->dd; |
---|
50 | ssize_t remaining = (ssize_t) count; |
---|
51 | off_t offset = iop->offset; |
---|
52 | ssize_t block_size = (ssize_t) rtems_disk_get_block_size(dd); |
---|
53 | rtems_blkdev_bnum block = (rtems_blkdev_bnum) (offset / block_size); |
---|
54 | ssize_t block_offset = (ssize_t) (offset % block_size); |
---|
55 | char *dst = buffer; |
---|
56 | |
---|
57 | while (remaining > 0) { |
---|
58 | rtems_bdbuf_buffer *bd; |
---|
59 | rtems_status_code sc = rtems_bdbuf_read(dd, block, &bd); |
---|
60 | |
---|
61 | if (sc == RTEMS_SUCCESSFUL) { |
---|
62 | ssize_t copy = block_size - block_offset; |
---|
63 | |
---|
64 | if (copy > remaining) { |
---|
65 | copy = remaining; |
---|
66 | } |
---|
67 | |
---|
68 | memcpy(dst, (char *) bd->buffer + block_offset, (size_t) copy); |
---|
69 | |
---|
70 | sc = rtems_bdbuf_release(bd); |
---|
71 | if (sc == RTEMS_SUCCESSFUL) { |
---|
72 | block_offset = 0; |
---|
73 | remaining -= copy; |
---|
74 | dst += copy; |
---|
75 | ++block; |
---|
76 | } else { |
---|
77 | remaining = -1; |
---|
78 | } |
---|
79 | } else { |
---|
80 | remaining = -1; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | if (remaining >= 0) { |
---|
85 | iop->offset += count; |
---|
86 | rv = (ssize_t) count; |
---|
87 | } else { |
---|
88 | errno = EIO; |
---|
89 | rv = -1; |
---|
90 | } |
---|
91 | |
---|
92 | return rv; |
---|
93 | } |
---|
94 | |
---|
95 | static ssize_t rtems_blkdev_imfs_write( |
---|
96 | rtems_libio_t *iop, |
---|
97 | const void *buffer, |
---|
98 | size_t count |
---|
99 | ) |
---|
100 | { |
---|
101 | int rv; |
---|
102 | rtems_blkdev_imfs_context *ctx = IMFS_generic_get_context_by_iop(iop); |
---|
103 | rtems_disk_device *dd = &ctx->dd; |
---|
104 | ssize_t remaining = (ssize_t) count; |
---|
105 | off_t offset = iop->offset; |
---|
106 | ssize_t block_size = (ssize_t) rtems_disk_get_block_size(dd); |
---|
107 | rtems_blkdev_bnum block = (rtems_blkdev_bnum) (offset / block_size); |
---|
108 | ssize_t block_offset = (ssize_t) (offset % block_size); |
---|
109 | const char *src = buffer; |
---|
110 | |
---|
111 | while (remaining > 0) { |
---|
112 | rtems_status_code sc; |
---|
113 | rtems_bdbuf_buffer *bd; |
---|
114 | |
---|
115 | if (block_offset == 0 && remaining >= block_size) { |
---|
116 | sc = rtems_bdbuf_get(dd, block, &bd); |
---|
117 | } else { |
---|
118 | sc = rtems_bdbuf_read(dd, block, &bd); |
---|
119 | } |
---|
120 | |
---|
121 | if (sc == RTEMS_SUCCESSFUL) { |
---|
122 | ssize_t copy = block_size - block_offset; |
---|
123 | |
---|
124 | if (copy > remaining) { |
---|
125 | copy = remaining; |
---|
126 | } |
---|
127 | |
---|
128 | memcpy((char *) bd->buffer + block_offset, src, (size_t) copy); |
---|
129 | |
---|
130 | sc = rtems_bdbuf_release_modified(bd); |
---|
131 | if (sc == RTEMS_SUCCESSFUL) { |
---|
132 | block_offset = 0; |
---|
133 | remaining -= copy; |
---|
134 | src += copy; |
---|
135 | ++block; |
---|
136 | } else { |
---|
137 | remaining = -1; |
---|
138 | } |
---|
139 | } else { |
---|
140 | remaining = -1; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | if (remaining >= 0) { |
---|
145 | iop->offset += count; |
---|
146 | rv = (ssize_t) count; |
---|
147 | } else { |
---|
148 | errno = EIO; |
---|
149 | rv = -1; |
---|
150 | } |
---|
151 | |
---|
152 | return rv; |
---|
153 | } |
---|
154 | |
---|
155 | static int rtems_blkdev_imfs_ioctl( |
---|
156 | rtems_libio_t *iop, |
---|
157 | uint32_t request, |
---|
158 | void *buffer |
---|
159 | ) |
---|
160 | { |
---|
161 | int rv = 0; |
---|
162 | |
---|
163 | if (request != RTEMS_BLKIO_REQUEST) { |
---|
164 | rtems_blkdev_imfs_context *ctx = IMFS_generic_get_context_by_iop(iop); |
---|
165 | rtems_disk_device *dd = &ctx->dd; |
---|
166 | |
---|
167 | rv = (*dd->ioctl)(dd, request, buffer); |
---|
168 | } else { |
---|
169 | /* |
---|
170 | * It is not allowed to directly access the driver circumventing the cache. |
---|
171 | */ |
---|
172 | errno = EINVAL; |
---|
173 | rv = -1; |
---|
174 | } |
---|
175 | |
---|
176 | return rv; |
---|
177 | } |
---|
178 | |
---|
179 | static int rtems_blkdev_imfs_fstat( |
---|
180 | const rtems_filesystem_location_info_t *loc, |
---|
181 | struct stat *buf |
---|
182 | ) |
---|
183 | { |
---|
184 | rtems_blkdev_imfs_context *ctx = |
---|
185 | IMFS_generic_get_context_by_location(loc); |
---|
186 | rtems_disk_device *dd = &ctx->dd; |
---|
187 | |
---|
188 | buf->st_rdev = rtems_disk_get_device_identifier(dd); |
---|
189 | buf->st_blksize = rtems_disk_get_block_size(dd); |
---|
190 | buf->st_blocks = rtems_disk_get_block_count(dd); |
---|
191 | |
---|
192 | return IMFS_stat(loc, buf); |
---|
193 | } |
---|
194 | |
---|
195 | static int rtems_blkdev_imfs_fsync_or_fdatasync( |
---|
196 | rtems_libio_t *iop |
---|
197 | ) |
---|
198 | { |
---|
199 | int rv = 0; |
---|
200 | rtems_blkdev_imfs_context *ctx = IMFS_generic_get_context_by_iop(iop); |
---|
201 | rtems_disk_device *dd = &ctx->dd; |
---|
202 | rtems_status_code sc = rtems_bdbuf_syncdev(dd); |
---|
203 | |
---|
204 | if (sc != RTEMS_SUCCESSFUL) { |
---|
205 | errno = EIO; |
---|
206 | rv = -1; |
---|
207 | } |
---|
208 | |
---|
209 | return rv; |
---|
210 | } |
---|
211 | |
---|
212 | static const rtems_filesystem_file_handlers_r rtems_blkdev_imfs_node = { |
---|
213 | .open_h = rtems_filesystem_default_open, |
---|
214 | .close_h = rtems_filesystem_default_close, |
---|
215 | .read_h = rtems_blkdev_imfs_read, |
---|
216 | .write_h = rtems_blkdev_imfs_write, |
---|
217 | .ioctl_h = rtems_blkdev_imfs_ioctl, |
---|
218 | .lseek_h = rtems_filesystem_default_lseek_file, |
---|
219 | .fstat_h = rtems_blkdev_imfs_fstat, |
---|
220 | .ftruncate_h = rtems_filesystem_default_ftruncate, |
---|
221 | .fsync_h = rtems_blkdev_imfs_fsync_or_fdatasync, |
---|
222 | .fdatasync_h = rtems_blkdev_imfs_fsync_or_fdatasync, |
---|
223 | .fcntl_h = rtems_filesystem_default_fcntl, |
---|
224 | .kqfilter_h = rtems_filesystem_default_kqfilter, |
---|
225 | .poll_h = rtems_filesystem_default_poll, |
---|
226 | .readv_h = rtems_filesystem_default_readv, |
---|
227 | .writev_h = rtems_filesystem_default_writev |
---|
228 | }; |
---|
229 | |
---|
230 | static IMFS_jnode_t *rtems_blkdev_imfs_initialize( |
---|
231 | IMFS_jnode_t *node, |
---|
232 | const IMFS_types_union *info |
---|
233 | ) |
---|
234 | { |
---|
235 | rtems_blkdev_imfs_context *ctx; |
---|
236 | rtems_disk_device *dd; |
---|
237 | |
---|
238 | node = IMFS_node_initialize_generic(node, info); |
---|
239 | |
---|
240 | ctx = IMFS_generic_get_context_by_node(node); |
---|
241 | dd = &ctx->dd; |
---|
242 | dd->dev = IMFS_generic_get_device_identifier_by_node(node); |
---|
243 | |
---|
244 | return node; |
---|
245 | } |
---|
246 | |
---|
247 | static IMFS_jnode_t *rtems_blkdev_imfs_destroy(IMFS_jnode_t *node) |
---|
248 | { |
---|
249 | rtems_blkdev_imfs_context *ctx = IMFS_generic_get_context_by_node(node); |
---|
250 | rtems_disk_device *dd = &ctx->dd; |
---|
251 | |
---|
252 | rtems_bdbuf_syncdev(dd); |
---|
253 | rtems_bdbuf_purge_dev(dd); |
---|
254 | |
---|
255 | if (ctx->fd >= 0) { |
---|
256 | close(ctx->fd); |
---|
257 | } else { |
---|
258 | (*dd->ioctl)(dd, RTEMS_BLKIO_DELETED, NULL); |
---|
259 | } |
---|
260 | |
---|
261 | free(ctx); |
---|
262 | |
---|
263 | return node; |
---|
264 | } |
---|
265 | |
---|
266 | static const IMFS_node_control rtems_blkdev_imfs_control = { |
---|
267 | .imfs_type = IMFS_GENERIC, |
---|
268 | .handlers = &rtems_blkdev_imfs_node, |
---|
269 | .node_initialize = rtems_blkdev_imfs_initialize, |
---|
270 | .node_remove = IMFS_node_remove_default, |
---|
271 | .node_destroy = rtems_blkdev_imfs_destroy |
---|
272 | }; |
---|
273 | |
---|
274 | rtems_status_code rtems_blkdev_create( |
---|
275 | const char *device, |
---|
276 | uint32_t media_block_size, |
---|
277 | rtems_blkdev_bnum media_block_count, |
---|
278 | rtems_block_device_ioctl handler, |
---|
279 | void *driver_data |
---|
280 | ) |
---|
281 | { |
---|
282 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
283 | rtems_blkdev_imfs_context *ctx = malloc(sizeof(*ctx)); |
---|
284 | |
---|
285 | if (ctx != NULL) { |
---|
286 | sc = rtems_disk_init_phys( |
---|
287 | &ctx->dd, |
---|
288 | media_block_size, |
---|
289 | media_block_count, |
---|
290 | handler, |
---|
291 | driver_data |
---|
292 | ); |
---|
293 | |
---|
294 | ctx->fd = -1; |
---|
295 | |
---|
296 | if (sc == RTEMS_SUCCESSFUL) { |
---|
297 | int rv = IMFS_make_generic_node( |
---|
298 | device, |
---|
299 | S_IFBLK | S_IRWXU | S_IRWXG | S_IRWXO, |
---|
300 | &rtems_blkdev_imfs_control, |
---|
301 | ctx |
---|
302 | ); |
---|
303 | |
---|
304 | if (rv != 0) { |
---|
305 | free(ctx); |
---|
306 | sc = RTEMS_UNSATISFIED; |
---|
307 | } |
---|
308 | } else { |
---|
309 | free(ctx); |
---|
310 | } |
---|
311 | } else { |
---|
312 | sc = RTEMS_NO_MEMORY; |
---|
313 | } |
---|
314 | |
---|
315 | return sc; |
---|
316 | } |
---|
317 | |
---|
318 | rtems_status_code rtems_blkdev_create_partition( |
---|
319 | const char *partition, |
---|
320 | const char *parent_block_device, |
---|
321 | rtems_blkdev_bnum media_block_begin, |
---|
322 | rtems_blkdev_bnum media_block_count |
---|
323 | ) |
---|
324 | { |
---|
325 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
326 | int fd = open(parent_block_device, O_RDWR); |
---|
327 | |
---|
328 | if (fd >= 0) { |
---|
329 | int rv; |
---|
330 | struct stat st; |
---|
331 | |
---|
332 | rv = fstat(fd, &st); |
---|
333 | if (rv == 0 && S_ISBLK(st.st_mode)) { |
---|
334 | rtems_disk_device *phys_dd; |
---|
335 | |
---|
336 | rv = rtems_disk_fd_get_disk_device(fd, &phys_dd); |
---|
337 | if (rv == 0) { |
---|
338 | rtems_blkdev_imfs_context *ctx = malloc(sizeof(*ctx)); |
---|
339 | |
---|
340 | if (ctx != NULL) { |
---|
341 | sc = rtems_disk_init_log( |
---|
342 | &ctx->dd, |
---|
343 | phys_dd, |
---|
344 | media_block_begin, |
---|
345 | media_block_count |
---|
346 | ); |
---|
347 | |
---|
348 | if (sc == RTEMS_SUCCESSFUL) { |
---|
349 | ctx->fd = fd; |
---|
350 | |
---|
351 | rv = IMFS_make_generic_node( |
---|
352 | partition, |
---|
353 | S_IFBLK | S_IRWXU | S_IRWXG | S_IRWXO, |
---|
354 | &rtems_blkdev_imfs_control, |
---|
355 | ctx |
---|
356 | ); |
---|
357 | |
---|
358 | if (rv != 0) { |
---|
359 | free(ctx); |
---|
360 | sc = RTEMS_UNSATISFIED; |
---|
361 | } |
---|
362 | } else { |
---|
363 | free(ctx); |
---|
364 | } |
---|
365 | } else { |
---|
366 | sc = RTEMS_NO_MEMORY; |
---|
367 | } |
---|
368 | } else { |
---|
369 | sc = RTEMS_NOT_IMPLEMENTED; |
---|
370 | } |
---|
371 | } else { |
---|
372 | sc = RTEMS_INVALID_NODE; |
---|
373 | } |
---|
374 | |
---|
375 | if (sc != RTEMS_SUCCESSFUL) { |
---|
376 | close(fd); |
---|
377 | } |
---|
378 | } else { |
---|
379 | sc = RTEMS_INVALID_ID; |
---|
380 | } |
---|
381 | |
---|
382 | return sc; |
---|
383 | } |
---|