📄 00000046.htm
字号:
registered <BR>· fs_flags - one or more (ORed) of the flags: FS_REQUIRES_DEV for filesyste <BR>ms that can only be mounted on a block device, FS_SINGLE for filesystems tha <BR>t can have only one superblock, FS_NOMOUNT for filesystems that cannot be mo <BR>unted from userspace by means of mount(2) system call - they can however be <BR>mounted internally using kern_mount() interface, e.g. pipefs <BR>· read_super - a pointer to the function that reads the super block during <BR>mount operation. This function is required - if it is not provided, mount op <BR>eration (whether from userspace or inkernel) will always fail except in FS_S <BR>INGLE case where it will Oops in get_sb_single() trying to dereference a NUL <BR>L pointer in fs_type-kern_mnt-mnt_sb with (fs_type-kern_mnt = NULL) <BR>· owner - pointer to the module that implements this filesystem. If the fil <BR>esystem is statically linked into the kernel then this is NULL. You don't ne <BR>ed to set this manually as the macro THIS_MODULE does the right thing automa <BR>tically <BR>· kern_mnt - for FS_SINGLE filesystems only. This is set by kern_mount(). ( <BR>TODO: kern_mount() should refuse to mount filesystems if FS_SINGLE isnot set <BR>) <BR>· next - linkage into singly-linked list headed by file_systems (see fs/sup <BR>er.c). The list is protected by the file_systems_lock read-write spinlock an <BR>d functions register/unregister_filesystem() modify it by linking and unlink <BR>ing the entry from the list <BR>The job of read_super function is to fill in the fields of the superblock, a <BR>llocate root inode and initialise any fs-private information associated with <BR> this mounted instance of the filesystem. So, typically the read_super() wou <BR>ld do: <BR>1. Read the superblock from the device specified via sb-s_dev argument using <BR> buffer cache bread() function. If it anticipates to read a few more subsequ <BR>ent metadata blocks immediately then it makes sense to use breada() to sched <BR>ule reading extra blocks asynchronously <BR>2. Verify that superblock contains the valid magic number and overall "looks <BR>" sane <BR>3. Initialise sb-s_op to point to 'struct super_block_operations' structure. <BR> This structure contains filesystem-specific functions implementing operatio <BR>ns like "read inode", "delete inode" etc <BR>4. Allocate root inode and root dentry using d_alloc_root() <BR>5. If the filesystem is not mounted read-only then set sb-s_dirt = 1 and mar <BR>k the buffer containing superblock dirty (TODO: why do we do this? I did it <BR>in BFS because MINIX did it...) <BR>3.3 File Descriptor Management <BR>Under Linux there are several levels of indirection between user file descri <BR>ptor and the kernel inode structure. When a process makes open(2) system cal <BR>l, the kernel returns a small non-negative integer which can be used for sub <BR>sequent io operations on this file. This integer is an index into an array o <BR>f pointers to 'struct file'. Each file structure points to a dentry via file <BR>-f_dentry. And each dentry points to an inode via dentry-d_inode. <BR>Each task contains a field tsk-files which is a pointer to 'struct files_str <BR>uct' defined in include/linux/sched.h: <BR>/* <BR> * Open file table structure <BR> */ <BR>struct files_struct { <BR> atomic_t count; <BR> rwlock_t file_lock; <BR> int max_fds; <BR> int max_fdset; <BR> int next_fd; <BR> struct file ** fd; /* current fd array */ <BR> fd_set *close_on_exec; <BR> fd_set *open_fds; <BR> fd_set close_on_exec_init; <BR> fd_set open_fds_init; <BR> struct file * fd_array[NR_OPEN_DEFAULT]; <BR>}; <BR>The file-count is a reference count, incremented by get_file() (usually call <BR>ed by fget()) and decremented by fput() and by put_filp(). The difference be <BR>tween fput() and put_filp() is that fput() does more work usually needed for <BR> regular files, such as releasing flock locks, releasing dentry etc while pu <BR>t_filp() is only manipulating file table structures, i.e. decrements the cou <BR>nt, removes the file from the anon_list and adds it to the free_list, under <BR>protection of files_lock spinlock. <BR>The tsk-files can be shared between parent and child if the child thread was <BR> created using clone() system call with CLONE_FILES set in the clone flags a <BR>rgument. This can be seen in kernel/fork.c:copy_files() (called by do_fork() <BR>) which only increments the file-count if CLONE_FILES is set instead of the <BR>usual copying file descriptor table in time-honoured tradition of classical <BR>UNIX fork(2). <BR>When a file is opened the file structure allocated for it is installed into <BR>current-files-fd[fd] slot and a 'fd' bit is set in the bitmap current-files- <BR>open_fds. All this is done under the write protection of current-files-file_ <BR>lock read-write spinlock. When the descriptor is closed a 'fd' bit is cleare <BR>d in current-files-open_fds and current-files-next_fd is set equal to 'fd' a <BR>s a hint for finding the first unused descriptor next time this process want <BR>s to open a file. <BR>3.4 File Structure Management <BR>The file structure is declared in include/linux/fs.h: <BR>struct fown_struct { <BR> int pid; /* pid or -pgrp where SIGIO should be sent * <BR>/ <BR> uid_t uid, euid; /* uid/euid of process setting the owner */ <BR> int signum; /* posix.1b rt signal to be delivered on IO <BR>*/ <BR>}; <BR>struct file { <BR> struct list_head f_list; <BR> struct dentry *f_dentry; <BR> struct vfsmount *f_vfsmnt; <BR> struct file_operations *f_op; <BR> atomic_t f_count; <BR> unsigned int f_flags; <BR> mode_t f_mode; <BR> loff_t f_pos; <BR> unsigned long f_reada, f_ramax, f_raend, f_ralen, f_rawin; <BR> <BR> struct fown_struct f_owner; <BR> unsigned int f_uid, f_gid; <BR> int f_error; <BR> unsigned long f_version; <BR> /* needed for tty driver, and maybe others */ <BR> void *private_data; <BR>}; <BR>Let us look at the various fields of 'struct file': <BR>1. f_list - this field links file structure on one (and only one) of the lis <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -