📄 vfs.txt
字号:
int (*rmdir) (struct inode *,struct dentry *); int (*mknod) (struct inode *,struct dentry *,int,dev_t); int (*rename) (struct inode *, struct dentry *, struct inode *, struct dentry *); int (*readlink) (struct dentry *, char __user *,int); void * (*follow_link) (struct dentry *, struct nameidata *); void (*put_link) (struct dentry *, struct nameidata *, void *); void (*truncate) (struct inode *); int (*permission) (struct inode *, int, struct nameidata *); 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 *); void (*truncate_range)(struct inode *, loff_t, loff_t);};Again, all methods are called without any locks being held, unlessotherwise noted. create: called by the open(2) and creat(2) system calls. Only required if you want to support regular files. The dentry you get should not have an inode (i.e. it should be a negative dentry). Here you will probably call d_instantiate() with the dentry and the newly created inode lookup: called when the VFS needs to look up an inode in a parent directory. The name to look for is found in the dentry. This method must call d_add() to insert the found inode into the dentry. The "i_count" field in the inode structure should be incremented. If the named inode does not exist a NULL inode should be inserted into the dentry (this is called a negative dentry). Returning an error code from this routine must only be done on a real error, otherwise creating inodes with system calls like create(2), mknod(2), mkdir(2) and so on will fail. If you wish to overload the dentry methods then you should initialise the "d_dop" field in the dentry; this is a pointer to a struct "dentry_operations". This method is called with the directory inode semaphore held link: called by the link(2) system call. Only required if you want to support hard links. You will probably need to call d_instantiate() just as you would in the create() method unlink: called by the unlink(2) system call. Only required if you want to support deleting inodes symlink: called by the symlink(2) system call. Only required if you want to support symlinks. You will probably need to call d_instantiate() just as you would in the create() method mkdir: called by the mkdir(2) system call. Only required if you want to support creating subdirectories. You will probably need to call d_instantiate() just as you would in the create() method rmdir: called by the rmdir(2) system call. Only required if you want to support deleting subdirectories mknod: called by the mknod(2) system call to create a device (char, block) inode or a named pipe (FIFO) or socket. Only required if you want to support creating these types of inodes. You will probably need to call d_instantiate() just as you would in the create() method rename: called by the rename(2) system call to rename the object to have the parent and name given by the second inode and dentry. readlink: called by the readlink(2) system call. Only required if you want to support reading symbolic links follow_link: called by the VFS to follow a symbolic link to the inode it points to. Only required if you want to support symbolic links. This method returns a void pointer cookie that is passed to put_link(). put_link: called by the VFS to release resources allocated by follow_link(). The cookie returned by follow_link() is passed to this method as the last parameter. It is used by filesystems such as NFS where page cache is not stable (i.e. page that was installed when the symbolic link walk started might not be in the page cache at the end of the walk). truncate: called by the VFS to change the size of a file. The i_size field of the inode is set to the desired size by the VFS before this method is called. This method is called by the truncate(2) system call and related functionality. permission: called by the VFS to check for access rights on a POSIX-like filesystem. setattr: called by the VFS to set attributes for a file. This method is called by chmod(2) and related system calls. getattr: called by the VFS to get attributes of a file. This method is called by stat(2) and related system calls. setxattr: called by the VFS to set an extended attribute for a file. Extended attribute is a name:value pair associated with an inode. This method is called by setxattr(2) system call. getxattr: called by the VFS to retrieve the value of an extended attribute name. This method is called by getxattr(2) function call. listxattr: called by the VFS to list all extended attributes for a given file. This method is called by listxattr(2) system call. removexattr: called by the VFS to remove an extended attribute from a file. This method is called by removexattr(2) system call. truncate_range: a method provided by the underlying filesystem to truncate a range of blocks , i.e. punch a hole somewhere in a file.The Address Space Object========================The address space object is used to group and manage pages in the pagecache. It can be used to keep track of the pages in a file (oranything else) and also track the mapping of sections of the file intoprocess address spaces.There are a number of distinct yet related services that anaddress-space can provide. These include communicating memorypressure, page lookup by address, and keeping track of pages tagged asDirty or Writeback.The first can be used independently to the others. The VM can try toeither write dirty pages in order to clean them, or release cleanpages in order to reuse them. To do this it can call the ->writepagemethod on dirty pages, and ->releasepage on clean pages withPagePrivate set. Clean pages without PagePrivate and with no externalreferences will be released without notice being given to theaddress_space.To achieve this functionality, pages need to be placed on an LRU withlru_cache_add and mark_page_active needs to be called whenever thepage is used.Pages are normally kept in a radix tree index by ->index. This treemaintains information about the PG_Dirty and PG_Writeback status ofeach page, so that pages with either of these flags can be foundquickly.The Dirty tag is primarily used by mpage_writepages - the default->writepages method. It uses the tag to find dirty pages to call->writepage on. If mpage_writepages is not used (i.e. the addressprovides its own ->writepages) , the PAGECACHE_TAG_DIRTY tag isalmost unused. write_inode_now and sync_inode do use it (through__sync_single_inode) to check if ->writepages has been successful inwriting out the whole address_space.The Writeback tag is used by filemap*wait* and sync_page* functions,via wait_on_page_writeback_range, to wait for all writeback tocomplete. While waiting ->sync_page (if defined) will be called oneach page that is found to require writeback.An address_space handler may attach extra information to a page,typically using the 'private' field in the 'struct page'. If suchinformation is attached, the PG_Private flag should be set. This willcause various VM routines to make extra calls into the address_spacehandler to deal with that data.An address space acts as an intermediate between storage andapplication. Data is read into the address space a whole page at atime, and provided to the application either by copying of the page,or by memory-mapping the page.Data is written into the address space by the application, and thenwritten-back to storage typically in whole pages, however theaddress_space has finer control of write sizes.The read process essentially only requires 'readpage'. The writeprocess is more complicated and uses prepare_write/commit_write orset_page_dirty to write data into the address_space, and writepage,sync_page, and writepages to writeback data to storage.Adding and removing pages to/from an address_space is protected by theinode's i_mutex.When data is written to a page, the PG_Dirty flag should be set. Ittypically remains set until writepage asks for it to be written. Thisshould clear PG_Dirty and set PG_Writeback. It can be actuallywritten at any point after PG_Dirty is clear. Once it is known to besafe, PG_Writeback is cleared.Writeback makes use of a writeback_control structure...struct address_space_operations-------------------------------This describes how the VFS can manipulate mapping of a file to page cache inyour filesystem. As of kernel 2.6.22, the following members are defined:struct address_space_operations { int (*writepage)(struct page *page, struct writeback_control *wbc); int (*readpage)(struct file *, struct page *); int (*sync_page)(struct page *); int (*writepages)(struct address_space *, struct writeback_control *); int (*set_page_dirty)(struct page *page); int (*readpages)(struct file *filp, struct address_space *mapping, struct list_head *pages, unsigned nr_pages); int (*prepare_write)(struct file *, struct page *, unsigned, unsigned); int (*commit_write)(struct file *, struct page *, unsigned, unsigned); int (*write_begin)(struct file *, struct address_space *mapping, loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata); int (*write_end)(struct file *, struct address_space *mapping, loff_t pos, unsigned len, unsigned copied, struct page *page, void *fsdata); sector_t (*bmap)(struct address_space *, sector_t); int (*invalidatepage) (struct page *, unsigned long); int (*releasepage) (struct page *, int); ssize_t (*direct_IO)(int, struct kiocb *, const struct iovec *iov, loff_t offset, unsigned long nr_segs); struct page* (*get_xip_page)(struct address_space *, sector_t, int); /* migrate the contents of a page to the specified target */ int (*migratepage) (struct page *, struct page *); int (*launder_page) (struct page *);}; writepage: called by the VM to write a dirty page to backing store. This may happen for data integrity reasons (i.e. 'sync'), or to free up memory (flush). The difference can be seen in wbc->sync_mode. The PG_Dirty flag has been cleared and PageLocked is true. writepage should start writeout, should set PG_Writeback, and should make sure the page is unlocked, either synchronously or asynchronously when the write operation completes. If wbc->sync_mode is WB_SYNC_NONE, ->writepage doesn't have to try too hard if there are problems, and may choose to write out other pages from the mapping if that is easier (e.g. due to internal dependencies). If it chooses not to start writeout, it should return AOP_WRITEPAGE_ACTIVATE so that the VM will not keep calling ->writepage on that page. See the file "Locking" for more details. readpage: called by the VM to read a page from backing store. The page will be Locked when readpage is called, and should be unlocked and marked uptodate once the read completes. If ->readpage discovers that it needs to unlock the page for some reason, it can do so, and then return AOP_TRUNCATED_PAGE. In this case, the page will be relocated, relocked and if that all succeeds, ->readpage will be called again. sync_page: called by the VM to notify the backing store to perform all queued I/O operations for a page. I/O operations for other pages associated with this address_space object may also be performed. This function is optional and is called only for pages with PG_Writeback set while waiting for the writeback to complete. writepages: called by the VM to write out pages associated with the address_space object. If wbc->sync_mode is WBC_SYNC_ALL, then the writeback_control will specify a range of pages that must be written out. If it is WBC_SYNC_NONE, then a nr_to_write is given and that many pages should be written if possible. If no ->writepages is given, then mpage_writepages is used instead. This will choose pages from the address space that are tagged as DIRTY and will pass them to ->writepage. set_page_dirty: called by the VM to set a page dirty. This is particularly needed if an address space attaches private data to a page, and that data needs to be updated when a page is dirtied. This is called, for example, when a memory mapped page gets modified. If defined, it should set the PageDirty flag, and the PAGECACHE_TAG_DIRTY tag in the radix tree. readpages: called by the VM to read pages associated with the address_space object. This is essentially just a vector version of readpage. Instead of just one page, several pages are requested. readpages is only used for read-ahead, so read errors are ignored. If anything goes wrong, feel free to give up. prepare_write: called by the generic write path in VM to set up a write request for a page. This indicates to the address space that the given range of bytes is about to be written. The address_space should check that the write will be able to complete, by allocating space if necessary and doing any other internal housekeeping. If the write will update parts of any basic-blocks on storage, then those blocks should be pre-read (if they haven't been read already) so that the updated blocks can be written out properly. The page will be locked. Note: the page _must not_ be marked uptodate in this function (or anywhere else) unless it actually is uptodate right now. As soon as a page is marked uptodate, it is possible for a concurrent read(2) to copy it to userspace. commit_write: If prepare_write succeeds, new data will be copied into the page and then commit_write will be called. It will typically update the size of the file (if appropriate) and mark the inode as dirty, and do any other related housekeeping operations. It should avoid returning an error if possible - errors should have been handled by prepare_write. write_begin: This is intended as a replacement for prepare_write. The key differences being that: - it returns a locked page (in *pagep) rather than being given a pre locked page; - it must be able to cope with short writes (where the length passed to write_begin is greater than the number of bytes copied into the page). Called by the generic buffered write code to ask the filesystem to prepare to write len bytes at the given offset in the file. The address_space should check that the write will be able to complete, by allocating space if necessary and doing any other internal housekeeping. If the write will update parts of any basic-blocks on storage, then those blocks should be pre-read (if they haven't been read already) so that the updated blocks can be written out properly. The filesystem must return the locked pagecache page for the specified offset, in *pagep, for the caller to write into. flags is a field for AOP_FLAG_xxx flags, described in include/linux/fs.h. A void * may be returned in fsdata, which then gets passed into write_end. Returns 0 on success; < 0 on failure (which is the error code), in which case write_end is not called. write_end: After a successful write_begin, and data copy, write_end must be called. len is the original len passed to write_begin, and copied is the amount that was able to be copied (copied == len is always true
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -