免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3875 | 回复: 5

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

论坛徽章:
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
发表于 2012-04-30 13:11 |显示全部楼层
本帖最后由 embeddedlwp 于 2012-04-30 13:12 编辑

在普通文件的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
发表于 2012-05-02 08:21 |显示全部楼层
本帖最后由 embeddedlwp 于 2012-05-02 20:28 编辑


函数前面的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
发表于 2018-01-25 15:40 |显示全部楼层
因为在块设备就没有hole这么一说,你在可以看看blkdev_get_block,除非inode有问题否则set_buffer_mapped(bh);永远都被设置。
为什么/dev/sda没有hole的概念,因为/dev/sda就是表示磁盘本身,磁盘本身所有的block必然与磁盘的物理扇区对应,所以没有hole。
  1. static int
  2. blkdev_get_block(struct inode *inode, sector_t iblock,
  3.                 struct buffer_head *bh, int create)
  4. {
  5.         if (iblock >= max_block(I_BDEV(inode))) {
  6.                 if (create)
  7.                         return -EIO;

  8.                 /*
  9.                  * for reads, we're just trying to fill a partial page.
  10.                  * return a hole, they will have to call get_block again
  11.                  * before they can fill it, and they will get -EIO at that
  12.                  * time
  13.                  */
  14.                 return 0;
  15.         }
  16.         bh->b_bdev = I_BDEV(inode);
  17.         bh->b_blocknr = iblock;
  18.         set_buffer_mapped(bh);
  19.         return 0;
  20. }
复制代码


子文件系统(ext2,ext3,ext4 etc)
文件有hole 的概念,这里的hole是为了节省磁盘block空间
例如读取一个带hole 的inode
sudo dd if=/dev/sda of=./test.txt bs=4096 seek=10 count=1

  1. inode[23]:
  2.         i_mode 81a4
  3.         i_uid 0000
  4.         i_size 0000b000
  5.         i_atime 5a69873b
  6.         i_ctime 5a69873b
  7.         i_mtime 5a69873b
  8.         i_dtime 00000000
  9.         i_gid 0000
  10.         i_links_count 0001
  11.         i_blocks 00000008
  12.         i_flags 00000000
  13.         i_faddr 00000000
  14.         i_dir_acl 00000000
  15.         i_file_acl 00000000
  16.         i_generation 248582a1
  17.                 i_block[ 0] 00000000
  18.                 i_block[ 1] 00000000
  19.                 i_block[ 2] 00000000
  20.                 i_block[ 3] 00000000
  21.                 i_block[ 4] 00000000
  22.                 i_block[ 5] 00000000
  23.                 i_block[ 6] 00000000
  24.                 i_block[ 7] 00000000
  25.                 i_block[ 8] 00000000
  26.                 i_block[ 9] 00000000
  27.                 [color=Red]i_block[10] 0008a200[/color]
  28.                 i_block[11] 00000000
  29.                 i_block[12] 00000000
  30.                 i_block[13] 00000000
  31.                 i_block[14] 00000000
复制代码

看看这里./test.txt虽然size大小等于44KB,但其实是占用了 0008a200H一个块,也就是文件实际大小只占用了4KB。

论坛徽章:
0
发表于 2018-01-25 15:40 |显示全部楼层
因为在块设备就没有hole这么一说,你在可以看看blkdev_get_block,除非inode有问题否则set_buffer_mapped(bh);永远都被设置。
为什么/dev/sda没有hole的概念,因为/dev/sda就是表示磁盘本身,磁盘本身所有的block必然与磁盘的物理扇区对应,所以没有hole。
  1. static int
  2. blkdev_get_block(struct inode *inode, sector_t iblock,
  3.                 struct buffer_head *bh, int create)
  4. {
  5.         if (iblock >= max_block(I_BDEV(inode))) {
  6.                 if (create)
  7.                         return -EIO;

  8.                 /*
  9.                  * for reads, we're just trying to fill a partial page.
  10.                  * return a hole, they will have to call get_block again
  11.                  * before they can fill it, and they will get -EIO at that
  12.                  * time
  13.                  */
  14.                 return 0;
  15.         }
  16.         bh->b_bdev = I_BDEV(inode);
  17.         bh->b_blocknr = iblock;
  18.         set_buffer_mapped(bh);
  19.         return 0;
  20. }
复制代码


