当前位置: 首页 > 工具软件 > simplefs > 使用案例 >

SimpleFs文件系统初步五(文件的读写)

微生慈
2023-12-01

1.写数据的过程:

ssize_t simplefs_write(struct file * filp, const char __user * buf, size_t len,
		       loff_t * ppos)
{
	/* After the commit dd37978c5 in the upstream linux kernel,
	 * we can use just filp->f_inode instead of the
	 * f->f_path.dentry->d_inode redirection */
	struct inode *inode;
	struct simplefs_inode *sfs_inode;
	struct buffer_head *bh;
	struct super_block *sb;

	char *buffer;

	int retval;

#if 0
	retval = generic_write_checks(filp, ppos, &len, 0);
	if (retval) {
		return retval;
	}
#endif
	//通过文件得到内核对应的Inode指针
	inode = filp->f_path.dentry->d_inode;
	//通过内核的Inode指针进而得到特定文件系统的Inode指针
	sfs_inode = SIMPLEFS_INODE(inode);
	//通过Inode得到SuperBlock
	sb = inode->i_sb;
	//获取该Inode指向的数据块
	bh = sb_bread(filp->f_path.dentry->d_inode->i_sb,
					    sfs_inode->data_block_number);

	if (!bh) {
		printk(KERN_ERR "Reading the block number [%llu] failed.",
		       sfs_inode->data_block_number);
		return 0;
	}
	
	//将数据区强制转换为char*型
	buffer = (char *)bh->b_data;

	/* Move the pointer until the required byte offset */
	//移动到vfs指定的偏移位置
	buffer += *ppos;

	//拷贝用户空间的数据到对应的数据块中
	if (copy_from_user(buffer, buf, len)) {
		brelse(bh);
		printk(KERN_ERR
		       "Error copying file contents from the userspace buffer to the kernel space\n");
		return -EFAULT;
	}
	//通知VFS指针偏移了多少
	*ppos += len;
	//将数据区设置为Dirty,并回写到磁盘
	mark_buffer_dirty(bh);
	sync_dirty_buffer(bh);
	//最后释放数据块的指针
	brelse(bh);

	/* Set new size
	 * sfs_inode->file_size = max(sfs_inode->file_size, *ppos);
	 *
	 * FIXME: What to do if someone writes only some parts in between ?
	 * The above code will also fail in case a file is overwritten with
	 * a shorter buffer */
	if (mutex_lock_interruptible(&simplefs_inodes_mgmt_lock)) {
		sfs_trace("Failed to acquire mutex lock\n");
		return -EINTR;
	}
	//更新Inode的文件大小
	sfs_inode->file_size = *ppos;
	//既然更新了Inode的信息,那么Inode的信息区也要同步更新下
	retval = simplefs_inode_save(sb, sfs_inode);
	if (retval) {
		len = retval;
	}
	mutex_unlock(&simplefs_inodes_mgmt_lock);

	return len;
}

2.读数据的过程:

ssize_t simplefs_read(struct file * filp, char __user * buf, size_t len,
		      loff_t * ppos)
{
	/* After the commit dd37978c5 in the upstream linux kernel,
	 * we can use just filp->f_inode instead of the
	 * f->f_path.dentry->d_inode redirection */
	struct simplefs_inode *inode =
	    SIMPLEFS_INODE(filp->f_path.dentry->d_inode);
	struct buffer_head *bh;

	char *buffer;
	int nbytes;

	//如果要读数据的偏移超出了该Inode的大小,那么直接返回读取长度为0
	if (*ppos >= inode->file_size) {
		/* Read request with offset beyond the filesize */
		return 0;
	}

	//得到该Inode的数据区,将其读取出来
	bh = sb_bread(filp->f_path.dentry->d_inode->i_sb,
					    inode->data_block_number);

	if (!bh) {
		printk(KERN_ERR "Reading the block number [%llu] failed.",
		       inode->data_block_number);
		return 0;
	}
	//将数据区强制转换为Char*
	buffer = (char *)bh->b_data;
	//既然是读,就要考虑到有可能你读取的长度会超过该Inode的大小,因此需要取俩者中的最大值
	nbytes = min((size_t) inode->file_size, len);
	//该Inode读取到的数据传递给用户层
	if (copy_to_user(buf, buffer, nbytes)) {
		brelse(bh);
		printk(KERN_ERR
		       "Error copying file contents to the userspace buffer\n");
		return -EFAULT;
	}
	//由于读取操作不会改变磁盘的内容因此不需要同步操作,直接释放数据块的指针
	brelse(bh);
	//改变游标的指针
	*ppos += nbytes;
	//返回读取的长度
	return nbytes;
}
 类似资料: