/branches/network/uspace/srv/fhc/fhc.c |
---|
File deleted |
/branches/network/uspace/srv/fhc/Makefile |
---|
File deleted |
/branches/network/uspace/srv/obio/obio.c |
---|
File deleted |
Property changes: |
Deleted: svn:mergeinfo |
/branches/network/uspace/srv/obio/Makefile |
---|
File deleted |
/branches/network/uspace/srv/obio |
---|
Property changes: |
Deleted: svn:mergeinfo |
/branches/network/uspace/srv/rd/Makefile |
---|
File deleted |
/branches/network/uspace/srv/rd/rd.c |
---|
File deleted |
/branches/network/uspace/srv/rd/rd.h |
---|
File deleted |
/branches/network/uspace/srv/kbd/ctl/pc.c |
---|
36,8 → 36,8 |
*/ |
#include <kbd.h> |
#include <kbd/kbd.h> |
#include <kbd/keycode.h> |
#include <io/console.h> |
#include <io/keycode.h> |
#include <kbd_ctl.h> |
#include <gsp.h> |
188,7 → 188,7 |
void kbd_ctl_parse_scancode(int scancode) |
{ |
kbd_ev_type_t type; |
console_ev_type_t type; |
unsigned int key; |
int *map; |
size_t map_length; |
213,9 → 213,9 |
if (scancode & 0x80) { |
scancode &= ~0x80; |
type = KE_RELEASE; |
type = KEY_RELEASE; |
} else { |
type = KE_PRESS; |
type = KEY_PRESS; |
} |
if (scancode < 0 || scancode >= map_length) |
/branches/network/uspace/srv/kbd/ctl/stty.c |
---|
36,8 → 36,7 |
*/ |
#include <kbd.h> |
#include <kbd/kbd.h> |
#include <kbd/keycode.h> |
#include <io/keycode.h> |
#include <kbd_ctl.h> |
#include <gsp.h> |
#include <stroke.h> |
/branches/network/uspace/srv/kbd/ctl/sun.c |
---|
36,8 → 36,8 |
*/ |
#include <kbd.h> |
#include <kbd/kbd.h> |
#include <kbd/keycode.h> |
#include <io/console.h> |
#include <io/keycode.h> |
#include <kbd_ctl.h> |
#define KBD_KEY_RELEASE 0x80 |
52,7 → 52,7 |
void kbd_ctl_parse_scancode(int scancode) |
{ |
kbd_ev_type_t type; |
console_ev_type_t type; |
unsigned int key; |
if (scancode < 0 || scancode >= 0x100) |
63,9 → 63,9 |
if (scancode & KBD_KEY_RELEASE) { |
scancode &= ~KBD_KEY_RELEASE; |
type = KE_RELEASE; |
type = KEY_RELEASE; |
} else { |
type = KE_PRESS; |
type = KEY_PRESS; |
} |
key = scanmap_simple[scancode]; |
/branches/network/uspace/srv/kbd/ctl/gxe_fb.c |
---|
36,8 → 36,8 |
*/ |
#include <kbd.h> |
#include <kbd/kbd.h> |
#include <kbd/keycode.h> |
#include <io/console.h> |
#include <io/keycode.h> |
#include <kbd_ctl.h> |
#include <gsp.h> |
#include <stroke.h> |
/branches/network/uspace/srv/kbd/include/key_buffer.h |
---|
File deleted |
/branches/network/uspace/srv/kbd/include/gsp.h |
---|
37,7 → 37,7 |
#ifndef KBD_GSP_H_ |
#define KBD_GSP_H_ |
#include <libadt/hash_table.h> |
#include <adt/hash_table.h> |
enum { |
GSP_END = -1, /**< Terminates a sequence. */ |
/branches/network/uspace/srv/kbd/include/layout.h |
---|
37,12 → 37,12 |
#ifndef KBD_LAYOUT_H_ |
#define KBD_LAYOUT_H_ |
#include <kbd/kbd.h> |
#include <sys/types.h> |
#include <io/console.h> |
typedef struct { |
void (*reset)(void); |
wchar_t (*parse_ev)(kbd_event_t *); |
wchar_t (*parse_ev)(console_event_t *); |
} layout_op_t; |
extern layout_op_t us_qwerty_op; |
54,4 → 54,3 |
/** |
* @} |
*/ |
/branches/network/uspace/srv/kbd/include/kbd.h |
---|
37,7 → 37,8 |
#ifndef KBD_KBD_H_ |
#define KBD_KBD_H_ |
#include <key_buffer.h> |
#include <keybuffer.h> |
#include <ipc/ipc.h> |
#define KBD_EVENT 1024 |
#define KBD_MS_LEFT 1025 |
45,6 → 46,11 |
#define KBD_MS_MIDDLE 1027 |
#define KBD_MS_MOVE 1028 |
typedef enum { |
KBD_YIELD = IPC_FIRST_USER_METHOD, |
KBD_RECLAIM |
} kbd_request_t; |
extern int cir_service; |
extern int cir_phone; |
56,4 → 62,3 |
/** |
* @} |
*/ |
/branches/network/uspace/srv/kbd/include/keybuffer.h |
---|
0,0 → 1,65 |
/* |
* Copyright (c) 2006 Josef Cejka |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup kbdgen |
* @brief HelenOS generic uspace keyboard handler. |
* @ingroup kbd |
* @{ |
*/ |
/** @file |
*/ |
#ifndef __KEYBUFFER_H__ |
#define __KEYBUFFER_H__ |
#include <sys/types.h> |
#include <io/console.h> |
#include <bool.h> |
/** Size of buffer for pressed keys */ |
#define KEYBUFFER_SIZE 128 |
typedef struct { |
console_event_t fifo[KEYBUFFER_SIZE]; |
unsigned long head; |
unsigned long tail; |
unsigned long items; |
} keybuffer_t; |
extern void keybuffer_free(keybuffer_t *); |
extern void keybuffer_init(keybuffer_t *); |
extern size_t keybuffer_available(keybuffer_t *); |
extern bool keybuffer_empty(keybuffer_t *); |
extern void keybuffer_push(keybuffer_t *, const console_event_t *); |
extern bool keybuffer_pop(keybuffer_t *, console_event_t *); |
#endif |
/** |
* @} |
*/ |
/branches/network/uspace/srv/kbd/include/kbd_port.h |
---|
38,6 → 38,8 |
#define KBD_PORT_H_ |
extern int kbd_port_init(void); |
extern void kbd_port_yield(void); |
extern void kbd_port_reclaim(void); |
#endif |
/branches/network/uspace/srv/kbd/port/gxemul.c |
---|
69,6 → 69,14 |
return 0; |
} |
void kbd_port_yield(void) |
{ |
} |
void kbd_port_reclaim(void) |
{ |
} |
/** Process data sent when a key is pressed. |
* |
* @param keybuffer Buffer of pressed keys. |
/branches/network/uspace/srv/kbd/port/ns16550.c |
---|
106,6 → 106,14 |
return pio_enable((void *) ns16550_physical, 8, &vaddr); |
} |
void ns16550_port_yield(void) |
{ |
} |
void ns16550_port_reclaim(void) |
{ |
} |
static void ns16550_irq_handler(ipc_callid_t iid, ipc_call_t *call) |
{ |
int scan_code = IPC_GET_ARG2(*call); |
/branches/network/uspace/srv/kbd/port/msim.c |
---|
69,22 → 69,17 |
return 0; |
} |
void kbd_port_yield(void) |
{ |
} |
void kbd_port_reclaim(void) |
{ |
} |
static void msim_irq_handler(ipc_callid_t iid, ipc_call_t *call) |
{ |
int scan_code = IPC_GET_ARG2(*call); |
// static int esc_count=0; |
// if (scan_code == 0x1b) { |
// esc_count++; |
// if (esc_count == 3) |
// __SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE); |
// } else { |
// esc_count=0; |
// } |
// if (fb_fb) |
// return kbd_arch_process_fb(keybuffer, scan_code); |
kbd_push_scancode(scan_code); |
} |
/branches/network/uspace/srv/kbd/port/sun.c |
---|
62,5 → 62,13 |
return -1; |
} |
void kbd_port_yield(void) |
{ |
} |
void kbd_port_reclaim(void) |
{ |
} |
/** @} |
*/ |
/branches/network/uspace/srv/kbd/port/i8042.c |
---|
151,6 → 151,14 |
return 0; |
} |
void kbd_port_yield(void) |
{ |
} |
void kbd_port_reclaim(void) |
{ |
} |
static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call) |
{ |
int status = IPC_GET_ARG1(*call); |
/branches/network/uspace/srv/kbd/port/ski.c |
---|
42,6 → 42,7 |
#include <kbd_port.h> |
#include <sys/types.h> |
#include <thread.h> |
#include <bool.h> |
#define SKI_GETCHAR 21 |
50,6 → 51,8 |
static void *ski_thread_impl(void *arg); |
static int32_t ski_getchar(void); |
static volatile bool polling_disabled = false; |
/** Initialize Ski port driver. */ |
int kbd_port_init(void) |
{ |
64,6 → 67,16 |
return 0; |
} |
void kbd_port_yield(void) |
{ |
polling_disabled = true; |
} |
void kbd_port_reclaim(void) |
{ |
polling_disabled = false; |
} |
/** Thread to poll Ski for keypresses. */ |
static void *ski_thread_impl(void *arg) |
{ |
71,7 → 84,7 |
(void) arg; |
while (1) { |
while (1) { |
while (polling_disabled == false) { |
c = ski_getchar(); |
if (c == 0) |
break; |
/branches/network/uspace/srv/kbd/port/z8530.c |
---|
95,6 → 95,14 |
return 0; |
} |
void z8530_port_yield(void) |
{ |
} |
void z8530_port_reclaim(void) |
{ |
} |
static void z8530_irq_handler(ipc_callid_t iid, ipc_call_t *call) |
{ |
int scan_code = IPC_GET_ARG2(*call); |
/branches/network/uspace/srv/kbd/port/sgcn.c |
---|
42,6 → 42,7 |
#include <sysinfo.h> |
#include <stdio.h> |
#include <thread.h> |
#include <bool.h> |
#define POLL_INTERVAL 10000 |
92,6 → 93,7 |
/* polling thread */ |
static void *sgcn_thread_impl(void *arg); |
static volatile bool polling_disabled = false; |
/** |
* Initializes the SGCN driver. |
120,6 → 122,16 |
return 0; |
} |
void kbd_port_yield(void) |
{ |
polling_disabled = true; |
} |
void kbd_port_reclaim(void) |
{ |
polling_disabled = false; |
} |
/** |
* Handler of the "key pressed" event. Reads codes of all the pressed keys from |
* the buffer. |
154,11 → 166,11 |
(void) arg; |
while (1) { |
if (polling_disabled == false) |
sgcn_key_pressed(); |
usleep(POLL_INTERVAL); |
} |
} |
/** @} |
*/ |
/branches/network/uspace/srv/kbd/port/dummy.c |
---|
42,5 → 42,13 |
return 0; |
} |
void kbd_port_yield(void) |
{ |
} |
void kbd_port_reclaim(void) |
{ |
} |
/** @} |
*/ |
/branches/network/uspace/srv/kbd/genarch/stroke.c |
---|
36,12 → 36,13 |
* When simulating a keyboard using a serial TTY we need to convert the |
* recognized strokes (such as Shift-A) to sequences of key presses and |
* releases (such as 'press Shift, press A, release A, release Shift'). |
* |
*/ |
#include <stroke.h> |
#include <kbd.h> |
#include <kbd/kbd.h> |
#include <kbd/keycode.h> |
#include <io/console.h> |
#include <io/keycode.h> |
/** Correspondence between modifers and the modifier keycodes. */ |
static unsigned int mods_keys[][2] = { |
58,7 → 59,7 |
i = 0; |
while (mods_keys[i][0] != 0) { |
if (mod & mods_keys[i][0]) { |
kbd_push_ev(KE_PRESS, mods_keys[i][1]); |
kbd_push_ev(KEY_PRESS, mods_keys[i][1]); |
} |
++i; |
} |
65,8 → 66,8 |
/* Simulate key press and release. */ |
if (key != 0) { |
kbd_push_ev(KE_PRESS, key); |
kbd_push_ev(KE_RELEASE, key); |
kbd_push_ev(KEY_PRESS, key); |
kbd_push_ev(KEY_RELEASE, key); |
} |
/* Simulate modifier releases. */ |
73,7 → 74,7 |
i = 0; |
while (mods_keys[i][0] != 0) { |
if (mod & mods_keys[i][0]) { |
kbd_push_ev(KE_RELEASE, mods_keys[i][1]); |
kbd_push_ev(KEY_RELEASE, mods_keys[i][1]); |
} |
++i; |
} |
/branches/network/uspace/srv/kbd/genarch/gsp.c |
---|
49,7 → 49,7 |
*/ |
#include <gsp.h> |
#include <libadt/hash_table.h> |
#include <adt/hash_table.h> |
#include <stdlib.h> |
#include <stdio.h> |
/branches/network/uspace/srv/kbd/generic/key_buffer.c |
---|
File deleted |
/branches/network/uspace/srv/kbd/generic/kbd.c |
---|
45,12 → 45,12 |
#include <ipc/ns.h> |
#include <async.h> |
#include <errno.h> |
#include <libadt/fifo.h> |
#include <kbd/kbd.h> |
#include <kbd/keycode.h> |
#include <adt/fifo.h> |
#include <io/console.h> |
#include <io/keycode.h> |
#include <kbd.h> |
#include <key_buffer.h> |
#include <keybuffer.h> |
#include <kbd_port.h> |
#include <kbd_ctl.h> |
#include <layout.h> |
88,7 → 88,7 |
void kbd_push_ev(int type, unsigned int key) |
{ |
kbd_event_t ev; |
console_event_t ev; |
unsigned mod_mask; |
switch (key) { |
102,7 → 102,7 |
} |
if (mod_mask != 0) { |
if (type == KE_PRESS) |
if (type == KEY_PRESS) |
mods = mods | mod_mask; |
else |
mods = mods & ~mod_mask; |
116,7 → 116,7 |
} |
if (mod_mask != 0) { |
if (type == KE_PRESS) { |
if (type == KEY_PRESS) { |
/* |
* Only change lock state on transition from released |
* to pressed. This prevents autorepeat from messing |
133,7 → 133,7 |
printf("mods: 0x%x\n", mods); |
printf("keycode: %u\n", key); |
*/ |
if (type == KE_PRESS && (mods & KM_LCTRL) && |
if (type == KEY_PRESS && (mods & KM_LCTRL) && |
key == KC_F1) { |
active_layout = 0; |
layout[active_layout]->reset(); |
140,7 → 140,7 |
return; |
} |
if (type == KE_PRESS && (mods & KM_LCTRL) && |
if (type == KEY_PRESS && (mods & KM_LCTRL) && |
key == KC_F2) { |
active_layout = 1; |
layout[active_layout]->reset(); |
147,7 → 147,7 |
return; |
} |
if (type == KE_PRESS && (mods & KM_LCTRL) && |
if (type == KEY_PRESS && (mods & KM_LCTRL) && |
key == KC_F3) { |
active_layout = 2; |
layout[active_layout]->reset(); |
193,6 → 193,14 |
phone2cons = IPC_GET_ARG5(call); |
retval = 0; |
break; |
case KBD_YIELD: |
kbd_port_yield(); |
retval = 0; |
break; |
case KBD_RECLAIM: |
kbd_port_reclaim(); |
retval = 0; |
break; |
default: |
retval = EINVAL; |
} |
/branches/network/uspace/srv/kbd/generic/keybuffer.c |
---|
0,0 → 1,132 |
/* |
* Copyright (c) 2006 Josef Cejka |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup kbdgen |
* @brief HelenOS generic uspace keyboard handler. |
* @ingroup kbd |
* @{ |
*/ |
/** @file |
*/ |
#include <keybuffer.h> |
#include <futex.h> |
atomic_t keybuffer_futex = FUTEX_INITIALIZER; |
/** Clear key buffer. |
*/ |
void keybuffer_free(keybuffer_t *keybuffer) |
{ |
futex_down(&keybuffer_futex); |
keybuffer->head = 0; |
keybuffer->tail = 0; |
keybuffer->items = 0; |
futex_up(&keybuffer_futex); |
} |
/** Key buffer initialization. |
* |
*/ |
void keybuffer_init(keybuffer_t *keybuffer) |
{ |
keybuffer_free(keybuffer); |
} |
/** Get free space in buffer. |
* |
* This function is useful for processing some scancodes that are translated |
* to more than one character. |
* |
* @return empty buffer space |
* |
*/ |
size_t keybuffer_available(keybuffer_t *keybuffer) |
{ |
return KEYBUFFER_SIZE - keybuffer->items; |
} |
/** |
* |
* @return nonzero, if buffer is not empty. |
* |
*/ |
bool keybuffer_empty(keybuffer_t *keybuffer) |
{ |
return (keybuffer->items == 0); |
} |
/** Push key event to key buffer. |
* |
* If the buffer is full, the event is ignored. |
* |
* @param keybuffer The keybuffer. |
* @param ev The event to push. |
* |
*/ |
void keybuffer_push(keybuffer_t *keybuffer, const console_event_t *ev) |
{ |
futex_down(&keybuffer_futex); |
if (keybuffer->items < KEYBUFFER_SIZE) { |
keybuffer->fifo[keybuffer->tail] = *ev; |
keybuffer->tail = (keybuffer->tail + 1) % KEYBUFFER_SIZE; |
keybuffer->items++; |
} |
futex_up(&keybuffer_futex); |
} |
/** Pop event from buffer. |
* |
* @param edst Pointer to where the event should be saved. |
* |
* @return True if an event was popped. |
* |
*/ |
bool keybuffer_pop(keybuffer_t *keybuffer, console_event_t *edst) |
{ |
futex_down(&keybuffer_futex); |
if (keybuffer->items > 0) { |
keybuffer->items--; |
*edst = (keybuffer->fifo[keybuffer->head]); |
keybuffer->head = (keybuffer->head + 1) % KEYBUFFER_SIZE; |
futex_up(&keybuffer_futex); |
return true; |
} |
futex_up(&keybuffer_futex); |
return false; |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/kbd/Makefile |
---|
34,7 → 34,7 |
include $(LIBC_PREFIX)/Makefile.toolchain |
CFLAGS += -Iinclude -I../libadt/include |
CFLAGS += -Iinclude |
LIBS = $(LIBC_PREFIX)/libc.a |
46,7 → 46,7 |
generic/kbd.c \ |
genarch/gsp.c \ |
genarch/stroke.c \ |
generic/key_buffer.c |
generic/keybuffer.c |
ARCH_SOURCES = |
GENARCH_SOURCES = \ |
112,9 → 112,16 |
ifeq ($(MACHINE), bgxemul) |
GENARCH_SOURCES += \ |
port/gxemul.c \ |
port/gxemul.c |
ifeq ($(CONFIG_FB), y) |
GENARCH_SOURCES += \ |
ctl/gxe_fb.c |
else |
GENARCH_SOURCES += \ |
ctl/stty.c |
endif |
endif |
ifeq ($(UARCH), ppc32) |
GENARCH_SOURCES += \ |
/branches/network/uspace/srv/kbd/layout/us_qwerty.c |
---|
32,12 → 32,12 |
*/ |
#include <kbd.h> |
#include <kbd/kbd.h> |
#include <kbd/keycode.h> |
#include <io/console.h> |
#include <io/keycode.h> |
#include <layout.h> |
static void layout_reset(void); |
static wchar_t layout_parse_ev(kbd_event_t *ev); |
static wchar_t layout_parse_ev(console_event_t *ev); |
layout_op_t us_qwerty_op = { |
layout_reset, |
203,7 → 203,7 |
{ |
} |
static wchar_t layout_parse_ev(kbd_event_t *ev) |
static wchar_t layout_parse_ev(console_event_t *ev) |
{ |
wchar_t c; |
/branches/network/uspace/srv/kbd/layout/cz.c |
---|
32,13 → 32,13 |
*/ |
#include <kbd.h> |
#include <kbd/kbd.h> |
#include <kbd/keycode.h> |
#include <io/console.h> |
#include <io/keycode.h> |
#include <bool.h> |
#include <layout.h> |
static void layout_reset(void); |
static wchar_t layout_parse_ev(kbd_event_t *ev); |
static wchar_t layout_parse_ev(console_event_t *ev); |
enum m_state { |
ms_start, |
272,7 → 272,7 |
return map[key]; |
} |
static wchar_t parse_ms_hacek(kbd_event_t *ev) |
static wchar_t parse_ms_hacek(console_event_t *ev) |
{ |
wchar_t c; |
290,7 → 290,7 |
return c; |
} |
static wchar_t parse_ms_carka(kbd_event_t *ev) |
static wchar_t parse_ms_carka(console_event_t *ev) |
{ |
wchar_t c; |
308,7 → 308,7 |
return c; |
} |
static wchar_t parse_ms_start(kbd_event_t *ev) |
static wchar_t parse_ms_start(console_event_t *ev) |
{ |
wchar_t c; |
383,18 → 383,21 |
mstate = ms_start; |
} |
static wchar_t layout_parse_ev(kbd_event_t *ev) |
static wchar_t layout_parse_ev(console_event_t *ev) |
{ |
if (ev->type != KE_PRESS) |
return '\0'; |
if (ev->type != KEY_PRESS) |
return 0; |
if (key_is_mod(ev->key)) |
return '\0'; |
return 0; |
switch (mstate) { |
case ms_start: return parse_ms_start(ev); |
case ms_hacek: return parse_ms_hacek(ev); |
case ms_carka: return parse_ms_carka(ev); |
case ms_start: |
return parse_ms_start(ev); |
case ms_hacek: |
return parse_ms_hacek(ev); |
case ms_carka: |
return parse_ms_carka(ev); |
} |
} |
/branches/network/uspace/srv/kbd/layout/us_dvorak.c |
---|
32,12 → 32,12 |
*/ |
#include <kbd.h> |
#include <kbd/kbd.h> |
#include <kbd/keycode.h> |
#include <io/console.h> |
#include <io/keycode.h> |
#include <layout.h> |
static void layout_reset(void); |
static wchar_t layout_parse_ev(kbd_event_t *ev); |
static wchar_t layout_parse_ev(console_event_t *ev); |
layout_op_t us_dvorak_op = { |
layout_reset, |
209,7 → 209,7 |
{ |
} |
static wchar_t layout_parse_ev(kbd_event_t *ev) |
static wchar_t layout_parse_ev(console_event_t *ev) |
{ |
wchar_t c; |
/branches/network/uspace/srv/ns/clonable.c |
---|
0,0 → 1,143 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup ns |
* @{ |
*/ |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <adt/list.h> |
#include <bool.h> |
#include <errno.h> |
#include <assert.h> |
#include <stdio.h> |
#include <malloc.h> |
#include <loader/loader.h> |
#include "clonable.h" |
#include "ns.h" |
/** Request for connection to a clonable service. */ |
typedef struct { |
link_t link; |
ipcarg_t service; |
ipc_call_t call; |
ipc_callid_t callid; |
} cs_req_t; |
/** List of clonable-service connection requests. */ |
static link_t cs_req; |
int clonable_init(void) |
{ |
list_initialize(&cs_req); |
return EOK; |
} |
/** Return true if @a service is clonable. */ |
bool service_clonable(int service) |
{ |
return (service == SERVICE_LOAD); |
} |
/** Register clonable service. |
* |
* @param service Service to be registered. |
* @param phone Phone to be used for connections to the service. |
* @param call Pointer to call structure. |
* |
*/ |
void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call, |
ipc_callid_t callid) |
{ |
if (list_empty(&cs_req)) { |
/* There was no pending connection request. */ |
printf(NAME ": Unexpected clonable server.\n"); |
ipc_answer_0(callid, EBUSY); |
return; |
} |
cs_req_t *csr = list_get_instance(cs_req.next, cs_req_t, link); |
list_remove(&csr->link); |
/* Currently we can only handle a single type of clonable service. */ |
assert(csr->service == SERVICE_LOAD); |
ipc_answer_0(callid, EOK); |
ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(csr->call), |
IPC_GET_ARG3(csr->call), 0, IPC_FF_NONE); |
free(csr); |
ipc_hangup(phone); |
} |
/** Connect client to clonable service. |
* |
* @param service Service to be connected to. |
* @param call Pointer to call structure. |
* @param callid Call ID of the request. |
* |
* @return Zero on success or a value from @ref errno.h. |
* |
*/ |
void connect_to_clonable(ipcarg_t service, ipc_call_t *call, |
ipc_callid_t callid) |
{ |
assert(service == SERVICE_LOAD); |
cs_req_t *csr = malloc(sizeof(cs_req_t)); |
if (csr == NULL) { |
ipc_answer_0(callid, ENOMEM); |
return; |
} |
/* Spawn a loader. */ |
int rc = loader_spawn("loader"); |
if (rc < 0) { |
free(csr); |
ipc_answer_0(callid, rc); |
return; |
} |
csr->service = service; |
csr->call = *call; |
csr->callid = callid; |
/* |
* We can forward the call only after the server we spawned connects |
* to us. Meanwhile we might need to service more connection requests. |
* Thus we store the call in a queue. |
*/ |
list_append(&csr->link, &cs_req); |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/ns/ns.c |
---|
35,93 → 35,28 |
* @brief Naming service for HelenOS IPC. |
*/ |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <ipc/ns.h> |
#include <ipc/services.h> |
#include <unistd.h> |
#include <stdio.h> |
#include <bool.h> |
#include <unistd.h> |
#include <stdlib.h> |
#include <errno.h> |
#include <assert.h> |
#include <libadt/list.h> |
#include <libadt/hash_table.h> |
#include <as.h> |
#include <ddi.h> |
#include <event.h> |
#include <macros.h> |
#include <sysinfo.h> |
#include <loader/loader.h> |
#include <ddi.h> |
#include <as.h> |
#include "ns.h" |
#include "service.h" |
#include "clonable.h" |
#include "task.h" |
#define NAME "ns" |
#define NS_HASH_TABLE_CHAINS 20 |
static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call); |
static void connect_to_service(ipcarg_t service, ipc_call_t *call, |
ipc_callid_t callid); |
void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call, |
ipc_callid_t callid); |
void connect_to_clonable(ipcarg_t service, ipc_call_t *call, |
ipc_callid_t callid); |
/* Static functions implementing NS hash table operations. */ |
static hash_index_t ns_hash(unsigned long *key); |
static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item); |
static void ns_remove(link_t *item); |
/** Operations for NS hash table. */ |
static hash_table_operations_t ns_hash_table_ops = { |
.hash = ns_hash, |
.compare = ns_compare, |
.remove_callback = ns_remove |
}; |
/** NS hash table structure. */ |
static hash_table_t ns_hash_table; |
/** NS hash table item. */ |
typedef struct { |
link_t link; |
ipcarg_t service; /**< Number of the service. */ |
ipcarg_t phone; /**< Phone registered with the service. */ |
ipcarg_t in_phone_hash; /**< Incoming phone hash. */ |
} hashed_service_t; |
/** Pending connection structure. */ |
typedef struct { |
link_t link; |
ipcarg_t service; /**< Number of the service. */ |
ipc_callid_t callid; /**< Call ID waiting for the connection */ |
ipcarg_t arg2; /**< Second argument */ |
ipcarg_t arg3; /**< Third argument */ |
} pending_req_t; |
static link_t pending_req; |
/** Request for connection to a clonable service. */ |
typedef struct { |
link_t link; |
ipcarg_t service; |
ipc_call_t call; |
ipc_callid_t callid; |
} cs_req_t; |
/** List of clonable-service connection requests. */ |
static link_t cs_req; |
static void *clockaddr = NULL; |
static void *klogaddr = NULL; |
/** Return true if @a service is clonable. */ |
static bool service_clonable(int service) |
static void get_as_area(ipc_callid_t callid, ipc_call_t *call, void *ph_addr, |
size_t pages, void **addr) |
{ |
return (service == SERVICE_LOAD); |
} |
static void get_as_area(ipc_callid_t callid, ipc_call_t *call, void *ph_addr, count_t pages, void **addr) |
{ |
if (ph_addr == NULL) { |
ipc_answer_0(callid, ENOENT); |
return; |
145,67 → 80,53 |
ipc_answer_2(callid, EOK, (ipcarg_t) *addr, AS_AREA_READ); |
} |
/** Process pending connection requests */ |
static void process_pending_req() |
{ |
link_t *cur; |
loop: |
for (cur = pending_req.next; cur != &pending_req; cur = cur->next) { |
pending_req_t *pr = list_get_instance(cur, pending_req_t, link); |
unsigned long keys[3] = { |
pr->service, |
0, |
0 |
}; |
link_t *link = hash_table_find(&ns_hash_table, keys); |
if (!link) |
continue; |
hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link); |
ipcarg_t retval = ipc_forward_fast(pr->callid, hs->phone, |
pr->arg2, pr->arg3, 0, IPC_FF_NONE); |
if (!(pr->callid & IPC_CALLID_NOTIFICATION)) |
ipc_answer_0(pr->callid, retval); |
list_remove(cur); |
free(pr); |
goto loop; |
} |
} |
int main(int argc, char **argv) |
{ |
printf(NAME ": HelenOS IPC Naming Service\n"); |
if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3, |
&ns_hash_table_ops)) { |
printf(NAME ": No memory available for services\n"); |
return ENOMEM; |
} |
int rc = service_init(); |
if (rc != EOK) |
return rc; |
list_initialize(&pending_req); |
list_initialize(&cs_req); |
rc = clonable_init(); |
if (rc != EOK) |
return rc; |
rc = task_init(); |
if (rc != EOK) |
return rc; |
printf(NAME ": Accepting connections\n"); |
while (true) { |
process_pending_req(); |
process_pending_conn(); |
process_pending_wait(); |
ipc_call_t call; |
ipc_callid_t callid = ipc_wait_for_call(&call); |
task_id_t id; |
ipcarg_t retval; |
if (callid & IPC_CALLID_NOTIFICATION) { |
id = (task_id_t) |
MERGE_LOUP32(IPC_GET_ARG2(call), IPC_GET_ARG3(call)); |
wait_notification((wait_type_t) IPC_GET_ARG1(call), id); |
continue; |
} |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_SHARE_IN: |
switch (IPC_GET_ARG3(call)) { |
case SERVICE_MEM_REALTIME: |
get_as_area(callid, &call, sysinfo_value("clock.faddr"), 1, &clockaddr); |
get_as_area(callid, &call, |
(void *) sysinfo_value("clock.faddr"), |
1, &clockaddr); |
break; |
case SERVICE_MEM_KLOG: |
get_as_area(callid, &call, sysinfo_value("klog.faddr"), sysinfo_value("klog.pages"), &klogaddr); |
get_as_area(callid, &call, |
(void *) sysinfo_value("klog.faddr"), |
sysinfo_value("klog.pages"), &klogaddr); |
break; |
default: |
ipc_answer_0(callid, ENOENT); |
241,6 → 162,14 |
continue; |
} |
break; |
case NS_PING: |
retval = EOK; |
break; |
case NS_TASK_WAIT: |
id = (task_id_t) |
MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call)); |
wait_for_task(id, &call, callid); |
continue; |
default: |
retval = ENOENT; |
break; |
254,213 → 183,6 |
return 0; |
} |
/** Register service. |
* |
* @param service Service to be registered. |
* @param phone Phone to be used for connections to the service. |
* @param call Pointer to call structure. |
* |
* @return Zero on success or a value from @ref errno.h. |
* |
*/ |
int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call) |
{ |
unsigned long keys[3] = { |
service, |
call->in_phone_hash, |
0 |
}; |
if (hash_table_find(&ns_hash_table, keys)) |
return EEXISTS; |
hashed_service_t *hs = (hashed_service_t *) malloc(sizeof(hashed_service_t)); |
if (!hs) |
return ENOMEM; |
link_initialize(&hs->link); |
hs->service = service; |
hs->phone = phone; |
hs->in_phone_hash = call->in_phone_hash; |
hash_table_insert(&ns_hash_table, keys, &hs->link); |
return 0; |
} |
/** Connect client to service. |
* |
* @param service Service to be connected to. |
* @param call Pointer to call structure. |
* @param callid Call ID of the request. |
* |
* @return Zero on success or a value from @ref errno.h. |
* |
*/ |
void connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid) |
{ |
ipcarg_t retval; |
unsigned long keys[3] = { |
service, |
0, |
0 |
}; |
link_t *link = hash_table_find(&ns_hash_table, keys); |
if (!link) { |
if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) { |
/* Blocking connection, add to pending list */ |
pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t)); |
if (!pr) { |
retval = ENOMEM; |
goto out; |
} |
pr->service = service; |
pr->callid = callid; |
pr->arg2 = IPC_GET_ARG2(*call); |
pr->arg3 = IPC_GET_ARG3(*call); |
list_append(&pr->link, &pending_req); |
return; |
} |
retval = ENOENT; |
goto out; |
} |
hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link); |
retval = ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call), |
IPC_GET_ARG3(*call), 0, IPC_FF_NONE); |
out: |
if (!(callid & IPC_CALLID_NOTIFICATION)) |
ipc_answer_0(callid, retval); |
} |
/** Register clonable service. |
* |
* @param service Service to be registered. |
* @param phone Phone to be used for connections to the service. |
* @param call Pointer to call structure. |
* |
*/ |
void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call, |
ipc_callid_t callid) |
{ |
if (list_empty(&cs_req)) { |
/* There was no pending connection request. */ |
printf(NAME ": Unexpected clonable server.\n"); |
ipc_answer_0(callid, EBUSY); |
return; |
} |
cs_req_t *csr = list_get_instance(cs_req.next, cs_req_t, link); |
list_remove(&csr->link); |
/* Currently we can only handle a single type of clonable service. */ |
assert(csr->service == SERVICE_LOAD); |
ipc_answer_0(callid, EOK); |
int rc = ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(csr->call), |
IPC_GET_ARG3(csr->call), 0, IPC_FF_NONE); |
free(csr); |
ipc_hangup(phone); |
} |
/** Connect client to clonable service. |
* |
* @param service Service to be connected to. |
* @param call Pointer to call structure. |
* @param callid Call ID of the request. |
* |
* @return Zero on success or a value from @ref errno.h. |
* |
*/ |
void connect_to_clonable(ipcarg_t service, ipc_call_t *call, |
ipc_callid_t callid) |
{ |
assert(service == SERVICE_LOAD); |
cs_req_t *csr = malloc(sizeof(cs_req_t)); |
if (csr == NULL) { |
ipc_answer_0(callid, ENOMEM); |
return; |
} |
/* Spawn a loader. */ |
int rc = loader_spawn("loader"); |
if (rc < 0) { |
free(csr); |
ipc_answer_0(callid, rc); |
return; |
} |
csr->service = service; |
csr->call = *call; |
csr->callid = callid; |
/* |
* We can forward the call only after the server we spawned connects |
* to us. Meanwhile we might need to service more connection requests. |
* Thus we store the call in a queue. |
*/ |
list_append(&csr->link, &cs_req); |
} |
/** Compute hash index into NS hash table. |
* |
* @param key Pointer keys. However, only the first key (i.e. service number) |
* is used to compute the hash index. |
* |
* @return Hash index corresponding to key[0]. |
* |
*/ |
hash_index_t ns_hash(unsigned long *key) |
{ |
assert(key); |
return (*key % NS_HASH_TABLE_CHAINS); |
} |
/** Compare a key with hashed item. |
* |
* This compare function always ignores the third key. |
* It exists only to make it possible to remove records |
* originating from connection with key[1] in_phone_hash |
* value. Note that this is close to being classified |
* as a nasty hack. |
* |
* @param key Array of keys. |
* @param keys Must be lesser or equal to 3. |
* @param item Pointer to a hash table item. |
* |
* @return Non-zero if the key matches the item, zero otherwise. |
* |
*/ |
int ns_compare(unsigned long key[], hash_count_t keys, link_t *item) |
{ |
assert(key); |
assert(keys <= 3); |
assert(item); |
hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link); |
if (keys == 2) |
return key[1] == hs->in_phone_hash; |
else |
return key[0] == hs->service; |
} |
/** Perform actions after removal of item from the hash table. |
* |
* @param item Item that was removed from the hash table. |
* |
*/ |
void ns_remove(link_t *item) |
{ |
assert(item); |
free(hash_table_get_instance(item, hashed_service_t, link)); |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/ns/service.c |
---|
0,0 → 1,254 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup ns |
* @{ |
*/ |
#include <ipc/ipc.h> |
#include <adt/hash_table.h> |
#include <assert.h> |
#include <errno.h> |
#include "service.h" |
#include "ns.h" |
#define SERVICE_HASH_TABLE_CHAINS 20 |
/** Service hash table item. */ |
typedef struct { |
link_t link; |
ipcarg_t service; /**< Number of the service. */ |
ipcarg_t phone; /**< Phone registered with the service. */ |
ipcarg_t in_phone_hash; /**< Incoming phone hash. */ |
} hashed_service_t; |
/** Compute hash index into service hash table. |
* |
* @param key Pointer keys. However, only the first key (i.e. service number) |
* is used to compute the hash index. |
* |
* @return Hash index corresponding to key[0]. |
* |
*/ |
static hash_index_t service_hash(unsigned long *key) |
{ |
assert(key); |
return (*key % SERVICE_HASH_TABLE_CHAINS); |
} |
/** Compare a key with hashed item. |
* |
* This compare function always ignores the third key. |
* It exists only to make it possible to remove records |
* originating from connection with key[1] in_phone_hash |
* value. Note that this is close to being classified |
* as a nasty hack. |
* |
* @param key Array of keys. |
* @param keys Must be lesser or equal to 3. |
* @param item Pointer to a hash table item. |
* |
* @return Non-zero if the key matches the item, zero otherwise. |
* |
*/ |
static int service_compare(unsigned long key[], hash_count_t keys, link_t *item) |
{ |
assert(key); |
assert(keys <= 3); |
assert(item); |
hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link); |
if (keys == 2) |
return (key[1] == hs->in_phone_hash); |
else |
return (key[0] == hs->service); |
} |
/** Perform actions after removal of item from the hash table. |
* |
* @param item Item that was removed from the hash table. |
* |
*/ |
static void service_remove(link_t *item) |
{ |
assert(item); |
free(hash_table_get_instance(item, hashed_service_t, link)); |
} |
/** Operations for service hash table. */ |
static hash_table_operations_t service_hash_table_ops = { |
.hash = service_hash, |
.compare = service_compare, |
.remove_callback = service_remove |
}; |
/** Service hash table structure. */ |
static hash_table_t service_hash_table; |
/** Pending connection structure. */ |
typedef struct { |
link_t link; |
ipcarg_t service; /**< Number of the service. */ |
ipc_callid_t callid; /**< Call ID waiting for the connection */ |
ipcarg_t arg2; /**< Second argument */ |
ipcarg_t arg3; /**< Third argument */ |
} pending_conn_t; |
static link_t pending_conn; |
int service_init(void) |
{ |
if (!hash_table_create(&service_hash_table, SERVICE_HASH_TABLE_CHAINS, |
3, &service_hash_table_ops)) { |
printf(NAME ": No memory available for services\n"); |
return ENOMEM; |
} |
list_initialize(&pending_conn); |
return EOK; |
} |
/** Process pending connection requests */ |
void process_pending_conn(void) |
{ |
link_t *cur; |
loop: |
for (cur = pending_conn.next; cur != &pending_conn; cur = cur->next) { |
pending_conn_t *pr = list_get_instance(cur, pending_conn_t, link); |
unsigned long keys[3] = { |
pr->service, |
0, |
0 |
}; |
link_t *link = hash_table_find(&service_hash_table, keys); |
if (!link) |
continue; |
hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link); |
ipcarg_t retval = ipc_forward_fast(pr->callid, hs->phone, |
pr->arg2, pr->arg3, 0, IPC_FF_NONE); |
if (!(pr->callid & IPC_CALLID_NOTIFICATION)) |
ipc_answer_0(pr->callid, retval); |
list_remove(cur); |
free(pr); |
goto loop; |
} |
} |
/** Register service. |
* |
* @param service Service to be registered. |
* @param phone Phone to be used for connections to the service. |
* @param call Pointer to call structure. |
* |
* @return Zero on success or a value from @ref errno.h. |
* |
*/ |
int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call) |
{ |
unsigned long keys[3] = { |
service, |
call->in_phone_hash, |
0 |
}; |
if (hash_table_find(&service_hash_table, keys)) |
return EEXISTS; |
hashed_service_t *hs = (hashed_service_t *) malloc(sizeof(hashed_service_t)); |
if (!hs) |
return ENOMEM; |
link_initialize(&hs->link); |
hs->service = service; |
hs->phone = phone; |
hs->in_phone_hash = call->in_phone_hash; |
hash_table_insert(&service_hash_table, keys, &hs->link); |
return 0; |
} |
/** Connect client to service. |
* |
* @param service Service to be connected to. |
* @param call Pointer to call structure. |
* @param callid Call ID of the request. |
* |
* @return Zero on success or a value from @ref errno.h. |
* |
*/ |
void connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid) |
{ |
ipcarg_t retval; |
unsigned long keys[3] = { |
service, |
0, |
0 |
}; |
link_t *link = hash_table_find(&service_hash_table, keys); |
if (!link) { |
if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) { |
/* Blocking connection, add to pending list */ |
pending_conn_t *pr = |
(pending_conn_t *) malloc(sizeof(pending_conn_t)); |
if (!pr) { |
retval = ENOMEM; |
goto out; |
} |
pr->service = service; |
pr->callid = callid; |
pr->arg2 = IPC_GET_ARG2(*call); |
pr->arg3 = IPC_GET_ARG3(*call); |
list_append(&pr->link, &pending_conn); |
return; |
} |
retval = ENOENT; |
goto out; |
} |
hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link); |
retval = ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call), |
IPC_GET_ARG3(*call), 0, IPC_FF_NONE); |
out: |
if (!(callid & IPC_CALLID_NOTIFICATION)) |
ipc_answer_0(callid, retval); |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/ns/task.c |
---|
0,0 → 1,269 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup ns |
* @{ |
*/ |
#include <ipc/ipc.h> |
#include <adt/hash_table.h> |
#include <bool.h> |
#include <errno.h> |
#include <assert.h> |
#include <stdio.h> |
#include <macros.h> |
#include "task.h" |
#include "ns.h" |
#define TASK_HASH_TABLE_CHAINS 256 |
/* TODO: |
* |
* The current implementation of waiting on a task is not perfect. If somebody |
* wants to wait on a task which has already finished before the NS asked |
* the kernel to receive notifications, it would block indefinitively. |
* |
* A solution to this is to fail immediately on a task for which no creation |
* notification was received yet. However, there is a danger of a race condition |
* in this solution -- the caller has to make sure that it is not trying to wait |
* before the NS has a change to receive the task creation notification. This |
* can be assured by waiting for this event in task_spawn(). |
* |
* Finally, as there is currently no convention that each task has to be waited |
* for, the NS can leak memory because of the zombie tasks. |
* |
*/ |
/** Task hash table item. */ |
typedef struct { |
link_t link; |
task_id_t id; /**< Task ID. */ |
bool destroyed; |
} hashed_task_t; |
/** Compute hash index into task hash table. |
* |
* @param key Pointer keys. However, only the first key (i.e. truncated task |
* number) is used to compute the hash index. |
* |
* @return Hash index corresponding to key[0]. |
* |
*/ |
static hash_index_t task_hash(unsigned long *key) |
{ |
assert(key); |
return (LOWER32(*key) % TASK_HASH_TABLE_CHAINS); |
} |
/** Compare a key with hashed item. |
* |
* @param key Array of keys. |
* @param keys Must be lesser or equal to 2. |
* @param item Pointer to a hash table item. |
* |
* @return Non-zero if the key matches the item, zero otherwise. |
* |
*/ |
static int task_compare(unsigned long key[], hash_count_t keys, link_t *item) |
{ |
assert(key); |
assert(keys <= 2); |
assert(item); |
hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link); |
if (keys == 2) |
return ((LOWER32(key[1]) == UPPER32(ht->id)) |
&& (LOWER32(key[0]) == LOWER32(ht->id))); |
else |
return (LOWER32(key[0]) == LOWER32(ht->id)); |
} |
/** Perform actions after removal of item from the hash table. |
* |
* @param item Item that was removed from the hash table. |
* |
*/ |
static void task_remove(link_t *item) |
{ |
assert(item); |
free(hash_table_get_instance(item, hashed_task_t, link)); |
} |
/** Operations for task hash table. */ |
static hash_table_operations_t task_hash_table_ops = { |
.hash = task_hash, |
.compare = task_compare, |
.remove_callback = task_remove |
}; |
/** Task hash table structure. */ |
static hash_table_t task_hash_table; |
/** Pending task wait structure. */ |
typedef struct { |
link_t link; |
task_id_t id; /**< Task ID. */ |
ipc_callid_t callid; /**< Call ID waiting for the connection */ |
} pending_wait_t; |
static link_t pending_wait; |
int task_init(void) |
{ |
if (!hash_table_create(&task_hash_table, TASK_HASH_TABLE_CHAINS, |
2, &task_hash_table_ops)) { |
printf(NAME ": No memory available for tasks\n"); |
return ENOMEM; |
} |
if (event_subscribe(EVENT_WAIT, 0) != EOK) |
printf(NAME ": Error registering wait notifications\n"); |
list_initialize(&pending_wait); |
return EOK; |
} |
/** Process pending wait requests */ |
void process_pending_wait(void) |
{ |
link_t *cur; |
loop: |
for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) { |
pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link); |
unsigned long keys[2] = { |
LOWER32(pr->id), |
UPPER32(pr->id) |
}; |
link_t *link = hash_table_find(&task_hash_table, keys); |
if (!link) |
continue; |
hashed_task_t *ht = hash_table_get_instance(link, hashed_task_t, link); |
if (!ht->destroyed) |
continue; |
if (!(pr->callid & IPC_CALLID_NOTIFICATION)) |
ipc_answer_0(pr->callid, EOK); |
hash_table_remove(&task_hash_table, keys, 2); |
list_remove(cur); |
free(pr); |
goto loop; |
} |
} |
static void fail_pending_wait(task_id_t id, int rc) |
{ |
link_t *cur; |
loop: |
for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) { |
pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link); |
if (pr->id == id) { |
if (!(pr->callid & IPC_CALLID_NOTIFICATION)) |
ipc_answer_0(pr->callid, rc); |
list_remove(cur); |
free(pr); |
goto loop; |
} |
} |
} |
void wait_notification(wait_type_t et, task_id_t id) |
{ |
unsigned long keys[2] = { |
LOWER32(id), |
UPPER32(id) |
}; |
link_t *link = hash_table_find(&task_hash_table, keys); |
if (link == NULL) { |
hashed_task_t *ht = |
(hashed_task_t *) malloc(sizeof(hashed_task_t)); |
if (ht == NULL) { |
fail_pending_wait(id, ENOMEM); |
return; |
} |
link_initialize(&ht->link); |
ht->id = id; |
ht->destroyed = (et == TASK_CREATE) ? false : true; |
hash_table_insert(&task_hash_table, keys, &ht->link); |
} else { |
hashed_task_t *ht = |
hash_table_get_instance(link, hashed_task_t, link); |
ht->destroyed = (et == TASK_CREATE) ? false : true; |
} |
} |
void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid) |
{ |
ipcarg_t retval; |
unsigned long keys[2] = { |
LOWER32(id), |
UPPER32(id) |
}; |
link_t *link = hash_table_find(&task_hash_table, keys); |
hashed_task_t *ht = (link != NULL) ? |
hash_table_get_instance(link, hashed_task_t, link) : NULL; |
if ((ht == NULL) || (!ht->destroyed)) { |
/* Add to pending list */ |
pending_wait_t *pr = |
(pending_wait_t *) malloc(sizeof(pending_wait_t)); |
if (!pr) { |
retval = ENOMEM; |
goto out; |
} |
pr->id = id; |
pr->callid = callid; |
list_append(&pr->link, &pending_wait); |
return; |
} |
hash_table_remove(&task_hash_table, keys, 2); |
retval = EOK; |
out: |
if (!(callid & IPC_CALLID_NOTIFICATION)) |
ipc_answer_0(callid, retval); |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/ns/clonable.h |
---|
0,0 → 1,51 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup ns |
* @{ |
*/ |
#ifndef NS_CLONABLE_H__ |
#define NS_CLONABLE_H__ |
#include <ipc/ipc.h> |
#include <bool.h> |
extern int clonable_init(void); |
extern bool service_clonable(int service); |
extern void register_clonable(ipcarg_t service, ipcarg_t phone, |
ipc_call_t *call, ipc_callid_t callid); |
extern void connect_to_clonable(ipcarg_t service, ipc_call_t *call, |
ipc_callid_t callid); |
#endif |
/** |
* @} |
*/ |
/branches/network/uspace/srv/ns/ns.h |
---|
0,0 → 1,42 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup ns |
* @{ |
*/ |
#ifndef NS_NS_H__ |
#define NS_NS_H__ |
#define NAME "ns" |
#endif |
/** |
* @} |
*/ |
/branches/network/uspace/srv/ns/service.h |
---|
0,0 → 1,49 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup ns |
* @{ |
*/ |
#ifndef NS_SERVICE_H__ |
#define NS_SERVICE_H__ |
#include <ipc/ipc.h> |
extern int service_init(void); |
extern void process_pending_conn(void); |
extern int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call); |
extern void connect_to_service(ipcarg_t service, ipc_call_t *call, |
ipc_callid_t callid); |
#endif |
/** |
* @} |
*/ |
/branches/network/uspace/srv/ns/task.h |
---|
0,0 → 1,49 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup ns |
* @{ |
*/ |
#ifndef NS_TASK_H__ |
#define NS_TASK_H__ |
#include <ipc/ipc.h> |
#include <event.h> |
extern int task_init(void); |
extern void process_pending_wait(void); |
extern void wait_notification(wait_type_t et, task_id_t id); |
extern void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid); |
#endif |
/** |
* @} |
*/ |
/branches/network/uspace/srv/ns/Makefile |
---|
41,7 → 41,10 |
OUTPUT = ns |
SOURCES = \ |
ns.c |
ns.c \ |
service.c \ |
clonable.c \ |
task.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
/branches/network/uspace/srv/console/nameic.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Deleted: svn:mime-type |
-application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/anim_4.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Deleted: svn:mime-type |
-application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/anim_3.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Deleted: svn:mime-type |
-application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/anim_2.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Deleted: svn:mime-type |
-application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/anim_1.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Deleted: svn:mime-type |
-application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/helenos.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Deleted: svn:mime-type |
-application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/cons_selected.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Deleted: svn:mime-type |
-application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/cons_idle.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Deleted: svn:mime-type |
-application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/cons_kernel.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Deleted: svn:mime-type |
-application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/cons_has_data.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Deleted: svn:mime-type |
-application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/gcons.h |
---|
32,19 → 32,24 |
/** @file |
*/ |
#ifndef _GCONS_H_ |
#define _GCONS_H_ |
#ifndef GCONS_H_ |
#define GCONS_H_ |
#include <sys/types.h> |
void gcons_init(int phone); |
void gcons_redraw_console(void); |
void gcons_change_console(int consnum); |
void gcons_notify_char(int consnum); |
void gcons_change_console(size_t index); |
void gcons_notify_char(size_t index); |
void gcons_in_kernel(void); |
void gcons_notify_connect(int consnum); |
void gcons_notify_disconnect(int consnum); |
void gcons_mouse_move(int dx, int dy); |
int gcons_mouse_btn(int state); |
void gcons_notify_connect(size_t index); |
void gcons_notify_disconnect(size_t index); |
void gcons_mouse_move(ssize_t dx, ssize_t dy); |
int gcons_mouse_btn(bool state); |
#endif |
/** @} |
/branches/network/uspace/srv/console/screenbuffer.h |
---|
32,11 → 32,12 |
/** @file |
*/ |
#ifndef __SCREENBUFFER_H__ |
#define __SCREENBUFFER_H__ |
#ifndef SCREENBUFFER_H__ |
#define SCREENBUFFER_H__ |
#include <stdint.h> |
#include <sys/types.h> |
#include <bool.h> |
#define DEFAULT_FOREGROUND 0x0 /**< default console foreground color */ |
#define DEFAULT_BACKGROUND 0xf0f0f0 /**< default console background color */ |
72,67 → 73,85 |
/** One field on screen. It contain one character and its attributes. */ |
typedef struct { |
wchar_t character; /**< Character itself */ |
attrs_t attrs; /**< Character`s attributes */ |
attrs_t attrs; /**< Character attributes */ |
} keyfield_t; |
/** Structure for buffering state of one virtual console. |
*/ |
typedef struct { |
keyfield_t *buffer; /**< Screen content - characters and their attributes. Used as a circular buffer. */ |
unsigned int size_x, size_y; /**< Number of columns and rows */ |
unsigned int position_x, position_y; /**< Coordinates of last printed character for determining cursor position */ |
keyfield_t *buffer; /**< Screen content - characters and |
their attributes (used as a circular buffer) */ |
size_t size_x; /**< Number of columns */ |
size_t size_y; /**< Number of rows */ |
/** Coordinates of last printed character for determining cursor position */ |
size_t position_x; |
size_t position_y; |
attrs_t attrs; /**< Current attributes. */ |
unsigned int top_line; /**< Points to buffer[][] line that will be printed at screen as the first line */ |
unsigned char is_cursor_visible; /**< Cursor state - default is visible */ |
size_t top_line; /**< Points to buffer[][] line that will |
be printed at screen as the first line */ |
bool is_cursor_visible; /**< Cursor state - default is visible */ |
} screenbuffer_t; |
/** Returns keyfield for position on screen. Screenbuffer->buffer is cyclic buffer so we must couted in index of the topmost line. |
* @param scr screenbuffer |
* @param x position on screen |
* @param y position on screen |
* @return keyfield structure with character and its attributes on x,y |
/** Returns keyfield for position on screen |
* |
* Screenbuffer->buffer is cyclic buffer so we |
* must couted in index of the topmost line. |
* |
* @param scr Screenbuffer |
* @param x Position on screen |
* @param y Position on screen |
* |
* @return Keyfield structure with character and its attributes on x, y |
* |
*/ |
static inline keyfield_t *get_field_at(screenbuffer_t *scr, unsigned int x, unsigned int y) |
static inline keyfield_t *get_field_at(screenbuffer_t *scr, size_t x, size_t y) |
{ |
return scr->buffer + x + ((y + scr->top_line) % scr->size_y) * scr->size_x; |
} |
/** Compares two sets of attributes. |
* @param s1 first style |
* @param s2 second style |
* @return nonzero on equality |
* |
* @param s1 First style |
* @param s2 Second style |
* |
* @return Nonzero on equality |
* |
*/ |
static inline int attrs_same(attrs_t a1, attrs_t a2) |
{ |
if (a1.t != a2.t) return 0; |
if (a1.t != a2.t) |
return 0; |
switch (a1.t) { |
case at_style: return a1.a.s.style == a2.a.s.style; |
case at_idx: return a1.a.i.fg_color == a2.a.i.fg_color && |
a1.a.i.bg_color == a2.a.i.bg_color && |
a1.a.i.flags == a2.a.i.flags; |
case at_rgb: return a1.a.r.fg_color == a2.a.r.fg_color && |
a1.a.r.bg_color == a2.a.r.bg_color; |
case at_style: |
return (a1.a.s.style == a2.a.s.style); |
case at_idx: |
return (a1.a.i.fg_color == a2.a.i.fg_color) |
&& (a1.a.i.bg_color == a2.a.i.bg_color) |
&& (a1.a.i.flags == a2.a.i.flags); |
case at_rgb: |
return (a1.a.r.fg_color == a2.a.r.fg_color) |
&& (a1.a.r.bg_color == a2.a.r.bg_color); |
} |
} |
void screenbuffer_putchar(screenbuffer_t *scr, wchar_t c); |
screenbuffer_t *screenbuffer_init(screenbuffer_t *scr, int size_x, int size_y); |
screenbuffer_t *screenbuffer_init(screenbuffer_t *scr, size_t size_x, size_t size_y); |
void screenbuffer_clear(screenbuffer_t *scr); |
void screenbuffer_clear_line(screenbuffer_t *scr, unsigned int line); |
void screenbuffer_clear_line(screenbuffer_t *scr, size_t line); |
void screenbuffer_copy_buffer(screenbuffer_t *scr, keyfield_t *dest); |
void screenbuffer_goto(screenbuffer_t *scr, unsigned int x, unsigned int y); |
void screenbuffer_set_style(screenbuffer_t *scr, int style); |
void screenbuffer_set_color(screenbuffer_t *scr, unsigned int fg_color, |
unsigned int bg_color, unsigned int attr); |
void screenbuffer_set_rgb_color(screenbuffer_t *scr, unsigned int fg_color, |
unsigned int bg_color); |
void screenbuffer_goto(screenbuffer_t *scr, size_t x, size_t y); |
void screenbuffer_set_style(screenbuffer_t *scr, uint8_t style); |
void screenbuffer_set_color(screenbuffer_t *scr, uint8_t fg_color, |
uint8_t bg_color, uint8_t attr); |
void screenbuffer_set_rgb_color(screenbuffer_t *scr, uint32_t fg_color, |
uint32_t bg_color); |
#endif |
/** @} |
*/ |
/branches/network/uspace/srv/console/console.c |
---|
36,96 → 36,71 |
#include <fb.h> |
#include <ipc/ipc.h> |
#include <kbd.h> |
#include <kbd/keycode.h> |
#include <io/keycode.h> |
#include <ipc/fb.h> |
#include <ipc/services.h> |
#include <errno.h> |
#include <key_buffer.h> |
#include <keybuffer.h> |
#include <ipc/console.h> |
#include <unistd.h> |
#include <async.h> |
#include <libadt/fifo.h> |
#include <screenbuffer.h> |
#include <adt/fifo.h> |
#include <sys/mman.h> |
#include <stdio.h> |
#include <string.h> |
#include <sysinfo.h> |
#include <event.h> |
#include <devmap.h> |
#include <fibril_sync.h> |
#include "console.h" |
#include "gcons.h" |
#include "screenbuffer.h" |
#define MAX_KEYREQUESTS_BUFFERED 32 |
#define NAME "console" |
/** Index of currently used virtual console. |
*/ |
int active_console = 0; |
int prev_console = 0; |
#define MAX_DEVICE_NAME 32 |
/** Phone to the keyboard driver. */ |
static int kbd_phone; |
/** Information about framebuffer */ |
struct { |
int phone; /**< Framebuffer phone */ |
ipcarg_t cols; /**< Framebuffer columns */ |
ipcarg_t rows; /**< Framebuffer rows */ |
ipcarg_t cols; /**< Framebuffer columns */ |
} fb_info; |
typedef struct { |
size_t index; /**< Console index */ |
size_t refcount; /**< Connection reference count */ |
dev_handle_t dev_handle; /**< Device handle */ |
keybuffer_t keybuffer; /**< Buffer for incoming keys. */ |
/** Buffer for unsatisfied request for keys. */ |
FIFO_CREATE_STATIC(keyrequests, ipc_callid_t, |
MAX_KEYREQUESTS_BUFFERED); |
int keyrequest_counter; /**< Number of requests in buffer. */ |
int client_phone; /**< Phone to connected client. */ |
int used; /**< 1 if this virtual console is |
* connected to some client.*/ |
screenbuffer_t screenbuffer; /**< Screenbuffer for saving screen |
* contents and related settings. */ |
} connection_t; |
screenbuffer_t scr; /**< Screenbuffer for saving screen |
contents and related settings. */ |
} console_t; |
static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual |
* consoles */ |
static keyfield_t *interbuffer = NULL; /**< Pointer to memory shared |
* with framebufer used for |
* faster virtual console |
* switching */ |
/** Array of data for virtual consoles */ |
static console_t consoles[CONSOLE_COUNT]; |
static console_t *active_console = &consoles[0]; |
static console_t *prev_console = &consoles[0]; |
static console_t *kernel_console = &consoles[KERNEL_CONSOLE]; |
/** Pointer to memory shared with framebufer used for |
faster virtual console switching */ |
static keyfield_t *interbuffer = NULL; |
/** Information on row-span yet unsent to FB driver. */ |
struct { |
int row; /**< Row where the span lies. */ |
int col; /**< Leftmost column of the span. */ |
int n; /**< Width of the span. */ |
size_t col; /**< Leftmost column of the span. */ |
size_t row; /**< Row where the span lies. */ |
size_t cnt; /**< Width of the span. */ |
} fb_pending; |
/** Size of cwrite_buf. */ |
#define CWRITE_BUF_SIZE 256 |
static FIBRIL_MUTEX_INITIALIZE(input_mutex); |
static FIBRIL_CONDVAR_INITIALIZE(input_cv); |
/** Buffer for receiving data via the CONSOLE_WRITE call from the client. */ |
static char cwrite_buf[CWRITE_BUF_SIZE]; |
static void fb_putchar(wchar_t c, int row, int col); |
/** Find unused virtual console. |
* |
*/ |
static int find_free_connection(void) |
{ |
int i; |
for (i = 0; i < CONSOLE_COUNT; i++) { |
if (!connections[i].used) |
return i; |
} |
return -1; |
} |
static void clrscr(void) |
{ |
async_msg_0(fb_info.phone, FB_CLEAR); |
} |
static void curs_visibility(bool visible) |
{ |
async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible); |
136,11 → 111,16 |
ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false); |
} |
static void curs_goto(int row, int col) |
static void curs_goto(size_t x, size_t y) |
{ |
async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col); |
async_msg_2(fb_info.phone, FB_CURSOR_GOTO, x, y); |
} |
static void screen_clear(void) |
{ |
async_msg_0(fb_info.phone, FB_CLEAR); |
} |
static void screen_yield(void) |
{ |
ipc_call_sync_0_0(fb_info.phone, FB_SCREEN_YIELD); |
151,6 → 131,16 |
ipc_call_sync_0_0(fb_info.phone, FB_SCREEN_RECLAIM); |
} |
static void kbd_yield(void) |
{ |
ipc_call_sync_0_0(kbd_phone, KBD_YIELD); |
} |
static void kbd_reclaim(void) |
{ |
ipc_call_sync_0_0(kbd_phone, KBD_RECLAIM); |
} |
static void set_style(int style) |
{ |
async_msg_1(fb_info.phone, FB_SET_STYLE, style); |
172,12 → 162,10 |
case at_style: |
set_style(attrs->a.s.style); |
break; |
case at_idx: |
set_color(attrs->a.i.fg_color, attrs->a.i.bg_color, |
attrs->a.i.flags); |
break; |
case at_rgb: |
set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color); |
break; |
185,57 → 173,31 |
} |
/** Send an area of screenbuffer to the FB driver. */ |
static void fb_update_area(connection_t *conn, int x, int y, int w, int h) |
static void fb_update_area(console_t *cons, ipcarg_t x0, ipcarg_t y0, ipcarg_t width, ipcarg_t height) |
{ |
int i, j; |
int rc; |
attrs_t *attrs; |
keyfield_t *field; |
if (interbuffer) { |
ipcarg_t x; |
ipcarg_t y; |
if (interbuffer) { |
for (j = 0; j < h; j++) { |
for (i = 0; i < w; i++) { |
interbuffer[i + j * w] = |
*get_field_at(&conn->screenbuffer, |
x + i, y + j); |
for (y = 0; y < height; y++) { |
for (x = 0; x < width; x++) { |
interbuffer[y * width + x] = |
*get_field_at(&cons->scr, x0 + x, y0 + y); |
} |
} |
rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA, |
x, y, w, h); |
} else { |
rc = ENOTSUP; |
async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA, |
x0, y0, width, height); |
} |
if (rc != 0) { |
/* |
attrs = &conn->screenbuffer.attrs; |
for (j = 0; j < h; j++) { |
for (i = 0; i < w; i++) { |
field = get_field_at(&conn->screenbuffer, |
x + i, y + j); |
if (!attrs_same(*attrs, field->attrs)) |
set_attrs(&field->attrs); |
attrs = &field->attrs; |
fb_putchar(field->character, y + j, x + i); |
} |
}*/ |
} |
} |
/** Flush pending cells to FB. */ |
static void fb_pending_flush(void) |
{ |
screenbuffer_t *scr; |
scr = &(connections[active_console].screenbuffer); |
if (fb_pending.n > 0) { |
fb_update_area(&connections[active_console], fb_pending.col, |
fb_pending.row, fb_pending.n, 1); |
fb_pending.n = 0; |
if (fb_pending.cnt > 0) { |
fb_update_area(active_console, fb_pending.col, |
fb_pending.row, fb_pending.cnt, 1); |
fb_pending.cnt = 0; |
} |
} |
243,167 → 205,162 |
* |
* This adds the cell to the pending rowspan if possible. Otherwise |
* the old span is flushed first. |
* |
*/ |
static void cell_mark_changed(int row, int col) |
static void cell_mark_changed(size_t col, size_t row) |
{ |
if (fb_pending.n != 0) { |
if (row != fb_pending.row || |
col != fb_pending.col + fb_pending.n) { |
if (fb_pending.cnt != 0) { |
if ((col != fb_pending.col + fb_pending.cnt) |
|| (row != fb_pending.row)) { |
fb_pending_flush(); |
} |
} |
if (fb_pending.n == 0) { |
if (fb_pending.cnt == 0) { |
fb_pending.col = col; |
fb_pending.row = row; |
fb_pending.col = col; |
} |
++fb_pending.n; |
fb_pending.cnt++; |
} |
/** Print a character to the active VC with buffering. */ |
static void fb_putchar(wchar_t c, int row, int col) |
static void fb_putchar(wchar_t c, ipcarg_t col, ipcarg_t row) |
{ |
async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col); |
async_msg_3(fb_info.phone, FB_PUTCHAR, c, col, row); |
} |
/** Process a character from the client (TTY emulation). */ |
static void write_char(int console, wchar_t ch) |
static void write_char(console_t *cons, wchar_t ch) |
{ |
bool flush_cursor = false; |
screenbuffer_t *scr = &(connections[console].screenbuffer); |
switch (ch) { |
case '\n': |
fb_pending_flush(); |
flush_cursor = true; |
scr->position_y++; |
scr->position_x = 0; |
cons->scr.position_y++; |
cons->scr.position_x = 0; |
break; |
case '\r': |
break; |
case '\t': |
scr->position_x += 8; |
scr->position_x -= scr->position_x % 8; |
cons->scr.position_x += 8; |
cons->scr.position_x -= cons->scr.position_x % 8; |
break; |
case '\b': |
if (scr->position_x == 0) |
if (cons->scr.position_x == 0) |
break; |
scr->position_x--; |
if (console == active_console) |
cell_mark_changed(scr->position_y, scr->position_x); |
screenbuffer_putchar(scr, ' '); |
cons->scr.position_x--; |
if (cons == active_console) |
cell_mark_changed(cons->scr.position_x, cons->scr.position_y); |
screenbuffer_putchar(&cons->scr, ' '); |
break; |
default: |
if (console == active_console) |
cell_mark_changed(scr->position_y, scr->position_x); |
if (cons == active_console) |
cell_mark_changed(cons->scr.position_x, cons->scr.position_y); |
screenbuffer_putchar(scr, ch); |
scr->position_x++; |
screenbuffer_putchar(&cons->scr, ch); |
cons->scr.position_x++; |
} |
if (scr->position_x >= scr->size_x) { |
if (cons->scr.position_x >= cons->scr.size_x) { |
flush_cursor = true; |
scr->position_y++; |
cons->scr.position_y++; |
} |
if (scr->position_y >= scr->size_y) { |
if (cons->scr.position_y >= cons->scr.size_y) { |
fb_pending_flush(); |
scr->position_y = scr->size_y - 1; |
screenbuffer_clear_line(scr, scr->top_line); |
scr->top_line = (scr->top_line + 1) % scr->size_y; |
if (console == active_console) |
cons->scr.position_y = cons->scr.size_y - 1; |
screenbuffer_clear_line(&cons->scr, cons->scr.top_line); |
cons->scr.top_line = (cons->scr.top_line + 1) % cons->scr.size_y; |
if (cons == active_console) |
async_msg_1(fb_info.phone, FB_SCROLL, 1); |
} |
scr->position_x = scr->position_x % scr->size_x; |
if (console == active_console && flush_cursor) |
curs_goto(scr->position_y, scr->position_x); |
if (cons == active_console && flush_cursor) |
curs_goto(cons->scr.position_x, cons->scr.position_y); |
cons->scr.position_x = cons->scr.position_x % cons->scr.size_x; |
} |
/** Switch to new console */ |
static void change_console(int newcons) |
static void change_console(console_t *cons) |
{ |
connection_t *conn; |
int i, j, rc; |
keyfield_t *field; |
attrs_t *attrs; |
if (newcons == active_console) |
if (cons == active_console) |
return; |
fb_pending_flush(); |
if (newcons == KERNEL_CONSOLE) { |
if (cons == kernel_console) { |
async_serialize_start(); |
curs_hide_sync(); |
gcons_in_kernel(); |
screen_yield(); |
kbd_yield(); |
async_serialize_end(); |
if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) { |
prev_console = active_console; |
active_console = KERNEL_CONSOLE; |
active_console = kernel_console; |
} else |
newcons = active_console; |
cons = active_console; |
} |
if (newcons != KERNEL_CONSOLE) { |
if (cons != kernel_console) { |
size_t x; |
size_t y; |
int rc = 0; |
async_serialize_start(); |
if (active_console == KERNEL_CONSOLE) { |
if (active_console == kernel_console) { |
screen_reclaim(); |
kbd_reclaim(); |
gcons_redraw_console(); |
} |
active_console = newcons; |
gcons_change_console(newcons); |
conn = &connections[active_console]; |
active_console = cons; |
gcons_change_console(cons->index); |
set_attrs(&conn->screenbuffer.attrs); |
set_attrs(&cons->scr.attrs); |
curs_visibility(false); |
if (interbuffer) { |
for (j = 0; j < conn->screenbuffer.size_y; j++) { |
for (i = 0; i < conn->screenbuffer.size_x; i++) { |
unsigned int size_x; |
size_x = conn->screenbuffer.size_x; |
interbuffer[j * size_x + i] = |
*get_field_at(&conn->screenbuffer, i, j); |
for (y = 0; y < cons->scr.size_y; y++) { |
for (x = 0; x < cons->scr.size_x; x++) { |
interbuffer[y * cons->scr.size_x + x] = |
*get_field_at(&cons->scr, x, y); |
} |
} |
/* This call can preempt, but we are already at the end */ |
rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA, |
0, 0, conn->screenbuffer.size_x, |
conn->screenbuffer.size_y); |
0, 0, cons->scr.size_x, |
cons->scr.size_y); |
} |
if ((!interbuffer) || (rc != 0)) { |
set_attrs(&conn->screenbuffer.attrs); |
clrscr(); |
attrs = &conn->screenbuffer.attrs; |
set_attrs(&cons->scr.attrs); |
screen_clear(); |
for (j = 0; j < conn->screenbuffer.size_y; j++) |
for (i = 0; i < conn->screenbuffer.size_x; i++) { |
field = get_field_at(&conn->screenbuffer, i, j); |
if (!attrs_same(*attrs, field->attrs)) |
for (y = 0; y < cons->scr.size_y; y++) |
for (x = 0; x < cons->scr.size_x; x++) { |
keyfield_t *field = get_field_at(&cons->scr, x, y); |
if (!attrs_same(cons->scr.attrs, field->attrs)) |
set_attrs(&field->attrs); |
attrs = &field->attrs; |
cons->scr.attrs = field->attrs; |
if ((field->character == ' ') && |
(attrs_same(field->attrs, |
conn->screenbuffer.attrs))) |
(attrs_same(field->attrs, cons->scr.attrs))) |
continue; |
fb_putchar(field->character, j, i); |
fb_putchar(field->character, x, y); |
} |
} |
curs_goto(conn->screenbuffer.position_y, |
conn->screenbuffer.position_x); |
curs_visibility(conn->screenbuffer.is_cursor_visible); |
curs_goto(cons->scr.position_x, cons->scr.position_y); |
curs_visibility(cons->scr.is_cursor_visible); |
async_serialize_end(); |
} |
412,31 → 369,19 |
/** Handler for keyboard */ |
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall) |
{ |
ipc_callid_t callid; |
/* Ignore parameters, the connection is already opened */ |
while (true) { |
ipc_call_t call; |
ipc_callid_t callid = async_get_call(&call); |
int retval; |
kbd_event_t ev; |
connection_t *conn; |
int newcon; |
console_event_t ev; |
/* Ignore parameters, the connection is alread opened */ |
while (1) { |
callid = async_get_call(&call); |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
/* TODO: Handle hangup */ |
return; |
case KBD_MS_LEFT: |
newcon = gcons_mouse_btn(IPC_GET_ARG1(call)); |
if (newcon != -1) |
change_console(newcon); |
retval = 0; |
break; |
case KBD_MS_MOVE: |
gcons_mouse_move(IPC_GET_ARG1(call), |
IPC_GET_ARG2(call)); |
retval = 0; |
break; |
case KBD_EVENT: |
/* Got event from keyboard driver. */ |
retval = 0; |
445,31 → 390,20 |
ev.mods = IPC_GET_ARG3(call); |
ev.c = IPC_GET_ARG4(call); |
/* switch to another virtual console */ |
conn = &connections[active_console]; |
if ((ev.key >= KC_F1) && (ev.key < KC_F1 + |
CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) { |
if (ev.key == KC_F12) |
change_console(KERNEL_CONSOLE); |
if (ev.key == KC_F1 + KERNEL_CONSOLE) |
change_console(kernel_console); |
else |
change_console(ev.key - KC_F1); |
change_console(&consoles[ev.key - KC_F1]); |
break; |
} |
/* if client is awaiting key, send it */ |
if (conn->keyrequest_counter > 0) { |
conn->keyrequest_counter--; |
ipc_answer_4(fifo_pop(conn->keyrequests), EOK, |
ev.type, ev.key, ev.mods, ev.c); |
fibril_mutex_lock(&input_mutex); |
keybuffer_push(&active_console->keybuffer, &ev); |
fibril_condvar_broadcast(&input_cv); |
fibril_mutex_unlock(&input_mutex); |
break; |
} |
keybuffer_push(&conn->keybuffer, &ev); |
retval = 0; |
break; |
default: |
retval = ENOENT; |
} |
477,62 → 411,132 |
} |
} |
/** Handle CONSOLE_WRITE call. */ |
static void cons_write(int consnum, ipc_callid_t rid, ipc_call_t *request) |
static void cons_write(console_t *cons, ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_callid_t callid; |
size_t size; |
wchar_t ch; |
size_t off; |
if (!ipc_data_write_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
if (size > CWRITE_BUF_SIZE) |
size = CWRITE_BUF_SIZE; |
char *buf = (char *) malloc(size); |
if (buf == NULL) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
(void) ipc_data_write_finalize(callid, cwrite_buf, size); |
(void) ipc_data_write_finalize(callid, buf, size); |
off = 0; |
async_serialize_start(); |
size_t off = 0; |
while (off < size) { |
ch = str_decode(cwrite_buf, &off, size); |
write_char(consnum, ch); |
wchar_t ch = str_decode(buf, &off, size); |
write_char(cons, ch); |
} |
gcons_notify_char(consnum); |
async_serialize_end(); |
gcons_notify_char(cons->index); |
ipc_answer_1(rid, EOK, size); |
free(buf); |
} |
static void cons_read(console_t *cons, ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_read_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
char *buf = (char *) malloc(size); |
if (buf == NULL) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
size_t pos = 0; |
console_event_t ev; |
fibril_mutex_lock(&input_mutex); |
recheck: |
while ((keybuffer_pop(&cons->keybuffer, &ev)) && (pos < size)) { |
if (ev.type == KEY_PRESS) { |
buf[pos] = ev.c; |
pos++; |
} |
} |
if (pos == size) { |
(void) ipc_data_read_finalize(callid, buf, size); |
ipc_answer_1(rid, EOK, size); |
free(buf); |
} else { |
fibril_condvar_wait(&input_cv, &input_mutex); |
goto recheck; |
} |
fibril_mutex_unlock(&input_mutex); |
} |
static void cons_get_event(console_t *cons, ipc_callid_t rid, ipc_call_t *request) |
{ |
console_event_t ev; |
fibril_mutex_lock(&input_mutex); |
recheck: |
if (keybuffer_pop(&cons->keybuffer, &ev)) { |
ipc_answer_4(rid, EOK, ev.type, ev.key, ev.mods, ev.c); |
} else { |
fibril_condvar_wait(&input_cv, &input_mutex); |
goto recheck; |
} |
fibril_mutex_unlock(&input_mutex); |
} |
/** Default thread for new connections */ |
static void client_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
ipc_callid_t callid; |
ipc_call_t call; |
int consnum; |
ipcarg_t arg1, arg2, arg3, arg4; |
connection_t *conn; |
screenbuffer_t *scr; |
console_t *cons = NULL; |
if ((consnum = find_free_connection()) == -1) { |
ipc_answer_0(iid, ELIMIT); |
size_t i; |
for (i = 0; i < CONSOLE_COUNT; i++) { |
if (i == KERNEL_CONSOLE) |
continue; |
if (consoles[i].dev_handle == (dev_handle_t) IPC_GET_ARG1(*icall)) { |
cons = &consoles[i]; |
break; |
} |
} |
if (cons == NULL) { |
ipc_answer_0(iid, ENOENT); |
return; |
} |
conn = &connections[consnum]; |
conn->used = 1; |
ipc_callid_t callid; |
ipc_call_t call; |
ipcarg_t arg1; |
ipcarg_t arg2; |
ipcarg_t arg3; |
async_serialize_start(); |
gcons_notify_connect(consnum); |
conn->client_phone = IPC_GET_ARG5(*icall); |
screenbuffer_clear(&conn->screenbuffer); |
if (consnum == active_console) |
clrscr(); |
if (cons->refcount == 0) |
gcons_notify_connect(cons->index); |
cons->refcount++; |
/* Accept the connection */ |
ipc_answer_0(iid, EOK); |
while (1) { |
while (true) { |
async_serialize_end(); |
callid = async_get_call(&call); |
async_serialize_start(); |
540,62 → 544,55 |
arg1 = 0; |
arg2 = 0; |
arg3 = 0; |
arg4 = 0; |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
gcons_notify_disconnect(consnum); |
cons->refcount--; |
if (cons->refcount == 0) |
gcons_notify_disconnect(cons->index); |
return; |
case VFS_READ: |
async_serialize_end(); |
cons_read(cons, callid, &call); |
async_serialize_start(); |
continue; |
case VFS_WRITE: |
async_serialize_end(); |
cons_write(cons, callid, &call); |
async_serialize_start(); |
continue; |
case VFS_SYNC: |
fb_pending_flush(); |
if (cons == active_console) { |
async_req_0_0(fb_info.phone, FB_FLUSH); |
/* Answer all pending requests */ |
while (conn->keyrequest_counter > 0) { |
conn->keyrequest_counter--; |
ipc_answer_0(fifo_pop(conn->keyrequests), |
ENOENT); |
break; |
curs_goto(cons->scr.position_x, cons->scr.position_y); |
} |
conn->used = 0; |
return; |
case CONSOLE_PUTCHAR: |
write_char(consnum, IPC_GET_ARG1(call)); |
gcons_notify_char(consnum); |
break; |
case CONSOLE_WRITE: |
cons_write(consnum, callid, &call); |
continue; |
case CONSOLE_CLEAR: |
/* Send message to fb */ |
if (consnum == active_console) { |
if (cons == active_console) |
async_msg_0(fb_info.phone, FB_CLEAR); |
} |
screenbuffer_clear(&conn->screenbuffer); |
screenbuffer_clear(&cons->scr); |
break; |
case CONSOLE_GOTO: |
screenbuffer_goto(&conn->screenbuffer, |
IPC_GET_ARG2(call), IPC_GET_ARG1(call)); |
if (consnum == active_console) |
screenbuffer_goto(&cons->scr, |
IPC_GET_ARG1(call), IPC_GET_ARG2(call)); |
if (cons == active_console) |
curs_goto(IPC_GET_ARG1(call), |
IPC_GET_ARG2(call)); |
break; |
case CONSOLE_GETSIZE: |
arg1 = fb_info.rows; |
arg2 = fb_info.cols; |
case CONSOLE_GET_SIZE: |
arg1 = fb_info.cols; |
arg2 = fb_info.rows; |
break; |
case CONSOLE_FLUSH: |
fb_pending_flush(); |
if (consnum == active_console) { |
async_req_0_0(fb_info.phone, FB_FLUSH); |
scr = &(connections[consnum].screenbuffer); |
curs_goto(scr->position_y, scr->position_x); |
} |
break; |
case CONSOLE_SET_STYLE: |
fb_pending_flush(); |
arg1 = IPC_GET_ARG1(call); |
screenbuffer_set_style(&conn->screenbuffer, arg1); |
if (consnum == active_console) |
screenbuffer_set_style(&cons->scr, arg1); |
if (cons == active_console) |
set_style(arg1); |
break; |
case CONSOLE_SET_COLOR: |
603,9 → 600,8 |
arg1 = IPC_GET_ARG1(call); |
arg2 = IPC_GET_ARG2(call); |
arg3 = IPC_GET_ARG3(call); |
screenbuffer_set_color(&conn->screenbuffer, arg1, |
arg2, arg3); |
if (consnum == active_console) |
screenbuffer_set_color(&cons->scr, arg1, arg2, arg3); |
if (cons == active_console) |
set_color(arg1, arg2, arg3); |
break; |
case CONSOLE_SET_RGB_COLOR: |
612,46 → 608,27 |
fb_pending_flush(); |
arg1 = IPC_GET_ARG1(call); |
arg2 = IPC_GET_ARG2(call); |
screenbuffer_set_rgb_color(&conn->screenbuffer, arg1, |
arg2); |
if (consnum == active_console) |
screenbuffer_set_rgb_color(&cons->scr, arg1, arg2); |
if (cons == active_console) |
set_rgb_color(arg1, arg2); |
break; |
case CONSOLE_CURSOR_VISIBILITY: |
fb_pending_flush(); |
arg1 = IPC_GET_ARG1(call); |
conn->screenbuffer.is_cursor_visible = arg1; |
if (consnum == active_console) |
cons->scr.is_cursor_visible = arg1; |
if (cons == active_console) |
curs_visibility(arg1); |
break; |
case CONSOLE_GETKEY: |
if (keybuffer_empty(&conn->keybuffer)) { |
/* buffer is empty -> store request */ |
if (conn->keyrequest_counter < |
MAX_KEYREQUESTS_BUFFERED) { |
fifo_push(conn->keyrequests, callid); |
conn->keyrequest_counter++; |
} else { |
/* |
* No key available and too many |
* requests => fail. |
*/ |
ipc_answer_0(callid, ELIMIT); |
} |
case CONSOLE_GET_EVENT: |
async_serialize_end(); |
cons_get_event(cons, callid, &call); |
async_serialize_start(); |
continue; |
} |
kbd_event_t ev; |
keybuffer_pop(&conn->keybuffer, &ev); |
arg1 = ev.type; |
arg2 = ev.key; |
arg3 = ev.mods; |
arg4 = ev.c; |
break; |
case CONSOLE_KCON_ENABLE: |
change_console(KERNEL_CONSOLE); |
change_console(kernel_console); |
break; |
} |
ipc_answer_4(callid, EOK, arg1, arg2, arg3, arg4); |
ipc_answer_3(callid, EOK, arg1, arg2, arg3); |
} |
} |
660,27 → 637,19 |
change_console(prev_console); |
} |
int main(int argc, char *argv[]) |
static bool console_init(void) |
{ |
printf(NAME ": HelenOS Console service\n"); |
ipcarg_t phonehash; |
int kbd_phone; |
size_t ib_size; |
int i; |
async_set_client_connection(client_connection); |
/* Connect to keyboard driver */ |
kbd_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_KEYBOARD, 0, 0); |
if (kbd_phone < 0) { |
printf(NAME ": Failed to connect to keyboard service\n"); |
return -1; |
return false; |
} |
ipcarg_t phonehash; |
if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) { |
printf(NAME ": Failed to create callback from keyboard service\n"); |
return -1; |
return false; |
} |
async_new_connection(phonehash, 0, NULL, keyboard_events); |
692,47 → 661,27 |
return -1; |
} |
/* Disable kernel output to the console */ |
__SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE); |
/* Register driver */ |
int rc = devmap_driver_register(NAME, client_connection); |
if (rc < 0) { |
printf(NAME ": Unable to register driver (%d)\n", rc); |
return false; |
} |
/* Initialize gcons */ |
gcons_init(fb_info.phone); |
/* Synchronize, the gcons can have something in queue */ |
/* Synchronize, the gcons could put something in queue */ |
async_req_0_0(fb_info.phone, FB_FLUSH); |
async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.cols, &fb_info.rows); |
async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows, |
&fb_info.cols); |
set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND); |
clrscr(); |
/* Init virtual consoles */ |
for (i = 0; i < CONSOLE_COUNT; i++) { |
connections[i].used = 0; |
keybuffer_init(&connections[i].keybuffer); |
connections[i].keyrequests.head = 0; |
connections[i].keyrequests.tail = 0; |
connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED; |
connections[i].keyrequest_counter = 0; |
if (screenbuffer_init(&connections[i].screenbuffer, |
fb_info.cols, fb_info.rows) == NULL) { |
/* FIXME: handle error */ |
return -1; |
} |
} |
connections[KERNEL_CONSOLE].used = 1; |
/* Set up shared memory buffer. */ |
ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows; |
size_t ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows; |
interbuffer = as_get_mappable_page(ib_size); |
fb_pending.n = 0; |
if (as_area_create(interbuffer, ib_size, AS_AREA_READ | |
AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) { |
AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) |
interbuffer = NULL; |
} |
if (interbuffer) { |
if (ipc_share_out_start(fb_info.phone, interbuffer, |
742,14 → 691,45 |
} |
} |
fb_pending.cnt = 0; |
/* Inititalize consoles */ |
size_t i; |
for (i = 0; i < CONSOLE_COUNT; i++) { |
if (i != KERNEL_CONSOLE) { |
if (screenbuffer_init(&consoles[i].scr, |
fb_info.cols, fb_info.rows) == NULL) { |
printf(NAME ": Unable to allocate screen buffer %u\n", i); |
return false; |
} |
screenbuffer_clear(&consoles[i].scr); |
keybuffer_init(&consoles[i].keybuffer); |
consoles[i].index = i; |
consoles[i].refcount = 0; |
char vc[MAX_DEVICE_NAME]; |
snprintf(vc, MAX_DEVICE_NAME, "vc%u", i); |
if (devmap_device_register(vc, &consoles[i].dev_handle) != EOK) { |
devmap_hangup_phone(DEVMAP_DRIVER); |
printf(NAME ": Unable to register device %s\n", vc); |
return false; |
} |
} |
} |
/* Disable kernel output to the console */ |
__SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE); |
/* Initialize the screen */ |
async_serialize_start(); |
gcons_redraw_console(); |
set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND); |
screen_clear(); |
curs_goto(0, 0); |
curs_visibility( |
connections[active_console].screenbuffer.is_cursor_visible); |
curs_visibility(active_console->scr.is_cursor_visible); |
async_serialize_end(); |
/* Register at NS */ |
if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) |
return -1; |
/* Receive kernel notifications */ |
if (event_subscribe(EVENT_KCONSOLE, 0) != EOK) |
printf(NAME ": Error registering kconsole notifications\n"); |
756,8 → 736,17 |
async_set_interrupt_received(interrupt_received); |
// FIXME: avoid connectiong to itself, keep using klog |
// printf(NAME ": Accepting connections\n"); |
return true; |
} |
int main(int argc, char *argv[]) |
{ |
printf(NAME ": HelenOS Console service\n"); |
if (!console_init()) |
return -1; |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
return 0; |
/branches/network/uspace/srv/console/gfx/anim_1.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/gfx/anim_2.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/gfx/anim_3.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/gfx/cons_has_data.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/gfx/anim_4.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/gfx/cons_selected.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/gfx/nameic.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/gfx/helenos.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/gfx/cons_idle.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/gfx/cons_kernel.ppm |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/branches/network/uspace/srv/console/console.h |
---|
35,10 → 35,9 |
#ifndef __CONSOLE_H__ |
#define __CONSOLE_H__ |
#define CONSOLE_COUNT 12 |
#define KERNEL_CONSOLE 11 |
#define CONSOLE_COUNT 12 |
#endif |
/** @} |
/branches/network/uspace/srv/console/screenbuffer.c |
---|
33,38 → 33,44 |
*/ |
#include <screenbuffer.h> |
#include <console/style.h> |
#include <io/style.h> |
#include <malloc.h> |
#include <unistd.h> |
/** Store one character to screenbuffer. Its position is determined by |
* scr->position_x and scr->position_y. |
/** Store one character to screenbuffer. |
* |
* @param scr screenbuffer |
* @param c stored character |
* Its position is determined by scr->position_x |
* and scr->position_y. |
* |
* @param scr Screenbuffer |
* @param c Stored character |
* |
*/ |
void screenbuffer_putchar(screenbuffer_t *scr, wchar_t ch) |
{ |
keyfield_t *field; |
keyfield_t *field = |
get_field_at(scr, scr->position_x, scr->position_y); |
field = get_field_at(scr, scr->position_x, scr->position_y); |
field->character = ch; |
field->attrs = scr->attrs; |
} |
/** Initilize screenbuffer. Allocate space for screen content in accordance to given size. |
* @param scr initialized screenbuffer |
* @param size_x width in characters |
* @param size_y height in characters |
* @return pointer to screenbuffer (same as scr parameter) or NULL |
/** Initilize screenbuffer. |
* |
* Allocate space for screen content in accordance to given size. |
* |
* @param scr Initialized screenbuffer |
* @param size_x Width in characters |
* @param size_y Height in characters |
* |
* @return Pointer to screenbuffer (same as scr parameter) or NULL |
* |
*/ |
screenbuffer_t *screenbuffer_init(screenbuffer_t *scr, int size_x, int size_y) |
screenbuffer_t *screenbuffer_init(screenbuffer_t *scr, size_t size_x, size_t size_y) |
{ |
scr->buffer = (keyfield_t *) malloc(sizeof(keyfield_t) * size_x * size_y); |
if (!scr->buffer) { |
if (!scr->buffer) |
return NULL; |
} |
scr->size_x = size_x; |
scr->size_y = size_y; |
78,11 → 84,13 |
} |
/** Clear screenbuffer. |
* @param scr screenbuffer |
* |
* @param scr Screenbuffer |
* |
*/ |
void screenbuffer_clear(screenbuffer_t *scr) |
{ |
unsigned int i; |
size_t i; |
for (i = 0; i < (scr->size_x * scr->size_y); i++) { |
scr->buffer[i].character = ' '; |
90,43 → 98,48 |
} |
scr->top_line = 0; |
scr->position_x = 0; |
scr->position_y = 0; |
scr->position_x = 0; |
} |
/** Clear one buffer line. |
* |
* @param scr |
* @param line One buffer line (not a screen line!) |
* |
*/ |
void screenbuffer_clear_line(screenbuffer_t *scr, unsigned int line) |
void screenbuffer_clear_line(screenbuffer_t *scr, size_t line) |
{ |
unsigned int i; |
size_t x; |
for (i = 0; i < scr->size_x; i++) { |
scr->buffer[i + line * scr->size_x].character = ' '; |
scr->buffer[i + line * scr->size_x].attrs = scr->attrs; |
for (x = 0; x < scr->size_x; x++) { |
scr->buffer[x + line * scr->size_x].character = ' '; |
scr->buffer[x + line * scr->size_x].attrs = scr->attrs; |
} |
} |
/** Copy content buffer from screenbuffer to given memory. |
* @param scr source screenbuffer |
* @param dest destination |
* |
* @param scr Source screenbuffer |
* @param dest Destination |
* |
*/ |
void screenbuffer_copy_buffer(screenbuffer_t *scr, keyfield_t *dest) |
{ |
unsigned int i; |
size_t i; |
for (i = 0; i < scr->size_x * scr->size_y; i++) { |
for (i = 0; i < (scr->size_x * scr->size_y); i++) |
dest[i] = scr->buffer[i]; |
} |
} |
/** Set new cursor position in screenbuffer. |
* |
* @param scr |
* @param x |
* @param y |
* |
*/ |
void screenbuffer_goto(screenbuffer_t *scr, unsigned int x, unsigned int y) |
void screenbuffer_goto(screenbuffer_t *scr, size_t x, size_t y) |
{ |
scr->position_x = x % scr->size_x; |
scr->position_y = y % scr->size_y; |
133,11 → 146,13 |
} |
/** Set new style. |
* |
* @param scr |
* @param fg_color |
* @param bg_color |
* |
*/ |
void screenbuffer_set_style(screenbuffer_t *scr, int style) |
void screenbuffer_set_style(screenbuffer_t *scr, uint8_t style) |
{ |
scr->attrs.t = at_style; |
scr->attrs.a.s.style = style; |
144,11 → 159,13 |
} |
/** Set new color. |
* |
* @param scr |
* @param fg_color |
* @param bg_color |
* |
*/ |
void screenbuffer_set_color(screenbuffer_t *scr, unsigned int fg_color, unsigned int bg_color, unsigned int flags) |
void screenbuffer_set_color(screenbuffer_t *scr, uint8_t fg_color, uint8_t bg_color, uint8_t flags) |
{ |
scr->attrs.t = at_idx; |
scr->attrs.a.i.fg_color = fg_color; |
157,11 → 174,13 |
} |
/** Set new RGB color. |
* |
* @param scr |
* @param fg_color |
* @param bg_color |
* |
*/ |
void screenbuffer_set_rgb_color(screenbuffer_t *scr, unsigned int fg_color, unsigned int bg_color) |
void screenbuffer_set_rgb_color(screenbuffer_t *scr, uint32_t fg_color, uint32_t bg_color) |
{ |
scr->attrs.t = at_rgb; |
scr->attrs.a.r.fg_color = fg_color; |
168,6 → 187,5 |
scr->attrs.a.r.bg_color = bg_color; |
} |
/** @} |
*/ |
/branches/network/uspace/srv/console/gcons.c |
---|
39,6 → 39,7 |
#include <sys/mman.h> |
#include <string.h> |
#include <align.h> |
#include <bool.h> |
#include "console.h" |
#include "gcons.h" |
54,8 → 55,9 |
#define MAIN_COLOR 0xffffff |
static int use_gcons = 0; |
static ipcarg_t xres,yres; |
static bool use_gcons = false; |
static ipcarg_t xres; |
static ipcarg_t yres; |
enum butstate { |
CONS_DISCONNECTED = 0, |
77,8 → 79,15 |
static int ic_pixmaps[CONS_LAST] = {-1, -1, -1, -1, -1, -1}; |
static int animation = -1; |
static int active_console = 0; |
static size_t active_console = 0; |
size_t mouse_x; |
size_t mouse_y; |
bool btn_pressed; |
size_t btn_x; |
size_t btn_y; |
static void vp_switch(int vp) |
{ |
async_msg_1(fbphone, FB_VIEWPORT_SWITCH, vp); |
85,8 → 94,7 |
} |
/** Create view port */ |
static int vp_create(unsigned int x, unsigned int y, unsigned int width, |
unsigned int height) |
static int vp_create(size_t x, size_t y, size_t width, size_t height) |
{ |
return async_req_2_0(fbphone, FB_VIEWPORT_CREATE, (x << 16) | y, |
(width << 16) | height); |
97,48 → 105,52 |
async_msg_0(fbphone, FB_CLEAR); |
} |
static void set_rgb_color(int fgcolor, int bgcolor) |
static void set_rgb_color(uint32_t fgcolor, uint32_t bgcolor) |
{ |
async_msg_2(fbphone, FB_SET_RGB_COLOR, fgcolor, bgcolor); |
} |
/** Transparent putchar */ |
static void tran_putch(char c, int row, int col) |
static void tran_putch(wchar_t ch, size_t col, size_t row) |
{ |
async_msg_3(fbphone, FB_PUTCHAR, c, row, col); |
async_msg_3(fbphone, FB_PUTCHAR, ch, col, row); |
} |
/** Redraw the button showing state of a given console */ |
static void redraw_state(int consnum) |
static void redraw_state(size_t index) |
{ |
char data[5]; |
int i; |
enum butstate state = console_state[consnum]; |
vp_switch(cstatus_vp[index]); |
vp_switch(cstatus_vp[consnum]); |
enum butstate state = console_state[index]; |
if (ic_pixmaps[state] != -1) |
async_msg_2(fbphone, FB_VP_DRAW_PIXMAP, cstatus_vp[consnum], |
async_msg_2(fbphone, FB_VP_DRAW_PIXMAP, cstatus_vp[index], |
ic_pixmaps[state]); |
if (state != CONS_DISCONNECTED && state != CONS_KERNEL && |
state != CONS_DISCONNECTED_SEL) { |
snprintf(data, 5, "%d", consnum + 1); |
for (i = 0; data[i]; i++) |
tran_putch(data[i], 1, 2 + i); |
if ((state != CONS_DISCONNECTED) && (state != CONS_KERNEL) |
&& (state != CONS_DISCONNECTED_SEL)) { |
char data[5]; |
snprintf(data, 5, "%u", index + 1); |
size_t i; |
for (i = 0; data[i] != 0; i++) |
tran_putch(data[i], 2 + i, 1); |
} |
} |
/** Notification run on changing console (except kernel console) */ |
void gcons_change_console(int consnum) |
void gcons_change_console(size_t index) |
{ |
int i; |
if (!use_gcons) |
return; |
if (active_console == KERNEL_CONSOLE) { |
size_t i; |
for (i = 0; i < CONSOLE_COUNT; i++) |
redraw_state(i); |
if (animation != -1) |
async_msg_1(fbphone, FB_ANIM_START, animation); |
} else { |
146,71 → 158,74 |
console_state[active_console] = CONS_DISCONNECTED; |
else |
console_state[active_console] = CONS_IDLE; |
redraw_state(active_console); |
} |
active_console = consnum; |
if (console_state[consnum] == CONS_DISCONNECTED) { |
console_state[consnum] = CONS_DISCONNECTED_SEL; |
redraw_state(consnum); |
} else |
console_state[consnum] = CONS_SELECTED; |
redraw_state(consnum); |
active_console = index; |
if ((console_state[index] == CONS_DISCONNECTED) |
|| (console_state[index] == CONS_DISCONNECTED_SEL)) |
console_state[index] = CONS_DISCONNECTED_SEL; |
else |
console_state[index] = CONS_SELECTED; |
redraw_state(index); |
vp_switch(console_vp); |
} |
/** Notification function that gets called on new output to virtual console */ |
void gcons_notify_char(int consnum) |
void gcons_notify_char(size_t index) |
{ |
if (!use_gcons) |
return; |
if (consnum == active_console || |
console_state[consnum] == CONS_HAS_DATA) |
if ((index == active_console) |
|| (console_state[index] == CONS_HAS_DATA)) |
return; |
console_state[consnum] = CONS_HAS_DATA; |
console_state[index] = CONS_HAS_DATA; |
if (active_console == KERNEL_CONSOLE) |
return; |
redraw_state(consnum); |
redraw_state(index); |
vp_switch(console_vp); |
} |
/** Notification function called on service disconnect from console */ |
void gcons_notify_disconnect(int consnum) |
void gcons_notify_disconnect(size_t index) |
{ |
if (!use_gcons) |
return; |
if (active_console == consnum) |
console_state[consnum] = CONS_DISCONNECTED_SEL; |
if (index == active_console) |
console_state[index] = CONS_DISCONNECTED_SEL; |
else |
console_state[consnum] = CONS_DISCONNECTED; |
console_state[index] = CONS_DISCONNECTED; |
if (active_console == KERNEL_CONSOLE) |
return; |
redraw_state(consnum); |
redraw_state(index); |
vp_switch(console_vp); |
} |
/** Notification function called on console connect */ |
void gcons_notify_connect(int consnum) |
void gcons_notify_connect(size_t index) |
{ |
if (!use_gcons) |
return; |
if (active_console == consnum) |
console_state[consnum] = CONS_SELECTED; |
if (index == active_console) |
console_state[index] = CONS_SELECTED; |
else |
console_state[consnum] = CONS_IDLE; |
console_state[index] = CONS_IDLE; |
if (active_console == KERNEL_CONSOLE) |
return; |
redraw_state(consnum); |
redraw_state(index); |
vp_switch(console_vp); |
} |
225,24 → 240,23 |
} |
/** Return x, where left <= x <= right && |a-x|==min(|a-x|) is smallest */ |
static inline int limit(int a,int left, int right) |
static inline int limit(size_t a, size_t left, size_t right) |
{ |
if (a < left) |
a = left; |
if (a >= right) |
a = right - 1; |
return a; |
} |
int mouse_x, mouse_y; |
int btn_pressed, btn_x, btn_y; |
/** Handle mouse move |
* |
* @param dx Delta X of mouse move |
* @param dy Delta Y of mouse move |
*/ |
void gcons_mouse_move(int dx, int dy) |
void gcons_mouse_move(ssize_t dx, ssize_t dy) |
{ |
mouse_x = limit(mouse_x + dx, 0, xres); |
mouse_y = limit(mouse_y + dy, 0, yres); |
254,7 → 268,7 |
{ |
int status_start = STATUS_START + (xres - 800) / 2; |
if (y < STATUS_TOP || y >= STATUS_TOP + STATUS_HEIGHT) |
if ((y < STATUS_TOP) || (y >= STATUS_TOP + STATUS_HEIGHT)) |
return -1; |
if (x < status_start) |
270,9 → 284,9 |
/** Handle mouse click |
* |
* @param state New state (1-pressed, 0-depressed) |
* @param state New state (true - pressed, false - depressed) |
*/ |
int gcons_mouse_btn(int state) |
int gcons_mouse_btn(bool state) |
{ |
int conbut; |
279,19 → 293,22 |
if (state) { |
conbut = gcons_find_conbut(mouse_x, mouse_y); |
if (conbut != -1) { |
btn_pressed = 1; |
btn_pressed = true; |
btn_x = mouse_x; |
btn_y = mouse_y; |
} |
return -1; |
} |
if (!state && !btn_pressed) |
if ((!state) && (!btn_pressed)) |
return -1; |
btn_pressed = 0; |
btn_pressed = false; |
conbut = gcons_find_conbut(mouse_x, mouse_y); |
if (conbut == gcons_find_conbut(btn_x, btn_y)) |
return conbut; |
return -1; |
} |
315,27 → 332,32 |
return; |
memcpy(shm, logo, size); |
/* Send area */ |
rc = async_req_1_0(fbphone, FB_PREPARE_SHM, (ipcarg_t) shm); |
if (rc) |
goto exit; |
rc = ipc_share_out_start(fbphone, shm, PROTO_READ); |
if (rc) |
goto drop; |
/* Draw logo */ |
async_msg_2(fbphone, FB_DRAW_PPM, x, y); |
drop: |
/* Drop area */ |
async_msg_0(fbphone, FB_DROP_SHM); |
exit: |
/* Remove area */ |
munmap(shm, size); |
} |
extern char _binary_helenos_ppm_start[0]; |
extern int _binary_helenos_ppm_size; |
extern char _binary_nameic_ppm_start[0]; |
extern int _binary_nameic_ppm_size; |
extern char _binary_gfx_helenos_ppm_start[0]; |
extern int _binary_gfx_helenos_ppm_size; |
extern char _binary_gfx_nameic_ppm_start[0]; |
extern int _binary_gfx_nameic_ppm_size; |
/** Redraws console graphics */ |
void gcons_redraw_console(void) |
348,13 → 370,14 |
vp_switch(0); |
set_rgb_color(MAIN_COLOR, MAIN_COLOR); |
clear(); |
draw_pixmap(_binary_helenos_ppm_start, |
(size_t) &_binary_helenos_ppm_size, xres - 66, 2); |
draw_pixmap(_binary_nameic_ppm_start, |
(size_t) &_binary_nameic_ppm_size, 5, 17); |
draw_pixmap(_binary_gfx_helenos_ppm_start, |
(size_t) &_binary_gfx_helenos_ppm_size, xres - 66, 2); |
draw_pixmap(_binary_gfx_nameic_ppm_start, |
(size_t) &_binary_gfx_nameic_ppm_size, 5, 17); |
for (i = 0; i < CONSOLE_COUNT; i++) |
redraw_state(i); |
vp_switch(console_vp); |
} |
362,9 → 385,11 |
* |
* @param data PPM data |
* @param size PPM data size |
* |
* @return Pixmap identification |
* |
*/ |
static int make_pixmap(char *data, int size) |
static int make_pixmap(char *data, size_t size) |
{ |
char *shm; |
int rc; |
377,10 → 402,12 |
return -1; |
memcpy(shm, data, size); |
/* Send area */ |
rc = async_req_1_0(fbphone, FB_PREPARE_SHM, (ipcarg_t) shm); |
if (rc) |
goto exit; |
rc = ipc_share_out_start(fbphone, shm, PROTO_READ); |
if (rc) |
goto drop; |
389,10 → 416,13 |
rc = async_req_0_0(fbphone, FB_SHM2PIXMAP); |
if (rc < 0) |
goto drop; |
pxid = rc; |
drop: |
/* Drop area */ |
async_msg_0(fbphone, FB_DROP_SHM); |
exit: |
/* Remove area */ |
munmap(shm, size); |
400,38 → 430,35 |
return pxid; |
} |
extern char _binary_anim_1_ppm_start[0]; |
extern int _binary_anim_1_ppm_size; |
extern char _binary_anim_2_ppm_start[0]; |
extern int _binary_anim_2_ppm_size; |
extern char _binary_anim_3_ppm_start[0]; |
extern int _binary_anim_3_ppm_size; |
extern char _binary_anim_4_ppm_start[0]; |
extern int _binary_anim_4_ppm_size; |
extern char _binary_gfx_anim_1_ppm_start[0]; |
extern int _binary_gfx_anim_1_ppm_size; |
extern char _binary_gfx_anim_2_ppm_start[0]; |
extern int _binary_gfx_anim_2_ppm_size; |
extern char _binary_gfx_anim_3_ppm_start[0]; |
extern int _binary_gfx_anim_3_ppm_size; |
extern char _binary_gfx_anim_4_ppm_start[0]; |
extern int _binary_gfx_anim_4_ppm_size; |
static void make_anim(void) |
{ |
int an; |
int pm; |
an = async_req_1_0(fbphone, FB_ANIM_CREATE, cstatus_vp[KERNEL_CONSOLE]); |
int an = async_req_1_0(fbphone, FB_ANIM_CREATE, cstatus_vp[KERNEL_CONSOLE]); |
if (an < 0) |
return; |
pm = make_pixmap(_binary_anim_1_ppm_start, |
(int) &_binary_anim_1_ppm_size); |
int pm = make_pixmap(_binary_gfx_anim_1_ppm_start, |
(int) &_binary_gfx_anim_1_ppm_size); |
async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm); |
pm = make_pixmap(_binary_anim_2_ppm_start, |
(int) &_binary_anim_2_ppm_size); |
pm = make_pixmap(_binary_gfx_anim_2_ppm_start, |
(int) &_binary_gfx_anim_2_ppm_size); |
async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm); |
pm = make_pixmap(_binary_anim_3_ppm_start, |
(int) &_binary_anim_3_ppm_size); |
pm = make_pixmap(_binary_gfx_anim_3_ppm_start, |
(int) &_binary_gfx_anim_3_ppm_size); |
async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm); |
pm = make_pixmap(_binary_anim_4_ppm_start, |
(int) &_binary_anim_4_ppm_size); |
pm = make_pixmap(_binary_gfx_anim_4_ppm_start, |
(int) &_binary_gfx_anim_4_ppm_size); |
async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm); |
async_msg_1(fbphone, FB_ANIM_START, an); |
439,25 → 466,21 |
animation = an; |
} |
extern char _binary_cons_selected_ppm_start[0]; |
extern int _binary_cons_selected_ppm_size; |
extern char _binary_cons_idle_ppm_start[0]; |
extern int _binary_cons_idle_ppm_size; |
extern char _binary_cons_has_data_ppm_start[0]; |
extern int _binary_cons_has_data_ppm_size; |
extern char _binary_cons_kernel_ppm_start[0]; |
extern int _binary_cons_kernel_ppm_size; |
extern char _binary_gfx_cons_selected_ppm_start[0]; |
extern int _binary_gfx_cons_selected_ppm_size; |
extern char _binary_gfx_cons_idle_ppm_start[0]; |
extern int _binary_gfx_cons_idle_ppm_size; |
extern char _binary_gfx_cons_has_data_ppm_start[0]; |
extern int _binary_gfx_cons_has_data_ppm_size; |
extern char _binary_gfx_cons_kernel_ppm_start[0]; |
extern int _binary_gfx_cons_kernel_ppm_size; |
/** Initialize nice graphical console environment */ |
void gcons_init(int phone) |
{ |
int rc; |
int i; |
int status_start = STATUS_START; |
fbphone = phone; |
rc = async_req_0_2(phone, FB_GET_RESOLUTION, &xres, &yres); |
int rc = async_req_0_2(phone, FB_GET_RESOLUTION, &xres, &yres); |
if (rc) |
return; |
464,22 → 487,27 |
if ((xres < 800) || (yres < 600)) |
return; |
/* create console viewport */ |
/* Create console viewport */ |
/* Align width & height to character size */ |
console_vp = vp_create(CONSOLE_MARGIN, CONSOLE_TOP, |
ALIGN_DOWN(xres - 2 * CONSOLE_MARGIN, 8), |
ALIGN_DOWN(yres - (CONSOLE_TOP + CONSOLE_MARGIN), 16)); |
if (console_vp < 0) |
return; |
/* Create status buttons */ |
status_start += (xres - 800) / 2; |
size_t status_start = STATUS_START + (xres - 800) / 2; |
size_t i; |
for (i = 0; i < CONSOLE_COUNT; i++) { |
cstatus_vp[i] = vp_create(status_start + CONSOLE_MARGIN + |
i * (STATUS_WIDTH + STATUS_SPACE), STATUS_TOP, |
STATUS_WIDTH, STATUS_HEIGHT); |
if (cstatus_vp[i] < 0) |
return; |
vp_switch(cstatus_vp[i]); |
set_rgb_color(0x202020, 0xffffff); |
} |
486,26 → 514,29 |
/* Initialize icons */ |
ic_pixmaps[CONS_SELECTED] = |
make_pixmap(_binary_cons_selected_ppm_start, |
(int) &_binary_cons_selected_ppm_size); |
ic_pixmaps[CONS_IDLE] = make_pixmap(_binary_cons_idle_ppm_start, |
(int) &_binary_cons_idle_ppm_size); |
make_pixmap(_binary_gfx_cons_selected_ppm_start, |
(size_t) &_binary_gfx_cons_selected_ppm_size); |
ic_pixmaps[CONS_IDLE] = |
make_pixmap(_binary_gfx_cons_idle_ppm_start, |
(size_t) &_binary_gfx_cons_idle_ppm_size); |
ic_pixmaps[CONS_HAS_DATA] = |
make_pixmap(_binary_cons_has_data_ppm_start, |
(int) &_binary_cons_has_data_ppm_size); |
make_pixmap(_binary_gfx_cons_has_data_ppm_start, |
(size_t) &_binary_gfx_cons_has_data_ppm_size); |
ic_pixmaps[CONS_DISCONNECTED] = |
make_pixmap(_binary_cons_idle_ppm_start, |
(int) &_binary_cons_idle_ppm_size); |
ic_pixmaps[CONS_KERNEL] = make_pixmap(_binary_cons_kernel_ppm_start, |
(int) &_binary_cons_kernel_ppm_size); |
make_pixmap(_binary_gfx_cons_idle_ppm_start, |
(size_t) &_binary_gfx_cons_idle_ppm_size); |
ic_pixmaps[CONS_KERNEL] = |
make_pixmap(_binary_gfx_cons_kernel_ppm_start, |
(size_t) &_binary_gfx_cons_kernel_ppm_size); |
ic_pixmaps[CONS_DISCONNECTED_SEL] = ic_pixmaps[CONS_SELECTED]; |
make_anim(); |
use_gcons = 1; |
use_gcons = true; |
console_state[0] = CONS_DISCONNECTED_SEL; |
console_state[KERNEL_CONSOLE] = CONS_KERNEL; |
gcons_redraw_console(); |
vp_switch(console_vp); |
} |
/** @} |
/branches/network/uspace/srv/console/Makefile |
---|
42,23 → 42,29 |
# |
OUTPUT = console |
GENERIC_SOURCES = \ |
console.c \ |
screenbuffer.c \ |
../kbd/generic/key_buffer.c \ |
../kbd/generic/keybuffer.c \ |
gcons.c |
IMAGES = helenos.ppm nameic.ppm cons_selected.ppm cons_idle.ppm \ |
cons_has_data.ppm cons_kernel.ppm anim_1.ppm anim_2.ppm anim_3.ppm \ |
anim_4.ppm |
IMAGES = \ |
gfx/helenos.ppm \ |
gfx/nameic.ppm \ |
gfx/cons_selected.ppm \ |
gfx/cons_idle.ppm \ |
gfx/cons_has_data.ppm \ |
gfx/cons_kernel.ppm \ |
gfx/anim_1.ppm \ |
gfx/anim_2.ppm \ |
gfx/anim_3.ppm \ |
gfx/anim_4.ppm |
ARCH_SOURCES = |
GENERIC_OBJECTS := $(addsuffix .o,$(basename $(GENERIC_SOURCES))) \ |
$(addsuffix .o,$(basename $(IMAGES))) |
ARCH_OBJECTS := $(addsuffix .o,$(basename $(ARCH_SOURCES))) |
OBJECTS := $(GENERIC_OBJECTS) $(ARCH_OBJECTS) |
OBJECTS := $(GENERIC_OBJECTS) |
.PHONY: all clean depend disasm |
/branches/network/uspace/srv/bd/rd/rd.c |
---|
0,0 → 1,241 |
/* |
* Copyright (c) 2007 Michal Konopa |
* Copyright (c) 2007 Martin Jelen |
* Copyright (c) 2007 Peter Majer |
* Copyright (c) 2007 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup rd |
* @{ |
*/ |
/** |
* @file rd.c |
* @brief Initial RAM disk for HelenOS. |
*/ |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <ipc/ns.h> |
#include <sysinfo.h> |
#include <as.h> |
#include <ddi.h> |
#include <align.h> |
#include <bool.h> |
#include <errno.h> |
#include <async.h> |
#include <align.h> |
#include <async.h> |
#include <fibril_sync.h> |
#include <stdio.h> |
#include <devmap.h> |
#include <ipc/bd.h> |
#define NAME "rd" |
/** Pointer to the ramdisk's image. */ |
static void *rd_addr; |
/** Size of the ramdisk. */ |
static size_t rd_size; |
/** |
* This rwlock protects the ramdisk's data. |
* If we were to serve multiple requests (read + write or several writes) |
* concurrently (i.e. from two or more threads), each read and write needs to be |
* protected by this rwlock. |
*/ |
fibril_rwlock_t rd_lock; |
/** Handle one connection to ramdisk. |
* |
* @param iid Hash of the request that opened the connection. |
* @param icall Call data of the request that opened the connection. |
*/ |
static void rd_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
ipc_callid_t callid; |
ipc_call_t call; |
int retval; |
void *fs_va = NULL; |
off_t offset; |
size_t block_size; |
size_t maxblock_size; |
/* |
* Answer the first IPC_M_CONNECT_ME_TO call. |
*/ |
ipc_answer_0(iid, EOK); |
/* |
* Now we wait for the client to send us its communication as_area. |
*/ |
int flags; |
if (ipc_share_out_receive(&callid, &maxblock_size, &flags)) { |
fs_va = as_get_mappable_page(maxblock_size); |
if (fs_va) { |
(void) ipc_share_out_finalize(callid, fs_va); |
} else { |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
} else { |
/* |
* The client doesn't speak the same protocol. |
* At this point we can't handle protocol variations. |
* Close the connection. |
*/ |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
while (true) { |
callid = async_get_call(&call); |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
/* |
* The other side has hung up. |
* Answer the message and exit the fibril. |
*/ |
ipc_answer_0(callid, EOK); |
return; |
case BD_READ_BLOCK: |
offset = IPC_GET_ARG1(call); |
block_size = IPC_GET_ARG2(call); |
if (block_size > maxblock_size) { |
/* |
* Maximum block size exceeded. |
*/ |
retval = ELIMIT; |
break; |
} |
if (offset * block_size > rd_size - block_size) { |
/* |
* Reading past the end of the device. |
*/ |
retval = ELIMIT; |
break; |
} |
fibril_rwlock_read_lock(&rd_lock); |
memcpy(fs_va, rd_addr + offset * block_size, block_size); |
fibril_rwlock_read_unlock(&rd_lock); |
retval = EOK; |
break; |
case BD_WRITE_BLOCK: |
offset = IPC_GET_ARG1(call); |
block_size = IPC_GET_ARG2(call); |
if (block_size > maxblock_size) { |
/* |
* Maximum block size exceeded. |
*/ |
retval = ELIMIT; |
break; |
} |
if (offset * block_size > rd_size - block_size) { |
/* |
* Writing past the end of the device. |
*/ |
retval = ELIMIT; |
break; |
} |
fibril_rwlock_write_lock(&rd_lock); |
memcpy(rd_addr + offset * block_size, fs_va, block_size); |
fibril_rwlock_write_unlock(&rd_lock); |
retval = EOK; |
break; |
default: |
/* |
* The client doesn't speak the same protocol. |
* Instead of closing the connection, we just ignore |
* the call. This can be useful if the client uses a |
* newer version of the protocol. |
*/ |
retval = EINVAL; |
break; |
} |
ipc_answer_0(callid, retval); |
} |
} |
/** Prepare the ramdisk image for operation. */ |
static bool rd_init(void) |
{ |
rd_size = sysinfo_value("rd.size"); |
void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical"); |
if (rd_size == 0) { |
printf(NAME ": No RAM disk found\n"); |
return false; |
} |
rd_addr = as_get_mappable_page(rd_size); |
int flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE; |
int retval = physmem_map(rd_ph_addr, rd_addr, |
ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags); |
if (retval < 0) { |
printf(NAME ": Error mapping RAM disk\n"); |
return false; |
} |
printf(NAME ": Found RAM disk at %p, %d bytes\n", rd_ph_addr, rd_size); |
int rc = devmap_driver_register(NAME, rd_connection); |
if (rc < 0) { |
printf(NAME ": Unable to register driver (%d)\n", rc); |
return false; |
} |
dev_handle_t dev_handle; |
if (devmap_device_register("initrd", &dev_handle) != EOK) { |
devmap_hangup_phone(DEVMAP_DRIVER); |
printf(NAME ": Unable to register device\n"); |
return false; |
} |
fibril_rwlock_initialize(&rd_lock); |
return true; |
} |
int main(int argc, char **argv) |
{ |
printf(NAME ": HelenOS RAM disk server\n"); |
if (!rd_init()) |
return -1; |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
/* Never reached */ |
return 0; |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/bd/rd/Makefile |
---|
0,0 → 1,76 |
# |
# Copyright (c) 2006 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
LIBS = $(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = rd |
SOURCES = \ |
rd.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |
/branches/network/uspace/srv/bd/file_bd/file_bd.c |
---|
0,0 → 1,213 |
/* |
* Copyright (c) 2009 Jiri Svoboda |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup bd |
* @{ |
*/ |
/** |
* @file |
* @brief File-backed block device driver |
* |
* Allows accessing a file as a block device. Useful for, e.g., mounting |
* a disk image. |
*/ |
#include <stdio.h> |
#include <unistd.h> |
#include <ipc/ipc.h> |
#include <ipc/bd.h> |
#include <async.h> |
#include <as.h> |
#include <fibril_sync.h> |
#include <devmap.h> |
#include <sys/types.h> |
#include <errno.h> |
#include <bool.h> |
#define NAME "file_bd" |
static size_t comm_size; |
static FILE *img; |
static dev_handle_t dev_handle; |
static fibril_mutex_t dev_lock; |
static int file_bd_init(const char *fname); |
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
static int file_bd_read(off_t blk_idx, size_t size, void *buf); |
static int file_bd_write(off_t blk_idx, size_t size, void *buf); |
int main(int argc, char **argv) |
{ |
int rc; |
printf(NAME ": File-backed block device driver\n"); |
if (argc != 3) { |
printf("Expected two arguments (image name, device name).\n"); |
return -1; |
} |
if (file_bd_init(argv[1]) != EOK) |
return -1; |
rc = devmap_device_register(argv[2], &dev_handle); |
if (rc != EOK) { |
devmap_hangup_phone(DEVMAP_DRIVER); |
printf(NAME ": Unable to register device %s.\n", |
argv[2]); |
return rc; |
} |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
/* Not reached */ |
return 0; |
} |
static int file_bd_init(const char *fname) |
{ |
int rc; |
rc = devmap_driver_register(NAME, file_bd_connection); |
if (rc < 0) { |
printf(NAME ": Unable to register driver.\n"); |
return rc; |
} |
img = fopen(fname, "rb+"); |
if (img == NULL) |
return EINVAL; |
fibril_mutex_initialize(&dev_lock); |
return EOK; |
} |
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
void *fs_va = NULL; |
ipc_callid_t callid; |
ipc_call_t call; |
ipcarg_t method; |
int flags; |
int retval; |
off_t idx; |
size_t size; |
/* Answer the IPC_M_CONNECT_ME_TO call. */ |
ipc_answer_0(iid, EOK); |
if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
fs_va = as_get_mappable_page(comm_size); |
if (fs_va == NULL) { |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
(void) ipc_share_out_finalize(callid, fs_va); |
while (1) { |
callid = async_get_call(&call); |
method = IPC_GET_METHOD(call); |
switch (method) { |
case IPC_M_PHONE_HUNGUP: |
/* The other side has hung up. */ |
ipc_answer_0(callid, EOK); |
return; |
case BD_READ_BLOCK: |
case BD_WRITE_BLOCK: |
idx = IPC_GET_ARG1(call); |
size = IPC_GET_ARG2(call); |
if (size > comm_size) { |
retval = EINVAL; |
break; |
} |
if (method == BD_READ_BLOCK) |
retval = file_bd_read(idx, size, fs_va); |
else |
retval = file_bd_write(idx, size, fs_va); |
break; |
default: |
retval = EINVAL; |
break; |
} |
ipc_answer_0(callid, retval); |
} |
} |
static int file_bd_read(off_t blk_idx, size_t size, void *buf) |
{ |
size_t n_rd; |
fibril_mutex_lock(&dev_lock); |
fseek(img, blk_idx * size, SEEK_SET); |
n_rd = fread(buf, 1, size, img); |
if (ferror(img)) { |
fibril_mutex_unlock(&dev_lock); |
return EIO; /* Read error */ |
} |
fibril_mutex_unlock(&dev_lock); |
if (n_rd < size) |
return EINVAL; /* Read beyond end of disk */ |
return EOK; |
} |
static int file_bd_write(off_t blk_idx, size_t size, void *buf) |
{ |
size_t n_wr; |
fibril_mutex_lock(&dev_lock); |
fseek(img, blk_idx * size, SEEK_SET); |
n_wr = fread(buf, 1, size, img); |
if (ferror(img) || n_wr < size) { |
fibril_mutex_unlock(&dev_lock); |
return EIO; /* Write error */ |
} |
fibril_mutex_unlock(&dev_lock); |
return EOK; |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/bd/file_bd/Makefile |
---|
0,0 → 1,76 |
# |
# Copyright (c) 2006 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
LIBS = $(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = file_bd |
SOURCES = \ |
file_bd.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |
/branches/network/uspace/srv/bd/gxe_bd/gxe_bd.c |
---|
0,0 → 1,308 |
/* |
* Copyright (c) 2009 Jiri Svoboda |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup bd |
* @{ |
*/ |
/** |
* @file |
* @brief GXemul disk driver |
*/ |
#include <stdio.h> |
#include <libarch/ddi.h> |
#include <ddi.h> |
#include <ipc/ipc.h> |
#include <ipc/bd.h> |
#include <async.h> |
#include <as.h> |
#include <fibril_sync.h> |
#include <devmap.h> |
#include <sys/types.h> |
#include <errno.h> |
#define NAME "gxe_bd" |
enum { |
CTL_READ_START = 0, |
CTL_WRITE_START = 1, |
}; |
enum { |
STATUS_FAILURE = 0 |
}; |
enum { |
MAX_DISKS = 2 |
}; |
typedef struct { |
uint32_t offset_lo; |
uint32_t pad0; |
uint32_t offset_hi; |
uint32_t pad1; |
uint32_t disk_id; |
uint32_t pad2[3]; |
uint32_t control; |
uint32_t pad3[3]; |
uint32_t status; |
uint32_t pad4[3]; |
uint8_t pad5[0x3fc0]; |
uint8_t buffer[512]; |
} gxe_bd_t; |
static const size_t block_size = 512; |
static size_t comm_size; |
static uintptr_t dev_physical = 0x13000000; |
static gxe_bd_t *dev; |
static dev_handle_t dev_handle[MAX_DISKS]; |
static fibril_mutex_t dev_lock[MAX_DISKS]; |
static int gxe_bd_init(void); |
static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
static int gx_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size, |
void *buf); |
static int gxe_bd_read_block(int disk_id, uint64_t offset, size_t size, |
void *buf); |
static int gxe_bd_write_block(int disk_id, uint64_t offset, size_t size, |
const void *buf); |
int main(int argc, char **argv) |
{ |
printf(NAME ": GXemul disk driver\n"); |
if (gxe_bd_init() != EOK) |
return -1; |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
/* Not reached */ |
return 0; |
} |
static int gxe_bd_init(void) |
{ |
void *vaddr; |
int rc, i; |
char name[16]; |
rc = devmap_driver_register(NAME, gxe_bd_connection); |
if (rc < 0) { |
printf(NAME ": Unable to register driver.\n"); |
return rc; |
} |
rc = pio_enable((void *) dev_physical, sizeof(gxe_bd_t), &vaddr); |
if (rc != EOK) { |
printf(NAME ": Could not initialize device I/O space.\n"); |
return rc; |
} |
dev = vaddr; |
for (i = 0; i < MAX_DISKS; i++) { |
snprintf(name, 16, "disk%d", i); |
rc = devmap_device_register(name, &dev_handle[i]); |
if (rc != EOK) { |
devmap_hangup_phone(DEVMAP_DRIVER); |
printf(NAME ": Unable to register device %s.\n", |
name); |
return rc; |
} |
fibril_mutex_initialize(&dev_lock[i]); |
} |
return EOK; |
} |
static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
void *fs_va = NULL; |
ipc_callid_t callid; |
ipc_call_t call; |
ipcarg_t method; |
dev_handle_t dh; |
int flags; |
int retval; |
off_t idx; |
size_t size; |
int disk_id, i; |
/* Get the device handle. */ |
dh = IPC_GET_ARG1(*icall); |
/* Determine which disk device is the client connecting to. */ |
disk_id = -1; |
for (i = 0; i < MAX_DISKS; i++) |
if (dev_handle[i] == dh) |
disk_id = i; |
if (disk_id < 0) { |
ipc_answer_0(iid, EINVAL); |
return; |
} |
/* Answer the IPC_M_CONNECT_ME_TO call. */ |
ipc_answer_0(iid, EOK); |
if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
fs_va = as_get_mappable_page(comm_size); |
if (fs_va == NULL) { |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
(void) ipc_share_out_finalize(callid, fs_va); |
while (1) { |
callid = async_get_call(&call); |
method = IPC_GET_METHOD(call); |
switch (method) { |
case IPC_M_PHONE_HUNGUP: |
/* The other side has hung up. */ |
ipc_answer_0(callid, EOK); |
return; |
case BD_READ_BLOCK: |
case BD_WRITE_BLOCK: |
idx = IPC_GET_ARG1(call); |
size = IPC_GET_ARG2(call); |
if (size > comm_size) { |
retval = EINVAL; |
break; |
} |
retval = gx_bd_rdwr(disk_id, method, idx * size, |
size, fs_va); |
break; |
default: |
retval = EINVAL; |
break; |
} |
ipc_answer_0(callid, retval); |
} |
} |
static int gx_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size, |
void *buf) |
{ |
int rc; |
size_t now; |
while (size > 0) { |
now = size < block_size ? size : block_size; |
if (method == BD_READ_BLOCK) |
rc = gxe_bd_read_block(disk_id, offset, now, buf); |
else |
rc = gxe_bd_write_block(disk_id, offset, now, buf); |
if (rc != EOK) |
return rc; |
buf += block_size; |
offset += block_size; |
if (size > block_size) |
size -= block_size; |
else |
size = 0; |
} |
return EOK; |
} |
static int gxe_bd_read_block(int disk_id, uint64_t offset, size_t size, |
void *buf) |
{ |
uint32_t status; |
size_t i; |
uint32_t w; |
fibril_mutex_lock(&dev_lock[disk_id]); |
pio_write_32(&dev->offset_lo, (uint32_t) offset); |
pio_write_32(&dev->offset_hi, offset >> 32); |
pio_write_32(&dev->disk_id, disk_id); |
pio_write_32(&dev->control, CTL_READ_START); |
status = pio_read_32(&dev->status); |
if (status == STATUS_FAILURE) { |
fibril_mutex_unlock(&dev_lock[disk_id]); |
return EIO; |
} |
for (i = 0; i < size; i++) { |
((uint8_t *) buf)[i] = w = pio_read_8(&dev->buffer[i]); |
} |
fibril_mutex_unlock(&dev_lock[disk_id]); |
return EOK; |
} |
static int gxe_bd_write_block(int disk_id, uint64_t offset, size_t size, |
const void *buf) |
{ |
uint32_t status; |
size_t i; |
for (i = 0; i < size; i++) { |
pio_write_8(&dev->buffer[i], ((const uint8_t *) buf)[i]); |
} |
fibril_mutex_lock(&dev_lock[disk_id]); |
pio_write_32(&dev->offset_lo, (uint32_t) offset); |
pio_write_32(&dev->offset_hi, offset >> 32); |
pio_write_32(&dev->disk_id, disk_id); |
pio_write_32(&dev->control, CTL_WRITE_START); |
status = pio_read_32(&dev->status); |
if (status == STATUS_FAILURE) { |
fibril_mutex_unlock(&dev_lock[disk_id]); |
return EIO; |
} |
fibril_mutex_unlock(&dev_lock[disk_id]); |
return EOK; |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/bd/gxe_bd/Makefile |
---|
0,0 → 1,76 |
# |
# Copyright (c) 2006 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
LIBS = $(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = gxe_bd |
SOURCES = \ |
gxe_bd.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |
/branches/network/uspace/srv/bd/ata_bd/ata_bd.h |
---|
0,0 → 1,151 |
/* |
* Copyright (c) 2009 Jiri Svoboda |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup bd |
* @{ |
*/ |
/** @file |
*/ |
#ifndef __ATA_BD_H__ |
#define __ATA_BD_H__ |
#include <sys/types.h> |
#include <fibril_sync.h> |
enum { |
CTL_READ_START = 0, |
CTL_WRITE_START = 1, |
}; |
enum { |
STATUS_FAILURE = 0 |
}; |
enum { |
MAX_DISKS = 2 |
}; |
/** ATA Command Register Block. */ |
typedef union { |
/* Read/Write */ |
struct { |
uint16_t data_port; |
uint8_t sector_count; |
uint8_t sector_number; |
uint8_t cylinder_low; |
uint8_t cylinder_high; |
uint8_t drive_head; |
uint8_t pad_rw0; |
}; |
/* Read Only */ |
struct { |
uint8_t pad_ro0; |
uint8_t error; |
uint8_t pad_ro1[5]; |
uint8_t status; |
}; |
/* Write Only */ |
struct { |
uint8_t pad_wo0; |
uint8_t features; |
uint8_t pad_wo1[5]; |
uint8_t command; |
}; |
} ata_cmd_t; |
typedef union { |
/* Read */ |
struct { |
uint8_t pad0[6]; |
uint8_t alt_status; |
uint8_t drive_address; |
}; |
/* Write */ |
struct { |
uint8_t pad1[6]; |
uint8_t device_control; |
uint8_t pad2; |
}; |
} ata_ctl_t; |
enum devctl_bits { |
DCR_SRST = 0x04, /**< Software Reset */ |
DCR_nIEN = 0x02 /**< Interrupt Enable (negated) */ |
}; |
enum status_bits { |
SR_BSY = 0x80, /**< Busy */ |
SR_DRDY = 0x40, /**< Drive Ready */ |
SR_DWF = 0x20, /**< Drive Write Fault */ |
SR_DSC = 0x10, /**< Drive Seek Complete */ |
SR_DRQ = 0x08, /**< Data Request */ |
SR_CORR = 0x04, /**< Corrected Data */ |
SR_IDX = 0x02, /**< Index */ |
SR_ERR = 0x01 /**< Error */ |
}; |
enum drive_head_bits { |
DHR_DRV = 0x10 |
}; |
enum error_bits { |
ER_BBK = 0x80, /**< Bad Block Detected */ |
ER_UNC = 0x40, /**< Uncorrectable Data Error */ |
ER_MC = 0x20, /**< Media Changed */ |
ER_IDNF = 0x10, /**< ID Not Found */ |
ER_MCR = 0x08, /**< Media Change Request */ |
ER_ABRT = 0x04, /**< Aborted Command */ |
ER_TK0NF = 0x02, /**< Track 0 Not Found */ |
ER_AMNF = 0x01 /**< Address Mark Not Found */ |
}; |
enum ata_command { |
CMD_IDENTIFY_DRIVE = 0xEC, |
CMD_READ_SECTORS = 0x20, |
CMD_WRITE_SECTORS = 0x30 |
}; |
typedef struct { |
bool present; |
unsigned heads; |
unsigned cylinders; |
unsigned sectors; |
uint64_t blocks; |
fibril_mutex_t lock; |
dev_handle_t dev_handle; |
} disk_t; |
#endif |
/** @} |
*/ |
/branches/network/uspace/srv/bd/ata_bd/ata_bd.c |
---|
0,0 → 1,438 |
/* |
* Copyright (c) 2009 Jiri Svoboda |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup bd |
* @{ |
*/ |
/** |
* @file |
* @brief ATA disk driver |
* |
* This driver currently works only with CHS addressing and uses PIO. |
* Currently based on the (now obsolete) ANSI X3.221-1994 (ATA-1) standard. |
* At this point only reading is possible, not writing. |
* |
* The driver services a single controller which can have up to two disks |
* attached. |
*/ |
#include <stdio.h> |
#include <libarch/ddi.h> |
#include <ddi.h> |
#include <ipc/ipc.h> |
#include <ipc/bd.h> |
#include <async.h> |
#include <as.h> |
#include <fibril_sync.h> |
#include <devmap.h> |
#include <sys/types.h> |
#include <errno.h> |
#include <bool.h> |
#include "ata_bd.h" |
#define NAME "ata_bd" |
static const size_t block_size = 512; |
static size_t comm_size; |
static uintptr_t cmd_physical = 0x1f0; |
static uintptr_t ctl_physical = 0x170; |
static ata_cmd_t *cmd; |
static ata_ctl_t *ctl; |
/** Per-disk state. */ |
static disk_t disk[MAX_DISKS]; |
static int ata_bd_init(void); |
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size, |
void *buf); |
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
void *buf); |
static int ata_bd_write_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
const void *buf); |
static int drive_identify(int drive_id, disk_t *d); |
int main(int argc, char **argv) |
{ |
uint8_t status; |
char name[16]; |
int i, rc; |
int n_disks; |
printf(NAME ": ATA disk driver\n"); |
printf("I/O address 0x%x\n", cmd_physical); |
if (ata_bd_init() != EOK) |
return -1; |
/* Put drives to reset, disable interrupts. */ |
printf("Reset drives...\n"); |
pio_write_8(&ctl->device_control, DCR_SRST); |
/* FIXME: Find out how to do this properly. */ |
async_usleep(100); |
pio_write_8(&ctl->device_control, 0); |
do { |
status = pio_read_8(&cmd->status); |
} while ((status & SR_BSY) != 0); |
printf("Done\n"); |
printf("Status = 0x%x\n", pio_read_8(&cmd->status)); |
(void) drive_identify(0, &disk[0]); |
(void) drive_identify(1, &disk[1]); |
n_disks = 0; |
for (i = 0; i < MAX_DISKS; i++) { |
/* Skip unattached drives. */ |
if (disk[i].present == false) |
continue; |
snprintf(name, 16, "disk%d", i); |
rc = devmap_device_register(name, &disk[i].dev_handle); |
if (rc != EOK) { |
devmap_hangup_phone(DEVMAP_DRIVER); |
printf(NAME ": Unable to register device %s.\n", |
name); |
return rc; |
} |
++n_disks; |
} |
if (n_disks == 0) { |
printf("No disks detected.\n"); |
return -1; |
} |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
/* Not reached */ |
return 0; |
} |
static int drive_identify(int disk_id, disk_t *d) |
{ |
uint16_t data; |
uint8_t status; |
size_t i; |
printf("Identify drive %d\n", disk_id); |
pio_write_8(&cmd->drive_head, ((disk_id != 0) ? DHR_DRV : 0)); |
async_usleep(100); |
pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE); |
status = pio_read_8(&cmd->status); |
printf("Status = 0x%x\n", status); |
d->present = false; |
/* |
* Detect if drive is present. This is Qemu only! Need to |
* do the right thing to work with real drives. |
*/ |
if ((status & SR_DRDY) == 0) { |
printf("None attached.\n"); |
return ENOENT; |
} |
for (i = 0; i < block_size / 2; i++) { |
do { |
status = pio_read_8(&cmd->status); |
} while ((status & SR_DRDY) == 0); |
data = pio_read_16(&cmd->data_port); |
switch (i) { |
case 1: d->cylinders = data; break; |
case 3: d->heads = data; break; |
case 6: d->sectors = data; break; |
} |
} |
d->blocks = d->cylinders * d->heads * d->sectors; |
printf("Geometry: %u cylinders, %u heads, %u sectors\n", |
d->cylinders, d->heads, d->sectors); |
d->present = true; |
fibril_mutex_initialize(&d->lock); |
return EOK; |
} |
static int ata_bd_init(void) |
{ |
void *vaddr; |
int rc; |
rc = devmap_driver_register(NAME, ata_bd_connection); |
if (rc < 0) { |
printf(NAME ": Unable to register driver.\n"); |
return rc; |
} |
rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr); |
if (rc != EOK) { |
printf(NAME ": Could not initialize device I/O space.\n"); |
return rc; |
} |
cmd = vaddr; |
rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr); |
if (rc != EOK) { |
printf(NAME ": Could not initialize device I/O space.\n"); |
return rc; |
} |
ctl = vaddr; |
return EOK; |
} |
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
void *fs_va = NULL; |
ipc_callid_t callid; |
ipc_call_t call; |
ipcarg_t method; |
dev_handle_t dh; |
int flags; |
int retval; |
off_t idx; |
size_t size; |
int disk_id, i; |
/* Get the device handle. */ |
dh = IPC_GET_ARG1(*icall); |
/* Determine which disk device is the client connecting to. */ |
disk_id = -1; |
for (i = 0; i < MAX_DISKS; i++) |
if (disk[i].dev_handle == dh) |
disk_id = i; |
if (disk_id < 0 || disk[disk_id].present == false) { |
ipc_answer_0(iid, EINVAL); |
return; |
} |
/* Answer the IPC_M_CONNECT_ME_TO call. */ |
ipc_answer_0(iid, EOK); |
if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
fs_va = as_get_mappable_page(comm_size); |
if (fs_va == NULL) { |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
(void) ipc_share_out_finalize(callid, fs_va); |
while (1) { |
callid = async_get_call(&call); |
method = IPC_GET_METHOD(call); |
switch (method) { |
case IPC_M_PHONE_HUNGUP: |
/* The other side has hung up. */ |
ipc_answer_0(callid, EOK); |
return; |
case BD_READ_BLOCK: |
case BD_WRITE_BLOCK: |
idx = IPC_GET_ARG1(call); |
size = IPC_GET_ARG2(call); |
if (size > comm_size) { |
retval = EINVAL; |
break; |
} |
retval = ata_bd_rdwr(disk_id, method, idx, |
size, fs_va); |
break; |
default: |
retval = EINVAL; |
break; |
} |
ipc_answer_0(callid, retval); |
} |
} |
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, size_t size, |
void *buf) |
{ |
int rc; |
size_t now; |
while (size > 0) { |
now = size < block_size ? size : block_size; |
if (now != block_size) |
return EINVAL; |
if (method == BD_READ_BLOCK) |
rc = ata_bd_read_block(disk_id, blk_idx, 1, buf); |
else |
rc = ata_bd_write_block(disk_id, blk_idx, 1, buf); |
if (rc != EOK) |
return rc; |
buf += block_size; |
blk_idx++; |
if (size > block_size) |
size -= block_size; |
else |
size = 0; |
} |
return EOK; |
} |
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
void *buf) |
{ |
size_t i; |
uint16_t data; |
uint8_t status; |
uint64_t c, h, s; |
uint64_t idx; |
uint8_t drv_head; |
disk_t *d; |
d = &disk[disk_id]; |
/* Check device bounds. */ |
if (blk_idx >= d->blocks) |
return EINVAL; |
/* Compute CHS. */ |
c = blk_idx / (d->heads * d->sectors); |
idx = blk_idx % (d->heads * d->sectors); |
h = idx / d->sectors; |
s = 1 + (idx % d->sectors); |
/* New value for Drive/Head register */ |
drv_head = |
((disk_id != 0) ? DHR_DRV : 0) | |
(h & 0x0f); |
fibril_mutex_lock(&d->lock); |
/* Program a Read Sectors operation. */ |
pio_write_8(&cmd->drive_head, drv_head); |
pio_write_8(&cmd->sector_count, 1); |
pio_write_8(&cmd->sector_number, s); |
pio_write_8(&cmd->cylinder_low, c & 0xff); |
pio_write_8(&cmd->cylinder_high, c >> 16); |
pio_write_8(&cmd->command, CMD_READ_SECTORS); |
/* Read data from the disk buffer. */ |
for (i = 0; i < block_size / 2; i++) { |
do { |
status = pio_read_8(&cmd->status); |
} while ((status & SR_DRDY) == 0); |
data = pio_read_16(&cmd->data_port); |
((uint16_t *) buf)[i] = data; |
} |
fibril_mutex_unlock(&d->lock); |
return EOK; |
} |
static int ata_bd_write_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
const void *buf) |
{ |
size_t i; |
uint8_t status; |
uint64_t c, h, s; |
uint64_t idx; |
uint8_t drv_head; |
disk_t *d; |
d = &disk[disk_id]; |
/* Check device bounds. */ |
if (blk_idx >= d->blocks) |
return EINVAL; |
/* Compute CHS. */ |
c = blk_idx / (d->heads * d->sectors); |
idx = blk_idx % (d->heads * d->sectors); |
h = idx / d->sectors; |
s = 1 + (idx % d->sectors); |
/* New value for Drive/Head register */ |
drv_head = |
((disk_id != 0) ? DHR_DRV : 0) | |
(h & 0x0f); |
fibril_mutex_lock(&d->lock); |
/* Program a Read Sectors operation. */ |
pio_write_8(&cmd->drive_head, drv_head); |
pio_write_8(&cmd->sector_count, 1); |
pio_write_8(&cmd->sector_number, s); |
pio_write_8(&cmd->cylinder_low, c & 0xff); |
pio_write_8(&cmd->cylinder_high, c >> 16); |
pio_write_8(&cmd->command, CMD_WRITE_SECTORS); |
/* Write data to the disk buffer. */ |
for (i = 0; i < block_size / 2; i++) { |
do { |
status = pio_read_8(&cmd->status); |
} while ((status & SR_DRDY) == 0); |
pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]); |
} |
fibril_mutex_unlock(&d->lock); |
return EOK; |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/bd/ata_bd/Makefile |
---|
0,0 → 1,76 |
# |
# Copyright (c) 2006 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
LIBS = $(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = ata_bd |
SOURCES = \ |
ata_bd.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |
/branches/network/uspace/srv/loader/main.c |
---|
53,9 → 53,9 |
#include <ipc/services.h> |
#include <ipc/loader.h> |
#include <loader/pcb.h> |
#include <console.h> |
#include <errno.h> |
#include <async.h> |
#include <string.h> |
#include <as.h> |
#include <elf.h> |
76,6 → 76,13 |
/** Buffer holding all arguments */ |
static char *arg_buf = NULL; |
/** Number of preset files */ |
static int filc = 0; |
/** Preset files vector */ |
static fdi_node_t **filv = NULL; |
/** Buffer holding all preset files */ |
static fdi_node_t *fil_buf = NULL; |
static elf_info_t prog_info; |
static elf_info_t interp_info; |
84,7 → 91,7 |
/** Used to limit number of connections to one. */ |
static bool connected; |
static void loader_get_taskid(ipc_callid_t rid, ipc_call_t *request) |
static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_callid_t callid; |
task_id_t task_id; |
111,7 → 118,7 |
* @param rid |
* @param request |
*/ |
static void loader_set_pathname(ipc_callid_t rid, ipc_call_t *request) |
static void ldr_set_pathname(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_callid_t callid; |
size_t len; |
147,7 → 154,7 |
* @param rid |
* @param request |
*/ |
static void loader_set_args(ipc_callid_t rid, ipc_call_t *request) |
static void ldr_set_args(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_callid_t callid; |
size_t buf_size, arg_size; |
220,6 → 227,70 |
ipc_answer_0(rid, EOK); |
} |
/** Receive a call setting preset files of the program to execute. |
* |
* @param rid |
* @param request |
*/ |
static void ldr_set_files(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_callid_t callid; |
size_t buf_size; |
if (!ipc_data_write_receive(&callid, &buf_size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
if ((buf_size % sizeof(fdi_node_t)) != 0) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
if (fil_buf != NULL) { |
free(fil_buf); |
fil_buf = NULL; |
} |
if (filv != NULL) { |
free(filv); |
filv = NULL; |
} |
fil_buf = malloc(buf_size); |
if (!fil_buf) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
ipc_data_write_finalize(callid, fil_buf, buf_size); |
int count = buf_size / sizeof(fdi_node_t); |
/* Allocate filvv */ |
filv = malloc((count + 1) * sizeof(fdi_node_t *)); |
if (filv == NULL) { |
free(fil_buf); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
/* |
* Fill filv with argument pointers |
*/ |
int i; |
for (i = 0; i < count; i++) |
filv[i] = &fil_buf[i]; |
filc = count; |
filv[count] = NULL; |
ipc_answer_0(rid, EOK); |
} |
/** Load the previously selected program. |
* |
* @param rid |
226,12 → 297,12 |
* @param request |
* @return 0 on success, !0 on error. |
*/ |
static int loader_load(ipc_callid_t rid, ipc_call_t *request) |
static int ldr_load(ipc_callid_t rid, ipc_call_t *request) |
{ |
int rc; |
rc = elf_load_file(pathname, 0, &prog_info); |
if (rc < 0) { |
if (rc != EE_OK) { |
DPRINTF("Failed to load executable '%s'.\n", pathname); |
ipc_answer_0(rid, EINVAL); |
return 1; |
242,6 → 313,9 |
pcb.argc = argc; |
pcb.argv = argv; |
pcb.filc = filc; |
pcb.filv = filv; |
if (prog_info.interp == NULL) { |
/* Statically linked program */ |
is_dyn_linked = false; |
250,7 → 324,7 |
} |
rc = elf_load_file(prog_info.interp, 0, &interp_info); |
if (rc < 0) { |
if (rc != EE_OK) { |
DPRINTF("Failed to load interpreter '%s.'\n", |
prog_info.interp); |
ipc_answer_0(rid, EINVAL); |
270,7 → 344,7 |
* @param request |
* @return 0 on success, !0 on error. |
*/ |
static void loader_run(ipc_callid_t rid, ipc_call_t *request) |
static void ldr_run(ipc_callid_t rid, ipc_call_t *request) |
{ |
const char *cp; |
283,13 → 357,11 |
/* Dynamically linked program */ |
DPRINTF("Run ELF interpreter.\n"); |
DPRINTF("Entry point: 0x%lx\n", interp_info.entry); |
console_close(); |
ipc_answer_0(rid, EOK); |
elf_run(&interp_info, &pcb); |
} else { |
/* Statically linked program */ |
console_close(); |
ipc_answer_0(rid, EOK); |
elf_run(&prog_info, &pcb); |
} |
302,7 → 374,7 |
* Receive and carry out commands (of which the last one should be |
* to execute the loaded program). |
*/ |
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall) |
static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
ipc_callid_t callid; |
ipc_call_t call; |
330,19 → 402,22 |
case IPC_M_PHONE_HUNGUP: |
exit(0); |
case LOADER_GET_TASKID: |
loader_get_taskid(callid, &call); |
ldr_get_taskid(callid, &call); |
continue; |
case LOADER_SET_PATHNAME: |
loader_set_pathname(callid, &call); |
ldr_set_pathname(callid, &call); |
continue; |
case LOADER_SET_ARGS: |
loader_set_args(callid, &call); |
ldr_set_args(callid, &call); |
continue; |
case LOADER_SET_FILES: |
ldr_set_files(callid, &call); |
continue; |
case LOADER_LOAD: |
loader_load(callid, &call); |
ldr_load(callid, &call); |
continue; |
case LOADER_RUN: |
loader_run(callid, &call); |
ldr_run(callid, &call); |
/* Not reached */ |
default: |
retval = ENOENT; |
366,7 → 441,7 |
connected = false; |
/* Set a handler of incomming connections. */ |
async_set_client_connection(loader_connection); |
async_set_client_connection(ldr_connection); |
/* Register at naming service. */ |
if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0) |
/branches/network/uspace/srv/loader/elf_load.c |
---|
74,7 → 74,7 |
static int load_segment(elf_ld_t *elf, elf_segment_header_t *entry); |
/** Read until the buffer is read in its entirety. */ |
static int my_read(int fd, char *buf, size_t len) |
static int my_read(int fd, void *buf, size_t len) |
{ |
int cnt = 0; |
do { |
331,21 → 331,26 |
int flags = 0; |
uintptr_t bias; |
uintptr_t base; |
void *seg_ptr; |
uintptr_t seg_addr; |
size_t mem_sz; |
int rc; |
DPRINTF("Load segment at addr 0x%x, size 0x%x\n", entry->p_vaddr, |
bias = elf->bias; |
seg_addr = entry->p_vaddr + bias; |
seg_ptr = (void *) seg_addr; |
DPRINTF("Load segment at addr 0x%x, size 0x%x\n", seg_addr, |
entry->p_memsz); |
bias = elf->bias; |
if (entry->p_align > 1) { |
if ((entry->p_offset % entry->p_align) != |
(entry->p_vaddr % entry->p_align)) { |
(seg_addr % entry->p_align)) { |
DPRINTF("Align check 1 failed offset%%align=%d, " |
"vaddr%%align=%d\n", |
entry->p_offset % entry->p_align, |
entry->p_vaddr % entry->p_align |
seg_addr % entry->p_align |
); |
return EE_INVALID; |
} |
364,7 → 369,7 |
base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE); |
mem_sz = entry->p_memsz + (entry->p_vaddr - base); |
DPRINTF("Map to p_vaddr=0x%x-0x%x.\n", entry->p_vaddr + bias, |
DPRINTF("Map to seg_addr=0x%x-0x%x.\n", seg_addr, |
entry->p_vaddr + bias + ALIGN_UP(entry->p_memsz, PAGE_SIZE)); |
/* |
379,7 → 384,7 |
} |
DPRINTF("as_area_create(0x%lx, 0x%x, %d) -> 0x%lx\n", |
entry->p_vaddr+bias, entry->p_memsz, flags, (uintptr_t)a); |
base + bias, mem_sz, flags, (uintptr_t)a); |
/* |
* Load segment data |
399,7 → 404,7 |
uint8_t *dp; |
left = entry->p_filesz; |
dp = (uint8_t *)(entry->p_vaddr + bias); |
dp = seg_ptr; |
while (left > 0) { |
now = 16384; |
416,7 → 421,7 |
dp += now; |
} |
rc = as_area_change_flags((uint8_t *)entry->p_vaddr + bias, flags); |
rc = as_area_change_flags(seg_ptr, flags); |
if (rc != 0) { |
DPRINTF("Failed to set memory area flags.\n"); |
return EE_MEMORY; |
424,7 → 429,7 |
if (flags & AS_AREA_EXEC) { |
/* Enforce SMC coherence for the segment */ |
if (smc_coherence(entry->p_vaddr + bias, entry->p_filesz)) |
if (smc_coherence(seg_ptr, entry->p_filesz)) |
return EE_MEMORY; |
} |
/branches/network/uspace/srv/loader/arch/ia32/ia32.s |
---|
46,4 → 46,4 |
# Save a tiny bit of stack space |
pop %ebp |
jmp %eax |
jmp *%eax |
/branches/network/uspace/srv/fb/ega.h |
---|
43,4 → 43,3 |
/** @} |
*/ |
/branches/network/uspace/srv/fb/serial_console.c |
---|
43,8 → 43,9 |
#include <ipc/fb.h> |
#include <bool.h> |
#include <errno.h> |
#include <console/color.h> |
#include <console/style.h> |
#include <io/color.h> |
#include <io/style.h> |
#include <string.h> |
#include "../console/screenbuffer.h" |
#include "main.h" |
128,9 → 129,9 |
} |
void serial_goto(const unsigned int row, const unsigned int col) |
void serial_goto(const unsigned int col, const unsigned int row) |
{ |
if ((row > scr_height) || (col > scr_width)) |
if ((col > scr_width) || (row > scr_height)) |
return; |
char control[MAX_CONTROL]; |
153,7 → 154,7 |
void serial_scroll(int i) |
{ |
if (i > 0) { |
serial_goto(scr_height - 1, 0); |
serial_goto(0, scr_height - 1); |
while (i--) |
serial_puts("\033D"); |
} else if (i < 0) { |
241,11 → 242,18 |
static void serial_set_attrs(const attrs_t *a) |
{ |
switch (a->t) { |
case at_style: serial_set_style(a->a.s.style); break; |
case at_rgb: serial_set_rgb(a->a.r.fg_color, a->a.r.bg_color); break; |
case at_idx: serial_set_idx(a->a.i.fg_color, |
a->a.i.bg_color, a->a.i.flags); break; |
default: break; |
case at_style: |
serial_set_style(a->a.s.style); |
break; |
case at_rgb: |
serial_set_rgb(a->a.r.fg_color, a->a.r.bg_color); |
break; |
case at_idx: |
serial_set_idx(a->a.i.fg_color, |
a->a.i.bg_color, a->a.i.flags); |
break; |
default: |
break; |
} |
} |
265,13 → 273,13 |
keyfield_t *field; |
attrs_t *a0, *a1; |
serial_goto(y, x); |
serial_goto(x, y); |
a0 = &data[0].attrs; |
serial_set_attrs(a0); |
for (j = 0; j < h; j++) { |
if (j > 0 && w != scr_width) |
serial_goto(y, x); |
serial_goto(x, j); |
for (i = 0; i < w; i++) { |
field = &data[j * w + i]; |
354,16 → 362,16 |
break; |
} |
draw_text_data(interbuf, col, row, w, h); |
lastcol = col + w; |
lastrow = row + h - 1; |
lastcol = col + w; |
retval = 0; |
break; |
case FB_PUTCHAR: |
c = IPC_GET_ARG1(call); |
row = IPC_GET_ARG2(call); |
col = IPC_GET_ARG3(call); |
col = IPC_GET_ARG2(call); |
row = IPC_GET_ARG3(call); |
if ((lastcol != col) || (lastrow != row)) |
serial_goto(row, col); |
serial_goto(col, row); |
lastcol = col + 1; |
lastrow = row; |
serial_putchar(c); |
370,15 → 378,15 |
retval = 0; |
break; |
case FB_CURSOR_GOTO: |
row = IPC_GET_ARG1(call); |
col = IPC_GET_ARG2(call); |
serial_goto(row, col); |
col = IPC_GET_ARG1(call); |
row = IPC_GET_ARG2(call); |
serial_goto(col, row); |
lastcol = col; |
lastrow = row; |
lastcol = col; |
retval = 0; |
break; |
case FB_GET_CSIZE: |
ipc_answer_2(callid, EOK, scr_height, scr_width); |
ipc_answer_2(callid, EOK, scr_width, scr_height); |
continue; |
case FB_CLEAR: |
serial_clrscr(); |
416,7 → 424,7 |
break; |
} |
serial_scroll(i); |
serial_goto(lastrow, lastcol); |
serial_goto(lastcol, lastrow); |
retval = 0; |
break; |
case FB_CURSOR_VISIBILITY: |
/branches/network/uspace/srv/fb/serial_console.h |
---|
43,7 → 43,7 |
typedef void (*putc_function_t)(char); |
void serial_puts(char *str); |
void serial_goto(const unsigned int row, const unsigned int col); |
void serial_goto(const unsigned int col, const unsigned int row); |
void serial_clrscr(void); |
void serial_scroll(int i); |
void serial_cursor_disable(void); |
/branches/network/uspace/srv/fb/fb.c |
---|
51,9 → 51,10 |
#include <ipc/services.h> |
#include <kernel/errno.h> |
#include <kernel/genarch/fb/visuals.h> |
#include <console/color.h> |
#include <console/style.h> |
#include <io/color.h> |
#include <io/style.h> |
#include <async.h> |
#include <fibril.h> |
#include <bool.h> |
#include "font-8x16.h" |
375,8 → 376,8 |
*/ |
static void vport_redraw(viewport_t *vport) |
{ |
unsigned int col; |
unsigned int row; |
unsigned int col; |
for (row = 0; row < vport->rows; row++) { |
for (col = 0; col < vport->cols; col++) { |
431,8 → 432,8 |
*/ |
static void vport_scroll(viewport_t *vport, int lines) |
{ |
unsigned int col; |
unsigned int row; |
unsigned int col; |
unsigned int x; |
unsigned int y; |
uint32_t glyph; |
449,7 → 450,8 |
for (row = 0; row < vport->rows; row++) { |
x = vport->x; |
for (col = 0; col < vport->cols; col++) { |
if ((row + lines >= 0) && (row + lines < vport->rows)) { |
if (((int) row + lines >= 0) && |
((int) row + lines < (int) vport->rows)) { |
xbp = &vport->backbuf[BB_POS(vport, col, row + lines)]; |
bbp = &vport->backbuf[BB_POS(vport, col, row)]; |
1038,8 → 1040,8 |
* |
* @return false if the call was not handled byt this function, true otherwise |
* |
* Note: this function is not threads safe, you would have |
* to redefine static variables with __thread |
* Note: this function is not thread-safe, you would have |
* to redefine static variables with fibril_local. |
* |
*/ |
static bool shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp) |
1564,7 → 1566,7 |
unsigned int i; |
int scroll; |
wchar_t ch; |
unsigned int row, col; |
unsigned int col, row; |
if ((vport->cursor_active) || (anims_enabled)) |
callid = async_get_call_timeout(&call, 250000); |
1601,8 → 1603,8 |
case FB_PUTCHAR: |
ch = IPC_GET_ARG1(call); |
row = IPC_GET_ARG2(call); |
col = IPC_GET_ARG3(call); |
col = IPC_GET_ARG2(call); |
row = IPC_GET_ARG3(call); |
if ((col >= vport->cols) || (row >= vport->rows)) { |
retval = EINVAL; |
1620,8 → 1622,8 |
retval = EOK; |
break; |
case FB_CURSOR_GOTO: |
row = IPC_GET_ARG1(call); |
col = IPC_GET_ARG2(call); |
col = IPC_GET_ARG1(call); |
row = IPC_GET_ARG2(call); |
if ((col >= vport->cols) || (row >= vport->rows)) { |
retval = EINVAL; |
1641,7 → 1643,7 |
retval = EOK; |
break; |
case FB_GET_CSIZE: |
ipc_answer_2(callid, EOK, vport->rows, vport->cols); |
ipc_answer_2(callid, EOK, vport->cols, vport->rows); |
continue; |
case FB_SCROLL: |
scroll = IPC_GET_ARG1(call); |
/branches/network/uspace/srv/fb/ppm.c |
---|
89,7 → 89,7 |
{ |
unsigned int width, height; |
unsigned int maxcolor; |
int i; |
unsigned i; |
unsigned int color; |
unsigned int coef; |
/branches/network/uspace/srv/fb/ega.c |
---|
49,8 → 49,8 |
#include <ipc/ns.h> |
#include <ipc/services.h> |
#include <libarch/ddi.h> |
#include <console/style.h> |
#include <console/color.h> |
#include <io/style.h> |
#include <io/color.h> |
#include <sys/types.h> |
#include "ega.h" |
87,7 → 87,7 |
static void clrscr(void) |
{ |
int i; |
unsigned i; |
for (i = 0; i < scr_width * scr_height; i++) { |
scr_addr[i * 2] = ' '; |
95,7 → 95,7 |
} |
} |
static void cursor_goto(unsigned int row, unsigned int col) |
static void cursor_goto(unsigned int col, unsigned int row) |
{ |
int ega_cursor; |
129,7 → 129,8 |
static void scroll(int rows) |
{ |
int i; |
unsigned i; |
if (rows > 0) { |
memmove(scr_addr, ((char *) scr_addr) + rows * scr_width * 2, |
scr_width * scr_height * 2 - rows * scr_width * 2); |
144,12 → 145,12 |
} |
} |
static void printchar(wchar_t c, unsigned int row, unsigned int col) |
static void printchar(wchar_t c, unsigned int col, unsigned int row) |
{ |
scr_addr[(row * scr_width + col) * 2] = ega_glyph(c); |
scr_addr[(row * scr_width + col) * 2 + 1] = style; |
cursor_goto(row, col + 1); |
cursor_goto(col + 1, row); |
} |
/** Draw text data to viewport. |
241,11 → 242,15 |
static unsigned attr_to_ega_style(const attrs_t *a) |
{ |
switch (a->t) { |
case at_style: return style_to_ega_style(a->a.s.style); |
case at_rgb: return rgb_to_ega_style(a->a.r.fg_color, a->a.r.bg_color); |
case at_idx: return color_to_ega_style(a->a.i.fg_color, |
case at_style: |
return style_to_ega_style(a->a.s.style); |
case at_rgb: |
return rgb_to_ega_style(a->a.r.fg_color, a->a.r.bg_color); |
case at_idx: |
return color_to_ega_style(a->a.i.fg_color, |
a->a.i.bg_color, a->a.i.flags); |
default: return INVERTED_COLOR; |
default: |
return INVERTED_COLOR; |
} |
} |
312,7 → 317,7 |
retval = 0; |
break; |
case FB_GET_CSIZE: |
ipc_answer_2(callid, EOK, scr_height, scr_width); |
ipc_answer_2(callid, EOK, scr_width, scr_height); |
continue; |
case FB_CLEAR: |
clrscr(); |
320,28 → 325,28 |
break; |
case FB_PUTCHAR: |
c = IPC_GET_ARG1(call); |
row = IPC_GET_ARG2(call); |
col = IPC_GET_ARG3(call); |
col = IPC_GET_ARG2(call); |
row = IPC_GET_ARG3(call); |
if (col >= scr_width || row >= scr_height) { |
retval = EINVAL; |
break; |
} |
printchar(c, row, col); |
printchar(c, col, row); |
retval = 0; |
break; |
case FB_CURSOR_GOTO: |
row = IPC_GET_ARG1(call); |
col = IPC_GET_ARG2(call); |
col = IPC_GET_ARG1(call); |
row = IPC_GET_ARG2(call); |
if (row >= scr_height || col >= scr_width) { |
retval = EINVAL; |
break; |
} |
cursor_goto(row, col); |
cursor_goto(col, row); |
retval = 0; |
break; |
case FB_SCROLL: |
i = IPC_GET_ARG1(call); |
if (i > scr_height || i < -((int) scr_height)) { |
if (i > (int) scr_height || i < -((int) scr_height)) { |
retval = EINVAL; |
break; |
} |
/branches/network/uspace/srv/fb/Makefile |
---|
34,8 → 34,6 |
include $(LIBC_PREFIX)/Makefile.toolchain |
CFLAGS += -I../libipc/include |
LIBS = $(LIBC_PREFIX)/libc.a |
## Sources |
/branches/network/uspace/srv/fs/devfs/devfs.c |
---|
0,0 → 1,141 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file devfs.c |
* @brief Devices file system. |
* |
* Every device registered to device mapper is represented as a file in this |
* file system. |
*/ |
#include <stdio.h> |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <async.h> |
#include <errno.h> |
#include <libfs.h> |
#include "devfs.h" |
#include "devfs_ops.h" |
#define NAME "devfs" |
static vfs_info_t devfs_vfs_info = { |
.name = "devfs", |
}; |
fs_reg_t devfs_reg; |
static void devfs_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
if (iid) |
ipc_answer_0(iid, EOK); |
while (true) { |
ipc_call_t call; |
ipc_callid_t callid = async_get_call(&call); |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
return; |
case VFS_MOUNTED: |
devfs_mounted(callid, &call); |
break; |
case VFS_MOUNT: |
devfs_mount(callid, &call); |
break; |
case VFS_LOOKUP: |
devfs_lookup(callid, &call); |
break; |
case VFS_OPEN_NODE: |
devfs_open_node(callid, &call); |
break; |
case VFS_DEVICE: |
devfs_device(callid, &call); |
break; |
case VFS_READ: |
devfs_read(callid, &call); |
break; |
case VFS_WRITE: |
devfs_write(callid, &call); |
break; |
case VFS_TRUNCATE: |
devfs_truncate(callid, &call); |
break; |
case VFS_CLOSE: |
devfs_close(callid, &call); |
break; |
case VFS_SYNC: |
devfs_sync(callid, &call); |
break; |
case VFS_DESTROY: |
devfs_destroy(callid, &call); |
break; |
default: |
ipc_answer_0(callid, ENOTSUP); |
break; |
} |
} |
} |
int main(int argc, char *argv[]) |
{ |
printf(NAME ": HelenOS Device Filesystem\n"); |
if (!devfs_init()) { |
printf(NAME ": failed to initialize devfs\n"); |
return -1; |
} |
int vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0); |
if (vfs_phone < EOK) { |
printf(NAME ": Unable to connect to VFS\n"); |
return -1; |
} |
int rc = fs_register(vfs_phone, &devfs_reg, &devfs_vfs_info, |
devfs_connection); |
if (rc != EOK) { |
printf(NAME ": Failed to register file system (%d)\n", rc); |
return rc; |
} |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
/* Not reached */ |
return 0; |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/fs/devfs/devfs_ops.c |
---|
0,0 → 1,513 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file devfs_ops.c |
* @brief Implementation of VFS operations for the devfs file system server. |
*/ |
#include <ipc/ipc.h> |
#include <bool.h> |
#include <errno.h> |
#include <malloc.h> |
#include <string.h> |
#include <libfs.h> |
#include <fibril_sync.h> |
#include <adt/hash_table.h> |
#include "devfs.h" |
#include "devfs_ops.h" |
#define PLB_GET_CHAR(pos) (devfs_reg.plb_ro[pos % PLB_SIZE]) |
/** Opened devices structure */ |
typedef struct { |
dev_handle_t handle; |
int phone; |
size_t refcount; |
link_t link; |
} device_t; |
/** Hash table of opened devices */ |
static hash_table_t devices; |
/** Hash table mutex */ |
static FIBRIL_MUTEX_INITIALIZE(devices_mutex); |
#define DEVICES_KEYS 1 |
#define DEVICES_KEY_HANDLE 0 |
#define DEVICES_BUCKETS 256 |
/* Implementation of hash table interface for the nodes hash table. */ |
static hash_index_t devices_hash(unsigned long key[]) |
{ |
return key[DEVICES_KEY_HANDLE] % DEVICES_BUCKETS; |
} |
static int devices_compare(unsigned long key[], hash_count_t keys, link_t *item) |
{ |
device_t *dev = hash_table_get_instance(item, device_t, link); |
return (dev->handle == (dev_handle_t) key[DEVICES_KEY_HANDLE]); |
} |
static void devices_remove_callback(link_t *item) |
{ |
free(hash_table_get_instance(item, device_t, link)); |
} |
static hash_table_operations_t devices_ops = { |
.hash = devices_hash, |
.compare = devices_compare, |
.remove_callback = devices_remove_callback |
}; |
bool devfs_init(void) |
{ |
if (!hash_table_create(&devices, DEVICES_BUCKETS, |
DEVICES_KEYS, &devices_ops)) |
return false; |
if (devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING) < 0) |
return false; |
return true; |
} |
void devfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
{ |
/* Accept the mount options */ |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_write_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
char *opts = malloc(size + 1); |
if (!opts) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
if (retval != EOK) { |
ipc_answer_0(rid, retval); |
free(opts); |
return; |
} |
free(opts); |
ipc_answer_3(rid, EOK, 0, 0, 0); |
} |
void devfs_mount(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, ENOTSUP); |
} |
void devfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipcarg_t first = IPC_GET_ARG1(*request); |
ipcarg_t last = IPC_GET_ARG2(*request); |
dev_handle_t dev_handle = IPC_GET_ARG3(*request); |
ipcarg_t lflag = IPC_GET_ARG4(*request); |
fs_index_t index = IPC_GET_ARG5(*request); |
/* Hierarchy is flat, no altroot is supported */ |
if (index != 0) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
if ((lflag & L_LINK) || (lflag & L_UNLINK)) { |
ipc_answer_0(rid, ENOTSUP); |
return; |
} |
/* Eat slash */ |
if (PLB_GET_CHAR(first) == '/') { |
first++; |
first %= PLB_SIZE; |
} |
if (first >= last) { |
/* Root entry */ |
if (lflag & L_DIRECTORY) |
ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, 0, 0, 0); |
else |
ipc_answer_0(rid, ENOENT); |
} else { |
if (lflag & L_FILE) { |
size_t len; |
if (last >= first) |
len = last - first + 1; |
else |
len = first + PLB_SIZE - last + 1; |
char *name = (char *) malloc(len + 1); |
if (name == NULL) { |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
size_t i; |
for (i = 0; i < len; i++) |
name[i] = PLB_GET_CHAR(first + i); |
name[len] = 0; |
dev_handle_t handle; |
if (devmap_device_get_handle(name, &handle, 0) != EOK) { |
free(name); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
if (lflag & L_OPEN) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) handle |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
int phone = devmap_device_connect(handle, 0); |
if (phone < 0) { |
fibril_mutex_unlock(&devices_mutex); |
free(name); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = (device_t *) malloc(sizeof(device_t)); |
if (dev == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
free(name); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
dev->handle = handle; |
dev->phone = phone; |
dev->refcount = 1; |
hash_table_insert(&devices, key, &dev->link); |
} else { |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
dev->refcount++; |
} |
fibril_mutex_unlock(&devices_mutex); |
} |
free(name); |
ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, handle, 0, 1); |
} else |
ipc_answer_0(rid, ENOENT); |
} |
} |
void devfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t handle = IPC_GET_ARG2(*request); |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) handle |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
int phone = devmap_device_connect(handle, 0); |
if (phone < 0) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = (device_t *) malloc(sizeof(device_t)); |
if (dev == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
dev->handle = handle; |
dev->phone = phone; |
dev->refcount = 1; |
hash_table_insert(&devices, key, &dev->link); |
} else { |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
dev->refcount++; |
} |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_3(rid, EOK, 0, 1, L_FILE); |
} |
void devfs_device(ipc_callid_t rid, ipc_call_t *request) |
{ |
fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
if (index != 0) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_1(rid, EOK, (ipcarg_t) index); |
} else |
ipc_answer_0(rid, ENOTSUP); |
} |
void devfs_read(ipc_callid_t rid, ipc_call_t *request) |
{ |
fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
off_t pos = (off_t) IPC_GET_ARG3(*request); |
if (index != 0) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
ipc_callid_t callid; |
if (!ipc_data_read_receive(&callid, NULL)) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
/* Make a request at the driver */ |
ipc_call_t answer; |
aid_t msg = async_send_3(dev->phone, IPC_GET_METHOD(*request), |
IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), |
IPC_GET_ARG3(*request), &answer); |
/* Forward the IPC_M_DATA_READ request to the driver */ |
ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
fibril_mutex_unlock(&devices_mutex); |
/* Wait for reply from the driver. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
size_t bytes = IPC_GET_ARG1(answer); |
/* Driver reply is the final result of the whole operation */ |
ipc_answer_1(rid, rc, bytes); |
} else { |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_read_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
size_t count = devmap_device_get_count(); |
dev_desc_t *desc = malloc(count * sizeof(dev_desc_t)); |
if (desc == NULL) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_1(rid, ENOMEM, 0); |
return; |
} |
size_t max = devmap_device_get_devices(count, desc); |
if (pos < max) { |
ipc_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1); |
} else { |
ipc_answer_0(callid, ENOENT); |
ipc_answer_1(rid, ENOENT, 0); |
return; |
} |
free(desc); |
ipc_answer_1(rid, EOK, 1); |
} |
} |
void devfs_write(ipc_callid_t rid, ipc_call_t *request) |
{ |
fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
off_t pos = (off_t) IPC_GET_ARG3(*request); |
if (index != 0) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
ipc_callid_t callid; |
if (!ipc_data_write_receive(&callid, NULL)) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
/* Make a request at the driver */ |
ipc_call_t answer; |
aid_t msg = async_send_3(dev->phone, IPC_GET_METHOD(*request), |
IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), |
IPC_GET_ARG3(*request), &answer); |
/* Forward the IPC_M_DATA_WRITE request to the driver */ |
ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
fibril_mutex_unlock(&devices_mutex); |
/* Wait for reply from the driver. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
size_t bytes = IPC_GET_ARG1(answer); |
/* Driver reply is the final result of the whole operation */ |
ipc_answer_1(rid, rc, bytes); |
} else { |
/* Read-only filesystem */ |
ipc_answer_0(rid, ENOTSUP); |
} |
} |
void devfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, ENOTSUP); |
} |
void devfs_close(ipc_callid_t rid, ipc_call_t *request) |
{ |
fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
if (index != 0) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
dev->refcount--; |
if (dev->refcount == 0) { |
ipc_hangup(dev->phone); |
hash_table_remove(&devices, key, DEVICES_KEYS); |
} |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, EOK); |
} else |
ipc_answer_0(rid, ENOTSUP); |
} |
void devfs_sync(ipc_callid_t rid, ipc_call_t *request) |
{ |
fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
if (index != 0) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
/* Make a request at the driver */ |
ipc_call_t answer; |
aid_t msg = async_send_2(dev->phone, IPC_GET_METHOD(*request), |
IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer); |
fibril_mutex_unlock(&devices_mutex); |
/* Wait for reply from the driver */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
/* Driver reply is the final result of the whole operation */ |
ipc_answer_0(rid, rc); |
} else |
ipc_answer_0(rid, ENOTSUP); |
} |
void devfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, ENOTSUP); |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/fs/devfs/devfs_ops.h |
---|
0,0 → 1,57 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
#ifndef DEVFS_DEVFS_OPS_H_ |
#define DEVFS_DEVFS_OPS_H_ |
#include <ipc/ipc.h> |
#include <bool.h> |
extern bool devfs_init(void); |
extern void devfs_mounted(ipc_callid_t, ipc_call_t *); |
extern void devfs_mount(ipc_callid_t, ipc_call_t *); |
extern void devfs_lookup(ipc_callid_t, ipc_call_t *); |
extern void devfs_open_node(ipc_callid_t, ipc_call_t *); |
extern void devfs_device(ipc_callid_t, ipc_call_t *); |
extern void devfs_sync(ipc_callid_t, ipc_call_t *); |
extern void devfs_read(ipc_callid_t, ipc_call_t *); |
extern void devfs_write(ipc_callid_t, ipc_call_t *); |
extern void devfs_truncate(ipc_callid_t, ipc_call_t *); |
extern void devfs_close(ipc_callid_t, ipc_call_t *); |
extern void devfs_destroy(ipc_callid_t, ipc_call_t *); |
#endif |
/** |
* @} |
*/ |
/branches/network/uspace/srv/fs/devfs/Makefile |
---|
0,0 → 1,82 |
# |
# Copyright (c) 2005 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
LIBFS_PREFIX = ../../../lib/libfs |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
CFLAGS += -I $(LIBFS_PREFIX) |
LIBS = \ |
$(LIBFS_PREFIX)/libfs.a \ |
$(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = devfs |
SOURCES = \ |
devfs.c \ |
devfs_ops.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |
/branches/network/uspace/srv/fs/devfs/devfs.h |
---|
0,0 → 1,44 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
#ifndef DEVFS_DEVFS_H_ |
#define DEVFS_DEVFS_H_ |
#include <libfs.h> |
extern fs_reg_t devfs_reg; |
#endif |
/** |
* @} |
*/ |
/branches/network/uspace/srv/fs/tmpfs/tmpfs.h |
---|
38,12 → 38,15 |
#include <atomic.h> |
#include <sys/types.h> |
#include <bool.h> |
#include <libadt/hash_table.h> |
#include <adt/hash_table.h> |
#ifndef dprintf |
#define dprintf(...) printf(__VA_ARGS__) |
#endif |
#define TMPFS_NODE(node) ((node) ? (tmpfs_node_t *)(node)->data : NULL) |
#define FS_NODE(node) ((node) ? (node)->bp : NULL) |
typedef enum { |
TMPFS_NONE, |
TMPFS_FILE, |
50,18 → 53,26 |
TMPFS_DIRECTORY |
} tmpfs_dentry_type_t; |
/* forward declaration */ |
struct tmpfs_node; |
typedef struct tmpfs_dentry { |
link_t link; /**< Linkage for the list of siblings. */ |
struct tmpfs_node *node;/**< Back pointer to TMPFS node. */ |
char *name; /**< Name of dentry. */ |
} tmpfs_dentry_t; |
typedef struct tmpfs_node { |
fs_node_t *bp; /**< Back pointer to the FS node. */ |
fs_index_t index; /**< TMPFS node index. */ |
dev_handle_t dev_handle;/**< Device handle. */ |
link_t dh_link; /**< Dentries hash table link. */ |
struct tmpfs_dentry *sibling; |
struct tmpfs_dentry *child; |
hash_table_t names; /**< All names linking to this TMPFS node. */ |
link_t nh_link; /**< Nodes hash table link. */ |
tmpfs_dentry_type_t type; |
unsigned lnkcnt; /**< Link count. */ |
size_t size; /**< File size if type is TMPFS_FILE. */ |
void *data; /**< File content's if type is TMPFS_FILE. */ |
} tmpfs_dentry_t; |
link_t cs_head; /**< Head of child's siblings list. */ |
} tmpfs_node_t; |
extern fs_reg_t tmpfs_reg; |
75,7 → 86,11 |
extern void tmpfs_read(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_write(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_truncate(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_close(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_destroy(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_open_node(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_device(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_sync(ipc_callid_t, ipc_call_t *); |
extern bool tmpfs_restore(dev_handle_t); |
/branches/network/uspace/srv/fs/tmpfs/tmpfs_dump.c |
---|
54,8 → 54,8 |
} __attribute__((packed)); |
static bool |
tmpfs_restore_recursion(int dev, off_t *bufpos, size_t *buflen, off_t *pos, |
tmpfs_dentry_t *parent) |
tmpfs_restore_recursion(dev_handle_t dev, off_t *bufpos, size_t *buflen, |
off_t *pos, fs_node_t *pfn) |
{ |
struct rdentry entry; |
libfs_ops_t *ops = &tmpfs_libfs_ops; |
63,7 → 63,8 |
do { |
char *fname; |
tmpfs_dentry_t *node; |
fs_node_t *fn; |
tmpfs_node_t *nodep; |
uint32_t size; |
if (block_read(dev, bufpos, buflen, pos, &entry, sizeof(entry), |
80,8 → 81,8 |
if (fname == NULL) |
return false; |
node = (tmpfs_dentry_t *) ops->create(dev, L_FILE); |
if (node == NULL) { |
fn = ops->create(dev, L_FILE); |
if (fn == NULL) { |
free(fname); |
return false; |
} |
88,15 → 89,15 |
if (block_read(dev, bufpos, buflen, pos, fname, |
entry.len, TMPFS_BLOCK_SIZE) != EOK) { |
ops->destroy((void *) node); |
ops->destroy(fn); |
free(fname); |
return false; |
} |
fname[entry.len] = 0; |
rc = ops->link((void *) parent, (void *) node, fname); |
rc = ops->link(pfn, fn, fname); |
if (rc != EOK) { |
ops->destroy((void *) node); |
ops->destroy(fn); |
free(fname); |
return false; |
} |
108,12 → 109,13 |
size = uint32_t_le2host(size); |
node->data = malloc(size); |
if (node->data == NULL) |
nodep = TMPFS_NODE(fn); |
nodep->data = malloc(size); |
if (nodep->data == NULL) |
return false; |
node->size = size; |
if (block_read(dev, bufpos, buflen, pos, node->data, |
nodep->size = size; |
if (block_read(dev, bufpos, buflen, pos, nodep->data, |
size, TMPFS_BLOCK_SIZE) != EOK) |
return false; |
123,8 → 125,8 |
if (fname == NULL) |
return false; |
node = (tmpfs_dentry_t *) ops->create(dev, L_DIRECTORY); |
if (node == NULL) { |
fn = ops->create(dev, L_DIRECTORY); |
if (fn == NULL) { |
free(fname); |
return false; |
} |
131,15 → 133,15 |
if (block_read(dev, bufpos, buflen, pos, fname, |
entry.len, TMPFS_BLOCK_SIZE) != EOK) { |
ops->destroy((void *) node); |
ops->destroy(fn); |
free(fname); |
return false; |
} |
fname[entry.len] = 0; |
rc = ops->link((void *) parent, (void *) node, fname); |
rc = ops->link(pfn, fn, fname); |
if (rc != EOK) { |
ops->destroy((void *) node); |
ops->destroy(fn); |
free(fname); |
return false; |
} |
146,7 → 148,7 |
free(fname); |
if (!tmpfs_restore_recursion(dev, bufpos, buflen, pos, |
node)) |
fn)) |
return false; |
break; |
/branches/network/uspace/srv/fs/tmpfs/tmpfs.c |
---|
96,6 → 96,8 |
callid = async_get_call(&call); |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
return; |
case VFS_MOUNTED: |
tmpfs_mounted(callid, &call); |
break; |
114,9 → 116,21 |
case VFS_TRUNCATE: |
tmpfs_truncate(callid, &call); |
break; |
case VFS_CLOSE: |
tmpfs_close(callid, &call); |
break; |
case VFS_DESTROY: |
tmpfs_destroy(callid, &call); |
break; |
case VFS_OPEN_NODE: |
tmpfs_open_node(callid, &call); |
break; |
case VFS_DEVICE: |
tmpfs_device(callid, &call); |
break; |
case VFS_SYNC: |
tmpfs_sync(callid, &call); |
break; |
default: |
ipc_answer_0(callid, ENOTSUP); |
break; |
/branches/network/uspace/srv/fs/tmpfs/tmpfs_ops.c |
---|
47,7 → 47,7 |
#include <stdio.h> |
#include <assert.h> |
#include <sys/types.h> |
#include <libadt/hash_table.h> |
#include <adt/hash_table.h> |
#include <as.h> |
#include <libfs.h> |
54,10 → 54,8 |
#define min(a, b) ((a) < (b) ? (a) : (b)) |
#define max(a, b) ((a) > (b) ? (a) : (b)) |
#define DENTRIES_BUCKETS 256 |
#define NODES_BUCKETS 256 |
#define NAMES_BUCKETS 4 |
/** All root nodes have index 0. */ |
#define TMPFS_SOME_ROOT 0 |
/** Global counter for assigning node indices. Shared by all instances. */ |
68,36 → 66,36 |
*/ |
/* Forward declarations of static functions. */ |
static void *tmpfs_match(void *, const char *); |
static void *tmpfs_node_get(dev_handle_t, fs_index_t); |
static void tmpfs_node_put(void *); |
static void *tmpfs_create_node(dev_handle_t, int); |
static int tmpfs_link_node(void *, void *, const char *); |
static int tmpfs_unlink_node(void *, void *); |
static int tmpfs_destroy_node(void *); |
static fs_node_t *tmpfs_match(fs_node_t *, const char *); |
static fs_node_t *tmpfs_node_get(dev_handle_t, fs_index_t); |
static void tmpfs_node_put(fs_node_t *); |
static fs_node_t *tmpfs_create_node(dev_handle_t, int); |
static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *); |
static int tmpfs_unlink_node(fs_node_t *, fs_node_t *, const char *); |
static int tmpfs_destroy_node(fs_node_t *); |
/* Implementation of helper functions. */ |
static fs_index_t tmpfs_index_get(void *nodep) |
static fs_index_t tmpfs_index_get(fs_node_t *fn) |
{ |
return ((tmpfs_dentry_t *) nodep)->index; |
return TMPFS_NODE(fn)->index; |
} |
static size_t tmpfs_size_get(void *nodep) |
static size_t tmpfs_size_get(fs_node_t *fn) |
{ |
return ((tmpfs_dentry_t *) nodep)->size; |
return TMPFS_NODE(fn)->size; |
} |
static unsigned tmpfs_lnkcnt_get(void *nodep) |
static unsigned tmpfs_lnkcnt_get(fs_node_t *fn) |
{ |
return ((tmpfs_dentry_t *) nodep)->lnkcnt; |
return TMPFS_NODE(fn)->lnkcnt; |
} |
static bool tmpfs_has_children(void *nodep) |
static bool tmpfs_has_children(fs_node_t *fn) |
{ |
return ((tmpfs_dentry_t *) nodep)->child != NULL; |
return !list_empty(&TMPFS_NODE(fn)->cs_head); |
} |
static void *tmpfs_root_get(dev_handle_t dev_handle) |
static fs_node_t *tmpfs_root_get(dev_handle_t dev_handle) |
{ |
return tmpfs_node_get(dev_handle, TMPFS_SOME_ROOT); |
} |
107,14 → 105,14 |
return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
} |
static bool tmpfs_is_directory(void *nodep) |
static bool tmpfs_is_directory(fs_node_t *fn) |
{ |
return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY; |
return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY; |
} |
static bool tmpfs_is_file(void *nodep) |
static bool tmpfs_is_file(fs_node_t *fn) |
{ |
return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE; |
return TMPFS_NODE(fn)->type == TMPFS_FILE; |
} |
/** libfs operations */ |
136,99 → 134,60 |
.is_file = tmpfs_is_file |
}; |
/** Hash table of all directory entries. */ |
hash_table_t dentries; |
/** Hash table of all TMPFS nodes. */ |
hash_table_t nodes; |
#define DENTRIES_KEY_INDEX 0 |
#define DENTRIES_KEY_DEV 1 |
#define NODES_KEY_INDEX 0 |
#define NODES_KEY_DEV 1 |
/* Implementation of hash table interface for the dentries hash table. */ |
static hash_index_t dentries_hash(unsigned long key[]) |
/* Implementation of hash table interface for the nodes hash table. */ |
static hash_index_t nodes_hash(unsigned long key[]) |
{ |
return key[DENTRIES_KEY_INDEX] % DENTRIES_BUCKETS; |
return key[NODES_KEY_INDEX] % NODES_BUCKETS; |
} |
static int dentries_compare(unsigned long key[], hash_count_t keys, |
link_t *item) |
static int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item) |
{ |
tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t, |
dh_link); |
return (dentry->index == key[DENTRIES_KEY_INDEX] && |
dentry->dev_handle == key[DENTRIES_KEY_DEV]); |
tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t, |
nh_link); |
return (nodep->index == key[NODES_KEY_INDEX] && |
nodep->dev_handle == key[NODES_KEY_DEV]); |
} |
static void dentries_remove_callback(link_t *item) |
static void nodes_remove_callback(link_t *item) |
{ |
} |
/** TMPFS dentries hash table operations. */ |
hash_table_operations_t dentries_ops = { |
.hash = dentries_hash, |
.compare = dentries_compare, |
.remove_callback = dentries_remove_callback |
/** TMPFS nodes hash table operations. */ |
hash_table_operations_t nodes_ops = { |
.hash = nodes_hash, |
.compare = nodes_compare, |
.remove_callback = nodes_remove_callback |
}; |
typedef struct { |
char *name; |
tmpfs_dentry_t *parent; |
link_t link; |
} tmpfs_name_t; |
/* Implementation of hash table interface for the names hash table. */ |
static hash_index_t names_hash(unsigned long *key) |
static void tmpfs_node_initialize(tmpfs_node_t *nodep) |
{ |
tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
return dentry->index % NAMES_BUCKETS; |
nodep->bp = NULL; |
nodep->index = 0; |
nodep->dev_handle = 0; |
nodep->type = TMPFS_NONE; |
nodep->lnkcnt = 0; |
nodep->size = 0; |
nodep->data = NULL; |
link_initialize(&nodep->nh_link); |
list_initialize(&nodep->cs_head); |
} |
static int names_compare(unsigned long *key, hash_count_t keys, link_t *item) |
static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp) |
{ |
tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
link); |
return dentry == namep->parent; |
link_initialize(&dentryp->link); |
dentryp->name = NULL; |
dentryp->node = NULL; |
} |
static void names_remove_callback(link_t *item) |
{ |
tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
link); |
free(namep->name); |
free(namep); |
} |
/** TMPFS node names hash table operations. */ |
static hash_table_operations_t names_ops = { |
.hash = names_hash, |
.compare = names_compare, |
.remove_callback = names_remove_callback |
}; |
static void tmpfs_name_initialize(tmpfs_name_t *namep) |
{ |
namep->name = NULL; |
namep->parent = NULL; |
link_initialize(&namep->link); |
} |
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
{ |
dentry->index = 0; |
dentry->dev_handle = 0; |
dentry->sibling = NULL; |
dentry->child = NULL; |
dentry->type = TMPFS_NONE; |
dentry->lnkcnt = 0; |
dentry->size = 0; |
dentry->data = NULL; |
link_initialize(&dentry->dh_link); |
return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1, |
&names_ops); |
} |
bool tmpfs_init(void) |
{ |
if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 2, &dentries_ops)) |
if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops)) |
return false; |
return true; |
236,181 → 195,170 |
static bool tmpfs_instance_init(dev_handle_t dev_handle) |
{ |
tmpfs_dentry_t *root; |
fs_node_t *rfn; |
root = (tmpfs_dentry_t *) tmpfs_create_node(dev_handle, L_DIRECTORY); |
if (!root) |
rfn = tmpfs_create_node(dev_handle, L_DIRECTORY); |
if (!rfn) |
return false; |
root->lnkcnt = 0; /* FS root is not linked */ |
TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */ |
return true; |
} |
/** Compare one component of path to a directory entry. |
* |
* @param parentp Pointer to node from which we descended. |
* @param childp Pointer to node to compare the path component with. |
* @param component Array of characters holding component name. |
* |
* @return True on match, false otherwise. |
*/ |
static bool |
tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp, |
const char *component) |
fs_node_t *tmpfs_match(fs_node_t *pfn, const char *component) |
{ |
unsigned long key = (unsigned long) parentp; |
link_t *hlp = hash_table_find(&childp->names, &key); |
assert(hlp); |
tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link); |
return !str_cmp(namep->name, component); |
tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
link_t *lnk; |
for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
lnk = lnk->next) { |
tmpfs_dentry_t *dentryp = list_get_instance(lnk, tmpfs_dentry_t, |
link); |
if (!str_cmp(dentryp->name, component)) |
return FS_NODE(dentryp->node); |
} |
void *tmpfs_match(void *prnt, const char *component) |
{ |
tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
tmpfs_dentry_t *childp = parentp->child; |
while (childp && !tmpfs_match_one(parentp, childp, component)) |
childp = childp->sibling; |
return (void *) childp; |
return NULL; |
} |
void * |
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
fs_node_t *tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
{ |
unsigned long key[] = { |
[DENTRIES_KEY_INDEX] = index, |
[DENTRIES_KEY_DEV] = dev_handle |
[NODES_KEY_INDEX] = index, |
[NODES_KEY_DEV] = dev_handle |
}; |
link_t *lnk = hash_table_find(&dentries, key); |
link_t *lnk = hash_table_find(&nodes, key); |
if (!lnk) |
return NULL; |
return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link); |
return FS_NODE(hash_table_get_instance(lnk, tmpfs_node_t, nh_link)); |
} |
void tmpfs_node_put(void *node) |
void tmpfs_node_put(fs_node_t *fn) |
{ |
/* nothing to do */ |
} |
void *tmpfs_create_node(dev_handle_t dev_handle, int lflag) |
fs_node_t *tmpfs_create_node(dev_handle_t dev_handle, int lflag) |
{ |
assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t)); |
if (!node) |
tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t)); |
if (!nodep) |
return NULL; |
if (!tmpfs_dentry_initialize(node)) { |
free(node); |
tmpfs_node_initialize(nodep); |
nodep->bp = malloc(sizeof(fs_node_t)); |
if (!nodep->bp) { |
free(nodep); |
return NULL; |
} |
fs_node_initialize(nodep->bp); |
nodep->bp->data = nodep; /* link the FS and TMPFS nodes */ |
if (!tmpfs_root_get(dev_handle)) |
node->index = TMPFS_SOME_ROOT; |
nodep->index = TMPFS_SOME_ROOT; |
else |
node->index = tmpfs_next_index++; |
node->dev_handle = dev_handle; |
nodep->index = tmpfs_next_index++; |
nodep->dev_handle = dev_handle; |
if (lflag & L_DIRECTORY) |
node->type = TMPFS_DIRECTORY; |
nodep->type = TMPFS_DIRECTORY; |
else |
node->type = TMPFS_FILE; |
nodep->type = TMPFS_FILE; |
/* Insert the new node into the dentry hash table. */ |
/* Insert the new node into the nodes hash table. */ |
unsigned long key[] = { |
[DENTRIES_KEY_INDEX] = node->index, |
[DENTRIES_KEY_DEV] = node->dev_handle |
[NODES_KEY_INDEX] = nodep->index, |
[NODES_KEY_DEV] = nodep->dev_handle |
}; |
hash_table_insert(&dentries, key, &node->dh_link); |
return (void *) node; |
hash_table_insert(&nodes, key, &nodep->nh_link); |
return FS_NODE(nodep); |
} |
int tmpfs_link_node(void *prnt, void *chld, const char *nm) |
int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
{ |
tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld; |
tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
tmpfs_node_t *childp = TMPFS_NODE(cfn); |
tmpfs_dentry_t *dentryp; |
link_t *lnk; |
assert(parentp->type == TMPFS_DIRECTORY); |
tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t)); |
if (!namep) |
/* Check for duplicit entries. */ |
for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
lnk = lnk->next) { |
dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
if (!str_cmp(dentryp->name, nm)) |
return EEXIST; |
} |
/* Allocate and initialize the dentry. */ |
dentryp = malloc(sizeof(tmpfs_dentry_t)); |
if (!dentryp) |
return ENOMEM; |
tmpfs_name_initialize(namep); |
tmpfs_dentry_initialize(dentryp); |
/* Populate and link the new dentry. */ |
size_t size = str_size(nm); |
namep->name = malloc(size + 1); |
if (!namep->name) { |
free(namep); |
dentryp->name = malloc(size + 1); |
if (!dentryp->name) { |
free(dentryp); |
return ENOMEM; |
} |
str_cpy(namep->name, size + 1, nm); |
namep->parent = parentp; |
str_cpy(dentryp->name, size + 1, nm); |
dentryp->node = childp; |
childp->lnkcnt++; |
list_append(&dentryp->link, &parentp->cs_head); |
unsigned long key = (unsigned long) parentp; |
hash_table_insert(&childp->names, &key, &namep->link); |
/* Insert the new node into the namespace. */ |
if (parentp->child) { |
tmpfs_dentry_t *tmp = parentp->child; |
while (tmp->sibling) |
tmp = tmp->sibling; |
tmp->sibling = childp; |
} else { |
parentp->child = childp; |
} |
return EOK; |
} |
int tmpfs_unlink_node(void *prnt, void *chld) |
int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
{ |
tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt; |
tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld; |
tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
tmpfs_node_t *childp = NULL; |
tmpfs_dentry_t *dentryp; |
link_t *lnk; |
if (!parentp) |
return EBUSY; |
if (childp->child) |
return ENOTEMPTY; |
if (parentp->child == childp) { |
parentp->child = childp->sibling; |
} else { |
/* TODO: consider doubly linked list for organizing siblings. */ |
tmpfs_dentry_t *tmp = parentp->child; |
while (tmp->sibling != childp) |
tmp = tmp->sibling; |
tmp->sibling = childp->sibling; |
for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
lnk = lnk->next) { |
dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
if (!str_cmp(dentryp->name, nm)) { |
childp = dentryp->node; |
assert(FS_NODE(childp) == cfn); |
break; |
} |
childp->sibling = NULL; |
} |
unsigned long key = (unsigned long) parentp; |
hash_table_remove(&childp->names, &key, 1); |
if (!childp) |
return ENOENT; |
if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head)) |
return ENOTEMPTY; |
list_remove(&dentryp->link); |
free(dentryp); |
childp->lnkcnt--; |
return EOK; |
} |
int tmpfs_destroy_node(void *nodep) |
int tmpfs_destroy_node(fs_node_t *fn) |
{ |
tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
tmpfs_node_t *nodep = TMPFS_NODE(fn); |
assert(!dentry->lnkcnt); |
assert(!dentry->child); |
assert(!dentry->sibling); |
assert(!nodep->lnkcnt); |
assert(list_empty(&nodep->cs_head)); |
unsigned long key[] = { |
[DENTRIES_KEY_INDEX] = dentry->index, |
[DENTRIES_KEY_DEV] = dentry->dev_handle |
[NODES_KEY_INDEX] = nodep->index, |
[NODES_KEY_DEV] = nodep->dev_handle |
}; |
hash_table_remove(&dentries, key, 2); |
hash_table_remove(&nodes, key, 2); |
hash_table_destroy(&dentry->names); |
if (dentry->type == TMPFS_FILE) |
free(dentry->data); |
free(dentry); |
if (nodep->type == TMPFS_FILE) |
free(nodep->data); |
free(nodep->bp); |
free(nodep); |
return EOK; |
} |
446,26 → 394,22 |
return; |
} |
tmpfs_dentry_t *root = tmpfs_root_get(dev_handle); |
tmpfs_node_t *rootp = TMPFS_NODE(tmpfs_root_get(dev_handle)); |
if (str_cmp(opts, "restore") == 0) { |
if (tmpfs_restore(dev_handle)) |
ipc_answer_3(rid, EOK, root->index, root->size, |
root->lnkcnt); |
ipc_answer_3(rid, EOK, rootp->index, rootp->size, |
rootp->lnkcnt); |
else |
ipc_answer_0(rid, ELIMIT); |
} else { |
ipc_answer_3(rid, EOK, root->index, root->size, root->lnkcnt); |
ipc_answer_3(rid, EOK, rootp->index, rootp->size, |
rootp->lnkcnt); |
} |
} |
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
fs_index_t mp_index = (fs_index_t) IPC_GET_ARG2(*request); |
fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request); |
dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request); |
ipc_answer_0(rid, ENOTSUP); |
libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
} |
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
480,20 → 424,20 |
off_t pos = (off_t)IPC_GET_ARG3(*request); |
/* |
* Lookup the respective dentry. |
* Lookup the respective TMPFS node. |
*/ |
link_t *hlp; |
unsigned long key[] = { |
[DENTRIES_KEY_INDEX] = index, |
[DENTRIES_KEY_DEV] = dev_handle, |
[NODES_KEY_INDEX] = index, |
[NODES_KEY_DEV] = dev_handle, |
}; |
hlp = hash_table_find(&dentries, key); |
hlp = hash_table_find(&nodes, key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
dh_link); |
tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
nh_link); |
/* |
* Receive the read request. |
507,15 → 451,16 |
} |
size_t bytes; |
if (dentry->type == TMPFS_FILE) { |
bytes = max(0, min(dentry->size - pos, size)); |
(void) ipc_data_read_finalize(callid, dentry->data + pos, |
if (nodep->type == TMPFS_FILE) { |
bytes = max(0, min(nodep->size - pos, size)); |
(void) ipc_data_read_finalize(callid, nodep->data + pos, |
bytes); |
} else { |
tmpfs_dentry_t *dentryp; |
link_t *lnk; |
int i; |
tmpfs_dentry_t *cur; |
assert(dentry->type == TMPFS_DIRECTORY); |
assert(nodep->type == TMPFS_DIRECTORY); |
/* |
* Yes, we really use O(n) algorithm here. |
522,24 → 467,21 |
* If it bothers someone, it could be fixed by introducing a |
* hash table. |
*/ |
for (i = 0, cur = dentry->child; i < pos && cur; i++, |
cur = cur->sibling) |
for (i = 0, lnk = nodep->cs_head.next; |
i < pos && lnk != &nodep->cs_head; |
i++, lnk = lnk->next) |
; |
if (!cur) { |
if (lnk == &nodep->cs_head) { |
ipc_answer_0(callid, ENOENT); |
ipc_answer_1(rid, ENOENT, 0); |
return; |
} |
unsigned long key = (unsigned long) dentry; |
link_t *hlp = hash_table_find(&cur->names, &key); |
assert(hlp); |
tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, |
link); |
dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
(void) ipc_data_read_finalize(callid, namep->name, |
str_size(namep->name) + 1); |
(void) ipc_data_read_finalize(callid, dentryp->name, |
str_size(dentryp->name) + 1); |
bytes = 1; |
} |
556,20 → 498,20 |
off_t pos = (off_t)IPC_GET_ARG3(*request); |
/* |
* Lookup the respective dentry. |
* Lookup the respective TMPFS node. |
*/ |
link_t *hlp; |
unsigned long key[] = { |
[DENTRIES_KEY_INDEX] = index, |
[DENTRIES_KEY_DEV] = dev_handle |
[NODES_KEY_INDEX] = index, |
[NODES_KEY_DEV] = dev_handle |
}; |
hlp = hash_table_find(&dentries, key); |
hlp = hash_table_find(&nodes, key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
dh_link); |
tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
nh_link); |
/* |
* Receive the write request. |
585,13 → 527,13 |
/* |
* Check whether the file needs to grow. |
*/ |
if (pos + size <= dentry->size) { |
if (pos + size <= nodep->size) { |
/* The file size is not changing. */ |
(void) ipc_data_write_finalize(callid, dentry->data + pos, size); |
ipc_answer_2(rid, EOK, size, dentry->size); |
(void) ipc_data_write_finalize(callid, nodep->data + pos, size); |
ipc_answer_2(rid, EOK, size, nodep->size); |
return; |
} |
size_t delta = (pos + size) - dentry->size; |
size_t delta = (pos + size) - nodep->size; |
/* |
* At this point, we are deliberately extremely straightforward and |
* simply realloc the contents of the file on every write that grows the |
599,18 → 541,18 |
* our heap allocator can save us and just grow the block whenever |
* possible. |
*/ |
void *newdata = realloc(dentry->data, dentry->size + delta); |
void *newdata = realloc(nodep->data, nodep->size + delta); |
if (!newdata) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_2(rid, EOK, 0, dentry->size); |
ipc_answer_2(rid, EOK, 0, nodep->size); |
return; |
} |
/* Clear any newly allocated memory in order to emulate gaps. */ |
memset(newdata + dentry->size, 0, delta); |
dentry->size += delta; |
dentry->data = newdata; |
(void) ipc_data_write_finalize(callid, dentry->data + pos, size); |
ipc_answer_2(rid, EOK, size, dentry->size); |
memset(newdata + nodep->size, 0, delta); |
nodep->size += delta; |
nodep->data = newdata; |
(void) ipc_data_write_finalize(callid, nodep->data + pos, size); |
ipc_answer_2(rid, EOK, size, nodep->size); |
} |
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
620,40 → 562,45 |
size_t size = (off_t)IPC_GET_ARG3(*request); |
/* |
* Lookup the respective dentry. |
* Lookup the respective TMPFS node. |
*/ |
link_t *hlp; |
unsigned long key[] = { |
[DENTRIES_KEY_INDEX] = index, |
[DENTRIES_KEY_DEV] = dev_handle |
[NODES_KEY_INDEX] = index, |
[NODES_KEY_DEV] = dev_handle |
}; |
hlp = hash_table_find(&dentries, key); |
hlp = hash_table_find(&nodes, key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
dh_link); |
tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
nh_link); |
if (size == dentry->size) { |
if (size == nodep->size) { |
ipc_answer_0(rid, EOK); |
return; |
} |
void *newdata = realloc(dentry->data, size); |
void *newdata = realloc(nodep->data, size); |
if (!newdata) { |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
if (size > dentry->size) { |
size_t delta = size - dentry->size; |
memset(newdata + dentry->size, 0, delta); |
if (size > nodep->size) { |
size_t delta = size - nodep->size; |
memset(newdata + nodep->size, 0, delta); |
} |
dentry->size = size; |
dentry->data = newdata; |
nodep->size = size; |
nodep->data = newdata; |
ipc_answer_0(rid, EOK); |
} |
void tmpfs_close(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, EOK); |
} |
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
662,20 → 609,36 |
link_t *hlp; |
unsigned long key[] = { |
[DENTRIES_KEY_INDEX] = index, |
[DENTRIES_KEY_DEV] = dev_handle |
[NODES_KEY_INDEX] = index, |
[NODES_KEY_DEV] = dev_handle |
}; |
hlp = hash_table_find(&dentries, key); |
hlp = hash_table_find(&nodes, key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
dh_link); |
rc = tmpfs_destroy_node(dentry); |
tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
nh_link); |
rc = tmpfs_destroy_node(FS_NODE(nodep)); |
ipc_answer_0(rid, rc); |
} |
void tmpfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
{ |
libfs_open_node(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
} |
void tmpfs_device(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, ENOTSUP); |
} |
void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request) |
{ |
/* Dummy implementation */ |
ipc_answer_0(rid, EOK); |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/fs/fat/fat_idx.c |
---|
39,10 → 39,10 |
#include "../../vfs/vfs.h" |
#include <errno.h> |
#include <string.h> |
#include <libadt/hash_table.h> |
#include <libadt/list.h> |
#include <adt/hash_table.h> |
#include <adt/list.h> |
#include <assert.h> |
#include <futex.h> |
#include <fibril_sync.h> |
/** Each instance of this type describes one interval of freed VFS indices. */ |
typedef struct { |
68,8 → 68,8 |
link_t freed_head; |
} unused_t; |
/** Futex protecting the list of unused structures. */ |
static futex_t unused_futex = FUTEX_INITIALIZER; |
/** Mutex protecting the list of unused structures. */ |
static FIBRIL_MUTEX_INITIALIZE(unused_lock); |
/** List of unused structures. */ |
static LIST_INITIALIZE(unused_head); |
89,7 → 89,7 |
link_t *l; |
if (lock) |
futex_down(&unused_futex); |
fibril_mutex_lock(&unused_lock); |
for (l = unused_head.next; l != &unused_head; l = l->next) { |
u = list_get_instance(l, unused_t, link); |
if (u->dev_handle == dev_handle) |
96,12 → 96,12 |
return u; |
} |
if (lock) |
futex_up(&unused_futex); |
fibril_mutex_unlock(&unused_lock); |
return NULL; |
} |
/** Futex protecting the up_hash and ui_hash. */ |
static futex_t used_futex = FUTEX_INITIALIZER; |
/** Mutex protecting the up_hash and ui_hash. */ |
static FIBRIL_MUTEX_INITIALIZE(used_lock); |
/** |
* Global hash table of all used fat_idx_t structures. |
231,7 → 231,7 |
*/ |
*index = u->next++; |
--u->remaining; |
futex_up(&unused_futex); |
fibril_mutex_unlock(&unused_lock); |
return true; |
} |
} else { |
244,7 → 244,7 |
list_remove(&f->link); |
free(f); |
} |
futex_up(&unused_futex); |
fibril_mutex_unlock(&unused_lock); |
return true; |
} |
/* |
252,7 → 252,7 |
* theoretically still possible (e.g. too many open unlinked nodes or |
* too many zero-sized nodes). |
*/ |
futex_up(&unused_futex); |
fibril_mutex_unlock(&unused_lock); |
return false; |
} |
302,7 → 302,7 |
if (lnk->prev != &u->freed_head) |
try_coalesce_intervals(lnk->prev, lnk, |
lnk); |
futex_up(&unused_futex); |
fibril_mutex_unlock(&unused_lock); |
return; |
} |
if (f->last == index - 1) { |
310,7 → 310,7 |
if (lnk->next != &u->freed_head) |
try_coalesce_intervals(lnk, lnk->next, |
lnk); |
futex_up(&unused_futex); |
fibril_mutex_unlock(&unused_lock); |
return; |
} |
if (index > f->first) { |
321,7 → 321,7 |
n->first = index; |
n->last = index; |
list_insert_before(&n->link, lnk); |
futex_up(&unused_futex); |
fibril_mutex_unlock(&unused_lock); |
return; |
} |
335,7 → 335,7 |
n->last = index; |
list_append(&n->link, &u->freed_head); |
} |
futex_up(&unused_futex); |
fibril_mutex_unlock(&unused_lock); |
} |
static fat_idx_t *fat_idx_create(dev_handle_t dev_handle) |
352,7 → 352,7 |
link_initialize(&fidx->uph_link); |
link_initialize(&fidx->uih_link); |
futex_initialize(&fidx->lock, 1); |
fibril_mutex_initialize(&fidx->lock); |
fidx->dev_handle = dev_handle; |
fidx->pfc = FAT_CLST_RES0; /* no parent yet */ |
fidx->pdi = 0; |
365,10 → 365,10 |
{ |
fat_idx_t *fidx; |
futex_down(&used_futex); |
fibril_mutex_lock(&used_lock); |
fidx = fat_idx_create(dev_handle); |
if (!fidx) { |
futex_up(&used_futex); |
fibril_mutex_unlock(&used_lock); |
return NULL; |
} |
378,8 → 378,8 |
}; |
hash_table_insert(&ui_hash, ikey, &fidx->uih_link); |
futex_down(&fidx->lock); |
futex_up(&used_futex); |
fibril_mutex_lock(&fidx->lock); |
fibril_mutex_unlock(&used_lock); |
return fidx; |
} |
395,7 → 395,7 |
[UPH_PDI_KEY] = pdi, |
}; |
futex_down(&used_futex); |
fibril_mutex_lock(&used_lock); |
l = hash_table_find(&up_hash, pkey); |
if (l) { |
fidx = hash_table_get_instance(l, fat_idx_t, uph_link); |
402,7 → 402,7 |
} else { |
fidx = fat_idx_create(dev_handle); |
if (!fidx) { |
futex_up(&used_futex); |
fibril_mutex_unlock(&used_lock); |
return NULL; |
} |
417,8 → 417,8 |
hash_table_insert(&up_hash, pkey, &fidx->uph_link); |
hash_table_insert(&ui_hash, ikey, &fidx->uih_link); |
} |
futex_down(&fidx->lock); |
futex_up(&used_futex); |
fibril_mutex_lock(&fidx->lock); |
fibril_mutex_unlock(&used_lock); |
return fidx; |
} |
431,9 → 431,9 |
[UPH_PDI_KEY] = idx->pdi, |
}; |
futex_down(&used_futex); |
fibril_mutex_lock(&used_lock); |
hash_table_insert(&up_hash, pkey, &idx->uph_link); |
futex_up(&used_futex); |
fibril_mutex_unlock(&used_lock); |
} |
void fat_idx_hashout(fat_idx_t *idx) |
444,9 → 444,9 |
[UPH_PDI_KEY] = idx->pdi, |
}; |
futex_down(&used_futex); |
fibril_mutex_lock(&used_lock); |
hash_table_remove(&up_hash, pkey, 3); |
futex_up(&used_futex); |
fibril_mutex_unlock(&used_lock); |
} |
fat_idx_t * |
459,13 → 459,13 |
[UIH_INDEX_KEY] = index, |
}; |
futex_down(&used_futex); |
fibril_mutex_lock(&used_lock); |
l = hash_table_find(&ui_hash, ikey); |
if (l) { |
fidx = hash_table_get_instance(l, fat_idx_t, uih_link); |
futex_down(&fidx->lock); |
fibril_mutex_lock(&fidx->lock); |
} |
futex_up(&used_futex); |
fibril_mutex_unlock(&used_lock); |
return fidx; |
} |
483,7 → 483,7 |
assert(idx->pfc == FAT_CLST_RES0); |
futex_down(&used_futex); |
fibril_mutex_lock(&used_lock); |
/* |
* Since we can only free unlinked nodes, the index structure is not |
* present in the position hash (uph). We therefore hash it out from |
490,7 → 490,7 |
* the index hash only. |
*/ |
hash_table_remove(&ui_hash, ikey, 2); |
futex_up(&used_futex); |
fibril_mutex_unlock(&used_lock); |
/* Release the VFS index. */ |
fat_index_free(idx->dev_handle, idx->index); |
/* Deallocate the structure. */ |
524,12 → 524,12 |
if (!u) |
return ENOMEM; |
unused_initialize(u, dev_handle); |
futex_down(&unused_futex); |
fibril_mutex_lock(&unused_lock); |
if (!unused_find(dev_handle, false)) |
list_append(&u->link, &unused_head); |
else |
rc = EEXIST; |
futex_up(&unused_futex); |
fibril_mutex_unlock(&unused_lock); |
return rc; |
} |
540,7 → 540,7 |
u = unused_find(dev_handle, true); |
assert(u); |
list_remove(&u->link); |
futex_up(&unused_futex); |
fibril_mutex_unlock(&unused_lock); |
while (!list_empty(&u->freed_head)) { |
freed_t *f; |
/branches/network/uspace/srv/fs/fat/fat.h |
---|
35,6 → 35,7 |
#include "fat_fat.h" |
#include <ipc/ipc.h> |
#include <fibril_sync.h> |
#include <libfs.h> |
#include <atomic.h> |
#include <sys/types.h> |
160,7 → 161,7 |
/** Used indices (index) hash table link. */ |
link_t uih_link; |
futex_t lock; |
fibril_mutex_t lock; |
dev_handle_t dev_handle; |
fs_index_t index; |
/** |
178,7 → 179,10 |
/** FAT in-core node. */ |
typedef struct fat_node { |
futex_t lock; |
/** Back pointer to the FS node. */ |
fs_node_t *bp; |
fibril_mutex_t lock; |
fat_node_type_t type; |
fat_idx_t *idx; |
/** |
203,7 → 207,11 |
extern void fat_read(ipc_callid_t, ipc_call_t *); |
extern void fat_write(ipc_callid_t, ipc_call_t *); |
extern void fat_truncate(ipc_callid_t, ipc_call_t *); |
extern void fat_close(ipc_callid_t, ipc_call_t *); |
extern void fat_destroy(ipc_callid_t, ipc_call_t *); |
extern void fat_open_node(ipc_callid_t, ipc_call_t *); |
extern void fat_device(ipc_callid_t, ipc_call_t *); |
extern void fat_sync(ipc_callid_t, ipc_call_t *); |
extern fat_idx_t *fat_idx_get_new(dev_handle_t); |
extern fat_idx_t *fat_idx_get_by_pos(dev_handle_t, fat_cluster_t, unsigned); |
/branches/network/uspace/srv/fs/fat/fat_dentry.c |
---|
114,37 → 114,51 |
void fat_dentry_name_get(const fat_dentry_t *d, char *buf) |
{ |
int i; |
unsigned int i; |
for (i = 0; i < FAT_NAME_LEN; i++) { |
if (d->name[i] == FAT_PAD) |
break; |
if (d->name[i] == FAT_DENTRY_E5_ESC) |
*buf++ = 0xe5; |
else { |
if (d->lcase & FAT_LCASE_LOWER_NAME) |
*buf++ = tolower(d->name[i]); |
else |
*buf++ = d->name[i]; |
} |
} |
if (d->ext[0] != FAT_PAD) |
*buf++ = '.'; |
for (i = 0; i < FAT_EXT_LEN; i++) { |
if (d->ext[i] == FAT_PAD) { |
*buf = '\0'; |
return; |
} |
if (d->ext[i] == FAT_DENTRY_E5_ESC) |
*buf++ = 0xe5; |
else { |
if (d->lcase & FAT_LCASE_LOWER_EXT) |
*buf++ = tolower(d->ext[i]); |
else |
*buf++ = d->ext[i]; |
} |
} |
*buf = '\0'; |
} |
void fat_dentry_name_set(fat_dentry_t *d, const char *name) |
{ |
int i; |
unsigned int i; |
const char fake_ext[] = " "; |
bool lower_name = true; |
bool lower_ext = true; |
for (i = 0; i < FAT_NAME_LEN; i++) { |
switch ((uint8_t) *name) { |
case 0xe5: |
156,12 → 170,19 |
d->name[i] = FAT_PAD; |
break; |
default: |
if (isalpha(*name)) { |
if (!islower(*name)) |
lower_name = false; |
} |
d->name[i] = toupper(*name++); |
break; |
} |
} |
if (*name++ != '.') |
name = fake_ext; |
for (i = 0; i < FAT_EXT_LEN; i++) { |
switch ((uint8_t) *name) { |
case 0xe5: |
172,10 → 193,25 |
d->ext[i] = FAT_PAD; |
break; |
default: |
if (isalpha(*name)) { |
if (!islower(*name)) |
lower_ext = false; |
} |
d->ext[i] = toupper(*name++); |
break; |
} |
} |
if (lower_name) |
d->lcase |= FAT_LCASE_LOWER_NAME; |
else |
d->lcase &= ~FAT_LCASE_LOWER_NAME; |
if (lower_ext) |
d->lcase |= FAT_LCASE_LOWER_EXT; |
else |
d->lcase &= ~FAT_LCASE_LOWER_EXT; |
} |
fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d) |
/branches/network/uspace/srv/fs/fat/fat_dentry.h |
---|
47,6 → 47,9 |
#define FAT_ATTR_VOLLABEL (1 << 3) |
#define FAT_ATTR_SUBDIR (1 << 4) |
#define FAT_LCASE_LOWER_NAME 0x08 |
#define FAT_LCASE_LOWER_EXT 0x10 |
#define FAT_PAD ' ' |
#define FAT_DENTRY_UNUSED 0x00 |
65,7 → 68,7 |
uint8_t name[8]; |
uint8_t ext[3]; |
uint8_t attr; |
uint8_t reserved; |
uint8_t lcase; |
uint8_t ctime_fine; |
uint16_t ctime; |
uint16_t cdate; |
/branches/network/uspace/srv/fs/fat/fat_fat.c |
---|
45,14 → 45,15 |
#include <byteorder.h> |
#include <align.h> |
#include <assert.h> |
#include <futex.h> |
#include <fibril_sync.h> |
#include <mem.h> |
/** |
* The fat_alloc_lock futex protects all copies of the File Allocation Table |
* The fat_alloc_lock mutex protects all copies of the File Allocation Table |
* during allocation of clusters. The lock does not have to be held durring |
* deallocation of clusters. |
*/ |
static futex_t fat_alloc_lock = FUTEX_INITIALIZER; |
static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock); |
/** Walk the cluster chain. |
* |
325,7 → 326,7 |
/* |
* Search FAT1 for unused clusters. |
*/ |
futex_down(&fat_alloc_lock); |
fibril_mutex_lock(&fat_alloc_lock); |
for (b = 0, cl = 0; b < sf; b++) { |
blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NONE); |
for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
349,7 → 350,7 |
*mcl = lifo[found - 1]; |
*lcl = lifo[0]; |
free(lifo); |
futex_up(&fat_alloc_lock); |
fibril_mutex_unlock(&fat_alloc_lock); |
return EOK; |
} |
} |
356,7 → 357,7 |
} |
block_put(blk); |
} |
futex_up(&fat_alloc_lock); |
fibril_mutex_unlock(&fat_alloc_lock); |
/* |
* We could not find enough clusters. Now we need to free the clusters |
/branches/network/uspace/srv/fs/fat/fat.c |
---|
89,6 → 89,8 |
callid = async_get_call(&call); |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
return; |
case VFS_MOUNTED: |
fat_mounted(callid, &call); |
break; |
107,9 → 109,21 |
case VFS_TRUNCATE: |
fat_truncate(callid, &call); |
break; |
case VFS_CLOSE: |
fat_close(callid, &call); |
break; |
case VFS_DESTROY: |
fat_destroy(callid, &call); |
break; |
case VFS_OPEN_NODE: |
fat_open_node(callid, &call); |
break; |
case VFS_DEVICE: |
fat_device(callid, &call); |
break; |
case VFS_SYNC: |
fat_sync(callid, &call); |
break; |
default: |
ipc_answer_0(callid, ENOTSUP); |
break; |
/branches/network/uspace/srv/fs/fat/fat_ops.c |
---|
48,22 → 48,26 |
#include <errno.h> |
#include <string.h> |
#include <byteorder.h> |
#include <libadt/hash_table.h> |
#include <libadt/list.h> |
#include <adt/hash_table.h> |
#include <adt/list.h> |
#include <assert.h> |
#include <futex.h> |
#include <fibril_sync.h> |
#include <sys/mman.h> |
#include <align.h> |
/** Futex protecting the list of cached free FAT nodes. */ |
static futex_t ffn_futex = FUTEX_INITIALIZER; |
#define FAT_NODE(node) ((node) ? (fat_node_t *) (node)->data : NULL) |
#define FS_NODE(node) ((node) ? (node)->bp : NULL) |
/** Mutex protecting the list of cached free FAT nodes. */ |
static FIBRIL_MUTEX_INITIALIZE(ffn_mutex); |
/** List of cached free FAT nodes. */ |
static LIST_INITIALIZE(ffn_head); |
static void fat_node_initialize(fat_node_t *node) |
{ |
futex_initialize(&node->lock, 1); |
fibril_mutex_initialize(&node->lock); |
node->bp = NULL; |
node->idx = NULL; |
node->type = 0; |
link_initialize(&node->ffn_link); |
108,36 → 112,46 |
static fat_node_t *fat_node_get_new(void) |
{ |
fs_node_t *fn; |
fat_node_t *nodep; |
futex_down(&ffn_futex); |
fibril_mutex_lock(&ffn_mutex); |
if (!list_empty(&ffn_head)) { |
/* Try to use a cached free node structure. */ |
fat_idx_t *idxp_tmp; |
nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
if (!fibril_mutex_trylock(&nodep->lock)) |
goto skip_cache; |
idxp_tmp = nodep->idx; |
if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
futex_up(&nodep->lock); |
if (!fibril_mutex_trylock(&idxp_tmp->lock)) { |
fibril_mutex_unlock(&nodep->lock); |
goto skip_cache; |
} |
list_remove(&nodep->ffn_link); |
futex_up(&ffn_futex); |
fibril_mutex_unlock(&ffn_mutex); |
if (nodep->dirty) |
fat_node_sync(nodep); |
idxp_tmp->nodep = NULL; |
futex_up(&nodep->lock); |
futex_up(&idxp_tmp->lock); |
fibril_mutex_unlock(&nodep->lock); |
fibril_mutex_unlock(&idxp_tmp->lock); |
fn = FS_NODE(nodep); |
} else { |
skip_cache: |
/* Try to allocate a new node structure. */ |
futex_up(&ffn_futex); |
fibril_mutex_unlock(&ffn_mutex); |
fn = (fs_node_t *)malloc(sizeof(fs_node_t)); |
if (!fn) |
return NULL; |
nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
if (!nodep) |
if (!nodep) { |
free(fn); |
return NULL; |
} |
} |
fat_node_initialize(nodep); |
fs_node_initialize(fn); |
fn->data = nodep; |
nodep->bp = fn; |
return nodep; |
} |
146,7 → 160,7 |
* |
* @param idxp Locked index structure. |
*/ |
static void *fat_node_get_core(fat_idx_t *idxp) |
static fat_node_t *fat_node_get_core(fat_idx_t *idxp) |
{ |
block_t *b; |
fat_bs_t *bs; |
161,10 → 175,10 |
* We are lucky. |
* The node is already instantiated in memory. |
*/ |
futex_down(&idxp->nodep->lock); |
fibril_mutex_lock(&idxp->nodep->lock); |
if (!idxp->nodep->refcnt++) |
list_remove(&idxp->nodep->ffn_link); |
futex_up(&idxp->nodep->lock); |
fibril_mutex_unlock(&idxp->nodep->lock); |
return idxp->nodep; |
} |
223,21 → 237,21 |
/* |
* Forward declarations of FAT libfs operations. |
*/ |
static void *fat_node_get(dev_handle_t, fs_index_t); |
static void fat_node_put(void *); |
static void *fat_create_node(dev_handle_t, int); |
static int fat_destroy_node(void *); |
static int fat_link(void *, void *, const char *); |
static int fat_unlink(void *, void *); |
static void *fat_match(void *, const char *); |
static fs_index_t fat_index_get(void *); |
static size_t fat_size_get(void *); |
static unsigned fat_lnkcnt_get(void *); |
static bool fat_has_children(void *); |
static void *fat_root_get(dev_handle_t); |
static fs_node_t *fat_node_get(dev_handle_t, fs_index_t); |
static void fat_node_put(fs_node_t *); |
static fs_node_t *fat_create_node(dev_handle_t, int); |
static int fat_destroy_node(fs_node_t *); |
static int fat_link(fs_node_t *, fs_node_t *, const char *); |
static int fat_unlink(fs_node_t *, fs_node_t *, const char *); |
static fs_node_t *fat_match(fs_node_t *, const char *); |
static fs_index_t fat_index_get(fs_node_t *); |
static size_t fat_size_get(fs_node_t *); |
static unsigned fat_lnkcnt_get(fs_node_t *); |
static bool fat_has_children(fs_node_t *); |
static fs_node_t *fat_root_get(dev_handle_t); |
static char fat_plb_get_char(unsigned); |
static bool fat_is_directory(void *); |
static bool fat_is_file(void *node); |
static bool fat_is_directory(fs_node_t *); |
static bool fat_is_file(fs_node_t *node); |
/* |
* FAT libfs operations. |
244,9 → 258,9 |
*/ |
/** Instantiate a FAT in-core node. */ |
void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
fs_node_t *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
{ |
void *node; |
fat_node_t *nodep; |
fat_idx_t *idxp; |
idxp = fat_idx_get_by_index(dev_handle, index); |
253,22 → 267,22 |
if (!idxp) |
return NULL; |
/* idxp->lock held */ |
node = fat_node_get_core(idxp); |
futex_up(&idxp->lock); |
return node; |
nodep = fat_node_get_core(idxp); |
fibril_mutex_unlock(&idxp->lock); |
return FS_NODE(nodep); |
} |
void fat_node_put(void *node) |
void fat_node_put(fs_node_t *fn) |
{ |
fat_node_t *nodep = (fat_node_t *)node; |
fat_node_t *nodep = FAT_NODE(fn); |
bool destroy = false; |
futex_down(&nodep->lock); |
fibril_mutex_lock(&nodep->lock); |
if (!--nodep->refcnt) { |
if (nodep->idx) { |
futex_down(&ffn_futex); |
fibril_mutex_lock(&ffn_mutex); |
list_append(&nodep->ffn_link, &ffn_head); |
futex_up(&ffn_futex); |
fibril_mutex_unlock(&ffn_mutex); |
} else { |
/* |
* The node does not have any index structure associated |
279,12 → 293,14 |
destroy = true; |
} |
} |
futex_up(&nodep->lock); |
if (destroy) |
free(node); |
fibril_mutex_unlock(&nodep->lock); |
if (destroy) { |
free(nodep->bp); |
free(nodep); |
} |
} |
void *fat_create_node(dev_handle_t dev_handle, int flags) |
fs_node_t *fat_create_node(dev_handle_t dev_handle, int flags) |
{ |
fat_idx_t *idxp; |
fat_node_t *nodep; |
310,7 → 326,7 |
idxp = fat_idx_get_new(dev_handle); |
if (!idxp) { |
fat_free_clusters(bs, dev_handle, mcl); |
fat_node_put(nodep); |
fat_node_put(FS_NODE(nodep)); |
return NULL; |
} |
/* idxp->lock held */ |
344,13 → 360,13 |
nodep->idx = idxp; |
idxp->nodep = nodep; |
futex_up(&idxp->lock); |
return nodep; |
fibril_mutex_unlock(&idxp->lock); |
return FS_NODE(nodep); |
} |
int fat_destroy_node(void *node) |
int fat_destroy_node(fs_node_t *fn) |
{ |
fat_node_t *nodep = (fat_node_t *)node; |
fat_node_t *nodep = FAT_NODE(fn); |
fat_bs_t *bs; |
/* |
364,7 → 380,7 |
/* |
* The node may not have any children. |
*/ |
assert(fat_has_children(node) == false); |
assert(fat_has_children(fn) == false); |
bs = block_bb_get(nodep->idx->dev_handle); |
if (nodep->firstc != FAT_CLST_RES0) { |
374,14 → 390,15 |
} |
fat_idx_destroy(nodep->idx); |
free(nodep->bp); |
free(nodep); |
return EOK; |
} |
int fat_link(void *prnt, void *chld, const char *name) |
int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name) |
{ |
fat_node_t *parentp = (fat_node_t *)prnt; |
fat_node_t *childp = (fat_node_t *)chld; |
fat_node_t *parentp = FAT_NODE(pfn); |
fat_node_t *childp = FAT_NODE(cfn); |
fat_dentry_t *d; |
fat_bs_t *bs; |
block_t *b; |
392,16 → 409,16 |
fat_cluster_t mcl, lcl; |
int rc; |
futex_down(&childp->lock); |
fibril_mutex_lock(&childp->lock); |
if (childp->lnkcnt == 1) { |
/* |
* On FAT, we don't support multiple hard links. |
*/ |
futex_up(&childp->lock); |
fibril_mutex_unlock(&childp->lock); |
return EMLINK; |
} |
assert(childp->lnkcnt == 0); |
futex_up(&childp->lock); |
fibril_mutex_unlock(&childp->lock); |
if (!fat_dentry_name_verify(name)) { |
/* |
415,7 → 432,7 |
* a new one. |
*/ |
futex_down(&parentp->idx->lock); |
fibril_mutex_lock(&parentp->idx->lock); |
bs = block_bb_get(parentp->idx->dev_handle); |
bps = uint16_t_le2host(bs->bps); |
dps = bps / sizeof(fat_dentry_t); |
446,12 → 463,12 |
*/ |
if (parentp->idx->pfc == FAT_CLST_ROOT) { |
/* Can't grow the root directory. */ |
futex_up(&parentp->idx->lock); |
fibril_mutex_unlock(&parentp->idx->lock); |
return ENOSPC; |
} |
rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl); |
if (rc != EOK) { |
futex_up(&parentp->idx->lock); |
fibril_mutex_unlock(&parentp->idx->lock); |
return rc; |
} |
fat_append_clusters(bs, parentp, mcl); |
474,9 → 491,9 |
fat_dentry_name_set(d, name); |
b->dirty = true; /* need to sync block */ |
block_put(b); |
futex_up(&parentp->idx->lock); |
fibril_mutex_unlock(&parentp->idx->lock); |
futex_down(&childp->idx->lock); |
fibril_mutex_lock(&childp->idx->lock); |
/* |
* If possible, create the Sub-directory Identifier Entry and the |
512,12 → 529,12 |
childp->idx->pfc = parentp->firstc; |
childp->idx->pdi = i * dps + j; |
futex_up(&childp->idx->lock); |
fibril_mutex_unlock(&childp->idx->lock); |
futex_down(&childp->lock); |
fibril_mutex_lock(&childp->lock); |
childp->lnkcnt = 1; |
childp->dirty = true; /* need to sync node */ |
futex_up(&childp->lock); |
fibril_mutex_unlock(&childp->lock); |
/* |
* Hash in the index structure into the position hash. |
527,19 → 544,25 |
return EOK; |
} |
int fat_unlink(void *prnt, void *chld) |
int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
{ |
fat_node_t *parentp = (fat_node_t *)prnt; |
fat_node_t *childp = (fat_node_t *)chld; |
fat_node_t *parentp = FAT_NODE(pfn); |
fat_node_t *childp = FAT_NODE(cfn); |
fat_bs_t *bs; |
fat_dentry_t *d; |
uint16_t bps; |
block_t *b; |
futex_down(&parentp->lock); |
futex_down(&childp->lock); |
if (!parentp) |
return EBUSY; |
if (fat_has_children(cfn)) |
return ENOTEMPTY; |
fibril_mutex_lock(&parentp->lock); |
fibril_mutex_lock(&childp->lock); |
assert(childp->lnkcnt == 1); |
futex_down(&childp->idx->lock); |
fibril_mutex_lock(&childp->idx->lock); |
bs = block_bb_get(childp->idx->dev_handle); |
bps = uint16_t_le2host(bs->bps); |
558,19 → 581,19 |
/* clear position information */ |
childp->idx->pfc = FAT_CLST_RES0; |
childp->idx->pdi = 0; |
futex_up(&childp->idx->lock); |
fibril_mutex_unlock(&childp->idx->lock); |
childp->lnkcnt = 0; |
childp->dirty = true; |
futex_up(&childp->lock); |
futex_up(&parentp->lock); |
fibril_mutex_unlock(&childp->lock); |
fibril_mutex_unlock(&parentp->lock); |
return EOK; |
} |
void *fat_match(void *prnt, const char *component) |
fs_node_t *fat_match(fs_node_t *pfn, const char *component) |
{ |
fat_bs_t *bs; |
fat_node_t *parentp = (fat_node_t *)prnt; |
fat_node_t *parentp = FAT_NODE(pfn); |
char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
unsigned i, j; |
unsigned bps; /* bytes per sector */ |
579,7 → 602,7 |
fat_dentry_t *d; |
block_t *b; |
futex_down(&parentp->idx->lock); |
fibril_mutex_lock(&parentp->idx->lock); |
bs = block_bb_get(parentp->idx->dev_handle); |
bps = uint16_t_le2host(bs->bps); |
dps = bps / sizeof(fat_dentry_t); |
594,7 → 617,7 |
continue; |
case FAT_DENTRY_LAST: |
block_put(b); |
futex_up(&parentp->idx->lock); |
fibril_mutex_unlock(&parentp->idx->lock); |
return NULL; |
default: |
case FAT_DENTRY_VALID: |
603,7 → 626,7 |
} |
if (fat_dentry_namecmp(name, component) == 0) { |
/* hit */ |
void *node; |
fat_node_t *nodep; |
/* |
* Assume tree hierarchy for locking. We |
* already have the parent and now we are going |
613,7 → 636,7 |
fat_idx_t *idx = fat_idx_get_by_pos( |
parentp->idx->dev_handle, parentp->firstc, |
i * dps + j); |
futex_up(&parentp->idx->lock); |
fibril_mutex_unlock(&parentp->idx->lock); |
if (!idx) { |
/* |
* Can happen if memory is low or if we |
622,41 → 645,38 |
block_put(b); |
return NULL; |
} |
node = fat_node_get_core(idx); |
futex_up(&idx->lock); |
nodep = fat_node_get_core(idx); |
fibril_mutex_unlock(&idx->lock); |
block_put(b); |
return node; |
return FS_NODE(nodep); |
} |
} |
block_put(b); |
} |
futex_up(&parentp->idx->lock); |
fibril_mutex_unlock(&parentp->idx->lock); |
return NULL; |
} |
fs_index_t fat_index_get(void *node) |
fs_index_t fat_index_get(fs_node_t *fn) |
{ |
fat_node_t *fnodep = (fat_node_t *)node; |
if (!fnodep) |
return 0; |
return fnodep->idx->index; |
return FAT_NODE(fn)->idx->index; |
} |
size_t fat_size_get(void *node) |
size_t fat_size_get(fs_node_t *fn) |
{ |
return ((fat_node_t *)node)->size; |
return FAT_NODE(fn)->size; |
} |
unsigned fat_lnkcnt_get(void *node) |
unsigned fat_lnkcnt_get(fs_node_t *fn) |
{ |
return ((fat_node_t *)node)->lnkcnt; |
return FAT_NODE(fn)->lnkcnt; |
} |
bool fat_has_children(void *node) |
bool fat_has_children(fs_node_t *fn) |
{ |
fat_bs_t *bs; |
fat_node_t *nodep = (fat_node_t *)node; |
fat_node_t *nodep = FAT_NODE(fn); |
unsigned bps; |
unsigned dps; |
unsigned blocks; |
666,7 → 686,7 |
if (nodep->type != FAT_DIRECTORY) |
return false; |
futex_down(&nodep->idx->lock); |
fibril_mutex_lock(&nodep->idx->lock); |
bs = block_bb_get(nodep->idx->dev_handle); |
bps = uint16_t_le2host(bs->bps); |
dps = bps / sizeof(fat_dentry_t); |
685,26 → 705,26 |
continue; |
case FAT_DENTRY_LAST: |
block_put(b); |
futex_up(&nodep->idx->lock); |
fibril_mutex_unlock(&nodep->idx->lock); |
return false; |
default: |
case FAT_DENTRY_VALID: |
block_put(b); |
futex_up(&nodep->idx->lock); |
fibril_mutex_unlock(&nodep->idx->lock); |
return true; |
} |
block_put(b); |
futex_up(&nodep->idx->lock); |
fibril_mutex_unlock(&nodep->idx->lock); |
return true; |
} |
block_put(b); |
} |
futex_up(&nodep->idx->lock); |
fibril_mutex_unlock(&nodep->idx->lock); |
return false; |
} |
void *fat_root_get(dev_handle_t dev_handle) |
fs_node_t *fat_root_get(dev_handle_t dev_handle) |
{ |
return fat_node_get(dev_handle, 0); |
} |
714,14 → 734,14 |
return fat_reg.plb_ro[pos % PLB_SIZE]; |
} |
bool fat_is_directory(void *node) |
bool fat_is_directory(fs_node_t *fn) |
{ |
return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
return FAT_NODE(fn)->type == FAT_DIRECTORY; |
} |
bool fat_is_file(void *node) |
bool fat_is_file(fs_node_t *fn) |
{ |
return ((fat_node_t *)node)->type == FAT_FILE; |
return FAT_NODE(fn)->type == FAT_FILE; |
} |
/** libfs operations */ |
750,6 → 770,7 |
void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
enum cache_mode cmode; |
fat_bs_t *bs; |
uint16_t bps; |
uint16_t rde; |
777,6 → 798,12 |
} |
opts[size] = '\0'; |
/* Check for option enabling write through. */ |
if (str_cmp(opts, "wtcache") == 0) |
cmode = CACHE_MODE_WT; |
else |
cmode = CACHE_MODE_WB; |
/* initialize libblock */ |
rc = block_init(dev_handle, BS_SIZE); |
if (rc != EOK) { |
806,7 → 833,7 |
} |
/* Initialize the block cache */ |
rc = block_cache_init(dev_handle, bps, 0 /* XXX */); |
rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode); |
if (rc != EOK) { |
block_fini(dev_handle); |
ipc_answer_0(rid, rc); |
821,8 → 848,17 |
} |
/* Initialize the root node. */ |
fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t)); |
if (!rfn) { |
block_fini(dev_handle); |
fat_idx_fini_by_dev_handle(dev_handle); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
fs_node_initialize(rfn); |
fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
if (!rootp) { |
free(rfn); |
block_fini(dev_handle); |
fat_idx_fini_by_dev_handle(dev_handle); |
ipc_answer_0(rid, ENOMEM); |
832,8 → 868,9 |
fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
if (!ridxp) { |
free(rfn); |
free(rootp); |
block_fini(dev_handle); |
free(rootp); |
fat_idx_fini_by_dev_handle(dev_handle); |
ipc_answer_0(rid, ENOMEM); |
return; |
848,8 → 885,10 |
rootp->size = rde * sizeof(fat_dentry_t); |
rootp->idx = ridxp; |
ridxp->nodep = rootp; |
rootp->bp = rfn; |
rfn->data = rootp; |
futex_up(&ridxp->lock); |
fibril_mutex_unlock(&ridxp->lock); |
ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
} |
856,7 → 895,7 |
void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, ENOTSUP); |
libfs_mount(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
} |
void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
869,21 → 908,23 |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
off_t pos = (off_t)IPC_GET_ARG3(*request); |
fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
fs_node_t *fn = fat_node_get(dev_handle, index); |
fat_node_t *nodep; |
fat_bs_t *bs; |
uint16_t bps; |
size_t bytes; |
block_t *b; |
if (!nodep) { |
if (!fn) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
nodep = FAT_NODE(fn); |
ipc_callid_t callid; |
size_t len; |
if (!ipc_data_read_receive(&callid, &len)) { |
fat_node_put(nodep); |
fat_node_put(fn); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
954,7 → 995,7 |
bnum++; |
} |
miss: |
fat_node_put(nodep); |
fat_node_put(fn); |
ipc_answer_0(callid, ENOENT); |
ipc_answer_1(rid, ENOENT, 0); |
return; |
963,7 → 1004,7 |
bytes = (pos - spos) + 1; |
} |
fat_node_put(nodep); |
fat_node_put(fn); |
ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
} |
972,7 → 1013,8 |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
off_t pos = (off_t)IPC_GET_ARG3(*request); |
fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
fs_node_t *fn = fat_node_get(dev_handle, index); |
fat_node_t *nodep; |
fat_bs_t *bs; |
size_t bytes; |
block_t *b; |
982,15 → 1024,16 |
off_t boundary; |
int flags = BLOCK_FLAGS_NONE; |
if (!nodep) { |
if (!fn) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
nodep = FAT_NODE(fn); |
ipc_callid_t callid; |
size_t len; |
if (!ipc_data_write_receive(&callid, &len)) { |
fat_node_put(nodep); |
fat_node_put(fn); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
1031,7 → 1074,7 |
nodep->dirty = true; /* need to sync node */ |
} |
ipc_answer_2(rid, EOK, bytes, nodep->size); |
fat_node_put(nodep); |
fat_node_put(fn); |
return; |
} else { |
/* |
1047,7 → 1090,7 |
status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); |
if (status != EOK) { |
/* could not allocate a chain of nclsts clusters */ |
fat_node_put(nodep); |
fat_node_put(fn); |
ipc_answer_0(callid, status); |
ipc_answer_0(rid, status); |
return; |
1068,7 → 1111,7 |
nodep->size = pos + bytes; |
nodep->dirty = true; /* need to sync node */ |
ipc_answer_2(rid, EOK, bytes, nodep->size); |
fat_node_put(nodep); |
fat_node_put(fn); |
return; |
} |
} |
1078,7 → 1121,8 |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
size_t size = (off_t)IPC_GET_ARG3(*request); |
fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
fs_node_t *fn = fat_node_get(dev_handle, index); |
fat_node_t *nodep; |
fat_bs_t *bs; |
uint16_t bps; |
uint8_t spc; |
1085,10 → 1129,11 |
unsigned bpc; /* bytes per cluster */ |
int rc; |
if (!nodep) { |
if (!fn) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
nodep = FAT_NODE(fn); |
bs = block_bb_get(dev_handle); |
bps = uint16_t_le2host(bs->bps); |
1126,11 → 1171,16 |
nodep->dirty = true; /* need to sync node */ |
rc = EOK; |
} |
fat_node_put(nodep); |
fat_node_put(fn); |
ipc_answer_0(rid, rc); |
return; |
} |
void fat_close(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, EOK); |
} |
void fat_destroy(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
1137,16 → 1187,32 |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
int rc; |
fat_node_t *nodep = fat_node_get(dev_handle, index); |
if (!nodep) { |
fs_node_t *fn = fat_node_get(dev_handle, index); |
if (!fn) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
rc = fat_destroy_node(nodep); |
rc = fat_destroy_node(fn); |
ipc_answer_0(rid, rc); |
} |
void fat_open_node(ipc_callid_t rid, ipc_call_t *request) |
{ |
libfs_open_node(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
} |
void fat_device(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, ENOTSUP); |
} |
void fat_sync(ipc_callid_t rid, ipc_call_t *request) |
{ |
/* Dummy implementation */ |
ipc_answer_0(rid, EOK); |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/devmap/Makefile |
---|
34,8 → 34,6 |
include $(LIBC_PREFIX)/Makefile.toolchain |
CFLAGS += -I../libipc/include |
LIBS = $(LIBC_PREFIX)/libc.a |
## Sources |
/branches/network/uspace/srv/devmap/devmap.c |
---|
41,7 → 41,7 |
#include <stdio.h> |
#include <errno.h> |
#include <bool.h> |
#include <futex.h> |
#include <fibril_sync.h> |
#include <stdlib.h> |
#include <string.h> |
#include <ipc/devmap.h> |
48,60 → 48,71 |
#define NAME "devmap" |
/** Pending lookup structure. */ |
/** Representation of device driver. |
* |
* Each driver is responsible for a set of devices. |
* |
*/ |
typedef struct { |
link_t link; |
char *name; /**< Device name */ |
ipc_callid_t callid; /**< Call ID waiting for the lookup */ |
} pending_req_t; |
/** Pointers to previous and next drivers in linked list */ |
link_t drivers; |
/** Pointer to the linked list of devices controlled by this driver */ |
link_t devices; |
/** Phone asociated with this driver */ |
ipcarg_t phone; |
/** Device driver name */ |
char *name; |
/** Fibril mutex for list of devices owned by this driver */ |
fibril_mutex_t devices_mutex; |
} devmap_driver_t; |
/** Info about registered device |
* |
*/ |
typedef struct { |
/** Pointer to the previous and next device in the list of all devices */ |
link_t devices; |
/** Pointer to the previous and next device in the list of devices |
owned by one driver */ |
link_t driver_devices; |
/** Unique device identifier */ |
dev_handle_t handle; |
/** Device name */ |
char *name; |
/** Device driver handling this device */ |
devmap_driver_t *driver; |
} devmap_device_t; |
LIST_INITIALIZE(devices_list); |
LIST_INITIALIZE(drivers_list); |
LIST_INITIALIZE(pending_req); |
/* Locking order: |
* drivers_list_futex |
* devices_list_futex |
* (devmap_driver_t *)->devices_futex |
* create_handle_futex |
* drivers_list_mutex |
* devices_list_mutex |
* (devmap_driver_t *)->devices_mutex |
* create_handle_mutex |
**/ |
static atomic_t devices_list_futex = FUTEX_INITIALIZER; |
static atomic_t drivers_list_futex = FUTEX_INITIALIZER; |
static atomic_t create_handle_futex = FUTEX_INITIALIZER; |
static FIBRIL_MUTEX_INITIALIZE(devices_list_mutex); |
static FIBRIL_CONDVAR_INITIALIZE(devices_list_cv); |
static FIBRIL_MUTEX_INITIALIZE(drivers_list_mutex); |
static FIBRIL_MUTEX_INITIALIZE(create_handle_mutex); |
static int devmap_create_handle(void) |
static dev_handle_t last_handle = 0; |
static dev_handle_t devmap_create_handle(void) |
{ |
static int last_handle = 0; |
int handle; |
/* TODO: allow reusing old handles after their unregistration |
* and implement some version of LRU algorithm |
* and implement some version of LRU algorithm, avoid overflow |
*/ |
/* FIXME: overflow */ |
futex_down(&create_handle_futex); |
fibril_mutex_lock(&create_handle_mutex); |
last_handle++; |
fibril_mutex_unlock(&create_handle_mutex); |
last_handle += 1; |
handle = last_handle; |
futex_up(&create_handle_futex); |
return handle; |
return last_handle; |
} |
/** Initialize device mapper. |
* |
* |
*/ |
static int devmap_init() |
{ |
/* TODO: */ |
return EOK; |
} |
/** Find device with given name. |
* |
*/ |
112,7 → 123,7 |
while (item != &devices_list) { |
device = list_get_instance(item, devmap_device_t, devices); |
if (0 == str_cmp(device->name, name)) |
if (str_cmp(device->name, name) == 0) |
break; |
item = item->next; |
} |
129,9 → 140,9 |
* @todo: use hash table |
* |
*/ |
static devmap_device_t *devmap_device_find_handle(int handle) |
static devmap_device_t *devmap_device_find_handle(dev_handle_t handle) |
{ |
futex_down(&devices_list_futex); |
fibril_mutex_lock(&devices_list_mutex); |
link_t *item = (&devices_list)->next; |
devmap_device_t *device = NULL; |
144,22 → 155,20 |
} |
if (item == &devices_list) { |
futex_up(&devices_list_futex); |
fibril_mutex_unlock(&devices_list_mutex); |
return NULL; |
} |
device = list_get_instance(item, devmap_device_t, devices); |
futex_up(&devices_list_futex); |
fibril_mutex_unlock(&devices_list_mutex); |
return device; |
} |
/** |
* |
* Unregister device and free it. It's assumed that driver's device list is |
* already locked. |
* |
*/ |
static int devmap_device_unregister_core(devmap_device_t *device) |
{ |
173,10 → 182,8 |
} |
/** |
* |
* Read info about new driver and add it into linked list of registered |
* drivers. |
* |
*/ |
static void devmap_driver_register(devmap_driver_t **odriver) |
{ |
230,7 → 237,7 |
/* |
* Send confirmation to sender and get data into buffer. |
*/ |
if (EOK != ipc_data_write_finalize(callid, driver->name, name_size)) { |
if (ipc_data_write_finalize(callid, driver->name, name_size) != EOK) { |
free(driver->name); |
free(driver); |
ipc_answer_0(iid, EREFUSED); |
239,13 → 246,13 |
driver->name[name_size] = 0; |
/* Initialize futex for list of devices owned by this driver */ |
futex_initialize(&(driver->devices_futex), 1); |
/* Initialize mutex for list of devices owned by this driver */ |
fibril_mutex_initialize(&driver->devices_mutex); |
/* |
* Initialize list of asociated devices |
*/ |
list_initialize(&(driver->devices)); |
list_initialize(&driver->devices); |
/* |
* Create connection to the driver |
253,7 → 260,7 |
ipc_call_t call; |
callid = async_get_call(&call); |
if (IPC_M_CONNECT_TO_ME != IPC_GET_METHOD(call)) { |
if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) { |
ipc_answer_0(callid, ENOTSUP); |
free(driver->name); |
268,7 → 275,7 |
list_initialize(&(driver->drivers)); |
futex_down(&drivers_list_futex); |
fibril_mutex_lock(&drivers_list_mutex); |
/* TODO: |
* check that no driver with name equal to driver->name is registered |
278,7 → 285,7 |
* Insert new driver into list of registered drivers |
*/ |
list_append(&(driver->drivers), &drivers_list); |
futex_up(&drivers_list_futex); |
fibril_mutex_unlock(&drivers_list_mutex); |
ipc_answer_0(iid, EOK); |
295,18 → 302,18 |
if (driver == NULL) |
return EEXISTS; |
futex_down(&drivers_list_futex); |
fibril_mutex_lock(&drivers_list_mutex); |
if (driver->phone != 0) |
ipc_hangup(driver->phone); |
/* remove it from list of drivers */ |
/* Remove it from list of drivers */ |
list_remove(&(driver->drivers)); |
/* unregister all its devices */ |
/* Unregister all its devices */ |
fibril_mutex_lock(&devices_list_mutex); |
fibril_mutex_lock(&driver->devices_mutex); |
futex_down(&devices_list_futex); |
futex_down(&(driver->devices_futex)); |
while (!list_empty(&(driver->devices))) { |
devmap_device_t *device = list_get_instance(driver->devices.next, |
devmap_device_t, driver_devices); |
313,12 → 320,12 |
devmap_device_unregister_core(device); |
} |
futex_up(&(driver->devices_futex)); |
futex_up(&devices_list_futex); |
futex_up(&drivers_list_futex); |
fibril_mutex_unlock(&driver->devices_mutex); |
fibril_mutex_unlock(&devices_list_mutex); |
fibril_mutex_unlock(&drivers_list_mutex); |
/* free name and driver */ |
if (NULL != driver->name) |
if (driver->name != NULL) |
free(driver->name); |
free(driver); |
326,30 → 333,6 |
return EOK; |
} |
/** Process pending lookup requests */ |
static void process_pending_lookup() |
{ |
link_t *cur; |
loop: |
for (cur = pending_req.next; cur != &pending_req; cur = cur->next) { |
pending_req_t *pr = list_get_instance(cur, pending_req_t, link); |
const devmap_device_t *dev = devmap_device_find_name(pr->name); |
if (!dev) |
continue; |
ipc_answer_1(pr->callid, EOK, dev->handle); |
free(pr->name); |
list_remove(cur); |
free(pr); |
goto loop; |
} |
} |
/** Register instance of device |
* |
*/ |
400,12 → 383,12 |
list_initialize(&(device->devices)); |
list_initialize(&(device->driver_devices)); |
futex_down(&devices_list_futex); |
fibril_mutex_lock(&devices_list_mutex); |
/* Check that device with such name is not already registered */ |
if (NULL != devmap_device_find_name(device->name)) { |
printf(NAME ": Device '%s' already registered\n", device->name); |
futex_up(&devices_list_futex); |
fibril_mutex_unlock(&devices_list_mutex); |
free(device->name); |
free(device); |
ipc_answer_0(iid, EEXISTS); |
421,16 → 404,15 |
list_append(&device->devices, &devices_list); |
/* Insert device into list of devices that belog to one driver */ |
futex_down(&device->driver->devices_futex); |
fibril_mutex_lock(&device->driver->devices_mutex); |
list_append(&device->driver_devices, &device->driver->devices); |
futex_up(&device->driver->devices_futex); |
futex_up(&devices_list_futex); |
fibril_mutex_unlock(&device->driver->devices_mutex); |
fibril_condvar_broadcast(&devices_list_cv); |
fibril_mutex_unlock(&devices_list_mutex); |
ipc_answer_1(iid, EOK, device->handle); |
process_pending_lookup(); |
} |
/** |
454,15 → 436,15 |
/* |
* Get handle from request |
*/ |
int handle = IPC_GET_ARG2(*call); |
dev_handle_t handle = IPC_GET_ARG2(*call); |
devmap_device_t *dev = devmap_device_find_handle(handle); |
if (NULL == dev) { |
if ((dev == NULL) || (dev->driver == NULL) || (dev->driver->phone == 0)) { |
ipc_answer_0(callid, ENOENT); |
return; |
} |
ipc_forward_fast(callid, dev->driver->phone, (ipcarg_t)(dev->handle), |
ipc_forward_fast(callid, dev->driver->phone, dev->handle, |
IPC_GET_ARG3(*call), 0, IPC_FF_NONE); |
} |
495,7 → 477,7 |
/* |
* Allocate buffer for device name. |
*/ |
char *name = (char *) malloc(size); |
char *name = (char *) malloc(size + 1); |
if (name == NULL) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(iid, EREFUSED); |
513,10 → 495,14 |
} |
name[size] = '\0'; |
fibril_mutex_lock(&devices_list_mutex); |
const devmap_device_t *dev; |
recheck: |
/* |
* Find device name in linked list of known devices. |
* Find device name in the list of known devices. |
*/ |
const devmap_device_t *dev = devmap_device_find_name(name); |
dev = devmap_device_find_name(name); |
/* |
* Device was not found. |
523,24 → 509,18 |
*/ |
if (dev == NULL) { |
if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) { |
/* Blocking lookup, add to pending list */ |
pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t)); |
if (!pr) { |
ipc_answer_0(iid, ENOMEM); |
free(name); |
return; |
/* Blocking lookup */ |
fibril_condvar_wait(&devices_list_cv, |
&devices_list_mutex); |
goto recheck; |
} |
pr->name = name; |
pr->callid = iid; |
list_append(&pr->link, &pending_req); |
return; |
} |
ipc_answer_0(iid, ENOENT); |
free(name); |
fibril_mutex_unlock(&devices_list_mutex); |
return; |
} |
fibril_mutex_unlock(&devices_list_mutex); |
ipc_answer_1(iid, EOK, dev->handle); |
free(name); |
579,6 → 559,99 |
/* TODO: send name in response */ |
} |
static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall) |
{ |
fibril_mutex_lock(&devices_list_mutex); |
ipc_answer_1(iid, EOK, list_count(&devices_list)); |
fibril_mutex_unlock(&devices_list_mutex); |
} |
static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall) |
{ |
fibril_mutex_lock(&devices_list_mutex); |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_read_receive(&callid, &size)) { |
ipc_answer_0(callid, EREFUSED); |
ipc_answer_0(iid, EREFUSED); |
return; |
} |
if ((size % sizeof(dev_desc_t)) != 0) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(iid, EREFUSED); |
return; |
} |
size_t count = size / sizeof(dev_desc_t); |
dev_desc_t *desc = (dev_desc_t *) malloc(size); |
if (desc == NULL) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(iid, EREFUSED); |
return; |
} |
size_t pos = 0; |
link_t *item = devices_list.next; |
while ((item != &devices_list) && (pos < count)) { |
devmap_device_t *device = list_get_instance(item, devmap_device_t, devices); |
desc[pos].handle = device->handle; |
str_cpy(desc[pos].name, DEVMAP_NAME_MAXLEN, device->name); |
pos++; |
item = item->next; |
} |
ipcarg_t retval = ipc_data_read_finalize(callid, desc, pos * sizeof(dev_desc_t)); |
if (retval != EOK) { |
ipc_answer_0(iid, EREFUSED); |
free(desc); |
return; |
} |
free(desc); |
fibril_mutex_unlock(&devices_list_mutex); |
ipc_answer_1(iid, EOK, pos); |
} |
/** Initialize device mapper. |
* |
* |
*/ |
static bool devmap_init(void) |
{ |
/* Create NULL device entry */ |
devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t)); |
if (device == NULL) |
return false; |
device->name = str_dup("null"); |
if (device->name == NULL) { |
free(device); |
return false; |
} |
list_initialize(&(device->devices)); |
list_initialize(&(device->driver_devices)); |
fibril_mutex_lock(&devices_list_mutex); |
/* Get unique device handle */ |
device->handle = devmap_create_handle(); |
device->driver = NULL; |
/* Insert device into list of all devices */ |
list_append(&device->devices, &devices_list); |
fibril_mutex_unlock(&devices_list_mutex); |
return true; |
} |
/** Handle connection with device driver. |
* |
*/ |
601,7 → 674,6 |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
cont = false; |
/* Exit thread */ |
continue; |
case DEVMAP_DRIVER_UNREGISTER: |
if (NULL == driver) |
621,7 → 693,7 |
devmap_get_handle(callid, &call); |
break; |
case DEVMAP_DEVICE_GET_NAME: |
devmap_get_handle(callid, &call); |
devmap_get_name(callid, &call); |
break; |
default: |
if (!(callid & IPC_CALLID_NOTIFICATION)) |
654,15 → 726,19 |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
cont = false; |
/* Exit thread */ |
continue; |
case DEVMAP_DEVICE_GET_HANDLE: |
devmap_get_handle(callid, &call); |
break; |
case DEVMAP_DEVICE_GET_NAME: |
/* TODO */ |
devmap_get_name(callid, &call); |
break; |
case DEVMAP_DEVICE_GET_COUNT: |
devmap_get_count(callid, &call); |
break; |
case DEVMAP_DEVICE_GET_DEVICES: |
devmap_get_devices(callid, &call); |
break; |
default: |
if (!(callid & IPC_CALLID_NOTIFICATION)) |
ipc_answer_0(callid, ENOENT); |
700,7 → 776,7 |
{ |
printf(NAME ": HelenOS Device Mapper\n"); |
if (devmap_init() != 0) { |
if (!devmap_init()) { |
printf(NAME ": Error while initializing service\n"); |
return -1; |
} |
/branches/network/uspace/srv/cir/obio/Makefile |
---|
0,0 → 1,76 |
# |
# Copyright (c) 2006 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
LIBS = $(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = obio |
SOURCES = \ |
obio.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |
/branches/network/uspace/srv/cir/obio/obio.c |
---|
0,0 → 1,158 |
/* |
* Copyright (c) 2009 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup obio |
* @{ |
*/ |
/** |
* @file obio.c |
* @brief OBIO driver. |
* |
* OBIO is a short for on-board I/O. On UltraSPARC IIi and systems with U2P, |
* there is a piece of the root PCI bus controller address space, which |
* contains interrupt mapping and clear registers for all on-board devices. |
* Although UltraSPARC IIi and U2P are different in general, these registers can |
* be found at the same addresses. |
*/ |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <ipc/bus.h> |
#include <ipc/ns.h> |
#include <sysinfo.h> |
#include <as.h> |
#include <ddi.h> |
#include <align.h> |
#include <bool.h> |
#include <errno.h> |
#include <async.h> |
#include <align.h> |
#include <async.h> |
#include <stdio.h> |
#include <ipc/devmap.h> |
#define NAME "obio" |
#define OBIO_SIZE 0x1898 |
#define OBIO_IMR_BASE 0x200 |
#define OBIO_IMR(ino) (OBIO_IMR_BASE + ((ino) & INO_MASK)) |
#define OBIO_CIR_BASE 0x300 |
#define OBIO_CIR(ino) (OBIO_CIR_BASE + ((ino) & INO_MASK)) |
#define INO_MASK 0x1f |
static void *base_phys; |
static volatile uint64_t *base_virt; |
/** Handle one connection to obio. |
* |
* @param iid Hash of the request that opened the connection. |
* @param icall Call data of the request that opened the connection. |
*/ |
static void obio_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
ipc_callid_t callid; |
ipc_call_t call; |
/* |
* Answer the first IPC_M_CONNECT_ME_TO call. |
*/ |
ipc_answer_0(iid, EOK); |
while (1) { |
int inr; |
callid = async_get_call(&call); |
switch (IPC_GET_METHOD(call)) { |
case BUS_CLEAR_INTERRUPT: |
inr = IPC_GET_ARG1(call); |
base_virt[OBIO_CIR(inr & INO_MASK)] = 0; |
ipc_answer_0(callid, EOK); |
break; |
default: |
ipc_answer_0(callid, EINVAL); |
break; |
} |
} |
} |
/** Initialize the OBIO driver. |
* |
* So far, the driver heavily depends on information provided by the kernel via |
* sysinfo. In the future, there should be a standalone OBIO driver. |
*/ |
static bool obio_init(void) |
{ |
ipcarg_t phonead; |
base_phys = (void *) sysinfo_value("obio.base.physical"); |
if (!base_phys) { |
printf(NAME ": no OBIO registers found\n"); |
return false; |
} |
base_virt = as_get_mappable_page(OBIO_SIZE); |
int flags = AS_AREA_READ | AS_AREA_WRITE; |
int retval = physmem_map(base_phys, (void *) base_virt, |
ALIGN_UP(OBIO_SIZE, PAGE_SIZE) >> PAGE_WIDTH, flags); |
if (retval < 0) { |
printf(NAME ": Error mapping OBIO registers\n"); |
return false; |
} |
printf(NAME ": OBIO registers with base at %p\n", base_phys); |
async_set_client_connection(obio_connection); |
ipc_connect_to_me(PHONE_NS, SERVICE_OBIO, 0, 0, &phonead); |
return true; |
} |
int main(int argc, char **argv) |
{ |
printf(NAME ": HelenOS OBIO driver\n"); |
if (!obio_init()) |
return -1; |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
/* Never reached */ |
return 0; |
} |
/** |
* @} |
*/ |
Property changes: |
Added: svn:mergeinfo |
/branches/network/uspace/srv/cir/obio |
---|
Property changes: |
Added: svn:mergeinfo |
/branches/network/uspace/srv/cir/fhc/Makefile |
---|
0,0 → 1,76 |
# |
# Copyright (c) 2006 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
LIBS = $(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = fhc |
SOURCES = \ |
fhc.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |
/branches/network/uspace/srv/cir/fhc/fhc.c |
---|
0,0 → 1,157 |
/* |
* Copyright (c) 2009 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fhc |
* @{ |
*/ |
/** |
* @file fhc.c |
* @brief FHC bus controller driver. |
*/ |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <ipc/bus.h> |
#include <ipc/ns.h> |
#include <sysinfo.h> |
#include <as.h> |
#include <ddi.h> |
#include <align.h> |
#include <bool.h> |
#include <errno.h> |
#include <async.h> |
#include <align.h> |
#include <async.h> |
#include <stdio.h> |
#include <ipc/devmap.h> |
#define NAME "fhc" |
#define FHC_UART_INR 0x39 |
#define FHC_UART_IMAP 0x0 |
#define FHC_UART_ICLR 0x4 |
static void *fhc_uart_phys; |
static volatile uint32_t *fhc_uart_virt; |
static size_t fhc_uart_size; |
/** Handle one connection to fhc. |
* |
* @param iid Hash of the request that opened the connection. |
* @param icall Call data of the request that opened the connection. |
*/ |
static void fhc_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
ipc_callid_t callid; |
ipc_call_t call; |
/* |
* Answer the first IPC_M_CONNECT_ME_TO call. |
*/ |
ipc_answer_0(iid, EOK); |
while (1) { |
int inr; |
callid = async_get_call(&call); |
switch (IPC_GET_METHOD(call)) { |
case BUS_CLEAR_INTERRUPT: |
inr = IPC_GET_ARG1(call); |
switch (inr) { |
case FHC_UART_INR: |
fhc_uart_virt[FHC_UART_ICLR] = 0; |
ipc_answer_0(callid, EOK); |
break; |
default: |
ipc_answer_0(callid, ENOTSUP); |
break; |
} |
break; |
default: |
ipc_answer_0(callid, EINVAL); |
break; |
} |
} |
} |
/** Initialize the FHC driver. |
* |
* So far, the driver heavily depends on information provided by the kernel via |
* sysinfo. In the future, there should be a standalone FHC driver. |
*/ |
static bool fhc_init(void) |
{ |
ipcarg_t phonead; |
fhc_uart_size = sysinfo_value("fhc.uart.size"); |
fhc_uart_phys = (void *) sysinfo_value("fhc.uart.physical"); |
if (!fhc_uart_size) { |
printf(NAME ": no FHC UART registers found\n"); |
return false; |
} |
fhc_uart_virt = as_get_mappable_page(fhc_uart_size); |
int flags = AS_AREA_READ | AS_AREA_WRITE; |
int retval = physmem_map(fhc_uart_phys, (void *) fhc_uart_virt, |
ALIGN_UP(fhc_uart_size, PAGE_SIZE) >> PAGE_WIDTH, flags); |
if (retval < 0) { |
printf(NAME ": Error mapping FHC UART registers\n"); |
return false; |
} |
printf(NAME ": FHC UART registers at %p, %d bytes\n", fhc_uart_phys, |
fhc_uart_size); |
async_set_client_connection(fhc_connection); |
ipc_connect_to_me(PHONE_NS, SERVICE_FHC, 0, 0, &phonead); |
return true; |
} |
int main(int argc, char **argv) |
{ |
printf(NAME ": HelenOS FHC bus controller driver\n"); |
if (!fhc_init()) |
return -1; |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
/* Never reached */ |
return 0; |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/vfs/vfs.c |
---|
43,7 → 43,6 |
#include <bool.h> |
#include <string.h> |
#include <as.h> |
#include <libadt/list.h> |
#include <atomic.h> |
#include "vfs.h" |
80,28 → 79,9 |
case IPC_M_PHONE_HUNGUP: |
keep_on_going = false; |
break; |
case IPC_M_CONNECT_ME_TO: |
/* |
* Connect the client file system to another one. |
*/ |
/* FIXME: |
* Prevent ordinary clients from connecting to file |
* system servers directly. This should be solved by |
* applying some security mechanisms. |
*/ |
fs_handle = IPC_GET_ARG1(call); |
phone = vfs_grab_phone(fs_handle); |
(void) ipc_forward_fast(callid, phone, 0, 0, 0, |
IPC_FF_NONE); |
vfs_release_phone(phone); |
break; |
case VFS_REGISTER: |
vfs_register(callid, &call); |
/* |
* Keep the connection open so that a file system can |
* later ask us to connect it to another file system. |
* This is necessary to support non-root mounts. |
*/ |
keep_on_going = false; |
break; |
case VFS_MOUNT: |
vfs_mount(callid, &call); |
109,6 → 89,9 |
case VFS_OPEN: |
vfs_open(callid, &call); |
break; |
case VFS_OPEN_NODE: |
vfs_open_node(callid, &call); |
break; |
case VFS_CLOSE: |
vfs_close(callid, &call); |
break; |
133,6 → 116,15 |
case VFS_RENAME: |
vfs_rename(callid, &call); |
break; |
case VFS_DEVICE: |
vfs_device(callid, &call); |
break; |
case VFS_SYNC: |
vfs_sync(callid, &call); |
break; |
case VFS_NODE: |
vfs_node(callid, &call); |
break; |
default: |
ipc_answer_0(callid, ENOTSUP); |
break; |
147,11 → 139,6 |
printf(NAME ": HelenOS VFS server\n"); |
/* |
* Initialize the list of registered file systems. |
*/ |
list_initialize(&fs_head); |
/* |
* Initialize VFS node hash table. |
*/ |
if (!vfs_nodes_init()) { |
162,7 → 149,6 |
/* |
* Allocate and initialize the Path Lookup Buffer. |
*/ |
list_initialize(&plb_head); |
plb = as_get_mappable_page(PLB_SIZE); |
if (!plb) { |
printf(NAME ": Cannot allocate a mappable piece of address space\n"); |
177,7 → 163,7 |
memset(plb, 0, PLB_SIZE); |
/* |
* Set a connectio handling function/fibril. |
* Set a connection handling function/fibril. |
*/ |
async_set_client_connection(vfs_connection); |
/branches/network/uspace/srv/vfs/vfs_ops.c |
---|
43,9 → 43,8 |
#include <stdlib.h> |
#include <string.h> |
#include <bool.h> |
#include <futex.h> |
#include <rwlock.h> |
#include <libadt/list.h> |
#include <fibril_sync.h> |
#include <adt/list.h> |
#include <unistd.h> |
#include <ctype.h> |
#include <fcntl.h> |
55,26 → 54,12 |
/* Forward declarations of static functions. */ |
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t); |
/** Pending mount structure. */ |
typedef struct { |
link_t link; |
char *fs_name; /**< File system name */ |
char *mp; /**< Mount point */ |
char *opts; /**< Mount options. */ |
ipc_callid_t callid; /**< Call ID waiting for the mount */ |
ipc_callid_t rid; /**< Request ID */ |
dev_handle_t dev_handle; /**< Device handle */ |
} pending_req_t; |
LIST_INITIALIZE(pending_req); |
/** |
* This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
* concurrent VFS operation which modifies the file system namespace. |
*/ |
RWLOCK_INITIALIZE(namespace_rwlock); |
FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock); |
futex_t rootfs_futex = FUTEX_INITIALIZER; |
vfs_pair_t rootfs = { |
.fs_handle = 0, |
.dev_handle = 0 |
84,7 → 69,12 |
fs_handle_t fs_handle, char *mp, char *opts) |
{ |
vfs_lookup_res_t mp_res; |
vfs_lookup_res_t mr_res; |
vfs_node_t *mp_node = NULL; |
vfs_node_t *mr_node; |
fs_index_t rindex; |
size_t rsize; |
unsigned rlnkcnt; |
ipcarg_t rc; |
int phone; |
aid_t msg; |
91,14 → 81,12 |
ipc_call_t answer; |
/* Resolve the path to the mountpoint. */ |
futex_down(&rootfs_futex); |
fibril_rwlock_write_lock(&namespace_rwlock); |
if (rootfs.fs_handle) { |
/* We already have the root FS. */ |
rwlock_write_lock(&namespace_rwlock); |
if (str_cmp(mp, "/") == 0) { |
/* Trying to mount root FS over root FS */ |
rwlock_write_unlock(&namespace_rwlock); |
futex_up(&rootfs_futex); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, EBUSY); |
return; |
} |
106,8 → 94,7 |
rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL); |
if (rc != EOK) { |
/* The lookup failed for some reason. */ |
rwlock_write_unlock(&namespace_rwlock); |
futex_up(&rootfs_futex); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
114,8 → 101,7 |
mp_node = vfs_node_get(&mp_res); |
if (!mp_node) { |
rwlock_write_unlock(&namespace_rwlock); |
futex_up(&rootfs_futex); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
125,16 → 111,9 |
* It will be dropped upon the corresponding VFS_UNMOUNT. |
* This prevents the mount point from being deleted. |
*/ |
rwlock_write_unlock(&namespace_rwlock); |
} else { |
/* We still don't have the root file system mounted. */ |
if (str_cmp(mp, "/") == 0) { |
vfs_lookup_res_t mr_res; |
vfs_node_t *mr_node; |
fs_index_t rindex; |
size_t rsize; |
unsigned rlnkcnt; |
/* |
* For this simple, but important case, |
* we are almost done. |
150,7 → 129,7 |
if (rc != EOK) { |
async_wait_for(msg, NULL); |
vfs_release_phone(phone); |
futex_up(&rootfs_futex); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
158,7 → 137,7 |
vfs_release_phone(phone); |
if (rc != EOK) { |
futex_up(&rootfs_futex); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
176,12 → 155,12 |
rootfs.fs_handle = fs_handle; |
rootfs.dev_handle = dev_handle; |
futex_up(&rootfs_futex); |
/* Add reference to the mounted root. */ |
mr_node = vfs_node_get(&mr_res); |
assert(mr_node); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} else { |
189,12 → 168,11 |
* We can't resolve this without the root filesystem |
* being mounted first. |
*/ |
futex_up(&rootfs_futex); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
} |
futex_up(&rootfs_futex); |
/* |
* At this point, we have all necessary pieces: file system and device |
201,6 → 179,9 |
* handles, and we know the mount point VFS node. |
*/ |
int mountee_phone = vfs_grab_phone(fs_handle); |
assert(mountee_phone >= 0); |
phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
msg = async_send_4(phone, VFS_MOUNT, |
(ipcarg_t) mp_res.triplet.dev_handle, |
207,6 → 188,23 |
(ipcarg_t) mp_res.triplet.index, |
(ipcarg_t) fs_handle, |
(ipcarg_t) dev_handle, &answer); |
/* send connection */ |
rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone); |
if (rc != EOK) { |
async_wait_for(msg, NULL); |
vfs_release_phone(mountee_phone); |
vfs_release_phone(phone); |
/* Mount failed, drop reference to mp_node. */ |
if (mp_node) |
vfs_node_put(mp_node); |
ipc_answer_0(rid, rc); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
return; |
} |
vfs_release_phone(mountee_phone); |
/* send the mount options */ |
rc = ipc_data_write_start(phone, (void *)opts, str_size(opts)); |
if (rc != EOK) { |
215,6 → 213,7 |
/* Mount failed, drop reference to mp_node. */ |
if (mp_node) |
vfs_node_put(mp_node); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
221,7 → 220,22 |
async_wait_for(msg, &rc); |
vfs_release_phone(phone); |
if (rc != EOK) { |
if (rc == EOK) { |
rindex = (fs_index_t) IPC_GET_ARG1(answer); |
rsize = (size_t) IPC_GET_ARG2(answer); |
rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
mr_res.triplet.fs_handle = fs_handle; |
mr_res.triplet.dev_handle = dev_handle; |
mr_res.triplet.index = rindex; |
mr_res.size = rsize; |
mr_res.lnkcnt = rlnkcnt; |
mr_res.type = VFS_NODE_DIRECTORY; |
/* Add reference to the mounted root. */ |
mr_node = vfs_node_get(&mr_res); |
assert(mr_node); |
} else { |
/* Mount failed, drop reference to mp_node. */ |
if (mp_node) |
vfs_node_put(mp_node); |
228,37 → 242,9 |
} |
ipc_answer_0(rid, rc); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
} |
/** Process pending mount requests */ |
void vfs_process_pending_mount() |
{ |
link_t *cur; |
loop: |
for (cur = pending_req.next; cur != &pending_req; cur = cur->next) { |
pending_req_t *pr = list_get_instance(cur, pending_req_t, link); |
fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true); |
if (!fs_handle) |
continue; |
/* Acknowledge that we know fs_name. */ |
ipc_answer_0(pr->callid, EOK); |
/* Do the mount */ |
vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp, |
pr->opts); |
free(pr->fs_name); |
free(pr->mp); |
free(pr->opts); |
list_remove(cur); |
free(pr); |
goto loop; |
} |
} |
void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
{ |
/* |
412,33 → 398,17 |
* Check if we know a file system with the same name as is in fs_name. |
* This will also give us its file system handle. |
*/ |
fs_handle_t fs_handle = fs_name_to_handle(fs_name, true); |
fibril_mutex_lock(&fs_head_lock); |
fs_handle_t fs_handle; |
recheck: |
fs_handle = fs_name_to_handle(fs_name, false); |
if (!fs_handle) { |
if (flags & IPC_FLAG_BLOCKING) { |
pending_req_t *pr; |
/* Blocking mount, add to pending list */ |
pr = (pending_req_t *) malloc(sizeof(pending_req_t)); |
if (!pr) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
free(mp); |
free(fs_name); |
free(opts); |
return; |
fibril_condvar_wait(&fs_head_cv, &fs_head_lock); |
goto recheck; |
} |
pr->fs_name = fs_name; |
pr->mp = mp; |
pr->opts = opts; |
pr->callid = callid; |
pr->rid = rid; |
pr->dev_handle = dev_handle; |
link_initialize(&pr->link); |
list_append(&pr->link, &pending_req); |
return; |
} |
fibril_mutex_unlock(&fs_head_lock); |
ipc_answer_0(callid, ENOENT); |
ipc_answer_0(rid, ENOENT); |
free(mp); |
446,6 → 416,7 |
free(opts); |
return; |
} |
fibril_mutex_unlock(&fs_head_lock); |
/* Acknowledge that we know fs_name. */ |
ipc_answer_0(callid, EOK); |
479,10 → 450,11 |
/* |
* Make sure that we are called with exactly one of L_FILE and |
* L_DIRECTORY. |
* L_DIRECTORY. Make sure that the user does not pass L_OPEN. |
*/ |
if ((lflag & (L_FILE | L_DIRECTORY)) == 0 || |
(lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) { |
if (((lflag & (L_FILE | L_DIRECTORY)) == 0) || |
((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) || |
((lflag & L_OPEN) != 0)) { |
ipc_answer_0(rid, EINVAL); |
return; |
} |
493,12 → 465,12 |
lflag |= L_EXCLUSIVE; |
ipc_callid_t callid; |
if (!ipc_data_write_receive(&callid, &len)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
char *path = malloc(len + 1); |
if (!path) { |
ipc_answer_0(callid, ENOMEM); |
505,6 → 477,7 |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
int rc; |
if ((rc = ipc_data_write_finalize(callid, path, len))) { |
ipc_answer_0(rid, rc); |
519,18 → 492,18 |
* triplet. |
*/ |
if (lflag & L_CREATE) |
rwlock_write_lock(&namespace_rwlock); |
fibril_rwlock_write_lock(&namespace_rwlock); |
else |
rwlock_read_lock(&namespace_rwlock); |
fibril_rwlock_read_lock(&namespace_rwlock); |
/* The path is now populated and we can call vfs_lookup_internal(). */ |
vfs_lookup_res_t lr; |
rc = vfs_lookup_internal(path, lflag, &lr, NULL); |
if (rc) { |
rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL); |
if (rc != EOK) { |
if (lflag & L_CREATE) |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
else |
rwlock_read_unlock(&namespace_rwlock); |
fibril_rwlock_read_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
free(path); |
return; |
541,18 → 514,18 |
vfs_node_t *node = vfs_node_get(&lr); |
if (lflag & L_CREATE) |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
else |
rwlock_read_unlock(&namespace_rwlock); |
fibril_rwlock_read_unlock(&namespace_rwlock); |
/* Truncate the file if requested and if necessary. */ |
if (oflag & O_TRUNC) { |
rwlock_write_lock(&node->contents_rwlock); |
fibril_rwlock_write_lock(&node->contents_rwlock); |
if (node->size) { |
rc = vfs_truncate_internal(node->fs_handle, |
node->dev_handle, node->index, 0); |
if (rc) { |
rwlock_write_unlock(&node->contents_rwlock); |
fibril_rwlock_write_unlock(&node->contents_rwlock); |
vfs_node_put(node); |
ipc_answer_0(rid, rc); |
return; |
559,7 → 532,7 |
} |
node->size = 0; |
} |
rwlock_write_unlock(&node->contents_rwlock); |
fibril_rwlock_write_unlock(&node->contents_rwlock); |
} |
/* |
591,13 → 564,205 |
ipc_answer_1(rid, EOK, fd); |
} |
void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
void vfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
{ |
// FIXME: check for sanity of the supplied fs, dev and index |
if (!vfs_files_init()) { |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
/* |
* The interface is open_node(fs, dev, index, oflag). |
*/ |
vfs_lookup_res_t lr; |
lr.triplet.fs_handle = IPC_GET_ARG1(*request); |
lr.triplet.dev_handle = IPC_GET_ARG2(*request); |
lr.triplet.index = IPC_GET_ARG3(*request); |
int oflag = IPC_GET_ARG4(*request); |
fibril_rwlock_read_lock(&namespace_rwlock); |
int rc = vfs_open_node_internal(&lr); |
if (rc != EOK) { |
fibril_rwlock_read_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
vfs_node_t *node = vfs_node_get(&lr); |
fibril_rwlock_read_unlock(&namespace_rwlock); |
/* Truncate the file if requested and if necessary. */ |
if (oflag & O_TRUNC) { |
fibril_rwlock_write_lock(&node->contents_rwlock); |
if (node->size) { |
rc = vfs_truncate_internal(node->fs_handle, |
node->dev_handle, node->index, 0); |
if (rc) { |
fibril_rwlock_write_unlock(&node->contents_rwlock); |
vfs_node_put(node); |
ipc_answer_0(rid, rc); |
return; |
} |
node->size = 0; |
} |
fibril_rwlock_write_unlock(&node->contents_rwlock); |
} |
/* |
* Get ourselves a file descriptor and the corresponding vfs_file_t |
* structure. |
*/ |
int fd = vfs_fd_alloc(); |
if (fd < 0) { |
vfs_node_put(node); |
ipc_answer_0(rid, fd); |
return; |
} |
vfs_file_t *file = vfs_file_get(fd); |
file->node = node; |
if (oflag & O_APPEND) |
file->append = true; |
/* |
* The following increase in reference count is for the fact that the |
* file is being opened and that a file structure is pointing to it. |
* It is necessary so that the file will not disappear when |
* vfs_node_put() is called. The reference will be dropped by the |
* respective VFS_CLOSE. |
*/ |
vfs_node_addref(node); |
vfs_node_put(node); |
/* Success! Return the new file descriptor to the client. */ |
ipc_answer_1(rid, EOK, fd); |
} |
void vfs_node(ipc_callid_t rid, ipc_call_t *request) |
{ |
int fd = IPC_GET_ARG1(*request); |
int rc = vfs_fd_free(fd); |
/* Lookup the file structure corresponding to the file descriptor. */ |
vfs_file_t *file = vfs_file_get(fd); |
if (!file) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
ipc_answer_3(rid, EOK, file->node->fs_handle, file->node->dev_handle, |
file->node->index); |
} |
void vfs_device(ipc_callid_t rid, ipc_call_t *request) |
{ |
int fd = IPC_GET_ARG1(*request); |
/* Lookup the file structure corresponding to the file descriptor. */ |
vfs_file_t *file = vfs_file_get(fd); |
if (!file) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
/* |
* Lock the open file structure so that no other thread can manipulate |
* the same open file at a time. |
*/ |
fibril_mutex_lock(&file->lock); |
int fs_phone = vfs_grab_phone(file->node->fs_handle); |
/* Make a VFS_DEVICE request at the destination FS server. */ |
aid_t msg; |
ipc_call_t answer; |
msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
file->node->dev_handle, file->node->index, &answer); |
/* Wait for reply from the FS server. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
vfs_release_phone(fs_phone); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_1(rid, EOK, IPC_GET_ARG1(answer)); |
} |
void vfs_sync(ipc_callid_t rid, ipc_call_t *request) |
{ |
int fd = IPC_GET_ARG1(*request); |
/* Lookup the file structure corresponding to the file descriptor. */ |
vfs_file_t *file = vfs_file_get(fd); |
if (!file) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
/* |
* Lock the open file structure so that no other thread can manipulate |
* the same open file at a time. |
*/ |
fibril_mutex_lock(&file->lock); |
int fs_phone = vfs_grab_phone(file->node->fs_handle); |
/* Make a VFS_SYMC request at the destination FS server. */ |
aid_t msg; |
ipc_call_t answer; |
msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
file->node->dev_handle, file->node->index, &answer); |
/* Wait for reply from the FS server. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
vfs_release_phone(fs_phone); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_0(rid, rc); |
} |
void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
{ |
int fd = IPC_GET_ARG1(*request); |
/* Lookup the file structure corresponding to the file descriptor. */ |
vfs_file_t *file = vfs_file_get(fd); |
if (!file) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
/* |
* Lock the open file structure so that no other thread can manipulate |
* the same open file at a time. |
*/ |
fibril_mutex_lock(&file->lock); |
int fs_phone = vfs_grab_phone(file->node->fs_handle); |
/* Make a VFS_CLOSE request at the destination FS server. */ |
aid_t msg; |
ipc_call_t answer; |
msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
file->node->dev_handle, file->node->index, &answer); |
/* Wait for reply from the FS server. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
vfs_release_phone(fs_phone); |
fibril_mutex_unlock(&file->lock); |
int retval = IPC_GET_ARG1(answer); |
if (retval != EOK) |
ipc_answer_0(rid, retval); |
retval = vfs_fd_free(fd); |
ipc_answer_0(rid, retval); |
} |
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
{ |
640,7 → 805,7 |
* Lock the open file structure so that no other thread can manipulate |
* the same open file at a time. |
*/ |
futex_down(&file->lock); |
fibril_mutex_lock(&file->lock); |
/* |
* Lock the file's node so that no other client can read/write to it at |
647,9 → 812,9 |
* the same time. |
*/ |
if (read) |
rwlock_read_lock(&file->node->contents_rwlock); |
fibril_rwlock_read_lock(&file->node->contents_rwlock); |
else |
rwlock_write_lock(&file->node->contents_rwlock); |
fibril_rwlock_write_lock(&file->node->contents_rwlock); |
if (file->node->type == VFS_NODE_DIRECTORY) { |
/* |
657,7 → 822,7 |
* while we are in readdir(). |
*/ |
assert(read); |
rwlock_read_lock(&namespace_rwlock); |
fibril_rwlock_read_lock(&namespace_rwlock); |
} |
int fs_phone = vfs_grab_phone(file->node->fs_handle); |
678,30 → 843,31 |
*/ |
ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
vfs_release_phone(fs_phone); |
/* Wait for reply from the FS server. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
vfs_release_phone(fs_phone); |
size_t bytes = IPC_GET_ARG1(answer); |
if (file->node->type == VFS_NODE_DIRECTORY) |
rwlock_read_unlock(&namespace_rwlock); |
fibril_rwlock_read_unlock(&namespace_rwlock); |
/* Unlock the VFS node. */ |
if (read) |
rwlock_read_unlock(&file->node->contents_rwlock); |
fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
else { |
/* Update the cached version of node's size. */ |
if (rc == EOK) |
file->node->size = IPC_GET_ARG2(answer); |
rwlock_write_unlock(&file->node->contents_rwlock); |
fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
} |
/* Update the position pointer and unlock the open file. */ |
if (rc == EOK) |
file->pos += bytes; |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
/* |
* FS server's reply is the final result of the whole operation we |
735,40 → 901,40 |
} |
off_t newpos; |
futex_down(&file->lock); |
fibril_mutex_lock(&file->lock); |
if (whence == SEEK_SET) { |
file->pos = off; |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_1(rid, EOK, off); |
return; |
} |
if (whence == SEEK_CUR) { |
if (file->pos + off < file->pos) { |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_0(rid, EOVERFLOW); |
return; |
} |
file->pos += off; |
newpos = file->pos; |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_1(rid, EOK, newpos); |
return; |
} |
if (whence == SEEK_END) { |
rwlock_read_lock(&file->node->contents_rwlock); |
fibril_rwlock_read_lock(&file->node->contents_rwlock); |
size_t size = file->node->size; |
rwlock_read_unlock(&file->node->contents_rwlock); |
fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
if (size + off < size) { |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_0(rid, EOVERFLOW); |
return; |
} |
newpos = size + off; |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_1(rid, EOK, newpos); |
return; |
} |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_0(rid, EINVAL); |
} |
797,16 → 963,16 |
ipc_answer_0(rid, ENOENT); |
return; |
} |
futex_down(&file->lock); |
fibril_mutex_lock(&file->lock); |
rwlock_write_lock(&file->node->contents_rwlock); |
fibril_rwlock_write_lock(&file->node->contents_rwlock); |
rc = vfs_truncate_internal(file->node->fs_handle, |
file->node->dev_handle, file->node->index, size); |
if (rc == EOK) |
file->node->size = size; |
rwlock_write_unlock(&file->node->contents_rwlock); |
fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_0(rid, (ipcarg_t)rc); |
} |
836,10 → 1002,10 |
} |
path[len] = '\0'; |
rwlock_write_lock(&namespace_rwlock); |
fibril_rwlock_write_lock(&namespace_rwlock); |
int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
free(path); |
ipc_answer_0(rid, rc); |
} |
870,13 → 1036,13 |
} |
path[len] = '\0'; |
rwlock_write_lock(&namespace_rwlock); |
fibril_rwlock_write_lock(&namespace_rwlock); |
lflag &= L_DIRECTORY; /* sanitize lflag */ |
vfs_lookup_res_t lr; |
rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
free(path); |
if (rc != EOK) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
887,10 → 1053,10 |
* VFS_DESTROY'ed after the last reference to it is dropped. |
*/ |
vfs_node_t *node = vfs_node_get(&lr); |
futex_down(&nodes_futex); |
fibril_mutex_lock(&nodes_mutex); |
node->lnkcnt--; |
futex_up(&nodes_futex); |
rwlock_write_unlock(&namespace_rwlock); |
fibril_mutex_unlock(&nodes_mutex); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
vfs_node_put(node); |
ipc_answer_0(rid, EOK); |
} |
952,8 → 1118,16 |
} |
oldc[olen] = '\0'; |
newc[nlen] = '\0'; |
if (!str_lcmp(newc, oldc, str_length(oldc))) { |
/* oldc is a prefix of newc */ |
if ((!str_lcmp(newc, oldc, str_length(oldc))) && |
((newc[str_length(oldc)] == '/') || |
(str_length(oldc) == 1) || |
(str_length(oldc) == str_length(newc)))) { |
/* |
* oldc is a prefix of newc and either |
* - newc continues with a / where oldc ends, or |
* - oldc was / itself, or |
* - oldc and newc are equal. |
*/ |
ipc_answer_0(rid, EINVAL); |
free(old); |
free(new); |
963,11 → 1137,11 |
vfs_lookup_res_t old_lr; |
vfs_lookup_res_t new_lr; |
vfs_lookup_res_t new_par_lr; |
rwlock_write_lock(&namespace_rwlock); |
fibril_rwlock_write_lock(&namespace_rwlock); |
/* Lookup the node belonging to the old file name. */ |
rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
if (rc != EOK) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
free(old); |
free(new); |
975,16 → 1149,31 |
} |
vfs_node_t *old_node = vfs_node_get(&old_lr); |
if (!old_node) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, ENOMEM); |
free(old); |
free(new); |
return; |
} |
/* Determine the path to the parent of the node with the new name. */ |
char *parentc = str_dup(newc); |
if (!parentc) { |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
free(old); |
free(new); |
return; |
} |
char *lastsl = str_rchr(parentc + 1, '/'); |
if (lastsl) |
*lastsl = '\0'; |
else |
parentc[1] = '\0'; |
/* Lookup parent of the new file name. */ |
rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL); |
rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL); |
free(parentc); /* not needed anymore */ |
if (rc != EOK) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
free(old); |
free(new); |
993,7 → 1182,7 |
/* Check whether linking to the same file system instance. */ |
if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
(old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, EXDEV); /* different file systems */ |
free(old); |
free(new); |
1009,18 → 1198,18 |
case EOK: |
new_node = vfs_node_get(&new_lr); |
if (!new_node) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, ENOMEM); |
free(old); |
free(new); |
return; |
} |
futex_down(&nodes_futex); |
fibril_mutex_lock(&nodes_mutex); |
new_node->lnkcnt--; |
futex_up(&nodes_futex); |
fibril_mutex_unlock(&nodes_mutex); |
break; |
default: |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, ENOTEMPTY); |
free(old); |
free(new); |
1029,7 → 1218,7 |
/* Create the new link for the new name. */ |
rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
if (rc != EOK) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
if (new_node) |
vfs_node_put(new_node); |
ipc_answer_0(rid, rc); |
1037,13 → 1226,13 |
free(new); |
return; |
} |
futex_down(&nodes_futex); |
fibril_mutex_lock(&nodes_mutex); |
old_node->lnkcnt++; |
futex_up(&nodes_futex); |
fibril_mutex_unlock(&nodes_mutex); |
/* Destroy the link for the old name. */ |
rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
if (rc != EOK) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
vfs_node_put(old_node); |
if (new_node) |
vfs_node_put(new_node); |
1052,10 → 1241,10 |
free(new); |
return; |
} |
futex_down(&nodes_futex); |
fibril_mutex_lock(&nodes_mutex); |
old_node->lnkcnt--; |
futex_up(&nodes_futex); |
rwlock_write_unlock(&namespace_rwlock); |
fibril_mutex_unlock(&nodes_mutex); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
vfs_node_put(old_node); |
if (new_node) |
vfs_node_put(new_node); |
/branches/network/uspace/srv/vfs/vfs_register.c |
---|
45,15 → 45,16 |
#include <string.h> |
#include <ctype.h> |
#include <bool.h> |
#include <futex.h> |
#include <libadt/list.h> |
#include <fibril_sync.h> |
#include <adt/list.h> |
#include <as.h> |
#include <assert.h> |
#include <atomic.h> |
#include "vfs.h" |
atomic_t fs_head_futex = FUTEX_INITIALIZER; |
link_t fs_head; |
FIBRIL_CONDVAR_INITIALIZE(fs_head_cv); |
FIBRIL_MUTEX_INITIALIZE(fs_head_lock); |
LIST_INITIALIZE(fs_head); |
atomic_t fs_handle_next = { |
.count = 1 |
159,7 → 160,7 |
return; |
} |
link_initialize(&fs_info->fs_link); |
futex_initialize(&fs_info->phone_futex, 1); |
fibril_mutex_initialize(&fs_info->phone_lock); |
rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size); |
if (rc != EOK) { |
180,8 → 181,7 |
return; |
} |
futex_down(&fs_head_futex); |
fibril_inc_sercount(); |
fibril_mutex_lock(&fs_head_lock); |
/* |
* Check for duplicit registrations. |
191,8 → 191,7 |
* We already register a fs like this. |
*/ |
dprintf("FS is already registered.\n"); |
fibril_dec_sercount(); |
futex_up(&fs_head_futex); |
fibril_mutex_unlock(&fs_head_lock); |
free(fs_info); |
ipc_answer_0(callid, EEXISTS); |
ipc_answer_0(rid, EEXISTS); |
214,8 → 213,7 |
if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) { |
dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
list_remove(&fs_info->fs_link); |
fibril_dec_sercount(); |
futex_up(&fs_head_futex); |
fibril_mutex_unlock(&fs_head_lock); |
free(fs_info); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
233,8 → 231,7 |
if (!ipc_share_in_receive(&callid, &size)) { |
dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
list_remove(&fs_info->fs_link); |
fibril_dec_sercount(); |
futex_up(&fs_head_futex); |
fibril_mutex_unlock(&fs_head_lock); |
ipc_hangup(fs_info->phone); |
free(fs_info); |
ipc_answer_0(callid, EINVAL); |
248,8 → 245,7 |
if (size != PLB_SIZE) { |
dprintf("Client suggests wrong size of PFB, size = %d\n", size); |
list_remove(&fs_info->fs_link); |
fibril_dec_sercount(); |
futex_up(&fs_head_futex); |
fibril_mutex_unlock(&fs_head_lock); |
ipc_hangup(fs_info->phone); |
free(fs_info); |
ipc_answer_0(callid, EINVAL); |
273,16 → 269,11 |
fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next); |
ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle); |
fibril_dec_sercount(); |
futex_up(&fs_head_futex); |
fibril_condvar_broadcast(&fs_head_cv); |
fibril_mutex_unlock(&fs_head_lock); |
dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n", |
FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle); |
/* Process pending mount requests possibly waiting |
* for this filesystem implementation. |
*/ |
vfs_process_pending_mount(); |
} |
/** For a given file system handle, implement policy for allocating a phone. |
294,76 → 285,49 |
*/ |
int vfs_grab_phone(fs_handle_t handle) |
{ |
int phone; |
/* |
* For now, we don't try to be very clever and very fast. |
* We simply lookup the phone in the fs_head list. We currently don't |
* open any additional phones (even though that itself would be pretty |
* straightforward; housekeeping multiple open phones to a FS task would |
* be more demanding). Instead, we simply take the respective |
* phone_futex and keep it until vfs_release_phone(). |
* For now, we don't try to be very clever and very fast. We simply |
* lookup the phone in the fs_head list and duplicate it. The duplicate |
* phone will be returned to the client and the client will use it for |
* communication. In the future, we should cache the connections so |
* that they do not have to be reestablished over and over again. |
*/ |
futex_down(&fs_head_futex); |
fibril_mutex_lock(&fs_head_lock); |
link_t *cur; |
fs_info_t *fs; |
for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
fs = list_get_instance(cur, fs_info_t, fs_link); |
if (fs->fs_handle == handle) { |
futex_up(&fs_head_futex); |
/* |
* For now, take the futex unconditionally. |
* Oh yeah, serialization rocks. |
* It will be up'ed in vfs_release_phone(). |
*/ |
futex_down(&fs->phone_futex); |
/* |
* Avoid deadlock with other fibrils in the same thread |
* by disabling fibril preemption. |
*/ |
fibril_inc_sercount(); |
return fs->phone; |
fibril_mutex_unlock(&fs_head_lock); |
fibril_mutex_lock(&fs->phone_lock); |
phone = ipc_connect_me_to(fs->phone, 0, 0, 0); |
fibril_mutex_unlock(&fs->phone_lock); |
assert(phone > 0); |
return phone; |
} |
} |
futex_up(&fs_head_futex); |
fibril_mutex_unlock(&fs_head_lock); |
return 0; |
} |
/** Tell VFS that the phone is in use for any request. |
/** Tell VFS that the phone is not needed anymore. |
* |
* @param phone Phone to FS task. |
*/ |
void vfs_release_phone(int phone) |
{ |
bool found = false; |
/* |
* Undo the fibril_inc_sercount() done in vfs_grab_phone(). |
*/ |
fibril_dec_sercount(); |
futex_down(&fs_head_futex); |
link_t *cur; |
for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link); |
if (fs->phone == phone) { |
found = true; |
futex_up(&fs_head_futex); |
futex_up(&fs->phone_futex); |
return; |
/* TODO: implement connection caching */ |
ipc_hangup(phone); |
} |
} |
futex_up(&fs_head_futex); |
/* |
* Not good to get here. |
*/ |
assert(found == true); |
} |
/** Convert file system name to its handle. |
* |
* @param name File system name. |
* @param lock If true, the function will down and up the |
* fs_head_futex. |
* @param lock If true, the function will lock and unlock the |
* fs_head_lock. |
* |
* @return File system handle or zero if file system not found. |
*/ |
372,7 → 336,7 |
int handle = 0; |
if (lock) |
futex_down(&fs_head_futex); |
fibril_mutex_lock(&fs_head_lock); |
link_t *cur; |
for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link); |
382,7 → 346,7 |
} |
} |
if (lock) |
futex_up(&fs_head_futex); |
fibril_mutex_unlock(&fs_head_lock); |
return handle; |
} |
/branches/network/uspace/srv/vfs/vfs.h |
---|
34,11 → 34,12 |
#define VFS_VFS_H_ |
#include <ipc/ipc.h> |
#include <libadt/list.h> |
#include <futex.h> |
#include <rwlock.h> |
#include <adt/list.h> |
#include <fibril_sync.h> |
#include <sys/types.h> |
#include <devmap.h> |
#include <bool.h> |
#include <ipc/vfs.h> |
// FIXME: according to CONFIG_DEBUG |
// #define dprintf(...) printf(__VA_ARGS__) |
45,53 → 46,7 |
#define dprintf(...) |
#define VFS_FIRST IPC_FIRST_USER_METHOD |
/* Basic types. */ |
typedef int16_t fs_handle_t; |
typedef int16_t dev_handle_t; |
typedef uint32_t fs_index_t; |
typedef enum { |
VFS_READ = VFS_FIRST, |
VFS_WRITE, |
VFS_TRUNCATE, |
VFS_MOUNT, |
VFS_UNMOUNT, |
VFS_LAST_CMN, /* keep this the last member of this enum */ |
} vfs_request_cmn_t; |
typedef enum { |
VFS_LOOKUP = VFS_LAST_CMN, |
VFS_MOUNTED, |
VFS_DESTROY, |
VFS_LAST_CLNT, /* keep this the last member of this enum */ |
} vfs_request_clnt_t; |
typedef enum { |
VFS_REGISTER = VFS_LAST_CMN, |
VFS_OPEN, |
VFS_CLOSE, |
VFS_SEEK, |
VFS_MKDIR, |
VFS_UNLINK, |
VFS_RENAME, |
VFS_LAST_SRV, /* keep this the last member of this enum */ |
} vfs_request_srv_t; |
#define FS_NAME_MAXLEN 20 |
/** |
* A structure like this is passed to VFS by each individual FS upon its |
* registration. It assosiates a human-readable identifier with each |
* registered FS. |
*/ |
typedef struct { |
/** Unique identifier of the fs. */ |
char name[FS_NAME_MAXLEN + 1]; |
} vfs_info_t; |
/** |
* A structure like this will be allocated for each registered file system. |
*/ |
typedef struct { |
98,7 → 53,7 |
link_t fs_link; |
vfs_info_t vfs_info; |
fs_handle_t fs_handle; |
futex_t phone_futex; /**< Phone serializing futex. */ |
fibril_mutex_t phone_lock; |
ipcarg_t phone; |
} fs_info_t; |
128,50 → 83,6 |
VFS_TRIPLET; |
} vfs_triplet_t; |
/* |
* Lookup flags. |
*/ |
/** |
* No lookup flags used. |
*/ |
#define L_NONE 0 |
/** |
* Lookup will succeed only if the object is a regular file. If L_CREATE is |
* specified, an empty file will be created. This flag is mutually exclusive |
* with L_DIRECTORY. |
*/ |
#define L_FILE 1 |
/** |
* Lookup wil succeed only if the object is a directory. If L_CREATE is |
* specified, an empty directory will be created. This flag is mutually |
* exclusive with L_FILE. |
*/ |
#define L_DIRECTORY 2 |
/** |
* When used with L_CREATE, L_EXCLUSIVE will cause the lookup to fail if the |
* object already exists. L_EXCLUSIVE is implied when L_DIRECTORY is used. |
*/ |
#define L_EXCLUSIVE 4 |
/** |
* L_CREATE is used for creating both regular files and directories. |
*/ |
#define L_CREATE 8 |
/** |
* L_LINK is used for linking to an already existing nodes. |
*/ |
#define L_LINK 16 |
/** |
* L_UNLINK is used to remove leaves from the file system namespace. This flag |
* cannot be passed directly by the client, but will be set by VFS during |
* VFS_UNLINK. |
*/ |
#define L_UNLINK 32 |
/** |
* L_PARENT performs a lookup but returns the triplet of the parent node. |
* This flag may not be combined with any other lookup flag. |
*/ |
#define L_PARENT 64 |
typedef enum vfs_node_type { |
VFS_NODE_UNKNOWN, |
VFS_NODE_FILE, |
210,7 → 121,7 |
/** |
* Holding this rwlock prevents modifications of the node's contents. |
*/ |
rwlock_t contents_rwlock; |
fibril_rwlock_t contents_rwlock; |
} vfs_node_t; |
/** |
219,7 → 130,7 |
*/ |
typedef struct { |
/** Serializes access to this open file. */ |
futex_t lock; |
fibril_mutex_t lock; |
vfs_node_t *node; |
233,16 → 144,14 |
off_t pos; |
} vfs_file_t; |
extern futex_t nodes_futex; |
extern fibril_mutex_t nodes_mutex; |
extern fibril_condvar_t fs_head_cv; |
extern fibril_mutex_t fs_head_lock; |
extern link_t fs_head; /**< List of registered file systems. */ |
extern vfs_pair_t rootfs; /**< Root file system. */ |
#define MAX_PATH_LEN (64 * 1024) |
#define PLB_SIZE (2 * MAX_PATH_LEN) |
/** Each instance of this type describes one path lookup in progress. */ |
typedef struct { |
link_t plb_link; /**< Active PLB entries list link. */ |
250,7 → 159,7 |
size_t len; /**< Number of characters in this PLB entry. */ |
} plb_entry_t; |
extern futex_t plb_futex; /**< Futex protecting plb and plb_head. */ |
extern fibril_mutex_t plb_mutex;/**< Mutex protecting plb and plb_head. */ |
extern uint8_t *plb; /**< Path Lookup Buffer */ |
extern link_t plb_head; /**< List of active PLB entries. */ |
257,7 → 166,7 |
#define MAX_MNTOPTS_LEN 256 |
/** Holding this rwlock prevents changes in file system namespace. */ |
extern rwlock_t namespace_rwlock; |
extern fibril_rwlock_t namespace_rwlock; |
extern int vfs_grab_phone(fs_handle_t); |
extern void vfs_release_phone(int); |
264,8 → 173,9 |
extern fs_handle_t fs_name_to_handle(char *, bool); |
extern int vfs_lookup_internal(char *, int, vfs_lookup_res_t *, vfs_pair_t *, |
...); |
extern int vfs_lookup_internal(char *, int, vfs_lookup_res_t *, |
vfs_pair_t *, ...); |
extern int vfs_open_node_internal(vfs_lookup_res_t *); |
extern bool vfs_nodes_init(void); |
extern vfs_node_t *vfs_node_get(vfs_lookup_res_t *); |
284,10 → 194,13 |
extern void vfs_node_addref(vfs_node_t *); |
extern void vfs_node_delref(vfs_node_t *); |
extern void vfs_process_pending_mount(void); |
extern void vfs_register(ipc_callid_t, ipc_call_t *); |
extern void vfs_mount(ipc_callid_t, ipc_call_t *); |
extern void vfs_open(ipc_callid_t, ipc_call_t *); |
extern void vfs_open_node(ipc_callid_t, ipc_call_t *); |
extern void vfs_device(ipc_callid_t, ipc_call_t *); |
extern void vfs_sync(ipc_callid_t, ipc_call_t *); |
extern void vfs_node(ipc_callid_t, ipc_call_t *); |
extern void vfs_close(ipc_callid_t, ipc_call_t *); |
extern void vfs_read(ipc_callid_t, ipc_call_t *); |
extern void vfs_write(ipc_callid_t, ipc_call_t *); |
/branches/network/uspace/srv/vfs/vfs_node.c |
---|
38,15 → 38,14 |
#include "vfs.h" |
#include <stdlib.h> |
#include <string.h> |
#include <futex.h> |
#include <rwlock.h> |
#include <libadt/hash_table.h> |
#include <fibril_sync.h> |
#include <adt/hash_table.h> |
#include <assert.h> |
#include <async.h> |
#include <errno.h> |
/** Futex protecting the VFS node hash table. */ |
futex_t nodes_futex = FUTEX_INITIALIZER; |
/** Mutex protecting the VFS node hash table. */ |
FIBRIL_MUTEX_INITIALIZE(nodes_mutex); |
#define NODES_BUCKETS_LOG 8 |
#define NODES_BUCKETS (1 << NODES_BUCKETS_LOG) |
89,9 → 88,9 |
*/ |
void vfs_node_addref(vfs_node_t *node) |
{ |
futex_down(&nodes_futex); |
fibril_mutex_lock(&nodes_mutex); |
_vfs_node_addref(node); |
futex_up(&nodes_futex); |
fibril_mutex_unlock(&nodes_mutex); |
} |
/** Decrement reference count of a VFS node. |
105,7 → 104,7 |
bool free_vfs_node = false; |
bool free_fs_node = false; |
futex_down(&nodes_futex); |
fibril_mutex_lock(&nodes_mutex); |
if (node->refcnt-- == 1) { |
/* |
* We are dropping the last reference to this node. |
121,7 → 120,7 |
if (!node->lnkcnt) |
free_fs_node = true; |
} |
futex_up(&nodes_futex); |
fibril_mutex_unlock(&nodes_mutex); |
if (free_fs_node) { |
/* |
161,12 → 160,12 |
link_t *tmp; |
vfs_node_t *node; |
futex_down(&nodes_futex); |
fibril_mutex_lock(&nodes_mutex); |
tmp = hash_table_find(&nodes, key); |
if (!tmp) { |
node = (vfs_node_t *) malloc(sizeof(vfs_node_t)); |
if (!node) { |
futex_up(&nodes_futex); |
fibril_mutex_unlock(&nodes_mutex); |
return NULL; |
} |
memset(node, 0, sizeof(vfs_node_t)); |
177,7 → 176,7 |
node->lnkcnt = result->lnkcnt; |
node->type = result->type; |
link_initialize(&node->nh_link); |
rwlock_initialize(&node->contents_rwlock); |
fibril_rwlock_initialize(&node->contents_rwlock); |
hash_table_insert(&nodes, key, &node->nh_link); |
} else { |
node = hash_table_get_instance(tmp, vfs_node_t, nh_link); |
193,7 → 192,7 |
assert(node->type == result->type || result->type == VFS_NODE_UNKNOWN); |
_vfs_node_addref(node); |
futex_up(&nodes_futex); |
fibril_mutex_unlock(&nodes_mutex); |
return node; |
} |
/branches/network/uspace/srv/vfs/vfs_lookup.c |
---|
42,14 → 42,14 |
#include <string.h> |
#include <stdarg.h> |
#include <bool.h> |
#include <futex.h> |
#include <libadt/list.h> |
#include <fibril_sync.h> |
#include <adt/list.h> |
#include <vfs/canonify.h> |
#define min(a, b) ((a) < (b) ? (a) : (b)) |
futex_t plb_futex = FUTEX_INITIALIZER; |
link_t plb_head; /**< PLB entry ring buffer. */ |
FIBRIL_MUTEX_INITIALIZE(plb_mutex); |
LIST_INITIALIZE(plb_head); /**< PLB entry ring buffer. */ |
uint8_t *plb = NULL; |
/** Perform a path lookup. |
63,6 → 63,7 |
* of the whole VFS tree. |
* |
* @return EOK on success or an error code from errno.h. |
* |
*/ |
int vfs_lookup_internal(char *path, int lflag, vfs_lookup_res_t *result, |
vfs_pair_t *altroot, ...) |
91,7 → 92,7 |
va_end(ap); |
} |
futex_down(&plb_futex); |
fibril_mutex_lock(&plb_mutex); |
plb_entry_t entry; |
link_initialize(&entry.plb_link); |
118,7 → 119,7 |
/* |
* The buffer cannot absorb the path. |
*/ |
futex_up(&plb_futex); |
fibril_mutex_unlock(&plb_mutex); |
return ELIMIT; |
} |
} else { |
126,7 → 127,7 |
/* |
* The buffer cannot absorb the path. |
*/ |
futex_up(&plb_futex); |
fibril_mutex_unlock(&plb_mutex); |
return ELIMIT; |
} |
} |
145,7 → 146,7 |
*/ |
list_append(&entry.plb_link, &plb_head); |
futex_up(&plb_futex); |
fibril_mutex_unlock(&plb_mutex); |
/* |
* Copy the path into PLB. |
162,12 → 163,12 |
(ipcarg_t) (first + len - 1) % PLB_SIZE, |
(ipcarg_t) root->dev_handle, (ipcarg_t) lflag, (ipcarg_t) index, |
&answer); |
vfs_release_phone(phone); |
ipcarg_t rc; |
async_wait_for(req, &rc); |
vfs_release_phone(phone); |
futex_down(&plb_futex); |
fibril_mutex_lock(&plb_mutex); |
list_remove(&entry.plb_link); |
/* |
* Erasing the path from PLB will come handy for debugging purposes. |
174,9 → 175,9 |
*/ |
memset(&plb[first], 0, cnt1); |
memset(plb, 0, cnt2); |
futex_up(&plb_futex); |
fibril_mutex_unlock(&plb_mutex); |
if ((rc == EOK) && result) { |
if ((rc == EOK) && (result)) { |
result->triplet.fs_handle = (fs_handle_t) IPC_GET_ARG1(answer); |
result->triplet.dev_handle = (dev_handle_t) IPC_GET_ARG2(answer); |
result->triplet.index = (fs_index_t) IPC_GET_ARG3(answer); |
193,6 → 194,39 |
return rc; |
} |
/** Perform a node open operation. |
* |
* @return EOK on success or an error code from errno.h. |
* |
*/ |
int vfs_open_node_internal(vfs_lookup_res_t *result) |
{ |
int phone = vfs_grab_phone(result->triplet.fs_handle); |
ipc_call_t answer; |
aid_t req = async_send_2(phone, VFS_OPEN_NODE, |
(ipcarg_t) result->triplet.dev_handle, |
(ipcarg_t) result->triplet.index, &answer); |
ipcarg_t rc; |
async_wait_for(req, &rc); |
vfs_release_phone(phone); |
if (rc == EOK) { |
result->size = (size_t) IPC_GET_ARG1(answer); |
result->lnkcnt = (unsigned) IPC_GET_ARG2(answer); |
if (IPC_GET_ARG3(answer) & L_FILE) |
result->type = VFS_NODE_FILE; |
else if (IPC_GET_ARG3(answer) & L_DIRECTORY) |
result->type = VFS_NODE_DIRECTORY; |
else |
result->type = VFS_NODE_UNKNOWN; |
} |
return rc; |
} |
/** |
* @} |
*/ |
/branches/network/uspace/srv/vfs/vfs_file.c |
---|
40,6 → 40,8 |
#include <string.h> |
#include <assert.h> |
#include <bool.h> |
#include <fibril.h> |
#include <fibril_sync.h> |
#include "vfs.h" |
/** |
55,9 → 57,9 |
* first VFS_OPEN operation. |
* |
* This resource being per-connection and, in the first place, per-fibril, we |
* don't need to protect it by a futex. |
* don't need to protect it by a mutex. |
*/ |
__thread vfs_file_t **files = NULL; |
fibril_local vfs_file_t **files = NULL; |
/** Initialize the table of open files. */ |
bool vfs_files_init(void) |
78,19 → 80,23 |
*/ |
int vfs_fd_alloc(void) |
{ |
int i; |
if (!vfs_files_init()) |
return ENOMEM; |
unsigned int i; |
for (i = 0; i < MAX_OPEN_FILES; i++) { |
if (!files[i]) { |
files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t)); |
if (!files[i]) |
return ENOMEM; |
memset(files[i], 0, sizeof(vfs_file_t)); |
futex_initialize(&files[i]->lock, 1); |
fibril_mutex_initialize(&files[i]->lock); |
vfs_file_addref(files[i]); |
return i; |
return (int) i; |
} |
} |
return EMFILE; |
} |
103,10 → 109,15 |
*/ |
int vfs_fd_free(int fd) |
{ |
if ((fd >= MAX_OPEN_FILES) || (files[fd] == NULL)) |
if (!vfs_files_init()) |
return ENOMEM; |
if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL)) |
return EBADF; |
vfs_file_delref(files[fd]); |
files[fd] = NULL; |
return EOK; |
} |
150,8 → 161,12 |
*/ |
vfs_file_t *vfs_file_get(int fd) |
{ |
if (fd < MAX_OPEN_FILES) |
if (!vfs_files_init()) |
return NULL; |
if ((fd >= 0) && (fd < MAX_OPEN_FILES)) |
return files[fd]; |
return NULL; |
} |