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

linux檔案系統(一)

檔案系統架構

結構體

虛擬檔案系統提供的struct super_operations, struct inode_operations, struct file_operations由具體的檔案系統實現結構體中對應的介面。

struct super_block

struct super_operations

struct super_operations {
        struct inode *(*alloc_inode)(struct super_block *sb);
        void (*destroy_inode)(struct inode *);
        void
(*dirty_inode) (struct inode *, int flags); int (*write_inode) (struct inode *, struct writeback_control *wbc); int (*drop_inode) (struct inode *); void (*evict_inode) (struct inode *); void (*put_super) (struct super_block *); int (*sync_fs)(struct super_block *sb, int
wait); int (*freeze_fs) (struct super_block *); int (*unfreeze_fs) (struct super_block *); int (*statfs) (struct dentry *, struct kstatfs *); int (*remount_fs) (struct super_block *, int *, char *); void (*umount_begin) (struct super_block *); int (*show_options)(struct
seq_file *, struct dentry *); int (*show_devname)(struct seq_file *, struct dentry *); int (*show_path)(struct seq_file *, struct dentry *); int (*show_stats)(struct seq_file *, struct dentry *); int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t); int (*nr_cached_objects)(struct super_block *); void (*free_cached_objects)(struct super_block *, int); };

struct inode

struct inode_operations

struct inode_operations {
        struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
        void * (*follow_link) (struct dentry *, struct nameidata *);
        int (*permission) (struct inode *, int);
        struct posix_acl * (*get_acl)(struct inode *, int);

        int (*readlink) (struct dentry *, char __user *,int);
        void (*put_link) (struct dentry *, struct nameidata *, void *);

        int (*create) (struct inode *,struct dentry *, umode_t, bool);
        int (*link) (struct dentry *,struct inode *,struct dentry *);
        int (*unlink) (struct inode *,struct dentry *);
        int (*symlink) (struct inode *,struct dentry *,const char *);
        int (*mkdir) (struct inode *,struct dentry *,umode_t);
        int (*rmdir) (struct inode *,struct dentry *);
        int (*mknod) (struct inode *,struct dentry *,umode_t,dev_t);
        int (*rename) (struct inode *, struct dentry *,
                        struct inode *, struct dentry *);
        int (*setattr) (struct dentry *, struct iattr *);
        int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);
        int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);
        ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
        ssize_t (*listxattr) (struct dentry *, char *, size_t);
        int (*removexattr) (struct dentry *, const char *);
        int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
                      u64 len);
        int (*update_time)(struct inode *, struct timespec *, int);
        int (*atomic_open)(struct inode *, struct dentry *,
                           struct file *, unsigned open_flag,
                           umode_t create_mode, int *opened);
} ____cacheline_aligned;

struct file

struct file_operations

struct file_operations {
        struct module *owner;
        loff_t (*llseek) (struct file *, loff_t, int);
        ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
        ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
        ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
        ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
        int (*readdir) (struct file *, void *, filldir_t);
        unsigned int (*poll) (struct file *, struct poll_table_struct *);
        long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
        long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
        int (*mmap) (struct file *, struct vm_area_struct *);
        int (*open) (struct inode *, struct file *);
        int (*flush) (struct file *, fl_owner_t id);
        int (*release) (struct inode *, struct file *);
        int (*fsync) (struct file *, loff_t, loff_t, int datasync);
        int (*aio_fsync) (struct kiocb *, int datasync);
        int (*fasync) (int, struct file *, int);
        int (*lock) (struct file *, int, struct file_lock *);
        ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
        unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
        int (*check_flags)(int);
        int (*flock) (struct file *, int, struct file_lock *);
        ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
        ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
        int (*setlease)(struct file *, long, struct file_lock **);
        long (*fallocate)(struct file *file, int mode, loff_t offset,
                          loff_t len);
        int (*show_fdinfo)(struct seq_file *m, struct file *f);
};

註冊檔案系統

static struct file_system_type vfat_fs_type = {
        .owner          = THIS_MODULE,
        .name           = "vfat",
        .mount          = vfat_mount,
        .kill_sb        = kill_block_super,
        .fs_flags       = FS_REQUIRES_DEV,
}
static int __init init_vfat_fs(void) 
{
    return register_filesystem(&vfat_fs_type);
}
module_init(init_vfat_fs)

register_filesystem在fs/filesystem.c檔案中實現,主要是在全域性struct file_system_type *file_systems連結串列中查詢是否已存在檔案系統,如果不存在,那麼將當前的檔案系統新增到全域性file_systems連結串列中

註冊super_operations\inode_operations\file_operations

在struct file_system_type結構體中的mount對應的函式是vfat_mount

vfat_mount
    mount_bdev(fs_type, flags, dev_name, data, vfat_fill_super);
vfat_fill_super
    fat_fill_super(sb, data, silent, 1, setup);
        sb->s_op = &fat_sops;//struct super_operations,位於inode.c
        setup
            //struct inode_operations, 位於namei_vfat.c
            MSDOS_SB(sb)->dir_ops = &vfat_dir_inode_operations;

在fat_fill_super函式中會賦值struct super_operations,然後通過setup函式賦值struct inode_operations,

繼續看看struct inode_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,
};
vfat_mkdir
    fat_build_inode
        fat_fill_inode
            if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
                inode->i_op = sbi->dir_ops;
                inode->i_fop = &fat_dir_operations;
            } else { /* not a directory */
                inode->i_op = &fat_file_inode_operations;
                inode->i_fop = &fat_file_operations;
            }

在fat_fill_inode這個函式中會給inode中的struct inode_operations和struct file_operations結構體賦值

相關推薦

Linux 檔案系統()---虛擬檔案系統VFS----超級塊、inode、dentry、file

一: 什麼是檔案系統,詳見:http://zh.wikipedia.org/zh/%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F 其實一句話就是管理這塊檔案的機制(組織方式,資料結構之類...) Linux系統中存在很多的檔案系統,例如常見的ex

linux檔案系統()

檔案系統架構 結構體 虛擬檔案系統提供的struct super_operations, struct inode_operations, struct file_operations由具體的檔案系統實現結構體中對應的介面。 struct

Linux核心中的Proc檔案系統()

(1)/proc檔案系統的特點和/proc檔案的說明/proc檔案系統是一種特殊的、由軟體建立的檔案系統,核心使用它向外界匯出資訊,/proc系統只存在記憶體當中,而不佔用外存空間。/proc下面的每個

推薦款Windows下讀取 Linux檔案系統Ext4的最佳軟體 Paragon ExtFS

現在完全拋棄Windows還不現實,大家一般跑的是雙系統,所以常常就需要在Windows下訪問Linux的分割槽,一般為Ext4格式。 用過好幾款類似的軟體,都不太滿意,要麼只能讀不能寫,要麼寫操作經常有bug 只有 Paragon ExtFS 能完美的讀寫,並且使用很方便。 Paragon

Linux學習筆記Linux檔案系統

Linux檔案系統root      存放root使用者相關檔案home    存放普通使用者的相關檔案bin         存放普通命令檔案的目錄sbin        具有一定權才能使用的命令的目錄mnt        掛載軟碟機,光碟機的地方etc         存

linux核心構建最小的根檔案系統-步精簡

linux核心init程序函式的部分程式碼如下: 01 if (execute_command) 02 run_init_process(execute_command); 03 04 run_init_process("/sbin/init");

Linux檔案系統快照

Linux檔案系統快照 來自:http://www.mike.org.cn/blog/index.php?load=read&id=619 檔案系統快照 (File System Snapshots) 顧名思義就是在檔案系統上照張相片,也

Linux檔案系統層次標準

      前言 Linux檔案系統層次標準,英文全稱Filesystem Hierarchy Standard,英文簡稱FHS。 由於利用Linux來開發產品的團隊和個人實在太多了,如果每個人都以自己的想法來配置檔案放置的目錄,那麼將可能造成很多管理上的困擾。 在這

