1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/inode.c
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
12 * linux/fs/minix/inode.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * 64-bit file support on 64-bit platforms by Jakub Jelinek
17 * (jj@sunsite.ms.mff.cuni.cz)
19 * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
23 #include <linux/time.h>
24 #include <linux/highuid.h>
25 #include <linux/pagemap.h>
26 #include <linux/dax.h>
27 #include <linux/quotaops.h>
28 #include <linux/string.h>
29 #include <linux/buffer_head.h>
30 #include <linux/writeback.h>
31 #include <linux/pagevec.h>
32 #include <linux/mpage.h>
33 #include <linux/namei.h>
34 #include <linux/uio.h>
35 #include <linux/bio.h>
36 #include <linux/workqueue.h>
37 #include <linux/kernel.h>
38 #include <linux/printk.h>
39 #include <linux/slab.h>
40 #include <linux/bitops.h>
41 #include <linux/iomap.h>
42 #include <linux/iversion.h>
44 #include "ext4_jbd2.h"
49 #include <trace/events/ext4.h>
51 static __u32
ext4_inode_csum(struct inode
*inode
, struct ext4_inode
*raw
,
52 struct ext4_inode_info
*ei
)
54 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
57 int offset
= offsetof(struct ext4_inode
, i_checksum_lo
);
58 unsigned int csum_size
= sizeof(dummy_csum
);
60 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)raw
, offset
);
61 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
, csum_size
);
63 csum
= ext4_chksum(sbi
, csum
, (__u8
*)raw
+ offset
,
64 EXT4_GOOD_OLD_INODE_SIZE
- offset
);
66 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
67 offset
= offsetof(struct ext4_inode
, i_checksum_hi
);
68 csum
= ext4_chksum(sbi
, csum
, (__u8
*)raw
+
69 EXT4_GOOD_OLD_INODE_SIZE
,
70 offset
- EXT4_GOOD_OLD_INODE_SIZE
);
71 if (EXT4_FITS_IN_INODE(raw
, ei
, i_checksum_hi
)) {
72 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
,
76 csum
= ext4_chksum(sbi
, csum
, (__u8
*)raw
+ offset
,
77 EXT4_INODE_SIZE(inode
->i_sb
) - offset
);
83 static int ext4_inode_csum_verify(struct inode
*inode
, struct ext4_inode
*raw
,
84 struct ext4_inode_info
*ei
)
86 __u32 provided
, calculated
;
88 if (EXT4_SB(inode
->i_sb
)->s_es
->s_creator_os
!=
89 cpu_to_le32(EXT4_OS_LINUX
) ||
90 !ext4_has_metadata_csum(inode
->i_sb
))
93 provided
= le16_to_cpu(raw
->i_checksum_lo
);
94 calculated
= ext4_inode_csum(inode
, raw
, ei
);
95 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
&&
96 EXT4_FITS_IN_INODE(raw
, ei
, i_checksum_hi
))
97 provided
|= ((__u32
)le16_to_cpu(raw
->i_checksum_hi
)) << 16;
101 return provided
== calculated
;
104 static void ext4_inode_csum_set(struct inode
*inode
, struct ext4_inode
*raw
,
105 struct ext4_inode_info
*ei
)
109 if (EXT4_SB(inode
->i_sb
)->s_es
->s_creator_os
!=
110 cpu_to_le32(EXT4_OS_LINUX
) ||
111 !ext4_has_metadata_csum(inode
->i_sb
))
114 csum
= ext4_inode_csum(inode
, raw
, ei
);
115 raw
->i_checksum_lo
= cpu_to_le16(csum
& 0xFFFF);
116 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
&&
117 EXT4_FITS_IN_INODE(raw
, ei
, i_checksum_hi
))
118 raw
->i_checksum_hi
= cpu_to_le16(csum
>> 16);
121 static inline int ext4_begin_ordered_truncate(struct inode
*inode
,
124 trace_ext4_begin_ordered_truncate(inode
, new_size
);
126 * If jinode is zero, then we never opened the file for
127 * writing, so there's no need to call
128 * jbd2_journal_begin_ordered_truncate() since there's no
129 * outstanding writes we need to flush.
131 if (!EXT4_I(inode
)->jinode
)
133 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode
),
134 EXT4_I(inode
)->jinode
,
138 static void ext4_invalidatepage(struct page
*page
, unsigned int offset
,
139 unsigned int length
);
140 static int __ext4_journalled_writepage(struct page
*page
, unsigned int len
);
141 static int ext4_bh_delay_or_unwritten(handle_t
*handle
, struct buffer_head
*bh
);
142 static int ext4_meta_trans_blocks(struct inode
*inode
, int lblocks
,
146 * Test whether an inode is a fast symlink.
147 * A fast symlink has its symlink data stored in ext4_inode_info->i_data.
149 int ext4_inode_is_fast_symlink(struct inode
*inode
)
151 if (!(EXT4_I(inode
)->i_flags
& EXT4_EA_INODE_FL
)) {
152 int ea_blocks
= EXT4_I(inode
)->i_file_acl
?
153 EXT4_CLUSTER_SIZE(inode
->i_sb
) >> 9 : 0;
155 if (ext4_has_inline_data(inode
))
158 return (S_ISLNK(inode
->i_mode
) && inode
->i_blocks
- ea_blocks
== 0);
160 return S_ISLNK(inode
->i_mode
) && inode
->i_size
&&
161 (inode
->i_size
< EXT4_N_BLOCKS
* 4);
165 * Called at the last iput() if i_nlink is zero.
167 void ext4_evict_inode(struct inode
*inode
)
172 * Credits for final inode cleanup and freeing:
173 * sb + inode (ext4_orphan_del()), block bitmap, group descriptor
174 * (xattr block freeing), bitmap, group descriptor (inode freeing)
176 int extra_credits
= 6;
177 struct ext4_xattr_inode_array
*ea_inode_array
= NULL
;
179 trace_ext4_evict_inode(inode
);
181 if (inode
->i_nlink
) {
183 * When journalling data dirty buffers are tracked only in the
184 * journal. So although mm thinks everything is clean and
185 * ready for reaping the inode might still have some pages to
186 * write in the running transaction or waiting to be
187 * checkpointed. Thus calling jbd2_journal_invalidatepage()
188 * (via truncate_inode_pages()) to discard these buffers can
189 * cause data loss. Also even if we did not discard these
190 * buffers, we would have no way to find them after the inode
191 * is reaped and thus user could see stale data if he tries to
192 * read them before the transaction is checkpointed. So be
193 * careful and force everything to disk here... We use
194 * ei->i_datasync_tid to store the newest transaction
195 * containing inode's data.
197 * Note that directories do not have this problem because they
198 * don't use page cache.
200 if (inode
->i_ino
!= EXT4_JOURNAL_INO
&&
201 ext4_should_journal_data(inode
) &&
202 (S_ISLNK(inode
->i_mode
) || S_ISREG(inode
->i_mode
)) &&
203 inode
->i_data
.nrpages
) {
204 journal_t
*journal
= EXT4_SB(inode
->i_sb
)->s_journal
;
205 tid_t commit_tid
= EXT4_I(inode
)->i_datasync_tid
;
207 jbd2_complete_transaction(journal
, commit_tid
);
208 filemap_write_and_wait(&inode
->i_data
);
210 truncate_inode_pages_final(&inode
->i_data
);
215 if (is_bad_inode(inode
))
217 dquot_initialize(inode
);
219 if (ext4_should_order_data(inode
))
220 ext4_begin_ordered_truncate(inode
, 0);
221 truncate_inode_pages_final(&inode
->i_data
);
224 * Protect us against freezing - iput() caller didn't have to have any
225 * protection against it
227 sb_start_intwrite(inode
->i_sb
);
229 if (!IS_NOQUOTA(inode
))
230 extra_credits
+= EXT4_MAXQUOTAS_DEL_BLOCKS(inode
->i_sb
);
233 * Block bitmap, group descriptor, and inode are accounted in both
234 * ext4_blocks_for_truncate() and extra_credits. So subtract 3.
236 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
,
237 ext4_blocks_for_truncate(inode
) + extra_credits
- 3);
238 if (IS_ERR(handle
)) {
239 ext4_std_error(inode
->i_sb
, PTR_ERR(handle
));
241 * If we're going to skip the normal cleanup, we still need to
242 * make sure that the in-core orphan linked list is properly
245 ext4_orphan_del(NULL
, inode
);
246 sb_end_intwrite(inode
->i_sb
);
251 ext4_handle_sync(handle
);
254 * Set inode->i_size to 0 before calling ext4_truncate(). We need
255 * special handling of symlinks here because i_size is used to
256 * determine whether ext4_inode_info->i_data contains symlink data or
257 * block mappings. Setting i_size to 0 will remove its fast symlink
258 * status. Erase i_data so that it becomes a valid empty block map.
260 if (ext4_inode_is_fast_symlink(inode
))
261 memset(EXT4_I(inode
)->i_data
, 0, sizeof(EXT4_I(inode
)->i_data
));
263 err
= ext4_mark_inode_dirty(handle
, inode
);
265 ext4_warning(inode
->i_sb
,
266 "couldn't mark inode dirty (err %d)", err
);
269 if (inode
->i_blocks
) {
270 err
= ext4_truncate(inode
);
272 ext4_error_err(inode
->i_sb
, -err
,
273 "couldn't truncate inode %lu (err %d)",
279 /* Remove xattr references. */
280 err
= ext4_xattr_delete_inode(handle
, inode
, &ea_inode_array
,
283 ext4_warning(inode
->i_sb
, "xattr delete (err %d)", err
);
285 ext4_journal_stop(handle
);
286 ext4_orphan_del(NULL
, inode
);
287 sb_end_intwrite(inode
->i_sb
);
288 ext4_xattr_inode_array_free(ea_inode_array
);
293 * Kill off the orphan record which ext4_truncate created.
294 * AKPM: I think this can be inside the above `if'.
295 * Note that ext4_orphan_del() has to be able to cope with the
296 * deletion of a non-existent orphan - this is because we don't
297 * know if ext4_truncate() actually created an orphan record.
298 * (Well, we could do this if we need to, but heck - it works)
300 ext4_orphan_del(handle
, inode
);
301 EXT4_I(inode
)->i_dtime
= (__u32
)ktime_get_real_seconds();
304 * One subtle ordering requirement: if anything has gone wrong
305 * (transaction abort, IO errors, whatever), then we can still
306 * do these next steps (the fs will already have been marked as
307 * having errors), but we can't free the inode if the mark_dirty
310 if (ext4_mark_inode_dirty(handle
, inode
))
311 /* If that failed, just do the required in-core inode clear. */
312 ext4_clear_inode(inode
);
314 ext4_free_inode(handle
, inode
);
315 ext4_journal_stop(handle
);
316 sb_end_intwrite(inode
->i_sb
);
317 ext4_xattr_inode_array_free(ea_inode_array
);
320 ext4_clear_inode(inode
); /* We must guarantee clearing of inode... */
324 qsize_t
*ext4_get_reserved_space(struct inode
*inode
)
326 return &EXT4_I(inode
)->i_reserved_quota
;
331 * Called with i_data_sem down, which is important since we can call
332 * ext4_discard_preallocations() from here.
334 void ext4_da_update_reserve_space(struct inode
*inode
,
335 int used
, int quota_claim
)
337 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
338 struct ext4_inode_info
*ei
= EXT4_I(inode
);
340 spin_lock(&ei
->i_block_reservation_lock
);
341 trace_ext4_da_update_reserve_space(inode
, used
, quota_claim
);
342 if (unlikely(used
> ei
->i_reserved_data_blocks
)) {
343 ext4_warning(inode
->i_sb
, "%s: ino %lu, used %d "
344 "with only %d reserved data blocks",
345 __func__
, inode
->i_ino
, used
,
346 ei
->i_reserved_data_blocks
);
348 used
= ei
->i_reserved_data_blocks
;
351 /* Update per-inode reservations */
352 ei
->i_reserved_data_blocks
-= used
;
353 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
, used
);
355 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
357 /* Update quota subsystem for data blocks */
359 dquot_claim_block(inode
, EXT4_C2B(sbi
, used
));
362 * We did fallocate with an offset that is already delayed
363 * allocated. So on delayed allocated writeback we should
364 * not re-claim the quota for fallocated blocks.
366 dquot_release_reservation_block(inode
, EXT4_C2B(sbi
, used
));
370 * If we have done all the pending block allocations and if
371 * there aren't any writers on the inode, we can discard the
372 * inode's preallocations.
374 if ((ei
->i_reserved_data_blocks
== 0) &&
375 !inode_is_open_for_write(inode
))
376 ext4_discard_preallocations(inode
);
379 static int __check_block_validity(struct inode
*inode
, const char *func
,
381 struct ext4_map_blocks
*map
)
383 if (ext4_has_feature_journal(inode
->i_sb
) &&
385 le32_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_journal_inum
)))
387 if (!ext4_data_block_valid(EXT4_SB(inode
->i_sb
), map
->m_pblk
,
389 ext4_error_inode(inode
, func
, line
, map
->m_pblk
,
390 "lblock %lu mapped to illegal pblock %llu "
391 "(length %d)", (unsigned long) map
->m_lblk
,
392 map
->m_pblk
, map
->m_len
);
393 return -EFSCORRUPTED
;
398 int ext4_issue_zeroout(struct inode
*inode
, ext4_lblk_t lblk
, ext4_fsblk_t pblk
,
403 if (IS_ENCRYPTED(inode
) && S_ISREG(inode
->i_mode
))
404 return fscrypt_zeroout_range(inode
, lblk
, pblk
, len
);
406 ret
= sb_issue_zeroout(inode
->i_sb
, pblk
, len
, GFP_NOFS
);
413 #define check_block_validity(inode, map) \
414 __check_block_validity((inode), __func__, __LINE__, (map))
416 #ifdef ES_AGGRESSIVE_TEST
417 static void ext4_map_blocks_es_recheck(handle_t
*handle
,
419 struct ext4_map_blocks
*es_map
,
420 struct ext4_map_blocks
*map
,
427 * There is a race window that the result is not the same.
428 * e.g. xfstests #223 when dioread_nolock enables. The reason
429 * is that we lookup a block mapping in extent status tree with
430 * out taking i_data_sem. So at the time the unwritten extent
431 * could be converted.
433 down_read(&EXT4_I(inode
)->i_data_sem
);
434 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
435 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
&
436 EXT4_GET_BLOCKS_KEEP_SIZE
);
438 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
&
439 EXT4_GET_BLOCKS_KEEP_SIZE
);
441 up_read((&EXT4_I(inode
)->i_data_sem
));
444 * We don't check m_len because extent will be collpased in status
445 * tree. So the m_len might not equal.
447 if (es_map
->m_lblk
!= map
->m_lblk
||
448 es_map
->m_flags
!= map
->m_flags
||
449 es_map
->m_pblk
!= map
->m_pblk
) {
450 printk("ES cache assertion failed for inode: %lu "
451 "es_cached ex [%d/%d/%llu/%x] != "
452 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
453 inode
->i_ino
, es_map
->m_lblk
, es_map
->m_len
,
454 es_map
->m_pblk
, es_map
->m_flags
, map
->m_lblk
,
455 map
->m_len
, map
->m_pblk
, map
->m_flags
,
459 #endif /* ES_AGGRESSIVE_TEST */
462 * The ext4_map_blocks() function tries to look up the requested blocks,
463 * and returns if the blocks are already mapped.
465 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
466 * and store the allocated blocks in the result buffer head and mark it
469 * If file type is extents based, it will call ext4_ext_map_blocks(),
470 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
473 * On success, it returns the number of blocks being mapped or allocated. if
474 * create==0 and the blocks are pre-allocated and unwritten, the resulting @map
475 * is marked as unwritten. If the create == 1, it will mark @map as mapped.
477 * It returns 0 if plain look up failed (blocks have not been allocated), in
478 * that case, @map is returned as unmapped but we still do fill map->m_len to
479 * indicate the length of a hole starting at map->m_lblk.
481 * It returns the error in case of allocation failure.
483 int ext4_map_blocks(handle_t
*handle
, struct inode
*inode
,
484 struct ext4_map_blocks
*map
, int flags
)
486 struct extent_status es
;
489 #ifdef ES_AGGRESSIVE_TEST
490 struct ext4_map_blocks orig_map
;
492 memcpy(&orig_map
, map
, sizeof(*map
));
496 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
497 "logical block %lu\n", inode
->i_ino
, flags
, map
->m_len
,
498 (unsigned long) map
->m_lblk
);
501 * ext4_map_blocks returns an int, and m_len is an unsigned int
503 if (unlikely(map
->m_len
> INT_MAX
))
504 map
->m_len
= INT_MAX
;
506 /* We can handle the block number less than EXT_MAX_BLOCKS */
507 if (unlikely(map
->m_lblk
>= EXT_MAX_BLOCKS
))
508 return -EFSCORRUPTED
;
510 /* Lookup extent status tree firstly */
511 if (ext4_es_lookup_extent(inode
, map
->m_lblk
, NULL
, &es
)) {
512 if (ext4_es_is_written(&es
) || ext4_es_is_unwritten(&es
)) {
513 map
->m_pblk
= ext4_es_pblock(&es
) +
514 map
->m_lblk
- es
.es_lblk
;
515 map
->m_flags
|= ext4_es_is_written(&es
) ?
516 EXT4_MAP_MAPPED
: EXT4_MAP_UNWRITTEN
;
517 retval
= es
.es_len
- (map
->m_lblk
- es
.es_lblk
);
518 if (retval
> map
->m_len
)
521 } else if (ext4_es_is_delayed(&es
) || ext4_es_is_hole(&es
)) {
523 retval
= es
.es_len
- (map
->m_lblk
- es
.es_lblk
);
524 if (retval
> map
->m_len
)
531 #ifdef ES_AGGRESSIVE_TEST
532 ext4_map_blocks_es_recheck(handle
, inode
, map
,
539 * Try to see if we can get the block without requesting a new
542 down_read(&EXT4_I(inode
)->i_data_sem
);
543 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
544 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
&
545 EXT4_GET_BLOCKS_KEEP_SIZE
);
547 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
&
548 EXT4_GET_BLOCKS_KEEP_SIZE
);
553 if (unlikely(retval
!= map
->m_len
)) {
554 ext4_warning(inode
->i_sb
,
555 "ES len assertion failed for inode "
556 "%lu: retval %d != map->m_len %d",
557 inode
->i_ino
, retval
, map
->m_len
);
561 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
562 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
563 if (!(flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) &&
564 !(status
& EXTENT_STATUS_WRITTEN
) &&
565 ext4_es_scan_range(inode
, &ext4_es_is_delayed
, map
->m_lblk
,
566 map
->m_lblk
+ map
->m_len
- 1))
567 status
|= EXTENT_STATUS_DELAYED
;
568 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
,
569 map
->m_len
, map
->m_pblk
, status
);
573 up_read((&EXT4_I(inode
)->i_data_sem
));
576 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
) {
577 ret
= check_block_validity(inode
, map
);
582 /* If it is only a block(s) look up */
583 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0)
587 * Returns if the blocks have already allocated
589 * Note that if blocks have been preallocated
590 * ext4_ext_get_block() returns the create = 0
591 * with buffer head unmapped.
593 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
)
595 * If we need to convert extent to unwritten
596 * we continue and do the actual work in
597 * ext4_ext_map_blocks()
599 if (!(flags
& EXT4_GET_BLOCKS_CONVERT_UNWRITTEN
))
603 * Here we clear m_flags because after allocating an new extent,
604 * it will be set again.
606 map
->m_flags
&= ~EXT4_MAP_FLAGS
;
609 * New blocks allocate and/or writing to unwritten extent
610 * will possibly result in updating i_data, so we take
611 * the write lock of i_data_sem, and call get_block()
612 * with create == 1 flag.
614 down_write(&EXT4_I(inode
)->i_data_sem
);
617 * We need to check for EXT4 here because migrate
618 * could have changed the inode type in between
620 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
621 retval
= ext4_ext_map_blocks(handle
, inode
, map
, flags
);
623 retval
= ext4_ind_map_blocks(handle
, inode
, map
, flags
);
625 if (retval
> 0 && map
->m_flags
& EXT4_MAP_NEW
) {
627 * We allocated new blocks which will result in
628 * i_data's format changing. Force the migrate
629 * to fail by clearing migrate flags
631 ext4_clear_inode_state(inode
, EXT4_STATE_EXT_MIGRATE
);
635 * Update reserved blocks/metadata blocks after successful
636 * block allocation which had been deferred till now. We don't
637 * support fallocate for non extent files. So we can update
638 * reserve space here.
641 (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
))
642 ext4_da_update_reserve_space(inode
, retval
, 1);
648 if (unlikely(retval
!= map
->m_len
)) {
649 ext4_warning(inode
->i_sb
,
650 "ES len assertion failed for inode "
651 "%lu: retval %d != map->m_len %d",
652 inode
->i_ino
, retval
, map
->m_len
);
657 * We have to zeroout blocks before inserting them into extent
658 * status tree. Otherwise someone could look them up there and
659 * use them before they are really zeroed. We also have to
660 * unmap metadata before zeroing as otherwise writeback can
661 * overwrite zeros with stale data from block device.
663 if (flags
& EXT4_GET_BLOCKS_ZERO
&&
664 map
->m_flags
& EXT4_MAP_MAPPED
&&
665 map
->m_flags
& EXT4_MAP_NEW
) {
666 ret
= ext4_issue_zeroout(inode
, map
->m_lblk
,
667 map
->m_pblk
, map
->m_len
);
675 * If the extent has been zeroed out, we don't need to update
676 * extent status tree.
678 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
) &&
679 ext4_es_lookup_extent(inode
, map
->m_lblk
, NULL
, &es
)) {
680 if (ext4_es_is_written(&es
))
683 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
684 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
685 if (!(flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) &&
686 !(status
& EXTENT_STATUS_WRITTEN
) &&
687 ext4_es_scan_range(inode
, &ext4_es_is_delayed
, map
->m_lblk
,
688 map
->m_lblk
+ map
->m_len
- 1))
689 status
|= EXTENT_STATUS_DELAYED
;
690 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
, map
->m_len
,
691 map
->m_pblk
, status
);
699 up_write((&EXT4_I(inode
)->i_data_sem
));
700 if (retval
> 0 && map
->m_flags
& EXT4_MAP_MAPPED
) {
701 ret
= check_block_validity(inode
, map
);
706 * Inodes with freshly allocated blocks where contents will be
707 * visible after transaction commit must be on transaction's
710 if (map
->m_flags
& EXT4_MAP_NEW
&&
711 !(map
->m_flags
& EXT4_MAP_UNWRITTEN
) &&
712 !(flags
& EXT4_GET_BLOCKS_ZERO
) &&
713 !ext4_is_quota_file(inode
) &&
714 ext4_should_order_data(inode
)) {
716 (loff_t
)map
->m_lblk
<< inode
->i_blkbits
;
717 loff_t length
= (loff_t
)map
->m_len
<< inode
->i_blkbits
;
719 if (flags
& EXT4_GET_BLOCKS_IO_SUBMIT
)
720 ret
= ext4_jbd2_inode_add_wait(handle
, inode
,
723 ret
= ext4_jbd2_inode_add_write(handle
, inode
,
733 * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
734 * we have to be careful as someone else may be manipulating b_state as well.
736 static void ext4_update_bh_state(struct buffer_head
*bh
, unsigned long flags
)
738 unsigned long old_state
;
739 unsigned long new_state
;
741 flags
&= EXT4_MAP_FLAGS
;
743 /* Dummy buffer_head? Set non-atomically. */
745 bh
->b_state
= (bh
->b_state
& ~EXT4_MAP_FLAGS
) | flags
;
749 * Someone else may be modifying b_state. Be careful! This is ugly but
750 * once we get rid of using bh as a container for mapping information
751 * to pass to / from get_block functions, this can go away.
754 old_state
= READ_ONCE(bh
->b_state
);
755 new_state
= (old_state
& ~EXT4_MAP_FLAGS
) | flags
;
757 cmpxchg(&bh
->b_state
, old_state
, new_state
) != old_state
));
760 static int _ext4_get_block(struct inode
*inode
, sector_t iblock
,
761 struct buffer_head
*bh
, int flags
)
763 struct ext4_map_blocks map
;
766 if (ext4_has_inline_data(inode
))
770 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
772 ret
= ext4_map_blocks(ext4_journal_current_handle(), inode
, &map
,
775 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
776 ext4_update_bh_state(bh
, map
.m_flags
);
777 bh
->b_size
= inode
->i_sb
->s_blocksize
* map
.m_len
;
779 } else if (ret
== 0) {
780 /* hole case, need to fill in bh->b_size */
781 bh
->b_size
= inode
->i_sb
->s_blocksize
* map
.m_len
;
786 int ext4_get_block(struct inode
*inode
, sector_t iblock
,
787 struct buffer_head
*bh
, int create
)
789 return _ext4_get_block(inode
, iblock
, bh
,
790 create
? EXT4_GET_BLOCKS_CREATE
: 0);
794 * Get block function used when preparing for buffered write if we require
795 * creating an unwritten extent if blocks haven't been allocated. The extent
796 * will be converted to written after the IO is complete.
798 int ext4_get_block_unwritten(struct inode
*inode
, sector_t iblock
,
799 struct buffer_head
*bh_result
, int create
)
801 ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
802 inode
->i_ino
, create
);
803 return _ext4_get_block(inode
, iblock
, bh_result
,
804 EXT4_GET_BLOCKS_IO_CREATE_EXT
);
807 /* Maximum number of blocks we map for direct IO at once. */
808 #define DIO_MAX_BLOCKS 4096
811 * `handle' can be NULL if create is zero
813 struct buffer_head
*ext4_getblk(handle_t
*handle
, struct inode
*inode
,
814 ext4_lblk_t block
, int map_flags
)
816 struct ext4_map_blocks map
;
817 struct buffer_head
*bh
;
818 int create
= map_flags
& EXT4_GET_BLOCKS_CREATE
;
821 J_ASSERT(handle
!= NULL
|| create
== 0);
825 err
= ext4_map_blocks(handle
, inode
, &map
, map_flags
);
828 return create
? ERR_PTR(-ENOSPC
) : NULL
;
832 bh
= sb_getblk(inode
->i_sb
, map
.m_pblk
);
834 return ERR_PTR(-ENOMEM
);
835 if (map
.m_flags
& EXT4_MAP_NEW
) {
836 J_ASSERT(create
!= 0);
837 J_ASSERT(handle
!= NULL
);
840 * Now that we do not always journal data, we should
841 * keep in mind whether this should always journal the
842 * new buffer as metadata. For now, regular file
843 * writes use ext4_get_block instead, so it's not a
847 BUFFER_TRACE(bh
, "call get_create_access");
848 err
= ext4_journal_get_create_access(handle
, bh
);
853 if (!buffer_uptodate(bh
)) {
854 memset(bh
->b_data
, 0, inode
->i_sb
->s_blocksize
);
855 set_buffer_uptodate(bh
);
858 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
859 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
863 BUFFER_TRACE(bh
, "not a new buffer");
870 struct buffer_head
*ext4_bread(handle_t
*handle
, struct inode
*inode
,
871 ext4_lblk_t block
, int map_flags
)
873 struct buffer_head
*bh
;
875 bh
= ext4_getblk(handle
, inode
, block
, map_flags
);
878 if (!bh
|| ext4_buffer_uptodate(bh
))
880 ll_rw_block(REQ_OP_READ
, REQ_META
| REQ_PRIO
, 1, &bh
);
882 if (buffer_uptodate(bh
))
885 return ERR_PTR(-EIO
);
888 /* Read a contiguous batch of blocks. */
889 int ext4_bread_batch(struct inode
*inode
, ext4_lblk_t block
, int bh_count
,
890 bool wait
, struct buffer_head
**bhs
)
894 for (i
= 0; i
< bh_count
; i
++) {
895 bhs
[i
] = ext4_getblk(NULL
, inode
, block
+ i
, 0 /* map_flags */);
896 if (IS_ERR(bhs
[i
])) {
897 err
= PTR_ERR(bhs
[i
]);
903 for (i
= 0; i
< bh_count
; i
++)
904 /* Note that NULL bhs[i] is valid because of holes. */
905 if (bhs
[i
] && !ext4_buffer_uptodate(bhs
[i
]))
906 ll_rw_block(REQ_OP_READ
, REQ_META
| REQ_PRIO
, 1,
912 for (i
= 0; i
< bh_count
; i
++)
914 wait_on_buffer(bhs
[i
]);
916 for (i
= 0; i
< bh_count
; i
++) {
917 if (bhs
[i
] && !buffer_uptodate(bhs
[i
])) {
925 for (i
= 0; i
< bh_count
; i
++) {
932 int ext4_walk_page_buffers(handle_t
*handle
,
933 struct buffer_head
*head
,
937 int (*fn
)(handle_t
*handle
,
938 struct buffer_head
*bh
))
940 struct buffer_head
*bh
;
941 unsigned block_start
, block_end
;
942 unsigned blocksize
= head
->b_size
;
944 struct buffer_head
*next
;
946 for (bh
= head
, block_start
= 0;
947 ret
== 0 && (bh
!= head
|| !block_start
);
948 block_start
= block_end
, bh
= next
) {
949 next
= bh
->b_this_page
;
950 block_end
= block_start
+ blocksize
;
951 if (block_end
<= from
|| block_start
>= to
) {
952 if (partial
&& !buffer_uptodate(bh
))
956 err
= (*fn
)(handle
, bh
);
964 * To preserve ordering, it is essential that the hole instantiation and
965 * the data write be encapsulated in a single transaction. We cannot
966 * close off a transaction and start a new one between the ext4_get_block()
967 * and the commit_write(). So doing the jbd2_journal_start at the start of
968 * prepare_write() is the right place.
970 * Also, this function can nest inside ext4_writepage(). In that case, we
971 * *know* that ext4_writepage() has generated enough buffer credits to do the
972 * whole page. So we won't block on the journal in that case, which is good,
973 * because the caller may be PF_MEMALLOC.
975 * By accident, ext4 can be reentered when a transaction is open via
976 * quota file writes. If we were to commit the transaction while thus
977 * reentered, there can be a deadlock - we would be holding a quota
978 * lock, and the commit would never complete if another thread had a
979 * transaction open and was blocking on the quota lock - a ranking
982 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
983 * will _not_ run commit under these circumstances because handle->h_ref
984 * is elevated. We'll still have enough credits for the tiny quotafile
987 int do_journal_get_write_access(handle_t
*handle
,
988 struct buffer_head
*bh
)
990 int dirty
= buffer_dirty(bh
);
993 if (!buffer_mapped(bh
) || buffer_freed(bh
))
996 * __block_write_begin() could have dirtied some buffers. Clean
997 * the dirty bit as jbd2_journal_get_write_access() could complain
998 * otherwise about fs integrity issues. Setting of the dirty bit
999 * by __block_write_begin() isn't a real problem here as we clear
1000 * the bit before releasing a page lock and thus writeback cannot
1001 * ever write the buffer.
1004 clear_buffer_dirty(bh
);
1005 BUFFER_TRACE(bh
, "get write access");
1006 ret
= ext4_journal_get_write_access(handle
, bh
);
1008 ret
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
1012 #ifdef CONFIG_FS_ENCRYPTION
1013 static int ext4_block_write_begin(struct page
*page
, loff_t pos
, unsigned len
,
1014 get_block_t
*get_block
)
1016 unsigned from
= pos
& (PAGE_SIZE
- 1);
1017 unsigned to
= from
+ len
;
1018 struct inode
*inode
= page
->mapping
->host
;
1019 unsigned block_start
, block_end
;
1022 unsigned blocksize
= inode
->i_sb
->s_blocksize
;
1024 struct buffer_head
*bh
, *head
, *wait
[2];
1028 BUG_ON(!PageLocked(page
));
1029 BUG_ON(from
> PAGE_SIZE
);
1030 BUG_ON(to
> PAGE_SIZE
);
1033 if (!page_has_buffers(page
))
1034 create_empty_buffers(page
, blocksize
, 0);
1035 head
= page_buffers(page
);
1036 bbits
= ilog2(blocksize
);
1037 block
= (sector_t
)page
->index
<< (PAGE_SHIFT
- bbits
);
1039 for (bh
= head
, block_start
= 0; bh
!= head
|| !block_start
;
1040 block
++, block_start
= block_end
, bh
= bh
->b_this_page
) {
1041 block_end
= block_start
+ blocksize
;
1042 if (block_end
<= from
|| block_start
>= to
) {
1043 if (PageUptodate(page
)) {
1044 if (!buffer_uptodate(bh
))
1045 set_buffer_uptodate(bh
);
1050 clear_buffer_new(bh
);
1051 if (!buffer_mapped(bh
)) {
1052 WARN_ON(bh
->b_size
!= blocksize
);
1053 err
= get_block(inode
, block
, bh
, 1);
1056 if (buffer_new(bh
)) {
1057 if (PageUptodate(page
)) {
1058 clear_buffer_new(bh
);
1059 set_buffer_uptodate(bh
);
1060 mark_buffer_dirty(bh
);
1063 if (block_end
> to
|| block_start
< from
)
1064 zero_user_segments(page
, to
, block_end
,
1069 if (PageUptodate(page
)) {
1070 if (!buffer_uptodate(bh
))
1071 set_buffer_uptodate(bh
);
1074 if (!buffer_uptodate(bh
) && !buffer_delay(bh
) &&
1075 !buffer_unwritten(bh
) &&
1076 (block_start
< from
|| block_end
> to
)) {
1077 ll_rw_block(REQ_OP_READ
, 0, 1, &bh
);
1078 wait
[nr_wait
++] = bh
;
1082 * If we issued read requests, let them complete.
1084 for (i
= 0; i
< nr_wait
; i
++) {
1085 wait_on_buffer(wait
[i
]);
1086 if (!buffer_uptodate(wait
[i
]))
1089 if (unlikely(err
)) {
1090 page_zero_new_buffers(page
, from
, to
);
1091 } else if (IS_ENCRYPTED(inode
) && S_ISREG(inode
->i_mode
)) {
1092 for (i
= 0; i
< nr_wait
; i
++) {
1095 err2
= fscrypt_decrypt_pagecache_blocks(page
, blocksize
,
1096 bh_offset(wait
[i
]));
1098 clear_buffer_uptodate(wait
[i
]);
1108 static int ext4_write_begin(struct file
*file
, struct address_space
*mapping
,
1109 loff_t pos
, unsigned len
, unsigned flags
,
1110 struct page
**pagep
, void **fsdata
)
1112 struct inode
*inode
= mapping
->host
;
1113 int ret
, needed_blocks
;
1120 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
1123 trace_ext4_write_begin(inode
, pos
, len
, flags
);
1125 * Reserve one block more for addition to orphan list in case
1126 * we allocate blocks but write fails for some reason
1128 needed_blocks
= ext4_writepage_trans_blocks(inode
) + 1;
1129 index
= pos
>> PAGE_SHIFT
;
1130 from
= pos
& (PAGE_SIZE
- 1);
1133 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
1134 ret
= ext4_try_to_write_inline_data(mapping
, inode
, pos
, len
,
1143 * grab_cache_page_write_begin() can take a long time if the
1144 * system is thrashing due to memory pressure, or if the page
1145 * is being written back. So grab it first before we start
1146 * the transaction handle. This also allows us to allocate
1147 * the page (if needed) without using GFP_NOFS.
1150 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
1156 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
, needed_blocks
);
1157 if (IS_ERR(handle
)) {
1159 return PTR_ERR(handle
);
1163 if (page
->mapping
!= mapping
) {
1164 /* The page got truncated from under us */
1167 ext4_journal_stop(handle
);
1170 /* In case writeback began while the page was unlocked */
1171 wait_for_stable_page(page
);
1173 #ifdef CONFIG_FS_ENCRYPTION
1174 if (ext4_should_dioread_nolock(inode
))
1175 ret
= ext4_block_write_begin(page
, pos
, len
,
1176 ext4_get_block_unwritten
);
1178 ret
= ext4_block_write_begin(page
, pos
, len
,
1181 if (ext4_should_dioread_nolock(inode
))
1182 ret
= __block_write_begin(page
, pos
, len
,
1183 ext4_get_block_unwritten
);
1185 ret
= __block_write_begin(page
, pos
, len
, ext4_get_block
);
1187 if (!ret
&& ext4_should_journal_data(inode
)) {
1188 ret
= ext4_walk_page_buffers(handle
, page_buffers(page
),
1190 do_journal_get_write_access
);
1194 bool extended
= (pos
+ len
> inode
->i_size
) &&
1195 !ext4_verity_in_progress(inode
);
1199 * __block_write_begin may have instantiated a few blocks
1200 * outside i_size. Trim these off again. Don't need
1201 * i_size_read because we hold i_mutex.
1203 * Add inode to orphan list in case we crash before
1206 if (extended
&& ext4_can_truncate(inode
))
1207 ext4_orphan_add(handle
, inode
);
1209 ext4_journal_stop(handle
);
1211 ext4_truncate_failed_write(inode
);
1213 * If truncate failed early the inode might
1214 * still be on the orphan list; we need to
1215 * make sure the inode is removed from the
1216 * orphan list in that case.
1219 ext4_orphan_del(NULL
, inode
);
1222 if (ret
== -ENOSPC
&&
1223 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1232 /* For write_end() in data=journal mode */
1233 static int write_end_fn(handle_t
*handle
, struct buffer_head
*bh
)
1236 if (!buffer_mapped(bh
) || buffer_freed(bh
))
1238 set_buffer_uptodate(bh
);
1239 ret
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
1240 clear_buffer_meta(bh
);
1241 clear_buffer_prio(bh
);
1246 * We need to pick up the new inode size which generic_commit_write gave us
1247 * `file' can be NULL - eg, when called from page_symlink().
1249 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1250 * buffers are managed internally.
1252 static int ext4_write_end(struct file
*file
,
1253 struct address_space
*mapping
,
1254 loff_t pos
, unsigned len
, unsigned copied
,
1255 struct page
*page
, void *fsdata
)
1257 handle_t
*handle
= ext4_journal_current_handle();
1258 struct inode
*inode
= mapping
->host
;
1259 loff_t old_size
= inode
->i_size
;
1261 int i_size_changed
= 0;
1262 int inline_data
= ext4_has_inline_data(inode
);
1263 bool verity
= ext4_verity_in_progress(inode
);
1265 trace_ext4_write_end(inode
, pos
, len
, copied
);
1267 ret
= ext4_write_inline_data_end(inode
, pos
, len
,
1276 copied
= block_write_end(file
, mapping
, pos
,
1277 len
, copied
, page
, fsdata
);
1279 * it's important to update i_size while still holding page lock:
1280 * page writeout could otherwise come in and zero beyond i_size.
1282 * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree
1283 * blocks are being written past EOF, so skip the i_size update.
1286 i_size_changed
= ext4_update_inode_size(inode
, pos
+ copied
);
1290 if (old_size
< pos
&& !verity
)
1291 pagecache_isize_extended(inode
, old_size
, pos
);
1293 * Don't mark the inode dirty under page lock. First, it unnecessarily
1294 * makes the holding time of page lock longer. Second, it forces lock
1295 * ordering of page lock and transaction start for journaling
1298 if (i_size_changed
|| inline_data
)
1299 ext4_mark_inode_dirty(handle
, inode
);
1301 if (pos
+ len
> inode
->i_size
&& !verity
&& ext4_can_truncate(inode
))
1302 /* if we have allocated more blocks and copied
1303 * less. We will have blocks allocated outside
1304 * inode->i_size. So truncate them
1306 ext4_orphan_add(handle
, inode
);
1308 ret2
= ext4_journal_stop(handle
);
1312 if (pos
+ len
> inode
->i_size
&& !verity
) {
1313 ext4_truncate_failed_write(inode
);
1315 * If truncate failed early the inode might still be
1316 * on the orphan list; we need to make sure the inode
1317 * is removed from the orphan list in that case.
1320 ext4_orphan_del(NULL
, inode
);
1323 return ret
? ret
: copied
;
1327 * This is a private version of page_zero_new_buffers() which doesn't
1328 * set the buffer to be dirty, since in data=journalled mode we need
1329 * to call ext4_handle_dirty_metadata() instead.
1331 static void ext4_journalled_zero_new_buffers(handle_t
*handle
,
1333 unsigned from
, unsigned to
)
1335 unsigned int block_start
= 0, block_end
;
1336 struct buffer_head
*head
, *bh
;
1338 bh
= head
= page_buffers(page
);
1340 block_end
= block_start
+ bh
->b_size
;
1341 if (buffer_new(bh
)) {
1342 if (block_end
> from
&& block_start
< to
) {
1343 if (!PageUptodate(page
)) {
1344 unsigned start
, size
;
1346 start
= max(from
, block_start
);
1347 size
= min(to
, block_end
) - start
;
1349 zero_user(page
, start
, size
);
1350 write_end_fn(handle
, bh
);
1352 clear_buffer_new(bh
);
1355 block_start
= block_end
;
1356 bh
= bh
->b_this_page
;
1357 } while (bh
!= head
);
1360 static int ext4_journalled_write_end(struct file
*file
,
1361 struct address_space
*mapping
,
1362 loff_t pos
, unsigned len
, unsigned copied
,
1363 struct page
*page
, void *fsdata
)
1365 handle_t
*handle
= ext4_journal_current_handle();
1366 struct inode
*inode
= mapping
->host
;
1367 loff_t old_size
= inode
->i_size
;
1371 int size_changed
= 0;
1372 int inline_data
= ext4_has_inline_data(inode
);
1373 bool verity
= ext4_verity_in_progress(inode
);
1375 trace_ext4_journalled_write_end(inode
, pos
, len
, copied
);
1376 from
= pos
& (PAGE_SIZE
- 1);
1379 BUG_ON(!ext4_handle_valid(handle
));
1382 ret
= ext4_write_inline_data_end(inode
, pos
, len
,
1390 } else if (unlikely(copied
< len
) && !PageUptodate(page
)) {
1392 ext4_journalled_zero_new_buffers(handle
, page
, from
, to
);
1394 if (unlikely(copied
< len
))
1395 ext4_journalled_zero_new_buffers(handle
, page
,
1397 ret
= ext4_walk_page_buffers(handle
, page_buffers(page
), from
,
1398 from
+ copied
, &partial
,
1401 SetPageUptodate(page
);
1404 size_changed
= ext4_update_inode_size(inode
, pos
+ copied
);
1405 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
1406 EXT4_I(inode
)->i_datasync_tid
= handle
->h_transaction
->t_tid
;
1410 if (old_size
< pos
&& !verity
)
1411 pagecache_isize_extended(inode
, old_size
, pos
);
1413 if (size_changed
|| inline_data
) {
1414 ret2
= ext4_mark_inode_dirty(handle
, inode
);
1419 if (pos
+ len
> inode
->i_size
&& !verity
&& ext4_can_truncate(inode
))
1420 /* if we have allocated more blocks and copied
1421 * less. We will have blocks allocated outside
1422 * inode->i_size. So truncate them
1424 ext4_orphan_add(handle
, inode
);
1427 ret2
= ext4_journal_stop(handle
);
1430 if (pos
+ len
> inode
->i_size
&& !verity
) {
1431 ext4_truncate_failed_write(inode
);
1433 * If truncate failed early the inode might still be
1434 * on the orphan list; we need to make sure the inode
1435 * is removed from the orphan list in that case.
1438 ext4_orphan_del(NULL
, inode
);
1441 return ret
? ret
: copied
;
1445 * Reserve space for a single cluster
1447 static int ext4_da_reserve_space(struct inode
*inode
)
1449 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1450 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1454 * We will charge metadata quota at writeout time; this saves
1455 * us from metadata over-estimation, though we may go over by
1456 * a small amount in the end. Here we just reserve for data.
1458 ret
= dquot_reserve_block(inode
, EXT4_C2B(sbi
, 1));
1462 spin_lock(&ei
->i_block_reservation_lock
);
1463 if (ext4_claim_free_clusters(sbi
, 1, 0)) {
1464 spin_unlock(&ei
->i_block_reservation_lock
);
1465 dquot_release_reservation_block(inode
, EXT4_C2B(sbi
, 1));
1468 ei
->i_reserved_data_blocks
++;
1469 trace_ext4_da_reserve_space(inode
);
1470 spin_unlock(&ei
->i_block_reservation_lock
);
1472 return 0; /* success */
1475 void ext4_da_release_space(struct inode
*inode
, int to_free
)
1477 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1478 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1481 return; /* Nothing to release, exit */
1483 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1485 trace_ext4_da_release_space(inode
, to_free
);
1486 if (unlikely(to_free
> ei
->i_reserved_data_blocks
)) {
1488 * if there aren't enough reserved blocks, then the
1489 * counter is messed up somewhere. Since this
1490 * function is called from invalidate page, it's
1491 * harmless to return without any action.
1493 ext4_warning(inode
->i_sb
, "ext4_da_release_space: "
1494 "ino %lu, to_free %d with only %d reserved "
1495 "data blocks", inode
->i_ino
, to_free
,
1496 ei
->i_reserved_data_blocks
);
1498 to_free
= ei
->i_reserved_data_blocks
;
1500 ei
->i_reserved_data_blocks
-= to_free
;
1502 /* update fs dirty data blocks counter */
1503 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
, to_free
);
1505 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1507 dquot_release_reservation_block(inode
, EXT4_C2B(sbi
, to_free
));
1511 * Delayed allocation stuff
1514 struct mpage_da_data
{
1515 struct inode
*inode
;
1516 struct writeback_control
*wbc
;
1518 pgoff_t first_page
; /* The first page to write */
1519 pgoff_t next_page
; /* Current page to examine */
1520 pgoff_t last_page
; /* Last page to examine */
1522 * Extent to map - this can be after first_page because that can be
1523 * fully mapped. We somewhat abuse m_flags to store whether the extent
1524 * is delalloc or unwritten.
1526 struct ext4_map_blocks map
;
1527 struct ext4_io_submit io_submit
; /* IO submission data */
1528 unsigned int do_map
:1;
1531 static void mpage_release_unused_pages(struct mpage_da_data
*mpd
,
1536 struct pagevec pvec
;
1537 struct inode
*inode
= mpd
->inode
;
1538 struct address_space
*mapping
= inode
->i_mapping
;
1540 /* This is necessary when next_page == 0. */
1541 if (mpd
->first_page
>= mpd
->next_page
)
1544 index
= mpd
->first_page
;
1545 end
= mpd
->next_page
- 1;
1547 ext4_lblk_t start
, last
;
1548 start
= index
<< (PAGE_SHIFT
- inode
->i_blkbits
);
1549 last
= end
<< (PAGE_SHIFT
- inode
->i_blkbits
);
1550 ext4_es_remove_extent(inode
, start
, last
- start
+ 1);
1553 pagevec_init(&pvec
);
1554 while (index
<= end
) {
1555 nr_pages
= pagevec_lookup_range(&pvec
, mapping
, &index
, end
);
1558 for (i
= 0; i
< nr_pages
; i
++) {
1559 struct page
*page
= pvec
.pages
[i
];
1561 BUG_ON(!PageLocked(page
));
1562 BUG_ON(PageWriteback(page
));
1564 if (page_mapped(page
))
1565 clear_page_dirty_for_io(page
);
1566 block_invalidatepage(page
, 0, PAGE_SIZE
);
1567 ClearPageUptodate(page
);
1571 pagevec_release(&pvec
);
1575 static void ext4_print_free_blocks(struct inode
*inode
)
1577 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1578 struct super_block
*sb
= inode
->i_sb
;
1579 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1581 ext4_msg(sb
, KERN_CRIT
, "Total free blocks count %lld",
1582 EXT4_C2B(EXT4_SB(inode
->i_sb
),
1583 ext4_count_free_clusters(sb
)));
1584 ext4_msg(sb
, KERN_CRIT
, "Free/Dirty block details");
1585 ext4_msg(sb
, KERN_CRIT
, "free_blocks=%lld",
1586 (long long) EXT4_C2B(EXT4_SB(sb
),
1587 percpu_counter_sum(&sbi
->s_freeclusters_counter
)));
1588 ext4_msg(sb
, KERN_CRIT
, "dirty_blocks=%lld",
1589 (long long) EXT4_C2B(EXT4_SB(sb
),
1590 percpu_counter_sum(&sbi
->s_dirtyclusters_counter
)));
1591 ext4_msg(sb
, KERN_CRIT
, "Block reservation details");
1592 ext4_msg(sb
, KERN_CRIT
, "i_reserved_data_blocks=%u",
1593 ei
->i_reserved_data_blocks
);
1597 static int ext4_bh_delay_or_unwritten(handle_t
*handle
, struct buffer_head
*bh
)
1599 return (buffer_delay(bh
) || buffer_unwritten(bh
)) && buffer_dirty(bh
);
1603 * ext4_insert_delayed_block - adds a delayed block to the extents status
1604 * tree, incrementing the reserved cluster/block
1605 * count or making a pending reservation
1608 * @inode - file containing the newly added block
1609 * @lblk - logical block to be added
1611 * Returns 0 on success, negative error code on failure.
1613 static int ext4_insert_delayed_block(struct inode
*inode
, ext4_lblk_t lblk
)
1615 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1617 bool allocated
= false;
1620 * If the cluster containing lblk is shared with a delayed,
1621 * written, or unwritten extent in a bigalloc file system, it's
1622 * already been accounted for and does not need to be reserved.
1623 * A pending reservation must be made for the cluster if it's
1624 * shared with a written or unwritten extent and doesn't already
1625 * have one. Written and unwritten extents can be purged from the
1626 * extents status tree if the system is under memory pressure, so
1627 * it's necessary to examine the extent tree if a search of the
1628 * extents status tree doesn't get a match.
1630 if (sbi
->s_cluster_ratio
== 1) {
1631 ret
= ext4_da_reserve_space(inode
);
1632 if (ret
!= 0) /* ENOSPC */
1634 } else { /* bigalloc */
1635 if (!ext4_es_scan_clu(inode
, &ext4_es_is_delonly
, lblk
)) {
1636 if (!ext4_es_scan_clu(inode
,
1637 &ext4_es_is_mapped
, lblk
)) {
1638 ret
= ext4_clu_mapped(inode
,
1639 EXT4_B2C(sbi
, lblk
));
1643 ret
= ext4_da_reserve_space(inode
);
1644 if (ret
!= 0) /* ENOSPC */
1655 ret
= ext4_es_insert_delayed_block(inode
, lblk
, allocated
);
1662 * This function is grabs code from the very beginning of
1663 * ext4_map_blocks, but assumes that the caller is from delayed write
1664 * time. This function looks up the requested blocks and sets the
1665 * buffer delay bit under the protection of i_data_sem.
1667 static int ext4_da_map_blocks(struct inode
*inode
, sector_t iblock
,
1668 struct ext4_map_blocks
*map
,
1669 struct buffer_head
*bh
)
1671 struct extent_status es
;
1673 sector_t invalid_block
= ~((sector_t
) 0xffff);
1674 #ifdef ES_AGGRESSIVE_TEST
1675 struct ext4_map_blocks orig_map
;
1677 memcpy(&orig_map
, map
, sizeof(*map
));
1680 if (invalid_block
< ext4_blocks_count(EXT4_SB(inode
->i_sb
)->s_es
))
1684 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1685 "logical block %lu\n", inode
->i_ino
, map
->m_len
,
1686 (unsigned long) map
->m_lblk
);
1688 /* Lookup extent status tree firstly */
1689 if (ext4_es_lookup_extent(inode
, iblock
, NULL
, &es
)) {
1690 if (ext4_es_is_hole(&es
)) {
1692 down_read(&EXT4_I(inode
)->i_data_sem
);
1697 * Delayed extent could be allocated by fallocate.
1698 * So we need to check it.
1700 if (ext4_es_is_delayed(&es
) && !ext4_es_is_unwritten(&es
)) {
1701 map_bh(bh
, inode
->i_sb
, invalid_block
);
1703 set_buffer_delay(bh
);
1707 map
->m_pblk
= ext4_es_pblock(&es
) + iblock
- es
.es_lblk
;
1708 retval
= es
.es_len
- (iblock
- es
.es_lblk
);
1709 if (retval
> map
->m_len
)
1710 retval
= map
->m_len
;
1711 map
->m_len
= retval
;
1712 if (ext4_es_is_written(&es
))
1713 map
->m_flags
|= EXT4_MAP_MAPPED
;
1714 else if (ext4_es_is_unwritten(&es
))
1715 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
1719 #ifdef ES_AGGRESSIVE_TEST
1720 ext4_map_blocks_es_recheck(NULL
, inode
, map
, &orig_map
, 0);
1726 * Try to see if we can get the block without requesting a new
1727 * file system block.
1729 down_read(&EXT4_I(inode
)->i_data_sem
);
1730 if (ext4_has_inline_data(inode
))
1732 else if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
1733 retval
= ext4_ext_map_blocks(NULL
, inode
, map
, 0);
1735 retval
= ext4_ind_map_blocks(NULL
, inode
, map
, 0);
1742 * XXX: __block_prepare_write() unmaps passed block,
1746 ret
= ext4_insert_delayed_block(inode
, map
->m_lblk
);
1752 map_bh(bh
, inode
->i_sb
, invalid_block
);
1754 set_buffer_delay(bh
);
1755 } else if (retval
> 0) {
1757 unsigned int status
;
1759 if (unlikely(retval
!= map
->m_len
)) {
1760 ext4_warning(inode
->i_sb
,
1761 "ES len assertion failed for inode "
1762 "%lu: retval %d != map->m_len %d",
1763 inode
->i_ino
, retval
, map
->m_len
);
1767 status
= map
->m_flags
& EXT4_MAP_UNWRITTEN
?
1768 EXTENT_STATUS_UNWRITTEN
: EXTENT_STATUS_WRITTEN
;
1769 ret
= ext4_es_insert_extent(inode
, map
->m_lblk
, map
->m_len
,
1770 map
->m_pblk
, status
);
1776 up_read((&EXT4_I(inode
)->i_data_sem
));
1782 * This is a special get_block_t callback which is used by
1783 * ext4_da_write_begin(). It will either return mapped block or
1784 * reserve space for a single block.
1786 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1787 * We also have b_blocknr = -1 and b_bdev initialized properly
1789 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1790 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1791 * initialized properly.
1793 int ext4_da_get_block_prep(struct inode
*inode
, sector_t iblock
,
1794 struct buffer_head
*bh
, int create
)
1796 struct ext4_map_blocks map
;
1799 BUG_ON(create
== 0);
1800 BUG_ON(bh
->b_size
!= inode
->i_sb
->s_blocksize
);
1802 map
.m_lblk
= iblock
;
1806 * first, we need to know whether the block is allocated already
1807 * preallocated blocks are unmapped but should treated
1808 * the same as allocated blocks.
1810 ret
= ext4_da_map_blocks(inode
, iblock
, &map
, bh
);
1814 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
1815 ext4_update_bh_state(bh
, map
.m_flags
);
1817 if (buffer_unwritten(bh
)) {
1818 /* A delayed write to unwritten bh should be marked
1819 * new and mapped. Mapped ensures that we don't do
1820 * get_block multiple times when we write to the same
1821 * offset and new ensures that we do proper zero out
1822 * for partial write.
1825 set_buffer_mapped(bh
);
1830 static int bget_one(handle_t
*handle
, struct buffer_head
*bh
)
1836 static int bput_one(handle_t
*handle
, struct buffer_head
*bh
)
1842 static int __ext4_journalled_writepage(struct page
*page
,
1845 struct address_space
*mapping
= page
->mapping
;
1846 struct inode
*inode
= mapping
->host
;
1847 struct buffer_head
*page_bufs
= NULL
;
1848 handle_t
*handle
= NULL
;
1849 int ret
= 0, err
= 0;
1850 int inline_data
= ext4_has_inline_data(inode
);
1851 struct buffer_head
*inode_bh
= NULL
;
1853 ClearPageChecked(page
);
1856 BUG_ON(page
->index
!= 0);
1857 BUG_ON(len
> ext4_get_max_inline_size(inode
));
1858 inode_bh
= ext4_journalled_write_inline_data(inode
, len
, page
);
1859 if (inode_bh
== NULL
)
1862 page_bufs
= page_buffers(page
);
1867 ext4_walk_page_buffers(handle
, page_bufs
, 0, len
,
1871 * We need to release the page lock before we start the
1872 * journal, so grab a reference so the page won't disappear
1873 * out from under us.
1878 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
1879 ext4_writepage_trans_blocks(inode
));
1880 if (IS_ERR(handle
)) {
1881 ret
= PTR_ERR(handle
);
1883 goto out_no_pagelock
;
1885 BUG_ON(!ext4_handle_valid(handle
));
1889 if (page
->mapping
!= mapping
) {
1890 /* The page got truncated from under us */
1891 ext4_journal_stop(handle
);
1897 ret
= ext4_mark_inode_dirty(handle
, inode
);
1899 ret
= ext4_walk_page_buffers(handle
, page_bufs
, 0, len
, NULL
,
1900 do_journal_get_write_access
);
1902 err
= ext4_walk_page_buffers(handle
, page_bufs
, 0, len
, NULL
,
1907 EXT4_I(inode
)->i_datasync_tid
= handle
->h_transaction
->t_tid
;
1908 err
= ext4_journal_stop(handle
);
1912 if (!ext4_has_inline_data(inode
))
1913 ext4_walk_page_buffers(NULL
, page_bufs
, 0, len
,
1915 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
1924 * Note that we don't need to start a transaction unless we're journaling data
1925 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1926 * need to file the inode to the transaction's list in ordered mode because if
1927 * we are writing back data added by write(), the inode is already there and if
1928 * we are writing back data modified via mmap(), no one guarantees in which
1929 * transaction the data will hit the disk. In case we are journaling data, we
1930 * cannot start transaction directly because transaction start ranks above page
1931 * lock so we have to do some magic.
1933 * This function can get called via...
1934 * - ext4_writepages after taking page lock (have journal handle)
1935 * - journal_submit_inode_data_buffers (no journal handle)
1936 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
1937 * - grab_page_cache when doing write_begin (have journal handle)
1939 * We don't do any block allocation in this function. If we have page with
1940 * multiple blocks we need to write those buffer_heads that are mapped. This
1941 * is important for mmaped based write. So if we do with blocksize 1K
1942 * truncate(f, 1024);
1943 * a = mmap(f, 0, 4096);
1945 * truncate(f, 4096);
1946 * we have in the page first buffer_head mapped via page_mkwrite call back
1947 * but other buffer_heads would be unmapped but dirty (dirty done via the
1948 * do_wp_page). So writepage should write the first block. If we modify
1949 * the mmap area beyond 1024 we will again get a page_fault and the
1950 * page_mkwrite callback will do the block allocation and mark the
1951 * buffer_heads mapped.
1953 * We redirty the page if we have any buffer_heads that is either delay or
1954 * unwritten in the page.
1956 * We can get recursively called as show below.
1958 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1961 * But since we don't do any block allocation we should not deadlock.
1962 * Page also have the dirty flag cleared so we don't get recurive page_lock.
1964 static int ext4_writepage(struct page
*page
,
1965 struct writeback_control
*wbc
)
1970 struct buffer_head
*page_bufs
= NULL
;
1971 struct inode
*inode
= page
->mapping
->host
;
1972 struct ext4_io_submit io_submit
;
1973 bool keep_towrite
= false;
1975 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
)))) {
1976 ext4_invalidatepage(page
, 0, PAGE_SIZE
);
1981 trace_ext4_writepage(page
);
1982 size
= i_size_read(inode
);
1983 if (page
->index
== size
>> PAGE_SHIFT
&&
1984 !ext4_verity_in_progress(inode
))
1985 len
= size
& ~PAGE_MASK
;
1989 page_bufs
= page_buffers(page
);
1991 * We cannot do block allocation or other extent handling in this
1992 * function. If there are buffers needing that, we have to redirty
1993 * the page. But we may reach here when we do a journal commit via
1994 * journal_submit_inode_data_buffers() and in that case we must write
1995 * allocated buffers to achieve data=ordered mode guarantees.
1997 * Also, if there is only one buffer per page (the fs block
1998 * size == the page size), if one buffer needs block
1999 * allocation or needs to modify the extent tree to clear the
2000 * unwritten flag, we know that the page can't be written at
2001 * all, so we might as well refuse the write immediately.
2002 * Unfortunately if the block size != page size, we can't as
2003 * easily detect this case using ext4_walk_page_buffers(), but
2004 * for the extremely common case, this is an optimization that
2005 * skips a useless round trip through ext4_bio_write_page().
2007 if (ext4_walk_page_buffers(NULL
, page_bufs
, 0, len
, NULL
,
2008 ext4_bh_delay_or_unwritten
)) {
2009 redirty_page_for_writepage(wbc
, page
);
2010 if ((current
->flags
& PF_MEMALLOC
) ||
2011 (inode
->i_sb
->s_blocksize
== PAGE_SIZE
)) {
2013 * For memory cleaning there's no point in writing only
2014 * some buffers. So just bail out. Warn if we came here
2015 * from direct reclaim.
2017 WARN_ON_ONCE((current
->flags
& (PF_MEMALLOC
|PF_KSWAPD
))
2022 keep_towrite
= true;
2025 if (PageChecked(page
) && ext4_should_journal_data(inode
))
2027 * It's mmapped pagecache. Add buffers and journal it. There
2028 * doesn't seem much point in redirtying the page here.
2030 return __ext4_journalled_writepage(page
, len
);
2032 ext4_io_submit_init(&io_submit
, wbc
);
2033 io_submit
.io_end
= ext4_init_io_end(inode
, GFP_NOFS
);
2034 if (!io_submit
.io_end
) {
2035 redirty_page_for_writepage(wbc
, page
);
2039 ret
= ext4_bio_write_page(&io_submit
, page
, len
, wbc
, keep_towrite
);
2040 ext4_io_submit(&io_submit
);
2041 /* Drop io_end reference we got from init */
2042 ext4_put_io_end_defer(io_submit
.io_end
);
2046 static int mpage_submit_page(struct mpage_da_data
*mpd
, struct page
*page
)
2052 BUG_ON(page
->index
!= mpd
->first_page
);
2053 clear_page_dirty_for_io(page
);
2055 * We have to be very careful here! Nothing protects writeback path
2056 * against i_size changes and the page can be writeably mapped into
2057 * page tables. So an application can be growing i_size and writing
2058 * data through mmap while writeback runs. clear_page_dirty_for_io()
2059 * write-protects our page in page tables and the page cannot get
2060 * written to again until we release page lock. So only after
2061 * clear_page_dirty_for_io() we are safe to sample i_size for
2062 * ext4_bio_write_page() to zero-out tail of the written page. We rely
2063 * on the barrier provided by TestClearPageDirty in
2064 * clear_page_dirty_for_io() to make sure i_size is really sampled only
2065 * after page tables are updated.
2067 size
= i_size_read(mpd
->inode
);
2068 if (page
->index
== size
>> PAGE_SHIFT
&&
2069 !ext4_verity_in_progress(mpd
->inode
))
2070 len
= size
& ~PAGE_MASK
;
2073 err
= ext4_bio_write_page(&mpd
->io_submit
, page
, len
, mpd
->wbc
, false);
2075 mpd
->wbc
->nr_to_write
--;
2081 #define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
2084 * mballoc gives us at most this number of blocks...
2085 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
2086 * The rest of mballoc seems to handle chunks up to full group size.
2088 #define MAX_WRITEPAGES_EXTENT_LEN 2048
2091 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
2093 * @mpd - extent of blocks
2094 * @lblk - logical number of the block in the file
2095 * @bh - buffer head we want to add to the extent
2097 * The function is used to collect contig. blocks in the same state. If the
2098 * buffer doesn't require mapping for writeback and we haven't started the
2099 * extent of buffers to map yet, the function returns 'true' immediately - the
2100 * caller can write the buffer right away. Otherwise the function returns true
2101 * if the block has been added to the extent, false if the block couldn't be
2104 static bool mpage_add_bh_to_extent(struct mpage_da_data
*mpd
, ext4_lblk_t lblk
,
2105 struct buffer_head
*bh
)
2107 struct ext4_map_blocks
*map
= &mpd
->map
;
2109 /* Buffer that doesn't need mapping for writeback? */
2110 if (!buffer_dirty(bh
) || !buffer_mapped(bh
) ||
2111 (!buffer_delay(bh
) && !buffer_unwritten(bh
))) {
2112 /* So far no extent to map => we write the buffer right away */
2113 if (map
->m_len
== 0)
2118 /* First block in the extent? */
2119 if (map
->m_len
== 0) {
2120 /* We cannot map unless handle is started... */
2125 map
->m_flags
= bh
->b_state
& BH_FLAGS
;
2129 /* Don't go larger than mballoc is willing to allocate */
2130 if (map
->m_len
>= MAX_WRITEPAGES_EXTENT_LEN
)
2133 /* Can we merge the block to our big extent? */
2134 if (lblk
== map
->m_lblk
+ map
->m_len
&&
2135 (bh
->b_state
& BH_FLAGS
) == map
->m_flags
) {
2143 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
2145 * @mpd - extent of blocks for mapping
2146 * @head - the first buffer in the page
2147 * @bh - buffer we should start processing from
2148 * @lblk - logical number of the block in the file corresponding to @bh
2150 * Walk through page buffers from @bh upto @head (exclusive) and either submit
2151 * the page for IO if all buffers in this page were mapped and there's no
2152 * accumulated extent of buffers to map or add buffers in the page to the
2153 * extent of buffers to map. The function returns 1 if the caller can continue
2154 * by processing the next page, 0 if it should stop adding buffers to the
2155 * extent to map because we cannot extend it anymore. It can also return value
2156 * < 0 in case of error during IO submission.
2158 static int mpage_process_page_bufs(struct mpage_da_data
*mpd
,
2159 struct buffer_head
*head
,
2160 struct buffer_head
*bh
,
2163 struct inode
*inode
= mpd
->inode
;
2165 ext4_lblk_t blocks
= (i_size_read(inode
) + i_blocksize(inode
) - 1)
2166 >> inode
->i_blkbits
;
2168 if (ext4_verity_in_progress(inode
))
2169 blocks
= EXT_MAX_BLOCKS
;
2172 BUG_ON(buffer_locked(bh
));
2174 if (lblk
>= blocks
|| !mpage_add_bh_to_extent(mpd
, lblk
, bh
)) {
2175 /* Found extent to map? */
2178 /* Buffer needs mapping and handle is not started? */
2181 /* Everything mapped so far and we hit EOF */
2184 } while (lblk
++, (bh
= bh
->b_this_page
) != head
);
2185 /* So far everything mapped? Submit the page for IO. */
2186 if (mpd
->map
.m_len
== 0) {
2187 err
= mpage_submit_page(mpd
, head
->b_page
);
2191 return lblk
< blocks
;
2195 * mpage_process_page - update page buffers corresponding to changed extent and
2196 * may submit fully mapped page for IO
2198 * @mpd - description of extent to map, on return next extent to map
2199 * @m_lblk - logical block mapping.
2200 * @m_pblk - corresponding physical mapping.
2201 * @map_bh - determines on return whether this page requires any further
2203 * Scan given page buffers corresponding to changed extent and update buffer
2204 * state according to new extent state.
2205 * We map delalloc buffers to their physical location, clear unwritten bits.
2206 * If the given page is not fully mapped, we update @map to the next extent in
2207 * the given page that needs mapping & return @map_bh as true.
2209 static int mpage_process_page(struct mpage_da_data
*mpd
, struct page
*page
,
2210 ext4_lblk_t
*m_lblk
, ext4_fsblk_t
*m_pblk
,
2213 struct buffer_head
*head
, *bh
;
2214 ext4_io_end_t
*io_end
= mpd
->io_submit
.io_end
;
2215 ext4_lblk_t lblk
= *m_lblk
;
2216 ext4_fsblk_t pblock
= *m_pblk
;
2218 int blkbits
= mpd
->inode
->i_blkbits
;
2219 ssize_t io_end_size
= 0;
2220 struct ext4_io_end_vec
*io_end_vec
= ext4_last_io_end_vec(io_end
);
2222 bh
= head
= page_buffers(page
);
2224 if (lblk
< mpd
->map
.m_lblk
)
2226 if (lblk
>= mpd
->map
.m_lblk
+ mpd
->map
.m_len
) {
2228 * Buffer after end of mapped extent.
2229 * Find next buffer in the page to map.
2232 mpd
->map
.m_flags
= 0;
2233 io_end_vec
->size
+= io_end_size
;
2236 err
= mpage_process_page_bufs(mpd
, head
, bh
, lblk
);
2239 if (!err
&& mpd
->map
.m_len
&& mpd
->map
.m_lblk
> lblk
) {
2240 io_end_vec
= ext4_alloc_io_end_vec(io_end
);
2241 if (IS_ERR(io_end_vec
)) {
2242 err
= PTR_ERR(io_end_vec
);
2245 io_end_vec
->offset
= mpd
->map
.m_lblk
<< blkbits
;
2250 if (buffer_delay(bh
)) {
2251 clear_buffer_delay(bh
);
2252 bh
->b_blocknr
= pblock
++;
2254 clear_buffer_unwritten(bh
);
2255 io_end_size
+= (1 << blkbits
);
2256 } while (lblk
++, (bh
= bh
->b_this_page
) != head
);
2258 io_end_vec
->size
+= io_end_size
;
2268 * mpage_map_buffers - update buffers corresponding to changed extent and
2269 * submit fully mapped pages for IO
2271 * @mpd - description of extent to map, on return next extent to map
2273 * Scan buffers corresponding to changed extent (we expect corresponding pages
2274 * to be already locked) and update buffer state according to new extent state.
2275 * We map delalloc buffers to their physical location, clear unwritten bits,
2276 * and mark buffers as uninit when we perform writes to unwritten extents
2277 * and do extent conversion after IO is finished. If the last page is not fully
2278 * mapped, we update @map to the next extent in the last page that needs
2279 * mapping. Otherwise we submit the page for IO.
2281 static int mpage_map_and_submit_buffers(struct mpage_da_data
*mpd
)
2283 struct pagevec pvec
;
2285 struct inode
*inode
= mpd
->inode
;
2286 int bpp_bits
= PAGE_SHIFT
- inode
->i_blkbits
;
2289 ext4_fsblk_t pblock
;
2291 bool map_bh
= false;
2293 start
= mpd
->map
.m_lblk
>> bpp_bits
;
2294 end
= (mpd
->map
.m_lblk
+ mpd
->map
.m_len
- 1) >> bpp_bits
;
2295 lblk
= start
<< bpp_bits
;
2296 pblock
= mpd
->map
.m_pblk
;
2298 pagevec_init(&pvec
);
2299 while (start
<= end
) {
2300 nr_pages
= pagevec_lookup_range(&pvec
, inode
->i_mapping
,
2304 for (i
= 0; i
< nr_pages
; i
++) {
2305 struct page
*page
= pvec
.pages
[i
];
2307 err
= mpage_process_page(mpd
, page
, &lblk
, &pblock
,
2310 * If map_bh is true, means page may require further bh
2311 * mapping, or maybe the page was submitted for IO.
2312 * So we return to call further extent mapping.
2314 if (err
< 0 || map_bh
== true)
2316 /* Page fully mapped - let IO run! */
2317 err
= mpage_submit_page(mpd
, page
);
2321 pagevec_release(&pvec
);
2323 /* Extent fully mapped and matches with page boundary. We are done. */
2325 mpd
->map
.m_flags
= 0;
2328 pagevec_release(&pvec
);
2332 static int mpage_map_one_extent(handle_t
*handle
, struct mpage_da_data
*mpd
)
2334 struct inode
*inode
= mpd
->inode
;
2335 struct ext4_map_blocks
*map
= &mpd
->map
;
2336 int get_blocks_flags
;
2337 int err
, dioread_nolock
;
2339 trace_ext4_da_write_pages_extent(inode
, map
);
2341 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
2342 * to convert an unwritten extent to be initialized (in the case
2343 * where we have written into one or more preallocated blocks). It is
2344 * possible that we're going to need more metadata blocks than
2345 * previously reserved. However we must not fail because we're in
2346 * writeback and there is nothing we can do about it so it might result
2347 * in data loss. So use reserved blocks to allocate metadata if
2350 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
2351 * the blocks in question are delalloc blocks. This indicates
2352 * that the blocks and quotas has already been checked when
2353 * the data was copied into the page cache.
2355 get_blocks_flags
= EXT4_GET_BLOCKS_CREATE
|
2356 EXT4_GET_BLOCKS_METADATA_NOFAIL
|
2357 EXT4_GET_BLOCKS_IO_SUBMIT
;
2358 dioread_nolock
= ext4_should_dioread_nolock(inode
);
2360 get_blocks_flags
|= EXT4_GET_BLOCKS_IO_CREATE_EXT
;
2361 if (map
->m_flags
& (1 << BH_Delay
))
2362 get_blocks_flags
|= EXT4_GET_BLOCKS_DELALLOC_RESERVE
;
2364 err
= ext4_map_blocks(handle
, inode
, map
, get_blocks_flags
);
2367 if (dioread_nolock
&& (map
->m_flags
& EXT4_MAP_UNWRITTEN
)) {
2368 if (!mpd
->io_submit
.io_end
->handle
&&
2369 ext4_handle_valid(handle
)) {
2370 mpd
->io_submit
.io_end
->handle
= handle
->h_rsv_handle
;
2371 handle
->h_rsv_handle
= NULL
;
2373 ext4_set_io_unwritten_flag(inode
, mpd
->io_submit
.io_end
);
2376 BUG_ON(map
->m_len
== 0);
2381 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2382 * mpd->len and submit pages underlying it for IO
2384 * @handle - handle for journal operations
2385 * @mpd - extent to map
2386 * @give_up_on_write - we set this to true iff there is a fatal error and there
2387 * is no hope of writing the data. The caller should discard
2388 * dirty pages to avoid infinite loops.
2390 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2391 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2392 * them to initialized or split the described range from larger unwritten
2393 * extent. Note that we need not map all the described range since allocation
2394 * can return less blocks or the range is covered by more unwritten extents. We
2395 * cannot map more because we are limited by reserved transaction credits. On
2396 * the other hand we always make sure that the last touched page is fully
2397 * mapped so that it can be written out (and thus forward progress is
2398 * guaranteed). After mapping we submit all mapped pages for IO.
2400 static int mpage_map_and_submit_extent(handle_t
*handle
,
2401 struct mpage_da_data
*mpd
,
2402 bool *give_up_on_write
)
2404 struct inode
*inode
= mpd
->inode
;
2405 struct ext4_map_blocks
*map
= &mpd
->map
;
2409 ext4_io_end_t
*io_end
= mpd
->io_submit
.io_end
;
2410 struct ext4_io_end_vec
*io_end_vec
;
2412 io_end_vec
= ext4_alloc_io_end_vec(io_end
);
2413 if (IS_ERR(io_end_vec
))
2414 return PTR_ERR(io_end_vec
);
2415 io_end_vec
->offset
= ((loff_t
)map
->m_lblk
) << inode
->i_blkbits
;
2417 err
= mpage_map_one_extent(handle
, mpd
);
2419 struct super_block
*sb
= inode
->i_sb
;
2421 if (ext4_forced_shutdown(EXT4_SB(sb
)) ||
2422 EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
)
2423 goto invalidate_dirty_pages
;
2425 * Let the uper layers retry transient errors.
2426 * In the case of ENOSPC, if ext4_count_free_blocks()
2427 * is non-zero, a commit should free up blocks.
2429 if ((err
== -ENOMEM
) ||
2430 (err
== -ENOSPC
&& ext4_count_free_clusters(sb
))) {
2432 goto update_disksize
;
2435 ext4_msg(sb
, KERN_CRIT
,
2436 "Delayed block allocation failed for "
2437 "inode %lu at logical offset %llu with"
2438 " max blocks %u with error %d",
2440 (unsigned long long)map
->m_lblk
,
2441 (unsigned)map
->m_len
, -err
);
2442 ext4_msg(sb
, KERN_CRIT
,
2443 "This should not happen!! Data will "
2446 ext4_print_free_blocks(inode
);
2447 invalidate_dirty_pages
:
2448 *give_up_on_write
= true;
2453 * Update buffer state, submit mapped pages, and get us new
2456 err
= mpage_map_and_submit_buffers(mpd
);
2458 goto update_disksize
;
2459 } while (map
->m_len
);
2463 * Update on-disk size after IO is submitted. Races with
2464 * truncate are avoided by checking i_size under i_data_sem.
2466 disksize
= ((loff_t
)mpd
->first_page
) << PAGE_SHIFT
;
2467 if (disksize
> READ_ONCE(EXT4_I(inode
)->i_disksize
)) {
2471 down_write(&EXT4_I(inode
)->i_data_sem
);
2472 i_size
= i_size_read(inode
);
2473 if (disksize
> i_size
)
2475 if (disksize
> EXT4_I(inode
)->i_disksize
)
2476 EXT4_I(inode
)->i_disksize
= disksize
;
2477 up_write(&EXT4_I(inode
)->i_data_sem
);
2478 err2
= ext4_mark_inode_dirty(handle
, inode
);
2480 ext4_error_err(inode
->i_sb
, -err2
,
2481 "Failed to mark inode %lu dirty",
2491 * Calculate the total number of credits to reserve for one writepages
2492 * iteration. This is called from ext4_writepages(). We map an extent of
2493 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
2494 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2495 * bpp - 1 blocks in bpp different extents.
2497 static int ext4_da_writepages_trans_blocks(struct inode
*inode
)
2499 int bpp
= ext4_journal_blocks_per_page(inode
);
2501 return ext4_meta_trans_blocks(inode
,
2502 MAX_WRITEPAGES_EXTENT_LEN
+ bpp
- 1, bpp
);
2506 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2507 * and underlying extent to map
2509 * @mpd - where to look for pages
2511 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2512 * IO immediately. When we find a page which isn't mapped we start accumulating
2513 * extent of buffers underlying these pages that needs mapping (formed by
2514 * either delayed or unwritten buffers). We also lock the pages containing
2515 * these buffers. The extent found is returned in @mpd structure (starting at
2516 * mpd->lblk with length mpd->len blocks).
2518 * Note that this function can attach bios to one io_end structure which are
2519 * neither logically nor physically contiguous. Although it may seem as an
2520 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2521 * case as we need to track IO to all buffers underlying a page in one io_end.
2523 static int mpage_prepare_extent_to_map(struct mpage_da_data
*mpd
)
2525 struct address_space
*mapping
= mpd
->inode
->i_mapping
;
2526 struct pagevec pvec
;
2527 unsigned int nr_pages
;
2528 long left
= mpd
->wbc
->nr_to_write
;
2529 pgoff_t index
= mpd
->first_page
;
2530 pgoff_t end
= mpd
->last_page
;
2533 int blkbits
= mpd
->inode
->i_blkbits
;
2535 struct buffer_head
*head
;
2537 if (mpd
->wbc
->sync_mode
== WB_SYNC_ALL
|| mpd
->wbc
->tagged_writepages
)
2538 tag
= PAGECACHE_TAG_TOWRITE
;
2540 tag
= PAGECACHE_TAG_DIRTY
;
2542 pagevec_init(&pvec
);
2544 mpd
->next_page
= index
;
2545 while (index
<= end
) {
2546 nr_pages
= pagevec_lookup_range_tag(&pvec
, mapping
, &index
, end
,
2551 for (i
= 0; i
< nr_pages
; i
++) {
2552 struct page
*page
= pvec
.pages
[i
];
2555 * Accumulated enough dirty pages? This doesn't apply
2556 * to WB_SYNC_ALL mode. For integrity sync we have to
2557 * keep going because someone may be concurrently
2558 * dirtying pages, and we might have synced a lot of
2559 * newly appeared dirty pages, but have not synced all
2560 * of the old dirty pages.
2562 if (mpd
->wbc
->sync_mode
== WB_SYNC_NONE
&& left
<= 0)
2565 /* If we can't merge this page, we are done. */
2566 if (mpd
->map
.m_len
> 0 && mpd
->next_page
!= page
->index
)
2571 * If the page is no longer dirty, or its mapping no
2572 * longer corresponds to inode we are writing (which
2573 * means it has been truncated or invalidated), or the
2574 * page is already under writeback and we are not doing
2575 * a data integrity writeback, skip the page
2577 if (!PageDirty(page
) ||
2578 (PageWriteback(page
) &&
2579 (mpd
->wbc
->sync_mode
== WB_SYNC_NONE
)) ||
2580 unlikely(page
->mapping
!= mapping
)) {
2585 wait_on_page_writeback(page
);
2586 BUG_ON(PageWriteback(page
));
2588 if (mpd
->map
.m_len
== 0)
2589 mpd
->first_page
= page
->index
;
2590 mpd
->next_page
= page
->index
+ 1;
2591 /* Add all dirty buffers to mpd */
2592 lblk
= ((ext4_lblk_t
)page
->index
) <<
2593 (PAGE_SHIFT
- blkbits
);
2594 head
= page_buffers(page
);
2595 err
= mpage_process_page_bufs(mpd
, head
, head
, lblk
);
2601 pagevec_release(&pvec
);
2606 pagevec_release(&pvec
);
2610 static int ext4_writepages(struct address_space
*mapping
,
2611 struct writeback_control
*wbc
)
2613 pgoff_t writeback_index
= 0;
2614 long nr_to_write
= wbc
->nr_to_write
;
2615 int range_whole
= 0;
2617 handle_t
*handle
= NULL
;
2618 struct mpage_da_data mpd
;
2619 struct inode
*inode
= mapping
->host
;
2620 int needed_blocks
, rsv_blocks
= 0, ret
= 0;
2621 struct ext4_sb_info
*sbi
= EXT4_SB(mapping
->host
->i_sb
);
2623 struct blk_plug plug
;
2624 bool give_up_on_write
= false;
2626 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
2629 percpu_down_read(&sbi
->s_writepages_rwsem
);
2630 trace_ext4_writepages(inode
, wbc
);
2633 * No pages to write? This is mainly a kludge to avoid starting
2634 * a transaction for special inodes like journal inode on last iput()
2635 * because that could violate lock ordering on umount
2637 if (!mapping
->nrpages
|| !mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
2638 goto out_writepages
;
2640 if (ext4_should_journal_data(inode
)) {
2641 ret
= generic_writepages(mapping
, wbc
);
2642 goto out_writepages
;
2646 * If the filesystem has aborted, it is read-only, so return
2647 * right away instead of dumping stack traces later on that
2648 * will obscure the real source of the problem. We test
2649 * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because
2650 * the latter could be true if the filesystem is mounted
2651 * read-only, and in that case, ext4_writepages should
2652 * *never* be called, so if that ever happens, we would want
2655 if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping
->host
->i_sb
)) ||
2656 sbi
->s_mount_flags
& EXT4_MF_FS_ABORTED
)) {
2658 goto out_writepages
;
2662 * If we have inline data and arrive here, it means that
2663 * we will soon create the block for the 1st page, so
2664 * we'd better clear the inline data here.
2666 if (ext4_has_inline_data(inode
)) {
2667 /* Just inode will be modified... */
2668 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
2669 if (IS_ERR(handle
)) {
2670 ret
= PTR_ERR(handle
);
2671 goto out_writepages
;
2673 BUG_ON(ext4_test_inode_state(inode
,
2674 EXT4_STATE_MAY_INLINE_DATA
));
2675 ext4_destroy_inline_data(handle
, inode
);
2676 ext4_journal_stop(handle
);
2679 if (ext4_should_dioread_nolock(inode
)) {
2681 * We may need to convert up to one extent per block in
2682 * the page and we may dirty the inode.
2684 rsv_blocks
= 1 + ext4_chunk_trans_blocks(inode
,
2685 PAGE_SIZE
>> inode
->i_blkbits
);
2688 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2691 if (wbc
->range_cyclic
) {
2692 writeback_index
= mapping
->writeback_index
;
2693 if (writeback_index
)
2695 mpd
.first_page
= writeback_index
;
2698 mpd
.first_page
= wbc
->range_start
>> PAGE_SHIFT
;
2699 mpd
.last_page
= wbc
->range_end
>> PAGE_SHIFT
;
2704 ext4_io_submit_init(&mpd
.io_submit
, wbc
);
2706 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2707 tag_pages_for_writeback(mapping
, mpd
.first_page
, mpd
.last_page
);
2709 blk_start_plug(&plug
);
2712 * First writeback pages that don't need mapping - we can avoid
2713 * starting a transaction unnecessarily and also avoid being blocked
2714 * in the block layer on device congestion while having transaction
2718 mpd
.io_submit
.io_end
= ext4_init_io_end(inode
, GFP_KERNEL
);
2719 if (!mpd
.io_submit
.io_end
) {
2723 ret
= mpage_prepare_extent_to_map(&mpd
);
2724 /* Unlock pages we didn't use */
2725 mpage_release_unused_pages(&mpd
, false);
2726 /* Submit prepared bio */
2727 ext4_io_submit(&mpd
.io_submit
);
2728 ext4_put_io_end_defer(mpd
.io_submit
.io_end
);
2729 mpd
.io_submit
.io_end
= NULL
;
2733 while (!done
&& mpd
.first_page
<= mpd
.last_page
) {
2734 /* For each extent of pages we use new io_end */
2735 mpd
.io_submit
.io_end
= ext4_init_io_end(inode
, GFP_KERNEL
);
2736 if (!mpd
.io_submit
.io_end
) {
2742 * We have two constraints: We find one extent to map and we
2743 * must always write out whole page (makes a difference when
2744 * blocksize < pagesize) so that we don't block on IO when we
2745 * try to write out the rest of the page. Journalled mode is
2746 * not supported by delalloc.
2748 BUG_ON(ext4_should_journal_data(inode
));
2749 needed_blocks
= ext4_da_writepages_trans_blocks(inode
);
2751 /* start a new transaction */
2752 handle
= ext4_journal_start_with_reserve(inode
,
2753 EXT4_HT_WRITE_PAGE
, needed_blocks
, rsv_blocks
);
2754 if (IS_ERR(handle
)) {
2755 ret
= PTR_ERR(handle
);
2756 ext4_msg(inode
->i_sb
, KERN_CRIT
, "%s: jbd2_start: "
2757 "%ld pages, ino %lu; err %d", __func__
,
2758 wbc
->nr_to_write
, inode
->i_ino
, ret
);
2759 /* Release allocated io_end */
2760 ext4_put_io_end(mpd
.io_submit
.io_end
);
2761 mpd
.io_submit
.io_end
= NULL
;
2766 trace_ext4_da_write_pages(inode
, mpd
.first_page
, mpd
.wbc
);
2767 ret
= mpage_prepare_extent_to_map(&mpd
);
2770 ret
= mpage_map_and_submit_extent(handle
, &mpd
,
2774 * We scanned the whole range (or exhausted
2775 * nr_to_write), submitted what was mapped and
2776 * didn't find anything needing mapping. We are
2783 * Caution: If the handle is synchronous,
2784 * ext4_journal_stop() can wait for transaction commit
2785 * to finish which may depend on writeback of pages to
2786 * complete or on page lock to be released. In that
2787 * case, we have to wait until after after we have
2788 * submitted all the IO, released page locks we hold,
2789 * and dropped io_end reference (for extent conversion
2790 * to be able to complete) before stopping the handle.
2792 if (!ext4_handle_valid(handle
) || handle
->h_sync
== 0) {
2793 ext4_journal_stop(handle
);
2797 /* Unlock pages we didn't use */
2798 mpage_release_unused_pages(&mpd
, give_up_on_write
);
2799 /* Submit prepared bio */
2800 ext4_io_submit(&mpd
.io_submit
);
2803 * Drop our io_end reference we got from init. We have
2804 * to be careful and use deferred io_end finishing if
2805 * we are still holding the transaction as we can
2806 * release the last reference to io_end which may end
2807 * up doing unwritten extent conversion.
2810 ext4_put_io_end_defer(mpd
.io_submit
.io_end
);
2811 ext4_journal_stop(handle
);
2813 ext4_put_io_end(mpd
.io_submit
.io_end
);
2814 mpd
.io_submit
.io_end
= NULL
;
2816 if (ret
== -ENOSPC
&& sbi
->s_journal
) {
2818 * Commit the transaction which would
2819 * free blocks released in the transaction
2822 jbd2_journal_force_commit_nested(sbi
->s_journal
);
2826 /* Fatal error - ENOMEM, EIO... */
2831 blk_finish_plug(&plug
);
2832 if (!ret
&& !cycled
&& wbc
->nr_to_write
> 0) {
2834 mpd
.last_page
= writeback_index
- 1;
2840 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
2842 * Set the writeback_index so that range_cyclic
2843 * mode will write it back later
2845 mapping
->writeback_index
= mpd
.first_page
;
2848 trace_ext4_writepages_result(inode
, wbc
, ret
,
2849 nr_to_write
- wbc
->nr_to_write
);
2850 percpu_up_read(&sbi
->s_writepages_rwsem
);
2854 static int ext4_dax_writepages(struct address_space
*mapping
,
2855 struct writeback_control
*wbc
)
2858 long nr_to_write
= wbc
->nr_to_write
;
2859 struct inode
*inode
= mapping
->host
;
2860 struct ext4_sb_info
*sbi
= EXT4_SB(mapping
->host
->i_sb
);
2862 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
2865 percpu_down_read(&sbi
->s_writepages_rwsem
);
2866 trace_ext4_writepages(inode
, wbc
);
2868 ret
= dax_writeback_mapping_range(mapping
, sbi
->s_daxdev
, wbc
);
2869 trace_ext4_writepages_result(inode
, wbc
, ret
,
2870 nr_to_write
- wbc
->nr_to_write
);
2871 percpu_up_read(&sbi
->s_writepages_rwsem
);
2875 static int ext4_nonda_switch(struct super_block
*sb
)
2877 s64 free_clusters
, dirty_clusters
;
2878 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2881 * switch to non delalloc mode if we are running low
2882 * on free block. The free block accounting via percpu
2883 * counters can get slightly wrong with percpu_counter_batch getting
2884 * accumulated on each CPU without updating global counters
2885 * Delalloc need an accurate free block accounting. So switch
2886 * to non delalloc when we are near to error range.
2889 percpu_counter_read_positive(&sbi
->s_freeclusters_counter
);
2891 percpu_counter_read_positive(&sbi
->s_dirtyclusters_counter
);
2893 * Start pushing delalloc when 1/2 of free blocks are dirty.
2895 if (dirty_clusters
&& (free_clusters
< 2 * dirty_clusters
))
2896 try_to_writeback_inodes_sb(sb
, WB_REASON_FS_FREE_SPACE
);
2898 if (2 * free_clusters
< 3 * dirty_clusters
||
2899 free_clusters
< (dirty_clusters
+ EXT4_FREECLUSTERS_WATERMARK
)) {
2901 * free block count is less than 150% of dirty blocks
2902 * or free blocks is less than watermark
2909 /* We always reserve for an inode update; the superblock could be there too */
2910 static int ext4_da_write_credits(struct inode
*inode
, loff_t pos
, unsigned len
)
2912 if (likely(ext4_has_feature_large_file(inode
->i_sb
)))
2915 if (pos
+ len
<= 0x7fffffffULL
)
2918 /* We might need to update the superblock to set LARGE_FILE */
2922 static int ext4_da_write_begin(struct file
*file
, struct address_space
*mapping
,
2923 loff_t pos
, unsigned len
, unsigned flags
,
2924 struct page
**pagep
, void **fsdata
)
2926 int ret
, retries
= 0;
2929 struct inode
*inode
= mapping
->host
;
2932 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
2935 index
= pos
>> PAGE_SHIFT
;
2937 if (ext4_nonda_switch(inode
->i_sb
) || S_ISLNK(inode
->i_mode
) ||
2938 ext4_verity_in_progress(inode
)) {
2939 *fsdata
= (void *)FALL_BACK_TO_NONDELALLOC
;
2940 return ext4_write_begin(file
, mapping
, pos
,
2941 len
, flags
, pagep
, fsdata
);
2943 *fsdata
= (void *)0;
2944 trace_ext4_da_write_begin(inode
, pos
, len
, flags
);
2946 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
2947 ret
= ext4_da_write_inline_data_begin(mapping
, inode
,
2957 * grab_cache_page_write_begin() can take a long time if the
2958 * system is thrashing due to memory pressure, or if the page
2959 * is being written back. So grab it first before we start
2960 * the transaction handle. This also allows us to allocate
2961 * the page (if needed) without using GFP_NOFS.
2964 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
2970 * With delayed allocation, we don't log the i_disksize update
2971 * if there is delayed block allocation. But we still need
2972 * to journalling the i_disksize update if writes to the end
2973 * of file which has an already mapped buffer.
2976 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
2977 ext4_da_write_credits(inode
, pos
, len
));
2978 if (IS_ERR(handle
)) {
2980 return PTR_ERR(handle
);
2984 if (page
->mapping
!= mapping
) {
2985 /* The page got truncated from under us */
2988 ext4_journal_stop(handle
);
2991 /* In case writeback began while the page was unlocked */
2992 wait_for_stable_page(page
);
2994 #ifdef CONFIG_FS_ENCRYPTION
2995 ret
= ext4_block_write_begin(page
, pos
, len
,
2996 ext4_da_get_block_prep
);
2998 ret
= __block_write_begin(page
, pos
, len
, ext4_da_get_block_prep
);
3002 ext4_journal_stop(handle
);
3004 * block_write_begin may have instantiated a few blocks
3005 * outside i_size. Trim these off again. Don't need
3006 * i_size_read because we hold i_mutex.
3008 if (pos
+ len
> inode
->i_size
)
3009 ext4_truncate_failed_write(inode
);
3011 if (ret
== -ENOSPC
&&
3012 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
3024 * Check if we should update i_disksize
3025 * when write to the end of file but not require block allocation
3027 static int ext4_da_should_update_i_disksize(struct page
*page
,
3028 unsigned long offset
)
3030 struct buffer_head
*bh
;
3031 struct inode
*inode
= page
->mapping
->host
;
3035 bh
= page_buffers(page
);
3036 idx
= offset
>> inode
->i_blkbits
;
3038 for (i
= 0; i
< idx
; i
++)
3039 bh
= bh
->b_this_page
;
3041 if (!buffer_mapped(bh
) || (buffer_delay(bh
)) || buffer_unwritten(bh
))
3046 static int ext4_da_write_end(struct file
*file
,
3047 struct address_space
*mapping
,
3048 loff_t pos
, unsigned len
, unsigned copied
,
3049 struct page
*page
, void *fsdata
)
3051 struct inode
*inode
= mapping
->host
;
3053 handle_t
*handle
= ext4_journal_current_handle();
3055 unsigned long start
, end
;
3056 int write_mode
= (int)(unsigned long)fsdata
;
3058 if (write_mode
== FALL_BACK_TO_NONDELALLOC
)
3059 return ext4_write_end(file
, mapping
, pos
,
3060 len
, copied
, page
, fsdata
);
3062 trace_ext4_da_write_end(inode
, pos
, len
, copied
);
3063 start
= pos
& (PAGE_SIZE
- 1);
3064 end
= start
+ copied
- 1;
3067 * generic_write_end() will run mark_inode_dirty() if i_size
3068 * changes. So let's piggyback the i_disksize mark_inode_dirty
3071 new_i_size
= pos
+ copied
;
3072 if (copied
&& new_i_size
> EXT4_I(inode
)->i_disksize
) {
3073 if (ext4_has_inline_data(inode
) ||
3074 ext4_da_should_update_i_disksize(page
, end
)) {
3075 ext4_update_i_disksize(inode
, new_i_size
);
3076 /* We need to mark inode dirty even if
3077 * new_i_size is less that inode->i_size
3078 * bu greater than i_disksize.(hint delalloc)
3080 ext4_mark_inode_dirty(handle
, inode
);
3084 if (write_mode
!= CONVERT_INLINE_DATA
&&
3085 ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
) &&
3086 ext4_has_inline_data(inode
))
3087 ret2
= ext4_da_write_inline_data_end(inode
, pos
, len
, copied
,
3090 ret2
= generic_write_end(file
, mapping
, pos
, len
, copied
,
3096 ret2
= ext4_journal_stop(handle
);
3100 return ret
? ret
: copied
;
3104 * Force all delayed allocation blocks to be allocated for a given inode.
3106 int ext4_alloc_da_blocks(struct inode
*inode
)
3108 trace_ext4_alloc_da_blocks(inode
);
3110 if (!EXT4_I(inode
)->i_reserved_data_blocks
)
3114 * We do something simple for now. The filemap_flush() will
3115 * also start triggering a write of the data blocks, which is
3116 * not strictly speaking necessary (and for users of
3117 * laptop_mode, not even desirable). However, to do otherwise
3118 * would require replicating code paths in:
3120 * ext4_writepages() ->
3121 * write_cache_pages() ---> (via passed in callback function)
3122 * __mpage_da_writepage() -->
3123 * mpage_add_bh_to_extent()
3124 * mpage_da_map_blocks()
3126 * The problem is that write_cache_pages(), located in
3127 * mm/page-writeback.c, marks pages clean in preparation for
3128 * doing I/O, which is not desirable if we're not planning on
3131 * We could call write_cache_pages(), and then redirty all of
3132 * the pages by calling redirty_page_for_writepage() but that
3133 * would be ugly in the extreme. So instead we would need to
3134 * replicate parts of the code in the above functions,
3135 * simplifying them because we wouldn't actually intend to
3136 * write out the pages, but rather only collect contiguous
3137 * logical block extents, call the multi-block allocator, and
3138 * then update the buffer heads with the block allocations.
3140 * For now, though, we'll cheat by calling filemap_flush(),
3141 * which will map the blocks, and start the I/O, but not
3142 * actually wait for the I/O to complete.
3144 return filemap_flush(inode
->i_mapping
);
3148 * bmap() is special. It gets used by applications such as lilo and by
3149 * the swapper to find the on-disk block of a specific piece of data.
3151 * Naturally, this is dangerous if the block concerned is still in the
3152 * journal. If somebody makes a swapfile on an ext4 data-journaling
3153 * filesystem and enables swap, then they may get a nasty shock when the
3154 * data getting swapped to that swapfile suddenly gets overwritten by
3155 * the original zero's written out previously to the journal and
3156 * awaiting writeback in the kernel's buffer cache.
3158 * So, if we see any bmap calls here on a modified, data-journaled file,
3159 * take extra steps to flush any blocks which might be in the cache.
3161 static sector_t
ext4_bmap(struct address_space
*mapping
, sector_t block
)
3163 struct inode
*inode
= mapping
->host
;
3168 * We can get here for an inline file via the FIBMAP ioctl
3170 if (ext4_has_inline_data(inode
))
3173 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
) &&
3174 test_opt(inode
->i_sb
, DELALLOC
)) {
3176 * With delalloc we want to sync the file
3177 * so that we can make sure we allocate
3180 filemap_write_and_wait(mapping
);
3183 if (EXT4_JOURNAL(inode
) &&
3184 ext4_test_inode_state(inode
, EXT4_STATE_JDATA
)) {
3186 * This is a REALLY heavyweight approach, but the use of
3187 * bmap on dirty files is expected to be extremely rare:
3188 * only if we run lilo or swapon on a freshly made file
3189 * do we expect this to happen.
3191 * (bmap requires CAP_SYS_RAWIO so this does not
3192 * represent an unprivileged user DOS attack --- we'd be
3193 * in trouble if mortal users could trigger this path at
3196 * NB. EXT4_STATE_JDATA is not set on files other than
3197 * regular files. If somebody wants to bmap a directory
3198 * or symlink and gets confused because the buffer
3199 * hasn't yet been flushed to disk, they deserve
3200 * everything they get.
3203 ext4_clear_inode_state(inode
, EXT4_STATE_JDATA
);
3204 journal
= EXT4_JOURNAL(inode
);
3205 jbd2_journal_lock_updates(journal
);
3206 err
= jbd2_journal_flush(journal
);
3207 jbd2_journal_unlock_updates(journal
);
3213 return iomap_bmap(mapping
, block
, &ext4_iomap_ops
);
3216 static int ext4_readpage(struct file
*file
, struct page
*page
)
3219 struct inode
*inode
= page
->mapping
->host
;
3221 trace_ext4_readpage(page
);
3223 if (ext4_has_inline_data(inode
))
3224 ret
= ext4_readpage_inline(inode
, page
);
3227 return ext4_mpage_readpages(page
->mapping
, NULL
, page
, 1,
3234 ext4_readpages(struct file
*file
, struct address_space
*mapping
,
3235 struct list_head
*pages
, unsigned nr_pages
)
3237 struct inode
*inode
= mapping
->host
;
3239 /* If the file has inline data, no need to do readpages. */
3240 if (ext4_has_inline_data(inode
))
3243 return ext4_mpage_readpages(mapping
, pages
, NULL
, nr_pages
, true);
3246 static void ext4_invalidatepage(struct page
*page
, unsigned int offset
,
3247 unsigned int length
)
3249 trace_ext4_invalidatepage(page
, offset
, length
);
3251 /* No journalling happens on data buffers when this function is used */
3252 WARN_ON(page_has_buffers(page
) && buffer_jbd(page_buffers(page
)));
3254 block_invalidatepage(page
, offset
, length
);
3257 static int __ext4_journalled_invalidatepage(struct page
*page
,
3258 unsigned int offset
,
3259 unsigned int length
)
3261 journal_t
*journal
= EXT4_JOURNAL(page
->mapping
->host
);
3263 trace_ext4_journalled_invalidatepage(page
, offset
, length
);
3266 * If it's a full truncate we just forget about the pending dirtying
3268 if (offset
== 0 && length
== PAGE_SIZE
)
3269 ClearPageChecked(page
);
3271 return jbd2_journal_invalidatepage(journal
, page
, offset
, length
);
3274 /* Wrapper for aops... */
3275 static void ext4_journalled_invalidatepage(struct page
*page
,
3276 unsigned int offset
,
3277 unsigned int length
)
3279 WARN_ON(__ext4_journalled_invalidatepage(page
, offset
, length
) < 0);
3282 static int ext4_releasepage(struct page
*page
, gfp_t wait
)
3284 journal_t
*journal
= EXT4_JOURNAL(page
->mapping
->host
);
3286 trace_ext4_releasepage(page
);
3288 /* Page has dirty journalled data -> cannot release */
3289 if (PageChecked(page
))
3292 return jbd2_journal_try_to_free_buffers(journal
, page
, wait
);
3294 return try_to_free_buffers(page
);
3297 static bool ext4_inode_datasync_dirty(struct inode
*inode
)
3299 journal_t
*journal
= EXT4_SB(inode
->i_sb
)->s_journal
;
3302 return !jbd2_transaction_committed(journal
,
3303 EXT4_I(inode
)->i_datasync_tid
);
3304 /* Any metadata buffers to write? */
3305 if (!list_empty(&inode
->i_mapping
->private_list
))
3307 return inode
->i_state
& I_DIRTY_DATASYNC
;
3310 static void ext4_set_iomap(struct inode
*inode
, struct iomap
*iomap
,
3311 struct ext4_map_blocks
*map
, loff_t offset
,
3314 u8 blkbits
= inode
->i_blkbits
;
3317 * Writes that span EOF might trigger an I/O size update on completion,
3318 * so consider them to be dirty for the purpose of O_DSYNC, even if
3319 * there is no other metadata changes being made or are pending.
3322 if (ext4_inode_datasync_dirty(inode
) ||
3323 offset
+ length
> i_size_read(inode
))
3324 iomap
->flags
|= IOMAP_F_DIRTY
;
3326 if (map
->m_flags
& EXT4_MAP_NEW
)
3327 iomap
->flags
|= IOMAP_F_NEW
;
3329 iomap
->bdev
= inode
->i_sb
->s_bdev
;
3330 iomap
->dax_dev
= EXT4_SB(inode
->i_sb
)->s_daxdev
;
3331 iomap
->offset
= (u64
) map
->m_lblk
<< blkbits
;
3332 iomap
->length
= (u64
) map
->m_len
<< blkbits
;
3334 if ((map
->m_flags
& EXT4_MAP_MAPPED
) &&
3335 !ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3336 iomap
->flags
|= IOMAP_F_MERGED
;
3339 * Flags passed to ext4_map_blocks() for direct I/O writes can result
3340 * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits
3341 * set. In order for any allocated unwritten extents to be converted
3342 * into written extents correctly within the ->end_io() handler, we
3343 * need to ensure that the iomap->type is set appropriately. Hence, the
3344 * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has
3347 if (map
->m_flags
& EXT4_MAP_UNWRITTEN
) {
3348 iomap
->type
= IOMAP_UNWRITTEN
;
3349 iomap
->addr
= (u64
) map
->m_pblk
<< blkbits
;
3350 } else if (map
->m_flags
& EXT4_MAP_MAPPED
) {
3351 iomap
->type
= IOMAP_MAPPED
;
3352 iomap
->addr
= (u64
) map
->m_pblk
<< blkbits
;
3354 iomap
->type
= IOMAP_HOLE
;
3355 iomap
->addr
= IOMAP_NULL_ADDR
;
3359 static int ext4_iomap_alloc(struct inode
*inode
, struct ext4_map_blocks
*map
,
3363 u8 blkbits
= inode
->i_blkbits
;
3364 int ret
, dio_credits
, m_flags
= 0, retries
= 0;
3367 * Trim the mapping request to the maximum value that we can map at
3368 * once for direct I/O.
3370 if (map
->m_len
> DIO_MAX_BLOCKS
)
3371 map
->m_len
= DIO_MAX_BLOCKS
;
3372 dio_credits
= ext4_chunk_trans_blocks(inode
, map
->m_len
);
3376 * Either we allocate blocks and then don't get an unwritten extent, so
3377 * in that case we have reserved enough credits. Or, the blocks are
3378 * already allocated and unwritten. In that case, the extent conversion
3379 * fits into the credits as well.
3381 handle
= ext4_journal_start(inode
, EXT4_HT_MAP_BLOCKS
, dio_credits
);
3383 return PTR_ERR(handle
);
3386 * DAX and direct I/O are the only two operations that are currently
3387 * supported with IOMAP_WRITE.
3389 WARN_ON(!IS_DAX(inode
) && !(flags
& IOMAP_DIRECT
));
3391 m_flags
= EXT4_GET_BLOCKS_CREATE_ZERO
;
3393 * We use i_size instead of i_disksize here because delalloc writeback
3394 * can complete at any point during the I/O and subsequently push the
3395 * i_disksize out to i_size. This could be beyond where direct I/O is
3396 * happening and thus expose allocated blocks to direct I/O reads.
3398 else if ((map
->m_lblk
* (1 << blkbits
)) >= i_size_read(inode
))
3399 m_flags
= EXT4_GET_BLOCKS_CREATE
;
3400 else if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
3401 m_flags
= EXT4_GET_BLOCKS_IO_CREATE_EXT
;
3403 ret
= ext4_map_blocks(handle
, inode
, map
, m_flags
);
3406 * We cannot fill holes in indirect tree based inodes as that could
3407 * expose stale data in the case of a crash. Use the magic error code
3408 * to fallback to buffered I/O.
3410 if (!m_flags
&& !ret
)
3413 ext4_journal_stop(handle
);
3414 if (ret
== -ENOSPC
&& ext4_should_retry_alloc(inode
->i_sb
, &retries
))
3421 static int ext4_iomap_begin(struct inode
*inode
, loff_t offset
, loff_t length
,
3422 unsigned flags
, struct iomap
*iomap
, struct iomap
*srcmap
)
3425 struct ext4_map_blocks map
;
3426 u8 blkbits
= inode
->i_blkbits
;
3428 if ((offset
>> blkbits
) > EXT4_MAX_LOGICAL_BLOCK
)
3431 if (WARN_ON_ONCE(ext4_has_inline_data(inode
)))
3435 * Calculate the first and last logical blocks respectively.
3437 map
.m_lblk
= offset
>> blkbits
;
3438 map
.m_len
= min_t(loff_t
, (offset
+ length
- 1) >> blkbits
,
3439 EXT4_MAX_LOGICAL_BLOCK
) - map
.m_lblk
+ 1;
3441 if (flags
& IOMAP_WRITE
)
3442 ret
= ext4_iomap_alloc(inode
, &map
, flags
);
3444 ret
= ext4_map_blocks(NULL
, inode
, &map
, 0);
3449 ext4_set_iomap(inode
, iomap
, &map
, offset
, length
);
3454 static int ext4_iomap_overwrite_begin(struct inode
*inode
, loff_t offset
,
3455 loff_t length
, unsigned flags
, struct iomap
*iomap
,
3456 struct iomap
*srcmap
)
3461 * Even for writes we don't need to allocate blocks, so just pretend
3462 * we are reading to save overhead of starting a transaction.
3464 flags
&= ~IOMAP_WRITE
;
3465 ret
= ext4_iomap_begin(inode
, offset
, length
, flags
, iomap
, srcmap
);
3466 WARN_ON_ONCE(iomap
->type
!= IOMAP_MAPPED
);
3470 static int ext4_iomap_end(struct inode
*inode
, loff_t offset
, loff_t length
,
3471 ssize_t written
, unsigned flags
, struct iomap
*iomap
)
3474 * Check to see whether an error occurred while writing out the data to
3475 * the allocated blocks. If so, return the magic error code so that we
3476 * fallback to buffered I/O and attempt to complete the remainder of
3477 * the I/O. Any blocks that may have been allocated in preparation for
3478 * the direct I/O will be reused during buffered I/O.
3480 if (flags
& (IOMAP_WRITE
| IOMAP_DIRECT
) && written
== 0)
3486 const struct iomap_ops ext4_iomap_ops
= {
3487 .iomap_begin
= ext4_iomap_begin
,
3488 .iomap_end
= ext4_iomap_end
,
3491 const struct iomap_ops ext4_iomap_overwrite_ops
= {
3492 .iomap_begin
= ext4_iomap_overwrite_begin
,
3493 .iomap_end
= ext4_iomap_end
,
3496 static bool ext4_iomap_is_delalloc(struct inode
*inode
,
3497 struct ext4_map_blocks
*map
)
3499 struct extent_status es
;
3500 ext4_lblk_t offset
= 0, end
= map
->m_lblk
+ map
->m_len
- 1;
3502 ext4_es_find_extent_range(inode
, &ext4_es_is_delayed
,
3503 map
->m_lblk
, end
, &es
);
3505 if (!es
.es_len
|| es
.es_lblk
> end
)
3508 if (es
.es_lblk
> map
->m_lblk
) {
3509 map
->m_len
= es
.es_lblk
- map
->m_lblk
;
3513 offset
= map
->m_lblk
- es
.es_lblk
;
3514 map
->m_len
= es
.es_len
- offset
;
3519 static int ext4_iomap_begin_report(struct inode
*inode
, loff_t offset
,
3520 loff_t length
, unsigned int flags
,
3521 struct iomap
*iomap
, struct iomap
*srcmap
)
3524 bool delalloc
= false;
3525 struct ext4_map_blocks map
;
3526 u8 blkbits
= inode
->i_blkbits
;
3528 if ((offset
>> blkbits
) > EXT4_MAX_LOGICAL_BLOCK
)
3531 if (ext4_has_inline_data(inode
)) {
3532 ret
= ext4_inline_data_iomap(inode
, iomap
);
3533 if (ret
!= -EAGAIN
) {
3534 if (ret
== 0 && offset
>= iomap
->length
)
3541 * Calculate the first and last logical block respectively.
3543 map
.m_lblk
= offset
>> blkbits
;
3544 map
.m_len
= min_t(loff_t
, (offset
+ length
- 1) >> blkbits
,
3545 EXT4_MAX_LOGICAL_BLOCK
) - map
.m_lblk
+ 1;
3548 * Fiemap callers may call for offset beyond s_bitmap_maxbytes.
3549 * So handle it here itself instead of querying ext4_map_blocks().
3550 * Since ext4_map_blocks() will warn about it and will return
3553 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))) {
3554 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
3556 if (offset
>= sbi
->s_bitmap_maxbytes
) {
3562 ret
= ext4_map_blocks(NULL
, inode
, &map
, 0);
3566 delalloc
= ext4_iomap_is_delalloc(inode
, &map
);
3569 ext4_set_iomap(inode
, iomap
, &map
, offset
, length
);
3570 if (delalloc
&& iomap
->type
== IOMAP_HOLE
)
3571 iomap
->type
= IOMAP_DELALLOC
;
3576 const struct iomap_ops ext4_iomap_report_ops
= {
3577 .iomap_begin
= ext4_iomap_begin_report
,
3581 * Pages can be marked dirty completely asynchronously from ext4's journalling
3582 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3583 * much here because ->set_page_dirty is called under VFS locks. The page is
3584 * not necessarily locked.
3586 * We cannot just dirty the page and leave attached buffers clean, because the
3587 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3588 * or jbddirty because all the journalling code will explode.
3590 * So what we do is to mark the page "pending dirty" and next time writepage
3591 * is called, propagate that into the buffers appropriately.
3593 static int ext4_journalled_set_page_dirty(struct page
*page
)
3595 SetPageChecked(page
);
3596 return __set_page_dirty_nobuffers(page
);
3599 static int ext4_set_page_dirty(struct page
*page
)
3601 WARN_ON_ONCE(!PageLocked(page
) && !PageDirty(page
));
3602 WARN_ON_ONCE(!page_has_buffers(page
));
3603 return __set_page_dirty_buffers(page
);
3606 static const struct address_space_operations ext4_aops
= {
3607 .readpage
= ext4_readpage
,
3608 .readpages
= ext4_readpages
,
3609 .writepage
= ext4_writepage
,
3610 .writepages
= ext4_writepages
,
3611 .write_begin
= ext4_write_begin
,
3612 .write_end
= ext4_write_end
,
3613 .set_page_dirty
= ext4_set_page_dirty
,
3615 .invalidatepage
= ext4_invalidatepage
,
3616 .releasepage
= ext4_releasepage
,
3617 .direct_IO
= noop_direct_IO
,
3618 .migratepage
= buffer_migrate_page
,
3619 .is_partially_uptodate
= block_is_partially_uptodate
,
3620 .error_remove_page
= generic_error_remove_page
,
3623 static const struct address_space_operations ext4_journalled_aops
= {
3624 .readpage
= ext4_readpage
,
3625 .readpages
= ext4_readpages
,
3626 .writepage
= ext4_writepage
,
3627 .writepages
= ext4_writepages
,
3628 .write_begin
= ext4_write_begin
,
3629 .write_end
= ext4_journalled_write_end
,
3630 .set_page_dirty
= ext4_journalled_set_page_dirty
,
3632 .invalidatepage
= ext4_journalled_invalidatepage
,
3633 .releasepage
= ext4_releasepage
,
3634 .direct_IO
= noop_direct_IO
,
3635 .is_partially_uptodate
= block_is_partially_uptodate
,
3636 .error_remove_page
= generic_error_remove_page
,
3639 static const struct address_space_operations ext4_da_aops
= {
3640 .readpage
= ext4_readpage
,
3641 .readpages
= ext4_readpages
,
3642 .writepage
= ext4_writepage
,
3643 .writepages
= ext4_writepages
,
3644 .write_begin
= ext4_da_write_begin
,
3645 .write_end
= ext4_da_write_end
,
3646 .set_page_dirty
= ext4_set_page_dirty
,
3648 .invalidatepage
= ext4_invalidatepage
,
3649 .releasepage
= ext4_releasepage
,
3650 .direct_IO
= noop_direct_IO
,
3651 .migratepage
= buffer_migrate_page
,
3652 .is_partially_uptodate
= block_is_partially_uptodate
,
3653 .error_remove_page
= generic_error_remove_page
,
3656 static const struct address_space_operations ext4_dax_aops
= {
3657 .writepages
= ext4_dax_writepages
,
3658 .direct_IO
= noop_direct_IO
,
3659 .set_page_dirty
= noop_set_page_dirty
,
3661 .invalidatepage
= noop_invalidatepage
,
3664 void ext4_set_aops(struct inode
*inode
)
3666 switch (ext4_inode_journal_mode(inode
)) {
3667 case EXT4_INODE_ORDERED_DATA_MODE
:
3668 case EXT4_INODE_WRITEBACK_DATA_MODE
:
3670 case EXT4_INODE_JOURNAL_DATA_MODE
:
3671 inode
->i_mapping
->a_ops
= &ext4_journalled_aops
;
3677 inode
->i_mapping
->a_ops
= &ext4_dax_aops
;
3678 else if (test_opt(inode
->i_sb
, DELALLOC
))
3679 inode
->i_mapping
->a_ops
= &ext4_da_aops
;
3681 inode
->i_mapping
->a_ops
= &ext4_aops
;
3684 static int __ext4_block_zero_page_range(handle_t
*handle
,
3685 struct address_space
*mapping
, loff_t from
, loff_t length
)
3687 ext4_fsblk_t index
= from
>> PAGE_SHIFT
;
3688 unsigned offset
= from
& (PAGE_SIZE
-1);
3689 unsigned blocksize
, pos
;
3691 struct inode
*inode
= mapping
->host
;
3692 struct buffer_head
*bh
;
3696 page
= find_or_create_page(mapping
, from
>> PAGE_SHIFT
,
3697 mapping_gfp_constraint(mapping
, ~__GFP_FS
));
3701 blocksize
= inode
->i_sb
->s_blocksize
;
3703 iblock
= index
<< (PAGE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
3705 if (!page_has_buffers(page
))
3706 create_empty_buffers(page
, blocksize
, 0);
3708 /* Find the buffer that contains "offset" */
3709 bh
= page_buffers(page
);
3711 while (offset
>= pos
) {
3712 bh
= bh
->b_this_page
;
3716 if (buffer_freed(bh
)) {
3717 BUFFER_TRACE(bh
, "freed: skip");
3720 if (!buffer_mapped(bh
)) {
3721 BUFFER_TRACE(bh
, "unmapped");
3722 ext4_get_block(inode
, iblock
, bh
, 0);
3723 /* unmapped? It's a hole - nothing to do */
3724 if (!buffer_mapped(bh
)) {
3725 BUFFER_TRACE(bh
, "still unmapped");
3730 /* Ok, it's mapped. Make sure it's up-to-date */
3731 if (PageUptodate(page
))
3732 set_buffer_uptodate(bh
);
3734 if (!buffer_uptodate(bh
)) {
3736 ll_rw_block(REQ_OP_READ
, 0, 1, &bh
);
3738 /* Uhhuh. Read error. Complain and punt. */
3739 if (!buffer_uptodate(bh
))
3741 if (S_ISREG(inode
->i_mode
) && IS_ENCRYPTED(inode
)) {
3742 /* We expect the key to be set. */
3743 BUG_ON(!fscrypt_has_encryption_key(inode
));
3744 err
= fscrypt_decrypt_pagecache_blocks(page
, blocksize
,
3747 clear_buffer_uptodate(bh
);
3752 if (ext4_should_journal_data(inode
)) {
3753 BUFFER_TRACE(bh
, "get write access");
3754 err
= ext4_journal_get_write_access(handle
, bh
);
3758 zero_user(page
, offset
, length
);
3759 BUFFER_TRACE(bh
, "zeroed end of block");
3761 if (ext4_should_journal_data(inode
)) {
3762 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
3765 mark_buffer_dirty(bh
);
3766 if (ext4_should_order_data(inode
))
3767 err
= ext4_jbd2_inode_add_write(handle
, inode
, from
,
3778 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3779 * starting from file offset 'from'. The range to be zero'd must
3780 * be contained with in one block. If the specified range exceeds
3781 * the end of the block it will be shortened to end of the block
3782 * that cooresponds to 'from'
3784 static int ext4_block_zero_page_range(handle_t
*handle
,
3785 struct address_space
*mapping
, loff_t from
, loff_t length
)
3787 struct inode
*inode
= mapping
->host
;
3788 unsigned offset
= from
& (PAGE_SIZE
-1);
3789 unsigned blocksize
= inode
->i_sb
->s_blocksize
;
3790 unsigned max
= blocksize
- (offset
& (blocksize
- 1));
3793 * correct length if it does not fall between
3794 * 'from' and the end of the block
3796 if (length
> max
|| length
< 0)
3799 if (IS_DAX(inode
)) {
3800 return iomap_zero_range(inode
, from
, length
, NULL
,
3803 return __ext4_block_zero_page_range(handle
, mapping
, from
, length
);
3807 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3808 * up to the end of the block which corresponds to `from'.
3809 * This required during truncate. We need to physically zero the tail end
3810 * of that block so it doesn't yield old data if the file is later grown.
3812 static int ext4_block_truncate_page(handle_t
*handle
,
3813 struct address_space
*mapping
, loff_t from
)
3815 unsigned offset
= from
& (PAGE_SIZE
-1);
3818 struct inode
*inode
= mapping
->host
;
3820 /* If we are processing an encrypted inode during orphan list handling */
3821 if (IS_ENCRYPTED(inode
) && !fscrypt_has_encryption_key(inode
))
3824 blocksize
= inode
->i_sb
->s_blocksize
;
3825 length
= blocksize
- (offset
& (blocksize
- 1));
3827 return ext4_block_zero_page_range(handle
, mapping
, from
, length
);
3830 int ext4_zero_partial_blocks(handle_t
*handle
, struct inode
*inode
,
3831 loff_t lstart
, loff_t length
)
3833 struct super_block
*sb
= inode
->i_sb
;
3834 struct address_space
*mapping
= inode
->i_mapping
;
3835 unsigned partial_start
, partial_end
;
3836 ext4_fsblk_t start
, end
;
3837 loff_t byte_end
= (lstart
+ length
- 1);
3840 partial_start
= lstart
& (sb
->s_blocksize
- 1);
3841 partial_end
= byte_end
& (sb
->s_blocksize
- 1);
3843 start
= lstart
>> sb
->s_blocksize_bits
;
3844 end
= byte_end
>> sb
->s_blocksize_bits
;
3846 /* Handle partial zero within the single block */
3848 (partial_start
|| (partial_end
!= sb
->s_blocksize
- 1))) {
3849 err
= ext4_block_zero_page_range(handle
, mapping
,
3853 /* Handle partial zero out on the start of the range */
3854 if (partial_start
) {
3855 err
= ext4_block_zero_page_range(handle
, mapping
,
3856 lstart
, sb
->s_blocksize
);
3860 /* Handle partial zero out on the end of the range */
3861 if (partial_end
!= sb
->s_blocksize
- 1)
3862 err
= ext4_block_zero_page_range(handle
, mapping
,
3863 byte_end
- partial_end
,
3868 int ext4_can_truncate(struct inode
*inode
)
3870 if (S_ISREG(inode
->i_mode
))
3872 if (S_ISDIR(inode
->i_mode
))
3874 if (S_ISLNK(inode
->i_mode
))
3875 return !ext4_inode_is_fast_symlink(inode
);
3880 * We have to make sure i_disksize gets properly updated before we truncate
3881 * page cache due to hole punching or zero range. Otherwise i_disksize update
3882 * can get lost as it may have been postponed to submission of writeback but
3883 * that will never happen after we truncate page cache.
3885 int ext4_update_disksize_before_punch(struct inode
*inode
, loff_t offset
,
3889 loff_t size
= i_size_read(inode
);
3891 WARN_ON(!inode_is_locked(inode
));
3892 if (offset
> size
|| offset
+ len
< size
)
3895 if (EXT4_I(inode
)->i_disksize
>= size
)
3898 handle
= ext4_journal_start(inode
, EXT4_HT_MISC
, 1);
3900 return PTR_ERR(handle
);
3901 ext4_update_i_disksize(inode
, size
);
3902 ext4_mark_inode_dirty(handle
, inode
);
3903 ext4_journal_stop(handle
);
3908 static void ext4_wait_dax_page(struct ext4_inode_info
*ei
)
3910 up_write(&ei
->i_mmap_sem
);
3912 down_write(&ei
->i_mmap_sem
);
3915 int ext4_break_layouts(struct inode
*inode
)
3917 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3921 if (WARN_ON_ONCE(!rwsem_is_locked(&ei
->i_mmap_sem
)))
3925 page
= dax_layout_busy_page(inode
->i_mapping
);
3929 error
= ___wait_var_event(&page
->_refcount
,
3930 atomic_read(&page
->_refcount
) == 1,
3931 TASK_INTERRUPTIBLE
, 0, 0,
3932 ext4_wait_dax_page(ei
));
3933 } while (error
== 0);
3939 * ext4_punch_hole: punches a hole in a file by releasing the blocks
3940 * associated with the given offset and length
3942 * @inode: File inode
3943 * @offset: The offset where the hole will begin
3944 * @len: The length of the hole
3946 * Returns: 0 on success or negative on failure
3949 int ext4_punch_hole(struct inode
*inode
, loff_t offset
, loff_t length
)
3951 struct super_block
*sb
= inode
->i_sb
;
3952 ext4_lblk_t first_block
, stop_block
;
3953 struct address_space
*mapping
= inode
->i_mapping
;
3954 loff_t first_block_offset
, last_block_offset
;
3956 unsigned int credits
;
3959 trace_ext4_punch_hole(inode
, offset
, length
, 0);
3961 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
3962 if (ext4_has_inline_data(inode
)) {
3963 down_write(&EXT4_I(inode
)->i_mmap_sem
);
3964 ret
= ext4_convert_inline_data(inode
);
3965 up_write(&EXT4_I(inode
)->i_mmap_sem
);
3971 * Write out all dirty pages to avoid race conditions
3972 * Then release them.
3974 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
)) {
3975 ret
= filemap_write_and_wait_range(mapping
, offset
,
3976 offset
+ length
- 1);
3983 /* No need to punch hole beyond i_size */
3984 if (offset
>= inode
->i_size
)
3988 * If the hole extends beyond i_size, set the hole
3989 * to end after the page that contains i_size
3991 if (offset
+ length
> inode
->i_size
) {
3992 length
= inode
->i_size
+
3993 PAGE_SIZE
- (inode
->i_size
& (PAGE_SIZE
- 1)) -
3997 if (offset
& (sb
->s_blocksize
- 1) ||
3998 (offset
+ length
) & (sb
->s_blocksize
- 1)) {
4000 * Attach jinode to inode for jbd2 if we do any zeroing of
4003 ret
= ext4_inode_attach_jinode(inode
);
4009 /* Wait all existing dio workers, newcomers will block on i_mutex */
4010 inode_dio_wait(inode
);
4013 * Prevent page faults from reinstantiating pages we have released from
4016 down_write(&EXT4_I(inode
)->i_mmap_sem
);
4018 ret
= ext4_break_layouts(inode
);
4022 first_block_offset
= round_up(offset
, sb
->s_blocksize
);
4023 last_block_offset
= round_down((offset
+ length
), sb
->s_blocksize
) - 1;
4025 /* Now release the pages and zero block aligned part of pages*/
4026 if (last_block_offset
> first_block_offset
) {
4027 ret
= ext4_update_disksize_before_punch(inode
, offset
, length
);
4030 truncate_pagecache_range(inode
, first_block_offset
,
4034 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
4035 credits
= ext4_writepage_trans_blocks(inode
);
4037 credits
= ext4_blocks_for_truncate(inode
);
4038 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
4039 if (IS_ERR(handle
)) {
4040 ret
= PTR_ERR(handle
);
4041 ext4_std_error(sb
, ret
);
4045 ret
= ext4_zero_partial_blocks(handle
, inode
, offset
,
4050 first_block
= (offset
+ sb
->s_blocksize
- 1) >>
4051 EXT4_BLOCK_SIZE_BITS(sb
);
4052 stop_block
= (offset
+ length
) >> EXT4_BLOCK_SIZE_BITS(sb
);
4054 /* If there are blocks to remove, do it */
4055 if (stop_block
> first_block
) {
4057 down_write(&EXT4_I(inode
)->i_data_sem
);
4058 ext4_discard_preallocations(inode
);
4060 ret
= ext4_es_remove_extent(inode
, first_block
,
4061 stop_block
- first_block
);
4063 up_write(&EXT4_I(inode
)->i_data_sem
);
4067 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
4068 ret
= ext4_ext_remove_space(inode
, first_block
,
4071 ret
= ext4_ind_remove_space(handle
, inode
, first_block
,
4074 up_write(&EXT4_I(inode
)->i_data_sem
);
4077 ext4_handle_sync(handle
);
4079 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
4080 ext4_mark_inode_dirty(handle
, inode
);
4082 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4084 ext4_journal_stop(handle
);
4086 up_write(&EXT4_I(inode
)->i_mmap_sem
);
4088 inode_unlock(inode
);
4092 int ext4_inode_attach_jinode(struct inode
*inode
)
4094 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4095 struct jbd2_inode
*jinode
;
4097 if (ei
->jinode
|| !EXT4_SB(inode
->i_sb
)->s_journal
)
4100 jinode
= jbd2_alloc_inode(GFP_KERNEL
);
4101 spin_lock(&inode
->i_lock
);
4104 spin_unlock(&inode
->i_lock
);
4107 ei
->jinode
= jinode
;
4108 jbd2_journal_init_jbd_inode(ei
->jinode
, inode
);
4111 spin_unlock(&inode
->i_lock
);
4112 if (unlikely(jinode
!= NULL
))
4113 jbd2_free_inode(jinode
);
4120 * We block out ext4_get_block() block instantiations across the entire
4121 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
4122 * simultaneously on behalf of the same inode.
4124 * As we work through the truncate and commit bits of it to the journal there
4125 * is one core, guiding principle: the file's tree must always be consistent on
4126 * disk. We must be able to restart the truncate after a crash.
4128 * The file's tree may be transiently inconsistent in memory (although it
4129 * probably isn't), but whenever we close off and commit a journal transaction,
4130 * the contents of (the filesystem + the journal) must be consistent and
4131 * restartable. It's pretty simple, really: bottom up, right to left (although
4132 * left-to-right works OK too).
4134 * Note that at recovery time, journal replay occurs *before* the restart of
4135 * truncate against the orphan inode list.
4137 * The committed inode has the new, desired i_size (which is the same as
4138 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
4139 * that this inode's truncate did not complete and it will again call
4140 * ext4_truncate() to have another go. So there will be instantiated blocks
4141 * to the right of the truncation point in a crashed ext4 filesystem. But
4142 * that's fine - as long as they are linked from the inode, the post-crash
4143 * ext4_truncate() run will find them and release them.
4145 int ext4_truncate(struct inode
*inode
)
4147 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4148 unsigned int credits
;
4151 struct address_space
*mapping
= inode
->i_mapping
;
4154 * There is a possibility that we're either freeing the inode
4155 * or it's a completely new inode. In those cases we might not
4156 * have i_mutex locked because it's not necessary.
4158 if (!(inode
->i_state
& (I_NEW
|I_FREEING
)))
4159 WARN_ON(!inode_is_locked(inode
));
4160 trace_ext4_truncate_enter(inode
);
4162 if (!ext4_can_truncate(inode
))
4165 if (inode
->i_size
== 0 && !test_opt(inode
->i_sb
, NO_AUTO_DA_ALLOC
))
4166 ext4_set_inode_state(inode
, EXT4_STATE_DA_ALLOC_CLOSE
);
4168 if (ext4_has_inline_data(inode
)) {
4171 err
= ext4_inline_data_truncate(inode
, &has_inline
);
4178 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
4179 if (inode
->i_size
& (inode
->i_sb
->s_blocksize
- 1)) {
4180 if (ext4_inode_attach_jinode(inode
) < 0)
4184 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
4185 credits
= ext4_writepage_trans_blocks(inode
);
4187 credits
= ext4_blocks_for_truncate(inode
);
4189 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
4191 return PTR_ERR(handle
);
4193 if (inode
->i_size
& (inode
->i_sb
->s_blocksize
- 1))
4194 ext4_block_truncate_page(handle
, mapping
, inode
->i_size
);
4197 * We add the inode to the orphan list, so that if this
4198 * truncate spans multiple transactions, and we crash, we will
4199 * resume the truncate when the filesystem recovers. It also
4200 * marks the inode dirty, to catch the new size.
4202 * Implication: the file must always be in a sane, consistent
4203 * truncatable state while each transaction commits.
4205 err
= ext4_orphan_add(handle
, inode
);
4209 down_write(&EXT4_I(inode
)->i_data_sem
);
4211 ext4_discard_preallocations(inode
);
4213 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
4214 err
= ext4_ext_truncate(handle
, inode
);
4216 ext4_ind_truncate(handle
, inode
);
4218 up_write(&ei
->i_data_sem
);
4223 ext4_handle_sync(handle
);
4227 * If this was a simple ftruncate() and the file will remain alive,
4228 * then we need to clear up the orphan record which we created above.
4229 * However, if this was a real unlink then we were called by
4230 * ext4_evict_inode(), and we allow that function to clean up the
4231 * orphan info for us.
4234 ext4_orphan_del(handle
, inode
);
4236 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
4237 ext4_mark_inode_dirty(handle
, inode
);
4238 ext4_journal_stop(handle
);
4240 trace_ext4_truncate_exit(inode
);
4245 * ext4_get_inode_loc returns with an extra refcount against the inode's
4246 * underlying buffer_head on success. If 'in_mem' is true, we have all
4247 * data in memory that is needed to recreate the on-disk version of this
4250 static int __ext4_get_inode_loc(struct inode
*inode
,
4251 struct ext4_iloc
*iloc
, int in_mem
)
4253 struct ext4_group_desc
*gdp
;
4254 struct buffer_head
*bh
;
4255 struct super_block
*sb
= inode
->i_sb
;
4257 struct blk_plug plug
;
4258 int inodes_per_block
, inode_offset
;
4261 if (inode
->i_ino
< EXT4_ROOT_INO
||
4262 inode
->i_ino
> le32_to_cpu(EXT4_SB(sb
)->s_es
->s_inodes_count
))
4263 return -EFSCORRUPTED
;
4265 iloc
->block_group
= (inode
->i_ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
4266 gdp
= ext4_get_group_desc(sb
, iloc
->block_group
, NULL
);
4271 * Figure out the offset within the block group inode table
4273 inodes_per_block
= EXT4_SB(sb
)->s_inodes_per_block
;
4274 inode_offset
= ((inode
->i_ino
- 1) %
4275 EXT4_INODES_PER_GROUP(sb
));
4276 block
= ext4_inode_table(sb
, gdp
) + (inode_offset
/ inodes_per_block
);
4277 iloc
->offset
= (inode_offset
% inodes_per_block
) * EXT4_INODE_SIZE(sb
);
4279 bh
= sb_getblk(sb
, block
);
4282 if (ext4_simulate_fail(sb
, EXT4_SIM_INODE_EIO
))
4284 if (!buffer_uptodate(bh
)) {
4288 * If the buffer has the write error flag, we have failed
4289 * to write out another inode in the same block. In this
4290 * case, we don't have to read the block because we may
4291 * read the old inode data successfully.
4293 if (buffer_write_io_error(bh
) && !buffer_uptodate(bh
))
4294 set_buffer_uptodate(bh
);
4296 if (buffer_uptodate(bh
)) {
4297 /* someone brought it uptodate while we waited */
4303 * If we have all information of the inode in memory and this
4304 * is the only valid inode in the block, we need not read the
4308 struct buffer_head
*bitmap_bh
;
4311 start
= inode_offset
& ~(inodes_per_block
- 1);
4313 /* Is the inode bitmap in cache? */
4314 bitmap_bh
= sb_getblk(sb
, ext4_inode_bitmap(sb
, gdp
));
4315 if (unlikely(!bitmap_bh
))
4319 * If the inode bitmap isn't in cache then the
4320 * optimisation may end up performing two reads instead
4321 * of one, so skip it.
4323 if (!buffer_uptodate(bitmap_bh
)) {
4327 for (i
= start
; i
< start
+ inodes_per_block
; i
++) {
4328 if (i
== inode_offset
)
4330 if (ext4_test_bit(i
, bitmap_bh
->b_data
))
4334 if (i
== start
+ inodes_per_block
) {
4335 /* all other inodes are free, so skip I/O */
4336 memset(bh
->b_data
, 0, bh
->b_size
);
4337 set_buffer_uptodate(bh
);
4345 * If we need to do any I/O, try to pre-readahead extra
4346 * blocks from the inode table.
4348 blk_start_plug(&plug
);
4349 if (EXT4_SB(sb
)->s_inode_readahead_blks
) {
4350 ext4_fsblk_t b
, end
, table
;
4352 __u32 ra_blks
= EXT4_SB(sb
)->s_inode_readahead_blks
;
4354 table
= ext4_inode_table(sb
, gdp
);
4355 /* s_inode_readahead_blks is always a power of 2 */
4356 b
= block
& ~((ext4_fsblk_t
) ra_blks
- 1);
4360 num
= EXT4_INODES_PER_GROUP(sb
);
4361 if (ext4_has_group_desc_csum(sb
))
4362 num
-= ext4_itable_unused_count(sb
, gdp
);
4363 table
+= num
/ inodes_per_block
;
4367 sb_breadahead(sb
, b
++);
4371 * There are other valid inodes in the buffer, this inode
4372 * has in-inode xattrs, or we don't have this inode in memory.
4373 * Read the block from disk.
4375 trace_ext4_load_inode(inode
);
4377 bh
->b_end_io
= end_buffer_read_sync
;
4378 submit_bh(REQ_OP_READ
, REQ_META
| REQ_PRIO
, bh
);
4379 blk_finish_plug(&plug
);
4381 if (!buffer_uptodate(bh
)) {
4383 ext4_error_inode_block(inode
, block
, EIO
,
4384 "unable to read itable block");
4394 int ext4_get_inode_loc(struct inode
*inode
, struct ext4_iloc
*iloc
)
4396 /* We have all inode data except xattrs in memory here. */
4397 return __ext4_get_inode_loc(inode
, iloc
,
4398 !ext4_test_inode_state(inode
, EXT4_STATE_XATTR
));
4401 static bool ext4_should_use_dax(struct inode
*inode
)
4403 if (!test_opt(inode
->i_sb
, DAX
))
4405 if (!S_ISREG(inode
->i_mode
))
4407 if (ext4_should_journal_data(inode
))
4409 if (ext4_has_inline_data(inode
))
4411 if (ext4_test_inode_flag(inode
, EXT4_INODE_ENCRYPT
))
4413 if (ext4_test_inode_flag(inode
, EXT4_INODE_VERITY
))
4418 void ext4_set_inode_flags(struct inode
*inode
)
4420 unsigned int flags
= EXT4_I(inode
)->i_flags
;
4421 unsigned int new_fl
= 0;
4423 if (flags
& EXT4_SYNC_FL
)
4425 if (flags
& EXT4_APPEND_FL
)
4427 if (flags
& EXT4_IMMUTABLE_FL
)
4428 new_fl
|= S_IMMUTABLE
;
4429 if (flags
& EXT4_NOATIME_FL
)
4430 new_fl
|= S_NOATIME
;
4431 if (flags
& EXT4_DIRSYNC_FL
)
4432 new_fl
|= S_DIRSYNC
;
4433 if (ext4_should_use_dax(inode
))
4435 if (flags
& EXT4_ENCRYPT_FL
)
4436 new_fl
|= S_ENCRYPTED
;
4437 if (flags
& EXT4_CASEFOLD_FL
)
4438 new_fl
|= S_CASEFOLD
;
4439 if (flags
& EXT4_VERITY_FL
)
4441 inode_set_flags(inode
, new_fl
,
4442 S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
|S_DAX
|
4443 S_ENCRYPTED
|S_CASEFOLD
|S_VERITY
);
4446 static blkcnt_t
ext4_inode_blocks(struct ext4_inode
*raw_inode
,
4447 struct ext4_inode_info
*ei
)
4450 struct inode
*inode
= &(ei
->vfs_inode
);
4451 struct super_block
*sb
= inode
->i_sb
;
4453 if (ext4_has_feature_huge_file(sb
)) {
4454 /* we are using combined 48 bit field */
4455 i_blocks
= ((u64
)le16_to_cpu(raw_inode
->i_blocks_high
)) << 32 |
4456 le32_to_cpu(raw_inode
->i_blocks_lo
);
4457 if (ext4_test_inode_flag(inode
, EXT4_INODE_HUGE_FILE
)) {
4458 /* i_blocks represent file system block size */
4459 return i_blocks
<< (inode
->i_blkbits
- 9);
4464 return le32_to_cpu(raw_inode
->i_blocks_lo
);
4468 static inline int ext4_iget_extra_inode(struct inode
*inode
,
4469 struct ext4_inode
*raw_inode
,
4470 struct ext4_inode_info
*ei
)
4472 __le32
*magic
= (void *)raw_inode
+
4473 EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
;
4475 if (EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
+ sizeof(__le32
) <=
4476 EXT4_INODE_SIZE(inode
->i_sb
) &&
4477 *magic
== cpu_to_le32(EXT4_XATTR_MAGIC
)) {
4478 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
4479 return ext4_find_inline_data_nolock(inode
);
4481 EXT4_I(inode
)->i_inline_off
= 0;
4485 int ext4_get_projid(struct inode
*inode
, kprojid_t
*projid
)
4487 if (!ext4_has_feature_project(inode
->i_sb
))
4489 *projid
= EXT4_I(inode
)->i_projid
;
4494 * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of
4495 * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag
4498 static inline void ext4_inode_set_iversion_queried(struct inode
*inode
, u64 val
)
4500 if (unlikely(EXT4_I(inode
)->i_flags
& EXT4_EA_INODE_FL
))
4501 inode_set_iversion_raw(inode
, val
);
4503 inode_set_iversion_queried(inode
, val
);
4505 static inline u64
ext4_inode_peek_iversion(const struct inode
*inode
)
4507 if (unlikely(EXT4_I(inode
)->i_flags
& EXT4_EA_INODE_FL
))
4508 return inode_peek_iversion_raw(inode
);
4510 return inode_peek_iversion(inode
);
4513 struct inode
*__ext4_iget(struct super_block
*sb
, unsigned long ino
,
4514 ext4_iget_flags flags
, const char *function
,
4517 struct ext4_iloc iloc
;
4518 struct ext4_inode
*raw_inode
;
4519 struct ext4_inode_info
*ei
;
4520 struct inode
*inode
;
4521 journal_t
*journal
= EXT4_SB(sb
)->s_journal
;
4529 if ((!(flags
& EXT4_IGET_SPECIAL
) &&
4530 (ino
< EXT4_FIRST_INO(sb
) && ino
!= EXT4_ROOT_INO
)) ||
4531 (ino
< EXT4_ROOT_INO
) ||
4532 (ino
> le32_to_cpu(EXT4_SB(sb
)->s_es
->s_inodes_count
))) {
4533 if (flags
& EXT4_IGET_HANDLE
)
4534 return ERR_PTR(-ESTALE
);
4535 __ext4_error(sb
, function
, line
, EFSCORRUPTED
, 0,
4536 "inode #%lu: comm %s: iget: illegal inode #",
4537 ino
, current
->comm
);
4538 return ERR_PTR(-EFSCORRUPTED
);
4541 inode
= iget_locked(sb
, ino
);
4543 return ERR_PTR(-ENOMEM
);
4544 if (!(inode
->i_state
& I_NEW
))
4550 ret
= __ext4_get_inode_loc(inode
, &iloc
, 0);
4553 raw_inode
= ext4_raw_inode(&iloc
);
4555 if ((ino
== EXT4_ROOT_INO
) && (raw_inode
->i_links_count
== 0)) {
4556 ext4_error_inode(inode
, function
, line
, 0,
4557 "iget: root inode unallocated");
4558 ret
= -EFSCORRUPTED
;
4562 if ((flags
& EXT4_IGET_HANDLE
) &&
4563 (raw_inode
->i_links_count
== 0) && (raw_inode
->i_mode
== 0)) {
4568 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4569 ei
->i_extra_isize
= le16_to_cpu(raw_inode
->i_extra_isize
);
4570 if (EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
>
4571 EXT4_INODE_SIZE(inode
->i_sb
) ||
4572 (ei
->i_extra_isize
& 3)) {
4573 ext4_error_inode(inode
, function
, line
, 0,
4574 "iget: bad extra_isize %u "
4577 EXT4_INODE_SIZE(inode
->i_sb
));
4578 ret
= -EFSCORRUPTED
;
4582 ei
->i_extra_isize
= 0;
4584 /* Precompute checksum seed for inode metadata */
4585 if (ext4_has_metadata_csum(sb
)) {
4586 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
4588 __le32 inum
= cpu_to_le32(inode
->i_ino
);
4589 __le32 gen
= raw_inode
->i_generation
;
4590 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&inum
,
4592 ei
->i_csum_seed
= ext4_chksum(sbi
, csum
, (__u8
*)&gen
,
4596 if (!ext4_inode_csum_verify(inode
, raw_inode
, ei
) ||
4597 ext4_simulate_fail(sb
, EXT4_SIM_INODE_CRC
)) {
4598 ext4_error_inode_err(inode
, function
, line
, 0, EFSBADCRC
,
4599 "iget: checksum invalid");
4604 inode
->i_mode
= le16_to_cpu(raw_inode
->i_mode
);
4605 i_uid
= (uid_t
)le16_to_cpu(raw_inode
->i_uid_low
);
4606 i_gid
= (gid_t
)le16_to_cpu(raw_inode
->i_gid_low
);
4607 if (ext4_has_feature_project(sb
) &&
4608 EXT4_INODE_SIZE(sb
) > EXT4_GOOD_OLD_INODE_SIZE
&&
4609 EXT4_FITS_IN_INODE(raw_inode
, ei
, i_projid
))
4610 i_projid
= (projid_t
)le32_to_cpu(raw_inode
->i_projid
);
4612 i_projid
= EXT4_DEF_PROJID
;
4614 if (!(test_opt(inode
->i_sb
, NO_UID32
))) {
4615 i_uid
|= le16_to_cpu(raw_inode
->i_uid_high
) << 16;
4616 i_gid
|= le16_to_cpu(raw_inode
->i_gid_high
) << 16;
4618 i_uid_write(inode
, i_uid
);
4619 i_gid_write(inode
, i_gid
);
4620 ei
->i_projid
= make_kprojid(&init_user_ns
, i_projid
);
4621 set_nlink(inode
, le16_to_cpu(raw_inode
->i_links_count
));
4623 ext4_clear_state_flags(ei
); /* Only relevant on 32-bit archs */
4624 ei
->i_inline_off
= 0;
4625 ei
->i_dir_start_lookup
= 0;
4626 ei
->i_dtime
= le32_to_cpu(raw_inode
->i_dtime
);
4627 /* We now have enough fields to check if the inode was active or not.
4628 * This is needed because nfsd might try to access dead inodes
4629 * the test is that same one that e2fsck uses
4630 * NeilBrown 1999oct15
4632 if (inode
->i_nlink
== 0) {
4633 if ((inode
->i_mode
== 0 ||
4634 !(EXT4_SB(inode
->i_sb
)->s_mount_state
& EXT4_ORPHAN_FS
)) &&
4635 ino
!= EXT4_BOOT_LOADER_INO
) {
4636 /* this inode is deleted */
4640 /* The only unlinked inodes we let through here have
4641 * valid i_mode and are being read by the orphan
4642 * recovery code: that's fine, we're about to complete
4643 * the process of deleting those.
4644 * OR it is the EXT4_BOOT_LOADER_INO which is
4645 * not initialized on a new filesystem. */
4647 ei
->i_flags
= le32_to_cpu(raw_inode
->i_flags
);
4648 ext4_set_inode_flags(inode
);
4649 inode
->i_blocks
= ext4_inode_blocks(raw_inode
, ei
);
4650 ei
->i_file_acl
= le32_to_cpu(raw_inode
->i_file_acl_lo
);
4651 if (ext4_has_feature_64bit(sb
))
4653 ((__u64
)le16_to_cpu(raw_inode
->i_file_acl_high
)) << 32;
4654 inode
->i_size
= ext4_isize(sb
, raw_inode
);
4655 if ((size
= i_size_read(inode
)) < 0) {
4656 ext4_error_inode(inode
, function
, line
, 0,
4657 "iget: bad i_size value: %lld", size
);
4658 ret
= -EFSCORRUPTED
;
4662 * If dir_index is not enabled but there's dir with INDEX flag set,
4663 * we'd normally treat htree data as empty space. But with metadata
4664 * checksumming that corrupts checksums so forbid that.
4666 if (!ext4_has_feature_dir_index(sb
) && ext4_has_metadata_csum(sb
) &&
4667 ext4_test_inode_flag(inode
, EXT4_INODE_INDEX
)) {
4668 ext4_error_inode(inode
, function
, line
, 0,
4669 "iget: Dir with htree data on filesystem without dir_index feature.");
4670 ret
= -EFSCORRUPTED
;
4673 ei
->i_disksize
= inode
->i_size
;
4675 ei
->i_reserved_quota
= 0;
4677 inode
->i_generation
= le32_to_cpu(raw_inode
->i_generation
);
4678 ei
->i_block_group
= iloc
.block_group
;
4679 ei
->i_last_alloc_group
= ~0;
4681 * NOTE! The in-memory inode i_data array is in little-endian order
4682 * even on big-endian machines: we do NOT byteswap the block numbers!
4684 for (block
= 0; block
< EXT4_N_BLOCKS
; block
++)
4685 ei
->i_data
[block
] = raw_inode
->i_block
[block
];
4686 INIT_LIST_HEAD(&ei
->i_orphan
);
4689 * Set transaction id's of transactions that have to be committed
4690 * to finish f[data]sync. We set them to currently running transaction
4691 * as we cannot be sure that the inode or some of its metadata isn't
4692 * part of the transaction - the inode could have been reclaimed and
4693 * now it is reread from disk.
4696 transaction_t
*transaction
;
4699 read_lock(&journal
->j_state_lock
);
4700 if (journal
->j_running_transaction
)
4701 transaction
= journal
->j_running_transaction
;
4703 transaction
= journal
->j_committing_transaction
;
4705 tid
= transaction
->t_tid
;
4707 tid
= journal
->j_commit_sequence
;
4708 read_unlock(&journal
->j_state_lock
);
4709 ei
->i_sync_tid
= tid
;
4710 ei
->i_datasync_tid
= tid
;
4713 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4714 if (ei
->i_extra_isize
== 0) {
4715 /* The extra space is currently unused. Use it. */
4716 BUILD_BUG_ON(sizeof(struct ext4_inode
) & 3);
4717 ei
->i_extra_isize
= sizeof(struct ext4_inode
) -
4718 EXT4_GOOD_OLD_INODE_SIZE
;
4720 ret
= ext4_iget_extra_inode(inode
, raw_inode
, ei
);
4726 EXT4_INODE_GET_XTIME(i_ctime
, inode
, raw_inode
);
4727 EXT4_INODE_GET_XTIME(i_mtime
, inode
, raw_inode
);
4728 EXT4_INODE_GET_XTIME(i_atime
, inode
, raw_inode
);
4729 EXT4_EINODE_GET_XTIME(i_crtime
, ei
, raw_inode
);
4731 if (likely(!test_opt2(inode
->i_sb
, HURD_COMPAT
))) {
4732 u64 ivers
= le32_to_cpu(raw_inode
->i_disk_version
);
4734 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
) {
4735 if (EXT4_FITS_IN_INODE(raw_inode
, ei
, i_version_hi
))
4737 (__u64
)(le32_to_cpu(raw_inode
->i_version_hi
)) << 32;
4739 ext4_inode_set_iversion_queried(inode
, ivers
);
4743 if (ei
->i_file_acl
&&
4744 !ext4_data_block_valid(EXT4_SB(sb
), ei
->i_file_acl
, 1)) {
4745 ext4_error_inode(inode
, function
, line
, 0,
4746 "iget: bad extended attribute block %llu",
4748 ret
= -EFSCORRUPTED
;
4750 } else if (!ext4_has_inline_data(inode
)) {
4751 /* validate the block references in the inode */
4752 if (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
4753 (S_ISLNK(inode
->i_mode
) &&
4754 !ext4_inode_is_fast_symlink(inode
))) {
4755 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
4756 ret
= ext4_ext_check_inode(inode
);
4758 ret
= ext4_ind_check_inode(inode
);
4764 if (S_ISREG(inode
->i_mode
)) {
4765 inode
->i_op
= &ext4_file_inode_operations
;
4766 inode
->i_fop
= &ext4_file_operations
;
4767 ext4_set_aops(inode
);
4768 } else if (S_ISDIR(inode
->i_mode
)) {
4769 inode
->i_op
= &ext4_dir_inode_operations
;
4770 inode
->i_fop
= &ext4_dir_operations
;
4771 } else if (S_ISLNK(inode
->i_mode
)) {
4772 /* VFS does not allow setting these so must be corruption */
4773 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
)) {
4774 ext4_error_inode(inode
, function
, line
, 0,
4775 "iget: immutable or append flags "
4776 "not allowed on symlinks");
4777 ret
= -EFSCORRUPTED
;
4780 if (IS_ENCRYPTED(inode
)) {
4781 inode
->i_op
= &ext4_encrypted_symlink_inode_operations
;
4782 ext4_set_aops(inode
);
4783 } else if (ext4_inode_is_fast_symlink(inode
)) {
4784 inode
->i_link
= (char *)ei
->i_data
;
4785 inode
->i_op
= &ext4_fast_symlink_inode_operations
;
4786 nd_terminate_link(ei
->i_data
, inode
->i_size
,
4787 sizeof(ei
->i_data
) - 1);
4789 inode
->i_op
= &ext4_symlink_inode_operations
;
4790 ext4_set_aops(inode
);
4792 inode_nohighmem(inode
);
4793 } else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
) ||
4794 S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
4795 inode
->i_op
= &ext4_special_inode_operations
;
4796 if (raw_inode
->i_block
[0])
4797 init_special_inode(inode
, inode
->i_mode
,
4798 old_decode_dev(le32_to_cpu(raw_inode
->i_block
[0])));
4800 init_special_inode(inode
, inode
->i_mode
,
4801 new_decode_dev(le32_to_cpu(raw_inode
->i_block
[1])));
4802 } else if (ino
== EXT4_BOOT_LOADER_INO
) {
4803 make_bad_inode(inode
);
4805 ret
= -EFSCORRUPTED
;
4806 ext4_error_inode(inode
, function
, line
, 0,
4807 "iget: bogus i_mode (%o)", inode
->i_mode
);
4810 if (IS_CASEFOLDED(inode
) && !ext4_has_feature_casefold(inode
->i_sb
))
4811 ext4_error_inode(inode
, function
, line
, 0,
4812 "casefold flag without casefold feature");
4815 unlock_new_inode(inode
);
4821 return ERR_PTR(ret
);
4824 static int ext4_inode_blocks_set(handle_t
*handle
,
4825 struct ext4_inode
*raw_inode
,
4826 struct ext4_inode_info
*ei
)
4828 struct inode
*inode
= &(ei
->vfs_inode
);
4829 u64 i_blocks
= READ_ONCE(inode
->i_blocks
);
4830 struct super_block
*sb
= inode
->i_sb
;
4832 if (i_blocks
<= ~0U) {
4834 * i_blocks can be represented in a 32 bit variable
4835 * as multiple of 512 bytes
4837 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4838 raw_inode
->i_blocks_high
= 0;
4839 ext4_clear_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4842 if (!ext4_has_feature_huge_file(sb
))
4845 if (i_blocks
<= 0xffffffffffffULL
) {
4847 * i_blocks can be represented in a 48 bit variable
4848 * as multiple of 512 bytes
4850 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4851 raw_inode
->i_blocks_high
= cpu_to_le16(i_blocks
>> 32);
4852 ext4_clear_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4854 ext4_set_inode_flag(inode
, EXT4_INODE_HUGE_FILE
);
4855 /* i_block is stored in file system block size */
4856 i_blocks
= i_blocks
>> (inode
->i_blkbits
- 9);
4857 raw_inode
->i_blocks_lo
= cpu_to_le32(i_blocks
);
4858 raw_inode
->i_blocks_high
= cpu_to_le16(i_blocks
>> 32);
4863 struct other_inode
{
4864 unsigned long orig_ino
;
4865 struct ext4_inode
*raw_inode
;
4868 static int other_inode_match(struct inode
* inode
, unsigned long ino
,
4871 struct other_inode
*oi
= (struct other_inode
*) data
;
4873 if ((inode
->i_ino
!= ino
) ||
4874 (inode
->i_state
& (I_FREEING
| I_WILL_FREE
| I_NEW
|
4876 ((inode
->i_state
& I_DIRTY_TIME
) == 0))
4878 spin_lock(&inode
->i_lock
);
4879 if (((inode
->i_state
& (I_FREEING
| I_WILL_FREE
| I_NEW
|
4880 I_DIRTY_INODE
)) == 0) &&
4881 (inode
->i_state
& I_DIRTY_TIME
)) {
4882 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4884 inode
->i_state
&= ~(I_DIRTY_TIME
| I_DIRTY_TIME_EXPIRED
);
4885 spin_unlock(&inode
->i_lock
);
4887 spin_lock(&ei
->i_raw_lock
);
4888 EXT4_INODE_SET_XTIME(i_ctime
, inode
, oi
->raw_inode
);
4889 EXT4_INODE_SET_XTIME(i_mtime
, inode
, oi
->raw_inode
);
4890 EXT4_INODE_SET_XTIME(i_atime
, inode
, oi
->raw_inode
);
4891 ext4_inode_csum_set(inode
, oi
->raw_inode
, ei
);
4892 spin_unlock(&ei
->i_raw_lock
);
4893 trace_ext4_other_inode_update_time(inode
, oi
->orig_ino
);
4896 spin_unlock(&inode
->i_lock
);
4901 * Opportunistically update the other time fields for other inodes in
4902 * the same inode table block.
4904 static void ext4_update_other_inodes_time(struct super_block
*sb
,
4905 unsigned long orig_ino
, char *buf
)
4907 struct other_inode oi
;
4909 int i
, inodes_per_block
= EXT4_SB(sb
)->s_inodes_per_block
;
4910 int inode_size
= EXT4_INODE_SIZE(sb
);
4912 oi
.orig_ino
= orig_ino
;
4914 * Calculate the first inode in the inode table block. Inode
4915 * numbers are one-based. That is, the first inode in a block
4916 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
4918 ino
= ((orig_ino
- 1) & ~(inodes_per_block
- 1)) + 1;
4919 for (i
= 0; i
< inodes_per_block
; i
++, ino
++, buf
+= inode_size
) {
4920 if (ino
== orig_ino
)
4922 oi
.raw_inode
= (struct ext4_inode
*) buf
;
4923 (void) find_inode_nowait(sb
, ino
, other_inode_match
, &oi
);
4928 * Post the struct inode info into an on-disk inode location in the
4929 * buffer-cache. This gobbles the caller's reference to the
4930 * buffer_head in the inode location struct.
4932 * The caller must have write access to iloc->bh.
4934 static int ext4_do_update_inode(handle_t
*handle
,
4935 struct inode
*inode
,
4936 struct ext4_iloc
*iloc
)
4938 struct ext4_inode
*raw_inode
= ext4_raw_inode(iloc
);
4939 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4940 struct buffer_head
*bh
= iloc
->bh
;
4941 struct super_block
*sb
= inode
->i_sb
;
4942 int err
= 0, rc
, block
;
4943 int need_datasync
= 0, set_large_file
= 0;
4948 spin_lock(&ei
->i_raw_lock
);
4950 /* For fields not tracked in the in-memory inode,
4951 * initialise them to zero for new inodes. */
4952 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
))
4953 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
4955 raw_inode
->i_mode
= cpu_to_le16(inode
->i_mode
);
4956 i_uid
= i_uid_read(inode
);
4957 i_gid
= i_gid_read(inode
);
4958 i_projid
= from_kprojid(&init_user_ns
, ei
->i_projid
);
4959 if (!(test_opt(inode
->i_sb
, NO_UID32
))) {
4960 raw_inode
->i_uid_low
= cpu_to_le16(low_16_bits(i_uid
));
4961 raw_inode
->i_gid_low
= cpu_to_le16(low_16_bits(i_gid
));
4963 * Fix up interoperability with old kernels. Otherwise, old inodes get
4964 * re-used with the upper 16 bits of the uid/gid intact
4966 if (ei
->i_dtime
&& list_empty(&ei
->i_orphan
)) {
4967 raw_inode
->i_uid_high
= 0;
4968 raw_inode
->i_gid_high
= 0;
4970 raw_inode
->i_uid_high
=
4971 cpu_to_le16(high_16_bits(i_uid
));
4972 raw_inode
->i_gid_high
=
4973 cpu_to_le16(high_16_bits(i_gid
));
4976 raw_inode
->i_uid_low
= cpu_to_le16(fs_high2lowuid(i_uid
));
4977 raw_inode
->i_gid_low
= cpu_to_le16(fs_high2lowgid(i_gid
));
4978 raw_inode
->i_uid_high
= 0;
4979 raw_inode
->i_gid_high
= 0;
4981 raw_inode
->i_links_count
= cpu_to_le16(inode
->i_nlink
);
4983 EXT4_INODE_SET_XTIME(i_ctime
, inode
, raw_inode
);
4984 EXT4_INODE_SET_XTIME(i_mtime
, inode
, raw_inode
);
4985 EXT4_INODE_SET_XTIME(i_atime
, inode
, raw_inode
);
4986 EXT4_EINODE_SET_XTIME(i_crtime
, ei
, raw_inode
);
4988 err
= ext4_inode_blocks_set(handle
, raw_inode
, ei
);
4990 spin_unlock(&ei
->i_raw_lock
);
4993 raw_inode
->i_dtime
= cpu_to_le32(ei
->i_dtime
);
4994 raw_inode
->i_flags
= cpu_to_le32(ei
->i_flags
& 0xFFFFFFFF);
4995 if (likely(!test_opt2(inode
->i_sb
, HURD_COMPAT
)))
4996 raw_inode
->i_file_acl_high
=
4997 cpu_to_le16(ei
->i_file_acl
>> 32);
4998 raw_inode
->i_file_acl_lo
= cpu_to_le32(ei
->i_file_acl
);
4999 if (READ_ONCE(ei
->i_disksize
) != ext4_isize(inode
->i_sb
, raw_inode
)) {
5000 ext4_isize_set(raw_inode
, ei
->i_disksize
);
5003 if (ei
->i_disksize
> 0x7fffffffULL
) {
5004 if (!ext4_has_feature_large_file(sb
) ||
5005 EXT4_SB(sb
)->s_es
->s_rev_level
==
5006 cpu_to_le32(EXT4_GOOD_OLD_REV
))
5009 raw_inode
->i_generation
= cpu_to_le32(inode
->i_generation
);
5010 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) {
5011 if (old_valid_dev(inode
->i_rdev
)) {
5012 raw_inode
->i_block
[0] =
5013 cpu_to_le32(old_encode_dev(inode
->i_rdev
));
5014 raw_inode
->i_block
[1] = 0;
5016 raw_inode
->i_block
[0] = 0;
5017 raw_inode
->i_block
[1] =
5018 cpu_to_le32(new_encode_dev(inode
->i_rdev
));
5019 raw_inode
->i_block
[2] = 0;
5021 } else if (!ext4_has_inline_data(inode
)) {
5022 for (block
= 0; block
< EXT4_N_BLOCKS
; block
++)
5023 raw_inode
->i_block
[block
] = ei
->i_data
[block
];
5026 if (likely(!test_opt2(inode
->i_sb
, HURD_COMPAT
))) {
5027 u64 ivers
= ext4_inode_peek_iversion(inode
);
5029 raw_inode
->i_disk_version
= cpu_to_le32(ivers
);
5030 if (ei
->i_extra_isize
) {
5031 if (EXT4_FITS_IN_INODE(raw_inode
, ei
, i_version_hi
))
5032 raw_inode
->i_version_hi
=
5033 cpu_to_le32(ivers
>> 32);
5034 raw_inode
->i_extra_isize
=
5035 cpu_to_le16(ei
->i_extra_isize
);
5039 BUG_ON(!ext4_has_feature_project(inode
->i_sb
) &&
5040 i_projid
!= EXT4_DEF_PROJID
);
5042 if (EXT4_INODE_SIZE(inode
->i_sb
) > EXT4_GOOD_OLD_INODE_SIZE
&&
5043 EXT4_FITS_IN_INODE(raw_inode
, ei
, i_projid
))
5044 raw_inode
->i_projid
= cpu_to_le32(i_projid
);
5046 ext4_inode_csum_set(inode
, raw_inode
, ei
);
5047 spin_unlock(&ei
->i_raw_lock
);
5048 if (inode
->i_sb
->s_flags
& SB_LAZYTIME
)
5049 ext4_update_other_inodes_time(inode
->i_sb
, inode
->i_ino
,
5052 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
5053 rc
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
5056 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
5057 if (set_large_file
) {
5058 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get write access");
5059 err
= ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
);
5062 ext4_set_feature_large_file(sb
);
5063 ext4_handle_sync(handle
);
5064 err
= ext4_handle_dirty_super(handle
, sb
);
5066 ext4_update_inode_fsync_trans(handle
, inode
, need_datasync
);
5069 ext4_std_error(inode
->i_sb
, err
);
5074 * ext4_write_inode()
5076 * We are called from a few places:
5078 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
5079 * Here, there will be no transaction running. We wait for any running
5080 * transaction to commit.
5082 * - Within flush work (sys_sync(), kupdate and such).
5083 * We wait on commit, if told to.
5085 * - Within iput_final() -> write_inode_now()
5086 * We wait on commit, if told to.
5088 * In all cases it is actually safe for us to return without doing anything,
5089 * because the inode has been copied into a raw inode buffer in
5090 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
5093 * Note that we are absolutely dependent upon all inode dirtiers doing the
5094 * right thing: they *must* call mark_inode_dirty() after dirtying info in
5095 * which we are interested.
5097 * It would be a bug for them to not do this. The code:
5099 * mark_inode_dirty(inode)
5101 * inode->i_size = expr;
5103 * is in error because write_inode() could occur while `stuff()' is running,
5104 * and the new i_size will be lost. Plus the inode will no longer be on the
5105 * superblock's dirty inode list.
5107 int ext4_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5111 if (WARN_ON_ONCE(current
->flags
& PF_MEMALLOC
) ||
5112 sb_rdonly(inode
->i_sb
))
5115 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
5118 if (EXT4_SB(inode
->i_sb
)->s_journal
) {
5119 if (ext4_journal_current_handle()) {
5120 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5126 * No need to force transaction in WB_SYNC_NONE mode. Also
5127 * ext4_sync_fs() will force the commit after everything is
5130 if (wbc
->sync_mode
!= WB_SYNC_ALL
|| wbc
->for_sync
)
5133 err
= jbd2_complete_transaction(EXT4_SB(inode
->i_sb
)->s_journal
,
5134 EXT4_I(inode
)->i_sync_tid
);
5136 struct ext4_iloc iloc
;
5138 err
= __ext4_get_inode_loc(inode
, &iloc
, 0);
5142 * sync(2) will flush the whole buffer cache. No need to do
5143 * it here separately for each inode.
5145 if (wbc
->sync_mode
== WB_SYNC_ALL
&& !wbc
->for_sync
)
5146 sync_dirty_buffer(iloc
.bh
);
5147 if (buffer_req(iloc
.bh
) && !buffer_uptodate(iloc
.bh
)) {
5148 ext4_error_inode_block(inode
, iloc
.bh
->b_blocknr
, EIO
,
5149 "IO error syncing inode");
5158 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
5159 * buffers that are attached to a page stradding i_size and are undergoing
5160 * commit. In that case we have to wait for commit to finish and try again.
5162 static void ext4_wait_for_tail_page_commit(struct inode
*inode
)
5166 journal_t
*journal
= EXT4_SB(inode
->i_sb
)->s_journal
;
5167 tid_t commit_tid
= 0;
5170 offset
= inode
->i_size
& (PAGE_SIZE
- 1);
5172 * If the page is fully truncated, we don't need to wait for any commit
5173 * (and we even should not as __ext4_journalled_invalidatepage() may
5174 * strip all buffers from the page but keep the page dirty which can then
5175 * confuse e.g. concurrent ext4_writepage() seeing dirty page without
5176 * buffers). Also we don't need to wait for any commit if all buffers in
5177 * the page remain valid. This is most beneficial for the common case of
5178 * blocksize == PAGESIZE.
5180 if (!offset
|| offset
> (PAGE_SIZE
- i_blocksize(inode
)))
5183 page
= find_lock_page(inode
->i_mapping
,
5184 inode
->i_size
>> PAGE_SHIFT
);
5187 ret
= __ext4_journalled_invalidatepage(page
, offset
,
5188 PAGE_SIZE
- offset
);
5194 read_lock(&journal
->j_state_lock
);
5195 if (journal
->j_committing_transaction
)
5196 commit_tid
= journal
->j_committing_transaction
->t_tid
;
5197 read_unlock(&journal
->j_state_lock
);
5199 jbd2_log_wait_commit(journal
, commit_tid
);
5206 * Called from notify_change.
5208 * We want to trap VFS attempts to truncate the file as soon as
5209 * possible. In particular, we want to make sure that when the VFS
5210 * shrinks i_size, we put the inode on the orphan list and modify
5211 * i_disksize immediately, so that during the subsequent flushing of
5212 * dirty pages and freeing of disk blocks, we can guarantee that any
5213 * commit will leave the blocks being flushed in an unused state on
5214 * disk. (On recovery, the inode will get truncated and the blocks will
5215 * be freed, so we have a strong guarantee that no future commit will
5216 * leave these blocks visible to the user.)
5218 * Another thing we have to assure is that if we are in ordered mode
5219 * and inode is still attached to the committing transaction, we must
5220 * we start writeout of all the dirty pages which are being truncated.
5221 * This way we are sure that all the data written in the previous
5222 * transaction are already on disk (truncate waits for pages under
5225 * Called with inode->i_mutex down.
5227 int ext4_setattr(struct dentry
*dentry
, struct iattr
*attr
)
5229 struct inode
*inode
= d_inode(dentry
);
5232 const unsigned int ia_valid
= attr
->ia_valid
;
5234 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
5237 if (unlikely(IS_IMMUTABLE(inode
)))
5240 if (unlikely(IS_APPEND(inode
) &&
5241 (ia_valid
& (ATTR_MODE
| ATTR_UID
|
5242 ATTR_GID
| ATTR_TIMES_SET
))))
5245 error
= setattr_prepare(dentry
, attr
);
5249 error
= fscrypt_prepare_setattr(dentry
, attr
);
5253 error
= fsverity_prepare_setattr(dentry
, attr
);
5257 if (is_quota_modification(inode
, attr
)) {
5258 error
= dquot_initialize(inode
);
5262 if ((ia_valid
& ATTR_UID
&& !uid_eq(attr
->ia_uid
, inode
->i_uid
)) ||
5263 (ia_valid
& ATTR_GID
&& !gid_eq(attr
->ia_gid
, inode
->i_gid
))) {
5266 /* (user+group)*(old+new) structure, inode write (sb,
5267 * inode block, ? - but truncate inode update has it) */
5268 handle
= ext4_journal_start(inode
, EXT4_HT_QUOTA
,
5269 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode
->i_sb
) +
5270 EXT4_MAXQUOTAS_DEL_BLOCKS(inode
->i_sb
)) + 3);
5271 if (IS_ERR(handle
)) {
5272 error
= PTR_ERR(handle
);
5276 /* dquot_transfer() calls back ext4_get_inode_usage() which
5277 * counts xattr inode references.
5279 down_read(&EXT4_I(inode
)->xattr_sem
);
5280 error
= dquot_transfer(inode
, attr
);
5281 up_read(&EXT4_I(inode
)->xattr_sem
);
5284 ext4_journal_stop(handle
);
5287 /* Update corresponding info in inode so that everything is in
5288 * one transaction */
5289 if (attr
->ia_valid
& ATTR_UID
)
5290 inode
->i_uid
= attr
->ia_uid
;
5291 if (attr
->ia_valid
& ATTR_GID
)
5292 inode
->i_gid
= attr
->ia_gid
;
5293 error
= ext4_mark_inode_dirty(handle
, inode
);
5294 ext4_journal_stop(handle
);
5297 if (attr
->ia_valid
& ATTR_SIZE
) {
5299 loff_t oldsize
= inode
->i_size
;
5300 int shrink
= (attr
->ia_size
< inode
->i_size
);
5302 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))) {
5303 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
5305 if (attr
->ia_size
> sbi
->s_bitmap_maxbytes
)
5308 if (!S_ISREG(inode
->i_mode
))
5311 if (IS_I_VERSION(inode
) && attr
->ia_size
!= inode
->i_size
)
5312 inode_inc_iversion(inode
);
5315 if (ext4_should_order_data(inode
)) {
5316 error
= ext4_begin_ordered_truncate(inode
,
5322 * Blocks are going to be removed from the inode. Wait
5323 * for dio in flight.
5325 inode_dio_wait(inode
);
5328 down_write(&EXT4_I(inode
)->i_mmap_sem
);
5330 rc
= ext4_break_layouts(inode
);
5332 up_write(&EXT4_I(inode
)->i_mmap_sem
);
5336 if (attr
->ia_size
!= inode
->i_size
) {
5337 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 3);
5338 if (IS_ERR(handle
)) {
5339 error
= PTR_ERR(handle
);
5342 if (ext4_handle_valid(handle
) && shrink
) {
5343 error
= ext4_orphan_add(handle
, inode
);
5347 * Update c/mtime on truncate up, ext4_truncate() will
5348 * update c/mtime in shrink case below
5351 inode
->i_mtime
= current_time(inode
);
5352 inode
->i_ctime
= inode
->i_mtime
;
5354 down_write(&EXT4_I(inode
)->i_data_sem
);
5355 EXT4_I(inode
)->i_disksize
= attr
->ia_size
;
5356 rc
= ext4_mark_inode_dirty(handle
, inode
);
5360 * We have to update i_size under i_data_sem together
5361 * with i_disksize to avoid races with writeback code
5362 * running ext4_wb_update_i_disksize().
5365 i_size_write(inode
, attr
->ia_size
);
5366 up_write(&EXT4_I(inode
)->i_data_sem
);
5367 ext4_journal_stop(handle
);
5371 pagecache_isize_extended(inode
, oldsize
,
5373 } else if (ext4_should_journal_data(inode
)) {
5374 ext4_wait_for_tail_page_commit(inode
);
5379 * Truncate pagecache after we've waited for commit
5380 * in data=journal mode to make pages freeable.
5382 truncate_pagecache(inode
, inode
->i_size
);
5384 * Call ext4_truncate() even if i_size didn't change to
5385 * truncate possible preallocated blocks.
5387 if (attr
->ia_size
<= oldsize
) {
5388 rc
= ext4_truncate(inode
);
5393 up_write(&EXT4_I(inode
)->i_mmap_sem
);
5397 setattr_copy(inode
, attr
);
5398 mark_inode_dirty(inode
);
5402 * If the call to ext4_truncate failed to get a transaction handle at
5403 * all, we need to clean up the in-core orphan list manually.
5405 if (orphan
&& inode
->i_nlink
)
5406 ext4_orphan_del(NULL
, inode
);
5408 if (!error
&& (ia_valid
& ATTR_MODE
))
5409 rc
= posix_acl_chmod(inode
, inode
->i_mode
);
5412 ext4_std_error(inode
->i_sb
, error
);
5418 int ext4_getattr(const struct path
*path
, struct kstat
*stat
,
5419 u32 request_mask
, unsigned int query_flags
)
5421 struct inode
*inode
= d_inode(path
->dentry
);
5422 struct ext4_inode
*raw_inode
;
5423 struct ext4_inode_info
*ei
= EXT4_I(inode
);
5426 if ((request_mask
& STATX_BTIME
) &&
5427 EXT4_FITS_IN_INODE(raw_inode
, ei
, i_crtime
)) {
5428 stat
->result_mask
|= STATX_BTIME
;
5429 stat
->btime
.tv_sec
= ei
->i_crtime
.tv_sec
;
5430 stat
->btime
.tv_nsec
= ei
->i_crtime
.tv_nsec
;
5433 flags
= ei
->i_flags
& EXT4_FL_USER_VISIBLE
;
5434 if (flags
& EXT4_APPEND_FL
)
5435 stat
->attributes
|= STATX_ATTR_APPEND
;
5436 if (flags
& EXT4_COMPR_FL
)
5437 stat
->attributes
|= STATX_ATTR_COMPRESSED
;
5438 if (flags
& EXT4_ENCRYPT_FL
)
5439 stat
->attributes
|= STATX_ATTR_ENCRYPTED
;
5440 if (flags
& EXT4_IMMUTABLE_FL
)
5441 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
5442 if (flags
& EXT4_NODUMP_FL
)
5443 stat
->attributes
|= STATX_ATTR_NODUMP
;
5444 if (flags
& EXT4_VERITY_FL
)
5445 stat
->attributes
|= STATX_ATTR_VERITY
;
5447 stat
->attributes_mask
|= (STATX_ATTR_APPEND
|
5448 STATX_ATTR_COMPRESSED
|
5449 STATX_ATTR_ENCRYPTED
|
5450 STATX_ATTR_IMMUTABLE
|
5454 generic_fillattr(inode
, stat
);
5458 int ext4_file_getattr(const struct path
*path
, struct kstat
*stat
,
5459 u32 request_mask
, unsigned int query_flags
)
5461 struct inode
*inode
= d_inode(path
->dentry
);
5462 u64 delalloc_blocks
;
5464 ext4_getattr(path
, stat
, request_mask
, query_flags
);
5467 * If there is inline data in the inode, the inode will normally not
5468 * have data blocks allocated (it may have an external xattr block).
5469 * Report at least one sector for such files, so tools like tar, rsync,
5470 * others don't incorrectly think the file is completely sparse.
5472 if (unlikely(ext4_has_inline_data(inode
)))
5473 stat
->blocks
+= (stat
->size
+ 511) >> 9;
5476 * We can't update i_blocks if the block allocation is delayed
5477 * otherwise in the case of system crash before the real block
5478 * allocation is done, we will have i_blocks inconsistent with
5479 * on-disk file blocks.
5480 * We always keep i_blocks updated together with real
5481 * allocation. But to not confuse with user, stat
5482 * will return the blocks that include the delayed allocation
5483 * blocks for this file.
5485 delalloc_blocks
= EXT4_C2B(EXT4_SB(inode
->i_sb
),
5486 EXT4_I(inode
)->i_reserved_data_blocks
);
5487 stat
->blocks
+= delalloc_blocks
<< (inode
->i_sb
->s_blocksize_bits
- 9);
5491 static int ext4_index_trans_blocks(struct inode
*inode
, int lblocks
,
5494 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
5495 return ext4_ind_trans_blocks(inode
, lblocks
);
5496 return ext4_ext_index_trans_blocks(inode
, pextents
);
5500 * Account for index blocks, block groups bitmaps and block group
5501 * descriptor blocks if modify datablocks and index blocks
5502 * worse case, the indexs blocks spread over different block groups
5504 * If datablocks are discontiguous, they are possible to spread over
5505 * different block groups too. If they are contiguous, with flexbg,
5506 * they could still across block group boundary.
5508 * Also account for superblock, inode, quota and xattr blocks
5510 static int ext4_meta_trans_blocks(struct inode
*inode
, int lblocks
,
5513 ext4_group_t groups
, ngroups
= ext4_get_groups_count(inode
->i_sb
);
5519 * How many index blocks need to touch to map @lblocks logical blocks
5520 * to @pextents physical extents?
5522 idxblocks
= ext4_index_trans_blocks(inode
, lblocks
, pextents
);
5527 * Now let's see how many group bitmaps and group descriptors need
5530 groups
= idxblocks
+ pextents
;
5532 if (groups
> ngroups
)
5534 if (groups
> EXT4_SB(inode
->i_sb
)->s_gdb_count
)
5535 gdpblocks
= EXT4_SB(inode
->i_sb
)->s_gdb_count
;
5537 /* bitmaps and block group descriptor blocks */
5538 ret
+= groups
+ gdpblocks
;
5540 /* Blocks for super block, inode, quota and xattr blocks */
5541 ret
+= EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
5547 * Calculate the total number of credits to reserve to fit
5548 * the modification of a single pages into a single transaction,
5549 * which may include multiple chunks of block allocations.
5551 * This could be called via ext4_write_begin()
5553 * We need to consider the worse case, when
5554 * one new block per extent.
5556 int ext4_writepage_trans_blocks(struct inode
*inode
)
5558 int bpp
= ext4_journal_blocks_per_page(inode
);
5561 ret
= ext4_meta_trans_blocks(inode
, bpp
, bpp
);
5563 /* Account for data blocks for journalled mode */
5564 if (ext4_should_journal_data(inode
))
5570 * Calculate the journal credits for a chunk of data modification.
5572 * This is called from DIO, fallocate or whoever calling
5573 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
5575 * journal buffers for data blocks are not included here, as DIO
5576 * and fallocate do no need to journal data buffers.
5578 int ext4_chunk_trans_blocks(struct inode
*inode
, int nrblocks
)
5580 return ext4_meta_trans_blocks(inode
, nrblocks
, 1);
5584 * The caller must have previously called ext4_reserve_inode_write().
5585 * Give this, we know that the caller already has write access to iloc->bh.
5587 int ext4_mark_iloc_dirty(handle_t
*handle
,
5588 struct inode
*inode
, struct ext4_iloc
*iloc
)
5592 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
)))) {
5596 if (IS_I_VERSION(inode
))
5597 inode_inc_iversion(inode
);
5599 /* the do_update_inode consumes one bh->b_count */
5602 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
5603 err
= ext4_do_update_inode(handle
, inode
, iloc
);
5609 * On success, We end up with an outstanding reference count against
5610 * iloc->bh. This _must_ be cleaned up later.
5614 ext4_reserve_inode_write(handle_t
*handle
, struct inode
*inode
,
5615 struct ext4_iloc
*iloc
)
5619 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
5622 err
= ext4_get_inode_loc(inode
, iloc
);
5624 BUFFER_TRACE(iloc
->bh
, "get_write_access");
5625 err
= ext4_journal_get_write_access(handle
, iloc
->bh
);
5631 ext4_std_error(inode
->i_sb
, err
);
5635 static int __ext4_expand_extra_isize(struct inode
*inode
,
5636 unsigned int new_extra_isize
,
5637 struct ext4_iloc
*iloc
,
5638 handle_t
*handle
, int *no_expand
)
5640 struct ext4_inode
*raw_inode
;
5641 struct ext4_xattr_ibody_header
*header
;
5642 unsigned int inode_size
= EXT4_INODE_SIZE(inode
->i_sb
);
5643 struct ext4_inode_info
*ei
= EXT4_I(inode
);
5646 /* this was checked at iget time, but double check for good measure */
5647 if ((EXT4_GOOD_OLD_INODE_SIZE
+ ei
->i_extra_isize
> inode_size
) ||
5648 (ei
->i_extra_isize
& 3)) {
5649 EXT4_ERROR_INODE(inode
, "bad extra_isize %u (inode size %u)",
5651 EXT4_INODE_SIZE(inode
->i_sb
));
5652 return -EFSCORRUPTED
;
5654 if ((new_extra_isize
< ei
->i_extra_isize
) ||
5655 (new_extra_isize
< 4) ||
5656 (new_extra_isize
> inode_size
- EXT4_GOOD_OLD_INODE_SIZE
))
5657 return -EINVAL
; /* Should never happen */
5659 raw_inode
= ext4_raw_inode(iloc
);
5661 header
= IHDR(inode
, raw_inode
);
5663 /* No extended attributes present */
5664 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
) ||
5665 header
->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
)) {
5666 memset((void *)raw_inode
+ EXT4_GOOD_OLD_INODE_SIZE
+
5667 EXT4_I(inode
)->i_extra_isize
, 0,
5668 new_extra_isize
- EXT4_I(inode
)->i_extra_isize
);
5669 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
5673 /* try to expand with EAs present */
5674 error
= ext4_expand_extra_isize_ea(inode
, new_extra_isize
,
5678 * Inode size expansion failed; don't try again
5687 * Expand an inode by new_extra_isize bytes.
5688 * Returns 0 on success or negative error number on failure.
5690 static int ext4_try_to_expand_extra_isize(struct inode
*inode
,
5691 unsigned int new_extra_isize
,
5692 struct ext4_iloc iloc
,
5698 if (ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
))
5702 * In nojournal mode, we can immediately attempt to expand
5703 * the inode. When journaled, we first need to obtain extra
5704 * buffer credits since we may write into the EA block
5705 * with this same handle. If journal_extend fails, then it will
5706 * only result in a minor loss of functionality for that inode.
5707 * If this is felt to be critical, then e2fsck should be run to
5708 * force a large enough s_min_extra_isize.
5710 if (ext4_journal_extend(handle
,
5711 EXT4_DATA_TRANS_BLOCKS(inode
->i_sb
), 0) != 0)
5714 if (ext4_write_trylock_xattr(inode
, &no_expand
) == 0)
5717 error
= __ext4_expand_extra_isize(inode
, new_extra_isize
, &iloc
,
5718 handle
, &no_expand
);
5719 ext4_write_unlock_xattr(inode
, &no_expand
);
5724 int ext4_expand_extra_isize(struct inode
*inode
,
5725 unsigned int new_extra_isize
,
5726 struct ext4_iloc
*iloc
)
5732 if (ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
)) {
5737 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
,
5738 EXT4_DATA_TRANS_BLOCKS(inode
->i_sb
));
5739 if (IS_ERR(handle
)) {
5740 error
= PTR_ERR(handle
);
5745 ext4_write_lock_xattr(inode
, &no_expand
);
5747 BUFFER_TRACE(iloc
->bh
, "get_write_access");
5748 error
= ext4_journal_get_write_access(handle
, iloc
->bh
);
5754 error
= __ext4_expand_extra_isize(inode
, new_extra_isize
, iloc
,
5755 handle
, &no_expand
);
5757 rc
= ext4_mark_iloc_dirty(handle
, inode
, iloc
);
5762 ext4_write_unlock_xattr(inode
, &no_expand
);
5763 ext4_journal_stop(handle
);
5768 * What we do here is to mark the in-core inode as clean with respect to inode
5769 * dirtiness (it may still be data-dirty).
5770 * This means that the in-core inode may be reaped by prune_icache
5771 * without having to perform any I/O. This is a very good thing,
5772 * because *any* task may call prune_icache - even ones which
5773 * have a transaction open against a different journal.
5775 * Is this cheating? Not really. Sure, we haven't written the
5776 * inode out, but prune_icache isn't a user-visible syncing function.
5777 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5778 * we start and wait on commits.
5780 int ext4_mark_inode_dirty(handle_t
*handle
, struct inode
*inode
)
5782 struct ext4_iloc iloc
;
5783 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
5787 trace_ext4_mark_inode_dirty(inode
, _RET_IP_
);
5788 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
5792 if (EXT4_I(inode
)->i_extra_isize
< sbi
->s_want_extra_isize
)
5793 ext4_try_to_expand_extra_isize(inode
, sbi
->s_want_extra_isize
,
5796 return ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
5800 * ext4_dirty_inode() is called from __mark_inode_dirty()
5802 * We're really interested in the case where a file is being extended.
5803 * i_size has been changed by generic_commit_write() and we thus need
5804 * to include the updated inode in the current transaction.
5806 * Also, dquot_alloc_block() will always dirty the inode when blocks
5807 * are allocated to the file.
5809 * If the inode is marked synchronous, we don't honour that here - doing
5810 * so would cause a commit on atime updates, which we don't bother doing.
5811 * We handle synchronous inodes at the highest possible level.
5813 * If only the I_DIRTY_TIME flag is set, we can skip everything. If
5814 * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
5815 * to copy into the on-disk inode structure are the timestamp files.
5817 void ext4_dirty_inode(struct inode
*inode
, int flags
)
5821 if (flags
== I_DIRTY_TIME
)
5823 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 2);
5827 ext4_mark_inode_dirty(handle
, inode
);
5829 ext4_journal_stop(handle
);
5834 int ext4_change_inode_journal_flag(struct inode
*inode
, int val
)
5839 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
5842 * We have to be very careful here: changing a data block's
5843 * journaling status dynamically is dangerous. If we write a
5844 * data block to the journal, change the status and then delete
5845 * that block, we risk forgetting to revoke the old log record
5846 * from the journal and so a subsequent replay can corrupt data.
5847 * So, first we make sure that the journal is empty and that
5848 * nobody is changing anything.
5851 journal
= EXT4_JOURNAL(inode
);
5854 if (is_journal_aborted(journal
))
5857 /* Wait for all existing dio workers */
5858 inode_dio_wait(inode
);
5861 * Before flushing the journal and switching inode's aops, we have
5862 * to flush all dirty data the inode has. There can be outstanding
5863 * delayed allocations, there can be unwritten extents created by
5864 * fallocate or buffered writes in dioread_nolock mode covered by
5865 * dirty data which can be converted only after flushing the dirty
5866 * data (and journalled aops don't know how to handle these cases).
5869 down_write(&EXT4_I(inode
)->i_mmap_sem
);
5870 err
= filemap_write_and_wait(inode
->i_mapping
);
5872 up_write(&EXT4_I(inode
)->i_mmap_sem
);
5877 percpu_down_write(&sbi
->s_writepages_rwsem
);
5878 jbd2_journal_lock_updates(journal
);
5881 * OK, there are no updates running now, and all cached data is
5882 * synced to disk. We are now in a completely consistent state
5883 * which doesn't have anything in the journal, and we know that
5884 * no filesystem updates are running, so it is safe to modify
5885 * the inode's in-core data-journaling state flag now.
5889 ext4_set_inode_flag(inode
, EXT4_INODE_JOURNAL_DATA
);
5891 err
= jbd2_journal_flush(journal
);
5893 jbd2_journal_unlock_updates(journal
);
5894 percpu_up_write(&sbi
->s_writepages_rwsem
);
5897 ext4_clear_inode_flag(inode
, EXT4_INODE_JOURNAL_DATA
);
5899 ext4_set_aops(inode
);
5901 jbd2_journal_unlock_updates(journal
);
5902 percpu_up_write(&sbi
->s_writepages_rwsem
);
5905 up_write(&EXT4_I(inode
)->i_mmap_sem
);
5907 /* Finally we can mark the inode as dirty. */
5909 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
5911 return PTR_ERR(handle
);
5913 err
= ext4_mark_inode_dirty(handle
, inode
);
5914 ext4_handle_sync(handle
);
5915 ext4_journal_stop(handle
);
5916 ext4_std_error(inode
->i_sb
, err
);
5921 static int ext4_bh_unmapped(handle_t
*handle
, struct buffer_head
*bh
)
5923 return !buffer_mapped(bh
);
5926 vm_fault_t
ext4_page_mkwrite(struct vm_fault
*vmf
)
5928 struct vm_area_struct
*vma
= vmf
->vma
;
5929 struct page
*page
= vmf
->page
;
5934 struct file
*file
= vma
->vm_file
;
5935 struct inode
*inode
= file_inode(file
);
5936 struct address_space
*mapping
= inode
->i_mapping
;
5938 get_block_t
*get_block
;
5941 if (unlikely(IS_IMMUTABLE(inode
)))
5942 return VM_FAULT_SIGBUS
;
5944 sb_start_pagefault(inode
->i_sb
);
5945 file_update_time(vma
->vm_file
);
5947 down_read(&EXT4_I(inode
)->i_mmap_sem
);
5949 err
= ext4_convert_inline_data(inode
);
5953 /* Delalloc case is easy... */
5954 if (test_opt(inode
->i_sb
, DELALLOC
) &&
5955 !ext4_should_journal_data(inode
) &&
5956 !ext4_nonda_switch(inode
->i_sb
)) {
5958 err
= block_page_mkwrite(vma
, vmf
,
5959 ext4_da_get_block_prep
);
5960 } while (err
== -ENOSPC
&&
5961 ext4_should_retry_alloc(inode
->i_sb
, &retries
));
5966 size
= i_size_read(inode
);
5967 /* Page got truncated from under us? */
5968 if (page
->mapping
!= mapping
|| page_offset(page
) > size
) {
5970 ret
= VM_FAULT_NOPAGE
;
5974 if (page
->index
== size
>> PAGE_SHIFT
)
5975 len
= size
& ~PAGE_MASK
;
5979 * Return if we have all the buffers mapped. This avoids the need to do
5980 * journal_start/journal_stop which can block and take a long time
5982 if (page_has_buffers(page
)) {
5983 if (!ext4_walk_page_buffers(NULL
, page_buffers(page
),
5985 ext4_bh_unmapped
)) {
5986 /* Wait so that we don't change page under IO */
5987 wait_for_stable_page(page
);
5988 ret
= VM_FAULT_LOCKED
;
5993 /* OK, we need to fill the hole... */
5994 if (ext4_should_dioread_nolock(inode
))
5995 get_block
= ext4_get_block_unwritten
;
5997 get_block
= ext4_get_block
;
5999 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
,
6000 ext4_writepage_trans_blocks(inode
));
6001 if (IS_ERR(handle
)) {
6002 ret
= VM_FAULT_SIGBUS
;
6005 err
= block_page_mkwrite(vma
, vmf
, get_block
);
6006 if (!err
&& ext4_should_journal_data(inode
)) {
6007 if (ext4_walk_page_buffers(handle
, page_buffers(page
), 0,
6008 PAGE_SIZE
, NULL
, do_journal_get_write_access
)) {
6010 ret
= VM_FAULT_SIGBUS
;
6011 ext4_journal_stop(handle
);
6014 ext4_set_inode_state(inode
, EXT4_STATE_JDATA
);
6016 ext4_journal_stop(handle
);
6017 if (err
== -ENOSPC
&& ext4_should_retry_alloc(inode
->i_sb
, &retries
))
6020 ret
= block_page_mkwrite_return(err
);
6022 up_read(&EXT4_I(inode
)->i_mmap_sem
);
6023 sb_end_pagefault(inode
->i_sb
);
6027 vm_fault_t
ext4_filemap_fault(struct vm_fault
*vmf
)
6029 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
6032 down_read(&EXT4_I(inode
)->i_mmap_sem
);
6033 ret
= filemap_fault(vmf
);
6034 up_read(&EXT4_I(inode
)->i_mmap_sem
);