子文件系统(ext2,ext3,ext4 etc)
文件有hole 的概念,这里的hole是为了节省磁盘block空间
例如读取一个带hole 的inode
sudo dd if=/dev/sda of=./test.txt bs=4096 seek=10 count=1

  1. inode[23]:
  2.         i_mode 81a4
  3.         i_uid 0000
  4.         i_size 0000b000
  5.         i_atime 5a69873b
  6.         i_ctime 5a69873b
  7.         i_mtime 5a69873b
  8.         i_dtime 00000000
  9.         i_gid 0000
  10.         i_links_count 0001
  11.         i_blocks 00000008
  12.         i_flags 00000000
  13.         i_faddr 00000000
  14.         i_dir_acl 00000000
  15.         i_file_acl 00000000
  16.         i_generation 248582a1
  17.                 i_block[ 0] 00000000
  18.                 i_block[ 1] 00000000
  19.                 i_block[ 2] 00000000
  20.                 i_block[ 3] 00000000
  21.                 i_block[ 4] 00000000
  22.                 i_block[ 5] 00000000
  23.                 i_block[ 6] 00000000
  24.                 i_block[ 7] 00000000
  25.                 i_block[ 8] 00000000
  26.                 i_block[ 9] 00000000
  27.                 [color=Red]i_block[10] 0008a200[/color]
  28.                 i_block[11] 00000000
  29.                 i_block[12] 00000000
  30.                 i_block[13] 00000000
  31.                 i_block[14] 00000000
复制代码

看看这里./test.txt虽然size大小等于44KB,但其实是占用了 0008a200H一个块,也就是文件实际大小只占用了4KB。

论坛徽章:
0
发表于 2018-01-25 15:40 |显示全部楼层
因为在块设备就没有hole这么一说,你在可以看看blkdev_get_block,除非inode有问题否则set_buffer_mapped(bh);永远都被设置。
为什么/dev/sda没有hole的概念,因为/dev/sda就是表示磁盘本身,磁盘本身所有的block必然与磁盘的物理扇区对应,所以没有hole。
  1. static int
  2. blkdev_get_block(struct inode *inode, sector_t iblock,
  3.         struct buffer_head *bh, int create)
  4. {
  5.     if (iblock >= max_block(I_BDEV(inode))) {
  6.         if (create)
  7.             return -EIO;

  8.         /*
  9.          * for reads, we're just trying to fill a partial page.
  10.          * return a hole, they will have to call get_block again
  11.          * before they can fill it, and they will get -EIO at that
  12.          * time
  13.          */
  14.         return 0;
  15.     }
  16.     bh->b_bdev = I_BDEV(inode);
  17.     bh->b_blocknr = iblock;
  18.     set_buffer_mapped(bh);
  19.     return 0;
  20. }
复制代码


子文件系统(ext2,ext3,ext4 etc)
文件有hole 的概念,这里的hole是为了节省磁盘block空间
例如读取一个带hole 的inode
sudo dd if=/dev/sda of=./test.txt bs=4096 seek=10 count=1

  1. inode[23]:
  2.     i_mode 81a4
  3.     i_uid 0000
  4.     i_size 0000b000
  5.     i_atime 5a69873b
  6.     i_ctime 5a69873b
  7.     i_mtime 5a69873b
  8.     i_dtime 00000000
  9.     i_gid 0000
  10.     i_links_count 0001
  11.     i_blocks 00000008
  12.     i_flags 00000000
  13.     i_faddr 00000000
  14.     i_dir_acl 00000000
  15.     i_file_acl 00000000
  16.     i_generation 248582a1
  17.         i_block[ 0] 00000000
  18.         i_block[ 1] 00000000
  19.         i_block[ 2] 00000000
  20.         i_block[ 3] 00000000
  21.         i_block[ 4] 00000000
  22.         i_block[ 5] 00000000
  23.         i_block[ 6] 00000000
  24.         i_block[ 7] 00000000
  25.         i_block[ 8] 00000000
  26.         i_block[ 9] 00000000
  27.         [color=Red]i_block[10] 0008a200[/color]
  28.         i_block[11] 00000000
  29.         i_block[12] 00000000
  30.         i_block[13] 00000000
  31.         i_block[14] 00000000
复制代码

看看这里./test.txt虽然size大小等于44KB,但其实是占用了 0008a200H一个块,也就是文件实际大小只占用了4KB。

论坛徽章:
20
程序设计版块每日发帖之星
日期:2015-08-17 06:20:00程序设计版块每日发帖之星
日期:2016-07-16 06:20:00程序设计版块每日发帖之星
日期:2016-07-18 06:20:00每日论坛发贴之星
日期:2016-07-18 06:20:00黑曼巴
日期:2016-12-26 16:00:3215-16赛季CBA联赛之江苏
日期:2017-06-26 11:05:5615-16赛季CBA联赛之上海
日期:2017-07-21 18:12:5015-16赛季CBA联赛之青岛
日期:2017-09-04 17:32:0515-16赛季CBA联赛之吉林
日期:2018-03-26 10:02:16程序设计版块每日发帖之星
日期:2016-07-15 06:20:0015-16赛季CBA联赛之江苏
日期:2016-07-07 18:37:512015亚冠之萨济拖拉机
日期:2015-08-17 12:21:08
发表于 2018-02-02 21:27 |显示全部楼层
inode这个结构是用什么工具dump出来的?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP