source: mainline/uspace/srv/bd/rd/rd.c@ 1ee00b7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1ee00b7 was 1ee00b7, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Revamp block device interface: (1) block size is fixed, determined by BDD. (2) block address is 64-bit, (3) allow reading/writing more blocks at a time.

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 * Copyright (c) 2007 Michal Konopa
3 * Copyright (c) 2007 Martin Jelen
4 * Copyright (c) 2007 Peter Majer
5 * Copyright (c) 2007 Jakub Jermar
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * - The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/** @addtogroup rd
33 * @{
34 */
35
36/**
37 * @file rd.c
38 * @brief Initial RAM disk for HelenOS.
39 */
40
41#include <ipc/ipc.h>
42#include <ipc/services.h>
43#include <ipc/ns.h>
44#include <sysinfo.h>
45#include <as.h>
46#include <ddi.h>
47#include <align.h>
48#include <bool.h>
49#include <errno.h>
50#include <async.h>
51#include <align.h>
52#include <async.h>
53#include <fibril_sync.h>
54#include <stdio.h>
55#include <devmap.h>
56#include <ipc/bd.h>
57#include <macros.h>
58
59#define NAME "rd"
60
61/** Pointer to the ramdisk's image */
62static void *rd_addr;
63/** Size of the ramdisk */
64static size_t rd_size;
65
66/** Block size */
67static const size_t block_size = 512;
68
69static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf);
70static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf);
71
72/**
73 * This rwlock protects the ramdisk's data.
74 * If we were to serve multiple requests (read + write or several writes)
75 * concurrently (i.e. from two or more threads), each read and write needs to be
76 * protected by this rwlock.
77 */
78fibril_rwlock_t rd_lock;
79
80/** Handle one connection to ramdisk.
81 *
82 * @param iid Hash of the request that opened the connection.
83 * @param icall Call data of the request that opened the connection.
84 */
85static void rd_connection(ipc_callid_t iid, ipc_call_t *icall)
86{
87 ipc_callid_t callid;
88 ipc_call_t call;
89 int retval;
90 void *fs_va = NULL;
91 uint64_t ba;
92 size_t cnt;
93 size_t comm_size;
94
95 /*
96 * Answer the first IPC_M_CONNECT_ME_TO call.
97 */
98 ipc_answer_0(iid, EOK);
99
100 /*
101 * Now we wait for the client to send us its communication as_area.
102 */
103 int flags;
104 if (ipc_share_out_receive(&callid, &comm_size, &flags)) {
105 fs_va = as_get_mappable_page(comm_size);
106 if (fs_va) {
107 (void) ipc_share_out_finalize(callid, fs_va);
108 } else {
109 ipc_answer_0(callid, EHANGUP);
110 return;
111 }
112 } else {
113 /*
114 * The client doesn't speak the same protocol.
115 * At this point we can't handle protocol variations.
116 * Close the connection.
117 */
118 ipc_answer_0(callid, EHANGUP);
119 return;
120 }
121
122 while (true) {
123 callid = async_get_call(&call);
124 switch (IPC_GET_METHOD(call)) {
125 case IPC_M_PHONE_HUNGUP:
126 /*
127 * The other side has hung up.
128 * Answer the message and exit the fibril.
129 */
130 ipc_answer_0(callid, EOK);
131 return;
132 case BD_READ_BLOCKS:
133 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
134 IPC_GET_ARG2(call));
135 cnt = IPC_GET_ARG3(call);
136 if (cnt * block_size > comm_size) {
137 retval = ELIMIT;
138 break;
139 }
140 retval = rd_read_blocks(ba, cnt, fs_va);
141 break;
142 case BD_WRITE_BLOCKS:
143 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
144 IPC_GET_ARG2(call));
145 cnt = IPC_GET_ARG3(call);
146 if (cnt * block_size > comm_size) {
147 retval = ELIMIT;
148 break;
149 }
150 retval = rd_write_blocks(ba, cnt, fs_va);
151 break;
152 case BD_GET_BLOCK_SIZE:
153 ipc_answer_1(callid, EOK, block_size);
154 continue;
155 default:
156 /*
157 * The client doesn't speak the same protocol.
158 * Instead of closing the connection, we just ignore
159 * the call. This can be useful if the client uses a
160 * newer version of the protocol.
161 */
162 retval = EINVAL;
163 break;
164 }
165 ipc_answer_0(callid, retval);
166 }
167}
168
169/** Read blocks from the device. */
170static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf)
171{
172 if ((ba + cnt) * block_size > rd_size) {
173 /* Reading past the end of the device. */
174 return ELIMIT;
175 }
176
177 fibril_rwlock_read_lock(&rd_lock);
178 memcpy(buf, rd_addr + ba * block_size, block_size * cnt);
179 fibril_rwlock_read_unlock(&rd_lock);
180
181 return EOK;
182}
183
184/** Write blocks to the device. */
185static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
186{
187 if ((ba + cnt) * block_size > rd_size) {
188 /* Writing past the end of the device. */
189 return ELIMIT;
190 }
191
192 fibril_rwlock_write_lock(&rd_lock);
193 memcpy(rd_addr + ba * block_size, buf, block_size * cnt);
194 fibril_rwlock_write_unlock(&rd_lock);
195
196 return EOK;
197}
198
199/** Prepare the ramdisk image for operation. */
200static bool rd_init(void)
201{
202 rd_size = sysinfo_value("rd.size");
203 void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical");
204
205 if (rd_size == 0) {
206 printf(NAME ": No RAM disk found\n");
207 return false;
208 }
209
210 rd_addr = as_get_mappable_page(rd_size);
211
212 int flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
213 int retval = physmem_map(rd_ph_addr, rd_addr,
214 ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
215
216 if (retval < 0) {
217 printf(NAME ": Error mapping RAM disk\n");
218 return false;
219 }
220
221 printf(NAME ": Found RAM disk at %p, %d bytes\n", rd_ph_addr, rd_size);
222
223 int rc = devmap_driver_register(NAME, rd_connection);
224 if (rc < 0) {
225 printf(NAME ": Unable to register driver (%d)\n", rc);
226 return false;
227 }
228
229 dev_handle_t dev_handle;
230 if (devmap_device_register("initrd", &dev_handle) != EOK) {
231 devmap_hangup_phone(DEVMAP_DRIVER);
232 printf(NAME ": Unable to register device\n");
233 return false;
234 }
235
236 fibril_rwlock_initialize(&rd_lock);
237
238 return true;
239}
240
241int main(int argc, char **argv)
242{
243 printf(NAME ": HelenOS RAM disk server\n");
244
245 if (!rd_init())
246 return -1;
247
248 printf(NAME ": Accepting connections\n");
249 async_manager();
250
251 /* Never reached */
252 return 0;
253}
254
255/**
256 * @}
257 */
Note: See TracBrowser for help on using the repository browser.