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/highuid.h>
24 #include <linux/pagemap.h>
25 #include <linux/dax.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/bitops.h>
41 #include "ext4_jbd2.h"
46 #include <trace/events/ext4.h>
48 #define MPAGE_DA_EXTENT_TAIL 0x01
50 static __u32
ext4_inode_csum(struct inode
*inode
, struct ext4_inode
*raw
,
51 struct ext4_inode_info
*ei
)
53 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
56 int offset
= offsetof(struct ext4_inode
, i_checksum_lo
);
57 unsigned int csum_size
= sizeof(dummy_csum
);
59 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)raw
, offset
);
60 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
, csum_size
);
62 csum
= ext4_chksum(sbi
, csum
, (__u8
*)raw
+ offset
,
63 EXT4_GOOD_OLD_INODE_SIZE
- offset
);
65 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
66 offset
= offsetof(struct ext4_inode
, i_checksum_hi
);
67 csum
= ext4_chksum(sbi
, csum
, (__u8
*)raw
+
68 EXT4_GOOD_OLD_INODE_SIZE
,
69 offset
- EXT4_GOOD_OLD_INODE_SIZE
);
70 if (EXT4_FITS_IN_INODE(raw
, ei
, i_checksum_hi
)) {
71 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
,
75 csum
= ext4_chksum(sbi
, csum
, (__u8
*)raw
+ offset
,
76 EXT4_INODE_SIZE(inode
->i_sb
) - offset
);
82 static int ext4_inode_csum_verify(struct inode
*inode
, struct ext4_inode
*raw
,
83 struct ext4_inode_info
*ei
)
85 __u32 provided
, calculated
;
87 if (EXT4_SB(inode
->i_sb
)->s_es
->s_creator_os
!=
88 cpu_to_le32(EXT4_OS_LINUX
) ||
89 !ext4_has_metadata_csum(inode
->i_sb
))
92 provided
= le16_to_cpu(raw
->i_checksum_lo
);
93 calculated
= ext4_inode_csum(inode
, raw
, ei
);
94 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
&&
95 EXT4_FITS_IN_INODE(raw
, ei
, i_checksum_hi
))
96 provided
|= ((__u32
)le16_to_cpu(raw
->i_checksum_hi
)) << 16;
100 return provided
== calculated
;
103 static void ext4_inode_csum_set(struct inode
*inode
, struct ext4_inode
*raw
,
104 struct ext4_inode_info
*ei
)
108 if (EXT4_SB(inode
->i_sb
)->s_es
->s_creator_os
!=
109 cpu_to_le32(EXT4_OS_LINUX
) ||
110 !ext4_has_metadata_csum(inode
->i_sb
))
113 csum
= ext4_inode_csum(inode
, raw
, ei
);
114 raw
->i_checksum_lo
= cpu_to_le16(csum
& 0xFFFF);
115 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
&&
116 EXT4_FITS_IN_INODE(raw
, ei
, i_checksum_hi
))
117 raw
->i_checksum_hi
= cpu_to_le16(csum
>> 16);
120 static inline int ext4_begin_ordered_truncate(struct inode
*inode
,
123 trace_ext4_begin_ordered_truncate(inode
, new_size
);
125 * If jinode is zero, then we never opened the file for
126 * writing, so there's no need to call
127 * jbd2_journal_begin_ordered_truncate() since there's no
128 * outstanding writes we need to flush.
130 if (!EXT4_I(inode
)->jinode
)
132 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode
),
133 EXT4_I(inode
)->jinode
,
137 static void ext4_invalidatepage(struct page
*page
, unsigned int offset
,
138 unsigned int length
);
139 static int __ext4_journalled_writepage(struct page
*page
, unsigned int len
);
140 static int ext4_bh_delay_or_unwritten(handle_t
*handle
, struct buffer_head
*bh
);
141 static int ext4_meta_trans_blocks(struct inode
*inode
, int lblocks
,
145 * Test whether an inode is a fast symlink.
147 int ext4_inode_is_fast_symlink(struct inode
*inode
)
149 int ea_blocks
= EXT4_I(inode
)->i_file_acl
?
150 EXT4_CLUSTER_SIZE(inode
->i_sb
) >> 9 : 0;
152 if (ext4_has_inline_data(inode
))
155 return (S_ISLNK(inode
->i_mode
) && inode
->i_blocks
- ea_blocks
== 0);
159 * Restart the transaction associated with *handle. This does a commit,
160 * so before we call here everything must be consistently dirtied against
163 int ext4_truncate_restart_trans(handle_t
*handle
, struct inode
*inode
,
169 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
170 * moment, get_block can be called only for blocks inside i_size since
171 * page cache has been already dropped and writes are blocked by
172 * i_mutex. So we can safely drop the i_data_sem here.
174 BUG_ON(EXT4_JOURNAL(inode
) == NULL
);
175 jbd_debug(2, "restarting handle %p\n", handle
);
176 up_write(&EXT4_I(inode
)->i_data_sem
);
177 ret
= ext4_journal_restart(handle
, nblocks
);
178 down_write(&EXT4_I(inode
)->i_data_sem
);
179 ext4_discard_preallocations(inode
);
185 * Called at the last iput() if i_nlink is zero.
187 void ext4_evict_inode(struct inode
*inode
)
192 trace_ext4_evict_inode(inode
);
194 if (inode
->i_nlink
) {
196 * When journalling data dirty buffers are tracked only in the
197 * journal. So although mm thinks everything is clean and
198 * ready for reaping the inode might still have some pages to
199 * write in the running transaction or waiting to be
200 * checkpointed. Thus calling jbd2_journal_invalidatepage()
201 * (via truncate_inode_pages()) to discard these buffers can
202 * cause data loss. Also even if we did not discard these
203 * buffers, we would have no way to find them after the inode
204 * is reaped and thus user could see stale data if he tries to
205 * read them before the transaction is checkpointed. So be
206 * careful and force everything to disk here... We use
207 * ei->i_datasync_tid to store the newest transaction
208 * containing inode's data.
210 * Note that directories do not have this problem because they
211 * don't use page cache.
213 if (inode
->i_ino
!= EXT4_JOURNAL_INO
&&
214 ext4_should_journal_data(inode
) &&
215 (S_ISLNK(inode
->i_mode
) || S_ISREG(inode
->i_mode
))) {
216 journal_t
*journal
= EXT4_SB(inode
->i_sb
)->s_journal
;
217 tid_t commit_tid
= EXT4_I(inode
)->i_datasync_tid
;
219 jbd2_complete_transaction(journal
, commit_tid
);
220 filemap_write_and_wait(&inode
->i_data
);
222 truncate_inode_pages_final(&inode
->i_data
);
224 WARN_ON(atomic_read(&EXT4_I(inode
)->i_ioend_count
));
228 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_final(&inode
->i_data
);
236 WARN_ON(atomic_read(&EXT4_I(inode
)->i_ioend_count
));
239 * Protect us against freezing - iput() caller didn't have to have any
240 * protection against it
242 sb_start_intwrite(inode
->i_sb
);
243 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
,
244 ext4_blocks_for_truncate(inode
)+3);
245 if (IS_ERR(handle
)) {
246 ext4_std_error(inode
->i_sb
, PTR_ERR(handle
));
248 * If we're going to skip the normal cleanup, we still need to
249 * make sure that the in-core orphan linked list is properly
252 ext4_orphan_del(NULL
, inode
);
253 sb_end_intwrite(inode
->i_sb
);
258 ext4_handle_sync(handle
);
260 err
= ext4_mark_inode_dirty(handle
, inode
);
262 ext4_warning(inode
->i_sb
,
263 "couldn't mark inode dirty (err %d)", err
);
267 ext4_truncate(inode
);
270 * ext4_ext_truncate() doesn't reserve any slop when it
271 * restarts journal transactions; therefore there may not be
272 * enough credits left in the handle to remove the inode from
273 * the orphan list and set the dtime field.
275 if (!ext4_handle_has_enough_credits(handle
, 3)) {
276 err
= ext4_journal_extend(handle
, 3);
278 err
= ext4_journal_restart(handle
, 3);
280 ext4_warning(inode
->i_sb
,
281 "couldn't extend journal (err %d)", err
);
283 ext4_journal_stop(handle
);
284 ext4_orphan_del(NULL
, inode
);
285 sb_end_intwrite(inode
->i_sb
);
291 * Kill off the orphan record which ext4_truncate created.
292 * AKPM: I think this can be inside the above `if'.
293 * Note that ext4_orphan_del() has to be able to cope with the
294 * deletion of a non-existent orphan - this is because we don't
295 * know if ext4_truncate() actually created an orphan record.
296 * (Well, we could do this if we need to, but heck - it works)
298 ext4_orphan_del(handle
, inode
);
299 EXT4_I(inode
)->i_dtime
= get_seconds();
302 * One subtle ordering requirement: if anything has gone wrong
303 * (transaction abort, IO errors, whatever), then we can still
304 * do these next steps (the fs will already have been marked as
305 * having errors), but we can't free the inode if the mark_dirty
308 if (ext4_mark_inode_dirty(handle
, inode
))
309 /* If that failed, just do the required in-core inode clear. */
310 ext4_clear_inode(inode
);
312 ext4_free_inode(handle
, inode
);
313 ext4_journal_stop(handle
);
314 sb_end_intwrite(inode
->i_sb
);
317 ext4_clear_inode(inode
); /* We must guarantee clearing of inode... */
321 qsize_t
*ext4_get_reserved_space(struct inode
*inode
)
323 return &EXT4_I(inode
)->i_reserved_quota
;
328 * Called with i_data_sem down, which is important since we can call
329 * ext4_discard_preallocations() from here.
331 void ext4_da_update_reserve_space(struct inode
*inode
,
332 int used
, int quota_claim
)
334 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
335 struct ext4_inode_info
*ei
= EXT4_I(inode
);
337 spin_lock(&ei
->i_block_reservation_lock
);
338 trace_ext4_da_update_reserve_space(inode
, used
, quota_claim
);
339 if (unlikely(used
> ei
->i_reserved_data_blocks
)) {
340 ext4_warning(inode
->i_sb
, "%s: ino %lu, used %d "
341 "with only %d reserved data blocks",
342 __func__
, inode
->i_ino
, used
,
343 ei
->i_reserved_data_blocks
);
345 used
= ei
->i_reserved_data_blocks
;
348 /* Update per-inode reservations */
349 ei
->i_reserved_data_blocks
-= used
;
350 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
, used
);
352 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
354 /* Update quota subsystem for data blocks */
356 dquot_claim_block(inode
, EXT4_C2B(sbi
, used
));
359 * We did fallocate with an offset that is already delayed
360 * allocated. So on delayed allocated writeback we should
361 * not re-claim the quota for fallocated blocks.
363 dquot_release_reservation_block(inode
, EXT4_C2B(sbi
, used
));
367 * If we have done all the pending block allocations and if
368 * there aren't any writers on the inode, we can discard the
369 * inode's preallocations.
371 if ((ei
->i_reserved_data_blocks
== 0) &&
372 (atomic_read(&inode
->i_writecount
) == 0))
373 ext4_discard_preallocations(inode
);
376 static int __check_block_validity(struct inode
*inode
, const char *func
,
378 struct ext4_map_blocks
*map
)
380 if (!ext4_data_block_valid(EXT4_SB(inode
->i_sb
), map
->m_pblk
,
382 ext4_error_inode(inode
, func
, line
, map
->m_pblk
,
383 "lblock %lu mapped to illegal pblock "
384 "(length %d)", (unsigned long) map
->m_lblk
,
386 return -EFSCORRUPTED
;
391 #define check_block_validity(inode, map) \
392 __check_block_validity((inode), __func__, __LINE__, (map))
394 #ifdef ES_AGGRESSIVE_TEST
395 static void ext4_map_blocks_es_recheck(handle_t
*handle
,
397 struct ext4_map_blocks
*es_map
,
398 struct ext4_map_blocks
*map
,
405 * There is a race window that the result is not the same.
406 * e.g. xfstests #223 when dioread_nolock enables. The reason
407 * is that we lookup a block mapping in extent status tree with
408 * out taking i_data_sem. So at the time the unwritten extent
409 * could be converted.
411 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
412 down_read(&EXT4_I(inode
)->i_data_sem
);
413 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
414 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
&
415 EXT4_GET_BLOCKS_KEEP_SIZE
);
417 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
&
418 EXT4_GET_BLOCKS_KEEP_SIZE
);
420 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
421 up_read((&EXT4_I(inode
)->i_data_sem
));
424 * We don't check m_len because extent will be collpased in status
425 * tree. So the m_len might not equal.
427 if (es_map
->m_lblk
!= map
->m_lblk
||
428 es_map
->m_flags
!= map
->m_flags
||
429 es_map
->m_pblk
!= map
->m_pblk
) {
430 printk("ES cache assertion failed for inode: %lu "
431 "es_cached ex [%d/%d/%llu/%x] != "
432 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
433 inode
->i_ino
, es_map
->m_lblk
, es_map
->m_len
,
434 es_map
->m_pblk
, es_map
->m_flags
, map
->m_lblk
,
435 map
->m_len
, map
->m_pblk
, map
->m_flags
,
439 #endif /* ES_AGGRESSIVE_TEST */
442 * The ext4_map_blocks() function tries to look up the requested blocks,
443 * and returns if the blocks are already mapped.
445 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
446 * and store the allocated blocks in the result buffer head and mark it
449 * If file type is extents based, it will call ext4_ext_map_blocks(),
450 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
453 * On success, it returns the number of blocks being mapped or allocated.
454 * if create==0 and the blocks are pre-allocated and unwritten block,
455 * the result buffer head is unmapped. If the create ==1, it will make sure
456 * the buffer head is mapped.
458 * It returns 0 if plain look up failed (blocks have not been allocated), in
459 * that case, buffer head is unmapped
461 * It returns the error in case of allocation failure.
463 int ext4_map_blocks(handle_t
*handle
, struct inode
*inode
,
464 struct ext4_map_blocks
*map
, int flags
)
466 struct extent_status es
;
469 #ifdef ES_AGGRESSIVE_TEST
470 struct ext4_map_blocks orig_map
;
472 memcpy(&orig_map
, map
, sizeof(*map
));
476 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
477 "logical block %lu\n", inode
->i_ino
, flags
, map
->m_len
,
478 (unsigned long) map
->m_lblk
);
481 * ext4_map_blocks returns an int, and m_len is an unsigned int
483 if (unlikely(map
->m_len
> INT_MAX
))
484 map
->m_len
= INT_MAX
;
486 /* We can handle the block number less than EXT_MAX_BLOCKS */
487 if (unlikely(map
->m_lblk
>= EXT_MAX_BLOCKS
))
488 return -EFSCORRUPTED
;
490 /* Lookup extent status tree firstly */
491 if (ext4_es_lookup_extent(inode
, map
->m_lblk
, &es
)) {
492 if (ext4_es_is_written(&es
) || ext4_es_is_unwritten(&es
)) {
493 map
->m_pblk
= ext4_es_pblock(&es
) +
494 map
->m_lblk
- es
.es_lblk
;
495 map
->m_flags
|= ext4_es_is_written(&es
) ?
496 EXT4_MAP_MAPPED
: EXT4_MAP_UNWRITTEN
;
497 retval
= es
.es_len
- (map
->m_lblk
- es
.es_lblk
);
498 if (retval
> map
->m_len
)
501 } else if (ext4_es_is_delayed(&es
) || ext4_es_is_hole(&es
)) {
506 #ifdef ES_AGGRESSIVE_TEST
507 ext4_map_blocks_es_recheck(handle
, inode
, map
,
514 * Try to see if we can get the block without requesting a new
517 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
518 down_read(&EXT4_I(inode
)->i_data_sem
);
519 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
520 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
&
521 EXT4_GET_BLOCKS_KEEP_SIZE
);
523 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
&
524 EXT4_GET_BLOCKS_KEEP_SIZE
);
529 if (unlikely(retval
!= map
->m_len
)) {
530 ext4_warning(inode
->i_sb
,
531 "ES len assertion failed for inode "
532 "%lu: retval %d != map->m_len %d",
533 inode
->i_ino
, retval
, map
->m_len
);
537 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
538 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
539 if (!(flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) &&
540 !(status
& EXTENT_STATUS_WRITTEN
) &&
541 ext4_find_delalloc_range(inode
, map
->m_lblk
,
542 map
->m_lblk
+ map
->m_len
- 1))
543 status
|= EXTENT_STATUS_DELAYED
;
544 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
,
545 map
->m_len
, map
->m_pblk
, status
);
549 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
550 up_read((&EXT4_I(inode
)->i_data_sem
));
553 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
) {
554 ret
= check_block_validity(inode
, map
);
559 /* If it is only a block(s) look up */
560 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0)
564 * Returns if the blocks have already allocated
566 * Note that if blocks have been preallocated
567 * ext4_ext_get_block() returns the create = 0
568 * with buffer head unmapped.
570 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
)
572 * If we need to convert extent to unwritten
573 * we continue and do the actual work in
574 * ext4_ext_map_blocks()
576 if (!(flags
& EXT4_GET_BLOCKS_CONVERT_UNWRITTEN
))
580 * Here we clear m_flags because after allocating an new extent,
581 * it will be set again.
583 map
->m_flags
&= ~EXT4_MAP_FLAGS
;
586 * New blocks allocate and/or writing to unwritten extent
587 * will possibly result in updating i_data, so we take
588 * the write lock of i_data_sem, and call get_block()
589 * with create == 1 flag.
591 down_write(&EXT4_I(inode
)->i_data_sem
);
594 * We need to check for EXT4 here because migrate
595 * could have changed the inode type in between
597 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
598 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
);
600 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
);
602 if (retval
> 0 && map
->m_flags
& EXT4_MAP_NEW
) {
604 * We allocated new blocks which will result in
605 * i_data's format changing. Force the migrate
606 * to fail by clearing migrate flags
608 ext4_clear_inode_state(inode
, EXT4_STATE_EXT_MIGRATE
);
612 * Update reserved blocks/metadata blocks after successful
613 * block allocation which had been deferred till now. We don't
614 * support fallocate for non extent files. So we can update
615 * reserve space here.
618 (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
))
619 ext4_da_update_reserve_space(inode
, retval
, 1);
625 if (unlikely(retval
!= map
->m_len
)) {
626 ext4_warning(inode
->i_sb
,
627 "ES len assertion failed for inode "
628 "%lu: retval %d != map->m_len %d",
629 inode
->i_ino
, retval
, map
->m_len
);
634 * If the extent has been zeroed out, we don't need to update
635 * extent status tree.
637 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
) &&
638 ext4_es_lookup_extent(inode
, map
->m_lblk
, &es
)) {
639 if (ext4_es_is_written(&es
))
642 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
643 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
644 if (!(flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) &&
645 !(status
& EXTENT_STATUS_WRITTEN
) &&
646 ext4_find_delalloc_range(inode
, map
->m_lblk
,
647 map
->m_lblk
+ map
->m_len
- 1))
648 status
|= EXTENT_STATUS_DELAYED
;
649 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
, map
->m_len
,
650 map
->m_pblk
, status
);
656 up_write((&EXT4_I(inode
)->i_data_sem
));
657 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
) {
658 ret
= check_block_validity(inode
, map
);
666 * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
667 * we have to be careful as someone else may be manipulating b_state as well.
669 static void ext4_update_bh_state(struct buffer_head
*bh
, unsigned long flags
)
671 unsigned long old_state
;
672 unsigned long new_state
;
674 flags
&= EXT4_MAP_FLAGS
;
676 /* Dummy buffer_head? Set non-atomically. */
678 bh
->b_state
= (bh
->b_state
& ~EXT4_MAP_FLAGS
) | flags
;
682 * Someone else may be modifying b_state. Be careful! This is ugly but
683 * once we get rid of using bh as a container for mapping information
684 * to pass to / from get_block functions, this can go away.
687 old_state
= READ_ONCE(bh
->b_state
);
688 new_state
= (old_state
& ~EXT4_MAP_FLAGS
) | flags
;
690 cmpxchg(&bh
->b_state
, old_state
, new_state
) != old_state
));
693 /* Maximum number of blocks we map for direct IO at once. */
694 #define DIO_MAX_BLOCKS 4096
696 static int _ext4_get_block(struct inode
*inode
, sector_t iblock
,
697 struct buffer_head
*bh
, int flags
)
699 handle_t
*handle
= ext4_journal_current_handle();
700 struct ext4_map_blocks map
;
701 int ret
= 0, started
= 0;
704 if (ext4_has_inline_data(inode
))
708 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
710 if (flags
&& !(flags
& EXT4_GET_BLOCKS_NO_LOCK
) && !handle
) {
711 /* Direct IO write... */
712 if (map
.m_len
> DIO_MAX_BLOCKS
)
713 map
.m_len
= DIO_MAX_BLOCKS
;
714 dio_credits
= ext4_chunk_trans_blocks(inode
, map
.m_len
);
715 handle
= ext4_journal_start(inode
, EXT4_HT_MAP_BLOCKS
,
717 if (IS_ERR(handle
)) {
718 ret
= PTR_ERR(handle
);
724 ret
= ext4_map_blocks(handle
, inode
, &map
, flags
);
726 ext4_io_end_t
*io_end
= ext4_inode_aio(inode
);
728 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
729 ext4_update_bh_state(bh
, map
.m_flags
);
730 if (IS_DAX(inode
) && buffer_unwritten(bh
)) {
732 * dgc: I suspect unwritten conversion on ext4+DAX is
733 * fundamentally broken here when there are concurrent
734 * read/write in progress on this inode.
736 WARN_ON_ONCE(io_end
);
737 bh
->b_assoc_map
= inode
->i_mapping
;
738 bh
->b_private
= (void *)(unsigned long)iblock
;
740 if (io_end
&& io_end
->flag
& EXT4_IO_END_UNWRITTEN
)
741 set_buffer_defer_completion(bh
);
742 bh
->b_size
= inode
->i_sb
->s_blocksize
* map
.m_len
;
746 ext4_journal_stop(handle
);
750 int ext4_get_block(struct inode
*inode
, sector_t iblock
,
751 struct buffer_head
*bh
, int create
)
753 return _ext4_get_block(inode
, iblock
, bh
,
754 create
? EXT4_GET_BLOCKS_CREATE
: 0);
758 * `handle' can be NULL if create is zero
760 struct buffer_head
*ext4_getblk(handle_t
*handle
, struct inode
*inode
,
761 ext4_lblk_t block
, int map_flags
)
763 struct ext4_map_blocks map
;
764 struct buffer_head
*bh
;
765 int create
= map_flags
& EXT4_GET_BLOCKS_CREATE
;
768 J_ASSERT(handle
!= NULL
|| create
== 0);
772 err
= ext4_map_blocks(handle
, inode
, &map
, map_flags
);
775 return create
? ERR_PTR(-ENOSPC
) : NULL
;
779 bh
= sb_getblk(inode
->i_sb
, map
.m_pblk
);
781 return ERR_PTR(-ENOMEM
);
782 if (map
.m_flags
& EXT4_MAP_NEW
) {
783 J_ASSERT(create
!= 0);
784 J_ASSERT(handle
!= NULL
);
787 * Now that we do not always journal data, we should
788 * keep in mind whether this should always journal the
789 * new buffer as metadata. For now, regular file
790 * writes use ext4_get_block instead, so it's not a
794 BUFFER_TRACE(bh
, "call get_create_access");
795 err
= ext4_journal_get_create_access(handle
, bh
);
800 if (!buffer_uptodate(bh
)) {
801 memset(bh
->b_data
, 0, inode
->i_sb
->s_blocksize
);
802 set_buffer_uptodate(bh
);
805 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
806 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
810 BUFFER_TRACE(bh
, "not a new buffer");
817 struct buffer_head
*ext4_bread(handle_t
*handle
, struct inode
*inode
,
818 ext4_lblk_t block
, int map_flags
)
820 struct buffer_head
*bh
;
822 bh
= ext4_getblk(handle
, inode
, block
, map_flags
);
825 if (!bh
|| buffer_uptodate(bh
))
827 ll_rw_block(READ
| REQ_META
| REQ_PRIO
, 1, &bh
);
829 if (buffer_uptodate(bh
))
832 return ERR_PTR(-EIO
);
835 int ext4_walk_page_buffers(handle_t
*handle
,
836 struct buffer_head
*head
,
840 int (*fn
)(handle_t
*handle
,
841 struct buffer_head
*bh
))
843 struct buffer_head
*bh
;
844 unsigned block_start
, block_end
;
845 unsigned blocksize
= head
->b_size
;
847 struct buffer_head
*next
;
849 for (bh
= head
, block_start
= 0;
850 ret
== 0 && (bh
!= head
|| !block_start
);
851 block_start
= block_end
, bh
= next
) {
852 next
= bh
->b_this_page
;
853 block_end
= block_start
+ blocksize
;
854 if (block_end
<= from
|| block_start
>= to
) {
855 if (partial
&& !buffer_uptodate(bh
))
859 err
= (*fn
)(handle
, bh
);
867 * To preserve ordering, it is essential that the hole instantiation and
868 * the data write be encapsulated in a single transaction. We cannot
869 * close off a transaction and start a new one between the ext4_get_block()
870 * and the commit_write(). So doing the jbd2_journal_start at the start of
871 * prepare_write() is the right place.
873 * Also, this function can nest inside ext4_writepage(). In that case, we
874 * *know* that ext4_writepage() has generated enough buffer credits to do the
875 * whole page. So we won't block on the journal in that case, which is good,
876 * because the caller may be PF_MEMALLOC.
878 * By accident, ext4 can be reentered when a transaction is open via
879 * quota file writes. If we were to commit the transaction while thus
880 * reentered, there can be a deadlock - we would be holding a quota
881 * lock, and the commit would never complete if another thread had a
882 * transaction open and was blocking on the quota lock - a ranking
885 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
886 * will _not_ run commit under these circumstances because handle->h_ref
887 * is elevated. We'll still have enough credits for the tiny quotafile
890 int do_journal_get_write_access(handle_t
*handle
,
891 struct buffer_head
*bh
)
893 int dirty
= buffer_dirty(bh
);
896 if (!buffer_mapped(bh
) || buffer_freed(bh
))
899 * __block_write_begin() could have dirtied some buffers. Clean
900 * the dirty bit as jbd2_journal_get_write_access() could complain
901 * otherwise about fs integrity issues. Setting of the dirty bit
902 * by __block_write_begin() isn't a real problem here as we clear
903 * the bit before releasing a page lock and thus writeback cannot
904 * ever write the buffer.
907 clear_buffer_dirty(bh
);
908 BUFFER_TRACE(bh
, "get write access");
909 ret
= ext4_journal_get_write_access(handle
, bh
);
911 ret
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
915 static int ext4_get_block_write_nolock(struct inode
*inode
, sector_t iblock
,
916 struct buffer_head
*bh_result
, int create
);
918 #ifdef CONFIG_EXT4_FS_ENCRYPTION
919 static int ext4_block_write_begin(struct page
*page
, loff_t pos
, unsigned len
,
920 get_block_t
*get_block
)
922 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
923 unsigned to
= from
+ len
;
924 struct inode
*inode
= page
->mapping
->host
;
925 unsigned block_start
, block_end
;
928 unsigned blocksize
= inode
->i_sb
->s_blocksize
;
930 struct buffer_head
*bh
, *head
, *wait
[2], **wait_bh
= wait
;
931 bool decrypt
= false;
933 BUG_ON(!PageLocked(page
));
934 BUG_ON(from
> PAGE_CACHE_SIZE
);
935 BUG_ON(to
> PAGE_CACHE_SIZE
);
938 if (!page_has_buffers(page
))
939 create_empty_buffers(page
, blocksize
, 0);
940 head
= page_buffers(page
);
941 bbits
= ilog2(blocksize
);
942 block
= (sector_t
)page
->index
<< (PAGE_CACHE_SHIFT
- bbits
);
944 for (bh
= head
, block_start
= 0; bh
!= head
|| !block_start
;
945 block
++, block_start
= block_end
, bh
= bh
->b_this_page
) {
946 block_end
= block_start
+ blocksize
;
947 if (block_end
<= from
|| block_start
>= to
) {
948 if (PageUptodate(page
)) {
949 if (!buffer_uptodate(bh
))
950 set_buffer_uptodate(bh
);
955 clear_buffer_new(bh
);
956 if (!buffer_mapped(bh
)) {
957 WARN_ON(bh
->b_size
!= blocksize
);
958 err
= get_block(inode
, block
, bh
, 1);
961 if (buffer_new(bh
)) {
962 unmap_underlying_metadata(bh
->b_bdev
,
964 if (PageUptodate(page
)) {
965 clear_buffer_new(bh
);
966 set_buffer_uptodate(bh
);
967 mark_buffer_dirty(bh
);
970 if (block_end
> to
|| block_start
< from
)
971 zero_user_segments(page
, to
, block_end
,
976 if (PageUptodate(page
)) {
977 if (!buffer_uptodate(bh
))
978 set_buffer_uptodate(bh
);
981 if (!buffer_uptodate(bh
) && !buffer_delay(bh
) &&
982 !buffer_unwritten(bh
) &&
983 (block_start
< from
|| block_end
> to
)) {
984 ll_rw_block(READ
, 1, &bh
);
986 decrypt
= ext4_encrypted_inode(inode
) &&
987 S_ISREG(inode
->i_mode
);
991 * If we issued read requests, let them complete.
993 while (wait_bh
> wait
) {
994 wait_on_buffer(*--wait_bh
);
995 if (!buffer_uptodate(*wait_bh
))
999 page_zero_new_buffers(page
, from
, to
);
1001 err
= ext4_decrypt(page
);
1006 static int ext4_write_begin(struct file
*file
, struct address_space
*mapping
,
1007 loff_t pos
, unsigned len
, unsigned flags
,
1008 struct page
**pagep
, void **fsdata
)
1010 struct inode
*inode
= mapping
->host
;
1011 int ret
, needed_blocks
;
1018 trace_ext4_write_begin(inode
, pos
, len
, flags
);
1020 * Reserve one block more for addition to orphan list in case
1021 * we allocate blocks but write fails for some reason
1023 needed_blocks
= ext4_writepage_trans_blocks(inode
) + 1;
1024 index
= pos
>> PAGE_CACHE_SHIFT
;
1025 from
= pos
& (PAGE_CACHE_SIZE
- 1);
1028 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
1029 ret
= ext4_try_to_write_inline_data(mapping
, inode
, pos
, len
,
1038 * grab_cache_page_write_begin() can take a long time if the
1039 * system is thrashing due to memory pressure, or if the page
1040 * is being written back. So grab it first before we start
1041 * the transaction handle. This also allows us to allocate
1042 * the page (if needed) without using GFP_NOFS.
1045 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
1051 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
, needed_blocks
);
1052 if (IS_ERR(handle
)) {
1053 page_cache_release(page
);
1054 return PTR_ERR(handle
);
1058 if (page
->mapping
!= mapping
) {
1059 /* The page got truncated from under us */
1061 page_cache_release(page
);
1062 ext4_journal_stop(handle
);
1065 /* In case writeback began while the page was unlocked */
1066 wait_for_stable_page(page
);
1068 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1069 if (ext4_should_dioread_nolock(inode
))
1070 ret
= ext4_block_write_begin(page
, pos
, len
,
1071 ext4_get_block_write
);
1073 ret
= ext4_block_write_begin(page
, pos
, len
,
1076 if (ext4_should_dioread_nolock(inode
))
1077 ret
= __block_write_begin(page
, pos
, len
, ext4_get_block_write
);
1079 ret
= __block_write_begin(page
, pos
, len
, ext4_get_block
);
1081 if (!ret
&& ext4_should_journal_data(inode
)) {
1082 ret
= ext4_walk_page_buffers(handle
, page_buffers(page
),
1084 do_journal_get_write_access
);
1090 * __block_write_begin may have instantiated a few blocks
1091 * outside i_size. Trim these off again. Don't need
1092 * i_size_read because we hold i_mutex.
1094 * Add inode to orphan list in case we crash before
1097 if (pos
+ len
> inode
->i_size
&& ext4_can_truncate(inode
))
1098 ext4_orphan_add(handle
, inode
);
1100 ext4_journal_stop(handle
);
1101 if (pos
+ len
> inode
->i_size
) {
1102 ext4_truncate_failed_write(inode
);
1104 * If truncate failed early the inode might
1105 * still be on the orphan list; we need to
1106 * make sure the inode is removed from the
1107 * orphan list in that case.
1110 ext4_orphan_del(NULL
, inode
);
1113 if (ret
== -ENOSPC
&&
1114 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1116 page_cache_release(page
);
1123 /* For write_end() in data=journal mode */
1124 static int write_end_fn(handle_t
*handle
, struct buffer_head
*bh
)
1127 if (!buffer_mapped(bh
) || buffer_freed(bh
))
1129 set_buffer_uptodate(bh
);
1130 ret
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
1131 clear_buffer_meta(bh
);
1132 clear_buffer_prio(bh
);
1137 * We need to pick up the new inode size which generic_commit_write gave us
1138 * `file' can be NULL - eg, when called from page_symlink().
1140 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1141 * buffers are managed internally.
1143 static int ext4_write_end(struct file
*file
,
1144 struct address_space
*mapping
,
1145 loff_t pos
, unsigned len
, unsigned copied
,
1146 struct page
*page
, void *fsdata
)
1148 handle_t
*handle
= ext4_journal_current_handle();
1149 struct inode
*inode
= mapping
->host
;
1150 loff_t old_size
= inode
->i_size
;
1152 int i_size_changed
= 0;
1154 trace_ext4_write_end(inode
, pos
, len
, copied
);
1155 if (ext4_test_inode_state(inode
, EXT4_STATE_ORDERED_MODE
)) {
1156 ret
= ext4_jbd2_file_inode(handle
, inode
);
1159 page_cache_release(page
);
1164 if (ext4_has_inline_data(inode
)) {
1165 ret
= ext4_write_inline_data_end(inode
, pos
, len
,
1174 copied
= block_write_end(file
, mapping
, pos
,
1175 len
, copied
, page
, fsdata
);
1177 * it's important to update i_size while still holding page lock:
1178 * page writeout could otherwise come in and zero beyond i_size.
1180 i_size_changed
= ext4_update_inode_size(inode
, pos
+ copied
);
1182 page_cache_release(page
);
1185 pagecache_isize_extended(inode
, old_size
, pos
);
1187 * Don't mark the inode dirty under page lock. First, it unnecessarily
1188 * makes the holding time of page lock longer. Second, it forces lock
1189 * ordering of page lock and transaction start for journaling
1193 ext4_mark_inode_dirty(handle
, inode
);
1195 if (pos
+ len
> inode
->i_size
&& ext4_can_truncate(inode
))
1196 /* if we have allocated more blocks and copied
1197 * less. We will have blocks allocated outside
1198 * inode->i_size. So truncate them
1200 ext4_orphan_add(handle
, inode
);
1202 ret2
= ext4_journal_stop(handle
);
1206 if (pos
+ len
> inode
->i_size
) {
1207 ext4_truncate_failed_write(inode
);
1209 * If truncate failed early the inode might still be
1210 * on the orphan list; we need to make sure the inode
1211 * is removed from the orphan list in that case.
1214 ext4_orphan_del(NULL
, inode
);
1217 return ret
? ret
: copied
;
1221 * This is a private version of page_zero_new_buffers() which doesn't
1222 * set the buffer to be dirty, since in data=journalled mode we need
1223 * to call ext4_handle_dirty_metadata() instead.
1225 static void ext4_journalled_zero_new_buffers(handle_t
*handle
,
1227 unsigned from
, unsigned to
)
1229 unsigned int block_start
= 0, block_end
;
1230 struct buffer_head
*head
, *bh
;
1232 bh
= head
= page_buffers(page
);
1234 block_end
= block_start
+ bh
->b_size
;
1235 if (buffer_new(bh
)) {
1236 if (block_end
> from
&& block_start
< to
) {
1237 if (!PageUptodate(page
)) {
1238 unsigned start
, size
;
1240 start
= max(from
, block_start
);
1241 size
= min(to
, block_end
) - start
;
1243 zero_user(page
, start
, size
);
1244 write_end_fn(handle
, bh
);
1246 clear_buffer_new(bh
);
1249 block_start
= block_end
;
1250 bh
= bh
->b_this_page
;
1251 } while (bh
!= head
);
1254 static int ext4_journalled_write_end(struct file
*file
,
1255 struct address_space
*mapping
,
1256 loff_t pos
, unsigned len
, unsigned copied
,
1257 struct page
*page
, void *fsdata
)
1259 handle_t
*handle
= ext4_journal_current_handle();
1260 struct inode
*inode
= mapping
->host
;
1261 loff_t old_size
= inode
->i_size
;
1265 int size_changed
= 0;
1267 trace_ext4_journalled_write_end(inode
, pos
, len
, copied
);
1268 from
= pos
& (PAGE_CACHE_SIZE
- 1);
1271 BUG_ON(!ext4_handle_valid(handle
));
1273 if (ext4_has_inline_data(inode
)) {
1274 ret
= ext4_write_inline_data_end(inode
, pos
, len
,
1282 } else if (unlikely(copied
< len
) && !PageUptodate(page
)) {
1284 ext4_journalled_zero_new_buffers(handle
, page
, from
, to
);
1286 if (unlikely(copied
< len
))
1287 ext4_journalled_zero_new_buffers(handle
, page
,
1289 ret
= ext4_walk_page_buffers(handle
, page_buffers(page
), from
,
1290 from
+ copied
, &partial
,
1293 SetPageUptodate(page
);
1295 size_changed
= ext4_update_inode_size(inode
, pos
+ copied
);
1296 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
1297 EXT4_I(inode
)->i_datasync_tid
= handle
->h_transaction
->t_tid
;
1299 page_cache_release(page
);
1302 pagecache_isize_extended(inode
, old_size
, pos
);
1305 ret2
= ext4_mark_inode_dirty(handle
, inode
);
1310 if (pos
+ len
> inode
->i_size
&& ext4_can_truncate(inode
))
1311 /* if we have allocated more blocks and copied
1312 * less. We will have blocks allocated outside
1313 * inode->i_size. So truncate them
1315 ext4_orphan_add(handle
, inode
);
1318 ret2
= ext4_journal_stop(handle
);
1321 if (pos
+ len
> inode
->i_size
) {
1322 ext4_truncate_failed_write(inode
);
1324 * If truncate failed early the inode might still be
1325 * on the orphan list; we need to make sure the inode
1326 * is removed from the orphan list in that case.
1329 ext4_orphan_del(NULL
, inode
);
1332 return ret
? ret
: copied
;
1336 * Reserve space for a single cluster
1338 static int ext4_da_reserve_space(struct inode
*inode
)
1340 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1341 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1345 * We will charge metadata quota at writeout time; this saves
1346 * us from metadata over-estimation, though we may go over by
1347 * a small amount in the end. Here we just reserve for data.
1349 ret
= dquot_reserve_block(inode
, EXT4_C2B(sbi
, 1));
1353 spin_lock(&ei
->i_block_reservation_lock
);
1354 if (ext4_claim_free_clusters(sbi
, 1, 0)) {
1355 spin_unlock(&ei
->i_block_reservation_lock
);
1356 dquot_release_reservation_block(inode
, EXT4_C2B(sbi
, 1));
1359 ei
->i_reserved_data_blocks
++;
1360 trace_ext4_da_reserve_space(inode
);
1361 spin_unlock(&ei
->i_block_reservation_lock
);
1363 return 0; /* success */
1366 static void ext4_da_release_space(struct inode
*inode
, int to_free
)
1368 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1369 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1372 return; /* Nothing to release, exit */
1374 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1376 trace_ext4_da_release_space(inode
, to_free
);
1377 if (unlikely(to_free
> ei
->i_reserved_data_blocks
)) {
1379 * if there aren't enough reserved blocks, then the
1380 * counter is messed up somewhere. Since this
1381 * function is called from invalidate page, it's
1382 * harmless to return without any action.
1384 ext4_warning(inode
->i_sb
, "ext4_da_release_space: "
1385 "ino %lu, to_free %d with only %d reserved "
1386 "data blocks", inode
->i_ino
, to_free
,
1387 ei
->i_reserved_data_blocks
);
1389 to_free
= ei
->i_reserved_data_blocks
;
1391 ei
->i_reserved_data_blocks
-= to_free
;
1393 /* update fs dirty data blocks counter */
1394 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
, to_free
);
1396 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1398 dquot_release_reservation_block(inode
, EXT4_C2B(sbi
, to_free
));
1401 static void ext4_da_page_release_reservation(struct page
*page
,
1402 unsigned int offset
,
1403 unsigned int length
)
1405 int to_release
= 0, contiguous_blks
= 0;
1406 struct buffer_head
*head
, *bh
;
1407 unsigned int curr_off
= 0;
1408 struct inode
*inode
= page
->mapping
->host
;
1409 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1410 unsigned int stop
= offset
+ length
;
1414 BUG_ON(stop
> PAGE_CACHE_SIZE
|| stop
< length
);
1416 head
= page_buffers(page
);
1419 unsigned int next_off
= curr_off
+ bh
->b_size
;
1421 if (next_off
> stop
)
1424 if ((offset
<= curr_off
) && (buffer_delay(bh
))) {
1427 clear_buffer_delay(bh
);
1428 } else if (contiguous_blks
) {
1429 lblk
= page
->index
<<
1430 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1431 lblk
+= (curr_off
>> inode
->i_blkbits
) -
1433 ext4_es_remove_extent(inode
, lblk
, contiguous_blks
);
1434 contiguous_blks
= 0;
1436 curr_off
= next_off
;
1437 } while ((bh
= bh
->b_this_page
) != head
);
1439 if (contiguous_blks
) {
1440 lblk
= page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1441 lblk
+= (curr_off
>> inode
->i_blkbits
) - contiguous_blks
;
1442 ext4_es_remove_extent(inode
, lblk
, contiguous_blks
);
1445 /* If we have released all the blocks belonging to a cluster, then we
1446 * need to release the reserved space for that cluster. */
1447 num_clusters
= EXT4_NUM_B2C(sbi
, to_release
);
1448 while (num_clusters
> 0) {
1449 lblk
= (page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
)) +
1450 ((num_clusters
- 1) << sbi
->s_cluster_bits
);
1451 if (sbi
->s_cluster_ratio
== 1 ||
1452 !ext4_find_delalloc_cluster(inode
, lblk
))
1453 ext4_da_release_space(inode
, 1);
1460 * Delayed allocation stuff
1463 struct mpage_da_data
{
1464 struct inode
*inode
;
1465 struct writeback_control
*wbc
;
1467 pgoff_t first_page
; /* The first page to write */
1468 pgoff_t next_page
; /* Current page to examine */
1469 pgoff_t last_page
; /* Last page to examine */
1471 * Extent to map - this can be after first_page because that can be
1472 * fully mapped. We somewhat abuse m_flags to store whether the extent
1473 * is delalloc or unwritten.
1475 struct ext4_map_blocks map
;
1476 struct ext4_io_submit io_submit
; /* IO submission data */
1479 static void mpage_release_unused_pages(struct mpage_da_data
*mpd
,
1484 struct pagevec pvec
;
1485 struct inode
*inode
= mpd
->inode
;
1486 struct address_space
*mapping
= inode
->i_mapping
;
1488 /* This is necessary when next_page == 0. */
1489 if (mpd
->first_page
>= mpd
->next_page
)
1492 index
= mpd
->first_page
;
1493 end
= mpd
->next_page
- 1;
1495 ext4_lblk_t start
, last
;
1496 start
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1497 last
= end
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1498 ext4_es_remove_extent(inode
, start
, last
- start
+ 1);
1501 pagevec_init(&pvec
, 0);
1502 while (index
<= end
) {
1503 nr_pages
= pagevec_lookup(&pvec
, mapping
, index
, PAGEVEC_SIZE
);
1506 for (i
= 0; i
< nr_pages
; i
++) {
1507 struct page
*page
= pvec
.pages
[i
];
1508 if (page
->index
> end
)
1510 BUG_ON(!PageLocked(page
));
1511 BUG_ON(PageWriteback(page
));
1513 block_invalidatepage(page
, 0, PAGE_CACHE_SIZE
);
1514 ClearPageUptodate(page
);
1518 index
= pvec
.pages
[nr_pages
- 1]->index
+ 1;
1519 pagevec_release(&pvec
);
1523 static void ext4_print_free_blocks(struct inode
*inode
)
1525 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1526 struct super_block
*sb
= inode
->i_sb
;
1527 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1529 ext4_msg(sb
, KERN_CRIT
, "Total free blocks count %lld",
1530 EXT4_C2B(EXT4_SB(inode
->i_sb
),
1531 ext4_count_free_clusters(sb
)));
1532 ext4_msg(sb
, KERN_CRIT
, "Free/Dirty block details");
1533 ext4_msg(sb
, KERN_CRIT
, "free_blocks=%lld",
1534 (long long) EXT4_C2B(EXT4_SB(sb
),
1535 percpu_counter_sum(&sbi
->s_freeclusters_counter
)));
1536 ext4_msg(sb
, KERN_CRIT
, "dirty_blocks=%lld",
1537 (long long) EXT4_C2B(EXT4_SB(sb
),
1538 percpu_counter_sum(&sbi
->s_dirtyclusters_counter
)));
1539 ext4_msg(sb
, KERN_CRIT
, "Block reservation details");
1540 ext4_msg(sb
, KERN_CRIT
, "i_reserved_data_blocks=%u",
1541 ei
->i_reserved_data_blocks
);
1545 static int ext4_bh_delay_or_unwritten(handle_t
*handle
, struct buffer_head
*bh
)
1547 return (buffer_delay(bh
) || buffer_unwritten(bh
)) && buffer_dirty(bh
);
1551 * This function is grabs code from the very beginning of
1552 * ext4_map_blocks, but assumes that the caller is from delayed write
1553 * time. This function looks up the requested blocks and sets the
1554 * buffer delay bit under the protection of i_data_sem.
1556 static int ext4_da_map_blocks(struct inode
*inode
, sector_t iblock
,
1557 struct ext4_map_blocks
*map
,
1558 struct buffer_head
*bh
)
1560 struct extent_status es
;
1562 sector_t invalid_block
= ~((sector_t
) 0xffff);
1563 #ifdef ES_AGGRESSIVE_TEST
1564 struct ext4_map_blocks orig_map
;
1566 memcpy(&orig_map
, map
, sizeof(*map
));
1569 if (invalid_block
< ext4_blocks_count(EXT4_SB(inode
->i_sb
)->s_es
))
1573 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1574 "logical block %lu\n", inode
->i_ino
, map
->m_len
,
1575 (unsigned long) map
->m_lblk
);
1577 /* Lookup extent status tree firstly */
1578 if (ext4_es_lookup_extent(inode
, iblock
, &es
)) {
1579 if (ext4_es_is_hole(&es
)) {
1581 down_read(&EXT4_I(inode
)->i_data_sem
);
1586 * Delayed extent could be allocated by fallocate.
1587 * So we need to check it.
1589 if (ext4_es_is_delayed(&es
) && !ext4_es_is_unwritten(&es
)) {
1590 map_bh(bh
, inode
->i_sb
, invalid_block
);
1592 set_buffer_delay(bh
);
1596 map
->m_pblk
= ext4_es_pblock(&es
) + iblock
- es
.es_lblk
;
1597 retval
= es
.es_len
- (iblock
- es
.es_lblk
);
1598 if (retval
> map
->m_len
)
1599 retval
= map
->m_len
;
1600 map
->m_len
= retval
;
1601 if (ext4_es_is_written(&es
))
1602 map
->m_flags
|= EXT4_MAP_MAPPED
;
1603 else if (ext4_es_is_unwritten(&es
))
1604 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
1608 #ifdef ES_AGGRESSIVE_TEST
1609 ext4_map_blocks_es_recheck(NULL
, inode
, map
, &orig_map
, 0);
1615 * Try to see if we can get the block without requesting a new
1616 * file system block.
1618 down_read(&EXT4_I(inode
)->i_data_sem
);
1619 if (ext4_has_inline_data(inode
))
1621 else if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
1622 retval
= ext4_ext_map_blocks(NULL
, inode
, map
, 0);
1624 retval
= ext4_ind_map_blocks(NULL
, inode
, map
, 0);
1630 * XXX: __block_prepare_write() unmaps passed block,
1634 * If the block was allocated from previously allocated cluster,
1635 * then we don't need to reserve it again. However we still need
1636 * to reserve metadata for every block we're going to write.
1638 if (EXT4_SB(inode
->i_sb
)->s_cluster_ratio
== 1 ||
1639 !ext4_find_delalloc_cluster(inode
, map
->m_lblk
)) {
1640 ret
= ext4_da_reserve_space(inode
);
1642 /* not enough space to reserve */
1648 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
, map
->m_len
,
1649 ~0, EXTENT_STATUS_DELAYED
);
1655 map_bh(bh
, inode
->i_sb
, invalid_block
);
1657 set_buffer_delay(bh
);
1658 } else if (retval
> 0) {
1660 unsigned int status
;
1662 if (unlikely(retval
!= map
->m_len
)) {
1663 ext4_warning(inode
->i_sb
,
1664 "ES len assertion failed for inode "
1665 "%lu: retval %d != map->m_len %d",
1666 inode
->i_ino
, retval
, map
->m_len
);
1670 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
1671 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
1672 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
, map
->m_len
,
1673 map
->m_pblk
, status
);
1679 up_read((&EXT4_I(inode
)->i_data_sem
));
1685 * This is a special get_block_t callback which is used by
1686 * ext4_da_write_begin(). It will either return mapped block or
1687 * reserve space for a single block.
1689 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1690 * We also have b_blocknr = -1 and b_bdev initialized properly
1692 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1693 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1694 * initialized properly.
1696 int ext4_da_get_block_prep(struct inode
*inode
, sector_t iblock
,
1697 struct buffer_head
*bh
, int create
)
1699 struct ext4_map_blocks map
;
1702 BUG_ON(create
== 0);
1703 BUG_ON(bh
->b_size
!= inode
->i_sb
->s_blocksize
);
1705 map
.m_lblk
= iblock
;
1709 * first, we need to know whether the block is allocated already
1710 * preallocated blocks are unmapped but should treated
1711 * the same as allocated blocks.
1713 ret
= ext4_da_map_blocks(inode
, iblock
, &map
, bh
);
1717 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
1718 ext4_update_bh_state(bh
, map
.m_flags
);
1720 if (buffer_unwritten(bh
)) {
1721 /* A delayed write to unwritten bh should be marked
1722 * new and mapped. Mapped ensures that we don't do
1723 * get_block multiple times when we write to the same
1724 * offset and new ensures that we do proper zero out
1725 * for partial write.
1728 set_buffer_mapped(bh
);
1733 static int bget_one(handle_t
*handle
, struct buffer_head
*bh
)
1739 static int bput_one(handle_t
*handle
, struct buffer_head
*bh
)
1745 static int __ext4_journalled_writepage(struct page
*page
,
1748 struct address_space
*mapping
= page
->mapping
;
1749 struct inode
*inode
= mapping
->host
;
1750 struct buffer_head
*page_bufs
= NULL
;
1751 handle_t
*handle
= NULL
;
1752 int ret
= 0, err
= 0;
1753 int inline_data
= ext4_has_inline_data(inode
);
1754 struct buffer_head
*inode_bh
= NULL
;
1756 ClearPageChecked(page
);
1759 BUG_ON(page
->index
!= 0);
1760 BUG_ON(len
> ext4_get_max_inline_size(inode
));
1761 inode_bh
= ext4_journalled_write_inline_data(inode
, len
, page
);
1762 if (inode_bh
== NULL
)
1765 page_bufs
= page_buffers(page
);
1770 ext4_walk_page_buffers(handle
, page_bufs
, 0, len
,
1774 * We need to release the page lock before we start the
1775 * journal, so grab a reference so the page won't disappear
1776 * out from under us.
1781 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
1782 ext4_writepage_trans_blocks(inode
));
1783 if (IS_ERR(handle
)) {
1784 ret
= PTR_ERR(handle
);
1786 goto out_no_pagelock
;
1788 BUG_ON(!ext4_handle_valid(handle
));
1792 if (page
->mapping
!= mapping
) {
1793 /* The page got truncated from under us */
1794 ext4_journal_stop(handle
);
1800 BUFFER_TRACE(inode_bh
, "get write access");
1801 ret
= ext4_journal_get_write_access(handle
, inode_bh
);
1803 err
= ext4_handle_dirty_metadata(handle
, inode
, inode_bh
);
1806 ret
= ext4_walk_page_buffers(handle
, page_bufs
, 0, len
, NULL
,
1807 do_journal_get_write_access
);
1809 err
= ext4_walk_page_buffers(handle
, page_bufs
, 0, len
, NULL
,
1814 EXT4_I(inode
)->i_datasync_tid
= handle
->h_transaction
->t_tid
;
1815 err
= ext4_journal_stop(handle
);
1819 if (!ext4_has_inline_data(inode
))
1820 ext4_walk_page_buffers(NULL
, page_bufs
, 0, len
,
1822 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
1831 * Note that we don't need to start a transaction unless we're journaling data
1832 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1833 * need to file the inode to the transaction's list in ordered mode because if
1834 * we are writing back data added by write(), the inode is already there and if
1835 * we are writing back data modified via mmap(), no one guarantees in which
1836 * transaction the data will hit the disk. In case we are journaling data, we
1837 * cannot start transaction directly because transaction start ranks above page
1838 * lock so we have to do some magic.
1840 * This function can get called via...
1841 * - ext4_writepages after taking page lock (have journal handle)
1842 * - journal_submit_inode_data_buffers (no journal handle)
1843 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
1844 * - grab_page_cache when doing write_begin (have journal handle)
1846 * We don't do any block allocation in this function. If we have page with
1847 * multiple blocks we need to write those buffer_heads that are mapped. This
1848 * is important for mmaped based write. So if we do with blocksize 1K
1849 * truncate(f, 1024);
1850 * a = mmap(f, 0, 4096);
1852 * truncate(f, 4096);
1853 * we have in the page first buffer_head mapped via page_mkwrite call back
1854 * but other buffer_heads would be unmapped but dirty (dirty done via the
1855 * do_wp_page). So writepage should write the first block. If we modify
1856 * the mmap area beyond 1024 we will again get a page_fault and the
1857 * page_mkwrite callback will do the block allocation and mark the
1858 * buffer_heads mapped.
1860 * We redirty the page if we have any buffer_heads that is either delay or
1861 * unwritten in the page.
1863 * We can get recursively called as show below.
1865 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1868 * But since we don't do any block allocation we should not deadlock.
1869 * Page also have the dirty flag cleared so we don't get recurive page_lock.
1871 static int ext4_writepage(struct page
*page
,
1872 struct writeback_control
*wbc
)
1877 struct buffer_head
*page_bufs
= NULL
;
1878 struct inode
*inode
= page
->mapping
->host
;
1879 struct ext4_io_submit io_submit
;
1880 bool keep_towrite
= false;
1882 trace_ext4_writepage(page
);
1883 size
= i_size_read(inode
);
1884 if (page
->index
== size
>> PAGE_CACHE_SHIFT
)
1885 len
= size
& ~PAGE_CACHE_MASK
;
1887 len
= PAGE_CACHE_SIZE
;
1889 page_bufs
= page_buffers(page
);
1891 * We cannot do block allocation or other extent handling in this
1892 * function. If there are buffers needing that, we have to redirty
1893 * the page. But we may reach here when we do a journal commit via
1894 * journal_submit_inode_data_buffers() and in that case we must write
1895 * allocated buffers to achieve data=ordered mode guarantees.
1897 * Also, if there is only one buffer per page (the fs block
1898 * size == the page size), if one buffer needs block
1899 * allocation or needs to modify the extent tree to clear the
1900 * unwritten flag, we know that the page can't be written at
1901 * all, so we might as well refuse the write immediately.
1902 * Unfortunately if the block size != page size, we can't as
1903 * easily detect this case using ext4_walk_page_buffers(), but
1904 * for the extremely common case, this is an optimization that
1905 * skips a useless round trip through ext4_bio_write_page().
1907 if (ext4_walk_page_buffers(NULL
, page_bufs
, 0, len
, NULL
,
1908 ext4_bh_delay_or_unwritten
)) {
1909 redirty_page_for_writepage(wbc
, page
);
1910 if ((current
->flags
& PF_MEMALLOC
) ||
1911 (inode
->i_sb
->s_blocksize
== PAGE_CACHE_SIZE
)) {
1913 * For memory cleaning there's no point in writing only
1914 * some buffers. So just bail out. Warn if we came here
1915 * from direct reclaim.
1917 WARN_ON_ONCE((current
->flags
& (PF_MEMALLOC
|PF_KSWAPD
))
1922 keep_towrite
= true;
1925 if (PageChecked(page
) && ext4_should_journal_data(inode
))
1927 * It's mmapped pagecache. Add buffers and journal it. There
1928 * doesn't seem much point in redirtying the page here.
1930 return __ext4_journalled_writepage(page
, len
);
1932 ext4_io_submit_init(&io_submit
, wbc
);
1933 io_submit
.io_end
= ext4_init_io_end(inode
, GFP_NOFS
);
1934 if (!io_submit
.io_end
) {
1935 redirty_page_for_writepage(wbc
, page
);
1939 ret
= ext4_bio_write_page(&io_submit
, page
, len
, wbc
, keep_towrite
);
1940 ext4_io_submit(&io_submit
);
1941 /* Drop io_end reference we got from init */
1942 ext4_put_io_end_defer(io_submit
.io_end
);
1946 static int mpage_submit_page(struct mpage_da_data
*mpd
, struct page
*page
)
1949 loff_t size
= i_size_read(mpd
->inode
);
1952 BUG_ON(page
->index
!= mpd
->first_page
);
1953 if (page
->index
== size
>> PAGE_CACHE_SHIFT
)
1954 len
= size
& ~PAGE_CACHE_MASK
;
1956 len
= PAGE_CACHE_SIZE
;
1957 clear_page_dirty_for_io(page
);
1958 err
= ext4_bio_write_page(&mpd
->io_submit
, page
, len
, mpd
->wbc
, false);
1960 mpd
->wbc
->nr_to_write
--;
1966 #define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
1969 * mballoc gives us at most this number of blocks...
1970 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
1971 * The rest of mballoc seems to handle chunks up to full group size.
1973 #define MAX_WRITEPAGES_EXTENT_LEN 2048
1976 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
1978 * @mpd - extent of blocks
1979 * @lblk - logical number of the block in the file
1980 * @bh - buffer head we want to add to the extent
1982 * The function is used to collect contig. blocks in the same state. If the
1983 * buffer doesn't require mapping for writeback and we haven't started the
1984 * extent of buffers to map yet, the function returns 'true' immediately - the
1985 * caller can write the buffer right away. Otherwise the function returns true
1986 * if the block has been added to the extent, false if the block couldn't be
1989 static bool mpage_add_bh_to_extent(struct mpage_da_data
*mpd
, ext4_lblk_t lblk
,
1990 struct buffer_head
*bh
)
1992 struct ext4_map_blocks
*map
= &mpd
->map
;
1994 /* Buffer that doesn't need mapping for writeback? */
1995 if (!buffer_dirty(bh
) || !buffer_mapped(bh
) ||
1996 (!buffer_delay(bh
) && !buffer_unwritten(bh
))) {
1997 /* So far no extent to map => we write the buffer right away */
1998 if (map
->m_len
== 0)
2003 /* First block in the extent? */
2004 if (map
->m_len
== 0) {
2007 map
->m_flags
= bh
->b_state
& BH_FLAGS
;
2011 /* Don't go larger than mballoc is willing to allocate */
2012 if (map
->m_len
>= MAX_WRITEPAGES_EXTENT_LEN
)
2015 /* Can we merge the block to our big extent? */
2016 if (lblk
== map
->m_lblk
+ map
->m_len
&&
2017 (bh
->b_state
& BH_FLAGS
) == map
->m_flags
) {
2025 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
2027 * @mpd - extent of blocks for mapping
2028 * @head - the first buffer in the page
2029 * @bh - buffer we should start processing from
2030 * @lblk - logical number of the block in the file corresponding to @bh
2032 * Walk through page buffers from @bh upto @head (exclusive) and either submit
2033 * the page for IO if all buffers in this page were mapped and there's no
2034 * accumulated extent of buffers to map or add buffers in the page to the
2035 * extent of buffers to map. The function returns 1 if the caller can continue
2036 * by processing the next page, 0 if it should stop adding buffers to the
2037 * extent to map because we cannot extend it anymore. It can also return value
2038 * < 0 in case of error during IO submission.
2040 static int mpage_process_page_bufs(struct mpage_da_data
*mpd
,
2041 struct buffer_head
*head
,
2042 struct buffer_head
*bh
,
2045 struct inode
*inode
= mpd
->inode
;
2047 ext4_lblk_t blocks
= (i_size_read(inode
) + (1 << inode
->i_blkbits
) - 1)
2048 >> inode
->i_blkbits
;
2051 BUG_ON(buffer_locked(bh
));
2053 if (lblk
>= blocks
|| !mpage_add_bh_to_extent(mpd
, lblk
, bh
)) {
2054 /* Found extent to map? */
2057 /* Everything mapped so far and we hit EOF */
2060 } while (lblk
++, (bh
= bh
->b_this_page
) != head
);
2061 /* So far everything mapped? Submit the page for IO. */
2062 if (mpd
->map
.m_len
== 0) {
2063 err
= mpage_submit_page(mpd
, head
->b_page
);
2067 return lblk
< blocks
;
2071 * mpage_map_buffers - update buffers corresponding to changed extent and
2072 * submit fully mapped pages for IO
2074 * @mpd - description of extent to map, on return next extent to map
2076 * Scan buffers corresponding to changed extent (we expect corresponding pages
2077 * to be already locked) and update buffer state according to new extent state.
2078 * We map delalloc buffers to their physical location, clear unwritten bits,
2079 * and mark buffers as uninit when we perform writes to unwritten extents
2080 * and do extent conversion after IO is finished. If the last page is not fully
2081 * mapped, we update @map to the next extent in the last page that needs
2082 * mapping. Otherwise we submit the page for IO.
2084 static int mpage_map_and_submit_buffers(struct mpage_da_data
*mpd
)
2086 struct pagevec pvec
;
2088 struct inode
*inode
= mpd
->inode
;
2089 struct buffer_head
*head
, *bh
;
2090 int bpp_bits
= PAGE_CACHE_SHIFT
- inode
->i_blkbits
;
2096 start
= mpd
->map
.m_lblk
>> bpp_bits
;
2097 end
= (mpd
->map
.m_lblk
+ mpd
->map
.m_len
- 1) >> bpp_bits
;
2098 lblk
= start
<< bpp_bits
;
2099 pblock
= mpd
->map
.m_pblk
;
2101 pagevec_init(&pvec
, 0);
2102 while (start
<= end
) {
2103 nr_pages
= pagevec_lookup(&pvec
, inode
->i_mapping
, start
,
2107 for (i
= 0; i
< nr_pages
; i
++) {
2108 struct page
*page
= pvec
.pages
[i
];
2110 if (page
->index
> end
)
2112 /* Up to 'end' pages must be contiguous */
2113 BUG_ON(page
->index
!= start
);
2114 bh
= head
= page_buffers(page
);
2116 if (lblk
< mpd
->map
.m_lblk
)
2118 if (lblk
>= mpd
->map
.m_lblk
+ mpd
->map
.m_len
) {
2120 * Buffer after end of mapped extent.
2121 * Find next buffer in the page to map.
2124 mpd
->map
.m_flags
= 0;
2126 * FIXME: If dioread_nolock supports
2127 * blocksize < pagesize, we need to make
2128 * sure we add size mapped so far to
2129 * io_end->size as the following call
2130 * can submit the page for IO.
2132 err
= mpage_process_page_bufs(mpd
, head
,
2134 pagevec_release(&pvec
);
2139 if (buffer_delay(bh
)) {
2140 clear_buffer_delay(bh
);
2141 bh
->b_blocknr
= pblock
++;
2143 clear_buffer_unwritten(bh
);
2144 } while (lblk
++, (bh
= bh
->b_this_page
) != head
);
2147 * FIXME: This is going to break if dioread_nolock
2148 * supports blocksize < pagesize as we will try to
2149 * convert potentially unmapped parts of inode.
2151 mpd
->io_submit
.io_end
->size
+= PAGE_CACHE_SIZE
;
2152 /* Page fully mapped - let IO run! */
2153 err
= mpage_submit_page(mpd
, page
);
2155 pagevec_release(&pvec
);
2160 pagevec_release(&pvec
);
2162 /* Extent fully mapped and matches with page boundary. We are done. */
2164 mpd
->map
.m_flags
= 0;
2168 static int mpage_map_one_extent(handle_t
*handle
, struct mpage_da_data
*mpd
)
2170 struct inode
*inode
= mpd
->inode
;
2171 struct ext4_map_blocks
*map
= &mpd
->map
;
2172 int get_blocks_flags
;
2173 int err
, dioread_nolock
;
2175 trace_ext4_da_write_pages_extent(inode
, map
);
2177 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
2178 * to convert an unwritten extent to be initialized (in the case
2179 * where we have written into one or more preallocated blocks). It is
2180 * possible that we're going to need more metadata blocks than
2181 * previously reserved. However we must not fail because we're in
2182 * writeback and there is nothing we can do about it so it might result
2183 * in data loss. So use reserved blocks to allocate metadata if
2186 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
2187 * the blocks in question are delalloc blocks. This indicates
2188 * that the blocks and quotas has already been checked when
2189 * the data was copied into the page cache.
2191 get_blocks_flags
= EXT4_GET_BLOCKS_CREATE
|
2192 EXT4_GET_BLOCKS_METADATA_NOFAIL
;
2193 dioread_nolock
= ext4_should_dioread_nolock(inode
);
2195 get_blocks_flags
|= EXT4_GET_BLOCKS_IO_CREATE_EXT
;
2196 if (map
->m_flags
& (1 << BH_Delay
))
2197 get_blocks_flags
|= EXT4_GET_BLOCKS_DELALLOC_RESERVE
;
2199 err
= ext4_map_blocks(handle
, inode
, map
, get_blocks_flags
);
2202 if (dioread_nolock
&& (map
->m_flags
& EXT4_MAP_UNWRITTEN
)) {
2203 if (!mpd
->io_submit
.io_end
->handle
&&
2204 ext4_handle_valid(handle
)) {
2205 mpd
->io_submit
.io_end
->handle
= handle
->h_rsv_handle
;
2206 handle
->h_rsv_handle
= NULL
;
2208 ext4_set_io_unwritten_flag(inode
, mpd
->io_submit
.io_end
);
2211 BUG_ON(map
->m_len
== 0);
2212 if (map
->m_flags
& EXT4_MAP_NEW
) {
2213 struct block_device
*bdev
= inode
->i_sb
->s_bdev
;
2216 for (i
= 0; i
< map
->m_len
; i
++)
2217 unmap_underlying_metadata(bdev
, map
->m_pblk
+ i
);
2223 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2224 * mpd->len and submit pages underlying it for IO
2226 * @handle - handle for journal operations
2227 * @mpd - extent to map
2228 * @give_up_on_write - we set this to true iff there is a fatal error and there
2229 * is no hope of writing the data. The caller should discard
2230 * dirty pages to avoid infinite loops.
2232 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2233 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2234 * them to initialized or split the described range from larger unwritten
2235 * extent. Note that we need not map all the described range since allocation
2236 * can return less blocks or the range is covered by more unwritten extents. We
2237 * cannot map more because we are limited by reserved transaction credits. On
2238 * the other hand we always make sure that the last touched page is fully
2239 * mapped so that it can be written out (and thus forward progress is
2240 * guaranteed). After mapping we submit all mapped pages for IO.
2242 static int mpage_map_and_submit_extent(handle_t
*handle
,
2243 struct mpage_da_data
*mpd
,
2244 bool *give_up_on_write
)
2246 struct inode
*inode
= mpd
->inode
;
2247 struct ext4_map_blocks
*map
= &mpd
->map
;
2252 mpd
->io_submit
.io_end
->offset
=
2253 ((loff_t
)map
->m_lblk
) << inode
->i_blkbits
;
2255 err
= mpage_map_one_extent(handle
, mpd
);
2257 struct super_block
*sb
= inode
->i_sb
;
2259 if (EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
)
2260 goto invalidate_dirty_pages
;
2262 * Let the uper layers retry transient errors.
2263 * In the case of ENOSPC, if ext4_count_free_blocks()
2264 * is non-zero, a commit should free up blocks.
2266 if ((err
== -ENOMEM
) ||
2267 (err
== -ENOSPC
&& ext4_count_free_clusters(sb
))) {
2269 goto update_disksize
;
2272 ext4_msg(sb
, KERN_CRIT
,
2273 "Delayed block allocation failed for "
2274 "inode %lu at logical offset %llu with"
2275 " max blocks %u with error %d",
2277 (unsigned long long)map
->m_lblk
,
2278 (unsigned)map
->m_len
, -err
);
2279 ext4_msg(sb
, KERN_CRIT
,
2280 "This should not happen!! Data will "
2283 ext4_print_free_blocks(inode
);
2284 invalidate_dirty_pages
:
2285 *give_up_on_write
= true;
2290 * Update buffer state, submit mapped pages, and get us new
2293 err
= mpage_map_and_submit_buffers(mpd
);
2295 goto update_disksize
;
2296 } while (map
->m_len
);
2300 * Update on-disk size after IO is submitted. Races with
2301 * truncate are avoided by checking i_size under i_data_sem.
2303 disksize
= ((loff_t
)mpd
->first_page
) << PAGE_CACHE_SHIFT
;
2304 if (disksize
> EXT4_I(inode
)->i_disksize
) {
2308 down_write(&EXT4_I(inode
)->i_data_sem
);
2309 i_size
= i_size_read(inode
);
2310 if (disksize
> i_size
)
2312 if (disksize
> EXT4_I(inode
)->i_disksize
)
2313 EXT4_I(inode
)->i_disksize
= disksize
;
2314 err2
= ext4_mark_inode_dirty(handle
, inode
);
2315 up_write(&EXT4_I(inode
)->i_data_sem
);
2317 ext4_error(inode
->i_sb
,
2318 "Failed to mark inode %lu dirty",
2327 * Calculate the total number of credits to reserve for one writepages
2328 * iteration. This is called from ext4_writepages(). We map an extent of
2329 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
2330 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2331 * bpp - 1 blocks in bpp different extents.
2333 static int ext4_da_writepages_trans_blocks(struct inode
*inode
)
2335 int bpp
= ext4_journal_blocks_per_page(inode
);
2337 return ext4_meta_trans_blocks(inode
,
2338 MAX_WRITEPAGES_EXTENT_LEN
+ bpp
- 1, bpp
);
2342 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2343 * and underlying extent to map
2345 * @mpd - where to look for pages
2347 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2348 * IO immediately. When we find a page which isn't mapped we start accumulating
2349 * extent of buffers underlying these pages that needs mapping (formed by
2350 * either delayed or unwritten buffers). We also lock the pages containing
2351 * these buffers. The extent found is returned in @mpd structure (starting at
2352 * mpd->lblk with length mpd->len blocks).
2354 * Note that this function can attach bios to one io_end structure which are
2355 * neither logically nor physically contiguous. Although it may seem as an
2356 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2357 * case as we need to track IO to all buffers underlying a page in one io_end.
2359 static int mpage_prepare_extent_to_map(struct mpage_da_data
*mpd
)
2361 struct address_space
*mapping
= mpd
->inode
->i_mapping
;
2362 struct pagevec pvec
;
2363 unsigned int nr_pages
;
2364 long left
= mpd
->wbc
->nr_to_write
;
2365 pgoff_t index
= mpd
->first_page
;
2366 pgoff_t end
= mpd
->last_page
;
2369 int blkbits
= mpd
->inode
->i_blkbits
;
2371 struct buffer_head
*head
;
2373 if (mpd
->wbc
->sync_mode
== WB_SYNC_ALL
|| mpd
->wbc
->tagged_writepages
)
2374 tag
= PAGECACHE_TAG_TOWRITE
;
2376 tag
= PAGECACHE_TAG_DIRTY
;
2378 pagevec_init(&pvec
, 0);
2380 mpd
->next_page
= index
;
2381 while (index
<= end
) {
2382 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
2383 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
2387 for (i
= 0; i
< nr_pages
; i
++) {
2388 struct page
*page
= pvec
.pages
[i
];
2391 * At this point, the page may be truncated or
2392 * invalidated (changing page->mapping to NULL), or
2393 * even swizzled back from swapper_space to tmpfs file
2394 * mapping. However, page->index will not change
2395 * because we have a reference on the page.
2397 if (page
->index
> end
)
2401 * Accumulated enough dirty pages? This doesn't apply
2402 * to WB_SYNC_ALL mode. For integrity sync we have to
2403 * keep going because someone may be concurrently
2404 * dirtying pages, and we might have synced a lot of
2405 * newly appeared dirty pages, but have not synced all
2406 * of the old dirty pages.
2408 if (mpd
->wbc
->sync_mode
== WB_SYNC_NONE
&& left
<= 0)
2411 /* If we can't merge this page, we are done. */
2412 if (mpd
->map
.m_len
> 0 && mpd
->next_page
!= page
->index
)
2417 * If the page is no longer dirty, or its mapping no
2418 * longer corresponds to inode we are writing (which
2419 * means it has been truncated or invalidated), or the
2420 * page is already under writeback and we are not doing
2421 * a data integrity writeback, skip the page
2423 if (!PageDirty(page
) ||
2424 (PageWriteback(page
) &&
2425 (mpd
->wbc
->sync_mode
== WB_SYNC_NONE
)) ||
2426 unlikely(page
->mapping
!= mapping
)) {
2431 wait_on_page_writeback(page
);
2432 BUG_ON(PageWriteback(page
));
2434 if (mpd
->map
.m_len
== 0)
2435 mpd
->first_page
= page
->index
;
2436 mpd
->next_page
= page
->index
+ 1;
2437 /* Add all dirty buffers to mpd */
2438 lblk
= ((ext4_lblk_t
)page
->index
) <<
2439 (PAGE_CACHE_SHIFT
- blkbits
);
2440 head
= page_buffers(page
);
2441 err
= mpage_process_page_bufs(mpd
, head
, head
, lblk
);
2447 pagevec_release(&pvec
);
2452 pagevec_release(&pvec
);
2456 static int __writepage(struct page
*page
, struct writeback_control
*wbc
,
2459 struct address_space
*mapping
= data
;
2460 int ret
= ext4_writepage(page
, wbc
);
2461 mapping_set_error(mapping
, ret
);
2465 static int ext4_writepages(struct address_space
*mapping
,
2466 struct writeback_control
*wbc
)
2468 pgoff_t writeback_index
= 0;
2469 long nr_to_write
= wbc
->nr_to_write
;
2470 int range_whole
= 0;
2472 handle_t
*handle
= NULL
;
2473 struct mpage_da_data mpd
;
2474 struct inode
*inode
= mapping
->host
;
2475 int needed_blocks
, rsv_blocks
= 0, ret
= 0;
2476 struct ext4_sb_info
*sbi
= EXT4_SB(mapping
->host
->i_sb
);
2478 struct blk_plug plug
;
2479 bool give_up_on_write
= false;
2481 trace_ext4_writepages(inode
, wbc
);
2484 * No pages to write? This is mainly a kludge to avoid starting
2485 * a transaction for special inodes like journal inode on last iput()
2486 * because that could violate lock ordering on umount
2488 if (!mapping
->nrpages
|| !mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
2489 goto out_writepages
;
2491 if (ext4_should_journal_data(inode
)) {
2492 struct blk_plug plug
;
2494 blk_start_plug(&plug
);
2495 ret
= write_cache_pages(mapping
, wbc
, __writepage
, mapping
);
2496 blk_finish_plug(&plug
);
2497 goto out_writepages
;
2501 * If the filesystem has aborted, it is read-only, so return
2502 * right away instead of dumping stack traces later on that
2503 * will obscure the real source of the problem. We test
2504 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
2505 * the latter could be true if the filesystem is mounted
2506 * read-only, and in that case, ext4_writepages should
2507 * *never* be called, so if that ever happens, we would want
2510 if (unlikely(sbi
->s_mount_flags
& EXT4_MF_FS_ABORTED
)) {
2512 goto out_writepages
;
2515 if (ext4_should_dioread_nolock(inode
)) {
2517 * We may need to convert up to one extent per block in
2518 * the page and we may dirty the inode.
2520 rsv_blocks
= 1 + (PAGE_CACHE_SIZE
>> inode
->i_blkbits
);
2524 * If we have inline data and arrive here, it means that
2525 * we will soon create the block for the 1st page, so
2526 * we'd better clear the inline data here.
2528 if (ext4_has_inline_data(inode
)) {
2529 /* Just inode will be modified... */
2530 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
2531 if (IS_ERR(handle
)) {
2532 ret
= PTR_ERR(handle
);
2533 goto out_writepages
;
2535 BUG_ON(ext4_test_inode_state(inode
,
2536 EXT4_STATE_MAY_INLINE_DATA
));
2537 ext4_destroy_inline_data(handle
, inode
);
2538 ext4_journal_stop(handle
);
2541 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2544 if (wbc
->range_cyclic
) {
2545 writeback_index
= mapping
->writeback_index
;
2546 if (writeback_index
)
2548 mpd
.first_page
= writeback_index
;
2551 mpd
.first_page
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2552 mpd
.last_page
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2557 ext4_io_submit_init(&mpd
.io_submit
, wbc
);
2559 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2560 tag_pages_for_writeback(mapping
, mpd
.first_page
, mpd
.last_page
);
2562 blk_start_plug(&plug
);
2563 while (!done
&& mpd
.first_page
<= mpd
.last_page
) {
2564 /* For each extent of pages we use new io_end */
2565 mpd
.io_submit
.io_end
= ext4_init_io_end(inode
, GFP_KERNEL
);
2566 if (!mpd
.io_submit
.io_end
) {
2572 * We have two constraints: We find one extent to map and we
2573 * must always write out whole page (makes a difference when
2574 * blocksize < pagesize) so that we don't block on IO when we
2575 * try to write out the rest of the page. Journalled mode is
2576 * not supported by delalloc.
2578 BUG_ON(ext4_should_journal_data(inode
));
2579 needed_blocks
= ext4_da_writepages_trans_blocks(inode
);
2581 /* start a new transaction */
2582 handle
= ext4_journal_start_with_reserve(inode
,
2583 EXT4_HT_WRITE_PAGE
, needed_blocks
, rsv_blocks
);
2584 if (IS_ERR(handle
)) {
2585 ret
= PTR_ERR(handle
);
2586 ext4_msg(inode
->i_sb
, KERN_CRIT
, "%s: jbd2_start: "
2587 "%ld pages, ino %lu; err %d", __func__
,
2588 wbc
->nr_to_write
, inode
->i_ino
, ret
);
2589 /* Release allocated io_end */
2590 ext4_put_io_end(mpd
.io_submit
.io_end
);
2594 trace_ext4_da_write_pages(inode
, mpd
.first_page
, mpd
.wbc
);
2595 ret
= mpage_prepare_extent_to_map(&mpd
);
2598 ret
= mpage_map_and_submit_extent(handle
, &mpd
,
2602 * We scanned the whole range (or exhausted
2603 * nr_to_write), submitted what was mapped and
2604 * didn't find anything needing mapping. We are
2611 * Caution: If the handle is synchronous,
2612 * ext4_journal_stop() can wait for transaction commit
2613 * to finish which may depend on writeback of pages to
2614 * complete or on page lock to be released. In that
2615 * case, we have to wait until after after we have
2616 * submitted all the IO, released page locks we hold,
2617 * and dropped io_end reference (for extent conversion
2618 * to be able to complete) before stopping the handle.
2620 if (!ext4_handle_valid(handle
) || handle
->h_sync
== 0) {
2621 ext4_journal_stop(handle
);
2624 /* Submit prepared bio */
2625 ext4_io_submit(&mpd
.io_submit
);
2626 /* Unlock pages we didn't use */
2627 mpage_release_unused_pages(&mpd
, give_up_on_write
);
2629 * Drop our io_end reference we got from init. We have
2630 * to be careful and use deferred io_end finishing if
2631 * we are still holding the transaction as we can
2632 * release the last reference to io_end which may end
2633 * up doing unwritten extent conversion.
2636 ext4_put_io_end_defer(mpd
.io_submit
.io_end
);
2637 ext4_journal_stop(handle
);
2639 ext4_put_io_end(mpd
.io_submit
.io_end
);
2641 if (ret
== -ENOSPC
&& sbi
->s_journal
) {
2643 * Commit the transaction which would
2644 * free blocks released in the transaction
2647 jbd2_journal_force_commit_nested(sbi
->s_journal
);
2651 /* Fatal error - ENOMEM, EIO... */
2655 blk_finish_plug(&plug
);
2656 if (!ret
&& !cycled
&& wbc
->nr_to_write
> 0) {
2658 mpd
.last_page
= writeback_index
- 1;
2664 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
2666 * Set the writeback_index so that range_cyclic
2667 * mode will write it back later
2669 mapping
->writeback_index
= mpd
.first_page
;
2672 trace_ext4_writepages_result(inode
, wbc
, ret
,
2673 nr_to_write
- wbc
->nr_to_write
);
2677 static int ext4_nonda_switch(struct super_block
*sb
)
2679 s64 free_clusters
, dirty_clusters
;
2680 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2683 * switch to non delalloc mode if we are running low
2684 * on free block. The free block accounting via percpu
2685 * counters can get slightly wrong with percpu_counter_batch getting
2686 * accumulated on each CPU without updating global counters
2687 * Delalloc need an accurate free block accounting. So switch
2688 * to non delalloc when we are near to error range.
2691 percpu_counter_read_positive(&sbi
->s_freeclusters_counter
);
2693 percpu_counter_read_positive(&sbi
->s_dirtyclusters_counter
);
2695 * Start pushing delalloc when 1/2 of free blocks are dirty.
2697 if (dirty_clusters
&& (free_clusters
< 2 * dirty_clusters
))
2698 try_to_writeback_inodes_sb(sb
, WB_REASON_FS_FREE_SPACE
);
2700 if (2 * free_clusters
< 3 * dirty_clusters
||
2701 free_clusters
< (dirty_clusters
+ EXT4_FREECLUSTERS_WATERMARK
)) {
2703 * free block count is less than 150% of dirty blocks
2704 * or free blocks is less than watermark
2711 /* We always reserve for an inode update; the superblock could be there too */
2712 static int ext4_da_write_credits(struct inode
*inode
, loff_t pos
, unsigned len
)
2714 if (likely(ext4_has_feature_large_file(inode
->i_sb
)))
2717 if (pos
+ len
<= 0x7fffffffULL
)
2720 /* We might need to update the superblock to set LARGE_FILE */
2724 static int ext4_da_write_begin(struct file
*file
, struct address_space
*mapping
,
2725 loff_t pos
, unsigned len
, unsigned flags
,
2726 struct page
**pagep
, void **fsdata
)
2728 int ret
, retries
= 0;
2731 struct inode
*inode
= mapping
->host
;
2734 index
= pos
>> PAGE_CACHE_SHIFT
;
2736 if (ext4_nonda_switch(inode
->i_sb
)) {
2737 *fsdata
= (void *)FALL_BACK_TO_NONDELALLOC
;
2738 return ext4_write_begin(file
, mapping
, pos
,
2739 len
, flags
, pagep
, fsdata
);
2741 *fsdata
= (void *)0;
2742 trace_ext4_da_write_begin(inode
, pos
, len
, flags
);
2744 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
2745 ret
= ext4_da_write_inline_data_begin(mapping
, inode
,
2755 * grab_cache_page_write_begin() can take a long time if the
2756 * system is thrashing due to memory pressure, or if the page
2757 * is being written back. So grab it first before we start
2758 * the transaction handle. This also allows us to allocate
2759 * the page (if needed) without using GFP_NOFS.
2762 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
2768 * With delayed allocation, we don't log the i_disksize update
2769 * if there is delayed block allocation. But we still need
2770 * to journalling the i_disksize update if writes to the end
2771 * of file which has an already mapped buffer.
2774 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
2775 ext4_da_write_credits(inode
, pos
, len
));
2776 if (IS_ERR(handle
)) {
2777 page_cache_release(page
);
2778 return PTR_ERR(handle
);
2782 if (page
->mapping
!= mapping
) {
2783 /* The page got truncated from under us */
2785 page_cache_release(page
);
2786 ext4_journal_stop(handle
);
2789 /* In case writeback began while the page was unlocked */
2790 wait_for_stable_page(page
);
2792 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2793 ret
= ext4_block_write_begin(page
, pos
, len
,
2794 ext4_da_get_block_prep
);
2796 ret
= __block_write_begin(page
, pos
, len
, ext4_da_get_block_prep
);
2800 ext4_journal_stop(handle
);
2802 * block_write_begin may have instantiated a few blocks
2803 * outside i_size. Trim these off again. Don't need
2804 * i_size_read because we hold i_mutex.
2806 if (pos
+ len
> inode
->i_size
)
2807 ext4_truncate_failed_write(inode
);
2809 if (ret
== -ENOSPC
&&
2810 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
2813 page_cache_release(page
);
2822 * Check if we should update i_disksize
2823 * when write to the end of file but not require block allocation
2825 static int ext4_da_should_update_i_disksize(struct page
*page
,
2826 unsigned long offset
)
2828 struct buffer_head
*bh
;
2829 struct inode
*inode
= page
->mapping
->host
;
2833 bh
= page_buffers(page
);
2834 idx
= offset
>> inode
->i_blkbits
;
2836 for (i
= 0; i
< idx
; i
++)
2837 bh
= bh
->b_this_page
;
2839 if (!buffer_mapped(bh
) || (buffer_delay(bh
)) || buffer_unwritten(bh
))
2844 static int ext4_da_write_end(struct file
*file
,
2845 struct address_space
*mapping
,
2846 loff_t pos
, unsigned len
, unsigned copied
,
2847 struct page
*page
, void *fsdata
)
2849 struct inode
*inode
= mapping
->host
;
2851 handle_t
*handle
= ext4_journal_current_handle();
2853 unsigned long start
, end
;
2854 int write_mode
= (int)(unsigned long)fsdata
;
2856 if (write_mode
== FALL_BACK_TO_NONDELALLOC
)
2857 return ext4_write_end(file
, mapping
, pos
,
2858 len
, copied
, page
, fsdata
);
2860 trace_ext4_da_write_end(inode
, pos
, len
, copied
);
2861 start
= pos
& (PAGE_CACHE_SIZE
- 1);
2862 end
= start
+ copied
- 1;
2865 * generic_write_end() will run mark_inode_dirty() if i_size
2866 * changes. So let's piggyback the i_disksize mark_inode_dirty
2869 new_i_size
= pos
+ copied
;
2870 if (copied
&& new_i_size
> EXT4_I(inode
)->i_disksize
) {
2871 if (ext4_has_inline_data(inode
) ||
2872 ext4_da_should_update_i_disksize(page
, end
)) {
2873 ext4_update_i_disksize(inode
, new_i_size
);
2874 /* We need to mark inode dirty even if
2875 * new_i_size is less that inode->i_size
2876 * bu greater than i_disksize.(hint delalloc)
2878 ext4_mark_inode_dirty(handle
, inode
);
2882 if (write_mode
!= CONVERT_INLINE_DATA
&&
2883 ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
) &&
2884 ext4_has_inline_data(inode
))
2885 ret2
= ext4_da_write_inline_data_end(inode
, pos
, len
, copied
,
2888 ret2
= generic_write_end(file
, mapping
, pos
, len
, copied
,
2894 ret2
= ext4_journal_stop(handle
);
2898 return ret
? ret
: copied
;
2901 static void ext4_da_invalidatepage(struct page
*page
, unsigned int offset
,
2902 unsigned int length
)
2905 * Drop reserved blocks
2907 BUG_ON(!PageLocked(page
));
2908 if (!page_has_buffers(page
))
2911 ext4_da_page_release_reservation(page
, offset
, length
);
2914 ext4_invalidatepage(page
, offset
, length
);
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
)
2930 * We do something simple for now. The filemap_flush() will
2931 * also start triggering a write of the data blocks, which is
2932 * not strictly speaking necessary (and for users of
2933 * laptop_mode, not even desirable). However, to do otherwise
2934 * would require replicating code paths in:
2936 * ext4_writepages() ->
2937 * write_cache_pages() ---> (via passed in callback function)
2938 * __mpage_da_writepage() -->
2939 * mpage_add_bh_to_extent()
2940 * mpage_da_map_blocks()
2942 * The problem is that write_cache_pages(), located in
2943 * mm/page-writeback.c, marks pages clean in preparation for
2944 * doing I/O, which is not desirable if we're not planning on
2947 * We could call write_cache_pages(), and then redirty all of
2948 * the pages by calling redirty_page_for_writepage() but that
2949 * would be ugly in the extreme. So instead we would need to
2950 * replicate parts of the code in the above functions,
2951 * simplifying them because we wouldn't actually intend to
2952 * write out the pages, but rather only collect contiguous
2953 * logical block extents, call the multi-block allocator, and
2954 * then update the buffer heads with the block allocations.
2956 * For now, though, we'll cheat by calling filemap_flush(),
2957 * which will map the blocks, and start the I/O, but not
2958 * actually wait for the I/O to complete.
2960 return filemap_flush(inode
->i_mapping
);
2964 * bmap() is special. It gets used by applications such as lilo and by
2965 * the swapper to find the on-disk block of a specific piece of data.
2967 * Naturally, this is dangerous if the block concerned is still in the
2968 * journal. If somebody makes a swapfile on an ext4 data-journaling
2969 * filesystem and enables swap, then they may get a nasty shock when the
2970 * data getting swapped to that swapfile suddenly gets overwritten by
2971 * the original zero's written out previously to the journal and
2972 * awaiting writeback in the kernel's buffer cache.
2974 * So, if we see any bmap calls here on a modified, data-journaled file,
2975 * take extra steps to flush any blocks which might be in the cache.
2977 static sector_t
ext4_bmap(struct address_space
*mapping
, sector_t block
)
2979 struct inode
*inode
= mapping
->host
;
2984 * We can get here for an inline file via the FIBMAP ioctl
2986 if (ext4_has_inline_data(inode
))
2989 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
) &&
2990 test_opt(inode
->i_sb
, DELALLOC
)) {
2992 * With delalloc we want to sync the file
2993 * so that we can make sure we allocate
2996 filemap_write_and_wait(mapping
);
2999 if (EXT4_JOURNAL(inode
) &&
3000 ext4_test_inode_state(inode
, EXT4_STATE_JDATA
)) {
3002 * This is a REALLY heavyweight approach, but the use of
3003 * bmap on dirty files is expected to be extremely rare:
3004 * only if we run lilo or swapon on a freshly made file
3005 * do we expect this to happen.
3007 * (bmap requires CAP_SYS_RAWIO so this does not
3008 * represent an unprivileged user DOS attack --- we'd be
3009 * in trouble if mortal users could trigger this path at
3012 * NB. EXT4_STATE_JDATA is not set on files other than
3013 * regular files. If somebody wants to bmap a directory
3014 * or symlink and gets confused because the buffer
3015 * hasn't yet been flushed to disk, they deserve
3016 * everything they get.
3019 ext4_clear_inode_state(inode
, EXT4_STATE_JDATA
);
3020 journal
= EXT4_JOURNAL(inode
);
3021 jbd2_journal_lock_updates(journal
);
3022 err
= jbd2_journal_flush(journal
);
3023 jbd2_journal_unlock_updates(journal
);
3029 return generic_block_bmap(mapping
, block
, ext4_get_block
);
3032 static int ext4_readpage(struct file
*file
, struct page
*page
)
3035 struct inode
*inode
= page
->mapping
->host
;
3037 trace_ext4_readpage(page
);
3039 if (ext4_has_inline_data(inode
))
3040 ret
= ext4_readpage_inline(inode
, page
);
3043 return ext4_mpage_readpages(page
->mapping
, NULL
, page
, 1);
3049 ext4_readpages(struct file
*file
, struct address_space
*mapping
,
3050 struct list_head
*pages
, unsigned nr_pages
)
3052 struct inode
*inode
= mapping
->host
;
3054 /* If the file has inline data, no need to do readpages. */
3055 if (ext4_has_inline_data(inode
))
3058 return ext4_mpage_readpages(mapping
, pages
, NULL
, nr_pages
);
3061 static void ext4_invalidatepage(struct page
*page
, unsigned int offset
,
3062 unsigned int length
)
3064 trace_ext4_invalidatepage(page
, offset
, length
);
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
, length
);
3072 static int __ext4_journalled_invalidatepage(struct page
*page
,
3073 unsigned int offset
,
3074 unsigned int length
)
3076 journal_t
*journal
= EXT4_JOURNAL(page
->mapping
->host
);
3078 trace_ext4_journalled_invalidatepage(page
, offset
, length
);
3081 * If it's a full truncate we just forget about the pending dirtying
3083 if (offset
== 0 && length
== PAGE_CACHE_SIZE
)
3084 ClearPageChecked(page
);
3086 return jbd2_journal_invalidatepage(journal
, page
, offset
, length
);
3089 /* Wrapper for aops... */
3090 static void ext4_journalled_invalidatepage(struct page
*page
,
3091 unsigned int offset
,
3092 unsigned int length
)
3094 WARN_ON(__ext4_journalled_invalidatepage(page
, offset
, length
) < 0);
3097 static int ext4_releasepage(struct page
*page
, gfp_t wait
)
3099 journal_t
*journal
= EXT4_JOURNAL(page
->mapping
->host
);
3101 trace_ext4_releasepage(page
);
3103 /* Page has dirty journalled data -> cannot release */
3104 if (PageChecked(page
))
3107 return jbd2_journal_try_to_free_buffers(journal
, page
, wait
);
3109 return try_to_free_buffers(page
);
3113 * ext4_get_block used when preparing for a DIO write or buffer write.
3114 * We allocate an uinitialized extent if blocks haven't been allocated.
3115 * The extent will be converted to initialized after the IO is complete.
3117 int ext4_get_block_write(struct inode
*inode
, sector_t iblock
,
3118 struct buffer_head
*bh_result
, int create
)
3120 ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
3121 inode
->i_ino
, create
);
3122 return _ext4_get_block(inode
, iblock
, bh_result
,
3123 EXT4_GET_BLOCKS_IO_CREATE_EXT
);
3126 static int ext4_get_block_write_nolock(struct inode
*inode
, sector_t iblock
,
3127 struct buffer_head
*bh_result
, int create
)
3129 ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
3130 inode
->i_ino
, create
);
3131 return _ext4_get_block(inode
, iblock
, bh_result
,
3132 EXT4_GET_BLOCKS_NO_LOCK
);
3135 int ext4_get_block_dax(struct inode
*inode
, sector_t iblock
,
3136 struct buffer_head
*bh_result
, int create
)
3138 int flags
= EXT4_GET_BLOCKS_PRE_IO
| EXT4_GET_BLOCKS_UNWRIT_EXT
;
3140 flags
|= EXT4_GET_BLOCKS_CREATE
;
3141 ext4_debug("ext4_get_block_dax: inode %lu, create flag %d\n",
3142 inode
->i_ino
, create
);
3143 return _ext4_get_block(inode
, iblock
, bh_result
, flags
);
3146 static void ext4_end_io_dio(struct kiocb
*iocb
, loff_t offset
,
3147 ssize_t size
, void *private)
3149 ext4_io_end_t
*io_end
= iocb
->private;
3151 /* if not async direct IO just return */
3155 ext_debug("ext4_end_io_dio(): io_end 0x%p "
3156 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
3157 iocb
->private, io_end
->inode
->i_ino
, iocb
, offset
,
3160 iocb
->private = NULL
;
3161 io_end
->offset
= offset
;
3162 io_end
->size
= size
;
3163 ext4_put_io_end(io_end
);
3167 * For ext4 extent files, ext4 will do direct-io write to holes,
3168 * preallocated extents, and those write extend the file, no need to
3169 * fall back to buffered IO.
3171 * For holes, we fallocate those blocks, mark them as unwritten
3172 * If those blocks were preallocated, we mark sure they are split, but
3173 * still keep the range to write as unwritten.
3175 * The unwritten extents will be converted to written when DIO is completed.
3176 * For async direct IO, since the IO may still pending when return, we
3177 * set up an end_io call back function, which will do the conversion
3178 * when async direct IO completed.
3180 * If the O_DIRECT write will extend the file then add this inode to the
3181 * orphan list. So recovery will truncate it back to the original size
3182 * if the machine crashes during the write.
3185 static ssize_t
ext4_ext_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
,
3188 struct file
*file
= iocb
->ki_filp
;
3189 struct inode
*inode
= file
->f_mapping
->host
;
3191 size_t count
= iov_iter_count(iter
);
3193 get_block_t
*get_block_func
= NULL
;
3195 loff_t final_size
= offset
+ count
;
3196 ext4_io_end_t
*io_end
= NULL
;
3198 /* Use the old path for reads and writes beyond i_size. */
3199 if (iov_iter_rw(iter
) != WRITE
|| final_size
> inode
->i_size
)
3200 return ext4_ind_direct_IO(iocb
, iter
, offset
);
3202 BUG_ON(iocb
->private == NULL
);
3205 * Make all waiters for direct IO properly wait also for extent
3206 * conversion. This also disallows race between truncate() and
3207 * overwrite DIO as i_dio_count needs to be incremented under i_mutex.
3209 if (iov_iter_rw(iter
) == WRITE
)
3210 inode_dio_begin(inode
);
3212 /* If we do a overwrite dio, i_mutex locking can be released */
3213 overwrite
= *((int *)iocb
->private);
3216 down_read(&EXT4_I(inode
)->i_data_sem
);
3217 mutex_unlock(&inode
->i_mutex
);
3221 * We could direct write to holes and fallocate.
3223 * Allocated blocks to fill the hole are marked as
3224 * unwritten to prevent parallel buffered read to expose
3225 * the stale data before DIO complete the data IO.
3227 * As to previously fallocated extents, ext4 get_block will
3228 * just simply mark the buffer mapped but still keep the
3229 * extents unwritten.
3231 * For non AIO case, we will convert those unwritten extents
3232 * to written after return back from blockdev_direct_IO.
3234 * For async DIO, the conversion needs to be deferred when the
3235 * IO is completed. The ext4 end_io callback function will be
3236 * called to take care of the conversion work. Here for async
3237 * case, we allocate an io_end structure to hook to the iocb.
3239 iocb
->private = NULL
;
3240 ext4_inode_aio_set(inode
, NULL
);
3241 if (!is_sync_kiocb(iocb
)) {
3242 io_end
= ext4_init_io_end(inode
, GFP_NOFS
);
3248 * Grab reference for DIO. Will be dropped in ext4_end_io_dio()
3250 iocb
->private = ext4_get_io_end(io_end
);
3252 * we save the io structure for current async direct
3253 * IO, so that later ext4_map_blocks() could flag the
3254 * io structure whether there is a unwritten extents
3255 * needs to be converted when IO is completed.
3257 ext4_inode_aio_set(inode
, io_end
);
3261 get_block_func
= ext4_get_block_write_nolock
;
3263 get_block_func
= ext4_get_block_write
;
3264 dio_flags
= DIO_LOCKING
;
3266 #ifdef CONFIG_EXT4_FS_ENCRYPTION
3267 BUG_ON(ext4_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
));
3270 ret
= dax_do_io(iocb
, inode
, iter
, offset
, get_block_func
,
3271 ext4_end_io_dio
, dio_flags
);
3273 ret
= __blockdev_direct_IO(iocb
, inode
,
3274 inode
->i_sb
->s_bdev
, iter
, offset
,
3276 ext4_end_io_dio
, NULL
, dio_flags
);
3279 * Put our reference to io_end. This can free the io_end structure e.g.
3280 * in sync IO case or in case of error. It can even perform extent
3281 * conversion if all bios we submitted finished before we got here.
3282 * Note that in that case iocb->private can be already set to NULL
3286 ext4_inode_aio_set(inode
, NULL
);
3287 ext4_put_io_end(io_end
);
3289 * When no IO was submitted ext4_end_io_dio() was not
3290 * called so we have to put iocb's reference.
3292 if (ret
<= 0 && ret
!= -EIOCBQUEUED
&& iocb
->private) {
3293 WARN_ON(iocb
->private != io_end
);
3294 WARN_ON(io_end
->flag
& EXT4_IO_END_UNWRITTEN
);
3295 ext4_put_io_end(io_end
);
3296 iocb
->private = NULL
;
3299 if (ret
> 0 && !overwrite
&& ext4_test_inode_state(inode
,
3300 EXT4_STATE_DIO_UNWRITTEN
)) {
3303 * for non AIO case, since the IO is already
3304 * completed, we could do the conversion right here
3306 err
= ext4_convert_unwritten_extents(NULL
, inode
,
3310 ext4_clear_inode_state(inode
, EXT4_STATE_DIO_UNWRITTEN
);
3314 if (iov_iter_rw(iter
) == WRITE
)
3315 inode_dio_end(inode
);
3316 /* take i_mutex locking again if we do a ovewrite dio */
3318 up_read(&EXT4_I(inode
)->i_data_sem
);
3319 mutex_lock(&inode
->i_mutex
);
3325 static ssize_t
ext4_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
,
3328 struct file
*file
= iocb
->ki_filp
;
3329 struct inode
*inode
= file
->f_mapping
->host
;
3330 size_t count
= iov_iter_count(iter
);
3333 #ifdef CONFIG_EXT4_FS_ENCRYPTION
3334 if (ext4_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
3339 * If we are doing data journalling we don't support O_DIRECT
3341 if (ext4_should_journal_data(inode
))
3344 /* Let buffer I/O handle the inline data case. */
3345 if (ext4_has_inline_data(inode
))
3348 trace_ext4_direct_IO_enter(inode
, offset
, count
, iov_iter_rw(iter
));
3349 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3350 ret
= ext4_ext_direct_IO(iocb
, iter
, offset
);
3352 ret
= ext4_ind_direct_IO(iocb
, iter
, offset
);
3353 trace_ext4_direct_IO_exit(inode
, offset
, count
, iov_iter_rw(iter
), ret
);
3358 * Pages can be marked dirty completely asynchronously from ext4's journalling
3359 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3360 * much here because ->set_page_dirty is called under VFS locks. The page is
3361 * not necessarily locked.
3363 * We cannot just dirty the page and leave attached buffers clean, because the
3364 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3365 * or jbddirty because all the journalling code will explode.
3367 * So what we do is to mark the page "pending dirty" and next time writepage
3368 * is called, propagate that into the buffers appropriately.
3370 static int ext4_journalled_set_page_dirty(struct page
*page
)
3372 SetPageChecked(page
);
3373 return __set_page_dirty_nobuffers(page
);
3376 static const struct address_space_operations ext4_aops
= {
3377 .readpage
= ext4_readpage
,
3378 .readpages
= ext4_readpages
,
3379 .writepage
= ext4_writepage
,
3380 .writepages
= ext4_writepages
,
3381 .write_begin
= ext4_write_begin
,
3382 .write_end
= ext4_write_end
,
3384 .invalidatepage
= ext4_invalidatepage
,
3385 .releasepage
= ext4_releasepage
,
3386 .direct_IO
= ext4_direct_IO
,
3387 .migratepage
= buffer_migrate_page
,
3388 .is_partially_uptodate
= block_is_partially_uptodate
,
3389 .error_remove_page
= generic_error_remove_page
,
3392 static const struct address_space_operations ext4_journalled_aops
= {
3393 .readpage
= ext4_readpage
,
3394 .readpages
= ext4_readpages
,
3395 .writepage
= ext4_writepage
,
3396 .writepages
= ext4_writepages
,
3397 .write_begin
= ext4_write_begin
,
3398 .write_end
= ext4_journalled_write_end
,
3399 .set_page_dirty
= ext4_journalled_set_page_dirty
,
3401 .invalidatepage
= ext4_journalled_invalidatepage
,
3402 .releasepage
= ext4_releasepage
,
3403 .direct_IO
= ext4_direct_IO
,
3404 .is_partially_uptodate
= block_is_partially_uptodate
,
3405 .error_remove_page
= generic_error_remove_page
,
3408 static const struct address_space_operations ext4_da_aops
= {
3409 .readpage
= ext4_readpage
,
3410 .readpages
= ext4_readpages
,
3411 .writepage
= ext4_writepage
,
3412 .writepages
= ext4_writepages
,
3413 .write_begin
= ext4_da_write_begin
,
3414 .write_end
= ext4_da_write_end
,
3416 .invalidatepage
= ext4_da_invalidatepage
,
3417 .releasepage
= ext4_releasepage
,
3418 .direct_IO
= ext4_direct_IO
,
3419 .migratepage
= buffer_migrate_page
,
3420 .is_partially_uptodate
= block_is_partially_uptodate
,
3421 .error_remove_page
= generic_error_remove_page
,
3424 void ext4_set_aops(struct inode
*inode
)
3426 switch (ext4_inode_journal_mode(inode
)) {
3427 case EXT4_INODE_ORDERED_DATA_MODE
:
3428 ext4_set_inode_state(inode
, EXT4_STATE_ORDERED_MODE
);
3430 case EXT4_INODE_WRITEBACK_DATA_MODE
:
3431 ext4_clear_inode_state(inode
, EXT4_STATE_ORDERED_MODE
);
3433 case EXT4_INODE_JOURNAL_DATA_MODE
:
3434 inode
->i_mapping
->a_ops
= &ext4_journalled_aops
;
3439 if (test_opt(inode
->i_sb
, DELALLOC
))
3440 inode
->i_mapping
->a_ops
= &ext4_da_aops
;
3442 inode
->i_mapping
->a_ops
= &ext4_aops
;
3445 static int __ext4_block_zero_page_range(handle_t
*handle
,
3446 struct address_space
*mapping
, loff_t from
, loff_t length
)
3448 ext4_fsblk_t index
= from
>> PAGE_CACHE_SHIFT
;
3449 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3450 unsigned blocksize
, pos
;
3452 struct inode
*inode
= mapping
->host
;
3453 struct buffer_head
*bh
;
3457 page
= find_or_create_page(mapping
, from
>> PAGE_CACHE_SHIFT
,
3458 mapping_gfp_constraint(mapping
, ~__GFP_FS
));
3462 blocksize
= inode
->i_sb
->s_blocksize
;
3464 iblock
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
3466 if (!page_has_buffers(page
))
3467 create_empty_buffers(page
, blocksize
, 0);
3469 /* Find the buffer that contains "offset" */
3470 bh
= page_buffers(page
);
3472 while (offset
>= pos
) {
3473 bh
= bh
->b_this_page
;
3477 if (buffer_freed(bh
)) {
3478 BUFFER_TRACE(bh
, "freed: skip");
3481 if (!buffer_mapped(bh
)) {
3482 BUFFER_TRACE(bh
, "unmapped");
3483 ext4_get_block(inode
, iblock
, bh
, 0);
3484 /* unmapped? It's a hole - nothing to do */
3485 if (!buffer_mapped(bh
)) {
3486 BUFFER_TRACE(bh
, "still unmapped");
3491 /* Ok, it's mapped. Make sure it's up-to-date */
3492 if (PageUptodate(page
))
3493 set_buffer_uptodate(bh
);
3495 if (!buffer_uptodate(bh
)) {
3497 ll_rw_block(READ
, 1, &bh
);
3499 /* Uhhuh. Read error. Complain and punt. */
3500 if (!buffer_uptodate(bh
))
3502 if (S_ISREG(inode
->i_mode
) &&
3503 ext4_encrypted_inode(inode
)) {
3504 /* We expect the key to be set. */
3505 BUG_ON(!ext4_has_encryption_key(inode
));
3506 BUG_ON(blocksize
!= PAGE_CACHE_SIZE
);
3507 WARN_ON_ONCE(ext4_decrypt(page
));
3510 if (ext4_should_journal_data(inode
)) {
3511 BUFFER_TRACE(bh
, "get write access");
3512 err
= ext4_journal_get_write_access(handle
, bh
);
3516 zero_user(page
, offset
, length
);
3517 BUFFER_TRACE(bh
, "zeroed end of block");
3519 if (ext4_should_journal_data(inode
)) {
3520 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
3523 mark_buffer_dirty(bh
);
3524 if (ext4_test_inode_state(inode
, EXT4_STATE_ORDERED_MODE
))
3525 err
= ext4_jbd2_file_inode(handle
, inode
);
3530 page_cache_release(page
);
3535 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3536 * starting from file offset 'from'. The range to be zero'd must
3537 * be contained with in one block. If the specified range exceeds
3538 * the end of the block it will be shortened to end of the block
3539 * that cooresponds to 'from'
3541 static int ext4_block_zero_page_range(handle_t
*handle
,
3542 struct address_space
*mapping
, loff_t from
, loff_t length
)
3544 struct inode
*inode
= mapping
->host
;
3545 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3546 unsigned blocksize
= inode
->i_sb
->s_blocksize
;
3547 unsigned max
= blocksize
- (offset
& (blocksize
- 1));
3550 * correct length if it does not fall between
3551 * 'from' and the end of the block
3553 if (length
> max
|| length
< 0)
3557 return dax_zero_page_range(inode
, from
, length
, ext4_get_block
);
3558 return __ext4_block_zero_page_range(handle
, mapping
, from
, length
);
3562 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3563 * up to the end of the block which corresponds to `from'.
3564 * This required during truncate. We need to physically zero the tail end
3565 * of that block so it doesn't yield old data if the file is later grown.
3567 static int ext4_block_truncate_page(handle_t
*handle
,
3568 struct address_space
*mapping
, loff_t from
)
3570 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3573 struct inode
*inode
= mapping
->host
;
3575 /* If we are processing an encrypted inode during orphan list handling */
3576 if (ext4_encrypted_inode(inode
) && !ext4_has_encryption_key(inode
))
3579 blocksize
= inode
->i_sb
->s_blocksize
;
3580 length
= blocksize
- (offset
& (blocksize
- 1));
3582 return ext4_block_zero_page_range(handle
, mapping
, from
, length
);
3585 int ext4_zero_partial_blocks(handle_t
*handle
, struct inode
*inode
,
3586 loff_t lstart
, loff_t length
)
3588 struct super_block
*sb
= inode
->i_sb
;
3589 struct address_space
*mapping
= inode
->i_mapping
;
3590 unsigned partial_start
, partial_end
;
3591 ext4_fsblk_t start
, end
;
3592 loff_t byte_end
= (lstart
+ length
- 1);
3595 partial_start
= lstart
& (sb
->s_blocksize
- 1);
3596 partial_end
= byte_end
& (sb
->s_blocksize
- 1);
3598 start
= lstart
>> sb
->s_blocksize_bits
;
3599 end
= byte_end
>> sb
->s_blocksize_bits
;
3601 /* Handle partial zero within the single block */
3603 (partial_start
|| (partial_end
!= sb
->s_blocksize
- 1))) {
3604 err
= ext4_block_zero_page_range(handle
, mapping
,
3608 /* Handle partial zero out on the start of the range */
3609 if (partial_start
) {
3610 err
= ext4_block_zero_page_range(handle
, mapping
,
3611 lstart
, sb
->s_blocksize
);
3615 /* Handle partial zero out on the end of the range */
3616 if (partial_end
!= sb
->s_blocksize
- 1)
3617 err
= ext4_block_zero_page_range(handle
, mapping
,
3618 byte_end
- partial_end
,
3623 int ext4_can_truncate(struct inode
*inode
)
3625 if (S_ISREG(inode
->i_mode
))
3627 if (S_ISDIR(inode
->i_mode
))
3629 if (S_ISLNK(inode
->i_mode
))
3630 return !ext4_inode_is_fast_symlink(inode
);
3635 * We have to make sure i_disksize gets properly updated before we truncate
3636 * page cache due to hole punching or zero range. Otherwise i_disksize update
3637 * can get lost as it may have been postponed to submission of writeback but
3638 * that will never happen after we truncate page cache.
3640 int ext4_update_disksize_before_punch(struct inode
*inode
, loff_t offset
,
3644 loff_t size
= i_size_read(inode
);
3646 WARN_ON(!mutex_is_locked(&inode
->i_mutex
));
3647 if (offset
> size
|| offset
+ len
< size
)
3650 if (EXT4_I(inode
)->i_disksize
>= size
)
3653 handle
= ext4_journal_start(inode
, EXT4_HT_MISC
, 1);
3655 return PTR_ERR(handle
);
3656 ext4_update_i_disksize(inode
, size
);
3657 ext4_mark_inode_dirty(handle
, inode
);
3658 ext4_journal_stop(handle
);
3664 * ext4_punch_hole: punches a hole in a file by releasing the blocks
3665 * associated with the given offset and length
3667 * @inode: File inode
3668 * @offset: The offset where the hole will begin
3669 * @len: The length of the hole
3671 * Returns: 0 on success or negative on failure
3674 int ext4_punch_hole(struct inode
*inode
, loff_t offset
, loff_t length
)
3676 struct super_block
*sb
= inode
->i_sb
;
3677 ext4_lblk_t first_block
, stop_block
;
3678 struct address_space
*mapping
= inode
->i_mapping
;
3679 loff_t first_block_offset
, last_block_offset
;
3681 unsigned int credits
;
3684 if (!S_ISREG(inode
->i_mode
))
3687 trace_ext4_punch_hole(inode
, offset
, length
, 0);
3690 * Write out all dirty pages to avoid race conditions
3691 * Then release them.
3693 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
)) {
3694 ret
= filemap_write_and_wait_range(mapping
, offset
,
3695 offset
+ length
- 1);
3700 mutex_lock(&inode
->i_mutex
);
3702 /* No need to punch hole beyond i_size */
3703 if (offset
>= inode
->i_size
)
3707 * If the hole extends beyond i_size, set the hole
3708 * to end after the page that contains i_size
3710 if (offset
+ length
> inode
->i_size
) {
3711 length
= inode
->i_size
+
3712 PAGE_CACHE_SIZE
- (inode
->i_size
& (PAGE_CACHE_SIZE
- 1)) -
3716 if (offset
& (sb
->s_blocksize
- 1) ||
3717 (offset
+ length
) & (sb
->s_blocksize
- 1)) {
3719 * Attach jinode to inode for jbd2 if we do any zeroing of
3722 ret
= ext4_inode_attach_jinode(inode
);
3728 /* Wait all existing dio workers, newcomers will block on i_mutex */
3729 ext4_inode_block_unlocked_dio(inode
);
3730 inode_dio_wait(inode
);
3733 * Prevent page faults from reinstantiating pages we have released from
3736 down_write(&EXT4_I(inode
)->i_mmap_sem
);
3737 first_block_offset
= round_up(offset
, sb
->s_blocksize
);
3738 last_block_offset
= round_down((offset
+ length
), sb
->s_blocksize
) - 1;
3740 /* Now release the pages and zero block aligned part of pages*/
3741 if (last_block_offset
> first_block_offset
) {
3742 ret
= ext4_update_disksize_before_punch(inode
, offset
, length
);
3745 truncate_pagecache_range(inode
, first_block_offset
,
3749 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3750 credits
= ext4_writepage_trans_blocks(inode
);
3752 credits
= ext4_blocks_for_truncate(inode
);
3753 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
3754 if (IS_ERR(handle
)) {
3755 ret
= PTR_ERR(handle
);
3756 ext4_std_error(sb
, ret
);
3760 ret
= ext4_zero_partial_blocks(handle
, inode
, offset
,
3765 first_block
= (offset
+ sb
->s_blocksize
- 1) >>
3766 EXT4_BLOCK_SIZE_BITS(sb
);
3767 stop_block
= (offset
+ length
) >> EXT4_BLOCK_SIZE_BITS(sb
);
3769 /* If there are no blocks to remove, return now */
3770 if (first_block
>= stop_block
)
3773 down_write(&EXT4_I(inode
)->i_data_sem
);
3774 ext4_discard_preallocations(inode
);
3776 ret
= ext4_es_remove_extent(inode
, first_block
,
3777 stop_block
- first_block
);
3779 up_write(&EXT4_I(inode
)->i_data_sem
);
3783 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3784 ret
= ext4_ext_remove_space(inode
, first_block
,
3787 ret
= ext4_ind_remove_space(handle
, inode
, first_block
,
3790 up_write(&EXT4_I(inode
)->i_data_sem
);
3792 ext4_handle_sync(handle
);
3794 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3795 ext4_mark_inode_dirty(handle
, inode
);
3797 ext4_journal_stop(handle
);
3799 up_write(&EXT4_I(inode
)->i_mmap_sem
);
3800 ext4_inode_resume_unlocked_dio(inode
);
3802 mutex_unlock(&inode
->i_mutex
);
3806 int ext4_inode_attach_jinode(struct inode
*inode
)
3808 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3809 struct jbd2_inode
*jinode
;
3811 if (ei
->jinode
|| !EXT4_SB(inode
->i_sb
)->s_journal
)
3814 jinode
= jbd2_alloc_inode(GFP_KERNEL
);
3815 spin_lock(&inode
->i_lock
);
3818 spin_unlock(&inode
->i_lock
);
3821 ei
->jinode
= jinode
;
3822 jbd2_journal_init_jbd_inode(ei
->jinode
, inode
);
3825 spin_unlock(&inode
->i_lock
);
3826 if (unlikely(jinode
!= NULL
))
3827 jbd2_free_inode(jinode
);
3834 * We block out ext4_get_block() block instantiations across the entire
3835 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
3836 * simultaneously on behalf of the same inode.
3838 * As we work through the truncate and commit bits of it to the journal there
3839 * is one core, guiding principle: the file's tree must always be consistent on
3840 * disk. We must be able to restart the truncate after a crash.
3842 * The file's tree may be transiently inconsistent in memory (although it
3843 * probably isn't), but whenever we close off and commit a journal transaction,
3844 * the contents of (the filesystem + the journal) must be consistent and
3845 * restartable. It's pretty simple, really: bottom up, right to left (although
3846 * left-to-right works OK too).
3848 * Note that at recovery time, journal replay occurs *before* the restart of
3849 * truncate against the orphan inode list.
3851 * The committed inode has the new, desired i_size (which is the same as
3852 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
3853 * that this inode's truncate did not complete and it will again call
3854 * ext4_truncate() to have another go. So there will be instantiated blocks
3855 * to the right of the truncation point in a crashed ext4 filesystem. But
3856 * that's fine - as long as they are linked from the inode, the post-crash
3857 * ext4_truncate() run will find them and release them.
3859 void ext4_truncate(struct inode
*inode
)
3861 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3862 unsigned int credits
;
3864 struct address_space
*mapping
= inode
->i_mapping
;
3867 * There is a possibility that we're either freeing the inode
3868 * or it's a completely new inode. In those cases we might not
3869 * have i_mutex locked because it's not necessary.
3871 if (!(inode
->i_state
& (I_NEW
|I_FREEING
)))
3872 WARN_ON(!mutex_is_locked(&inode
->i_mutex
));
3873 trace_ext4_truncate_enter(inode
);
3875 if (!ext4_can_truncate(inode
))
3878 ext4_clear_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3880 if (inode
->i_size
== 0 && !test_opt(inode
->i_sb
, NO_AUTO_DA_ALLOC
))
3881 ext4_set_inode_state(inode
, EXT4_STATE_DA_ALLOC_CLOSE
);
3883 if (ext4_has_inline_data(inode
)) {
3886 ext4_inline_data_truncate(inode
, &has_inline
);
3891 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
3892 if (inode
->i_size
& (inode
->i_sb
->s_blocksize
- 1)) {
3893 if (ext4_inode_attach_jinode(inode
) < 0)
3897 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3898 credits
= ext4_writepage_trans_blocks(inode
);
3900 credits
= ext4_blocks_for_truncate(inode
);
3902 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
3903 if (IS_ERR(handle
)) {
3904 ext4_std_error(inode
->i_sb
, PTR_ERR(handle
));
3908 if (inode
->i_size
& (inode
->i_sb
->s_blocksize
- 1))
3909 ext4_block_truncate_page(handle
, mapping
, inode
->i_size
);
3912 * We add the inode to the orphan list, so that if this
3913 * truncate spans multiple transactions, and we crash, we will
3914 * resume the truncate when the filesystem recovers. It also
3915 * marks the inode dirty, to catch the new size.
3917 * Implication: the file must always be in a sane, consistent
3918 * truncatable state while each transaction commits.
3920 if (ext4_orphan_add(handle
, inode
))
3923 down_write(&EXT4_I(inode
)->i_data_sem
);
3925 ext4_discard_preallocations(inode
);
3927 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3928 ext4_ext_truncate(handle
, inode
);
3930 ext4_ind_truncate(handle
, inode
);
3932 up_write(&ei
->i_data_sem
);
3935 ext4_handle_sync(handle
);
3939 * If this was a simple ftruncate() and the file will remain alive,
3940 * then we need to clear up the orphan record which we created above.
3941 * However, if this was a real unlink then we were called by
3942 * ext4_evict_inode(), and we allow that function to clean up the
3943 * orphan info for us.
3946 ext4_orphan_del(handle
, inode
);
3948 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3949 ext4_mark_inode_dirty(handle
, inode
);
3950 ext4_journal_stop(handle
);
3952 trace_ext4_truncate_exit(inode
);
3956 * ext4_get_inode_loc returns with an extra refcount against the inode's
3957 * underlying buffer_head on success. If 'in_mem' is true, we have all
3958 * data in memory that is needed to recreate the on-disk version of this
3961 static int __ext4_get_inode_loc(struct inode
*inode
,
3962 struct ext4_iloc
*iloc
, int in_mem
)
3964 struct ext4_group_desc
*gdp
;
3965 struct buffer_head
*bh
;
3966 struct super_block
*sb
= inode
->i_sb
;
3968 int inodes_per_block
, inode_offset
;
3971 if (!ext4_valid_inum(sb
, inode
->i_ino
))
3972 return -EFSCORRUPTED
;
3974 iloc
->block_group
= (inode
->i_ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
3975 gdp
= ext4_get_group_desc(sb
, iloc
->block_group
, NULL
);
3980 * Figure out the offset within the block group inode table
3982 inodes_per_block
= EXT4_SB(sb
)->s_inodes_per_block
;
3983 inode_offset
= ((inode
->i_ino
- 1) %
3984 EXT4_INODES_PER_GROUP(sb
));
3985 block
= ext4_inode_table(sb
, gdp
) + (inode_offset
/ inodes_per_block
);
3986 iloc
->offset
= (inode_offset
% inodes_per_block
) * EXT4_INODE_SIZE(sb
);
3988 bh
= sb_getblk(sb
, block
);
3991 if (!buffer_uptodate(bh
)) {
3995 * If the buffer has the write error flag, we have failed
3996 * to write out another inode in the same block. In this
3997 * case, we don't have to read the block because we may
3998 * read the old inode data successfully.
4000 if (buffer_write_io_error(bh
) && !buffer_uptodate(bh
))
4001 set_buffer_uptodate(bh
);
4003 if (buffer_uptodate(bh
)) {
4004 /* someone brought it uptodate while we waited */
4010 * If we have all information of the inode in memory and this
4011 * is the only valid inode in the block, we need not read the
4015 struct buffer_head
*bitmap_bh
;
4018 start
= inode_offset
& ~(inodes_per_block
- 1);
4020 /* Is the inode bitmap in cache? */
4021 bitmap_bh
= sb_getblk(sb
, ext4_inode_bitmap(sb
, gdp
));
4022 if (unlikely(!bitmap_bh
))
4026 * If the inode bitmap isn't in cache then the
4027 * optimisation may end up performing two reads instead
4028 * of one, so skip it.
4030 if (!buffer_uptodate(bitmap_bh
)) {
4034 for (i
= start
; i
< start
+ inodes_per_block
; i
++) {
4035 if (i
== inode_offset
)
4037 if (ext4_test_bit(i
, bitmap_bh
->b_data
))
4041 if (i
== start
+ inodes_per_block
) {
4042 /* all other inodes are free, so skip I/O */
4043 memset(bh
->b_data
, 0, bh
->b_size
);
4044 set_buffer_uptodate(bh
);
4052 * If we need to do any I/O, try to pre-readahead extra
4053 * blocks from the inode table.
4055 if (EXT4_SB(sb
)->s_inode_readahead_blks
) {
4056 ext4_fsblk_t b
, end
, table
;
4058 __u32 ra_blks
= EXT4_SB(sb
)->s_inode_readahead_blks
;
4060 table
= ext4_inode_table(sb
, gdp
);
4061 /* s_inode_readahead_blks is always a power of 2 */
4062 b
= block
& ~((ext4_fsblk_t
) ra_blks
- 1);
4066 num
= EXT4_INODES_PER_GROUP(sb
);
4067 if (ext4_has_group_desc_csum(sb
))
4068 num
-= ext4_itable_unused_count(sb
, gdp
);
4069 table
+= num
/ inodes_per_block
;
4073 sb_breadahead(sb
, b
++);
4077 * There are other valid inodes in the buffer, this inode
4078 * has in-inode xattrs, or we don't have this inode in memory.
4079 * Read the block from disk.
4081 trace_ext4_load_inode(inode
);
4083 bh
->b_end_io
= end_buffer_read_sync
;
4084 submit_bh(READ
| REQ_META
| REQ_PRIO
, bh
);
4086 if (!buffer_uptodate(bh
)) {
4087 EXT4_ERROR_INODE_BLOCK(inode
, block
,
4088 "unable to read itable block");
4098 int ext4_get_inode_loc(struct inode
*inode
, struct ext4_iloc
*iloc
)
4100 /* We have all inode data except xattrs in memory here. */
4101 return __ext4_get_inode_loc(inode
, iloc
,
4102 !ext4_test_inode_state(inode
, EXT4_STATE_XATTR
));
4105 void ext4_set_inode_flags(struct inode
*inode
)
4107 unsigned int flags
= EXT4_I(inode
)->i_flags
;
4108 unsigned int new_fl
= 0;
4110 if (flags
& EXT4_SYNC_FL
)
4112 if (flags
& EXT4_APPEND_FL
)
4114 if (flags
& EXT4_IMMUTABLE_FL
)
4115 new_fl
|= S_IMMUTABLE
;
4116 if (flags
& EXT4_NOATIME_FL
)
4117 new_fl
|= S_NOATIME
;
4118 if (flags
& EXT4_DIRSYNC_FL
)
4119 new_fl
|= S_DIRSYNC
;
4120 if (test_opt(inode
->i_sb
, DAX
))
4122 inode_set_flags(inode
, new_fl
,
4123 S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
|S_DAX
);
4126 /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4127 void ext4_get_inode_flags(struct ext4_inode_info
*ei
)
4129 unsigned int vfs_fl
;
4130 unsigned long old_fl
, new_fl
;
4133 vfs_fl
= ei
->vfs_inode
.i_flags
;
4134 old_fl
= ei
->i_flags
;
4135 new_fl
= old_fl
& ~(EXT4_SYNC_FL
|EXT4_APPEND_FL
|
4136 EXT4_IMMUTABLE_FL
|EXT4_NOATIME_FL
|
4138 if (vfs_fl
& S_SYNC
)
4139 new_fl
|= EXT4_SYNC_FL
;
4140 if (vfs_fl
& S_APPEND
)
4141 new_fl
|= EXT4_APPEND_FL
;
4142 if (vfs_fl
& S_IMMUTABLE
)
4143 new_fl
|= EXT4_IMMUTABLE_FL
;
4144 if (vfs_fl
& S_NOATIME
)
4145 new_fl
|= EXT4_NOATIME_FL
;
4146 if (vfs_fl
& S_DIRSYNC
)
4147 new_fl
|= EXT4_DIRSYNC_FL
;
4148 } while (cmpxchg(&ei
->i_flags
, old_fl
, new_fl
) != old_fl
);
4151 static blkcnt_t
ext4_inode_blocks(struct ext4_inode
*raw_inode
,
4152 struct ext4_inode_info
*ei
)
4155 struct inode
*inode
= &(ei
->vfs_inode
);
4156 struct super_block
*sb
= inode
->i_sb
;
4158 if (ext4_has_feature_huge_file(sb
)) {
4159 /* we are using combined 48 bit field */
4160 i_blocks
= ((u64
)le16_to_cpu(raw_inode
->i_blocks_high
)) << 32 |
4161 le32_to_cpu(raw_inode
->i_blocks_lo
);
4162 if (ext4_test_inode_flag(inode
, EXT4_INODE_HUGE_FILE
)) {
4163 /* i_blocks represent file system block size */
4164 return i_blocks
<< (inode
->i_blkbits
- 9);
4169 return le32_to_cpu(raw_inode
->i_blocks_lo
);
4173 static inline void ext4_iget_extra_inode(struct inode
*inode
,
4174 struct ext4_inode
*raw_inode
,
4175 struct ext4_inode_info
*ei
)
4177 __le32
*magic
= (void *)raw_inode
+
4178 EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
;
4179 if (*magic
== cpu_to_le32(EXT4_XATTR_MAGIC
)) {
4180 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
4181 ext4_find_inline_data_nolock(inode
);
4183 EXT4_I(inode
)->i_inline_off
= 0;
4186 struct inode
*ext4_iget(struct super_block
*sb
, unsigned long ino
)
4188 struct ext4_iloc iloc
;
4189 struct ext4_inode
*raw_inode
;
4190 struct ext4_inode_info
*ei
;
4191 struct inode
*inode
;
4192 journal_t
*journal
= EXT4_SB(sb
)->s_journal
;
4199 inode
= iget_locked(sb
, ino
);
4201 return ERR_PTR(-ENOMEM
);
4202 if (!(inode
->i_state
& I_NEW
))
4208 ret
= __ext4_get_inode_loc(inode
, &iloc
, 0);
4211 raw_inode
= ext4_raw_inode(&iloc
);
4213 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4214 ei
->i_extra_isize
= le16_to_cpu(raw_inode
->i_extra_isize
);
4215 if (EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
>
4216 EXT4_INODE_SIZE(inode
->i_sb
)) {
4217 EXT4_ERROR_INODE(inode
, "bad extra_isize (%u != %u)",
4218 EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
,
4219 EXT4_INODE_SIZE(inode
->i_sb
));
4220 ret
= -EFSCORRUPTED
;
4224 ei
->i_extra_isize
= 0;
4226 /* Precompute checksum seed for inode metadata */
4227 if (ext4_has_metadata_csum(sb
)) {
4228 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
4230 __le32 inum
= cpu_to_le32(inode
->i_ino
);
4231 __le32 gen
= raw_inode
->i_generation
;
4232 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&inum
,
4234 ei
->i_csum_seed
= ext4_chksum(sbi
, csum
, (__u8
*)&gen
,
4238 if (!ext4_inode_csum_verify(inode
, raw_inode
, ei
)) {
4239 EXT4_ERROR_INODE(inode
, "checksum invalid");
4244 inode
->i_mode
= le16_to_cpu(raw_inode
->i_mode
);
4245 i_uid
= (uid_t
)le16_to_cpu(raw_inode
->i_uid_low
);
4246 i_gid
= (gid_t
)le16_to_cpu(raw_inode
->i_gid_low
);
4247 if (!(test_opt(inode
->i_sb
, NO_UID32
))) {
4248 i_uid
|= le16_to_cpu(raw_inode
->i_uid_high
) << 16;
4249 i_gid
|= le16_to_cpu(raw_inode
->i_gid_high
) << 16;
4251 i_uid_write(inode
, i_uid
);
4252 i_gid_write(inode
, i_gid
);
4253 set_nlink(inode
, le16_to_cpu(raw_inode
->i_links_count
));
4255 ext4_clear_state_flags(ei
); /* Only relevant on 32-bit archs */
4256 ei
->i_inline_off
= 0;
4257 ei
->i_dir_start_lookup
= 0;
4258 ei
->i_dtime
= le32_to_cpu(raw_inode
->i_dtime
);
4259 /* We now have enough fields to check if the inode was active or not.
4260 * This is needed because nfsd might try to access dead inodes
4261 * the test is that same one that e2fsck uses
4262 * NeilBrown 1999oct15
4264 if (inode
->i_nlink
== 0) {
4265 if ((inode
->i_mode
== 0 ||
4266 !(EXT4_SB(inode
->i_sb
)->s_mount_state
& EXT4_ORPHAN_FS
)) &&
4267 ino
!= EXT4_BOOT_LOADER_INO
) {
4268 /* this inode is deleted */
4272 /* The only unlinked inodes we let through here have
4273 * valid i_mode and are being read by the orphan
4274 * recovery code: that's fine, we're about to complete
4275 * the process of deleting those.
4276 * OR it is the EXT4_BOOT_LOADER_INO which is
4277 * not initialized on a new filesystem. */
4279 ei
->i_flags
= le32_to_cpu(raw_inode
->i_flags
);
4280 inode
->i_blocks
= ext4_inode_blocks(raw_inode
, ei
);
4281 ei
->i_file_acl
= le32_to_cpu(raw_inode
->i_file_acl_lo
);
4282 if (ext4_has_feature_64bit(sb
))
4284 ((__u64
)le16_to_cpu(raw_inode
->i_file_acl_high
)) << 32;
4285 inode
->i_size
= ext4_isize(raw_inode
);
4286 if ((size
= i_size_read(inode
)) < 0) {
4287 EXT4_ERROR_INODE(inode
, "bad i_size value: %lld", size
);
4288 ret
= -EFSCORRUPTED
;
4291 ei
->i_disksize
= inode
->i_size
;
4293 ei
->i_reserved_quota
= 0;
4295 inode
->i_generation
= le32_to_cpu(raw_inode
->i_generation
);
4296 ei
->i_block_group
= iloc
.block_group
;
4297 ei
->i_last_alloc_group
= ~0;
4299 * NOTE! The in-memory inode i_data array is in little-endian order
4300 * even on big-endian machines: we do NOT byteswap the block numbers!
4302 for (block
= 0; block
< EXT4_N_BLOCKS
; block
++)
4303 ei
->i_data
[block
] = raw_inode
->i_block
[block
];
4304 INIT_LIST_HEAD(&ei
->i_orphan
);
4307 * Set transaction id's of transactions that have to be committed
4308 * to finish f[data]sync. We set them to currently running transaction
4309 * as we cannot be sure that the inode or some of its metadata isn't
4310 * part of the transaction - the inode could have been reclaimed and
4311 * now it is reread from disk.
4314 transaction_t
*transaction
;
4317 read_lock(&journal
->j_state_lock
);
4318 if (journal
->j_running_transaction
)
4319 transaction
= journal
->j_running_transaction
;
4321 transaction
= journal
->j_committing_transaction
;
4323 tid
= transaction
->t_tid
;
4325 tid
= journal
->j_commit_sequence
;
4326 read_unlock(&journal
->j_state_lock
);
4327 ei
->i_sync_tid
= tid
;
4328 ei
->i_datasync_tid
= tid
;
4331 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4332 if (ei
->i_extra_isize
== 0) {
4333 /* The extra space is currently unused. Use it. */
4334 ei
->i_extra_isize
= sizeof(struct ext4_inode
) -
4335 EXT4_GOOD_OLD_INODE_SIZE
;
4337 ext4_iget_extra_inode(inode
, raw_inode
, ei
);
4341 EXT4_INODE_GET_XTIME(i_ctime
, inode
, raw_inode
);
4342 EXT4_INODE_GET_XTIME(i_mtime
, inode
, raw_inode
);
4343 EXT4_INODE_GET_XTIME(i_atime
, inode
, raw_inode
);
4344 EXT4_EINODE_GET_XTIME(i_crtime
, ei
, raw_inode
);
4346 if (likely(!test_opt2(inode
->i_sb
, HURD_COMPAT
))) {
4347 inode
->i_version
= le32_to_cpu(raw_inode
->i_disk_version
);
4348 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4349 if (EXT4_FITS_IN_INODE(raw_inode
, ei
, i_version_hi
))
4351 (__u64
)(le32_to_cpu(raw_inode
->i_version_hi
)) << 32;
4356 if (ei
->i_file_acl
&&
4357 !ext4_data_block_valid(EXT4_SB(sb
), ei
->i_file_acl
, 1)) {
4358 EXT4_ERROR_INODE(inode
, "bad extended attribute block %llu",
4360 ret
= -EFSCORRUPTED
;
4362 } else if (!ext4_has_inline_data(inode
)) {
4363 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
4364 if ((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
4365 (S_ISLNK(inode
->i_mode
) &&
4366 !ext4_inode_is_fast_symlink(inode
))))
4367 /* Validate extent which is part of inode */
4368 ret
= ext4_ext_check_inode(inode
);
4369 } else if (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
4370 (S_ISLNK(inode
->i_mode
) &&
4371 !ext4_inode_is_fast_symlink(inode
))) {
4372 /* Validate block references which are part of inode */
4373 ret
= ext4_ind_check_inode(inode
);
4379 if (S_ISREG(inode
->i_mode
)) {
4380 inode
->i_op
= &ext4_file_inode_operations
;
4381 inode
->i_fop
= &ext4_file_operations
;
4382 ext4_set_aops(inode
);
4383 } else if (S_ISDIR(inode
->i_mode
)) {
4384 inode
->i_op
= &ext4_dir_inode_operations
;
4385 inode
->i_fop
= &ext4_dir_operations
;
4386 } else if (S_ISLNK(inode
->i_mode
)) {
4387 if (ext4_encrypted_inode(inode
)) {
4388 inode
->i_op
= &ext4_encrypted_symlink_inode_operations
;
4389 ext4_set_aops(inode
);
4390 } else if (ext4_inode_is_fast_symlink(inode
)) {
4391 inode
->i_link
= (char *)ei
->i_data
;
4392 inode
->i_op
= &ext4_fast_symlink_inode_operations
;
4393 nd_terminate_link(ei
->i_data
, inode
->i_size
,
4394 sizeof(ei
->i_data
) - 1);
4396 inode
->i_op
= &ext4_symlink_inode_operations
;
4397 ext4_set_aops(inode
);
4399 } else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
) ||
4400 S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
4401 inode
->i_op
= &ext4_special_inode_operations
;
4402 if (raw_inode
->i_block
[0])
4403 init_special_inode(inode
, inode
->i_mode
,
4404 old_decode_dev(le32_to_cpu(raw_inode
->i_block
[0])));
4406 init_special_inode(inode
, inode
->i_mode
,
4407 new_decode_dev(le32_to_cpu(raw_inode
->i_block
[1])));
4408 } else if (ino
== EXT4_BOOT_LOADER_INO
) {
4409 make_bad_inode(inode
);
4411 ret
= -EFSCORRUPTED
;
4412 EXT4_ERROR_INODE(inode
, "bogus i_mode (%o)", inode
->i_mode
);
4416 ext4_set_inode_flags(inode
);
4417 unlock_new_inode(inode
);
4423 return ERR_PTR(ret
);
4426 struct inode
*ext4_iget_normal(struct super_block
*sb
, unsigned long ino
)
4428 if (ino
< EXT4_FIRST_INO(sb
) && ino
!= EXT4_ROOT_INO
)
4429 return ERR_PTR(-EFSCORRUPTED
);
4430 return ext4_iget(sb
, ino
);
4433 static int ext4_inode_blocks_set(handle_t
*handle
,
4434 struct ext4_inode
*raw_inode
,
4435 struct ext4_inode_info
*ei
)
4437 struct inode
*inode
= &(ei
->vfs_inode
);
4438 u64 i_blocks
= inode
->i_blocks
;
4439 struct super_block
*sb
= inode
->i_sb
;
4441 if (i_blocks
<= ~0U) {
4443 * i_blocks can be represented in a 32 bit variable
4444 * as multiple of 512 bytes
4446 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4447 raw_inode
->i_blocks_high
= 0;
4448 ext4_clear_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4451 if (!ext4_has_feature_huge_file(sb
))
4454 if (i_blocks
<= 0xffffffffffffULL
) {
4456 * i_blocks can be represented in a 48 bit variable
4457 * as multiple of 512 bytes
4459 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4460 raw_inode
->i_blocks_high
= cpu_to_le16(i_blocks
>> 32);
4461 ext4_clear_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4463 ext4_set_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4464 /* i_block is stored in file system block size */
4465 i_blocks
= i_blocks
>> (inode
->i_blkbits
- 9);
4466 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4467 raw_inode
->i_blocks_high
= cpu_to_le16(i_blocks
>> 32);
4472 struct other_inode
{
4473 unsigned long orig_ino
;
4474 struct ext4_inode
*raw_inode
;
4477 static int other_inode_match(struct inode
* inode
, unsigned long ino
,
4480 struct other_inode
*oi
= (struct other_inode
*) data
;
4482 if ((inode
->i_ino
!= ino
) ||
4483 (inode
->i_state
& (I_FREEING
| I_WILL_FREE
| I_NEW
|
4484 I_DIRTY_SYNC
| I_DIRTY_DATASYNC
)) ||
4485 ((inode
->i_state
& I_DIRTY_TIME
) == 0))
4487 spin_lock(&inode
->i_lock
);
4488 if (((inode
->i_state
& (I_FREEING
| I_WILL_FREE
| I_NEW
|
4489 I_DIRTY_SYNC
| I_DIRTY_DATASYNC
)) == 0) &&
4490 (inode
->i_state
& I_DIRTY_TIME
)) {
4491 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4493 inode
->i_state
&= ~(I_DIRTY_TIME
| I_DIRTY_TIME_EXPIRED
);
4494 spin_unlock(&inode
->i_lock
);
4496 spin_lock(&ei
->i_raw_lock
);
4497 EXT4_INODE_SET_XTIME(i_ctime
, inode
, oi
->raw_inode
);
4498 EXT4_INODE_SET_XTIME(i_mtime
, inode
, oi
->raw_inode
);
4499 EXT4_INODE_SET_XTIME(i_atime
, inode
, oi
->raw_inode
);
4500 ext4_inode_csum_set(inode
, oi
->raw_inode
, ei
);
4501 spin_unlock(&ei
->i_raw_lock
);
4502 trace_ext4_other_inode_update_time(inode
, oi
->orig_ino
);
4505 spin_unlock(&inode
->i_lock
);
4510 * Opportunistically update the other time fields for other inodes in
4511 * the same inode table block.
4513 static void ext4_update_other_inodes_time(struct super_block
*sb
,
4514 unsigned long orig_ino
, char *buf
)
4516 struct other_inode oi
;
4518 int i
, inodes_per_block
= EXT4_SB(sb
)->s_inodes_per_block
;
4519 int inode_size
= EXT4_INODE_SIZE(sb
);
4521 oi
.orig_ino
= orig_ino
;
4523 * Calculate the first inode in the inode table block. Inode
4524 * numbers are one-based. That is, the first inode in a block
4525 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
4527 ino
= ((orig_ino
- 1) & ~(inodes_per_block
- 1)) + 1;
4528 for (i
= 0; i
< inodes_per_block
; i
++, ino
++, buf
+= inode_size
) {
4529 if (ino
== orig_ino
)
4531 oi
.raw_inode
= (struct ext4_inode
*) buf
;
4532 (void) find_inode_nowait(sb
, ino
, other_inode_match
, &oi
);
4537 * Post the struct inode info into an on-disk inode location in the
4538 * buffer-cache. This gobbles the caller's reference to the
4539 * buffer_head in the inode location struct.
4541 * The caller must have write access to iloc->bh.
4543 static int ext4_do_update_inode(handle_t
*handle
,
4544 struct inode
*inode
,
4545 struct ext4_iloc
*iloc
)
4547 struct ext4_inode
*raw_inode
= ext4_raw_inode(iloc
);
4548 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4549 struct buffer_head
*bh
= iloc
->bh
;
4550 struct super_block
*sb
= inode
->i_sb
;
4551 int err
= 0, rc
, block
;
4552 int need_datasync
= 0, set_large_file
= 0;
4556 spin_lock(&ei
->i_raw_lock
);
4558 /* For fields not tracked in the in-memory inode,
4559 * initialise them to zero for new inodes. */
4560 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
))
4561 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
4563 ext4_get_inode_flags(ei
);
4564 raw_inode
->i_mode
= cpu_to_le16(inode
->i_mode
);
4565 i_uid
= i_uid_read(inode
);
4566 i_gid
= i_gid_read(inode
);
4567 if (!(test_opt(inode
->i_sb
, NO_UID32
))) {
4568 raw_inode
->i_uid_low
= cpu_to_le16(low_16_bits(i_uid
));
4569 raw_inode
->i_gid_low
= cpu_to_le16(low_16_bits(i_gid
));
4571 * Fix up interoperability with old kernels. Otherwise, old inodes get
4572 * re-used with the upper 16 bits of the uid/gid intact
4574 if (ei
->i_dtime
&& list_empty(&ei
->i_orphan
)) {
4575 raw_inode
->i_uid_high
= 0;
4576 raw_inode
->i_gid_high
= 0;
4578 raw_inode
->i_uid_high
=
4579 cpu_to_le16(high_16_bits(i_uid
));
4580 raw_inode
->i_gid_high
=
4581 cpu_to_le16(high_16_bits(i_gid
));
4584 raw_inode
->i_uid_low
= cpu_to_le16(fs_high2lowuid(i_uid
));
4585 raw_inode
->i_gid_low
= cpu_to_le16(fs_high2lowgid(i_gid
));
4586 raw_inode
->i_uid_high
= 0;
4587 raw_inode
->i_gid_high
= 0;
4589 raw_inode
->i_links_count
= cpu_to_le16(inode
->i_nlink
);
4591 EXT4_INODE_SET_XTIME(i_ctime
, inode
, raw_inode
);
4592 EXT4_INODE_SET_XTIME(i_mtime
, inode
, raw_inode
);
4593 EXT4_INODE_SET_XTIME(i_atime
, inode
, raw_inode
);
4594 EXT4_EINODE_SET_XTIME(i_crtime
, ei
, raw_inode
);
4596 err
= ext4_inode_blocks_set(handle
, raw_inode
, ei
);
4598 spin_unlock(&ei
->i_raw_lock
);
4601 raw_inode
->i_dtime
= cpu_to_le32(ei
->i_dtime
);
4602 raw_inode
->i_flags
= cpu_to_le32(ei
->i_flags
& 0xFFFFFFFF);
4603 if (likely(!test_opt2(inode
->i_sb
, HURD_COMPAT
)))
4604 raw_inode
->i_file_acl_high
=
4605 cpu_to_le16(ei
->i_file_acl
>> 32);
4606 raw_inode
->i_file_acl_lo
= cpu_to_le32(ei
->i_file_acl
);
4607 if (ei
->i_disksize
!= ext4_isize(raw_inode
)) {
4608 ext4_isize_set(raw_inode
, ei
->i_disksize
);
4611 if (ei
->i_disksize
> 0x7fffffffULL
) {
4612 if (!ext4_has_feature_large_file(sb
) ||
4613 EXT4_SB(sb
)->s_es
->s_rev_level
==
4614 cpu_to_le32(EXT4_GOOD_OLD_REV
))
4617 raw_inode
->i_generation
= cpu_to_le32(inode
->i_generation
);
4618 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) {
4619 if (old_valid_dev(inode
->i_rdev
)) {
4620 raw_inode
->i_block
[0] =
4621 cpu_to_le32(old_encode_dev(inode
->i_rdev
));
4622 raw_inode
->i_block
[1] = 0;
4624 raw_inode
->i_block
[0] = 0;
4625 raw_inode
->i_block
[1] =
4626 cpu_to_le32(new_encode_dev(inode
->i_rdev
));
4627 raw_inode
->i_block
[2] = 0;
4629 } else if (!ext4_has_inline_data(inode
)) {
4630 for (block
= 0; block
< EXT4_N_BLOCKS
; block
++)
4631 raw_inode
->i_block
[block
] = ei
->i_data
[block
];
4634 if (likely(!test_opt2(inode
->i_sb
, HURD_COMPAT
))) {
4635 raw_inode
->i_disk_version
= cpu_to_le32(inode
->i_version
);
4636 if (ei
->i_extra_isize
) {
4637 if (EXT4_FITS_IN_INODE(raw_inode
, ei
, i_version_hi
))
4638 raw_inode
->i_version_hi
=
4639 cpu_to_le32(inode
->i_version
>> 32);
4640 raw_inode
->i_extra_isize
=
4641 cpu_to_le16(ei
->i_extra_isize
);
4644 ext4_inode_csum_set(inode
, raw_inode
, ei
);
4645 spin_unlock(&ei
->i_raw_lock
);
4646 if (inode
->i_sb
->s_flags
& MS_LAZYTIME
)
4647 ext4_update_other_inodes_time(inode
->i_sb
, inode
->i_ino
,
4650 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
4651 rc
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
4654 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
4655 if (set_large_file
) {
4656 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get write access");
4657 err
= ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
);
4660 ext4_update_dynamic_rev(sb
);
4661 ext4_set_feature_large_file(sb
);
4662 ext4_handle_sync(handle
);
4663 err
= ext4_handle_dirty_super(handle
, sb
);
4665 ext4_update_inode_fsync_trans(handle
, inode
, need_datasync
);
4668 ext4_std_error(inode
->i_sb
, err
);
4673 * ext4_write_inode()
4675 * We are called from a few places:
4677 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
4678 * Here, there will be no transaction running. We wait for any running
4679 * transaction to commit.
4681 * - Within flush work (sys_sync(), kupdate and such).
4682 * We wait on commit, if told to.
4684 * - Within iput_final() -> write_inode_now()
4685 * We wait on commit, if told to.
4687 * In all cases it is actually safe for us to return without doing anything,
4688 * because the inode has been copied into a raw inode buffer in
4689 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
4692 * Note that we are absolutely dependent upon all inode dirtiers doing the
4693 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4694 * which we are interested.
4696 * It would be a bug for them to not do this. The code:
4698 * mark_inode_dirty(inode)
4700 * inode->i_size = expr;
4702 * is in error because write_inode() could occur while `stuff()' is running,
4703 * and the new i_size will be lost. Plus the inode will no longer be on the
4704 * superblock's dirty inode list.
4706 int ext4_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4710 if (WARN_ON_ONCE(current
->flags
& PF_MEMALLOC
))
4713 if (EXT4_SB(inode
->i_sb
)->s_journal
) {
4714 if (ext4_journal_current_handle()) {
4715 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4721 * No need to force transaction in WB_SYNC_NONE mode. Also
4722 * ext4_sync_fs() will force the commit after everything is
4725 if (wbc
->sync_mode
!= WB_SYNC_ALL
|| wbc
->for_sync
)
4728 err
= ext4_force_commit(inode
->i_sb
);
4730 struct ext4_iloc iloc
;
4732 err
= __ext4_get_inode_loc(inode
, &iloc
, 0);
4736 * sync(2) will flush the whole buffer cache. No need to do
4737 * it here separately for each inode.
4739 if (wbc
->sync_mode
== WB_SYNC_ALL
&& !wbc
->for_sync
)
4740 sync_dirty_buffer(iloc
.bh
);
4741 if (buffer_req(iloc
.bh
) && !buffer_uptodate(iloc
.bh
)) {
4742 EXT4_ERROR_INODE_BLOCK(inode
, iloc
.bh
->b_blocknr
,
4743 "IO error syncing inode");
4752 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
4753 * buffers that are attached to a page stradding i_size and are undergoing
4754 * commit. In that case we have to wait for commit to finish and try again.
4756 static void ext4_wait_for_tail_page_commit(struct inode
*inode
)
4760 journal_t
*journal
= EXT4_SB(inode
->i_sb
)->s_journal
;
4761 tid_t commit_tid
= 0;
4764 offset
= inode
->i_size
& (PAGE_CACHE_SIZE
- 1);
4766 * All buffers in the last page remain valid? Then there's nothing to
4767 * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
4770 if (offset
> PAGE_CACHE_SIZE
- (1 << inode
->i_blkbits
))
4773 page
= find_lock_page(inode
->i_mapping
,
4774 inode
->i_size
>> PAGE_CACHE_SHIFT
);
4777 ret
= __ext4_journalled_invalidatepage(page
, offset
,
4778 PAGE_CACHE_SIZE
- offset
);
4780 page_cache_release(page
);
4784 read_lock(&journal
->j_state_lock
);
4785 if (journal
->j_committing_transaction
)
4786 commit_tid
= journal
->j_committing_transaction
->t_tid
;
4787 read_unlock(&journal
->j_state_lock
);
4789 jbd2_log_wait_commit(journal
, commit_tid
);
4796 * Called from notify_change.
4798 * We want to trap VFS attempts to truncate the file as soon as
4799 * possible. In particular, we want to make sure that when the VFS
4800 * shrinks i_size, we put the inode on the orphan list and modify
4801 * i_disksize immediately, so that during the subsequent flushing of
4802 * dirty pages and freeing of disk blocks, we can guarantee that any
4803 * commit will leave the blocks being flushed in an unused state on
4804 * disk. (On recovery, the inode will get truncated and the blocks will
4805 * be freed, so we have a strong guarantee that no future commit will
4806 * leave these blocks visible to the user.)
4808 * Another thing we have to assure is that if we are in ordered mode
4809 * and inode is still attached to the committing transaction, we must
4810 * we start writeout of all the dirty pages which are being truncated.
4811 * This way we are sure that all the data written in the previous
4812 * transaction are already on disk (truncate waits for pages under
4815 * Called with inode->i_mutex down.
4817 int ext4_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4819 struct inode
*inode
= d_inode(dentry
);
4822 const unsigned int ia_valid
= attr
->ia_valid
;
4824 error
= inode_change_ok(inode
, attr
);
4828 if (is_quota_modification(inode
, attr
)) {
4829 error
= dquot_initialize(inode
);
4833 if ((ia_valid
& ATTR_UID
&& !uid_eq(attr
->ia_uid
, inode
->i_uid
)) ||
4834 (ia_valid
& ATTR_GID
&& !gid_eq(attr
->ia_gid
, inode
->i_gid
))) {
4837 /* (user+group)*(old+new) structure, inode write (sb,
4838 * inode block, ? - but truncate inode update has it) */
4839 handle
= ext4_journal_start(inode
, EXT4_HT_QUOTA
,
4840 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode
->i_sb
) +
4841 EXT4_MAXQUOTAS_DEL_BLOCKS(inode
->i_sb
)) + 3);
4842 if (IS_ERR(handle
)) {
4843 error
= PTR_ERR(handle
);
4846 error
= dquot_transfer(inode
, attr
);
4848 ext4_journal_stop(handle
);
4851 /* Update corresponding info in inode so that everything is in
4852 * one transaction */
4853 if (attr
->ia_valid
& ATTR_UID
)
4854 inode
->i_uid
= attr
->ia_uid
;
4855 if (attr
->ia_valid
& ATTR_GID
)
4856 inode
->i_gid
= attr
->ia_gid
;
4857 error
= ext4_mark_inode_dirty(handle
, inode
);
4858 ext4_journal_stop(handle
);
4861 if (attr
->ia_valid
& ATTR_SIZE
) {
4863 loff_t oldsize
= inode
->i_size
;
4864 int shrink
= (attr
->ia_size
<= inode
->i_size
);
4866 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))) {
4867 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
4869 if (attr
->ia_size
> sbi
->s_bitmap_maxbytes
)
4872 if (!S_ISREG(inode
->i_mode
))
4875 if (IS_I_VERSION(inode
) && attr
->ia_size
!= inode
->i_size
)
4876 inode_inc_iversion(inode
);
4878 if (ext4_should_order_data(inode
) &&
4879 (attr
->ia_size
< inode
->i_size
)) {
4880 error
= ext4_begin_ordered_truncate(inode
,
4885 if (attr
->ia_size
!= inode
->i_size
) {
4886 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 3);
4887 if (IS_ERR(handle
)) {
4888 error
= PTR_ERR(handle
);
4891 if (ext4_handle_valid(handle
) && shrink
) {
4892 error
= ext4_orphan_add(handle
, inode
);
4896 * Update c/mtime on truncate up, ext4_truncate() will
4897 * update c/mtime in shrink case below
4900 inode
->i_mtime
= ext4_current_time(inode
);
4901 inode
->i_ctime
= inode
->i_mtime
;
4903 down_write(&EXT4_I(inode
)->i_data_sem
);
4904 EXT4_I(inode
)->i_disksize
= attr
->ia_size
;
4905 rc
= ext4_mark_inode_dirty(handle
, inode
);
4909 * We have to update i_size under i_data_sem together
4910 * with i_disksize to avoid races with writeback code
4911 * running ext4_wb_update_i_disksize().
4914 i_size_write(inode
, attr
->ia_size
);
4915 up_write(&EXT4_I(inode
)->i_data_sem
);
4916 ext4_journal_stop(handle
);
4919 ext4_orphan_del(NULL
, inode
);
4924 pagecache_isize_extended(inode
, oldsize
, inode
->i_size
);
4927 * Blocks are going to be removed from the inode. Wait
4928 * for dio in flight. Temporarily disable
4929 * dioread_nolock to prevent livelock.
4932 if (!ext4_should_journal_data(inode
)) {
4933 ext4_inode_block_unlocked_dio(inode
);
4934 inode_dio_wait(inode
);
4935 ext4_inode_resume_unlocked_dio(inode
);
4937 ext4_wait_for_tail_page_commit(inode
);
4939 down_write(&EXT4_I(inode
)->i_mmap_sem
);
4941 * Truncate pagecache after we've waited for commit
4942 * in data=journal mode to make pages freeable.
4944 truncate_pagecache(inode
, inode
->i_size
);
4946 ext4_truncate(inode
);
4947 up_write(&EXT4_I(inode
)->i_mmap_sem
);
4951 setattr_copy(inode
, attr
);
4952 mark_inode_dirty(inode
);
4956 * If the call to ext4_truncate failed to get a transaction handle at
4957 * all, we need to clean up the in-core orphan list manually.
4959 if (orphan
&& inode
->i_nlink
)
4960 ext4_orphan_del(NULL
, inode
);
4962 if (!rc
&& (ia_valid
& ATTR_MODE
))
4963 rc
= posix_acl_chmod(inode
, inode
->i_mode
);
4966 ext4_std_error(inode
->i_sb
, error
);
4972 int ext4_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
4975 struct inode
*inode
;
4976 unsigned long long delalloc_blocks
;
4978 inode
= d_inode(dentry
);
4979 generic_fillattr(inode
, stat
);
4982 * If there is inline data in the inode, the inode will normally not
4983 * have data blocks allocated (it may have an external xattr block).
4984 * Report at least one sector for such files, so tools like tar, rsync,
4985 * others doen't incorrectly think the file is completely sparse.
4987 if (unlikely(ext4_has_inline_data(inode
)))
4988 stat
->blocks
+= (stat
->size
+ 511) >> 9;
4991 * We can't update i_blocks if the block allocation is delayed
4992 * otherwise in the case of system crash before the real block
4993 * allocation is done, we will have i_blocks inconsistent with
4994 * on-disk file blocks.
4995 * We always keep i_blocks updated together with real
4996 * allocation. But to not confuse with user, stat
4997 * will return the blocks that include the delayed allocation
4998 * blocks for this file.
5000 delalloc_blocks
= EXT4_C2B(EXT4_SB(inode
->i_sb
),
5001 EXT4_I(inode
)->i_reserved_data_blocks
);
5002 stat
->blocks
+= delalloc_blocks
<< (inode
->i_sb
->s_blocksize_bits
- 9);
5006 static int ext4_index_trans_blocks(struct inode
*inode
, int lblocks
,
5009 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
5010 return ext4_ind_trans_blocks(inode
, lblocks
);
5011 return ext4_ext_index_trans_blocks(inode
, pextents
);
5015 * Account for index blocks, block groups bitmaps and block group
5016 * descriptor blocks if modify datablocks and index blocks
5017 * worse case, the indexs blocks spread over different block groups
5019 * If datablocks are discontiguous, they are possible to spread over
5020 * different block groups too. If they are contiguous, with flexbg,
5021 * they could still across block group boundary.
5023 * Also account for superblock, inode, quota and xattr blocks
5025 static int ext4_meta_trans_blocks(struct inode
*inode
, int lblocks
,
5028 ext4_group_t groups
, ngroups
= ext4_get_groups_count(inode
->i_sb
);
5034 * How many index blocks need to touch to map @lblocks logical blocks
5035 * to @pextents physical extents?
5037 idxblocks
= ext4_index_trans_blocks(inode
, lblocks
, pextents
);
5042 * Now let's see how many group bitmaps and group descriptors need
5045 groups
= idxblocks
+ pextents
;
5047 if (groups
> ngroups
)
5049 if (groups
> EXT4_SB(inode
->i_sb
)->s_gdb_count
)
5050 gdpblocks
= EXT4_SB(inode
->i_sb
)->s_gdb_count
;
5052 /* bitmaps and block group descriptor blocks */
5053 ret
+= groups
+ gdpblocks
;
5055 /* Blocks for super block, inode, quota and xattr blocks */
5056 ret
+= EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
5062 * Calculate the total number of credits to reserve to fit
5063 * the modification of a single pages into a single transaction,
5064 * which may include multiple chunks of block allocations.
5066 * This could be called via ext4_write_begin()
5068 * We need to consider the worse case, when
5069 * one new block per extent.
5071 int ext4_writepage_trans_blocks(struct inode
*inode
)
5073 int bpp
= ext4_journal_blocks_per_page(inode
);
5076 ret
= ext4_meta_trans_blocks(inode
, bpp
, bpp
);
5078 /* Account for data blocks for journalled mode */
5079 if (ext4_should_journal_data(inode
))
5085 * Calculate the journal credits for a chunk of data modification.
5087 * This is called from DIO, fallocate or whoever calling
5088 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
5090 * journal buffers for data blocks are not included here, as DIO
5091 * and fallocate do no need to journal data buffers.
5093 int ext4_chunk_trans_blocks(struct inode
*inode
, int nrblocks
)
5095 return ext4_meta_trans_blocks(inode
, nrblocks
, 1);
5099 * The caller must have previously called ext4_reserve_inode_write().
5100 * Give this, we know that the caller already has write access to iloc->bh.
5102 int ext4_mark_iloc_dirty(handle_t
*handle
,
5103 struct inode
*inode
, struct ext4_iloc
*iloc
)
5107 if (IS_I_VERSION(inode
))
5108 inode_inc_iversion(inode
);
5110 /* the do_update_inode consumes one bh->b_count */
5113 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
5114 err
= ext4_do_update_inode(handle
, inode
, iloc
);
5120 * On success, We end up with an outstanding reference count against
5121 * iloc->bh. This _must_ be cleaned up later.
5125 ext4_reserve_inode_write(handle_t
*handle
, struct inode
*inode
,
5126 struct ext4_iloc
*iloc
)
5130 err
= ext4_get_inode_loc(inode
, iloc
);
5132 BUFFER_TRACE(iloc
->bh
, "get_write_access");
5133 err
= ext4_journal_get_write_access(handle
, iloc
->bh
);
5139 ext4_std_error(inode
->i_sb
, err
);
5144 * Expand an inode by new_extra_isize bytes.
5145 * Returns 0 on success or negative error number on failure.
5147 static int ext4_expand_extra_isize(struct inode
*inode
,
5148 unsigned int new_extra_isize
,
5149 struct ext4_iloc iloc
,
5152 struct ext4_inode
*raw_inode
;
5153 struct ext4_xattr_ibody_header
*header
;
5155 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
)
5158 raw_inode
= ext4_raw_inode(&iloc
);
5160 header
= IHDR(inode
, raw_inode
);
5162 /* No extended attributes present */
5163 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
) ||
5164 header
->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
)) {
5165 memset((void *)raw_inode
+ EXT4_GOOD_OLD_INODE_SIZE
, 0,
5167 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
5171 /* try to expand with EAs present */
5172 return ext4_expand_extra_isize_ea(inode
, new_extra_isize
,
5177 * What we do here is to mark the in-core inode as clean with respect to inode
5178 * dirtiness (it may still be data-dirty).
5179 * This means that the in-core inode may be reaped by prune_icache
5180 * without having to perform any I/O. This is a very good thing,
5181 * because *any* task may call prune_icache - even ones which
5182 * have a transaction open against a different journal.
5184 * Is this cheating? Not really. Sure, we haven't written the
5185 * inode out, but prune_icache isn't a user-visible syncing function.
5186 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5187 * we start and wait on commits.
5189 int ext4_mark_inode_dirty(handle_t
*handle
, struct inode
*inode
)
5191 struct ext4_iloc iloc
;
5192 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
5193 static unsigned int mnt_count
;
5197 trace_ext4_mark_inode_dirty(inode
, _RET_IP_
);
5198 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
5201 if (ext4_handle_valid(handle
) &&
5202 EXT4_I(inode
)->i_extra_isize
< sbi
->s_want_extra_isize
&&
5203 !ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
)) {
5205 * We need extra buffer credits since we may write into EA block
5206 * with this same handle. If journal_extend fails, then it will
5207 * only result in a minor loss of functionality for that inode.
5208 * If this is felt to be critical, then e2fsck should be run to
5209 * force a large enough s_min_extra_isize.
5211 if ((jbd2_journal_extend(handle
,
5212 EXT4_DATA_TRANS_BLOCKS(inode
->i_sb
))) == 0) {
5213 ret
= ext4_expand_extra_isize(inode
,
5214 sbi
->s_want_extra_isize
,
5218 le16_to_cpu(sbi
->s_es
->s_mnt_count
)) {
5219 ext4_warning(inode
->i_sb
,
5220 "Unable to expand inode %lu. Delete"
5221 " some EAs or run e2fsck.",
5224 le16_to_cpu(sbi
->s_es
->s_mnt_count
);
5229 return ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
5233 * ext4_dirty_inode() is called from __mark_inode_dirty()
5235 * We're really interested in the case where a file is being extended.
5236 * i_size has been changed by generic_commit_write() and we thus need
5237 * to include the updated inode in the current transaction.
5239 * Also, dquot_alloc_block() will always dirty the inode when blocks
5240 * are allocated to the file.
5242 * If the inode is marked synchronous, we don't honour that here - doing
5243 * so would cause a commit on atime updates, which we don't bother doing.
5244 * We handle synchronous inodes at the highest possible level.
5246 * If only the I_DIRTY_TIME flag is set, we can skip everything. If
5247 * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
5248 * to copy into the on-disk inode structure are the timestamp files.
5250 void ext4_dirty_inode(struct inode
*inode
, int flags
)
5254 if (flags
== I_DIRTY_TIME
)
5256 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 2);
5260 ext4_mark_inode_dirty(handle
, inode
);
5262 ext4_journal_stop(handle
);
5269 * Bind an inode's backing buffer_head into this transaction, to prevent
5270 * it from being flushed to disk early. Unlike
5271 * ext4_reserve_inode_write, this leaves behind no bh reference and
5272 * returns no iloc structure, so the caller needs to repeat the iloc
5273 * lookup to mark the inode dirty later.
5275 static int ext4_pin_inode(handle_t
*handle
, struct inode
*inode
)
5277 struct ext4_iloc iloc
;
5281 err
= ext4_get_inode_loc(inode
, &iloc
);
5283 BUFFER_TRACE(iloc
.bh
, "get_write_access");
5284 err
= jbd2_journal_get_write_access(handle
, iloc
.bh
);
5286 err
= ext4_handle_dirty_metadata(handle
,
5292 ext4_std_error(inode
->i_sb
, err
);
5297 int ext4_change_inode_journal_flag(struct inode
*inode
, int val
)
5304 * We have to be very careful here: changing a data block's
5305 * journaling status dynamically is dangerous. If we write a
5306 * data block to the journal, change the status and then delete
5307 * that block, we risk forgetting to revoke the old log record
5308 * from the journal and so a subsequent replay can corrupt data.
5309 * So, first we make sure that the journal is empty and that
5310 * nobody is changing anything.
5313 journal
= EXT4_JOURNAL(inode
);
5316 if (is_journal_aborted(journal
))
5318 /* We have to allocate physical blocks for delalloc blocks
5319 * before flushing journal. otherwise delalloc blocks can not
5320 * be allocated any more. even more truncate on delalloc blocks
5321 * could trigger BUG by flushing delalloc blocks in journal.
5322 * There is no delalloc block in non-journal data mode.
5324 if (val
&& test_opt(inode
->i_sb
, DELALLOC
)) {
5325 err
= ext4_alloc_da_blocks(inode
);
5330 /* Wait for all existing dio workers */
5331 ext4_inode_block_unlocked_dio(inode
);
5332 inode_dio_wait(inode
);
5334 jbd2_journal_lock_updates(journal
);
5337 * OK, there are no updates running now, and all cached data is
5338 * synced to disk. We are now in a completely consistent state
5339 * which doesn't have anything in the journal, and we know that
5340 * no filesystem updates are running, so it is safe to modify
5341 * the inode's in-core data-journaling state flag now.
5345 ext4_set_inode_flag(inode
, EXT4_INODE_JOURNAL_DATA
);
5347 err
= jbd2_journal_flush(journal
);
5349 jbd2_journal_unlock_updates(journal
);
5350 ext4_inode_resume_unlocked_dio(inode
);
5353 ext4_clear_inode_flag(inode
, EXT4_INODE_JOURNAL_DATA
);
5355 ext4_set_aops(inode
);
5357 jbd2_journal_unlock_updates(journal
);
5358 ext4_inode_resume_unlocked_dio(inode
);
5360 /* Finally we can mark the inode as dirty. */
5362 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
5364 return PTR_ERR(handle
);
5366 err
= ext4_mark_inode_dirty(handle
, inode
);
5367 ext4_handle_sync(handle
);
5368 ext4_journal_stop(handle
);
5369 ext4_std_error(inode
->i_sb
, err
);
5374 static int ext4_bh_unmapped(handle_t
*handle
, struct buffer_head
*bh
)
5376 return !buffer_mapped(bh
);
5379 int ext4_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
5381 struct page
*page
= vmf
->page
;
5385 struct file
*file
= vma
->vm_file
;
5386 struct inode
*inode
= file_inode(file
);
5387 struct address_space
*mapping
= inode
->i_mapping
;
5389 get_block_t
*get_block
;
5392 sb_start_pagefault(inode
->i_sb
);
5393 file_update_time(vma
->vm_file
);
5395 down_read(&EXT4_I(inode
)->i_mmap_sem
);
5397 ret
= ext4_convert_inline_data(inode
);
5401 /* Delalloc case is easy... */
5402 if (test_opt(inode
->i_sb
, DELALLOC
) &&
5403 !ext4_should_journal_data(inode
) &&
5404 !ext4_nonda_switch(inode
->i_sb
)) {
5406 ret
= block_page_mkwrite(vma
, vmf
,
5407 ext4_da_get_block_prep
);
5408 } while (ret
== -ENOSPC
&&
5409 ext4_should_retry_alloc(inode
->i_sb
, &retries
));
5414 size
= i_size_read(inode
);
5415 /* Page got truncated from under us? */
5416 if (page
->mapping
!= mapping
|| page_offset(page
) > size
) {
5418 ret
= VM_FAULT_NOPAGE
;
5422 if (page
->index
== size
>> PAGE_CACHE_SHIFT
)
5423 len
= size
& ~PAGE_CACHE_MASK
;
5425 len
= PAGE_CACHE_SIZE
;
5427 * Return if we have all the buffers mapped. This avoids the need to do
5428 * journal_start/journal_stop which can block and take a long time
5430 if (page_has_buffers(page
)) {
5431 if (!ext4_walk_page_buffers(NULL
, page_buffers(page
),
5433 ext4_bh_unmapped
)) {
5434 /* Wait so that we don't change page under IO */
5435 wait_for_stable_page(page
);
5436 ret
= VM_FAULT_LOCKED
;
5441 /* OK, we need to fill the hole... */
5442 if (ext4_should_dioread_nolock(inode
))
5443 get_block
= ext4_get_block_write
;
5445 get_block
= ext4_get_block
;
5447 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
5448 ext4_writepage_trans_blocks(inode
));
5449 if (IS_ERR(handle
)) {
5450 ret
= VM_FAULT_SIGBUS
;
5453 ret
= block_page_mkwrite(vma
, vmf
, get_block
);
5454 if (!ret
&& ext4_should_journal_data(inode
)) {
5455 if (ext4_walk_page_buffers(handle
, page_buffers(page
), 0,
5456 PAGE_CACHE_SIZE
, NULL
, do_journal_get_write_access
)) {
5458 ret
= VM_FAULT_SIGBUS
;
5459 ext4_journal_stop(handle
);
5462 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
5464 ext4_journal_stop(handle
);
5465 if (ret
== -ENOSPC
&& ext4_should_retry_alloc(inode
->i_sb
, &retries
))
5468 ret
= block_page_mkwrite_return(ret
);
5470 up_read(&EXT4_I(inode
)->i_mmap_sem
);
5471 sb_end_pagefault(inode
->i_sb
);
5475 int ext4_filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
5477 struct inode
*inode
= file_inode(vma
->vm_file
);
5480 down_read(&EXT4_I(inode
)->i_mmap_sem
);
5481 err
= filemap_fault(vma
, vmf
);
5482 up_read(&EXT4_I(inode
)->i_mmap_sem
);