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

Linux缓存机制之块缓存

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

将整页写入到缓冲区

除了读操作之外,页面的写操作也可以划分为更小的单位。只有页中实际修改的内容需要回写,而不用回写整页的内容。遗憾的是,从缓冲区的角度来看,写操作的实现比上述的读操作复杂的多。

__block_wirte_full_page函数中回写脏页面设计的缓冲区相关操作。

  1. /* 
  2.  * NOTE! All mapped/uptodate combinations are valid: 
  3.  * 
  4.  *  Mapped  Uptodate    Meaning 
  5.  * 
  6.  *  No  No      "unknown" - must do get_block() 
  7.  *  No  Yes     "hole" - zero-filled 
  8.  *  Yes No      "allocated" - allocated on disk, not read in 
  9.  *  Yes Yes     "valid" - allocated and up-to-date in memory. 
  10.  * 
  11.  * "Dirty" is valid only with the last case (mapped+uptodate). 
  12.  */  
  13.   
  14. /* 
  15.  * While block_write_full_page is writing back the dirty buffers under 
  16.  * the page lock, whoever dirtied the buffers may decide to clean them 
  17.  * again at any time.  We handle that by only looking at the buffer 
  18.  * state inside lock_buffer(). 
  19.  * 
  20.  * If block_write_full_page() is called for regular writeback 
  21.  * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a 
  22.  * locked buffer.   This only can happen if someone has written the buffer 
  23.  * directly, with submit_bh().  At the address_space level PageWriteback 
  24.  * prevents this contention from occurring. 
  25.  * 
  26.  * If block_write_full_page() is called with wbc->sync_mode == 
  27.  * WB_SYNC_ALL, the writes are posted using WRITE_SYNC_PLUG; this 
  28.  * causes the writes to be flagged as synchronous writes, but the 
  29.  * block device queue will NOT be unplugged, since usually many pages 
  30.  * will be pushed to the out before the higher-level caller actually 
  31.  * waits for the writes to be completed.  The various wait functions, 
  32.  * such as wait_on_writeback_range() will ultimately call sync_page() 
  33.  * which will ultimately call blk_run_backing_dev(), which will end up 
  34.  * unplugging the device queue. 
  35.  */  
  36. static int __block_write_full_page(struct inode *inode, struct page *page,  
  37.             get_block_t *get_block, struct writeback_control *wbc,  
  38.             bh_end_io_t *handler)  
  39. {  
  40.     int err;  
  41.     sector_t block;  
  42.     sector_t last_block;  
  43.     struct buffer_head *bh, *head;  
  44.     const unsigned blocksize = 1 << inode->i_blkbits;  
  45.     int nr_underway = 0;  
  46.     int write_op = (wbc->sync_mode == WB_SYNC_ALL ?  
  47.             WRITE_SYNC_PLUG : WRITE);  
  48.   
  49.     BUG_ON(!PageLocked(page));  
  50.   
  51.     last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;  
  52.     /*页面是否有关联缓冲区,如果没有创建他*/  
  53.     if (!page_has_buffers(page)) {  
  54.         create_empty_buffers(page, blocksize,  
  55.                     (1 << BH_Dirty)|(1 << BH_Uptodate));  
  56.     }  
  57.   
  58.     /* 
  59.      * Be very careful.  We have no exclusion from __set_page_dirty_buffers 
  60.      * here, and the (potentially unmapped) buffers may become dirty at 
  61.      * any time.  If a buffer becomes dirty here after we've inspected it 
  62.      * then we just miss that fact, and the page stays dirty. 
  63.      * 
  64.      * Buffers outside i_size may be dirtied by __set_page_dirty_buffers; 
  65.      * handle that here by just cleaning them. 
  66.      */  
  67.   
  68.     block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);  
  69.     head = page_buffers(page);  
  70.     bh = head;  
  71.   
  72.     /* 
  73.      * Get all the dirty buffers mapped to disk addresses and 
  74.      * handle any aliases from the underlying blockdev's mapping. 
  75.      */  
  76.      /*对所有未映射的脏缓冲区,在缓冲区和块设备 
  77.     之间建立映射*/  
  78.     do {  
  79.         if (block > last_block) {  
  80.             /* 
  81.              * mapped buffers outside i_size will occur, because 
  82.              * this page can be outside i_size when there is a 
  83.              * truncate in progress. 
  84.              */  
  85.             /* 
  86.              * The buffer was zeroed by block_write_full_page() 
  87.              */  
  88.             clear_buffer_dirty(bh);  
  89.             set_buffer_uptodate(bh);  
  90.         } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&  
  91.                buffer_dirty(bh)) {  
  92.             WARN_ON(bh->b_size != blocksize);  
  93.             /*查找块设备上与缓冲区项匹配的块*/  
  94.             err = get_block(inode, block, bh, 1);  
  95.             if (err)  
  96.                 goto recover;  
  97.             clear_buffer_delay(bh);  
  98.             if (buffer_new(bh)) {  
  99.                 /* blockdev mappings never come here */  
  100.                 clear_buffer_new(bh);  
  101.                 unmap_underlying_metadata(bh->b_bdev,  
  102.                             bh->b_blocknr);  
  103.             }  
  104.         }  
  105.         bh = bh->b_this_page;  
  106.         block++;  
  107.     } while (bh != head);  
  108.     /*第二遍遍历,将滤出所有的脏缓冲区*/  
  109.     do {  
  110.         if (!buffer_mapped(bh))  
  111.             continue;  
  112.         /* 
  113.          * If it's a fully non-blocking write attempt and we cannot 
  114.          * lock the buffer then redirty the page.  Note that this can 
  115.          * potentially cause a busy-wait loop from writeback threads 
  116.          * and kswapd activity, but those code paths have their own 
  117.          * higher-level throttling. 
  118.          */  
  119.         if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {  
  120.             lock_buffer(bh);  
  121.         } else if (!trylock_buffer(bh)) {  
  122.             redirty_page_for_writepage(wbc, page);  
  123.             continue;  
  124.         }  
  125.         /*如果设置了脏页标志,则会在调用该函数时清除 
  126.         因为缓冲区的内容将立即回写*/  
  127.         if (test_clear_buffer_dirty(bh)) {  
  128.             /*设置BH_Async_Write状态位,并将end_buffer_async_write 
  129.             指定为BIO完成处理程序即b_end_io*/  
  130.             mark_buffer_async_write_endio(bh, handler);  
  131.         } else {  
  132.             unlock_buffer(bh);  
  133.         }  
  134.     } while ((bh = bh->b_this_page) != head);  
  135.   
  136.     /* 
  137.      * The page and its buffers are protected by PageWriteback(), so we can 
  138.      * drop the bh refcounts early. 
  139.      */  
  140.     BUG_ON(PageWriteback(page));  
  141.     set_page_writeback(page);  
  142.     /*最后一次遍历*/  
  143.     do {  
  144.         struct buffer_head *next = bh->b_this_page;  
  145.         if (buffer_async_write(bh)) {  
  146.             /*将前一次遍历中标记为BH_Async_Write的所有缓冲区 
  147.             转交给块层执行实际的写操作,该函数向块层提交 
  148.             了对应的请求*/  
  149.             submit_bh(write_op, bh);  
  150.             nr_underway++;  
  151.         }  
  152.         bh = next;  
  153.     } while (bh != head);  
  154.     unlock_page(page);  
  155.   
  156.     err = 0;  
  157. done:  
  158.     if (nr_underway == 0) {  
  159.         /* 
  160.          * The page was marked dirty, but the buffers were 
  161.          * clean.  Someone wrote them back by hand with 
  162.          * ll_rw_block/submit_bh.  A rare case. 
  163.          */  
  164.         end_page_writeback(page);  
  165.   
  166.         /* 
  167.          * The page and buffer_heads can be released at any time from 
  168.          * here on. 
  169.          */  
  170.     }  
  171.     return err;  
  172.   
  173. recover:  
  174.     /* 
  175.      * ENOSPC, or some other error.  We may already have added some 
  176.      * blocks to the file, so we need to write these out to avoid 
  177.      * exposing stale data. 
  178.      * The page is currently locked and not marked for writeback 
  179.      */  
  180.     bh = head;  
  181.     /* Recovery: lock and submit the mapped buffers */  
  182.     do {  
  183.         if (buffer_mapped(bh) && buffer_dirty(bh) &&  
  184.             !buffer_delay(bh)) {  
  185.             lock_buffer(bh);  
  186.             mark_buffer_async_write_endio(bh, handler);  
  187.         } else {  
  188.             /* 
  189.              * The buffer may have been set dirty during 
  190.              * attachment to a dirty page. 
  191.              */  
  192.             clear_buffer_dirty(bh);  
  193.         }  
  194.     } while ((bh = bh->b_this_page) != head);  
  195.     SetPageError(page);  
  196.     BUG_ON(PageWriteback(page));  
  197.     mapping_set_error(page->mapping, err);  
  198.     set_page_writeback(page);  
  199.     do {  
  200.         struct buffer_head *next = bh->b_this_page;  
  201.         if (buffer_async_write(bh)) {  
  202.             clear_buffer_dirty(bh);  
  203.             submit_bh(write_op, bh);  
  204.             nr_underway++;  
  205.         }  
  206.         bh = next;  
  207.     } while (bh != head);  
  208.     unlock_page(page);  
  209.     goto done;  
  210. }  
linux
相关资讯       Linux教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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