| 1 | // SPDX-License-Identifier: GPL-2.0 | 
|---|
| 2 | /* | 
|---|
| 3 | *  linux/fs/ext4/inode.c | 
|---|
| 4 | * | 
|---|
| 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) | 
|---|
| 9 | * | 
|---|
| 10 | *  from | 
|---|
| 11 | * | 
|---|
| 12 | *  linux/fs/minix/inode.c | 
|---|
| 13 | * | 
|---|
| 14 | *  Copyright (C) 1991, 1992  Linus Torvalds | 
|---|
| 15 | * | 
|---|
| 16 | *  64-bit file support on 64-bit platforms by Jakub Jelinek | 
|---|
| 17 | *	(jj@sunsite.ms.mff.cuni.cz) | 
|---|
| 18 | * | 
|---|
| 19 | *  Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 22 | #include <linux/fs.h> | 
|---|
| 23 | #include <linux/mount.h> | 
|---|
| 24 | #include <linux/time.h> | 
|---|
| 25 | #include <linux/highuid.h> | 
|---|
| 26 | #include <linux/pagemap.h> | 
|---|
| 27 | #include <linux/dax.h> | 
|---|
| 28 | #include <linux/quotaops.h> | 
|---|
| 29 | #include <linux/string.h> | 
|---|
| 30 | #include <linux/buffer_head.h> | 
|---|
| 31 | #include <linux/writeback.h> | 
|---|
| 32 | #include <linux/pagevec.h> | 
|---|
| 33 | #include <linux/mpage.h> | 
|---|
| 34 | #include <linux/rmap.h> | 
|---|
| 35 | #include <linux/namei.h> | 
|---|
| 36 | #include <linux/uio.h> | 
|---|
| 37 | #include <linux/bio.h> | 
|---|
| 38 | #include <linux/workqueue.h> | 
|---|
| 39 | #include <linux/kernel.h> | 
|---|
| 40 | #include <linux/printk.h> | 
|---|
| 41 | #include <linux/slab.h> | 
|---|
| 42 | #include <linux/bitops.h> | 
|---|
| 43 | #include <linux/iomap.h> | 
|---|
| 44 | #include <linux/iversion.h> | 
|---|
| 45 |  | 
|---|
| 46 | #include "ext4_jbd2.h" | 
|---|
| 47 | #include "xattr.h" | 
|---|
| 48 | #include "acl.h" | 
|---|
| 49 | #include "truncate.h" | 
|---|
| 50 |  | 
|---|
| 51 | #include <trace/events/ext4.h> | 
|---|
| 52 |  | 
|---|
| 53 | static void ext4_journalled_zero_new_buffers(handle_t *handle, | 
|---|
| 54 | struct inode *inode, | 
|---|
| 55 | struct folio *folio, | 
|---|
| 56 | unsigned from, unsigned to); | 
|---|
| 57 |  | 
|---|
| 58 | static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, | 
|---|
| 59 | struct ext4_inode_info *ei) | 
|---|
| 60 | { | 
|---|
| 61 | __u32 csum; | 
|---|
| 62 | __u16 dummy_csum = 0; | 
|---|
| 63 | int offset = offsetof(struct ext4_inode, i_checksum_lo); | 
|---|
| 64 | unsigned int csum_size = sizeof(dummy_csum); | 
|---|
| 65 |  | 
|---|
| 66 | csum = ext4_chksum(crc: ei->i_csum_seed, address: (__u8 *)raw, length: offset); | 
|---|
| 67 | csum = ext4_chksum(crc: csum, address: (__u8 *)&dummy_csum, length: csum_size); | 
|---|
| 68 | offset += csum_size; | 
|---|
| 69 | csum = ext4_chksum(crc: csum, address: (__u8 *)raw + offset, | 
|---|
| 70 | EXT4_GOOD_OLD_INODE_SIZE - offset); | 
|---|
| 71 |  | 
|---|
| 72 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { | 
|---|
| 73 | offset = offsetof(struct ext4_inode, i_checksum_hi); | 
|---|
| 74 | csum = ext4_chksum(crc: csum, address: (__u8 *)raw + EXT4_GOOD_OLD_INODE_SIZE, | 
|---|
| 75 | length: offset - EXT4_GOOD_OLD_INODE_SIZE); | 
|---|
| 76 | if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { | 
|---|
| 77 | csum = ext4_chksum(crc: csum, address: (__u8 *)&dummy_csum, | 
|---|
| 78 | length: csum_size); | 
|---|
| 79 | offset += csum_size; | 
|---|
| 80 | } | 
|---|
| 81 | csum = ext4_chksum(crc: csum, address: (__u8 *)raw + offset, | 
|---|
| 82 | EXT4_INODE_SIZE(inode->i_sb) - offset); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | return csum; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, | 
|---|
| 89 | struct ext4_inode_info *ei) | 
|---|
| 90 | { | 
|---|
| 91 | __u32 provided, calculated; | 
|---|
| 92 |  | 
|---|
| 93 | if (EXT4_SB(sb: inode->i_sb)->s_es->s_creator_os != | 
|---|
| 94 | cpu_to_le32(EXT4_OS_LINUX) || | 
|---|
| 95 | !ext4_has_feature_metadata_csum(sb: inode->i_sb)) | 
|---|
| 96 | return 1; | 
|---|
| 97 |  | 
|---|
| 98 | provided = le16_to_cpu(raw->i_checksum_lo); | 
|---|
| 99 | calculated = ext4_inode_csum(inode, raw, ei); | 
|---|
| 100 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && | 
|---|
| 101 | EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) | 
|---|
| 102 | provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; | 
|---|
| 103 | else | 
|---|
| 104 | calculated &= 0xFFFF; | 
|---|
| 105 |  | 
|---|
| 106 | return provided == calculated; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, | 
|---|
| 110 | struct ext4_inode_info *ei) | 
|---|
| 111 | { | 
|---|
| 112 | __u32 csum; | 
|---|
| 113 |  | 
|---|
| 114 | if (EXT4_SB(sb: inode->i_sb)->s_es->s_creator_os != | 
|---|
| 115 | cpu_to_le32(EXT4_OS_LINUX) || | 
|---|
| 116 | !ext4_has_feature_metadata_csum(sb: inode->i_sb)) | 
|---|
| 117 | return; | 
|---|
| 118 |  | 
|---|
| 119 | csum = ext4_inode_csum(inode, raw, ei); | 
|---|
| 120 | raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); | 
|---|
| 121 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && | 
|---|
| 122 | EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) | 
|---|
| 123 | raw->i_checksum_hi = cpu_to_le16(csum >> 16); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | static inline int ext4_begin_ordered_truncate(struct inode *inode, | 
|---|
| 127 | loff_t new_size) | 
|---|
| 128 | { | 
|---|
| 129 | trace_ext4_begin_ordered_truncate(inode, new_size); | 
|---|
| 130 | /* | 
|---|
| 131 | * If jinode is zero, then we never opened the file for | 
|---|
| 132 | * writing, so there's no need to call | 
|---|
| 133 | * jbd2_journal_begin_ordered_truncate() since there's no | 
|---|
| 134 | * outstanding writes we need to flush. | 
|---|
| 135 | */ | 
|---|
| 136 | if (!EXT4_I(inode)->jinode) | 
|---|
| 137 | return 0; | 
|---|
| 138 | return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), | 
|---|
| 139 | EXT4_I(inode)->jinode, | 
|---|
| 140 | new_size); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | /* | 
|---|
| 144 | * Test whether an inode is a fast symlink. | 
|---|
| 145 | * A fast symlink has its symlink data stored in ext4_inode_info->i_data. | 
|---|
| 146 | */ | 
|---|
| 147 | int ext4_inode_is_fast_symlink(struct inode *inode) | 
|---|
| 148 | { | 
|---|
| 149 | if (!ext4_has_feature_ea_inode(sb: inode->i_sb)) { | 
|---|
| 150 | int ea_blocks = EXT4_I(inode)->i_file_acl ? | 
|---|
| 151 | EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0; | 
|---|
| 152 |  | 
|---|
| 153 | if (ext4_has_inline_data(inode)) | 
|---|
| 154 | return 0; | 
|---|
| 155 |  | 
|---|
| 156 | return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); | 
|---|
| 157 | } | 
|---|
| 158 | return S_ISLNK(inode->i_mode) && inode->i_size && | 
|---|
| 159 | (inode->i_size < EXT4_N_BLOCKS * 4); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | /* | 
|---|
| 163 | * Called at the last iput() if i_nlink is zero. | 
|---|
| 164 | */ | 
|---|
| 165 | void ext4_evict_inode(struct inode *inode) | 
|---|
| 166 | { | 
|---|
| 167 | handle_t *handle; | 
|---|
| 168 | int err; | 
|---|
| 169 | /* | 
|---|
| 170 | * Credits for final inode cleanup and freeing: | 
|---|
| 171 | * sb + inode (ext4_orphan_del()), block bitmap, group descriptor | 
|---|
| 172 | * (xattr block freeing), bitmap, group descriptor (inode freeing) | 
|---|
| 173 | */ | 
|---|
| 174 | int  = 6; | 
|---|
| 175 | struct ext4_xattr_inode_array *ea_inode_array = NULL; | 
|---|
| 176 | bool freeze_protected = false; | 
|---|
| 177 |  | 
|---|
| 178 | trace_ext4_evict_inode(inode); | 
|---|
| 179 |  | 
|---|
| 180 | dax_break_layout_final(inode); | 
|---|
| 181 |  | 
|---|
| 182 | if (EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL) | 
|---|
| 183 | ext4_evict_ea_inode(inode); | 
|---|
| 184 | if (inode->i_nlink) { | 
|---|
| 185 | truncate_inode_pages_final(&inode->i_data); | 
|---|
| 186 |  | 
|---|
| 187 | goto no_delete; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | if (is_bad_inode(inode)) | 
|---|
| 191 | goto no_delete; | 
|---|
| 192 | dquot_initialize(inode); | 
|---|
| 193 |  | 
|---|
| 194 | if (ext4_should_order_data(inode)) | 
|---|
| 195 | ext4_begin_ordered_truncate(inode, new_size: 0); | 
|---|
| 196 | truncate_inode_pages_final(&inode->i_data); | 
|---|
| 197 |  | 
|---|
| 198 | /* | 
|---|
| 199 | * For inodes with journalled data, transaction commit could have | 
|---|
| 200 | * dirtied the inode. And for inodes with dioread_nolock, unwritten | 
|---|
| 201 | * extents converting worker could merge extents and also have dirtied | 
|---|
| 202 | * the inode. Flush worker is ignoring it because of I_FREEING flag but | 
|---|
| 203 | * we still need to remove the inode from the writeback lists. | 
|---|
| 204 | */ | 
|---|
| 205 | if (!list_empty_careful(head: &inode->i_io_list)) | 
|---|
| 206 | inode_io_list_del(inode); | 
|---|
| 207 |  | 
|---|
| 208 | /* | 
|---|
| 209 | * Protect us against freezing - iput() caller didn't have to have any | 
|---|
| 210 | * protection against it. When we are in a running transaction though, | 
|---|
| 211 | * we are already protected against freezing and we cannot grab further | 
|---|
| 212 | * protection due to lock ordering constraints. | 
|---|
| 213 | */ | 
|---|
| 214 | if (!ext4_journal_current_handle()) { | 
|---|
| 215 | sb_start_intwrite(sb: inode->i_sb); | 
|---|
| 216 | freeze_protected = true; | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | if (!IS_NOQUOTA(inode)) | 
|---|
| 220 | extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb); | 
|---|
| 221 |  | 
|---|
| 222 | /* | 
|---|
| 223 | * Block bitmap, group descriptor, and inode are accounted in both | 
|---|
| 224 | * ext4_blocks_for_truncate() and extra_credits. So subtract 3. | 
|---|
| 225 | */ | 
|---|
| 226 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, | 
|---|
| 227 | ext4_blocks_for_truncate(inode) + extra_credits - 3); | 
|---|
| 228 | if (IS_ERR(ptr: handle)) { | 
|---|
| 229 | ext4_std_error(inode->i_sb, PTR_ERR(handle)); | 
|---|
| 230 | /* | 
|---|
| 231 | * If we're going to skip the normal cleanup, we still need to | 
|---|
| 232 | * make sure that the in-core orphan linked list is properly | 
|---|
| 233 | * cleaned up. | 
|---|
| 234 | */ | 
|---|
| 235 | ext4_orphan_del(NULL, inode); | 
|---|
| 236 | if (freeze_protected) | 
|---|
| 237 | sb_end_intwrite(sb: inode->i_sb); | 
|---|
| 238 | goto no_delete; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | if (IS_SYNC(inode)) | 
|---|
| 242 | ext4_handle_sync(handle); | 
|---|
| 243 |  | 
|---|
| 244 | /* | 
|---|
| 245 | * Set inode->i_size to 0 before calling ext4_truncate(). We need | 
|---|
| 246 | * special handling of symlinks here because i_size is used to | 
|---|
| 247 | * determine whether ext4_inode_info->i_data contains symlink data or | 
|---|
| 248 | * block mappings. Setting i_size to 0 will remove its fast symlink | 
|---|
| 249 | * status. Erase i_data so that it becomes a valid empty block map. | 
|---|
| 250 | */ | 
|---|
| 251 | if (ext4_inode_is_fast_symlink(inode)) | 
|---|
| 252 | memset(EXT4_I(inode)->i_data, c: 0, n: sizeof(EXT4_I(inode)->i_data)); | 
|---|
| 253 | inode->i_size = 0; | 
|---|
| 254 | err = ext4_mark_inode_dirty(handle, inode); | 
|---|
| 255 | if (err) { | 
|---|
| 256 | ext4_warning(inode->i_sb, | 
|---|
| 257 | "couldn't mark inode dirty (err %d)", err); | 
|---|
| 258 | goto stop_handle; | 
|---|
| 259 | } | 
|---|
| 260 | if (inode->i_blocks) { | 
|---|
| 261 | err = ext4_truncate(inode); | 
|---|
| 262 | if (err) { | 
|---|
| 263 | ext4_error_err(inode->i_sb, -err, | 
|---|
| 264 | "couldn't truncate inode %lu (err %d)", | 
|---|
| 265 | inode->i_ino, err); | 
|---|
| 266 | goto stop_handle; | 
|---|
| 267 | } | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | /* Remove xattr references. */ | 
|---|
| 271 | err = ext4_xattr_delete_inode(handle, inode, array: &ea_inode_array, | 
|---|
| 272 | extra_credits); | 
|---|
| 273 | if (err) { | 
|---|
| 274 | ext4_warning(inode->i_sb, "xattr delete (err %d)", err); | 
|---|
| 275 | stop_handle: | 
|---|
| 276 | ext4_journal_stop(handle); | 
|---|
| 277 | ext4_orphan_del(NULL, inode); | 
|---|
| 278 | if (freeze_protected) | 
|---|
| 279 | sb_end_intwrite(sb: inode->i_sb); | 
|---|
| 280 | ext4_xattr_inode_array_free(array: ea_inode_array); | 
|---|
| 281 | goto no_delete; | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | /* | 
|---|
| 285 | * Kill off the orphan record which ext4_truncate created. | 
|---|
| 286 | * AKPM: I think this can be inside the above `if'. | 
|---|
| 287 | * Note that ext4_orphan_del() has to be able to cope with the | 
|---|
| 288 | * deletion of a non-existent orphan - this is because we don't | 
|---|
| 289 | * know if ext4_truncate() actually created an orphan record. | 
|---|
| 290 | * (Well, we could do this if we need to, but heck - it works) | 
|---|
| 291 | */ | 
|---|
| 292 | ext4_orphan_del(handle, inode); | 
|---|
| 293 | EXT4_I(inode)->i_dtime	= (__u32)ktime_get_real_seconds(); | 
|---|
| 294 |  | 
|---|
| 295 | /* | 
|---|
| 296 | * One subtle ordering requirement: if anything has gone wrong | 
|---|
| 297 | * (transaction abort, IO errors, whatever), then we can still | 
|---|
| 298 | * do these next steps (the fs will already have been marked as | 
|---|
| 299 | * having errors), but we can't free the inode if the mark_dirty | 
|---|
| 300 | * fails. | 
|---|
| 301 | */ | 
|---|
| 302 | if (ext4_mark_inode_dirty(handle, inode)) | 
|---|
| 303 | /* If that failed, just do the required in-core inode clear. */ | 
|---|
| 304 | ext4_clear_inode(inode); | 
|---|
| 305 | else | 
|---|
| 306 | ext4_free_inode(handle, inode); | 
|---|
| 307 | ext4_journal_stop(handle); | 
|---|
| 308 | if (freeze_protected) | 
|---|
| 309 | sb_end_intwrite(sb: inode->i_sb); | 
|---|
| 310 | ext4_xattr_inode_array_free(array: ea_inode_array); | 
|---|
| 311 | return; | 
|---|
| 312 | no_delete: | 
|---|
| 313 | /* | 
|---|
| 314 | * Check out some where else accidentally dirty the evicting inode, | 
|---|
| 315 | * which may probably cause inode use-after-free issues later. | 
|---|
| 316 | */ | 
|---|
| 317 | WARN_ON_ONCE(!list_empty_careful(&inode->i_io_list)); | 
|---|
| 318 |  | 
|---|
| 319 | if (!list_empty(head: &EXT4_I(inode)->i_fc_list)) | 
|---|
| 320 | ext4_fc_mark_ineligible(sb: inode->i_sb, reason: EXT4_FC_REASON_NOMEM, NULL); | 
|---|
| 321 | ext4_clear_inode(inode);	/* We must guarantee clearing of inode... */ | 
|---|
| 322 | } | 
|---|
| 323 |  | 
|---|
| 324 | #ifdef CONFIG_QUOTA | 
|---|
| 325 | qsize_t *ext4_get_reserved_space(struct inode *inode) | 
|---|
| 326 | { | 
|---|
| 327 | return &EXT4_I(inode)->i_reserved_quota; | 
|---|
| 328 | } | 
|---|
| 329 | #endif | 
|---|
| 330 |  | 
|---|
| 331 | /* | 
|---|
| 332 | * Called with i_data_sem down, which is important since we can call | 
|---|
| 333 | * ext4_discard_preallocations() from here. | 
|---|
| 334 | */ | 
|---|
| 335 | void ext4_da_update_reserve_space(struct inode *inode, | 
|---|
| 336 | int used, int quota_claim) | 
|---|
| 337 | { | 
|---|
| 338 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 339 | struct ext4_inode_info *ei = EXT4_I(inode); | 
|---|
| 340 |  | 
|---|
| 341 | spin_lock(lock: &ei->i_block_reservation_lock); | 
|---|
| 342 | trace_ext4_da_update_reserve_space(inode, used_blocks: used, quota_claim); | 
|---|
| 343 | if (unlikely(used > ei->i_reserved_data_blocks)) { | 
|---|
| 344 | ext4_warning(inode->i_sb, "%s: ino %lu, used %d " | 
|---|
| 345 | "with only %d reserved data blocks", | 
|---|
| 346 | __func__, inode->i_ino, used, | 
|---|
| 347 | ei->i_reserved_data_blocks); | 
|---|
| 348 | WARN_ON(1); | 
|---|
| 349 | used = ei->i_reserved_data_blocks; | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | /* Update per-inode reservations */ | 
|---|
| 353 | ei->i_reserved_data_blocks -= used; | 
|---|
| 354 | percpu_counter_sub(fbc: &sbi->s_dirtyclusters_counter, amount: used); | 
|---|
| 355 |  | 
|---|
| 356 | spin_unlock(lock: &ei->i_block_reservation_lock); | 
|---|
| 357 |  | 
|---|
| 358 | /* Update quota subsystem for data blocks */ | 
|---|
| 359 | if (quota_claim) | 
|---|
| 360 | dquot_claim_block(inode, EXT4_C2B(sbi, used)); | 
|---|
| 361 | else { | 
|---|
| 362 | /* | 
|---|
| 363 | * We did fallocate with an offset that is already delayed | 
|---|
| 364 | * allocated. So on delayed allocated writeback we should | 
|---|
| 365 | * not re-claim the quota for fallocated blocks. | 
|---|
| 366 | */ | 
|---|
| 367 | dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); | 
|---|
| 368 | } | 
|---|
| 369 |  | 
|---|
| 370 | /* | 
|---|
| 371 | * If we have done all the pending block allocations and if | 
|---|
| 372 | * there aren't any writers on the inode, we can discard the | 
|---|
| 373 | * inode's preallocations. | 
|---|
| 374 | */ | 
|---|
| 375 | if ((ei->i_reserved_data_blocks == 0) && | 
|---|
| 376 | !inode_is_open_for_write(inode)) | 
|---|
| 377 | ext4_discard_preallocations(inode); | 
|---|
| 378 | } | 
|---|
| 379 |  | 
|---|
| 380 | static int __check_block_validity(struct inode *inode, const char *func, | 
|---|
| 381 | unsigned int line, | 
|---|
| 382 | struct ext4_map_blocks *map) | 
|---|
| 383 | { | 
|---|
| 384 | journal_t *journal = EXT4_SB(sb: inode->i_sb)->s_journal; | 
|---|
| 385 |  | 
|---|
| 386 | if (journal && inode == journal->j_inode) | 
|---|
| 387 | return 0; | 
|---|
| 388 |  | 
|---|
| 389 | if (!ext4_inode_block_valid(inode, start_blk: map->m_pblk, count: map->m_len)) { | 
|---|
| 390 | ext4_error_inode(inode, func, line, map->m_pblk, | 
|---|
| 391 | "lblock %lu mapped to illegal pblock %llu " | 
|---|
| 392 | "(length %d)", (unsigned long) map->m_lblk, | 
|---|
| 393 | map->m_pblk, map->m_len); | 
|---|
| 394 | return -EFSCORRUPTED; | 
|---|
| 395 | } | 
|---|
| 396 | return 0; | 
|---|
| 397 | } | 
|---|
| 398 |  | 
|---|
| 399 | int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, | 
|---|
| 400 | ext4_lblk_t len) | 
|---|
| 401 | { | 
|---|
| 402 | int ret; | 
|---|
| 403 |  | 
|---|
| 404 | if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) | 
|---|
| 405 | return fscrypt_zeroout_range(inode, lblk, pblk, len); | 
|---|
| 406 |  | 
|---|
| 407 | ret = sb_issue_zeroout(sb: inode->i_sb, block: pblk, nr_blocks: len, GFP_NOFS); | 
|---|
| 408 | if (ret > 0) | 
|---|
| 409 | ret = 0; | 
|---|
| 410 |  | 
|---|
| 411 | return ret; | 
|---|
| 412 | } | 
|---|
| 413 |  | 
|---|
| 414 | /* | 
|---|
| 415 | * For generic regular files, when updating the extent tree, Ext4 should | 
|---|
| 416 | * hold the i_rwsem and invalidate_lock exclusively. This ensures | 
|---|
| 417 | * exclusion against concurrent page faults, as well as reads and writes. | 
|---|
| 418 | */ | 
|---|
| 419 | #ifdef CONFIG_EXT4_DEBUG | 
|---|
| 420 | void ext4_check_map_extents_env(struct inode *inode) | 
|---|
| 421 | { | 
|---|
| 422 | if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) | 
|---|
| 423 | return; | 
|---|
| 424 |  | 
|---|
| 425 | if (!S_ISREG(inode->i_mode) || | 
|---|
| 426 | IS_NOQUOTA(inode) || IS_VERITY(inode) || | 
|---|
| 427 | is_special_ino(inode->i_sb, inode->i_ino) || | 
|---|
| 428 | (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) || | 
|---|
| 429 | ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE) || | 
|---|
| 430 | ext4_verity_in_progress(inode)) | 
|---|
| 431 | return; | 
|---|
| 432 |  | 
|---|
| 433 | WARN_ON_ONCE(!inode_is_locked(inode) && | 
|---|
| 434 | !rwsem_is_locked(&inode->i_mapping->invalidate_lock)); | 
|---|
| 435 | } | 
|---|
| 436 | #else | 
|---|
| 437 | void ext4_check_map_extents_env(struct inode *inode) {} | 
|---|
| 438 | #endif | 
|---|
| 439 |  | 
|---|
| 440 | #define check_block_validity(inode, map)	\ | 
|---|
| 441 | __check_block_validity((inode), __func__, __LINE__, (map)) | 
|---|
| 442 |  | 
|---|
| 443 | #ifdef ES_AGGRESSIVE_TEST | 
|---|
| 444 | static void ext4_map_blocks_es_recheck(handle_t *handle, | 
|---|
| 445 | struct inode *inode, | 
|---|
| 446 | struct ext4_map_blocks *es_map, | 
|---|
| 447 | struct ext4_map_blocks *map, | 
|---|
| 448 | int flags) | 
|---|
| 449 | { | 
|---|
| 450 | int retval; | 
|---|
| 451 |  | 
|---|
| 452 | map->m_flags = 0; | 
|---|
| 453 | /* | 
|---|
| 454 | * There is a race window that the result is not the same. | 
|---|
| 455 | * e.g. xfstests #223 when dioread_nolock enables.  The reason | 
|---|
| 456 | * is that we lookup a block mapping in extent status tree with | 
|---|
| 457 | * out taking i_data_sem.  So at the time the unwritten extent | 
|---|
| 458 | * could be converted. | 
|---|
| 459 | */ | 
|---|
| 460 | down_read(&EXT4_I(inode)->i_data_sem); | 
|---|
| 461 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { | 
|---|
| 462 | retval = ext4_ext_map_blocks(handle, inode, map, 0); | 
|---|
| 463 | } else { | 
|---|
| 464 | retval = ext4_ind_map_blocks(handle, inode, map, 0); | 
|---|
| 465 | } | 
|---|
| 466 | up_read((&EXT4_I(inode)->i_data_sem)); | 
|---|
| 467 |  | 
|---|
| 468 | /* | 
|---|
| 469 | * We don't check m_len because extent will be collpased in status | 
|---|
| 470 | * tree.  So the m_len might not equal. | 
|---|
| 471 | */ | 
|---|
| 472 | if (es_map->m_lblk != map->m_lblk || | 
|---|
| 473 | es_map->m_flags != map->m_flags || | 
|---|
| 474 | es_map->m_pblk != map->m_pblk) { | 
|---|
| 475 | printk( "ES cache assertion failed for inode: %lu " | 
|---|
| 476 | "es_cached ex [%d/%d/%llu/%x] != " | 
|---|
| 477 | "found ex [%d/%d/%llu/%x] retval %d flags %x\n", | 
|---|
| 478 | inode->i_ino, es_map->m_lblk, es_map->m_len, | 
|---|
| 479 | es_map->m_pblk, es_map->m_flags, map->m_lblk, | 
|---|
| 480 | map->m_len, map->m_pblk, map->m_flags, | 
|---|
| 481 | retval, flags); | 
|---|
| 482 | } | 
|---|
| 483 | } | 
|---|
| 484 | #endif /* ES_AGGRESSIVE_TEST */ | 
|---|
| 485 |  | 
|---|
| 486 | static int ext4_map_query_blocks_next_in_leaf(handle_t *handle, | 
|---|
| 487 | struct inode *inode, struct ext4_map_blocks *map, | 
|---|
| 488 | unsigned int orig_mlen) | 
|---|
| 489 | { | 
|---|
| 490 | struct ext4_map_blocks map2; | 
|---|
| 491 | unsigned int status, status2; | 
|---|
| 492 | int retval; | 
|---|
| 493 |  | 
|---|
| 494 | status = map->m_flags & EXT4_MAP_UNWRITTEN ? | 
|---|
| 495 | EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; | 
|---|
| 496 |  | 
|---|
| 497 | WARN_ON_ONCE(!(map->m_flags & EXT4_MAP_QUERY_LAST_IN_LEAF)); | 
|---|
| 498 | WARN_ON_ONCE(orig_mlen <= map->m_len); | 
|---|
| 499 |  | 
|---|
| 500 | /* Prepare map2 for lookup in next leaf block */ | 
|---|
| 501 | map2.m_lblk = map->m_lblk + map->m_len; | 
|---|
| 502 | map2.m_len = orig_mlen - map->m_len; | 
|---|
| 503 | map2.m_flags = 0; | 
|---|
| 504 | retval = ext4_ext_map_blocks(handle, inode, map: &map2, flags: 0); | 
|---|
| 505 |  | 
|---|
| 506 | if (retval <= 0) { | 
|---|
| 507 | ext4_es_insert_extent(inode, lblk: map->m_lblk, len: map->m_len, | 
|---|
| 508 | pblk: map->m_pblk, status, delalloc_reserve_used: false); | 
|---|
| 509 | return map->m_len; | 
|---|
| 510 | } | 
|---|
| 511 |  | 
|---|
| 512 | if (unlikely(retval != map2.m_len)) { | 
|---|
| 513 | ext4_warning(inode->i_sb, | 
|---|
| 514 | "ES len assertion failed for inode " | 
|---|
| 515 | "%lu: retval %d != map->m_len %d", | 
|---|
| 516 | inode->i_ino, retval, map2.m_len); | 
|---|
| 517 | WARN_ON(1); | 
|---|
| 518 | } | 
|---|
| 519 |  | 
|---|
| 520 | status2 = map2.m_flags & EXT4_MAP_UNWRITTEN ? | 
|---|
| 521 | EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; | 
|---|
| 522 |  | 
|---|
| 523 | /* | 
|---|
| 524 | * If map2 is contiguous with map, then let's insert it as a single | 
|---|
| 525 | * extent in es cache and return the combined length of both the maps. | 
|---|
| 526 | */ | 
|---|
| 527 | if (map->m_pblk + map->m_len == map2.m_pblk && | 
|---|
| 528 | status == status2) { | 
|---|
| 529 | ext4_es_insert_extent(inode, lblk: map->m_lblk, | 
|---|
| 530 | len: map->m_len + map2.m_len, pblk: map->m_pblk, | 
|---|
| 531 | status, delalloc_reserve_used: false); | 
|---|
| 532 | map->m_len += map2.m_len; | 
|---|
| 533 | } else { | 
|---|
| 534 | ext4_es_insert_extent(inode, lblk: map->m_lblk, len: map->m_len, | 
|---|
| 535 | pblk: map->m_pblk, status, delalloc_reserve_used: false); | 
|---|
| 536 | } | 
|---|
| 537 |  | 
|---|
| 538 | return map->m_len; | 
|---|
| 539 | } | 
|---|
| 540 |  | 
|---|
| 541 | static int ext4_map_query_blocks(handle_t *handle, struct inode *inode, | 
|---|
| 542 | struct ext4_map_blocks *map, int flags) | 
|---|
| 543 | { | 
|---|
| 544 | unsigned int status; | 
|---|
| 545 | int retval; | 
|---|
| 546 | unsigned int orig_mlen = map->m_len; | 
|---|
| 547 |  | 
|---|
| 548 | flags &= EXT4_EX_QUERY_FILTER; | 
|---|
| 549 | if (ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS)) | 
|---|
| 550 | retval = ext4_ext_map_blocks(handle, inode, map, flags); | 
|---|
| 551 | else | 
|---|
| 552 | retval = ext4_ind_map_blocks(handle, inode, map, flags); | 
|---|
| 553 |  | 
|---|
| 554 | if (retval <= 0) | 
|---|
| 555 | return retval; | 
|---|
| 556 |  | 
|---|
| 557 | if (unlikely(retval != map->m_len)) { | 
|---|
| 558 | ext4_warning(inode->i_sb, | 
|---|
| 559 | "ES len assertion failed for inode " | 
|---|
| 560 | "%lu: retval %d != map->m_len %d", | 
|---|
| 561 | inode->i_ino, retval, map->m_len); | 
|---|
| 562 | WARN_ON(1); | 
|---|
| 563 | } | 
|---|
| 564 |  | 
|---|
| 565 | /* | 
|---|
| 566 | * No need to query next in leaf: | 
|---|
| 567 | * - if returned extent is not last in leaf or | 
|---|
| 568 | * - if the last in leaf is the full requested range | 
|---|
| 569 | */ | 
|---|
| 570 | if (!(map->m_flags & EXT4_MAP_QUERY_LAST_IN_LEAF) || | 
|---|
| 571 | map->m_len == orig_mlen) { | 
|---|
| 572 | status = map->m_flags & EXT4_MAP_UNWRITTEN ? | 
|---|
| 573 | EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; | 
|---|
| 574 | ext4_es_insert_extent(inode, lblk: map->m_lblk, len: map->m_len, | 
|---|
| 575 | pblk: map->m_pblk, status, delalloc_reserve_used: false); | 
|---|
| 576 | return retval; | 
|---|
| 577 | } | 
|---|
| 578 |  | 
|---|
| 579 | return ext4_map_query_blocks_next_in_leaf(handle, inode, map, | 
|---|
| 580 | orig_mlen); | 
|---|
| 581 | } | 
|---|
| 582 |  | 
|---|
| 583 | static int ext4_map_create_blocks(handle_t *handle, struct inode *inode, | 
|---|
| 584 | struct ext4_map_blocks *map, int flags) | 
|---|
| 585 | { | 
|---|
| 586 | struct extent_status es; | 
|---|
| 587 | unsigned int status; | 
|---|
| 588 | int err, retval = 0; | 
|---|
| 589 |  | 
|---|
| 590 | /* | 
|---|
| 591 | * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE | 
|---|
| 592 | * indicates that the blocks and quotas has already been | 
|---|
| 593 | * checked when the data was copied into the page cache. | 
|---|
| 594 | */ | 
|---|
| 595 | if (map->m_flags & EXT4_MAP_DELAYED) | 
|---|
| 596 | flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; | 
|---|
| 597 |  | 
|---|
| 598 | /* | 
|---|
| 599 | * Here we clear m_flags because after allocating an new extent, | 
|---|
| 600 | * it will be set again. | 
|---|
| 601 | */ | 
|---|
| 602 | map->m_flags &= ~EXT4_MAP_FLAGS; | 
|---|
| 603 |  | 
|---|
| 604 | /* | 
|---|
| 605 | * We need to check for EXT4 here because migrate could have | 
|---|
| 606 | * changed the inode type in between. | 
|---|
| 607 | */ | 
|---|
| 608 | if (ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS)) { | 
|---|
| 609 | retval = ext4_ext_map_blocks(handle, inode, map, flags); | 
|---|
| 610 | } else { | 
|---|
| 611 | retval = ext4_ind_map_blocks(handle, inode, map, flags); | 
|---|
| 612 |  | 
|---|
| 613 | /* | 
|---|
| 614 | * We allocated new blocks which will result in i_data's | 
|---|
| 615 | * format changing. Force the migrate to fail by clearing | 
|---|
| 616 | * migrate flags. | 
|---|
| 617 | */ | 
|---|
| 618 | if (retval > 0 && map->m_flags & EXT4_MAP_NEW) | 
|---|
| 619 | ext4_clear_inode_state(inode, bit: EXT4_STATE_EXT_MIGRATE); | 
|---|
| 620 | } | 
|---|
| 621 | if (retval <= 0) | 
|---|
| 622 | return retval; | 
|---|
| 623 |  | 
|---|
| 624 | if (unlikely(retval != map->m_len)) { | 
|---|
| 625 | ext4_warning(inode->i_sb, | 
|---|
| 626 | "ES len assertion failed for inode %lu: " | 
|---|
| 627 | "retval %d != map->m_len %d", | 
|---|
| 628 | inode->i_ino, retval, map->m_len); | 
|---|
| 629 | WARN_ON(1); | 
|---|
| 630 | } | 
|---|
| 631 |  | 
|---|
| 632 | /* | 
|---|
| 633 | * We have to zeroout blocks before inserting them into extent | 
|---|
| 634 | * status tree. Otherwise someone could look them up there and | 
|---|
| 635 | * use them before they are really zeroed. We also have to | 
|---|
| 636 | * unmap metadata before zeroing as otherwise writeback can | 
|---|
| 637 | * overwrite zeros with stale data from block device. | 
|---|
| 638 | */ | 
|---|
| 639 | if (flags & EXT4_GET_BLOCKS_ZERO && | 
|---|
| 640 | map->m_flags & EXT4_MAP_MAPPED && map->m_flags & EXT4_MAP_NEW) { | 
|---|
| 641 | err = ext4_issue_zeroout(inode, lblk: map->m_lblk, pblk: map->m_pblk, | 
|---|
| 642 | len: map->m_len); | 
|---|
| 643 | if (err) | 
|---|
| 644 | return err; | 
|---|
| 645 | } | 
|---|
| 646 |  | 
|---|
| 647 | /* | 
|---|
| 648 | * If the extent has been zeroed out, we don't need to update | 
|---|
| 649 | * extent status tree. | 
|---|
| 650 | */ | 
|---|
| 651 | if (flags & EXT4_GET_BLOCKS_PRE_IO && | 
|---|
| 652 | ext4_es_lookup_extent(inode, lblk: map->m_lblk, NULL, es: &es)) { | 
|---|
| 653 | if (ext4_es_is_written(es: &es)) | 
|---|
| 654 | return retval; | 
|---|
| 655 | } | 
|---|
| 656 |  | 
|---|
| 657 | status = map->m_flags & EXT4_MAP_UNWRITTEN ? | 
|---|
| 658 | EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; | 
|---|
| 659 | ext4_es_insert_extent(inode, lblk: map->m_lblk, len: map->m_len, pblk: map->m_pblk, | 
|---|
| 660 | status, delalloc_reserve_used: flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE); | 
|---|
| 661 |  | 
|---|
| 662 | return retval; | 
|---|
| 663 | } | 
|---|
| 664 |  | 
|---|
| 665 | /* | 
|---|
| 666 | * The ext4_map_blocks() function tries to look up the requested blocks, | 
|---|
| 667 | * and returns if the blocks are already mapped. | 
|---|
| 668 | * | 
|---|
| 669 | * Otherwise it takes the write lock of the i_data_sem and allocate blocks | 
|---|
| 670 | * and store the allocated blocks in the result buffer head and mark it | 
|---|
| 671 | * mapped. | 
|---|
| 672 | * | 
|---|
| 673 | * If file type is extents based, it will call ext4_ext_map_blocks(), | 
|---|
| 674 | * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping | 
|---|
| 675 | * based files | 
|---|
| 676 | * | 
|---|
| 677 | * On success, it returns the number of blocks being mapped or allocated. | 
|---|
| 678 | * If flags doesn't contain EXT4_GET_BLOCKS_CREATE the blocks are | 
|---|
| 679 | * pre-allocated and unwritten, the resulting @map is marked as unwritten. | 
|---|
| 680 | * If the flags contain EXT4_GET_BLOCKS_CREATE, it will mark @map as mapped. | 
|---|
| 681 | * | 
|---|
| 682 | * It returns 0 if plain look up failed (blocks have not been allocated), in | 
|---|
| 683 | * that case, @map is returned as unmapped but we still do fill map->m_len to | 
|---|
| 684 | * indicate the length of a hole starting at map->m_lblk. | 
|---|
| 685 | * | 
|---|
| 686 | * It returns the error in case of allocation failure. | 
|---|
| 687 | */ | 
|---|
| 688 | int ext4_map_blocks(handle_t *handle, struct inode *inode, | 
|---|
| 689 | struct ext4_map_blocks *map, int flags) | 
|---|
| 690 | { | 
|---|
| 691 | struct extent_status es; | 
|---|
| 692 | int retval; | 
|---|
| 693 | int ret = 0; | 
|---|
| 694 | unsigned int orig_mlen = map->m_len; | 
|---|
| 695 | #ifdef ES_AGGRESSIVE_TEST | 
|---|
| 696 | struct ext4_map_blocks orig_map; | 
|---|
| 697 |  | 
|---|
| 698 | memcpy(&orig_map, map, sizeof(*map)); | 
|---|
| 699 | #endif | 
|---|
| 700 |  | 
|---|
| 701 | map->m_flags = 0; | 
|---|
| 702 | ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n", | 
|---|
| 703 | flags, map->m_len, (unsigned long) map->m_lblk); | 
|---|
| 704 |  | 
|---|
| 705 | /* | 
|---|
| 706 | * ext4_map_blocks returns an int, and m_len is an unsigned int | 
|---|
| 707 | */ | 
|---|
| 708 | if (unlikely(map->m_len > INT_MAX)) | 
|---|
| 709 | map->m_len = INT_MAX; | 
|---|
| 710 |  | 
|---|
| 711 | /* We can handle the block number less than EXT_MAX_BLOCKS */ | 
|---|
| 712 | if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) | 
|---|
| 713 | return -EFSCORRUPTED; | 
|---|
| 714 |  | 
|---|
| 715 | /* | 
|---|
| 716 | * Callers from the context of data submission are the only exceptions | 
|---|
| 717 | * for regular files that do not hold the i_rwsem or invalidate_lock. | 
|---|
| 718 | * However, caching unrelated ranges is not permitted. | 
|---|
| 719 | */ | 
|---|
| 720 | if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) | 
|---|
| 721 | WARN_ON_ONCE(!(flags & EXT4_EX_NOCACHE)); | 
|---|
| 722 | else | 
|---|
| 723 | ext4_check_map_extents_env(inode); | 
|---|
| 724 |  | 
|---|
| 725 | /* Lookup extent status tree firstly */ | 
|---|
| 726 | if (ext4_es_lookup_extent(inode, lblk: map->m_lblk, NULL, es: &es)) { | 
|---|
| 727 | if (ext4_es_is_written(es: &es) || ext4_es_is_unwritten(es: &es)) { | 
|---|
| 728 | map->m_pblk = ext4_es_pblock(es: &es) + | 
|---|
| 729 | map->m_lblk - es.es_lblk; | 
|---|
| 730 | map->m_flags |= ext4_es_is_written(es: &es) ? | 
|---|
| 731 | EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; | 
|---|
| 732 | retval = es.es_len - (map->m_lblk - es.es_lblk); | 
|---|
| 733 | if (retval > map->m_len) | 
|---|
| 734 | retval = map->m_len; | 
|---|
| 735 | map->m_len = retval; | 
|---|
| 736 | } else if (ext4_es_is_delayed(es: &es) || ext4_es_is_hole(es: &es)) { | 
|---|
| 737 | map->m_pblk = 0; | 
|---|
| 738 | map->m_flags |= ext4_es_is_delayed(es: &es) ? | 
|---|
| 739 | EXT4_MAP_DELAYED : 0; | 
|---|
| 740 | retval = es.es_len - (map->m_lblk - es.es_lblk); | 
|---|
| 741 | if (retval > map->m_len) | 
|---|
| 742 | retval = map->m_len; | 
|---|
| 743 | map->m_len = retval; | 
|---|
| 744 | retval = 0; | 
|---|
| 745 | } else { | 
|---|
| 746 | BUG(); | 
|---|
| 747 | } | 
|---|
| 748 |  | 
|---|
| 749 | if (flags & EXT4_GET_BLOCKS_CACHED_NOWAIT) | 
|---|
| 750 | return retval; | 
|---|
| 751 | #ifdef ES_AGGRESSIVE_TEST | 
|---|
| 752 | ext4_map_blocks_es_recheck(handle, inode, map, | 
|---|
| 753 | &orig_map, flags); | 
|---|
| 754 | #endif | 
|---|
| 755 | if (!(flags & EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF) || | 
|---|
| 756 | orig_mlen == map->m_len) | 
|---|
| 757 | goto found; | 
|---|
| 758 |  | 
|---|
| 759 | map->m_len = orig_mlen; | 
|---|
| 760 | } | 
|---|
| 761 | /* | 
|---|
| 762 | * In the query cache no-wait mode, nothing we can do more if we | 
|---|
| 763 | * cannot find extent in the cache. | 
|---|
| 764 | */ | 
|---|
| 765 | if (flags & EXT4_GET_BLOCKS_CACHED_NOWAIT) | 
|---|
| 766 | return 0; | 
|---|
| 767 |  | 
|---|
| 768 | /* | 
|---|
| 769 | * Try to see if we can get the block without requesting a new | 
|---|
| 770 | * file system block. | 
|---|
| 771 | */ | 
|---|
| 772 | down_read(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 773 | retval = ext4_map_query_blocks(handle, inode, map, flags); | 
|---|
| 774 | up_read(sem: (&EXT4_I(inode)->i_data_sem)); | 
|---|
| 775 |  | 
|---|
| 776 | found: | 
|---|
| 777 | if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { | 
|---|
| 778 | ret = check_block_validity(inode, map); | 
|---|
| 779 | if (ret != 0) | 
|---|
| 780 | return ret; | 
|---|
| 781 | } | 
|---|
| 782 |  | 
|---|
| 783 | /* If it is only a block(s) look up */ | 
|---|
| 784 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) | 
|---|
| 785 | return retval; | 
|---|
| 786 |  | 
|---|
| 787 | /* | 
|---|
| 788 | * Returns if the blocks have already allocated | 
|---|
| 789 | * | 
|---|
| 790 | * Note that if blocks have been preallocated | 
|---|
| 791 | * ext4_ext_map_blocks() returns with buffer head unmapped | 
|---|
| 792 | */ | 
|---|
| 793 | if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) | 
|---|
| 794 | /* | 
|---|
| 795 | * If we need to convert extent to unwritten | 
|---|
| 796 | * we continue and do the actual work in | 
|---|
| 797 | * ext4_ext_map_blocks() | 
|---|
| 798 | */ | 
|---|
| 799 | if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) | 
|---|
| 800 | return retval; | 
|---|
| 801 |  | 
|---|
| 802 |  | 
|---|
| 803 | ext4_fc_track_inode(handle, inode); | 
|---|
| 804 | /* | 
|---|
| 805 | * New blocks allocate and/or writing to unwritten extent | 
|---|
| 806 | * will possibly result in updating i_data, so we take | 
|---|
| 807 | * the write lock of i_data_sem, and call get_block() | 
|---|
| 808 | * with create == 1 flag. | 
|---|
| 809 | */ | 
|---|
| 810 | down_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 811 | retval = ext4_map_create_blocks(handle, inode, map, flags); | 
|---|
| 812 | up_write(sem: (&EXT4_I(inode)->i_data_sem)); | 
|---|
| 813 | if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { | 
|---|
| 814 | ret = check_block_validity(inode, map); | 
|---|
| 815 | if (ret != 0) | 
|---|
| 816 | return ret; | 
|---|
| 817 |  | 
|---|
| 818 | /* | 
|---|
| 819 | * Inodes with freshly allocated blocks where contents will be | 
|---|
| 820 | * visible after transaction commit must be on transaction's | 
|---|
| 821 | * ordered data list. | 
|---|
| 822 | */ | 
|---|
| 823 | if (map->m_flags & EXT4_MAP_NEW && | 
|---|
| 824 | !(map->m_flags & EXT4_MAP_UNWRITTEN) && | 
|---|
| 825 | !(flags & EXT4_GET_BLOCKS_ZERO) && | 
|---|
| 826 | !ext4_is_quota_file(inode) && | 
|---|
| 827 | ext4_should_order_data(inode)) { | 
|---|
| 828 | loff_t start_byte = | 
|---|
| 829 | (loff_t)map->m_lblk << inode->i_blkbits; | 
|---|
| 830 | loff_t length = (loff_t)map->m_len << inode->i_blkbits; | 
|---|
| 831 |  | 
|---|
| 832 | if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) | 
|---|
| 833 | ret = ext4_jbd2_inode_add_wait(handle, inode, | 
|---|
| 834 | start_byte, length); | 
|---|
| 835 | else | 
|---|
| 836 | ret = ext4_jbd2_inode_add_write(handle, inode, | 
|---|
| 837 | start_byte, length); | 
|---|
| 838 | if (ret) | 
|---|
| 839 | return ret; | 
|---|
| 840 | } | 
|---|
| 841 | } | 
|---|
| 842 | if (retval > 0 && (map->m_flags & EXT4_MAP_UNWRITTEN || | 
|---|
| 843 | map->m_flags & EXT4_MAP_MAPPED)) | 
|---|
| 844 | ext4_fc_track_range(handle, inode, start: map->m_lblk, | 
|---|
| 845 | end: map->m_lblk + map->m_len - 1); | 
|---|
| 846 | if (retval < 0) | 
|---|
| 847 | ext_debug(inode, "failed with err %d\n", retval); | 
|---|
| 848 | return retval; | 
|---|
| 849 | } | 
|---|
| 850 |  | 
|---|
| 851 | /* | 
|---|
| 852 | * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages | 
|---|
| 853 | * we have to be careful as someone else may be manipulating b_state as well. | 
|---|
| 854 | */ | 
|---|
| 855 | static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) | 
|---|
| 856 | { | 
|---|
| 857 | unsigned long old_state; | 
|---|
| 858 | unsigned long new_state; | 
|---|
| 859 |  | 
|---|
| 860 | flags &= EXT4_MAP_FLAGS; | 
|---|
| 861 |  | 
|---|
| 862 | /* Dummy buffer_head? Set non-atomically. */ | 
|---|
| 863 | if (!bh->b_folio) { | 
|---|
| 864 | bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; | 
|---|
| 865 | return; | 
|---|
| 866 | } | 
|---|
| 867 | /* | 
|---|
| 868 | * Someone else may be modifying b_state. Be careful! This is ugly but | 
|---|
| 869 | * once we get rid of using bh as a container for mapping information | 
|---|
| 870 | * to pass to / from get_block functions, this can go away. | 
|---|
| 871 | */ | 
|---|
| 872 | old_state = READ_ONCE(bh->b_state); | 
|---|
| 873 | do { | 
|---|
| 874 | new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; | 
|---|
| 875 | } while (unlikely(!try_cmpxchg(&bh->b_state, &old_state, new_state))); | 
|---|
| 876 | } | 
|---|
| 877 |  | 
|---|
| 878 | /* | 
|---|
| 879 | * Make sure that the current journal transaction has enough credits to map | 
|---|
| 880 | * one extent. Return -EAGAIN if it cannot extend the current running | 
|---|
| 881 | * transaction. | 
|---|
| 882 | */ | 
|---|
| 883 | static inline int ext4_journal_ensure_extent_credits(handle_t *handle, | 
|---|
| 884 | struct inode *inode) | 
|---|
| 885 | { | 
|---|
| 886 | int credits; | 
|---|
| 887 | int ret; | 
|---|
| 888 |  | 
|---|
| 889 | /* Called from ext4_da_write_begin() which has no handle started? */ | 
|---|
| 890 | if (!handle) | 
|---|
| 891 | return 0; | 
|---|
| 892 |  | 
|---|
| 893 | credits = ext4_chunk_trans_blocks(inode, nrblocks: 1); | 
|---|
| 894 | ret = __ext4_journal_ensure_credits(handle, check_cred: credits, extend_cred: credits, revoke_cred: 0); | 
|---|
| 895 | return ret <= 0 ? ret : -EAGAIN; | 
|---|
| 896 | } | 
|---|
| 897 |  | 
|---|
| 898 | static int _ext4_get_block(struct inode *inode, sector_t iblock, | 
|---|
| 899 | struct buffer_head *bh, int flags) | 
|---|
| 900 | { | 
|---|
| 901 | struct ext4_map_blocks map; | 
|---|
| 902 | int ret = 0; | 
|---|
| 903 |  | 
|---|
| 904 | if (ext4_has_inline_data(inode)) | 
|---|
| 905 | return -ERANGE; | 
|---|
| 906 |  | 
|---|
| 907 | map.m_lblk = iblock; | 
|---|
| 908 | map.m_len = bh->b_size >> inode->i_blkbits; | 
|---|
| 909 |  | 
|---|
| 910 | ret = ext4_map_blocks(handle: ext4_journal_current_handle(), inode, map: &map, | 
|---|
| 911 | flags); | 
|---|
| 912 | if (ret > 0) { | 
|---|
| 913 | map_bh(bh, sb: inode->i_sb, block: map.m_pblk); | 
|---|
| 914 | ext4_update_bh_state(bh, flags: map.m_flags); | 
|---|
| 915 | bh->b_size = inode->i_sb->s_blocksize * map.m_len; | 
|---|
| 916 | ret = 0; | 
|---|
| 917 | } else if (ret == 0) { | 
|---|
| 918 | /* hole case, need to fill in bh->b_size */ | 
|---|
| 919 | bh->b_size = inode->i_sb->s_blocksize * map.m_len; | 
|---|
| 920 | } | 
|---|
| 921 | return ret; | 
|---|
| 922 | } | 
|---|
| 923 |  | 
|---|
| 924 | int ext4_get_block(struct inode *inode, sector_t iblock, | 
|---|
| 925 | struct buffer_head *bh, int create) | 
|---|
| 926 | { | 
|---|
| 927 | return _ext4_get_block(inode, iblock, bh, | 
|---|
| 928 | flags: create ? EXT4_GET_BLOCKS_CREATE : 0); | 
|---|
| 929 | } | 
|---|
| 930 |  | 
|---|
| 931 | /* | 
|---|
| 932 | * Get block function used when preparing for buffered write if we require | 
|---|
| 933 | * creating an unwritten extent if blocks haven't been allocated.  The extent | 
|---|
| 934 | * will be converted to written after the IO is complete. | 
|---|
| 935 | */ | 
|---|
| 936 | int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, | 
|---|
| 937 | struct buffer_head *bh_result, int create) | 
|---|
| 938 | { | 
|---|
| 939 | int ret = 0; | 
|---|
| 940 |  | 
|---|
| 941 | ext4_debug( "ext4_get_block_unwritten: inode %lu, create flag %d\n", | 
|---|
| 942 | inode->i_ino, create); | 
|---|
| 943 | ret = _ext4_get_block(inode, iblock, bh: bh_result, | 
|---|
| 944 | EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT); | 
|---|
| 945 |  | 
|---|
| 946 | /* | 
|---|
| 947 | * If the buffer is marked unwritten, mark it as new to make sure it is | 
|---|
| 948 | * zeroed out correctly in case of partial writes. Otherwise, there is | 
|---|
| 949 | * a chance of stale data getting exposed. | 
|---|
| 950 | */ | 
|---|
| 951 | if (ret == 0 && buffer_unwritten(bh: bh_result)) | 
|---|
| 952 | set_buffer_new(bh_result); | 
|---|
| 953 |  | 
|---|
| 954 | return ret; | 
|---|
| 955 | } | 
|---|
| 956 |  | 
|---|
| 957 | /* Maximum number of blocks we map for direct IO at once. */ | 
|---|
| 958 | #define DIO_MAX_BLOCKS 4096 | 
|---|
| 959 |  | 
|---|
| 960 | /* | 
|---|
| 961 | * `handle' can be NULL if create is zero | 
|---|
| 962 | */ | 
|---|
| 963 | struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, | 
|---|
| 964 | ext4_lblk_t block, int map_flags) | 
|---|
| 965 | { | 
|---|
| 966 | struct ext4_map_blocks map; | 
|---|
| 967 | struct buffer_head *bh; | 
|---|
| 968 | int create = map_flags & EXT4_GET_BLOCKS_CREATE; | 
|---|
| 969 | bool nowait = map_flags & EXT4_GET_BLOCKS_CACHED_NOWAIT; | 
|---|
| 970 | int err; | 
|---|
| 971 |  | 
|---|
| 972 | ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) | 
|---|
| 973 | || handle != NULL || create == 0); | 
|---|
| 974 | ASSERT(create == 0 || !nowait); | 
|---|
| 975 |  | 
|---|
| 976 | map.m_lblk = block; | 
|---|
| 977 | map.m_len = 1; | 
|---|
| 978 | err = ext4_map_blocks(handle, inode, map: &map, flags: map_flags); | 
|---|
| 979 |  | 
|---|
| 980 | if (err == 0) | 
|---|
| 981 | return create ? ERR_PTR(error: -ENOSPC) : NULL; | 
|---|
| 982 | if (err < 0) | 
|---|
| 983 | return ERR_PTR(error: err); | 
|---|
| 984 |  | 
|---|
| 985 | if (nowait) | 
|---|
| 986 | return sb_find_get_block(sb: inode->i_sb, block: map.m_pblk); | 
|---|
| 987 |  | 
|---|
| 988 | /* | 
|---|
| 989 | * Since bh could introduce extra ref count such as referred by | 
|---|
| 990 | * journal_head etc. Try to avoid using __GFP_MOVABLE here | 
|---|
| 991 | * as it may fail the migration when journal_head remains. | 
|---|
| 992 | */ | 
|---|
| 993 | bh = getblk_unmovable(bdev: inode->i_sb->s_bdev, block: map.m_pblk, | 
|---|
| 994 | size: inode->i_sb->s_blocksize); | 
|---|
| 995 |  | 
|---|
| 996 | if (unlikely(!bh)) | 
|---|
| 997 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 998 | if (map.m_flags & EXT4_MAP_NEW) { | 
|---|
| 999 | ASSERT(create != 0); | 
|---|
| 1000 | ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) | 
|---|
| 1001 | || (handle != NULL)); | 
|---|
| 1002 |  | 
|---|
| 1003 | /* | 
|---|
| 1004 | * Now that we do not always journal data, we should | 
|---|
| 1005 | * keep in mind whether this should always journal the | 
|---|
| 1006 | * new buffer as metadata.  For now, regular file | 
|---|
| 1007 | * writes use ext4_get_block instead, so it's not a | 
|---|
| 1008 | * problem. | 
|---|
| 1009 | */ | 
|---|
| 1010 | lock_buffer(bh); | 
|---|
| 1011 | BUFFER_TRACE(bh, "call get_create_access"); | 
|---|
| 1012 | err = ext4_journal_get_create_access(handle, inode->i_sb, bh, | 
|---|
| 1013 | EXT4_JTR_NONE); | 
|---|
| 1014 | if (unlikely(err)) { | 
|---|
| 1015 | unlock_buffer(bh); | 
|---|
| 1016 | goto errout; | 
|---|
| 1017 | } | 
|---|
| 1018 | if (!buffer_uptodate(bh)) { | 
|---|
| 1019 | memset(s: bh->b_data, c: 0, n: inode->i_sb->s_blocksize); | 
|---|
| 1020 | set_buffer_uptodate(bh); | 
|---|
| 1021 | } | 
|---|
| 1022 | unlock_buffer(bh); | 
|---|
| 1023 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); | 
|---|
| 1024 | err = ext4_handle_dirty_metadata(handle, inode, bh); | 
|---|
| 1025 | if (unlikely(err)) | 
|---|
| 1026 | goto errout; | 
|---|
| 1027 | } else | 
|---|
| 1028 | BUFFER_TRACE(bh, "not a new buffer"); | 
|---|
| 1029 | return bh; | 
|---|
| 1030 | errout: | 
|---|
| 1031 | brelse(bh); | 
|---|
| 1032 | return ERR_PTR(error: err); | 
|---|
| 1033 | } | 
|---|
| 1034 |  | 
|---|
| 1035 | struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, | 
|---|
| 1036 | ext4_lblk_t block, int map_flags) | 
|---|
| 1037 | { | 
|---|
| 1038 | struct buffer_head *bh; | 
|---|
| 1039 | int ret; | 
|---|
| 1040 |  | 
|---|
| 1041 | bh = ext4_getblk(handle, inode, block, map_flags); | 
|---|
| 1042 | if (IS_ERR(ptr: bh)) | 
|---|
| 1043 | return bh; | 
|---|
| 1044 | if (!bh || ext4_buffer_uptodate(bh)) | 
|---|
| 1045 | return bh; | 
|---|
| 1046 |  | 
|---|
| 1047 | ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, wait: true); | 
|---|
| 1048 | if (ret) { | 
|---|
| 1049 | put_bh(bh); | 
|---|
| 1050 | return ERR_PTR(error: ret); | 
|---|
| 1051 | } | 
|---|
| 1052 | return bh; | 
|---|
| 1053 | } | 
|---|
| 1054 |  | 
|---|
| 1055 | /* Read a contiguous batch of blocks. */ | 
|---|
| 1056 | int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, | 
|---|
| 1057 | bool wait, struct buffer_head **bhs) | 
|---|
| 1058 | { | 
|---|
| 1059 | int i, err; | 
|---|
| 1060 |  | 
|---|
| 1061 | for (i = 0; i < bh_count; i++) { | 
|---|
| 1062 | bhs[i] = ext4_getblk(NULL, inode, block: block + i, map_flags: 0 /* map_flags */); | 
|---|
| 1063 | if (IS_ERR(ptr: bhs[i])) { | 
|---|
| 1064 | err = PTR_ERR(ptr: bhs[i]); | 
|---|
| 1065 | bh_count = i; | 
|---|
| 1066 | goto out_brelse; | 
|---|
| 1067 | } | 
|---|
| 1068 | } | 
|---|
| 1069 |  | 
|---|
| 1070 | for (i = 0; i < bh_count; i++) | 
|---|
| 1071 | /* Note that NULL bhs[i] is valid because of holes. */ | 
|---|
| 1072 | if (bhs[i] && !ext4_buffer_uptodate(bh: bhs[i])) | 
|---|
| 1073 | ext4_read_bh_lock(bh: bhs[i], REQ_META | REQ_PRIO, wait: false); | 
|---|
| 1074 |  | 
|---|
| 1075 | if (!wait) | 
|---|
| 1076 | return 0; | 
|---|
| 1077 |  | 
|---|
| 1078 | for (i = 0; i < bh_count; i++) | 
|---|
| 1079 | if (bhs[i]) | 
|---|
| 1080 | wait_on_buffer(bh: bhs[i]); | 
|---|
| 1081 |  | 
|---|
| 1082 | for (i = 0; i < bh_count; i++) { | 
|---|
| 1083 | if (bhs[i] && !buffer_uptodate(bh: bhs[i])) { | 
|---|
| 1084 | err = -EIO; | 
|---|
| 1085 | goto out_brelse; | 
|---|
| 1086 | } | 
|---|
| 1087 | } | 
|---|
| 1088 | return 0; | 
|---|
| 1089 |  | 
|---|
| 1090 | out_brelse: | 
|---|
| 1091 | for (i = 0; i < bh_count; i++) { | 
|---|
| 1092 | brelse(bh: bhs[i]); | 
|---|
| 1093 | bhs[i] = NULL; | 
|---|
| 1094 | } | 
|---|
| 1095 | return err; | 
|---|
| 1096 | } | 
|---|
| 1097 |  | 
|---|
| 1098 | int ext4_walk_page_buffers(handle_t *handle, struct inode *inode, | 
|---|
| 1099 | struct buffer_head *head, | 
|---|
| 1100 | unsigned from, | 
|---|
| 1101 | unsigned to, | 
|---|
| 1102 | int *partial, | 
|---|
| 1103 | int (*fn)(handle_t *handle, struct inode *inode, | 
|---|
| 1104 | struct buffer_head *bh)) | 
|---|
| 1105 | { | 
|---|
| 1106 | struct buffer_head *bh; | 
|---|
| 1107 | unsigned block_start, block_end; | 
|---|
| 1108 | unsigned blocksize = head->b_size; | 
|---|
| 1109 | int err, ret = 0; | 
|---|
| 1110 | struct buffer_head *next; | 
|---|
| 1111 |  | 
|---|
| 1112 | for (bh = head, block_start = 0; | 
|---|
| 1113 | ret == 0 && (bh != head || !block_start); | 
|---|
| 1114 | block_start = block_end, bh = next) { | 
|---|
| 1115 | next = bh->b_this_page; | 
|---|
| 1116 | block_end = block_start + blocksize; | 
|---|
| 1117 | if (block_end <= from || block_start >= to) { | 
|---|
| 1118 | if (partial && !buffer_uptodate(bh)) | 
|---|
| 1119 | *partial = 1; | 
|---|
| 1120 | continue; | 
|---|
| 1121 | } | 
|---|
| 1122 | err = (*fn)(handle, inode, bh); | 
|---|
| 1123 | if (!ret) | 
|---|
| 1124 | ret = err; | 
|---|
| 1125 | } | 
|---|
| 1126 | return ret; | 
|---|
| 1127 | } | 
|---|
| 1128 |  | 
|---|
| 1129 | /* | 
|---|
| 1130 | * Helper for handling dirtying of journalled data. We also mark the folio as | 
|---|
| 1131 | * dirty so that writeback code knows about this page (and inode) contains | 
|---|
| 1132 | * dirty data. ext4_writepages() then commits appropriate transaction to | 
|---|
| 1133 | * make data stable. | 
|---|
| 1134 | */ | 
|---|
| 1135 | static int ext4_dirty_journalled_data(handle_t *handle, struct buffer_head *bh) | 
|---|
| 1136 | { | 
|---|
| 1137 | struct folio *folio = bh->b_folio; | 
|---|
| 1138 | struct inode *inode = folio->mapping->host; | 
|---|
| 1139 |  | 
|---|
| 1140 | /* only regular files have a_ops */ | 
|---|
| 1141 | if (S_ISREG(inode->i_mode)) | 
|---|
| 1142 | folio_mark_dirty(folio); | 
|---|
| 1143 | return ext4_handle_dirty_metadata(handle, NULL, bh); | 
|---|
| 1144 | } | 
|---|
| 1145 |  | 
|---|
| 1146 | int do_journal_get_write_access(handle_t *handle, struct inode *inode, | 
|---|
| 1147 | struct buffer_head *bh) | 
|---|
| 1148 | { | 
|---|
| 1149 | if (!buffer_mapped(bh) || buffer_freed(bh)) | 
|---|
| 1150 | return 0; | 
|---|
| 1151 | BUFFER_TRACE(bh, "get write access"); | 
|---|
| 1152 | return ext4_journal_get_write_access(handle, inode->i_sb, bh, | 
|---|
| 1153 | EXT4_JTR_NONE); | 
|---|
| 1154 | } | 
|---|
| 1155 |  | 
|---|
| 1156 | int ext4_block_write_begin(handle_t *handle, struct folio *folio, | 
|---|
| 1157 | loff_t pos, unsigned len, | 
|---|
| 1158 | get_block_t *get_block) | 
|---|
| 1159 | { | 
|---|
| 1160 | unsigned int from = offset_in_folio(folio, pos); | 
|---|
| 1161 | unsigned to = from + len; | 
|---|
| 1162 | struct inode *inode = folio->mapping->host; | 
|---|
| 1163 | unsigned block_start, block_end; | 
|---|
| 1164 | sector_t block; | 
|---|
| 1165 | int err = 0; | 
|---|
| 1166 | unsigned blocksize = inode->i_sb->s_blocksize; | 
|---|
| 1167 | unsigned bbits; | 
|---|
| 1168 | struct buffer_head *bh, *head, *wait[2]; | 
|---|
| 1169 | int nr_wait = 0; | 
|---|
| 1170 | int i; | 
|---|
| 1171 | bool should_journal_data = ext4_should_journal_data(inode); | 
|---|
| 1172 |  | 
|---|
| 1173 | BUG_ON(!folio_test_locked(folio)); | 
|---|
| 1174 | BUG_ON(to > folio_size(folio)); | 
|---|
| 1175 | BUG_ON(from > to); | 
|---|
| 1176 |  | 
|---|
| 1177 | head = folio_buffers(folio); | 
|---|
| 1178 | if (!head) | 
|---|
| 1179 | head = create_empty_buffers(folio, blocksize, b_state: 0); | 
|---|
| 1180 | bbits = ilog2(blocksize); | 
|---|
| 1181 | block = (sector_t)folio->index << (PAGE_SHIFT - bbits); | 
|---|
| 1182 |  | 
|---|
| 1183 | for (bh = head, block_start = 0; bh != head || !block_start; | 
|---|
| 1184 | block++, block_start = block_end, bh = bh->b_this_page) { | 
|---|
| 1185 | block_end = block_start + blocksize; | 
|---|
| 1186 | if (block_end <= from || block_start >= to) { | 
|---|
| 1187 | if (folio_test_uptodate(folio)) { | 
|---|
| 1188 | set_buffer_uptodate(bh); | 
|---|
| 1189 | } | 
|---|
| 1190 | continue; | 
|---|
| 1191 | } | 
|---|
| 1192 | if (WARN_ON_ONCE(buffer_new(bh))) | 
|---|
| 1193 | clear_buffer_new(bh); | 
|---|
| 1194 | if (!buffer_mapped(bh)) { | 
|---|
| 1195 | WARN_ON(bh->b_size != blocksize); | 
|---|
| 1196 | err = ext4_journal_ensure_extent_credits(handle, inode); | 
|---|
| 1197 | if (!err) | 
|---|
| 1198 | err = get_block(inode, block, bh, 1); | 
|---|
| 1199 | if (err) | 
|---|
| 1200 | break; | 
|---|
| 1201 | if (buffer_new(bh)) { | 
|---|
| 1202 | /* | 
|---|
| 1203 | * We may be zeroing partial buffers or all new | 
|---|
| 1204 | * buffers in case of failure. Prepare JBD2 for | 
|---|
| 1205 | * that. | 
|---|
| 1206 | */ | 
|---|
| 1207 | if (should_journal_data) | 
|---|
| 1208 | do_journal_get_write_access(handle, | 
|---|
| 1209 | inode, bh); | 
|---|
| 1210 | if (folio_test_uptodate(folio)) { | 
|---|
| 1211 | /* | 
|---|
| 1212 | * Unlike __block_write_begin() we leave | 
|---|
| 1213 | * dirtying of new uptodate buffers to | 
|---|
| 1214 | * ->write_end() time or | 
|---|
| 1215 | * folio_zero_new_buffers(). | 
|---|
| 1216 | */ | 
|---|
| 1217 | set_buffer_uptodate(bh); | 
|---|
| 1218 | continue; | 
|---|
| 1219 | } | 
|---|
| 1220 | if (block_end > to || block_start < from) | 
|---|
| 1221 | folio_zero_segments(folio, start1: to, | 
|---|
| 1222 | xend1: block_end, | 
|---|
| 1223 | start2: block_start, xend2: from); | 
|---|
| 1224 | continue; | 
|---|
| 1225 | } | 
|---|
| 1226 | } | 
|---|
| 1227 | if (folio_test_uptodate(folio)) { | 
|---|
| 1228 | set_buffer_uptodate(bh); | 
|---|
| 1229 | continue; | 
|---|
| 1230 | } | 
|---|
| 1231 | if (!buffer_uptodate(bh) && !buffer_delay(bh) && | 
|---|
| 1232 | !buffer_unwritten(bh) && | 
|---|
| 1233 | (block_start < from || block_end > to)) { | 
|---|
| 1234 | ext4_read_bh_lock(bh, op_flags: 0, wait: false); | 
|---|
| 1235 | wait[nr_wait++] = bh; | 
|---|
| 1236 | } | 
|---|
| 1237 | } | 
|---|
| 1238 | /* | 
|---|
| 1239 | * If we issued read requests, let them complete. | 
|---|
| 1240 | */ | 
|---|
| 1241 | for (i = 0; i < nr_wait; i++) { | 
|---|
| 1242 | wait_on_buffer(bh: wait[i]); | 
|---|
| 1243 | if (!buffer_uptodate(bh: wait[i])) | 
|---|
| 1244 | err = -EIO; | 
|---|
| 1245 | } | 
|---|
| 1246 | if (unlikely(err)) { | 
|---|
| 1247 | if (should_journal_data) | 
|---|
| 1248 | ext4_journalled_zero_new_buffers(handle, inode, folio, | 
|---|
| 1249 | from, to); | 
|---|
| 1250 | else | 
|---|
| 1251 | folio_zero_new_buffers(folio, from, to); | 
|---|
| 1252 | } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) { | 
|---|
| 1253 | for (i = 0; i < nr_wait; i++) { | 
|---|
| 1254 | int err2; | 
|---|
| 1255 |  | 
|---|
| 1256 | err2 = fscrypt_decrypt_pagecache_blocks(folio, | 
|---|
| 1257 | len: blocksize, offs: bh_offset(bh: wait[i])); | 
|---|
| 1258 | if (err2) { | 
|---|
| 1259 | clear_buffer_uptodate(bh: wait[i]); | 
|---|
| 1260 | err = err2; | 
|---|
| 1261 | } | 
|---|
| 1262 | } | 
|---|
| 1263 | } | 
|---|
| 1264 |  | 
|---|
| 1265 | return err; | 
|---|
| 1266 | } | 
|---|
| 1267 |  | 
|---|
| 1268 | /* | 
|---|
| 1269 | * To preserve ordering, it is essential that the hole instantiation and | 
|---|
| 1270 | * the data write be encapsulated in a single transaction.  We cannot | 
|---|
| 1271 | * close off a transaction and start a new one between the ext4_get_block() | 
|---|
| 1272 | * and the ext4_write_end().  So doing the jbd2_journal_start at the start of | 
|---|
| 1273 | * ext4_write_begin() is the right place. | 
|---|
| 1274 | */ | 
|---|
| 1275 | static int ext4_write_begin(const struct kiocb *iocb, | 
|---|
| 1276 | struct address_space *mapping, | 
|---|
| 1277 | loff_t pos, unsigned len, | 
|---|
| 1278 | struct folio **foliop, void **fsdata) | 
|---|
| 1279 | { | 
|---|
| 1280 | struct inode *inode = mapping->host; | 
|---|
| 1281 | int ret, needed_blocks; | 
|---|
| 1282 | handle_t *handle; | 
|---|
| 1283 | int retries = 0; | 
|---|
| 1284 | struct folio *folio; | 
|---|
| 1285 | pgoff_t index; | 
|---|
| 1286 | unsigned from, to; | 
|---|
| 1287 |  | 
|---|
| 1288 | ret = ext4_emergency_state(sb: inode->i_sb); | 
|---|
| 1289 | if (unlikely(ret)) | 
|---|
| 1290 | return ret; | 
|---|
| 1291 |  | 
|---|
| 1292 | trace_ext4_write_begin(inode, pos, len); | 
|---|
| 1293 | /* | 
|---|
| 1294 | * Reserve one block more for addition to orphan list in case | 
|---|
| 1295 | * we allocate blocks but write fails for some reason | 
|---|
| 1296 | */ | 
|---|
| 1297 | needed_blocks = ext4_chunk_trans_extent(inode, | 
|---|
| 1298 | nrblocks: ext4_journal_blocks_per_folio(inode)) + 1; | 
|---|
| 1299 | index = pos >> PAGE_SHIFT; | 
|---|
| 1300 |  | 
|---|
| 1301 | if (ext4_test_inode_state(inode, bit: EXT4_STATE_MAY_INLINE_DATA)) { | 
|---|
| 1302 | ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, | 
|---|
| 1303 | foliop); | 
|---|
| 1304 | if (ret < 0) | 
|---|
| 1305 | return ret; | 
|---|
| 1306 | if (ret == 1) | 
|---|
| 1307 | return 0; | 
|---|
| 1308 | } | 
|---|
| 1309 |  | 
|---|
| 1310 | /* | 
|---|
| 1311 | * write_begin_get_folio() can take a long time if the | 
|---|
| 1312 | * system is thrashing due to memory pressure, or if the folio | 
|---|
| 1313 | * is being written back.  So grab it first before we start | 
|---|
| 1314 | * the transaction handle.  This also allows us to allocate | 
|---|
| 1315 | * the folio (if needed) without using GFP_NOFS. | 
|---|
| 1316 | */ | 
|---|
| 1317 | retry_grab: | 
|---|
| 1318 | folio = write_begin_get_folio(iocb, mapping, index, len); | 
|---|
| 1319 | if (IS_ERR(ptr: folio)) | 
|---|
| 1320 | return PTR_ERR(ptr: folio); | 
|---|
| 1321 |  | 
|---|
| 1322 | if (pos + len > folio_pos(folio) + folio_size(folio)) | 
|---|
| 1323 | len = folio_pos(folio) + folio_size(folio) - pos; | 
|---|
| 1324 |  | 
|---|
| 1325 | from = offset_in_folio(folio, pos); | 
|---|
| 1326 | to = from + len; | 
|---|
| 1327 |  | 
|---|
| 1328 | /* | 
|---|
| 1329 | * The same as page allocation, we prealloc buffer heads before | 
|---|
| 1330 | * starting the handle. | 
|---|
| 1331 | */ | 
|---|
| 1332 | if (!folio_buffers(folio)) | 
|---|
| 1333 | create_empty_buffers(folio, blocksize: inode->i_sb->s_blocksize, b_state: 0); | 
|---|
| 1334 |  | 
|---|
| 1335 | folio_unlock(folio); | 
|---|
| 1336 |  | 
|---|
| 1337 | retry_journal: | 
|---|
| 1338 | handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); | 
|---|
| 1339 | if (IS_ERR(ptr: handle)) { | 
|---|
| 1340 | folio_put(folio); | 
|---|
| 1341 | return PTR_ERR(ptr: handle); | 
|---|
| 1342 | } | 
|---|
| 1343 |  | 
|---|
| 1344 | folio_lock(folio); | 
|---|
| 1345 | if (folio->mapping != mapping) { | 
|---|
| 1346 | /* The folio got truncated from under us */ | 
|---|
| 1347 | folio_unlock(folio); | 
|---|
| 1348 | folio_put(folio); | 
|---|
| 1349 | ext4_journal_stop(handle); | 
|---|
| 1350 | goto retry_grab; | 
|---|
| 1351 | } | 
|---|
| 1352 | /* In case writeback began while the folio was unlocked */ | 
|---|
| 1353 | folio_wait_stable(folio); | 
|---|
| 1354 |  | 
|---|
| 1355 | if (ext4_should_dioread_nolock(inode)) | 
|---|
| 1356 | ret = ext4_block_write_begin(handle, folio, pos, len, | 
|---|
| 1357 | get_block: ext4_get_block_unwritten); | 
|---|
| 1358 | else | 
|---|
| 1359 | ret = ext4_block_write_begin(handle, folio, pos, len, | 
|---|
| 1360 | get_block: ext4_get_block); | 
|---|
| 1361 | if (!ret && ext4_should_journal_data(inode)) { | 
|---|
| 1362 | ret = ext4_walk_page_buffers(handle, inode, | 
|---|
| 1363 | folio_buffers(folio), from, to, | 
|---|
| 1364 | NULL, fn: do_journal_get_write_access); | 
|---|
| 1365 | } | 
|---|
| 1366 |  | 
|---|
| 1367 | if (ret) { | 
|---|
| 1368 | bool extended = (pos + len > inode->i_size) && | 
|---|
| 1369 | !ext4_verity_in_progress(inode); | 
|---|
| 1370 |  | 
|---|
| 1371 | folio_unlock(folio); | 
|---|
| 1372 | /* | 
|---|
| 1373 | * ext4_block_write_begin may have instantiated a few blocks | 
|---|
| 1374 | * outside i_size.  Trim these off again. Don't need | 
|---|
| 1375 | * i_size_read because we hold i_rwsem. | 
|---|
| 1376 | * | 
|---|
| 1377 | * Add inode to orphan list in case we crash before | 
|---|
| 1378 | * truncate finishes | 
|---|
| 1379 | */ | 
|---|
| 1380 | if (extended && ext4_can_truncate(inode)) | 
|---|
| 1381 | ext4_orphan_add(handle, inode); | 
|---|
| 1382 |  | 
|---|
| 1383 | ext4_journal_stop(handle); | 
|---|
| 1384 | if (extended) { | 
|---|
| 1385 | ext4_truncate_failed_write(inode); | 
|---|
| 1386 | /* | 
|---|
| 1387 | * If truncate failed early the inode might | 
|---|
| 1388 | * still be on the orphan list; we need to | 
|---|
| 1389 | * make sure the inode is removed from the | 
|---|
| 1390 | * orphan list in that case. | 
|---|
| 1391 | */ | 
|---|
| 1392 | if (inode->i_nlink) | 
|---|
| 1393 | ext4_orphan_del(NULL, inode); | 
|---|
| 1394 | } | 
|---|
| 1395 |  | 
|---|
| 1396 | if (ret == -EAGAIN || | 
|---|
| 1397 | (ret == -ENOSPC && | 
|---|
| 1398 | ext4_should_retry_alloc(sb: inode->i_sb, retries: &retries))) | 
|---|
| 1399 | goto retry_journal; | 
|---|
| 1400 | folio_put(folio); | 
|---|
| 1401 | return ret; | 
|---|
| 1402 | } | 
|---|
| 1403 | *foliop = folio; | 
|---|
| 1404 | return ret; | 
|---|
| 1405 | } | 
|---|
| 1406 |  | 
|---|
| 1407 | /* For write_end() in data=journal mode */ | 
|---|
| 1408 | static int write_end_fn(handle_t *handle, struct inode *inode, | 
|---|
| 1409 | struct buffer_head *bh) | 
|---|
| 1410 | { | 
|---|
| 1411 | int ret; | 
|---|
| 1412 | if (!buffer_mapped(bh) || buffer_freed(bh)) | 
|---|
| 1413 | return 0; | 
|---|
| 1414 | set_buffer_uptodate(bh); | 
|---|
| 1415 | ret = ext4_dirty_journalled_data(handle, bh); | 
|---|
| 1416 | clear_buffer_meta(bh); | 
|---|
| 1417 | clear_buffer_prio(bh); | 
|---|
| 1418 | clear_buffer_new(bh); | 
|---|
| 1419 | return ret; | 
|---|
| 1420 | } | 
|---|
| 1421 |  | 
|---|
| 1422 | /* | 
|---|
| 1423 | * We need to pick up the new inode size which generic_commit_write gave us | 
|---|
| 1424 | * `iocb` can be NULL - eg, when called from page_symlink(). | 
|---|
| 1425 | * | 
|---|
| 1426 | * ext4 never places buffers on inode->i_mapping->i_private_list.  metadata | 
|---|
| 1427 | * buffers are managed internally. | 
|---|
| 1428 | */ | 
|---|
| 1429 | static int ext4_write_end(const struct kiocb *iocb, | 
|---|
| 1430 | struct address_space *mapping, | 
|---|
| 1431 | loff_t pos, unsigned len, unsigned copied, | 
|---|
| 1432 | struct folio *folio, void *fsdata) | 
|---|
| 1433 | { | 
|---|
| 1434 | handle_t *handle = ext4_journal_current_handle(); | 
|---|
| 1435 | struct inode *inode = mapping->host; | 
|---|
| 1436 | loff_t old_size = inode->i_size; | 
|---|
| 1437 | int ret = 0, ret2; | 
|---|
| 1438 | int i_size_changed = 0; | 
|---|
| 1439 | bool verity = ext4_verity_in_progress(inode); | 
|---|
| 1440 |  | 
|---|
| 1441 | trace_ext4_write_end(inode, pos, len, copied); | 
|---|
| 1442 |  | 
|---|
| 1443 | if (ext4_has_inline_data(inode) && | 
|---|
| 1444 | ext4_test_inode_state(inode, bit: EXT4_STATE_MAY_INLINE_DATA)) | 
|---|
| 1445 | return ext4_write_inline_data_end(inode, pos, len, copied, | 
|---|
| 1446 | folio); | 
|---|
| 1447 |  | 
|---|
| 1448 | copied = block_write_end(pos, len, copied, folio); | 
|---|
| 1449 | /* | 
|---|
| 1450 | * it's important to update i_size while still holding folio lock: | 
|---|
| 1451 | * page writeout could otherwise come in and zero beyond i_size. | 
|---|
| 1452 | * | 
|---|
| 1453 | * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree | 
|---|
| 1454 | * blocks are being written past EOF, so skip the i_size update. | 
|---|
| 1455 | */ | 
|---|
| 1456 | if (!verity) | 
|---|
| 1457 | i_size_changed = ext4_update_inode_size(inode, newsize: pos + copied); | 
|---|
| 1458 | folio_unlock(folio); | 
|---|
| 1459 | folio_put(folio); | 
|---|
| 1460 |  | 
|---|
| 1461 | if (old_size < pos && !verity) { | 
|---|
| 1462 | pagecache_isize_extended(inode, from: old_size, to: pos); | 
|---|
| 1463 | ext4_zero_partial_blocks(handle, inode, lstart: old_size, lend: pos - old_size); | 
|---|
| 1464 | } | 
|---|
| 1465 | /* | 
|---|
| 1466 | * Don't mark the inode dirty under folio lock. First, it unnecessarily | 
|---|
| 1467 | * makes the holding time of folio lock longer. Second, it forces lock | 
|---|
| 1468 | * ordering of folio lock and transaction start for journaling | 
|---|
| 1469 | * filesystems. | 
|---|
| 1470 | */ | 
|---|
| 1471 | if (i_size_changed) | 
|---|
| 1472 | ret = ext4_mark_inode_dirty(handle, inode); | 
|---|
| 1473 |  | 
|---|
| 1474 | if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) | 
|---|
| 1475 | /* if we have allocated more blocks and copied | 
|---|
| 1476 | * less. We will have blocks allocated outside | 
|---|
| 1477 | * inode->i_size. So truncate them | 
|---|
| 1478 | */ | 
|---|
| 1479 | ext4_orphan_add(handle, inode); | 
|---|
| 1480 |  | 
|---|
| 1481 | ret2 = ext4_journal_stop(handle); | 
|---|
| 1482 | if (!ret) | 
|---|
| 1483 | ret = ret2; | 
|---|
| 1484 |  | 
|---|
| 1485 | if (pos + len > inode->i_size && !verity) { | 
|---|
| 1486 | ext4_truncate_failed_write(inode); | 
|---|
| 1487 | /* | 
|---|
| 1488 | * If truncate failed early the inode might still be | 
|---|
| 1489 | * on the orphan list; we need to make sure the inode | 
|---|
| 1490 | * is removed from the orphan list in that case. | 
|---|
| 1491 | */ | 
|---|
| 1492 | if (inode->i_nlink) | 
|---|
| 1493 | ext4_orphan_del(NULL, inode); | 
|---|
| 1494 | } | 
|---|
| 1495 |  | 
|---|
| 1496 | return ret ? ret : copied; | 
|---|
| 1497 | } | 
|---|
| 1498 |  | 
|---|
| 1499 | /* | 
|---|
| 1500 | * This is a private version of folio_zero_new_buffers() which doesn't | 
|---|
| 1501 | * set the buffer to be dirty, since in data=journalled mode we need | 
|---|
| 1502 | * to call ext4_dirty_journalled_data() instead. | 
|---|
| 1503 | */ | 
|---|
| 1504 | static void ext4_journalled_zero_new_buffers(handle_t *handle, | 
|---|
| 1505 | struct inode *inode, | 
|---|
| 1506 | struct folio *folio, | 
|---|
| 1507 | unsigned from, unsigned to) | 
|---|
| 1508 | { | 
|---|
| 1509 | unsigned int block_start = 0, block_end; | 
|---|
| 1510 | struct buffer_head *head, *bh; | 
|---|
| 1511 |  | 
|---|
| 1512 | bh = head = folio_buffers(folio); | 
|---|
| 1513 | do { | 
|---|
| 1514 | block_end = block_start + bh->b_size; | 
|---|
| 1515 | if (buffer_new(bh)) { | 
|---|
| 1516 | if (block_end > from && block_start < to) { | 
|---|
| 1517 | if (!folio_test_uptodate(folio)) { | 
|---|
| 1518 | unsigned start, size; | 
|---|
| 1519 |  | 
|---|
| 1520 | start = max(from, block_start); | 
|---|
| 1521 | size = min(to, block_end) - start; | 
|---|
| 1522 |  | 
|---|
| 1523 | folio_zero_range(folio, start, length: size); | 
|---|
| 1524 | } | 
|---|
| 1525 | clear_buffer_new(bh); | 
|---|
| 1526 | write_end_fn(handle, inode, bh); | 
|---|
| 1527 | } | 
|---|
| 1528 | } | 
|---|
| 1529 | block_start = block_end; | 
|---|
| 1530 | bh = bh->b_this_page; | 
|---|
| 1531 | } while (bh != head); | 
|---|
| 1532 | } | 
|---|
| 1533 |  | 
|---|
| 1534 | static int ext4_journalled_write_end(const struct kiocb *iocb, | 
|---|
| 1535 | struct address_space *mapping, | 
|---|
| 1536 | loff_t pos, unsigned len, unsigned copied, | 
|---|
| 1537 | struct folio *folio, void *fsdata) | 
|---|
| 1538 | { | 
|---|
| 1539 | handle_t *handle = ext4_journal_current_handle(); | 
|---|
| 1540 | struct inode *inode = mapping->host; | 
|---|
| 1541 | loff_t old_size = inode->i_size; | 
|---|
| 1542 | int ret = 0, ret2; | 
|---|
| 1543 | int partial = 0; | 
|---|
| 1544 | unsigned from, to; | 
|---|
| 1545 | int size_changed = 0; | 
|---|
| 1546 | bool verity = ext4_verity_in_progress(inode); | 
|---|
| 1547 |  | 
|---|
| 1548 | trace_ext4_journalled_write_end(inode, pos, len, copied); | 
|---|
| 1549 | from = pos & (PAGE_SIZE - 1); | 
|---|
| 1550 | to = from + len; | 
|---|
| 1551 |  | 
|---|
| 1552 | BUG_ON(!ext4_handle_valid(handle)); | 
|---|
| 1553 |  | 
|---|
| 1554 | if (ext4_has_inline_data(inode)) | 
|---|
| 1555 | return ext4_write_inline_data_end(inode, pos, len, copied, | 
|---|
| 1556 | folio); | 
|---|
| 1557 |  | 
|---|
| 1558 | if (unlikely(copied < len) && !folio_test_uptodate(folio)) { | 
|---|
| 1559 | copied = 0; | 
|---|
| 1560 | ext4_journalled_zero_new_buffers(handle, inode, folio, | 
|---|
| 1561 | from, to); | 
|---|
| 1562 | } else { | 
|---|
| 1563 | if (unlikely(copied < len)) | 
|---|
| 1564 | ext4_journalled_zero_new_buffers(handle, inode, folio, | 
|---|
| 1565 | from: from + copied, to); | 
|---|
| 1566 | ret = ext4_walk_page_buffers(handle, inode, | 
|---|
| 1567 | folio_buffers(folio), | 
|---|
| 1568 | from, to: from + copied, partial: &partial, | 
|---|
| 1569 | fn: write_end_fn); | 
|---|
| 1570 | if (!partial) | 
|---|
| 1571 | folio_mark_uptodate(folio); | 
|---|
| 1572 | } | 
|---|
| 1573 | if (!verity) | 
|---|
| 1574 | size_changed = ext4_update_inode_size(inode, newsize: pos + copied); | 
|---|
| 1575 | EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; | 
|---|
| 1576 | folio_unlock(folio); | 
|---|
| 1577 | folio_put(folio); | 
|---|
| 1578 |  | 
|---|
| 1579 | if (old_size < pos && !verity) { | 
|---|
| 1580 | pagecache_isize_extended(inode, from: old_size, to: pos); | 
|---|
| 1581 | ext4_zero_partial_blocks(handle, inode, lstart: old_size, lend: pos - old_size); | 
|---|
| 1582 | } | 
|---|
| 1583 |  | 
|---|
| 1584 | if (size_changed) { | 
|---|
| 1585 | ret2 = ext4_mark_inode_dirty(handle, inode); | 
|---|
| 1586 | if (!ret) | 
|---|
| 1587 | ret = ret2; | 
|---|
| 1588 | } | 
|---|
| 1589 |  | 
|---|
| 1590 | if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) | 
|---|
| 1591 | /* if we have allocated more blocks and copied | 
|---|
| 1592 | * less. We will have blocks allocated outside | 
|---|
| 1593 | * inode->i_size. So truncate them | 
|---|
| 1594 | */ | 
|---|
| 1595 | ext4_orphan_add(handle, inode); | 
|---|
| 1596 |  | 
|---|
| 1597 | ret2 = ext4_journal_stop(handle); | 
|---|
| 1598 | if (!ret) | 
|---|
| 1599 | ret = ret2; | 
|---|
| 1600 | if (pos + len > inode->i_size && !verity) { | 
|---|
| 1601 | ext4_truncate_failed_write(inode); | 
|---|
| 1602 | /* | 
|---|
| 1603 | * If truncate failed early the inode might still be | 
|---|
| 1604 | * on the orphan list; we need to make sure the inode | 
|---|
| 1605 | * is removed from the orphan list in that case. | 
|---|
| 1606 | */ | 
|---|
| 1607 | if (inode->i_nlink) | 
|---|
| 1608 | ext4_orphan_del(NULL, inode); | 
|---|
| 1609 | } | 
|---|
| 1610 |  | 
|---|
| 1611 | return ret ? ret : copied; | 
|---|
| 1612 | } | 
|---|
| 1613 |  | 
|---|
| 1614 | /* | 
|---|
| 1615 | * Reserve space for 'nr_resv' clusters | 
|---|
| 1616 | */ | 
|---|
| 1617 | static int ext4_da_reserve_space(struct inode *inode, int nr_resv) | 
|---|
| 1618 | { | 
|---|
| 1619 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 1620 | struct ext4_inode_info *ei = EXT4_I(inode); | 
|---|
| 1621 | int ret; | 
|---|
| 1622 |  | 
|---|
| 1623 | /* | 
|---|
| 1624 | * We will charge metadata quota at writeout time; this saves | 
|---|
| 1625 | * us from metadata over-estimation, though we may go over by | 
|---|
| 1626 | * a small amount in the end.  Here we just reserve for data. | 
|---|
| 1627 | */ | 
|---|
| 1628 | ret = dquot_reserve_block(inode, EXT4_C2B(sbi, nr_resv)); | 
|---|
| 1629 | if (ret) | 
|---|
| 1630 | return ret; | 
|---|
| 1631 |  | 
|---|
| 1632 | spin_lock(lock: &ei->i_block_reservation_lock); | 
|---|
| 1633 | if (ext4_claim_free_clusters(sbi, nclusters: nr_resv, flags: 0)) { | 
|---|
| 1634 | spin_unlock(lock: &ei->i_block_reservation_lock); | 
|---|
| 1635 | dquot_release_reservation_block(inode, EXT4_C2B(sbi, nr_resv)); | 
|---|
| 1636 | return -ENOSPC; | 
|---|
| 1637 | } | 
|---|
| 1638 | ei->i_reserved_data_blocks += nr_resv; | 
|---|
| 1639 | trace_ext4_da_reserve_space(inode, nr_resv); | 
|---|
| 1640 | spin_unlock(lock: &ei->i_block_reservation_lock); | 
|---|
| 1641 |  | 
|---|
| 1642 | return 0;       /* success */ | 
|---|
| 1643 | } | 
|---|
| 1644 |  | 
|---|
| 1645 | void ext4_da_release_space(struct inode *inode, int to_free) | 
|---|
| 1646 | { | 
|---|
| 1647 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 1648 | struct ext4_inode_info *ei = EXT4_I(inode); | 
|---|
| 1649 |  | 
|---|
| 1650 | if (!to_free) | 
|---|
| 1651 | return;		/* Nothing to release, exit */ | 
|---|
| 1652 |  | 
|---|
| 1653 | spin_lock(lock: &EXT4_I(inode)->i_block_reservation_lock); | 
|---|
| 1654 |  | 
|---|
| 1655 | trace_ext4_da_release_space(inode, freed_blocks: to_free); | 
|---|
| 1656 | if (unlikely(to_free > ei->i_reserved_data_blocks)) { | 
|---|
| 1657 | /* | 
|---|
| 1658 | * if there aren't enough reserved blocks, then the | 
|---|
| 1659 | * counter is messed up somewhere.  Since this | 
|---|
| 1660 | * function is called from invalidate page, it's | 
|---|
| 1661 | * harmless to return without any action. | 
|---|
| 1662 | */ | 
|---|
| 1663 | ext4_warning(inode->i_sb, "ext4_da_release_space: " | 
|---|
| 1664 | "ino %lu, to_free %d with only %d reserved " | 
|---|
| 1665 | "data blocks", inode->i_ino, to_free, | 
|---|
| 1666 | ei->i_reserved_data_blocks); | 
|---|
| 1667 | WARN_ON(1); | 
|---|
| 1668 | to_free = ei->i_reserved_data_blocks; | 
|---|
| 1669 | } | 
|---|
| 1670 | ei->i_reserved_data_blocks -= to_free; | 
|---|
| 1671 |  | 
|---|
| 1672 | /* update fs dirty data blocks counter */ | 
|---|
| 1673 | percpu_counter_sub(fbc: &sbi->s_dirtyclusters_counter, amount: to_free); | 
|---|
| 1674 |  | 
|---|
| 1675 | spin_unlock(lock: &EXT4_I(inode)->i_block_reservation_lock); | 
|---|
| 1676 |  | 
|---|
| 1677 | dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); | 
|---|
| 1678 | } | 
|---|
| 1679 |  | 
|---|
| 1680 | /* | 
|---|
| 1681 | * Delayed allocation stuff | 
|---|
| 1682 | */ | 
|---|
| 1683 |  | 
|---|
| 1684 | struct mpage_da_data { | 
|---|
| 1685 | /* These are input fields for ext4_do_writepages() */ | 
|---|
| 1686 | struct inode *inode; | 
|---|
| 1687 | struct writeback_control *wbc; | 
|---|
| 1688 | unsigned int can_map:1;	/* Can writepages call map blocks? */ | 
|---|
| 1689 |  | 
|---|
| 1690 | /* These are internal state of ext4_do_writepages() */ | 
|---|
| 1691 | loff_t start_pos;	/* The start pos to write */ | 
|---|
| 1692 | loff_t next_pos;	/* Current pos to examine */ | 
|---|
| 1693 | loff_t end_pos;		/* Last pos to examine */ | 
|---|
| 1694 |  | 
|---|
| 1695 | /* | 
|---|
| 1696 | * Extent to map - this can be after start_pos because that can be | 
|---|
| 1697 | * fully mapped. We somewhat abuse m_flags to store whether the extent | 
|---|
| 1698 | * is delalloc or unwritten. | 
|---|
| 1699 | */ | 
|---|
| 1700 | struct ext4_map_blocks map; | 
|---|
| 1701 | struct ext4_io_submit io_submit;	/* IO submission data */ | 
|---|
| 1702 | unsigned int do_map:1; | 
|---|
| 1703 | unsigned int scanned_until_end:1; | 
|---|
| 1704 | unsigned int journalled_more_data:1; | 
|---|
| 1705 | }; | 
|---|
| 1706 |  | 
|---|
| 1707 | static void mpage_release_unused_pages(struct mpage_da_data *mpd, | 
|---|
| 1708 | bool invalidate) | 
|---|
| 1709 | { | 
|---|
| 1710 | unsigned nr, i; | 
|---|
| 1711 | pgoff_t index, end; | 
|---|
| 1712 | struct folio_batch fbatch; | 
|---|
| 1713 | struct inode *inode = mpd->inode; | 
|---|
| 1714 | struct address_space *mapping = inode->i_mapping; | 
|---|
| 1715 |  | 
|---|
| 1716 | /* This is necessary when next_pos == 0. */ | 
|---|
| 1717 | if (mpd->start_pos >= mpd->next_pos) | 
|---|
| 1718 | return; | 
|---|
| 1719 |  | 
|---|
| 1720 | mpd->scanned_until_end = 0; | 
|---|
| 1721 | if (invalidate) { | 
|---|
| 1722 | ext4_lblk_t start, last; | 
|---|
| 1723 | start = EXT4_B_TO_LBLK(inode, mpd->start_pos); | 
|---|
| 1724 | last = mpd->next_pos >> inode->i_blkbits; | 
|---|
| 1725 |  | 
|---|
| 1726 | /* | 
|---|
| 1727 | * avoid racing with extent status tree scans made by | 
|---|
| 1728 | * ext4_insert_delayed_block() | 
|---|
| 1729 | */ | 
|---|
| 1730 | down_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 1731 | ext4_es_remove_extent(inode, lblk: start, len: last - start); | 
|---|
| 1732 | up_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 1733 | } | 
|---|
| 1734 |  | 
|---|
| 1735 | folio_batch_init(fbatch: &fbatch); | 
|---|
| 1736 | index = mpd->start_pos >> PAGE_SHIFT; | 
|---|
| 1737 | end = mpd->next_pos >> PAGE_SHIFT; | 
|---|
| 1738 | while (index < end) { | 
|---|
| 1739 | nr = filemap_get_folios(mapping, start: &index, end: end - 1, fbatch: &fbatch); | 
|---|
| 1740 | if (nr == 0) | 
|---|
| 1741 | break; | 
|---|
| 1742 | for (i = 0; i < nr; i++) { | 
|---|
| 1743 | struct folio *folio = fbatch.folios[i]; | 
|---|
| 1744 |  | 
|---|
| 1745 | if (folio_pos(folio) < mpd->start_pos) | 
|---|
| 1746 | continue; | 
|---|
| 1747 | if (folio_next_index(folio) > end) | 
|---|
| 1748 | continue; | 
|---|
| 1749 | BUG_ON(!folio_test_locked(folio)); | 
|---|
| 1750 | BUG_ON(folio_test_writeback(folio)); | 
|---|
| 1751 | if (invalidate) { | 
|---|
| 1752 | if (folio_mapped(folio)) | 
|---|
| 1753 | folio_clear_dirty_for_io(folio); | 
|---|
| 1754 | block_invalidate_folio(folio, offset: 0, | 
|---|
| 1755 | length: folio_size(folio)); | 
|---|
| 1756 | folio_clear_uptodate(folio); | 
|---|
| 1757 | } | 
|---|
| 1758 | folio_unlock(folio); | 
|---|
| 1759 | } | 
|---|
| 1760 | folio_batch_release(fbatch: &fbatch); | 
|---|
| 1761 | } | 
|---|
| 1762 | } | 
|---|
| 1763 |  | 
|---|
| 1764 | static void ext4_print_free_blocks(struct inode *inode) | 
|---|
| 1765 | { | 
|---|
| 1766 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 1767 | struct super_block *sb = inode->i_sb; | 
|---|
| 1768 | struct ext4_inode_info *ei = EXT4_I(inode); | 
|---|
| 1769 |  | 
|---|
| 1770 | ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", | 
|---|
| 1771 | EXT4_C2B(EXT4_SB(inode->i_sb), | 
|---|
| 1772 | ext4_count_free_clusters(sb))); | 
|---|
| 1773 | ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); | 
|---|
| 1774 | ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", | 
|---|
| 1775 | (long long) EXT4_C2B(EXT4_SB(sb), | 
|---|
| 1776 | percpu_counter_sum(&sbi->s_freeclusters_counter))); | 
|---|
| 1777 | ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", | 
|---|
| 1778 | (long long) EXT4_C2B(EXT4_SB(sb), | 
|---|
| 1779 | percpu_counter_sum(&sbi->s_dirtyclusters_counter))); | 
|---|
| 1780 | ext4_msg(sb, KERN_CRIT, "Block reservation details"); | 
|---|
| 1781 | ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", | 
|---|
| 1782 | ei->i_reserved_data_blocks); | 
|---|
| 1783 | return; | 
|---|
| 1784 | } | 
|---|
| 1785 |  | 
|---|
| 1786 | /* | 
|---|
| 1787 | * Check whether the cluster containing lblk has been allocated or has | 
|---|
| 1788 | * delalloc reservation. | 
|---|
| 1789 | * | 
|---|
| 1790 | * Returns 0 if the cluster doesn't have either, 1 if it has delalloc | 
|---|
| 1791 | * reservation, 2 if it's already been allocated, negative error code on | 
|---|
| 1792 | * failure. | 
|---|
| 1793 | */ | 
|---|
| 1794 | static int ext4_clu_alloc_state(struct inode *inode, ext4_lblk_t lblk) | 
|---|
| 1795 | { | 
|---|
| 1796 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 1797 | int ret; | 
|---|
| 1798 |  | 
|---|
| 1799 | /* Has delalloc reservation? */ | 
|---|
| 1800 | if (ext4_es_scan_clu(inode, matching_fn: &ext4_es_is_delayed, lblk)) | 
|---|
| 1801 | return 1; | 
|---|
| 1802 |  | 
|---|
| 1803 | /* Already been allocated? */ | 
|---|
| 1804 | if (ext4_es_scan_clu(inode, matching_fn: &ext4_es_is_mapped, lblk)) | 
|---|
| 1805 | return 2; | 
|---|
| 1806 | ret = ext4_clu_mapped(inode, EXT4_B2C(sbi, lblk)); | 
|---|
| 1807 | if (ret < 0) | 
|---|
| 1808 | return ret; | 
|---|
| 1809 | if (ret > 0) | 
|---|
| 1810 | return 2; | 
|---|
| 1811 |  | 
|---|
| 1812 | return 0; | 
|---|
| 1813 | } | 
|---|
| 1814 |  | 
|---|
| 1815 | /* | 
|---|
| 1816 | * ext4_insert_delayed_blocks - adds a multiple delayed blocks to the extents | 
|---|
| 1817 | *                              status tree, incrementing the reserved | 
|---|
| 1818 | *                              cluster/block count or making pending | 
|---|
| 1819 | *                              reservations where needed | 
|---|
| 1820 | * | 
|---|
| 1821 | * @inode - file containing the newly added block | 
|---|
| 1822 | * @lblk - start logical block to be added | 
|---|
| 1823 | * @len - length of blocks to be added | 
|---|
| 1824 | * | 
|---|
| 1825 | * Returns 0 on success, negative error code on failure. | 
|---|
| 1826 | */ | 
|---|
| 1827 | static int ext4_insert_delayed_blocks(struct inode *inode, ext4_lblk_t lblk, | 
|---|
| 1828 | ext4_lblk_t len) | 
|---|
| 1829 | { | 
|---|
| 1830 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 1831 | int ret; | 
|---|
| 1832 | bool lclu_allocated = false; | 
|---|
| 1833 | bool end_allocated = false; | 
|---|
| 1834 | ext4_lblk_t resv_clu; | 
|---|
| 1835 | ext4_lblk_t end = lblk + len - 1; | 
|---|
| 1836 |  | 
|---|
| 1837 | /* | 
|---|
| 1838 | * If the cluster containing lblk or end is shared with a delayed, | 
|---|
| 1839 | * written, or unwritten extent in a bigalloc file system, it's | 
|---|
| 1840 | * already been accounted for and does not need to be reserved. | 
|---|
| 1841 | * A pending reservation must be made for the cluster if it's | 
|---|
| 1842 | * shared with a written or unwritten extent and doesn't already | 
|---|
| 1843 | * have one.  Written and unwritten extents can be purged from the | 
|---|
| 1844 | * extents status tree if the system is under memory pressure, so | 
|---|
| 1845 | * it's necessary to examine the extent tree if a search of the | 
|---|
| 1846 | * extents status tree doesn't get a match. | 
|---|
| 1847 | */ | 
|---|
| 1848 | if (sbi->s_cluster_ratio == 1) { | 
|---|
| 1849 | ret = ext4_da_reserve_space(inode, nr_resv: len); | 
|---|
| 1850 | if (ret != 0)   /* ENOSPC */ | 
|---|
| 1851 | return ret; | 
|---|
| 1852 | } else {   /* bigalloc */ | 
|---|
| 1853 | resv_clu = EXT4_B2C(sbi, end) - EXT4_B2C(sbi, lblk) + 1; | 
|---|
| 1854 |  | 
|---|
| 1855 | ret = ext4_clu_alloc_state(inode, lblk); | 
|---|
| 1856 | if (ret < 0) | 
|---|
| 1857 | return ret; | 
|---|
| 1858 | if (ret > 0) { | 
|---|
| 1859 | resv_clu--; | 
|---|
| 1860 | lclu_allocated = (ret == 2); | 
|---|
| 1861 | } | 
|---|
| 1862 |  | 
|---|
| 1863 | if (EXT4_B2C(sbi, lblk) != EXT4_B2C(sbi, end)) { | 
|---|
| 1864 | ret = ext4_clu_alloc_state(inode, lblk: end); | 
|---|
| 1865 | if (ret < 0) | 
|---|
| 1866 | return ret; | 
|---|
| 1867 | if (ret > 0) { | 
|---|
| 1868 | resv_clu--; | 
|---|
| 1869 | end_allocated = (ret == 2); | 
|---|
| 1870 | } | 
|---|
| 1871 | } | 
|---|
| 1872 |  | 
|---|
| 1873 | if (resv_clu) { | 
|---|
| 1874 | ret = ext4_da_reserve_space(inode, nr_resv: resv_clu); | 
|---|
| 1875 | if (ret != 0)   /* ENOSPC */ | 
|---|
| 1876 | return ret; | 
|---|
| 1877 | } | 
|---|
| 1878 | } | 
|---|
| 1879 |  | 
|---|
| 1880 | ext4_es_insert_delayed_extent(inode, lblk, len, lclu_allocated, | 
|---|
| 1881 | end_allocated); | 
|---|
| 1882 | return 0; | 
|---|
| 1883 | } | 
|---|
| 1884 |  | 
|---|
| 1885 | /* | 
|---|
| 1886 | * Looks up the requested blocks and sets the delalloc extent map. | 
|---|
| 1887 | * First try to look up for the extent entry that contains the requested | 
|---|
| 1888 | * blocks in the extent status tree without i_data_sem, then try to look | 
|---|
| 1889 | * up for the ondisk extent mapping with i_data_sem in read mode, | 
|---|
| 1890 | * finally hold i_data_sem in write mode, looks up again and add a | 
|---|
| 1891 | * delalloc extent entry if it still couldn't find any extent. Pass out | 
|---|
| 1892 | * the mapped extent through @map and return 0 on success. | 
|---|
| 1893 | */ | 
|---|
| 1894 | static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map) | 
|---|
| 1895 | { | 
|---|
| 1896 | struct extent_status es; | 
|---|
| 1897 | int retval; | 
|---|
| 1898 | #ifdef ES_AGGRESSIVE_TEST | 
|---|
| 1899 | struct ext4_map_blocks orig_map; | 
|---|
| 1900 |  | 
|---|
| 1901 | memcpy(&orig_map, map, sizeof(*map)); | 
|---|
| 1902 | #endif | 
|---|
| 1903 |  | 
|---|
| 1904 | map->m_flags = 0; | 
|---|
| 1905 | ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len, | 
|---|
| 1906 | (unsigned long) map->m_lblk); | 
|---|
| 1907 |  | 
|---|
| 1908 | ext4_check_map_extents_env(inode); | 
|---|
| 1909 |  | 
|---|
| 1910 | /* Lookup extent status tree firstly */ | 
|---|
| 1911 | if (ext4_es_lookup_extent(inode, lblk: map->m_lblk, NULL, es: &es)) { | 
|---|
| 1912 | map->m_len = min_t(unsigned int, map->m_len, | 
|---|
| 1913 | es.es_len - (map->m_lblk - es.es_lblk)); | 
|---|
| 1914 |  | 
|---|
| 1915 | if (ext4_es_is_hole(es: &es)) | 
|---|
| 1916 | goto add_delayed; | 
|---|
| 1917 |  | 
|---|
| 1918 | found: | 
|---|
| 1919 | /* | 
|---|
| 1920 | * Delayed extent could be allocated by fallocate. | 
|---|
| 1921 | * So we need to check it. | 
|---|
| 1922 | */ | 
|---|
| 1923 | if (ext4_es_is_delayed(es: &es)) { | 
|---|
| 1924 | map->m_flags |= EXT4_MAP_DELAYED; | 
|---|
| 1925 | return 0; | 
|---|
| 1926 | } | 
|---|
| 1927 |  | 
|---|
| 1928 | map->m_pblk = ext4_es_pblock(es: &es) + map->m_lblk - es.es_lblk; | 
|---|
| 1929 | if (ext4_es_is_written(es: &es)) | 
|---|
| 1930 | map->m_flags |= EXT4_MAP_MAPPED; | 
|---|
| 1931 | else if (ext4_es_is_unwritten(es: &es)) | 
|---|
| 1932 | map->m_flags |= EXT4_MAP_UNWRITTEN; | 
|---|
| 1933 | else | 
|---|
| 1934 | BUG(); | 
|---|
| 1935 |  | 
|---|
| 1936 | #ifdef ES_AGGRESSIVE_TEST | 
|---|
| 1937 | ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); | 
|---|
| 1938 | #endif | 
|---|
| 1939 | return 0; | 
|---|
| 1940 | } | 
|---|
| 1941 |  | 
|---|
| 1942 | /* | 
|---|
| 1943 | * Try to see if we can get the block without requesting a new | 
|---|
| 1944 | * file system block. | 
|---|
| 1945 | */ | 
|---|
| 1946 | down_read(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 1947 | if (ext4_has_inline_data(inode)) | 
|---|
| 1948 | retval = 0; | 
|---|
| 1949 | else | 
|---|
| 1950 | retval = ext4_map_query_blocks(NULL, inode, map, flags: 0); | 
|---|
| 1951 | up_read(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 1952 | if (retval) | 
|---|
| 1953 | return retval < 0 ? retval : 0; | 
|---|
| 1954 |  | 
|---|
| 1955 | add_delayed: | 
|---|
| 1956 | down_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 1957 | /* | 
|---|
| 1958 | * Page fault path (ext4_page_mkwrite does not take i_rwsem) | 
|---|
| 1959 | * and fallocate path (no folio lock) can race. Make sure we | 
|---|
| 1960 | * lookup the extent status tree here again while i_data_sem | 
|---|
| 1961 | * is held in write mode, before inserting a new da entry in | 
|---|
| 1962 | * the extent status tree. | 
|---|
| 1963 | */ | 
|---|
| 1964 | if (ext4_es_lookup_extent(inode, lblk: map->m_lblk, NULL, es: &es)) { | 
|---|
| 1965 | map->m_len = min_t(unsigned int, map->m_len, | 
|---|
| 1966 | es.es_len - (map->m_lblk - es.es_lblk)); | 
|---|
| 1967 |  | 
|---|
| 1968 | if (!ext4_es_is_hole(es: &es)) { | 
|---|
| 1969 | up_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 1970 | goto found; | 
|---|
| 1971 | } | 
|---|
| 1972 | } else if (!ext4_has_inline_data(inode)) { | 
|---|
| 1973 | retval = ext4_map_query_blocks(NULL, inode, map, flags: 0); | 
|---|
| 1974 | if (retval) { | 
|---|
| 1975 | up_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 1976 | return retval < 0 ? retval : 0; | 
|---|
| 1977 | } | 
|---|
| 1978 | } | 
|---|
| 1979 |  | 
|---|
| 1980 | map->m_flags |= EXT4_MAP_DELAYED; | 
|---|
| 1981 | retval = ext4_insert_delayed_blocks(inode, lblk: map->m_lblk, len: map->m_len); | 
|---|
| 1982 | up_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 1983 |  | 
|---|
| 1984 | return retval; | 
|---|
| 1985 | } | 
|---|
| 1986 |  | 
|---|
| 1987 | /* | 
|---|
| 1988 | * This is a special get_block_t callback which is used by | 
|---|
| 1989 | * ext4_da_write_begin().  It will either return mapped block or | 
|---|
| 1990 | * reserve space for a single block. | 
|---|
| 1991 | * | 
|---|
| 1992 | * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. | 
|---|
| 1993 | * We also have b_blocknr = -1 and b_bdev initialized properly | 
|---|
| 1994 | * | 
|---|
| 1995 | * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. | 
|---|
| 1996 | * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev | 
|---|
| 1997 | * initialized properly. | 
|---|
| 1998 | */ | 
|---|
| 1999 | int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, | 
|---|
| 2000 | struct buffer_head *bh, int create) | 
|---|
| 2001 | { | 
|---|
| 2002 | struct ext4_map_blocks map; | 
|---|
| 2003 | sector_t invalid_block = ~((sector_t) 0xffff); | 
|---|
| 2004 | int ret = 0; | 
|---|
| 2005 |  | 
|---|
| 2006 | BUG_ON(create == 0); | 
|---|
| 2007 | BUG_ON(bh->b_size != inode->i_sb->s_blocksize); | 
|---|
| 2008 |  | 
|---|
| 2009 | if (invalid_block < ext4_blocks_count(es: EXT4_SB(sb: inode->i_sb)->s_es)) | 
|---|
| 2010 | invalid_block = ~0; | 
|---|
| 2011 |  | 
|---|
| 2012 | map.m_lblk = iblock; | 
|---|
| 2013 | map.m_len = 1; | 
|---|
| 2014 |  | 
|---|
| 2015 | /* | 
|---|
| 2016 | * first, we need to know whether the block is allocated already | 
|---|
| 2017 | * preallocated blocks are unmapped but should treated | 
|---|
| 2018 | * the same as allocated blocks. | 
|---|
| 2019 | */ | 
|---|
| 2020 | ret = ext4_da_map_blocks(inode, map: &map); | 
|---|
| 2021 | if (ret < 0) | 
|---|
| 2022 | return ret; | 
|---|
| 2023 |  | 
|---|
| 2024 | if (map.m_flags & EXT4_MAP_DELAYED) { | 
|---|
| 2025 | map_bh(bh, sb: inode->i_sb, block: invalid_block); | 
|---|
| 2026 | set_buffer_new(bh); | 
|---|
| 2027 | set_buffer_delay(bh); | 
|---|
| 2028 | return 0; | 
|---|
| 2029 | } | 
|---|
| 2030 |  | 
|---|
| 2031 | map_bh(bh, sb: inode->i_sb, block: map.m_pblk); | 
|---|
| 2032 | ext4_update_bh_state(bh, flags: map.m_flags); | 
|---|
| 2033 |  | 
|---|
| 2034 | if (buffer_unwritten(bh)) { | 
|---|
| 2035 | /* A delayed write to unwritten bh should be marked | 
|---|
| 2036 | * new and mapped.  Mapped ensures that we don't do | 
|---|
| 2037 | * get_block multiple times when we write to the same | 
|---|
| 2038 | * offset and new ensures that we do proper zero out | 
|---|
| 2039 | * for partial write. | 
|---|
| 2040 | */ | 
|---|
| 2041 | set_buffer_new(bh); | 
|---|
| 2042 | set_buffer_mapped(bh); | 
|---|
| 2043 | } | 
|---|
| 2044 | return 0; | 
|---|
| 2045 | } | 
|---|
| 2046 |  | 
|---|
| 2047 | static void mpage_folio_done(struct mpage_da_data *mpd, struct folio *folio) | 
|---|
| 2048 | { | 
|---|
| 2049 | mpd->start_pos += folio_size(folio); | 
|---|
| 2050 | mpd->wbc->nr_to_write -= folio_nr_pages(folio); | 
|---|
| 2051 | folio_unlock(folio); | 
|---|
| 2052 | } | 
|---|
| 2053 |  | 
|---|
| 2054 | static int mpage_submit_folio(struct mpage_da_data *mpd, struct folio *folio) | 
|---|
| 2055 | { | 
|---|
| 2056 | size_t len; | 
|---|
| 2057 | loff_t size; | 
|---|
| 2058 | int err; | 
|---|
| 2059 |  | 
|---|
| 2060 | WARN_ON_ONCE(folio_pos(folio) != mpd->start_pos); | 
|---|
| 2061 | folio_clear_dirty_for_io(folio); | 
|---|
| 2062 | /* | 
|---|
| 2063 | * We have to be very careful here!  Nothing protects writeback path | 
|---|
| 2064 | * against i_size changes and the page can be writeably mapped into | 
|---|
| 2065 | * page tables. So an application can be growing i_size and writing | 
|---|
| 2066 | * data through mmap while writeback runs. folio_clear_dirty_for_io() | 
|---|
| 2067 | * write-protects our page in page tables and the page cannot get | 
|---|
| 2068 | * written to again until we release folio lock. So only after | 
|---|
| 2069 | * folio_clear_dirty_for_io() we are safe to sample i_size for | 
|---|
| 2070 | * ext4_bio_write_folio() to zero-out tail of the written page. We rely | 
|---|
| 2071 | * on the barrier provided by folio_test_clear_dirty() in | 
|---|
| 2072 | * folio_clear_dirty_for_io() to make sure i_size is really sampled only | 
|---|
| 2073 | * after page tables are updated. | 
|---|
| 2074 | */ | 
|---|
| 2075 | size = i_size_read(inode: mpd->inode); | 
|---|
| 2076 | len = folio_size(folio); | 
|---|
| 2077 | if (folio_pos(folio) + len > size && | 
|---|
| 2078 | !ext4_verity_in_progress(inode: mpd->inode)) | 
|---|
| 2079 | len = size & (len - 1); | 
|---|
| 2080 | err = ext4_bio_write_folio(io: &mpd->io_submit, page: folio, len); | 
|---|
| 2081 |  | 
|---|
| 2082 | return err; | 
|---|
| 2083 | } | 
|---|
| 2084 |  | 
|---|
| 2085 | #define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay)) | 
|---|
| 2086 |  | 
|---|
| 2087 | /* | 
|---|
| 2088 | * mballoc gives us at most this number of blocks... | 
|---|
| 2089 | * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). | 
|---|
| 2090 | * The rest of mballoc seems to handle chunks up to full group size. | 
|---|
| 2091 | */ | 
|---|
| 2092 | #define MAX_WRITEPAGES_EXTENT_LEN 2048 | 
|---|
| 2093 |  | 
|---|
| 2094 | /* | 
|---|
| 2095 | * mpage_add_bh_to_extent - try to add bh to extent of blocks to map | 
|---|
| 2096 | * | 
|---|
| 2097 | * @mpd - extent of blocks | 
|---|
| 2098 | * @lblk - logical number of the block in the file | 
|---|
| 2099 | * @bh - buffer head we want to add to the extent | 
|---|
| 2100 | * | 
|---|
| 2101 | * The function is used to collect contig. blocks in the same state. If the | 
|---|
| 2102 | * buffer doesn't require mapping for writeback and we haven't started the | 
|---|
| 2103 | * extent of buffers to map yet, the function returns 'true' immediately - the | 
|---|
| 2104 | * caller can write the buffer right away. Otherwise the function returns true | 
|---|
| 2105 | * if the block has been added to the extent, false if the block couldn't be | 
|---|
| 2106 | * added. | 
|---|
| 2107 | */ | 
|---|
| 2108 | static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, | 
|---|
| 2109 | struct buffer_head *bh) | 
|---|
| 2110 | { | 
|---|
| 2111 | struct ext4_map_blocks *map = &mpd->map; | 
|---|
| 2112 |  | 
|---|
| 2113 | /* Buffer that doesn't need mapping for writeback? */ | 
|---|
| 2114 | if (!buffer_dirty(bh) || !buffer_mapped(bh) || | 
|---|
| 2115 | (!buffer_delay(bh) && !buffer_unwritten(bh))) { | 
|---|
| 2116 | /* So far no extent to map => we write the buffer right away */ | 
|---|
| 2117 | if (map->m_len == 0) | 
|---|
| 2118 | return true; | 
|---|
| 2119 | return false; | 
|---|
| 2120 | } | 
|---|
| 2121 |  | 
|---|
| 2122 | /* First block in the extent? */ | 
|---|
| 2123 | if (map->m_len == 0) { | 
|---|
| 2124 | /* We cannot map unless handle is started... */ | 
|---|
| 2125 | if (!mpd->do_map) | 
|---|
| 2126 | return false; | 
|---|
| 2127 | map->m_lblk = lblk; | 
|---|
| 2128 | map->m_len = 1; | 
|---|
| 2129 | map->m_flags = bh->b_state & BH_FLAGS; | 
|---|
| 2130 | return true; | 
|---|
| 2131 | } | 
|---|
| 2132 |  | 
|---|
| 2133 | /* Don't go larger than mballoc is willing to allocate */ | 
|---|
| 2134 | if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) | 
|---|
| 2135 | return false; | 
|---|
| 2136 |  | 
|---|
| 2137 | /* Can we merge the block to our big extent? */ | 
|---|
| 2138 | if (lblk == map->m_lblk + map->m_len && | 
|---|
| 2139 | (bh->b_state & BH_FLAGS) == map->m_flags) { | 
|---|
| 2140 | map->m_len++; | 
|---|
| 2141 | return true; | 
|---|
| 2142 | } | 
|---|
| 2143 | return false; | 
|---|
| 2144 | } | 
|---|
| 2145 |  | 
|---|
| 2146 | /* | 
|---|
| 2147 | * mpage_process_page_bufs - submit page buffers for IO or add them to extent | 
|---|
| 2148 | * | 
|---|
| 2149 | * @mpd - extent of blocks for mapping | 
|---|
| 2150 | * @head - the first buffer in the page | 
|---|
| 2151 | * @bh - buffer we should start processing from | 
|---|
| 2152 | * @lblk - logical number of the block in the file corresponding to @bh | 
|---|
| 2153 | * | 
|---|
| 2154 | * Walk through page buffers from @bh upto @head (exclusive) and either submit | 
|---|
| 2155 | * the page for IO if all buffers in this page were mapped and there's no | 
|---|
| 2156 | * accumulated extent of buffers to map or add buffers in the page to the | 
|---|
| 2157 | * extent of buffers to map. The function returns 1 if the caller can continue | 
|---|
| 2158 | * by processing the next page, 0 if it should stop adding buffers to the | 
|---|
| 2159 | * extent to map because we cannot extend it anymore. It can also return value | 
|---|
| 2160 | * < 0 in case of error during IO submission. | 
|---|
| 2161 | */ | 
|---|
| 2162 | static int mpage_process_page_bufs(struct mpage_da_data *mpd, | 
|---|
| 2163 | struct buffer_head *head, | 
|---|
| 2164 | struct buffer_head *bh, | 
|---|
| 2165 | ext4_lblk_t lblk) | 
|---|
| 2166 | { | 
|---|
| 2167 | struct inode *inode = mpd->inode; | 
|---|
| 2168 | int err; | 
|---|
| 2169 | ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(node: inode) - 1) | 
|---|
| 2170 | >> inode->i_blkbits; | 
|---|
| 2171 |  | 
|---|
| 2172 | if (ext4_verity_in_progress(inode)) | 
|---|
| 2173 | blocks = EXT_MAX_BLOCKS; | 
|---|
| 2174 |  | 
|---|
| 2175 | do { | 
|---|
| 2176 | BUG_ON(buffer_locked(bh)); | 
|---|
| 2177 |  | 
|---|
| 2178 | if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { | 
|---|
| 2179 | /* Found extent to map? */ | 
|---|
| 2180 | if (mpd->map.m_len) | 
|---|
| 2181 | return 0; | 
|---|
| 2182 | /* Buffer needs mapping and handle is not started? */ | 
|---|
| 2183 | if (!mpd->do_map) | 
|---|
| 2184 | return 0; | 
|---|
| 2185 | /* Everything mapped so far and we hit EOF */ | 
|---|
| 2186 | break; | 
|---|
| 2187 | } | 
|---|
| 2188 | } while (lblk++, (bh = bh->b_this_page) != head); | 
|---|
| 2189 | /* So far everything mapped? Submit the page for IO. */ | 
|---|
| 2190 | if (mpd->map.m_len == 0) { | 
|---|
| 2191 | err = mpage_submit_folio(mpd, folio: head->b_folio); | 
|---|
| 2192 | if (err < 0) | 
|---|
| 2193 | return err; | 
|---|
| 2194 | mpage_folio_done(mpd, folio: head->b_folio); | 
|---|
| 2195 | } | 
|---|
| 2196 | if (lblk >= blocks) { | 
|---|
| 2197 | mpd->scanned_until_end = 1; | 
|---|
| 2198 | return 0; | 
|---|
| 2199 | } | 
|---|
| 2200 | return 1; | 
|---|
| 2201 | } | 
|---|
| 2202 |  | 
|---|
| 2203 | /* | 
|---|
| 2204 | * mpage_process_folio - update folio buffers corresponding to changed extent | 
|---|
| 2205 | *			 and may submit fully mapped page for IO | 
|---|
| 2206 | * @mpd: description of extent to map, on return next extent to map | 
|---|
| 2207 | * @folio: Contains these buffers. | 
|---|
| 2208 | * @m_lblk: logical block mapping. | 
|---|
| 2209 | * @m_pblk: corresponding physical mapping. | 
|---|
| 2210 | * @map_bh: determines on return whether this page requires any further | 
|---|
| 2211 | *		  mapping or not. | 
|---|
| 2212 | * | 
|---|
| 2213 | * Scan given folio buffers corresponding to changed extent and update buffer | 
|---|
| 2214 | * state according to new extent state. | 
|---|
| 2215 | * We map delalloc buffers to their physical location, clear unwritten bits. | 
|---|
| 2216 | * If the given folio is not fully mapped, we update @mpd to the next extent in | 
|---|
| 2217 | * the given folio that needs mapping & return @map_bh as true. | 
|---|
| 2218 | */ | 
|---|
| 2219 | static int mpage_process_folio(struct mpage_da_data *mpd, struct folio *folio, | 
|---|
| 2220 | ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, | 
|---|
| 2221 | bool *map_bh) | 
|---|
| 2222 | { | 
|---|
| 2223 | struct buffer_head *head, *bh; | 
|---|
| 2224 | ext4_io_end_t *io_end = mpd->io_submit.io_end; | 
|---|
| 2225 | ext4_lblk_t lblk = *m_lblk; | 
|---|
| 2226 | ext4_fsblk_t pblock = *m_pblk; | 
|---|
| 2227 | int err = 0; | 
|---|
| 2228 | int blkbits = mpd->inode->i_blkbits; | 
|---|
| 2229 | ssize_t io_end_size = 0; | 
|---|
| 2230 | struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); | 
|---|
| 2231 |  | 
|---|
| 2232 | bh = head = folio_buffers(folio); | 
|---|
| 2233 | do { | 
|---|
| 2234 | if (lblk < mpd->map.m_lblk) | 
|---|
| 2235 | continue; | 
|---|
| 2236 | if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { | 
|---|
| 2237 | /* | 
|---|
| 2238 | * Buffer after end of mapped extent. | 
|---|
| 2239 | * Find next buffer in the folio to map. | 
|---|
| 2240 | */ | 
|---|
| 2241 | mpd->map.m_len = 0; | 
|---|
| 2242 | mpd->map.m_flags = 0; | 
|---|
| 2243 | io_end_vec->size += io_end_size; | 
|---|
| 2244 |  | 
|---|
| 2245 | err = mpage_process_page_bufs(mpd, head, bh, lblk); | 
|---|
| 2246 | if (err > 0) | 
|---|
| 2247 | err = 0; | 
|---|
| 2248 | if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { | 
|---|
| 2249 | io_end_vec = ext4_alloc_io_end_vec(io_end); | 
|---|
| 2250 | if (IS_ERR(ptr: io_end_vec)) { | 
|---|
| 2251 | err = PTR_ERR(ptr: io_end_vec); | 
|---|
| 2252 | goto out; | 
|---|
| 2253 | } | 
|---|
| 2254 | io_end_vec->offset = (loff_t)mpd->map.m_lblk << blkbits; | 
|---|
| 2255 | } | 
|---|
| 2256 | *map_bh = true; | 
|---|
| 2257 | goto out; | 
|---|
| 2258 | } | 
|---|
| 2259 | if (buffer_delay(bh)) { | 
|---|
| 2260 | clear_buffer_delay(bh); | 
|---|
| 2261 | bh->b_blocknr = pblock++; | 
|---|
| 2262 | } | 
|---|
| 2263 | clear_buffer_unwritten(bh); | 
|---|
| 2264 | io_end_size += (1 << blkbits); | 
|---|
| 2265 | } while (lblk++, (bh = bh->b_this_page) != head); | 
|---|
| 2266 |  | 
|---|
| 2267 | io_end_vec->size += io_end_size; | 
|---|
| 2268 | *map_bh = false; | 
|---|
| 2269 | out: | 
|---|
| 2270 | *m_lblk = lblk; | 
|---|
| 2271 | *m_pblk = pblock; | 
|---|
| 2272 | return err; | 
|---|
| 2273 | } | 
|---|
| 2274 |  | 
|---|
| 2275 | /* | 
|---|
| 2276 | * mpage_map_buffers - update buffers corresponding to changed extent and | 
|---|
| 2277 | *		       submit fully mapped pages for IO | 
|---|
| 2278 | * | 
|---|
| 2279 | * @mpd - description of extent to map, on return next extent to map | 
|---|
| 2280 | * | 
|---|
| 2281 | * Scan buffers corresponding to changed extent (we expect corresponding pages | 
|---|
| 2282 | * to be already locked) and update buffer state according to new extent state. | 
|---|
| 2283 | * We map delalloc buffers to their physical location, clear unwritten bits, | 
|---|
| 2284 | * and mark buffers as uninit when we perform writes to unwritten extents | 
|---|
| 2285 | * and do extent conversion after IO is finished. If the last page is not fully | 
|---|
| 2286 | * mapped, we update @map to the next extent in the last page that needs | 
|---|
| 2287 | * mapping. Otherwise we submit the page for IO. | 
|---|
| 2288 | */ | 
|---|
| 2289 | static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) | 
|---|
| 2290 | { | 
|---|
| 2291 | struct folio_batch fbatch; | 
|---|
| 2292 | unsigned nr, i; | 
|---|
| 2293 | struct inode *inode = mpd->inode; | 
|---|
| 2294 | int bpp_bits = PAGE_SHIFT - inode->i_blkbits; | 
|---|
| 2295 | pgoff_t start, end; | 
|---|
| 2296 | ext4_lblk_t lblk; | 
|---|
| 2297 | ext4_fsblk_t pblock; | 
|---|
| 2298 | int err; | 
|---|
| 2299 | bool map_bh = false; | 
|---|
| 2300 |  | 
|---|
| 2301 | start = mpd->map.m_lblk >> bpp_bits; | 
|---|
| 2302 | end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; | 
|---|
| 2303 | pblock = mpd->map.m_pblk; | 
|---|
| 2304 |  | 
|---|
| 2305 | folio_batch_init(fbatch: &fbatch); | 
|---|
| 2306 | while (start <= end) { | 
|---|
| 2307 | nr = filemap_get_folios(mapping: inode->i_mapping, start: &start, end, fbatch: &fbatch); | 
|---|
| 2308 | if (nr == 0) | 
|---|
| 2309 | break; | 
|---|
| 2310 | for (i = 0; i < nr; i++) { | 
|---|
| 2311 | struct folio *folio = fbatch.folios[i]; | 
|---|
| 2312 |  | 
|---|
| 2313 | lblk = folio->index << bpp_bits; | 
|---|
| 2314 | err = mpage_process_folio(mpd, folio, m_lblk: &lblk, m_pblk: &pblock, | 
|---|
| 2315 | map_bh: &map_bh); | 
|---|
| 2316 | /* | 
|---|
| 2317 | * If map_bh is true, means page may require further bh | 
|---|
| 2318 | * mapping, or maybe the page was submitted for IO. | 
|---|
| 2319 | * So we return to call further extent mapping. | 
|---|
| 2320 | */ | 
|---|
| 2321 | if (err < 0 || map_bh) | 
|---|
| 2322 | goto out; | 
|---|
| 2323 | /* Page fully mapped - let IO run! */ | 
|---|
| 2324 | err = mpage_submit_folio(mpd, folio); | 
|---|
| 2325 | if (err < 0) | 
|---|
| 2326 | goto out; | 
|---|
| 2327 | mpage_folio_done(mpd, folio); | 
|---|
| 2328 | } | 
|---|
| 2329 | folio_batch_release(fbatch: &fbatch); | 
|---|
| 2330 | } | 
|---|
| 2331 | /* Extent fully mapped and matches with page boundary. We are done. */ | 
|---|
| 2332 | mpd->map.m_len = 0; | 
|---|
| 2333 | mpd->map.m_flags = 0; | 
|---|
| 2334 | return 0; | 
|---|
| 2335 | out: | 
|---|
| 2336 | folio_batch_release(fbatch: &fbatch); | 
|---|
| 2337 | return err; | 
|---|
| 2338 | } | 
|---|
| 2339 |  | 
|---|
| 2340 | static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) | 
|---|
| 2341 | { | 
|---|
| 2342 | struct inode *inode = mpd->inode; | 
|---|
| 2343 | struct ext4_map_blocks *map = &mpd->map; | 
|---|
| 2344 | int get_blocks_flags; | 
|---|
| 2345 | int err, dioread_nolock; | 
|---|
| 2346 |  | 
|---|
| 2347 | /* Make sure transaction has enough credits for this extent */ | 
|---|
| 2348 | err = ext4_journal_ensure_extent_credits(handle, inode); | 
|---|
| 2349 | if (err < 0) | 
|---|
| 2350 | return err; | 
|---|
| 2351 |  | 
|---|
| 2352 | trace_ext4_da_write_pages_extent(inode, map); | 
|---|
| 2353 | /* | 
|---|
| 2354 | * Call ext4_map_blocks() to allocate any delayed allocation blocks, or | 
|---|
| 2355 | * to convert an unwritten extent to be initialized (in the case | 
|---|
| 2356 | * where we have written into one or more preallocated blocks).  It is | 
|---|
| 2357 | * possible that we're going to need more metadata blocks than | 
|---|
| 2358 | * previously reserved. However we must not fail because we're in | 
|---|
| 2359 | * writeback and there is nothing we can do about it so it might result | 
|---|
| 2360 | * in data loss.  So use reserved blocks to allocate metadata if | 
|---|
| 2361 | * possible. In addition, do not cache any unrelated extents, as it | 
|---|
| 2362 | * only holds the folio lock but does not hold the i_rwsem or | 
|---|
| 2363 | * invalidate_lock, which could corrupt the extent status tree. | 
|---|
| 2364 | */ | 
|---|
| 2365 | get_blocks_flags = EXT4_GET_BLOCKS_CREATE | | 
|---|
| 2366 | EXT4_GET_BLOCKS_METADATA_NOFAIL | | 
|---|
| 2367 | EXT4_GET_BLOCKS_IO_SUBMIT | | 
|---|
| 2368 | EXT4_EX_NOCACHE; | 
|---|
| 2369 |  | 
|---|
| 2370 | dioread_nolock = ext4_should_dioread_nolock(inode); | 
|---|
| 2371 | if (dioread_nolock) | 
|---|
| 2372 | get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; | 
|---|
| 2373 |  | 
|---|
| 2374 | err = ext4_map_blocks(handle, inode, map, flags: get_blocks_flags); | 
|---|
| 2375 | if (err < 0) | 
|---|
| 2376 | return err; | 
|---|
| 2377 | if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { | 
|---|
| 2378 | if (!mpd->io_submit.io_end->handle && | 
|---|
| 2379 | ext4_handle_valid(handle)) { | 
|---|
| 2380 | mpd->io_submit.io_end->handle = handle->h_rsv_handle; | 
|---|
| 2381 | handle->h_rsv_handle = NULL; | 
|---|
| 2382 | } | 
|---|
| 2383 | ext4_set_io_unwritten_flag(io_end: mpd->io_submit.io_end); | 
|---|
| 2384 | } | 
|---|
| 2385 |  | 
|---|
| 2386 | BUG_ON(map->m_len == 0); | 
|---|
| 2387 | return 0; | 
|---|
| 2388 | } | 
|---|
| 2389 |  | 
|---|
| 2390 | /* | 
|---|
| 2391 | * This is used to submit mapped buffers in a single folio that is not fully | 
|---|
| 2392 | * mapped for various reasons, such as insufficient space or journal credits. | 
|---|
| 2393 | */ | 
|---|
| 2394 | static int mpage_submit_partial_folio(struct mpage_da_data *mpd) | 
|---|
| 2395 | { | 
|---|
| 2396 | struct inode *inode = mpd->inode; | 
|---|
| 2397 | struct folio *folio; | 
|---|
| 2398 | loff_t pos; | 
|---|
| 2399 | int ret; | 
|---|
| 2400 |  | 
|---|
| 2401 | folio = filemap_get_folio(mapping: inode->i_mapping, | 
|---|
| 2402 | index: mpd->start_pos >> PAGE_SHIFT); | 
|---|
| 2403 | if (IS_ERR(ptr: folio)) | 
|---|
| 2404 | return PTR_ERR(ptr: folio); | 
|---|
| 2405 | /* | 
|---|
| 2406 | * The mapped position should be within the current processing folio | 
|---|
| 2407 | * but must not be the folio start position. | 
|---|
| 2408 | */ | 
|---|
| 2409 | pos = ((loff_t)mpd->map.m_lblk) << inode->i_blkbits; | 
|---|
| 2410 | if (WARN_ON_ONCE((folio_pos(folio) == pos) || | 
|---|
| 2411 | !folio_contains(folio, pos >> PAGE_SHIFT))) | 
|---|
| 2412 | return -EINVAL; | 
|---|
| 2413 |  | 
|---|
| 2414 | ret = mpage_submit_folio(mpd, folio); | 
|---|
| 2415 | if (ret) | 
|---|
| 2416 | goto out; | 
|---|
| 2417 | /* | 
|---|
| 2418 | * Update start_pos to prevent this folio from being released in | 
|---|
| 2419 | * mpage_release_unused_pages(), it will be reset to the aligned folio | 
|---|
| 2420 | * pos when this folio is written again in the next round. Additionally, | 
|---|
| 2421 | * do not update wbc->nr_to_write here, as it will be updated once the | 
|---|
| 2422 | * entire folio has finished processing. | 
|---|
| 2423 | */ | 
|---|
| 2424 | mpd->start_pos = pos; | 
|---|
| 2425 | out: | 
|---|
| 2426 | folio_unlock(folio); | 
|---|
| 2427 | folio_put(folio); | 
|---|
| 2428 | return ret; | 
|---|
| 2429 | } | 
|---|
| 2430 |  | 
|---|
| 2431 | /* | 
|---|
| 2432 | * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length | 
|---|
| 2433 | *				 mpd->len and submit pages underlying it for IO | 
|---|
| 2434 | * | 
|---|
| 2435 | * @handle - handle for journal operations | 
|---|
| 2436 | * @mpd - extent to map | 
|---|
| 2437 | * @give_up_on_write - we set this to true iff there is a fatal error and there | 
|---|
| 2438 | *                     is no hope of writing the data. The caller should discard | 
|---|
| 2439 | *                     dirty pages to avoid infinite loops. | 
|---|
| 2440 | * | 
|---|
| 2441 | * The function maps extent starting at mpd->lblk of length mpd->len. If it is | 
|---|
| 2442 | * delayed, blocks are allocated, if it is unwritten, we may need to convert | 
|---|
| 2443 | * them to initialized or split the described range from larger unwritten | 
|---|
| 2444 | * extent. Note that we need not map all the described range since allocation | 
|---|
| 2445 | * can return less blocks or the range is covered by more unwritten extents. We | 
|---|
| 2446 | * cannot map more because we are limited by reserved transaction credits. On | 
|---|
| 2447 | * the other hand we always make sure that the last touched page is fully | 
|---|
| 2448 | * mapped so that it can be written out (and thus forward progress is | 
|---|
| 2449 | * guaranteed). After mapping we submit all mapped pages for IO. | 
|---|
| 2450 | */ | 
|---|
| 2451 | static int mpage_map_and_submit_extent(handle_t *handle, | 
|---|
| 2452 | struct mpage_da_data *mpd, | 
|---|
| 2453 | bool *give_up_on_write) | 
|---|
| 2454 | { | 
|---|
| 2455 | struct inode *inode = mpd->inode; | 
|---|
| 2456 | struct ext4_map_blocks *map = &mpd->map; | 
|---|
| 2457 | int err; | 
|---|
| 2458 | loff_t disksize; | 
|---|
| 2459 | int progress = 0; | 
|---|
| 2460 | ext4_io_end_t *io_end = mpd->io_submit.io_end; | 
|---|
| 2461 | struct ext4_io_end_vec *io_end_vec; | 
|---|
| 2462 |  | 
|---|
| 2463 | io_end_vec = ext4_alloc_io_end_vec(io_end); | 
|---|
| 2464 | if (IS_ERR(ptr: io_end_vec)) | 
|---|
| 2465 | return PTR_ERR(ptr: io_end_vec); | 
|---|
| 2466 | io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; | 
|---|
| 2467 | do { | 
|---|
| 2468 | err = mpage_map_one_extent(handle, mpd); | 
|---|
| 2469 | if (err < 0) { | 
|---|
| 2470 | struct super_block *sb = inode->i_sb; | 
|---|
| 2471 |  | 
|---|
| 2472 | if (ext4_emergency_state(sb)) | 
|---|
| 2473 | goto invalidate_dirty_pages; | 
|---|
| 2474 | /* | 
|---|
| 2475 | * Let the uper layers retry transient errors. | 
|---|
| 2476 | * In the case of ENOSPC, if ext4_count_free_blocks() | 
|---|
| 2477 | * is non-zero, a commit should free up blocks. | 
|---|
| 2478 | */ | 
|---|
| 2479 | if ((err == -ENOMEM) || (err == -EAGAIN) || | 
|---|
| 2480 | (err == -ENOSPC && ext4_count_free_clusters(sb))) { | 
|---|
| 2481 | /* | 
|---|
| 2482 | * We may have already allocated extents for | 
|---|
| 2483 | * some bhs inside the folio, issue the | 
|---|
| 2484 | * corresponding data to prevent stale data. | 
|---|
| 2485 | */ | 
|---|
| 2486 | if (progress) { | 
|---|
| 2487 | if (mpage_submit_partial_folio(mpd)) | 
|---|
| 2488 | goto invalidate_dirty_pages; | 
|---|
| 2489 | goto update_disksize; | 
|---|
| 2490 | } | 
|---|
| 2491 | return err; | 
|---|
| 2492 | } | 
|---|
| 2493 | ext4_msg(sb, KERN_CRIT, | 
|---|
| 2494 | "Delayed block allocation failed for " | 
|---|
| 2495 | "inode %lu at logical offset %llu with" | 
|---|
| 2496 | " max blocks %u with error %d", | 
|---|
| 2497 | inode->i_ino, | 
|---|
| 2498 | (unsigned long long)map->m_lblk, | 
|---|
| 2499 | (unsigned)map->m_len, -err); | 
|---|
| 2500 | ext4_msg(sb, KERN_CRIT, | 
|---|
| 2501 | "This should not happen!! Data will " | 
|---|
| 2502 | "be lost\n"); | 
|---|
| 2503 | if (err == -ENOSPC) | 
|---|
| 2504 | ext4_print_free_blocks(inode); | 
|---|
| 2505 | invalidate_dirty_pages: | 
|---|
| 2506 | *give_up_on_write = true; | 
|---|
| 2507 | return err; | 
|---|
| 2508 | } | 
|---|
| 2509 | progress = 1; | 
|---|
| 2510 | /* | 
|---|
| 2511 | * Update buffer state, submit mapped pages, and get us new | 
|---|
| 2512 | * extent to map | 
|---|
| 2513 | */ | 
|---|
| 2514 | err = mpage_map_and_submit_buffers(mpd); | 
|---|
| 2515 | if (err < 0) | 
|---|
| 2516 | goto update_disksize; | 
|---|
| 2517 | } while (map->m_len); | 
|---|
| 2518 |  | 
|---|
| 2519 | update_disksize: | 
|---|
| 2520 | /* | 
|---|
| 2521 | * Update on-disk size after IO is submitted.  Races with | 
|---|
| 2522 | * truncate are avoided by checking i_size under i_data_sem. | 
|---|
| 2523 | */ | 
|---|
| 2524 | disksize = mpd->start_pos; | 
|---|
| 2525 | if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { | 
|---|
| 2526 | int err2; | 
|---|
| 2527 | loff_t i_size; | 
|---|
| 2528 |  | 
|---|
| 2529 | down_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 2530 | i_size = i_size_read(inode); | 
|---|
| 2531 | if (disksize > i_size) | 
|---|
| 2532 | disksize = i_size; | 
|---|
| 2533 | if (disksize > EXT4_I(inode)->i_disksize) | 
|---|
| 2534 | EXT4_I(inode)->i_disksize = disksize; | 
|---|
| 2535 | up_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 2536 | err2 = ext4_mark_inode_dirty(handle, inode); | 
|---|
| 2537 | if (err2) { | 
|---|
| 2538 | ext4_error_err(inode->i_sb, -err2, | 
|---|
| 2539 | "Failed to mark inode %lu dirty", | 
|---|
| 2540 | inode->i_ino); | 
|---|
| 2541 | } | 
|---|
| 2542 | if (!err) | 
|---|
| 2543 | err = err2; | 
|---|
| 2544 | } | 
|---|
| 2545 | return err; | 
|---|
| 2546 | } | 
|---|
| 2547 |  | 
|---|
| 2548 | static int ext4_journal_folio_buffers(handle_t *handle, struct folio *folio, | 
|---|
| 2549 | size_t len) | 
|---|
| 2550 | { | 
|---|
| 2551 | struct buffer_head *page_bufs = folio_buffers(folio); | 
|---|
| 2552 | struct inode *inode = folio->mapping->host; | 
|---|
| 2553 | int ret, err; | 
|---|
| 2554 |  | 
|---|
| 2555 | ret = ext4_walk_page_buffers(handle, inode, head: page_bufs, from: 0, to: len, | 
|---|
| 2556 | NULL, fn: do_journal_get_write_access); | 
|---|
| 2557 | err = ext4_walk_page_buffers(handle, inode, head: page_bufs, from: 0, to: len, | 
|---|
| 2558 | NULL, fn: write_end_fn); | 
|---|
| 2559 | if (ret == 0) | 
|---|
| 2560 | ret = err; | 
|---|
| 2561 | err = ext4_jbd2_inode_add_write(handle, inode, start_byte: folio_pos(folio), length: len); | 
|---|
| 2562 | if (ret == 0) | 
|---|
| 2563 | ret = err; | 
|---|
| 2564 | EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; | 
|---|
| 2565 |  | 
|---|
| 2566 | return ret; | 
|---|
| 2567 | } | 
|---|
| 2568 |  | 
|---|
| 2569 | static int mpage_journal_page_buffers(handle_t *handle, | 
|---|
| 2570 | struct mpage_da_data *mpd, | 
|---|
| 2571 | struct folio *folio) | 
|---|
| 2572 | { | 
|---|
| 2573 | struct inode *inode = mpd->inode; | 
|---|
| 2574 | loff_t size = i_size_read(inode); | 
|---|
| 2575 | size_t len = folio_size(folio); | 
|---|
| 2576 |  | 
|---|
| 2577 | folio_clear_checked(folio); | 
|---|
| 2578 | mpd->wbc->nr_to_write -= folio_nr_pages(folio); | 
|---|
| 2579 |  | 
|---|
| 2580 | if (folio_pos(folio) + len > size && | 
|---|
| 2581 | !ext4_verity_in_progress(inode)) | 
|---|
| 2582 | len = size & (len - 1); | 
|---|
| 2583 |  | 
|---|
| 2584 | return ext4_journal_folio_buffers(handle, folio, len); | 
|---|
| 2585 | } | 
|---|
| 2586 |  | 
|---|
| 2587 | /* | 
|---|
| 2588 | * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages | 
|---|
| 2589 | * 				 needing mapping, submit mapped pages | 
|---|
| 2590 | * | 
|---|
| 2591 | * @mpd - where to look for pages | 
|---|
| 2592 | * | 
|---|
| 2593 | * Walk dirty pages in the mapping. If they are fully mapped, submit them for | 
|---|
| 2594 | * IO immediately. If we cannot map blocks, we submit just already mapped | 
|---|
| 2595 | * buffers in the page for IO and keep page dirty. When we can map blocks and | 
|---|
| 2596 | * we find a page which isn't mapped we start accumulating extent of buffers | 
|---|
| 2597 | * underlying these pages that needs mapping (formed by either delayed or | 
|---|
| 2598 | * unwritten buffers). We also lock the pages containing these buffers. The | 
|---|
| 2599 | * extent found is returned in @mpd structure (starting at mpd->lblk with | 
|---|
| 2600 | * length mpd->len blocks). | 
|---|
| 2601 | * | 
|---|
| 2602 | * Note that this function can attach bios to one io_end structure which are | 
|---|
| 2603 | * neither logically nor physically contiguous. Although it may seem as an | 
|---|
| 2604 | * unnecessary complication, it is actually inevitable in blocksize < pagesize | 
|---|
| 2605 | * case as we need to track IO to all buffers underlying a page in one io_end. | 
|---|
| 2606 | */ | 
|---|
| 2607 | static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) | 
|---|
| 2608 | { | 
|---|
| 2609 | struct address_space *mapping = mpd->inode->i_mapping; | 
|---|
| 2610 | struct folio_batch fbatch; | 
|---|
| 2611 | unsigned int nr_folios; | 
|---|
| 2612 | pgoff_t index = mpd->start_pos >> PAGE_SHIFT; | 
|---|
| 2613 | pgoff_t end = mpd->end_pos >> PAGE_SHIFT; | 
|---|
| 2614 | xa_mark_t tag; | 
|---|
| 2615 | int i, err = 0; | 
|---|
| 2616 | int blkbits = mpd->inode->i_blkbits; | 
|---|
| 2617 | ext4_lblk_t lblk; | 
|---|
| 2618 | struct buffer_head *head; | 
|---|
| 2619 | handle_t *handle = NULL; | 
|---|
| 2620 | int bpp = ext4_journal_blocks_per_folio(inode: mpd->inode); | 
|---|
| 2621 |  | 
|---|
| 2622 | if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) | 
|---|
| 2623 | tag = PAGECACHE_TAG_TOWRITE; | 
|---|
| 2624 | else | 
|---|
| 2625 | tag = PAGECACHE_TAG_DIRTY; | 
|---|
| 2626 |  | 
|---|
| 2627 | mpd->map.m_len = 0; | 
|---|
| 2628 | mpd->next_pos = mpd->start_pos; | 
|---|
| 2629 | if (ext4_should_journal_data(inode: mpd->inode)) { | 
|---|
| 2630 | handle = ext4_journal_start(mpd->inode, EXT4_HT_WRITE_PAGE, | 
|---|
| 2631 | bpp); | 
|---|
| 2632 | if (IS_ERR(ptr: handle)) | 
|---|
| 2633 | return PTR_ERR(ptr: handle); | 
|---|
| 2634 | } | 
|---|
| 2635 | folio_batch_init(fbatch: &fbatch); | 
|---|
| 2636 | while (index <= end) { | 
|---|
| 2637 | nr_folios = filemap_get_folios_tag(mapping, start: &index, end, | 
|---|
| 2638 | tag, fbatch: &fbatch); | 
|---|
| 2639 | if (nr_folios == 0) | 
|---|
| 2640 | break; | 
|---|
| 2641 |  | 
|---|
| 2642 | for (i = 0; i < nr_folios; i++) { | 
|---|
| 2643 | struct folio *folio = fbatch.folios[i]; | 
|---|
| 2644 |  | 
|---|
| 2645 | /* | 
|---|
| 2646 | * Accumulated enough dirty pages? This doesn't apply | 
|---|
| 2647 | * to WB_SYNC_ALL mode. For integrity sync we have to | 
|---|
| 2648 | * keep going because someone may be concurrently | 
|---|
| 2649 | * dirtying pages, and we might have synced a lot of | 
|---|
| 2650 | * newly appeared dirty pages, but have not synced all | 
|---|
| 2651 | * of the old dirty pages. | 
|---|
| 2652 | */ | 
|---|
| 2653 | if (mpd->wbc->sync_mode == WB_SYNC_NONE && | 
|---|
| 2654 | mpd->wbc->nr_to_write <= | 
|---|
| 2655 | mpd->map.m_len >> (PAGE_SHIFT - blkbits)) | 
|---|
| 2656 | goto out; | 
|---|
| 2657 |  | 
|---|
| 2658 | /* If we can't merge this page, we are done. */ | 
|---|
| 2659 | if (mpd->map.m_len > 0 && | 
|---|
| 2660 | mpd->next_pos != folio_pos(folio)) | 
|---|
| 2661 | goto out; | 
|---|
| 2662 |  | 
|---|
| 2663 | if (handle) { | 
|---|
| 2664 | err = ext4_journal_ensure_credits(handle, credits: bpp, | 
|---|
| 2665 | revoke_creds: 0); | 
|---|
| 2666 | if (err < 0) | 
|---|
| 2667 | goto out; | 
|---|
| 2668 | } | 
|---|
| 2669 |  | 
|---|
| 2670 | folio_lock(folio); | 
|---|
| 2671 | /* | 
|---|
| 2672 | * If the page is no longer dirty, or its mapping no | 
|---|
| 2673 | * longer corresponds to inode we are writing (which | 
|---|
| 2674 | * means it has been truncated or invalidated), or the | 
|---|
| 2675 | * page is already under writeback and we are not doing | 
|---|
| 2676 | * a data integrity writeback, skip the page | 
|---|
| 2677 | */ | 
|---|
| 2678 | if (!folio_test_dirty(folio) || | 
|---|
| 2679 | (folio_test_writeback(folio) && | 
|---|
| 2680 | (mpd->wbc->sync_mode == WB_SYNC_NONE)) || | 
|---|
| 2681 | unlikely(folio->mapping != mapping)) { | 
|---|
| 2682 | folio_unlock(folio); | 
|---|
| 2683 | continue; | 
|---|
| 2684 | } | 
|---|
| 2685 |  | 
|---|
| 2686 | folio_wait_writeback(folio); | 
|---|
| 2687 | BUG_ON(folio_test_writeback(folio)); | 
|---|
| 2688 |  | 
|---|
| 2689 | /* | 
|---|
| 2690 | * Should never happen but for buggy code in | 
|---|
| 2691 | * other subsystems that call | 
|---|
| 2692 | * set_page_dirty() without properly warning | 
|---|
| 2693 | * the file system first.  See [1] for more | 
|---|
| 2694 | * information. | 
|---|
| 2695 | * | 
|---|
| 2696 | * [1] https://lore.kernel.org/linux-mm/20180103100430.GE4911@quack2.suse.cz | 
|---|
| 2697 | */ | 
|---|
| 2698 | if (!folio_buffers(folio)) { | 
|---|
| 2699 | ext4_warning_inode(mpd->inode, "page %lu does not have buffers attached", folio->index); | 
|---|
| 2700 | folio_clear_dirty(folio); | 
|---|
| 2701 | folio_unlock(folio); | 
|---|
| 2702 | continue; | 
|---|
| 2703 | } | 
|---|
| 2704 |  | 
|---|
| 2705 | if (mpd->map.m_len == 0) | 
|---|
| 2706 | mpd->start_pos = folio_pos(folio); | 
|---|
| 2707 | mpd->next_pos = folio_pos(folio) + folio_size(folio); | 
|---|
| 2708 | /* | 
|---|
| 2709 | * Writeout when we cannot modify metadata is simple. | 
|---|
| 2710 | * Just submit the page. For data=journal mode we | 
|---|
| 2711 | * first handle writeout of the page for checkpoint and | 
|---|
| 2712 | * only after that handle delayed page dirtying. This | 
|---|
| 2713 | * makes sure current data is checkpointed to the final | 
|---|
| 2714 | * location before possibly journalling it again which | 
|---|
| 2715 | * is desirable when the page is frequently dirtied | 
|---|
| 2716 | * through a pin. | 
|---|
| 2717 | */ | 
|---|
| 2718 | if (!mpd->can_map) { | 
|---|
| 2719 | err = mpage_submit_folio(mpd, folio); | 
|---|
| 2720 | if (err < 0) | 
|---|
| 2721 | goto out; | 
|---|
| 2722 | /* Pending dirtying of journalled data? */ | 
|---|
| 2723 | if (folio_test_checked(folio)) { | 
|---|
| 2724 | err = mpage_journal_page_buffers(handle, | 
|---|
| 2725 | mpd, folio); | 
|---|
| 2726 | if (err < 0) | 
|---|
| 2727 | goto out; | 
|---|
| 2728 | mpd->journalled_more_data = 1; | 
|---|
| 2729 | } | 
|---|
| 2730 | mpage_folio_done(mpd, folio); | 
|---|
| 2731 | } else { | 
|---|
| 2732 | /* Add all dirty buffers to mpd */ | 
|---|
| 2733 | lblk = ((ext4_lblk_t)folio->index) << | 
|---|
| 2734 | (PAGE_SHIFT - blkbits); | 
|---|
| 2735 | head = folio_buffers(folio); | 
|---|
| 2736 | err = mpage_process_page_bufs(mpd, head, bh: head, | 
|---|
| 2737 | lblk); | 
|---|
| 2738 | if (err <= 0) | 
|---|
| 2739 | goto out; | 
|---|
| 2740 | err = 0; | 
|---|
| 2741 | } | 
|---|
| 2742 | } | 
|---|
| 2743 | folio_batch_release(fbatch: &fbatch); | 
|---|
| 2744 | cond_resched(); | 
|---|
| 2745 | } | 
|---|
| 2746 | mpd->scanned_until_end = 1; | 
|---|
| 2747 | if (handle) | 
|---|
| 2748 | ext4_journal_stop(handle); | 
|---|
| 2749 | return 0; | 
|---|
| 2750 | out: | 
|---|
| 2751 | folio_batch_release(fbatch: &fbatch); | 
|---|
| 2752 | if (handle) | 
|---|
| 2753 | ext4_journal_stop(handle); | 
|---|
| 2754 | return err; | 
|---|
| 2755 | } | 
|---|
| 2756 |  | 
|---|
| 2757 | static int ext4_do_writepages(struct mpage_da_data *mpd) | 
|---|
| 2758 | { | 
|---|
| 2759 | struct writeback_control *wbc = mpd->wbc; | 
|---|
| 2760 | pgoff_t	writeback_index = 0; | 
|---|
| 2761 | long nr_to_write = wbc->nr_to_write; | 
|---|
| 2762 | int range_whole = 0; | 
|---|
| 2763 | int cycled = 1; | 
|---|
| 2764 | handle_t *handle = NULL; | 
|---|
| 2765 | struct inode *inode = mpd->inode; | 
|---|
| 2766 | struct address_space *mapping = inode->i_mapping; | 
|---|
| 2767 | int needed_blocks, rsv_blocks = 0, ret = 0; | 
|---|
| 2768 | struct ext4_sb_info *sbi = EXT4_SB(sb: mapping->host->i_sb); | 
|---|
| 2769 | struct blk_plug plug; | 
|---|
| 2770 | bool give_up_on_write = false; | 
|---|
| 2771 |  | 
|---|
| 2772 | trace_ext4_writepages(inode, wbc); | 
|---|
| 2773 |  | 
|---|
| 2774 | /* | 
|---|
| 2775 | * No pages to write? This is mainly a kludge to avoid starting | 
|---|
| 2776 | * a transaction for special inodes like journal inode on last iput() | 
|---|
| 2777 | * because that could violate lock ordering on umount | 
|---|
| 2778 | */ | 
|---|
| 2779 | if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) | 
|---|
| 2780 | goto out_writepages; | 
|---|
| 2781 |  | 
|---|
| 2782 | /* | 
|---|
| 2783 | * If the filesystem has aborted, it is read-only, so return | 
|---|
| 2784 | * right away instead of dumping stack traces later on that | 
|---|
| 2785 | * will obscure the real source of the problem.  We test | 
|---|
| 2786 | * fs shutdown state instead of sb->s_flag's SB_RDONLY because | 
|---|
| 2787 | * the latter could be true if the filesystem is mounted | 
|---|
| 2788 | * read-only, and in that case, ext4_writepages should | 
|---|
| 2789 | * *never* be called, so if that ever happens, we would want | 
|---|
| 2790 | * the stack trace. | 
|---|
| 2791 | */ | 
|---|
| 2792 | ret = ext4_emergency_state(sb: mapping->host->i_sb); | 
|---|
| 2793 | if (unlikely(ret)) | 
|---|
| 2794 | goto out_writepages; | 
|---|
| 2795 |  | 
|---|
| 2796 | /* | 
|---|
| 2797 | * If we have inline data and arrive here, it means that | 
|---|
| 2798 | * we will soon create the block for the 1st page, so | 
|---|
| 2799 | * we'd better clear the inline data here. | 
|---|
| 2800 | */ | 
|---|
| 2801 | if (ext4_has_inline_data(inode)) { | 
|---|
| 2802 | /* Just inode will be modified... */ | 
|---|
| 2803 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); | 
|---|
| 2804 | if (IS_ERR(ptr: handle)) { | 
|---|
| 2805 | ret = PTR_ERR(ptr: handle); | 
|---|
| 2806 | goto out_writepages; | 
|---|
| 2807 | } | 
|---|
| 2808 | BUG_ON(ext4_test_inode_state(inode, | 
|---|
| 2809 | EXT4_STATE_MAY_INLINE_DATA)); | 
|---|
| 2810 | ext4_destroy_inline_data(handle, inode); | 
|---|
| 2811 | ext4_journal_stop(handle); | 
|---|
| 2812 | } | 
|---|
| 2813 |  | 
|---|
| 2814 | /* | 
|---|
| 2815 | * data=journal mode does not do delalloc so we just need to writeout / | 
|---|
| 2816 | * journal already mapped buffers. On the other hand we need to commit | 
|---|
| 2817 | * transaction to make data stable. We expect all the data to be | 
|---|
| 2818 | * already in the journal (the only exception are DMA pinned pages | 
|---|
| 2819 | * dirtied behind our back) so we commit transaction here and run the | 
|---|
| 2820 | * writeback loop to checkpoint them. The checkpointing is not actually | 
|---|
| 2821 | * necessary to make data persistent *but* quite a few places (extent | 
|---|
| 2822 | * shifting operations, fsverity, ...) depend on being able to drop | 
|---|
| 2823 | * pagecache pages after calling filemap_write_and_wait() and for that | 
|---|
| 2824 | * checkpointing needs to happen. | 
|---|
| 2825 | */ | 
|---|
| 2826 | if (ext4_should_journal_data(inode)) { | 
|---|
| 2827 | mpd->can_map = 0; | 
|---|
| 2828 | if (wbc->sync_mode == WB_SYNC_ALL) | 
|---|
| 2829 | ext4_fc_commit(journal: sbi->s_journal, | 
|---|
| 2830 | EXT4_I(inode)->i_datasync_tid); | 
|---|
| 2831 | } | 
|---|
| 2832 | mpd->journalled_more_data = 0; | 
|---|
| 2833 |  | 
|---|
| 2834 | if (ext4_should_dioread_nolock(inode)) { | 
|---|
| 2835 | int bpf = ext4_journal_blocks_per_folio(inode); | 
|---|
| 2836 | /* | 
|---|
| 2837 | * We may need to convert up to one extent per block in | 
|---|
| 2838 | * the folio and we may dirty the inode. | 
|---|
| 2839 | */ | 
|---|
| 2840 | rsv_blocks = 1 + ext4_ext_index_trans_blocks(inode, extents: bpf); | 
|---|
| 2841 | } | 
|---|
| 2842 |  | 
|---|
| 2843 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) | 
|---|
| 2844 | range_whole = 1; | 
|---|
| 2845 |  | 
|---|
| 2846 | if (wbc->range_cyclic) { | 
|---|
| 2847 | writeback_index = mapping->writeback_index; | 
|---|
| 2848 | if (writeback_index) | 
|---|
| 2849 | cycled = 0; | 
|---|
| 2850 | mpd->start_pos = writeback_index << PAGE_SHIFT; | 
|---|
| 2851 | mpd->end_pos = LLONG_MAX; | 
|---|
| 2852 | } else { | 
|---|
| 2853 | mpd->start_pos = wbc->range_start; | 
|---|
| 2854 | mpd->end_pos = wbc->range_end; | 
|---|
| 2855 | } | 
|---|
| 2856 |  | 
|---|
| 2857 | ext4_io_submit_init(io: &mpd->io_submit, wbc); | 
|---|
| 2858 | retry: | 
|---|
| 2859 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) | 
|---|
| 2860 | tag_pages_for_writeback(mapping, start: mpd->start_pos >> PAGE_SHIFT, | 
|---|
| 2861 | end: mpd->end_pos >> PAGE_SHIFT); | 
|---|
| 2862 | blk_start_plug(&plug); | 
|---|
| 2863 |  | 
|---|
| 2864 | /* | 
|---|
| 2865 | * First writeback pages that don't need mapping - we can avoid | 
|---|
| 2866 | * starting a transaction unnecessarily and also avoid being blocked | 
|---|
| 2867 | * in the block layer on device congestion while having transaction | 
|---|
| 2868 | * started. | 
|---|
| 2869 | */ | 
|---|
| 2870 | mpd->do_map = 0; | 
|---|
| 2871 | mpd->scanned_until_end = 0; | 
|---|
| 2872 | mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); | 
|---|
| 2873 | if (!mpd->io_submit.io_end) { | 
|---|
| 2874 | ret = -ENOMEM; | 
|---|
| 2875 | goto unplug; | 
|---|
| 2876 | } | 
|---|
| 2877 | ret = mpage_prepare_extent_to_map(mpd); | 
|---|
| 2878 | /* Unlock pages we didn't use */ | 
|---|
| 2879 | mpage_release_unused_pages(mpd, invalidate: false); | 
|---|
| 2880 | /* Submit prepared bio */ | 
|---|
| 2881 | ext4_io_submit(io: &mpd->io_submit); | 
|---|
| 2882 | ext4_put_io_end_defer(io_end: mpd->io_submit.io_end); | 
|---|
| 2883 | mpd->io_submit.io_end = NULL; | 
|---|
| 2884 | if (ret < 0) | 
|---|
| 2885 | goto unplug; | 
|---|
| 2886 |  | 
|---|
| 2887 | while (!mpd->scanned_until_end && wbc->nr_to_write > 0) { | 
|---|
| 2888 | /* For each extent of pages we use new io_end */ | 
|---|
| 2889 | mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); | 
|---|
| 2890 | if (!mpd->io_submit.io_end) { | 
|---|
| 2891 | ret = -ENOMEM; | 
|---|
| 2892 | break; | 
|---|
| 2893 | } | 
|---|
| 2894 |  | 
|---|
| 2895 | WARN_ON_ONCE(!mpd->can_map); | 
|---|
| 2896 | /* | 
|---|
| 2897 | * We have two constraints: We find one extent to map and we | 
|---|
| 2898 | * must always write out whole page (makes a difference when | 
|---|
| 2899 | * blocksize < pagesize) so that we don't block on IO when we | 
|---|
| 2900 | * try to write out the rest of the page. Journalled mode is | 
|---|
| 2901 | * not supported by delalloc. | 
|---|
| 2902 | */ | 
|---|
| 2903 | BUG_ON(ext4_should_journal_data(inode)); | 
|---|
| 2904 | /* | 
|---|
| 2905 | * Calculate the number of credits needed to reserve for one | 
|---|
| 2906 | * extent of up to MAX_WRITEPAGES_EXTENT_LEN blocks. It will | 
|---|
| 2907 | * attempt to extend the transaction or start a new iteration | 
|---|
| 2908 | * if the reserved credits are insufficient. | 
|---|
| 2909 | */ | 
|---|
| 2910 | needed_blocks = ext4_chunk_trans_blocks(inode, | 
|---|
| 2911 | MAX_WRITEPAGES_EXTENT_LEN); | 
|---|
| 2912 | /* start a new transaction */ | 
|---|
| 2913 | handle = ext4_journal_start_with_reserve(inode, | 
|---|
| 2914 | EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); | 
|---|
| 2915 | if (IS_ERR(ptr: handle)) { | 
|---|
| 2916 | ret = PTR_ERR(ptr: handle); | 
|---|
| 2917 | ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " | 
|---|
| 2918 | "%ld pages, ino %lu; err %d", __func__, | 
|---|
| 2919 | wbc->nr_to_write, inode->i_ino, ret); | 
|---|
| 2920 | /* Release allocated io_end */ | 
|---|
| 2921 | ext4_put_io_end(io_end: mpd->io_submit.io_end); | 
|---|
| 2922 | mpd->io_submit.io_end = NULL; | 
|---|
| 2923 | break; | 
|---|
| 2924 | } | 
|---|
| 2925 | mpd->do_map = 1; | 
|---|
| 2926 |  | 
|---|
| 2927 | trace_ext4_da_write_folios_start(inode, start_pos: mpd->start_pos, | 
|---|
| 2928 | next_pos: mpd->next_pos, wbc); | 
|---|
| 2929 | ret = mpage_prepare_extent_to_map(mpd); | 
|---|
| 2930 | if (!ret && mpd->map.m_len) | 
|---|
| 2931 | ret = mpage_map_and_submit_extent(handle, mpd, | 
|---|
| 2932 | give_up_on_write: &give_up_on_write); | 
|---|
| 2933 | /* | 
|---|
| 2934 | * Caution: If the handle is synchronous, | 
|---|
| 2935 | * ext4_journal_stop() can wait for transaction commit | 
|---|
| 2936 | * to finish which may depend on writeback of pages to | 
|---|
| 2937 | * complete or on page lock to be released.  In that | 
|---|
| 2938 | * case, we have to wait until after we have | 
|---|
| 2939 | * submitted all the IO, released page locks we hold, | 
|---|
| 2940 | * and dropped io_end reference (for extent conversion | 
|---|
| 2941 | * to be able to complete) before stopping the handle. | 
|---|
| 2942 | */ | 
|---|
| 2943 | if (!ext4_handle_valid(handle) || handle->h_sync == 0) { | 
|---|
| 2944 | ext4_journal_stop(handle); | 
|---|
| 2945 | handle = NULL; | 
|---|
| 2946 | mpd->do_map = 0; | 
|---|
| 2947 | } | 
|---|
| 2948 | /* Unlock pages we didn't use */ | 
|---|
| 2949 | mpage_release_unused_pages(mpd, invalidate: give_up_on_write); | 
|---|
| 2950 | /* Submit prepared bio */ | 
|---|
| 2951 | ext4_io_submit(io: &mpd->io_submit); | 
|---|
| 2952 |  | 
|---|
| 2953 | /* | 
|---|
| 2954 | * Drop our io_end reference we got from init. We have | 
|---|
| 2955 | * to be careful and use deferred io_end finishing if | 
|---|
| 2956 | * we are still holding the transaction as we can | 
|---|
| 2957 | * release the last reference to io_end which may end | 
|---|
| 2958 | * up doing unwritten extent conversion. | 
|---|
| 2959 | */ | 
|---|
| 2960 | if (handle) { | 
|---|
| 2961 | ext4_put_io_end_defer(io_end: mpd->io_submit.io_end); | 
|---|
| 2962 | ext4_journal_stop(handle); | 
|---|
| 2963 | } else | 
|---|
| 2964 | ext4_put_io_end(io_end: mpd->io_submit.io_end); | 
|---|
| 2965 | mpd->io_submit.io_end = NULL; | 
|---|
| 2966 | trace_ext4_da_write_folios_end(inode, start_pos: mpd->start_pos, | 
|---|
| 2967 | next_pos: mpd->next_pos, wbc, ret); | 
|---|
| 2968 |  | 
|---|
| 2969 | if (ret == -ENOSPC && sbi->s_journal) { | 
|---|
| 2970 | /* | 
|---|
| 2971 | * Commit the transaction which would | 
|---|
| 2972 | * free blocks released in the transaction | 
|---|
| 2973 | * and try again | 
|---|
| 2974 | */ | 
|---|
| 2975 | jbd2_journal_force_commit_nested(sbi->s_journal); | 
|---|
| 2976 | ret = 0; | 
|---|
| 2977 | continue; | 
|---|
| 2978 | } | 
|---|
| 2979 | if (ret == -EAGAIN) | 
|---|
| 2980 | ret = 0; | 
|---|
| 2981 | /* Fatal error - ENOMEM, EIO... */ | 
|---|
| 2982 | if (ret) | 
|---|
| 2983 | break; | 
|---|
| 2984 | } | 
|---|
| 2985 | unplug: | 
|---|
| 2986 | blk_finish_plug(&plug); | 
|---|
| 2987 | if (!ret && !cycled && wbc->nr_to_write > 0) { | 
|---|
| 2988 | cycled = 1; | 
|---|
| 2989 | mpd->end_pos = (writeback_index << PAGE_SHIFT) - 1; | 
|---|
| 2990 | mpd->start_pos = 0; | 
|---|
| 2991 | goto retry; | 
|---|
| 2992 | } | 
|---|
| 2993 |  | 
|---|
| 2994 | /* Update index */ | 
|---|
| 2995 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) | 
|---|
| 2996 | /* | 
|---|
| 2997 | * Set the writeback_index so that range_cyclic | 
|---|
| 2998 | * mode will write it back later | 
|---|
| 2999 | */ | 
|---|
| 3000 | mapping->writeback_index = mpd->start_pos >> PAGE_SHIFT; | 
|---|
| 3001 |  | 
|---|
| 3002 | out_writepages: | 
|---|
| 3003 | trace_ext4_writepages_result(inode, wbc, ret, | 
|---|
| 3004 | pages_written: nr_to_write - wbc->nr_to_write); | 
|---|
| 3005 | return ret; | 
|---|
| 3006 | } | 
|---|
| 3007 |  | 
|---|
| 3008 | static int ext4_writepages(struct address_space *mapping, | 
|---|
| 3009 | struct writeback_control *wbc) | 
|---|
| 3010 | { | 
|---|
| 3011 | struct super_block *sb = mapping->host->i_sb; | 
|---|
| 3012 | struct mpage_da_data mpd = { | 
|---|
| 3013 | .inode = mapping->host, | 
|---|
| 3014 | .wbc = wbc, | 
|---|
| 3015 | .can_map = 1, | 
|---|
| 3016 | }; | 
|---|
| 3017 | int ret; | 
|---|
| 3018 | int alloc_ctx; | 
|---|
| 3019 |  | 
|---|
| 3020 | ret = ext4_emergency_state(sb); | 
|---|
| 3021 | if (unlikely(ret)) | 
|---|
| 3022 | return ret; | 
|---|
| 3023 |  | 
|---|
| 3024 | alloc_ctx = ext4_writepages_down_read(sb); | 
|---|
| 3025 | ret = ext4_do_writepages(mpd: &mpd); | 
|---|
| 3026 | /* | 
|---|
| 3027 | * For data=journal writeback we could have come across pages marked | 
|---|
| 3028 | * for delayed dirtying (PageChecked) which were just added to the | 
|---|
| 3029 | * running transaction. Try once more to get them to stable storage. | 
|---|
| 3030 | */ | 
|---|
| 3031 | if (!ret && mpd.journalled_more_data) | 
|---|
| 3032 | ret = ext4_do_writepages(mpd: &mpd); | 
|---|
| 3033 | ext4_writepages_up_read(sb, ctx: alloc_ctx); | 
|---|
| 3034 |  | 
|---|
| 3035 | return ret; | 
|---|
| 3036 | } | 
|---|
| 3037 |  | 
|---|
| 3038 | int ext4_normal_submit_inode_data_buffers(struct jbd2_inode *jinode) | 
|---|
| 3039 | { | 
|---|
| 3040 | struct writeback_control wbc = { | 
|---|
| 3041 | .sync_mode = WB_SYNC_ALL, | 
|---|
| 3042 | .nr_to_write = LONG_MAX, | 
|---|
| 3043 | .range_start = jinode->i_dirty_start, | 
|---|
| 3044 | .range_end = jinode->i_dirty_end, | 
|---|
| 3045 | }; | 
|---|
| 3046 | struct mpage_da_data mpd = { | 
|---|
| 3047 | .inode = jinode->i_vfs_inode, | 
|---|
| 3048 | .wbc = &wbc, | 
|---|
| 3049 | .can_map = 0, | 
|---|
| 3050 | }; | 
|---|
| 3051 | return ext4_do_writepages(mpd: &mpd); | 
|---|
| 3052 | } | 
|---|
| 3053 |  | 
|---|
| 3054 | static int ext4_dax_writepages(struct address_space *mapping, | 
|---|
| 3055 | struct writeback_control *wbc) | 
|---|
| 3056 | { | 
|---|
| 3057 | int ret; | 
|---|
| 3058 | long nr_to_write = wbc->nr_to_write; | 
|---|
| 3059 | struct inode *inode = mapping->host; | 
|---|
| 3060 | int alloc_ctx; | 
|---|
| 3061 |  | 
|---|
| 3062 | ret = ext4_emergency_state(sb: inode->i_sb); | 
|---|
| 3063 | if (unlikely(ret)) | 
|---|
| 3064 | return ret; | 
|---|
| 3065 |  | 
|---|
| 3066 | alloc_ctx = ext4_writepages_down_read(sb: inode->i_sb); | 
|---|
| 3067 | trace_ext4_writepages(inode, wbc); | 
|---|
| 3068 |  | 
|---|
| 3069 | ret = dax_writeback_mapping_range(mapping, | 
|---|
| 3070 | dax_dev: EXT4_SB(sb: inode->i_sb)->s_daxdev, wbc); | 
|---|
| 3071 | trace_ext4_writepages_result(inode, wbc, ret, | 
|---|
| 3072 | pages_written: nr_to_write - wbc->nr_to_write); | 
|---|
| 3073 | ext4_writepages_up_read(sb: inode->i_sb, ctx: alloc_ctx); | 
|---|
| 3074 | return ret; | 
|---|
| 3075 | } | 
|---|
| 3076 |  | 
|---|
| 3077 | static int ext4_nonda_switch(struct super_block *sb) | 
|---|
| 3078 | { | 
|---|
| 3079 | s64 free_clusters, dirty_clusters; | 
|---|
| 3080 | struct ext4_sb_info *sbi = EXT4_SB(sb); | 
|---|
| 3081 |  | 
|---|
| 3082 | /* | 
|---|
| 3083 | * switch to non delalloc mode if we are running low | 
|---|
| 3084 | * on free block. The free block accounting via percpu | 
|---|
| 3085 | * counters can get slightly wrong with percpu_counter_batch getting | 
|---|
| 3086 | * accumulated on each CPU without updating global counters | 
|---|
| 3087 | * Delalloc need an accurate free block accounting. So switch | 
|---|
| 3088 | * to non delalloc when we are near to error range. | 
|---|
| 3089 | */ | 
|---|
| 3090 | free_clusters = | 
|---|
| 3091 | percpu_counter_read_positive(fbc: &sbi->s_freeclusters_counter); | 
|---|
| 3092 | dirty_clusters = | 
|---|
| 3093 | percpu_counter_read_positive(fbc: &sbi->s_dirtyclusters_counter); | 
|---|
| 3094 | /* | 
|---|
| 3095 | * Start pushing delalloc when 1/2 of free blocks are dirty. | 
|---|
| 3096 | */ | 
|---|
| 3097 | if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) | 
|---|
| 3098 | try_to_writeback_inodes_sb(sb, reason: WB_REASON_FS_FREE_SPACE); | 
|---|
| 3099 |  | 
|---|
| 3100 | if (2 * free_clusters < 3 * dirty_clusters || | 
|---|
| 3101 | free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { | 
|---|
| 3102 | /* | 
|---|
| 3103 | * free block count is less than 150% of dirty blocks | 
|---|
| 3104 | * or free blocks is less than watermark | 
|---|
| 3105 | */ | 
|---|
| 3106 | return 1; | 
|---|
| 3107 | } | 
|---|
| 3108 | return 0; | 
|---|
| 3109 | } | 
|---|
| 3110 |  | 
|---|
| 3111 | static int ext4_da_write_begin(const struct kiocb *iocb, | 
|---|
| 3112 | struct address_space *mapping, | 
|---|
| 3113 | loff_t pos, unsigned len, | 
|---|
| 3114 | struct folio **foliop, void **fsdata) | 
|---|
| 3115 | { | 
|---|
| 3116 | int ret, retries = 0; | 
|---|
| 3117 | struct folio *folio; | 
|---|
| 3118 | pgoff_t index; | 
|---|
| 3119 | struct inode *inode = mapping->host; | 
|---|
| 3120 |  | 
|---|
| 3121 | ret = ext4_emergency_state(sb: inode->i_sb); | 
|---|
| 3122 | if (unlikely(ret)) | 
|---|
| 3123 | return ret; | 
|---|
| 3124 |  | 
|---|
| 3125 | index = pos >> PAGE_SHIFT; | 
|---|
| 3126 |  | 
|---|
| 3127 | if (ext4_nonda_switch(sb: inode->i_sb) || ext4_verity_in_progress(inode)) { | 
|---|
| 3128 | *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; | 
|---|
| 3129 | return ext4_write_begin(iocb, mapping, pos, | 
|---|
| 3130 | len, foliop, fsdata); | 
|---|
| 3131 | } | 
|---|
| 3132 | *fsdata = (void *)0; | 
|---|
| 3133 | trace_ext4_da_write_begin(inode, pos, len); | 
|---|
| 3134 |  | 
|---|
| 3135 | if (ext4_test_inode_state(inode, bit: EXT4_STATE_MAY_INLINE_DATA)) { | 
|---|
| 3136 | ret = ext4_generic_write_inline_data(mapping, inode, pos, len, | 
|---|
| 3137 | foliop, fsdata, da: true); | 
|---|
| 3138 | if (ret < 0) | 
|---|
| 3139 | return ret; | 
|---|
| 3140 | if (ret == 1) | 
|---|
| 3141 | return 0; | 
|---|
| 3142 | } | 
|---|
| 3143 |  | 
|---|
| 3144 | retry: | 
|---|
| 3145 | folio = write_begin_get_folio(iocb, mapping, index, len); | 
|---|
| 3146 | if (IS_ERR(ptr: folio)) | 
|---|
| 3147 | return PTR_ERR(ptr: folio); | 
|---|
| 3148 |  | 
|---|
| 3149 | if (pos + len > folio_pos(folio) + folio_size(folio)) | 
|---|
| 3150 | len = folio_pos(folio) + folio_size(folio) - pos; | 
|---|
| 3151 |  | 
|---|
| 3152 | ret = ext4_block_write_begin(NULL, folio, pos, len, | 
|---|
| 3153 | get_block: ext4_da_get_block_prep); | 
|---|
| 3154 | if (ret < 0) { | 
|---|
| 3155 | folio_unlock(folio); | 
|---|
| 3156 | folio_put(folio); | 
|---|
| 3157 | /* | 
|---|
| 3158 | * ext4_block_write_begin may have instantiated a few blocks | 
|---|
| 3159 | * outside i_size.  Trim these off again. Don't need | 
|---|
| 3160 | * i_size_read because we hold inode lock. | 
|---|
| 3161 | */ | 
|---|
| 3162 | if (pos + len > inode->i_size) | 
|---|
| 3163 | ext4_truncate_failed_write(inode); | 
|---|
| 3164 |  | 
|---|
| 3165 | if (ret == -ENOSPC && | 
|---|
| 3166 | ext4_should_retry_alloc(sb: inode->i_sb, retries: &retries)) | 
|---|
| 3167 | goto retry; | 
|---|
| 3168 | return ret; | 
|---|
| 3169 | } | 
|---|
| 3170 |  | 
|---|
| 3171 | *foliop = folio; | 
|---|
| 3172 | return ret; | 
|---|
| 3173 | } | 
|---|
| 3174 |  | 
|---|
| 3175 | /* | 
|---|
| 3176 | * Check if we should update i_disksize | 
|---|
| 3177 | * when write to the end of file but not require block allocation | 
|---|
| 3178 | */ | 
|---|
| 3179 | static int ext4_da_should_update_i_disksize(struct folio *folio, | 
|---|
| 3180 | unsigned long offset) | 
|---|
| 3181 | { | 
|---|
| 3182 | struct buffer_head *bh; | 
|---|
| 3183 | struct inode *inode = folio->mapping->host; | 
|---|
| 3184 | unsigned int idx; | 
|---|
| 3185 | int i; | 
|---|
| 3186 |  | 
|---|
| 3187 | bh = folio_buffers(folio); | 
|---|
| 3188 | idx = offset >> inode->i_blkbits; | 
|---|
| 3189 |  | 
|---|
| 3190 | for (i = 0; i < idx; i++) | 
|---|
| 3191 | bh = bh->b_this_page; | 
|---|
| 3192 |  | 
|---|
| 3193 | if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) | 
|---|
| 3194 | return 0; | 
|---|
| 3195 | return 1; | 
|---|
| 3196 | } | 
|---|
| 3197 |  | 
|---|
| 3198 | static int ext4_da_do_write_end(struct address_space *mapping, | 
|---|
| 3199 | loff_t pos, unsigned len, unsigned copied, | 
|---|
| 3200 | struct folio *folio) | 
|---|
| 3201 | { | 
|---|
| 3202 | struct inode *inode = mapping->host; | 
|---|
| 3203 | loff_t old_size = inode->i_size; | 
|---|
| 3204 | bool disksize_changed = false; | 
|---|
| 3205 | loff_t new_i_size, zero_len = 0; | 
|---|
| 3206 | handle_t *handle; | 
|---|
| 3207 |  | 
|---|
| 3208 | if (unlikely(!folio_buffers(folio))) { | 
|---|
| 3209 | folio_unlock(folio); | 
|---|
| 3210 | folio_put(folio); | 
|---|
| 3211 | return -EIO; | 
|---|
| 3212 | } | 
|---|
| 3213 | /* | 
|---|
| 3214 | * block_write_end() will mark the inode as dirty with I_DIRTY_PAGES | 
|---|
| 3215 | * flag, which all that's needed to trigger page writeback. | 
|---|
| 3216 | */ | 
|---|
| 3217 | copied = block_write_end(pos, len, copied, folio); | 
|---|
| 3218 | new_i_size = pos + copied; | 
|---|
| 3219 |  | 
|---|
| 3220 | /* | 
|---|
| 3221 | * It's important to update i_size while still holding folio lock, | 
|---|
| 3222 | * because folio writeout could otherwise come in and zero beyond | 
|---|
| 3223 | * i_size. | 
|---|
| 3224 | * | 
|---|
| 3225 | * Since we are holding inode lock, we are sure i_disksize <= | 
|---|
| 3226 | * i_size. We also know that if i_disksize < i_size, there are | 
|---|
| 3227 | * delalloc writes pending in the range up to i_size. If the end of | 
|---|
| 3228 | * the current write is <= i_size, there's no need to touch | 
|---|
| 3229 | * i_disksize since writeback will push i_disksize up to i_size | 
|---|
| 3230 | * eventually. If the end of the current write is > i_size and | 
|---|
| 3231 | * inside an allocated block which ext4_da_should_update_i_disksize() | 
|---|
| 3232 | * checked, we need to update i_disksize here as certain | 
|---|
| 3233 | * ext4_writepages() paths not allocating blocks and update i_disksize. | 
|---|
| 3234 | */ | 
|---|
| 3235 | if (new_i_size > inode->i_size) { | 
|---|
| 3236 | unsigned long end; | 
|---|
| 3237 |  | 
|---|
| 3238 | i_size_write(inode, i_size: new_i_size); | 
|---|
| 3239 | end = offset_in_folio(folio, new_i_size - 1); | 
|---|
| 3240 | if (copied && ext4_da_should_update_i_disksize(folio, offset: end)) { | 
|---|
| 3241 | ext4_update_i_disksize(inode, newsize: new_i_size); | 
|---|
| 3242 | disksize_changed = true; | 
|---|
| 3243 | } | 
|---|
| 3244 | } | 
|---|
| 3245 |  | 
|---|
| 3246 | folio_unlock(folio); | 
|---|
| 3247 | folio_put(folio); | 
|---|
| 3248 |  | 
|---|
| 3249 | if (pos > old_size) { | 
|---|
| 3250 | pagecache_isize_extended(inode, from: old_size, to: pos); | 
|---|
| 3251 | zero_len = pos - old_size; | 
|---|
| 3252 | } | 
|---|
| 3253 |  | 
|---|
| 3254 | if (!disksize_changed && !zero_len) | 
|---|
| 3255 | return copied; | 
|---|
| 3256 |  | 
|---|
| 3257 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); | 
|---|
| 3258 | if (IS_ERR(ptr: handle)) | 
|---|
| 3259 | return PTR_ERR(ptr: handle); | 
|---|
| 3260 | if (zero_len) | 
|---|
| 3261 | ext4_zero_partial_blocks(handle, inode, lstart: old_size, lend: zero_len); | 
|---|
| 3262 | ext4_mark_inode_dirty(handle, inode); | 
|---|
| 3263 | ext4_journal_stop(handle); | 
|---|
| 3264 |  | 
|---|
| 3265 | return copied; | 
|---|
| 3266 | } | 
|---|
| 3267 |  | 
|---|
| 3268 | static int ext4_da_write_end(const struct kiocb *iocb, | 
|---|
| 3269 | struct address_space *mapping, | 
|---|
| 3270 | loff_t pos, unsigned len, unsigned copied, | 
|---|
| 3271 | struct folio *folio, void *fsdata) | 
|---|
| 3272 | { | 
|---|
| 3273 | struct inode *inode = mapping->host; | 
|---|
| 3274 | int write_mode = (int)(unsigned long)fsdata; | 
|---|
| 3275 |  | 
|---|
| 3276 | if (write_mode == FALL_BACK_TO_NONDELALLOC) | 
|---|
| 3277 | return ext4_write_end(iocb, mapping, pos, | 
|---|
| 3278 | len, copied, folio, fsdata); | 
|---|
| 3279 |  | 
|---|
| 3280 | trace_ext4_da_write_end(inode, pos, len, copied); | 
|---|
| 3281 |  | 
|---|
| 3282 | if (write_mode != CONVERT_INLINE_DATA && | 
|---|
| 3283 | ext4_test_inode_state(inode, bit: EXT4_STATE_MAY_INLINE_DATA) && | 
|---|
| 3284 | ext4_has_inline_data(inode)) | 
|---|
| 3285 | return ext4_write_inline_data_end(inode, pos, len, copied, | 
|---|
| 3286 | folio); | 
|---|
| 3287 |  | 
|---|
| 3288 | if (unlikely(copied < len) && !folio_test_uptodate(folio)) | 
|---|
| 3289 | copied = 0; | 
|---|
| 3290 |  | 
|---|
| 3291 | return ext4_da_do_write_end(mapping, pos, len, copied, folio); | 
|---|
| 3292 | } | 
|---|
| 3293 |  | 
|---|
| 3294 | /* | 
|---|
| 3295 | * Force all delayed allocation blocks to be allocated for a given inode. | 
|---|
| 3296 | */ | 
|---|
| 3297 | int ext4_alloc_da_blocks(struct inode *inode) | 
|---|
| 3298 | { | 
|---|
| 3299 | trace_ext4_alloc_da_blocks(inode); | 
|---|
| 3300 |  | 
|---|
| 3301 | if (!EXT4_I(inode)->i_reserved_data_blocks) | 
|---|
| 3302 | return 0; | 
|---|
| 3303 |  | 
|---|
| 3304 | /* | 
|---|
| 3305 | * We do something simple for now.  The filemap_flush() will | 
|---|
| 3306 | * also start triggering a write of the data blocks, which is | 
|---|
| 3307 | * not strictly speaking necessary (and for users of | 
|---|
| 3308 | * laptop_mode, not even desirable).  However, to do otherwise | 
|---|
| 3309 | * would require replicating code paths in: | 
|---|
| 3310 | * | 
|---|
| 3311 | * ext4_writepages() -> | 
|---|
| 3312 | *    write_cache_pages() ---> (via passed in callback function) | 
|---|
| 3313 | *        __mpage_da_writepage() --> | 
|---|
| 3314 | *           mpage_add_bh_to_extent() | 
|---|
| 3315 | *           mpage_da_map_blocks() | 
|---|
| 3316 | * | 
|---|
| 3317 | * The problem is that write_cache_pages(), located in | 
|---|
| 3318 | * mm/page-writeback.c, marks pages clean in preparation for | 
|---|
| 3319 | * doing I/O, which is not desirable if we're not planning on | 
|---|
| 3320 | * doing I/O at all. | 
|---|
| 3321 | * | 
|---|
| 3322 | * We could call write_cache_pages(), and then redirty all of | 
|---|
| 3323 | * the pages by calling redirty_page_for_writepage() but that | 
|---|
| 3324 | * would be ugly in the extreme.  So instead we would need to | 
|---|
| 3325 | * replicate parts of the code in the above functions, | 
|---|
| 3326 | * simplifying them because we wouldn't actually intend to | 
|---|
| 3327 | * write out the pages, but rather only collect contiguous | 
|---|
| 3328 | * logical block extents, call the multi-block allocator, and | 
|---|
| 3329 | * then update the buffer heads with the block allocations. | 
|---|
| 3330 | * | 
|---|
| 3331 | * For now, though, we'll cheat by calling filemap_flush(), | 
|---|
| 3332 | * which will map the blocks, and start the I/O, but not | 
|---|
| 3333 | * actually wait for the I/O to complete. | 
|---|
| 3334 | */ | 
|---|
| 3335 | return filemap_flush(inode->i_mapping); | 
|---|
| 3336 | } | 
|---|
| 3337 |  | 
|---|
| 3338 | /* | 
|---|
| 3339 | * bmap() is special.  It gets used by applications such as lilo and by | 
|---|
| 3340 | * the swapper to find the on-disk block of a specific piece of data. | 
|---|
| 3341 | * | 
|---|
| 3342 | * Naturally, this is dangerous if the block concerned is still in the | 
|---|
| 3343 | * journal.  If somebody makes a swapfile on an ext4 data-journaling | 
|---|
| 3344 | * filesystem and enables swap, then they may get a nasty shock when the | 
|---|
| 3345 | * data getting swapped to that swapfile suddenly gets overwritten by | 
|---|
| 3346 | * the original zero's written out previously to the journal and | 
|---|
| 3347 | * awaiting writeback in the kernel's buffer cache. | 
|---|
| 3348 | * | 
|---|
| 3349 | * So, if we see any bmap calls here on a modified, data-journaled file, | 
|---|
| 3350 | * take extra steps to flush any blocks which might be in the cache. | 
|---|
| 3351 | */ | 
|---|
| 3352 | static sector_t ext4_bmap(struct address_space *mapping, sector_t block) | 
|---|
| 3353 | { | 
|---|
| 3354 | struct inode *inode = mapping->host; | 
|---|
| 3355 | sector_t ret = 0; | 
|---|
| 3356 |  | 
|---|
| 3357 | inode_lock_shared(inode); | 
|---|
| 3358 | /* | 
|---|
| 3359 | * We can get here for an inline file via the FIBMAP ioctl | 
|---|
| 3360 | */ | 
|---|
| 3361 | if (ext4_has_inline_data(inode)) | 
|---|
| 3362 | goto out; | 
|---|
| 3363 |  | 
|---|
| 3364 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && | 
|---|
| 3365 | (test_opt(inode->i_sb, DELALLOC) || | 
|---|
| 3366 | ext4_should_journal_data(inode))) { | 
|---|
| 3367 | /* | 
|---|
| 3368 | * With delalloc or journalled data we want to sync the file so | 
|---|
| 3369 | * that we can make sure we allocate blocks for file and data | 
|---|
| 3370 | * is in place for the user to see it | 
|---|
| 3371 | */ | 
|---|
| 3372 | filemap_write_and_wait(mapping); | 
|---|
| 3373 | } | 
|---|
| 3374 |  | 
|---|
| 3375 | ret = iomap_bmap(mapping, bno: block, ops: &ext4_iomap_ops); | 
|---|
| 3376 |  | 
|---|
| 3377 | out: | 
|---|
| 3378 | inode_unlock_shared(inode); | 
|---|
| 3379 | return ret; | 
|---|
| 3380 | } | 
|---|
| 3381 |  | 
|---|
| 3382 | static int ext4_read_folio(struct file *file, struct folio *folio) | 
|---|
| 3383 | { | 
|---|
| 3384 | int ret = -EAGAIN; | 
|---|
| 3385 | struct inode *inode = folio->mapping->host; | 
|---|
| 3386 |  | 
|---|
| 3387 | trace_ext4_read_folio(inode, folio); | 
|---|
| 3388 |  | 
|---|
| 3389 | if (ext4_has_inline_data(inode)) | 
|---|
| 3390 | ret = ext4_readpage_inline(inode, folio); | 
|---|
| 3391 |  | 
|---|
| 3392 | if (ret == -EAGAIN) | 
|---|
| 3393 | return ext4_mpage_readpages(inode, NULL, folio); | 
|---|
| 3394 |  | 
|---|
| 3395 | return ret; | 
|---|
| 3396 | } | 
|---|
| 3397 |  | 
|---|
| 3398 | static void ext4_readahead(struct readahead_control *rac) | 
|---|
| 3399 | { | 
|---|
| 3400 | struct inode *inode = rac->mapping->host; | 
|---|
| 3401 |  | 
|---|
| 3402 | /* If the file has inline data, no need to do readahead. */ | 
|---|
| 3403 | if (ext4_has_inline_data(inode)) | 
|---|
| 3404 | return; | 
|---|
| 3405 |  | 
|---|
| 3406 | ext4_mpage_readpages(inode, rac, NULL); | 
|---|
| 3407 | } | 
|---|
| 3408 |  | 
|---|
| 3409 | static void ext4_invalidate_folio(struct folio *folio, size_t offset, | 
|---|
| 3410 | size_t length) | 
|---|
| 3411 | { | 
|---|
| 3412 | trace_ext4_invalidate_folio(folio, offset, length); | 
|---|
| 3413 |  | 
|---|
| 3414 | /* No journalling happens on data buffers when this function is used */ | 
|---|
| 3415 | WARN_ON(folio_buffers(folio) && buffer_jbd(folio_buffers(folio))); | 
|---|
| 3416 |  | 
|---|
| 3417 | block_invalidate_folio(folio, offset, length); | 
|---|
| 3418 | } | 
|---|
| 3419 |  | 
|---|
| 3420 | static int __ext4_journalled_invalidate_folio(struct folio *folio, | 
|---|
| 3421 | size_t offset, size_t length) | 
|---|
| 3422 | { | 
|---|
| 3423 | journal_t *journal = EXT4_JOURNAL(folio->mapping->host); | 
|---|
| 3424 |  | 
|---|
| 3425 | trace_ext4_journalled_invalidate_folio(folio, offset, length); | 
|---|
| 3426 |  | 
|---|
| 3427 | /* | 
|---|
| 3428 | * If it's a full truncate we just forget about the pending dirtying | 
|---|
| 3429 | */ | 
|---|
| 3430 | if (offset == 0 && length == folio_size(folio)) | 
|---|
| 3431 | folio_clear_checked(folio); | 
|---|
| 3432 |  | 
|---|
| 3433 | return jbd2_journal_invalidate_folio(journal, folio, offset, length); | 
|---|
| 3434 | } | 
|---|
| 3435 |  | 
|---|
| 3436 | /* Wrapper for aops... */ | 
|---|
| 3437 | static void ext4_journalled_invalidate_folio(struct folio *folio, | 
|---|
| 3438 | size_t offset, | 
|---|
| 3439 | size_t length) | 
|---|
| 3440 | { | 
|---|
| 3441 | WARN_ON(__ext4_journalled_invalidate_folio(folio, offset, length) < 0); | 
|---|
| 3442 | } | 
|---|
| 3443 |  | 
|---|
| 3444 | static bool ext4_release_folio(struct folio *folio, gfp_t wait) | 
|---|
| 3445 | { | 
|---|
| 3446 | struct inode *inode = folio->mapping->host; | 
|---|
| 3447 | journal_t *journal = EXT4_JOURNAL(inode); | 
|---|
| 3448 |  | 
|---|
| 3449 | trace_ext4_release_folio(inode, folio); | 
|---|
| 3450 |  | 
|---|
| 3451 | /* Page has dirty journalled data -> cannot release */ | 
|---|
| 3452 | if (folio_test_checked(folio)) | 
|---|
| 3453 | return false; | 
|---|
| 3454 | if (journal) | 
|---|
| 3455 | return jbd2_journal_try_to_free_buffers(journal, folio); | 
|---|
| 3456 | else | 
|---|
| 3457 | return try_to_free_buffers(folio); | 
|---|
| 3458 | } | 
|---|
| 3459 |  | 
|---|
| 3460 | static bool ext4_inode_datasync_dirty(struct inode *inode) | 
|---|
| 3461 | { | 
|---|
| 3462 | journal_t *journal = EXT4_SB(sb: inode->i_sb)->s_journal; | 
|---|
| 3463 |  | 
|---|
| 3464 | if (journal) { | 
|---|
| 3465 | if (jbd2_transaction_committed(journal, | 
|---|
| 3466 | EXT4_I(inode)->i_datasync_tid)) | 
|---|
| 3467 | return false; | 
|---|
| 3468 | if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT)) | 
|---|
| 3469 | return !list_empty(head: &EXT4_I(inode)->i_fc_list); | 
|---|
| 3470 | return true; | 
|---|
| 3471 | } | 
|---|
| 3472 |  | 
|---|
| 3473 | /* Any metadata buffers to write? */ | 
|---|
| 3474 | if (!list_empty(head: &inode->i_mapping->i_private_list)) | 
|---|
| 3475 | return true; | 
|---|
| 3476 | return inode->i_state & I_DIRTY_DATASYNC; | 
|---|
| 3477 | } | 
|---|
| 3478 |  | 
|---|
| 3479 | static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, | 
|---|
| 3480 | struct ext4_map_blocks *map, loff_t offset, | 
|---|
| 3481 | loff_t length, unsigned int flags) | 
|---|
| 3482 | { | 
|---|
| 3483 | u8 blkbits = inode->i_blkbits; | 
|---|
| 3484 |  | 
|---|
| 3485 | /* | 
|---|
| 3486 | * Writes that span EOF might trigger an I/O size update on completion, | 
|---|
| 3487 | * so consider them to be dirty for the purpose of O_DSYNC, even if | 
|---|
| 3488 | * there is no other metadata changes being made or are pending. | 
|---|
| 3489 | */ | 
|---|
| 3490 | iomap->flags = 0; | 
|---|
| 3491 | if (ext4_inode_datasync_dirty(inode) || | 
|---|
| 3492 | offset + length > i_size_read(inode)) | 
|---|
| 3493 | iomap->flags |= IOMAP_F_DIRTY; | 
|---|
| 3494 |  | 
|---|
| 3495 | if (map->m_flags & EXT4_MAP_NEW) | 
|---|
| 3496 | iomap->flags |= IOMAP_F_NEW; | 
|---|
| 3497 |  | 
|---|
| 3498 | /* HW-offload atomics are always used */ | 
|---|
| 3499 | if (flags & IOMAP_ATOMIC) | 
|---|
| 3500 | iomap->flags |= IOMAP_F_ATOMIC_BIO; | 
|---|
| 3501 |  | 
|---|
| 3502 | if (flags & IOMAP_DAX) | 
|---|
| 3503 | iomap->dax_dev = EXT4_SB(sb: inode->i_sb)->s_daxdev; | 
|---|
| 3504 | else | 
|---|
| 3505 | iomap->bdev = inode->i_sb->s_bdev; | 
|---|
| 3506 | iomap->offset = (u64) map->m_lblk << blkbits; | 
|---|
| 3507 | iomap->length = (u64) map->m_len << blkbits; | 
|---|
| 3508 |  | 
|---|
| 3509 | if ((map->m_flags & EXT4_MAP_MAPPED) && | 
|---|
| 3510 | !ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS)) | 
|---|
| 3511 | iomap->flags |= IOMAP_F_MERGED; | 
|---|
| 3512 |  | 
|---|
| 3513 | /* | 
|---|
| 3514 | * Flags passed to ext4_map_blocks() for direct I/O writes can result | 
|---|
| 3515 | * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits | 
|---|
| 3516 | * set. In order for any allocated unwritten extents to be converted | 
|---|
| 3517 | * into written extents correctly within the ->end_io() handler, we | 
|---|
| 3518 | * need to ensure that the iomap->type is set appropriately. Hence, the | 
|---|
| 3519 | * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has | 
|---|
| 3520 | * been set first. | 
|---|
| 3521 | */ | 
|---|
| 3522 | if (map->m_flags & EXT4_MAP_UNWRITTEN) { | 
|---|
| 3523 | iomap->type = IOMAP_UNWRITTEN; | 
|---|
| 3524 | iomap->addr = (u64) map->m_pblk << blkbits; | 
|---|
| 3525 | if (flags & IOMAP_DAX) | 
|---|
| 3526 | iomap->addr += EXT4_SB(sb: inode->i_sb)->s_dax_part_off; | 
|---|
| 3527 | } else if (map->m_flags & EXT4_MAP_MAPPED) { | 
|---|
| 3528 | iomap->type = IOMAP_MAPPED; | 
|---|
| 3529 | iomap->addr = (u64) map->m_pblk << blkbits; | 
|---|
| 3530 | if (flags & IOMAP_DAX) | 
|---|
| 3531 | iomap->addr += EXT4_SB(sb: inode->i_sb)->s_dax_part_off; | 
|---|
| 3532 | } else if (map->m_flags & EXT4_MAP_DELAYED) { | 
|---|
| 3533 | iomap->type = IOMAP_DELALLOC; | 
|---|
| 3534 | iomap->addr = IOMAP_NULL_ADDR; | 
|---|
| 3535 | } else { | 
|---|
| 3536 | iomap->type = IOMAP_HOLE; | 
|---|
| 3537 | iomap->addr = IOMAP_NULL_ADDR; | 
|---|
| 3538 | } | 
|---|
| 3539 | } | 
|---|
| 3540 |  | 
|---|
| 3541 | static int ext4_map_blocks_atomic_write_slow(handle_t *handle, | 
|---|
| 3542 | struct inode *inode, struct ext4_map_blocks *map) | 
|---|
| 3543 | { | 
|---|
| 3544 | ext4_lblk_t m_lblk = map->m_lblk; | 
|---|
| 3545 | unsigned int m_len = map->m_len; | 
|---|
| 3546 | unsigned int mapped_len = 0, m_flags = 0; | 
|---|
| 3547 | ext4_fsblk_t next_pblk; | 
|---|
| 3548 | bool check_next_pblk = false; | 
|---|
| 3549 | int ret = 0; | 
|---|
| 3550 |  | 
|---|
| 3551 | WARN_ON_ONCE(!ext4_has_feature_bigalloc(inode->i_sb)); | 
|---|
| 3552 |  | 
|---|
| 3553 | /* | 
|---|
| 3554 | * This is a slow path in case of mixed mapping. We use | 
|---|
| 3555 | * EXT4_GET_BLOCKS_CREATE_ZERO flag here to make sure we get a single | 
|---|
| 3556 | * contiguous mapped mapping. This will ensure any unwritten or hole | 
|---|
| 3557 | * regions within the requested range is zeroed out and we return | 
|---|
| 3558 | * a single contiguous mapped extent. | 
|---|
| 3559 | */ | 
|---|
| 3560 | m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; | 
|---|
| 3561 |  | 
|---|
| 3562 | do { | 
|---|
| 3563 | ret = ext4_map_blocks(handle, inode, map, flags: m_flags); | 
|---|
| 3564 | if (ret < 0 && ret != -ENOSPC) | 
|---|
| 3565 | goto out_err; | 
|---|
| 3566 | /* | 
|---|
| 3567 | * This should never happen, but let's return an error code to | 
|---|
| 3568 | * avoid an infinite loop in here. | 
|---|
| 3569 | */ | 
|---|
| 3570 | if (ret == 0) { | 
|---|
| 3571 | ret = -EFSCORRUPTED; | 
|---|
| 3572 | ext4_warning_inode(inode, | 
|---|
| 3573 | "ext4_map_blocks() couldn't allocate blocks m_flags: 0x%x, ret:%d", | 
|---|
| 3574 | m_flags, ret); | 
|---|
| 3575 | goto out_err; | 
|---|
| 3576 | } | 
|---|
| 3577 | /* | 
|---|
| 3578 | * With bigalloc we should never get ENOSPC nor discontiguous | 
|---|
| 3579 | * physical extents. | 
|---|
| 3580 | */ | 
|---|
| 3581 | if ((check_next_pblk && next_pblk != map->m_pblk) || | 
|---|
| 3582 | ret == -ENOSPC) { | 
|---|
| 3583 | ext4_warning_inode(inode, | 
|---|
| 3584 | "Non-contiguous allocation detected: expected %llu, got %llu, " | 
|---|
| 3585 | "or ext4_map_blocks() returned out of space ret: %d", | 
|---|
| 3586 | next_pblk, map->m_pblk, ret); | 
|---|
| 3587 | ret = -EFSCORRUPTED; | 
|---|
| 3588 | goto out_err; | 
|---|
| 3589 | } | 
|---|
| 3590 | next_pblk = map->m_pblk + map->m_len; | 
|---|
| 3591 | check_next_pblk = true; | 
|---|
| 3592 |  | 
|---|
| 3593 | mapped_len += map->m_len; | 
|---|
| 3594 | map->m_lblk += map->m_len; | 
|---|
| 3595 | map->m_len = m_len - mapped_len; | 
|---|
| 3596 | } while (mapped_len < m_len); | 
|---|
| 3597 |  | 
|---|
| 3598 | /* | 
|---|
| 3599 | * We might have done some work in above loop, so we need to query the | 
|---|
| 3600 | * start of the physical extent, based on the origin m_lblk and m_len. | 
|---|
| 3601 | * Let's also ensure we were able to allocate the required range for | 
|---|
| 3602 | * mixed mapping case. | 
|---|
| 3603 | */ | 
|---|
| 3604 | map->m_lblk = m_lblk; | 
|---|
| 3605 | map->m_len = m_len; | 
|---|
| 3606 | map->m_flags = 0; | 
|---|
| 3607 |  | 
|---|
| 3608 | ret = ext4_map_blocks(handle, inode, map, | 
|---|
| 3609 | EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF); | 
|---|
| 3610 | if (ret != m_len) { | 
|---|
| 3611 | ext4_warning_inode(inode, | 
|---|
| 3612 | "allocation failed for atomic write request m_lblk:%u, m_len:%u, ret:%d\n", | 
|---|
| 3613 | m_lblk, m_len, ret); | 
|---|
| 3614 | ret = -EINVAL; | 
|---|
| 3615 | } | 
|---|
| 3616 | return ret; | 
|---|
| 3617 |  | 
|---|
| 3618 | out_err: | 
|---|
| 3619 | /* reset map before returning an error */ | 
|---|
| 3620 | map->m_lblk = m_lblk; | 
|---|
| 3621 | map->m_len = m_len; | 
|---|
| 3622 | map->m_flags = 0; | 
|---|
| 3623 | return ret; | 
|---|
| 3624 | } | 
|---|
| 3625 |  | 
|---|
| 3626 | /* | 
|---|
| 3627 | * ext4_map_blocks_atomic: Helper routine to ensure the entire requested | 
|---|
| 3628 | * range in @map [lblk, lblk + len) is one single contiguous extent with no | 
|---|
| 3629 | * mixed mappings. | 
|---|
| 3630 | * | 
|---|
| 3631 | * We first use m_flags passed to us by our caller (ext4_iomap_alloc()). | 
|---|
| 3632 | * We only call EXT4_GET_BLOCKS_ZERO in the slow path, when the underlying | 
|---|
| 3633 | * physical extent for the requested range does not have a single contiguous | 
|---|
| 3634 | * mapping type i.e. (Hole, Mapped, or Unwritten) throughout. | 
|---|
| 3635 | * In that case we will loop over the requested range to allocate and zero out | 
|---|
| 3636 | * the unwritten / holes in between, to get a single mapped extent from | 
|---|
| 3637 | * [m_lblk, m_lblk +  m_len). Note that this is only possible because we know | 
|---|
| 3638 | * this can be called only with bigalloc enabled filesystem where the underlying | 
|---|
| 3639 | * cluster is already allocated. This avoids allocating discontiguous extents | 
|---|
| 3640 | * in the slow path due to multiple calls to ext4_map_blocks(). | 
|---|
| 3641 | * The slow path is mostly non-performance critical path, so it should be ok to | 
|---|
| 3642 | * loop using ext4_map_blocks() with appropriate flags to allocate & zero the | 
|---|
| 3643 | * underlying short holes/unwritten extents within the requested range. | 
|---|
| 3644 | */ | 
|---|
| 3645 | static int ext4_map_blocks_atomic_write(handle_t *handle, struct inode *inode, | 
|---|
| 3646 | struct ext4_map_blocks *map, int m_flags, | 
|---|
| 3647 | bool *force_commit) | 
|---|
| 3648 | { | 
|---|
| 3649 | ext4_lblk_t m_lblk = map->m_lblk; | 
|---|
| 3650 | unsigned int m_len = map->m_len; | 
|---|
| 3651 | int ret = 0; | 
|---|
| 3652 |  | 
|---|
| 3653 | WARN_ON_ONCE(m_len > 1 && !ext4_has_feature_bigalloc(inode->i_sb)); | 
|---|
| 3654 |  | 
|---|
| 3655 | ret = ext4_map_blocks(handle, inode, map, flags: m_flags); | 
|---|
| 3656 | if (ret < 0 || ret == m_len) | 
|---|
| 3657 | goto out; | 
|---|
| 3658 | /* | 
|---|
| 3659 | * This is a mixed mapping case where we were not able to allocate | 
|---|
| 3660 | * a single contiguous extent. In that case let's reset requested | 
|---|
| 3661 | * mapping and call the slow path. | 
|---|
| 3662 | */ | 
|---|
| 3663 | map->m_lblk = m_lblk; | 
|---|
| 3664 | map->m_len = m_len; | 
|---|
| 3665 | map->m_flags = 0; | 
|---|
| 3666 |  | 
|---|
| 3667 | /* | 
|---|
| 3668 | * slow path means we have mixed mapping, that means we will need | 
|---|
| 3669 | * to force txn commit. | 
|---|
| 3670 | */ | 
|---|
| 3671 | *force_commit = true; | 
|---|
| 3672 | return ext4_map_blocks_atomic_write_slow(handle, inode, map); | 
|---|
| 3673 | out: | 
|---|
| 3674 | return ret; | 
|---|
| 3675 | } | 
|---|
| 3676 |  | 
|---|
| 3677 | static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, | 
|---|
| 3678 | unsigned int flags) | 
|---|
| 3679 | { | 
|---|
| 3680 | handle_t *handle; | 
|---|
| 3681 | u8 blkbits = inode->i_blkbits; | 
|---|
| 3682 | int ret, dio_credits, m_flags = 0, retries = 0; | 
|---|
| 3683 | bool force_commit = false; | 
|---|
| 3684 |  | 
|---|
| 3685 | /* | 
|---|
| 3686 | * Trim the mapping request to the maximum value that we can map at | 
|---|
| 3687 | * once for direct I/O. | 
|---|
| 3688 | */ | 
|---|
| 3689 | if (map->m_len > DIO_MAX_BLOCKS) | 
|---|
| 3690 | map->m_len = DIO_MAX_BLOCKS; | 
|---|
| 3691 |  | 
|---|
| 3692 | /* | 
|---|
| 3693 | * journal credits estimation for atomic writes. We call | 
|---|
| 3694 | * ext4_map_blocks(), to find if there could be a mixed mapping. If yes, | 
|---|
| 3695 | * then let's assume the no. of pextents required can be m_len i.e. | 
|---|
| 3696 | * every alternate block can be unwritten and hole. | 
|---|
| 3697 | */ | 
|---|
| 3698 | if (flags & IOMAP_ATOMIC) { | 
|---|
| 3699 | unsigned int orig_mlen = map->m_len; | 
|---|
| 3700 |  | 
|---|
| 3701 | ret = ext4_map_blocks(NULL, inode, map, flags: 0); | 
|---|
| 3702 | if (ret < 0) | 
|---|
| 3703 | return ret; | 
|---|
| 3704 | if (map->m_len < orig_mlen) { | 
|---|
| 3705 | map->m_len = orig_mlen; | 
|---|
| 3706 | dio_credits = ext4_meta_trans_blocks(inode, lblocks: orig_mlen, | 
|---|
| 3707 | pextents: map->m_len); | 
|---|
| 3708 | } else { | 
|---|
| 3709 | dio_credits = ext4_chunk_trans_blocks(inode, | 
|---|
| 3710 | nrblocks: map->m_len); | 
|---|
| 3711 | } | 
|---|
| 3712 | } else { | 
|---|
| 3713 | dio_credits = ext4_chunk_trans_blocks(inode, nrblocks: map->m_len); | 
|---|
| 3714 | } | 
|---|
| 3715 |  | 
|---|
| 3716 | retry: | 
|---|
| 3717 | /* | 
|---|
| 3718 | * Either we allocate blocks and then don't get an unwritten extent, so | 
|---|
| 3719 | * in that case we have reserved enough credits. Or, the blocks are | 
|---|
| 3720 | * already allocated and unwritten. In that case, the extent conversion | 
|---|
| 3721 | * fits into the credits as well. | 
|---|
| 3722 | */ | 
|---|
| 3723 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); | 
|---|
| 3724 | if (IS_ERR(ptr: handle)) | 
|---|
| 3725 | return PTR_ERR(ptr: handle); | 
|---|
| 3726 |  | 
|---|
| 3727 | /* | 
|---|
| 3728 | * DAX and direct I/O are the only two operations that are currently | 
|---|
| 3729 | * supported with IOMAP_WRITE. | 
|---|
| 3730 | */ | 
|---|
| 3731 | WARN_ON(!(flags & (IOMAP_DAX | IOMAP_DIRECT))); | 
|---|
| 3732 | if (flags & IOMAP_DAX) | 
|---|
| 3733 | m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; | 
|---|
| 3734 | /* | 
|---|
| 3735 | * We use i_size instead of i_disksize here because delalloc writeback | 
|---|
| 3736 | * can complete at any point during the I/O and subsequently push the | 
|---|
| 3737 | * i_disksize out to i_size. This could be beyond where direct I/O is | 
|---|
| 3738 | * happening and thus expose allocated blocks to direct I/O reads. | 
|---|
| 3739 | */ | 
|---|
| 3740 | else if (((loff_t)map->m_lblk << blkbits) >= i_size_read(inode)) | 
|---|
| 3741 | m_flags = EXT4_GET_BLOCKS_CREATE; | 
|---|
| 3742 | else if (ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS)) | 
|---|
| 3743 | m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; | 
|---|
| 3744 |  | 
|---|
| 3745 | if (flags & IOMAP_ATOMIC) | 
|---|
| 3746 | ret = ext4_map_blocks_atomic_write(handle, inode, map, m_flags, | 
|---|
| 3747 | force_commit: &force_commit); | 
|---|
| 3748 | else | 
|---|
| 3749 | ret = ext4_map_blocks(handle, inode, map, flags: m_flags); | 
|---|
| 3750 |  | 
|---|
| 3751 | /* | 
|---|
| 3752 | * We cannot fill holes in indirect tree based inodes as that could | 
|---|
| 3753 | * expose stale data in the case of a crash. Use the magic error code | 
|---|
| 3754 | * to fallback to buffered I/O. | 
|---|
| 3755 | */ | 
|---|
| 3756 | if (!m_flags && !ret) | 
|---|
| 3757 | ret = -ENOTBLK; | 
|---|
| 3758 |  | 
|---|
| 3759 | ext4_journal_stop(handle); | 
|---|
| 3760 | if (ret == -ENOSPC && ext4_should_retry_alloc(sb: inode->i_sb, retries: &retries)) | 
|---|
| 3761 | goto retry; | 
|---|
| 3762 |  | 
|---|
| 3763 | /* | 
|---|
| 3764 | * Force commit the current transaction if the allocation spans a mixed | 
|---|
| 3765 | * mapping range. This ensures any pending metadata updates (like | 
|---|
| 3766 | * unwritten to written extents conversion) in this range are in | 
|---|
| 3767 | * consistent state with the file data blocks, before performing the | 
|---|
| 3768 | * actual write I/O. If the commit fails, the whole I/O must be aborted | 
|---|
| 3769 | * to prevent any possible torn writes. | 
|---|
| 3770 | */ | 
|---|
| 3771 | if (ret > 0 && force_commit) { | 
|---|
| 3772 | int ret2; | 
|---|
| 3773 |  | 
|---|
| 3774 | ret2 = ext4_force_commit(sb: inode->i_sb); | 
|---|
| 3775 | if (ret2) | 
|---|
| 3776 | return ret2; | 
|---|
| 3777 | } | 
|---|
| 3778 |  | 
|---|
| 3779 | return ret; | 
|---|
| 3780 | } | 
|---|
| 3781 |  | 
|---|
| 3782 |  | 
|---|
| 3783 | static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, | 
|---|
| 3784 | unsigned flags, struct iomap *iomap, struct iomap *srcmap) | 
|---|
| 3785 | { | 
|---|
| 3786 | int ret; | 
|---|
| 3787 | struct ext4_map_blocks map; | 
|---|
| 3788 | u8 blkbits = inode->i_blkbits; | 
|---|
| 3789 | unsigned int orig_mlen; | 
|---|
| 3790 |  | 
|---|
| 3791 | if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) | 
|---|
| 3792 | return -EINVAL; | 
|---|
| 3793 |  | 
|---|
| 3794 | if (WARN_ON_ONCE(ext4_has_inline_data(inode))) | 
|---|
| 3795 | return -ERANGE; | 
|---|
| 3796 |  | 
|---|
| 3797 | /* | 
|---|
| 3798 | * Calculate the first and last logical blocks respectively. | 
|---|
| 3799 | */ | 
|---|
| 3800 | map.m_lblk = offset >> blkbits; | 
|---|
| 3801 | map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, | 
|---|
| 3802 | EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; | 
|---|
| 3803 | orig_mlen = map.m_len; | 
|---|
| 3804 |  | 
|---|
| 3805 | if (flags & IOMAP_WRITE) { | 
|---|
| 3806 | /* | 
|---|
| 3807 | * We check here if the blocks are already allocated, then we | 
|---|
| 3808 | * don't need to start a journal txn and we can directly return | 
|---|
| 3809 | * the mapping information. This could boost performance | 
|---|
| 3810 | * especially in multi-threaded overwrite requests. | 
|---|
| 3811 | */ | 
|---|
| 3812 | if (offset + length <= i_size_read(inode)) { | 
|---|
| 3813 | ret = ext4_map_blocks(NULL, inode, map: &map, flags: 0); | 
|---|
| 3814 | /* | 
|---|
| 3815 | * For atomic writes the entire requested length should | 
|---|
| 3816 | * be mapped. | 
|---|
| 3817 | */ | 
|---|
| 3818 | if (map.m_flags & EXT4_MAP_MAPPED) { | 
|---|
| 3819 | if ((!(flags & IOMAP_ATOMIC) && ret > 0) || | 
|---|
| 3820 | (flags & IOMAP_ATOMIC && ret >= orig_mlen)) | 
|---|
| 3821 | goto out; | 
|---|
| 3822 | } | 
|---|
| 3823 | map.m_len = orig_mlen; | 
|---|
| 3824 | } | 
|---|
| 3825 | ret = ext4_iomap_alloc(inode, map: &map, flags); | 
|---|
| 3826 | } else { | 
|---|
| 3827 | /* | 
|---|
| 3828 | * This can be called for overwrites path from | 
|---|
| 3829 | * ext4_iomap_overwrite_begin(). | 
|---|
| 3830 | */ | 
|---|
| 3831 | ret = ext4_map_blocks(NULL, inode, map: &map, flags: 0); | 
|---|
| 3832 | } | 
|---|
| 3833 |  | 
|---|
| 3834 | if (ret < 0) | 
|---|
| 3835 | return ret; | 
|---|
| 3836 | out: | 
|---|
| 3837 | /* | 
|---|
| 3838 | * When inline encryption is enabled, sometimes I/O to an encrypted file | 
|---|
| 3839 | * has to be broken up to guarantee DUN contiguity.  Handle this by | 
|---|
| 3840 | * limiting the length of the mapping returned. | 
|---|
| 3841 | */ | 
|---|
| 3842 | map.m_len = fscrypt_limit_io_blocks(inode, lblk: map.m_lblk, nr_blocks: map.m_len); | 
|---|
| 3843 |  | 
|---|
| 3844 | /* | 
|---|
| 3845 | * Before returning to iomap, let's ensure the allocated mapping | 
|---|
| 3846 | * covers the entire requested length for atomic writes. | 
|---|
| 3847 | */ | 
|---|
| 3848 | if (flags & IOMAP_ATOMIC) { | 
|---|
| 3849 | if (map.m_len < (length >> blkbits)) { | 
|---|
| 3850 | WARN_ON_ONCE(1); | 
|---|
| 3851 | return -EINVAL; | 
|---|
| 3852 | } | 
|---|
| 3853 | } | 
|---|
| 3854 | ext4_set_iomap(inode, iomap, map: &map, offset, length, flags); | 
|---|
| 3855 |  | 
|---|
| 3856 | return 0; | 
|---|
| 3857 | } | 
|---|
| 3858 |  | 
|---|
| 3859 | static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset, | 
|---|
| 3860 | loff_t length, unsigned flags, struct iomap *iomap, | 
|---|
| 3861 | struct iomap *srcmap) | 
|---|
| 3862 | { | 
|---|
| 3863 | int ret; | 
|---|
| 3864 |  | 
|---|
| 3865 | /* | 
|---|
| 3866 | * Even for writes we don't need to allocate blocks, so just pretend | 
|---|
| 3867 | * we are reading to save overhead of starting a transaction. | 
|---|
| 3868 | */ | 
|---|
| 3869 | flags &= ~IOMAP_WRITE; | 
|---|
| 3870 | ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap); | 
|---|
| 3871 | WARN_ON_ONCE(!ret && iomap->type != IOMAP_MAPPED); | 
|---|
| 3872 | return ret; | 
|---|
| 3873 | } | 
|---|
| 3874 |  | 
|---|
| 3875 | const struct iomap_ops ext4_iomap_ops = { | 
|---|
| 3876 | .iomap_begin		= ext4_iomap_begin, | 
|---|
| 3877 | }; | 
|---|
| 3878 |  | 
|---|
| 3879 | const struct iomap_ops ext4_iomap_overwrite_ops = { | 
|---|
| 3880 | .iomap_begin		= ext4_iomap_overwrite_begin, | 
|---|
| 3881 | }; | 
|---|
| 3882 |  | 
|---|
| 3883 | static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, | 
|---|
| 3884 | loff_t length, unsigned int flags, | 
|---|
| 3885 | struct iomap *iomap, struct iomap *srcmap) | 
|---|
| 3886 | { | 
|---|
| 3887 | int ret; | 
|---|
| 3888 | struct ext4_map_blocks map; | 
|---|
| 3889 | u8 blkbits = inode->i_blkbits; | 
|---|
| 3890 |  | 
|---|
| 3891 | if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) | 
|---|
| 3892 | return -EINVAL; | 
|---|
| 3893 |  | 
|---|
| 3894 | if (ext4_has_inline_data(inode)) { | 
|---|
| 3895 | ret = ext4_inline_data_iomap(inode, iomap); | 
|---|
| 3896 | if (ret != -EAGAIN) { | 
|---|
| 3897 | if (ret == 0 && offset >= iomap->length) | 
|---|
| 3898 | ret = -ENOENT; | 
|---|
| 3899 | return ret; | 
|---|
| 3900 | } | 
|---|
| 3901 | } | 
|---|
| 3902 |  | 
|---|
| 3903 | /* | 
|---|
| 3904 | * Calculate the first and last logical block respectively. | 
|---|
| 3905 | */ | 
|---|
| 3906 | map.m_lblk = offset >> blkbits; | 
|---|
| 3907 | map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, | 
|---|
| 3908 | EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; | 
|---|
| 3909 |  | 
|---|
| 3910 | /* | 
|---|
| 3911 | * Fiemap callers may call for offset beyond s_bitmap_maxbytes. | 
|---|
| 3912 | * So handle it here itself instead of querying ext4_map_blocks(). | 
|---|
| 3913 | * Since ext4_map_blocks() will warn about it and will return | 
|---|
| 3914 | * -EIO error. | 
|---|
| 3915 | */ | 
|---|
| 3916 | if (!(ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS))) { | 
|---|
| 3917 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 3918 |  | 
|---|
| 3919 | if (offset >= sbi->s_bitmap_maxbytes) { | 
|---|
| 3920 | map.m_flags = 0; | 
|---|
| 3921 | goto set_iomap; | 
|---|
| 3922 | } | 
|---|
| 3923 | } | 
|---|
| 3924 |  | 
|---|
| 3925 | ret = ext4_map_blocks(NULL, inode, map: &map, flags: 0); | 
|---|
| 3926 | if (ret < 0) | 
|---|
| 3927 | return ret; | 
|---|
| 3928 | set_iomap: | 
|---|
| 3929 | ext4_set_iomap(inode, iomap, map: &map, offset, length, flags); | 
|---|
| 3930 |  | 
|---|
| 3931 | return 0; | 
|---|
| 3932 | } | 
|---|
| 3933 |  | 
|---|
| 3934 | const struct iomap_ops ext4_iomap_report_ops = { | 
|---|
| 3935 | .iomap_begin = ext4_iomap_begin_report, | 
|---|
| 3936 | }; | 
|---|
| 3937 |  | 
|---|
| 3938 | /* | 
|---|
| 3939 | * For data=journal mode, folio should be marked dirty only when it was | 
|---|
| 3940 | * writeably mapped. When that happens, it was already attached to the | 
|---|
| 3941 | * transaction and marked as jbddirty (we take care of this in | 
|---|
| 3942 | * ext4_page_mkwrite()). On transaction commit, we writeprotect page mappings | 
|---|
| 3943 | * so we should have nothing to do here, except for the case when someone | 
|---|
| 3944 | * had the page pinned and dirtied the page through this pin (e.g. by doing | 
|---|
| 3945 | * direct IO to it). In that case we'd need to attach buffers here to the | 
|---|
| 3946 | * transaction but we cannot due to lock ordering.  We cannot just dirty the | 
|---|
| 3947 | * folio and leave attached buffers clean, because the buffers' dirty state is | 
|---|
| 3948 | * "definitive".  We cannot just set the buffers dirty or jbddirty because all | 
|---|
| 3949 | * the journalling code will explode.  So what we do is to mark the folio | 
|---|
| 3950 | * "pending dirty" and next time ext4_writepages() is called, attach buffers | 
|---|
| 3951 | * to the transaction appropriately. | 
|---|
| 3952 | */ | 
|---|
| 3953 | static bool ext4_journalled_dirty_folio(struct address_space *mapping, | 
|---|
| 3954 | struct folio *folio) | 
|---|
| 3955 | { | 
|---|
| 3956 | WARN_ON_ONCE(!folio_buffers(folio)); | 
|---|
| 3957 | if (folio_maybe_dma_pinned(folio)) | 
|---|
| 3958 | folio_set_checked(folio); | 
|---|
| 3959 | return filemap_dirty_folio(mapping, folio); | 
|---|
| 3960 | } | 
|---|
| 3961 |  | 
|---|
| 3962 | static bool ext4_dirty_folio(struct address_space *mapping, struct folio *folio) | 
|---|
| 3963 | { | 
|---|
| 3964 | WARN_ON_ONCE(!folio_test_locked(folio) && !folio_test_dirty(folio)); | 
|---|
| 3965 | WARN_ON_ONCE(!folio_buffers(folio)); | 
|---|
| 3966 | return block_dirty_folio(mapping, folio); | 
|---|
| 3967 | } | 
|---|
| 3968 |  | 
|---|
| 3969 | static int ext4_iomap_swap_activate(struct swap_info_struct *sis, | 
|---|
| 3970 | struct file *file, sector_t *span) | 
|---|
| 3971 | { | 
|---|
| 3972 | return iomap_swapfile_activate(sis, swap_file: file, pagespan: span, | 
|---|
| 3973 | ops: &ext4_iomap_report_ops); | 
|---|
| 3974 | } | 
|---|
| 3975 |  | 
|---|
| 3976 | static const struct address_space_operations ext4_aops = { | 
|---|
| 3977 | .read_folio		= ext4_read_folio, | 
|---|
| 3978 | .readahead		= ext4_readahead, | 
|---|
| 3979 | .writepages		= ext4_writepages, | 
|---|
| 3980 | .write_begin		= ext4_write_begin, | 
|---|
| 3981 | .write_end		= ext4_write_end, | 
|---|
| 3982 | .dirty_folio		= ext4_dirty_folio, | 
|---|
| 3983 | .bmap			= ext4_bmap, | 
|---|
| 3984 | .invalidate_folio	= ext4_invalidate_folio, | 
|---|
| 3985 | .release_folio		= ext4_release_folio, | 
|---|
| 3986 | .migrate_folio		= buffer_migrate_folio, | 
|---|
| 3987 | .is_partially_uptodate  = block_is_partially_uptodate, | 
|---|
| 3988 | .error_remove_folio	= generic_error_remove_folio, | 
|---|
| 3989 | .swap_activate		= ext4_iomap_swap_activate, | 
|---|
| 3990 | }; | 
|---|
| 3991 |  | 
|---|
| 3992 | static const struct address_space_operations ext4_journalled_aops = { | 
|---|
| 3993 | .read_folio		= ext4_read_folio, | 
|---|
| 3994 | .readahead		= ext4_readahead, | 
|---|
| 3995 | .writepages		= ext4_writepages, | 
|---|
| 3996 | .write_begin		= ext4_write_begin, | 
|---|
| 3997 | .write_end		= ext4_journalled_write_end, | 
|---|
| 3998 | .dirty_folio		= ext4_journalled_dirty_folio, | 
|---|
| 3999 | .bmap			= ext4_bmap, | 
|---|
| 4000 | .invalidate_folio	= ext4_journalled_invalidate_folio, | 
|---|
| 4001 | .release_folio		= ext4_release_folio, | 
|---|
| 4002 | .migrate_folio		= buffer_migrate_folio_norefs, | 
|---|
| 4003 | .is_partially_uptodate  = block_is_partially_uptodate, | 
|---|
| 4004 | .error_remove_folio	= generic_error_remove_folio, | 
|---|
| 4005 | .swap_activate		= ext4_iomap_swap_activate, | 
|---|
| 4006 | }; | 
|---|
| 4007 |  | 
|---|
| 4008 | static const struct address_space_operations ext4_da_aops = { | 
|---|
| 4009 | .read_folio		= ext4_read_folio, | 
|---|
| 4010 | .readahead		= ext4_readahead, | 
|---|
| 4011 | .writepages		= ext4_writepages, | 
|---|
| 4012 | .write_begin		= ext4_da_write_begin, | 
|---|
| 4013 | .write_end		= ext4_da_write_end, | 
|---|
| 4014 | .dirty_folio		= ext4_dirty_folio, | 
|---|
| 4015 | .bmap			= ext4_bmap, | 
|---|
| 4016 | .invalidate_folio	= ext4_invalidate_folio, | 
|---|
| 4017 | .release_folio		= ext4_release_folio, | 
|---|
| 4018 | .migrate_folio		= buffer_migrate_folio, | 
|---|
| 4019 | .is_partially_uptodate  = block_is_partially_uptodate, | 
|---|
| 4020 | .error_remove_folio	= generic_error_remove_folio, | 
|---|
| 4021 | .swap_activate		= ext4_iomap_swap_activate, | 
|---|
| 4022 | }; | 
|---|
| 4023 |  | 
|---|
| 4024 | static const struct address_space_operations ext4_dax_aops = { | 
|---|
| 4025 | .writepages		= ext4_dax_writepages, | 
|---|
| 4026 | .dirty_folio		= noop_dirty_folio, | 
|---|
| 4027 | .bmap			= ext4_bmap, | 
|---|
| 4028 | .swap_activate		= ext4_iomap_swap_activate, | 
|---|
| 4029 | }; | 
|---|
| 4030 |  | 
|---|
| 4031 | void ext4_set_aops(struct inode *inode) | 
|---|
| 4032 | { | 
|---|
| 4033 | switch (ext4_inode_journal_mode(inode)) { | 
|---|
| 4034 | case EXT4_INODE_ORDERED_DATA_MODE: | 
|---|
| 4035 | case EXT4_INODE_WRITEBACK_DATA_MODE: | 
|---|
| 4036 | break; | 
|---|
| 4037 | case EXT4_INODE_JOURNAL_DATA_MODE: | 
|---|
| 4038 | inode->i_mapping->a_ops = &ext4_journalled_aops; | 
|---|
| 4039 | return; | 
|---|
| 4040 | default: | 
|---|
| 4041 | BUG(); | 
|---|
| 4042 | } | 
|---|
| 4043 | if (IS_DAX(inode)) | 
|---|
| 4044 | inode->i_mapping->a_ops = &ext4_dax_aops; | 
|---|
| 4045 | else if (test_opt(inode->i_sb, DELALLOC)) | 
|---|
| 4046 | inode->i_mapping->a_ops = &ext4_da_aops; | 
|---|
| 4047 | else | 
|---|
| 4048 | inode->i_mapping->a_ops = &ext4_aops; | 
|---|
| 4049 | } | 
|---|
| 4050 |  | 
|---|
| 4051 | /* | 
|---|
| 4052 | * Here we can't skip an unwritten buffer even though it usually reads zero | 
|---|
| 4053 | * because it might have data in pagecache (eg, if called from ext4_zero_range, | 
|---|
| 4054 | * ext4_punch_hole, etc) which needs to be properly zeroed out. Otherwise a | 
|---|
| 4055 | * racing writeback can come later and flush the stale pagecache to disk. | 
|---|
| 4056 | */ | 
|---|
| 4057 | static int __ext4_block_zero_page_range(handle_t *handle, | 
|---|
| 4058 | struct address_space *mapping, loff_t from, loff_t length) | 
|---|
| 4059 | { | 
|---|
| 4060 | unsigned int offset, blocksize, pos; | 
|---|
| 4061 | ext4_lblk_t iblock; | 
|---|
| 4062 | struct inode *inode = mapping->host; | 
|---|
| 4063 | struct buffer_head *bh; | 
|---|
| 4064 | struct folio *folio; | 
|---|
| 4065 | int err = 0; | 
|---|
| 4066 |  | 
|---|
| 4067 | folio = __filemap_get_folio(mapping, index: from >> PAGE_SHIFT, | 
|---|
| 4068 | FGP_LOCK | FGP_ACCESSED | FGP_CREAT, | 
|---|
| 4069 | gfp: mapping_gfp_constraint(mapping, gfp_mask: ~__GFP_FS)); | 
|---|
| 4070 | if (IS_ERR(ptr: folio)) | 
|---|
| 4071 | return PTR_ERR(ptr: folio); | 
|---|
| 4072 |  | 
|---|
| 4073 | blocksize = inode->i_sb->s_blocksize; | 
|---|
| 4074 |  | 
|---|
| 4075 | iblock = folio->index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); | 
|---|
| 4076 |  | 
|---|
| 4077 | bh = folio_buffers(folio); | 
|---|
| 4078 | if (!bh) | 
|---|
| 4079 | bh = create_empty_buffers(folio, blocksize, b_state: 0); | 
|---|
| 4080 |  | 
|---|
| 4081 | /* Find the buffer that contains "offset" */ | 
|---|
| 4082 | offset = offset_in_folio(folio, from); | 
|---|
| 4083 | pos = blocksize; | 
|---|
| 4084 | while (offset >= pos) { | 
|---|
| 4085 | bh = bh->b_this_page; | 
|---|
| 4086 | iblock++; | 
|---|
| 4087 | pos += blocksize; | 
|---|
| 4088 | } | 
|---|
| 4089 | if (buffer_freed(bh)) { | 
|---|
| 4090 | BUFFER_TRACE(bh, "freed: skip"); | 
|---|
| 4091 | goto unlock; | 
|---|
| 4092 | } | 
|---|
| 4093 | if (!buffer_mapped(bh)) { | 
|---|
| 4094 | BUFFER_TRACE(bh, "unmapped"); | 
|---|
| 4095 | ext4_get_block(inode, iblock, bh, create: 0); | 
|---|
| 4096 | /* unmapped? It's a hole - nothing to do */ | 
|---|
| 4097 | if (!buffer_mapped(bh)) { | 
|---|
| 4098 | BUFFER_TRACE(bh, "still unmapped"); | 
|---|
| 4099 | goto unlock; | 
|---|
| 4100 | } | 
|---|
| 4101 | } | 
|---|
| 4102 |  | 
|---|
| 4103 | /* Ok, it's mapped. Make sure it's up-to-date */ | 
|---|
| 4104 | if (folio_test_uptodate(folio)) | 
|---|
| 4105 | set_buffer_uptodate(bh); | 
|---|
| 4106 |  | 
|---|
| 4107 | if (!buffer_uptodate(bh)) { | 
|---|
| 4108 | err = ext4_read_bh_lock(bh, op_flags: 0, wait: true); | 
|---|
| 4109 | if (err) | 
|---|
| 4110 | goto unlock; | 
|---|
| 4111 | if (fscrypt_inode_uses_fs_layer_crypto(inode)) { | 
|---|
| 4112 | /* We expect the key to be set. */ | 
|---|
| 4113 | BUG_ON(!fscrypt_has_encryption_key(inode)); | 
|---|
| 4114 | err = fscrypt_decrypt_pagecache_blocks(folio, | 
|---|
| 4115 | len: blocksize, | 
|---|
| 4116 | offs: bh_offset(bh)); | 
|---|
| 4117 | if (err) { | 
|---|
| 4118 | clear_buffer_uptodate(bh); | 
|---|
| 4119 | goto unlock; | 
|---|
| 4120 | } | 
|---|
| 4121 | } | 
|---|
| 4122 | } | 
|---|
| 4123 | if (ext4_should_journal_data(inode)) { | 
|---|
| 4124 | BUFFER_TRACE(bh, "get write access"); | 
|---|
| 4125 | err = ext4_journal_get_write_access(handle, inode->i_sb, bh, | 
|---|
| 4126 | EXT4_JTR_NONE); | 
|---|
| 4127 | if (err) | 
|---|
| 4128 | goto unlock; | 
|---|
| 4129 | } | 
|---|
| 4130 | folio_zero_range(folio, start: offset, length); | 
|---|
| 4131 | BUFFER_TRACE(bh, "zeroed end of block"); | 
|---|
| 4132 |  | 
|---|
| 4133 | if (ext4_should_journal_data(inode)) { | 
|---|
| 4134 | err = ext4_dirty_journalled_data(handle, bh); | 
|---|
| 4135 | } else { | 
|---|
| 4136 | err = 0; | 
|---|
| 4137 | mark_buffer_dirty(bh); | 
|---|
| 4138 | if (ext4_should_order_data(inode)) | 
|---|
| 4139 | err = ext4_jbd2_inode_add_write(handle, inode, start_byte: from, | 
|---|
| 4140 | length); | 
|---|
| 4141 | } | 
|---|
| 4142 |  | 
|---|
| 4143 | unlock: | 
|---|
| 4144 | folio_unlock(folio); | 
|---|
| 4145 | folio_put(folio); | 
|---|
| 4146 | return err; | 
|---|
| 4147 | } | 
|---|
| 4148 |  | 
|---|
| 4149 | /* | 
|---|
| 4150 | * ext4_block_zero_page_range() zeros out a mapping of length 'length' | 
|---|
| 4151 | * starting from file offset 'from'.  The range to be zero'd must | 
|---|
| 4152 | * be contained with in one block.  If the specified range exceeds | 
|---|
| 4153 | * the end of the block it will be shortened to end of the block | 
|---|
| 4154 | * that corresponds to 'from' | 
|---|
| 4155 | */ | 
|---|
| 4156 | static int ext4_block_zero_page_range(handle_t *handle, | 
|---|
| 4157 | struct address_space *mapping, loff_t from, loff_t length) | 
|---|
| 4158 | { | 
|---|
| 4159 | struct inode *inode = mapping->host; | 
|---|
| 4160 | unsigned offset = from & (PAGE_SIZE-1); | 
|---|
| 4161 | unsigned blocksize = inode->i_sb->s_blocksize; | 
|---|
| 4162 | unsigned max = blocksize - (offset & (blocksize - 1)); | 
|---|
| 4163 |  | 
|---|
| 4164 | /* | 
|---|
| 4165 | * correct length if it does not fall between | 
|---|
| 4166 | * 'from' and the end of the block | 
|---|
| 4167 | */ | 
|---|
| 4168 | if (length > max || length < 0) | 
|---|
| 4169 | length = max; | 
|---|
| 4170 |  | 
|---|
| 4171 | if (IS_DAX(inode)) { | 
|---|
| 4172 | return dax_zero_range(inode, pos: from, len: length, NULL, | 
|---|
| 4173 | ops: &ext4_iomap_ops); | 
|---|
| 4174 | } | 
|---|
| 4175 | return __ext4_block_zero_page_range(handle, mapping, from, length); | 
|---|
| 4176 | } | 
|---|
| 4177 |  | 
|---|
| 4178 | /* | 
|---|
| 4179 | * ext4_block_truncate_page() zeroes out a mapping from file offset `from' | 
|---|
| 4180 | * up to the end of the block which corresponds to `from'. | 
|---|
| 4181 | * This required during truncate. We need to physically zero the tail end | 
|---|
| 4182 | * of that block so it doesn't yield old data if the file is later grown. | 
|---|
| 4183 | */ | 
|---|
| 4184 | static int ext4_block_truncate_page(handle_t *handle, | 
|---|
| 4185 | struct address_space *mapping, loff_t from) | 
|---|
| 4186 | { | 
|---|
| 4187 | unsigned offset = from & (PAGE_SIZE-1); | 
|---|
| 4188 | unsigned length; | 
|---|
| 4189 | unsigned blocksize; | 
|---|
| 4190 | struct inode *inode = mapping->host; | 
|---|
| 4191 |  | 
|---|
| 4192 | /* If we are processing an encrypted inode during orphan list handling */ | 
|---|
| 4193 | if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) | 
|---|
| 4194 | return 0; | 
|---|
| 4195 |  | 
|---|
| 4196 | blocksize = inode->i_sb->s_blocksize; | 
|---|
| 4197 | length = blocksize - (offset & (blocksize - 1)); | 
|---|
| 4198 |  | 
|---|
| 4199 | return ext4_block_zero_page_range(handle, mapping, from, length); | 
|---|
| 4200 | } | 
|---|
| 4201 |  | 
|---|
| 4202 | int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, | 
|---|
| 4203 | loff_t lstart, loff_t length) | 
|---|
| 4204 | { | 
|---|
| 4205 | struct super_block *sb = inode->i_sb; | 
|---|
| 4206 | struct address_space *mapping = inode->i_mapping; | 
|---|
| 4207 | unsigned partial_start, partial_end; | 
|---|
| 4208 | ext4_fsblk_t start, end; | 
|---|
| 4209 | loff_t byte_end = (lstart + length - 1); | 
|---|
| 4210 | int err = 0; | 
|---|
| 4211 |  | 
|---|
| 4212 | partial_start = lstart & (sb->s_blocksize - 1); | 
|---|
| 4213 | partial_end = byte_end & (sb->s_blocksize - 1); | 
|---|
| 4214 |  | 
|---|
| 4215 | start = lstart >> sb->s_blocksize_bits; | 
|---|
| 4216 | end = byte_end >> sb->s_blocksize_bits; | 
|---|
| 4217 |  | 
|---|
| 4218 | /* Handle partial zero within the single block */ | 
|---|
| 4219 | if (start == end && | 
|---|
| 4220 | (partial_start || (partial_end != sb->s_blocksize - 1))) { | 
|---|
| 4221 | err = ext4_block_zero_page_range(handle, mapping, | 
|---|
| 4222 | from: lstart, length); | 
|---|
| 4223 | return err; | 
|---|
| 4224 | } | 
|---|
| 4225 | /* Handle partial zero out on the start of the range */ | 
|---|
| 4226 | if (partial_start) { | 
|---|
| 4227 | err = ext4_block_zero_page_range(handle, mapping, | 
|---|
| 4228 | from: lstart, length: sb->s_blocksize); | 
|---|
| 4229 | if (err) | 
|---|
| 4230 | return err; | 
|---|
| 4231 | } | 
|---|
| 4232 | /* Handle partial zero out on the end of the range */ | 
|---|
| 4233 | if (partial_end != sb->s_blocksize - 1) | 
|---|
| 4234 | err = ext4_block_zero_page_range(handle, mapping, | 
|---|
| 4235 | from: byte_end - partial_end, | 
|---|
| 4236 | length: partial_end + 1); | 
|---|
| 4237 | return err; | 
|---|
| 4238 | } | 
|---|
| 4239 |  | 
|---|
| 4240 | int ext4_can_truncate(struct inode *inode) | 
|---|
| 4241 | { | 
|---|
| 4242 | if (S_ISREG(inode->i_mode)) | 
|---|
| 4243 | return 1; | 
|---|
| 4244 | if (S_ISDIR(inode->i_mode)) | 
|---|
| 4245 | return 1; | 
|---|
| 4246 | if (S_ISLNK(inode->i_mode)) | 
|---|
| 4247 | return !ext4_inode_is_fast_symlink(inode); | 
|---|
| 4248 | return 0; | 
|---|
| 4249 | } | 
|---|
| 4250 |  | 
|---|
| 4251 | /* | 
|---|
| 4252 | * We have to make sure i_disksize gets properly updated before we truncate | 
|---|
| 4253 | * page cache due to hole punching or zero range. Otherwise i_disksize update | 
|---|
| 4254 | * can get lost as it may have been postponed to submission of writeback but | 
|---|
| 4255 | * that will never happen if we remove the folio containing i_size from the | 
|---|
| 4256 | * page cache. Also if we punch hole within i_size but above i_disksize, | 
|---|
| 4257 | * following ext4_page_mkwrite() may mistakenly allocate written blocks over | 
|---|
| 4258 | * the hole and thus introduce allocated blocks beyond i_disksize which is | 
|---|
| 4259 | * not allowed (e2fsck would complain in case of crash). | 
|---|
| 4260 | */ | 
|---|
| 4261 | int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, | 
|---|
| 4262 | loff_t len) | 
|---|
| 4263 | { | 
|---|
| 4264 | handle_t *handle; | 
|---|
| 4265 | int ret; | 
|---|
| 4266 |  | 
|---|
| 4267 | loff_t size = i_size_read(inode); | 
|---|
| 4268 |  | 
|---|
| 4269 | WARN_ON(!inode_is_locked(inode)); | 
|---|
| 4270 | if (offset > size) | 
|---|
| 4271 | return 0; | 
|---|
| 4272 |  | 
|---|
| 4273 | if (offset + len < size) | 
|---|
| 4274 | size = offset + len; | 
|---|
| 4275 | if (EXT4_I(inode)->i_disksize >= size) | 
|---|
| 4276 | return 0; | 
|---|
| 4277 |  | 
|---|
| 4278 | handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); | 
|---|
| 4279 | if (IS_ERR(ptr: handle)) | 
|---|
| 4280 | return PTR_ERR(ptr: handle); | 
|---|
| 4281 | ext4_update_i_disksize(inode, newsize: size); | 
|---|
| 4282 | ret = ext4_mark_inode_dirty(handle, inode); | 
|---|
| 4283 | ext4_journal_stop(handle); | 
|---|
| 4284 |  | 
|---|
| 4285 | return ret; | 
|---|
| 4286 | } | 
|---|
| 4287 |  | 
|---|
| 4288 | static inline void ext4_truncate_folio(struct inode *inode, | 
|---|
| 4289 | loff_t start, loff_t end) | 
|---|
| 4290 | { | 
|---|
| 4291 | unsigned long blocksize = i_blocksize(node: inode); | 
|---|
| 4292 | struct folio *folio; | 
|---|
| 4293 |  | 
|---|
| 4294 | /* Nothing to be done if no complete block needs to be truncated. */ | 
|---|
| 4295 | if (round_up(start, blocksize) >= round_down(end, blocksize)) | 
|---|
| 4296 | return; | 
|---|
| 4297 |  | 
|---|
| 4298 | folio = filemap_lock_folio(mapping: inode->i_mapping, index: start >> PAGE_SHIFT); | 
|---|
| 4299 | if (IS_ERR(ptr: folio)) | 
|---|
| 4300 | return; | 
|---|
| 4301 |  | 
|---|
| 4302 | if (folio_mkclean(folio)) | 
|---|
| 4303 | folio_mark_dirty(folio); | 
|---|
| 4304 | folio_unlock(folio); | 
|---|
| 4305 | folio_put(folio); | 
|---|
| 4306 | } | 
|---|
| 4307 |  | 
|---|
| 4308 | int ext4_truncate_page_cache_block_range(struct inode *inode, | 
|---|
| 4309 | loff_t start, loff_t end) | 
|---|
| 4310 | { | 
|---|
| 4311 | unsigned long blocksize = i_blocksize(node: inode); | 
|---|
| 4312 | int ret; | 
|---|
| 4313 |  | 
|---|
| 4314 | /* | 
|---|
| 4315 | * For journalled data we need to write (and checkpoint) pages | 
|---|
| 4316 | * before discarding page cache to avoid inconsitent data on disk | 
|---|
| 4317 | * in case of crash before freeing or unwritten converting trans | 
|---|
| 4318 | * is committed. | 
|---|
| 4319 | */ | 
|---|
| 4320 | if (ext4_should_journal_data(inode)) { | 
|---|
| 4321 | ret = filemap_write_and_wait_range(mapping: inode->i_mapping, lstart: start, | 
|---|
| 4322 | lend: end - 1); | 
|---|
| 4323 | if (ret) | 
|---|
| 4324 | return ret; | 
|---|
| 4325 | goto truncate_pagecache; | 
|---|
| 4326 | } | 
|---|
| 4327 |  | 
|---|
| 4328 | /* | 
|---|
| 4329 | * If the block size is less than the page size, the file's mapped | 
|---|
| 4330 | * blocks within one page could be freed or converted to unwritten. | 
|---|
| 4331 | * So it's necessary to remove writable userspace mappings, and then | 
|---|
| 4332 | * ext4_page_mkwrite() can be called during subsequent write access | 
|---|
| 4333 | * to these partial folios. | 
|---|
| 4334 | */ | 
|---|
| 4335 | if (!IS_ALIGNED(start | end, PAGE_SIZE) && | 
|---|
| 4336 | blocksize < PAGE_SIZE && start < inode->i_size) { | 
|---|
| 4337 | loff_t page_boundary = round_up(start, PAGE_SIZE); | 
|---|
| 4338 |  | 
|---|
| 4339 | ext4_truncate_folio(inode, start, min(page_boundary, end)); | 
|---|
| 4340 | if (end > page_boundary) | 
|---|
| 4341 | ext4_truncate_folio(inode, | 
|---|
| 4342 | round_down(end, PAGE_SIZE), end); | 
|---|
| 4343 | } | 
|---|
| 4344 |  | 
|---|
| 4345 | truncate_pagecache: | 
|---|
| 4346 | truncate_pagecache_range(inode, offset: start, end: end - 1); | 
|---|
| 4347 | return 0; | 
|---|
| 4348 | } | 
|---|
| 4349 |  | 
|---|
| 4350 | static void ext4_wait_dax_page(struct inode *inode) | 
|---|
| 4351 | { | 
|---|
| 4352 | filemap_invalidate_unlock(mapping: inode->i_mapping); | 
|---|
| 4353 | schedule(); | 
|---|
| 4354 | filemap_invalidate_lock(mapping: inode->i_mapping); | 
|---|
| 4355 | } | 
|---|
| 4356 |  | 
|---|
| 4357 | int ext4_break_layouts(struct inode *inode) | 
|---|
| 4358 | { | 
|---|
| 4359 | if (WARN_ON_ONCE(!rwsem_is_locked(&inode->i_mapping->invalidate_lock))) | 
|---|
| 4360 | return -EINVAL; | 
|---|
| 4361 |  | 
|---|
| 4362 | return dax_break_layout_inode(inode, cb: ext4_wait_dax_page); | 
|---|
| 4363 | } | 
|---|
| 4364 |  | 
|---|
| 4365 | /* | 
|---|
| 4366 | * ext4_punch_hole: punches a hole in a file by releasing the blocks | 
|---|
| 4367 | * associated with the given offset and length | 
|---|
| 4368 | * | 
|---|
| 4369 | * @inode:  File inode | 
|---|
| 4370 | * @offset: The offset where the hole will begin | 
|---|
| 4371 | * @len:    The length of the hole | 
|---|
| 4372 | * | 
|---|
| 4373 | * Returns: 0 on success or negative on failure | 
|---|
| 4374 | */ | 
|---|
| 4375 |  | 
|---|
| 4376 | int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) | 
|---|
| 4377 | { | 
|---|
| 4378 | struct inode *inode = file_inode(f: file); | 
|---|
| 4379 | struct super_block *sb = inode->i_sb; | 
|---|
| 4380 | ext4_lblk_t start_lblk, end_lblk; | 
|---|
| 4381 | loff_t max_end = sb->s_maxbytes; | 
|---|
| 4382 | loff_t end = offset + length; | 
|---|
| 4383 | handle_t *handle; | 
|---|
| 4384 | unsigned int credits; | 
|---|
| 4385 | int ret; | 
|---|
| 4386 |  | 
|---|
| 4387 | trace_ext4_punch_hole(inode, offset, len: length, mode: 0); | 
|---|
| 4388 | WARN_ON_ONCE(!inode_is_locked(inode)); | 
|---|
| 4389 |  | 
|---|
| 4390 | /* | 
|---|
| 4391 | * For indirect-block based inodes, make sure that the hole within | 
|---|
| 4392 | * one block before last range. | 
|---|
| 4393 | */ | 
|---|
| 4394 | if (!ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS)) | 
|---|
| 4395 | max_end = EXT4_SB(sb)->s_bitmap_maxbytes - sb->s_blocksize; | 
|---|
| 4396 |  | 
|---|
| 4397 | /* No need to punch hole beyond i_size */ | 
|---|
| 4398 | if (offset >= inode->i_size || offset >= max_end) | 
|---|
| 4399 | return 0; | 
|---|
| 4400 |  | 
|---|
| 4401 | /* | 
|---|
| 4402 | * If the hole extends beyond i_size, set the hole to end after | 
|---|
| 4403 | * the page that contains i_size. | 
|---|
| 4404 | */ | 
|---|
| 4405 | if (end > inode->i_size) | 
|---|
| 4406 | end = round_up(inode->i_size, PAGE_SIZE); | 
|---|
| 4407 | if (end > max_end) | 
|---|
| 4408 | end = max_end; | 
|---|
| 4409 | length = end - offset; | 
|---|
| 4410 |  | 
|---|
| 4411 | /* | 
|---|
| 4412 | * Attach jinode to inode for jbd2 if we do any zeroing of partial | 
|---|
| 4413 | * block. | 
|---|
| 4414 | */ | 
|---|
| 4415 | if (!IS_ALIGNED(offset | end, sb->s_blocksize)) { | 
|---|
| 4416 | ret = ext4_inode_attach_jinode(inode); | 
|---|
| 4417 | if (ret < 0) | 
|---|
| 4418 | return ret; | 
|---|
| 4419 | } | 
|---|
| 4420 |  | 
|---|
| 4421 |  | 
|---|
| 4422 | ret = ext4_update_disksize_before_punch(inode, offset, len: length); | 
|---|
| 4423 | if (ret) | 
|---|
| 4424 | return ret; | 
|---|
| 4425 |  | 
|---|
| 4426 | /* Now release the pages and zero block aligned part of pages*/ | 
|---|
| 4427 | ret = ext4_truncate_page_cache_block_range(inode, start: offset, end); | 
|---|
| 4428 | if (ret) | 
|---|
| 4429 | return ret; | 
|---|
| 4430 |  | 
|---|
| 4431 | if (ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS)) | 
|---|
| 4432 | credits = ext4_chunk_trans_extent(inode, nrblocks: 2); | 
|---|
| 4433 | else | 
|---|
| 4434 | credits = ext4_blocks_for_truncate(inode); | 
|---|
| 4435 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); | 
|---|
| 4436 | if (IS_ERR(ptr: handle)) { | 
|---|
| 4437 | ret = PTR_ERR(ptr: handle); | 
|---|
| 4438 | ext4_std_error(sb, ret); | 
|---|
| 4439 | return ret; | 
|---|
| 4440 | } | 
|---|
| 4441 |  | 
|---|
| 4442 | ret = ext4_zero_partial_blocks(handle, inode, lstart: offset, length); | 
|---|
| 4443 | if (ret) | 
|---|
| 4444 | goto out_handle; | 
|---|
| 4445 |  | 
|---|
| 4446 | /* If there are blocks to remove, do it */ | 
|---|
| 4447 | start_lblk = EXT4_B_TO_LBLK(inode, offset); | 
|---|
| 4448 | end_lblk = end >> inode->i_blkbits; | 
|---|
| 4449 |  | 
|---|
| 4450 | if (end_lblk > start_lblk) { | 
|---|
| 4451 | ext4_lblk_t hole_len = end_lblk - start_lblk; | 
|---|
| 4452 |  | 
|---|
| 4453 | ext4_fc_track_inode(handle, inode); | 
|---|
| 4454 | ext4_check_map_extents_env(inode); | 
|---|
| 4455 | down_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 4456 | ext4_discard_preallocations(inode); | 
|---|
| 4457 |  | 
|---|
| 4458 | ext4_es_remove_extent(inode, lblk: start_lblk, len: hole_len); | 
|---|
| 4459 |  | 
|---|
| 4460 | if (ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS)) | 
|---|
| 4461 | ret = ext4_ext_remove_space(inode, start: start_lblk, | 
|---|
| 4462 | end: end_lblk - 1); | 
|---|
| 4463 | else | 
|---|
| 4464 | ret = ext4_ind_remove_space(handle, inode, start: start_lblk, | 
|---|
| 4465 | end: end_lblk); | 
|---|
| 4466 | if (ret) { | 
|---|
| 4467 | up_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 4468 | goto out_handle; | 
|---|
| 4469 | } | 
|---|
| 4470 |  | 
|---|
| 4471 | ext4_es_insert_extent(inode, lblk: start_lblk, len: hole_len, pblk: ~0, | 
|---|
| 4472 | EXTENT_STATUS_HOLE, delalloc_reserve_used: 0); | 
|---|
| 4473 | up_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 4474 | } | 
|---|
| 4475 | ext4_fc_track_range(handle, inode, start: start_lblk, end: end_lblk); | 
|---|
| 4476 |  | 
|---|
| 4477 | ret = ext4_mark_inode_dirty(handle, inode); | 
|---|
| 4478 | if (unlikely(ret)) | 
|---|
| 4479 | goto out_handle; | 
|---|
| 4480 |  | 
|---|
| 4481 | ext4_update_inode_fsync_trans(handle, inode, datasync: 1); | 
|---|
| 4482 | if (IS_SYNC(inode)) | 
|---|
| 4483 | ext4_handle_sync(handle); | 
|---|
| 4484 | out_handle: | 
|---|
| 4485 | ext4_journal_stop(handle); | 
|---|
| 4486 | return ret; | 
|---|
| 4487 | } | 
|---|
| 4488 |  | 
|---|
| 4489 | int ext4_inode_attach_jinode(struct inode *inode) | 
|---|
| 4490 | { | 
|---|
| 4491 | struct ext4_inode_info *ei = EXT4_I(inode); | 
|---|
| 4492 | struct jbd2_inode *jinode; | 
|---|
| 4493 |  | 
|---|
| 4494 | if (ei->jinode || !EXT4_SB(sb: inode->i_sb)->s_journal) | 
|---|
| 4495 | return 0; | 
|---|
| 4496 |  | 
|---|
| 4497 | jinode = jbd2_alloc_inode(GFP_KERNEL); | 
|---|
| 4498 | spin_lock(lock: &inode->i_lock); | 
|---|
| 4499 | if (!ei->jinode) { | 
|---|
| 4500 | if (!jinode) { | 
|---|
| 4501 | spin_unlock(lock: &inode->i_lock); | 
|---|
| 4502 | return -ENOMEM; | 
|---|
| 4503 | } | 
|---|
| 4504 | ei->jinode = jinode; | 
|---|
| 4505 | jbd2_journal_init_jbd_inode(jinode: ei->jinode, inode); | 
|---|
| 4506 | jinode = NULL; | 
|---|
| 4507 | } | 
|---|
| 4508 | spin_unlock(lock: &inode->i_lock); | 
|---|
| 4509 | if (unlikely(jinode != NULL)) | 
|---|
| 4510 | jbd2_free_inode(jinode); | 
|---|
| 4511 | return 0; | 
|---|
| 4512 | } | 
|---|
| 4513 |  | 
|---|
| 4514 | /* | 
|---|
| 4515 | * ext4_truncate() | 
|---|
| 4516 | * | 
|---|
| 4517 | * We block out ext4_get_block() block instantiations across the entire | 
|---|
| 4518 | * transaction, and VFS/VM ensures that ext4_truncate() cannot run | 
|---|
| 4519 | * simultaneously on behalf of the same inode. | 
|---|
| 4520 | * | 
|---|
| 4521 | * As we work through the truncate and commit bits of it to the journal there | 
|---|
| 4522 | * is one core, guiding principle: the file's tree must always be consistent on | 
|---|
| 4523 | * disk.  We must be able to restart the truncate after a crash. | 
|---|
| 4524 | * | 
|---|
| 4525 | * The file's tree may be transiently inconsistent in memory (although it | 
|---|
| 4526 | * probably isn't), but whenever we close off and commit a journal transaction, | 
|---|
| 4527 | * the contents of (the filesystem + the journal) must be consistent and | 
|---|
| 4528 | * restartable.  It's pretty simple, really: bottom up, right to left (although | 
|---|
| 4529 | * left-to-right works OK too). | 
|---|
| 4530 | * | 
|---|
| 4531 | * Note that at recovery time, journal replay occurs *before* the restart of | 
|---|
| 4532 | * truncate against the orphan inode list. | 
|---|
| 4533 | * | 
|---|
| 4534 | * The committed inode has the new, desired i_size (which is the same as | 
|---|
| 4535 | * i_disksize in this case).  After a crash, ext4_orphan_cleanup() will see | 
|---|
| 4536 | * that this inode's truncate did not complete and it will again call | 
|---|
| 4537 | * ext4_truncate() to have another go.  So there will be instantiated blocks | 
|---|
| 4538 | * to the right of the truncation point in a crashed ext4 filesystem.  But | 
|---|
| 4539 | * that's fine - as long as they are linked from the inode, the post-crash | 
|---|
| 4540 | * ext4_truncate() run will find them and release them. | 
|---|
| 4541 | */ | 
|---|
| 4542 | int ext4_truncate(struct inode *inode) | 
|---|
| 4543 | { | 
|---|
| 4544 | struct ext4_inode_info *ei = EXT4_I(inode); | 
|---|
| 4545 | unsigned int credits; | 
|---|
| 4546 | int err = 0, err2; | 
|---|
| 4547 | handle_t *handle; | 
|---|
| 4548 | struct address_space *mapping = inode->i_mapping; | 
|---|
| 4549 |  | 
|---|
| 4550 | /* | 
|---|
| 4551 | * There is a possibility that we're either freeing the inode | 
|---|
| 4552 | * or it's a completely new inode. In those cases we might not | 
|---|
| 4553 | * have i_rwsem locked because it's not necessary. | 
|---|
| 4554 | */ | 
|---|
| 4555 | if (!(inode->i_state & (I_NEW|I_FREEING))) | 
|---|
| 4556 | WARN_ON(!inode_is_locked(inode)); | 
|---|
| 4557 | trace_ext4_truncate_enter(inode); | 
|---|
| 4558 |  | 
|---|
| 4559 | if (!ext4_can_truncate(inode)) | 
|---|
| 4560 | goto out_trace; | 
|---|
| 4561 |  | 
|---|
| 4562 | if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) | 
|---|
| 4563 | ext4_set_inode_state(inode, bit: EXT4_STATE_DA_ALLOC_CLOSE); | 
|---|
| 4564 |  | 
|---|
| 4565 | if (ext4_has_inline_data(inode)) { | 
|---|
| 4566 | int has_inline = 1; | 
|---|
| 4567 |  | 
|---|
| 4568 | err = ext4_inline_data_truncate(inode, has_inline: &has_inline); | 
|---|
| 4569 | if (err || has_inline) | 
|---|
| 4570 | goto out_trace; | 
|---|
| 4571 | } | 
|---|
| 4572 |  | 
|---|
| 4573 | /* If we zero-out tail of the page, we have to create jinode for jbd2 */ | 
|---|
| 4574 | if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { | 
|---|
| 4575 | err = ext4_inode_attach_jinode(inode); | 
|---|
| 4576 | if (err) | 
|---|
| 4577 | goto out_trace; | 
|---|
| 4578 | } | 
|---|
| 4579 |  | 
|---|
| 4580 | if (ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS)) | 
|---|
| 4581 | credits = ext4_chunk_trans_extent(inode, nrblocks: 1); | 
|---|
| 4582 | else | 
|---|
| 4583 | credits = ext4_blocks_for_truncate(inode); | 
|---|
| 4584 |  | 
|---|
| 4585 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); | 
|---|
| 4586 | if (IS_ERR(ptr: handle)) { | 
|---|
| 4587 | err = PTR_ERR(ptr: handle); | 
|---|
| 4588 | goto out_trace; | 
|---|
| 4589 | } | 
|---|
| 4590 |  | 
|---|
| 4591 | if (inode->i_size & (inode->i_sb->s_blocksize - 1)) | 
|---|
| 4592 | ext4_block_truncate_page(handle, mapping, from: inode->i_size); | 
|---|
| 4593 |  | 
|---|
| 4594 | /* | 
|---|
| 4595 | * We add the inode to the orphan list, so that if this | 
|---|
| 4596 | * truncate spans multiple transactions, and we crash, we will | 
|---|
| 4597 | * resume the truncate when the filesystem recovers.  It also | 
|---|
| 4598 | * marks the inode dirty, to catch the new size. | 
|---|
| 4599 | * | 
|---|
| 4600 | * Implication: the file must always be in a sane, consistent | 
|---|
| 4601 | * truncatable state while each transaction commits. | 
|---|
| 4602 | */ | 
|---|
| 4603 | err = ext4_orphan_add(handle, inode); | 
|---|
| 4604 | if (err) | 
|---|
| 4605 | goto out_stop; | 
|---|
| 4606 |  | 
|---|
| 4607 | ext4_fc_track_inode(handle, inode); | 
|---|
| 4608 | ext4_check_map_extents_env(inode); | 
|---|
| 4609 |  | 
|---|
| 4610 | down_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 4611 | ext4_discard_preallocations(inode); | 
|---|
| 4612 |  | 
|---|
| 4613 | if (ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS)) | 
|---|
| 4614 | err = ext4_ext_truncate(handle, inode); | 
|---|
| 4615 | else | 
|---|
| 4616 | ext4_ind_truncate(handle, inode); | 
|---|
| 4617 |  | 
|---|
| 4618 | up_write(sem: &ei->i_data_sem); | 
|---|
| 4619 | if (err) | 
|---|
| 4620 | goto out_stop; | 
|---|
| 4621 |  | 
|---|
| 4622 | if (IS_SYNC(inode)) | 
|---|
| 4623 | ext4_handle_sync(handle); | 
|---|
| 4624 |  | 
|---|
| 4625 | out_stop: | 
|---|
| 4626 | /* | 
|---|
| 4627 | * If this was a simple ftruncate() and the file will remain alive, | 
|---|
| 4628 | * then we need to clear up the orphan record which we created above. | 
|---|
| 4629 | * However, if this was a real unlink then we were called by | 
|---|
| 4630 | * ext4_evict_inode(), and we allow that function to clean up the | 
|---|
| 4631 | * orphan info for us. | 
|---|
| 4632 | */ | 
|---|
| 4633 | if (inode->i_nlink) | 
|---|
| 4634 | ext4_orphan_del(handle, inode); | 
|---|
| 4635 |  | 
|---|
| 4636 | inode_set_mtime_to_ts(inode, ts: inode_set_ctime_current(inode)); | 
|---|
| 4637 | err2 = ext4_mark_inode_dirty(handle, inode); | 
|---|
| 4638 | if (unlikely(err2 && !err)) | 
|---|
| 4639 | err = err2; | 
|---|
| 4640 | ext4_journal_stop(handle); | 
|---|
| 4641 |  | 
|---|
| 4642 | out_trace: | 
|---|
| 4643 | trace_ext4_truncate_exit(inode); | 
|---|
| 4644 | return err; | 
|---|
| 4645 | } | 
|---|
| 4646 |  | 
|---|
| 4647 | static inline u64 ext4_inode_peek_iversion(const struct inode *inode) | 
|---|
| 4648 | { | 
|---|
| 4649 | if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) | 
|---|
| 4650 | return inode_peek_iversion_raw(inode); | 
|---|
| 4651 | else | 
|---|
| 4652 | return inode_peek_iversion(inode); | 
|---|
| 4653 | } | 
|---|
| 4654 |  | 
|---|
| 4655 | static int ext4_inode_blocks_set(struct ext4_inode *raw_inode, | 
|---|
| 4656 | struct ext4_inode_info *ei) | 
|---|
| 4657 | { | 
|---|
| 4658 | struct inode *inode = &(ei->vfs_inode); | 
|---|
| 4659 | u64 i_blocks = READ_ONCE(inode->i_blocks); | 
|---|
| 4660 | struct super_block *sb = inode->i_sb; | 
|---|
| 4661 |  | 
|---|
| 4662 | if (i_blocks <= ~0U) { | 
|---|
| 4663 | /* | 
|---|
| 4664 | * i_blocks can be represented in a 32 bit variable | 
|---|
| 4665 | * as multiple of 512 bytes | 
|---|
| 4666 | */ | 
|---|
| 4667 | raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks); | 
|---|
| 4668 | raw_inode->i_blocks_high = 0; | 
|---|
| 4669 | ext4_clear_inode_flag(inode, bit: EXT4_INODE_HUGE_FILE); | 
|---|
| 4670 | return 0; | 
|---|
| 4671 | } | 
|---|
| 4672 |  | 
|---|
| 4673 | /* | 
|---|
| 4674 | * This should never happen since sb->s_maxbytes should not have | 
|---|
| 4675 | * allowed this, sb->s_maxbytes was set according to the huge_file | 
|---|
| 4676 | * feature in ext4_fill_super(). | 
|---|
| 4677 | */ | 
|---|
| 4678 | if (!ext4_has_feature_huge_file(sb)) | 
|---|
| 4679 | return -EFSCORRUPTED; | 
|---|
| 4680 |  | 
|---|
| 4681 | if (i_blocks <= 0xffffffffffffULL) { | 
|---|
| 4682 | /* | 
|---|
| 4683 | * i_blocks can be represented in a 48 bit variable | 
|---|
| 4684 | * as multiple of 512 bytes | 
|---|
| 4685 | */ | 
|---|
| 4686 | raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks); | 
|---|
| 4687 | raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); | 
|---|
| 4688 | ext4_clear_inode_flag(inode, bit: EXT4_INODE_HUGE_FILE); | 
|---|
| 4689 | } else { | 
|---|
| 4690 | ext4_set_inode_flag(inode, bit: EXT4_INODE_HUGE_FILE); | 
|---|
| 4691 | /* i_block is stored in file system block size */ | 
|---|
| 4692 | i_blocks = i_blocks >> (inode->i_blkbits - 9); | 
|---|
| 4693 | raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks); | 
|---|
| 4694 | raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); | 
|---|
| 4695 | } | 
|---|
| 4696 | return 0; | 
|---|
| 4697 | } | 
|---|
| 4698 |  | 
|---|
| 4699 | static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode) | 
|---|
| 4700 | { | 
|---|
| 4701 | struct ext4_inode_info *ei = EXT4_I(inode); | 
|---|
| 4702 | uid_t i_uid; | 
|---|
| 4703 | gid_t i_gid; | 
|---|
| 4704 | projid_t i_projid; | 
|---|
| 4705 | int block; | 
|---|
| 4706 | int err; | 
|---|
| 4707 |  | 
|---|
| 4708 | err = ext4_inode_blocks_set(raw_inode, ei); | 
|---|
| 4709 |  | 
|---|
| 4710 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); | 
|---|
| 4711 | i_uid = i_uid_read(inode); | 
|---|
| 4712 | i_gid = i_gid_read(inode); | 
|---|
| 4713 | i_projid = from_kprojid(to: &init_user_ns, kprojid: ei->i_projid); | 
|---|
| 4714 | if (!(test_opt(inode->i_sb, NO_UID32))) { | 
|---|
| 4715 | raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); | 
|---|
| 4716 | raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); | 
|---|
| 4717 | /* | 
|---|
| 4718 | * Fix up interoperability with old kernels. Otherwise, | 
|---|
| 4719 | * old inodes get re-used with the upper 16 bits of the | 
|---|
| 4720 | * uid/gid intact. | 
|---|
| 4721 | */ | 
|---|
| 4722 | if (ei->i_dtime && !ext4_inode_orphan_tracked(inode)) { | 
|---|
| 4723 | raw_inode->i_uid_high = 0; | 
|---|
| 4724 | raw_inode->i_gid_high = 0; | 
|---|
| 4725 | } else { | 
|---|
| 4726 | raw_inode->i_uid_high = | 
|---|
| 4727 | cpu_to_le16(high_16_bits(i_uid)); | 
|---|
| 4728 | raw_inode->i_gid_high = | 
|---|
| 4729 | cpu_to_le16(high_16_bits(i_gid)); | 
|---|
| 4730 | } | 
|---|
| 4731 | } else { | 
|---|
| 4732 | raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); | 
|---|
| 4733 | raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); | 
|---|
| 4734 | raw_inode->i_uid_high = 0; | 
|---|
| 4735 | raw_inode->i_gid_high = 0; | 
|---|
| 4736 | } | 
|---|
| 4737 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); | 
|---|
| 4738 |  | 
|---|
| 4739 | EXT4_INODE_SET_CTIME(inode, raw_inode); | 
|---|
| 4740 | EXT4_INODE_SET_MTIME(inode, raw_inode); | 
|---|
| 4741 | EXT4_INODE_SET_ATIME(inode, raw_inode); | 
|---|
| 4742 | EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); | 
|---|
| 4743 |  | 
|---|
| 4744 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); | 
|---|
| 4745 | raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); | 
|---|
| 4746 | if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) | 
|---|
| 4747 | raw_inode->i_file_acl_high = | 
|---|
| 4748 | cpu_to_le16(ei->i_file_acl >> 32); | 
|---|
| 4749 | raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); | 
|---|
| 4750 | ext4_isize_set(raw_inode, i_size: ei->i_disksize); | 
|---|
| 4751 |  | 
|---|
| 4752 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); | 
|---|
| 4753 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { | 
|---|
| 4754 | if (old_valid_dev(dev: inode->i_rdev)) { | 
|---|
| 4755 | raw_inode->i_block[0] = | 
|---|
| 4756 | cpu_to_le32(old_encode_dev(inode->i_rdev)); | 
|---|
| 4757 | raw_inode->i_block[1] = 0; | 
|---|
| 4758 | } else { | 
|---|
| 4759 | raw_inode->i_block[0] = 0; | 
|---|
| 4760 | raw_inode->i_block[1] = | 
|---|
| 4761 | cpu_to_le32(new_encode_dev(inode->i_rdev)); | 
|---|
| 4762 | raw_inode->i_block[2] = 0; | 
|---|
| 4763 | } | 
|---|
| 4764 | } else if (!ext4_has_inline_data(inode)) { | 
|---|
| 4765 | for (block = 0; block < EXT4_N_BLOCKS; block++) | 
|---|
| 4766 | raw_inode->i_block[block] = ei->i_data[block]; | 
|---|
| 4767 | } | 
|---|
| 4768 |  | 
|---|
| 4769 | if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { | 
|---|
| 4770 | u64 ivers = ext4_inode_peek_iversion(inode); | 
|---|
| 4771 |  | 
|---|
| 4772 | raw_inode->i_disk_version = cpu_to_le32(ivers); | 
|---|
| 4773 | if (ei->i_extra_isize) { | 
|---|
| 4774 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) | 
|---|
| 4775 | raw_inode->i_version_hi = | 
|---|
| 4776 | cpu_to_le32(ivers >> 32); | 
|---|
| 4777 | raw_inode->i_extra_isize = | 
|---|
| 4778 | cpu_to_le16(ei->i_extra_isize); | 
|---|
| 4779 | } | 
|---|
| 4780 | } | 
|---|
| 4781 |  | 
|---|
| 4782 | if (i_projid != EXT4_DEF_PROJID && | 
|---|
| 4783 | !ext4_has_feature_project(sb: inode->i_sb)) | 
|---|
| 4784 | err = err ?: -EFSCORRUPTED; | 
|---|
| 4785 |  | 
|---|
| 4786 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && | 
|---|
| 4787 | EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) | 
|---|
| 4788 | raw_inode->i_projid = cpu_to_le32(i_projid); | 
|---|
| 4789 |  | 
|---|
| 4790 | ext4_inode_csum_set(inode, raw: raw_inode, ei); | 
|---|
| 4791 | return err; | 
|---|
| 4792 | } | 
|---|
| 4793 |  | 
|---|
| 4794 | /* | 
|---|
| 4795 | * ext4_get_inode_loc returns with an extra refcount against the inode's | 
|---|
| 4796 | * underlying buffer_head on success. If we pass 'inode' and it does not | 
|---|
| 4797 | * have in-inode xattr, we have all inode data in memory that is needed | 
|---|
| 4798 | * to recreate the on-disk version of this inode. | 
|---|
| 4799 | */ | 
|---|
| 4800 | static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, | 
|---|
| 4801 | struct inode *inode, struct ext4_iloc *iloc, | 
|---|
| 4802 | ext4_fsblk_t *ret_block) | 
|---|
| 4803 | { | 
|---|
| 4804 | struct ext4_group_desc	*gdp; | 
|---|
| 4805 | struct buffer_head	*bh; | 
|---|
| 4806 | ext4_fsblk_t		block; | 
|---|
| 4807 | struct blk_plug		plug; | 
|---|
| 4808 | int			inodes_per_block, inode_offset; | 
|---|
| 4809 |  | 
|---|
| 4810 | iloc->bh = NULL; | 
|---|
| 4811 | if (ino < EXT4_ROOT_INO || | 
|---|
| 4812 | ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) | 
|---|
| 4813 | return -EFSCORRUPTED; | 
|---|
| 4814 |  | 
|---|
| 4815 | iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); | 
|---|
| 4816 | gdp = ext4_get_group_desc(sb, block_group: iloc->block_group, NULL); | 
|---|
| 4817 | if (!gdp) | 
|---|
| 4818 | return -EIO; | 
|---|
| 4819 |  | 
|---|
| 4820 | /* | 
|---|
| 4821 | * Figure out the offset within the block group inode table | 
|---|
| 4822 | */ | 
|---|
| 4823 | inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; | 
|---|
| 4824 | inode_offset = ((ino - 1) % | 
|---|
| 4825 | EXT4_INODES_PER_GROUP(sb)); | 
|---|
| 4826 | iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); | 
|---|
| 4827 |  | 
|---|
| 4828 | block = ext4_inode_table(sb, bg: gdp); | 
|---|
| 4829 | if ((block <= le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) || | 
|---|
| 4830 | (block >= ext4_blocks_count(es: EXT4_SB(sb)->s_es))) { | 
|---|
| 4831 | ext4_error(sb, "Invalid inode table block %llu in " | 
|---|
| 4832 | "block_group %u", block, iloc->block_group); | 
|---|
| 4833 | return -EFSCORRUPTED; | 
|---|
| 4834 | } | 
|---|
| 4835 | block += (inode_offset / inodes_per_block); | 
|---|
| 4836 |  | 
|---|
| 4837 | bh = sb_getblk(sb, block); | 
|---|
| 4838 | if (unlikely(!bh)) | 
|---|
| 4839 | return -ENOMEM; | 
|---|
| 4840 | if (ext4_buffer_uptodate(bh)) | 
|---|
| 4841 | goto has_buffer; | 
|---|
| 4842 |  | 
|---|
| 4843 | lock_buffer(bh); | 
|---|
| 4844 | if (ext4_buffer_uptodate(bh)) { | 
|---|
| 4845 | /* Someone brought it uptodate while we waited */ | 
|---|
| 4846 | unlock_buffer(bh); | 
|---|
| 4847 | goto has_buffer; | 
|---|
| 4848 | } | 
|---|
| 4849 |  | 
|---|
| 4850 | /* | 
|---|
| 4851 | * If we have all information of the inode in memory and this | 
|---|
| 4852 | * is the only valid inode in the block, we need not read the | 
|---|
| 4853 | * block. | 
|---|
| 4854 | */ | 
|---|
| 4855 | if (inode && !ext4_test_inode_state(inode, bit: EXT4_STATE_XATTR)) { | 
|---|
| 4856 | struct buffer_head *bitmap_bh; | 
|---|
| 4857 | int i, start; | 
|---|
| 4858 |  | 
|---|
| 4859 | start = inode_offset & ~(inodes_per_block - 1); | 
|---|
| 4860 |  | 
|---|
| 4861 | /* Is the inode bitmap in cache? */ | 
|---|
| 4862 | bitmap_bh = sb_getblk(sb, block: ext4_inode_bitmap(sb, bg: gdp)); | 
|---|
| 4863 | if (unlikely(!bitmap_bh)) | 
|---|
| 4864 | goto make_io; | 
|---|
| 4865 |  | 
|---|
| 4866 | /* | 
|---|
| 4867 | * If the inode bitmap isn't in cache then the | 
|---|
| 4868 | * optimisation may end up performing two reads instead | 
|---|
| 4869 | * of one, so skip it. | 
|---|
| 4870 | */ | 
|---|
| 4871 | if (!buffer_uptodate(bh: bitmap_bh)) { | 
|---|
| 4872 | brelse(bh: bitmap_bh); | 
|---|
| 4873 | goto make_io; | 
|---|
| 4874 | } | 
|---|
| 4875 | for (i = start; i < start + inodes_per_block; i++) { | 
|---|
| 4876 | if (i == inode_offset) | 
|---|
| 4877 | continue; | 
|---|
| 4878 | if (ext4_test_bit(nr: i, addr: bitmap_bh->b_data)) | 
|---|
| 4879 | break; | 
|---|
| 4880 | } | 
|---|
| 4881 | brelse(bh: bitmap_bh); | 
|---|
| 4882 | if (i == start + inodes_per_block) { | 
|---|
| 4883 | struct ext4_inode *raw_inode = | 
|---|
| 4884 | (struct ext4_inode *) (bh->b_data + iloc->offset); | 
|---|
| 4885 |  | 
|---|
| 4886 | /* all other inodes are free, so skip I/O */ | 
|---|
| 4887 | memset(s: bh->b_data, c: 0, n: bh->b_size); | 
|---|
| 4888 | if (!ext4_test_inode_state(inode, bit: EXT4_STATE_NEW)) | 
|---|
| 4889 | ext4_fill_raw_inode(inode, raw_inode); | 
|---|
| 4890 | set_buffer_uptodate(bh); | 
|---|
| 4891 | unlock_buffer(bh); | 
|---|
| 4892 | goto has_buffer; | 
|---|
| 4893 | } | 
|---|
| 4894 | } | 
|---|
| 4895 |  | 
|---|
| 4896 | make_io: | 
|---|
| 4897 | /* | 
|---|
| 4898 | * If we need to do any I/O, try to pre-readahead extra | 
|---|
| 4899 | * blocks from the inode table. | 
|---|
| 4900 | */ | 
|---|
| 4901 | blk_start_plug(&plug); | 
|---|
| 4902 | if (EXT4_SB(sb)->s_inode_readahead_blks) { | 
|---|
| 4903 | ext4_fsblk_t b, end, table; | 
|---|
| 4904 | unsigned num; | 
|---|
| 4905 | __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; | 
|---|
| 4906 |  | 
|---|
| 4907 | table = ext4_inode_table(sb, bg: gdp); | 
|---|
| 4908 | /* s_inode_readahead_blks is always a power of 2 */ | 
|---|
| 4909 | b = block & ~((ext4_fsblk_t) ra_blks - 1); | 
|---|
| 4910 | if (table > b) | 
|---|
| 4911 | b = table; | 
|---|
| 4912 | end = b + ra_blks; | 
|---|
| 4913 | num = EXT4_INODES_PER_GROUP(sb); | 
|---|
| 4914 | if (ext4_has_group_desc_csum(sb)) | 
|---|
| 4915 | num -= ext4_itable_unused_count(sb, bg: gdp); | 
|---|
| 4916 | table += num / inodes_per_block; | 
|---|
| 4917 | if (end > table) | 
|---|
| 4918 | end = table; | 
|---|
| 4919 | while (b <= end) | 
|---|
| 4920 | ext4_sb_breadahead_unmovable(sb, block: b++); | 
|---|
| 4921 | } | 
|---|
| 4922 |  | 
|---|
| 4923 | /* | 
|---|
| 4924 | * There are other valid inodes in the buffer, this inode | 
|---|
| 4925 | * has in-inode xattrs, or we don't have this inode in memory. | 
|---|
| 4926 | * Read the block from disk. | 
|---|
| 4927 | */ | 
|---|
| 4928 | trace_ext4_load_inode(sb, ino); | 
|---|
| 4929 | ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL, | 
|---|
| 4930 | simu_fail: ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO)); | 
|---|
| 4931 | blk_finish_plug(&plug); | 
|---|
| 4932 | wait_on_buffer(bh); | 
|---|
| 4933 | if (!buffer_uptodate(bh)) { | 
|---|
| 4934 | if (ret_block) | 
|---|
| 4935 | *ret_block = block; | 
|---|
| 4936 | brelse(bh); | 
|---|
| 4937 | return -EIO; | 
|---|
| 4938 | } | 
|---|
| 4939 | has_buffer: | 
|---|
| 4940 | iloc->bh = bh; | 
|---|
| 4941 | return 0; | 
|---|
| 4942 | } | 
|---|
| 4943 |  | 
|---|
| 4944 | static int __ext4_get_inode_loc_noinmem(struct inode *inode, | 
|---|
| 4945 | struct ext4_iloc *iloc) | 
|---|
| 4946 | { | 
|---|
| 4947 | ext4_fsblk_t err_blk = 0; | 
|---|
| 4948 | int ret; | 
|---|
| 4949 |  | 
|---|
| 4950 | ret = __ext4_get_inode_loc(sb: inode->i_sb, ino: inode->i_ino, NULL, iloc, | 
|---|
| 4951 | ret_block: &err_blk); | 
|---|
| 4952 |  | 
|---|
| 4953 | if (ret == -EIO) | 
|---|
| 4954 | ext4_error_inode_block(inode, err_blk, EIO, | 
|---|
| 4955 | "unable to read itable block"); | 
|---|
| 4956 |  | 
|---|
| 4957 | return ret; | 
|---|
| 4958 | } | 
|---|
| 4959 |  | 
|---|
| 4960 | int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) | 
|---|
| 4961 | { | 
|---|
| 4962 | ext4_fsblk_t err_blk = 0; | 
|---|
| 4963 | int ret; | 
|---|
| 4964 |  | 
|---|
| 4965 | ret = __ext4_get_inode_loc(sb: inode->i_sb, ino: inode->i_ino, inode, iloc, | 
|---|
| 4966 | ret_block: &err_blk); | 
|---|
| 4967 |  | 
|---|
| 4968 | if (ret == -EIO) | 
|---|
| 4969 | ext4_error_inode_block(inode, err_blk, EIO, | 
|---|
| 4970 | "unable to read itable block"); | 
|---|
| 4971 |  | 
|---|
| 4972 | return ret; | 
|---|
| 4973 | } | 
|---|
| 4974 |  | 
|---|
| 4975 |  | 
|---|
| 4976 | int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino, | 
|---|
| 4977 | struct ext4_iloc *iloc) | 
|---|
| 4978 | { | 
|---|
| 4979 | return __ext4_get_inode_loc(sb, ino, NULL, iloc, NULL); | 
|---|
| 4980 | } | 
|---|
| 4981 |  | 
|---|
| 4982 | static bool ext4_should_enable_dax(struct inode *inode) | 
|---|
| 4983 | { | 
|---|
| 4984 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 4985 |  | 
|---|
| 4986 | if (test_opt2(inode->i_sb, DAX_NEVER)) | 
|---|
| 4987 | return false; | 
|---|
| 4988 | if (!S_ISREG(inode->i_mode)) | 
|---|
| 4989 | return false; | 
|---|
| 4990 | if (ext4_should_journal_data(inode)) | 
|---|
| 4991 | return false; | 
|---|
| 4992 | if (ext4_has_inline_data(inode)) | 
|---|
| 4993 | return false; | 
|---|
| 4994 | if (ext4_test_inode_flag(inode, bit: EXT4_INODE_ENCRYPT)) | 
|---|
| 4995 | return false; | 
|---|
| 4996 | if (ext4_test_inode_flag(inode, bit: EXT4_INODE_VERITY)) | 
|---|
| 4997 | return false; | 
|---|
| 4998 | if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) | 
|---|
| 4999 | return false; | 
|---|
| 5000 | if (test_opt(inode->i_sb, DAX_ALWAYS)) | 
|---|
| 5001 | return true; | 
|---|
| 5002 |  | 
|---|
| 5003 | return ext4_test_inode_flag(inode, bit: EXT4_INODE_DAX); | 
|---|
| 5004 | } | 
|---|
| 5005 |  | 
|---|
| 5006 | void ext4_set_inode_flags(struct inode *inode, bool init) | 
|---|
| 5007 | { | 
|---|
| 5008 | unsigned int flags = EXT4_I(inode)->i_flags; | 
|---|
| 5009 | unsigned int new_fl = 0; | 
|---|
| 5010 |  | 
|---|
| 5011 | WARN_ON_ONCE(IS_DAX(inode) && init); | 
|---|
| 5012 |  | 
|---|
| 5013 | if (flags & EXT4_SYNC_FL) | 
|---|
| 5014 | new_fl |= S_SYNC; | 
|---|
| 5015 | if (flags & EXT4_APPEND_FL) | 
|---|
| 5016 | new_fl |= S_APPEND; | 
|---|
| 5017 | if (flags & EXT4_IMMUTABLE_FL) | 
|---|
| 5018 | new_fl |= S_IMMUTABLE; | 
|---|
| 5019 | if (flags & EXT4_NOATIME_FL) | 
|---|
| 5020 | new_fl |= S_NOATIME; | 
|---|
| 5021 | if (flags & EXT4_DIRSYNC_FL) | 
|---|
| 5022 | new_fl |= S_DIRSYNC; | 
|---|
| 5023 |  | 
|---|
| 5024 | /* Because of the way inode_set_flags() works we must preserve S_DAX | 
|---|
| 5025 | * here if already set. */ | 
|---|
| 5026 | new_fl |= (inode->i_flags & S_DAX); | 
|---|
| 5027 | if (init && ext4_should_enable_dax(inode)) | 
|---|
| 5028 | new_fl |= S_DAX; | 
|---|
| 5029 |  | 
|---|
| 5030 | if (flags & EXT4_ENCRYPT_FL) | 
|---|
| 5031 | new_fl |= S_ENCRYPTED; | 
|---|
| 5032 | if (flags & EXT4_CASEFOLD_FL) | 
|---|
| 5033 | new_fl |= S_CASEFOLD; | 
|---|
| 5034 | if (flags & EXT4_VERITY_FL) | 
|---|
| 5035 | new_fl |= S_VERITY; | 
|---|
| 5036 | inode_set_flags(inode, flags: new_fl, | 
|---|
| 5037 | S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| | 
|---|
| 5038 | S_ENCRYPTED|S_CASEFOLD|S_VERITY); | 
|---|
| 5039 | } | 
|---|
| 5040 |  | 
|---|
| 5041 | static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, | 
|---|
| 5042 | struct ext4_inode_info *ei) | 
|---|
| 5043 | { | 
|---|
| 5044 | blkcnt_t i_blocks ; | 
|---|
| 5045 | struct inode *inode = &(ei->vfs_inode); | 
|---|
| 5046 | struct super_block *sb = inode->i_sb; | 
|---|
| 5047 |  | 
|---|
| 5048 | if (ext4_has_feature_huge_file(sb)) { | 
|---|
| 5049 | /* we are using combined 48 bit field */ | 
|---|
| 5050 | i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | | 
|---|
| 5051 | le32_to_cpu(raw_inode->i_blocks_lo); | 
|---|
| 5052 | if (ext4_test_inode_flag(inode, bit: EXT4_INODE_HUGE_FILE)) { | 
|---|
| 5053 | /* i_blocks represent file system block size */ | 
|---|
| 5054 | return i_blocks  << (inode->i_blkbits - 9); | 
|---|
| 5055 | } else { | 
|---|
| 5056 | return i_blocks; | 
|---|
| 5057 | } | 
|---|
| 5058 | } else { | 
|---|
| 5059 | return le32_to_cpu(raw_inode->i_blocks_lo); | 
|---|
| 5060 | } | 
|---|
| 5061 | } | 
|---|
| 5062 |  | 
|---|
| 5063 | static inline int (struct inode *inode, | 
|---|
| 5064 | struct ext4_inode *raw_inode, | 
|---|
| 5065 | struct ext4_inode_info *ei) | 
|---|
| 5066 | { | 
|---|
| 5067 | __le32 *magic = (void *)raw_inode + | 
|---|
| 5068 | EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; | 
|---|
| 5069 |  | 
|---|
| 5070 | if (EXT4_INODE_HAS_XATTR_SPACE(inode)  && | 
|---|
| 5071 | *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { | 
|---|
| 5072 | int err; | 
|---|
| 5073 |  | 
|---|
| 5074 | err = xattr_check_inode(inode, IHDR(inode, raw_inode), | 
|---|
| 5075 | ITAIL(inode, raw_inode)); | 
|---|
| 5076 | if (err) | 
|---|
| 5077 | return err; | 
|---|
| 5078 |  | 
|---|
| 5079 | ext4_set_inode_state(inode, bit: EXT4_STATE_XATTR); | 
|---|
| 5080 | err = ext4_find_inline_data_nolock(inode); | 
|---|
| 5081 | if (!err && ext4_has_inline_data(inode)) | 
|---|
| 5082 | ext4_set_inode_state(inode, bit: EXT4_STATE_MAY_INLINE_DATA); | 
|---|
| 5083 | return err; | 
|---|
| 5084 | } else | 
|---|
| 5085 | EXT4_I(inode)->i_inline_off = 0; | 
|---|
| 5086 | return 0; | 
|---|
| 5087 | } | 
|---|
| 5088 |  | 
|---|
| 5089 | int ext4_get_projid(struct inode *inode, kprojid_t *projid) | 
|---|
| 5090 | { | 
|---|
| 5091 | if (!ext4_has_feature_project(sb: inode->i_sb)) | 
|---|
| 5092 | return -EOPNOTSUPP; | 
|---|
| 5093 | *projid = EXT4_I(inode)->i_projid; | 
|---|
| 5094 | return 0; | 
|---|
| 5095 | } | 
|---|
| 5096 |  | 
|---|
| 5097 | /* | 
|---|
| 5098 | * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of | 
|---|
| 5099 | * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag | 
|---|
| 5100 | * set. | 
|---|
| 5101 | */ | 
|---|
| 5102 | static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) | 
|---|
| 5103 | { | 
|---|
| 5104 | if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) | 
|---|
| 5105 | inode_set_iversion_raw(inode, val); | 
|---|
| 5106 | else | 
|---|
| 5107 | inode_set_iversion_queried(inode, val); | 
|---|
| 5108 | } | 
|---|
| 5109 |  | 
|---|
| 5110 | static int check_igot_inode(struct inode *inode, ext4_iget_flags flags, | 
|---|
| 5111 | const char *function, unsigned int line) | 
|---|
| 5112 | { | 
|---|
| 5113 | const char *err_str; | 
|---|
| 5114 |  | 
|---|
| 5115 | if (flags & EXT4_IGET_EA_INODE) { | 
|---|
| 5116 | if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { | 
|---|
| 5117 | err_str = "missing EA_INODE flag"; | 
|---|
| 5118 | goto error; | 
|---|
| 5119 | } | 
|---|
| 5120 | if (ext4_test_inode_state(inode, bit: EXT4_STATE_XATTR) || | 
|---|
| 5121 | EXT4_I(inode)->i_file_acl) { | 
|---|
| 5122 | err_str = "ea_inode with extended attributes"; | 
|---|
| 5123 | goto error; | 
|---|
| 5124 | } | 
|---|
| 5125 | } else { | 
|---|
| 5126 | if ((EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { | 
|---|
| 5127 | /* | 
|---|
| 5128 | * open_by_handle_at() could provide an old inode number | 
|---|
| 5129 | * that has since been reused for an ea_inode; this does | 
|---|
| 5130 | * not indicate filesystem corruption | 
|---|
| 5131 | */ | 
|---|
| 5132 | if (flags & EXT4_IGET_HANDLE) | 
|---|
| 5133 | return -ESTALE; | 
|---|
| 5134 | err_str = "unexpected EA_INODE flag"; | 
|---|
| 5135 | goto error; | 
|---|
| 5136 | } | 
|---|
| 5137 | } | 
|---|
| 5138 | if (is_bad_inode(inode) && !(flags & EXT4_IGET_BAD)) { | 
|---|
| 5139 | err_str = "unexpected bad inode w/o EXT4_IGET_BAD"; | 
|---|
| 5140 | goto error; | 
|---|
| 5141 | } | 
|---|
| 5142 | return 0; | 
|---|
| 5143 |  | 
|---|
| 5144 | error: | 
|---|
| 5145 | ext4_error_inode(inode, function, line, 0, "%s", err_str); | 
|---|
| 5146 | return -EFSCORRUPTED; | 
|---|
| 5147 | } | 
|---|
| 5148 |  | 
|---|
| 5149 | static bool ext4_should_enable_large_folio(struct inode *inode) | 
|---|
| 5150 | { | 
|---|
| 5151 | struct super_block *sb = inode->i_sb; | 
|---|
| 5152 |  | 
|---|
| 5153 | if (!S_ISREG(inode->i_mode)) | 
|---|
| 5154 | return false; | 
|---|
| 5155 | if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || | 
|---|
| 5156 | ext4_test_inode_flag(inode, bit: EXT4_INODE_JOURNAL_DATA)) | 
|---|
| 5157 | return false; | 
|---|
| 5158 | if (ext4_has_feature_verity(sb)) | 
|---|
| 5159 | return false; | 
|---|
| 5160 | if (ext4_has_feature_encrypt(sb)) | 
|---|
| 5161 | return false; | 
|---|
| 5162 |  | 
|---|
| 5163 | return true; | 
|---|
| 5164 | } | 
|---|
| 5165 |  | 
|---|
| 5166 | /* | 
|---|
| 5167 | * Limit the maximum folio order to 2048 blocks to prevent overestimation | 
|---|
| 5168 | * of reserve handle credits during the folio writeback in environments | 
|---|
| 5169 | * where the PAGE_SIZE exceeds 4KB. | 
|---|
| 5170 | */ | 
|---|
| 5171 | #define EXT4_MAX_PAGECACHE_ORDER(i)		\ | 
|---|
| 5172 | umin(MAX_PAGECACHE_ORDER, (11 + (i)->i_blkbits - PAGE_SHIFT)) | 
|---|
| 5173 | void ext4_set_inode_mapping_order(struct inode *inode) | 
|---|
| 5174 | { | 
|---|
| 5175 | if (!ext4_should_enable_large_folio(inode)) | 
|---|
| 5176 | return; | 
|---|
| 5177 |  | 
|---|
| 5178 | mapping_set_folio_order_range(mapping: inode->i_mapping, min: 0, | 
|---|
| 5179 | EXT4_MAX_PAGECACHE_ORDER(inode)); | 
|---|
| 5180 | } | 
|---|
| 5181 |  | 
|---|
| 5182 | struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, | 
|---|
| 5183 | ext4_iget_flags flags, const char *function, | 
|---|
| 5184 | unsigned int line) | 
|---|
| 5185 | { | 
|---|
| 5186 | struct ext4_iloc iloc; | 
|---|
| 5187 | struct ext4_inode *raw_inode; | 
|---|
| 5188 | struct ext4_inode_info *ei; | 
|---|
| 5189 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; | 
|---|
| 5190 | struct inode *inode; | 
|---|
| 5191 | journal_t *journal = EXT4_SB(sb)->s_journal; | 
|---|
| 5192 | long ret; | 
|---|
| 5193 | loff_t size; | 
|---|
| 5194 | int block; | 
|---|
| 5195 | uid_t i_uid; | 
|---|
| 5196 | gid_t i_gid; | 
|---|
| 5197 | projid_t i_projid; | 
|---|
| 5198 |  | 
|---|
| 5199 | if ((!(flags & EXT4_IGET_SPECIAL) && is_special_ino(sb, ino)) || | 
|---|
| 5200 | (ino < EXT4_ROOT_INO) || | 
|---|
| 5201 | (ino > le32_to_cpu(es->s_inodes_count))) { | 
|---|
| 5202 | if (flags & EXT4_IGET_HANDLE) | 
|---|
| 5203 | return ERR_PTR(error: -ESTALE); | 
|---|
| 5204 | __ext4_error(sb, function, line, false, EFSCORRUPTED, 0, | 
|---|
| 5205 | "inode #%lu: comm %s: iget: illegal inode #", | 
|---|
| 5206 | ino, current->comm); | 
|---|
| 5207 | return ERR_PTR(error: -EFSCORRUPTED); | 
|---|
| 5208 | } | 
|---|
| 5209 |  | 
|---|
| 5210 | inode = iget_locked(sb, ino); | 
|---|
| 5211 | if (!inode) | 
|---|
| 5212 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 5213 | if (!(inode->i_state & I_NEW)) { | 
|---|
| 5214 | ret = check_igot_inode(inode, flags, function, line); | 
|---|
| 5215 | if (ret) { | 
|---|
| 5216 | iput(inode); | 
|---|
| 5217 | return ERR_PTR(error: ret); | 
|---|
| 5218 | } | 
|---|
| 5219 | return inode; | 
|---|
| 5220 | } | 
|---|
| 5221 |  | 
|---|
| 5222 | ei = EXT4_I(inode); | 
|---|
| 5223 | iloc.bh = NULL; | 
|---|
| 5224 |  | 
|---|
| 5225 | ret = __ext4_get_inode_loc_noinmem(inode, iloc: &iloc); | 
|---|
| 5226 | if (ret < 0) | 
|---|
| 5227 | goto bad_inode; | 
|---|
| 5228 | raw_inode = ext4_raw_inode(iloc: &iloc); | 
|---|
| 5229 |  | 
|---|
| 5230 | if ((flags & EXT4_IGET_HANDLE) && | 
|---|
| 5231 | (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { | 
|---|
| 5232 | ret = -ESTALE; | 
|---|
| 5233 | goto bad_inode; | 
|---|
| 5234 | } | 
|---|
| 5235 |  | 
|---|
| 5236 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { | 
|---|
| 5237 | ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); | 
|---|
| 5238 | if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > | 
|---|
| 5239 | EXT4_INODE_SIZE(inode->i_sb) || | 
|---|
| 5240 | (ei->i_extra_isize & 3)) { | 
|---|
| 5241 | ext4_error_inode(inode, function, line, 0, | 
|---|
| 5242 | "iget: bad extra_isize %u " | 
|---|
| 5243 | "(inode size %u)", | 
|---|
| 5244 | ei->i_extra_isize, | 
|---|
| 5245 | EXT4_INODE_SIZE(inode->i_sb)); | 
|---|
| 5246 | ret = -EFSCORRUPTED; | 
|---|
| 5247 | goto bad_inode; | 
|---|
| 5248 | } | 
|---|
| 5249 | } else | 
|---|
| 5250 | ei->i_extra_isize = 0; | 
|---|
| 5251 |  | 
|---|
| 5252 | /* Precompute checksum seed for inode metadata */ | 
|---|
| 5253 | if (ext4_has_feature_metadata_csum(sb)) { | 
|---|
| 5254 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 5255 | __u32 csum; | 
|---|
| 5256 | __le32 inum = cpu_to_le32(inode->i_ino); | 
|---|
| 5257 | __le32 gen = raw_inode->i_generation; | 
|---|
| 5258 | csum = ext4_chksum(crc: sbi->s_csum_seed, address: (__u8 *)&inum, | 
|---|
| 5259 | length: sizeof(inum)); | 
|---|
| 5260 | ei->i_csum_seed = ext4_chksum(crc: csum, address: (__u8 *)&gen, length: sizeof(gen)); | 
|---|
| 5261 | } | 
|---|
| 5262 |  | 
|---|
| 5263 | if ((!ext4_inode_csum_verify(inode, raw: raw_inode, ei) || | 
|---|
| 5264 | ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) && | 
|---|
| 5265 | (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) { | 
|---|
| 5266 | ext4_error_inode_err(inode, function, line, 0, | 
|---|
| 5267 | EFSBADCRC, "iget: checksum invalid"); | 
|---|
| 5268 | ret = -EFSBADCRC; | 
|---|
| 5269 | goto bad_inode; | 
|---|
| 5270 | } | 
|---|
| 5271 |  | 
|---|
| 5272 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); | 
|---|
| 5273 | i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); | 
|---|
| 5274 | i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); | 
|---|
| 5275 | if (ext4_has_feature_project(sb) && | 
|---|
| 5276 | EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && | 
|---|
| 5277 | EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) | 
|---|
| 5278 | i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); | 
|---|
| 5279 | else | 
|---|
| 5280 | i_projid = EXT4_DEF_PROJID; | 
|---|
| 5281 |  | 
|---|
| 5282 | if (!(test_opt(inode->i_sb, NO_UID32))) { | 
|---|
| 5283 | i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; | 
|---|
| 5284 | i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; | 
|---|
| 5285 | } | 
|---|
| 5286 | i_uid_write(inode, uid: i_uid); | 
|---|
| 5287 | i_gid_write(inode, gid: i_gid); | 
|---|
| 5288 | ei->i_projid = make_kprojid(from: &init_user_ns, projid: i_projid); | 
|---|
| 5289 | set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); | 
|---|
| 5290 |  | 
|---|
| 5291 | ext4_clear_state_flags(ei);	/* Only relevant on 32-bit archs */ | 
|---|
| 5292 | ei->i_inline_off = 0; | 
|---|
| 5293 | ei->i_dir_start_lookup = 0; | 
|---|
| 5294 | ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); | 
|---|
| 5295 | /* We now have enough fields to check if the inode was active or not. | 
|---|
| 5296 | * This is needed because nfsd might try to access dead inodes | 
|---|
| 5297 | * the test is that same one that e2fsck uses | 
|---|
| 5298 | * NeilBrown 1999oct15 | 
|---|
| 5299 | */ | 
|---|
| 5300 | if (inode->i_nlink == 0) { | 
|---|
| 5301 | if ((inode->i_mode == 0 || flags & EXT4_IGET_SPECIAL || | 
|---|
| 5302 | !(EXT4_SB(sb: inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && | 
|---|
| 5303 | ino != EXT4_BOOT_LOADER_INO) { | 
|---|
| 5304 | /* this inode is deleted or unallocated */ | 
|---|
| 5305 | if (flags & EXT4_IGET_SPECIAL) { | 
|---|
| 5306 | ext4_error_inode(inode, function, line, 0, | 
|---|
| 5307 | "iget: special inode unallocated"); | 
|---|
| 5308 | ret = -EFSCORRUPTED; | 
|---|
| 5309 | } else | 
|---|
| 5310 | ret = -ESTALE; | 
|---|
| 5311 | goto bad_inode; | 
|---|
| 5312 | } | 
|---|
| 5313 | /* The only unlinked inodes we let through here have | 
|---|
| 5314 | * valid i_mode and are being read by the orphan | 
|---|
| 5315 | * recovery code: that's fine, we're about to complete | 
|---|
| 5316 | * the process of deleting those. | 
|---|
| 5317 | * OR it is the EXT4_BOOT_LOADER_INO which is | 
|---|
| 5318 | * not initialized on a new filesystem. */ | 
|---|
| 5319 | } | 
|---|
| 5320 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); | 
|---|
| 5321 | ext4_set_inode_flags(inode, init: true); | 
|---|
| 5322 | inode->i_blocks = ext4_inode_blocks(raw_inode, ei); | 
|---|
| 5323 | ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); | 
|---|
| 5324 | if (ext4_has_feature_64bit(sb)) | 
|---|
| 5325 | ei->i_file_acl |= | 
|---|
| 5326 | ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; | 
|---|
| 5327 | inode->i_size = ext4_isize(sb, raw_inode); | 
|---|
| 5328 | size = i_size_read(inode); | 
|---|
| 5329 | if (size < 0 || size > ext4_get_maxbytes(inode)) { | 
|---|
| 5330 | ext4_error_inode(inode, function, line, 0, | 
|---|
| 5331 | "iget: bad i_size value: %lld", size); | 
|---|
| 5332 | ret = -EFSCORRUPTED; | 
|---|
| 5333 | goto bad_inode; | 
|---|
| 5334 | } | 
|---|
| 5335 | /* | 
|---|
| 5336 | * If dir_index is not enabled but there's dir with INDEX flag set, | 
|---|
| 5337 | * we'd normally treat htree data as empty space. But with metadata | 
|---|
| 5338 | * checksumming that corrupts checksums so forbid that. | 
|---|
| 5339 | */ | 
|---|
| 5340 | if (!ext4_has_feature_dir_index(sb) && | 
|---|
| 5341 | ext4_has_feature_metadata_csum(sb) && | 
|---|
| 5342 | ext4_test_inode_flag(inode, bit: EXT4_INODE_INDEX)) { | 
|---|
| 5343 | ext4_error_inode(inode, function, line, 0, | 
|---|
| 5344 | "iget: Dir with htree data on filesystem without dir_index feature."); | 
|---|
| 5345 | ret = -EFSCORRUPTED; | 
|---|
| 5346 | goto bad_inode; | 
|---|
| 5347 | } | 
|---|
| 5348 | ei->i_disksize = inode->i_size; | 
|---|
| 5349 | #ifdef CONFIG_QUOTA | 
|---|
| 5350 | ei->i_reserved_quota = 0; | 
|---|
| 5351 | #endif | 
|---|
| 5352 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); | 
|---|
| 5353 | ei->i_block_group = iloc.block_group; | 
|---|
| 5354 | ei->i_last_alloc_group = ~0; | 
|---|
| 5355 | /* | 
|---|
| 5356 | * NOTE! The in-memory inode i_data array is in little-endian order | 
|---|
| 5357 | * even on big-endian machines: we do NOT byteswap the block numbers! | 
|---|
| 5358 | */ | 
|---|
| 5359 | for (block = 0; block < EXT4_N_BLOCKS; block++) | 
|---|
| 5360 | ei->i_data[block] = raw_inode->i_block[block]; | 
|---|
| 5361 | INIT_LIST_HEAD(list: &ei->i_orphan); | 
|---|
| 5362 | ext4_fc_init_inode(inode: &ei->vfs_inode); | 
|---|
| 5363 |  | 
|---|
| 5364 | /* | 
|---|
| 5365 | * Set transaction id's of transactions that have to be committed | 
|---|
| 5366 | * to finish f[data]sync. We set them to currently running transaction | 
|---|
| 5367 | * as we cannot be sure that the inode or some of its metadata isn't | 
|---|
| 5368 | * part of the transaction - the inode could have been reclaimed and | 
|---|
| 5369 | * now it is reread from disk. | 
|---|
| 5370 | */ | 
|---|
| 5371 | if (journal) { | 
|---|
| 5372 | transaction_t *transaction; | 
|---|
| 5373 | tid_t tid; | 
|---|
| 5374 |  | 
|---|
| 5375 | read_lock(&journal->j_state_lock); | 
|---|
| 5376 | if (journal->j_running_transaction) | 
|---|
| 5377 | transaction = journal->j_running_transaction; | 
|---|
| 5378 | else | 
|---|
| 5379 | transaction = journal->j_committing_transaction; | 
|---|
| 5380 | if (transaction) | 
|---|
| 5381 | tid = transaction->t_tid; | 
|---|
| 5382 | else | 
|---|
| 5383 | tid = journal->j_commit_sequence; | 
|---|
| 5384 | read_unlock(&journal->j_state_lock); | 
|---|
| 5385 | ei->i_sync_tid = tid; | 
|---|
| 5386 | ei->i_datasync_tid = tid; | 
|---|
| 5387 | } | 
|---|
| 5388 |  | 
|---|
| 5389 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { | 
|---|
| 5390 | if (ei->i_extra_isize == 0) { | 
|---|
| 5391 | /* The extra space is currently unused. Use it. */ | 
|---|
| 5392 | BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); | 
|---|
| 5393 | ei->i_extra_isize = sizeof(struct ext4_inode) - | 
|---|
| 5394 | EXT4_GOOD_OLD_INODE_SIZE; | 
|---|
| 5395 | } else { | 
|---|
| 5396 | ret = ext4_iget_extra_inode(inode, raw_inode, ei); | 
|---|
| 5397 | if (ret) | 
|---|
| 5398 | goto bad_inode; | 
|---|
| 5399 | } | 
|---|
| 5400 | } | 
|---|
| 5401 |  | 
|---|
| 5402 | EXT4_INODE_GET_CTIME(inode, raw_inode); | 
|---|
| 5403 | EXT4_INODE_GET_ATIME(inode, raw_inode); | 
|---|
| 5404 | EXT4_INODE_GET_MTIME(inode, raw_inode); | 
|---|
| 5405 | EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); | 
|---|
| 5406 |  | 
|---|
| 5407 | if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { | 
|---|
| 5408 | u64 ivers = le32_to_cpu(raw_inode->i_disk_version); | 
|---|
| 5409 |  | 
|---|
| 5410 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { | 
|---|
| 5411 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) | 
|---|
| 5412 | ivers |= | 
|---|
| 5413 | (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; | 
|---|
| 5414 | } | 
|---|
| 5415 | ext4_inode_set_iversion_queried(inode, val: ivers); | 
|---|
| 5416 | } | 
|---|
| 5417 |  | 
|---|
| 5418 | ret = 0; | 
|---|
| 5419 | if (ei->i_file_acl && | 
|---|
| 5420 | !ext4_inode_block_valid(inode, start_blk: ei->i_file_acl, count: 1)) { | 
|---|
| 5421 | ext4_error_inode(inode, function, line, 0, | 
|---|
| 5422 | "iget: bad extended attribute block %llu", | 
|---|
| 5423 | ei->i_file_acl); | 
|---|
| 5424 | ret = -EFSCORRUPTED; | 
|---|
| 5425 | goto bad_inode; | 
|---|
| 5426 | } else if (!ext4_has_inline_data(inode)) { | 
|---|
| 5427 | /* validate the block references in the inode */ | 
|---|
| 5428 | if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) && | 
|---|
| 5429 | (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | 
|---|
| 5430 | (S_ISLNK(inode->i_mode) && | 
|---|
| 5431 | !ext4_inode_is_fast_symlink(inode)))) { | 
|---|
| 5432 | if (ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS)) | 
|---|
| 5433 | ret = ext4_ext_check_inode(inode); | 
|---|
| 5434 | else | 
|---|
| 5435 | ret = ext4_ind_check_inode(inode); | 
|---|
| 5436 | } | 
|---|
| 5437 | } | 
|---|
| 5438 | if (ret) | 
|---|
| 5439 | goto bad_inode; | 
|---|
| 5440 |  | 
|---|
| 5441 | if (S_ISREG(inode->i_mode)) { | 
|---|
| 5442 | inode->i_op = &ext4_file_inode_operations; | 
|---|
| 5443 | inode->i_fop = &ext4_file_operations; | 
|---|
| 5444 | ext4_set_aops(inode); | 
|---|
| 5445 | } else if (S_ISDIR(inode->i_mode)) { | 
|---|
| 5446 | inode->i_op = &ext4_dir_inode_operations; | 
|---|
| 5447 | inode->i_fop = &ext4_dir_operations; | 
|---|
| 5448 | } else if (S_ISLNK(inode->i_mode)) { | 
|---|
| 5449 | /* VFS does not allow setting these so must be corruption */ | 
|---|
| 5450 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { | 
|---|
| 5451 | ext4_error_inode(inode, function, line, 0, | 
|---|
| 5452 | "iget: immutable or append flags " | 
|---|
| 5453 | "not allowed on symlinks"); | 
|---|
| 5454 | ret = -EFSCORRUPTED; | 
|---|
| 5455 | goto bad_inode; | 
|---|
| 5456 | } | 
|---|
| 5457 | if (IS_ENCRYPTED(inode)) { | 
|---|
| 5458 | inode->i_op = &ext4_encrypted_symlink_inode_operations; | 
|---|
| 5459 | } else if (ext4_inode_is_fast_symlink(inode)) { | 
|---|
| 5460 | inode->i_op = &ext4_fast_symlink_inode_operations; | 
|---|
| 5461 | if (inode->i_size == 0 || | 
|---|
| 5462 | inode->i_size >= sizeof(ei->i_data) || | 
|---|
| 5463 | strnlen((char *)ei->i_data, inode->i_size + 1) != | 
|---|
| 5464 | inode->i_size) { | 
|---|
| 5465 | ext4_error_inode(inode, function, line, 0, | 
|---|
| 5466 | "invalid fast symlink length %llu", | 
|---|
| 5467 | (unsigned long long)inode->i_size); | 
|---|
| 5468 | ret = -EFSCORRUPTED; | 
|---|
| 5469 | goto bad_inode; | 
|---|
| 5470 | } | 
|---|
| 5471 | inode_set_cached_link(inode, link: (char *)ei->i_data, | 
|---|
| 5472 | linklen: inode->i_size); | 
|---|
| 5473 | } else { | 
|---|
| 5474 | inode->i_op = &ext4_symlink_inode_operations; | 
|---|
| 5475 | } | 
|---|
| 5476 | } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || | 
|---|
| 5477 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { | 
|---|
| 5478 | inode->i_op = &ext4_special_inode_operations; | 
|---|
| 5479 | if (raw_inode->i_block[0]) | 
|---|
| 5480 | init_special_inode(inode, inode->i_mode, | 
|---|
| 5481 | old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); | 
|---|
| 5482 | else | 
|---|
| 5483 | init_special_inode(inode, inode->i_mode, | 
|---|
| 5484 | new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); | 
|---|
| 5485 | } else if (ino == EXT4_BOOT_LOADER_INO) { | 
|---|
| 5486 | make_bad_inode(inode); | 
|---|
| 5487 | } else { | 
|---|
| 5488 | ret = -EFSCORRUPTED; | 
|---|
| 5489 | ext4_error_inode(inode, function, line, 0, | 
|---|
| 5490 | "iget: bogus i_mode (%o)", inode->i_mode); | 
|---|
| 5491 | goto bad_inode; | 
|---|
| 5492 | } | 
|---|
| 5493 | if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(sb: inode->i_sb)) { | 
|---|
| 5494 | ext4_error_inode(inode, function, line, 0, | 
|---|
| 5495 | "casefold flag without casefold feature"); | 
|---|
| 5496 | ret = -EFSCORRUPTED; | 
|---|
| 5497 | goto bad_inode; | 
|---|
| 5498 | } | 
|---|
| 5499 |  | 
|---|
| 5500 | ext4_set_inode_mapping_order(inode); | 
|---|
| 5501 |  | 
|---|
| 5502 | ret = check_igot_inode(inode, flags, function, line); | 
|---|
| 5503 | /* | 
|---|
| 5504 | * -ESTALE here means there is nothing inherently wrong with the inode, | 
|---|
| 5505 | * it's just not an inode we can return for an fhandle lookup. | 
|---|
| 5506 | */ | 
|---|
| 5507 | if (ret == -ESTALE) { | 
|---|
| 5508 | brelse(bh: iloc.bh); | 
|---|
| 5509 | unlock_new_inode(inode); | 
|---|
| 5510 | iput(inode); | 
|---|
| 5511 | return ERR_PTR(error: -ESTALE); | 
|---|
| 5512 | } | 
|---|
| 5513 | if (ret) | 
|---|
| 5514 | goto bad_inode; | 
|---|
| 5515 | brelse(bh: iloc.bh); | 
|---|
| 5516 |  | 
|---|
| 5517 | unlock_new_inode(inode); | 
|---|
| 5518 | return inode; | 
|---|
| 5519 |  | 
|---|
| 5520 | bad_inode: | 
|---|
| 5521 | brelse(bh: iloc.bh); | 
|---|
| 5522 | iget_failed(inode); | 
|---|
| 5523 | return ERR_PTR(error: ret); | 
|---|
| 5524 | } | 
|---|
| 5525 |  | 
|---|
| 5526 | static void __ext4_update_other_inode_time(struct super_block *sb, | 
|---|
| 5527 | unsigned long orig_ino, | 
|---|
| 5528 | unsigned long ino, | 
|---|
| 5529 | struct ext4_inode *raw_inode) | 
|---|
| 5530 | { | 
|---|
| 5531 | struct inode *inode; | 
|---|
| 5532 |  | 
|---|
| 5533 | inode = find_inode_by_ino_rcu(sb, ino); | 
|---|
| 5534 | if (!inode) | 
|---|
| 5535 | return; | 
|---|
| 5536 |  | 
|---|
| 5537 | if (!inode_is_dirtytime_only(inode)) | 
|---|
| 5538 | return; | 
|---|
| 5539 |  | 
|---|
| 5540 | spin_lock(lock: &inode->i_lock); | 
|---|
| 5541 | if (inode_is_dirtytime_only(inode)) { | 
|---|
| 5542 | struct ext4_inode_info	*ei = EXT4_I(inode); | 
|---|
| 5543 |  | 
|---|
| 5544 | inode->i_state &= ~I_DIRTY_TIME; | 
|---|
| 5545 | spin_unlock(lock: &inode->i_lock); | 
|---|
| 5546 |  | 
|---|
| 5547 | spin_lock(lock: &ei->i_raw_lock); | 
|---|
| 5548 | EXT4_INODE_SET_CTIME(inode, raw_inode); | 
|---|
| 5549 | EXT4_INODE_SET_MTIME(inode, raw_inode); | 
|---|
| 5550 | EXT4_INODE_SET_ATIME(inode, raw_inode); | 
|---|
| 5551 | ext4_inode_csum_set(inode, raw: raw_inode, ei); | 
|---|
| 5552 | spin_unlock(lock: &ei->i_raw_lock); | 
|---|
| 5553 | trace_ext4_other_inode_update_time(inode, orig_ino); | 
|---|
| 5554 | return; | 
|---|
| 5555 | } | 
|---|
| 5556 | spin_unlock(lock: &inode->i_lock); | 
|---|
| 5557 | } | 
|---|
| 5558 |  | 
|---|
| 5559 | /* | 
|---|
| 5560 | * Opportunistically update the other time fields for other inodes in | 
|---|
| 5561 | * the same inode table block. | 
|---|
| 5562 | */ | 
|---|
| 5563 | static void ext4_update_other_inodes_time(struct super_block *sb, | 
|---|
| 5564 | unsigned long orig_ino, char *buf) | 
|---|
| 5565 | { | 
|---|
| 5566 | unsigned long ino; | 
|---|
| 5567 | int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; | 
|---|
| 5568 | int inode_size = EXT4_INODE_SIZE(sb); | 
|---|
| 5569 |  | 
|---|
| 5570 | /* | 
|---|
| 5571 | * Calculate the first inode in the inode table block.  Inode | 
|---|
| 5572 | * numbers are one-based.  That is, the first inode in a block | 
|---|
| 5573 | * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). | 
|---|
| 5574 | */ | 
|---|
| 5575 | ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; | 
|---|
| 5576 | rcu_read_lock(); | 
|---|
| 5577 | for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { | 
|---|
| 5578 | if (ino == orig_ino) | 
|---|
| 5579 | continue; | 
|---|
| 5580 | __ext4_update_other_inode_time(sb, orig_ino, ino, | 
|---|
| 5581 | raw_inode: (struct ext4_inode *)buf); | 
|---|
| 5582 | } | 
|---|
| 5583 | rcu_read_unlock(); | 
|---|
| 5584 | } | 
|---|
| 5585 |  | 
|---|
| 5586 | /* | 
|---|
| 5587 | * Post the struct inode info into an on-disk inode location in the | 
|---|
| 5588 | * buffer-cache.  This gobbles the caller's reference to the | 
|---|
| 5589 | * buffer_head in the inode location struct. | 
|---|
| 5590 | * | 
|---|
| 5591 | * The caller must have write access to iloc->bh. | 
|---|
| 5592 | */ | 
|---|
| 5593 | static int ext4_do_update_inode(handle_t *handle, | 
|---|
| 5594 | struct inode *inode, | 
|---|
| 5595 | struct ext4_iloc *iloc) | 
|---|
| 5596 | { | 
|---|
| 5597 | struct ext4_inode *raw_inode = ext4_raw_inode(iloc); | 
|---|
| 5598 | struct ext4_inode_info *ei = EXT4_I(inode); | 
|---|
| 5599 | struct buffer_head *bh = iloc->bh; | 
|---|
| 5600 | struct super_block *sb = inode->i_sb; | 
|---|
| 5601 | int err; | 
|---|
| 5602 | int need_datasync = 0, set_large_file = 0; | 
|---|
| 5603 |  | 
|---|
| 5604 | spin_lock(lock: &ei->i_raw_lock); | 
|---|
| 5605 |  | 
|---|
| 5606 | /* | 
|---|
| 5607 | * For fields not tracked in the in-memory inode, initialise them | 
|---|
| 5608 | * to zero for new inodes. | 
|---|
| 5609 | */ | 
|---|
| 5610 | if (ext4_test_inode_state(inode, bit: EXT4_STATE_NEW)) | 
|---|
| 5611 | memset(s: raw_inode, c: 0, n: EXT4_SB(sb: inode->i_sb)->s_inode_size); | 
|---|
| 5612 |  | 
|---|
| 5613 | if (READ_ONCE(ei->i_disksize) != ext4_isize(sb: inode->i_sb, raw_inode)) | 
|---|
| 5614 | need_datasync = 1; | 
|---|
| 5615 | if (ei->i_disksize > 0x7fffffffULL) { | 
|---|
| 5616 | if (!ext4_has_feature_large_file(sb) || | 
|---|
| 5617 | EXT4_SB(sb)->s_es->s_rev_level == cpu_to_le32(EXT4_GOOD_OLD_REV)) | 
|---|
| 5618 | set_large_file = 1; | 
|---|
| 5619 | } | 
|---|
| 5620 |  | 
|---|
| 5621 | err = ext4_fill_raw_inode(inode, raw_inode); | 
|---|
| 5622 | spin_unlock(lock: &ei->i_raw_lock); | 
|---|
| 5623 | if (err) { | 
|---|
| 5624 | EXT4_ERROR_INODE(inode, "corrupted inode contents"); | 
|---|
| 5625 | goto out_brelse; | 
|---|
| 5626 | } | 
|---|
| 5627 |  | 
|---|
| 5628 | if (inode->i_sb->s_flags & SB_LAZYTIME) | 
|---|
| 5629 | ext4_update_other_inodes_time(sb: inode->i_sb, orig_ino: inode->i_ino, | 
|---|
| 5630 | buf: bh->b_data); | 
|---|
| 5631 |  | 
|---|
| 5632 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); | 
|---|
| 5633 | err = ext4_handle_dirty_metadata(handle, NULL, bh); | 
|---|
| 5634 | if (err) | 
|---|
| 5635 | goto out_error; | 
|---|
| 5636 | ext4_clear_inode_state(inode, bit: EXT4_STATE_NEW); | 
|---|
| 5637 | if (set_large_file) { | 
|---|
| 5638 | BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); | 
|---|
| 5639 | err = ext4_journal_get_write_access(handle, sb, | 
|---|
| 5640 | EXT4_SB(sb)->s_sbh, | 
|---|
| 5641 | EXT4_JTR_NONE); | 
|---|
| 5642 | if (err) | 
|---|
| 5643 | goto out_error; | 
|---|
| 5644 | lock_buffer(bh: EXT4_SB(sb)->s_sbh); | 
|---|
| 5645 | ext4_set_feature_large_file(sb); | 
|---|
| 5646 | ext4_superblock_csum_set(sb); | 
|---|
| 5647 | unlock_buffer(bh: EXT4_SB(sb)->s_sbh); | 
|---|
| 5648 | ext4_handle_sync(handle); | 
|---|
| 5649 | err = ext4_handle_dirty_metadata(handle, NULL, | 
|---|
| 5650 | EXT4_SB(sb)->s_sbh); | 
|---|
| 5651 | } | 
|---|
| 5652 | ext4_update_inode_fsync_trans(handle, inode, datasync: need_datasync); | 
|---|
| 5653 | out_error: | 
|---|
| 5654 | ext4_std_error(inode->i_sb, err); | 
|---|
| 5655 | out_brelse: | 
|---|
| 5656 | brelse(bh); | 
|---|
| 5657 | return err; | 
|---|
| 5658 | } | 
|---|
| 5659 |  | 
|---|
| 5660 | /* | 
|---|
| 5661 | * ext4_write_inode() | 
|---|
| 5662 | * | 
|---|
| 5663 | * We are called from a few places: | 
|---|
| 5664 | * | 
|---|
| 5665 | * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. | 
|---|
| 5666 | *   Here, there will be no transaction running. We wait for any running | 
|---|
| 5667 | *   transaction to commit. | 
|---|
| 5668 | * | 
|---|
| 5669 | * - Within flush work (sys_sync(), kupdate and such). | 
|---|
| 5670 | *   We wait on commit, if told to. | 
|---|
| 5671 | * | 
|---|
| 5672 | * - Within iput_final() -> write_inode_now() | 
|---|
| 5673 | *   We wait on commit, if told to. | 
|---|
| 5674 | * | 
|---|
| 5675 | * In all cases it is actually safe for us to return without doing anything, | 
|---|
| 5676 | * because the inode has been copied into a raw inode buffer in | 
|---|
| 5677 | * ext4_mark_inode_dirty().  This is a correctness thing for WB_SYNC_ALL | 
|---|
| 5678 | * writeback. | 
|---|
| 5679 | * | 
|---|
| 5680 | * Note that we are absolutely dependent upon all inode dirtiers doing the | 
|---|
| 5681 | * right thing: they *must* call mark_inode_dirty() after dirtying info in | 
|---|
| 5682 | * which we are interested. | 
|---|
| 5683 | * | 
|---|
| 5684 | * It would be a bug for them to not do this.  The code: | 
|---|
| 5685 | * | 
|---|
| 5686 | *	mark_inode_dirty(inode) | 
|---|
| 5687 | *	stuff(); | 
|---|
| 5688 | *	inode->i_size = expr; | 
|---|
| 5689 | * | 
|---|
| 5690 | * is in error because write_inode() could occur while `stuff()' is running, | 
|---|
| 5691 | * and the new i_size will be lost.  Plus the inode will no longer be on the | 
|---|
| 5692 | * superblock's dirty inode list. | 
|---|
| 5693 | */ | 
|---|
| 5694 | int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) | 
|---|
| 5695 | { | 
|---|
| 5696 | int err; | 
|---|
| 5697 |  | 
|---|
| 5698 | if (WARN_ON_ONCE(current->flags & PF_MEMALLOC)) | 
|---|
| 5699 | return 0; | 
|---|
| 5700 |  | 
|---|
| 5701 | err = ext4_emergency_state(sb: inode->i_sb); | 
|---|
| 5702 | if (unlikely(err)) | 
|---|
| 5703 | return err; | 
|---|
| 5704 |  | 
|---|
| 5705 | if (EXT4_SB(sb: inode->i_sb)->s_journal) { | 
|---|
| 5706 | if (ext4_journal_current_handle()) { | 
|---|
| 5707 | ext4_debug( "called recursively, non-PF_MEMALLOC!\n"); | 
|---|
| 5708 | dump_stack(); | 
|---|
| 5709 | return -EIO; | 
|---|
| 5710 | } | 
|---|
| 5711 |  | 
|---|
| 5712 | /* | 
|---|
| 5713 | * No need to force transaction in WB_SYNC_NONE mode. Also | 
|---|
| 5714 | * ext4_sync_fs() will force the commit after everything is | 
|---|
| 5715 | * written. | 
|---|
| 5716 | */ | 
|---|
| 5717 | if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) | 
|---|
| 5718 | return 0; | 
|---|
| 5719 |  | 
|---|
| 5720 | err = ext4_fc_commit(journal: EXT4_SB(sb: inode->i_sb)->s_journal, | 
|---|
| 5721 | EXT4_I(inode)->i_sync_tid); | 
|---|
| 5722 | } else { | 
|---|
| 5723 | struct ext4_iloc iloc; | 
|---|
| 5724 |  | 
|---|
| 5725 | err = __ext4_get_inode_loc_noinmem(inode, iloc: &iloc); | 
|---|
| 5726 | if (err) | 
|---|
| 5727 | return err; | 
|---|
| 5728 | /* | 
|---|
| 5729 | * sync(2) will flush the whole buffer cache. No need to do | 
|---|
| 5730 | * it here separately for each inode. | 
|---|
| 5731 | */ | 
|---|
| 5732 | if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) | 
|---|
| 5733 | sync_dirty_buffer(bh: iloc.bh); | 
|---|
| 5734 | if (buffer_req(bh: iloc.bh) && !buffer_uptodate(bh: iloc.bh)) { | 
|---|
| 5735 | ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO, | 
|---|
| 5736 | "IO error syncing inode"); | 
|---|
| 5737 | err = -EIO; | 
|---|
| 5738 | } | 
|---|
| 5739 | brelse(bh: iloc.bh); | 
|---|
| 5740 | } | 
|---|
| 5741 | return err; | 
|---|
| 5742 | } | 
|---|
| 5743 |  | 
|---|
| 5744 | /* | 
|---|
| 5745 | * In data=journal mode ext4_journalled_invalidate_folio() may fail to invalidate | 
|---|
| 5746 | * buffers that are attached to a folio straddling i_size and are undergoing | 
|---|
| 5747 | * commit. In that case we have to wait for commit to finish and try again. | 
|---|
| 5748 | */ | 
|---|
| 5749 | static void ext4_wait_for_tail_page_commit(struct inode *inode) | 
|---|
| 5750 | { | 
|---|
| 5751 | unsigned offset; | 
|---|
| 5752 | journal_t *journal = EXT4_SB(sb: inode->i_sb)->s_journal; | 
|---|
| 5753 | tid_t commit_tid; | 
|---|
| 5754 | int ret; | 
|---|
| 5755 | bool has_transaction; | 
|---|
| 5756 |  | 
|---|
| 5757 | offset = inode->i_size & (PAGE_SIZE - 1); | 
|---|
| 5758 | /* | 
|---|
| 5759 | * If the folio is fully truncated, we don't need to wait for any commit | 
|---|
| 5760 | * (and we even should not as __ext4_journalled_invalidate_folio() may | 
|---|
| 5761 | * strip all buffers from the folio but keep the folio dirty which can then | 
|---|
| 5762 | * confuse e.g. concurrent ext4_writepages() seeing dirty folio without | 
|---|
| 5763 | * buffers). Also we don't need to wait for any commit if all buffers in | 
|---|
| 5764 | * the folio remain valid. This is most beneficial for the common case of | 
|---|
| 5765 | * blocksize == PAGESIZE. | 
|---|
| 5766 | */ | 
|---|
| 5767 | if (!offset || offset > (PAGE_SIZE - i_blocksize(node: inode))) | 
|---|
| 5768 | return; | 
|---|
| 5769 | while (1) { | 
|---|
| 5770 | struct folio *folio = filemap_lock_folio(mapping: inode->i_mapping, | 
|---|
| 5771 | index: inode->i_size >> PAGE_SHIFT); | 
|---|
| 5772 | if (IS_ERR(ptr: folio)) | 
|---|
| 5773 | return; | 
|---|
| 5774 | ret = __ext4_journalled_invalidate_folio(folio, offset, | 
|---|
| 5775 | length: folio_size(folio) - offset); | 
|---|
| 5776 | folio_unlock(folio); | 
|---|
| 5777 | folio_put(folio); | 
|---|
| 5778 | if (ret != -EBUSY) | 
|---|
| 5779 | return; | 
|---|
| 5780 | has_transaction = false; | 
|---|
| 5781 | read_lock(&journal->j_state_lock); | 
|---|
| 5782 | if (journal->j_committing_transaction) { | 
|---|
| 5783 | commit_tid = journal->j_committing_transaction->t_tid; | 
|---|
| 5784 | has_transaction = true; | 
|---|
| 5785 | } | 
|---|
| 5786 | read_unlock(&journal->j_state_lock); | 
|---|
| 5787 | if (has_transaction) | 
|---|
| 5788 | jbd2_log_wait_commit(journal, tid: commit_tid); | 
|---|
| 5789 | } | 
|---|
| 5790 | } | 
|---|
| 5791 |  | 
|---|
| 5792 | /* | 
|---|
| 5793 | * ext4_setattr() | 
|---|
| 5794 | * | 
|---|
| 5795 | * Called from notify_change. | 
|---|
| 5796 | * | 
|---|
| 5797 | * We want to trap VFS attempts to truncate the file as soon as | 
|---|
| 5798 | * possible.  In particular, we want to make sure that when the VFS | 
|---|
| 5799 | * shrinks i_size, we put the inode on the orphan list and modify | 
|---|
| 5800 | * i_disksize immediately, so that during the subsequent flushing of | 
|---|
| 5801 | * dirty pages and freeing of disk blocks, we can guarantee that any | 
|---|
| 5802 | * commit will leave the blocks being flushed in an unused state on | 
|---|
| 5803 | * disk.  (On recovery, the inode will get truncated and the blocks will | 
|---|
| 5804 | * be freed, so we have a strong guarantee that no future commit will | 
|---|
| 5805 | * leave these blocks visible to the user.) | 
|---|
| 5806 | * | 
|---|
| 5807 | * Another thing we have to assure is that if we are in ordered mode | 
|---|
| 5808 | * and inode is still attached to the committing transaction, we must | 
|---|
| 5809 | * we start writeout of all the dirty pages which are being truncated. | 
|---|
| 5810 | * This way we are sure that all the data written in the previous | 
|---|
| 5811 | * transaction are already on disk (truncate waits for pages under | 
|---|
| 5812 | * writeback). | 
|---|
| 5813 | * | 
|---|
| 5814 | * Called with inode->i_rwsem down. | 
|---|
| 5815 | */ | 
|---|
| 5816 | int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry, | 
|---|
| 5817 | struct iattr *attr) | 
|---|
| 5818 | { | 
|---|
| 5819 | struct inode *inode = d_inode(dentry); | 
|---|
| 5820 | int error, rc = 0; | 
|---|
| 5821 | int orphan = 0; | 
|---|
| 5822 | const unsigned int ia_valid = attr->ia_valid; | 
|---|
| 5823 | bool inc_ivers = true; | 
|---|
| 5824 |  | 
|---|
| 5825 | error = ext4_emergency_state(sb: inode->i_sb); | 
|---|
| 5826 | if (unlikely(error)) | 
|---|
| 5827 | return error; | 
|---|
| 5828 |  | 
|---|
| 5829 | if (unlikely(IS_IMMUTABLE(inode))) | 
|---|
| 5830 | return -EPERM; | 
|---|
| 5831 |  | 
|---|
| 5832 | if (unlikely(IS_APPEND(inode) && | 
|---|
| 5833 | (ia_valid & (ATTR_MODE | ATTR_UID | | 
|---|
| 5834 | ATTR_GID | ATTR_TIMES_SET)))) | 
|---|
| 5835 | return -EPERM; | 
|---|
| 5836 |  | 
|---|
| 5837 | error = setattr_prepare(idmap, dentry, attr); | 
|---|
| 5838 | if (error) | 
|---|
| 5839 | return error; | 
|---|
| 5840 |  | 
|---|
| 5841 | error = fscrypt_prepare_setattr(dentry, attr); | 
|---|
| 5842 | if (error) | 
|---|
| 5843 | return error; | 
|---|
| 5844 |  | 
|---|
| 5845 | error = fsverity_prepare_setattr(dentry, attr); | 
|---|
| 5846 | if (error) | 
|---|
| 5847 | return error; | 
|---|
| 5848 |  | 
|---|
| 5849 | if (is_quota_modification(idmap, inode, ia: attr)) { | 
|---|
| 5850 | error = dquot_initialize(inode); | 
|---|
| 5851 | if (error) | 
|---|
| 5852 | return error; | 
|---|
| 5853 | } | 
|---|
| 5854 |  | 
|---|
| 5855 | if (i_uid_needs_update(idmap, attr, inode) || | 
|---|
| 5856 | i_gid_needs_update(idmap, attr, inode)) { | 
|---|
| 5857 | handle_t *handle; | 
|---|
| 5858 |  | 
|---|
| 5859 | /* (user+group)*(old+new) structure, inode write (sb, | 
|---|
| 5860 | * inode block, ? - but truncate inode update has it) */ | 
|---|
| 5861 | handle = ext4_journal_start(inode, EXT4_HT_QUOTA, | 
|---|
| 5862 | (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + | 
|---|
| 5863 | EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); | 
|---|
| 5864 | if (IS_ERR(ptr: handle)) { | 
|---|
| 5865 | error = PTR_ERR(ptr: handle); | 
|---|
| 5866 | goto err_out; | 
|---|
| 5867 | } | 
|---|
| 5868 |  | 
|---|
| 5869 | /* dquot_transfer() calls back ext4_get_inode_usage() which | 
|---|
| 5870 | * counts xattr inode references. | 
|---|
| 5871 | */ | 
|---|
| 5872 | down_read(sem: &EXT4_I(inode)->xattr_sem); | 
|---|
| 5873 | error = dquot_transfer(idmap, inode, iattr: attr); | 
|---|
| 5874 | up_read(sem: &EXT4_I(inode)->xattr_sem); | 
|---|
| 5875 |  | 
|---|
| 5876 | if (error) { | 
|---|
| 5877 | ext4_journal_stop(handle); | 
|---|
| 5878 | return error; | 
|---|
| 5879 | } | 
|---|
| 5880 | /* Update corresponding info in inode so that everything is in | 
|---|
| 5881 | * one transaction */ | 
|---|
| 5882 | i_uid_update(idmap, attr, inode); | 
|---|
| 5883 | i_gid_update(idmap, attr, inode); | 
|---|
| 5884 | error = ext4_mark_inode_dirty(handle, inode); | 
|---|
| 5885 | ext4_journal_stop(handle); | 
|---|
| 5886 | if (unlikely(error)) { | 
|---|
| 5887 | return error; | 
|---|
| 5888 | } | 
|---|
| 5889 | } | 
|---|
| 5890 |  | 
|---|
| 5891 | if (attr->ia_valid & ATTR_SIZE) { | 
|---|
| 5892 | handle_t *handle; | 
|---|
| 5893 | loff_t oldsize = inode->i_size; | 
|---|
| 5894 | loff_t old_disksize; | 
|---|
| 5895 | int shrink = (attr->ia_size < inode->i_size); | 
|---|
| 5896 |  | 
|---|
| 5897 | if (!(ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS))) { | 
|---|
| 5898 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 5899 |  | 
|---|
| 5900 | if (attr->ia_size > sbi->s_bitmap_maxbytes) { | 
|---|
| 5901 | return -EFBIG; | 
|---|
| 5902 | } | 
|---|
| 5903 | } | 
|---|
| 5904 | if (!S_ISREG(inode->i_mode)) { | 
|---|
| 5905 | return -EINVAL; | 
|---|
| 5906 | } | 
|---|
| 5907 |  | 
|---|
| 5908 | if (attr->ia_size == inode->i_size) | 
|---|
| 5909 | inc_ivers = false; | 
|---|
| 5910 |  | 
|---|
| 5911 | if (shrink) { | 
|---|
| 5912 | if (ext4_should_order_data(inode)) { | 
|---|
| 5913 | error = ext4_begin_ordered_truncate(inode, | 
|---|
| 5914 | new_size: attr->ia_size); | 
|---|
| 5915 | if (error) | 
|---|
| 5916 | goto err_out; | 
|---|
| 5917 | } | 
|---|
| 5918 | /* | 
|---|
| 5919 | * Blocks are going to be removed from the inode. Wait | 
|---|
| 5920 | * for dio in flight. | 
|---|
| 5921 | */ | 
|---|
| 5922 | inode_dio_wait(inode); | 
|---|
| 5923 | } | 
|---|
| 5924 |  | 
|---|
| 5925 | filemap_invalidate_lock(mapping: inode->i_mapping); | 
|---|
| 5926 |  | 
|---|
| 5927 | rc = ext4_break_layouts(inode); | 
|---|
| 5928 | if (rc) { | 
|---|
| 5929 | filemap_invalidate_unlock(mapping: inode->i_mapping); | 
|---|
| 5930 | goto err_out; | 
|---|
| 5931 | } | 
|---|
| 5932 |  | 
|---|
| 5933 | if (attr->ia_size != inode->i_size) { | 
|---|
| 5934 | /* attach jbd2 jinode for EOF folio tail zeroing */ | 
|---|
| 5935 | if (attr->ia_size & (inode->i_sb->s_blocksize - 1) || | 
|---|
| 5936 | oldsize & (inode->i_sb->s_blocksize - 1)) { | 
|---|
| 5937 | error = ext4_inode_attach_jinode(inode); | 
|---|
| 5938 | if (error) | 
|---|
| 5939 | goto out_mmap_sem; | 
|---|
| 5940 | } | 
|---|
| 5941 |  | 
|---|
| 5942 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); | 
|---|
| 5943 | if (IS_ERR(ptr: handle)) { | 
|---|
| 5944 | error = PTR_ERR(ptr: handle); | 
|---|
| 5945 | goto out_mmap_sem; | 
|---|
| 5946 | } | 
|---|
| 5947 | if (ext4_handle_valid(handle) && shrink) { | 
|---|
| 5948 | error = ext4_orphan_add(handle, inode); | 
|---|
| 5949 | orphan = 1; | 
|---|
| 5950 | } | 
|---|
| 5951 | /* | 
|---|
| 5952 | * Update c/mtime and tail zero the EOF folio on | 
|---|
| 5953 | * truncate up. ext4_truncate() handles the shrink case | 
|---|
| 5954 | * below. | 
|---|
| 5955 | */ | 
|---|
| 5956 | if (!shrink) { | 
|---|
| 5957 | inode_set_mtime_to_ts(inode, | 
|---|
| 5958 | ts: inode_set_ctime_current(inode)); | 
|---|
| 5959 | if (oldsize & (inode->i_sb->s_blocksize - 1)) | 
|---|
| 5960 | ext4_block_truncate_page(handle, | 
|---|
| 5961 | mapping: inode->i_mapping, from: oldsize); | 
|---|
| 5962 | } | 
|---|
| 5963 |  | 
|---|
| 5964 | if (shrink) | 
|---|
| 5965 | ext4_fc_track_range(handle, inode, | 
|---|
| 5966 | start: (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> | 
|---|
| 5967 | inode->i_sb->s_blocksize_bits, | 
|---|
| 5968 | EXT_MAX_BLOCKS - 1); | 
|---|
| 5969 | else | 
|---|
| 5970 | ext4_fc_track_range( | 
|---|
| 5971 | handle, inode, | 
|---|
| 5972 | start: (oldsize > 0 ? oldsize - 1 : oldsize) >> | 
|---|
| 5973 | inode->i_sb->s_blocksize_bits, | 
|---|
| 5974 | end: (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> | 
|---|
| 5975 | inode->i_sb->s_blocksize_bits); | 
|---|
| 5976 |  | 
|---|
| 5977 | down_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 5978 | old_disksize = EXT4_I(inode)->i_disksize; | 
|---|
| 5979 | EXT4_I(inode)->i_disksize = attr->ia_size; | 
|---|
| 5980 |  | 
|---|
| 5981 | /* | 
|---|
| 5982 | * We have to update i_size under i_data_sem together | 
|---|
| 5983 | * with i_disksize to avoid races with writeback code | 
|---|
| 5984 | * running ext4_wb_update_i_disksize(). | 
|---|
| 5985 | */ | 
|---|
| 5986 | if (!error) | 
|---|
| 5987 | i_size_write(inode, i_size: attr->ia_size); | 
|---|
| 5988 | else | 
|---|
| 5989 | EXT4_I(inode)->i_disksize = old_disksize; | 
|---|
| 5990 | up_write(sem: &EXT4_I(inode)->i_data_sem); | 
|---|
| 5991 | rc = ext4_mark_inode_dirty(handle, inode); | 
|---|
| 5992 | if (!error) | 
|---|
| 5993 | error = rc; | 
|---|
| 5994 | ext4_journal_stop(handle); | 
|---|
| 5995 | if (error) | 
|---|
| 5996 | goto out_mmap_sem; | 
|---|
| 5997 | if (!shrink) { | 
|---|
| 5998 | pagecache_isize_extended(inode, from: oldsize, | 
|---|
| 5999 | to: inode->i_size); | 
|---|
| 6000 | } else if (ext4_should_journal_data(inode)) { | 
|---|
| 6001 | ext4_wait_for_tail_page_commit(inode); | 
|---|
| 6002 | } | 
|---|
| 6003 | } | 
|---|
| 6004 |  | 
|---|
| 6005 | /* | 
|---|
| 6006 | * Truncate pagecache after we've waited for commit | 
|---|
| 6007 | * in data=journal mode to make pages freeable. | 
|---|
| 6008 | */ | 
|---|
| 6009 | truncate_pagecache(inode, new: inode->i_size); | 
|---|
| 6010 | /* | 
|---|
| 6011 | * Call ext4_truncate() even if i_size didn't change to | 
|---|
| 6012 | * truncate possible preallocated blocks. | 
|---|
| 6013 | */ | 
|---|
| 6014 | if (attr->ia_size <= oldsize) { | 
|---|
| 6015 | rc = ext4_truncate(inode); | 
|---|
| 6016 | if (rc) | 
|---|
| 6017 | error = rc; | 
|---|
| 6018 | } | 
|---|
| 6019 | out_mmap_sem: | 
|---|
| 6020 | filemap_invalidate_unlock(mapping: inode->i_mapping); | 
|---|
| 6021 | } | 
|---|
| 6022 |  | 
|---|
| 6023 | if (!error) { | 
|---|
| 6024 | if (inc_ivers) | 
|---|
| 6025 | inode_inc_iversion(inode); | 
|---|
| 6026 | setattr_copy(idmap, inode, attr); | 
|---|
| 6027 | mark_inode_dirty(inode); | 
|---|
| 6028 | } | 
|---|
| 6029 |  | 
|---|
| 6030 | /* | 
|---|
| 6031 | * If the call to ext4_truncate failed to get a transaction handle at | 
|---|
| 6032 | * all, we need to clean up the in-core orphan list manually. | 
|---|
| 6033 | */ | 
|---|
| 6034 | if (orphan && inode->i_nlink) | 
|---|
| 6035 | ext4_orphan_del(NULL, inode); | 
|---|
| 6036 |  | 
|---|
| 6037 | if (!error && (ia_valid & ATTR_MODE)) | 
|---|
| 6038 | rc = posix_acl_chmod(idmap, dentry, inode->i_mode); | 
|---|
| 6039 |  | 
|---|
| 6040 | err_out: | 
|---|
| 6041 | if  (error) | 
|---|
| 6042 | ext4_std_error(inode->i_sb, error); | 
|---|
| 6043 | if (!error) | 
|---|
| 6044 | error = rc; | 
|---|
| 6045 | return error; | 
|---|
| 6046 | } | 
|---|
| 6047 |  | 
|---|
| 6048 | u32 ext4_dio_alignment(struct inode *inode) | 
|---|
| 6049 | { | 
|---|
| 6050 | if (fsverity_active(inode)) | 
|---|
| 6051 | return 0; | 
|---|
| 6052 | if (ext4_should_journal_data(inode)) | 
|---|
| 6053 | return 0; | 
|---|
| 6054 | if (ext4_has_inline_data(inode)) | 
|---|
| 6055 | return 0; | 
|---|
| 6056 | if (IS_ENCRYPTED(inode)) { | 
|---|
| 6057 | if (!fscrypt_dio_supported(inode)) | 
|---|
| 6058 | return 0; | 
|---|
| 6059 | return i_blocksize(node: inode); | 
|---|
| 6060 | } | 
|---|
| 6061 | return 1; /* use the iomap defaults */ | 
|---|
| 6062 | } | 
|---|
| 6063 |  | 
|---|
| 6064 | int ext4_getattr(struct mnt_idmap *idmap, const struct path *path, | 
|---|
| 6065 | struct kstat *stat, u32 request_mask, unsigned int query_flags) | 
|---|
| 6066 | { | 
|---|
| 6067 | struct inode *inode = d_inode(dentry: path->dentry); | 
|---|
| 6068 | struct ext4_inode *raw_inode; | 
|---|
| 6069 | struct ext4_inode_info *ei = EXT4_I(inode); | 
|---|
| 6070 | unsigned int flags; | 
|---|
| 6071 |  | 
|---|
| 6072 | if ((request_mask & STATX_BTIME) && | 
|---|
| 6073 | EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { | 
|---|
| 6074 | stat->result_mask |= STATX_BTIME; | 
|---|
| 6075 | stat->btime.tv_sec = ei->i_crtime.tv_sec; | 
|---|
| 6076 | stat->btime.tv_nsec = ei->i_crtime.tv_nsec; | 
|---|
| 6077 | } | 
|---|
| 6078 |  | 
|---|
| 6079 | /* | 
|---|
| 6080 | * Return the DIO alignment restrictions if requested.  We only return | 
|---|
| 6081 | * this information when requested, since on encrypted files it might | 
|---|
| 6082 | * take a fair bit of work to get if the file wasn't opened recently. | 
|---|
| 6083 | */ | 
|---|
| 6084 | if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) { | 
|---|
| 6085 | u32 dio_align = ext4_dio_alignment(inode); | 
|---|
| 6086 |  | 
|---|
| 6087 | stat->result_mask |= STATX_DIOALIGN; | 
|---|
| 6088 | if (dio_align == 1) { | 
|---|
| 6089 | struct block_device *bdev = inode->i_sb->s_bdev; | 
|---|
| 6090 |  | 
|---|
| 6091 | /* iomap defaults */ | 
|---|
| 6092 | stat->dio_mem_align = bdev_dma_alignment(bdev) + 1; | 
|---|
| 6093 | stat->dio_offset_align = bdev_logical_block_size(bdev); | 
|---|
| 6094 | } else { | 
|---|
| 6095 | stat->dio_mem_align = dio_align; | 
|---|
| 6096 | stat->dio_offset_align = dio_align; | 
|---|
| 6097 | } | 
|---|
| 6098 | } | 
|---|
| 6099 |  | 
|---|
| 6100 | if ((request_mask & STATX_WRITE_ATOMIC) && S_ISREG(inode->i_mode)) { | 
|---|
| 6101 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 6102 | unsigned int awu_min = 0, awu_max = 0; | 
|---|
| 6103 |  | 
|---|
| 6104 | if (ext4_inode_can_atomic_write(inode)) { | 
|---|
| 6105 | awu_min = sbi->s_awu_min; | 
|---|
| 6106 | awu_max = sbi->s_awu_max; | 
|---|
| 6107 | } | 
|---|
| 6108 |  | 
|---|
| 6109 | generic_fill_statx_atomic_writes(stat, unit_min: awu_min, unit_max: awu_max, unit_max_opt: 0); | 
|---|
| 6110 | } | 
|---|
| 6111 |  | 
|---|
| 6112 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; | 
|---|
| 6113 | if (flags & EXT4_APPEND_FL) | 
|---|
| 6114 | stat->attributes |= STATX_ATTR_APPEND; | 
|---|
| 6115 | if (flags & EXT4_COMPR_FL) | 
|---|
| 6116 | stat->attributes |= STATX_ATTR_COMPRESSED; | 
|---|
| 6117 | if (flags & EXT4_ENCRYPT_FL) | 
|---|
| 6118 | stat->attributes |= STATX_ATTR_ENCRYPTED; | 
|---|
| 6119 | if (flags & EXT4_IMMUTABLE_FL) | 
|---|
| 6120 | stat->attributes |= STATX_ATTR_IMMUTABLE; | 
|---|
| 6121 | if (flags & EXT4_NODUMP_FL) | 
|---|
| 6122 | stat->attributes |= STATX_ATTR_NODUMP; | 
|---|
| 6123 | if (flags & EXT4_VERITY_FL) | 
|---|
| 6124 | stat->attributes |= STATX_ATTR_VERITY; | 
|---|
| 6125 |  | 
|---|
| 6126 | stat->attributes_mask |= (STATX_ATTR_APPEND | | 
|---|
| 6127 | STATX_ATTR_COMPRESSED | | 
|---|
| 6128 | STATX_ATTR_ENCRYPTED | | 
|---|
| 6129 | STATX_ATTR_IMMUTABLE | | 
|---|
| 6130 | STATX_ATTR_NODUMP | | 
|---|
| 6131 | STATX_ATTR_VERITY); | 
|---|
| 6132 |  | 
|---|
| 6133 | generic_fillattr(idmap, request_mask, inode, stat); | 
|---|
| 6134 | return 0; | 
|---|
| 6135 | } | 
|---|
| 6136 |  | 
|---|
| 6137 | int ext4_file_getattr(struct mnt_idmap *idmap, | 
|---|
| 6138 | const struct path *path, struct kstat *stat, | 
|---|
| 6139 | u32 request_mask, unsigned int query_flags) | 
|---|
| 6140 | { | 
|---|
| 6141 | struct inode *inode = d_inode(dentry: path->dentry); | 
|---|
| 6142 | u64 delalloc_blocks; | 
|---|
| 6143 |  | 
|---|
| 6144 | ext4_getattr(idmap, path, stat, request_mask, query_flags); | 
|---|
| 6145 |  | 
|---|
| 6146 | /* | 
|---|
| 6147 | * If there is inline data in the inode, the inode will normally not | 
|---|
| 6148 | * have data blocks allocated (it may have an external xattr block). | 
|---|
| 6149 | * Report at least one sector for such files, so tools like tar, rsync, | 
|---|
| 6150 | * others don't incorrectly think the file is completely sparse. | 
|---|
| 6151 | */ | 
|---|
| 6152 | if (unlikely(ext4_has_inline_data(inode))) | 
|---|
| 6153 | stat->blocks += (stat->size + 511) >> 9; | 
|---|
| 6154 |  | 
|---|
| 6155 | /* | 
|---|
| 6156 | * We can't update i_blocks if the block allocation is delayed | 
|---|
| 6157 | * otherwise in the case of system crash before the real block | 
|---|
| 6158 | * allocation is done, we will have i_blocks inconsistent with | 
|---|
| 6159 | * on-disk file blocks. | 
|---|
| 6160 | * We always keep i_blocks updated together with real | 
|---|
| 6161 | * allocation. But to not confuse with user, stat | 
|---|
| 6162 | * will return the blocks that include the delayed allocation | 
|---|
| 6163 | * blocks for this file. | 
|---|
| 6164 | */ | 
|---|
| 6165 | delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), | 
|---|
| 6166 | EXT4_I(inode)->i_reserved_data_blocks); | 
|---|
| 6167 | stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); | 
|---|
| 6168 | return 0; | 
|---|
| 6169 | } | 
|---|
| 6170 |  | 
|---|
| 6171 | static int ext4_index_trans_blocks(struct inode *inode, int lblocks, | 
|---|
| 6172 | int pextents) | 
|---|
| 6173 | { | 
|---|
| 6174 | if (!(ext4_test_inode_flag(inode, bit: EXT4_INODE_EXTENTS))) | 
|---|
| 6175 | return ext4_ind_trans_blocks(inode, nrblocks: lblocks); | 
|---|
| 6176 | return ext4_ext_index_trans_blocks(inode, extents: pextents); | 
|---|
| 6177 | } | 
|---|
| 6178 |  | 
|---|
| 6179 | /* | 
|---|
| 6180 | * Account for index blocks, block groups bitmaps and block group | 
|---|
| 6181 | * descriptor blocks if modify datablocks and index blocks | 
|---|
| 6182 | * worse case, the indexs blocks spread over different block groups | 
|---|
| 6183 | * | 
|---|
| 6184 | * If datablocks are discontiguous, they are possible to spread over | 
|---|
| 6185 | * different block groups too. If they are contiguous, with flexbg, | 
|---|
| 6186 | * they could still across block group boundary. | 
|---|
| 6187 | * | 
|---|
| 6188 | * Also account for superblock, inode, quota and xattr blocks | 
|---|
| 6189 | */ | 
|---|
| 6190 | int ext4_meta_trans_blocks(struct inode *inode, int lblocks, int pextents) | 
|---|
| 6191 | { | 
|---|
| 6192 | ext4_group_t groups, ngroups = ext4_get_groups_count(sb: inode->i_sb); | 
|---|
| 6193 | int gdpblocks; | 
|---|
| 6194 | int idxblocks; | 
|---|
| 6195 | int ret; | 
|---|
| 6196 |  | 
|---|
| 6197 | /* | 
|---|
| 6198 | * How many index and leaf blocks need to touch to map @lblocks | 
|---|
| 6199 | * logical blocks to @pextents physical extents? | 
|---|
| 6200 | */ | 
|---|
| 6201 | idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); | 
|---|
| 6202 |  | 
|---|
| 6203 | /* | 
|---|
| 6204 | * Now let's see how many group bitmaps and group descriptors need | 
|---|
| 6205 | * to account | 
|---|
| 6206 | */ | 
|---|
| 6207 | groups = idxblocks + pextents; | 
|---|
| 6208 | gdpblocks = groups; | 
|---|
| 6209 | if (groups > ngroups) | 
|---|
| 6210 | groups = ngroups; | 
|---|
| 6211 | if (groups > EXT4_SB(sb: inode->i_sb)->s_gdb_count) | 
|---|
| 6212 | gdpblocks = EXT4_SB(sb: inode->i_sb)->s_gdb_count; | 
|---|
| 6213 |  | 
|---|
| 6214 | /* bitmaps and block group descriptor blocks */ | 
|---|
| 6215 | ret = idxblocks + groups + gdpblocks; | 
|---|
| 6216 |  | 
|---|
| 6217 | /* Blocks for super block, inode, quota and xattr blocks */ | 
|---|
| 6218 | ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); | 
|---|
| 6219 |  | 
|---|
| 6220 | return ret; | 
|---|
| 6221 | } | 
|---|
| 6222 |  | 
|---|
| 6223 | /* | 
|---|
| 6224 | * Calculate the journal credits for modifying the number of blocks | 
|---|
| 6225 | * in a single extent within one transaction. 'nrblocks' is used only | 
|---|
| 6226 | * for non-extent inodes. For extent type inodes, 'nrblocks' can be | 
|---|
| 6227 | * zero if the exact number of blocks is unknown. | 
|---|
| 6228 | */ | 
|---|
| 6229 | int ext4_chunk_trans_extent(struct inode *inode, int nrblocks) | 
|---|
| 6230 | { | 
|---|
| 6231 | int ret; | 
|---|
| 6232 |  | 
|---|
| 6233 | ret = ext4_meta_trans_blocks(inode, lblocks: nrblocks, pextents: 1); | 
|---|
| 6234 | /* Account for data blocks for journalled mode */ | 
|---|
| 6235 | if (ext4_should_journal_data(inode)) | 
|---|
| 6236 | ret += nrblocks; | 
|---|
| 6237 | return ret; | 
|---|
| 6238 | } | 
|---|
| 6239 |  | 
|---|
| 6240 | /* | 
|---|
| 6241 | * Calculate the journal credits for a chunk of data modification. | 
|---|
| 6242 | * | 
|---|
| 6243 | * This is called from DIO, fallocate or whoever calling | 
|---|
| 6244 | * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. | 
|---|
| 6245 | * | 
|---|
| 6246 | * journal buffers for data blocks are not included here, as DIO | 
|---|
| 6247 | * and fallocate do no need to journal data buffers. | 
|---|
| 6248 | */ | 
|---|
| 6249 | int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) | 
|---|
| 6250 | { | 
|---|
| 6251 | return ext4_meta_trans_blocks(inode, lblocks: nrblocks, pextents: 1); | 
|---|
| 6252 | } | 
|---|
| 6253 |  | 
|---|
| 6254 | /* | 
|---|
| 6255 | * The caller must have previously called ext4_reserve_inode_write(). | 
|---|
| 6256 | * Give this, we know that the caller already has write access to iloc->bh. | 
|---|
| 6257 | */ | 
|---|
| 6258 | int ext4_mark_iloc_dirty(handle_t *handle, | 
|---|
| 6259 | struct inode *inode, struct ext4_iloc *iloc) | 
|---|
| 6260 | { | 
|---|
| 6261 | int err = 0; | 
|---|
| 6262 |  | 
|---|
| 6263 | err = ext4_emergency_state(sb: inode->i_sb); | 
|---|
| 6264 | if (unlikely(err)) { | 
|---|
| 6265 | put_bh(bh: iloc->bh); | 
|---|
| 6266 | return err; | 
|---|
| 6267 | } | 
|---|
| 6268 | ext4_fc_track_inode(handle, inode); | 
|---|
| 6269 |  | 
|---|
| 6270 | /* the do_update_inode consumes one bh->b_count */ | 
|---|
| 6271 | get_bh(bh: iloc->bh); | 
|---|
| 6272 |  | 
|---|
| 6273 | /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ | 
|---|
| 6274 | err = ext4_do_update_inode(handle, inode, iloc); | 
|---|
| 6275 | put_bh(bh: iloc->bh); | 
|---|
| 6276 | return err; | 
|---|
| 6277 | } | 
|---|
| 6278 |  | 
|---|
| 6279 | /* | 
|---|
| 6280 | * On success, We end up with an outstanding reference count against | 
|---|
| 6281 | * iloc->bh.  This _must_ be cleaned up later. | 
|---|
| 6282 | */ | 
|---|
| 6283 |  | 
|---|
| 6284 | int | 
|---|
| 6285 | ext4_reserve_inode_write(handle_t *handle, struct inode *inode, | 
|---|
| 6286 | struct ext4_iloc *iloc) | 
|---|
| 6287 | { | 
|---|
| 6288 | int err; | 
|---|
| 6289 |  | 
|---|
| 6290 | err = ext4_emergency_state(sb: inode->i_sb); | 
|---|
| 6291 | if (unlikely(err)) | 
|---|
| 6292 | return err; | 
|---|
| 6293 |  | 
|---|
| 6294 | err = ext4_get_inode_loc(inode, iloc); | 
|---|
| 6295 | if (!err) { | 
|---|
| 6296 | BUFFER_TRACE(iloc->bh, "get_write_access"); | 
|---|
| 6297 | err = ext4_journal_get_write_access(handle, inode->i_sb, | 
|---|
| 6298 | iloc->bh, EXT4_JTR_NONE); | 
|---|
| 6299 | if (err) { | 
|---|
| 6300 | brelse(bh: iloc->bh); | 
|---|
| 6301 | iloc->bh = NULL; | 
|---|
| 6302 | } | 
|---|
| 6303 | ext4_fc_track_inode(handle, inode); | 
|---|
| 6304 | } | 
|---|
| 6305 | ext4_std_error(inode->i_sb, err); | 
|---|
| 6306 | return err; | 
|---|
| 6307 | } | 
|---|
| 6308 |  | 
|---|
| 6309 | static int __ext4_expand_extra_isize(struct inode *inode, | 
|---|
| 6310 | unsigned int , | 
|---|
| 6311 | struct ext4_iloc *iloc, | 
|---|
| 6312 | handle_t *handle, int *no_expand) | 
|---|
| 6313 | { | 
|---|
| 6314 | struct ext4_inode *raw_inode; | 
|---|
| 6315 | struct ext4_xattr_ibody_header *; | 
|---|
| 6316 | unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); | 
|---|
| 6317 | struct ext4_inode_info *ei = EXT4_I(inode); | 
|---|
| 6318 | int error; | 
|---|
| 6319 |  | 
|---|
| 6320 | /* this was checked at iget time, but double check for good measure */ | 
|---|
| 6321 | if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || | 
|---|
| 6322 | (ei->i_extra_isize & 3)) { | 
|---|
| 6323 | EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", | 
|---|
| 6324 | ei->i_extra_isize, | 
|---|
| 6325 | EXT4_INODE_SIZE(inode->i_sb)); | 
|---|
| 6326 | return -EFSCORRUPTED; | 
|---|
| 6327 | } | 
|---|
| 6328 | if ((new_extra_isize < ei->i_extra_isize) || | 
|---|
| 6329 | (new_extra_isize < 4) || | 
|---|
| 6330 | (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) | 
|---|
| 6331 | return -EINVAL;	/* Should never happen */ | 
|---|
| 6332 |  | 
|---|
| 6333 | raw_inode = ext4_raw_inode(iloc); | 
|---|
| 6334 |  | 
|---|
| 6335 | header = IHDR(inode, raw_inode); | 
|---|
| 6336 |  | 
|---|
| 6337 | /* No extended attributes present */ | 
|---|
| 6338 | if (!ext4_test_inode_state(inode, bit: EXT4_STATE_XATTR) || | 
|---|
| 6339 | header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { | 
|---|
| 6340 | memset(s: (void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + | 
|---|
| 6341 | EXT4_I(inode)->i_extra_isize, c: 0, | 
|---|
| 6342 | n: new_extra_isize - EXT4_I(inode)->i_extra_isize); | 
|---|
| 6343 | EXT4_I(inode)->i_extra_isize = new_extra_isize; | 
|---|
| 6344 | return 0; | 
|---|
| 6345 | } | 
|---|
| 6346 |  | 
|---|
| 6347 | /* | 
|---|
| 6348 | * We may need to allocate external xattr block so we need quotas | 
|---|
| 6349 | * initialized. Here we can be called with various locks held so we | 
|---|
| 6350 | * cannot affort to initialize quotas ourselves. So just bail. | 
|---|
| 6351 | */ | 
|---|
| 6352 | if (dquot_initialize_needed(inode)) | 
|---|
| 6353 | return -EAGAIN; | 
|---|
| 6354 |  | 
|---|
| 6355 | /* try to expand with EAs present */ | 
|---|
| 6356 | error = ext4_expand_extra_isize_ea(inode, new_extra_isize, | 
|---|
| 6357 | raw_inode, handle); | 
|---|
| 6358 | if (error) { | 
|---|
| 6359 | /* | 
|---|
| 6360 | * Inode size expansion failed; don't try again | 
|---|
| 6361 | */ | 
|---|
| 6362 | *no_expand = 1; | 
|---|
| 6363 | } | 
|---|
| 6364 |  | 
|---|
| 6365 | return error; | 
|---|
| 6366 | } | 
|---|
| 6367 |  | 
|---|
| 6368 | /* | 
|---|
| 6369 | * Expand an inode by new_extra_isize bytes. | 
|---|
| 6370 | * Returns 0 on success or negative error number on failure. | 
|---|
| 6371 | */ | 
|---|
| 6372 | static int ext4_try_to_expand_extra_isize(struct inode *inode, | 
|---|
| 6373 | unsigned int , | 
|---|
| 6374 | struct ext4_iloc iloc, | 
|---|
| 6375 | handle_t *handle) | 
|---|
| 6376 | { | 
|---|
| 6377 | int no_expand; | 
|---|
| 6378 | int error; | 
|---|
| 6379 |  | 
|---|
| 6380 | if (ext4_test_inode_state(inode, bit: EXT4_STATE_NO_EXPAND)) | 
|---|
| 6381 | return -EOVERFLOW; | 
|---|
| 6382 |  | 
|---|
| 6383 | /* | 
|---|
| 6384 | * In nojournal mode, we can immediately attempt to expand | 
|---|
| 6385 | * the inode.  When journaled, we first need to obtain extra | 
|---|
| 6386 | * buffer credits since we may write into the EA block | 
|---|
| 6387 | * with this same handle. If journal_extend fails, then it will | 
|---|
| 6388 | * only result in a minor loss of functionality for that inode. | 
|---|
| 6389 | * If this is felt to be critical, then e2fsck should be run to | 
|---|
| 6390 | * force a large enough s_min_extra_isize. | 
|---|
| 6391 | */ | 
|---|
| 6392 | if (ext4_journal_extend(handle, | 
|---|
| 6393 | EXT4_DATA_TRANS_BLOCKS(inode->i_sb), revoke: 0) != 0) | 
|---|
| 6394 | return -ENOSPC; | 
|---|
| 6395 |  | 
|---|
| 6396 | if (ext4_write_trylock_xattr(inode, save: &no_expand) == 0) | 
|---|
| 6397 | return -EBUSY; | 
|---|
| 6398 |  | 
|---|
| 6399 | error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc: &iloc, | 
|---|
| 6400 | handle, no_expand: &no_expand); | 
|---|
| 6401 | ext4_write_unlock_xattr(inode, save: &no_expand); | 
|---|
| 6402 |  | 
|---|
| 6403 | return error; | 
|---|
| 6404 | } | 
|---|
| 6405 |  | 
|---|
| 6406 | int ext4_expand_extra_isize(struct inode *inode, | 
|---|
| 6407 | unsigned int , | 
|---|
| 6408 | struct ext4_iloc *iloc) | 
|---|
| 6409 | { | 
|---|
| 6410 | handle_t *handle; | 
|---|
| 6411 | int no_expand; | 
|---|
| 6412 | int error, rc; | 
|---|
| 6413 |  | 
|---|
| 6414 | if (ext4_test_inode_state(inode, bit: EXT4_STATE_NO_EXPAND)) { | 
|---|
| 6415 | brelse(bh: iloc->bh); | 
|---|
| 6416 | return -EOVERFLOW; | 
|---|
| 6417 | } | 
|---|
| 6418 |  | 
|---|
| 6419 | handle = ext4_journal_start(inode, EXT4_HT_INODE, | 
|---|
| 6420 | EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); | 
|---|
| 6421 | if (IS_ERR(ptr: handle)) { | 
|---|
| 6422 | error = PTR_ERR(ptr: handle); | 
|---|
| 6423 | brelse(bh: iloc->bh); | 
|---|
| 6424 | return error; | 
|---|
| 6425 | } | 
|---|
| 6426 |  | 
|---|
| 6427 | ext4_write_lock_xattr(inode, save: &no_expand); | 
|---|
| 6428 |  | 
|---|
| 6429 | BUFFER_TRACE(iloc->bh, "get_write_access"); | 
|---|
| 6430 | error = ext4_journal_get_write_access(handle, inode->i_sb, iloc->bh, | 
|---|
| 6431 | EXT4_JTR_NONE); | 
|---|
| 6432 | if (error) { | 
|---|
| 6433 | brelse(bh: iloc->bh); | 
|---|
| 6434 | goto out_unlock; | 
|---|
| 6435 | } | 
|---|
| 6436 |  | 
|---|
| 6437 | error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, | 
|---|
| 6438 | handle, no_expand: &no_expand); | 
|---|
| 6439 |  | 
|---|
| 6440 | rc = ext4_mark_iloc_dirty(handle, inode, iloc); | 
|---|
| 6441 | if (!error) | 
|---|
| 6442 | error = rc; | 
|---|
| 6443 |  | 
|---|
| 6444 | out_unlock: | 
|---|
| 6445 | ext4_write_unlock_xattr(inode, save: &no_expand); | 
|---|
| 6446 | ext4_journal_stop(handle); | 
|---|
| 6447 | return error; | 
|---|
| 6448 | } | 
|---|
| 6449 |  | 
|---|
| 6450 | /* | 
|---|
| 6451 | * What we do here is to mark the in-core inode as clean with respect to inode | 
|---|
| 6452 | * dirtiness (it may still be data-dirty). | 
|---|
| 6453 | * This means that the in-core inode may be reaped by prune_icache | 
|---|
| 6454 | * without having to perform any I/O.  This is a very good thing, | 
|---|
| 6455 | * because *any* task may call prune_icache - even ones which | 
|---|
| 6456 | * have a transaction open against a different journal. | 
|---|
| 6457 | * | 
|---|
| 6458 | * Is this cheating?  Not really.  Sure, we haven't written the | 
|---|
| 6459 | * inode out, but prune_icache isn't a user-visible syncing function. | 
|---|
| 6460 | * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) | 
|---|
| 6461 | * we start and wait on commits. | 
|---|
| 6462 | */ | 
|---|
| 6463 | int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode, | 
|---|
| 6464 | const char *func, unsigned int line) | 
|---|
| 6465 | { | 
|---|
| 6466 | struct ext4_iloc iloc; | 
|---|
| 6467 | struct ext4_sb_info *sbi = EXT4_SB(sb: inode->i_sb); | 
|---|
| 6468 | int err; | 
|---|
| 6469 |  | 
|---|
| 6470 | might_sleep(); | 
|---|
| 6471 | trace_ext4_mark_inode_dirty(inode, _RET_IP_); | 
|---|
| 6472 | err = ext4_reserve_inode_write(handle, inode, iloc: &iloc); | 
|---|
| 6473 | if (err) | 
|---|
| 6474 | goto out; | 
|---|
| 6475 |  | 
|---|
| 6476 | if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) | 
|---|
| 6477 | ext4_try_to_expand_extra_isize(inode, new_extra_isize: sbi->s_want_extra_isize, | 
|---|
| 6478 | iloc, handle); | 
|---|
| 6479 |  | 
|---|
| 6480 | err = ext4_mark_iloc_dirty(handle, inode, iloc: &iloc); | 
|---|
| 6481 | out: | 
|---|
| 6482 | if (unlikely(err)) | 
|---|
| 6483 | ext4_error_inode_err(inode, func, line, 0, err, | 
|---|
| 6484 | "mark_inode_dirty error"); | 
|---|
| 6485 | return err; | 
|---|
| 6486 | } | 
|---|
| 6487 |  | 
|---|
| 6488 | /* | 
|---|
| 6489 | * ext4_dirty_inode() is called from __mark_inode_dirty() | 
|---|
| 6490 | * | 
|---|
| 6491 | * We're really interested in the case where a file is being extended. | 
|---|
| 6492 | * i_size has been changed by generic_commit_write() and we thus need | 
|---|
| 6493 | * to include the updated inode in the current transaction. | 
|---|
| 6494 | * | 
|---|
| 6495 | * Also, dquot_alloc_block() will always dirty the inode when blocks | 
|---|
| 6496 | * are allocated to the file. | 
|---|
| 6497 | * | 
|---|
| 6498 | * If the inode is marked synchronous, we don't honour that here - doing | 
|---|
| 6499 | * so would cause a commit on atime updates, which we don't bother doing. | 
|---|
| 6500 | * We handle synchronous inodes at the highest possible level. | 
|---|
| 6501 | */ | 
|---|
| 6502 | void ext4_dirty_inode(struct inode *inode, int flags) | 
|---|
| 6503 | { | 
|---|
| 6504 | handle_t *handle; | 
|---|
| 6505 |  | 
|---|
| 6506 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); | 
|---|
| 6507 | if (IS_ERR(ptr: handle)) | 
|---|
| 6508 | return; | 
|---|
| 6509 | ext4_mark_inode_dirty(handle, inode); | 
|---|
| 6510 | ext4_journal_stop(handle); | 
|---|
| 6511 | } | 
|---|
| 6512 |  | 
|---|
| 6513 | int ext4_change_inode_journal_flag(struct inode *inode, int val) | 
|---|
| 6514 | { | 
|---|
| 6515 | journal_t *journal; | 
|---|
| 6516 | handle_t *handle; | 
|---|
| 6517 | int err; | 
|---|
| 6518 | int alloc_ctx; | 
|---|
| 6519 |  | 
|---|
| 6520 | /* | 
|---|
| 6521 | * We have to be very careful here: changing a data block's | 
|---|
| 6522 | * journaling status dynamically is dangerous.  If we write a | 
|---|
| 6523 | * data block to the journal, change the status and then delete | 
|---|
| 6524 | * that block, we risk forgetting to revoke the old log record | 
|---|
| 6525 | * from the journal and so a subsequent replay can corrupt data. | 
|---|
| 6526 | * So, first we make sure that the journal is empty and that | 
|---|
| 6527 | * nobody is changing anything. | 
|---|
| 6528 | */ | 
|---|
| 6529 |  | 
|---|
| 6530 | journal = EXT4_JOURNAL(inode); | 
|---|
| 6531 | if (!journal) | 
|---|
| 6532 | return 0; | 
|---|
| 6533 | if (is_journal_aborted(journal)) | 
|---|
| 6534 | return -EROFS; | 
|---|
| 6535 |  | 
|---|
| 6536 | /* Wait for all existing dio workers */ | 
|---|
| 6537 | inode_dio_wait(inode); | 
|---|
| 6538 |  | 
|---|
| 6539 | /* | 
|---|
| 6540 | * Before flushing the journal and switching inode's aops, we have | 
|---|
| 6541 | * to flush all dirty data the inode has. There can be outstanding | 
|---|
| 6542 | * delayed allocations, there can be unwritten extents created by | 
|---|
| 6543 | * fallocate or buffered writes in dioread_nolock mode covered by | 
|---|
| 6544 | * dirty data which can be converted only after flushing the dirty | 
|---|
| 6545 | * data (and journalled aops don't know how to handle these cases). | 
|---|
| 6546 | */ | 
|---|
| 6547 | if (val) { | 
|---|
| 6548 | filemap_invalidate_lock(mapping: inode->i_mapping); | 
|---|
| 6549 | err = filemap_write_and_wait(mapping: inode->i_mapping); | 
|---|
| 6550 | if (err < 0) { | 
|---|
| 6551 | filemap_invalidate_unlock(mapping: inode->i_mapping); | 
|---|
| 6552 | return err; | 
|---|
| 6553 | } | 
|---|
| 6554 | } | 
|---|
| 6555 |  | 
|---|
| 6556 | alloc_ctx = ext4_writepages_down_write(sb: inode->i_sb); | 
|---|
| 6557 | jbd2_journal_lock_updates(journal); | 
|---|
| 6558 |  | 
|---|
| 6559 | /* | 
|---|
| 6560 | * OK, there are no updates running now, and all cached data is | 
|---|
| 6561 | * synced to disk.  We are now in a completely consistent state | 
|---|
| 6562 | * which doesn't have anything in the journal, and we know that | 
|---|
| 6563 | * no filesystem updates are running, so it is safe to modify | 
|---|
| 6564 | * the inode's in-core data-journaling state flag now. | 
|---|
| 6565 | */ | 
|---|
| 6566 |  | 
|---|
| 6567 | if (val) | 
|---|
| 6568 | ext4_set_inode_flag(inode, bit: EXT4_INODE_JOURNAL_DATA); | 
|---|
| 6569 | else { | 
|---|
| 6570 | err = jbd2_journal_flush(journal, flags: 0); | 
|---|
| 6571 | if (err < 0) { | 
|---|
| 6572 | jbd2_journal_unlock_updates(journal); | 
|---|
| 6573 | ext4_writepages_up_write(sb: inode->i_sb, ctx: alloc_ctx); | 
|---|
| 6574 | return err; | 
|---|
| 6575 | } | 
|---|
| 6576 | ext4_clear_inode_flag(inode, bit: EXT4_INODE_JOURNAL_DATA); | 
|---|
| 6577 | } | 
|---|
| 6578 | ext4_set_aops(inode); | 
|---|
| 6579 |  | 
|---|
| 6580 | jbd2_journal_unlock_updates(journal); | 
|---|
| 6581 | ext4_writepages_up_write(sb: inode->i_sb, ctx: alloc_ctx); | 
|---|
| 6582 |  | 
|---|
| 6583 | if (val) | 
|---|
| 6584 | filemap_invalidate_unlock(mapping: inode->i_mapping); | 
|---|
| 6585 |  | 
|---|
| 6586 | /* Finally we can mark the inode as dirty. */ | 
|---|
| 6587 |  | 
|---|
| 6588 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); | 
|---|
| 6589 | if (IS_ERR(ptr: handle)) | 
|---|
| 6590 | return PTR_ERR(ptr: handle); | 
|---|
| 6591 |  | 
|---|
| 6592 | ext4_fc_mark_ineligible(sb: inode->i_sb, | 
|---|
| 6593 | reason: EXT4_FC_REASON_JOURNAL_FLAG_CHANGE, handle); | 
|---|
| 6594 | err = ext4_mark_inode_dirty(handle, inode); | 
|---|
| 6595 | ext4_handle_sync(handle); | 
|---|
| 6596 | ext4_journal_stop(handle); | 
|---|
| 6597 | ext4_std_error(inode->i_sb, err); | 
|---|
| 6598 |  | 
|---|
| 6599 | return err; | 
|---|
| 6600 | } | 
|---|
| 6601 |  | 
|---|
| 6602 | static int ext4_bh_unmapped(handle_t *handle, struct inode *inode, | 
|---|
| 6603 | struct buffer_head *bh) | 
|---|
| 6604 | { | 
|---|
| 6605 | return !buffer_mapped(bh); | 
|---|
| 6606 | } | 
|---|
| 6607 |  | 
|---|
| 6608 | static int ext4_block_page_mkwrite(struct inode *inode, struct folio *folio, | 
|---|
| 6609 | get_block_t get_block) | 
|---|
| 6610 | { | 
|---|
| 6611 | handle_t *handle; | 
|---|
| 6612 | loff_t size; | 
|---|
| 6613 | unsigned long len; | 
|---|
| 6614 | int credits; | 
|---|
| 6615 | int ret; | 
|---|
| 6616 |  | 
|---|
| 6617 | credits = ext4_chunk_trans_extent(inode, | 
|---|
| 6618 | nrblocks: ext4_journal_blocks_per_folio(inode)); | 
|---|
| 6619 | handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, credits); | 
|---|
| 6620 | if (IS_ERR(ptr: handle)) | 
|---|
| 6621 | return PTR_ERR(ptr: handle); | 
|---|
| 6622 |  | 
|---|
| 6623 | folio_lock(folio); | 
|---|
| 6624 | size = i_size_read(inode); | 
|---|
| 6625 | /* Page got truncated from under us? */ | 
|---|
| 6626 | if (folio->mapping != inode->i_mapping || folio_pos(folio) > size) { | 
|---|
| 6627 | ret = -EFAULT; | 
|---|
| 6628 | goto out_error; | 
|---|
| 6629 | } | 
|---|
| 6630 |  | 
|---|
| 6631 | len = folio_size(folio); | 
|---|
| 6632 | if (folio_pos(folio) + len > size) | 
|---|
| 6633 | len = size - folio_pos(folio); | 
|---|
| 6634 |  | 
|---|
| 6635 | ret = ext4_block_write_begin(handle, folio, pos: 0, len, get_block); | 
|---|
| 6636 | if (ret) | 
|---|
| 6637 | goto out_error; | 
|---|
| 6638 |  | 
|---|
| 6639 | if (!ext4_should_journal_data(inode)) { | 
|---|
| 6640 | block_commit_write(folio, from: 0, to: len); | 
|---|
| 6641 | folio_mark_dirty(folio); | 
|---|
| 6642 | } else { | 
|---|
| 6643 | ret = ext4_journal_folio_buffers(handle, folio, len); | 
|---|
| 6644 | if (ret) | 
|---|
| 6645 | goto out_error; | 
|---|
| 6646 | } | 
|---|
| 6647 | ext4_journal_stop(handle); | 
|---|
| 6648 | folio_wait_stable(folio); | 
|---|
| 6649 | return ret; | 
|---|
| 6650 |  | 
|---|
| 6651 | out_error: | 
|---|
| 6652 | folio_unlock(folio); | 
|---|
| 6653 | ext4_journal_stop(handle); | 
|---|
| 6654 | return ret; | 
|---|
| 6655 | } | 
|---|
| 6656 |  | 
|---|
| 6657 | vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) | 
|---|
| 6658 | { | 
|---|
| 6659 | struct vm_area_struct *vma = vmf->vma; | 
|---|
| 6660 | struct folio *folio = page_folio(vmf->page); | 
|---|
| 6661 | loff_t size; | 
|---|
| 6662 | unsigned long len; | 
|---|
| 6663 | int err; | 
|---|
| 6664 | vm_fault_t ret; | 
|---|
| 6665 | struct file *file = vma->vm_file; | 
|---|
| 6666 | struct inode *inode = file_inode(f: file); | 
|---|
| 6667 | struct address_space *mapping = inode->i_mapping; | 
|---|
| 6668 | get_block_t *get_block = ext4_get_block; | 
|---|
| 6669 | int retries = 0; | 
|---|
| 6670 |  | 
|---|
| 6671 | if (unlikely(IS_IMMUTABLE(inode))) | 
|---|
| 6672 | return VM_FAULT_SIGBUS; | 
|---|
| 6673 |  | 
|---|
| 6674 | sb_start_pagefault(sb: inode->i_sb); | 
|---|
| 6675 | file_update_time(file: vma->vm_file); | 
|---|
| 6676 |  | 
|---|
| 6677 | filemap_invalidate_lock_shared(mapping); | 
|---|
| 6678 |  | 
|---|
| 6679 | err = ext4_convert_inline_data(inode); | 
|---|
| 6680 | if (err) | 
|---|
| 6681 | goto out_ret; | 
|---|
| 6682 |  | 
|---|
| 6683 | /* | 
|---|
| 6684 | * On data journalling we skip straight to the transaction handle: | 
|---|
| 6685 | * there's no delalloc; page truncated will be checked later; the | 
|---|
| 6686 | * early return w/ all buffers mapped (calculates size/len) can't | 
|---|
| 6687 | * be used; and there's no dioread_nolock, so only ext4_get_block. | 
|---|
| 6688 | */ | 
|---|
| 6689 | if (ext4_should_journal_data(inode)) | 
|---|
| 6690 | goto retry_alloc; | 
|---|
| 6691 |  | 
|---|
| 6692 | /* Delalloc case is easy... */ | 
|---|
| 6693 | if (test_opt(inode->i_sb, DELALLOC) && | 
|---|
| 6694 | !ext4_nonda_switch(sb: inode->i_sb)) { | 
|---|
| 6695 | do { | 
|---|
| 6696 | err = block_page_mkwrite(vma, vmf, | 
|---|
| 6697 | get_block: ext4_da_get_block_prep); | 
|---|
| 6698 | } while (err == -ENOSPC && | 
|---|
| 6699 | ext4_should_retry_alloc(sb: inode->i_sb, retries: &retries)); | 
|---|
| 6700 | goto out_ret; | 
|---|
| 6701 | } | 
|---|
| 6702 |  | 
|---|
| 6703 | folio_lock(folio); | 
|---|
| 6704 | size = i_size_read(inode); | 
|---|
| 6705 | /* Page got truncated from under us? */ | 
|---|
| 6706 | if (folio->mapping != mapping || folio_pos(folio) > size) { | 
|---|
| 6707 | folio_unlock(folio); | 
|---|
| 6708 | ret = VM_FAULT_NOPAGE; | 
|---|
| 6709 | goto out; | 
|---|
| 6710 | } | 
|---|
| 6711 |  | 
|---|
| 6712 | len = folio_size(folio); | 
|---|
| 6713 | if (folio_pos(folio) + len > size) | 
|---|
| 6714 | len = size - folio_pos(folio); | 
|---|
| 6715 | /* | 
|---|
| 6716 | * Return if we have all the buffers mapped. This avoids the need to do | 
|---|
| 6717 | * journal_start/journal_stop which can block and take a long time | 
|---|
| 6718 | * | 
|---|
| 6719 | * This cannot be done for data journalling, as we have to add the | 
|---|
| 6720 | * inode to the transaction's list to writeprotect pages on commit. | 
|---|
| 6721 | */ | 
|---|
| 6722 | if (folio_buffers(folio)) { | 
|---|
| 6723 | if (!ext4_walk_page_buffers(NULL, inode, folio_buffers(folio), | 
|---|
| 6724 | from: 0, to: len, NULL, | 
|---|
| 6725 | fn: ext4_bh_unmapped)) { | 
|---|
| 6726 | /* Wait so that we don't change page under IO */ | 
|---|
| 6727 | folio_wait_stable(folio); | 
|---|
| 6728 | ret = VM_FAULT_LOCKED; | 
|---|
| 6729 | goto out; | 
|---|
| 6730 | } | 
|---|
| 6731 | } | 
|---|
| 6732 | folio_unlock(folio); | 
|---|
| 6733 | /* OK, we need to fill the hole... */ | 
|---|
| 6734 | if (ext4_should_dioread_nolock(inode)) | 
|---|
| 6735 | get_block = ext4_get_block_unwritten; | 
|---|
| 6736 | retry_alloc: | 
|---|
| 6737 | /* Start journal and allocate blocks */ | 
|---|
| 6738 | err = ext4_block_page_mkwrite(inode, folio, get_block); | 
|---|
| 6739 | if (err == -EAGAIN || | 
|---|
| 6740 | (err == -ENOSPC && ext4_should_retry_alloc(sb: inode->i_sb, retries: &retries))) | 
|---|
| 6741 | goto retry_alloc; | 
|---|
| 6742 | out_ret: | 
|---|
| 6743 | ret = vmf_fs_error(err); | 
|---|
| 6744 | out: | 
|---|
| 6745 | filemap_invalidate_unlock_shared(mapping); | 
|---|
| 6746 | sb_end_pagefault(sb: inode->i_sb); | 
|---|
| 6747 | return ret; | 
|---|
| 6748 | } | 
|---|
| 6749 |  | 
|---|