summaryrefslogtreecommitdiffstats
path: root/src/sys/vfs/vfs_main.C
blob: 4308da8d4d784c713896883a7be317139e12dbe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <string.h>

#include <sys/msg.h>
#include <sys/vfs.h>
#include <sys/task.h>
#include <sys/sync.h>

#include <util/locked/list.H>
#include <kernel/console.H>  // TODO : Remove this.

const char* VFS_ROOT = "/";
const char* VFS_ROOT_BIN = "/bin/";
const char* VFS_ROOT_DATA = "/data/";
const char* VFS_ROOT_MSG = "/msg/";
const char* VFS_MSG = "/vfs/";

void vfs_module_init();

/**
 * Call the module start routine
 * @param[in] i_module VfsSystemModule data for the module
 * @param[in] i_param parameter to pass to task_create() for this module
 * @return tid_t of started task | -1 if i_module is NULL | -2 if there is no start()
 */
tid_t vfs_exec(VfsSystemModule * i_module, void* i_param);

struct VfsPath
{
    char key[64];

    bool operator!=(VfsPath& r) { return 0 != strcmp(key, r.key); };
};

struct VfsEntry
{
    typedef VfsPath key_type;
    key_type key;
    msg_q_t msg_q;

    VfsEntry* next;
    VfsEntry* prev;
};

void vfs_main(void* i_barrier)
{
    barrier_t * barrier = (barrier_t *)i_barrier;
    // Create message queue, register with kernel.
    msg_q_t vfsMsgQ = msg_q_create();
    msg_q_register(vfsMsgQ, VFS_ROOT);

    printk("done.\n");

    barrier_wait(barrier);

    // Initalize modules.
    vfs_module_init();

    Util::Locked::List<VfsEntry, VfsEntry::key_type> vfsContents;

    while(1)
    {
	msg_t* msg = msg_wait(vfsMsgQ);

	switch(msg->type)
	{
	    case VFS_MSG_REGISTER_MSGQ:
		{
		    VfsEntry* e = new VfsEntry();
		    strcpy(e->key.key, (char*) msg->extra_data);
		    e->msg_q = (msg_q_t) msg->data[0];
		    vfsContents.insert(e);

		    printk("VFS: Registering %p as %s\n",
			   e->msg_q, e->key.key);
		    msg_respond(vfsMsgQ, msg);
		}
		break;

	    case VFS_MSG_RESOLVE_MSGQ:
		{
		    VfsEntry::key_type k;
		    strcpy(k.key, (char*) msg->extra_data);
		    VfsEntry* e = vfsContents.find(k);
		    if (NULL == e)
			msg->data[0] = (uint64_t) NULL;
		    else
			msg->data[0] = (uint64_t) e->msg_q;
		    msg_respond(vfsMsgQ, msg);
		}
		break;

	    case VFS_MSG_EXEC:
		{
		    printk("VFS: Got exec request of %s\n",
		           (const char*)msg->data[0]);

		    VfsSystemModule* module =
                        vfs_find_module(VFS_MODULES,
                                        (const char *) msg->data[0]);

		    tid_t child = vfs_exec(module,(void*) msg->data[1]);

                    // child == -1 means module not found in base image so send
                    // a message to VFS_MSG queue to look in the extended image
                    // VFS_MSG queue will handle the msg_respond()
                    if( child == -1 ) // forward msg to usr vfs
                    {
                        VfsEntry::key_type k;
                        strcpy(k.key, VFS_MSG);
                        VfsEntry* e = vfsContents.find(k);
                        if(e != NULL)
                        {
                            msg_t* emsg = msg_allocate();
                            emsg->type = msg->type;
                            emsg->data[0] = (uint64_t) msg;
                            emsg->data[1] = (uint64_t) vfsMsgQ;
                            msg_send(e->msg_q, emsg); // send async msg
                        }
                        else  // Cant find VFS_MSG queue - not started yet
                        {
                            msg->data[0] =  child;
                            msg_respond(vfsMsgQ, msg);
                        }
                    }
                    else // send back child (or errno)
                    {
                        msg->data[0] = child;
                        msg_respond(vfsMsgQ, msg);
                    }
		}
		break;

	    default:
		msg_free(msg);
		break;
	}
    }   // end while(1)
}

// ----------------------------------------------------------------------------

tid_t vfs_exec(VfsSystemModule * i_module, void* i_param)
{
    tid_t child = -1;
    if(i_module != NULL)
    {
        if (i_module->start == NULL)
        {
            child = -2;  // module has no start() routine
        }
        else
        {
            child = task_create(i_module->start, i_param);
        }
    }
    return child;
}
  


OpenPOWER on IntegriCloud