手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux缓存机制之块缓存

[日期:2012-03-01] 来源:Linux社区  作者:bullbat [字体:大 中 小]

交互

如果对内核的其他部分无益,那么在页和缓冲区之间建立关联就没起作用。一些与块设备之间的传输操作,传输单位的长度依赖于底层设备的块长度,而内核的许多部分更喜欢按页的粒度来执行I/O操作,因为这使得其他事情更容易处理,特别是内存管理方面。在这种场景下,缓冲头区充当了双方的中介。

从缓冲区中读取整页

首先考察内核在从块设备读取整页时采用的方法,以block_read_full_page为例。我们讨论缓冲区实现所关注的部分。

  1. /* 
  2.  * Generic "read page" function for block devices that have the normal 
  3.  * get_block functionality. This is most of the block device filesystems. 
  4.  * Reads the page asynchronously --- the unlock_buffer() and 
  5.  * set/clear_buffer_uptodate() functions propagate buffer state into the 
  6.  * page struct once IO has completed. 
  7.  */  
  8. int block_read_full_page(struct page *page, get_block_t *get_block)  
  9. {  
  10.     struct inode *inode = page->mapping->host;  
  11.     sector_t iblock, lblock;  
  12.     struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];  
  13.     unsigned int blocksize;  
  14.     int nr, i;  
  15.     int fully_mapped = 1;  
  16.   
  17.     BUG_ON(!PageLocked(page));  
  18.     blocksize = 1 << inode->i_blkbits;  
  19.     /*检查页是否有相关联的缓冲区,如果没有,则创建他*/  
  20.     if (!page_has_buffers(page))  
  21.         create_empty_buffers(page, blocksize, 0);  
  22.     /*获得这些缓冲区,无论是新建的还是已经存在的 
  23.     只是将page的private成员转换为buffer_head指针,因为按照 
  24.     惯例,private指向与page关联的第一个缓冲头*/  
  25.     head = page_buffers(page);  
  26.   
  27.     iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);  
  28.     lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;  
  29.     bh = head;  
  30.     nr = 0;  
  31.     i = 0;  
  32.     /*内核遍历与页面关联的所有缓冲区*/  
  33.     do {  
  34.         /*如果缓冲区内容是最新的,内核继续处理下一个 
  35.         缓冲区。在这种情况下,页面缓冲区中的数据与块 
  36.         设备匹配,无需额外的读操作*/  
  37.         if (buffer_uptodate(bh))  
  38.             continue;  
  39.         /*如果没有映射*/  
  40.         if (!buffer_mapped(bh)) {  
  41.             int err = 0;  
  42.   
  43.             fully_mapped = 0;  
  44.             if (iblock < lblock) {  
  45.                 WARN_ON(bh->b_size != blocksize);  
  46.                 /*确定块在块设备上的位置*/  
  47.                 err = get_block(inode, iblock, bh, 0);  
  48.                 if (err)  
  49.                     SetPageError(page);  
  50.             }  
  51.             if (!buffer_mapped(bh)) {  
  52.                 zero_user(page, i * blocksize, blocksize);  
  53.                 if (!err)  
  54.                     set_buffer_uptodate(bh);  
  55.                 continue;  
  56.             }  
  57.             /* 
  58.              * get_block() might have updated the buffer 
  59.              * synchronously 
  60.              */  
  61.             if (buffer_uptodate(bh))  
  62.                 continue;  
  63.         }  
  64.         /*如果缓冲区已经建立了与块的映射,但是其内容不是最新 
  65.         的则将缓冲区放置到一个临时的数组中*/  
  66.         arr[nr++] = bh;  
  67.     } while (i++, iblock++, (bh = bh->b_this_page) != head);  
  68.   
  69.     if (fully_mapped)  
  70.         SetPageMappedToDisk(page);  
  71.   
  72.     if (!nr) {  
  73.         /* 
  74.          * All buffers are uptodate - we can set the page uptodate 
  75.          * as well. But not if get_block() returned an error. 
  76.          */  
  77.         if (!PageError(page))  
  78.             SetPageUptodate(page);  
  79.         unlock_page(page);  
  80.         return 0;  
  81.     }  
  82.   
  83.     /* Stage two: lock the buffers */  
  84.     for (i = 0; i < nr; i++) {  
  85.         bh = arr[i];  
  86.         lock_buffer(bh);  
  87.         /*将b_end_io设置为end_buffer_async_read,该函数将在数据传输结构时 
  88.         调用*/  
  89.         mark_buffer_async_read(bh);  
  90.     }  
  91.   
  92.     /* 
  93.      * Stage 3: start the IO.  Check for uptodateness 
  94.      * inside the buffer lock in case another process reading 
  95.      * the underlying blockdev brought it uptodate (the sct fix). 
  96.      */  
  97.     for (i = 0; i < nr; i++) {  
  98.         bh = arr[i];  
  99.         if (buffer_uptodate(bh))  
  100.             end_buffer_async_read(bh, 1);  
  101.         else  
  102.             /*将所有需要读取的缓冲区转交给块层 
  103.             也就是BIO层,在其中开始读操作*/  
  104.             submit_bh(READ, bh);  
  105.     }  
  106.     return 0;  
  107. }  
linux
相关资讯       Linux教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款