source: mainline/uspace/srv/bd/rd/rd.c@ 86018c1

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

Obtain block device size automatically, if possible. Implement ftell(). Fix seek implementation in VFS if whence == SEEK_END.

  • Property mode set to 100644
File size: 6.6 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_synch.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 (async_share_out_receive(&callid, &comm_size, &flags)) {
105 fs_va = as_get_mappable_page(comm_size);
106 if (fs_va) {
107 (void) async_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 case BD_GET_NUM_BLOCKS:
156 ipc_answer_2(callid, EOK, LOWER32(rd_size / block_size),
157 UPPER32(rd_size / block_size));
158 continue;
159 default:
160 /*
161 * The client doesn't speak the same protocol.
162 * Instead of closing the connection, we just ignore
163 * the call. This can be useful if the client uses a
164 * newer version of the protocol.
165 */
166 retval = EINVAL;
167 break;
168 }
169 ipc_answer_0(callid, retval);
170 }
171}
172
173/** Read blocks from the device. */
174static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf)
175{
176 if ((ba + cnt) * block_size > rd_size) {
177 /* Reading past the end of the device. */
178 return ELIMIT;
179 }
180
181 fibril_rwlock_read_lock(&rd_lock);
182 memcpy(buf, rd_addr + ba * block_size, block_size * cnt);
183 fibril_rwlock_read_unlock(&rd_lock);
184
185 return EOK;
186}
187
188/** Write blocks to the device. */
189static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
190{
191 if ((ba + cnt) * block_size > rd_size) {
192 /* Writing past the end of the device. */
193 return ELIMIT;
194 }
195
196 fibril_rwlock_write_lock(&rd_lock);
197 memcpy(rd_addr + ba * block_size, buf, block_size * cnt);
198 fibril_rwlock_write_unlock(&rd_lock);
199
200 return EOK;
201}
202
203/** Prepare the ramdisk image for operation. */
204static bool rd_init(void)
205{
206 rd_size = sysinfo_value("rd.size");
207 void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical");
208
209 if (rd_size == 0) {
210 printf(NAME ": No RAM disk found\n");
211 return false;
212 }
213
214 rd_addr = as_get_mappable_page(rd_size);
215
216 int flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
217 int retval = physmem_map(rd_ph_addr, rd_addr,
218 ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
219
220 if (retval < 0) {
221 printf(NAME ": Error mapping RAM disk\n");
222 return false;
223 }
224
225 printf(NAME ": Found RAM disk at %p, %d bytes\n", rd_ph_addr, rd_size);
226
227 int rc = devmap_driver_register(NAME, rd_connection);
228 if (rc < 0) {
229 printf(NAME ": Unable to register driver (%d)\n", rc);
230 return false;
231 }
232
233 dev_handle_t dev_handle;
234 if (devmap_device_register("bd/initrd", &dev_handle) != EOK) {
235 devmap_hangup_phone(DEVMAP_DRIVER);
236 printf(NAME ": Unable to register device\n");
237 return false;
238 }
239
240 fibril_rwlock_initialize(&rd_lock);
241
242 return true;
243}
244
245int main(int argc, char **argv)
246{
247 printf(NAME ": HelenOS RAM disk server\n");
248
249 if (!rd_init())
250 return -1;
251
252 printf(NAME ": Accepting connections\n");
253 async_manager();
254
255 /* Never reached */
256 return 0;
257}
258
259/**
260 * @}
261 */
Note: See TracBrowser for help on using the repository browser.