Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/inode.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 5 | * Remy Card (card@masi.ibp.fr) |
| 6 | * Laboratoire MASI - Institut Blaise Pascal |
| 7 | * Universite Pierre et Marie Curie (Paris VI) |
| 8 | * |
| 9 | * from |
| 10 | * |
| 11 | * linux/fs/minix/inode.c |
| 12 | * |
| 13 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 14 | * |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 15 | * 64-bit file support on 64-bit platforms by Jakub Jelinek |
| 16 | * (jj@sunsite.ms.mff.cuni.cz) |
| 17 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 18 | * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/time.h> |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 24 | #include <linux/jbd2.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 25 | #include <linux/highuid.h> |
| 26 | #include <linux/pagemap.h> |
| 27 | #include <linux/quotaops.h> |
| 28 | #include <linux/string.h> |
| 29 | #include <linux/buffer_head.h> |
| 30 | #include <linux/writeback.h> |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 31 | #include <linux/pagevec.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 32 | #include <linux/mpage.h> |
Duane Griffin | e83c139 | 2008-12-19 20:47:15 +0000 | [diff] [blame] | 33 | #include <linux/namei.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 34 | #include <linux/uio.h> |
| 35 | #include <linux/bio.h> |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 36 | #include <linux/workqueue.h> |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 37 | #include <linux/kernel.h> |
Andrew Morton | 6db26ff | 2011-01-12 16:59:13 -0800 | [diff] [blame] | 38 | #include <linux/printk.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Theodore Ts'o | a8901d3 | 2010-12-17 10:40:47 -0500 | [diff] [blame] | 40 | #include <linux/ratelimit.h> |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 41 | |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 42 | #include "ext4_jbd2.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 43 | #include "xattr.h" |
| 44 | #include "acl.h" |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 45 | #include "ext4_extents.h" |
Theodore Ts'o | 9f125d6 | 2011-06-27 19:16:04 -0400 | [diff] [blame] | 46 | #include "truncate.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 47 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 48 | #include <trace/events/ext4.h> |
| 49 | |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 50 | #define MPAGE_DA_EXTENT_TAIL 0x01 |
| 51 | |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 52 | static inline int ext4_begin_ordered_truncate(struct inode *inode, |
| 53 | loff_t new_size) |
| 54 | { |
Theodore Ts'o | 7ff9c07 | 2010-11-08 13:51:33 -0500 | [diff] [blame] | 55 | trace_ext4_begin_ordered_truncate(inode, new_size); |
Theodore Ts'o | 8aefcd5 | 2011-01-10 12:29:43 -0500 | [diff] [blame] | 56 | /* |
| 57 | * If jinode is zero, then we never opened the file for |
| 58 | * writing, so there's no need to call |
| 59 | * jbd2_journal_begin_ordered_truncate() since there's no |
| 60 | * outstanding writes we need to flush. |
| 61 | */ |
| 62 | if (!EXT4_I(inode)->jinode) |
| 63 | return 0; |
| 64 | return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), |
| 65 | EXT4_I(inode)->jinode, |
| 66 | new_size); |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 69 | static void ext4_invalidatepage(struct page *page, unsigned long offset); |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 70 | static int noalloc_get_block_write(struct inode *inode, sector_t iblock, |
| 71 | struct buffer_head *bh_result, int create); |
| 72 | static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); |
| 73 | static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); |
| 74 | static int __ext4_journalled_writepage(struct page *page, unsigned int len); |
| 75 | static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 76 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 77 | /* |
| 78 | * Test whether an inode is a fast symlink. |
| 79 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 80 | static int ext4_inode_is_fast_symlink(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 81 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 82 | int ea_blocks = EXT4_I(inode)->i_file_acl ? |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 83 | (inode->i_sb->s_blocksize >> 9) : 0; |
| 84 | |
| 85 | return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); |
| 86 | } |
| 87 | |
| 88 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 89 | * Restart the transaction associated with *handle. This does a commit, |
| 90 | * so before we call here everything must be consistently dirtied against |
| 91 | * this transaction. |
| 92 | */ |
Aneesh Kumar K.V | fa5d111 | 2009-11-02 18:50:49 -0500 | [diff] [blame] | 93 | int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 94 | int nblocks) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 95 | { |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 96 | int ret; |
| 97 | |
| 98 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 99 | * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 100 | * moment, get_block can be called only for blocks inside i_size since |
| 101 | * page cache has been already dropped and writes are blocked by |
| 102 | * i_mutex. So we can safely drop the i_data_sem here. |
| 103 | */ |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 104 | BUG_ON(EXT4_JOURNAL(inode) == NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 105 | jbd_debug(2, "restarting handle %p\n", handle); |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 106 | up_write(&EXT4_I(inode)->i_data_sem); |
Amir Goldstein | 8e8eaab | 2011-02-27 23:32:12 -0500 | [diff] [blame] | 107 | ret = ext4_journal_restart(handle, nblocks); |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 108 | down_write(&EXT4_I(inode)->i_data_sem); |
Aneesh Kumar K.V | fa5d111 | 2009-11-02 18:50:49 -0500 | [diff] [blame] | 109 | ext4_discard_preallocations(inode); |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 110 | |
| 111 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Called at the last iput() if i_nlink is zero. |
| 116 | */ |
Al Viro | 0930fcc | 2010-06-07 13:16:22 -0400 | [diff] [blame] | 117 | void ext4_evict_inode(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 118 | { |
| 119 | handle_t *handle; |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 120 | int err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 121 | |
Theodore Ts'o | 7ff9c07 | 2010-11-08 13:51:33 -0500 | [diff] [blame] | 122 | trace_ext4_evict_inode(inode); |
Al Viro | 0930fcc | 2010-06-07 13:16:22 -0400 | [diff] [blame] | 123 | if (inode->i_nlink) { |
| 124 | truncate_inode_pages(&inode->i_data, 0); |
| 125 | goto no_delete; |
| 126 | } |
| 127 | |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 128 | if (!is_bad_inode(inode)) |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 129 | dquot_initialize(inode); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 130 | |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 131 | if (ext4_should_order_data(inode)) |
| 132 | ext4_begin_ordered_truncate(inode, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 133 | truncate_inode_pages(&inode->i_data, 0); |
| 134 | |
| 135 | if (is_bad_inode(inode)) |
| 136 | goto no_delete; |
| 137 | |
Theodore Ts'o | 9f125d6 | 2011-06-27 19:16:04 -0400 | [diff] [blame] | 138 | handle = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)+3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 139 | if (IS_ERR(handle)) { |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 140 | ext4_std_error(inode->i_sb, PTR_ERR(handle)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 141 | /* |
| 142 | * If we're going to skip the normal cleanup, we still need to |
| 143 | * make sure that the in-core orphan linked list is properly |
| 144 | * cleaned up. |
| 145 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 146 | ext4_orphan_del(NULL, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 147 | goto no_delete; |
| 148 | } |
| 149 | |
| 150 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 151 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 152 | inode->i_size = 0; |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 153 | err = ext4_mark_inode_dirty(handle, inode); |
| 154 | if (err) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 155 | ext4_warning(inode->i_sb, |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 156 | "couldn't mark inode dirty (err %d)", err); |
| 157 | goto stop_handle; |
| 158 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 159 | if (inode->i_blocks) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 160 | ext4_truncate(inode); |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * ext4_ext_truncate() doesn't reserve any slop when it |
| 164 | * restarts journal transactions; therefore there may not be |
| 165 | * enough credits left in the handle to remove the inode from |
| 166 | * the orphan list and set the dtime field. |
| 167 | */ |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 168 | if (!ext4_handle_has_enough_credits(handle, 3)) { |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 169 | err = ext4_journal_extend(handle, 3); |
| 170 | if (err > 0) |
| 171 | err = ext4_journal_restart(handle, 3); |
| 172 | if (err != 0) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 173 | ext4_warning(inode->i_sb, |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 174 | "couldn't extend journal (err %d)", err); |
| 175 | stop_handle: |
| 176 | ext4_journal_stop(handle); |
Theodore Ts'o | 4538821 | 2010-07-29 15:06:10 -0400 | [diff] [blame] | 177 | ext4_orphan_del(NULL, inode); |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 178 | goto no_delete; |
| 179 | } |
| 180 | } |
| 181 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 182 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 183 | * Kill off the orphan record which ext4_truncate created. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 184 | * AKPM: I think this can be inside the above `if'. |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 185 | * Note that ext4_orphan_del() has to be able to cope with the |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 186 | * deletion of a non-existent orphan - this is because we don't |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 187 | * know if ext4_truncate() actually created an orphan record. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 188 | * (Well, we could do this if we need to, but heck - it works) |
| 189 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 190 | ext4_orphan_del(handle, inode); |
| 191 | EXT4_I(inode)->i_dtime = get_seconds(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * One subtle ordering requirement: if anything has gone wrong |
| 195 | * (transaction abort, IO errors, whatever), then we can still |
| 196 | * do these next steps (the fs will already have been marked as |
| 197 | * having errors), but we can't free the inode if the mark_dirty |
| 198 | * fails. |
| 199 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 200 | if (ext4_mark_inode_dirty(handle, inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 201 | /* If that failed, just do the required in-core inode clear. */ |
Al Viro | 0930fcc | 2010-06-07 13:16:22 -0400 | [diff] [blame] | 202 | ext4_clear_inode(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 203 | else |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 204 | ext4_free_inode(handle, inode); |
| 205 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 206 | return; |
| 207 | no_delete: |
Al Viro | 0930fcc | 2010-06-07 13:16:22 -0400 | [diff] [blame] | 208 | ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Dmitry Monakhov | a9e7f44 | 2009-12-14 15:21:14 +0300 | [diff] [blame] | 211 | #ifdef CONFIG_QUOTA |
| 212 | qsize_t *ext4_get_reserved_space(struct inode *inode) |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 213 | { |
Dmitry Monakhov | a9e7f44 | 2009-12-14 15:21:14 +0300 | [diff] [blame] | 214 | return &EXT4_I(inode)->i_reserved_quota; |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 215 | } |
Dmitry Monakhov | a9e7f44 | 2009-12-14 15:21:14 +0300 | [diff] [blame] | 216 | #endif |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 217 | |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 218 | /* |
| 219 | * Calculate the number of metadata blocks need to reserve |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 220 | * to allocate a block located at @lblock |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 221 | */ |
Theodore Ts'o | 01f49d0 | 2011-01-10 12:13:03 -0500 | [diff] [blame] | 222 | static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 223 | { |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 224 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 225 | return ext4_ext_calc_metadata_amount(inode, lblock); |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 226 | |
Amir Goldstein | 8bb2b24 | 2011-06-27 17:10:28 -0400 | [diff] [blame] | 227 | return ext4_ind_calc_metadata_amount(inode, lblock); |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 230 | /* |
| 231 | * Called with i_data_sem down, which is important since we can call |
| 232 | * ext4_discard_preallocations() from here. |
| 233 | */ |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 234 | void ext4_da_update_reserve_space(struct inode *inode, |
| 235 | int used, int quota_claim) |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 236 | { |
| 237 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 238 | struct ext4_inode_info *ei = EXT4_I(inode); |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 239 | |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 240 | spin_lock(&ei->i_block_reservation_lock); |
Theodore Ts'o | f8ec9d6 | 2010-01-01 01:00:21 -0500 | [diff] [blame] | 241 | trace_ext4_da_update_reserve_space(inode, used); |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 242 | if (unlikely(used > ei->i_reserved_data_blocks)) { |
| 243 | ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d " |
| 244 | "with only %d reserved data blocks\n", |
| 245 | __func__, inode->i_ino, used, |
| 246 | ei->i_reserved_data_blocks); |
| 247 | WARN_ON(1); |
| 248 | used = ei->i_reserved_data_blocks; |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 249 | } |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 250 | |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 251 | /* Update per-inode reservations */ |
| 252 | ei->i_reserved_data_blocks -= used; |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 253 | ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 254 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, |
| 255 | used + ei->i_allocated_meta_blocks); |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 256 | ei->i_allocated_meta_blocks = 0; |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 257 | |
| 258 | if (ei->i_reserved_data_blocks == 0) { |
| 259 | /* |
| 260 | * We can release all of the reserved metadata blocks |
| 261 | * only when we have written all of the delayed |
| 262 | * allocation blocks. |
| 263 | */ |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 264 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, |
| 265 | ei->i_reserved_meta_blocks); |
Theodore Ts'o | ee5f4d9 | 2010-01-01 02:36:15 -0500 | [diff] [blame] | 266 | ei->i_reserved_meta_blocks = 0; |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 267 | ei->i_da_metadata_calc_len = 0; |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 268 | } |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 269 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 270 | |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 271 | /* Update quota subsystem for data blocks */ |
| 272 | if (quota_claim) |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 273 | dquot_claim_block(inode, used); |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 274 | else { |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 275 | /* |
| 276 | * We did fallocate with an offset that is already delayed |
| 277 | * allocated. So on delayed allocated writeback we should |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 278 | * not re-claim the quota for fallocated blocks. |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 279 | */ |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 280 | dquot_release_reservation_block(inode, used); |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 281 | } |
Aneesh Kumar K.V | d601430 | 2009-03-27 22:36:43 -0400 | [diff] [blame] | 282 | |
| 283 | /* |
| 284 | * If we have done all the pending block allocations and if |
| 285 | * there aren't any writers on the inode, we can discard the |
| 286 | * inode's preallocations. |
| 287 | */ |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 288 | if ((ei->i_reserved_data_blocks == 0) && |
| 289 | (atomic_read(&inode->i_writecount) == 0)) |
Aneesh Kumar K.V | d601430 | 2009-03-27 22:36:43 -0400 | [diff] [blame] | 290 | ext4_discard_preallocations(inode); |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 293 | static int __check_block_validity(struct inode *inode, const char *func, |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 294 | unsigned int line, |
| 295 | struct ext4_map_blocks *map) |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 296 | { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 297 | if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, |
| 298 | map->m_len)) { |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 299 | ext4_error_inode(inode, func, line, map->m_pblk, |
| 300 | "lblock %lu mapped to illegal pblock " |
| 301 | "(length %d)", (unsigned long) map->m_lblk, |
| 302 | map->m_len); |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 303 | return -EIO; |
| 304 | } |
| 305 | return 0; |
| 306 | } |
| 307 | |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 308 | #define check_block_validity(inode, map) \ |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 309 | __check_block_validity((inode), __func__, __LINE__, (map)) |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 310 | |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 311 | /* |
Theodore Ts'o | 1f94533 | 2009-09-30 22:57:41 -0400 | [diff] [blame] | 312 | * Return the number of contiguous dirty pages in a given inode |
| 313 | * starting at page frame idx. |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 314 | */ |
| 315 | static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, |
| 316 | unsigned int max_pages) |
| 317 | { |
| 318 | struct address_space *mapping = inode->i_mapping; |
| 319 | pgoff_t index; |
| 320 | struct pagevec pvec; |
| 321 | pgoff_t num = 0; |
| 322 | int i, nr_pages, done = 0; |
| 323 | |
| 324 | if (max_pages == 0) |
| 325 | return 0; |
| 326 | pagevec_init(&pvec, 0); |
| 327 | while (!done) { |
| 328 | index = idx; |
| 329 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 330 | PAGECACHE_TAG_DIRTY, |
| 331 | (pgoff_t)PAGEVEC_SIZE); |
| 332 | if (nr_pages == 0) |
| 333 | break; |
| 334 | for (i = 0; i < nr_pages; i++) { |
| 335 | struct page *page = pvec.pages[i]; |
| 336 | struct buffer_head *bh, *head; |
| 337 | |
| 338 | lock_page(page); |
| 339 | if (unlikely(page->mapping != mapping) || |
| 340 | !PageDirty(page) || |
| 341 | PageWriteback(page) || |
| 342 | page->index != idx) { |
| 343 | done = 1; |
| 344 | unlock_page(page); |
| 345 | break; |
| 346 | } |
Theodore Ts'o | 1f94533 | 2009-09-30 22:57:41 -0400 | [diff] [blame] | 347 | if (page_has_buffers(page)) { |
| 348 | bh = head = page_buffers(page); |
| 349 | do { |
| 350 | if (!buffer_delay(bh) && |
| 351 | !buffer_unwritten(bh)) |
| 352 | done = 1; |
| 353 | bh = bh->b_this_page; |
| 354 | } while (!done && (bh != head)); |
| 355 | } |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 356 | unlock_page(page); |
| 357 | if (done) |
| 358 | break; |
| 359 | idx++; |
| 360 | num++; |
Eric Sandeen | 659c600 | 2010-10-27 21:30:03 -0400 | [diff] [blame] | 361 | if (num >= max_pages) { |
| 362 | done = 1; |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 363 | break; |
Eric Sandeen | 659c600 | 2010-10-27 21:30:03 -0400 | [diff] [blame] | 364 | } |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 365 | } |
| 366 | pagevec_release(&pvec); |
| 367 | } |
| 368 | return num; |
| 369 | } |
| 370 | |
| 371 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 372 | * The ext4_map_blocks() function tries to look up the requested blocks, |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 373 | * and returns if the blocks are already mapped. |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 374 | * |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 375 | * Otherwise it takes the write lock of the i_data_sem and allocate blocks |
| 376 | * and store the allocated blocks in the result buffer head and mark it |
| 377 | * mapped. |
| 378 | * |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 379 | * If file type is extents based, it will call ext4_ext_map_blocks(), |
| 380 | * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 381 | * based files |
| 382 | * |
| 383 | * On success, it returns the number of blocks being mapped or allocate. |
| 384 | * if create==0 and the blocks are pre-allocated and uninitialized block, |
| 385 | * the result buffer head is unmapped. If the create ==1, it will make sure |
| 386 | * the buffer head is mapped. |
| 387 | * |
| 388 | * It returns 0 if plain look up failed (blocks have not been allocated), in |
| 389 | * that casem, buffer head is unmapped |
| 390 | * |
| 391 | * It returns the error in case of allocation failure. |
| 392 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 393 | int ext4_map_blocks(handle_t *handle, struct inode *inode, |
| 394 | struct ext4_map_blocks *map, int flags) |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 395 | { |
| 396 | int retval; |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 397 | |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 398 | map->m_flags = 0; |
| 399 | ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," |
| 400 | "logical block %lu\n", inode->i_ino, flags, map->m_len, |
| 401 | (unsigned long) map->m_lblk); |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 402 | /* |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 403 | * Try to see if we can get the block without requesting a new |
| 404 | * file system block. |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 405 | */ |
| 406 | down_read((&EXT4_I(inode)->i_data_sem)); |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 407 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 408 | retval = ext4_ext_map_blocks(handle, inode, map, 0); |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 409 | } else { |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 410 | retval = ext4_ind_map_blocks(handle, inode, map, 0); |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 411 | } |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 412 | up_read((&EXT4_I(inode)->i_data_sem)); |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 413 | |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 414 | if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 415 | int ret = check_block_validity(inode, map); |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 416 | if (ret != 0) |
| 417 | return ret; |
| 418 | } |
| 419 | |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 420 | /* If it is only a block(s) look up */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 421 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 422 | return retval; |
| 423 | |
| 424 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 425 | * Returns if the blocks have already allocated |
| 426 | * |
| 427 | * Note that if blocks have been preallocated |
| 428 | * ext4_ext_get_block() returns th create = 0 |
| 429 | * with buffer head unmapped. |
| 430 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 431 | if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 432 | return retval; |
| 433 | |
| 434 | /* |
Aneesh Kumar K.V | 2a8964d | 2009-05-14 17:05:39 -0400 | [diff] [blame] | 435 | * When we call get_blocks without the create flag, the |
| 436 | * BH_Unwritten flag could have gotten set if the blocks |
| 437 | * requested were part of a uninitialized extent. We need to |
| 438 | * clear this flag now that we are committed to convert all or |
| 439 | * part of the uninitialized extent to be an initialized |
| 440 | * extent. This is because we need to avoid the combination |
| 441 | * of BH_Unwritten and BH_Mapped flags being simultaneously |
| 442 | * set on the buffer_head. |
| 443 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 444 | map->m_flags &= ~EXT4_MAP_UNWRITTEN; |
Aneesh Kumar K.V | 2a8964d | 2009-05-14 17:05:39 -0400 | [diff] [blame] | 445 | |
| 446 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 447 | * New blocks allocate and/or writing to uninitialized extent |
| 448 | * will possibly result in updating i_data, so we take |
| 449 | * the write lock of i_data_sem, and call get_blocks() |
| 450 | * with create == 1 flag. |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 451 | */ |
| 452 | down_write((&EXT4_I(inode)->i_data_sem)); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 453 | |
| 454 | /* |
| 455 | * if the caller is from delayed allocation writeout path |
| 456 | * we have already reserved fs blocks for allocation |
| 457 | * let the underlying get_block() function know to |
| 458 | * avoid double accounting |
| 459 | */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 460 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
Theodore Ts'o | f232109 | 2011-01-10 12:12:36 -0500 | [diff] [blame] | 461 | ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 462 | /* |
| 463 | * We need to check for EXT4 here because migrate |
| 464 | * could have changed the inode type in between |
| 465 | */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 466 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 467 | retval = ext4_ext_map_blocks(handle, inode, map, flags); |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 468 | } else { |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 469 | retval = ext4_ind_map_blocks(handle, inode, map, flags); |
Aneesh Kumar K.V | 267e4db | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 470 | |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 471 | if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { |
Aneesh Kumar K.V | 267e4db | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 472 | /* |
| 473 | * We allocated new blocks which will result in |
| 474 | * i_data's format changing. Force the migrate |
| 475 | * to fail by clearing migrate flags |
| 476 | */ |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 477 | ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); |
Aneesh Kumar K.V | 267e4db | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 478 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 479 | |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 480 | /* |
| 481 | * Update reserved blocks/metadata blocks after successful |
| 482 | * block allocation which had been deferred till now. We don't |
| 483 | * support fallocate for non extent files. So we can update |
| 484 | * reserve space here. |
| 485 | */ |
| 486 | if ((retval > 0) && |
Aneesh Kumar K.V | 1296cc8 | 2010-01-15 01:27:59 -0500 | [diff] [blame] | 487 | (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 488 | ext4_da_update_reserve_space(inode, retval, 1); |
| 489 | } |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 490 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
Theodore Ts'o | f232109 | 2011-01-10 12:12:36 -0500 | [diff] [blame] | 491 | ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 492 | |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 493 | up_write((&EXT4_I(inode)->i_data_sem)); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 494 | if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 495 | int ret = check_block_validity(inode, map); |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 496 | if (ret != 0) |
| 497 | return ret; |
| 498 | } |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 499 | return retval; |
| 500 | } |
| 501 | |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 502 | /* Maximum number of blocks we map for direct IO at once. */ |
| 503 | #define DIO_MAX_BLOCKS 4096 |
| 504 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 505 | static int _ext4_get_block(struct inode *inode, sector_t iblock, |
| 506 | struct buffer_head *bh, int flags) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 507 | { |
Dmitriy Monakhov | 3e4fdaf | 2007-02-10 01:46:35 -0800 | [diff] [blame] | 508 | handle_t *handle = ext4_journal_current_handle(); |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 509 | struct ext4_map_blocks map; |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 510 | int ret = 0, started = 0; |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 511 | int dio_credits; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 512 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 513 | map.m_lblk = iblock; |
| 514 | map.m_len = bh->b_size >> inode->i_blkbits; |
| 515 | |
| 516 | if (flags && !handle) { |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 517 | /* Direct IO write... */ |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 518 | if (map.m_len > DIO_MAX_BLOCKS) |
| 519 | map.m_len = DIO_MAX_BLOCKS; |
| 520 | dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 521 | handle = ext4_journal_start(inode, dio_credits); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 522 | if (IS_ERR(handle)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 523 | ret = PTR_ERR(handle); |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 524 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 525 | } |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 526 | started = 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 529 | ret = ext4_map_blocks(handle, inode, &map, flags); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 530 | if (ret > 0) { |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 531 | map_bh(bh, inode->i_sb, map.m_pblk); |
| 532 | bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; |
| 533 | bh->b_size = inode->i_sb->s_blocksize * map.m_len; |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 534 | ret = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 535 | } |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 536 | if (started) |
| 537 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 538 | return ret; |
| 539 | } |
| 540 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 541 | int ext4_get_block(struct inode *inode, sector_t iblock, |
| 542 | struct buffer_head *bh, int create) |
| 543 | { |
| 544 | return _ext4_get_block(inode, iblock, bh, |
| 545 | create ? EXT4_GET_BLOCKS_CREATE : 0); |
| 546 | } |
| 547 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 548 | /* |
| 549 | * `handle' can be NULL if create is zero |
| 550 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 551 | struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 552 | ext4_lblk_t block, int create, int *errp) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 553 | { |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 554 | struct ext4_map_blocks map; |
| 555 | struct buffer_head *bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 556 | int fatal = 0, err; |
| 557 | |
| 558 | J_ASSERT(handle != NULL || create == 0); |
| 559 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 560 | map.m_lblk = block; |
| 561 | map.m_len = 1; |
| 562 | err = ext4_map_blocks(handle, inode, &map, |
| 563 | create ? EXT4_GET_BLOCKS_CREATE : 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 564 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 565 | if (err < 0) |
| 566 | *errp = err; |
| 567 | if (err <= 0) |
| 568 | return NULL; |
| 569 | *errp = 0; |
| 570 | |
| 571 | bh = sb_getblk(inode->i_sb, map.m_pblk); |
| 572 | if (!bh) { |
| 573 | *errp = -EIO; |
| 574 | return NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 575 | } |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 576 | if (map.m_flags & EXT4_MAP_NEW) { |
| 577 | J_ASSERT(create != 0); |
| 578 | J_ASSERT(handle != NULL); |
| 579 | |
| 580 | /* |
| 581 | * Now that we do not always journal data, we should |
| 582 | * keep in mind whether this should always journal the |
| 583 | * new buffer as metadata. For now, regular file |
| 584 | * writes use ext4_get_block instead, so it's not a |
| 585 | * problem. |
| 586 | */ |
| 587 | lock_buffer(bh); |
| 588 | BUFFER_TRACE(bh, "call get_create_access"); |
| 589 | fatal = ext4_journal_get_create_access(handle, bh); |
| 590 | if (!fatal && !buffer_uptodate(bh)) { |
| 591 | memset(bh->b_data, 0, inode->i_sb->s_blocksize); |
| 592 | set_buffer_uptodate(bh); |
| 593 | } |
| 594 | unlock_buffer(bh); |
| 595 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
| 596 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
| 597 | if (!fatal) |
| 598 | fatal = err; |
| 599 | } else { |
| 600 | BUFFER_TRACE(bh, "not a new buffer"); |
| 601 | } |
| 602 | if (fatal) { |
| 603 | *errp = fatal; |
| 604 | brelse(bh); |
| 605 | bh = NULL; |
| 606 | } |
| 607 | return bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 610 | struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 611 | ext4_lblk_t block, int create, int *err) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 612 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 613 | struct buffer_head *bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 614 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 615 | bh = ext4_getblk(handle, inode, block, create, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 616 | if (!bh) |
| 617 | return bh; |
| 618 | if (buffer_uptodate(bh)) |
| 619 | return bh; |
| 620 | ll_rw_block(READ_META, 1, &bh); |
| 621 | wait_on_buffer(bh); |
| 622 | if (buffer_uptodate(bh)) |
| 623 | return bh; |
| 624 | put_bh(bh); |
| 625 | *err = -EIO; |
| 626 | return NULL; |
| 627 | } |
| 628 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 629 | static int walk_page_buffers(handle_t *handle, |
| 630 | struct buffer_head *head, |
| 631 | unsigned from, |
| 632 | unsigned to, |
| 633 | int *partial, |
| 634 | int (*fn)(handle_t *handle, |
| 635 | struct buffer_head *bh)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 636 | { |
| 637 | struct buffer_head *bh; |
| 638 | unsigned block_start, block_end; |
| 639 | unsigned blocksize = head->b_size; |
| 640 | int err, ret = 0; |
| 641 | struct buffer_head *next; |
| 642 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 643 | for (bh = head, block_start = 0; |
| 644 | ret == 0 && (bh != head || !block_start); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 645 | block_start = block_end, bh = next) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 646 | next = bh->b_this_page; |
| 647 | block_end = block_start + blocksize; |
| 648 | if (block_end <= from || block_start >= to) { |
| 649 | if (partial && !buffer_uptodate(bh)) |
| 650 | *partial = 1; |
| 651 | continue; |
| 652 | } |
| 653 | err = (*fn)(handle, bh); |
| 654 | if (!ret) |
| 655 | ret = err; |
| 656 | } |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * To preserve ordering, it is essential that the hole instantiation and |
| 662 | * the data write be encapsulated in a single transaction. We cannot |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 663 | * close off a transaction and start a new one between the ext4_get_block() |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 664 | * and the commit_write(). So doing the jbd2_journal_start at the start of |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 665 | * prepare_write() is the right place. |
| 666 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 667 | * Also, this function can nest inside ext4_writepage() -> |
| 668 | * block_write_full_page(). In that case, we *know* that ext4_writepage() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 669 | * has generated enough buffer credits to do the whole page. So we won't |
| 670 | * block on the journal in that case, which is good, because the caller may |
| 671 | * be PF_MEMALLOC. |
| 672 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 673 | * By accident, ext4 can be reentered when a transaction is open via |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 674 | * quota file writes. If we were to commit the transaction while thus |
| 675 | * reentered, there can be a deadlock - we would be holding a quota |
| 676 | * lock, and the commit would never complete if another thread had a |
| 677 | * transaction open and was blocking on the quota lock - a ranking |
| 678 | * violation. |
| 679 | * |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 680 | * So what we do is to rely on the fact that jbd2_journal_stop/journal_start |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 681 | * will _not_ run commit under these circumstances because handle->h_ref |
| 682 | * is elevated. We'll still have enough credits for the tiny quotafile |
| 683 | * write. |
| 684 | */ |
| 685 | static int do_journal_get_write_access(handle_t *handle, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 686 | struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 687 | { |
Jan Kara | 56d35a4 | 2010-08-05 14:41:42 -0400 | [diff] [blame] | 688 | int dirty = buffer_dirty(bh); |
| 689 | int ret; |
| 690 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 691 | if (!buffer_mapped(bh) || buffer_freed(bh)) |
| 692 | return 0; |
Jan Kara | 56d35a4 | 2010-08-05 14:41:42 -0400 | [diff] [blame] | 693 | /* |
Christoph Hellwig | ebdec24 | 2010-10-06 10:47:23 +0200 | [diff] [blame] | 694 | * __block_write_begin() could have dirtied some buffers. Clean |
Jan Kara | 56d35a4 | 2010-08-05 14:41:42 -0400 | [diff] [blame] | 695 | * the dirty bit as jbd2_journal_get_write_access() could complain |
| 696 | * otherwise about fs integrity issues. Setting of the dirty bit |
Christoph Hellwig | ebdec24 | 2010-10-06 10:47:23 +0200 | [diff] [blame] | 697 | * by __block_write_begin() isn't a real problem here as we clear |
Jan Kara | 56d35a4 | 2010-08-05 14:41:42 -0400 | [diff] [blame] | 698 | * the bit before releasing a page lock and thus writeback cannot |
| 699 | * ever write the buffer. |
| 700 | */ |
| 701 | if (dirty) |
| 702 | clear_buffer_dirty(bh); |
| 703 | ret = ext4_journal_get_write_access(handle, bh); |
| 704 | if (!ret && dirty) |
| 705 | ret = ext4_handle_dirty_metadata(handle, NULL, bh); |
| 706 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 709 | static int ext4_get_block_write(struct inode *inode, sector_t iblock, |
| 710 | struct buffer_head *bh_result, int create); |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 711 | static int ext4_write_begin(struct file *file, struct address_space *mapping, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 712 | loff_t pos, unsigned len, unsigned flags, |
| 713 | struct page **pagep, void **fsdata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 714 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 715 | struct inode *inode = mapping->host; |
Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 716 | int ret, needed_blocks; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 717 | handle_t *handle; |
| 718 | int retries = 0; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 719 | struct page *page; |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 720 | pgoff_t index; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 721 | unsigned from, to; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 722 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 723 | trace_ext4_write_begin(inode, pos, len, flags); |
Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 724 | /* |
| 725 | * Reserve one block more for addition to orphan list in case |
| 726 | * we allocate blocks but write fails for some reason |
| 727 | */ |
| 728 | needed_blocks = ext4_writepage_trans_blocks(inode) + 1; |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 729 | index = pos >> PAGE_CACHE_SHIFT; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 730 | from = pos & (PAGE_CACHE_SIZE - 1); |
| 731 | to = from + len; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 732 | |
| 733 | retry: |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 734 | handle = ext4_journal_start(inode, needed_blocks); |
| 735 | if (IS_ERR(handle)) { |
| 736 | ret = PTR_ERR(handle); |
| 737 | goto out; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Jan Kara | ebd3610 | 2009-02-22 21:09:59 -0500 | [diff] [blame] | 740 | /* We cannot recurse into the filesystem as the transaction is already |
| 741 | * started */ |
| 742 | flags |= AOP_FLAG_NOFS; |
| 743 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 744 | page = grab_cache_page_write_begin(mapping, index, flags); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 745 | if (!page) { |
| 746 | ext4_journal_stop(handle); |
| 747 | ret = -ENOMEM; |
| 748 | goto out; |
| 749 | } |
| 750 | *pagep = page; |
| 751 | |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 752 | if (ext4_should_dioread_nolock(inode)) |
Christoph Hellwig | 6e1db88 | 2010-06-04 11:29:57 +0200 | [diff] [blame] | 753 | ret = __block_write_begin(page, pos, len, ext4_get_block_write); |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 754 | else |
Christoph Hellwig | 6e1db88 | 2010-06-04 11:29:57 +0200 | [diff] [blame] | 755 | ret = __block_write_begin(page, pos, len, ext4_get_block); |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 756 | |
| 757 | if (!ret && ext4_should_journal_data(inode)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 758 | ret = walk_page_buffers(handle, page_buffers(page), |
| 759 | from, to, NULL, do_journal_get_write_access); |
| 760 | } |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 761 | |
| 762 | if (ret) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 763 | unlock_page(page); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 764 | page_cache_release(page); |
Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 765 | /* |
Christoph Hellwig | 6e1db88 | 2010-06-04 11:29:57 +0200 | [diff] [blame] | 766 | * __block_write_begin may have instantiated a few blocks |
Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 767 | * outside i_size. Trim these off again. Don't need |
| 768 | * i_size_read because we hold i_mutex. |
Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 769 | * |
| 770 | * Add inode to orphan list in case we crash before |
| 771 | * truncate finishes |
Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 772 | */ |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 773 | if (pos + len > inode->i_size && ext4_can_truncate(inode)) |
Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 774 | ext4_orphan_add(handle, inode); |
| 775 | |
| 776 | ext4_journal_stop(handle); |
| 777 | if (pos + len > inode->i_size) { |
Jan Kara | b9a4207 | 2009-12-08 21:24:33 -0500 | [diff] [blame] | 778 | ext4_truncate_failed_write(inode); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 779 | /* |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 780 | * If truncate failed early the inode might |
Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 781 | * still be on the orphan list; we need to |
| 782 | * make sure the inode is removed from the |
| 783 | * orphan list in that case. |
| 784 | */ |
| 785 | if (inode->i_nlink) |
| 786 | ext4_orphan_del(NULL, inode); |
| 787 | } |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 790 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 791 | goto retry; |
Andrew Morton | 7479d2b | 2007-04-01 23:49:44 -0700 | [diff] [blame] | 792 | out: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 793 | return ret; |
| 794 | } |
| 795 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 796 | /* For write_end() in data=journal mode */ |
| 797 | static int write_end_fn(handle_t *handle, struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 798 | { |
| 799 | if (!buffer_mapped(bh) || buffer_freed(bh)) |
| 800 | return 0; |
| 801 | set_buffer_uptodate(bh); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 802 | return ext4_handle_dirty_metadata(handle, NULL, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 805 | static int ext4_generic_write_end(struct file *file, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 806 | struct address_space *mapping, |
| 807 | loff_t pos, unsigned len, unsigned copied, |
| 808 | struct page *page, void *fsdata) |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 809 | { |
| 810 | int i_size_changed = 0; |
| 811 | struct inode *inode = mapping->host; |
| 812 | handle_t *handle = ext4_journal_current_handle(); |
| 813 | |
| 814 | copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); |
| 815 | |
| 816 | /* |
| 817 | * No need to use i_size_read() here, the i_size |
| 818 | * cannot change under us because we hold i_mutex. |
| 819 | * |
| 820 | * But it's important to update i_size while still holding page lock: |
| 821 | * page writeout could otherwise come in and zero beyond i_size. |
| 822 | */ |
| 823 | if (pos + copied > inode->i_size) { |
| 824 | i_size_write(inode, pos + copied); |
| 825 | i_size_changed = 1; |
| 826 | } |
| 827 | |
| 828 | if (pos + copied > EXT4_I(inode)->i_disksize) { |
| 829 | /* We need to mark inode dirty even if |
| 830 | * new_i_size is less that inode->i_size |
| 831 | * bu greater than i_disksize.(hint delalloc) |
| 832 | */ |
| 833 | ext4_update_i_disksize(inode, (pos + copied)); |
| 834 | i_size_changed = 1; |
| 835 | } |
| 836 | unlock_page(page); |
| 837 | page_cache_release(page); |
| 838 | |
| 839 | /* |
| 840 | * Don't mark the inode dirty under page lock. First, it unnecessarily |
| 841 | * makes the holding time of page lock longer. Second, it forces lock |
| 842 | * ordering of page lock and transaction start for journaling |
| 843 | * filesystems. |
| 844 | */ |
| 845 | if (i_size_changed) |
| 846 | ext4_mark_inode_dirty(handle, inode); |
| 847 | |
| 848 | return copied; |
| 849 | } |
| 850 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 851 | /* |
| 852 | * We need to pick up the new inode size which generic_commit_write gave us |
| 853 | * `file' can be NULL - eg, when called from page_symlink(). |
| 854 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 855 | * ext4 never places buffers on inode->i_mapping->private_list. metadata |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 856 | * buffers are managed internally. |
| 857 | */ |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 858 | static int ext4_ordered_write_end(struct file *file, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 859 | struct address_space *mapping, |
| 860 | loff_t pos, unsigned len, unsigned copied, |
| 861 | struct page *page, void *fsdata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 862 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 863 | handle_t *handle = ext4_journal_current_handle(); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 864 | struct inode *inode = mapping->host; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 865 | int ret = 0, ret2; |
| 866 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 867 | trace_ext4_ordered_write_end(inode, pos, len, copied); |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 868 | ret = ext4_jbd2_file_inode(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 869 | |
| 870 | if (ret == 0) { |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 871 | ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 872 | page, fsdata); |
Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 873 | copied = ret2; |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 874 | if (pos + len > inode->i_size && ext4_can_truncate(inode)) |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 875 | /* if we have allocated more blocks and copied |
| 876 | * less. We will have blocks allocated outside |
| 877 | * inode->i_size. So truncate them |
| 878 | */ |
| 879 | ext4_orphan_add(handle, inode); |
Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 880 | if (ret2 < 0) |
| 881 | ret = ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 882 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 883 | ret2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 884 | if (!ret) |
| 885 | ret = ret2; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 886 | |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 887 | if (pos + len > inode->i_size) { |
Jan Kara | b9a4207 | 2009-12-08 21:24:33 -0500 | [diff] [blame] | 888 | ext4_truncate_failed_write(inode); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 889 | /* |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 890 | * If truncate failed early the inode might still be |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 891 | * on the orphan list; we need to make sure the inode |
| 892 | * is removed from the orphan list in that case. |
| 893 | */ |
| 894 | if (inode->i_nlink) |
| 895 | ext4_orphan_del(NULL, inode); |
| 896 | } |
| 897 | |
| 898 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 899 | return ret ? ret : copied; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 902 | static int ext4_writeback_write_end(struct file *file, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 903 | struct address_space *mapping, |
| 904 | loff_t pos, unsigned len, unsigned copied, |
| 905 | struct page *page, void *fsdata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 906 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 907 | handle_t *handle = ext4_journal_current_handle(); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 908 | struct inode *inode = mapping->host; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 909 | int ret = 0, ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 910 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 911 | trace_ext4_writeback_write_end(inode, pos, len, copied); |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 912 | ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 913 | page, fsdata); |
Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 914 | copied = ret2; |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 915 | if (pos + len > inode->i_size && ext4_can_truncate(inode)) |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 916 | /* if we have allocated more blocks and copied |
| 917 | * less. We will have blocks allocated outside |
| 918 | * inode->i_size. So truncate them |
| 919 | */ |
| 920 | ext4_orphan_add(handle, inode); |
| 921 | |
Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 922 | if (ret2 < 0) |
| 923 | ret = ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 924 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 925 | ret2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 926 | if (!ret) |
| 927 | ret = ret2; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 928 | |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 929 | if (pos + len > inode->i_size) { |
Jan Kara | b9a4207 | 2009-12-08 21:24:33 -0500 | [diff] [blame] | 930 | ext4_truncate_failed_write(inode); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 931 | /* |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 932 | * If truncate failed early the inode might still be |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 933 | * on the orphan list; we need to make sure the inode |
| 934 | * is removed from the orphan list in that case. |
| 935 | */ |
| 936 | if (inode->i_nlink) |
| 937 | ext4_orphan_del(NULL, inode); |
| 938 | } |
| 939 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 940 | return ret ? ret : copied; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 943 | static int ext4_journalled_write_end(struct file *file, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 944 | struct address_space *mapping, |
| 945 | loff_t pos, unsigned len, unsigned copied, |
| 946 | struct page *page, void *fsdata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 947 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 948 | handle_t *handle = ext4_journal_current_handle(); |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 949 | struct inode *inode = mapping->host; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 950 | int ret = 0, ret2; |
| 951 | int partial = 0; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 952 | unsigned from, to; |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 953 | loff_t new_i_size; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 954 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 955 | trace_ext4_journalled_write_end(inode, pos, len, copied); |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 956 | from = pos & (PAGE_CACHE_SIZE - 1); |
| 957 | to = from + len; |
| 958 | |
| 959 | if (copied < len) { |
| 960 | if (!PageUptodate(page)) |
| 961 | copied = 0; |
| 962 | page_zero_new_buffers(page, from+copied, to); |
| 963 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 964 | |
| 965 | ret = walk_page_buffers(handle, page_buffers(page), from, |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 966 | to, &partial, write_end_fn); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 967 | if (!partial) |
| 968 | SetPageUptodate(page); |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 969 | new_i_size = pos + copied; |
| 970 | if (new_i_size > inode->i_size) |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 971 | i_size_write(inode, pos+copied); |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 972 | ext4_set_inode_state(inode, EXT4_STATE_JDATA); |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 973 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
| 974 | ext4_update_i_disksize(inode, new_i_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 975 | ret2 = ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 976 | if (!ret) |
| 977 | ret = ret2; |
| 978 | } |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 979 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 980 | unlock_page(page); |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 981 | page_cache_release(page); |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 982 | if (pos + len > inode->i_size && ext4_can_truncate(inode)) |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 983 | /* if we have allocated more blocks and copied |
| 984 | * less. We will have blocks allocated outside |
| 985 | * inode->i_size. So truncate them |
| 986 | */ |
| 987 | ext4_orphan_add(handle, inode); |
| 988 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 989 | ret2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 990 | if (!ret) |
| 991 | ret = ret2; |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 992 | if (pos + len > inode->i_size) { |
Jan Kara | b9a4207 | 2009-12-08 21:24:33 -0500 | [diff] [blame] | 993 | ext4_truncate_failed_write(inode); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 994 | /* |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 995 | * If truncate failed early the inode might still be |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 996 | * on the orphan list; we need to make sure the inode |
| 997 | * is removed from the orphan list in that case. |
| 998 | */ |
| 999 | if (inode->i_nlink) |
| 1000 | ext4_orphan_del(NULL, inode); |
| 1001 | } |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1002 | |
| 1003 | return ret ? ret : copied; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1004 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1005 | |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 1006 | /* |
| 1007 | * Reserve a single block located at lblock |
| 1008 | */ |
Theodore Ts'o | 01f49d0 | 2011-01-10 12:13:03 -0500 | [diff] [blame] | 1009 | static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1010 | { |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1011 | int retries = 0; |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1012 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 1013 | struct ext4_inode_info *ei = EXT4_I(inode); |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 1014 | unsigned long md_needed; |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 1015 | int ret; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1016 | |
| 1017 | /* |
| 1018 | * recalculate the amount of metadata blocks to reserve |
| 1019 | * in order to allocate nrblocks |
| 1020 | * worse case is one extent per block |
| 1021 | */ |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1022 | repeat: |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 1023 | spin_lock(&ei->i_block_reservation_lock); |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 1024 | md_needed = ext4_calc_metadata_amount(inode, lblock); |
Theodore Ts'o | f8ec9d6 | 2010-01-01 01:00:21 -0500 | [diff] [blame] | 1025 | trace_ext4_da_reserve_space(inode, md_needed); |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 1026 | spin_unlock(&ei->i_block_reservation_lock); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1027 | |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1028 | /* |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 1029 | * We will charge metadata quota at writeout time; this saves |
| 1030 | * us from metadata over-estimation, though we may go over by |
| 1031 | * a small amount in the end. Here we just reserve for data. |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1032 | */ |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 1033 | ret = dquot_reserve_block(inode, 1); |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 1034 | if (ret) |
| 1035 | return ret; |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 1036 | /* |
| 1037 | * We do still charge estimated metadata to the sb though; |
| 1038 | * we cannot afford to run out of free blocks. |
| 1039 | */ |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1040 | if (ext4_claim_free_blocks(sbi, md_needed + 1, 0)) { |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 1041 | dquot_release_reservation_block(inode, 1); |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1042 | if (ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 1043 | yield(); |
| 1044 | goto repeat; |
| 1045 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1046 | return -ENOSPC; |
| 1047 | } |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 1048 | spin_lock(&ei->i_block_reservation_lock); |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 1049 | ei->i_reserved_data_blocks++; |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 1050 | ei->i_reserved_meta_blocks += md_needed; |
| 1051 | spin_unlock(&ei->i_block_reservation_lock); |
Dmitry Monakhov | 39bc680 | 2009-12-10 16:36:27 +0000 | [diff] [blame] | 1052 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1053 | return 0; /* success */ |
| 1054 | } |
| 1055 | |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1056 | static void ext4_da_release_space(struct inode *inode, int to_free) |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1057 | { |
| 1058 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 1059 | struct ext4_inode_info *ei = EXT4_I(inode); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1060 | |
Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1061 | if (!to_free) |
| 1062 | return; /* Nothing to release, exit */ |
| 1063 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1064 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1065 | |
Li Zefan | 5a58ec8 | 2010-05-17 02:00:00 -0400 | [diff] [blame] | 1066 | trace_ext4_da_release_space(inode, to_free); |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 1067 | if (unlikely(to_free > ei->i_reserved_data_blocks)) { |
Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1068 | /* |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 1069 | * if there aren't enough reserved blocks, then the |
| 1070 | * counter is messed up somewhere. Since this |
| 1071 | * function is called from invalidate page, it's |
| 1072 | * harmless to return without any action. |
Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1073 | */ |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 1074 | ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: " |
| 1075 | "ino %lu, to_free %d with only %d reserved " |
| 1076 | "data blocks\n", inode->i_ino, to_free, |
| 1077 | ei->i_reserved_data_blocks); |
| 1078 | WARN_ON(1); |
| 1079 | to_free = ei->i_reserved_data_blocks; |
| 1080 | } |
| 1081 | ei->i_reserved_data_blocks -= to_free; |
| 1082 | |
| 1083 | if (ei->i_reserved_data_blocks == 0) { |
| 1084 | /* |
| 1085 | * We can release all of the reserved metadata blocks |
| 1086 | * only when we have written all of the delayed |
| 1087 | * allocation blocks. |
| 1088 | */ |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 1089 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, |
| 1090 | ei->i_reserved_meta_blocks); |
Theodore Ts'o | ee5f4d9 | 2010-01-01 02:36:15 -0500 | [diff] [blame] | 1091 | ei->i_reserved_meta_blocks = 0; |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 1092 | ei->i_da_metadata_calc_len = 0; |
Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1093 | } |
| 1094 | |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 1095 | /* update fs dirty data blocks counter */ |
Theodore Ts'o | 0637c6f | 2009-12-30 14:20:45 -0500 | [diff] [blame] | 1096 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, to_free); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1097 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1098 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1099 | |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 1100 | dquot_release_reservation_block(inode, to_free); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static void ext4_da_page_release_reservation(struct page *page, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1104 | unsigned long offset) |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1105 | { |
| 1106 | int to_release = 0; |
| 1107 | struct buffer_head *head, *bh; |
| 1108 | unsigned int curr_off = 0; |
| 1109 | |
| 1110 | head = page_buffers(page); |
| 1111 | bh = head; |
| 1112 | do { |
| 1113 | unsigned int next_off = curr_off + bh->b_size; |
| 1114 | |
| 1115 | if ((offset <= curr_off) && (buffer_delay(bh))) { |
| 1116 | to_release++; |
| 1117 | clear_buffer_delay(bh); |
| 1118 | } |
| 1119 | curr_off = next_off; |
| 1120 | } while ((bh = bh->b_this_page) != head); |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1121 | ext4_da_release_space(page->mapping->host, to_release); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1122 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1123 | |
| 1124 | /* |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1125 | * Delayed allocation stuff |
| 1126 | */ |
| 1127 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1128 | /* |
| 1129 | * mpage_da_submit_io - walks through extent of pages and try to write |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1130 | * them with writepage() call back |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1131 | * |
| 1132 | * @mpd->inode: inode |
| 1133 | * @mpd->first_page: first page of the extent |
| 1134 | * @mpd->next_page: page after the last page of the extent |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1135 | * |
| 1136 | * By the time mpage_da_submit_io() is called we expect all blocks |
| 1137 | * to be allocated. this may be wrong if allocation failed. |
| 1138 | * |
| 1139 | * As pages are already locked by write_cache_pages(), we can't use it |
| 1140 | */ |
Theodore Ts'o | 1de3e3d | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1141 | static int mpage_da_submit_io(struct mpage_da_data *mpd, |
| 1142 | struct ext4_map_blocks *map) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1143 | { |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1144 | struct pagevec pvec; |
| 1145 | unsigned long index, end; |
| 1146 | int ret = 0, err, nr_pages, i; |
| 1147 | struct inode *inode = mpd->inode; |
| 1148 | struct address_space *mapping = inode->i_mapping; |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1149 | loff_t size = i_size_read(inode); |
Theodore Ts'o | 3ecdb3a | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1150 | unsigned int len, block_start; |
| 1151 | struct buffer_head *bh, *page_bufs = NULL; |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1152 | int journal_data = ext4_should_journal_data(inode); |
Theodore Ts'o | 1de3e3d | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1153 | sector_t pblock = 0, cur_logical = 0; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1154 | struct ext4_io_submit io_submit; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1155 | |
| 1156 | BUG_ON(mpd->next_page <= mpd->first_page); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1157 | memset(&io_submit, 0, sizeof(io_submit)); |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1158 | /* |
| 1159 | * We need to start from the first_page to the next_page - 1 |
| 1160 | * to make sure we also write the mapped dirty buffer_heads. |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 1161 | * If we look at mpd->b_blocknr we would only be looking |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1162 | * at the currently mapped buffer_heads. |
| 1163 | */ |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1164 | index = mpd->first_page; |
| 1165 | end = mpd->next_page - 1; |
| 1166 | |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1167 | pagevec_init(&pvec, 0); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1168 | while (index <= end) { |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1169 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1170 | if (nr_pages == 0) |
| 1171 | break; |
| 1172 | for (i = 0; i < nr_pages; i++) { |
Theodore Ts'o | 9749895 | 2011-02-26 14:08:01 -0500 | [diff] [blame] | 1173 | int commit_write = 0, skip_page = 0; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1174 | struct page *page = pvec.pages[i]; |
| 1175 | |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1176 | index = page->index; |
| 1177 | if (index > end) |
| 1178 | break; |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1179 | |
| 1180 | if (index == size >> PAGE_CACHE_SHIFT) |
| 1181 | len = size & ~PAGE_CACHE_MASK; |
| 1182 | else |
| 1183 | len = PAGE_CACHE_SIZE; |
Theodore Ts'o | 1de3e3d | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1184 | if (map) { |
| 1185 | cur_logical = index << (PAGE_CACHE_SHIFT - |
| 1186 | inode->i_blkbits); |
| 1187 | pblock = map->m_pblk + (cur_logical - |
| 1188 | map->m_lblk); |
| 1189 | } |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1190 | index++; |
| 1191 | |
| 1192 | BUG_ON(!PageLocked(page)); |
| 1193 | BUG_ON(PageWriteback(page)); |
| 1194 | |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1195 | /* |
| 1196 | * If the page does not have buffers (for |
| 1197 | * whatever reason), try to create them using |
Theodore Ts'o | a107e5a | 2010-10-27 23:44:47 -0400 | [diff] [blame] | 1198 | * __block_write_begin. If this fails, |
Theodore Ts'o | 9749895 | 2011-02-26 14:08:01 -0500 | [diff] [blame] | 1199 | * skip the page and move on. |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1200 | */ |
| 1201 | if (!page_has_buffers(page)) { |
Theodore Ts'o | a107e5a | 2010-10-27 23:44:47 -0400 | [diff] [blame] | 1202 | if (__block_write_begin(page, 0, len, |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1203 | noalloc_get_block_write)) { |
Theodore Ts'o | 9749895 | 2011-02-26 14:08:01 -0500 | [diff] [blame] | 1204 | skip_page: |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1205 | unlock_page(page); |
| 1206 | continue; |
| 1207 | } |
| 1208 | commit_write = 1; |
| 1209 | } |
Theodore Ts'o | 3ecdb3a | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1210 | |
| 1211 | bh = page_bufs = page_buffers(page); |
| 1212 | block_start = 0; |
| 1213 | do { |
Theodore Ts'o | 1de3e3d | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1214 | if (!bh) |
Theodore Ts'o | 9749895 | 2011-02-26 14:08:01 -0500 | [diff] [blame] | 1215 | goto skip_page; |
Theodore Ts'o | 1de3e3d | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1216 | if (map && (cur_logical >= map->m_lblk) && |
| 1217 | (cur_logical <= (map->m_lblk + |
| 1218 | (map->m_len - 1)))) { |
| 1219 | if (buffer_delay(bh)) { |
| 1220 | clear_buffer_delay(bh); |
| 1221 | bh->b_blocknr = pblock; |
| 1222 | } |
| 1223 | if (buffer_unwritten(bh) || |
| 1224 | buffer_mapped(bh)) |
| 1225 | BUG_ON(bh->b_blocknr != pblock); |
| 1226 | if (map->m_flags & EXT4_MAP_UNINIT) |
| 1227 | set_buffer_uninit(bh); |
| 1228 | clear_buffer_unwritten(bh); |
| 1229 | } |
| 1230 | |
Theodore Ts'o | 9749895 | 2011-02-26 14:08:01 -0500 | [diff] [blame] | 1231 | /* skip page if block allocation undone */ |
Theodore Ts'o | 1de3e3d | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1232 | if (buffer_delay(bh) || buffer_unwritten(bh)) |
Theodore Ts'o | 9749895 | 2011-02-26 14:08:01 -0500 | [diff] [blame] | 1233 | skip_page = 1; |
Theodore Ts'o | 3ecdb3a | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1234 | bh = bh->b_this_page; |
| 1235 | block_start += bh->b_size; |
Theodore Ts'o | 1de3e3d | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1236 | cur_logical++; |
| 1237 | pblock++; |
| 1238 | } while (bh != page_bufs); |
| 1239 | |
Theodore Ts'o | 9749895 | 2011-02-26 14:08:01 -0500 | [diff] [blame] | 1240 | if (skip_page) |
| 1241 | goto skip_page; |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1242 | |
| 1243 | if (commit_write) |
| 1244 | /* mark the buffer_heads as dirty & uptodate */ |
| 1245 | block_commit_write(page, 0, len); |
| 1246 | |
Theodore Ts'o | 9749895 | 2011-02-26 14:08:01 -0500 | [diff] [blame] | 1247 | clear_page_dirty_for_io(page); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1248 | /* |
| 1249 | * Delalloc doesn't support data journalling, |
| 1250 | * but eventually maybe we'll lift this |
| 1251 | * restriction. |
| 1252 | */ |
| 1253 | if (unlikely(journal_data && PageChecked(page))) |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1254 | err = __ext4_journalled_writepage(page, len); |
Theodore Ts'o | 1449032 | 2010-12-14 15:27:50 -0500 | [diff] [blame] | 1255 | else if (test_opt(inode->i_sb, MBLK_IO_SUBMIT)) |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1256 | err = ext4_bio_write_page(&io_submit, page, |
| 1257 | len, mpd->wbc); |
Theodore Ts'o | 1449032 | 2010-12-14 15:27:50 -0500 | [diff] [blame] | 1258 | else |
| 1259 | err = block_write_full_page(page, |
| 1260 | noalloc_get_block_write, mpd->wbc); |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1261 | |
| 1262 | if (!err) |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1263 | mpd->pages_written++; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1264 | /* |
| 1265 | * In error case, we have to continue because |
| 1266 | * remaining pages are still locked |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1267 | */ |
| 1268 | if (ret == 0) |
| 1269 | ret = err; |
| 1270 | } |
| 1271 | pagevec_release(&pvec); |
| 1272 | } |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1273 | ext4_io_submit(&io_submit); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1274 | return ret; |
| 1275 | } |
| 1276 | |
Curt Wohlgemuth | c7f5938 | 2011-02-26 12:27:52 -0500 | [diff] [blame] | 1277 | static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1278 | { |
| 1279 | int nr_pages, i; |
| 1280 | pgoff_t index, end; |
| 1281 | struct pagevec pvec; |
| 1282 | struct inode *inode = mpd->inode; |
| 1283 | struct address_space *mapping = inode->i_mapping; |
| 1284 | |
Curt Wohlgemuth | c7f5938 | 2011-02-26 12:27:52 -0500 | [diff] [blame] | 1285 | index = mpd->first_page; |
| 1286 | end = mpd->next_page - 1; |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1287 | while (index <= end) { |
| 1288 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); |
| 1289 | if (nr_pages == 0) |
| 1290 | break; |
| 1291 | for (i = 0; i < nr_pages; i++) { |
| 1292 | struct page *page = pvec.pages[i]; |
Jan Kara | 9b1d0998 | 2010-03-03 16:19:32 -0500 | [diff] [blame] | 1293 | if (page->index > end) |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1294 | break; |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1295 | BUG_ON(!PageLocked(page)); |
| 1296 | BUG_ON(PageWriteback(page)); |
| 1297 | block_invalidatepage(page, 0); |
| 1298 | ClearPageUptodate(page); |
| 1299 | unlock_page(page); |
| 1300 | } |
Jan Kara | 9b1d0998 | 2010-03-03 16:19:32 -0500 | [diff] [blame] | 1301 | index = pvec.pages[nr_pages - 1]->index + 1; |
| 1302 | pagevec_release(&pvec); |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1303 | } |
| 1304 | return; |
| 1305 | } |
| 1306 | |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1307 | static void ext4_print_free_blocks(struct inode *inode) |
| 1308 | { |
| 1309 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Theodore Ts'o | 1693918 | 2009-09-26 17:43:59 -0400 | [diff] [blame] | 1310 | printk(KERN_CRIT "Total free blocks count %lld\n", |
| 1311 | ext4_count_free_blocks(inode->i_sb)); |
| 1312 | printk(KERN_CRIT "Free/Dirty block details\n"); |
| 1313 | printk(KERN_CRIT "free_blocks=%lld\n", |
| 1314 | (long long) percpu_counter_sum(&sbi->s_freeblocks_counter)); |
| 1315 | printk(KERN_CRIT "dirty_blocks=%lld\n", |
| 1316 | (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter)); |
| 1317 | printk(KERN_CRIT "Block reservation details\n"); |
| 1318 | printk(KERN_CRIT "i_reserved_data_blocks=%u\n", |
| 1319 | EXT4_I(inode)->i_reserved_data_blocks); |
| 1320 | printk(KERN_CRIT "i_reserved_meta_blocks=%u\n", |
| 1321 | EXT4_I(inode)->i_reserved_meta_blocks); |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1322 | return; |
| 1323 | } |
| 1324 | |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 1325 | /* |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1326 | * mpage_da_map_and_submit - go through given space, map them |
| 1327 | * if necessary, and then submit them for I/O |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1328 | * |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 1329 | * @mpd - bh describing space |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1330 | * |
| 1331 | * The function skips space we know is already mapped to disk blocks. |
| 1332 | * |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1333 | */ |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1334 | static void mpage_da_map_and_submit(struct mpage_da_data *mpd) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1335 | { |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 1336 | int err, blks, get_blocks_flags; |
Theodore Ts'o | 1de3e3d | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1337 | struct ext4_map_blocks map, *mapp = NULL; |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 1338 | sector_t next = mpd->b_blocknr; |
| 1339 | unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; |
| 1340 | loff_t disksize = EXT4_I(mpd->inode)->i_disksize; |
| 1341 | handle_t *handle = NULL; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1342 | |
| 1343 | /* |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1344 | * If the blocks are mapped already, or we couldn't accumulate |
| 1345 | * any blocks, then proceed immediately to the submission stage. |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1346 | */ |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1347 | if ((mpd->b_size == 0) || |
| 1348 | ((mpd->b_state & (1 << BH_Mapped)) && |
| 1349 | !(mpd->b_state & (1 << BH_Delay)) && |
| 1350 | !(mpd->b_state & (1 << BH_Unwritten)))) |
| 1351 | goto submit_io; |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 1352 | |
| 1353 | handle = ext4_journal_current_handle(); |
| 1354 | BUG_ON(!handle); |
| 1355 | |
Aneesh Kumar K.V | 79ffab3 | 2009-05-13 15:13:42 -0400 | [diff] [blame] | 1356 | /* |
Eric Sandeen | 79e8303 | 2010-07-27 11:56:07 -0400 | [diff] [blame] | 1357 | * Call ext4_map_blocks() to allocate any delayed allocation |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 1358 | * blocks, or to convert an uninitialized extent to be |
| 1359 | * initialized (in the case where we have written into |
| 1360 | * one or more preallocated blocks). |
| 1361 | * |
| 1362 | * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to |
| 1363 | * indicate that we are on the delayed allocation path. This |
| 1364 | * affects functions in many different parts of the allocation |
| 1365 | * call path. This flag exists primarily because we don't |
Eric Sandeen | 79e8303 | 2010-07-27 11:56:07 -0400 | [diff] [blame] | 1366 | * want to change *many* call functions, so ext4_map_blocks() |
Theodore Ts'o | f232109 | 2011-01-10 12:12:36 -0500 | [diff] [blame] | 1367 | * will set the EXT4_STATE_DELALLOC_RESERVED flag once the |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 1368 | * inode's allocation semaphore is taken. |
| 1369 | * |
| 1370 | * If the blocks in questions were delalloc blocks, set |
| 1371 | * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting |
| 1372 | * variables are updated after the blocks have been allocated. |
Aneesh Kumar K.V | 79ffab3 | 2009-05-13 15:13:42 -0400 | [diff] [blame] | 1373 | */ |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1374 | map.m_lblk = next; |
| 1375 | map.m_len = max_blocks; |
Aneesh Kumar K.V | 1296cc8 | 2010-01-15 01:27:59 -0500 | [diff] [blame] | 1376 | get_blocks_flags = EXT4_GET_BLOCKS_CREATE; |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 1377 | if (ext4_should_dioread_nolock(mpd->inode)) |
| 1378 | get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 1379 | if (mpd->b_state & (1 << BH_Delay)) |
Aneesh Kumar K.V | 1296cc8 | 2010-01-15 01:27:59 -0500 | [diff] [blame] | 1380 | get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; |
| 1381 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1382 | blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 1383 | if (blks < 0) { |
Eric Sandeen | e357063 | 2010-07-27 11:56:08 -0400 | [diff] [blame] | 1384 | struct super_block *sb = mpd->inode->i_sb; |
| 1385 | |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 1386 | err = blks; |
Theodore Ts'o | ed5bde0 | 2009-02-23 10:48:07 -0500 | [diff] [blame] | 1387 | /* |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1388 | * If get block returns EAGAIN or ENOSPC and there |
Theodore Ts'o | 9749895 | 2011-02-26 14:08:01 -0500 | [diff] [blame] | 1389 | * appears to be free blocks we will just let |
| 1390 | * mpage_da_submit_io() unlock all of the pages. |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1391 | */ |
| 1392 | if (err == -EAGAIN) |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1393 | goto submit_io; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1394 | |
| 1395 | if (err == -ENOSPC && |
Eric Sandeen | e357063 | 2010-07-27 11:56:08 -0400 | [diff] [blame] | 1396 | ext4_count_free_blocks(sb)) { |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1397 | mpd->retval = err; |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1398 | goto submit_io; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1399 | } |
| 1400 | |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1401 | /* |
Theodore Ts'o | ed5bde0 | 2009-02-23 10:48:07 -0500 | [diff] [blame] | 1402 | * get block failure will cause us to loop in |
| 1403 | * writepages, because a_ops->writepage won't be able |
| 1404 | * to make progress. The page will be redirtied by |
| 1405 | * writepage and writepages will again try to write |
| 1406 | * the same. |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1407 | */ |
Eric Sandeen | e357063 | 2010-07-27 11:56:08 -0400 | [diff] [blame] | 1408 | if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { |
| 1409 | ext4_msg(sb, KERN_CRIT, |
| 1410 | "delayed block allocation failed for inode %lu " |
| 1411 | "at logical offset %llu with max blocks %zd " |
| 1412 | "with error %d", mpd->inode->i_ino, |
| 1413 | (unsigned long long) next, |
| 1414 | mpd->b_size >> mpd->inode->i_blkbits, err); |
| 1415 | ext4_msg(sb, KERN_CRIT, |
| 1416 | "This should not happen!! Data will be lost\n"); |
| 1417 | if (err == -ENOSPC) |
| 1418 | ext4_print_free_blocks(mpd->inode); |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1419 | } |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 1420 | /* invalidate all the pages */ |
Curt Wohlgemuth | c7f5938 | 2011-02-26 12:27:52 -0500 | [diff] [blame] | 1421 | ext4_da_block_invalidatepages(mpd); |
Curt Wohlgemuth | e0fd9b9 | 2011-02-26 12:25:52 -0500 | [diff] [blame] | 1422 | |
| 1423 | /* Mark this page range as having been completed */ |
| 1424 | mpd->io_done = 1; |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1425 | return; |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1426 | } |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 1427 | BUG_ON(blks == 0); |
| 1428 | |
Theodore Ts'o | 1de3e3d | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1429 | mapp = ↦ |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1430 | if (map.m_flags & EXT4_MAP_NEW) { |
| 1431 | struct block_device *bdev = mpd->inode->i_sb->s_bdev; |
| 1432 | int i; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1433 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1434 | for (i = 0; i < map.m_len; i++) |
| 1435 | unmap_underlying_metadata(bdev, map.m_pblk + i); |
| 1436 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1437 | |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 1438 | if (ext4_should_order_data(mpd->inode)) { |
| 1439 | err = ext4_jbd2_file_inode(handle, mpd->inode); |
| 1440 | if (err) |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1441 | /* This only happens if the journal is aborted */ |
| 1442 | return; |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | /* |
Jan Kara | 03f5d8b | 2009-06-09 00:17:05 -0400 | [diff] [blame] | 1446 | * Update on-disk size along with block allocation. |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 1447 | */ |
| 1448 | disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; |
| 1449 | if (disksize > i_size_read(mpd->inode)) |
| 1450 | disksize = i_size_read(mpd->inode); |
| 1451 | if (disksize > EXT4_I(mpd->inode)->i_disksize) { |
| 1452 | ext4_update_i_disksize(mpd->inode, disksize); |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1453 | err = ext4_mark_inode_dirty(handle, mpd->inode); |
| 1454 | if (err) |
| 1455 | ext4_error(mpd->inode->i_sb, |
| 1456 | "Failed to mark inode %lu dirty", |
| 1457 | mpd->inode->i_ino); |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 1458 | } |
| 1459 | |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1460 | submit_io: |
Theodore Ts'o | 1de3e3d | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 1461 | mpage_da_submit_io(mpd, mapp); |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1462 | mpd->io_done = 1; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1463 | } |
| 1464 | |
Aneesh Kumar K.V | bf068ee | 2008-08-19 22:16:43 -0400 | [diff] [blame] | 1465 | #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ |
| 1466 | (1 << BH_Delay) | (1 << BH_Unwritten)) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1467 | |
| 1468 | /* |
| 1469 | * mpage_add_bh_to_extent - try to add one more block to extent of blocks |
| 1470 | * |
| 1471 | * @mpd->lbh - extent of blocks |
| 1472 | * @logical - logical number of the block in the file |
| 1473 | * @bh - bh of the block (used to access block's state) |
| 1474 | * |
| 1475 | * the function is used to collect contig. blocks in same state |
| 1476 | */ |
| 1477 | static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 1478 | sector_t logical, size_t b_size, |
| 1479 | unsigned long b_state) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1480 | { |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1481 | sector_t next; |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 1482 | int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1483 | |
Eric Sandeen | c445e3e | 2010-05-16 04:00:00 -0400 | [diff] [blame] | 1484 | /* |
| 1485 | * XXX Don't go larger than mballoc is willing to allocate |
| 1486 | * This is a stopgap solution. We eventually need to fold |
| 1487 | * mpage_da_submit_io() into this function and then call |
Eric Sandeen | 79e8303 | 2010-07-27 11:56:07 -0400 | [diff] [blame] | 1488 | * ext4_map_blocks() multiple times in a loop |
Eric Sandeen | c445e3e | 2010-05-16 04:00:00 -0400 | [diff] [blame] | 1489 | */ |
| 1490 | if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize) |
| 1491 | goto flush_it; |
| 1492 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1493 | /* check if thereserved journal credits might overflow */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 1494 | if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) { |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1495 | if (nrblocks >= EXT4_MAX_TRANS_DATA) { |
| 1496 | /* |
| 1497 | * With non-extent format we are limited by the journal |
| 1498 | * credit available. Total credit needed to insert |
| 1499 | * nrblocks contiguous blocks is dependent on the |
| 1500 | * nrblocks. So limit nrblocks. |
| 1501 | */ |
| 1502 | goto flush_it; |
| 1503 | } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > |
| 1504 | EXT4_MAX_TRANS_DATA) { |
| 1505 | /* |
| 1506 | * Adding the new buffer_head would make it cross the |
| 1507 | * allowed limit for which we have journal credit |
| 1508 | * reserved. So limit the new bh->b_size |
| 1509 | */ |
| 1510 | b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << |
| 1511 | mpd->inode->i_blkbits; |
| 1512 | /* we will do mpage_da_submit_io in the next loop */ |
| 1513 | } |
| 1514 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1515 | /* |
| 1516 | * First block in the extent |
| 1517 | */ |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 1518 | if (mpd->b_size == 0) { |
| 1519 | mpd->b_blocknr = logical; |
| 1520 | mpd->b_size = b_size; |
| 1521 | mpd->b_state = b_state & BH_FLAGS; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1522 | return; |
| 1523 | } |
| 1524 | |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 1525 | next = mpd->b_blocknr + nrblocks; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1526 | /* |
| 1527 | * Can we merge the block to our big extent? |
| 1528 | */ |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 1529 | if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { |
| 1530 | mpd->b_size += b_size; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1531 | return; |
| 1532 | } |
| 1533 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1534 | flush_it: |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1535 | /* |
| 1536 | * We couldn't merge the block to our extent, so we |
| 1537 | * need to flush current extent and start new one |
| 1538 | */ |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1539 | mpage_da_map_and_submit(mpd); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1540 | return; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1541 | } |
| 1542 | |
Aneesh Kumar K.V | c364b22 | 2009-06-14 17:57:10 -0400 | [diff] [blame] | 1543 | static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 1544 | { |
Aneesh Kumar K.V | c364b22 | 2009-06-14 17:57:10 -0400 | [diff] [blame] | 1545 | return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 1546 | } |
| 1547 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1548 | /* |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 1549 | * This is a special get_blocks_t callback which is used by |
| 1550 | * ext4_da_write_begin(). It will either return mapped block or |
| 1551 | * reserve space for a single block. |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 1552 | * |
| 1553 | * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. |
| 1554 | * We also have b_blocknr = -1 and b_bdev initialized properly |
| 1555 | * |
| 1556 | * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. |
| 1557 | * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev |
| 1558 | * initialized properly. |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1559 | */ |
| 1560 | static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1561 | struct buffer_head *bh, int create) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1562 | { |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1563 | struct ext4_map_blocks map; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1564 | int ret = 0; |
Aneesh Kumar K.V | 33b9817 | 2009-05-12 14:40:37 -0400 | [diff] [blame] | 1565 | sector_t invalid_block = ~((sector_t) 0xffff); |
| 1566 | |
| 1567 | if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) |
| 1568 | invalid_block = ~0; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1569 | |
| 1570 | BUG_ON(create == 0); |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1571 | BUG_ON(bh->b_size != inode->i_sb->s_blocksize); |
| 1572 | |
| 1573 | map.m_lblk = iblock; |
| 1574 | map.m_len = 1; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1575 | |
| 1576 | /* |
| 1577 | * first, we need to know whether the block is allocated already |
| 1578 | * preallocated blocks are unmapped but should treated |
| 1579 | * the same as allocated blocks. |
| 1580 | */ |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1581 | ret = ext4_map_blocks(NULL, inode, &map, 0); |
| 1582 | if (ret < 0) |
| 1583 | return ret; |
| 1584 | if (ret == 0) { |
| 1585 | if (buffer_delay(bh)) |
| 1586 | return 0; /* Not sure this could or should happen */ |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1587 | /* |
Christoph Hellwig | ebdec24 | 2010-10-06 10:47:23 +0200 | [diff] [blame] | 1588 | * XXX: __block_write_begin() unmaps passed block, is it OK? |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1589 | */ |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 1590 | ret = ext4_da_reserve_space(inode, iblock); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1591 | if (ret) |
| 1592 | /* not enough space to reserve */ |
| 1593 | return ret; |
| 1594 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1595 | map_bh(bh, inode->i_sb, invalid_block); |
| 1596 | set_buffer_new(bh); |
| 1597 | set_buffer_delay(bh); |
| 1598 | return 0; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1599 | } |
| 1600 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1601 | map_bh(bh, inode->i_sb, map.m_pblk); |
| 1602 | bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; |
| 1603 | |
| 1604 | if (buffer_unwritten(bh)) { |
| 1605 | /* A delayed write to unwritten bh should be marked |
| 1606 | * new and mapped. Mapped ensures that we don't do |
| 1607 | * get_block multiple times when we write to the same |
| 1608 | * offset and new ensures that we do proper zero out |
| 1609 | * for partial write. |
| 1610 | */ |
| 1611 | set_buffer_new(bh); |
Theodore Ts'o | c820563 | 2011-04-10 22:30:07 -0400 | [diff] [blame] | 1612 | set_buffer_mapped(bh); |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1613 | } |
| 1614 | return 0; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1615 | } |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1616 | |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 1617 | /* |
| 1618 | * This function is used as a standard get_block_t calback function |
| 1619 | * when there is no desire to allocate any blocks. It is used as a |
Christoph Hellwig | ebdec24 | 2010-10-06 10:47:23 +0200 | [diff] [blame] | 1620 | * callback function for block_write_begin() and block_write_full_page(). |
Christoph Hellwig | 206f7ab | 2010-06-14 14:42:49 -0400 | [diff] [blame] | 1621 | * These functions should only try to map a single block at a time. |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 1622 | * |
| 1623 | * Since this function doesn't do block allocations even if the caller |
| 1624 | * requests it by passing in create=1, it is critically important that |
| 1625 | * any caller checks to make sure that any buffer heads are returned |
| 1626 | * by this function are either all already mapped or marked for |
Christoph Hellwig | 206f7ab | 2010-06-14 14:42:49 -0400 | [diff] [blame] | 1627 | * delayed allocation before calling block_write_full_page(). Otherwise, |
| 1628 | * b_blocknr could be left unitialized, and the page write functions will |
| 1629 | * be taken by surprise. |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 1630 | */ |
| 1631 | static int noalloc_get_block_write(struct inode *inode, sector_t iblock, |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1632 | struct buffer_head *bh_result, int create) |
| 1633 | { |
Theodore Ts'o | a2dc52b | 2009-05-12 13:51:29 -0400 | [diff] [blame] | 1634 | BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 1635 | return _ext4_get_block(inode, iblock, bh_result, 0); |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1636 | } |
| 1637 | |
Aneesh Kumar K.V | 62e086b | 2009-06-14 17:59:34 -0400 | [diff] [blame] | 1638 | static int bget_one(handle_t *handle, struct buffer_head *bh) |
| 1639 | { |
| 1640 | get_bh(bh); |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
| 1644 | static int bput_one(handle_t *handle, struct buffer_head *bh) |
| 1645 | { |
| 1646 | put_bh(bh); |
| 1647 | return 0; |
| 1648 | } |
| 1649 | |
| 1650 | static int __ext4_journalled_writepage(struct page *page, |
Aneesh Kumar K.V | 62e086b | 2009-06-14 17:59:34 -0400 | [diff] [blame] | 1651 | unsigned int len) |
| 1652 | { |
| 1653 | struct address_space *mapping = page->mapping; |
| 1654 | struct inode *inode = mapping->host; |
| 1655 | struct buffer_head *page_bufs; |
| 1656 | handle_t *handle = NULL; |
| 1657 | int ret = 0; |
| 1658 | int err; |
| 1659 | |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1660 | ClearPageChecked(page); |
Aneesh Kumar K.V | 62e086b | 2009-06-14 17:59:34 -0400 | [diff] [blame] | 1661 | page_bufs = page_buffers(page); |
| 1662 | BUG_ON(!page_bufs); |
| 1663 | walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); |
| 1664 | /* As soon as we unlock the page, it can go away, but we have |
| 1665 | * references to buffers so we are safe */ |
| 1666 | unlock_page(page); |
| 1667 | |
| 1668 | handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); |
| 1669 | if (IS_ERR(handle)) { |
| 1670 | ret = PTR_ERR(handle); |
| 1671 | goto out; |
| 1672 | } |
| 1673 | |
| 1674 | ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, |
| 1675 | do_journal_get_write_access); |
| 1676 | |
| 1677 | err = walk_page_buffers(handle, page_bufs, 0, len, NULL, |
| 1678 | write_end_fn); |
| 1679 | if (ret == 0) |
| 1680 | ret = err; |
| 1681 | err = ext4_journal_stop(handle); |
| 1682 | if (!ret) |
| 1683 | ret = err; |
| 1684 | |
| 1685 | walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1686 | ext4_set_inode_state(inode, EXT4_STATE_JDATA); |
Aneesh Kumar K.V | 62e086b | 2009-06-14 17:59:34 -0400 | [diff] [blame] | 1687 | out: |
| 1688 | return ret; |
| 1689 | } |
| 1690 | |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 1691 | static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); |
| 1692 | static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); |
| 1693 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1694 | /* |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 1695 | * Note that we don't need to start a transaction unless we're journaling data |
| 1696 | * because we should have holes filled from ext4_page_mkwrite(). We even don't |
| 1697 | * need to file the inode to the transaction's list in ordered mode because if |
| 1698 | * we are writing back data added by write(), the inode is already there and if |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1699 | * we are writing back data modified via mmap(), no one guarantees in which |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 1700 | * transaction the data will hit the disk. In case we are journaling data, we |
| 1701 | * cannot start transaction directly because transaction start ranks above page |
| 1702 | * lock so we have to do some magic. |
| 1703 | * |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 1704 | * This function can get called via... |
| 1705 | * - ext4_da_writepages after taking page lock (have journal handle) |
| 1706 | * - journal_submit_inode_data_buffers (no journal handle) |
| 1707 | * - shrink_page_list via pdflush (no journal handle) |
| 1708 | * - grab_page_cache when doing write_begin (have journal handle) |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 1709 | * |
| 1710 | * We don't do any block allocation in this function. If we have page with |
| 1711 | * multiple blocks we need to write those buffer_heads that are mapped. This |
| 1712 | * is important for mmaped based write. So if we do with blocksize 1K |
| 1713 | * truncate(f, 1024); |
| 1714 | * a = mmap(f, 0, 4096); |
| 1715 | * a[0] = 'a'; |
| 1716 | * truncate(f, 4096); |
| 1717 | * we have in the page first buffer_head mapped via page_mkwrite call back |
| 1718 | * but other bufer_heads would be unmapped but dirty(dirty done via the |
| 1719 | * do_wp_page). So writepage should write the first block. If we modify |
| 1720 | * the mmap area beyond 1024 we will again get a page_fault and the |
| 1721 | * page_mkwrite callback will do the block allocation and mark the |
| 1722 | * buffer_heads mapped. |
| 1723 | * |
| 1724 | * We redirty the page if we have any buffer_heads that is either delay or |
| 1725 | * unwritten in the page. |
| 1726 | * |
| 1727 | * We can get recursively called as show below. |
| 1728 | * |
| 1729 | * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> |
| 1730 | * ext4_writepage() |
| 1731 | * |
| 1732 | * But since we don't do any block allocation we should not deadlock. |
| 1733 | * Page also have the dirty flag cleared so we don't get recurive page_lock. |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1734 | */ |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 1735 | static int ext4_writepage(struct page *page, |
Aneesh Kumar K.V | 62e086b | 2009-06-14 17:59:34 -0400 | [diff] [blame] | 1736 | struct writeback_control *wbc) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1737 | { |
Theodore Ts'o | a42afc5 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1738 | int ret = 0, commit_write = 0; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1739 | loff_t size; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1740 | unsigned int len; |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 1741 | struct buffer_head *page_bufs = NULL; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1742 | struct inode *inode = page->mapping->host; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1743 | |
Lukas Czerner | a9c667f | 2011-06-06 09:51:52 -0400 | [diff] [blame] | 1744 | trace_ext4_writepage(page); |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1745 | size = i_size_read(inode); |
| 1746 | if (page->index == size >> PAGE_CACHE_SHIFT) |
| 1747 | len = size & ~PAGE_CACHE_MASK; |
| 1748 | else |
| 1749 | len = PAGE_CACHE_SIZE; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1750 | |
Theodore Ts'o | a42afc5 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1751 | /* |
| 1752 | * If the page does not have buffers (for whatever reason), |
Theodore Ts'o | a107e5a | 2010-10-27 23:44:47 -0400 | [diff] [blame] | 1753 | * try to create them using __block_write_begin. If this |
Theodore Ts'o | a42afc5 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1754 | * fails, redirty the page and move on. |
| 1755 | */ |
Theodore Ts'o | b1142e8 | 2010-10-28 17:33:57 -0400 | [diff] [blame] | 1756 | if (!page_has_buffers(page)) { |
Theodore Ts'o | a107e5a | 2010-10-27 23:44:47 -0400 | [diff] [blame] | 1757 | if (__block_write_begin(page, 0, len, |
Theodore Ts'o | a42afc5 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1758 | noalloc_get_block_write)) { |
| 1759 | redirty_page: |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1760 | redirty_page_for_writepage(wbc, page); |
| 1761 | unlock_page(page); |
| 1762 | return 0; |
| 1763 | } |
Theodore Ts'o | a42afc5 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1764 | commit_write = 1; |
| 1765 | } |
| 1766 | page_bufs = page_buffers(page); |
| 1767 | if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, |
| 1768 | ext4_bh_delay_or_unwritten)) { |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1769 | /* |
Theodore Ts'o | b1142e8 | 2010-10-28 17:33:57 -0400 | [diff] [blame] | 1770 | * We don't want to do block allocation, so redirty |
| 1771 | * the page and return. We may reach here when we do |
| 1772 | * a journal commit via journal_submit_inode_data_buffers. |
| 1773 | * We can also reach here via shrink_page_list |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1774 | */ |
Theodore Ts'o | a42afc5 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1775 | goto redirty_page; |
| 1776 | } |
| 1777 | if (commit_write) |
Aneesh Kumar K.V | ed9b3e3 | 2008-11-07 09:06:45 -0500 | [diff] [blame] | 1778 | /* now mark the buffer_heads as dirty and uptodate */ |
Aneesh Kumar K.V | b767e78 | 2009-06-04 08:06:06 -0400 | [diff] [blame] | 1779 | block_commit_write(page, 0, len); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1780 | |
Theodore Ts'o | cb20d51 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1781 | if (PageChecked(page) && ext4_should_journal_data(inode)) |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 1782 | /* |
| 1783 | * It's mmapped pagecache. Add buffers and journal it. There |
| 1784 | * doesn't seem much point in redirtying the page here. |
| 1785 | */ |
Wu Fengguang | 3f0ca30 | 2009-11-24 11:15:44 -0500 | [diff] [blame] | 1786 | return __ext4_journalled_writepage(page, len); |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 1787 | |
Theodore Ts'o | a42afc5 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 1788 | if (buffer_uninit(page_bufs)) { |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 1789 | ext4_set_bh_endio(page_bufs, inode); |
| 1790 | ret = block_write_full_page_endio(page, noalloc_get_block_write, |
| 1791 | wbc, ext4_end_io_buffer_write); |
| 1792 | } else |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 1793 | ret = block_write_full_page(page, noalloc_get_block_write, |
| 1794 | wbc); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1795 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1796 | return ret; |
| 1797 | } |
| 1798 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1799 | /* |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1800 | * This is called via ext4_da_writepages() to |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1801 | * calculate the total number of credits to reserve to fit |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1802 | * a single extent allocation into a single transaction, |
| 1803 | * ext4_da_writpeages() will loop calling this before |
| 1804 | * the block allocation. |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1805 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1806 | |
| 1807 | static int ext4_da_writepages_trans_blocks(struct inode *inode) |
| 1808 | { |
| 1809 | int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; |
| 1810 | |
| 1811 | /* |
| 1812 | * With non-extent format the journal credit needed to |
| 1813 | * insert nrblocks contiguous block is dependent on |
| 1814 | * number of contiguous block. So we will limit |
| 1815 | * number of contiguous block to a sane value |
| 1816 | */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 1817 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1818 | (max_blocks > EXT4_MAX_TRANS_DATA)) |
| 1819 | max_blocks = EXT4_MAX_TRANS_DATA; |
| 1820 | |
| 1821 | return ext4_chunk_trans_blocks(inode, max_blocks); |
| 1822 | } |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1823 | |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1824 | /* |
| 1825 | * write_cache_pages_da - walk the list of dirty pages of the given |
Theodore Ts'o | 8eb9e5c | 2011-02-26 14:07:31 -0500 | [diff] [blame] | 1826 | * address space and accumulate pages that need writing, and call |
Theodore Ts'o | 168fc02 | 2011-02-26 14:09:20 -0500 | [diff] [blame] | 1827 | * mpage_da_map_and_submit to map a single contiguous memory region |
| 1828 | * and then write them. |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1829 | */ |
| 1830 | static int write_cache_pages_da(struct address_space *mapping, |
| 1831 | struct writeback_control *wbc, |
Eric Sandeen | 72f84e6 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 1832 | struct mpage_da_data *mpd, |
| 1833 | pgoff_t *done_index) |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1834 | { |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1835 | struct buffer_head *bh, *head; |
Theodore Ts'o | 168fc02 | 2011-02-26 14:09:20 -0500 | [diff] [blame] | 1836 | struct inode *inode = mapping->host; |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1837 | struct pagevec pvec; |
| 1838 | unsigned int nr_pages; |
| 1839 | sector_t logical; |
| 1840 | pgoff_t index, end; |
| 1841 | long nr_to_write = wbc->nr_to_write; |
| 1842 | int i, tag, ret = 0; |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1843 | |
Theodore Ts'o | 168fc02 | 2011-02-26 14:09:20 -0500 | [diff] [blame] | 1844 | memset(mpd, 0, sizeof(struct mpage_da_data)); |
| 1845 | mpd->wbc = wbc; |
| 1846 | mpd->inode = inode; |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1847 | pagevec_init(&pvec, 0); |
| 1848 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 1849 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 1850 | |
Eric Sandeen | 5b41d92 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 1851 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 1852 | tag = PAGECACHE_TAG_TOWRITE; |
| 1853 | else |
| 1854 | tag = PAGECACHE_TAG_DIRTY; |
| 1855 | |
Eric Sandeen | 72f84e6 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 1856 | *done_index = index; |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1857 | while (index <= end) { |
Eric Sandeen | 5b41d92 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 1858 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1859 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); |
| 1860 | if (nr_pages == 0) |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1861 | return 0; |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1862 | |
| 1863 | for (i = 0; i < nr_pages; i++) { |
| 1864 | struct page *page = pvec.pages[i]; |
| 1865 | |
| 1866 | /* |
| 1867 | * At this point, the page may be truncated or |
| 1868 | * invalidated (changing page->mapping to NULL), or |
| 1869 | * even swizzled back from swapper_space to tmpfs file |
| 1870 | * mapping. However, page->index will not change |
| 1871 | * because we have a reference on the page. |
| 1872 | */ |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1873 | if (page->index > end) |
| 1874 | goto out; |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1875 | |
Eric Sandeen | 72f84e6 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 1876 | *done_index = page->index + 1; |
| 1877 | |
Theodore Ts'o | 78aaced | 2011-02-26 14:09:14 -0500 | [diff] [blame] | 1878 | /* |
| 1879 | * If we can't merge this page, and we have |
| 1880 | * accumulated an contiguous region, write it |
| 1881 | */ |
| 1882 | if ((mpd->next_page != page->index) && |
| 1883 | (mpd->next_page != mpd->first_page)) { |
| 1884 | mpage_da_map_and_submit(mpd); |
| 1885 | goto ret_extent_tail; |
| 1886 | } |
| 1887 | |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1888 | lock_page(page); |
| 1889 | |
| 1890 | /* |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1891 | * If the page is no longer dirty, or its |
| 1892 | * mapping no longer corresponds to inode we |
| 1893 | * are writing (which means it has been |
| 1894 | * truncated or invalidated), or the page is |
| 1895 | * already under writeback and we are not |
| 1896 | * doing a data integrity writeback, skip the page |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1897 | */ |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1898 | if (!PageDirty(page) || |
| 1899 | (PageWriteback(page) && |
| 1900 | (wbc->sync_mode == WB_SYNC_NONE)) || |
| 1901 | unlikely(page->mapping != mapping)) { |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1902 | unlock_page(page); |
| 1903 | continue; |
| 1904 | } |
| 1905 | |
Darrick J. Wong | 7cb1a53 | 2011-05-18 13:53:20 -0400 | [diff] [blame] | 1906 | wait_on_page_writeback(page); |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1907 | BUG_ON(PageWriteback(page)); |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1908 | |
Theodore Ts'o | 168fc02 | 2011-02-26 14:09:20 -0500 | [diff] [blame] | 1909 | if (mpd->next_page != page->index) |
Theodore Ts'o | 8eb9e5c | 2011-02-26 14:07:31 -0500 | [diff] [blame] | 1910 | mpd->first_page = page->index; |
Theodore Ts'o | 8eb9e5c | 2011-02-26 14:07:31 -0500 | [diff] [blame] | 1911 | mpd->next_page = page->index + 1; |
| 1912 | logical = (sector_t) page->index << |
| 1913 | (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 1914 | |
| 1915 | if (!page_has_buffers(page)) { |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1916 | mpage_add_bh_to_extent(mpd, logical, |
| 1917 | PAGE_CACHE_SIZE, |
Theodore Ts'o | 8eb9e5c | 2011-02-26 14:07:31 -0500 | [diff] [blame] | 1918 | (1 << BH_Dirty) | (1 << BH_Uptodate)); |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1919 | if (mpd->io_done) |
| 1920 | goto ret_extent_tail; |
Theodore Ts'o | 8eb9e5c | 2011-02-26 14:07:31 -0500 | [diff] [blame] | 1921 | } else { |
| 1922 | /* |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1923 | * Page with regular buffer heads, |
| 1924 | * just add all dirty ones |
Theodore Ts'o | 8eb9e5c | 2011-02-26 14:07:31 -0500 | [diff] [blame] | 1925 | */ |
| 1926 | head = page_buffers(page); |
| 1927 | bh = head; |
| 1928 | do { |
| 1929 | BUG_ON(buffer_locked(bh)); |
| 1930 | /* |
| 1931 | * We need to try to allocate |
| 1932 | * unmapped blocks in the same page. |
| 1933 | * Otherwise we won't make progress |
| 1934 | * with the page in ext4_writepage |
| 1935 | */ |
| 1936 | if (ext4_bh_delay_or_unwritten(NULL, bh)) { |
| 1937 | mpage_add_bh_to_extent(mpd, logical, |
| 1938 | bh->b_size, |
| 1939 | bh->b_state); |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1940 | if (mpd->io_done) |
| 1941 | goto ret_extent_tail; |
Theodore Ts'o | 8eb9e5c | 2011-02-26 14:07:31 -0500 | [diff] [blame] | 1942 | } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { |
| 1943 | /* |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1944 | * mapped dirty buffer. We need |
| 1945 | * to update the b_state |
| 1946 | * because we look at b_state |
| 1947 | * in mpage_da_map_blocks. We |
| 1948 | * don't update b_size because |
| 1949 | * if we find an unmapped |
| 1950 | * buffer_head later we need to |
| 1951 | * use the b_state flag of that |
| 1952 | * buffer_head. |
Theodore Ts'o | 8eb9e5c | 2011-02-26 14:07:31 -0500 | [diff] [blame] | 1953 | */ |
| 1954 | if (mpd->b_size == 0) |
| 1955 | mpd->b_state = bh->b_state & BH_FLAGS; |
| 1956 | } |
| 1957 | logical++; |
| 1958 | } while ((bh = bh->b_this_page) != head); |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | if (nr_to_write > 0) { |
| 1962 | nr_to_write--; |
| 1963 | if (nr_to_write == 0 && |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1964 | wbc->sync_mode == WB_SYNC_NONE) |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1965 | /* |
| 1966 | * We stop writing back only if we are |
| 1967 | * not doing integrity sync. In case of |
| 1968 | * integrity sync we have to keep going |
| 1969 | * because someone may be concurrently |
| 1970 | * dirtying pages, and we might have |
| 1971 | * synced a lot of newly appeared dirty |
| 1972 | * pages, but have not synced all of the |
| 1973 | * old dirty pages. |
| 1974 | */ |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1975 | goto out; |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1976 | } |
| 1977 | } |
| 1978 | pagevec_release(&pvec); |
| 1979 | cond_resched(); |
| 1980 | } |
Theodore Ts'o | 4f01b02 | 2011-02-26 14:07:37 -0500 | [diff] [blame] | 1981 | return 0; |
| 1982 | ret_extent_tail: |
| 1983 | ret = MPAGE_DA_EXTENT_TAIL; |
Theodore Ts'o | 8eb9e5c | 2011-02-26 14:07:31 -0500 | [diff] [blame] | 1984 | out: |
| 1985 | pagevec_release(&pvec); |
| 1986 | cond_resched(); |
Theodore Ts'o | 8e48dcf | 2010-05-16 18:00:00 -0400 | [diff] [blame] | 1987 | return ret; |
| 1988 | } |
| 1989 | |
| 1990 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1991 | static int ext4_da_writepages(struct address_space *mapping, |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1992 | struct writeback_control *wbc) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1993 | { |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 1994 | pgoff_t index; |
| 1995 | int range_whole = 0; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1996 | handle_t *handle = NULL; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1997 | struct mpage_da_data mpd; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 1998 | struct inode *inode = mapping->host; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1999 | int pages_written = 0; |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 2000 | unsigned int max_pages; |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2001 | int range_cyclic, cycled = 1, io_done = 0; |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 2002 | int needed_blocks, ret = 0; |
| 2003 | long desired_nr_to_write, nr_to_writebump = 0; |
Theodore Ts'o | de89de6 | 2009-08-31 17:00:59 -0400 | [diff] [blame] | 2004 | loff_t range_start = wbc->range_start; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 2005 | struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); |
Eric Sandeen | 72f84e6 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 2006 | pgoff_t done_index = 0; |
Eric Sandeen | 5b41d92 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 2007 | pgoff_t end; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2008 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 2009 | trace_ext4_da_writepages(inode, wbc); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 2010 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2011 | /* |
| 2012 | * No pages to write? This is mainly a kludge to avoid starting |
| 2013 | * a transaction for special inodes like journal inode on last iput() |
| 2014 | * because that could violate lock ordering on umount |
| 2015 | */ |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2016 | if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2017 | return 0; |
Theodore Ts'o | 2a21e37 | 2008-11-05 09:22:24 -0500 | [diff] [blame] | 2018 | |
| 2019 | /* |
| 2020 | * If the filesystem has aborted, it is read-only, so return |
| 2021 | * right away instead of dumping stack traces later on that |
| 2022 | * will obscure the real source of the problem. We test |
Theodore Ts'o | 4ab2f15 | 2009-06-13 10:09:36 -0400 | [diff] [blame] | 2023 | * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because |
Theodore Ts'o | 2a21e37 | 2008-11-05 09:22:24 -0500 | [diff] [blame] | 2024 | * the latter could be true if the filesystem is mounted |
| 2025 | * read-only, and in that case, ext4_da_writepages should |
| 2026 | * *never* be called, so if that ever happens, we would want |
| 2027 | * the stack trace. |
| 2028 | */ |
Theodore Ts'o | 4ab2f15 | 2009-06-13 10:09:36 -0400 | [diff] [blame] | 2029 | if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) |
Theodore Ts'o | 2a21e37 | 2008-11-05 09:22:24 -0500 | [diff] [blame] | 2030 | return -EROFS; |
| 2031 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2032 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 2033 | range_whole = 1; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2034 | |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2035 | range_cyclic = wbc->range_cyclic; |
| 2036 | if (wbc->range_cyclic) { |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2037 | index = mapping->writeback_index; |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2038 | if (index) |
| 2039 | cycled = 0; |
| 2040 | wbc->range_start = index << PAGE_CACHE_SHIFT; |
| 2041 | wbc->range_end = LLONG_MAX; |
| 2042 | wbc->range_cyclic = 0; |
Eric Sandeen | 5b41d92 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 2043 | end = -1; |
| 2044 | } else { |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2045 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
Eric Sandeen | 5b41d92 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 2046 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 2047 | } |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2048 | |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 2049 | /* |
| 2050 | * This works around two forms of stupidity. The first is in |
| 2051 | * the writeback code, which caps the maximum number of pages |
| 2052 | * written to be 1024 pages. This is wrong on multiple |
| 2053 | * levels; different architectues have a different page size, |
| 2054 | * which changes the maximum amount of data which gets |
| 2055 | * written. Secondly, 4 megabytes is way too small. XFS |
| 2056 | * forces this value to be 16 megabytes by multiplying |
| 2057 | * nr_to_write parameter by four, and then relies on its |
| 2058 | * allocator to allocate larger extents to make them |
| 2059 | * contiguous. Unfortunately this brings us to the second |
| 2060 | * stupidity, which is that ext4's mballoc code only allocates |
| 2061 | * at most 2048 blocks. So we force contiguous writes up to |
| 2062 | * the number of dirty blocks in the inode, or |
| 2063 | * sbi->max_writeback_mb_bump whichever is smaller. |
| 2064 | */ |
| 2065 | max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); |
Eric Sandeen | b443e73 | 2010-10-27 21:30:03 -0400 | [diff] [blame] | 2066 | if (!range_cyclic && range_whole) { |
| 2067 | if (wbc->nr_to_write == LONG_MAX) |
| 2068 | desired_nr_to_write = wbc->nr_to_write; |
| 2069 | else |
| 2070 | desired_nr_to_write = wbc->nr_to_write * 8; |
| 2071 | } else |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 2072 | desired_nr_to_write = ext4_num_dirty_pages(inode, index, |
| 2073 | max_pages); |
| 2074 | if (desired_nr_to_write > max_pages) |
| 2075 | desired_nr_to_write = max_pages; |
| 2076 | |
| 2077 | if (wbc->nr_to_write < desired_nr_to_write) { |
| 2078 | nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; |
| 2079 | wbc->nr_to_write = desired_nr_to_write; |
| 2080 | } |
| 2081 | |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2082 | retry: |
Eric Sandeen | 5b41d92 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 2083 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 2084 | tag_pages_for_writeback(mapping, index, end); |
| 2085 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2086 | while (!ret && wbc->nr_to_write > 0) { |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2087 | |
| 2088 | /* |
| 2089 | * we insert one extent at a time. So we need |
| 2090 | * credit needed for single extent allocation. |
| 2091 | * journalled mode is currently not supported |
| 2092 | * by delalloc |
| 2093 | */ |
| 2094 | BUG_ON(ext4_should_journal_data(inode)); |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2095 | needed_blocks = ext4_da_writepages_trans_blocks(inode); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2096 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2097 | /* start a new transaction*/ |
| 2098 | handle = ext4_journal_start(inode, needed_blocks); |
| 2099 | if (IS_ERR(handle)) { |
| 2100 | ret = PTR_ERR(handle); |
Theodore Ts'o | 1693918 | 2009-09-26 17:43:59 -0400 | [diff] [blame] | 2101 | ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " |
Curt Wohlgemuth | fbe845d | 2010-05-16 13:00:00 -0400 | [diff] [blame] | 2102 | "%ld pages, ino %lu; err %d", __func__, |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2103 | wbc->nr_to_write, inode->i_ino, ret); |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2104 | goto out_writepages; |
| 2105 | } |
Theodore Ts'o | f63e600 | 2009-02-23 16:42:39 -0500 | [diff] [blame] | 2106 | |
| 2107 | /* |
Theodore Ts'o | 8eb9e5c | 2011-02-26 14:07:31 -0500 | [diff] [blame] | 2108 | * Now call write_cache_pages_da() to find the next |
Theodore Ts'o | f63e600 | 2009-02-23 16:42:39 -0500 | [diff] [blame] | 2109 | * contiguous region of logical blocks that need |
Theodore Ts'o | 8eb9e5c | 2011-02-26 14:07:31 -0500 | [diff] [blame] | 2110 | * blocks to be allocated by ext4 and submit them. |
Theodore Ts'o | f63e600 | 2009-02-23 16:42:39 -0500 | [diff] [blame] | 2111 | */ |
Eric Sandeen | 72f84e6 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 2112 | ret = write_cache_pages_da(mapping, wbc, &mpd, &done_index); |
Theodore Ts'o | f63e600 | 2009-02-23 16:42:39 -0500 | [diff] [blame] | 2113 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2114 | * If we have a contiguous extent of pages and we |
Theodore Ts'o | f63e600 | 2009-02-23 16:42:39 -0500 | [diff] [blame] | 2115 | * haven't done the I/O yet, map the blocks and submit |
| 2116 | * them for I/O. |
| 2117 | */ |
| 2118 | if (!mpd.io_done && mpd.next_page != mpd.first_page) { |
Theodore Ts'o | 5a87b7a | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 2119 | mpage_da_map_and_submit(&mpd); |
Theodore Ts'o | f63e600 | 2009-02-23 16:42:39 -0500 | [diff] [blame] | 2120 | ret = MPAGE_DA_EXTENT_TAIL; |
| 2121 | } |
Theodore Ts'o | b3a3ca8 | 2009-08-31 23:13:11 -0400 | [diff] [blame] | 2122 | trace_ext4_da_write_pages(inode, &mpd); |
Theodore Ts'o | f63e600 | 2009-02-23 16:42:39 -0500 | [diff] [blame] | 2123 | wbc->nr_to_write -= mpd.pages_written; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2124 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2125 | ext4_journal_stop(handle); |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2126 | |
Eric Sandeen | 8f64b32 | 2009-02-26 00:57:35 -0500 | [diff] [blame] | 2127 | if ((mpd.retval == -ENOSPC) && sbi->s_journal) { |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2128 | /* commit the transaction which would |
| 2129 | * free blocks released in the transaction |
| 2130 | * and try again |
| 2131 | */ |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2132 | jbd2_journal_force_commit_nested(sbi->s_journal); |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2133 | ret = 0; |
| 2134 | } else if (ret == MPAGE_DA_EXTENT_TAIL) { |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2135 | /* |
| 2136 | * got one extent now try with |
| 2137 | * rest of the pages |
| 2138 | */ |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2139 | pages_written += mpd.pages_written; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2140 | ret = 0; |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2141 | io_done = 1; |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2142 | } else if (wbc->nr_to_write) |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2143 | /* |
| 2144 | * There is no more writeout needed |
| 2145 | * or we requested for a noblocking writeout |
| 2146 | * and we found the device congested |
| 2147 | */ |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2148 | break; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2149 | } |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2150 | if (!io_done && !cycled) { |
| 2151 | cycled = 1; |
| 2152 | index = 0; |
| 2153 | wbc->range_start = index << PAGE_CACHE_SHIFT; |
| 2154 | wbc->range_end = mapping->writeback_index - 1; |
| 2155 | goto retry; |
| 2156 | } |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2157 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2158 | /* Update index */ |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2159 | wbc->range_cyclic = range_cyclic; |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2160 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 2161 | /* |
| 2162 | * set the writeback_index so that range_cyclic |
| 2163 | * mode will write it back later |
| 2164 | */ |
Eric Sandeen | 72f84e6 | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 2165 | mapping->writeback_index = done_index; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2166 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2167 | out_writepages: |
Richard Kennedy | 2faf2e1 | 2009-12-25 15:46:07 -0500 | [diff] [blame] | 2168 | wbc->nr_to_write -= nr_to_writebump; |
Theodore Ts'o | de89de6 | 2009-08-31 17:00:59 -0400 | [diff] [blame] | 2169 | wbc->range_start = range_start; |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 2170 | trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2171 | return ret; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2172 | } |
| 2173 | |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2174 | #define FALL_BACK_TO_NONDELALLOC 1 |
| 2175 | static int ext4_nonda_switch(struct super_block *sb) |
| 2176 | { |
| 2177 | s64 free_blocks, dirty_blocks; |
| 2178 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2179 | |
| 2180 | /* |
| 2181 | * switch to non delalloc mode if we are running low |
| 2182 | * on free block. The free block accounting via percpu |
Eric Dumazet | 179f7eb | 2009-01-06 14:41:04 -0800 | [diff] [blame] | 2183 | * counters can get slightly wrong with percpu_counter_batch getting |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2184 | * accumulated on each CPU without updating global counters |
| 2185 | * Delalloc need an accurate free block accounting. So switch |
| 2186 | * to non delalloc when we are near to error range. |
| 2187 | */ |
| 2188 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); |
| 2189 | dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); |
| 2190 | if (2 * free_blocks < 3 * dirty_blocks || |
| 2191 | free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { |
| 2192 | /* |
Eric Sandeen | c8afb44 | 2009-12-23 07:58:12 -0500 | [diff] [blame] | 2193 | * free block count is less than 150% of dirty blocks |
| 2194 | * or free blocks is less than watermark |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2195 | */ |
| 2196 | return 1; |
| 2197 | } |
Eric Sandeen | c8afb44 | 2009-12-23 07:58:12 -0500 | [diff] [blame] | 2198 | /* |
| 2199 | * Even if we don't switch but are nearing capacity, |
| 2200 | * start pushing delalloc when 1/2 of free blocks are dirty. |
| 2201 | */ |
| 2202 | if (free_blocks < 2 * dirty_blocks) |
| 2203 | writeback_inodes_sb_if_idle(sb); |
| 2204 | |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2205 | return 0; |
| 2206 | } |
| 2207 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2208 | static int ext4_da_write_begin(struct file *file, struct address_space *mapping, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 2209 | loff_t pos, unsigned len, unsigned flags, |
| 2210 | struct page **pagep, void **fsdata) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2211 | { |
Eric Sandeen | 72b8ab9 | 2010-05-16 11:00:00 -0400 | [diff] [blame] | 2212 | int ret, retries = 0; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2213 | struct page *page; |
| 2214 | pgoff_t index; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2215 | struct inode *inode = mapping->host; |
| 2216 | handle_t *handle; |
| 2217 | |
| 2218 | index = pos >> PAGE_CACHE_SHIFT; |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2219 | |
| 2220 | if (ext4_nonda_switch(inode->i_sb)) { |
| 2221 | *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; |
| 2222 | return ext4_write_begin(file, mapping, pos, |
| 2223 | len, flags, pagep, fsdata); |
| 2224 | } |
| 2225 | *fsdata = (void *)0; |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 2226 | trace_ext4_da_write_begin(inode, pos, len, flags); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2227 | retry: |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2228 | /* |
| 2229 | * With delayed allocation, we don't log the i_disksize update |
| 2230 | * if there is delayed block allocation. But we still need |
| 2231 | * to journalling the i_disksize update if writes to the end |
| 2232 | * of file which has an already mapped buffer. |
| 2233 | */ |
| 2234 | handle = ext4_journal_start(inode, 1); |
| 2235 | if (IS_ERR(handle)) { |
| 2236 | ret = PTR_ERR(handle); |
| 2237 | goto out; |
| 2238 | } |
Jan Kara | ebd3610 | 2009-02-22 21:09:59 -0500 | [diff] [blame] | 2239 | /* We cannot recurse into the filesystem as the transaction is already |
| 2240 | * started */ |
| 2241 | flags |= AOP_FLAG_NOFS; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2242 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 2243 | page = grab_cache_page_write_begin(mapping, index, flags); |
Eric Sandeen | d5a0d4f | 2008-08-02 18:51:06 -0400 | [diff] [blame] | 2244 | if (!page) { |
| 2245 | ext4_journal_stop(handle); |
| 2246 | ret = -ENOMEM; |
| 2247 | goto out; |
| 2248 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2249 | *pagep = page; |
| 2250 | |
Christoph Hellwig | 6e1db88 | 2010-06-04 11:29:57 +0200 | [diff] [blame] | 2251 | ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2252 | if (ret < 0) { |
| 2253 | unlock_page(page); |
| 2254 | ext4_journal_stop(handle); |
| 2255 | page_cache_release(page); |
Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 2256 | /* |
| 2257 | * block_write_begin may have instantiated a few blocks |
| 2258 | * outside i_size. Trim these off again. Don't need |
| 2259 | * i_size_read because we hold i_mutex. |
| 2260 | */ |
| 2261 | if (pos + len > inode->i_size) |
Jan Kara | b9a4207 | 2009-12-08 21:24:33 -0500 | [diff] [blame] | 2262 | ext4_truncate_failed_write(inode); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2263 | } |
| 2264 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2265 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
| 2266 | goto retry; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2267 | out: |
| 2268 | return ret; |
| 2269 | } |
| 2270 | |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2271 | /* |
| 2272 | * Check if we should update i_disksize |
| 2273 | * when write to the end of file but not require block allocation |
| 2274 | */ |
| 2275 | static int ext4_da_should_update_i_disksize(struct page *page, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 2276 | unsigned long offset) |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2277 | { |
| 2278 | struct buffer_head *bh; |
| 2279 | struct inode *inode = page->mapping->host; |
| 2280 | unsigned int idx; |
| 2281 | int i; |
| 2282 | |
| 2283 | bh = page_buffers(page); |
| 2284 | idx = offset >> inode->i_blkbits; |
| 2285 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2286 | for (i = 0; i < idx; i++) |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2287 | bh = bh->b_this_page; |
| 2288 | |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2289 | if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2290 | return 0; |
| 2291 | return 1; |
| 2292 | } |
| 2293 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2294 | static int ext4_da_write_end(struct file *file, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 2295 | struct address_space *mapping, |
| 2296 | loff_t pos, unsigned len, unsigned copied, |
| 2297 | struct page *page, void *fsdata) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2298 | { |
| 2299 | struct inode *inode = mapping->host; |
| 2300 | int ret = 0, ret2; |
| 2301 | handle_t *handle = ext4_journal_current_handle(); |
| 2302 | loff_t new_i_size; |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2303 | unsigned long start, end; |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2304 | int write_mode = (int)(unsigned long)fsdata; |
| 2305 | |
| 2306 | if (write_mode == FALL_BACK_TO_NONDELALLOC) { |
| 2307 | if (ext4_should_order_data(inode)) { |
| 2308 | return ext4_ordered_write_end(file, mapping, pos, |
| 2309 | len, copied, page, fsdata); |
| 2310 | } else if (ext4_should_writeback_data(inode)) { |
| 2311 | return ext4_writeback_write_end(file, mapping, pos, |
| 2312 | len, copied, page, fsdata); |
| 2313 | } else { |
| 2314 | BUG(); |
| 2315 | } |
| 2316 | } |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2317 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 2318 | trace_ext4_da_write_end(inode, pos, len, copied); |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2319 | start = pos & (PAGE_CACHE_SIZE - 1); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2320 | end = start + copied - 1; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2321 | |
| 2322 | /* |
| 2323 | * generic_write_end() will run mark_inode_dirty() if i_size |
| 2324 | * changes. So let's piggyback the i_disksize mark_inode_dirty |
| 2325 | * into that. |
| 2326 | */ |
| 2327 | |
| 2328 | new_i_size = pos + copied; |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2329 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
| 2330 | if (ext4_da_should_update_i_disksize(page, end)) { |
| 2331 | down_write(&EXT4_I(inode)->i_data_sem); |
| 2332 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
| 2333 | /* |
| 2334 | * Updating i_disksize when extending file |
| 2335 | * without needing block allocation |
| 2336 | */ |
| 2337 | if (ext4_should_order_data(inode)) |
| 2338 | ret = ext4_jbd2_file_inode(handle, |
| 2339 | inode); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2340 | |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2341 | EXT4_I(inode)->i_disksize = new_i_size; |
| 2342 | } |
| 2343 | up_write(&EXT4_I(inode)->i_data_sem); |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 2344 | /* We need to mark inode dirty even if |
| 2345 | * new_i_size is less that inode->i_size |
| 2346 | * bu greater than i_disksize.(hint delalloc) |
| 2347 | */ |
| 2348 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2349 | } |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2350 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2351 | ret2 = generic_write_end(file, mapping, pos, len, copied, |
| 2352 | page, fsdata); |
| 2353 | copied = ret2; |
| 2354 | if (ret2 < 0) |
| 2355 | ret = ret2; |
| 2356 | ret2 = ext4_journal_stop(handle); |
| 2357 | if (!ret) |
| 2358 | ret = ret2; |
| 2359 | |
| 2360 | return ret ? ret : copied; |
| 2361 | } |
| 2362 | |
| 2363 | static void ext4_da_invalidatepage(struct page *page, unsigned long offset) |
| 2364 | { |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2365 | /* |
| 2366 | * Drop reserved blocks |
| 2367 | */ |
| 2368 | BUG_ON(!PageLocked(page)); |
| 2369 | if (!page_has_buffers(page)) |
| 2370 | goto out; |
| 2371 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2372 | ext4_da_page_release_reservation(page, offset); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2373 | |
| 2374 | out: |
| 2375 | ext4_invalidatepage(page, offset); |
| 2376 | |
| 2377 | return; |
| 2378 | } |
| 2379 | |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 2380 | /* |
| 2381 | * Force all delayed allocation blocks to be allocated for a given inode. |
| 2382 | */ |
| 2383 | int ext4_alloc_da_blocks(struct inode *inode) |
| 2384 | { |
Theodore Ts'o | fb40ba0 | 2009-09-16 19:30:40 -0400 | [diff] [blame] | 2385 | trace_ext4_alloc_da_blocks(inode); |
| 2386 | |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 2387 | if (!EXT4_I(inode)->i_reserved_data_blocks && |
| 2388 | !EXT4_I(inode)->i_reserved_meta_blocks) |
| 2389 | return 0; |
| 2390 | |
| 2391 | /* |
| 2392 | * We do something simple for now. The filemap_flush() will |
| 2393 | * also start triggering a write of the data blocks, which is |
| 2394 | * not strictly speaking necessary (and for users of |
| 2395 | * laptop_mode, not even desirable). However, to do otherwise |
| 2396 | * would require replicating code paths in: |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 2397 | * |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 2398 | * ext4_da_writepages() -> |
| 2399 | * write_cache_pages() ---> (via passed in callback function) |
| 2400 | * __mpage_da_writepage() --> |
| 2401 | * mpage_add_bh_to_extent() |
| 2402 | * mpage_da_map_blocks() |
| 2403 | * |
| 2404 | * The problem is that write_cache_pages(), located in |
| 2405 | * mm/page-writeback.c, marks pages clean in preparation for |
| 2406 | * doing I/O, which is not desirable if we're not planning on |
| 2407 | * doing I/O at all. |
| 2408 | * |
| 2409 | * We could call write_cache_pages(), and then redirty all of |
Wu Fengguang | 380cf09 | 2010-11-11 19:23:29 +0800 | [diff] [blame] | 2410 | * the pages by calling redirty_page_for_writepage() but that |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 2411 | * would be ugly in the extreme. So instead we would need to |
| 2412 | * replicate parts of the code in the above functions, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2413 | * simplifying them because we wouldn't actually intend to |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 2414 | * write out the pages, but rather only collect contiguous |
| 2415 | * logical block extents, call the multi-block allocator, and |
| 2416 | * then update the buffer heads with the block allocations. |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 2417 | * |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 2418 | * For now, though, we'll cheat by calling filemap_flush(), |
| 2419 | * which will map the blocks, and start the I/O, but not |
| 2420 | * actually wait for the I/O to complete. |
| 2421 | */ |
| 2422 | return filemap_flush(inode->i_mapping); |
| 2423 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2424 | |
| 2425 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2426 | * bmap() is special. It gets used by applications such as lilo and by |
| 2427 | * the swapper to find the on-disk block of a specific piece of data. |
| 2428 | * |
| 2429 | * Naturally, this is dangerous if the block concerned is still in the |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2430 | * journal. If somebody makes a swapfile on an ext4 data-journaling |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2431 | * filesystem and enables swap, then they may get a nasty shock when the |
| 2432 | * data getting swapped to that swapfile suddenly gets overwritten by |
| 2433 | * the original zero's written out previously to the journal and |
| 2434 | * awaiting writeback in the kernel's buffer cache. |
| 2435 | * |
| 2436 | * So, if we see any bmap calls here on a modified, data-journaled file, |
| 2437 | * take extra steps to flush any blocks which might be in the cache. |
| 2438 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2439 | static sector_t ext4_bmap(struct address_space *mapping, sector_t block) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2440 | { |
| 2441 | struct inode *inode = mapping->host; |
| 2442 | journal_t *journal; |
| 2443 | int err; |
| 2444 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2445 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && |
| 2446 | test_opt(inode->i_sb, DELALLOC)) { |
| 2447 | /* |
| 2448 | * With delalloc we want to sync the file |
| 2449 | * so that we can make sure we allocate |
| 2450 | * blocks for file |
| 2451 | */ |
| 2452 | filemap_write_and_wait(mapping); |
| 2453 | } |
| 2454 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 2455 | if (EXT4_JOURNAL(inode) && |
| 2456 | ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2457 | /* |
| 2458 | * This is a REALLY heavyweight approach, but the use of |
| 2459 | * bmap on dirty files is expected to be extremely rare: |
| 2460 | * only if we run lilo or swapon on a freshly made file |
| 2461 | * do we expect this to happen. |
| 2462 | * |
| 2463 | * (bmap requires CAP_SYS_RAWIO so this does not |
| 2464 | * represent an unprivileged user DOS attack --- we'd be |
| 2465 | * in trouble if mortal users could trigger this path at |
| 2466 | * will.) |
| 2467 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2468 | * NB. EXT4_STATE_JDATA is not set on files other than |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2469 | * regular files. If somebody wants to bmap a directory |
| 2470 | * or symlink and gets confused because the buffer |
| 2471 | * hasn't yet been flushed to disk, they deserve |
| 2472 | * everything they get. |
| 2473 | */ |
| 2474 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 2475 | ext4_clear_inode_state(inode, EXT4_STATE_JDATA); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2476 | journal = EXT4_JOURNAL(inode); |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 2477 | jbd2_journal_lock_updates(journal); |
| 2478 | err = jbd2_journal_flush(journal); |
| 2479 | jbd2_journal_unlock_updates(journal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2480 | |
| 2481 | if (err) |
| 2482 | return 0; |
| 2483 | } |
| 2484 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2485 | return generic_block_bmap(mapping, block, ext4_get_block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2486 | } |
| 2487 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2488 | static int ext4_readpage(struct file *file, struct page *page) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2489 | { |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 2490 | trace_ext4_readpage(page); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2491 | return mpage_readpage(page, ext4_get_block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2495 | ext4_readpages(struct file *file, struct address_space *mapping, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2496 | struct list_head *pages, unsigned nr_pages) |
| 2497 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2498 | return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2499 | } |
| 2500 | |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 2501 | static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset) |
| 2502 | { |
| 2503 | struct buffer_head *head, *bh; |
| 2504 | unsigned int curr_off = 0; |
| 2505 | |
| 2506 | if (!page_has_buffers(page)) |
| 2507 | return; |
| 2508 | head = bh = page_buffers(page); |
| 2509 | do { |
| 2510 | if (offset <= curr_off && test_clear_buffer_uninit(bh) |
| 2511 | && bh->b_private) { |
| 2512 | ext4_free_io_end(bh->b_private); |
| 2513 | bh->b_private = NULL; |
| 2514 | bh->b_end_io = NULL; |
| 2515 | } |
| 2516 | curr_off = curr_off + bh->b_size; |
| 2517 | bh = bh->b_this_page; |
| 2518 | } while (bh != head); |
| 2519 | } |
| 2520 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2521 | static void ext4_invalidatepage(struct page *page, unsigned long offset) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2522 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2523 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2524 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 2525 | trace_ext4_invalidatepage(page, offset); |
| 2526 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2527 | /* |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 2528 | * free any io_end structure allocated for buffers to be discarded |
| 2529 | */ |
| 2530 | if (ext4_should_dioread_nolock(page->mapping->host)) |
| 2531 | ext4_invalidatepage_free_endio(page, offset); |
| 2532 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2533 | * If it's a full truncate we just forget about the pending dirtying |
| 2534 | */ |
| 2535 | if (offset == 0) |
| 2536 | ClearPageChecked(page); |
| 2537 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2538 | if (journal) |
| 2539 | jbd2_journal_invalidatepage(journal, page, offset); |
| 2540 | else |
| 2541 | block_invalidatepage(page, offset); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2542 | } |
| 2543 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2544 | static int ext4_releasepage(struct page *page, gfp_t wait) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2545 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2546 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2547 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 2548 | trace_ext4_releasepage(page); |
| 2549 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2550 | WARN_ON(PageChecked(page)); |
| 2551 | if (!page_has_buffers(page)) |
| 2552 | return 0; |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2553 | if (journal) |
| 2554 | return jbd2_journal_try_to_free_buffers(journal, page, wait); |
| 2555 | else |
| 2556 | return try_to_free_buffers(page); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | /* |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 2560 | * ext4_get_block used when preparing for a DIO write or buffer write. |
| 2561 | * We allocate an uinitialized extent if blocks haven't been allocated. |
| 2562 | * The extent will be converted to initialized after the IO is complete. |
| 2563 | */ |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 2564 | static int ext4_get_block_write(struct inode *inode, sector_t iblock, |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2565 | struct buffer_head *bh_result, int create) |
| 2566 | { |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 2567 | ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2568 | inode->i_ino, create); |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 2569 | return _ext4_get_block(inode, iblock, bh_result, |
| 2570 | EXT4_GET_BLOCKS_IO_CREATE_EXT); |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2571 | } |
| 2572 | |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2573 | static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, |
Christoph Hellwig | 552ef802 | 2010-07-27 11:56:06 -0400 | [diff] [blame] | 2574 | ssize_t size, void *private, int ret, |
| 2575 | bool is_async) |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2576 | { |
| 2577 | ext4_io_end_t *io_end = iocb->private; |
| 2578 | struct workqueue_struct *wq; |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 2579 | unsigned long flags; |
| 2580 | struct ext4_inode_info *ei; |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2581 | |
Mingming | 4b70df1 | 2009-11-03 14:44:54 -0500 | [diff] [blame] | 2582 | /* if not async direct IO or dio with 0 bytes write, just return */ |
| 2583 | if (!io_end || !size) |
Christoph Hellwig | 552ef802 | 2010-07-27 11:56:06 -0400 | [diff] [blame] | 2584 | goto out; |
Mingming | 4b70df1 | 2009-11-03 14:44:54 -0500 | [diff] [blame] | 2585 | |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2586 | ext_debug("ext4_end_io_dio(): io_end 0x%p" |
| 2587 | "for inode %lu, iocb 0x%p, offset %llu, size %llu\n", |
| 2588 | iocb->private, io_end->inode->i_ino, iocb, offset, |
| 2589 | size); |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2590 | |
| 2591 | /* if not aio dio with unwritten extents, just free io and return */ |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 2592 | if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2593 | ext4_free_io_end(io_end); |
| 2594 | iocb->private = NULL; |
jiayingz@google.com (Jiaying Zhang) | 5b3ff23 | 2010-07-27 11:56:06 -0400 | [diff] [blame] | 2595 | out: |
| 2596 | if (is_async) |
| 2597 | aio_complete(iocb, ret, 0); |
| 2598 | return; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2599 | } |
| 2600 | |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2601 | io_end->offset = offset; |
| 2602 | io_end->size = size; |
jiayingz@google.com (Jiaying Zhang) | 5b3ff23 | 2010-07-27 11:56:06 -0400 | [diff] [blame] | 2603 | if (is_async) { |
| 2604 | io_end->iocb = iocb; |
| 2605 | io_end->result = ret; |
| 2606 | } |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2607 | wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq; |
| 2608 | |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2609 | /* Add the io_end to per-inode completed aio dio list*/ |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 2610 | ei = EXT4_I(io_end->inode); |
| 2611 | spin_lock_irqsave(&ei->i_completed_io_lock, flags); |
| 2612 | list_add_tail(&io_end->list, &ei->i_completed_io_list); |
| 2613 | spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); |
Eric Sandeen | c999af2 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 2614 | |
| 2615 | /* queue the work to convert unwritten extents to written */ |
| 2616 | queue_work(wq, &io_end->work); |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2617 | iocb->private = NULL; |
| 2618 | } |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 2619 | |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 2620 | static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate) |
| 2621 | { |
| 2622 | ext4_io_end_t *io_end = bh->b_private; |
| 2623 | struct workqueue_struct *wq; |
| 2624 | struct inode *inode; |
| 2625 | unsigned long flags; |
| 2626 | |
| 2627 | if (!test_clear_buffer_uninit(bh) || !io_end) |
| 2628 | goto out; |
| 2629 | |
| 2630 | if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) { |
| 2631 | printk("sb umounted, discard end_io request for inode %lu\n", |
| 2632 | io_end->inode->i_ino); |
| 2633 | ext4_free_io_end(io_end); |
| 2634 | goto out; |
| 2635 | } |
| 2636 | |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 2637 | io_end->flag = EXT4_IO_END_UNWRITTEN; |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 2638 | inode = io_end->inode; |
| 2639 | |
| 2640 | /* Add the io_end to per-inode completed io list*/ |
| 2641 | spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags); |
| 2642 | list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list); |
| 2643 | spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags); |
| 2644 | |
| 2645 | wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq; |
| 2646 | /* queue the work to convert unwritten extents to written */ |
| 2647 | queue_work(wq, &io_end->work); |
| 2648 | out: |
| 2649 | bh->b_private = NULL; |
| 2650 | bh->b_end_io = NULL; |
| 2651 | clear_buffer_uninit(bh); |
| 2652 | end_buffer_async_write(bh, uptodate); |
| 2653 | } |
| 2654 | |
| 2655 | static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode) |
| 2656 | { |
| 2657 | ext4_io_end_t *io_end; |
| 2658 | struct page *page = bh->b_page; |
| 2659 | loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT; |
| 2660 | size_t size = bh->b_size; |
| 2661 | |
| 2662 | retry: |
| 2663 | io_end = ext4_init_io_end(inode, GFP_ATOMIC); |
| 2664 | if (!io_end) { |
Andrew Morton | 6db26ff | 2011-01-12 16:59:13 -0800 | [diff] [blame] | 2665 | pr_warn_ratelimited("%s: allocation fail\n", __func__); |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 2666 | schedule(); |
| 2667 | goto retry; |
| 2668 | } |
| 2669 | io_end->offset = offset; |
| 2670 | io_end->size = size; |
| 2671 | /* |
| 2672 | * We need to hold a reference to the page to make sure it |
| 2673 | * doesn't get evicted before ext4_end_io_work() has a chance |
| 2674 | * to convert the extent from written to unwritten. |
| 2675 | */ |
| 2676 | io_end->page = page; |
| 2677 | get_page(io_end->page); |
| 2678 | |
| 2679 | bh->b_private = io_end; |
| 2680 | bh->b_end_io = ext4_end_io_buffer_write; |
| 2681 | return 0; |
| 2682 | } |
| 2683 | |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2684 | /* |
| 2685 | * For ext4 extent files, ext4 will do direct-io write to holes, |
| 2686 | * preallocated extents, and those write extend the file, no need to |
| 2687 | * fall back to buffered IO. |
| 2688 | * |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 2689 | * For holes, we fallocate those blocks, mark them as uninitialized |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2690 | * If those blocks were preallocated, we mark sure they are splited, but |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 2691 | * still keep the range to write as uninitialized. |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2692 | * |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2693 | * The unwrritten extents will be converted to written when DIO is completed. |
| 2694 | * For async direct IO, since the IO may still pending when return, we |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2695 | * set up an end_io call back function, which will do the conversion |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2696 | * when async direct IO completed. |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2697 | * |
| 2698 | * If the O_DIRECT write will extend the file then add this inode to the |
| 2699 | * orphan list. So recovery will truncate it back to the original size |
| 2700 | * if the machine crashes during the write. |
| 2701 | * |
| 2702 | */ |
| 2703 | static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, |
| 2704 | const struct iovec *iov, loff_t offset, |
| 2705 | unsigned long nr_segs) |
| 2706 | { |
| 2707 | struct file *file = iocb->ki_filp; |
| 2708 | struct inode *inode = file->f_mapping->host; |
| 2709 | ssize_t ret; |
| 2710 | size_t count = iov_length(iov, nr_segs); |
| 2711 | |
| 2712 | loff_t final_size = offset + count; |
| 2713 | if (rw == WRITE && final_size <= inode->i_size) { |
| 2714 | /* |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2715 | * We could direct write to holes and fallocate. |
| 2716 | * |
| 2717 | * Allocated blocks to fill the hole are marked as uninitialized |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2718 | * to prevent parallel buffered read to expose the stale data |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2719 | * before DIO complete the data IO. |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2720 | * |
| 2721 | * As to previously fallocated extents, ext4 get_block |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2722 | * will just simply mark the buffer mapped but still |
| 2723 | * keep the extents uninitialized. |
| 2724 | * |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2725 | * for non AIO case, we will convert those unwritten extents |
| 2726 | * to written after return back from blockdev_direct_IO. |
| 2727 | * |
| 2728 | * for async DIO, the conversion needs to be defered when |
| 2729 | * the IO is completed. The ext4 end_io callback function |
| 2730 | * will be called to take care of the conversion work. |
| 2731 | * Here for async case, we allocate an io_end structure to |
| 2732 | * hook to the iocb. |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2733 | */ |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2734 | iocb->private = NULL; |
| 2735 | EXT4_I(inode)->cur_aio_dio = NULL; |
| 2736 | if (!is_sync_kiocb(iocb)) { |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 2737 | iocb->private = ext4_init_io_end(inode, GFP_NOFS); |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2738 | if (!iocb->private) |
| 2739 | return -ENOMEM; |
| 2740 | /* |
| 2741 | * we save the io structure for current async |
Eric Sandeen | 79e8303 | 2010-07-27 11:56:07 -0400 | [diff] [blame] | 2742 | * direct IO, so that later ext4_map_blocks() |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2743 | * could flag the io structure whether there |
| 2744 | * is a unwritten extents needs to be converted |
| 2745 | * when IO is completed. |
| 2746 | */ |
| 2747 | EXT4_I(inode)->cur_aio_dio = iocb->private; |
| 2748 | } |
| 2749 | |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2750 | ret = blockdev_direct_IO(rw, iocb, inode, |
| 2751 | inode->i_sb->s_bdev, iov, |
| 2752 | offset, nr_segs, |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 2753 | ext4_get_block_write, |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2754 | ext4_end_io_dio); |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2755 | if (iocb->private) |
| 2756 | EXT4_I(inode)->cur_aio_dio = NULL; |
| 2757 | /* |
| 2758 | * The io_end structure takes a reference to the inode, |
| 2759 | * that structure needs to be destroyed and the |
| 2760 | * reference to the inode need to be dropped, when IO is |
| 2761 | * complete, even with 0 byte write, or failed. |
| 2762 | * |
| 2763 | * In the successful AIO DIO case, the io_end structure will be |
| 2764 | * desctroyed and the reference to the inode will be dropped |
| 2765 | * after the end_io call back function is called. |
| 2766 | * |
| 2767 | * In the case there is 0 byte write, or error case, since |
| 2768 | * VFS direct IO won't invoke the end_io call back function, |
| 2769 | * we need to free the end_io structure here. |
| 2770 | */ |
| 2771 | if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { |
| 2772 | ext4_free_io_end(iocb->private); |
| 2773 | iocb->private = NULL; |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 2774 | } else if (ret > 0 && ext4_test_inode_state(inode, |
| 2775 | EXT4_STATE_DIO_UNWRITTEN)) { |
Mingming | 109f556 | 2009-11-10 10:48:08 -0500 | [diff] [blame] | 2776 | int err; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2777 | /* |
| 2778 | * for non AIO case, since the IO is already |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2779 | * completed, we could do the conversion right here |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2780 | */ |
Mingming | 109f556 | 2009-11-10 10:48:08 -0500 | [diff] [blame] | 2781 | err = ext4_convert_unwritten_extents(inode, |
| 2782 | offset, ret); |
| 2783 | if (err < 0) |
| 2784 | ret = err; |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 2785 | ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); |
Mingming | 109f556 | 2009-11-10 10:48:08 -0500 | [diff] [blame] | 2786 | } |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2787 | return ret; |
| 2788 | } |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 2789 | |
| 2790 | /* for write the the end of file case, we fall back to old way */ |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2791 | return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); |
| 2792 | } |
| 2793 | |
| 2794 | static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, |
| 2795 | const struct iovec *iov, loff_t offset, |
| 2796 | unsigned long nr_segs) |
| 2797 | { |
| 2798 | struct file *file = iocb->ki_filp; |
| 2799 | struct inode *inode = file->f_mapping->host; |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 2800 | ssize_t ret; |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2801 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 2802 | trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 2803 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 2804 | ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); |
| 2805 | else |
| 2806 | ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); |
| 2807 | trace_ext4_direct_IO_exit(inode, offset, |
| 2808 | iov_length(iov, nr_segs), rw, ret); |
| 2809 | return ret; |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 2810 | } |
| 2811 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2812 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2813 | * Pages can be marked dirty completely asynchronously from ext4's journalling |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2814 | * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do |
| 2815 | * much here because ->set_page_dirty is called under VFS locks. The page is |
| 2816 | * not necessarily locked. |
| 2817 | * |
| 2818 | * We cannot just dirty the page and leave attached buffers clean, because the |
| 2819 | * buffers' dirty state is "definitive". We cannot just set the buffers dirty |
| 2820 | * or jbddirty because all the journalling code will explode. |
| 2821 | * |
| 2822 | * So what we do is to mark the page "pending dirty" and next time writepage |
| 2823 | * is called, propagate that into the buffers appropriately. |
| 2824 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2825 | static int ext4_journalled_set_page_dirty(struct page *page) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2826 | { |
| 2827 | SetPageChecked(page); |
| 2828 | return __set_page_dirty_nobuffers(page); |
| 2829 | } |
| 2830 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2831 | static const struct address_space_operations ext4_ordered_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2832 | .readpage = ext4_readpage, |
| 2833 | .readpages = ext4_readpages, |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 2834 | .writepage = ext4_writepage, |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2835 | .write_begin = ext4_write_begin, |
| 2836 | .write_end = ext4_ordered_write_end, |
| 2837 | .bmap = ext4_bmap, |
| 2838 | .invalidatepage = ext4_invalidatepage, |
| 2839 | .releasepage = ext4_releasepage, |
| 2840 | .direct_IO = ext4_direct_IO, |
| 2841 | .migratepage = buffer_migrate_page, |
| 2842 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 2843 | .error_remove_page = generic_error_remove_page, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2844 | }; |
| 2845 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2846 | static const struct address_space_operations ext4_writeback_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2847 | .readpage = ext4_readpage, |
| 2848 | .readpages = ext4_readpages, |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 2849 | .writepage = ext4_writepage, |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2850 | .write_begin = ext4_write_begin, |
| 2851 | .write_end = ext4_writeback_write_end, |
| 2852 | .bmap = ext4_bmap, |
| 2853 | .invalidatepage = ext4_invalidatepage, |
| 2854 | .releasepage = ext4_releasepage, |
| 2855 | .direct_IO = ext4_direct_IO, |
| 2856 | .migratepage = buffer_migrate_page, |
| 2857 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 2858 | .error_remove_page = generic_error_remove_page, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2859 | }; |
| 2860 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2861 | static const struct address_space_operations ext4_journalled_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2862 | .readpage = ext4_readpage, |
| 2863 | .readpages = ext4_readpages, |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 2864 | .writepage = ext4_writepage, |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2865 | .write_begin = ext4_write_begin, |
| 2866 | .write_end = ext4_journalled_write_end, |
| 2867 | .set_page_dirty = ext4_journalled_set_page_dirty, |
| 2868 | .bmap = ext4_bmap, |
| 2869 | .invalidatepage = ext4_invalidatepage, |
| 2870 | .releasepage = ext4_releasepage, |
| 2871 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 2872 | .error_remove_page = generic_error_remove_page, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2873 | }; |
| 2874 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2875 | static const struct address_space_operations ext4_da_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2876 | .readpage = ext4_readpage, |
| 2877 | .readpages = ext4_readpages, |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 2878 | .writepage = ext4_writepage, |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2879 | .writepages = ext4_da_writepages, |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2880 | .write_begin = ext4_da_write_begin, |
| 2881 | .write_end = ext4_da_write_end, |
| 2882 | .bmap = ext4_bmap, |
| 2883 | .invalidatepage = ext4_da_invalidatepage, |
| 2884 | .releasepage = ext4_releasepage, |
| 2885 | .direct_IO = ext4_direct_IO, |
| 2886 | .migratepage = buffer_migrate_page, |
| 2887 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 2888 | .error_remove_page = generic_error_remove_page, |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2889 | }; |
| 2890 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2891 | void ext4_set_aops(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2892 | { |
Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2893 | if (ext4_should_order_data(inode) && |
| 2894 | test_opt(inode->i_sb, DELALLOC)) |
| 2895 | inode->i_mapping->a_ops = &ext4_da_aops; |
| 2896 | else if (ext4_should_order_data(inode)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2897 | inode->i_mapping->a_ops = &ext4_ordered_aops; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2898 | else if (ext4_should_writeback_data(inode) && |
| 2899 | test_opt(inode->i_sb, DELALLOC)) |
| 2900 | inode->i_mapping->a_ops = &ext4_da_aops; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2901 | else if (ext4_should_writeback_data(inode)) |
| 2902 | inode->i_mapping->a_ops = &ext4_writeback_aops; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2903 | else |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2904 | inode->i_mapping->a_ops = &ext4_journalled_aops; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2908 | * ext4_block_truncate_page() zeroes out a mapping from file offset `from' |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2909 | * up to the end of the block which corresponds to `from'. |
| 2910 | * This required during truncate. We need to physically zero the tail end |
| 2911 | * of that block so it doesn't yield old data if the file is later grown. |
| 2912 | */ |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2913 | int ext4_block_truncate_page(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2914 | struct address_space *mapping, loff_t from) |
| 2915 | { |
Allison Henderson | 3084885 | 2011-05-25 07:41:32 -0400 | [diff] [blame] | 2916 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 2917 | unsigned length; |
| 2918 | unsigned blocksize; |
| 2919 | struct inode *inode = mapping->host; |
| 2920 | |
| 2921 | blocksize = inode->i_sb->s_blocksize; |
| 2922 | length = blocksize - (offset & (blocksize - 1)); |
| 2923 | |
| 2924 | return ext4_block_zero_page_range(handle, mapping, from, length); |
| 2925 | } |
| 2926 | |
| 2927 | /* |
| 2928 | * ext4_block_zero_page_range() zeros out a mapping of length 'length' |
| 2929 | * starting from file offset 'from'. The range to be zero'd must |
| 2930 | * be contained with in one block. If the specified range exceeds |
| 2931 | * the end of the block it will be shortened to end of the block |
| 2932 | * that cooresponds to 'from' |
| 2933 | */ |
| 2934 | int ext4_block_zero_page_range(handle_t *handle, |
| 2935 | struct address_space *mapping, loff_t from, loff_t length) |
| 2936 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2937 | ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2938 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
Allison Henderson | 3084885 | 2011-05-25 07:41:32 -0400 | [diff] [blame] | 2939 | unsigned blocksize, max, pos; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2940 | ext4_lblk_t iblock; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2941 | struct inode *inode = mapping->host; |
| 2942 | struct buffer_head *bh; |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2943 | struct page *page; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2944 | int err = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2945 | |
Theodore Ts'o | f4a0101 | 2009-07-05 22:08:16 -0400 | [diff] [blame] | 2946 | page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, |
| 2947 | mapping_gfp_mask(mapping) & ~__GFP_FS); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2948 | if (!page) |
| 2949 | return -EINVAL; |
| 2950 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2951 | blocksize = inode->i_sb->s_blocksize; |
Allison Henderson | 3084885 | 2011-05-25 07:41:32 -0400 | [diff] [blame] | 2952 | max = blocksize - (offset & (blocksize - 1)); |
| 2953 | |
| 2954 | /* |
| 2955 | * correct length if it does not fall between |
| 2956 | * 'from' and the end of the block |
| 2957 | */ |
| 2958 | if (length > max || length < 0) |
| 2959 | length = max; |
| 2960 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2961 | iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); |
| 2962 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2963 | if (!page_has_buffers(page)) |
| 2964 | create_empty_buffers(page, blocksize, 0); |
| 2965 | |
| 2966 | /* Find the buffer that contains "offset" */ |
| 2967 | bh = page_buffers(page); |
| 2968 | pos = blocksize; |
| 2969 | while (offset >= pos) { |
| 2970 | bh = bh->b_this_page; |
| 2971 | iblock++; |
| 2972 | pos += blocksize; |
| 2973 | } |
| 2974 | |
| 2975 | err = 0; |
| 2976 | if (buffer_freed(bh)) { |
| 2977 | BUFFER_TRACE(bh, "freed: skip"); |
| 2978 | goto unlock; |
| 2979 | } |
| 2980 | |
| 2981 | if (!buffer_mapped(bh)) { |
| 2982 | BUFFER_TRACE(bh, "unmapped"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2983 | ext4_get_block(inode, iblock, bh, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2984 | /* unmapped? It's a hole - nothing to do */ |
| 2985 | if (!buffer_mapped(bh)) { |
| 2986 | BUFFER_TRACE(bh, "still unmapped"); |
| 2987 | goto unlock; |
| 2988 | } |
| 2989 | } |
| 2990 | |
| 2991 | /* Ok, it's mapped. Make sure it's up-to-date */ |
| 2992 | if (PageUptodate(page)) |
| 2993 | set_buffer_uptodate(bh); |
| 2994 | |
| 2995 | if (!buffer_uptodate(bh)) { |
| 2996 | err = -EIO; |
| 2997 | ll_rw_block(READ, 1, &bh); |
| 2998 | wait_on_buffer(bh); |
| 2999 | /* Uhhuh. Read error. Complain and punt. */ |
| 3000 | if (!buffer_uptodate(bh)) |
| 3001 | goto unlock; |
| 3002 | } |
| 3003 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3004 | if (ext4_should_journal_data(inode)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3005 | BUFFER_TRACE(bh, "get write access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3006 | err = ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3007 | if (err) |
| 3008 | goto unlock; |
| 3009 | } |
| 3010 | |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 3011 | zero_user(page, offset, length); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3012 | |
| 3013 | BUFFER_TRACE(bh, "zeroed end of block"); |
| 3014 | |
| 3015 | err = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3016 | if (ext4_should_journal_data(inode)) { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3017 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3018 | } else { |
Theodore Ts'o | 8aefcd5 | 2011-01-10 12:29:43 -0500 | [diff] [blame] | 3019 | if (ext4_should_order_data(inode) && EXT4_I(inode)->jinode) |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3020 | err = ext4_jbd2_file_inode(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3021 | mark_buffer_dirty(bh); |
| 3022 | } |
| 3023 | |
| 3024 | unlock: |
| 3025 | unlock_page(page); |
| 3026 | page_cache_release(page); |
| 3027 | return err; |
| 3028 | } |
| 3029 | |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3030 | int ext4_can_truncate(struct inode *inode) |
| 3031 | { |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3032 | if (S_ISREG(inode->i_mode)) |
| 3033 | return 1; |
| 3034 | if (S_ISDIR(inode->i_mode)) |
| 3035 | return 1; |
| 3036 | if (S_ISLNK(inode->i_mode)) |
| 3037 | return !ext4_inode_is_fast_symlink(inode); |
| 3038 | return 0; |
| 3039 | } |
| 3040 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3041 | /* |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 3042 | * ext4_punch_hole: punches a hole in a file by releaseing the blocks |
| 3043 | * associated with the given offset and length |
| 3044 | * |
| 3045 | * @inode: File inode |
| 3046 | * @offset: The offset where the hole will begin |
| 3047 | * @len: The length of the hole |
| 3048 | * |
| 3049 | * Returns: 0 on sucess or negative on failure |
| 3050 | */ |
| 3051 | |
| 3052 | int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) |
| 3053 | { |
| 3054 | struct inode *inode = file->f_path.dentry->d_inode; |
| 3055 | if (!S_ISREG(inode->i_mode)) |
| 3056 | return -ENOTSUPP; |
| 3057 | |
| 3058 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
| 3059 | /* TODO: Add support for non extent hole punching */ |
| 3060 | return -ENOTSUPP; |
| 3061 | } |
| 3062 | |
| 3063 | return ext4_ext_punch_hole(file, offset, length); |
| 3064 | } |
| 3065 | |
| 3066 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3067 | * ext4_truncate() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3068 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3069 | * We block out ext4_get_block() block instantiations across the entire |
| 3070 | * transaction, and VFS/VM ensures that ext4_truncate() cannot run |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3071 | * simultaneously on behalf of the same inode. |
| 3072 | * |
| 3073 | * As we work through the truncate and commmit bits of it to the journal there |
| 3074 | * is one core, guiding principle: the file's tree must always be consistent on |
| 3075 | * disk. We must be able to restart the truncate after a crash. |
| 3076 | * |
| 3077 | * The file's tree may be transiently inconsistent in memory (although it |
| 3078 | * probably isn't), but whenever we close off and commit a journal transaction, |
| 3079 | * the contents of (the filesystem + the journal) must be consistent and |
| 3080 | * restartable. It's pretty simple, really: bottom up, right to left (although |
| 3081 | * left-to-right works OK too). |
| 3082 | * |
| 3083 | * Note that at recovery time, journal replay occurs *before* the restart of |
| 3084 | * truncate against the orphan inode list. |
| 3085 | * |
| 3086 | * The committed inode has the new, desired i_size (which is the same as |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3087 | * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3088 | * that this inode's truncate did not complete and it will again call |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3089 | * ext4_truncate() to have another go. So there will be instantiated blocks |
| 3090 | * to the right of the truncation point in a crashed ext4 filesystem. But |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3091 | * that's fine - as long as they are linked from the inode, the post-crash |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3092 | * ext4_truncate() run will find them and release them. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3093 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3094 | void ext4_truncate(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3095 | { |
Amir Goldstein | ff9893d | 2011-06-27 16:36:31 -0400 | [diff] [blame] | 3096 | trace_ext4_truncate_enter(inode); |
| 3097 | |
| 3098 | if (!ext4_can_truncate(inode)) |
| 3099 | return; |
| 3100 | |
| 3101 | ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); |
| 3102 | |
| 3103 | if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) |
| 3104 | ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); |
| 3105 | |
| 3106 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 3107 | ext4_ext_truncate(inode); |
| 3108 | else |
| 3109 | ext4_ind_truncate(inode); |
| 3110 | |
| 3111 | trace_ext4_truncate_exit(inode); |
| 3112 | } |
| 3113 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3114 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3115 | * ext4_get_inode_loc returns with an extra refcount against the inode's |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3116 | * underlying buffer_head on success. If 'in_mem' is true, we have all |
| 3117 | * data in memory that is needed to recreate the on-disk version of this |
| 3118 | * inode. |
| 3119 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3120 | static int __ext4_get_inode_loc(struct inode *inode, |
| 3121 | struct ext4_iloc *iloc, int in_mem) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3122 | { |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3123 | struct ext4_group_desc *gdp; |
| 3124 | struct buffer_head *bh; |
| 3125 | struct super_block *sb = inode->i_sb; |
| 3126 | ext4_fsblk_t block; |
| 3127 | int inodes_per_block, inode_offset; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3128 | |
Aneesh Kumar K.V | 3a06d77 | 2008-11-22 15:04:59 -0500 | [diff] [blame] | 3129 | iloc->bh = NULL; |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3130 | if (!ext4_valid_inum(sb, inode->i_ino)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3131 | return -EIO; |
| 3132 | |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3133 | iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); |
| 3134 | gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); |
| 3135 | if (!gdp) |
| 3136 | return -EIO; |
| 3137 | |
| 3138 | /* |
| 3139 | * Figure out the offset within the block group inode table |
| 3140 | */ |
Tao Ma | 00d0988 | 2011-05-09 10:26:41 -0400 | [diff] [blame] | 3141 | inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3142 | inode_offset = ((inode->i_ino - 1) % |
| 3143 | EXT4_INODES_PER_GROUP(sb)); |
| 3144 | block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); |
| 3145 | iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); |
| 3146 | |
| 3147 | bh = sb_getblk(sb, block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3148 | if (!bh) { |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 3149 | EXT4_ERROR_INODE_BLOCK(inode, block, |
| 3150 | "unable to read itable block"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3151 | return -EIO; |
| 3152 | } |
| 3153 | if (!buffer_uptodate(bh)) { |
| 3154 | lock_buffer(bh); |
Hidehiro Kawai | 9c83a92 | 2008-07-26 16:39:26 -0400 | [diff] [blame] | 3155 | |
| 3156 | /* |
| 3157 | * If the buffer has the write error flag, we have failed |
| 3158 | * to write out another inode in the same block. In this |
| 3159 | * case, we don't have to read the block because we may |
| 3160 | * read the old inode data successfully. |
| 3161 | */ |
| 3162 | if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) |
| 3163 | set_buffer_uptodate(bh); |
| 3164 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3165 | if (buffer_uptodate(bh)) { |
| 3166 | /* someone brought it uptodate while we waited */ |
| 3167 | unlock_buffer(bh); |
| 3168 | goto has_buffer; |
| 3169 | } |
| 3170 | |
| 3171 | /* |
| 3172 | * If we have all information of the inode in memory and this |
| 3173 | * is the only valid inode in the block, we need not read the |
| 3174 | * block. |
| 3175 | */ |
| 3176 | if (in_mem) { |
| 3177 | struct buffer_head *bitmap_bh; |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3178 | int i, start; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3179 | |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3180 | start = inode_offset & ~(inodes_per_block - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3181 | |
| 3182 | /* Is the inode bitmap in cache? */ |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3183 | bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3184 | if (!bitmap_bh) |
| 3185 | goto make_io; |
| 3186 | |
| 3187 | /* |
| 3188 | * If the inode bitmap isn't in cache then the |
| 3189 | * optimisation may end up performing two reads instead |
| 3190 | * of one, so skip it. |
| 3191 | */ |
| 3192 | if (!buffer_uptodate(bitmap_bh)) { |
| 3193 | brelse(bitmap_bh); |
| 3194 | goto make_io; |
| 3195 | } |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3196 | for (i = start; i < start + inodes_per_block; i++) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3197 | if (i == inode_offset) |
| 3198 | continue; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3199 | if (ext4_test_bit(i, bitmap_bh->b_data)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3200 | break; |
| 3201 | } |
| 3202 | brelse(bitmap_bh); |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3203 | if (i == start + inodes_per_block) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3204 | /* all other inodes are free, so skip I/O */ |
| 3205 | memset(bh->b_data, 0, bh->b_size); |
| 3206 | set_buffer_uptodate(bh); |
| 3207 | unlock_buffer(bh); |
| 3208 | goto has_buffer; |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | make_io: |
| 3213 | /* |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3214 | * If we need to do any I/O, try to pre-readahead extra |
| 3215 | * blocks from the inode table. |
| 3216 | */ |
| 3217 | if (EXT4_SB(sb)->s_inode_readahead_blks) { |
| 3218 | ext4_fsblk_t b, end, table; |
| 3219 | unsigned num; |
| 3220 | |
| 3221 | table = ext4_inode_table(sb, gdp); |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 3222 | /* s_inode_readahead_blks is always a power of 2 */ |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3223 | b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); |
| 3224 | if (table > b) |
| 3225 | b = table; |
| 3226 | end = b + EXT4_SB(sb)->s_inode_readahead_blks; |
| 3227 | num = EXT4_INODES_PER_GROUP(sb); |
| 3228 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 3229 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 3230 | num -= ext4_itable_unused_count(sb, gdp); |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3231 | table += num / inodes_per_block; |
| 3232 | if (end > table) |
| 3233 | end = table; |
| 3234 | while (b <= end) |
| 3235 | sb_breadahead(sb, b++); |
| 3236 | } |
| 3237 | |
| 3238 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3239 | * There are other valid inodes in the buffer, this inode |
| 3240 | * has in-inode xattrs, or we don't have this inode in memory. |
| 3241 | * Read the block from disk. |
| 3242 | */ |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 3243 | trace_ext4_load_inode(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3244 | get_bh(bh); |
| 3245 | bh->b_end_io = end_buffer_read_sync; |
| 3246 | submit_bh(READ_META, bh); |
| 3247 | wait_on_buffer(bh); |
| 3248 | if (!buffer_uptodate(bh)) { |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 3249 | EXT4_ERROR_INODE_BLOCK(inode, block, |
| 3250 | "unable to read itable block"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3251 | brelse(bh); |
| 3252 | return -EIO; |
| 3253 | } |
| 3254 | } |
| 3255 | has_buffer: |
| 3256 | iloc->bh = bh; |
| 3257 | return 0; |
| 3258 | } |
| 3259 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3260 | int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3261 | { |
| 3262 | /* We have all inode data except xattrs in memory here. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3263 | return __ext4_get_inode_loc(inode, iloc, |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 3264 | !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3265 | } |
| 3266 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3267 | void ext4_set_inode_flags(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3268 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3269 | unsigned int flags = EXT4_I(inode)->i_flags; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3270 | |
| 3271 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3272 | if (flags & EXT4_SYNC_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3273 | inode->i_flags |= S_SYNC; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3274 | if (flags & EXT4_APPEND_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3275 | inode->i_flags |= S_APPEND; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3276 | if (flags & EXT4_IMMUTABLE_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3277 | inode->i_flags |= S_IMMUTABLE; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3278 | if (flags & EXT4_NOATIME_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3279 | inode->i_flags |= S_NOATIME; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3280 | if (flags & EXT4_DIRSYNC_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3281 | inode->i_flags |= S_DIRSYNC; |
| 3282 | } |
| 3283 | |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 3284 | /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ |
| 3285 | void ext4_get_inode_flags(struct ext4_inode_info *ei) |
| 3286 | { |
Dmitry Monakhov | 84a8dce | 2010-06-05 11:51:27 -0400 | [diff] [blame] | 3287 | unsigned int vfs_fl; |
| 3288 | unsigned long old_fl, new_fl; |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 3289 | |
Dmitry Monakhov | 84a8dce | 2010-06-05 11:51:27 -0400 | [diff] [blame] | 3290 | do { |
| 3291 | vfs_fl = ei->vfs_inode.i_flags; |
| 3292 | old_fl = ei->i_flags; |
| 3293 | new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| |
| 3294 | EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| |
| 3295 | EXT4_DIRSYNC_FL); |
| 3296 | if (vfs_fl & S_SYNC) |
| 3297 | new_fl |= EXT4_SYNC_FL; |
| 3298 | if (vfs_fl & S_APPEND) |
| 3299 | new_fl |= EXT4_APPEND_FL; |
| 3300 | if (vfs_fl & S_IMMUTABLE) |
| 3301 | new_fl |= EXT4_IMMUTABLE_FL; |
| 3302 | if (vfs_fl & S_NOATIME) |
| 3303 | new_fl |= EXT4_NOATIME_FL; |
| 3304 | if (vfs_fl & S_DIRSYNC) |
| 3305 | new_fl |= EXT4_DIRSYNC_FL; |
| 3306 | } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 3307 | } |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 3308 | |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3309 | static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 3310 | struct ext4_inode_info *ei) |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3311 | { |
| 3312 | blkcnt_t i_blocks ; |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3313 | struct inode *inode = &(ei->vfs_inode); |
| 3314 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3315 | |
| 3316 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 3317 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { |
| 3318 | /* we are using combined 48 bit field */ |
| 3319 | i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | |
| 3320 | le32_to_cpu(raw_inode->i_blocks_lo); |
Theodore Ts'o | 07a0382 | 2010-06-14 09:54:48 -0400 | [diff] [blame] | 3321 | if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3322 | /* i_blocks represent file system block size */ |
| 3323 | return i_blocks << (inode->i_blkbits - 9); |
| 3324 | } else { |
| 3325 | return i_blocks; |
| 3326 | } |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3327 | } else { |
| 3328 | return le32_to_cpu(raw_inode->i_blocks_lo); |
| 3329 | } |
| 3330 | } |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 3331 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 3332 | struct inode *ext4_iget(struct super_block *sb, unsigned long ino) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3333 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3334 | struct ext4_iloc iloc; |
| 3335 | struct ext4_inode *raw_inode; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 3336 | struct ext4_inode_info *ei; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 3337 | struct inode *inode; |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3338 | journal_t *journal = EXT4_SB(sb)->s_journal; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 3339 | long ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3340 | int block; |
| 3341 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 3342 | inode = iget_locked(sb, ino); |
| 3343 | if (!inode) |
| 3344 | return ERR_PTR(-ENOMEM); |
| 3345 | if (!(inode->i_state & I_NEW)) |
| 3346 | return inode; |
| 3347 | |
| 3348 | ei = EXT4_I(inode); |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 3349 | iloc.bh = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3350 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 3351 | ret = __ext4_get_inode_loc(inode, &iloc, 0); |
| 3352 | if (ret < 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3353 | goto bad_inode; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3354 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3355 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); |
| 3356 | inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); |
| 3357 | inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3358 | if (!(test_opt(inode->i_sb, NO_UID32))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3359 | inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; |
| 3360 | inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; |
| 3361 | } |
| 3362 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3363 | |
Theodore Ts'o | 353eb83 | 2011-01-10 12:18:25 -0500 | [diff] [blame] | 3364 | ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3365 | ei->i_dir_start_lookup = 0; |
| 3366 | ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); |
| 3367 | /* We now have enough fields to check if the inode was active or not. |
| 3368 | * This is needed because nfsd might try to access dead inodes |
| 3369 | * the test is that same one that e2fsck uses |
| 3370 | * NeilBrown 1999oct15 |
| 3371 | */ |
| 3372 | if (inode->i_nlink == 0) { |
| 3373 | if (inode->i_mode == 0 || |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3374 | !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3375 | /* this inode is deleted */ |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 3376 | ret = -ESTALE; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3377 | goto bad_inode; |
| 3378 | } |
| 3379 | /* The only unlinked inodes we let through here have |
| 3380 | * valid i_mode and are being read by the orphan |
| 3381 | * recovery code: that's fine, we're about to complete |
| 3382 | * the process of deleting those. */ |
| 3383 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3384 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3385 | inode->i_blocks = ext4_inode_blocks(raw_inode, ei); |
Aneesh Kumar K.V | 7973c0c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3386 | ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); |
Theodore Ts'o | a9e8174 | 2009-04-24 16:11:18 -0400 | [diff] [blame] | 3387 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) |
Badari Pulavarty | a1ddeb7 | 2006-10-11 01:21:09 -0700 | [diff] [blame] | 3388 | ei->i_file_acl |= |
| 3389 | ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; |
Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3390 | inode->i_size = ext4_isize(raw_inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3391 | ei->i_disksize = inode->i_size; |
Dmitry Monakhov | a9e7f44 | 2009-12-14 15:21:14 +0300 | [diff] [blame] | 3392 | #ifdef CONFIG_QUOTA |
| 3393 | ei->i_reserved_quota = 0; |
| 3394 | #endif |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3395 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); |
| 3396 | ei->i_block_group = iloc.block_group; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 3397 | ei->i_last_alloc_group = ~0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3398 | /* |
| 3399 | * NOTE! The in-memory inode i_data array is in little-endian order |
| 3400 | * even on big-endian machines: we do NOT byteswap the block numbers! |
| 3401 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3402 | for (block = 0; block < EXT4_N_BLOCKS; block++) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3403 | ei->i_data[block] = raw_inode->i_block[block]; |
| 3404 | INIT_LIST_HEAD(&ei->i_orphan); |
| 3405 | |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3406 | /* |
| 3407 | * Set transaction id's of transactions that have to be committed |
| 3408 | * to finish f[data]sync. We set them to currently running transaction |
| 3409 | * as we cannot be sure that the inode or some of its metadata isn't |
| 3410 | * part of the transaction - the inode could have been reclaimed and |
| 3411 | * now it is reread from disk. |
| 3412 | */ |
| 3413 | if (journal) { |
| 3414 | transaction_t *transaction; |
| 3415 | tid_t tid; |
| 3416 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 3417 | read_lock(&journal->j_state_lock); |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3418 | if (journal->j_running_transaction) |
| 3419 | transaction = journal->j_running_transaction; |
| 3420 | else |
| 3421 | transaction = journal->j_committing_transaction; |
| 3422 | if (transaction) |
| 3423 | tid = transaction->t_tid; |
| 3424 | else |
| 3425 | tid = journal->j_commit_sequence; |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 3426 | read_unlock(&journal->j_state_lock); |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3427 | ei->i_sync_tid = tid; |
| 3428 | ei->i_datasync_tid = tid; |
| 3429 | } |
| 3430 | |
Eric Sandeen | 0040d98 | 2008-02-05 22:36:43 -0500 | [diff] [blame] | 3431 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3432 | ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3433 | if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > |
Kirill Korotaev | e5d2861 | 2007-06-23 17:16:51 -0700 | [diff] [blame] | 3434 | EXT4_INODE_SIZE(inode->i_sb)) { |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 3435 | ret = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3436 | goto bad_inode; |
Kirill Korotaev | e5d2861 | 2007-06-23 17:16:51 -0700 | [diff] [blame] | 3437 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3438 | if (ei->i_extra_isize == 0) { |
| 3439 | /* The extra space is currently unused. Use it. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3440 | ei->i_extra_isize = sizeof(struct ext4_inode) - |
| 3441 | EXT4_GOOD_OLD_INODE_SIZE; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3442 | } else { |
| 3443 | __le32 *magic = (void *)raw_inode + |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3444 | EXT4_GOOD_OLD_INODE_SIZE + |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3445 | ei->i_extra_isize; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3446 | if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 3447 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3448 | } |
| 3449 | } else |
| 3450 | ei->i_extra_isize = 0; |
| 3451 | |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 3452 | EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); |
| 3453 | EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); |
| 3454 | EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); |
| 3455 | EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); |
| 3456 | |
Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3457 | inode->i_version = le32_to_cpu(raw_inode->i_disk_version); |
| 3458 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { |
| 3459 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) |
| 3460 | inode->i_version |= |
| 3461 | (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; |
| 3462 | } |
| 3463 | |
Theodore Ts'o | c4b5a61 | 2009-04-24 18:45:35 -0400 | [diff] [blame] | 3464 | ret = 0; |
Theodore Ts'o | 485c26e | 2009-04-24 13:43:20 -0400 | [diff] [blame] | 3465 | if (ei->i_file_acl && |
Theodore Ts'o | 1032988 | 2009-11-15 15:29:56 -0500 | [diff] [blame] | 3466 | !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 3467 | EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", |
| 3468 | ei->i_file_acl); |
Theodore Ts'o | 485c26e | 2009-04-24 13:43:20 -0400 | [diff] [blame] | 3469 | ret = -EIO; |
| 3470 | goto bad_inode; |
Theodore Ts'o | 07a0382 | 2010-06-14 09:54:48 -0400 | [diff] [blame] | 3471 | } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
Theodore Ts'o | c4b5a61 | 2009-04-24 18:45:35 -0400 | [diff] [blame] | 3472 | if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 3473 | (S_ISLNK(inode->i_mode) && |
| 3474 | !ext4_inode_is_fast_symlink(inode))) |
| 3475 | /* Validate extent which is part of inode */ |
| 3476 | ret = ext4_ext_check_inode(inode); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 3477 | } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 3478 | (S_ISLNK(inode->i_mode) && |
| 3479 | !ext4_inode_is_fast_symlink(inode))) { |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 3480 | /* Validate block references which are part of inode */ |
Theodore Ts'o | 1f7d1e7 | 2011-06-27 19:16:02 -0400 | [diff] [blame] | 3481 | ret = ext4_ind_check_inode(inode); |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 3482 | } |
Theodore Ts'o | 567f3e9 | 2009-11-14 08:19:05 -0500 | [diff] [blame] | 3483 | if (ret) |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 3484 | goto bad_inode; |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 3485 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3486 | if (S_ISREG(inode->i_mode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3487 | inode->i_op = &ext4_file_inode_operations; |
| 3488 | inode->i_fop = &ext4_file_operations; |
| 3489 | ext4_set_aops(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3490 | } else if (S_ISDIR(inode->i_mode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3491 | inode->i_op = &ext4_dir_inode_operations; |
| 3492 | inode->i_fop = &ext4_dir_operations; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3493 | } else if (S_ISLNK(inode->i_mode)) { |
Duane Griffin | e83c139 | 2008-12-19 20:47:15 +0000 | [diff] [blame] | 3494 | if (ext4_inode_is_fast_symlink(inode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3495 | inode->i_op = &ext4_fast_symlink_inode_operations; |
Duane Griffin | e83c139 | 2008-12-19 20:47:15 +0000 | [diff] [blame] | 3496 | nd_terminate_link(ei->i_data, inode->i_size, |
| 3497 | sizeof(ei->i_data) - 1); |
| 3498 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3499 | inode->i_op = &ext4_symlink_inode_operations; |
| 3500 | ext4_set_aops(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3501 | } |
Theodore Ts'o | 563bdd6 | 2009-03-26 00:06:19 -0400 | [diff] [blame] | 3502 | } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 3503 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3504 | inode->i_op = &ext4_special_inode_operations; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3505 | if (raw_inode->i_block[0]) |
| 3506 | init_special_inode(inode, inode->i_mode, |
| 3507 | old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); |
| 3508 | else |
| 3509 | init_special_inode(inode, inode->i_mode, |
| 3510 | new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); |
Theodore Ts'o | 563bdd6 | 2009-03-26 00:06:19 -0400 | [diff] [blame] | 3511 | } else { |
Theodore Ts'o | 563bdd6 | 2009-03-26 00:06:19 -0400 | [diff] [blame] | 3512 | ret = -EIO; |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 3513 | EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); |
Theodore Ts'o | 563bdd6 | 2009-03-26 00:06:19 -0400 | [diff] [blame] | 3514 | goto bad_inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3515 | } |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3516 | brelse(iloc.bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3517 | ext4_set_inode_flags(inode); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 3518 | unlock_new_inode(inode); |
| 3519 | return inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3520 | |
| 3521 | bad_inode: |
Theodore Ts'o | 567f3e9 | 2009-11-14 08:19:05 -0500 | [diff] [blame] | 3522 | brelse(iloc.bh); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 3523 | iget_failed(inode); |
| 3524 | return ERR_PTR(ret); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3525 | } |
| 3526 | |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3527 | static int ext4_inode_blocks_set(handle_t *handle, |
| 3528 | struct ext4_inode *raw_inode, |
| 3529 | struct ext4_inode_info *ei) |
| 3530 | { |
| 3531 | struct inode *inode = &(ei->vfs_inode); |
| 3532 | u64 i_blocks = inode->i_blocks; |
| 3533 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3534 | |
| 3535 | if (i_blocks <= ~0U) { |
| 3536 | /* |
| 3537 | * i_blocks can be represnted in a 32 bit variable |
| 3538 | * as multiple of 512 bytes |
| 3539 | */ |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3540 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3541 | raw_inode->i_blocks_high = 0; |
Dmitry Monakhov | 84a8dce | 2010-06-05 11:51:27 -0400 | [diff] [blame] | 3542 | ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); |
Theodore Ts'o | f287a1a | 2008-10-16 22:50:48 -0400 | [diff] [blame] | 3543 | return 0; |
| 3544 | } |
| 3545 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) |
| 3546 | return -EFBIG; |
| 3547 | |
| 3548 | if (i_blocks <= 0xffffffffffffULL) { |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3549 | /* |
| 3550 | * i_blocks can be represented in a 48 bit variable |
| 3551 | * as multiple of 512 bytes |
| 3552 | */ |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3553 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3554 | raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); |
Dmitry Monakhov | 84a8dce | 2010-06-05 11:51:27 -0400 | [diff] [blame] | 3555 | ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3556 | } else { |
Dmitry Monakhov | 84a8dce | 2010-06-05 11:51:27 -0400 | [diff] [blame] | 3557 | ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3558 | /* i_block is stored in file system block size */ |
| 3559 | i_blocks = i_blocks >> (inode->i_blkbits - 9); |
| 3560 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
| 3561 | raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3562 | } |
Theodore Ts'o | f287a1a | 2008-10-16 22:50:48 -0400 | [diff] [blame] | 3563 | return 0; |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3564 | } |
| 3565 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3566 | /* |
| 3567 | * Post the struct inode info into an on-disk inode location in the |
| 3568 | * buffer-cache. This gobbles the caller's reference to the |
| 3569 | * buffer_head in the inode location struct. |
| 3570 | * |
| 3571 | * The caller must have write access to iloc->bh. |
| 3572 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3573 | static int ext4_do_update_inode(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3574 | struct inode *inode, |
Frank Mayhar | 830156c | 2009-09-29 10:07:47 -0400 | [diff] [blame] | 3575 | struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3576 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3577 | struct ext4_inode *raw_inode = ext4_raw_inode(iloc); |
| 3578 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3579 | struct buffer_head *bh = iloc->bh; |
| 3580 | int err = 0, rc, block; |
| 3581 | |
| 3582 | /* For fields not not tracking in the in-memory inode, |
| 3583 | * initialise them to zero for new inodes. */ |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 3584 | if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3585 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3586 | |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 3587 | ext4_get_inode_flags(ei); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3588 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3589 | if (!(test_opt(inode->i_sb, NO_UID32))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3590 | raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); |
| 3591 | raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); |
| 3592 | /* |
| 3593 | * Fix up interoperability with old kernels. Otherwise, old inodes get |
| 3594 | * re-used with the upper 16 bits of the uid/gid intact |
| 3595 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3596 | if (!ei->i_dtime) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3597 | raw_inode->i_uid_high = |
| 3598 | cpu_to_le16(high_16_bits(inode->i_uid)); |
| 3599 | raw_inode->i_gid_high = |
| 3600 | cpu_to_le16(high_16_bits(inode->i_gid)); |
| 3601 | } else { |
| 3602 | raw_inode->i_uid_high = 0; |
| 3603 | raw_inode->i_gid_high = 0; |
| 3604 | } |
| 3605 | } else { |
| 3606 | raw_inode->i_uid_low = |
| 3607 | cpu_to_le16(fs_high2lowuid(inode->i_uid)); |
| 3608 | raw_inode->i_gid_low = |
| 3609 | cpu_to_le16(fs_high2lowgid(inode->i_gid)); |
| 3610 | raw_inode->i_uid_high = 0; |
| 3611 | raw_inode->i_gid_high = 0; |
| 3612 | } |
| 3613 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 3614 | |
| 3615 | EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); |
| 3616 | EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); |
| 3617 | EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); |
| 3618 | EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); |
| 3619 | |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3620 | if (ext4_inode_blocks_set(handle, raw_inode, ei)) |
| 3621 | goto out_brelse; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3622 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); |
Theodore Ts'o | 353eb83 | 2011-01-10 12:18:25 -0500 | [diff] [blame] | 3623 | raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 3624 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != |
| 3625 | cpu_to_le32(EXT4_OS_HURD)) |
Badari Pulavarty | a1ddeb7 | 2006-10-11 01:21:09 -0700 | [diff] [blame] | 3626 | raw_inode->i_file_acl_high = |
| 3627 | cpu_to_le16(ei->i_file_acl >> 32); |
Aneesh Kumar K.V | 7973c0c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3628 | raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); |
Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3629 | ext4_isize_set(raw_inode, ei->i_disksize); |
| 3630 | if (ei->i_disksize > 0x7fffffffULL) { |
| 3631 | struct super_block *sb = inode->i_sb; |
| 3632 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 3633 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || |
| 3634 | EXT4_SB(sb)->s_es->s_rev_level == |
| 3635 | cpu_to_le32(EXT4_GOOD_OLD_REV)) { |
| 3636 | /* If this is the first large file |
| 3637 | * created, add a flag to the superblock. |
| 3638 | */ |
| 3639 | err = ext4_journal_get_write_access(handle, |
| 3640 | EXT4_SB(sb)->s_sbh); |
| 3641 | if (err) |
| 3642 | goto out_brelse; |
| 3643 | ext4_update_dynamic_rev(sb); |
| 3644 | EXT4_SET_RO_COMPAT_FEATURE(sb, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3645 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE); |
Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3646 | sb->s_dirt = 1; |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3647 | ext4_handle_sync(handle); |
Curt Wohlgemuth | 73b50c1 | 2010-02-16 15:06:29 -0500 | [diff] [blame] | 3648 | err = ext4_handle_dirty_metadata(handle, NULL, |
Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3649 | EXT4_SB(sb)->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3650 | } |
| 3651 | } |
| 3652 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); |
| 3653 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
| 3654 | if (old_valid_dev(inode->i_rdev)) { |
| 3655 | raw_inode->i_block[0] = |
| 3656 | cpu_to_le32(old_encode_dev(inode->i_rdev)); |
| 3657 | raw_inode->i_block[1] = 0; |
| 3658 | } else { |
| 3659 | raw_inode->i_block[0] = 0; |
| 3660 | raw_inode->i_block[1] = |
| 3661 | cpu_to_le32(new_encode_dev(inode->i_rdev)); |
| 3662 | raw_inode->i_block[2] = 0; |
| 3663 | } |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 3664 | } else |
| 3665 | for (block = 0; block < EXT4_N_BLOCKS; block++) |
| 3666 | raw_inode->i_block[block] = ei->i_data[block]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3667 | |
Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3668 | raw_inode->i_disk_version = cpu_to_le32(inode->i_version); |
| 3669 | if (ei->i_extra_isize) { |
| 3670 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) |
| 3671 | raw_inode->i_version_hi = |
| 3672 | cpu_to_le32(inode->i_version >> 32); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3673 | raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); |
Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3674 | } |
| 3675 | |
Frank Mayhar | 830156c | 2009-09-29 10:07:47 -0400 | [diff] [blame] | 3676 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
Curt Wohlgemuth | 73b50c1 | 2010-02-16 15:06:29 -0500 | [diff] [blame] | 3677 | rc = ext4_handle_dirty_metadata(handle, NULL, bh); |
Frank Mayhar | 830156c | 2009-09-29 10:07:47 -0400 | [diff] [blame] | 3678 | if (!err) |
| 3679 | err = rc; |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 3680 | ext4_clear_inode_state(inode, EXT4_STATE_NEW); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3681 | |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3682 | ext4_update_inode_fsync_trans(handle, inode, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3683 | out_brelse: |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3684 | brelse(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3685 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3686 | return err; |
| 3687 | } |
| 3688 | |
| 3689 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3690 | * ext4_write_inode() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3691 | * |
| 3692 | * We are called from a few places: |
| 3693 | * |
| 3694 | * - Within generic_file_write() for O_SYNC files. |
| 3695 | * Here, there will be no transaction running. We wait for any running |
| 3696 | * trasnaction to commit. |
| 3697 | * |
| 3698 | * - Within sys_sync(), kupdate and such. |
| 3699 | * We wait on commit, if tol to. |
| 3700 | * |
| 3701 | * - Within prune_icache() (PF_MEMALLOC == true) |
| 3702 | * Here we simply return. We can't afford to block kswapd on the |
| 3703 | * journal commit. |
| 3704 | * |
| 3705 | * In all cases it is actually safe for us to return without doing anything, |
| 3706 | * because the inode has been copied into a raw inode buffer in |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3707 | * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3708 | * knfsd. |
| 3709 | * |
| 3710 | * Note that we are absolutely dependent upon all inode dirtiers doing the |
| 3711 | * right thing: they *must* call mark_inode_dirty() after dirtying info in |
| 3712 | * which we are interested. |
| 3713 | * |
| 3714 | * It would be a bug for them to not do this. The code: |
| 3715 | * |
| 3716 | * mark_inode_dirty(inode) |
| 3717 | * stuff(); |
| 3718 | * inode->i_size = expr; |
| 3719 | * |
| 3720 | * is in error because a kswapd-driven write_inode() could occur while |
| 3721 | * `stuff()' is running, and the new i_size will be lost. Plus the inode |
| 3722 | * will no longer be on the superblock's dirty inode list. |
| 3723 | */ |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 3724 | int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3725 | { |
Frank Mayhar | 91ac6f4 | 2009-09-09 22:33:47 -0400 | [diff] [blame] | 3726 | int err; |
| 3727 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3728 | if (current->flags & PF_MEMALLOC) |
| 3729 | return 0; |
| 3730 | |
Frank Mayhar | 91ac6f4 | 2009-09-09 22:33:47 -0400 | [diff] [blame] | 3731 | if (EXT4_SB(inode->i_sb)->s_journal) { |
| 3732 | if (ext4_journal_current_handle()) { |
| 3733 | jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); |
| 3734 | dump_stack(); |
| 3735 | return -EIO; |
| 3736 | } |
| 3737 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 3738 | if (wbc->sync_mode != WB_SYNC_ALL) |
Frank Mayhar | 91ac6f4 | 2009-09-09 22:33:47 -0400 | [diff] [blame] | 3739 | return 0; |
| 3740 | |
| 3741 | err = ext4_force_commit(inode->i_sb); |
| 3742 | } else { |
| 3743 | struct ext4_iloc iloc; |
| 3744 | |
Curt Wohlgemuth | 8b472d7 | 2010-04-03 16:45:06 -0400 | [diff] [blame] | 3745 | err = __ext4_get_inode_loc(inode, &iloc, 0); |
Frank Mayhar | 91ac6f4 | 2009-09-09 22:33:47 -0400 | [diff] [blame] | 3746 | if (err) |
| 3747 | return err; |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 3748 | if (wbc->sync_mode == WB_SYNC_ALL) |
Frank Mayhar | 830156c | 2009-09-29 10:07:47 -0400 | [diff] [blame] | 3749 | sync_dirty_buffer(iloc.bh); |
| 3750 | if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 3751 | EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, |
| 3752 | "IO error syncing inode"); |
Frank Mayhar | 830156c | 2009-09-29 10:07:47 -0400 | [diff] [blame] | 3753 | err = -EIO; |
| 3754 | } |
Curt Wohlgemuth | fd2dd9f | 2010-04-03 17:44:16 -0400 | [diff] [blame] | 3755 | brelse(iloc.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3756 | } |
Frank Mayhar | 91ac6f4 | 2009-09-09 22:33:47 -0400 | [diff] [blame] | 3757 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3761 | * ext4_setattr() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3762 | * |
| 3763 | * Called from notify_change. |
| 3764 | * |
| 3765 | * We want to trap VFS attempts to truncate the file as soon as |
| 3766 | * possible. In particular, we want to make sure that when the VFS |
| 3767 | * shrinks i_size, we put the inode on the orphan list and modify |
| 3768 | * i_disksize immediately, so that during the subsequent flushing of |
| 3769 | * dirty pages and freeing of disk blocks, we can guarantee that any |
| 3770 | * commit will leave the blocks being flushed in an unused state on |
| 3771 | * disk. (On recovery, the inode will get truncated and the blocks will |
| 3772 | * be freed, so we have a strong guarantee that no future commit will |
| 3773 | * leave these blocks visible to the user.) |
| 3774 | * |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3775 | * Another thing we have to assure is that if we are in ordered mode |
| 3776 | * and inode is still attached to the committing transaction, we must |
| 3777 | * we start writeout of all the dirty pages which are being truncated. |
| 3778 | * This way we are sure that all the data written in the previous |
| 3779 | * transaction are already on disk (truncate waits for pages under |
| 3780 | * writeback). |
| 3781 | * |
| 3782 | * Called with inode->i_mutex down. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3783 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3784 | int ext4_setattr(struct dentry *dentry, struct iattr *attr) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3785 | { |
| 3786 | struct inode *inode = dentry->d_inode; |
| 3787 | int error, rc = 0; |
Dmitry Monakhov | 3d287de | 2010-10-27 22:08:46 -0400 | [diff] [blame] | 3788 | int orphan = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3789 | const unsigned int ia_valid = attr->ia_valid; |
| 3790 | |
| 3791 | error = inode_change_ok(inode, attr); |
| 3792 | if (error) |
| 3793 | return error; |
| 3794 | |
Dmitry Monakhov | 1275562 | 2010-04-08 22:04:20 +0400 | [diff] [blame] | 3795 | if (is_quota_modification(inode, attr)) |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 3796 | dquot_initialize(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3797 | if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || |
| 3798 | (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { |
| 3799 | handle_t *handle; |
| 3800 | |
| 3801 | /* (user+group)*(old+new) structure, inode write (sb, |
| 3802 | * inode block, ? - but truncate inode update has it) */ |
Dmitry Monakhov | 5aca07e | 2009-12-08 22:42:15 -0500 | [diff] [blame] | 3803 | handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+ |
Dmitry Monakhov | 194074a | 2009-12-08 22:42:28 -0500 | [diff] [blame] | 3804 | EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3805 | if (IS_ERR(handle)) { |
| 3806 | error = PTR_ERR(handle); |
| 3807 | goto err_out; |
| 3808 | } |
Christoph Hellwig | b43fa82 | 2010-03-03 09:05:03 -0500 | [diff] [blame] | 3809 | error = dquot_transfer(inode, attr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3810 | if (error) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3811 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3812 | return error; |
| 3813 | } |
| 3814 | /* Update corresponding info in inode so that everything is in |
| 3815 | * one transaction */ |
| 3816 | if (attr->ia_valid & ATTR_UID) |
| 3817 | inode->i_uid = attr->ia_uid; |
| 3818 | if (attr->ia_valid & ATTR_GID) |
| 3819 | inode->i_gid = attr->ia_gid; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3820 | error = ext4_mark_inode_dirty(handle, inode); |
| 3821 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3822 | } |
| 3823 | |
Eric Sandeen | e2b4657 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3824 | if (attr->ia_valid & ATTR_SIZE) { |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 3825 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
Eric Sandeen | e2b4657 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3826 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 3827 | |
Theodore Ts'o | 0c095c7 | 2010-07-27 11:56:06 -0400 | [diff] [blame] | 3828 | if (attr->ia_size > sbi->s_bitmap_maxbytes) |
| 3829 | return -EFBIG; |
Eric Sandeen | e2b4657 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3830 | } |
| 3831 | } |
| 3832 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3833 | if (S_ISREG(inode->i_mode) && |
Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 3834 | attr->ia_valid & ATTR_SIZE && |
Theodore Ts'o | 072bd7e | 2011-05-23 15:13:02 -0400 | [diff] [blame] | 3835 | (attr->ia_size < inode->i_size)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3836 | handle_t *handle; |
| 3837 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3838 | handle = ext4_journal_start(inode, 3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3839 | if (IS_ERR(handle)) { |
| 3840 | error = PTR_ERR(handle); |
| 3841 | goto err_out; |
| 3842 | } |
Dmitry Monakhov | 3d287de | 2010-10-27 22:08:46 -0400 | [diff] [blame] | 3843 | if (ext4_handle_valid(handle)) { |
| 3844 | error = ext4_orphan_add(handle, inode); |
| 3845 | orphan = 1; |
| 3846 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3847 | EXT4_I(inode)->i_disksize = attr->ia_size; |
| 3848 | rc = ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3849 | if (!error) |
| 3850 | error = rc; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3851 | ext4_journal_stop(handle); |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3852 | |
| 3853 | if (ext4_should_order_data(inode)) { |
| 3854 | error = ext4_begin_ordered_truncate(inode, |
| 3855 | attr->ia_size); |
| 3856 | if (error) { |
| 3857 | /* Do as much error cleanup as possible */ |
| 3858 | handle = ext4_journal_start(inode, 3); |
| 3859 | if (IS_ERR(handle)) { |
| 3860 | ext4_orphan_del(NULL, inode); |
| 3861 | goto err_out; |
| 3862 | } |
| 3863 | ext4_orphan_del(handle, inode); |
Dmitry Monakhov | 3d287de | 2010-10-27 22:08:46 -0400 | [diff] [blame] | 3864 | orphan = 0; |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3865 | ext4_journal_stop(handle); |
| 3866 | goto err_out; |
| 3867 | } |
| 3868 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3869 | } |
| 3870 | |
Theodore Ts'o | 072bd7e | 2011-05-23 15:13:02 -0400 | [diff] [blame] | 3871 | if (attr->ia_valid & ATTR_SIZE) { |
| 3872 | if (attr->ia_size != i_size_read(inode)) { |
| 3873 | truncate_setsize(inode, attr->ia_size); |
| 3874 | ext4_truncate(inode); |
| 3875 | } else if (ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)) |
| 3876 | ext4_truncate(inode); |
| 3877 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3878 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3879 | if (!rc) { |
| 3880 | setattr_copy(inode, attr); |
| 3881 | mark_inode_dirty(inode); |
| 3882 | } |
| 3883 | |
| 3884 | /* |
| 3885 | * If the call to ext4_truncate failed to get a transaction handle at |
| 3886 | * all, we need to clean up the in-core orphan list manually. |
| 3887 | */ |
Dmitry Monakhov | 3d287de | 2010-10-27 22:08:46 -0400 | [diff] [blame] | 3888 | if (orphan && inode->i_nlink) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3889 | ext4_orphan_del(NULL, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3890 | |
| 3891 | if (!rc && (ia_valid & ATTR_MODE)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3892 | rc = ext4_acl_chmod(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3893 | |
| 3894 | err_out: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3895 | ext4_std_error(inode->i_sb, error); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3896 | if (!error) |
| 3897 | error = rc; |
| 3898 | return error; |
| 3899 | } |
| 3900 | |
Mingming Cao | 3e3398a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3901 | int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 3902 | struct kstat *stat) |
| 3903 | { |
| 3904 | struct inode *inode; |
| 3905 | unsigned long delalloc_blocks; |
| 3906 | |
| 3907 | inode = dentry->d_inode; |
| 3908 | generic_fillattr(inode, stat); |
| 3909 | |
| 3910 | /* |
| 3911 | * We can't update i_blocks if the block allocation is delayed |
| 3912 | * otherwise in the case of system crash before the real block |
| 3913 | * allocation is done, we will have i_blocks inconsistent with |
| 3914 | * on-disk file blocks. |
| 3915 | * We always keep i_blocks updated together with real |
| 3916 | * allocation. But to not confuse with user, stat |
| 3917 | * will return the blocks that include the delayed allocation |
| 3918 | * blocks for this file. |
| 3919 | */ |
Mingming Cao | 3e3398a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3920 | delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; |
Mingming Cao | 3e3398a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3921 | |
| 3922 | stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; |
| 3923 | return 0; |
| 3924 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3925 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3926 | static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) |
| 3927 | { |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 3928 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Amir Goldstein | 8bb2b24 | 2011-06-27 17:10:28 -0400 | [diff] [blame] | 3929 | return ext4_ind_trans_blocks(inode, nrblocks, chunk); |
Theodore Ts'o | ac51d83 | 2008-11-06 16:49:36 -0500 | [diff] [blame] | 3930 | return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3931 | } |
Theodore Ts'o | ac51d83 | 2008-11-06 16:49:36 -0500 | [diff] [blame] | 3932 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3933 | /* |
| 3934 | * Account for index blocks, block groups bitmaps and block group |
| 3935 | * descriptor blocks if modify datablocks and index blocks |
| 3936 | * worse case, the indexs blocks spread over different block groups |
| 3937 | * |
| 3938 | * If datablocks are discontiguous, they are possible to spread over |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 3939 | * different block groups too. If they are contiuguous, with flexbg, |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3940 | * they could still across block group boundary. |
| 3941 | * |
| 3942 | * Also account for superblock, inode, quota and xattr blocks |
| 3943 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3944 | static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3945 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 3946 | ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); |
| 3947 | int gdpblocks; |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3948 | int idxblocks; |
| 3949 | int ret = 0; |
| 3950 | |
| 3951 | /* |
| 3952 | * How many index blocks need to touch to modify nrblocks? |
| 3953 | * The "Chunk" flag indicating whether the nrblocks is |
| 3954 | * physically contiguous on disk |
| 3955 | * |
| 3956 | * For Direct IO and fallocate, they calls get_block to allocate |
| 3957 | * one single extent at a time, so they could set the "Chunk" flag |
| 3958 | */ |
| 3959 | idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); |
| 3960 | |
| 3961 | ret = idxblocks; |
| 3962 | |
| 3963 | /* |
| 3964 | * Now let's see how many group bitmaps and group descriptors need |
| 3965 | * to account |
| 3966 | */ |
| 3967 | groups = idxblocks; |
| 3968 | if (chunk) |
| 3969 | groups += 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3970 | else |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3971 | groups += nrblocks; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3972 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3973 | gdpblocks = groups; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 3974 | if (groups > ngroups) |
| 3975 | groups = ngroups; |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3976 | if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) |
| 3977 | gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; |
| 3978 | |
| 3979 | /* bitmaps and block group descriptor blocks */ |
| 3980 | ret += groups + gdpblocks; |
| 3981 | |
| 3982 | /* Blocks for super block, inode, quota and xattr blocks */ |
| 3983 | ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3984 | |
| 3985 | return ret; |
| 3986 | } |
| 3987 | |
| 3988 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3989 | * Calculate the total number of credits to reserve to fit |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 3990 | * the modification of a single pages into a single transaction, |
| 3991 | * which may include multiple chunks of block allocations. |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3992 | * |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 3993 | * This could be called via ext4_write_begin() |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3994 | * |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 3995 | * We need to consider the worse case, when |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3996 | * one new block per extent. |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 3997 | */ |
| 3998 | int ext4_writepage_trans_blocks(struct inode *inode) |
| 3999 | { |
| 4000 | int bpp = ext4_journal_blocks_per_page(inode); |
| 4001 | int ret; |
| 4002 | |
| 4003 | ret = ext4_meta_trans_blocks(inode, bpp, 0); |
| 4004 | |
| 4005 | /* Account for data blocks for journalled mode */ |
| 4006 | if (ext4_should_journal_data(inode)) |
| 4007 | ret += bpp; |
| 4008 | return ret; |
| 4009 | } |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 4010 | |
| 4011 | /* |
| 4012 | * Calculate the journal credits for a chunk of data modification. |
| 4013 | * |
| 4014 | * This is called from DIO, fallocate or whoever calling |
Eric Sandeen | 79e8303 | 2010-07-27 11:56:07 -0400 | [diff] [blame] | 4015 | * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 4016 | * |
| 4017 | * journal buffers for data blocks are not included here, as DIO |
| 4018 | * and fallocate do no need to journal data buffers. |
| 4019 | */ |
| 4020 | int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) |
| 4021 | { |
| 4022 | return ext4_meta_trans_blocks(inode, nrblocks, 1); |
| 4023 | } |
| 4024 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4025 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4026 | * The caller must have previously called ext4_reserve_inode_write(). |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4027 | * Give this, we know that the caller already has write access to iloc->bh. |
| 4028 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4029 | int ext4_mark_iloc_dirty(handle_t *handle, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4030 | struct inode *inode, struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4031 | { |
| 4032 | int err = 0; |
| 4033 | |
Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4034 | if (test_opt(inode->i_sb, I_VERSION)) |
| 4035 | inode_inc_iversion(inode); |
| 4036 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4037 | /* the do_update_inode consumes one bh->b_count */ |
| 4038 | get_bh(iloc->bh); |
| 4039 | |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 4040 | /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ |
Frank Mayhar | 830156c | 2009-09-29 10:07:47 -0400 | [diff] [blame] | 4041 | err = ext4_do_update_inode(handle, inode, iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4042 | put_bh(iloc->bh); |
| 4043 | return err; |
| 4044 | } |
| 4045 | |
| 4046 | /* |
| 4047 | * On success, We end up with an outstanding reference count against |
| 4048 | * iloc->bh. This _must_ be cleaned up later. |
| 4049 | */ |
| 4050 | |
| 4051 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4052 | ext4_reserve_inode_write(handle_t *handle, struct inode *inode, |
| 4053 | struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4054 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4055 | int err; |
| 4056 | |
| 4057 | err = ext4_get_inode_loc(inode, iloc); |
| 4058 | if (!err) { |
| 4059 | BUFFER_TRACE(iloc->bh, "get_write_access"); |
| 4060 | err = ext4_journal_get_write_access(handle, iloc->bh); |
| 4061 | if (err) { |
| 4062 | brelse(iloc->bh); |
| 4063 | iloc->bh = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4064 | } |
| 4065 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4066 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4067 | return err; |
| 4068 | } |
| 4069 | |
| 4070 | /* |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4071 | * Expand an inode by new_extra_isize bytes. |
| 4072 | * Returns 0 on success or negative error number on failure. |
| 4073 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4074 | static int ext4_expand_extra_isize(struct inode *inode, |
| 4075 | unsigned int new_extra_isize, |
| 4076 | struct ext4_iloc iloc, |
| 4077 | handle_t *handle) |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4078 | { |
| 4079 | struct ext4_inode *raw_inode; |
| 4080 | struct ext4_xattr_ibody_header *header; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4081 | |
| 4082 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) |
| 4083 | return 0; |
| 4084 | |
| 4085 | raw_inode = ext4_raw_inode(&iloc); |
| 4086 | |
| 4087 | header = IHDR(inode, raw_inode); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4088 | |
| 4089 | /* No extended attributes present */ |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 4090 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || |
| 4091 | header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4092 | memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, |
| 4093 | new_extra_isize); |
| 4094 | EXT4_I(inode)->i_extra_isize = new_extra_isize; |
| 4095 | return 0; |
| 4096 | } |
| 4097 | |
| 4098 | /* try to expand with EAs present */ |
| 4099 | return ext4_expand_extra_isize_ea(inode, new_extra_isize, |
| 4100 | raw_inode, handle); |
| 4101 | } |
| 4102 | |
| 4103 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4104 | * What we do here is to mark the in-core inode as clean with respect to inode |
| 4105 | * dirtiness (it may still be data-dirty). |
| 4106 | * This means that the in-core inode may be reaped by prune_icache |
| 4107 | * without having to perform any I/O. This is a very good thing, |
| 4108 | * because *any* task may call prune_icache - even ones which |
| 4109 | * have a transaction open against a different journal. |
| 4110 | * |
| 4111 | * Is this cheating? Not really. Sure, we haven't written the |
| 4112 | * inode out, but prune_icache isn't a user-visible syncing function. |
| 4113 | * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) |
| 4114 | * we start and wait on commits. |
| 4115 | * |
| 4116 | * Is this efficient/effective? Well, we're being nice to the system |
| 4117 | * by cleaning up our inodes proactively so they can be reaped |
| 4118 | * without I/O. But we are potentially leaving up to five seconds' |
| 4119 | * worth of inodes floating about which prune_icache wants us to |
| 4120 | * write out. One way to fix that would be to get prune_icache() |
| 4121 | * to do a write_super() to free up some memory. It has the desired |
| 4122 | * effect. |
| 4123 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4124 | int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4125 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4126 | struct ext4_iloc iloc; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4127 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 4128 | static unsigned int mnt_count; |
| 4129 | int err, ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4130 | |
| 4131 | might_sleep(); |
Theodore Ts'o | 7ff9c07 | 2010-11-08 13:51:33 -0500 | [diff] [blame] | 4132 | trace_ext4_mark_inode_dirty(inode, _RET_IP_); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4133 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4134 | if (ext4_handle_valid(handle) && |
| 4135 | EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 4136 | !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4137 | /* |
| 4138 | * We need extra buffer credits since we may write into EA block |
| 4139 | * with this same handle. If journal_extend fails, then it will |
| 4140 | * only result in a minor loss of functionality for that inode. |
| 4141 | * If this is felt to be critical, then e2fsck should be run to |
| 4142 | * force a large enough s_min_extra_isize. |
| 4143 | */ |
| 4144 | if ((jbd2_journal_extend(handle, |
| 4145 | EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { |
| 4146 | ret = ext4_expand_extra_isize(inode, |
| 4147 | sbi->s_want_extra_isize, |
| 4148 | iloc, handle); |
| 4149 | if (ret) { |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 4150 | ext4_set_inode_state(inode, |
| 4151 | EXT4_STATE_NO_EXPAND); |
Aneesh Kumar K.V | c1bddad | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 4152 | if (mnt_count != |
| 4153 | le16_to_cpu(sbi->s_es->s_mnt_count)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 4154 | ext4_warning(inode->i_sb, |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4155 | "Unable to expand inode %lu. Delete" |
| 4156 | " some EAs or run e2fsck.", |
| 4157 | inode->i_ino); |
Aneesh Kumar K.V | c1bddad | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 4158 | mnt_count = |
| 4159 | le16_to_cpu(sbi->s_es->s_mnt_count); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4160 | } |
| 4161 | } |
| 4162 | } |
| 4163 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4164 | if (!err) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4165 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4166 | return err; |
| 4167 | } |
| 4168 | |
| 4169 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4170 | * ext4_dirty_inode() is called from __mark_inode_dirty() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4171 | * |
| 4172 | * We're really interested in the case where a file is being extended. |
| 4173 | * i_size has been changed by generic_commit_write() and we thus need |
| 4174 | * to include the updated inode in the current transaction. |
| 4175 | * |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 4176 | * Also, dquot_alloc_block() will always dirty the inode when blocks |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4177 | * are allocated to the file. |
| 4178 | * |
| 4179 | * If the inode is marked synchronous, we don't honour that here - doing |
| 4180 | * so would cause a commit on atime updates, which we don't bother doing. |
| 4181 | * We handle synchronous inodes at the highest possible level. |
| 4182 | */ |
Christoph Hellwig | aa38572 | 2011-05-27 06:53:02 -0400 | [diff] [blame] | 4183 | void ext4_dirty_inode(struct inode *inode, int flags) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4184 | { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4185 | handle_t *handle; |
| 4186 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4187 | handle = ext4_journal_start(inode, 2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4188 | if (IS_ERR(handle)) |
| 4189 | goto out; |
Curt Wohlgemuth | f3dc272 | 2009-09-29 16:06:01 -0400 | [diff] [blame] | 4190 | |
Curt Wohlgemuth | f3dc272 | 2009-09-29 16:06:01 -0400 | [diff] [blame] | 4191 | ext4_mark_inode_dirty(handle, inode); |
| 4192 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4193 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4194 | out: |
| 4195 | return; |
| 4196 | } |
| 4197 | |
| 4198 | #if 0 |
| 4199 | /* |
| 4200 | * Bind an inode's backing buffer_head into this transaction, to prevent |
| 4201 | * it from being flushed to disk early. Unlike |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4202 | * ext4_reserve_inode_write, this leaves behind no bh reference and |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4203 | * returns no iloc structure, so the caller needs to repeat the iloc |
| 4204 | * lookup to mark the inode dirty later. |
| 4205 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4206 | static int ext4_pin_inode(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4207 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4208 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4209 | |
| 4210 | int err = 0; |
| 4211 | if (handle) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4212 | err = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4213 | if (!err) { |
| 4214 | BUFFER_TRACE(iloc.bh, "get_write_access"); |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 4215 | err = jbd2_journal_get_write_access(handle, iloc.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4216 | if (!err) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4217 | err = ext4_handle_dirty_metadata(handle, |
Curt Wohlgemuth | 73b50c1 | 2010-02-16 15:06:29 -0500 | [diff] [blame] | 4218 | NULL, |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4219 | iloc.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4220 | brelse(iloc.bh); |
| 4221 | } |
| 4222 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4223 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4224 | return err; |
| 4225 | } |
| 4226 | #endif |
| 4227 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4228 | int ext4_change_inode_journal_flag(struct inode *inode, int val) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4229 | { |
| 4230 | journal_t *journal; |
| 4231 | handle_t *handle; |
| 4232 | int err; |
| 4233 | |
| 4234 | /* |
| 4235 | * We have to be very careful here: changing a data block's |
| 4236 | * journaling status dynamically is dangerous. If we write a |
| 4237 | * data block to the journal, change the status and then delete |
| 4238 | * that block, we risk forgetting to revoke the old log record |
| 4239 | * from the journal and so a subsequent replay can corrupt data. |
| 4240 | * So, first we make sure that the journal is empty and that |
| 4241 | * nobody is changing anything. |
| 4242 | */ |
| 4243 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4244 | journal = EXT4_JOURNAL(inode); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4245 | if (!journal) |
| 4246 | return 0; |
Dave Hansen | d699594 | 2007-07-18 08:33:51 -0400 | [diff] [blame] | 4247 | if (is_journal_aborted(journal)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4248 | return -EROFS; |
| 4249 | |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 4250 | jbd2_journal_lock_updates(journal); |
| 4251 | jbd2_journal_flush(journal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4252 | |
| 4253 | /* |
| 4254 | * OK, there are no updates running now, and all cached data is |
| 4255 | * synced to disk. We are now in a completely consistent state |
| 4256 | * which doesn't have anything in the journal, and we know that |
| 4257 | * no filesystem updates are running, so it is safe to modify |
| 4258 | * the inode's in-core data-journaling state flag now. |
| 4259 | */ |
| 4260 | |
| 4261 | if (val) |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 4262 | ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4263 | else |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 4264 | ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4265 | ext4_set_aops(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4266 | |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 4267 | jbd2_journal_unlock_updates(journal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4268 | |
| 4269 | /* Finally we can mark the inode as dirty. */ |
| 4270 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4271 | handle = ext4_journal_start(inode, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4272 | if (IS_ERR(handle)) |
| 4273 | return PTR_ERR(handle); |
| 4274 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4275 | err = ext4_mark_inode_dirty(handle, inode); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4276 | ext4_handle_sync(handle); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4277 | ext4_journal_stop(handle); |
| 4278 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4279 | |
| 4280 | return err; |
| 4281 | } |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4282 | |
| 4283 | static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) |
| 4284 | { |
| 4285 | return !buffer_mapped(bh); |
| 4286 | } |
| 4287 | |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 4288 | int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4289 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 4290 | struct page *page = vmf->page; |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4291 | loff_t size; |
| 4292 | unsigned long len; |
| 4293 | int ret = -EINVAL; |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 4294 | void *fsdata; |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4295 | struct file *file = vma->vm_file; |
| 4296 | struct inode *inode = file->f_path.dentry->d_inode; |
| 4297 | struct address_space *mapping = inode->i_mapping; |
| 4298 | |
| 4299 | /* |
| 4300 | * Get i_alloc_sem to stop truncates messing with the inode. We cannot |
| 4301 | * get i_mutex because we are already holding mmap_sem. |
| 4302 | */ |
| 4303 | down_read(&inode->i_alloc_sem); |
| 4304 | size = i_size_read(inode); |
| 4305 | if (page->mapping != mapping || size <= page_offset(page) |
| 4306 | || !PageUptodate(page)) { |
| 4307 | /* page got truncated from under us? */ |
| 4308 | goto out_unlock; |
| 4309 | } |
| 4310 | ret = 0; |
Darrick J. Wong | 0e49989 | 2011-05-18 13:55:20 -0400 | [diff] [blame] | 4311 | |
| 4312 | lock_page(page); |
| 4313 | wait_on_page_writeback(page); |
| 4314 | if (PageMappedToDisk(page)) { |
| 4315 | up_read(&inode->i_alloc_sem); |
| 4316 | return VM_FAULT_LOCKED; |
| 4317 | } |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4318 | |
| 4319 | if (page->index == size >> PAGE_CACHE_SHIFT) |
| 4320 | len = size & ~PAGE_CACHE_MASK; |
| 4321 | else |
| 4322 | len = PAGE_CACHE_SIZE; |
| 4323 | |
Aneesh Kumar K.V | a827eaf | 2009-09-09 22:36:03 -0400 | [diff] [blame] | 4324 | /* |
| 4325 | * return if we have all the buffers mapped. This avoid |
| 4326 | * the need to call write_begin/write_end which does a |
| 4327 | * journal_start/journal_stop which can block and take |
| 4328 | * long time |
| 4329 | */ |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4330 | if (page_has_buffers(page)) { |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4331 | if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, |
Aneesh Kumar K.V | a827eaf | 2009-09-09 22:36:03 -0400 | [diff] [blame] | 4332 | ext4_bh_unmapped)) { |
Darrick J. Wong | 0e49989 | 2011-05-18 13:55:20 -0400 | [diff] [blame] | 4333 | up_read(&inode->i_alloc_sem); |
| 4334 | return VM_FAULT_LOCKED; |
Aneesh Kumar K.V | a827eaf | 2009-09-09 22:36:03 -0400 | [diff] [blame] | 4335 | } |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4336 | } |
Aneesh Kumar K.V | a827eaf | 2009-09-09 22:36:03 -0400 | [diff] [blame] | 4337 | unlock_page(page); |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4338 | /* |
| 4339 | * OK, we need to fill the hole... Do write_begin write_end |
| 4340 | * to do block allocation/reservation.We are not holding |
| 4341 | * inode.i__mutex here. That allow * parallel write_begin, |
| 4342 | * write_end call. lock_page prevent this from happening |
| 4343 | * on the same page though |
| 4344 | */ |
| 4345 | ret = mapping->a_ops->write_begin(file, mapping, page_offset(page), |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 4346 | len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4347 | if (ret < 0) |
| 4348 | goto out_unlock; |
| 4349 | ret = mapping->a_ops->write_end(file, mapping, page_offset(page), |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 4350 | len, len, page, fsdata); |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4351 | if (ret < 0) |
| 4352 | goto out_unlock; |
| 4353 | ret = 0; |
Darrick J. Wong | 0e49989 | 2011-05-18 13:55:20 -0400 | [diff] [blame] | 4354 | |
| 4355 | /* |
| 4356 | * write_begin/end might have created a dirty page and someone |
| 4357 | * could wander in and start the IO. Make sure that hasn't |
| 4358 | * happened. |
| 4359 | */ |
| 4360 | lock_page(page); |
| 4361 | wait_on_page_writeback(page); |
| 4362 | up_read(&inode->i_alloc_sem); |
| 4363 | return VM_FAULT_LOCKED; |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4364 | out_unlock: |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 4365 | if (ret) |
| 4366 | ret = VM_FAULT_SIGBUS; |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4367 | up_read(&inode->i_alloc_sem); |
| 4368 | return ret; |
| 4369 | } |