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