~ubuntu-branches/ubuntu/karmic/linux-mvl-dove/karmic-proposed

« back to all changes in this revision

Viewing changes to fs/ext4/inode.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Bader, Stefan Bader, Ubuntu: 2.6.31-21.59
  • Date: 2010-03-17 23:42:27 UTC
  • mfrom: (15.1.2 karmic-security)
  • Revision ID: james.westby@ubuntu.com-20100317234227-s08ijis2wxmnz4mj
Tags: 2.6.31-213.27
[ Stefan Bader ]

* Rebased to 2.6.31-21.59

[ Ubuntu: 2.6.31-21.59 ]

* [Config] generic-pae switch to M586TSC
  - LP: #519448
* (pre-stable) drm/i915: Increase fb alignment to 64k
  - LP: #404064
* Input: i8042 - bypass AUX IRQ delivery test on laptops
  - LP: #534448
* SAUCE: Fix volume hotkeys for Dell Studio 1557
  - LP: #465250
* SAUCE: aufs: Fix header files inclusion in debug.h
  - LP: #517151
* [Config] Enable all CGROUP configuration options
  - LP: #480739
* Revert "[Upstream] acerhdf: Limit modalias matching to supported
  boards"
  - LP: #509730
* [Config] ext3 defaults to ordered mode
  - LP: #510067
* [Config] Fix sub-flavours package conflicts
  - LP: #454827
* PCI/cardbus: Add a fixup hook and fix powerpc
  - LP: #455723
* fnctl: f_modown should call write_lock_irqsave/restore
  - LP: #519436
* ACPI: enable C2 and Turbo-mode on Nehalem notebooks on A/C
  - LP: #516325
* tg3: Add 57788, remove 57720
  - LP: #515390
* HID: ignore all recent SoundGraph iMON devices
  - LP: #488443
* Input: ALPS - add interleaved protocol support (Dell E6x00 series)
  - LP: #296610
* acerhdf: limit modalias matching to supported
  - LP: #509730
* ASoC: Do not write to invalid registers on the wm9712.
  - LP: #509730
* cifs: NULL out tcon, pSesInfo, and srvTcp pointers when chasing DFS
  referrals
  - LP: #509730
* clockevents: Prevent clockevent_devices list corruption on cpu hotplug
  - LP: #509730
* dma: at_hdmac: correct incompatible type for argument 1 of
  'spin_lock_bh'
  - LP: #509730
* drivers/net/usb: Correct code taking the size of a pointer
  - LP: #509730
* Libertas: fix buffer overflow in lbs_get_essid()
  - LP: #509730
* md: Fix unfortunate interaction with evms
  - LP: #509730
* pata_cmd64x: fix overclocking of UDMA0-2 modes
  - LP: #509730
* pata_hpt3x2n: fix clock turnaround
  - LP: #509730
* SCSI: fc class: fix fc_transport_init error handling
  - LP: #509730
* sound: sgio2audio/pdaudiocf/usb-audio: initialize PCM buffer
  - LP: #509730
* USB: emi62: fix crash when trying to load EMI 6|2 firmware
  - LP: #509730
* USB: Fix a bug on appledisplay.c regarding signedness
  - LP: #509730
* USB: musb: gadget_ep0: avoid SetupEnd interrupt
  - LP: #509730
* USB: option: support hi speed for modem Haier CE100
  - LP: #490068, #509730
* x86, cpuid: Add "volatile" to asm in native_cpuid()
  - LP: #509730
* e100: Use pci pool to work around GFP_ATOMIC order 5 memory allocation
  failure
  - LP: #509730
* e100: Fix broken cbs accounting due to missing memset.
  - LP: #509730
* hostap: Revert a toxic part of the conversion to net_device_ops
  - LP: #509730
* hwmon: (fschmd) Fix check on unsigned in watchdog_write()
  - LP: #509730
* hwmon: (sht15) Off-by-one error in array index + incorrect constants
  - LP: #509730
* i2c/tsl2550: Fix lux value in extended mode
  - LP: #509730
* ipv6: reassembly: use seperate reassembly queues for conntrack and
  local delivery
  - LP: #509730
* S390: dasd: support DIAG access for read-only devices
  - LP: #509730
* udf: Try harder when looking for VAT inode
  - LP: #509730
* V4L/DVB (13596): ov511.c typo: lock => unlock
  - LP: #509730
* x86/ptrace: make genregs[32]_get/set more robust
  - LP: #509730
* XFS bug in log recover with quota (bugzilla id 855)
  - LP: #509730
* generic_permission: MAY_OPEN is not write access
  - LP: #509730
* memcg: avoid oom-killing innocent task in case of use_hierarchy
  - LP: #509730
* Input: atkbd - add force relese key quirk for Samsung R59P/R60P/R61P
  - LP: #253874, #509730
* Add unlocked version of inode_add_bytes() function
  - LP: #509730
* ext4: fix sleep inside spinlock issue with quota and dealloc (#14739)
  - LP: #509730
* Linux 2.6.31.10
  - LP: #509730
* Linux 2.6.31.11
  - LP: #509730
* quota: decouple fs reserved space from quota reservation
  - LP: #510674
* ext4: Convert to generic reserved quota's space management.
  - LP: #510674
* hwmon: (adt7462) Fix pin 28 monitoring
  - LP: #510674
* netfilter: nf_ct_ftp: fix out of bounds read in update_nl_seq()
  - LP: #510674
* quota: Fix dquot_transfer for filesystems different from ext4
  - LP: #510674
* fix braindamage in audit_tree.c untag_chunk()
  - LP: #510674
* fix more leaks in audit_tree.c tag_chunk()
  - LP: #510674
* ACPI: sleep: another HP DMI entry for init_set_sci_en_on_resume
  - LP: #453963, #510674
* ACPI: add DMI entry for SCI_EN resume quirk on HP dv4
  - LP: #453963, #510674
* ACPI: sleep: another HP/Compaq DMI entries for
  init_set_sci_en_on_resume
  - LP: #453963, #510674
* ACPI: DMI init_set_sci_en_on_resume for HP-Compaq C700
  - LP: #453963, #510674
* Linux 2.6.31.12
  - LP: #510674

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include <linux/namei.h>
38
38
#include <linux/uio.h>
39
39
#include <linux/bio.h>
 
40
#include <linux/workqueue.h>
40
41
 
41
42
#include "ext4_jbd2.h"
42
43
#include "xattr.h"
192
193
 * so before we call here everything must be consistently dirtied against
193
194
 * this transaction.
194
195
 */
195
 
static int ext4_journal_test_restart(handle_t *handle, struct inode *inode)
 
196
int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
 
197
                                 int nblocks)
