os-xv6分析-文件系统-fs.c 代码分析
// fs.c 代码设计三个部分,盘块操作部分,inode操作代码,目录操作代码
// File system implementation. Five layers:
// + Blocks: allocator for raw disk blocks.
// + Log: crash recovery for multi-step updates.
// + Files: inode allocator, reading, writing, metadata.
// + Directories: inode with special contents (list of other inodes!)
// + Names: paths like /usr/rtm/xv6/fs.c for convenient naming.
//
// This file contains the low-level file system manipulation
// routines. The (higher-level) system call implementations
// are in sysfile.c.
//此文件包含低级文件系统操作例程。(高级)系统调用实现在sysfile.c中。
// 导包
#include "types.h"
#include "defs.h"
#include "param.h"
#include "stat.h"
#include "mmu.h"
#include "proc.h"
#include "spinlock.h"
#include "sleeplock.h"
#include "fs.h"
#include "buf.h"
#include "file.h"
// 定义函数min
#define min(a, b) ((a) < (b) ? (a) : (b))
static void itrunc(struct inode*); // 声明函数
// there should be one superblock per disk device, but we run with
// only one device
struct superblock sb;
// Read the super block.
void
readsb(int dev, struct superblock *sb) // 读入超级块
{
struct buf *bp;
bp = bread(dev, 1); // 超级块编号为1,因此执行bread(dev,1)
memmove(sb, bp->data, sizeof(*sb));
// 通过memmove()从块缓存复制到内存超级块对象sb
brelse(bp); // 释放超级块
}
// Zero a block.
static void
bzero(int dev, int bno) // 清零指定盘块
{
struct buf *bp;
bp = bread(dev, bno); // 获取指定盘号的盘块
memset(bp->data, 0, BSIZE); // 块缓存清0
log_write(bp); // 调用log_write()函数写回
brelse(bp); // 释放盘块
}
// Blocks.
// Allocate a zeroed disk block.
static uint
balloc(uint dev) // 分配一个空闲磁盘盘块,并将其内容清零
{
int b, bi, m;
struct buf *bp;
bp = 0;
for(b = 0; b < sb.size; b += BPB){ // 遍历所有盘块,每次处理BPB个块
bp = bread(dev, BBLOCK(b, sb)); // 读入一个bmap块
for(bi = 0; bi < BPB && b + bi < sb.size; bi++){
//遍历bmap块管理的所有盘块
m = 1 << (bi % 8);
if((bp->data[bi/8] & m) == 0){ // Is block free? // 检查对应的位
bp->data[bi/8] |= m; // Mark block in use.// 空闲,则标记为分配
log_write(bp); // 修改对应的bmap块
brelse(bp); // 释放盘块
bzero(dev, b + bi); // 将刚分配的块清空
return b + bi;
}
}
brelse(bp); // 释放盘块
}
panic("balloc: out of blocks"); // 否则输出无盘块
}
// Free a disk block.
static void
bfree(int dev, uint b) // 释放一个磁盘盘块
{
struct buf *bp;
int bi, m;
bp = bread(dev, BBLOCK(b, sb)); // 读取超级块
bi = b % BPB;
m = 1 << (bi % 8);
if((bp->data[bi/8] & m) == 0) //检查对应位
panic("freeing free block");
bp->data[bi/8] &= ~m; // 位清零
log_write(bp); // 调用log_write函数回写,完成位图的更新修改
brelse(bp);// 释放盘块
}
// 第二部分,inode操作
// Inodes.
//
// An inode describes a single unnamed file.
// The inode disk structure holds metadata: the file's type,
// its size, the number of links referring to it, and the
// list of blocks holding the file's content.
//
// The inodes are laid out sequentially on disk at
// sb.startinode. Each inode has a number, indicating its
// position on the disk.
//
// The kernel keeps a cache of in-use inodes in memory
// to provide a place for synchronizing access
// to inodes used by multiple processes. The cached
// inodes include book-keeping information that is
// not stored on disk: ip->ref and ip->valid.
//
// An inode and its in-memory representation go through a
// sequence of states before they can be used by the
// rest of the file system code.
//
// * Allocation: an inode is allocated if its type (on disk)
// is non-zero. ialloc() allocates, and iput() frees if
// the reference and link counts have fallen to zero.
//
// * Referencing in cache: an entry in the inode cache
// is free if ip->ref is zero. Otherwise ip->ref tracks
// the number of in-memory pointers to the entry (open
// files and current directories). iget() finds or
// creates a cache entry and increments its ref; iput()
// decrements ref.
//
// * Valid: the information (type, size, &c) in an inode
// cache entry is only correct when ip->valid is 1.
// ilock() reads the inode from
// the disk and sets ip->valid, while iput() clears
// ip->valid if ip->ref has fallen to zero.
//
// * Locked: file system code may only examine and modify
// the information in an inode and its content if it
// has first locked the inode.
//
// Thus a typical sequence is:
// ip = iget(dev, inum)
// ilock(ip)
// ... examine and modify ip->xxx ...
// iunlock(ip)
// iput(ip)
//
// ilock() is separate from iget() so that system calls can
// get a long-term reference to an inode (as for an open file)
// and only lock it for short periods (e.g., in read()).
// The separation also helps avoid deadlock and races during
// pathname lookup. iget() increments ip->ref so that the inode
// stays cached and pointers to it remain valid.
//
// Many internal file system functions expect the caller to
// have locked the inodes involved; this lets callers create
// multi-step atomic operations.
//
// The icache.lock spin-lock protects the allocation of icache
// entries. Since ip->ref indicates whether an entry is free,
// and ip->dev and ip->inum indicate which i-node an entry
// holds, one must hold icache.lock while using any of those fields.
//
// An ip->lock sleep-lock protects all ip-> fields other than ref,
// dev, and inum. One must hold ip->lock in order to
// read or write that inode's ip->valid, ip->size, ip->type, &c.
// 索引节点缓存icache结构体,磁盘索引节点在内存中的缓存,可以加快
// 索引节点的读写操作。
struct {
struct spinlock lock;
struct inode inode[NINODE];
} icache;
void
iinit(int dev) //初始化内存索引节点缓存
{
int i = 0;
initlock(&icache.lock, "icache"); // 初始化互斥锁
// 扫描NINODE,初始化睡眠锁
for(i = 0; i < NINODE; i++) {
initsleeplock(&icache.inode[i].lock, "inode");
}
readsb(dev, &sb); // 读入超级块
// 打印磁盘布局信息
cprintf("sb: size %d nblocks %d ninodes %d nlog %d logstart %d\
inodestart %d bmap start %d\n", sb.size, sb.nblocks,
sb.ninodes, sb.nlog, sb.logstart, sb.inodestart,
sb.bmapstart);
}
static struct inode* iget(uint dev, uint inum); //声明获取inode缓存函数
//PAGEBREAK!
// Allocate an inode on device dev.
//在设备设备上分配inode。
// Mark it as allocated by giving it type .
//通过指定类型将其标记为已分配。
// Returns an unlocked but allocated and referenced inode.
//返回未锁定但已分配和引用的索引节点。
struct inode*
ialloc(uint dev, short type) // 在磁盘上寻找空闲inode,并返回其在内存中的表示
{
int inum; //定义for 循环中的循坏变量
struct buf *bp; //定义单块缓冲区buf结构体指针
struct dinode *dip; // 定义索引节点dinode指针
for(inum = 1; inum < sb.ninodes; inum++){ //遍历所有的inode
bp = bread(dev, IBLOCK(inum, sb)); //读入inum对应的盘块
//bread(),块读操作,读取扇区号对应的盘块数据
//IBLOCK(),根据inode的编号计算该inode在磁盘中的扇区号;
dip = (struct dinode*)bp->data + inum%IPB; // 定位到inum号inode
//IPB表示一个扇区最大容纳inode值
// inum%IPB 确定偏移地址
//盘块数据强转为索引节点指针+偏移地址得到inode在磁盘上的数据结构信息
if(dip->type == 0){ // a free inode //判定是否空闲未用,即
//type是否为0,为0表示空闲
memset(dip, 0, sizeof(*dip)); //将inode结构清零
dip->type = type; // 标记类型,表示此块被占用
log_write(bp); // mark it allocated on the disk
//将修改后的inode信息写回磁盘
brelse(bp); //释放bp指定的块缓存,并将其插入到块缓存LRU头部
return iget(dev, inum); //iget(),根据设备号dev和索引节点号inum在索
//引节点缓存中查找,返回所匹配的索引节点缓存
}
brelse(bp); //释放bp指定的块缓存,并将其插入到块缓存LRU头部
}
panic("ialloc: no inodes"); //若无空闲块,则输出“ialloc: no inodes”
}
// Copy a modified in-memory inode to disk.
// Must be called after every change to an ip->xxx field
// that lives on disk, since i-node cache is write-through.
// Caller must hold ip->lock.
// inode写回操作,iupdate()讲inode缓存的内容更新到磁盘dinode上,最后
// 写出到磁盘中。
void
iupdate(struct inode *ip) // 写回操作
{
struct buf *bp;
struct dinode *dip;
bp = bread(ip->dev, IBLOCK(ip->inum, sb)); //获得inode所在的块缓存
dip = (struct dinode*)bp->data + ip->inum%IPB; // 定位inode位置
// 以下操作将inode内容更新到dinode中
dip->type = ip->type;
dip->major = ip->major;
dip->minor = ip->minor;
dip->nlink = ip->nlink;
dip->size = ip->size;
memmove(dip->addrs, ip->addrs, sizeof(ip->addrs));
// 通过memmove()从inode复制到dinode
log_write(bp); // 日志方式写出
brelse(bp); // 释放盘块
}
// Find the inode with number inum on device dev
// and return the in-memory copy. Does not lock
// the inode and does not read it from disk.
// 获取inode缓存,iget()根据设备好dev,索引节点号inum,在索引节点缓存中
// 查找,返回所匹配的索引节点缓存,或者分配一个空闲的索引节点缓存,
// 此操作只是inode缓存的操作,并不涉及磁盘的inode操作
static struct inode*
iget(uint dev, uint inum) //获取inode缓存
{
struct inode *ip, *empty;
acquire(&icache.lock); // 获得锁
// Is the inode already cached?
empty = 0;
for(ip = &icache.inode[0]; ip < &icache.inode[NINODE]; ip++){ //遍历缓存
if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){ // 成功匹配
ip->ref++; //增加引用计数
release(&icache.lock); // 释放锁
return ip; // 返回匹配的inode
}
if(empty == 0 && ip->ref == 0) // Remember empty slot. //找到空闲块
empty = ip; //记录第一个空闲的inode
}
// Recycle an inode cache entry.
if(empty == 0) // 到此处说明没有匹配的inode
panic("iget: no inodes"); // 若没有空闲的inode缓存,panic(需回收)
ip = empty; //使用锁找到的空闲inode
ip->dev = dev;
ip->inum = inum;
ip->ref = 1;
ip->valid = 0;
release(&icache.lock); //释放锁
return ip; // 返回空前的inode
}
// Increment reference count for ip.
// Returns ip to enable ip = idup(ip1) idiom.
struct inode*
idup(struct inode *ip) // 增加inode的引用
{
acquire(&icache.lock); // 获得锁
ip->ref++; // 索引节点缓存的引用计数增加,将其成员ref++
release(&icache.lock); // 释放锁
return ip; // 返回增加引用数的inode
}
// Lock the given inode.
// Reads the inode from disk if necessary.
void
ilock(struct inode *ip) // 对指定的inode缓存加锁
{
struct buf *bp;
struct dinode *dip;
if(ip == 0 || ip->ref < 1)
panic("ilock");
acquiresleep(&ip->lock); //获得睡眠锁
// 若指定的inode缓存块的valid无效,还需要从磁盘读入其内容
// 并将其Valid设置为有效
if(ip->valid == 0){
bp = bread(ip->dev, IBLOCK(ip->inum, sb)); //从磁盘读取内容
dip = (struct dinode*)bp->data + ip->inum%IPB; //定位inode位置
// 将dinode内容更新到inode
ip->type = dip->type;
ip->major = dip->major;
ip->minor = dip->minor;
ip->nlink = dip->nlink;
ip->size = dip->size;
memmove(ip->addrs, dip->addrs, sizeof(ip->addrs));
brelse(bp); // 释放盘块
ip->valid = 1; // valid置1
if(ip->type == 0)
panic("ilock: no type");
}
}
// Unlock the given inode.
void
iunlock(struct inode *ip) // 解锁锁定
{
if(ip == 0 || !holdingsleep(&ip->lock) || ip->ref < 1)
panic("iunlock");
releasesleep(&ip->lock); //释放inode缓存的自旋锁
}
// Drop a reference to an in-memory inode.
// If that was the last reference, the inode cache entry can
// be recycled.
// If that was the last reference and the inode has no links
// to it, free the inode (and its content) on disk.
// All calls to iput() must be inside a transaction in
// case it has to free the inode.
// 减少inode引用计数,iput()将指定的inode缓存引用计数减一,若自身是最后//一个引用该inode缓存的,则释放该缓存进行回收
void
iput(struct inode *ip)
{
acquiresleep(&ip->lock); // 获得自旋锁
if(ip->valid && ip->nlink == 0){ // 最后一个引用,释放缓存进行回收
acquire(&icache.lock);
int r = ip->ref;
release(&icache.lock);
// 对应盘块无引用
if(r == 1){
// inode has no links and no other references: truncate and free.
itrunc(ip); // 释放磁盘数据文件
ip->type = 0; // 表示空闲未用,从而完成释放和回收
iupdate(ip); //调用iupdate函数进行写回操作
ip->valid = 0;
}
}
releasesleep(&ip->lock); // 释放自旋锁
acquire(&icache.lock); //获得锁
ip->ref--; //引用数减一
release(&icache.lock); // 释放锁
}
// Common idiom: unlock, then put.
// 常用:先解锁,再减
void
iunlockput(struct inode *ip)
{
iunlock(ip); // iunlock 解锁
iput(ip); // 执行iput函数
}
//PAGEBREAK!
// Inode content
//
// The content (data) associated with each inode is stored
// in blocks on the disk. The first NDIRECT block numbers
// are listed in ip->addrs[]. The next NINDIRECT blocks are
// listed in block ip->addrs[NDIRECT].
//与每个inode相关联的内容(数据)存储在磁盘上的块中。第一个NDIRECT
//块号列在ip->addrs[]中。接下来的NINIDIRECT块列在块ip->addrs[NDIRECT]
//中。
// Return the disk block address of the nth block in inode ip.
//返回inode ip中第n个块的磁盘块地址。
// If there is no such block, bmap allocates one.
//如果没有这样的块,bmap将分配一个。
// bmap通过给定的inode的数据块号bn,返回此数据块在磁盘上扇区号,如果
//此数据块还没分配则分配一个。
static uint
bmap(struct inode *ip, uint bn)
{
uint addr, *a; //定义扇区号变量和指针
struct buf *bp; //定义但缓存区指针变量
if(bn < NDIRECT){ //判断数据块号是否小于第一个NDIRECT快号,若小//于进if
if((addr = ip->addrs[bn]) == 0) //将数据块号bn的地址赋值为addr变量, //并判断是否为0
ip->addrs[bn] = addr = balloc(ip->dev); //为0则通过balloc分配一个数据//块并记录数据块的扇区号,
return addr; //返回地址(扇区号)
}
bn -= NDIRECT; //bn减去直接索引号,fs.h中定义#define NDIRECT 12
if(bn < NINDIRECT){ //数据块号是否在间接address表中
// Load indirect block, allocating if necessary. //加载间接块,必要时进行分配
if((addr = ip->addrs[NDIRECT]) == 0) //判断间接表是否存在即addr是否为0
ip->addrs[NDIRECT] = addr = balloc(ip->dev); //为0,间接索引盘块还未//分配则通过balloc分配一个盘块
//用于间接索引并记录
bp = bread(ip->dev, addr); //通过bread()函数读取间接块的内容
a = (uint*)bp->data; //指针a指向间接块的内容
if((addr = a[bn]) == 0){ //判断对应的数据块存不存在,即未建立间接索引
a[bn] = addr = balloc(ip->dev); //不存在balloc分配一个数据判断,并记录
log_write(bp); //上述记录操作更新了磁盘数据,需要log_write回写磁盘
}
brelse(bp); //释放不再使用的冲区
return addr; //返回bn所映射的盘块号
}
panic("bmap: out of range"); //若即不在ip->addrs[]又不在
//ip->addrs[NDIRECT],则输出"bmap: out of range"
}
// Truncate inode (discard contents).
// Only called when the inode has no links
// to it (no directory entries referring to it)
// and has no in-memory reference to it (is
// not an open file or current directory).
// 截断inode(丢弃内容)。仅当inode没有指向它的链接(没有引用它的目录
// 条目)并且没有对它的内存引用(不是打开的文件或当前目录)时调用。
static void
itrunc(struct inode *ip) // 截断文件itrunc函数,将索引节点管理的文件数据
//(直接块和间接块)都释放掉
{
int i, j;
struct buf *bp;
uint *a;
// 循坏每个磁盘盘块,回收前12个数据块
for(i = 0; i < NDIRECT; i++){
if(ip->addrs[i]){
bfree(ip->dev, ip->addrs[i]); //使用bfree()函数释放(数据盘块的位图清零)
ip->addrs[i] = 0; //直接address表置0
}
}
// 判断是否存在间接address
if(ip->addrs[NDIRECT]){
bp = bread(ip->dev, ip->addrs[NDIRECT]); //读取间接盘块内容
a = (uint*)bp->data;
for(j = 0; j < NINDIRECT; j++){ //回收间接表中的所有的有效数据块
if(a[j])
bfree(ip->dev, a[j]);
}
brelse(bp); // 释放块缓存
bfree(ip->dev, ip->addrs[NDIRECT]); // 回收间接表所在的数据块
ip->addrs[NDIRECT] = 0;
}
ip->size = 0;
iupdate(ip); //调用写回iupdate函数写回到磁盘中。
}
// Copy stat information from inode.
// Caller must hold ip->lock.
void
stati(struct inode *ip, struct stat *st) // 拷贝inode的信息给上层使用
{
//将inode的信息拷贝到上层信息
st->dev = ip->dev;
st->ino = ip->inum;
st->type = ip->type;
st->nlink = ip->nlink;
st->size = ip->size;
}
//PAGEBREAK!
// Read data from inode.
// Caller must hold ip->lock.
// readi()函数用于从inode对应的磁盘文件的偏移off处,读入n给字节到
// dst指向的数据缓存区。
int
readi(struct inode *ip, char *dst, uint off, uint n)
{
uint tot, m;
struct buf *bp;
if(ip->type == T_DEV){ // 如果是设备文件
if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read)
return -1;
return devsw[ip->major].read(ip, dst, n); //使用设备文件自己的read操作函数
}
// 以下为磁盘文件的读操作
if(off > ip->size || off + n < off) //检查读入起点是否越界
return -1;
if(off + n > ip->size) //检查读入终点是否越界
n = ip->size - off; //只提供文件的有效数据
for(tot=0; tot<n; tot+=m, off+=m, dst+=m){ //逐个盘块读入
bp = bread(ip->dev, bmap(ip, off/BSIZE)); // off/BSIZE表示将偏移起始地址转//换为Inode的数据块号。通过bmap
//得到对应的扇区号,然后读取到 //buffer cache。
m = min(n - tot, BSIZE - off%BSIZE); // 调整偏移量的增量。n-tot表示还剩多//少字节没有读取,off%BSIZE表示off
//偏移在数据块中的偏移,BSIZE - //off%BSIZE表示在一个数据块中剩余//的字节数。(n - tot), (BSIZE - //off%BSIZE)中的最小者表示当前需
//要读取的字节数。可以看出,数据读//取是按每一个扇区来计算每一次要读//取的字节数的。
memmove(dst, bp->data + off%BSIZE, m); //bp->data + off%BSIZE表示在当
//前扇区中的起始读位置,通过memmove将数据复制到目的地址。
brelse(bp); // 释放块缓存
}
return n;
}
// PAGEBREAK!
// Write data to inode.
// Caller must hold ip->lock.
//将数据写入索引节点。
// 调用者必须持有ip->lock。
// writei 用于将指定的偏移位置和大小的数据写到inode中
int
writei(struct inode *ip, char *src, uint off, uint n)
{
uint tot, m;
struct buf *bp;
if(ip->type == T_DEV){ //判断是否是设备文件
if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write)
return -1;
return devsw[ip->major].write(ip, src, n); // 调用设备驱动程序中的写函数
}
// 磁盘文件的读操作
if(off > ip->size || off + n < off) // 检查写入的起点是否越界
return -1;
if(off + n > MAXFILE*BSIZE) // 检查写入的数据总量不会超过文件的最大值
return -1;
for(tot=0; tot<n; tot+=m, off+=m, src+=m){ //对每个扇区(盘块)进行操作
bp = bread(ip->dev, bmap(ip, off/BSIZE)); // 读入块缓存 off/BSIZE表示将偏//移起始地址转//换为Inode的数据
//块号。通过bmap得到对应的扇区
//号,然后读取buffer cache。
m = min(n - tot, BSIZE - off%BSIZE); // 调整偏移量的增量。n-tot表示还剩多//少字节没有写,off%BSIZE表示off
//偏移在数据块中的偏移,BSIZE - //off%BSIZE表示在一个数据块中剩余//的字节数。(n - tot), (BSIZE - //off%BSIZE)中的最小者表示当前需
//要写的字节数。
memmove(bp->data + off%BSIZE, src, m); //bp->data + off%BSIZE表示在当
//前扇区中的起始写位置,通过memmove将数据写入到块缓存。
log_write(bp); //每次写完一个扇区后,都调用log_write写回磁盘
brelse(bp); // 释放块缓存
}
if(n > 0 && off > ip->size){ //判断文件现在的大小是否超过了原来的大小
ip->size = off; // 超过,则更新文件的大熊
iupdate(ip); // 更新磁盘dinode
}
return n;
}
//PAGEBREAK!
// Directories
// 目录操作
// namecmp函数,用于比较两个字符串是否一致。
int
namecmp(const char *s, const char *t)
{
return strncmp(s, t, DIRSIZ); //调用字符串比较strncmp函数,
//比较字符串是否一致
}
// Look for a directory entry in a directory.
// If found, set *poff to byte offset of entry.
// dirlookup()函数,在一个目录文件中,根据指定的文件名查找目录项,并返//回此条目的inode
struct inode*
dirlookup(struct inode *dp, char *name, uint *poff) //目录查找函数
{
uint off, inum;
struct dirent de;
if(dp->type != T_DIR) //inode必须为目录项
panic("dirlookup not DIR");
for(off = 0; off < dp->size; off += sizeof(de)){ //循环,逐个目录项处理
if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) //读入当前目录项,文件// 偏移量为sizeof(de)
panic("dirlookup read");
if(de.inum == 0) //无效目录
continue;
if(namecmp(name, de.name) == 0){ //找到名字匹配的目录项
// entry matches path element
if(poff)
*poff = off; //将目录项偏移值通过*poff返回
inum = de.inum; //确定索引节点号
return iget(dp->dev, inum); //建立inode缓存
}
}
return 0;
}
// Write a new directory entry (name, inum) into the directory dp.
// dirlink()用于在某个目录中创建一个目录项,对应于系统调用sys_link()
int
dirlink(struct inode *dp, char *name, uint inum) //将一个新的目录条目写到目录//inode中,参数为文件名和对应的
//文件inode
{
int off;
struct dirent de;
struct inode *ip;
// Check that name is not present.
if((ip = dirlookup(dp, name, 0)) != 0){ //检查目录中是否已有此条目
iput(ip); // 有调用iput检查inode的引用计数
return -1;
}
// Look for an empty dirent.
for(off = 0; off < dp->size; off += sizeof(de)){ //找一个空闲目录
if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) //判断是否是空闲目录
panic("dirlink read");
if(de.inum == 0) //找到空闲目录
break;
}
strncpy(de.name, name, DIRSIZ); // 目录项中写入文件名字字符串
de.inum = inum; //目录项中记录对应的inode
if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) // 写到磁盘
panic("dirlink");
return 0;
}
//PAGEBREAK!
// Paths
// 路径名解析
// Copy the next path element from path into name.
// Return a pointer to the element following the copied one.
// The returned path has no leading slashes,
// so the caller can check *path=='\0' to see if the name is the last one.
// If no name to remove, return 0.
//
// Examples:
// skipelem("a/bb/c", name) = "bb/c", setting name = "a"
// skipelem("///a//bb", name) = "bb", setting name = "a"
// skipelem("a", name) = "", setting name = "a"
// skipelem("", name) = skipelem("", name) = 0
//
static char*
skipelem(char *path, char *name) //用于提及一级路径名,path为文件的路径名,
// 用于保护解析出的第一个元素的名字
{
char *s;
int len;
while(*path == '/') //跳过所有的’/’字符
path++;
if(*path == 0) //如果path第一个非’/’符为0,则返回
return 0;
s = path; //第一个既不是’/’也不是’0’字符的位置
while(*path != '/' && *path != 0) // 选择下一个为‘/’或’0‘的字符的位置,保//存到path中。
path++;
len = path - s; //计算出第一个元素的长度
if(len >= DIRSIZ) //判断元素长度是否大于DIRSIZ
memmove(name, s, DIRSIZ); //将s代表的字符复制到name中,表示解析出
//的第一元素,元素长度为DIRSIZ和len的小的一个
else {
memmove(name, s, len);
name[len] = 0;
}
while(*path == '/') // 跳过path中所有的’/’字符
path++;
return path; //返回去除第一个元素后的路径名。
}
// Look up and return the inode for a path name.
// If parent != 0, return the inode for the parent and copy the final
// path element into name, which must have room for DIRSIZ bytes.
// Must be called inside a transaction since it calls iput().
//namex就是解析路径名的核心函数,此函数会被namei和nameiparent调用。//nameiparent参数用于标明最后返回的是路径名的inode还是路径名上一层元素//的Inode。
static struct inode*
namex(char *path, int nameiparent, char *name)
//根据文件的路径名,返回文件的inode
{
struct inode *ip, *next;
if(*path == '/') //首先检查路径名是全路径名还是相对路径名
ip = iget(ROOTDEV, ROOTINO);
//如果是全路径名则调用iget获取根目录的inode
else
ip = idup(myproc()->cwd);
//如果是相对路径则通过调用idup获取当前工作目录的inode
//通过while循环调用skipelem循环解析路径名
while((path = skipelem(path, name)) != 0){
ilock(ip); //调用ilock锁住Inode
if(ip->type != T_DIR){ //接着检查inode的类型
iunlockput(ip); // 解锁
return 0;//如果不是目录类型直接返回
}
if(nameiparent && *path == '\0'){
//检查调用者是否想返回的是路径名上一层的inode
// Stop one level early.
iunlock(ip); //解锁
return ip; //返回路径名
}
if((next = dirlookup(ip, name, 0)) == 0){
//根据解析的名字调用dirlookup来在目录中寻找其对应条目并返回inode
iunlockput(ip); //解锁
return 0; //返回
}
iunlockput(ip); //解锁
ip = next; // ip表示的是上一层目录的inode,而next表示的是当前name
//对应的Inode,如果找到则返回,如果没有则继续下一轮解析查//找。
}
if(nameiparent){ //如果while循环结束且nameiparent为1
iput(ip); //表示返回上一级目录失败
return 0;
}
return ip; //最后返回路径名对应的Inode
}
struct inode*
namei(char *path) //根据路径名,查找并返回对应文件的inode
{
char name[DIRSIZ];
return namex(path, 0, name); //调用namex完成,调用时必须将namex()的第//二个参数设置为0,否则namex()将返回对应文件的父目录项。
}
struct inode*
nameiparent(char *path, char *name) //根据路径名,返回倒数第二级目录的inode,
// 即目标文件的父目录。
{
return namex(path, 1, name);
}