[9] | 1 | /* This file contains the main program of the File System. It consists of
|
---|
| 2 | * a loop that gets messages requesting work, carries out the work, and sends
|
---|
| 3 | * replies.
|
---|
| 4 | *
|
---|
| 5 | * The entry points into this file are:
|
---|
| 6 | * main: main program of the File System
|
---|
| 7 | * reply: send a reply to a process after the requested work is done
|
---|
| 8 | *
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | struct super_block; /* proto.h needs to know this */
|
---|
| 12 |
|
---|
| 13 | #include "fs.h"
|
---|
| 14 | #include <fcntl.h>
|
---|
| 15 | #include <string.h>
|
---|
| 16 | #include <stdio.h>
|
---|
| 17 | #include <signal.h>
|
---|
| 18 | #include <stdlib.h>
|
---|
| 19 | #include <sys/ioc_memory.h>
|
---|
| 20 | #include <sys/svrctl.h>
|
---|
| 21 | #include <sys/select.h>
|
---|
| 22 | #include <minix/callnr.h>
|
---|
| 23 | #include <minix/com.h>
|
---|
| 24 | #include <minix/keymap.h>
|
---|
| 25 | #include <minix/const.h>
|
---|
| 26 | #include <minix/endpoint.h>
|
---|
| 27 | #include "buf.h"
|
---|
| 28 | #include "file.h"
|
---|
| 29 | #include "fproc.h"
|
---|
| 30 | #include "inode.h"
|
---|
| 31 | #include "param.h"
|
---|
| 32 | #include "super.h"
|
---|
| 33 |
|
---|
| 34 | FORWARD _PROTOTYPE( void fs_init, (void) );
|
---|
| 35 | FORWARD _PROTOTYPE( void get_work, (void) );
|
---|
| 36 | FORWARD _PROTOTYPE( void init_root, (void) );
|
---|
| 37 |
|
---|
| 38 | /*===========================================================================*
|
---|
| 39 | * main *
|
---|
| 40 | *===========================================================================*/
|
---|
| 41 | PUBLIC int main()
|
---|
| 42 | {
|
---|
| 43 | /* This is the main program of the file system. The main loop consists of
|
---|
| 44 | * three major activities: getting new work, processing the work, and sending
|
---|
| 45 | * the reply. This loop never terminates as long as the file system runs.
|
---|
| 46 | */
|
---|
| 47 | int error;
|
---|
| 48 |
|
---|
| 49 | fs_init();
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | /* This is the main loop that gets work, processes it, and sends replies. */
|
---|
| 53 | while (TRUE) {
|
---|
| 54 | get_work(); /* sets who and call_nr */
|
---|
| 55 | fp = &fproc[who_p]; /* pointer to proc table struct */
|
---|
| 56 | super_user = (fp->fp_effuid == SU_UID ? TRUE : FALSE); /* su? */
|
---|
| 57 |
|
---|
| 58 | /* Check for special control messages first. */
|
---|
| 59 | if (call_nr == PROC_EVENT) {
|
---|
| 60 | /* Assume FS got signal. Synchronize, but don't exit. */
|
---|
| 61 | do_sync();
|
---|
| 62 | } else if (call_nr == SYN_ALARM) {
|
---|
| 63 | /* Alarm timer expired. Used only for select(). Check it. */
|
---|
| 64 | fs_expire_timers(m_in.NOTIFY_TIMESTAMP);
|
---|
| 65 | } else if ((call_nr & NOTIFY_MESSAGE)) {
|
---|
| 66 | /* Device notifies us of an event. */
|
---|
| 67 | dev_status(&m_in);
|
---|
| 68 | } else {
|
---|
| 69 | /* Call the internal function that does the work. */
|
---|
| 70 | if (call_nr < 0 || call_nr >= NCALLS) {
|
---|
| 71 | error = ENOSYS;
|
---|
| 72 | /* Not supposed to happen. */
|
---|
| 73 | printf("FS, warning illegal %d system call by %d\n", call_nr, who_e);
|
---|
| 74 | } else if (fp->fp_pid == PID_FREE) {
|
---|
| 75 | error = ENOSYS;
|
---|
| 76 | printf("FS, bad process, who = %d, call_nr = %d, endpt1 = %d\n",
|
---|
| 77 | who_e, call_nr, m_in.endpt1);
|
---|
| 78 | } else {
|
---|
| 79 | error = (*call_vec[call_nr])();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /* Copy the results back to the user and send reply. */
|
---|
| 83 | if (error != SUSPEND) { reply(who_e, error); }
|
---|
| 84 | if (rdahed_inode != NIL_INODE) {
|
---|
| 85 | read_ahead(); /* do block read ahead */
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | return(OK); /* shouldn't come here */
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /*===========================================================================*
|
---|
| 93 | * get_work *
|
---|
| 94 | *===========================================================================*/
|
---|
| 95 | PRIVATE void get_work()
|
---|
| 96 | {
|
---|
| 97 | /* Normally wait for new input. However, if 'reviving' is
|
---|
| 98 | * nonzero, a suspended process must be awakened.
|
---|
| 99 | */
|
---|
| 100 | register struct fproc *rp;
|
---|
| 101 |
|
---|
| 102 | if (reviving != 0) {
|
---|
| 103 | /* Revive a suspended process. */
|
---|
| 104 | for (rp = &fproc[0]; rp < &fproc[NR_PROCS]; rp++)
|
---|
| 105 | if (rp->fp_pid != PID_FREE && rp->fp_revived == REVIVING) {
|
---|
| 106 | who_p = (int)(rp - fproc);
|
---|
| 107 | who_e = rp->fp_endpoint;
|
---|
| 108 | call_nr = rp->fp_fd & BYTE;
|
---|
| 109 | m_in.fd = (rp->fp_fd >>8) & BYTE;
|
---|
| 110 | m_in.buffer = rp->fp_buffer;
|
---|
| 111 | m_in.nbytes = rp->fp_nbytes;
|
---|
| 112 | rp->fp_suspended = NOT_SUSPENDED; /*no longer hanging*/
|
---|
| 113 | rp->fp_revived = NOT_REVIVING;
|
---|
| 114 | reviving--;
|
---|
| 115 | return;
|
---|
| 116 | }
|
---|
| 117 | panic(__FILE__,"get_work couldn't revive anyone", NO_NUM);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | for(;;) {
|
---|
| 121 | /* Normal case. No one to revive. */
|
---|
| 122 | if (receive(ANY, &m_in) != OK)
|
---|
| 123 | panic(__FILE__,"fs receive error", NO_NUM);
|
---|
| 124 | who_e = m_in.m_source;
|
---|
| 125 | who_p = _ENDPOINT_P(who_e);
|
---|
| 126 | if(who_p < -NR_TASKS || who_p >= NR_PROCS)
|
---|
| 127 | panic(__FILE__,"receive process out of range", who_p);
|
---|
| 128 | if(who_p >= 0 && fproc[who_p].fp_endpoint == NONE) {
|
---|
| 129 | printf("FS: ignoring request from %d, endpointless slot %d (%d)\n",
|
---|
| 130 | m_in.m_source, who_p, m_in.m_type);
|
---|
| 131 | continue;
|
---|
| 132 | }
|
---|
| 133 | if(who_p >= 0 && fproc[who_p].fp_endpoint != who_e) {
|
---|
| 134 | printf("FS: receive endpoint inconsistent (%d, %d, %d).\n",
|
---|
| 135 | who_e, fproc[who_p].fp_endpoint, who_e);
|
---|
| 136 | panic(__FILE__, "FS: inconsistent endpoint ", NO_NUM);
|
---|
| 137 | continue;
|
---|
| 138 | }
|
---|
| 139 | call_nr = m_in.m_type;
|
---|
| 140 | return;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /*===========================================================================*
|
---|
| 145 | * buf_pool *
|
---|
| 146 | *===========================================================================*/
|
---|
| 147 | PRIVATE void buf_pool(void)
|
---|
| 148 | {
|
---|
| 149 | /* Initialize the buffer pool. */
|
---|
| 150 |
|
---|
| 151 | register struct buf *bp;
|
---|
| 152 |
|
---|
| 153 | bufs_in_use = 0;
|
---|
| 154 | front = &buf[0];
|
---|
| 155 | rear = &buf[NR_BUFS - 1];
|
---|
| 156 |
|
---|
| 157 | for (bp = &buf[0]; bp < &buf[NR_BUFS]; bp++) {
|
---|
| 158 | bp->b_blocknr = NO_BLOCK;
|
---|
| 159 | bp->b_dev = NO_DEV;
|
---|
| 160 | bp->b_next = bp + 1;
|
---|
| 161 | bp->b_prev = bp - 1;
|
---|
| 162 | }
|
---|
| 163 | buf[0].b_prev = NIL_BUF;
|
---|
| 164 | buf[NR_BUFS - 1].b_next = NIL_BUF;
|
---|
| 165 |
|
---|
| 166 | for (bp = &buf[0]; bp < &buf[NR_BUFS]; bp++) bp->b_hash = bp->b_next;
|
---|
| 167 | buf_hash[0] = front;
|
---|
| 168 |
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /*===========================================================================*
|
---|
| 172 | * reply *
|
---|
| 173 | *===========================================================================*/
|
---|
| 174 | PUBLIC void reply(whom, result)
|
---|
| 175 | int whom; /* process to reply to */
|
---|
| 176 | int result; /* result of the call (usually OK or error #) */
|
---|
| 177 | {
|
---|
| 178 | /* Send a reply to a user process. If the send fails, just ignore it. */
|
---|
| 179 | int s;
|
---|
| 180 | m_out.reply_type = result;
|
---|
| 181 | s = send(whom, &m_out);
|
---|
| 182 | if (s != OK) printf("FS: couldn't send reply %d to %d: %d\n",
|
---|
| 183 | result, whom, s);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /*===========================================================================*
|
---|
| 187 | * fs_init *
|
---|
| 188 | *===========================================================================*/
|
---|
| 189 | PRIVATE void fs_init()
|
---|
| 190 | {
|
---|
| 191 | /* Initialize global variables, tables, etc. */
|
---|
| 192 | register struct inode *rip;
|
---|
| 193 | register struct fproc *rfp;
|
---|
| 194 | message mess;
|
---|
| 195 | int s;
|
---|
| 196 |
|
---|
| 197 | /* Initialize the process table with help of the process manager messages.
|
---|
| 198 | * Expect one message for each system process with its slot number and pid.
|
---|
| 199 | * When no more processes follow, the magic process number NONE is sent.
|
---|
| 200 | * Then, stop and synchronize with the PM.
|
---|
| 201 | */
|
---|
| 202 | do {
|
---|
| 203 | if (OK != (s=receive(PM_PROC_NR, &mess)))
|
---|
| 204 | panic(__FILE__,"FS couldn't receive from PM", s);
|
---|
| 205 | if (NONE == mess.PR_ENDPT) break;
|
---|
| 206 |
|
---|
| 207 | rfp = &fproc[mess.PR_SLOT];
|
---|
| 208 | rfp->fp_pid = mess.PR_PID;
|
---|
| 209 | rfp->fp_endpoint = mess.PR_ENDPT;
|
---|
| 210 | rfp->fp_realuid = (uid_t) SYS_UID;
|
---|
| 211 | rfp->fp_effuid = (uid_t) SYS_UID;
|
---|
| 212 | rfp->fp_realgid = (gid_t) SYS_GID;
|
---|
| 213 | rfp->fp_effgid = (gid_t) SYS_GID;
|
---|
| 214 | rfp->fp_umask = ~0;
|
---|
| 215 |
|
---|
| 216 | } while (TRUE); /* continue until process NONE */
|
---|
| 217 | mess.m_type = OK; /* tell PM that we succeeded */
|
---|
| 218 | s=send(PM_PROC_NR, &mess); /* send synchronization message */
|
---|
| 219 |
|
---|
| 220 | /* All process table entries have been set. Continue with FS initialization.
|
---|
| 221 | * Certain relations must hold for the file system to work at all. Some
|
---|
| 222 | * extra block_size requirements are checked at super-block-read-in time.
|
---|
| 223 | */
|
---|
| 224 | if (OPEN_MAX > 127) panic(__FILE__,"OPEN_MAX > 127", NO_NUM);
|
---|
| 225 | if (NR_BUFS < 6) panic(__FILE__,"NR_BUFS < 6", NO_NUM);
|
---|
| 226 | if (V1_INODE_SIZE != 32) panic(__FILE__,"V1 inode size != 32", NO_NUM);
|
---|
| 227 | if (V2_INODE_SIZE != 64) panic(__FILE__,"V2 inode size != 64", NO_NUM);
|
---|
| 228 | if (OPEN_MAX > 8 * sizeof(long))
|
---|
| 229 | panic(__FILE__,"Too few bits in fp_cloexec", NO_NUM);
|
---|
| 230 |
|
---|
| 231 | /* The following initializations are needed to let dev_opcl succeed .*/
|
---|
| 232 | fp = (struct fproc *) NULL;
|
---|
| 233 | who_e = who_p = FS_PROC_NR;
|
---|
| 234 |
|
---|
| 235 | buf_pool(); /* initialize buffer pool */
|
---|
| 236 | build_dmap(); /* build device table and map boot driver */
|
---|
| 237 | init_root(); /* init root device and load super block */
|
---|
| 238 | init_select(); /* init select() structures */
|
---|
| 239 |
|
---|
| 240 | /* The root device can now be accessed; set process directories. */
|
---|
| 241 | for (rfp=&fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
|
---|
| 242 | FD_ZERO(&(rfp->fp_filp_inuse));
|
---|
| 243 | if (rfp->fp_pid != PID_FREE) {
|
---|
| 244 | rip = get_inode(root_dev, ROOT_INODE);
|
---|
| 245 | dup_inode(rip);
|
---|
| 246 | rfp->fp_rootdir = rip;
|
---|
| 247 | rfp->fp_workdir = rip;
|
---|
| 248 | } else rfp->fp_endpoint = NONE;
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | /*===========================================================================*
|
---|
| 253 | * init_root *
|
---|
| 254 | *===========================================================================*/
|
---|
| 255 | PRIVATE void init_root()
|
---|
| 256 | {
|
---|
| 257 | int bad;
|
---|
| 258 | register struct super_block *sp;
|
---|
| 259 | register struct inode *rip = NIL_INODE;
|
---|
| 260 | int s;
|
---|
| 261 |
|
---|
| 262 | /* Open the root device. */
|
---|
| 263 | root_dev = DEV_IMGRD;
|
---|
| 264 | if ((s=dev_open(root_dev, FS_PROC_NR, R_BIT|W_BIT)) != OK)
|
---|
| 265 | panic(__FILE__,"Cannot open root device", s);
|
---|
| 266 |
|
---|
| 267 | #if ENABLE_CACHE2
|
---|
| 268 | /* The RAM disk is a second level block cache while not otherwise used. */
|
---|
| 269 | init_cache2(ram_size);
|
---|
| 270 | #endif
|
---|
| 271 |
|
---|
| 272 | /* Initialize the super_block table. */
|
---|
| 273 | for (sp = &super_block[0]; sp < &super_block[NR_SUPERS]; sp++)
|
---|
| 274 | sp->s_dev = NO_DEV;
|
---|
| 275 |
|
---|
| 276 | /* Read in super_block for the root file system. */
|
---|
| 277 | sp = &super_block[0];
|
---|
| 278 | sp->s_dev = root_dev;
|
---|
| 279 |
|
---|
| 280 | /* Check super_block for consistency. */
|
---|
| 281 | bad = (read_super(sp) != OK);
|
---|
| 282 | if (!bad) {
|
---|
| 283 | rip = get_inode(root_dev, ROOT_INODE); /* inode for root dir */
|
---|
| 284 | if ( (rip->i_mode & I_TYPE) != I_DIRECTORY || rip->i_nlinks < 3) bad++;
|
---|
| 285 | }
|
---|
| 286 | if (bad) panic(__FILE__,"Invalid root file system", NO_NUM);
|
---|
| 287 |
|
---|
| 288 | sp->s_imount = rip;
|
---|
| 289 | dup_inode(rip);
|
---|
| 290 | sp->s_isup = rip;
|
---|
| 291 | sp->s_rd_only = 0;
|
---|
| 292 | return;
|
---|
| 293 | }
|
---|