2 * linux/fs/ext4/inode.c
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)
11 * linux/fs/minix/inode.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * 64-bit file support on 64-bit platforms by Jakub Jelinek
16 * (jj@sunsite.ms.mff.cuni.cz)
18 * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
22 #include <linux/time.h>
23 #include <linux/jbd2.h>
24 #include <linux/highuid.h>
25 #include <linux/pagemap.h>
26 #include <linux/quotaops.h>
27 #include <linux/string.h>
28 #include <linux/buffer_head.h>
29 #include <linux/writeback.h>
30 #include <linux/pagevec.h>
31 #include <linux/mpage.h>
32 #include <linux/namei.h>
33 #include <linux/uio.h>
34 #include <linux/bio.h>
35 #include <linux/workqueue.h>
36 #include <linux/kernel.h>
37 #include <linux/printk.h>
38 #include <linux/slab.h>
39 #include <linux/ratelimit.h>
40 #include <linux/aio.h>
41 #include <linux/bitops.h>
43 #include "ext4_jbd2.h"
48 #include <trace/events/ext4.h>
49 #include <linux/blkdev.h>
51 #define MPAGE_DA_EXTENT_TAIL 0x01
53 static __u32
ext4_inode_csum(struct inode
*inode
, struct ext4_inode
*raw
,
54 struct ext4_inode_info
*ei
)
56 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
59 int offset
= offsetof(struct ext4_inode
, i_checksum_lo
);
60 unsigned int csum_size
= sizeof(dummy_csum
);
62 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)raw
, offset
);
63 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
, csum_size
);
65 csum
= ext4_chksum(sbi
, csum
, (__u8
*)raw
+ offset
,
66 EXT4_GOOD_OLD_INODE_SIZE
- offset
);
68 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
69 offset
= offsetof(struct ext4_inode
, i_checksum_hi
);
70 csum
= ext4_chksum(sbi
, csum
, (__u8
*)raw
+
71 EXT4_GOOD_OLD_INODE_SIZE
,
72 offset
- EXT4_GOOD_OLD_INODE_SIZE
);
73 if (EXT4_FITS_IN_INODE(raw
, ei
, i_checksum_hi
)) {
74 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
,
78 csum
= ext4_chksum(sbi
, csum
, (__u8
*)raw
+ offset
,
79 EXT4_INODE_SIZE(inode
->i_sb
) - offset
);
85 static int ext4_inode_csum_verify(struct inode
*inode
, struct ext4_inode
*raw
,
86 struct ext4_inode_info
*ei
)
88 __u32 provided
, calculated
;
90 if (EXT4_SB(inode
->i_sb
)->s_es
->s_creator_os
!=
91 cpu_to_le32(EXT4_OS_LINUX
) ||
92 !EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
93 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
96 provided
= le16_to_cpu(raw
->i_checksum_lo
);
97 calculated
= ext4_inode_csum(inode
, raw
, ei
);
98 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
&&
99 EXT4_FITS_IN_INODE(raw
, ei
, i_checksum_hi
))
100 provided
|= ((__u32
)le16_to_cpu(raw
->i_checksum_hi
)) << 16;
102 calculated
&= 0xFFFF;
104 return provided
== calculated
;
107 static void ext4_inode_csum_set(struct inode
*inode
, struct ext4_inode
*raw
,
108 struct ext4_inode_info
*ei
)
112 if (EXT4_SB(inode
->i_sb
)->s_es
->s_creator_os
!=
113 cpu_to_le32(EXT4_OS_LINUX
) ||
114 !EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
115 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
118 csum
= ext4_inode_csum(inode
, raw
, ei
);
119 raw
->i_checksum_lo
= cpu_to_le16(csum
& 0xFFFF);
120 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
&&
121 EXT4_FITS_IN_INODE(raw
, ei
, i_checksum_hi
))
122 raw
->i_checksum_hi
= cpu_to_le16(csum
>> 16);
125 static inline int ext4_begin_ordered_truncate(struct inode
*inode
,
128 trace_ext4_begin_ordered_truncate(inode
, new_size
);
130 * If jinode is zero, then we never opened the file for
131 * writing, so there's no need to call
132 * jbd2_journal_begin_ordered_truncate() since there's no
133 * outstanding writes we need to flush.
135 if (!EXT4_I(inode
)->jinode
)
137 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode
),
138 EXT4_I(inode
)->jinode
,
142 static void ext4_invalidatepage(struct page
*page
, unsigned long offset
);
143 static int __ext4_journalled_writepage(struct page
*page
, unsigned int len
);
144 static int ext4_bh_delay_or_unwritten(handle_t
*handle
, struct buffer_head
*bh
);
145 static int ext4_discard_partial_page_buffers_no_lock(handle_t
*handle
,
146 struct inode
*inode
, struct page
*page
, loff_t from
,
147 loff_t length
, int flags
);
150 * Test whether an inode is a fast symlink.
152 static int ext4_inode_is_fast_symlink(struct inode
*inode
)
154 int ea_blocks
= EXT4_I(inode
)->i_file_acl
?
155 (inode
->i_sb
->s_blocksize
>> 9) : 0;
157 return (S_ISLNK(inode
->i_mode
) && inode
->i_blocks
- ea_blocks
== 0);
161 * Restart the transaction associated with *handle. This does a commit,
162 * so before we call here everything must be consistently dirtied against
165 int ext4_truncate_restart_trans(handle_t
*handle
, struct inode
*inode
,
171 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
172 * moment, get_block can be called only for blocks inside i_size since
173 * page cache has been already dropped and writes are blocked by
174 * i_mutex. So we can safely drop the i_data_sem here.
176 BUG_ON(EXT4_JOURNAL(inode
) == NULL
);
177 jbd_debug(2, "restarting handle %p\n", handle
);
178 up_write(&EXT4_I(inode
)->i_data_sem
);
179 ret
= ext4_journal_restart(handle
, nblocks
);
180 down_write(&EXT4_I(inode
)->i_data_sem
);
181 ext4_discard_preallocations(inode
);
187 * Called at the last iput() if i_nlink is zero.
189 void ext4_evict_inode(struct inode
*inode
)
194 trace_ext4_evict_inode(inode
);
196 if (inode
->i_nlink
) {
198 * When journalling data dirty buffers are tracked only in the
199 * journal. So although mm thinks everything is clean and
200 * ready for reaping the inode might still have some pages to
201 * write in the running transaction or waiting to be
202 * checkpointed. Thus calling jbd2_journal_invalidatepage()
203 * (via truncate_inode_pages()) to discard these buffers can
204 * cause data loss. Also even if we did not discard these
205 * buffers, we would have no way to find them after the inode
206 * is reaped and thus user could see stale data if he tries to
207 * read them before the transaction is checkpointed. So be
208 * careful and force everything to disk here... We use
209 * ei->i_datasync_tid to store the newest transaction
210 * containing inode's data.
212 * Note that directories do not have this problem because they
213 * don't use page cache.
215 if (inode
->i_ino
!= EXT4_JOURNAL_INO
&&
216 ext4_should_journal_data(inode
) &&
217 (S_ISLNK(inode
->i_mode
) || S_ISREG(inode
->i_mode
))) {
218 journal_t
*journal
= EXT4_SB(inode
->i_sb
)->s_journal
;
219 tid_t commit_tid
= EXT4_I(inode
)->i_datasync_tid
;
221 jbd2_complete_transaction(journal
, commit_tid
);
222 filemap_write_and_wait(&inode
->i_data
);
224 truncate_inode_pages(&inode
->i_data
, 0);
225 ext4_ioend_shutdown(inode
);
229 if (!is_bad_inode(inode
))
230 dquot_initialize(inode
);
232 if (ext4_should_order_data(inode
))
233 ext4_begin_ordered_truncate(inode
, 0);
234 truncate_inode_pages(&inode
->i_data
, 0);
235 ext4_ioend_shutdown(inode
);
237 if (is_bad_inode(inode
))
241 * Protect us against freezing - iput() caller didn't have to have any
242 * protection against it
244 sb_start_intwrite(inode
->i_sb
);
245 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
,
246 ext4_blocks_for_truncate(inode
)+3);
247 if (IS_ERR(handle
)) {
248 ext4_std_error(inode
->i_sb
, PTR_ERR(handle
));
250 * If we're going to skip the normal cleanup, we still need to
251 * make sure that the in-core orphan linked list is properly
254 ext4_orphan_del(NULL
, inode
);
255 sb_end_intwrite(inode
->i_sb
);
260 ext4_handle_sync(handle
);
262 err
= ext4_mark_inode_dirty(handle
, inode
);
264 ext4_warning(inode
->i_sb
,
265 "couldn't mark inode dirty (err %d)", err
);
269 ext4_truncate(inode
);
272 * ext4_ext_truncate() doesn't reserve any slop when it
273 * restarts journal transactions; therefore there may not be
274 * enough credits left in the handle to remove the inode from
275 * the orphan list and set the dtime field.
277 if (!ext4_handle_has_enough_credits(handle
, 3)) {
278 err
= ext4_journal_extend(handle
, 3);
280 err
= ext4_journal_restart(handle
, 3);
282 ext4_warning(inode
->i_sb
,
283 "couldn't extend journal (err %d)", err
);
285 ext4_journal_stop(handle
);
286 ext4_orphan_del(NULL
, inode
);
287 sb_end_intwrite(inode
->i_sb
);
293 * Kill off the orphan record which ext4_truncate created.
294 * AKPM: I think this can be inside the above `if'.
295 * Note that ext4_orphan_del() has to be able to cope with the
296 * deletion of a non-existent orphan - this is because we don't
297 * know if ext4_truncate() actually created an orphan record.
298 * (Well, we could do this if we need to, but heck - it works)
300 ext4_orphan_del(handle
, inode
);
301 EXT4_I(inode
)->i_dtime
= get_seconds();
304 * One subtle ordering requirement: if anything has gone wrong
305 * (transaction abort, IO errors, whatever), then we can still
306 * do these next steps (the fs will already have been marked as
307 * having errors), but we can't free the inode if the mark_dirty
310 if (ext4_mark_inode_dirty(handle
, inode
))
311 /* If that failed, just do the required in-core inode clear. */
312 ext4_clear_inode(inode
);
314 ext4_free_inode(handle
, inode
);
315 ext4_journal_stop(handle
);
316 sb_end_intwrite(inode
->i_sb
);
319 ext4_clear_inode(inode
); /* We must guarantee clearing of inode... */
323 qsize_t
*ext4_get_reserved_space(struct inode
*inode
)
325 return &EXT4_I(inode
)->i_reserved_quota
;
330 * Calculate the number of metadata blocks need to reserve
331 * to allocate a block located at @lblock
333 static int ext4_calc_metadata_amount(struct inode
*inode
, ext4_lblk_t lblock
)
335 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
336 return ext4_ext_calc_metadata_amount(inode
, lblock
);
338 return ext4_ind_calc_metadata_amount(inode
, lblock
);
342 * Called with i_data_sem down, which is important since we can call
343 * ext4_discard_preallocations() from here.
345 void ext4_da_update_reserve_space(struct inode
*inode
,
346 int used
, int quota_claim
)
348 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
349 struct ext4_inode_info
*ei
= EXT4_I(inode
);
351 spin_lock(&ei
->i_block_reservation_lock
);
352 trace_ext4_da_update_reserve_space(inode
, used
, quota_claim
);
353 if (unlikely(used
> ei
->i_reserved_data_blocks
)) {
354 ext4_warning(inode
->i_sb
, "%s: ino %lu, used %d "
355 "with only %d reserved data blocks",
356 __func__
, inode
->i_ino
, used
,
357 ei
->i_reserved_data_blocks
);
359 used
= ei
->i_reserved_data_blocks
;
362 if (unlikely(ei
->i_allocated_meta_blocks
> ei
->i_reserved_meta_blocks
)) {
363 ext4_warning(inode
->i_sb
, "ino %lu, allocated %d "
364 "with only %d reserved metadata blocks "
365 "(releasing %d blocks with reserved %d data blocks)",
366 inode
->i_ino
, ei
->i_allocated_meta_blocks
,
367 ei
->i_reserved_meta_blocks
, used
,
368 ei
->i_reserved_data_blocks
);
370 ei
->i_allocated_meta_blocks
= ei
->i_reserved_meta_blocks
;
373 /* Update per-inode reservations */
374 ei
->i_reserved_data_blocks
-= used
;
375 ei
->i_reserved_meta_blocks
-= ei
->i_allocated_meta_blocks
;
376 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
377 used
+ ei
->i_allocated_meta_blocks
);
378 ei
->i_allocated_meta_blocks
= 0;
380 if (ei
->i_reserved_data_blocks
== 0) {
382 * We can release all of the reserved metadata blocks
383 * only when we have written all of the delayed
386 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
387 ei
->i_reserved_meta_blocks
);
388 ei
->i_reserved_meta_blocks
= 0;
389 ei
->i_da_metadata_calc_len
= 0;
391 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
393 /* Update quota subsystem for data blocks */
395 dquot_claim_block(inode
, EXT4_C2B(sbi
, used
));
398 * We did fallocate with an offset that is already delayed
399 * allocated. So on delayed allocated writeback we should
400 * not re-claim the quota for fallocated blocks.
402 dquot_release_reservation_block(inode
, EXT4_C2B(sbi
, used
));
406 * If we have done all the pending block allocations and if
407 * there aren't any writers on the inode, we can discard the
408 * inode's preallocations.
410 if ((ei
->i_reserved_data_blocks
== 0) &&
411 (atomic_read(&inode
->i_writecount
) == 0))
412 ext4_discard_preallocations(inode
);
415 static int __check_block_validity(struct inode
*inode
, const char *func
,
417 struct ext4_map_blocks
*map
)
419 if (!ext4_data_block_valid(EXT4_SB(inode
->i_sb
), map
->m_pblk
,
421 ext4_error_inode(inode
, func
, line
, map
->m_pblk
,
422 "lblock %lu mapped to illegal pblock "
423 "(length %d)", (unsigned long) map
->m_lblk
,
430 #define check_block_validity(inode, map) \
431 __check_block_validity((inode), __func__, __LINE__, (map))
434 * Return the number of contiguous dirty pages in a given inode
435 * starting at page frame idx.
437 static pgoff_t
ext4_num_dirty_pages(struct inode
*inode
, pgoff_t idx
,
438 unsigned int max_pages
)
440 struct address_space
*mapping
= inode
->i_mapping
;
444 int i
, nr_pages
, done
= 0;
448 pagevec_init(&pvec
, 0);
451 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
453 (pgoff_t
)PAGEVEC_SIZE
);
456 for (i
= 0; i
< nr_pages
; i
++) {
457 struct page
*page
= pvec
.pages
[i
];
458 struct buffer_head
*bh
, *head
;
461 if (unlikely(page
->mapping
!= mapping
) ||
463 PageWriteback(page
) ||
464 page
->index
!= idx
) {
469 if (page_has_buffers(page
)) {
470 bh
= head
= page_buffers(page
);
472 if (!buffer_delay(bh
) &&
473 !buffer_unwritten(bh
))
475 bh
= bh
->b_this_page
;
476 } while (!done
&& (bh
!= head
));
483 if (num
>= max_pages
) {
488 pagevec_release(&pvec
);
493 #ifdef ES_AGGRESSIVE_TEST
494 static void ext4_map_blocks_es_recheck(handle_t
*handle
,
496 struct ext4_map_blocks
*es_map
,
497 struct ext4_map_blocks
*map
,
504 * There is a race window that the result is not the same.
505 * e.g. xfstests #223 when dioread_nolock enables. The reason
506 * is that we lookup a block mapping in extent status tree with
507 * out taking i_data_sem. So at the time the unwritten extent
508 * could be converted.
510 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
511 down_read((&EXT4_I(inode
)->i_data_sem
));
512 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
513 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
&
514 EXT4_GET_BLOCKS_KEEP_SIZE
);
516 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
&
517 EXT4_GET_BLOCKS_KEEP_SIZE
);
519 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
520 up_read((&EXT4_I(inode
)->i_data_sem
));
522 * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag
523 * because it shouldn't be marked in es_map->m_flags.
525 map
->m_flags
&= ~(EXT4_MAP_FROM_CLUSTER
| EXT4_MAP_BOUNDARY
);
528 * We don't check m_len because extent will be collpased in status
529 * tree. So the m_len might not equal.
531 if (es_map
->m_lblk
!= map
->m_lblk
||
532 es_map
->m_flags
!= map
->m_flags
||
533 es_map
->m_pblk
!= map
->m_pblk
) {
534 printk("ES cache assertation failed for inode: %lu "
535 "es_cached ex [%d/%d/%llu/%x] != "
536 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
537 inode
->i_ino
, es_map
->m_lblk
, es_map
->m_len
,
538 es_map
->m_pblk
, es_map
->m_flags
, map
->m_lblk
,
539 map
->m_len
, map
->m_pblk
, map
->m_flags
,
543 #endif /* ES_AGGRESSIVE_TEST */
546 * The ext4_map_blocks() function tries to look up the requested blocks,
547 * and returns if the blocks are already mapped.
549 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
550 * and store the allocated blocks in the result buffer head and mark it
553 * If file type is extents based, it will call ext4_ext_map_blocks(),
554 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
557 * On success, it returns the number of blocks being mapped or allocate.
558 * if create==0 and the blocks are pre-allocated and uninitialized block,
559 * the result buffer head is unmapped. If the create ==1, it will make sure
560 * the buffer head is mapped.
562 * It returns 0 if plain look up failed (blocks have not been allocated), in
563 * that case, buffer head is unmapped
565 * It returns the error in case of allocation failure.
567 int ext4_map_blocks(handle_t
*handle
, struct inode
*inode
,
568 struct ext4_map_blocks
*map
, int flags
)
570 struct extent_status es
;
572 #ifdef ES_AGGRESSIVE_TEST
573 struct ext4_map_blocks orig_map
;
575 memcpy(&orig_map
, map
, sizeof(*map
));
579 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
580 "logical block %lu\n", inode
->i_ino
, flags
, map
->m_len
,
581 (unsigned long) map
->m_lblk
);
583 /* Lookup extent status tree firstly */
584 if (ext4_es_lookup_extent(inode
, map
->m_lblk
, &es
)) {
585 if (ext4_es_is_written(&es
) || ext4_es_is_unwritten(&es
)) {
586 map
->m_pblk
= ext4_es_pblock(&es
) +
587 map
->m_lblk
- es
.es_lblk
;
588 map
->m_flags
|= ext4_es_is_written(&es
) ?
589 EXT4_MAP_MAPPED
: EXT4_MAP_UNWRITTEN
;
590 retval
= es
.es_len
- (map
->m_lblk
- es
.es_lblk
);
591 if (retval
> map
->m_len
)
594 } else if (ext4_es_is_delayed(&es
) || ext4_es_is_hole(&es
)) {
599 #ifdef ES_AGGRESSIVE_TEST
600 ext4_map_blocks_es_recheck(handle
, inode
, map
,
607 * Try to see if we can get the block without requesting a new
610 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
611 down_read((&EXT4_I(inode
)->i_data_sem
));
612 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
613 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
&
614 EXT4_GET_BLOCKS_KEEP_SIZE
);
616 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
&
617 EXT4_GET_BLOCKS_KEEP_SIZE
);
621 unsigned long long status
;
623 #ifdef ES_AGGRESSIVE_TEST
624 if (retval
!= map
->m_len
) {
625 printk("ES len assertation failed for inode: %lu "
626 "retval %d != map->m_len %d "
627 "in %s (lookup)\n", inode
->i_ino
, retval
,
628 map
->m_len
, __func__
);
632 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
633 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
634 if (!(flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) &&
635 !(status
& EXTENT_STATUS_WRITTEN
) &&
636 ext4_find_delalloc_range(inode
, map
->m_lblk
,
637 map
->m_lblk
+ map
->m_len
- 1))
638 status
|= EXTENT_STATUS_DELAYED
;
639 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
,
640 map
->m_len
, map
->m_pblk
, status
);
644 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
645 up_read((&EXT4_I(inode
)->i_data_sem
));
648 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
) {
649 int ret
= check_block_validity(inode
, map
);
654 /* If it is only a block(s) look up */
655 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0)
659 * Returns if the blocks have already allocated
661 * Note that if blocks have been preallocated
662 * ext4_ext_get_block() returns the create = 0
663 * with buffer head unmapped.
665 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
)
669 * Here we clear m_flags because after allocating an new extent,
670 * it will be set again.
672 map
->m_flags
&= ~EXT4_MAP_FLAGS
;
675 * New blocks allocate and/or writing to uninitialized extent
676 * will possibly result in updating i_data, so we take
677 * the write lock of i_data_sem, and call get_blocks()
678 * with create == 1 flag.
680 down_write((&EXT4_I(inode
)->i_data_sem
));
683 * if the caller is from delayed allocation writeout path
684 * we have already reserved fs blocks for allocation
685 * let the underlying get_block() function know to
686 * avoid double accounting
688 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
689 ext4_set_inode_state(inode
, EXT4_STATE_DELALLOC_RESERVED
);
691 * We need to check for EXT4 here because migrate
692 * could have changed the inode type in between
694 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
695 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
);
697 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
);
699 if (retval
> 0 && map
->m_flags
& EXT4_MAP_NEW
) {
701 * We allocated new blocks which will result in
702 * i_data's format changing. Force the migrate
703 * to fail by clearing migrate flags
705 ext4_clear_inode_state(inode
, EXT4_STATE_EXT_MIGRATE
);
709 * Update reserved blocks/metadata blocks after successful
710 * block allocation which had been deferred till now. We don't
711 * support fallocate for non extent files. So we can update
712 * reserve space here.
715 (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
))
716 ext4_da_update_reserve_space(inode
, retval
, 1);
718 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
719 ext4_clear_inode_state(inode
, EXT4_STATE_DELALLOC_RESERVED
);
723 unsigned long long status
;
725 #ifdef ES_AGGRESSIVE_TEST
726 if (retval
!= map
->m_len
) {
727 printk("ES len assertation failed for inode: %lu "
728 "retval %d != map->m_len %d "
729 "in %s (allocation)\n", inode
->i_ino
, retval
,
730 map
->m_len
, __func__
);
735 * If the extent has been zeroed out, we don't need to update
736 * extent status tree.
738 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
) &&
739 ext4_es_lookup_extent(inode
, map
->m_lblk
, &es
)) {
740 if (ext4_es_is_written(&es
))
743 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
744 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
745 if (!(flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) &&
746 !(status
& EXTENT_STATUS_WRITTEN
) &&
747 ext4_find_delalloc_range(inode
, map
->m_lblk
,
748 map
->m_lblk
+ map
->m_len
- 1))
749 status
|= EXTENT_STATUS_DELAYED
;
750 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
, map
->m_len
,
751 map
->m_pblk
, status
);
757 up_write((&EXT4_I(inode
)->i_data_sem
));
758 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
) {
759 int ret
= check_block_validity(inode
, map
);
764 * Inodes with freshly allocated blocks where contents will be
765 * visible after transaction commit must be on transaction's
768 if (map
->m_flags
& EXT4_MAP_NEW
&&
769 !(map
->m_flags
& EXT4_MAP_UNWRITTEN
) &&
770 !IS_NOQUOTA(inode
) &&
771 ext4_should_order_data(inode
)) {
772 ret
= ext4_jbd2_file_inode(handle
, inode
);
780 /* Maximum number of blocks we map for direct IO at once. */
781 #define DIO_MAX_BLOCKS 4096
783 static int _ext4_get_block(struct inode
*inode
, sector_t iblock
,
784 struct buffer_head
*bh
, int flags
)
786 handle_t
*handle
= ext4_journal_current_handle();
787 struct ext4_map_blocks map
;
788 int ret
= 0, started
= 0;
791 if (ext4_has_inline_data(inode
))
795 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
797 if (flags
&& !(flags
& EXT4_GET_BLOCKS_NO_LOCK
) && !handle
) {
798 /* Direct IO write... */
799 if (map
.m_len
> DIO_MAX_BLOCKS
)
800 map
.m_len
= DIO_MAX_BLOCKS
;
801 dio_credits
= ext4_chunk_trans_blocks(inode
, map
.m_len
);
802 handle
= ext4_journal_start(inode
, EXT4_HT_MAP_BLOCKS
,
804 if (IS_ERR(handle
)) {
805 ret
= PTR_ERR(handle
);
811 ret
= ext4_map_blocks(handle
, inode
, &map
, flags
);
813 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
814 bh
->b_state
= (bh
->b_state
& ~EXT4_MAP_FLAGS
) | map
.m_flags
;
815 bh
->b_size
= inode
->i_sb
->s_blocksize
* map
.m_len
;
819 ext4_journal_stop(handle
);
823 int ext4_get_block(struct inode
*inode
, sector_t iblock
,
824 struct buffer_head
*bh
, int create
)
826 return _ext4_get_block(inode
, iblock
, bh
,
827 create
? EXT4_GET_BLOCKS_CREATE
: 0);
831 * `handle' can be NULL if create is zero
833 struct buffer_head
*ext4_getblk(handle_t
*handle
, struct inode
*inode
,
834 ext4_lblk_t block
, int create
, int *errp
)
836 struct ext4_map_blocks map
;
837 struct buffer_head
*bh
;
840 J_ASSERT(handle
!= NULL
|| create
== 0);
844 err
= ext4_map_blocks(handle
, inode
, &map
,
845 create
? EXT4_GET_BLOCKS_CREATE
: 0);
847 /* ensure we send some value back into *errp */
850 if (create
&& err
== 0)
851 err
= -ENOSPC
; /* should never happen */
857 bh
= sb_getblk(inode
->i_sb
, map
.m_pblk
);
862 if (map
.m_flags
& EXT4_MAP_NEW
) {
863 J_ASSERT(create
!= 0);
864 J_ASSERT(handle
!= NULL
);
867 * Now that we do not always journal data, we should
868 * keep in mind whether this should always journal the
869 * new buffer as metadata. For now, regular file
870 * writes use ext4_get_block instead, so it's not a
874 BUFFER_TRACE(bh
, "call get_create_access");
875 fatal
= ext4_journal_get_create_access(handle
, bh
);
876 if (!fatal
&& !buffer_uptodate(bh
)) {
877 memset(bh
->b_data
, 0, inode
->i_sb
->s_blocksize
);
878 set_buffer_uptodate(bh
);
881 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
882 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
886 BUFFER_TRACE(bh
, "not a new buffer");
896 struct buffer_head
*ext4_bread(handle_t
*handle
, struct inode
*inode
,
897 ext4_lblk_t block
, int create
, int *err
)
899 struct buffer_head
*bh
;
901 bh
= ext4_getblk(handle
, inode
, block
, create
, err
);
904 if (buffer_uptodate(bh
))
906 ll_rw_block(READ
| REQ_META
| REQ_PRIO
, 1, &bh
);
908 if (buffer_uptodate(bh
))
915 int ext4_walk_page_buffers(handle_t
*handle
,
916 struct buffer_head
*head
,
920 int (*fn
)(handle_t
*handle
,
921 struct buffer_head
*bh
))
923 struct buffer_head
*bh
;
924 unsigned block_start
, block_end
;
925 unsigned blocksize
= head
->b_size
;
927 struct buffer_head
*next
;
929 for (bh
= head
, block_start
= 0;
930 ret
== 0 && (bh
!= head
|| !block_start
);
931 block_start
= block_end
, bh
= next
) {
932 next
= bh
->b_this_page
;
933 block_end
= block_start
+ blocksize
;
934 if (block_end
<= from
|| block_start
>= to
) {
935 if (partial
&& !buffer_uptodate(bh
))
939 err
= (*fn
)(handle
, bh
);
947 * To preserve ordering, it is essential that the hole instantiation and
948 * the data write be encapsulated in a single transaction. We cannot
949 * close off a transaction and start a new one between the ext4_get_block()
950 * and the commit_write(). So doing the jbd2_journal_start at the start of
951 * prepare_write() is the right place.
953 * Also, this function can nest inside ext4_writepage(). In that case, we
954 * *know* that ext4_writepage() has generated enough buffer credits to do the
955 * whole page. So we won't block on the journal in that case, which is good,
956 * because the caller may be PF_MEMALLOC.
958 * By accident, ext4 can be reentered when a transaction is open via
959 * quota file writes. If we were to commit the transaction while thus
960 * reentered, there can be a deadlock - we would be holding a quota
961 * lock, and the commit would never complete if another thread had a
962 * transaction open and was blocking on the quota lock - a ranking
965 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
966 * will _not_ run commit under these circumstances because handle->h_ref
967 * is elevated. We'll still have enough credits for the tiny quotafile
970 int do_journal_get_write_access(handle_t
*handle
,
971 struct buffer_head
*bh
)
973 int dirty
= buffer_dirty(bh
);
976 if (!buffer_mapped(bh
) || buffer_freed(bh
))
979 * __block_write_begin() could have dirtied some buffers. Clean
980 * the dirty bit as jbd2_journal_get_write_access() could complain
981 * otherwise about fs integrity issues. Setting of the dirty bit
982 * by __block_write_begin() isn't a real problem here as we clear
983 * the bit before releasing a page lock and thus writeback cannot
984 * ever write the buffer.
987 clear_buffer_dirty(bh
);
988 ret
= ext4_journal_get_write_access(handle
, bh
);
990 ret
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
994 static int ext4_get_block_write_nolock(struct inode
*inode
, sector_t iblock
,
995 struct buffer_head
*bh_result
, int create
);
996 static int ext4_write_begin(struct file
*file
, struct address_space
*mapping
,
997 loff_t pos
, unsigned len
, unsigned flags
,
998 struct page
**pagep
, void **fsdata
)
1000 struct inode
*inode
= mapping
->host
;
1001 int ret
, needed_blocks
;
1007 #if defined(FEATURE_STORAGE_PID_LOGGER)
1008 extern unsigned char *page_logger
;
1009 struct page_pid_logger
*tmp_logger
;
1010 unsigned long page_index
;
1011 extern spinlock_t g_locker
;
1012 unsigned long g_flags
;
1015 trace_ext4_write_begin(inode
, pos
, len
, flags
);
1017 * Reserve one block more for addition to orphan list in case
1018 * we allocate blocks but write fails for some reason
1020 needed_blocks
= ext4_writepage_trans_blocks(inode
) + 1;
1021 index
= pos
>> PAGE_CACHE_SHIFT
;
1022 from
= pos
& (PAGE_CACHE_SIZE
- 1);
1025 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
1026 ret
= ext4_try_to_write_inline_data(mapping
, inode
, pos
, len
,
1035 * grab_cache_page_write_begin() can take a long time if the
1036 * system is thrashing due to memory pressure, or if the page
1037 * is being written back. So grab it first before we start
1038 * the transaction handle. This also allows us to allocate
1039 * the page (if needed) without using GFP_NOFS.
1042 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
1048 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
, needed_blocks
);
1049 if (IS_ERR(handle
)) {
1050 page_cache_release(page
);
1051 return PTR_ERR(handle
);
1055 if (page
->mapping
!= mapping
) {
1056 /* The page got truncated from under us */
1058 page_cache_release(page
);
1059 ext4_journal_stop(handle
);
1062 /* In case writeback began while the page was unlocked */
1063 wait_for_stable_page(page
);
1065 if (ext4_should_dioread_nolock(inode
))
1066 ret
= __block_write_begin(page
, pos
, len
, ext4_get_block_write
);
1068 ret
= __block_write_begin(page
, pos
, len
, ext4_get_block
);
1070 if (!ret
&& ext4_should_journal_data(inode
)) {
1071 ret
= ext4_walk_page_buffers(handle
, page_buffers(page
),
1073 do_journal_get_write_access
);
1079 * __block_write_begin may have instantiated a few blocks
1080 * outside i_size. Trim these off again. Don't need
1081 * i_size_read because we hold i_mutex.
1083 * Add inode to orphan list in case we crash before
1086 if (pos
+ len
> inode
->i_size
&& ext4_can_truncate(inode
))
1087 ext4_orphan_add(handle
, inode
);
1089 ext4_journal_stop(handle
);
1090 if (pos
+ len
> inode
->i_size
) {
1091 ext4_truncate_failed_write(inode
);
1093 * If truncate failed early the inode might
1094 * still be on the orphan list; we need to
1095 * make sure the inode is removed from the
1096 * orphan list in that case.
1099 ext4_orphan_del(NULL
, inode
);
1102 if (ret
== -ENOSPC
&&
1103 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1105 page_cache_release(page
);
1109 #if defined(FEATURE_STORAGE_PID_LOGGER)
1110 if( page_logger
&& (*pagep
)) {
1111 //#if defined(CONFIG_FLATMEM)
1112 //page_index = (unsigned long)((*pagep) - mem_map) ;
1114 page_index
= (unsigned long)(__page_to_pfn(*pagep
))- PHYS_PFN_OFFSET
;
1116 tmp_logger
=((struct page_pid_logger
*)page_logger
) + page_index
;
1117 spin_lock_irqsave(&g_locker
, g_flags
);
1118 if( page_index
< num_physpages
) {
1119 if( tmp_logger
->pid1
== 0XFFFF)
1120 tmp_logger
->pid1
= current
->pid
;
1121 else if( tmp_logger
->pid1
!= current
->pid
)
1122 tmp_logger
->pid2
= current
->pid
;
1124 spin_unlock_irqrestore(&g_locker
, g_flags
);
1130 /* For write_end() in data=journal mode */
1131 static int write_end_fn(handle_t
*handle
, struct buffer_head
*bh
)
1134 if (!buffer_mapped(bh
) || buffer_freed(bh
))
1136 set_buffer_uptodate(bh
);
1137 ret
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
1138 clear_buffer_meta(bh
);
1139 clear_buffer_prio(bh
);
1144 * We need to pick up the new inode size which generic_commit_write gave us
1145 * `file' can be NULL - eg, when called from page_symlink().
1147 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1148 * buffers are managed internally.
1150 static int ext4_write_end(struct file
*file
,
1151 struct address_space
*mapping
,
1152 loff_t pos
, unsigned len
, unsigned copied
,
1153 struct page
*page
, void *fsdata
)
1155 handle_t
*handle
= ext4_journal_current_handle();
1156 struct inode
*inode
= mapping
->host
;
1158 int i_size_changed
= 0;
1160 trace_ext4_write_end(inode
, pos
, len
, copied
);
1161 if (ext4_has_inline_data(inode
)) {
1162 ret
= ext4_write_inline_data_end(inode
, pos
, len
,
1168 copied
= block_write_end(file
, mapping
, pos
,
1169 len
, copied
, page
, fsdata
);
1172 * No need to use i_size_read() here, the i_size
1173 * cannot change under us because we hole i_mutex.
1175 * But it's important to update i_size while still holding page lock:
1176 * page writeout could otherwise come in and zero beyond i_size.
1178 if (pos
+ copied
> inode
->i_size
) {
1179 i_size_write(inode
, pos
+ copied
);
1183 if (pos
+ copied
> EXT4_I(inode
)->i_disksize
) {
1184 /* We need to mark inode dirty even if
1185 * new_i_size is less that inode->i_size
1186 * but greater than i_disksize. (hint delalloc)
1188 ext4_update_i_disksize(inode
, (pos
+ copied
));
1192 page_cache_release(page
);
1195 * Don't mark the inode dirty under page lock. First, it unnecessarily
1196 * makes the holding time of page lock longer. Second, it forces lock
1197 * ordering of page lock and transaction start for journaling
1201 ext4_mark_inode_dirty(handle
, inode
);
1205 if (pos
+ len
> inode
->i_size
&& ext4_can_truncate(inode
))
1206 /* if we have allocated more blocks and copied
1207 * less. We will have blocks allocated outside
1208 * inode->i_size. So truncate them
1210 ext4_orphan_add(handle
, inode
);
1212 ret2
= ext4_journal_stop(handle
);
1216 if (pos
+ len
> inode
->i_size
) {
1217 ext4_truncate_failed_write(inode
);
1219 * If truncate failed early the inode might still be
1220 * on the orphan list; we need to make sure the inode
1221 * is removed from the orphan list in that case.
1224 ext4_orphan_del(NULL
, inode
);
1227 return ret
? ret
: copied
;
1230 static int ext4_journalled_write_end(struct file
*file
,
1231 struct address_space
*mapping
,
1232 loff_t pos
, unsigned len
, unsigned copied
,
1233 struct page
*page
, void *fsdata
)
1235 handle_t
*handle
= ext4_journal_current_handle();
1236 struct inode
*inode
= mapping
->host
;
1242 trace_ext4_journalled_write_end(inode
, pos
, len
, copied
);
1243 from
= pos
& (PAGE_CACHE_SIZE
- 1);
1246 BUG_ON(!ext4_handle_valid(handle
));
1248 if (ext4_has_inline_data(inode
))
1249 copied
= ext4_write_inline_data_end(inode
, pos
, len
,
1253 if (!PageUptodate(page
))
1255 page_zero_new_buffers(page
, from
+copied
, to
);
1258 ret
= ext4_walk_page_buffers(handle
, page_buffers(page
), from
,
1259 to
, &partial
, write_end_fn
);
1261 SetPageUptodate(page
);
1263 new_i_size
= pos
+ copied
;
1264 if (new_i_size
> inode
->i_size
)
1265 i_size_write(inode
, pos
+copied
);
1266 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
1267 EXT4_I(inode
)->i_datasync_tid
= handle
->h_transaction
->t_tid
;
1268 if (new_i_size
> EXT4_I(inode
)->i_disksize
) {
1269 ext4_update_i_disksize(inode
, new_i_size
);
1270 ret2
= ext4_mark_inode_dirty(handle
, inode
);
1276 page_cache_release(page
);
1277 if (pos
+ len
> inode
->i_size
&& ext4_can_truncate(inode
))
1278 /* if we have allocated more blocks and copied
1279 * less. We will have blocks allocated outside
1280 * inode->i_size. So truncate them
1282 ext4_orphan_add(handle
, inode
);
1284 ret2
= ext4_journal_stop(handle
);
1287 if (pos
+ len
> inode
->i_size
) {
1288 ext4_truncate_failed_write(inode
);
1290 * If truncate failed early the inode might still be
1291 * on the orphan list; we need to make sure the inode
1292 * is removed from the orphan list in that case.
1295 ext4_orphan_del(NULL
, inode
);
1298 return ret
? ret
: copied
;
1302 * Reserve a metadata for a single block located at lblock
1304 static int ext4_da_reserve_metadata(struct inode
*inode
, ext4_lblk_t lblock
)
1306 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1307 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1308 unsigned int md_needed
;
1309 ext4_lblk_t save_last_lblock
;
1313 * recalculate the amount of metadata blocks to reserve
1314 * in order to allocate nrblocks
1315 * worse case is one extent per block
1317 spin_lock(&ei
->i_block_reservation_lock
);
1319 * ext4_calc_metadata_amount() has side effects, which we have
1320 * to be prepared undo if we fail to claim space.
1322 save_len
= ei
->i_da_metadata_calc_len
;
1323 save_last_lblock
= ei
->i_da_metadata_calc_last_lblock
;
1324 md_needed
= EXT4_NUM_B2C(sbi
,
1325 ext4_calc_metadata_amount(inode
, lblock
));
1326 trace_ext4_da_reserve_space(inode
, md_needed
);
1329 * We do still charge estimated metadata to the sb though;
1330 * we cannot afford to run out of free blocks.
1332 if (ext4_claim_free_clusters(sbi
, md_needed
, 0)) {
1333 ei
->i_da_metadata_calc_len
= save_len
;
1334 ei
->i_da_metadata_calc_last_lblock
= save_last_lblock
;
1335 spin_unlock(&ei
->i_block_reservation_lock
);
1338 ei
->i_reserved_meta_blocks
+= md_needed
;
1339 spin_unlock(&ei
->i_block_reservation_lock
);
1341 return 0; /* success */
1345 * Reserve a single cluster located at lblock
1347 static int ext4_da_reserve_space(struct inode
*inode
, ext4_lblk_t lblock
)
1349 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1350 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1351 unsigned int md_needed
;
1353 ext4_lblk_t save_last_lblock
;
1357 * We will charge metadata quota at writeout time; this saves
1358 * us from metadata over-estimation, though we may go over by
1359 * a small amount in the end. Here we just reserve for data.
1361 ret
= dquot_reserve_block(inode
, EXT4_C2B(sbi
, 1));
1366 * recalculate the amount of metadata blocks to reserve
1367 * in order to allocate nrblocks
1368 * worse case is one extent per block
1370 spin_lock(&ei
->i_block_reservation_lock
);
1372 * ext4_calc_metadata_amount() has side effects, which we have
1373 * to be prepared undo if we fail to claim space.
1375 save_len
= ei
->i_da_metadata_calc_len
;
1376 save_last_lblock
= ei
->i_da_metadata_calc_last_lblock
;
1377 md_needed
= EXT4_NUM_B2C(sbi
,
1378 ext4_calc_metadata_amount(inode
, lblock
));
1379 trace_ext4_da_reserve_space(inode
, md_needed
);
1382 * We do still charge estimated metadata to the sb though;
1383 * we cannot afford to run out of free blocks.
1385 if (ext4_claim_free_clusters(sbi
, md_needed
+ 1, 0)) {
1386 ei
->i_da_metadata_calc_len
= save_len
;
1387 ei
->i_da_metadata_calc_last_lblock
= save_last_lblock
;
1388 spin_unlock(&ei
->i_block_reservation_lock
);
1389 dquot_release_reservation_block(inode
, EXT4_C2B(sbi
, 1));
1392 ei
->i_reserved_data_blocks
++;
1393 ei
->i_reserved_meta_blocks
+= md_needed
;
1394 spin_unlock(&ei
->i_block_reservation_lock
);
1396 return 0; /* success */
1399 static void ext4_da_release_space(struct inode
*inode
, int to_free
)
1401 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1402 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1405 return; /* Nothing to release, exit */
1407 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1409 trace_ext4_da_release_space(inode
, to_free
);
1410 if (unlikely(to_free
> ei
->i_reserved_data_blocks
)) {
1412 * if there aren't enough reserved blocks, then the
1413 * counter is messed up somewhere. Since this
1414 * function is called from invalidate page, it's
1415 * harmless to return without any action.
1417 ext4_warning(inode
->i_sb
, "ext4_da_release_space: "
1418 "ino %lu, to_free %d with only %d reserved "
1419 "data blocks", inode
->i_ino
, to_free
,
1420 ei
->i_reserved_data_blocks
);
1422 to_free
= ei
->i_reserved_data_blocks
;
1424 ei
->i_reserved_data_blocks
-= to_free
;
1426 if (ei
->i_reserved_data_blocks
== 0) {
1428 * We can release all of the reserved metadata blocks
1429 * only when we have written all of the delayed
1430 * allocation blocks.
1431 * Note that in case of bigalloc, i_reserved_meta_blocks,
1432 * i_reserved_data_blocks, etc. refer to number of clusters.
1434 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
1435 ei
->i_reserved_meta_blocks
);
1436 ei
->i_reserved_meta_blocks
= 0;
1437 ei
->i_da_metadata_calc_len
= 0;
1440 /* update fs dirty data blocks counter */
1441 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
, to_free
);
1443 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1445 dquot_release_reservation_block(inode
, EXT4_C2B(sbi
, to_free
));
1448 static void ext4_da_page_release_reservation(struct page
*page
,
1449 unsigned long offset
)
1451 int to_release
= 0, contiguous_blks
= 0;
1452 struct buffer_head
*head
, *bh
;
1453 unsigned int curr_off
= 0;
1454 struct inode
*inode
= page
->mapping
->host
;
1455 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1459 head
= page_buffers(page
);
1462 unsigned int next_off
= curr_off
+ bh
->b_size
;
1464 if ((offset
<= curr_off
) && (buffer_delay(bh
))) {
1467 clear_buffer_delay(bh
);
1468 } else if (contiguous_blks
) {
1469 lblk
= page
->index
<<
1470 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1471 lblk
+= (curr_off
>> inode
->i_blkbits
) -
1473 ext4_es_remove_extent(inode
, lblk
, contiguous_blks
);
1474 contiguous_blks
= 0;
1476 curr_off
= next_off
;
1477 } while ((bh
= bh
->b_this_page
) != head
);
1479 if (contiguous_blks
) {
1480 lblk
= page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1481 lblk
+= (curr_off
>> inode
->i_blkbits
) - contiguous_blks
;
1482 ext4_es_remove_extent(inode
, lblk
, contiguous_blks
);
1485 /* If we have released all the blocks belonging to a cluster, then we
1486 * need to release the reserved space for that cluster. */
1487 num_clusters
= EXT4_NUM_B2C(sbi
, to_release
);
1488 while (num_clusters
> 0) {
1489 lblk
= (page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
)) +
1490 ((num_clusters
- 1) << sbi
->s_cluster_bits
);
1491 if (sbi
->s_cluster_ratio
== 1 ||
1492 !ext4_find_delalloc_cluster(inode
, lblk
))
1493 ext4_da_release_space(inode
, 1);
1500 * Delayed allocation stuff
1504 * mpage_da_submit_io - walks through extent of pages and try to write
1505 * them with writepage() call back
1507 * @mpd->inode: inode
1508 * @mpd->first_page: first page of the extent
1509 * @mpd->next_page: page after the last page of the extent
1511 * By the time mpage_da_submit_io() is called we expect all blocks
1512 * to be allocated. this may be wrong if allocation failed.
1514 * As pages are already locked by write_cache_pages(), we can't use it
1516 static int mpage_da_submit_io(struct mpage_da_data
*mpd
,
1517 struct ext4_map_blocks
*map
)
1519 struct pagevec pvec
;
1520 unsigned long index
, end
;
1521 int ret
= 0, err
, nr_pages
, i
;
1522 struct inode
*inode
= mpd
->inode
;
1523 struct address_space
*mapping
= inode
->i_mapping
;
1524 loff_t size
= i_size_read(inode
);
1525 unsigned int len
, block_start
;
1526 struct buffer_head
*bh
, *page_bufs
= NULL
;
1527 sector_t pblock
= 0, cur_logical
= 0;
1528 struct ext4_io_submit io_submit
;
1530 BUG_ON(mpd
->next_page
<= mpd
->first_page
);
1531 memset(&io_submit
, 0, sizeof(io_submit
));
1533 * We need to start from the first_page to the next_page - 1
1534 * to make sure we also write the mapped dirty buffer_heads.
1535 * If we look at mpd->b_blocknr we would only be looking
1536 * at the currently mapped buffer_heads.
1538 index
= mpd
->first_page
;
1539 end
= mpd
->next_page
- 1;
1541 pagevec_init(&pvec
, 0);
1542 while (index
<= end
) {
1543 nr_pages
= pagevec_lookup(&pvec
, mapping
, index
, PAGEVEC_SIZE
);
1546 for (i
= 0; i
< nr_pages
; i
++) {
1548 struct page
*page
= pvec
.pages
[i
];
1550 index
= page
->index
;
1554 if (index
== size
>> PAGE_CACHE_SHIFT
)
1555 len
= size
& ~PAGE_CACHE_MASK
;
1557 len
= PAGE_CACHE_SIZE
;
1559 cur_logical
= index
<< (PAGE_CACHE_SHIFT
-
1561 pblock
= map
->m_pblk
+ (cur_logical
-
1566 BUG_ON(!PageLocked(page
));
1567 BUG_ON(PageWriteback(page
));
1569 bh
= page_bufs
= page_buffers(page
);
1572 if (map
&& (cur_logical
>= map
->m_lblk
) &&
1573 (cur_logical
<= (map
->m_lblk
+
1574 (map
->m_len
- 1)))) {
1575 if (buffer_delay(bh
)) {
1576 clear_buffer_delay(bh
);
1577 bh
->b_blocknr
= pblock
;
1579 if (buffer_unwritten(bh
) ||
1581 BUG_ON(bh
->b_blocknr
!= pblock
);
1582 if (map
->m_flags
& EXT4_MAP_UNINIT
)
1583 set_buffer_uninit(bh
);
1584 clear_buffer_unwritten(bh
);
1588 * skip page if block allocation undone and
1591 if (ext4_bh_delay_or_unwritten(NULL
, bh
))
1593 bh
= bh
->b_this_page
;
1594 block_start
+= bh
->b_size
;
1597 } while (bh
!= page_bufs
);
1604 clear_page_dirty_for_io(page
);
1605 err
= ext4_bio_write_page(&io_submit
, page
, len
,
1608 mpd
->pages_written
++;
1610 * In error case, we have to continue because
1611 * remaining pages are still locked
1616 pagevec_release(&pvec
);
1618 ext4_io_submit(&io_submit
);
1622 static void ext4_da_block_invalidatepages(struct mpage_da_data
*mpd
)
1626 struct pagevec pvec
;
1627 struct inode
*inode
= mpd
->inode
;
1628 struct address_space
*mapping
= inode
->i_mapping
;
1629 ext4_lblk_t start
, last
;
1631 index
= mpd
->first_page
;
1632 end
= mpd
->next_page
- 1;
1634 start
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1635 last
= end
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1636 ext4_es_remove_extent(inode
, start
, last
- start
+ 1);
1638 pagevec_init(&pvec
, 0);
1639 while (index
<= end
) {
1640 nr_pages
= pagevec_lookup(&pvec
, mapping
, index
, PAGEVEC_SIZE
);
1643 for (i
= 0; i
< nr_pages
; i
++) {
1644 struct page
*page
= pvec
.pages
[i
];
1645 if (page
->index
> end
)
1647 BUG_ON(!PageLocked(page
));
1648 BUG_ON(PageWriteback(page
));
1649 block_invalidatepage(page
, 0);
1650 ClearPageUptodate(page
);
1653 index
= pvec
.pages
[nr_pages
- 1]->index
+ 1;
1654 pagevec_release(&pvec
);
1659 static void ext4_print_free_blocks(struct inode
*inode
)
1661 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1662 struct super_block
*sb
= inode
->i_sb
;
1663 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1665 ext4_msg(sb
, KERN_CRIT
, "Total free blocks count %lld",
1666 EXT4_C2B(EXT4_SB(inode
->i_sb
),
1667 ext4_count_free_clusters(sb
)));
1668 ext4_msg(sb
, KERN_CRIT
, "Free/Dirty block details");
1669 ext4_msg(sb
, KERN_CRIT
, "free_blocks=%lld",
1670 (long long) EXT4_C2B(EXT4_SB(sb
),
1671 percpu_counter_sum(&sbi
->s_freeclusters_counter
)));
1672 ext4_msg(sb
, KERN_CRIT
, "dirty_blocks=%lld",
1673 (long long) EXT4_C2B(EXT4_SB(sb
),
1674 percpu_counter_sum(&sbi
->s_dirtyclusters_counter
)));
1675 ext4_msg(sb
, KERN_CRIT
, "Block reservation details");
1676 ext4_msg(sb
, KERN_CRIT
, "i_reserved_data_blocks=%u",
1677 ei
->i_reserved_data_blocks
);
1678 ext4_msg(sb
, KERN_CRIT
, "i_reserved_meta_blocks=%u",
1679 ei
->i_reserved_meta_blocks
);
1680 ext4_msg(sb
, KERN_CRIT
, "i_allocated_meta_blocks=%u",
1681 ei
->i_allocated_meta_blocks
);
1686 * mpage_da_map_and_submit - go through given space, map them
1687 * if necessary, and then submit them for I/O
1689 * @mpd - bh describing space
1691 * The function skips space we know is already mapped to disk blocks.
1694 static void mpage_da_map_and_submit(struct mpage_da_data
*mpd
)
1696 int err
, blks
, get_blocks_flags
;
1697 struct ext4_map_blocks map
, *mapp
= NULL
;
1698 sector_t next
= mpd
->b_blocknr
;
1699 unsigned max_blocks
= mpd
->b_size
>> mpd
->inode
->i_blkbits
;
1700 loff_t disksize
= EXT4_I(mpd
->inode
)->i_disksize
;
1701 handle_t
*handle
= NULL
;
1704 * If the blocks are mapped already, or we couldn't accumulate
1705 * any blocks, then proceed immediately to the submission stage.
1707 if ((mpd
->b_size
== 0) ||
1708 ((mpd
->b_state
& (1 << BH_Mapped
)) &&
1709 !(mpd
->b_state
& (1 << BH_Delay
)) &&
1710 !(mpd
->b_state
& (1 << BH_Unwritten
))))
1713 handle
= ext4_journal_current_handle();
1717 * Call ext4_map_blocks() to allocate any delayed allocation
1718 * blocks, or to convert an uninitialized extent to be
1719 * initialized (in the case where we have written into
1720 * one or more preallocated blocks).
1722 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
1723 * indicate that we are on the delayed allocation path. This
1724 * affects functions in many different parts of the allocation
1725 * call path. This flag exists primarily because we don't
1726 * want to change *many* call functions, so ext4_map_blocks()
1727 * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
1728 * inode's allocation semaphore is taken.
1730 * If the blocks in questions were delalloc blocks, set
1731 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
1732 * variables are updated after the blocks have been allocated.
1735 map
.m_len
= max_blocks
;
1737 * We're in delalloc path and it is possible that we're going to
1738 * need more metadata blocks than previously reserved. However
1739 * we must not fail because we're in writeback and there is
1740 * nothing we can do about it so it might result in data loss.
1741 * So use reserved blocks to allocate metadata if possible.
1743 get_blocks_flags
= EXT4_GET_BLOCKS_CREATE
|
1744 EXT4_GET_BLOCKS_METADATA_NOFAIL
;
1745 if (ext4_should_dioread_nolock(mpd
->inode
))
1746 get_blocks_flags
|= EXT4_GET_BLOCKS_IO_CREATE_EXT
;
1747 if (mpd
->b_state
& (1 << BH_Delay
))
1748 get_blocks_flags
|= EXT4_GET_BLOCKS_DELALLOC_RESERVE
;
1751 blks
= ext4_map_blocks(handle
, mpd
->inode
, &map
, get_blocks_flags
);
1753 struct super_block
*sb
= mpd
->inode
->i_sb
;
1757 * If get block returns EAGAIN or ENOSPC and there
1758 * appears to be free blocks we will just let
1759 * mpage_da_submit_io() unlock all of the pages.
1764 if (err
== -ENOSPC
&& ext4_count_free_clusters(sb
)) {
1770 * get block failure will cause us to loop in
1771 * writepages, because a_ops->writepage won't be able
1772 * to make progress. The page will be redirtied by
1773 * writepage and writepages will again try to write
1776 if (!(EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
)) {
1777 ext4_msg(sb
, KERN_CRIT
,
1778 "delayed block allocation failed for inode %lu "
1779 "at logical offset %llu with max blocks %zd "
1780 "with error %d", mpd
->inode
->i_ino
,
1781 (unsigned long long) next
,
1782 mpd
->b_size
>> mpd
->inode
->i_blkbits
, err
);
1783 ext4_msg(sb
, KERN_CRIT
,
1784 "This should not happen!! Data will be lost");
1786 ext4_print_free_blocks(mpd
->inode
);
1788 /* invalidate all the pages */
1789 ext4_da_block_invalidatepages(mpd
);
1791 /* Mark this page range as having been completed */
1798 if (map
.m_flags
& EXT4_MAP_NEW
) {
1799 struct block_device
*bdev
= mpd
->inode
->i_sb
->s_bdev
;
1802 for (i
= 0; i
< map
.m_len
; i
++)
1803 unmap_underlying_metadata(bdev
, map
.m_pblk
+ i
);
1807 * Update on-disk size along with block allocation.
1809 disksize
= ((loff_t
) next
+ blks
) << mpd
->inode
->i_blkbits
;
1810 if (disksize
> i_size_read(mpd
->inode
))
1811 disksize
= i_size_read(mpd
->inode
);
1812 if (disksize
> EXT4_I(mpd
->inode
)->i_disksize
) {
1813 ext4_update_i_disksize(mpd
->inode
, disksize
);
1814 err
= ext4_mark_inode_dirty(handle
, mpd
->inode
);
1816 ext4_error(mpd
->inode
->i_sb
,
1817 "Failed to mark inode %lu dirty",
1822 mpage_da_submit_io(mpd
, mapp
);
1826 #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
1827 (1 << BH_Delay) | (1 << BH_Unwritten))
1830 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
1832 * @mpd->lbh - extent of blocks
1833 * @logical - logical number of the block in the file
1834 * @b_state - b_state of the buffer head added
1836 * the function is used to collect contig. blocks in same state
1838 static void mpage_add_bh_to_extent(struct mpage_da_data
*mpd
, sector_t logical
,
1839 unsigned long b_state
)
1842 int blkbits
= mpd
->inode
->i_blkbits
;
1843 int nrblocks
= mpd
->b_size
>> blkbits
;
1846 * XXX Don't go larger than mballoc is willing to allocate
1847 * This is a stopgap solution. We eventually need to fold
1848 * mpage_da_submit_io() into this function and then call
1849 * ext4_map_blocks() multiple times in a loop
1851 if (nrblocks
>= (8*1024*1024 >> blkbits
))
1854 /* check if the reserved journal credits might overflow */
1855 if (!ext4_test_inode_flag(mpd
->inode
, EXT4_INODE_EXTENTS
)) {
1856 if (nrblocks
>= EXT4_MAX_TRANS_DATA
) {
1858 * With non-extent format we are limited by the journal
1859 * credit available. Total credit needed to insert
1860 * nrblocks contiguous blocks is dependent on the
1861 * nrblocks. So limit nrblocks.
1867 * First block in the extent
1869 if (mpd
->b_size
== 0) {
1870 mpd
->b_blocknr
= logical
;
1871 mpd
->b_size
= 1 << blkbits
;
1872 mpd
->b_state
= b_state
& BH_FLAGS
;
1876 next
= mpd
->b_blocknr
+ nrblocks
;
1878 * Can we merge the block to our big extent?
1880 if (logical
== next
&& (b_state
& BH_FLAGS
) == mpd
->b_state
) {
1881 mpd
->b_size
+= 1 << blkbits
;
1887 * We couldn't merge the block to our extent, so we
1888 * need to flush current extent and start new one
1890 mpage_da_map_and_submit(mpd
);
1894 static int ext4_bh_delay_or_unwritten(handle_t
*handle
, struct buffer_head
*bh
)
1896 return (buffer_delay(bh
) || buffer_unwritten(bh
)) && buffer_dirty(bh
);
1900 * This function is grabs code from the very beginning of
1901 * ext4_map_blocks, but assumes that the caller is from delayed write
1902 * time. This function looks up the requested blocks and sets the
1903 * buffer delay bit under the protection of i_data_sem.
1905 static int ext4_da_map_blocks(struct inode
*inode
, sector_t iblock
,
1906 struct ext4_map_blocks
*map
,
1907 struct buffer_head
*bh
)
1909 struct extent_status es
;
1911 sector_t invalid_block
= ~((sector_t
) 0xffff);
1912 #ifdef ES_AGGRESSIVE_TEST
1913 struct ext4_map_blocks orig_map
;
1915 memcpy(&orig_map
, map
, sizeof(*map
));
1918 if (invalid_block
< ext4_blocks_count(EXT4_SB(inode
->i_sb
)->s_es
))
1922 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1923 "logical block %lu\n", inode
->i_ino
, map
->m_len
,
1924 (unsigned long) map
->m_lblk
);
1926 /* Lookup extent status tree firstly */
1927 if (ext4_es_lookup_extent(inode
, iblock
, &es
)) {
1929 if (ext4_es_is_hole(&es
)) {
1931 down_read((&EXT4_I(inode
)->i_data_sem
));
1936 * Delayed extent could be allocated by fallocate.
1937 * So we need to check it.
1939 if (ext4_es_is_delayed(&es
) && !ext4_es_is_unwritten(&es
)) {
1940 map_bh(bh
, inode
->i_sb
, invalid_block
);
1942 set_buffer_delay(bh
);
1946 map
->m_pblk
= ext4_es_pblock(&es
) + iblock
- es
.es_lblk
;
1947 retval
= es
.es_len
- (iblock
- es
.es_lblk
);
1948 if (retval
> map
->m_len
)
1949 retval
= map
->m_len
;
1950 map
->m_len
= retval
;
1951 if (ext4_es_is_written(&es
))
1952 map
->m_flags
|= EXT4_MAP_MAPPED
;
1953 else if (ext4_es_is_unwritten(&es
))
1954 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
1958 #ifdef ES_AGGRESSIVE_TEST
1959 ext4_map_blocks_es_recheck(NULL
, inode
, map
, &orig_map
, 0);
1965 * Try to see if we can get the block without requesting a new
1966 * file system block.
1968 down_read((&EXT4_I(inode
)->i_data_sem
));
1969 if (ext4_has_inline_data(inode
)) {
1971 * We will soon create blocks for this page, and let
1972 * us pretend as if the blocks aren't allocated yet.
1973 * In case of clusters, we have to handle the work
1974 * of mapping from cluster so that the reserved space
1975 * is calculated properly.
1977 if ((EXT4_SB(inode
->i_sb
)->s_cluster_ratio
> 1) &&
1978 ext4_find_delalloc_cluster(inode
, map
->m_lblk
))
1979 map
->m_flags
|= EXT4_MAP_FROM_CLUSTER
;
1981 } else if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
1982 retval
= ext4_ext_map_blocks(NULL
, inode
, map
,
1983 EXT4_GET_BLOCKS_NO_PUT_HOLE
);
1985 retval
= ext4_ind_map_blocks(NULL
, inode
, map
,
1986 EXT4_GET_BLOCKS_NO_PUT_HOLE
);
1992 * XXX: __block_prepare_write() unmaps passed block,
1996 * If the block was allocated from previously allocated cluster,
1997 * then we don't need to reserve it again. However we still need
1998 * to reserve metadata for every block we're going to write.
2000 if (!(map
->m_flags
& EXT4_MAP_FROM_CLUSTER
)) {
2001 ret
= ext4_da_reserve_space(inode
, iblock
);
2003 /* not enough space to reserve */
2008 ret
= ext4_da_reserve_metadata(inode
, iblock
);
2010 /* not enough space to reserve */
2016 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
, map
->m_len
,
2017 ~0, EXTENT_STATUS_DELAYED
);
2023 /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served
2024 * and it should not appear on the bh->b_state.
2026 map
->m_flags
&= ~EXT4_MAP_FROM_CLUSTER
;
2028 map_bh(bh
, inode
->i_sb
, invalid_block
);
2030 set_buffer_delay(bh
);
2031 } else if (retval
> 0) {
2033 unsigned long long status
;
2035 #ifdef ES_AGGRESSIVE_TEST
2036 if (retval
!= map
->m_len
) {
2037 printk("ES len assertation failed for inode: %lu "
2038 "retval %d != map->m_len %d "
2039 "in %s (lookup)\n", inode
->i_ino
, retval
,
2040 map
->m_len
, __func__
);
2044 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
2045 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
2046 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
, map
->m_len
,
2047 map
->m_pblk
, status
);
2053 up_read((&EXT4_I(inode
)->i_data_sem
));
2059 * This is a special get_blocks_t callback which is used by
2060 * ext4_da_write_begin(). It will either return mapped block or
2061 * reserve space for a single block.
2063 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2064 * We also have b_blocknr = -1 and b_bdev initialized properly
2066 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2067 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2068 * initialized properly.
2070 int ext4_da_get_block_prep(struct inode
*inode
, sector_t iblock
,
2071 struct buffer_head
*bh
, int create
)
2073 struct ext4_map_blocks map
;
2076 BUG_ON(create
== 0);
2077 BUG_ON(bh
->b_size
!= inode
->i_sb
->s_blocksize
);
2079 map
.m_lblk
= iblock
;
2083 * first, we need to know whether the block is allocated already
2084 * preallocated blocks are unmapped but should treated
2085 * the same as allocated blocks.
2087 ret
= ext4_da_map_blocks(inode
, iblock
, &map
, bh
);
2091 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
2092 bh
->b_state
= (bh
->b_state
& ~EXT4_MAP_FLAGS
) | map
.m_flags
;
2094 if (buffer_unwritten(bh
)) {
2095 /* A delayed write to unwritten bh should be marked
2096 * new and mapped. Mapped ensures that we don't do
2097 * get_block multiple times when we write to the same
2098 * offset and new ensures that we do proper zero out
2099 * for partial write.
2102 set_buffer_mapped(bh
);
2107 static int bget_one(handle_t
*handle
, struct buffer_head
*bh
)
2113 static int bput_one(handle_t
*handle
, struct buffer_head
*bh
)
2119 static int __ext4_journalled_writepage(struct page
*page
,
2122 struct address_space
*mapping
= page
->mapping
;
2123 struct inode
*inode
= mapping
->host
;
2124 struct buffer_head
*page_bufs
= NULL
;
2125 handle_t
*handle
= NULL
;
2126 int ret
= 0, err
= 0;
2127 int inline_data
= ext4_has_inline_data(inode
);
2128 struct buffer_head
*inode_bh
= NULL
;
2130 ClearPageChecked(page
);
2133 BUG_ON(page
->index
!= 0);
2134 BUG_ON(len
> ext4_get_max_inline_size(inode
));
2135 inode_bh
= ext4_journalled_write_inline_data(inode
, len
, page
);
2136 if (inode_bh
== NULL
)
2139 page_bufs
= page_buffers(page
);
2144 ext4_walk_page_buffers(handle
, page_bufs
, 0, len
,
2148 * We need to release the page lock before we start the
2149 * journal, so grab a reference so the page won't disappear
2150 * out from under us.
2155 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
2156 ext4_writepage_trans_blocks(inode
));
2157 if (IS_ERR(handle
)) {
2158 ret
= PTR_ERR(handle
);
2160 goto out_no_pagelock
;
2162 BUG_ON(!ext4_handle_valid(handle
));
2166 if (page
->mapping
!= mapping
) {
2167 /* The page got truncated from under us */
2168 ext4_journal_stop(handle
);
2174 ret
= ext4_journal_get_write_access(handle
, inode_bh
);
2176 err
= ext4_handle_dirty_metadata(handle
, inode
, inode_bh
);
2179 ret
= ext4_walk_page_buffers(handle
, page_bufs
, 0, len
, NULL
,
2180 do_journal_get_write_access
);
2182 err
= ext4_walk_page_buffers(handle
, page_bufs
, 0, len
, NULL
,
2187 EXT4_I(inode
)->i_datasync_tid
= handle
->h_transaction
->t_tid
;
2188 err
= ext4_journal_stop(handle
);
2192 if (!ext4_has_inline_data(inode
))
2193 ext4_walk_page_buffers(handle
, page_bufs
, 0, len
,
2195 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
2204 * Note that we don't need to start a transaction unless we're journaling data
2205 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2206 * need to file the inode to the transaction's list in ordered mode because if
2207 * we are writing back data added by write(), the inode is already there and if
2208 * we are writing back data modified via mmap(), no one guarantees in which
2209 * transaction the data will hit the disk. In case we are journaling data, we
2210 * cannot start transaction directly because transaction start ranks above page
2211 * lock so we have to do some magic.
2213 * This function can get called via...
2214 * - ext4_da_writepages after taking page lock (have journal handle)
2215 * - journal_submit_inode_data_buffers (no journal handle)
2216 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
2217 * - grab_page_cache when doing write_begin (have journal handle)
2219 * We don't do any block allocation in this function. If we have page with
2220 * multiple blocks we need to write those buffer_heads that are mapped. This
2221 * is important for mmaped based write. So if we do with blocksize 1K
2222 * truncate(f, 1024);
2223 * a = mmap(f, 0, 4096);
2225 * truncate(f, 4096);
2226 * we have in the page first buffer_head mapped via page_mkwrite call back
2227 * but other buffer_heads would be unmapped but dirty (dirty done via the
2228 * do_wp_page). So writepage should write the first block. If we modify
2229 * the mmap area beyond 1024 we will again get a page_fault and the
2230 * page_mkwrite callback will do the block allocation and mark the
2231 * buffer_heads mapped.
2233 * We redirty the page if we have any buffer_heads that is either delay or
2234 * unwritten in the page.
2236 * We can get recursively called as show below.
2238 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2241 * But since we don't do any block allocation we should not deadlock.
2242 * Page also have the dirty flag cleared so we don't get recurive page_lock.
2244 static int ext4_writepage(struct page
*page
,
2245 struct writeback_control
*wbc
)
2250 struct buffer_head
*page_bufs
= NULL
;
2251 struct inode
*inode
= page
->mapping
->host
;
2252 struct ext4_io_submit io_submit
;
2254 trace_ext4_writepage(page
);
2255 size
= i_size_read(inode
);
2256 if (page
->index
== size
>> PAGE_CACHE_SHIFT
)
2257 len
= size
& ~PAGE_CACHE_MASK
;
2259 len
= PAGE_CACHE_SIZE
;
2261 page_bufs
= page_buffers(page
);
2263 * We cannot do block allocation or other extent handling in this
2264 * function. If there are buffers needing that, we have to redirty
2265 * the page. But we may reach here when we do a journal commit via
2266 * journal_submit_inode_data_buffers() and in that case we must write
2267 * allocated buffers to achieve data=ordered mode guarantees.
2269 if (ext4_walk_page_buffers(NULL
, page_bufs
, 0, len
, NULL
,
2270 ext4_bh_delay_or_unwritten
)) {
2271 redirty_page_for_writepage(wbc
, page
);
2272 if (current
->flags
& PF_MEMALLOC
) {
2274 * For memory cleaning there's no point in writing only
2275 * some buffers. So just bail out. Warn if we came here
2276 * from direct reclaim.
2278 WARN_ON_ONCE((current
->flags
& (PF_MEMALLOC
|PF_KSWAPD
))
2285 if (PageChecked(page
) && ext4_should_journal_data(inode
))
2287 * It's mmapped pagecache. Add buffers and journal it. There
2288 * doesn't seem much point in redirtying the page here.
2290 return __ext4_journalled_writepage(page
, len
);
2292 memset(&io_submit
, 0, sizeof(io_submit
));
2293 ret
= ext4_bio_write_page(&io_submit
, page
, len
, wbc
);
2294 ext4_io_submit(&io_submit
);
2299 * This is called via ext4_da_writepages() to
2300 * calculate the total number of credits to reserve to fit
2301 * a single extent allocation into a single transaction,
2302 * ext4_da_writpeages() will loop calling this before
2303 * the block allocation.
2306 static int ext4_da_writepages_trans_blocks(struct inode
*inode
)
2308 int max_blocks
= EXT4_I(inode
)->i_reserved_data_blocks
;
2311 * With non-extent format the journal credit needed to
2312 * insert nrblocks contiguous block is dependent on
2313 * number of contiguous block. So we will limit
2314 * number of contiguous block to a sane value
2316 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) &&
2317 (max_blocks
> EXT4_MAX_TRANS_DATA
))
2318 max_blocks
= EXT4_MAX_TRANS_DATA
;
2320 return ext4_chunk_trans_blocks(inode
, max_blocks
);
2324 * write_cache_pages_da - walk the list of dirty pages of the given
2325 * address space and accumulate pages that need writing, and call
2326 * mpage_da_map_and_submit to map a single contiguous memory region
2327 * and then write them.
2329 static int write_cache_pages_da(handle_t
*handle
,
2330 struct address_space
*mapping
,
2331 struct writeback_control
*wbc
,
2332 struct mpage_da_data
*mpd
,
2333 pgoff_t
*done_index
)
2335 struct buffer_head
*bh
, *head
;
2336 struct inode
*inode
= mapping
->host
;
2337 struct pagevec pvec
;
2338 unsigned int nr_pages
;
2341 long nr_to_write
= wbc
->nr_to_write
;
2342 int i
, tag
, ret
= 0;
2344 memset(mpd
, 0, sizeof(struct mpage_da_data
));
2347 pagevec_init(&pvec
, 0);
2348 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2349 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2351 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2352 tag
= PAGECACHE_TAG_TOWRITE
;
2354 tag
= PAGECACHE_TAG_DIRTY
;
2356 *done_index
= index
;
2357 while (index
<= end
) {
2358 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
2359 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
2363 for (i
= 0; i
< nr_pages
; i
++) {
2364 struct page
*page
= pvec
.pages
[i
];
2367 * At this point, the page may be truncated or
2368 * invalidated (changing page->mapping to NULL), or
2369 * even swizzled back from swapper_space to tmpfs file
2370 * mapping. However, page->index will not change
2371 * because we have a reference on the page.
2373 if (page
->index
> end
)
2376 *done_index
= page
->index
+ 1;
2379 * If we can't merge this page, and we have
2380 * accumulated an contiguous region, write it
2382 if ((mpd
->next_page
!= page
->index
) &&
2383 (mpd
->next_page
!= mpd
->first_page
)) {
2384 mpage_da_map_and_submit(mpd
);
2385 goto ret_extent_tail
;
2391 * If the page is no longer dirty, or its
2392 * mapping no longer corresponds to inode we
2393 * are writing (which means it has been
2394 * truncated or invalidated), or the page is
2395 * already under writeback and we are not
2396 * doing a data integrity writeback, skip the page
2398 if (!PageDirty(page
) ||
2399 (PageWriteback(page
) &&
2400 (wbc
->sync_mode
== WB_SYNC_NONE
)) ||
2401 unlikely(page
->mapping
!= mapping
)) {
2406 wait_on_page_writeback(page
);
2407 BUG_ON(PageWriteback(page
));
2410 * If we have inline data and arrive here, it means that
2411 * we will soon create the block for the 1st page, so
2412 * we'd better clear the inline data here.
2414 if (ext4_has_inline_data(inode
)) {
2415 BUG_ON(ext4_test_inode_state(inode
,
2416 EXT4_STATE_MAY_INLINE_DATA
));
2417 ext4_destroy_inline_data(handle
, inode
);
2420 if (mpd
->next_page
!= page
->index
)
2421 mpd
->first_page
= page
->index
;
2422 mpd
->next_page
= page
->index
+ 1;
2423 logical
= (sector_t
) page
->index
<<
2424 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
2426 /* Add all dirty buffers to mpd */
2427 head
= page_buffers(page
);
2430 BUG_ON(buffer_locked(bh
));
2432 * We need to try to allocate unmapped blocks
2433 * in the same page. Otherwise we won't make
2434 * progress with the page in ext4_writepage
2436 if (ext4_bh_delay_or_unwritten(NULL
, bh
)) {
2437 mpage_add_bh_to_extent(mpd
, logical
,
2440 goto ret_extent_tail
;
2441 } else if (buffer_dirty(bh
) &&
2442 buffer_mapped(bh
)) {
2444 * mapped dirty buffer. We need to
2445 * update the b_state because we look
2446 * at b_state in mpage_da_map_blocks.
2447 * We don't update b_size because if we
2448 * find an unmapped buffer_head later
2449 * we need to use the b_state flag of
2452 if (mpd
->b_size
== 0)
2454 bh
->b_state
& BH_FLAGS
;
2457 } while ((bh
= bh
->b_this_page
) != head
);
2459 if (nr_to_write
> 0) {
2461 if (nr_to_write
== 0 &&
2462 wbc
->sync_mode
== WB_SYNC_NONE
)
2464 * We stop writing back only if we are
2465 * not doing integrity sync. In case of
2466 * integrity sync we have to keep going
2467 * because someone may be concurrently
2468 * dirtying pages, and we might have
2469 * synced a lot of newly appeared dirty
2470 * pages, but have not synced all of the
2476 pagevec_release(&pvec
);
2481 ret
= MPAGE_DA_EXTENT_TAIL
;
2483 pagevec_release(&pvec
);
2489 static int ext4_da_writepages(struct address_space
*mapping
,
2490 struct writeback_control
*wbc
)
2493 int range_whole
= 0;
2494 handle_t
*handle
= NULL
;
2495 struct mpage_da_data mpd
;
2496 struct inode
*inode
= mapping
->host
;
2497 int pages_written
= 0;
2498 unsigned int max_pages
;
2499 int range_cyclic
, cycled
= 1, io_done
= 0;
2500 int needed_blocks
, ret
= 0;
2501 long desired_nr_to_write
, nr_to_writebump
= 0;
2502 loff_t range_start
= wbc
->range_start
;
2503 struct ext4_sb_info
*sbi
= EXT4_SB(mapping
->host
->i_sb
);
2504 pgoff_t done_index
= 0;
2506 struct blk_plug plug
;
2508 trace_ext4_da_writepages(inode
, wbc
);
2511 * No pages to write? This is mainly a kludge to avoid starting
2512 * a transaction for special inodes like journal inode on last iput()
2513 * because that could violate lock ordering on umount
2515 if (!mapping
->nrpages
|| !mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
2519 * If the filesystem has aborted, it is read-only, so return
2520 * right away instead of dumping stack traces later on that
2521 * will obscure the real source of the problem. We test
2522 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
2523 * the latter could be true if the filesystem is mounted
2524 * read-only, and in that case, ext4_da_writepages should
2525 * *never* be called, so if that ever happens, we would want
2528 if (unlikely(sbi
->s_mount_flags
& EXT4_MF_FS_ABORTED
))
2531 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2534 range_cyclic
= wbc
->range_cyclic
;
2535 if (wbc
->range_cyclic
) {
2536 index
= mapping
->writeback_index
;
2539 wbc
->range_start
= index
<< PAGE_CACHE_SHIFT
;
2540 wbc
->range_end
= LLONG_MAX
;
2541 wbc
->range_cyclic
= 0;
2544 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2545 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2549 * This works around two forms of stupidity. The first is in
2550 * the writeback code, which caps the maximum number of pages
2551 * written to be 1024 pages. This is wrong on multiple
2552 * levels; different architectues have a different page size,
2553 * which changes the maximum amount of data which gets
2554 * written. Secondly, 4 megabytes is way too small. XFS
2555 * forces this value to be 16 megabytes by multiplying
2556 * nr_to_write parameter by four, and then relies on its
2557 * allocator to allocate larger extents to make them
2558 * contiguous. Unfortunately this brings us to the second
2559 * stupidity, which is that ext4's mballoc code only allocates
2560 * at most 2048 blocks. So we force contiguous writes up to
2561 * the number of dirty blocks in the inode, or
2562 * sbi->max_writeback_mb_bump whichever is smaller.
2564 max_pages
= sbi
->s_max_writeback_mb_bump
<< (20 - PAGE_CACHE_SHIFT
);
2565 if (!range_cyclic
&& range_whole
) {
2566 if (wbc
->nr_to_write
== LONG_MAX
)
2567 desired_nr_to_write
= wbc
->nr_to_write
;
2569 desired_nr_to_write
= wbc
->nr_to_write
* 8;
2571 desired_nr_to_write
= ext4_num_dirty_pages(inode
, index
,
2573 if (desired_nr_to_write
> max_pages
)
2574 desired_nr_to_write
= max_pages
;
2576 if (wbc
->nr_to_write
< desired_nr_to_write
) {
2577 nr_to_writebump
= desired_nr_to_write
- wbc
->nr_to_write
;
2578 wbc
->nr_to_write
= desired_nr_to_write
;
2582 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2583 tag_pages_for_writeback(mapping
, index
, end
);
2585 blk_start_plug(&plug
);
2586 while (!ret
&& wbc
->nr_to_write
> 0) {
2589 * we insert one extent at a time. So we need
2590 * credit needed for single extent allocation.
2591 * journalled mode is currently not supported
2594 BUG_ON(ext4_should_journal_data(inode
));
2595 needed_blocks
= ext4_da_writepages_trans_blocks(inode
);
2597 /* start a new transaction*/
2598 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
2600 if (IS_ERR(handle
)) {
2601 ret
= PTR_ERR(handle
);
2602 ext4_msg(inode
->i_sb
, KERN_CRIT
, "%s: jbd2_start: "
2603 "%ld pages, ino %lu; err %d", __func__
,
2604 wbc
->nr_to_write
, inode
->i_ino
, ret
);
2605 blk_finish_plug(&plug
);
2606 goto out_writepages
;
2610 * Now call write_cache_pages_da() to find the next
2611 * contiguous region of logical blocks that need
2612 * blocks to be allocated by ext4 and submit them.
2614 ret
= write_cache_pages_da(handle
, mapping
,
2615 wbc
, &mpd
, &done_index
);
2617 * If we have a contiguous extent of pages and we
2618 * haven't done the I/O yet, map the blocks and submit
2621 if (!mpd
.io_done
&& mpd
.next_page
!= mpd
.first_page
) {
2622 mpage_da_map_and_submit(&mpd
);
2623 ret
= MPAGE_DA_EXTENT_TAIL
;
2625 trace_ext4_da_write_pages(inode
, &mpd
);
2626 wbc
->nr_to_write
-= mpd
.pages_written
;
2628 ext4_journal_stop(handle
);
2630 if ((mpd
.retval
== -ENOSPC
) && sbi
->s_journal
) {
2631 /* commit the transaction which would
2632 * free blocks released in the transaction
2635 jbd2_journal_force_commit_nested(sbi
->s_journal
);
2637 } else if (ret
== MPAGE_DA_EXTENT_TAIL
) {
2639 * Got one extent now try with rest of the pages.
2640 * If mpd.retval is set -EIO, journal is aborted.
2641 * So we don't need to write any more.
2643 pages_written
+= mpd
.pages_written
;
2646 } else if (wbc
->nr_to_write
)
2648 * There is no more writeout needed
2649 * or we requested for a noblocking writeout
2650 * and we found the device congested
2654 blk_finish_plug(&plug
);
2655 if (!io_done
&& !cycled
) {
2658 wbc
->range_start
= index
<< PAGE_CACHE_SHIFT
;
2659 wbc
->range_end
= mapping
->writeback_index
- 1;
2664 wbc
->range_cyclic
= range_cyclic
;
2665 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
2667 * set the writeback_index so that range_cyclic
2668 * mode will write it back later
2670 mapping
->writeback_index
= done_index
;
2673 wbc
->nr_to_write
-= nr_to_writebump
;
2674 wbc
->range_start
= range_start
;
2675 trace_ext4_da_writepages_result(inode
, wbc
, ret
, pages_written
);
2679 static int ext4_nonda_switch(struct super_block
*sb
)
2681 s64 free_clusters
, dirty_clusters
;
2682 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2685 * switch to non delalloc mode if we are running low
2686 * on free block. The free block accounting via percpu
2687 * counters can get slightly wrong with percpu_counter_batch getting
2688 * accumulated on each CPU without updating global counters
2689 * Delalloc need an accurate free block accounting. So switch
2690 * to non delalloc when we are near to error range.
2693 percpu_counter_read_positive(&sbi
->s_freeclusters_counter
);
2695 percpu_counter_read_positive(&sbi
->s_dirtyclusters_counter
);
2697 * Start pushing delalloc when 1/2 of free blocks are dirty.
2699 if (dirty_clusters
&& (free_clusters
< 2 * dirty_clusters
))
2700 try_to_writeback_inodes_sb(sb
, WB_REASON_FS_FREE_SPACE
);
2702 if (2 * free_clusters
< 3 * dirty_clusters
||
2703 free_clusters
< (dirty_clusters
+ EXT4_FREECLUSTERS_WATERMARK
)) {
2705 * free block count is less than 150% of dirty blocks
2706 * or free blocks is less than watermark
2713 /* We always reserve for an inode update; the superblock could be there too */
2714 static int ext4_da_write_credits(struct inode
*inode
, loff_t pos
, unsigned len
)
2716 if (likely(EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
2717 EXT4_FEATURE_RO_COMPAT_LARGE_FILE
)))
2720 if (pos
+ len
<= 0x7fffffffULL
)
2723 /* We might need to update the superblock to set LARGE_FILE */
2727 static int ext4_da_write_begin(struct file
*file
, struct address_space
*mapping
,
2728 loff_t pos
, unsigned len
, unsigned flags
,
2729 struct page
**pagep
, void **fsdata
)
2731 int ret
, retries
= 0;
2734 struct inode
*inode
= mapping
->host
;
2737 index
= pos
>> PAGE_CACHE_SHIFT
;
2739 if (ext4_nonda_switch(inode
->i_sb
)) {
2740 *fsdata
= (void *)FALL_BACK_TO_NONDELALLOC
;
2741 return ext4_write_begin(file
, mapping
, pos
,
2742 len
, flags
, pagep
, fsdata
);
2744 *fsdata
= (void *)0;
2745 trace_ext4_da_write_begin(inode
, pos
, len
, flags
);
2747 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
2748 ret
= ext4_da_write_inline_data_begin(mapping
, inode
,
2758 * grab_cache_page_write_begin() can take a long time if the
2759 * system is thrashing due to memory pressure, or if the page
2760 * is being written back. So grab it first before we start
2761 * the transaction handle. This also allows us to allocate
2762 * the page (if needed) without using GFP_NOFS.
2765 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
2771 * With delayed allocation, we don't log the i_disksize update
2772 * if there is delayed block allocation. But we still need
2773 * to journalling the i_disksize update if writes to the end
2774 * of file which has an already mapped buffer.
2777 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
2778 ext4_da_write_credits(inode
, pos
, len
));
2779 if (IS_ERR(handle
)) {
2780 page_cache_release(page
);
2781 return PTR_ERR(handle
);
2785 if (page
->mapping
!= mapping
) {
2786 /* The page got truncated from under us */
2788 page_cache_release(page
);
2789 ext4_journal_stop(handle
);
2792 /* In case writeback began while the page was unlocked */
2793 wait_for_stable_page(page
);
2795 ret
= __block_write_begin(page
, pos
, len
, ext4_da_get_block_prep
);
2798 ext4_journal_stop(handle
);
2800 * block_write_begin may have instantiated a few blocks
2801 * outside i_size. Trim these off again. Don't need
2802 * i_size_read because we hold i_mutex.
2804 if (pos
+ len
> inode
->i_size
)
2805 ext4_truncate_failed_write(inode
);
2807 if (ret
== -ENOSPC
&&
2808 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
2811 page_cache_release(page
);
2820 * Check if we should update i_disksize
2821 * when write to the end of file but not require block allocation
2823 static int ext4_da_should_update_i_disksize(struct page
*page
,
2824 unsigned long offset
)
2826 struct buffer_head
*bh
;
2827 struct inode
*inode
= page
->mapping
->host
;
2831 bh
= page_buffers(page
);
2832 idx
= offset
>> inode
->i_blkbits
;
2834 for (i
= 0; i
< idx
; i
++)
2835 bh
= bh
->b_this_page
;
2837 if (!buffer_mapped(bh
) || (buffer_delay(bh
)) || buffer_unwritten(bh
))
2842 static int ext4_da_write_end(struct file
*file
,
2843 struct address_space
*mapping
,
2844 loff_t pos
, unsigned len
, unsigned copied
,
2845 struct page
*page
, void *fsdata
)
2847 struct inode
*inode
= mapping
->host
;
2849 handle_t
*handle
= ext4_journal_current_handle();
2851 unsigned long start
, end
;
2852 int write_mode
= (int)(unsigned long)fsdata
;
2854 if (write_mode
== FALL_BACK_TO_NONDELALLOC
)
2855 return ext4_write_end(file
, mapping
, pos
,
2856 len
, copied
, page
, fsdata
);
2858 trace_ext4_da_write_end(inode
, pos
, len
, copied
);
2859 start
= pos
& (PAGE_CACHE_SIZE
- 1);
2860 end
= start
+ copied
- 1;
2863 * generic_write_end() will run mark_inode_dirty() if i_size
2864 * changes. So let's piggyback the i_disksize mark_inode_dirty
2867 new_i_size
= pos
+ copied
;
2868 if (copied
&& new_i_size
> EXT4_I(inode
)->i_disksize
) {
2869 if (ext4_has_inline_data(inode
) ||
2870 ext4_da_should_update_i_disksize(page
, end
)) {
2871 down_write(&EXT4_I(inode
)->i_data_sem
);
2872 if (new_i_size
> EXT4_I(inode
)->i_disksize
)
2873 EXT4_I(inode
)->i_disksize
= new_i_size
;
2874 up_write(&EXT4_I(inode
)->i_data_sem
);
2875 /* We need to mark inode dirty even if
2876 * new_i_size is less that inode->i_size
2877 * bu greater than i_disksize.(hint delalloc)
2879 ext4_mark_inode_dirty(handle
, inode
);
2883 if (write_mode
!= CONVERT_INLINE_DATA
&&
2884 ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
) &&
2885 ext4_has_inline_data(inode
))
2886 ret2
= ext4_da_write_inline_data_end(inode
, pos
, len
, copied
,
2889 ret2
= generic_write_end(file
, mapping
, pos
, len
, copied
,
2895 ret2
= ext4_journal_stop(handle
);
2899 return ret
? ret
: copied
;
2902 static void ext4_da_invalidatepage(struct page
*page
, unsigned long offset
)
2905 * Drop reserved blocks
2907 BUG_ON(!PageLocked(page
));
2908 if (!page_has_buffers(page
))
2911 ext4_da_page_release_reservation(page
, offset
);
2914 ext4_invalidatepage(page
, offset
);
2920 * Force all delayed allocation blocks to be allocated for a given inode.
2922 int ext4_alloc_da_blocks(struct inode
*inode
)
2924 trace_ext4_alloc_da_blocks(inode
);
2926 if (!EXT4_I(inode
)->i_reserved_data_blocks
&&
2927 !EXT4_I(inode
)->i_reserved_meta_blocks
)
2931 * We do something simple for now. The filemap_flush() will
2932 * also start triggering a write of the data blocks, which is
2933 * not strictly speaking necessary (and for users of
2934 * laptop_mode, not even desirable). However, to do otherwise
2935 * would require replicating code paths in:
2937 * ext4_da_writepages() ->
2938 * write_cache_pages() ---> (via passed in callback function)
2939 * __mpage_da_writepage() -->
2940 * mpage_add_bh_to_extent()
2941 * mpage_da_map_blocks()
2943 * The problem is that write_cache_pages(), located in
2944 * mm/page-writeback.c, marks pages clean in preparation for
2945 * doing I/O, which is not desirable if we're not planning on
2948 * We could call write_cache_pages(), and then redirty all of
2949 * the pages by calling redirty_page_for_writepage() but that
2950 * would be ugly in the extreme. So instead we would need to
2951 * replicate parts of the code in the above functions,
2952 * simplifying them because we wouldn't actually intend to
2953 * write out the pages, but rather only collect contiguous
2954 * logical block extents, call the multi-block allocator, and
2955 * then update the buffer heads with the block allocations.
2957 * For now, though, we'll cheat by calling filemap_flush(),
2958 * which will map the blocks, and start the I/O, but not
2959 * actually wait for the I/O to complete.
2961 return filemap_flush(inode
->i_mapping
);
2965 * bmap() is special. It gets used by applications such as lilo and by
2966 * the swapper to find the on-disk block of a specific piece of data.
2968 * Naturally, this is dangerous if the block concerned is still in the
2969 * journal. If somebody makes a swapfile on an ext4 data-journaling
2970 * filesystem and enables swap, then they may get a nasty shock when the
2971 * data getting swapped to that swapfile suddenly gets overwritten by
2972 * the original zero's written out previously to the journal and
2973 * awaiting writeback in the kernel's buffer cache.
2975 * So, if we see any bmap calls here on a modified, data-journaled file,
2976 * take extra steps to flush any blocks which might be in the cache.
2978 static sector_t
ext4_bmap(struct address_space
*mapping
, sector_t block
)
2980 struct inode
*inode
= mapping
->host
;
2985 * We can get here for an inline file via the FIBMAP ioctl
2987 if (ext4_has_inline_data(inode
))
2990 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
) &&
2991 test_opt(inode
->i_sb
, DELALLOC
)) {
2993 * With delalloc we want to sync the file
2994 * so that we can make sure we allocate
2997 filemap_write_and_wait(mapping
);
3000 if (EXT4_JOURNAL(inode
) &&
3001 ext4_test_inode_state(inode
, EXT4_STATE_JDATA
)) {
3003 * This is a REALLY heavyweight approach, but the use of
3004 * bmap on dirty files is expected to be extremely rare:
3005 * only if we run lilo or swapon on a freshly made file
3006 * do we expect this to happen.
3008 * (bmap requires CAP_SYS_RAWIO so this does not
3009 * represent an unprivileged user DOS attack --- we'd be
3010 * in trouble if mortal users could trigger this path at
3013 * NB. EXT4_STATE_JDATA is not set on files other than
3014 * regular files. If somebody wants to bmap a directory
3015 * or symlink and gets confused because the buffer
3016 * hasn't yet been flushed to disk, they deserve
3017 * everything they get.
3020 ext4_clear_inode_state(inode
, EXT4_STATE_JDATA
);
3021 journal
= EXT4_JOURNAL(inode
);
3022 jbd2_journal_lock_updates(journal
);
3023 err
= jbd2_journal_flush(journal
);
3024 jbd2_journal_unlock_updates(journal
);
3030 return generic_block_bmap(mapping
, block
, ext4_get_block
);
3033 static int ext4_readpage(struct file
*file
, struct page
*page
)
3036 struct inode
*inode
= page
->mapping
->host
;
3038 trace_ext4_readpage(page
);
3040 if (ext4_has_inline_data(inode
))
3041 ret
= ext4_readpage_inline(inode
, page
);
3044 return mpage_readpage(page
, ext4_get_block
);
3050 ext4_readpages(struct file
*file
, struct address_space
*mapping
,
3051 struct list_head
*pages
, unsigned nr_pages
)
3053 struct inode
*inode
= mapping
->host
;
3055 /* If the file has inline data, no need to do readpages. */
3056 if (ext4_has_inline_data(inode
))
3059 return mpage_readpages(mapping
, pages
, nr_pages
, ext4_get_block
);
3062 static void ext4_invalidatepage(struct page
*page
, unsigned long offset
)
3064 trace_ext4_invalidatepage(page
, offset
);
3066 /* No journalling happens on data buffers when this function is used */
3067 WARN_ON(page_has_buffers(page
) && buffer_jbd(page_buffers(page
)));
3069 block_invalidatepage(page
, offset
);
3072 static int __ext4_journalled_invalidatepage(struct page
*page
,
3073 unsigned long offset
)
3075 journal_t
*journal
= EXT4_JOURNAL(page
->mapping
->host
);
3077 trace_ext4_journalled_invalidatepage(page
, offset
);
3080 * If it's a full truncate we just forget about the pending dirtying
3083 ClearPageChecked(page
);
3085 return jbd2_journal_invalidatepage(journal
, page
, offset
);
3088 /* Wrapper for aops... */
3089 static void ext4_journalled_invalidatepage(struct page
*page
,
3090 unsigned long offset
)
3092 WARN_ON(__ext4_journalled_invalidatepage(page
, offset
) < 0);
3095 static int ext4_releasepage(struct page
*page
, gfp_t wait
)
3097 journal_t
*journal
= EXT4_JOURNAL(page
->mapping
->host
);
3099 trace_ext4_releasepage(page
);
3101 /* Page has dirty journalled data -> cannot release */
3102 if (PageChecked(page
))
3105 return jbd2_journal_try_to_free_buffers(journal
, page
, wait
);
3107 return try_to_free_buffers(page
);
3111 * ext4_get_block used when preparing for a DIO write or buffer write.
3112 * We allocate an uinitialized extent if blocks haven't been allocated.
3113 * The extent will be converted to initialized after the IO is complete.
3115 int ext4_get_block_write(struct inode
*inode
, sector_t iblock
,
3116 struct buffer_head
*bh_result
, int create
)
3118 ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
3119 inode
->i_ino
, create
);
3120 return _ext4_get_block(inode
, iblock
, bh_result
,
3121 EXT4_GET_BLOCKS_IO_CREATE_EXT
);
3124 static int ext4_get_block_write_nolock(struct inode
*inode
, sector_t iblock
,
3125 struct buffer_head
*bh_result
, int create
)
3127 ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
3128 inode
->i_ino
, create
);
3129 return _ext4_get_block(inode
, iblock
, bh_result
,
3130 EXT4_GET_BLOCKS_NO_LOCK
);
3133 static void ext4_end_io_dio(struct kiocb
*iocb
, loff_t offset
,
3134 ssize_t size
, void *private, int ret
,
3137 struct inode
*inode
= file_inode(iocb
->ki_filp
);
3138 ext4_io_end_t
*io_end
= iocb
->private;
3140 /* if not async direct IO or dio with 0 bytes write, just return */
3141 if (!io_end
|| !size
)
3144 ext_debug("ext4_end_io_dio(): io_end 0x%p "
3145 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
3146 iocb
->private, io_end
->inode
->i_ino
, iocb
, offset
,
3149 iocb
->private = NULL
;
3151 /* if not aio dio with unwritten extents, just free io and return */
3152 if (!(io_end
->flag
& EXT4_IO_END_UNWRITTEN
)) {
3153 ext4_free_io_end(io_end
);
3155 inode_dio_done(inode
);
3157 aio_complete(iocb
, ret
, 0);
3161 io_end
->offset
= offset
;
3162 io_end
->size
= size
;
3164 io_end
->iocb
= iocb
;
3165 io_end
->result
= ret
;
3168 ext4_add_complete_io(io_end
);
3172 * For ext4 extent files, ext4 will do direct-io write to holes,
3173 * preallocated extents, and those write extend the file, no need to
3174 * fall back to buffered IO.
3176 * For holes, we fallocate those blocks, mark them as uninitialized
3177 * If those blocks were preallocated, we mark sure they are split, but
3178 * still keep the range to write as uninitialized.
3180 * The unwritten extents will be converted to written when DIO is completed.
3181 * For async direct IO, since the IO may still pending when return, we
3182 * set up an end_io call back function, which will do the conversion
3183 * when async direct IO completed.
3185 * If the O_DIRECT write will extend the file then add this inode to the
3186 * orphan list. So recovery will truncate it back to the original size
3187 * if the machine crashes during the write.
3190 static ssize_t
ext4_ext_direct_IO(int rw
, struct kiocb
*iocb
,
3191 const struct iovec
*iov
, loff_t offset
,
3192 unsigned long nr_segs
)
3194 struct file
*file
= iocb
->ki_filp
;
3195 struct inode
*inode
= file
->f_mapping
->host
;
3197 size_t count
= iov_length(iov
, nr_segs
);
3199 get_block_t
*get_block_func
= NULL
;
3201 loff_t final_size
= offset
+ count
;
3203 /* Use the old path for reads and writes beyond i_size. */
3204 if (rw
!= WRITE
|| final_size
> inode
->i_size
)
3205 return ext4_ind_direct_IO(rw
, iocb
, iov
, offset
, nr_segs
);
3207 BUG_ON(iocb
->private == NULL
);
3209 /* If we do a overwrite dio, i_mutex locking can be released */
3210 overwrite
= *((int *)iocb
->private);
3213 atomic_inc(&inode
->i_dio_count
);
3214 down_read(&EXT4_I(inode
)->i_data_sem
);
3215 mutex_unlock(&inode
->i_mutex
);
3219 * We could direct write to holes and fallocate.
3221 * Allocated blocks to fill the hole are marked as
3222 * uninitialized to prevent parallel buffered read to expose
3223 * the stale data before DIO complete the data IO.
3225 * As to previously fallocated extents, ext4 get_block will
3226 * just simply mark the buffer mapped but still keep the
3227 * extents uninitialized.
3229 * For non AIO case, we will convert those unwritten extents
3230 * to written after return back from blockdev_direct_IO.
3232 * For async DIO, the conversion needs to be deferred when the
3233 * IO is completed. The ext4 end_io callback function will be
3234 * called to take care of the conversion work. Here for async
3235 * case, we allocate an io_end structure to hook to the iocb.
3237 iocb
->private = NULL
;
3238 ext4_inode_aio_set(inode
, NULL
);
3239 if (!is_sync_kiocb(iocb
)) {
3240 ext4_io_end_t
*io_end
= ext4_init_io_end(inode
, GFP_NOFS
);
3245 io_end
->flag
|= EXT4_IO_END_DIRECT
;
3246 iocb
->private = io_end
;
3248 * we save the io structure for current async direct
3249 * IO, so that later ext4_map_blocks() could flag the
3250 * io structure whether there is a unwritten extents
3251 * needs to be converted when IO is completed.
3253 ext4_inode_aio_set(inode
, io_end
);
3257 get_block_func
= ext4_get_block_write_nolock
;
3259 get_block_func
= ext4_get_block_write
;
3260 dio_flags
= DIO_LOCKING
;
3262 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
3263 inode
->i_sb
->s_bdev
, iov
,
3271 ext4_inode_aio_set(inode
, NULL
);
3273 * The io_end structure takes a reference to the inode, that
3274 * structure needs to be destroyed and the reference to the
3275 * inode need to be dropped, when IO is complete, even with 0
3276 * byte write, or failed.
3278 * In the successful AIO DIO case, the io_end structure will
3279 * be destroyed and the reference to the inode will be dropped
3280 * after the end_io call back function is called.
3282 * In the case there is 0 byte write, or error case, since VFS
3283 * direct IO won't invoke the end_io call back function, we
3284 * need to free the end_io structure here.
3286 if (ret
!= -EIOCBQUEUED
&& ret
<= 0 && iocb
->private) {
3287 ext4_free_io_end(iocb
->private);
3288 iocb
->private = NULL
;
3289 } else if (ret
> 0 && !overwrite
&& ext4_test_inode_state(inode
,
3290 EXT4_STATE_DIO_UNWRITTEN
)) {
3293 * for non AIO case, since the IO is already
3294 * completed, we could do the conversion right here
3296 err
= ext4_convert_unwritten_extents(inode
,
3300 ext4_clear_inode_state(inode
, EXT4_STATE_DIO_UNWRITTEN
);
3304 /* take i_mutex locking again if we do a ovewrite dio */
3306 inode_dio_done(inode
);
3307 up_read(&EXT4_I(inode
)->i_data_sem
);
3308 mutex_lock(&inode
->i_mutex
);
3314 static ssize_t
ext4_direct_IO(int rw
, struct kiocb
*iocb
,
3315 const struct iovec
*iov
, loff_t offset
,
3316 unsigned long nr_segs
)
3318 struct file
*file
= iocb
->ki_filp
;
3319 struct inode
*inode
= file
->f_mapping
->host
;
3323 * If we are doing data journalling we don't support O_DIRECT
3325 if (ext4_should_journal_data(inode
))
3328 /* Let buffer I/O handle the inline data case. */
3329 if (ext4_has_inline_data(inode
))
3332 trace_ext4_direct_IO_enter(inode
, offset
, iov_length(iov
, nr_segs
), rw
);
3333 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3334 ret
= ext4_ext_direct_IO(rw
, iocb
, iov
, offset
, nr_segs
);
3336 ret
= ext4_ind_direct_IO(rw
, iocb
, iov
, offset
, nr_segs
);
3337 trace_ext4_direct_IO_exit(inode
, offset
,
3338 iov_length(iov
, nr_segs
), rw
, ret
);
3343 * Pages can be marked dirty completely asynchronously from ext4's journalling
3344 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3345 * much here because ->set_page_dirty is called under VFS locks. The page is
3346 * not necessarily locked.
3348 * We cannot just dirty the page and leave attached buffers clean, because the
3349 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3350 * or jbddirty because all the journalling code will explode.
3352 * So what we do is to mark the page "pending dirty" and next time writepage
3353 * is called, propagate that into the buffers appropriately.
3355 static int ext4_journalled_set_page_dirty(struct page
*page
)
3357 SetPageChecked(page
);
3358 return __set_page_dirty_nobuffers(page
);
3361 static const struct address_space_operations ext4_aops
= {
3362 .readpage
= ext4_readpage
,
3363 .readpages
= ext4_readpages
,
3364 .writepage
= ext4_writepage
,
3365 .write_begin
= ext4_write_begin
,
3366 .write_end
= ext4_write_end
,
3368 .invalidatepage
= ext4_invalidatepage
,
3369 .releasepage
= ext4_releasepage
,
3370 .direct_IO
= ext4_direct_IO
,
3371 .migratepage
= buffer_migrate_page
,
3372 .is_partially_uptodate
= block_is_partially_uptodate
,
3373 .error_remove_page
= generic_error_remove_page
,
3376 static const struct address_space_operations ext4_journalled_aops
= {
3377 .readpage
= ext4_readpage
,
3378 .readpages
= ext4_readpages
,
3379 .writepage
= ext4_writepage
,
3380 .write_begin
= ext4_write_begin
,
3381 .write_end
= ext4_journalled_write_end
,
3382 .set_page_dirty
= ext4_journalled_set_page_dirty
,
3384 .invalidatepage
= ext4_journalled_invalidatepage
,
3385 .releasepage
= ext4_releasepage
,
3386 .direct_IO
= ext4_direct_IO
,
3387 .is_partially_uptodate
= block_is_partially_uptodate
,
3388 .error_remove_page
= generic_error_remove_page
,
3391 static const struct address_space_operations ext4_da_aops
= {
3392 .readpage
= ext4_readpage
,
3393 .readpages
= ext4_readpages
,
3394 .writepage
= ext4_writepage
,
3395 .writepages
= ext4_da_writepages
,
3396 .write_begin
= ext4_da_write_begin
,
3397 .write_end
= ext4_da_write_end
,
3399 .invalidatepage
= ext4_da_invalidatepage
,
3400 .releasepage
= ext4_releasepage
,
3401 .direct_IO
= ext4_direct_IO
,
3402 .migratepage
= buffer_migrate_page
,
3403 .is_partially_uptodate
= block_is_partially_uptodate
,
3404 .error_remove_page
= generic_error_remove_page
,
3407 void ext4_set_aops(struct inode
*inode
)
3409 switch (ext4_inode_journal_mode(inode
)) {
3410 case EXT4_INODE_ORDERED_DATA_MODE
:
3411 ext4_set_inode_state(inode
, EXT4_STATE_ORDERED_MODE
);
3413 case EXT4_INODE_WRITEBACK_DATA_MODE
:
3414 ext4_clear_inode_state(inode
, EXT4_STATE_ORDERED_MODE
);
3416 case EXT4_INODE_JOURNAL_DATA_MODE
:
3417 inode
->i_mapping
->a_ops
= &ext4_journalled_aops
;
3422 if (test_opt(inode
->i_sb
, DELALLOC
))
3423 inode
->i_mapping
->a_ops
= &ext4_da_aops
;
3425 inode
->i_mapping
->a_ops
= &ext4_aops
;
3430 * ext4_discard_partial_page_buffers()
3431 * Wrapper function for ext4_discard_partial_page_buffers_no_lock.
3432 * This function finds and locks the page containing the offset
3433 * "from" and passes it to ext4_discard_partial_page_buffers_no_lock.
3434 * Calling functions that already have the page locked should call
3435 * ext4_discard_partial_page_buffers_no_lock directly.
3437 int ext4_discard_partial_page_buffers(handle_t
*handle
,
3438 struct address_space
*mapping
, loff_t from
,
3439 loff_t length
, int flags
)
3441 struct inode
*inode
= mapping
->host
;
3445 page
= find_or_create_page(mapping
, from
>> PAGE_CACHE_SHIFT
,
3446 mapping_gfp_mask(mapping
) & ~__GFP_FS
);
3450 err
= ext4_discard_partial_page_buffers_no_lock(handle
, inode
, page
,
3451 from
, length
, flags
);
3454 page_cache_release(page
);
3459 * ext4_discard_partial_page_buffers_no_lock()
3460 * Zeros a page range of length 'length' starting from offset 'from'.
3461 * Buffer heads that correspond to the block aligned regions of the
3462 * zeroed range will be unmapped. Unblock aligned regions
3463 * will have the corresponding buffer head mapped if needed so that
3464 * that region of the page can be updated with the partial zero out.
3466 * This function assumes that the page has already been locked. The
3467 * The range to be discarded must be contained with in the given page.
3468 * If the specified range exceeds the end of the page it will be shortened
3469 * to the end of the page that corresponds to 'from'. This function is
3470 * appropriate for updating a page and it buffer heads to be unmapped and
3471 * zeroed for blocks that have been either released, or are going to be
3474 * handle: The journal handle
3475 * inode: The files inode
3476 * page: A locked page that contains the offset "from"
3477 * from: The starting byte offset (from the beginning of the file)
3478 * to begin discarding
3479 * len: The length of bytes to discard
3480 * flags: Optional flags that may be used:
3482 * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED
3483 * Only zero the regions of the page whose buffer heads
3484 * have already been unmapped. This flag is appropriate
3485 * for updating the contents of a page whose blocks may
3486 * have already been released, and we only want to zero
3487 * out the regions that correspond to those released blocks.
3489 * Returns zero on success or negative on failure.
3491 static int ext4_discard_partial_page_buffers_no_lock(handle_t
*handle
,
3492 struct inode
*inode
, struct page
*page
, loff_t from
,
3493 loff_t length
, int flags
)
3495 ext4_fsblk_t index
= from
>> PAGE_CACHE_SHIFT
;
3496 unsigned int offset
= from
& (PAGE_CACHE_SIZE
-1);
3497 unsigned int blocksize
, max
, pos
;
3499 struct buffer_head
*bh
;
3502 blocksize
= inode
->i_sb
->s_blocksize
;
3503 max
= PAGE_CACHE_SIZE
- offset
;
3505 if (index
!= page
->index
)
3509 * correct length if it does not fall between
3510 * 'from' and the end of the page
3512 if (length
> max
|| length
< 0)
3515 iblock
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
3517 if (!page_has_buffers(page
))
3518 create_empty_buffers(page
, blocksize
, 0);
3520 /* Find the buffer that contains "offset" */
3521 bh
= page_buffers(page
);
3523 while (offset
>= pos
) {
3524 bh
= bh
->b_this_page
;
3530 while (pos
< offset
+ length
) {
3531 unsigned int end_of_block
, range_to_discard
;
3535 /* The length of space left to zero and unmap */
3536 range_to_discard
= offset
+ length
- pos
;
3538 /* The length of space until the end of the block */
3539 end_of_block
= blocksize
- (pos
& (blocksize
-1));
3542 * Do not unmap or zero past end of block
3543 * for this buffer head
3545 if (range_to_discard
> end_of_block
)
3546 range_to_discard
= end_of_block
;
3550 * Skip this buffer head if we are only zeroing unampped
3551 * regions of the page
3553 if (flags
& EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED
&&
3557 /* If the range is block aligned, unmap */
3558 if (range_to_discard
== blocksize
) {
3559 clear_buffer_dirty(bh
);
3561 clear_buffer_mapped(bh
);
3562 clear_buffer_req(bh
);
3563 clear_buffer_new(bh
);
3564 clear_buffer_delay(bh
);
3565 clear_buffer_unwritten(bh
);
3566 clear_buffer_uptodate(bh
);
3567 zero_user(page
, pos
, range_to_discard
);
3568 BUFFER_TRACE(bh
, "Buffer discarded");
3573 * If this block is not completely contained in the range
3574 * to be discarded, then it is not going to be released. Because
3575 * we need to keep this block, we need to make sure this part
3576 * of the page is uptodate before we modify it by writeing
3577 * partial zeros on it.
3579 if (!buffer_mapped(bh
)) {
3581 * Buffer head must be mapped before we can read
3584 BUFFER_TRACE(bh
, "unmapped");
3585 ext4_get_block(inode
, iblock
, bh
, 0);
3586 /* unmapped? It's a hole - nothing to do */
3587 if (!buffer_mapped(bh
)) {
3588 BUFFER_TRACE(bh
, "still unmapped");
3593 /* Ok, it's mapped. Make sure it's up-to-date */
3594 if (PageUptodate(page
))
3595 set_buffer_uptodate(bh
);
3597 if (!buffer_uptodate(bh
)) {
3599 ll_rw_block(READ
, 1, &bh
);
3601 /* Uhhuh. Read error. Complain and punt.*/
3602 if (!buffer_uptodate(bh
))
3606 if (ext4_should_journal_data(inode
)) {
3607 BUFFER_TRACE(bh
, "get write access");
3608 err
= ext4_journal_get_write_access(handle
, bh
);
3613 zero_user(page
, pos
, range_to_discard
);
3616 if (ext4_should_journal_data(inode
)) {
3617 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
3619 mark_buffer_dirty(bh
);
3621 BUFFER_TRACE(bh
, "Partial buffer zeroed");
3623 bh
= bh
->b_this_page
;
3625 pos
+= range_to_discard
;
3631 int ext4_can_truncate(struct inode
*inode
)
3633 if (S_ISREG(inode
->i_mode
))
3635 if (S_ISDIR(inode
->i_mode
))
3637 if (S_ISLNK(inode
->i_mode
))
3638 return !ext4_inode_is_fast_symlink(inode
);
3643 * ext4_punch_hole: punches a hole in a file by releasing the blocks
3644 * associated with the given offset and length
3646 * @inode: File inode
3647 * @offset: The offset where the hole will begin
3648 * @len: The length of the hole
3650 * Returns: 0 on success or negative on failure
3653 int ext4_punch_hole(struct file
*file
, loff_t offset
, loff_t length
)
3656 struct inode
*inode
= file_inode(file
);
3657 struct super_block
*sb
= inode
->i_sb
;
3658 ext4_lblk_t first_block
, stop_block
;
3659 struct address_space
*mapping
= inode
->i_mapping
;
3660 loff_t first_page
, last_page
, page_len
;
3661 loff_t first_page_offset
, last_page_offset
;
3663 unsigned int credits
;
3666 if (!S_ISREG(inode
->i_mode
))
3669 if (EXT4_SB(sb
)->s_cluster_ratio
> 1) {
3670 /* TODO: Add support for bigalloc file systems */
3674 trace_ext4_punch_hole(inode
, offset
, length
);
3677 * Write out all dirty pages to avoid race conditions
3678 * Then release them.
3680 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
)) {
3681 ret
= filemap_write_and_wait_range(mapping
, offset
,
3682 offset
+ length
- 1);
3687 mutex_lock(&inode
->i_mutex
);
3688 /* It's not possible punch hole on append only file */
3689 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
)) {
3693 if (IS_SWAPFILE(inode
)) {
3698 /* No need to punch hole beyond i_size */
3699 if (offset
>= inode
->i_size
)
3703 * If the hole extends beyond i_size, set the hole
3704 * to end after the page that contains i_size
3706 if (offset
+ length
> inode
->i_size
) {
3707 length
= inode
->i_size
+
3708 PAGE_CACHE_SIZE
- (inode
->i_size
& (PAGE_CACHE_SIZE
- 1)) -
3712 first_page
= (offset
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
3713 last_page
= (offset
+ length
) >> PAGE_CACHE_SHIFT
;
3715 first_page_offset
= first_page
<< PAGE_CACHE_SHIFT
;
3716 last_page_offset
= last_page
<< PAGE_CACHE_SHIFT
;
3718 /* Now release the pages */
3719 if (last_page_offset
> first_page_offset
) {
3720 truncate_pagecache_range(inode
, first_page_offset
,
3721 last_page_offset
- 1);
3724 /* Wait all existing dio workers, newcomers will block on i_mutex */
3725 ext4_inode_block_unlocked_dio(inode
);
3726 ret
= ext4_flush_unwritten_io(inode
);
3729 inode_dio_wait(inode
);
3731 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3732 credits
= ext4_writepage_trans_blocks(inode
);
3734 credits
= ext4_blocks_for_truncate(inode
);
3735 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
3736 if (IS_ERR(handle
)) {
3737 ret
= PTR_ERR(handle
);
3738 ext4_std_error(sb
, ret
);
3743 * Now we need to zero out the non-page-aligned data in the
3744 * pages at the start and tail of the hole, and unmap the
3745 * buffer heads for the block aligned regions of the page that
3746 * were completely zeroed.
3748 if (first_page
> last_page
) {
3750 * If the file space being truncated is contained
3751 * within a page just zero out and unmap the middle of
3754 ret
= ext4_discard_partial_page_buffers(handle
,
3755 mapping
, offset
, length
, 0);
3761 * zero out and unmap the partial page that contains
3762 * the start of the hole
3764 page_len
= first_page_offset
- offset
;
3766 ret
= ext4_discard_partial_page_buffers(handle
, mapping
,
3767 offset
, page_len
, 0);
3773 * zero out and unmap the partial page that contains
3774 * the end of the hole
3776 page_len
= offset
+ length
- last_page_offset
;
3778 ret
= ext4_discard_partial_page_buffers(handle
, mapping
,
3779 last_page_offset
, page_len
, 0);
3786 * If i_size is contained in the last page, we need to
3787 * unmap and zero the partial page after i_size
3789 if (inode
->i_size
>> PAGE_CACHE_SHIFT
== last_page
&&
3790 inode
->i_size
% PAGE_CACHE_SIZE
!= 0) {
3791 page_len
= PAGE_CACHE_SIZE
-
3792 (inode
->i_size
& (PAGE_CACHE_SIZE
- 1));
3795 ret
= ext4_discard_partial_page_buffers(handle
,
3796 mapping
, inode
->i_size
, page_len
, 0);
3803 first_block
= (offset
+ sb
->s_blocksize
- 1) >>
3804 EXT4_BLOCK_SIZE_BITS(sb
);
3805 stop_block
= (offset
+ length
) >> EXT4_BLOCK_SIZE_BITS(sb
);
3807 /* If there are no blocks to remove, return now */
3808 if (first_block
>= stop_block
)
3811 down_write(&EXT4_I(inode
)->i_data_sem
);
3812 ext4_discard_preallocations(inode
);
3814 ret
= ext4_es_remove_extent(inode
, first_block
,
3815 stop_block
- first_block
);
3817 up_write(&EXT4_I(inode
)->i_data_sem
);
3821 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3822 ret
= ext4_ext_remove_space(inode
, first_block
,
3825 ret
= ext4_free_hole_blocks(handle
, inode
, first_block
,
3828 ext4_discard_preallocations(inode
);
3829 up_write(&EXT4_I(inode
)->i_data_sem
);
3831 ext4_handle_sync(handle
);
3832 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3833 ext4_mark_inode_dirty(handle
, inode
);
3835 ext4_journal_stop(handle
);
3837 ext4_inode_resume_unlocked_dio(inode
);
3839 mutex_unlock(&inode
->i_mutex
);
3843 * Disabled as per b/28760453
3852 * We block out ext4_get_block() block instantiations across the entire
3853 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
3854 * simultaneously on behalf of the same inode.
3856 * As we work through the truncate and commit bits of it to the journal there
3857 * is one core, guiding principle: the file's tree must always be consistent on
3858 * disk. We must be able to restart the truncate after a crash.
3860 * The file's tree may be transiently inconsistent in memory (although it
3861 * probably isn't), but whenever we close off and commit a journal transaction,
3862 * the contents of (the filesystem + the journal) must be consistent and
3863 * restartable. It's pretty simple, really: bottom up, right to left (although
3864 * left-to-right works OK too).
3866 * Note that at recovery time, journal replay occurs *before* the restart of
3867 * truncate against the orphan inode list.
3869 * The committed inode has the new, desired i_size (which is the same as
3870 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
3871 * that this inode's truncate did not complete and it will again call
3872 * ext4_truncate() to have another go. So there will be instantiated blocks
3873 * to the right of the truncation point in a crashed ext4 filesystem. But
3874 * that's fine - as long as they are linked from the inode, the post-crash
3875 * ext4_truncate() run will find them and release them.
3877 void ext4_truncate(struct inode
*inode
)
3879 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3880 unsigned int credits
;
3882 struct address_space
*mapping
= inode
->i_mapping
;
3886 * There is a possibility that we're either freeing the inode
3887 * or it completely new indode. In those cases we might not
3888 * have i_mutex locked because it's not necessary.
3890 if (!(inode
->i_state
& (I_NEW
|I_FREEING
)))
3891 WARN_ON(!mutex_is_locked(&inode
->i_mutex
));
3892 trace_ext4_truncate_enter(inode
);
3894 if (!ext4_can_truncate(inode
))
3897 ext4_clear_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3899 if (inode
->i_size
== 0 && !test_opt(inode
->i_sb
, NO_AUTO_DA_ALLOC
))
3900 ext4_set_inode_state(inode
, EXT4_STATE_DA_ALLOC_CLOSE
);
3902 if (ext4_has_inline_data(inode
)) {
3905 ext4_inline_data_truncate(inode
, &has_inline
);
3911 * finish any pending end_io work so we won't run the risk of
3912 * converting any truncated blocks to initialized later
3914 ext4_flush_unwritten_io(inode
);
3916 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3917 credits
= ext4_writepage_trans_blocks(inode
);
3919 credits
= ext4_blocks_for_truncate(inode
);
3921 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
3922 if (IS_ERR(handle
)) {
3923 ext4_std_error(inode
->i_sb
, PTR_ERR(handle
));
3927 if (inode
->i_size
% PAGE_CACHE_SIZE
!= 0) {
3928 page_len
= PAGE_CACHE_SIZE
-
3929 (inode
->i_size
& (PAGE_CACHE_SIZE
- 1));
3931 if (ext4_discard_partial_page_buffers(handle
,
3932 mapping
, inode
->i_size
, page_len
, 0))
3937 * We add the inode to the orphan list, so that if this
3938 * truncate spans multiple transactions, and we crash, we will
3939 * resume the truncate when the filesystem recovers. It also
3940 * marks the inode dirty, to catch the new size.
3942 * Implication: the file must always be in a sane, consistent
3943 * truncatable state while each transaction commits.
3945 if (ext4_orphan_add(handle
, inode
))
3948 down_write(&EXT4_I(inode
)->i_data_sem
);
3950 ext4_discard_preallocations(inode
);
3952 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3953 ext4_ext_truncate(handle
, inode
);
3955 ext4_ind_truncate(handle
, inode
);
3957 up_write(&ei
->i_data_sem
);
3960 ext4_handle_sync(handle
);
3964 * If this was a simple ftruncate() and the file will remain alive,
3965 * then we need to clear up the orphan record which we created above.
3966 * However, if this was a real unlink then we were called by
3967 * ext4_delete_inode(), and we allow that function to clean up the
3968 * orphan info for us.
3971 ext4_orphan_del(handle
, inode
);
3973 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3974 ext4_mark_inode_dirty(handle
, inode
);
3975 ext4_journal_stop(handle
);
3977 trace_ext4_truncate_exit(inode
);
3981 * ext4_get_inode_loc returns with an extra refcount against the inode's
3982 * underlying buffer_head on success. If 'in_mem' is true, we have all
3983 * data in memory that is needed to recreate the on-disk version of this
3986 static int __ext4_get_inode_loc(struct inode
*inode
,
3987 struct ext4_iloc
*iloc
, int in_mem
)
3989 struct ext4_group_desc
*gdp
;
3990 struct buffer_head
*bh
;
3991 struct super_block
*sb
= inode
->i_sb
;
3993 int inodes_per_block
, inode_offset
;
3996 if (!ext4_valid_inum(sb
, inode
->i_ino
))
3999 iloc
->block_group
= (inode
->i_ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
4000 gdp
= ext4_get_group_desc(sb
, iloc
->block_group
, NULL
);
4005 * Figure out the offset within the block group inode table
4007 inodes_per_block
= EXT4_SB(sb
)->s_inodes_per_block
;
4008 inode_offset
= ((inode
->i_ino
- 1) %
4009 EXT4_INODES_PER_GROUP(sb
));
4010 block
= ext4_inode_table(sb
, gdp
) + (inode_offset
/ inodes_per_block
);
4011 iloc
->offset
= (inode_offset
% inodes_per_block
) * EXT4_INODE_SIZE(sb
);
4013 bh
= sb_getblk(sb
, block
);
4016 if (!buffer_uptodate(bh
)) {
4020 * If the buffer has the write error flag, we have failed
4021 * to write out another inode in the same block. In this
4022 * case, we don't have to read the block because we may
4023 * read the old inode data successfully.
4025 if (buffer_write_io_error(bh
) && !buffer_uptodate(bh
))
4026 set_buffer_uptodate(bh
);
4028 if (buffer_uptodate(bh
)) {
4029 /* someone brought it uptodate while we waited */
4035 * If we have all information of the inode in memory and this
4036 * is the only valid inode in the block, we need not read the
4040 struct buffer_head
*bitmap_bh
;
4043 start
= inode_offset
& ~(inodes_per_block
- 1);
4045 /* Is the inode bitmap in cache? */
4046 bitmap_bh
= sb_getblk(sb
, ext4_inode_bitmap(sb
, gdp
));
4047 if (unlikely(!bitmap_bh
))
4051 * If the inode bitmap isn't in cache then the
4052 * optimisation may end up performing two reads instead
4053 * of one, so skip it.
4055 if (!buffer_uptodate(bitmap_bh
)) {
4059 for (i
= start
; i
< start
+ inodes_per_block
; i
++) {
4060 if (i
== inode_offset
)
4062 if (ext4_test_bit(i
, bitmap_bh
->b_data
))
4066 if (i
== start
+ inodes_per_block
) {
4067 /* all other inodes are free, so skip I/O */
4068 memset(bh
->b_data
, 0, bh
->b_size
);
4069 set_buffer_uptodate(bh
);
4077 * If we need to do any I/O, try to pre-readahead extra
4078 * blocks from the inode table.
4080 if (EXT4_SB(sb
)->s_inode_readahead_blks
) {
4081 ext4_fsblk_t b
, end
, table
;
4083 __u32 ra_blks
= EXT4_SB(sb
)->s_inode_readahead_blks
;
4085 table
= ext4_inode_table(sb
, gdp
);
4086 /* s_inode_readahead_blks is always a power of 2 */
4087 b
= block
& ~((ext4_fsblk_t
) ra_blks
- 1);
4091 num
= EXT4_INODES_PER_GROUP(sb
);
4092 if (ext4_has_group_desc_csum(sb
))
4093 num
-= ext4_itable_unused_count(sb
, gdp
);
4094 table
+= num
/ inodes_per_block
;
4098 sb_breadahead(sb
, b
++);
4102 * There are other valid inodes in the buffer, this inode
4103 * has in-inode xattrs, or we don't have this inode in memory.
4104 * Read the block from disk.
4106 trace_ext4_load_inode(inode
);
4108 bh
->b_end_io
= end_buffer_read_sync
;
4109 #ifdef FEATURE_STORAGE_META_LOG
4110 if( bh
&& bh
->b_bdev
&& bh
->b_bdev
->bd_disk
)
4111 set_metadata_rw_status(bh
->b_bdev
->bd_disk
->first_minor
, WAIT_READ_CNT
);
4113 submit_bh(READ
| REQ_META
| REQ_PRIO
, bh
);
4115 if (!buffer_uptodate(bh
)) {
4116 EXT4_ERROR_INODE_BLOCK(inode
, block
,
4117 "unable to read itable block");
4127 int ext4_get_inode_loc(struct inode
*inode
, struct ext4_iloc
*iloc
)
4129 /* We have all inode data except xattrs in memory here. */
4130 return __ext4_get_inode_loc(inode
, iloc
,
4131 !ext4_test_inode_state(inode
, EXT4_STATE_XATTR
));
4134 void ext4_set_inode_flags(struct inode
*inode
)
4136 unsigned int flags
= EXT4_I(inode
)->i_flags
;
4137 unsigned int new_fl
= 0;
4139 if (flags
& EXT4_SYNC_FL
)
4141 if (flags
& EXT4_APPEND_FL
)
4143 if (flags
& EXT4_IMMUTABLE_FL
)
4144 new_fl
|= S_IMMUTABLE
;
4145 if (flags
& EXT4_NOATIME_FL
)
4146 new_fl
|= S_NOATIME
;
4147 if (flags
& EXT4_DIRSYNC_FL
)
4148 new_fl
|= S_DIRSYNC
;
4149 set_mask_bits(&inode
->i_flags
,
4150 S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
, new_fl
);
4153 /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4154 void ext4_get_inode_flags(struct ext4_inode_info
*ei
)
4156 unsigned int vfs_fl
;
4157 unsigned long old_fl
, new_fl
;
4160 vfs_fl
= ei
->vfs_inode
.i_flags
;
4161 old_fl
= ei
->i_flags
;
4162 new_fl
= old_fl
& ~(EXT4_SYNC_FL
|EXT4_APPEND_FL
|
4163 EXT4_IMMUTABLE_FL
|EXT4_NOATIME_FL
|
4165 if (vfs_fl
& S_SYNC
)
4166 new_fl
|= EXT4_SYNC_FL
;
4167 if (vfs_fl
& S_APPEND
)
4168 new_fl
|= EXT4_APPEND_FL
;
4169 if (vfs_fl
& S_IMMUTABLE
)
4170 new_fl
|= EXT4_IMMUTABLE_FL
;
4171 if (vfs_fl
& S_NOATIME
)
4172 new_fl
|= EXT4_NOATIME_FL
;
4173 if (vfs_fl
& S_DIRSYNC
)
4174 new_fl
|= EXT4_DIRSYNC_FL
;
4175 } while (cmpxchg(&ei
->i_flags
, old_fl
, new_fl
) != old_fl
);
4178 static blkcnt_t
ext4_inode_blocks(struct ext4_inode
*raw_inode
,
4179 struct ext4_inode_info
*ei
)
4182 struct inode
*inode
= &(ei
->vfs_inode
);
4183 struct super_block
*sb
= inode
->i_sb
;
4185 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
,
4186 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
)) {
4187 /* we are using combined 48 bit field */
4188 i_blocks
= ((u64
)le16_to_cpu(raw_inode
->i_blocks_high
)) << 32 |
4189 le32_to_cpu(raw_inode
->i_blocks_lo
);
4190 if (ext4_test_inode_flag(inode
, EXT4_INODE_HUGE_FILE
)) {
4191 /* i_blocks represent file system block size */
4192 return i_blocks
<< (inode
->i_blkbits
- 9);
4197 return le32_to_cpu(raw_inode
->i_blocks_lo
);
4201 static inline void ext4_iget_extra_inode(struct inode
*inode
,
4202 struct ext4_inode
*raw_inode
,
4203 struct ext4_inode_info
*ei
)
4205 __le32
*magic
= (void *)raw_inode
+
4206 EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
;
4207 if (*magic
== cpu_to_le32(EXT4_XATTR_MAGIC
)) {
4208 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
4209 ext4_find_inline_data_nolock(inode
);
4211 EXT4_I(inode
)->i_inline_off
= 0;
4214 struct inode
*ext4_iget(struct super_block
*sb
, unsigned long ino
)
4216 struct ext4_iloc iloc
;
4217 struct ext4_inode
*raw_inode
;
4218 struct ext4_inode_info
*ei
;
4219 struct inode
*inode
;
4220 journal_t
*journal
= EXT4_SB(sb
)->s_journal
;
4227 inode
= iget_locked(sb
, ino
);
4229 return ERR_PTR(-ENOMEM
);
4230 if (!(inode
->i_state
& I_NEW
))
4236 ret
= __ext4_get_inode_loc(inode
, &iloc
, 0);
4239 raw_inode
= ext4_raw_inode(&iloc
);
4241 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4242 ei
->i_extra_isize
= le16_to_cpu(raw_inode
->i_extra_isize
);
4243 if (EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
>
4244 EXT4_INODE_SIZE(inode
->i_sb
)) {
4245 EXT4_ERROR_INODE(inode
, "bad extra_isize (%u != %u)",
4246 EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
,
4247 EXT4_INODE_SIZE(inode
->i_sb
));
4252 ei
->i_extra_isize
= 0;
4254 /* Precompute checksum seed for inode metadata */
4255 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
,
4256 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
)) {
4257 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
4259 __le32 inum
= cpu_to_le32(inode
->i_ino
);
4260 __le32 gen
= raw_inode
->i_generation
;
4261 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&inum
,
4263 ei
->i_csum_seed
= ext4_chksum(sbi
, csum
, (__u8
*)&gen
,
4267 if (!ext4_inode_csum_verify(inode
, raw_inode
, ei
)) {
4268 EXT4_ERROR_INODE(inode
, "checksum invalid");
4273 inode
->i_mode
= le16_to_cpu(raw_inode
->i_mode
);
4274 i_uid
= (uid_t
)le16_to_cpu(raw_inode
->i_uid_low
);
4275 i_gid
= (gid_t
)le16_to_cpu(raw_inode
->i_gid_low
);
4276 if (!(test_opt(inode
->i_sb
, NO_UID32
))) {
4277 i_uid
|= le16_to_cpu(raw_inode
->i_uid_high
) << 16;
4278 i_gid
|= le16_to_cpu(raw_inode
->i_gid_high
) << 16;
4280 i_uid_write(inode
, i_uid
);
4281 i_gid_write(inode
, i_gid
);
4282 set_nlink(inode
, le16_to_cpu(raw_inode
->i_links_count
));
4284 ext4_clear_state_flags(ei
); /* Only relevant on 32-bit archs */
4285 ei
->i_inline_off
= 0;
4286 ei
->i_dir_start_lookup
= 0;
4287 ei
->i_dtime
= le32_to_cpu(raw_inode
->i_dtime
);
4288 /* We now have enough fields to check if the inode was active or not.
4289 * This is needed because nfsd might try to access dead inodes
4290 * the test is that same one that e2fsck uses
4291 * NeilBrown 1999oct15
4293 if (inode
->i_nlink
== 0) {
4294 if ((inode
->i_mode
== 0 ||
4295 !(EXT4_SB(inode
->i_sb
)->s_mount_state
& EXT4_ORPHAN_FS
)) &&
4296 ino
!= EXT4_BOOT_LOADER_INO
) {
4297 /* this inode is deleted */
4301 /* The only unlinked inodes we let through here have
4302 * valid i_mode and are being read by the orphan
4303 * recovery code: that's fine, we're about to complete
4304 * the process of deleting those.
4305 * OR it is the EXT4_BOOT_LOADER_INO which is
4306 * not initialized on a new filesystem. */
4308 ei
->i_flags
= le32_to_cpu(raw_inode
->i_flags
);
4309 inode
->i_blocks
= ext4_inode_blocks(raw_inode
, ei
);
4310 ei
->i_file_acl
= le32_to_cpu(raw_inode
->i_file_acl_lo
);
4311 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_64BIT
))
4313 ((__u64
)le16_to_cpu(raw_inode
->i_file_acl_high
)) << 32;
4314 inode
->i_size
= ext4_isize(raw_inode
);
4315 if ((size
= i_size_read(inode
)) < 0) {
4316 EXT4_ERROR_INODE(inode
, "bad i_size value: %lld", size
);
4320 ei
->i_disksize
= inode
->i_size
;
4322 ei
->i_reserved_quota
= 0;
4324 inode
->i_generation
= le32_to_cpu(raw_inode
->i_generation
);
4325 ei
->i_block_group
= iloc
.block_group
;
4326 ei
->i_last_alloc_group
= ~0;
4328 * NOTE! The in-memory inode i_data array is in little-endian order
4329 * even on big-endian machines: we do NOT byteswap the block numbers!
4331 for (block
= 0; block
< EXT4_N_BLOCKS
; block
++)
4332 ei
->i_data
[block
] = raw_inode
->i_block
[block
];
4333 INIT_LIST_HEAD(&ei
->i_orphan
);
4336 * Set transaction id's of transactions that have to be committed
4337 * to finish f[data]sync. We set them to currently running transaction
4338 * as we cannot be sure that the inode or some of its metadata isn't
4339 * part of the transaction - the inode could have been reclaimed and
4340 * now it is reread from disk.
4343 transaction_t
*transaction
;
4346 read_lock(&journal
->j_state_lock
);
4347 if (journal
->j_running_transaction
)
4348 transaction
= journal
->j_running_transaction
;
4350 transaction
= journal
->j_committing_transaction
;
4352 tid
= transaction
->t_tid
;
4354 tid
= journal
->j_commit_sequence
;
4355 read_unlock(&journal
->j_state_lock
);
4356 ei
->i_sync_tid
= tid
;
4357 ei
->i_datasync_tid
= tid
;
4360 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4361 if (ei
->i_extra_isize
== 0) {
4362 /* The extra space is currently unused. Use it. */
4363 ei
->i_extra_isize
= sizeof(struct ext4_inode
) -
4364 EXT4_GOOD_OLD_INODE_SIZE
;
4366 ext4_iget_extra_inode(inode
, raw_inode
, ei
);
4370 EXT4_INODE_GET_XTIME(i_ctime
, inode
, raw_inode
);
4371 EXT4_INODE_GET_XTIME(i_mtime
, inode
, raw_inode
);
4372 EXT4_INODE_GET_XTIME(i_atime
, inode
, raw_inode
);
4373 EXT4_EINODE_GET_XTIME(i_crtime
, ei
, raw_inode
);
4375 inode
->i_version
= le32_to_cpu(raw_inode
->i_disk_version
);
4376 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4377 if (EXT4_FITS_IN_INODE(raw_inode
, ei
, i_version_hi
))
4379 (__u64
)(le32_to_cpu(raw_inode
->i_version_hi
)) << 32;
4383 if (ei
->i_file_acl
&&
4384 !ext4_data_block_valid(EXT4_SB(sb
), ei
->i_file_acl
, 1)) {
4385 EXT4_ERROR_INODE(inode
, "bad extended attribute block %llu",
4389 } else if (!ext4_has_inline_data(inode
)) {
4390 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
4391 if ((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
4392 (S_ISLNK(inode
->i_mode
) &&
4393 !ext4_inode_is_fast_symlink(inode
))))
4394 /* Validate extent which is part of inode */
4395 ret
= ext4_ext_check_inode(inode
);
4396 } else if (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
4397 (S_ISLNK(inode
->i_mode
) &&
4398 !ext4_inode_is_fast_symlink(inode
))) {
4399 /* Validate block references which are part of inode */
4400 ret
= ext4_ind_check_inode(inode
);
4406 if (S_ISREG(inode
->i_mode
)) {
4407 inode
->i_op
= &ext4_file_inode_operations
;
4408 inode
->i_fop
= &ext4_file_operations
;
4409 ext4_set_aops(inode
);
4410 } else if (S_ISDIR(inode
->i_mode
)) {
4411 inode
->i_op
= &ext4_dir_inode_operations
;
4412 inode
->i_fop
= &ext4_dir_operations
;
4413 } else if (S_ISLNK(inode
->i_mode
)) {
4414 if (ext4_inode_is_fast_symlink(inode
)) {
4415 inode
->i_op
= &ext4_fast_symlink_inode_operations
;
4416 nd_terminate_link(ei
->i_data
, inode
->i_size
,
4417 sizeof(ei
->i_data
) - 1);
4419 inode
->i_op
= &ext4_symlink_inode_operations
;
4420 ext4_set_aops(inode
);
4422 } else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
) ||
4423 S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
4424 inode
->i_op
= &ext4_special_inode_operations
;
4425 if (raw_inode
->i_block
[0])
4426 init_special_inode(inode
, inode
->i_mode
,
4427 old_decode_dev(le32_to_cpu(raw_inode
->i_block
[0])));
4429 init_special_inode(inode
, inode
->i_mode
,
4430 new_decode_dev(le32_to_cpu(raw_inode
->i_block
[1])));
4431 } else if (ino
== EXT4_BOOT_LOADER_INO
) {
4432 make_bad_inode(inode
);
4435 EXT4_ERROR_INODE(inode
, "bogus i_mode (%o)", inode
->i_mode
);
4439 ext4_set_inode_flags(inode
);
4440 unlock_new_inode(inode
);
4446 return ERR_PTR(ret
);
4449 struct inode
*ext4_iget_normal(struct super_block
*sb
, unsigned long ino
)
4451 if (ino
< EXT4_FIRST_INO(sb
) && ino
!= EXT4_ROOT_INO
)
4452 return ERR_PTR(-EIO
);
4453 return ext4_iget(sb
, ino
);
4456 static int ext4_inode_blocks_set(handle_t
*handle
,
4457 struct ext4_inode
*raw_inode
,
4458 struct ext4_inode_info
*ei
)
4460 struct inode
*inode
= &(ei
->vfs_inode
);
4461 u64 i_blocks
= inode
->i_blocks
;
4462 struct super_block
*sb
= inode
->i_sb
;
4464 if (i_blocks
<= ~0U) {
4466 * i_blocks can be represented in a 32 bit variable
4467 * as multiple of 512 bytes
4469 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4470 raw_inode
->i_blocks_high
= 0;
4471 ext4_clear_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4474 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb
, EXT4_FEATURE_RO_COMPAT_HUGE_FILE
))
4477 if (i_blocks
<= 0xffffffffffffULL
) {
4479 * i_blocks can be represented in a 48 bit variable
4480 * as multiple of 512 bytes
4482 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4483 raw_inode
->i_blocks_high
= cpu_to_le16(i_blocks
>> 32);
4484 ext4_clear_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4486 ext4_set_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4487 /* i_block is stored in file system block size */
4488 i_blocks
= i_blocks
>> (inode
->i_blkbits
- 9);
4489 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4490 raw_inode
->i_blocks_high
= cpu_to_le16(i_blocks
>> 32);
4496 * Post the struct inode info into an on-disk inode location in the
4497 * buffer-cache. This gobbles the caller's reference to the
4498 * buffer_head in the inode location struct.
4500 * The caller must have write access to iloc->bh.
4502 static int ext4_do_update_inode(handle_t
*handle
,
4503 struct inode
*inode
,
4504 struct ext4_iloc
*iloc
)
4506 struct ext4_inode
*raw_inode
= ext4_raw_inode(iloc
);
4507 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4508 struct buffer_head
*bh
= iloc
->bh
;
4509 int err
= 0, rc
, block
;
4510 int need_datasync
= 0;
4514 /* For fields not not tracking in the in-memory inode,
4515 * initialise them to zero for new inodes. */
4516 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
))
4517 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
4519 ext4_get_inode_flags(ei
);
4520 raw_inode
->i_mode
= cpu_to_le16(inode
->i_mode
);
4521 i_uid
= i_uid_read(inode
);
4522 i_gid
= i_gid_read(inode
);
4523 if (!(test_opt(inode
->i_sb
, NO_UID32
))) {
4524 raw_inode
->i_uid_low
= cpu_to_le16(low_16_bits(i_uid
));
4525 raw_inode
->i_gid_low
= cpu_to_le16(low_16_bits(i_gid
));
4527 * Fix up interoperability with old kernels. Otherwise, old inodes get
4528 * re-used with the upper 16 bits of the uid/gid intact
4530 if (ei
->i_dtime
&& list_empty(&ei
->i_orphan
)) {
4531 raw_inode
->i_uid_high
= 0;
4532 raw_inode
->i_gid_high
= 0;
4534 raw_inode
->i_uid_high
=
4535 cpu_to_le16(high_16_bits(i_uid
));
4536 raw_inode
->i_gid_high
=
4537 cpu_to_le16(high_16_bits(i_gid
));
4540 raw_inode
->i_uid_low
= cpu_to_le16(fs_high2lowuid(i_uid
));
4541 raw_inode
->i_gid_low
= cpu_to_le16(fs_high2lowgid(i_gid
));
4542 raw_inode
->i_uid_high
= 0;
4543 raw_inode
->i_gid_high
= 0;
4545 raw_inode
->i_links_count
= cpu_to_le16(inode
->i_nlink
);
4547 EXT4_INODE_SET_XTIME(i_ctime
, inode
, raw_inode
);
4548 EXT4_INODE_SET_XTIME(i_mtime
, inode
, raw_inode
);
4549 EXT4_INODE_SET_XTIME(i_atime
, inode
, raw_inode
);
4550 EXT4_EINODE_SET_XTIME(i_crtime
, ei
, raw_inode
);
4552 if (ext4_inode_blocks_set(handle
, raw_inode
, ei
))
4554 raw_inode
->i_dtime
= cpu_to_le32(ei
->i_dtime
);
4555 raw_inode
->i_flags
= cpu_to_le32(ei
->i_flags
& 0xFFFFFFFF);
4556 if (EXT4_SB(inode
->i_sb
)->s_es
->s_creator_os
!=
4557 cpu_to_le32(EXT4_OS_HURD
))
4558 raw_inode
->i_file_acl_high
=
4559 cpu_to_le16(ei
->i_file_acl
>> 32);
4560 raw_inode
->i_file_acl_lo
= cpu_to_le32(ei
->i_file_acl
);
4561 if (ei
->i_disksize
!= ext4_isize(raw_inode
)) {
4562 ext4_isize_set(raw_inode
, ei
->i_disksize
);
4565 if (ei
->i_disksize
> 0x7fffffffULL
) {
4566 struct super_block
*sb
= inode
->i_sb
;
4567 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb
,
4568 EXT4_FEATURE_RO_COMPAT_LARGE_FILE
) ||
4569 EXT4_SB(sb
)->s_es
->s_rev_level
==
4570 cpu_to_le32(EXT4_GOOD_OLD_REV
)) {
4571 /* If this is the first large file
4572 * created, add a flag to the superblock.
4574 err
= ext4_journal_get_write_access(handle
,
4575 EXT4_SB(sb
)->s_sbh
);
4578 ext4_update_dynamic_rev(sb
);
4579 EXT4_SET_RO_COMPAT_FEATURE(sb
,
4580 EXT4_FEATURE_RO_COMPAT_LARGE_FILE
);
4581 ext4_handle_sync(handle
);
4582 err
= ext4_handle_dirty_super(handle
, sb
);
4585 raw_inode
->i_generation
= cpu_to_le32(inode
->i_generation
);
4586 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) {
4587 if (old_valid_dev(inode
->i_rdev
)) {
4588 raw_inode
->i_block
[0] =
4589 cpu_to_le32(old_encode_dev(inode
->i_rdev
));
4590 raw_inode
->i_block
[1] = 0;
4592 raw_inode
->i_block
[0] = 0;
4593 raw_inode
->i_block
[1] =
4594 cpu_to_le32(new_encode_dev(inode
->i_rdev
));
4595 raw_inode
->i_block
[2] = 0;
4597 } else if (!ext4_has_inline_data(inode
)) {
4598 for (block
= 0; block
< EXT4_N_BLOCKS
; block
++)
4599 raw_inode
->i_block
[block
] = ei
->i_data
[block
];
4602 raw_inode
->i_disk_version
= cpu_to_le32(inode
->i_version
);
4603 if (ei
->i_extra_isize
) {
4604 if (EXT4_FITS_IN_INODE(raw_inode
, ei
, i_version_hi
))
4605 raw_inode
->i_version_hi
=
4606 cpu_to_le32(inode
->i_version
>> 32);
4607 raw_inode
->i_extra_isize
= cpu_to_le16(ei
->i_extra_isize
);
4610 ext4_inode_csum_set(inode
, raw_inode
, ei
);
4612 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
4613 rc
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
4616 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
4618 ext4_update_inode_fsync_trans(handle
, inode
, need_datasync
);
4621 ext4_std_error(inode
->i_sb
, err
);
4626 * ext4_write_inode()
4628 * We are called from a few places:
4630 * - Within generic_file_write() for O_SYNC files.
4631 * Here, there will be no transaction running. We wait for any running
4632 * transaction to commit.
4634 * - Within sys_sync(), kupdate and such.
4635 * We wait on commit, if tol to.
4637 * - Within prune_icache() (PF_MEMALLOC == true)
4638 * Here we simply return. We can't afford to block kswapd on the
4641 * In all cases it is actually safe for us to return without doing anything,
4642 * because the inode has been copied into a raw inode buffer in
4643 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
4646 * Note that we are absolutely dependent upon all inode dirtiers doing the
4647 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4648 * which we are interested.
4650 * It would be a bug for them to not do this. The code:
4652 * mark_inode_dirty(inode)
4654 * inode->i_size = expr;
4656 * is in error because a kswapd-driven write_inode() could occur while
4657 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4658 * will no longer be on the superblock's dirty inode list.
4660 int ext4_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4664 if (current
->flags
& PF_MEMALLOC
)
4667 if (EXT4_SB(inode
->i_sb
)->s_journal
) {
4668 if (ext4_journal_current_handle()) {
4669 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4674 if (wbc
->sync_mode
!= WB_SYNC_ALL
)
4677 err
= ext4_force_commit(inode
->i_sb
);
4679 struct ext4_iloc iloc
;
4681 err
= __ext4_get_inode_loc(inode
, &iloc
, 0);
4684 if (wbc
->sync_mode
== WB_SYNC_ALL
)
4685 sync_dirty_buffer(iloc
.bh
);
4686 if (buffer_req(iloc
.bh
) && !buffer_uptodate(iloc
.bh
)) {
4687 EXT4_ERROR_INODE_BLOCK(inode
, iloc
.bh
->b_blocknr
,
4688 "IO error syncing inode");
4697 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
4698 * buffers that are attached to a page stradding i_size and are undergoing
4699 * commit. In that case we have to wait for commit to finish and try again.
4701 static void ext4_wait_for_tail_page_commit(struct inode
*inode
)
4705 journal_t
*journal
= EXT4_SB(inode
->i_sb
)->s_journal
;
4706 tid_t commit_tid
= 0;
4709 offset
= inode
->i_size
& (PAGE_CACHE_SIZE
- 1);
4711 * All buffers in the last page remain valid? Then there's nothing to
4712 * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
4715 if (offset
> PAGE_CACHE_SIZE
- (1 << inode
->i_blkbits
))
4718 page
= find_lock_page(inode
->i_mapping
,
4719 inode
->i_size
>> PAGE_CACHE_SHIFT
);
4722 ret
= __ext4_journalled_invalidatepage(page
, offset
);
4724 page_cache_release(page
);
4728 read_lock(&journal
->j_state_lock
);
4729 if (journal
->j_committing_transaction
)
4730 commit_tid
= journal
->j_committing_transaction
->t_tid
;
4731 read_unlock(&journal
->j_state_lock
);
4733 jbd2_log_wait_commit(journal
, commit_tid
);
4740 * Called from notify_change.
4742 * We want to trap VFS attempts to truncate the file as soon as
4743 * possible. In particular, we want to make sure that when the VFS
4744 * shrinks i_size, we put the inode on the orphan list and modify
4745 * i_disksize immediately, so that during the subsequent flushing of
4746 * dirty pages and freeing of disk blocks, we can guarantee that any
4747 * commit will leave the blocks being flushed in an unused state on
4748 * disk. (On recovery, the inode will get truncated and the blocks will
4749 * be freed, so we have a strong guarantee that no future commit will
4750 * leave these blocks visible to the user.)
4752 * Another thing we have to assure is that if we are in ordered mode
4753 * and inode is still attached to the committing transaction, we must
4754 * we start writeout of all the dirty pages which are being truncated.
4755 * This way we are sure that all the data written in the previous
4756 * transaction are already on disk (truncate waits for pages under
4759 * Called with inode->i_mutex down.
4761 int ext4_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4763 struct inode
*inode
= dentry
->d_inode
;
4766 const unsigned int ia_valid
= attr
->ia_valid
;
4768 error
= inode_change_ok(inode
, attr
);
4772 if (is_quota_modification(inode
, attr
))
4773 dquot_initialize(inode
);
4774 if ((ia_valid
& ATTR_UID
&& !uid_eq(attr
->ia_uid
, inode
->i_uid
)) ||
4775 (ia_valid
& ATTR_GID
&& !gid_eq(attr
->ia_gid
, inode
->i_gid
))) {
4778 /* (user+group)*(old+new) structure, inode write (sb,
4779 * inode block, ? - but truncate inode update has it) */
4780 handle
= ext4_journal_start(inode
, EXT4_HT_QUOTA
,
4781 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode
->i_sb
) +
4782 EXT4_MAXQUOTAS_DEL_BLOCKS(inode
->i_sb
)) + 3);
4783 if (IS_ERR(handle
)) {
4784 error
= PTR_ERR(handle
);
4787 error
= dquot_transfer(inode
, attr
);
4789 ext4_journal_stop(handle
);
4792 /* Update corresponding info in inode so that everything is in
4793 * one transaction */
4794 if (attr
->ia_valid
& ATTR_UID
)
4795 inode
->i_uid
= attr
->ia_uid
;
4796 if (attr
->ia_valid
& ATTR_GID
)
4797 inode
->i_gid
= attr
->ia_gid
;
4798 error
= ext4_mark_inode_dirty(handle
, inode
);
4799 ext4_journal_stop(handle
);
4802 if (attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
!= inode
->i_size
) {
4804 loff_t oldsize
= inode
->i_size
;
4806 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))) {
4807 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
4809 if (attr
->ia_size
> sbi
->s_bitmap_maxbytes
)
4813 if (IS_I_VERSION(inode
) && attr
->ia_size
!= inode
->i_size
)
4814 inode_inc_iversion(inode
);
4816 if (S_ISREG(inode
->i_mode
) &&
4817 (attr
->ia_size
< inode
->i_size
)) {
4818 if (ext4_should_order_data(inode
)) {
4819 error
= ext4_begin_ordered_truncate(inode
,
4824 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 3);
4825 if (IS_ERR(handle
)) {
4826 error
= PTR_ERR(handle
);
4829 if (ext4_handle_valid(handle
)) {
4830 error
= ext4_orphan_add(handle
, inode
);
4833 EXT4_I(inode
)->i_disksize
= attr
->ia_size
;
4834 rc
= ext4_mark_inode_dirty(handle
, inode
);
4837 ext4_journal_stop(handle
);
4839 ext4_orphan_del(NULL
, inode
);
4844 i_size_write(inode
, attr
->ia_size
);
4846 * Blocks are going to be removed from the inode. Wait
4847 * for dio in flight. Temporarily disable
4848 * dioread_nolock to prevent livelock.
4851 if (!ext4_should_journal_data(inode
)) {
4852 ext4_inode_block_unlocked_dio(inode
);
4853 inode_dio_wait(inode
);
4854 ext4_inode_resume_unlocked_dio(inode
);
4856 ext4_wait_for_tail_page_commit(inode
);
4859 * Truncate pagecache after we've waited for commit
4860 * in data=journal mode to make pages freeable.
4862 truncate_pagecache(inode
, oldsize
, inode
->i_size
);
4865 * We want to call ext4_truncate() even if attr->ia_size ==
4866 * inode->i_size for cases like truncation of fallocated space
4868 if (attr
->ia_valid
& ATTR_SIZE
)
4869 ext4_truncate(inode
);
4872 setattr_copy(inode
, attr
);
4873 mark_inode_dirty(inode
);
4877 * If the call to ext4_truncate failed to get a transaction handle at
4878 * all, we need to clean up the in-core orphan list manually.
4880 if (orphan
&& inode
->i_nlink
)
4881 ext4_orphan_del(NULL
, inode
);
4883 if (!rc
&& (ia_valid
& ATTR_MODE
))
4884 rc
= ext4_acl_chmod(inode
);
4887 ext4_std_error(inode
->i_sb
, error
);
4893 int ext4_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
4896 struct inode
*inode
;
4897 unsigned long long delalloc_blocks
;
4899 inode
= dentry
->d_inode
;
4900 generic_fillattr(inode
, stat
);
4903 * We can't update i_blocks if the block allocation is delayed
4904 * otherwise in the case of system crash before the real block
4905 * allocation is done, we will have i_blocks inconsistent with
4906 * on-disk file blocks.
4907 * We always keep i_blocks updated together with real
4908 * allocation. But to not confuse with user, stat
4909 * will return the blocks that include the delayed allocation
4910 * blocks for this file.
4912 delalloc_blocks
= EXT4_C2B(EXT4_SB(inode
->i_sb
),
4913 EXT4_I(inode
)->i_reserved_data_blocks
);
4915 stat
->blocks
+= delalloc_blocks
<< (inode
->i_sb
->s_blocksize_bits
-9);
4919 static int ext4_index_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
4921 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
4922 return ext4_ind_trans_blocks(inode
, nrblocks
, chunk
);
4923 return ext4_ext_index_trans_blocks(inode
, nrblocks
, chunk
);
4927 * Account for index blocks, block groups bitmaps and block group
4928 * descriptor blocks if modify datablocks and index blocks
4929 * worse case, the indexs blocks spread over different block groups
4931 * If datablocks are discontiguous, they are possible to spread over
4932 * different block groups too. If they are contiguous, with flexbg,
4933 * they could still across block group boundary.
4935 * Also account for superblock, inode, quota and xattr blocks
4937 static int ext4_meta_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
4939 ext4_group_t groups
, ngroups
= ext4_get_groups_count(inode
->i_sb
);
4945 * How many index blocks need to touch to modify nrblocks?
4946 * The "Chunk" flag indicating whether the nrblocks is
4947 * physically contiguous on disk
4949 * For Direct IO and fallocate, they calls get_block to allocate
4950 * one single extent at a time, so they could set the "Chunk" flag
4952 idxblocks
= ext4_index_trans_blocks(inode
, nrblocks
, chunk
);
4957 * Now let's see how many group bitmaps and group descriptors need
4967 if (groups
> ngroups
)
4969 if (groups
> EXT4_SB(inode
->i_sb
)->s_gdb_count
)
4970 gdpblocks
= EXT4_SB(inode
->i_sb
)->s_gdb_count
;
4972 /* bitmaps and block group descriptor blocks */
4973 ret
+= groups
+ gdpblocks
;
4975 /* Blocks for super block, inode, quota and xattr blocks */
4976 ret
+= EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
4982 * Calculate the total number of credits to reserve to fit
4983 * the modification of a single pages into a single transaction,
4984 * which may include multiple chunks of block allocations.
4986 * This could be called via ext4_write_begin()
4988 * We need to consider the worse case, when
4989 * one new block per extent.
4991 int ext4_writepage_trans_blocks(struct inode
*inode
)
4993 int bpp
= ext4_journal_blocks_per_page(inode
);
4996 ret
= ext4_meta_trans_blocks(inode
, bpp
, 0);
4998 /* Account for data blocks for journalled mode */
4999 if (ext4_should_journal_data(inode
))
5005 * Calculate the journal credits for a chunk of data modification.
5007 * This is called from DIO, fallocate or whoever calling
5008 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
5010 * journal buffers for data blocks are not included here, as DIO
5011 * and fallocate do no need to journal data buffers.
5013 int ext4_chunk_trans_blocks(struct inode
*inode
, int nrblocks
)
5015 return ext4_meta_trans_blocks(inode
, nrblocks
, 1);
5019 * The caller must have previously called ext4_reserve_inode_write().
5020 * Give this, we know that the caller already has write access to iloc->bh.
5022 int ext4_mark_iloc_dirty(handle_t
*handle
,
5023 struct inode
*inode
, struct ext4_iloc
*iloc
)
5027 if (IS_I_VERSION(inode
))
5028 inode_inc_iversion(inode
);
5030 /* the do_update_inode consumes one bh->b_count */
5033 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
5034 err
= ext4_do_update_inode(handle
, inode
, iloc
);
5040 * On success, We end up with an outstanding reference count against
5041 * iloc->bh. This _must_ be cleaned up later.
5045 ext4_reserve_inode_write(handle_t
*handle
, struct inode
*inode
,
5046 struct ext4_iloc
*iloc
)
5050 err
= ext4_get_inode_loc(inode
, iloc
);
5052 BUFFER_TRACE(iloc
->bh
, "get_write_access");
5053 err
= ext4_journal_get_write_access(handle
, iloc
->bh
);
5059 ext4_std_error(inode
->i_sb
, err
);
5064 * Expand an inode by new_extra_isize bytes.
5065 * Returns 0 on success or negative error number on failure.
5067 static int ext4_expand_extra_isize(struct inode
*inode
,
5068 unsigned int new_extra_isize
,
5069 struct ext4_iloc iloc
,
5072 struct ext4_inode
*raw_inode
;
5073 struct ext4_xattr_ibody_header
*header
;
5075 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
)
5078 raw_inode
= ext4_raw_inode(&iloc
);
5080 header
= IHDR(inode
, raw_inode
);
5082 /* No extended attributes present */
5083 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
) ||
5084 header
->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
)) {
5085 memset((void *)raw_inode
+ EXT4_GOOD_OLD_INODE_SIZE
, 0,
5087 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
5091 /* try to expand with EAs present */
5092 return ext4_expand_extra_isize_ea(inode
, new_extra_isize
,
5097 * What we do here is to mark the in-core inode as clean with respect to inode
5098 * dirtiness (it may still be data-dirty).
5099 * This means that the in-core inode may be reaped by prune_icache
5100 * without having to perform any I/O. This is a very good thing,
5101 * because *any* task may call prune_icache - even ones which
5102 * have a transaction open against a different journal.
5104 * Is this cheating? Not really. Sure, we haven't written the
5105 * inode out, but prune_icache isn't a user-visible syncing function.
5106 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5107 * we start and wait on commits.
5109 int ext4_mark_inode_dirty(handle_t
*handle
, struct inode
*inode
)
5111 struct ext4_iloc iloc
;
5112 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
5113 static unsigned int mnt_count
;
5117 trace_ext4_mark_inode_dirty(inode
, _RET_IP_
);
5118 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
5121 if (ext4_handle_valid(handle
) &&
5122 EXT4_I(inode
)->i_extra_isize
< sbi
->s_want_extra_isize
&&
5123 !ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
)) {
5125 * We need extra buffer credits since we may write into EA block
5126 * with this same handle. If journal_extend fails, then it will
5127 * only result in a minor loss of functionality for that inode.
5128 * If this is felt to be critical, then e2fsck should be run to
5129 * force a large enough s_min_extra_isize.
5131 if ((jbd2_journal_extend(handle
,
5132 EXT4_DATA_TRANS_BLOCKS(inode
->i_sb
))) == 0) {
5133 ret
= ext4_expand_extra_isize(inode
,
5134 sbi
->s_want_extra_isize
,
5137 ext4_set_inode_state(inode
,
5138 EXT4_STATE_NO_EXPAND
);
5140 le16_to_cpu(sbi
->s_es
->s_mnt_count
)) {
5141 ext4_warning(inode
->i_sb
,
5142 "Unable to expand inode %lu. Delete"
5143 " some EAs or run e2fsck.",
5146 le16_to_cpu(sbi
->s_es
->s_mnt_count
);
5151 return ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
5155 * ext4_dirty_inode() is called from __mark_inode_dirty()
5157 * We're really interested in the case where a file is being extended.
5158 * i_size has been changed by generic_commit_write() and we thus need
5159 * to include the updated inode in the current transaction.
5161 * Also, dquot_alloc_block() will always dirty the inode when blocks
5162 * are allocated to the file.
5164 * If the inode is marked synchronous, we don't honour that here - doing
5165 * so would cause a commit on atime updates, which we don't bother doing.
5166 * We handle synchronous inodes at the highest possible level.
5168 void ext4_dirty_inode(struct inode
*inode
, int flags
)
5172 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 2);
5176 ext4_mark_inode_dirty(handle
, inode
);
5178 ext4_journal_stop(handle
);
5185 * Bind an inode's backing buffer_head into this transaction, to prevent
5186 * it from being flushed to disk early. Unlike
5187 * ext4_reserve_inode_write, this leaves behind no bh reference and
5188 * returns no iloc structure, so the caller needs to repeat the iloc
5189 * lookup to mark the inode dirty later.
5191 static int ext4_pin_inode(handle_t
*handle
, struct inode
*inode
)
5193 struct ext4_iloc iloc
;
5197 err
= ext4_get_inode_loc(inode
, &iloc
);
5199 BUFFER_TRACE(iloc
.bh
, "get_write_access");
5200 err
= jbd2_journal_get_write_access(handle
, iloc
.bh
);
5202 err
= ext4_handle_dirty_metadata(handle
,
5208 ext4_std_error(inode
->i_sb
, err
);
5213 int ext4_change_inode_journal_flag(struct inode
*inode
, int val
)
5220 * We have to be very careful here: changing a data block's
5221 * journaling status dynamically is dangerous. If we write a
5222 * data block to the journal, change the status and then delete
5223 * that block, we risk forgetting to revoke the old log record
5224 * from the journal and so a subsequent replay can corrupt data.
5225 * So, first we make sure that the journal is empty and that
5226 * nobody is changing anything.
5229 journal
= EXT4_JOURNAL(inode
);
5232 if (is_journal_aborted(journal
))
5234 /* We have to allocate physical blocks for delalloc blocks
5235 * before flushing journal. otherwise delalloc blocks can not
5236 * be allocated any more. even more truncate on delalloc blocks
5237 * could trigger BUG by flushing delalloc blocks in journal.
5238 * There is no delalloc block in non-journal data mode.
5240 if (val
&& test_opt(inode
->i_sb
, DELALLOC
)) {
5241 err
= ext4_alloc_da_blocks(inode
);
5246 /* Wait for all existing dio workers */
5247 ext4_inode_block_unlocked_dio(inode
);
5248 inode_dio_wait(inode
);
5250 jbd2_journal_lock_updates(journal
);
5253 * OK, there are no updates running now, and all cached data is
5254 * synced to disk. We are now in a completely consistent state
5255 * which doesn't have anything in the journal, and we know that
5256 * no filesystem updates are running, so it is safe to modify
5257 * the inode's in-core data-journaling state flag now.
5261 ext4_set_inode_flag(inode
, EXT4_INODE_JOURNAL_DATA
);
5263 jbd2_journal_flush(journal
);
5264 ext4_clear_inode_flag(inode
, EXT4_INODE_JOURNAL_DATA
);
5266 ext4_set_aops(inode
);
5268 jbd2_journal_unlock_updates(journal
);
5269 ext4_inode_resume_unlocked_dio(inode
);
5271 /* Finally we can mark the inode as dirty. */
5273 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
5275 return PTR_ERR(handle
);
5277 err
= ext4_mark_inode_dirty(handle
, inode
);
5278 ext4_handle_sync(handle
);
5279 ext4_journal_stop(handle
);
5280 ext4_std_error(inode
->i_sb
, err
);
5285 static int ext4_bh_unmapped(handle_t
*handle
, struct buffer_head
*bh
)
5287 return !buffer_mapped(bh
);
5290 int ext4_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
5292 struct page
*page
= vmf
->page
;
5296 struct file
*file
= vma
->vm_file
;
5297 struct inode
*inode
= file_inode(file
);
5298 struct address_space
*mapping
= inode
->i_mapping
;
5300 get_block_t
*get_block
;
5303 sb_start_pagefault(inode
->i_sb
);
5304 file_update_time(vma
->vm_file
);
5305 /* Delalloc case is easy... */
5306 if (test_opt(inode
->i_sb
, DELALLOC
) &&
5307 !ext4_should_journal_data(inode
) &&
5308 !ext4_nonda_switch(inode
->i_sb
)) {
5310 ret
= __block_page_mkwrite(vma
, vmf
,
5311 ext4_da_get_block_prep
);
5312 } while (ret
== -ENOSPC
&&
5313 ext4_should_retry_alloc(inode
->i_sb
, &retries
));
5318 size
= i_size_read(inode
);
5319 /* Page got truncated from under us? */
5320 if (page
->mapping
!= mapping
|| page_offset(page
) > size
) {
5322 ret
= VM_FAULT_NOPAGE
;
5326 if (page
->index
== size
>> PAGE_CACHE_SHIFT
)
5327 len
= size
& ~PAGE_CACHE_MASK
;
5329 len
= PAGE_CACHE_SIZE
;
5331 * Return if we have all the buffers mapped. This avoids the need to do
5332 * journal_start/journal_stop which can block and take a long time
5334 if (page_has_buffers(page
)) {
5335 if (!ext4_walk_page_buffers(NULL
, page_buffers(page
),
5337 ext4_bh_unmapped
)) {
5338 /* Wait so that we don't change page under IO */
5339 wait_for_stable_page(page
);
5340 ret
= VM_FAULT_LOCKED
;
5345 /* OK, we need to fill the hole... */
5346 if (ext4_should_dioread_nolock(inode
))
5347 get_block
= ext4_get_block_write
;
5349 get_block
= ext4_get_block
;
5351 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
5352 ext4_writepage_trans_blocks(inode
));
5353 if (IS_ERR(handle
)) {
5354 ret
= VM_FAULT_SIGBUS
;
5357 ret
= __block_page_mkwrite(vma
, vmf
, get_block
);
5358 if (!ret
&& ext4_should_journal_data(inode
)) {
5359 if (ext4_walk_page_buffers(handle
, page_buffers(page
), 0,
5360 PAGE_CACHE_SIZE
, NULL
, do_journal_get_write_access
)) {
5362 ret
= VM_FAULT_SIGBUS
;
5363 ext4_journal_stop(handle
);
5366 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
5368 ext4_journal_stop(handle
);
5369 if (ret
== -ENOSPC
&& ext4_should_retry_alloc(inode
->i_sb
, &retries
))
5372 ret
= block_page_mkwrite_return(ret
);
5374 sb_end_pagefault(inode
->i_sb
);