source: rtems/cpukit/libblock/src/ramdisk.c @ eb649786

4.104.115
Last change on this file since eb649786 was eb649786, checked in by Chris Johns <chrisj@…>, on 10/08/09 at 07:07:36

2009-10-08 Chris Johns <chrisj@…>

  • Makefile.am, preinstall.am: Added statvfs.h.
  • libcsupport/Makefile.am: Add statvfs.c.
  • libcsupport/include/sys/statvfs.h, libcsupport/src/statvfs.c: New.
  • libcsupport/include/rtems/libio.h: Add a file system handler for the statvfs call.
  • libfs/src/devfs/devfs_init.c, libfs/src/dosfs/msdos_init.c, libfs/src/imfs/imfs_init.c, libfs/src/nfsclient/src/nfs.c: Set the statvfs handler to NULL.
  • include/rtems/fs.h: Add a second node access field for the RFS file system to hold a directory offset while the existing field holds the inode number. This save a rescan of the directory when working with directories.
  • libblock/include/rtems/bdbuf.h: Added references and user fields to the buffer descriptor.
  • libblock/src/bdbuf.c: Added dynamic buffer support for different block sizes. Fixed a number of bugs.
  • libblock/src/blkdev.c: Release the disk device on an error.
  • libblock/src/diskdevs.c: Set the block size to the media block size during initialisation of the disk device.
  • libblock/src/flashdisk.c, libblock/src/nvdisk.c, libblock/src/ramdisk.c: Updated the drivers to handle variable block sizes.
  • libfs/src/dosfs/fat.c, libfs/src/dosfs/fat.h: Release any buffers when an error occurs. The FAT buffer layer hangs onto a single buffer while mounted. This should be fixed.
  • sapi/inline/rtems/chain.inl: Added rtems_chain_set_off_chain, rtems_chain_is_node_off_chain, and rtems_chain_previous.
  • score/inline/rtems/score/chain.inl: Added _Chain_Set_off_chain, and _Chain_Is_node_off_chain.
  • libmisc/shell/main_ln.c, libmisc/shell/main_mknod.c, libmisc/shell/mknod-pack_dev.c, libmisc/shell/mknod-pack_dev.h: New shell commands.
  • libmisc/Makefile.am, libmisc/shell/shellconfig.h: Added ln and mknod commands.
  • libmisc/shell/hexdump-display.c: Fixed the reopen bug which showed up as a free with a bad pointer.
  • libmisc/shell/main_mount.c: List the user adding file system when listing the available file systems to mount.
  • libmisc/shell/utils-cp.c: Remove the fixed static copy buffer and use a large dynamic buffer.
  • score/inline/rtems/score/address.inl, score/src/coremsgsubmit.c, score/src/objectallocate.c, score/src/objectfree.c: Remove warnings.
  • Property mode set to 100644