196
198
{
 
199
        int ret;
 
200
 
 
201
        /*
 
202
         * Drop i_data_sem to avoid deadlock with ext4_get_blocks At this
 
203
         * moment, get_block can be called only for blocks inside i_size since
 
204
         * page cache has been already dropped and writes are blocked by
 
205
         * i_mutex. So we can safely drop the i_data_sem here.
 
206
         */
197
207
        BUG_ON(EXT4_JOURNAL(inode) == NULL);
198
208
        jbd_debug(2, "restarting handle %p\n", handle);
199
 
        return ext4_journal_restart(handle, blocks_for_truncate(inode));
 
209
        up_write(&EXT4_I(inode)->i_data_sem);
 
210
        ret = ext4_journal_restart(handle, blocks_for_truncate(inode));
 
211
        down_write(&EXT4_I(inode)->i_data_sem);
 
212
        ext4_discard_preallocations(inode);
 
213
 
 
214
        return ret;
200
215
}
201
216
 
202
217
/*
551
566
 *
552
567
 *      Normally this function find the preferred place for block allocation,
553
568
 *      returns it.
 
569
 *      Because this is only used for non-extent files, we limit the block nr
 
570
 *      to 32 bits.
554
571
 */
555
572
static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
556
573
                                   Indirect *partial)
557
574
{
 
575
        ext4_fsblk_t goal;
 
576
 
558
577
        /*
559
578
         * XXX need to get goal block from mballoc's data structures
560
579
         */
561
580
 
562
 
        return ext4_find_near(inode, partial);
 
581
        goal = ext4_find_near(inode, partial);
 
582
        goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
 
583
        return goal;
563
584
}
564
585
 
565
586
/**
640
661
                if (*err)
641
662
                        goto failed_out;
642
663
 
 
664
                BUG_ON(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS);
 
665
 
643
666
                target -= count;
644
667
                /* allocate blocks for indirect blocks */
645
668
                while (index < indirect_blks && count) {
674
697
                ar.flags = EXT4_MB_HINT_DATA;
675
698
 
676
699
        current_block = ext4_mb_new_blocks(handle, &ar, err);
 
700
        BUG_ON(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS);
677
701
 
678
702
        if (*err && (target == blks)) {
679
703
                /*
998
1022
        if (!err)
999
1023
                err = ext4_splice_branch(handle, inode, iblock,
1000
1024
                                         partial, indirect_blks, count);
1001
 
        else
 
1025
        if (err)
1002
1026
                goto cleanup;
1003
1027
 
1004
1028
        set_buffer_new(bh_result);
 
1029
 
 
1030
        ext4_update_inode_fsync_trans(handle, inode, 1);
1005
1031
got_it:
1006
1032
        map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
1007
1033
        if (count > blocks_to_boundary)
1020
1046
        return err;
1021
1047
}
1022
1048
 
1023
 
qsize_t ext4_get_reserved_space(struct inode *inode)
 
1049
#ifdef CONFIG_QUOTA
 
1050
qsize_t *ext4_get_reserved_space(struct inode *inode)
1024
1051
{
1025
 
        unsigned long long total;
1026
 
 
1027
 
        spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1028
 
        total = EXT4_I(inode)->i_reserved_data_blocks +
1029
 
                EXT4_I(inode)->i_reserved_meta_blocks;
1030
 
        spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1031
 
 
1032
 
        return total;
 
1052
        return &EXT4_I(inode)->i_reserved_quota;
1033
1053
}
 
1054
#endif
1034
1055
/*
1035
1056
 * Calculate the number of metadata blocks need to reserve
1036
1057
 * to allocate @blocks for non extent file based file
1109
1130
                ext4_discard_preallocations(inode);
1110
1131
}
1111
1132
 
1112
 
static int check_block_validity(struct inode *inode, sector_t logical,
1113
 
                                sector_t phys, int len)
 
1133
static int check_block_validity(struct inode *inode, const char *msg,
 
1134
                                sector_t logical, sector_t phys, int len)
1114
1135
{
1115
1136
        if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) {
1116
 
                ext4_error(inode->i_sb, "check_block_validity",
 
1137
                ext4_error(inode->i_sb, msg,
1117
1138
                           "inode #%lu logical block %llu mapped to %llu "
1118
1139
                           "(size %d)", inode->i_ino,
1119
1140
                           (unsigned long long) logical,
1120
1141
                           (unsigned long long) phys, len);
1121
 
                WARN_ON(1);
1122
1142
                return -EIO;
1123
1143
        }
1124
1144
        return 0;
1125
1145
}
1126
1146
 
1127
1147
/*
 
1148
 * Return the number of contiguous dirty pages in a given inode
 
1149
 * starting at page frame idx.
 
1150
 */
 
1151
static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
 
1152
                                    unsigned int max_pages)
 
1153
{
 
1154
        struct address_space *mapping = inode->i_mapping;
 
1155
        pgoff_t index;
 
1156
        struct pagevec pvec;
 
1157
        pgoff_t num = 0;
 
1158
        int i, nr_pages, done = 0;
 
1159
 
 
1160
        if (max_pages == 0)
 
1161
                return 0;
 
1162
        pagevec_init(&pvec, 0);
 
1163
        while (!done) {
 
1164
                index = idx;
 
1165
                nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
 
1166
                                              PAGECACHE_TAG_DIRTY,
 
1167
                                              (pgoff_t)PAGEVEC_SIZE);
 
1168
                if (nr_pages == 0)
 
1169
                        break;
 
1170
                for (i = 0; i < nr_pages; i++) {
 
1171
                        struct page *page = pvec.pages[i];
 
1172
                        struct buffer_head *bh, *head;
 
1173
 
 
1174
                        lock_page(page);
 
1175
                        if (unlikely(page->mapping != mapping) ||
 
1176
                            !PageDirty(page) ||
 
1177
                            PageWriteback(page) ||
 
1178
                            page->index != idx) {
 
1179
                                done = 1;
 
1180
                                unlock_page(page);
 
1181
                                break;
 
1182
                        }
 
1183
                        if (page_has_buffers(page)) {
 
1184
                                bh = head = page_buffers(page);
 
1185
                                do {
 
1186
                                        if (!buffer_delay(bh) &&
 
1187
                                            !buffer_unwritten(bh))
 
1188
                                                done = 1;
 
1189
                                        bh = bh->b_this_page;
 
1190
                                } while (!done && (bh != head));
 
1191
                        }
 
1192
                        unlock_page(page);
 
1193
                        if (done)
 
1194
                                break;
 
1195
                        idx++;
 
1196
                        num++;
 
1197
                        if (num >= max_pages)
 
1198
                                break;
 
1199
                }
 
1200
                pagevec_release(&pvec);
 
1201
        }
 
1202
        return num;
 
1203
}
 
