Much USB code for ARM USB support
[minix3.git] / minix / fs / mfs / main.c
blob43c881dd073ead4ddf4228cb0e0f1c1801f1735e
1 #include "fs.h"
2 #include <assert.h>
3 #include <minix/callnr.h>
4 #include <signal.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <minix/dmap.h>
8 #include <minix/endpoint.h>
9 #include <minix/vfsif.h>
10 #include "buf.h"
11 #include "inode.h"
14 /* Declare some local functions. */
15 static void get_work(message *m_in);
16 static void reply(endpoint_t who, message *m_out);
18 /* SEF functions and variables. */
19 static void sef_local_startup(void);
20 static int sef_cb_init_fresh(int type, sef_init_info_t *info);
21 static void sef_cb_signal_handler(int signo);
23 /*===========================================================================*
24 * main *
25 *===========================================================================*/
26 int main(int argc, char *argv[])
28 /* This is the main routine of this service. The main loop consists of
29 * three major activities: getting new work, processing the work, and
30 * sending the reply. The loop never terminates, unless a panic occurs.
32 int error = OK, ind, transid;
34 /* SEF local startup. */
35 env_setargs(argc, argv);
36 sef_local_startup();
38 while(!unmountdone || !exitsignaled) {
39 endpoint_t src;
41 /* Wait for request message. */
42 get_work(&fs_m_in);
44 transid = TRNS_GET_ID(fs_m_in.m_type);
45 fs_m_in.m_type = TRNS_DEL_ID(fs_m_in.m_type);
46 if (fs_m_in.m_type == 0) {
47 assert(!IS_VFS_FS_TRANSID(transid));
48 fs_m_in.m_type = transid; /* Backwards compat. */
49 transid = 0;
50 } else
51 assert(IS_VFS_FS_TRANSID(transid));
53 src = fs_m_in.m_source;
54 caller_uid = INVAL_UID; /* To trap errors */
55 caller_gid = INVAL_GID;
56 req_nr = fs_m_in.m_type;
58 if (req_nr < FS_BASE) {
59 fs_m_in.m_type += FS_BASE;
60 req_nr = fs_m_in.m_type;
62 ind = req_nr - FS_BASE;
64 if (ind < 0 || ind >= NREQS) {
65 printf("MFS: bad request %d from %d\n", req_nr, src);
66 printf("ind = %d\n", ind);
67 error = EINVAL;
68 } else {
69 error = (*fs_call_vec[ind])();
70 /*cch_check();*/
73 fs_m_out.m_type = error;
74 if (IS_VFS_FS_TRANSID(transid)) {
75 /* If a transaction ID was set, reset it */
76 fs_m_out.m_type = TRNS_ADD_ID(fs_m_out.m_type, transid);
78 reply(src, &fs_m_out);
81 return(OK);
84 /*===========================================================================*
85 * sef_local_startup *
86 *===========================================================================*/
87 static void sef_local_startup()
89 /* Register init callbacks. */
90 sef_setcb_init_fresh(sef_cb_init_fresh);
91 sef_setcb_init_restart(sef_cb_init_fail);
93 /* No live update support for now. */
95 /* Register signal callbacks. */
96 sef_setcb_signal_handler(sef_cb_signal_handler);
98 /* Let SEF perform startup. */
99 sef_startup();
102 /*===========================================================================*
103 * sef_cb_init_fresh *
104 *===========================================================================*/
105 static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
107 /* Initialize the Minix file server. */
108 int i;
110 lmfs_may_use_vmcache(1);
112 /* Init inode table */
113 for (i = 0; i < NR_INODES; ++i) {
114 inode[i].i_count = 0;
115 cch[i] = 0;
118 init_inode_cache();
120 lmfs_buf_pool(DEFAULT_NR_BUFS);
122 return(OK);
125 /*===========================================================================*
126 * sef_cb_signal_handler *
127 *===========================================================================*/
128 static void sef_cb_signal_handler(int signo)
130 /* Only check for termination signal, ignore anything else. */
131 if (signo != SIGTERM) return;
133 exitsignaled = 1;
134 (void) fs_sync();
136 /* If unmounting has already been performed, exit immediately.
137 * We might not get another message.
139 if (unmountdone) exit(0);
142 /*===========================================================================*
143 * get_work *
144 *===========================================================================*/
145 static void get_work(m_in)
146 message *m_in; /* pointer to message */
148 int r, srcok = 0;
149 endpoint_t src;
151 do {
152 if ((r = sef_receive(ANY, m_in)) != OK) /* wait for message */
153 panic("sef_receive failed: %d", r);
154 src = m_in->m_source;
156 if(src == VFS_PROC_NR) {
157 if(unmountdone)
158 printf("MFS: unmounted: unexpected message from FS\n");
159 else
160 srcok = 1; /* Normal FS request. */
162 } else
163 printf("MFS: unexpected source %d\n", src);
164 } while(!srcok);
166 assert((src == VFS_PROC_NR && !unmountdone));
170 /*===========================================================================*
171 * reply *
172 *===========================================================================*/
173 static void reply(
174 endpoint_t who,
175 message *m_out /* report result */
178 if (OK != ipc_send(who, m_out)) /* send the message */
179 printf("MFS(%d) was unable to send reply\n", sef_self());
183 #if 0
184 /*===========================================================================*
185 * cch_check *
186 *===========================================================================*/
187 static void cch_check(void)
189 int i;
191 for (i = 0; i < NR_INODES; ++i) {
192 if (inode[i].i_count != cch[i] && req_nr != REQ_GETNODE &&
193 req_nr != REQ_PUTNODE && req_nr != REQ_READSUPER &&
194 req_nr != REQ_MOUNTPOINT && req_nr != REQ_UNMOUNT &&
195 req_nr != REQ_SYNC && req_nr != REQ_LOOKUP) {
196 printf("MFS(%d) inode(%lu) cc: %d req_nr: %d\n", sef_self(),
197 inode[i].i_num, inode[i].i_count - cch[i], req_nr);
200 cch[i] = inode[i].i_count;
203 #endif