File size: 7.4 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_ramdisk
5 *
6 * RAM disk block device.
7 */
8
9/*
10 * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
11 * Author: Victor V. Vengerov <vvv@oktet.ru>
12 *
13 * @(#) $Id$
14 */
15
16#if HAVE_CONFIG_H
17#include "config.h"
18#endif
19
20#include <rtems.h>
21#include <rtems/libio.h>
22#include <errno.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <inttypes.h>
27
28#include <rtems/blkdev.h>
29#include <rtems/diskdevs.h>
30#include <rtems/ramdisk.h>
31
32/**
33 * Control tracing. It can be compiled out of the code for small
34 * footprint targets. Leave in by default.
35 */
36#if !defined (RTEMS_RAMDISK_TRACE)
37#define RTEMS_RAMDISK_TRACE 0
38#endif
39
40#define RAMDISK_DEVICE_BASE_NAME "/dev/rd"
41
42/* Internal RAM disk descriptor */
43struct ramdisk {
44  uint32_t block_size; /* RAM disk block size, the media size */
45  rtems_blkdev_bnum block_num; /* Number of blocks on this RAM disk */
46  void *area; /* RAM disk memory area */
47  bool initialized; /* RAM disk is initialized */
48  bool malloced; /* != 0, if memory allocated by malloc for this RAM disk */
49#if RTEMS_RAMDISK_TRACE
50  int info_level; /* Trace level */
51#endif
52};
53
54static struct ramdisk *ramdisk;
55static uint32_t nramdisks;
56
57#if RTEMS_RAMDISK_TRACE
58/**
59 * Print a message to the ramdisk output and flush it.
60 *
61 * @param rd The ramdisk control structure.
62 * @param format The format string. See printf for details.
63 * @param ... The arguments for the format text.
64 * @return int The number of bytes written to the output.
65 */
66static int
67rtems_ramdisk_printf (struct ramdisk *rd, const char *format, ...)
68{
69  int ret = 0;
70  if (rd->info_level >= 1)
71  {
72    va_list args;
73    va_start (args, format);
74    fprintf (stdout, "ramdisk:");
75    ret =  vfprintf (stdout, format, args);
76    fprintf (stdout, "\n");
77    fflush (stdout);
78  }
79  return ret;
80}
81#endif
82
83/* ramdisk_read --
84 *     RAM disk READ request handler. This primitive copies data from RAM
85 *     disk to supplied buffer and invoke the callout function to inform
86 *     upper layer that reading is completed.
87 *
88 * PARAMETERS:
89 *     req - pointer to the READ block device request info
90 *
91 * RETURNS:
92 *     ioctl return value
93 */
94static int
95ramdisk_read(struct ramdisk *rd, rtems_blkdev_request *req)
96{
97    uint8_t *from = rd->area;
98    uint32_t   i;
99    rtems_blkdev_sg_buffer *sg;
100
101#if RTEMS_RAMDISK_TRACE
102    rtems_ramdisk_printf (rd, "ramdisk read: start=%d, blocks=%d",
103                          req->bufs[0].block, req->bufnum);
104#endif
105
106    for (i = 0, sg = req->bufs; i < req->bufnum; i++, sg++)
107    {
108#if RTEMS_RAMDISK_TRACE
109        rtems_ramdisk_printf (rd, "ramdisk read: buf=%d block=%d length=%d off=%d addr=%p",
110                              i, sg->block, sg->length, sg->block * rd->block_size,
111                              from + (sg->block * rd->block_size));
112#endif
113        memcpy(sg->buffer, from + (sg->block * rd->block_size), sg->length);
114    }
115    req->req_done(req->done_arg, RTEMS_SUCCESSFUL, 0);
116    return 0;
117}
118
119/* ramdisk_write --
120 *     RAM disk WRITE request handler. This primitive copies data from
121 *     supplied buffer to RAM disk and invoke the callout function to inform
122 *     upper layer that writing is completed.
123 *
124 * PARAMETERS:
125 *     req - pointer to the WRITE block device request info
126 *
127 * RETURNS:
128 *     ioctl return value
129 */
130static int
131ramdisk_write(struct ramdisk *rd, rtems_blkdev_request *req)
132{
133    uint8_t *to = rd->area;
134    uint32_t   i;
135    rtems_blkdev_sg_buffer *sg;
136
137#if RTEMS_RAMDISK_TRACE
138    rtems_ramdisk_printf (rd, "ramdisk write: start=%d, blocks=%d",
139                          req->bufs[0].block, req->bufnum);
140#endif
141    for (i = 0, sg = req->bufs; i < req->bufnum; i++, sg++)
142    {
143#if RTEMS_RAMDISK_TRACE
144        rtems_ramdisk_printf (rd, "ramdisk write: buf=%d block=%d length=%d off=%d addr=%p",
145                              i, sg->block, sg->length, sg->block * rd->block_size,
146                              to + (sg->block * rd->block_size));
147#endif
148        memcpy(to + (sg->block * rd->block_size), sg->buffer, sg->length);
149    }
150    req->req_done(req->done_arg, RTEMS_SUCCESSFUL, 0);
151    return 0;
152}
153
154/* ramdisk_ioctl --
155 *     IOCTL handler for RAM disk device.
156 *
157 * PARAMETERS:
158 *      dev  - device number (major, minor number)
159 *      req  - IOCTL request code
160 *      argp - IOCTL argument
161 *
162 * RETURNS:
163 *     IOCTL return value
164 */
165static int
166ramdisk_ioctl(dev_t dev, uint32_t req, void *argp)
167{
168    switch (req)
169    {
170        case RTEMS_BLKIO_REQUEST:
171        {
172            rtems_device_minor_number minor;
173            rtems_blkdev_request *r = argp;
174            struct ramdisk *rd;
175
176            minor = rtems_filesystem_dev_minor_t(dev);
177            if ((minor >= nramdisks) || !ramdisk[minor].initialized)
178            {
179                errno = ENODEV;
180                return -1;
181            }
182
183            rd = ramdisk + minor;
184
185            switch (r->req)
186            {
187                case RTEMS_BLKDEV_REQ_READ:
188                    return ramdisk_read(rd, r);
189
190                case RTEMS_BLKDEV_REQ_WRITE:
191                    return ramdisk_write(rd, r);
192
193                default:
194                    errno = EINVAL;
195                    return -1;
196            }
197            break;
198        }
199 
200        default:
201            return rtems_blkdev_ioctl (dev, req, argp);
202            break;
203    }
204
205    errno = EINVAL;
206    return -1;
207}
208
209/* ramdisk_initialize --
210 *     RAM disk device driver initialization. Run through RAM disk
211 *     configuration information and configure appropriate RAM disks.
212 *
213 * PARAMETERS:
214 *     major - RAM disk major device number
215 *     minor - minor device number, not applicable
216 *     arg   - initialization argument, not applicable
217 *
218 * RETURNS:
219 *     none
220 */
221rtems_device_driver
222ramdisk_initialize(
223    rtems_device_major_number major,
224    rtems_device_minor_number minor __attribute__((unused)),
225    void *arg __attribute__((unused)))
226{
227    rtems_device_minor_number i;
228    rtems_ramdisk_config *c = rtems_ramdisk_configuration;
229    struct ramdisk *r;
230    rtems_status_code rc;
231
232    rc = rtems_disk_io_initialize();
233    if (rc != RTEMS_SUCCESSFUL)
234        return rc;
235
236    r = ramdisk = calloc(rtems_ramdisk_configuration_size,
237                         sizeof(struct ramdisk));
238#if RTEMS_RAMDISK_TRACE
239    r->info_level = 1;
240#endif   
241    for (i = 0; i < rtems_ramdisk_configuration_size; i++, c++, r++)
242    {
243        dev_t dev = rtems_filesystem_make_dev_t(major, i);
244        char name [] = RAMDISK_DEVICE_BASE_NAME "a";
245        name [sizeof(RAMDISK_DEVICE_BASE_NAME)] += i;
246        r->block_size = c->block_size;
247        r->block_num = c->block_num;
248        if (c->location == NULL)
249        {
250            r->malloced = true;
251            r->area = malloc(r->block_size * r->block_num);
252            if (r->area == NULL) /* No enough memory for this disk */
253            {
254                r->initialized = false;
255                continue;
256            }
257            else
258            {
259                r->initialized = true;
260            }
261        }
262        else
263        {
264            r->malloced = false;
265            r->initialized = true;
266            r->area = c->location;
267        }
268        rc = rtems_disk_create_phys(dev, c->block_size, c->block_num,
269                                    ramdisk_ioctl, name);
270        if (rc != RTEMS_SUCCESSFUL)
271        {
272            if (r->malloced)
273            {
274                free(r->area);
275            }
276            r->initialized = false;
277        }
278    }
279    nramdisks = rtems_ramdisk_configuration_size;
280    return RTEMS_SUCCESSFUL;
281}
Note: See TracBrowser for help on using the repository browser.