1204
 
 
1205
/*
1128
1206
 * The ext4_get_blocks() function tries to look up the requested blocks,
1129
1207
 * and returns if the blocks are already mapped.
1130
1208
 *
1155
1233
        clear_buffer_mapped(bh);
1156
1234
        clear_buffer_unwritten(bh);
1157
1235
 
 
1236
        ext_debug("ext4_get_blocks(): inode %lu, flag %d, max_blocks %u,"
 
1237
                  "logical block %lu\n", inode->i_ino, flags, max_blocks,
 
1238
                  (unsigned long)block);
1158
1239
        /*
1159
1240
         * Try to see if we can get the block without requesting a new
1160
1241
         * file system block.
1170
1251
        up_read((&EXT4_I(inode)->i_data_sem));
1171
1252
 
1172
1253
        if (retval > 0 && buffer_mapped(bh)) {
1173
 
                int ret = check_block_validity(inode, block,
1174
 
                                               bh->b_blocknr, retval);
 
1254
                int ret = check_block_validity(inode, "file system corruption",
 
1255
                                               block, bh->b_blocknr, retval);
1175
1256
                if (ret != 0)
1176
1257
                        return ret;
1177
1258
        }
1235
1316
                         * i_data's format changing.  Force the migrate
1236
1317
                         * to fail by clearing migrate flags
1237
1318
                         */
1238
 
                        EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
1239
 
                                                        ~EXT4_EXT_MIGRATE;
 
1319
                        EXT4_I(inode)->i_state &= ~EXT4_STATE_EXT_MIGRATE;
1240
1320
                }
1241
1321
        }
1242
1322
 
1252
1332
 
1253
1333
        up_write((&EXT4_I(inode)->i_data_sem));
1254
1334
        if (retval > 0 && buffer_mapped(bh)) {
1255
 
                int ret = check_block_validity(inode, block,
1256
 
                                               bh->b_blocknr, retval);
 
1335
                int ret = check_block_validity(inode, "file system "
 
1336
                                               "corruption after allocation",
 
1337
                                               block, bh->b_blocknr, retval);
1257
1338
                if (ret != 0)
1258
1339
                        return ret;
1259
1340
        }
1451
1532
        return ext4_journal_get_write_access(handle, bh);
1452
1533
}
1453
1534
 
 
1535
/*
 
1536
 * Truncate blocks that were not used by write. We have to truncate the
 
1537
 * pagecache as well so that corresponding buffers get properly unmapped.
 
1538
 */
 
1539
static void ext4_truncate_failed_write(struct inode *inode)
 
1540
{
 
1541
        truncate_inode_pages(inode->i_mapping, inode->i_size);
 
1542
        ext4_truncate(inode);
 
1543
}
 
1544
 
1454
1545
static int ext4_write_begin(struct file *file, struct address_space *mapping,
1455
1546
                            loff_t pos, unsigned len, unsigned flags,
1456
1547
                            struct page **pagep, void **fsdata)
1516
1607
 
1517
1608
                ext4_journal_stop(handle);
1518
1609
                if (pos + len > inode->i_size) {
1519
 
                        ext4_truncate(inode);
 
1610
                        ext4_truncate_failed_write(inode);
1520
1611
                        /*
1521
1612
                         * If truncate failed early the inode might
1522
1613
                         * still be on the orphan list; we need to
1626
1717
                ret = ret2;
1627
1718
 
1628
1719
        if (pos + len > inode->i_size) {
1629
 
                ext4_truncate(inode);
 
1720
                ext4_truncate_failed_write(inode);
1630
1721
                /*
1631
1722
                 * If truncate failed early the inode might still be
1632
1723
                 * on the orphan list; we need to make sure the inode
1668
1759
                ret = ret2;
1669
1760
 
1670
1761
        if (pos + len > inode->i_size) {
1671
 
                ext4_truncate(inode);
 
1762
                ext4_truncate_failed_write(inode);
1672
1763
                /*
1673
1764
                 * If truncate failed early the inode might still be
1674
1765
                 * on the orphan list; we need to make sure the inode
1731
1822
        if (!ret)
1732
1823
                ret = ret2;
1733
1824
        if (pos + len > inode->i_size) {
1734
 
                ext4_truncate(inode);
 
1825
                ext4_truncate_failed_write(inode);
1735
1826
                /*
1736
1827
                 * If truncate failed early the inode might still be
1737
1828
                 * on the orphan list; we need to make sure the inode
1763
1854
 
1764
1855
        md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
1765
1856
        total = md_needed + nrblocks;
 
1857
        spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1766
1858
 
1767
1859
        /*
1768
1860
         * Make quota reservation here to prevent quota overflow
1769
1861
         * later. Real quota accounting is done at pages writeout
1770
1862
         * time.
1771
1863
         */
1772
 
        if (vfs_dq_reserve_block(inode, total)) {
1773
 
                spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
 
1864
        if (vfs_dq_reserve_block(inode, total))
1774
1865
                return -EDQUOT;
1775
 
        }
1776
1866
 
1777
1867
        if (ext4_claim_free_blocks(sbi, total)) {
1778
 
                spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
 
1868
                vfs_dq_release_reservation_block(inode, total);
1779
1869
                if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1780
1870
                        yield();
1781
1871
                        goto repeat;
1782
1872
                }
1783
 
                vfs_dq_release_reservation_block(inode, total);
1784
1873
                return -ENOSPC;
1785
1874
        }
 
1875
        spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1786
1876
        EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
1787
 
        EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
 
1877
        EXT4_I(inode)->i_reserved_meta_blocks += md_needed;
 
1878
        spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1788
1879
 
1789
 
        spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1790
1880
        return 0;       /* success */
1791
1881
}
1792
1882
 
1860
1950
}
1861
1951
 
1862
1952
/*
1863
 
 * Delayed allocation stuff
1864
 
 */
1865
 
 
1866
 
struct mpage_da_data {
1867
 
        struct inode *inode;
1868
 
        sector_t b_blocknr;             /* start block number of extent */
1869
 
        size_t b_size;                  /* size of extent */
1870
 
        unsigned long b_state;          /* state of the extent */
1871
 
        unsigned long first_page, next_page;    /* extent of pages */
1872
 
        struct writeback_control *wbc;
1873
 
        int io_done;
1874
 
        int pages_written;
1875
 
        int retval;
1876
 
};
1877
 
 
1878
 
/*
1879
1953
 * mpage_da_submit_io - walks through extent of pages and try to write
1880
1954
 * them with writepage() call back
1881
1955
 *
2717
2791
         * number of contiguous block. So we will limit
2718
2792
         * number of contiguous block to a sane value
2719
2793
         */
2720
 
        if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
 
2794
        if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) &&
2721
2795
            (max_blocks > EXT4_MAX_TRANS_DATA))
2722
2796
                max_blocks = EXT4_MAX_TRANS_DATA;
2723
2797
 
