summaryrefslogtreecommitdiffstats
path: root/src/sys/vfs/vfs_main.C
blob: 291005945056a2ecdcb87e7bdf2b6204646f1f19 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/sys/vfs/vfs_main.C $                                      */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2010,2014              */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#include <string.h>
#include <errno.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_ROOT_MSG_VFS = "/msg/vfs";
const char* VFS_ROOT_MSG_MBOX = "/msg/mbox";

void vfs_module_init();

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");

    // Initalize modules.
    vfs_module_init();

    // Signal with sys/init to continue on.
    barrier_wait(barrier);

    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);

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

            case VFS_MSG_REMOVE_MSGQ:
                {
                    VfsEntry::key_type k;
                    strcpy(k.key, (char*) msg->extra_data);
                    VfsEntry* e = vfsContents.find(k);
                    if(NULL != e)
                    {
                        msg->data[0] = 0; //(uint64_t) e->msg_q;
                        printkd("VFS: Removing msg queue %s\n",e->key.key);
                        vfsContents.erase(e);
                        delete e;
                    }
                    else
                    {
                        msg->data[0] = (uint64_t) (-ENXIO);
                    }
                    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:
                {
                    printkd("VFS: Got exec request of %s\n",
                            (const char*)msg->data[0]);

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

                    void* fnptr = vfs_start_entrypoint(module);

                    // child == -ENOENT 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( fnptr == reinterpret_cast<void*>(-ENOENT) )
                        // forward msg to usr vfs
                    {
                        VfsEntry::key_type k;
                        strcpy(k.key, VFS_ROOT_MSG_VFS);
                        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] = (uint64_t) fnptr;
                            msg_respond(vfsMsgQ, msg);
                        }
                    }
                    else // send back child (or errno)
                    {
                        msg->data[0] = (uint64_t) fnptr;
                        msg_respond(vfsMsgQ, msg);
                    }
                }
                break;

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

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

void* vfs_start_entrypoint(VfsSystemModule * i_module)
{
    void* ptr = reinterpret_cast<void*>(-ENOENT);
    if(i_module != NULL)
    {
        if (i_module->start == NULL)
        {
            // module has no start() routine
            ptr = reinterpret_cast<void*>(-ENOEXEC);
        }
        else
        {
            ptr = reinterpret_cast<void*>(i_module->start);
        }
    }
    return ptr;
}



OpenPOWER on IntegriCloud