免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2313 | 回复: 2
打印 上一主题 下一主题

[文件系统] 文件洞问题 [复制链接]

论坛徽章:
16
2015亚冠之吉达阿赫利
日期:2015-08-17 11:21:462015年迎新春徽章
日期:2015-03-04 09:58:11酉鸡
日期:2014-12-07 09:06:19水瓶座
日期:2014-11-04 14:23:29天秤座
日期:2014-03-02 08:57:52双鱼座
日期:2014-02-22 13:07:56午马
日期:2014-02-14 11:08:18双鱼座
日期:2014-02-13 11:09:37卯兔
日期:2014-02-06 15:10:34子鼠
日期:2014-01-20 14:48:19戌狗
日期:2013-12-19 09:37:46射手座
日期:2013-12-19 09:33:47
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-05-03 21:13 |只看该作者 |倒序浏览
30可用积分
在普通文件的readpage方法中和块设备文件的readpage方法中对file hole的处理不一样,linux 2.6.12:
在ext3_readpage->mpage_readpage->do_mpage_readpage函数中:
  1. 191        for (page_block = 0; page_block < blocks_per_page;
  2. 192                                page_block++, block_in_file++) {
  3. 193                bh.b_state = 0;
  4. 194                if (block_in_file < last_block) {
  5. 195                        if (get_block(inode, block_in_file, &bh, 0))
  6. 196                                goto confused;
  7. 197                }
  8. 198
  9. 199                if (!buffer_mapped(&bh)) {
  10. 200                        fully_mapped = 0;
  11. 201                        if (first_hole == blocks_per_page)
  12. 202                                first_hole = page_block;
  13. 203                        continue;
  14. 204                }
  15. 205
  16. 206                /* some filesystems will copy data into the page during
  17. 207                 * the get_block call, in which case we don't want to
  18. 208                 * read it again.  map_buffer_to_page copies the data
  19. 209                 * we just collected from get_block into the page's buffers
  20. 210                 * so readpage doesn't have to repeat the get_block call
  21. 211                 */
  22. 212                if (buffer_uptodate(&bh)) {
  23. 213                        map_buffer_to_page(page, &bh, page_block);
  24. 214                        goto confused;
  25. 215                }
  26. 216        
  27. 217                if (first_hole != blocks_per_page)
  28. 218                        goto confused;          /* hole -> non-hole */
  29. 219
  30. 220                /* Contiguous blocks? */
  31. 221                if (page_block && blocks[page_block-1] != bh.b_blocknr-1)
  32. 222                        goto confused;
  33. 223                blocks[page_block] = bh.b_blocknr;
  34. 224                bdev = bh.b_bdev;
  35. 225        }
  36. 226
  37. 227        if (first_hole != blocks_per_page) {
  38. 228                char *kaddr = kmap_atomic(page, KM_USER0);
  39. 229                memset(kaddr + (first_hole << blkbits), 0,
  40. 230                                PAGE_CACHE_SIZE - (first_hole << blkbits));
  41. 231                flush_dcache_page(page);
  42. 232                kunmap_atomic(kaddr, KM_USER0);
  43. 233                if (first_hole == 0) {
  44. 234                        SetPageUptodate(page);
  45. 235                        unlock_page(page);
  46. 236                        goto out;
  47. 237                }
  48. 238        } else if (fully_mapped) {
  49. 239                SetPageMappedToDisk(page);
  50. 240        }
复制代码
在出现hole->non-hole的情况下,不会执行将file hole填充为0的代码。

而在块设备文件的readpage方法中,blkdev_readpage->block_read_full_page函数中:
  1. 2092        do {
  2. 2093                if (buffer_uptodate(bh))
  3. 2094                        continue;
  4. 2095
  5. 2096                if (!buffer_mapped(bh)) {
  6. 2097                        int err = 0;
  7. 2098
  8. 2099                        fully_mapped = 0;
  9. 2100                        if (iblock < lblock) {
  10. 2101                                err = get_block(inode, iblock, bh, 0);
  11. 2102                                if (err)
  12. 2103                                        SetPageError(page);
  13. 2104                        }
  14. 2105                        if (!buffer_mapped(bh)) {
  15. 2106                                void *kaddr = kmap_atomic(page, KM_USER0);
  16. 2107                                memset(kaddr + i * blocksize, 0, blocksize);
  17. 2108                                flush_dcache_page(page);
  18. 2109                                kunmap_atomic(kaddr, KM_USER0);
  19. 2110                                if (!err)
  20. 2111                                        set_buffer_uptodate(bh);
  21. 2112                                continue;
  22. 2113                        }
  23. 2114                        /*
  24. 2115                         * get_block() might have updated the buffer
  25. 2116                         * synchronously
  26. 2117                         */
  27. 2118                        if (buffer_uptodate(bh))
  28. 2119                                continue;
  29. 2120                }
  30. 2121                arr[nr++] = bh;
  31. 2122        } while (i++, iblock++, (bh = bh->b_this_page) != head);
