1. 程式人生 > >linux檔案系統(二)

linux檔案系統(二)

虛擬檔案系統這一層提供的大多數是形如vfs_xxx的函式,他們最終呼叫的還是各個具體的檔案系統註冊給虛擬檔案系統的struct inode_operations和struct file_operations等結構體中的一些操作函式。以fat檔案系統為例,他註冊的struct inode_operations和struct file_operations如下:

//fs/fat/namei_vfat.c
static const struct inode_operations vfat_dir_inode_operations = {
        .create         = vfat_create,
        .lookup
= vfat_lookup, .unlink = vfat_unlink, .mkdir = vfat_mkdir, .rmdir = vfat_rmdir, .rename = vfat_rename, .setattr = fat_setattr, .getattr = fat_getattr, }; //fs/fat/file.c const struct file_operations fat_file_operations = { .llseek
= generic_file_llseek, .read = do_sync_read, .write = do_sync_write, .aio_read = generic_file_aio_read, .aio_write = generic_file_aio_write, .mmap = generic_file_mmap, .release = fat_file_release, .unlocked
_ioctl = fat_generic_ioctl, .fsync = fat_file_fsync, .splice_read = generic_file_splice_read, };

open系統呼叫

//fs/open.c
SYSCALL_DEFINE3(open, const char __user *, filename, ...)
    do_sys_open
        do_filp_open //fs/namei.c
            path_openat
                do_last
                    lookup_open
                        vfs_create
                        finish_open
                            do_dentry_open
                                f->f_op = fops_get(inode->i_fop);

在open的時候會將inode中結構體struct file_operations i_fop儲存的函式指標賦值給file中的結構體struct file_operations f_op,以後read,write等函式呼叫的都是這裡的struct file_operations結構體中的回撥函式。

write系統呼叫

//fs/read_write.c
SYSCALL_DEFINE3(write, ...
    vfs_write
        file->f_op->write (fat檔案系統為do_sync_write)
        do_sync_write
            filp->f_op->aio_write (fat檔案系統為generic_file_aio_write)
            generic_file_aio_write //mm/filemap.c
                __generic_file_aio_write
                    generic_file_buffered_write
                        generic_perform_write
                            a_ops->write_begin
                            iov_iter_copy_from_user_atomic
                            a_ops->write_end

首先,系統呼叫write會呼叫虛擬檔案系統中的函式vfs_write,vfs_write最終會呼叫fat檔案系統註冊的struct file_operations函式中的f_op->aio_write函式,generic_file_aio_write是在mm/filemap.c檔案中實現的函式,這個函式會將資料寫到page cache中,等待回寫執行緒來將page cache寫到磁碟中。

mkdir系統呼叫

//fs/namei.c
SYSCALL_DEFINE2(mkdir, ...
    sys_mkdirat
SYSCALL_DEFINE3(mkdirat, ...
    vfs_mkdir(struct inode *dir, struct dentry *dentry, ...)
        dir->i_op->mkdir

mkdir系統呼叫與write系統呼叫不一樣,他會呼叫inode中的struct inode_operations結構體中的函式,而write系統呼叫呼叫的是file中的struct file_operations結構體中的函式。