庞大资源库的计算机教程网站!
设为首页
加入收藏
总编信箱
投稿或申请专栏请先 [登 陆]
首页 操作系统 程序设计 图形图像 媒体动画 机械电子 WEB开发 数 据 库 办公系列 路由技术 网络原理 网络应用
认证考试 安全技术
首页>操作系统>Linux>编程开发>正文
资料搜索
Google搜索
Google
返回上级列表

推荐文章

快速保存网页中所有图片的方法
Windows中让光驱巧妙“隐身”技
防范非法用户入侵Win 2000/XP系
两款比较典型的ASP木马防范方法
有关表格边框的css语法整理
Windows XP中可以被禁用的服务
SQL Server导出导入数据方法
Javascript所有对象的属性的获
网页(HTML)中的特殊字符
与篮球共舞,尽显模式本色
QQ病毒的手工清除方法
Photoshop为极品美女打造性感睫
天衣无缝:IIS与PHP水火也相容
SQL Server存储过程编写和优化

Linux Kernel Internals(4)--Linux Page Cache

 作者:本站收集   日期:2005-4-13
字号选择〖 〗/ 双击滚屏 单击停止   
Next Previous Contents
--------------------------------------------------------------------------------

4. Linux Page Cache

In this chapter we describe the Linux 2.4 pagecache. The pagecache is - as the name suggests - a cache of physical pages. In the UNIX world the concept of a pagecache became popular with the introduction of SVR4 UNIX, where it replaced the buffercache for data IO operations.

While the SVR4 pagecache is only used for filesystem data cache and thus uses the struct vnode and an offset into the file as hash parameters, the Linux page cache is designed to be more generic, and therefore uses a struct address_space (explained below) as first parameter. Because the Linux pagecache is tightly coupled to the notation of address spaces, you will need at least a basic understanding of adress_spaces to understand the way the pagecache works. An address_space is some kind of software MMU that maps all pages of one object (e.g. inode) to an other concurrency (typically physical disk blocks). The struct address_space is defined in include/linux/fs.h as:



--------------------------------------------------------------------------------

struct address_space {
struct list_head pages;
unsigned long nrpages;
struct address_space_operations * a_ops;
void * host;
struct vm_area_struct * i_mmap;
struct vm_area_struct * i_mmap_shared;
spinlock_t i_shared_lock;
};


--------------------------------------------------------------------------------

To understand the way address_spaces works, we only need to look at a few of these fields: pages is a double linked list of all pages that belong to this address_space, nrpages is the number of pages in pages, a_ops defines the methods of this address_space and host is a opaque pointer to the object this address_space belongs to. The usage of pages and nrpages is obvious, so we will take a tighter look at the address_space_operations structure, defined in the same header:



--------------------------------------------------------------------------------

struct address_space_operations {
int (*writepage)(struct page *);
int (*readpage)(struct file *, struct page *);
int (*sync_page)(struct page *);
int (*prepare_write)(struct file *,
struct page *, unsigned, unsigned);
int (*commit_write)(struct file *,
struct page *, unsigned, unsigned);
int (*bmap)(struct address_space *, long);
};


--------------------------------------------------------------------------------

For a basic view at the principle of address_spaces (and the pagecache) we need to take a look at ->writepage and ->readpage, but in practice we need to take a look at ->prepare_write and ->commit_write, too.

You can probably guess what the address_space_operations methods do by virtue of their names alone; nevertheless, they do require some explanation. Their use in the course of filesystem data I/O, by far the most common path through the pagecache, provides a good way of understanding them. Unlike most other UNIX-like operating systems, Linux has generic file operations (a subset of the SYSVish vnode operations) for data IO through the pagecache. This means that the data will not directly interact with the file- system on read/write/mmap, but will be read/written from/to the pagecache whenever possible. The pagecache has to get data from the actual low-level filesystem in case the user wants to read from a page not yet in memory, or write data to disk in case memory gets low.

In the read path the generic methods will first try to find a page that matches the wanted inode/index tuple.


hash = page_hash(inode->i_mapping, index);
Then we test whether the page actually exists.


hash = page_hash(inode->i_mapping, index); page = __find_page_nolock(inode->i_mapping, index, *hash);
When it does not exist, we allocate a new free page, and add it to the page- cache hash.


page = page_cache_alloc(); __add_to_page_cache(page, mapping, index, hash);
After the page is hashed we use the ->readpage address_space operation to actually fill the page with data. (file is an open instance of inode).


error = mapping->a_ops->readpage(file, page);
Finally we can copy the data to userspace.

For writing to the filesystem two pathes exist: one for writable mappings (mmap) and one for the write(2) family of syscalls. The mmap case is very simple, so it will be discussed first. When a user modifies mappings, the VM subsystem marks the page dirty.


SetPageDirty(page);
The bdflush kernel thread that is trying to free pages, either as background activity or because memory gets low will try to call ->writepage on the pages that are explicitly marked dirty. The ->writepage method does now have to write the pages content back to disk and free the page.

The second write path is _much_ more complicated. For each page the user writes to, we are basically doing the following: (for the full code see mm/filemap.c:generic_file_write()).


page = __grab_cache_page(mapping, index, &cached_page); mapping->a_ops->prepare_write(file, page, offset, offset+bytes); copy_from_user(kaddr+offset, buf, bytes); mapping->a_ops->commit_write(file, page, offset, offset+bytes);
So first we try to find the hashed page or allocate a new one, then we call the ->prepare_write address_space method, copy the user buffer to kernel memory and finally call the ->commit_write method. As you probably have seen ->prepare_write and ->commit_write are fundamentally different from ->readpage and ->writepage, because they are not only called when physical IO is actually wanted but everytime the user modifies the file. There are two (or more?) ways to handle this, the first one uses the Linux buffercache to delay the physical IO, by filling a page->buffers pointer with buffer_heads, that will be used in try_to_free_buffers (fs/buffers.c) to request IO once memory gets low, and is used very widespread in the current kernel. The other way just sets the page dirty and relies on ->writepage to do all the work. Due to the lack of a validitity bitmap in struct page this does not work with filesystem that have a smaller granuality then PAGE_SIZE.




--------------------------------------------------------------------------------
Next Previous Contents
上一篇:Linux 命令好帮手 Bash    下一篇:Linux Kernel Internals(3cont.)--Virtual Filesystem  
[发送给好友]  [关闭窗口]  [返回顶部]   转载请注明来源:www.it00.com   
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
责任编辑: 原点 投稿作者: 本站收集
信息来源: 网络 录入时间: 2005-4-13
关于我们 - 广告服务 - 版权申明 - 网站地图 - 联系方式 - 总编信箱 - 会员投稿