📄 main.c
字号:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/fs/main.c
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22500 /* This file contains the main program of the File System. It consists of
22501 * a loop that gets messages requesting work, carries out the work, and sends
22502 * replies.
22503 *
22504 * The entry points into this file are
22505 * main: main program of the File System
22506 * reply: send a reply to a process after the requested work is done
22507 */
22508
22509 struct super_block; /* proto.h needs to know this */
22510
22511 #include "fs.h"
22512 #include <fcntl.h>
22513 #include <string.h>
22514 #include <sys/ioctl.h>
22515 #include <minix/callnr.h>
22516 #include <minix/com.h>
22517 #include <minix/boot.h>
22518 #include "buf.h"
22519 #include "dev.h"
22520 #include "file.h"
22521 #include "fproc.h"
22522 #include "inode.h"
22523 #include "param.h"
22524 #include "super.h"
22525
22526 FORWARD _PROTOTYPE( void buf_pool, (void) );
22527 FORWARD _PROTOTYPE( void fs_init, (void) );
22528 FORWARD _PROTOTYPE( void get_boot_parameters, (void) );
22529 FORWARD _PROTOTYPE( void get_work, (void) );
22530 FORWARD _PROTOTYPE( void load_ram, (void) );
22531 FORWARD _PROTOTYPE( void load_super, (Dev_t super_dev) );
22532
22533
22534 /*===========================================================================*
22535 * main *
22536 *===========================================================================*/
22537 PUBLIC void main()
22538 {
22539 /* This is the main program of the file system. The main loop consists of
22540 * three major activities: getting new work, processing the work, and sending
22541 * the reply. This loop never terminates as long as the file system runs.
22542 */
22543 int error;
22544
22545 fs_init();
22546
22547 /* This is the main loop that gets work, processes it, and sends replies. */
22548 while (TRUE) {
22549 get_work(); /* sets who and fs_call */
22550
22551 fp = &fproc[who]; /* pointer to proc table struct */
22552 super_user = (fp->fp_effuid == SU_UID ? TRUE : FALSE); /* su? */
22553 dont_reply = FALSE; /* in other words, do reply is default */
22554
22555 /* Call the internal function that does the work. */
22556 if (fs_call < 0 || fs_call >= NCALLS)
22557 error = EBADCALL;
22558 else
22559 error = (*call_vector[fs_call])();
22560
22561 /* Copy the results back to the user and send reply. */
22562 if (dont_reply) continue;
22563 reply(who, error);
22564 if (rdahed_inode != NIL_INODE) read_ahead(); /* do block read ahead */
22565 }
22566 }
22569 /*===========================================================================*
22570 * get_work *
22571 *===========================================================================*/
22572 PRIVATE void get_work()
22573 {
22574 /* Normally wait for new input. However, if 'reviving' is
22575 * nonzero, a suspended process must be awakened.
22576 */
22577
22578 register struct fproc *rp;
22579
22580 if (reviving != 0) {
22581 /* Revive a suspended process. */
22582 for (rp = &fproc[0]; rp < &fproc[NR_PROCS]; rp++)
22583 if (rp->fp_revived == REVIVING) {
22584 who = (int)(rp - fproc);
22585 fs_call = rp->fp_fd & BYTE;
22586 fd = (rp->fp_fd >>8) & BYTE;
22587 buffer = rp->fp_buffer;
22588 nbytes = rp->fp_nbytes;
22589 rp->fp_suspended = NOT_SUSPENDED; /*no longer hanging*/
22590 rp->fp_revived = NOT_REVIVING;
22591 reviving--;
22592 return;
22593 }
22594 panic("get_work couldn't revive anyone", NO_NUM);
22595 }
22596
22597 /* Normal case. No one to revive. */
22598 if (receive(ANY, &m) != OK) panic("fs receive error", NO_NUM);
22599
22600 who = m.m_source;
22601 fs_call = m.m_type;
22602 }
22605 /*===========================================================================*
22606 * reply *
22607 *===========================================================================*/
22608 PUBLIC void reply(whom, result)
22609 int whom; /* process to reply to */
22610 int result; /* result of the call (usually OK or error #) */
22611 {
22612 /* Send a reply to a user process. It may fail (if the process has just
22613 * been killed by a signal), so don't check the return code. If the send
22614 * fails, just ignore it.
22615 */
22616
22617 reply_type = result;
22618 send(whom, &m1);
22619 }
22622 /*===========================================================================*
22623 * fs_init *
22624 *===========================================================================*/
22625 PRIVATE void fs_init()
22626 {
22627 /* Initialize global variables, tables, etc. */
22628
22629 register struct inode *rip;
22630 int i;
22631 message mess;
22632
22633 /* The following initializations are needed to let dev_opcl succeed .*/
22634 fp = (struct fproc *) NULL;
22635 who = FS_PROC_NR;
22636
22637 buf_pool(); /* initialize buffer pool */
22638 get_boot_parameters(); /* get the parameters from the menu */
22639 load_ram(); /* init RAM disk, load if it is root */
22640 load_super(ROOT_DEV); /* load super block for root device */
22641
22642 /* Initialize the 'fproc' fields for process 0 .. INIT. */
22643 for (i = 0; i <= LOW_USER; i+= 1) {
22644 if (i == FS_PROC_NR) continue; /* do not initialize FS */
22645 fp = &fproc[i];
22646 rip = get_inode(ROOT_DEV, ROOT_INODE);
22647 fp->fp_rootdir = rip;
22648 dup_inode(rip);
22649 fp->fp_workdir = rip;
22650 fp->fp_realuid = (uid_t) SYS_UID;
22651 fp->fp_effuid = (uid_t) SYS_UID;
22652 fp->fp_realgid = (gid_t) SYS_GID;
22653 fp->fp_effgid = (gid_t) SYS_GID;
22654 fp->fp_umask = ~0;
22655 }
22656
22657 /* Certain relations must hold for the file system to work at all. */
22658 if (SUPER_SIZE > BLOCK_SIZE) panic("SUPER_SIZE > BLOCK_SIZE", NO_NUM);
22659 if (BLOCK_SIZE % V2_INODE_SIZE != 0) /* this checks V1_INODE_SIZE too */
22660 panic("BLOCK_SIZE % V2_INODE_SIZE != 0", NO_NUM);
22661 if (OPEN_MAX > 127) panic("OPEN_MAX > 127", NO_NUM);
22662 if (NR_BUFS < 6) panic("NR_BUFS < 6", NO_NUM);
22663 if (V1_INODE_SIZE != 32) panic("V1 inode size != 32", NO_NUM);
22664 if (V2_INODE_SIZE != 64) panic("V2 inode size != 64", NO_NUM);
22665 if (OPEN_MAX > 8 * sizeof(long)) panic("Too few bits in fp_cloexec", NO_NUM);
22666
22667 /* Tell the memory task where my process table is for the sake of ps(1). */
22668 mess.m_type = DEV_IOCTL;
22669 mess.PROC_NR = FS_PROC_NR;
22670 mess.REQUEST = MIOCSPSINFO;
22671 mess.ADDRESS = (void *) fproc;
22672 (void) sendrec(MEM, &mess);
22673 }
22676 /*===========================================================================*
22677 * buf_pool *
22678 *===========================================================================*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -