LCOV - code coverage report
Current view: top level - fs/ext4 - inode.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 1091 2191 49.8 %
Date: 2020-09-30 20:25:40 Functions: 69 117 59.0 %
Branches: 721 2029 35.5 %

           Branch data     Line data    Source code
       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/time.h>
      24                 :            : #include <linux/highuid.h>
      25                 :            : #include <linux/pagemap.h>
      26                 :            : #include <linux/dax.h>
      27                 :            : #include <linux/quotaops.h>
      28                 :            : #include <linux/string.h>
      29                 :            : #include <linux/buffer_head.h>
      30                 :            : #include <linux/writeback.h>
      31                 :            : #include <linux/pagevec.h>
      32                 :            : #include <linux/mpage.h>
      33                 :            : #include <linux/namei.h>
      34                 :            : #include <linux/uio.h>
      35                 :            : #include <linux/bio.h>
      36                 :            : #include <linux/workqueue.h>
      37                 :            : #include <linux/kernel.h>
      38                 :            : #include <linux/printk.h>
      39                 :            : #include <linux/slab.h>
      40                 :            : #include <linux/bitops.h>
      41                 :            : #include <linux/iomap.h>
      42                 :            : #include <linux/iversion.h>
      43                 :            : 
      44                 :            : #include "ext4_jbd2.h"
      45                 :            : #include "xattr.h"
      46                 :            : #include "acl.h"
      47                 :            : #include "truncate.h"
      48                 :            : 
      49                 :            : #include <trace/events/ext4.h>
      50                 :            : 
      51                 :            : #define MPAGE_DA_EXTENT_TAIL 0x01
      52                 :            : 
      53                 :          0 : static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
      54                 :            :                               struct ext4_inode_info *ei)
      55                 :            : {
      56                 :          0 :         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
      57                 :            :         __u32 csum;
      58                 :          0 :         __u16 dummy_csum = 0;
      59                 :            :         int offset = offsetof(struct ext4_inode, i_checksum_lo);
      60                 :            :         unsigned int csum_size = sizeof(dummy_csum);
      61                 :            : 
      62                 :          0 :         csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset);
      63                 :          0 :         csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size);
      64                 :            :         offset += csum_size;
      65                 :          0 :         csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
      66                 :            :                            EXT4_GOOD_OLD_INODE_SIZE - offset);
      67                 :            : 
      68         [ #  # ]:          0 :         if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
      69                 :            :                 offset = offsetof(struct ext4_inode, i_checksum_hi);
      70                 :          0 :                 csum = ext4_chksum(sbi, csum, (__u8 *)raw +
      71                 :            :                                    EXT4_GOOD_OLD_INODE_SIZE,
      72                 :            :                                    offset - EXT4_GOOD_OLD_INODE_SIZE);
      73         [ #  # ]:          0 :                 if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
      74                 :          0 :                         csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum,
      75                 :            :                                            csum_size);
      76                 :            :                         offset += csum_size;
      77                 :            :                 }
      78                 :          0 :                 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
      79                 :          0 :                                    EXT4_INODE_SIZE(inode->i_sb) - offset);
      80                 :            :         }
      81                 :            : 
      82                 :          0 :         return csum;
      83                 :            : }
      84                 :            : 
      85                 :     655254 : static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
      86                 :            :                                   struct ext4_inode_info *ei)
      87                 :            : {
      88                 :            :         __u32 provided, calculated;
      89                 :            : 
      90         [ +  + ]:    1310508 :         if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
      91         [ -  + ]:     655196 :             cpu_to_le32(EXT4_OS_LINUX) ||
      92                 :     655227 :             !ext4_has_metadata_csum(inode->i_sb))
      93                 :            :                 return 1;
      94                 :            : 
      95                 :          0 :         provided = le16_to_cpu(raw->i_checksum_lo);
      96                 :          0 :         calculated = ext4_inode_csum(inode, raw, ei);
      97   [ #  #  #  # ]:          0 :         if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
      98                 :          0 :             EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
      99                 :          0 :                 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
     100                 :            :         else
     101                 :          0 :                 calculated &= 0xFFFF;
     102                 :            : 
     103                 :          0 :         return provided == calculated;
     104                 :            : }
     105                 :            : 
     106                 :    1407275 : static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
     107                 :            :                                 struct ext4_inode_info *ei)
     108                 :            : {
     109                 :            :         __u32 csum;
     110                 :            : 
     111         [ +  + ]:    2814550 :         if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
     112         [ -  + ]:    1407223 :             cpu_to_le32(EXT4_OS_LINUX) ||
     113                 :    1407225 :             !ext4_has_metadata_csum(inode->i_sb))
     114                 :    1407273 :                 return;
     115                 :            : 
     116                 :          0 :         csum = ext4_inode_csum(inode, raw, ei);
     117                 :          0 :         raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
     118   [ #  #  #  # ]:          0 :         if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
     119                 :          0 :             EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
     120                 :          0 :                 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
     121                 :            : }
     122                 :            : 
     123                 :       2560 : static inline int ext4_begin_ordered_truncate(struct inode *inode,
     124                 :            :                                               loff_t new_size)
     125                 :            : {
     126                 :       2560 :         trace_ext4_begin_ordered_truncate(inode, new_size);
     127                 :            :         /*
     128                 :            :          * If jinode is zero, then we never opened the file for
     129                 :            :          * writing, so there's no need to call
     130                 :            :          * jbd2_journal_begin_ordered_truncate() since there's no
     131                 :            :          * outstanding writes we need to flush.
     132                 :            :          */
     133         [ +  + ]:       2560 :         if (!EXT4_I(inode)->jinode)
     134                 :            :                 return 0;
     135                 :       2580 :         return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
     136                 :            :                                                    EXT4_I(inode)->jinode,
     137                 :            :                                                    new_size);
     138                 :            : }
     139                 :            : 
     140                 :            : static void ext4_invalidatepage(struct page *page, unsigned int offset,
     141                 :            :                                 unsigned int length);
     142                 :            : static int __ext4_journalled_writepage(struct page *page, unsigned int len);
     143                 :            : static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
     144                 :            : static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
     145                 :            :                                   int pextents);
     146                 :            : 
     147                 :            : /*
     148                 :            :  * Test whether an inode is a fast symlink.
     149                 :            :  * A fast symlink has its symlink data stored in ext4_inode_info->i_data.
     150                 :            :  */
     151                 :     176890 : int ext4_inode_is_fast_symlink(struct inode *inode)
     152                 :            : {
     153         [ +  - ]:     176890 :         if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
     154         [ -  + ]:     176890 :                 int ea_blocks = EXT4_I(inode)->i_file_acl ?
     155                 :          0 :                                 EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
     156                 :            : 
     157         [ +  - ]:     176890 :                 if (ext4_has_inline_data(inode))
     158                 :            :                         return 0;
     159                 :            : 
     160   [ +  +  +  + ]:     176897 :                 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
     161                 :            :         }
     162   [ #  #  #  #  :          0 :         return S_ISLNK(inode->i_mode) && inode->i_size &&
                   #  # ]
     163                 :            :                (inode->i_size < EXT4_N_BLOCKS * 4);
     164                 :            : }
     165                 :            : 
     166                 :            : /*
     167                 :            :  * Restart the transaction associated with *handle.  This does a commit,
     168                 :            :  * so before we call here everything must be consistently dirtied against
     169                 :            :  * this transaction.
     170                 :            :  */
     171                 :          0 : int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
     172                 :            :                                  int nblocks)
     173                 :            : {
     174                 :            :         int ret;
     175                 :            : 
     176                 :            :         /*
     177                 :            :          * Drop i_data_sem to avoid deadlock with ext4_map_blocks.  At this
     178                 :            :          * moment, get_block can be called only for blocks inside i_size since
     179                 :            :          * page cache has been already dropped and writes are blocked by
     180                 :            :          * i_mutex. So we can safely drop the i_data_sem here.
     181                 :            :          */
     182         [ #  # ]:          0 :         BUG_ON(EXT4_JOURNAL(inode) == NULL);
     183                 :            :         jbd_debug(2, "restarting handle %p\n", handle);
     184                 :          0 :         up_write(&EXT4_I(inode)->i_data_sem);
     185                 :            :         ret = ext4_journal_restart(handle, nblocks);
     186                 :          0 :         down_write(&EXT4_I(inode)->i_data_sem);
     187                 :          0 :         ext4_discard_preallocations(inode);
     188                 :            : 
     189                 :          0 :         return ret;
     190                 :            : }
     191                 :            : 
     192                 :            : /*
     193                 :            :  * Called at the last iput() if i_nlink is zero.
     194                 :            :  */
     195                 :       3292 : void ext4_evict_inode(struct inode *inode)
     196                 :            : {
     197                 :            :         handle_t *handle;
     198                 :            :         int err;
     199                 :            :         /*
     200                 :            :          * Credits for final inode cleanup and freeing:
     201                 :            :          * sb + inode (ext4_orphan_del()), block bitmap, group descriptor
     202                 :            :          * (xattr block freeing), bitmap, group descriptor (inode freeing)
     203                 :            :          */
     204                 :            :         int extra_credits = 6;
     205                 :       3292 :         struct ext4_xattr_inode_array *ea_inode_array = NULL;
     206                 :            : 
     207                 :       3292 :         trace_ext4_evict_inode(inode);
     208                 :            : 
     209         [ -  + ]:       3291 :         if (inode->i_nlink) {
     210                 :            :                 /*
     211                 :            :                  * When journalling data dirty buffers are tracked only in the
     212                 :            :                  * journal. So although mm thinks everything is clean and
     213                 :            :                  * ready for reaping the inode might still have some pages to
     214                 :            :                  * write in the running transaction or waiting to be
     215                 :            :                  * checkpointed. Thus calling jbd2_journal_invalidatepage()
     216                 :            :                  * (via truncate_inode_pages()) to discard these buffers can
     217                 :            :                  * cause data loss. Also even if we did not discard these
     218                 :            :                  * buffers, we would have no way to find them after the inode
     219                 :            :                  * is reaped and thus user could see stale data if he tries to
     220                 :            :                  * read them before the transaction is checkpointed. So be
     221                 :            :                  * careful and force everything to disk here... We use
     222                 :            :                  * ei->i_datasync_tid to store the newest transaction
     223                 :            :                  * containing inode's data.
     224                 :            :                  *
     225                 :            :                  * Note that directories do not have this problem because they
     226                 :            :                  * don't use page cache.
     227                 :            :                  */
     228   [ #  #  #  # ]:          0 :                 if (inode->i_ino != EXT4_JOURNAL_INO &&
     229         [ #  # ]:          0 :                     ext4_should_journal_data(inode) &&
     230         [ #  # ]:          0 :                     (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
     231                 :          0 :                     inode->i_data.nrpages) {
     232                 :          0 :                         journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
     233                 :          0 :                         tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
     234                 :            : 
     235                 :          0 :                         jbd2_complete_transaction(journal, commit_tid);
     236                 :          0 :                         filemap_write_and_wait(&inode->i_data);
     237                 :            :                 }
     238                 :          0 :                 truncate_inode_pages_final(&inode->i_data);
     239                 :            : 
     240                 :          0 :                 goto no_delete;
     241                 :            :         }
     242                 :            : 
     243         [ +  - ]:       3291 :         if (is_bad_inode(inode))
     244                 :            :                 goto no_delete;
     245                 :       3292 :         dquot_initialize(inode);
     246                 :            : 
     247         [ +  + ]:       3292 :         if (ext4_should_order_data(inode))
     248                 :       1828 :                 ext4_begin_ordered_truncate(inode, 0);
     249                 :       3292 :         truncate_inode_pages_final(&inode->i_data);
     250                 :            : 
     251                 :            :         /*
     252                 :            :          * Protect us against freezing - iput() caller didn't have to have any
     253                 :            :          * protection against it
     254                 :            :          */
     255                 :       3292 :         sb_start_intwrite(inode->i_sb);
     256                 :            : 
     257         [ +  - ]:       3292 :         if (!IS_NOQUOTA(inode))
     258   [ +  +  +  - ]:       9875 :                 extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb);
     259                 :            : 
     260                 :            :         /*
     261                 :            :          * Block bitmap, group descriptor, and inode are accounted in both
     262                 :            :          * ext4_blocks_for_truncate() and extra_credits. So subtract 3.
     263                 :            :          */
     264                 :       3292 :         handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
     265                 :            :                          ext4_blocks_for_truncate(inode) + extra_credits - 3);
     266         [ -  + ]:       3292 :         if (IS_ERR(handle)) {
     267         [ #  # ]:          0 :                 ext4_std_error(inode->i_sb, PTR_ERR(handle));
     268                 :            :                 /*
     269                 :            :                  * If we're going to skip the normal cleanup, we still need to
     270                 :            :                  * make sure that the in-core orphan linked list is properly
     271                 :            :                  * cleaned up.
     272                 :            :                  */
     273                 :          0 :                 ext4_orphan_del(NULL, inode);
     274                 :          0 :                 sb_end_intwrite(inode->i_sb);
     275                 :            :                 goto no_delete;
     276                 :            :         }
     277                 :            : 
     278   [ +  -  -  + ]:       3292 :         if (IS_SYNC(inode))
     279                 :            :                 ext4_handle_sync(handle);
     280                 :            : 
     281                 :            :         /*
     282                 :            :          * Set inode->i_size to 0 before calling ext4_truncate(). We need
     283                 :            :          * special handling of symlinks here because i_size is used to
     284                 :            :          * determine whether ext4_inode_info->i_data contains symlink data or
     285                 :            :          * block mappings. Setting i_size to 0 will remove its fast symlink
     286                 :            :          * status. Erase i_data so that it becomes a valid empty block map.
     287                 :            :          */
     288         [ +  + ]:       3292 :         if (ext4_inode_is_fast_symlink(inode))
     289                 :          1 :                 memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data));
     290                 :       3292 :         inode->i_size = 0;
     291                 :       3292 :         err = ext4_mark_inode_dirty(handle, inode);
     292         [ -  + ]:       3292 :         if (err) {
     293                 :          0 :                 ext4_warning(inode->i_sb,
     294                 :            :                              "couldn't mark inode dirty (err %d)", err);
     295                 :          0 :                 goto stop_handle;
     296                 :            :         }
     297         [ +  + ]:       3292 :         if (inode->i_blocks) {
     298                 :       2809 :                 err = ext4_truncate(inode);
     299         [ -  + ]:       2809 :                 if (err) {
     300                 :          0 :                         ext4_error(inode->i_sb,
     301                 :            :                                    "couldn't truncate inode %lu (err %d)",
     302                 :            :                                    inode->i_ino, err);
     303                 :          0 :                         goto stop_handle;
     304                 :            :                 }
     305                 :            :         }
     306                 :            : 
     307                 :            :         /* Remove xattr references. */
     308                 :       3292 :         err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array,
     309                 :            :                                       extra_credits);
     310         [ -  + ]:       3292 :         if (err) {
     311                 :          0 :                 ext4_warning(inode->i_sb, "xattr delete (err %d)", err);
     312                 :            : stop_handle:
     313                 :          0 :                 ext4_journal_stop(handle);
     314                 :          0 :                 ext4_orphan_del(NULL, inode);
     315                 :          0 :                 sb_end_intwrite(inode->i_sb);
     316                 :          0 :                 ext4_xattr_inode_array_free(ea_inode_array);
     317                 :          0 :                 goto no_delete;
     318                 :            :         }
     319                 :            : 
     320                 :            :         /*
     321                 :            :          * Kill off the orphan record which ext4_truncate created.
     322                 :            :          * AKPM: I think this can be inside the above `if'.
     323                 :            :          * Note that ext4_orphan_del() has to be able to cope with the
     324                 :            :          * deletion of a non-existent orphan - this is because we don't
     325                 :            :          * know if ext4_truncate() actually created an orphan record.
     326                 :            :          * (Well, we could do this if we need to, but heck - it works)
     327                 :            :          */
     328                 :       3292 :         ext4_orphan_del(handle, inode);
     329                 :       3292 :         EXT4_I(inode)->i_dtime       = (__u32)ktime_get_real_seconds();
     330                 :            : 
     331                 :            :         /*
     332                 :            :          * One subtle ordering requirement: if anything has gone wrong
     333                 :            :          * (transaction abort, IO errors, whatever), then we can still
     334                 :            :          * do these next steps (the fs will already have been marked as
     335                 :            :          * having errors), but we can't free the inode if the mark_dirty
     336                 :            :          * fails.
     337                 :            :          */
     338         [ -  + ]:       3292 :         if (ext4_mark_inode_dirty(handle, inode))
     339                 :            :                 /* If that failed, just do the required in-core inode clear. */
     340                 :          0 :                 ext4_clear_inode(inode);
     341                 :            :         else
     342                 :       3292 :                 ext4_free_inode(handle, inode);
     343                 :       3292 :         ext4_journal_stop(handle);
     344                 :       3292 :         sb_end_intwrite(inode->i_sb);
     345                 :       3292 :         ext4_xattr_inode_array_free(ea_inode_array);
     346                 :       6584 :         return;
     347                 :            : no_delete:
     348                 :          0 :         ext4_clear_inode(inode);        /* We must guarantee clearing of inode... */
     349                 :            : }
     350                 :            : 
     351                 :            : #ifdef CONFIG_QUOTA
     352                 :     187988 : qsize_t *ext4_get_reserved_space(struct inode *inode)
     353                 :            : {
     354                 :     187988 :         return &EXT4_I(inode)->i_reserved_quota;
     355                 :            : }
     356                 :            : #endif
     357                 :            : 
     358                 :            : /*
     359                 :            :  * Called with i_data_sem down, which is important since we can call
     360                 :            :  * ext4_discard_preallocations() from here.
     361                 :            :  */
     362                 :      13636 : void ext4_da_update_reserve_space(struct inode *inode,
     363                 :            :                                         int used, int quota_claim)
     364                 :            : {
     365                 :      13636 :         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
     366                 :            :         struct ext4_inode_info *ei = EXT4_I(inode);
     367                 :            : 
     368                 :            :         spin_lock(&ei->i_block_reservation_lock);
     369                 :      13636 :         trace_ext4_da_update_reserve_space(inode, used, quota_claim);
     370         [ -  + ]:      13636 :         if (unlikely(used > ei->i_reserved_data_blocks)) {
     371                 :          0 :                 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
     372                 :            :                          "with only %d reserved data blocks",
     373                 :            :                          __func__, inode->i_ino, used,
     374                 :            :                          ei->i_reserved_data_blocks);
     375                 :          0 :                 WARN_ON(1);
     376                 :          0 :                 used = ei->i_reserved_data_blocks;
     377                 :            :         }
     378                 :            : 
     379                 :            :         /* Update per-inode reservations */
     380                 :      13636 :         ei->i_reserved_data_blocks -= used;
     381                 :      13636 :         percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
     382                 :            : 
     383                 :            :         spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
     384                 :            : 
     385                 :            :         /* Update quota subsystem for data blocks */
     386         [ +  - ]:      13636 :         if (quota_claim)
     387                 :      13636 :                 dquot_claim_block(inode, EXT4_C2B(sbi, used));
     388                 :            :         else {
     389                 :            :                 /*
     390                 :            :                  * We did fallocate with an offset that is already delayed
     391                 :            :                  * allocated. So on delayed allocated writeback we should
     392                 :            :                  * not re-claim the quota for fallocated blocks.
     393                 :            :                  */
     394                 :          0 :                 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
     395                 :            :         }
     396                 :            : 
     397                 :            :         /*
     398                 :            :          * If we have done all the pending block allocations and if
     399                 :            :          * there aren't any writers on the inode, we can discard the
     400                 :            :          * inode's preallocations.
     401                 :            :          */
     402   [ +  +  +  + ]:      27266 :         if ((ei->i_reserved_data_blocks == 0) &&
     403                 :            :             !inode_is_open_for_write(inode))
     404                 :      10012 :                 ext4_discard_preallocations(inode);
     405                 :      13636 : }
     406                 :            : 
     407                 :    3258521 : static int __check_block_validity(struct inode *inode, const char *func,
     408                 :            :                                 unsigned int line,
     409                 :            :                                 struct ext4_map_blocks *map)
     410                 :            : {
     411   [ +  +  +  + ]:    9774999 :         if (ext4_has_feature_journal(inode->i_sb) &&
     412                 :    3257957 :             (inode->i_ino ==
     413                 :    3257957 :              le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
     414                 :            :                 return 0;
     415         [ -  + ]:    3156027 :         if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
     416                 :            :                                    map->m_len)) {
     417                 :          0 :                 ext4_error_inode(inode, func, line, map->m_pblk,
     418                 :            :                                  "lblock %lu mapped to illegal pblock %llu "
     419                 :            :                                  "(length %d)", (unsigned long) map->m_lblk,
     420                 :            :                                  map->m_pblk, map->m_len);
     421                 :          0 :                 return -EFSCORRUPTED;
     422                 :            :         }
     423                 :            :         return 0;
     424                 :            : }
     425                 :            : 
     426                 :          0 : int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
     427                 :            :                        ext4_lblk_t len)
     428                 :            : {
     429                 :            :         int ret;
     430                 :            : 
     431         [ #  # ]:          0 :         if (IS_ENCRYPTED(inode))
     432                 :          0 :                 return fscrypt_zeroout_range(inode, lblk, pblk, len);
     433                 :            : 
     434                 :          0 :         ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
     435         [ #  # ]:          0 :         if (ret > 0)
     436                 :            :                 ret = 0;
     437                 :            : 
     438                 :          0 :         return ret;
     439                 :            : }
     440                 :            : 
     441                 :            : #define check_block_validity(inode, map)        \
     442                 :            :         __check_block_validity((inode), __func__, __LINE__, (map))
     443                 :            : 
     444                 :            : #ifdef ES_AGGRESSIVE_TEST
     445                 :            : static void ext4_map_blocks_es_recheck(handle_t *handle,
     446                 :            :                                        struct inode *inode,
     447                 :            :                                        struct ext4_map_blocks *es_map,
     448                 :            :                                        struct ext4_map_blocks *map,
     449                 :            :                                        int flags)
     450                 :            : {
     451                 :            :         int retval;
     452                 :            : 
     453                 :            :         map->m_flags = 0;
     454                 :            :         /*
     455                 :            :          * There is a race window that the result is not the same.
     456                 :            :          * e.g. xfstests #223 when dioread_nolock enables.  The reason
     457                 :            :          * is that we lookup a block mapping in extent status tree with
     458                 :            :          * out taking i_data_sem.  So at the time the unwritten extent
     459                 :            :          * could be converted.
     460                 :            :          */
     461                 :            :         down_read(&EXT4_I(inode)->i_data_sem);
     462                 :            :         if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
     463                 :            :                 retval = ext4_ext_map_blocks(handle, inode, map, flags &
     464                 :            :                                              EXT4_GET_BLOCKS_KEEP_SIZE);
     465                 :            :         } else {
     466                 :            :                 retval = ext4_ind_map_blocks(handle, inode, map, flags &
     467                 :            :                                              EXT4_GET_BLOCKS_KEEP_SIZE);
     468                 :            :         }
     469                 :            :         up_read((&EXT4_I(inode)->i_data_sem));
     470                 :            : 
     471                 :            :         /*
     472                 :            :          * We don't check m_len because extent will be collpased in status
     473                 :            :          * tree.  So the m_len might not equal.
     474                 :            :          */
     475                 :            :         if (es_map->m_lblk != map->m_lblk ||
     476                 :            :             es_map->m_flags != map->m_flags ||
     477                 :            :             es_map->m_pblk != map->m_pblk) {
     478                 :            :                 printk("ES cache assertion failed for inode: %lu "
     479                 :            :                        "es_cached ex [%d/%d/%llu/%x] != "
     480                 :            :                        "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
     481                 :            :                        inode->i_ino, es_map->m_lblk, es_map->m_len,
     482                 :            :                        es_map->m_pblk, es_map->m_flags, map->m_lblk,
     483                 :            :                        map->m_len, map->m_pblk, map->m_flags,
     484                 :            :                        retval, flags);
     485                 :            :         }
     486                 :            : }
     487                 :            : #endif /* ES_AGGRESSIVE_TEST */
     488                 :            : 
     489                 :            : /*
     490                 :            :  * The ext4_map_blocks() function tries to look up the requested blocks,
     491                 :            :  * and returns if the blocks are already mapped.
     492                 :            :  *
     493                 :            :  * Otherwise it takes the write lock of the i_data_sem and allocate blocks
     494                 :            :  * and store the allocated blocks in the result buffer head and mark it
     495                 :            :  * mapped.
     496                 :            :  *
     497                 :            :  * If file type is extents based, it will call ext4_ext_map_blocks(),
     498                 :            :  * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
     499                 :            :  * based files
     500                 :            :  *
     501                 :            :  * On success, it returns the number of blocks being mapped or allocated.  if
     502                 :            :  * create==0 and the blocks are pre-allocated and unwritten, the resulting @map
     503                 :            :  * is marked as unwritten. If the create == 1, it will mark @map as mapped.
     504                 :            :  *
     505                 :            :  * It returns 0 if plain look up failed (blocks have not been allocated), in
     506                 :            :  * that case, @map is returned as unmapped but we still do fill map->m_len to
     507                 :            :  * indicate the length of a hole starting at map->m_lblk.
     508                 :            :  *
     509                 :            :  * It returns the error in case of allocation failure.
     510                 :            :  */
     511                 :    8414007 : int ext4_map_blocks(handle_t *handle, struct inode *inode,
     512                 :            :                     struct ext4_map_blocks *map, int flags)
     513                 :            : {
     514                 :            :         struct extent_status es;
     515                 :            :         int retval;
     516                 :            :         int ret = 0;
     517                 :            : #ifdef ES_AGGRESSIVE_TEST
     518                 :            :         struct ext4_map_blocks orig_map;
     519                 :            : 
     520                 :            :         memcpy(&orig_map, map, sizeof(*map));
     521                 :            : #endif
     522                 :            : 
     523                 :    8414007 :         map->m_flags = 0;
     524                 :            :         ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
     525                 :            :                   "logical block %lu\n", inode->i_ino, flags, map->m_len,
     526                 :            :                   (unsigned long) map->m_lblk);
     527                 :            : 
     528                 :            :         /*
     529                 :            :          * ext4_map_blocks returns an int, and m_len is an unsigned int
     530                 :            :          */
     531         [ -  + ]:    8414007 :         if (unlikely(map->m_len > INT_MAX))
     532                 :          0 :                 map->m_len = INT_MAX;
     533                 :            : 
     534                 :            :         /* We can handle the block number less than EXT_MAX_BLOCKS */
     535         [ +  + ]:    8414007 :         if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
     536                 :            :                 return -EFSCORRUPTED;
     537                 :            : 
     538                 :            :         /* Lookup extent status tree firstly */
     539         [ +  + ]:    8414578 :         if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
     540   [ +  +  +  + ]:   13034547 :                 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
     541                 :   15692382 :                         map->m_pblk = ext4_es_pblock(&es) +
     542                 :   15692382 :                                         map->m_lblk - es.es_lblk;
     543                 :    7846191 :                         map->m_flags |= ext4_es_is_written(&es) ?
     544         [ +  + ]:    7846191 :                                         EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
     545                 :    7846191 :                         retval = es.es_len - (map->m_lblk - es.es_lblk);
     546         [ +  + ]:    7846191 :                         if (retval > map->m_len)
     547                 :    5722516 :                                 retval = map->m_len;
     548                 :    7846191 :                         map->m_len = retval;
     549   [ +  +  +  - ]:      21512 :                 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
     550                 :      17574 :                         map->m_pblk = 0;
     551                 :      17574 :                         retval = es.es_len - (map->m_lblk - es.es_lblk);
     552         [ +  + ]:      17574 :                         if (retval > map->m_len)
     553                 :          5 :                                 retval = map->m_len;
     554                 :      17574 :                         map->m_len = retval;
     555                 :      17574 :                         retval = 0;
     556                 :            :                 } else {
     557                 :          0 :                         BUG();
     558                 :            :                 }
     559                 :            : #ifdef ES_AGGRESSIVE_TEST
     560                 :            :                 ext4_map_blocks_es_recheck(handle, inode, map,
     561                 :            :                                            &orig_map, flags);
     562                 :            : #endif
     563                 :            :                 goto found;
     564                 :            :         }
     565                 :            : 
     566                 :            :         /*
     567                 :            :          * Try to see if we can get the block without requesting a new
     568                 :            :          * file system block.
     569                 :            :          */
     570                 :     552740 :         down_read(&EXT4_I(inode)->i_data_sem);
     571         [ +  - ]:     552384 :         if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
     572                 :     552384 :                 retval = ext4_ext_map_blocks(handle, inode, map, flags &
     573                 :            :                                              EXT4_GET_BLOCKS_KEEP_SIZE);
     574                 :            :         } else {
     575                 :          0 :                 retval = ext4_ind_map_blocks(handle, inode, map, flags &
     576                 :            :                                              EXT4_GET_BLOCKS_KEEP_SIZE);
     577                 :            :         }
     578         [ +  + ]:     552128 :         if (retval > 0) {
     579                 :            :                 unsigned int status;
     580                 :            : 
     581         [ -  + ]:     506343 :                 if (unlikely(retval != map->m_len)) {
     582                 :          0 :                         ext4_warning(inode->i_sb,
     583                 :            :                                      "ES len assertion failed for inode "
     584                 :            :                                      "%lu: retval %d != map->m_len %d",
     585                 :            :                                      inode->i_ino, retval, map->m_len);
     586                 :          0 :                         WARN_ON(1);
     587                 :            :                 }
     588                 :            : 
     589         [ +  + ]:     506980 :                 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
     590                 :            :                                 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
     591   [ +  -  -  + ]:    1013960 :                 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
     592         [ #  # ]:     506980 :                     !(status & EXTENT_STATUS_WRITTEN) &&
     593                 :          0 :                     ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
     594                 :          0 :                                        map->m_lblk + map->m_len - 1))
     595                 :          0 :                         status |= EXTENT_STATUS_DELAYED;
     596                 :     506980 :                 ret = ext4_es_insert_extent(inode, map->m_lblk,
     597                 :            :                                             map->m_len, map->m_pblk, status);
     598         [ -  + ]:     506953 :                 if (ret < 0)
     599                 :            :                         retval = ret;
     600                 :            :         }
     601                 :     552738 :         up_read((&EXT4_I(inode)->i_data_sem));
     602                 :            : 
     603                 :            : found:
     604   [ +  +  +  + ]:    8415442 :         if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
     605                 :    3199823 :                 ret = check_block_validity(inode, map);
     606         [ +  - ]:    3198435 :                 if (ret != 0)
     607                 :            :                         return ret;
     608                 :            :         }
     609                 :            : 
     610                 :            :         /* If it is only a block(s) look up */
     611         [ +  + ]:    8415583 :         if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
     612                 :            :                 return retval;
     613                 :            : 
     614                 :            :         /*
     615                 :            :          * Returns if the blocks have already allocated
     616                 :            :          *
     617                 :            :          * Note that if blocks have been preallocated
     618                 :            :          * ext4_ext_get_block() returns the create = 0
     619                 :            :          * with buffer head unmapped.
     620                 :            :          */
     621   [ +  +  -  + ]:      59452 :         if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
     622                 :            :                 /*
     623                 :            :                  * If we need to convert extent to unwritten
     624                 :            :                  * we continue and do the actual work in
     625                 :            :                  * ext4_ext_map_blocks()
     626                 :            :                  */
     627         [ #  # ]:          0 :                 if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
     628                 :            :                         return retval;
     629                 :            : 
     630                 :            :         /*
     631                 :            :          * Here we clear m_flags because after allocating an new extent,
     632                 :            :          * it will be set again.
     633                 :            :          */
     634                 :      59452 :         map->m_flags &= ~EXT4_MAP_FLAGS;
     635                 :            : 
     636                 :            :         /*
     637                 :            :          * New blocks allocate and/or writing to unwritten extent
     638                 :            :          * will possibly result in updating i_data, so we take
     639                 :            :          * the write lock of i_data_sem, and call get_block()
     640                 :            :          * with create == 1 flag.
     641                 :            :          */
     642                 :      59452 :         down_write(&EXT4_I(inode)->i_data_sem);
     643                 :            : 
     644                 :            :         /*
     645                 :            :          * We need to check for EXT4 here because migrate
     646                 :            :          * could have changed the inode type in between
     647                 :            :          */
     648         [ +  - ]:      59452 :         if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
     649                 :      59452 :                 retval = ext4_ext_map_blocks(handle, inode, map, flags);
     650                 :            :         } else {
     651                 :          0 :                 retval = ext4_ind_map_blocks(handle, inode, map, flags);
     652                 :            : 
     653   [ #  #  #  # ]:          0 :                 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
     654                 :            :                         /*
     655                 :            :                          * We allocated new blocks which will result in
     656                 :            :                          * i_data's format changing.  Force the migrate
     657                 :            :                          * to fail by clearing migrate flags
     658                 :            :                          */
     659                 :            :                         ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
     660                 :            :                 }
     661                 :            : 
     662                 :            :                 /*
     663                 :            :                  * Update reserved blocks/metadata blocks after successful
     664                 :            :                  * block allocation which had been deferred till now. We don't
     665                 :            :                  * support fallocate for non extent files. So we can update
     666                 :            :                  * reserve space here.
     667                 :            :                  */
     668   [ #  #  #  # ]:          0 :                 if ((retval > 0) &&
     669                 :          0 :                         (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
     670                 :          0 :                         ext4_da_update_reserve_space(inode, retval, 1);
     671                 :            :         }
     672                 :            : 
     673         [ +  - ]:      59452 :         if (retval > 0) {
     674                 :            :                 unsigned int status;
     675                 :            : 
     676         [ -  + ]:      59452 :                 if (unlikely(retval != map->m_len)) {
     677                 :          0 :                         ext4_warning(inode->i_sb,
     678                 :            :                                      "ES len assertion failed for inode "
     679                 :            :                                      "%lu: retval %d != map->m_len %d",
     680                 :            :                                      inode->i_ino, retval, map->m_len);
     681                 :          0 :                         WARN_ON(1);
     682                 :            :                 }
     683                 :            : 
     684                 :            :                 /*
     685                 :            :                  * We have to zeroout blocks before inserting them into extent
     686                 :            :                  * status tree. Otherwise someone could look them up there and
     687                 :            :                  * use them before they are really zeroed. We also have to
     688                 :            :                  * unmap metadata before zeroing as otherwise writeback can
     689                 :            :                  * overwrite zeros with stale data from block device.
     690                 :            :                  */
     691         [ -  + ]:      59452 :                 if (flags & EXT4_GET_BLOCKS_ZERO &&
     692         [ #  # ]:          0 :                     map->m_flags & EXT4_MAP_MAPPED &&
     693                 :            :                     map->m_flags & EXT4_MAP_NEW) {
     694                 :          0 :                         ret = ext4_issue_zeroout(inode, map->m_lblk,
     695                 :            :                                                  map->m_pblk, map->m_len);
     696         [ #  # ]:          0 :                         if (ret) {
     697                 :            :                                 retval = ret;
     698                 :            :                                 goto out_sem;
     699                 :            :                         }
     700                 :            :                 }
     701                 :            : 
     702                 :            :                 /*
     703                 :            :                  * If the extent has been zeroed out, we don't need to update
     704                 :            :                  * extent status tree.
     705                 :            :                  */
     706   [ -  +  #  # ]:      59452 :                 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
     707                 :          0 :                     ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
     708         [ #  # ]:          0 :                         if (ext4_es_is_written(&es))
     709                 :            :                                 goto out_sem;
     710                 :            :                 }
     711         [ +  + ]:      59452 :                 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
     712                 :            :                                 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
     713   [ +  +  +  + ]:     105268 :                 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
     714         [ -  + ]:      45847 :                     !(status & EXTENT_STATUS_WRITTEN) &&
     715                 :         31 :                     ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
     716                 :         31 :                                        map->m_lblk + map->m_len - 1))
     717                 :          0 :                         status |= EXTENT_STATUS_DELAYED;
     718                 :      59452 :                 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
     719                 :            :                                             map->m_pblk, status);
     720         [ -  + ]:      59452 :                 if (ret < 0) {
     721                 :            :                         retval = ret;
     722                 :          0 :                         goto out_sem;
     723                 :            :                 }
     724                 :            :         }
     725                 :            : 
     726                 :            : out_sem:
     727                 :      59452 :         up_write((&EXT4_I(inode)->i_data_sem));
     728   [ +  -  +  - ]:      59452 :         if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
     729                 :      59452 :                 ret = check_block_validity(inode, map);
     730         [ +  - ]:      59452 :                 if (ret != 0)
     731                 :            :                         return ret;
     732                 :            : 
     733                 :            :                 /*
     734                 :            :                  * Inodes with freshly allocated blocks where contents will be
     735                 :            :                  * visible after transaction commit must be on transaction's
     736                 :            :                  * ordered data list.
     737                 :            :                  */
     738         [ +  + ]:      59452 :                 if (map->m_flags & EXT4_MAP_NEW &&
     739         [ +  - ]:      59421 :                     !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
     740         [ +  - ]:     118842 :                     !(flags & EXT4_GET_BLOCKS_ZERO) &&
     741         [ +  + ]:      59421 :                     !ext4_is_quota_file(inode) &&
     742                 :            :                     ext4_should_order_data(inode)) {
     743                 :      13662 :                         loff_t start_byte =
     744                 :      13662 :                                 (loff_t)map->m_lblk << inode->i_blkbits;
     745                 :      13662 :                         loff_t length = (loff_t)map->m_len << inode->i_blkbits;
     746                 :            : 
     747         [ +  - ]:      13662 :                         if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
     748                 :            :                                 ret = ext4_jbd2_inode_add_wait(handle, inode,
     749                 :            :                                                 start_byte, length);
     750                 :            :                         else
     751                 :            :                                 ret = ext4_jbd2_inode_add_write(handle, inode,
     752                 :            :                                                 start_byte, length);
     753         [ +  - ]:      13662 :                         if (ret)
     754                 :            :                                 return ret;
     755                 :            :                 }
     756                 :            :         }
     757                 :      59452 :         return retval;
     758                 :            : }
     759                 :            : 
     760                 :            : /*
     761                 :            :  * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
     762                 :            :  * we have to be careful as someone else may be manipulating b_state as well.
     763                 :            :  */
     764                 :    5246535 : static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
     765                 :            : {
     766                 :            :         unsigned long old_state;
     767                 :            :         unsigned long new_state;
     768                 :            : 
     769                 :    5246535 :         flags &= EXT4_MAP_FLAGS;
     770                 :            : 
     771                 :            :         /* Dummy buffer_head? Set non-atomically. */
     772         [ +  + ]:    5246535 :         if (!bh->b_page) {
     773                 :    5243829 :                 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
     774                 :   10490364 :                 return;
     775                 :            :         }
     776                 :            :         /*
     777                 :            :          * Someone else may be modifying b_state. Be careful! This is ugly but
     778                 :            :          * once we get rid of using bh as a container for mapping information
     779                 :            :          * to pass to / from get_block functions, this can go away.
     780                 :            :          */
     781                 :            :         do {
     782                 :            :                 old_state = READ_ONCE(bh->b_state);
     783                 :       2706 :                 new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
     784         [ -  + ]:       2706 :         } while (unlikely(
     785                 :            :                  cmpxchg(&bh->b_state, old_state, new_state) != old_state));
     786                 :            : }
     787                 :            : 
     788                 :    5243829 : static int _ext4_get_block(struct inode *inode, sector_t iblock,
     789                 :            :                            struct buffer_head *bh, int flags)
     790                 :            : {
     791                 :            :         struct ext4_map_blocks map;
     792                 :            :         int ret = 0;
     793                 :            : 
     794         [ +  - ]:    5243829 :         if (ext4_has_inline_data(inode))
     795                 :            :                 return -ERANGE;
     796                 :            : 
     797                 :    5243829 :         map.m_lblk = iblock;
     798                 :    5243829 :         map.m_len = bh->b_size >> inode->i_blkbits;
     799                 :            : 
     800                 :    5243829 :         ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
     801                 :            :                               flags);
     802         [ +  - ]:    5243829 :         if (ret > 0) {
     803                 :    5243829 :                 map_bh(bh, inode->i_sb, map.m_pblk);
     804                 :    5243829 :                 ext4_update_bh_state(bh, map.m_flags);
     805                 :    5243829 :                 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
     806                 :            :                 ret = 0;
     807         [ #  # ]:          0 :         } else if (ret == 0) {
     808                 :            :                 /* hole case, need to fill in bh->b_size */
     809                 :          0 :                 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
     810                 :            :         }
     811                 :    5243829 :         return ret;
     812                 :            : }
     813                 :            : 
     814                 :    5243829 : int ext4_get_block(struct inode *inode, sector_t iblock,
     815                 :            :                    struct buffer_head *bh, int create)
     816                 :            : {
     817                 :    5243829 :         return _ext4_get_block(inode, iblock, bh,
     818                 :            :                                create ? EXT4_GET_BLOCKS_CREATE : 0);
     819                 :            : }
     820                 :            : 
     821                 :            : /*
     822                 :            :  * Get block function used when preparing for buffered write if we require
     823                 :            :  * creating an unwritten extent if blocks haven't been allocated.  The extent
     824                 :            :  * will be converted to written after the IO is complete.
     825                 :            :  */
     826                 :          0 : int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
     827                 :            :                              struct buffer_head *bh_result, int create)
     828                 :            : {
     829                 :            :         ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
     830                 :            :                    inode->i_ino, create);
     831                 :          0 :         return _ext4_get_block(inode, iblock, bh_result,
     832                 :            :                                EXT4_GET_BLOCKS_IO_CREATE_EXT);
     833                 :            : }
     834                 :            : 
     835                 :            : /* Maximum number of blocks we map for direct IO at once. */
     836                 :            : #define DIO_MAX_BLOCKS 4096
     837                 :            : 
     838                 :            : /*
     839                 :            :  * Get blocks function for the cases that need to start a transaction -
     840                 :            :  * generally difference cases of direct IO and DAX IO. It also handles retries
     841                 :            :  * in case of ENOSPC.
     842                 :            :  */
     843                 :          0 : static int ext4_get_block_trans(struct inode *inode, sector_t iblock,
     844                 :            :                                 struct buffer_head *bh_result, int flags)
     845                 :            : {
     846                 :            :         int dio_credits;
     847                 :            :         handle_t *handle;
     848                 :          0 :         int retries = 0;
     849                 :            :         int ret;
     850                 :            : 
     851                 :            :         /* Trim mapping request to maximum we can map at once for DIO */
     852         [ #  # ]:          0 :         if (bh_result->b_size >> inode->i_blkbits > DIO_MAX_BLOCKS)
     853                 :          0 :                 bh_result->b_size = DIO_MAX_BLOCKS << inode->i_blkbits;
     854                 :          0 :         dio_credits = ext4_chunk_trans_blocks(inode,
     855                 :          0 :                                       bh_result->b_size >> inode->i_blkbits);
     856                 :            : retry:
     857                 :            :         handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
     858         [ #  # ]:          0 :         if (IS_ERR(handle))
     859                 :          0 :                 return PTR_ERR(handle);
     860                 :            : 
     861                 :          0 :         ret = _ext4_get_block(inode, iblock, bh_result, flags);
     862                 :          0 :         ext4_journal_stop(handle);
     863                 :            : 
     864   [ #  #  #  # ]:          0 :         if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
     865                 :            :                 goto retry;
     866                 :          0 :         return ret;
     867                 :            : }
     868                 :            : 
     869                 :            : /* Get block function for DIO reads and writes to inodes without extents */
     870                 :          0 : int ext4_dio_get_block(struct inode *inode, sector_t iblock,
     871                 :            :                        struct buffer_head *bh, int create)
     872                 :            : {
     873                 :            :         /* We don't expect handle for direct IO */
     874   [ #  #  #  # ]:          0 :         WARN_ON_ONCE(ext4_journal_current_handle());
     875                 :            : 
     876         [ #  # ]:          0 :         if (!create)
     877                 :          0 :                 return _ext4_get_block(inode, iblock, bh, 0);
     878                 :          0 :         return ext4_get_block_trans(inode, iblock, bh, EXT4_GET_BLOCKS_CREATE);
     879                 :            : }
     880                 :            : 
     881                 :            : /*
     882                 :            :  * Get block function for AIO DIO writes when we create unwritten extent if
     883                 :            :  * blocks are not allocated yet. The extent will be converted to written
     884                 :            :  * after IO is complete.
     885                 :            :  */
     886                 :          0 : static int ext4_dio_get_block_unwritten_async(struct inode *inode,
     887                 :            :                 sector_t iblock, struct buffer_head *bh_result, int create)
     888                 :            : {
     889                 :            :         int ret;
     890                 :            : 
     891                 :            :         /* We don't expect handle for direct IO */
     892   [ #  #  #  # ]:          0 :         WARN_ON_ONCE(ext4_journal_current_handle());
     893                 :            : 
     894                 :          0 :         ret = ext4_get_block_trans(inode, iblock, bh_result,
     895                 :            :                                    EXT4_GET_BLOCKS_IO_CREATE_EXT);
     896                 :            : 
     897                 :            :         /*
     898                 :            :          * When doing DIO using unwritten extents, we need io_end to convert
     899                 :            :          * unwritten extents to written on IO completion. We allocate io_end
     900                 :            :          * once we spot unwritten extent and store it in b_private. Generic
     901                 :            :          * DIO code keeps b_private set and furthermore passes the value to
     902                 :            :          * our completion callback in 'private' argument.
     903                 :            :          */
     904   [ #  #  #  # ]:          0 :         if (!ret && buffer_unwritten(bh_result)) {
     905         [ #  # ]:          0 :                 if (!bh_result->b_private) {
     906                 :            :                         ext4_io_end_t *io_end;
     907                 :            : 
     908                 :          0 :                         io_end = ext4_init_io_end(inode, GFP_KERNEL);
     909         [ #  # ]:          0 :                         if (!io_end)
     910                 :            :                                 return -ENOMEM;
     911                 :          0 :                         bh_result->b_private = io_end;
     912                 :          0 :                         ext4_set_io_unwritten_flag(inode, io_end);
     913                 :            :                 }
     914                 :            :                 set_buffer_defer_completion(bh_result);
     915                 :            :         }
     916                 :            : 
     917                 :          0 :         return ret;
     918                 :            : }
     919                 :            : 
     920                 :            : /*
     921                 :            :  * Get block function for non-AIO DIO writes when we create unwritten extent if
     922                 :            :  * blocks are not allocated yet. The extent will be converted to written
     923                 :            :  * after IO is complete by ext4_direct_IO_write().
     924                 :            :  */
     925                 :          0 : static int ext4_dio_get_block_unwritten_sync(struct inode *inode,
     926                 :            :                 sector_t iblock, struct buffer_head *bh_result, int create)
     927                 :            : {
     928                 :            :         int ret;
     929                 :            : 
     930                 :            :         /* We don't expect handle for direct IO */
     931   [ #  #  #  # ]:          0 :         WARN_ON_ONCE(ext4_journal_current_handle());
     932                 :            : 
     933                 :          0 :         ret = ext4_get_block_trans(inode, iblock, bh_result,
     934                 :            :                                    EXT4_GET_BLOCKS_IO_CREATE_EXT);
     935                 :            : 
     936                 :            :         /*
     937                 :            :          * Mark inode as having pending DIO writes to unwritten extents.
     938                 :            :          * ext4_direct_IO_write() checks this flag and converts extents to
     939                 :            :          * written.
     940                 :            :          */
     941   [ #  #  #  # ]:          0 :         if (!ret && buffer_unwritten(bh_result))
     942                 :            :                 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
     943                 :            : 
     944                 :          0 :         return ret;
     945                 :            : }
     946                 :            : 
     947                 :          0 : static int ext4_dio_get_block_overwrite(struct inode *inode, sector_t iblock,
     948                 :            :                    struct buffer_head *bh_result, int create)
     949                 :            : {
     950                 :            :         int ret;
     951                 :            : 
     952                 :            :         ext4_debug("ext4_dio_get_block_overwrite: inode %lu, create flag %d\n",
     953                 :            :                    inode->i_ino, create);
     954                 :            :         /* We don't expect handle for direct IO */
     955   [ #  #  #  # ]:          0 :         WARN_ON_ONCE(ext4_journal_current_handle());
     956                 :            : 
     957                 :          0 :         ret = _ext4_get_block(inode, iblock, bh_result, 0);
     958                 :            :         /*
     959                 :            :          * Blocks should have been preallocated! ext4_file_write_iter() checks
     960                 :            :          * that.
     961                 :            :          */
     962   [ #  #  #  #  :          0 :         WARN_ON_ONCE(!buffer_mapped(bh_result) || buffer_unwritten(bh_result));
             #  #  #  # ]
     963                 :            : 
     964                 :          0 :         return ret;
     965                 :            : }
     966                 :            : 
     967                 :            : 
     968                 :            : /*
     969                 :            :  * `handle' can be NULL if create is zero
     970                 :            :  */
     971                 :    2524000 : struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
     972                 :            :                                 ext4_lblk_t block, int map_flags)
     973                 :            : {
     974                 :            :         struct ext4_map_blocks map;
     975                 :            :         struct buffer_head *bh;
     976                 :    2524000 :         int create = map_flags & EXT4_GET_BLOCKS_CREATE;
     977                 :            :         int err;
     978                 :            : 
     979         [ -  + ]:    2524000 :         J_ASSERT(handle != NULL || create == 0);
     980                 :            : 
     981                 :    2524000 :         map.m_lblk = block;
     982                 :    2524000 :         map.m_len = 1;
     983                 :    2524000 :         err = ext4_map_blocks(handle, inode, &map, map_flags);
     984                 :            : 
     985         [ -  + ]:    2523882 :         if (err == 0)
     986         [ #  # ]:          0 :                 return create ? ERR_PTR(-ENOSPC) : NULL;
     987         [ -  + ]:    2523882 :         if (err < 0)
     988                 :          0 :                 return ERR_PTR(err);
     989                 :            : 
     990                 :    2523882 :         bh = sb_getblk(inode->i_sb, map.m_pblk);
     991         [ +  + ]:    2525842 :         if (unlikely(!bh))
     992                 :            :                 return ERR_PTR(-ENOMEM);
     993         [ +  + ]:    2524761 :         if (map.m_flags & EXT4_MAP_NEW) {
     994         [ -  + ]:      45759 :                 J_ASSERT(create != 0);
     995         [ -  + ]:      45759 :                 J_ASSERT(handle != NULL);
     996                 :            : 
     997                 :            :                 /*
     998                 :            :                  * Now that we do not always journal data, we should
     999                 :            :                  * keep in mind whether this should always journal the
    1000                 :            :                  * new buffer as metadata.  For now, regular file
    1001                 :            :                  * writes use ext4_get_block instead, so it's not a
    1002                 :            :                  * problem.
    1003                 :            :                  */
    1004                 :      45759 :                 lock_buffer(bh);
    1005                 :            :                 BUFFER_TRACE(bh, "call get_create_access");
    1006                 :      45759 :                 err = ext4_journal_get_create_access(handle, bh);
    1007         [ -  + ]:      45759 :                 if (unlikely(err)) {
    1008                 :          0 :                         unlock_buffer(bh);
    1009                 :          0 :                         goto errout;
    1010                 :            :                 }
    1011         [ +  + ]:      45759 :                 if (!buffer_uptodate(bh)) {
    1012                 :      44509 :                         memset(bh->b_data, 0, inode->i_sb->s_blocksize);
    1013                 :            :                         set_buffer_uptodate(bh);
    1014                 :            :                 }
    1015                 :      45759 :                 unlock_buffer(bh);
    1016                 :            :                 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
    1017                 :      45759 :                 err = ext4_handle_dirty_metadata(handle, inode, bh);
    1018         [ +  + ]:      45759 :                 if (unlikely(err))
    1019                 :            :                         goto errout;
    1020                 :            :         } else
    1021                 :            :                 BUFFER_TRACE(bh, "not a new buffer");
    1022                 :    2525832 :         return bh;
    1023                 :            : errout:
    1024                 :            :         brelse(bh);
    1025                 :          0 :         return ERR_PTR(err);
    1026                 :            : }
    1027                 :            : 
    1028                 :    1420078 : struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
    1029                 :            :                                ext4_lblk_t block, int map_flags)
    1030                 :            : {
    1031                 :            :         struct buffer_head *bh;
    1032                 :            : 
    1033                 :    1420078 :         bh = ext4_getblk(handle, inode, block, map_flags);
    1034         [ +  + ]:    1420385 :         if (IS_ERR(bh))
    1035                 :            :                 return bh;
    1036   [ +  +  +  + ]:    1419891 :         if (!bh || ext4_buffer_uptodate(bh))
    1037                 :    1248491 :                 return bh;
    1038                 :     171960 :         ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &bh);
    1039                 :     171990 :         wait_on_buffer(bh);
    1040         [ +  - ]:     343973 :         if (buffer_uptodate(bh))
    1041                 :     171989 :                 return bh;
    1042                 :          0 :         put_bh(bh);
    1043                 :          0 :         return ERR_PTR(-EIO);
    1044                 :            : }
    1045                 :            : 
    1046                 :            : /* Read a contiguous batch of blocks. */
    1047                 :    1105303 : int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
    1048                 :            :                      bool wait, struct buffer_head **bhs)
    1049                 :            : {
    1050                 :            :         int i, err;
    1051                 :            : 
    1052         [ +  + ]:    2210517 :         for (i = 0; i < bh_count; i++) {
    1053                 :    1105210 :                 bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */);
    1054         [ -  + ]:    1105214 :                 if (IS_ERR(bhs[i])) {
    1055                 :          0 :                         err = PTR_ERR(bhs[i]);
    1056                 :          0 :                         bh_count = i;
    1057                 :          0 :                         goto out_brelse;
    1058                 :            :                 }
    1059                 :            :         }
    1060                 :            : 
    1061         [ +  + ]:    1105142 :         for (i = 0; i < bh_count; i++)
    1062                 :            :                 /* Note that NULL bhs[i] is valid because of holes. */
    1063   [ +  +  +  + ]:    1105220 :                 if (bhs[i] && !ext4_buffer_uptodate(bhs[i]))
    1064                 :      72358 :                         ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1,
    1065                 :            :                                     &bhs[i]);
    1066                 :            : 
    1067         [ -  + ]:    1105229 :         if (!wait)
    1068                 :            :                 return 0;
    1069                 :            : 
    1070         [ #  # ]:          0 :         for (i = 0; i < bh_count; i++)
    1071         [ #  # ]:          0 :                 if (bhs[i])
    1072                 :          0 :                         wait_on_buffer(bhs[i]);
    1073                 :            : 
    1074         [ #  # ]:          0 :         for (i = 0; i < bh_count; i++) {
    1075   [ #  #  #  # ]:          0 :                 if (bhs[i] && !buffer_uptodate(bhs[i])) {
    1076                 :            :                         err = -EIO;
    1077                 :            :                         goto out_brelse;
    1078                 :            :                 }
    1079                 :            :         }
    1080                 :            :         return 0;
    1081                 :            : 
    1082                 :            : out_brelse:
    1083         [ #  # ]:          0 :         for (i = 0; i < bh_count; i++) {
    1084                 :          0 :                 brelse(bhs[i]);
    1085                 :          0 :                 bhs[i] = NULL;
    1086                 :            :         }
    1087                 :            :         return err;
    1088                 :            : }
    1089                 :            : 
    1090                 :          0 : int ext4_walk_page_buffers(handle_t *handle,
    1091                 :            :                            struct buffer_head *head,
    1092                 :            :                            unsigned from,
    1093                 :            :                            unsigned to,
    1094                 :            :                            int *partial,
    1095                 :            :                            int (*fn)(handle_t *handle,
    1096                 :            :                                      struct buffer_head *bh))
    1097                 :            : {
    1098                 :            :         struct buffer_head *bh;
    1099                 :            :         unsigned block_start, block_end;
    1100                 :          0 :         unsigned blocksize = head->b_size;
    1101                 :            :         int err, ret = 0;
    1102                 :            :         struct buffer_head *next;
    1103                 :            : 
    1104         [ #  # ]:          0 :         for (bh = head, block_start = 0;
    1105         [ #  # ]:          0 :              ret == 0 && (bh != head || !block_start);
    1106                 :            :              block_start = block_end, bh = next) {
    1107                 :          0 :                 next = bh->b_this_page;
    1108                 :          0 :                 block_end = block_start + blocksize;
    1109         [ #  # ]:          0 :                 if (block_end <= from || block_start >= to) {
    1110   [ #  #  #  # ]:          0 :                         if (partial && !buffer_uptodate(bh))
    1111                 :          0 :                                 *partial = 1;
    1112                 :          0 :                         continue;
    1113                 :            :                 }
    1114                 :          0 :                 err = (*fn)(handle, bh);
    1115         [ #  # ]:          0 :                 if (!ret)
    1116                 :            :                         ret = err;
    1117                 :            :         }
    1118                 :          0 :         return ret;
    1119                 :            : }
    1120                 :            : 
    1121                 :            : /*
    1122                 :            :  * To preserve ordering, it is essential that the hole instantiation and
    1123                 :            :  * the data write be encapsulated in a single transaction.  We cannot
    1124                 :            :  * close off a transaction and start a new one between the ext4_get_block()
    1125                 :            :  * and the commit_write().  So doing the jbd2_journal_start at the start of
    1126                 :            :  * prepare_write() is the right place.
    1127                 :            :  *
    1128                 :            :  * Also, this function can nest inside ext4_writepage().  In that case, we
    1129                 :            :  * *know* that ext4_writepage() has generated enough buffer credits to do the
    1130                 :            :  * whole page.  So we won't block on the journal in that case, which is good,
    1131                 :            :  * because the caller may be PF_MEMALLOC.
    1132                 :            :  *
    1133                 :            :  * By accident, ext4 can be reentered when a transaction is open via
    1134                 :            :  * quota file writes.  If we were to commit the transaction while thus
    1135                 :            :  * reentered, there can be a deadlock - we would be holding a quota
    1136                 :            :  * lock, and the commit would never complete if another thread had a
    1137                 :            :  * transaction open and was blocking on the quota lock - a ranking
    1138                 :            :  * violation.
    1139                 :            :  *
    1140                 :            :  * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
    1141                 :            :  * will _not_ run commit under these circumstances because handle->h_ref
    1142                 :            :  * is elevated.  We'll still have enough credits for the tiny quotafile
    1143                 :            :  * write.
    1144                 :            :  */
    1145                 :          0 : int do_journal_get_write_access(handle_t *handle,
    1146                 :            :                                 struct buffer_head *bh)
    1147                 :            : {
    1148                 :            :         int dirty = buffer_dirty(bh);
    1149                 :            :         int ret;
    1150                 :            : 
    1151   [ #  #  #  # ]:          0 :         if (!buffer_mapped(bh) || buffer_freed(bh))
    1152                 :            :                 return 0;
    1153                 :            :         /*
    1154                 :            :          * __block_write_begin() could have dirtied some buffers. Clean
    1155                 :            :          * the dirty bit as jbd2_journal_get_write_access() could complain
    1156                 :            :          * otherwise about fs integrity issues. Setting of the dirty bit
    1157                 :            :          * by __block_write_begin() isn't a real problem here as we clear
    1158                 :            :          * the bit before releasing a page lock and thus writeback cannot
    1159                 :            :          * ever write the buffer.
    1160                 :            :          */
    1161         [ #  # ]:          0 :         if (dirty)
    1162                 :            :                 clear_buffer_dirty(bh);
    1163                 :            :         BUFFER_TRACE(bh, "get write access");
    1164                 :          0 :         ret = ext4_journal_get_write_access(handle, bh);
    1165         [ #  # ]:          0 :         if (!ret && dirty)
    1166                 :          0 :                 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
    1167                 :          0 :         return ret;
    1168                 :            : }
    1169                 :            : 
    1170                 :            : #ifdef CONFIG_FS_ENCRYPTION
    1171                 :     410836 : static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
    1172                 :            :                                   get_block_t *get_block)
    1173                 :            : {
    1174                 :     410836 :         unsigned from = pos & (PAGE_SIZE - 1);
    1175                 :     410836 :         unsigned to = from + len;
    1176                 :     410836 :         struct inode *inode = page->mapping->host;
    1177                 :            :         unsigned block_start, block_end;
    1178                 :            :         sector_t block;
    1179                 :            :         int err = 0;
    1180                 :     410836 :         unsigned blocksize = inode->i_sb->s_blocksize;
    1181                 :            :         unsigned bbits;
    1182                 :            :         struct buffer_head *bh, *head, *wait[2];
    1183                 :            :         int nr_wait = 0;
    1184                 :            :         int i;
    1185                 :            : 
    1186         [ -  + ]:     410845 :         BUG_ON(!PageLocked(page));
    1187                 :            :         BUG_ON(from > PAGE_SIZE);
    1188         [ -  + ]:     410845 :         BUG_ON(to > PAGE_SIZE);
    1189         [ -  + ]:     410845 :         BUG_ON(from > to);
    1190                 :            : 
    1191         [ +  + ]:     410803 :         if (!page_has_buffers(page))
    1192                 :     176584 :                 create_empty_buffers(page, blocksize, 0);
    1193         [ -  + ]:     410788 :         head = page_buffers(page);
    1194   [ +  +  -  +  :     821611 :         bbits = ilog2(blocksize);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1195                 :     410788 :         block = (sector_t)page->index << (PAGE_SHIFT - bbits);
    1196                 :            : 
    1197         [ +  + ]:    1232399 :         for (bh = head, block_start = 0; bh != head || !block_start;
    1198                 :     410823 :             block++, block_start = block_end, bh = bh->b_this_page) {
    1199                 :     410799 :                 block_end = block_start + blocksize;
    1200         [ +  + ]:     410799 :                 if (block_end <= from || block_start >= to) {
    1201   [ -  +  -  - ]:          8 :                         if (PageUptodate(page)) {
    1202         [ #  # ]:          0 :                                 if (!buffer_uptodate(bh))
    1203                 :          0 :                                         set_buffer_uptodate(bh);
    1204                 :            :                         }
    1205                 :          8 :                         continue;
    1206                 :            :                 }
    1207         [ -  + ]:     410851 :                 if (buffer_new(bh))
    1208                 :          0 :                         clear_buffer_new(bh);
    1209         [ +  + ]:     821681 :                 if (!buffer_mapped(bh)) {
    1210         [ -  + ]:     176584 :                         WARN_ON(bh->b_size != blocksize);
    1211                 :     176584 :                         err = get_block(inode, block, bh, 1);
    1212         [ +  - ]:     176584 :                         if (err)
    1213                 :            :                                 break;
    1214         [ +  + ]:     353168 :                         if (buffer_new(bh)) {
    1215         [ +  + ]:     173904 :                                 if (PageUptodate(page)) {
    1216                 :         26 :                                         clear_buffer_new(bh);
    1217                 :         26 :                                         set_buffer_uptodate(bh);
    1218                 :         26 :                                         mark_buffer_dirty(bh);
    1219                 :         26 :                                         continue;
    1220                 :            :                                 }
    1221         [ +  + ]:     173878 :                                 if (block_end > to || block_start < from)
    1222                 :     107562 :                                         zero_user_segments(page, to, block_end,
    1223                 :            :                                                            block_start, from);
    1224                 :     173878 :                                 continue;
    1225                 :            :                         }
    1226                 :            :                 }
    1227         [ +  + ]:     236884 :                 if (PageUptodate(page)) {
    1228         [ -  + ]:     469704 :                         if (!buffer_uptodate(bh))
    1229                 :          0 :                                 set_buffer_uptodate(bh);
    1230                 :     234870 :                         continue;
    1231                 :            :                 }
    1232   [ +  -  +  -  :       8236 :                 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
                   +  - ]
    1233         [ +  - ]:       4118 :                     !buffer_unwritten(bh) &&
    1234                 :       2059 :                     (block_start < from || block_end > to)) {
    1235                 :       2059 :                         ll_rw_block(REQ_OP_READ, 0, 1, &bh);
    1236                 :       2059 :                         wait[nr_wait++] = bh;
    1237                 :            :                 }
    1238                 :            :         }
    1239                 :            :         /*
    1240                 :            :          * If we issued read requests, let them complete.
    1241                 :            :          */
    1242         [ +  + ]:       2059 :         for (i = 0; i < nr_wait; i++) {
    1243                 :       2059 :                 wait_on_buffer(wait[i]);
    1244         [ -  + ]:       2059 :                 if (!buffer_uptodate(wait[i]))
    1245                 :            :                         err = -EIO;
    1246                 :            :         }
    1247         [ -  + ]:     410812 :         if (unlikely(err)) {
    1248                 :          0 :                 page_zero_new_buffers(page, from, to);
    1249   [ -  +  #  # ]:     410812 :         } else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
    1250         [ #  # ]:          0 :                 for (i = 0; i < nr_wait; i++) {
    1251                 :            :                         int err2;
    1252                 :            : 
    1253                 :          0 :                         err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize,
    1254                 :          0 :                                                                 bh_offset(wait[i]));
    1255         [ #  # ]:          0 :                         if (err2) {
    1256                 :            :                                 clear_buffer_uptodate(wait[i]);
    1257                 :            :                                 err = err2;
    1258                 :            :                         }
    1259                 :            :                 }
    1260                 :            :         }
    1261                 :            : 
    1262                 :     410812 :         return err;
    1263                 :            : }
    1264                 :            : #endif
    1265                 :            : 
    1266                 :          0 : static int ext4_write_begin(struct file *file, struct address_space *mapping,
    1267                 :            :                             loff_t pos, unsigned len, unsigned flags,
    1268                 :            :                             struct page **pagep, void **fsdata)
    1269                 :            : {
    1270                 :          0 :         struct inode *inode = mapping->host;
    1271                 :            :         int ret, needed_blocks;
    1272                 :            :         handle_t *handle;
    1273                 :          0 :         int retries = 0;
    1274                 :            :         struct page *page;
    1275                 :            :         pgoff_t index;
    1276                 :            :         unsigned from, to;
    1277                 :            : 
    1278         [ #  # ]:          0 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
    1279                 :            :                 return -EIO;
    1280                 :            : 
    1281                 :          0 :         trace_ext4_write_begin(inode, pos, len, flags);
    1282                 :            :         /*
    1283                 :            :          * Reserve one block more for addition to orphan list in case
    1284                 :            :          * we allocate blocks but write fails for some reason
    1285                 :            :          */
    1286                 :          0 :         needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
    1287                 :          0 :         index = pos >> PAGE_SHIFT;
    1288                 :          0 :         from = pos & (PAGE_SIZE - 1);
    1289                 :          0 :         to = from + len;
    1290                 :            : 
    1291         [ #  # ]:          0 :         if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
    1292                 :          0 :                 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
    1293                 :            :                                                     flags, pagep);
    1294         [ #  # ]:          0 :                 if (ret < 0)
    1295                 :            :                         return ret;
    1296         [ #  # ]:          0 :                 if (ret == 1)
    1297                 :            :                         return 0;
    1298                 :            :         }
    1299                 :            : 
    1300                 :            :         /*
    1301                 :            :          * grab_cache_page_write_begin() can take a long time if the
    1302                 :            :          * system is thrashing due to memory pressure, or if the page
    1303                 :            :          * is being written back.  So grab it first before we start
    1304                 :            :          * the transaction handle.  This also allows us to allocate
    1305                 :            :          * the page (if needed) without using GFP_NOFS.
    1306                 :            :          */
    1307                 :            : retry_grab:
    1308                 :          0 :         page = grab_cache_page_write_begin(mapping, index, flags);
    1309         [ #  # ]:          0 :         if (!page)
    1310                 :            :                 return -ENOMEM;
    1311                 :          0 :         unlock_page(page);
    1312                 :            : 
    1313                 :            : retry_journal:
    1314                 :            :         handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
    1315         [ #  # ]:          0 :         if (IS_ERR(handle)) {
    1316                 :          0 :                 put_page(page);
    1317                 :          0 :                 return PTR_ERR(handle);
    1318                 :            :         }
    1319                 :            : 
    1320                 :          0 :         lock_page(page);
    1321         [ #  # ]:          0 :         if (page->mapping != mapping) {
    1322                 :            :                 /* The page got truncated from under us */
    1323                 :          0 :                 unlock_page(page);
    1324                 :          0 :                 put_page(page);
    1325                 :          0 :                 ext4_journal_stop(handle);
    1326                 :          0 :                 goto retry_grab;
    1327                 :            :         }
    1328                 :            :         /* In case writeback began while the page was unlocked */
    1329                 :          0 :         wait_for_stable_page(page);
    1330                 :            : 
    1331                 :            : #ifdef CONFIG_FS_ENCRYPTION
    1332         [ #  # ]:          0 :         if (ext4_should_dioread_nolock(inode))
    1333                 :          0 :                 ret = ext4_block_write_begin(page, pos, len,
    1334                 :            :                                              ext4_get_block_unwritten);
    1335                 :            :         else
    1336                 :          0 :                 ret = ext4_block_write_begin(page, pos, len,
    1337                 :            :                                              ext4_get_block);
    1338                 :            : #else
    1339                 :            :         if (ext4_should_dioread_nolock(inode))
    1340                 :            :                 ret = __block_write_begin(page, pos, len,
    1341                 :            :                                           ext4_get_block_unwritten);
    1342                 :            :         else
    1343                 :            :                 ret = __block_write_begin(page, pos, len, ext4_get_block);
    1344                 :            : #endif
    1345   [ #  #  #  # ]:          0 :         if (!ret && ext4_should_journal_data(inode)) {
    1346         [ #  # ]:          0 :                 ret = ext4_walk_page_buffers(handle, page_buffers(page),
    1347                 :            :                                              from, to, NULL,
    1348                 :            :                                              do_journal_get_write_access);
    1349                 :            :         }
    1350                 :            : 
    1351         [ #  # ]:          0 :         if (ret) {
    1352         [ #  # ]:          0 :                 bool extended = (pos + len > inode->i_size) &&
    1353                 :            :                                 !ext4_verity_in_progress(inode);
    1354                 :            : 
    1355                 :          0 :                 unlock_page(page);
    1356                 :            :                 /*
    1357                 :            :                  * __block_write_begin may have instantiated a few blocks
    1358                 :            :                  * outside i_size.  Trim these off again. Don't need
    1359                 :            :                  * i_size_read because we hold i_mutex.
    1360                 :            :                  *
    1361                 :            :                  * Add inode to orphan list in case we crash before
    1362                 :            :                  * truncate finishes
    1363                 :            :                  */
    1364   [ #  #  #  # ]:          0 :                 if (extended && ext4_can_truncate(inode))
    1365                 :          0 :                         ext4_orphan_add(handle, inode);
    1366                 :            : 
    1367                 :          0 :                 ext4_journal_stop(handle);
    1368         [ #  # ]:          0 :                 if (extended) {
    1369                 :          0 :                         ext4_truncate_failed_write(inode);
    1370                 :            :                         /*
    1371                 :            :                          * If truncate failed early the inode might
    1372                 :            :                          * still be on the orphan list; we need to
    1373                 :            :                          * make sure the inode is removed from the
    1374                 :            :                          * orphan list in that case.
    1375                 :            :                          */
    1376         [ #  # ]:          0 :                         if (inode->i_nlink)
    1377                 :          0 :                                 ext4_orphan_del(NULL, inode);
    1378                 :            :                 }
    1379                 :            : 
    1380   [ #  #  #  # ]:          0 :                 if (ret == -ENOSPC &&
    1381                 :          0 :                     ext4_should_retry_alloc(inode->i_sb, &retries))
    1382                 :            :                         goto retry_journal;
    1383                 :          0 :                 put_page(page);
    1384                 :          0 :                 return ret;
    1385                 :            :         }
    1386                 :          0 :         *pagep = page;
    1387                 :          0 :         return ret;
    1388                 :            : }
    1389                 :            : 
    1390                 :            : /* For write_end() in data=journal mode */
    1391                 :          0 : static int write_end_fn(handle_t *handle, struct buffer_head *bh)
    1392                 :            : {
    1393                 :            :         int ret;
    1394   [ #  #  #  # ]:          0 :         if (!buffer_mapped(bh) || buffer_freed(bh))
    1395                 :            :                 return 0;
    1396                 :            :         set_buffer_uptodate(bh);
    1397                 :          0 :         ret = ext4_handle_dirty_metadata(handle, NULL, bh);
    1398                 :            :         clear_buffer_meta(bh);
    1399                 :            :         clear_buffer_prio(bh);
    1400                 :          0 :         return ret;
    1401                 :            : }
    1402                 :            : 
    1403                 :            : /*
    1404                 :            :  * We need to pick up the new inode size which generic_commit_write gave us
    1405                 :            :  * `file' can be NULL - eg, when called from page_symlink().
    1406                 :            :  *
    1407                 :            :  * ext4 never places buffers on inode->i_mapping->private_list.  metadata
    1408                 :            :  * buffers are managed internally.
    1409                 :            :  */
    1410                 :          0 : static int ext4_write_end(struct file *file,
    1411                 :            :                           struct address_space *mapping,
    1412                 :            :                           loff_t pos, unsigned len, unsigned copied,
    1413                 :            :                           struct page *page, void *fsdata)
    1414                 :            : {
    1415                 :            :         handle_t *handle = ext4_journal_current_handle();
    1416                 :          0 :         struct inode *inode = mapping->host;
    1417                 :          0 :         loff_t old_size = inode->i_size;
    1418                 :            :         int ret = 0, ret2;
    1419                 :            :         int i_size_changed = 0;
    1420                 :            :         int inline_data = ext4_has_inline_data(inode);
    1421                 :            :         bool verity = ext4_verity_in_progress(inode);
    1422                 :            : 
    1423                 :          0 :         trace_ext4_write_end(inode, pos, len, copied);
    1424         [ #  # ]:          0 :         if (inline_data) {
    1425                 :          0 :                 ret = ext4_write_inline_data_end(inode, pos, len,
    1426                 :            :                                                  copied, page);
    1427         [ #  # ]:          0 :                 if (ret < 0) {
    1428                 :          0 :                         unlock_page(page);
    1429                 :          0 :                         put_page(page);
    1430                 :          0 :                         goto errout;
    1431                 :            :                 }
    1432                 :          0 :                 copied = ret;
    1433                 :            :         } else
    1434                 :          0 :                 copied = block_write_end(file, mapping, pos,
    1435                 :            :                                          len, copied, page, fsdata);
    1436                 :            :         /*
    1437                 :            :          * it's important to update i_size while still holding page lock:
    1438                 :            :          * page writeout could otherwise come in and zero beyond i_size.
    1439                 :            :          *
    1440                 :            :          * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree
    1441                 :            :          * blocks are being written past EOF, so skip the i_size update.
    1442                 :            :          */
    1443                 :            :         if (!verity)
    1444                 :          0 :                 i_size_changed = ext4_update_inode_size(inode, pos + copied);
    1445                 :          0 :         unlock_page(page);
    1446                 :          0 :         put_page(page);
    1447                 :            : 
    1448         [ #  # ]:          0 :         if (old_size < pos && !verity)
    1449                 :          0 :                 pagecache_isize_extended(inode, old_size, pos);
    1450                 :            :         /*
    1451                 :            :          * Don't mark the inode dirty under page lock. First, it unnecessarily
    1452                 :            :          * makes the holding time of page lock longer. Second, it forces lock
    1453                 :            :          * ordering of page lock and transaction start for journaling
    1454                 :            :          * filesystems.
    1455                 :            :          */
    1456         [ #  # ]:          0 :         if (i_size_changed || inline_data)
    1457                 :          0 :                 ext4_mark_inode_dirty(handle, inode);
    1458                 :            : 
    1459   [ #  #  #  # ]:          0 :         if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
    1460                 :            :                 /* if we have allocated more blocks and copied
    1461                 :            :                  * less. We will have blocks allocated outside
    1462                 :            :                  * inode->i_size. So truncate them
    1463                 :            :                  */
    1464                 :          0 :                 ext4_orphan_add(handle, inode);
    1465                 :            : errout:
    1466                 :          0 :         ret2 = ext4_journal_stop(handle);
    1467         [ #  # ]:          0 :         if (!ret)
    1468                 :            :                 ret = ret2;
    1469                 :            : 
    1470         [ #  # ]:          0 :         if (pos + len > inode->i_size && !verity) {
    1471                 :          0 :                 ext4_truncate_failed_write(inode);
    1472                 :            :                 /*
    1473                 :            :                  * If truncate failed early the inode might still be
    1474                 :            :                  * on the orphan list; we need to make sure the inode
    1475                 :            :                  * is removed from the orphan list in that case.
    1476                 :            :                  */
    1477         [ #  # ]:          0 :                 if (inode->i_nlink)
    1478                 :          0 :                         ext4_orphan_del(NULL, inode);
    1479                 :            :         }
    1480                 :            : 
    1481         [ #  # ]:          0 :         return ret ? ret : copied;
    1482                 :            : }
    1483                 :            : 
    1484                 :            : /*
    1485                 :            :  * This is a private version of page_zero_new_buffers() which doesn't
    1486                 :            :  * set the buffer to be dirty, since in data=journalled mode we need
    1487                 :            :  * to call ext4_handle_dirty_metadata() instead.
    1488                 :            :  */
    1489                 :          0 : static void ext4_journalled_zero_new_buffers(handle_t *handle,
    1490                 :            :                                             struct page *page,
    1491                 :            :                                             unsigned from, unsigned to)
    1492                 :            : {
    1493                 :            :         unsigned int block_start = 0, block_end;
    1494                 :            :         struct buffer_head *head, *bh;
    1495                 :            : 
    1496         [ #  # ]:          0 :         bh = head = page_buffers(page);
    1497                 :            :         do {
    1498                 :          0 :                 block_end = block_start + bh->b_size;
    1499         [ #  # ]:          0 :                 if (buffer_new(bh)) {
    1500         [ #  # ]:          0 :                         if (block_end > from && block_start < to) {
    1501         [ #  # ]:          0 :                                 if (!PageUptodate(page)) {
    1502                 :            :                                         unsigned start, size;
    1503                 :            : 
    1504                 :          0 :                                         start = max(from, block_start);
    1505                 :          0 :                                         size = min(to, block_end) - start;
    1506                 :            : 
    1507                 :            :                                         zero_user(page, start, size);
    1508                 :          0 :                                         write_end_fn(handle, bh);
    1509                 :            :                                 }
    1510                 :            :                                 clear_buffer_new(bh);
    1511                 :            :                         }
    1512                 :            :                 }
    1513                 :            :                 block_start = block_end;
    1514                 :          0 :                 bh = bh->b_this_page;
    1515         [ #  # ]:          0 :         } while (bh != head);
    1516                 :          0 : }
    1517                 :            : 
    1518                 :          0 : static int ext4_journalled_write_end(struct file *file,
    1519                 :            :                                      struct address_space *mapping,
    1520                 :            :                                      loff_t pos, unsigned len, unsigned copied,
    1521                 :            :                                      struct page *page, void *fsdata)
    1522                 :            : {
    1523                 :            :         handle_t *handle = ext4_journal_current_handle();
    1524                 :          0 :         struct inode *inode = mapping->host;
    1525                 :          0 :         loff_t old_size = inode->i_size;
    1526                 :            :         int ret = 0, ret2;
    1527                 :          0 :         int partial = 0;
    1528                 :            :         unsigned from, to;
    1529                 :            :         int size_changed = 0;
    1530                 :            :         int inline_data = ext4_has_inline_data(inode);
    1531                 :            :         bool verity = ext4_verity_in_progress(inode);
    1532                 :            : 
    1533                 :          0 :         trace_ext4_journalled_write_end(inode, pos, len, copied);
    1534                 :          0 :         from = pos & (PAGE_SIZE - 1);
    1535                 :          0 :         to = from + len;
    1536                 :            : 
    1537         [ #  # ]:          0 :         BUG_ON(!ext4_handle_valid(handle));
    1538                 :            : 
    1539         [ #  # ]:          0 :         if (inline_data) {
    1540                 :          0 :                 ret = ext4_write_inline_data_end(inode, pos, len,
    1541                 :            :                                                  copied, page);
    1542         [ #  # ]:          0 :                 if (ret < 0) {
    1543                 :          0 :                         unlock_page(page);
    1544                 :          0 :                         put_page(page);
    1545                 :          0 :                         goto errout;
    1546                 :            :                 }
    1547                 :          0 :                 copied = ret;
    1548   [ #  #  #  # ]:          0 :         } else if (unlikely(copied < len) && !PageUptodate(page)) {
    1549                 :            :                 copied = 0;
    1550                 :          0 :                 ext4_journalled_zero_new_buffers(handle, page, from, to);
    1551                 :            :         } else {
    1552         [ #  # ]:          0 :                 if (unlikely(copied < len))
    1553                 :          0 :                         ext4_journalled_zero_new_buffers(handle, page,
    1554                 :            :                                                          from + copied, to);
    1555         [ #  # ]:          0 :                 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
    1556                 :            :                                              from + copied, &partial,
    1557                 :            :                                              write_end_fn);
    1558         [ #  # ]:          0 :                 if (!partial)
    1559                 :            :                         SetPageUptodate(page);
    1560                 :            :         }
    1561                 :            :         if (!verity)
    1562                 :          0 :                 size_changed = ext4_update_inode_size(inode, pos + copied);
    1563                 :            :         ext4_set_inode_state(inode, EXT4_STATE_JDATA);
    1564                 :          0 :         EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
    1565                 :          0 :         unlock_page(page);
    1566                 :          0 :         put_page(page);
    1567                 :            : 
    1568         [ #  # ]:          0 :         if (old_size < pos && !verity)
    1569                 :          0 :                 pagecache_isize_extended(inode, old_size, pos);
    1570                 :            : 
    1571         [ #  # ]:          0 :         if (size_changed || inline_data) {
    1572                 :          0 :                 ret2 = ext4_mark_inode_dirty(handle, inode);
    1573         [ #  # ]:          0 :                 if (!ret)
    1574                 :            :                         ret = ret2;
    1575                 :            :         }
    1576                 :            : 
    1577   [ #  #  #  # ]:          0 :         if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
    1578                 :            :                 /* if we have allocated more blocks and copied
    1579                 :            :                  * less. We will have blocks allocated outside
    1580                 :            :                  * inode->i_size. So truncate them
    1581                 :            :                  */
    1582                 :          0 :                 ext4_orphan_add(handle, inode);
    1583                 :            : 
    1584                 :            : errout:
    1585                 :          0 :         ret2 = ext4_journal_stop(handle);
    1586         [ #  # ]:          0 :         if (!ret)
    1587                 :            :                 ret = ret2;
    1588         [ #  # ]:          0 :         if (pos + len > inode->i_size && !verity) {
    1589                 :          0 :                 ext4_truncate_failed_write(inode);
    1590                 :            :                 /*
    1591                 :            :                  * If truncate failed early the inode might still be
    1592                 :            :                  * on the orphan list; we need to make sure the inode
    1593                 :            :                  * is removed from the orphan list in that case.
    1594                 :            :                  */
    1595         [ #  # ]:          0 :                 if (inode->i_nlink)
    1596                 :          0 :                         ext4_orphan_del(NULL, inode);
    1597                 :            :         }
    1598                 :            : 
    1599         [ #  # ]:          0 :         return ret ? ret : copied;
    1600                 :            : }
    1601                 :            : 
    1602                 :            : /*
    1603                 :            :  * Reserve space for a single cluster
    1604                 :            :  */
    1605                 :     173878 : static int ext4_da_reserve_space(struct inode *inode)
    1606                 :            : {
    1607                 :     173878 :         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
    1608                 :            :         struct ext4_inode_info *ei = EXT4_I(inode);
    1609                 :            :         int ret;
    1610                 :            : 
    1611                 :            :         /*
    1612                 :            :          * We will charge metadata quota at writeout time; this saves
    1613                 :            :          * us from metadata over-estimation, though we may go over by
    1614                 :            :          * a small amount in the end.  Here we just reserve for data.
    1615                 :            :          */
    1616                 :     173878 :         ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
    1617         [ +  - ]:     173878 :         if (ret)
    1618                 :            :                 return ret;
    1619                 :            : 
    1620                 :            :         spin_lock(&ei->i_block_reservation_lock);
    1621         [ -  + ]:     173878 :         if (ext4_claim_free_clusters(sbi, 1, 0)) {
    1622                 :            :                 spin_unlock(&ei->i_block_reservation_lock);
    1623                 :          0 :                 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
    1624                 :          0 :                 return -ENOSPC;
    1625                 :            :         }
    1626                 :     173878 :         ei->i_reserved_data_blocks++;
    1627                 :     173878 :         trace_ext4_da_reserve_space(inode);
    1628                 :            :         spin_unlock(&ei->i_block_reservation_lock);
    1629                 :            : 
    1630                 :     173878 :         return 0;       /* success */
    1631                 :            : }
    1632                 :            : 
    1633                 :       6835 : void ext4_da_release_space(struct inode *inode, int to_free)
    1634                 :            : {
    1635                 :       6835 :         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
    1636                 :            :         struct ext4_inode_info *ei = EXT4_I(inode);
    1637                 :            : 
    1638         [ +  + ]:       6835 :         if (!to_free)
    1639                 :       6835 :                 return;         /* Nothing to release, exit */
    1640                 :            : 
    1641                 :            :         spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
    1642                 :            : 
    1643                 :        474 :         trace_ext4_da_release_space(inode, to_free);
    1644         [ -  + ]:        474 :         if (unlikely(to_free > ei->i_reserved_data_blocks)) {
    1645                 :            :                 /*
    1646                 :            :                  * if there aren't enough reserved blocks, then the
    1647                 :            :                  * counter is messed up somewhere.  Since this
    1648                 :            :                  * function is called from invalidate page, it's
    1649                 :            :                  * harmless to return without any action.
    1650                 :            :                  */
    1651                 :          0 :                 ext4_warning(inode->i_sb, "ext4_da_release_space: "
    1652                 :            :                          "ino %lu, to_free %d with only %d reserved "
    1653                 :            :                          "data blocks", inode->i_ino, to_free,
    1654                 :            :                          ei->i_reserved_data_blocks);
    1655                 :          0 :                 WARN_ON(1);
    1656                 :          0 :                 to_free = ei->i_reserved_data_blocks;
    1657                 :            :         }
    1658                 :        474 :         ei->i_reserved_data_blocks -= to_free;
    1659                 :            : 
    1660                 :            :         /* update fs dirty data blocks counter */
    1661                 :        474 :         percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
    1662                 :            : 
    1663                 :            :         spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
    1664                 :            : 
    1665                 :        474 :         dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
    1666                 :            : }
    1667                 :            : 
    1668                 :            : /*
    1669                 :            :  * Delayed allocation stuff
    1670                 :            :  */
    1671                 :            : 
    1672                 :            : struct mpage_da_data {
    1673                 :            :         struct inode *inode;
    1674                 :            :         struct writeback_control *wbc;
    1675                 :            : 
    1676                 :            :         pgoff_t first_page;     /* The first page to write */
    1677                 :            :         pgoff_t next_page;      /* Current page to examine */
    1678                 :            :         pgoff_t last_page;      /* Last page to examine */
    1679                 :            :         /*
    1680                 :            :          * Extent to map - this can be after first_page because that can be
    1681                 :            :          * fully mapped. We somewhat abuse m_flags to store whether the extent
    1682                 :            :          * is delalloc or unwritten.
    1683                 :            :          */
    1684                 :            :         struct ext4_map_blocks map;
    1685                 :            :         struct ext4_io_submit io_submit;        /* IO submission data */
    1686                 :            :         unsigned int do_map:1;
    1687                 :            : };
    1688                 :            : 
    1689                 :      48215 : static void mpage_release_unused_pages(struct mpage_da_data *mpd,
    1690                 :            :                                        bool invalidate)
    1691                 :            : {
    1692                 :            :         int nr_pages, i;
    1693                 :            :         pgoff_t index, end;
    1694                 :            :         struct pagevec pvec;
    1695                 :      48215 :         struct inode *inode = mpd->inode;
    1696                 :      48215 :         struct address_space *mapping = inode->i_mapping;
    1697                 :            : 
    1698                 :            :         /* This is necessary when next_page == 0. */
    1699         [ +  + ]:      48215 :         if (mpd->first_page >= mpd->next_page)
    1700                 :      34553 :                 return;
    1701                 :            : 
    1702                 :      13662 :         index = mpd->first_page;
    1703                 :      13662 :         end   = mpd->next_page - 1;
    1704         [ -  + ]:      13662 :         if (invalidate) {
    1705                 :            :                 ext4_lblk_t start, last;
    1706                 :          0 :                 start = index << (PAGE_SHIFT - inode->i_blkbits);
    1707                 :          0 :                 last = end << (PAGE_SHIFT - inode->i_blkbits);
    1708                 :          0 :                 ext4_es_remove_extent(inode, start, last - start + 1);
    1709                 :            :         }
    1710                 :            : 
    1711                 :            :         pagevec_init(&pvec);
    1712         [ +  + ]:      27324 :         while (index <= end) {
    1713                 :      13662 :                 nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end);
    1714         [ +  - ]:      13662 :                 if (nr_pages == 0)
    1715                 :            :                         break;
    1716         [ +  + ]:      13685 :                 for (i = 0; i < nr_pages; i++) {
    1717                 :      13685 :                         struct page *page = pvec.pages[i];
    1718                 :            : 
    1719         [ -  + ]:      13685 :                         BUG_ON(!PageLocked(page));
    1720         [ -  + ]:      13685 :                         BUG_ON(PageWriteback(page));
    1721         [ -  + ]:      13685 :                         if (invalidate) {
    1722         [ #  # ]:          0 :                                 if (page_mapped(page))
    1723                 :          0 :                                         clear_page_dirty_for_io(page);
    1724                 :          0 :                                 block_invalidatepage(page, 0, PAGE_SIZE);
    1725                 :            :                                 ClearPageUptodate(page);
    1726                 :            :                         }
    1727                 :      13685 :                         unlock_page(page);
    1728                 :            :                 }
    1729                 :            :                 pagevec_release(&pvec);
    1730                 :            :         }
    1731                 :            : }
    1732                 :            : 
    1733                 :          0 : static void ext4_print_free_blocks(struct inode *inode)
    1734                 :            : {
    1735                 :          0 :         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
    1736                 :            :         struct super_block *sb = inode->i_sb;
    1737                 :            :         struct ext4_inode_info *ei = EXT4_I(inode);
    1738                 :            : 
    1739                 :          0 :         ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
    1740                 :            :                EXT4_C2B(EXT4_SB(inode->i_sb),
    1741                 :            :                         ext4_count_free_clusters(sb)));
    1742                 :          0 :         ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
    1743                 :          0 :         ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
    1744                 :            :                (long long) EXT4_C2B(EXT4_SB(sb),
    1745                 :            :                 percpu_counter_sum(&sbi->s_freeclusters_counter)));
    1746                 :          0 :         ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
    1747                 :            :                (long long) EXT4_C2B(EXT4_SB(sb),
    1748                 :            :                 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
    1749                 :          0 :         ext4_msg(sb, KERN_CRIT, "Block reservation details");
    1750                 :          0 :         ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
    1751                 :            :                  ei->i_reserved_data_blocks);
    1752                 :          0 :         return;
    1753                 :            : }
    1754                 :            : 
    1755                 :          0 : static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
    1756                 :            : {
    1757   [ #  #  #  #  :          0 :         return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
                   #  # ]
    1758                 :            : }
    1759                 :            : 
    1760                 :            : /*
    1761                 :            :  * ext4_insert_delayed_block - adds a delayed block to the extents status
    1762                 :            :  *                             tree, incrementing the reserved cluster/block
    1763                 :            :  *                             count or making a pending reservation
    1764                 :            :  *                             where needed
    1765                 :            :  *
    1766                 :            :  * @inode - file containing the newly added block
    1767                 :            :  * @lblk - logical block to be added
    1768                 :            :  *
    1769                 :            :  * Returns 0 on success, negative error code on failure.
    1770                 :            :  */
    1771                 :     173878 : static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
    1772                 :            : {
    1773                 :     173878 :         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
    1774                 :            :         int ret;
    1775                 :            :         bool allocated = false;
    1776                 :            : 
    1777                 :            :         /*
    1778                 :            :          * If the cluster containing lblk is shared with a delayed,
    1779                 :            :          * written, or unwritten extent in a bigalloc file system, it's
    1780                 :            :          * already been accounted for and does not need to be reserved.
    1781                 :            :          * A pending reservation must be made for the cluster if it's
    1782                 :            :          * shared with a written or unwritten extent and doesn't already
    1783                 :            :          * have one.  Written and unwritten extents can be purged from the
    1784                 :            :          * extents status tree if the system is under memory pressure, so
    1785                 :            :          * it's necessary to examine the extent tree if a search of the
    1786                 :            :          * extents status tree doesn't get a match.
    1787                 :            :          */
    1788         [ +  - ]:     173878 :         if (sbi->s_cluster_ratio == 1) {
    1789                 :     173878 :                 ret = ext4_da_reserve_space(inode);
    1790         [ +  - ]:     173878 :                 if (ret != 0)   /* ENOSPC */
    1791                 :            :                         goto errout;
    1792                 :            :         } else {   /* bigalloc */
    1793         [ #  # ]:          0 :                 if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
    1794         [ #  # ]:          0 :                         if (!ext4_es_scan_clu(inode,
    1795                 :            :                                               &ext4_es_is_mapped, lblk)) {
    1796                 :          0 :                                 ret = ext4_clu_mapped(inode,
    1797                 :          0 :                                                       EXT4_B2C(sbi, lblk));
    1798         [ #  # ]:          0 :                                 if (ret < 0)
    1799                 :            :                                         goto errout;
    1800         [ #  # ]:          0 :                                 if (ret == 0) {
    1801                 :          0 :                                         ret = ext4_da_reserve_space(inode);
    1802         [ #  # ]:          0 :                                         if (ret != 0)   /* ENOSPC */
    1803                 :            :                                                 goto errout;
    1804                 :            :                                 } else {
    1805                 :            :                                         allocated = true;
    1806                 :            :                                 }
    1807                 :            :                         } else {
    1808                 :            :                                 allocated = true;
    1809                 :            :                         }
    1810                 :            :                 }
    1811                 :            :         }
    1812                 :            : 
    1813                 :     173878 :         ret = ext4_es_insert_delayed_block(inode, lblk, allocated);
    1814                 :            : 
    1815                 :            : errout:
    1816                 :     173878 :         return ret;
    1817                 :            : }
    1818                 :            : 
    1819                 :            : /*
    1820                 :            :  * This function is grabs code from the very beginning of
    1821                 :            :  * ext4_map_blocks, but assumes that the caller is from delayed write
    1822                 :            :  * time. This function looks up the requested blocks and sets the
    1823                 :            :  * buffer delay bit under the protection of i_data_sem.
    1824                 :            :  */
    1825                 :     176584 : static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
    1826                 :            :                               struct ext4_map_blocks *map,
    1827                 :            :                               struct buffer_head *bh)
    1828                 :            : {
    1829                 :            :         struct extent_status es;
    1830                 :            :         int retval;
    1831                 :            :         sector_t invalid_block = ~((sector_t) 0xffff);
    1832                 :            : #ifdef ES_AGGRESSIVE_TEST
    1833                 :            :         struct ext4_map_blocks orig_map;
    1834                 :            : 
    1835                 :            :         memcpy(&orig_map, map, sizeof(*map));
    1836                 :            : #endif
    1837                 :            : 
    1838         [ -  + ]:     529752 :         if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
    1839                 :            :                 invalid_block = ~0;
    1840                 :            : 
    1841                 :     176584 :         map->m_flags = 0;
    1842                 :            :         ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
    1843                 :            :                   "logical block %lu\n", inode->i_ino, map->m_len,
    1844                 :            :                   (unsigned long) map->m_lblk);
    1845                 :            : 
    1846                 :            :         /* Lookup extent status tree firstly */
    1847         [ +  + ]:     176584 :         if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) {
    1848         [ +  + ]:      74161 :                 if (ext4_es_is_hole(&es)) {
    1849                 :            :                         retval = 0;
    1850                 :      73514 :                         down_read(&EXT4_I(inode)->i_data_sem);
    1851                 :      73514 :                         goto add_delayed;
    1852                 :            :                 }
    1853                 :            : 
    1854                 :            :                 /*
    1855                 :            :                  * Delayed extent could be allocated by fallocate.
    1856                 :            :                  * So we need to check it.
    1857                 :            :                  */
    1858   [ -  +  #  # ]:        647 :                 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
    1859                 :          0 :                         map_bh(bh, inode->i_sb, invalid_block);
    1860                 :            :                         set_buffer_new(bh);
    1861                 :            :                         set_buffer_delay(bh);
    1862                 :            :                         return 0;
    1863                 :            :                 }
    1864                 :            : 
    1865                 :        647 :                 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
    1866                 :        647 :                 retval = es.es_len - (iblock - es.es_lblk);
    1867         [ +  + ]:        647 :                 if (retval > map->m_len)
    1868                 :         26 :                         retval = map->m_len;
    1869                 :        647 :                 map->m_len = retval;
    1870         [ +  + ]:        647 :                 if (ext4_es_is_written(&es))
    1871                 :        621 :                         map->m_flags |= EXT4_MAP_MAPPED;
    1872         [ +  - ]:         26 :                 else if (ext4_es_is_unwritten(&es))
    1873                 :         26 :                         map->m_flags |= EXT4_MAP_UNWRITTEN;
    1874                 :            :                 else
    1875                 :          0 :                         BUG();
    1876                 :            : 
    1877                 :            : #ifdef ES_AGGRESSIVE_TEST
    1878                 :            :                 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
    1879                 :            : #endif
    1880                 :        647 :                 return retval;
    1881                 :            :         }
    1882                 :            : 
    1883                 :            :         /*
    1884                 :            :          * Try to see if we can get the block without requesting a new
    1885                 :            :          * file system block.
    1886                 :            :          */
    1887                 :     102423 :         down_read(&EXT4_I(inode)->i_data_sem);
    1888         [ +  - ]:     102423 :         if (ext4_has_inline_data(inode))
    1889                 :            :                 retval = 0;
    1890         [ +  - ]:     102423 :         else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
    1891                 :     102423 :                 retval = ext4_ext_map_blocks(NULL, inode, map, 0);
    1892                 :            :         else
    1893                 :          0 :                 retval = ext4_ind_map_blocks(NULL, inode, map, 0);
    1894                 :            : 
    1895                 :            : add_delayed:
    1896         [ +  + ]:     175937 :         if (retval == 0) {
    1897                 :            :                 int ret;
    1898                 :            : 
    1899                 :            :                 /*
    1900                 :            :                  * XXX: __block_prepare_write() unmaps passed block,
    1901                 :            :                  * is it OK?
    1902                 :            :                  */
    1903                 :            : 
    1904                 :     173878 :                 ret = ext4_insert_delayed_block(inode, map->m_lblk);
    1905         [ +  - ]:     173878 :                 if (ret != 0) {
    1906                 :            :                         retval = ret;
    1907                 :            :                         goto out_unlock;
    1908                 :            :                 }
    1909                 :            : 
    1910                 :     173878 :                 map_bh(bh, inode->i_sb, invalid_block);
    1911                 :            :                 set_buffer_new(bh);
    1912                 :            :                 set_buffer_delay(bh);
    1913         [ +  - ]:       2059 :         } else if (retval > 0) {
    1914                 :            :                 int ret;
    1915                 :            :                 unsigned int status;
    1916                 :            : 
    1917         [ -  + ]:       2059 :                 if (unlikely(retval != map->m_len)) {
    1918                 :          0 :                         ext4_warning(inode->i_sb,
    1919                 :            :                                      "ES len assertion failed for inode "
    1920                 :            :                                      "%lu: retval %d != map->m_len %d",
    1921                 :            :                                      inode->i_ino, retval, map->m_len);
    1922                 :          0 :                         WARN_ON(1);
    1923                 :            :                 }
    1924                 :            : 
    1925         [ +  - ]:       2059 :                 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
    1926                 :            :                                 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
    1927                 :       2059 :                 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
    1928                 :            :                                             map->m_pblk, status);
    1929         [ -  + ]:       2059 :                 if (ret != 0)
    1930                 :            :                         retval = ret;
    1931                 :            :         }
    1932                 :            : 
    1933                 :            : out_unlock:
    1934                 :     175937 :         up_read((&EXT4_I(inode)->i_data_sem));
    1935                 :            : 
    1936                 :     175937 :         return retval;
    1937                 :            : }
    1938                 :            : 
    1939                 :            : /*
    1940                 :            :  * This is a special get_block_t callback which is used by
    1941                 :            :  * ext4_da_write_begin().  It will either return mapped block or
    1942                 :            :  * reserve space for a single block.
    1943                 :            :  *
    1944                 :            :  * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
    1945                 :            :  * We also have b_blocknr = -1 and b_bdev initialized properly
    1946                 :            :  *
    1947                 :            :  * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
    1948                 :            :  * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
    1949                 :            :  * initialized properly.
    1950                 :            :  */
    1951                 :     176584 : int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
    1952                 :            :                            struct buffer_head *bh, int create)
    1953                 :            : {
    1954                 :            :         struct ext4_map_blocks map;
    1955                 :            :         int ret = 0;
    1956                 :            : 
    1957         [ -  + ]:     176584 :         BUG_ON(create == 0);
    1958         [ -  + ]:     176584 :         BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
    1959                 :            : 
    1960                 :     176584 :         map.m_lblk = iblock;
    1961                 :     176584 :         map.m_len = 1;
    1962                 :            : 
    1963                 :            :         /*
    1964                 :            :          * first, we need to know whether the block is allocated already
    1965                 :            :          * preallocated blocks are unmapped but should treated
    1966                 :            :          * the same as allocated blocks.
    1967                 :            :          */
    1968                 :     176584 :         ret = ext4_da_map_blocks(inode, iblock, &map, bh);
    1969         [ +  + ]:     176584 :         if (ret <= 0)
    1970                 :            :                 return ret;
    1971                 :            : 
    1972                 :       2706 :         map_bh(bh, inode->i_sb, map.m_pblk);
    1973                 :       2706 :         ext4_update_bh_state(bh, map.m_flags);
    1974                 :            : 
    1975         [ +  + ]:       2706 :         if (buffer_unwritten(bh)) {
    1976                 :            :                 /* A delayed write to unwritten bh should be marked
    1977                 :            :                  * new and mapped.  Mapped ensures that we don't do
    1978                 :            :                  * get_block multiple times when we write to the same
    1979                 :            :                  * offset and new ensures that we do proper zero out
    1980                 :            :                  * for partial write.
    1981                 :            :                  */
    1982                 :            :                 set_buffer_new(bh);
    1983                 :            :                 set_buffer_mapped(bh);
    1984                 :            :         }
    1985                 :            :         return 0;
    1986                 :            : }
    1987                 :            : 
    1988                 :          0 : static int bget_one(handle_t *handle, struct buffer_head *bh)
    1989                 :            : {
    1990                 :            :         get_bh(bh);
    1991                 :          0 :         return 0;
    1992                 :            : }
    1993                 :            : 
    1994                 :          0 : static int bput_one(handle_t *handle, struct buffer_head *bh)
    1995                 :            : {
    1996                 :          0 :         put_bh(bh);
    1997                 :          0 :         return 0;
    1998                 :            : }
    1999                 :            : 
    2000                 :          0 : static int __ext4_journalled_writepage(struct page *page,
    2001                 :            :                                        unsigned int len)
    2002                 :            : {
    2003                 :          0 :         struct address_space *mapping = page->mapping;
    2004                 :          0 :         struct inode *inode = mapping->host;
    2005                 :            :         struct buffer_head *page_bufs = NULL;
    2006                 :            :         handle_t *handle = NULL;
    2007                 :            :         int ret = 0, err = 0;
    2008                 :            :         int inline_data = ext4_has_inline_data(inode);
    2009                 :            :         struct buffer_head *inode_bh = NULL;
    2010                 :            : 
    2011                 :            :         ClearPageChecked(page);
    2012                 :            : 
    2013         [ #  # ]:          0 :         if (inline_data) {
    2014         [ #  # ]:          0 :                 BUG_ON(page->index != 0);
    2015         [ #  # ]:          0 :                 BUG_ON(len > ext4_get_max_inline_size(inode));
    2016                 :          0 :                 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
    2017         [ #  # ]:          0 :                 if (inode_bh == NULL)
    2018                 :            :                         goto out;
    2019                 :            :         } else {
    2020         [ #  # ]:          0 :                 page_bufs = page_buffers(page);
    2021         [ #  # ]:          0 :                 if (!page_bufs) {
    2022                 :          0 :                         BUG();
    2023                 :            :                         goto out;
    2024                 :            :                 }
    2025                 :          0 :                 ext4_walk_page_buffers(handle, page_bufs, 0, len,
    2026                 :            :                                        NULL, bget_one);
    2027                 :            :         }
    2028                 :            :         /*
    2029                 :            :          * We need to release the page lock before we start the
    2030                 :            :          * journal, so grab a reference so the page won't disappear
    2031                 :            :          * out from under us.
    2032                 :            :          */
    2033                 :          0 :         get_page(page);
    2034                 :          0 :         unlock_page(page);
    2035                 :            : 
    2036                 :          0 :         handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
    2037                 :            :                                     ext4_writepage_trans_blocks(inode));
    2038         [ #  # ]:          0 :         if (IS_ERR(handle)) {
    2039                 :            :                 ret = PTR_ERR(handle);
    2040                 :          0 :                 put_page(page);
    2041                 :          0 :                 goto out_no_pagelock;
    2042                 :            :         }
    2043         [ #  # ]:          0 :         BUG_ON(!ext4_handle_valid(handle));
    2044                 :            : 
    2045                 :          0 :         lock_page(page);
    2046                 :          0 :         put_page(page);
    2047         [ #  # ]:          0 :         if (page->mapping != mapping) {
    2048                 :            :                 /* The page got truncated from under us */
    2049                 :          0 :                 ext4_journal_stop(handle);
    2050                 :            :                 ret = 0;
    2051                 :          0 :                 goto out;
    2052                 :            :         }
    2053                 :            : 
    2054         [ #  # ]:          0 :         if (inline_data) {
    2055                 :          0 :                 ret = ext4_mark_inode_dirty(handle, inode);
    2056                 :            :         } else {
    2057                 :          0 :                 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
    2058                 :            :                                              do_journal_get_write_access);
    2059                 :            : 
    2060                 :          0 :                 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
    2061                 :            :                                              write_end_fn);
    2062                 :            :         }
    2063         [ #  # ]:          0 :         if (ret == 0)
    2064                 :            :                 ret = err;
    2065                 :          0 :         EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
    2066                 :          0 :         err = ext4_journal_stop(handle);
    2067         [ #  # ]:          0 :         if (!ret)
    2068                 :            :                 ret = err;
    2069                 :            : 
    2070         [ #  # ]:          0 :         if (!ext4_has_inline_data(inode))
    2071                 :          0 :                 ext4_walk_page_buffers(NULL, page_bufs, 0, len,
    2072                 :            :                                        NULL, bput_one);
    2073                 :            :         ext4_set_inode_state(inode, EXT4_STATE_JDATA);
    2074                 :            : out:
    2075                 :          0 :         unlock_page(page);
    2076                 :            : out_no_pagelock:
    2077                 :            :         brelse(inode_bh);
    2078                 :          0 :         return ret;
    2079                 :            : }
    2080                 :            : 
    2081                 :            : /*
    2082                 :            :  * Note that we don't need to start a transaction unless we're journaling data
    2083                 :            :  * because we should have holes filled from ext4_page_mkwrite(). We even don't
    2084                 :            :  * need to file the inode to the transaction's list in ordered mode because if
    2085                 :            :  * we are writing back data added by write(), the inode is already there and if
    2086                 :            :  * we are writing back data modified via mmap(), no one guarantees in which
    2087                 :            :  * transaction the data will hit the disk. In case we are journaling data, we
    2088                 :            :  * cannot start transaction directly because transaction start ranks above page
    2089                 :            :  * lock so we have to do some magic.
    2090                 :            :  *
    2091                 :            :  * This function can get called via...
    2092                 :            :  *   - ext4_writepages after taking page lock (have journal handle)
    2093                 :            :  *   - journal_submit_inode_data_buffers (no journal handle)
    2094                 :            :  *   - shrink_page_list via the kswapd/direct reclaim (no journal handle)
    2095                 :            :  *   - grab_page_cache when doing write_begin (have journal handle)
    2096                 :            :  *
    2097                 :            :  * We don't do any block allocation in this function. If we have page with
    2098                 :            :  * multiple blocks we need to write those buffer_heads that are mapped. This
    2099                 :            :  * is important for mmaped based write. So if we do with blocksize 1K
    2100                 :            :  * truncate(f, 1024);
    2101                 :            :  * a = mmap(f, 0, 4096);
    2102                 :            :  * a[0] = 'a';
    2103                 :            :  * truncate(f, 4096);
    2104                 :            :  * we have in the page first buffer_head mapped via page_mkwrite call back
    2105                 :            :  * but other buffer_heads would be unmapped but dirty (dirty done via the
    2106                 :            :  * do_wp_page). So writepage should write the first block. If we modify
    2107                 :            :  * the mmap area beyond 1024 we will again get a page_fault and the
    2108                 :            :  * page_mkwrite callback will do the block allocation and mark the
    2109                 :            :  * buffer_heads mapped.
    2110                 :            :  *
    2111                 :            :  * We redirty the page if we have any buffer_heads that is either delay or
    2112                 :            :  * unwritten in the page.
    2113                 :            :  *
    2114                 :            :  * We can get recursively called as show below.
    2115                 :            :  *
    2116                 :            :  *      ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
    2117                 :            :  *              ext4_writepage()
    2118                 :            :  *
    2119                 :            :  * But since we don't do any block allocation we should not deadlock.
    2120                 :            :  * Page also have the dirty flag cleared so we don't get recurive page_lock.
    2121                 :            :  */
    2122                 :          0 : static int ext4_writepage(struct page *page,
    2123                 :            :                           struct writeback_control *wbc)
    2124                 :            : {
    2125                 :            :         int ret = 0;
    2126                 :            :         loff_t size;
    2127                 :            :         unsigned int len;
    2128                 :            :         struct buffer_head *page_bufs = NULL;
    2129                 :          0 :         struct inode *inode = page->mapping->host;
    2130                 :            :         struct ext4_io_submit io_submit;
    2131                 :            :         bool keep_towrite = false;
    2132                 :            : 
    2133         [ #  # ]:          0 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
    2134                 :          0 :                 inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
    2135                 :          0 :                 unlock_page(page);
    2136                 :          0 :                 return -EIO;
    2137                 :            :         }
    2138                 :            : 
    2139                 :          0 :         trace_ext4_writepage(page);
    2140                 :            :         size = i_size_read(inode);
    2141         [ #  # ]:          0 :         if (page->index == size >> PAGE_SHIFT &&
    2142                 :            :             !ext4_verity_in_progress(inode))
    2143                 :          0 :                 len = size & ~PAGE_MASK;
    2144                 :            :         else
    2145                 :            :                 len = PAGE_SIZE;
    2146                 :            : 
    2147         [ #  # ]:          0 :         page_bufs = page_buffers(page);
    2148                 :            :         /*
    2149                 :            :          * We cannot do block allocation or other extent handling in this
    2150                 :            :          * function. If there are buffers needing that, we have to redirty
    2151                 :            :          * the page. But we may reach here when we do a journal commit via
    2152                 :            :          * journal_submit_inode_data_buffers() and in that case we must write
    2153                 :            :          * allocated buffers to achieve data=ordered mode guarantees.
    2154                 :            :          *
    2155                 :            :          * Also, if there is only one buffer per page (the fs block
    2156                 :            :          * size == the page size), if one buffer needs block
    2157                 :            :          * allocation or needs to modify the extent tree to clear the
    2158                 :            :          * unwritten flag, we know that the page can't be written at
    2159                 :            :          * all, so we might as well refuse the write immediately.
    2160                 :            :          * Unfortunately if the block size != page size, we can't as
    2161                 :            :          * easily detect this case using ext4_walk_page_buffers(), but
    2162                 :            :          * for the extremely common case, this is an optimization that
    2163                 :            :          * skips a useless round trip through ext4_bio_write_page().
    2164                 :            :          */
    2165         [ #  # ]:          0 :         if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
    2166                 :            :                                    ext4_bh_delay_or_unwritten)) {
    2167                 :          0 :                 redirty_page_for_writepage(wbc, page);
    2168   [ #  #  #  # ]:          0 :                 if ((current->flags & PF_MEMALLOC) ||
    2169                 :          0 :                     (inode->i_sb->s_blocksize == PAGE_SIZE)) {
    2170                 :            :                         /*
    2171                 :            :                          * For memory cleaning there's no point in writing only
    2172                 :            :                          * some buffers. So just bail out. Warn if we came here
    2173                 :            :                          * from direct reclaim.
    2174                 :            :                          */
    2175   [ #  #  #  # ]:          0 :                         WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
    2176                 :            :                                                         == PF_MEMALLOC);
    2177                 :          0 :                         unlock_page(page);
    2178                 :          0 :                         return 0;
    2179                 :            :                 }
    2180                 :            :                 keep_towrite = true;
    2181                 :            :         }
    2182                 :            : 
    2183   [ #  #  #  # ]:          0 :         if (PageChecked(page) && ext4_should_journal_data(inode))
    2184                 :            :                 /*
    2185                 :            :                  * It's mmapped pagecache.  Add buffers and journal it.  There
    2186                 :            :                  * doesn't seem much point in redirtying the page here.
    2187                 :            :                  */
    2188                 :          0 :                 return __ext4_journalled_writepage(page, len);
    2189                 :            : 
    2190                 :          0 :         ext4_io_submit_init(&io_submit, wbc);
    2191                 :          0 :         io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
    2192         [ #  # ]:          0 :         if (!io_submit.io_end) {
    2193                 :          0 :                 redirty_page_for_writepage(wbc, page);
    2194                 :          0 :                 unlock_page(page);
    2195                 :          0 :                 return -ENOMEM;
    2196                 :            :         }
    2197                 :          0 :         ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite);
    2198                 :          0 :         ext4_io_submit(&io_submit);
    2199                 :            :         /* Drop io_end reference we got from init */
    2200                 :          0 :         ext4_put_io_end_defer(io_submit.io_end);
    2201                 :          0 :         return ret;
    2202                 :            : }
    2203                 :            : 
    2204                 :      32530 : static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
    2205                 :            : {
    2206                 :            :         int len;
    2207                 :            :         loff_t size;
    2208                 :            :         int err;
    2209                 :            : 
    2210         [ -  + ]:      32530 :         BUG_ON(page->index != mpd->first_page);
    2211                 :      32530 :         clear_page_dirty_for_io(page);
    2212                 :            :         /*
    2213                 :            :          * We have to be very careful here!  Nothing protects writeback path
    2214                 :            :          * against i_size changes and the page can be writeably mapped into
    2215                 :            :          * page tables. So an application can be growing i_size and writing
    2216                 :            :          * data through mmap while writeback runs. clear_page_dirty_for_io()
    2217                 :            :          * write-protects our page in page tables and the page cannot get
    2218                 :            :          * written to again until we release page lock. So only after
    2219                 :            :          * clear_page_dirty_for_io() we are safe to sample i_size for
    2220                 :            :          * ext4_bio_write_page() to zero-out tail of the written page. We rely
    2221                 :            :          * on the barrier provided by TestClearPageDirty in
    2222                 :            :          * clear_page_dirty_for_io() to make sure i_size is really sampled only
    2223                 :            :          * after page tables are updated.
    2224                 :            :          */
    2225                 :      32530 :         size = i_size_read(mpd->inode);
    2226         [ +  + ]:      32530 :         if (page->index == size >> PAGE_SHIFT &&
    2227                 :            :             !ext4_verity_in_progress(mpd->inode))
    2228                 :      16267 :                 len = size & ~PAGE_MASK;
    2229                 :            :         else
    2230                 :            :                 len = PAGE_SIZE;
    2231                 :      32530 :         err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false);
    2232         [ +  - ]:      32530 :         if (!err)
    2233                 :      32530 :                 mpd->wbc->nr_to_write--;
    2234                 :      32530 :         mpd->first_page++;
    2235                 :            : 
    2236                 :      32530 :         return err;
    2237                 :            : }
    2238                 :            : 
    2239                 :            : #define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
    2240                 :            : 
    2241                 :            : /*
    2242                 :            :  * mballoc gives us at most this number of blocks...
    2243                 :            :  * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
    2244                 :            :  * The rest of mballoc seems to handle chunks up to full group size.
    2245                 :            :  */
    2246                 :            : #define MAX_WRITEPAGES_EXTENT_LEN 2048
    2247                 :            : 
    2248                 :            : /*
    2249                 :            :  * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
    2250                 :            :  *
    2251                 :            :  * @mpd - extent of blocks
    2252                 :            :  * @lblk - logical number of the block in the file
    2253                 :            :  * @bh - buffer head we want to add to the extent
    2254                 :            :  *
    2255                 :            :  * The function is used to collect contig. blocks in the same state. If the
    2256                 :            :  * buffer doesn't require mapping for writeback and we haven't started the
    2257                 :            :  * extent of buffers to map yet, the function returns 'true' immediately - the
    2258                 :            :  * caller can write the buffer right away. Otherwise the function returns true
    2259                 :            :  * if the block has been added to the extent, false if the block couldn't be
    2260                 :            :  * added.
    2261                 :            :  */
    2262                 :      46215 : static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
    2263                 :            :                                    struct buffer_head *bh)
    2264                 :            : {
    2265                 :            :         struct ext4_map_blocks *map = &mpd->map;
    2266                 :            : 
    2267                 :            :         /* Buffer that doesn't need mapping for writeback? */
    2268   [ +  -  +  -  :     138645 :         if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
                   +  + ]
    2269         [ +  + ]:       4957 :             (!buffer_delay(bh) && !buffer_unwritten(bh))) {
    2270                 :            :                 /* So far no extent to map => we write the buffer right away */
    2271         [ -  + ]:       4905 :                 if (map->m_len == 0)
    2272                 :            :                         return true;
    2273                 :          0 :                 return false;
    2274                 :            :         }
    2275                 :            : 
    2276                 :            :         /* First block in the extent? */
    2277         [ +  + ]:      41310 :         if (map->m_len == 0) {
    2278                 :            :                 /* We cannot map unless handle is started... */
    2279         [ +  + ]:      27318 :                 if (!mpd->do_map)
    2280                 :            :                         return false;
    2281                 :      13662 :                 map->m_lblk = lblk;
    2282                 :      13662 :                 map->m_len = 1;
    2283                 :      13662 :                 map->m_flags = bh->b_state & BH_FLAGS;
    2284                 :      13662 :                 return true;
    2285                 :            :         }
    2286                 :            : 
    2287                 :            :         /* Don't go larger than mballoc is willing to allocate */
    2288         [ +  - ]:      13992 :         if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
    2289                 :            :                 return false;
    2290                 :            : 
    2291                 :            :         /* Can we merge the block to our big extent? */
    2292   [ +  -  +  - ]:      27984 :         if (lblk == map->m_lblk + map->m_len &&
    2293                 :      13992 :             (bh->b_state & BH_FLAGS) == map->m_flags) {
    2294                 :      13992 :                 map->m_len++;
    2295                 :      13992 :                 return true;
    2296                 :            :         }
    2297                 :            :         return false;
    2298                 :            : }
    2299                 :            : 
    2300                 :            : /*
    2301                 :            :  * mpage_process_page_bufs - submit page buffers for IO or add them to extent
    2302                 :            :  *
    2303                 :            :  * @mpd - extent of blocks for mapping
    2304                 :            :  * @head - the first buffer in the page
    2305                 :            :  * @bh - buffer we should start processing from
    2306                 :            :  * @lblk - logical number of the block in the file corresponding to @bh
    2307                 :            :  *
    2308                 :            :  * Walk through page buffers from @bh upto @head (exclusive) and either submit
    2309                 :            :  * the page for IO if all buffers in this page were mapped and there's no
    2310                 :            :  * accumulated extent of buffers to map or add buffers in the page to the
    2311                 :            :  * extent of buffers to map. The function returns 1 if the caller can continue
    2312                 :            :  * by processing the next page, 0 if it should stop adding buffers to the
    2313                 :            :  * extent to map because we cannot extend it anymore. It can also return value
    2314                 :            :  * < 0 in case of error during IO submission.
    2315                 :            :  */
    2316                 :      46215 : static int mpage_process_page_bufs(struct mpage_da_data *mpd,
    2317                 :            :                                    struct buffer_head *head,
    2318                 :            :                                    struct buffer_head *bh,
    2319                 :            :                                    ext4_lblk_t lblk)
    2320                 :            : {
    2321                 :      46215 :         struct inode *inode = mpd->inode;
    2322                 :            :         int err;
    2323                 :      92430 :         ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1)
    2324                 :      46215 :                                                         >> inode->i_blkbits;
    2325                 :            : 
    2326                 :            :         if (ext4_verity_in_progress(inode))
    2327                 :            :                 blocks = EXT_MAX_BLOCKS;
    2328                 :            : 
    2329                 :            :         do {
    2330         [ -  + ]:      46215 :                 BUG_ON(buffer_locked(bh));
    2331                 :            : 
    2332   [ +  -  +  + ]:      46215 :                 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
    2333                 :            :                         /* Found extent to map? */
    2334         [ +  - ]:      13656 :                         if (mpd->map.m_len)
    2335                 :            :                                 return 0;
    2336                 :            :                         /* Buffer needs mapping and handle is not started? */
    2337         [ -  + ]:      13656 :                         if (!mpd->do_map)
    2338                 :            :                                 return 0;
    2339                 :            :                         /* Everything mapped so far and we hit EOF */
    2340                 :            :                         break;
    2341                 :            :                 }
    2342         [ -  + ]:      32559 :         } while (lblk++, (bh = bh->b_this_page) != head);
    2343                 :            :         /* So far everything mapped? Submit the page for IO. */
    2344         [ +  + ]:      32559 :         if (mpd->map.m_len == 0) {
    2345                 :       4905 :                 err = mpage_submit_page(mpd, head->b_page);
    2346         [ +  - ]:       4905 :                 if (err < 0)
    2347                 :            :                         return err;
    2348                 :            :         }
    2349                 :      32559 :         return lblk < blocks;
    2350                 :            : }
    2351                 :            : 
    2352                 :            : /*
    2353                 :            :  * mpage_map_buffers - update buffers corresponding to changed extent and
    2354                 :            :  *                     submit fully mapped pages for IO
    2355                 :            :  *
    2356                 :            :  * @mpd - description of extent to map, on return next extent to map
    2357                 :            :  *
    2358                 :            :  * Scan buffers corresponding to changed extent (we expect corresponding pages
    2359                 :            :  * to be already locked) and update buffer state according to new extent state.
    2360                 :            :  * We map delalloc buffers to their physical location, clear unwritten bits,
    2361                 :            :  * and mark buffers as uninit when we perform writes to unwritten extents
    2362                 :            :  * and do extent conversion after IO is finished. If the last page is not fully
    2363                 :            :  * mapped, we update @map to the next extent in the last page that needs
    2364                 :            :  * mapping. Otherwise we submit the page for IO.
    2365                 :            :  */
    2366                 :      13662 : static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
    2367                 :            : {
    2368                 :            :         struct pagevec pvec;
    2369                 :            :         int nr_pages, i;
    2370                 :      13662 :         struct inode *inode = mpd->inode;
    2371                 :            :         struct buffer_head *head, *bh;
    2372                 :      13662 :         int bpp_bits = PAGE_SHIFT - inode->i_blkbits;
    2373                 :            :         pgoff_t start, end;
    2374                 :            :         ext4_lblk_t lblk;
    2375                 :            :         sector_t pblock;
    2376                 :            :         int err;
    2377                 :            : 
    2378                 :      13662 :         start = mpd->map.m_lblk >> bpp_bits;
    2379                 :      13662 :         end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
    2380                 :      13662 :         lblk = start << bpp_bits;
    2381                 :      13662 :         pblock = mpd->map.m_pblk;
    2382                 :            : 
    2383                 :            :         pagevec_init(&pvec);
    2384         [ +  + ]:      27359 :         while (start <= end) {
    2385                 :      13697 :                 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping,
    2386                 :            :                                                 &start, end);
    2387         [ +  - ]:      13697 :                 if (nr_pages == 0)
    2388                 :            :                         break;
    2389         [ +  + ]:      27625 :                 for (i = 0; i < nr_pages; i++) {
    2390                 :      27625 :                         struct page *page = pvec.pages[i];
    2391                 :            : 
    2392         [ -  + ]:      27625 :                         bh = head = page_buffers(page);
    2393                 :            :                         do {
    2394         [ -  + ]:      27625 :                                 if (lblk < mpd->map.m_lblk)
    2395                 :          0 :                                         continue;
    2396         [ -  + ]:      27625 :                                 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
    2397                 :            :                                         /*
    2398                 :            :                                          * Buffer after end of mapped extent.
    2399                 :            :                                          * Find next buffer in the page to map.
    2400                 :            :                                          */
    2401                 :          0 :                                         mpd->map.m_len = 0;
    2402                 :          0 :                                         mpd->map.m_flags = 0;
    2403                 :            :                                         /*
    2404                 :            :                                          * FIXME: If dioread_nolock supports
    2405                 :            :                                          * blocksize < pagesize, we need to make
    2406                 :            :                                          * sure we add size mapped so far to
    2407                 :            :                                          * io_end->size as the following call
    2408                 :            :                                          * can submit the page for IO.
    2409                 :            :                                          */
    2410                 :          0 :                                         err = mpage_process_page_bufs(mpd, head,
    2411                 :            :                                                                       bh, lblk);
    2412                 :            :                                         pagevec_release(&pvec);
    2413         [ #  # ]:          0 :                                         if (err > 0)
    2414                 :            :                                                 err = 0;
    2415                 :          0 :                                         return err;
    2416                 :            :                                 }
    2417         [ +  + ]:      27625 :                                 if (buffer_delay(bh)) {
    2418                 :            :                                         clear_buffer_delay(bh);
    2419                 :      27599 :                                         bh->b_blocknr = pblock++;
    2420                 :            :                                 }
    2421                 :            :                                 clear_buffer_unwritten(bh);
    2422         [ -  + ]:      27625 :                         } while (lblk++, (bh = bh->b_this_page) != head);
    2423                 :            : 
    2424                 :            :                         /*
    2425                 :            :                          * FIXME: This is going to break if dioread_nolock
    2426                 :            :                          * supports blocksize < pagesize as we will try to
    2427                 :            :                          * convert potentially unmapped parts of inode.
    2428                 :            :                          */
    2429                 :      27625 :                         mpd->io_submit.io_end->size += PAGE_SIZE;
    2430                 :            :                         /* Page fully mapped - let IO run! */
    2431                 :      27625 :                         err = mpage_submit_page(mpd, page);
    2432         [ -  + ]:      27625 :                         if (err < 0) {
    2433                 :            :                                 pagevec_release(&pvec);
    2434                 :          0 :                                 return err;
    2435                 :            :                         }
    2436                 :            :                 }
    2437                 :            :                 pagevec_release(&pvec);
    2438                 :            :         }
    2439                 :            :         /* Extent fully mapped and matches with page boundary. We are done. */
    2440                 :      13662 :         mpd->map.m_len = 0;
    2441                 :      13662 :         mpd->map.m_flags = 0;
    2442                 :      13662 :         return 0;
    2443                 :            : }
    2444                 :            : 
    2445                 :      13662 : static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
    2446                 :            : {
    2447                 :      13662 :         struct inode *inode = mpd->inode;
    2448                 :      13662 :         struct ext4_map_blocks *map = &mpd->map;
    2449                 :            :         int get_blocks_flags;
    2450                 :            :         int err, dioread_nolock;
    2451                 :            : 
    2452                 :      13662 :         trace_ext4_da_write_pages_extent(inode, map);
    2453                 :            :         /*
    2454                 :            :          * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
    2455                 :            :          * to convert an unwritten extent to be initialized (in the case
    2456                 :            :          * where we have written into one or more preallocated blocks).  It is
    2457                 :            :          * possible that we're going to need more metadata blocks than
    2458                 :            :          * previously reserved. However we must not fail because we're in
    2459                 :            :          * writeback and there is nothing we can do about it so it might result
    2460                 :            :          * in data loss.  So use reserved blocks to allocate metadata if
    2461                 :            :          * possible.
    2462                 :            :          *
    2463                 :            :          * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
    2464                 :            :          * the blocks in question are delalloc blocks.  This indicates
    2465                 :            :          * that the blocks and quotas has already been checked when
    2466                 :            :          * the data was copied into the page cache.
    2467                 :            :          */
    2468                 :            :         get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
    2469                 :            :                            EXT4_GET_BLOCKS_METADATA_NOFAIL |
    2470                 :            :                            EXT4_GET_BLOCKS_IO_SUBMIT;
    2471                 :      13662 :         dioread_nolock = ext4_should_dioread_nolock(inode);
    2472         [ -  + ]:      13662 :         if (dioread_nolock)
    2473                 :            :                 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
    2474         [ +  + ]:      13662 :         if (map->m_flags & (1 << BH_Delay))
    2475                 :      13636 :                 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
    2476                 :            : 
    2477                 :      13662 :         err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
    2478         [ +  - ]:      13662 :         if (err < 0)
    2479                 :            :                 return err;
    2480   [ -  +  #  # ]:      13662 :         if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
    2481   [ #  #  #  # ]:          0 :                 if (!mpd->io_submit.io_end->handle &&
    2482                 :            :                     ext4_handle_valid(handle)) {
    2483                 :          0 :                         mpd->io_submit.io_end->handle = handle->h_rsv_handle;
    2484                 :          0 :                         handle->h_rsv_handle = NULL;
    2485                 :            :                 }
    2486                 :          0 :                 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
    2487                 :            :         }
    2488                 :            : 
    2489         [ -  + ]:      13662 :         BUG_ON(map->m_len == 0);
    2490                 :            :         return 0;
    2491                 :            : }
    2492                 :            : 
    2493                 :            : /*
    2494                 :            :  * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
    2495                 :            :  *                               mpd->len and submit pages underlying it for IO
    2496                 :            :  *
    2497                 :            :  * @handle - handle for journal operations
    2498                 :            :  * @mpd - extent to map
    2499                 :            :  * @give_up_on_write - we set this to true iff there is a fatal error and there
    2500                 :            :  *                     is no hope of writing the data. The caller should discard
    2501                 :            :  *                     dirty pages to avoid infinite loops.
    2502                 :            :  *
    2503                 :            :  * The function maps extent starting at mpd->lblk of length mpd->len. If it is
    2504                 :            :  * delayed, blocks are allocated, if it is unwritten, we may need to convert
    2505                 :            :  * them to initialized or split the described range from larger unwritten
    2506                 :            :  * extent. Note that we need not map all the described range since allocation
    2507                 :            :  * can return less blocks or the range is covered by more unwritten extents. We
    2508                 :            :  * cannot map more because we are limited by reserved transaction credits. On
    2509                 :            :  * the other hand we always make sure that the last touched page is fully
    2510                 :            :  * mapped so that it can be written out (and thus forward progress is
    2511                 :            :  * guaranteed). After mapping we submit all mapped pages for IO.
    2512                 :            :  */
    2513                 :      13662 : static int mpage_map_and_submit_extent(handle_t *handle,
    2514                 :            :                                        struct mpage_da_data *mpd,
    2515                 :            :                                        bool *give_up_on_write)
    2516                 :            : {
    2517                 :      13662 :         struct inode *inode = mpd->inode;
    2518                 :            :         struct ext4_map_blocks *map = &mpd->map;
    2519                 :            :         int err;
    2520                 :            :         loff_t disksize;
    2521                 :            :         int progress = 0;
    2522                 :            : 
    2523                 :      27324 :         mpd->io_submit.io_end->offset =
    2524                 :      13662 :                                 ((loff_t)map->m_lblk) << inode->i_blkbits;
    2525                 :            :         do {
    2526                 :      13662 :                 err = mpage_map_one_extent(handle, mpd);
    2527         [ -  + ]:      13662 :                 if (err < 0) {
    2528                 :          0 :                         struct super_block *sb = inode->i_sb;
    2529                 :            : 
    2530   [ #  #  #  # ]:          0 :                         if (ext4_forced_shutdown(EXT4_SB(sb)) ||
    2531                 :          0 :                             EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
    2532                 :            :                                 goto invalidate_dirty_pages;
    2533                 :            :                         /*
    2534                 :            :                          * Let the uper layers retry transient errors.
    2535                 :            :                          * In the case of ENOSPC, if ext4_count_free_blocks()
    2536                 :            :                          * is non-zero, a commit should free up blocks.
    2537                 :            :                          */
    2538   [ #  #  #  # ]:          0 :                         if ((err == -ENOMEM) ||
    2539         [ #  # ]:          0 :                             (err == -ENOSPC && ext4_count_free_clusters(sb))) {
    2540         [ #  # ]:          0 :                                 if (progress)
    2541                 :            :                                         goto update_disksize;
    2542                 :          0 :                                 return err;
    2543                 :            :                         }
    2544                 :          0 :                         ext4_msg(sb, KERN_CRIT,
    2545                 :            :                                  "Delayed block allocation failed for "
    2546                 :            :                                  "inode %lu at logical offset %llu with"
    2547                 :            :                                  " max blocks %u with error %d",
    2548                 :            :                                  inode->i_ino,
    2549                 :            :                                  (unsigned long long)map->m_lblk,
    2550                 :            :                                  (unsigned)map->m_len, -err);
    2551                 :          0 :                         ext4_msg(sb, KERN_CRIT,
    2552                 :            :                                  "This should not happen!! Data will "
    2553                 :            :                                  "be lost\n");
    2554         [ #  # ]:          0 :                         if (err == -ENOSPC)
    2555                 :          0 :                                 ext4_print_free_blocks(inode);
    2556                 :            :                 invalidate_dirty_pages:
    2557                 :          0 :                         *give_up_on_write = true;
    2558                 :          0 :                         return err;
    2559                 :            :                 }
    2560                 :            :                 progress = 1;
    2561                 :            :                 /*
    2562                 :            :                  * Update buffer state, submit mapped pages, and get us new
    2563                 :            :                  * extent to map
    2564                 :            :                  */
    2565                 :      13662 :                 err = mpage_map_and_submit_buffers(mpd);
    2566         [ +  - ]:      13662 :                 if (err < 0)
    2567                 :            :                         goto update_disksize;
    2568         [ -  + ]:      13662 :         } while (map->m_len);
    2569                 :            : 
    2570                 :            : update_disksize:
    2571                 :            :         /*
    2572                 :            :          * Update on-disk size after IO is submitted.  Races with
    2573                 :            :          * truncate are avoided by checking i_size under i_data_sem.
    2574                 :            :          */
    2575                 :      13662 :         disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT;
    2576         [ +  + ]:      13662 :         if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) {
    2577                 :            :                 int err2;
    2578                 :            :                 loff_t i_size;
    2579                 :            : 
    2580                 :      13636 :                 down_write(&EXT4_I(inode)->i_data_sem);
    2581                 :            :                 i_size = i_size_read(inode);
    2582         [ +  + ]:      13636 :                 if (disksize > i_size)
    2583                 :            :                         disksize = i_size;
    2584         [ +  + ]:      13636 :                 if (disksize > EXT4_I(inode)->i_disksize)
    2585                 :      13635 :                         EXT4_I(inode)->i_disksize = disksize;
    2586                 :      13636 :                 up_write(&EXT4_I(inode)->i_data_sem);
    2587                 :      13636 :                 err2 = ext4_mark_inode_dirty(handle, inode);
    2588         [ -  + ]:      13636 :                 if (err2)
    2589                 :          0 :                         ext4_error(inode->i_sb,
    2590                 :            :                                    "Failed to mark inode %lu dirty",
    2591                 :            :                                    inode->i_ino);
    2592         [ +  - ]:      13636 :                 if (!err)
    2593                 :            :                         err = err2;
    2594                 :            :         }
    2595                 :      13662 :         return err;
    2596                 :            : }
    2597                 :            : 
    2598                 :            : /*
    2599                 :            :  * Calculate the total number of credits to reserve for one writepages
    2600                 :            :  * iteration. This is called from ext4_writepages(). We map an extent of
    2601                 :            :  * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
    2602                 :            :  * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
    2603                 :            :  * bpp - 1 blocks in bpp different extents.
    2604                 :            :  */
    2605                 :      30221 : static int ext4_da_writepages_trans_blocks(struct inode *inode)
    2606                 :            : {
    2607                 :            :         int bpp = ext4_journal_blocks_per_page(inode);
    2608                 :            : 
    2609                 :      30221 :         return ext4_meta_trans_blocks(inode,
    2610                 :            :                                 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
    2611                 :            : }
    2612                 :            : 
    2613                 :            : /*
    2614                 :            :  * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
    2615                 :            :  *                               and underlying extent to map
    2616                 :            :  *
    2617                 :            :  * @mpd - where to look for pages
    2618                 :            :  *
    2619                 :            :  * Walk dirty pages in the mapping. If they are fully mapped, submit them for
    2620                 :            :  * IO immediately. When we find a page which isn't mapped we start accumulating
    2621                 :            :  * extent of buffers underlying these pages that needs mapping (formed by
    2622                 :            :  * either delayed or unwritten buffers). We also lock the pages containing
    2623                 :            :  * these buffers. The extent found is returned in @mpd structure (starting at
    2624                 :            :  * mpd->lblk with length mpd->len blocks).
    2625                 :            :  *
    2626                 :            :  * Note that this function can attach bios to one io_end structure which are
    2627                 :            :  * neither logically nor physically contiguous. Although it may seem as an
    2628                 :            :  * unnecessary complication, it is actually inevitable in blocksize < pagesize
    2629                 :            :  * case as we need to track IO to all buffers underlying a page in one io_end.
    2630                 :            :  */
    2631                 :      48215 : static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
    2632                 :            : {
    2633                 :      48215 :         struct address_space *mapping = mpd->inode->i_mapping;
    2634                 :            :         struct pagevec pvec;
    2635                 :            :         unsigned int nr_pages;
    2636                 :      48215 :         long left = mpd->wbc->nr_to_write;
    2637                 :      48215 :         pgoff_t index = mpd->first_page;
    2638                 :      48215 :         pgoff_t end = mpd->last_page;
    2639                 :            :         xa_mark_t tag;
    2640                 :            :         int i, err = 0;
    2641                 :      48215 :         int blkbits = mpd->inode->i_blkbits;
    2642                 :            :         ext4_lblk_t lblk;
    2643                 :            :         struct buffer_head *head;
    2644                 :            : 
    2645   [ +  +  +  - ]:      48215 :         if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
    2646                 :            :                 tag = PAGECACHE_TAG_TOWRITE;
    2647                 :            :         else
    2648                 :            :                 tag = PAGECACHE_TAG_DIRTY;
    2649                 :            : 
    2650                 :            :         pagevec_init(&pvec);
    2651                 :      48215 :         mpd->map.m_len = 0;
    2652                 :      48215 :         mpd->next_page = index;
    2653         [ +  + ]:      96975 :         while (index <= end) {
    2654                 :      48286 :                 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
    2655                 :            :                                 tag);
    2656         [ +  + ]:      48286 :                 if (nr_pages == 0)
    2657                 :            :                         goto out;
    2658                 :            : 
    2659         [ +  + ]:      16192 :                 for (i = 0; i < nr_pages; i++) {
    2660                 :      46231 :                         struct page *page = pvec.pages[i];
    2661                 :            : 
    2662                 :            :                         /*
    2663                 :            :                          * Accumulated enough dirty pages? This doesn't apply
    2664                 :            :                          * to WB_SYNC_ALL mode. For integrity sync we have to
    2665                 :            :                          * keep going because someone may be concurrently
    2666                 :            :                          * dirtying pages, and we might have synced a lot of
    2667                 :            :                          * newly appeared dirty pages, but have not synced all
    2668                 :            :                          * of the old dirty pages.
    2669                 :            :                          */
    2670   [ +  +  +  - ]:      46231 :                         if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
    2671                 :            :                                 goto out;
    2672                 :            : 
    2673                 :            :                         /* If we can't merge this page, we are done. */
    2674   [ +  +  +  - ]:      46231 :                         if (mpd->map.m_len > 0 && mpd->next_page != page->index)
    2675                 :            :                                 goto out;
    2676                 :            : 
    2677                 :      46231 :                         lock_page(page);
    2678                 :            :                         /*
    2679                 :            :                          * If the page is no longer dirty, or its mapping no
    2680                 :            :                          * longer corresponds to inode we are writing (which
    2681                 :            :                          * means it has been truncated or invalidated), or the
    2682                 :            :                          * page is already under writeback and we are not doing
    2683                 :            :                          * a data integrity writeback, skip the page
    2684                 :            :                          */
    2685   [ +  -  +  + ]:      92462 :                         if (!PageDirty(page) ||
    2686         [ -  + ]:         16 :                             (PageWriteback(page) &&
    2687         [ -  + ]:      46231 :                              (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
    2688                 :      46215 :                             unlikely(page->mapping != mapping)) {
    2689                 :         16 :                                 unlock_page(page);
    2690                 :         16 :                                 continue;
    2691                 :            :                         }
    2692                 :            : 
    2693                 :      46215 :                         wait_on_page_writeback(page);
    2694         [ -  + ]:      46215 :                         BUG_ON(PageWriteback(page));
    2695                 :            : 
    2696         [ +  + ]:      46215 :                         if (mpd->map.m_len == 0)
    2697                 :      32223 :                                 mpd->first_page = page->index;
    2698                 :      46215 :                         mpd->next_page = page->index + 1;
    2699                 :            :                         /* Add all dirty buffers to mpd */
    2700                 :      92430 :                         lblk = ((ext4_lblk_t)page->index) <<
    2701                 :      46215 :                                 (PAGE_SHIFT - blkbits);
    2702         [ -  + ]:      46215 :                         head = page_buffers(page);
    2703                 :      46215 :                         err = mpage_process_page_bufs(mpd, head, head, lblk);
    2704         [ +  + ]:      46215 :                         if (err <= 0)
    2705                 :            :                                 goto out;
    2706                 :            :                         err = 0;
    2707                 :      16176 :                         left--;
    2708                 :            :                 }
    2709                 :            :                 pagevec_release(&pvec);
    2710                 :        545 :                 cond_resched();
    2711                 :            :         }
    2712                 :            :         return 0;
    2713                 :            : out:
    2714                 :            :         pagevec_release(&pvec);
    2715                 :      47741 :         return err;
    2716                 :            : }
    2717                 :            : 
    2718                 :      18065 : static int ext4_writepages(struct address_space *mapping,
    2719                 :            :                            struct writeback_control *wbc)
    2720                 :            : {
    2721                 :            :         pgoff_t writeback_index = 0;
    2722                 :      18065 :         long nr_to_write = wbc->nr_to_write;
    2723                 :            :         int range_whole = 0;
    2724                 :            :         int cycled = 1;
    2725                 :            :         handle_t *handle = NULL;
    2726                 :            :         struct mpage_da_data mpd;
    2727                 :      18065 :         struct inode *inode = mapping->host;
    2728                 :            :         int needed_blocks, rsv_blocks = 0, ret = 0;
    2729                 :      18065 :         struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
    2730                 :            :         bool done;
    2731                 :            :         struct blk_plug plug;
    2732                 :      18065 :         bool give_up_on_write = false;
    2733                 :            : 
    2734         [ +  - ]:      18065 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
    2735                 :            :                 return -EIO;
    2736                 :            : 
    2737                 :      18065 :         percpu_down_read(&sbi->s_writepages_rwsem);
    2738                 :      18065 :         trace_ext4_writepages(inode, wbc);
    2739                 :            : 
    2740                 :            :         /*
    2741                 :            :          * No pages to write? This is mainly a kludge to avoid starting
    2742                 :            :          * a transaction for special inodes like journal inode on last iput()
    2743                 :            :          * because that could violate lock ordering on umount
    2744                 :            :          */
    2745   [ +  +  +  + ]:      35594 :         if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
    2746                 :            :                 goto out_writepages;
    2747                 :            : 
    2748         [ -  + ]:      16411 :         if (ext4_should_journal_data(inode)) {
    2749                 :          0 :                 ret = generic_writepages(mapping, wbc);
    2750                 :          0 :                 goto out_writepages;
    2751                 :            :         }
    2752                 :            : 
    2753                 :            :         /*
    2754                 :            :          * If the filesystem has aborted, it is read-only, so return
    2755                 :            :          * right away instead of dumping stack traces later on that
    2756                 :            :          * will obscure the real source of the problem.  We test
    2757                 :            :          * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because
    2758                 :            :          * the latter could be true if the filesystem is mounted
    2759                 :            :          * read-only, and in that case, ext4_writepages should
    2760                 :            :          * *never* be called, so if that ever happens, we would want
    2761                 :            :          * the stack trace.
    2762                 :            :          */
    2763   [ +  -  +  - ]:      32822 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) ||
    2764                 :            :                      sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) {
    2765                 :            :                 ret = -EROFS;
    2766                 :            :                 goto out_writepages;
    2767                 :            :         }
    2768                 :            : 
    2769                 :            :         /*
    2770                 :            :          * If we have inline data and arrive here, it means that
    2771                 :            :          * we will soon create the block for the 1st page, so
    2772                 :            :          * we'd better clear the inline data here.
    2773                 :            :          */
    2774         [ -  + ]:      16411 :         if (ext4_has_inline_data(inode)) {
    2775                 :            :                 /* Just inode will be modified... */
    2776                 :            :                 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
    2777         [ #  # ]:          0 :                 if (IS_ERR(handle)) {
    2778                 :            :                         ret = PTR_ERR(handle);
    2779                 :          0 :                         goto out_writepages;
    2780                 :            :                 }
    2781         [ #  # ]:          0 :                 BUG_ON(ext4_test_inode_state(inode,
    2782                 :            :                                 EXT4_STATE_MAY_INLINE_DATA));
    2783                 :          0 :                 ext4_destroy_inline_data(handle, inode);
    2784                 :          0 :                 ext4_journal_stop(handle);
    2785                 :            :         }
    2786                 :            : 
    2787         [ -  + ]:      16411 :         if (ext4_should_dioread_nolock(inode)) {
    2788                 :            :                 /*
    2789                 :            :                  * We may need to convert up to one extent per block in
    2790                 :            :                  * the page and we may dirty the inode.
    2791                 :            :                  */
    2792                 :          0 :                 rsv_blocks = 1 + ext4_chunk_trans_blocks(inode,
    2793                 :          0 :                                                 PAGE_SIZE >> inode->i_blkbits);
    2794                 :            :         }
    2795                 :            : 
    2796   [ +  -  +  + ]:      16411 :         if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
    2797                 :            :                 range_whole = 1;
    2798                 :            : 
    2799         [ +  + ]:      16411 :         if (wbc->range_cyclic) {
    2800                 :      12842 :                 writeback_index = mapping->writeback_index;
    2801         [ +  + ]:      12842 :                 if (writeback_index)
    2802                 :            :                         cycled = 0;
    2803                 :      12842 :                 mpd.first_page = writeback_index;
    2804                 :      12842 :                 mpd.last_page = -1;
    2805                 :            :         } else {
    2806                 :       3569 :                 mpd.first_page = wbc->range_start >> PAGE_SHIFT;
    2807                 :       3569 :                 mpd.last_page = wbc->range_end >> PAGE_SHIFT;
    2808                 :            :         }
    2809                 :            : 
    2810                 :      16411 :         mpd.inode = inode;
    2811                 :      16411 :         mpd.wbc = wbc;
    2812                 :      16411 :         ext4_io_submit_init(&mpd.io_submit, wbc);
    2813                 :            : retry:
    2814   [ +  +  -  + ]:      17994 :         if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
    2815                 :       2837 :                 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
    2816                 :            :         done = false;
    2817                 :      17994 :         blk_start_plug(&plug);
    2818                 :            : 
    2819                 :            :         /*
    2820                 :            :          * First writeback pages that don't need mapping - we can avoid
    2821                 :            :          * starting a transaction unnecessarily and also avoid being blocked
    2822                 :            :          * in the block layer on device congestion while having transaction
    2823                 :            :          * started.
    2824                 :            :          */
    2825                 :      17994 :         mpd.do_map = 0;
    2826                 :      17994 :         mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
    2827         [ +  - ]:      17994 :         if (!mpd.io_submit.io_end) {
    2828                 :            :                 ret = -ENOMEM;
    2829                 :            :                 goto unplug;
    2830                 :            :         }
    2831                 :      17994 :         ret = mpage_prepare_extent_to_map(&mpd);
    2832                 :            :         /* Unlock pages we didn't use */
    2833                 :      17994 :         mpage_release_unused_pages(&mpd, false);
    2834                 :            :         /* Submit prepared bio */
    2835                 :      17994 :         ext4_io_submit(&mpd.io_submit);
    2836                 :      17994 :         ext4_put_io_end_defer(mpd.io_submit.io_end);
    2837                 :      17994 :         mpd.io_submit.io_end = NULL;
    2838         [ +  - ]:      17994 :         if (ret < 0)
    2839                 :            :                 goto unplug;
    2840                 :            : 
    2841   [ +  +  +  + ]:      48215 :         while (!done && mpd.first_page <= mpd.last_page) {
    2842                 :            :                 /* For each extent of pages we use new io_end */
    2843                 :      30221 :                 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
    2844         [ +  - ]:      30221 :                 if (!mpd.io_submit.io_end) {
    2845                 :            :                         ret = -ENOMEM;
    2846                 :            :                         break;
    2847                 :            :                 }
    2848                 :            : 
    2849                 :            :                 /*
    2850                 :            :                  * We have two constraints: We find one extent to map and we
    2851                 :            :                  * must always write out whole page (makes a difference when
    2852                 :            :                  * blocksize < pagesize) so that we don't block on IO when we
    2853                 :            :                  * try to write out the rest of the page. Journalled mode is
    2854                 :            :                  * not supported by delalloc.
    2855                 :            :                  */
    2856         [ -  + ]:      30221 :                 BUG_ON(ext4_should_journal_data(inode));
    2857                 :      30221 :                 needed_blocks = ext4_da_writepages_trans_blocks(inode);
    2858                 :            : 
    2859                 :            :                 /* start a new transaction */
    2860                 :            :                 handle = ext4_journal_start_with_reserve(inode,
    2861                 :            :                                 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
    2862         [ -  + ]:      30221 :                 if (IS_ERR(handle)) {
    2863                 :            :                         ret = PTR_ERR(handle);
    2864                 :          0 :                         ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
    2865                 :            :                                "%ld pages, ino %lu; err %d", __func__,
    2866                 :            :                                 wbc->nr_to_write, inode->i_ino, ret);
    2867                 :            :                         /* Release allocated io_end */
    2868                 :          0 :                         ext4_put_io_end(mpd.io_submit.io_end);
    2869                 :          0 :                         mpd.io_submit.io_end = NULL;
    2870                 :          0 :                         break;
    2871                 :            :                 }
    2872                 :      30221 :                 mpd.do_map = 1;
    2873                 :            : 
    2874                 :      30221 :                 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
    2875                 :      30221 :                 ret = mpage_prepare_extent_to_map(&mpd);
    2876         [ +  - ]:      30221 :                 if (!ret) {
    2877         [ +  + ]:      30221 :                         if (mpd.map.m_len)
    2878                 :      13662 :                                 ret = mpage_map_and_submit_extent(handle, &mpd,
    2879                 :            :                                         &give_up_on_write);
    2880                 :            :                         else {
    2881                 :            :                                 /*
    2882                 :            :                                  * We scanned the whole range (or exhausted
    2883                 :            :                                  * nr_to_write), submitted what was mapped and
    2884                 :            :                                  * didn't find anything needing mapping. We are
    2885                 :            :                                  * done.
    2886                 :            :                                  */
    2887                 :            :                                 done = true;
    2888                 :            :                         }
    2889                 :            :                 }
    2890                 :            :                 /*
    2891                 :            :                  * Caution: If the handle is synchronous,
    2892                 :            :                  * ext4_journal_stop() can wait for transaction commit
    2893                 :            :                  * to finish which may depend on writeback of pages to
    2894                 :            :                  * complete or on page lock to be released.  In that
    2895                 :            :                  * case, we have to wait until after after we have
    2896                 :            :                  * submitted all the IO, released page locks we hold,
    2897                 :            :                  * and dropped io_end reference (for extent conversion
    2898                 :            :                  * to be able to complete) before stopping the handle.
    2899                 :            :                  */
    2900   [ +  -  +  - ]:      30221 :                 if (!ext4_handle_valid(handle) || handle->h_sync == 0) {
    2901                 :      30221 :                         ext4_journal_stop(handle);
    2902                 :            :                         handle = NULL;
    2903                 :      30221 :                         mpd.do_map = 0;
    2904                 :            :                 }
    2905                 :            :                 /* Unlock pages we didn't use */
    2906                 :      30221 :                 mpage_release_unused_pages(&mpd, give_up_on_write);
    2907                 :            :                 /* Submit prepared bio */
    2908                 :      30221 :                 ext4_io_submit(&mpd.io_submit);
    2909                 :            : 
    2910                 :            :                 /*
    2911                 :            :                  * Drop our io_end reference we got from init. We have
    2912                 :            :                  * to be careful and use deferred io_end finishing if
    2913                 :            :                  * we are still holding the transaction as we can
    2914                 :            :                  * release the last reference to io_end which may end
    2915                 :            :                  * up doing unwritten extent conversion.
    2916                 :            :                  */
    2917         [ -  + ]:      30221 :                 if (handle) {
    2918                 :          0 :                         ext4_put_io_end_defer(mpd.io_submit.io_end);
    2919                 :          0 :                         ext4_journal_stop(handle);
    2920                 :            :                 } else
    2921                 :      30221 :                         ext4_put_io_end(mpd.io_submit.io_end);
    2922                 :      30221 :                 mpd.io_submit.io_end = NULL;
    2923                 :            : 
    2924   [ -  +  #  # ]:      30221 :                 if (ret == -ENOSPC && sbi->s_journal) {
    2925                 :            :                         /*
    2926                 :            :                          * Commit the transaction which would
    2927                 :            :                          * free blocks released in the transaction
    2928                 :            :                          * and try again
    2929                 :            :                          */
    2930                 :          0 :                         jbd2_journal_force_commit_nested(sbi->s_journal);
    2931                 :            :                         ret = 0;
    2932                 :          0 :                         continue;
    2933                 :            :                 }
    2934                 :            :                 /* Fatal error - ENOMEM, EIO... */
    2935         [ +  - ]:      30221 :                 if (ret)
    2936                 :            :                         break;
    2937                 :            :         }
    2938                 :            : unplug:
    2939                 :      17994 :         blk_finish_plug(&plug);
    2940   [ +  +  +  - ]:      17994 :         if (!ret && !cycled && wbc->nr_to_write > 0) {
    2941                 :            :                 cycled = 1;
    2942                 :       1583 :                 mpd.last_page = writeback_index - 1;
    2943                 :       1583 :                 mpd.first_page = 0;
    2944                 :       1583 :                 goto retry;
    2945                 :            :         }
    2946                 :            : 
    2947                 :            :         /* Update index */
    2948   [ +  +  +  +  :      16411 :         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
                   +  - ]
    2949                 :            :                 /*
    2950                 :            :                  * Set the writeback_index so that range_cyclic
    2951                 :            :                  * mode will write it back later
    2952                 :            :                  */
    2953                 :      16361 :                 mapping->writeback_index = mpd.first_page;
    2954                 :            : 
    2955                 :            : out_writepages:
    2956                 :      18065 :         trace_ext4_writepages_result(inode, wbc, ret,
    2957                 :      18065 :                                      nr_to_write - wbc->nr_to_write);
    2958                 :      18065 :         percpu_up_read(&sbi->s_writepages_rwsem);
    2959                 :      18065 :         return ret;
    2960                 :            : }
    2961                 :            : 
    2962                 :            : static int ext4_dax_writepages(struct address_space *mapping,
    2963                 :            :                                struct writeback_control *wbc)
    2964                 :            : {
    2965                 :            :         int ret;
    2966                 :            :         long nr_to_write = wbc->nr_to_write;
    2967                 :            :         struct inode *inode = mapping->host;
    2968                 :            :         struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
    2969                 :            : 
    2970                 :            :         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
    2971                 :            :                 return -EIO;
    2972                 :            : 
    2973                 :            :         percpu_down_read(&sbi->s_writepages_rwsem);
    2974                 :            :         trace_ext4_writepages(inode, wbc);
    2975                 :            : 
    2976                 :            :         ret = dax_writeback_mapping_range(mapping, inode->i_sb->s_bdev, wbc);
    2977                 :            :         trace_ext4_writepages_result(inode, wbc, ret,
    2978                 :            :                                      nr_to_write - wbc->nr_to_write);
    2979                 :            :         percpu_up_read(&sbi->s_writepages_rwsem);
    2980                 :            :         return ret;
    2981                 :            : }
    2982                 :            : 
    2983                 :     411940 : static int ext4_nonda_switch(struct super_block *sb)
    2984                 :            : {
    2985                 :            :         s64 free_clusters, dirty_clusters;
    2986                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    2987                 :            : 
    2988                 :            :         /*
    2989                 :            :          * switch to non delalloc mode if we are running low
    2990                 :            :          * on free block. The free block accounting via percpu
    2991                 :            :          * counters can get slightly wrong with percpu_counter_batch getting
    2992                 :            :          * accumulated on each CPU without updating global counters
    2993                 :            :          * Delalloc need an accurate free block accounting. So switch
    2994                 :            :          * to non delalloc when we are near to error range.
    2995                 :            :          */
    2996                 :            :         free_clusters =
    2997                 :            :                 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
    2998                 :            :         dirty_clusters =
    2999                 :            :                 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
    3000                 :            :         /*
    3001                 :            :          * Start pushing delalloc when 1/2 of free blocks are dirty.
    3002                 :            :          */
    3003   [ +  +  -  + ]:     411940 :         if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
    3004                 :          0 :                 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
    3005                 :            : 
    3006   [ +  -  +  + ]:     823870 :         if (2 * free_clusters < 3 * dirty_clusters ||
    3007                 :     411935 :             free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
    3008                 :            :                 /*
    3009                 :            :                  * free block count is less than 150% of dirty blocks
    3010                 :            :                  * or free blocks is less than watermark
    3011                 :            :                  */
    3012                 :            :                 return 1;
    3013                 :            :         }
    3014                 :     411929 :         return 0;
    3015                 :            : }
    3016                 :            : 
    3017                 :            : /* We always reserve for an inode update; the superblock could be there too */
    3018                 :            : static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
    3019                 :            : {
    3020         [ -  + ]:     821694 :         if (likely(ext4_has_feature_large_file(inode->i_sb)))
    3021                 :            :                 return 1;
    3022                 :            : 
    3023         [ #  # ]:          0 :         if (pos + len <= 0x7fffffffULL)
    3024                 :            :                 return 1;
    3025                 :            : 
    3026                 :            :         /* We might need to update the superblock to set LARGE_FILE */
    3027                 :            :         return 2;
    3028                 :            : }
    3029                 :            : 
    3030                 :     410851 : static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
    3031                 :            :                                loff_t pos, unsigned len, unsigned flags,
    3032                 :            :                                struct page **pagep, void **fsdata)
    3033                 :            : {
    3034                 :     410851 :         int ret, retries = 0;
    3035                 :            :         struct page *page;
    3036                 :            :         pgoff_t index;
    3037                 :     410851 :         struct inode *inode = mapping->host;
    3038                 :            :         handle_t *handle;
    3039                 :            : 
    3040         [ +  + ]:     821702 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
    3041                 :            :                 return -EIO;
    3042                 :            : 
    3043                 :     410838 :         index = pos >> PAGE_SHIFT;
    3044                 :            : 
    3045   [ +  +  +  + ]:     410838 :         if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) ||
    3046                 :            :             ext4_verity_in_progress(inode)) {
    3047                 :          9 :                 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
    3048                 :          9 :                 return ext4_write_begin(file, mapping, pos,
    3049                 :            :                                         len, flags, pagep, fsdata);
    3050                 :            :         }
    3051                 :     410816 :         *fsdata = (void *)0;
    3052                 :     410816 :         trace_ext4_da_write_begin(inode, pos, len, flags);
    3053                 :            : 
    3054         [ +  - ]:     410813 :         if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
    3055                 :          0 :                 ret = ext4_da_write_inline_data_begin(mapping, inode,
    3056                 :            :                                                       pos, len, flags,
    3057                 :            :                                                       pagep, fsdata);
    3058         [ #  # ]:          0 :                 if (ret < 0)
    3059                 :            :                         return ret;
    3060         [ #  # ]:          0 :                 if (ret == 1)
    3061                 :            :                         return 0;
    3062                 :            :         }
    3063                 :            : 
    3064                 :            :         /*
    3065                 :            :          * grab_cache_page_write_begin() can take a long time if the
    3066                 :            :          * system is thrashing due to memory pressure, or if the page
    3067                 :            :          * is being written back.  So grab it first before we start
    3068                 :            :          * the transaction handle.  This also allows us to allocate
    3069                 :            :          * the page (if needed) without using GFP_NOFS.
    3070                 :            :          */
    3071                 :            : retry_grab:
    3072                 :     410845 :         page = grab_cache_page_write_begin(mapping, index, flags);
    3073         [ +  + ]:     410847 :         if (!page)
    3074                 :            :                 return -ENOMEM;
    3075                 :     410819 :         unlock_page(page);
    3076                 :            : 
    3077                 :            :         /*
    3078                 :            :          * With delayed allocation, we don't log the i_disksize update
    3079                 :            :          * if there is delayed block allocation. But we still need
    3080                 :            :          * to journalling the i_disksize update if writes to the end
    3081                 :            :          * of file which has an already mapped buffer.
    3082                 :            :          */
    3083                 :            : retry_journal:
    3084                 :            :         handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
    3085                 :            :                                 ext4_da_write_credits(inode, pos, len));
    3086         [ -  + ]:     410828 :         if (IS_ERR(handle)) {
    3087                 :          0 :                 put_page(page);
    3088                 :          0 :                 return PTR_ERR(handle);
    3089                 :            :         }
    3090                 :            : 
    3091                 :     410828 :         lock_page(page);
    3092         [ -  + ]:     410814 :         if (page->mapping != mapping) {
    3093                 :            :                 /* The page got truncated from under us */
    3094                 :          0 :                 unlock_page(page);
    3095                 :          0 :                 put_page(page);
    3096                 :          0 :                 ext4_journal_stop(handle);
    3097                 :          0 :                 goto retry_grab;
    3098                 :            :         }
    3099                 :            :         /* In case writeback began while the page was unlocked */
    3100                 :     410814 :         wait_for_stable_page(page);
    3101                 :            : 
    3102                 :            : #ifdef CONFIG_FS_ENCRYPTION
    3103                 :     410848 :         ret = ext4_block_write_begin(page, pos, len,
    3104                 :            :                                      ext4_da_get_block_prep);
    3105                 :            : #else
    3106                 :            :         ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
    3107                 :            : #endif
    3108         [ -  + ]:     410803 :         if (ret < 0) {
    3109                 :          0 :                 unlock_page(page);
    3110                 :          0 :                 ext4_journal_stop(handle);
    3111                 :            :                 /*
    3112                 :            :                  * block_write_begin may have instantiated a few blocks
    3113                 :            :                  * outside i_size.  Trim these off again. Don't need
    3114                 :            :                  * i_size_read because we hold i_mutex.
    3115                 :            :                  */
    3116         [ #  # ]:          0 :                 if (pos + len > inode->i_size)
    3117                 :          0 :                         ext4_truncate_failed_write(inode);
    3118                 :            : 
    3119   [ #  #  #  # ]:          0 :                 if (ret == -ENOSPC &&
    3120                 :          0 :                     ext4_should_retry_alloc(inode->i_sb, &retries))
    3121                 :            :                         goto retry_journal;
    3122                 :            : 
    3123                 :          0 :                 put_page(page);
    3124                 :          0 :                 return ret;
    3125                 :            :         }
    3126                 :            : 
    3127                 :     410803 :         *pagep = page;
    3128                 :     410803 :         return ret;
    3129                 :            : }
    3130                 :            : 
    3131                 :            : /*
    3132                 :            :  * Check if we should update i_disksize
    3133                 :            :  * when write to the end of file but not require block allocation
    3134                 :            :  */
    3135                 :     408493 : static int ext4_da_should_update_i_disksize(struct page *page,
    3136                 :            :                                             unsigned long offset)
    3137                 :            : {
    3138                 :            :         struct buffer_head *bh;
    3139                 :     408493 :         struct inode *inode = page->mapping->host;
    3140                 :            :         unsigned int idx;
    3141                 :            :         int i;
    3142                 :            : 
    3143         [ -  + ]:     408463 :         bh = page_buffers(page);
    3144                 :     408463 :         idx = offset >> inode->i_blkbits;
    3145                 :            : 
    3146         [ -  + ]:     408463 :         for (i = 0; i < idx; i++)
    3147                 :          0 :                 bh = bh->b_this_page;
    3148                 :            : 
    3149   [ +  +  +  +  :     840030 :         if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
                   -  + ]
    3150                 :            :                 return 0;
    3151                 :            :         return 1;
    3152                 :            : }
    3153                 :            : 
    3154                 :     410849 : static int ext4_da_write_end(struct file *file,
    3155                 :            :                              struct address_space *mapping,
    3156                 :            :                              loff_t pos, unsigned len, unsigned copied,
    3157                 :            :                              struct page *page, void *fsdata)
    3158                 :            : {
    3159                 :     410849 :         struct inode *inode = mapping->host;
    3160                 :            :         int ret = 0, ret2;
    3161                 :            :         handle_t *handle = ext4_journal_current_handle();
    3162                 :            :         loff_t new_i_size;
    3163                 :            :         unsigned long start, end;
    3164                 :     410849 :         int write_mode = (int)(unsigned long)fsdata;
    3165                 :            : 
    3166         [ -  + ]:     410849 :         if (write_mode == FALL_BACK_TO_NONDELALLOC)
    3167                 :          0 :                 return ext4_write_end(file, mapping, pos,
    3168                 :            :                                       len, copied, page, fsdata);
    3169                 :            : 
    3170                 :     410849 :         trace_ext4_da_write_end(inode, pos, len, copied);
    3171                 :     410848 :         start = pos & (PAGE_SIZE - 1);
    3172                 :     410848 :         end = start + copied - 1;
    3173                 :            : 
    3174                 :            :         /*
    3175                 :            :          * generic_write_end() will run mark_inode_dirty() if i_size
    3176                 :            :          * changes.  So let's piggyback the i_disksize mark_inode_dirty
    3177                 :            :          * into that.
    3178                 :            :          */
    3179                 :     410848 :         new_i_size = pos + copied;
    3180   [ +  +  +  + ]:     410848 :         if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
    3181   [ +  +  +  + ]:     817018 :                 if (ext4_has_inline_data(inode) ||
    3182                 :     408486 :                     ext4_da_should_update_i_disksize(page, end)) {
    3183                 :      23040 :                         ext4_update_i_disksize(inode, new_i_size);
    3184                 :            :                         /* We need to mark inode dirty even if
    3185                 :            :                          * new_i_size is less that inode->i_size
    3186                 :            :                          * bu greater than i_disksize.(hint delalloc)
    3187                 :            :                          */
    3188                 :      23023 :                         ext4_mark_inode_dirty(handle, inode);
    3189                 :            :                 }
    3190                 :            :         }
    3191                 :            : 
    3192   [ +  -  -  + ]:     821700 :         if (write_mode != CONVERT_INLINE_DATA &&
    3193         [ #  # ]:          0 :             ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
    3194                 :            :             ext4_has_inline_data(inode))
    3195                 :          0 :                 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
    3196                 :            :                                                      page);
    3197                 :            :         else
    3198                 :     410850 :                 ret2 = generic_write_end(file, mapping, pos, len, copied,
    3199                 :            :                                                         page, fsdata);
    3200                 :            : 
    3201                 :            :         copied = ret2;
    3202         [ -  + ]:     410844 :         if (ret2 < 0)
    3203                 :            :                 ret = ret2;
    3204                 :     410844 :         ret2 = ext4_journal_stop(handle);
    3205         [ +  - ]:     410851 :         if (!ret)
    3206                 :            :                 ret = ret2;
    3207                 :            : 
    3208         [ -  + ]:     410851 :         return ret ? ret : copied;
    3209                 :            : }
    3210                 :            : 
    3211                 :            : /*
    3212                 :            :  * Force all delayed allocation blocks to be allocated for a given inode.
    3213                 :            :  */
    3214                 :       1640 : int ext4_alloc_da_blocks(struct inode *inode)
    3215                 :            : {
    3216                 :       1640 :         trace_ext4_alloc_da_blocks(inode);
    3217                 :            : 
    3218         [ +  + ]:       1640 :         if (!EXT4_I(inode)->i_reserved_data_blocks)
    3219                 :            :                 return 0;
    3220                 :            : 
    3221                 :            :         /*
    3222                 :            :          * We do something simple for now.  The filemap_flush() will
    3223                 :            :          * also start triggering a write of the data blocks, which is
    3224                 :            :          * not strictly speaking necessary (and for users of
    3225                 :            :          * laptop_mode, not even desirable).  However, to do otherwise
    3226                 :            :          * would require replicating code paths in:
    3227                 :            :          *
    3228                 :            :          * ext4_writepages() ->
    3229                 :            :          *    write_cache_pages() ---> (via passed in callback function)
    3230                 :            :          *        __mpage_da_writepage() -->
    3231                 :            :          *           mpage_add_bh_to_extent()
    3232                 :            :          *           mpage_da_map_blocks()
    3233                 :            :          *
    3234                 :            :          * The problem is that write_cache_pages(), located in
    3235                 :            :          * mm/page-writeback.c, marks pages clean in preparation for
    3236                 :            :          * doing I/O, which is not desirable if we're not planning on
    3237                 :            :          * doing I/O at all.
    3238                 :            :          *
    3239                 :            :          * We could call write_cache_pages(), and then redirty all of
    3240                 :            :          * the pages by calling redirty_page_for_writepage() but that
    3241                 :            :          * would be ugly in the extreme.  So instead we would need to
    3242                 :            :          * replicate parts of the code in the above functions,
    3243                 :            :          * simplifying them because we wouldn't actually intend to
    3244                 :            :          * write out the pages, but rather only collect contiguous
    3245                 :            :          * logical block extents, call the multi-block allocator, and
    3246                 :            :          * then update the buffer heads with the block allocations.
    3247                 :            :          *
    3248                 :            :          * For now, though, we'll cheat by calling filemap_flush(),
    3249                 :            :          * which will map the blocks, and start the I/O, but not
    3250                 :            :          * actually wait for the I/O to complete.
    3251                 :            :          */
    3252                 :        732 :         return filemap_flush(inode->i_mapping);
    3253                 :            : }
    3254                 :            : 
    3255                 :            : /*
    3256                 :            :  * bmap() is special.  It gets used by applications such as lilo and by
    3257                 :            :  * the swapper to find the on-disk block of a specific piece of data.
    3258                 :            :  *
    3259                 :            :  * Naturally, this is dangerous if the block concerned is still in the
    3260                 :            :  * journal.  If somebody makes a swapfile on an ext4 data-journaling
    3261                 :            :  * filesystem and enables swap, then they may get a nasty shock when the
    3262                 :            :  * data getting swapped to that swapfile suddenly gets overwritten by
    3263                 :            :  * the original zero's written out previously to the journal and
    3264                 :            :  * awaiting writeback in the kernel's buffer cache.
    3265                 :            :  *
    3266                 :            :  * So, if we see any bmap calls here on a modified, data-journaled file,
    3267                 :            :  * take extra steps to flush any blocks which might be in the cache.
    3268                 :            :  */
    3269                 :    5243829 : static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
    3270                 :            : {
    3271                 :    5243829 :         struct inode *inode = mapping->host;
    3272                 :            :         journal_t *journal;
    3273                 :            :         int err;
    3274                 :            : 
    3275                 :            :         /*
    3276                 :            :          * We can get here for an inline file via the FIBMAP ioctl
    3277                 :            :          */
    3278         [ +  - ]:    5243829 :         if (ext4_has_inline_data(inode))
    3279                 :            :                 return 0;
    3280                 :            : 
    3281   [ -  +  #  # ]:    5243829 :         if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
    3282                 :          0 :                         test_opt(inode->i_sb, DELALLOC)) {
    3283                 :            :                 /*
    3284                 :            :                  * With delalloc we want to sync the file
    3285                 :            :                  * so that we can make sure we allocate
    3286                 :            :                  * blocks for file
    3287                 :            :                  */
    3288                 :          0 :                 filemap_write_and_wait(mapping);
    3289                 :            :         }
    3290                 :            : 
    3291   [ +  +  -  + ]:   15731280 :         if (EXT4_JOURNAL(inode) &&
    3292                 :            :             ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
    3293                 :            :                 /*
    3294                 :            :                  * This is a REALLY heavyweight approach, but the use of
    3295                 :            :                  * bmap on dirty files is expected to be extremely rare:
    3296                 :            :                  * only if we run lilo or swapon on a freshly made file
    3297                 :            :                  * do we expect this to happen.
    3298                 :            :                  *
    3299                 :            :                  * (bmap requires CAP_SYS_RAWIO so this does not
    3300                 :            :                  * represent an unprivileged user DOS attack --- we'd be
    3301                 :            :                  * in trouble if mortal users could trigger this path at
    3302                 :            :                  * will.)
    3303                 :            :                  *
    3304                 :            :                  * NB. EXT4_STATE_JDATA is not set on files other than
    3305                 :            :                  * regular files.  If somebody wants to bmap a directory
    3306                 :            :                  * or symlink and gets confused because the buffer
    3307                 :            :                  * hasn't yet been flushed to disk, they deserve
    3308                 :            :                  * everything they get.
    3309                 :            :                  */
    3310                 :            : 
    3311                 :            :                 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
    3312                 :          0 :                 journal = EXT4_JOURNAL(inode);
    3313                 :          0 :                 jbd2_journal_lock_updates(journal);
    3314                 :          0 :                 err = jbd2_journal_flush(journal);
    3315                 :          0 :                 jbd2_journal_unlock_updates(journal);
    3316                 :            : 
    3317         [ #  # ]:          0 :                 if (err)
    3318                 :            :                         return 0;
    3319                 :            :         }
    3320                 :            : 
    3321                 :    5243829 :         return generic_block_bmap(mapping, block, ext4_get_block);
    3322                 :            : }
    3323                 :            : 
    3324                 :        673 : static int ext4_readpage(struct file *file, struct page *page)
    3325                 :            : {
    3326                 :            :         int ret = -EAGAIN;
    3327                 :        673 :         struct inode *inode = page->mapping->host;
    3328                 :            : 
    3329                 :        673 :         trace_ext4_readpage(page);
    3330                 :            : 
    3331         [ -  + ]:        673 :         if (ext4_has_inline_data(inode))
    3332                 :          0 :                 ret = ext4_readpage_inline(inode, page);
    3333                 :            : 
    3334         [ +  - ]:        673 :         if (ret == -EAGAIN)
    3335                 :        673 :                 return ext4_mpage_readpages(page->mapping, NULL, page, 1,
    3336                 :            :                                                 false);
    3337                 :            : 
    3338                 :            :         return ret;
    3339                 :            : }
    3340                 :            : 
    3341                 :            : static int
    3342                 :     617737 : ext4_readpages(struct file *file, struct address_space *mapping,
    3343                 :            :                 struct list_head *pages, unsigned nr_pages)
    3344                 :            : {
    3345                 :     617737 :         struct inode *inode = mapping->host;
    3346                 :            : 
    3347                 :            :         /* If the file has inline data, no need to do readpages. */
    3348         [ +  + ]:     617737 :         if (ext4_has_inline_data(inode))
    3349                 :            :                 return 0;
    3350                 :            : 
    3351                 :     617970 :         return ext4_mpage_readpages(mapping, pages, NULL, nr_pages, true);
    3352                 :            : }
    3353                 :            : 
    3354                 :       1639 : static void ext4_invalidatepage(struct page *page, unsigned int offset,
    3355                 :            :                                 unsigned int length)
    3356                 :            : {
    3357                 :       1639 :         trace_ext4_invalidatepage(page, offset, length);
    3358                 :            : 
    3359                 :            :         /* No journalling happens on data buffers when this function is used */
    3360   [ +  -  -  +  :       4917 :         WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
             +  -  -  + ]
    3361                 :            : 
    3362                 :       1639 :         block_invalidatepage(page, offset, length);
    3363                 :       1639 : }
    3364                 :            : 
    3365                 :          0 : static int __ext4_journalled_invalidatepage(struct page *page,
    3366                 :            :                                             unsigned int offset,
    3367                 :            :                                             unsigned int length)
    3368                 :            : {
    3369                 :          0 :         journal_t *journal = EXT4_JOURNAL(page->mapping->host);
    3370                 :            : 
    3371                 :          0 :         trace_ext4_journalled_invalidatepage(page, offset, length);
    3372                 :            : 
    3373                 :            :         /*
    3374                 :            :          * If it's a full truncate we just forget about the pending dirtying
    3375                 :            :          */
    3376         [ #  # ]:          0 :         if (offset == 0 && length == PAGE_SIZE)
    3377                 :            :                 ClearPageChecked(page);
    3378                 :            : 
    3379                 :          0 :         return jbd2_journal_invalidatepage(journal, page, offset, length);
    3380                 :            : }
    3381                 :            : 
    3382                 :            : /* Wrapper for aops... */
    3383                 :          0 : static void ext4_journalled_invalidatepage(struct page *page,
    3384                 :            :                                            unsigned int offset,
    3385                 :            :                                            unsigned int length)
    3386                 :            : {
    3387         [ #  # ]:          0 :         WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
    3388                 :          0 : }
    3389                 :            : 
    3390                 :       1639 : static int ext4_releasepage(struct page *page, gfp_t wait)
    3391                 :            : {
    3392                 :       3278 :         journal_t *journal = EXT4_JOURNAL(page->mapping->host);
    3393                 :            : 
    3394                 :       1639 :         trace_ext4_releasepage(page);
    3395                 :            : 
    3396                 :            :         /* Page has dirty journalled data -> cannot release */
    3397         [ +  - ]:       1639 :         if (PageChecked(page))
    3398                 :            :                 return 0;
    3399         [ +  - ]:       1639 :         if (journal)
    3400                 :       1639 :                 return jbd2_journal_try_to_free_buffers(journal, page, wait);
    3401                 :            :         else
    3402                 :          0 :                 return try_to_free_buffers(page);
    3403                 :            : }
    3404                 :            : 
    3405                 :          0 : static bool ext4_inode_datasync_dirty(struct inode *inode)
    3406                 :            : {
    3407                 :          0 :         journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
    3408                 :            : 
    3409         [ #  # ]:          0 :         if (journal)
    3410                 :          0 :                 return !jbd2_transaction_committed(journal,
    3411                 :            :                                         EXT4_I(inode)->i_datasync_tid);
    3412                 :            :         /* Any metadata buffers to write? */
    3413         [ #  # ]:          0 :         if (!list_empty(&inode->i_mapping->private_list))
    3414                 :            :                 return true;
    3415                 :          0 :         return inode->i_state & I_DIRTY_DATASYNC;
    3416                 :            : }
    3417                 :            : 
    3418                 :          0 : static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
    3419                 :            :                             unsigned flags, struct iomap *iomap)
    3420                 :            : {
    3421                 :          0 :         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
    3422                 :          0 :         unsigned int blkbits = inode->i_blkbits;
    3423                 :            :         unsigned long first_block, last_block;
    3424                 :            :         struct ext4_map_blocks map;
    3425                 :            :         bool delalloc = false;
    3426                 :            :         int ret;
    3427                 :            : 
    3428         [ #  # ]:          0 :         if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
    3429                 :            :                 return -EINVAL;
    3430                 :          0 :         first_block = offset >> blkbits;
    3431                 :          0 :         last_block = min_t(loff_t, (offset + length - 1) >> blkbits,
    3432                 :            :                            EXT4_MAX_LOGICAL_BLOCK);
    3433                 :            : 
    3434         [ #  # ]:          0 :         if (flags & IOMAP_REPORT) {
    3435         [ #  # ]:          0 :                 if (ext4_has_inline_data(inode)) {
    3436                 :          0 :                         ret = ext4_inline_data_iomap(inode, iomap);
    3437         [ #  # ]:          0 :                         if (ret != -EAGAIN) {
    3438   [ #  #  #  # ]:          0 :                                 if (ret == 0 && offset >= iomap->length)
    3439                 :            :                                         ret = -ENOENT;
    3440                 :          0 :                                 return ret;
    3441                 :            :                         }
    3442                 :            :                 }
    3443                 :            :         } else {
    3444   [ #  #  #  #  :          0 :                 if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
                   #  # ]
    3445                 :            :                         return -ERANGE;
    3446                 :            :         }
    3447                 :            : 
    3448                 :          0 :         map.m_lblk = first_block;
    3449                 :          0 :         map.m_len = last_block - first_block + 1;
    3450                 :            : 
    3451         [ #  # ]:          0 :         if (flags & IOMAP_REPORT) {
    3452                 :          0 :                 ret = ext4_map_blocks(NULL, inode, &map, 0);
    3453         [ #  # ]:          0 :                 if (ret < 0)
    3454                 :            :                         return ret;
    3455                 :            : 
    3456         [ #  # ]:          0 :                 if (ret == 0) {
    3457                 :          0 :                         ext4_lblk_t end = map.m_lblk + map.m_len - 1;
    3458                 :            :                         struct extent_status es;
    3459                 :            : 
    3460                 :          0 :                         ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
    3461                 :            :                                                   map.m_lblk, end, &es);
    3462                 :            : 
    3463   [ #  #  #  # ]:          0 :                         if (!es.es_len || es.es_lblk > end) {
    3464                 :            :                                 /* entire range is a hole */
    3465         [ #  # ]:          0 :                         } else if (es.es_lblk > map.m_lblk) {
    3466                 :            :                                 /* range starts with a hole */
    3467                 :          0 :                                 map.m_len = es.es_lblk - map.m_lblk;
    3468                 :            :                         } else {
    3469                 :            :                                 ext4_lblk_t offs = 0;
    3470                 :            : 
    3471         [ #  # ]:          0 :                                 if (es.es_lblk < map.m_lblk)
    3472                 :          0 :                                         offs = map.m_lblk - es.es_lblk;
    3473                 :          0 :                                 map.m_lblk = es.es_lblk + offs;
    3474                 :          0 :                                 map.m_len = es.es_len - offs;
    3475                 :            :                                 delalloc = true;
    3476                 :            :                         }
    3477                 :            :                 }
    3478         [ #  # ]:          0 :         } else if (flags & IOMAP_WRITE) {
    3479                 :            :                 int dio_credits;
    3480                 :            :                 handle_t *handle;
    3481                 :          0 :                 int retries = 0;
    3482                 :            : 
    3483                 :            :                 /* Trim mapping request to maximum we can map at once for DIO */
    3484         [ #  # ]:          0 :                 if (map.m_len > DIO_MAX_BLOCKS)
    3485                 :          0 :                         map.m_len = DIO_MAX_BLOCKS;
    3486                 :          0 :                 dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
    3487                 :            : retry:
    3488                 :            :                 /*
    3489                 :            :                  * Either we allocate blocks and then we don't get unwritten
    3490                 :            :                  * extent so we have reserved enough credits, or the blocks
    3491                 :            :                  * are already allocated and unwritten and in that case
    3492                 :            :                  * extent conversion fits in the credits as well.
    3493                 :            :                  */
    3494                 :            :                 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
    3495                 :            :                                             dio_credits);
    3496         [ #  # ]:          0 :                 if (IS_ERR(handle))
    3497                 :          0 :                         return PTR_ERR(handle);
    3498                 :            : 
    3499                 :          0 :                 ret = ext4_map_blocks(handle, inode, &map,
    3500                 :            :                                       EXT4_GET_BLOCKS_CREATE_ZERO);
    3501         [ #  # ]:          0 :                 if (ret < 0) {
    3502                 :          0 :                         ext4_journal_stop(handle);
    3503   [ #  #  #  # ]:          0 :                         if (ret == -ENOSPC &&
    3504                 :          0 :                             ext4_should_retry_alloc(inode->i_sb, &retries))
    3505                 :            :                                 goto retry;
    3506                 :          0 :                         return ret;
    3507                 :            :                 }
    3508                 :            : 
    3509                 :            :                 /*
    3510                 :            :                  * If we added blocks beyond i_size, we need to make sure they
    3511                 :            :                  * will get truncated if we crash before updating i_size in
    3512                 :            :                  * ext4_iomap_end(). For faults we don't need to do that (and
    3513                 :            :                  * even cannot because for orphan list operations inode_lock is
    3514                 :            :                  * required) - if we happen to instantiate block beyond i_size,
    3515                 :            :                  * it is because we race with truncate which has already added
    3516                 :            :                  * the inode to the orphan list.
    3517                 :            :                  */
    3518   [ #  #  #  # ]:          0 :                 if (!(flags & IOMAP_FAULT) && first_block + map.m_len >
    3519                 :          0 :                     (i_size_read(inode) + (1 << blkbits) - 1) >> blkbits) {
    3520                 :            :                         int err;
    3521                 :            : 
    3522                 :          0 :                         err = ext4_orphan_add(handle, inode);
    3523         [ #  # ]:          0 :                         if (err < 0) {
    3524                 :          0 :                                 ext4_journal_stop(handle);
    3525                 :          0 :                                 return err;
    3526                 :            :                         }
    3527                 :            :                 }
    3528                 :          0 :                 ext4_journal_stop(handle);
    3529                 :            :         } else {
    3530                 :          0 :                 ret = ext4_map_blocks(NULL, inode, &map, 0);
    3531         [ #  # ]:          0 :                 if (ret < 0)
    3532                 :            :                         return ret;
    3533                 :            :         }
    3534                 :            : 
    3535                 :            :         /*
    3536                 :            :          * Writes that span EOF might trigger an I/O size update on completion,
    3537                 :            :          * so consider them to be dirty for the purposes of O_DSYNC, even if
    3538                 :            :          * there is no other metadata changes being made or are pending here.
    3539                 :            :          */
    3540                 :          0 :         iomap->flags = 0;
    3541   [ #  #  #  # ]:          0 :         if (ext4_inode_datasync_dirty(inode) ||
    3542                 :            :             offset + length > i_size_read(inode))
    3543                 :          0 :                 iomap->flags |= IOMAP_F_DIRTY;
    3544                 :          0 :         iomap->bdev = inode->i_sb->s_bdev;
    3545                 :          0 :         iomap->dax_dev = sbi->s_daxdev;
    3546                 :          0 :         iomap->offset = (u64)first_block << blkbits;
    3547                 :          0 :         iomap->length = (u64)map.m_len << blkbits;
    3548                 :            : 
    3549         [ #  # ]:          0 :         if (ret == 0) {
    3550         [ #  # ]:          0 :                 iomap->type = delalloc ? IOMAP_DELALLOC : IOMAP_HOLE;
    3551                 :          0 :                 iomap->addr = IOMAP_NULL_ADDR;
    3552                 :            :         } else {
    3553         [ #  # ]:          0 :                 if (map.m_flags & EXT4_MAP_MAPPED) {
    3554                 :          0 :                         iomap->type = IOMAP_MAPPED;
    3555         [ #  # ]:          0 :                 } else if (map.m_flags & EXT4_MAP_UNWRITTEN) {
    3556                 :          0 :                         iomap->type = IOMAP_UNWRITTEN;
    3557                 :            :                 } else {
    3558         [ #  # ]:          0 :                         WARN_ON_ONCE(1);
    3559                 :            :                         return -EIO;
    3560                 :            :                 }
    3561                 :          0 :                 iomap->addr = (u64)map.m_pblk << blkbits;
    3562                 :            :         }
    3563                 :            : 
    3564         [ #  # ]:          0 :         if (map.m_flags & EXT4_MAP_NEW)
    3565                 :          0 :                 iomap->flags |= IOMAP_F_NEW;
    3566                 :            : 
    3567                 :            :         return 0;
    3568                 :            : }
    3569                 :            : 
    3570                 :          0 : static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
    3571                 :            :                           ssize_t written, unsigned flags, struct iomap *iomap)
    3572                 :            : {
    3573                 :            :         int ret = 0;
    3574                 :            :         handle_t *handle;
    3575                 :          0 :         int blkbits = inode->i_blkbits;
    3576                 :            :         bool truncate = false;
    3577                 :            : 
    3578         [ #  # ]:          0 :         if (!(flags & IOMAP_WRITE) || (flags & IOMAP_FAULT))
    3579                 :            :                 return 0;
    3580                 :            : 
    3581                 :            :         handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
    3582         [ #  # ]:          0 :         if (IS_ERR(handle)) {
    3583                 :            :                 ret = PTR_ERR(handle);
    3584                 :          0 :                 goto orphan_del;
    3585                 :            :         }
    3586         [ #  # ]:          0 :         if (ext4_update_inode_size(inode, offset + written))
    3587                 :          0 :                 ext4_mark_inode_dirty(handle, inode);
    3588                 :            :         /*
    3589                 :            :          * We may need to truncate allocated but not written blocks beyond EOF.
    3590                 :            :          */
    3591         [ #  # ]:          0 :         if (iomap->offset + iomap->length > 
    3592                 :          0 :             ALIGN(inode->i_size, 1 << blkbits)) {
    3593                 :            :                 ext4_lblk_t written_blk, end_blk;
    3594                 :            : 
    3595                 :          0 :                 written_blk = (offset + written) >> blkbits;
    3596                 :          0 :                 end_blk = (offset + length) >> blkbits;
    3597   [ #  #  #  # ]:          0 :                 if (written_blk < end_blk && ext4_can_truncate(inode))
    3598                 :            :                         truncate = true;
    3599                 :            :         }
    3600                 :            :         /*
    3601                 :            :          * Remove inode from orphan list if we were extending a inode and
    3602                 :            :          * everything went fine.
    3603                 :            :          */
    3604   [ #  #  #  #  :          0 :         if (!truncate && inode->i_nlink &&
                   #  # ]
    3605                 :          0 :             !list_empty(&EXT4_I(inode)->i_orphan))
    3606                 :          0 :                 ext4_orphan_del(handle, inode);
    3607                 :          0 :         ext4_journal_stop(handle);
    3608         [ #  # ]:          0 :         if (truncate) {
    3609                 :          0 :                 ext4_truncate_failed_write(inode);
    3610                 :            : orphan_del:
    3611                 :            :                 /*
    3612                 :            :                  * If truncate failed early the inode might still be on the
    3613                 :            :                  * orphan list; we need to make sure the inode is removed from
    3614                 :            :                  * the orphan list in that case.
    3615                 :            :                  */
    3616         [ #  # ]:          0 :                 if (inode->i_nlink)
    3617                 :          0 :                         ext4_orphan_del(NULL, inode);
    3618                 :            :         }
    3619                 :          0 :         return ret;
    3620                 :            : }
    3621                 :            : 
    3622                 :            : const struct iomap_ops ext4_iomap_ops = {
    3623                 :            :         .iomap_begin            = ext4_iomap_begin,
    3624                 :            :         .iomap_end              = ext4_iomap_end,
    3625                 :            : };
    3626                 :            : 
    3627                 :          0 : static int ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
    3628                 :            :                             ssize_t size, void *private)
    3629                 :            : {
    3630                 :            :         ext4_io_end_t *io_end = private;
    3631                 :            : 
    3632                 :            :         /* if not async direct IO just return */
    3633         [ #  # ]:          0 :         if (!io_end)
    3634                 :            :                 return 0;
    3635                 :            : 
    3636                 :            :         ext_debug("ext4_end_io_dio(): io_end 0x%p "
    3637                 :            :                   "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
    3638                 :            :                   io_end, io_end->inode->i_ino, iocb, offset, size);
    3639                 :            : 
    3640                 :            :         /*
    3641                 :            :          * Error during AIO DIO. We cannot convert unwritten extents as the
    3642                 :            :          * data was not written. Just clear the unwritten flag and drop io_end.
    3643                 :            :          */
    3644         [ #  # ]:          0 :         if (size <= 0) {
    3645                 :          0 :                 ext4_clear_io_unwritten_flag(io_end);
    3646                 :            :                 size = 0;
    3647                 :            :         }
    3648                 :          0 :         io_end->offset = offset;
    3649                 :          0 :         io_end->size = size;
    3650                 :          0 :         ext4_put_io_end(io_end);
    3651                 :            : 
    3652                 :          0 :         return 0;
    3653                 :            : }
    3654                 :            : 
    3655                 :            : /*
    3656                 :            :  * Handling of direct IO writes.
    3657                 :            :  *
    3658                 :            :  * For ext4 extent files, ext4 will do direct-io write even to holes,
    3659                 :            :  * preallocated extents, and those write extend the file, no need to
    3660                 :            :  * fall back to buffered IO.
    3661                 :            :  *
    3662                 :            :  * For holes, we fallocate those blocks, mark them as unwritten
    3663                 :            :  * If those blocks were preallocated, we mark sure they are split, but
    3664                 :            :  * still keep the range to write as unwritten.
    3665                 :            :  *
    3666                 :            :  * The unwritten extents will be converted to written when DIO is completed.
    3667                 :            :  * For async direct IO, since the IO may still pending when return, we
    3668                 :            :  * set up an end_io call back function, which will do the conversion
    3669                 :            :  * when async direct IO completed.
    3670                 :            :  *
    3671                 :            :  * If the O_DIRECT write will extend the file then add this inode to the
    3672                 :            :  * orphan list.  So recovery will truncate it back to the original size
    3673                 :            :  * if the machine crashes during the write.
    3674                 :            :  *
    3675                 :            :  */
    3676                 :          0 : static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
    3677                 :            : {
    3678                 :          0 :         struct file *file = iocb->ki_filp;
    3679                 :          0 :         struct inode *inode = file->f_mapping->host;
    3680                 :            :         struct ext4_inode_info *ei = EXT4_I(inode);
    3681                 :            :         ssize_t ret;
    3682                 :          0 :         loff_t offset = iocb->ki_pos;
    3683                 :            :         size_t count = iov_iter_count(iter);
    3684                 :            :         int overwrite = 0;
    3685                 :            :         get_block_t *get_block_func = NULL;
    3686                 :            :         int dio_flags = 0;
    3687                 :          0 :         loff_t final_size = offset + count;
    3688                 :            :         int orphan = 0;
    3689                 :            :         handle_t *handle;
    3690                 :            : 
    3691   [ #  #  #  # ]:          0 :         if (final_size > inode->i_size || final_size > ei->i_disksize) {
    3692                 :            :                 /* Credits for sb + inode write */
    3693                 :            :                 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
    3694         [ #  # ]:          0 :                 if (IS_ERR(handle)) {
    3695                 :            :                         ret = PTR_ERR(handle);
    3696                 :          0 :                         goto out;
    3697                 :            :                 }
    3698                 :          0 :                 ret = ext4_orphan_add(handle, inode);
    3699         [ #  # ]:          0 :                 if (ret) {
    3700                 :          0 :                         ext4_journal_stop(handle);
    3701                 :          0 :                         goto out;
    3702                 :            :                 }
    3703                 :            :                 orphan = 1;
    3704                 :          0 :                 ext4_update_i_disksize(inode, inode->i_size);
    3705                 :          0 :                 ext4_journal_stop(handle);
    3706                 :            :         }
    3707                 :            : 
    3708         [ #  # ]:          0 :         BUG_ON(iocb->private == NULL);
    3709                 :            : 
    3710                 :            :         /*
    3711                 :            :          * Make all waiters for direct IO properly wait also for extent
    3712                 :            :          * conversion. This also disallows race between truncate() and
    3713                 :            :          * overwrite DIO as i_dio_count needs to be incremented under i_mutex.
    3714                 :            :          */
    3715                 :            :         inode_dio_begin(inode);
    3716                 :            : 
    3717                 :            :         /* If we do a overwrite dio, i_mutex locking can be released */
    3718                 :          0 :         overwrite = *((int *)iocb->private);
    3719                 :            : 
    3720         [ #  # ]:          0 :         if (overwrite)
    3721                 :            :                 inode_unlock(inode);
    3722                 :            : 
    3723                 :            :         /*
    3724                 :            :          * For extent mapped files we could direct write to holes and fallocate.
    3725                 :            :          *
    3726                 :            :          * Allocated blocks to fill the hole are marked as unwritten to prevent
    3727                 :            :          * parallel buffered read to expose the stale data before DIO complete
    3728                 :            :          * the data IO.
    3729                 :            :          *
    3730                 :            :          * As to previously fallocated extents, ext4 get_block will just simply
    3731                 :            :          * mark the buffer mapped but still keep the extents unwritten.
    3732                 :            :          *
    3733                 :            :          * For non AIO case, we will convert those unwritten extents to written
    3734                 :            :          * after return back from blockdev_direct_IO. That way we save us from
    3735                 :            :          * allocating io_end structure and also the overhead of offloading
    3736                 :            :          * the extent convertion to a workqueue.
    3737                 :            :          *
    3738                 :            :          * For async DIO, the conversion needs to be deferred when the
    3739                 :            :          * IO is completed. The ext4 end_io callback function will be
    3740                 :            :          * called to take care of the conversion work.  Here for async
    3741                 :            :          * case, we allocate an io_end structure to hook to the iocb.
    3742                 :            :          */
    3743                 :          0 :         iocb->private = NULL;
    3744         [ #  # ]:          0 :         if (overwrite)
    3745                 :            :                 get_block_func = ext4_dio_get_block_overwrite;
    3746   [ #  #  #  # ]:          0 :         else if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) ||
    3747                 :          0 :                    round_down(offset, i_blocksize(inode)) >= inode->i_size) {
    3748                 :            :                 get_block_func = ext4_dio_get_block;
    3749                 :            :                 dio_flags = DIO_LOCKING | DIO_SKIP_HOLES;
    3750         [ #  # ]:          0 :         } else if (is_sync_kiocb(iocb)) {
    3751                 :            :                 get_block_func = ext4_dio_get_block_unwritten_sync;
    3752                 :            :                 dio_flags = DIO_LOCKING;
    3753                 :            :         } else {
    3754                 :            :                 get_block_func = ext4_dio_get_block_unwritten_async;
    3755                 :            :                 dio_flags = DIO_LOCKING;
    3756                 :            :         }
    3757                 :          0 :         ret = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
    3758                 :            :                                    get_block_func, ext4_end_io_dio, NULL,
    3759                 :            :                                    dio_flags);
    3760                 :            : 
    3761   [ #  #  #  # ]:          0 :         if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
    3762                 :            :                                                 EXT4_STATE_DIO_UNWRITTEN)) {
    3763                 :            :                 int err;
    3764                 :            :                 /*
    3765                 :            :                  * for non AIO case, since the IO is already
    3766                 :            :                  * completed, we could do the conversion right here
    3767                 :            :                  */
    3768                 :          0 :                 err = ext4_convert_unwritten_extents(NULL, inode,
    3769                 :            :                                                      offset, ret);
    3770         [ #  # ]:          0 :                 if (err < 0)
    3771                 :            :                         ret = err;
    3772                 :            :                 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
    3773                 :            :         }
    3774                 :            : 
    3775                 :          0 :         inode_dio_end(inode);
    3776                 :            :         /* take i_mutex locking again if we do a ovewrite dio */
    3777         [ #  # ]:          0 :         if (overwrite)
    3778                 :            :                 inode_lock(inode);
    3779                 :            : 
    3780   [ #  #  #  # ]:          0 :         if (ret < 0 && final_size > inode->i_size)
    3781                 :          0 :                 ext4_truncate_failed_write(inode);
    3782                 :            : 
    3783                 :            :         /* Handle extending of i_size after direct IO write */
    3784         [ #  # ]:          0 :         if (orphan) {
    3785                 :            :                 int err;
    3786                 :            : 
    3787                 :            :                 /* Credits for sb + inode write */
    3788                 :            :                 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
    3789         [ #  # ]:          0 :                 if (IS_ERR(handle)) {
    3790                 :            :                         /*
    3791                 :            :                          * We wrote the data but cannot extend
    3792                 :            :                          * i_size. Bail out. In async io case, we do
    3793                 :            :                          * not return error here because we have
    3794                 :            :                          * already submmitted the corresponding
    3795                 :            :                          * bio. Returning error here makes the caller
    3796                 :            :                          * think that this IO is done and failed
    3797                 :            :                          * resulting in race with bio's completion
    3798                 :            :                          * handler.
    3799                 :            :                          */
    3800         [ #  # ]:          0 :                         if (!ret)
    3801                 :            :                                 ret = PTR_ERR(handle);
    3802         [ #  # ]:          0 :                         if (inode->i_nlink)
    3803                 :          0 :                                 ext4_orphan_del(NULL, inode);
    3804                 :            : 
    3805                 :            :                         goto out;
    3806                 :            :                 }
    3807         [ #  # ]:          0 :                 if (inode->i_nlink)
    3808                 :          0 :                         ext4_orphan_del(handle, inode);
    3809         [ #  # ]:          0 :                 if (ret > 0) {
    3810                 :          0 :                         loff_t end = offset + ret;
    3811   [ #  #  #  # ]:          0 :                         if (end > inode->i_size || end > ei->i_disksize) {
    3812                 :          0 :                                 ext4_update_i_disksize(inode, end);
    3813         [ #  # ]:          0 :                                 if (end > inode->i_size)
    3814                 :            :                                         i_size_write(inode, end);
    3815                 :            :                                 /*
    3816                 :            :                                  * We're going to return a positive `ret'
    3817                 :            :                                  * here due to non-zero-length I/O, so there's
    3818                 :            :                                  * no way of reporting error returns from
    3819                 :            :                                  * ext4_mark_inode_dirty() to userspace.  So
    3820                 :            :                                  * ignore it.
    3821                 :            :                                  */
    3822                 :          0 :                                 ext4_mark_inode_dirty(handle, inode);
    3823                 :            :                         }
    3824                 :            :                 }
    3825                 :          0 :                 err = ext4_journal_stop(handle);
    3826         [ #  # ]:          0 :                 if (ret == 0)
    3827                 :            :                         ret = err;
    3828                 :            :         }
    3829                 :            : out:
    3830                 :          0 :         return ret;
    3831                 :            : }
    3832                 :            : 
    3833                 :          0 : static ssize_t ext4_direct_IO_read(struct kiocb *iocb, struct iov_iter *iter)
    3834                 :            : {
    3835                 :          0 :         struct address_space *mapping = iocb->ki_filp->f_mapping;
    3836                 :          0 :         struct inode *inode = mapping->host;
    3837                 :            :         size_t count = iov_iter_count(iter);
    3838                 :            :         ssize_t ret;
    3839                 :          0 :         loff_t offset = iocb->ki_pos;
    3840                 :            :         loff_t size = i_size_read(inode);
    3841                 :            : 
    3842         [ #  # ]:          0 :         if (offset >= size)
    3843                 :            :                 return 0;
    3844                 :            : 
    3845                 :            :         /*
    3846                 :            :          * Shared inode_lock is enough for us - it protects against concurrent
    3847                 :            :          * writes & truncates and since we take care of writing back page cache,
    3848                 :            :          * we are protected against page writeback as well.
    3849                 :            :          */
    3850         [ #  # ]:          0 :         if (iocb->ki_flags & IOCB_NOWAIT) {
    3851         [ #  # ]:          0 :                 if (!inode_trylock_shared(inode))
    3852                 :            :                         return -EAGAIN;
    3853                 :            :         } else {
    3854                 :            :                 inode_lock_shared(inode);
    3855                 :            :         }
    3856                 :            : 
    3857                 :          0 :         ret = filemap_write_and_wait_range(mapping, iocb->ki_pos,
    3858                 :          0 :                                            iocb->ki_pos + count - 1);
    3859         [ #  # ]:          0 :         if (ret)
    3860                 :            :                 goto out_unlock;
    3861                 :          0 :         ret = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
    3862                 :            :                                    iter, ext4_dio_get_block, NULL, NULL, 0);
    3863                 :            : out_unlock:
    3864                 :            :         inode_unlock_shared(inode);
    3865                 :          0 :         return ret;
    3866                 :            : }
    3867                 :            : 
    3868                 :          0 : static ssize_t ext4_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
    3869                 :            : {
    3870                 :          0 :         struct file *file = iocb->ki_filp;
    3871                 :          0 :         struct inode *inode = file->f_mapping->host;
    3872                 :            :         size_t count = iov_iter_count(iter);
    3873                 :          0 :         loff_t offset = iocb->ki_pos;
    3874                 :            :         ssize_t ret;
    3875                 :            : 
    3876                 :            : #ifdef CONFIG_FS_ENCRYPTION
    3877   [ #  #  #  # ]:          0 :         if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode))
    3878                 :            :                 return 0;
    3879                 :            : #endif
    3880                 :            :         if (fsverity_active(inode))
    3881                 :            :                 return 0;
    3882                 :            : 
    3883                 :            :         /*
    3884                 :            :          * If we are doing data journalling we don't support O_DIRECT
    3885                 :            :          */
    3886         [ #  # ]:          0 :         if (ext4_should_journal_data(inode))
    3887                 :            :                 return 0;
    3888                 :            : 
    3889                 :            :         /* Let buffer I/O handle the inline data case. */
    3890         [ #  # ]:          0 :         if (ext4_has_inline_data(inode))
    3891                 :            :                 return 0;
    3892                 :            : 
    3893                 :          0 :         trace_ext4_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
    3894         [ #  # ]:          0 :         if (iov_iter_rw(iter) == READ)
    3895                 :          0 :                 ret = ext4_direct_IO_read(iocb, iter);
    3896                 :            :         else
    3897                 :          0 :                 ret = ext4_direct_IO_write(iocb, iter);
    3898                 :          0 :         trace_ext4_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), ret);
    3899                 :          0 :         return ret;
    3900                 :            : }
    3901                 :            : 
    3902                 :            : /*
    3903                 :            :  * Pages can be marked dirty completely asynchronously from ext4's journalling
    3904                 :            :  * activity.  By filemap_sync_pte(), try_to_unmap_one(), etc.  We cannot do
    3905                 :            :  * much here because ->set_page_dirty is called under VFS locks.  The page is
    3906                 :            :  * not necessarily locked.
    3907                 :            :  *
    3908                 :            :  * We cannot just dirty the page and leave attached buffers clean, because the
    3909                 :            :  * buffers' dirty state is "definitive".  We cannot just set the buffers dirty
    3910                 :            :  * or jbddirty because all the journalling code will explode.
    3911                 :            :  *
    3912                 :            :  * So what we do is to mark the page "pending dirty" and next time writepage
    3913                 :            :  * is called, propagate that into the buffers appropriately.
    3914                 :            :  */
    3915                 :          0 : static int ext4_journalled_set_page_dirty(struct page *page)
    3916                 :            : {
    3917                 :            :         SetPageChecked(page);
    3918                 :          0 :         return __set_page_dirty_nobuffers(page);
    3919                 :            : }
    3920                 :            : 
    3921                 :       3270 : static int ext4_set_page_dirty(struct page *page)
    3922                 :            : {
    3923   [ +  +  +  -  :       4220 :         WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page));
             -  +  #  # ]
    3924   [ -  +  #  # ]:       3270 :         WARN_ON_ONCE(!page_has_buffers(page));
    3925                 :       3270 :         return __set_page_dirty_buffers(page);
    3926                 :            : }
    3927                 :            : 
    3928                 :            : static const struct address_space_operations ext4_aops = {
    3929                 :            :         .readpage               = ext4_readpage,
    3930                 :            :         .readpages              = ext4_readpages,
    3931                 :            :         .writepage              = ext4_writepage,
    3932                 :            :         .writepages             = ext4_writepages,
    3933                 :            :         .write_begin            = ext4_write_begin,
    3934                 :            :         .write_end              = ext4_write_end,
    3935                 :            :         .set_page_dirty         = ext4_set_page_dirty,
    3936                 :            :         .bmap                   = ext4_bmap,
    3937                 :            :         .invalidatepage         = ext4_invalidatepage,
    3938                 :            :         .releasepage            = ext4_releasepage,
    3939                 :            :         .direct_IO              = ext4_direct_IO,
    3940                 :            :         .migratepage            = buffer_migrate_page,
    3941                 :            :         .is_partially_uptodate  = block_is_partially_uptodate,
    3942                 :            :         .error_remove_page      = generic_error_remove_page,
    3943                 :            : };
    3944                 :            : 
    3945                 :            : static const struct address_space_operations ext4_journalled_aops = {
    3946                 :            :         .readpage               = ext4_readpage,
    3947                 :            :         .readpages              = ext4_readpages,
    3948                 :            :         .writepage              = ext4_writepage,
    3949                 :            :         .writepages             = ext4_writepages,
    3950                 :            :         .write_begin            = ext4_write_begin,
    3951                 :            :         .write_end              = ext4_journalled_write_end,
    3952                 :            :         .set_page_dirty         = ext4_journalled_set_page_dirty,
    3953                 :            :         .bmap                   = ext4_bmap,
    3954                 :            :         .invalidatepage         = ext4_journalled_invalidatepage,
    3955                 :            :         .releasepage            = ext4_releasepage,
    3956                 :            :         .direct_IO              = ext4_direct_IO,
    3957                 :            :         .is_partially_uptodate  = block_is_partially_uptodate,
    3958                 :            :         .error_remove_page      = generic_error_remove_page,
    3959                 :            : };
    3960                 :            : 
    3961                 :            : static const struct address_space_operations ext4_da_aops = {
    3962                 :            :         .readpage               = ext4_readpage,
    3963                 :            :         .readpages              = ext4_readpages,
    3964                 :            :         .writepage              = ext4_writepage,
    3965                 :            :         .writepages             = ext4_writepages,
    3966                 :            :         .write_begin            = ext4_da_write_begin,
    3967                 :            :         .write_end              = ext4_da_write_end,
    3968                 :            :         .set_page_dirty         = ext4_set_page_dirty,
    3969                 :            :         .bmap                   = ext4_bmap,
    3970                 :            :         .invalidatepage         = ext4_invalidatepage,
    3971                 :            :         .releasepage            = ext4_releasepage,
    3972                 :            :         .direct_IO              = ext4_direct_IO,
    3973                 :            :         .migratepage            = buffer_migrate_page,
    3974                 :            :         .is_partially_uptodate  = block_is_partially_uptodate,
    3975                 :            :         .error_remove_page      = generic_error_remove_page,
    3976                 :            : };
    3977                 :            : 
    3978                 :            : static const struct address_space_operations ext4_dax_aops = {
    3979                 :            :         .writepages             = ext4_dax_writepages,
    3980                 :            :         .direct_IO              = noop_direct_IO,
    3981                 :            :         .set_page_dirty         = noop_set_page_dirty,
    3982                 :            :         .bmap                   = ext4_bmap,
    3983                 :            :         .invalidatepage         = noop_invalidatepage,
    3984                 :            : };
    3985                 :            : 
    3986                 :     449903 : void ext4_set_aops(struct inode *inode)
    3987                 :            : {
    3988      [ +  -  + ]:     449903 :         switch (ext4_inode_journal_mode(inode)) {
    3989                 :            :         case EXT4_INODE_ORDERED_DATA_MODE:
    3990                 :            :         case EXT4_INODE_WRITEBACK_DATA_MODE:
    3991                 :            :                 break;
    3992                 :            :         case EXT4_INODE_JOURNAL_DATA_MODE:
    3993                 :        209 :                 inode->i_mapping->a_ops = &ext4_journalled_aops;
    3994                 :     450408 :                 return;
    3995                 :            :         default:
    3996                 :          0 :                 BUG();
    3997                 :            :         }
    3998                 :            :         if (IS_DAX(inode))
    3999                 :            :                 inode->i_mapping->a_ops = &ext4_dax_aops;
    4000         [ +  - ]:     899980 :         else if (test_opt(inode->i_sb, DELALLOC))
    4001                 :     449990 :                 inode->i_mapping->a_ops = &ext4_da_aops;
    4002                 :            :         else
    4003                 :          0 :                 inode->i_mapping->a_ops = &ext4_aops;
    4004                 :            : }
    4005                 :            : 
    4006                 :          0 : static int __ext4_block_zero_page_range(handle_t *handle,
    4007                 :            :                 struct address_space *mapping, loff_t from, loff_t length)
    4008                 :            : {
    4009                 :          0 :         ext4_fsblk_t index = from >> PAGE_SHIFT;
    4010                 :          0 :         unsigned offset = from & (PAGE_SIZE-1);
    4011                 :            :         unsigned blocksize, pos;
    4012                 :            :         ext4_lblk_t iblock;
    4013                 :          0 :         struct inode *inode = mapping->host;
    4014                 :            :         struct buffer_head *bh;
    4015                 :            :         struct page *page;
    4016                 :            :         int err = 0;
    4017                 :            : 
    4018                 :          0 :         page = find_or_create_page(mapping, from >> PAGE_SHIFT,
    4019                 :            :                                    mapping_gfp_constraint(mapping, ~__GFP_FS));
    4020         [ #  # ]:          0 :         if (!page)
    4021                 :            :                 return -ENOMEM;
    4022                 :            : 
    4023                 :          0 :         blocksize = inode->i_sb->s_blocksize;
    4024                 :            : 
    4025                 :          0 :         iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
    4026                 :            : 
    4027         [ #  # ]:          0 :         if (!page_has_buffers(page))
    4028                 :          0 :                 create_empty_buffers(page, blocksize, 0);
    4029                 :            : 
    4030                 :            :         /* Find the buffer that contains "offset" */
    4031         [ #  # ]:          0 :         bh = page_buffers(page);
    4032                 :            :         pos = blocksize;
    4033         [ #  # ]:          0 :         while (offset >= pos) {
    4034                 :          0 :                 bh = bh->b_this_page;
    4035                 :          0 :                 iblock++;
    4036                 :          0 :                 pos += blocksize;
    4037                 :            :         }
    4038         [ #  # ]:          0 :         if (buffer_freed(bh)) {
    4039                 :            :                 BUFFER_TRACE(bh, "freed: skip");
    4040                 :            :                 goto unlock;
    4041                 :            :         }
    4042         [ #  # ]:          0 :         if (!buffer_mapped(bh)) {
    4043                 :            :                 BUFFER_TRACE(bh, "unmapped");
    4044                 :          0 :                 ext4_get_block(inode, iblock, bh, 0);
    4045                 :            :                 /* unmapped? It's a hole - nothing to do */
    4046         [ #  # ]:          0 :                 if (!buffer_mapped(bh)) {
    4047                 :            :                         BUFFER_TRACE(bh, "still unmapped");
    4048                 :            :                         goto unlock;
    4049                 :            :                 }
    4050                 :            :         }
    4051                 :            : 
    4052                 :            :         /* Ok, it's mapped. Make sure it's up-to-date */
    4053         [ #  # ]:          0 :         if (PageUptodate(page))
    4054                 :          0 :                 set_buffer_uptodate(bh);
    4055                 :            : 
    4056         [ #  # ]:          0 :         if (!buffer_uptodate(bh)) {
    4057                 :            :                 err = -EIO;
    4058                 :          0 :                 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
    4059                 :          0 :                 wait_on_buffer(bh);
    4060                 :            :                 /* Uhhuh. Read error. Complain and punt. */
    4061         [ #  # ]:          0 :                 if (!buffer_uptodate(bh))
    4062                 :            :                         goto unlock;
    4063   [ #  #  #  # ]:          0 :                 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
    4064                 :            :                         /* We expect the key to be set. */
    4065         [ #  # ]:          0 :                         BUG_ON(!fscrypt_has_encryption_key(inode));
    4066   [ #  #  #  # ]:          0 :                         WARN_ON_ONCE(fscrypt_decrypt_pagecache_blocks(
    4067                 :            :                                         page, blocksize, bh_offset(bh)));
    4068                 :            :                 }
    4069                 :            :         }
    4070         [ #  # ]:          0 :         if (ext4_should_journal_data(inode)) {
    4071                 :            :                 BUFFER_TRACE(bh, "get write access");
    4072                 :          0 :                 err = ext4_journal_get_write_access(handle, bh);
    4073         [ #  # ]:          0 :                 if (err)
    4074                 :            :                         goto unlock;
    4075                 :            :         }
    4076                 :          0 :         zero_user(page, offset, length);
    4077                 :            :         BUFFER_TRACE(bh, "zeroed end of block");
    4078                 :            : 
    4079         [ #  # ]:          0 :         if (ext4_should_journal_data(inode)) {
    4080                 :          0 :                 err = ext4_handle_dirty_metadata(handle, inode, bh);
    4081                 :            :         } else {
    4082                 :            :                 err = 0;
    4083                 :          0 :                 mark_buffer_dirty(bh);
    4084         [ #  # ]:          0 :                 if (ext4_should_order_data(inode))
    4085                 :            :                         err = ext4_jbd2_inode_add_write(handle, inode, from,
    4086                 :            :                                         length);
    4087                 :            :         }
    4088                 :            : 
    4089                 :            : unlock:
    4090                 :          0 :         unlock_page(page);
    4091                 :          0 :         put_page(page);
    4092                 :          0 :         return err;
    4093                 :            : }
    4094                 :            : 
    4095                 :            : /*
    4096                 :            :  * ext4_block_zero_page_range() zeros out a mapping of length 'length'
    4097                 :            :  * starting from file offset 'from'.  The range to be zero'd must
    4098                 :            :  * be contained with in one block.  If the specified range exceeds
    4099                 :            :  * the end of the block it will be shortened to end of the block
    4100                 :            :  * that cooresponds to 'from'
    4101                 :            :  */
    4102                 :          0 : static int ext4_block_zero_page_range(handle_t *handle,
    4103                 :            :                 struct address_space *mapping, loff_t from, loff_t length)
    4104                 :            : {
    4105                 :          0 :         struct inode *inode = mapping->host;
    4106                 :          0 :         unsigned offset = from & (PAGE_SIZE-1);
    4107                 :          0 :         unsigned blocksize = inode->i_sb->s_blocksize;
    4108                 :          0 :         unsigned max = blocksize - (offset & (blocksize - 1));
    4109                 :            : 
    4110                 :            :         /*
    4111                 :            :          * correct length if it does not fall between
    4112                 :            :          * 'from' and the end of the block
    4113                 :            :          */
    4114   [ #  #  #  # ]:          0 :         if (length > max || length < 0)
    4115                 :            :                 length = max;
    4116                 :            : 
    4117                 :            :         if (IS_DAX(inode)) {
    4118                 :            :                 return iomap_zero_range(inode, from, length, NULL,
    4119                 :            :                                         &ext4_iomap_ops);
    4120                 :            :         }
    4121                 :          0 :         return __ext4_block_zero_page_range(handle, mapping, from, length);
    4122                 :            : }
    4123                 :            : 
    4124                 :            : /*
    4125                 :            :  * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
    4126                 :            :  * up to the end of the block which corresponds to `from'.
    4127                 :            :  * This required during truncate. We need to physically zero the tail end
    4128                 :            :  * of that block so it doesn't yield old data if the file is later grown.
    4129                 :            :  */
    4130                 :          0 : static int ext4_block_truncate_page(handle_t *handle,
    4131                 :            :                 struct address_space *mapping, loff_t from)
    4132                 :            : {
    4133                 :          0 :         unsigned offset = from & (PAGE_SIZE-1);
    4134                 :            :         unsigned length;
    4135                 :            :         unsigned blocksize;
    4136                 :          0 :         struct inode *inode = mapping->host;
    4137                 :            : 
    4138                 :            :         /* If we are processing an encrypted inode during orphan list handling */
    4139   [ #  #  #  # ]:          0 :         if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
    4140                 :            :                 return 0;
    4141                 :            : 
    4142                 :          0 :         blocksize = inode->i_sb->s_blocksize;
    4143                 :          0 :         length = blocksize - (offset & (blocksize - 1));
    4144                 :            : 
    4145                 :          0 :         return ext4_block_zero_page_range(handle, mapping, from, length);
    4146                 :            : }
    4147                 :            : 
    4148                 :          0 : int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
    4149                 :            :                              loff_t lstart, loff_t length)
    4150                 :            : {
    4151                 :          0 :         struct super_block *sb = inode->i_sb;
    4152                 :          0 :         struct address_space *mapping = inode->i_mapping;
    4153                 :            :         unsigned partial_start, partial_end;
    4154                 :            :         ext4_fsblk_t start, end;
    4155                 :          0 :         loff_t byte_end = (lstart + length - 1);
    4156                 :            :         int err = 0;
    4157                 :            : 
    4158                 :          0 :         partial_start = lstart & (sb->s_blocksize - 1);
    4159                 :          0 :         partial_end = byte_end & (sb->s_blocksize - 1);
    4160                 :            : 
    4161                 :          0 :         start = lstart >> sb->s_blocksize_bits;
    4162                 :          0 :         end = byte_end >> sb->s_blocksize_bits;
    4163                 :            : 
    4164                 :            :         /* Handle partial zero within the single block */
    4165   [ #  #  #  # ]:          0 :         if (start == end &&
    4166         [ #  # ]:          0 :             (partial_start || (partial_end != sb->s_blocksize - 1))) {
    4167                 :          0 :                 err = ext4_block_zero_page_range(handle, mapping,
    4168                 :            :                                                  lstart, length);
    4169                 :          0 :                 return err;
    4170                 :            :         }
    4171                 :            :         /* Handle partial zero out on the start of the range */
    4172         [ #  # ]:          0 :         if (partial_start) {
    4173                 :          0 :                 err = ext4_block_zero_page_range(handle, mapping,
    4174                 :            :                                                  lstart, sb->s_blocksize);
    4175         [ #  # ]:          0 :                 if (err)
    4176                 :            :                         return err;
    4177                 :            :         }
    4178                 :            :         /* Handle partial zero out on the end of the range */
    4179         [ #  # ]:          0 :         if (partial_end != sb->s_blocksize - 1)
    4180                 :          0 :                 err = ext4_block_zero_page_range(handle, mapping,
    4181                 :            :                                                  byte_end - partial_end,
    4182                 :          0 :                                                  partial_end + 1);
    4183                 :          0 :         return err;
    4184                 :            : }
    4185                 :            : 
    4186                 :       3543 : int ext4_can_truncate(struct inode *inode)
    4187                 :            : {
    4188         [ +  + ]:       3543 :         if (S_ISREG(inode->i_mode))
    4189                 :            :                 return 1;
    4190         [ -  + ]:       1462 :         if (S_ISDIR(inode->i_mode))
    4191                 :            :                 return 1;
    4192         [ #  # ]:          0 :         if (S_ISLNK(inode->i_mode))
    4193                 :          0 :                 return !ext4_inode_is_fast_symlink(inode);
    4194                 :            :         return 0;
    4195                 :            : }
    4196                 :            : 
    4197                 :            : /*
    4198                 :            :  * We have to make sure i_disksize gets properly updated before we truncate
    4199                 :            :  * page cache due to hole punching or zero range. Otherwise i_disksize update
    4200                 :            :  * can get lost as it may have been postponed to submission of writeback but
    4201                 :            :  * that will never happen after we truncate page cache.
    4202                 :            :  */
    4203                 :          0 : int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
    4204                 :            :                                       loff_t len)
    4205                 :            : {
    4206                 :            :         handle_t *handle;
    4207                 :            :         loff_t size = i_size_read(inode);
    4208                 :            : 
    4209         [ #  # ]:          0 :         WARN_ON(!inode_is_locked(inode));
    4210   [ #  #  #  # ]:          0 :         if (offset > size || offset + len < size)
    4211                 :            :                 return 0;
    4212                 :            : 
    4213         [ #  # ]:          0 :         if (EXT4_I(inode)->i_disksize >= size)
    4214                 :            :                 return 0;
    4215                 :            : 
    4216                 :            :         handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
    4217         [ #  # ]:          0 :         if (IS_ERR(handle))
    4218                 :          0 :                 return PTR_ERR(handle);
    4219                 :          0 :         ext4_update_i_disksize(inode, size);
    4220                 :          0 :         ext4_mark_inode_dirty(handle, inode);
    4221                 :          0 :         ext4_journal_stop(handle);
    4222                 :            : 
    4223                 :          0 :         return 0;
    4224                 :            : }
    4225                 :            : 
    4226                 :            : static void ext4_wait_dax_page(struct ext4_inode_info *ei)
    4227                 :            : {
    4228                 :            :         up_write(&ei->i_mmap_sem);
    4229                 :            :         schedule();
    4230                 :            :         down_write(&ei->i_mmap_sem);
    4231                 :            : }
    4232                 :            : 
    4233                 :        734 : int ext4_break_layouts(struct inode *inode)
    4234                 :            : {
    4235                 :            :         struct ext4_inode_info *ei = EXT4_I(inode);
    4236                 :            :         struct page *page;
    4237                 :            :         int error;
    4238                 :            : 
    4239   [ -  +  #  #  :        734 :         if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem)))
                   +  - ]
    4240                 :            :                 return -EINVAL;
    4241                 :            : 
    4242                 :            :         do {
    4243                 :            :                 page = dax_layout_busy_page(inode->i_mapping);
    4244                 :            :                 if (!page)
    4245                 :            :                         return 0;
    4246                 :            : 
    4247                 :            :                 error = ___wait_var_event(&page->_refcount,
    4248                 :            :                                 atomic_read(&page->_refcount) == 1,
    4249                 :            :                                 TASK_INTERRUPTIBLE, 0, 0,
    4250                 :            :                                 ext4_wait_dax_page(ei));
    4251                 :            :         } while (error == 0);
    4252                 :            : 
    4253                 :            :         return error;
    4254                 :            : }
    4255                 :            : 
    4256                 :            : /*
    4257                 :            :  * ext4_punch_hole: punches a hole in a file by releasing the blocks
    4258                 :            :  * associated with the given offset and length
    4259                 :            :  *
    4260                 :            :  * @inode:  File inode
    4261                 :            :  * @offset: The offset where the hole will begin
    4262                 :            :  * @len:    The length of the hole
    4263                 :            :  *
    4264                 :            :  * Returns: 0 on success or negative on failure
    4265                 :            :  */
    4266                 :            : 
    4267                 :          0 : int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
    4268                 :            : {
    4269                 :          0 :         struct super_block *sb = inode->i_sb;
    4270                 :            :         ext4_lblk_t first_block, stop_block;
    4271                 :          0 :         struct address_space *mapping = inode->i_mapping;
    4272                 :            :         loff_t first_block_offset, last_block_offset;
    4273                 :            :         handle_t *handle;
    4274                 :            :         unsigned int credits;
    4275                 :            :         int ret = 0;
    4276                 :            : 
    4277         [ #  # ]:          0 :         if (!S_ISREG(inode->i_mode))
    4278                 :            :                 return -EOPNOTSUPP;
    4279                 :            : 
    4280                 :          0 :         trace_ext4_punch_hole(inode, offset, length, 0);
    4281                 :            : 
    4282                 :            :         ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
    4283         [ #  # ]:          0 :         if (ext4_has_inline_data(inode)) {
    4284                 :          0 :                 down_write(&EXT4_I(inode)->i_mmap_sem);
    4285                 :          0 :                 ret = ext4_convert_inline_data(inode);
    4286                 :          0 :                 up_write(&EXT4_I(inode)->i_mmap_sem);
    4287         [ #  # ]:          0 :                 if (ret)
    4288                 :            :                         return ret;
    4289                 :            :         }
    4290                 :            : 
    4291                 :            :         /*
    4292                 :            :          * Write out all dirty pages to avoid race conditions
    4293                 :            :          * Then release them.
    4294                 :            :          */
    4295         [ #  # ]:          0 :         if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
    4296                 :          0 :                 ret = filemap_write_and_wait_range(mapping, offset,
    4297                 :          0 :                                                    offset + length - 1);
    4298         [ #  # ]:          0 :                 if (ret)
    4299                 :            :                         return ret;
    4300                 :            :         }
    4301                 :            : 
    4302                 :            :         inode_lock(inode);
    4303                 :            : 
    4304                 :            :         /* No need to punch hole beyond i_size */
    4305         [ #  # ]:          0 :         if (offset >= inode->i_size)
    4306                 :            :                 goto out_mutex;
    4307                 :            : 
    4308                 :            :         /*
    4309                 :            :          * If the hole extends beyond i_size, set the hole
    4310                 :            :          * to end after the page that contains i_size
    4311                 :            :          */
    4312         [ #  # ]:          0 :         if (offset + length > inode->i_size) {
    4313                 :          0 :                 length = inode->i_size +
    4314                 :          0 :                    PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) -
    4315                 :            :                    offset;
    4316                 :            :         }
    4317                 :            : 
    4318   [ #  #  #  # ]:          0 :         if (offset & (sb->s_blocksize - 1) ||
    4319                 :          0 :             (offset + length) & (sb->s_blocksize - 1)) {
    4320                 :            :                 /*
    4321                 :            :                  * Attach jinode to inode for jbd2 if we do any zeroing of
    4322                 :            :                  * partial block
    4323                 :            :                  */
    4324                 :          0 :                 ret = ext4_inode_attach_jinode(inode);
    4325         [ #  # ]:          0 :                 if (ret < 0)
    4326                 :            :                         goto out_mutex;
    4327                 :            : 
    4328                 :            :         }
    4329                 :            : 
    4330                 :            :         /* Wait all existing dio workers, newcomers will block on i_mutex */
    4331                 :          0 :         inode_dio_wait(inode);
    4332                 :            : 
    4333                 :            :         /*
    4334                 :            :          * Prevent page faults from reinstantiating pages we have released from
    4335                 :            :          * page cache.
    4336                 :            :          */
    4337                 :          0 :         down_write(&EXT4_I(inode)->i_mmap_sem);
    4338                 :            : 
    4339                 :          0 :         ret = ext4_break_layouts(inode);
    4340         [ #  # ]:          0 :         if (ret)
    4341                 :            :                 goto out_dio;
    4342                 :            : 
    4343                 :          0 :         first_block_offset = round_up(offset, sb->s_blocksize);
    4344                 :          0 :         last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
    4345                 :            : 
    4346                 :            :         /* Now release the pages and zero block aligned part of pages*/
    4347         [ #  # ]:          0 :         if (last_block_offset > first_block_offset) {
    4348                 :          0 :                 ret = ext4_update_disksize_before_punch(inode, offset, length);
    4349         [ #  # ]:          0 :                 if (ret)
    4350                 :            :                         goto out_dio;
    4351                 :          0 :                 truncate_pagecache_range(inode, first_block_offset,
    4352                 :            :                                          last_block_offset);
    4353                 :            :         }
    4354                 :            : 
    4355         [ #  # ]:          0 :         if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
    4356                 :          0 :                 credits = ext4_writepage_trans_blocks(inode);
    4357                 :            :         else
    4358                 :          0 :                 credits = ext4_blocks_for_truncate(inode);
    4359                 :          0 :         handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
    4360         [ #  # ]:          0 :         if (IS_ERR(handle)) {
    4361                 :            :                 ret = PTR_ERR(handle);
    4362         [ #  # ]:          0 :                 ext4_std_error(sb, ret);
    4363                 :            :                 goto out_dio;
    4364                 :            :         }
    4365                 :            : 
    4366                 :          0 :         ret = ext4_zero_partial_blocks(handle, inode, offset,
    4367                 :            :                                        length);
    4368         [ #  # ]:          0 :         if (ret)
    4369                 :            :                 goto out_stop;
    4370                 :            : 
    4371                 :          0 :         first_block = (offset + sb->s_blocksize - 1) >>
    4372                 :          0 :                 EXT4_BLOCK_SIZE_BITS(sb);
    4373                 :          0 :         stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
    4374                 :            : 
    4375                 :            :         /* If there are blocks to remove, do it */
    4376         [ #  # ]:          0 :         if (stop_block > first_block) {
    4377                 :            : 
    4378                 :          0 :                 down_write(&EXT4_I(inode)->i_data_sem);
    4379                 :          0 :                 ext4_discard_preallocations(inode);
    4380                 :            : 
    4381                 :          0 :                 ret = ext4_es_remove_extent(inode, first_block,
    4382                 :            :                                             stop_block - first_block);
    4383         [ #  # ]:          0 :                 if (ret) {
    4384                 :          0 :                         up_write(&EXT4_I(inode)->i_data_sem);
    4385                 :          0 :                         goto out_stop;
    4386                 :            :                 }
    4387                 :            : 
    4388         [ #  # ]:          0 :                 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
    4389                 :          0 :                         ret = ext4_ext_remove_space(inode, first_block,
    4390                 :            :                                                     stop_block - 1);
    4391                 :            :                 else
    4392                 :          0 :                         ret = ext4_ind_remove_space(handle, inode, first_block,
    4393                 :            :                                                     stop_block);
    4394                 :            : 
    4395                 :          0 :                 up_write(&EXT4_I(inode)->i_data_sem);
    4396                 :            :         }
    4397   [ #  #  #  # ]:          0 :         if (IS_SYNC(inode))
    4398                 :            :                 ext4_handle_sync(handle);
    4399                 :            : 
    4400                 :          0 :         inode->i_mtime = inode->i_ctime = current_time(inode);
    4401                 :          0 :         ext4_mark_inode_dirty(handle, inode);
    4402         [ #  # ]:          0 :         if (ret >= 0)
    4403                 :          0 :                 ext4_update_inode_fsync_trans(handle, inode, 1);
    4404                 :            : out_stop:
    4405                 :          0 :         ext4_journal_stop(handle);
    4406                 :            : out_dio:
    4407                 :          0 :         up_write(&EXT4_I(inode)->i_mmap_sem);
    4408                 :            : out_mutex:
    4409                 :            :         inode_unlock(inode);
    4410                 :          0 :         return ret;
    4411                 :            : }
    4412                 :            : 
    4413                 :     104594 : int ext4_inode_attach_jinode(struct inode *inode)
    4414                 :            : {
    4415                 :            :         struct ext4_inode_info *ei = EXT4_I(inode);
    4416                 :            :         struct jbd2_inode *jinode;
    4417                 :            : 
    4418   [ +  +  +  - ]:     207119 :         if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
    4419                 :            :                 return 0;
    4420                 :            : 
    4421                 :            :         jinode = jbd2_alloc_inode(GFP_KERNEL);
    4422                 :            :         spin_lock(&inode->i_lock);
    4423         [ +  - ]:     102525 :         if (!ei->jinode) {
    4424         [ -  + ]:     102525 :                 if (!jinode) {
    4425                 :            :                         spin_unlock(&inode->i_lock);
    4426                 :          0 :                         return -ENOMEM;
    4427                 :            :                 }
    4428                 :     102525 :                 ei->jinode = jinode;
    4429                 :     102525 :                 jbd2_journal_init_jbd_inode(ei->jinode, inode);
    4430                 :            :                 jinode = NULL;
    4431                 :            :         }
    4432                 :            :         spin_unlock(&inode->i_lock);
    4433         [ -  + ]:     102525 :         if (unlikely(jinode != NULL))
    4434                 :            :                 jbd2_free_inode(jinode);
    4435                 :            :         return 0;
    4436                 :            : }
    4437                 :            : 
    4438                 :            : /*
    4439                 :            :  * ext4_truncate()
    4440                 :            :  *
    4441                 :            :  * We block out ext4_get_block() block instantiations across the entire
    4442                 :            :  * transaction, and VFS/VM ensures that ext4_truncate() cannot run
    4443                 :            :  * simultaneously on behalf of the same inode.
    4444                 :            :  *
    4445                 :            :  * As we work through the truncate and commit bits of it to the journal there
    4446                 :            :  * is one core, guiding principle: the file's tree must always be consistent on
    4447                 :            :  * disk.  We must be able to restart the truncate after a crash.
    4448                 :            :  *
    4449                 :            :  * The file's tree may be transiently inconsistent in memory (although it
    4450                 :            :  * probably isn't), but whenever we close off and commit a journal transaction,
    4451                 :            :  * the contents of (the filesystem + the journal) must be consistent and
    4452                 :            :  * restartable.  It's pretty simple, really: bottom up, right to left (although
    4453                 :            :  * left-to-right works OK too).
    4454                 :            :  *
    4455                 :            :  * Note that at recovery time, journal replay occurs *before* the restart of
    4456                 :            :  * truncate against the orphan inode list.
    4457                 :            :  *
    4458                 :            :  * The committed inode has the new, desired i_size (which is the same as
    4459                 :            :  * i_disksize in this case).  After a crash, ext4_orphan_cleanup() will see
    4460                 :            :  * that this inode's truncate did not complete and it will again call
    4461                 :            :  * ext4_truncate() to have another go.  So there will be instantiated blocks
    4462                 :            :  * to the right of the truncation point in a crashed ext4 filesystem.  But
    4463                 :            :  * that's fine - as long as they are linked from the inode, the post-crash
    4464                 :            :  * ext4_truncate() run will find them and release them.
    4465                 :            :  */
    4466                 :       3543 : int ext4_truncate(struct inode *inode)
    4467                 :            : {
    4468                 :            :         struct ext4_inode_info *ei = EXT4_I(inode);
    4469                 :            :         unsigned int credits;
    4470                 :            :         int err = 0;
    4471                 :            :         handle_t *handle;
    4472                 :       3543 :         struct address_space *mapping = inode->i_mapping;
    4473                 :            : 
    4474                 :            :         /*
    4475                 :            :          * There is a possibility that we're either freeing the inode
    4476                 :            :          * or it's a completely new inode. In those cases we might not
    4477                 :            :          * have i_mutex locked because it's not necessary.
    4478                 :            :          */
    4479         [ +  + ]:       3543 :         if (!(inode->i_state & (I_NEW|I_FREEING)))
    4480         [ -  + ]:        734 :                 WARN_ON(!inode_is_locked(inode));
    4481                 :       3543 :         trace_ext4_truncate_enter(inode);
    4482                 :            : 
    4483         [ +  - ]:       3543 :         if (!ext4_can_truncate(inode))
    4484                 :            :                 return 0;
    4485                 :            : 
    4486                 :            :         ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
    4487                 :            : 
    4488   [ +  -  +  - ]:       7086 :         if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
    4489                 :            :                 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
    4490                 :            : 
    4491         [ -  + ]:       3543 :         if (ext4_has_inline_data(inode)) {
    4492                 :          0 :                 int has_inline = 1;
    4493                 :            : 
    4494                 :          0 :                 err = ext4_inline_data_truncate(inode, &has_inline);
    4495         [ #  # ]:          0 :                 if (err)
    4496                 :          0 :                         return err;
    4497         [ #  # ]:          0 :                 if (has_inline)
    4498                 :            :                         return 0;
    4499                 :            :         }
    4500                 :            : 
    4501                 :            :         /* If we zero-out tail of the page, we have to create jinode for jbd2 */
    4502         [ -  + ]:       3543 :         if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
    4503         [ #  # ]:          0 :                 if (ext4_inode_attach_jinode(inode) < 0)
    4504                 :            :                         return 0;
    4505                 :            :         }
    4506                 :            : 
    4507         [ +  - ]:       3543 :         if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
    4508                 :       3543 :                 credits = ext4_writepage_trans_blocks(inode);
    4509                 :            :         else
    4510                 :          0 :                 credits = ext4_blocks_for_truncate(inode);
    4511                 :            : 
    4512                 :       3543 :         handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
    4513         [ -  + ]:       3542 :         if (IS_ERR(handle))
    4514                 :          0 :                 return PTR_ERR(handle);
    4515                 :            : 
    4516         [ -  + ]:       3542 :         if (inode->i_size & (inode->i_sb->s_blocksize - 1))
    4517                 :          0 :                 ext4_block_truncate_page(handle, mapping, inode->i_size);
    4518                 :            : 
    4519                 :            :         /*
    4520                 :            :          * We add the inode to the orphan list, so that if this
    4521                 :            :          * truncate spans multiple transactions, and we crash, we will
    4522                 :            :          * resume the truncate when the filesystem recovers.  It also
    4523                 :            :          * marks the inode dirty, to catch the new size.
    4524                 :            :          *
    4525                 :            :          * Implication: the file must always be in a sane, consistent
    4526                 :            :          * truncatable state while each transaction commits.
    4527                 :            :          */
    4528                 :       3542 :         err = ext4_orphan_add(handle, inode);
    4529         [ +  - ]:       3542 :         if (err)
    4530                 :            :                 goto out_stop;
    4531                 :            : 
    4532                 :       3542 :         down_write(&EXT4_I(inode)->i_data_sem);
    4533                 :            : 
    4534                 :       3542 :         ext4_discard_preallocations(inode);
    4535                 :            : 
    4536         [ +  - ]:       3542 :         if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
    4537                 :       3542 :                 err = ext4_ext_truncate(handle, inode);
    4538                 :            :         else
    4539                 :          0 :                 ext4_ind_truncate(handle, inode);
    4540                 :            : 
    4541                 :       3543 :         up_write(&ei->i_data_sem);
    4542         [ +  - ]:       3543 :         if (err)
    4543                 :            :                 goto out_stop;
    4544                 :            : 
    4545   [ +  +  -  + ]:       3543 :         if (IS_SYNC(inode))
    4546                 :            :                 ext4_handle_sync(handle);
    4547                 :            : 
    4548                 :            : out_stop:
    4549                 :            :         /*
    4550                 :            :          * If this was a simple ftruncate() and the file will remain alive,
    4551                 :            :          * then we need to clear up the orphan record which we created above.
    4552                 :            :          * However, if this was a real unlink then we were called by
    4553                 :            :          * ext4_evict_inode(), and we allow that function to clean up the
    4554                 :            :          * orphan info for us.
    4555                 :            :          */
    4556         [ +  + ]:       3543 :         if (inode->i_nlink)
    4557                 :        734 :                 ext4_orphan_del(handle, inode);
    4558                 :            : 
    4559                 :       3543 :         inode->i_mtime = inode->i_ctime = current_time(inode);
    4560                 :       3543 :         ext4_mark_inode_dirty(handle, inode);
    4561                 :       3543 :         ext4_journal_stop(handle);
    4562                 :            : 
    4563                 :       3543 :         trace_ext4_truncate_exit(inode);
    4564                 :       3543 :         return err;
    4565                 :            : }
    4566                 :            : 
    4567                 :            : /*
    4568                 :            :  * ext4_get_inode_loc returns with an extra refcount against the inode's
    4569                 :            :  * underlying buffer_head on success. If 'in_mem' is true, we have all
    4570                 :            :  * data in memory that is needed to recreate the on-disk version of this
    4571                 :            :  * inode.
    4572                 :            :  */
    4573                 :    2062459 : static int __ext4_get_inode_loc(struct inode *inode,
    4574                 :            :                                 struct ext4_iloc *iloc, int in_mem)
    4575                 :            : {
    4576                 :            :         struct ext4_group_desc  *gdp;
    4577                 :            :         struct buffer_head      *bh;
    4578                 :    2062459 :         struct super_block      *sb = inode->i_sb;
    4579                 :            :         ext4_fsblk_t            block;
    4580                 :            :         struct blk_plug         plug;
    4581                 :            :         int                     inodes_per_block, inode_offset;
    4582                 :            : 
    4583                 :    2062459 :         iloc->bh = NULL;
    4584   [ +  +  +  + ]:    4124805 :         if (inode->i_ino < EXT4_ROOT_INO ||
    4585                 :    2062346 :             inode->i_ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
    4586                 :            :                 return -EFSCORRUPTED;
    4587                 :            : 
    4588                 :    2062276 :         iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
    4589                 :    2062276 :         gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
    4590         [ +  + ]:    2062339 :         if (!gdp)
    4591                 :            :                 return -EIO;
    4592                 :            : 
    4593                 :            :         /*
    4594                 :            :          * Figure out the offset within the block group inode table
    4595                 :            :          */
    4596                 :    2062076 :         inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
    4597                 :    4124152 :         inode_offset = ((inode->i_ino - 1) %
    4598                 :    2062076 :                         EXT4_INODES_PER_GROUP(sb));
    4599                 :    2062076 :         block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
    4600                 :    4124742 :         iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
    4601                 :            : 
    4602                 :            :         bh = sb_getblk(sb, block);
    4603         [ +  + ]:    2062565 :         if (unlikely(!bh))
    4604                 :            :                 return -ENOMEM;
    4605         [ +  + ]:    2062545 :         if (!buffer_uptodate(bh)) {
    4606                 :      25185 :                 lock_buffer(bh);
    4607                 :            : 
    4608                 :            :                 /*
    4609                 :            :                  * If the buffer has the write error flag, we have failed
    4610                 :            :                  * to write out another inode in the same block.  In this
    4611                 :            :                  * case, we don't have to read the block because we may
    4612                 :            :                  * read the old inode data successfully.
    4613                 :            :                  */
    4614   [ -  +  #  # ]:      25180 :                 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
    4615                 :            :                         set_buffer_uptodate(bh);
    4616                 :            : 
    4617         [ +  + ]:      25182 :                 if (buffer_uptodate(bh)) {
    4618                 :            :                         /* someone brought it uptodate while we waited */
    4619                 :         78 :                         unlock_buffer(bh);
    4620                 :         78 :                         goto has_buffer;
    4621                 :            :                 }
    4622                 :            : 
    4623                 :            :                 /*
    4624                 :            :                  * If we have all information of the inode in memory and this
    4625                 :            :                  * is the only valid inode in the block, we need not read the
    4626                 :            :                  * block.
    4627                 :            :                  */
    4628         [ +  + ]:      25104 :                 if (in_mem) {
    4629                 :            :                         struct buffer_head *bitmap_bh;
    4630                 :            :                         int i, start;
    4631                 :            : 
    4632                 :       1863 :                         start = inode_offset & ~(inodes_per_block - 1);
    4633                 :            : 
    4634                 :            :                         /* Is the inode bitmap in cache? */
    4635                 :       1863 :                         bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
    4636         [ +  - ]:       1863 :                         if (unlikely(!bitmap_bh))
    4637                 :            :                                 goto make_io;
    4638                 :            : 
    4639                 :            :                         /*
    4640                 :            :                          * If the inode bitmap isn't in cache then the
    4641                 :            :                          * optimisation may end up performing two reads instead
    4642                 :            :                          * of one, so skip it.
    4643                 :            :                          */
    4644         [ +  - ]:       1863 :                         if (!buffer_uptodate(bitmap_bh)) {
    4645                 :            :                                 brelse(bitmap_bh);
    4646                 :            :                                 goto make_io;
    4647                 :            :                         }
    4648         [ +  + ]:      16560 :                         for (i = start; i < start + inodes_per_block; i++) {
    4649         [ +  + ]:      17388 :                                 if (i == inode_offset)
    4650                 :       1035 :                                         continue;
    4651         [ +  + ]:      32706 :                                 if (ext4_test_bit(i, bitmap_bh->b_data))
    4652                 :            :                                         break;
    4653                 :            :                         }
    4654                 :            :                         brelse(bitmap_bh);
    4655         [ +  + ]:       1863 :                         if (i == start + inodes_per_block) {
    4656                 :            :                                 /* all other inodes are free, so skip I/O */
    4657                 :       1035 :                                 memset(bh->b_data, 0, bh->b_size);
    4658                 :            :                                 set_buffer_uptodate(bh);
    4659                 :       1035 :                                 unlock_buffer(bh);
    4660                 :       1035 :                                 goto has_buffer;
    4661                 :            :                         }
    4662                 :            :                 }
    4663                 :            : 
    4664                 :            : make_io:
    4665                 :            :                 /*
    4666                 :            :                  * If we need to do any I/O, try to pre-readahead extra
    4667                 :            :                  * blocks from the inode table.
    4668                 :            :                  */
    4669                 :      24069 :                 blk_start_plug(&plug);
    4670         [ +  - ]:      24082 :                 if (EXT4_SB(sb)->s_inode_readahead_blks) {
    4671                 :            :                         ext4_fsblk_t b, end, table;
    4672                 :            :                         unsigned num;
    4673                 :            :                         __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
    4674                 :            : 
    4675                 :      24082 :                         table = ext4_inode_table(sb, gdp);
    4676                 :            :                         /* s_inode_readahead_blks is always a power of 2 */
    4677                 :      24065 :                         b = block & ~((ext4_fsblk_t) ra_blks - 1);
    4678         [ +  + ]:      24065 :                         if (table > b)
    4679                 :            :                                 b = table;
    4680                 :      24065 :                         end = b + ra_blks;
    4681                 :      24065 :                         num = EXT4_INODES_PER_GROUP(sb);
    4682         [ -  + ]:      24065 :                         if (ext4_has_group_desc_csum(sb))
    4683                 :          0 :                                 num -= ext4_itable_unused_count(sb, gdp);
    4684                 :      24064 :                         table += num / inodes_per_block;
    4685         [ +  + ]:      24064 :                         if (end > table)
    4686                 :            :                                 end = table;
    4687         [ +  + ]:     815874 :                         while (b <= end)
    4688                 :     791792 :                                 sb_breadahead_unmovable(sb, b++);
    4689                 :            :                 }
    4690                 :            : 
    4691                 :            :                 /*
    4692                 :            :                  * There are other valid inodes in the buffer, this inode
    4693                 :            :                  * has in-inode xattrs, or we don't have this inode in memory.
    4694                 :            :                  * Read the block from disk.
    4695                 :            :                  */
    4696                 :      24082 :                 trace_ext4_load_inode(inode);
    4697                 :            :                 get_bh(bh);
    4698                 :      24081 :                 bh->b_end_io = end_buffer_read_sync;
    4699                 :      24081 :                 submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
    4700                 :      24082 :                 blk_finish_plug(&plug);
    4701                 :      24082 :                 wait_on_buffer(bh);
    4702         [ -  + ]:      24082 :                 if (!buffer_uptodate(bh)) {
    4703                 :          0 :                         EXT4_ERROR_INODE_BLOCK(inode, block,
    4704                 :            :                                                "unable to read itable block");
    4705                 :            :                         brelse(bh);
    4706                 :            :                         return -EIO;
    4707                 :            :                 }
    4708                 :            :         }
    4709                 :            : has_buffer:
    4710                 :    2062555 :         iloc->bh = bh;
    4711                 :    2062555 :         return 0;
    4712                 :            : }
    4713                 :            : 
    4714                 :          0 : int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
    4715                 :            : {
    4716                 :            :         /* We have all inode data except xattrs in memory here. */
    4717                 :    1407285 :         return __ext4_get_inode_loc(inode, iloc,
    4718                 :            :                 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
    4719                 :            : }
    4720                 :            : 
    4721                 :            : static bool ext4_should_use_dax(struct inode *inode)
    4722                 :            : {
    4723                 :            :         if (!test_opt(inode->i_sb, DAX))
    4724                 :            :                 return false;
    4725                 :            :         if (!S_ISREG(inode->i_mode))
    4726                 :            :                 return false;
    4727                 :            :         if (ext4_should_journal_data(inode))
    4728                 :            :                 return false;
    4729                 :            :         if (ext4_has_inline_data(inode))
    4730                 :            :                 return false;
    4731                 :            :         if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT))
    4732                 :            :                 return false;
    4733                 :            :         if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY))
    4734                 :            :                 return false;
    4735                 :            :         return true;
    4736                 :            : }
    4737                 :            : 
    4738                 :     800147 : void ext4_set_inode_flags(struct inode *inode)
    4739                 :            : {
    4740                 :     800147 :         unsigned int flags = EXT4_I(inode)->i_flags;
    4741                 :            :         unsigned int new_fl = 0;
    4742                 :            : 
    4743         [ -  + ]:     800147 :         if (flags & EXT4_SYNC_FL)
    4744                 :            :                 new_fl |= S_SYNC;
    4745         [ -  + ]:     800147 :         if (flags & EXT4_APPEND_FL)
    4746                 :          0 :                 new_fl |= S_APPEND;
    4747         [ -  + ]:     800147 :         if (flags & EXT4_IMMUTABLE_FL)
    4748                 :          0 :                 new_fl |= S_IMMUTABLE;
    4749         [ -  + ]:     800147 :         if (flags & EXT4_NOATIME_FL)
    4750                 :          0 :                 new_fl |= S_NOATIME;
    4751         [ -  + ]:     800147 :         if (flags & EXT4_DIRSYNC_FL)
    4752                 :          0 :                 new_fl |= S_DIRSYNC;
    4753                 :            :         if (ext4_should_use_dax(inode))
    4754                 :            :                 new_fl |= S_DAX;
    4755         [ -  + ]:     800147 :         if (flags & EXT4_ENCRYPT_FL)
    4756                 :          0 :                 new_fl |= S_ENCRYPTED;
    4757         [ -  + ]:     800147 :         if (flags & EXT4_CASEFOLD_FL)
    4758                 :          0 :                 new_fl |= S_CASEFOLD;
    4759         [ -  + ]:     800147 :         if (flags & EXT4_VERITY_FL)
    4760                 :          0 :                 new_fl |= S_VERITY;
    4761                 :     800147 :         inode_set_flags(inode, new_fl,
    4762                 :            :                         S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX|
    4763                 :            :                         S_ENCRYPTED|S_CASEFOLD|S_VERITY);
    4764                 :     800131 : }
    4765                 :            : 
    4766                 :     655250 : static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
    4767                 :            :                                   struct ext4_inode_info *ei)
    4768                 :            : {
    4769                 :            :         blkcnt_t i_blocks ;
    4770                 :            :         struct inode *inode = &(ei->vfs_inode);
    4771                 :     655250 :         struct super_block *sb = inode->i_sb;
    4772                 :            : 
    4773         [ -  + ]:     655250 :         if (ext4_has_feature_huge_file(sb)) {
    4774                 :            :                 /* we are using combined 48 bit field */
    4775                 :          0 :                 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
    4776                 :          0 :                                         le32_to_cpu(raw_inode->i_blocks_lo);
    4777         [ #  # ]:          0 :                 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
    4778                 :            :                         /* i_blocks represent file system block size */
    4779                 :          0 :                         return i_blocks  << (inode->i_blkbits - 9);
    4780                 :            :                 } else {
    4781                 :            :                         return i_blocks;
    4782                 :            :                 }
    4783                 :            :         } else {
    4784                 :     655250 :                 return le32_to_cpu(raw_inode->i_blocks_lo);
    4785                 :            :         }
    4786                 :            : }
    4787                 :            : 
    4788                 :     654681 : static inline int ext4_iget_extra_inode(struct inode *inode,
    4789                 :            :                                          struct ext4_inode *raw_inode,
    4790                 :            :                                          struct ext4_inode_info *ei)
    4791                 :            : {
    4792                 :     654681 :         __le32 *magic = (void *)raw_inode +
    4793                 :     654681 :                         EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
    4794                 :            : 
    4795         [ +  + ]:    1309362 :         if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <=
    4796         [ -  + ]:    1964382 :             EXT4_INODE_SIZE(inode->i_sb) &&
    4797                 :     655020 :             *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
    4798                 :            :                 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
    4799                 :          0 :                 return ext4_find_inline_data_nolock(inode);
    4800                 :            :         } else
    4801                 :     654681 :                 EXT4_I(inode)->i_inline_off = 0;
    4802                 :     654681 :         return 0;
    4803                 :            : }
    4804                 :            : 
    4805                 :          0 : int ext4_get_projid(struct inode *inode, kprojid_t *projid)
    4806                 :            : {
    4807         [ #  # ]:          0 :         if (!ext4_has_feature_project(inode->i_sb))
    4808                 :            :                 return -EOPNOTSUPP;
    4809                 :          0 :         *projid = EXT4_I(inode)->i_projid;
    4810                 :          0 :         return 0;
    4811                 :            : }
    4812                 :            : 
    4813                 :            : /*
    4814                 :            :  * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of
    4815                 :            :  * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag
    4816                 :            :  * set.
    4817                 :            :  */
    4818                 :     655220 : static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val)
    4819                 :            : {
    4820         [ -  + ]:     655220 :         if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
    4821                 :            :                 inode_set_iversion_raw(inode, val);
    4822                 :            :         else
    4823                 :            :                 inode_set_iversion_queried(inode, val);
    4824                 :     655173 : }
    4825                 :            : static inline u64 ext4_inode_peek_iversion(const struct inode *inode)
    4826                 :            : {
    4827         [ -  + ]:    1407274 :         if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
    4828                 :            :                 return inode_peek_iversion_raw(inode);
    4829                 :            :         else
    4830                 :            :                 return inode_peek_iversion(inode);
    4831                 :            : }
    4832                 :            : 
    4833                 :     684408 : struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
    4834                 :            :                           ext4_iget_flags flags, const char *function,
    4835                 :            :                           unsigned int line)
    4836                 :            : {
    4837                 :            :         struct ext4_iloc iloc;
    4838                 :            :         struct ext4_inode *raw_inode;
    4839                 :            :         struct ext4_inode_info *ei;
    4840                 :            :         struct inode *inode;
    4841                 :     684408 :         journal_t *journal = EXT4_SB(sb)->s_journal;
    4842                 :            :         long ret;
    4843                 :            :         loff_t size;
    4844                 :            :         int block;
    4845                 :            :         uid_t i_uid;
    4846                 :            :         gid_t i_gid;
    4847                 :            :         projid_t i_projid;
    4848                 :            : 
    4849   [ +  +  -  + ]:    1368163 :         if ((!(flags & EXT4_IGET_SPECIAL) &&
    4850   [ #  #  +  - ]:     683755 :              (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) ||
    4851         [ +  + ]:     684368 :             (ino < EXT4_ROOT_INO) ||
    4852                 :     684368 :             (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) {
    4853         [ -  + ]:        157 :                 if (flags & EXT4_IGET_HANDLE)
    4854                 :            :                         return ERR_PTR(-ESTALE);
    4855                 :          0 :                 __ext4_error(sb, function, line,
    4856                 :            :                              "inode #%lu: comm %s: iget: illegal inode #",
    4857                 :          0 :                              ino, current->comm);
    4858                 :          0 :                 return ERR_PTR(-EFSCORRUPTED);
    4859                 :            :         }
    4860                 :            : 
    4861                 :     684254 :         inode = iget_locked(sb, ino);
    4862         [ +  - ]:     684432 :         if (!inode)
    4863                 :            :                 return ERR_PTR(-ENOMEM);
    4864         [ +  + ]:     684433 :         if (!(inode->i_state & I_NEW))
    4865                 :            :                 return inode;
    4866                 :            : 
    4867                 :     655239 :         ei = EXT4_I(inode);
    4868                 :     655239 :         iloc.bh = NULL;
    4869                 :            : 
    4870                 :     655239 :         ret = __ext4_get_inode_loc(inode, &iloc, 0);
    4871         [ +  + ]:     655226 :         if (ret < 0)
    4872                 :            :                 goto bad_inode;
    4873                 :            :         raw_inode = ext4_raw_inode(&iloc);
    4874                 :            : 
    4875   [ +  +  -  + ]:     655066 :         if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) {
    4876                 :          0 :                 ext4_error_inode(inode, function, line, 0,
    4877                 :            :                                  "iget: root inode unallocated");
    4878                 :            :                 ret = -EFSCORRUPTED;
    4879                 :          0 :                 goto bad_inode;
    4880                 :            :         }
    4881                 :            : 
    4882   [ -  +  #  # ]:     655066 :         if ((flags & EXT4_IGET_HANDLE) &&
    4883         [ #  # ]:          0 :             (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
    4884                 :            :                 ret = -ESTALE;
    4885                 :            :                 goto bad_inode;
    4886                 :            :         }
    4887                 :            : 
    4888         [ +  - ]:    1310526 :         if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
    4889                 :     655263 :                 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
    4890         [ +  + ]:    1310526 :                 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
    4891         [ +  + ]:    1310497 :                         EXT4_INODE_SIZE(inode->i_sb) ||
    4892                 :     655234 :                     (ei->i_extra_isize & 3)) {
    4893                 :        211 :                         ext4_error_inode(inode, function, line, 0,
    4894                 :            :                                          "iget: bad extra_isize %u "
    4895                 :            :                                          "(inode size %u)",
    4896                 :            :                                          ei->i_extra_isize,
    4897                 :            :                                          EXT4_INODE_SIZE(inode->i_sb));
    4898                 :            :                         ret = -EFSCORRUPTED;
    4899                 :          0 :                         goto bad_inode;
    4900                 :            :                 }
    4901                 :            :         } else
    4902                 :          0 :                 ei->i_extra_isize = 0;
    4903                 :            : 
    4904                 :            :         /* Precompute checksum seed for inode metadata */
    4905         [ -  + ]:     655052 :         if (ext4_has_metadata_csum(sb)) {
    4906                 :          0 :                 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
    4907                 :            :                 __u32 csum;
    4908                 :          0 :                 __le32 inum = cpu_to_le32(inode->i_ino);
    4909                 :          0 :                 __le32 gen = raw_inode->i_generation;
    4910                 :          0 :                 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
    4911                 :            :                                    sizeof(inum));
    4912                 :          0 :                 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
    4913                 :            :                                               sizeof(gen));
    4914                 :            :         }
    4915                 :            : 
    4916         [ -  + ]:     655175 :         if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
    4917                 :          0 :                 ext4_error_inode(inode, function, line, 0,
    4918                 :            :                                  "iget: checksum invalid");
    4919                 :            :                 ret = -EFSBADCRC;
    4920                 :          0 :                 goto bad_inode;
    4921                 :            :         }
    4922                 :            : 
    4923                 :     655154 :         inode->i_mode = le16_to_cpu(raw_inode->i_mode);
    4924                 :     655154 :         i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
    4925                 :     655154 :         i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
    4926   [ -  +  #  # ]:     655154 :         if (ext4_has_feature_project(sb) &&
    4927         [ #  # ]:          0 :             EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
    4928                 :          0 :             EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
    4929                 :          0 :                 i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
    4930                 :            :         else
    4931                 :            :                 i_projid = EXT4_DEF_PROJID;
    4932                 :            : 
    4933         [ +  + ]:    1310308 :         if (!(test_opt(inode->i_sb, NO_UID32))) {
    4934                 :     655266 :                 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
    4935                 :     655266 :                 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
    4936                 :            :         }
    4937                 :            :         i_uid_write(inode, i_uid);
    4938                 :            :         i_gid_write(inode, i_gid);
    4939                 :     655266 :         ei->i_projid = make_kprojid(&init_user_ns, i_projid);
    4940                 :     655240 :         set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
    4941                 :            : 
    4942                 :            :         ext4_clear_state_flags(ei);     /* Only relevant on 32-bit archs */
    4943                 :     654825 :         ei->i_inline_off = 0;
    4944                 :     654825 :         ei->i_dir_start_lookup = 0;
    4945                 :     654825 :         ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
    4946                 :            :         /* We now have enough fields to check if the inode was active or not.
    4947                 :            :          * This is needed because nfsd might try to access dead inodes
    4948                 :            :          * the test is that same one that e2fsck uses
    4949                 :            :          * NeilBrown 1999oct15
    4950                 :            :          */
    4951         [ -  + ]:     654825 :         if (inode->i_nlink == 0) {
    4952   [ #  #  #  # ]:          0 :                 if ((inode->i_mode == 0 ||
    4953   [ -  +  -  - ]:        176 :                      !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
    4954                 :            :                     ino != EXT4_BOOT_LOADER_INO) {
    4955                 :            :                         /* this inode is deleted */
    4956                 :            :                         ret = -ESTALE;
    4957                 :            :                         goto bad_inode;
    4958                 :            :                 }
    4959                 :            :                 /* The only unlinked inodes we let through here have
    4960                 :            :                  * valid i_mode and are being read by the orphan
    4961                 :            :                  * recovery code: that's fine, we're about to complete
    4962                 :            :                  * the process of deleting those.
    4963                 :            :                  * OR it is the EXT4_BOOT_LOADER_INO which is
    4964                 :            :                  * not initialized on a new filesystem. */
    4965                 :            :         }
    4966                 :     654729 :         ei->i_flags = le32_to_cpu(raw_inode->i_flags);
    4967                 :     654729 :         ext4_set_inode_flags(inode);
    4968                 :     655233 :         inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
    4969                 :     654275 :         ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
    4970         [ -  + ]:     654275 :         if (ext4_has_feature_64bit(sb))
    4971                 :          0 :                 ei->i_file_acl |=
    4972                 :          0 :                         ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
    4973                 :     654275 :         inode->i_size = ext4_isize(sb, raw_inode);
    4974         [ -  + ]:     654765 :         if ((size = i_size_read(inode)) < 0) {
    4975                 :          0 :                 ext4_error_inode(inode, function, line, 0,
    4976                 :            :                                  "iget: bad i_size value: %lld", size);
    4977                 :            :                 ret = -EFSCORRUPTED;
    4978                 :          0 :                 goto bad_inode;
    4979                 :            :         }
    4980                 :            :         /*
    4981                 :            :          * If dir_index is not enabled but there's dir with INDEX flag set,
    4982                 :            :          * we'd normally treat htree data as empty space. But with metadata
    4983                 :            :          * checksumming that corrupts checksums so forbid that.
    4984                 :            :          */
    4985   [ -  +  #  #  :     654765 :         if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) &&
                   #  # ]
    4986                 :            :             ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) {
    4987                 :          0 :                 ext4_error_inode(inode, function, line, 0,
    4988                 :            :                          "iget: Dir with htree data on filesystem without dir_index feature.");
    4989                 :            :                 ret = -EFSCORRUPTED;
    4990                 :          0 :                 goto bad_inode;
    4991                 :            :         }
    4992                 :     654765 :         ei->i_disksize = inode->i_size;
    4993                 :            : #ifdef CONFIG_QUOTA
    4994                 :     654765 :         ei->i_reserved_quota = 0;
    4995                 :            : #endif
    4996                 :     654765 :         inode->i_generation = le32_to_cpu(raw_inode->i_generation);
    4997                 :     654765 :         ei->i_block_group = iloc.block_group;
    4998                 :     654765 :         ei->i_last_alloc_group = ~0;
    4999                 :            :         /*
    5000                 :            :          * NOTE! The in-memory inode i_data array is in little-endian order
    5001                 :            :          * even on big-endian machines: we do NOT byteswap the block numbers!
    5002                 :            :          */
    5003         [ +  + ]:   10473733 :         for (block = 0; block < EXT4_N_BLOCKS; block++)
    5004                 :    9818968 :                 ei->i_data[block] = raw_inode->i_block[block];
    5005                 :     654765 :         INIT_LIST_HEAD(&ei->i_orphan);
    5006                 :            : 
    5007                 :            :         /*
    5008                 :            :          * Set transaction id's of transactions that have to be committed
    5009                 :            :          * to finish f[data]sync. We set them to currently running transaction
    5010                 :            :          * as we cannot be sure that the inode or some of its metadata isn't
    5011                 :            :          * part of the transaction - the inode could have been reclaimed and
    5012                 :            :          * now it is reread from disk.
    5013                 :            :          */
    5014         [ +  + ]:     654765 :         if (journal) {
    5015                 :            :                 transaction_t *transaction;
    5016                 :            :                 tid_t tid;
    5017                 :            : 
    5018                 :     654703 :                 read_lock(&journal->j_state_lock);
    5019         [ +  + ]:     654663 :                 if (journal->j_running_transaction)
    5020                 :            :                         transaction = journal->j_running_transaction;
    5021                 :            :                 else
    5022                 :     177160 :                         transaction = journal->j_committing_transaction;
    5023         [ +  + ]:     654663 :                 if (transaction)
    5024                 :     478112 :                         tid = transaction->t_tid;
    5025                 :            :                 else
    5026                 :     176551 :                         tid = journal->j_commit_sequence;
    5027                 :            :                 read_unlock(&journal->j_state_lock);
    5028                 :     655062 :                 ei->i_sync_tid = tid;
    5029                 :     655062 :                 ei->i_datasync_tid = tid;
    5030                 :            :         }
    5031                 :            : 
    5032         [ +  + ]:    1310248 :         if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
    5033         [ -  + ]:     655004 :                 if (ei->i_extra_isize == 0) {
    5034                 :            :                         /* The extra space is currently unused. Use it. */
    5035                 :            :                         BUILD_BUG_ON(sizeof(struct ext4_inode) & 3);
    5036                 :          0 :                         ei->i_extra_isize = sizeof(struct ext4_inode) -
    5037                 :            :                                             EXT4_GOOD_OLD_INODE_SIZE;
    5038                 :            :                 } else {
    5039                 :     655004 :                         ret = ext4_iget_extra_inode(inode, raw_inode, ei);
    5040         [ +  + ]:     654877 :                         if (ret)
    5041                 :            :                                 goto bad_inode;
    5042                 :            :                 }
    5043                 :            :         }
    5044                 :            : 
    5045         [ +  - ]:     655056 :         EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
    5046         [ +  - ]:     655056 :         EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
    5047         [ +  - ]:     655056 :         EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
    5048   [ +  -  +  - ]:     655056 :         EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
    5049                 :            : 
    5050         [ +  + ]:    1310112 :         if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
    5051                 :     655079 :                 u64 ivers = le32_to_cpu(raw_inode->i_disk_version);
    5052                 :            : 
    5053         [ +  + ]:     655079 :                 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
    5054         [ +  + ]:     654861 :                         if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
    5055                 :     654681 :                                 ivers |=
    5056                 :     654681 :                     (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
    5057                 :            :                 }
    5058                 :     655079 :                 ext4_inode_set_iversion_queried(inode, ivers);
    5059                 :            :         }
    5060                 :            : 
    5061                 :            :         ret = 0;
    5062   [ -  +  #  # ]:     655275 :         if (ei->i_file_acl &&
    5063                 :          0 :             !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
    5064                 :          0 :                 ext4_error_inode(inode, function, line, 0,
    5065                 :            :                                  "iget: bad extended attribute block %llu",
    5066                 :            :                                  ei->i_file_acl);
    5067                 :            :                 ret = -EFSCORRUPTED;
    5068                 :          0 :                 goto bad_inode;
    5069         [ +  + ]:     655275 :         } else if (!ext4_has_inline_data(inode)) {
    5070                 :            :                 /* validate the block references in the inode */
    5071   [ +  +  +  + ]:     654890 :                 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
    5072         [ +  + ]:      86797 :                    (S_ISLNK(inode->i_mode) &&
    5073                 :      86781 :                     !ext4_inode_is_fast_symlink(inode))) {
    5074         [ +  - ]:     568643 :                         if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
    5075                 :     568643 :                                 ret = ext4_ext_check_inode(inode);
    5076                 :            :                         else
    5077                 :          0 :                                 ret = ext4_ind_check_inode(inode);
    5078                 :            :                 }
    5079                 :            :         }
    5080         [ +  - ]:     655201 :         if (ret)
    5081                 :            :                 goto bad_inode;
    5082                 :            : 
    5083         [ +  + ]:     655201 :         if (S_ISREG(inode->i_mode)) {
    5084                 :     351423 :                 inode->i_op = &ext4_file_inode_operations;
    5085                 :     351423 :                 inode->i_fop = &ext4_file_operations;
    5086                 :     351423 :                 ext4_set_aops(inode);
    5087         [ +  + ]:     303778 :         } else if (S_ISDIR(inode->i_mode)) {
    5088                 :     216977 :                 inode->i_op = &ext4_dir_inode_operations;
    5089                 :     216977 :                 inode->i_fop = &ext4_dir_operations;
    5090         [ +  + ]:      86801 :         } else if (S_ISLNK(inode->i_mode)) {
    5091                 :            :                 /* VFS does not allow setting these so must be corruption */
    5092         [ -  + ]:      86800 :                 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
    5093                 :          0 :                         ext4_error_inode(inode, function, line, 0,
    5094                 :            :                                          "iget: immutable or append flags "
    5095                 :            :                                          "not allowed on symlinks");
    5096                 :            :                         ret = -EFSCORRUPTED;
    5097                 :          0 :                         goto bad_inode;
    5098                 :            :                 }
    5099         [ -  + ]:      86800 :                 if (IS_ENCRYPTED(inode)) {
    5100                 :          0 :                         inode->i_op = &ext4_encrypted_symlink_inode_operations;
    5101                 :          0 :                         ext4_set_aops(inode);
    5102         [ +  + ]:      86800 :                 } else if (ext4_inode_is_fast_symlink(inode)) {
    5103                 :      86592 :                         inode->i_link = (char *)ei->i_data;
    5104                 :      86592 :                         inode->i_op = &ext4_fast_symlink_inode_operations;
    5105                 :      86592 :                         nd_terminate_link(ei->i_data, inode->i_size,
    5106                 :            :                                 sizeof(ei->i_data) - 1);
    5107                 :            :                 } else {
    5108                 :        209 :                         inode->i_op = &ext4_symlink_inode_operations;
    5109                 :        209 :                         ext4_set_aops(inode);
    5110                 :            :                 }
    5111                 :      86801 :                 inode_nohighmem(inode);
    5112   [ +  -  +  - ]:          1 :         } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
    5113         [ +  - ]:          1 :               S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
    5114                 :          1 :                 inode->i_op = &ext4_special_inode_operations;
    5115         [ -  + ]:          1 :                 if (raw_inode->i_block[0])
    5116                 :          0 :                         init_special_inode(inode, inode->i_mode,
    5117                 :            :                            old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
    5118                 :            :                 else
    5119                 :          2 :                         init_special_inode(inode, inode->i_mode,
    5120                 :            :                            new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
    5121         [ #  # ]:          0 :         } else if (ino == EXT4_BOOT_LOADER_INO) {
    5122                 :          0 :                 make_bad_inode(inode);
    5123                 :            :         } else {
    5124                 :            :                 ret = -EFSCORRUPTED;
    5125                 :          0 :                 ext4_error_inode(inode, function, line, 0,
    5126                 :            :                                  "iget: bogus i_mode (%o)", inode->i_mode);
    5127                 :          0 :                 goto bad_inode;
    5128                 :            :         }
    5129   [ -  +  #  # ]:     655247 :         if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb))
    5130                 :          0 :                 ext4_error_inode(inode, function, line, 0,
    5131                 :            :                                  "casefold flag without casefold feature");
    5132                 :     655247 :         brelse(iloc.bh);
    5133                 :            : 
    5134                 :     655252 :         unlock_new_inode(inode);
    5135                 :     655242 :         return inode;
    5136                 :            : 
    5137                 :            : bad_inode:
    5138                 :          0 :         brelse(iloc.bh);
    5139                 :          0 :         iget_failed(inode);
    5140                 :          0 :         return ERR_PTR(ret);
    5141                 :            : }
    5142                 :            : 
    5143                 :    1407272 : static int ext4_inode_blocks_set(handle_t *handle,
    5144                 :            :                                 struct ext4_inode *raw_inode,
    5145                 :            :                                 struct ext4_inode_info *ei)
    5146                 :            : {
    5147                 :            :         struct inode *inode = &(ei->vfs_inode);
    5148                 :            :         u64 i_blocks = READ_ONCE(inode->i_blocks);
    5149                 :    1407272 :         struct super_block *sb = inode->i_sb;
    5150                 :            : 
    5151         [ +  - ]:    1407272 :         if (i_blocks <= ~0U) {
    5152                 :            :                 /*
    5153                 :            :                  * i_blocks can be represented in a 32 bit variable
    5154                 :            :                  * as multiple of 512 bytes
    5155                 :            :                  */
    5156                 :    1407272 :                 raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
    5157                 :    1407272 :                 raw_inode->i_blocks_high = 0;
    5158                 :            :                 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
    5159                 :    1407265 :                 return 0;
    5160                 :            :         }
    5161         [ #  # ]:          0 :         if (!ext4_has_feature_huge_file(sb))
    5162                 :            :                 return -EFBIG;
    5163                 :            : 
    5164         [ #  # ]:          0 :         if (i_blocks <= 0xffffffffffffULL) {
    5165                 :            :                 /*
    5166                 :            :                  * i_blocks can be represented in a 48 bit variable
    5167                 :            :                  * as multiple of 512 bytes
    5168                 :            :                  */
    5169                 :          0 :                 raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
    5170                 :          0 :                 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
    5171                 :            :                 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
    5172                 :            :         } else {
    5173                 :            :                 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
    5174                 :            :                 /* i_block is stored in file system block size */
    5175                 :          0 :                 i_blocks = i_blocks >> (inode->i_blkbits - 9);
    5176                 :          0 :                 raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
    5177                 :          0 :                 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
    5178                 :            :         }
    5179                 :            :         return 0;
    5180                 :            : }
    5181                 :            : 
    5182                 :            : struct other_inode {
    5183                 :            :         unsigned long           orig_ino;
    5184                 :            :         struct ext4_inode       *raw_inode;
    5185                 :            : };
    5186                 :            : 
    5187                 :          0 : static int other_inode_match(struct inode * inode, unsigned long ino,
    5188                 :            :                              void *data)
    5189                 :            : {
    5190                 :            :         struct other_inode *oi = (struct other_inode *) data;
    5191                 :            : 
    5192         [ #  # ]:          0 :         if ((inode->i_ino != ino) ||
    5193                 :            :             (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
    5194         [ #  # ]:          0 :                                I_DIRTY_INODE)) ||
    5195                 :            :             ((inode->i_state & I_DIRTY_TIME) == 0))
    5196                 :            :                 return 0;
    5197                 :            :         spin_lock(&inode->i_lock);
    5198         [ #  # ]:          0 :         if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
    5199                 :          0 :                                 I_DIRTY_INODE)) == 0) &&
    5200                 :            :             (inode->i_state & I_DIRTY_TIME)) {
    5201                 :          0 :                 struct ext4_inode_info  *ei = EXT4_I(inode);
    5202                 :            : 
    5203                 :          0 :                 inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
    5204                 :            :                 spin_unlock(&inode->i_lock);
    5205                 :            : 
    5206                 :            :                 spin_lock(&ei->i_raw_lock);
    5207         [ #  # ]:          0 :                 EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode);
    5208         [ #  # ]:          0 :                 EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode);
    5209         [ #  # ]:          0 :                 EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode);
    5210                 :          0 :                 ext4_inode_csum_set(inode, oi->raw_inode, ei);
    5211                 :            :                 spin_unlock(&ei->i_raw_lock);
    5212                 :          0 :                 trace_ext4_other_inode_update_time(inode, oi->orig_ino);
    5213                 :          0 :                 return -1;
    5214                 :            :         }
    5215                 :            :         spin_unlock(&inode->i_lock);
    5216                 :          0 :         return -1;
    5217                 :            : }
    5218                 :            : 
    5219                 :            : /*
    5220                 :            :  * Opportunistically update the other time fields for other inodes in
    5221                 :            :  * the same inode table block.
    5222                 :            :  */
    5223                 :          0 : static void ext4_update_other_inodes_time(struct super_block *sb,
    5224                 :            :                                           unsigned long orig_ino, char *buf)
    5225                 :            : {
    5226                 :            :         struct other_inode oi;
    5227                 :            :         unsigned long ino;
    5228                 :          0 :         int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
    5229                 :          0 :         int inode_size = EXT4_INODE_SIZE(sb);
    5230                 :            : 
    5231                 :          0 :         oi.orig_ino = orig_ino;
    5232                 :            :         /*
    5233                 :            :          * Calculate the first inode in the inode table block.  Inode
    5234                 :            :          * numbers are one-based.  That is, the first inode in a block
    5235                 :            :          * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
    5236                 :            :          */
    5237                 :          0 :         ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
    5238         [ #  # ]:          0 :         for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
    5239         [ #  # ]:          0 :                 if (ino == orig_ino)
    5240                 :          0 :                         continue;
    5241                 :          0 :                 oi.raw_inode = (struct ext4_inode *) buf;
    5242                 :          0 :                 (void) find_inode_nowait(sb, ino, other_inode_match, &oi);
    5243                 :            :         }
    5244                 :          0 : }
    5245                 :            : 
    5246                 :            : /*
    5247                 :            :  * Post the struct inode info into an on-disk inode location in the
    5248                 :            :  * buffer-cache.  This gobbles the caller's reference to the
    5249                 :            :  * buffer_head in the inode location struct.
    5250                 :            :  *
    5251                 :            :  * The caller must have write access to iloc->bh.
    5252                 :            :  */
    5253                 :    1407207 : static int ext4_do_update_inode(handle_t *handle,
    5254                 :            :                                 struct inode *inode,
    5255                 :            :                                 struct ext4_iloc *iloc)
    5256                 :            : {
    5257                 :            :         struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
    5258                 :    1407207 :         struct ext4_inode_info *ei = EXT4_I(inode);
    5259                 :            :         struct buffer_head *bh = iloc->bh;
    5260                 :    1407207 :         struct super_block *sb = inode->i_sb;
    5261                 :            :         int err = 0, rc, block;
    5262                 :            :         int need_datasync = 0, set_large_file = 0;
    5263                 :            :         uid_t i_uid;
    5264                 :            :         gid_t i_gid;
    5265                 :            :         projid_t i_projid;
    5266                 :            : 
    5267                 :            :         spin_lock(&ei->i_raw_lock);
    5268                 :            : 
    5269                 :            :         /* For fields not tracked in the in-memory inode,
    5270                 :            :          * initialise them to zero for new inodes. */
    5271         [ +  + ]:    1407281 :         if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
    5272                 :     289772 :                 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
    5273                 :            : 
    5274                 :    1407281 :         raw_inode->i_mode = cpu_to_le16(inode->i_mode);
    5275                 :            :         i_uid = i_uid_read(inode);
    5276                 :            :         i_gid = i_gid_read(inode);
    5277                 :    1407236 :         i_projid = from_kprojid(&init_user_ns, ei->i_projid);
    5278         [ +  - ]:    2814442 :         if (!(test_opt(inode->i_sb, NO_UID32))) {
    5279                 :    1407221 :                 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
    5280                 :    1407221 :                 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
    5281                 :            : /*
    5282                 :            :  * Fix up interoperability with old kernels. Otherwise, old inodes get
    5283                 :            :  * re-used with the upper 16 bits of the uid/gid intact
    5284                 :            :  */
    5285   [ +  +  +  + ]:    1426656 :                 if (ei->i_dtime && list_empty(&ei->i_orphan)) {
    5286                 :       3290 :                         raw_inode->i_uid_high = 0;
    5287                 :       3290 :                         raw_inode->i_gid_high = 0;
    5288                 :            :                 } else {
    5289                 :    1403931 :                         raw_inode->i_uid_high =
    5290                 :    1403931 :                                 cpu_to_le16(high_16_bits(i_uid));
    5291                 :    1403931 :                         raw_inode->i_gid_high =
    5292                 :    1403931 :                                 cpu_to_le16(high_16_bits(i_gid));
    5293                 :            :                 }
    5294                 :            :         } else {
    5295         [ #  # ]:          0 :                 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
    5296         [ #  # ]:          0 :                 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
    5297                 :          0 :                 raw_inode->i_uid_high = 0;
    5298                 :          0 :                 raw_inode->i_gid_high = 0;
    5299                 :            :         }
    5300                 :    1407221 :         raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
    5301                 :            : 
    5302         [ +  - ]:    2814442 :         EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
    5303         [ +  - ]:    2814442 :         EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
    5304         [ +  - ]:    2814442 :         EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
    5305   [ +  +  +  + ]:    2814453 :         EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
    5306                 :            : 
    5307                 :    1407221 :         err = ext4_inode_blocks_set(handle, raw_inode, ei);
    5308         [ -  + ]:    1407253 :         if (err) {
    5309                 :            :                 spin_unlock(&ei->i_raw_lock);
    5310                 :            :                 goto out_brelse;
    5311                 :            :         }
    5312                 :    1407253 :         raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
    5313                 :    1407253 :         raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
    5314         [ +  + ]:    2814506 :         if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
    5315                 :    1407247 :                 raw_inode->i_file_acl_high =
    5316                 :    1407247 :                         cpu_to_le16(ei->i_file_acl >> 32);
    5317                 :    1407253 :         raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
    5318         [ +  + ]:    2814506 :         if (ei->i_disksize != ext4_isize(inode->i_sb, raw_inode)) {
    5319                 :            :                 ext4_isize_set(raw_inode, ei->i_disksize);
    5320                 :            :                 need_datasync = 1;
    5321                 :            :         }
    5322         [ -  + ]:    1407253 :         if (ei->i_disksize > 0x7fffffffULL) {
    5323   [ #  #  #  # ]:          0 :                 if (!ext4_has_feature_large_file(sb) ||
    5324                 :          0 :                                 EXT4_SB(sb)->s_es->s_rev_level ==
    5325                 :            :                     cpu_to_le32(EXT4_GOOD_OLD_REV))
    5326                 :            :                         set_large_file = 1;
    5327                 :            :         }
    5328                 :    1407253 :         raw_inode->i_generation = cpu_to_le32(inode->i_generation);
    5329         [ -  + ]:    1407253 :         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
    5330         [ #  # ]:          0 :                 if (old_valid_dev(inode->i_rdev)) {
    5331                 :          0 :                         raw_inode->i_block[0] =
    5332                 :          0 :                                 cpu_to_le32(old_encode_dev(inode->i_rdev));
    5333                 :          0 :                         raw_inode->i_block[1] = 0;
    5334                 :            :                 } else {
    5335                 :          0 :                         raw_inode->i_block[0] = 0;
    5336                 :          0 :                         raw_inode->i_block[1] =
    5337                 :          0 :                                 cpu_to_le32(new_encode_dev(inode->i_rdev));
    5338                 :          0 :                         raw_inode->i_block[2] = 0;
    5339                 :            :                 }
    5340         [ +  + ]:    1407253 :         } else if (!ext4_has_inline_data(inode)) {
    5341         [ +  + ]:   21106903 :                 for (block = 0; block < EXT4_N_BLOCKS; block++)
    5342                 :   21106903 :                         raw_inode->i_block[block] = ei->i_data[block];
    5343                 :            :         }
    5344                 :            : 
    5345         [ +  + ]:    2814506 :         if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
    5346                 :            :                 u64 ivers = ext4_inode_peek_iversion(inode);
    5347                 :            : 
    5348                 :    1407266 :                 raw_inode->i_disk_version = cpu_to_le32(ivers);
    5349         [ +  + ]:    1407266 :                 if (ei->i_extra_isize) {
    5350         [ +  + ]:    1407264 :                         if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
    5351                 :    1407273 :                                 raw_inode->i_version_hi =
    5352                 :    1407273 :                                         cpu_to_le32(ivers >> 32);
    5353                 :    1407264 :                         raw_inode->i_extra_isize =
    5354                 :    1407264 :                                 cpu_to_le16(ei->i_extra_isize);
    5355                 :            :                 }
    5356                 :            :         }
    5357                 :            : 
    5358   [ +  +  -  + ]:    2814490 :         BUG_ON(!ext4_has_feature_project(inode->i_sb) &&
    5359                 :            :                i_projid != EXT4_DEF_PROJID);
    5360                 :            : 
    5361   [ +  +  +  + ]:    2814509 :         if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
    5362                 :    1407264 :             EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
    5363                 :    1407274 :                 raw_inode->i_projid = cpu_to_le32(i_projid);
    5364                 :            : 
    5365                 :    1407245 :         ext4_inode_csum_set(inode, raw_inode, ei);
    5366                 :            :         spin_unlock(&ei->i_raw_lock);
    5367         [ -  + ]:    1407224 :         if (inode->i_sb->s_flags & SB_LAZYTIME)
    5368                 :          0 :                 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
    5369                 :            :                                               bh->b_data);
    5370                 :            : 
    5371                 :            :         BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
    5372                 :    1407224 :         rc = ext4_handle_dirty_metadata(handle, NULL, bh);
    5373         [ +  + ]:    1407274 :         if (!err)
    5374                 :            :                 err = rc;
    5375                 :            :         ext4_clear_inode_state(inode, EXT4_STATE_NEW);
    5376         [ -  + ]:    1407273 :         if (set_large_file) {
    5377                 :            :                 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
    5378                 :          0 :                 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
    5379         [ #  # ]:          0 :                 if (err)
    5380                 :            :                         goto out_brelse;
    5381                 :            :                 ext4_set_feature_large_file(sb);
    5382                 :            :                 ext4_handle_sync(handle);
    5383                 :          0 :                 err = ext4_handle_dirty_super(handle, sb);
    5384                 :            :         }
    5385                 :    1407273 :         ext4_update_inode_fsync_trans(handle, inode, need_datasync);
    5386                 :            : out_brelse:
    5387                 :            :         brelse(bh);
    5388         [ -  + ]:    1407230 :         ext4_std_error(inode->i_sb, err);
    5389                 :    1407230 :         return err;
    5390                 :            : }
    5391                 :            : 
    5392                 :            : /*
    5393                 :            :  * ext4_write_inode()
    5394                 :            :  *
    5395                 :            :  * We are called from a few places:
    5396                 :            :  *
    5397                 :            :  * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
    5398                 :            :  *   Here, there will be no transaction running. We wait for any running
    5399                 :            :  *   transaction to commit.
    5400                 :            :  *
    5401                 :            :  * - Within flush work (sys_sync(), kupdate and such).
    5402                 :            :  *   We wait on commit, if told to.
    5403                 :            :  *
    5404                 :            :  * - Within iput_final() -> write_inode_now()
    5405                 :            :  *   We wait on commit, if told to.
    5406                 :            :  *
    5407                 :            :  * In all cases it is actually safe for us to return without doing anything,
    5408                 :            :  * because the inode has been copied into a raw inode buffer in
    5409                 :            :  * ext4_mark_inode_dirty().  This is a correctness thing for WB_SYNC_ALL
    5410                 :            :  * writeback.
    5411                 :            :  *
    5412                 :            :  * Note that we are absolutely dependent upon all inode dirtiers doing the
    5413                 :            :  * right thing: they *must* call mark_inode_dirty() after dirtying info in
    5414                 :            :  * which we are interested.
    5415                 :            :  *
    5416                 :            :  * It would be a bug for them to not do this.  The code:
    5417                 :            :  *
    5418                 :            :  *      mark_inode_dirty(inode)
    5419                 :            :  *      stuff();
    5420                 :            :  *      inode->i_size = expr;
    5421                 :            :  *
    5422                 :            :  * is in error because write_inode() could occur while `stuff()' is running,
    5423                 :            :  * and the new i_size will be lost.  Plus the inode will no longer be on the
    5424                 :            :  * superblock's dirty inode list.
    5425                 :            :  */
    5426                 :      39864 : int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
    5427                 :            : {
    5428                 :            :         int err;
    5429                 :            : 
    5430   [ -  +  #  #  :      79728 :         if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) ||
             +  -  +  - ]
    5431                 :      39864 :             sb_rdonly(inode->i_sb))
    5432                 :            :                 return 0;
    5433                 :            : 
    5434         [ +  - ]:      39864 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
    5435                 :            :                 return -EIO;
    5436                 :            : 
    5437         [ +  - ]:      39864 :         if (EXT4_SB(inode->i_sb)->s_journal) {
    5438         [ -  + ]:      39864 :                 if (ext4_journal_current_handle()) {
    5439                 :            :                         jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
    5440                 :          0 :                         dump_stack();
    5441                 :          0 :                         return -EIO;
    5442                 :            :                 }
    5443                 :            : 
    5444                 :            :                 /*
    5445                 :            :                  * No need to force transaction in WB_SYNC_NONE mode. Also
    5446                 :            :                  * ext4_sync_fs() will force the commit after everything is
    5447                 :            :                  * written.
    5448                 :            :                  */
    5449   [ +  +  -  + ]:      39864 :                 if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
    5450                 :            :                         return 0;
    5451                 :            : 
    5452                 :          0 :                 err = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
    5453                 :            :                                                 EXT4_I(inode)->i_sync_tid);
    5454                 :            :         } else {
    5455                 :            :                 struct ext4_iloc iloc;
    5456                 :            : 
    5457                 :          0 :                 err = __ext4_get_inode_loc(inode, &iloc, 0);
    5458         [ #  # ]:          0 :                 if (err)
    5459                 :          0 :                         return err;
    5460                 :            :                 /*
    5461                 :            :                  * sync(2) will flush the whole buffer cache. No need to do
    5462                 :            :                  * it here separately for each inode.
    5463                 :            :                  */
    5464   [ #  #  #  # ]:          0 :                 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
    5465                 :          0 :                         sync_dirty_buffer(iloc.bh);
    5466   [ #  #  #  # ]:          0 :                 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
    5467                 :          0 :                         EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
    5468                 :            :                                          "IO error syncing inode");
    5469                 :            :                         err = -EIO;
    5470                 :            :                 }
    5471                 :          0 :                 brelse(iloc.bh);
    5472                 :            :         }
    5473                 :          0 :         return err;
    5474                 :            : }
    5475                 :            : 
    5476                 :            : /*
    5477                 :            :  * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
    5478                 :            :  * buffers that are attached to a page stradding i_size and are undergoing
    5479                 :            :  * commit. In that case we have to wait for commit to finish and try again.
    5480                 :            :  */
    5481                 :          0 : static void ext4_wait_for_tail_page_commit(struct inode *inode)
    5482                 :            : {
    5483                 :            :         struct page *page;
    5484                 :            :         unsigned offset;
    5485                 :          0 :         journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
    5486                 :            :         tid_t commit_tid = 0;
    5487                 :            :         int ret;
    5488                 :            : 
    5489                 :          0 :         offset = inode->i_size & (PAGE_SIZE - 1);
    5490                 :            :         /*
    5491                 :            :          * If the page is fully truncated, we don't need to wait for any commit
    5492                 :            :          * (and we even should not as __ext4_journalled_invalidatepage() may
    5493                 :            :          * strip all buffers from the page but keep the page dirty which can then
    5494                 :            :          * confuse e.g. concurrent ext4_writepage() seeing dirty page without
    5495                 :            :          * buffers). Also we don't need to wait for any commit if all buffers in
    5496                 :            :          * the page remain valid. This is most beneficial for the common case of
    5497                 :            :          * blocksize == PAGESIZE.
    5498                 :            :          */
    5499   [ #  #  #  # ]:          0 :         if (!offset || offset > (PAGE_SIZE - i_blocksize(inode)))
    5500                 :            :                 return;
    5501                 :            :         while (1) {
    5502                 :          0 :                 page = find_lock_page(inode->i_mapping,
    5503                 :          0 :                                       inode->i_size >> PAGE_SHIFT);
    5504         [ #  # ]:          0 :                 if (!page)
    5505                 :            :                         return;
    5506                 :          0 :                 ret = __ext4_journalled_invalidatepage(page, offset,
    5507                 :          0 :                                                 PAGE_SIZE - offset);
    5508                 :          0 :                 unlock_page(page);
    5509                 :          0 :                 put_page(page);
    5510         [ #  # ]:          0 :                 if (ret != -EBUSY)
    5511                 :            :                         return;
    5512                 :            :                 commit_tid = 0;
    5513                 :          0 :                 read_lock(&journal->j_state_lock);
    5514         [ #  # ]:          0 :                 if (journal->j_committing_transaction)
    5515                 :          0 :                         commit_tid = journal->j_committing_transaction->t_tid;
    5516                 :            :                 read_unlock(&journal->j_state_lock);
    5517         [ #  # ]:          0 :                 if (commit_tid)
    5518                 :          0 :                         jbd2_log_wait_commit(journal, commit_tid);
    5519                 :            :         }
    5520                 :            : }
    5521                 :            : 
    5522                 :            : /*
    5523                 :            :  * ext4_setattr()
    5524                 :            :  *
    5525                 :            :  * Called from notify_change.
    5526                 :            :  *
    5527                 :            :  * We want to trap VFS attempts to truncate the file as soon as
    5528                 :            :  * possible.  In particular, we want to make sure that when the VFS
    5529                 :            :  * shrinks i_size, we put the inode on the orphan list and modify
    5530                 :            :  * i_disksize immediately, so that during the subsequent flushing of
    5531                 :            :  * dirty pages and freeing of disk blocks, we can guarantee that any
    5532                 :            :  * commit will leave the blocks being flushed in an unused state on
    5533                 :            :  * disk.  (On recovery, the inode will get truncated and the blocks will
    5534                 :            :  * be freed, so we have a strong guarantee that no future commit will
    5535                 :            :  * leave these blocks visible to the user.)
    5536                 :            :  *
    5537                 :            :  * Another thing we have to assure is that if we are in ordered mode
    5538                 :            :  * and inode is still attached to the committing transaction, we must
    5539                 :            :  * we start writeout of all the dirty pages which are being truncated.
    5540                 :            :  * This way we are sure that all the data written in the previous
    5541                 :            :  * transaction are already on disk (truncate waits for pages under
    5542                 :            :  * writeback).
    5543                 :            :  *
    5544                 :            :  * Called with inode->i_mutex down.
    5545                 :            :  */
    5546                 :       4189 : int ext4_setattr(struct dentry *dentry, struct iattr *attr)
    5547                 :            : {
    5548                 :            :         struct inode *inode = d_inode(dentry);
    5549                 :            :         int error, rc = 0;
    5550                 :            :         int orphan = 0;
    5551                 :       4189 :         const unsigned int ia_valid = attr->ia_valid;
    5552                 :            : 
    5553         [ +  - ]:       8378 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
    5554                 :            :                 return -EIO;
    5555                 :            : 
    5556         [ +  - ]:       4189 :         if (unlikely(IS_IMMUTABLE(inode)))
    5557                 :            :                 return -EPERM;
    5558                 :            : 
    5559   [ -  +  #  # ]:       4189 :         if (unlikely(IS_APPEND(inode) &&
    5560                 :            :                      (ia_valid & (ATTR_MODE | ATTR_UID |
    5561                 :            :                                   ATTR_GID | ATTR_TIMES_SET))))
    5562                 :            :                 return -EPERM;
    5563                 :            : 
    5564                 :       4189 :         error = setattr_prepare(dentry, attr);
    5565         [ +  - ]:       4189 :         if (error)
    5566                 :            :                 return error;
    5567                 :            : 
    5568                 :            :         error = fscrypt_prepare_setattr(dentry, attr);
    5569         [ +  - ]:       4189 :         if (error)
    5570                 :            :                 return error;
    5571                 :            : 
    5572                 :            :         error = fsverity_prepare_setattr(dentry, attr);
    5573         [ +  - ]:       4189 :         if (error)
    5574                 :            :                 return error;
    5575                 :            : 
    5576         [ +  + ]:       4189 :         if (is_quota_modification(inode, attr)) {
    5577                 :        743 :                 error = dquot_initialize(inode);
    5578         [ +  - ]:        743 :                 if (error)
    5579                 :            :                         return error;
    5580                 :            :         }
    5581   [ +  +  +  -  :       8378 :         if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
                   +  + ]
    5582         [ +  + ]:       5076 :             (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
    5583                 :            :                 handle_t *handle;
    5584                 :            : 
    5585                 :            :                 /* (user+group)*(old+new) structure, inode write (sb,
    5586                 :            :                  * inode block, ? - but truncate inode update has it) */
    5587   [ +  -  -  +  :         36 :                 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
          #  #  +  -  +  
                      - ]
    5588                 :            :                         (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
    5589                 :            :                          EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
    5590         [ -  + ]:          9 :                 if (IS_ERR(handle)) {
    5591                 :            :                         error = PTR_ERR(handle);
    5592                 :          0 :                         goto err_out;
    5593                 :            :                 }
    5594                 :            : 
    5595                 :            :                 /* dquot_transfer() calls back ext4_get_inode_usage() which
    5596                 :            :                  * counts xattr inode references.
    5597                 :            :                  */
    5598                 :          9 :                 down_read(&EXT4_I(inode)->xattr_sem);
    5599                 :          9 :                 error = dquot_transfer(inode, attr);
    5600                 :          9 :                 up_read(&EXT4_I(inode)->xattr_sem);
    5601                 :            : 
    5602         [ -  + ]:          9 :                 if (error) {
    5603                 :          0 :                         ext4_journal_stop(handle);
    5604                 :          0 :                         return error;
    5605                 :            :                 }
    5606                 :            :                 /* Update corresponding info in inode so that everything is in
    5607                 :            :                  * one transaction */
    5608         [ +  - ]:          9 :                 if (attr->ia_valid & ATTR_UID)
    5609                 :          9 :                         inode->i_uid = attr->ia_uid;
    5610         [ +  - ]:          9 :                 if (attr->ia_valid & ATTR_GID)
    5611                 :          9 :                         inode->i_gid = attr->ia_gid;
    5612                 :          9 :                 error = ext4_mark_inode_dirty(handle, inode);
    5613                 :          9 :                 ext4_journal_stop(handle);
    5614                 :            :         }
    5615                 :            : 
    5616         [ +  + ]:       4189 :         if (attr->ia_valid & ATTR_SIZE) {
    5617                 :            :                 handle_t *handle;
    5618                 :        734 :                 loff_t oldsize = inode->i_size;
    5619                 :        734 :                 int shrink = (attr->ia_size < inode->i_size);
    5620                 :            : 
    5621         [ -  + ]:        734 :                 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
    5622                 :          0 :                         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
    5623                 :            : 
    5624         [ #  # ]:          0 :                         if (attr->ia_size > sbi->s_bitmap_maxbytes)
    5625                 :            :                                 return -EFBIG;
    5626                 :            :                 }
    5627         [ +  - ]:        734 :                 if (!S_ISREG(inode->i_mode))
    5628                 :            :                         return -EINVAL;
    5629                 :            : 
    5630   [ -  +  #  # ]:        734 :                 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
    5631                 :            :                         inode_inc_iversion(inode);
    5632                 :            : 
    5633         [ +  + ]:        734 :                 if (shrink) {
    5634         [ +  - ]:        732 :                         if (ext4_should_order_data(inode)) {
    5635                 :        732 :                                 error = ext4_begin_ordered_truncate(inode,
    5636                 :            :                                                             attr->ia_size);
    5637         [ +  - ]:        732 :                                 if (error)
    5638                 :            :                                         goto err_out;
    5639                 :            :                         }
    5640                 :            :                         /*
    5641                 :            :                          * Blocks are going to be removed from the inode. Wait
    5642                 :            :                          * for dio in flight.
    5643                 :            :                          */
    5644                 :        732 :                         inode_dio_wait(inode);
    5645                 :            :                 }
    5646                 :            : 
    5647                 :        734 :                 down_write(&EXT4_I(inode)->i_mmap_sem);
    5648                 :            : 
    5649                 :        734 :                 rc = ext4_break_layouts(inode);
    5650         [ -  + ]:        734 :                 if (rc) {
    5651                 :          0 :                         up_write(&EXT4_I(inode)->i_mmap_sem);
    5652                 :          0 :                         return rc;
    5653                 :            :                 }
    5654                 :            : 
    5655         [ +  + ]:        734 :                 if (attr->ia_size != inode->i_size) {
    5656                 :            :                         handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
    5657         [ -  + ]:        732 :                         if (IS_ERR(handle)) {
    5658                 :            :                                 error = PTR_ERR(handle);
    5659                 :          0 :                                 goto out_mmap_sem;
    5660                 :            :                         }
    5661   [ +  -  +  - ]:        732 :                         if (ext4_handle_valid(handle) && shrink) {
    5662                 :        732 :                                 error = ext4_orphan_add(handle, inode);
    5663                 :            :                                 orphan = 1;
    5664                 :            :                         }
    5665                 :            :                         /*
    5666                 :            :                          * Update c/mtime on truncate up, ext4_truncate() will
    5667                 :            :                          * update c/mtime in shrink case below
    5668                 :            :                          */
    5669         [ -  + ]:        732 :                         if (!shrink) {
    5670                 :          0 :                                 inode->i_mtime = current_time(inode);
    5671                 :          0 :                                 inode->i_ctime = inode->i_mtime;
    5672                 :            :                         }
    5673                 :        732 :                         down_write(&EXT4_I(inode)->i_data_sem);
    5674                 :        732 :                         EXT4_I(inode)->i_disksize = attr->ia_size;
    5675                 :        732 :                         rc = ext4_mark_inode_dirty(handle, inode);
    5676         [ +  - ]:        732 :                         if (!error)
    5677                 :            :                                 error = rc;
    5678                 :            :                         /*
    5679                 :            :                          * We have to update i_size under i_data_sem together
    5680                 :            :                          * with i_disksize to avoid races with writeback code
    5681                 :            :                          * running ext4_wb_update_i_disksize().
    5682                 :            :                          */
    5683         [ +  - ]:        732 :                         if (!error)
    5684                 :        732 :                                 i_size_write(inode, attr->ia_size);
    5685                 :        732 :                         up_write(&EXT4_I(inode)->i_data_sem);
    5686                 :        732 :                         ext4_journal_stop(handle);
    5687         [ +  - ]:        732 :                         if (error)
    5688                 :            :                                 goto out_mmap_sem;
    5689         [ -  + ]:        732 :                         if (!shrink) {
    5690                 :          0 :                                 pagecache_isize_extended(inode, oldsize,
    5691                 :            :                                                          inode->i_size);
    5692         [ -  + ]:        732 :                         } else if (ext4_should_journal_data(inode)) {
    5693                 :          0 :                                 ext4_wait_for_tail_page_commit(inode);
    5694                 :            :                         }
    5695                 :            :                 }
    5696                 :            : 
    5697                 :            :                 /*
    5698                 :            :                  * Truncate pagecache after we've waited for commit
    5699                 :            :                  * in data=journal mode to make pages freeable.
    5700                 :            :                  */
    5701                 :        734 :                 truncate_pagecache(inode, inode->i_size);
    5702                 :            :                 /*
    5703                 :            :                  * Call ext4_truncate() even if i_size didn't change to
    5704                 :            :                  * truncate possible preallocated blocks.
    5705                 :            :                  */
    5706         [ +  - ]:        734 :                 if (attr->ia_size <= oldsize) {
    5707                 :        734 :                         rc = ext4_truncate(inode);
    5708         [ -  + ]:        734 :                         if (rc)
    5709                 :            :                                 error = rc;
    5710                 :            :                 }
    5711                 :            : out_mmap_sem:
    5712                 :        734 :                 up_write(&EXT4_I(inode)->i_mmap_sem);
    5713                 :            :         }
    5714                 :            : 
    5715         [ +  - ]:       4189 :         if (!error) {
    5716                 :       4189 :                 setattr_copy(inode, attr);
    5717                 :            :                 mark_inode_dirty(inode);
    5718                 :            :         }
    5719                 :            : 
    5720                 :            :         /*
    5721                 :            :          * If the call to ext4_truncate failed to get a transaction handle at
    5722                 :            :          * all, we need to clean up the in-core orphan list manually.
    5723                 :            :          */
    5724   [ +  +  +  - ]:       4189 :         if (orphan && inode->i_nlink)
    5725                 :        732 :                 ext4_orphan_del(NULL, inode);
    5726                 :            : 
    5727   [ +  -  +  + ]:       4189 :         if (!error && (ia_valid & ATTR_MODE))
    5728                 :       1843 :                 rc = posix_acl_chmod(inode, inode->i_mode);
    5729                 :            : 
    5730                 :            : err_out:
    5731         [ -  + ]:       4189 :         ext4_std_error(inode->i_sb, error);
    5732         [ +  - ]:       4189 :         if (!error)
    5733                 :            :                 error = rc;
    5734                 :       4189 :         return error;
    5735                 :            : }
    5736                 :            : 
    5737                 :    7606224 : int ext4_getattr(const struct path *path, struct kstat *stat,
    5738                 :            :                  u32 request_mask, unsigned int query_flags)
    5739                 :            : {
    5740                 :    7606224 :         struct inode *inode = d_inode(path->dentry);
    5741                 :            :         struct ext4_inode *raw_inode;
    5742                 :            :         struct ext4_inode_info *ei = EXT4_I(inode);
    5743                 :            :         unsigned int flags;
    5744                 :            : 
    5745         [ +  + ]:    7606224 :         if (EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) {
    5746                 :    7605998 :                 stat->result_mask |= STATX_BTIME;
    5747                 :    7605998 :                 stat->btime.tv_sec = ei->i_crtime.tv_sec;
    5748                 :    7605998 :                 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
    5749                 :            :         }
    5750                 :            : 
    5751                 :    7606224 :         flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
    5752         [ -  + ]:    7606224 :         if (flags & EXT4_APPEND_FL)
    5753                 :          0 :                 stat->attributes |= STATX_ATTR_APPEND;
    5754         [ -  + ]:    7606224 :         if (flags & EXT4_COMPR_FL)
    5755                 :          0 :                 stat->attributes |= STATX_ATTR_COMPRESSED;
    5756         [ -  + ]:    7606224 :         if (flags & EXT4_ENCRYPT_FL)
    5757                 :          0 :                 stat->attributes |= STATX_ATTR_ENCRYPTED;
    5758         [ -  + ]:    7606224 :         if (flags & EXT4_IMMUTABLE_FL)
    5759                 :          0 :                 stat->attributes |= STATX_ATTR_IMMUTABLE;
    5760         [ -  + ]:    7606224 :         if (flags & EXT4_NODUMP_FL)
    5761                 :          0 :                 stat->attributes |= STATX_ATTR_NODUMP;
    5762                 :            : 
    5763                 :    7606224 :         stat->attributes_mask |= (STATX_ATTR_APPEND |
    5764                 :            :                                   STATX_ATTR_COMPRESSED |
    5765                 :            :                                   STATX_ATTR_ENCRYPTED |
    5766                 :            :                                   STATX_ATTR_IMMUTABLE |
    5767                 :            :                                   STATX_ATTR_NODUMP);
    5768                 :            : 
    5769                 :    7606224 :         generic_fillattr(inode, stat);
    5770                 :    7609183 :         return 0;
    5771                 :            : }
    5772                 :            : 
    5773                 :    3503046 : int ext4_file_getattr(const struct path *path, struct kstat *stat,
    5774                 :            :                       u32 request_mask, unsigned int query_flags)
    5775                 :            : {
    5776                 :    3503046 :         struct inode *inode = d_inode(path->dentry);
    5777                 :            :         u64 delalloc_blocks;
    5778                 :            : 
    5779                 :    3503046 :         ext4_getattr(path, stat, request_mask, query_flags);
    5780                 :            : 
    5781                 :            :         /*
    5782                 :            :          * If there is inline data in the inode, the inode will normally not
    5783                 :            :          * have data blocks allocated (it may have an external xattr block).
    5784                 :            :          * Report at least one sector for such files, so tools like tar, rsync,
    5785                 :            :          * others don't incorrectly think the file is completely sparse.
    5786                 :            :          */
    5787         [ -  + ]:    3502626 :         if (unlikely(ext4_has_inline_data(inode)))
    5788                 :          0 :                 stat->blocks += (stat->size + 511) >> 9;
    5789                 :            : 
    5790                 :            :         /*
    5791                 :            :          * We can't update i_blocks if the block allocation is delayed
    5792                 :            :          * otherwise in the case of system crash before the real block
    5793                 :            :          * allocation is done, we will have i_blocks inconsistent with
    5794                 :            :          * on-disk file blocks.
    5795                 :            :          * We always keep i_blocks updated together with real
    5796                 :            :          * allocation. But to not confuse with user, stat
    5797                 :            :          * will return the blocks that include the delayed allocation
    5798                 :            :          * blocks for this file.
    5799                 :            :          */
    5800                 :    7005252 :         delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
    5801                 :            :                                    EXT4_I(inode)->i_reserved_data_blocks);
    5802                 :    3502626 :         stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
    5803                 :    3502626 :         return 0;
    5804                 :            : }
    5805                 :            : 
    5806                 :      33790 : static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
    5807                 :            :                                    int pextents)
    5808                 :            : {
    5809         [ -  + ]:      33790 :         if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
    5810                 :          0 :                 return ext4_ind_trans_blocks(inode, lblocks);
    5811                 :      33790 :         return ext4_ext_index_trans_blocks(inode, pextents);
    5812                 :            : }
    5813                 :            : 
    5814                 :            : /*
    5815                 :            :  * Account for index blocks, block groups bitmaps and block group
    5816                 :            :  * descriptor blocks if modify datablocks and index blocks
    5817                 :            :  * worse case, the indexs blocks spread over different block groups
    5818                 :            :  *
    5819                 :            :  * If datablocks are discontiguous, they are possible to spread over
    5820                 :            :  * different block groups too. If they are contiguous, with flexbg,
    5821                 :            :  * they could still across block group boundary.
    5822                 :            :  *
    5823                 :            :  * Also account for superblock, inode, quota and xattr blocks
    5824                 :            :  */
    5825                 :      33790 : static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
    5826                 :            :                                   int pextents)
    5827                 :            : {
    5828                 :      33790 :         ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
    5829                 :            :         int gdpblocks;
    5830                 :            :         int idxblocks;
    5831                 :            :         int ret = 0;
    5832                 :            : 
    5833                 :            :         /*
    5834                 :            :          * How many index blocks need to touch to map @lblocks logical blocks
    5835                 :            :          * to @pextents physical extents?
    5836                 :            :          */
    5837                 :      33790 :         idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
    5838                 :            : 
    5839                 :            :         ret = idxblocks;
    5840                 :            : 
    5841                 :            :         /*
    5842                 :            :          * Now let's see how many group bitmaps and group descriptors need
    5843                 :            :          * to account
    5844                 :            :          */
    5845                 :      33788 :         groups = idxblocks + pextents;
    5846                 :            :         gdpblocks = groups;
    5847         [ -  + ]:      33788 :         if (groups > ngroups)
    5848                 :            :                 groups = ngroups;
    5849         [ +  + ]:      67576 :         if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
    5850                 :       6056 :                 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
    5851                 :            : 
    5852                 :            :         /* bitmaps and block group descriptor blocks */
    5853                 :      33788 :         ret += groups + gdpblocks;
    5854                 :            : 
    5855                 :            :         /* Blocks for super block, inode, quota and xattr blocks */
    5856   [ +  -  +  - ]:      67577 :         ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
    5857                 :            : 
    5858                 :      33788 :         return ret;
    5859                 :            : }
    5860                 :            : 
    5861                 :            : /*
    5862                 :            :  * Calculate the total number of credits to reserve to fit
    5863                 :            :  * the modification of a single pages into a single transaction,
    5864                 :            :  * which may include multiple chunks of block allocations.
    5865                 :            :  *
    5866                 :            :  * This could be called via ext4_write_begin()
    5867                 :            :  *
    5868                 :            :  * We need to consider the worse case, when
    5869                 :            :  * one new block per extent.
    5870                 :            :  */
    5871                 :       3543 : int ext4_writepage_trans_blocks(struct inode *inode)
    5872                 :            : {
    5873                 :            :         int bpp = ext4_journal_blocks_per_page(inode);
    5874                 :            :         int ret;
    5875                 :            : 
    5876                 :       3543 :         ret = ext4_meta_trans_blocks(inode, bpp, bpp);
    5877                 :            : 
    5878                 :            :         /* Account for data blocks for journalled mode */
    5879         [ +  + ]:       3541 :         if (ext4_should_journal_data(inode))
    5880                 :       1460 :                 ret += bpp;
    5881                 :       3541 :         return ret;
    5882                 :            : }
    5883                 :            : 
    5884                 :            : /*
    5885                 :            :  * Calculate the journal credits for a chunk of data modification.
    5886                 :            :  *
    5887                 :            :  * This is called from DIO, fallocate or whoever calling
    5888                 :            :  * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
    5889                 :            :  *
    5890                 :            :  * journal buffers for data blocks are not included here, as DIO
    5891                 :            :  * and fallocate do no need to journal data buffers.
    5892                 :            :  */
    5893                 :         26 : int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
    5894                 :            : {
    5895                 :         26 :         return ext4_meta_trans_blocks(inode, nrblocks, 1);
    5896                 :            : }
    5897                 :            : 
    5898                 :            : /*
    5899                 :            :  * The caller must have previously called ext4_reserve_inode_write().
    5900                 :            :  * Give this, we know that the caller already has write access to iloc->bh.
    5901                 :            :  */
    5902                 :    1407283 : int ext4_mark_iloc_dirty(handle_t *handle,
    5903                 :            :                          struct inode *inode, struct ext4_iloc *iloc)
    5904                 :            : {
    5905                 :            :         int err = 0;
    5906                 :            : 
    5907         [ -  + ]:    2814566 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
    5908                 :          0 :                 put_bh(iloc->bh);
    5909                 :          0 :                 return -EIO;
    5910                 :            :         }
    5911         [ -  + ]:    1407283 :         if (IS_I_VERSION(inode))
    5912                 :            :                 inode_inc_iversion(inode);
    5913                 :            : 
    5914                 :            :         /* the do_update_inode consumes one bh->b_count */
    5915                 :    1407283 :         get_bh(iloc->bh);
    5916                 :            : 
    5917                 :            :         /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
    5918                 :    1407270 :         err = ext4_do_update_inode(handle, inode, iloc);
    5919                 :    1407247 :         put_bh(iloc->bh);
    5920                 :    1407252 :         return err;
    5921                 :            : }
    5922                 :            : 
    5923                 :            : /*
    5924                 :            :  * On success, We end up with an outstanding reference count against
    5925                 :            :  * iloc->bh.  This _must_ be cleaned up later.
    5926                 :            :  */
    5927                 :            : 
    5928                 :            : int
    5929                 :    1407275 : ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
    5930                 :            :                          struct ext4_iloc *iloc)
    5931                 :            : {
    5932                 :            :         int err;
    5933                 :            : 
    5934         [ +  + ]:    2814550 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
    5935                 :            :                 return -EIO;
    5936                 :            : 
    5937                 :            :         err = ext4_get_inode_loc(inode, iloc);
    5938         [ +  + ]:    1407290 :         if (!err) {
    5939                 :            :                 BUFFER_TRACE(iloc->bh, "get_write_access");
    5940                 :    1407263 :                 err = ext4_journal_get_write_access(handle, iloc->bh);
    5941         [ -  + ]:    1407263 :                 if (err) {
    5942                 :          0 :                         brelse(iloc->bh);
    5943                 :          0 :                         iloc->bh = NULL;
    5944                 :            :                 }
    5945                 :            :         }
    5946         [ -  + ]:    1407290 :         ext4_std_error(inode->i_sb, err);
    5947                 :    1407295 :         return err;
    5948                 :            : }
    5949                 :            : 
    5950                 :          0 : static int __ext4_expand_extra_isize(struct inode *inode,
    5951                 :            :                                      unsigned int new_extra_isize,
    5952                 :            :                                      struct ext4_iloc *iloc,
    5953                 :            :                                      handle_t *handle, int *no_expand)
    5954                 :            : {
    5955                 :            :         struct ext4_inode *raw_inode;
    5956                 :            :         struct ext4_xattr_ibody_header *header;
    5957                 :          0 :         unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb);
    5958                 :            :         struct ext4_inode_info *ei = EXT4_I(inode);
    5959                 :            :         int error;
    5960                 :            : 
    5961                 :            :         /* this was checked at iget time, but double check for good measure */
    5962   [ #  #  #  # ]:          0 :         if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) ||
    5963                 :          0 :             (ei->i_extra_isize & 3)) {
    5964                 :          0 :                 EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)",
    5965                 :            :                                  ei->i_extra_isize,
    5966                 :            :                                  EXT4_INODE_SIZE(inode->i_sb));
    5967                 :          0 :                 return -EFSCORRUPTED;
    5968                 :            :         }
    5969   [ #  #  #  # ]:          0 :         if ((new_extra_isize < ei->i_extra_isize) ||
    5970         [ #  # ]:          0 :             (new_extra_isize < 4) ||
    5971                 :          0 :             (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE))
    5972                 :            :                 return -EINVAL; /* Should never happen */
    5973                 :            : 
    5974                 :            :         raw_inode = ext4_raw_inode(iloc);
    5975                 :            : 
    5976                 :          0 :         header = IHDR(inode, raw_inode);
    5977                 :            : 
    5978                 :            :         /* No extended attributes present */
    5979   [ #  #  #  # ]:          0 :         if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
    5980                 :          0 :             header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
    5981                 :          0 :                 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
    5982                 :            :                        EXT4_I(inode)->i_extra_isize, 0,
    5983                 :            :                        new_extra_isize - EXT4_I(inode)->i_extra_isize);
    5984                 :          0 :                 EXT4_I(inode)->i_extra_isize = new_extra_isize;
    5985                 :          0 :                 return 0;
    5986                 :            :         }
    5987                 :            : 
    5988                 :            :         /* try to expand with EAs present */
    5989                 :          0 :         error = ext4_expand_extra_isize_ea(inode, new_extra_isize,
    5990                 :            :                                            raw_inode, handle);
    5991         [ #  # ]:          0 :         if (error) {
    5992                 :            :                 /*
    5993                 :            :                  * Inode size expansion failed; don't try again
    5994                 :            :                  */
    5995                 :          0 :                 *no_expand = 1;
    5996                 :            :         }
    5997                 :            : 
    5998                 :          0 :         return error;
    5999                 :            : }
    6000                 :            : 
    6001                 :            : /*
    6002                 :            :  * Expand an inode by new_extra_isize bytes.
    6003                 :            :  * Returns 0 on success or negative error number on failure.
    6004                 :            :  */
    6005                 :          0 : static int ext4_try_to_expand_extra_isize(struct inode *inode,
    6006                 :            :                                           unsigned int new_extra_isize,
    6007                 :            :                                           struct ext4_iloc iloc,
    6008                 :            :                                           handle_t *handle)
    6009                 :            : {
    6010                 :            :         int no_expand;
    6011                 :            :         int error;
    6012                 :            : 
    6013         [ #  # ]:          0 :         if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND))
    6014                 :            :                 return -EOVERFLOW;
    6015                 :            : 
    6016                 :            :         /*
    6017                 :            :          * In nojournal mode, we can immediately attempt to expand
    6018                 :            :          * the inode.  When journaled, we first need to obtain extra
    6019                 :            :          * buffer credits since we may write into the EA block
    6020                 :            :          * with this same handle. If journal_extend fails, then it will
    6021                 :            :          * only result in a minor loss of functionality for that inode.
    6022                 :            :          * If this is felt to be critical, then e2fsck should be run to
    6023                 :            :          * force a large enough s_min_extra_isize.
    6024                 :            :          */
    6025   [ #  #  #  # ]:          0 :         if (ext4_handle_valid(handle) &&
    6026                 :          0 :             jbd2_journal_extend(handle,
    6027   [ #  #  #  #  :          0 :                                 EXT4_DATA_TRANS_BLOCKS(inode->i_sb)) != 0)
                   #  # ]
    6028                 :            :                 return -ENOSPC;
    6029                 :            : 
    6030         [ #  # ]:          0 :         if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
    6031                 :            :                 return -EBUSY;
    6032                 :            : 
    6033                 :          0 :         error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc,
    6034                 :            :                                           handle, &no_expand);
    6035                 :          0 :         ext4_write_unlock_xattr(inode, &no_expand);
    6036                 :            : 
    6037                 :          0 :         return error;
    6038                 :            : }
    6039                 :            : 
    6040                 :          0 : int ext4_expand_extra_isize(struct inode *inode,
    6041                 :            :                             unsigned int new_extra_isize,
    6042                 :            :                             struct ext4_iloc *iloc)
    6043                 :            : {
    6044                 :            :         handle_t *handle;
    6045                 :            :         int no_expand;
    6046                 :            :         int error, rc;
    6047                 :            : 
    6048         [ #  # ]:          0 :         if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
    6049                 :          0 :                 brelse(iloc->bh);
    6050                 :            :                 return -EOVERFLOW;
    6051                 :            :         }
    6052                 :            : 
    6053   [ #  #  #  #  :          0 :         handle = ext4_journal_start(inode, EXT4_HT_INODE,
                   #  # ]
    6054                 :            :                                     EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
    6055         [ #  # ]:          0 :         if (IS_ERR(handle)) {
    6056                 :            :                 error = PTR_ERR(handle);
    6057                 :          0 :                 brelse(iloc->bh);
    6058                 :          0 :                 return error;
    6059                 :            :         }
    6060                 :            : 
    6061                 :          0 :         ext4_write_lock_xattr(inode, &no_expand);
    6062                 :            : 
    6063                 :            :         BUFFER_TRACE(iloc->bh, "get_write_access");
    6064                 :          0 :         error = ext4_journal_get_write_access(handle, iloc->bh);
    6065         [ #  # ]:          0 :         if (error) {
    6066                 :          0 :                 brelse(iloc->bh);
    6067                 :            :                 goto out_unlock;
    6068                 :            :         }
    6069                 :            : 
    6070                 :          0 :         error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc,
    6071                 :            :                                           handle, &no_expand);
    6072                 :            : 
    6073                 :          0 :         rc = ext4_mark_iloc_dirty(handle, inode, iloc);
    6074         [ #  # ]:          0 :         if (!error)
    6075                 :            :                 error = rc;
    6076                 :            : 
    6077                 :            : out_unlock:
    6078                 :          0 :         ext4_write_unlock_xattr(inode, &no_expand);
    6079                 :          0 :         ext4_journal_stop(handle);
    6080                 :          0 :         return error;
    6081                 :            : }
    6082                 :            : 
    6083                 :            : /*
    6084                 :            :  * What we do here is to mark the in-core inode as clean with respect to inode
    6085                 :            :  * dirtiness (it may still be data-dirty).
    6086                 :            :  * This means that the in-core inode may be reaped by prune_icache
    6087                 :            :  * without having to perform any I/O.  This is a very good thing,
    6088                 :            :  * because *any* task may call prune_icache - even ones which
    6089                 :            :  * have a transaction open against a different journal.
    6090                 :            :  *
    6091                 :            :  * Is this cheating?  Not really.  Sure, we haven't written the
    6092                 :            :  * inode out, but prune_icache isn't a user-visible syncing function.
    6093                 :            :  * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
    6094                 :            :  * we start and wait on commits.
    6095                 :            :  */
    6096                 :    1399001 : int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
    6097                 :            : {
    6098                 :            :         struct ext4_iloc iloc;
    6099                 :    1399001 :         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
    6100                 :            :         int err;
    6101                 :            : 
    6102                 :    1399001 :         might_sleep();
    6103                 :    1399000 :         trace_ext4_mark_inode_dirty(inode, _RET_IP_);
    6104                 :    1398996 :         err = ext4_reserve_inode_write(handle, inode, &iloc);
    6105         [ +  + ]:    1399019 :         if (err)
    6106                 :            :                 return err;
    6107                 :            : 
    6108         [ -  + ]:    1399011 :         if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize)
    6109                 :          0 :                 ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize,
    6110                 :            :                                                iloc, handle);
    6111                 :            : 
    6112                 :    1399011 :         return ext4_mark_iloc_dirty(handle, inode, &iloc);
    6113                 :            : }
    6114                 :            : 
    6115                 :            : /*
    6116                 :            :  * ext4_dirty_inode() is called from __mark_inode_dirty()
    6117                 :            :  *
    6118                 :            :  * We're really interested in the case where a file is being extended.
    6119                 :            :  * i_size has been changed by generic_commit_write() and we thus need
    6120                 :            :  * to include the updated inode in the current transaction.
    6121                 :            :  *
    6122                 :            :  * Also, dquot_alloc_block() will always dirty the inode when blocks
    6123                 :            :  * are allocated to the file.
    6124                 :            :  *
    6125                 :            :  * If the inode is marked synchronous, we don't honour that here - doing
    6126                 :            :  * so would cause a commit on atime updates, which we don't bother doing.
    6127                 :            :  * We handle synchronous inodes at the highest possible level.
    6128                 :            :  *
    6129                 :            :  * If only the I_DIRTY_TIME flag is set, we can skip everything.  If
    6130                 :            :  * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
    6131                 :            :  * to copy into the on-disk inode structure are the timestamp files.
    6132                 :            :  */
    6133                 :     649214 : void ext4_dirty_inode(struct inode *inode, int flags)
    6134                 :            : {
    6135                 :            :         handle_t *handle;
    6136                 :            : 
    6137         [ +  + ]:     649214 :         if (flags == I_DIRTY_TIME)
    6138                 :            :                 return;
    6139                 :            :         handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
    6140         [ +  + ]:     649215 :         if (IS_ERR(handle))
    6141                 :            :                 goto out;
    6142                 :            : 
    6143                 :     649212 :         ext4_mark_inode_dirty(handle, inode);
    6144                 :            : 
    6145                 :     649193 :         ext4_journal_stop(handle);
    6146                 :            : out:
    6147                 :            :         return;
    6148                 :            : }
    6149                 :            : 
    6150                 :          0 : int ext4_change_inode_journal_flag(struct inode *inode, int val)
    6151                 :            : {
    6152                 :            :         journal_t *journal;
    6153                 :            :         handle_t *handle;
    6154                 :            :         int err;
    6155                 :          0 :         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
    6156                 :            : 
    6157                 :            :         /*
    6158                 :            :          * We have to be very careful here: changing a data block's
    6159                 :            :          * journaling status dynamically is dangerous.  If we write a
    6160                 :            :          * data block to the journal, change the status and then delete
    6161                 :            :          * that block, we risk forgetting to revoke the old log record
    6162                 :            :          * from the journal and so a subsequent replay can corrupt data.
    6163                 :            :          * So, first we make sure that the journal is empty and that
    6164                 :            :          * nobody is changing anything.
    6165                 :            :          */
    6166                 :            : 
    6167                 :          0 :         journal = EXT4_JOURNAL(inode);
    6168         [ #  # ]:          0 :         if (!journal)
    6169                 :            :                 return 0;
    6170         [ #  # ]:          0 :         if (is_journal_aborted(journal))
    6171                 :            :                 return -EROFS;
    6172                 :            : 
    6173                 :            :         /* Wait for all existing dio workers */
    6174                 :          0 :         inode_dio_wait(inode);
    6175                 :            : 
    6176                 :            :         /*
    6177                 :            :          * Before flushing the journal and switching inode's aops, we have
    6178                 :            :          * to flush all dirty data the inode has. There can be outstanding
    6179                 :            :          * delayed allocations, there can be unwritten extents created by
    6180                 :            :          * fallocate or buffered writes in dioread_nolock mode covered by
    6181                 :            :          * dirty data which can be converted only after flushing the dirty
    6182                 :            :          * data (and journalled aops don't know how to handle these cases).
    6183                 :            :          */
    6184         [ #  # ]:          0 :         if (val) {
    6185                 :          0 :                 down_write(&EXT4_I(inode)->i_mmap_sem);
    6186                 :          0 :                 err = filemap_write_and_wait(inode->i_mapping);
    6187         [ #  # ]:          0 :                 if (err < 0) {
    6188                 :          0 :                         up_write(&EXT4_I(inode)->i_mmap_sem);
    6189                 :          0 :                         return err;
    6190                 :            :                 }
    6191                 :            :         }
    6192                 :            : 
    6193                 :          0 :         percpu_down_write(&sbi->s_writepages_rwsem);
    6194                 :          0 :         jbd2_journal_lock_updates(journal);
    6195                 :            : 
    6196                 :            :         /*
    6197                 :            :          * OK, there are no updates running now, and all cached data is
    6198                 :            :          * synced to disk.  We are now in a completely consistent state
    6199                 :            :          * which doesn't have anything in the journal, and we know that
    6200                 :            :          * no filesystem updates are running, so it is safe to modify
    6201                 :            :          * the inode's in-core data-journaling state flag now.
    6202                 :            :          */
    6203                 :            : 
    6204         [ #  # ]:          0 :         if (val)
    6205                 :            :                 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
    6206                 :            :         else {
    6207                 :          0 :                 err = jbd2_journal_flush(journal);
    6208         [ #  # ]:          0 :                 if (err < 0) {
    6209                 :          0 :                         jbd2_journal_unlock_updates(journal);
    6210                 :          0 :                         percpu_up_write(&sbi->s_writepages_rwsem);
    6211                 :          0 :                         return err;
    6212                 :            :                 }
    6213                 :            :                 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
    6214                 :            :         }
    6215                 :          0 :         ext4_set_aops(inode);
    6216                 :            : 
    6217                 :          0 :         jbd2_journal_unlock_updates(journal);
    6218                 :          0 :         percpu_up_write(&sbi->s_writepages_rwsem);
    6219                 :            : 
    6220         [ #  # ]:          0 :         if (val)
    6221                 :          0 :                 up_write(&EXT4_I(inode)->i_mmap_sem);
    6222                 :            : 
    6223                 :            :         /* Finally we can mark the inode as dirty. */
    6224                 :            : 
    6225                 :            :         handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
    6226         [ #  # ]:          0 :         if (IS_ERR(handle))
    6227                 :          0 :                 return PTR_ERR(handle);
    6228                 :            : 
    6229                 :          0 :         err = ext4_mark_inode_dirty(handle, inode);
    6230                 :            :         ext4_handle_sync(handle);
    6231                 :          0 :         ext4_journal_stop(handle);
    6232         [ #  # ]:          0 :         ext4_std_error(inode->i_sb, err);
    6233                 :            : 
    6234                 :          0 :         return err;
    6235                 :            : }
    6236                 :            : 
    6237                 :          0 : static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
    6238                 :            : {
    6239                 :          0 :         return !buffer_mapped(bh);
    6240                 :            : }
    6241                 :            : 
    6242                 :       1090 : vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
    6243                 :            : {
    6244                 :       1090 :         struct vm_area_struct *vma = vmf->vma;
    6245                 :       1090 :         struct page *page = vmf->page;
    6246                 :            :         loff_t size;
    6247                 :            :         unsigned long len;
    6248                 :            :         int err;
    6249                 :            :         vm_fault_t ret;
    6250                 :       1090 :         struct file *file = vma->vm_file;
    6251                 :            :         struct inode *inode = file_inode(file);
    6252                 :       1090 :         struct address_space *mapping = inode->i_mapping;
    6253                 :            :         handle_t *handle;
    6254                 :            :         get_block_t *get_block;
    6255                 :       1090 :         int retries = 0;
    6256                 :            : 
    6257         [ +  - ]:       1090 :         if (unlikely(IS_IMMUTABLE(inode)))
    6258                 :            :                 return VM_FAULT_SIGBUS;
    6259                 :            : 
    6260                 :       1090 :         sb_start_pagefault(inode->i_sb);
    6261                 :       1090 :         file_update_time(vma->vm_file);
    6262                 :            : 
    6263                 :       1090 :         down_read(&EXT4_I(inode)->i_mmap_sem);
    6264                 :            : 
    6265                 :       1090 :         err = ext4_convert_inline_data(inode);
    6266         [ +  - ]:       1090 :         if (err)
    6267                 :            :                 goto out_ret;
    6268                 :            : 
    6269                 :            :         /* Delalloc case is easy... */
    6270   [ +  -  +  - ]:       3270 :         if (test_opt(inode->i_sb, DELALLOC) &&
    6271         [ +  - ]:       1090 :             !ext4_should_journal_data(inode) &&
    6272                 :       1090 :             !ext4_nonda_switch(inode->i_sb)) {
    6273                 :            :                 do {
    6274                 :       1090 :                         err = block_page_mkwrite(vma, vmf,
    6275                 :            :                                                    ext4_da_get_block_prep);
    6276         [ #  # ]:          0 :                 } while (err == -ENOSPC &&
    6277         [ -  + ]:       1090 :                        ext4_should_retry_alloc(inode->i_sb, &retries));
    6278                 :            :                 goto out_ret;
    6279                 :            :         }
    6280                 :            : 
    6281                 :          0 :         lock_page(page);
    6282                 :            :         size = i_size_read(inode);
    6283                 :            :         /* Page got truncated from under us? */
    6284   [ #  #  #  # ]:          0 :         if (page->mapping != mapping || page_offset(page) > size) {
    6285                 :          0 :                 unlock_page(page);
    6286                 :            :                 ret = VM_FAULT_NOPAGE;
    6287                 :          0 :                 goto out;
    6288                 :            :         }
    6289                 :            : 
    6290         [ #  # ]:          0 :         if (page->index == size >> PAGE_SHIFT)
    6291                 :          0 :                 len = size & ~PAGE_MASK;
    6292                 :            :         else
    6293                 :            :                 len = PAGE_SIZE;
    6294                 :            :         /*
    6295                 :            :          * Return if we have all the buffers mapped. This avoids the need to do
    6296                 :            :          * journal_start/journal_stop which can block and take a long time
    6297                 :            :          */
    6298         [ #  # ]:          0 :         if (page_has_buffers(page)) {
    6299   [ #  #  #  # ]:          0 :                 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
    6300                 :            :                                             0, len, NULL,
    6301                 :            :                                             ext4_bh_unmapped)) {
    6302                 :            :                         /* Wait so that we don't change page under IO */
    6303                 :          0 :                         wait_for_stable_page(page);
    6304                 :            :                         ret = VM_FAULT_LOCKED;
    6305                 :          0 :                         goto out;
    6306                 :            :                 }
    6307                 :            :         }
    6308                 :          0 :         unlock_page(page);
    6309                 :            :         /* OK, we need to fill the hole... */
    6310         [ #  # ]:          0 :         if (ext4_should_dioread_nolock(inode))
    6311                 :            :                 get_block = ext4_get_block_unwritten;
    6312                 :            :         else
    6313                 :            :                 get_block = ext4_get_block;
    6314                 :            : retry_alloc:
    6315                 :          0 :         handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
    6316                 :            :                                     ext4_writepage_trans_blocks(inode));
    6317         [ #  # ]:          0 :         if (IS_ERR(handle)) {
    6318                 :            :                 ret = VM_FAULT_SIGBUS;
    6319                 :            :                 goto out;
    6320                 :            :         }
    6321                 :          0 :         err = block_page_mkwrite(vma, vmf, get_block);
    6322   [ #  #  #  # ]:          0 :         if (!err && ext4_should_journal_data(inode)) {
    6323   [ #  #  #  # ]:          0 :                 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
    6324                 :            :                           PAGE_SIZE, NULL, do_journal_get_write_access)) {
    6325                 :          0 :                         unlock_page(page);
    6326                 :            :                         ret = VM_FAULT_SIGBUS;
    6327                 :          0 :                         ext4_journal_stop(handle);
    6328                 :          0 :                         goto out;
    6329                 :            :                 }
    6330                 :            :                 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
    6331                 :            :         }
    6332                 :          0 :         ext4_journal_stop(handle);
    6333   [ #  #  #  # ]:          0 :         if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
    6334                 :            :                 goto retry_alloc;
    6335                 :            : out_ret:
    6336                 :            :         ret = block_page_mkwrite_return(err);
    6337                 :            : out:
    6338                 :       1090 :         up_read(&EXT4_I(inode)->i_mmap_sem);
    6339                 :       1090 :         sb_end_pagefault(inode->i_sb);
    6340                 :       1090 :         return ret;
    6341                 :            : }
    6342                 :            : 
    6343                 :    2426690 : vm_fault_t ext4_filemap_fault(struct vm_fault *vmf)
    6344                 :            : {
    6345                 :    2426690 :         struct inode *inode = file_inode(vmf->vma->vm_file);
    6346                 :            :         vm_fault_t ret;
    6347                 :            : 
    6348                 :    2426690 :         down_read(&EXT4_I(inode)->i_mmap_sem);
    6349                 :    2432423 :         ret = filemap_fault(vmf);
    6350                 :    2433171 :         up_read(&EXT4_I(inode)->i_mmap_sem);
    6351                 :            : 
    6352                 :    2432999 :         return ret;
    6353                 :            : }

Generated by: LCOV version 1.14