4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
33 * \brief KBlock block size reducer
35 * This module allows access to a KBlock device using a smaller block
36 * size than the native one exported by the device.
37 * Note that the device being remapped needs either to be opened in buffered
38 * mode or to support partial writes.
40 * \author Stefano Fedrigo <aleph@develer.com>
42 * $WIZ$ module_depends = "kblock"
46 #include <string.h> /* memset */
49 static size_t reblock_readDirect(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
51 Reblock *r = REBLOCK_CAST(b);
53 offset += idx % (r->native_fd->blk_size / r->fd.blk_size) * r->fd.blk_size;
54 idx = idx / (r->native_fd->blk_size / r->fd.blk_size);
56 return kblock_read(r->native_fd, idx, buf, offset, size);
60 static size_t reblock_writeDirect(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
62 Reblock *r = REBLOCK_CAST(b);
64 offset += idx % (r->native_fd->blk_size / r->fd.blk_size) * r->fd.blk_size;
65 idx = idx / (r->native_fd->blk_size / r->fd.blk_size);
67 return kblock_write(r->native_fd, idx, buf, offset, size);
71 static int reblock_error(struct KBlock *b)
73 return kblock_error(REBLOCK_CAST(b)->native_fd);
76 static void reblock_clearerr(struct KBlock *b)
78 kblock_clearerr(REBLOCK_CAST(b)->native_fd);
81 static int reblock_close(struct KBlock *b)
83 return kblock_close(REBLOCK_CAST(b)->native_fd);
87 static const KBlockVTable reblock_vt =
89 .readDirect = reblock_readDirect,
90 .writeDirect = reblock_writeDirect,
92 .error = reblock_error,
93 .clearerr = reblock_clearerr,
94 .close = reblock_close,
99 * Initialize reblock device.
101 * \param rbl kblock reblock device
102 * \param native_fd kblock descriptor of the reblocked device
103 * \param new_blk_size new block size to export
105 * \note new block size is required to be a submultiple of the
106 * native device block size.
108 void reblock_init(Reblock *rbl, KBlock *native_fd, size_t new_blk_size)
110 ASSERT(new_blk_size);
111 ASSERT(new_blk_size < native_fd->blk_size);
112 ASSERT(native_fd->blk_size % new_blk_size == 0);
113 ASSERT(kblock_buffered(native_fd) || kblock_partialWrite(native_fd));
115 memset(rbl, 0, sizeof(Reblock));
117 DB(rbl->fd.priv.type = KBT_REBLOCK);
119 rbl->fd.blk_size = new_blk_size;
120 rbl->fd.blk_cnt = native_fd->blk_cnt * (native_fd->blk_size / new_blk_size);
122 rbl->fd.priv.flags |= KB_PARTIAL_WRITE;
123 rbl->fd.priv.vt = &reblock_vt;
125 rbl->native_fd = native_fd;