2735
2809
        int no_nrwrite_index_update;
2736
2810
        int pages_written = 0;
2737
2811
        long pages_skipped;
 
2812
        unsigned int max_pages;
2738
2813
        int range_cyclic, cycled = 1, io_done = 0;
2739
 
        int needed_blocks, ret = 0, nr_to_writebump = 0;
 
2814
        int needed_blocks, ret = 0;
 
2815
        long desired_nr_to_write, nr_to_writebump = 0;
 
2816
        loff_t range_start = wbc->range_start;
2740
2817
        struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
2741
2818
 
2742
2819
        trace_ext4_da_writepages(inode, wbc);
2762
2839
        if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
2763
2840
                return -EROFS;
2764
2841
 
2765
 
        /*
2766
 
         * Make sure nr_to_write is >= sbi->s_mb_stream_request
2767
 
         * This make sure small files blocks are allocated in
2768
 
         * single attempt. This ensure that small files
2769
 
         * get less fragmented.
2770
 
         */
2771
 
        if (wbc->nr_to_write < sbi->s_mb_stream_request) {
2772
 
                nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
2773
 
                wbc->nr_to_write = sbi->s_mb_stream_request;
2774
 
        }
2775
2842
        if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2776
2843
                range_whole = 1;
2777
2844
 
2786
2853
        } else
2787
2854
                index = wbc->range_start >> PAGE_CACHE_SHIFT;
2788
2855
 
 
2856
        /*
 
2857
         * This works around two forms of stupidity.  The first is in
 
2858
         * the writeback code, which caps the maximum number of pages
 
2859
         * written to be 1024 pages.  This is wrong on multiple
 
2860
         * levels; different architectues have a different page size,
 
2861
         * which changes the maximum amount of data which gets
 
2862
         * written.  Secondly, 4 megabytes is way too small.  XFS
 
2863
         * forces this value to be 16 megabytes by multiplying
 
2864
         * nr_to_write parameter by four, and then relies on its
 
2865
         * allocator to allocate larger extents to make them
 
2866
         * contiguous.  Unfortunately this brings us to the second
 
2867
         * stupidity, which is that ext4's mballoc code only allocates
 
2868
         * at most 2048 blocks.  So we force contiguous writes up to
 
2869
         * the number of dirty blocks in the inode, or
 
2870
         * sbi->max_writeback_mb_bump whichever is smaller.
 
2871
         */
 
2872
        max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
 
2873
        if (!range_cyclic && range_whole)
 
2874
                desired_nr_to_write = wbc->nr_to_write * 8;
 
2875
        else
 
2876
                desired_nr_to_write = ext4_num_dirty_pages(inode, index,
 
2877
                                                           max_pages);
 
2878
        if (desired_nr_to_write > max_pages)
 
2879
                desired_nr_to_write = max_pages;
 
2880
 
 
2881
        if (wbc->nr_to_write < desired_nr_to_write) {
 
2882
                nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
 
2883
                wbc->nr_to_write = desired_nr_to_write;
 
2884
        }
 
2885
 
2789
2886
        mpd.wbc = wbc;
2790
2887
        mpd.inode = mapping->host;
2791
2888
 
2904
3001
out_writepages:
2905
3002
        if (!no_nrwrite_index_update)
2906
3003
                wbc->no_nrwrite_index_update = 0;
2907
 
        wbc->nr_to_write -= nr_to_writebump;
 
3004
        if (wbc->nr_to_write > nr_to_writebump)
 
3005
                wbc->nr_to_write -= nr_to_writebump;
 
3006
        wbc->range_start = range_start;
2908
3007
        trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
2909
3008
        return ret;
2910
3009
}
2994
3093
                 * i_size_read because we hold i_mutex.
2995
3094
                 */
2996
3095
                if (pos + len > inode->i_size)
2997
 
                        ext4_truncate(inode);
 
3096
                        ext4_truncate_failed_write(inode);
2998
3097
        }
2999
3098
 
3000
3099
        if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3259
3358
}
3260
3359
 
3261
3360
/*
 
3361
 * O_DIRECT for ext3 (or indirect map) based files
 
3362
 *
3262
3363
 * If the O_DIRECT write will extend the file then add this inode to the
3263
3364
 * orphan list.  So recovery will truncate it back to the original size
3264
3365
 * if the machine crashes during the write.
3267
3368
 * crashes then stale disk data _may_ be exposed inside the file. But current
3268
3369
 * VFS code falls back into buffered path in that case so we are safe.
3269
3370
 */
3270
 
static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
 
3371
static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb,
3271
3372
                              const struct iovec *iov, loff_t offset,
3272
3373
                              unsigned long nr_segs)
3273
3374
{
3278
3379
        ssize_t ret;
3279
3380
        int orphan = 0;
3280
3381
        size_t count = iov_length(iov, nr_segs);
 
3382
        int retries = 0;
3281
3383
 
3282
3384
        if (rw == WRITE) {
3283
3385
                loff_t final_size = offset + count;
3300
3402
                }
3301
3403
        }
3302
3404
 
 
3405
retry:
3303
3406
        ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3304
3407
                                 offset, nr_segs,
3305
3408
                                 ext4_get_block, NULL);
 
3409
        if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
 
3410
                goto retry;
3306
3411
 
3307
3412
        if (orphan) {
3308
3413
                int err;
3341
3446
        return ret;
3342
3447
}
3343
3448
 
 
3449
static int ext4_get_block_dio_write(struct inode *inode, sector_t iblock,
 
3450
                   struct buffer_head *bh_result, int create)
 
3451
{
 
3452
        handle_t *handle = NULL;
 
3453
        int ret = 0;
 
3454
        unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
 
3455
        int dio_credits;
 
3456
 
 
3457
        ext4_debug("ext4_get_block_dio_write: inode %lu, create flag %d\n",
 
3458
                   inode->i_ino, create);
 
3459
        /*
 
3460
         * DIO VFS code passes create = 0 flag for write to
 
3461
         * the middle of file. It does this to avoid block
 
3462
         * allocation for holes, to prevent expose stale data
 
3463
         * out when there is parallel buffered read (which does
 
3464
         * not hold the i_mutex lock) while direct IO write has
 
3465
         * not completed. DIO request on holes finally falls back
 
3466
         * to buffered IO for this reason.
 
3467
         *
 
3468
         * For ext4 extent based file, since we support fallocate,
 
3469
         * new allocated extent as uninitialized, for holes, we
 
3470
         * could fallocate blocks for holes, thus parallel
 
3471
         * buffered IO read will zero out the page when read on
 
3472
         * a hole while parallel DIO write to the hole has not completed.
 
3473
         *
 
3474
         * when we come here, we know it's a direct IO write to
 
3475
         * to the middle of file (<i_size)
 
3476
         * so it's safe to override the create flag from VFS.
 
3477
         */
 
3478
        create = EXT4_GET_BLOCKS_DIO_CREATE_EXT;
 
3479
 
 
3480
        if (max_blocks > DIO_MAX_BLOCKS)
 
3481
                max_blocks = DIO_MAX_BLOCKS;
 
3482
        dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
 
3483
        handle = ext4_journal_start(inode, dio_credits);
 
3484
        if (IS_ERR(handle)) {
 
3485
                ret = PTR_ERR(handle);
 
3486
                goto out;
 
3487
        }
 
3488
        ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
 
3489
                              create);
 
