minix/servers/fs/main.c
Ben Gras 2384a85296 FS support for grant-based i/o.
For character device i/o, FS does a so-called 'magic' grant to let the
driver copy from or to user space. As this is done in FS address space,
the driver is told to do this in FS address space. The redirection to
the right user process then happens at copy-time in the kernel, using the
FS grant table. This also happens for DEV_READ and DEV_WRITE on block
devices.

For other block device i/o, which happens from/to FS buffers, FS does
a 'direct' grant to its own address space for the driver.

After the i/o returns, this access has to be K-I-L-L-E-D, revoked.
Sometimes this is after a SUSPEND and DEV_REVIVE, in which case the
revoking happens in pipe.c.

This conversion happens in safe_io_conversion() in device.c, called
by dev_io and dev_bio.

FS has to pre-allocate its own space for these grant tables. This happens
in main.c.
2006-06-20 10:12:09 +00:00

463 lines
13 KiB
C

/* This file contains the main program of the File System. It consists of
* a loop that gets messages requesting work, carries out the work, and sends
* replies.
*
* The entry points into this file are:
* main: main program of the File System
* reply: send a reply to a process after the requested work is done
*
*/
struct super_block; /* proto.h needs to know this */
#include "fs.h"
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/ioc_memory.h>
#include <sys/svrctl.h>
#include <sys/select.h>
#include <minix/callnr.h>
#include <minix/com.h>
#include <minix/keymap.h>
#include <minix/const.h>
#include <minix/endpoint.h>
#include <minix/safecopies.h>
#include "buf.h"
#include "file.h"
#include "fproc.h"
#include "inode.h"
#include "param.h"
#include "super.h"
FORWARD _PROTOTYPE( void fs_init, (void) );
FORWARD _PROTOTYPE( void get_work, (void) );
FORWARD _PROTOTYPE( void init_root, (void) );
FORWARD _PROTOTYPE( void service_pm, (void) );
/*===========================================================================*
* main *
*===========================================================================*/
PUBLIC int main()
{
/* This is the main program of the file system. The main loop consists of
* three major activities: getting new work, processing the work, and sending
* the reply. This loop never terminates as long as the file system runs.
*/
int error;
fs_init();
/* This is the main loop that gets work, processes it, and sends replies. */
while (TRUE) {
get_work(); /* sets who and call_nr */
fp = &fproc[who_p]; /* pointer to proc table struct */
super_user = (fp->fp_effuid == SU_UID ? TRUE : FALSE); /* su? */
if (who_e == PM_PROC_NR && call_nr != PROC_EVENT)
printf("FS: strange, got message %d from PM\n", call_nr);
/* Check for special control messages first. */
if ((call_nr & NOTIFY_MESSAGE)) {
if (call_nr == PROC_EVENT)
{
/* PM tries to get FS to do something */
service_pm();
}
else if (call_nr == SYN_ALARM)
{
/* Alarm timer expired. Used only for select().
* Check it.
*/
fs_expire_timers(m_in.NOTIFY_TIMESTAMP);
}
else
{
/* Device notifies us of an event. */
dev_status(&m_in);
}
continue;
}
switch(call_nr)
{
case DEVCTL:
error= do_devctl();
if (error != SUSPEND) reply(who_e, error);
break;
default:
/* Call the internal function that does the work. */
if (call_nr < 0 || call_nr >= NCALLS) {
error = ENOSYS;
/* Not supposed to happen. */
printf("FS, warning illegal %d system call by %d\n",
call_nr, who_e);
} else if (fp->fp_pid == PID_FREE) {
error = ENOSYS;
printf(
"FS, bad process, who = %d, call_nr = %d, endpt1 = %d\n",
who_e, call_nr, m_in.endpt1);
} else {
error = (*call_vec[call_nr])();
}
/* Copy the results back to the user and send reply. */
if (error != SUSPEND) { reply(who_e, error); }
if (rdahed_inode != NIL_INODE) {
read_ahead(); /* do block read ahead */
}
}
}
return(OK); /* shouldn't come here */
}
/*===========================================================================*
* get_work *
*===========================================================================*/
PRIVATE void get_work()
{
/* Normally wait for new input. However, if 'reviving' is
* nonzero, a suspended process must be awakened.
*/
register struct fproc *rp;
if (reviving != 0) {
/* Revive a suspended process. */
for (rp = &fproc[0]; rp < &fproc[NR_PROCS]; rp++)
if (rp->fp_pid != PID_FREE && rp->fp_revived == REVIVING) {
who_p = (int)(rp - fproc);
who_e = rp->fp_endpoint;
call_nr = rp->fp_fd & BYTE;
m_in.fd = (rp->fp_fd >>8) & BYTE;
m_in.buffer = rp->fp_buffer;
m_in.nbytes = rp->fp_nbytes;
rp->fp_suspended = NOT_SUSPENDED; /*no longer hanging*/
rp->fp_revived = NOT_REVIVING;
reviving--;
/* This should be a pipe I/O, not a device I/O.
* If it is, it'll 'leak' grants.
*/
assert(!GRANT_VALID(rp->fp_grant));
return;
}
panic(__FILE__,"get_work couldn't revive anyone", NO_NUM);
}
for(;;) {
/* Normal case. No one to revive. */
if (receive(ANY, &m_in) != OK)
panic(__FILE__,"fs receive error", NO_NUM);
who_e = m_in.m_source;
who_p = _ENDPOINT_P(who_e);
if(who_p < -NR_TASKS || who_p >= NR_PROCS)
panic(__FILE__,"receive process out of range", who_p);
if(who_p >= 0 && fproc[who_p].fp_endpoint == NONE) {
printf("FS: ignoring request from %d, endpointless slot %d (%d)\n",
m_in.m_source, who_p, m_in.m_type);
continue;
}
if(who_p >= 0 && fproc[who_p].fp_endpoint != who_e) {
printf("FS: receive endpoint inconsistent (%d, %d, %d).\n",
who_e, fproc[who_p].fp_endpoint, who_e);
panic(__FILE__, "FS: inconsistent endpoint ", NO_NUM);
continue;
}
call_nr = m_in.m_type;
return;
}
}
/*===========================================================================*
* buf_pool *
*===========================================================================*/
PRIVATE void buf_pool(void)
{
/* Initialize the buffer pool. */
register struct buf *bp;
bufs_in_use = 0;
front = &buf[0];
rear = &buf[NR_BUFS - 1];
for (bp = &buf[0]; bp < &buf[NR_BUFS]; bp++) {
bp->b_blocknr = NO_BLOCK;
bp->b_dev = NO_DEV;
bp->b_next = bp + 1;
bp->b_prev = bp - 1;
}
buf[0].b_prev = NIL_BUF;
buf[NR_BUFS - 1].b_next = NIL_BUF;
for (bp = &buf[0]; bp < &buf[NR_BUFS]; bp++) bp->b_hash = bp->b_next;
buf_hash[0] = front;
}
/*===========================================================================*
* reply *
*===========================================================================*/
PUBLIC void reply(whom, result)
int whom; /* process to reply to */
int result; /* result of the call (usually OK or error #) */
{
/* Send a reply to a user process. If the send fails, just ignore it. */
int s;
m_out.reply_type = result;
s = send(whom, &m_out);
if (s != OK) printf("FS: couldn't send reply %d to %d: %d\n",
result, whom, s);
}
/*===========================================================================*
* fs_init *
*===========================================================================*/
PRIVATE void fs_init()
{
/* Initialize global variables, tables, etc. */
register struct inode *rip;
register struct fproc *rfp;
message mess;
int s;
/* Maximum number of outstanding grants is one full i/o
* vector, and all processes hanging on SUSPENDed i/o, and
* grants for printf() to tty and log.
*
* Space is declared for it here, and cpf_preallocate()
* uses it internally and internally tells the kernel
* about it. FS then never touches the data again directly,
* only through the cpf_* library routines.
*/
#define NGRANTS (NR_PROCS + NR_IOREQS + 10)
static cp_grant_t grants[NGRANTS];
/* Set data copy grant table, as FS can't allocate it dynamically. */
if(cpf_preallocate(grants, NGRANTS) != OK)
panic(__FILE__,"cpf_preallocate failed", NO_NUM);
/* Initialize the process table with help of the process manager messages.
* Expect one message for each system process with its slot number and pid.
* When no more processes follow, the magic process number NONE is sent.
* Then, stop and synchronize with the PM.
*/
do {
if (OK != (s=receive(PM_PROC_NR, &mess)))
panic(__FILE__,"FS couldn't receive from PM", s);
if (NONE == mess.PR_ENDPT) break;
rfp = &fproc[mess.PR_SLOT];
rfp->fp_pid = mess.PR_PID;
rfp->fp_endpoint = mess.PR_ENDPT;
rfp->fp_realuid = (uid_t) SYS_UID;
rfp->fp_effuid = (uid_t) SYS_UID;
rfp->fp_realgid = (gid_t) SYS_GID;
rfp->fp_effgid = (gid_t) SYS_GID;
rfp->fp_umask = ~0;
rfp->fp_grant = GRANT_INVALID;
} while (TRUE); /* continue until process NONE */
mess.m_type = OK; /* tell PM that we succeeded */
s=send(PM_PROC_NR, &mess); /* send synchronization message */
/* All process table entries have been set. Continue with FS initialization.
* Certain relations must hold for the file system to work at all. Some
* extra block_size requirements are checked at super-block-read-in time.
*/
if (OPEN_MAX > 127) panic(__FILE__,"OPEN_MAX > 127", NO_NUM);
if (NR_BUFS < 6) panic(__FILE__,"NR_BUFS < 6", NO_NUM);
if (V1_INODE_SIZE != 32) panic(__FILE__,"V1 inode size != 32", NO_NUM);
if (V2_INODE_SIZE != 64) panic(__FILE__,"V2 inode size != 64", NO_NUM);
if (OPEN_MAX > 8 * sizeof(long))
panic(__FILE__,"Too few bits in fp_cloexec", NO_NUM);
/* The following initializations are needed to let dev_opcl succeed .*/
fp = (struct fproc *) NULL;
who_e = who_p = FS_PROC_NR;
buf_pool(); /* initialize buffer pool */
build_dmap(); /* build device table and map boot driver */
init_root(); /* init root device and load super block */
init_select(); /* init select() structures */
/* The root device can now be accessed; set process directories. */
for (rfp=&fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
FD_ZERO(&(rfp->fp_filp_inuse));
if (rfp->fp_pid != PID_FREE) {
rip = get_inode(root_dev, ROOT_INODE);
dup_inode(rip);
rfp->fp_rootdir = rip;
rfp->fp_workdir = rip;
} else rfp->fp_endpoint = NONE;
}
}
/*===========================================================================*
* init_root *
*===========================================================================*/
PRIVATE void init_root()
{
int bad;
register struct super_block *sp;
register struct inode *rip = NIL_INODE;
int s;
/* Open the root device. */
root_dev = DEV_IMGRD;
if ((s=dev_open(root_dev, FS_PROC_NR, R_BIT|W_BIT)) != OK)
panic(__FILE__,"Cannot open root device", s);
#if ENABLE_CACHE2
/* The RAM disk is a second level block cache while not otherwise used. */
init_cache2(ram_size);
#endif
/* Initialize the super_block table. */
for (sp = &super_block[0]; sp < &super_block[NR_SUPERS]; sp++)
sp->s_dev = NO_DEV;
/* Read in super_block for the root file system. */
sp = &super_block[0];
sp->s_dev = root_dev;
/* Check super_block for consistency. */
bad = (read_super(sp) != OK);
if (!bad) {
rip = get_inode(root_dev, ROOT_INODE); /* inode for root dir */
if ( (rip->i_mode & I_TYPE) != I_DIRECTORY || rip->i_nlinks < 3) bad++;
}
if (bad) panic(__FILE__,"Invalid root file system", NO_NUM);
sp->s_imount = rip;
dup_inode(rip);
sp->s_isup = rip;
sp->s_rd_only = 0;
return;
}
/*===========================================================================*
* service_pm *
*===========================================================================*/
PRIVATE void service_pm()
{
int r, call;
message m;
/* Ask PM for work until there is nothing left to do */
for (;;)
{
m.m_type= PM_GET_WORK;
r= sendrec(PM_PROC_NR, &m);
if (r != OK)
{
panic("fs", "service_pm: sendrec failed", r);
}
if (m.m_type == PM_IDLE) {
break;
}
call= m.m_type;
switch(call)
{
case PM_STIME:
boottime= m.PM_STIME_TIME;
/* No need to report status to PM */
break;
case PM_SETSID:
pm_setsid(m.PM_SETSID_PROC);
/* No need to report status to PM */
break;
case PM_SETGID:
pm_setgid(m.PM_SETGID_PROC, m.PM_SETGID_EGID,
m.PM_SETGID_RGID);
/* No need to report status to PM */
break;
case PM_SETUID:
pm_setuid(m.PM_SETUID_PROC, m.PM_SETUID_EGID,
m.PM_SETUID_RGID);
/* No need to report status to PM */
break;
case PM_FORK:
pm_fork(m.PM_FORK_PPROC, m.PM_FORK_CPROC,
m.PM_FORK_CPID);
/* No need to report status to PM */
break;
case PM_EXIT:
case PM_EXIT_TR:
pm_exit(m.PM_EXIT_PROC);
/* Reply dummy status to PM for synchronization */
m.m_type= (call == PM_EXIT_TR ? PM_EXIT_REPLY_TR :
PM_EXIT_REPLY);
/* Keep m.PM_EXIT_PROC */
r= send(PM_PROC_NR, &m);
if (r != OK)
panic(__FILE__, "service_pm: send failed", r);
break;
case PM_UNPAUSE:
case PM_UNPAUSE_TR:
unpause(m.PM_UNPAUSE_PROC);
/* No need to report status to PM */
break;
case PM_REBOOT:
pm_reboot();
/* Reply dummy status to PM for synchronization */
m.m_type= PM_REBOOT_REPLY;
r= send(PM_PROC_NR, &m);
if (r != OK)
panic(__FILE__, "service_pm: send failed", r);
break;
case PM_EXEC:
r= pm_exec(m.PM_EXEC_PROC, m.PM_EXEC_PATH,
m.PM_EXEC_PATH_LEN, m.PM_EXEC_FRAME,
m.PM_EXEC_FRAME_LEN);
/* Reply status to PM */
m.m_type= PM_EXEC_REPLY;
/* Keep m.PM_EXEC_PROC */
m.PM_EXEC_STATUS= r;
r= send(PM_PROC_NR, &m);
if (r != OK)
panic(__FILE__, "service_pm: send failed", r);
break;
case PM_DUMPCORE:
r= pm_dumpcore(m.PM_CORE_PROC,
(struct mem_map *)m.PM_CORE_SEGPTR);
/* Reply status to PM */
m.m_type= PM_CORE_REPLY;
/* Keep m.PM_CORE_PROC */
m.PM_CORE_STATUS= r;
r= send(PM_PROC_NR, &m);
if (r != OK)
panic(__FILE__, "service_pm: send failed", r);
break;
default:
panic("fs", "service_pm: unknown call", m.m_type);
}
}
}