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 | * |
| 15 | * Goal-directed block allocation by Stephen Tweedie |
| 16 | * (sct@redhat.com), 1993, 1998 |
| 17 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 18 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 19 | * 64-bit file support on 64-bit platforms by Jakub Jelinek |
| 20 | * (jj@sunsite.ms.mff.cuni.cz) |
| 21 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 22 | * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/time.h> |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 28 | #include <linux/jbd2.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 29 | #include <linux/highuid.h> |
| 30 | #include <linux/pagemap.h> |
| 31 | #include <linux/quotaops.h> |
| 32 | #include <linux/string.h> |
| 33 | #include <linux/buffer_head.h> |
| 34 | #include <linux/writeback.h> |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 35 | #include <linux/pagevec.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 36 | #include <linux/mpage.h> |
Duane Griffin | e83c139 | 2008-12-19 20:47:15 +0000 | [diff] [blame] | 37 | #include <linux/namei.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 38 | #include <linux/uio.h> |
| 39 | #include <linux/bio.h> |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 40 | #include <linux/workqueue.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" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 46 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 47 | #include <trace/events/ext4.h> |
| 48 | |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 49 | #define MPAGE_DA_EXTENT_TAIL 0x01 |
| 50 | |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 51 | static inline int ext4_begin_ordered_truncate(struct inode *inode, |
| 52 | loff_t new_size) |
| 53 | { |
Jan Kara | 7f5aa21 | 2009-02-10 11:15:34 -0500 | [diff] [blame] | 54 | return jbd2_journal_begin_ordered_truncate( |
| 55 | EXT4_SB(inode->i_sb)->s_journal, |
| 56 | &EXT4_I(inode)->jinode, |
| 57 | new_size); |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 60 | static void ext4_invalidatepage(struct page *page, unsigned long offset); |
| 61 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 62 | /* |
| 63 | * Test whether an inode is a fast symlink. |
| 64 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 65 | static int ext4_inode_is_fast_symlink(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 66 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 67 | int ea_blocks = EXT4_I(inode)->i_file_acl ? |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 68 | (inode->i_sb->s_blocksize >> 9) : 0; |
| 69 | |
| 70 | return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); |
| 71 | } |
| 72 | |
| 73 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 74 | * The ext4 forget function must perform a revoke if we are freeing data |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 75 | * which has been journaled. Metadata (eg. indirect blocks) must be |
| 76 | * revoked in all cases. |
| 77 | * |
| 78 | * "bh" may be NULL: a metadata block may have been freed from memory |
| 79 | * but there may still be a record of it in the journal, and that record |
| 80 | * still needs to be revoked. |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 81 | * |
Curt Wohlgemuth | e6b5d30 | 2009-07-13 09:07:20 -0400 | [diff] [blame] | 82 | * If the handle isn't valid we're not journaling, but we still need to |
| 83 | * call into ext4_journal_revoke() to put the buffer head. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 84 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 85 | int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 86 | struct buffer_head *bh, ext4_fsblk_t blocknr) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 87 | { |
| 88 | int err; |
| 89 | |
| 90 | might_sleep(); |
| 91 | |
| 92 | BUFFER_TRACE(bh, "enter"); |
| 93 | |
| 94 | jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, " |
Theodore Ts'o | 7f4520c | 2009-06-13 10:09:41 -0400 | [diff] [blame] | 95 | "data mode %x\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 96 | bh, is_metadata, inode->i_mode, |
| 97 | test_opt(inode->i_sb, DATA_FLAGS)); |
| 98 | |
| 99 | /* Never use the revoke function if we are doing full data |
| 100 | * journaling: there is no need to, and a V1 superblock won't |
| 101 | * support it. Otherwise, only skip the revoke on un-journaled |
| 102 | * data blocks. */ |
| 103 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 104 | if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || |
| 105 | (!is_metadata && !ext4_should_journal_data(inode))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 106 | if (bh) { |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 107 | BUFFER_TRACE(bh, "call jbd2_journal_forget"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 108 | return ext4_journal_forget(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * data!=journal && (is_metadata || should_journal_data(inode)) |
| 115 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 116 | BUFFER_TRACE(bh, "call ext4_journal_revoke"); |
| 117 | err = ext4_journal_revoke(handle, blocknr, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 118 | if (err) |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 119 | ext4_abort(inode->i_sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 120 | "error %d when attempting revoke", err); |
| 121 | BUFFER_TRACE(bh, "exit"); |
| 122 | return err; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Work out how many blocks we need to proceed with the next chunk of a |
| 127 | * truncate transaction. |
| 128 | */ |
| 129 | static unsigned long blocks_for_truncate(struct inode *inode) |
| 130 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 131 | ext4_lblk_t needed; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 132 | |
| 133 | needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); |
| 134 | |
| 135 | /* Give ourselves just enough room to cope with inodes in which |
| 136 | * i_blocks is corrupt: we've seen disk corruptions in the past |
| 137 | * which resulted in random data in an inode which looked enough |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 138 | * like a regular file for ext4 to try to delete it. Things |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 139 | * will go a bit crazy if that happens, but at least we should |
| 140 | * try not to panic the whole kernel. */ |
| 141 | if (needed < 2) |
| 142 | needed = 2; |
| 143 | |
| 144 | /* But we need to bound the transaction so we don't overflow the |
| 145 | * journal. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 146 | if (needed > EXT4_MAX_TRANS_DATA) |
| 147 | needed = EXT4_MAX_TRANS_DATA; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 148 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 149 | return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Truncate transactions can be complex and absolutely huge. So we need to |
| 154 | * be able to restart the transaction at a conventient checkpoint to make |
| 155 | * sure we don't overflow the journal. |
| 156 | * |
| 157 | * start_transaction gets us a new handle for a truncate transaction, |
| 158 | * and extend_transaction tries to extend the existing one a bit. If |
| 159 | * extend fails, we need to propagate the failure up and restart the |
| 160 | * transaction in the top-level truncate loop. --sct |
| 161 | */ |
| 162 | static handle_t *start_transaction(struct inode *inode) |
| 163 | { |
| 164 | handle_t *result; |
| 165 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 166 | result = ext4_journal_start(inode, blocks_for_truncate(inode)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 167 | if (!IS_ERR(result)) |
| 168 | return result; |
| 169 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 170 | ext4_std_error(inode->i_sb, PTR_ERR(result)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 171 | return result; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Try to extend this transaction for the purposes of truncation. |
| 176 | * |
| 177 | * Returns 0 if we managed to create more room. If we can't create more |
| 178 | * room, and the transaction must be restarted we return 1. |
| 179 | */ |
| 180 | static int try_to_extend_transaction(handle_t *handle, struct inode *inode) |
| 181 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 182 | if (!ext4_handle_valid(handle)) |
| 183 | return 0; |
| 184 | if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 185 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 186 | if (!ext4_journal_extend(handle, blocks_for_truncate(inode))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 187 | return 0; |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Restart the transaction associated with *handle. This does a commit, |
| 193 | * so before we call here everything must be consistently dirtied against |
| 194 | * this transaction. |
| 195 | */ |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 196 | int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, |
| 197 | int nblocks) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 198 | { |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 199 | int ret; |
| 200 | |
| 201 | /* |
| 202 | * Drop i_data_sem to avoid deadlock with ext4_get_blocks At this |
| 203 | * moment, get_block can be called only for blocks inside i_size since |
| 204 | * page cache has been already dropped and writes are blocked by |
| 205 | * i_mutex. So we can safely drop the i_data_sem here. |
| 206 | */ |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 207 | BUG_ON(EXT4_JOURNAL(inode) == NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 208 | jbd_debug(2, "restarting handle %p\n", handle); |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 209 | up_write(&EXT4_I(inode)->i_data_sem); |
| 210 | ret = ext4_journal_restart(handle, blocks_for_truncate(inode)); |
| 211 | down_write(&EXT4_I(inode)->i_data_sem); |
| 212 | |
| 213 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Called at the last iput() if i_nlink is zero. |
| 218 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 219 | void ext4_delete_inode(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 220 | { |
| 221 | handle_t *handle; |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 222 | int err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 223 | |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 224 | if (ext4_should_order_data(inode)) |
| 225 | ext4_begin_ordered_truncate(inode, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 226 | truncate_inode_pages(&inode->i_data, 0); |
| 227 | |
| 228 | if (is_bad_inode(inode)) |
| 229 | goto no_delete; |
| 230 | |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 231 | handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 232 | if (IS_ERR(handle)) { |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 233 | ext4_std_error(inode->i_sb, PTR_ERR(handle)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 234 | /* |
| 235 | * If we're going to skip the normal cleanup, we still need to |
| 236 | * make sure that the in-core orphan linked list is properly |
| 237 | * cleaned up. |
| 238 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 239 | ext4_orphan_del(NULL, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 240 | goto no_delete; |
| 241 | } |
| 242 | |
| 243 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 244 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 245 | inode->i_size = 0; |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 246 | err = ext4_mark_inode_dirty(handle, inode); |
| 247 | if (err) { |
| 248 | ext4_warning(inode->i_sb, __func__, |
| 249 | "couldn't mark inode dirty (err %d)", err); |
| 250 | goto stop_handle; |
| 251 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 252 | if (inode->i_blocks) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 253 | ext4_truncate(inode); |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 254 | |
| 255 | /* |
| 256 | * ext4_ext_truncate() doesn't reserve any slop when it |
| 257 | * restarts journal transactions; therefore there may not be |
| 258 | * enough credits left in the handle to remove the inode from |
| 259 | * the orphan list and set the dtime field. |
| 260 | */ |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 261 | if (!ext4_handle_has_enough_credits(handle, 3)) { |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 262 | err = ext4_journal_extend(handle, 3); |
| 263 | if (err > 0) |
| 264 | err = ext4_journal_restart(handle, 3); |
| 265 | if (err != 0) { |
| 266 | ext4_warning(inode->i_sb, __func__, |
| 267 | "couldn't extend journal (err %d)", err); |
| 268 | stop_handle: |
| 269 | ext4_journal_stop(handle); |
| 270 | goto no_delete; |
| 271 | } |
| 272 | } |
| 273 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 274 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 275 | * Kill off the orphan record which ext4_truncate created. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 276 | * AKPM: I think this can be inside the above `if'. |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 277 | * 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] | 278 | * deletion of a non-existent orphan - this is because we don't |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 279 | * know if ext4_truncate() actually created an orphan record. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 280 | * (Well, we could do this if we need to, but heck - it works) |
| 281 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 282 | ext4_orphan_del(handle, inode); |
| 283 | EXT4_I(inode)->i_dtime = get_seconds(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | * One subtle ordering requirement: if anything has gone wrong |
| 287 | * (transaction abort, IO errors, whatever), then we can still |
| 288 | * do these next steps (the fs will already have been marked as |
| 289 | * having errors), but we can't free the inode if the mark_dirty |
| 290 | * fails. |
| 291 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 292 | if (ext4_mark_inode_dirty(handle, inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 293 | /* If that failed, just do the required in-core inode clear. */ |
| 294 | clear_inode(inode); |
| 295 | else |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 296 | ext4_free_inode(handle, inode); |
| 297 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 298 | return; |
| 299 | no_delete: |
| 300 | clear_inode(inode); /* We must guarantee clearing of inode... */ |
| 301 | } |
| 302 | |
| 303 | typedef struct { |
| 304 | __le32 *p; |
| 305 | __le32 key; |
| 306 | struct buffer_head *bh; |
| 307 | } Indirect; |
| 308 | |
| 309 | static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) |
| 310 | { |
| 311 | p->key = *(p->p = v); |
| 312 | p->bh = bh; |
| 313 | } |
| 314 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 315 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 316 | * ext4_block_to_path - parse the block number into array of offsets |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 317 | * @inode: inode in question (we are only interested in its superblock) |
| 318 | * @i_block: block number to be parsed |
| 319 | * @offsets: array to store the offsets in |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 320 | * @boundary: set this non-zero if the referred-to block is likely to be |
| 321 | * followed (on disk) by an indirect block. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 322 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 323 | * To store the locations of file's data ext4 uses a data structure common |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 324 | * for UNIX filesystems - tree of pointers anchored in the inode, with |
| 325 | * data blocks at leaves and indirect blocks in intermediate nodes. |
| 326 | * This function translates the block number into path in that tree - |
| 327 | * return value is the path length and @offsets[n] is the offset of |
| 328 | * pointer to (n+1)th node in the nth one. If @block is out of range |
| 329 | * (negative or too large) warning is printed and zero returned. |
| 330 | * |
| 331 | * Note: function doesn't find node addresses, so no IO is needed. All |
| 332 | * we need to know is the capacity of indirect blocks (taken from the |
| 333 | * inode->i_sb). |
| 334 | */ |
| 335 | |
| 336 | /* |
| 337 | * Portability note: the last comparison (check that we fit into triple |
| 338 | * indirect block) is spelled differently, because otherwise on an |
| 339 | * architecture with 32-bit longs and 8Kb pages we might get into trouble |
| 340 | * if our filesystem had 8Kb blocks. We might use long long, but that would |
| 341 | * kill us on x86. Oh, well, at least the sign propagation does not matter - |
| 342 | * i_block would have to be negative in the very beginning, so we would not |
| 343 | * get there at all. |
| 344 | */ |
| 345 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 346 | static int ext4_block_to_path(struct inode *inode, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 347 | ext4_lblk_t i_block, |
| 348 | ext4_lblk_t offsets[4], int *boundary) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 349 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 350 | int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
| 351 | int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); |
| 352 | const long direct_blocks = EXT4_NDIR_BLOCKS, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 353 | indirect_blocks = ptrs, |
| 354 | double_blocks = (1 << (ptrs_bits * 2)); |
| 355 | int n = 0; |
| 356 | int final = 0; |
| 357 | |
Roel Kluin | c333e07 | 2009-08-10 22:47:22 -0400 | [diff] [blame] | 358 | if (i_block < direct_blocks) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 359 | offsets[n++] = i_block; |
| 360 | final = direct_blocks; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 361 | } else if ((i_block -= direct_blocks) < indirect_blocks) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 362 | offsets[n++] = EXT4_IND_BLOCK; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 363 | offsets[n++] = i_block; |
| 364 | final = ptrs; |
| 365 | } else if ((i_block -= indirect_blocks) < double_blocks) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 366 | offsets[n++] = EXT4_DIND_BLOCK; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 367 | offsets[n++] = i_block >> ptrs_bits; |
| 368 | offsets[n++] = i_block & (ptrs - 1); |
| 369 | final = ptrs; |
| 370 | } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 371 | offsets[n++] = EXT4_TIND_BLOCK; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 372 | offsets[n++] = i_block >> (ptrs_bits * 2); |
| 373 | offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); |
| 374 | offsets[n++] = i_block & (ptrs - 1); |
| 375 | final = ptrs; |
| 376 | } else { |
Eric Sandeen | e2b4657 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 377 | ext4_warning(inode->i_sb, "ext4_block_to_path", |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 378 | "block %lu > max in inode %lu", |
| 379 | i_block + direct_blocks + |
| 380 | indirect_blocks + double_blocks, inode->i_ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 381 | } |
| 382 | if (boundary) |
| 383 | *boundary = final - 1 - (i_block & (ptrs - 1)); |
| 384 | return n; |
| 385 | } |
| 386 | |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 387 | static int __ext4_check_blockref(const char *function, struct inode *inode, |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 388 | __le32 *p, unsigned int max) |
| 389 | { |
Thiemo Nagel | f73953c | 2009-04-07 18:46:47 -0400 | [diff] [blame] | 390 | __le32 *bref = p; |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 391 | unsigned int blk; |
| 392 | |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 393 | while (bref < p+max) { |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 394 | blk = le32_to_cpu(*bref++); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 395 | if (blk && |
| 396 | unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb), |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 397 | blk, 1))) { |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 398 | ext4_error(inode->i_sb, function, |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 399 | "invalid block reference %u " |
| 400 | "in inode #%lu", blk, inode->i_ino); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 401 | return -EIO; |
| 402 | } |
| 403 | } |
| 404 | return 0; |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
| 408 | #define ext4_check_indirect_blockref(inode, bh) \ |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 409 | __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \ |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 410 | EXT4_ADDR_PER_BLOCK((inode)->i_sb)) |
| 411 | |
| 412 | #define ext4_check_inode_blockref(inode) \ |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 413 | __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \ |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 414 | EXT4_NDIR_BLOCKS) |
| 415 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 416 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 417 | * ext4_get_branch - read the chain of indirect blocks leading to data |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 418 | * @inode: inode in question |
| 419 | * @depth: depth of the chain (1 - direct pointer, etc.) |
| 420 | * @offsets: offsets of pointers in inode/indirect blocks |
| 421 | * @chain: place to store the result |
| 422 | * @err: here we store the error value |
| 423 | * |
| 424 | * Function fills the array of triples <key, p, bh> and returns %NULL |
| 425 | * if everything went OK or the pointer to the last filled triple |
| 426 | * (incomplete one) otherwise. Upon the return chain[i].key contains |
| 427 | * the number of (i+1)-th block in the chain (as it is stored in memory, |
| 428 | * i.e. little-endian 32-bit), chain[i].p contains the address of that |
| 429 | * number (it points into struct inode for i==0 and into the bh->b_data |
| 430 | * for i>0) and chain[i].bh points to the buffer_head of i-th indirect |
| 431 | * block for i>0 and NULL for i==0. In other words, it holds the block |
| 432 | * numbers of the chain, addresses they were taken from (and where we can |
| 433 | * verify that chain did not change) and buffer_heads hosting these |
| 434 | * numbers. |
| 435 | * |
| 436 | * Function stops when it stumbles upon zero pointer (absent block) |
| 437 | * (pointer to last triple returned, *@err == 0) |
| 438 | * or when it gets an IO error reading an indirect block |
| 439 | * (ditto, *@err == -EIO) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 440 | * or when it reads all @depth-1 indirect blocks successfully and finds |
| 441 | * the whole chain, all way to the data (returns %NULL, *err == 0). |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 442 | * |
| 443 | * Need to be called with |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 444 | * down_read(&EXT4_I(inode)->i_data_sem) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 445 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 446 | static Indirect *ext4_get_branch(struct inode *inode, int depth, |
| 447 | ext4_lblk_t *offsets, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 448 | Indirect chain[4], int *err) |
| 449 | { |
| 450 | struct super_block *sb = inode->i_sb; |
| 451 | Indirect *p = chain; |
| 452 | struct buffer_head *bh; |
| 453 | |
| 454 | *err = 0; |
| 455 | /* i_data is not going away, no lock needed */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 456 | add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 457 | if (!p->key) |
| 458 | goto no_block; |
| 459 | while (--depth) { |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 460 | bh = sb_getblk(sb, le32_to_cpu(p->key)); |
| 461 | if (unlikely(!bh)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 462 | goto failure; |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 463 | |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 464 | if (!bh_uptodate_or_lock(bh)) { |
| 465 | if (bh_submit_read(bh) < 0) { |
| 466 | put_bh(bh); |
| 467 | goto failure; |
| 468 | } |
| 469 | /* validate block references */ |
| 470 | if (ext4_check_indirect_blockref(inode, bh)) { |
| 471 | put_bh(bh); |
| 472 | goto failure; |
| 473 | } |
| 474 | } |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 475 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 476 | add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 477 | /* Reader: end */ |
| 478 | if (!p->key) |
| 479 | goto no_block; |
| 480 | } |
| 481 | return NULL; |
| 482 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 483 | failure: |
| 484 | *err = -EIO; |
| 485 | no_block: |
| 486 | return p; |
| 487 | } |
| 488 | |
| 489 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 490 | * ext4_find_near - find a place for allocation with sufficient locality |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 491 | * @inode: owner |
| 492 | * @ind: descriptor of indirect block. |
| 493 | * |
Benoit Boissinot | 1cc8dcf | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 494 | * This function returns the preferred place for block allocation. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 495 | * It is used when heuristic for sequential allocation fails. |
| 496 | * Rules are: |
| 497 | * + if there is a block to the left of our position - allocate near it. |
| 498 | * + if pointer will live in indirect block - allocate near that block. |
| 499 | * + if pointer will live in inode - allocate in the same |
| 500 | * cylinder group. |
| 501 | * |
| 502 | * In the latter case we colour the starting block by the callers PID to |
| 503 | * prevent it from clashing with concurrent allocations for a different inode |
| 504 | * in the same block group. The PID is used here so that functionally related |
| 505 | * files will be close-by on-disk. |
| 506 | * |
| 507 | * Caller must make sure that @ind is valid and will stay that way. |
| 508 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 509 | static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 510 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 511 | struct ext4_inode_info *ei = EXT4_I(inode); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 512 | __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 513 | __le32 *p; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 514 | ext4_fsblk_t bg_start; |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 515 | ext4_fsblk_t last_block; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 516 | ext4_grpblk_t colour; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 517 | ext4_group_t block_group; |
| 518 | int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 519 | |
| 520 | /* Try to find previous block */ |
| 521 | for (p = ind->p - 1; p >= start; p--) { |
| 522 | if (*p) |
| 523 | return le32_to_cpu(*p); |
| 524 | } |
| 525 | |
| 526 | /* No such thing, so let's try location of indirect block */ |
| 527 | if (ind->bh) |
| 528 | return ind->bh->b_blocknr; |
| 529 | |
| 530 | /* |
| 531 | * It is going to be referred to from the inode itself? OK, just put it |
| 532 | * into the same cylinder group then. |
| 533 | */ |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 534 | block_group = ei->i_block_group; |
| 535 | if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { |
| 536 | block_group &= ~(flex_size-1); |
| 537 | if (S_ISREG(inode->i_mode)) |
| 538 | block_group++; |
| 539 | } |
| 540 | bg_start = ext4_group_first_block_no(inode->i_sb, block_group); |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 541 | last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; |
| 542 | |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 543 | /* |
| 544 | * If we are doing delayed allocation, we don't need take |
| 545 | * colour into account. |
| 546 | */ |
| 547 | if (test_opt(inode->i_sb, DELALLOC)) |
| 548 | return bg_start; |
| 549 | |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 550 | if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) |
| 551 | colour = (current->pid % 16) * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 552 | (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 553 | else |
| 554 | colour = (current->pid % 16) * ((last_block - bg_start) / 16); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 555 | return bg_start + colour; |
| 556 | } |
| 557 | |
| 558 | /** |
Benoit Boissinot | 1cc8dcf | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 559 | * ext4_find_goal - find a preferred place for allocation. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 560 | * @inode: owner |
| 561 | * @block: block we want |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 562 | * @partial: pointer to the last triple within a chain |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 563 | * |
Benoit Boissinot | 1cc8dcf | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 564 | * Normally this function find the preferred place for block allocation, |
Akinobu Mita | fb01bfd | 2008-02-06 01:40:16 -0800 | [diff] [blame] | 565 | * returns it. |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 566 | * Because this is only used for non-extent files, we limit the block nr |
| 567 | * to 32 bits. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 568 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 569 | static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 570 | Indirect *partial) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 571 | { |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 572 | ext4_fsblk_t goal; |
| 573 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 574 | /* |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 575 | * XXX need to get goal block from mballoc's data structures |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 576 | */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 577 | |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 578 | goal = ext4_find_near(inode, partial); |
| 579 | goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; |
| 580 | return goal; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 584 | * ext4_blks_to_allocate: Look up the block map and count the number |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 585 | * of direct blocks need to be allocated for the given branch. |
| 586 | * |
| 587 | * @branch: chain of indirect blocks |
| 588 | * @k: number of blocks need for indirect blocks |
| 589 | * @blks: number of data blocks to be mapped. |
| 590 | * @blocks_to_boundary: the offset in the indirect block |
| 591 | * |
| 592 | * return the total number of blocks to be allocate, including the |
| 593 | * direct and indirect blocks. |
| 594 | */ |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 595 | static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 596 | int blocks_to_boundary) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 597 | { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 598 | unsigned int count = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 599 | |
| 600 | /* |
| 601 | * Simple case, [t,d]Indirect block(s) has not allocated yet |
| 602 | * then it's clear blocks on that path have not allocated |
| 603 | */ |
| 604 | if (k > 0) { |
| 605 | /* right now we don't handle cross boundary allocation */ |
| 606 | if (blks < blocks_to_boundary + 1) |
| 607 | count += blks; |
| 608 | else |
| 609 | count += blocks_to_boundary + 1; |
| 610 | return count; |
| 611 | } |
| 612 | |
| 613 | count++; |
| 614 | while (count < blks && count <= blocks_to_boundary && |
| 615 | le32_to_cpu(*(branch[0].p + count)) == 0) { |
| 616 | count++; |
| 617 | } |
| 618 | return count; |
| 619 | } |
| 620 | |
| 621 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 622 | * ext4_alloc_blocks: multiple allocate blocks needed for a branch |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 623 | * @indirect_blks: the number of blocks need to allocate for indirect |
| 624 | * blocks |
| 625 | * |
| 626 | * @new_blocks: on return it will store the new block numbers for |
| 627 | * the indirect blocks(if needed) and the first direct block, |
| 628 | * @blks: on return it will store the total number of allocated |
| 629 | * direct blocks |
| 630 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 631 | static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 632 | ext4_lblk_t iblock, ext4_fsblk_t goal, |
| 633 | int indirect_blks, int blks, |
| 634 | ext4_fsblk_t new_blocks[4], int *err) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 635 | { |
Theodore Ts'o | 815a113 | 2009-01-01 23:59:43 -0500 | [diff] [blame] | 636 | struct ext4_allocation_request ar; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 637 | int target, i; |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 638 | unsigned long count = 0, blk_allocated = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 639 | int index = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 640 | ext4_fsblk_t current_block = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 641 | int ret = 0; |
| 642 | |
| 643 | /* |
| 644 | * Here we try to allocate the requested multiple blocks at once, |
| 645 | * on a best-effort basis. |
| 646 | * To build a branch, we should allocate blocks for |
| 647 | * the indirect blocks(if not allocated yet), and at least |
| 648 | * the first direct block of this branch. That's the |
| 649 | * minimum number of blocks need to allocate(required) |
| 650 | */ |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 651 | /* first we try to allocate the indirect blocks */ |
| 652 | target = indirect_blks; |
| 653 | while (target > 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 654 | count = target; |
| 655 | /* allocating blocks for indirect blocks and direct blocks */ |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 656 | current_block = ext4_new_meta_blocks(handle, inode, |
| 657 | goal, &count, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 658 | if (*err) |
| 659 | goto failed_out; |
| 660 | |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 661 | BUG_ON(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS); |
| 662 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 663 | target -= count; |
| 664 | /* allocate blocks for indirect blocks */ |
| 665 | while (index < indirect_blks && count) { |
| 666 | new_blocks[index++] = current_block++; |
| 667 | count--; |
| 668 | } |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 669 | if (count > 0) { |
| 670 | /* |
| 671 | * save the new block number |
| 672 | * for the first direct block |
| 673 | */ |
| 674 | new_blocks[index] = current_block; |
| 675 | printk(KERN_INFO "%s returned more blocks than " |
| 676 | "requested\n", __func__); |
| 677 | WARN_ON(1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 678 | break; |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 679 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 682 | target = blks - count ; |
| 683 | blk_allocated = count; |
| 684 | if (!target) |
| 685 | goto allocated; |
| 686 | /* Now allocate data blocks */ |
Theodore Ts'o | 815a113 | 2009-01-01 23:59:43 -0500 | [diff] [blame] | 687 | memset(&ar, 0, sizeof(ar)); |
| 688 | ar.inode = inode; |
| 689 | ar.goal = goal; |
| 690 | ar.len = target; |
| 691 | ar.logical = iblock; |
| 692 | if (S_ISREG(inode->i_mode)) |
| 693 | /* enable in-core preallocation only for regular files */ |
| 694 | ar.flags = EXT4_MB_HINT_DATA; |
| 695 | |
| 696 | current_block = ext4_mb_new_blocks(handle, &ar, err); |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 697 | BUG_ON(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS); |
Theodore Ts'o | 815a113 | 2009-01-01 23:59:43 -0500 | [diff] [blame] | 698 | |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 699 | if (*err && (target == blks)) { |
| 700 | /* |
| 701 | * if the allocation failed and we didn't allocate |
| 702 | * any blocks before |
| 703 | */ |
| 704 | goto failed_out; |
| 705 | } |
| 706 | if (!*err) { |
| 707 | if (target == blks) { |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 708 | /* |
| 709 | * save the new block number |
| 710 | * for the first direct block |
| 711 | */ |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 712 | new_blocks[index] = current_block; |
| 713 | } |
Theodore Ts'o | 815a113 | 2009-01-01 23:59:43 -0500 | [diff] [blame] | 714 | blk_allocated += ar.len; |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 715 | } |
| 716 | allocated: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 717 | /* total number of blocks allocated for direct blocks */ |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 718 | ret = blk_allocated; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 719 | *err = 0; |
| 720 | return ret; |
| 721 | failed_out: |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 722 | for (i = 0; i < index; i++) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 723 | ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 724 | return ret; |
| 725 | } |
| 726 | |
| 727 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 728 | * ext4_alloc_branch - allocate and set up a chain of blocks. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 729 | * @inode: owner |
| 730 | * @indirect_blks: number of allocated indirect blocks |
| 731 | * @blks: number of allocated direct blocks |
| 732 | * @offsets: offsets (in the blocks) to store the pointers to next. |
| 733 | * @branch: place to store the chain in. |
| 734 | * |
| 735 | * This function allocates blocks, zeroes out all but the last one, |
| 736 | * links them into chain and (if we are synchronous) writes them to disk. |
| 737 | * In other words, it prepares a branch that can be spliced onto the |
| 738 | * inode. It stores the information about that chain in the branch[], in |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 739 | * the same format as ext4_get_branch() would do. We are calling it after |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 740 | * we had read the existing part of chain and partial points to the last |
| 741 | * triple of that (one with zero ->key). Upon the exit we have the same |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 742 | * picture as after the successful ext4_get_block(), except that in one |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 743 | * place chain is disconnected - *branch->p is still zero (we did not |
| 744 | * set the last link), but branch->key contains the number that should |
| 745 | * be placed into *branch->p to fill that gap. |
| 746 | * |
| 747 | * If allocation fails we free all blocks we've allocated (and forget |
| 748 | * their buffer_heads) and return the error value the from failed |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 749 | * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 750 | * as described above and return 0. |
| 751 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 752 | static int ext4_alloc_branch(handle_t *handle, struct inode *inode, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 753 | ext4_lblk_t iblock, int indirect_blks, |
| 754 | int *blks, ext4_fsblk_t goal, |
| 755 | ext4_lblk_t *offsets, Indirect *branch) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 756 | { |
| 757 | int blocksize = inode->i_sb->s_blocksize; |
| 758 | int i, n = 0; |
| 759 | int err = 0; |
| 760 | struct buffer_head *bh; |
| 761 | int num; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 762 | ext4_fsblk_t new_blocks[4]; |
| 763 | ext4_fsblk_t current_block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 764 | |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 765 | num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 766 | *blks, new_blocks, &err); |
| 767 | if (err) |
| 768 | return err; |
| 769 | |
| 770 | branch[0].key = cpu_to_le32(new_blocks[0]); |
| 771 | /* |
| 772 | * metadata blocks and data blocks are allocated. |
| 773 | */ |
| 774 | for (n = 1; n <= indirect_blks; n++) { |
| 775 | /* |
| 776 | * Get buffer_head for parent block, zero it out |
| 777 | * and set the pointer to new one, then send |
| 778 | * parent to disk. |
| 779 | */ |
| 780 | bh = sb_getblk(inode->i_sb, new_blocks[n-1]); |
| 781 | branch[n].bh = bh; |
| 782 | lock_buffer(bh); |
| 783 | BUFFER_TRACE(bh, "call get_create_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 784 | err = ext4_journal_get_create_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 785 | if (err) { |
Curt Wohlgemuth | 6487a9d | 2009-07-17 10:54:08 -0400 | [diff] [blame] | 786 | /* Don't brelse(bh) here; it's done in |
| 787 | * ext4_journal_forget() below */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 788 | unlock_buffer(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 789 | goto failed; |
| 790 | } |
| 791 | |
| 792 | memset(bh->b_data, 0, blocksize); |
| 793 | branch[n].p = (__le32 *) bh->b_data + offsets[n]; |
| 794 | branch[n].key = cpu_to_le32(new_blocks[n]); |
| 795 | *branch[n].p = branch[n].key; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 796 | if (n == indirect_blks) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 797 | current_block = new_blocks[n]; |
| 798 | /* |
| 799 | * End of chain, update the last new metablock of |
| 800 | * the chain to point to the new allocated |
| 801 | * data blocks numbers |
| 802 | */ |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 803 | for (i = 1; i < num; i++) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 804 | *(branch[n].p + i) = cpu_to_le32(++current_block); |
| 805 | } |
| 806 | BUFFER_TRACE(bh, "marking uptodate"); |
| 807 | set_buffer_uptodate(bh); |
| 808 | unlock_buffer(bh); |
| 809 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 810 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
| 811 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 812 | if (err) |
| 813 | goto failed; |
| 814 | } |
| 815 | *blks = num; |
| 816 | return err; |
| 817 | failed: |
| 818 | /* Allocation failed, free what we already allocated */ |
| 819 | for (i = 1; i <= n ; i++) { |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 820 | BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 821 | ext4_journal_forget(handle, branch[i].bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 822 | } |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 823 | for (i = 0; i < indirect_blks; i++) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 824 | ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 825 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 826 | ext4_free_blocks(handle, inode, new_blocks[i], num, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 827 | |
| 828 | return err; |
| 829 | } |
| 830 | |
| 831 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 832 | * ext4_splice_branch - splice the allocated branch onto inode. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 833 | * @inode: owner |
| 834 | * @block: (logical) number of block we are adding |
| 835 | * @chain: chain of indirect blocks (with a missing link - see |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 836 | * ext4_alloc_branch) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 837 | * @where: location of missing link |
| 838 | * @num: number of indirect blocks we are adding |
| 839 | * @blks: number of direct blocks we are adding |
| 840 | * |
| 841 | * This function fills the missing link and does all housekeeping needed in |
| 842 | * inode (->i_blocks, etc.). In case of success we end up with the full |
| 843 | * chain to new block and return 0. |
| 844 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 845 | static int ext4_splice_branch(handle_t *handle, struct inode *inode, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 846 | ext4_lblk_t block, Indirect *where, int num, |
| 847 | int blks) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 848 | { |
| 849 | int i; |
| 850 | int err = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 851 | ext4_fsblk_t current_block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 852 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 853 | /* |
| 854 | * If we're splicing into a [td]indirect block (as opposed to the |
| 855 | * inode) then we need to get write access to the [td]indirect block |
| 856 | * before the splice. |
| 857 | */ |
| 858 | if (where->bh) { |
| 859 | BUFFER_TRACE(where->bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 860 | err = ext4_journal_get_write_access(handle, where->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 861 | if (err) |
| 862 | goto err_out; |
| 863 | } |
| 864 | /* That's it */ |
| 865 | |
| 866 | *where->p = where->key; |
| 867 | |
| 868 | /* |
| 869 | * Update the host buffer_head or inode to point to more just allocated |
| 870 | * direct blocks blocks |
| 871 | */ |
| 872 | if (num == 0 && blks > 1) { |
| 873 | current_block = le32_to_cpu(where->key) + 1; |
| 874 | for (i = 1; i < blks; i++) |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 875 | *(where->p + i) = cpu_to_le32(current_block++); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 878 | /* We are done with atomic stuff, now do the rest of housekeeping */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 879 | /* had we spliced it onto indirect block? */ |
| 880 | if (where->bh) { |
| 881 | /* |
| 882 | * If we spliced it onto an indirect block, we haven't |
| 883 | * altered the inode. Note however that if it is being spliced |
| 884 | * onto an indirect block at the very end of the file (the |
| 885 | * file is growing) then we *will* alter the inode to reflect |
| 886 | * the new i_size. But that is not done here - it is done in |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 887 | * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 888 | */ |
| 889 | jbd_debug(5, "splicing indirect only\n"); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 890 | BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata"); |
| 891 | err = ext4_handle_dirty_metadata(handle, inode, where->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 892 | if (err) |
| 893 | goto err_out; |
| 894 | } else { |
| 895 | /* |
| 896 | * OK, we spliced it into the inode itself on a direct block. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 897 | */ |
Theodore Ts'o | 4159175 | 2009-06-15 03:41:23 -0400 | [diff] [blame] | 898 | ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 899 | jbd_debug(5, "splicing direct\n"); |
| 900 | } |
| 901 | return err; |
| 902 | |
| 903 | err_out: |
| 904 | for (i = 1; i <= num; i++) { |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 905 | BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 906 | ext4_journal_forget(handle, where[i].bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 907 | ext4_free_blocks(handle, inode, |
| 908 | le32_to_cpu(where[i-1].key), 1, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 909 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 910 | ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 911 | |
| 912 | return err; |
| 913 | } |
| 914 | |
| 915 | /* |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 916 | * The ext4_ind_get_blocks() function handles non-extents inodes |
| 917 | * (i.e., using the traditional indirect/double-indirect i_blocks |
| 918 | * scheme) for ext4_get_blocks(). |
| 919 | * |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 920 | * Allocation strategy is simple: if we have to allocate something, we will |
| 921 | * have to go the whole way to leaf. So let's do it before attaching anything |
| 922 | * to tree, set linkage between the newborn blocks, write them if sync is |
| 923 | * required, recheck the path, free and repeat if check fails, otherwise |
| 924 | * set the last missing link (that will protect us from any truncate-generated |
| 925 | * removals - all blocks on the path are immune now) and possibly force the |
| 926 | * write on the parent block. |
| 927 | * That has a nice additional property: no special recovery from the failed |
| 928 | * allocations is needed - we simply release blocks and do not touch anything |
| 929 | * reachable from inode. |
| 930 | * |
| 931 | * `handle' can be NULL if create == 0. |
| 932 | * |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 933 | * return > 0, # of blocks mapped or allocated. |
| 934 | * return = 0, if plain lookup failed. |
| 935 | * return < 0, error case. |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 936 | * |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 937 | * The ext4_ind_get_blocks() function should be called with |
| 938 | * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem |
| 939 | * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or |
| 940 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system |
| 941 | * blocks. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 942 | */ |
Theodore Ts'o | e4d996c | 2009-05-12 00:25:28 -0400 | [diff] [blame] | 943 | static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 944 | ext4_lblk_t iblock, unsigned int maxblocks, |
| 945 | struct buffer_head *bh_result, |
| 946 | int flags) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 947 | { |
| 948 | int err = -EIO; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 949 | ext4_lblk_t offsets[4]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 950 | Indirect chain[4]; |
| 951 | Indirect *partial; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 952 | ext4_fsblk_t goal; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 953 | int indirect_blks; |
| 954 | int blocks_to_boundary = 0; |
| 955 | int depth; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 956 | int count = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 957 | ext4_fsblk_t first_block = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 958 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 959 | J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 960 | J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 961 | depth = ext4_block_to_path(inode, iblock, offsets, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 962 | &blocks_to_boundary); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 963 | |
| 964 | if (depth == 0) |
| 965 | goto out; |
| 966 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 967 | partial = ext4_get_branch(inode, depth, offsets, chain, &err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 968 | |
| 969 | /* Simplest case - block found, no allocation needed */ |
| 970 | if (!partial) { |
| 971 | first_block = le32_to_cpu(chain[depth - 1].key); |
| 972 | clear_buffer_new(bh_result); |
| 973 | count++; |
| 974 | /*map more blocks*/ |
| 975 | while (count < maxblocks && count <= blocks_to_boundary) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 976 | ext4_fsblk_t blk; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 977 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 978 | blk = le32_to_cpu(*(chain[depth-1].p + count)); |
| 979 | |
| 980 | if (blk == first_block + count) |
| 981 | count++; |
| 982 | else |
| 983 | break; |
| 984 | } |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 985 | goto got_it; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /* Next simple case - plain lookup or failed read of indirect block */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 989 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 990 | goto cleanup; |
| 991 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 992 | /* |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 993 | * Okay, we need to do block allocation. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 994 | */ |
Akinobu Mita | fb01bfd | 2008-02-06 01:40:16 -0800 | [diff] [blame] | 995 | goal = ext4_find_goal(inode, iblock, partial); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 996 | |
| 997 | /* the number of blocks need to allocate for [d,t]indirect blocks */ |
| 998 | indirect_blks = (chain + depth) - partial - 1; |
| 999 | |
| 1000 | /* |
| 1001 | * Next look up the indirect map to count the totoal number of |
| 1002 | * direct blocks to allocate for this branch. |
| 1003 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1004 | count = ext4_blks_to_allocate(partial, indirect_blks, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1005 | maxblocks, blocks_to_boundary); |
| 1006 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1007 | * Block out ext4_truncate while we alter the tree |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1008 | */ |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1009 | err = ext4_alloc_branch(handle, inode, iblock, indirect_blks, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1010 | &count, goal, |
| 1011 | offsets + (partial - chain), partial); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1012 | |
| 1013 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1014 | * The ext4_splice_branch call will free and forget any buffers |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1015 | * on the new chain if there is a failure, but that risks using |
| 1016 | * up transaction credits, especially for bitmaps where the |
| 1017 | * credits cannot be returned. Can we handle this somehow? We |
| 1018 | * may need to return -EAGAIN upwards in the worst case. --sct |
| 1019 | */ |
| 1020 | if (!err) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1021 | err = ext4_splice_branch(handle, inode, iblock, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1022 | partial, indirect_blks, count); |
| 1023 | else |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1024 | goto cleanup; |
| 1025 | |
| 1026 | set_buffer_new(bh_result); |
| 1027 | got_it: |
| 1028 | map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); |
| 1029 | if (count > blocks_to_boundary) |
| 1030 | set_buffer_boundary(bh_result); |
| 1031 | err = count; |
| 1032 | /* Clean up and exit */ |
| 1033 | partial = chain + depth - 1; /* the whole chain */ |
| 1034 | cleanup: |
| 1035 | while (partial > chain) { |
| 1036 | BUFFER_TRACE(partial->bh, "call brelse"); |
| 1037 | brelse(partial->bh); |
| 1038 | partial--; |
| 1039 | } |
| 1040 | BUFFER_TRACE(bh_result, "returned"); |
| 1041 | out: |
| 1042 | return err; |
| 1043 | } |
| 1044 | |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1045 | qsize_t ext4_get_reserved_space(struct inode *inode) |
| 1046 | { |
| 1047 | unsigned long long total; |
| 1048 | |
| 1049 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1050 | total = EXT4_I(inode)->i_reserved_data_blocks + |
| 1051 | EXT4_I(inode)->i_reserved_meta_blocks; |
| 1052 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1053 | |
| 1054 | return total; |
| 1055 | } |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1056 | /* |
| 1057 | * Calculate the number of metadata blocks need to reserve |
| 1058 | * to allocate @blocks for non extent file based file |
| 1059 | */ |
| 1060 | static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks) |
| 1061 | { |
| 1062 | int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
| 1063 | int ind_blks, dind_blks, tind_blks; |
| 1064 | |
| 1065 | /* number of new indirect blocks needed */ |
| 1066 | ind_blks = (blocks + icap - 1) / icap; |
| 1067 | |
| 1068 | dind_blks = (ind_blks + icap - 1) / icap; |
| 1069 | |
| 1070 | tind_blks = 1; |
| 1071 | |
| 1072 | return ind_blks + dind_blks + tind_blks; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Calculate the number of metadata blocks need to reserve |
| 1077 | * to allocate given number of blocks |
| 1078 | */ |
| 1079 | static int ext4_calc_metadata_amount(struct inode *inode, int blocks) |
| 1080 | { |
Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1081 | if (!blocks) |
| 1082 | return 0; |
| 1083 | |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1084 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) |
| 1085 | return ext4_ext_calc_metadata_amount(inode, blocks); |
| 1086 | |
| 1087 | return ext4_indirect_calc_metadata_amount(inode, blocks); |
| 1088 | } |
| 1089 | |
| 1090 | static void ext4_da_update_reserve_space(struct inode *inode, int used) |
| 1091 | { |
| 1092 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1093 | int total, mdb, mdb_free; |
| 1094 | |
| 1095 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1096 | /* recalculate the number of metablocks still need to be reserved */ |
| 1097 | total = EXT4_I(inode)->i_reserved_data_blocks - used; |
| 1098 | mdb = ext4_calc_metadata_amount(inode, total); |
| 1099 | |
| 1100 | /* figure out how many metablocks to release */ |
| 1101 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); |
| 1102 | mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; |
| 1103 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 1104 | if (mdb_free) { |
| 1105 | /* Account for allocated meta_blocks */ |
| 1106 | mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks; |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1107 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 1108 | /* update fs dirty blocks counter */ |
| 1109 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free); |
| 1110 | EXT4_I(inode)->i_allocated_meta_blocks = 0; |
| 1111 | EXT4_I(inode)->i_reserved_meta_blocks = mdb; |
| 1112 | } |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1113 | |
| 1114 | /* update per-inode reservations */ |
| 1115 | BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks); |
| 1116 | EXT4_I(inode)->i_reserved_data_blocks -= used; |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1117 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1118 | |
| 1119 | /* |
| 1120 | * free those over-booking quota for metadata blocks |
| 1121 | */ |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1122 | if (mdb_free) |
| 1123 | vfs_dq_release_reservation_block(inode, mdb_free); |
Aneesh Kumar K.V | d601430 | 2009-03-27 22:36:43 -0400 | [diff] [blame] | 1124 | |
| 1125 | /* |
| 1126 | * If we have done all the pending block allocations and if |
| 1127 | * there aren't any writers on the inode, we can discard the |
| 1128 | * inode's preallocations. |
| 1129 | */ |
| 1130 | if (!total && (atomic_read(&inode->i_writecount) == 0)) |
| 1131 | ext4_discard_preallocations(inode); |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1132 | } |
| 1133 | |
Theodore Ts'o | 80e4246 | 2009-09-08 08:21:26 -0400 | [diff] [blame] | 1134 | static int check_block_validity(struct inode *inode, const char *msg, |
| 1135 | sector_t logical, sector_t phys, int len) |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 1136 | { |
| 1137 | if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) { |
Theodore Ts'o | 80e4246 | 2009-09-08 08:21:26 -0400 | [diff] [blame] | 1138 | ext4_error(inode->i_sb, msg, |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 1139 | "inode #%lu logical block %llu mapped to %llu " |
| 1140 | "(size %d)", inode->i_ino, |
| 1141 | (unsigned long long) logical, |
| 1142 | (unsigned long long) phys, len); |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 1143 | return -EIO; |
| 1144 | } |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1148 | /* |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 1149 | * Return the number of dirty pages in the given inode starting at |
| 1150 | * page frame idx. |
| 1151 | */ |
| 1152 | static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, |
| 1153 | unsigned int max_pages) |
| 1154 | { |
| 1155 | struct address_space *mapping = inode->i_mapping; |
| 1156 | pgoff_t index; |
| 1157 | struct pagevec pvec; |
| 1158 | pgoff_t num = 0; |
| 1159 | int i, nr_pages, done = 0; |
| 1160 | |
| 1161 | if (max_pages == 0) |
| 1162 | return 0; |
| 1163 | pagevec_init(&pvec, 0); |
| 1164 | while (!done) { |
| 1165 | index = idx; |
| 1166 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 1167 | PAGECACHE_TAG_DIRTY, |
| 1168 | (pgoff_t)PAGEVEC_SIZE); |
| 1169 | if (nr_pages == 0) |
| 1170 | break; |
| 1171 | for (i = 0; i < nr_pages; i++) { |
| 1172 | struct page *page = pvec.pages[i]; |
| 1173 | struct buffer_head *bh, *head; |
| 1174 | |
| 1175 | lock_page(page); |
| 1176 | if (unlikely(page->mapping != mapping) || |
| 1177 | !PageDirty(page) || |
| 1178 | PageWriteback(page) || |
| 1179 | page->index != idx) { |
| 1180 | done = 1; |
| 1181 | unlock_page(page); |
| 1182 | break; |
| 1183 | } |
| 1184 | head = page_buffers(page); |
| 1185 | bh = head; |
| 1186 | do { |
| 1187 | if (!buffer_delay(bh) && |
| 1188 | !buffer_unwritten(bh)) { |
| 1189 | done = 1; |
| 1190 | break; |
| 1191 | } |
| 1192 | } while ((bh = bh->b_this_page) != head); |
| 1193 | unlock_page(page); |
| 1194 | if (done) |
| 1195 | break; |
| 1196 | idx++; |
| 1197 | num++; |
| 1198 | if (num >= max_pages) |
| 1199 | break; |
| 1200 | } |
| 1201 | pagevec_release(&pvec); |
| 1202 | } |
| 1203 | return num; |
| 1204 | } |
| 1205 | |
| 1206 | /* |
Theodore Ts'o | 12b7ac1 | 2009-05-14 00:57:44 -0400 | [diff] [blame] | 1207 | * The ext4_get_blocks() function tries to look up the requested blocks, |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1208 | * and returns if the blocks are already mapped. |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1209 | * |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1210 | * Otherwise it takes the write lock of the i_data_sem and allocate blocks |
| 1211 | * and store the allocated blocks in the result buffer head and mark it |
| 1212 | * mapped. |
| 1213 | * |
| 1214 | * If file type is extents based, it will call ext4_ext_get_blocks(), |
Theodore Ts'o | e4d996c | 2009-05-12 00:25:28 -0400 | [diff] [blame] | 1215 | * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1216 | * based files |
| 1217 | * |
| 1218 | * On success, it returns the number of blocks being mapped or allocate. |
| 1219 | * if create==0 and the blocks are pre-allocated and uninitialized block, |
| 1220 | * the result buffer head is unmapped. If the create ==1, it will make sure |
| 1221 | * the buffer head is mapped. |
| 1222 | * |
| 1223 | * It returns 0 if plain look up failed (blocks have not been allocated), in |
| 1224 | * that casem, buffer head is unmapped |
| 1225 | * |
| 1226 | * It returns the error in case of allocation failure. |
| 1227 | */ |
Theodore Ts'o | 12b7ac1 | 2009-05-14 00:57:44 -0400 | [diff] [blame] | 1228 | int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, |
| 1229 | unsigned int max_blocks, struct buffer_head *bh, |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1230 | int flags) |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1231 | { |
| 1232 | int retval; |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1233 | |
| 1234 | clear_buffer_mapped(bh); |
Aneesh Kumar K.V | 2a8964d | 2009-05-14 17:05:39 -0400 | [diff] [blame] | 1235 | clear_buffer_unwritten(bh); |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1236 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 1237 | ext_debug("ext4_get_blocks(): inode %lu, flag %d, max_blocks %u," |
| 1238 | "logical block %lu\n", inode->i_ino, flags, max_blocks, |
| 1239 | (unsigned long)block); |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1240 | /* |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 1241 | * Try to see if we can get the block without requesting a new |
| 1242 | * file system block. |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1243 | */ |
| 1244 | down_read((&EXT4_I(inode)->i_data_sem)); |
| 1245 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { |
| 1246 | retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1247 | bh, 0); |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1248 | } else { |
Theodore Ts'o | e4d996c | 2009-05-12 00:25:28 -0400 | [diff] [blame] | 1249 | retval = ext4_ind_get_blocks(handle, inode, block, max_blocks, |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1250 | bh, 0); |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1251 | } |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1252 | up_read((&EXT4_I(inode)->i_data_sem)); |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1253 | |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 1254 | if (retval > 0 && buffer_mapped(bh)) { |
Theodore Ts'o | 80e4246 | 2009-09-08 08:21:26 -0400 | [diff] [blame] | 1255 | int ret = check_block_validity(inode, "file system corruption", |
| 1256 | block, bh->b_blocknr, retval); |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 1257 | if (ret != 0) |
| 1258 | return ret; |
| 1259 | } |
| 1260 | |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1261 | /* If it is only a block(s) look up */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1262 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1263 | return retval; |
| 1264 | |
| 1265 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1266 | * Returns if the blocks have already allocated |
| 1267 | * |
| 1268 | * Note that if blocks have been preallocated |
| 1269 | * ext4_ext_get_block() returns th create = 0 |
| 1270 | * with buffer head unmapped. |
| 1271 | */ |
| 1272 | if (retval > 0 && buffer_mapped(bh)) |
| 1273 | return retval; |
| 1274 | |
| 1275 | /* |
Aneesh Kumar K.V | 2a8964d | 2009-05-14 17:05:39 -0400 | [diff] [blame] | 1276 | * When we call get_blocks without the create flag, the |
| 1277 | * BH_Unwritten flag could have gotten set if the blocks |
| 1278 | * requested were part of a uninitialized extent. We need to |
| 1279 | * clear this flag now that we are committed to convert all or |
| 1280 | * part of the uninitialized extent to be an initialized |
| 1281 | * extent. This is because we need to avoid the combination |
| 1282 | * of BH_Unwritten and BH_Mapped flags being simultaneously |
| 1283 | * set on the buffer_head. |
| 1284 | */ |
| 1285 | clear_buffer_unwritten(bh); |
| 1286 | |
| 1287 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1288 | * New blocks allocate and/or writing to uninitialized extent |
| 1289 | * will possibly result in updating i_data, so we take |
| 1290 | * the write lock of i_data_sem, and call get_blocks() |
| 1291 | * with create == 1 flag. |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1292 | */ |
| 1293 | down_write((&EXT4_I(inode)->i_data_sem)); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1294 | |
| 1295 | /* |
| 1296 | * if the caller is from delayed allocation writeout path |
| 1297 | * we have already reserved fs blocks for allocation |
| 1298 | * let the underlying get_block() function know to |
| 1299 | * avoid double accounting |
| 1300 | */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1301 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1302 | EXT4_I(inode)->i_delalloc_reserved_flag = 1; |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1303 | /* |
| 1304 | * We need to check for EXT4 here because migrate |
| 1305 | * could have changed the inode type in between |
| 1306 | */ |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1307 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { |
| 1308 | retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1309 | bh, flags); |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1310 | } else { |
Theodore Ts'o | e4d996c | 2009-05-12 00:25:28 -0400 | [diff] [blame] | 1311 | retval = ext4_ind_get_blocks(handle, inode, block, |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1312 | max_blocks, bh, flags); |
Aneesh Kumar K.V | 267e4db | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 1313 | |
| 1314 | if (retval > 0 && buffer_new(bh)) { |
| 1315 | /* |
| 1316 | * We allocated new blocks which will result in |
| 1317 | * i_data's format changing. Force the migrate |
| 1318 | * to fail by clearing migrate flags |
| 1319 | */ |
Theodore Ts'o | 1b9c12f | 2009-09-17 08:32:22 -0400 | [diff] [blame] | 1320 | EXT4_I(inode)->i_state &= ~EXT4_STATE_EXT_MIGRATE; |
Aneesh Kumar K.V | 267e4db | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 1321 | } |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1322 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1323 | |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 1324 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1325 | EXT4_I(inode)->i_delalloc_reserved_flag = 0; |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 1326 | |
| 1327 | /* |
| 1328 | * Update reserved blocks/metadata blocks after successful |
| 1329 | * block allocation which had been deferred till now. |
| 1330 | */ |
| 1331 | if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE)) |
| 1332 | ext4_da_update_reserve_space(inode, retval); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1333 | |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1334 | up_write((&EXT4_I(inode)->i_data_sem)); |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 1335 | if (retval > 0 && buffer_mapped(bh)) { |
Theodore Ts'o | 80e4246 | 2009-09-08 08:21:26 -0400 | [diff] [blame] | 1336 | int ret = check_block_validity(inode, "file system " |
| 1337 | "corruption after allocation", |
| 1338 | block, bh->b_blocknr, retval); |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 1339 | if (ret != 0) |
| 1340 | return ret; |
| 1341 | } |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1342 | return retval; |
| 1343 | } |
| 1344 | |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 1345 | /* Maximum number of blocks we map for direct IO at once. */ |
| 1346 | #define DIO_MAX_BLOCKS 4096 |
| 1347 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1348 | int ext4_get_block(struct inode *inode, sector_t iblock, |
| 1349 | struct buffer_head *bh_result, int create) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1350 | { |
Dmitriy Monakhov | 3e4fdaf | 2007-02-10 01:46:35 -0800 | [diff] [blame] | 1351 | handle_t *handle = ext4_journal_current_handle(); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1352 | int ret = 0, started = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1353 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 1354 | int dio_credits; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1355 | |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1356 | if (create && !handle) { |
| 1357 | /* Direct IO write... */ |
| 1358 | if (max_blocks > DIO_MAX_BLOCKS) |
| 1359 | max_blocks = DIO_MAX_BLOCKS; |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 1360 | dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); |
| 1361 | handle = ext4_journal_start(inode, dio_credits); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1362 | if (IS_ERR(handle)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1363 | ret = PTR_ERR(handle); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1364 | goto out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1365 | } |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1366 | started = 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Theodore Ts'o | 12b7ac1 | 2009-05-14 00:57:44 -0400 | [diff] [blame] | 1369 | ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result, |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1370 | create ? EXT4_GET_BLOCKS_CREATE : 0); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1371 | if (ret > 0) { |
| 1372 | bh_result->b_size = (ret << inode->i_blkbits); |
| 1373 | ret = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1374 | } |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1375 | if (started) |
| 1376 | ext4_journal_stop(handle); |
| 1377 | out: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1378 | return ret; |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * `handle' can be NULL if create is zero |
| 1383 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1384 | 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] | 1385 | ext4_lblk_t block, int create, int *errp) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1386 | { |
| 1387 | struct buffer_head dummy; |
| 1388 | int fatal = 0, err; |
Jan Kara | 03f5d8b | 2009-06-09 00:17:05 -0400 | [diff] [blame] | 1389 | int flags = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1390 | |
| 1391 | J_ASSERT(handle != NULL || create == 0); |
| 1392 | |
| 1393 | dummy.b_state = 0; |
| 1394 | dummy.b_blocknr = -1000; |
| 1395 | buffer_trace_init(&dummy.b_history); |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1396 | if (create) |
| 1397 | flags |= EXT4_GET_BLOCKS_CREATE; |
| 1398 | err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1399 | /* |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1400 | * ext4_get_blocks() returns number of blocks mapped. 0 in |
| 1401 | * case of a HOLE. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1402 | */ |
| 1403 | if (err > 0) { |
| 1404 | if (err > 1) |
| 1405 | WARN_ON(1); |
| 1406 | err = 0; |
| 1407 | } |
| 1408 | *errp = err; |
| 1409 | if (!err && buffer_mapped(&dummy)) { |
| 1410 | struct buffer_head *bh; |
| 1411 | bh = sb_getblk(inode->i_sb, dummy.b_blocknr); |
| 1412 | if (!bh) { |
| 1413 | *errp = -EIO; |
| 1414 | goto err; |
| 1415 | } |
| 1416 | if (buffer_new(&dummy)) { |
| 1417 | J_ASSERT(create != 0); |
Aneesh Kumar K.V | ac39849 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1418 | J_ASSERT(handle != NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1419 | |
| 1420 | /* |
| 1421 | * Now that we do not always journal data, we should |
| 1422 | * keep in mind whether this should always journal the |
| 1423 | * new buffer as metadata. For now, regular file |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1424 | * writes use ext4_get_block instead, so it's not a |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1425 | * problem. |
| 1426 | */ |
| 1427 | lock_buffer(bh); |
| 1428 | BUFFER_TRACE(bh, "call get_create_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1429 | fatal = ext4_journal_get_create_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1430 | if (!fatal && !buffer_uptodate(bh)) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1431 | memset(bh->b_data, 0, inode->i_sb->s_blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1432 | set_buffer_uptodate(bh); |
| 1433 | } |
| 1434 | unlock_buffer(bh); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1435 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
| 1436 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1437 | if (!fatal) |
| 1438 | fatal = err; |
| 1439 | } else { |
| 1440 | BUFFER_TRACE(bh, "not a new buffer"); |
| 1441 | } |
| 1442 | if (fatal) { |
| 1443 | *errp = fatal; |
| 1444 | brelse(bh); |
| 1445 | bh = NULL; |
| 1446 | } |
| 1447 | return bh; |
| 1448 | } |
| 1449 | err: |
| 1450 | return NULL; |
| 1451 | } |
| 1452 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1453 | 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] | 1454 | ext4_lblk_t block, int create, int *err) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1455 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1456 | struct buffer_head *bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1457 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1458 | bh = ext4_getblk(handle, inode, block, create, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1459 | if (!bh) |
| 1460 | return bh; |
| 1461 | if (buffer_uptodate(bh)) |
| 1462 | return bh; |
| 1463 | ll_rw_block(READ_META, 1, &bh); |
| 1464 | wait_on_buffer(bh); |
| 1465 | if (buffer_uptodate(bh)) |
| 1466 | return bh; |
| 1467 | put_bh(bh); |
| 1468 | *err = -EIO; |
| 1469 | return NULL; |
| 1470 | } |
| 1471 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1472 | static int walk_page_buffers(handle_t *handle, |
| 1473 | struct buffer_head *head, |
| 1474 | unsigned from, |
| 1475 | unsigned to, |
| 1476 | int *partial, |
| 1477 | int (*fn)(handle_t *handle, |
| 1478 | struct buffer_head *bh)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1479 | { |
| 1480 | struct buffer_head *bh; |
| 1481 | unsigned block_start, block_end; |
| 1482 | unsigned blocksize = head->b_size; |
| 1483 | int err, ret = 0; |
| 1484 | struct buffer_head *next; |
| 1485 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1486 | for (bh = head, block_start = 0; |
| 1487 | ret == 0 && (bh != head || !block_start); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1488 | block_start = block_end, bh = next) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1489 | next = bh->b_this_page; |
| 1490 | block_end = block_start + blocksize; |
| 1491 | if (block_end <= from || block_start >= to) { |
| 1492 | if (partial && !buffer_uptodate(bh)) |
| 1493 | *partial = 1; |
| 1494 | continue; |
| 1495 | } |
| 1496 | err = (*fn)(handle, bh); |
| 1497 | if (!ret) |
| 1498 | ret = err; |
| 1499 | } |
| 1500 | return ret; |
| 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | * To preserve ordering, it is essential that the hole instantiation and |
| 1505 | * the data write be encapsulated in a single transaction. We cannot |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1506 | * 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] | 1507 | * 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] | 1508 | * prepare_write() is the right place. |
| 1509 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1510 | * Also, this function can nest inside ext4_writepage() -> |
| 1511 | * block_write_full_page(). In that case, we *know* that ext4_writepage() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1512 | * has generated enough buffer credits to do the whole page. So we won't |
| 1513 | * block on the journal in that case, which is good, because the caller may |
| 1514 | * be PF_MEMALLOC. |
| 1515 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1516 | * By accident, ext4 can be reentered when a transaction is open via |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1517 | * quota file writes. If we were to commit the transaction while thus |
| 1518 | * reentered, there can be a deadlock - we would be holding a quota |
| 1519 | * lock, and the commit would never complete if another thread had a |
| 1520 | * transaction open and was blocking on the quota lock - a ranking |
| 1521 | * violation. |
| 1522 | * |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 1523 | * 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] | 1524 | * will _not_ run commit under these circumstances because handle->h_ref |
| 1525 | * is elevated. We'll still have enough credits for the tiny quotafile |
| 1526 | * write. |
| 1527 | */ |
| 1528 | static int do_journal_get_write_access(handle_t *handle, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1529 | struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1530 | { |
| 1531 | if (!buffer_mapped(bh) || buffer_freed(bh)) |
| 1532 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1533 | return ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1536 | 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] | 1537 | loff_t pos, unsigned len, unsigned flags, |
| 1538 | struct page **pagep, void **fsdata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1539 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1540 | struct inode *inode = mapping->host; |
Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 1541 | int ret, needed_blocks; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1542 | handle_t *handle; |
| 1543 | int retries = 0; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1544 | struct page *page; |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1545 | pgoff_t index; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1546 | unsigned from, to; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1547 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 1548 | trace_ext4_write_begin(inode, pos, len, flags); |
Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 1549 | /* |
| 1550 | * Reserve one block more for addition to orphan list in case |
| 1551 | * we allocate blocks but write fails for some reason |
| 1552 | */ |
| 1553 | needed_blocks = ext4_writepage_trans_blocks(inode) + 1; |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1554 | index = pos >> PAGE_CACHE_SHIFT; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1555 | from = pos & (PAGE_CACHE_SIZE - 1); |
| 1556 | to = from + len; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1557 | |
| 1558 | retry: |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1559 | handle = ext4_journal_start(inode, needed_blocks); |
| 1560 | if (IS_ERR(handle)) { |
| 1561 | ret = PTR_ERR(handle); |
| 1562 | goto out; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
Jan Kara | ebd3610 | 2009-02-22 21:09:59 -0500 | [diff] [blame] | 1565 | /* We cannot recurse into the filesystem as the transaction is already |
| 1566 | * started */ |
| 1567 | flags |= AOP_FLAG_NOFS; |
| 1568 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 1569 | page = grab_cache_page_write_begin(mapping, index, flags); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1570 | if (!page) { |
| 1571 | ext4_journal_stop(handle); |
| 1572 | ret = -ENOMEM; |
| 1573 | goto out; |
| 1574 | } |
| 1575 | *pagep = page; |
| 1576 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1577 | ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
Jan Kara | ebd3610 | 2009-02-22 21:09:59 -0500 | [diff] [blame] | 1578 | ext4_get_block); |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1579 | |
| 1580 | if (!ret && ext4_should_journal_data(inode)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1581 | ret = walk_page_buffers(handle, page_buffers(page), |
| 1582 | from, to, NULL, do_journal_get_write_access); |
| 1583 | } |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1584 | |
| 1585 | if (ret) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1586 | unlock_page(page); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1587 | page_cache_release(page); |
Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 1588 | /* |
| 1589 | * block_write_begin may have instantiated a few blocks |
| 1590 | * outside i_size. Trim these off again. Don't need |
| 1591 | * i_size_read because we hold i_mutex. |
Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 1592 | * |
| 1593 | * Add inode to orphan list in case we crash before |
| 1594 | * truncate finishes |
Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 1595 | */ |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1596 | if (pos + len > inode->i_size && ext4_can_truncate(inode)) |
Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 1597 | ext4_orphan_add(handle, inode); |
| 1598 | |
| 1599 | ext4_journal_stop(handle); |
| 1600 | if (pos + len > inode->i_size) { |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1601 | ext4_truncate(inode); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1602 | /* |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1603 | * If truncate failed early the inode might |
Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 1604 | * still be on the orphan list; we need to |
| 1605 | * make sure the inode is removed from the |
| 1606 | * orphan list in that case. |
| 1607 | */ |
| 1608 | if (inode->i_nlink) |
| 1609 | ext4_orphan_del(NULL, inode); |
| 1610 | } |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1613 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1614 | goto retry; |
Andrew Morton | 7479d2b | 2007-04-01 23:49:44 -0700 | [diff] [blame] | 1615 | out: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1616 | return ret; |
| 1617 | } |
| 1618 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1619 | /* For write_end() in data=journal mode */ |
| 1620 | static int write_end_fn(handle_t *handle, struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1621 | { |
| 1622 | if (!buffer_mapped(bh) || buffer_freed(bh)) |
| 1623 | return 0; |
| 1624 | set_buffer_uptodate(bh); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1625 | return ext4_handle_dirty_metadata(handle, NULL, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1628 | static int ext4_generic_write_end(struct file *file, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1629 | struct address_space *mapping, |
| 1630 | loff_t pos, unsigned len, unsigned copied, |
| 1631 | struct page *page, void *fsdata) |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1632 | { |
| 1633 | int i_size_changed = 0; |
| 1634 | struct inode *inode = mapping->host; |
| 1635 | handle_t *handle = ext4_journal_current_handle(); |
| 1636 | |
| 1637 | copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); |
| 1638 | |
| 1639 | /* |
| 1640 | * No need to use i_size_read() here, the i_size |
| 1641 | * cannot change under us because we hold i_mutex. |
| 1642 | * |
| 1643 | * But it's important to update i_size while still holding page lock: |
| 1644 | * page writeout could otherwise come in and zero beyond i_size. |
| 1645 | */ |
| 1646 | if (pos + copied > inode->i_size) { |
| 1647 | i_size_write(inode, pos + copied); |
| 1648 | i_size_changed = 1; |
| 1649 | } |
| 1650 | |
| 1651 | if (pos + copied > EXT4_I(inode)->i_disksize) { |
| 1652 | /* We need to mark inode dirty even if |
| 1653 | * new_i_size is less that inode->i_size |
| 1654 | * bu greater than i_disksize.(hint delalloc) |
| 1655 | */ |
| 1656 | ext4_update_i_disksize(inode, (pos + copied)); |
| 1657 | i_size_changed = 1; |
| 1658 | } |
| 1659 | unlock_page(page); |
| 1660 | page_cache_release(page); |
| 1661 | |
| 1662 | /* |
| 1663 | * Don't mark the inode dirty under page lock. First, it unnecessarily |
| 1664 | * makes the holding time of page lock longer. Second, it forces lock |
| 1665 | * ordering of page lock and transaction start for journaling |
| 1666 | * filesystems. |
| 1667 | */ |
| 1668 | if (i_size_changed) |
| 1669 | ext4_mark_inode_dirty(handle, inode); |
| 1670 | |
| 1671 | return copied; |
| 1672 | } |
| 1673 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1674 | /* |
| 1675 | * We need to pick up the new inode size which generic_commit_write gave us |
| 1676 | * `file' can be NULL - eg, when called from page_symlink(). |
| 1677 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1678 | * ext4 never places buffers on inode->i_mapping->private_list. metadata |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1679 | * buffers are managed internally. |
| 1680 | */ |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1681 | static int ext4_ordered_write_end(struct file *file, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1682 | struct address_space *mapping, |
| 1683 | loff_t pos, unsigned len, unsigned copied, |
| 1684 | struct page *page, void *fsdata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1685 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1686 | handle_t *handle = ext4_journal_current_handle(); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1687 | struct inode *inode = mapping->host; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1688 | int ret = 0, ret2; |
| 1689 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 1690 | trace_ext4_ordered_write_end(inode, pos, len, copied); |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1691 | ret = ext4_jbd2_file_inode(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1692 | |
| 1693 | if (ret == 0) { |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1694 | ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1695 | page, fsdata); |
Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 1696 | copied = ret2; |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1697 | if (pos + len > inode->i_size && ext4_can_truncate(inode)) |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1698 | /* if we have allocated more blocks and copied |
| 1699 | * less. We will have blocks allocated outside |
| 1700 | * inode->i_size. So truncate them |
| 1701 | */ |
| 1702 | ext4_orphan_add(handle, inode); |
Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 1703 | if (ret2 < 0) |
| 1704 | ret = ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1705 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1706 | ret2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1707 | if (!ret) |
| 1708 | ret = ret2; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1709 | |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1710 | if (pos + len > inode->i_size) { |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1711 | ext4_truncate(inode); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1712 | /* |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1713 | * If truncate failed early the inode might still be |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1714 | * on the orphan list; we need to make sure the inode |
| 1715 | * is removed from the orphan list in that case. |
| 1716 | */ |
| 1717 | if (inode->i_nlink) |
| 1718 | ext4_orphan_del(NULL, inode); |
| 1719 | } |
| 1720 | |
| 1721 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1722 | return ret ? ret : copied; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1725 | static int ext4_writeback_write_end(struct file *file, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1726 | struct address_space *mapping, |
| 1727 | loff_t pos, unsigned len, unsigned copied, |
| 1728 | struct page *page, void *fsdata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1729 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1730 | handle_t *handle = ext4_journal_current_handle(); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1731 | struct inode *inode = mapping->host; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1732 | int ret = 0, ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1733 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 1734 | trace_ext4_writeback_write_end(inode, pos, len, copied); |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1735 | ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1736 | page, fsdata); |
Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 1737 | copied = ret2; |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1738 | if (pos + len > inode->i_size && ext4_can_truncate(inode)) |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1739 | /* if we have allocated more blocks and copied |
| 1740 | * less. We will have blocks allocated outside |
| 1741 | * inode->i_size. So truncate them |
| 1742 | */ |
| 1743 | ext4_orphan_add(handle, inode); |
| 1744 | |
Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 1745 | if (ret2 < 0) |
| 1746 | ret = ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1747 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1748 | ret2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1749 | if (!ret) |
| 1750 | ret = ret2; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1751 | |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1752 | if (pos + len > inode->i_size) { |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1753 | ext4_truncate(inode); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1754 | /* |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1755 | * If truncate failed early the inode might still be |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1756 | * on the orphan list; we need to make sure the inode |
| 1757 | * is removed from the orphan list in that case. |
| 1758 | */ |
| 1759 | if (inode->i_nlink) |
| 1760 | ext4_orphan_del(NULL, inode); |
| 1761 | } |
| 1762 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1763 | return ret ? ret : copied; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1766 | static int ext4_journalled_write_end(struct file *file, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1767 | struct address_space *mapping, |
| 1768 | loff_t pos, unsigned len, unsigned copied, |
| 1769 | struct page *page, void *fsdata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1770 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1771 | handle_t *handle = ext4_journal_current_handle(); |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1772 | struct inode *inode = mapping->host; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1773 | int ret = 0, ret2; |
| 1774 | int partial = 0; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1775 | unsigned from, to; |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 1776 | loff_t new_i_size; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1777 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 1778 | trace_ext4_journalled_write_end(inode, pos, len, copied); |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1779 | from = pos & (PAGE_CACHE_SIZE - 1); |
| 1780 | to = from + len; |
| 1781 | |
| 1782 | if (copied < len) { |
| 1783 | if (!PageUptodate(page)) |
| 1784 | copied = 0; |
| 1785 | page_zero_new_buffers(page, from+copied, to); |
| 1786 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1787 | |
| 1788 | ret = walk_page_buffers(handle, page_buffers(page), from, |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1789 | to, &partial, write_end_fn); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1790 | if (!partial) |
| 1791 | SetPageUptodate(page); |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 1792 | new_i_size = pos + copied; |
| 1793 | if (new_i_size > inode->i_size) |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1794 | i_size_write(inode, pos+copied); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1795 | EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 1796 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
| 1797 | ext4_update_i_disksize(inode, new_i_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1798 | ret2 = ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1799 | if (!ret) |
| 1800 | ret = ret2; |
| 1801 | } |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1802 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1803 | unlock_page(page); |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1804 | page_cache_release(page); |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1805 | if (pos + len > inode->i_size && ext4_can_truncate(inode)) |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1806 | /* if we have allocated more blocks and copied |
| 1807 | * less. We will have blocks allocated outside |
| 1808 | * inode->i_size. So truncate them |
| 1809 | */ |
| 1810 | ext4_orphan_add(handle, inode); |
| 1811 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1812 | ret2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1813 | if (!ret) |
| 1814 | ret = ret2; |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1815 | if (pos + len > inode->i_size) { |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1816 | ext4_truncate(inode); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1817 | /* |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 1818 | * If truncate failed early the inode might still be |
Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1819 | * on the orphan list; we need to make sure the inode |
| 1820 | * is removed from the orphan list in that case. |
| 1821 | */ |
| 1822 | if (inode->i_nlink) |
| 1823 | ext4_orphan_del(NULL, inode); |
| 1824 | } |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1825 | |
| 1826 | return ret ? ret : copied; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1827 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1828 | |
| 1829 | static int ext4_da_reserve_space(struct inode *inode, int nrblocks) |
| 1830 | { |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1831 | int retries = 0; |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1832 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1833 | unsigned long md_needed, mdblocks, total = 0; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1834 | |
| 1835 | /* |
| 1836 | * recalculate the amount of metadata blocks to reserve |
| 1837 | * in order to allocate nrblocks |
| 1838 | * worse case is one extent per block |
| 1839 | */ |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1840 | repeat: |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1841 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1842 | total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks; |
| 1843 | mdblocks = ext4_calc_metadata_amount(inode, total); |
| 1844 | BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks); |
| 1845 | |
| 1846 | md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks; |
| 1847 | total = md_needed + nrblocks; |
| 1848 | |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1849 | /* |
| 1850 | * Make quota reservation here to prevent quota overflow |
| 1851 | * later. Real quota accounting is done at pages writeout |
| 1852 | * time. |
| 1853 | */ |
| 1854 | if (vfs_dq_reserve_block(inode, total)) { |
| 1855 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1856 | return -EDQUOT; |
| 1857 | } |
| 1858 | |
Aneesh Kumar K.V | a30d542a | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 1859 | if (ext4_claim_free_blocks(sbi, total)) { |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1860 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
Mingming Cao | 9f0ccfd | 2009-09-28 15:49:52 -0400 | [diff] [blame] | 1861 | vfs_dq_release_reservation_block(inode, total); |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1862 | if (ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 1863 | yield(); |
| 1864 | goto repeat; |
| 1865 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1866 | return -ENOSPC; |
| 1867 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1868 | EXT4_I(inode)->i_reserved_data_blocks += nrblocks; |
| 1869 | EXT4_I(inode)->i_reserved_meta_blocks = mdblocks; |
| 1870 | |
| 1871 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1872 | return 0; /* success */ |
| 1873 | } |
| 1874 | |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1875 | static void ext4_da_release_space(struct inode *inode, int to_free) |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1876 | { |
| 1877 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1878 | int total, mdb, mdb_free, release; |
| 1879 | |
Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1880 | if (!to_free) |
| 1881 | return; /* Nothing to release, exit */ |
| 1882 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1883 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1884 | |
| 1885 | if (!EXT4_I(inode)->i_reserved_data_blocks) { |
| 1886 | /* |
| 1887 | * if there is no reserved blocks, but we try to free some |
| 1888 | * then the counter is messed up somewhere. |
| 1889 | * but since this function is called from invalidate |
| 1890 | * page, it's harmless to return without any action |
| 1891 | */ |
| 1892 | printk(KERN_INFO "ext4 delalloc try to release %d reserved " |
| 1893 | "blocks for inode %lu, but there is no reserved " |
| 1894 | "data blocks\n", to_free, inode->i_ino); |
| 1895 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1896 | return; |
| 1897 | } |
| 1898 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1899 | /* recalculate the number of metablocks still need to be reserved */ |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1900 | total = EXT4_I(inode)->i_reserved_data_blocks - to_free; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1901 | mdb = ext4_calc_metadata_amount(inode, total); |
| 1902 | |
| 1903 | /* figure out how many metablocks to release */ |
| 1904 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); |
| 1905 | mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; |
| 1906 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1907 | release = to_free + mdb_free; |
| 1908 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 1909 | /* update fs dirty blocks counter for truncate case */ |
| 1910 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, release); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1911 | |
| 1912 | /* update per-inode reservations */ |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1913 | BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks); |
| 1914 | EXT4_I(inode)->i_reserved_data_blocks -= to_free; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1915 | |
| 1916 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); |
| 1917 | EXT4_I(inode)->i_reserved_meta_blocks = mdb; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1918 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1919 | |
| 1920 | vfs_dq_release_reservation_block(inode, release); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | static void ext4_da_page_release_reservation(struct page *page, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 1924 | unsigned long offset) |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1925 | { |
| 1926 | int to_release = 0; |
| 1927 | struct buffer_head *head, *bh; |
| 1928 | unsigned int curr_off = 0; |
| 1929 | |
| 1930 | head = page_buffers(page); |
| 1931 | bh = head; |
| 1932 | do { |
| 1933 | unsigned int next_off = curr_off + bh->b_size; |
| 1934 | |
| 1935 | if ((offset <= curr_off) && (buffer_delay(bh))) { |
| 1936 | to_release++; |
| 1937 | clear_buffer_delay(bh); |
| 1938 | } |
| 1939 | curr_off = next_off; |
| 1940 | } while ((bh = bh->b_this_page) != head); |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1941 | ext4_da_release_space(page->mapping->host, to_release); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1942 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1943 | |
| 1944 | /* |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1945 | * Delayed allocation stuff |
| 1946 | */ |
| 1947 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1948 | /* |
| 1949 | * 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] | 1950 | * them with writepage() call back |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1951 | * |
| 1952 | * @mpd->inode: inode |
| 1953 | * @mpd->first_page: first page of the extent |
| 1954 | * @mpd->next_page: page after the last page of the extent |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1955 | * |
| 1956 | * By the time mpage_da_submit_io() is called we expect all blocks |
| 1957 | * to be allocated. this may be wrong if allocation failed. |
| 1958 | * |
| 1959 | * As pages are already locked by write_cache_pages(), we can't use it |
| 1960 | */ |
| 1961 | static int mpage_da_submit_io(struct mpage_da_data *mpd) |
| 1962 | { |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 1963 | long pages_skipped; |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1964 | struct pagevec pvec; |
| 1965 | unsigned long index, end; |
| 1966 | int ret = 0, err, nr_pages, i; |
| 1967 | struct inode *inode = mpd->inode; |
| 1968 | struct address_space *mapping = inode->i_mapping; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1969 | |
| 1970 | BUG_ON(mpd->next_page <= mpd->first_page); |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1971 | /* |
| 1972 | * We need to start from the first_page to the next_page - 1 |
| 1973 | * to make sure we also write the mapped dirty buffer_heads. |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 1974 | * 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] | 1975 | * at the currently mapped buffer_heads. |
| 1976 | */ |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1977 | index = mpd->first_page; |
| 1978 | end = mpd->next_page - 1; |
| 1979 | |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1980 | pagevec_init(&pvec, 0); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1981 | while (index <= end) { |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1982 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1983 | if (nr_pages == 0) |
| 1984 | break; |
| 1985 | for (i = 0; i < nr_pages; i++) { |
| 1986 | struct page *page = pvec.pages[i]; |
| 1987 | |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1988 | index = page->index; |
| 1989 | if (index > end) |
| 1990 | break; |
| 1991 | index++; |
| 1992 | |
| 1993 | BUG_ON(!PageLocked(page)); |
| 1994 | BUG_ON(PageWriteback(page)); |
| 1995 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 1996 | pages_skipped = mpd->wbc->pages_skipped; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1997 | err = mapping->a_ops->writepage(page, mpd->wbc); |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 1998 | if (!err && (pages_skipped == mpd->wbc->pages_skipped)) |
| 1999 | /* |
| 2000 | * have successfully written the page |
| 2001 | * without skipping the same |
| 2002 | */ |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2003 | mpd->pages_written++; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2004 | /* |
| 2005 | * In error case, we have to continue because |
| 2006 | * remaining pages are still locked |
| 2007 | * XXX: unlock and re-dirty them? |
| 2008 | */ |
| 2009 | if (ret == 0) |
| 2010 | ret = err; |
| 2011 | } |
| 2012 | pagevec_release(&pvec); |
| 2013 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2014 | return ret; |
| 2015 | } |
| 2016 | |
| 2017 | /* |
| 2018 | * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers |
| 2019 | * |
| 2020 | * @mpd->inode - inode to walk through |
| 2021 | * @exbh->b_blocknr - first block on a disk |
| 2022 | * @exbh->b_size - amount of space in bytes |
| 2023 | * @logical - first logical block to start assignment with |
| 2024 | * |
| 2025 | * the function goes through all passed space and put actual disk |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2026 | * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2027 | */ |
| 2028 | static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical, |
| 2029 | struct buffer_head *exbh) |
| 2030 | { |
| 2031 | struct inode *inode = mpd->inode; |
| 2032 | struct address_space *mapping = inode->i_mapping; |
| 2033 | int blocks = exbh->b_size >> inode->i_blkbits; |
| 2034 | sector_t pblock = exbh->b_blocknr, cur_logical; |
| 2035 | struct buffer_head *head, *bh; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2036 | pgoff_t index, end; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2037 | struct pagevec pvec; |
| 2038 | int nr_pages, i; |
| 2039 | |
| 2040 | index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 2041 | end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 2042 | cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 2043 | |
| 2044 | pagevec_init(&pvec, 0); |
| 2045 | |
| 2046 | while (index <= end) { |
| 2047 | /* XXX: optimize tail */ |
| 2048 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); |
| 2049 | if (nr_pages == 0) |
| 2050 | break; |
| 2051 | for (i = 0; i < nr_pages; i++) { |
| 2052 | struct page *page = pvec.pages[i]; |
| 2053 | |
| 2054 | index = page->index; |
| 2055 | if (index > end) |
| 2056 | break; |
| 2057 | index++; |
| 2058 | |
| 2059 | BUG_ON(!PageLocked(page)); |
| 2060 | BUG_ON(PageWriteback(page)); |
| 2061 | BUG_ON(!page_has_buffers(page)); |
| 2062 | |
| 2063 | bh = page_buffers(page); |
| 2064 | head = bh; |
| 2065 | |
| 2066 | /* skip blocks out of the range */ |
| 2067 | do { |
| 2068 | if (cur_logical >= logical) |
| 2069 | break; |
| 2070 | cur_logical++; |
| 2071 | } while ((bh = bh->b_this_page) != head); |
| 2072 | |
| 2073 | do { |
| 2074 | if (cur_logical >= logical + blocks) |
| 2075 | break; |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2076 | |
| 2077 | if (buffer_delay(bh) || |
| 2078 | buffer_unwritten(bh)) { |
| 2079 | |
| 2080 | BUG_ON(bh->b_bdev != inode->i_sb->s_bdev); |
| 2081 | |
| 2082 | if (buffer_delay(bh)) { |
| 2083 | clear_buffer_delay(bh); |
| 2084 | bh->b_blocknr = pblock; |
| 2085 | } else { |
| 2086 | /* |
| 2087 | * unwritten already should have |
| 2088 | * blocknr assigned. Verify that |
| 2089 | */ |
| 2090 | clear_buffer_unwritten(bh); |
| 2091 | BUG_ON(bh->b_blocknr != pblock); |
| 2092 | } |
| 2093 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2094 | } else if (buffer_mapped(bh)) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2095 | BUG_ON(bh->b_blocknr != pblock); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2096 | |
| 2097 | cur_logical++; |
| 2098 | pblock++; |
| 2099 | } while ((bh = bh->b_this_page) != head); |
| 2100 | } |
| 2101 | pagevec_release(&pvec); |
| 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | |
| 2106 | /* |
| 2107 | * __unmap_underlying_blocks - just a helper function to unmap |
| 2108 | * set of blocks described by @bh |
| 2109 | */ |
| 2110 | static inline void __unmap_underlying_blocks(struct inode *inode, |
| 2111 | struct buffer_head *bh) |
| 2112 | { |
| 2113 | struct block_device *bdev = inode->i_sb->s_bdev; |
| 2114 | int blocks, i; |
| 2115 | |
| 2116 | blocks = bh->b_size >> inode->i_blkbits; |
| 2117 | for (i = 0; i < blocks; i++) |
| 2118 | unmap_underlying_metadata(bdev, bh->b_blocknr + i); |
| 2119 | } |
| 2120 | |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2121 | static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd, |
| 2122 | sector_t logical, long blk_cnt) |
| 2123 | { |
| 2124 | int nr_pages, i; |
| 2125 | pgoff_t index, end; |
| 2126 | struct pagevec pvec; |
| 2127 | struct inode *inode = mpd->inode; |
| 2128 | struct address_space *mapping = inode->i_mapping; |
| 2129 | |
| 2130 | index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 2131 | end = (logical + blk_cnt - 1) >> |
| 2132 | (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 2133 | while (index <= end) { |
| 2134 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); |
| 2135 | if (nr_pages == 0) |
| 2136 | break; |
| 2137 | for (i = 0; i < nr_pages; i++) { |
| 2138 | struct page *page = pvec.pages[i]; |
| 2139 | index = page->index; |
| 2140 | if (index > end) |
| 2141 | break; |
| 2142 | index++; |
| 2143 | |
| 2144 | BUG_ON(!PageLocked(page)); |
| 2145 | BUG_ON(PageWriteback(page)); |
| 2146 | block_invalidatepage(page, 0); |
| 2147 | ClearPageUptodate(page); |
| 2148 | unlock_page(page); |
| 2149 | } |
| 2150 | } |
| 2151 | return; |
| 2152 | } |
| 2153 | |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2154 | static void ext4_print_free_blocks(struct inode *inode) |
| 2155 | { |
| 2156 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Theodore Ts'o | 1693918 | 2009-09-26 17:43:59 -0400 | [diff] [blame] | 2157 | printk(KERN_CRIT "Total free blocks count %lld\n", |
| 2158 | ext4_count_free_blocks(inode->i_sb)); |
| 2159 | printk(KERN_CRIT "Free/Dirty block details\n"); |
| 2160 | printk(KERN_CRIT "free_blocks=%lld\n", |
| 2161 | (long long) percpu_counter_sum(&sbi->s_freeblocks_counter)); |
| 2162 | printk(KERN_CRIT "dirty_blocks=%lld\n", |
| 2163 | (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter)); |
| 2164 | printk(KERN_CRIT "Block reservation details\n"); |
| 2165 | printk(KERN_CRIT "i_reserved_data_blocks=%u\n", |
| 2166 | EXT4_I(inode)->i_reserved_data_blocks); |
| 2167 | printk(KERN_CRIT "i_reserved_meta_blocks=%u\n", |
| 2168 | EXT4_I(inode)->i_reserved_meta_blocks); |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2169 | return; |
| 2170 | } |
| 2171 | |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2172 | /* |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2173 | * mpage_da_map_blocks - go through given space |
| 2174 | * |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2175 | * @mpd - bh describing space |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2176 | * |
| 2177 | * The function skips space we know is already mapped to disk blocks. |
| 2178 | * |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2179 | */ |
Theodore Ts'o | ed5bde0 | 2009-02-23 10:48:07 -0500 | [diff] [blame] | 2180 | static int mpage_da_map_blocks(struct mpage_da_data *mpd) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2181 | { |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 2182 | int err, blks, get_blocks_flags; |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 2183 | struct buffer_head new; |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2184 | sector_t next = mpd->b_blocknr; |
| 2185 | unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; |
| 2186 | loff_t disksize = EXT4_I(mpd->inode)->i_disksize; |
| 2187 | handle_t *handle = NULL; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2188 | |
| 2189 | /* |
| 2190 | * We consider only non-mapped and non-allocated blocks |
| 2191 | */ |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2192 | if ((mpd->b_state & (1 << BH_Mapped)) && |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2193 | !(mpd->b_state & (1 << BH_Delay)) && |
| 2194 | !(mpd->b_state & (1 << BH_Unwritten))) |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2195 | return 0; |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2196 | |
| 2197 | /* |
| 2198 | * If we didn't accumulate anything to write simply return |
| 2199 | */ |
| 2200 | if (!mpd->b_size) |
| 2201 | return 0; |
| 2202 | |
| 2203 | handle = ext4_journal_current_handle(); |
| 2204 | BUG_ON(!handle); |
| 2205 | |
Aneesh Kumar K.V | 79ffab3 | 2009-05-13 15:13:42 -0400 | [diff] [blame] | 2206 | /* |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 2207 | * Call ext4_get_blocks() to allocate any delayed allocation |
| 2208 | * blocks, or to convert an uninitialized extent to be |
| 2209 | * initialized (in the case where we have written into |
| 2210 | * one or more preallocated blocks). |
| 2211 | * |
| 2212 | * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to |
| 2213 | * indicate that we are on the delayed allocation path. This |
| 2214 | * affects functions in many different parts of the allocation |
| 2215 | * call path. This flag exists primarily because we don't |
| 2216 | * want to change *many* call functions, so ext4_get_blocks() |
| 2217 | * will set the magic i_delalloc_reserved_flag once the |
| 2218 | * inode's allocation semaphore is taken. |
| 2219 | * |
| 2220 | * If the blocks in questions were delalloc blocks, set |
| 2221 | * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting |
| 2222 | * variables are updated after the blocks have been allocated. |
Aneesh Kumar K.V | 79ffab3 | 2009-05-13 15:13:42 -0400 | [diff] [blame] | 2223 | */ |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 2224 | new.b_state = 0; |
| 2225 | get_blocks_flags = (EXT4_GET_BLOCKS_CREATE | |
| 2226 | EXT4_GET_BLOCKS_DELALLOC_RESERVE); |
| 2227 | if (mpd->b_state & (1 << BH_Delay)) |
| 2228 | get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE; |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2229 | blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks, |
Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 2230 | &new, get_blocks_flags); |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2231 | if (blks < 0) { |
| 2232 | err = blks; |
Theodore Ts'o | ed5bde0 | 2009-02-23 10:48:07 -0500 | [diff] [blame] | 2233 | /* |
| 2234 | * If get block returns with error we simply |
| 2235 | * return. Later writepage will redirty the page and |
| 2236 | * writepages will find the dirty page again |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2237 | */ |
| 2238 | if (err == -EAGAIN) |
| 2239 | return 0; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2240 | |
| 2241 | if (err == -ENOSPC && |
Theodore Ts'o | ed5bde0 | 2009-02-23 10:48:07 -0500 | [diff] [blame] | 2242 | ext4_count_free_blocks(mpd->inode->i_sb)) { |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2243 | mpd->retval = err; |
| 2244 | return 0; |
| 2245 | } |
| 2246 | |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2247 | /* |
Theodore Ts'o | ed5bde0 | 2009-02-23 10:48:07 -0500 | [diff] [blame] | 2248 | * get block failure will cause us to loop in |
| 2249 | * writepages, because a_ops->writepage won't be able |
| 2250 | * to make progress. The page will be redirtied by |
| 2251 | * writepage and writepages will again try to write |
| 2252 | * the same. |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2253 | */ |
Theodore Ts'o | 1693918 | 2009-09-26 17:43:59 -0400 | [diff] [blame] | 2254 | ext4_msg(mpd->inode->i_sb, KERN_CRIT, |
| 2255 | "delayed block allocation failed for inode %lu at " |
| 2256 | "logical offset %llu with max blocks %zd with " |
| 2257 | "error %d\n", mpd->inode->i_ino, |
| 2258 | (unsigned long long) next, |
| 2259 | mpd->b_size >> mpd->inode->i_blkbits, err); |
| 2260 | printk(KERN_CRIT "This should not happen!! " |
| 2261 | "Data will be lost\n"); |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 2262 | if (err == -ENOSPC) { |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2263 | ext4_print_free_blocks(mpd->inode); |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 2264 | } |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2265 | /* invalidate all the pages */ |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2266 | ext4_da_block_invalidatepages(mpd, next, |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2267 | mpd->b_size >> mpd->inode->i_blkbits); |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2268 | return err; |
| 2269 | } |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2270 | BUG_ON(blks == 0); |
| 2271 | |
| 2272 | new.b_size = (blks << mpd->inode->i_blkbits); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2273 | |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2274 | if (buffer_new(&new)) |
| 2275 | __unmap_underlying_blocks(mpd->inode, &new); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2276 | |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2277 | /* |
| 2278 | * If blocks are delayed marked, we need to |
| 2279 | * put actual blocknr and drop delayed bit |
| 2280 | */ |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2281 | if ((mpd->b_state & (1 << BH_Delay)) || |
| 2282 | (mpd->b_state & (1 << BH_Unwritten))) |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2283 | mpage_put_bnr_to_bhs(mpd, next, &new); |
| 2284 | |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2285 | if (ext4_should_order_data(mpd->inode)) { |
| 2286 | err = ext4_jbd2_file_inode(handle, mpd->inode); |
| 2287 | if (err) |
| 2288 | return err; |
| 2289 | } |
| 2290 | |
| 2291 | /* |
Jan Kara | 03f5d8b | 2009-06-09 00:17:05 -0400 | [diff] [blame] | 2292 | * Update on-disk size along with block allocation. |
Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2293 | */ |
| 2294 | disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; |
| 2295 | if (disksize > i_size_read(mpd->inode)) |
| 2296 | disksize = i_size_read(mpd->inode); |
| 2297 | if (disksize > EXT4_I(mpd->inode)->i_disksize) { |
| 2298 | ext4_update_i_disksize(mpd->inode, disksize); |
| 2299 | return ext4_mark_inode_dirty(handle, mpd->inode); |
| 2300 | } |
| 2301 | |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2302 | return 0; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2303 | } |
| 2304 | |
Aneesh Kumar K.V | bf068ee | 2008-08-19 22:16:43 -0400 | [diff] [blame] | 2305 | #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ |
| 2306 | (1 << BH_Delay) | (1 << BH_Unwritten)) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2307 | |
| 2308 | /* |
| 2309 | * mpage_add_bh_to_extent - try to add one more block to extent of blocks |
| 2310 | * |
| 2311 | * @mpd->lbh - extent of blocks |
| 2312 | * @logical - logical number of the block in the file |
| 2313 | * @bh - bh of the block (used to access block's state) |
| 2314 | * |
| 2315 | * the function is used to collect contig. blocks in same state |
| 2316 | */ |
| 2317 | 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] | 2318 | sector_t logical, size_t b_size, |
| 2319 | unsigned long b_state) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2320 | { |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2321 | sector_t next; |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2322 | int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2323 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2324 | /* check if thereserved journal credits might overflow */ |
| 2325 | if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) { |
| 2326 | if (nrblocks >= EXT4_MAX_TRANS_DATA) { |
| 2327 | /* |
| 2328 | * With non-extent format we are limited by the journal |
| 2329 | * credit available. Total credit needed to insert |
| 2330 | * nrblocks contiguous blocks is dependent on the |
| 2331 | * nrblocks. So limit nrblocks. |
| 2332 | */ |
| 2333 | goto flush_it; |
| 2334 | } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > |
| 2335 | EXT4_MAX_TRANS_DATA) { |
| 2336 | /* |
| 2337 | * Adding the new buffer_head would make it cross the |
| 2338 | * allowed limit for which we have journal credit |
| 2339 | * reserved. So limit the new bh->b_size |
| 2340 | */ |
| 2341 | b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << |
| 2342 | mpd->inode->i_blkbits; |
| 2343 | /* we will do mpage_da_submit_io in the next loop */ |
| 2344 | } |
| 2345 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2346 | /* |
| 2347 | * First block in the extent |
| 2348 | */ |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2349 | if (mpd->b_size == 0) { |
| 2350 | mpd->b_blocknr = logical; |
| 2351 | mpd->b_size = b_size; |
| 2352 | mpd->b_state = b_state & BH_FLAGS; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2353 | return; |
| 2354 | } |
| 2355 | |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2356 | next = mpd->b_blocknr + nrblocks; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2357 | /* |
| 2358 | * Can we merge the block to our big extent? |
| 2359 | */ |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2360 | if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { |
| 2361 | mpd->b_size += b_size; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2362 | return; |
| 2363 | } |
| 2364 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2365 | flush_it: |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2366 | /* |
| 2367 | * We couldn't merge the block to our extent, so we |
| 2368 | * need to flush current extent and start new one |
| 2369 | */ |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2370 | if (mpage_da_map_blocks(mpd) == 0) |
| 2371 | mpage_da_submit_io(mpd); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2372 | mpd->io_done = 1; |
| 2373 | return; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2374 | } |
| 2375 | |
Aneesh Kumar K.V | c364b22 | 2009-06-14 17:57:10 -0400 | [diff] [blame] | 2376 | 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] | 2377 | { |
Aneesh Kumar K.V | c364b22 | 2009-06-14 17:57:10 -0400 | [diff] [blame] | 2378 | return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2379 | } |
| 2380 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2381 | /* |
| 2382 | * __mpage_da_writepage - finds extent of pages and blocks |
| 2383 | * |
| 2384 | * @page: page to consider |
| 2385 | * @wbc: not used, we just follow rules |
| 2386 | * @data: context |
| 2387 | * |
| 2388 | * The function finds extents of pages and scan them for all blocks. |
| 2389 | */ |
| 2390 | static int __mpage_da_writepage(struct page *page, |
| 2391 | struct writeback_control *wbc, void *data) |
| 2392 | { |
| 2393 | struct mpage_da_data *mpd = data; |
| 2394 | struct inode *inode = mpd->inode; |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2395 | struct buffer_head *bh, *head; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2396 | sector_t logical; |
| 2397 | |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2398 | if (mpd->io_done) { |
| 2399 | /* |
| 2400 | * Rest of the page in the page_vec |
| 2401 | * redirty then and skip then. We will |
Anand Gadiyar | fd589a8 | 2009-07-16 17:13:03 +0200 | [diff] [blame] | 2402 | * try to write them again after |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2403 | * starting a new transaction |
| 2404 | */ |
| 2405 | redirty_page_for_writepage(wbc, page); |
| 2406 | unlock_page(page); |
| 2407 | return MPAGE_DA_EXTENT_TAIL; |
| 2408 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2409 | /* |
| 2410 | * Can we merge this page to current extent? |
| 2411 | */ |
| 2412 | if (mpd->next_page != page->index) { |
| 2413 | /* |
| 2414 | * Nope, we can't. So, we map non-allocated blocks |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2415 | * and start IO on them using writepage() |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2416 | */ |
| 2417 | if (mpd->next_page != mpd->first_page) { |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2418 | if (mpage_da_map_blocks(mpd) == 0) |
| 2419 | mpage_da_submit_io(mpd); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2420 | /* |
| 2421 | * skip rest of the page in the page_vec |
| 2422 | */ |
| 2423 | mpd->io_done = 1; |
| 2424 | redirty_page_for_writepage(wbc, page); |
| 2425 | unlock_page(page); |
| 2426 | return MPAGE_DA_EXTENT_TAIL; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2427 | } |
| 2428 | |
| 2429 | /* |
| 2430 | * Start next extent of pages ... |
| 2431 | */ |
| 2432 | mpd->first_page = page->index; |
| 2433 | |
| 2434 | /* |
| 2435 | * ... and blocks |
| 2436 | */ |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2437 | mpd->b_size = 0; |
| 2438 | mpd->b_state = 0; |
| 2439 | mpd->b_blocknr = 0; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | mpd->next_page = page->index + 1; |
| 2443 | logical = (sector_t) page->index << |
| 2444 | (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 2445 | |
| 2446 | if (!page_has_buffers(page)) { |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2447 | mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE, |
| 2448 | (1 << BH_Dirty) | (1 << BH_Uptodate)); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2449 | if (mpd->io_done) |
| 2450 | return MPAGE_DA_EXTENT_TAIL; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2451 | } else { |
| 2452 | /* |
| 2453 | * Page with regular buffer heads, just add all dirty ones |
| 2454 | */ |
| 2455 | head = page_buffers(page); |
| 2456 | bh = head; |
| 2457 | do { |
| 2458 | BUG_ON(buffer_locked(bh)); |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 2459 | /* |
| 2460 | * We need to try to allocate |
| 2461 | * unmapped blocks in the same page. |
| 2462 | * Otherwise we won't make progress |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 2463 | * with the page in ext4_writepage |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 2464 | */ |
Aneesh Kumar K.V | c364b22 | 2009-06-14 17:57:10 -0400 | [diff] [blame] | 2465 | if (ext4_bh_delay_or_unwritten(NULL, bh)) { |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2466 | mpage_add_bh_to_extent(mpd, logical, |
| 2467 | bh->b_size, |
| 2468 | bh->b_state); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2469 | if (mpd->io_done) |
| 2470 | return MPAGE_DA_EXTENT_TAIL; |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 2471 | } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { |
| 2472 | /* |
| 2473 | * mapped dirty buffer. We need to update |
| 2474 | * the b_state because we look at |
| 2475 | * b_state in mpage_da_map_blocks. We don't |
| 2476 | * update b_size because if we find an |
| 2477 | * unmapped buffer_head later we need to |
| 2478 | * use the b_state flag of that buffer_head. |
| 2479 | */ |
Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2480 | if (mpd->b_size == 0) |
| 2481 | mpd->b_state = bh->b_state & BH_FLAGS; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2482 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2483 | logical++; |
| 2484 | } while ((bh = bh->b_this_page) != head); |
| 2485 | } |
| 2486 | |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | /* |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2491 | * This is a special get_blocks_t callback which is used by |
| 2492 | * ext4_da_write_begin(). It will either return mapped block or |
| 2493 | * reserve space for a single block. |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2494 | * |
| 2495 | * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. |
| 2496 | * We also have b_blocknr = -1 and b_bdev initialized properly |
| 2497 | * |
| 2498 | * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. |
| 2499 | * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev |
| 2500 | * initialized properly. |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2501 | */ |
| 2502 | static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, |
| 2503 | struct buffer_head *bh_result, int create) |
| 2504 | { |
| 2505 | int ret = 0; |
Aneesh Kumar K.V | 33b9817 | 2009-05-12 14:40:37 -0400 | [diff] [blame] | 2506 | sector_t invalid_block = ~((sector_t) 0xffff); |
| 2507 | |
| 2508 | if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) |
| 2509 | invalid_block = ~0; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2510 | |
| 2511 | BUG_ON(create == 0); |
| 2512 | BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); |
| 2513 | |
| 2514 | /* |
| 2515 | * first, we need to know whether the block is allocated already |
| 2516 | * preallocated blocks are unmapped but should treated |
| 2517 | * the same as allocated blocks. |
| 2518 | */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2519 | ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2520 | if ((ret == 0) && !buffer_delay(bh_result)) { |
| 2521 | /* the block isn't (pre)allocated yet, let's reserve space */ |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2522 | /* |
| 2523 | * XXX: __block_prepare_write() unmaps passed block, |
| 2524 | * is it OK? |
| 2525 | */ |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2526 | ret = ext4_da_reserve_space(inode, 1); |
| 2527 | if (ret) |
| 2528 | /* not enough space to reserve */ |
| 2529 | return ret; |
| 2530 | |
Aneesh Kumar K.V | 33b9817 | 2009-05-12 14:40:37 -0400 | [diff] [blame] | 2531 | map_bh(bh_result, inode->i_sb, invalid_block); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2532 | set_buffer_new(bh_result); |
| 2533 | set_buffer_delay(bh_result); |
| 2534 | } else if (ret > 0) { |
| 2535 | bh_result->b_size = (ret << inode->i_blkbits); |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2536 | if (buffer_unwritten(bh_result)) { |
| 2537 | /* A delayed write to unwritten bh should |
| 2538 | * be marked new and mapped. Mapped ensures |
| 2539 | * that we don't do get_block multiple times |
| 2540 | * when we write to the same offset and new |
| 2541 | * ensures that we do proper zero out for |
| 2542 | * partial write. |
| 2543 | */ |
Aneesh Kumar K.V | 9c1ee18 | 2009-05-13 18:36:58 -0400 | [diff] [blame] | 2544 | set_buffer_new(bh_result); |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2545 | set_buffer_mapped(bh_result); |
| 2546 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2547 | ret = 0; |
| 2548 | } |
| 2549 | |
| 2550 | return ret; |
| 2551 | } |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2552 | |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2553 | /* |
| 2554 | * This function is used as a standard get_block_t calback function |
| 2555 | * when there is no desire to allocate any blocks. It is used as a |
| 2556 | * callback function for block_prepare_write(), nobh_writepage(), and |
| 2557 | * block_write_full_page(). These functions should only try to map a |
| 2558 | * single block at a time. |
| 2559 | * |
| 2560 | * Since this function doesn't do block allocations even if the caller |
| 2561 | * requests it by passing in create=1, it is critically important that |
| 2562 | * any caller checks to make sure that any buffer heads are returned |
| 2563 | * by this function are either all already mapped or marked for |
| 2564 | * delayed allocation before calling nobh_writepage() or |
| 2565 | * block_write_full_page(). Otherwise, b_blocknr could be left |
| 2566 | * unitialized, and the page write functions will be taken by |
| 2567 | * surprise. |
| 2568 | */ |
| 2569 | 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] | 2570 | struct buffer_head *bh_result, int create) |
| 2571 | { |
| 2572 | int ret = 0; |
| 2573 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; |
| 2574 | |
Theodore Ts'o | a2dc52b | 2009-05-12 13:51:29 -0400 | [diff] [blame] | 2575 | BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); |
| 2576 | |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2577 | /* |
| 2578 | * we don't want to do block allocation in writepage |
| 2579 | * so call get_block_wrap with create = 0 |
| 2580 | */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2581 | ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0); |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2582 | if (ret > 0) { |
| 2583 | bh_result->b_size = (ret << inode->i_blkbits); |
| 2584 | ret = 0; |
| 2585 | } |
| 2586 | return ret; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2587 | } |
| 2588 | |
Aneesh Kumar K.V | 62e086b | 2009-06-14 17:59:34 -0400 | [diff] [blame] | 2589 | static int bget_one(handle_t *handle, struct buffer_head *bh) |
| 2590 | { |
| 2591 | get_bh(bh); |
| 2592 | return 0; |
| 2593 | } |
| 2594 | |
| 2595 | static int bput_one(handle_t *handle, struct buffer_head *bh) |
| 2596 | { |
| 2597 | put_bh(bh); |
| 2598 | return 0; |
| 2599 | } |
| 2600 | |
| 2601 | static int __ext4_journalled_writepage(struct page *page, |
| 2602 | struct writeback_control *wbc, |
| 2603 | unsigned int len) |
| 2604 | { |
| 2605 | struct address_space *mapping = page->mapping; |
| 2606 | struct inode *inode = mapping->host; |
| 2607 | struct buffer_head *page_bufs; |
| 2608 | handle_t *handle = NULL; |
| 2609 | int ret = 0; |
| 2610 | int err; |
| 2611 | |
| 2612 | page_bufs = page_buffers(page); |
| 2613 | BUG_ON(!page_bufs); |
| 2614 | walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); |
| 2615 | /* As soon as we unlock the page, it can go away, but we have |
| 2616 | * references to buffers so we are safe */ |
| 2617 | unlock_page(page); |
| 2618 | |
| 2619 | handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); |
| 2620 | if (IS_ERR(handle)) { |
| 2621 | ret = PTR_ERR(handle); |
| 2622 | goto out; |
| 2623 | } |
| 2624 | |
| 2625 | ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, |
| 2626 | do_journal_get_write_access); |
| 2627 | |
| 2628 | err = walk_page_buffers(handle, page_bufs, 0, len, NULL, |
| 2629 | write_end_fn); |
| 2630 | if (ret == 0) |
| 2631 | ret = err; |
| 2632 | err = ext4_journal_stop(handle); |
| 2633 | if (!ret) |
| 2634 | ret = err; |
| 2635 | |
| 2636 | walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); |
| 2637 | EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; |
| 2638 | out: |
| 2639 | return ret; |
| 2640 | } |
| 2641 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2642 | /* |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 2643 | * Note that we don't need to start a transaction unless we're journaling data |
| 2644 | * because we should have holes filled from ext4_page_mkwrite(). We even don't |
| 2645 | * need to file the inode to the transaction's list in ordered mode because if |
| 2646 | * we are writing back data added by write(), the inode is already there and if |
| 2647 | * we are writing back data modified via mmap(), noone guarantees in which |
| 2648 | * transaction the data will hit the disk. In case we are journaling data, we |
| 2649 | * cannot start transaction directly because transaction start ranks above page |
| 2650 | * lock so we have to do some magic. |
| 2651 | * |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2652 | * This function can get called via... |
| 2653 | * - ext4_da_writepages after taking page lock (have journal handle) |
| 2654 | * - journal_submit_inode_data_buffers (no journal handle) |
| 2655 | * - shrink_page_list via pdflush (no journal handle) |
| 2656 | * - grab_page_cache when doing write_begin (have journal handle) |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 2657 | * |
| 2658 | * We don't do any block allocation in this function. If we have page with |
| 2659 | * multiple blocks we need to write those buffer_heads that are mapped. This |
| 2660 | * is important for mmaped based write. So if we do with blocksize 1K |
| 2661 | * truncate(f, 1024); |
| 2662 | * a = mmap(f, 0, 4096); |
| 2663 | * a[0] = 'a'; |
| 2664 | * truncate(f, 4096); |
| 2665 | * we have in the page first buffer_head mapped via page_mkwrite call back |
| 2666 | * but other bufer_heads would be unmapped but dirty(dirty done via the |
| 2667 | * do_wp_page). So writepage should write the first block. If we modify |
| 2668 | * the mmap area beyond 1024 we will again get a page_fault and the |
| 2669 | * page_mkwrite callback will do the block allocation and mark the |
| 2670 | * buffer_heads mapped. |
| 2671 | * |
| 2672 | * We redirty the page if we have any buffer_heads that is either delay or |
| 2673 | * unwritten in the page. |
| 2674 | * |
| 2675 | * We can get recursively called as show below. |
| 2676 | * |
| 2677 | * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> |
| 2678 | * ext4_writepage() |
| 2679 | * |
| 2680 | * But since we don't do any block allocation we should not deadlock. |
| 2681 | * 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] | 2682 | */ |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 2683 | static int ext4_writepage(struct page *page, |
Aneesh Kumar K.V | 62e086b | 2009-06-14 17:59:34 -0400 | [diff] [blame] | 2684 | struct writeback_control *wbc) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2685 | { |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2686 | int ret = 0; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2687 | loff_t size; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2688 | unsigned int len; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2689 | struct buffer_head *page_bufs; |
| 2690 | struct inode *inode = page->mapping->host; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2691 | |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 2692 | trace_ext4_writepage(inode, page); |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2693 | size = i_size_read(inode); |
| 2694 | if (page->index == size >> PAGE_CACHE_SHIFT) |
| 2695 | len = size & ~PAGE_CACHE_MASK; |
| 2696 | else |
| 2697 | len = PAGE_CACHE_SIZE; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2698 | |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2699 | if (page_has_buffers(page)) { |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2700 | page_bufs = page_buffers(page); |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2701 | if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, |
Aneesh Kumar K.V | c364b22 | 2009-06-14 17:57:10 -0400 | [diff] [blame] | 2702 | ext4_bh_delay_or_unwritten)) { |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2703 | /* |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2704 | * We don't want to do block allocation |
| 2705 | * So redirty the page and return |
Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2706 | * We may reach here when we do a journal commit |
| 2707 | * via journal_submit_inode_data_buffers. |
| 2708 | * If we don't have mapping block we just ignore |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2709 | * them. We can also reach here via shrink_page_list |
| 2710 | */ |
| 2711 | redirty_page_for_writepage(wbc, page); |
| 2712 | unlock_page(page); |
| 2713 | return 0; |
| 2714 | } |
| 2715 | } else { |
| 2716 | /* |
| 2717 | * The test for page_has_buffers() is subtle: |
| 2718 | * We know the page is dirty but it lost buffers. That means |
| 2719 | * that at some moment in time after write_begin()/write_end() |
| 2720 | * has been called all buffers have been clean and thus they |
| 2721 | * must have been written at least once. So they are all |
| 2722 | * mapped and we can happily proceed with mapping them |
| 2723 | * and writing the page. |
| 2724 | * |
| 2725 | * Try to initialize the buffer_heads and check whether |
| 2726 | * all are mapped and non delay. We don't want to |
| 2727 | * do block allocation here. |
| 2728 | */ |
Aneesh Kumar K.V | b767e78 | 2009-06-04 08:06:06 -0400 | [diff] [blame] | 2729 | ret = block_prepare_write(page, 0, len, |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2730 | noalloc_get_block_write); |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2731 | if (!ret) { |
| 2732 | page_bufs = page_buffers(page); |
| 2733 | /* check whether all are mapped and non delay */ |
| 2734 | if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, |
Aneesh Kumar K.V | c364b22 | 2009-06-14 17:57:10 -0400 | [diff] [blame] | 2735 | ext4_bh_delay_or_unwritten)) { |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2736 | redirty_page_for_writepage(wbc, page); |
| 2737 | unlock_page(page); |
| 2738 | return 0; |
| 2739 | } |
| 2740 | } else { |
| 2741 | /* |
| 2742 | * We can't do block allocation here |
| 2743 | * so just redity the page and unlock |
| 2744 | * and return |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2745 | */ |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2746 | redirty_page_for_writepage(wbc, page); |
| 2747 | unlock_page(page); |
| 2748 | return 0; |
| 2749 | } |
Aneesh Kumar K.V | ed9b3e3 | 2008-11-07 09:06:45 -0500 | [diff] [blame] | 2750 | /* now mark the buffer_heads as dirty and uptodate */ |
Aneesh Kumar K.V | b767e78 | 2009-06-04 08:06:06 -0400 | [diff] [blame] | 2751 | block_commit_write(page, 0, len); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2752 | } |
| 2753 | |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 2754 | if (PageChecked(page) && ext4_should_journal_data(inode)) { |
| 2755 | /* |
| 2756 | * It's mmapped pagecache. Add buffers and journal it. There |
| 2757 | * doesn't seem much point in redirtying the page here. |
| 2758 | */ |
| 2759 | ClearPageChecked(page); |
| 2760 | return __ext4_journalled_writepage(page, wbc, len); |
| 2761 | } |
| 2762 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2763 | if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode)) |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2764 | ret = nobh_writepage(page, noalloc_get_block_write, wbc); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2765 | else |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2766 | ret = block_write_full_page(page, noalloc_get_block_write, |
| 2767 | wbc); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2768 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2769 | return ret; |
| 2770 | } |
| 2771 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2772 | /* |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2773 | * This is called via ext4_da_writepages() to |
| 2774 | * calulate the total number of credits to reserve to fit |
| 2775 | * a single extent allocation into a single transaction, |
| 2776 | * ext4_da_writpeages() will loop calling this before |
| 2777 | * the block allocation. |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2778 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2779 | |
| 2780 | static int ext4_da_writepages_trans_blocks(struct inode *inode) |
| 2781 | { |
| 2782 | int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; |
| 2783 | |
| 2784 | /* |
| 2785 | * With non-extent format the journal credit needed to |
| 2786 | * insert nrblocks contiguous block is dependent on |
| 2787 | * number of contiguous block. So we will limit |
| 2788 | * number of contiguous block to a sane value |
| 2789 | */ |
| 2790 | if (!(inode->i_flags & EXT4_EXTENTS_FL) && |
| 2791 | (max_blocks > EXT4_MAX_TRANS_DATA)) |
| 2792 | max_blocks = EXT4_MAX_TRANS_DATA; |
| 2793 | |
| 2794 | return ext4_chunk_trans_blocks(inode, max_blocks); |
| 2795 | } |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2796 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2797 | static int ext4_da_writepages(struct address_space *mapping, |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2798 | struct writeback_control *wbc) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2799 | { |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2800 | pgoff_t index; |
| 2801 | int range_whole = 0; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2802 | handle_t *handle = NULL; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2803 | struct mpage_da_data mpd; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 2804 | struct inode *inode = mapping->host; |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2805 | int no_nrwrite_index_update; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2806 | int pages_written = 0; |
| 2807 | long pages_skipped; |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 2808 | unsigned int max_pages; |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2809 | int range_cyclic, cycled = 1, io_done = 0; |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 2810 | int needed_blocks, ret = 0; |
| 2811 | long desired_nr_to_write, nr_to_writebump = 0; |
Theodore Ts'o | de89de6 | 2009-08-31 17:00:59 -0400 | [diff] [blame] | 2812 | loff_t range_start = wbc->range_start; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 2813 | struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2814 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 2815 | trace_ext4_da_writepages(inode, wbc); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 2816 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2817 | /* |
| 2818 | * No pages to write? This is mainly a kludge to avoid starting |
| 2819 | * a transaction for special inodes like journal inode on last iput() |
| 2820 | * because that could violate lock ordering on umount |
| 2821 | */ |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2822 | if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2823 | return 0; |
Theodore Ts'o | 2a21e37 | 2008-11-05 09:22:24 -0500 | [diff] [blame] | 2824 | |
| 2825 | /* |
| 2826 | * If the filesystem has aborted, it is read-only, so return |
| 2827 | * right away instead of dumping stack traces later on that |
| 2828 | * will obscure the real source of the problem. We test |
Theodore Ts'o | 4ab2f15 | 2009-06-13 10:09:36 -0400 | [diff] [blame] | 2829 | * 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] | 2830 | * the latter could be true if the filesystem is mounted |
| 2831 | * read-only, and in that case, ext4_da_writepages should |
| 2832 | * *never* be called, so if that ever happens, we would want |
| 2833 | * the stack trace. |
| 2834 | */ |
Theodore Ts'o | 4ab2f15 | 2009-06-13 10:09:36 -0400 | [diff] [blame] | 2835 | if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) |
Theodore Ts'o | 2a21e37 | 2008-11-05 09:22:24 -0500 | [diff] [blame] | 2836 | return -EROFS; |
| 2837 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2838 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 2839 | range_whole = 1; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2840 | |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2841 | range_cyclic = wbc->range_cyclic; |
| 2842 | if (wbc->range_cyclic) { |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2843 | index = mapping->writeback_index; |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2844 | if (index) |
| 2845 | cycled = 0; |
| 2846 | wbc->range_start = index << PAGE_CACHE_SHIFT; |
| 2847 | wbc->range_end = LLONG_MAX; |
| 2848 | wbc->range_cyclic = 0; |
| 2849 | } else |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2850 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2851 | |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 2852 | /* |
| 2853 | * This works around two forms of stupidity. The first is in |
| 2854 | * the writeback code, which caps the maximum number of pages |
| 2855 | * written to be 1024 pages. This is wrong on multiple |
| 2856 | * levels; different architectues have a different page size, |
| 2857 | * which changes the maximum amount of data which gets |
| 2858 | * written. Secondly, 4 megabytes is way too small. XFS |
| 2859 | * forces this value to be 16 megabytes by multiplying |
| 2860 | * nr_to_write parameter by four, and then relies on its |
| 2861 | * allocator to allocate larger extents to make them |
| 2862 | * contiguous. Unfortunately this brings us to the second |
| 2863 | * stupidity, which is that ext4's mballoc code only allocates |
| 2864 | * at most 2048 blocks. So we force contiguous writes up to |
| 2865 | * the number of dirty blocks in the inode, or |
| 2866 | * sbi->max_writeback_mb_bump whichever is smaller. |
| 2867 | */ |
| 2868 | max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); |
| 2869 | if (!range_cyclic && range_whole) |
| 2870 | desired_nr_to_write = wbc->nr_to_write * 8; |
| 2871 | else |
| 2872 | desired_nr_to_write = ext4_num_dirty_pages(inode, index, |
| 2873 | max_pages); |
| 2874 | if (desired_nr_to_write > max_pages) |
| 2875 | desired_nr_to_write = max_pages; |
| 2876 | |
| 2877 | if (wbc->nr_to_write < desired_nr_to_write) { |
| 2878 | nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; |
| 2879 | wbc->nr_to_write = desired_nr_to_write; |
| 2880 | } |
| 2881 | |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2882 | mpd.wbc = wbc; |
| 2883 | mpd.inode = mapping->host; |
| 2884 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2885 | /* |
| 2886 | * we don't want write_cache_pages to update |
| 2887 | * nr_to_write and writeback_index |
| 2888 | */ |
| 2889 | no_nrwrite_index_update = wbc->no_nrwrite_index_update; |
| 2890 | wbc->no_nrwrite_index_update = 1; |
| 2891 | pages_skipped = wbc->pages_skipped; |
| 2892 | |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2893 | retry: |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2894 | while (!ret && wbc->nr_to_write > 0) { |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2895 | |
| 2896 | /* |
| 2897 | * we insert one extent at a time. So we need |
| 2898 | * credit needed for single extent allocation. |
| 2899 | * journalled mode is currently not supported |
| 2900 | * by delalloc |
| 2901 | */ |
| 2902 | BUG_ON(ext4_should_journal_data(inode)); |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2903 | needed_blocks = ext4_da_writepages_trans_blocks(inode); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2904 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2905 | /* start a new transaction*/ |
| 2906 | handle = ext4_journal_start(inode, needed_blocks); |
| 2907 | if (IS_ERR(handle)) { |
| 2908 | ret = PTR_ERR(handle); |
Theodore Ts'o | 1693918 | 2009-09-26 17:43:59 -0400 | [diff] [blame] | 2909 | ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2910 | "%ld pages, ino %lu; err %d\n", __func__, |
| 2911 | wbc->nr_to_write, inode->i_ino, ret); |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2912 | goto out_writepages; |
| 2913 | } |
Theodore Ts'o | f63e600 | 2009-02-23 16:42:39 -0500 | [diff] [blame] | 2914 | |
| 2915 | /* |
| 2916 | * Now call __mpage_da_writepage to find the next |
| 2917 | * contiguous region of logical blocks that need |
| 2918 | * blocks to be allocated by ext4. We don't actually |
| 2919 | * submit the blocks for I/O here, even though |
| 2920 | * write_cache_pages thinks it will, and will set the |
| 2921 | * pages as clean for write before calling |
| 2922 | * __mpage_da_writepage(). |
| 2923 | */ |
| 2924 | mpd.b_size = 0; |
| 2925 | mpd.b_state = 0; |
| 2926 | mpd.b_blocknr = 0; |
| 2927 | mpd.first_page = 0; |
| 2928 | mpd.next_page = 0; |
| 2929 | mpd.io_done = 0; |
| 2930 | mpd.pages_written = 0; |
| 2931 | mpd.retval = 0; |
| 2932 | ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, |
| 2933 | &mpd); |
| 2934 | /* |
| 2935 | * If we have a contigous extent of pages and we |
| 2936 | * haven't done the I/O yet, map the blocks and submit |
| 2937 | * them for I/O. |
| 2938 | */ |
| 2939 | if (!mpd.io_done && mpd.next_page != mpd.first_page) { |
| 2940 | if (mpage_da_map_blocks(&mpd) == 0) |
| 2941 | mpage_da_submit_io(&mpd); |
| 2942 | mpd.io_done = 1; |
| 2943 | ret = MPAGE_DA_EXTENT_TAIL; |
| 2944 | } |
Theodore Ts'o | b3a3ca8 | 2009-08-31 23:13:11 -0400 | [diff] [blame] | 2945 | trace_ext4_da_write_pages(inode, &mpd); |
Theodore Ts'o | f63e600 | 2009-02-23 16:42:39 -0500 | [diff] [blame] | 2946 | wbc->nr_to_write -= mpd.pages_written; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2947 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2948 | ext4_journal_stop(handle); |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2949 | |
Eric Sandeen | 8f64b32 | 2009-02-26 00:57:35 -0500 | [diff] [blame] | 2950 | if ((mpd.retval == -ENOSPC) && sbi->s_journal) { |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2951 | /* commit the transaction which would |
| 2952 | * free blocks released in the transaction |
| 2953 | * and try again |
| 2954 | */ |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2955 | jbd2_journal_force_commit_nested(sbi->s_journal); |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2956 | wbc->pages_skipped = pages_skipped; |
| 2957 | ret = 0; |
| 2958 | } else if (ret == MPAGE_DA_EXTENT_TAIL) { |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2959 | /* |
| 2960 | * got one extent now try with |
| 2961 | * rest of the pages |
| 2962 | */ |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2963 | pages_written += mpd.pages_written; |
| 2964 | wbc->pages_skipped = pages_skipped; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2965 | ret = 0; |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2966 | io_done = 1; |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2967 | } else if (wbc->nr_to_write) |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2968 | /* |
| 2969 | * There is no more writeout needed |
| 2970 | * or we requested for a noblocking writeout |
| 2971 | * and we found the device congested |
| 2972 | */ |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2973 | break; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2974 | } |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2975 | if (!io_done && !cycled) { |
| 2976 | cycled = 1; |
| 2977 | index = 0; |
| 2978 | wbc->range_start = index << PAGE_CACHE_SHIFT; |
| 2979 | wbc->range_end = mapping->writeback_index - 1; |
| 2980 | goto retry; |
| 2981 | } |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2982 | if (pages_skipped != wbc->pages_skipped) |
Theodore Ts'o | 1693918 | 2009-09-26 17:43:59 -0400 | [diff] [blame] | 2983 | ext4_msg(inode->i_sb, KERN_CRIT, |
| 2984 | "This should not happen leaving %s " |
| 2985 | "with nr_to_write = %ld ret = %d\n", |
| 2986 | __func__, wbc->nr_to_write, ret); |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2987 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2988 | /* Update index */ |
| 2989 | index += pages_written; |
Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2990 | wbc->range_cyclic = range_cyclic; |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2991 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 2992 | /* |
| 2993 | * set the writeback_index so that range_cyclic |
| 2994 | * mode will write it back later |
| 2995 | */ |
| 2996 | mapping->writeback_index = index; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2997 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2998 | out_writepages: |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2999 | if (!no_nrwrite_index_update) |
| 3000 | wbc->no_nrwrite_index_update = 0; |
Theodore Ts'o | 55138e0 | 2009-09-29 13:31:31 -0400 | [diff] [blame] | 3001 | if (wbc->nr_to_write > nr_to_writebump) |
| 3002 | wbc->nr_to_write -= nr_to_writebump; |
Theodore Ts'o | de89de6 | 2009-08-31 17:00:59 -0400 | [diff] [blame] | 3003 | wbc->range_start = range_start; |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3004 | trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3005 | return ret; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3006 | } |
| 3007 | |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 3008 | #define FALL_BACK_TO_NONDELALLOC 1 |
| 3009 | static int ext4_nonda_switch(struct super_block *sb) |
| 3010 | { |
| 3011 | s64 free_blocks, dirty_blocks; |
| 3012 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3013 | |
| 3014 | /* |
| 3015 | * switch to non delalloc mode if we are running low |
| 3016 | * on free block. The free block accounting via percpu |
Eric Dumazet | 179f7eb | 2009-01-06 14:41:04 -0800 | [diff] [blame] | 3017 | * counters can get slightly wrong with percpu_counter_batch getting |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 3018 | * accumulated on each CPU without updating global counters |
| 3019 | * Delalloc need an accurate free block accounting. So switch |
| 3020 | * to non delalloc when we are near to error range. |
| 3021 | */ |
| 3022 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); |
| 3023 | dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); |
| 3024 | if (2 * free_blocks < 3 * dirty_blocks || |
| 3025 | free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { |
| 3026 | /* |
| 3027 | * free block count is less that 150% of dirty blocks |
| 3028 | * or free blocks is less that watermark |
| 3029 | */ |
| 3030 | return 1; |
| 3031 | } |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3035 | 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] | 3036 | loff_t pos, unsigned len, unsigned flags, |
| 3037 | struct page **pagep, void **fsdata) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3038 | { |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 3039 | int ret, retries = 0; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3040 | struct page *page; |
| 3041 | pgoff_t index; |
| 3042 | unsigned from, to; |
| 3043 | struct inode *inode = mapping->host; |
| 3044 | handle_t *handle; |
| 3045 | |
| 3046 | index = pos >> PAGE_CACHE_SHIFT; |
| 3047 | from = pos & (PAGE_CACHE_SIZE - 1); |
| 3048 | to = from + len; |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 3049 | |
| 3050 | if (ext4_nonda_switch(inode->i_sb)) { |
| 3051 | *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; |
| 3052 | return ext4_write_begin(file, mapping, pos, |
| 3053 | len, flags, pagep, fsdata); |
| 3054 | } |
| 3055 | *fsdata = (void *)0; |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3056 | trace_ext4_da_write_begin(inode, pos, len, flags); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 3057 | retry: |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3058 | /* |
| 3059 | * With delayed allocation, we don't log the i_disksize update |
| 3060 | * if there is delayed block allocation. But we still need |
| 3061 | * to journalling the i_disksize update if writes to the end |
| 3062 | * of file which has an already mapped buffer. |
| 3063 | */ |
| 3064 | handle = ext4_journal_start(inode, 1); |
| 3065 | if (IS_ERR(handle)) { |
| 3066 | ret = PTR_ERR(handle); |
| 3067 | goto out; |
| 3068 | } |
Jan Kara | ebd3610 | 2009-02-22 21:09:59 -0500 | [diff] [blame] | 3069 | /* We cannot recurse into the filesystem as the transaction is already |
| 3070 | * started */ |
| 3071 | flags |= AOP_FLAG_NOFS; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3072 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3073 | page = grab_cache_page_write_begin(mapping, index, flags); |
Eric Sandeen | d5a0d4f | 2008-08-02 18:51:06 -0400 | [diff] [blame] | 3074 | if (!page) { |
| 3075 | ext4_journal_stop(handle); |
| 3076 | ret = -ENOMEM; |
| 3077 | goto out; |
| 3078 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3079 | *pagep = page; |
| 3080 | |
| 3081 | ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 3082 | ext4_da_get_block_prep); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3083 | if (ret < 0) { |
| 3084 | unlock_page(page); |
| 3085 | ext4_journal_stop(handle); |
| 3086 | page_cache_release(page); |
Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 3087 | /* |
| 3088 | * block_write_begin may have instantiated a few blocks |
| 3089 | * outside i_size. Trim these off again. Don't need |
| 3090 | * i_size_read because we hold i_mutex. |
| 3091 | */ |
| 3092 | if (pos + len > inode->i_size) |
Jan Kara | ffacfa7 | 2009-07-13 16:22:22 -0400 | [diff] [blame] | 3093 | ext4_truncate(inode); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3094 | } |
| 3095 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 3096 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
| 3097 | goto retry; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3098 | out: |
| 3099 | return ret; |
| 3100 | } |
| 3101 | |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3102 | /* |
| 3103 | * Check if we should update i_disksize |
| 3104 | * when write to the end of file but not require block allocation |
| 3105 | */ |
| 3106 | static int ext4_da_should_update_i_disksize(struct page *page, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 3107 | unsigned long offset) |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3108 | { |
| 3109 | struct buffer_head *bh; |
| 3110 | struct inode *inode = page->mapping->host; |
| 3111 | unsigned int idx; |
| 3112 | int i; |
| 3113 | |
| 3114 | bh = page_buffers(page); |
| 3115 | idx = offset >> inode->i_blkbits; |
| 3116 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3117 | for (i = 0; i < idx; i++) |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3118 | bh = bh->b_this_page; |
| 3119 | |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 3120 | if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3121 | return 0; |
| 3122 | return 1; |
| 3123 | } |
| 3124 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3125 | static int ext4_da_write_end(struct file *file, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 3126 | struct address_space *mapping, |
| 3127 | loff_t pos, unsigned len, unsigned copied, |
| 3128 | struct page *page, void *fsdata) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3129 | { |
| 3130 | struct inode *inode = mapping->host; |
| 3131 | int ret = 0, ret2; |
| 3132 | handle_t *handle = ext4_journal_current_handle(); |
| 3133 | loff_t new_i_size; |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3134 | unsigned long start, end; |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 3135 | int write_mode = (int)(unsigned long)fsdata; |
| 3136 | |
| 3137 | if (write_mode == FALL_BACK_TO_NONDELALLOC) { |
| 3138 | if (ext4_should_order_data(inode)) { |
| 3139 | return ext4_ordered_write_end(file, mapping, pos, |
| 3140 | len, copied, page, fsdata); |
| 3141 | } else if (ext4_should_writeback_data(inode)) { |
| 3142 | return ext4_writeback_write_end(file, mapping, pos, |
| 3143 | len, copied, page, fsdata); |
| 3144 | } else { |
| 3145 | BUG(); |
| 3146 | } |
| 3147 | } |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3148 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3149 | trace_ext4_da_write_end(inode, pos, len, copied); |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3150 | start = pos & (PAGE_CACHE_SIZE - 1); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3151 | end = start + copied - 1; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3152 | |
| 3153 | /* |
| 3154 | * generic_write_end() will run mark_inode_dirty() if i_size |
| 3155 | * changes. So let's piggyback the i_disksize mark_inode_dirty |
| 3156 | * into that. |
| 3157 | */ |
| 3158 | |
| 3159 | new_i_size = pos + copied; |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3160 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
| 3161 | if (ext4_da_should_update_i_disksize(page, end)) { |
| 3162 | down_write(&EXT4_I(inode)->i_data_sem); |
| 3163 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
| 3164 | /* |
| 3165 | * Updating i_disksize when extending file |
| 3166 | * without needing block allocation |
| 3167 | */ |
| 3168 | if (ext4_should_order_data(inode)) |
| 3169 | ret = ext4_jbd2_file_inode(handle, |
| 3170 | inode); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3171 | |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3172 | EXT4_I(inode)->i_disksize = new_i_size; |
| 3173 | } |
| 3174 | up_write(&EXT4_I(inode)->i_data_sem); |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 3175 | /* We need to mark inode dirty even if |
| 3176 | * new_i_size is less that inode->i_size |
| 3177 | * bu greater than i_disksize.(hint delalloc) |
| 3178 | */ |
| 3179 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3180 | } |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3181 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3182 | ret2 = generic_write_end(file, mapping, pos, len, copied, |
| 3183 | page, fsdata); |
| 3184 | copied = ret2; |
| 3185 | if (ret2 < 0) |
| 3186 | ret = ret2; |
| 3187 | ret2 = ext4_journal_stop(handle); |
| 3188 | if (!ret) |
| 3189 | ret = ret2; |
| 3190 | |
| 3191 | return ret ? ret : copied; |
| 3192 | } |
| 3193 | |
| 3194 | static void ext4_da_invalidatepage(struct page *page, unsigned long offset) |
| 3195 | { |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3196 | /* |
| 3197 | * Drop reserved blocks |
| 3198 | */ |
| 3199 | BUG_ON(!PageLocked(page)); |
| 3200 | if (!page_has_buffers(page)) |
| 3201 | goto out; |
| 3202 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 3203 | ext4_da_page_release_reservation(page, offset); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3204 | |
| 3205 | out: |
| 3206 | ext4_invalidatepage(page, offset); |
| 3207 | |
| 3208 | return; |
| 3209 | } |
| 3210 | |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 3211 | /* |
| 3212 | * Force all delayed allocation blocks to be allocated for a given inode. |
| 3213 | */ |
| 3214 | int ext4_alloc_da_blocks(struct inode *inode) |
| 3215 | { |
Theodore Ts'o | fb40ba0 | 2009-09-16 19:30:40 -0400 | [diff] [blame] | 3216 | trace_ext4_alloc_da_blocks(inode); |
| 3217 | |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 3218 | if (!EXT4_I(inode)->i_reserved_data_blocks && |
| 3219 | !EXT4_I(inode)->i_reserved_meta_blocks) |
| 3220 | return 0; |
| 3221 | |
| 3222 | /* |
| 3223 | * We do something simple for now. The filemap_flush() will |
| 3224 | * also start triggering a write of the data blocks, which is |
| 3225 | * not strictly speaking necessary (and for users of |
| 3226 | * laptop_mode, not even desirable). However, to do otherwise |
| 3227 | * would require replicating code paths in: |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 3228 | * |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 3229 | * ext4_da_writepages() -> |
| 3230 | * write_cache_pages() ---> (via passed in callback function) |
| 3231 | * __mpage_da_writepage() --> |
| 3232 | * mpage_add_bh_to_extent() |
| 3233 | * mpage_da_map_blocks() |
| 3234 | * |
| 3235 | * The problem is that write_cache_pages(), located in |
| 3236 | * mm/page-writeback.c, marks pages clean in preparation for |
| 3237 | * doing I/O, which is not desirable if we're not planning on |
| 3238 | * doing I/O at all. |
| 3239 | * |
| 3240 | * We could call write_cache_pages(), and then redirty all of |
| 3241 | * the pages by calling redirty_page_for_writeback() but that |
| 3242 | * would be ugly in the extreme. So instead we would need to |
| 3243 | * replicate parts of the code in the above functions, |
| 3244 | * simplifying them becuase we wouldn't actually intend to |
| 3245 | * write out the pages, but rather only collect contiguous |
| 3246 | * logical block extents, call the multi-block allocator, and |
| 3247 | * then update the buffer heads with the block allocations. |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 3248 | * |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 3249 | * For now, though, we'll cheat by calling filemap_flush(), |
| 3250 | * which will map the blocks, and start the I/O, but not |
| 3251 | * actually wait for the I/O to complete. |
| 3252 | */ |
| 3253 | return filemap_flush(inode->i_mapping); |
| 3254 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3255 | |
| 3256 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3257 | * bmap() is special. It gets used by applications such as lilo and by |
| 3258 | * the swapper to find the on-disk block of a specific piece of data. |
| 3259 | * |
| 3260 | * Naturally, this is dangerous if the block concerned is still in the |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3261 | * journal. If somebody makes a swapfile on an ext4 data-journaling |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3262 | * filesystem and enables swap, then they may get a nasty shock when the |
| 3263 | * data getting swapped to that swapfile suddenly gets overwritten by |
| 3264 | * the original zero's written out previously to the journal and |
| 3265 | * awaiting writeback in the kernel's buffer cache. |
| 3266 | * |
| 3267 | * So, if we see any bmap calls here on a modified, data-journaled file, |
| 3268 | * take extra steps to flush any blocks which might be in the cache. |
| 3269 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3270 | static sector_t ext4_bmap(struct address_space *mapping, sector_t block) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3271 | { |
| 3272 | struct inode *inode = mapping->host; |
| 3273 | journal_t *journal; |
| 3274 | int err; |
| 3275 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3276 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && |
| 3277 | test_opt(inode->i_sb, DELALLOC)) { |
| 3278 | /* |
| 3279 | * With delalloc we want to sync the file |
| 3280 | * so that we can make sure we allocate |
| 3281 | * blocks for file |
| 3282 | */ |
| 3283 | filemap_write_and_wait(mapping); |
| 3284 | } |
| 3285 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3286 | if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3287 | /* |
| 3288 | * This is a REALLY heavyweight approach, but the use of |
| 3289 | * bmap on dirty files is expected to be extremely rare: |
| 3290 | * only if we run lilo or swapon on a freshly made file |
| 3291 | * do we expect this to happen. |
| 3292 | * |
| 3293 | * (bmap requires CAP_SYS_RAWIO so this does not |
| 3294 | * represent an unprivileged user DOS attack --- we'd be |
| 3295 | * in trouble if mortal users could trigger this path at |
| 3296 | * will.) |
| 3297 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3298 | * NB. EXT4_STATE_JDATA is not set on files other than |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3299 | * regular files. If somebody wants to bmap a directory |
| 3300 | * or symlink and gets confused because the buffer |
| 3301 | * hasn't yet been flushed to disk, they deserve |
| 3302 | * everything they get. |
| 3303 | */ |
| 3304 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3305 | EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA; |
| 3306 | journal = EXT4_JOURNAL(inode); |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3307 | jbd2_journal_lock_updates(journal); |
| 3308 | err = jbd2_journal_flush(journal); |
| 3309 | jbd2_journal_unlock_updates(journal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3310 | |
| 3311 | if (err) |
| 3312 | return 0; |
| 3313 | } |
| 3314 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3315 | return generic_block_bmap(mapping, block, ext4_get_block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3316 | } |
| 3317 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3318 | static int ext4_readpage(struct file *file, struct page *page) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3319 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3320 | return mpage_readpage(page, ext4_get_block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3324 | ext4_readpages(struct file *file, struct address_space *mapping, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3325 | struct list_head *pages, unsigned nr_pages) |
| 3326 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3327 | return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3328 | } |
| 3329 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3330 | static void ext4_invalidatepage(struct page *page, unsigned long offset) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3331 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3332 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3333 | |
| 3334 | /* |
| 3335 | * If it's a full truncate we just forget about the pending dirtying |
| 3336 | */ |
| 3337 | if (offset == 0) |
| 3338 | ClearPageChecked(page); |
| 3339 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3340 | if (journal) |
| 3341 | jbd2_journal_invalidatepage(journal, page, offset); |
| 3342 | else |
| 3343 | block_invalidatepage(page, offset); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3344 | } |
| 3345 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3346 | static int ext4_releasepage(struct page *page, gfp_t wait) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3347 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3348 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3349 | |
| 3350 | WARN_ON(PageChecked(page)); |
| 3351 | if (!page_has_buffers(page)) |
| 3352 | return 0; |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3353 | if (journal) |
| 3354 | return jbd2_journal_try_to_free_buffers(journal, page, wait); |
| 3355 | else |
| 3356 | return try_to_free_buffers(page); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | /* |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3360 | * O_DIRECT for ext3 (or indirect map) based files |
| 3361 | * |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3362 | * If the O_DIRECT write will extend the file then add this inode to the |
| 3363 | * orphan list. So recovery will truncate it back to the original size |
| 3364 | * if the machine crashes during the write. |
| 3365 | * |
| 3366 | * If the O_DIRECT write is intantiating holes inside i_size and the machine |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3367 | * crashes then stale disk data _may_ be exposed inside the file. But current |
| 3368 | * VFS code falls back into buffered path in that case so we are safe. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3369 | */ |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3370 | static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 3371 | const struct iovec *iov, loff_t offset, |
| 3372 | unsigned long nr_segs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3373 | { |
| 3374 | struct file *file = iocb->ki_filp; |
| 3375 | struct inode *inode = file->f_mapping->host; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3376 | struct ext4_inode_info *ei = EXT4_I(inode); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3377 | handle_t *handle; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3378 | ssize_t ret; |
| 3379 | int orphan = 0; |
| 3380 | size_t count = iov_length(iov, nr_segs); |
| 3381 | |
| 3382 | if (rw == WRITE) { |
| 3383 | loff_t final_size = offset + count; |
| 3384 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3385 | if (final_size > inode->i_size) { |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3386 | /* Credits for sb + inode write */ |
| 3387 | handle = ext4_journal_start(inode, 2); |
| 3388 | if (IS_ERR(handle)) { |
| 3389 | ret = PTR_ERR(handle); |
| 3390 | goto out; |
| 3391 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3392 | ret = ext4_orphan_add(handle, inode); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3393 | if (ret) { |
| 3394 | ext4_journal_stop(handle); |
| 3395 | goto out; |
| 3396 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3397 | orphan = 1; |
| 3398 | ei->i_disksize = inode->i_size; |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3399 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3400 | } |
| 3401 | } |
| 3402 | |
| 3403 | ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, |
| 3404 | offset, nr_segs, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3405 | ext4_get_block, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3406 | |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3407 | if (orphan) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3408 | int err; |
| 3409 | |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3410 | /* Credits for sb + inode write */ |
| 3411 | handle = ext4_journal_start(inode, 2); |
| 3412 | if (IS_ERR(handle)) { |
| 3413 | /* This is really bad luck. We've written the data |
| 3414 | * but cannot extend i_size. Bail out and pretend |
| 3415 | * the write failed... */ |
| 3416 | ret = PTR_ERR(handle); |
| 3417 | goto out; |
| 3418 | } |
| 3419 | if (inode->i_nlink) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3420 | ext4_orphan_del(handle, inode); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3421 | if (ret > 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3422 | loff_t end = offset + ret; |
| 3423 | if (end > inode->i_size) { |
| 3424 | ei->i_disksize = end; |
| 3425 | i_size_write(inode, end); |
| 3426 | /* |
| 3427 | * We're going to return a positive `ret' |
| 3428 | * here due to non-zero-length I/O, so there's |
| 3429 | * no way of reporting error returns from |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3430 | * ext4_mark_inode_dirty() to userspace. So |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3431 | * ignore it. |
| 3432 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3433 | ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3434 | } |
| 3435 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3436 | err = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3437 | if (ret == 0) |
| 3438 | ret = err; |
| 3439 | } |
| 3440 | out: |
| 3441 | return ret; |
| 3442 | } |
| 3443 | |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3444 | /* Maximum number of blocks we map for direct IO at once. */ |
| 3445 | |
| 3446 | static int ext4_get_block_dio_write(struct inode *inode, sector_t iblock, |
| 3447 | struct buffer_head *bh_result, int create) |
| 3448 | { |
| 3449 | handle_t *handle = NULL; |
| 3450 | int ret = 0; |
| 3451 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; |
| 3452 | int dio_credits; |
| 3453 | |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3454 | ext4_debug("ext4_get_block_dio_write: inode %lu, create flag %d\n", |
| 3455 | inode->i_ino, create); |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3456 | /* |
| 3457 | * DIO VFS code passes create = 0 flag for write to |
| 3458 | * the middle of file. It does this to avoid block |
| 3459 | * allocation for holes, to prevent expose stale data |
| 3460 | * out when there is parallel buffered read (which does |
| 3461 | * not hold the i_mutex lock) while direct IO write has |
| 3462 | * not completed. DIO request on holes finally falls back |
| 3463 | * to buffered IO for this reason. |
| 3464 | * |
| 3465 | * For ext4 extent based file, since we support fallocate, |
| 3466 | * new allocated extent as uninitialized, for holes, we |
| 3467 | * could fallocate blocks for holes, thus parallel |
| 3468 | * buffered IO read will zero out the page when read on |
| 3469 | * a hole while parallel DIO write to the hole has not completed. |
| 3470 | * |
| 3471 | * when we come here, we know it's a direct IO write to |
| 3472 | * to the middle of file (<i_size) |
| 3473 | * so it's safe to override the create flag from VFS. |
| 3474 | */ |
| 3475 | create = EXT4_GET_BLOCKS_DIO_CREATE_EXT; |
| 3476 | |
| 3477 | if (max_blocks > DIO_MAX_BLOCKS) |
| 3478 | max_blocks = DIO_MAX_BLOCKS; |
| 3479 | dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); |
| 3480 | handle = ext4_journal_start(inode, dio_credits); |
| 3481 | if (IS_ERR(handle)) { |
| 3482 | ret = PTR_ERR(handle); |
| 3483 | goto out; |
| 3484 | } |
| 3485 | ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result, |
| 3486 | create); |
| 3487 | if (ret > 0) { |
| 3488 | bh_result->b_size = (ret << inode->i_blkbits); |
| 3489 | ret = 0; |
| 3490 | } |
| 3491 | ext4_journal_stop(handle); |
| 3492 | out: |
| 3493 | return ret; |
| 3494 | } |
| 3495 | |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3496 | static void ext4_free_io_end(ext4_io_end_t *io) |
| 3497 | { |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3498 | BUG_ON(!io); |
| 3499 | iput(io->inode); |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3500 | kfree(io); |
| 3501 | } |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3502 | static void dump_aio_dio_list(struct inode * inode) |
| 3503 | { |
| 3504 | #ifdef EXT4_DEBUG |
| 3505 | struct list_head *cur, *before, *after; |
| 3506 | ext4_io_end_t *io, *io0, *io1; |
| 3507 | |
| 3508 | if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){ |
| 3509 | ext4_debug("inode %lu aio dio list is empty\n", inode->i_ino); |
| 3510 | return; |
| 3511 | } |
| 3512 | |
| 3513 | ext4_debug("Dump inode %lu aio_dio_completed_IO list \n", inode->i_ino); |
| 3514 | list_for_each_entry(io, &EXT4_I(inode)->i_aio_dio_complete_list, list){ |
| 3515 | cur = &io->list; |
| 3516 | before = cur->prev; |
| 3517 | io0 = container_of(before, ext4_io_end_t, list); |
| 3518 | after = cur->next; |
| 3519 | io1 = container_of(after, ext4_io_end_t, list); |
| 3520 | |
| 3521 | ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n", |
| 3522 | io, inode->i_ino, io0, io1); |
| 3523 | } |
| 3524 | #endif |
| 3525 | } |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3526 | |
| 3527 | /* |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3528 | * check a range of space and convert unwritten extents to written. |
| 3529 | */ |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3530 | static int ext4_end_aio_dio_nolock(ext4_io_end_t *io) |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3531 | { |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3532 | struct inode *inode = io->inode; |
| 3533 | loff_t offset = io->offset; |
| 3534 | size_t size = io->size; |
| 3535 | int ret = 0; |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3536 | |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3537 | ext4_debug("end_aio_dio_onlock: io 0x%p from inode %lu,list->next 0x%p," |
| 3538 | "list->prev 0x%p\n", |
| 3539 | io, inode->i_ino, io->list.next, io->list.prev); |
| 3540 | |
| 3541 | if (list_empty(&io->list)) |
| 3542 | return ret; |
| 3543 | |
| 3544 | if (io->flag != DIO_AIO_UNWRITTEN) |
| 3545 | return ret; |
| 3546 | |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3547 | if (offset + size <= i_size_read(inode)) |
| 3548 | ret = ext4_convert_unwritten_extents(inode, offset, size); |
| 3549 | |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3550 | if (ret < 0) { |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3551 | printk(KERN_EMERG "%s: failed to convert unwritten" |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3552 | "extents to written extents, error is %d" |
| 3553 | " io is still on inode %lu aio dio list\n", |
| 3554 | __func__, ret, inode->i_ino); |
| 3555 | return ret; |
| 3556 | } |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3557 | |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3558 | /* clear the DIO AIO unwritten flag */ |
| 3559 | io->flag = 0; |
| 3560 | return ret; |
| 3561 | } |
| 3562 | /* |
| 3563 | * work on completed aio dio IO, to convert unwritten extents to extents |
| 3564 | */ |
| 3565 | static void ext4_end_aio_dio_work(struct work_struct *work) |
| 3566 | { |
| 3567 | ext4_io_end_t *io = container_of(work, ext4_io_end_t, work); |
| 3568 | struct inode *inode = io->inode; |
| 3569 | int ret = 0; |
| 3570 | |
| 3571 | mutex_lock(&inode->i_mutex); |
| 3572 | ret = ext4_end_aio_dio_nolock(io); |
| 3573 | if (ret >= 0) { |
| 3574 | if (!list_empty(&io->list)) |
| 3575 | list_del_init(&io->list); |
| 3576 | ext4_free_io_end(io); |
| 3577 | } |
| 3578 | mutex_unlock(&inode->i_mutex); |
| 3579 | } |
| 3580 | /* |
| 3581 | * This function is called from ext4_sync_file(). |
| 3582 | * |
| 3583 | * When AIO DIO IO is completed, the work to convert unwritten |
| 3584 | * extents to written is queued on workqueue but may not get immediately |
| 3585 | * scheduled. When fsync is called, we need to ensure the |
| 3586 | * conversion is complete before fsync returns. |
| 3587 | * The inode keeps track of a list of completed AIO from DIO path |
| 3588 | * that might needs to do the conversion. This function walks through |
| 3589 | * the list and convert the related unwritten extents to written. |
| 3590 | */ |
| 3591 | int flush_aio_dio_completed_IO(struct inode *inode) |
| 3592 | { |
| 3593 | ext4_io_end_t *io; |
| 3594 | int ret = 0; |
| 3595 | int ret2 = 0; |
| 3596 | |
| 3597 | if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)) |
| 3598 | return ret; |
| 3599 | |
| 3600 | dump_aio_dio_list(inode); |
| 3601 | while (!list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){ |
| 3602 | io = list_entry(EXT4_I(inode)->i_aio_dio_complete_list.next, |
| 3603 | ext4_io_end_t, list); |
| 3604 | /* |
| 3605 | * Calling ext4_end_aio_dio_nolock() to convert completed |
| 3606 | * IO to written. |
| 3607 | * |
| 3608 | * When ext4_sync_file() is called, run_queue() may already |
| 3609 | * about to flush the work corresponding to this io structure. |
| 3610 | * It will be upset if it founds the io structure related |
| 3611 | * to the work-to-be schedule is freed. |
| 3612 | * |
| 3613 | * Thus we need to keep the io structure still valid here after |
| 3614 | * convertion finished. The io structure has a flag to |
| 3615 | * avoid double converting from both fsync and background work |
| 3616 | * queue work. |
| 3617 | */ |
| 3618 | ret = ext4_end_aio_dio_nolock(io); |
| 3619 | if (ret < 0) |
| 3620 | ret2 = ret; |
| 3621 | else |
| 3622 | list_del_init(&io->list); |
| 3623 | } |
| 3624 | return (ret2 < 0) ? ret2 : 0; |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3625 | } |
| 3626 | |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3627 | static ext4_io_end_t *ext4_init_io_end (struct inode *inode) |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3628 | { |
| 3629 | ext4_io_end_t *io = NULL; |
| 3630 | |
| 3631 | io = kmalloc(sizeof(*io), GFP_NOFS); |
| 3632 | |
| 3633 | if (io) { |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3634 | igrab(inode); |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3635 | io->inode = inode; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3636 | io->flag = 0; |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3637 | io->offset = 0; |
| 3638 | io->size = 0; |
| 3639 | io->error = 0; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3640 | INIT_WORK(&io->work, ext4_end_aio_dio_work); |
| 3641 | INIT_LIST_HEAD(&io->list); |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | return io; |
| 3645 | } |
| 3646 | |
| 3647 | static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, |
| 3648 | ssize_t size, void *private) |
| 3649 | { |
| 3650 | ext4_io_end_t *io_end = iocb->private; |
| 3651 | struct workqueue_struct *wq; |
| 3652 | |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3653 | ext_debug("ext4_end_io_dio(): io_end 0x%p" |
| 3654 | "for inode %lu, iocb 0x%p, offset %llu, size %llu\n", |
| 3655 | iocb->private, io_end->inode->i_ino, iocb, offset, |
| 3656 | size); |
| 3657 | /* if not async direct IO or dio with 0 bytes write, just return */ |
| 3658 | if (!io_end || !size) |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3659 | return; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3660 | |
| 3661 | /* if not aio dio with unwritten extents, just free io and return */ |
| 3662 | if (io_end->flag != DIO_AIO_UNWRITTEN){ |
| 3663 | ext4_free_io_end(io_end); |
| 3664 | iocb->private = NULL; |
| 3665 | return; |
| 3666 | } |
| 3667 | |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3668 | io_end->offset = offset; |
| 3669 | io_end->size = size; |
| 3670 | wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq; |
| 3671 | |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3672 | /* queue the work to convert unwritten extents to written */ |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3673 | queue_work(wq, &io_end->work); |
| 3674 | |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3675 | /* Add the io_end to per-inode completed aio dio list*/ |
| 3676 | list_add_tail(&io_end->list, |
| 3677 | &EXT4_I(io_end->inode)->i_aio_dio_complete_list); |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3678 | iocb->private = NULL; |
| 3679 | } |
| 3680 | /* |
| 3681 | * For ext4 extent files, ext4 will do direct-io write to holes, |
| 3682 | * preallocated extents, and those write extend the file, no need to |
| 3683 | * fall back to buffered IO. |
| 3684 | * |
| 3685 | * For holes, we fallocate those blocks, mark them as unintialized |
| 3686 | * If those blocks were preallocated, we mark sure they are splited, but |
| 3687 | * still keep the range to write as unintialized. |
| 3688 | * |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3689 | * The unwrritten extents will be converted to written when DIO is completed. |
| 3690 | * For async direct IO, since the IO may still pending when return, we |
| 3691 | * set up an end_io call back function, which will do the convertion |
| 3692 | * when async direct IO completed. |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3693 | * |
| 3694 | * If the O_DIRECT write will extend the file then add this inode to the |
| 3695 | * orphan list. So recovery will truncate it back to the original size |
| 3696 | * if the machine crashes during the write. |
| 3697 | * |
| 3698 | */ |
| 3699 | static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, |
| 3700 | const struct iovec *iov, loff_t offset, |
| 3701 | unsigned long nr_segs) |
| 3702 | { |
| 3703 | struct file *file = iocb->ki_filp; |
| 3704 | struct inode *inode = file->f_mapping->host; |
| 3705 | ssize_t ret; |
| 3706 | size_t count = iov_length(iov, nr_segs); |
| 3707 | |
| 3708 | loff_t final_size = offset + count; |
| 3709 | if (rw == WRITE && final_size <= inode->i_size) { |
| 3710 | /* |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3711 | * We could direct write to holes and fallocate. |
| 3712 | * |
| 3713 | * Allocated blocks to fill the hole are marked as uninitialized |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3714 | * to prevent paralel buffered read to expose the stale data |
| 3715 | * before DIO complete the data IO. |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3716 | * |
| 3717 | * As to previously fallocated extents, ext4 get_block |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3718 | * will just simply mark the buffer mapped but still |
| 3719 | * keep the extents uninitialized. |
| 3720 | * |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3721 | * for non AIO case, we will convert those unwritten extents |
| 3722 | * to written after return back from blockdev_direct_IO. |
| 3723 | * |
| 3724 | * for async DIO, the conversion needs to be defered when |
| 3725 | * the IO is completed. The ext4 end_io callback function |
| 3726 | * will be called to take care of the conversion work. |
| 3727 | * Here for async case, we allocate an io_end structure to |
| 3728 | * hook to the iocb. |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3729 | */ |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3730 | iocb->private = NULL; |
| 3731 | EXT4_I(inode)->cur_aio_dio = NULL; |
| 3732 | if (!is_sync_kiocb(iocb)) { |
| 3733 | iocb->private = ext4_init_io_end(inode); |
| 3734 | if (!iocb->private) |
| 3735 | return -ENOMEM; |
| 3736 | /* |
| 3737 | * we save the io structure for current async |
| 3738 | * direct IO, so that later ext4_get_blocks() |
| 3739 | * could flag the io structure whether there |
| 3740 | * is a unwritten extents needs to be converted |
| 3741 | * when IO is completed. |
| 3742 | */ |
| 3743 | EXT4_I(inode)->cur_aio_dio = iocb->private; |
| 3744 | } |
| 3745 | |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3746 | ret = blockdev_direct_IO(rw, iocb, inode, |
| 3747 | inode->i_sb->s_bdev, iov, |
| 3748 | offset, nr_segs, |
| 3749 | ext4_get_block_dio_write, |
| 3750 | ext4_end_io_dio); |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3751 | if (iocb->private) |
| 3752 | EXT4_I(inode)->cur_aio_dio = NULL; |
| 3753 | /* |
| 3754 | * The io_end structure takes a reference to the inode, |
| 3755 | * that structure needs to be destroyed and the |
| 3756 | * reference to the inode need to be dropped, when IO is |
| 3757 | * complete, even with 0 byte write, or failed. |
| 3758 | * |
| 3759 | * In the successful AIO DIO case, the io_end structure will be |
| 3760 | * desctroyed and the reference to the inode will be dropped |
| 3761 | * after the end_io call back function is called. |
| 3762 | * |
| 3763 | * In the case there is 0 byte write, or error case, since |
| 3764 | * VFS direct IO won't invoke the end_io call back function, |
| 3765 | * we need to free the end_io structure here. |
| 3766 | */ |
| 3767 | if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { |
| 3768 | ext4_free_io_end(iocb->private); |
| 3769 | iocb->private = NULL; |
| 3770 | } else if (ret > 0) |
| 3771 | /* |
| 3772 | * for non AIO case, since the IO is already |
| 3773 | * completed, we could do the convertion right here |
| 3774 | */ |
| 3775 | ret = ext4_convert_unwritten_extents(inode, |
| 3776 | offset, ret); |
Mingming Cao | 4c0425f | 2009-09-28 15:48:41 -0400 | [diff] [blame] | 3777 | return ret; |
| 3778 | } |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3779 | |
| 3780 | /* 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] | 3781 | return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); |
| 3782 | } |
| 3783 | |
| 3784 | static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, |
| 3785 | const struct iovec *iov, loff_t offset, |
| 3786 | unsigned long nr_segs) |
| 3787 | { |
| 3788 | struct file *file = iocb->ki_filp; |
| 3789 | struct inode *inode = file->f_mapping->host; |
| 3790 | |
| 3791 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) |
| 3792 | return ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); |
| 3793 | |
| 3794 | return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); |
| 3795 | } |
| 3796 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3797 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3798 | * Pages can be marked dirty completely asynchronously from ext4's journalling |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3799 | * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do |
| 3800 | * much here because ->set_page_dirty is called under VFS locks. The page is |
| 3801 | * not necessarily locked. |
| 3802 | * |
| 3803 | * We cannot just dirty the page and leave attached buffers clean, because the |
| 3804 | * buffers' dirty state is "definitive". We cannot just set the buffers dirty |
| 3805 | * or jbddirty because all the journalling code will explode. |
| 3806 | * |
| 3807 | * So what we do is to mark the page "pending dirty" and next time writepage |
| 3808 | * is called, propagate that into the buffers appropriately. |
| 3809 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3810 | static int ext4_journalled_set_page_dirty(struct page *page) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3811 | { |
| 3812 | SetPageChecked(page); |
| 3813 | return __set_page_dirty_nobuffers(page); |
| 3814 | } |
| 3815 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3816 | static const struct address_space_operations ext4_ordered_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3817 | .readpage = ext4_readpage, |
| 3818 | .readpages = ext4_readpages, |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 3819 | .writepage = ext4_writepage, |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3820 | .sync_page = block_sync_page, |
| 3821 | .write_begin = ext4_write_begin, |
| 3822 | .write_end = ext4_ordered_write_end, |
| 3823 | .bmap = ext4_bmap, |
| 3824 | .invalidatepage = ext4_invalidatepage, |
| 3825 | .releasepage = ext4_releasepage, |
| 3826 | .direct_IO = ext4_direct_IO, |
| 3827 | .migratepage = buffer_migrate_page, |
| 3828 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 3829 | .error_remove_page = generic_error_remove_page, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3830 | }; |
| 3831 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3832 | static const struct address_space_operations ext4_writeback_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3833 | .readpage = ext4_readpage, |
| 3834 | .readpages = ext4_readpages, |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 3835 | .writepage = ext4_writepage, |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3836 | .sync_page = block_sync_page, |
| 3837 | .write_begin = ext4_write_begin, |
| 3838 | .write_end = ext4_writeback_write_end, |
| 3839 | .bmap = ext4_bmap, |
| 3840 | .invalidatepage = ext4_invalidatepage, |
| 3841 | .releasepage = ext4_releasepage, |
| 3842 | .direct_IO = ext4_direct_IO, |
| 3843 | .migratepage = buffer_migrate_page, |
| 3844 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 3845 | .error_remove_page = generic_error_remove_page, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3846 | }; |
| 3847 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3848 | static const struct address_space_operations ext4_journalled_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3849 | .readpage = ext4_readpage, |
| 3850 | .readpages = ext4_readpages, |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 3851 | .writepage = ext4_writepage, |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3852 | .sync_page = block_sync_page, |
| 3853 | .write_begin = ext4_write_begin, |
| 3854 | .write_end = ext4_journalled_write_end, |
| 3855 | .set_page_dirty = ext4_journalled_set_page_dirty, |
| 3856 | .bmap = ext4_bmap, |
| 3857 | .invalidatepage = ext4_invalidatepage, |
| 3858 | .releasepage = ext4_releasepage, |
| 3859 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 3860 | .error_remove_page = generic_error_remove_page, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3861 | }; |
| 3862 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3863 | static const struct address_space_operations ext4_da_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3864 | .readpage = ext4_readpage, |
| 3865 | .readpages = ext4_readpages, |
Aneesh Kumar K.V | 43ce1d2 | 2009-06-14 17:58:45 -0400 | [diff] [blame] | 3866 | .writepage = ext4_writepage, |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3867 | .writepages = ext4_da_writepages, |
| 3868 | .sync_page = block_sync_page, |
| 3869 | .write_begin = ext4_da_write_begin, |
| 3870 | .write_end = ext4_da_write_end, |
| 3871 | .bmap = ext4_bmap, |
| 3872 | .invalidatepage = ext4_da_invalidatepage, |
| 3873 | .releasepage = ext4_releasepage, |
| 3874 | .direct_IO = ext4_direct_IO, |
| 3875 | .migratepage = buffer_migrate_page, |
| 3876 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 3877 | .error_remove_page = generic_error_remove_page, |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3878 | }; |
| 3879 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3880 | void ext4_set_aops(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3881 | { |
Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3882 | if (ext4_should_order_data(inode) && |
| 3883 | test_opt(inode->i_sb, DELALLOC)) |
| 3884 | inode->i_mapping->a_ops = &ext4_da_aops; |
| 3885 | else if (ext4_should_order_data(inode)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3886 | inode->i_mapping->a_ops = &ext4_ordered_aops; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3887 | else if (ext4_should_writeback_data(inode) && |
| 3888 | test_opt(inode->i_sb, DELALLOC)) |
| 3889 | inode->i_mapping->a_ops = &ext4_da_aops; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3890 | else if (ext4_should_writeback_data(inode)) |
| 3891 | inode->i_mapping->a_ops = &ext4_writeback_aops; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3892 | else |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3893 | inode->i_mapping->a_ops = &ext4_journalled_aops; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3897 | * ext4_block_truncate_page() zeroes out a mapping from file offset `from' |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3898 | * up to the end of the block which corresponds to `from'. |
| 3899 | * This required during truncate. We need to physically zero the tail end |
| 3900 | * of that block so it doesn't yield old data if the file is later grown. |
| 3901 | */ |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3902 | int ext4_block_truncate_page(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3903 | struct address_space *mapping, loff_t from) |
| 3904 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3905 | ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3906 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3907 | unsigned blocksize, length, pos; |
| 3908 | ext4_lblk_t iblock; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3909 | struct inode *inode = mapping->host; |
| 3910 | struct buffer_head *bh; |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3911 | struct page *page; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3912 | int err = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3913 | |
Theodore Ts'o | f4a0101 | 2009-07-05 22:08:16 -0400 | [diff] [blame] | 3914 | page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, |
| 3915 | mapping_gfp_mask(mapping) & ~__GFP_FS); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3916 | if (!page) |
| 3917 | return -EINVAL; |
| 3918 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3919 | blocksize = inode->i_sb->s_blocksize; |
| 3920 | length = blocksize - (offset & (blocksize - 1)); |
| 3921 | iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); |
| 3922 | |
| 3923 | /* |
| 3924 | * For "nobh" option, we can only work if we don't need to |
| 3925 | * read-in the page - otherwise we create buffers to do the IO. |
| 3926 | */ |
| 3927 | if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) && |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3928 | ext4_should_writeback_data(inode) && PageUptodate(page)) { |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 3929 | zero_user(page, offset, length); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3930 | set_page_dirty(page); |
| 3931 | goto unlock; |
| 3932 | } |
| 3933 | |
| 3934 | if (!page_has_buffers(page)) |
| 3935 | create_empty_buffers(page, blocksize, 0); |
| 3936 | |
| 3937 | /* Find the buffer that contains "offset" */ |
| 3938 | bh = page_buffers(page); |
| 3939 | pos = blocksize; |
| 3940 | while (offset >= pos) { |
| 3941 | bh = bh->b_this_page; |
| 3942 | iblock++; |
| 3943 | pos += blocksize; |
| 3944 | } |
| 3945 | |
| 3946 | err = 0; |
| 3947 | if (buffer_freed(bh)) { |
| 3948 | BUFFER_TRACE(bh, "freed: skip"); |
| 3949 | goto unlock; |
| 3950 | } |
| 3951 | |
| 3952 | if (!buffer_mapped(bh)) { |
| 3953 | BUFFER_TRACE(bh, "unmapped"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3954 | ext4_get_block(inode, iblock, bh, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3955 | /* unmapped? It's a hole - nothing to do */ |
| 3956 | if (!buffer_mapped(bh)) { |
| 3957 | BUFFER_TRACE(bh, "still unmapped"); |
| 3958 | goto unlock; |
| 3959 | } |
| 3960 | } |
| 3961 | |
| 3962 | /* Ok, it's mapped. Make sure it's up-to-date */ |
| 3963 | if (PageUptodate(page)) |
| 3964 | set_buffer_uptodate(bh); |
| 3965 | |
| 3966 | if (!buffer_uptodate(bh)) { |
| 3967 | err = -EIO; |
| 3968 | ll_rw_block(READ, 1, &bh); |
| 3969 | wait_on_buffer(bh); |
| 3970 | /* Uhhuh. Read error. Complain and punt. */ |
| 3971 | if (!buffer_uptodate(bh)) |
| 3972 | goto unlock; |
| 3973 | } |
| 3974 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3975 | if (ext4_should_journal_data(inode)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3976 | BUFFER_TRACE(bh, "get write access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3977 | err = ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3978 | if (err) |
| 3979 | goto unlock; |
| 3980 | } |
| 3981 | |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 3982 | zero_user(page, offset, length); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3983 | |
| 3984 | BUFFER_TRACE(bh, "zeroed end of block"); |
| 3985 | |
| 3986 | err = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3987 | if (ext4_should_journal_data(inode)) { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3988 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3989 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3990 | if (ext4_should_order_data(inode)) |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3991 | err = ext4_jbd2_file_inode(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3992 | mark_buffer_dirty(bh); |
| 3993 | } |
| 3994 | |
| 3995 | unlock: |
| 3996 | unlock_page(page); |
| 3997 | page_cache_release(page); |
| 3998 | return err; |
| 3999 | } |
| 4000 | |
| 4001 | /* |
| 4002 | * Probably it should be a library function... search for first non-zero word |
| 4003 | * or memcmp with zero_page, whatever is better for particular architecture. |
| 4004 | * Linus? |
| 4005 | */ |
| 4006 | static inline int all_zeroes(__le32 *p, __le32 *q) |
| 4007 | { |
| 4008 | while (p < q) |
| 4009 | if (*p++) |
| 4010 | return 0; |
| 4011 | return 1; |
| 4012 | } |
| 4013 | |
| 4014 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4015 | * ext4_find_shared - find the indirect blocks for partial truncation. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4016 | * @inode: inode in question |
| 4017 | * @depth: depth of the affected branch |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4018 | * @offsets: offsets of pointers in that branch (see ext4_block_to_path) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4019 | * @chain: place to store the pointers to partial indirect blocks |
| 4020 | * @top: place to the (detached) top of branch |
| 4021 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4022 | * This is a helper function used by ext4_truncate(). |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4023 | * |
| 4024 | * When we do truncate() we may have to clean the ends of several |
| 4025 | * indirect blocks but leave the blocks themselves alive. Block is |
| 4026 | * partially truncated if some data below the new i_size is refered |
| 4027 | * from it (and it is on the path to the first completely truncated |
| 4028 | * data block, indeed). We have to free the top of that path along |
| 4029 | * with everything to the right of the path. Since no allocation |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4030 | * past the truncation point is possible until ext4_truncate() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4031 | * finishes, we may safely do the latter, but top of branch may |
| 4032 | * require special attention - pageout below the truncation point |
| 4033 | * might try to populate it. |
| 4034 | * |
| 4035 | * We atomically detach the top of branch from the tree, store the |
| 4036 | * block number of its root in *@top, pointers to buffer_heads of |
| 4037 | * partially truncated blocks - in @chain[].bh and pointers to |
| 4038 | * their last elements that should not be removed - in |
| 4039 | * @chain[].p. Return value is the pointer to last filled element |
| 4040 | * of @chain. |
| 4041 | * |
| 4042 | * The work left to caller to do the actual freeing of subtrees: |
| 4043 | * a) free the subtree starting from *@top |
| 4044 | * b) free the subtrees whose roots are stored in |
| 4045 | * (@chain[i].p+1 .. end of @chain[i].bh->b_data) |
| 4046 | * c) free the subtrees growing from the inode past the @chain[0]. |
| 4047 | * (no partially truncated stuff there). */ |
| 4048 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4049 | static Indirect *ext4_find_shared(struct inode *inode, int depth, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4050 | ext4_lblk_t offsets[4], Indirect chain[4], |
| 4051 | __le32 *top) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4052 | { |
| 4053 | Indirect *partial, *p; |
| 4054 | int k, err; |
| 4055 | |
| 4056 | *top = 0; |
| 4057 | /* Make k index the deepest non-null offest + 1 */ |
| 4058 | for (k = depth; k > 1 && !offsets[k-1]; k--) |
| 4059 | ; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4060 | partial = ext4_get_branch(inode, k, offsets, chain, &err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4061 | /* Writer: pointers */ |
| 4062 | if (!partial) |
| 4063 | partial = chain + k-1; |
| 4064 | /* |
| 4065 | * If the branch acquired continuation since we've looked at it - |
| 4066 | * fine, it should all survive and (new) top doesn't belong to us. |
| 4067 | */ |
| 4068 | if (!partial->key && *partial->p) |
| 4069 | /* Writer: end */ |
| 4070 | goto no_top; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4071 | for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4072 | ; |
| 4073 | /* |
| 4074 | * OK, we've found the last block that must survive. The rest of our |
| 4075 | * branch should be detached before unlocking. However, if that rest |
| 4076 | * of branch is all ours and does not grow immediately from the inode |
| 4077 | * it's easier to cheat and just decrement partial->p. |
| 4078 | */ |
| 4079 | if (p == chain + k - 1 && p > chain) { |
| 4080 | p->p--; |
| 4081 | } else { |
| 4082 | *top = *p->p; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4083 | /* Nope, don't do this in ext4. Must leave the tree intact */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4084 | #if 0 |
| 4085 | *p->p = 0; |
| 4086 | #endif |
| 4087 | } |
| 4088 | /* Writer: end */ |
| 4089 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4090 | while (partial > p) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4091 | brelse(partial->bh); |
| 4092 | partial--; |
| 4093 | } |
| 4094 | no_top: |
| 4095 | return partial; |
| 4096 | } |
| 4097 | |
| 4098 | /* |
| 4099 | * Zero a number of block pointers in either an inode or an indirect block. |
| 4100 | * If we restart the transaction we must again get write access to the |
| 4101 | * indirect block for further modification. |
| 4102 | * |
| 4103 | * We release `count' blocks on disk, but (last - first) may be greater |
| 4104 | * than `count' because there can be holes in there. |
| 4105 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4106 | static void ext4_clear_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4107 | struct buffer_head *bh, |
| 4108 | ext4_fsblk_t block_to_free, |
| 4109 | unsigned long count, __le32 *first, |
| 4110 | __le32 *last) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4111 | { |
| 4112 | __le32 *p; |
| 4113 | if (try_to_extend_transaction(handle, inode)) { |
| 4114 | if (bh) { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4115 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
| 4116 | ext4_handle_dirty_metadata(handle, inode, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4117 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4118 | ext4_mark_inode_dirty(handle, inode); |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 4119 | ext4_truncate_restart_trans(handle, inode, |
| 4120 | blocks_for_truncate(inode)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4121 | if (bh) { |
| 4122 | BUFFER_TRACE(bh, "retaking write access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4123 | ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4124 | } |
| 4125 | } |
| 4126 | |
| 4127 | /* |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4128 | * Any buffers which are on the journal will be in memory. We |
| 4129 | * find them on the hash table so jbd2_journal_revoke() will |
| 4130 | * run jbd2_journal_forget() on them. We've already detached |
| 4131 | * each block from the file, so bforget() in |
| 4132 | * jbd2_journal_forget() should be safe. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4133 | * |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 4134 | * AKPM: turn on bforget in jbd2_journal_forget()!!! |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4135 | */ |
| 4136 | for (p = first; p < last; p++) { |
| 4137 | u32 nr = le32_to_cpu(*p); |
| 4138 | if (nr) { |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4139 | struct buffer_head *tbh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4140 | |
| 4141 | *p = 0; |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4142 | tbh = sb_find_get_block(inode->i_sb, nr); |
| 4143 | ext4_forget(handle, 0, inode, tbh, nr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4144 | } |
| 4145 | } |
| 4146 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4147 | ext4_free_blocks(handle, inode, block_to_free, count, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4148 | } |
| 4149 | |
| 4150 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4151 | * ext4_free_data - free a list of data blocks |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4152 | * @handle: handle for this transaction |
| 4153 | * @inode: inode we are dealing with |
| 4154 | * @this_bh: indirect buffer_head which contains *@first and *@last |
| 4155 | * @first: array of block numbers |
| 4156 | * @last: points immediately past the end of array |
| 4157 | * |
| 4158 | * We are freeing all blocks refered from that array (numbers are stored as |
| 4159 | * little-endian 32-bit) and updating @inode->i_blocks appropriately. |
| 4160 | * |
| 4161 | * We accumulate contiguous runs of blocks to free. Conveniently, if these |
| 4162 | * blocks are contiguous then releasing them at one time will only affect one |
| 4163 | * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't |
| 4164 | * actually use a lot of journal space. |
| 4165 | * |
| 4166 | * @this_bh will be %NULL if @first and @last point into the inode's direct |
| 4167 | * block pointers. |
| 4168 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4169 | static void ext4_free_data(handle_t *handle, struct inode *inode, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4170 | struct buffer_head *this_bh, |
| 4171 | __le32 *first, __le32 *last) |
| 4172 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4173 | ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4174 | unsigned long count = 0; /* Number of blocks in the run */ |
| 4175 | __le32 *block_to_free_p = NULL; /* Pointer into inode/ind |
| 4176 | corresponding to |
| 4177 | block_to_free */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4178 | ext4_fsblk_t nr; /* Current block # */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4179 | __le32 *p; /* Pointer into inode/ind |
| 4180 | for current block */ |
| 4181 | int err; |
| 4182 | |
| 4183 | if (this_bh) { /* For indirect block */ |
| 4184 | BUFFER_TRACE(this_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4185 | err = ext4_journal_get_write_access(handle, this_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4186 | /* Important: if we can't update the indirect pointers |
| 4187 | * to the blocks, we can't free them. */ |
| 4188 | if (err) |
| 4189 | return; |
| 4190 | } |
| 4191 | |
| 4192 | for (p = first; p < last; p++) { |
| 4193 | nr = le32_to_cpu(*p); |
| 4194 | if (nr) { |
| 4195 | /* accumulate blocks to free if they're contiguous */ |
| 4196 | if (count == 0) { |
| 4197 | block_to_free = nr; |
| 4198 | block_to_free_p = p; |
| 4199 | count = 1; |
| 4200 | } else if (nr == block_to_free + count) { |
| 4201 | count++; |
| 4202 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4203 | ext4_clear_blocks(handle, inode, this_bh, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4204 | block_to_free, |
| 4205 | count, block_to_free_p, p); |
| 4206 | block_to_free = nr; |
| 4207 | block_to_free_p = p; |
| 4208 | count = 1; |
| 4209 | } |
| 4210 | } |
| 4211 | } |
| 4212 | |
| 4213 | if (count > 0) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4214 | ext4_clear_blocks(handle, inode, this_bh, block_to_free, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4215 | count, block_to_free_p, p); |
| 4216 | |
| 4217 | if (this_bh) { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4218 | BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata"); |
Duane Griffin | 71dc8fb | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4219 | |
| 4220 | /* |
| 4221 | * The buffer head should have an attached journal head at this |
| 4222 | * point. However, if the data is corrupted and an indirect |
| 4223 | * block pointed to itself, it would have been detached when |
| 4224 | * the block was cleared. Check for this instead of OOPSing. |
| 4225 | */ |
Theodore Ts'o | e7f0796 | 2009-01-20 09:50:19 -0500 | [diff] [blame] | 4226 | if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4227 | ext4_handle_dirty_metadata(handle, inode, this_bh); |
Duane Griffin | 71dc8fb | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4228 | else |
| 4229 | ext4_error(inode->i_sb, __func__, |
| 4230 | "circular indirect block detected, " |
| 4231 | "inode=%lu, block=%llu", |
| 4232 | inode->i_ino, |
| 4233 | (unsigned long long) this_bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4234 | } |
| 4235 | } |
| 4236 | |
| 4237 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4238 | * ext4_free_branches - free an array of branches |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4239 | * @handle: JBD handle for this transaction |
| 4240 | * @inode: inode we are dealing with |
| 4241 | * @parent_bh: the buffer_head which contains *@first and *@last |
| 4242 | * @first: array of block numbers |
| 4243 | * @last: pointer immediately past the end of array |
| 4244 | * @depth: depth of the branches to free |
| 4245 | * |
| 4246 | * We are freeing all blocks refered from these branches (numbers are |
| 4247 | * stored as little-endian 32-bit) and updating @inode->i_blocks |
| 4248 | * appropriately. |
| 4249 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4250 | static void ext4_free_branches(handle_t *handle, struct inode *inode, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4251 | struct buffer_head *parent_bh, |
| 4252 | __le32 *first, __le32 *last, int depth) |
| 4253 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4254 | ext4_fsblk_t nr; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4255 | __le32 *p; |
| 4256 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4257 | if (ext4_handle_is_aborted(handle)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4258 | return; |
| 4259 | |
| 4260 | if (depth--) { |
| 4261 | struct buffer_head *bh; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4262 | int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4263 | p = last; |
| 4264 | while (--p >= first) { |
| 4265 | nr = le32_to_cpu(*p); |
| 4266 | if (!nr) |
| 4267 | continue; /* A hole */ |
| 4268 | |
| 4269 | /* Go read the buffer for the next level down */ |
| 4270 | bh = sb_bread(inode->i_sb, nr); |
| 4271 | |
| 4272 | /* |
| 4273 | * A read failure? Report error and clear slot |
| 4274 | * (should be rare). |
| 4275 | */ |
| 4276 | if (!bh) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4277 | ext4_error(inode->i_sb, "ext4_free_branches", |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 4278 | "Read failure, inode=%lu, block=%llu", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4279 | inode->i_ino, nr); |
| 4280 | continue; |
| 4281 | } |
| 4282 | |
| 4283 | /* This zaps the entire block. Bottom up. */ |
| 4284 | BUFFER_TRACE(bh, "free child branches"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4285 | ext4_free_branches(handle, inode, bh, |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4286 | (__le32 *) bh->b_data, |
| 4287 | (__le32 *) bh->b_data + addr_per_block, |
| 4288 | depth); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4289 | |
| 4290 | /* |
| 4291 | * We've probably journalled the indirect block several |
| 4292 | * times during the truncate. But it's no longer |
| 4293 | * needed and we now drop it from the transaction via |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 4294 | * jbd2_journal_revoke(). |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4295 | * |
| 4296 | * That's easy if it's exclusively part of this |
| 4297 | * transaction. But if it's part of the committing |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 4298 | * transaction then jbd2_journal_forget() will simply |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4299 | * brelse() it. That means that if the underlying |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4300 | * block is reallocated in ext4_get_block(), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4301 | * unmap_underlying_metadata() will find this block |
| 4302 | * and will try to get rid of it. damn, damn. |
| 4303 | * |
| 4304 | * If this block has already been committed to the |
| 4305 | * journal, a revoke record will be written. And |
| 4306 | * revoke records must be emitted *before* clearing |
| 4307 | * this block's bit in the bitmaps. |
| 4308 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4309 | ext4_forget(handle, 1, inode, bh, bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4310 | |
| 4311 | /* |
| 4312 | * Everything below this this pointer has been |
| 4313 | * released. Now let this top-of-subtree go. |
| 4314 | * |
| 4315 | * We want the freeing of this indirect block to be |
| 4316 | * atomic in the journal with the updating of the |
| 4317 | * bitmap block which owns it. So make some room in |
| 4318 | * the journal. |
| 4319 | * |
| 4320 | * We zero the parent pointer *after* freeing its |
| 4321 | * pointee in the bitmaps, so if extend_transaction() |
| 4322 | * for some reason fails to put the bitmap changes and |
| 4323 | * the release into the same transaction, recovery |
| 4324 | * will merely complain about releasing a free block, |
| 4325 | * rather than leaking blocks. |
| 4326 | */ |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4327 | if (ext4_handle_is_aborted(handle)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4328 | return; |
| 4329 | if (try_to_extend_transaction(handle, inode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4330 | ext4_mark_inode_dirty(handle, inode); |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 4331 | ext4_truncate_restart_trans(handle, inode, |
| 4332 | blocks_for_truncate(inode)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4333 | } |
| 4334 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4335 | ext4_free_blocks(handle, inode, nr, 1, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4336 | |
| 4337 | if (parent_bh) { |
| 4338 | /* |
| 4339 | * The block which we have just freed is |
| 4340 | * pointed to by an indirect block: journal it |
| 4341 | */ |
| 4342 | BUFFER_TRACE(parent_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4343 | if (!ext4_journal_get_write_access(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4344 | parent_bh)){ |
| 4345 | *p = 0; |
| 4346 | BUFFER_TRACE(parent_bh, |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4347 | "call ext4_handle_dirty_metadata"); |
| 4348 | ext4_handle_dirty_metadata(handle, |
| 4349 | inode, |
| 4350 | parent_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4351 | } |
| 4352 | } |
| 4353 | } |
| 4354 | } else { |
| 4355 | /* We have reached the bottom of the tree. */ |
| 4356 | BUFFER_TRACE(parent_bh, "free data blocks"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4357 | ext4_free_data(handle, inode, parent_bh, first, last); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4358 | } |
| 4359 | } |
| 4360 | |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4361 | int ext4_can_truncate(struct inode *inode) |
| 4362 | { |
| 4363 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 4364 | return 0; |
| 4365 | if (S_ISREG(inode->i_mode)) |
| 4366 | return 1; |
| 4367 | if (S_ISDIR(inode->i_mode)) |
| 4368 | return 1; |
| 4369 | if (S_ISLNK(inode->i_mode)) |
| 4370 | return !ext4_inode_is_fast_symlink(inode); |
| 4371 | return 0; |
| 4372 | } |
| 4373 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4374 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4375 | * ext4_truncate() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4376 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4377 | * We block out ext4_get_block() block instantiations across the entire |
| 4378 | * transaction, and VFS/VM ensures that ext4_truncate() cannot run |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4379 | * simultaneously on behalf of the same inode. |
| 4380 | * |
| 4381 | * As we work through the truncate and commmit bits of it to the journal there |
| 4382 | * is one core, guiding principle: the file's tree must always be consistent on |
| 4383 | * disk. We must be able to restart the truncate after a crash. |
| 4384 | * |
| 4385 | * The file's tree may be transiently inconsistent in memory (although it |
| 4386 | * probably isn't), but whenever we close off and commit a journal transaction, |
| 4387 | * the contents of (the filesystem + the journal) must be consistent and |
| 4388 | * restartable. It's pretty simple, really: bottom up, right to left (although |
| 4389 | * left-to-right works OK too). |
| 4390 | * |
| 4391 | * Note that at recovery time, journal replay occurs *before* the restart of |
| 4392 | * truncate against the orphan inode list. |
| 4393 | * |
| 4394 | * 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] | 4395 | * 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] | 4396 | * 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] | 4397 | * ext4_truncate() to have another go. So there will be instantiated blocks |
| 4398 | * 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] | 4399 | * 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] | 4400 | * ext4_truncate() run will find them and release them. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4401 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4402 | void ext4_truncate(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4403 | { |
| 4404 | handle_t *handle; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4405 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4406 | __le32 *i_data = ei->i_data; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4407 | int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4408 | struct address_space *mapping = inode->i_mapping; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4409 | ext4_lblk_t offsets[4]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4410 | Indirect chain[4]; |
| 4411 | Indirect *partial; |
| 4412 | __le32 nr = 0; |
| 4413 | int n; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4414 | ext4_lblk_t last_block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4415 | unsigned blocksize = inode->i_sb->s_blocksize; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4416 | |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4417 | if (!ext4_can_truncate(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4418 | return; |
| 4419 | |
Theodore Ts'o | 5534fb5 | 2009-09-17 09:34:16 -0400 | [diff] [blame] | 4420 | if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) |
Theodore Ts'o | 7d8f9f7 | 2009-02-24 08:21:14 -0500 | [diff] [blame] | 4421 | ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE; |
| 4422 | |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4423 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4424 | ext4_ext_truncate(inode); |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4425 | return; |
| 4426 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4427 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4428 | handle = start_transaction(inode); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4429 | if (IS_ERR(handle)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4430 | return; /* AKPM: return what? */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4431 | |
| 4432 | last_block = (inode->i_size + blocksize-1) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4433 | >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4434 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4435 | if (inode->i_size & (blocksize - 1)) |
| 4436 | if (ext4_block_truncate_page(handle, mapping, inode->i_size)) |
| 4437 | goto out_stop; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4438 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4439 | n = ext4_block_to_path(inode, last_block, offsets, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4440 | if (n == 0) |
| 4441 | goto out_stop; /* error */ |
| 4442 | |
| 4443 | /* |
| 4444 | * OK. This truncate is going to happen. We add the inode to the |
| 4445 | * orphan list, so that if this truncate spans multiple transactions, |
| 4446 | * and we crash, we will resume the truncate when the filesystem |
| 4447 | * recovers. It also marks the inode dirty, to catch the new size. |
| 4448 | * |
| 4449 | * Implication: the file must always be in a sane, consistent |
| 4450 | * truncatable state while each transaction commits. |
| 4451 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4452 | if (ext4_orphan_add(handle, inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4453 | goto out_stop; |
| 4454 | |
| 4455 | /* |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4456 | * From here we block out all ext4_get_block() callers who want to |
| 4457 | * modify the block allocation tree. |
| 4458 | */ |
| 4459 | down_write(&ei->i_data_sem); |
Theodore Ts'o | b4df203 | 2008-08-13 21:44:34 -0400 | [diff] [blame] | 4460 | |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4461 | ext4_discard_preallocations(inode); |
Theodore Ts'o | b4df203 | 2008-08-13 21:44:34 -0400 | [diff] [blame] | 4462 | |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4463 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4464 | * The orphan list entry will now protect us from any crash which |
| 4465 | * occurs before the truncate completes, so it is now safe to propagate |
| 4466 | * the new, shorter inode size (held for now in i_size) into the |
| 4467 | * on-disk inode. We do this via i_disksize, which is the value which |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4468 | * ext4 *really* writes onto the disk inode. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4469 | */ |
| 4470 | ei->i_disksize = inode->i_size; |
| 4471 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4472 | if (n == 1) { /* direct blocks */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4473 | ext4_free_data(handle, inode, NULL, i_data+offsets[0], |
| 4474 | i_data + EXT4_NDIR_BLOCKS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4475 | goto do_indirects; |
| 4476 | } |
| 4477 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4478 | partial = ext4_find_shared(inode, n, offsets, chain, &nr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4479 | /* Kill the top of shared branch (not detached) */ |
| 4480 | if (nr) { |
| 4481 | if (partial == chain) { |
| 4482 | /* Shared branch grows from the inode */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4483 | ext4_free_branches(handle, inode, NULL, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4484 | &nr, &nr+1, (chain+n-1) - partial); |
| 4485 | *partial->p = 0; |
| 4486 | /* |
| 4487 | * We mark the inode dirty prior to restart, |
| 4488 | * and prior to stop. No need for it here. |
| 4489 | */ |
| 4490 | } else { |
| 4491 | /* Shared branch grows from an indirect block */ |
| 4492 | BUFFER_TRACE(partial->bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4493 | ext4_free_branches(handle, inode, partial->bh, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4494 | partial->p, |
| 4495 | partial->p+1, (chain+n-1) - partial); |
| 4496 | } |
| 4497 | } |
| 4498 | /* Clear the ends of indirect blocks on the shared branch */ |
| 4499 | while (partial > chain) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4500 | ext4_free_branches(handle, inode, partial->bh, partial->p + 1, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4501 | (__le32*)partial->bh->b_data+addr_per_block, |
| 4502 | (chain+n-1) - partial); |
| 4503 | BUFFER_TRACE(partial->bh, "call brelse"); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4504 | brelse(partial->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4505 | partial--; |
| 4506 | } |
| 4507 | do_indirects: |
| 4508 | /* Kill the remaining (whole) subtrees */ |
| 4509 | switch (offsets[0]) { |
| 4510 | default: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4511 | nr = i_data[EXT4_IND_BLOCK]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4512 | if (nr) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4513 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); |
| 4514 | i_data[EXT4_IND_BLOCK] = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4515 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4516 | case EXT4_IND_BLOCK: |
| 4517 | nr = i_data[EXT4_DIND_BLOCK]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4518 | if (nr) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4519 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); |
| 4520 | i_data[EXT4_DIND_BLOCK] = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4521 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4522 | case EXT4_DIND_BLOCK: |
| 4523 | nr = i_data[EXT4_TIND_BLOCK]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4524 | if (nr) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4525 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); |
| 4526 | i_data[EXT4_TIND_BLOCK] = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4527 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4528 | case EXT4_TIND_BLOCK: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4529 | ; |
| 4530 | } |
| 4531 | |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4532 | up_write(&ei->i_data_sem); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 4533 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4534 | ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4535 | |
| 4536 | /* |
| 4537 | * In a multi-transaction truncate, we only make the final transaction |
| 4538 | * synchronous |
| 4539 | */ |
| 4540 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4541 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4542 | out_stop: |
| 4543 | /* |
| 4544 | * If this was a simple ftruncate(), and the file will remain alive |
| 4545 | * then we need to clear up the orphan record which we created above. |
| 4546 | * However, if this was a real unlink then we were called by |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4547 | * ext4_delete_inode(), and we allow that function to clean up the |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4548 | * orphan info for us. |
| 4549 | */ |
| 4550 | if (inode->i_nlink) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4551 | ext4_orphan_del(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4552 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4553 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4554 | } |
| 4555 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4556 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4557 | * 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] | 4558 | * underlying buffer_head on success. If 'in_mem' is true, we have all |
| 4559 | * data in memory that is needed to recreate the on-disk version of this |
| 4560 | * inode. |
| 4561 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4562 | static int __ext4_get_inode_loc(struct inode *inode, |
| 4563 | struct ext4_iloc *iloc, int in_mem) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4564 | { |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4565 | struct ext4_group_desc *gdp; |
| 4566 | struct buffer_head *bh; |
| 4567 | struct super_block *sb = inode->i_sb; |
| 4568 | ext4_fsblk_t block; |
| 4569 | int inodes_per_block, inode_offset; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4570 | |
Aneesh Kumar K.V | 3a06d77 | 2008-11-22 15:04:59 -0500 | [diff] [blame] | 4571 | iloc->bh = NULL; |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4572 | if (!ext4_valid_inum(sb, inode->i_ino)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4573 | return -EIO; |
| 4574 | |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4575 | iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); |
| 4576 | gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); |
| 4577 | if (!gdp) |
| 4578 | return -EIO; |
| 4579 | |
| 4580 | /* |
| 4581 | * Figure out the offset within the block group inode table |
| 4582 | */ |
| 4583 | inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb)); |
| 4584 | inode_offset = ((inode->i_ino - 1) % |
| 4585 | EXT4_INODES_PER_GROUP(sb)); |
| 4586 | block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); |
| 4587 | iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); |
| 4588 | |
| 4589 | bh = sb_getblk(sb, block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4590 | if (!bh) { |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4591 | ext4_error(sb, "ext4_get_inode_loc", "unable to read " |
| 4592 | "inode block - inode=%lu, block=%llu", |
| 4593 | inode->i_ino, block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4594 | return -EIO; |
| 4595 | } |
| 4596 | if (!buffer_uptodate(bh)) { |
| 4597 | lock_buffer(bh); |
Hidehiro Kawai | 9c83a92 | 2008-07-26 16:39:26 -0400 | [diff] [blame] | 4598 | |
| 4599 | /* |
| 4600 | * If the buffer has the write error flag, we have failed |
| 4601 | * to write out another inode in the same block. In this |
| 4602 | * case, we don't have to read the block because we may |
| 4603 | * read the old inode data successfully. |
| 4604 | */ |
| 4605 | if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) |
| 4606 | set_buffer_uptodate(bh); |
| 4607 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4608 | if (buffer_uptodate(bh)) { |
| 4609 | /* someone brought it uptodate while we waited */ |
| 4610 | unlock_buffer(bh); |
| 4611 | goto has_buffer; |
| 4612 | } |
| 4613 | |
| 4614 | /* |
| 4615 | * If we have all information of the inode in memory and this |
| 4616 | * is the only valid inode in the block, we need not read the |
| 4617 | * block. |
| 4618 | */ |
| 4619 | if (in_mem) { |
| 4620 | struct buffer_head *bitmap_bh; |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4621 | int i, start; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4622 | |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4623 | start = inode_offset & ~(inodes_per_block - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4624 | |
| 4625 | /* Is the inode bitmap in cache? */ |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4626 | bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4627 | if (!bitmap_bh) |
| 4628 | goto make_io; |
| 4629 | |
| 4630 | /* |
| 4631 | * If the inode bitmap isn't in cache then the |
| 4632 | * optimisation may end up performing two reads instead |
| 4633 | * of one, so skip it. |
| 4634 | */ |
| 4635 | if (!buffer_uptodate(bitmap_bh)) { |
| 4636 | brelse(bitmap_bh); |
| 4637 | goto make_io; |
| 4638 | } |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4639 | for (i = start; i < start + inodes_per_block; i++) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4640 | if (i == inode_offset) |
| 4641 | continue; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4642 | if (ext4_test_bit(i, bitmap_bh->b_data)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4643 | break; |
| 4644 | } |
| 4645 | brelse(bitmap_bh); |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4646 | if (i == start + inodes_per_block) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4647 | /* all other inodes are free, so skip I/O */ |
| 4648 | memset(bh->b_data, 0, bh->b_size); |
| 4649 | set_buffer_uptodate(bh); |
| 4650 | unlock_buffer(bh); |
| 4651 | goto has_buffer; |
| 4652 | } |
| 4653 | } |
| 4654 | |
| 4655 | make_io: |
| 4656 | /* |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4657 | * If we need to do any I/O, try to pre-readahead extra |
| 4658 | * blocks from the inode table. |
| 4659 | */ |
| 4660 | if (EXT4_SB(sb)->s_inode_readahead_blks) { |
| 4661 | ext4_fsblk_t b, end, table; |
| 4662 | unsigned num; |
| 4663 | |
| 4664 | table = ext4_inode_table(sb, gdp); |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 4665 | /* s_inode_readahead_blks is always a power of 2 */ |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4666 | b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); |
| 4667 | if (table > b) |
| 4668 | b = table; |
| 4669 | end = b + EXT4_SB(sb)->s_inode_readahead_blks; |
| 4670 | num = EXT4_INODES_PER_GROUP(sb); |
| 4671 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 4672 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 4673 | num -= ext4_itable_unused_count(sb, gdp); |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4674 | table += num / inodes_per_block; |
| 4675 | if (end > table) |
| 4676 | end = table; |
| 4677 | while (b <= end) |
| 4678 | sb_breadahead(sb, b++); |
| 4679 | } |
| 4680 | |
| 4681 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4682 | * There are other valid inodes in the buffer, this inode |
| 4683 | * has in-inode xattrs, or we don't have this inode in memory. |
| 4684 | * Read the block from disk. |
| 4685 | */ |
| 4686 | get_bh(bh); |
| 4687 | bh->b_end_io = end_buffer_read_sync; |
| 4688 | submit_bh(READ_META, bh); |
| 4689 | wait_on_buffer(bh); |
| 4690 | if (!buffer_uptodate(bh)) { |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4691 | ext4_error(sb, __func__, |
| 4692 | "unable to read inode block - inode=%lu, " |
| 4693 | "block=%llu", inode->i_ino, block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4694 | brelse(bh); |
| 4695 | return -EIO; |
| 4696 | } |
| 4697 | } |
| 4698 | has_buffer: |
| 4699 | iloc->bh = bh; |
| 4700 | return 0; |
| 4701 | } |
| 4702 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4703 | int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4704 | { |
| 4705 | /* We have all inode data except xattrs in memory here. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4706 | return __ext4_get_inode_loc(inode, iloc, |
| 4707 | !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4708 | } |
| 4709 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4710 | void ext4_set_inode_flags(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4711 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4712 | unsigned int flags = EXT4_I(inode)->i_flags; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4713 | |
| 4714 | 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] | 4715 | if (flags & EXT4_SYNC_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4716 | inode->i_flags |= S_SYNC; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4717 | if (flags & EXT4_APPEND_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4718 | inode->i_flags |= S_APPEND; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4719 | if (flags & EXT4_IMMUTABLE_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4720 | inode->i_flags |= S_IMMUTABLE; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4721 | if (flags & EXT4_NOATIME_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4722 | inode->i_flags |= S_NOATIME; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4723 | if (flags & EXT4_DIRSYNC_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4724 | inode->i_flags |= S_DIRSYNC; |
| 4725 | } |
| 4726 | |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 4727 | /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ |
| 4728 | void ext4_get_inode_flags(struct ext4_inode_info *ei) |
| 4729 | { |
| 4730 | unsigned int flags = ei->vfs_inode.i_flags; |
| 4731 | |
| 4732 | ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL| |
| 4733 | EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL); |
| 4734 | if (flags & S_SYNC) |
| 4735 | ei->i_flags |= EXT4_SYNC_FL; |
| 4736 | if (flags & S_APPEND) |
| 4737 | ei->i_flags |= EXT4_APPEND_FL; |
| 4738 | if (flags & S_IMMUTABLE) |
| 4739 | ei->i_flags |= EXT4_IMMUTABLE_FL; |
| 4740 | if (flags & S_NOATIME) |
| 4741 | ei->i_flags |= EXT4_NOATIME_FL; |
| 4742 | if (flags & S_DIRSYNC) |
| 4743 | ei->i_flags |= EXT4_DIRSYNC_FL; |
| 4744 | } |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4745 | |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4746 | static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4747 | struct ext4_inode_info *ei) |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4748 | { |
| 4749 | blkcnt_t i_blocks ; |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4750 | struct inode *inode = &(ei->vfs_inode); |
| 4751 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4752 | |
| 4753 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 4754 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { |
| 4755 | /* we are using combined 48 bit field */ |
| 4756 | i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | |
| 4757 | le32_to_cpu(raw_inode->i_blocks_lo); |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4758 | if (ei->i_flags & EXT4_HUGE_FILE_FL) { |
| 4759 | /* i_blocks represent file system block size */ |
| 4760 | return i_blocks << (inode->i_blkbits - 9); |
| 4761 | } else { |
| 4762 | return i_blocks; |
| 4763 | } |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4764 | } else { |
| 4765 | return le32_to_cpu(raw_inode->i_blocks_lo); |
| 4766 | } |
| 4767 | } |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 4768 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4769 | struct inode *ext4_iget(struct super_block *sb, unsigned long ino) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4770 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4771 | struct ext4_iloc iloc; |
| 4772 | struct ext4_inode *raw_inode; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4773 | struct ext4_inode_info *ei; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4774 | struct buffer_head *bh; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4775 | struct inode *inode; |
| 4776 | long ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4777 | int block; |
| 4778 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4779 | inode = iget_locked(sb, ino); |
| 4780 | if (!inode) |
| 4781 | return ERR_PTR(-ENOMEM); |
| 4782 | if (!(inode->i_state & I_NEW)) |
| 4783 | return inode; |
| 4784 | |
| 4785 | ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4786 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4787 | ret = __ext4_get_inode_loc(inode, &iloc, 0); |
| 4788 | if (ret < 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4789 | goto bad_inode; |
| 4790 | bh = iloc.bh; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4791 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4792 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); |
| 4793 | inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); |
| 4794 | 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] | 4795 | if (!(test_opt(inode->i_sb, NO_UID32))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4796 | inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; |
| 4797 | inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; |
| 4798 | } |
| 4799 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4800 | |
| 4801 | ei->i_state = 0; |
| 4802 | ei->i_dir_start_lookup = 0; |
| 4803 | ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); |
| 4804 | /* We now have enough fields to check if the inode was active or not. |
| 4805 | * This is needed because nfsd might try to access dead inodes |
| 4806 | * the test is that same one that e2fsck uses |
| 4807 | * NeilBrown 1999oct15 |
| 4808 | */ |
| 4809 | if (inode->i_nlink == 0) { |
| 4810 | if (inode->i_mode == 0 || |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4811 | !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4812 | /* this inode is deleted */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4813 | brelse(bh); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4814 | ret = -ESTALE; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4815 | goto bad_inode; |
| 4816 | } |
| 4817 | /* The only unlinked inodes we let through here have |
| 4818 | * valid i_mode and are being read by the orphan |
| 4819 | * recovery code: that's fine, we're about to complete |
| 4820 | * the process of deleting those. */ |
| 4821 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4822 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4823 | inode->i_blocks = ext4_inode_blocks(raw_inode, ei); |
Aneesh Kumar K.V | 7973c0c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4824 | 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] | 4825 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) |
Badari Pulavarty | a1ddeb7 | 2006-10-11 01:21:09 -0700 | [diff] [blame] | 4826 | ei->i_file_acl |= |
| 4827 | ((__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] | 4828 | inode->i_size = ext4_isize(raw_inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4829 | ei->i_disksize = inode->i_size; |
| 4830 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); |
| 4831 | ei->i_block_group = iloc.block_group; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 4832 | ei->i_last_alloc_group = ~0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4833 | /* |
| 4834 | * NOTE! The in-memory inode i_data array is in little-endian order |
| 4835 | * even on big-endian machines: we do NOT byteswap the block numbers! |
| 4836 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4837 | for (block = 0; block < EXT4_N_BLOCKS; block++) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4838 | ei->i_data[block] = raw_inode->i_block[block]; |
| 4839 | INIT_LIST_HEAD(&ei->i_orphan); |
| 4840 | |
Eric Sandeen | 0040d98 | 2008-02-05 22:36:43 -0500 | [diff] [blame] | 4841 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4842 | ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4843 | if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > |
Kirill Korotaev | e5d2861 | 2007-06-23 17:16:51 -0700 | [diff] [blame] | 4844 | EXT4_INODE_SIZE(inode->i_sb)) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4845 | brelse(bh); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4846 | ret = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4847 | goto bad_inode; |
Kirill Korotaev | e5d2861 | 2007-06-23 17:16:51 -0700 | [diff] [blame] | 4848 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4849 | if (ei->i_extra_isize == 0) { |
| 4850 | /* The extra space is currently unused. Use it. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4851 | ei->i_extra_isize = sizeof(struct ext4_inode) - |
| 4852 | EXT4_GOOD_OLD_INODE_SIZE; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4853 | } else { |
| 4854 | __le32 *magic = (void *)raw_inode + |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4855 | EXT4_GOOD_OLD_INODE_SIZE + |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4856 | ei->i_extra_isize; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4857 | if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4858 | ei->i_state |= EXT4_STATE_XATTR; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4859 | } |
| 4860 | } else |
| 4861 | ei->i_extra_isize = 0; |
| 4862 | |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 4863 | EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); |
| 4864 | EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); |
| 4865 | EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); |
| 4866 | EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); |
| 4867 | |
Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4868 | inode->i_version = le32_to_cpu(raw_inode->i_disk_version); |
| 4869 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { |
| 4870 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) |
| 4871 | inode->i_version |= |
| 4872 | (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; |
| 4873 | } |
| 4874 | |
Theodore Ts'o | c4b5a61 | 2009-04-24 18:45:35 -0400 | [diff] [blame] | 4875 | ret = 0; |
Theodore Ts'o | 485c26e | 2009-04-24 13:43:20 -0400 | [diff] [blame] | 4876 | if (ei->i_file_acl && |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4877 | ((ei->i_file_acl < |
Theodore Ts'o | 485c26e | 2009-04-24 13:43:20 -0400 | [diff] [blame] | 4878 | (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) + |
| 4879 | EXT4_SB(sb)->s_gdb_count)) || |
| 4880 | (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) { |
| 4881 | ext4_error(sb, __func__, |
| 4882 | "bad extended attribute block %llu in inode #%lu", |
| 4883 | ei->i_file_acl, inode->i_ino); |
| 4884 | ret = -EIO; |
| 4885 | goto bad_inode; |
| 4886 | } else if (ei->i_flags & EXT4_EXTENTS_FL) { |
Theodore Ts'o | c4b5a61 | 2009-04-24 18:45:35 -0400 | [diff] [blame] | 4887 | if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 4888 | (S_ISLNK(inode->i_mode) && |
| 4889 | !ext4_inode_is_fast_symlink(inode))) |
| 4890 | /* Validate extent which is part of inode */ |
| 4891 | ret = ext4_ext_check_inode(inode); |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4892 | } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 4893 | (S_ISLNK(inode->i_mode) && |
| 4894 | !ext4_inode_is_fast_symlink(inode))) { |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4895 | /* Validate block references which are part of inode */ |
Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 4896 | ret = ext4_check_inode_blockref(inode); |
| 4897 | } |
| 4898 | if (ret) { |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4899 | brelse(bh); |
| 4900 | goto bad_inode; |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 4901 | } |
| 4902 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4903 | if (S_ISREG(inode->i_mode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4904 | inode->i_op = &ext4_file_inode_operations; |
| 4905 | inode->i_fop = &ext4_file_operations; |
| 4906 | ext4_set_aops(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4907 | } else if (S_ISDIR(inode->i_mode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4908 | inode->i_op = &ext4_dir_inode_operations; |
| 4909 | inode->i_fop = &ext4_dir_operations; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4910 | } else if (S_ISLNK(inode->i_mode)) { |
Duane Griffin | e83c139 | 2008-12-19 20:47:15 +0000 | [diff] [blame] | 4911 | if (ext4_inode_is_fast_symlink(inode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4912 | inode->i_op = &ext4_fast_symlink_inode_operations; |
Duane Griffin | e83c139 | 2008-12-19 20:47:15 +0000 | [diff] [blame] | 4913 | nd_terminate_link(ei->i_data, inode->i_size, |
| 4914 | sizeof(ei->i_data) - 1); |
| 4915 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4916 | inode->i_op = &ext4_symlink_inode_operations; |
| 4917 | ext4_set_aops(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4918 | } |
Theodore Ts'o | 563bdd6 | 2009-03-26 00:06:19 -0400 | [diff] [blame] | 4919 | } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 4920 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4921 | inode->i_op = &ext4_special_inode_operations; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4922 | if (raw_inode->i_block[0]) |
| 4923 | init_special_inode(inode, inode->i_mode, |
| 4924 | old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); |
| 4925 | else |
| 4926 | init_special_inode(inode, inode->i_mode, |
| 4927 | new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); |
Theodore Ts'o | 563bdd6 | 2009-03-26 00:06:19 -0400 | [diff] [blame] | 4928 | } else { |
| 4929 | brelse(bh); |
| 4930 | ret = -EIO; |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 4931 | ext4_error(inode->i_sb, __func__, |
Theodore Ts'o | 563bdd6 | 2009-03-26 00:06:19 -0400 | [diff] [blame] | 4932 | "bogus i_mode (%o) for inode=%lu", |
| 4933 | inode->i_mode, inode->i_ino); |
| 4934 | goto bad_inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4935 | } |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4936 | brelse(iloc.bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4937 | ext4_set_inode_flags(inode); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4938 | unlock_new_inode(inode); |
| 4939 | return inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4940 | |
| 4941 | bad_inode: |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4942 | iget_failed(inode); |
| 4943 | return ERR_PTR(ret); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4944 | } |
| 4945 | |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4946 | static int ext4_inode_blocks_set(handle_t *handle, |
| 4947 | struct ext4_inode *raw_inode, |
| 4948 | struct ext4_inode_info *ei) |
| 4949 | { |
| 4950 | struct inode *inode = &(ei->vfs_inode); |
| 4951 | u64 i_blocks = inode->i_blocks; |
| 4952 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4953 | |
| 4954 | if (i_blocks <= ~0U) { |
| 4955 | /* |
| 4956 | * i_blocks can be represnted in a 32 bit variable |
| 4957 | * as multiple of 512 bytes |
| 4958 | */ |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4959 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4960 | raw_inode->i_blocks_high = 0; |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4961 | ei->i_flags &= ~EXT4_HUGE_FILE_FL; |
Theodore Ts'o | f287a1a | 2008-10-16 22:50:48 -0400 | [diff] [blame] | 4962 | return 0; |
| 4963 | } |
| 4964 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) |
| 4965 | return -EFBIG; |
| 4966 | |
| 4967 | if (i_blocks <= 0xffffffffffffULL) { |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4968 | /* |
| 4969 | * i_blocks can be represented in a 48 bit variable |
| 4970 | * as multiple of 512 bytes |
| 4971 | */ |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4972 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4973 | raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4974 | ei->i_flags &= ~EXT4_HUGE_FILE_FL; |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4975 | } else { |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4976 | ei->i_flags |= EXT4_HUGE_FILE_FL; |
| 4977 | /* i_block is stored in file system block size */ |
| 4978 | i_blocks = i_blocks >> (inode->i_blkbits - 9); |
| 4979 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
| 4980 | 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] | 4981 | } |
Theodore Ts'o | f287a1a | 2008-10-16 22:50:48 -0400 | [diff] [blame] | 4982 | return 0; |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4983 | } |
| 4984 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4985 | /* |
| 4986 | * Post the struct inode info into an on-disk inode location in the |
| 4987 | * buffer-cache. This gobbles the caller's reference to the |
| 4988 | * buffer_head in the inode location struct. |
| 4989 | * |
| 4990 | * The caller must have write access to iloc->bh. |
| 4991 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4992 | static int ext4_do_update_inode(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4993 | struct inode *inode, |
Frank Mayhar | 830156c | 2009-09-29 10:07:47 -0400 | [diff] [blame] | 4994 | struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4995 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4996 | struct ext4_inode *raw_inode = ext4_raw_inode(iloc); |
| 4997 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4998 | struct buffer_head *bh = iloc->bh; |
| 4999 | int err = 0, rc, block; |
| 5000 | |
| 5001 | /* For fields not not tracking in the in-memory inode, |
| 5002 | * initialise them to zero for new inodes. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5003 | if (ei->i_state & EXT4_STATE_NEW) |
| 5004 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5005 | |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 5006 | ext4_get_inode_flags(ei); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5007 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 5008 | if (!(test_opt(inode->i_sb, NO_UID32))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5009 | raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); |
| 5010 | raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); |
| 5011 | /* |
| 5012 | * Fix up interoperability with old kernels. Otherwise, old inodes get |
| 5013 | * re-used with the upper 16 bits of the uid/gid intact |
| 5014 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 5015 | if (!ei->i_dtime) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5016 | raw_inode->i_uid_high = |
| 5017 | cpu_to_le16(high_16_bits(inode->i_uid)); |
| 5018 | raw_inode->i_gid_high = |
| 5019 | cpu_to_le16(high_16_bits(inode->i_gid)); |
| 5020 | } else { |
| 5021 | raw_inode->i_uid_high = 0; |
| 5022 | raw_inode->i_gid_high = 0; |
| 5023 | } |
| 5024 | } else { |
| 5025 | raw_inode->i_uid_low = |
| 5026 | cpu_to_le16(fs_high2lowuid(inode->i_uid)); |
| 5027 | raw_inode->i_gid_low = |
| 5028 | cpu_to_le16(fs_high2lowgid(inode->i_gid)); |
| 5029 | raw_inode->i_uid_high = 0; |
| 5030 | raw_inode->i_gid_high = 0; |
| 5031 | } |
| 5032 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 5033 | |
| 5034 | EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); |
| 5035 | EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); |
| 5036 | EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); |
| 5037 | EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); |
| 5038 | |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 5039 | if (ext4_inode_blocks_set(handle, raw_inode, ei)) |
| 5040 | goto out_brelse; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5041 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); |
Theodore Ts'o | 1b9c12f | 2009-09-17 08:32:22 -0400 | [diff] [blame] | 5042 | raw_inode->i_flags = cpu_to_le32(ei->i_flags); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 5043 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != |
| 5044 | cpu_to_le32(EXT4_OS_HURD)) |
Badari Pulavarty | a1ddeb7 | 2006-10-11 01:21:09 -0700 | [diff] [blame] | 5045 | raw_inode->i_file_acl_high = |
| 5046 | cpu_to_le16(ei->i_file_acl >> 32); |
Aneesh Kumar K.V | 7973c0c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 5047 | 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] | 5048 | ext4_isize_set(raw_inode, ei->i_disksize); |
| 5049 | if (ei->i_disksize > 0x7fffffffULL) { |
| 5050 | struct super_block *sb = inode->i_sb; |
| 5051 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 5052 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || |
| 5053 | EXT4_SB(sb)->s_es->s_rev_level == |
| 5054 | cpu_to_le32(EXT4_GOOD_OLD_REV)) { |
| 5055 | /* If this is the first large file |
| 5056 | * created, add a flag to the superblock. |
| 5057 | */ |
| 5058 | err = ext4_journal_get_write_access(handle, |
| 5059 | EXT4_SB(sb)->s_sbh); |
| 5060 | if (err) |
| 5061 | goto out_brelse; |
| 5062 | ext4_update_dynamic_rev(sb); |
| 5063 | EXT4_SET_RO_COMPAT_FEATURE(sb, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5064 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE); |
Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 5065 | sb->s_dirt = 1; |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5066 | ext4_handle_sync(handle); |
| 5067 | err = ext4_handle_dirty_metadata(handle, inode, |
Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 5068 | EXT4_SB(sb)->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5069 | } |
| 5070 | } |
| 5071 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); |
| 5072 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
| 5073 | if (old_valid_dev(inode->i_rdev)) { |
| 5074 | raw_inode->i_block[0] = |
| 5075 | cpu_to_le32(old_encode_dev(inode->i_rdev)); |
| 5076 | raw_inode->i_block[1] = 0; |
| 5077 | } else { |
| 5078 | raw_inode->i_block[0] = 0; |
| 5079 | raw_inode->i_block[1] = |
| 5080 | cpu_to_le32(new_encode_dev(inode->i_rdev)); |
| 5081 | raw_inode->i_block[2] = 0; |
| 5082 | } |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 5083 | } else |
| 5084 | for (block = 0; block < EXT4_N_BLOCKS; block++) |
| 5085 | raw_inode->i_block[block] = ei->i_data[block]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5086 | |
Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 5087 | raw_inode->i_disk_version = cpu_to_le32(inode->i_version); |
| 5088 | if (ei->i_extra_isize) { |
| 5089 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) |
| 5090 | raw_inode->i_version_hi = |
| 5091 | cpu_to_le32(inode->i_version >> 32); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5092 | 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] | 5093 | } |
| 5094 | |
Frank Mayhar | 830156c | 2009-09-29 10:07:47 -0400 | [diff] [blame] | 5095 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
| 5096 | rc = ext4_handle_dirty_metadata(handle, inode, bh); |
| 5097 | if (!err) |
| 5098 | err = rc; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5099 | ei->i_state &= ~EXT4_STATE_NEW; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5100 | |
| 5101 | out_brelse: |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 5102 | brelse(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5103 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5104 | return err; |
| 5105 | } |
| 5106 | |
| 5107 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5108 | * ext4_write_inode() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5109 | * |
| 5110 | * We are called from a few places: |
| 5111 | * |
| 5112 | * - Within generic_file_write() for O_SYNC files. |
| 5113 | * Here, there will be no transaction running. We wait for any running |
| 5114 | * trasnaction to commit. |
| 5115 | * |
| 5116 | * - Within sys_sync(), kupdate and such. |
| 5117 | * We wait on commit, if tol to. |
| 5118 | * |
| 5119 | * - Within prune_icache() (PF_MEMALLOC == true) |
| 5120 | * Here we simply return. We can't afford to block kswapd on the |
| 5121 | * journal commit. |
| 5122 | * |
| 5123 | * In all cases it is actually safe for us to return without doing anything, |
| 5124 | * because the inode has been copied into a raw inode buffer in |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5125 | * 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] | 5126 | * knfsd. |
| 5127 | * |
| 5128 | * Note that we are absolutely dependent upon all inode dirtiers doing the |
| 5129 | * right thing: they *must* call mark_inode_dirty() after dirtying info in |
| 5130 | * which we are interested. |
| 5131 | * |
| 5132 | * It would be a bug for them to not do this. The code: |
| 5133 | * |
| 5134 | * mark_inode_dirty(inode) |
| 5135 | * stuff(); |
| 5136 | * inode->i_size = expr; |
| 5137 | * |
| 5138 | * is in error because a kswapd-driven write_inode() could occur while |
| 5139 | * `stuff()' is running, and the new i_size will be lost. Plus the inode |
| 5140 | * will no longer be on the superblock's dirty inode list. |
| 5141 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5142 | int ext4_write_inode(struct inode *inode, int wait) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5143 | { |
Frank Mayhar | 91ac6f4 | 2009-09-09 22:33:47 -0400 | [diff] [blame] | 5144 | int err; |
| 5145 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5146 | if (current->flags & PF_MEMALLOC) |
| 5147 | return 0; |
| 5148 | |
Frank Mayhar | 91ac6f4 | 2009-09-09 22:33:47 -0400 | [diff] [blame] | 5149 | if (EXT4_SB(inode->i_sb)->s_journal) { |
| 5150 | if (ext4_journal_current_handle()) { |
| 5151 | jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); |
| 5152 | dump_stack(); |
| 5153 | return -EIO; |
| 5154 | } |
| 5155 | |
| 5156 | if (!wait) |
| 5157 | return 0; |
| 5158 | |
| 5159 | err = ext4_force_commit(inode->i_sb); |
| 5160 | } else { |
| 5161 | struct ext4_iloc iloc; |
| 5162 | |
| 5163 | err = ext4_get_inode_loc(inode, &iloc); |
| 5164 | if (err) |
| 5165 | return err; |
Frank Mayhar | 830156c | 2009-09-29 10:07:47 -0400 | [diff] [blame] | 5166 | if (wait) |
| 5167 | sync_dirty_buffer(iloc.bh); |
| 5168 | if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { |
| 5169 | ext4_error(inode->i_sb, __func__, |
| 5170 | "IO error syncing inode, " |
| 5171 | "inode=%lu, block=%llu", |
| 5172 | inode->i_ino, |
| 5173 | (unsigned long long)iloc.bh->b_blocknr); |
| 5174 | err = -EIO; |
| 5175 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5176 | } |
Frank Mayhar | 91ac6f4 | 2009-09-09 22:33:47 -0400 | [diff] [blame] | 5177 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5178 | } |
| 5179 | |
| 5180 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5181 | * ext4_setattr() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5182 | * |
| 5183 | * Called from notify_change. |
| 5184 | * |
| 5185 | * We want to trap VFS attempts to truncate the file as soon as |
| 5186 | * possible. In particular, we want to make sure that when the VFS |
| 5187 | * shrinks i_size, we put the inode on the orphan list and modify |
| 5188 | * i_disksize immediately, so that during the subsequent flushing of |
| 5189 | * dirty pages and freeing of disk blocks, we can guarantee that any |
| 5190 | * commit will leave the blocks being flushed in an unused state on |
| 5191 | * disk. (On recovery, the inode will get truncated and the blocks will |
| 5192 | * be freed, so we have a strong guarantee that no future commit will |
| 5193 | * leave these blocks visible to the user.) |
| 5194 | * |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5195 | * Another thing we have to assure is that if we are in ordered mode |
| 5196 | * and inode is still attached to the committing transaction, we must |
| 5197 | * we start writeout of all the dirty pages which are being truncated. |
| 5198 | * This way we are sure that all the data written in the previous |
| 5199 | * transaction are already on disk (truncate waits for pages under |
| 5200 | * writeback). |
| 5201 | * |
| 5202 | * Called with inode->i_mutex down. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5203 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5204 | int ext4_setattr(struct dentry *dentry, struct iattr *attr) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5205 | { |
| 5206 | struct inode *inode = dentry->d_inode; |
| 5207 | int error, rc = 0; |
| 5208 | const unsigned int ia_valid = attr->ia_valid; |
| 5209 | |
| 5210 | error = inode_change_ok(inode, attr); |
| 5211 | if (error) |
| 5212 | return error; |
| 5213 | |
| 5214 | if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || |
| 5215 | (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { |
| 5216 | handle_t *handle; |
| 5217 | |
| 5218 | /* (user+group)*(old+new) structure, inode write (sb, |
| 5219 | * inode block, ? - but truncate inode update has it) */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5220 | handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+ |
| 5221 | EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5222 | if (IS_ERR(handle)) { |
| 5223 | error = PTR_ERR(handle); |
| 5224 | goto err_out; |
| 5225 | } |
Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 5226 | error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5227 | if (error) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5228 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5229 | return error; |
| 5230 | } |
| 5231 | /* Update corresponding info in inode so that everything is in |
| 5232 | * one transaction */ |
| 5233 | if (attr->ia_valid & ATTR_UID) |
| 5234 | inode->i_uid = attr->ia_uid; |
| 5235 | if (attr->ia_valid & ATTR_GID) |
| 5236 | inode->i_gid = attr->ia_gid; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5237 | error = ext4_mark_inode_dirty(handle, inode); |
| 5238 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5239 | } |
| 5240 | |
Eric Sandeen | e2b4657 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 5241 | if (attr->ia_valid & ATTR_SIZE) { |
| 5242 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) { |
| 5243 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 5244 | |
| 5245 | if (attr->ia_size > sbi->s_bitmap_maxbytes) { |
| 5246 | error = -EFBIG; |
| 5247 | goto err_out; |
| 5248 | } |
| 5249 | } |
| 5250 | } |
| 5251 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5252 | if (S_ISREG(inode->i_mode) && |
| 5253 | attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) { |
| 5254 | handle_t *handle; |
| 5255 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5256 | handle = ext4_journal_start(inode, 3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5257 | if (IS_ERR(handle)) { |
| 5258 | error = PTR_ERR(handle); |
| 5259 | goto err_out; |
| 5260 | } |
| 5261 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5262 | error = ext4_orphan_add(handle, inode); |
| 5263 | EXT4_I(inode)->i_disksize = attr->ia_size; |
| 5264 | rc = ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5265 | if (!error) |
| 5266 | error = rc; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5267 | ext4_journal_stop(handle); |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5268 | |
| 5269 | if (ext4_should_order_data(inode)) { |
| 5270 | error = ext4_begin_ordered_truncate(inode, |
| 5271 | attr->ia_size); |
| 5272 | if (error) { |
| 5273 | /* Do as much error cleanup as possible */ |
| 5274 | handle = ext4_journal_start(inode, 3); |
| 5275 | if (IS_ERR(handle)) { |
| 5276 | ext4_orphan_del(NULL, inode); |
| 5277 | goto err_out; |
| 5278 | } |
| 5279 | ext4_orphan_del(handle, inode); |
| 5280 | ext4_journal_stop(handle); |
| 5281 | goto err_out; |
| 5282 | } |
| 5283 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | rc = inode_setattr(inode, attr); |
| 5287 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5288 | /* If inode_setattr's call to ext4_truncate failed to get a |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5289 | * transaction handle at all, we need to clean up the in-core |
| 5290 | * orphan list manually. */ |
| 5291 | if (inode->i_nlink) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5292 | ext4_orphan_del(NULL, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5293 | |
| 5294 | if (!rc && (ia_valid & ATTR_MODE)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5295 | rc = ext4_acl_chmod(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5296 | |
| 5297 | err_out: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5298 | ext4_std_error(inode->i_sb, error); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5299 | if (!error) |
| 5300 | error = rc; |
| 5301 | return error; |
| 5302 | } |
| 5303 | |
Mingming Cao | 3e3398a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5304 | int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 5305 | struct kstat *stat) |
| 5306 | { |
| 5307 | struct inode *inode; |
| 5308 | unsigned long delalloc_blocks; |
| 5309 | |
| 5310 | inode = dentry->d_inode; |
| 5311 | generic_fillattr(inode, stat); |
| 5312 | |
| 5313 | /* |
| 5314 | * We can't update i_blocks if the block allocation is delayed |
| 5315 | * otherwise in the case of system crash before the real block |
| 5316 | * allocation is done, we will have i_blocks inconsistent with |
| 5317 | * on-disk file blocks. |
| 5318 | * We always keep i_blocks updated together with real |
| 5319 | * allocation. But to not confuse with user, stat |
| 5320 | * will return the blocks that include the delayed allocation |
| 5321 | * blocks for this file. |
| 5322 | */ |
| 5323 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
| 5324 | delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; |
| 5325 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 5326 | |
| 5327 | stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; |
| 5328 | return 0; |
| 5329 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5330 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5331 | static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks, |
| 5332 | int chunk) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5333 | { |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5334 | int indirects; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5335 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5336 | /* if nrblocks are contiguous */ |
| 5337 | if (chunk) { |
| 5338 | /* |
| 5339 | * With N contiguous data blocks, it need at most |
| 5340 | * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks |
| 5341 | * 2 dindirect blocks |
| 5342 | * 1 tindirect block |
| 5343 | */ |
| 5344 | indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb); |
| 5345 | return indirects + 3; |
| 5346 | } |
| 5347 | /* |
| 5348 | * if nrblocks are not contiguous, worse case, each block touch |
| 5349 | * a indirect block, and each indirect block touch a double indirect |
| 5350 | * block, plus a triple indirect block |
| 5351 | */ |
| 5352 | indirects = nrblocks * 2 + 1; |
| 5353 | return indirects; |
| 5354 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 5355 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5356 | static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) |
| 5357 | { |
| 5358 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) |
Theodore Ts'o | ac51d83 | 2008-11-06 16:49:36 -0500 | [diff] [blame] | 5359 | return ext4_indirect_trans_blocks(inode, nrblocks, chunk); |
| 5360 | return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5361 | } |
Theodore Ts'o | ac51d83 | 2008-11-06 16:49:36 -0500 | [diff] [blame] | 5362 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5363 | /* |
| 5364 | * Account for index blocks, block groups bitmaps and block group |
| 5365 | * descriptor blocks if modify datablocks and index blocks |
| 5366 | * worse case, the indexs blocks spread over different block groups |
| 5367 | * |
| 5368 | * If datablocks are discontiguous, they are possible to spread over |
| 5369 | * different block groups too. If they are contiugous, with flexbg, |
| 5370 | * they could still across block group boundary. |
| 5371 | * |
| 5372 | * Also account for superblock, inode, quota and xattr blocks |
| 5373 | */ |
| 5374 | int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) |
| 5375 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 5376 | ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); |
| 5377 | int gdpblocks; |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5378 | int idxblocks; |
| 5379 | int ret = 0; |
| 5380 | |
| 5381 | /* |
| 5382 | * How many index blocks need to touch to modify nrblocks? |
| 5383 | * The "Chunk" flag indicating whether the nrblocks is |
| 5384 | * physically contiguous on disk |
| 5385 | * |
| 5386 | * For Direct IO and fallocate, they calls get_block to allocate |
| 5387 | * one single extent at a time, so they could set the "Chunk" flag |
| 5388 | */ |
| 5389 | idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); |
| 5390 | |
| 5391 | ret = idxblocks; |
| 5392 | |
| 5393 | /* |
| 5394 | * Now let's see how many group bitmaps and group descriptors need |
| 5395 | * to account |
| 5396 | */ |
| 5397 | groups = idxblocks; |
| 5398 | if (chunk) |
| 5399 | groups += 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5400 | else |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5401 | groups += nrblocks; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5402 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5403 | gdpblocks = groups; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 5404 | if (groups > ngroups) |
| 5405 | groups = ngroups; |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5406 | if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) |
| 5407 | gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; |
| 5408 | |
| 5409 | /* bitmaps and block group descriptor blocks */ |
| 5410 | ret += groups + gdpblocks; |
| 5411 | |
| 5412 | /* Blocks for super block, inode, quota and xattr blocks */ |
| 5413 | ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5414 | |
| 5415 | return ret; |
| 5416 | } |
| 5417 | |
| 5418 | /* |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5419 | * Calulate the total number of credits to reserve to fit |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 5420 | * the modification of a single pages into a single transaction, |
| 5421 | * which may include multiple chunks of block allocations. |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5422 | * |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 5423 | * This could be called via ext4_write_begin() |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5424 | * |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 5425 | * We need to consider the worse case, when |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5426 | * one new block per extent. |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5427 | */ |
| 5428 | int ext4_writepage_trans_blocks(struct inode *inode) |
| 5429 | { |
| 5430 | int bpp = ext4_journal_blocks_per_page(inode); |
| 5431 | int ret; |
| 5432 | |
| 5433 | ret = ext4_meta_trans_blocks(inode, bpp, 0); |
| 5434 | |
| 5435 | /* Account for data blocks for journalled mode */ |
| 5436 | if (ext4_should_journal_data(inode)) |
| 5437 | ret += bpp; |
| 5438 | return ret; |
| 5439 | } |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 5440 | |
| 5441 | /* |
| 5442 | * Calculate the journal credits for a chunk of data modification. |
| 5443 | * |
| 5444 | * This is called from DIO, fallocate or whoever calling |
Theodore Ts'o | 12b7ac1 | 2009-05-14 00:57:44 -0400 | [diff] [blame] | 5445 | * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks. |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 5446 | * |
| 5447 | * journal buffers for data blocks are not included here, as DIO |
| 5448 | * and fallocate do no need to journal data buffers. |
| 5449 | */ |
| 5450 | int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) |
| 5451 | { |
| 5452 | return ext4_meta_trans_blocks(inode, nrblocks, 1); |
| 5453 | } |
| 5454 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5455 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5456 | * The caller must have previously called ext4_reserve_inode_write(). |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5457 | * Give this, we know that the caller already has write access to iloc->bh. |
| 5458 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5459 | int ext4_mark_iloc_dirty(handle_t *handle, |
Theodore Ts'o | de9a55b | 2009-06-14 17:45:34 -0400 | [diff] [blame] | 5460 | struct inode *inode, struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5461 | { |
| 5462 | int err = 0; |
| 5463 | |
Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 5464 | if (test_opt(inode->i_sb, I_VERSION)) |
| 5465 | inode_inc_iversion(inode); |
| 5466 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5467 | /* the do_update_inode consumes one bh->b_count */ |
| 5468 | get_bh(iloc->bh); |
| 5469 | |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 5470 | /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ |
Frank Mayhar | 830156c | 2009-09-29 10:07:47 -0400 | [diff] [blame] | 5471 | err = ext4_do_update_inode(handle, inode, iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5472 | put_bh(iloc->bh); |
| 5473 | return err; |
| 5474 | } |
| 5475 | |
| 5476 | /* |
| 5477 | * On success, We end up with an outstanding reference count against |
| 5478 | * iloc->bh. This _must_ be cleaned up later. |
| 5479 | */ |
| 5480 | |
| 5481 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5482 | ext4_reserve_inode_write(handle_t *handle, struct inode *inode, |
| 5483 | struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5484 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5485 | int err; |
| 5486 | |
| 5487 | err = ext4_get_inode_loc(inode, iloc); |
| 5488 | if (!err) { |
| 5489 | BUFFER_TRACE(iloc->bh, "get_write_access"); |
| 5490 | err = ext4_journal_get_write_access(handle, iloc->bh); |
| 5491 | if (err) { |
| 5492 | brelse(iloc->bh); |
| 5493 | iloc->bh = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5494 | } |
| 5495 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5496 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5497 | return err; |
| 5498 | } |
| 5499 | |
| 5500 | /* |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5501 | * Expand an inode by new_extra_isize bytes. |
| 5502 | * Returns 0 on success or negative error number on failure. |
| 5503 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 5504 | static int ext4_expand_extra_isize(struct inode *inode, |
| 5505 | unsigned int new_extra_isize, |
| 5506 | struct ext4_iloc iloc, |
| 5507 | handle_t *handle) |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5508 | { |
| 5509 | struct ext4_inode *raw_inode; |
| 5510 | struct ext4_xattr_ibody_header *header; |
| 5511 | struct ext4_xattr_entry *entry; |
| 5512 | |
| 5513 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) |
| 5514 | return 0; |
| 5515 | |
| 5516 | raw_inode = ext4_raw_inode(&iloc); |
| 5517 | |
| 5518 | header = IHDR(inode, raw_inode); |
| 5519 | entry = IFIRST(header); |
| 5520 | |
| 5521 | /* No extended attributes present */ |
| 5522 | if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) || |
| 5523 | header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { |
| 5524 | memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, |
| 5525 | new_extra_isize); |
| 5526 | EXT4_I(inode)->i_extra_isize = new_extra_isize; |
| 5527 | return 0; |
| 5528 | } |
| 5529 | |
| 5530 | /* try to expand with EAs present */ |
| 5531 | return ext4_expand_extra_isize_ea(inode, new_extra_isize, |
| 5532 | raw_inode, handle); |
| 5533 | } |
| 5534 | |
| 5535 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5536 | * What we do here is to mark the in-core inode as clean with respect to inode |
| 5537 | * dirtiness (it may still be data-dirty). |
| 5538 | * This means that the in-core inode may be reaped by prune_icache |
| 5539 | * without having to perform any I/O. This is a very good thing, |
| 5540 | * because *any* task may call prune_icache - even ones which |
| 5541 | * have a transaction open against a different journal. |
| 5542 | * |
| 5543 | * Is this cheating? Not really. Sure, we haven't written the |
| 5544 | * inode out, but prune_icache isn't a user-visible syncing function. |
| 5545 | * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) |
| 5546 | * we start and wait on commits. |
| 5547 | * |
| 5548 | * Is this efficient/effective? Well, we're being nice to the system |
| 5549 | * by cleaning up our inodes proactively so they can be reaped |
| 5550 | * without I/O. But we are potentially leaving up to five seconds' |
| 5551 | * worth of inodes floating about which prune_icache wants us to |
| 5552 | * write out. One way to fix that would be to get prune_icache() |
| 5553 | * to do a write_super() to free up some memory. It has the desired |
| 5554 | * effect. |
| 5555 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5556 | int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5557 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5558 | struct ext4_iloc iloc; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5559 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 5560 | static unsigned int mnt_count; |
| 5561 | int err, ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5562 | |
| 5563 | might_sleep(); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5564 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5565 | if (ext4_handle_valid(handle) && |
| 5566 | EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5567 | !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) { |
| 5568 | /* |
| 5569 | * We need extra buffer credits since we may write into EA block |
| 5570 | * with this same handle. If journal_extend fails, then it will |
| 5571 | * only result in a minor loss of functionality for that inode. |
| 5572 | * If this is felt to be critical, then e2fsck should be run to |
| 5573 | * force a large enough s_min_extra_isize. |
| 5574 | */ |
| 5575 | if ((jbd2_journal_extend(handle, |
| 5576 | EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { |
| 5577 | ret = ext4_expand_extra_isize(inode, |
| 5578 | sbi->s_want_extra_isize, |
| 5579 | iloc, handle); |
| 5580 | if (ret) { |
| 5581 | EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND; |
Aneesh Kumar K.V | c1bddad | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 5582 | if (mnt_count != |
| 5583 | le16_to_cpu(sbi->s_es->s_mnt_count)) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 5584 | ext4_warning(inode->i_sb, __func__, |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5585 | "Unable to expand inode %lu. Delete" |
| 5586 | " some EAs or run e2fsck.", |
| 5587 | inode->i_ino); |
Aneesh Kumar K.V | c1bddad | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 5588 | mnt_count = |
| 5589 | le16_to_cpu(sbi->s_es->s_mnt_count); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5590 | } |
| 5591 | } |
| 5592 | } |
| 5593 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5594 | if (!err) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5595 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5596 | return err; |
| 5597 | } |
| 5598 | |
| 5599 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5600 | * ext4_dirty_inode() is called from __mark_inode_dirty() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5601 | * |
| 5602 | * We're really interested in the case where a file is being extended. |
| 5603 | * i_size has been changed by generic_commit_write() and we thus need |
| 5604 | * to include the updated inode in the current transaction. |
| 5605 | * |
Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 5606 | * Also, vfs_dq_alloc_block() will always dirty the inode when blocks |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5607 | * are allocated to the file. |
| 5608 | * |
| 5609 | * If the inode is marked synchronous, we don't honour that here - doing |
| 5610 | * so would cause a commit on atime updates, which we don't bother doing. |
| 5611 | * We handle synchronous inodes at the highest possible level. |
| 5612 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5613 | void ext4_dirty_inode(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5614 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5615 | handle_t *current_handle = ext4_journal_current_handle(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5616 | handle_t *handle; |
| 5617 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5618 | handle = ext4_journal_start(inode, 2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5619 | if (IS_ERR(handle)) |
| 5620 | goto out; |
Curt Wohlgemuth | f3dc272 | 2009-09-29 16:06:01 -0400 | [diff] [blame^] | 5621 | |
| 5622 | jbd_debug(5, "marking dirty. outer handle=%p\n", current_handle); |
| 5623 | ext4_mark_inode_dirty(handle, inode); |
| 5624 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5625 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5626 | out: |
| 5627 | return; |
| 5628 | } |
| 5629 | |
| 5630 | #if 0 |
| 5631 | /* |
| 5632 | * Bind an inode's backing buffer_head into this transaction, to prevent |
| 5633 | * it from being flushed to disk early. Unlike |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5634 | * ext4_reserve_inode_write, this leaves behind no bh reference and |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5635 | * returns no iloc structure, so the caller needs to repeat the iloc |
| 5636 | * lookup to mark the inode dirty later. |
| 5637 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5638 | static int ext4_pin_inode(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5639 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5640 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5641 | |
| 5642 | int err = 0; |
| 5643 | if (handle) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5644 | err = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5645 | if (!err) { |
| 5646 | BUFFER_TRACE(iloc.bh, "get_write_access"); |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 5647 | err = jbd2_journal_get_write_access(handle, iloc.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5648 | if (!err) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5649 | err = ext4_handle_dirty_metadata(handle, |
| 5650 | inode, |
| 5651 | iloc.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5652 | brelse(iloc.bh); |
| 5653 | } |
| 5654 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5655 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5656 | return err; |
| 5657 | } |
| 5658 | #endif |
| 5659 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5660 | int ext4_change_inode_journal_flag(struct inode *inode, int val) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5661 | { |
| 5662 | journal_t *journal; |
| 5663 | handle_t *handle; |
| 5664 | int err; |
| 5665 | |
| 5666 | /* |
| 5667 | * We have to be very careful here: changing a data block's |
| 5668 | * journaling status dynamically is dangerous. If we write a |
| 5669 | * data block to the journal, change the status and then delete |
| 5670 | * that block, we risk forgetting to revoke the old log record |
| 5671 | * from the journal and so a subsequent replay can corrupt data. |
| 5672 | * So, first we make sure that the journal is empty and that |
| 5673 | * nobody is changing anything. |
| 5674 | */ |
| 5675 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5676 | journal = EXT4_JOURNAL(inode); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5677 | if (!journal) |
| 5678 | return 0; |
Dave Hansen | d699594 | 2007-07-18 08:33:51 -0400 | [diff] [blame] | 5679 | if (is_journal_aborted(journal)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5680 | return -EROFS; |
| 5681 | |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 5682 | jbd2_journal_lock_updates(journal); |
| 5683 | jbd2_journal_flush(journal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5684 | |
| 5685 | /* |
| 5686 | * OK, there are no updates running now, and all cached data is |
| 5687 | * synced to disk. We are now in a completely consistent state |
| 5688 | * which doesn't have anything in the journal, and we know that |
| 5689 | * no filesystem updates are running, so it is safe to modify |
| 5690 | * the inode's in-core data-journaling state flag now. |
| 5691 | */ |
| 5692 | |
| 5693 | if (val) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5694 | EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5695 | else |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5696 | EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL; |
| 5697 | ext4_set_aops(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5698 | |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 5699 | jbd2_journal_unlock_updates(journal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5700 | |
| 5701 | /* Finally we can mark the inode as dirty. */ |
| 5702 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5703 | handle = ext4_journal_start(inode, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5704 | if (IS_ERR(handle)) |
| 5705 | return PTR_ERR(handle); |
| 5706 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5707 | err = ext4_mark_inode_dirty(handle, inode); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5708 | ext4_handle_sync(handle); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5709 | ext4_journal_stop(handle); |
| 5710 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5711 | |
| 5712 | return err; |
| 5713 | } |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5714 | |
| 5715 | static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) |
| 5716 | { |
| 5717 | return !buffer_mapped(bh); |
| 5718 | } |
| 5719 | |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5720 | 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] | 5721 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5722 | struct page *page = vmf->page; |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5723 | loff_t size; |
| 5724 | unsigned long len; |
| 5725 | int ret = -EINVAL; |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 5726 | void *fsdata; |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5727 | struct file *file = vma->vm_file; |
| 5728 | struct inode *inode = file->f_path.dentry->d_inode; |
| 5729 | struct address_space *mapping = inode->i_mapping; |
| 5730 | |
| 5731 | /* |
| 5732 | * Get i_alloc_sem to stop truncates messing with the inode. We cannot |
| 5733 | * get i_mutex because we are already holding mmap_sem. |
| 5734 | */ |
| 5735 | down_read(&inode->i_alloc_sem); |
| 5736 | size = i_size_read(inode); |
| 5737 | if (page->mapping != mapping || size <= page_offset(page) |
| 5738 | || !PageUptodate(page)) { |
| 5739 | /* page got truncated from under us? */ |
| 5740 | goto out_unlock; |
| 5741 | } |
| 5742 | ret = 0; |
| 5743 | if (PageMappedToDisk(page)) |
| 5744 | goto out_unlock; |
| 5745 | |
| 5746 | if (page->index == size >> PAGE_CACHE_SHIFT) |
| 5747 | len = size & ~PAGE_CACHE_MASK; |
| 5748 | else |
| 5749 | len = PAGE_CACHE_SIZE; |
| 5750 | |
Aneesh Kumar K.V | a827eaf | 2009-09-09 22:36:03 -0400 | [diff] [blame] | 5751 | lock_page(page); |
| 5752 | /* |
| 5753 | * return if we have all the buffers mapped. This avoid |
| 5754 | * the need to call write_begin/write_end which does a |
| 5755 | * journal_start/journal_stop which can block and take |
| 5756 | * long time |
| 5757 | */ |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5758 | if (page_has_buffers(page)) { |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5759 | 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] | 5760 | ext4_bh_unmapped)) { |
| 5761 | unlock_page(page); |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5762 | goto out_unlock; |
Aneesh Kumar K.V | a827eaf | 2009-09-09 22:36:03 -0400 | [diff] [blame] | 5763 | } |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5764 | } |
Aneesh Kumar K.V | a827eaf | 2009-09-09 22:36:03 -0400 | [diff] [blame] | 5765 | unlock_page(page); |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5766 | /* |
| 5767 | * OK, we need to fill the hole... Do write_begin write_end |
| 5768 | * to do block allocation/reservation.We are not holding |
| 5769 | * inode.i__mutex here. That allow * parallel write_begin, |
| 5770 | * write_end call. lock_page prevent this from happening |
| 5771 | * on the same page though |
| 5772 | */ |
| 5773 | 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] | 5774 | len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5775 | if (ret < 0) |
| 5776 | goto out_unlock; |
| 5777 | 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] | 5778 | len, len, page, fsdata); |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5779 | if (ret < 0) |
| 5780 | goto out_unlock; |
| 5781 | ret = 0; |
| 5782 | out_unlock: |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5783 | if (ret) |
| 5784 | ret = VM_FAULT_SIGBUS; |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5785 | up_read(&inode->i_alloc_sem); |
| 5786 | return ret; |
| 5787 | } |