mkfs, mkproto: minor improvements
[minix.git] / servers / mfs / main.c
blob039953fb9aacb66c2f58a3c336db447743c4871b
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 < VFS_BASE) {
59 fs_m_in.m_type += VFS_BASE;
60 req_nr = fs_m_in.m_type;
62 ind = req_nr - VFS_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 SELF_E = getprocnr();
121 lmfs_buf_pool(DEFAULT_NR_BUFS);
123 return(OK);
126 /*===========================================================================*
127 * sef_cb_signal_handler *
128 *===========================================================================*/
129 static void sef_cb_signal_handler(int signo)
131 /* Only check for termination signal, ignore anything else. */
132 if (signo != SIGTERM) return;
134 exitsignaled = 1;
135 (void) fs_sync();
137 /* If unmounting has already been performed, exit immediately.
138 * We might not get another message.
140 if (unmountdone) exit(0);
143 /*===========================================================================*
144 * get_work *
145 *===========================================================================*/
146 static void get_work(m_in)
147 message *m_in; /* pointer to message */
149 int r, srcok = 0;
150 endpoint_t src;
152 do {
153 if ((r = sef_receive(ANY, m_in)) != OK) /* wait for message */
154 panic("sef_receive failed: %d", r);
155 src = m_in->m_source;
157 if(src == VFS_PROC_NR) {
158 if(unmountdone)
159 printf("MFS: unmounted: unexpected message from FS\n");
160 else
161 srcok = 1; /* Normal FS request. */
163 } else
164 printf("MFS: unexpected source %d\n", src);
165 } while(!srcok);
167 assert((src == VFS_PROC_NR && !unmountdone));
171 /*===========================================================================*
172 * reply *
173 *===========================================================================*/
174 static void reply(
175 endpoint_t who,
176 message *m_out /* report result */
179 if (OK != send(who, m_out)) /* send the message */
180 printf("MFS(%d) was unable to send reply\n", SELF_E);
184 #if 0
185 /*===========================================================================*
186 * cch_check *
187 *===========================================================================*/
188 static void cch_check(void)
190 int i;
192 for (i = 0; i < NR_INODES; ++i) {
193 if (inode[i].i_count != cch[i] && req_nr != REQ_GETNODE &&
194 req_nr != REQ_PUTNODE && req_nr != REQ_READSUPER &&
195 req_nr != REQ_MOUNTPOINT && req_nr != REQ_UNMOUNT &&
196 req_nr != REQ_SYNC && req_nr != REQ_LOOKUP) {
197 printf("MFS(%d) inode(%lu) cc: %d req_nr: %d\n", SELF_E,
198 inode[i].i_num, inode[i].i_count - cch[i], req_nr);
201 cch[i] = inode[i].i_count;
204 #endif