3490
        if (ret > 0) {
 
3491
                bh_result->b_size = (ret << inode->i_blkbits);
 
3492
                ret = 0;
 
3493
        }
 
3494
        ext4_journal_stop(handle);
 
3495
out:
 
3496
        return ret;
 
3497
}
 
3498
 
 
3499
static void ext4_free_io_end(ext4_io_end_t *io)
 
3500
{
 
3501
        BUG_ON(!io);
 
3502
        iput(io->inode);
 
3503
        kfree(io);
 
3504
}
 
3505
static void dump_aio_dio_list(struct inode * inode)
 
3506
{
 
3507
#ifdef  EXT4_DEBUG
 
3508
        struct list_head *cur, *before, *after;
 
3509
        ext4_io_end_t *io, *io0, *io1;
 
3510
 
 
3511
        if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){
 
3512
                ext4_debug("inode %lu aio dio list is empty\n", inode->i_ino);
 
3513
                return;
 
3514
        }
 
3515
 
 
3516
        ext4_debug("Dump inode %lu aio_dio_completed_IO list \n", inode->i_ino);
 
3517
        list_for_each_entry(io, &EXT4_I(inode)->i_aio_dio_complete_list, list){
 
3518
                cur = &io->list;
 
3519
                before = cur->prev;
 
3520
                io0 = container_of(before, ext4_io_end_t, list);
 
3521
                after = cur->next;
 
3522
                io1 = container_of(after, ext4_io_end_t, list);
 
3523
 
 
3524
                ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
 
3525
                            io, inode->i_ino, io0, io1);
 
3526
        }
 
3527
#endif
 
3528
}
 
3529
 
 
3530
/*
 
3531
 * check a range of space and convert unwritten extents to written.
 
3532
 */
 
3533
static int ext4_end_aio_dio_nolock(ext4_io_end_t *io)
 
3534
{
 
3535
        struct inode *inode = io->inode;
 
3536
        loff_t offset = io->offset;
 
3537
        size_t size = io->size;
 
3538
        int ret = 0;
 
3539
 
 
3540
        ext4_debug("end_aio_dio_onlock: io 0x%p from inode %lu,list->next 0x%p,"
 
3541
                   "list->prev 0x%p\n",
 
3542
                   io, inode->i_ino, io->list.next, io->list.prev);
 
3543
 
 
3544
        if (list_empty(&io->list))
 
3545
                return ret;
 
3546
 
 
3547
        if (io->flag != DIO_AIO_UNWRITTEN)
 
3548
                return ret;
 
3549
 
 
3550
        if (offset + size <= i_size_read(inode))
 
3551
                ret = ext4_convert_unwritten_extents(inode, offset, size);
 
3552
 
 
3553
        if (ret < 0) {
 
3554
                printk(KERN_EMERG "%s: failed to convert unwritten"
 
3555
                        "extents to written extents, error is %d"
 
3556
                        " io is still on inode %lu aio dio list\n",
 
3557
                       __func__, ret, inode->i_ino);
 
3558
                return ret;
 
3559
        }
 
3560
 
 
3561
        /* clear the DIO AIO unwritten flag */
 
3562
        io->flag = 0;
 
3563
        return ret;
 
3564
}
 
3565
/*
 
3566
 * work on completed aio dio IO, to convert unwritten extents to extents
 
3567
 */
 
3568
static void ext4_end_aio_dio_work(struct work_struct *work)
 
3569
{
 
3570
        ext4_io_end_t *io  = container_of(work, ext4_io_end_t, work);
 
3571
        struct inode *inode = io->inode;
 
3572
        int ret = 0;
 
3573
 
 
3574
        mutex_lock(&inode->i_mutex);
 
3575
        ret = ext4_end_aio_dio_nolock(io);
 
3576
        if (ret >= 0) {
 
3577
                if (!list_empty(&io->list))
 
3578
                        list_del_init(&io->list);
 
3579
                ext4_free_io_end(io);
 
3580
        }
 
3581
        mutex_unlock(&inode->i_mutex);
 
3582
}
 
3583
/*
 
3584
 * This function is called from ext4_sync_file().
 
3585
 *
 
3586
 * When AIO DIO IO is completed, the work to convert unwritten
 
3587
 * extents to written is queued on workqueue but may not get immediately
 
3588
 * scheduled. When fsync is called, we need to ensure the
 
3589
 * conversion is complete before fsync returns.
 
3590
 * The inode keeps track of a list of completed AIO from DIO path
 
3591
 * that might needs to do the conversion. This function walks through
 
3592
 * the list and convert the related unwritten extents to written.
 
3593
 */
 
3594
int flush_aio_dio_completed_IO(struct inode *inode)
 
3595
{
 
3596
        ext4_io_end_t *io;
 
3597
        int ret = 0;
 
3598
        int ret2 = 0;
 
3599
 
 
3600
        if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list))
 
3601
                return ret;
 
3602
 
 
3603
        dump_aio_dio_list(inode);
 
3604
        while (!list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){
 
3605
                io = list_entry(EXT4_I(inode)->i_aio_dio_complete_list.next,
 
3606
                                ext4_io_end_t, list);
 
3607
                /*
 
3608
                 * Calling ext4_end_aio_dio_nolock() to convert completed
 
3609
                 * IO to written.
 
3610
                 *
 
3611
                 * When ext4_sync_file() is called, run_queue() may already
 
3612
                 * about to flush the work corresponding to this io structure.
 
3613
                 * It will be upset if it founds the io structure related
 
3614
                 * to the work-to-be schedule is freed.
 
3615
                 *
 
3616
                 * Thus we need to keep the io structure still valid here after
 
3617
                 * convertion finished. The io structure has a flag to
 
3618
                 * avoid double converting from both fsync and background work
 
3619
                 * queue work.
 
3620
                 */
 
3621
                ret = ext4_end_aio_dio_nolock(io);
 
3622
                if (ret < 0)
 
3623
                        ret2 = ret;
 
3624
                else
 
3625
                        list_del_init(&io->list);
 
3626
        }
 
3627
        return (ret2 < 0) ? ret2 : 0;
 
3628
}
 
3629
 
 
3630
static ext4_io_end_t *ext4_init_io_end (struct inode *inode)
 
