code:iinit - ikarishinjieva/unixV6-code-analyze-chs GitHub Wiki
- 该函数在系统启动时调用
- 初始化 文件系统
- 初始化 系统时间
6911
6912 /*
6913 * iinit is called once (from main)
6914 * very early in initialization.
6915 * It reads the root’s super block
6916 * and initializes the current date
6917 * from the last modified date.
6918 *
6919 * panic: iinit -- cannot read the super
6920 * block. Usually because of an IO error.
6921 */
6922 iinit()
6923 {
6924 register *cp, *bp;
6925
6926 (*bdevsw[rootdev.d_major].d_open)(rootdev, 1);
6927 bp = bread(rootdev, 1);
6928 cp = getblk(NODEV);
- 读取 根设备的第一块 bp
- 根设备的第一块 为 超级块
- 关于 超级块,参考 文件系统
6929 if(u.u_error)
- 申请512字节长的缓存 cp
- 这里以NODEV为参数,使用了getblk的第二功能
6930 panic("iinit");
6931 bcopy(bp->b_addr, cp->b_addr, 256);
- 若检测出错误(由前几行的函数抛出错误),调用panic处理错误
6932 brelse(bp);
6933 mount[0].m_bufp = cp;
- 将bp对应的缓存内容 复制到 cp的缓存内容
- 释放bp
- 此时,cp(buffers的一个元素)成为了 设备超级块 的缓存
6934 mount[0].m_dev = rootdev;
6935 cp = cp->b_addr;
- mount[0] 置初值
- m_bufp 指向 cp
- m_dev 置为根设备
6936 cp->s_flock = 0;
- 之前 cp 为 struct buf 类型
- 之后 cp 为 filsys 类型
6937 cp->s_ilock = 0;
6938 cp->s_ronly = 0;
6939 time[0] = cp->s_time[0];
- 置 超级块 的参数,参看filsys
- 两个操作锁 清零
- 只读标志 清零
6940 time[1] = cp->s_time[1];
6941 }
- 用 根设备的最近修改时间 置 系统时间
- 由此看到系统时间不精确,如果根设备停止运转一段时间,需要手动设置系统时间
6942 /* ------------------------- */
6943 /* ------------------------- */