minix/lib/libvtreefs/vtreefs.c

160 lines
4.1 KiB
C
Raw Normal View History

2010-08-10 22:05:51 +02:00
/* VTreeFS - vtreefs.c - by Alen Stojanov and David van Moolenbroek */
#include "inc.h"
2012-03-25 20:25:53 +02:00
static int get_work(void);
static void send_reply(int err, int transid);
static void got_signal(int signal);
2010-08-10 22:05:51 +02:00
2012-03-25 20:25:53 +02:00
static unsigned int inodes;
static struct inode_stat *root_stat;
static index_t root_entries;
2010-08-10 22:05:51 +02:00
/*===========================================================================*
* init_server *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
static int init_server(int UNUSED(type), sef_init_info_t *UNUSED(info))
2010-08-10 22:05:51 +02:00
{
/* Initialize internal state, and register with VFS.
*/
/* Initialize the virtual tree. */
init_inodes(inodes, root_stat, root_entries);
/* Do not yet allow any requests except REQ_READSUPER. */
fs_mounted = FALSE;
return OK;
}
/*===========================================================================*
* sef_local_startup *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
static void sef_local_startup(void)
2010-08-10 22:05:51 +02:00
{
sef_setcb_init_fresh(init_server);
sef_setcb_init_restart(init_server);
sef_setcb_signal_handler(got_signal);
/* No support for live update yet. */
sef_startup();
}
/*===========================================================================*
* start_vtreefs *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
void start_vtreefs(struct fs_hooks *hooks, unsigned int nr_inodes,
2010-08-10 22:05:51 +02:00
struct inode_stat *stat, index_t nr_indexed_entries)
{
/* This is the main routine of this service. The main loop consists of
* three major activities: getting new work, processing the work, and
* sending the reply. The loop exits when the process is signaled to
* exit; due to limitations of SEF, it can not return to the caller.
*/
2011-07-27 17:49:42 +02:00
int call_nr, err, transid;
2010-08-10 22:05:51 +02:00
/* Use global variables to work around the inability to pass parameters
* through SEF to the initialization function..
*/
vtreefs_hooks = hooks;
inodes = nr_inodes;
root_stat = stat;
root_entries = nr_indexed_entries;
sef_local_startup();
for (;;) {
2011-07-27 17:49:42 +02:00
get_work();
transid = TRNS_GET_ID(fs_m_in.m_type);
fs_m_in.m_type = TRNS_DEL_ID(fs_m_in.m_type);
if (fs_m_in.m_type == 0) {
assert(!IS_VFS_FS_TRANSID(transid));
fs_m_in.m_type = transid; /* Backwards compat. */
transid = 0;
} else
assert(IS_VFS_FS_TRANSID(transid));
call_nr = fs_m_in.m_type;
2010-08-10 22:05:51 +02:00
if (fs_m_in.m_source != VFS_PROC_NR) {
if (vtreefs_hooks->message_hook != NULL) {
/* If the request is not among the recognized
* requests, call the message hook.
*/
vtreefs_hooks->message_hook(&fs_m_in);
}
continue;
}
if (fs_mounted || call_nr == REQ_READSUPER) {
call_nr -= VFS_BASE;
if (call_nr >= 0 && call_nr < NREQS) {
err = (*fs_call_vec[call_nr])();
} else {
err = ENOSYS;
}
}
else err = EINVAL;
2011-07-27 17:49:42 +02:00
send_reply(err, transid);
2010-08-10 22:05:51 +02:00
}
}
/*===========================================================================*
* get_work *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
static int get_work(void)
2010-08-10 22:05:51 +02:00
{
/* Retrieve work. Return the call number.
*/
int r;
if ((r = sef_receive(ANY, &fs_m_in)) != OK)
panic(__FILE__, "receive failed", r);
return fs_m_in.m_type;
}
/*===========================================================================*
* send_reply *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
static void send_reply(int err, int transid)
2010-08-10 22:05:51 +02:00
{
/* Send a reply to the caller.
*/
int r;
fs_m_out.m_type = err;
2011-07-27 17:49:42 +02:00
if (IS_VFS_FS_TRANSID(transid)) {
fs_m_out.m_type = TRNS_ADD_ID(fs_m_out.m_type, transid);
}
2010-08-10 22:05:51 +02:00
if ((r = send(fs_m_in.m_source, &fs_m_out)) != OK)
panic(__FILE__, "unable to send reply", r);
}
/*===========================================================================*
* got_signal *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
static void got_signal(int signal)
2010-08-10 22:05:51 +02:00
{
/* We received a signal. If it is a termination signal, and the file
* system has already been unmounted, clean up and exit.
*/
if (signal != SIGTERM)
return;
if (fs_mounted)
return;
cleanup_inodes();
exit(0);
}