3631
{
 
3632
        ext4_io_end_t *io = NULL;
 
3633
 
 
3634
        io = kmalloc(sizeof(*io), GFP_NOFS);
 
3635
 
 
3636
        if (io) {
 
3637
                igrab(inode);
 
3638
                io->inode = inode;
 
3639
                io->flag = 0;
 
3640
                io->offset = 0;
 
3641
                io->size = 0;
 
3642
                io->error = 0;
 
3643
                INIT_WORK(&io->work, ext4_end_aio_dio_work);
 
3644
                INIT_LIST_HEAD(&io->list);
 
3645
        }
 
3646
 
 
3647
        return io;
 
3648
}
 
3649
 
 
3650
static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
 
3651
                            ssize_t size, void *private)
 
3652
{
 
3653
        ext4_io_end_t *io_end = iocb->private;
 
3654
        struct workqueue_struct *wq;
 
3655
 
 
3656
        /* if not async direct IO or dio with 0 bytes write, just return */
 
3657
        if (!io_end || !size)
 
3658
                return;
 
3659
 
 
3660
        ext_debug("ext4_end_io_dio(): io_end 0x%p"
 
3661
                  "for inode %lu, iocb 0x%p, offset %llu, size %llu\n",
 
3662
                  iocb->private, io_end->inode->i_ino, iocb, offset,
 
3663
                  size);
 
3664
 
 
3665
        /* if not aio dio with unwritten extents, just free io and return */
 
3666
        if (io_end->flag != DIO_AIO_UNWRITTEN){
 
3667
                ext4_free_io_end(io_end);
 
3668
                iocb->private = NULL;
 
3669
                return;
 
3670
        }
 
3671
 
 
3672
        io_end->offset = offset;
 
3673
        io_end->size = size;
 
3674
        wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
 
3675
 
 
3676
        /* queue the work to convert unwritten extents to written */
 
3677
        queue_work(wq, &io_end->work);
 
3678
 
 
3679
        /* Add the io_end to per-inode completed aio dio list*/
 
3680
        list_add_tail(&io_end->list,
 
3681
                 &EXT4_I(io_end->inode)->i_aio_dio_complete_list);
 
3682
        iocb->private = NULL;
 
3683
}
 
3684
/*
 
3685
 * For ext4 extent files, ext4 will do direct-io write to holes,
 
3686
 * preallocated extents, and those write extend the file, no need to
 
3687
 * fall back to buffered IO.
 
3688
 *
 
3689
 * For holes, we fallocate those blocks, mark them as unintialized
 
3690
 * If those blocks were preallocated, we mark sure they are splited, but
 
3691
 * still keep the range to write as unintialized.
 
3692
 *
 
3693
 * The unwrritten extents will be converted to written when DIO is completed.
 
3694
 * For async direct IO, since the IO may still pending when return, we
 
3695
 * set up an end_io call back function, which will do the convertion
 
3696
 * when async direct IO completed.
 
3697
 *
 
3698
 * If the O_DIRECT write will extend the file then add this inode to the
 
3699
 * orphan list.  So recovery will truncate it back to the original size
 
3700
 * if the machine crashes during the write.
 
3701
 *
 
3702
 */
 
3703
static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
 
3704
                              const struct iovec *iov, loff_t offset,
 
3705
                              unsigned long nr_segs)
 
3706
{
 
3707
        struct file *file = iocb->ki_filp;
 
3708
        struct inode *inode = file->f_mapping->host;
 
3709
        ssize_t ret;
 
3710
        size_t count = iov_length(iov, nr_segs);
 
3711
 
 
3712
        loff_t final_size = offset + count;
 
3713
        if (rw == WRITE && final_size <= inode->i_size) {
 
3714
                /*
 
3715
                 * We could direct write to holes and fallocate.
 
3716
                 *
 
3717
                 * Allocated blocks to fill the hole are marked as uninitialized
 
3718
                 * to prevent paralel buffered read to expose the stale data
 
3719
                 * before DIO complete the data IO.
 
3720
                 *
 
3721
                 * As to previously fallocated extents, ext4 get_block
 
3722
                 * will just simply mark the buffer mapped but still
 
3723
                 * keep the extents uninitialized.
 
3724
                 *
 
3725
                 * for non AIO case, we will convert those unwritten extents
 
3726
                 * to written after return back from blockdev_direct_IO.
 
3727
                 *
 
3728
                 * for async DIO, the conversion needs to be defered when
 
3729
                 * the IO is completed. The ext4 end_io callback function
 
3730
                 * will be called to take care of the conversion work.
 
3731
                 * Here for async case, we allocate an io_end structure to
 
3732
                 * hook to the iocb.
 
3733
                 */
 
3734
                iocb->private = NULL;
 
3735
                EXT4_I(inode)->cur_aio_dio = NULL;
 
3736
                if (!is_sync_kiocb(iocb)) {
 
3737
                        iocb->private = ext4_init_io_end(inode);
 
3738
                        if (!iocb->private)
 
3739
                                return -ENOMEM;
 
3740
                        /*
 
3741
                         * we save the io structure for current async
 
3742
                         * direct IO, so that later ext4_get_blocks()
 
3743
                         * could flag the io structure whether there
 
3744
                         * is a unwritten extents needs to be converted
 
3745
                         * when IO is completed.
 
3746
                         */
 
3747
                        EXT4_I(inode)->cur_aio_dio = iocb->private;
 
3748
                }
 
3749
 
 
3750
                ret = blockdev_direct_IO(rw, iocb, inode,
 
3751
                                         inode->i_sb->s_bdev, iov,
 
3752
                                         offset, nr_segs,
 
3753
                                         ext4_get_block_dio_write,
 
3754
                                         ext4_end_io_dio);
 
3755
                if (iocb->private)
 
3756
                        EXT4_I(inode)->cur_aio_dio = NULL;
 
3757
                /*
 
3758
                 * The io_end structure takes a reference to the inode,
 
3759
                 * that structure needs to be destroyed and the
 
3760
                 * reference to the inode need to be dropped, when IO is
 
3761
                 * complete, even with 0 byte write, or failed.
 
3762
                 *
 
3763
                 * In the successful AIO DIO case, the io_end structure will be
 
3764
                 * desctroyed and the reference to the inode will be dropped
 
3765
                 * after the end_io call back function is called.
 
3766
                 *
 
3767
                 * In the case there is 0 byte write, or error case, since
 
3768
                 * VFS direct IO won't invoke the end_io call back function,
 
3769
                 * we need to free the end_io structure here.
 
3770
                 */
 
3771
                if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
 
3772
                        ext4_free_io_end(iocb->private);
 
3773
                        iocb->private = NULL;
 
3774
                } else if (ret > 0 && (EXT4_I(inode)->i_state &
 
3775
                                       EXT4_STATE_DIO_UNWRITTEN)) {
 
3776
                        int err;
 
3777
                        /*
 
3778
                         * for non AIO case, since the IO is already
 
3779
                         * completed, we could do the convertion right here
 
3780
                         */
 
3781
                        err = ext4_convert_unwritten_extents(inode,
 
3782
                                                             offset, ret);
 
3783
                        if (err < 0)
 
3784
                                ret = err;
 
3785
                        EXT4_I(inode)->i_state &= ~EXT4_STATE_DIO_UNWRITTEN;
 
3786
                }
 
3787
                return ret;
 
3788
        }
 
