source: rtems/cpukit/libfs/src/jffs2/src/flashio.c @ 3c96bee

4.115
Last change on this file since 3c96bee was 3c96bee, checked in by Sebastian Huber <sebastian.huber@…>, on 09/12/13 at 13:32:07

JFFS2: Add RTEMS support

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2001-2003 Red Hat, Inc.
5 *
6 * Created by Dominic Ostrowski <dominic.ostrowski@3glab.com>
7 * Contributors: David Woodhouse, Nick Garnett, Richard Panton.
8 *
9 * For licensing information, see the file 'LICENCE' in this directory.
10 *
11 * $Id: flashio.c,v 1.1 2003/11/26 14:09:29 dwmw2 Exp $
12 *
13 */
14
15#include <linux/kernel.h>
16#include "nodelist.h"
17
18int jffs2_flash_read(struct jffs2_sb_info * c,
19                          cyg_uint32 read_buffer_offset, const size_t size,
20                          size_t * return_size, unsigned char *write_buffer)
21{
22        const struct super_block *sb = OFNI_BS_2SFFJ(c);
23        rtems_jffs2_flash_control *fc = sb->s_flash_control;
24
25        *return_size = size;
26
27        return (*fc->read)(fc, read_buffer_offset, write_buffer, size);
28}
29
30int jffs2_flash_write(struct jffs2_sb_info * c,
31                           cyg_uint32 write_buffer_offset, const size_t size,
32                           size_t * return_size, unsigned char *read_buffer)
33{
34        const struct super_block *sb = OFNI_BS_2SFFJ(c);
35        rtems_jffs2_flash_control *fc = sb->s_flash_control;
36
37        *return_size = size;
38
39        return (*fc->write)(fc, write_buffer_offset, read_buffer, size);
40}
41
42int
43jffs2_flash_direct_writev(struct jffs2_sb_info *c, const struct iovec *vecs,
44                   unsigned long count, loff_t to, size_t * retlen)
45{
46        unsigned long i;
47        size_t totlen = 0, thislen;
48        int ret = 0;
49
50        for (i = 0; i < count; i++) {
51                // writes need to be aligned but the data we're passed may not be
52                // Observation suggests most unaligned writes are small, so we
53                // optimize for that case.
54
55                if (((vecs[i].iov_len & (sizeof (int) - 1))) ||
56                    (((unsigned long) vecs[i].
57                      iov_base & (sizeof (unsigned long) - 1)))) {
58                        // are there iov's after this one? Or is it so much we'd need
59                        // to do multiple writes anyway?
60                        if ((i + 1) < count || vecs[i].iov_len > 256) {
61                                // cop out and malloc
62                                unsigned long j;
63                                ssize_t sizetomalloc = 0, totvecsize = 0;
64                                char *cbuf, *cbufptr;
65
66                                for (j = i; j < count; j++)
67                                        totvecsize += vecs[j].iov_len;
68
69                                // pad up in case unaligned
70                                sizetomalloc = totvecsize + sizeof (int) - 1;
71                                sizetomalloc &= ~(sizeof (int) - 1);
72                                cbuf = (char *) malloc(sizetomalloc);
73                                // malloc returns aligned memory
74                                if (!cbuf) {
75                                        ret = -ENOMEM;
76                                        goto writev_out;
77                                }
78                                cbufptr = cbuf;
79                                for (j = i; j < count; j++) {
80                                        memcpy(cbufptr, vecs[j].iov_base,
81                                               vecs[j].iov_len);
82                                        cbufptr += vecs[j].iov_len;
83                                }
84                                ret =
85                                    jffs2_flash_write(c, to, sizetomalloc,
86                                                      &thislen, cbuf);
87                                if (thislen > totvecsize)       // in case it was aligned up
88                                        thislen = totvecsize;
89                                totlen += thislen;
90                                free(cbuf);
91                                goto writev_out;
92                        } else {
93                                // otherwise optimize for the common case
94                                int buf[256 / sizeof (int)];    // int, so int aligned
95                                size_t lentowrite;
96
97                                lentowrite = vecs[i].iov_len;
98                                // pad up in case its unaligned
99                                lentowrite += sizeof (int) - 1;
100                                lentowrite &= ~(sizeof (int) - 1);
101                                memcpy(buf, vecs[i].iov_base, lentowrite);
102
103                                ret =
104                                    jffs2_flash_write(c, to, lentowrite,
105                                                      &thislen, (char *) &buf);
106                                if (thislen > vecs[i].iov_len)
107                                        thislen = vecs[i].iov_len;
108                        }       // else
109                } else
110                        ret =
111                            jffs2_flash_write(c, to, vecs[i].iov_len, &thislen,
112                                              vecs[i].iov_base);
113                totlen += thislen;
114                if (ret || thislen != vecs[i].iov_len)
115                        break;
116                to += vecs[i].iov_len;
117        }
118      writev_out:
119        if (retlen)
120                *retlen = totlen;
121
122        return ret;
123}
124
125int jffs2_flash_erase(struct jffs2_sb_info * c,
126                           struct jffs2_eraseblock * jeb)
127{
128        const struct super_block *sb = OFNI_BS_2SFFJ(c);
129        rtems_jffs2_flash_control *fc = sb->s_flash_control;
130
131        return (*fc->erase)(fc, jeb->offset);
132}
133
Note: See TracBrowser for help on using the repository browser.