inode.c (ea77f7a2e8561012cf100c530170f12351c3b53e) inode.c (aa38572954ade525817fe88c54faebf85e5a61c0)
1/*
2 * linux/fs/ext4/inode.c
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *

--- 625 unchanged lines hidden (view full) ---

634 * the first direct block of this branch. That's the
635 * minimum number of blocks need to allocate(required)
636 */
637 /* first we try to allocate the indirect blocks */
638 target = indirect_blks;
639 while (target > 0) {
640 count = target;
641 /* allocating blocks for indirect blocks and direct blocks */
1/*
2 * linux/fs/ext4/inode.c
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *

--- 625 unchanged lines hidden (view full) ---

634 * the first direct block of this branch. That's the
635 * minimum number of blocks need to allocate(required)
636 */
637 /* first we try to allocate the indirect blocks */
638 target = indirect_blks;
639 while (target > 0) {
640 count = target;
641 /* allocating blocks for indirect blocks and direct blocks */
642 current_block = ext4_new_meta_blocks(handle, inode,
643 goal, &count, err);
642 current_block = ext4_new_meta_blocks(handle, inode, goal,
643 0, &count, err);
644 if (*err)
645 goto failed_out;
646
647 if (unlikely(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS)) {
648 EXT4_ERROR_INODE(inode,
649 "current_block %llu + count %lu > %d!",
650 current_block, count,
651 EXT4_MAX_BLOCK_FILE_PHYS);

--- 1273 unchanged lines hidden (view full) ---

1925 */
1926 ret = dquot_reserve_block(inode, 1);
1927 if (ret)
1928 return ret;
1929 /*
1930 * We do still charge estimated metadata to the sb though;
1931 * we cannot afford to run out of free blocks.
1932 */
644 if (*err)
645 goto failed_out;
646
647 if (unlikely(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS)) {
648 EXT4_ERROR_INODE(inode,
649 "current_block %llu + count %lu > %d!",
650 current_block, count,
651 EXT4_MAX_BLOCK_FILE_PHYS);

--- 1273 unchanged lines hidden (view full) ---

1925 */
1926 ret = dquot_reserve_block(inode, 1);
1927 if (ret)
1928 return ret;
1929 /*
1930 * We do still charge estimated metadata to the sb though;
1931 * we cannot afford to run out of free blocks.
1932 */
1933 if (ext4_claim_free_blocks(sbi, md_needed + 1)) {
1933 if (ext4_claim_free_blocks(sbi, md_needed + 1, 0)) {
1934 dquot_release_reservation_block(inode, 1);
1935 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1936 yield();
1937 goto repeat;
1938 }
1939 return -ENOSPC;
1940 }
1941 spin_lock(&ei->i_block_reservation_lock);

--- 849 unchanged lines hidden (view full) ---

2791 if (!PageDirty(page) ||
2792 (PageWriteback(page) &&
2793 (wbc->sync_mode == WB_SYNC_NONE)) ||
2794 unlikely(page->mapping != mapping)) {
2795 unlock_page(page);
2796 continue;
2797 }
2798
1934 dquot_release_reservation_block(inode, 1);
1935 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1936 yield();
1937 goto repeat;
1938 }
1939 return -ENOSPC;
1940 }
1941 spin_lock(&ei->i_block_reservation_lock);

--- 849 unchanged lines hidden (view full) ---

2791 if (!PageDirty(page) ||
2792 (PageWriteback(page) &&
2793 (wbc->sync_mode == WB_SYNC_NONE)) ||
2794 unlikely(page->mapping != mapping)) {
2795 unlock_page(page);
2796 continue;
2797 }
2798
2799 if (PageWriteback(page))
2800 wait_on_page_writeback(page);
2801
2799 wait_on_page_writeback(page);
2802 BUG_ON(PageWriteback(page));
2803
2804 if (mpd->next_page != page->index)
2805 mpd->first_page = page->index;
2806 mpd->next_page = page->index + 1;
2807 logical = (sector_t) page->index <<
2808 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2809

--- 698 unchanged lines hidden (view full) ---

3508 offset, nr_segs,
3509 ext4_get_block, NULL);
3510
3511 if (unlikely((rw & WRITE) && ret < 0)) {
3512 loff_t isize = i_size_read(inode);
3513 loff_t end = offset + iov_length(iov, nr_segs);
3514
3515 if (end > isize)
2800 BUG_ON(PageWriteback(page));
2801
2802 if (mpd->next_page != page->index)
2803 mpd->first_page = page->index;
2804 mpd->next_page = page->index + 1;
2805 logical = (sector_t) page->index <<
2806 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2807

--- 698 unchanged lines hidden (view full) ---

3506 offset, nr_segs,
3507 ext4_get_block, NULL);
3508
3509 if (unlikely((rw & WRITE) && ret < 0)) {
3510 loff_t isize = i_size_read(inode);
3511 loff_t end = offset + iov_length(iov, nr_segs);
3512
3513 if (end > isize)
3516 vmtruncate(inode, isize);
3514 ext4_truncate_failed_write(inode);
3517 }
3518 }
3519 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3520 goto retry;
3521
3522 if (orphan) {
3523 int err;
3524

--- 386 unchanged lines hidden (view full) ---

3911 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3912 * up to the end of the block which corresponds to `from'.
3913 * This required during truncate. We need to physically zero the tail end
3914 * of that block so it doesn't yield old data if the file is later grown.
3915 */
3916int ext4_block_truncate_page(handle_t *handle,
3917 struct address_space *mapping, loff_t from)
3918{
3515 }
3516 }
3517 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3518 goto retry;
3519
3520 if (orphan) {
3521 int err;
3522

--- 386 unchanged lines hidden (view full) ---

3909 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3910 * up to the end of the block which corresponds to `from'.
3911 * This required during truncate. We need to physically zero the tail end
3912 * of that block so it doesn't yield old data if the file is later grown.
3913 */
3914int ext4_block_truncate_page(handle_t *handle,
3915 struct address_space *mapping, loff_t from)
3916{
3917 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3918 unsigned length;
3919 unsigned blocksize;
3920 struct inode *inode = mapping->host;
3921
3922 blocksize = inode->i_sb->s_blocksize;
3923 length = blocksize - (offset & (blocksize - 1));
3924
3925 return ext4_block_zero_page_range(handle, mapping, from, length);
3926}
3927
3928/*
3929 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3930 * starting from file offset 'from'. The range to be zero'd must
3931 * be contained with in one block. If the specified range exceeds
3932 * the end of the block it will be shortened to end of the block
3933 * that cooresponds to 'from'
3934 */
3935int ext4_block_zero_page_range(handle_t *handle,
3936 struct address_space *mapping, loff_t from, loff_t length)
3937{
3919 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
3920 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3938 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
3939 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3921 unsigned blocksize, length, pos;
3940 unsigned blocksize, max, pos;
3922 ext4_lblk_t iblock;
3923 struct inode *inode = mapping->host;
3924 struct buffer_head *bh;
3925 struct page *page;
3926 int err = 0;
3927
3928 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3929 mapping_gfp_mask(mapping) & ~__GFP_FS);
3930 if (!page)
3931 return -EINVAL;
3932
3933 blocksize = inode->i_sb->s_blocksize;
3941 ext4_lblk_t iblock;
3942 struct inode *inode = mapping->host;
3943 struct buffer_head *bh;
3944 struct page *page;
3945 int err = 0;
3946
3947 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3948 mapping_gfp_mask(mapping) & ~__GFP_FS);
3949 if (!page)
3950 return -EINVAL;
3951
3952 blocksize = inode->i_sb->s_blocksize;
3934 length = blocksize - (offset & (blocksize - 1));
3953 max = blocksize - (offset & (blocksize - 1));
3954
3955 /*
3956 * correct length if it does not fall between
3957 * 'from' and the end of the block
3958 */
3959 if (length > max || length < 0)
3960 length = max;
3961
3935 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3936
3937 if (!page_has_buffers(page))
3938 create_empty_buffers(page, blocksize, 0);
3939
3940 /* Find the buffer that contains "offset" */
3941 bh = page_buffers(page);
3942 pos = blocksize;