3789
 
 
3790
        /* for write the the end of file case, we fall back to old way */
 
3791
        return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
 
3792
}
 
3793
 
 
3794
static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
 
3795
                              const struct iovec *iov, loff_t offset,
 
3796
                              unsigned long nr_segs)
 
3797
{
 
3798
        struct file *file = iocb->ki_filp;
 
3799
        struct inode *inode = file->f_mapping->host;
 
3800
 
 
3801
        if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
 
3802
                return ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
 
3803
 
 
3804
        return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
 
3805
}
 
3806
 
3344
3807
/*
3345
3808
 * Pages can be marked dirty completely asynchronously from ext4's journalling
3346
3809
 * activity.  By filemap_sync_pte(), try_to_unmap_one(), etc.  We cannot do
3653
4116
                              __le32 *last)
3654
4117
{
3655
4118
        __le32 *p;
 
4119
        int     is_metadata = S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode);
 
4120
 
3656
4121
        if (try_to_extend_transaction(handle, inode)) {
3657
4122
                if (bh) {
3658
4123
                        BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
3659
4124
                        ext4_handle_dirty_metadata(handle, inode, bh);
3660
4125
                }
3661
4126
                ext4_mark_inode_dirty(handle, inode);
3662
 
                ext4_journal_test_restart(handle, inode);
 
4127
                ext4_truncate_restart_trans(handle, inode,
 
4128
                                            blocks_for_truncate(inode));
3663
4129
                if (bh) {
3664
4130
                        BUFFER_TRACE(bh, "retaking write access");
3665
4131
                        ext4_journal_get_write_access(handle, bh);
3682
4148
 
3683
4149
                        *p = 0;
3684
4150
                        tbh = sb_find_get_block(inode->i_sb, nr);
3685
 
                        ext4_forget(handle, 0, inode, tbh, nr);
 
4151
                        ext4_forget(handle, is_metadata, inode, tbh, nr);
3686
4152
                }
3687
4153
        }
3688
4154
 
3689
 
        ext4_free_blocks(handle, inode, block_to_free, count, 0);
 
4155
        ext4_free_blocks(handle, inode, block_to_free, count, is_metadata);
3690
4156
}
3691
4157
 
3692
4158
/**
3870
4336
                                return;
3871
4337
                        if (try_to_extend_transaction(handle, inode)) {
3872
4338
                                ext4_mark_inode_dirty(handle, inode);
3873
 
                                ext4_journal_test_restart(handle, inode);
 
4339
                                ext4_truncate_restart_trans(handle, inode,
 
4340
                                            blocks_for_truncate(inode));
3874
4341
                        }
3875
4342
 
3876
4343
                        ext4_free_blocks(handle, inode, nr, 1, 1);
3958
4425
        if (!ext4_can_truncate(inode))
3959
4426
                return;
3960
4427
 
3961
 
        if (ei->i_disksize && inode->i_size == 0 &&
3962
 
            !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
 
4428
        if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
3963
4429
                ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE;
3964
4430
 
3965
4431
        if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
4313
4779
        struct ext4_iloc iloc;
4314
4780
        struct ext4_inode *raw_inode;
4315
4781
        struct ext4_inode_info *ei;
4316
 
        struct buffer_head *bh;
4317
4782
        struct inode *inode;
 
4783
        journal_t *journal = EXT4_SB(sb)->s_journal;
4318
4784
        long ret;
4319
4785
        int block;
4320
4786
 
4325
4791
                return inode;
4326
4792
 
4327
4793
        ei = EXT4_I(inode);
 
4794
        iloc.bh = 0;
4328
4795
 
4329
4796
        ret = __ext4_get_inode_loc(inode, &iloc, 0);
4330
4797
        if (ret < 0)
4331
4798
                goto bad_inode;
4332
 
        bh = iloc.bh;
4333
4799
        raw_inode = ext4_raw_inode(&iloc);
4334
4800
        inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4335
4801
        inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4352
4818
                if (inode->i_mode == 0 ||
4353
4819
                    !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
4354
4820
                        /* this inode is deleted */
4355
 
                        brelse(bh);
4356
4821
                        ret = -ESTALE;
4357
4822
                        goto bad_inode;
4358
4823
                }
4369
4834
                        ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
4370
4835
        inode->i_size = ext4_isize(raw_inode);
4371
4836
        ei->i_disksize = inode->i_size;
 
4837
#ifdef CONFIG_QUOTA
 
4838
        ei->i_reserved_quota = 0;
 
4839
#endif
4372
4840
        inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4373
4841
        ei->i_block_group = iloc.block_group;
4374
4842
        ei->i_last_alloc_group = ~0;
4380
4848
                ei->i_data[block] = raw_inode->i_block[block];
4381
4849
        INIT_LIST_HEAD(&ei->i_orphan);
4382
4850
 
 
4851
        /*
 
4852
         * Set transaction id's of transactions that have to be committed
 
4853
         * to finish f[data]sync. We set them to currently running transaction
 
4854
         * as we cannot be sure that the inode or some of its metadata isn't
 
4855
         * part of the transaction - the inode could have been reclaimed and
 
4856
         * now it is reread from disk.
 
4857
         */
 
4858
        if (journal) {
 
4859
                transaction_t *transaction;
 
4860
                tid_t tid;
 
4861
 
 
4862
                spin_lock(&journal->j_state_lock);
 
4863
                if (journal->j_running_transaction)
 
4864
                        transaction = journal->j_running_transaction;
 
4865
                else
 
4866
                        transaction = journal->j_committing_transaction;
 
4867
                if (transaction)
 
4868
                        tid = transaction->t_tid;
 
4869
                else
 
4870
                        tid = journal->j_commit_sequence;
 
4871
                spin_unlock(&journal->j_state_lock);
 
4872
                ei->i_sync_tid = tid;
 
4873
                ei->i_datasync_tid = tid;
 
4874
        }
 
4875
 
4383
4876
        if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4384
4877
                ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4385
4878
                if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
4386
4879
                    EXT4_INODE_SIZE(inode->i_sb)) {
4387
 
                        brelse(bh);
4388
4880
                        ret = -EIO;
4389
4881
                        goto bad_inode;
4390
4882
                }
