Linux Kernel linux/fs.h

  1 //
  2 // linux/fs.h
  3 // linux文件系统最外围结构之一,超级块
  4 //
  5 struct super_block {
  6     //
  7     // 双向链表,所有文件系统的超级快通过这个链接在一起
  8     //
  9     struct list_head    s_list;        /* Keep this first */
 10     //
 11     // 设备???目前不明
 12     // 
 13     dev_t            s_dev;        /* search index; _not_ kdev_t */
 14     unsigned char        s_blocksize_bits;
 15     unsigned long        s_blocksize;
 16     //
 17     //该文件系统最大文件大小
 18     //
 19     loff_t            s_maxbytes;    /* Max file size */
 20     //
 21     // 文件系统指针,在一个文件系统驱动注册的时候填写好这个
 22     // 结构体,通过 register_filesystem(); 注册进来!!!!!
 23     //
 24     struct file_system_type    *s_type;
 25     //
 26     // 最重要的结构之一,超级块的操作
 27     //
 28     const struct super_operations    *s_op;
 29     const struct dquot_operations    *dq_op;
 30     const struct quotactl_ops    *s_qcop;
 31     const struct export_operations *s_export_op;
 32     unsigned long        s_flags;
 33     unsigned long        s_iflags;    /* internal SB_I_* flags */
 34     //
 35     // 超级块的魔数
 36     //
 37     unsigned long        s_magic;
 38     //
 39     // 文件系统挂载的目录项
 40     //
 41     struct dentry        *s_root;
 42     struct rw_semaphore    s_umount;
 43     int            s_count;
 44     atomic_t        s_active;
 45 #ifdef CONFIG_SECURITY
 46     void                    *s_security;
 47 #endif
 48     const struct xattr_handler **s_xattr;
 49 #ifdef CONFIG_FS_ENCRYPTION
 50     const struct fscrypt_operations    *s_cop;
 51 #endif
 52     struct hlist_bl_head    s_roots;    /* alternate root dentries for NFS */
 53     struct list_head    s_mounts;    /* list of mounts; _not_ for fs use */
 54     struct block_device    *s_bdev;
 55     struct backing_dev_info *s_bdi;
 56     struct mtd_info        *s_mtd;
 57     struct hlist_node    s_instances;
 58     unsigned int        s_quota_types;    /* Bitmask of supported quota types */
 59     struct quota_info    s_dquot;    /* Diskquota specific options */
 60 
 61     struct sb_writers    s_writers;
 62 
 63     /*
 64      * Keep s_fs_info, s_time_gran, s_fsnotify_mask, and
 65      * s_fsnotify_marks together for cache efficiency. They are frequently
 66      * accessed and rarely modified.
 67      */
 68     void            *s_fs_info;    /* Filesystem private info */
 69 
 70     /* Granularity of c/m/atime in ns (cannot be worse than a second) */
 71     u32            s_time_gran;
 72 #ifdef CONFIG_FSNOTIFY
 73     __u32            s_fsnotify_mask;
 74     struct fsnotify_mark_connector __rcu    *s_fsnotify_marks;
 75 #endif
 76 
 77     char            s_id[32];    /* Informational name */
 78     uuid_t            s_uuid;        /* UUID */
 79 
 80     unsigned int        s_max_links;
 81     fmode_t            s_mode;
 82 
 83     /*
 84      * The next field is for VFS *only*. No filesystems have any business
 85      * even looking at it. You had been warned.
 86      */
 87     struct mutex s_vfs_rename_mutex;    /* Kludge */
 88 
 89     /*
 90      * Filesystem subtype.  If non-empty the filesystem type field
 91      * in /proc/mounts will be "type.subtype"
 92      */
 93     const char *s_subtype;
 94     //
 95     // 目录项的操作方法
 96     //
 97     const struct dentry_operations *s_d_op; /* default d_op for dentries */
 98 
 99     /*
100      * Saved pool identifier for cleancache (-1 means none)
101      */
102     int cleancache_poolid;
103 
104     struct shrinker s_shrink;    /* per-sb shrinker handle */
105 
106     /* Number of inodes with nlink == 0 but still referenced */
107     atomic_long_t s_remove_count;
108 
109     /* Pending fsnotify inode refs */
110     atomic_long_t s_fsnotify_inode_refs;
111 
112     /* Being remounted read-only */
113     int s_readonly_remount;
114 
115     /* AIO completions deferred from interrupt context */
116     struct workqueue_struct *s_dio_done_wq;
117     struct hlist_head s_pins;
118 
119     /*
120      * Owning user namespace and default context in which to
121      * interpret filesystem uids, gids, quotas, device nodes,
122      * xattrs and security labels.
123      */
124     struct user_namespace *s_user_ns;
125 
126     /*
127      * The list_lru structure is essentially just a pointer to a table
128      * of per-node lru lists, each of which has its own spinlock.
129      * There is no need to put them into separate cachelines.
130      */
131     //
132     // 应该是未使用的dentry和inode链表
133     //
134     struct list_lru        s_dentry_lru;
135     struct list_lru        s_inode_lru;
136     struct rcu_head        rcu;
137     struct work_struct    destroy_work;
138 
139     struct mutex        s_sync_lock;    /* sync serialisation lock */
140 
141     /*
142      * Indicates how deep in a filesystem stack this SB is
143      */
144     int s_stack_depth;
145 
146     /* s_inode_list_lock protects s_inodes */
147     spinlock_t        s_inode_list_lock ____cacheline_aligned_in_smp;
148     struct list_head    s_inodes;    /* all inodes */
149 
150     spinlock_t        s_inode_wblist_lock;
151     struct list_head    s_inodes_wb;    /* writeback inodes */
152 } __randomize_layout;
153 
154 
155 //
156 // 对应 super_block->s_op
157 //
158 struct super_operations {
159     //
160     // 初始化新的索引节点, inode
161     //
162        struct inode *(*alloc_inode)(struct super_block *sb);
163     void (*destroy_inode)(struct inode *);
164     void (*free_inode)(struct inode *);
165 
166        void (*dirty_inode) (struct inode *, int flags);
167     int (*write_inode) (struct inode *, struct writeback_control *wbc);
168     int (*drop_inode) (struct inode *);
169     void (*evict_inode) (struct inode *);
170     void (*put_super) (struct super_block *);
171     int (*sync_fs)(struct super_block *sb, int wait);
172     int (*freeze_super) (struct super_block *);
173     int (*freeze_fs) (struct super_block *);
174     int (*thaw_super) (struct super_block *);
175     int (*unfreeze_fs) (struct super_block *);
176     int (*statfs) (struct dentry *, struct kstatfs *);
177     int (*remount_fs) (struct super_block *, int *, char *);
178     void (*umount_begin) (struct super_block *);
179 
180     int (*show_options)(struct seq_file *, struct dentry *);
181     int (*show_devname)(struct seq_file *, struct dentry *);
182     int (*show_path)(struct seq_file *, struct dentry *);
183     int (*show_stats)(struct seq_file *, struct dentry *);
184 #ifdef CONFIG_QUOTA
185     ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
186     ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
187     struct dquot **(*get_dquots)(struct inode *);
188 #endif
189     int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t);
190     long (*nr_cached_objects)(struct super_block *,
191                   struct shrink_control *);
192     long (*free_cached_objects)(struct super_block *,
193                     struct shrink_control *);
194 };
195 
196 
197 /*
198  * Keep mostly read-only and often accessed (especially for
199  * the RCU path lookup and 'stat' data) fields at the beginning
200  * of the 'struct inode'
201  */
202  
203 //
204 // 文件系统inode节点
205 //
206 
207 struct inode {
208     umode_t            i_mode;
209     unsigned short        i_opflags;
210     kuid_t            i_uid;
211     kgid_t            i_gid;
212     unsigned int        i_flags;
213 
214     //
215     // 当内核配置了POSIX ACL后开启,一般开启
216     //
217 #ifdef CONFIG_FS_POSIX_ACL
218     struct posix_acl    *i_acl;
219     struct posix_acl    *i_default_acl;
220 #endif
221     //
222     // inode操作
223     //
224     const struct inode_operations    *i_op;
225     //
226     // 指向超级块
227     //
228     struct super_block    *i_sb;
229     //
230     // 指向address_space
231     //
232     struct address_space    *i_mapping;
233 
234 #ifdef CONFIG_SECURITY
235     void            *i_security;
236 #endif
237     //
238     // inode number
239     //
240     /* Stat data, not accessed from path walking */
241     unsigned long        i_ino;
242     /*
243      * Filesystems may only read i_nlink directly.  They shall use the
244      * following functions for modification:
245      *
246      *    (set|clear|inc|drop)_nlink
247      *    inode_(inc|dec)_link_count
248      */
249     union {
250         const unsigned int i_nlink;
251         unsigned int __i_nlink;
252     };
253     dev_t            i_rdev;
254     //
255     // 文件大小,多少个字节
256     //
257     loff_t            i_size;
258     // i_atime access time
259     // i_mtime modify time
260     // i_ctime create time
261     struct timespec64    i_atime;
262     struct timespec64    i_mtime;
263     struct timespec64    i_ctime;
264     spinlock_t        i_lock;    /* i_blocks, i_bytes, maybe i_size */
265     unsigned short          i_bytes;
266     u8            i_blkbits;
267     u8            i_write_hint;
268     blkcnt_t        i_blocks;
269 
270 #ifdef __NEED_I_SIZE_ORDERED
271     seqcount_t        i_size_seqcount;
272 #endif
273 
274     /* Misc */
275     unsigned long        i_state;
276     struct rw_semaphore    i_rwsem;
277 
278     unsigned long        dirtied_when;    /* jiffies of first dirtying */
279     unsigned long        dirtied_time_when;
280 
281     struct hlist_node    i_hash;
282     struct list_head    i_io_list;    /* backing dev IO list */
283 #ifdef CONFIG_CGROUP_WRITEBACK
284     struct bdi_writeback    *i_wb;        /* the associated cgroup wb */
285 
286     /* foreign inode detection, see wbc_detach_inode() */
287     int            i_wb_frn_winner;
288     u16            i_wb_frn_avg_time;
289     u16            i_wb_frn_history;
290 #endif
291     struct list_head    i_lru;        /* inode LRU list */
292     struct list_head    i_sb_list;
293     struct list_head    i_wb_list;    /* backing dev writeback list */
294     union {
295         struct hlist_head    i_dentry;
296         struct rcu_head        i_rcu;
297     };
298     atomic64_t        i_version;
299     atomic_t        i_count;
300     atomic_t        i_dio_count;
301     atomic_t        i_writecount;
302 #if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
303     atomic_t        i_readcount; /* struct files open RO */
304 #endif
305     union {
306         const struct file_operations    *i_fop;    /* former ->i_op->default_file_ops */
307         void (*free_inode)(struct inode *);
308     };
309     struct file_lock_context    *i_flctx;
310     struct address_space    i_data;
311     struct list_head    i_devices;
312     union {
313         struct pipe_inode_info    *i_pipe;
314         struct block_device    *i_bdev;
315         struct cdev        *i_cdev;
316         char            *i_link;
317         unsigned        i_dir_seq;
318     };
319 
320     __u32            i_generation;
321 
322 #ifdef CONFIG_FSNOTIFY
323     __u32            i_fsnotify_mask; /* all events this inode cares about */
324     struct fsnotify_mark_connector __rcu    *i_fsnotify_marks;
325 #endif
326 
327 #ifdef CONFIG_FS_ENCRYPTION
328     struct fscrypt_info    *i_crypt_info;
329 #endif
330     //
331     // 私有结构
332     //
333     void            *i_private; /* fs or device private pointer */
334 } __randomize_layout;
335 
336 //
337 // inode 节点操作方法
338 //
339 struct inode_operations {
340     //查找
341     struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
342     const char * (*get_link) (struct dentry *, struct inode *, struct delayed_call *);
343     int (*permission) (struct inode *, int);
344     struct posix_acl * (*get_acl)(struct inode *, int);
345 
346     int (*readlink) (struct dentry *, char __user *,int);
347     //
348     // 创建一个新的索引节点
349     //
350     int (*create) (struct inode *,struct dentry *, umode_t, bool);
351     //
352     // 创建硬链接
353     //
354     int (*link) (struct dentry *,struct inode *,struct dentry *);
355     //
356     // 取消硬链接
357     //
358     int (*unlink) (struct inode *,struct dentry *);
359     //
360     // 创建符号链接
361     //
362     int (*symlink) (struct inode *,struct dentry *,const char *);
363     // 
364     // 创建目录
365     //
366     int (*mkdir) (struct inode *,struct dentry *,umode_t);
367     //
368     // 删除目录
369     //
370     int (*rmdir) (struct inode *,struct dentry *);
371     //
372     // 创建块设备节点
373     //
374     int (*mknod) (struct inode *,struct dentry *,umode_t,dev_t);
375     //
376     // 重命名
377     //
378     int (*rename) (struct inode *, struct dentry *,
379             struct inode *, struct dentry *, unsigned int);
380     //
381     // 设置属性
382     //
383     int (*setattr) (struct dentry *, struct iattr *);
384     //
385     // 得到属性
386     //
387     int (*getattr) (const struct path *, struct kstat *, u32, unsigned int);
388     ssize_t (*listxattr) (struct dentry *, char *, size_t);
389     //
390     // 从函数名来看是文件映射
391     //
392     int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
393               u64 len);
394     int (*update_time)(struct inode *, struct timespec64 *, int);
395     int (*atomic_open)(struct inode *, struct dentry *,
396                struct file *, unsigned open_flag,
397                umode_t create_mode);
398     int (*tmpfile) (struct inode *, struct dentry *, umode_t);
399     int (*set_acl)(struct inode *, struct posix_acl *, int);
400 } ____cacheline_aligned;
401 
402 //
403 //    dentry --> directory entry 目录项
404 //  一般结构是: file ==> dentry ==> inode
405 //  对于进程来说: task_struct 有一个结构体叫
406 //    task_struct -> files_struct -> fdtable->file
407 //
408 //
409 //
410 //
411 
412 
413 //
414 // dentry 定义在dcache.h
415 //
416 struct dentry {
417     /* RCU lookup touched fields */
418     unsigned int d_flags;        /* protected by d_lock */
419     seqcount_t d_seq;        /* per dentry seqlock */
420     //
421     // 查找hash表
422     //
423     struct hlist_bl_node d_hash;    /* lookup hash list */
424     //
425     // 父目录项
426     //
427     struct dentry *d_parent;    /* parent directory */
428     struct qstr d_name;
429     //
430     // dentry 对应inode节点
431     //
432     struct inode *d_inode;        /* Where the name belongs to - NULL is
433                      * negative */
434     unsigned char d_iname[DNAME_INLINE_LEN];    /* small names */
435 
436     /* Ref lookup also touches following */
437     struct lockref d_lockref;    /* per-dentry lock and refcount */
438     const struct dentry_operations *d_op;
439     struct super_block *d_sb;    /* The root of the dentry tree */
440     unsigned long d_time;        /* used by d_revalidate */
441     void *d_fsdata;            /* fs-specific data */
442 
443     union {
444         struct list_head d_lru;        /* LRU list */
445         wait_queue_head_t *d_wait;    /* in-lookup ones only */
446     };
447     //
448     // 父目录目录项链表
449     //
450     struct list_head d_child;    /* child of parent list */
451     //
452     // 自己子目录链表
453     //
454     struct list_head d_subdirs;    /* our children */
455     /*
456      * d_alias and d_rcu can share memory
457      */
458     union {
459         struct hlist_node d_alias;    /* inode alias list */
460         struct hlist_bl_node d_in_lookup_hash;    /* only for in-lookup ones */
461          struct rcu_head d_rcu;
462     } d_u;
463 } __randomize_layout;
464 
465 
466 struct dentry_operations {
467     // 
468     // 判断目录项是否实效
469     //
470     int (*d_revalidate)(struct dentry *, unsigned int);
471     //
472     // 
473     //
474     int (*d_weak_revalidate)(struct dentry *, unsigned int);
475     //
476     // 生成hash,VFS插入目录项时会计算hash
477     //
478     int (*d_hash)(const struct dentry *, struct qstr *);
479     //
480     // 比较字符串
481     //
482     int (*d_compare)(const struct dentry *,
483             unsigned int, const char *, const struct qstr *);
484     //
485     // 当d_count 为零是调用
486     // 
487     int (*d_delete)(const struct dentry *);
488     int (*d_init)(struct dentry *);
489     //
490     // 文件被释放时被调用
491     //
492     void (*d_release)(struct dentry *);
493     void (*d_prune)(struct dentry *);
494     //
495     // 当目录项丢失inode节点时候调用
496     //
497     void (*d_iput)(struct dentry *, struct inode *);
498     char *(*d_dname)(struct dentry *, char *, int);
499     struct vfsmount *(*d_automount)(struct path *);
500     int (*d_manage)(const struct path *, bool);
501     struct dentry *(*d_real)(struct dentry *, const struct inode *);
502 } ____cacheline_aligned;
503 
504 //
505 // path
506 //
507 struct path {
508     struct vfsmount *mnt;
509     struct dentry *dentry;
510 } __randomize_layout;
511 
512 struct vfsmount {
513     struct dentry *mnt_root;    /* root of the mounted tree */
514     struct super_block *mnt_sb;    /* pointer to superblock */
515     int mnt_flags;
516 } __randomize_layout;
517 
518 //
519 // struct file, 文件对象
520 //
521 struct file {
522     union {
523         //
524         // 文件对象链表
525         //
526         struct llist_node    fu_llist;
527         struct rcu_head     fu_rcuhead;
528     } f_u;
529     //
530     // path struct path {
531     //    struct vfsmount *mnt;    // vfs挂载点
532     //    struct dentry *dentry;    // 对应目录项
533     //} __randomize_layout;
534 
535     //
536     struct path        f_path;
537     struct inode        *f_inode;    /* cached value */
538     //
539     // 文件操作函数
540     //
541     const struct file_operations    *f_op;
542 
543     /*
544      * Protects f_ep_links, f_flags.
545      * Must not be taken from IRQ context.
546      */
547     spinlock_t        f_lock;
548     enum rw_hint        f_write_hint;
549     // 
550     // 文件被多少进程使用
551     //
552     atomic_long_t        f_count;
553     unsigned int         f_flags;
554     fmode_t            f_mode;
555     struct mutex        f_pos_lock;
556     //
557     // 文件指针位置
558     //
559     loff_t            f_pos;
560     struct fown_struct    f_owner;
561     const struct cred    *f_cred;
562     struct file_ra_state    f_ra;
563 
564     u64            f_version;
565 #ifdef CONFIG_SECURITY
566     void            *f_security;
567 #endif
568     /* needed for tty driver, and maybe others */
569     void            *private_data;
570 
571 #ifdef CONFIG_EPOLL
572     /* Used by fs/eventpoll.c to link all the hooks to this file */
573     struct list_head    f_ep_links;
574     struct list_head    f_tfile_llink;
575 #endif /* #ifdef CONFIG_EPOLL */
576     //
577     // address_space 
578     //
579     struct address_space    *f_mapping;
580     errseq_t        f_wb_err;
581 } __randomize_layout
582   __attribute__((aligned(4)));    /* lest something weird decides that 2 is OK */
583 
584 struct file_handle {
585     __u32 handle_bytes;
586     int handle_type;
587     /* file identifier */
588     unsigned char f_handle[0];
589 };
590 
591 //
592 // 
593 //
594 struct file_operations {
595     struct module *owner;
596     loff_t (*llseek) (struct file *, loff_t, int);
597     ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
598     ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
599     ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
600     ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
601     int (*iopoll)(struct kiocb *kiocb, bool spin);
602     int (*iterate) (struct file *, struct dir_context *);
603     int (*iterate_shared) (struct file *, struct dir_context *);
604     __poll_t (*poll) (struct file *, struct poll_table_struct *);
605     long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
606     long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
607     int (*mmap) (struct file *, struct vm_area_struct *);
608     unsigned long mmap_supported_flags;
609     int (*open) (struct inode *, struct file *);
610     int (*flush) (struct file *, fl_owner_t id);
611     int (*release) (struct inode *, struct file *);
612     int (*fsync) (struct file *, loff_t, loff_t, int datasync);
613     int (*fasync) (int, struct file *, int);
614     int (*lock) (struct file *, int, struct file_lock *);
615     ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
616     unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
617     int (*check_flags)(int);
618     int (*flock) (struct file *, int, struct file_lock *);
619     ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
620     ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
621     int (*setlease)(struct file *, long, struct file_lock **, void **);
622     long (*fallocate)(struct file *file, int mode, loff_t offset,
623               loff_t len);
624     void (*show_fdinfo)(struct seq_file *m, struct file *f);
625 #ifndef CONFIG_MMU
626     unsigned (*mmap_capabilities)(struct file *);
627 #endif
628     ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
629             loff_t, size_t, unsigned int);
630     loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,
631                    struct file *file_out, loff_t pos_out,
632                    loff_t len, unsigned int remap_flags);
633     int (*fadvise)(struct file *, loff_t, loff_t, int);
634 } __randomize_layout;
635 
636 //
637 // 文件系统类型
638 //
639 struct file_system_type {
640     const char *name;
641     int fs_flags;
642 #define FS_REQUIRES_DEV        1 
643 #define FS_BINARY_MOUNTDATA    2
644 #define FS_HAS_SUBTYPE        4
645 #define FS_USERNS_MOUNT        8    /* Can be mounted by userns root */
646 #define FS_DISALLOW_NOTIFY_PERM    16    /* Disable fanotify permission events */
647 #define FS_RENAME_DOES_D_MOVE    32768    /* FS will handle d_move() during rename() internally. */
648     int (*init_fs_context)(struct fs_context *);
649     const struct fs_parameter_description *parameters;
650     struct dentry *(*mount) (struct file_system_type *, int,
651                const char *, void *);
652     void (*kill_sb) (struct super_block *);
653     struct module *owner;
654     struct file_system_type * next;
655     struct hlist_head fs_supers;
656 
657     struct lock_class_key s_lock_key;
658     struct lock_class_key s_umount_key;
659     struct lock_class_key s_vfs_rename_key;
660     struct lock_class_key s_writers_key[SB_FREEZE_LEVELS];
661 
662     struct lock_class_key i_lock_key;
663     struct lock_class_key i_mutex_key;
664     struct lock_class_key i_mutex_dir_key;
665 };
666 //
667 // 挂载点
668 //
669 struct vfsmount {
670     struct dentry *mnt_root;    /* root of the mounted tree */
671     struct super_block *mnt_sb;    /* pointer to superblock */
672     int mnt_flags;
673 } __randomize_layout;

 

posted @ 2020-09-19 15:06  maojun1998  阅读(703)  评论(0编辑  收藏  举报