diff options
Diffstat (limited to 'fs/ext4/inode.c')
-rw-r--r-- | fs/ext4/inode.c | 126 |
1 files changed, 102 insertions, 24 deletions
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 02bc8cbe7281..89b59cb7f9b8 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c | |||
@@ -346,6 +346,15 @@ void ext4_da_update_reserve_space(struct inode *inode, | |||
346 | used = ei->i_reserved_data_blocks; | 346 | used = ei->i_reserved_data_blocks; |
347 | } | 347 | } |
348 | 348 | ||
349 | if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) { | ||
350 | ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, allocated %d " | ||
351 | "with only %d reserved metadata blocks\n", __func__, | ||
352 | inode->i_ino, ei->i_allocated_meta_blocks, | ||
353 | ei->i_reserved_meta_blocks); | ||
354 | WARN_ON(1); | ||
355 | ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks; | ||
356 | } | ||
357 | |||
349 | /* Update per-inode reservations */ | 358 | /* Update per-inode reservations */ |
350 | ei->i_reserved_data_blocks -= used; | 359 | ei->i_reserved_data_blocks -= used; |
351 | ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; | 360 | ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; |
@@ -544,7 +553,8 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode, | |||
544 | * Try to see if we can get the block without requesting a new | 553 | * Try to see if we can get the block without requesting a new |
545 | * file system block. | 554 | * file system block. |
546 | */ | 555 | */ |
547 | down_read((&EXT4_I(inode)->i_data_sem)); | 556 | if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) |
557 | down_read((&EXT4_I(inode)->i_data_sem)); | ||
548 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { | 558 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
549 | retval = ext4_ext_map_blocks(handle, inode, map, flags & | 559 | retval = ext4_ext_map_blocks(handle, inode, map, flags & |
550 | EXT4_GET_BLOCKS_KEEP_SIZE); | 560 | EXT4_GET_BLOCKS_KEEP_SIZE); |
@@ -552,7 +562,8 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode, | |||
552 | retval = ext4_ind_map_blocks(handle, inode, map, flags & | 562 | retval = ext4_ind_map_blocks(handle, inode, map, flags & |
553 | EXT4_GET_BLOCKS_KEEP_SIZE); | 563 | EXT4_GET_BLOCKS_KEEP_SIZE); |
554 | } | 564 | } |
555 | up_read((&EXT4_I(inode)->i_data_sem)); | 565 | if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) |
566 | up_read((&EXT4_I(inode)->i_data_sem)); | ||
556 | 567 | ||
557 | if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { | 568 | if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { |
558 | int ret = check_block_validity(inode, map); | 569 | int ret = check_block_validity(inode, map); |
@@ -1171,6 +1182,17 @@ static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) | |||
1171 | struct ext4_inode_info *ei = EXT4_I(inode); | 1182 | struct ext4_inode_info *ei = EXT4_I(inode); |
1172 | unsigned int md_needed; | 1183 | unsigned int md_needed; |
1173 | int ret; | 1184 | int ret; |
1185 | ext4_lblk_t save_last_lblock; | ||
1186 | int save_len; | ||
1187 | |||
1188 | /* | ||
1189 | * We will charge metadata quota at writeout time; this saves | ||
1190 | * us from metadata over-estimation, though we may go over by | ||
1191 | * a small amount in the end. Here we just reserve for data. | ||
1192 | */ | ||
1193 | ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); | ||
1194 | if (ret) | ||
1195 | return ret; | ||
1174 | 1196 | ||
1175 | /* | 1197 | /* |
1176 | * recalculate the amount of metadata blocks to reserve | 1198 | * recalculate the amount of metadata blocks to reserve |
@@ -1179,32 +1201,31 @@ static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) | |||
1179 | */ | 1201 | */ |
1180 | repeat: | 1202 | repeat: |
1181 | spin_lock(&ei->i_block_reservation_lock); | 1203 | spin_lock(&ei->i_block_reservation_lock); |
1204 | /* | ||
1205 | * ext4_calc_metadata_amount() has side effects, which we have | ||
1206 | * to be prepared undo if we fail to claim space. | ||
1207 | */ | ||
1208 | save_len = ei->i_da_metadata_calc_len; | ||
1209 | save_last_lblock = ei->i_da_metadata_calc_last_lblock; | ||
1182 | md_needed = EXT4_NUM_B2C(sbi, | 1210 | md_needed = EXT4_NUM_B2C(sbi, |
1183 | ext4_calc_metadata_amount(inode, lblock)); | 1211 | ext4_calc_metadata_amount(inode, lblock)); |
1184 | trace_ext4_da_reserve_space(inode, md_needed); | 1212 | trace_ext4_da_reserve_space(inode, md_needed); |
1185 | spin_unlock(&ei->i_block_reservation_lock); | ||
1186 | 1213 | ||
1187 | /* | 1214 | /* |
1188 | * We will charge metadata quota at writeout time; this saves | ||
1189 | * us from metadata over-estimation, though we may go over by | ||
1190 | * a small amount in the end. Here we just reserve for data. | ||
1191 | */ | ||
1192 | ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); | ||
1193 | if (ret) | ||
1194 | return ret; | ||
1195 | /* | ||
1196 | * We do still charge estimated metadata to the sb though; | 1215 | * We do still charge estimated metadata to the sb though; |
1197 | * we cannot afford to run out of free blocks. | 1216 | * we cannot afford to run out of free blocks. |
1198 | */ | 1217 | */ |
1199 | if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { | 1218 | if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { |
1200 | dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); | 1219 | ei->i_da_metadata_calc_len = save_len; |
1220 | ei->i_da_metadata_calc_last_lblock = save_last_lblock; | ||
1221 | spin_unlock(&ei->i_block_reservation_lock); | ||
1201 | if (ext4_should_retry_alloc(inode->i_sb, &retries)) { | 1222 | if (ext4_should_retry_alloc(inode->i_sb, &retries)) { |
1202 | yield(); | 1223 | yield(); |
1203 | goto repeat; | 1224 | goto repeat; |
1204 | } | 1225 | } |
1226 | dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); | ||
1205 | return -ENOSPC; | 1227 | return -ENOSPC; |
1206 | } | 1228 | } |
1207 | spin_lock(&ei->i_block_reservation_lock); | ||
1208 | ei->i_reserved_data_blocks++; | 1229 | ei->i_reserved_data_blocks++; |
1209 | ei->i_reserved_meta_blocks += md_needed; | 1230 | ei->i_reserved_meta_blocks += md_needed; |
1210 | spin_unlock(&ei->i_block_reservation_lock); | 1231 | spin_unlock(&ei->i_block_reservation_lock); |
@@ -2818,6 +2839,32 @@ static int ext4_get_block_write(struct inode *inode, sector_t iblock, | |||
2818 | EXT4_GET_BLOCKS_IO_CREATE_EXT); | 2839 | EXT4_GET_BLOCKS_IO_CREATE_EXT); |
2819 | } | 2840 | } |
2820 | 2841 | ||
2842 | static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, | ||
2843 | struct buffer_head *bh_result, int flags) | ||
2844 | { | ||
2845 | handle_t *handle = ext4_journal_current_handle(); | ||
2846 | struct ext4_map_blocks map; | ||
2847 | int ret = 0; | ||
2848 | |||
2849 | ext4_debug("ext4_get_block_write_nolock: inode %lu, flag %d\n", | ||
2850 | inode->i_ino, flags); | ||
2851 | |||
2852 | flags = EXT4_GET_BLOCKS_NO_LOCK; | ||
2853 | |||
2854 | map.m_lblk = iblock; | ||
2855 | map.m_len = bh_result->b_size >> inode->i_blkbits; | ||
2856 | |||
2857 | ret = ext4_map_blocks(handle, inode, &map, flags); | ||
2858 | if (ret > 0) { | ||
2859 | map_bh(bh_result, inode->i_sb, map.m_pblk); | ||
2860 | bh_result->b_state = (bh_result->b_state & ~EXT4_MAP_FLAGS) | | ||
2861 | map.m_flags; | ||
2862 | bh_result->b_size = inode->i_sb->s_blocksize * map.m_len; | ||
2863 | ret = 0; | ||
2864 | } | ||
2865 | return ret; | ||
2866 | } | ||
2867 | |||
2821 | static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, | 2868 | static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, |
2822 | ssize_t size, void *private, int ret, | 2869 | ssize_t size, void *private, int ret, |
2823 | bool is_async) | 2870 | bool is_async) |
@@ -2966,6 +3013,18 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, | |||
2966 | 3013 | ||
2967 | loff_t final_size = offset + count; | 3014 | loff_t final_size = offset + count; |
2968 | if (rw == WRITE && final_size <= inode->i_size) { | 3015 | if (rw == WRITE && final_size <= inode->i_size) { |
3016 | int overwrite = 0; | ||
3017 | |||
3018 | BUG_ON(iocb->private == NULL); | ||
3019 | |||
3020 | /* If we do a overwrite dio, i_mutex locking can be released */ | ||
3021 | overwrite = *((int *)iocb->private); | ||
3022 | |||
3023 | if (overwrite) { | ||
3024 | down_read(&EXT4_I(inode)->i_data_sem); | ||
3025 | mutex_unlock(&inode->i_mutex); | ||
3026 | } | ||
3027 | |||
2969 | /* | 3028 | /* |
2970 | * We could direct write to holes and fallocate. | 3029 | * We could direct write to holes and fallocate. |
2971 | * | 3030 | * |
@@ -2991,8 +3050,10 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, | |||
2991 | if (!is_sync_kiocb(iocb)) { | 3050 | if (!is_sync_kiocb(iocb)) { |
2992 | ext4_io_end_t *io_end = | 3051 | ext4_io_end_t *io_end = |
2993 | ext4_init_io_end(inode, GFP_NOFS); | 3052 | ext4_init_io_end(inode, GFP_NOFS); |
2994 | if (!io_end) | 3053 | if (!io_end) { |
2995 | return -ENOMEM; | 3054 | ret = -ENOMEM; |
3055 | goto retake_lock; | ||
3056 | } | ||
2996 | io_end->flag |= EXT4_IO_END_DIRECT; | 3057 | io_end->flag |= EXT4_IO_END_DIRECT; |
2997 | iocb->private = io_end; | 3058 | iocb->private = io_end; |
2998 | /* | 3059 | /* |
@@ -3005,13 +3066,22 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, | |||
3005 | EXT4_I(inode)->cur_aio_dio = iocb->private; | 3066 | EXT4_I(inode)->cur_aio_dio = iocb->private; |
3006 | } | 3067 | } |
3007 | 3068 | ||
3008 | ret = __blockdev_direct_IO(rw, iocb, inode, | 3069 | if (overwrite) |
3009 | inode->i_sb->s_bdev, iov, | 3070 | ret = __blockdev_direct_IO(rw, iocb, inode, |
3010 | offset, nr_segs, | 3071 | inode->i_sb->s_bdev, iov, |
3011 | ext4_get_block_write, | 3072 | offset, nr_segs, |
3012 | ext4_end_io_dio, | 3073 | ext4_get_block_write_nolock, |
3013 | NULL, | 3074 | ext4_end_io_dio, |
3014 | DIO_LOCKING); | 3075 | NULL, |
3076 | 0); | ||
3077 | else | ||
3078 | ret = __blockdev_direct_IO(rw, iocb, inode, | ||
3079 | inode->i_sb->s_bdev, iov, | ||
3080 | offset, nr_segs, | ||
3081 | ext4_get_block_write, | ||
3082 | ext4_end_io_dio, | ||
3083 | NULL, | ||
3084 | DIO_LOCKING); | ||
3015 | if (iocb->private) | 3085 | if (iocb->private) |
3016 | EXT4_I(inode)->cur_aio_dio = NULL; | 3086 | EXT4_I(inode)->cur_aio_dio = NULL; |
3017 | /* | 3087 | /* |
@@ -3031,7 +3101,7 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, | |||
3031 | if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { | 3101 | if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { |
3032 | ext4_free_io_end(iocb->private); | 3102 | ext4_free_io_end(iocb->private); |
3033 | iocb->private = NULL; | 3103 | iocb->private = NULL; |
3034 | } else if (ret > 0 && ext4_test_inode_state(inode, | 3104 | } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, |
3035 | EXT4_STATE_DIO_UNWRITTEN)) { | 3105 | EXT4_STATE_DIO_UNWRITTEN)) { |
3036 | int err; | 3106 | int err; |
3037 | /* | 3107 | /* |
@@ -3044,6 +3114,14 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, | |||
3044 | ret = err; | 3114 | ret = err; |
3045 | ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); | 3115 | ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); |
3046 | } | 3116 | } |
3117 | |||
3118 | retake_lock: | ||
3119 | /* take i_mutex locking again if we do a ovewrite dio */ | ||
3120 | if (overwrite) { | ||
3121 | up_read(&EXT4_I(inode)->i_data_sem); | ||
3122 | mutex_lock(&inode->i_mutex); | ||
3123 | } | ||
3124 | |||
3047 | return ret; | 3125 | return ret; |
3048 | } | 3126 | } |
3049 | 3127 | ||
@@ -4034,7 +4112,7 @@ static int ext4_do_update_inode(handle_t *handle, | |||
4034 | EXT4_SET_RO_COMPAT_FEATURE(sb, | 4112 | EXT4_SET_RO_COMPAT_FEATURE(sb, |
4035 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE); | 4113 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE); |
4036 | ext4_handle_sync(handle); | 4114 | ext4_handle_sync(handle); |
4037 | err = ext4_handle_dirty_super_now(handle, sb); | 4115 | err = ext4_handle_dirty_super(handle, sb); |
4038 | } | 4116 | } |
4039 | } | 4117 | } |
4040 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); | 4118 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); |