4416
4908
 
4417
4909
        ret = 0;
4418
4910
        if (ei->i_file_acl &&
4419
 
            ((ei->i_file_acl <
4420
 
              (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) +
4421
 
               EXT4_SB(sb)->s_gdb_count)) ||
4422
 
             (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) {
 
4911
            !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
4423
4912
                ext4_error(sb, __func__,
4424
4913
                           "bad extended attribute block %llu in inode #%lu",
4425
4914
                           ei->i_file_acl, inode->i_ino);
4437
4926
                /* Validate block references which are part of inode */
4438
4927
                ret = ext4_check_inode_blockref(inode);
4439
4928
        }
4440
 
        if (ret) {
4441
 
                brelse(bh);
 
4929
        if (ret)
4442
4930
                goto bad_inode;
4443
 
        }
4444
4931
 
4445
4932
        if (S_ISREG(inode->i_mode)) {
4446
4933
                inode->i_op = &ext4_file_inode_operations;
4468
4955
                        init_special_inode(inode, inode->i_mode,
4469
4956
                           new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
4470
4957
        } else {
4471
 
                brelse(bh);
4472
4958
                ret = -EIO;
4473
4959
                ext4_error(inode->i_sb, __func__,
4474
4960
                           "bogus i_mode (%o) for inode=%lu",
4481
4967
        return inode;
4482
4968
 
4483
4969
bad_inode:
 
4970
        brelse(iloc.bh);
4484
4971
        iget_failed(inode);
4485
4972
        return ERR_PTR(ret);
4486
4973
}
4581
5068
        if (ext4_inode_blocks_set(handle, raw_inode, ei))
4582
5069
                goto out_brelse;
4583
5070
        raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
4584
 
        /* clear the migrate flag in the raw_inode */
4585
 
        raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
 
5071
        raw_inode->i_flags = cpu_to_le32(ei->i_flags);
4586
5072
        if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4587
5073
            cpu_to_le32(EXT4_OS_HURD))
4588
5074
                raw_inode->i_file_acl_high =
4641
5127
                err = rc;
4642
5128
        ei->i_state &= ~EXT4_STATE_NEW;
4643
5129
 
 
5130
        ext4_update_inode_fsync_trans(handle, inode, 0);
4644
5131
out_brelse:
4645
5132
        brelse(bh);
4646
5133
        ext4_std_error(inode->i_sb, err);
4684
5171
 */
4685
5172
int ext4_write_inode(struct inode *inode, int wait)
4686
5173
{
 
5174
        int err;
 
5175
 
4687
5176
        if (current->flags & PF_MEMALLOC)
4688
5177
                return 0;
4689
5178
 
4690
 
        if (ext4_journal_current_handle()) {
4691
 
                jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4692
 
                dump_stack();
4693
 
                return -EIO;
 
5179
        if (EXT4_SB(inode->i_sb)->s_journal) {
 
5180
                if (ext4_journal_current_handle()) {
 
5181
                        jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
 
5182
                        dump_stack();
 
5183
                        return -EIO;
 
5184
                }
 
5185
 
 
5186
                if (!wait)
 
5187
                        return 0;
 
5188
 
 
5189
                err = ext4_force_commit(inode->i_sb);
 
5190
        } else {
 
5191
                struct ext4_iloc iloc;
 
5192
 
 
5193
                err = ext4_get_inode_loc(inode, &iloc);
 
5194
                if (err)
 
5195
                        return err;
 
5196
                if (wait)
 
5197
                        sync_dirty_buffer(iloc.bh);
 
5198
                if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
 
5199
                        ext4_error(inode->i_sb, __func__,
 
5200
                                   "IO error syncing inode, "
 
5201
                                   "inode=%lu, block=%llu",
 
5202
                                   inode->i_ino,
 
5203
                                   (unsigned long long)iloc.bh->b_blocknr);
 
5204
                        err = -EIO;
 
5205
                }
4694
5206
        }
4695
 
 
4696
 
        if (!wait)
4697
 
                return 0;
4698
 
 
4699
 
        return ext4_force_commit(inode->i_sb);
 
5207
        return err;
4700
5208
}
4701
5209
 
4702
5210
/*
4739
5247
 
4740
5248
                /* (user+group)*(old+new) structure, inode write (sb,
4741
5249
                 * inode block, ? - but truncate inode update has it) */
4742
 
                handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
4743
 
                                        EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
 
5250
                handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
 
5251
                                        EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3);
4744
5252
                if (IS_ERR(handle)) {
4745
5253
                        error = PTR_ERR(handle);
4746
5254
                        goto err_out;
5137
5645
        handle_t *current_handle = ext4_journal_current_handle();
5138
5646
        handle_t *handle;
5139
5647
 
5140
 
        if (!ext4_handle_valid(current_handle)) {
5141
 
                ext4_mark_inode_dirty(current_handle, inode);
5142
 
                return;
5143
 
        }
5144
 
 
5145
5648
        handle = ext4_journal_start(inode, 2);
5146
5649
        if (IS_ERR(handle))
5147
5650
                goto out;
5148
 
        if (current_handle &&
5149
 
                current_handle->h_transaction != handle->h_transaction) {
5150
 
                /* This task has a transaction open against a different fs */
5151
 
                printk(KERN_EMERG "%s: transactions do not match!\n",
5152
 
                       __func__);
5153
 
        } else {
5154
 
                jbd_debug(5, "marking dirty.  outer handle=%p\n",
5155
 
                                current_handle);
5156
 
                ext4_mark_inode_dirty(handle, inode);
5157
 
        }
 
5651
 
 
5652
        jbd_debug(5, "marking dirty.  outer handle=%p\n", current_handle);
 
5653
        ext4_mark_inode_dirty(handle, inode);
 
5654
 
5158
5655
        ext4_journal_stop(handle);
5159
5656
out:
5160
5657
        return;
5281
5778
        else
5282
5779
                len = PAGE_CACHE_SIZE;
5283
5780
 
 
5781
        lock_page(page);
 
5782
        /*
 
5783
         * return if we have all the buffers mapped. This avoid
 
5784
         * the need to call write_begin/write_end which does a
 
5785
         * journal_start/journal_stop which can block and take
 
5786
         * long time
 
5787
         */
5284
5788
        if (page_has_buffers(page)) {
5285
 
                /* return if we have all the buffers mapped */
5286
5789
                if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
5287
 
                                       ext4_bh_unmapped))
 
5790
                                        ext4_bh_unmapped)) {
 
5791
                        unlock_page(page);
5288
5792
                        goto out_unlock;
 
5793
                }
5289
5794
        }
 
5795
        unlock_page(page);
5290
5796
        /*
5291
5797
         * OK, we need to fill the hole... Do write_begin write_end
5292
5798
         * to do block allocation/reservation.We are not holding