Linux 檔案系統的建立與掛載方法

轉自:https://blog.csdn.net/gz153016/article/details/51655994 Linux的  檔案系統的建立與掛載方法 1 Linux 檔案系統的建立 Linux的  作業系統在安裝伺服器時,安裝程式已經建立了自己的檔案系統,但是在使

Linux 檔案系統呼叫open七日遊(三)

接著上回,當對“.”和“..”處理完成後就直接返回進入下一個子路徑迴圈了,但如果當前子路徑不是“.”或“..”呢? 【fs/namei.c】 sys_open > do_sys_open > do_filp_open >&

linux檔案系統管理---分割槽掛載篇

轉:HTTP://www.cnblogs.com/alantu2018/p/8461680.html 一,系統在初始化時如何識別硬碟  1,系統初始時根據MBR的資訊來識別硬碟,其中包括了一些執行檔案就來載入系統,這些執行檔案就是MBR裡前面446bytes裡的啟動載入器程式,而後面

Linux檔案系統呼叫open 七日遊 (六)

還記得在上一個場景中,build_open_flags裡面有一個對標誌位O_PATH的判斷麼?現在我們就來看看這個標誌位是幹啥的: 【場景二】open(pathname,O_PATH)     這個O_PATH似乎是不常用的,咱們先看看它的使用

linux檔案系統呼叫 open 七日遊(四)

現在,我們的“路徑行走”只剩下最後一個小問題需要處理了——符號連結。 【fs/namei.c】 sys_open > do_sys_open > do_filp_open > path_openat &g

linux檔案管理(

linux檔案管理(一) 2018-10-20 在linux裡,最主要是文字檔案,今天主要介紹檔案的管理方式。一、目錄結構:windows:是多根的結構,比如以C:\ D:\linux:是以單根的方式組織檔案。/[[email 

Linux檔案系統只讀Read-only file system的解決方法

問題原因:系統沒有正常關機,導致虛擬磁碟出現檔案系統錯誤。 解決方法:使用fsck手動修復,具體操作如下: 重啟系統後使用root進入單使用者模式,執行 fsck.ext3 -y /dev/vda3 說明:ext3的檔案系統使用fsck.ext3,ext4檔案系統使用fsck.et

linux 檔案系統之superblock

為了實際測試這個pagecache和對裸盤操作的區別,我一不小心敲錯命令,將一個磁碟的super_block給抹掉了,全是0, dd if =/dev/zero of=/dev/sda2 bs=4096 count=1 seek=2234789 2234789是我的某個測試檔案的

嵌入式Linux檔案系統-jffs2,yaffs2,ubifs,ramfs,網路檔案系統

綜述: 常見的檔案系統有統的格式有:jffs2,yaffs2,ubifs等。但可基於儲存或者執行的位置可分為 flash型檔案系統,Ram型檔案系統,網路檔案系統 Flash型檔案系統: Flash因為其特殊的硬體結構,普通的檔案系統如ext2,ext3等不適合使用。常見的檔案系統的

Linux檔案系統許可權詳解

Linux許可權說明: 444 r--r--r-- 600 rw------- 644 rw-r--r-- 666 rw-rw-rw- 700 rwx------ 744 rwxr--r-- 755 rwxr-xr-x 777 rwxrwxrwx

Linux檔案系統管理 parted分割槽命令

概述 parted 命令是可以在命令列直接分割槽和格式化的,不過 parted 互動模式才是更加常用的命令方式。 parted命令 進入互動模式命令如下: [[email protected] ~]# parted 硬碟裝置檔名   例如: [[email&#

Linux檔案系統管理 swap分割槽及作用

概述 在安裝系統的時候已經建立了 swap 分割槽。swap 分割槽是 Linux 系統的交換分割槽,當記憶體不夠用的時候,我們使用 swap 分割槽存放記憶體中暫時不用的資料。也就是說,當記憶體不夠用時,我們使用 swap 分割槽來臨時頂替。在記憶體較小的情況下建議 swap 分割槽的