--- 432 unchanged lines hidden (view full) ---

4375 /* We have reached the bottom of the tree. */
4376 BUFFER_TRACE(parent_bh, "free data blocks");
4377 ext4_free_data(handle, inode, parent_bh, first, last);
4378 }
4379}
4380
4381int ext4_can_truncate(struct inode *inode)
4382{
3962 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3963
3964 if (!page_has_buffers(page))
3965 create_empty_buffers(page, blocksize, 0);
3966
3967 /* Find the buffer that contains "offset" */
3968 bh = page_buffers(page);
3969 pos = blocksize;

--- 432 unchanged lines hidden (view full) ---

4402 /* We have reached the bottom of the tree. */
4403 BUFFER_TRACE(parent_bh, "free data blocks");
4404 ext4_free_data(handle, inode, parent_bh, first, last);
4405 }
4406}
4407
4408int ext4_can_truncate(struct inode *inode)
4409{
4383 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4384 return 0;
4385 if (S_ISREG(inode->i_mode))
4386 return 1;
4387 if (S_ISDIR(inode->i_mode))
4388 return 1;
4389 if (S_ISLNK(inode->i_mode))
4390 return !ext4_inode_is_fast_symlink(inode);
4391 return 0;
4392}
4393
4394/*
4410 if (S_ISREG(inode->i_mode))
4411 return 1;
4412 if (S_ISDIR(inode->i_mode))
4413 return 1;
4414 if (S_ISLNK(inode->i_mode))
4415 return !ext4_inode_is_fast_symlink(inode);
4416 return 0;
4417}
4418
4419/*
4420 * ext4_punch_hole: punches a hole in a file by releaseing the blocks
4421 * associated with the given offset and length
4422 *
4423 * @inode: File inode
4424 * @offset: The offset where the hole will begin
4425 * @len: The length of the hole
4426 *
4427 * Returns: 0 on sucess or negative on failure
4428 */
4429
4430int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
4431{
4432 struct inode *inode = file->f_path.dentry->d_inode;
4433 if (!S_ISREG(inode->i_mode))
4434 return -ENOTSUPP;
4435
4436 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
4437 /* TODO: Add support for non extent hole punching */
4438 return -ENOTSUPP;
4439 }
4440
4441 return ext4_ext_punch_hole(file, offset, length);
4442}
4443
4444/*
4395 * ext4_truncate()
4396 *
4397 * We block out ext4_get_block() block instantiations across the entire
4398 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
4399 * simultaneously on behalf of the same inode.
4400 *
4401 * As we work through the truncate and commmit bits of it to the journal there
4402 * is one core, guiding principle: the file's tree must always be consistent on

--- 209 unchanged lines hidden (view full) ---

4612 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4613 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4614 if (!gdp)
4615 return -EIO;
4616
4617 /*
4618 * Figure out the offset within the block group inode table
4619 */
4445 * ext4_truncate()
4446 *
4447 * We block out ext4_get_block() block instantiations across the entire
4448 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
4449 * simultaneously on behalf of the same inode.
4450 *
4451 * As we work through the truncate and commmit bits of it to the journal there
4452 * is one core, guiding principle: the file's tree must always be consistent on

--- 209 unchanged lines hidden (view full) ---

4662 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4663 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4664 if (!gdp)
4665 return -EIO;
4666
4667 /*
4668 * Figure out the offset within the block group inode table
4669 */
4620 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
4670 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
4621 inode_offset = ((inode->i_ino - 1) %
4622 EXT4_INODES_PER_GROUP(sb));
4623 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4624 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4625
4626 bh = sb_getblk(sb, block);
4627 if (!bh) {
4628 EXT4_ERROR_INODE_BLOCK(inode, block,

--- 677 unchanged lines hidden (view full) ---

5306
5307 if (attr->ia_size > sbi->s_bitmap_maxbytes)
5308 return -EFBIG;
5309 }
5310 }
5311
5312 if (S_ISREG(inode->i_mode) &&
5313 attr->ia_valid & ATTR_SIZE &&
4671 inode_offset = ((inode->i_ino - 1) %
4672 EXT4_INODES_PER_GROUP(sb));
4673 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4674 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4675
4676 bh = sb_getblk(sb, block);
4677 if (!bh) {
4678 EXT4_ERROR_INODE_BLOCK(inode, block,

--- 677 unchanged lines hidden (view full) ---

5356
5357 if (attr->ia_size > sbi->s_bitmap_maxbytes)
5358 return -EFBIG;
5359 }
5360 }
5361
5362 if (S_ISREG(inode->i_mode) &&
5363 attr->ia_valid & ATTR_SIZE &&
5314 (attr->ia_size < inode->i_size ||
5315 (ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)))) {
5364 (attr->ia_size < inode->i_size)) {
5316 handle_t *handle;
5317
5318 handle = ext4_journal_start(inode, 3);
5319 if (IS_ERR(handle)) {
5320 error = PTR_ERR(handle);
5321 goto err_out;
5322 }
5323 if (ext4_handle_valid(handle)) {

--- 17 unchanged lines hidden (view full) ---

5341 goto err_out;
5342 }
5343 ext4_orphan_del(handle, inode);
5344 orphan = 0;
5345 ext4_journal_stop(handle);
5346 goto err_out;
5347 }
5348 }
5365 handle_t *handle;
5366
5367 handle = ext4_journal_start(inode, 3);
5368 if (IS_ERR(handle)) {
5369 error = PTR_ERR(handle);
5370 goto err_out;
5371 }
5372 if (ext4_handle_valid(handle)) {

--- 17 unchanged lines hidden (view full) ---

5390 goto err_out;
5391 }
5392 ext4_orphan_del(handle, inode);
5393 orphan = 0;
5394 ext4_journal_stop(handle);
5395 goto err_out;
5396 }
5397 }
5349 /* ext4_truncate will clear the flag */
5350 if ((ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)))
5398 }
5399
5400 if (attr->ia_valid & ATTR_SIZE) {
5401 if (attr->ia_size != i_size_read(inode)) {
5402 truncate_setsize(inode, attr->ia_size);
5351 ext4_truncate(inode);
5403 ext4_truncate(inode);
5404 } else if (ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
5405 ext4_truncate(inode);
5352 }
5353
5406 }
5407
5354 if ((attr->ia_valid & ATTR_SIZE) &&
5355 attr->ia_size != i_size_read(inode))
5356 rc = vmtruncate(inode, attr->ia_size);
5357
5358 if (!rc) {
5359 setattr_copy(inode, attr);
5360 mark_inode_dirty(inode);
5361 }
5362
5363 /*
5364 * If the call to ext4_truncate failed to get a transaction handle at
5365 * all, we need to clean up the in-core orphan list manually.

--- 312 unchanged lines hidden (view full) ---

5678 *
5679 * Also, dquot_alloc_block() will always dirty the inode when blocks
5680 * are allocated to the file.
5681 *
5682 * If the inode is marked synchronous, we don't honour that here - doing
5683 * so would cause a commit on atime updates, which we don't bother doing.
5684 * We handle synchronous inodes at the highest possible level.
5685 */
5408 if (!rc) {
5409 setattr_copy(inode, attr);
5410 mark_inode_dirty(inode);
5411 }
5412
5413 /*
5414 * If the call to ext4_truncate failed to get a transaction handle at
5415 * all, we need to clean up the in-core orphan list manually.

--- 312 unchanged lines hidden (view full) ---

5728 *
5729 * Also, dquot_alloc_block() will always dirty the inode when blocks
5730 * are allocated to the file.
5731 *
5732 * If the inode is marked synchronous, we don't honour that here - doing
5733 * so would cause a commit on atime updates, which we don't bother doing.
5734 * We handle synchronous inodes at the highest possible level.
5735 */
5686void ext4_dirty_inode(struct inode *inode)
5736void ext4_dirty_inode(struct inode *inode, int flags)
5687{
5688 handle_t *handle;
5689
5690 handle = ext4_journal_start(inode, 2);
5691 if (IS_ERR(handle))
5692 goto out;
5693
5694 ext4_mark_inode_dirty(handle, inode);

--- 111 unchanged lines hidden (view full) ---

5806 down_read(&inode->i_alloc_sem);
5807 size = i_size_read(inode);
5808 if (page->mapping != mapping || size <= page_offset(page)
5809 || !PageUptodate(page)) {
5810 /* page got truncated from under us? */
5811 goto out_unlock;
5812 }
5813 ret = 0;
5737{
5738 handle_t *handle;
5739
5740 handle = ext4_journal_start(inode, 2);
5741 if (IS_ERR(handle))
5742 goto out;
5743
5744 ext4_mark_inode_dirty(handle, inode);

--- 111 unchanged lines hidden (view full) ---

5856 down_read(&inode->i_alloc_sem);
5857 size = i_size_read(inode);
5858 if (page->mapping != mapping || size <= page_offset(page)
5859 || !PageUptodate(page)) {
5860 /* page got truncated from under us? */
5861 goto out_unlock;
5862 }
5863 ret = 0;
5814 if (PageMappedToDisk(page))
5815 goto out_unlock;
5816
5864
5865 lock_page(page);
5866 wait_on_page_writeback(page);
5867 if (PageMappedToDisk(page)) {
5868 up_read(&inode->i_alloc_sem);
5869 return VM_FAULT_LOCKED;
5870 }
5871
5817 if (page->index == size >> PAGE_CACHE_SHIFT)
5818 len = size & ~PAGE_CACHE_MASK;
5819 else
5820 len = PAGE_CACHE_SIZE;
5821
5872 if (page->index == size >> PAGE_CACHE_SHIFT)
5873 len = size & ~PAGE_CACHE_MASK;
5874 else
5875 len = PAGE_CACHE_SIZE;
5876
5822 lock_page(page);
5823 /*
5824 * return if we have all the buffers mapped. This avoid
5825 * the need to call write_begin/write_end which does a
5826 * journal_start/journal_stop which can block and take
5827 * long time
5828 */
5829 if (page_has_buffers(page)) {
5830 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
5831 ext4_bh_unmapped)) {
5877 /*
5878 * return if we have all the buffers mapped. This avoid
5879 * the need to call write_begin/write_end which does a
5880 * journal_start/journal_stop which can block and take
5881 * long time
5882 */
5883 if (page_has_buffers(page)) {
5884 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
5885 ext4_bh_unmapped)) {
5832 unlock_page(page);
5833 goto out_unlock;
5886 up_read(&inode->i_alloc_sem);
5887 return VM_FAULT_LOCKED;
5834 }
5835 }
5836 unlock_page(page);
5837 /*
5838 * OK, we need to fill the hole... Do write_begin write_end
5839 * to do block allocation/reservation.We are not holding
5840 * inode.i__mutex here. That allow * parallel write_begin,
5841 * write_end call. lock_page prevent this from happening
5842 * on the same page though
5843 */
5844 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
5845 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
5846 if (ret < 0)
5847 goto out_unlock;
5848 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
5849 len, len, page, fsdata);
5850 if (ret < 0)
5851 goto out_unlock;
5852 ret = 0;
5888 }
5889 }
5890 unlock_page(page);
5891 /*
5892 * OK, we need to fill the hole... Do write_begin write_end
5893 * to do block allocation/reservation.We are not holding
5894 * inode.i__mutex here. That allow * parallel write_begin,
5895 * write_end call. lock_page prevent this from happening
5896 * on the same page though
5897 */
5898 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
5899 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
5900 if (ret < 0)
5901 goto out_unlock;
5902 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
5903 len, len, page, fsdata);
5904 if (ret < 0)
5905 goto out_unlock;
5906 ret = 0;
5907
5908 /*
5909 * write_begin/end might have created a dirty page and someone
5910 * could wander in and start the IO. Make sure that hasn't
5911 * happened.
5912 */
5913 lock_page(page);
5914 wait_on_page_writeback(page);
5915 up_read(&inode->i_alloc_sem);
5916 return VM_FAULT_LOCKED;
5853out_unlock:
5854 if (ret)
5855 ret = VM_FAULT_SIGBUS;
5856 up_read(&inode->i_alloc_sem);
5857 return ret;
5858}
5917out_unlock:
5918 if (ret)
5919 ret = VM_FAULT_SIGBUS;
5920 up_read(&inode->i_alloc_sem);
5921 return ret;
5922}