复制代码
每遇到一个逻辑块是file hole的,都会用0填充

为什么在普通文件的readpage方法中和块设备文件的readpage方法中对file hole的处理不一样?

论坛徽章:
16
2015亚冠之吉达阿赫利
日期:2015-08-17 11:21:462015年迎新春徽章
日期:2015-03-04 09:58:11酉鸡
日期:2014-12-07 09:06:19水瓶座
日期:2014-11-04 14:23:29天秤座
日期:2014-03-02 08:57:52双鱼座
日期:2014-02-22 13:07:56午马
日期:2014-02-14 11:08:18双鱼座
日期:2014-02-13 11:09:37卯兔
日期:2014-02-06 15:10:34子鼠
日期:2014-01-20 14:48:19戌狗
日期:2013-12-19 09:37:46射手座
日期:2013-12-19 09:33:47
2 [报告]
发表于 2012-05-03 21:17 |只看该作者
回复 1# embeddedlwp

函数前面的comments:


162/**
163 * mpage_readpages - populate an address space with some pages, and
164 *                       start reads against them.
165 *
166 * @mapping: the address_space
167 * @pages: The address of a list_head which contains the target pages.  These
168 *   pages have their ->index populated and are otherwise uninitialised.
169 *
170 *   The page at @pages->prev has the lowest file offset, and reads should be
171 *   issued in @pages->prev to @pages->next order.
172 *
173 * @nr_pages: The number of pages at *@pages
174 * @get_block: The filesystem's block mapper function.
175 *
176 * This function walks the pages and the blocks within each page, building and
177 * emitting large BIOs.
178 *
179 * If anything unusual happens, such as:
180 *
181 * - encountering a page which has buffers
182 * - encountering a page which has a non-hole after a hole
183 * - encountering a page with non-contiguous blocks
184 *
185 * then this code just gives up and calls the buffer_head-based read function.
186 * It does handle a page which has holes at the end - that is a common case:
187 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
188 *
189 * BH_Boundary explanation:
190 *
191 * There is a problem.  The mpage read code assembles several pages, gets all
192 * their disk mappings, and then submits them all.  That's fine, but obtaining
193 * the disk mappings may require I/O.  Reads of indirect blocks, for example.
194 *
195 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
196 * submitted in the following order:
197 *      12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
198 * because the indirect block has to be read to get the mappings of blocks
199 * 13,14,15,16.  Obviously, this impacts performance.
200 *
201 * So what we do it to allow the filesystem's get_block() function to set
202 * BH_Boundary when it maps block 11.  BH_Boundary says: mapping of the block
203 * after this one will require I/O against a block which is probably close to
204 * this one.  So you should push what I/O you have currently accumulated.
205 *
206 * This all causes the disk requests to be issued in the correct order.
207 */


   

论坛徽章:
0
3 [报告]
发表于 2012-09-21 11:48 |只看该作者
本帖最后由 blake326 于 2012-09-21 11:50 编辑

readpage以page为单位来处理的,假设块1024B大小,readpage会对4个block分别检查:
如果没有hole并且连续的,mpage_bio憋住等待提交请求。
如果没有hole但是不连续的,bufferhead方式读取。
如果发现第2,3,4个block是first hole的话,用bufferhead方式读取。
如果发现第1个lock是first hole,2,3,4block存在数据时,用bufferhead方式读取。
如果发现4个block都是hole的话,将page清0,直接返回不用读了。

block read fullpage是以block为单位操作的。

没有感觉到哪有有不正常的啊:9
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP