📄 00000046.htm
字号:
ts: a) sb-s_files list of all open files on this filesystem, if the correspo <BR>nding inode is not anonymous, then dentry_open() (called by filp_open() link <BR>s the file into this list; b) fs/file_table.c:free_list containing unused fi <BR>le structures; c) fs/file_table.c:anon_list, when a new file structure is cr <BR>eated by get_empty_filp() it is placed on this list. All these lists are pro <BR>tected by files_lock spinlock <BR>2. f_dentry - the dentry corresponding to this file. The dentry is created a <BR>t nameidata lookup time by open_namei() (or rather path_walk() which it call <BR>s) but the actual file-f_dentry field is set by dentry_open() to contain the <BR> dentry thus found <BR>3. f_vfsmnt - the pointer to vfsmount structure of the filesystem containing <BR> the file. This is set by dentry_open() but is found as part of nameidata lo <BR>okup by open_namei() (or rather path_init() which it calls) <BR>4. f_op - the pointer to file_operations which contains various methods that <BR> can be invoked on the file. This is copied from inode-i_fop which is placed <BR> there by filesystem-specific s_op-read_inode() method during nameidata look <BR>up. We will look at file_operations methods in detail later on in this secti <BR>on <BR>5. f_count - reference count manipulated by get_file/put_filp/fput <BR>6. f_flags - O_XXX flags from open(2) system call copied there (with slight <BR>modifications by filp_open) by dentry_open and after clearing O_CREAT, O_EXC <BR>L, O_NOCTTY, O_TRUNC - there is no point in storing these flags permanently <BR>since they cannot be modified by F_SETFL (or queried by F_GETFL) fcntl(2) ca <BR>lls <BR>7. f_mode - a combination of userspace flags and mode, set by dentry_open(). <BR> The point of the conversion is to store read and write access in separate b <BR>its so one could do easy checks like (f_mode & FMODE_WRITE) and (f_mode & FM <BR>ODE_READ) <BR>8. f_pos - a current file position for next read or write to the file. Under <BR> i386 it is of type long long, i.e. a 64bit value <BR>9. f_reada, f_ramax, f_raend, f_ralen, f_rawin - to support readahead - too <BR>complex to be discussed by mortals ;) <BR>10. f_owner - owner of file io to receive asynchronous io notifications via <BR>SIGIO mechanism (see fs/fcntl.c:kill_fasync()) <BR>11. f_uid, f_gid - set to user id and group id of the process that opened th <BR>e file, when the file structure is created in get_empty_filp(). If the file <BR>is a socket, used by ipv4 netfilter <BR>12. f_error - used by NFS client to return write errors. It is set in fs/nfs <BR>/file.c and checked in mm/filemap.c:generic_file_write() <BR>13. f_version - versioning mechanism for invalidating caches, incremented (u <BR>sing global 'event') whenever f_pos changes <BR>14. private_data - private per-file data which can be used by filesystems (e <BR>.g. coda stores credentials here) or by device drivers. Device drivers (in t <BR>he presence of devfs) could use this field to differentiate between multiple <BR> instances instead of the classical minor number encoded in file-f_dentry-d_ <BR>inode-i_rdev <BR>Now let us look at file_operations structure which contains the methods that <BR> can be invoked on files. Let us recall that it is copied from inode-i_fop w <BR>here it is set by s_op-read_inode() method. It is declared in include/linux/ <BR>fs.h: <BR>struct file_operations { <BR> struct module *owner; <BR> loff_t (*llseek) (struct file *, loff_t, int); <BR> ssize_t (*read) (struct file *, char *, size_t, loff_t *); <BR> ssize_t (*write) (struct file *, const char *, size_t, loff_t *); <BR> int (*readdir) (struct file *, void *, filldir_t); <BR> unsigned int (*poll) (struct file *, struct poll_table_struct *); <BR> int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned <BR>long); <BR> int (*mmap) (struct file *, struct vm_area_struct *); <BR> int (*open) (struct inode *, struct file *); <BR> int (*flush) (struct file *); <BR> int (*release) (struct inode *, struct file *); <BR> int (*fsync) (struct file *, struct dentry *, int datasync); <BR> int (*fasync) (int, struct file *, int); <BR> int (*lock) (struct file *, int, struct file_lock *); <BR> ssize_t (*readv) (struct file *, const struct iovec *, unsigned long <BR>, loff_t *); <BR> ssize_t (*writev) (struct file *, const struct iovec *, unsigned lon <BR>g, loff_t *); <BR>}; <BR>1. owner - a pointer to the module that owns the subsystem in question. Only <BR> drivers need to set it to THIS_MODULE, filesystems can happily ignore it be <BR>cause their module counts are controlled at mount/umount time whilst the dri <BR>vers need to control it at open/release time <BR>2. llseek - implements the lseek(2) system call. Usually it is omitted and f <BR>s/read_write.c:default_llseek() is used which does the right thing (TODO: fo <BR>rce all those who set it to NULL currently to use default_llseek - that way <BR>we save an if() in llseek()) <BR>3. read - implements read(2) system call. Filesystems can use mm/filemap.c:g <BR>eneric_file_read() for regular files and fs/read_write.c:generic_read_dir() <BR>(which simply returns -EISDIR) for directories here <BR>4. write - implements write(2) system call. Filesystems can use mm/filemap.c <BR>∶<I>generic_file_write() for regular files and ignore it for directories here </I><BR>5. readdir - used by filesystems. Ignored for regular files and implements r <BR>eaddir(2) and getdents(2) system calls for directories <BR>6. poll - implements poll(2) and select(2) system calls <BR>7. ioctl - implements driver or filesystem-specific ioctls. Note that generi <BR>c file ioctls like FIBMAP, FIGETBSZ, FIONREAD are implemented by higher leve <BR>ls so they never read f_op-ioctl() method <BR>8. mmap - implements mmap system call. Filesystems can use generic_file_mmap <BR> here for regular files and ignore it on directories <BR>9. open - called at open(2) time by dentry_open(). Filesystems rarely use th <BR>is, e.g. coda tries to cache the file locally at open time <BR>10. flush - called at each close(2) of this file, not necessarily the last o <BR>ne (see release() method below). The only filesystem that uses this is NFS c <BR>lient to flush all dirty pages. Note that this can return an error which wil <BR>l be passed back to userspace which made the close(2) system call <BR>11. release - called at the last close(2) of this file, i.e. when file-f_cou <BR>nt reaches 0. Although defined as returning int, the return value is ignored <BR> by VFS (see fs/file_table.c:__fput()) <BR>12. fsync - maps directly to fsync(2)/fdatasync(2) system calls, with the la <BR>st argument specifying whether it is fsync or fdatasync. Almost no work is d <BR>one by VFS around this, except to map file descriptor to a file structure (f <BR>ile = fget(fd)) and down/up inode-i_sem semaphore. Ext2 filesystem currently <BR> ignores the last argument and does exactly the same for fsync(2) and fdatas <BR>ync(2) <BR>13. fasync - this method is called when file-f_flags & FASYNC changes <BR>14. lock - the filesystem-specific portion of the POSIX fcntl() file region <BR>locking mechanism. The only bug here is that because it is called before fs- <BR>independent portion (posix_lock_file()), if it succeeds but the standard pos <BR>ix lock code fails then it will never be unlocked on fs-dependent level.. <BR>15. readv - implements readv(2) system call <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -