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_has_feature_journal(inode
->i_sb
) &&
382 le32_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_journal_inum
)))
384 if (!ext4_data_block_valid(EXT4_SB(inode
->i_sb
), map
->m_pblk
,
386 ext4_error_inode(inode
, func
, line
, map
->m_pblk
,
387 "lblock %lu mapped to illegal pblock %llu "
388 "(length %d)", (unsigned long) map
->m_lblk
,
389 map
->m_pblk
, map
->m_len
);
390 return -EFSCORRUPTED
;
395 #define check_block_validity(inode, map) \
396 __check_block_validity((inode), __func__, __LINE__, (map))
398 #ifdef ES_AGGRESSIVE_TEST
399 static void ext4_map_blocks_es_recheck(handle_t
*handle
,
401 struct ext4_map_blocks
*es_map
,
402 struct ext4_map_blocks
*map
,
409 * There is a race window that the result is not the same.
410 * e.g. xfstests #223 when dioread_nolock enables. The reason
411 * is that we lookup a block mapping in extent status tree with
412 * out taking i_data_sem. So at the time the unwritten extent
413 * could be converted.
415 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
416 down_read(&EXT4_I(inode
)->i_data_sem
);
417 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
418 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
&
419 EXT4_GET_BLOCKS_KEEP_SIZE
);
421 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
&
422 EXT4_GET_BLOCKS_KEEP_SIZE
);
424 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
425 up_read((&EXT4_I(inode
)->i_data_sem
));
428 * We don't check m_len because extent will be collpased in status
429 * tree. So the m_len might not equal.
431 if (es_map
->m_lblk
!= map
->m_lblk
||
432 es_map
->m_flags
!= map
->m_flags
||
433 es_map
->m_pblk
!= map
->m_pblk
) {
434 printk("ES cache assertion failed for inode: %lu "
435 "es_cached ex [%d/%d/%llu/%x] != "
436 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
437 inode
->i_ino
, es_map
->m_lblk
, es_map
->m_len
,
438 es_map
->m_pblk
, es_map
->m_flags
, map
->m_lblk
,
439 map
->m_len
, map
->m_pblk
, map
->m_flags
,
443 #endif /* ES_AGGRESSIVE_TEST */
446 * The ext4_map_blocks() function tries to look up the requested blocks,
447 * and returns if the blocks are already mapped.
449 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
450 * and store the allocated blocks in the result buffer head and mark it
453 * If file type is extents based, it will call ext4_ext_map_blocks(),
454 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
457 * On success, it returns the number of blocks being mapped or allocated.
458 * if create==0 and the blocks are pre-allocated and unwritten block,
459 * the result buffer head is unmapped. If the create ==1, it will make sure
460 * the buffer head is mapped.
462 * It returns 0 if plain look up failed (blocks have not been allocated), in
463 * that case, buffer head is unmapped
465 * It returns the error in case of allocation failure.
467 int ext4_map_blocks(handle_t
*handle
, struct inode
*inode
,
468 struct ext4_map_blocks
*map
, int flags
)
470 struct extent_status es
;
473 #ifdef ES_AGGRESSIVE_TEST
474 struct ext4_map_blocks orig_map
;
476 memcpy(&orig_map
, map
, sizeof(*map
));
480 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
481 "logical block %lu\n", inode
->i_ino
, flags
, map
->m_len
,
482 (unsigned long) map
->m_lblk
);
485 * ext4_map_blocks returns an int, and m_len is an unsigned int
487 if (unlikely(map
->m_len
> INT_MAX
))
488 map
->m_len
= INT_MAX
;
490 /* We can handle the block number less than EXT_MAX_BLOCKS */
491 if (unlikely(map
->m_lblk
>= EXT_MAX_BLOCKS
))
492 return -EFSCORRUPTED
;
494 /* Lookup extent status tree firstly */
495 if (ext4_es_lookup_extent(inode
, map
->m_lblk
, &es
)) {
496 if (ext4_es_is_written(&es
) || ext4_es_is_unwritten(&es
)) {
497 map
->m_pblk
= ext4_es_pblock(&es
) +
498 map
->m_lblk
- es
.es_lblk
;
499 map
->m_flags
|= ext4_es_is_written(&es
) ?
500 EXT4_MAP_MAPPED
: EXT4_MAP_UNWRITTEN
;
501 retval
= es
.es_len
- (map
->m_lblk
- es
.es_lblk
);
502 if (retval
> map
->m_len
)
505 } else if (ext4_es_is_delayed(&es
) || ext4_es_is_hole(&es
)) {
510 #ifdef ES_AGGRESSIVE_TEST
511 ext4_map_blocks_es_recheck(handle
, inode
, map
,
518 * Try to see if we can get the block without requesting a new
521 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
522 down_read(&EXT4_I(inode
)->i_data_sem
);
523 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
524 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
&
525 EXT4_GET_BLOCKS_KEEP_SIZE
);
527 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
&
528 EXT4_GET_BLOCKS_KEEP_SIZE
);
533 if (unlikely(retval
!= map
->m_len
)) {
534 ext4_warning(inode
->i_sb
,
535 "ES len assertion failed for inode "
536 "%lu: retval %d != map->m_len %d",
537 inode
->i_ino
, retval
, map
->m_len
);
541 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
542 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
543 if (!(flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) &&
544 !(status
& EXTENT_STATUS_WRITTEN
) &&
545 ext4_find_delalloc_range(inode
, map
->m_lblk
,
546 map
->m_lblk
+ map
->m_len
- 1))
547 status
|= EXTENT_STATUS_DELAYED
;
548 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
,
549 map
->m_len
, map
->m_pblk
, status
);
553 if (!(flags
& EXT4_GET_BLOCKS_NO_LOCK
))
554 up_read((&EXT4_I(inode
)->i_data_sem
));
557 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
) {
558 ret
= check_block_validity(inode
, map
);
563 /* If it is only a block(s) look up */
564 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0)
568 * Returns if the blocks have already allocated
570 * Note that if blocks have been preallocated
571 * ext4_ext_get_block() returns the create = 0
572 * with buffer head unmapped.
574 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
)
576 * If we need to convert extent to unwritten
577 * we continue and do the actual work in
578 * ext4_ext_map_blocks()
580 if (!(flags
& EXT4_GET_BLOCKS_CONVERT_UNWRITTEN
))
584 * Here we clear m_flags because after allocating an new extent,
585 * it will be set again.
587 map
->m_flags
&= ~EXT4_MAP_FLAGS
;
590 * New blocks allocate and/or writing to unwritten extent
591 * will possibly result in updating i_data, so we take
592 * the write lock of i_data_sem, and call get_block()
593 * with create == 1 flag.
595 down_write(&EXT4_I(inode
)->i_data_sem
);
598 * We need to check for EXT4 here because migrate
599 * could have changed the inode type in between
601 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
602 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
);
604 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
);
606 if (retval
> 0 && map
->m_flags
& EXT4_MAP_NEW
) {
608 * We allocated new blocks which will result in
609 * i_data's format changing. Force the migrate
610 * to fail by clearing migrate flags
612 ext4_clear_inode_state(inode
, EXT4_STATE_EXT_MIGRATE
);
616 * Update reserved blocks/metadata blocks after successful
617 * block allocation which had been deferred till now. We don't
618 * support fallocate for non extent files. So we can update
619 * reserve space here.
622 (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
))
623 ext4_da_update_reserve_space(inode
, retval
, 1);
629 if (unlikely(retval
!= map
->m_len
)) {
630 ext4_warning(inode
->i_sb
,
631 "ES len assertion failed for inode "
632 "%lu: retval %d != map->m_len %d",
633 inode
->i_ino
, retval
, map
->m_len
);
638 * If the extent has been zeroed out, we don't need to update
639 * extent status tree.
641 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
) &&
642 ext4_es_lookup_extent(inode
, map
->m_lblk
, &es
)) {
643 if (ext4_es_is_written(&es
))
646 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
647 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
648 if (!(flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) &&
649 !(status
& EXTENT_STATUS_WRITTEN
) &&
650 ext4_find_delalloc_range(inode
, map
->m_lblk
,
651 map
->m_lblk
+ map
->m_len
- 1))
652 status
|= EXTENT_STATUS_DELAYED
;
653 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
, map
->m_len
,
654 map
->m_pblk
, status
);
660 up_write((&EXT4_I(inode
)->i_data_sem
));
661 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
) {
662 ret
= check_block_validity(inode
, map
);
667 * Inodes with freshly allocated blocks where contents will be
668 * visible after transaction commit must be on transaction's
671 if (map
->m_flags
& EXT4_MAP_NEW
&&
672 !(map
->m_flags
& EXT4_MAP_UNWRITTEN
) &&
673 !IS_NOQUOTA(inode
) &&
674 ext4_should_order_data(inode
)) {
675 ret
= ext4_jbd2_file_inode(handle
, inode
);
684 * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
685 * we have to be careful as someone else may be manipulating b_state as well.
687 static void ext4_update_bh_state(struct buffer_head
*bh
, unsigned long flags
)
689 unsigned long old_state
;
690 unsigned long new_state
;
692 flags
&= EXT4_MAP_FLAGS
;
694 /* Dummy buffer_head? Set non-atomically. */
696 bh
->b_state
= (bh
->b_state
& ~EXT4_MAP_FLAGS
) | flags
;
700 * Someone else may be modifying b_state. Be careful! This is ugly but
701 * once we get rid of using bh as a container for mapping information
702 * to pass to / from get_block functions, this can go away.
705 old_state
= READ_ONCE(bh
->b_state
);
706 new_state
= (old_state
& ~EXT4_MAP_FLAGS
) | flags
;
708 cmpxchg(&bh
->b_state
, old_state
, new_state
) != old_state
));
711 /* Maximum number of blocks we map for direct IO at once. */
712 #define DIO_MAX_BLOCKS 4096
714 static int _ext4_get_block(struct inode
*inode
, sector_t iblock
,
715 struct buffer_head
*bh
, int flags
)
717 handle_t
*handle
= ext4_journal_current_handle();
718 struct ext4_map_blocks map
;
719 int ret
= 0, started
= 0;
722 if (ext4_has_inline_data(inode
))
726 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
728 if (flags
&& !(flags
& EXT4_GET_BLOCKS_NO_LOCK
) && !handle
) {
729 /* Direct IO write... */
730 if (map
.m_len
> DIO_MAX_BLOCKS
)
731 map
.m_len
= DIO_MAX_BLOCKS
;
732 dio_credits
= ext4_chunk_trans_blocks(inode
, map
.m_len
);
733 handle
= ext4_journal_start(inode
, EXT4_HT_MAP_BLOCKS
,
735 if (IS_ERR(handle
)) {
736 ret
= PTR_ERR(handle
);
742 ret
= ext4_map_blocks(handle
, inode
, &map
, flags
);
744 ext4_io_end_t
*io_end
= ext4_inode_aio(inode
);
746 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
747 ext4_update_bh_state(bh
, map
.m_flags
);
748 if (IS_DAX(inode
) && buffer_unwritten(bh
)) {
750 * dgc: I suspect unwritten conversion on ext4+DAX is
751 * fundamentally broken here when there are concurrent
752 * read/write in progress on this inode.
754 WARN_ON_ONCE(io_end
);
755 bh
->b_assoc_map
= inode
->i_mapping
;
756 bh
->b_private
= (void *)(unsigned long)iblock
;
758 if (io_end
&& io_end
->flag
& EXT4_IO_END_UNWRITTEN
)
759 set_buffer_defer_completion(bh
);
760 bh
->b_size
= inode
->i_sb
->s_blocksize
* map
.m_len
;
764 ext4_journal_stop(handle
);
768 int ext4_get_block(struct inode
*inode
, sector_t iblock
,
769 struct buffer_head
*bh
, int create
)
771 return _ext4_get_block(inode
, iblock
, bh
,
772 create
? EXT4_GET_BLOCKS_CREATE
: 0);
776 * `handle' can be NULL if create is zero
778 struct buffer_head
*ext4_getblk(handle_t
*handle
, struct inode
*inode
,
779 ext4_lblk_t block
, int map_flags
)
781 struct ext4_map_blocks map
;
782 struct buffer_head
*bh
;
783 int create
= map_flags
& EXT4_GET_BLOCKS_CREATE
;
786 J_ASSERT(handle
!= NULL
|| create
== 0);
790 err
= ext4_map_blocks(handle
, inode
, &map
, map_flags
);
793 return create
? ERR_PTR(-ENOSPC
) : NULL
;
797 bh
= sb_getblk(inode
->i_sb
, map
.m_pblk
);
799 return ERR_PTR(-ENOMEM
);
800 if (map
.m_flags
& EXT4_MAP_NEW
) {
801 J_ASSERT(create
!= 0);
802 J_ASSERT(handle
!= NULL
);
805 * Now that we do not always journal data, we should
806 * keep in mind whether this should always journal the
807 * new buffer as metadata. For now, regular file
808 * writes use ext4_get_block instead, so it's not a
812 BUFFER_TRACE(bh
, "call get_create_access");
813 err
= ext4_journal_get_create_access(handle
, bh
);
818 if (!buffer_uptodate(bh
)) {
819 memset(bh
->b_data
, 0, inode
->i_sb
->s_blocksize
);
820 set_buffer_uptodate(bh
);
823 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
824 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
828 BUFFER_TRACE(bh
, "not a new buffer");
835 struct buffer_head
*ext4_bread(handle_t
*handle
, struct inode
*inode
,
836 ext4_lblk_t block
, int map_flags
)
838 struct buffer_head
*bh
;
840 bh
= ext4_getblk(handle
, inode
, block
, map_flags
);
843 if (!bh
|| buffer_uptodate(bh
))
845 ll_rw_block(READ
| REQ_META
| REQ_PRIO
, 1, &bh
);
847 if (buffer_uptodate(bh
))
850 return ERR_PTR(-EIO
);
853 int ext4_walk_page_buffers(handle_t
*handle
,
854 struct buffer_head
*head
,
858 int (*fn
)(handle_t
*handle
,
859 struct buffer_head
*bh
))
861 struct buffer_head
*bh
;
862 unsigned block_start
, block_end
;
863 unsigned blocksize
= head
->b_size
;
865 struct buffer_head
*next
;
867 for (bh
= head
, block_start
= 0;
868 ret
== 0 && (bh
!= head
|| !block_start
);
869 block_start
= block_end
, bh
= next
) {
870 next
= bh
->b_this_page
;
871 block_end
= block_start
+ blocksize
;
872 if (block_end
<= from
|| block_start
>= to
) {
873 if (partial
&& !buffer_uptodate(bh
))
877 err
= (*fn
)(handle
, bh
);
885 * To preserve ordering, it is essential that the hole instantiation and
886 * the data write be encapsulated in a single transaction. We cannot
887 * close off a transaction and start a new one between the ext4_get_block()
888 * and the commit_write(). So doing the jbd2_journal_start at the start of
889 * prepare_write() is the right place.
891 * Also, this function can nest inside ext4_writepage(). In that case, we
892 * *know* that ext4_writepage() has generated enough buffer credits to do the
893 * whole page. So we won't block on the journal in that case, which is good,
894 * because the caller may be PF_MEMALLOC.
896 * By accident, ext4 can be reentered when a transaction is open via
897 * quota file writes. If we were to commit the transaction while thus
898 * reentered, there can be a deadlock - we would be holding a quota
899 * lock, and the commit would never complete if another thread had a
900 * transaction open and was blocking on the quota lock - a ranking
903 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
904 * will _not_ run commit under these circumstances because handle->h_ref
905 * is elevated. We'll still have enough credits for the tiny quotafile
908 int do_journal_get_write_access(handle_t
*handle
,
909 struct buffer_head
*bh
)
911 int dirty
= buffer_dirty(bh
);
914 if (!buffer_mapped(bh
) || buffer_freed(bh
))
917 * __block_write_begin() could have dirtied some buffers. Clean
918 * the dirty bit as jbd2_journal_get_write_access() could complain
919 * otherwise about fs integrity issues. Setting of the dirty bit
920 * by __block_write_begin() isn't a real problem here as we clear
921 * the bit before releasing a page lock and thus writeback cannot
922 * ever write the buffer.
925 clear_buffer_dirty(bh
);
926 BUFFER_TRACE(bh
, "get write access");
927 ret
= ext4_journal_get_write_access(handle
, bh
);
929 ret
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
933 static int ext4_get_block_write_nolock(struct inode
*inode
, sector_t iblock
,
934 struct buffer_head
*bh_result
, int create
);
936 #ifdef CONFIG_EXT4_FS_ENCRYPTION
937 static int ext4_block_write_begin(struct page
*page
, loff_t pos
, unsigned len
,
938 get_block_t
*get_block
)
940 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
941 unsigned to
= from
+ len
;
942 struct inode
*inode
= page
->mapping
->host
;
943 unsigned block_start
, block_end
;
946 unsigned blocksize
= inode
->i_sb
->s_blocksize
;
948 struct buffer_head
*bh
, *head
, *wait
[2], **wait_bh
= wait
;
949 bool decrypt
= false;
951 BUG_ON(!PageLocked(page
));
952 BUG_ON(from
> PAGE_CACHE_SIZE
);
953 BUG_ON(to
> PAGE_CACHE_SIZE
);
956 if (!page_has_buffers(page
))
957 create_empty_buffers(page
, blocksize
, 0);
958 head
= page_buffers(page
);
959 bbits
= ilog2(blocksize
);
960 block
= (sector_t
)page
->index
<< (PAGE_CACHE_SHIFT
- bbits
);
962 for (bh
= head
, block_start
= 0; bh
!= head
|| !block_start
;
963 block
++, block_start
= block_end
, bh
= bh
->b_this_page
) {
964 block_end
= block_start
+ blocksize
;
965 if (block_end
<= from
|| block_start
>= to
) {
966 if (PageUptodate(page
)) {
967 if (!buffer_uptodate(bh
))
968 set_buffer_uptodate(bh
);
973 clear_buffer_new(bh
);
974 if (!buffer_mapped(bh
)) {
975 WARN_ON(bh
->b_size
!= blocksize
);
976 err
= get_block(inode
, block
, bh
, 1);
979 if (buffer_new(bh
)) {
980 unmap_underlying_metadata(bh
->b_bdev
,
982 if (PageUptodate(page
)) {
983 clear_buffer_new(bh
);
984 set_buffer_uptodate(bh
);
985 mark_buffer_dirty(bh
);
988 if (block_end
> to
|| block_start
< from
)
989 zero_user_segments(page
, to
, block_end
,
994 if (PageUptodate(page
)) {
995 if (!buffer_uptodate(bh
))
996 set_buffer_uptodate(bh
);
999 if (!buffer_uptodate(bh
) && !buffer_delay(bh
) &&
1000 !buffer_unwritten(bh
) &&
1001 (block_start
< from
|| block_end
> to
)) {
1002 ll_rw_block(READ
, 1, &bh
);
1004 decrypt
= ext4_encrypted_inode(inode
) &&
1005 S_ISREG(inode
->i_mode
);
1009 * If we issued read requests, let them complete.
1011 while (wait_bh
> wait
) {
1012 wait_on_buffer(*--wait_bh
);
1013 if (!buffer_uptodate(*wait_bh
))
1017 page_zero_new_buffers(page
, from
, to
);
1019 err
= ext4_decrypt(page
);
1024 static int ext4_write_begin(struct file
*file
, struct address_space
*mapping
,
1025 loff_t pos
, unsigned len
, unsigned flags
,
1026 struct page
**pagep
, void **fsdata
)
1028 struct inode
*inode
= mapping
->host
;
1029 int ret
, needed_blocks
;
1036 trace_ext4_write_begin(inode
, pos
, len
, flags
);
1038 * Reserve one block more for addition to orphan list in case
1039 * we allocate blocks but write fails for some reason
1041 needed_blocks
= ext4_writepage_trans_blocks(inode
) + 1;
1042 index
= pos
>> PAGE_CACHE_SHIFT
;
1043 from
= pos
& (PAGE_CACHE_SIZE
- 1);
1046 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
1047 ret
= ext4_try_to_write_inline_data(mapping
, inode
, pos
, len
,
1056 * grab_cache_page_write_begin() can take a long time if the
1057 * system is thrashing due to memory pressure, or if the page
1058 * is being written back. So grab it first before we start
1059 * the transaction handle. This also allows us to allocate
1060 * the page (if needed) without using GFP_NOFS.
1063 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
1069 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
, needed_blocks
);
1070 if (IS_ERR(handle
)) {
1071 page_cache_release(page
);
1072 return PTR_ERR(handle
);
1076 if (page
->mapping
!= mapping
) {
1077 /* The page got truncated from under us */
1079 page_cache_release(page
);
1080 ext4_journal_stop(handle
);
1083 /* In case writeback began while the page was unlocked */
1084 wait_for_stable_page(page
);
1086 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1087 if (ext4_should_dioread_nolock(inode
))
1088 ret
= ext4_block_write_begin(page
, pos
, len
,
1089 ext4_get_block_write
);
1091 ret
= ext4_block_write_begin(page
, pos
, len
,
1094 if (ext4_should_dioread_nolock(inode
))
1095 ret
= __block_write_begin(page
, pos
, len
, ext4_get_block_write
);
1097 ret
= __block_write_begin(page
, pos
, len
, ext4_get_block
);
1099 if (!ret
&& ext4_should_journal_data(inode
)) {
1100 ret
= ext4_walk_page_buffers(handle
, page_buffers(page
),
1102 do_journal_get_write_access
);
1108 * __block_write_begin may have instantiated a few blocks
1109 * outside i_size. Trim these off again. Don't need
1110 * i_size_read because we hold i_mutex.
1112 * Add inode to orphan list in case we crash before
1115 if (pos
+ len
> inode
->i_size
&& ext4_can_truncate(inode
))
1116 ext4_orphan_add(handle
, inode
);
1118 ext4_journal_stop(handle
);
1119 if (pos
+ len
> inode
->i_size
) {
1120 ext4_truncate_failed_write(inode
);
1122 * If truncate failed early the inode might
1123 * still be on the orphan list; we need to
1124 * make sure the inode is removed from the
1125 * orphan list in that case.
1128 ext4_orphan_del(NULL
, inode
);
1131 if (ret
== -ENOSPC
&&
1132 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1134 page_cache_release(page
);
1141 /* For write_end() in data=journal mode */
1142 static int write_end_fn(handle_t
*handle
, struct buffer_head
*bh
)
1145 if (!buffer_mapped(bh
) || buffer_freed(bh
))
1147 set_buffer_uptodate(bh
);
1148 ret
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
1149 clear_buffer_meta(bh
);
1150 clear_buffer_prio(bh
);
1155 * We need to pick up the new inode size which generic_commit_write gave us
1156 * `file' can be NULL - eg, when called from page_symlink().
1158 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1159 * buffers are managed internally.
1161 static int ext4_write_end(struct file
*file
,
1162 struct address_space
*mapping
,
1163 loff_t pos
, unsigned len
, unsigned copied
,
1164 struct page
*page
, void *fsdata
)
1166 handle_t
*handle
= ext4_journal_current_handle();
1167 struct inode
*inode
= mapping
->host
;
1168 loff_t old_size
= inode
->i_size
;
1170 int i_size_changed
= 0;
1171 int inline_data
= ext4_has_inline_data(inode
);
1173 trace_ext4_write_end(inode
, pos
, len
, copied
);
1175 ret
= ext4_write_inline_data_end(inode
, pos
, len
,
1184 copied
= block_write_end(file
, mapping
, pos
,
1185 len
, copied
, page
, fsdata
);
1187 * it's important to update i_size while still holding page lock:
1188 * page writeout could otherwise come in and zero beyond i_size.
1190 i_size_changed
= ext4_update_inode_size(inode
, pos
+ copied
);
1192 page_cache_release(page
);
1195 pagecache_isize_extended(inode
, old_size
, pos
);
1197 * Don't mark the inode dirty under page lock. First, it unnecessarily
1198 * makes the holding time of page lock longer. Second, it forces lock
1199 * ordering of page lock and transaction start for journaling
1202 if (i_size_changed
|| inline_data
)
1203 ext4_mark_inode_dirty(handle
, inode
);
1205 if (pos
+ len
> inode
->i_size
&& ext4_can_truncate(inode
))
1206 /* if we have allocated more blocks and copied
1207 * less. We will have blocks allocated outside
1208 * inode->i_size. So truncate them
1210 ext4_orphan_add(handle
, inode
);
1212 ret2
= ext4_journal_stop(handle
);
1216 if (pos
+ len
> inode
->i_size
) {
1217 ext4_truncate_failed_write(inode
);
1219 * If truncate failed early the inode might still be
1220 * on the orphan list; we need to make sure the inode
1221 * is removed from the orphan list in that case.
1224 ext4_orphan_del(NULL
, inode
);
1227 return ret
? ret
: copied
;
1231 * This is a private version of page_zero_new_buffers() which doesn't
1232 * set the buffer to be dirty, since in data=journalled mode we need
1233 * to call ext4_handle_dirty_metadata() instead.
1235 static void ext4_journalled_zero_new_buffers(handle_t
*handle
,
1237 unsigned from
, unsigned to
)
1239 unsigned int block_start
= 0, block_end
;
1240 struct buffer_head
*head
, *bh
;
1242 bh
= head
= page_buffers(page
);
1244 block_end
= block_start
+ bh
->b_size
;
1245 if (buffer_new(bh
)) {
1246 if (block_end
> from
&& block_start
< to
) {
1247 if (!PageUptodate(page
)) {
1248 unsigned start
, size
;
1250 start
= max(from
, block_start
);
1251 size
= min(to
, block_end
) - start
;
1253 zero_user(page
, start
, size
);
1254 write_end_fn(handle
, bh
);
1256 clear_buffer_new(bh
);
1259 block_start
= block_end
;
1260 bh
= bh
->b_this_page
;
1261 } while (bh
!= head
);
1264 static int ext4_journalled_write_end(struct file
*file
,
1265 struct address_space
*mapping
,
1266 loff_t pos
, unsigned len
, unsigned copied
,
1267 struct page
*page
, void *fsdata
)
1269 handle_t
*handle
= ext4_journal_current_handle();
1270 struct inode
*inode
= mapping
->host
;
1271 loff_t old_size
= inode
->i_size
;
1275 int size_changed
= 0;
1276 int inline_data
= ext4_has_inline_data(inode
);
1278 trace_ext4_journalled_write_end(inode
, pos
, len
, copied
);
1279 from
= pos
& (PAGE_CACHE_SIZE
- 1);
1282 BUG_ON(!ext4_handle_valid(handle
));
1285 ret
= ext4_write_inline_data_end(inode
, pos
, len
,
1293 } else if (unlikely(copied
< len
) && !PageUptodate(page
)) {
1295 ext4_journalled_zero_new_buffers(handle
, page
, from
, to
);
1297 if (unlikely(copied
< len
))
1298 ext4_journalled_zero_new_buffers(handle
, page
,
1300 ret
= ext4_walk_page_buffers(handle
, page_buffers(page
), from
,
1301 from
+ copied
, &partial
,
1304 SetPageUptodate(page
);
1306 size_changed
= ext4_update_inode_size(inode
, pos
+ copied
);
1307 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
1308 EXT4_I(inode
)->i_datasync_tid
= handle
->h_transaction
->t_tid
;
1310 page_cache_release(page
);
1313 pagecache_isize_extended(inode
, old_size
, pos
);
1315 if (size_changed
|| inline_data
) {
1316 ret2
= ext4_mark_inode_dirty(handle
, inode
);
1321 if (pos
+ len
> inode
->i_size
&& ext4_can_truncate(inode
))
1322 /* if we have allocated more blocks and copied
1323 * less. We will have blocks allocated outside
1324 * inode->i_size. So truncate them
1326 ext4_orphan_add(handle
, inode
);
1329 ret2
= ext4_journal_stop(handle
);
1332 if (pos
+ len
> inode
->i_size
) {
1333 ext4_truncate_failed_write(inode
);
1335 * If truncate failed early the inode might still be
1336 * on the orphan list; we need to make sure the inode
1337 * is removed from the orphan list in that case.
1340 ext4_orphan_del(NULL
, inode
);
1343 return ret
? ret
: copied
;
1347 * Reserve space for a single cluster
1349 static int ext4_da_reserve_space(struct inode
*inode
)
1351 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1352 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1356 * We will charge metadata quota at writeout time; this saves
1357 * us from metadata over-estimation, though we may go over by
1358 * a small amount in the end. Here we just reserve for data.
1360 ret
= dquot_reserve_block(inode
, EXT4_C2B(sbi
, 1));
1364 spin_lock(&ei
->i_block_reservation_lock
);
1365 if (ext4_claim_free_clusters(sbi
, 1, 0)) {
1366 spin_unlock(&ei
->i_block_reservation_lock
);
1367 dquot_release_reservation_block(inode
, EXT4_C2B(sbi
, 1));
1370 ei
->i_reserved_data_blocks
++;
1371 trace_ext4_da_reserve_space(inode
);
1372 spin_unlock(&ei
->i_block_reservation_lock
);
1374 return 0; /* success */
1377 static void ext4_da_release_space(struct inode
*inode
, int to_free
)
1379 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1380 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1383 return; /* Nothing to release, exit */
1385 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1387 trace_ext4_da_release_space(inode
, to_free
);
1388 if (unlikely(to_free
> ei
->i_reserved_data_blocks
)) {
1390 * if there aren't enough reserved blocks, then the
1391 * counter is messed up somewhere. Since this
1392 * function is called from invalidate page, it's
1393 * harmless to return without any action.
1395 ext4_warning(inode
->i_sb
, "ext4_da_release_space: "
1396 "ino %lu, to_free %d with only %d reserved "
1397 "data blocks", inode
->i_ino
, to_free
,
1398 ei
->i_reserved_data_blocks
);
1400 to_free
= ei
->i_reserved_data_blocks
;
1402 ei
->i_reserved_data_blocks
-= to_free
;
1404 /* update fs dirty data blocks counter */
1405 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
, to_free
);
1407 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1409 dquot_release_reservation_block(inode
, EXT4_C2B(sbi
, to_free
));
1412 static void ext4_da_page_release_reservation(struct page
*page
,
1413 unsigned int offset
,
1414 unsigned int length
)
1416 int to_release
= 0, contiguous_blks
= 0;
1417 struct buffer_head
*head
, *bh
;
1418 unsigned int curr_off
= 0;
1419 struct inode
*inode
= page
->mapping
->host
;
1420 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1421 unsigned int stop
= offset
+ length
;
1425 BUG_ON(stop
> PAGE_CACHE_SIZE
|| stop
< length
);
1427 head
= page_buffers(page
);
1430 unsigned int next_off
= curr_off
+ bh
->b_size
;
1432 if (next_off
> stop
)
1435 if ((offset
<= curr_off
) && (buffer_delay(bh
))) {
1438 clear_buffer_delay(bh
);
1439 } else if (contiguous_blks
) {
1440 lblk
= page
->index
<<
1441 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1442 lblk
+= (curr_off
>> inode
->i_blkbits
) -
1444 ext4_es_remove_extent(inode
, lblk
, contiguous_blks
);
1445 contiguous_blks
= 0;
1447 curr_off
= next_off
;
1448 } while ((bh
= bh
->b_this_page
) != head
);
1450 if (contiguous_blks
) {
1451 lblk
= page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1452 lblk
+= (curr_off
>> inode
->i_blkbits
) - contiguous_blks
;
1453 ext4_es_remove_extent(inode
, lblk
, contiguous_blks
);
1456 /* If we have released all the blocks belonging to a cluster, then we
1457 * need to release the reserved space for that cluster. */
1458 num_clusters
= EXT4_NUM_B2C(sbi
, to_release
);
1459 while (num_clusters
> 0) {
1460 lblk
= (page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
)) +
1461 ((num_clusters
- 1) << sbi
->s_cluster_bits
);
1462 if (sbi
->s_cluster_ratio
== 1 ||
1463 !ext4_find_delalloc_cluster(inode
, lblk
))
1464 ext4_da_release_space(inode
, 1);
1471 * Delayed allocation stuff
1474 struct mpage_da_data
{
1475 struct inode
*inode
;
1476 struct writeback_control
*wbc
;
1478 pgoff_t first_page
; /* The first page to write */
1479 pgoff_t next_page
; /* Current page to examine */
1480 pgoff_t last_page
; /* Last page to examine */
1482 * Extent to map - this can be after first_page because that can be
1483 * fully mapped. We somewhat abuse m_flags to store whether the extent
1484 * is delalloc or unwritten.
1486 struct ext4_map_blocks map
;
1487 struct ext4_io_submit io_submit
; /* IO submission data */
1490 static void mpage_release_unused_pages(struct mpage_da_data
*mpd
,
1495 struct pagevec pvec
;
1496 struct inode
*inode
= mpd
->inode
;
1497 struct address_space
*mapping
= inode
->i_mapping
;
1499 /* This is necessary when next_page == 0. */
1500 if (mpd
->first_page
>= mpd
->next_page
)
1503 index
= mpd
->first_page
;
1504 end
= mpd
->next_page
- 1;
1506 ext4_lblk_t start
, last
;
1507 start
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1508 last
= end
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1509 ext4_es_remove_extent(inode
, start
, last
- start
+ 1);
1512 pagevec_init(&pvec
, 0);
1513 while (index
<= end
) {
1514 nr_pages
= pagevec_lookup(&pvec
, mapping
, index
, PAGEVEC_SIZE
);
1517 for (i
= 0; i
< nr_pages
; i
++) {
1518 struct page
*page
= pvec
.pages
[i
];
1519 if (page
->index
> end
)
1521 BUG_ON(!PageLocked(page
));
1522 BUG_ON(PageWriteback(page
));
1524 if (page_mapped(page
))
1525 clear_page_dirty_for_io(page
);
1526 block_invalidatepage(page
, 0, PAGE_CACHE_SIZE
);
1527 ClearPageUptodate(page
);
1531 index
= pvec
.pages
[nr_pages
- 1]->index
+ 1;
1532 pagevec_release(&pvec
);
1536 static void ext4_print_free_blocks(struct inode
*inode
)
1538 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1539 struct super_block
*sb
= inode
->i_sb
;
1540 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1542 ext4_msg(sb
, KERN_CRIT
, "Total free blocks count %lld",
1543 EXT4_C2B(EXT4_SB(inode
->i_sb
),
1544 ext4_count_free_clusters(sb
)));
1545 ext4_msg(sb
, KERN_CRIT
, "Free/Dirty block details");
1546 ext4_msg(sb
, KERN_CRIT
, "free_blocks=%lld",
1547 (long long) EXT4_C2B(EXT4_SB(sb
),
1548 percpu_counter_sum(&sbi
->s_freeclusters_counter
)));
1549 ext4_msg(sb
, KERN_CRIT
, "dirty_blocks=%lld",
1550 (long long) EXT4_C2B(EXT4_SB(sb
),
1551 percpu_counter_sum(&sbi
->s_dirtyclusters_counter
)));
1552 ext4_msg(sb
, KERN_CRIT
, "Block reservation details");
1553 ext4_msg(sb
, KERN_CRIT
, "i_reserved_data_blocks=%u",
1554 ei
->i_reserved_data_blocks
);
1558 static int ext4_bh_delay_or_unwritten(handle_t
*handle
, struct buffer_head
*bh
)
1560 return (buffer_delay(bh
) || buffer_unwritten(bh
)) && buffer_dirty(bh
);
1564 * This function is grabs code from the very beginning of
1565 * ext4_map_blocks, but assumes that the caller is from delayed write
1566 * time. This function looks up the requested blocks and sets the
1567 * buffer delay bit under the protection of i_data_sem.
1569 static int ext4_da_map_blocks(struct inode
*inode
, sector_t iblock
,
1570 struct ext4_map_blocks
*map
,
1571 struct buffer_head
*bh
)
1573 struct extent_status es
;
1575 sector_t invalid_block
= ~((sector_t
) 0xffff);
1576 #ifdef ES_AGGRESSIVE_TEST
1577 struct ext4_map_blocks orig_map
;
1579 memcpy(&orig_map
, map
, sizeof(*map
));
1582 if (invalid_block
< ext4_blocks_count(EXT4_SB(inode
->i_sb
)->s_es
))
1586 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1587 "logical block %lu\n", inode
->i_ino
, map
->m_len
,
1588 (unsigned long) map
->m_lblk
);
1590 /* Lookup extent status tree firstly */
1591 if (ext4_es_lookup_extent(inode
, iblock
, &es
)) {
1592 if (ext4_es_is_hole(&es
)) {
1594 down_read(&EXT4_I(inode
)->i_data_sem
);
1599 * Delayed extent could be allocated by fallocate.
1600 * So we need to check it.
1602 if (ext4_es_is_delayed(&es
) && !ext4_es_is_unwritten(&es
)) {
1603 map_bh(bh
, inode
->i_sb
, invalid_block
);
1605 set_buffer_delay(bh
);
1609 map
->m_pblk
= ext4_es_pblock(&es
) + iblock
- es
.es_lblk
;
1610 retval
= es
.es_len
- (iblock
- es
.es_lblk
);
1611 if (retval
> map
->m_len
)
1612 retval
= map
->m_len
;
1613 map
->m_len
= retval
;
1614 if (ext4_es_is_written(&es
))
1615 map
->m_flags
|= EXT4_MAP_MAPPED
;
1616 else if (ext4_es_is_unwritten(&es
))
1617 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
1621 #ifdef ES_AGGRESSIVE_TEST
1622 ext4_map_blocks_es_recheck(NULL
, inode
, map
, &orig_map
, 0);
1628 * Try to see if we can get the block without requesting a new
1629 * file system block.
1631 down_read(&EXT4_I(inode
)->i_data_sem
);
1632 if (ext4_has_inline_data(inode
))
1634 else if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
1635 retval
= ext4_ext_map_blocks(NULL
, inode
, map
, 0);
1637 retval
= ext4_ind_map_blocks(NULL
, inode
, map
, 0);
1643 * XXX: __block_prepare_write() unmaps passed block,
1647 * If the block was allocated from previously allocated cluster,
1648 * then we don't need to reserve it again. However we still need
1649 * to reserve metadata for every block we're going to write.
1651 if (EXT4_SB(inode
->i_sb
)->s_cluster_ratio
== 1 ||
1652 !ext4_find_delalloc_cluster(inode
, map
->m_lblk
)) {
1653 ret
= ext4_da_reserve_space(inode
);
1655 /* not enough space to reserve */
1661 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
, map
->m_len
,
1662 ~0, EXTENT_STATUS_DELAYED
);
1668 map_bh(bh
, inode
->i_sb
, invalid_block
);
1670 set_buffer_delay(bh
);
1671 } else if (retval
> 0) {
1673 unsigned int status
;
1675 if (unlikely(retval
!= map
->m_len
)) {
1676 ext4_warning(inode
->i_sb
,
1677 "ES len assertion failed for inode "
1678 "%lu: retval %d != map->m_len %d",
1679 inode
->i_ino
, retval
, map
->m_len
);
1683 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
1684 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
1685 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
, map
->m_len
,
1686 map
->m_pblk
, status
);
1692 up_read((&EXT4_I(inode
)->i_data_sem
));
1698 * This is a special get_block_t callback which is used by
1699 * ext4_da_write_begin(). It will either return mapped block or
1700 * reserve space for a single block.
1702 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1703 * We also have b_blocknr = -1 and b_bdev initialized properly
1705 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1706 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1707 * initialized properly.
1709 int ext4_da_get_block_prep(struct inode
*inode
, sector_t iblock
,
1710 struct buffer_head
*bh
, int create
)
1712 struct ext4_map_blocks map
;
1715 BUG_ON(create
== 0);
1716 BUG_ON(bh
->b_size
!= inode
->i_sb
->s_blocksize
);
1718 map
.m_lblk
= iblock
;
1722 * first, we need to know whether the block is allocated already
1723 * preallocated blocks are unmapped but should treated
1724 * the same as allocated blocks.
1726 ret
= ext4_da_map_blocks(inode
, iblock
, &map
, bh
);
1730 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
1731 ext4_update_bh_state(bh
, map
.m_flags
);
1733 if (buffer_unwritten(bh
)) {
1734 /* A delayed write to unwritten bh should be marked
1735 * new and mapped. Mapped ensures that we don't do
1736 * get_block multiple times when we write to the same
1737 * offset and new ensures that we do proper zero out
1738 * for partial write.
1741 set_buffer_mapped(bh
);
1746 static int bget_one(handle_t
*handle
, struct buffer_head
*bh
)
1752 static int bput_one(handle_t
*handle
, struct buffer_head
*bh
)
1758 static int __ext4_journalled_writepage(struct page
*page
,
1761 struct address_space
*mapping
= page
->mapping
;
1762 struct inode
*inode
= mapping
->host
;
1763 struct buffer_head
*page_bufs
= NULL
;
1764 handle_t
*handle
= NULL
;
1765 int ret
= 0, err
= 0;
1766 int inline_data
= ext4_has_inline_data(inode
);
1767 struct buffer_head
*inode_bh
= NULL
;
1769 ClearPageChecked(page
);
1772 BUG_ON(page
->index
!= 0);
1773 BUG_ON(len
> ext4_get_max_inline_size(inode
));
1774 inode_bh
= ext4_journalled_write_inline_data(inode
, len
, page
);
1775 if (inode_bh
== NULL
)
1778 page_bufs
= page_buffers(page
);
1783 ext4_walk_page_buffers(handle
, page_bufs
, 0, len
,
1787 * We need to release the page lock before we start the
1788 * journal, so grab a reference so the page won't disappear
1789 * out from under us.
1794 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
1795 ext4_writepage_trans_blocks(inode
));
1796 if (IS_ERR(handle
)) {
1797 ret
= PTR_ERR(handle
);
1799 goto out_no_pagelock
;
1801 BUG_ON(!ext4_handle_valid(handle
));
1805 if (page
->mapping
!= mapping
) {
1806 /* The page got truncated from under us */
1807 ext4_journal_stop(handle
);
1813 ret
= ext4_mark_inode_dirty(handle
, inode
);
1815 ret
= ext4_walk_page_buffers(handle
, page_bufs
, 0, len
, NULL
,
1816 do_journal_get_write_access
);
1818 err
= ext4_walk_page_buffers(handle
, page_bufs
, 0, len
, NULL
,
1823 EXT4_I(inode
)->i_datasync_tid
= handle
->h_transaction
->t_tid
;
1824 err
= ext4_journal_stop(handle
);
1828 if (!ext4_has_inline_data(inode
))
1829 ext4_walk_page_buffers(NULL
, page_bufs
, 0, len
,
1831 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
1840 * Note that we don't need to start a transaction unless we're journaling data
1841 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1842 * need to file the inode to the transaction's list in ordered mode because if
1843 * we are writing back data added by write(), the inode is already there and if
1844 * we are writing back data modified via mmap(), no one guarantees in which
1845 * transaction the data will hit the disk. In case we are journaling data, we
1846 * cannot start transaction directly because transaction start ranks above page
1847 * lock so we have to do some magic.
1849 * This function can get called via...
1850 * - ext4_writepages after taking page lock (have journal handle)
1851 * - journal_submit_inode_data_buffers (no journal handle)
1852 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
1853 * - grab_page_cache when doing write_begin (have journal handle)
1855 * We don't do any block allocation in this function. If we have page with
1856 * multiple blocks we need to write those buffer_heads that are mapped. This
1857 * is important for mmaped based write. So if we do with blocksize 1K
1858 * truncate(f, 1024);
1859 * a = mmap(f, 0, 4096);
1861 * truncate(f, 4096);
1862 * we have in the page first buffer_head mapped via page_mkwrite call back
1863 * but other buffer_heads would be unmapped but dirty (dirty done via the
1864 * do_wp_page). So writepage should write the first block. If we modify
1865 * the mmap area beyond 1024 we will again get a page_fault and the
1866 * page_mkwrite callback will do the block allocation and mark the
1867 * buffer_heads mapped.
1869 * We redirty the page if we have any buffer_heads that is either delay or
1870 * unwritten in the page.
1872 * We can get recursively called as show below.
1874 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1877 * But since we don't do any block allocation we should not deadlock.
1878 * Page also have the dirty flag cleared so we don't get recurive page_lock.
1880 static int ext4_writepage(struct page
*page
,
1881 struct writeback_control
*wbc
)
1886 struct buffer_head
*page_bufs
= NULL
;
1887 struct inode
*inode
= page
->mapping
->host
;
1888 struct ext4_io_submit io_submit
;
1889 bool keep_towrite
= false;
1891 trace_ext4_writepage(page
);
1892 size
= i_size_read(inode
);
1893 if (page
->index
== size
>> PAGE_CACHE_SHIFT
)
1894 len
= size
& ~PAGE_CACHE_MASK
;
1896 len
= PAGE_CACHE_SIZE
;
1898 page_bufs
= page_buffers(page
);
1900 * We cannot do block allocation or other extent handling in this
1901 * function. If there are buffers needing that, we have to redirty
1902 * the page. But we may reach here when we do a journal commit via
1903 * journal_submit_inode_data_buffers() and in that case we must write
1904 * allocated buffers to achieve data=ordered mode guarantees.
1906 * Also, if there is only one buffer per page (the fs block
1907 * size == the page size), if one buffer needs block
1908 * allocation or needs to modify the extent tree to clear the
1909 * unwritten flag, we know that the page can't be written at
1910 * all, so we might as well refuse the write immediately.
1911 * Unfortunately if the block size != page size, we can't as
1912 * easily detect this case using ext4_walk_page_buffers(), but
1913 * for the extremely common case, this is an optimization that
1914 * skips a useless round trip through ext4_bio_write_page().
1916 if (ext4_walk_page_buffers(NULL
, page_bufs
, 0, len
, NULL
,
1917 ext4_bh_delay_or_unwritten
)) {
1918 redirty_page_for_writepage(wbc
, page
);
1919 if ((current
->flags
& PF_MEMALLOC
) ||
1920 (inode
->i_sb
->s_blocksize
== PAGE_CACHE_SIZE
)) {
1922 * For memory cleaning there's no point in writing only
1923 * some buffers. So just bail out. Warn if we came here
1924 * from direct reclaim.
1926 WARN_ON_ONCE((current
->flags
& (PF_MEMALLOC
|PF_KSWAPD
))
1931 keep_towrite
= true;
1934 if (PageChecked(page
) && ext4_should_journal_data(inode
))
1936 * It's mmapped pagecache. Add buffers and journal it. There
1937 * doesn't seem much point in redirtying the page here.
1939 return __ext4_journalled_writepage(page
, len
);
1941 ext4_io_submit_init(&io_submit
, wbc
);
1942 io_submit
.io_end
= ext4_init_io_end(inode
, GFP_NOFS
);
1943 if (!io_submit
.io_end
) {
1944 redirty_page_for_writepage(wbc
, page
);
1948 ret
= ext4_bio_write_page(&io_submit
, page
, len
, wbc
, keep_towrite
);
1949 ext4_io_submit(&io_submit
);
1950 /* Drop io_end reference we got from init */
1951 ext4_put_io_end_defer(io_submit
.io_end
);
1955 static int mpage_submit_page(struct mpage_da_data
*mpd
, struct page
*page
)
1961 BUG_ON(page
->index
!= mpd
->first_page
);
1962 clear_page_dirty_for_io(page
);
1964 * We have to be very careful here! Nothing protects writeback path
1965 * against i_size changes and the page can be writeably mapped into
1966 * page tables. So an application can be growing i_size and writing
1967 * data through mmap while writeback runs. clear_page_dirty_for_io()
1968 * write-protects our page in page tables and the page cannot get
1969 * written to again until we release page lock. So only after
1970 * clear_page_dirty_for_io() we are safe to sample i_size for
1971 * ext4_bio_write_page() to zero-out tail of the written page. We rely
1972 * on the barrier provided by TestClearPageDirty in
1973 * clear_page_dirty_for_io() to make sure i_size is really sampled only
1974 * after page tables are updated.
1976 size
= i_size_read(mpd
->inode
);
1977 if (page
->index
== size
>> PAGE_SHIFT
)
1978 len
= size
& ~PAGE_MASK
;
1981 err
= ext4_bio_write_page(&mpd
->io_submit
, page
, len
, mpd
->wbc
, false);
1983 mpd
->wbc
->nr_to_write
--;
1989 #define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
1992 * mballoc gives us at most this number of blocks...
1993 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
1994 * The rest of mballoc seems to handle chunks up to full group size.
1996 #define MAX_WRITEPAGES_EXTENT_LEN 2048
1999 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
2001 * @mpd - extent of blocks
2002 * @lblk - logical number of the block in the file
2003 * @bh - buffer head we want to add to the extent
2005 * The function is used to collect contig. blocks in the same state. If the
2006 * buffer doesn't require mapping for writeback and we haven't started the
2007 * extent of buffers to map yet, the function returns 'true' immediately - the
2008 * caller can write the buffer right away. Otherwise the function returns true
2009 * if the block has been added to the extent, false if the block couldn't be
2012 static bool mpage_add_bh_to_extent(struct mpage_da_data
*mpd
, ext4_lblk_t lblk
,
2013 struct buffer_head
*bh
)
2015 struct ext4_map_blocks
*map
= &mpd
->map
;
2017 /* Buffer that doesn't need mapping for writeback? */
2018 if (!buffer_dirty(bh
) || !buffer_mapped(bh
) ||
2019 (!buffer_delay(bh
) && !buffer_unwritten(bh
))) {
2020 /* So far no extent to map => we write the buffer right away */
2021 if (map
->m_len
== 0)
2026 /* First block in the extent? */
2027 if (map
->m_len
== 0) {
2030 map
->m_flags
= bh
->b_state
& BH_FLAGS
;
2034 /* Don't go larger than mballoc is willing to allocate */
2035 if (map
->m_len
>= MAX_WRITEPAGES_EXTENT_LEN
)
2038 /* Can we merge the block to our big extent? */
2039 if (lblk
== map
->m_lblk
+ map
->m_len
&&
2040 (bh
->b_state
& BH_FLAGS
) == map
->m_flags
) {
2048 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
2050 * @mpd - extent of blocks for mapping
2051 * @head - the first buffer in the page
2052 * @bh - buffer we should start processing from
2053 * @lblk - logical number of the block in the file corresponding to @bh
2055 * Walk through page buffers from @bh upto @head (exclusive) and either submit
2056 * the page for IO if all buffers in this page were mapped and there's no
2057 * accumulated extent of buffers to map or add buffers in the page to the
2058 * extent of buffers to map. The function returns 1 if the caller can continue
2059 * by processing the next page, 0 if it should stop adding buffers to the
2060 * extent to map because we cannot extend it anymore. It can also return value
2061 * < 0 in case of error during IO submission.
2063 static int mpage_process_page_bufs(struct mpage_da_data
*mpd
,
2064 struct buffer_head
*head
,
2065 struct buffer_head
*bh
,
2068 struct inode
*inode
= mpd
->inode
;
2070 ext4_lblk_t blocks
= (i_size_read(inode
) + i_blocksize(inode
) - 1)
2071 >> inode
->i_blkbits
;
2074 BUG_ON(buffer_locked(bh
));
2076 if (lblk
>= blocks
|| !mpage_add_bh_to_extent(mpd
, lblk
, bh
)) {
2077 /* Found extent to map? */
2080 /* Everything mapped so far and we hit EOF */
2083 } while (lblk
++, (bh
= bh
->b_this_page
) != head
);
2084 /* So far everything mapped? Submit the page for IO. */
2085 if (mpd
->map
.m_len
== 0) {
2086 err
= mpage_submit_page(mpd
, head
->b_page
);
2090 return lblk
< blocks
;
2094 * mpage_map_buffers - update buffers corresponding to changed extent and
2095 * submit fully mapped pages for IO
2097 * @mpd - description of extent to map, on return next extent to map
2099 * Scan buffers corresponding to changed extent (we expect corresponding pages
2100 * to be already locked) and update buffer state according to new extent state.
2101 * We map delalloc buffers to their physical location, clear unwritten bits,
2102 * and mark buffers as uninit when we perform writes to unwritten extents
2103 * and do extent conversion after IO is finished. If the last page is not fully
2104 * mapped, we update @map to the next extent in the last page that needs
2105 * mapping. Otherwise we submit the page for IO.
2107 static int mpage_map_and_submit_buffers(struct mpage_da_data
*mpd
)
2109 struct pagevec pvec
;
2111 struct inode
*inode
= mpd
->inode
;
2112 struct buffer_head
*head
, *bh
;
2113 int bpp_bits
= PAGE_CACHE_SHIFT
- inode
->i_blkbits
;
2119 start
= mpd
->map
.m_lblk
>> bpp_bits
;
2120 end
= (mpd
->map
.m_lblk
+ mpd
->map
.m_len
- 1) >> bpp_bits
;
2121 lblk
= start
<< bpp_bits
;
2122 pblock
= mpd
->map
.m_pblk
;
2124 pagevec_init(&pvec
, 0);
2125 while (start
<= end
) {
2126 nr_pages
= pagevec_lookup(&pvec
, inode
->i_mapping
, start
,
2130 for (i
= 0; i
< nr_pages
; i
++) {
2131 struct page
*page
= pvec
.pages
[i
];
2133 if (page
->index
> end
)
2135 /* Up to 'end' pages must be contiguous */
2136 BUG_ON(page
->index
!= start
);
2137 bh
= head
= page_buffers(page
);
2139 if (lblk
< mpd
->map
.m_lblk
)
2141 if (lblk
>= mpd
->map
.m_lblk
+ mpd
->map
.m_len
) {
2143 * Buffer after end of mapped extent.
2144 * Find next buffer in the page to map.
2147 mpd
->map
.m_flags
= 0;
2149 * FIXME: If dioread_nolock supports
2150 * blocksize < pagesize, we need to make
2151 * sure we add size mapped so far to
2152 * io_end->size as the following call
2153 * can submit the page for IO.
2155 err
= mpage_process_page_bufs(mpd
, head
,
2157 pagevec_release(&pvec
);
2162 if (buffer_delay(bh
)) {
2163 clear_buffer_delay(bh
);
2164 bh
->b_blocknr
= pblock
++;
2166 clear_buffer_unwritten(bh
);
2167 } while (lblk
++, (bh
= bh
->b_this_page
) != head
);
2170 * FIXME: This is going to break if dioread_nolock
2171 * supports blocksize < pagesize as we will try to
2172 * convert potentially unmapped parts of inode.
2174 mpd
->io_submit
.io_end
->size
+= PAGE_CACHE_SIZE
;
2175 /* Page fully mapped - let IO run! */
2176 err
= mpage_submit_page(mpd
, page
);
2178 pagevec_release(&pvec
);
2183 pagevec_release(&pvec
);
2185 /* Extent fully mapped and matches with page boundary. We are done. */
2187 mpd
->map
.m_flags
= 0;
2191 static int mpage_map_one_extent(handle_t
*handle
, struct mpage_da_data
*mpd
)
2193 struct inode
*inode
= mpd
->inode
;
2194 struct ext4_map_blocks
*map
= &mpd
->map
;
2195 int get_blocks_flags
;
2196 int err
, dioread_nolock
;
2198 trace_ext4_da_write_pages_extent(inode
, map
);
2200 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
2201 * to convert an unwritten extent to be initialized (in the case
2202 * where we have written into one or more preallocated blocks). It is
2203 * possible that we're going to need more metadata blocks than
2204 * previously reserved. However we must not fail because we're in
2205 * writeback and there is nothing we can do about it so it might result
2206 * in data loss. So use reserved blocks to allocate metadata if
2209 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
2210 * the blocks in question are delalloc blocks. This indicates
2211 * that the blocks and quotas has already been checked when
2212 * the data was copied into the page cache.
2214 get_blocks_flags
= EXT4_GET_BLOCKS_CREATE
|
2215 EXT4_GET_BLOCKS_METADATA_NOFAIL
;
2216 dioread_nolock
= ext4_should_dioread_nolock(inode
);
2218 get_blocks_flags
|= EXT4_GET_BLOCKS_IO_CREATE_EXT
;
2219 if (map
->m_flags
& (1 << BH_Delay
))
2220 get_blocks_flags
|= EXT4_GET_BLOCKS_DELALLOC_RESERVE
;
2222 err
= ext4_map_blocks(handle
, inode
, map
, get_blocks_flags
);
2225 if (dioread_nolock
&& (map
->m_flags
& EXT4_MAP_UNWRITTEN
)) {
2226 if (!mpd
->io_submit
.io_end
->handle
&&
2227 ext4_handle_valid(handle
)) {
2228 mpd
->io_submit
.io_end
->handle
= handle
->h_rsv_handle
;
2229 handle
->h_rsv_handle
= NULL
;
2231 ext4_set_io_unwritten_flag(inode
, mpd
->io_submit
.io_end
);
2234 BUG_ON(map
->m_len
== 0);
2235 if (map
->m_flags
& EXT4_MAP_NEW
) {
2236 struct block_device
*bdev
= inode
->i_sb
->s_bdev
;
2239 for (i
= 0; i
< map
->m_len
; i
++)
2240 unmap_underlying_metadata(bdev
, map
->m_pblk
+ i
);
2246 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2247 * mpd->len and submit pages underlying it for IO
2249 * @handle - handle for journal operations
2250 * @mpd - extent to map
2251 * @give_up_on_write - we set this to true iff there is a fatal error and there
2252 * is no hope of writing the data. The caller should discard
2253 * dirty pages to avoid infinite loops.
2255 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2256 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2257 * them to initialized or split the described range from larger unwritten
2258 * extent. Note that we need not map all the described range since allocation
2259 * can return less blocks or the range is covered by more unwritten extents. We
2260 * cannot map more because we are limited by reserved transaction credits. On
2261 * the other hand we always make sure that the last touched page is fully
2262 * mapped so that it can be written out (and thus forward progress is
2263 * guaranteed). After mapping we submit all mapped pages for IO.
2265 static int mpage_map_and_submit_extent(handle_t
*handle
,
2266 struct mpage_da_data
*mpd
,
2267 bool *give_up_on_write
)
2269 struct inode
*inode
= mpd
->inode
;
2270 struct ext4_map_blocks
*map
= &mpd
->map
;
2275 mpd
->io_submit
.io_end
->offset
=
2276 ((loff_t
)map
->m_lblk
) << inode
->i_blkbits
;
2278 err
= mpage_map_one_extent(handle
, mpd
);
2280 struct super_block
*sb
= inode
->i_sb
;
2282 if (EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
)
2283 goto invalidate_dirty_pages
;
2285 * Let the uper layers retry transient errors.
2286 * In the case of ENOSPC, if ext4_count_free_blocks()
2287 * is non-zero, a commit should free up blocks.
2289 if ((err
== -ENOMEM
) ||
2290 (err
== -ENOSPC
&& ext4_count_free_clusters(sb
))) {
2292 goto update_disksize
;
2295 ext4_msg(sb
, KERN_CRIT
,
2296 "Delayed block allocation failed for "
2297 "inode %lu at logical offset %llu with"
2298 " max blocks %u with error %d",
2300 (unsigned long long)map
->m_lblk
,
2301 (unsigned)map
->m_len
, -err
);
2302 ext4_msg(sb
, KERN_CRIT
,
2303 "This should not happen!! Data will "
2306 ext4_print_free_blocks(inode
);
2307 invalidate_dirty_pages
:
2308 *give_up_on_write
= true;
2313 * Update buffer state, submit mapped pages, and get us new
2316 err
= mpage_map_and_submit_buffers(mpd
);
2318 goto update_disksize
;
2319 } while (map
->m_len
);
2323 * Update on-disk size after IO is submitted. Races with
2324 * truncate are avoided by checking i_size under i_data_sem.
2326 disksize
= ((loff_t
)mpd
->first_page
) << PAGE_CACHE_SHIFT
;
2327 if (disksize
> READ_ONCE(EXT4_I(inode
)->i_disksize
)) {
2331 down_write(&EXT4_I(inode
)->i_data_sem
);
2332 i_size
= i_size_read(inode
);
2333 if (disksize
> i_size
)
2335 if (disksize
> EXT4_I(inode
)->i_disksize
)
2336 EXT4_I(inode
)->i_disksize
= disksize
;
2337 err2
= ext4_mark_inode_dirty(handle
, inode
);
2338 up_write(&EXT4_I(inode
)->i_data_sem
);
2340 ext4_error(inode
->i_sb
,
2341 "Failed to mark inode %lu dirty",
2350 * Calculate the total number of credits to reserve for one writepages
2351 * iteration. This is called from ext4_writepages(). We map an extent of
2352 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
2353 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2354 * bpp - 1 blocks in bpp different extents.
2356 static int ext4_da_writepages_trans_blocks(struct inode
*inode
)
2358 int bpp
= ext4_journal_blocks_per_page(inode
);
2360 return ext4_meta_trans_blocks(inode
,
2361 MAX_WRITEPAGES_EXTENT_LEN
+ bpp
- 1, bpp
);
2365 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2366 * and underlying extent to map
2368 * @mpd - where to look for pages
2370 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2371 * IO immediately. When we find a page which isn't mapped we start accumulating
2372 * extent of buffers underlying these pages that needs mapping (formed by
2373 * either delayed or unwritten buffers). We also lock the pages containing
2374 * these buffers. The extent found is returned in @mpd structure (starting at
2375 * mpd->lblk with length mpd->len blocks).
2377 * Note that this function can attach bios to one io_end structure which are
2378 * neither logically nor physically contiguous. Although it may seem as an
2379 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2380 * case as we need to track IO to all buffers underlying a page in one io_end.
2382 static int mpage_prepare_extent_to_map(struct mpage_da_data
*mpd
)
2384 struct address_space
*mapping
= mpd
->inode
->i_mapping
;
2385 struct pagevec pvec
;
2386 unsigned int nr_pages
;
2387 long left
= mpd
->wbc
->nr_to_write
;
2388 pgoff_t index
= mpd
->first_page
;
2389 pgoff_t end
= mpd
->last_page
;
2392 int blkbits
= mpd
->inode
->i_blkbits
;
2394 struct buffer_head
*head
;
2396 if (mpd
->wbc
->sync_mode
== WB_SYNC_ALL
|| mpd
->wbc
->tagged_writepages
)
2397 tag
= PAGECACHE_TAG_TOWRITE
;
2399 tag
= PAGECACHE_TAG_DIRTY
;
2401 pagevec_init(&pvec
, 0);
2403 mpd
->next_page
= index
;
2404 while (index
<= end
) {
2405 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
2406 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
2410 for (i
= 0; i
< nr_pages
; i
++) {
2411 struct page
*page
= pvec
.pages
[i
];
2414 * At this point, the page may be truncated or
2415 * invalidated (changing page->mapping to NULL), or
2416 * even swizzled back from swapper_space to tmpfs file
2417 * mapping. However, page->index will not change
2418 * because we have a reference on the page.
2420 if (page
->index
> end
)
2424 * Accumulated enough dirty pages? This doesn't apply
2425 * to WB_SYNC_ALL mode. For integrity sync we have to
2426 * keep going because someone may be concurrently
2427 * dirtying pages, and we might have synced a lot of
2428 * newly appeared dirty pages, but have not synced all
2429 * of the old dirty pages.
2431 if (mpd
->wbc
->sync_mode
== WB_SYNC_NONE
&& left
<= 0)
2434 /* If we can't merge this page, we are done. */
2435 if (mpd
->map
.m_len
> 0 && mpd
->next_page
!= page
->index
)
2440 * If the page is no longer dirty, or its mapping no
2441 * longer corresponds to inode we are writing (which
2442 * means it has been truncated or invalidated), or the
2443 * page is already under writeback and we are not doing
2444 * a data integrity writeback, skip the page
2446 if (!PageDirty(page
) ||
2447 (PageWriteback(page
) &&
2448 (mpd
->wbc
->sync_mode
== WB_SYNC_NONE
)) ||
2449 unlikely(page
->mapping
!= mapping
)) {
2454 wait_on_page_writeback(page
);
2455 BUG_ON(PageWriteback(page
));
2457 if (mpd
->map
.m_len
== 0)
2458 mpd
->first_page
= page
->index
;
2459 mpd
->next_page
= page
->index
+ 1;
2460 /* Add all dirty buffers to mpd */
2461 lblk
= ((ext4_lblk_t
)page
->index
) <<
2462 (PAGE_CACHE_SHIFT
- blkbits
);
2463 head
= page_buffers(page
);
2464 err
= mpage_process_page_bufs(mpd
, head
, head
, lblk
);
2470 pagevec_release(&pvec
);
2475 pagevec_release(&pvec
);
2479 static int __writepage(struct page
*page
, struct writeback_control
*wbc
,
2482 struct address_space
*mapping
= data
;
2483 int ret
= ext4_writepage(page
, wbc
);
2484 mapping_set_error(mapping
, ret
);
2488 static int ext4_writepages(struct address_space
*mapping
,
2489 struct writeback_control
*wbc
)
2491 pgoff_t writeback_index
= 0;
2492 long nr_to_write
= wbc
->nr_to_write
;
2493 int range_whole
= 0;
2495 handle_t
*handle
= NULL
;
2496 struct mpage_da_data mpd
;
2497 struct inode
*inode
= mapping
->host
;
2498 int needed_blocks
, rsv_blocks
= 0, ret
= 0;
2499 struct ext4_sb_info
*sbi
= EXT4_SB(mapping
->host
->i_sb
);
2501 struct blk_plug plug
;
2502 bool give_up_on_write
= false;
2504 trace_ext4_writepages(inode
, wbc
);
2507 * No pages to write? This is mainly a kludge to avoid starting
2508 * a transaction for special inodes like journal inode on last iput()
2509 * because that could violate lock ordering on umount
2511 if (!mapping
->nrpages
|| !mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
2512 goto out_writepages
;
2514 if (ext4_should_journal_data(inode
)) {
2515 struct blk_plug plug
;
2517 blk_start_plug(&plug
);
2518 ret
= write_cache_pages(mapping
, wbc
, __writepage
, mapping
);
2519 blk_finish_plug(&plug
);
2520 goto out_writepages
;
2524 * If the filesystem has aborted, it is read-only, so return
2525 * right away instead of dumping stack traces later on that
2526 * will obscure the real source of the problem. We test
2527 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
2528 * the latter could be true if the filesystem is mounted
2529 * read-only, and in that case, ext4_writepages should
2530 * *never* be called, so if that ever happens, we would want
2533 if (unlikely(sbi
->s_mount_flags
& EXT4_MF_FS_ABORTED
)) {
2535 goto out_writepages
;
2538 if (ext4_should_dioread_nolock(inode
)) {
2540 * We may need to convert up to one extent per block in
2541 * the page and we may dirty the inode.
2543 rsv_blocks
= 1 + (PAGE_CACHE_SIZE
>> inode
->i_blkbits
);
2547 * If we have inline data and arrive here, it means that
2548 * we will soon create the block for the 1st page, so
2549 * we'd better clear the inline data here.
2551 if (ext4_has_inline_data(inode
)) {
2552 /* Just inode will be modified... */
2553 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
2554 if (IS_ERR(handle
)) {
2555 ret
= PTR_ERR(handle
);
2556 goto out_writepages
;
2558 BUG_ON(ext4_test_inode_state(inode
,
2559 EXT4_STATE_MAY_INLINE_DATA
));
2560 ext4_destroy_inline_data(handle
, inode
);
2561 ext4_journal_stop(handle
);
2564 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2567 if (wbc
->range_cyclic
) {
2568 writeback_index
= mapping
->writeback_index
;
2569 if (writeback_index
)
2571 mpd
.first_page
= writeback_index
;
2574 mpd
.first_page
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2575 mpd
.last_page
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2580 ext4_io_submit_init(&mpd
.io_submit
, wbc
);
2582 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2583 tag_pages_for_writeback(mapping
, mpd
.first_page
, mpd
.last_page
);
2585 blk_start_plug(&plug
);
2586 while (!done
&& mpd
.first_page
<= mpd
.last_page
) {
2587 /* For each extent of pages we use new io_end */
2588 mpd
.io_submit
.io_end
= ext4_init_io_end(inode
, GFP_KERNEL
);
2589 if (!mpd
.io_submit
.io_end
) {
2595 * We have two constraints: We find one extent to map and we
2596 * must always write out whole page (makes a difference when
2597 * blocksize < pagesize) so that we don't block on IO when we
2598 * try to write out the rest of the page. Journalled mode is
2599 * not supported by delalloc.
2601 BUG_ON(ext4_should_journal_data(inode
));
2602 needed_blocks
= ext4_da_writepages_trans_blocks(inode
);
2604 /* start a new transaction */
2605 handle
= ext4_journal_start_with_reserve(inode
,
2606 EXT4_HT_WRITE_PAGE
, needed_blocks
, rsv_blocks
);
2607 if (IS_ERR(handle
)) {
2608 ret
= PTR_ERR(handle
);
2609 ext4_msg(inode
->i_sb
, KERN_CRIT
, "%s: jbd2_start: "
2610 "%ld pages, ino %lu; err %d", __func__
,
2611 wbc
->nr_to_write
, inode
->i_ino
, ret
);
2612 /* Release allocated io_end */
2613 ext4_put_io_end(mpd
.io_submit
.io_end
);
2617 trace_ext4_da_write_pages(inode
, mpd
.first_page
, mpd
.wbc
);
2618 ret
= mpage_prepare_extent_to_map(&mpd
);
2621 ret
= mpage_map_and_submit_extent(handle
, &mpd
,
2625 * We scanned the whole range (or exhausted
2626 * nr_to_write), submitted what was mapped and
2627 * didn't find anything needing mapping. We are
2634 * Caution: If the handle is synchronous,
2635 * ext4_journal_stop() can wait for transaction commit
2636 * to finish which may depend on writeback of pages to
2637 * complete or on page lock to be released. In that
2638 * case, we have to wait until after after we have
2639 * submitted all the IO, released page locks we hold,
2640 * and dropped io_end reference (for extent conversion
2641 * to be able to complete) before stopping the handle.
2643 if (!ext4_handle_valid(handle
) || handle
->h_sync
== 0) {
2644 ext4_journal_stop(handle
);
2647 /* Submit prepared bio */
2648 ext4_io_submit(&mpd
.io_submit
);
2649 /* Unlock pages we didn't use */
2650 mpage_release_unused_pages(&mpd
, give_up_on_write
);
2652 * Drop our io_end reference we got from init. We have
2653 * to be careful and use deferred io_end finishing if
2654 * we are still holding the transaction as we can
2655 * release the last reference to io_end which may end
2656 * up doing unwritten extent conversion.
2659 ext4_put_io_end_defer(mpd
.io_submit
.io_end
);
2660 ext4_journal_stop(handle
);
2662 ext4_put_io_end(mpd
.io_submit
.io_end
);
2664 if (ret
== -ENOSPC
&& sbi
->s_journal
) {
2666 * Commit the transaction which would
2667 * free blocks released in the transaction
2670 jbd2_journal_force_commit_nested(sbi
->s_journal
);
2674 /* Fatal error - ENOMEM, EIO... */
2678 blk_finish_plug(&plug
);
2679 if (!ret
&& !cycled
&& wbc
->nr_to_write
> 0) {
2681 mpd
.last_page
= writeback_index
- 1;
2687 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
2689 * Set the writeback_index so that range_cyclic
2690 * mode will write it back later
2692 mapping
->writeback_index
= mpd
.first_page
;
2695 trace_ext4_writepages_result(inode
, wbc
, ret
,
2696 nr_to_write
- wbc
->nr_to_write
);
2700 static int ext4_nonda_switch(struct super_block
*sb
)
2702 s64 free_clusters
, dirty_clusters
;
2703 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2706 * switch to non delalloc mode if we are running low
2707 * on free block. The free block accounting via percpu
2708 * counters can get slightly wrong with percpu_counter_batch getting
2709 * accumulated on each CPU without updating global counters
2710 * Delalloc need an accurate free block accounting. So switch
2711 * to non delalloc when we are near to error range.
2714 percpu_counter_read_positive(&sbi
->s_freeclusters_counter
);
2716 percpu_counter_read_positive(&sbi
->s_dirtyclusters_counter
);
2718 * Start pushing delalloc when 1/2 of free blocks are dirty.
2720 if (dirty_clusters
&& (free_clusters
< 2 * dirty_clusters
))
2721 try_to_writeback_inodes_sb(sb
, WB_REASON_FS_FREE_SPACE
);
2723 if (2 * free_clusters
< 3 * dirty_clusters
||
2724 free_clusters
< (dirty_clusters
+ EXT4_FREECLUSTERS_WATERMARK
)) {
2726 * free block count is less than 150% of dirty blocks
2727 * or free blocks is less than watermark
2734 /* We always reserve for an inode update; the superblock could be there too */
2735 static int ext4_da_write_credits(struct inode
*inode
, loff_t pos
, unsigned len
)
2737 if (likely(ext4_has_feature_large_file(inode
->i_sb
)))
2740 if (pos
+ len
<= 0x7fffffffULL
)
2743 /* We might need to update the superblock to set LARGE_FILE */
2747 static int ext4_da_write_begin(struct file
*file
, struct address_space
*mapping
,
2748 loff_t pos
, unsigned len
, unsigned flags
,
2749 struct page
**pagep
, void **fsdata
)
2751 int ret
, retries
= 0;
2754 struct inode
*inode
= mapping
->host
;
2757 index
= pos
>> PAGE_CACHE_SHIFT
;
2759 if (ext4_nonda_switch(inode
->i_sb
)) {
2760 *fsdata
= (void *)FALL_BACK_TO_NONDELALLOC
;
2761 return ext4_write_begin(file
, mapping
, pos
,
2762 len
, flags
, pagep
, fsdata
);
2764 *fsdata
= (void *)0;
2765 trace_ext4_da_write_begin(inode
, pos
, len
, flags
);
2767 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
2768 ret
= ext4_da_write_inline_data_begin(mapping
, inode
,
2778 * grab_cache_page_write_begin() can take a long time if the
2779 * system is thrashing due to memory pressure, or if the page
2780 * is being written back. So grab it first before we start
2781 * the transaction handle. This also allows us to allocate
2782 * the page (if needed) without using GFP_NOFS.
2785 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
2791 * With delayed allocation, we don't log the i_disksize update
2792 * if there is delayed block allocation. But we still need
2793 * to journalling the i_disksize update if writes to the end
2794 * of file which has an already mapped buffer.
2797 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
2798 ext4_da_write_credits(inode
, pos
, len
));
2799 if (IS_ERR(handle
)) {
2800 page_cache_release(page
);
2801 return PTR_ERR(handle
);
2805 if (page
->mapping
!= mapping
) {
2806 /* The page got truncated from under us */
2808 page_cache_release(page
);
2809 ext4_journal_stop(handle
);
2812 /* In case writeback began while the page was unlocked */
2813 wait_for_stable_page(page
);
2815 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2816 ret
= ext4_block_write_begin(page
, pos
, len
,
2817 ext4_da_get_block_prep
);
2819 ret
= __block_write_begin(page
, pos
, len
, ext4_da_get_block_prep
);
2823 ext4_journal_stop(handle
);
2825 * block_write_begin may have instantiated a few blocks
2826 * outside i_size. Trim these off again. Don't need
2827 * i_size_read because we hold i_mutex.
2829 if (pos
+ len
> inode
->i_size
)
2830 ext4_truncate_failed_write(inode
);
2832 if (ret
== -ENOSPC
&&
2833 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
2836 page_cache_release(page
);
2845 * Check if we should update i_disksize
2846 * when write to the end of file but not require block allocation
2848 static int ext4_da_should_update_i_disksize(struct page
*page
,
2849 unsigned long offset
)
2851 struct buffer_head
*bh
;
2852 struct inode
*inode
= page
->mapping
->host
;
2856 bh
= page_buffers(page
);
2857 idx
= offset
>> inode
->i_blkbits
;
2859 for (i
= 0; i
< idx
; i
++)
2860 bh
= bh
->b_this_page
;
2862 if (!buffer_mapped(bh
) || (buffer_delay(bh
)) || buffer_unwritten(bh
))
2867 static int ext4_da_write_end(struct file
*file
,
2868 struct address_space
*mapping
,
2869 loff_t pos
, unsigned len
, unsigned copied
,
2870 struct page
*page
, void *fsdata
)
2872 struct inode
*inode
= mapping
->host
;
2874 handle_t
*handle
= ext4_journal_current_handle();
2876 unsigned long start
, end
;
2877 int write_mode
= (int)(unsigned long)fsdata
;
2879 if (write_mode
== FALL_BACK_TO_NONDELALLOC
)
2880 return ext4_write_end(file
, mapping
, pos
,
2881 len
, copied
, page
, fsdata
);
2883 trace_ext4_da_write_end(inode
, pos
, len
, copied
);
2884 start
= pos
& (PAGE_CACHE_SIZE
- 1);
2885 end
= start
+ copied
- 1;
2888 * generic_write_end() will run mark_inode_dirty() if i_size
2889 * changes. So let's piggyback the i_disksize mark_inode_dirty
2892 new_i_size
= pos
+ copied
;
2893 if (copied
&& new_i_size
> EXT4_I(inode
)->i_disksize
) {
2894 if (ext4_has_inline_data(inode
) ||
2895 ext4_da_should_update_i_disksize(page
, end
)) {
2896 ext4_update_i_disksize(inode
, new_i_size
);
2897 /* We need to mark inode dirty even if
2898 * new_i_size is less that inode->i_size
2899 * bu greater than i_disksize.(hint delalloc)
2901 ext4_mark_inode_dirty(handle
, inode
);
2905 if (write_mode
!= CONVERT_INLINE_DATA
&&
2906 ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
) &&
2907 ext4_has_inline_data(inode
))
2908 ret2
= ext4_da_write_inline_data_end(inode
, pos
, len
, copied
,
2911 ret2
= generic_write_end(file
, mapping
, pos
, len
, copied
,
2917 ret2
= ext4_journal_stop(handle
);
2921 return ret
? ret
: copied
;
2924 static void ext4_da_invalidatepage(struct page
*page
, unsigned int offset
,
2925 unsigned int length
)
2928 * Drop reserved blocks
2930 BUG_ON(!PageLocked(page
));
2931 if (!page_has_buffers(page
))
2934 ext4_da_page_release_reservation(page
, offset
, length
);
2937 ext4_invalidatepage(page
, offset
, length
);
2943 * Force all delayed allocation blocks to be allocated for a given inode.
2945 int ext4_alloc_da_blocks(struct inode
*inode
)
2947 trace_ext4_alloc_da_blocks(inode
);
2949 if (!EXT4_I(inode
)->i_reserved_data_blocks
)
2953 * We do something simple for now. The filemap_flush() will
2954 * also start triggering a write of the data blocks, which is
2955 * not strictly speaking necessary (and for users of
2956 * laptop_mode, not even desirable). However, to do otherwise
2957 * would require replicating code paths in:
2959 * ext4_writepages() ->
2960 * write_cache_pages() ---> (via passed in callback function)
2961 * __mpage_da_writepage() -->
2962 * mpage_add_bh_to_extent()
2963 * mpage_da_map_blocks()
2965 * The problem is that write_cache_pages(), located in
2966 * mm/page-writeback.c, marks pages clean in preparation for
2967 * doing I/O, which is not desirable if we're not planning on
2970 * We could call write_cache_pages(), and then redirty all of
2971 * the pages by calling redirty_page_for_writepage() but that
2972 * would be ugly in the extreme. So instead we would need to
2973 * replicate parts of the code in the above functions,
2974 * simplifying them because we wouldn't actually intend to
2975 * write out the pages, but rather only collect contiguous
2976 * logical block extents, call the multi-block allocator, and
2977 * then update the buffer heads with the block allocations.
2979 * For now, though, we'll cheat by calling filemap_flush(),
2980 * which will map the blocks, and start the I/O, but not
2981 * actually wait for the I/O to complete.
2983 return filemap_flush(inode
->i_mapping
);
2987 * bmap() is special. It gets used by applications such as lilo and by
2988 * the swapper to find the on-disk block of a specific piece of data.
2990 * Naturally, this is dangerous if the block concerned is still in the
2991 * journal. If somebody makes a swapfile on an ext4 data-journaling
2992 * filesystem and enables swap, then they may get a nasty shock when the
2993 * data getting swapped to that swapfile suddenly gets overwritten by
2994 * the original zero's written out previously to the journal and
2995 * awaiting writeback in the kernel's buffer cache.
2997 * So, if we see any bmap calls here on a modified, data-journaled file,
2998 * take extra steps to flush any blocks which might be in the cache.
3000 static sector_t
ext4_bmap(struct address_space
*mapping
, sector_t block
)
3002 struct inode
*inode
= mapping
->host
;
3007 * We can get here for an inline file via the FIBMAP ioctl
3009 if (ext4_has_inline_data(inode
))
3012 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
) &&
3013 test_opt(inode
->i_sb
, DELALLOC
)) {
3015 * With delalloc we want to sync the file
3016 * so that we can make sure we allocate
3019 filemap_write_and_wait(mapping
);
3022 if (EXT4_JOURNAL(inode
) &&
3023 ext4_test_inode_state(inode
, EXT4_STATE_JDATA
)) {
3025 * This is a REALLY heavyweight approach, but the use of
3026 * bmap on dirty files is expected to be extremely rare:
3027 * only if we run lilo or swapon on a freshly made file
3028 * do we expect this to happen.
3030 * (bmap requires CAP_SYS_RAWIO so this does not
3031 * represent an unprivileged user DOS attack --- we'd be
3032 * in trouble if mortal users could trigger this path at
3035 * NB. EXT4_STATE_JDATA is not set on files other than
3036 * regular files. If somebody wants to bmap a directory
3037 * or symlink and gets confused because the buffer
3038 * hasn't yet been flushed to disk, they deserve
3039 * everything they get.
3042 ext4_clear_inode_state(inode
, EXT4_STATE_JDATA
);
3043 journal
= EXT4_JOURNAL(inode
);
3044 jbd2_journal_lock_updates(journal
);
3045 err
= jbd2_journal_flush(journal
);
3046 jbd2_journal_unlock_updates(journal
);
3052 return generic_block_bmap(mapping
, block
, ext4_get_block
);
3055 static int ext4_readpage(struct file
*file
, struct page
*page
)
3058 struct inode
*inode
= page
->mapping
->host
;
3060 trace_ext4_readpage(page
);
3062 if (ext4_has_inline_data(inode
))
3063 ret
= ext4_readpage_inline(inode
, page
);
3066 return ext4_mpage_readpages(page
->mapping
, NULL
, page
, 1);
3072 ext4_readpages(struct file
*file
, struct address_space
*mapping
,
3073 struct list_head
*pages
, unsigned nr_pages
)
3075 struct inode
*inode
= mapping
->host
;
3077 /* If the file has inline data, no need to do readpages. */
3078 if (ext4_has_inline_data(inode
))
3081 return ext4_mpage_readpages(mapping
, pages
, NULL
, nr_pages
);
3084 static void ext4_invalidatepage(struct page
*page
, unsigned int offset
,
3085 unsigned int length
)
3087 trace_ext4_invalidatepage(page
, offset
, length
);
3089 /* No journalling happens on data buffers when this function is used */
3090 WARN_ON(page_has_buffers(page
) && buffer_jbd(page_buffers(page
)));
3092 block_invalidatepage(page
, offset
, length
);
3095 static int __ext4_journalled_invalidatepage(struct page
*page
,
3096 unsigned int offset
,
3097 unsigned int length
)
3099 journal_t
*journal
= EXT4_JOURNAL(page
->mapping
->host
);
3101 trace_ext4_journalled_invalidatepage(page
, offset
, length
);
3104 * If it's a full truncate we just forget about the pending dirtying
3106 if (offset
== 0 && length
== PAGE_CACHE_SIZE
)
3107 ClearPageChecked(page
);
3109 return jbd2_journal_invalidatepage(journal
, page
, offset
, length
);
3112 /* Wrapper for aops... */
3113 static void ext4_journalled_invalidatepage(struct page
*page
,
3114 unsigned int offset
,
3115 unsigned int length
)
3117 WARN_ON(__ext4_journalled_invalidatepage(page
, offset
, length
) < 0);
3120 static int ext4_releasepage(struct page
*page
, gfp_t wait
)
3122 journal_t
*journal
= EXT4_JOURNAL(page
->mapping
->host
);
3124 trace_ext4_releasepage(page
);
3126 /* Page has dirty journalled data -> cannot release */
3127 if (PageChecked(page
))
3130 return jbd2_journal_try_to_free_buffers(journal
, page
, wait
);
3132 return try_to_free_buffers(page
);
3136 * ext4_get_block used when preparing for a DIO write or buffer write.
3137 * We allocate an uinitialized extent if blocks haven't been allocated.
3138 * The extent will be converted to initialized after the IO is complete.
3140 int ext4_get_block_write(struct inode
*inode
, sector_t iblock
,
3141 struct buffer_head
*bh_result
, int create
)
3143 ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
3144 inode
->i_ino
, create
);
3145 return _ext4_get_block(inode
, iblock
, bh_result
,
3146 EXT4_GET_BLOCKS_IO_CREATE_EXT
);
3149 static int ext4_get_block_write_nolock(struct inode
*inode
, sector_t iblock
,
3150 struct buffer_head
*bh_result
, int create
)
3152 ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
3153 inode
->i_ino
, create
);
3154 return _ext4_get_block(inode
, iblock
, bh_result
,
3155 EXT4_GET_BLOCKS_NO_LOCK
);
3158 int ext4_get_block_dax(struct inode
*inode
, sector_t iblock
,
3159 struct buffer_head
*bh_result
, int create
)
3161 int flags
= EXT4_GET_BLOCKS_PRE_IO
| EXT4_GET_BLOCKS_UNWRIT_EXT
;
3163 flags
|= EXT4_GET_BLOCKS_CREATE
;
3164 ext4_debug("ext4_get_block_dax: inode %lu, create flag %d\n",
3165 inode
->i_ino
, create
);
3166 return _ext4_get_block(inode
, iblock
, bh_result
, flags
);
3169 static void ext4_end_io_dio(struct kiocb
*iocb
, loff_t offset
,
3170 ssize_t size
, void *private)
3172 ext4_io_end_t
*io_end
= iocb
->private;
3174 /* if not async direct IO just return */
3178 ext_debug("ext4_end_io_dio(): io_end 0x%p "
3179 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
3180 iocb
->private, io_end
->inode
->i_ino
, iocb
, offset
,
3183 iocb
->private = NULL
;
3184 io_end
->offset
= offset
;
3185 io_end
->size
= size
;
3186 ext4_put_io_end(io_end
);
3190 * For ext4 extent files, ext4 will do direct-io write to holes,
3191 * preallocated extents, and those write extend the file, no need to
3192 * fall back to buffered IO.
3194 * For holes, we fallocate those blocks, mark them as unwritten
3195 * If those blocks were preallocated, we mark sure they are split, but
3196 * still keep the range to write as unwritten.
3198 * The unwritten extents will be converted to written when DIO is completed.
3199 * For async direct IO, since the IO may still pending when return, we
3200 * set up an end_io call back function, which will do the conversion
3201 * when async direct IO completed.
3203 * If the O_DIRECT write will extend the file then add this inode to the
3204 * orphan list. So recovery will truncate it back to the original size
3205 * if the machine crashes during the write.
3208 static ssize_t
ext4_ext_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
,
3211 struct file
*file
= iocb
->ki_filp
;
3212 struct inode
*inode
= file
->f_mapping
->host
;
3214 size_t count
= iov_iter_count(iter
);
3216 get_block_t
*get_block_func
= NULL
;
3218 loff_t final_size
= offset
+ count
;
3219 ext4_io_end_t
*io_end
= NULL
;
3221 /* Use the old path for reads and writes beyond i_size. */
3222 if (iov_iter_rw(iter
) != WRITE
|| final_size
> inode
->i_size
)
3223 return ext4_ind_direct_IO(iocb
, iter
, offset
);
3225 BUG_ON(iocb
->private == NULL
);
3228 * Make all waiters for direct IO properly wait also for extent
3229 * conversion. This also disallows race between truncate() and
3230 * overwrite DIO as i_dio_count needs to be incremented under i_mutex.
3232 if (iov_iter_rw(iter
) == WRITE
)
3233 inode_dio_begin(inode
);
3235 /* If we do a overwrite dio, i_mutex locking can be released */
3236 overwrite
= *((int *)iocb
->private);
3239 down_read(&EXT4_I(inode
)->i_data_sem
);
3240 mutex_unlock(&inode
->i_mutex
);
3244 * We could direct write to holes and fallocate.
3246 * Allocated blocks to fill the hole are marked as
3247 * unwritten to prevent parallel buffered read to expose
3248 * the stale data before DIO complete the data IO.
3250 * As to previously fallocated extents, ext4 get_block will
3251 * just simply mark the buffer mapped but still keep the
3252 * extents unwritten.
3254 * For non AIO case, we will convert those unwritten extents
3255 * to written after return back from blockdev_direct_IO.
3257 * For async DIO, the conversion needs to be deferred when the
3258 * IO is completed. The ext4 end_io callback function will be
3259 * called to take care of the conversion work. Here for async
3260 * case, we allocate an io_end structure to hook to the iocb.
3262 iocb
->private = NULL
;
3264 get_block_func
= ext4_get_block_write_nolock
;
3266 ext4_inode_aio_set(inode
, NULL
);
3267 if (!is_sync_kiocb(iocb
)) {
3268 io_end
= ext4_init_io_end(inode
, GFP_NOFS
);
3274 * Grab reference for DIO. Will be dropped in
3277 iocb
->private = ext4_get_io_end(io_end
);
3279 * we save the io structure for current async direct
3280 * IO, so that later ext4_map_blocks() could flag the
3281 * io structure whether there is a unwritten extents
3282 * needs to be converted when IO is completed.
3284 ext4_inode_aio_set(inode
, io_end
);
3286 get_block_func
= ext4_get_block_write
;
3287 dio_flags
= DIO_LOCKING
;
3289 #ifdef CONFIG_EXT4_FS_ENCRYPTION
3290 BUG_ON(ext4_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
));
3293 ret
= dax_do_io(iocb
, inode
, iter
, offset
, get_block_func
,
3294 ext4_end_io_dio
, dio_flags
);
3296 ret
= __blockdev_direct_IO(iocb
, inode
,
3297 inode
->i_sb
->s_bdev
, iter
, offset
,
3299 ext4_end_io_dio
, NULL
, dio_flags
);
3302 * Put our reference to io_end. This can free the io_end structure e.g.
3303 * in sync IO case or in case of error. It can even perform extent
3304 * conversion if all bios we submitted finished before we got here.
3305 * Note that in that case iocb->private can be already set to NULL
3309 ext4_inode_aio_set(inode
, NULL
);
3310 ext4_put_io_end(io_end
);
3312 * When no IO was submitted ext4_end_io_dio() was not
3313 * called so we have to put iocb's reference.
3315 if (ret
<= 0 && ret
!= -EIOCBQUEUED
&& iocb
->private) {
3316 WARN_ON(iocb
->private != io_end
);
3317 WARN_ON(io_end
->flag
& EXT4_IO_END_UNWRITTEN
);
3318 ext4_put_io_end(io_end
);
3319 iocb
->private = NULL
;
3322 if (ret
> 0 && !overwrite
&& ext4_test_inode_state(inode
,
3323 EXT4_STATE_DIO_UNWRITTEN
)) {
3326 * for non AIO case, since the IO is already
3327 * completed, we could do the conversion right here
3329 err
= ext4_convert_unwritten_extents(NULL
, inode
,
3333 ext4_clear_inode_state(inode
, EXT4_STATE_DIO_UNWRITTEN
);
3337 if (iov_iter_rw(iter
) == WRITE
)
3338 inode_dio_end(inode
);
3339 /* take i_mutex locking again if we do a ovewrite dio */
3341 up_read(&EXT4_I(inode
)->i_data_sem
);
3342 mutex_lock(&inode
->i_mutex
);
3348 static ssize_t
ext4_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
,
3351 struct file
*file
= iocb
->ki_filp
;
3352 struct inode
*inode
= file
->f_mapping
->host
;
3353 size_t count
= iov_iter_count(iter
);
3356 if (iov_iter_rw(iter
) == READ
) {
3357 loff_t size
= i_size_read(inode
);
3363 #ifdef CONFIG_EXT4_FS_ENCRYPTION
3364 if (ext4_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
3369 * If we are doing data journalling we don't support O_DIRECT
3371 if (ext4_should_journal_data(inode
))
3374 /* Let buffer I/O handle the inline data case. */
3375 if (ext4_has_inline_data(inode
))
3378 trace_ext4_direct_IO_enter(inode
, offset
, count
, iov_iter_rw(iter
));
3379 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3380 ret
= ext4_ext_direct_IO(iocb
, iter
, offset
);
3382 ret
= ext4_ind_direct_IO(iocb
, iter
, offset
);
3383 trace_ext4_direct_IO_exit(inode
, offset
, count
, iov_iter_rw(iter
), ret
);
3388 * Pages can be marked dirty completely asynchronously from ext4's journalling
3389 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3390 * much here because ->set_page_dirty is called under VFS locks. The page is
3391 * not necessarily locked.
3393 * We cannot just dirty the page and leave attached buffers clean, because the
3394 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3395 * or jbddirty because all the journalling code will explode.
3397 * So what we do is to mark the page "pending dirty" and next time writepage
3398 * is called, propagate that into the buffers appropriately.
3400 static int ext4_journalled_set_page_dirty(struct page
*page
)
3402 SetPageChecked(page
);
3403 return __set_page_dirty_nobuffers(page
);
3406 static const struct address_space_operations ext4_aops
= {
3407 .readpage
= ext4_readpage
,
3408 .readpages
= ext4_readpages
,
3409 .writepage
= ext4_writepage
,
3410 .writepages
= ext4_writepages
,
3411 .write_begin
= ext4_write_begin
,
3412 .write_end
= ext4_write_end
,
3414 .invalidatepage
= ext4_invalidatepage
,
3415 .releasepage
= ext4_releasepage
,
3416 .direct_IO
= ext4_direct_IO
,
3417 .migratepage
= buffer_migrate_page
,
3418 .is_partially_uptodate
= block_is_partially_uptodate
,
3419 .error_remove_page
= generic_error_remove_page
,
3422 static const struct address_space_operations ext4_journalled_aops
= {
3423 .readpage
= ext4_readpage
,
3424 .readpages
= ext4_readpages
,
3425 .writepage
= ext4_writepage
,
3426 .writepages
= ext4_writepages
,
3427 .write_begin
= ext4_write_begin
,
3428 .write_end
= ext4_journalled_write_end
,
3429 .set_page_dirty
= ext4_journalled_set_page_dirty
,
3431 .invalidatepage
= ext4_journalled_invalidatepage
,
3432 .releasepage
= ext4_releasepage
,
3433 .direct_IO
= ext4_direct_IO
,
3434 .is_partially_uptodate
= block_is_partially_uptodate
,
3435 .error_remove_page
= generic_error_remove_page
,
3438 static const struct address_space_operations ext4_da_aops
= {
3439 .readpage
= ext4_readpage
,
3440 .readpages
= ext4_readpages
,
3441 .writepage
= ext4_writepage
,
3442 .writepages
= ext4_writepages
,
3443 .write_begin
= ext4_da_write_begin
,
3444 .write_end
= ext4_da_write_end
,
3446 .invalidatepage
= ext4_da_invalidatepage
,
3447 .releasepage
= ext4_releasepage
,
3448 .direct_IO
= ext4_direct_IO
,
3449 .migratepage
= buffer_migrate_page
,
3450 .is_partially_uptodate
= block_is_partially_uptodate
,
3451 .error_remove_page
= generic_error_remove_page
,
3454 void ext4_set_aops(struct inode
*inode
)
3456 switch (ext4_inode_journal_mode(inode
)) {
3457 case EXT4_INODE_ORDERED_DATA_MODE
:
3458 ext4_set_inode_state(inode
, EXT4_STATE_ORDERED_MODE
);
3460 case EXT4_INODE_WRITEBACK_DATA_MODE
:
3461 ext4_clear_inode_state(inode
, EXT4_STATE_ORDERED_MODE
);
3463 case EXT4_INODE_JOURNAL_DATA_MODE
:
3464 inode
->i_mapping
->a_ops
= &ext4_journalled_aops
;
3469 if (test_opt(inode
->i_sb
, DELALLOC
))
3470 inode
->i_mapping
->a_ops
= &ext4_da_aops
;
3472 inode
->i_mapping
->a_ops
= &ext4_aops
;
3475 static int __ext4_block_zero_page_range(handle_t
*handle
,
3476 struct address_space
*mapping
, loff_t from
, loff_t length
)
3478 ext4_fsblk_t index
= from
>> PAGE_CACHE_SHIFT
;
3479 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3480 unsigned blocksize
, pos
;
3482 struct inode
*inode
= mapping
->host
;
3483 struct buffer_head
*bh
;
3487 page
= find_or_create_page(mapping
, from
>> PAGE_CACHE_SHIFT
,
3488 mapping_gfp_constraint(mapping
, ~__GFP_FS
));
3492 blocksize
= inode
->i_sb
->s_blocksize
;
3494 iblock
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
3496 if (!page_has_buffers(page
))
3497 create_empty_buffers(page
, blocksize
, 0);
3499 /* Find the buffer that contains "offset" */
3500 bh
= page_buffers(page
);
3502 while (offset
>= pos
) {
3503 bh
= bh
->b_this_page
;
3507 if (buffer_freed(bh
)) {
3508 BUFFER_TRACE(bh
, "freed: skip");
3511 if (!buffer_mapped(bh
)) {
3512 BUFFER_TRACE(bh
, "unmapped");
3513 ext4_get_block(inode
, iblock
, bh
, 0);
3514 /* unmapped? It's a hole - nothing to do */
3515 if (!buffer_mapped(bh
)) {
3516 BUFFER_TRACE(bh
, "still unmapped");
3521 /* Ok, it's mapped. Make sure it's up-to-date */
3522 if (PageUptodate(page
))
3523 set_buffer_uptodate(bh
);
3525 if (!buffer_uptodate(bh
)) {
3527 ll_rw_block(READ
, 1, &bh
);
3529 /* Uhhuh. Read error. Complain and punt. */
3530 if (!buffer_uptodate(bh
))
3532 if (S_ISREG(inode
->i_mode
) &&
3533 ext4_encrypted_inode(inode
)) {
3534 /* We expect the key to be set. */
3535 BUG_ON(!ext4_has_encryption_key(inode
));
3536 BUG_ON(blocksize
!= PAGE_CACHE_SIZE
);
3537 WARN_ON_ONCE(ext4_decrypt(page
));
3540 if (ext4_should_journal_data(inode
)) {
3541 BUFFER_TRACE(bh
, "get write access");
3542 err
= ext4_journal_get_write_access(handle
, bh
);
3546 zero_user(page
, offset
, length
);
3547 BUFFER_TRACE(bh
, "zeroed end of block");
3549 if (ext4_should_journal_data(inode
)) {
3550 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
3553 mark_buffer_dirty(bh
);
3554 if (ext4_test_inode_state(inode
, EXT4_STATE_ORDERED_MODE
))
3555 err
= ext4_jbd2_file_inode(handle
, inode
);
3560 page_cache_release(page
);
3565 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3566 * starting from file offset 'from'. The range to be zero'd must
3567 * be contained with in one block. If the specified range exceeds
3568 * the end of the block it will be shortened to end of the block
3569 * that cooresponds to 'from'
3571 static int ext4_block_zero_page_range(handle_t
*handle
,
3572 struct address_space
*mapping
, loff_t from
, loff_t length
)
3574 struct inode
*inode
= mapping
->host
;
3575 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3576 unsigned blocksize
= inode
->i_sb
->s_blocksize
;
3577 unsigned max
= blocksize
- (offset
& (blocksize
- 1));
3580 * correct length if it does not fall between
3581 * 'from' and the end of the block
3583 if (length
> max
|| length
< 0)
3587 return dax_zero_page_range(inode
, from
, length
, ext4_get_block
);
3588 return __ext4_block_zero_page_range(handle
, mapping
, from
, length
);
3592 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3593 * up to the end of the block which corresponds to `from'.
3594 * This required during truncate. We need to physically zero the tail end
3595 * of that block so it doesn't yield old data if the file is later grown.
3597 static int ext4_block_truncate_page(handle_t
*handle
,
3598 struct address_space
*mapping
, loff_t from
)
3600 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3603 struct inode
*inode
= mapping
->host
;
3605 /* If we are processing an encrypted inode during orphan list handling */
3606 if (ext4_encrypted_inode(inode
) && !ext4_has_encryption_key(inode
))
3609 blocksize
= inode
->i_sb
->s_blocksize
;
3610 length
= blocksize
- (offset
& (blocksize
- 1));
3612 return ext4_block_zero_page_range(handle
, mapping
, from
, length
);
3615 int ext4_zero_partial_blocks(handle_t
*handle
, struct inode
*inode
,
3616 loff_t lstart
, loff_t length
)
3618 struct super_block
*sb
= inode
->i_sb
;
3619 struct address_space
*mapping
= inode
->i_mapping
;
3620 unsigned partial_start
, partial_end
;
3621 ext4_fsblk_t start
, end
;
3622 loff_t byte_end
= (lstart
+ length
- 1);
3625 partial_start
= lstart
& (sb
->s_blocksize
- 1);
3626 partial_end
= byte_end
& (sb
->s_blocksize
- 1);
3628 start
= lstart
>> sb
->s_blocksize_bits
;
3629 end
= byte_end
>> sb
->s_blocksize_bits
;
3631 /* Handle partial zero within the single block */
3633 (partial_start
|| (partial_end
!= sb
->s_blocksize
- 1))) {
3634 err
= ext4_block_zero_page_range(handle
, mapping
,
3638 /* Handle partial zero out on the start of the range */
3639 if (partial_start
) {
3640 err
= ext4_block_zero_page_range(handle
, mapping
,
3641 lstart
, sb
->s_blocksize
);
3645 /* Handle partial zero out on the end of the range */
3646 if (partial_end
!= sb
->s_blocksize
- 1)
3647 err
= ext4_block_zero_page_range(handle
, mapping
,
3648 byte_end
- partial_end
,
3653 int ext4_can_truncate(struct inode
*inode
)
3655 if (S_ISREG(inode
->i_mode
))
3657 if (S_ISDIR(inode
->i_mode
))
3659 if (S_ISLNK(inode
->i_mode
))
3660 return !ext4_inode_is_fast_symlink(inode
);
3665 * We have to make sure i_disksize gets properly updated before we truncate
3666 * page cache due to hole punching or zero range. Otherwise i_disksize update
3667 * can get lost as it may have been postponed to submission of writeback but
3668 * that will never happen after we truncate page cache.
3670 int ext4_update_disksize_before_punch(struct inode
*inode
, loff_t offset
,
3674 loff_t size
= i_size_read(inode
);
3676 WARN_ON(!mutex_is_locked(&inode
->i_mutex
));
3677 if (offset
> size
|| offset
+ len
< size
)
3680 if (EXT4_I(inode
)->i_disksize
>= size
)
3683 handle
= ext4_journal_start(inode
, EXT4_HT_MISC
, 1);
3685 return PTR_ERR(handle
);
3686 ext4_update_i_disksize(inode
, size
);
3687 ext4_mark_inode_dirty(handle
, inode
);
3688 ext4_journal_stop(handle
);
3694 * ext4_punch_hole: punches a hole in a file by releasing the blocks
3695 * associated with the given offset and length
3697 * @inode: File inode
3698 * @offset: The offset where the hole will begin
3699 * @len: The length of the hole
3701 * Returns: 0 on success or negative on failure
3704 int ext4_punch_hole(struct inode
*inode
, loff_t offset
, loff_t length
)
3706 struct super_block
*sb
= inode
->i_sb
;
3707 ext4_lblk_t first_block
, stop_block
;
3708 struct address_space
*mapping
= inode
->i_mapping
;
3709 loff_t first_block_offset
, last_block_offset
;
3711 unsigned int credits
;
3714 if (!S_ISREG(inode
->i_mode
))
3717 trace_ext4_punch_hole(inode
, offset
, length
, 0);
3719 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
3720 if (ext4_has_inline_data(inode
)) {
3721 down_write(&EXT4_I(inode
)->i_mmap_sem
);
3722 ret
= ext4_convert_inline_data(inode
);
3723 up_write(&EXT4_I(inode
)->i_mmap_sem
);
3729 * Write out all dirty pages to avoid race conditions
3730 * Then release them.
3732 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
)) {
3733 ret
= filemap_write_and_wait_range(mapping
, offset
,
3734 offset
+ length
- 1);
3739 mutex_lock(&inode
->i_mutex
);
3741 /* No need to punch hole beyond i_size */
3742 if (offset
>= inode
->i_size
)
3746 * If the hole extends beyond i_size, set the hole
3747 * to end after the page that contains i_size
3749 if (offset
+ length
> inode
->i_size
) {
3750 length
= inode
->i_size
+
3751 PAGE_CACHE_SIZE
- (inode
->i_size
& (PAGE_CACHE_SIZE
- 1)) -
3755 if (offset
& (sb
->s_blocksize
- 1) ||
3756 (offset
+ length
) & (sb
->s_blocksize
- 1)) {
3758 * Attach jinode to inode for jbd2 if we do any zeroing of
3761 ret
= ext4_inode_attach_jinode(inode
);
3767 /* Wait all existing dio workers, newcomers will block on i_mutex */
3768 ext4_inode_block_unlocked_dio(inode
);
3769 inode_dio_wait(inode
);
3772 * Prevent page faults from reinstantiating pages we have released from
3775 down_write(&EXT4_I(inode
)->i_mmap_sem
);
3776 first_block_offset
= round_up(offset
, sb
->s_blocksize
);
3777 last_block_offset
= round_down((offset
+ length
), sb
->s_blocksize
) - 1;
3779 /* Now release the pages and zero block aligned part of pages*/
3780 if (last_block_offset
> first_block_offset
) {
3781 ret
= ext4_update_disksize_before_punch(inode
, offset
, length
);
3784 truncate_pagecache_range(inode
, first_block_offset
,
3788 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3789 credits
= ext4_writepage_trans_blocks(inode
);
3791 credits
= ext4_blocks_for_truncate(inode
);
3792 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
3793 if (IS_ERR(handle
)) {
3794 ret
= PTR_ERR(handle
);
3795 ext4_std_error(sb
, ret
);
3799 ret
= ext4_zero_partial_blocks(handle
, inode
, offset
,
3804 first_block
= (offset
+ sb
->s_blocksize
- 1) >>
3805 EXT4_BLOCK_SIZE_BITS(sb
);
3806 stop_block
= (offset
+ length
) >> EXT4_BLOCK_SIZE_BITS(sb
);
3808 /* If there are blocks to remove, do it */
3809 if (stop_block
> first_block
) {
3811 down_write(&EXT4_I(inode
)->i_data_sem
);
3812 ext4_discard_preallocations(inode
);
3814 ret
= ext4_es_remove_extent(inode
, first_block
,
3815 stop_block
- first_block
);
3817 up_write(&EXT4_I(inode
)->i_data_sem
);
3821 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3822 ret
= ext4_ext_remove_space(inode
, first_block
,
3825 ret
= ext4_ind_remove_space(handle
, inode
, first_block
,
3828 up_write(&EXT4_I(inode
)->i_data_sem
);
3831 ext4_handle_sync(handle
);
3833 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3834 ext4_mark_inode_dirty(handle
, inode
);
3836 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3838 ext4_journal_stop(handle
);
3840 up_write(&EXT4_I(inode
)->i_mmap_sem
);
3841 ext4_inode_resume_unlocked_dio(inode
);
3843 mutex_unlock(&inode
->i_mutex
);
3847 int ext4_inode_attach_jinode(struct inode
*inode
)
3849 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3850 struct jbd2_inode
*jinode
;
3852 if (ei
->jinode
|| !EXT4_SB(inode
->i_sb
)->s_journal
)
3855 jinode
= jbd2_alloc_inode(GFP_KERNEL
);
3856 spin_lock(&inode
->i_lock
);
3859 spin_unlock(&inode
->i_lock
);
3862 ei
->jinode
= jinode
;
3863 jbd2_journal_init_jbd_inode(ei
->jinode
, inode
);
3866 spin_unlock(&inode
->i_lock
);
3867 if (unlikely(jinode
!= NULL
))
3868 jbd2_free_inode(jinode
);
3875 * We block out ext4_get_block() block instantiations across the entire
3876 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
3877 * simultaneously on behalf of the same inode.
3879 * As we work through the truncate and commit bits of it to the journal there
3880 * is one core, guiding principle: the file's tree must always be consistent on
3881 * disk. We must be able to restart the truncate after a crash.
3883 * The file's tree may be transiently inconsistent in memory (although it
3884 * probably isn't), but whenever we close off and commit a journal transaction,
3885 * the contents of (the filesystem + the journal) must be consistent and
3886 * restartable. It's pretty simple, really: bottom up, right to left (although
3887 * left-to-right works OK too).
3889 * Note that at recovery time, journal replay occurs *before* the restart of
3890 * truncate against the orphan inode list.
3892 * The committed inode has the new, desired i_size (which is the same as
3893 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
3894 * that this inode's truncate did not complete and it will again call
3895 * ext4_truncate() to have another go. So there will be instantiated blocks
3896 * to the right of the truncation point in a crashed ext4 filesystem. But
3897 * that's fine - as long as they are linked from the inode, the post-crash
3898 * ext4_truncate() run will find them and release them.
3900 void ext4_truncate(struct inode
*inode
)
3902 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3903 unsigned int credits
;
3905 struct address_space
*mapping
= inode
->i_mapping
;
3908 * There is a possibility that we're either freeing the inode
3909 * or it's a completely new inode. In those cases we might not
3910 * have i_mutex locked because it's not necessary.
3912 if (!(inode
->i_state
& (I_NEW
|I_FREEING
)))
3913 WARN_ON(!mutex_is_locked(&inode
->i_mutex
));
3914 trace_ext4_truncate_enter(inode
);
3916 if (!ext4_can_truncate(inode
))
3919 ext4_clear_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3921 if (inode
->i_size
== 0 && !test_opt(inode
->i_sb
, NO_AUTO_DA_ALLOC
))
3922 ext4_set_inode_state(inode
, EXT4_STATE_DA_ALLOC_CLOSE
);
3924 if (ext4_has_inline_data(inode
)) {
3927 ext4_inline_data_truncate(inode
, &has_inline
);
3932 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
3933 if (inode
->i_size
& (inode
->i_sb
->s_blocksize
- 1)) {
3934 if (ext4_inode_attach_jinode(inode
) < 0)
3938 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3939 credits
= ext4_writepage_trans_blocks(inode
);
3941 credits
= ext4_blocks_for_truncate(inode
);
3943 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
3944 if (IS_ERR(handle
)) {
3945 ext4_std_error(inode
->i_sb
, PTR_ERR(handle
));
3949 if (inode
->i_size
& (inode
->i_sb
->s_blocksize
- 1))
3950 ext4_block_truncate_page(handle
, mapping
, inode
->i_size
);
3953 * We add the inode to the orphan list, so that if this
3954 * truncate spans multiple transactions, and we crash, we will
3955 * resume the truncate when the filesystem recovers. It also
3956 * marks the inode dirty, to catch the new size.
3958 * Implication: the file must always be in a sane, consistent
3959 * truncatable state while each transaction commits.
3961 if (ext4_orphan_add(handle
, inode
))
3964 down_write(&EXT4_I(inode
)->i_data_sem
);
3966 ext4_discard_preallocations(inode
);
3968 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3969 ext4_ext_truncate(handle
, inode
);
3971 ext4_ind_truncate(handle
, inode
);
3973 up_write(&ei
->i_data_sem
);
3976 ext4_handle_sync(handle
);
3980 * If this was a simple ftruncate() and the file will remain alive,
3981 * then we need to clear up the orphan record which we created above.
3982 * However, if this was a real unlink then we were called by
3983 * ext4_evict_inode(), and we allow that function to clean up the
3984 * orphan info for us.
3987 ext4_orphan_del(handle
, inode
);
3989 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3990 ext4_mark_inode_dirty(handle
, inode
);
3991 ext4_journal_stop(handle
);
3993 trace_ext4_truncate_exit(inode
);
3997 * ext4_get_inode_loc returns with an extra refcount against the inode's
3998 * underlying buffer_head on success. If 'in_mem' is true, we have all
3999 * data in memory that is needed to recreate the on-disk version of this
4002 static int __ext4_get_inode_loc(struct inode
*inode
,
4003 struct ext4_iloc
*iloc
, int in_mem
)
4005 struct ext4_group_desc
*gdp
;
4006 struct buffer_head
*bh
;
4007 struct super_block
*sb
= inode
->i_sb
;
4009 int inodes_per_block
, inode_offset
;
4012 if (inode
->i_ino
< EXT4_ROOT_INO
||
4013 inode
->i_ino
> le32_to_cpu(EXT4_SB(sb
)->s_es
->s_inodes_count
))
4014 return -EFSCORRUPTED
;
4016 iloc
->block_group
= (inode
->i_ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
4017 gdp
= ext4_get_group_desc(sb
, iloc
->block_group
, NULL
);
4022 * Figure out the offset within the block group inode table
4024 inodes_per_block
= EXT4_SB(sb
)->s_inodes_per_block
;
4025 inode_offset
= ((inode
->i_ino
- 1) %
4026 EXT4_INODES_PER_GROUP(sb
));
4027 block
= ext4_inode_table(sb
, gdp
) + (inode_offset
/ inodes_per_block
);
4028 iloc
->offset
= (inode_offset
% inodes_per_block
) * EXT4_INODE_SIZE(sb
);
4030 bh
= sb_getblk(sb
, block
);
4033 if (!buffer_uptodate(bh
)) {
4037 * If the buffer has the write error flag, we have failed
4038 * to write out another inode in the same block. In this
4039 * case, we don't have to read the block because we may
4040 * read the old inode data successfully.
4042 if (buffer_write_io_error(bh
) && !buffer_uptodate(bh
))
4043 set_buffer_uptodate(bh
);
4045 if (buffer_uptodate(bh
)) {
4046 /* someone brought it uptodate while we waited */
4052 * If we have all information of the inode in memory and this
4053 * is the only valid inode in the block, we need not read the
4057 struct buffer_head
*bitmap_bh
;
4060 start
= inode_offset
& ~(inodes_per_block
- 1);
4062 /* Is the inode bitmap in cache? */
4063 bitmap_bh
= sb_getblk(sb
, ext4_inode_bitmap(sb
, gdp
));
4064 if (unlikely(!bitmap_bh
))
4068 * If the inode bitmap isn't in cache then the
4069 * optimisation may end up performing two reads instead
4070 * of one, so skip it.
4072 if (!buffer_uptodate(bitmap_bh
)) {
4076 for (i
= start
; i
< start
+ inodes_per_block
; i
++) {
4077 if (i
== inode_offset
)
4079 if (ext4_test_bit(i
, bitmap_bh
->b_data
))
4083 if (i
== start
+ inodes_per_block
) {
4084 /* all other inodes are free, so skip I/O */
4085 memset(bh
->b_data
, 0, bh
->b_size
);
4086 set_buffer_uptodate(bh
);
4094 * If we need to do any I/O, try to pre-readahead extra
4095 * blocks from the inode table.
4097 if (EXT4_SB(sb
)->s_inode_readahead_blks
) {
4098 ext4_fsblk_t b
, end
, table
;
4100 __u32 ra_blks
= EXT4_SB(sb
)->s_inode_readahead_blks
;
4102 table
= ext4_inode_table(sb
, gdp
);
4103 /* s_inode_readahead_blks is always a power of 2 */
4104 b
= block
& ~((ext4_fsblk_t
) ra_blks
- 1);
4108 num
= EXT4_INODES_PER_GROUP(sb
);
4109 if (ext4_has_group_desc_csum(sb
))
4110 num
-= ext4_itable_unused_count(sb
, gdp
);
4111 table
+= num
/ inodes_per_block
;
4115 sb_breadahead(sb
, b
++);
4119 * There are other valid inodes in the buffer, this inode
4120 * has in-inode xattrs, or we don't have this inode in memory.
4121 * Read the block from disk.
4123 trace_ext4_load_inode(inode
);
4125 bh
->b_end_io
= end_buffer_read_sync
;
4126 submit_bh(READ
| REQ_META
| REQ_PRIO
, bh
);
4128 if (!buffer_uptodate(bh
)) {
4129 EXT4_ERROR_INODE_BLOCK(inode
, block
,
4130 "unable to read itable block");
4140 int ext4_get_inode_loc(struct inode
*inode
, struct ext4_iloc
*iloc
)
4142 /* We have all inode data except xattrs in memory here. */
4143 return __ext4_get_inode_loc(inode
, iloc
,
4144 !ext4_test_inode_state(inode
, EXT4_STATE_XATTR
));
4147 void ext4_set_inode_flags(struct inode
*inode
)
4149 unsigned int flags
= EXT4_I(inode
)->i_flags
;
4150 unsigned int new_fl
= 0;
4152 if (flags
& EXT4_SYNC_FL
)
4154 if (flags
& EXT4_APPEND_FL
)
4156 if (flags
& EXT4_IMMUTABLE_FL
)
4157 new_fl
|= S_IMMUTABLE
;
4158 if (flags
& EXT4_NOATIME_FL
)
4159 new_fl
|= S_NOATIME
;
4160 if (flags
& EXT4_DIRSYNC_FL
)
4161 new_fl
|= S_DIRSYNC
;
4162 if (test_opt(inode
->i_sb
, DAX
))
4164 inode_set_flags(inode
, new_fl
,
4165 S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
|S_DAX
);
4168 /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4169 void ext4_get_inode_flags(struct ext4_inode_info
*ei
)
4171 unsigned int vfs_fl
;
4172 unsigned long old_fl
, new_fl
;
4175 vfs_fl
= ei
->vfs_inode
.i_flags
;
4176 old_fl
= ei
->i_flags
;
4177 new_fl
= old_fl
& ~(EXT4_SYNC_FL
|EXT4_APPEND_FL
|
4178 EXT4_IMMUTABLE_FL
|EXT4_NOATIME_FL
|
4180 if (vfs_fl
& S_SYNC
)
4181 new_fl
|= EXT4_SYNC_FL
;
4182 if (vfs_fl
& S_APPEND
)
4183 new_fl
|= EXT4_APPEND_FL
;
4184 if (vfs_fl
& S_IMMUTABLE
)
4185 new_fl
|= EXT4_IMMUTABLE_FL
;
4186 if (vfs_fl
& S_NOATIME
)
4187 new_fl
|= EXT4_NOATIME_FL
;
4188 if (vfs_fl
& S_DIRSYNC
)
4189 new_fl
|= EXT4_DIRSYNC_FL
;
4190 } while (cmpxchg(&ei
->i_flags
, old_fl
, new_fl
) != old_fl
);
4193 static blkcnt_t
ext4_inode_blocks(struct ext4_inode
*raw_inode
,
4194 struct ext4_inode_info
*ei
)
4197 struct inode
*inode
= &(ei
->vfs_inode
);
4198 struct super_block
*sb
= inode
->i_sb
;
4200 if (ext4_has_feature_huge_file(sb
)) {
4201 /* we are using combined 48 bit field */
4202 i_blocks
= ((u64
)le16_to_cpu(raw_inode
->i_blocks_high
)) << 32 |
4203 le32_to_cpu(raw_inode
->i_blocks_lo
);
4204 if (ext4_test_inode_flag(inode
, EXT4_INODE_HUGE_FILE
)) {
4205 /* i_blocks represent file system block size */
4206 return i_blocks
<< (inode
->i_blkbits
- 9);
4211 return le32_to_cpu(raw_inode
->i_blocks_lo
);
4215 static inline void ext4_iget_extra_inode(struct inode
*inode
,
4216 struct ext4_inode
*raw_inode
,
4217 struct ext4_inode_info
*ei
)
4219 __le32
*magic
= (void *)raw_inode
+
4220 EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
;
4221 if (*magic
== cpu_to_le32(EXT4_XATTR_MAGIC
)) {
4222 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
4223 ext4_find_inline_data_nolock(inode
);
4225 EXT4_I(inode
)->i_inline_off
= 0;
4228 struct inode
*__ext4_iget(struct super_block
*sb
, unsigned long ino
,
4229 ext4_iget_flags flags
, const char *function
,
4232 struct ext4_iloc iloc
;
4233 struct ext4_inode
*raw_inode
;
4234 struct ext4_inode_info
*ei
;
4235 struct inode
*inode
;
4236 journal_t
*journal
= EXT4_SB(sb
)->s_journal
;
4243 if ((!(flags
& EXT4_IGET_SPECIAL
) &&
4244 (ino
< EXT4_FIRST_INO(sb
) && ino
!= EXT4_ROOT_INO
)) ||
4245 (ino
< EXT4_ROOT_INO
) ||
4246 (ino
> le32_to_cpu(EXT4_SB(sb
)->s_es
->s_inodes_count
))) {
4247 if (flags
& EXT4_IGET_HANDLE
)
4248 return ERR_PTR(-ESTALE
);
4249 __ext4_error(sb
, function
, line
,
4250 "inode #%lu: comm %s: iget: illegal inode #",
4251 ino
, current
->comm
);
4252 return ERR_PTR(-EFSCORRUPTED
);
4255 inode
= iget_locked(sb
, ino
);
4257 return ERR_PTR(-ENOMEM
);
4258 if (!(inode
->i_state
& I_NEW
))
4264 ret
= __ext4_get_inode_loc(inode
, &iloc
, 0);
4267 raw_inode
= ext4_raw_inode(&iloc
);
4269 if ((ino
== EXT4_ROOT_INO
) && (raw_inode
->i_links_count
== 0)) {
4270 ext4_error_inode(inode
, function
, line
, 0,
4271 "iget: root inode unallocated");
4272 ret
= -EFSCORRUPTED
;
4276 if ((flags
& EXT4_IGET_HANDLE
) &&
4277 (raw_inode
->i_links_count
== 0) && (raw_inode
->i_mode
== 0)) {
4282 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4283 ei
->i_extra_isize
= le16_to_cpu(raw_inode
->i_extra_isize
);
4284 if (EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
>
4285 EXT4_INODE_SIZE(inode
->i_sb
)) {
4286 EXT4_ERROR_INODE(inode
, "bad extra_isize (%u != %u)",
4287 EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
,
4288 EXT4_INODE_SIZE(inode
->i_sb
));
4289 ret
= -EFSCORRUPTED
;
4293 ei
->i_extra_isize
= 0;
4295 /* Precompute checksum seed for inode metadata */
4296 if (ext4_has_metadata_csum(sb
)) {
4297 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
4299 __le32 inum
= cpu_to_le32(inode
->i_ino
);
4300 __le32 gen
= raw_inode
->i_generation
;
4301 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&inum
,
4303 ei
->i_csum_seed
= ext4_chksum(sbi
, csum
, (__u8
*)&gen
,
4307 if (!ext4_inode_csum_verify(inode
, raw_inode
, ei
)) {
4308 ext4_error_inode(inode
, function
, line
, 0,
4309 "iget: checksum invalid");
4314 inode
->i_mode
= le16_to_cpu(raw_inode
->i_mode
);
4315 i_uid
= (uid_t
)le16_to_cpu(raw_inode
->i_uid_low
);
4316 i_gid
= (gid_t
)le16_to_cpu(raw_inode
->i_gid_low
);
4317 if (!(test_opt(inode
->i_sb
, NO_UID32
))) {
4318 i_uid
|= le16_to_cpu(raw_inode
->i_uid_high
) << 16;
4319 i_gid
|= le16_to_cpu(raw_inode
->i_gid_high
) << 16;
4321 i_uid_write(inode
, i_uid
);
4322 i_gid_write(inode
, i_gid
);
4323 set_nlink(inode
, le16_to_cpu(raw_inode
->i_links_count
));
4325 ext4_clear_state_flags(ei
); /* Only relevant on 32-bit archs */
4326 ei
->i_inline_off
= 0;
4327 ei
->i_dir_start_lookup
= 0;
4328 ei
->i_dtime
= le32_to_cpu(raw_inode
->i_dtime
);
4329 /* We now have enough fields to check if the inode was active or not.
4330 * This is needed because nfsd might try to access dead inodes
4331 * the test is that same one that e2fsck uses
4332 * NeilBrown 1999oct15
4334 if (inode
->i_nlink
== 0) {
4335 if ((inode
->i_mode
== 0 ||
4336 !(EXT4_SB(inode
->i_sb
)->s_mount_state
& EXT4_ORPHAN_FS
)) &&
4337 ino
!= EXT4_BOOT_LOADER_INO
) {
4338 /* this inode is deleted */
4342 /* The only unlinked inodes we let through here have
4343 * valid i_mode and are being read by the orphan
4344 * recovery code: that's fine, we're about to complete
4345 * the process of deleting those.
4346 * OR it is the EXT4_BOOT_LOADER_INO which is
4347 * not initialized on a new filesystem. */
4349 ei
->i_flags
= le32_to_cpu(raw_inode
->i_flags
);
4350 inode
->i_blocks
= ext4_inode_blocks(raw_inode
, ei
);
4351 ei
->i_file_acl
= le32_to_cpu(raw_inode
->i_file_acl_lo
);
4352 if (ext4_has_feature_64bit(sb
))
4354 ((__u64
)le16_to_cpu(raw_inode
->i_file_acl_high
)) << 32;
4355 inode
->i_size
= ext4_isize(raw_inode
);
4356 if ((size
= i_size_read(inode
)) < 0) {
4357 ext4_error_inode(inode
, function
, line
, 0,
4358 "iget: bad i_size value: %lld", size
);
4359 ret
= -EFSCORRUPTED
;
4363 * If dir_index is not enabled but there's dir with INDEX flag set,
4364 * we'd normally treat htree data as empty space. But with metadata
4365 * checksumming that corrupts checksums so forbid that.
4367 if (!ext4_has_feature_dir_index(sb
) && ext4_has_metadata_csum(sb
) &&
4368 ext4_test_inode_flag(inode
, EXT4_INODE_INDEX
)) {
4369 EXT4_ERROR_INODE(inode
,
4370 "iget: Dir with htree data on filesystem without dir_index feature.");
4371 ret
= -EFSCORRUPTED
;
4374 ei
->i_disksize
= inode
->i_size
;
4376 ei
->i_reserved_quota
= 0;
4378 inode
->i_generation
= le32_to_cpu(raw_inode
->i_generation
);
4379 ei
->i_block_group
= iloc
.block_group
;
4380 ei
->i_last_alloc_group
= ~0;
4382 * NOTE! The in-memory inode i_data array is in little-endian order
4383 * even on big-endian machines: we do NOT byteswap the block numbers!
4385 for (block
= 0; block
< EXT4_N_BLOCKS
; block
++)
4386 ei
->i_data
[block
] = raw_inode
->i_block
[block
];
4387 INIT_LIST_HEAD(&ei
->i_orphan
);
4390 * Set transaction id's of transactions that have to be committed
4391 * to finish f[data]sync. We set them to currently running transaction
4392 * as we cannot be sure that the inode or some of its metadata isn't
4393 * part of the transaction - the inode could have been reclaimed and
4394 * now it is reread from disk.
4397 transaction_t
*transaction
;
4400 read_lock(&journal
->j_state_lock
);
4401 if (journal
->j_running_transaction
)
4402 transaction
= journal
->j_running_transaction
;
4404 transaction
= journal
->j_committing_transaction
;
4406 tid
= transaction
->t_tid
;
4408 tid
= journal
->j_commit_sequence
;
4409 read_unlock(&journal
->j_state_lock
);
4410 ei
->i_sync_tid
= tid
;
4411 ei
->i_datasync_tid
= tid
;
4414 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4415 if (ei
->i_extra_isize
== 0) {
4416 /* The extra space is currently unused. Use it. */
4417 ei
->i_extra_isize
= sizeof(struct ext4_inode
) -
4418 EXT4_GOOD_OLD_INODE_SIZE
;
4420 ext4_iget_extra_inode(inode
, raw_inode
, ei
);
4424 EXT4_INODE_GET_XTIME(i_ctime
, inode
, raw_inode
);
4425 EXT4_INODE_GET_XTIME(i_mtime
, inode
, raw_inode
);
4426 EXT4_INODE_GET_XTIME(i_atime
, inode
, raw_inode
);
4427 EXT4_EINODE_GET_XTIME(i_crtime
, ei
, raw_inode
);
4429 if (likely(!test_opt2(inode
->i_sb
, HURD_COMPAT
))) {
4430 inode
->i_version
= le32_to_cpu(raw_inode
->i_disk_version
);
4431 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4432 if (EXT4_FITS_IN_INODE(raw_inode
, ei
, i_version_hi
))
4434 (__u64
)(le32_to_cpu(raw_inode
->i_version_hi
)) << 32;
4439 if (ei
->i_file_acl
&&
4440 !ext4_data_block_valid(EXT4_SB(sb
), ei
->i_file_acl
, 1)) {
4441 ext4_error_inode(inode
, function
, line
, 0,
4442 "iget: bad extended attribute block %llu",
4444 ret
= -EFSCORRUPTED
;
4446 } else if (!ext4_has_inline_data(inode
)) {
4447 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
4448 if ((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
4449 (S_ISLNK(inode
->i_mode
) &&
4450 !ext4_inode_is_fast_symlink(inode
))))
4451 /* Validate extent which is part of inode */
4452 ret
= ext4_ext_check_inode(inode
);
4453 } else if (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
4454 (S_ISLNK(inode
->i_mode
) &&
4455 !ext4_inode_is_fast_symlink(inode
))) {
4456 /* Validate block references which are part of inode */
4457 ret
= ext4_ind_check_inode(inode
);
4463 if (S_ISREG(inode
->i_mode
)) {
4464 inode
->i_op
= &ext4_file_inode_operations
;
4465 inode
->i_fop
= &ext4_file_operations
;
4466 ext4_set_aops(inode
);
4467 } else if (S_ISDIR(inode
->i_mode
)) {
4468 inode
->i_op
= &ext4_dir_inode_operations
;
4469 inode
->i_fop
= &ext4_dir_operations
;
4470 } else if (S_ISLNK(inode
->i_mode
)) {
4471 if (ext4_encrypted_inode(inode
)) {
4472 inode
->i_op
= &ext4_encrypted_symlink_inode_operations
;
4473 ext4_set_aops(inode
);
4474 } else if (ext4_inode_is_fast_symlink(inode
)) {
4475 inode
->i_link
= (char *)ei
->i_data
;
4476 inode
->i_op
= &ext4_fast_symlink_inode_operations
;
4477 nd_terminate_link(ei
->i_data
, inode
->i_size
,
4478 sizeof(ei
->i_data
) - 1);
4480 inode
->i_op
= &ext4_symlink_inode_operations
;
4481 ext4_set_aops(inode
);
4483 inode_nohighmem(inode
);
4484 } else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
) ||
4485 S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
4486 inode
->i_op
= &ext4_special_inode_operations
;
4487 if (raw_inode
->i_block
[0])
4488 init_special_inode(inode
, inode
->i_mode
,
4489 old_decode_dev(le32_to_cpu(raw_inode
->i_block
[0])));
4491 init_special_inode(inode
, inode
->i_mode
,
4492 new_decode_dev(le32_to_cpu(raw_inode
->i_block
[1])));
4493 } else if (ino
== EXT4_BOOT_LOADER_INO
) {
4494 make_bad_inode(inode
);
4496 ret
= -EFSCORRUPTED
;
4497 ext4_error_inode(inode
, function
, line
, 0,
4498 "iget: bogus i_mode (%o)", inode
->i_mode
);
4502 ext4_set_inode_flags(inode
);
4503 unlock_new_inode(inode
);
4509 return ERR_PTR(ret
);
4512 static int ext4_inode_blocks_set(handle_t
*handle
,
4513 struct ext4_inode
*raw_inode
,
4514 struct ext4_inode_info
*ei
)
4516 struct inode
*inode
= &(ei
->vfs_inode
);
4517 u64 i_blocks
= READ_ONCE(inode
->i_blocks
);
4518 struct super_block
*sb
= inode
->i_sb
;
4520 if (i_blocks
<= ~0U) {
4522 * i_blocks can be represented in a 32 bit variable
4523 * as multiple of 512 bytes
4525 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4526 raw_inode
->i_blocks_high
= 0;
4527 ext4_clear_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4530 if (!ext4_has_feature_huge_file(sb
))
4533 if (i_blocks
<= 0xffffffffffffULL
) {
4535 * i_blocks can be represented in a 48 bit variable
4536 * as multiple of 512 bytes
4538 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4539 raw_inode
->i_blocks_high
= cpu_to_le16(i_blocks
>> 32);
4540 ext4_clear_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4542 ext4_set_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4543 /* i_block is stored in file system block size */
4544 i_blocks
= i_blocks
>> (inode
->i_blkbits
- 9);
4545 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4546 raw_inode
->i_blocks_high
= cpu_to_le16(i_blocks
>> 32);
4551 struct other_inode
{
4552 unsigned long orig_ino
;
4553 struct ext4_inode
*raw_inode
;
4556 static int other_inode_match(struct inode
* inode
, unsigned long ino
,
4559 struct other_inode
*oi
= (struct other_inode
*) data
;
4561 if ((inode
->i_ino
!= ino
) ||
4562 (inode
->i_state
& (I_FREEING
| I_WILL_FREE
| I_NEW
|
4563 I_DIRTY_SYNC
| I_DIRTY_DATASYNC
)) ||
4564 ((inode
->i_state
& I_DIRTY_TIME
) == 0))
4566 spin_lock(&inode
->i_lock
);
4567 if (((inode
->i_state
& (I_FREEING
| I_WILL_FREE
| I_NEW
|
4568 I_DIRTY_SYNC
| I_DIRTY_DATASYNC
)) == 0) &&
4569 (inode
->i_state
& I_DIRTY_TIME
)) {
4570 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4572 inode
->i_state
&= ~(I_DIRTY_TIME
| I_DIRTY_TIME_EXPIRED
);
4573 spin_unlock(&inode
->i_lock
);
4575 spin_lock(&ei
->i_raw_lock
);
4576 EXT4_INODE_SET_XTIME(i_ctime
, inode
, oi
->raw_inode
);
4577 EXT4_INODE_SET_XTIME(i_mtime
, inode
, oi
->raw_inode
);
4578 EXT4_INODE_SET_XTIME(i_atime
, inode
, oi
->raw_inode
);
4579 ext4_inode_csum_set(inode
, oi
->raw_inode
, ei
);
4580 spin_unlock(&ei
->i_raw_lock
);
4581 trace_ext4_other_inode_update_time(inode
, oi
->orig_ino
);
4584 spin_unlock(&inode
->i_lock
);
4589 * Opportunistically update the other time fields for other inodes in
4590 * the same inode table block.
4592 static void ext4_update_other_inodes_time(struct super_block
*sb
,
4593 unsigned long orig_ino
, char *buf
)
4595 struct other_inode oi
;
4597 int i
, inodes_per_block
= EXT4_SB(sb
)->s_inodes_per_block
;
4598 int inode_size
= EXT4_INODE_SIZE(sb
);
4600 oi
.orig_ino
= orig_ino
;
4602 * Calculate the first inode in the inode table block. Inode
4603 * numbers are one-based. That is, the first inode in a block
4604 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
4606 ino
= ((orig_ino
- 1) & ~(inodes_per_block
- 1)) + 1;
4607 for (i
= 0; i
< inodes_per_block
; i
++, ino
++, buf
+= inode_size
) {
4608 if (ino
== orig_ino
)
4610 oi
.raw_inode
= (struct ext4_inode
*) buf
;
4611 (void) find_inode_nowait(sb
, ino
, other_inode_match
, &oi
);
4616 * Post the struct inode info into an on-disk inode location in the
4617 * buffer-cache. This gobbles the caller's reference to the
4618 * buffer_head in the inode location struct.
4620 * The caller must have write access to iloc->bh.
4622 static int ext4_do_update_inode(handle_t
*handle
,
4623 struct inode
*inode
,
4624 struct ext4_iloc
*iloc
)
4626 struct ext4_inode
*raw_inode
= ext4_raw_inode(iloc
);
4627 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4628 struct buffer_head
*bh
= iloc
->bh
;
4629 struct super_block
*sb
= inode
->i_sb
;
4630 int err
= 0, rc
, block
;
4631 int need_datasync
= 0, set_large_file
= 0;
4635 spin_lock(&ei
->i_raw_lock
);
4637 /* For fields not tracked in the in-memory inode,
4638 * initialise them to zero for new inodes. */
4639 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
))
4640 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
4642 ext4_get_inode_flags(ei
);
4643 raw_inode
->i_mode
= cpu_to_le16(inode
->i_mode
);
4644 i_uid
= i_uid_read(inode
);
4645 i_gid
= i_gid_read(inode
);
4646 if (!(test_opt(inode
->i_sb
, NO_UID32
))) {
4647 raw_inode
->i_uid_low
= cpu_to_le16(low_16_bits(i_uid
));
4648 raw_inode
->i_gid_low
= cpu_to_le16(low_16_bits(i_gid
));
4650 * Fix up interoperability with old kernels. Otherwise, old inodes get
4651 * re-used with the upper 16 bits of the uid/gid intact
4653 if (ei
->i_dtime
&& list_empty(&ei
->i_orphan
)) {
4654 raw_inode
->i_uid_high
= 0;
4655 raw_inode
->i_gid_high
= 0;
4657 raw_inode
->i_uid_high
=
4658 cpu_to_le16(high_16_bits(i_uid
));
4659 raw_inode
->i_gid_high
=
4660 cpu_to_le16(high_16_bits(i_gid
));
4663 raw_inode
->i_uid_low
= cpu_to_le16(fs_high2lowuid(i_uid
));
4664 raw_inode
->i_gid_low
= cpu_to_le16(fs_high2lowgid(i_gid
));
4665 raw_inode
->i_uid_high
= 0;
4666 raw_inode
->i_gid_high
= 0;
4668 raw_inode
->i_links_count
= cpu_to_le16(inode
->i_nlink
);
4670 EXT4_INODE_SET_XTIME(i_ctime
, inode
, raw_inode
);
4671 EXT4_INODE_SET_XTIME(i_mtime
, inode
, raw_inode
);
4672 EXT4_INODE_SET_XTIME(i_atime
, inode
, raw_inode
);
4673 EXT4_EINODE_SET_XTIME(i_crtime
, ei
, raw_inode
);
4675 err
= ext4_inode_blocks_set(handle
, raw_inode
, ei
);
4677 spin_unlock(&ei
->i_raw_lock
);
4680 raw_inode
->i_dtime
= cpu_to_le32(ei
->i_dtime
);
4681 raw_inode
->i_flags
= cpu_to_le32(ei
->i_flags
& 0xFFFFFFFF);
4682 if (likely(!test_opt2(inode
->i_sb
, HURD_COMPAT
)))
4683 raw_inode
->i_file_acl_high
=
4684 cpu_to_le16(ei
->i_file_acl
>> 32);
4685 raw_inode
->i_file_acl_lo
= cpu_to_le32(ei
->i_file_acl
);
4686 if (ei
->i_disksize
!= ext4_isize(raw_inode
)) {
4687 ext4_isize_set(raw_inode
, ei
->i_disksize
);
4690 if (ei
->i_disksize
> 0x7fffffffULL
) {
4691 if (!ext4_has_feature_large_file(sb
) ||
4692 EXT4_SB(sb
)->s_es
->s_rev_level
==
4693 cpu_to_le32(EXT4_GOOD_OLD_REV
))
4696 raw_inode
->i_generation
= cpu_to_le32(inode
->i_generation
);
4697 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) {
4698 if (old_valid_dev(inode
->i_rdev
)) {
4699 raw_inode
->i_block
[0] =
4700 cpu_to_le32(old_encode_dev(inode
->i_rdev
));
4701 raw_inode
->i_block
[1] = 0;
4703 raw_inode
->i_block
[0] = 0;
4704 raw_inode
->i_block
[1] =
4705 cpu_to_le32(new_encode_dev(inode
->i_rdev
));
4706 raw_inode
->i_block
[2] = 0;
4708 } else if (!ext4_has_inline_data(inode
)) {
4709 for (block
= 0; block
< EXT4_N_BLOCKS
; block
++)
4710 raw_inode
->i_block
[block
] = ei
->i_data
[block
];
4713 if (likely(!test_opt2(inode
->i_sb
, HURD_COMPAT
))) {
4714 raw_inode
->i_disk_version
= cpu_to_le32(inode
->i_version
);
4715 if (ei
->i_extra_isize
) {
4716 if (EXT4_FITS_IN_INODE(raw_inode
, ei
, i_version_hi
))
4717 raw_inode
->i_version_hi
=
4718 cpu_to_le32(inode
->i_version
>> 32);
4719 raw_inode
->i_extra_isize
=
4720 cpu_to_le16(ei
->i_extra_isize
);
4723 ext4_inode_csum_set(inode
, raw_inode
, ei
);
4724 spin_unlock(&ei
->i_raw_lock
);
4725 if (inode
->i_sb
->s_flags
& MS_LAZYTIME
)
4726 ext4_update_other_inodes_time(inode
->i_sb
, inode
->i_ino
,
4729 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
4730 rc
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
4733 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
4734 if (set_large_file
) {
4735 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get write access");
4736 err
= ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
);
4739 ext4_update_dynamic_rev(sb
);
4740 ext4_set_feature_large_file(sb
);
4741 ext4_handle_sync(handle
);
4742 err
= ext4_handle_dirty_super(handle
, sb
);
4744 ext4_update_inode_fsync_trans(handle
, inode
, need_datasync
);
4747 ext4_std_error(inode
->i_sb
, err
);
4752 * ext4_write_inode()
4754 * We are called from a few places:
4756 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
4757 * Here, there will be no transaction running. We wait for any running
4758 * transaction to commit.
4760 * - Within flush work (sys_sync(), kupdate and such).
4761 * We wait on commit, if told to.
4763 * - Within iput_final() -> write_inode_now()
4764 * We wait on commit, if told to.
4766 * In all cases it is actually safe for us to return without doing anything,
4767 * because the inode has been copied into a raw inode buffer in
4768 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
4771 * Note that we are absolutely dependent upon all inode dirtiers doing the
4772 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4773 * which we are interested.
4775 * It would be a bug for them to not do this. The code:
4777 * mark_inode_dirty(inode)
4779 * inode->i_size = expr;
4781 * is in error because write_inode() could occur while `stuff()' is running,
4782 * and the new i_size will be lost. Plus the inode will no longer be on the
4783 * superblock's dirty inode list.
4785 int ext4_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4789 if (WARN_ON_ONCE(current
->flags
& PF_MEMALLOC
))
4792 if (EXT4_SB(inode
->i_sb
)->s_journal
) {
4793 if (ext4_journal_current_handle()) {
4794 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4800 * No need to force transaction in WB_SYNC_NONE mode. Also
4801 * ext4_sync_fs() will force the commit after everything is
4804 if (wbc
->sync_mode
!= WB_SYNC_ALL
|| wbc
->for_sync
)
4807 err
= ext4_force_commit(inode
->i_sb
);
4809 struct ext4_iloc iloc
;
4811 err
= __ext4_get_inode_loc(inode
, &iloc
, 0);
4815 * sync(2) will flush the whole buffer cache. No need to do
4816 * it here separately for each inode.
4818 if (wbc
->sync_mode
== WB_SYNC_ALL
&& !wbc
->for_sync
)
4819 sync_dirty_buffer(iloc
.bh
);
4820 if (buffer_req(iloc
.bh
) && !buffer_uptodate(iloc
.bh
)) {
4821 EXT4_ERROR_INODE_BLOCK(inode
, iloc
.bh
->b_blocknr
,
4822 "IO error syncing inode");
4831 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
4832 * buffers that are attached to a page stradding i_size and are undergoing
4833 * commit. In that case we have to wait for commit to finish and try again.
4835 static void ext4_wait_for_tail_page_commit(struct inode
*inode
)
4839 journal_t
*journal
= EXT4_SB(inode
->i_sb
)->s_journal
;
4840 tid_t commit_tid
= 0;
4843 offset
= inode
->i_size
& (PAGE_CACHE_SIZE
- 1);
4845 * All buffers in the last page remain valid? Then there's nothing to
4846 * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
4849 if (offset
> PAGE_CACHE_SIZE
- (1 << inode
->i_blkbits
))
4852 page
= find_lock_page(inode
->i_mapping
,
4853 inode
->i_size
>> PAGE_CACHE_SHIFT
);
4856 ret
= __ext4_journalled_invalidatepage(page
, offset
,
4857 PAGE_CACHE_SIZE
- offset
);
4859 page_cache_release(page
);
4863 read_lock(&journal
->j_state_lock
);
4864 if (journal
->j_committing_transaction
)
4865 commit_tid
= journal
->j_committing_transaction
->t_tid
;
4866 read_unlock(&journal
->j_state_lock
);
4868 jbd2_log_wait_commit(journal
, commit_tid
);
4875 * Called from notify_change.
4877 * We want to trap VFS attempts to truncate the file as soon as
4878 * possible. In particular, we want to make sure that when the VFS
4879 * shrinks i_size, we put the inode on the orphan list and modify
4880 * i_disksize immediately, so that during the subsequent flushing of
4881 * dirty pages and freeing of disk blocks, we can guarantee that any
4882 * commit will leave the blocks being flushed in an unused state on
4883 * disk. (On recovery, the inode will get truncated and the blocks will
4884 * be freed, so we have a strong guarantee that no future commit will
4885 * leave these blocks visible to the user.)
4887 * Another thing we have to assure is that if we are in ordered mode
4888 * and inode is still attached to the committing transaction, we must
4889 * we start writeout of all the dirty pages which are being truncated.
4890 * This way we are sure that all the data written in the previous
4891 * transaction are already on disk (truncate waits for pages under
4894 * Called with inode->i_mutex down.
4896 int ext4_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4898 struct inode
*inode
= d_inode(dentry
);
4901 const unsigned int ia_valid
= attr
->ia_valid
;
4903 error
= inode_change_ok(inode
, attr
);
4907 if (is_quota_modification(inode
, attr
)) {
4908 error
= dquot_initialize(inode
);
4912 if ((ia_valid
& ATTR_UID
&& !uid_eq(attr
->ia_uid
, inode
->i_uid
)) ||
4913 (ia_valid
& ATTR_GID
&& !gid_eq(attr
->ia_gid
, inode
->i_gid
))) {
4916 /* (user+group)*(old+new) structure, inode write (sb,
4917 * inode block, ? - but truncate inode update has it) */
4918 handle
= ext4_journal_start(inode
, EXT4_HT_QUOTA
,
4919 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode
->i_sb
) +
4920 EXT4_MAXQUOTAS_DEL_BLOCKS(inode
->i_sb
)) + 3);
4921 if (IS_ERR(handle
)) {
4922 error
= PTR_ERR(handle
);
4925 error
= dquot_transfer(inode
, attr
);
4927 ext4_journal_stop(handle
);
4930 /* Update corresponding info in inode so that everything is in
4931 * one transaction */
4932 if (attr
->ia_valid
& ATTR_UID
)
4933 inode
->i_uid
= attr
->ia_uid
;
4934 if (attr
->ia_valid
& ATTR_GID
)
4935 inode
->i_gid
= attr
->ia_gid
;
4936 error
= ext4_mark_inode_dirty(handle
, inode
);
4937 ext4_journal_stop(handle
);
4940 if (attr
->ia_valid
& ATTR_SIZE
) {
4942 loff_t oldsize
= inode
->i_size
;
4943 int shrink
= (attr
->ia_size
<= inode
->i_size
);
4945 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))) {
4946 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
4948 if (attr
->ia_size
> sbi
->s_bitmap_maxbytes
)
4951 if (!S_ISREG(inode
->i_mode
))
4954 if (IS_I_VERSION(inode
) && attr
->ia_size
!= inode
->i_size
)
4955 inode_inc_iversion(inode
);
4957 if (ext4_should_order_data(inode
) &&
4958 (attr
->ia_size
< inode
->i_size
)) {
4959 error
= ext4_begin_ordered_truncate(inode
,
4964 if (attr
->ia_size
!= inode
->i_size
) {
4965 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 3);
4966 if (IS_ERR(handle
)) {
4967 error
= PTR_ERR(handle
);
4970 if (ext4_handle_valid(handle
) && shrink
) {
4971 error
= ext4_orphan_add(handle
, inode
);
4975 * Update c/mtime on truncate up, ext4_truncate() will
4976 * update c/mtime in shrink case below
4979 inode
->i_mtime
= ext4_current_time(inode
);
4980 inode
->i_ctime
= inode
->i_mtime
;
4982 down_write(&EXT4_I(inode
)->i_data_sem
);
4983 EXT4_I(inode
)->i_disksize
= attr
->ia_size
;
4984 rc
= ext4_mark_inode_dirty(handle
, inode
);
4988 * We have to update i_size under i_data_sem together
4989 * with i_disksize to avoid races with writeback code
4990 * running ext4_wb_update_i_disksize().
4993 i_size_write(inode
, attr
->ia_size
);
4994 up_write(&EXT4_I(inode
)->i_data_sem
);
4995 ext4_journal_stop(handle
);
4997 if (orphan
&& inode
->i_nlink
)
4998 ext4_orphan_del(NULL
, inode
);
5003 pagecache_isize_extended(inode
, oldsize
, inode
->i_size
);
5006 * Blocks are going to be removed from the inode. Wait
5007 * for dio in flight. Temporarily disable
5008 * dioread_nolock to prevent livelock.
5011 if (!ext4_should_journal_data(inode
)) {
5012 ext4_inode_block_unlocked_dio(inode
);
5013 inode_dio_wait(inode
);
5014 ext4_inode_resume_unlocked_dio(inode
);
5016 ext4_wait_for_tail_page_commit(inode
);
5018 down_write(&EXT4_I(inode
)->i_mmap_sem
);
5020 * Truncate pagecache after we've waited for commit
5021 * in data=journal mode to make pages freeable.
5023 truncate_pagecache(inode
, inode
->i_size
);
5025 ext4_truncate(inode
);
5026 up_write(&EXT4_I(inode
)->i_mmap_sem
);
5030 setattr_copy(inode
, attr
);
5031 mark_inode_dirty(inode
);
5035 * If the call to ext4_truncate failed to get a transaction handle at
5036 * all, we need to clean up the in-core orphan list manually.
5038 if (orphan
&& inode
->i_nlink
)
5039 ext4_orphan_del(NULL
, inode
);
5041 if (!rc
&& (ia_valid
& ATTR_MODE
))
5042 rc
= posix_acl_chmod(inode
, inode
->i_mode
);
5045 ext4_std_error(inode
->i_sb
, error
);
5051 int ext4_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
5054 struct inode
*inode
;
5055 unsigned long long delalloc_blocks
;
5057 inode
= d_inode(dentry
);
5058 generic_fillattr(inode
, stat
);
5061 * If there is inline data in the inode, the inode will normally not
5062 * have data blocks allocated (it may have an external xattr block).
5063 * Report at least one sector for such files, so tools like tar, rsync,
5064 * others doen't incorrectly think the file is completely sparse.
5066 if (unlikely(ext4_has_inline_data(inode
)))
5067 stat
->blocks
+= (stat
->size
+ 511) >> 9;
5070 * We can't update i_blocks if the block allocation is delayed
5071 * otherwise in the case of system crash before the real block
5072 * allocation is done, we will have i_blocks inconsistent with
5073 * on-disk file blocks.
5074 * We always keep i_blocks updated together with real
5075 * allocation. But to not confuse with user, stat
5076 * will return the blocks that include the delayed allocation
5077 * blocks for this file.
5079 delalloc_blocks
= EXT4_C2B(EXT4_SB(inode
->i_sb
),
5080 EXT4_I(inode
)->i_reserved_data_blocks
);
5081 stat
->blocks
+= delalloc_blocks
<< (inode
->i_sb
->s_blocksize_bits
- 9);
5085 static int ext4_index_trans_blocks(struct inode
*inode
, int lblocks
,
5088 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
5089 return ext4_ind_trans_blocks(inode
, lblocks
);
5090 return ext4_ext_index_trans_blocks(inode
, pextents
);
5094 * Account for index blocks, block groups bitmaps and block group
5095 * descriptor blocks if modify datablocks and index blocks
5096 * worse case, the indexs blocks spread over different block groups
5098 * If datablocks are discontiguous, they are possible to spread over
5099 * different block groups too. If they are contiguous, with flexbg,
5100 * they could still across block group boundary.
5102 * Also account for superblock, inode, quota and xattr blocks
5104 static int ext4_meta_trans_blocks(struct inode
*inode
, int lblocks
,
5107 ext4_group_t groups
, ngroups
= ext4_get_groups_count(inode
->i_sb
);
5113 * How many index blocks need to touch to map @lblocks logical blocks
5114 * to @pextents physical extents?
5116 idxblocks
= ext4_index_trans_blocks(inode
, lblocks
, pextents
);
5121 * Now let's see how many group bitmaps and group descriptors need
5124 groups
= idxblocks
+ pextents
;
5126 if (groups
> ngroups
)
5128 if (groups
> EXT4_SB(inode
->i_sb
)->s_gdb_count
)
5129 gdpblocks
= EXT4_SB(inode
->i_sb
)->s_gdb_count
;
5131 /* bitmaps and block group descriptor blocks */
5132 ret
+= groups
+ gdpblocks
;
5134 /* Blocks for super block, inode, quota and xattr blocks */
5135 ret
+= EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
5141 * Calculate the total number of credits to reserve to fit
5142 * the modification of a single pages into a single transaction,
5143 * which may include multiple chunks of block allocations.
5145 * This could be called via ext4_write_begin()
5147 * We need to consider the worse case, when
5148 * one new block per extent.
5150 int ext4_writepage_trans_blocks(struct inode
*inode
)
5152 int bpp
= ext4_journal_blocks_per_page(inode
);
5155 ret
= ext4_meta_trans_blocks(inode
, bpp
, bpp
);
5157 /* Account for data blocks for journalled mode */
5158 if (ext4_should_journal_data(inode
))
5164 * Calculate the journal credits for a chunk of data modification.
5166 * This is called from DIO, fallocate or whoever calling
5167 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
5169 * journal buffers for data blocks are not included here, as DIO
5170 * and fallocate do no need to journal data buffers.
5172 int ext4_chunk_trans_blocks(struct inode
*inode
, int nrblocks
)
5174 return ext4_meta_trans_blocks(inode
, nrblocks
, 1);
5178 * The caller must have previously called ext4_reserve_inode_write().
5179 * Give this, we know that the caller already has write access to iloc->bh.
5181 int ext4_mark_iloc_dirty(handle_t
*handle
,
5182 struct inode
*inode
, struct ext4_iloc
*iloc
)
5186 if (IS_I_VERSION(inode
))
5187 inode_inc_iversion(inode
);
5189 /* the do_update_inode consumes one bh->b_count */
5192 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
5193 err
= ext4_do_update_inode(handle
, inode
, iloc
);
5199 * On success, We end up with an outstanding reference count against
5200 * iloc->bh. This _must_ be cleaned up later.
5204 ext4_reserve_inode_write(handle_t
*handle
, struct inode
*inode
,
5205 struct ext4_iloc
*iloc
)
5209 err
= ext4_get_inode_loc(inode
, iloc
);
5211 BUFFER_TRACE(iloc
->bh
, "get_write_access");
5212 err
= ext4_journal_get_write_access(handle
, iloc
->bh
);
5218 ext4_std_error(inode
->i_sb
, err
);
5223 * Expand an inode by new_extra_isize bytes.
5224 * Returns 0 on success or negative error number on failure.
5226 static int ext4_expand_extra_isize(struct inode
*inode
,
5227 unsigned int new_extra_isize
,
5228 struct ext4_iloc iloc
,
5231 struct ext4_inode
*raw_inode
;
5232 struct ext4_xattr_ibody_header
*header
;
5233 unsigned int inode_size
= EXT4_INODE_SIZE(inode
->i_sb
);
5234 struct ext4_inode_info
*ei
= EXT4_I(inode
);
5236 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
)
5239 /* this was checked at iget time, but double check for good measure */
5240 if ((EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
> inode_size
) ||
5241 (ei
->i_extra_isize
& 3)) {
5242 EXT4_ERROR_INODE(inode
, "bad extra_isize %u (inode size %u)",
5244 EXT4_INODE_SIZE(inode
->i_sb
));
5245 return -EFSCORRUPTED
;
5247 if ((new_extra_isize
< ei
->i_extra_isize
) ||
5248 (new_extra_isize
< 4) ||
5249 (new_extra_isize
> inode_size
- EXT4_GOOD_OLD_INODE_SIZE
))
5250 return -EINVAL
; /* Should never happen */
5252 raw_inode
= ext4_raw_inode(&iloc
);
5254 header
= IHDR(inode
, raw_inode
);
5256 /* No extended attributes present */
5257 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
) ||
5258 header
->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
)) {
5259 memset((void *)raw_inode
+ EXT4_GOOD_OLD_INODE_SIZE
+
5260 EXT4_I(inode
)->i_extra_isize
, 0,
5261 new_extra_isize
- EXT4_I(inode
)->i_extra_isize
);
5262 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
5266 /* try to expand with EAs present */
5267 return ext4_expand_extra_isize_ea(inode
, new_extra_isize
,
5272 * What we do here is to mark the in-core inode as clean with respect to inode
5273 * dirtiness (it may still be data-dirty).
5274 * This means that the in-core inode may be reaped by prune_icache
5275 * without having to perform any I/O. This is a very good thing,
5276 * because *any* task may call prune_icache - even ones which
5277 * have a transaction open against a different journal.
5279 * Is this cheating? Not really. Sure, we haven't written the
5280 * inode out, but prune_icache isn't a user-visible syncing function.
5281 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5282 * we start and wait on commits.
5284 int ext4_mark_inode_dirty(handle_t
*handle
, struct inode
*inode
)
5286 struct ext4_iloc iloc
;
5287 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
5288 static unsigned int mnt_count
;
5292 trace_ext4_mark_inode_dirty(inode
, _RET_IP_
);
5293 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
5296 if (ext4_handle_valid(handle
) &&
5297 EXT4_I(inode
)->i_extra_isize
< sbi
->s_want_extra_isize
&&
5298 !ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
)) {
5300 * We need extra buffer credits since we may write into EA block
5301 * with this same handle. If journal_extend fails, then it will
5302 * only result in a minor loss of functionality for that inode.
5303 * If this is felt to be critical, then e2fsck should be run to
5304 * force a large enough s_min_extra_isize.
5306 if ((jbd2_journal_extend(handle
,
5307 EXT4_DATA_TRANS_BLOCKS(inode
->i_sb
))) == 0) {
5308 ret
= ext4_expand_extra_isize(inode
,
5309 sbi
->s_want_extra_isize
,
5313 le16_to_cpu(sbi
->s_es
->s_mnt_count
)) {
5314 ext4_warning(inode
->i_sb
,
5315 "Unable to expand inode %lu. Delete"
5316 " some EAs or run e2fsck.",
5319 le16_to_cpu(sbi
->s_es
->s_mnt_count
);
5324 return ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
5328 * ext4_dirty_inode() is called from __mark_inode_dirty()
5330 * We're really interested in the case where a file is being extended.
5331 * i_size has been changed by generic_commit_write() and we thus need
5332 * to include the updated inode in the current transaction.
5334 * Also, dquot_alloc_block() will always dirty the inode when blocks
5335 * are allocated to the file.
5337 * If the inode is marked synchronous, we don't honour that here - doing
5338 * so would cause a commit on atime updates, which we don't bother doing.
5339 * We handle synchronous inodes at the highest possible level.
5341 * If only the I_DIRTY_TIME flag is set, we can skip everything. If
5342 * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
5343 * to copy into the on-disk inode structure are the timestamp files.
5345 void ext4_dirty_inode(struct inode
*inode
, int flags
)
5349 if (flags
== I_DIRTY_TIME
)
5351 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 2);
5355 ext4_mark_inode_dirty(handle
, inode
);
5357 ext4_journal_stop(handle
);
5364 * Bind an inode's backing buffer_head into this transaction, to prevent
5365 * it from being flushed to disk early. Unlike
5366 * ext4_reserve_inode_write, this leaves behind no bh reference and
5367 * returns no iloc structure, so the caller needs to repeat the iloc
5368 * lookup to mark the inode dirty later.
5370 static int ext4_pin_inode(handle_t
*handle
, struct inode
*inode
)
5372 struct ext4_iloc iloc
;
5376 err
= ext4_get_inode_loc(inode
, &iloc
);
5378 BUFFER_TRACE(iloc
.bh
, "get_write_access");
5379 err
= jbd2_journal_get_write_access(handle
, iloc
.bh
);
5381 err
= ext4_handle_dirty_metadata(handle
,
5387 ext4_std_error(inode
->i_sb
, err
);
5392 int ext4_change_inode_journal_flag(struct inode
*inode
, int val
)
5399 * We have to be very careful here: changing a data block's
5400 * journaling status dynamically is dangerous. If we write a
5401 * data block to the journal, change the status and then delete
5402 * that block, we risk forgetting to revoke the old log record
5403 * from the journal and so a subsequent replay can corrupt data.
5404 * So, first we make sure that the journal is empty and that
5405 * nobody is changing anything.
5408 journal
= EXT4_JOURNAL(inode
);
5411 if (is_journal_aborted(journal
))
5413 /* We have to allocate physical blocks for delalloc blocks
5414 * before flushing journal. otherwise delalloc blocks can not
5415 * be allocated any more. even more truncate on delalloc blocks
5416 * could trigger BUG by flushing delalloc blocks in journal.
5417 * There is no delalloc block in non-journal data mode.
5419 if (val
&& test_opt(inode
->i_sb
, DELALLOC
)) {
5420 err
= ext4_alloc_da_blocks(inode
);
5425 /* Wait for all existing dio workers */
5426 ext4_inode_block_unlocked_dio(inode
);
5427 inode_dio_wait(inode
);
5429 jbd2_journal_lock_updates(journal
);
5432 * OK, there are no updates running now, and all cached data is
5433 * synced to disk. We are now in a completely consistent state
5434 * which doesn't have anything in the journal, and we know that
5435 * no filesystem updates are running, so it is safe to modify
5436 * the inode's in-core data-journaling state flag now.
5440 ext4_set_inode_flag(inode
, EXT4_INODE_JOURNAL_DATA
);
5442 err
= jbd2_journal_flush(journal
);
5444 jbd2_journal_unlock_updates(journal
);
5445 ext4_inode_resume_unlocked_dio(inode
);
5448 ext4_clear_inode_flag(inode
, EXT4_INODE_JOURNAL_DATA
);
5450 ext4_set_aops(inode
);
5452 jbd2_journal_unlock_updates(journal
);
5453 ext4_inode_resume_unlocked_dio(inode
);
5455 /* Finally we can mark the inode as dirty. */
5457 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
5459 return PTR_ERR(handle
);
5461 err
= ext4_mark_inode_dirty(handle
, inode
);
5462 ext4_handle_sync(handle
);
5463 ext4_journal_stop(handle
);
5464 ext4_std_error(inode
->i_sb
, err
);
5469 static int ext4_bh_unmapped(handle_t
*handle
, struct buffer_head
*bh
)
5471 return !buffer_mapped(bh
);
5474 int ext4_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
5476 struct page
*page
= vmf
->page
;
5480 struct file
*file
= vma
->vm_file
;
5481 struct inode
*inode
= file_inode(file
);
5482 struct address_space
*mapping
= inode
->i_mapping
;
5484 get_block_t
*get_block
;
5487 sb_start_pagefault(inode
->i_sb
);
5488 file_update_time(vma
->vm_file
);
5490 down_read(&EXT4_I(inode
)->i_mmap_sem
);
5492 ret
= ext4_convert_inline_data(inode
);
5496 /* Delalloc case is easy... */
5497 if (test_opt(inode
->i_sb
, DELALLOC
) &&
5498 !ext4_should_journal_data(inode
) &&
5499 !ext4_nonda_switch(inode
->i_sb
)) {
5501 ret
= block_page_mkwrite(vma
, vmf
,
5502 ext4_da_get_block_prep
);
5503 } while (ret
== -ENOSPC
&&
5504 ext4_should_retry_alloc(inode
->i_sb
, &retries
));
5509 size
= i_size_read(inode
);
5510 /* Page got truncated from under us? */
5511 if (page
->mapping
!= mapping
|| page_offset(page
) > size
) {
5513 ret
= VM_FAULT_NOPAGE
;
5517 if (page
->index
== size
>> PAGE_CACHE_SHIFT
)
5518 len
= size
& ~PAGE_CACHE_MASK
;
5520 len
= PAGE_CACHE_SIZE
;
5522 * Return if we have all the buffers mapped. This avoids the need to do
5523 * journal_start/journal_stop which can block and take a long time
5525 if (page_has_buffers(page
)) {
5526 if (!ext4_walk_page_buffers(NULL
, page_buffers(page
),
5528 ext4_bh_unmapped
)) {
5529 /* Wait so that we don't change page under IO */
5530 wait_for_stable_page(page
);
5531 ret
= VM_FAULT_LOCKED
;
5536 /* OK, we need to fill the hole... */
5537 if (ext4_should_dioread_nolock(inode
))
5538 get_block
= ext4_get_block_write
;
5540 get_block
= ext4_get_block
;
5542 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
5543 ext4_writepage_trans_blocks(inode
));
5544 if (IS_ERR(handle
)) {
5545 ret
= VM_FAULT_SIGBUS
;
5548 ret
= block_page_mkwrite(vma
, vmf
, get_block
);
5549 if (!ret
&& ext4_should_journal_data(inode
)) {
5550 if (ext4_walk_page_buffers(handle
, page_buffers(page
), 0,
5551 PAGE_CACHE_SIZE
, NULL
, do_journal_get_write_access
)) {
5553 ret
= VM_FAULT_SIGBUS
;
5554 ext4_journal_stop(handle
);
5557 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
5559 ext4_journal_stop(handle
);
5560 if (ret
== -ENOSPC
&& ext4_should_retry_alloc(inode
->i_sb
, &retries
))
5563 ret
= block_page_mkwrite_return(ret
);
5565 up_read(&EXT4_I(inode
)->i_mmap_sem
);
5566 sb_end_pagefault(inode
->i_sb
);
5570 int ext4_filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
5572 struct inode
*inode
= file_inode(vma
->vm_file
);
5575 down_read(&EXT4_I(inode
)->i_mmap_sem
);
5576 err
= filemap_fault(vma
, vmf
);
5577 up_read(&EXT4_I(inode
)->i_mmap_sem
);